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