]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
place old schema codes behind -DSLAPD_SCHEMA_COMPAT
[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 "ldap_pvt.h"
22 #include "slap.h"
23
24
25 int
26 do_search(
27     Connection  *conn,  /* where to send results                       */
28     Operation   *op     /* info about the op to which we're responding */
29 )
30 {
31         int             i, err;
32         ber_int_t               scope, deref, attrsonly;
33         ber_int_t               sizelimit, timelimit;
34         char            *base = NULL, *nbase = NULL, *fstr = NULL;
35         Filter          *filter = NULL;
36         char            **attrs = NULL;
37         Backend         *be;
38         int                     rc;
39
40         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
41
42         if( op->o_bind_in_progress ) {
43                 Debug( LDAP_DEBUG_ANY, "do_search: SASL bind in progress.\n",
44                         0, 0, 0 );
45                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
46                         NULL, "SASL bind in progress", NULL, NULL );
47                 return LDAP_SASL_BIND_IN_PROGRESS;
48         }
49
50         /*
51          * Parse the search request.  It looks like this:
52          *
53          *      SearchRequest := [APPLICATION 3] SEQUENCE {
54          *              baseObject      DistinguishedName,
55          *              scope           ENUMERATED {
56          *                      baseObject      (0),
57          *                      singleLevel     (1),
58          *                      wholeSubtree    (2)
59          *              },
60          *              derefAliases    ENUMERATED {
61          *                      neverDerefaliases       (0),
62          *                      derefInSearching        (1),
63          *                      derefFindingBaseObj     (2),
64          *                      alwaysDerefAliases      (3)
65          *              },
66          *              sizelimit       INTEGER (0 .. 65535),
67          *              timelimit       INTEGER (0 .. 65535),
68          *              attrsOnly       BOOLEAN,
69          *              filter          Filter,
70          *              attributes      SEQUENCE OF AttributeType
71          *      }
72          */
73
74         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
75         if ( ber_scanf( op->o_ber, "{aiiiib",
76                 &base, &scope, &deref, &sizelimit,
77             &timelimit, &attrsonly ) == LBER_ERROR ) {
78                 send_ldap_disconnect( conn, op,
79                         LDAP_PROTOCOL_ERROR, "decoding error" );
80                 rc = -1;
81                 goto return_results;
82         }
83
84         switch( scope ) {
85         case LDAP_SCOPE_BASE:
86         case LDAP_SCOPE_ONELEVEL:
87         case LDAP_SCOPE_SUBTREE:
88                 break;
89         default:
90                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
91                         NULL, "invalid scope", NULL, NULL );
92                 rc = -1;
93                 goto return_results;
94         }
95
96         switch( deref ) {
97         case LDAP_DEREF_NEVER:
98         case LDAP_DEREF_FINDING:
99         case LDAP_DEREF_SEARCHING:
100         case LDAP_DEREF_ALWAYS:
101                 break;
102         default:
103                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
104                         NULL, "invalid deref", NULL, NULL );
105                 rc = -1;
106                 goto return_results;
107         }
108
109         nbase = ch_strdup( base );
110
111         if( dn_normalize( nbase ) == NULL ) {
112                 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX,
113                         NULL, "invalid DN", NULL, NULL );
114                 rc = -1;
115                 goto return_results;
116         }
117
118         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
119         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
120             attrsonly);
121
122         /* filter - returns a "normalized" version */
123         if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
124                 if( err == -1 ) {
125                         send_ldap_disconnect( conn, op,
126                                 LDAP_PROTOCOL_ERROR, "decode error" );
127                 } else {
128                         send_ldap_result( conn, op, err,
129                                 NULL, "Bad search filter", NULL, NULL );
130                 }
131                 rc = -1;
132                 goto return_results;
133         }
134
135         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr, 0, 0 );
136
137         /* attributes */
138         if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
139                 send_ldap_disconnect( conn, op,
140                         LDAP_PROTOCOL_ERROR, "decoding error" );
141                 rc = -1;
142                 goto return_results;
143         }
144
145         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
146                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
147                 goto return_results;
148         } 
149
150         rc = 0;
151
152         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
153
154         if ( attrs != NULL ) {
155                 for ( i = 0; attrs[i] != NULL; i++ ) {
156                         attr_normalize( attrs[i] );
157                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
158                 }
159         }
160
161         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
162
163         Statslog( LDAP_DEBUG_STATS,
164             "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
165             op->o_connid, op->o_opid, base, scope, fstr );
166
167         if ( scope == LDAP_SCOPE_BASE ) {
168 #if defined( SLAPD_MONITOR_DN )
169                 if ( strcmp( nbase, SLAPD_MONITOR_DN ) == 0 ) {
170                         monitor_info( conn, op, attrs, attrsonly );
171                         goto return_results;
172                 }
173 #endif
174
175 #if defined( SLAPD_CONFIG_DN )
176                 if ( strcmp( nbase, SLAPD_CONFIG_DN ) == 0 ) {
177                         config_info( conn, op, attrs, attrsonly );
178                         goto return_results;
179                 }
180 #endif
181
182 #if defined( SLAPD_SCHEMA_DN )
183                 if ( strcmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
184                         schema_info( conn, op, attrs, attrsonly );
185                         goto return_results;
186                 }
187 #endif
188
189                 if ( strcmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
190                         root_dse_info( conn, op, attrs, attrsonly );
191                         goto return_results;
192                 }
193         }
194
195         /*
196          * We could be serving multiple database backends.  Select the
197          * appropriate one, or send a referral to our "referral server"
198          * if we don't hold it.
199          */
200         if ( (be = select_backend( nbase )) == NULL ) {
201                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
202                         NULL, NULL, default_referral, NULL );
203
204                 goto return_results;
205         }
206
207         /* make sure this backend recongizes critical controls */
208         rc = backend_check_controls( be, conn, op ) ;
209
210         if( rc != LDAP_SUCCESS ) {
211                 send_ldap_result( conn, op, rc,
212                         NULL, NULL, NULL, NULL );
213         }
214
215         /* deref the base if needed */
216         nbase = suffix_alias( be, nbase );
217
218         /* actually do the search and send the result(s) */
219         if ( be->be_search ) {
220                 (*be->be_search)( be, conn, op, base, nbase, scope, deref, sizelimit,
221                     timelimit, filter, fstr, attrs, attrsonly );
222         } else {
223                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
224                         NULL, "Function not implemented", NULL, NULL );
225         }
226
227 return_results:;
228         if( base != NULL) free( base );
229         if( nbase != NULL) free( nbase );
230         if( fstr != NULL) free( fstr );
231         if( filter != NULL) filter_free( filter );
232         if ( attrs != NULL ) {
233                 charray_free( attrs );
234         }
235
236         return rc;
237 }