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