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