]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / servers / slapd / search.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1995 Regents of the University of Michigan.
4  * All rights reserved.
5  *
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.
12  */
13
14 #include "portable.h"
15
16 #include <stdio.h>
17
18 #include <ac/string.h>
19 #include <ac/socket.h>
20
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,
45                         NULL, "SASL bind in progress", NULL, NULL );
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_disconnect( conn, op,
78                         LDAP_PROTOCOL_ERROR, "decoding error" );
79                 rc = -1;
80                 goto return_results;
81         }
82
83         switch( scope ) {
84         case LDAP_SCOPE_BASE:
85         case LDAP_SCOPE_ONELEVEL:
86         case LDAP_SCOPE_SUBTREE:
87                 break;
88         default:
89                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
90                         NULL, "invalid scope", NULL, NULL );
91                 rc = -1;
92                 goto return_results;
93         }
94
95         switch( deref ) {
96         case LDAP_DEREF_NEVER:
97         case LDAP_DEREF_FINDING:
98         case LDAP_DEREF_SEARCHING:
99         case LDAP_DEREF_ALWAYS:
100                 break;
101         default:
102                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
103                         NULL, "invalid deref", NULL, NULL );
104                 rc = -1;
105                 goto return_results;
106         }
107
108         if( dn_normalize_case( base ) == NULL ) {
109                 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX,
110                         NULL, "invalid DN", NULL, NULL );
111                 rc = -1;
112                 goto return_results;
113         }
114
115         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
116         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
117             attrsonly);
118
119         /* filter - returns a "normalized" version */
120         if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
121                 if( err == -1 ) {
122                         send_ldap_disconnect( conn, op,
123                                 LDAP_PROTOCOL_ERROR, "decode error" );
124                 } else {
125                         send_ldap_result( conn, op, err,
126                                 NULL, "Bad search filter", NULL, NULL );
127                 }
128                 rc = -1;
129                 goto return_results;
130         }
131
132         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr, 0, 0 );
133
134         /* attributes */
135         if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
136                 send_ldap_disconnect( conn, op,
137                         LDAP_PROTOCOL_ERROR, "decoding error" );
138                 rc = -1;
139                 goto return_results;
140         }
141
142         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
143                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
144                 goto return_results;
145         } 
146
147         rc = 0;
148
149         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
150
151         if ( attrs != NULL ) {
152                 for ( i = 0; attrs[i] != NULL; i++ ) {
153                         attr_normalize( attrs[i] );
154                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
155                 }
156         }
157
158         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
159
160         Statslog( LDAP_DEBUG_STATS,
161             "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
162             op->o_connid, op->o_opid, base, scope, fstr );
163
164         if ( scope == LDAP_SCOPE_BASE ) {
165 #if defined( SLAPD_MONITOR_DN )
166                 if ( strcmp( base, SLAPD_MONITOR_DN ) == 0 ) {
167                         monitor_info( conn, op, attrs, attrsonly );
168                         goto return_results;
169                 }
170 #endif
171
172 #if defined( SLAPD_CONFIG_DN )
173                 if ( strcmp( base, SLAPD_CONFIG_DN ) == 0 ) {
174                         config_info( conn, op, attrs, attrsonly );
175                         goto return_results;
176                 }
177 #endif
178
179 #if defined( SLAPD_SCHEMA_DN )
180                 if ( strcmp( base, SLAPD_SCHEMA_DN ) == 0 ) {
181                         schema_info( conn, op, attrs, attrsonly );
182                         goto return_results;
183                 }
184 #endif
185
186                 if ( strcmp( base, LDAP_ROOT_DSE ) == 0 ) {
187                         root_dse_info( conn, op, attrs, attrsonly );
188                         goto return_results;
189                 }
190         }
191
192         /*
193          * We could be serving multiple database backends.  Select the
194          * appropriate one, or send a referral to our "referral server"
195          * if we don't hold it.
196          */
197         if ( (be = select_backend( base )) == NULL ) {
198                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
199                         NULL, NULL, default_referral, NULL );
200
201                 goto return_results;
202         }
203
204         /* deref the base if needed */
205         base = suffix_alias( be, base );
206
207         /* actually do the search and send the result(s) */
208         if ( be->be_search ) {
209                 (*be->be_search)( be, conn, op, base, scope, deref, sizelimit,
210                     timelimit, filter, fstr, attrs, attrsonly );
211         } else {
212                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
213                         NULL, "Function not implemented", NULL, NULL );
214         }
215
216 return_results:;
217         if( base != NULL) free( base );
218         if( fstr != NULL) free( fstr );
219         if( filter != NULL) filter_free( filter );
220         if ( attrs != NULL ) {
221                 charray_free( attrs );
222         }
223
224         return rc;
225 }