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