search.c 10.8 KB
Newer Older
1
/* $OpenLDAP$ */
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2007 The OpenLDAP Foundation.
5
 * All rights reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
 *
7
8
9
10
11
12
13
14
15
16
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Portions Copyright (c) 1990 Regents of the University of Michigan.
 * All rights reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
17
18
 */

19
20
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
21
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
22
23

#include <ac/stdlib.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
24

25
26
27
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
28

Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
#include "ldap-int.h"
Julius Enarusai's avatar
   
Julius Enarusai committed
30
#include "ldap_log.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
31

32
33
34
35
36
37
38
/*
 * ldap_search_ext - initiate an ldap search operation.
 *
 * Parameters:
 *
 *	ld		LDAP descriptor
 *	base		DN of the base object
Kurt Zeilenga's avatar
Kurt Zeilenga committed
39
40
41
42
43
 *	scope		the search scope - one of
 *				LDAP_SCOPE_BASE (baseObject),
 *			    LDAP_SCOPE_ONELEVEL (oneLevel),
 *				LDAP_SCOPE_SUBTREE (subtree), or
 *				LDAP_SCOPE_SUBORDINATE (children) -- OpenLDAP extension
44
45
46
47
48
49
50
 *	filter		a string containing the search filter
 *			(e.g., "(|(cn=bob)(sn=bob))")
 *	attrs		list of attribute types to return for matches
 *	attrsonly	1 => attributes only 0 => attributes and values
 *
 * Example:
 *	char	*attrs[] = { "mail", "title", 0 };
51
 *	ldap_search_ext( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 *	    attrs, attrsonly, sctrls, ctrls, timeout, sizelimit,
 *		&msgid );
 */
int
ldap_search_ext(
	LDAP *ld,
	LDAP_CONST char *base,
	int scope,
	LDAP_CONST char *filter,
	char **attrs,
	int attrsonly,
	LDAPControl **sctrls,
	LDAPControl **cctrls,
	struct timeval *timeout,
	int sizelimit,
	int *msgidp )
{
69
	int rc;
70
71
	BerElement	*ber;
	int timelimit;
72
	ber_int_t id;
73
74
75

	Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
76
77
78
	assert( ld != NULL );
	assert( LDAP_VALID( ld ) );

79
80
81
82
	/* check client controls */
	rc = ldap_int_client_controls( ld, cctrls );
	if( rc != LDAP_SUCCESS ) return rc;

83
	/*
84
	 * if timeout is provided, both tv_sec and tv_usec must
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
85
	 * not be zero
86
	 */
87
88
89
90
91
92
93
94
95
96
97
98
	if( timeout != NULL ) {
		if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
			return LDAP_PARAM_ERROR;
		}

		/* timelimit must be non-zero if timeout is provided */
		timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;

	} else {
		/* no timeout, no timelimit */
		timelimit = -1;
	}
99
100

	ber = ldap_build_search_req( ld, base, scope, filter, attrs,
101
	    attrsonly, sctrls, cctrls, timelimit, sizelimit, &id ); 
102

103
	if ( ber == NULL ) {
104
105
106
107
108
		return ld->ld_errno;
	}


	/* send the message */
109
	*msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber, id );
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

	if( *msgidp < 0 )
		return ld->ld_errno;

	return LDAP_SUCCESS;
}

int
ldap_search_ext_s(
	LDAP *ld,
	LDAP_CONST char *base,
	int scope,
	LDAP_CONST char *filter,
	char **attrs,
	int attrsonly,
	LDAPControl **sctrls,
	LDAPControl **cctrls,
	struct timeval *timeout,
	int sizelimit,
	LDAPMessage **res )
{
	int rc;
	int	msgid;

	rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
		sctrls, cctrls, timeout, sizelimit, &msgid );

	if ( rc != LDAP_SUCCESS ) {
		return( rc );
	}

141
	rc = ldap_result( ld, msgid, LDAP_MSG_ALL, timeout, res );
142
143
144

	if( rc <= 0 ) {
		/* error(-1) or timeout(0) */
145
		return( ld->ld_errno );
146
	}
147

148
	if( rc == LDAP_RES_SEARCH_REFERENCE || rc == LDAP_RES_INTERMEDIATE ) {
149
150
151
		return( ld->ld_errno );
	}

152
153
154
	return( ldap_result2error( ld, *res, 0 ) );
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
155
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
156
157
158
 * ldap_search - initiate an ldap search operation.
 *
 * Parameters:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
159
160
161
 *
 *	ld		LDAP descriptor
 *	base		DN of the base object
Kurt Zeilenga's avatar
Kurt Zeilenga committed
162
163
164
165
166
 *	scope		the search scope - one of
 *				LDAP_SCOPE_BASE (baseObject),
 *			    LDAP_SCOPE_ONELEVEL (oneLevel),
 *				LDAP_SCOPE_SUBTREE (subtree), or
 *				LDAP_SCOPE_SUBORDINATE (children) -- OpenLDAP extension
Kurt Zeilenga's avatar
Kurt Zeilenga committed
167
168
169
170
171
172
173
 *	filter		a string containing the search filter
 *			(e.g., "(|(cn=bob)(sn=bob))")
 *	attrs		list of attribute types to return for matches
 *	attrsonly	1 => attributes only 0 => attributes and values
 *
 * Example:
 *	char	*attrs[] = { "mail", "title", 0 };
174
 *	msgid = ldap_search( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
175
176
177
 *	    attrs, attrsonly );
 */
int
178
179
ldap_search(
	LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
180
181
182
	char **attrs, int attrsonly )
{
	BerElement	*ber;
183
	ber_int_t	id;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
184
185
186

	Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
187
188
189
	assert( ld != NULL );
	assert( LDAP_VALID( ld ) );

190
	ber = ldap_build_search_req( ld, base, scope, filter, attrs,
191
	    attrsonly, NULL, NULL, -1, -1, &id ); 
192

193
	if ( ber == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
194
195
196
197
198
		return( -1 );
	}


	/* send the message */
199
	return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber, id ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
200
201
202
203
}


BerElement *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
204
205
ldap_build_search_req(
	LDAP *ld,
Hallvard Furuseth's avatar
Hallvard Furuseth committed
206
	LDAP_CONST char *base,
207
	ber_int_t scope,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
208
	LDAP_CONST char *filter,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
209
	char **attrs,
210
	ber_int_t attrsonly,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
211
	LDAPControl **sctrls,
212
	LDAPControl **cctrls,
213
	ber_int_t timelimit,
214
215
	ber_int_t sizelimit,
	ber_int_t *idp)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
216
217
{
	BerElement	*ber;
218
	int		err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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

	/*
	 * Create 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
	 *	}
	 * wrapped in an ldap message.
	 */

	/* create a message to send */
245
246
	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
		return( NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
247
248
	}

Hallvard Furuseth's avatar
Hallvard Furuseth committed
249
	if ( base == NULL ) {
250
251
252
		/* no base provided, use session default base */
		base = ld->ld_options.ldo_defbase;

Hallvard Furuseth's avatar
Hallvard Furuseth committed
253
254
255
256
		if ( base == NULL ) {
			/* no session default base, use top */
			base = "";
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
257
258
	}

259
	LDAP_NEXT_MSGID( ld, *idp );
260
261
#ifdef LDAP_CONNECTIONLESS
	if ( LDAP_IS_UDP(ld) ) {
262
263
264
		struct sockaddr sa = {0};
		/* dummy, filled with ldo_peer in request.c */
	    err = ber_write( ber, &sa, sizeof( sa ), 0 );
265
266
267
268
	}
	if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
	    char *dn = ld->ld_options.ldo_cldapdn;
	    if (!dn) dn = "";
269
	    err = ber_printf( ber, "{ist{seeiib", *idp, dn,
270
271
272
273
		LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
		(sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
		(timelimit < 0) ? ld->ld_timelimit : timelimit,
		attrsonly );
274
275
276
	} else
#endif
	{
277
	    err = ber_printf( ber, "{it{seeiib", *idp,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
278
279
280
281
		LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
		(sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
		(timelimit < 0) ? ld->ld_timelimit : timelimit,
		attrsonly );
282
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
283
284
285
286

	if ( err == -1 ) {
		ld->ld_errno = LDAP_ENCODING_ERROR;
		ber_free( ber, 1 );
287
		return( NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
288
289
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
290
291
	if( filter == NULL ) {
		filter = "(objectclass=*)";
Kurt Zeilenga's avatar
Kurt Zeilenga committed
292
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
293

294
	err = ldap_pvt_put_filter( ber, filter );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
295
296
297
298

	if ( err  == -1 ) {
		ld->ld_errno = LDAP_FILTER_ERROR;
		ber_free( ber, 1 );
299
		return( NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
300
301
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
302
303
#ifdef LDAP_DEBUG
	if ( ldap_debug & LDAP_DEBUG_ARGS ) {
304
		char	buf[ BUFSIZ ] = { ' ', '*', '\0' };
Pierangelo Masarati's avatar
Pierangelo Masarati committed
305

306
307
308
		if ( attrs != NULL ) {
			char	*ptr;
			int	i;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
309

310
311
312
313
314
315
			for ( ptr = buf, i = 0;
				attrs[ i ] != NULL && ptr < &buf[ sizeof( buf ) ];
				i++ )
			{
				ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
					" %s", attrs[ i ] );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
316
			}
317
318
319
320
321

			if ( ptr >= &buf[ sizeof( buf ) ] ) {
				AC_MEMCPY( &buf[ sizeof( buf ) - STRLENOF( "...(truncated)" ) - 1 ],
					"...(truncated)", STRLENOF( "...(truncated)" ) + 1 );
			} 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
322
		}
323
324

		Debug( LDAP_DEBUG_ARGS, "ldap_build_search_req ATTRS:%s\n", buf, 0, 0 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
325
326
327
	}
#endif /* LDAP_DEBUG */

328
	if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
329
330
		ld->ld_errno = LDAP_ENCODING_ERROR;
		ber_free( ber, 1 );
331
		return( NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
332
333
334
335
336
	}

	/* Put Server Controls */
	if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
		ber_free( ber, 1 );
337
		return( NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
338
339
	}

340
	if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
341
342
		ld->ld_errno = LDAP_ENCODING_ERROR;
		ber_free( ber, 1 );
343
		return( NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
344
345
346
347
348
349
	}

	return( ber );
}

int
Kurt Zeilenga's avatar
Kurt Zeilenga committed
350
351
352
ldap_search_st(
	LDAP *ld, LDAP_CONST char *base, int scope,
	LDAP_CONST char *filter, char **attrs,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
353
354
355
356
357
358
359
360
	int attrsonly, struct timeval *timeout, LDAPMessage **res )
{
	int	msgid;

	if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
	    == -1 )
		return( ld->ld_errno );

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
361
	if ( ldap_result( ld, msgid, LDAP_MSG_ALL, timeout, res ) == -1 || !*res )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
362
363
364
365
366
367
368
369
370
371
372
373
		return( ld->ld_errno );

	if ( ld->ld_errno == LDAP_TIMEOUT ) {
		(void) ldap_abandon( ld, msgid );
		ld->ld_errno = LDAP_TIMEOUT;
		return( ld->ld_errno );
	}

	return( ldap_result2error( ld, *res, 0 ) );
}

int
Kurt Zeilenga's avatar
Kurt Zeilenga committed
374
375
376
377
378
379
380
381
ldap_search_s(
	LDAP *ld,
	LDAP_CONST char *base,
	int scope,
	LDAP_CONST char *filter,
	char **attrs,
	int attrsonly,
	LDAPMessage **res )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
382
383
384
385
386
387
388
{
	int	msgid;

	if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
	    == -1 )
		return( ld->ld_errno );

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
389
	if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, res ) == -1 || !*res )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
390
391
392
393
394
		return( ld->ld_errno );

	return( ldap_result2error( ld, *res, 0 ) );
}

395
static char escape[128] = {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
396
397
398
399
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
400
401
402
403
404
405
406
407
408
409
410
411
412
413

	0, 0, 0, 0, 0, 0, 0, 0,
	1, 1, 1, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,

	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 1, 0, 0, 0,

	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
414
	0, 0, 0, 0, 0, 0, 0, 1
415
416
417
418
};
#define	NEEDFLTESCAPE(c)	((c) & 0x80 || escape[ (unsigned)(c) ])

/*
Pierangelo Masarati's avatar
Pierangelo Masarati committed
419
 * compute the length of the escaped value
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
 */
ber_len_t
ldap_bv2escaped_filter_value_len( struct berval *in )
{
	ber_len_t	i, l;

	assert( in != NULL );

	if ( in->bv_len == 0 ) {
		return 0;
	}

	for( l = 0, i = 0; i < in->bv_len; l++, i++ ) {
		char c = in->bv_val[ i ];
		if ( NEEDFLTESCAPE( c ) ) {
			l += 2;
		}
	}

	return l;
}

442
443
444
int
ldap_bv2escaped_filter_value( struct berval *in, struct berval *out )
{
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
	return ldap_bv2escaped_filter_value_x( in, out, 0, NULL );
}

int
ldap_bv2escaped_filter_value_x( struct berval *in, struct berval *out, int inplace, void *ctx )
{
	ber_len_t	i, l;

	assert( in != NULL );
	assert( out != NULL );

	BER_BVZERO( out );

	if ( in->bv_len == 0 ) {
		return 0;
	}
461
462

	/* assume we'll escape everything */
463
464
465
466
467
468
469
470
471
472
473
474
475
	l = ldap_bv2escaped_filter_value_len( in );
	if ( l == in->bv_len ) {
		if ( inplace ) {
			*out = *in;
		} else {
			ber_dupbv( out, in );
		}
		return 0;
	}
	out->bv_val = LDAP_MALLOCX( l + 1, ctx );
	if ( out->bv_val == NULL ) {
		return -1;
	}
476

477
	for ( i = 0; i < in->bv_len; i++ ) {
478
		char c = in->bv_val[ i ];
479
480
		if ( NEEDFLTESCAPE( c ) ) {
			assert( out->bv_len < l - 2 );
481
482
483
			out->bv_val[out->bv_len++] = '\\';
			out->bv_val[out->bv_len++] = "0123456789ABCDEF"[0x0f & (c>>4)];
			out->bv_val[out->bv_len++] = "0123456789ABCDEF"[0x0f & c];
484

485
		} else {
486
			assert( out->bv_len < l );
487
488
489
490
491
			out->bv_val[out->bv_len++] = c;
		}
	}

	out->bv_val[out->bv_len] = '\0';
492

493
494
495
	return 0;
}