]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
Added Will Ballantyne's General Aliasing code.
[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 <stdio.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include "slap.h"
18 #include "ldapconfig.h"
19
20 extern int      get_filter();
21 extern Backend  *select_backend();
22 extern char     *suffixAlias();
23
24 extern char     *default_referral;
25
26 void
27 do_search( conn, op )
28     Connection  *conn;  /* where to send results                       */
29     Operation   *op;    /* info about the op to which we're responding */
30 {
31         int             i, err;
32         int             scope, deref, attrsonly;
33         int             sizelimit, timelimit;
34         char            *base, *fstr;
35         Filter          *filter;
36         char            **attrs;
37         Backend         *be;
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", &base, &scope, &deref, &sizelimit,
67             &timelimit, &attrsonly ) == LBER_ERROR ) {
68                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
69                 return;
70         }
71         if ( scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL
72             && scope != LDAP_SCOPE_SUBTREE ) {
73                 free( base );
74                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
75                     "Unknown search scope" );
76                 return;
77         }
78         (void) dn_normalize( base );
79
80         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base, scope, deref );
81         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
82             attrsonly);
83
84         /* filter - returns a "normalized" version */
85         filter = NULL;
86         fstr = NULL;
87         if ( (err = get_filter( conn, op->o_ber, &filter, &fstr )) != 0 ) {
88                 if ( fstr != NULL ) {
89                         free( fstr );
90                 }
91                 free( base );
92                 send_ldap_result( conn, op, err, NULL, "Bad search filter" );
93                 return;
94         }
95         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr, 0, 0 );
96
97         /* attributes */
98         attrs = NULL;
99         if ( ber_scanf( op->o_ber, "{v}}", &attrs ) == LBER_ERROR ) {
100                 free( base );
101                 free( fstr );
102                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
103                 return;
104         }
105         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
106         if ( attrs != NULL ) {
107                 for ( i = 0; attrs[i] != NULL; i++ ) {
108                         attr_normalize( attrs[i] );
109                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
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 ( strcasecmp( base, SLAPD_MONITOR_DN ) == 0 ) {
122                         free( base );
123                         free( fstr );
124                         monitor_info( conn, op );
125                         return;
126                 }
127 #endif
128 #if defined( SLAPD_CONFIG_DN )
129                 if ( strcasecmp( base, SLAPD_CONFIG_DN ) == 0 ) {
130                         free( base );
131                         free( fstr );
132                         config_info( conn, op );
133                         return;
134                 }
135 #endif
136 #if defined( SLAPD_SCHEMA_DN )
137                 if ( strcasecmp( base, SLAPD_SCHEMA_DN ) == 0 ) {
138                         free( base );
139                         free( fstr );
140                         schema_info( conn, op );
141                         return;
142                 }
143 #endif
144         }
145 #endif /* monitor or config or schema dn */
146
147         /*
148          * We could be serving multiple database backends.  Select the
149          * appropriate one, or send a referral to our "referral server"
150          * if we don't hold it.
151          */
152         if ( (be = select_backend( base )) == NULL ) {
153                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
154                     default_referral );
155
156                 free( base );
157                 free( fstr );
158                 filter_free( filter );
159                 if ( attrs != NULL ) {
160                         charray_free( attrs );
161                 }
162                 return;
163         }
164
165         /* translate the base if it matches an aliased base part */
166         base = suffixAlias ( base, op, be );
167
168         /* actually do the search and send the result(s) */
169         if ( be->be_search != NULL ) {
170                 (*be->be_search)( be, conn, op, base, scope, deref, sizelimit,
171                     timelimit, filter, fstr, attrs, attrsonly );
172         } else {
173                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
174                     "Function not implemented" );
175         }
176
177         free( base );
178         free( fstr );
179         filter_free( filter );
180         if ( attrs != NULL ) {
181                 charray_free( attrs );
182         }
183 }