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