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