3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
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.
22 #include <ac/string.h>
23 #include <ac/socket.h>
30 Connection *conn, /* where to send results */
31 Operation *op /* info about the op to which we're responding */
34 ber_int_t scope, deref, attrsonly;
35 ber_int_t sizelimit, timelimit;
36 struct berval base = { 0, NULL };
37 char *nbase = NULL, *fstr = NULL;
38 Filter *filter = NULL;
46 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
47 "do_search: conn %d\n", conn->c_connid ));
49 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
53 * Parse the search request. It looks like this:
55 * SearchRequest := [APPLICATION 3] SEQUENCE {
56 * baseObject DistinguishedName,
62 * derefAliases ENUMERATED {
63 * neverDerefaliases (0),
64 * derefInSearching (1),
65 * derefFindingBaseObj (2),
66 * alwaysDerefAliases (3)
68 * sizelimit INTEGER (0 .. 65535),
69 * timelimit INTEGER (0 .. 65535),
72 * attributes SEQUENCE OF AttributeType
76 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
77 if ( ber_scanf( op->o_ber, "{oiiiib" /*}*/,
78 &base, &scope, &deref, &sizelimit,
79 &timelimit, &attrsonly ) == LBER_ERROR )
81 send_ldap_disconnect( conn, op,
82 LDAP_PROTOCOL_ERROR, "decoding error" );
83 rc = SLAPD_DISCONNECT;
89 case LDAP_SCOPE_ONELEVEL:
90 case LDAP_SCOPE_SUBTREE:
93 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
94 NULL, "invalid scope", NULL, NULL );
99 case LDAP_DEREF_NEVER:
100 case LDAP_DEREF_FINDING:
101 case LDAP_DEREF_SEARCHING:
102 case LDAP_DEREF_ALWAYS:
105 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
106 NULL, "invalid deref", NULL, NULL );
110 nbase = ch_strdup( base.bv_val );
112 if( dn_normalize( nbase ) == NULL ) {
113 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
114 NULL, "invalid DN", NULL, NULL );
119 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
120 "do_search \"%s\" %d %d %d %d %d\n", base.bv_val, scope,
121 deref, sizelimit, timelimit, attrsonly ));
123 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base.bv_val, scope, deref );
124 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
128 /* filter - returns a "normalized" version */
129 rc = get_filter( conn, op->o_ber, &filter, &fstr, &text );
130 if( rc != LDAP_SUCCESS ) {
131 if( rc == SLAPD_DISCONNECT ) {
132 send_ldap_disconnect( conn, op,
133 LDAP_PROTOCOL_ERROR, text );
135 send_ldap_result( conn, op, rc,
136 NULL, text, NULL, NULL );
142 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
143 "do_search: conn %d filter: %s\n", conn->c_connid, fstr ));
145 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr, 0, 0 );
150 if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
151 send_ldap_disconnect( conn, op,
152 LDAP_PROTOCOL_ERROR, "decoding attrs error" );
153 rc = SLAPD_DISCONNECT;
157 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
159 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
160 "do_search: conn %d get_ctrls failed (%d)\n",
161 conn->c_connid, rc ));
163 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
172 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
173 "do_search: conn %d attrs:", conn->c_connid ));
175 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
179 if ( attrs != NULL ) {
180 for ( i = 0; attrs[i] != NULL; i++ ) {
182 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
183 "do_search: %s", attrs[i] ));
185 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
192 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS, "\n" ));
194 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
197 Statslog( LDAP_DEBUG_STATS,
198 "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
199 op->o_connid, op->o_opid, base.bv_val, scope, fstr );
201 manageDSAit = get_manageDSAit( op );
203 if ( scope == LDAP_SCOPE_BASE ) {
206 if ( strcasecmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
207 #ifdef LDAP_CONNECTIONLESS
208 /* Ignore LDAPv2 CLDAP DSE queries */
209 if (op->o_protocol==LDAP_VERSION2 && conn->c_is_udp) {
213 /* check restrictions */
214 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
215 if( rc != LDAP_SUCCESS ) {
216 send_ldap_result( conn, op, rc,
217 NULL, text, NULL, NULL );
221 rc = root_dse_info( conn, &entry, &text );
224 #if defined( SLAPD_SCHEMA_DN )
225 else if ( strcasecmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
226 /* check restrictions */
227 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
228 if( rc != LDAP_SUCCESS ) {
229 send_ldap_result( conn, op, rc,
230 NULL, text, NULL, NULL );
234 rc = schema_info( &entry, &text );
238 if( rc != LDAP_SUCCESS ) {
239 send_ldap_result( conn, op, rc,
240 NULL, text, NULL, NULL );
243 } else if ( entry != NULL ) {
244 rc = test_filter( NULL, conn, op,
247 if( rc == LDAP_COMPARE_TRUE ) {
248 send_search_entry( NULL, conn, op,
249 entry, attrs, attrsonly, NULL );
253 send_ldap_result( conn, op, LDAP_SUCCESS,
254 NULL, NULL, NULL, NULL );
260 if( nbase[0] == '\0' && default_search_nbase != NULL ) {
261 ch_free( base.bv_val );
263 base.bv_val = ch_strdup( default_search_base );
264 base.bv_len = strlen( default_search_nbase );
265 nbase = ch_strdup( default_search_nbase );
269 * We could be serving multiple database backends. Select the
270 * appropriate one, or send a referral to our "referral server"
271 * if we don't hold it.
273 if ( (be = select_backend( nbase, manageDSAit, 1 )) == NULL ) {
274 struct berval **ref = referral_rewrite( default_referral,
275 NULL, base.bv_val, scope );
277 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
278 NULL, NULL, ref ? ref : default_referral, NULL );
284 /* check restrictions */
285 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
286 if( rc != LDAP_SUCCESS ) {
287 send_ldap_result( conn, op, rc,
288 NULL, text, NULL, NULL );
292 /* check for referrals */
293 rc = backend_check_referrals( be, conn, op, base.bv_val, nbase );
294 if ( rc != LDAP_SUCCESS ) {
298 /* deref the base if needed */
299 nbase = suffix_alias( be, nbase );
301 /* actually do the search and send the result(s) */
302 if ( be->be_search ) {
303 (*be->be_search)( be, conn, op, base.bv_val, nbase,
304 scope, deref, sizelimit,
305 timelimit, filter, fstr, attrs, attrsonly );
307 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
308 NULL, "operation not supported within namingContext", NULL, NULL );
313 if( nbase != NULL) free( nbase );
314 if( fstr != NULL) free( fstr );
315 if( filter != NULL) filter_free( filter );
316 if ( attrs != NULL ) {
317 charray_free( attrs );