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.
33 #include <ac/socket.h>
34 #include <ac/string.h>
38 #include "back-ldap.h"
40 static void ldap_send_entry( Backend *be, Operation *op, struct ldapconn *lc,
41 LDAPMessage *e, char **attrs, int attrsonly );
55 const char *filterstr,
60 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
64 int i, rc, msgid, sres = LDAP_SUCCESS;
65 char *match = NULL, *err = NULL;
67 lc = ldap_back_getconn(li, conn, op);
72 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
74 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&time);
76 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&size);
78 ldap_back_dobind(lc, op);
83 if ((msgid = ldap_search(lc->ld, base, scope, filterstr, attrs,
85 fail: return( ldap_back_op_result(lc, op) );
87 /* We pull apart the ber result, stuff it into a slapd entry, and
88 * let send_search_entry stuff it back into ber format. Slow & ugly,
89 * but this is necessary for version matching, and for ACL processing.
92 for (i=0, rc=0; rc != -1;
93 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res)) {
96 /* check for abandon */
97 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
99 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
102 ldap_abandon(lc->ld, msgid);
103 } else if (rc == 0) {
106 ldap_pvt_thread_yield();
108 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
109 e = ldap_first_entry(lc->ld,res);
110 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
114 sres = ldap_result2error(lc->ld, res, 1);
115 sres = ldap_back_map_result(sres);
116 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
117 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
129 send_search_result( conn, op, sres,
130 match, err, NULL, NULL, i );
150 BerElement *ber = NULL;
151 Attribute *attr, **attrp;
152 struct berval *dummy = NULL;
155 ent.e_dn = ldap_get_dn(lc->ld, e);
156 ent.e_ndn = ch_strdup( ent.e_dn);
157 (void) dn_normalize( ent.e_ndn );
161 attrp = &ent.e_attrs;
163 for ( a = ldap_first_attribute(lc->ld, e, &ber);
165 a = ldap_next_attribute(lc->ld, e, ber))
167 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
172 slap_str2ad(a, &attr->a_desc, &text);
173 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
175 attr->a_vals = &dummy;
177 attrp = &attr->a_next;
179 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
180 for (;ent.e_attrs;) {
182 ent.e_attrs = attr->a_next;
183 ad_free(attr->a_desc, 1);
184 if (attr->a_vals != &dummy)
185 ber_bvecfree(attr->a_vals);