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