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