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