]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
67956eebfe7bbb0d9a9637383ee52436b30eedda
[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
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;
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         char            *text;
44
45         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
46
47         /*
48          * Parse the search request.  It looks like this:
49          *
50          *      SearchRequest := [APPLICATION 3] SEQUENCE {
51          *              baseObject      DistinguishedName,
52          *              scope           ENUMERATED {
53          *                      baseObject      (0),
54          *                      singleLevel     (1),
55          *                      wholeSubtree    (2)
56          *              },
57          *              derefAliases    ENUMERATED {
58          *                      neverDerefaliases       (0),
59          *                      derefInSearching        (1),
60          *                      derefFindingBaseObj     (2),
61          *                      alwaysDerefAliases      (3)
62          *              },
63          *              sizelimit       INTEGER (0 .. 65535),
64          *              timelimit       INTEGER (0 .. 65535),
65          *              attrsOnly       BOOLEAN,
66          *              filter          Filter,
67          *              attributes      SEQUENCE OF AttributeType
68          *      }
69          */
70
71         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
72         if ( ber_scanf( op->o_ber, "{aiiiib" /*}*/,
73                 &base, &scope, &deref, &sizelimit,
74             &timelimit, &attrsonly ) == LBER_ERROR ) {
75                 send_ldap_disconnect( conn, op,
76                         LDAP_PROTOCOL_ERROR, "decoding error" );
77                 rc = SLAPD_DISCONNECT;
78                 goto return_results;
79         }
80
81         switch( scope ) {
82         case LDAP_SCOPE_BASE:
83         case LDAP_SCOPE_ONELEVEL:
84         case LDAP_SCOPE_SUBTREE:
85                 break;
86         default:
87                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
88                         NULL, "invalid scope", NULL, NULL );
89                 goto return_results;
90         }
91
92         switch( deref ) {
93         case LDAP_DEREF_NEVER:
94         case LDAP_DEREF_FINDING:
95         case LDAP_DEREF_SEARCHING:
96         case LDAP_DEREF_ALWAYS:
97                 break;
98         default:
99                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
100                         NULL, "invalid deref", NULL, NULL );
101                 goto return_results;
102         }
103
104         nbase = ch_strdup( base );
105
106         if( dn_normalize( nbase ) == NULL ) {
107                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
108                         NULL, "invalid DN", NULL, NULL );
109                 goto return_results;
110         }
111
112         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
113         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
114             attrsonly);
115
116         /* filter - returns a "normalized" version */
117         if ( (rc = get_filter( conn, op->o_ber, &filter, &fstr, &text )) != 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 #ifndef SLAPD_SCHEMA_NOT_COMPAT
150                         attr_normalize( attrs[i] );
151 #endif
152                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
153                 }
154         }
155
156         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
157
158         Statslog( LDAP_DEBUG_STATS,
159             "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
160             op->o_connid, op->o_opid, base, scope, fstr );
161
162         if ( scope == LDAP_SCOPE_BASE ) {
163                 Entry *entry = NULL;
164
165                 if ( strcasecmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
166                         rc = root_dse_info( &entry, &text );
167                 }
168
169 #if defined( SLAPD_MONITOR_DN )
170                 else if ( strcasecmp( nbase, SLAPD_MONITOR_DN ) == 0 ) {
171                         rc = monitor_info( &entry, &text );
172                 }
173 #endif
174
175 #if defined( SLAPD_CONFIG_DN )
176                 else if ( strcasecmp( nbase, SLAPD_CONFIG_DN ) == 0 ) {
177                         rc = config_info( &entry, &text );
178                 }
179 #endif
180
181 #if defined( SLAPD_SCHEMA_DN )
182                 else if ( strcasecmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
183                         rc= schema_info( &entry, &text );
184                 }
185 #endif
186
187                 if( rc != LDAP_SUCCESS ) {
188                         send_ldap_result( conn, op, rc,
189                                 NULL, text, NULL, NULL );
190                         goto return_results;
191
192                 } else if ( entry != NULL ) {
193                         rc = test_filter( NULL, conn, op,
194                                 entry, filter );
195
196                         if( rc == LDAP_COMPARE_TRUE ) {
197                                 send_search_entry( &backends[0], conn, op,
198                                         entry, attrs, attrsonly, NULL );
199                         }
200                         entry_free( entry );
201
202                         send_ldap_result( conn, op, LDAP_SUCCESS,
203                                 NULL, NULL, NULL, NULL );
204
205                         goto return_results;
206                 }
207         }
208
209         /*
210          * We could be serving multiple database backends.  Select the
211          * appropriate one, or send a referral to our "referral server"
212          * if we don't hold it.
213          */
214         if ( (be = select_backend( nbase )) == NULL ) {
215                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
216                         NULL, NULL, default_referral, NULL );
217
218                 goto return_results;
219         }
220
221         /* make sure this backend recongizes critical controls */
222         rc = backend_check_controls( be, conn, op, &text ) ;
223
224         if( rc != LDAP_SUCCESS ) {
225                 send_ldap_result( conn, op, rc,
226                         NULL, text, NULL, NULL );
227                 goto return_results;
228         }
229
230         /* deref the base if needed */
231         nbase = suffix_alias( be, nbase );
232
233         /* actually do the search and send the result(s) */
234         if ( be->be_search ) {
235                 (*be->be_search)( be, conn, op, base, nbase, scope, deref, sizelimit,
236                     timelimit, filter, fstr, attrs, attrsonly );
237         } else {
238                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
239                         NULL, "operation not supported within namingContext", NULL, NULL );
240         }
241
242 return_results:;
243         if( base != NULL) free( base );
244         if( nbase != NULL) free( nbase );
245         if( fstr != NULL) free( fstr );
246         if( filter != NULL) filter_free( filter );
247         if ( attrs != NULL ) {
248                 charray_free( attrs );
249         }
250
251         return rc;
252 }