]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
document option '-F'
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31
32 #include "slap.h"
33 #include "back-ldap.h"
34 #undef ldap_debug       /* silence a warning in ldap-int.h */
35 #include "../../../libraries/libldap/ldap-int.h"
36
37 #include "lutil.h"
38
39 static int
40 ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
41          struct berval *bdn, int flags );
42 #define LDAP_BUILD_ENTRY_PRIVATE        0x01
43
44 int
45 ldap_back_search(
46                 Operation       *op,
47                 SlapReply       *rs )
48 {
49         struct ldapconn *lc;
50         struct timeval  tv;
51         LDAPMessage     *res,
52                         *e;
53         int             rc = 0,
54                         msgid; 
55         struct berval   match = BER_BVNULL;
56         int             i;
57         char            **attrs = NULL;
58         int             dontfreetext = 0;
59         int             do_retry = 1;
60         LDAPControl     **ctrls = NULL;
61
62         lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
63         if ( !lc ) {
64                 return rs->sr_err;
65         }
66
67         /*
68          * FIXME: in case of values return filter, we might want
69          * to map attrs and maybe rewrite value
70          */
71         if ( !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
72                 return rs->sr_err;
73         }
74
75         /* should we check return values? */
76         if ( op->ors_deref != -1 ) {
77                 ldap_set_option( lc->lc_ld, LDAP_OPT_DEREF,
78                                 (void *)&op->ors_deref );
79         }
80
81         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
82                 tv.tv_sec = op->ors_tlimit;
83                 tv.tv_usec = 0;
84
85         } else {
86                 tv.tv_sec = 0;
87         }
88
89         if ( op->ors_attrs ) {
90                 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ )
91                         /* just count attrs */ ;
92
93                 attrs = ch_malloc( ( i + 1 )*sizeof( char * ) );
94                 if ( attrs == NULL ) {
95                         rs->sr_err = LDAP_NO_MEMORY;
96                         rc = -1;
97                         goto finish;
98                 }
99         
100                 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ ) {
101                         attrs[ i ] = op->ors_attrs[i].an_name.bv_val;
102                 }
103                 attrs[ i ] = NULL;
104         }
105
106         ctrls = op->o_ctrls;
107         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
108         if ( rc != LDAP_SUCCESS ) {
109                 dontfreetext = 1;
110                 goto finish;
111         }
112         
113 retry:
114         rs->sr_err = ldap_search_ext( lc->lc_ld, op->o_req_ndn.bv_val,
115                         op->ors_scope, op->ors_filterstr.bv_val,
116                         attrs, op->ors_attrsonly, ctrls, NULL,
117                         tv.tv_sec ? &tv : NULL,
118                         op->ors_slimit, &msgid );
119
120         if ( rs->sr_err != LDAP_SUCCESS ) {
121 fail:;
122                 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
123                         if ( do_retry ) {
124                                 do_retry = 0;
125                                 if ( ldap_back_retry( lc, op, rs, LDAP_BACK_DONTSEND ) ) {
126                                         goto retry;
127                                 }
128                         }
129                         rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
130                         ldap_back_freeconn( op, lc );
131                         lc = NULL;
132                         goto finish;
133                 }
134         }
135
136         /* We pull apart the ber result, stuff it into a slapd entry, and
137          * let send_search_entry stuff it back into ber format. Slow & ugly,
138          * but this is necessary for version matching, and for ACL processing.
139          */
140
141         for ( rc = 0; rc != -1; rc = ldap_result( lc->lc_ld, msgid, 0, &tv, &res ) )
142         {
143                 /* check for abandon */
144                 if ( op->o_abandon ) {
145                         ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
146                         rc = SLAPD_ABANDON;
147                         goto finish;
148                 }
149
150                 if ( rc == 0 ) {
151                         tv.tv_sec = 0;
152                         tv.tv_usec = 100000;
153                         ldap_pvt_thread_yield();
154
155                 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
156                         Entry           ent = {0};
157                         struct berval   bdn;
158                         int             abort = 0;
159
160                         do_retry = 0;
161
162                         e = ldap_first_entry( lc->lc_ld, res );
163                         rc = ldap_build_entry( op, e, &ent, &bdn,
164                                         LDAP_BUILD_ENTRY_PRIVATE );
165                        if ( rc == LDAP_SUCCESS ) {
166                                 rs->sr_entry = &ent;
167                                 rs->sr_attrs = op->ors_attrs;
168                                 rs->sr_operational_attrs = NULL;
169                                 rs->sr_flags = 0;
170                                 abort = send_search_entry( op, rs );
171                                 while ( ent.e_attrs ) {
172                                         Attribute       *a;
173                                         BerVarray       v;
174
175                                         a = ent.e_attrs;
176                                         ent.e_attrs = a->a_next;
177
178                                         v = a->a_vals;
179                                         if ( a->a_vals != &slap_dummy_bv ) {
180                                                 ber_bvarray_free( a->a_vals );
181                                         }
182                                         if ( a->a_nvals != v ) {
183                                                 ber_bvarray_free( a->a_nvals );
184                                         }
185                                         ch_free( a );
186                                 }
187                                 
188                                 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) ) {
189                                         free( ent.e_dn );
190                                 }
191                                 if ( ent.e_ndn ) {
192                                         free( ent.e_ndn );
193                                 }
194                         }
195                         ldap_msgfree( res );
196                         if ( abort ) {
197                                 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
198                                 goto finish;
199                         }
200
201                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
202                         char            **references = NULL;
203                         int             cnt;
204
205                         do_retry = 0;
206                         rc = ldap_parse_reference( lc->lc_ld, res,
207                                         &references, &rs->sr_ctrls, 1 );
208
209                         if ( rc != LDAP_SUCCESS ) {
210                                 continue;
211                         }
212
213                         if ( references == NULL ) {
214                                 continue;
215                         }
216
217                         for ( cnt = 0; references[ cnt ]; cnt++ )
218                                 /* NO OP */ ;
219                                 
220                         rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
221
222                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
223                                 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
224                         }
225
226                         /* ignore return value by now */
227                         ( void )send_search_reference( op, rs );
228
229                         /* cleanup */
230                         if ( references ) {
231                                 ldap_value_free( references );
232                                 ch_free( rs->sr_ref );
233                                 rs->sr_ref = NULL;
234                         }
235
236                         if ( rs->sr_ctrls ) {
237                                 ldap_controls_free( rs->sr_ctrls );
238                                 rs->sr_ctrls = NULL;
239                         }
240
241                 } else {
242                         char            **references = NULL;
243
244                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
245                                         &match.bv_val, (char **)&rs->sr_text,
246                                         &references, &rs->sr_ctrls, 1 );
247                         if ( rc != LDAP_SUCCESS ) {
248                                 rs->sr_err = rc;
249                         }
250                         rs->sr_err = slap_map_api2result( rs );
251
252                         if ( references ) {
253                                 int     cnt;
254
255                                 for ( cnt = 0; references[ cnt ]; cnt++ )
256                                         /* NO OP */ ;
257                                 
258                                 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
259
260                                 for ( cnt = 0; references[ cnt ]; cnt++ ) {
261                                         ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
262                                 }
263
264                                 /* cleanup */
265                                 if ( references ) {
266                                         ldap_value_free( references );
267                                 }
268                         }
269
270                         rc = 0;
271                         break;
272                 }
273         }
274
275         if ( rc == -1 ) {
276                 if ( do_retry ) {
277                         do_retry = 0;
278                         if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) ) {
279                                 goto retry;
280                         }
281                 }
282                 rs->sr_err = LDAP_SERVER_DOWN;
283                 goto fail;
284         }
285
286         /*
287          * Rewrite the matched portion of the search base, if required
288          */
289         if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
290                 rs->sr_matched = match.bv_val;
291         }
292         if ( rs->sr_v2ref ) {
293                 rs->sr_err = LDAP_REFERRAL;
294         }
295
296 finish:;
297         if ( rc != SLAPD_ABANDON ) {
298                 send_ldap_result( op, rs );
299         }
300
301         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
302
303         if ( rs->sr_ctrls ) {
304                 ldap_controls_free( rs->sr_ctrls );
305                 rs->sr_ctrls = NULL;
306         }
307
308         if ( match.bv_val ) {
309                 rs->sr_matched = NULL;
310                 LDAP_FREE( match.bv_val );
311         }
312
313         if ( rs->sr_text ) {
314                 if ( !dontfreetext ) {
315                         LDAP_FREE( (char *)rs->sr_text );
316                 }
317                 rs->sr_text = NULL;
318         }
319
320         if ( rs->sr_ref ) {
321                 ber_bvarray_free( rs->sr_ref );
322                 rs->sr_ref = NULL;
323         }
324
325         if ( attrs ) {
326                 ch_free( attrs );
327         }
328
329         return rc;
330 }
331
332 static int
333 ldap_build_entry(
334                 Operation       *op,
335                 LDAPMessage     *e,
336                 Entry           *ent,
337                 struct berval   *bdn,
338                 int             flags )
339 {
340         struct berval   a;
341         BerElement      ber = *e->lm_ber;
342         Attribute       *attr, **attrp;
343         const char      *text;
344         int             last;
345         int             private = flags & LDAP_BUILD_ENTRY_PRIVATE;
346
347         /* safe assumptions ... */
348         assert( ent );
349         BER_BVZERO( &ent->e_bv );
350
351         if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
352                 return LDAP_DECODING_ERROR;
353         }
354
355         /*
356          * Note: this may fail if the target host(s) schema differs
357          * from the one known to the meta, and a DN with unknown
358          * attributes is returned.
359          * 
360          * FIXME: should we log anything, or delegate to dnNormalize?
361          */
362         /* Note: if the distinguished values or the naming attributes
363          * change, should we massage them as well?
364          */
365         if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
366                 op->o_tmpmemctx ) != LDAP_SUCCESS )
367         {
368                 return LDAP_INVALID_DN_SYNTAX;
369         }
370
371         attrp = &ent->e_attrs;
372
373         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
374                 int                             i;
375                 slap_syntax_validate_func       *validate;
376                 slap_syntax_transform_func      *pretty;
377
378                 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
379                 if ( attr == NULL ) {
380                         continue;
381                 }
382                 attr->a_flags = 0;
383                 attr->a_next = 0;
384                 attr->a_desc = NULL;
385                 if ( slap_bv2ad( &a, &attr->a_desc, &text ) 
386                                 != LDAP_SUCCESS )
387                 {
388                         if ( slap_bv2undef_ad( &a, &attr->a_desc, &text ) 
389                                         != LDAP_SUCCESS )
390                         {
391                                 Debug( LDAP_DEBUG_ANY, 
392                                         "slap_bv2undef_ad(%s):  %s\n",
393                                         a.bv_val, text, 0 );
394                                 ch_free( attr );
395                                 continue;
396                         }
397                 }
398
399                 /* no subschemaSubentry */
400                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
401
402                         /* 
403                          * We eat target's subschemaSubentry because
404                          * a search for this value is likely not
405                          * to resolve to the appropriate backend;
406                          * later, the local subschemaSubentry is
407                          * added.
408                          */
409                         ( void )ber_scanf( &ber, "x" /* [W] */ );
410
411                         ch_free( attr );
412                         continue;
413                 }
414                 
415                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
416                                 || attr->a_vals == NULL )
417                 {
418                         /*
419                          * Note: attr->a_vals can be null when using
420                          * values result filter
421                          */
422                         if ( private ) {
423                                 attr->a_vals = (struct berval *)&slap_dummy_bv;
424                                 
425                         } else {
426                                 attr->a_vals = ch_malloc( sizeof( struct berval ) );
427                                 BER_BVZERO( &attr->a_vals[ 0 ] );
428                         }
429                         last = 0;
430
431                 } else {
432                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); last++ )
433                                 /* just count vals */ ;
434                 }
435
436                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
437                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
438
439                 if ( !validate && !pretty ) {
440                         attr->a_nvals = NULL;
441                         attr_free( attr );
442                         goto next_attr;
443                 }
444
445                 for ( i = 0; i < last; i++ ) {
446                         struct berval   pval;
447                         int             rc;
448
449                         if ( pretty ) {
450                                 rc = pretty( attr->a_desc->ad_type->sat_syntax,
451                                         &attr->a_vals[i], &pval, NULL );
452
453                         } else {
454                                 rc = validate( attr->a_desc->ad_type->sat_syntax,
455                                         &attr->a_vals[i] );
456                         }
457
458                         if ( rc != LDAP_SUCCESS ) {
459                                 /* check if, by chance, it's an undefined objectClass */
460                                 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
461                                                 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
462                                 {
463                                         ber_dupbv( &pval, &attr->a_vals[i] );
464
465                                 } else {
466                                         attr->a_nvals = NULL;
467                                         attr_free( attr );
468                                         goto next_attr;
469                                 }
470                         }
471
472                         if ( pretty ) {
473                                 LBER_FREE( attr->a_vals[i].bv_val );
474                                 attr->a_vals[i] = pval;
475                         }
476                 }
477
478                 if ( last && attr->a_desc->ad_type->sat_equality &&
479                                 attr->a_desc->ad_type->sat_equality->smr_normalize )
480                 {
481                         attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
482                         for ( i = 0; i < last; i++ ) {
483                                 int             rc;
484
485                                 /*
486                                  * check that each value is valid per syntax
487                                  * and pretty if appropriate
488                                  */
489                                 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
490                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
491                                         attr->a_desc->ad_type->sat_syntax,
492                                         attr->a_desc->ad_type->sat_equality,
493                                         &attr->a_vals[i], &attr->a_nvals[i],
494                                         NULL );
495
496                                 if ( rc != LDAP_SUCCESS ) {
497                                         BER_BVZERO( &attr->a_nvals[i] );
498                                         ch_free( attr );
499                                         goto next_attr;
500                                 }
501                         }
502                         BER_BVZERO( &attr->a_nvals[i] );
503
504                 } else {
505                         attr->a_nvals = attr->a_vals;
506                 }
507                 *attrp = attr;
508                 attrp = &attr->a_next;
509
510 next_attr:;
511         }
512
513         return LDAP_SUCCESS;
514 }
515
516 /* return 0 IFF we can retrieve the entry with ndn
517  */
518 int
519 ldap_back_entry_get(
520                 Operation               *op,
521                 struct berval           *ndn,
522                 ObjectClass             *oc,
523                 AttributeDescription    *at,
524                 int                     rw,
525                 Entry                   **ent
526 )
527 {
528         struct ldapconn *lc;
529         int             rc = 1,
530                         do_not_cache;
531         struct berval   bdn;
532         LDAPMessage     *result = NULL,
533                         *e = NULL;
534         char            *gattr[3];
535         char            *filter = NULL;
536         SlapReply       rs;
537         int             do_retry = 1;
538         LDAPControl     **ctrls = NULL;
539
540         /* Tell getconn this is a privileged op */
541         do_not_cache = op->o_do_not_cache;
542         op->o_do_not_cache = 1;
543         lc = ldap_back_getconn( op, &rs, LDAP_BACK_DONTSEND );
544         if ( !lc || !ldap_back_dobind( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
545                 op->o_do_not_cache = do_not_cache;
546                 return rs.sr_err;
547         }
548         op->o_do_not_cache = do_not_cache;
549
550         if ( at ) {
551                 if ( oc && at != slap_schema.si_ad_objectClass ) {
552                         gattr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
553                         gattr[1] = at->ad_cname.bv_val;
554                         gattr[2] = NULL;
555
556                 } else {
557                         gattr[0] = at->ad_cname.bv_val;
558                         gattr[1] = NULL;
559                 }
560         }
561
562         if ( oc ) {
563                 char    *ptr;
564
565                 filter = ch_malloc( STRLENOF( "(objectclass=)" ) 
566                                 + oc->soc_cname.bv_len + 1 );
567                 ptr = lutil_strcopy( filter, "(objectclass=" );
568                 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
569                 *ptr++ = ')';
570                 *ptr++ = '\0';
571         }
572
573         ctrls = op->o_ctrls;
574         rc = ldap_back_proxy_authz_ctrl( lc, op, &rs, &ctrls );
575         if ( rc != LDAP_SUCCESS ) {
576                 goto cleanup;
577         }
578         
579 retry:
580         rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
581                                 at ? gattr : NULL, 0, ctrls, NULL,
582                                 LDAP_NO_LIMIT, LDAP_NO_LIMIT, &result );
583         if ( rc != LDAP_SUCCESS ) {
584                 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
585                         do_retry = 0;
586                         if ( ldap_back_retry( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
587                                 goto retry;
588                         }
589                 }
590                 goto cleanup;
591         }
592
593         e = ldap_first_entry( lc->lc_ld, result );
594         if ( e == NULL ) {
595                 goto cleanup;
596         }
597
598         *ent = ch_calloc( 1, sizeof( Entry ) );
599
600         rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
601
602         if ( rc != LDAP_SUCCESS ) {
603                 ch_free( *ent );
604                 *ent = NULL;
605         }
606
607 cleanup:
608         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
609
610         if ( result ) {
611                 ldap_msgfree( result );
612         }
613
614         if ( filter ) {
615                 ch_free( filter );
616         }
617
618         return rc;
619 }
620