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