summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/search.c
blob: f9935a72d4e2a3cc7b255cbbd160ee582a897242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * do so, delete this exception statement from your version. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
/*
 * Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "slap.h"
#include "pratom.h"
#include "snmp_collator.h"

static void log_search_access (Slapi_PBlock *pb, const char *base, int scope, const char *filter, const char *msg);

void
do_search( Slapi_PBlock *pb )
{
	Slapi_Operation *operation;
    BerElement	*ber;
    int			i, err;
    int			scope, deref, attrsonly;
    int			sizelimit, timelimit;
    long		long_scope,long_deref,long_sizelimit,long_timelimit;
    char		*base = NULL, *fstr = NULL;
    struct slapi_filter	*filter = NULL;
    char		**attrs = NULL;
    int			psearch = 0;
    struct berval	*psbvp;
	int			changetypes;
	int			send_entchg_controls;
	int			changesonly = 0;
	int			rc = -1;
	char		*original_base = 0;
	char		*new_base = 0;

    LDAPDebug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );

	slapi_pblock_get( pb, SLAPI_OPERATION, &operation);
    ber = operation->o_ber;

    /* count the search request */
    PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsSearchOps);

    /*
     * Parse the search request.  It looks like this:
     *
     *	SearchRequest := [APPLICATION 3] SEQUENCE {
     *		baseObject	DistinguishedName,
     *		scope		ENUMERATED {
     *			baseObject	(0),
     *			singleLevel	(1),
     *			wholeSubtree	(2)
     *		},
     *		derefAliases	ENUMERATED {
     *			neverDerefaliases	(0),
     *			derefInSearching	(1),
     *			derefFindingBaseObj	(2),
     *			alwaysDerefAliases	(3)
     *		},
     *		sizelimit	INTEGER (0 .. 65535),
     *		timelimit	INTEGER (0 .. 65535),
     *		attrsOnly	BOOLEAN,
     *		filter		Filter,
     *		attributes	SEQUENCE OF AttributeType
     *	}
     */

    /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
    if ( ber_scanf( ber, "{aiiiib", &base, &long_scope, &long_deref, &long_sizelimit, &long_timelimit, &attrsonly ) == LBER_ERROR ){
        slapi_ch_free((void**)&base );
        log_search_access (pb, "???", -1, "???", "decoding error");
        send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0, NULL );
        return;
    }
    scope = long_scope;
    deref = long_deref;
    sizelimit = long_sizelimit;
    timelimit = long_timelimit;

    /*
     * ignore negative time and size limits since they make no sense
     */
    if ( timelimit < 0 ) {
		timelimit = 0;
    }
    if ( sizelimit < 0 ) {
		sizelimit = 0;
    }

    if ( scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL
	&& scope != LDAP_SCOPE_SUBTREE ) {
		log_search_access (pb, base, scope, "???", "Unknown search scope");
	    send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
		"Unknown search scope", 0, NULL );
	    goto free_and_return;
    }
    /* check and record the scope for snmp */
    if ( scope == LDAP_SCOPE_ONELEVEL) {
		/* count the one level search request */
		PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsOneLevelSearchOps);

    } else if (scope == LDAP_SCOPE_SUBTREE) {
		/* count the subtree search request */
		PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsWholeSubtreeSearchOps);
    }

    /* filter - returns a "normalized" version */
    filter = NULL;
    fstr = NULL;
    if ( (err = get_filter( pb->pb_conn, ber, scope, &filter, &fstr )) != 0 ) {
		char	*errtxt;

		if ( LDAP_UNWILLING_TO_PERFORM == err ) {
			errtxt = "The search filter is too deeply nested";
		} else {
			errtxt = "Bad search filter";
		} 
		log_search_access( pb, base, scope, "???", errtxt );
	    send_ldap_result( pb, err, NULL, errtxt, 0, NULL );
	    goto free_and_return;
    }

    /* attributes */
    attrs = NULL;
    if ( ber_scanf( ber, "{v}}", &attrs ) == LBER_ERROR ) {
		log_search_access (pb, base, scope, fstr, "decoding error");
	    send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0,
		NULL );
	    goto free_and_return;
    }

	/* 
	 * This search is performed against the legacy consumer, so ask explicitly
	 * for the aci attribute as it is an operational in 5.0
	 */
	if ( operation->o_flags & OP_FLAG_LEGACY_REPLICATION_DN )
	{
		/* If attrs==NULL in a 4.x request, means that we want all the attributes, as aci is 
		 * now operational, we need to ask it explicilty as well as all the attributes
		 */
		if ( (attrs == NULL) || (attrs[0] == NULL) )
		{
			charray_add(&attrs, slapi_attr_syntax_normalize("aci"));
			charray_add(&attrs, slapi_attr_syntax_normalize(LDAP_ALL_USER_ATTRS));
		}
	    else 
		{
			for ( i = 0; attrs[i] != NULL; i++ )
			{
				if ( strcasecmp(attrs[i], LDAP_ALL_USER_ATTRS) == 0 )
				{
					charray_add(&attrs, slapi_attr_syntax_normalize("aci"));
					break;
				}
			}
		}
	}

    if ( attrs != NULL ) {
	    operation->o_searchattrs = cool_charray_dup( attrs );
	    for ( i = 0; attrs[i] != NULL; i++ ) {
		    char	*type;

		    type = slapi_attr_syntax_normalize(attrs[i]);
		    slapi_ch_free( (void**)&(attrs[i]) );
		    attrs[i] = type;
	    }
    }
    if ( slapd_ldap_debug & LDAP_DEBUG_ARGS ) {
	char abuf[ 1024 ], *astr;

	if ( NULL == attrs ) {
		astr = "ALL";
	} else {
		strarray2str( attrs, abuf, sizeof( abuf ), 1 /* include quotes */);
		astr = abuf;
	}
	slapi_log_error( SLAPI_LOG_ARGS, NULL, "SRCH base=\"%s\" "
	"scope=%d deref=%d "
	"sizelimit=%d timelimit=%d attrsonly=%d filter=\"%s\" "
	"attrs=%s\n", base, scope, deref, sizelimit, timelimit,
	attrsonly, fstr, astr );
    }

    /*
     * in LDAPv3 there can be optional control extensions on
     * the end of an LDAPMessage. we need to read them in and
     * pass them to the backend. get_ldapmessage_controls()
     * reads the controls and sets any we know about in the pb.
     */
    if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 ) {
		log_search_access (pb, base, scope, fstr, "failed to decode LDAP controls");
	    send_ldap_result( pb, err, NULL, NULL, 0, NULL );
	    goto free_and_return;
    }

	/* we support persistent search for regular operations only */
	if ( slapi_control_present( operation->o_params.request_controls,
								LDAP_CONTROL_PERSISTENTSEARCH, &psbvp, NULL )){
		operation_set_flag(operation, OP_FLAG_PS);
		psearch = 1;
		if ( ps_parse_control_value( psbvp, &changetypes,
									 &changesonly, &send_entchg_controls ) != LDAP_SUCCESS )
		{
			changetypes = LDAP_CHANGETYPE_ANY;
			send_entchg_controls = 0;
		}
		else if ( changesonly )
		{
			operation_set_flag(operation, OP_FLAG_PS_CHANGESONLY);
		}
	}

    slapi_pblock_set( pb, SLAPI_SEARCH_TARGET, base );
	slapi_pblock_set( pb, SLAPI_ORIGINAL_TARGET_DN, slapi_ch_strdup(base) );
    slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE, &scope );
    slapi_pblock_set( pb, SLAPI_SEARCH_DEREF, &deref );
    slapi_pblock_set( pb, SLAPI_SEARCH_FILTER, filter );
    slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, fstr );
    slapi_pblock_set( pb, SLAPI_SEARCH_ATTRS, attrs );
    slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly );
    slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &operation->o_isroot );
	slapi_pblock_set( pb, SLAPI_SEARCH_SIZELIMIT, &sizelimit );
	slapi_pblock_set( pb, SLAPI_SEARCH_TIMELIMIT, &timelimit );

	/* plugins which play with the search may 
	 * change the search params may allocate
	 * memory so we need to keep track of
	 * changed base search strings
	 */
	slapi_pblock_get(pb, SLAPI_SEARCH_TARGET, &original_base);

	op_shared_search (pb, psearch ? 0 : 1/* send result */);

	slapi_pblock_get(pb, SLAPI_SEARCH_TARGET, &new_base);
	slapi_pblock_get (pb, SLAPI_PLUGIN_OPRETURN, &rc);
    slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, &filter );
	
	if ( psearch && rc == 0 ) { 		
		ps_add( pb, changetypes, send_entchg_controls );
	}	

free_and_return:;
    if ( !psearch || rc < 0 ) {
		if(original_base != new_base) {
			slapi_ch_free_string(&new_base);
		}
        slapi_ch_free_string(&base);
        slapi_ch_free_string(&fstr);
        slapi_filter_free( filter, 1 );
        if ( attrs != NULL ) {
            charray_free( attrs );
        }
		/* 
		 * Fix for defect 526719 / 553356 : Persistent search op failed. 
		 * Marking it as non-persistent so that operation resources get freed 
		 */
		if (psearch){
			operation->o_flags &= ~OP_FLAG_PS;
		}
		/* we strdup'd this above - need to free */
		slapi_pblock_get(pb, SLAPI_ORIGINAL_TARGET_DN, &base);
		slapi_ch_free_string(&base);
    }
}

static void log_search_access (Slapi_PBlock *pb, const char *base, int scope, const char *fstr, const char *msg)
{
	char ebuf[BUFSIZ];
	slapi_log_access(LDAP_DEBUG_STATS,
					 "conn=%d op=%d SRCH base=\"%s\" scope=%d filter=\"%s\", %s\n",
					 pb->pb_conn->c_connid, pb->pb_op->o_opid, 
					 escape_string(base, ebuf), scope, fstr, msg ? msg : "");

}