1 /* search.c - ldap backend search function */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 /* This is an altered version */
9 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
11 * Permission is granted to anyone to use this software for any purpose
12 * on any computer system, and to alter it and redistribute it, subject
13 * to the following restrictions:
15 * 1. The author is not responsible for the consequences of use of this
16 * software, no matter how awful, even if they arise from flaws in it.
18 * 2. The origin of this software must not be misrepresented, either by
19 * explicit claim or by omission. Since few users ever read sources,
20 * credits should appear in the documentation.
22 * 3. Altered versions must be plainly marked as such, and must not be
23 * misrepresented as being the original software. Since few users
24 * ever read sources, credits should appear in the documentation.
26 * 4. This notice may not be removed or altered.
30 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
32 * This software is being modified by Pierangelo Masarati.
33 * The previously reported conditions apply to the modified code as well.
34 * Changes in the original code are highlighted where required.
35 * Credits for the original code go to the author, Howard Chu.
42 #include <ac/socket.h>
43 #include <ac/string.h>
47 #include "back-ldap.h"
48 #undef ldap_debug /* silence a warning in ldap-int.h */
49 #include "../../../libraries/libldap/ldap-int.h"
51 static int ldap_send_entry( Backend *be, Operation *op, Connection *conn,
52 LDAPMessage *e, AttributeName *attrs, int attrsonly );
66 struct berval *filterstr,
71 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
75 int count, rc = 0, msgid, sres = LDAP_SUCCESS;
76 char *match = NULL, *err = NULL;
77 char **mapped_attrs = NULL;
81 #endif /* ENABLE_REWRITE */
82 struct berval mfilter = { 0, NULL };
83 struct slap_limits_set *limit = NULL;
85 BerVarray v2refs = NULL;
87 lc = ldap_back_getconn(li, conn, op);
93 * FIXME: in case of values return filter, we might want
94 * to map attrs and maybe rewrite value
96 if ( !ldap_back_dobind( li, lc, conn, op ) ) {
100 /* if not root, get appropriate limits */
101 if ( be_isroot( be, &op->o_ndn ) ) {
104 ( void ) get_limits( be, &op->o_ndn, &limit );
107 /* if no time limit requested, rely on remote server limits */
108 /* if requested limit higher than hard limit, abort */
109 if ( !isroot && tlimit > limit->lms_t_hard ) {
110 /* no hard limit means use soft instead */
111 if ( limit->lms_t_hard == 0
112 && limit->lms_t_soft > -1
113 && tlimit > limit->lms_t_soft ) {
114 tlimit = limit->lms_t_soft;
116 /* positive hard limit means abort */
117 } else if ( limit->lms_t_hard > 0 ) {
118 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
119 NULL, NULL, NULL, NULL );
124 /* negative hard limit means no limit */
127 /* if no size limit requested, rely on remote server limits */
128 /* if requested limit higher than hard limit, abort */
129 if ( !isroot && slimit > limit->lms_s_hard ) {
130 /* no hard limit means use soft instead */
131 if ( limit->lms_s_hard == 0
132 && limit->lms_s_soft > -1
133 && slimit > limit->lms_s_soft ) {
134 slimit = limit->lms_s_soft;
136 /* positive hard limit means abort */
137 } else if ( limit->lms_s_hard > 0 ) {
138 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
139 NULL, NULL, NULL, NULL );
144 /* negative hard limit means no limit */
147 /* should we check return values? */
149 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
158 * Rewrite the search base, if required
160 #ifdef ENABLE_REWRITE
161 switch ( rewrite_session( li->rwinfo, "searchBase",
162 base->bv_val, conn, &mbase.bv_val ) ) {
163 case REWRITE_REGEXEC_OK:
164 if ( mbase.bv_val == NULL ) {
168 LDAP_LOG( BACK_LDAP, DETAIL1,
169 "[rw] searchBase: \"%s\" -> \"%s\"\n",
170 base->bv_val, mbase.bv_val, 0 );
171 #else /* !NEW_LOGGING */
172 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
173 base->bv_val, mbase.bv_val, "" );
174 #endif /* !NEW_LOGGING */
177 case REWRITE_REGEXEC_UNWILLING:
178 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
179 NULL, "Operation not allowed", NULL, NULL );
183 case REWRITE_REGEXEC_ERR:
184 send_ldap_result( conn, op, LDAP_OTHER,
185 NULL, "Rewrite error", NULL, NULL );
190 #else /* !ENABLE_REWRITE */
191 ldap_back_dn_massage( li, base, &mbase, 0, 1 );
192 #endif /* !ENABLE_REWRITE */
194 #ifdef ENABLE_REWRITE
195 rc = ldap_back_filter_map_rewrite_( li->rwinfo, conn,
196 &li->at_map, &li->oc_map, filter, &mfilter,
198 #else /* ! ENABLE_REWRITE */
199 rc = ldap_back_filter_map_rewrite_( &li->at_map, &li->oc_map,
200 filter, &mfilter, BACKLDAP_MAP );
201 #endif /* ! ENABLE_REWRITE */
208 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, BACKLDAP_MAP);
209 if ( mapped_attrs == NULL && attrs) {
210 for (count=0; attrs[count].an_name.bv_val; count++);
211 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
212 for (count=0; attrs[count].an_name.bv_val; count++) {
213 mapped_attrs[count] = attrs[count].an_name.bv_val;
215 mapped_attrs[count] = NULL;
218 rc = ldap_search_ext(lc->ld, mbase.bv_val, scope, mfilter.bv_val,
219 mapped_attrs, attrsonly, op->o_ctrls, NULL, tv.tv_sec ? &tv
220 : NULL, slimit, &msgid);
221 if ( rc != LDAP_SUCCESS ) {
223 rc = ldap_back_op_result(li, lc, conn, op, msgid, rc, 0);
227 /* We pull apart the ber result, stuff it into a slapd entry, and
228 * let send_search_entry stuff it back into ber format. Slow & ugly,
229 * but this is necessary for version matching, and for ACL processing.
234 rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
236 /* check for abandon */
238 ldap_abandon(lc->ld, msgid);
246 ldap_pvt_thread_yield();
248 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
249 e = ldap_first_entry(lc->ld,res);
250 if ( ldap_send_entry(be, op, conn, e, attrs, attrsonly)
256 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
257 char **references = NULL;
258 LDAPControl **ctrls = NULL;
262 rc = ldap_parse_reference( lc->ld, res,
263 &references, &ctrls, 1 );
265 if ( rc != LDAP_SUCCESS ) {
269 if ( references == NULL ) {
273 for ( cnt = 0; references[ cnt ]; cnt++ )
276 refs = ch_calloc( cnt + 1, sizeof( struct berval ) );
278 for ( cnt = 0; references[ cnt ]; cnt++ ) {
279 refs[ cnt ].bv_val = references[ cnt ];
280 refs[ cnt ].bv_len = strlen( references[ cnt ] );
283 /* ignore return value by now */
284 ( void )send_search_reference( be, conn, op,
285 NULL, refs, ctrls, &v2refs );
289 ldap_value_free( references );
294 ldap_controls_free( ctrls );
298 rc = ldap_parse_result(lc->ld, res, &sres, &match,
299 &err, NULL, NULL, 1);
300 if (rc != LDAP_SUCCESS ) sres = rc;
301 sres = ldap_back_map_result(sres);
310 #ifdef ENABLE_REWRITE
312 * Rewrite the matched portion of the search base, if required
314 if ( match != NULL ) {
315 switch ( rewrite_session( li->rwinfo, "matchedDn",
316 match, conn, &mmatch ) ) {
317 case REWRITE_REGEXEC_OK:
318 if ( mmatch == NULL ) {
319 mmatch = ( char * )match;
322 LDAP_LOG( BACK_LDAP, DETAIL1,
323 "[rw] matchedDn:" " \"%s\" -> \"%s\"\n", match, mmatch, 0 );
324 #else /* !NEW_LOGGING */
325 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
326 " \"%s\" -> \"%s\"\n%s",
328 #endif /* !NEW_LOGGING */
331 case REWRITE_REGEXEC_UNWILLING:
333 case REWRITE_REGEXEC_ERR:
334 /* FIXME: no error, but no matched ... */
341 sres = LDAP_REFERRAL;
344 send_search_result( conn, op, sres,
345 mmatch, err, v2refs, NULL, count );
347 #else /* !ENABLE_REWRITE */
349 sres = LDAP_REFERRAL;
351 send_search_result( conn, op, sres,
352 match, err, v2refs, NULL, count );
353 #endif /* !ENABLE_REWRITE */
357 #ifdef ENABLE_REWRITE
358 if ( mmatch != match ) {
361 #endif /* ENABLE_REWRITE */
367 if ( mapped_attrs ) {
368 ch_free( mapped_attrs );
370 if ( mfilter.bv_val != filterstr->bv_val ) {
371 ch_free( mfilter.bv_val );
373 if ( mbase.bv_val != base->bv_val ) {
374 free( mbase.bv_val );
386 AttributeName *attrs,
390 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
391 struct berval a, mapped;
393 BerElement ber = *e->lm_ber;
394 Attribute *attr, **attrp;
395 struct berval dummy = { 0, NULL };
396 struct berval *bv, bdn;
399 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
400 return LDAP_DECODING_ERROR;
402 #ifdef ENABLE_REWRITE
405 * Rewrite the dn of the result, if needed
407 switch ( rewrite_session( li->rwinfo, "searchResult",
408 bdn.bv_val, conn, &ent.e_name.bv_val ) ) {
409 case REWRITE_REGEXEC_OK:
410 if ( ent.e_name.bv_val == NULL ) {
414 LDAP_LOG( BACK_LDAP, DETAIL1,
415 "[rw] searchResult: \"%s\"" " -> \"%s\"\n",
416 bdn.bv_val, ent.e_dn, 0 );
417 #else /* !NEW_LOGGING */
418 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
419 " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
420 #endif /* !NEW_LOGGING */
421 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
425 case REWRITE_REGEXEC_ERR:
426 case REWRITE_REGEXEC_UNWILLING:
429 #else /* !ENABLE_REWRITE */
430 ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
431 #endif /* !ENABLE_REWRITE */
434 * Note: this may fail if the target host(s) schema differs
435 * from the one known to the meta, and a DN with unknown
436 * attributes is returned.
438 * FIXME: should we log anything, or delegate to dnNormalize2?
440 if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
441 return LDAP_INVALID_DN_SYNTAX;
447 attrp = &ent.e_attrs;
449 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
450 ldap_back_map(&li->at_map, &a, &mapped, BACKLDAP_REMAP);
451 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
453 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
459 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
460 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
463 LDAP_LOG( BACK_LDAP, DETAIL1,
464 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
465 #else /* !NEW_LOGGING */
466 Debug( LDAP_DEBUG_ANY,
467 "slap_bv2undef_ad(%s): "
468 "%s\n%s", mapped.bv_val, text, "" );
469 #endif /* !NEW_LOGGING */
475 /* no subschemaSubentry */
476 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
481 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
482 || attr->a_vals == NULL ) {
484 * Note: attr->a_vals can be null when using
485 * values result filter
487 attr->a_vals = &dummy;
489 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
490 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
493 for ( last = 0; attr->a_vals[last].bv_val; last++ );
495 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
496 ldap_back_map(&li->oc_map, bv, &mapped,
498 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
499 LBER_FREE(bv->bv_val);
503 *bv = attr->a_vals[last];
504 attr->a_vals[last].bv_val = NULL;
507 } else if ( mapped.bv_val != bv->bv_val ) {
509 * FIXME: after LBER_FREEing
510 * the value is replaced by
513 LBER_FREE(bv->bv_val);
514 ber_dupbv( bv, &mapped );
519 * It is necessary to try to rewrite attributes with
520 * dn syntax because they might be used in ACLs as
521 * members of groups; since ACLs are applied to the
522 * rewritten stuff, no dn-based subject clause could
523 * be used at the ldap backend side (see
524 * http://www.OpenLDAP.org/faq/data/cache/452.html)
525 * The problem can be overcome by moving the dn-based
526 * ACLs to the target directory server, and letting
527 * everything pass thru the ldap backend.
529 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
530 SLAPD_DN_SYNTAX ) == 0 ) {
533 for ( last = 0; attr->a_vals[last].bv_val; last++ );
535 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
536 struct berval newval;
538 #ifdef ENABLE_REWRITE
539 switch ( rewrite_session( li->rwinfo,
544 case REWRITE_REGEXEC_OK:
546 if ( newval.bv_val == NULL ) {
549 newval.bv_len = strlen( newval.bv_val );
551 LDAP_LOG( BACK_LDAP, DETAIL1,
552 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
553 attr->a_desc->ad_type->sat_cname.bv_val,
554 bv->bv_val, newval.bv_val );
555 #else /* !NEW_LOGGING */
556 Debug( LDAP_DEBUG_ARGS,
557 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
558 attr->a_desc->ad_type->sat_cname.bv_val,
559 bv->bv_val, newval.bv_val );
560 #endif /* !NEW_LOGGING */
565 case REWRITE_REGEXEC_UNWILLING:
566 LBER_FREE(bv->bv_val);
570 *bv = attr->a_vals[last];
571 attr->a_vals[last].bv_val = NULL;
575 case REWRITE_REGEXEC_ERR:
577 * FIXME: better give up,
579 * or leave it untouched?
583 #else /* !ENABLE_REWRITE */
584 ldap_back_dn_massage( li, bv, &newval, 0, 0 );
586 #endif /* !ENABLE_REWRITE */
593 attrp = &attr->a_next;
595 send_search_entry( be, conn, op, &ent, attrs, attrsonly, NULL );
596 while (ent.e_attrs) {
598 ent.e_attrs = attr->a_next;
599 if (attr->a_vals != &dummy)
600 ber_bvarray_free(attr->a_vals);
604 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )