]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
c0fef8bf6fbeedb053a8c4dff2f24c3789f537dd
[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 void
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
38         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
39
40         /*
41          * Parse the search request.  It looks like this:
42          *
43          *      SearchRequest := [APPLICATION 3] SEQUENCE {
44          *              baseObject      DistinguishedName,
45          *              scope           ENUMERATED {
46          *                      baseObject      (0),
47          *                      singleLevel     (1),
48          *                      wholeSubtree    (2)
49          *              },
50          *              derefAliases    ENUMERATED {
51          *                      neverDerefaliases       (0),
52          *                      derefInSearching        (1),
53          *                      derefFindingBaseObj     (2),
54          *                      alwaysDerefAliases      (3)
55          *              },
56          *              sizelimit       INTEGER (0 .. 65535),
57          *              timelimit       INTEGER (0 .. 65535),
58          *              attrsOnly       BOOLEAN,
59          *              filter          Filter,
60          *              attributes      SEQUENCE OF AttributeType
61          *      }
62          */
63
64         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
65         if ( ber_scanf( op->o_ber, "{aiiiib", &base, &scope, &deref, &sizelimit,
66             &timelimit, &attrsonly ) == LBER_ERROR ) {
67                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
68                 goto return_results;
69         }
70         if ( scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL
71             && scope != LDAP_SCOPE_SUBTREE ) {
72                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
73                     "Unknown search scope" );
74                 goto return_results;
75         }
76
77         (void) dn_normalize_case( base );
78
79         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
80         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
81             attrsonly);
82
83         /* filter - returns a "normalized" version */
84         if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
85                 send_ldap_result( conn, op, err, NULL, "Bad search filter" );
86                 goto return_results;
87         }
88         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr, 0, 0 );
89
90         /* attributes */
91         if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
92                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
93                 goto return_results;
94         }
95
96 #ifdef GET_CTRLS
97         if( get_ctrls( conn, op, 1 ) == -1 ) {
98                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
99                 goto return_results;
100         } 
101 #endif
102
103         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
104
105         if ( attrs != NULL ) {
106                 for ( i = 0; attrs[i] != NULL; i++ ) {
107                         attr_normalize( attrs[i] );
108                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
109                 }
110         }
111
112         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
113
114         Statslog( LDAP_DEBUG_STATS,
115             "conn=%d op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
116             conn->c_connid, op->o_opid, base, scope, fstr );
117
118 #if defined( SLAPD_MONITOR_DN ) || defined( SLAPD_CONFIG_DN ) || defined( SLAPD_SCHEMA_DN )
119         if ( scope == LDAP_SCOPE_BASE ) {
120 #if defined( SLAPD_MONITOR_DN )
121                 if ( strcmp( base, SLAPD_MONITOR_DN ) == 0 ) {
122                         monitor_info( conn, op );
123                         goto return_results;
124                 }
125 #endif
126 #if defined( SLAPD_CONFIG_DN )
127                 if ( strcmp( base, SLAPD_CONFIG_DN ) == 0 ) {
128                         config_info( conn, op );
129                         goto return_results;
130                 }
131 #endif
132 #if defined( SLAPD_SCHEMA_DN )
133                 if ( strcmp( base, SLAPD_SCHEMA_DN ) == 0 ) {
134                         schema_info( conn, op, attrs, attrsonly );
135                         goto return_results;
136                 }
137 #endif
138         }
139 #endif /* monitor or config or schema dn */
140
141         if ( strcmp( base, LDAP_ROOT_DSE ) == 0 && scope == LDAP_SCOPE_BASE ) {
142                 root_dse_info( conn, op, attrs, attrsonly );
143                 goto return_results;
144         }
145
146         /*
147          * We could be serving multiple database backends.  Select the
148          * appropriate one, or send a referral to our "referral server"
149          * if we don't hold it.
150          */
151         if ( (be = select_backend( base )) == NULL ) {
152                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
153                     default_referral );
154
155                 goto return_results;
156         }
157
158         /* translate the base if it matches an aliased base part */
159         base = suffixAlias ( base, op, be );
160
161         /* actually do the search and send the result(s) */
162         if ( be->be_search ) {
163                 (*be->be_search)( be, conn, op, base, scope, deref, sizelimit,
164                     timelimit, filter, fstr, attrs, attrsonly );
165         } else {
166                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
167                     "Function not implemented" );
168         }
169
170 return_results:;
171         if( base != NULL) free( base );
172         if( fstr != NULL) free( fstr );
173         if( filter != NULL) filter_free( filter );
174         if ( attrs != NULL ) {
175                 charray_free( attrs );
176         }
177 }