3 * Copyright (c) 1995 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/string.h>
19 #include <ac/socket.h>
26 Connection *conn, /* where to send results */
27 Operation *op /* info about the op to which we're responding */
31 ber_int_t scope, deref, attrsonly;
32 ber_int_t sizelimit, timelimit;
33 char *base = NULL, *nbase = NULL, *fstr = NULL;
34 Filter *filter = NULL;
39 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
41 if( op->o_bind_in_progress ) {
42 Debug( LDAP_DEBUG_ANY, "do_search: SASL bind in progress.\n",
44 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
45 NULL, "SASL bind in progress", NULL, NULL );
46 return LDAP_SASL_BIND_IN_PROGRESS;
50 * Parse the search request. It looks like this:
52 * SearchRequest := [APPLICATION 3] SEQUENCE {
53 * baseObject DistinguishedName,
59 * derefAliases ENUMERATED {
60 * neverDerefaliases (0),
61 * derefInSearching (1),
62 * derefFindingBaseObj (2),
63 * alwaysDerefAliases (3)
65 * sizelimit INTEGER (0 .. 65535),
66 * timelimit INTEGER (0 .. 65535),
69 * attributes SEQUENCE OF AttributeType
73 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
74 if ( ber_scanf( op->o_ber, "{aiiiib",
75 &base, &scope, &deref, &sizelimit,
76 &timelimit, &attrsonly ) == LBER_ERROR ) {
77 send_ldap_disconnect( conn, op,
78 LDAP_PROTOCOL_ERROR, "decoding error" );
85 case LDAP_SCOPE_ONELEVEL:
86 case LDAP_SCOPE_SUBTREE:
89 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
90 NULL, "invalid scope", NULL, NULL );
96 case LDAP_DEREF_NEVER:
97 case LDAP_DEREF_FINDING:
98 case LDAP_DEREF_SEARCHING:
99 case LDAP_DEREF_ALWAYS:
102 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
103 NULL, "invalid deref", NULL, NULL );
108 nbase = ch_strdup( base );
110 if( dn_normalize( nbase ) == NULL ) {
111 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX,
112 NULL, "invalid DN", NULL, NULL );
117 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
118 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
121 /* filter - returns a "normalized" version */
122 if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
124 send_ldap_disconnect( conn, op,
125 LDAP_PROTOCOL_ERROR, "decode error" );
127 send_ldap_result( conn, op, err,
128 NULL, "Bad search filter", NULL, NULL );
134 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr, 0, 0 );
137 if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
138 send_ldap_disconnect( conn, op,
139 LDAP_PROTOCOL_ERROR, "decoding error" );
144 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
145 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
151 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
153 if ( attrs != NULL ) {
154 for ( i = 0; attrs[i] != NULL; i++ ) {
155 attr_normalize( attrs[i] );
156 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
160 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
162 Statslog( LDAP_DEBUG_STATS,
163 "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
164 op->o_connid, op->o_opid, base, scope, fstr );
166 if ( scope == LDAP_SCOPE_BASE ) {
167 #if defined( SLAPD_MONITOR_DN )
168 if ( strcmp( nbase, SLAPD_MONITOR_DN ) == 0 ) {
169 monitor_info( conn, op, attrs, attrsonly );
174 #if defined( SLAPD_CONFIG_DN )
175 if ( strcmp( nbase, SLAPD_CONFIG_DN ) == 0 ) {
176 config_info( conn, op, attrs, attrsonly );
181 #if defined( SLAPD_SCHEMA_DN )
182 if ( strcmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
183 schema_info( conn, op, attrs, attrsonly );
188 if ( strcmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
189 root_dse_info( conn, op, attrs, attrsonly );
195 * We could be serving multiple database backends. Select the
196 * appropriate one, or send a referral to our "referral server"
197 * if we don't hold it.
199 if ( (be = select_backend( nbase )) == NULL ) {
200 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
201 NULL, NULL, default_referral, NULL );
206 /* deref the base if needed */
207 nbase = suffix_alias( be, nbase );
209 /* actually do the search and send the result(s) */
210 if ( be->be_search ) {
211 (*be->be_search)( be, conn, op, base, nbase, scope, deref, sizelimit,
212 timelimit, filter, fstr, attrs, attrsonly );
214 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
215 NULL, "Function not implemented", NULL, NULL );
219 if( base != NULL) free( base );
220 if( nbase != NULL) free( nbase );
221 if( fstr != NULL) free( fstr );
222 if( filter != NULL) filter_free( filter );
223 if ( attrs != NULL ) {
224 charray_free( attrs );