]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
920685e58a7cdf41a7d2fd3725f4025a14240a29
[openldap] / servers / slapd / search.c
1 /*
2  * Copyright (c) 1995 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/string.h>
18 #include <ac/socket.h>
19
20 #include "ldap_defaults.h"
21 #include "slap.h"
22
23
24 int
25 do_search(
26     Connection  *conn,  /* where to send results                       */
27     Operation   *op     /* info about the op to which we're responding */
28 )
29 {
30         int             i, err;
31         ber_int_t               scope, deref, attrsonly;
32         ber_int_t               sizelimit, timelimit;
33         char            *base = NULL, *fstr = NULL;
34         Filter          *filter = NULL;
35         char            **attrs = NULL;
36         Backend         *be;
37         int                     rc;
38
39         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
40
41         /*
42          * Parse the search request.  It looks like this:
43          *
44          *      SearchRequest := [APPLICATION 3] SEQUENCE {
45          *              baseObject      DistinguishedName,
46          *              scope           ENUMERATED {
47          *                      baseObject      (0),
48          *                      singleLevel     (1),
49          *                      wholeSubtree    (2)
50          *              },
51          *              derefAliases    ENUMERATED {
52          *                      neverDerefaliases       (0),
53          *                      derefInSearching        (1),
54          *                      derefFindingBaseObj     (2),
55          *                      alwaysDerefAliases      (3)
56          *              },
57          *              sizelimit       INTEGER (0 .. 65535),
58          *              timelimit       INTEGER (0 .. 65535),
59          *              attrsOnly       BOOLEAN,
60          *              filter          Filter,
61          *              attributes      SEQUENCE OF AttributeType
62          *      }
63          */
64
65         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
66         if ( ber_scanf( op->o_ber, "{aiiiib",
67                 &base, &scope, &deref, &sizelimit,
68             &timelimit, &attrsonly ) == LBER_ERROR ) {
69                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL, "" );
70                 goto return_results;
71         }
72
73         if ( scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL
74             && scope != LDAP_SCOPE_SUBTREE ) {
75                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
76                     "Unknown search scope" );
77                 goto return_results;
78         }
79
80         (void) dn_normalize_case( base );
81
82         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
83         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
84             attrsonly);
85
86         /* filter - returns a "normalized" version */
87         if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
88                 send_ldap_result( conn, op, err, NULL, "Bad search filter" );
89                 goto return_results;
90         }
91         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr, 0, 0 );
92
93         /* attributes */
94         if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
95                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL, "" );
96                 goto return_results;
97         }
98
99 #ifdef GET_CTRLS
100         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
101                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
102                 goto return_results;
103         } 
104 #endif
105
106         rc = 0;
107
108         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
109
110         if ( attrs != NULL ) {
111                 for ( i = 0; attrs[i] != NULL; i++ ) {
112                         attr_normalize( attrs[i] );
113                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
114                 }
115         }
116
117         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
118
119         Statslog( LDAP_DEBUG_STATS,
120             "conn=%d op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
121             conn->c_connid, op->o_opid, base, scope, fstr );
122
123 #if defined( SLAPD_MONITOR_DN ) || defined( SLAPD_CONFIG_DN ) || defined( SLAPD_SCHEMA_DN )
124         if ( scope == LDAP_SCOPE_BASE ) {
125 #if defined( SLAPD_MONITOR_DN )
126                 if ( strcmp( base, SLAPD_MONITOR_DN ) == 0 ) {
127                         monitor_info( conn, op );
128                         goto return_results;
129                 }
130 #endif
131 #if defined( SLAPD_CONFIG_DN )
132                 if ( strcmp( base, SLAPD_CONFIG_DN ) == 0 ) {
133                         config_info( conn, op );
134                         goto return_results;
135                 }
136 #endif
137 #if defined( SLAPD_SCHEMA_DN )
138                 if ( strcmp( base, SLAPD_SCHEMA_DN ) == 0 ) {
139                         schema_info( conn, op, attrs, attrsonly );
140                         goto return_results;
141                 }
142 #endif
143         }
144 #endif /* monitor or config or schema dn */
145
146         if ( strcmp( base, LDAP_ROOT_DSE ) == 0 && scope == LDAP_SCOPE_BASE ) {
147                 root_dse_info( conn, op, attrs, attrsonly );
148                 goto return_results;
149         }
150
151         /*
152          * We could be serving multiple database backends.  Select the
153          * appropriate one, or send a referral to our "referral server"
154          * if we don't hold it.
155          */
156         if ( (be = select_backend( base )) == NULL ) {
157                 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
158                     default_referral );
159
160                 goto return_results;
161         }
162
163         /* translate the base if it matches an aliased base part */
164         base = suffixAlias ( base, op, be );
165
166         /* actually do the search and send the result(s) */
167         if ( be->be_search ) {
168                 (*be->be_search)( be, conn, op, base, scope, deref, sizelimit,
169                     timelimit, filter, fstr, attrs, attrsonly );
170         } else {
171                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
172                     "Function not implemented" );
173         }
174
175 return_results:;
176         if( base != NULL) free( base );
177         if( fstr != NULL) free( fstr );
178         if( filter != NULL) filter_free( filter );
179         if ( attrs != NULL ) {
180                 charray_free( attrs );
181         }
182
183         return rc;
184 }