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" );
84 rc = SLAPD_DISCONNECT;
90 case LDAP_SCOPE_ONELEVEL:
91 case LDAP_SCOPE_SUBTREE:
94 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
95 NULL, "invalid scope", NULL, NULL );
100 case LDAP_DEREF_NEVER:
101 case LDAP_DEREF_FINDING:
102 case LDAP_DEREF_SEARCHING:
103 case LDAP_DEREF_ALWAYS:
106 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
107 NULL, "invalid deref", NULL, NULL );
111 nbase = ch_strdup( base );
113 if( dn_normalize( nbase ) == NULL ) {
114 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
115 NULL, "invalid DN", NULL, NULL );
119 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
120 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
123 /* filter - returns a "normalized" version */
124 if ( (rc = get_filter( conn, op->o_ber, &filter, &fstr )) != LDAP_SUCCESS ) {
125 if( rc == SLAPD_DISCONNECT ) {
126 send_ldap_disconnect( conn, op,
127 LDAP_PROTOCOL_ERROR, "decode filter error" );
129 send_ldap_result( conn, op, rc,
130 NULL, "bad search filter", NULL, NULL );
135 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr, 0, 0 );
138 if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
139 send_ldap_disconnect( conn, op,
140 LDAP_PROTOCOL_ERROR, "decoding attrs error" );
141 rc = SLAPD_DISCONNECT;
145 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
146 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
152 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
154 if ( attrs != NULL ) {
155 for ( i = 0; attrs[i] != NULL; i++ ) {
156 #ifndef SLAPD_SCHEMA_NOT_COMPAT
157 attr_normalize( attrs[i] );
159 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
163 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
165 Statslog( LDAP_DEBUG_STATS,
166 "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
167 op->o_connid, op->o_opid, base, scope, fstr );
169 if ( scope == LDAP_SCOPE_BASE ) {
170 #if defined( SLAPD_MONITOR_DN )
171 if ( strcmp( nbase, SLAPD_MONITOR_DN ) == 0 ) {
172 monitor_info( conn, op, attrs, attrsonly );
177 #if defined( SLAPD_CONFIG_DN )
178 if ( strcmp( nbase, SLAPD_CONFIG_DN ) == 0 ) {
179 config_info( conn, op, attrs, attrsonly );
184 #if defined( SLAPD_SCHEMA_DN )
185 if ( strcmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
186 schema_info( conn, op, attrs, attrsonly );
191 if ( strcmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
192 root_dse_info( conn, op, attrs, attrsonly );
198 * We could be serving multiple database backends. Select the
199 * appropriate one, or send a referral to our "referral server"
200 * if we don't hold it.
202 if ( (be = select_backend( nbase )) == NULL ) {
203 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
204 NULL, NULL, default_referral, NULL );
209 /* make sure this backend recongizes critical controls */
210 rc = backend_check_controls( be, conn, op ) ;
212 if( rc != LDAP_SUCCESS ) {
213 send_ldap_result( conn, op, rc,
214 NULL, NULL, NULL, NULL );
218 /* deref the base if needed */
219 nbase = suffix_alias( be, nbase );
221 /* actually do the search and send the result(s) */
222 if ( be->be_search ) {
223 (*be->be_search)( be, conn, op, base, nbase, scope, deref, sizelimit,
224 timelimit, filter, fstr, attrs, attrsonly );
226 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
227 NULL, "Function not implemented", NULL, NULL );
231 if( base != NULL) free( base );
232 if( nbase != NULL) free( nbase );
233 if( fstr != NULL) free( fstr );
234 if( filter != NULL) filter_free( filter );
235 if ( attrs != NULL ) {
236 charray_free( attrs );