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