]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
95a278947063c450f59ac6a5c655b071903ea8c8
[openldap] / servers / slapd / search.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* Portions
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "slap.h"
27
28 int
29 do_search(
30     Connection  *conn,  /* where to send results */
31     Operation   *op     /* info about the op to which we're responding */
32 ) {
33         ber_int_t       scope, deref, attrsonly;
34         ber_int_t       sizelimit, timelimit;
35         struct berval base = { 0, NULL };
36         struct berval pbase = { 0, NULL };
37         struct berval nbase = { 0, NULL };
38         struct berval   fstr = { 0, NULL };
39         Filter          *filter = NULL;
40         AttributeName   *an = NULL;
41         ber_len_t       siz, off, i;
42         Backend         *be;
43         int                     rc;
44         const char      *text;
45         int                     manageDSAit;
46
47 #ifdef NEW_LOGGING
48         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
49                 "do_search: conn %d\n", conn->c_connid ));
50 #else
51         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
52 #endif
53
54         /*
55          * Parse the search request.  It looks like this:
56          *
57          *      SearchRequest := [APPLICATION 3] SEQUENCE {
58          *              baseObject      DistinguishedName,
59          *              scope           ENUMERATED {
60          *                      baseObject      (0),
61          *                      singleLevel     (1),
62          *                      wholeSubtree    (2)
63          *              },
64          *              derefAliases    ENUMERATED {
65          *                      neverDerefaliases       (0),
66          *                      derefInSearching        (1),
67          *                      derefFindingBaseObj     (2),
68          *                      alwaysDerefAliases      (3)
69          *              },
70          *              sizelimit       INTEGER (0 .. 65535),
71          *              timelimit       INTEGER (0 .. 65535),
72          *              attrsOnly       BOOLEAN,
73          *              filter          Filter,
74          *              attributes      SEQUENCE OF AttributeType
75          *      }
76          */
77
78         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
79         if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
80                 &base, &scope, &deref, &sizelimit,
81             &timelimit, &attrsonly ) == LBER_ERROR )
82         {
83                 send_ldap_disconnect( conn, op,
84                         LDAP_PROTOCOL_ERROR, "decoding error" );
85                 rc = SLAPD_DISCONNECT;
86                 goto return_results;
87         }
88
89         switch( scope ) {
90         case LDAP_SCOPE_BASE:
91         case LDAP_SCOPE_ONELEVEL:
92         case LDAP_SCOPE_SUBTREE:
93                 break;
94         default:
95                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
96                         NULL, "invalid scope", NULL, NULL );
97                 goto return_results;
98         }
99
100         switch( deref ) {
101         case LDAP_DEREF_NEVER:
102         case LDAP_DEREF_FINDING:
103         case LDAP_DEREF_SEARCHING:
104         case LDAP_DEREF_ALWAYS:
105                 break;
106         default:
107                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
108                         NULL, "invalid deref", NULL, NULL );
109                 goto return_results;
110         }
111
112         rc = dnPrettyNormal( NULL, &base, &pbase, &nbase );
113         if( rc != LDAP_SUCCESS ) {
114 #ifdef NEW_LOGGING
115                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
116                         "do_search: conn %d  invalid dn (%s)\n",
117                         conn->c_connid, base.bv_val ));
118 #else
119                 Debug( LDAP_DEBUG_ANY,
120                         "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
121 #endif
122                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
123                     "invalid DN", NULL, NULL );
124                 goto return_results;
125         }
126
127 #ifdef NEW_LOGGING
128         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
129                 "do_search \"%s\" %d %d %d %d %d\n", base.bv_val, scope,
130                 deref, sizelimit, timelimit, attrsonly ));
131 #else
132         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
133                 base.bv_val, scope, deref );
134         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n",
135                 sizelimit, timelimit, attrsonly);
136 #endif
137
138         /* filter - returns a "normalized" version */
139         rc = get_filter( conn, op->o_ber, &filter, &fstr, &text );
140         if( rc != LDAP_SUCCESS ) {
141                 if( rc == SLAPD_DISCONNECT ) {
142                         send_ldap_disconnect( conn, op,
143                                 LDAP_PROTOCOL_ERROR, text );
144                 } else {
145                         send_ldap_result( conn, op, rc,
146                                 NULL, text, NULL, NULL );
147                 }
148                 goto return_results;
149         }
150
151 #ifdef NEW_LOGGING
152         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
153                 "do_search: conn %d     filter: %s\n", conn->c_connid, fstr.bv_val ));
154 #else
155         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr.bv_val, 0, 0 );
156 #endif
157
158         /* attributes */
159         siz = sizeof(AttributeName);
160         off = 0;
161         if ( ber_scanf( op->o_ber, "{M}}", &an, &siz, off ) == LBER_ERROR ) {
162                 send_ldap_disconnect( conn, op,
163                         LDAP_PROTOCOL_ERROR, "decoding attrs error" );
164                 rc = SLAPD_DISCONNECT;
165                 goto return_results;
166         }
167         for ( i=0; i<siz; i++ ) {
168                 an[i].an_desc = NULL;
169                 an[i].an_oc = NULL;
170                 slap_bv2ad(&an[i].an_name, &an[i].an_desc, &text);
171         }
172
173         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
174 #ifdef NEW_LOGGING
175                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
176                         "do_search: conn %d  get_ctrls failed (%d)\n",
177                         conn->c_connid, rc ));
178 #else
179                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
180 #endif
181
182                 goto return_results;
183         }
184
185 #ifdef NEW_LOGGING
186         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
187                 "do_search: conn %d     attrs:", conn->c_connid ));
188 #else
189         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
190 #endif
191
192         if ( siz != 0 ) {
193                 for ( i = 0; i<siz; i++ ) {
194 #ifdef NEW_LOGGING
195                         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
196                                 "do_search:        %s", an[i].an_name.bv_val ));
197 #else
198                         Debug( LDAP_DEBUG_ARGS, " %s", an[i].an_name.bv_val, 0, 0 );
199 #endif
200                 }
201         }
202
203 #ifdef NEW_LOGGING
204         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS, "\n" ));
205 #else
206         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
207 #endif
208
209         Statslog( LDAP_DEBUG_STATS,
210             "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
211             op->o_connid, op->o_opid, pbase.bv_val, scope, fstr.bv_val );
212
213         manageDSAit = get_manageDSAit( op );
214
215         if ( scope == LDAP_SCOPE_BASE ) {
216                 Entry *entry = NULL;
217
218                 if ( nbase.bv_len == 0 ) {
219 #ifdef LDAP_CONNECTIONLESS
220                         /* Ignore LDAPv2 CLDAP Root DSE queries */
221                         if (op->o_protocol==LDAP_VERSION2 && conn->c_is_udp) {
222                                 goto return_results;
223                         }
224 #endif
225                         /* check restrictions */
226                         rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
227                         if( rc != LDAP_SUCCESS ) {
228                                 send_ldap_result( conn, op, rc,
229                                         NULL, text, NULL, NULL );
230                                 goto return_results;
231                         }
232
233                         rc = root_dse_info( conn, &entry, &text );
234                 }
235
236 #if defined( SLAPD_SCHEMA_DN )
237                 else if ( strcasecmp( nbase.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
238                         /* check restrictions */
239                         rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
240                         if( rc != LDAP_SUCCESS ) {
241                                 send_ldap_result( conn, op, rc,
242                                         NULL, text, NULL, NULL );
243                                 goto return_results;
244                         }
245
246                         rc = schema_info( &entry, &text );
247                 }
248 #endif
249
250                 if( rc != LDAP_SUCCESS ) {
251                         send_ldap_result( conn, op, rc,
252                                 NULL, text, NULL, NULL );
253                         goto return_results;
254
255                 } else if ( entry != NULL ) {
256                         rc = test_filter( NULL, conn, op,
257                                 entry, filter );
258
259                         if( rc == LDAP_COMPARE_TRUE ) {
260                                 send_search_entry( NULL, conn, op,
261                                         entry, an, attrsonly, NULL );
262                         }
263                         entry_free( entry );
264
265                         send_ldap_result( conn, op, LDAP_SUCCESS,
266                                 NULL, NULL, NULL, NULL );
267
268                         goto return_results;
269                 }
270         }
271
272         if( !nbase.bv_len && default_search_nbase.bv_len ) {
273                 ch_free( pbase.bv_val );
274                 ch_free( nbase.bv_val );
275
276                 ber_dupbv( &pbase, &default_search_base );
277                 ber_dupbv( &nbase, &default_search_nbase );
278         }
279
280         /*
281          * We could be serving multiple database backends.  Select the
282          * appropriate one, or send a referral to our "referral server"
283          * if we don't hold it.
284          */
285         if ( (be = select_backend( &nbase, manageDSAit, 1 )) == NULL ) {
286                 BerVarray ref = referral_rewrite( default_referral,
287                         NULL, &pbase, scope );
288
289                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
290                         NULL, NULL, ref ? ref : default_referral, NULL );
291
292                 ber_bvarray_free( ref );
293                 goto return_results;
294         }
295
296         /* check restrictions */
297         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
298         if( rc != LDAP_SUCCESS ) {
299                 send_ldap_result( conn, op, rc,
300                         NULL, text, NULL, NULL );
301                 goto return_results;
302         }
303
304         /* check for referrals */
305         rc = backend_check_referrals( be, conn, op, &pbase, &nbase );
306         if ( rc != LDAP_SUCCESS ) {
307                 goto return_results;
308         }
309
310         /* deref the base if needed */
311         suffix_alias( be, &nbase );
312
313         /* actually do the search and send the result(s) */
314         if ( be->be_search ) {
315                 (*be->be_search)( be, conn, op, &pbase, &nbase,
316                         scope, deref, sizelimit,
317                     timelimit, filter, &fstr, an, attrsonly );
318         } else {
319                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
320                         NULL, "operation not supported within namingContext",
321                         NULL, NULL );
322         }
323
324 return_results:;
325         if( pbase.bv_val != NULL) free( pbase.bv_val );
326         if( nbase.bv_val != NULL) free( nbase.bv_val );
327
328         if( fstr.bv_val != NULL) free( fstr.bv_val );
329         if( filter != NULL) filter_free( filter );
330         if( an != NULL ) free( an );
331
332         return rc;
333 }