]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
6f192f13b88c77912118b089632bc3bc8b2dca5d
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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.bv_val = ( char * )base->bv_val;
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                 }
189 #ifdef NEW_LOGGING
190                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
191                                 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
192                                 filterstr->bv_val, mfilter.bv_val ));
193 #else /* !NEW_LOGGING */
194                 Debug( LDAP_DEBUG_ARGS,
195                                 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
196                                 filterstr->bv_val, mfilter.bv_val, "" );
197 #endif /* !NEW_LOGGING */
198                 break;
199                 
200         case REWRITE_REGEXEC_UNWILLING:
201                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
202                                 NULL, "Unwilling to perform", NULL, NULL );
203         case REWRITE_REGEXEC_ERR:
204                 rc = -1;
205                 goto finish;
206         }
207 #else /* !ENABLE_REWRITE */
208         ldap_back_dn_massage( li, base, &mbase, 0, 1 );
209 #endif /* !ENABLE_REWRITE */
210
211         mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
212 #ifdef ENABLE_REWRITE
213                         &mfilter,
214 #else /* !ENABLE_REWRITE */
215                         filterstr,
216 #endif /* !ENABLE_REWRITE */
217                         0);
218         if ( mapped_filter == NULL ) {
219 #ifdef ENABLE_REWRITE
220                 mapped_filter = mfilter.bv_val;
221 #else /* !ENABLE_REWRITE */
222                 mapped_filter = filterstr->bv_val;
223 #endif /* !ENABLE_REWRITE */
224         }
225
226         mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
227         if ( mapped_attrs == NULL && attrs) {
228                 AttributeName *an;
229                 for (count=0, an=attrs; an; an=an->an_next,count++);
230                 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
231                 for (count=0, an=attrs; an; an=an->an_next,count++) {
232                         mapped_attrs[count] = an->an_name.bv_val;
233                 }
234                 mapped_attrs[count] = NULL;
235         }
236
237         if ((msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter, mapped_attrs,
238                 attrsonly)) == -1)
239         {
240 fail:;
241                 rc = ldap_back_op_result(lc, op);
242                 goto finish;
243         }
244
245         /* We pull apart the ber result, stuff it into a slapd entry, and
246          * let send_search_entry stuff it back into ber format. Slow & ugly,
247          * but this is necessary for version matching, and for ACL processing.
248          */
249         
250         for (   count=0, rc=0;
251                         rc != -1;
252                         rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
253         {
254                 int ab;
255
256                 /* check for abandon */
257                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
258                 ab = op->o_abandon;
259                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
260
261                 if (ab) {
262                         ldap_abandon(lc->ld, msgid);
263                         rc = 0;
264                         goto finish;
265                 }
266                 if (rc == 0) {
267                         tv.tv_sec = 0;
268                         tv.tv_usec = 100000;
269                         ldap_pvt_thread_yield();
270                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
271                         e = ldap_first_entry(lc->ld,res);
272                         ldap_send_entry(be, op, lc, e, attrs, attrsonly);
273                         count++;
274                         ldap_msgfree(res);
275                 } else {
276                         sres = ldap_result2error(lc->ld, res, 1);
277                         sres = ldap_back_map_result(sres);
278                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
279                         ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
280                         rc = 0;
281                         break;
282                 }
283         }
284
285         if (rc == -1)
286                 goto fail;
287
288 #ifdef ENABLE_REWRITE
289         /*
290          * Rewrite the matched portion of the search base, if required
291          */
292         if ( match != NULL ) {
293                 switch ( rewrite_session( li->rwinfo, "matchedDn",
294                                 match, conn, &mmatch ) ) {
295                 case REWRITE_REGEXEC_OK:
296                         if ( mmatch == NULL ) {
297                                 mmatch = ( char * )match;
298                         }
299 #ifdef NEW_LOGGING
300                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
301                                         "[rw]  matchedDn:"
302                                         " \"%s\" -> \"%s\"\n",
303                                         match, mmatch ));
304 #else /* !NEW_LOGGING */
305                         Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
306                                         " \"%s\" -> \"%s\"\n%s",
307                                         match, mmatch, "" );
308 #endif /* !NEW_LOGGING */
309                         break;
310                         
311                 case REWRITE_REGEXEC_UNWILLING:
312                         send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
313                                         NULL, "Unwilling to perform",
314                                         NULL, NULL );
315                         
316                 case REWRITE_REGEXEC_ERR:
317                         rc = -1;
318                         goto finish;
319                 }
320         }
321
322         send_search_result( conn, op, sres,
323                 mmatch, err, NULL, NULL, count );
324
325 #else /* !ENABLE_REWRITE */
326         send_search_result( conn, op, sres,
327                 match, err, NULL, NULL, count );
328 #endif /* !ENABLE_REWRITE */
329
330 finish:;
331         if ( match ) {
332 #ifdef ENABLE_REWRITE
333                 if ( mmatch != match ) {
334                         free( mmatch );
335                 }
336 #endif /* ENABLE_REWRITE */
337                 free(match);
338         }
339         if ( err ) {
340                 free( err );
341         }
342         if ( mapped_attrs ) {
343                 free( mapped_attrs );
344         }
345 #ifdef ENABLE_REWRITE
346         if ( mapped_filter != mfilter.bv_val ) {
347                 free( mapped_filter );
348         }
349         if ( mfilter.bv_val != filterstr->bv_val ) {
350                 free( mfilter.bv_val );
351         }
352 #else /* !ENABLE_REWRITE */
353         if ( mapped_filter != filterstr->bv_val ) {
354                 free( mapped_filter );
355         }
356 #endif /* !ENABLE_REWRITE */
357         
358         if ( mbase.bv_val != base->bv_val ) {
359                 free( mbase.bv_val );
360         }
361         
362         return rc;
363 }
364
365 static void
366 ldap_send_entry(
367         Backend *be,
368         Operation *op,
369         struct ldapconn *lc,
370         LDAPMessage *e,
371         AttributeName *attrs,
372         int attrsonly
373 )
374 {
375         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
376         struct berval a, mapped;
377         Entry ent;
378         BerElement ber = *e->lm_ber;
379         Attribute *attr, **attrp;
380         struct berval dummy = { 0, NULL };
381         struct berval *bv, bdn;
382         const char *text;
383
384         if ( ber_scanf( &ber, "{o", &bdn ) == LBER_ERROR ) {
385                 return;
386         }
387 #ifdef ENABLE_REWRITE
388
389         /*
390          * Rewrite the dn of the result, if needed
391          */
392         switch ( rewrite_session( li->rwinfo, "searchResult",
393                                 bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
394         case REWRITE_REGEXEC_OK:
395                 if ( ent.e_name.bv_val == NULL ) {
396                         ent.e_name = bdn;
397                         
398                 } else {
399 #ifdef NEW_LOGGING
400                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
401                                         "[rw] searchResult: \"%s\""
402                                         " -> \"%s\"\n", bdn.bv_val, ent.e_dn ));
403 #else /* !NEW_LOGGING */
404                         Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
405                                         " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
406 #endif /* !NEW_LOGGING */
407                         free( bdn.bv_val );
408                         bdn.bv_val = NULL;
409                         ent.e_name.bv_len = strlen( ent.e_name.bv_val );
410                 }
411                 break;
412                 
413         case REWRITE_REGEXEC_ERR:
414         case REWRITE_REGEXEC_UNWILLING:
415                 return;
416         }
417 #else /* !ENABLE_REWRITE */
418         ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
419 #endif /* !ENABLE_REWRITE */
420
421         dnNormalize2( NULL, &ent.e_name, &ent.e_nname );
422         ent.e_id = 0;
423         ent.e_attrs = 0;
424         ent.e_private = 0;
425         attrp = &ent.e_attrs;
426
427         while ( ber_scanf( &ber, "{{o", &a ) != LBER_ERROR ) {
428                 ldap_back_map(&li->at_map, &a, &mapped, 1);
429                 if (mapped.bv_val == NULL)
430                         continue;
431                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
432                 if (attr == NULL)
433                         continue;
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(( "backend", LDAP_LEVEL_DETAIL1,
441                                                 "slap_str2undef_ad(%s): "
442                                                 "%s\n", mapped.bv_val, text ));
443 #else /* !NEW_LOGGING */
444                                 Debug( LDAP_DEBUG_ANY, 
445                                                 "slap_str2undef_ad(%s): "
446                                                 "%s\n%s", mapped.bv_val, text, "" );
447 #endif /* !NEW_LOGGING */
448                                 
449                                 ch_free(attr);
450                                 continue;
451                         }
452                 }
453                 if (ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR ) {
454                         attr->a_vals = &dummy;
455                 } else if ( strcasecmp( mapped.bv_val, "objectclass" ) == 0 ) {
456                         int i, last;
457                         for ( last = 0; attr->a_vals[last].bv_val; last++ ) ;
458                         for ( i = 0; ( bv = &attr->a_vals[i] ); i++ ) {
459                                 ldap_back_map(&li->oc_map, bv, &mapped, 1);
460                                 if (mapped.bv_val == NULL) {
461                                         free(attr->a_vals[i].bv_val);
462                                         attr->a_vals[i].bv_val = NULL;
463                                         if (--last < 0)
464                                                 break;
465                                         attr->a_vals[i] = attr->a_vals[last];
466                                         attr->a_vals[last].bv_val = NULL;
467                                         i--;
468                                 } else if ( mapped.bv_val != bv->bv_val ) {
469                                         ch_free(bv->bv_val);
470                                         ber_dupbv( bv, &mapped );
471                                 }
472                         }
473
474 #ifdef ENABLE_REWRITE
475                 /*
476                  * It is necessary to try to rewrite attributes with
477                  * dn syntax because they might be used in ACLs as
478                  * members of groups; since ACLs are applied to the
479                  * rewritten stuff, no dn-based subecj clause could
480                  * be used at the ldap backend side (see
481                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
482                  * The problem can be overcome by moving the dn-based
483                  * ACLs to the target directory server, and letting
484                  * everything pass thru the ldap backend.
485                  */
486                 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
487                                         SLAPD_DN_SYNTAX ) == 0 ) {
488                         int i;
489                         for ( i = 0; ( bv = &attr->a_vals[ i ] ); i++ ) {
490                                 char *newval;
491                                 
492                                 switch ( rewrite_session( li->rwinfo,
493                                                         "searchResult",
494                                                         bv->bv_val,
495                                                         lc->conn, &newval )) {
496                                 case REWRITE_REGEXEC_OK:
497                                         /* left as is */
498                                         if ( newval == NULL ) {
499                                                 break;
500                                         }
501 #ifdef NEW_LOGGING
502                                         LDAP_LOG(( "backend",
503                                                         LDAP_LEVEL_DETAIL1,
504                                                         "[rw] searchResult on"
505                                                         " attr=%s:"
506                                                         " \"%s\" -> \"%s\"\n",
507                                                         attr->a_desc->ad_type->sat_cname.bv_val,
508                                                         bv->bv_val, newval ));
509 #else /* !NEW_LOGGING */
510                                         Debug( LDAP_DEBUG_ARGS,
511                 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
512                                                 attr->a_desc->ad_type->sat_cname.bv_val,
513                                                 bv->bv_val, newval );
514 #endif /* !NEW_LOGGING */
515                                         
516                                         free( bv->bv_val );
517                                         bv->bv_val = newval;
518                                         bv->bv_len = strlen( newval );
519                                         
520                                         break;
521                                         
522                                 case REWRITE_REGEXEC_UNWILLING:
523                                         
524                                 case REWRITE_REGEXEC_ERR:
525                                         /*
526                                          * FIXME: better give up,
527                                          * skip the attribute
528                                          * or leave it untouched?
529                                          */
530                                         break;
531                                 }
532                         }
533 #endif /* ENABLE_REWRITE */
534                 }
535
536                 *attrp = attr;
537                 attrp = &attr->a_next;
538         }
539         send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
540         while (ent.e_attrs) {
541                 attr = ent.e_attrs;
542                 ent.e_attrs = attr->a_next;
543                 if (attr->a_vals != &dummy)
544                         bvarray_free(attr->a_vals);
545                 free(attr);
546         }
547         
548         if ( ent.e_dn )
549                 free( ent.e_dn );
550         if ( ent.e_ndn )
551                 free( ent.e_ndn );
552 }
553