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 "ldapconfig.h"
26 Connection *conn, /* where to send results */
27 Operation *op /* info about the op to which we're responding */
31 int scope, deref, attrsonly;
32 int sizelimit, timelimit;
33 char *base = NULL, *fstr = NULL;
34 Filter *filter = NULL;
38 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
41 * Parse the search request. It looks like this:
43 * SearchRequest := [APPLICATION 3] SEQUENCE {
44 * baseObject DistinguishedName,
50 * derefAliases ENUMERATED {
51 * neverDerefaliases (0),
52 * derefInSearching (1),
53 * derefFindingBaseObj (2),
54 * alwaysDerefAliases (3)
56 * sizelimit INTEGER (0 .. 65535),
57 * timelimit INTEGER (0 .. 65535),
60 * attributes SEQUENCE OF AttributeType
64 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
65 if ( ber_scanf( op->o_ber, "{aiiiib", &base, &scope, &deref, &sizelimit,
66 &timelimit, &attrsonly ) == LBER_ERROR ) {
67 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
70 if ( scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL
71 && scope != LDAP_SCOPE_SUBTREE ) {
72 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
73 "Unknown search scope" );
76 (void) dn_normalize( base );
78 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
79 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", sizelimit, timelimit,
82 /* filter - returns a "normalized" version */
83 if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
84 send_ldap_result( conn, op, err, NULL, "Bad search filter" );
87 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", fstr, 0, 0 );
90 if ( ber_scanf( op->o_ber, "{v}}", &attrs ) == LBER_ERROR ) {
91 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
95 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
96 if ( attrs != NULL ) {
97 for ( i = 0; attrs[i] != NULL; i++ ) {
98 attr_normalize( attrs[i] );
99 Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
102 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
104 Statslog( LDAP_DEBUG_STATS,
105 "conn=%d op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
106 conn->c_connid, op->o_opid, base, scope, fstr );
108 #if defined( SLAPD_MONITOR_DN ) || defined( SLAPD_CONFIG_DN ) || defined( SLAPD_SCHEMA_DN )
109 if ( scope == LDAP_SCOPE_BASE ) {
110 #if defined( SLAPD_MONITOR_DN )
111 if ( strcasecmp( base, SLAPD_MONITOR_DN ) == 0 ) {
112 monitor_info( conn, op );
116 #if defined( SLAPD_CONFIG_DN )
117 if ( strcasecmp( base, SLAPD_CONFIG_DN ) == 0 ) {
118 config_info( conn, op );
122 #if defined( SLAPD_SCHEMA_DN )
123 if ( strcasecmp( base, SLAPD_SCHEMA_DN ) == 0 ) {
124 schema_info( conn, op );
129 #endif /* monitor or config or schema dn */
132 * We could be serving multiple database backends. Select the
133 * appropriate one, or send a referral to our "referral server"
134 * if we don't hold it.
136 if ( (be = select_backend( base )) == NULL ) {
137 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
143 /* translate the base if it matches an aliased base part */
144 base = suffixAlias ( base, op, be );
146 /* actually do the search and send the result(s) */
147 if ( be->be_search != NULL ) {
148 (*be->be_search)( be, conn, op, base, scope, deref, sizelimit,
149 timelimit, filter, fstr, attrs, attrsonly );
151 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
152 "Function not implemented" );
156 if( base != NULL) free( base );
157 if( fstr != NULL) free( fstr );
158 if( filter != NULL) filter_free( filter );
159 if ( attrs != NULL ) {
160 charray_free( attrs );