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