]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/attribute.c
Sync with HEAD
[openldap] / servers / slapd / back-meta / attribute.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/socket.h>
28 #include <ac/string.h>
29
30 #include "slap.h"
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
33
34
35 /* return 0 IFF we can retrieve the attributes
36  * of entry with e_ndn
37  */
38
39 /*
40  * FIXME: I never testd this function; I know it compiles ... :)
41  */
42 int
43 meta_back_attribute(
44                 Backend                 *be,
45                 Connection              *conn,
46                 Operation               *op,
47                 Entry                   *target,
48                 struct berval           *ndn,
49                 AttributeDescription    *entry_at,
50                 BerVarray                       *vals
51 )
52 {
53         struct metainfo *li = ( struct metainfo * )be->be_private;    
54         int rc = 1, i, j, count, is_oc, candidate;
55         Attribute *attr;
56         BerVarray abv, v;
57         char **vs; 
58         struct berval   mapped;
59         LDAPMessage     *result, *e;
60         char *gattr[ 2 ];
61         LDAP *ld;
62
63         *vals = NULL;
64         if ( target != NULL && dn_match( &target->e_nname, ndn ) ) {
65                 /* we already have a copy of the entry */
66                 /* attribute and objectclass mapping has already been done */
67                 attr = attr_find( target->e_attrs, entry_at );
68                 if ( attr == NULL ) {
69                         return 1;
70                 }
71
72                 for ( count = 0; attr->a_vals[ count ].bv_val != NULL; count++ )
73                         ;
74                 v = ( BerVarray )ch_calloc( ( count + 1 ), sizeof( struct berval ) );
75                 if ( v == NULL ) {
76                         return 1;
77                 }
78
79                 for ( j = 0, abv = attr->a_vals; --count >= 0; abv++ ) {
80                         if ( abv->bv_len > 0 ) {
81                                 ber_dupbv( &v[ j ], abv );
82                                 if ( v[ j ].bv_val == NULL ) {
83                                         break;
84                                 }
85                         }
86                 }
87                 v[ j ].bv_val = NULL;
88                 *vals = v;
89
90                 return 0;
91         } /* else */
92
93         candidate = meta_back_select_unique_candidate( li, ndn );
94         if ( candidate == META_TARGET_NONE ) {
95                 return 1;
96         }
97
98         ldap_back_map( &li->targets[ candidate ]->at_map,
99                         &entry_at->ad_cname, &mapped, BACKLDAP_MAP );
100         if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' )
101                 return 1;
102
103         rc =  ldap_initialize( &ld, li->targets[ candidate ]->uri );
104         if ( rc != LDAP_SUCCESS ) {
105                 return 1;
106         }
107
108         rc = ldap_bind_s( ld, li->targets[ candidate ]->binddn.bv_val,
109                         li->targets[ candidate ]->bindpw.bv_val, LDAP_AUTH_SIMPLE );
110         if ( rc != LDAP_SUCCESS) {
111                 return 1;
112         }
113
114         gattr[ 0 ] = mapped.bv_val;
115         gattr[ 1 ] = NULL;
116         if ( ldap_search_ext_s( ld, ndn->bv_val, LDAP_SCOPE_BASE, 
117                                 "(objectClass=*)",
118                                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
119                                 LDAP_NO_LIMIT, &result) == LDAP_SUCCESS) {
120                 if ( ( e = ldap_first_entry( ld, result ) ) != NULL ) {
121                         vs = ldap_get_values( ld, e, mapped.bv_val );
122                         if ( vs != NULL ) {
123                                 for ( count = 0; vs[ count ] != NULL;
124                                                 count++ ) { }
125                                 v = ( BerVarray )ch_calloc( ( count + 1 ),
126                                                 sizeof( struct berval ) );
127                                 if ( v == NULL ) {
128                                         ldap_value_free( vs );
129                                 } else {
130                                         is_oc = ( strcasecmp( "objectclass", mapped.bv_val ) == 0 );
131                                         for ( i = 0, j = 0; i < count; i++ ) {
132                                                 ber_str2bv( vs[ i ], 0, 0, &v[ j ] );
133                                                 if ( !is_oc ) {
134                                                         if ( v[ j ].bv_val == NULL ) {
135                                                                 ch_free( vs[ i ] );
136                                                         } else {
137                                                                 j++;
138                                                         }
139                                                 } else {
140                                                         ldap_back_map( &li->targets[ candidate ]->oc_map, &v[ j ], &mapped, BACKLDAP_REMAP );
141                                                         if ( mapped.bv_val && mapped.bv_val[0] != '\0' ) {
142                                                                 ber_dupbv( &v[ j ], &mapped );
143                                                                 if ( v[ j ].bv_val ) {
144                                                                         j++;
145                                                                 }
146                                                         }
147                                                         ch_free( vs[ i ] );
148                                                 }
149                                         }
150                                         v[ j ].bv_val = NULL;
151                                         *vals = v;
152                                         rc = 0;
153                                         ch_free( vs );
154                                 }
155                         }
156                 }
157                 ldap_msgfree( result );
158         }
159         ldap_unbind( ld );
160
161         return(rc);
162 }
163