1 /* search.c - ldap backend search function */
4 * Copyright 1998-1999 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"
49 static void ldap_send_entry( Backend *be, Operation *op, struct ldapconn *lc,
50 LDAPMessage *e, char **attrs, int attrsonly );
64 const char *filterstr,
69 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
73 int count, rc = 0, msgid, sres = LDAP_SUCCESS;
74 char *match = NULL, *err = NULL;
75 char *mbase = NULL, *mapped_filter = NULL, **mapped_attrs = NULL;
77 char *mfilter = NULL, *mmatch = NULL;
78 #endif /* ENABLE_REWRITE */
80 lc = ldap_back_getconn(li, conn, op);
86 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
88 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&time);
90 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&size);
92 if ( !ldap_back_dobind( lc, op ) ) {
97 * Rewrite the search base, if required
100 switch ( rewrite_session( li->rwinfo, "searchBase",
101 base, conn, &mbase ) ) {
102 case REWRITE_REGEXEC_OK:
103 if ( mbase == NULL ) {
104 mbase = ( char * )base;
107 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
108 "[rw] searchBase: \"%s\" -> \"%s\"\n%",
110 #else /* !NEW_LOGGING */
111 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
113 #endif /* !NEW_LOGGING */
116 case REWRITE_REGEXEC_UNWILLING:
117 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
118 NULL, "Unwilling to perform", NULL, NULL );
122 case REWRITE_REGEXEC_ERR:
123 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
124 NULL, "Operations error", NULL, NULL );
130 * Rewrite the search filter, if required
132 switch ( rewrite_session( li->rwinfo, "searchFilter",
133 filterstr, conn, &mfilter ) ) {
134 case REWRITE_REGEXEC_OK:
135 if ( mfilter == NULL || mfilter[0] == '\0') {
136 if ( mfilter != NULL ) {
139 mfilter = ( char * )filterstr;
142 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
143 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
144 filterstr, mfilter ));
145 #else /* !NEW_LOGGING */
146 Debug( LDAP_DEBUG_ARGS,
147 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
148 filterstr, mfilter, "" );
149 #endif /* !NEW_LOGGING */
152 case REWRITE_REGEXEC_UNWILLING:
153 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
154 NULL, "Unwilling to perform", NULL, NULL );
155 case REWRITE_REGEXEC_ERR:
159 #else /* !ENABLE_REWRITE */
160 mbase = ldap_back_dn_massage( li, ch_strdup( base ), 0 );
161 #endif /* !ENABLE_REWRITE */
163 mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
164 #ifdef ENABLE_REWRITE
166 #else /* !ENABLE_REWRITE */
168 #endif /* !ENABLE_REWRITE */
170 if ( mapped_filter == NULL ) {
171 #ifdef ENABLE_REWRITE
172 mapped_filter = (char *)mfilter;
173 #else /* !ENABLE_REWRITE */
174 mapped_filter = (char *)filterstr;
175 #endif /* !ENABLE_REWRITE */
178 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
179 if ( mapped_attrs == NULL ) {
180 mapped_attrs = attrs;
183 if ((msgid = ldap_search(lc->ld, mbase, scope, mapped_filter, mapped_attrs,
187 rc = ldap_back_op_result(lc, op);
191 /* We pull apart the ber result, stuff it into a slapd entry, and
192 * let send_search_entry stuff it back into ber format. Slow & ugly,
193 * but this is necessary for version matching, and for ACL processing.
198 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
202 /* check for abandon */
203 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
205 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
208 ldap_abandon(lc->ld, msgid);
215 ldap_pvt_thread_yield();
216 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
217 e = ldap_first_entry(lc->ld,res);
218 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
222 sres = ldap_result2error(lc->ld, res, 1);
223 sres = ldap_back_map_result(sres);
224 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
225 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
234 #ifdef ENABLE_REWRITE
236 * Rewrite the matched portion of the search base, if required
238 if ( match != NULL ) {
239 switch ( rewrite_session( li->rwinfo, "matchedDn",
240 match, conn, &mmatch ) ) {
241 case REWRITE_REGEXEC_OK:
242 if ( mmatch == NULL ) {
243 mmatch = ( char * )match;
246 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
248 " \"%s\" -> \"%s\"\n",
250 #else /* !NEW_LOGGING */
251 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
252 " \"%s\" -> \"%s\"\n%s",
254 #endif /* !NEW_LOGGING */
257 case REWRITE_REGEXEC_UNWILLING:
258 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
259 NULL, "Unwilling to perform",
262 case REWRITE_REGEXEC_ERR:
268 send_search_result( conn, op, sres,
269 mmatch, err, NULL, NULL, count );
271 #else /* !ENABLE_REWRITE */
272 send_search_result( conn, op, sres,
273 match, err, NULL, NULL, count );
274 #endif /* !ENABLE_REWRITE */
278 #ifdef ENABLE_REWRITE
279 if ( mmatch != match ) {
282 #endif /* ENABLE_REWRITE */
288 if ( mapped_attrs != attrs ) {
289 charray_free( mapped_attrs );
291 #ifdef ENABLE_REWRITE
292 if ( mapped_filter != mfilter ) {
293 free( mapped_filter );
295 if ( mfilter != filterstr ) {
298 #else /* !ENABLE_REWRITE */
299 if ( mapped_filter != filterstr ) {
300 free( mapped_filter );
302 #endif /* !ENABLE_REWRITE */
304 #ifdef ENABLE_REWRITE
305 if ( mbase != base ) {
306 #endif /* ENABLE_REWRITE */
308 #ifdef ENABLE_REWRITE
310 #endif /* ENABLE_REWRITE */
325 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
328 BerElement *ber = NULL;
329 Attribute *attr, **attrp;
330 struct berval *dummy = NULL;
334 #ifdef ENABLE_REWRITE
337 dn = ldap_get_dn(lc->ld, e);
343 * Rewrite the dn of the result, if needed
345 switch ( rewrite_session( li->rwinfo, "searchResult",
346 dn, lc->conn, &ent.e_dn ) ) {
347 case REWRITE_REGEXEC_OK:
348 if ( ent.e_dn == NULL ) {
352 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
353 "[rw] searchResult: \"%s\""
354 " -> \"%s\"\n", dn, ent.e_dn ));
355 #else /* !NEW_LOGGING */
356 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
357 " -> \"%s\"\n%s", dn, ent.e_dn, "" );
358 #endif /* !NEW_LOGGING */
364 case REWRITE_REGEXEC_ERR:
365 case REWRITE_REGEXEC_UNWILLING:
369 #else /* !ENABLE_REWRITE */
370 ent.e_dn = ldap_back_dn_restore( li, ldap_get_dn(lc->ld, e), 0 );
371 #endif /* !ENABLE_REWRITE */
373 ent.e_ndn = ch_strdup( ent.e_dn );
374 (void) dn_normalize( ent.e_ndn );
378 attrp = &ent.e_attrs;
380 for ( a = ldap_first_attribute(lc->ld, e, &ber);
382 a = ldap_next_attribute(lc->ld, e, ber))
384 mapped = ldap_back_map(&li->at_map, a, 1);
387 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
392 if (slap_str2ad(mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
396 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
398 attr->a_vals = &dummy;
399 } else if ( strcasecmp( mapped, "objectclass" ) == 0 ) {
401 for ( last = 0; attr->a_vals[last]; last++ ) ;
402 for ( i = 0; ( bv = attr->a_vals[i] ); i++ ) {
403 mapped = ldap_back_map(&li->oc_map, bv->bv_val, 1);
404 if (mapped == NULL) {
405 ber_bvfree(attr->a_vals[i]);
406 attr->a_vals[i] = NULL;
409 attr->a_vals[i] = attr->a_vals[last];
410 attr->a_vals[last] = NULL;
412 } else if ( mapped != bv->bv_val ) {
414 bv->bv_val = ch_strdup( mapped );
415 bv->bv_len = strlen( mapped );
419 #ifdef ENABLE_REWRITE
421 * It is necessary to try to rewrite attributes with
422 * dn syntax because they might be used in ACLs as
423 * members of groups; since ACLs are applied to the
424 * rewritten stuff, no dn-based subecj clause could
425 * be used at the ldap backend side (see
426 * http://www.OpenLDAP.org/faq/data/cache/452.html)
427 * The problem can be overcome by moving the dn-based
428 * ACLs to the target directory server, and letting
429 * everything pass thru the ldap backend.
431 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
432 SLAPD_DN_SYNTAX ) == 0 ) {
434 for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
437 switch ( rewrite_session( li->rwinfo,
440 lc->conn, &newval )) {
441 case REWRITE_REGEXEC_OK:
443 if ( newval == NULL ) {
447 LDAP_LOG(( "backend",
449 "[rw] searchResult on"
451 " \"%s\" -> \"%s\"\n",
452 attr->a_desc->ad_type->sat_cname,
453 bv->bv_val, newval ));
454 #else /* !NEW_LOGGING */
455 Debug( LDAP_DEBUG_ARGS,
456 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
457 attr->a_desc->ad_type->sat_cname,
458 bv->bv_val, newval );
459 #endif /* !NEW_LOGGING */
463 bv->bv_len = strlen( newval );
467 case REWRITE_REGEXEC_UNWILLING:
469 case REWRITE_REGEXEC_ERR:
471 * FIXME: better give up,
473 * or leave it untouched?
478 #endif /* ENABLE_REWRITE */
482 attrp = &attr->a_next;
484 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
485 while (ent.e_attrs) {
487 ent.e_attrs = attr->a_next;
488 ad_free(attr->a_desc, 1);
489 if (attr->a_vals != &dummy)
490 ber_bvecfree(attr->a_vals);