]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
25f73163791911241c414609bfc501b8764c6ba0
[openldap] / servers / slapd / search.c
1 /*
2  * Copyright (c) 1995 Regents of the University of Michigan.
3  * All rights reserved.
4  *
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.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/string.h>
18 #include <ac/socket.h>
19
20 #include "ldap_defaults.h"
21 #include "slap.h"
22
23
24 int
25 do_search(
26     Connection  *conn,  /* where to send results                       */
27     Operation   *op     /* info about the op to which we're responding */
28 )
29 {
30         int             i, err;
31         ber_int_t               scope, deref, attrsonly;
32         ber_int_t               sizelimit, timelimit;
33         char            *base = NULL, *fstr = NULL;
34         Filter          *filter = NULL;
35         char            **attrs = NULL;
36         Backend         *be;
37         int                     rc;
38
39         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
40
41         if( op->o_bind_in_progress ) {
42                 Debug( LDAP_DEBUG_ANY, "do_search: SASL bind in progress.\n",
43                         0, 0, 0 );
44                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
45                     "SASL bind in progress" );
46                 return LDAP_SASL_BIND_IN_PROGRESS;
47         }
48
49         /*
50          * Parse the search request.  It looks like this:
51          *
52          *      SearchRequest := [APPLICATION 3] SEQUENCE {
53          *              baseObject      DistinguishedName,
54          *              scope           ENUMERATED {
55          *                      baseObject      (0),
56          *                      singleLevel     (1),
57          *                      wholeSubtree    (2)
58          *              },
59          *              derefAliases    ENUMERATED {
60          *                      neverDerefaliases       (0),
61          *                      derefInSearching        (1),
62          *                      derefFindingBaseObj     (2),
63          *                      alwaysDerefAliases      (3)
64          *              },
65          *              sizelimit       INTEGER (0 .. 65535),
66          *              timelimit       INTEGER (0 .. 65535),
67          *              attrsOnly       BOOLEAN,
68          *              filter          Filter,
69          *              attributes      SEQUENCE OF AttributeType
70          *      }
71          */
72
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_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL, "" );
78                 goto return_results;
79         }
80
81         if ( scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL
82             && scope != LDAP_SCOPE_SUBTREE ) {
83                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
84                     "Unknown search scope" );
85                 goto return_results;
86         }
87
88         (void) dn_normalize_case( base );
89
90         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
91         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
92             attrsonly);
93
94         /* filter - returns a "normalized" version */
95         if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
96                 send_ldap_result( conn, op, err, NULL, "Bad search filter" );
97                 goto return_results;
98         }
99         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr, 0, 0 );
100
101         /* attributes */
102         if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
103                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL, "" );
104                 goto return_results;
105         }
106
107 #ifdef GET_CTRLS
108         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
109                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
110                 goto return_results;
111         } 
112 #endif
113
114         rc = 0;
115
116         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
117
118         if ( attrs != NULL ) {
119                 for ( i = 0; attrs[i] != NULL; i++ ) {
120                         attr_normalize( attrs[i] );
121                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
122                 }
123         }
124
125         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
126
127         Statslog( LDAP_DEBUG_STATS,
128             "conn=%d op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
129             conn->c_connid, op->o_opid, base, scope, fstr );
130
131 #if defined( SLAPD_MONITOR_DN ) || defined( SLAPD_CONFIG_DN ) || defined( SLAPD_SCHEMA_DN )
132         if ( scope == LDAP_SCOPE_BASE ) {
133 #if defined( SLAPD_MONITOR_DN )
134                 if ( strcmp( base, SLAPD_MONITOR_DN ) == 0 ) {
135                         monitor_info( conn, op );
136                         goto return_results;
137                 }
138 #endif
139 #if defined( SLAPD_CONFIG_DN )
140                 if ( strcmp( base, SLAPD_CONFIG_DN ) == 0 ) {
141                         config_info( conn, op );
142                         goto return_results;
143                 }
144 #endif
145 #if defined( SLAPD_SCHEMA_DN )
146                 if ( strcmp( base, SLAPD_SCHEMA_DN ) == 0 ) {
147                         schema_info( conn, op, attrs, attrsonly );
148                         goto return_results;
149                 }
150 #endif
151         }
152 #endif /* monitor or config or schema dn */
153
154         if ( strcmp( base, LDAP_ROOT_DSE ) == 0 && scope == LDAP_SCOPE_BASE ) {
155                 root_dse_info( conn, op, attrs, attrsonly );
156                 goto return_results;
157         }
158
159         /*
160          * We could be serving multiple database backends.  Select the
161          * appropriate one, or send a referral to our "referral server"
162          * if we don't hold it.
163          */
164         if ( (be = select_backend( base )) == NULL ) {
165                 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
166                     default_referral );
167
168                 goto return_results;
169         }
170
171         /* translate the base if it matches an aliased base part */
172         base = suffixAlias ( base, op, be );
173
174         /* actually do the search and send the result(s) */
175         if ( be->be_search ) {
176                 (*be->be_search)( be, conn, op, base, scope, deref, sizelimit,
177                     timelimit, filter, fstr, attrs, attrsonly );
178         } else {
179                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
180                     "Function not implemented" );
181         }
182
183 return_results:;
184         if( base != NULL) free( base );
185         if( fstr != NULL) free( fstr );
186         if( filter != NULL) filter_free( filter );
187         if ( attrs != NULL ) {
188                 charray_free( attrs );
189         }
190
191         return rc;
192 }