]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
eaeae0e7d07a060b3b56a66fa60cb53bca7cf272
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2
3 /*
4  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
5  * 
6  * Permission is granted to anyone to use this software for any purpose
7  * on any computer system, and to alter it and redistribute it, subject
8  * to the following restrictions:
9  * 
10  * 1. The author is not responsible for the consequences of use of this
11  *    software, no matter how awful, even if they arise from flaws in it.
12  * 
13  * 2. The origin of this software must not be misrepresented, either by
14  *    explicit claim or by omission.  Since few users ever read sources,
15  *    credits should appear in the documentation.
16  * 
17  * 3. Altered versions must be plainly marked as such, and must not be
18  *    misrepresented as being the original software.  Since few users
19  *    ever read sources, credits should appear in the documentation.
20  * 
21  * 4. This notice may not be removed or altered.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31
32 #include "slap.h"
33 #include "back-ldap.h"
34
35 static void ldap_send_entry( Backend *be, Operation *op, struct ldapconn *lc,
36                              LDAPMessage *e, char **attrs, int attrsonly );
37
38 int
39 ldap_back_search(
40     Backend     *be,
41     Connection  *conn,
42     Operation   *op,
43     char        *base,
44     int         scope,
45     int         deref,
46     int         size,
47     int         time,
48     Filter      *filter,
49     char        *filterstr,
50     char        **attrs,
51     int         attrsonly
52 )
53 {
54         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
55         struct ldapconn *lc;
56         struct timeval  tv;
57         LDAPMessage             *res, *e;
58         int                     i, rc, msgid, sres = LDAP_SUCCESS; 
59         char *match = NULL, *err = NULL;
60
61         lc = ldap_back_getconn(li, conn, op);
62         if (!lc)
63                 return( -1 );
64
65         if (deref != -1)
66                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
67         if (time != -1)
68                 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&time);
69         if (size != -1)
70                 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&size);
71         if (!lc->bound) {
72                 ldap_back_dobind(lc, op);
73                 if (!lc->bound)
74                         return( -1 );
75         }
76
77         if ((msgid = ldap_search(lc->ld, base, scope, filterstr, attrs,
78                 attrsonly)) == -1)
79 fail:           return( ldap_back_op_result(lc, op) );
80
81         /* We pull apart the ber result, stuff it into a slapd entry, and
82          * let send_search_entry stuff it back into ber format. Slow & ugly,
83          * but this is necessary for version matching, and for ACL processing.
84          */
85         
86         for (i=0, rc=0; rc != -1;
87                 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res)) {
88                 int ab;
89
90                 /* check for abandon */
91                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
92                 ab = op->o_abandon;
93                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
94
95                 if (ab) {
96                         ldap_abandon(lc->ld, msgid);
97                 } else if (rc == 0) {
98                         tv.tv_sec = 0;
99                         tv.tv_usec = 100000;
100                         ldap_pvt_thread_yield();
101                         continue;
102                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
103                         e = ldap_first_entry(lc->ld,res);
104                         ldap_send_entry(be, op, lc, e, attrs, attrsonly);
105                         i++;
106                 } else {
107                         sres = ldap_result2error(lc->ld, res, 1);
108                         ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
109                         ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
110                         rc = 0;
111                 }
112                 ldap_msgfree(res);
113                 if (ab)
114                         return (0);
115                 else if (rc == 0)
116                         break;
117         }
118
119         if (rc == -1)
120                 goto fail;
121
122         send_search_result( conn, op, sres,
123                 match, err, NULL, NULL, i );
124         if (match)
125                 free(match);
126         if (err)
127                 free(err);
128         return( 0 );
129 }
130
131 static void
132 ldap_send_entry(
133         Backend *be,
134         Operation *op,
135         struct ldapconn *lc,
136         LDAPMessage *e,
137         char **attrs,
138         int attrsonly
139 )
140 {
141         char *a;
142         Entry ent;
143         BerElement *ber = NULL;
144         Attribute *attr, **attrp;
145         struct berval *dummy = NULL;
146
147         ent.e_dn = ldap_get_dn(lc->ld, e);
148         ent.e_ndn = ch_strdup( ent.e_dn);
149         (void) dn_normalize_case( ent.e_ndn );
150         ent.e_id = 0;
151         ent.e_attrs = 0;
152         ent.e_private = 0;
153         attrp = &ent.e_attrs;
154
155         for (a = ldap_first_attribute(lc->ld, e, &ber); a;
156                 a = ldap_next_attribute(lc->ld, e, ber)) {
157                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
158                 attr->a_next = 0;
159                 attr->a_type = ch_strdup(a);
160                 attr->a_syntax = attr_syntax(a);
161                 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
162                 if (!attr->a_vals)
163                         attr->a_vals = &dummy;
164                 *attrp = attr;
165                 attrp = &attr->a_next;
166         }
167         send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
168         for (;ent.e_attrs;) {
169                 attr=ent.e_attrs;
170                 ent.e_attrs = attr->a_next;
171                 free(attr->a_type);
172                 if (attr->a_vals != &dummy)
173                         ber_bvecfree(attr->a_vals);
174                 free(attr);
175         }
176         if (ber)
177                 ber_free(ber,0);
178 }