]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
01b90e3d9bcd961785eaac6d781b2f239fa1ce82
[openldap] / servers / slapd / search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2017 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, "%s do_search\n",
45                 op->o_log_prefix, 0, 0 );
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         case LDAP_SCOPE_SUBORDINATE:
96                 break;
97         default:
98                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
99                 goto return_results;
100         }
101
102         switch( op->ors_deref ) {
103         case LDAP_DEREF_NEVER:
104         case LDAP_DEREF_FINDING:
105         case LDAP_DEREF_SEARCHING:
106         case LDAP_DEREF_ALWAYS:
107                 break;
108         default:
109                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
110                 goto return_results;
111         }
112
113         rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
114         if( rs->sr_err != LDAP_SUCCESS ) {
115                 Debug( LDAP_DEBUG_ANY, "%s do_search: invalid dn: \"%s\"\n",
116                         op->o_log_prefix, base.bv_val, 0 );
117                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
118                 goto return_results;
119         }
120
121         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
122                 base.bv_val, op->ors_scope, op->ors_deref );
123         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n",
124                 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
125
126         /* filter - returns a "normalized" version */
127         rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
128         if( rs->sr_err != LDAP_SUCCESS ) {
129                 if( rs->sr_err == SLAPD_DISCONNECT ) {
130                         rs->sr_err = LDAP_PROTOCOL_ERROR;
131                         send_ldap_disconnect( op, rs );
132                         rs->sr_err = SLAPD_DISCONNECT;
133                 } else {
134                         send_ldap_result( op, rs );
135                 }
136                 goto return_results;
137         }
138         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
139         
140         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n",
141                 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
142
143         /* attributes */
144         siz = sizeof(AttributeName);
145         off = offsetof(AttributeName,an_name);
146         if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
147                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
148                 rs->sr_err = SLAPD_DISCONNECT;
149                 goto return_results;
150         }
151         for ( i=0; i<siz; i++ ) {
152                 const char *dummy;      /* ignore msgs from bv2ad */
153                 op->ors_attrs[i].an_desc = NULL;
154                 op->ors_attrs[i].an_oc = NULL;
155                 op->ors_attrs[i].an_flags = 0;
156                 if ( slap_bv2ad( &op->ors_attrs[i].an_name,
157                         &op->ors_attrs[i].an_desc, &dummy ) != LDAP_SUCCESS )
158                 {
159                         if ( slap_bv2undef_ad( &op->ors_attrs[i].an_name,
160                                 &op->ors_attrs[i].an_desc, &dummy,
161                                 SLAP_AD_PROXIED|SLAP_AD_NOINSERT ) )
162                         {
163                                 struct berval *bv = &op->ors_attrs[i].an_name;
164
165                                 /* RFC 4511 LDAPv3: All User Attributes */
166                                 if ( bvmatch( bv, slap_bv_all_user_attrs ) ) {
167                                         continue;
168                                 }
169
170                                 /* RFC 3673 LDAPv3: All Operational Attributes */
171                                 if ( bvmatch( bv, slap_bv_all_operational_attrs ) ) {
172                                         continue;
173                                 }
174
175                                 /* RFC 4529 LDAP: Requesting Attributes by Object Class */
176                                 if ( bv->bv_len > 1 && bv->bv_val[0] == '@' ) {
177                                         /* FIXME: check if remaining is valid oc name? */
178                                         continue;
179                                 }
180
181                                 /* add more "exceptions" to RFC 4511 4.5.1.8. */
182
183                                 /* invalid attribute description? remove */
184                                 if ( ad_keystring( bv ) ) {
185                                         /* NOTE: parsed in-place, don't modify;
186                                          * rather add "1.1", which must be ignored */
187                                         BER_BVSTR( &op->ors_attrs[i].an_name, LDAP_NO_ATTRS );
188                                 }
189
190                                 /* otherwise leave in place... */
191                         }
192                 }
193         }
194
195         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
196                 Debug( LDAP_DEBUG_ANY, "%s do_search: get_ctrls failed\n",
197                         op->o_log_prefix, 0, 0 );
198                 goto return_results;
199         }
200
201         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
202
203         if ( siz != 0 ) {
204                 for ( i = 0; i<siz; i++ ) {
205                         Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
206                 }
207         }
208
209         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
210
211         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
212                 char abuf[BUFSIZ/2], *ptr = abuf;
213                 unsigned len = 0, alen;
214
215                 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
216                 Statslog( LDAP_DEBUG_STATS,
217                         "%s SRCH base=\"%s\" %s filter=\"%s\"\n",
218                         op->o_log_prefix, op->o_req_dn.bv_val, abuf,
219                         op->ors_filterstr.bv_val, 0 );
220
221                 for ( i = 0; i<siz; i++ ) {
222                         alen = op->ors_attrs[i].an_name.bv_len;
223                         if (alen >= sizeof(abuf)) {
224                                 alen = sizeof(abuf)-1;
225                         }
226                         if (len && (len + 1 + alen >= sizeof(abuf))) {
227                                 Statslog( LDAP_DEBUG_STATS, "%s SRCH attr=%s\n",
228                                     op->o_log_prefix, abuf, 0, 0, 0 );
229                                 len = 0;
230                                 ptr = abuf;
231                         }
232                         if (len) {
233                                 *ptr++ = ' ';
234                                 len++;
235                         }
236                         ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
237                         len += alen;
238                         *ptr = '\0';
239                 }
240                 if (len) {
241                         Statslog( LDAP_DEBUG_STATS, "%s SRCH attr=%s\n",
242                                 op->o_log_prefix, abuf, 0, 0, 0 );
243                 }
244         }
245
246         op->o_bd = frontendDB;
247         rs->sr_err = frontendDB->be_search( op, rs );
248         if ( rs->sr_err == SLAPD_ASYNCOP ) {
249                 /* skip cleanup */
250                 return rs->sr_err;
251         }
252
253 return_results:;
254         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
255                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
256         }
257         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
258                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
259         }
260         if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
261                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
262         }
263         if ( op->ors_filter != NULL) {
264                 filter_free_x( op, op->ors_filter, 1 );
265         }
266         if ( op->ors_attrs != NULL ) {
267                 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
268         }
269
270         return rs->sr_err;
271 }
272
273 int
274 fe_op_search( Operation *op, SlapReply *rs )
275 {
276         BackendDB               *bd = op->o_bd;
277
278         if ( op->ors_scope == LDAP_SCOPE_BASE ) {
279                 Entry *entry = NULL;
280
281                 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
282 #ifdef LDAP_CONNECTIONLESS
283                         /* Ignore LDAPv2 CLDAP Root DSE queries */
284                         if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
285                                 goto return_results;
286                         }
287 #endif
288                         /* check restrictions */
289                         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
290                                 send_ldap_result( op, rs );
291                                 goto return_results;
292                         }
293
294                         rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
295
296                 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
297                         /* check restrictions */
298                         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
299                                 send_ldap_result( op, rs );
300                                 goto return_results;
301                         }
302
303                         rs->sr_err = schema_info( &entry, &rs->sr_text );
304                 }
305
306                 if( rs->sr_err != LDAP_SUCCESS ) {
307                         send_ldap_result( op, rs );
308                         goto return_results;
309
310                 } else if ( entry != NULL ) {
311                         if ( get_assert( op ) &&
312                                 ( test_filter( op, entry, get_assertion( op )) != LDAP_COMPARE_TRUE )) {
313                                 rs->sr_err = LDAP_ASSERTION_FAILED;
314                                 goto fail1;
315                         }
316
317                         rs->sr_err = test_filter( op, entry, op->ors_filter );
318
319                         if( rs->sr_err == LDAP_COMPARE_TRUE ) {
320                                 /* note: we set no limits because either
321                                  * no limit is specified, or at least 1
322                                  * is specified, and we're going to return
323                                  * at most one entry */                 
324                                 op->ors_slimit = SLAP_NO_LIMIT;
325                                 op->ors_tlimit = SLAP_NO_LIMIT;
326
327                                 rs->sr_entry = entry;
328                                 rs->sr_attrs = op->ors_attrs;
329                                 rs->sr_operational_attrs = NULL;
330                                 rs->sr_flags = 0;
331                                 send_search_entry( op, rs );
332                                 rs->sr_entry = NULL;
333                                 rs->sr_operational_attrs = NULL;
334                         }
335                         rs->sr_err = LDAP_SUCCESS;
336 fail1:
337                         entry_free( entry );
338                         send_ldap_result( op, rs );
339                         goto return_results;
340                 }
341         }
342
343         if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) {
344                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
345                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
346
347                 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
348                 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
349         }
350
351         /*
352          * We could be serving multiple database backends.  Select the
353          * appropriate one, or send a referral to our "referral server"
354          * if we don't hold it.
355          */
356
357         op->o_bd = select_backend( &op->o_req_ndn, 1 );
358         if ( op->o_bd == NULL ) {
359                 rs->sr_ref = referral_rewrite( default_referral,
360                         NULL, &op->o_req_dn, op->ors_scope );
361
362                 if (!rs->sr_ref) rs->sr_ref = default_referral;
363                 rs->sr_err = LDAP_REFERRAL;
364                 op->o_bd = bd;
365                 send_ldap_result( op, rs );
366
367                 if (rs->sr_ref != default_referral)
368                 ber_bvarray_free( rs->sr_ref );
369                 rs->sr_ref = NULL;
370                 goto return_results;
371         }
372
373         /* check restrictions */
374         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
375                 send_ldap_result( op, rs );
376                 goto return_results;
377         }
378
379         /* check for referrals */
380         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
381                 goto return_results;
382         }
383
384         if ( SLAP_SHADOW(op->o_bd) && get_dontUseCopy(op) ) {
385                 /* don't use shadow copy */
386                 BerVarray defref = op->o_bd->be_update_refs
387                         ? op->o_bd->be_update_refs : default_referral;
388
389                 if( defref != NULL ) {
390                         rs->sr_ref = referral_rewrite( defref,
391                                 NULL, &op->o_req_dn, op->ors_scope );
392                         if( !rs->sr_ref) rs->sr_ref = defref;
393                         rs->sr_err = LDAP_REFERRAL;
394                         send_ldap_result( op, rs );
395
396                         if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
397
398                 } else {
399                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
400                                 "copy not used; no referral information available" );
401                 }
402
403         } else if ( op->o_bd->be_search ) {
404                 if ( limits_check( op, rs ) == 0 ) {
405                         /* actually do the search and send the result(s) */
406                         (op->o_bd->be_search)( op, rs );
407                 }
408                 /* else limits_check() sends error */
409
410         } else {
411                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
412                         "operation not supported within namingContext" );
413         }
414
415 return_results:;
416         op->o_bd = bd;
417         return rs->sr_err;
418 }
419