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 struct berval pbase = { 0, NULL };
38 struct berval nbase = { 0, NULL };
39 struct berval fstr = { 0, NULL };
40 Filter *filter = NULL;
41 struct berval **attrs = NULL;
48 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
49 "do_search: conn %d\n", conn->c_connid ));
51 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
55 * Parse the search request. It looks like this:
57 * SearchRequest := [APPLICATION 3] SEQUENCE {
58 * baseObject DistinguishedName,
64 * derefAliases ENUMERATED {
65 * neverDerefaliases (0),
66 * derefInSearching (1),
67 * derefFindingBaseObj (2),
68 * alwaysDerefAliases (3)
70 * sizelimit INTEGER (0 .. 65535),
71 * timelimit INTEGER (0 .. 65535),
74 * attributes SEQUENCE OF AttributeType
78 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
79 if ( ber_scanf( op->o_ber, "{oiiiib" /*}*/,
80 &base, &scope, &deref, &sizelimit,
81 &timelimit, &attrsonly ) == LBER_ERROR )
83 send_ldap_disconnect( conn, op,
84 LDAP_PROTOCOL_ERROR, "decoding error" );
85 rc = SLAPD_DISCONNECT;
91 case LDAP_SCOPE_ONELEVEL:
92 case LDAP_SCOPE_SUBTREE:
95 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
96 NULL, "invalid scope", NULL, NULL );
101 case LDAP_DEREF_NEVER:
102 case LDAP_DEREF_FINDING:
103 case LDAP_DEREF_SEARCHING:
104 case LDAP_DEREF_ALWAYS:
107 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
108 NULL, "invalid deref", NULL, NULL );
112 rc = dnPrettyNormal( NULL, &base, &pbase, &nbase );
113 if( rc != LDAP_SUCCESS ) {
115 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
116 "do_search: conn %d invalid dn (%s)\n",
117 conn->c_connid, base.bv_val ));
119 Debug( LDAP_DEBUG_ANY,
120 "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
122 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
123 "invalid DN", NULL, NULL );
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 ));
132 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base.bv_val, scope, deref );
133 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
137 /* filter - returns a "normalized" version */
138 rc = get_filter( conn, op->o_ber, &filter, &fstr, &text );
139 if( rc != LDAP_SUCCESS ) {
140 if( rc == SLAPD_DISCONNECT ) {
141 send_ldap_disconnect( conn, op,
142 LDAP_PROTOCOL_ERROR, text );
144 send_ldap_result( conn, op, rc,
145 NULL, text, NULL, NULL );
151 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
152 "do_search: conn %d filter: %s\n", conn->c_connid, fstr.bv_val ));
154 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr.bv_val, 0, 0 );
159 if ( ber_scanf( op->o_ber, /*{*/ "{V}}", &attrs ) == LBER_ERROR ) {
160 send_ldap_disconnect( conn, op,
161 LDAP_PROTOCOL_ERROR, "decoding attrs error" );
162 rc = SLAPD_DISCONNECT;
166 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
168 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
169 "do_search: conn %d get_ctrls failed (%d)\n",
170 conn->c_connid, rc ));
172 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
181 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
182 "do_search: conn %d attrs:", conn->c_connid ));
184 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
188 if ( attrs != NULL ) {
189 for ( i = 0; attrs[i] != NULL; i++ ) {
191 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
192 "do_search: %s", attrs[i]->bv_val ));
194 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i]->bv_val, 0, 0 );
201 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS, "\n" ));
203 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
206 Statslog( LDAP_DEBUG_STATS,
207 "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
208 op->o_connid, op->o_opid, pbase.bv_val, scope, fstr.bv_val );
210 manageDSAit = get_manageDSAit( op );
212 if ( scope == LDAP_SCOPE_BASE ) {
215 if ( strcasecmp( nbase.bv_val, LDAP_ROOT_DSE ) == 0 ) {
216 #ifdef LDAP_CONNECTIONLESS
217 /* Ignore LDAPv2 CLDAP DSE queries */
218 if (op->o_protocol==LDAP_VERSION2 && conn->c_is_udp) {
222 /* check restrictions */
223 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
224 if( rc != LDAP_SUCCESS ) {
225 send_ldap_result( conn, op, rc,
226 NULL, text, NULL, NULL );
230 rc = root_dse_info( conn, &entry, &text );
233 #if defined( SLAPD_SCHEMA_DN )
234 else if ( strcasecmp( nbase.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
235 /* check restrictions */
236 rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
237 if( rc != LDAP_SUCCESS ) {
238 send_ldap_result( conn, op, rc,
239 NULL, text, NULL, NULL );
243 rc = schema_info( &entry, &text );
247 if( rc != LDAP_SUCCESS ) {
248 send_ldap_result( conn, op, rc,
249 NULL, text, NULL, NULL );
252 } else if ( entry != NULL ) {
253 rc = test_filter( NULL, conn, op,
256 if( rc == LDAP_COMPARE_TRUE ) {
257 send_search_entry( NULL, conn, op,
258 entry, attrs, attrsonly, NULL );
262 send_ldap_result( conn, op, LDAP_SUCCESS,
263 NULL, NULL, NULL, NULL );
269 if( !nbase.bv_len && default_search_nbase.bv_len ) {
270 ch_free( pbase.bv_val );
271 ch_free( nbase.bv_val );
273 ber_dupbv( &pbase, &default_search_base );
274 ber_dupbv( &nbase, &default_search_nbase );
278 * We could be serving multiple database backends. Select the
279 * appropriate one, or send a referral to our "referral server"
280 * if we don't hold it.
282 if ( (be = select_backend( &nbase, manageDSAit, 1 )) == NULL ) {
283 struct berval **ref = referral_rewrite( default_referral,
284 NULL, &pbase, scope );
286 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
287 NULL, NULL, ref ? ref : default_referral, NULL );
293 /* check restrictions */
294 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
295 if( rc != LDAP_SUCCESS ) {
296 send_ldap_result( conn, op, rc,
297 NULL, text, NULL, NULL );
301 /* check for referrals */
302 rc = backend_check_referrals( be, conn, op, &pbase, &nbase );
303 if ( rc != LDAP_SUCCESS ) {
307 /* deref the base if needed */
308 suffix_alias( be, &nbase );
310 /* actually do the search and send the result(s) */
311 if ( be->be_search ) {
312 (*be->be_search)( be, conn, op, &pbase, &nbase,
313 scope, deref, sizelimit,
314 timelimit, filter, fstr.bv_val, attrs, attrsonly );
316 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
317 NULL, "operation not supported within namingContext", NULL, NULL );
322 if( pbase.bv_val != NULL) free( pbase.bv_val );
323 if( nbase.bv_val != NULL) free( nbase.bv_val );
325 if( fstr.bv_val != NULL) free( fstr.bv_val );
326 if( filter != NULL) filter_free( filter );
327 if ( attrs != NULL ) {
328 ber_bvecfree( attrs );