]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
reflect Kurt's comments on ID assertion
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2004 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31
32 #include "slap.h"
33 #include "back-ldap.h"
34 #undef ldap_debug       /* silence a warning in ldap-int.h */
35 #include "../../../libraries/libldap/ldap-int.h"
36
37 #include "lutil.h"
38
39 static int
40 ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
41          struct berval *bdn, int flags );
42 #define LDAP_BUILD_ENTRY_PRIVATE        0x01
43 #define LDAP_BUILD_ENTRY_NORMALIZE      0x02
44
45 static struct berval dummy = BER_BVNULL;
46
47 int
48 ldap_back_search(
49     Operation   *op,
50     SlapReply *rs )
51 {
52         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
53         struct ldapconn *lc;
54         struct timeval  tv;
55         LDAPMessage             *res, *e;
56         int     rc = 0, msgid; 
57         struct berval match = BER_BVNULL;
58         char **mapped_attrs = NULL;
59         struct berval mbase;
60         struct berval mfilter = BER_BVNULL;
61         int dontfreetext = 0;
62         dncookie dc;
63         LDAPControl **ctrls = NULL;
64
65         lc = ldap_back_getconn(op, rs);
66         if ( !lc ) {
67                 return( -1 );
68         }
69
70         /*
71          * FIXME: in case of values return filter, we might want
72          * to map attrs and maybe rewrite value
73          */
74         if ( !ldap_back_dobind( lc, op, rs ) ) {
75                 return( -1 );
76         }
77
78         /* should we check return values? */
79         if (op->ors_deref != -1)
80                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->ors_deref);
81         if (op->ors_tlimit != -1) {
82                 tv.tv_sec = op->ors_tlimit;
83                 tv.tv_usec = 0;
84         } else {
85                 tv.tv_sec = 0;
86         }
87
88         /*
89          * Rewrite the search base, if required
90          */
91         dc.rwmap = &li->rwmap;
92 #ifdef ENABLE_REWRITE
93         dc.conn = op->o_conn;
94         dc.rs = rs;
95         dc.ctx = "searchBase";
96 #else
97         dc.tofrom = 1;
98         dc.normalized = 0;
99 #endif
100         if ( ldap_back_dn_massage( &dc, &op->o_req_ndn, &mbase ) ) {
101                 send_ldap_result( op, rs );
102                 return -1;
103         }
104
105         rc = ldap_back_filter_map_rewrite( &dc, op->ors_filter,
106                         &mfilter, BACKLDAP_MAP );
107
108         switch ( rc ) {
109         case LDAP_SUCCESS:
110                 break;
111
112         case LDAP_COMPARE_FALSE:
113                 rs->sr_err = LDAP_SUCCESS;
114                 rs->sr_text = NULL;
115                 rc = 0;
116                 goto finish;
117
118         default:
119                 rs->sr_err = LDAP_OTHER;
120                 rs->sr_text = "Rewrite error";
121                 dontfreetext = 1;
122                 rc = -1;
123                 goto finish;
124         }
125
126         rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
127                         op->ors_attrs,
128                         BACKLDAP_MAP, &mapped_attrs );
129         if ( rs->sr_err ) {
130                 rc = -1;
131                 goto finish;
132         }
133
134         ctrls = op->o_ctrls;
135 #ifdef LDAP_BACK_PROXY_AUTHZ
136         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
137         if ( rc != LDAP_SUCCESS ) {
138                 dontfreetext = 1;
139                 goto finish;
140         }
141 #endif /* LDAP_BACK_PROXY_AUTHZ */
142         
143         rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
144                         op->ors_scope, mfilter.bv_val,
145                         mapped_attrs, op->ors_attrsonly,
146                         ctrls, NULL,
147                         tv.tv_sec ? &tv : NULL, op->ors_slimit,
148                         &msgid );
149
150         if ( rs->sr_err != LDAP_SUCCESS ) {
151 fail:;
152                 rc = ldap_back_op_result(lc, op, rs, msgid, 0);
153                 goto finish;
154         }
155
156         /* We pull apart the ber result, stuff it into a slapd entry, and
157          * let send_search_entry stuff it back into ber format. Slow & ugly,
158          * but this is necessary for version matching, and for ACL processing.
159          */
160
161         for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
162         {
163                 /* check for abandon */
164                 if (op->o_abandon) {
165                         ldap_abandon(lc->ld, msgid);
166                         rc = 0;
167                         goto finish;
168                 }
169
170                 if (rc == 0) {
171                         tv.tv_sec = 0;
172                         tv.tv_usec = 100000;
173                         ldap_pvt_thread_yield();
174
175                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
176                         Entry ent = {0};
177                         struct berval bdn;
178                         int abort = 0;
179                         e = ldap_first_entry(lc->ld,res);
180                         if ( ( rc = ldap_build_entry(op, e, &ent, &bdn,
181                                                 LDAP_BUILD_ENTRY_PRIVATE)) == LDAP_SUCCESS ) {
182                                 rs->sr_entry = &ent;
183                                 rs->sr_attrs = op->ors_attrs;
184                                 rs->sr_flags = 0;
185                                 abort = send_search_entry( op, rs );
186                                 while (ent.e_attrs) {
187                                         Attribute *a;
188                                         BerVarray v;
189
190                                         a = ent.e_attrs;
191                                         ent.e_attrs = a->a_next;
192
193                                         v = a->a_vals;
194                                         if (a->a_vals != &dummy)
195                                                 ber_bvarray_free(a->a_vals);
196                                         if (a->a_nvals != v)
197                                                 ber_bvarray_free(a->a_nvals);
198                                         ch_free(a);
199                                 }
200                                 
201                                 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
202                                         free( ent.e_dn );
203                                 if ( ent.e_ndn )
204                                         free( ent.e_ndn );
205                         }
206                         ldap_msgfree(res);
207                         if ( abort ) {
208                                 ldap_abandon(lc->ld, msgid);
209                                 goto finish;
210                         }
211
212                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
213                         char            **references = NULL;
214                         int             cnt;
215
216                         rc = ldap_parse_reference( lc->ld, res,
217                                         &references, &rs->sr_ctrls, 1 );
218
219                         if ( rc != LDAP_SUCCESS ) {
220                                 continue;
221                         }
222
223                         if ( references == NULL ) {
224                                 continue;
225                         }
226
227                         for ( cnt = 0; references[ cnt ]; cnt++ )
228                                 /* NO OP */ ;
229                                 
230                         rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
231
232                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
233                                 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
234                                 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
235                         }
236
237                         /* ignore return value by now */
238                         ( void )send_search_reference( op, rs );
239
240                         /* cleanup */
241                         if ( references ) {
242                                 ldap_value_free( references );
243                                 ch_free( rs->sr_ref );
244                                 rs->sr_ref = NULL;
245                         }
246
247                         if ( rs->sr_ctrls ) {
248                                 ldap_controls_free( rs->sr_ctrls );
249                                 rs->sr_ctrls = NULL;
250                         }
251
252                 } else {
253                         rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
254                                         &match.bv_val, (char **)&rs->sr_text,
255                                         NULL, NULL, 1);
256                         if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
257                         rs->sr_err = slap_map_api2result( rs );
258                         rc = 0;
259                         break;
260                 }
261         }
262
263         if (rc == -1)
264                 goto fail;
265
266         /*
267          * Rewrite the matched portion of the search base, if required
268          */
269         if ( match.bv_val && *match.bv_val ) {
270                 struct berval mdn;
271
272 #ifdef ENABLE_REWRITE
273                 dc.ctx = "matchedDN";
274 #else
275                 dc.tofrom = 0;
276                 dc.normalized = 0;
277 #endif
278                 match.bv_len = strlen( match.bv_val );
279                 ldap_back_dn_massage(&dc, &match, &mdn);
280                 rs->sr_matched = mdn.bv_val;
281         }
282         if ( rs->sr_v2ref ) {
283                 rs->sr_err = LDAP_REFERRAL;
284         }
285
286 finish:;
287         send_ldap_result( op, rs );
288
289 #ifdef LDAP_BACK_PROXY_AUTHZ
290         if ( ctrls && ctrls != op->o_ctrls ) {
291                 free( ctrls[ 0 ] );
292                 free( ctrls );
293         }
294 #endif /* LDAP_BACK_PROXY_AUTHZ */
295
296         if ( match.bv_val ) {
297                 if ( rs->sr_matched != match.bv_val ) {
298                         free( (char *)rs->sr_matched );
299                 }
300                 rs->sr_matched = NULL;
301                 LDAP_FREE( match.bv_val );
302         }
303         if ( rs->sr_text ) {
304                 if ( !dontfreetext ) {
305                         LDAP_FREE( (char *)rs->sr_text );
306                 }
307                 rs->sr_text = NULL;
308         }
309         if ( mapped_attrs ) {
310                 ch_free( mapped_attrs );
311         }
312         if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
313                 ch_free( mfilter.bv_val );
314         }
315         if ( mbase.bv_val != op->o_req_ndn.bv_val ) {
316                 free( mbase.bv_val );
317         }
318         
319         return rc;
320 }
321
322 static int
323 ldap_build_entry(
324         Operation *op,
325         LDAPMessage *e,
326         Entry *ent,
327         struct berval *bdn,
328         int flags
329 )
330 {
331         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
332         struct berval a, mapped;
333         BerElement ber = *e->lm_ber;
334         Attribute *attr, **attrp;
335         struct berval *bv;
336         const char *text;
337         int last;
338         int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
339         int normalize = flags & LDAP_BUILD_ENTRY_NORMALIZE;
340         dncookie dc;
341
342         /* safe assumptions ... */
343         assert( ent );
344         ent->e_bv.bv_val = NULL;
345
346         if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
347                 return LDAP_DECODING_ERROR;
348         }
349
350         /*
351          * Rewrite the dn of the result, if needed
352          */
353         dc.rwmap = &li->rwmap;
354 #ifdef ENABLE_REWRITE
355         dc.conn = op->o_conn;
356         dc.rs = NULL;
357         dc.ctx = "searchResult";
358 #else
359         dc.tofrom = 0;
360         dc.normalized = 0;
361 #endif
362         if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
363                 return LDAP_OTHER;
364         }
365
366         /*
367          * Note: this may fail if the target host(s) schema differs
368          * from the one known to the meta, and a DN with unknown
369          * attributes is returned.
370          * 
371          * FIXME: should we log anything, or delegate to dnNormalize?
372          */
373         if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
374                 op->o_tmpmemctx ) != LDAP_SUCCESS )
375         {
376                 return LDAP_INVALID_DN_SYNTAX;
377         }
378         
379         attrp = &ent->e_attrs;
380
381 #ifdef ENABLE_REWRITE
382         dc.ctx = "searchAttrDN";
383 #endif
384         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
385                 ldap_back_map(&li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP);
386                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
387                         continue;
388                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
389                 if (attr == NULL)
390                         continue;
391                 attr->a_flags = 0;
392                 attr->a_next = 0;
393                 attr->a_desc = NULL;
394                 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
395                         if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text) 
396                                         != LDAP_SUCCESS) {
397 #ifdef NEW_LOGGING
398                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
399                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
400 #else /* !NEW_LOGGING */
401                                 Debug( LDAP_DEBUG_ANY, 
402                                                 "slap_bv2undef_ad(%s):  "
403                                                 "%s\n%s", mapped.bv_val, text, "" );
404 #endif /* !NEW_LOGGING */
405                                 ch_free(attr);
406                                 continue;
407                         }
408                 }
409
410                 /* no subschemaSubentry */
411                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
412
413                         /* 
414                          * We eat target's subschemaSubentry because
415                          * a search for this value is likely not
416                          * to resolve to the appropriate backend;
417                          * later, the local subschemaSubentry is
418                          * added.
419                          */
420                         ( void )ber_scanf( &ber, "x" /* [W] */ );
421
422                         ch_free(attr);
423                         continue;
424                 }
425                 
426                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
427                                 || attr->a_vals == NULL ) {
428                         /*
429                          * Note: attr->a_vals can be null when using
430                          * values result filter
431                          */
432                         if (private) {
433                                 attr->a_vals = &dummy;
434                         } else {
435                                 attr->a_vals = ch_malloc(sizeof(struct berval));
436                                 attr->a_vals->bv_val = NULL;
437                                 attr->a_vals->bv_len = 0;
438                         }
439                         last = 0;
440                 } else {
441                         for ( last = 0; attr->a_vals[last].bv_val; last++ );
442                 }
443                 if ( last == 0 ) {
444                         /* empty */
445                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
446                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
447                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
448                                 ldap_back_map(&li->rwmap.rwm_oc, bv, &mapped,
449                                                 BACKLDAP_REMAP);
450                                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
451                                         LBER_FREE(bv->bv_val);
452                                         bv->bv_val = NULL;
453                                         if (--last < 0)
454                                                 break;
455                                         *bv = attr->a_vals[last];
456                                         attr->a_vals[last].bv_val = NULL;
457                                         bv--;
458
459                                 } else if ( mapped.bv_val != bv->bv_val ) {
460                                         /*
461                                          * FIXME: after LBER_FREEing
462                                          * the value is replaced by
463                                          * ch_alloc'ed memory
464                                          */
465                                         LBER_FREE(bv->bv_val);
466                                         ber_dupbv( bv, &mapped );
467                                 }
468                         }
469
470                 /*
471                  * It is necessary to try to rewrite attributes with
472                  * dn syntax because they might be used in ACLs as
473                  * members of groups; since ACLs are applied to the
474                  * rewritten stuff, no dn-based subject clause could
475                  * be used at the ldap backend side (see
476                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
477                  * The problem can be overcome by moving the dn-based
478                  * ACLs to the target directory server, and letting
479                  * everything pass thru the ldap backend.
480                  */
481                 } else if ( attr->a_desc->ad_type->sat_syntax ==
482                                 slap_schema.si_syn_distinguishedName ) {
483                         ldap_dnattr_result_rewrite( &dc, attr->a_vals );
484                 }
485
486                 if ( normalize && last && attr->a_desc->ad_type->sat_equality &&
487                         attr->a_desc->ad_type->sat_equality->smr_normalize ) {
488                         int i;
489
490                         attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
491                         for (i=0; i<last; i++) {
492                                 attr->a_desc->ad_type->sat_equality->smr_normalize(
493                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
494                                         attr->a_desc->ad_type->sat_syntax,
495                                         attr->a_desc->ad_type->sat_equality,
496                                         &attr->a_vals[i], &attr->a_nvals[i],
497                                         NULL /* op->o_tmpmemctx */ );
498                         }
499                         attr->a_nvals[i].bv_val = NULL;
500                         attr->a_nvals[i].bv_len = 0;
501                 } else {
502                         attr->a_nvals = attr->a_vals;
503                 }
504                 *attrp = attr;
505                 attrp = &attr->a_next;
506         }
507
508         /* make sure it's free'able */
509         if (!private && ent->e_name.bv_val == bdn->bv_val)
510                 ber_dupbv( &ent->e_name, bdn );
511         return LDAP_SUCCESS;
512 }
513
514 /* return 0 IFF we can retrieve the entry with ndn
515  */
516 int
517 ldap_back_entry_get(
518         Operation *op,
519         struct berval   *ndn,
520         ObjectClass *oc,
521         AttributeDescription *at,
522         int rw,
523         Entry **ent
524 )
525 {
526         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;    
527         struct ldapconn *lc;
528         int rc = 1, is_oc;
529         struct berval mapped = BER_BVNULL, bdn, mdn;
530         LDAPMessage     *result = NULL, *e = NULL;
531         char *gattr[3];
532         char *filter = NULL;
533         Connection *oconn;
534         SlapReply rs;
535         dncookie dc;
536
537         /* Tell getconn this is a privileged op */
538         is_oc = op->o_do_not_cache;
539         op->o_do_not_cache = 1;
540         lc = ldap_back_getconn(op, &rs);
541         oconn = op->o_conn;
542         op->o_conn = NULL;
543         if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
544                 op->o_do_not_cache = is_oc;
545                 op->o_conn = oconn;
546                 return 1;
547         }
548         op->o_do_not_cache = is_oc;
549         op->o_conn = oconn;
550
551         /*
552          * Rewrite the search base, if required
553          */
554         dc.rwmap = &li->rwmap;
555 #ifdef ENABLE_REWRITE
556         dc.conn = op->o_conn;
557         dc.rs = &rs;
558         dc.ctx = "searchBase";
559 #else
560         dc.tofrom = 1;
561         dc.normalized = 1;
562 #endif
563         if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
564                 return 1;
565         }
566
567         if ( at ) {
568                 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
569                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
570                         rc = 1;
571                         goto cleanup;
572                 }
573         }
574
575         is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
576         if (oc && !is_oc) {
577                 gattr[0] = "objectclass";
578                 gattr[1] = mapped.bv_val;
579                 gattr[2] = NULL;
580         } else {
581                 gattr[0] = mapped.bv_val;
582                 gattr[1] = NULL;
583         }
584         if (oc) {
585                 char *ptr;
586                 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
587                                                 BACKLDAP_MAP);
588                 filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
589                 ptr = lutil_strcopy(filter, "(objectclass=");
590                 ptr = lutil_strcopy(ptr, mapped.bv_val);
591                 *ptr++ = ')';
592                 *ptr++ = '\0';
593         }
594
595         if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
596                                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
597                                 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
598         {
599                 goto cleanup;
600         }
601
602         if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
603                 goto cleanup;
604         }
605
606         *ent = ch_calloc(1,sizeof(Entry));
607
608         rc = ldap_build_entry(op, e, *ent, &bdn, LDAP_BUILD_ENTRY_NORMALIZE);
609
610         if (rc != LDAP_SUCCESS) {
611                 ch_free(*ent);
612                 *ent = NULL;
613         }
614
615 cleanup:
616         if (result) {
617                 ldap_msgfree(result);
618         }
619
620         if ( filter ) {
621                 ch_free( filter );
622         }
623
624         if ( mdn.bv_val != ndn->bv_val ) {
625                 ch_free( mdn.bv_val );
626         }
627
628         return(rc);
629 }
630