3 * Copyright 1998-1999 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>
31 Connection *conn, /* where to send results */
32 Operation *op /* info about the op to which we're responding */
36 ber_int_t scope, deref, attrsonly;
37 ber_int_t sizelimit, timelimit;
38 char *base = NULL, *nbase = NULL, *fstr = NULL;
39 Filter *filter = NULL;
44 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
46 if( op->o_bind_in_progress ) {
47 Debug( LDAP_DEBUG_ANY, "do_search: SASL bind in progress.\n",
49 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
50 NULL, "SASL bind in progress", NULL, NULL );
51 return LDAP_SASL_BIND_IN_PROGRESS;
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, "{aiiiib",
80 &base, &scope, &deref, &sizelimit,
81 &timelimit, &attrsonly ) == LBER_ERROR ) {
82 send_ldap_disconnect( conn, op,
83 LDAP_PROTOCOL_ERROR, "decoding error" );
90 case LDAP_SCOPE_ONELEVEL:
91 case LDAP_SCOPE_SUBTREE:
94 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
95 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, LDAP_PROTOCOL_ERROR,
108 NULL, "invalid deref", NULL, NULL );
113 nbase = ch_strdup( base );
115 if( dn_normalize( nbase ) == NULL ) {
116 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX,
117 NULL, "invalid DN", NULL, NULL );
122 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
123 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
126 /* filter - returns a "normalized" version */
127 if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
129 send_ldap_disconnect( conn, op,
130 LDAP_PROTOCOL_ERROR, "decode error" );
132 send_ldap_result( conn, op, err,
133 NULL, "Bad search filter", NULL, NULL );
139 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr, 0, 0 );
142 if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
143 send_ldap_disconnect( conn, op,
144 LDAP_PROTOCOL_ERROR, "decoding error" );
149 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
150 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
156 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
158 if ( attrs != NULL ) {
159 for ( i = 0; attrs[i] != NULL; i++ ) {
160 #ifndef SLAPD_SCHEMA_NOT_COMPAT
161 attr_normalize( attrs[i] );
163 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
167 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
169 Statslog( LDAP_DEBUG_STATS,
170 "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
171 op->o_connid, op->o_opid, base, scope, fstr );
173 if ( scope == LDAP_SCOPE_BASE ) {
174 #if defined( SLAPD_MONITOR_DN )
175 if ( strcmp( nbase, SLAPD_MONITOR_DN ) == 0 ) {
176 monitor_info( conn, op, attrs, attrsonly );
181 #if defined( SLAPD_CONFIG_DN )
182 if ( strcmp( nbase, SLAPD_CONFIG_DN ) == 0 ) {
183 config_info( conn, op, attrs, attrsonly );
188 #if defined( SLAPD_SCHEMA_DN )
189 if ( strcmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
190 schema_info( conn, op, attrs, attrsonly );
195 if ( strcmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
196 root_dse_info( conn, op, attrs, attrsonly );
202 * We could be serving multiple database backends. Select the
203 * appropriate one, or send a referral to our "referral server"
204 * if we don't hold it.
206 if ( (be = select_backend( nbase )) == NULL ) {
207 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
208 NULL, NULL, default_referral, NULL );
213 /* make sure this backend recongizes critical controls */
214 rc = backend_check_controls( be, conn, op ) ;
216 if( rc != LDAP_SUCCESS ) {
217 send_ldap_result( conn, op, rc,
218 NULL, NULL, NULL, NULL );
222 /* deref the base if needed */
223 nbase = suffix_alias( be, nbase );
225 /* actually do the search and send the result(s) */
226 if ( be->be_search ) {
227 (*be->be_search)( be, conn, op, base, nbase, scope, deref, sizelimit,
228 timelimit, filter, fstr, attrs, attrsonly );
230 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
231 NULL, "Function not implemented", NULL, NULL );
235 if( base != NULL) free( base );
236 if( nbase != NULL) free( nbase );
237 if( fstr != NULL) free( fstr );
238 if( filter != NULL) filter_free( filter );
239 if ( attrs != NULL ) {
240 charray_free( attrs );