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