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, msgid, sres = LDAP_SUCCESS;
74 char *match = NULL, *err = NULL;
75 char *mbase, *mapped_filter, **mapped_attrs;
77 lc = ldap_back_getconn(li, conn, op);
83 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
85 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&time);
87 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&size);
89 if ( !ldap_back_dobind( lc, op ) ) {
93 mbase = ldap_back_dn_massage( li, ch_strdup( base ), 0 );
94 if ( mbase == NULL ) {
98 mapped_filter = ldap_back_map_filter(li, (char *)filterstr, 0);
99 if ( mapped_filter == NULL ) {
100 mapped_filter = (char *)filterstr;
103 mapped_attrs = ldap_back_map_attrs(li, attrs, 0);
104 if ( mapped_attrs == NULL ) {
105 mapped_attrs = attrs;
108 if ((msgid = ldap_search(lc->ld, mbase, scope, mapped_filter, mapped_attrs,
116 if (mapped_attrs != attrs)
117 charray_free(mapped_attrs);
118 if (mapped_filter != filterstr)
121 return( ldap_back_op_result(lc, op) );
124 /* We pull apart the ber result, stuff it into a slapd entry, and
125 * let send_search_entry stuff it back into ber format. Slow & ugly,
126 * but this is necessary for version matching, and for ACL processing.
131 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
135 /* check for abandon */
136 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
138 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
141 ldap_abandon(lc->ld, msgid);
147 ldap_pvt_thread_yield();
148 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
149 e = ldap_first_entry(lc->ld,res);
150 ldap_send_entry(be, op, lc, e, mapped_attrs, attrsonly);
154 sres = ldap_result2error(lc->ld, res, 1);
155 sres = ldap_back_map_result(sres);
156 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
157 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
166 send_search_result( conn, op, sres,
167 match, err, NULL, NULL, count );
174 if (mapped_attrs != attrs)
175 charray_free(mapped_attrs);
176 if (mapped_filter != filterstr)
192 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
195 BerElement *ber = NULL;
196 Attribute *attr, **attrp;
197 struct berval *dummy = NULL;
201 ent.e_dn = ldap_back_dn_restore( li, ldap_get_dn(lc->ld, e), 0 );
202 ent.e_ndn = ch_strdup( ent.e_dn );
203 (void) dn_normalize( ent.e_ndn );
207 attrp = &ent.e_attrs;
209 for ( a = ldap_first_attribute(lc->ld, e, &ber);
211 a = ldap_next_attribute(lc->ld, e, ber))
213 mapped = ldap_back_map(&li->at_map, a, 1);
216 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
221 if (slap_str2ad(mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
225 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
227 attr->a_vals = &dummy;
228 } else if ( strcasecmp( mapped, "objectclass" ) == 0 ) {
230 for ( last = 0; attr->a_vals[last]; last++ ) ;
231 for ( i = 0; bv = attr->a_vals[i]; i++ ) {
232 mapped = ldap_back_map(&li->oc_map, bv->bv_val, 1);
233 if (mapped == NULL) {
234 ber_bvfree(attr->a_vals[i]);
235 attr->a_vals[i] = NULL;
238 attr->a_vals[i] = attr->a_vals[last];
239 attr->a_vals[last] = NULL;
241 } else if ( mapped != bv->bv_val ) {
243 bv->bv_val = ch_strdup( mapped );
244 bv->bv_len = strlen( mapped );
249 attrp = &attr->a_next;
251 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
252 while (ent.e_attrs) {
254 ent.e_attrs = attr->a_next;
255 ad_free(attr->a_desc, 1);
256 if (attr->a_vals != &dummy)
257 ber_bvecfree(attr->a_vals);