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 char *base = NULL, *nbase = NULL, *fstr = NULL;
37 Filter *filter = NULL;
45 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
46 "do_search: conn %d\n", conn->c_connid ));
48 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
52 * Parse the search request. It looks like this:
54 * SearchRequest := [APPLICATION 3] SEQUENCE {
55 * baseObject DistinguishedName,
61 * derefAliases ENUMERATED {
62 * neverDerefaliases (0),
63 * derefInSearching (1),
64 * derefFindingBaseObj (2),
65 * alwaysDerefAliases (3)
67 * sizelimit INTEGER (0 .. 65535),
68 * timelimit INTEGER (0 .. 65535),
71 * attributes SEQUENCE OF AttributeType
75 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
76 if ( ber_scanf( op->o_ber, "{aiiiib" /*}*/,
77 &base, &scope, &deref, &sizelimit,
78 &timelimit, &attrsonly ) == LBER_ERROR ) {
79 send_ldap_disconnect( conn, op,
80 LDAP_PROTOCOL_ERROR, "decoding error" );
81 rc = SLAPD_DISCONNECT;
87 case LDAP_SCOPE_ONELEVEL:
88 case LDAP_SCOPE_SUBTREE:
91 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
92 NULL, "invalid scope", NULL, NULL );
97 case LDAP_DEREF_NEVER:
98 case LDAP_DEREF_FINDING:
99 case LDAP_DEREF_SEARCHING:
100 case LDAP_DEREF_ALWAYS:
103 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
104 NULL, "invalid deref", NULL, NULL );
108 nbase = ch_strdup( base );
110 if( dn_normalize( nbase ) == NULL ) {
111 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
112 NULL, "invalid DN", NULL, NULL );
117 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
118 "do_search \"%s\" %d %d %d %d %d\n", base, scope,
119 deref, sizelimit, timelimit, attrsonly ));
121 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
122 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
126 /* filter - returns a "normalized" version */
127 rc = get_filter( conn, op->o_ber, &filter, &fstr, &text );
128 if( rc != LDAP_SUCCESS ) {
129 if( rc == SLAPD_DISCONNECT ) {
130 send_ldap_disconnect( conn, op,
131 LDAP_PROTOCOL_ERROR, text );
133 send_ldap_result( conn, op, rc,
134 NULL, text, NULL, NULL );
140 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
141 "do_search: conn %d filter: %s\n", conn->c_connid, fstr ));
143 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr, 0, 0 );
148 if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
149 send_ldap_disconnect( conn, op,
150 LDAP_PROTOCOL_ERROR, "decoding attrs error" );
151 rc = SLAPD_DISCONNECT;
155 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
157 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
158 "do_search: conn %d get_ctrls failed (%d)\n",
159 conn->c_connid, rc ));
161 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
170 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
171 "do_search: conn %d attrs:", conn->c_connid ));
173 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
177 if ( attrs != NULL ) {
178 for ( i = 0; attrs[i] != NULL; i++ ) {
180 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
181 "do_search: %s", attrs[i] ));
183 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
190 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS, "\n" ));
192 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
195 Statslog( LDAP_DEBUG_STATS,
196 "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
197 op->o_connid, op->o_opid, base, scope, fstr );
199 manageDSAit = get_manageDSAit( op );
201 if ( scope == LDAP_SCOPE_BASE ) {
204 if ( strcasecmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
205 #ifdef LDAP_CONNECTIONLESS
206 /* Ignore LDAPv2 CLDAP DSE queries */
207 if (op->o_protocol==LDAP_VERSION2 && conn->c_is_udp) {
211 /* check restrictions */
212 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
213 if( rc != LDAP_SUCCESS ) {
214 send_ldap_result( conn, op, rc,
215 NULL, text, NULL, NULL );
219 rc = root_dse_info( conn, &entry, &text );
222 #if defined( SLAPD_SCHEMA_DN )
223 else if ( strcasecmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
224 /* check restrictions */
225 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
226 if( rc != LDAP_SUCCESS ) {
227 send_ldap_result( conn, op, rc,
228 NULL, text, NULL, NULL );
232 rc = schema_info( &entry, &text );
236 if( rc != LDAP_SUCCESS ) {
237 send_ldap_result( conn, op, rc,
238 NULL, text, NULL, NULL );
241 } else if ( entry != NULL ) {
242 rc = test_filter( NULL, conn, op,
245 if( rc == LDAP_COMPARE_TRUE ) {
246 send_search_entry( &backends[0], conn, op,
247 entry, attrs, attrsonly, NULL );
251 send_ldap_result( conn, op, LDAP_SUCCESS,
252 NULL, NULL, NULL, NULL );
258 if( nbase[0] == '\0' && default_search_nbase != NULL ) {
261 base = ch_strdup( default_search_base );
262 nbase = ch_strdup( default_search_nbase );
266 * We could be serving multiple database backends. Select the
267 * appropriate one, or send a referral to our "referral server"
268 * if we don't hold it.
270 if ( (be = select_backend( nbase, manageDSAit, 1 )) == NULL ) {
271 struct berval **ref = referral_rewrite( default_referral,
274 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
275 NULL, NULL, ref ? ref : default_referral, NULL );
281 /* check restrictions */
282 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
283 if( rc != LDAP_SUCCESS ) {
284 send_ldap_result( conn, op, rc,
285 NULL, text, NULL, NULL );
289 /* check for referrals */
290 rc = backend_check_referrals( be, conn, op, base, nbase );
291 if ( rc != LDAP_SUCCESS ) {
295 /* deref the base if needed */
296 nbase = suffix_alias( be, nbase );
298 /* actually do the search and send the result(s) */
299 if ( be->be_search ) {
300 (*be->be_search)( be, conn, op, base, nbase, scope, deref, sizelimit,
301 timelimit, filter, fstr, attrs, attrsonly );
303 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
304 NULL, "operation not supported within namingContext", NULL, NULL );
308 if( base != NULL) free( base );
309 if( nbase != NULL) free( nbase );
310 if( fstr != NULL) free( fstr );
311 if( filter != NULL) filter_free( filter );
312 if ( attrs != NULL ) {
313 charray_free( attrs );