]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/compare.c
Sync with HEAD
[openldap] / servers / slapd / back-meta / compare.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/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
33
34 int
35 meta_back_compare( Operation *op, SlapReply *rs )
36 {
37         struct metainfo         *li = ( struct metainfo * )op->o_bd->be_private;
38         struct metaconn         *lc;
39         struct metasingleconn   *lsc;
40         char                    *match = NULL,
41                                 *err = NULL;
42         struct berval           mmatch = BER_BVNULL;
43         int                     candidates = 0,
44                                 last = 0,
45                                 i,
46                                 count = 0,
47                                 rc,
48                                 cres = LDAP_SUCCESS,
49                                 rres = LDAP_SUCCESS,
50                                 *msgid;
51         dncookie                dc;
52
53         lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
54                         &op->o_req_ndn, NULL, LDAP_BACK_SENDERR );
55         if ( !lc || !meta_back_dobind( lc, op, LDAP_BACK_SENDERR ) ) {
56                 return rs->sr_err;
57         }
58         
59         msgid = ch_calloc( sizeof( int ), li->ntargets );
60         if ( msgid == NULL ) {
61                 return -1;
62         }
63
64         /*
65          * start an asynchronous compare for each candidate target
66          */
67         dc.conn = op->o_conn;
68         dc.rs = rs;
69         dc.ctx = "compareDN";
70
71         for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
72                 struct berval mdn = BER_BVNULL;
73                 struct berval mapped_attr = op->orc_ava->aa_desc->ad_cname;
74                 struct berval mapped_value = op->orc_ava->aa_value;
75
76                 if ( lsc->msc_candidate != META_CANDIDATE ) {
77                         msgid[ i ] = -1;
78                         continue;
79                 }
80
81                 /*
82                  * Rewrite the compare dn, if needed
83                  */
84                 dc.rwmap = &li->targets[ i ]->mt_rwmap;
85
86                 switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
87                 case LDAP_UNWILLING_TO_PERFORM:
88                         rc = 1;
89                         goto finish;
90
91                 default:
92                         break;
93                 }
94
95                 /*
96                  * if attr is objectClass, try to remap the value
97                  */
98                 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
99                         ldap_back_map( &li->targets[ i ]->mt_rwmap.rwm_oc,
100                                         &op->orc_ava->aa_value,
101                                         &mapped_value, BACKLDAP_MAP );
102
103                         if ( mapped_value.bv_val == NULL || mapped_value.bv_val[0] == '\0' ) {
104                                 continue;
105                         }
106                 /*
107                  * else try to remap the attribute
108                  */
109                 } else {
110                         ldap_back_map( &li->targets[ i ]->mt_rwmap.rwm_at,
111                                 &op->orc_ava->aa_desc->ad_cname,
112                                 &mapped_attr, BACKLDAP_MAP );
113                         if ( mapped_attr.bv_val == NULL || mapped_attr.bv_val[0] == '\0' ) {
114                                 continue;
115                         }
116
117                         if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
118                         {
119                                 dc.ctx = "compareAttrDN";
120
121                                 switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
122                                 {
123                                 case LDAP_UNWILLING_TO_PERFORM:
124                                         rc = 1;
125                                         goto finish;
126
127                                 default:
128                                         break;
129                                 }
130                         }
131                 }
132                 
133                 /*
134                  * the compare op is spawned across the targets and the first
135                  * that returns determines the result; a constraint on unicity
136                  * of the result ought to be enforced
137                  */
138                  rc = ldap_compare_ext( lc->mc_conns[ i ].msc_ld, mdn.bv_val,
139                                 mapped_attr.bv_val, &mapped_value,
140                                 op->o_ctrls, NULL, &msgid[ i ] );
141
142                 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
143                         free( mdn.bv_val );
144                         BER_BVZERO( &mdn );
145                 }
146
147                 if ( mapped_attr.bv_val != op->orc_ava->aa_desc->ad_cname.bv_val ) {
148                         free( mapped_attr.bv_val );
149                         BER_BVZERO( &mapped_attr );
150                 }
151
152                 if ( mapped_value.bv_val != op->orc_ava->aa_value.bv_val ) {
153                         free( mapped_value.bv_val );
154                         BER_BVZERO( &mapped_value );
155                 }
156
157                 if ( rc != LDAP_SUCCESS ) {
158                         /* FIXME: what should we do with the error? */
159                         continue;
160                 }
161
162                 ++candidates;
163         }
164
165         /*
166          * wait for replies
167          */
168         for ( rc = 0, count = 0; candidates > 0; ) {
169
170                 /*
171                  * FIXME: should we check for abandon?
172                  */
173                 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); lsc++, i++ ) {
174                         int             lrc;
175                         LDAPMessage     *res = NULL;
176
177                         if ( msgid[ i ] == -1 ) {
178                                 continue;
179                         }
180
181                         lrc = ldap_result( lsc->msc_ld, msgid[ i ],
182                                         0, NULL, &res );
183
184                         if ( lrc == 0 ) {
185                                 /*
186                                  * FIXME: should we yield?
187                                  */
188                                 if ( res ) {
189                                         ldap_msgfree( res );
190                                 }
191                                 continue;
192
193                         } else if ( lrc == LDAP_RES_COMPARE ) {
194                                 if ( count > 0 ) {
195                                         rres = LDAP_OTHER;
196                                         rc = -1;
197                                         goto finish;
198                                 }
199
200                                 rc = ldap_parse_result( lsc->msc_ld, res,
201                                                 &rs->sr_err,
202                                                 NULL, NULL, NULL, NULL, 1 );
203                                 if ( rc != LDAP_SUCCESS ) {
204                                         rres = rc;
205                                         rc = -1;
206                                         goto finish;
207                                 }
208                                 
209                                 switch ( rs->sr_err ) {
210                                 case LDAP_COMPARE_TRUE:
211                                 case LDAP_COMPARE_FALSE:
212
213                                         /*
214                                          * true or flase, got it;
215                                          * sending to cache ...
216                                          */
217                                         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
218                                                 ( void )meta_dncache_update_entry( &li->cache, &op->o_req_ndn, i );
219                                         }
220
221                                         count++;
222                                         rc = 0;
223                                         break;
224
225                                 default:
226                                         rres = slap_map_api2result( rs );
227
228                                         if ( err != NULL ) {
229                                                 free( err );
230                                         }
231                                         ldap_get_option( lsc->msc_ld,
232                                                 LDAP_OPT_ERROR_STRING, &err );
233
234                                         if ( match != NULL ) {
235                                                 free( match );
236                                         }
237                                         ldap_get_option( lsc->msc_ld,
238                                                 LDAP_OPT_MATCHED_DN, &match );
239                                         
240                                         last = i;
241                                         break;
242                                 }
243                                 msgid[ i ] = -1;
244                                 --candidates;
245
246                         } else {
247                                 msgid[ i ] = -1;
248                                 --candidates;
249                                 if ( res ) {
250                                         ldap_msgfree( res );
251                                 }
252                                 break;
253                         }
254                 }
255         }
256
257 finish:;
258
259         /*
260          * Rewrite the matched portion of the search base, if required
261          * 
262          * FIXME: only the last one gets caught!
263          */
264         if ( count == 1 ) {
265                 if ( match != NULL ) {
266                         free( match );
267                         match = NULL;
268                 }
269                 
270                 /*
271                  * the result of the compare is assigned to the res code
272                  * that will be returned
273                  */
274                 rres = cres;
275                 
276                 /*
277                  * At least one compare failed with matched portion,
278                  * and none was successful
279                  */
280         } else if ( match != NULL &&  match[0] != '\0' ) {
281                 struct berval matched;
282
283                 matched.bv_val = match;
284                 matched.bv_len = strlen( match );
285
286                 dc.ctx = "matchedDN";
287                 ldap_back_dn_massage( &dc, &matched, &mmatch );
288         }
289
290         if ( rres != LDAP_SUCCESS ) {
291                 rs->sr_err = rres;
292         }
293         rs->sr_matched = mmatch.bv_val;
294         send_ldap_result( op, rs );
295         rs->sr_matched = NULL;
296
297         if ( match != NULL ) {
298                 if ( mmatch.bv_val != match ) {
299                         free( mmatch.bv_val );
300                 }
301                 free( match );
302         }
303
304         if ( msgid ) {
305                 free( msgid );
306         }
307         
308         return rc;
309 }
310