]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
Cleanup result handling
[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-2004 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 #define LDAP_BUILD_ENTRY_NORMALIZE      0x02
44
45 static struct berval dummy = { 0, NULL };
46
47 int
48 ldap_back_search(
49     Operation   *op,
50     SlapReply *rs )
51 {
52         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
53         struct ldapconn *lc;
54         struct timeval  tv;
55         LDAPMessage             *res, *e;
56         int     rc = 0, msgid; 
57         struct berval match = { 0, NULL };
58         char **mapped_attrs = NULL;
59         struct berval mbase;
60         struct berval mfilter = { 0, NULL };
61         int dontfreetext = 0;
62         dncookie dc;
63 #ifdef LDAP_BACK_PROXY_AUTHZ
64         LDAPControl **ctrls = NULL;
65 #endif /* LDAP_BACK_PROXY_AUTHZ */
66
67         lc = ldap_back_getconn(op, rs);
68         if ( !lc ) {
69                 return( -1 );
70         }
71
72         /*
73          * FIXME: in case of values return filter, we might want
74          * to map attrs and maybe rewrite value
75          */
76         if ( !ldap_back_dobind( lc, op, rs ) ) {
77                 return( -1 );
78         }
79
80         /* should we check return values? */
81         if (op->ors_deref != -1)
82                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->ors_deref);
83         if (op->ors_tlimit != -1) {
84                 tv.tv_sec = op->ors_tlimit;
85                 tv.tv_usec = 0;
86         } else {
87                 tv.tv_sec = 0;
88         }
89
90         /*
91          * Rewrite the search base, if required
92          */
93         dc.rwmap = &li->rwmap;
94 #ifdef ENABLE_REWRITE
95         dc.conn = op->o_conn;
96         dc.rs = rs;
97         dc.ctx = "searchBase";
98 #else
99         dc.tofrom = 1;
100         dc.normalized = 0;
101 #endif
102         if ( ldap_back_dn_massage( &dc, &op->o_req_ndn, &mbase ) ) {
103                 send_ldap_result( op, rs );
104                 return -1;
105         }
106
107         rc = ldap_back_filter_map_rewrite( &dc, op->ors_filter,
108                         &mfilter, BACKLDAP_MAP );
109
110         switch ( rc ) {
111         case LDAP_SUCCESS:
112                 break;
113
114         case LDAP_COMPARE_FALSE:
115                 rs->sr_err = LDAP_SUCCESS;
116                 rs->sr_text = NULL;
117                 rc = 0;
118                 goto finish;
119
120         default:
121                 rs->sr_err = LDAP_OTHER;
122                 rs->sr_text = "Rewrite error";
123                 dontfreetext = 1;
124                 rc = -1;
125                 goto finish;
126         }
127
128         rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
129                         op->ors_attrs,
130                         BACKLDAP_MAP, &mapped_attrs );
131         if ( rs->sr_err ) {
132                 rc = -1;
133                 goto finish;
134         }
135
136 #ifdef LDAP_BACK_PROXY_AUTHZ
137         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
138         if ( rc != LDAP_SUCCESS ) {
139                 dontfreetext = 1;
140                 goto finish;
141         }
142 #endif /* LDAP_BACK_PROXY_AUTHZ */
143         
144         rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
145                         op->ors_scope, mfilter.bv_val,
146                         mapped_attrs, op->ors_attrsonly,
147 #ifdef LDAP_BACK_PROXY_AUTHZ
148                         ctrls,
149 #else /* ! LDAP_BACK_PROXY_AUTHZ */
150                         op->o_ctrls,
151 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
152                         NULL,
153                         tv.tv_sec ? &tv : NULL, op->ors_slimit,
154                         &msgid );
155
156         if ( rs->sr_err != LDAP_SUCCESS ) {
157 fail:;
158                 rc = ldap_back_op_result(lc, op, rs, msgid, 0);
159                 goto finish;
160         }
161
162         /* We pull apart the ber result, stuff it into a slapd entry, and
163          * let send_search_entry stuff it back into ber format. Slow & ugly,
164          * but this is necessary for version matching, and for ACL processing.
165          */
166
167         for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
168         {
169                 /* check for abandon */
170                 if (op->o_abandon) {
171                         ldap_abandon(lc->ld, msgid);
172                         rc = 0;
173                         goto finish;
174                 }
175
176                 if (rc == 0) {
177                         tv.tv_sec = 0;
178                         tv.tv_usec = 100000;
179                         ldap_pvt_thread_yield();
180
181                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
182                         Entry ent = {0};
183                         struct berval bdn;
184                         int abort = 0;
185                         e = ldap_first_entry(lc->ld,res);
186                         if ( ( rc = ldap_build_entry(op, e, &ent, &bdn,
187                                                 LDAP_BUILD_ENTRY_PRIVATE)) == LDAP_SUCCESS ) {
188                                 rs->sr_entry = &ent;
189                                 rs->sr_attrs = op->ors_attrs;
190                                 abort = send_search_entry( op, rs );
191                                 while (ent.e_attrs) {
192                                         Attribute *a;
193                                         BerVarray v;
194
195                                         a = ent.e_attrs;
196                                         ent.e_attrs = a->a_next;
197
198                                         v = a->a_vals;
199                                         if (a->a_vals != &dummy)
200                                                 ber_bvarray_free(a->a_vals);
201                                         if (a->a_nvals != v)
202                                                 ber_bvarray_free(a->a_nvals);
203                                         ch_free(a);
204                                 }
205                                 
206                                 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
207                                         free( ent.e_dn );
208                                 if ( ent.e_ndn )
209                                         free( ent.e_ndn );
210                         }
211                         ldap_msgfree(res);
212                         if ( abort ) {
213                                 ldap_abandon(lc->ld, msgid);
214                                 goto finish;
215                         }
216
217                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
218                         char            **references = NULL;
219                         int             cnt;
220
221                         rc = ldap_parse_reference( lc->ld, res,
222                                         &references, &rs->sr_ctrls, 1 );
223
224                         if ( rc != LDAP_SUCCESS ) {
225                                 continue;
226                         }
227
228                         if ( references == NULL ) {
229                                 continue;
230                         }
231
232                         for ( cnt = 0; references[ cnt ]; cnt++ )
233                                 /* NO OP */ ;
234                                 
235                         rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
236
237                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
238                                 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
239                                 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
240                         }
241
242                         /* ignore return value by now */
243                         ( void )send_search_reference( op, rs );
244
245                         /* cleanup */
246                         if ( references ) {
247                                 ldap_value_free( references );
248                                 ch_free( rs->sr_ref );
249                                 rs->sr_ref = NULL;
250                         }
251
252                         if ( rs->sr_ctrls ) {
253                                 ldap_controls_free( rs->sr_ctrls );
254                                 rs->sr_ctrls = NULL;
255                         }
256
257                 } else {
258                         rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
259                                         &match.bv_val, (char **)&rs->sr_text,
260                                         NULL, NULL, 1);
261                         if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
262                         rs->sr_err = ldap_back_map_result(rs);
263                         rc = 0;
264                         break;
265                 }
266         }
267
268         if (rc == -1)
269                 goto fail;
270
271         /*
272          * Rewrite the matched portion of the search base, if required
273          */
274         if ( match.bv_val && *match.bv_val ) {
275                 struct berval mdn;
276
277 #ifdef ENABLE_REWRITE
278                 dc.ctx = "matchedDN";
279 #else
280                 dc.tofrom = 0;
281                 dc.normalized = 0;
282 #endif
283                 match.bv_len = strlen( match.bv_val );
284                 ldap_back_dn_massage(&dc, &match, &mdn);
285                 rs->sr_matched = mdn.bv_val;
286         }
287         if ( rs->sr_v2ref ) {
288                 rs->sr_err = LDAP_REFERRAL;
289         }
290
291 finish:;
292         send_ldap_result( op, rs );
293
294 #ifdef LDAP_BACK_PROXY_AUTHZ
295         if ( ctrls && ctrls != op->o_ctrls ) {
296                 free( ctrls[ 0 ] );
297                 free( ctrls );
298         }
299 #endif /* LDAP_BACK_PROXY_AUTHZ */
300
301         if ( match.bv_val ) {
302                 if ( rs->sr_matched != match.bv_val ) {
303                         free( (char *)rs->sr_matched );
304                 }
305                 rs->sr_matched = NULL;
306                 LDAP_FREE( match.bv_val );
307         }
308         if ( rs->sr_text ) {
309                 if ( !dontfreetext ) {
310                         LDAP_FREE( (char *)rs->sr_text );
311                 }
312                 rs->sr_text = NULL;
313         }
314         if ( mapped_attrs ) {
315                 ch_free( mapped_attrs );
316         }
317         if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
318                 ch_free( mfilter.bv_val );
319         }
320         if ( mbase.bv_val != op->o_req_ndn.bv_val ) {
321                 free( mbase.bv_val );
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 {
336         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
337         struct berval a, mapped;
338         BerElement ber = *e->lm_ber;
339         Attribute *attr, **attrp;
340         struct berval *bv;
341         const char *text;
342         int last;
343         int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
344         int normalize = flags & LDAP_BUILD_ENTRY_NORMALIZE;
345         dncookie dc;
346
347         /* safe assumptions ... */
348         assert( ent );
349         ent->e_bv.bv_val = NULL;
350
351         if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
352                 return LDAP_DECODING_ERROR;
353         }
354
355         /*
356          * Rewrite the dn of the result, if needed
357          */
358         dc.rwmap = &li->rwmap;
359 #ifdef ENABLE_REWRITE
360         dc.conn = op->o_conn;
361         dc.rs = NULL;
362         dc.ctx = "searchResult";
363 #else
364         dc.tofrom = 0;
365         dc.normalized = 0;
366 #endif
367         if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
368                 return LDAP_OTHER;
369         }
370
371         /*
372          * Note: this may fail if the target host(s) schema differs
373          * from the one known to the meta, and a DN with unknown
374          * attributes is returned.
375          * 
376          * FIXME: should we log anything, or delegate to dnNormalize?
377          */
378         if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
379                 op->o_tmpmemctx ) != LDAP_SUCCESS )
380         {
381                 return LDAP_INVALID_DN_SYNTAX;
382         }
383         
384         attrp = &ent->e_attrs;
385
386 #ifdef ENABLE_REWRITE
387         dc.ctx = "searchAttrDN";
388 #endif
389         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
390                 ldap_back_map(&li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP);
391                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
392                         continue;
393                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
394                 if (attr == NULL)
395                         continue;
396                 attr->a_flags = 0;
397                 attr->a_next = 0;
398                 attr->a_desc = NULL;
399                 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
400                         if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text) 
401                                         != LDAP_SUCCESS) {
402 #ifdef NEW_LOGGING
403                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
404                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
405 #else /* !NEW_LOGGING */
406                                 Debug( LDAP_DEBUG_ANY, 
407                                                 "slap_bv2undef_ad(%s):  "
408                                                 "%s\n%s", mapped.bv_val, text, "" );
409 #endif /* !NEW_LOGGING */
410                                 ch_free(attr);
411                                 continue;
412                         }
413                 }
414
415                 /* no subschemaSubentry */
416                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
417
418                         /* 
419                          * We eat target's subschemaSubentry because
420                          * a search for this value is likely not
421                          * to resolve to the appropriate backend;
422                          * later, the local subschemaSubentry is
423                          * added.
424                          */
425                         ( void )ber_scanf( &ber, "x" /* [W] */ );
426
427                         ch_free(attr);
428                         continue;
429                 }
430                 
431                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
432                                 || attr->a_vals == NULL ) {
433                         /*
434                          * Note: attr->a_vals can be null when using
435                          * values result filter
436                          */
437                         if (private) {
438                                 attr->a_vals = &dummy;
439                         } else {
440                                 attr->a_vals = ch_malloc(sizeof(struct berval));
441                                 attr->a_vals->bv_val = NULL;
442                                 attr->a_vals->bv_len = 0;
443                         }
444                         last = 0;
445                 } else {
446                         for ( last = 0; attr->a_vals[last].bv_val; last++ );
447                 }
448                 if ( last == 0 ) {
449                         /* empty */
450                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
451                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
452                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
453                                 ldap_back_map(&li->rwmap.rwm_oc, bv, &mapped,
454                                                 BACKLDAP_REMAP);
455                                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
456                                         LBER_FREE(bv->bv_val);
457                                         bv->bv_val = NULL;
458                                         if (--last < 0)
459                                                 break;
460                                         *bv = attr->a_vals[last];
461                                         attr->a_vals[last].bv_val = NULL;
462                                         bv--;
463
464                                 } else if ( mapped.bv_val != bv->bv_val ) {
465                                         /*
466                                          * FIXME: after LBER_FREEing
467                                          * the value is replaced by
468                                          * ch_alloc'ed memory
469                                          */
470                                         LBER_FREE(bv->bv_val);
471                                         ber_dupbv( bv, &mapped );
472                                 }
473                         }
474
475                 /*
476                  * It is necessary to try to rewrite attributes with
477                  * dn syntax because they might be used in ACLs as
478                  * members of groups; since ACLs are applied to the
479                  * rewritten stuff, no dn-based subject clause could
480                  * be used at the ldap backend side (see
481                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
482                  * The problem can be overcome by moving the dn-based
483                  * ACLs to the target directory server, and letting
484                  * everything pass thru the ldap backend.
485                  */
486                 } else if ( attr->a_desc->ad_type->sat_syntax ==
487                                 slap_schema.si_syn_distinguishedName ) {
488                         ldap_dnattr_result_rewrite( &dc, attr->a_vals );
489                 }
490
491                 if ( normalize && last && attr->a_desc->ad_type->sat_equality &&
492                         attr->a_desc->ad_type->sat_equality->smr_normalize ) {
493                         int i;
494
495                         attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
496                         for (i=0; i<last; i++) {
497                                 attr->a_desc->ad_type->sat_equality->smr_normalize(
498                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
499                                         attr->a_desc->ad_type->sat_syntax,
500                                         attr->a_desc->ad_type->sat_equality,
501                                         &attr->a_vals[i], &attr->a_nvals[i],
502                                         NULL /* op->o_tmpmemctx */ );
503                         }
504                         attr->a_nvals[i].bv_val = NULL;
505                         attr->a_nvals[i].bv_len = 0;
506                 } else {
507                         attr->a_nvals = attr->a_vals;
508                 }
509                 *attrp = attr;
510                 attrp = &attr->a_next;
511         }
512
513         /* make sure it's free'able */
514         if (!private && ent->e_name.bv_val == bdn->bv_val)
515                 ber_dupbv( &ent->e_name, bdn );
516         return LDAP_SUCCESS;
517 }
518
519 /* return 0 IFF we can retrieve the entry with ndn
520  */
521 int
522 ldap_back_entry_get(
523         Operation *op,
524         struct berval   *ndn,
525         ObjectClass *oc,
526         AttributeDescription *at,
527         int rw,
528         Entry **ent
529 )
530 {
531         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;    
532         struct ldapconn *lc;
533         int rc = 1, is_oc;
534         struct berval mapped = { 0, NULL }, bdn, mdn;
535         LDAPMessage     *result = NULL, *e = NULL;
536         char *gattr[3];
537         char *filter = NULL;
538         Connection *oconn;
539         SlapReply rs;
540         dncookie dc;
541
542         /* Tell getconn this is a privileged op */
543         is_oc = op->o_do_not_cache;
544         op->o_do_not_cache = 1;
545         lc = ldap_back_getconn(op, &rs);
546         oconn = op->o_conn;
547         op->o_conn = NULL;
548         if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
549                 op->o_do_not_cache = is_oc;
550                 op->o_conn = oconn;
551                 return 1;
552         }
553         op->o_do_not_cache = is_oc;
554         op->o_conn = oconn;
555
556         /*
557          * Rewrite the search base, if required
558          */
559         dc.rwmap = &li->rwmap;
560 #ifdef ENABLE_REWRITE
561         dc.conn = op->o_conn;
562         dc.rs = &rs;
563         dc.ctx = "searchBase";
564 #else
565         dc.tofrom = 1;
566         dc.normalized = 1;
567 #endif
568         if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
569                 return 1;
570         }
571
572         if ( at ) {
573                 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
574                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
575                         rc = 1;
576                         goto cleanup;
577                 }
578         }
579
580         is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
581         if (oc && !is_oc) {
582                 gattr[0] = "objectclass";
583                 gattr[1] = mapped.bv_val;
584                 gattr[2] = NULL;
585         } else {
586                 gattr[0] = mapped.bv_val;
587                 gattr[1] = NULL;
588         }
589         if (oc) {
590                 char *ptr;
591                 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
592                                                 BACKLDAP_MAP);
593                 filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
594                 ptr = lutil_strcopy(filter, "(objectclass=");
595                 ptr = lutil_strcopy(ptr, mapped.bv_val);
596                 *ptr++ = ')';
597                 *ptr++ = '\0';
598         }
599
600         if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
601                                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
602                                 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
603         {
604                 goto cleanup;
605         }
606
607         if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
608                 goto cleanup;
609         }
610
611         *ent = ch_calloc(1,sizeof(Entry));
612
613         rc = ldap_build_entry(op, e, *ent, &bdn, LDAP_BUILD_ENTRY_NORMALIZE);
614
615         if (rc != LDAP_SUCCESS) {
616                 ch_free(*ent);
617                 *ent = NULL;
618         }
619
620 cleanup:
621         if (result) {
622                 ldap_msgfree(result);
623         }
624
625         if ( filter ) {
626                 ch_free( filter );
627         }
628
629         if ( mdn.bv_val != ndn->bv_val ) {
630                 ch_free( mdn.bv_val );
631         }
632
633         return(rc);
634 }
635