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