]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
240a85736b2b576eb67f09b049fa30925d3fee68
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* This is an altered version */
8 /*
9  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10  * 
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:
14  * 
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.
17  * 
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.
21  * 
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.
25  * 
26  * 4. This notice may not be removed or altered.
27  *
28  *
29  *
30  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31  * 
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.
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45
46 #include "slap.h"
47 #include "back-ldap.h"
48
49 static void ldap_send_entry( Backend *be, Operation *op, struct ldapconn *lc,
50                              LDAPMessage *e, char **attrs, int attrsonly );
51
52 int
53 ldap_back_search(
54     Backend     *be,
55     Connection  *conn,
56     Operation   *op,
57     const char  *base,
58     const char  *nbase,
59     int         scope,
60     int         deref,
61     int         size,
62     int         time,
63     Filter      *filter,
64     const char  *filterstr,
65     char        **attrs,
66     int         attrsonly
67 )
68 {
69         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
70         struct ldapconn *lc;
71         struct timeval  tv;
72         LDAPMessage             *res, *e;
73         int                     i, rc, msgid, sres = LDAP_SUCCESS; 
74         char *match = NULL, *err = NULL;
75
76         char *mbase;
77
78         lc = ldap_back_getconn(li, conn, op);
79         if ( !lc ) {
80                 return( -1 );
81         }
82
83         if (deref != -1)
84                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
85         if (time != -1)
86                 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&time);
87         if (size != -1)
88                 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&size);
89         
90         if ( !ldap_back_dobind( lc, op ) ) {
91                 return( -1 );
92         }
93
94         mbase = ldap_back_dn_massage( li, ch_strdup( base ), 0 );
95         if ( mbase == NULL ) {
96                 return -1;
97         }
98
99         if ((msgid = ldap_search(lc->ld, mbase, scope, filterstr, attrs,
100                 attrsonly)) == -1)
101 fail:           return( ldap_back_op_result(lc, op) );
102
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.
106          */
107         
108         for (i=0, rc=0; rc != -1;
109                 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res)) {
110                 int ab;
111
112                 /* check for abandon */
113                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
114                 ab = op->o_abandon;
115                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
116
117                 if (ab) {
118                         ldap_abandon(lc->ld, msgid);
119                 } else if (rc == 0) {
120                         tv.tv_sec = 0;
121                         tv.tv_usec = 100000;
122                         ldap_pvt_thread_yield();
123                         continue;
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);
127                         i++;
128                         ldap_msgfree(res);
129                 } else {
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);
134                         rc = 0;
135                 }
136                 if (ab)
137                         return (0);
138                 else if (rc == 0)
139                         break;
140         }
141
142         if (rc == -1)
143                 goto fail;
144
145         send_search_result( conn, op, sres,
146                 match, err, NULL, NULL, i );
147         if (match)
148                 free(match);
149         if (err)
150                 free(err);
151         if (mbase) 
152                 free(mbase);
153         return( 0 );
154 }
155
156 static void
157 ldap_send_entry(
158         Backend *be,
159         Operation *op,
160         struct ldapconn *lc,
161         LDAPMessage *e,
162         char **attrs,
163         int attrsonly
164 )
165 {
166         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
167         char *a;
168         Entry ent;
169         BerElement *ber = NULL;
170         Attribute *attr, **attrp;
171         struct berval *dummy = NULL;
172         const char *text;
173
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 );
177         ent.e_id = 0;
178         ent.e_attrs = 0;
179         ent.e_private = 0;
180         attrp = &ent.e_attrs;
181
182         for (   a = ldap_first_attribute(lc->ld, e, &ber);
183                         a != NULL;
184                         a = ldap_next_attribute(lc->ld, e, ber))
185         {
186                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
187                 if (attr == NULL)
188                         continue;
189                 attr->a_next = 0;
190                 attr->a_desc = NULL;
191                 slap_str2ad(a, &attr->a_desc, &text);
192                 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
193                 if (!attr->a_vals)
194                         attr->a_vals = &dummy;
195                 *attrp = attr;
196                 attrp = &attr->a_next;
197         }
198         send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
199         for (;ent.e_attrs;) {
200                 attr=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);
205                 free(attr);
206         }
207         if (ber)
208                 ber_free(ber,0);
209         
210         if ( ent.e_dn )
211                 free( ent.e_dn );
212         if ( ent.e_ndn )
213                 free( ent.e_ndn );
214 }