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