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