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