]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
cleanup bind
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* This is an altered version */
8 /*
9  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10  * 
11  * Permission is granted to anyone to use this software for any purpose
12  * on any computer system, and to alter it and redistribute it, subject
13  * to the following restrictions:
14  * 
15  * 1. The author is not responsible for the consequences of use of this
16  *    software, no matter how awful, even if they arise from flaws in it.
17  * 
18  * 2. The origin of this software must not be misrepresented, either by
19  *    explicit claim or by omission.  Since few users ever read sources,
20  *    credits should appear in the documentation.
21  * 
22  * 3. Altered versions must be plainly marked as such, and must not be
23  *    misrepresented as being the original software.  Since few users
24  *    ever read sources, credits should appear in the documentation.
25  * 
26  * 4. This notice may not be removed or altered.
27  *
28  *
29  *
30  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31  * 
32  * This software is being modified by Pierangelo Masarati.
33  * The previously reported conditions apply to the modified code as well.
34  * Changes in the original code are highlighted where required.
35  * Credits for the original code go to the author, Howard Chu.
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45
46 #include "slap.h"
47 #include "back-ldap.h"
48 #undef ldap_debug       /* silence a warning in ldap-int.h */
49 #include "../../../libraries/libldap/ldap-int.h"
50
51 static int ldap_send_entry( Backend *be, Operation *op, Connection *conn,
52                              LDAPMessage *e, AttributeName *attrs, int attrsonly );
53
54 int
55 ldap_back_search(
56     Backend     *be,
57     Connection  *conn,
58     Operation   *op,
59     struct berval       *base,
60     struct berval       *nbase,
61     int         scope,
62     int         deref,
63     int         slimit,
64     int         tlimit,
65     Filter      *filter,
66     struct berval       *filterstr,
67     AttributeName       *attrs,
68     int         attrsonly
69 )
70 {
71         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
72         struct ldapconn *lc;
73         struct timeval  tv;
74         LDAPMessage             *res, *e;
75         int     count, rc = 0, msgid, sres = LDAP_SUCCESS; 
76         char *match = NULL, *err = NULL;
77         char **mapped_attrs = NULL;
78         struct berval mbase;
79 #ifdef ENABLE_REWRITE
80         char *mmatch = NULL;
81 #endif /* ENABLE_REWRITE */
82         struct berval mfilter = { 0, NULL };
83         struct slap_limits_set *limit = NULL;
84         int isroot = 0;
85         BerVarray v2refs = NULL;
86
87         lc = ldap_back_getconn(li, conn, op);
88         if ( !lc ) {
89                 return( -1 );
90         }
91
92         /*
93          * FIXME: in case of values return filter, we might want
94          * to map attrs and maybe rewrite value
95          */
96         if ( !ldap_back_dobind( li, lc, conn, op ) ) {
97                 return( -1 );
98         }
99
100         /* if not root, get appropriate limits */
101         if ( be_isroot( be, &op->o_ndn ) ) {
102                 isroot = 1;
103         } else {
104                 ( void ) get_limits( be, &op->o_ndn, &limit );
105         }
106         
107         /* if no time limit requested, rely on remote server limits */
108         /* if requested limit higher than hard limit, abort */
109         if ( !isroot && tlimit > limit->lms_t_hard ) {
110                 /* no hard limit means use soft instead */
111                 if ( limit->lms_t_hard == 0
112                                 && limit->lms_t_soft > -1
113                                 && tlimit > limit->lms_t_soft ) {
114                         tlimit = limit->lms_t_soft;
115                         
116                 /* positive hard limit means abort */
117                 } else if ( limit->lms_t_hard > 0 ) {
118                         send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
119                                         NULL, NULL, NULL, NULL );
120                         rc = 0;
121                         goto finish;
122                 }
123                 
124                 /* negative hard limit means no limit */
125         }
126         
127         /* if no size limit requested, rely on remote server limits */
128         /* if requested limit higher than hard limit, abort */
129         if ( !isroot && slimit > limit->lms_s_hard ) {
130                 /* no hard limit means use soft instead */
131                 if ( limit->lms_s_hard == 0
132                                 && limit->lms_s_soft > -1
133                                 && slimit > limit->lms_s_soft ) {
134                         slimit = limit->lms_s_soft;
135                         
136                 /* positive hard limit means abort */
137                 } else if ( limit->lms_s_hard > 0 ) {
138                         send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
139                                         NULL, NULL, NULL, NULL );
140                         rc = 0;
141                         goto finish;
142                 }
143                 
144                 /* negative hard limit means no limit */
145         }
146
147         /* should we check return values? */
148         if (deref != -1)
149                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
150         if (tlimit != -1) {
151                 tv.tv_sec = tlimit;
152                 tv.tv_usec = 0;
153         } else {
154                 tv.tv_sec = 0;
155         }
156
157         /*
158          * Rewrite the search base, if required
159          */
160 #ifdef ENABLE_REWRITE
161         switch ( rewrite_session( li->rwinfo, "searchBase",
162                                 base->bv_val, conn, &mbase.bv_val ) ) {
163         case REWRITE_REGEXEC_OK:
164                 if ( mbase.bv_val == NULL ) {
165                         mbase = *base;
166                 }
167 #ifdef NEW_LOGGING
168                 LDAP_LOG( BACK_LDAP, DETAIL1, 
169                         "[rw] searchBase: \"%s\" -> \"%s\"\n", 
170                         base->bv_val, mbase.bv_val, 0 );
171 #else /* !NEW_LOGGING */
172                 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
173                                 base->bv_val, mbase.bv_val, "" );
174 #endif /* !NEW_LOGGING */
175                 break;
176                 
177         case REWRITE_REGEXEC_UNWILLING:
178                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
179                                 NULL, "Operation not allowed", NULL, NULL );
180                 rc = -1;
181                 goto finish;
182
183         case REWRITE_REGEXEC_ERR:
184                 send_ldap_result( conn, op, LDAP_OTHER,
185                                 NULL, "Rewrite error", NULL, NULL );
186                 rc = -1;
187                 goto finish;
188         }
189
190 #else /* !ENABLE_REWRITE */
191         ldap_back_dn_massage( li, base, &mbase, 0, 1 );
192 #endif /* !ENABLE_REWRITE */
193
194 #ifdef ENABLE_REWRITE
195         rc = ldap_back_filter_map_rewrite_( li->rwinfo, conn,
196                         &li->at_map, &li->oc_map, filter, &mfilter, 
197                         BACKLDAP_MAP );
198 #else /* ! ENABLE_REWRITE */
199         rc = ldap_back_filter_map_rewrite_( &li->at_map, &li->oc_map, 
200                         filter, &mfilter, BACKLDAP_MAP );
201 #endif /* ! ENABLE_REWRITE */
202
203         if ( rc ) {
204                 rc = -1;
205                 goto finish;
206         }
207
208         mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, BACKLDAP_MAP);
209         if ( mapped_attrs == NULL && attrs) {
210                 for (count=0; attrs[count].an_name.bv_val; count++);
211                 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
212                 for (count=0; attrs[count].an_name.bv_val; count++) {
213                         mapped_attrs[count] = attrs[count].an_name.bv_val;
214                 }
215                 mapped_attrs[count] = NULL;
216         }
217
218         rc = ldap_search_ext(lc->ld, mbase.bv_val, scope, mfilter.bv_val,
219                         mapped_attrs, attrsonly, op->o_ctrls, NULL, tv.tv_sec ? &tv
220                         : NULL, slimit, &msgid);
221         if ( rc != LDAP_SUCCESS ) {
222 fail:;
223                 rc = ldap_back_op_result(li, lc, conn, op, msgid, rc, 0);
224                 goto finish;
225         }
226
227         /* We pull apart the ber result, stuff it into a slapd entry, and
228          * let send_search_entry stuff it back into ber format. Slow & ugly,
229          * but this is necessary for version matching, and for ACL processing.
230          */
231
232         for (   count=0, rc=0;
233                         rc != -1;
234                         rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
235         {
236                 /* check for abandon */
237                 if (op->o_abandon) {
238                         ldap_abandon(lc->ld, msgid);
239                         rc = 0;
240                         goto finish;
241                 }
242
243                 if (rc == 0) {
244                         tv.tv_sec = 0;
245                         tv.tv_usec = 100000;
246                         ldap_pvt_thread_yield();
247
248                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
249                         e = ldap_first_entry(lc->ld,res);
250                         if ( ldap_send_entry(be, op, conn, e, attrs, attrsonly) 
251                                         == LDAP_SUCCESS ) {
252                                 count++;
253                         }
254                         ldap_msgfree(res);
255
256                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
257                         char            **references = NULL;
258                         LDAPControl     **ctrls = NULL;
259                         BerVarray       refs;
260                         int             cnt;
261
262                         rc = ldap_parse_reference( lc->ld, res,
263                                         &references, &ctrls, 1 );
264
265                         if ( rc != LDAP_SUCCESS ) {
266                                 continue;
267                         }
268
269                         if ( references == NULL ) {
270                                 continue;
271                         }
272
273                         for ( cnt = 0; references[ cnt ]; cnt++ )
274                                 /* NO OP */ ;
275                                 
276                         refs = ch_calloc( cnt + 1, sizeof( struct berval ) );
277
278                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
279                                 refs[ cnt ].bv_val = references[ cnt ];
280                                 refs[ cnt ].bv_len = strlen( references[ cnt ] );
281                         }
282
283                         /* ignore return value by now */
284                         ( void )send_search_reference( be, conn, op, 
285                                         NULL, refs, ctrls, &v2refs );
286
287                         /* cleanup */
288                         if ( references ) {
289                                 ldap_value_free( references );
290                                 ch_free( refs );
291                         }
292
293                         if ( ctrls ) {
294                                 ldap_controls_free( ctrls );
295                         }
296
297                 } else {
298                         rc = ldap_parse_result(lc->ld, res, &sres, &match,
299                                 &err, NULL, NULL, 1);
300                         if (rc != LDAP_SUCCESS ) sres = rc;
301                         sres = ldap_back_map_result(sres);
302                         rc = 0;
303                         break;
304                 }
305         }
306
307         if (rc == -1)
308                 goto fail;
309
310 #ifdef ENABLE_REWRITE
311         /*
312          * Rewrite the matched portion of the search base, if required
313          */
314         if ( match != NULL ) {
315                 switch ( rewrite_session( li->rwinfo, "matchedDn",
316                                 match, conn, &mmatch ) ) {
317                 case REWRITE_REGEXEC_OK:
318                         if ( mmatch == NULL ) {
319                                 mmatch = ( char * )match;
320                         }
321 #ifdef NEW_LOGGING
322                         LDAP_LOG( BACK_LDAP, DETAIL1, 
323                                 "[rw]  matchedDn:" " \"%s\" -> \"%s\"\n", match, mmatch, 0 );
324 #else /* !NEW_LOGGING */
325                         Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
326                                         " \"%s\" -> \"%s\"\n%s",
327                                         match, mmatch, "" );
328 #endif /* !NEW_LOGGING */
329                         break;
330                         
331                 case REWRITE_REGEXEC_UNWILLING:
332                         
333                 case REWRITE_REGEXEC_ERR:
334                         /* FIXME: no error, but no matched ... */
335                         mmatch = NULL;
336                         break;
337                 }
338         }
339
340         if ( v2refs ) {
341                 sres = LDAP_REFERRAL;
342         }
343
344         send_search_result( conn, op, sres,
345                 mmatch, err, v2refs, NULL, count );
346
347 #else /* !ENABLE_REWRITE */
348         if ( v2refs ) {
349                 sres = LDAP_REFERRAL;
350         }
351         send_search_result( conn, op, sres,
352                 match, err, v2refs, NULL, count );
353 #endif /* !ENABLE_REWRITE */
354
355 finish:;
356         if ( match ) {
357 #ifdef ENABLE_REWRITE
358                 if ( mmatch != match ) {
359                         free( mmatch );
360                 }
361 #endif /* ENABLE_REWRITE */
362                 LDAP_FREE(match);
363         }
364         if ( err ) {
365                 LDAP_FREE( err );
366         }
367         if ( mapped_attrs ) {
368                 ch_free( mapped_attrs );
369         }
370         if ( mfilter.bv_val != filterstr->bv_val ) {
371                 ch_free( mfilter.bv_val );
372         }
373         if ( mbase.bv_val != base->bv_val ) {
374                 free( mbase.bv_val );
375         }
376         
377         return rc;
378 }
379
380 static int
381 ldap_send_entry(
382         Backend *be,
383         Operation *op,
384         Connection *conn,
385         LDAPMessage *e,
386         AttributeName *attrs,
387         int attrsonly
388 )
389 {
390         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
391         struct berval a, mapped;
392         Entry ent;
393         BerElement ber = *e->lm_ber;
394         Attribute *attr, **attrp;
395         struct berval dummy = { 0, NULL };
396         struct berval *bv, bdn;
397         const char *text;
398
399         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
400                 return LDAP_DECODING_ERROR;
401         }
402 #ifdef ENABLE_REWRITE
403
404         /*
405          * Rewrite the dn of the result, if needed
406          */
407         switch ( rewrite_session( li->rwinfo, "searchResult",
408                                 bdn.bv_val, conn, &ent.e_name.bv_val ) ) {
409         case REWRITE_REGEXEC_OK:
410                 if ( ent.e_name.bv_val == NULL ) {
411                         ent.e_name = bdn;
412                 } else {
413 #ifdef NEW_LOGGING
414                         LDAP_LOG( BACK_LDAP, DETAIL1, 
415                                 "[rw] searchResult: \"%s\"" " -> \"%s\"\n", 
416                                 bdn.bv_val, ent.e_dn, 0 );
417 #else /* !NEW_LOGGING */
418                         Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
419                                         " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
420 #endif /* !NEW_LOGGING */
421                         ent.e_name.bv_len = strlen( ent.e_name.bv_val );
422                 }
423                 break;
424                 
425         case REWRITE_REGEXEC_ERR:
426         case REWRITE_REGEXEC_UNWILLING:
427                 return LDAP_OTHER;
428         }
429 #else /* !ENABLE_REWRITE */
430         ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
431 #endif /* !ENABLE_REWRITE */
432
433         /*
434          * Note: this may fail if the target host(s) schema differs
435          * from the one known to the meta, and a DN with unknown
436          * attributes is returned.
437          * 
438          * FIXME: should we log anything, or delegate to dnNormalize2?
439          */
440         if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
441                 return LDAP_INVALID_DN_SYNTAX;
442         }
443         
444         ent.e_id = 0;
445         ent.e_attrs = 0;
446         ent.e_private = 0;
447         attrp = &ent.e_attrs;
448
449         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
450                 ldap_back_map(&li->at_map, &a, &mapped, BACKLDAP_REMAP);
451                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
452                         continue;
453                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
454                 if (attr == NULL)
455                         continue;
456                 attr->a_flags = 0;
457                 attr->a_next = 0;
458                 attr->a_desc = NULL;
459                 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
460                         if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text) 
461                                         != LDAP_SUCCESS) {
462 #ifdef NEW_LOGGING
463                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
464                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
465 #else /* !NEW_LOGGING */
466                                 Debug( LDAP_DEBUG_ANY, 
467                                                 "slap_bv2undef_ad(%s):  "
468                                                 "%s\n%s", mapped.bv_val, text, "" );
469 #endif /* !NEW_LOGGING */
470                                 ch_free(attr);
471                                 continue;
472                         }
473                 }
474
475                 /* no subschemaSubentry */
476                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
477                         ch_free(attr);
478                         continue;
479                 }
480                 
481                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
482                                 || attr->a_vals == NULL ) {
483                         /*
484                          * Note: attr->a_vals can be null when using
485                          * values result filter
486                          */
487                         attr->a_vals = &dummy;
488
489                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
490                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
491                         int             last;
492
493                         for ( last = 0; attr->a_vals[last].bv_val; last++ );
494
495                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
496                                 ldap_back_map(&li->oc_map, bv, &mapped,
497                                                 BACKLDAP_REMAP);
498                                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
499                                         LBER_FREE(bv->bv_val);
500                                         bv->bv_val = NULL;
501                                         if (--last < 0)
502                                                 break;
503                                         *bv = attr->a_vals[last];
504                                         attr->a_vals[last].bv_val = NULL;
505                                         bv--;
506
507                                 } else if ( mapped.bv_val != bv->bv_val ) {
508                                         /*
509                                          * FIXME: after LBER_FREEing
510                                          * the value is replaced by
511                                          * ch_alloc'ed memory
512                                          */
513                                         LBER_FREE(bv->bv_val);
514                                         ber_dupbv( bv, &mapped );
515                                 }
516                         }
517
518                 /*
519                  * It is necessary to try to rewrite attributes with
520                  * dn syntax because they might be used in ACLs as
521                  * members of groups; since ACLs are applied to the
522                  * rewritten stuff, no dn-based subject clause could
523                  * be used at the ldap backend side (see
524                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
525                  * The problem can be overcome by moving the dn-based
526                  * ACLs to the target directory server, and letting
527                  * everything pass thru the ldap backend.
528                  */
529                 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
530                                         SLAPD_DN_SYNTAX ) == 0 ) {
531                         int             last;
532
533                         for ( last = 0; attr->a_vals[last].bv_val; last++ );
534
535                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
536                                 struct berval   newval;
537                                 
538 #ifdef ENABLE_REWRITE
539                                 switch ( rewrite_session( li->rwinfo,
540                                                         "searchResult",
541                                                         bv->bv_val,
542                                                         conn, 
543                                                         &newval.bv_val )) {
544                                 case REWRITE_REGEXEC_OK:
545                                         /* left as is */
546                                         if ( newval.bv_val == NULL ) {
547                                                 break;
548                                         }
549                                         newval.bv_len = strlen( newval.bv_val );
550 #ifdef NEW_LOGGING
551                                         LDAP_LOG( BACK_LDAP, DETAIL1, 
552                                                 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
553                                                 attr->a_desc->ad_type->sat_cname.bv_val,
554                                                 bv->bv_val, newval.bv_val );
555 #else /* !NEW_LOGGING */
556                                         Debug( LDAP_DEBUG_ARGS,
557                 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
558                                                 attr->a_desc->ad_type->sat_cname.bv_val,
559                                                 bv->bv_val, newval.bv_val );
560 #endif /* !NEW_LOGGING */
561                                         free( bv->bv_val );
562                                         *bv = newval;
563                                         break;
564                                         
565                                 case REWRITE_REGEXEC_UNWILLING:
566                                         LBER_FREE(bv->bv_val);
567                                         bv->bv_val = NULL;
568                                         if (--last < 0)
569                                                 goto next_attr;
570                                         *bv = attr->a_vals[last];
571                                         attr->a_vals[last].bv_val = NULL;
572                                         bv--;
573                                         break;
574
575                                 case REWRITE_REGEXEC_ERR:
576                                         /*
577                                          * FIXME: better give up,
578                                          * skip the attribute
579                                          * or leave it untouched?
580                                          */
581                                         break;
582                                 }
583 #else /* !ENABLE_REWRITE */
584                                 ldap_back_dn_massage( li, bv, &newval, 0, 0 );
585                                 *bv = newval;
586 #endif /* !ENABLE_REWRITE */
587                         }
588                 }
589
590 next_attr:;
591
592                 *attrp = attr;
593                 attrp = &attr->a_next;
594         }
595         send_search_entry( be, conn, op, &ent, attrs, attrsonly, NULL );
596         while (ent.e_attrs) {
597                 attr = ent.e_attrs;
598                 ent.e_attrs = attr->a_next;
599                 if (attr->a_vals != &dummy)
600                         ber_bvarray_free(attr->a_vals);
601                 ch_free(attr);
602         }
603         
604         if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
605                 free( ent.e_dn );
606         if ( ent.e_ndn )
607                 free( ent.e_ndn );
608
609         return LDAP_SUCCESS;
610 }