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;
106 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
110 case REWRITE_REGEXEC_UNWILLING:
111 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
112 NULL, "Unwilling to perform", NULL, NULL );
114 case REWRITE_REGEXEC_ERR:
120 * Rewrite the search filter, if required
122 switch ( rewrite_session( li->rwinfo, "searchFilter",
123 filterstr, conn, &mfilter ) ) {
124 case REWRITE_REGEXEC_OK:
125 if ( mfilter == NULL || mfilter[0] == '\0') {
126 if ( mfilter != NULL ) {
129 mfilter = ( char * )filterstr;
131 Debug( LDAP_DEBUG_ARGS,
132 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
133 filterstr, mfilter, "" );
136 case REWRITE_REGEXEC_UNWILLING:
137 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
138 NULL, "Unwilling to perform", NULL, NULL );
139 case REWRITE_REGEXEC_ERR:
143 #else /* !ENABLE_REWRITE */
144 mbase = ldap_back_dn_massage( li, ch_strdup( base ), 0 );
145 #endif /* !ENABLE_REWRITE */
147 mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
148 #ifdef ENABLE_REWRITE
150 #else /* !ENABLE_REWRITE */
152 #endif /* !ENABLE_REWRITE */
154 if ( mapped_filter == NULL ) {
155 #ifdef ENABLE_REWRITE
156 mapped_filter = (char *)mfilter;
157 #else /* !ENABLE_REWRITE */
158 mapped_filter = (char *)filterstr;
159 #endif /* !ENABLE_REWRITE */
162 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
163 if ( mapped_attrs == NULL ) {
164 mapped_attrs = attrs;
167 if ((msgid = ldap_search(lc->ld, mbase, scope, mapped_filter, mapped_attrs,
171 rc = ldap_back_op_result(lc, op);
175 /* We pull apart the ber result, stuff it into a slapd entry, and
176 * let send_search_entry stuff it back into ber format. Slow & ugly,
177 * but this is necessary for version matching, and for ACL processing.
182 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
186 /* check for abandon */
187 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
189 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
192 ldap_abandon(lc->ld, msgid);
199 ldap_pvt_thread_yield();
200 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
201 e = ldap_first_entry(lc->ld,res);
202 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
206 sres = ldap_result2error(lc->ld, res, 1);
207 sres = ldap_back_map_result(sres);
208 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
209 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
218 #ifdef ENABLE_REWRITE
220 * Rewrite the matched portion of the search base, if required
222 if ( match != NULL ) {
223 switch ( rewrite_session( li->rwinfo, "matchedDn",
224 match, conn, &mmatch ) ) {
225 case REWRITE_REGEXEC_OK:
226 if ( mmatch == NULL ) {
227 mmatch = ( char * )match;
229 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
230 " \"%s\" -> \"%s\"\n%s",
234 case REWRITE_REGEXEC_UNWILLING:
235 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
236 NULL, "Unwilling to perform",
239 case REWRITE_REGEXEC_ERR:
245 send_search_result( conn, op, sres,
246 mmatch, err, NULL, NULL, count );
248 #else /* !ENABLE_REWRITE */
249 send_search_result( conn, op, sres,
250 match, err, NULL, NULL, count );
251 #endif /* !ENABLE_REWRITE */
255 #ifdef ENABLE_REWRITE
256 if ( mmatch != match ) {
259 #endif /* ENABLE_REWRITE */
265 if ( mapped_attrs != attrs ) {
266 charray_free( mapped_attrs );
268 #ifdef ENABLE_REWRITE
269 if ( mapped_filter != mfilter ) {
270 free( mapped_filter );
272 if ( mfilter != filterstr ) {
275 #else /* !ENABLE_REWRITE */
276 if ( mapped_filter != filterstr ) {
277 free( mapped_filter );
279 #endif /* !ENABLE_REWRITE */
281 #ifdef ENABLE_REWRITE
282 if ( mbase != base ) {
283 #endif /* ENABLE_REWRITE */
285 #ifdef ENABLE_REWRITE
287 #endif /* ENABLE_REWRITE */
302 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
305 BerElement *ber = NULL;
306 Attribute *attr, **attrp;
307 struct berval *dummy = NULL;
311 #ifdef ENABLE_REWRITE
314 dn = ldap_get_dn(lc->ld, e);
320 * Rewrite the dn of the result, if needed
322 switch ( rewrite_session( li->rwinfo, "searchResult",
323 dn, lc->conn, &ent.e_dn ) ) {
324 case REWRITE_REGEXEC_OK:
325 if ( ent.e_dn == NULL ) {
328 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
329 " -> \"%s\"\n%s", dn, ent.e_dn, "" );
335 case REWRITE_REGEXEC_ERR:
336 case REWRITE_REGEXEC_UNWILLING:
340 #else /* !ENABLE_REWRITE */
341 ent.e_dn = ldap_back_dn_restore( li, ldap_get_dn(lc->ld, e), 0 );
342 #endif /* !ENABLE_REWRITE */
344 ent.e_ndn = ch_strdup( ent.e_dn );
345 (void) dn_normalize( ent.e_ndn );
349 attrp = &ent.e_attrs;
351 for ( a = ldap_first_attribute(lc->ld, e, &ber);
353 a = ldap_next_attribute(lc->ld, e, ber))
355 mapped = ldap_back_map(&li->at_map, a, 1);
358 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
363 if (slap_str2ad(mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
367 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
369 attr->a_vals = &dummy;
370 } else if ( strcasecmp( mapped, "objectclass" ) == 0 ) {
372 for ( last = 0; attr->a_vals[last]; last++ ) ;
373 for ( i = 0; ( bv = attr->a_vals[i] ); i++ ) {
374 mapped = ldap_back_map(&li->oc_map, bv->bv_val, 1);
375 if (mapped == NULL) {
376 ber_bvfree(attr->a_vals[i]);
377 attr->a_vals[i] = NULL;
380 attr->a_vals[i] = attr->a_vals[last];
381 attr->a_vals[last] = NULL;
383 } else if ( mapped != bv->bv_val ) {
385 bv->bv_val = ch_strdup( mapped );
386 bv->bv_len = strlen( mapped );
390 #ifdef ENABLE_REWRITE
392 * It is necessary to try to rewrite attributes with
393 * dn syntax because they might be used in ACLs as
394 * members of groups; since ACLs are applied to the
395 * rewritten stuff, no dn-based subecj clause could
396 * be used at the ldap backend side (see
397 * http://www.OpenLDAP.org/faq/data/cache/452.html)
398 * The problem can be overcome by moving the dn-based
399 * ACLs to the target directory server, and letting
400 * everything pass thru the ldap backend.
402 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
403 SLAPD_DN_SYNTAX ) == 0 ) {
405 for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
408 switch ( rewrite_session( li->rwinfo,
411 lc->conn, &newval )) {
412 case REWRITE_REGEXEC_OK:
414 if ( newval == NULL ) {
417 Debug( LDAP_DEBUG_ARGS,
418 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
419 attr->a_desc->ad_type->sat_cname,
420 bv->bv_val, newval );
424 bv->bv_len = strlen( newval );
428 case REWRITE_REGEXEC_UNWILLING:
430 case REWRITE_REGEXEC_ERR:
432 * FIXME: better give up,
434 * or leave it untouched?
439 #endif /* ENABLE_REWRITE */
443 attrp = &attr->a_next;
445 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
446 while (ent.e_attrs) {
448 ent.e_attrs = attr->a_next;
449 ad_free(attr->a_desc, 1);
450 if (attr->a_vals != &dummy)
451 ber_bvecfree(attr->a_vals);