]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
7fdb10973fe82c1e30c5fb2ab896ea3186bd765b
[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     struct berval       *base,
58     struct berval       *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, &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->bv_val, conn, &mbase ) ) {
147         case REWRITE_REGEXEC_OK:
148                 if ( mbase == NULL ) {
149                         mbase = ( char * )base->bv_val;
150                 }
151 #ifdef NEW_LOGGING
152                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
153                                 "[rw] searchBase: \"%s\" -> \"%s\"\n%",
154                                 base->bv_val, mbase ));
155 #else /* !NEW_LOGGING */
156                 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
157                                 base->bv_val, 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->bv_val ), 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 && attrs) {
225                 for (count=0; attrs[count]; count++);
226                 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
227                 for (count=0; attrs[count]; count++) {
228                         mapped_attrs[count] = attrs[count]->bv_val;
229                 }
230                 mapped_attrs[count] = NULL;
231         }
232
233         if ((msgid = ldap_search(lc->ld, mbase, scope, mapped_filter, mapped_attrs,
234                 attrsonly)) == -1)
235         {
236 fail:;
237                 rc = ldap_back_op_result(lc, op);
238                 goto finish;
239         }
240
241         /* We pull apart the ber result, stuff it into a slapd entry, and
242          * let send_search_entry stuff it back into ber format. Slow & ugly,
243          * but this is necessary for version matching, and for ACL processing.
244          */
245         
246         for (   count=0, rc=0;
247                         rc != -1;
248                         rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
249         {
250                 int ab;
251
252                 /* check for abandon */
253                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
254                 ab = op->o_abandon;
255                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
256
257                 if (ab) {
258                         ldap_abandon(lc->ld, msgid);
259                         rc = 0;
260                         goto finish;
261                 }
262                 if (rc == 0) {
263                         tv.tv_sec = 0;
264                         tv.tv_usec = 100000;
265                         ldap_pvt_thread_yield();
266                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
267                         e = ldap_first_entry(lc->ld,res);
268                         ldap_send_entry(be, op, lc, e, attrs, attrsonly);
269                         count++;
270                         ldap_msgfree(res);
271                 } else {
272                         sres = ldap_result2error(lc->ld, res, 1);
273                         sres = ldap_back_map_result(sres);
274                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
275                         ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
276                         rc = 0;
277                         break;
278                 }
279         }
280
281         if (rc == -1)
282                 goto fail;
283
284 #ifdef ENABLE_REWRITE
285         /*
286          * Rewrite the matched portion of the search base, if required
287          */
288         if ( match != NULL ) {
289                 switch ( rewrite_session( li->rwinfo, "matchedDn",
290                                 match, conn, &mmatch ) ) {
291                 case REWRITE_REGEXEC_OK:
292                         if ( mmatch == NULL ) {
293                                 mmatch = ( char * )match;
294                         }
295 #ifdef NEW_LOGGING
296                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
297                                         "[rw]  matchedDn:"
298                                         " \"%s\" -> \"%s\"\n",
299                                         match, mmatch ));
300 #else /* !NEW_LOGGING */
301                         Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
302                                         " \"%s\" -> \"%s\"\n%s",
303                                         match, mmatch, "" );
304 #endif /* !NEW_LOGGING */
305                         break;
306                         
307                 case REWRITE_REGEXEC_UNWILLING:
308                         send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
309                                         NULL, "Unwilling to perform",
310                                         NULL, NULL );
311                         
312                 case REWRITE_REGEXEC_ERR:
313                         rc = -1;
314                         goto finish;
315                 }
316         }
317
318         send_search_result( conn, op, sres,
319                 mmatch, err, NULL, NULL, count );
320
321 #else /* !ENABLE_REWRITE */
322         send_search_result( conn, op, sres,
323                 match, err, NULL, NULL, count );
324 #endif /* !ENABLE_REWRITE */
325
326 finish:;
327         if ( match ) {
328 #ifdef ENABLE_REWRITE
329                 if ( mmatch != match ) {
330                         free( mmatch );
331                 }
332 #endif /* ENABLE_REWRITE */
333                 free(match);
334         }
335         if ( err ) {
336                 free( err );
337         }
338         if ( mapped_attrs ) {
339                 free( mapped_attrs );
340         }
341 #ifdef ENABLE_REWRITE
342         if ( mapped_filter != mfilter ) {
343                 free( mapped_filter );
344         }
345         if ( mfilter != filterstr ) {
346                 free( mfilter );
347         }
348 #else /* !ENABLE_REWRITE */
349         if ( mapped_filter != filterstr ) {
350                 free( mapped_filter );
351         }
352 #endif /* !ENABLE_REWRITE */
353         
354 #ifdef ENABLE_REWRITE
355         if ( mbase != base->bv_val ) {
356 #endif /* ENABLE_REWRITE */
357                 free( mbase );
358 #ifdef ENABLE_REWRITE
359         }
360 #endif /* ENABLE_REWRITE */
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         struct berval **attrs,
372         int attrsonly
373 )
374 {
375         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
376         char *a, *mapped;
377         Entry ent;
378         BerElement *ber = NULL;
379         Attribute *attr, **attrp;
380         struct berval *dummy = NULL;
381         struct berval *bv;
382         const char *text;
383
384 #ifdef ENABLE_REWRITE
385         char *dn;
386
387         dn = ldap_get_dn(lc->ld, e);
388         if ( dn == NULL ) {
389                 return;
390         }
391
392         /*
393          * Rewrite the dn of the result, if needed
394          */
395         switch ( rewrite_session( li->rwinfo, "searchResult",
396                                 dn, lc->conn, &ent.e_dn ) ) {
397         case REWRITE_REGEXEC_OK:
398                 if ( ent.e_dn == NULL ) {
399                         ent.e_dn = dn;
400                 } else {
401 #ifdef NEW_LOGGING
402                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
403                                         "[rw] searchResult: \"%s\""
404                                         " -> \"%s\"\n", dn, ent.e_dn ));
405 #else /* !NEW_LOGGING */
406                         Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
407                                         " -> \"%s\"\n%s", dn, ent.e_dn, "" );
408 #endif /* !NEW_LOGGING */
409                         free( dn );
410                         dn = NULL;
411                 }
412                 break;
413                 
414         case REWRITE_REGEXEC_ERR:
415         case REWRITE_REGEXEC_UNWILLING:
416                 free( dn );
417                 return;
418         }
419 #else /* !ENABLE_REWRITE */
420         ent.e_dn = ldap_back_dn_restore( li, ldap_get_dn(lc->ld, e), 0 );
421 #endif /* !ENABLE_REWRITE */
422
423         ent.e_ndn = ch_strdup( ent.e_dn );
424         (void) dn_normalize( ent.e_ndn );
425         ent.e_id = 0;
426         ent.e_attrs = 0;
427         ent.e_private = 0;
428         attrp = &ent.e_attrs;
429
430         for (   a = ldap_first_attribute(lc->ld, e, &ber);
431                         a != NULL;
432                         a = ldap_next_attribute(lc->ld, e, ber))
433         {
434                 mapped = ldap_back_map(&li->at_map, a, 1);
435                 if (mapped == NULL)
436                         continue;
437                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
438                 if (attr == NULL)
439                         continue;
440                 attr->a_next = 0;
441                 attr->a_desc = NULL;
442                 if (slap_str2ad(mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
443                         if (slap_str2undef_ad(mapped, &attr->a_desc, &text) 
444                                         != LDAP_SUCCESS) {
445 #ifdef NEW_LOGGING
446                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
447                                                 "slap_str2undef_ad(%s): "
448                                                 "%s\n", mapped, text ));
449 #else /* !NEW_LOGGING */
450                                 Debug( LDAP_DEBUG_ANY, 
451                                                 "slap_str2undef_ad(%s): "
452                                                 "%s\n%s", mapped, text, "" );
453 #endif /* !NEW_LOGGING */
454                                 
455                                 ch_free(attr);
456                                 continue;
457                         }
458                 }
459                 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
460                 if (!attr->a_vals) {
461                         attr->a_vals = &dummy;
462                 } else if ( strcasecmp( mapped, "objectclass" ) == 0 ) {
463                         int i, last;
464                         for ( last = 0; attr->a_vals[last]; last++ ) ;
465                         for ( i = 0; ( bv = attr->a_vals[i] ); i++ ) {
466                                 mapped = ldap_back_map(&li->oc_map, bv->bv_val, 1);
467                                 if (mapped == NULL) {
468                                         ber_bvfree(attr->a_vals[i]);
469                                         attr->a_vals[i] = NULL;
470                                         if (--last < 0)
471                                                 break;
472                                         attr->a_vals[i] = attr->a_vals[last];
473                                         attr->a_vals[last] = NULL;
474                                         i--;
475                                 } else if ( mapped != bv->bv_val ) {
476                                         ch_free(bv->bv_val);
477                                         bv->bv_val = ch_strdup( mapped );
478                                         bv->bv_len = strlen( 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[ i ] ); 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                                         
524                                         free( bv->bv_val );
525                                         bv->bv_val = newval;
526                                         bv->bv_len = strlen( newval );
527                                         
528                                         break;
529                                         
530                                 case REWRITE_REGEXEC_UNWILLING:
531                                         
532                                 case REWRITE_REGEXEC_ERR:
533                                         /*
534                                          * FIXME: better give up,
535                                          * skip the attribute
536                                          * or leave it untouched?
537                                          */
538                                         break;
539                                 }
540                         }
541 #endif /* ENABLE_REWRITE */
542                 }
543
544                 *attrp = attr;
545                 attrp = &attr->a_next;
546         }
547         send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
548         while (ent.e_attrs) {
549                 attr = ent.e_attrs;
550                 ent.e_attrs = attr->a_next;
551                 if (attr->a_vals != &dummy)
552                         ber_bvecfree(attr->a_vals);
553                 free(attr);
554         }
555         if (ber)
556                 ber_free(ber,0);
557         
558         if ( ent.e_dn )
559                 free( ent.e_dn );
560         if ( ent.e_ndn )
561                 free( ent.e_ndn );
562 }
563