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 i, rc, msgid, sres = LDAP_SUCCESS;
74 char *match = NULL, *err = NULL;
78 lc = ldap_back_getconn(li, conn, op);
84 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
86 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&time);
88 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&size);
90 if ( !ldap_back_dobind( lc, op ) ) {
94 mbase = ldap_back_dn_massage( li, ch_strdup( base ), 0 );
95 if ( mbase == NULL ) {
99 if ((msgid = ldap_search(lc->ld, mbase, scope, filterstr, attrs,
101 fail: return( ldap_back_op_result(lc, op) );
103 /* We pull apart the ber result, stuff it into a slapd entry, and
104 * let send_search_entry stuff it back into ber format. Slow & ugly,
105 * but this is necessary for version matching, and for ACL processing.
108 for (i=0, rc=0; rc != -1;
109 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res)) {
112 /* check for abandon */
113 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
115 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
118 ldap_abandon(lc->ld, msgid);
119 } else if (rc == 0) {
122 ldap_pvt_thread_yield();
124 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
125 e = ldap_first_entry(lc->ld,res);
126 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
130 sres = ldap_result2error(lc->ld, res, 1);
131 sres = ldap_back_map_result(sres);
132 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
133 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
145 send_search_result( conn, op, sres,
146 match, err, NULL, NULL, i );
166 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
169 BerElement *ber = NULL;
170 Attribute *attr, **attrp;
171 struct berval *dummy = NULL;
174 ent.e_dn = ldap_back_dn_restore( li, ldap_get_dn(lc->ld, e), 0 );
175 ent.e_ndn = ch_strdup( ent.e_dn );
176 (void) dn_normalize( ent.e_ndn );
180 attrp = &ent.e_attrs;
182 for ( a = ldap_first_attribute(lc->ld, e, &ber);
184 a = ldap_next_attribute(lc->ld, e, ber))
186 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
191 slap_str2ad(a, &attr->a_desc, &text);
192 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
194 attr->a_vals = &dummy;
196 attrp = &attr->a_next;
198 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
199 for (;ent.e_attrs;) {
201 ent.e_attrs = attr->a_next;
202 ad_free(attr->a_desc, 1);
203 if (attr->a_vals != &dummy)
204 ber_bvecfree(attr->a_vals);