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