]> 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 );
64         if ( !lc ) {
65                 return -1;
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 ) ) {
73                 return -1;
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, 0 );
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                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
238                                         &match.bv_val, (char **)&rs->sr_text,
239                                         NULL, &rs->sr_ctrls, 1 );
240                         if (rc != LDAP_SUCCESS ) {
241                                 rs->sr_err = rc;
242                         }
243                         rs->sr_err = slap_map_api2result( rs );
244                         rc = 0;
245                         break;
246                 }
247         }
248
249         if ( rc == -1 ) {
250                 if ( do_retry ) {
251                         do_retry = 0;
252                         if ( ldap_back_retry( lc, op, rs ) ) {
253                                 goto retry;
254                         }
255                 }
256                 /* FIXME: invalidate the connection? */
257                 rs->sr_err = LDAP_SERVER_DOWN;
258                 freeconn = 1;
259                 goto fail;
260         }
261
262         /*
263          * Rewrite the matched portion of the search base, if required
264          */
265         if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
266                 rs->sr_matched = match.bv_val;
267         }
268         if ( rs->sr_v2ref ) {
269                 rs->sr_err = LDAP_REFERRAL;
270         }
271
272 finish:;
273         send_ldap_result( op, rs );
274
275         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
276
277         if ( rs->sr_ctrls ) {
278                 ldap_controls_free( rs->sr_ctrls );
279                 rs->sr_ctrls = NULL;
280         }
281
282         if ( match.bv_val ) {
283                 rs->sr_matched = NULL;
284                 LDAP_FREE( match.bv_val );
285         }
286         if ( rs->sr_text ) {
287                 if ( !dontfreetext ) {
288                         LDAP_FREE( (char *)rs->sr_text );
289                 }
290                 rs->sr_text = NULL;
291         }
292         if ( attrs ) {
293                 ch_free( attrs );
294         }
295
296         return rc;
297 }
298
299 static int
300 ldap_build_entry(
301                 Operation       *op,
302                 LDAPMessage     *e,
303                 Entry           *ent,
304                 struct berval   *bdn,
305                 int             flags )
306 {
307         struct berval   a;
308         BerElement      ber = *e->lm_ber;
309         Attribute       *attr, **attrp;
310         const char      *text;
311         int             last;
312         int             private = flags & LDAP_BUILD_ENTRY_PRIVATE;
313
314         /* safe assumptions ... */
315         assert( ent );
316         BER_BVZERO( &ent->e_bv );
317
318         if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
319                 return LDAP_DECODING_ERROR;
320         }
321
322         /*
323          * Note: this may fail if the target host(s) schema differs
324          * from the one known to the meta, and a DN with unknown
325          * attributes is returned.
326          * 
327          * FIXME: should we log anything, or delegate to dnNormalize?
328          */
329         /* Note: if the distinguished values or the naming attributes
330          * change, should we massage them as well?
331          */
332         if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
333                 op->o_tmpmemctx ) != LDAP_SUCCESS )
334         {
335                 return LDAP_INVALID_DN_SYNTAX;
336         }
337
338         attrp = &ent->e_attrs;
339
340         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
341                 int                             i;
342                 slap_syntax_validate_func       *validate;
343                 slap_syntax_transform_func      *pretty;
344
345                 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
346                 if ( attr == NULL ) {
347                         continue;
348                 }
349                 attr->a_flags = 0;
350                 attr->a_next = 0;
351                 attr->a_desc = NULL;
352                 if ( slap_bv2ad( &a, &attr->a_desc, &text ) 
353                                 != LDAP_SUCCESS )
354                 {
355                         if ( slap_bv2undef_ad( &a, &attr->a_desc, &text ) 
356                                         != LDAP_SUCCESS )
357                         {
358                                 Debug( LDAP_DEBUG_ANY, 
359                                         "slap_bv2undef_ad(%s):  %s\n",
360                                         a.bv_val, text, 0 );
361                                 ch_free( attr );
362                                 continue;
363                         }
364                 }
365
366                 /* no subschemaSubentry */
367                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
368
369                         /* 
370                          * We eat target's subschemaSubentry because
371                          * a search for this value is likely not
372                          * to resolve to the appropriate backend;
373                          * later, the local subschemaSubentry is
374                          * added.
375                          */
376                         ( void )ber_scanf( &ber, "x" /* [W] */ );
377
378                         ch_free( attr );
379                         continue;
380                 }
381                 
382                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
383                                 || attr->a_vals == NULL )
384                 {
385                         /*
386                          * Note: attr->a_vals can be null when using
387                          * values result filter
388                          */
389                         if ( private ) {
390                                 attr->a_vals = (struct berval *)&slap_dummy_bv;
391                                 
392                         } else {
393                                 attr->a_vals = ch_malloc( sizeof( struct berval ) );
394                                 BER_BVZERO( &attr->a_vals[ 0 ] );
395                         }
396                         last = 0;
397
398                 } else {
399                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); last++ )
400                                 /* just count vals */ ;
401                 }
402
403                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
404                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
405
406                 if ( !validate && !pretty ) {
407                         attr->a_nvals = NULL;
408                         attr_free( attr );
409                         goto next_attr;
410                 }
411
412                 for ( i = 0; i < last; i++ ) {
413                         struct berval   pval;
414                         int             rc;
415
416                         if ( pretty ) {
417                                 rc = pretty( attr->a_desc->ad_type->sat_syntax,
418                                         &attr->a_vals[i], &pval, NULL );
419
420                         } else {
421                                 rc = validate( attr->a_desc->ad_type->sat_syntax,
422                                         &attr->a_vals[i] );
423                         }
424
425                         if ( rc != LDAP_SUCCESS ) {
426                                 /* check if, by chance, it's an undefined objectClass */
427                                 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
428                                                 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
429                                 {
430                                         ber_dupbv( &pval, &attr->a_vals[i] );
431
432                                 } else {
433                                         attr->a_nvals = NULL;
434                                         attr_free( attr );
435                                         goto next_attr;
436                                 }
437                         }
438
439                         if ( pretty ) {
440                                 LBER_FREE( attr->a_vals[i].bv_val );
441                                 attr->a_vals[i] = pval;
442                         }
443                 }
444
445                 if ( last && attr->a_desc->ad_type->sat_equality &&
446                                 attr->a_desc->ad_type->sat_equality->smr_normalize )
447                 {
448                         attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
449                         for ( i = 0; i < last; i++ ) {
450                                 int             rc;
451
452                                 /*
453                                  * check that each value is valid per syntax
454                                  * and pretty if appropriate
455                                  */
456                                 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
457                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
458                                         attr->a_desc->ad_type->sat_syntax,
459                                         attr->a_desc->ad_type->sat_equality,
460                                         &attr->a_vals[i], &attr->a_nvals[i],
461                                         NULL );
462
463                                 if ( rc != LDAP_SUCCESS ) {
464                                         BER_BVZERO( &attr->a_nvals[i] );
465                                         ch_free( attr );
466                                         goto next_attr;
467                                 }
468                         }
469                         BER_BVZERO( &attr->a_nvals[i] );
470
471                 } else {
472                         attr->a_nvals = attr->a_vals;
473                 }
474                 *attrp = attr;
475                 attrp = &attr->a_next;
476
477 next_attr:;
478         }
479
480         return LDAP_SUCCESS;
481 }
482
483 /* return 0 IFF we can retrieve the entry with ndn
484  */
485 int
486 ldap_back_entry_get(
487                 Operation               *op,
488                 struct berval           *ndn,
489                 ObjectClass             *oc,
490                 AttributeDescription    *at,
491                 int                     rw,
492                 Entry                   **ent
493 )
494 {
495         struct ldapconn *lc;
496         int             rc = 1,
497                         do_not_cache;
498         struct berval   bdn;
499         LDAPMessage     *result = NULL,
500                         *e = NULL;
501         char            *gattr[3];
502         char            *filter = NULL;
503         Connection      *oconn;
504         SlapReply       rs;
505         int             do_retry = 1;
506         LDAPControl     **ctrls = NULL;
507
508         /* Tell getconn this is a privileged op */
509         do_not_cache = op->o_do_not_cache;
510         op->o_do_not_cache = 1;
511         lc = ldap_back_getconn( op, &rs );
512         oconn = op->o_conn;
513         op->o_conn = NULL;
514         if ( !lc || !ldap_back_dobind( lc, op, &rs ) ) {
515                 op->o_do_not_cache = do_not_cache;
516                 op->o_conn = oconn;
517                 return 1;
518         }
519         op->o_do_not_cache = do_not_cache;
520         op->o_conn = oconn;
521
522         if ( at ) {
523                 if ( oc && at != slap_schema.si_ad_objectClass ) {
524                         gattr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
525                         gattr[1] = at->ad_cname.bv_val;
526                         gattr[2] = NULL;
527
528                 } else {
529                         gattr[0] = at->ad_cname.bv_val;
530                         gattr[1] = NULL;
531                 }
532         }
533
534         if ( oc ) {
535                 char    *ptr;
536
537                 filter = ch_malloc( STRLENOF( "(objectclass=)" ) 
538                                 + oc->soc_cname.bv_len + 1 );
539                 ptr = lutil_strcopy( filter, "(objectclass=" );
540                 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
541                 *ptr++ = ')';
542                 *ptr++ = '\0';
543         }
544
545         ctrls = op->o_ctrls;
546         rc = ldap_back_proxy_authz_ctrl( lc, op, &rs, &ctrls );
547         if ( rc != LDAP_SUCCESS ) {
548                 goto cleanup;
549         }
550         
551 retry:
552         rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
553                                 at ? gattr : NULL, 0, ctrls, NULL, LDAP_NO_LIMIT,
554                                 LDAP_NO_LIMIT, &result );
555         if ( rc != LDAP_SUCCESS ) {
556                 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
557                         do_retry = 0;
558                         if ( ldap_back_retry( lc, op, &rs ) ) {
559                                 goto retry;
560                         }
561                 }
562                 goto cleanup;
563         }
564
565         e = ldap_first_entry( lc->lc_ld, result );
566         if ( e == NULL ) {
567                 goto cleanup;
568         }
569
570         *ent = ch_calloc( 1, sizeof( Entry ) );
571
572         rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
573
574         if ( rc != LDAP_SUCCESS ) {
575                 ch_free( *ent );
576                 *ent = NULL;
577         }
578
579 cleanup:
580         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
581
582         if ( result ) {
583                 ldap_msgfree( result );
584         }
585
586         if ( filter ) {
587                 ch_free( filter );
588         }
589
590         return rc;
591 }
592