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