2 * Copyright (c) 1995 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
17 #include <ac/string.h>
18 #include <ac/socket.h>
20 #include "ldap_defaults.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, *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, NULL,
45 "SASL bind in progress" );
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" );
83 if ( scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL
84 && scope != LDAP_SCOPE_SUBTREE ) {
85 send_ldap_disconnect( conn, op,
86 LDAP_PROTOCOL_ERROR, "decoding error" );
91 (void) dn_normalize_case( base );
93 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
94 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
97 /* filter - returns a "normalized" version */
98 if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
100 send_ldap_disconnect( conn, op,
101 LDAP_PROTOCOL_ERROR, "decode error" );
103 send_ldap_result( conn, op, err, NULL, "Bad search filter" );
108 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr, 0, 0 );
111 if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
112 send_ldap_disconnect( conn, op,
113 LDAP_PROTOCOL_ERROR, "decoding error" );
118 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
119 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
125 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
127 if ( attrs != NULL ) {
128 for ( i = 0; attrs[i] != NULL; i++ ) {
129 attr_normalize( attrs[i] );
130 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
134 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
136 Statslog( LDAP_DEBUG_STATS,
137 "conn=%d op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
138 conn->c_connid, op->o_opid, base, scope, fstr );
140 #if defined( SLAPD_MONITOR_DN ) || defined( SLAPD_CONFIG_DN ) || defined( SLAPD_SCHEMA_DN )
141 if ( scope == LDAP_SCOPE_BASE ) {
142 #if defined( SLAPD_MONITOR_DN )
143 if ( strcmp( base, SLAPD_MONITOR_DN ) == 0 ) {
144 monitor_info( conn, op );
148 #if defined( SLAPD_CONFIG_DN )
149 if ( strcmp( base, SLAPD_CONFIG_DN ) == 0 ) {
150 config_info( conn, op );
154 #if defined( SLAPD_SCHEMA_DN )
155 if ( strcmp( base, SLAPD_SCHEMA_DN ) == 0 ) {
156 schema_info( conn, op, attrs, attrsonly );
161 #endif /* monitor or config or schema dn */
163 if ( strcmp( base, LDAP_ROOT_DSE ) == 0 && scope == LDAP_SCOPE_BASE ) {
164 root_dse_info( conn, op, attrs, attrsonly );
169 * We could be serving multiple database backends. Select the
170 * appropriate one, or send a referral to our "referral server"
171 * if we don't hold it.
173 if ( (be = select_backend( base )) == NULL ) {
174 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
180 /* translate the base if it matches an aliased base part */
181 base = suffixAlias ( base, op, be );
183 /* actually do the search and send the result(s) */
184 if ( be->be_search ) {
185 (*be->be_search)( be, conn, op, base, scope, deref, sizelimit,
186 timelimit, filter, fstr, attrs, attrsonly );
188 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
189 "Function not implemented" );
193 if( base != NULL) free( base );
194 if( fstr != NULL) free( fstr );
195 if( filter != NULL) filter_free( filter );
196 if ( attrs != NULL ) {
197 charray_free( attrs );