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.
15 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include "ldapconfig.h"
20 extern int get_filter();
21 extern Backend *select_backend();
22 extern char *suffixAlias();
24 extern char *default_referral;
28 Connection *conn; /* where to send results */
29 Operation *op; /* info about the op to which we're responding */
32 int scope, deref, attrsonly;
33 int sizelimit, timelimit;
39 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
42 * Parse the search request. It looks like this:
44 * SearchRequest := [APPLICATION 3] SEQUENCE {
45 * baseObject DistinguishedName,
51 * derefAliases ENUMERATED {
52 * neverDerefaliases (0),
53 * derefInSearching (1),
54 * derefFindingBaseObj (2),
55 * alwaysDerefAliases (3)
57 * sizelimit INTEGER (0 .. 65535),
58 * timelimit INTEGER (0 .. 65535),
61 * attributes SEQUENCE OF AttributeType
65 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
66 if ( ber_scanf( op->o_ber, "{aiiiib", &base, &scope, &deref, &sizelimit,
67 &timelimit, &attrsonly ) == LBER_ERROR ) {
68 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
71 if ( scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL
72 && scope != LDAP_SCOPE_SUBTREE ) {
74 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
75 "Unknown search scope" );
78 (void) dn_normalize( base );
80 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
81 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
84 /* filter - returns a "normalized" version */
87 if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
92 send_ldap_result( conn, op, err, NULL, "Bad search filter" );
95 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr, 0, 0 );
99 if ( ber_scanf( op->o_ber, "{v}}", &attrs ) == LBER_ERROR ) {
102 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
105 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
106 if ( attrs != NULL ) {
107 for ( i = 0; attrs[i] != NULL; i++ ) {
108 attr_normalize( attrs[i] );
109 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
112 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
114 Statslog( LDAP_DEBUG_STATS,
115 "conn=%d op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
116 conn->c_connid, op->o_opid, base, scope, fstr );
118 #if defined( SLAPD_MONITOR_DN ) || defined( SLAPD_CONFIG_DN ) || defined( SLAPD_SCHEMA_DN )
119 if ( scope == LDAP_SCOPE_BASE ) {
120 #if defined( SLAPD_MONITOR_DN )
121 if ( strcasecmp( base, SLAPD_MONITOR_DN ) == 0 ) {
124 monitor_info( conn, op );
128 #if defined( SLAPD_CONFIG_DN )
129 if ( strcasecmp( base, SLAPD_CONFIG_DN ) == 0 ) {
132 config_info( conn, op );
136 #if defined( SLAPD_SCHEMA_DN )
137 if ( strcasecmp( base, SLAPD_SCHEMA_DN ) == 0 ) {
140 schema_info( conn, op );
145 #endif /* monitor or config or schema dn */
148 * We could be serving multiple database backends. Select the
149 * appropriate one, or send a referral to our "referral server"
150 * if we don't hold it.
152 if ( (be = select_backend( base )) == NULL ) {
153 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
158 filter_free( filter );
159 if ( attrs != NULL ) {
160 charray_free( attrs );
165 /* translate the base if it matches an aliased base part */
166 base = suffixAlias ( base, op, be );
168 /* actually do the search and send the result(s) */
169 if ( be->be_search != NULL ) {
170 (*be->be_search)( be, conn, op, base, scope, deref, sizelimit,
171 timelimit, filter, fstr, attrs, attrsonly );
173 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
174 "Function not implemented" );
179 filter_free( filter );
180 if ( attrs != NULL ) {
181 charray_free( attrs );