]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
24bf9c34b3a1ccffc4d2df49160f517513567c2f
[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;
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 = SLAPD_DISCONNECT;
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, rc = LDAP_PROTOCOL_ERROR,
95                         NULL, "invalid scope", NULL, NULL );
96                 goto return_results;
97         }
98
99         switch( deref ) {
100         case LDAP_DEREF_NEVER:
101         case LDAP_DEREF_FINDING:
102         case LDAP_DEREF_SEARCHING:
103         case LDAP_DEREF_ALWAYS:
104                 break;
105         default:
106                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
107                         NULL, "invalid deref", NULL, NULL );
108                 goto return_results;
109         }
110
111         nbase = ch_strdup( base );
112
113         if( dn_normalize( nbase ) == NULL ) {
114                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
115                         NULL, "invalid DN", NULL, NULL );
116                 goto return_results;
117         }
118
119         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
120         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
121             attrsonly);
122
123         /* filter - returns a "normalized" version */
124         if ( (rc = get_filter( conn, op->o_ber, &filter, &fstr )) != LDAP_SUCCESS ) {
125                 if( rc == SLAPD_DISCONNECT ) {
126                         send_ldap_disconnect( conn, op,
127                                 LDAP_PROTOCOL_ERROR, "decode filter error" );
128                 } else {
129                         send_ldap_result( conn, op, rc,
130                                 NULL, "bad search filter", NULL, NULL );
131                 }
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 attrs error" );
141                 rc = SLAPD_DISCONNECT;
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 #ifndef SLAPD_SCHEMA_NOT_COMPAT
157                         attr_normalize( attrs[i] );
158 #endif
159                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
160                 }
161         }
162
163         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
164
165         Statslog( LDAP_DEBUG_STATS,
166             "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
167             op->o_connid, op->o_opid, base, scope, fstr );
168
169         if ( scope == LDAP_SCOPE_BASE ) {
170 #if defined( SLAPD_MONITOR_DN )
171                 if ( strcmp( nbase, SLAPD_MONITOR_DN ) == 0 ) {
172                         monitor_info( conn, op, attrs, attrsonly );
173                         goto return_results;
174                 }
175 #endif
176
177 #if defined( SLAPD_CONFIG_DN )
178                 if ( strcmp( nbase, SLAPD_CONFIG_DN ) == 0 ) {
179                         config_info( conn, op, attrs, attrsonly );
180                         goto return_results;
181                 }
182 #endif
183
184 #if defined( SLAPD_SCHEMA_DN )
185                 if ( strcmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
186                         schema_info( conn, op, attrs, attrsonly );
187                         goto return_results;
188                 }
189 #endif
190
191                 if ( strcmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
192                         root_dse_info( conn, op, attrs, attrsonly );
193                         goto return_results;
194                 }
195         }
196
197         /*
198          * We could be serving multiple database backends.  Select the
199          * appropriate one, or send a referral to our "referral server"
200          * if we don't hold it.
201          */
202         if ( (be = select_backend( nbase )) == NULL ) {
203                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
204                         NULL, NULL, default_referral, NULL );
205
206                 goto return_results;
207         }
208
209         /* make sure this backend recongizes critical controls */
210         rc = backend_check_controls( be, conn, op ) ;
211
212         if( rc != LDAP_SUCCESS ) {
213                 send_ldap_result( conn, op, rc,
214                         NULL, NULL, NULL, NULL );
215                 goto return_results;
216         }
217
218         /* deref the base if needed */
219         nbase = suffix_alias( be, nbase );
220
221         /* actually do the search and send the result(s) */
222         if ( be->be_search ) {
223                 (*be->be_search)( be, conn, op, base, nbase, scope, deref, sizelimit,
224                     timelimit, filter, fstr, attrs, attrsonly );
225         } else {
226                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
227                         NULL, "Function not implemented", NULL, NULL );
228         }
229
230 return_results:;
231         if( base != NULL) free( base );
232         if( nbase != NULL) free( nbase );
233         if( fstr != NULL) free( fstr );
234         if( filter != NULL) filter_free( filter );
235         if ( attrs != NULL ) {
236                 charray_free( attrs );
237         }
238
239         return rc;
240 }