]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
Move most of SLAPI frontend into overlay
[openldap] / servers / slapd / search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "lutil.h"
34 #include "slap.h"
35
36 int
37 do_search(
38     Operation   *op,    /* info about the op to which we're responding */
39     SlapReply   *rs     /* all the response data we'll send */ )
40 {
41         struct berval base = BER_BVNULL;
42         ber_len_t       siz, off, i;
43
44         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
45
46         /*
47          * Parse the search request.  It looks like this:
48          *
49          *      SearchRequest := [APPLICATION 3] SEQUENCE {
50          *              baseObject      DistinguishedName,
51          *              scope           ENUMERATED {
52          *                      baseObject      (0),
53          *                      singleLevel     (1),
54          *                      wholeSubtree (2),
55          *          subordinate (3)  -- OpenLDAP extension
56          *              },
57          *              derefAliases    ENUMERATED {
58          *                      neverDerefaliases       (0),
59          *                      derefInSearching        (1),
60          *                      derefFindingBaseObj     (2),
61          *                      alwaysDerefAliases      (3)
62          *              },
63          *              sizelimit       INTEGER (0 .. 65535),
64          *              timelimit       INTEGER (0 .. 65535),
65          *              attrsOnly       BOOLEAN,
66          *              filter          Filter,
67          *              attributes      SEQUENCE OF AttributeType
68          *      }
69          */
70
71         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
72         if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
73                 &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
74             &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
75         {
76                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
77                 rs->sr_err = SLAPD_DISCONNECT;
78                 goto return_results;
79         }
80
81         if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) {
82                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" );
83                 goto return_results;
84         }
85
86         if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) {
87                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" );
88                 goto return_results;
89         }
90
91         switch( op->ors_scope ) {
92         case LDAP_SCOPE_BASE:
93         case LDAP_SCOPE_ONELEVEL:
94         case LDAP_SCOPE_SUBTREE:
95 #ifdef LDAP_SCOPE_SUBORDINATE
96         case LDAP_SCOPE_SUBORDINATE:
97 #endif
98                 break;
99         default:
100                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
101                 goto return_results;
102         }
103
104         switch( op->ors_deref ) {
105         case LDAP_DEREF_NEVER:
106         case LDAP_DEREF_FINDING:
107         case LDAP_DEREF_SEARCHING:
108         case LDAP_DEREF_ALWAYS:
109                 break;
110         default:
111                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
112                 goto return_results;
113         }
114
115         rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
116         if( rs->sr_err != LDAP_SUCCESS ) {
117                 Debug( LDAP_DEBUG_ANY,
118                         "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
119                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
120                 goto return_results;
121         }
122
123         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
124                 base.bv_val, op->ors_scope, op->ors_deref );
125         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n",
126                 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
127
128         /* filter - returns a "normalized" version */
129         rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
130         if( rs->sr_err != LDAP_SUCCESS ) {
131                 if( rs->sr_err == SLAPD_DISCONNECT ) {
132                         rs->sr_err = LDAP_PROTOCOL_ERROR;
133                         send_ldap_disconnect( op, rs );
134                         rs->sr_err = SLAPD_DISCONNECT;
135                 } else {
136                         send_ldap_result( op, rs );
137                 }
138                 goto return_results;
139         }
140         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
141         
142         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n",
143                 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
144
145         /* attributes */
146         siz = sizeof(AttributeName);
147         off = offsetof(AttributeName,an_name);
148         if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
149                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
150                 rs->sr_err = SLAPD_DISCONNECT;
151                 goto return_results;
152         }
153         for ( i=0; i<siz; i++ ) {
154                 const char *dummy;      /* ignore msgs from bv2ad */
155                 op->ors_attrs[i].an_desc = NULL;
156                 op->ors_attrs[i].an_oc = NULL;
157                 op->ors_attrs[i].an_oc_exclude = 0;
158                 slap_bv2ad(&op->ors_attrs[i].an_name,
159                         &op->ors_attrs[i].an_desc, &dummy);
160         }
161
162         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
163                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
164
165                 goto return_results;
166         }
167
168         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
169
170         if ( siz != 0 ) {
171                 for ( i = 0; i<siz; i++ ) {
172                         Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
173                 }
174         }
175
176         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
177
178         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
179                 char abuf[BUFSIZ/2], *ptr = abuf;
180                 int len = 0, alen;
181
182                 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
183                 Statslog( LDAP_DEBUG_STATS,
184                         "%s SRCH base=\"%s\" %s filter=\"%s\"\n",
185                         op->o_log_prefix, op->o_req_dn.bv_val, abuf,
186                         op->ors_filterstr.bv_val, 0 );
187
188                 for ( i = 0; i<siz; i++ ) {
189                         alen = op->ors_attrs[i].an_name.bv_len;
190                         if (alen >= sizeof(abuf)) {
191                                 alen = sizeof(abuf)-1;
192                         }
193                         if (len && (len + 1 + alen >= sizeof(abuf))) {
194                                 Statslog( LDAP_DEBUG_STATS, "%s SRCH attr=%s\n",
195                                     op->o_log_prefix, abuf, 0, 0, 0 );
196                                 len = 0;
197                                 ptr = abuf;
198                         }
199                         if (len) {
200                                 *ptr++ = ' ';
201                                 len++;
202                         }
203                         ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
204                         len += alen;
205                         *ptr = '\0';
206                 }
207                 if (len) {
208                         Statslog( LDAP_DEBUG_STATS, "%s SRCH attr=%s\n",
209                                 op->o_log_prefix, abuf, 0, 0, 0 );
210                 }
211         }
212
213         op->o_bd = frontendDB;
214         rs->sr_err = frontendDB->be_search( op, rs );
215
216 return_results:;
217         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
218                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
219         }
220         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
221                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
222         }
223         if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
224                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
225         }
226         if ( op->ors_filter != NULL) {
227                 filter_free_x( op, op->ors_filter );
228         }
229         if ( op->ors_attrs != NULL ) {
230                 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
231         }
232
233         return rs->sr_err;
234 }
235
236 int
237 fe_op_search( Operation *op, SlapReply *rs )
238 {
239         int                     manageDSAit;
240         int                     be_manageDSAit;
241
242         manageDSAit = get_manageDSAit( op );
243
244         /* fake while loop to allow breaking out */
245         while ( op->ors_scope == LDAP_SCOPE_BASE ) {
246                 Entry *entry = NULL;
247
248                 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
249 #ifdef LDAP_CONNECTIONLESS
250                         /* Ignore LDAPv2 CLDAP Root DSE queries */
251                         if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
252                                 goto return_results;
253                         }
254 #endif
255                         /* check restrictions */
256                         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
257                                 send_ldap_result( op, rs );
258                                 goto return_results;
259                         }
260
261                         rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
262
263                 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
264                         /* check restrictions */
265                         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
266                                 send_ldap_result( op, rs );
267                                 goto return_results;
268                         }
269
270                         rs->sr_err = schema_info( &entry, &rs->sr_text );
271                 }
272
273                 if( rs->sr_err != LDAP_SUCCESS ) {
274                         send_ldap_result( op, rs );
275                         goto return_results;
276
277                 } else if ( entry != NULL ) {
278                         rs->sr_err = test_filter( op, entry, op->ors_filter );
279
280                         if( rs->sr_err == LDAP_COMPARE_TRUE ) {
281                                 rs->sr_entry = entry;
282                                 rs->sr_attrs = op->ors_attrs;
283                                 rs->sr_operational_attrs = NULL;
284                                 send_search_entry( op, rs );
285                                 rs->sr_entry = NULL;
286                                 rs->sr_operational_attrs = NULL;
287                         }
288                         entry_free( entry );
289
290                         rs->sr_err = LDAP_SUCCESS;
291                         send_ldap_result( op, rs );
292                         goto return_results;
293                 }
294                 break;
295         }
296
297         if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) {
298                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
299                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
300
301                 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
302                 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
303         }
304
305         /*
306          * We could be serving multiple database backends.  Select the
307          * appropriate one, or send a referral to our "referral server"
308          * if we don't hold it.
309          */
310
311         be_manageDSAit = manageDSAit;
312
313         op->o_bd = select_backend( &op->o_req_ndn, be_manageDSAit, 1 );
314         if ( op->o_bd == NULL ) {
315                 rs->sr_ref = referral_rewrite( default_referral,
316                         NULL, &op->o_req_dn, op->ors_scope );
317
318                 if (!rs->sr_ref) rs->sr_ref = default_referral;
319                 rs->sr_err = LDAP_REFERRAL;
320                 op->o_bd = frontendDB;
321                 send_ldap_result( op, rs );
322                 op->o_bd = NULL;
323
324                 if (rs->sr_ref != default_referral)
325                 ber_bvarray_free( rs->sr_ref );
326                 rs->sr_ref = NULL;
327                 goto return_results;
328         }
329
330         /* check restrictions */
331         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
332                 send_ldap_result( op, rs );
333                 goto return_results;
334         }
335
336         /* check for referrals */
337         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
338                 goto return_results;
339         }
340
341         /* actually do the search and send the result(s) */
342         if ( op->o_bd->be_search ) {
343                 if ( limits_check( op, rs ) == 0 ) {
344                         (op->o_bd->be_search)( op, rs );
345                 }
346                 /* else limits_check() sends error */
347
348         } else {
349                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
350                         "operation not supported within namingContext" );
351         }
352
353 return_results:;
354         return rs->sr_err;
355 }
356