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