]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
e2990e81d87ee05876cdc6c0ddef43623dc965b9
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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, struct ldapconn *lc,
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_filter = NULL, **mapped_attrs = NULL;
78         struct berval mbase;
79 #ifdef ENABLE_REWRITE
80         char *mmatch = NULL;
81         struct berval mfilter = { 0, NULL };
82 #endif /* ENABLE_REWRITE */
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          * controls are set in ldap_back_dobind()
94          * 
95          * FIXME: in case of values return filter, we might want
96          * to map attrs and maybe rewrite value
97          */
98         if ( !ldap_back_dobind( lc, op ) ) {
99                 return( -1 );
100         }
101
102         /* if not root, get appropriate limits */
103         if ( be_isroot( be, &op->o_ndn ) ) {
104                 isroot = 1;
105         } else {
106                 ( void ) get_limits( be, &op->o_ndn, &limit );
107         }
108         
109         /* if no time limit requested, rely on remote server limits */
110         /* if requested limit higher than hard limit, abort */
111         if ( !isroot && tlimit > limit->lms_t_hard ) {
112                 /* no hard limit means use soft instead */
113                 if ( limit->lms_t_hard == 0 && 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_UNWILLING_TO_PERFORM,
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 && slimit > limit->lms_s_soft ) {
132                         slimit = limit->lms_s_soft;
133                         
134                 /* positive hard limit means abort */
135                 } else if ( limit->lms_s_hard > 0 ) {
136                         send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
137                                         NULL, NULL, NULL, NULL );
138                         rc = 0;
139                         goto finish;
140                 }
141                 
142                 /* negative hard limit means no limit */
143         }
144
145         /* should we check return values? */
146         if (deref != -1)
147                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
148         if (tlimit != -1)
149                 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&tlimit);
150         if (slimit != -1)
151                 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&slimit);
152
153         /*
154          * Rewrite the search base, if required
155          */
156 #ifdef ENABLE_REWRITE
157         switch ( rewrite_session( li->rwinfo, "searchBase",
158                                 base->bv_val, conn, &mbase.bv_val ) ) {
159         case REWRITE_REGEXEC_OK:
160                 if ( mbase.bv_val == NULL ) {
161                         mbase = *base;
162                 }
163 #ifdef NEW_LOGGING
164                 LDAP_LOG( BACK_LDAP, DETAIL1, 
165                         "[rw] searchBase: \"%s\" -> \"%s\"\n", 
166                         base->bv_val, mbase.bv_val, 0 );
167 #else /* !NEW_LOGGING */
168                 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
169                                 base->bv_val, mbase.bv_val, "" );
170 #endif /* !NEW_LOGGING */
171                 break;
172                 
173         case REWRITE_REGEXEC_UNWILLING:
174                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
175                                 NULL, "Operation not allowed", NULL, NULL );
176                 rc = -1;
177                 goto finish;
178
179         case REWRITE_REGEXEC_ERR:
180                 send_ldap_result( conn, op, LDAP_OTHER,
181                                 NULL, "Rewrite error", NULL, NULL );
182                 rc = -1;
183                 goto finish;
184         }
185         
186         /*
187          * Rewrite the search filter, if required
188          */
189         switch ( rewrite_session( li->rwinfo, "searchFilter",
190                                 filterstr->bv_val, conn, &mfilter.bv_val ) ) {
191         case REWRITE_REGEXEC_OK:
192                 if ( mfilter.bv_val == NULL || mfilter.bv_val[0] == '\0') {
193                         if ( mfilter.bv_val != NULL ) {
194                                 free( mfilter.bv_val );
195                         }
196                         mfilter = *filterstr;
197                 } else {
198                         mfilter.bv_len = strlen( mfilter.bv_val );
199                 }
200
201 #ifdef NEW_LOGGING
202                 LDAP_LOG( BACK_LDAP, DETAIL1, 
203                         "[rw] searchFilter: \"%s\" -> \"%s\"\n",
204                         filterstr->bv_val, mfilter.bv_val, 0 );
205 #else /* !NEW_LOGGING */
206                 Debug( LDAP_DEBUG_ARGS,
207                                 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
208                                 filterstr->bv_val, mfilter.bv_val, "" );
209 #endif /* !NEW_LOGGING */
210                 break;
211                 
212         case REWRITE_REGEXEC_UNWILLING:
213                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
214                                 NULL, "Operation not allowed", NULL, NULL );
215                 rc = -1;
216                 goto finish;
217
218         case REWRITE_REGEXEC_ERR:
219                 send_ldap_result( conn, op, LDAP_OTHER,
220                                 NULL, "Rewrite error", NULL, NULL );
221                 rc = -1;
222                 goto finish;
223         }
224 #else /* !ENABLE_REWRITE */
225         ldap_back_dn_massage( li, base, &mbase, 0, 1 );
226 #endif /* !ENABLE_REWRITE */
227
228         mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
229 #ifdef ENABLE_REWRITE
230                         &mfilter,
231 #else /* !ENABLE_REWRITE */
232                         filterstr,
233 #endif /* !ENABLE_REWRITE */
234                         0);
235         if ( mapped_filter == NULL ) {
236 #ifdef ENABLE_REWRITE
237                 mapped_filter = mfilter.bv_val;
238 #else /* !ENABLE_REWRITE */
239                 mapped_filter = filterstr->bv_val;
240 #endif /* !ENABLE_REWRITE */
241         }
242
243 #ifdef ENABLE_REWRITE
244         if ( mfilter.bv_val != filterstr->bv_val ) {
245                 free( mfilter.bv_val );
246         }
247 #endif /* ENABLE_REWRITE */
248
249         mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
250         if ( mapped_attrs == NULL && attrs) {
251                 for (count=0; attrs[count].an_name.bv_val; count++);
252                 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
253                 for (count=0; attrs[count].an_name.bv_val; count++) {
254                         mapped_attrs[count] = attrs[count].an_name.bv_val;
255                 }
256                 mapped_attrs[count] = NULL;
257         }
258
259         msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter,
260                         mapped_attrs, attrsonly);
261         if ( msgid == -1 ) {
262 fail:;
263                 rc = ldap_back_op_result(lc, op);
264                 goto finish;
265         }
266
267         /* We pull apart the ber result, stuff it into a slapd entry, and
268          * let send_search_entry stuff it back into ber format. Slow & ugly,
269          * but this is necessary for version matching, and for ACL processing.
270          */
271         
272         for (   count=0, rc=0;
273                         rc != -1;
274                         rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
275         {
276                 /* check for abandon */
277                 if (op->o_abandon) {
278                         ldap_abandon(lc->ld, msgid);
279                         rc = 0;
280                         goto finish;
281                 }
282
283                 if (rc == 0) {
284                         tv.tv_sec = 0;
285                         tv.tv_usec = 100000;
286                         ldap_pvt_thread_yield();
287
288                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
289                         e = ldap_first_entry(lc->ld,res);
290                         if ( ldap_send_entry(be, op, lc, e, attrs, attrsonly) 
291                                         == LDAP_SUCCESS ) {
292                                 count++;
293                         }
294                         ldap_msgfree(res);
295
296                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
297                         char            **references = NULL;
298                         LDAPControl     **ctrls = NULL;
299                         BerVarray       refs;
300                         int             cnt;
301
302                         rc = ldap_parse_reference( lc->ld, res,
303                                         &references, &ctrls, 1 );
304
305                         if ( rc != LDAP_SUCCESS ) {
306                                 continue;
307                         }
308
309                         if ( references == NULL ) {
310                                 continue;
311                         }
312
313                         for ( cnt = 0; references[ cnt ]; cnt++ )
314                                 /* NO OP */ ;
315                                 
316                         refs = ch_calloc( cnt + 1, sizeof( struct berval ) );
317
318                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
319                                 refs[ cnt ].bv_val = references[ cnt ];
320                                 refs[ cnt ].bv_len = strlen( references[ cnt ] );
321                         }
322
323                         /* ignore return value by now */
324                         ( void )send_search_reference( be, conn, op, 
325                                         NULL, refs, ctrls, &v2refs );
326
327                         /* cleanup */
328                         if ( references ) {
329                                 ldap_value_free( references );
330                                 ch_free( refs );
331                         }
332
333                         if ( ctrls ) {
334                                 ldap_controls_free( ctrls );
335                         }
336
337                 } else {
338                         sres = ldap_result2error(lc->ld, res, 1);
339                         sres = ldap_back_map_result(sres);
340                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
341                         ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
342                         rc = 0;
343                         break;
344                 }
345         }
346
347         if (rc == -1)
348                 goto fail;
349
350 #ifdef ENABLE_REWRITE
351         /*
352          * Rewrite the matched portion of the search base, if required
353          */
354         if ( match != NULL ) {
355                 switch ( rewrite_session( li->rwinfo, "matchedDn",
356                                 match, conn, &mmatch ) ) {
357                 case REWRITE_REGEXEC_OK:
358                         if ( mmatch == NULL ) {
359                                 mmatch = ( char * )match;
360                         }
361 #ifdef NEW_LOGGING
362                         LDAP_LOG( BACK_LDAP, DETAIL1, 
363                                 "[rw]  matchedDn:" " \"%s\" -> \"%s\"\n", match, mmatch, 0 );
364 #else /* !NEW_LOGGING */
365                         Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
366                                         " \"%s\" -> \"%s\"\n%s",
367                                         match, mmatch, "" );
368 #endif /* !NEW_LOGGING */
369                         break;
370                         
371                 case REWRITE_REGEXEC_UNWILLING:
372                         
373                 case REWRITE_REGEXEC_ERR:
374                         /* FIXME: no error, but no matched ... */
375                         mmatch = NULL;
376                         break;
377                 }
378         }
379
380         if ( v2refs ) {
381                 sres = LDAP_REFERRAL;
382         }
383
384         send_search_result( conn, op, sres,
385                 mmatch, err, v2refs, NULL, count );
386
387 #else /* !ENABLE_REWRITE */
388         if ( v2refs ) {
389                 sres = LDAP_REFERRAL;
390         }
391         send_search_result( conn, op, sres,
392                 match, err, v2refs, NULL, count );
393 #endif /* !ENABLE_REWRITE */
394
395 finish:;
396         if ( match ) {
397 #ifdef ENABLE_REWRITE
398                 if ( mmatch != match ) {
399                         free( mmatch );
400                 }
401 #endif /* ENABLE_REWRITE */
402                 LDAP_FREE(match);
403         }
404         if ( err ) {
405                 LDAP_FREE( err );
406         }
407         if ( mapped_attrs ) {
408                 ch_free( mapped_attrs );
409         }
410         if ( mapped_filter != filterstr->bv_val ) {
411                 ch_free( mapped_filter );
412         }
413         if ( mbase.bv_val != base->bv_val ) {
414                 free( mbase.bv_val );
415         }
416         
417         return rc;
418 }
419
420 static int
421 ldap_send_entry(
422         Backend *be,
423         Operation *op,
424         struct ldapconn *lc,
425         LDAPMessage *e,
426         AttributeName *attrs,
427         int attrsonly
428 )
429 {
430         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
431         struct berval a, mapped;
432         Entry ent;
433         BerElement ber = *e->lm_ber;
434         Attribute *attr, **attrp;
435         struct berval dummy = { 0, NULL };
436         struct berval *bv, bdn;
437         const char *text;
438
439         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
440                 return LDAP_DECODING_ERROR;
441         }
442 #ifdef ENABLE_REWRITE
443
444         /*
445          * Rewrite the dn of the result, if needed
446          */
447         switch ( rewrite_session( li->rwinfo, "searchResult",
448                                 bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
449         case REWRITE_REGEXEC_OK:
450                 if ( ent.e_name.bv_val == NULL ) {
451                         ent.e_name = bdn;
452                 } else {
453 #ifdef NEW_LOGGING
454                         LDAP_LOG( BACK_LDAP, DETAIL1, 
455                                 "[rw] searchResult: \"%s\"" " -> \"%s\"\n", 
456                                 bdn.bv_val, ent.e_dn, 0 );
457 #else /* !NEW_LOGGING */
458                         Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
459                                         " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
460 #endif /* !NEW_LOGGING */
461                         ent.e_name.bv_len = strlen( ent.e_name.bv_val );
462                 }
463                 break;
464                 
465         case REWRITE_REGEXEC_ERR:
466         case REWRITE_REGEXEC_UNWILLING:
467                 return LDAP_OTHER;
468         }
469 #else /* !ENABLE_REWRITE */
470         ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
471 #endif /* !ENABLE_REWRITE */
472
473         /*
474          * Note: this may fail if the target host(s) schema differs
475          * from the one known to the meta, and a DN with unknown
476          * attributes is returned.
477          * 
478          * FIXME: should we log anything, or delegate to dnNormalize2?
479          */
480         if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
481                 return LDAP_INVALID_DN_SYNTAX;
482         }
483         
484         ent.e_id = 0;
485         ent.e_attrs = 0;
486         ent.e_private = 0;
487         attrp = &ent.e_attrs;
488
489         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
490                 ldap_back_map(&li->at_map, &a, &mapped, 1);
491                 if (mapped.bv_val == NULL)
492                         continue;
493                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
494                 if (attr == NULL)
495                         continue;
496                 attr->a_flags = 0;
497                 attr->a_next = 0;
498                 attr->a_desc = NULL;
499                 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
500                         if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text) 
501                                         != LDAP_SUCCESS) {
502 #ifdef NEW_LOGGING
503                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
504                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
505 #else /* !NEW_LOGGING */
506                                 Debug( LDAP_DEBUG_ANY, 
507                                                 "slap_bv2undef_ad(%s):  "
508                                                 "%s\n%s", mapped.bv_val, text, "" );
509 #endif /* !NEW_LOGGING */
510                                 ch_free(attr);
511                                 continue;
512                         }
513                 }
514
515                 /* no subschemaSubentry */
516                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
517                         ch_free(attr);
518                         continue;
519                 }
520                 
521                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
522                                 || attr->a_vals == NULL ) {
523                         /*
524                          * Note: attr->a_vals can be null when using
525                          * values result filter
526                          */
527                         attr->a_vals = &dummy;
528
529                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
530                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
531                         int i, last;
532
533                         for ( last = 0; attr->a_vals[last].bv_val; last++ ) ;
534                         for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
535                                 ldap_back_map(&li->oc_map, bv, &mapped, 1);
536                                 if (mapped.bv_val == NULL) {
537                                         LBER_FREE(bv->bv_val);
538                                         bv->bv_val = NULL;
539                                         if (--last < 0)
540                                                 break;
541                                         *bv = attr->a_vals[last];
542                                         attr->a_vals[last].bv_val = NULL;
543                                         i--;
544                                 } else if ( mapped.bv_val != bv->bv_val ) {
545                                         /*
546                                          * FIXME: after LBER_FREEing
547                                          * the value is replaced by
548                                          * ch_alloc'ed memory
549                                          */
550                                         LBER_FREE(bv->bv_val);
551                                         ber_dupbv( bv, &mapped );
552                                 }
553                         }
554
555                 /*
556                  * It is necessary to try to rewrite attributes with
557                  * dn syntax because they might be used in ACLs as
558                  * members of groups; since ACLs are applied to the
559                  * rewritten stuff, no dn-based subject clause could
560                  * be used at the ldap backend side (see
561                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
562                  * The problem can be overcome by moving the dn-based
563                  * ACLs to the target directory server, and letting
564                  * everything pass thru the ldap backend.
565                  */
566                 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
567                                         SLAPD_DN_SYNTAX ) == 0 ) {
568                         int i;
569                         for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
570                                 struct berval newval;
571                                 
572 #ifdef ENABLE_REWRITE
573                                 switch ( rewrite_session( li->rwinfo,
574                                                         "searchResult",
575                                                         bv->bv_val,
576                                                         lc->conn, 
577                                                         &newval.bv_val )) {
578                                 case REWRITE_REGEXEC_OK:
579                                         /* left as is */
580                                         if ( newval.bv_val == NULL ) {
581                                                 break;
582                                         }
583                                         newval.bv_len = strlen( newval.bv_val );
584 #ifdef NEW_LOGGING
585                                         LDAP_LOG( BACK_LDAP, DETAIL1, 
586                                                 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
587                                                 attr->a_desc->ad_type->sat_cname.bv_val,
588                                                 bv->bv_val, newval.bv_val );
589 #else /* !NEW_LOGGING */
590                                         Debug( LDAP_DEBUG_ARGS,
591                 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
592                                                 attr->a_desc->ad_type->sat_cname.bv_val,
593                                                 bv->bv_val, newval.bv_val );
594 #endif /* !NEW_LOGGING */
595                                         free( bv->bv_val );
596                                         *bv = newval;
597                                         break;
598                                         
599                                 case REWRITE_REGEXEC_UNWILLING:
600                                         
601                                 case REWRITE_REGEXEC_ERR:
602                                         /*
603                                          * FIXME: better give up,
604                                          * skip the attribute
605                                          * or leave it untouched?
606                                          */
607                                         break;
608                                 }
609 #else /* !ENABLE_REWRITE */
610                                 ldap_back_dn_massage( li, bv, &newval, 0, 0 );
611                                 *bv = newval;
612 #endif /* !ENABLE_REWRITE */
613                         }
614                 }
615
616                 *attrp = attr;
617                 attrp = &attr->a_next;
618         }
619         send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
620         while (ent.e_attrs) {
621                 attr = ent.e_attrs;
622                 ent.e_attrs = attr->a_next;
623                 if (attr->a_vals != &dummy)
624                         ber_bvarray_free(attr->a_vals);
625                 ch_free(attr);
626         }
627         
628         if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
629                 free( ent.e_dn );
630         if ( ent.e_ndn )
631                 free( ent.e_ndn );
632
633         return LDAP_SUCCESS;
634 }