search.c 21.7 KB
Newer Older
1
/* search.c - ldap backend search function */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
5
 * Copyright 1999-2009 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
 * Portions Copyright 1999-2003 Howard Chu.
 * Portions Copyright 2000-2003 Pierangelo Masarati.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * All rights reserved.
 *
 * 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>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by the Howard Chu for inclusion
 * in OpenLDAP Software and subsequently enhanced by Pierangelo
 * Masarati.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
22
 */
23
24
25
26
27
28
29

#include "portable.h"

#include <stdio.h>

#include <ac/socket.h>
#include <ac/string.h>
30
#include <ac/time.h>
31
32
33

#include "slap.h"
#include "back-ldap.h"
34
#undef ldap_debug	/* silence a warning in ldap-int.h */
35
#include "../../../libraries/libldap/ldap-int.h"
36

37
38
#include "lutil.h"

39
40
static int
ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
41
	 struct berval *bdn );
42

43
44
45
46
47
/*
 * Quick'n'dirty rewrite of filter in case of error, to deal with
 * <draft-zeilenga-ldap-t-f>.
 */
static int
48
ldap_back_munge_filter(
49
50
51
	Operation	*op,
	struct berval	*filter )
{
52
	ldapinfo_t	*li = (ldapinfo_t *) op->o_bd->be_private;
53
54
55
56
57
58
59
60
61
62
63
64
65
66

	char		*ptr;
	int		gotit = 0;

	Debug( LDAP_DEBUG_ARGS, "=> ldap_back_munge_filter \"%s\"\n",
			filter->bv_val, 0, 0 );

	for ( ptr = strstr( filter->bv_val, "(?=" ); 
			ptr;
			ptr = strstr( ptr, "(?=" ) )
	{
		static struct berval
			bv_true = BER_BVC( "(?=true)" ),
			bv_false = BER_BVC( "(?=false)" ),
67
			bv_undefined = BER_BVC( "(?=undefined)" ),
68
69
70
71
72
73
74
75
76
77
			bv_t = BER_BVC( "(&)" ),
			bv_f = BER_BVC( "(|)" ),
			bv_T = BER_BVC( "(objectClass=*)" ),
			bv_F = BER_BVC( "(!(objectClass=*))" );
		struct berval	*oldbv = NULL,
				*newbv = NULL,
				oldfilter = BER_BVNULL;

		if ( strncmp( ptr, bv_true.bv_val, bv_true.bv_len ) == 0 ) {
			oldbv = &bv_true;
78
			if ( LDAP_BACK_T_F( li ) ) {
79
80
81
82
83
84
85
86
87
				newbv = &bv_t;

			} else {
				newbv = &bv_T;
			}

		} else if ( strncmp( ptr, bv_false.bv_val, bv_false.bv_len ) == 0 )
		{
			oldbv = &bv_false;
88
			if ( LDAP_BACK_T_F( li ) ) {
89
90
91
92
93
94
				newbv = &bv_f;

			} else {
				newbv = &bv_F;
			}

95
96
		} else if ( strncmp( ptr, bv_undefined.bv_val, bv_undefined.bv_len ) == 0 )
		{
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
97
98
99
100
101
102
103
104
105
106
107
			/* if undef or invalid filter is not allowed,
			 * don't rewrite filter */
			if ( LDAP_BACK_NOUNDEFFILTER( li ) ) {
				if ( filter->bv_val != op->ors_filterstr.bv_val ) {
					op->o_tmpfree( filter->bv_val, op->o_tmpmemctx );
				}
				BER_BVZERO( filter );
				gotit = -1;
				goto done;
			}

108
109
110
			oldbv = &bv_undefined;
			newbv = &bv_F;

111
		} else {
112
113
			gotit = 0;
			goto done;
114
115
116
		}

		oldfilter = *filter;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
117
118
119
120
		filter->bv_len += newbv->bv_len - oldbv->bv_len;
		if ( filter->bv_val == op->ors_filterstr.bv_val ) {
			filter->bv_val = op->o_tmpalloc( filter->bv_len + 1,
					op->o_tmpmemctx );
121

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
122
123
			AC_MEMCPY( filter->bv_val, op->ors_filterstr.bv_val,
					op->ors_filterstr.bv_len + 1 );
124

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
125
126
127
		} else {
			filter->bv_val = op->o_tmprealloc( filter->bv_val,
					filter->bv_len + 1, op->o_tmpmemctx );
128
129
		}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
130
131
		ptr = filter->bv_val + ( ptr - oldfilter.bv_val );

132
133
134
135
136
137
138
139
140
		AC_MEMCPY( &ptr[ newbv->bv_len ],
				&ptr[ oldbv->bv_len ], 
				oldfilter.bv_len - ( ptr - filter->bv_val ) - oldbv->bv_len + 1 );
		AC_MEMCPY( ptr, newbv->bv_val, newbv->bv_len );

		ptr += newbv->bv_len;
		gotit = 1;
	}

141
done:;
142
143
144
145
146
147
	Debug( LDAP_DEBUG_ARGS, "<= ldap_back_munge_filter \"%s\" (%d)\n",
			filter->bv_val, gotit, 0 );

	return gotit;
}

148
149
int
ldap_back_search(
150
151
		Operation	*op,
		SlapReply	*rs )
152
{
153
154
	ldapinfo_t	*li = (ldapinfo_t *) op->o_bd->be_private;

155
	ldapconn_t	*lc = NULL;
156
	struct timeval	tv;
157
	time_t		stoptime = (time_t)(-1);
158
159
160
161
	LDAPMessage	*res,
			*e;
	int		rc = 0,
			msgid; 
162
163
	struct berval	match = BER_BVNULL,
			filter = BER_BVNULL;
164
	int		i;
165
	char		**attrs = NULL;
166
	int		freetext = 0;
167
	int		do_retry = 1, dont_retry = 0;
168
	LDAPControl	**ctrls = NULL;
169
170
	char		**references = NULL;

171
172
	/* FIXME: shouldn't this be null? */
	const char	*save_matched = rs->sr_matched;
173

174
	if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
175
		return rs->sr_err;
176
	}
177

178
179
180
181
182
	/*
	 * FIXME: in case of values return filter, we might want
	 * to map attrs and maybe rewrite value
	 */

183
	if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
184
		tv.tv_sec = op->ors_tlimit;
185
		tv.tv_usec = 0;
186
		stoptime = op->o_time + op->ors_tlimit;
187

188
	} else {
189
		LDAP_BACK_TV_SET( &tv );
190
	}
191

192
193
194
	if ( op->ors_attrs ) {
		for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ )
			/* just count attrs */ ;
195

196
197
198
199
200
201
202
203
204
205
206
		attrs = ch_malloc( ( i + 1 )*sizeof( char * ) );
		if ( attrs == NULL ) {
			rs->sr_err = LDAP_NO_MEMORY;
			rc = -1;
			goto finish;
		}
	
		for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ ) {
			attrs[ i ] = op->ors_attrs[i].an_name.bv_val;
		}
		attrs[ i ] = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
207
208
	}

209
	ctrls = op->o_ctrls;
210
	rc = ldap_back_controls_add( op, rs, lc, &ctrls );
211
212
	if ( rc != LDAP_SUCCESS ) {
		goto finish;
213
	}
214
215

	/* deal with <draft-zeilenga-ldap-t-f> filters */
216
	filter = op->ors_filterstr;
217
retry:
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
218
	rs->sr_err = ldap_pvt_search( lc->lc_ld, op->o_req_dn.bv_val,
219
			op->ors_scope, filter.bv_val,
220
221
			attrs, op->ors_attrsonly, ctrls, NULL,
			tv.tv_sec ? &tv : NULL,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
222
			op->ors_slimit, op->ors_deref, &msgid );
223

224
	if ( rs->sr_err != LDAP_SUCCESS ) {
225
226
227
228
		switch ( rs->sr_err ) {
		case LDAP_SERVER_DOWN:
			if ( do_retry ) {
				do_retry = 0;
229
				if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) {
230
231
232
					goto retry;
				}
			}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
233

234
235
236
237
238
239
240
241
			if ( lc == NULL ) {
				/* reset by ldap_back_retry ... */
				rs->sr_err = slap_map_api2result( rs );

			} else {
				rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_DONTSEND );
			}
				
242
			goto finish;
243

244
		case LDAP_FILTER_ERROR:
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
245
			if (ldap_back_munge_filter( op, &filter ) > 0 ) {
246
247
				goto retry;
			}
248
249
250
251
252

			/* invalid filters return success with no data */
			rs->sr_err = LDAP_SUCCESS;
			rs->sr_text = NULL;
			goto finish;
253
254
255
256
257
		
		default:
			rs->sr_err = slap_map_api2result( rs );
			rs->sr_text = NULL;
			goto finish;
258
		}
259
	}
260

261
262
263
264
265
266
267
268
	/* if needed, initialize timeout */
	if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
		if ( tv.tv_sec == 0 || tv.tv_sec > li->li_timeout[ SLAP_OP_SEARCH ] ) {
			tv.tv_sec = li->li_timeout[ SLAP_OP_SEARCH ];
			tv.tv_usec = 0;
		}
	}

269
270
271
272
	/* We pull apart the ber result, stuff it into a slapd entry, and
	 * let send_search_entry stuff it back into ber format. Slow & ugly,
	 * but this is necessary for version matching, and for ACL processing.
	 */
Howard Chu's avatar
Howard Chu committed
273

274
	for ( rc = -2; rc != -1; rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ONE, &tv, &res ) )
275
	{
276
		/* check for abandon */
277
		if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( lc ) ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
278
279
280
			if ( rc > 0 ) {
				ldap_msgfree( res );
			}
281
			(void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
282
			rc = SLAPD_ABANDON;
283
284
			goto finish;
		}
285

286
		if ( rc == 0 || rc == -2 ) {
287
			ldap_pvt_thread_yield();
288

289
290
291
292
293
			/* check timeout */
			if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
				if ( rc == 0 ) {
					(void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
					rs->sr_text = "Operation timed out";
294
295
					rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
						LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
296
297
298
299
300
301
302
					goto finish;
				}

			} else {
				LDAP_BACK_TV_SET( &tv );
			}

303
304
305
306
			/* check time limit */
			if ( op->ors_tlimit != SLAP_NO_LIMIT
					&& slap_get_time() > stoptime )
			{
307
				(void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
308
309
310
				rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
				goto finish;
			}
311
			continue;
312

313
		} else {
314
315
316
317
318
			/* only touch when activity actually took place... */
			if ( li->li_idle_timeout && lc ) {
				lc->lc_time = op->o_time;
			}

319
320
321
322
323
324
			/* don't retry any more */
			dont_retry = 1;
		}


		if ( rc == LDAP_RES_SEARCH_ENTRY ) {
325
326
			Entry		ent = { 0 };
			struct berval	bdn = BER_BVNULL;
327

328
329
			do_retry = 0;

330
			e = ldap_first_entry( lc->lc_ld, res );
331
			rc = ldap_build_entry( op, e, &ent, &bdn );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
332
			if ( rc == LDAP_SUCCESS ) {
333
				ldap_get_entry_controls( lc->lc_ld, res, &rs->sr_ctrls );
334
				rs->sr_entry = &ent;
335
				rs->sr_attrs = op->ors_attrs;
336
				rs->sr_operational_attrs = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
337
				rs->sr_flags = 0;
338
				rs->sr_err = LDAP_SUCCESS;
339
				rc = rs->sr_err = send_search_entry( op, rs );
340
341
342
343
344
				if ( rs->sr_ctrls ) {
					ldap_controls_free( rs->sr_ctrls );
					rs->sr_ctrls = NULL;
				}
				rs->sr_entry = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
345
346
				if ( !BER_BVISNULL( &ent.e_name ) ) {
					assert( ent.e_name.bv_val != bdn.bv_val );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
347
					op->o_tmpfree( ent.e_name.bv_val, op->o_tmpmemctx );
348
					BER_BVZERO( &ent.e_name );
349
				}
350
				if ( !BER_BVISNULL( &ent.e_nname ) ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
351
					op->o_tmpfree( ent.e_nname.bv_val, op->o_tmpmemctx );
352
					BER_BVZERO( &ent.e_nname );
353
				}
354
				entry_clean( &ent );
355
			}
356
			ldap_msgfree( res );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
357
358
359
360
361
362
			switch ( rc ) {
			case LDAP_SUCCESS:
			case LDAP_INSUFFICIENT_ACCESS:
				break;

			default:
363
364
365
				if ( rc == LDAP_UNAVAILABLE ) {
					rc = rs->sr_err = LDAP_OTHER;
				} else {
366
					(void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
367
				}
368
369
				goto finish;
			}
370
371

		} else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
372
373
374
375
376
			if ( LDAP_BACK_NOREFS( li ) ) {
				ldap_msgfree( res );
				continue;
			}

377
			do_retry = 0;
378
			rc = ldap_parse_reference( lc->lc_ld, res,
379
					&references, &rs->sr_ctrls, 1 );
380
381
382
383
384

			if ( rc != LDAP_SUCCESS ) {
				continue;
			}

385
386
387
			/* FIXME: there MUST be at least one */
			if ( references && references[ 0 ] && references[ 0 ][ 0 ] ) {
				int		cnt;
388

389
390
				for ( cnt = 0; references[ cnt ]; cnt++ )
					/* NO OP */ ;
391

392
				/* FIXME: there MUST be at least one */
393
394
				rs->sr_ref = op->o_tmpalloc( ( cnt + 1 ) * sizeof( struct berval ),
					op->o_tmpmemctx );
395

396
397
398
399
400
401
				for ( cnt = 0; references[ cnt ]; cnt++ ) {
					ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
				}
				BER_BVZERO( &rs->sr_ref[ cnt ] );

				/* ignore return value by now */
402
				rs->sr_entry = NULL;
403
404
405
406
407
408
409
410
411
				( void )send_search_reference( op, rs );

			} else {
				Debug( LDAP_DEBUG_ANY,
					"%s ldap_back_search: "
					"got SEARCH_REFERENCE "
					"with no referrals\n",
					op->o_log_prefix, 0, 0 );
			}
412
413
414

			/* cleanup */
			if ( references ) {
415
				ber_memvfree( (void **)references );
416
				op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
417
				rs->sr_ref = NULL;
418
				references = NULL;
419
420
			}

421
422
423
			if ( rs->sr_ctrls ) {
				ldap_controls_free( rs->sr_ctrls );
				rs->sr_ctrls = NULL;
424
425
			}

426
		} else if ( rc == LDAP_RES_INTERMEDIATE ) {
427
428
429
430
			/* FIXME: response controls
			 * are passed without checks */
			rc = ldap_parse_intermediate( lc->lc_ld,
				res,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
431
				(char **)&rs->sr_rspoid,
432
433
434
435
436
437
438
439
440
441
				&rs->sr_rspdata,
				&rs->sr_ctrls,
				0 );
			if ( rc != LDAP_SUCCESS ) {
				continue;
			}

			slap_send_ldap_intermediate( op, rs );

			if ( rs->sr_rspoid != NULL ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
442
				ber_memfree( (char *)rs->sr_rspoid );
443
444
445
446
447
448
449
450
451
452
453
454
				rs->sr_rspoid = NULL;
			}

			if ( rs->sr_rspdata != NULL ) {
				ber_bvfree( rs->sr_rspdata );
				rs->sr_rspdata = NULL;
			}

			if ( rs->sr_ctrls != NULL ) {
				ldap_controls_free( rs->sr_ctrls );
				rs->sr_ctrls = NULL;
			}
455

456
		} else {
457
			char		*err = NULL;
458

459
			rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
460
					&match.bv_val, &err,
461
462
					&references, &rs->sr_ctrls, 1 );
			if ( rc != LDAP_SUCCESS ) {
463
464
				rs->sr_err = rc;
			}
465
			rs->sr_err = slap_map_api2result( rs );
466
467
468
469
			if ( err ) {
				rs->sr_text = err;
				freetext = 1;
			}
470

471
472
473
474
475
476
			/* RFC 4511: referrals can only appear
			 * if result code is LDAP_REFERRAL */
			if ( references 
				&& references[ 0 ]
				&& references[ 0 ][ 0 ] )
			{
477
478
479
				if ( rs->sr_err != LDAP_REFERRAL ) {
					Debug( LDAP_DEBUG_ANY,
						"%s ldap_back_search: "
480
						"got referrals with err=%d\n",
481
482
483
						op->o_log_prefix,
						rs->sr_err, 0 );

484
485
486
487
488
				} else {
					int	cnt;

					for ( cnt = 0; references[ cnt ]; cnt++ )
						/* NO OP */ ;
489
				
490
491
					rs->sr_ref = op->o_tmpalloc( ( cnt + 1 ) * sizeof( struct berval ),
						op->o_tmpmemctx );
492

493
494
495
496
497
					for ( cnt = 0; references[ cnt ]; cnt++ ) {
						/* duplicating ...*/
						ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
					}
					BER_BVZERO( &rs->sr_ref[ cnt ] );
498
				}
499
500
501
502
503
504
505
506
507
508

			} else if ( rs->sr_err == LDAP_REFERRAL ) {
				Debug( LDAP_DEBUG_ANY,
					"%s ldap_back_search: "
					"got err=%d with null "
					"or empty referrals\n",
					op->o_log_prefix,
					rs->sr_err, 0 );

				rs->sr_err = LDAP_NO_SUCH_OBJECT;
509
			}
510

511
			if ( match.bv_val != NULL ) {
512
				match.bv_len = strlen( match.bv_val );
513
514
			}

515
516
			rc = 0;
			break;
517
		}
518
519
520
521
522
523
524
525

		/* if needed, restore timeout */
		if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
			if ( tv.tv_sec == 0 || tv.tv_sec > li->li_timeout[ SLAP_OP_SEARCH ] ) {
				tv.tv_sec = li->li_timeout[ SLAP_OP_SEARCH ];
				tv.tv_usec = 0;
			}
		}
526
527
	}

528
 	if ( rc == -1 && dont_retry == 0 ) {
529
530
		if ( do_retry ) {
			do_retry = 0;
531
			if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) {
532
				goto retry;
533
			}
534
		}
535
		rs->sr_err = LDAP_SERVER_DOWN;
536
537
		rs->sr_err = slap_map_api2result( rs );
		goto finish;
538
	}
539

Pierangelo Masarati's avatar
Pierangelo Masarati committed
540
541
542
	/*
	 * Rewrite the matched portion of the search base, if required
	 */
543
	if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
544
545
546
547
		struct berval	pmatch;

		if ( dnPretty( NULL, &match, &pmatch, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
			rs->sr_matched = pmatch.bv_val;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
548
			LDAP_FREE( match.bv_val );
549
550
551
552

		} else {
			rs->sr_matched = match.bv_val;
		}
553
	}
554

555
556
557
	if ( rs->sr_v2ref ) {
		rs->sr_err = LDAP_REFERRAL;
	}
558

559
finish:;
560
	if ( LDAP_BACK_QUARANTINE( li ) ) {
561
		ldap_back_quarantine( op, rs );
562
563
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
564
565
566
567
	if ( filter.bv_val != op->ors_filterstr.bv_val ) {
		op->o_tmpfree( filter.bv_val, op->o_tmpmemctx );
	}

568
569
570
571
572
#if 0
	/* let send_ldap_result play cleanup handlers (ITS#4645) */
	if ( rc != SLAPD_ABANDON )
#endif
	{
573
574
		send_ldap_result( op, rs );
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
575

576
	(void)ldap_back_controls_free( op, rs, &ctrls );
577

578
579
580
581
582
	if ( rs->sr_ctrls ) {
		ldap_controls_free( rs->sr_ctrls );
		rs->sr_ctrls = NULL;
	}

583
	if ( rs->sr_matched != NULL && rs->sr_matched != save_matched ) {
584
		if ( rs->sr_matched != match.bv_val ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
585
			ber_memfree_x( (char *)rs->sr_matched, op->o_tmpmemctx );
586
587
588
589

		} else {
			LDAP_FREE( match.bv_val );
		}
590
		rs->sr_matched = save_matched;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
591
	}
592

593
	if ( rs->sr_text ) {
594
		if ( freetext ) {
595
596
			LDAP_FREE( (char *)rs->sr_text );
		}
597
		rs->sr_text = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
598
	}
599
600

	if ( rs->sr_ref ) {
601
		op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
602
603
604
		rs->sr_ref = NULL;
	}

605
606
607
608
	if ( references ) {
		ber_memvfree( (void **)references );
	}

609
610
	if ( attrs ) {
		ch_free( attrs );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
611
	}
612

613
	if ( lc != NULL ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
614
		ldap_back_release_conn( li, lc );
615
616
	}

617
	return rs->sr_err;
618
619
}

620
static int
621
ldap_build_entry(
622
623
624
		Operation	*op,
		LDAPMessage	*e,
		Entry		*ent,
625
		struct berval	*bdn )
626
{
627
628
629
630
631
	struct berval	a;
	BerElement	ber = *e->lm_ber;
	Attribute	*attr, **attrp;
	const char	*text;
	int		last;
632
633
	char *lastb;
	ber_len_t len;
634

635
	/* safe assumptions ... */
636
	assert( ent != NULL );
637
	BER_BVZERO( &ent->e_bv );
638

639
	if ( ber_scanf( &ber, "{m", bdn ) == LBER_ERROR ) {
640
		return LDAP_DECODING_ERROR;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
641
642
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
643
644
645
646
647
	/*
	 * Note: this may fail if the target host(s) schema differs
	 * from the one known to the meta, and a DN with unknown
	 * attributes is returned.
	 * 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
648
	 * FIXME: should we log anything, or delegate to dnNormalize?
Pierangelo Masarati's avatar
Pierangelo Masarati committed
649
	 */
650
651
652
	/* Note: if the distinguished values or the naming attributes
	 * change, should we massage them as well?
	 */
653
	if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
654
655
		op->o_tmpmemctx ) != LDAP_SUCCESS )
	{
656
		return LDAP_INVALID_DN_SYNTAX;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
657
	}
658

659
660
661
662
	ent->e_attrs = NULL;
	if ( ber_first_element( &ber, &len, &lastb ) != LBER_SEQUENCE ) {
		return LDAP_SUCCESS;
	}
663

664
665
666
	attrp = &ent->e_attrs;
	while ( ber_next_element( &ber, &len, lastb ) == LBER_SEQUENCE &&
		ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
667
668
669
		int				i;
		slap_syntax_validate_func	*validate;
		slap_syntax_transform_func	*pretty;
670

671
		attr = attr_alloc( NULL );
672
		if ( attr == NULL ) {
673
			return LDAP_OTHER;
674
675
676
677
		}
		if ( slap_bv2ad( &a, &attr->a_desc, &text ) 
				!= LDAP_SUCCESS )
		{
678
679
			if ( slap_bv2undef_ad( &a, &attr->a_desc, &text,
				SLAP_AD_PROXIED ) != LDAP_SUCCESS )
680
			{
681
				Debug( LDAP_DEBUG_ANY, 
682
683
684
					"%s ldap_build_entry: "
					"slap_bv2undef_ad(%s): %s\n",
					op->o_log_prefix, a.bv_val, text );
685
686

				( void )ber_scanf( &ber, "x" /* [W] */ );
687
				attr_free( attr );
688
689
				continue;
			}
690
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
691
692

		/* no subschemaSubentry */
693
694
695
		if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
			|| attr->a_desc == slap_schema.si_ad_entryDN )
		{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
696
697
698
699
700
701
702

			/* 
			 * We eat target's subschemaSubentry because
			 * a search for this value is likely not
			 * to resolve to the appropriate backend;
			 * later, the local subschemaSubentry is
			 * added.
703
704
705
706
			 *
			 * We also eat entryDN because the frontend
			 * will reattach it without checking if already
			 * present...
707
			 */
708
			( void )ber_scanf( &ber, "x" /* [W] */ );
709
			attr_free( attr );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
710
711
712
			continue;
		}
		
713
		if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
714
715
				|| attr->a_vals == NULL )
		{
716
717
718
719
			/*
			 * Note: attr->a_vals can be null when using
			 * values result filter
			 */
720
			attr->a_vals = (struct berval *)&slap_dummy_bv;
721
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
722

723
724
725
726
727
728
729
730
731
		validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
		pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;

		if ( !validate && !pretty ) {
			attr->a_nvals = NULL;
			attr_free( attr );
			goto next_attr;
		}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
732
733
734
735
		for ( i = 0; !BER_BVISNULL( &attr->a_vals[i] ); i++ ) ;
		last = i;

		for ( i = 0; i<last; i++ ) {
736
737
738
739
740
741
			struct berval	pval;
			int		rc;

			if ( pretty ) {
				rc = pretty( attr->a_desc->ad_type->sat_syntax,
					&attr->a_vals[i], &pval, NULL );
742

743
744
745
746
747
748
			} else {
				rc = validate( attr->a_desc->ad_type->sat_syntax,
					&attr->a_vals[i] );
			}

			if ( rc != LDAP_SUCCESS ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
749
750
				ObjectClass *oc;

751
752
				/* check if, by chance, it's an undefined objectClass */
				if ( attr->a_desc == slap_schema.si_ad_objectClass &&
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
753
						( oc = oc_bvfind_undef( &attr->a_vals[i] ) ) != NULL )
754
				{
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
755
					ber_dupbv( &pval, &oc->soc_cname );
756
757

				} else {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
758
759
760
761
762
763
764
					LBER_FREE( attr->a_vals[i].bv_val );
					if ( --last == i ) {
						BER_BVZERO( &attr->a_vals[i] );
						break;
					}
					attr->a_vals[i] = attr->a_vals[last];
					BER_BVZERO( &attr->a_vals[last] );
765
				}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
766
			} else if ( pretty ) {
767
768
769
770
				LBER_FREE( attr->a_vals[i].bv_val );
				attr->a_vals[i] = pval;
			}
		}
771
		attr->a_numvals = last = i;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
772
773
774
775
776
		if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
			attr->a_nvals = NULL;
			attr_free( attr );
			goto next_attr;
		}
777
778
779
780
781
782
783
784
785
786
787
788
789

		if ( last && attr->a_desc->ad_type->sat_equality &&
				attr->a_desc->ad_type->sat_equality->smr_normalize )
		{
			attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
			for ( i = 0; i < last; i++ ) {
				int		rc;

				/*
				 * check that each value is valid per syntax
				 * and pretty if appropriate
				 */
				rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
Howard Chu's avatar
Howard Chu committed
790
791
792
					SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
					attr->a_desc->ad_type->sat_syntax,
					attr->a_desc->ad_type->sat_equality,
Howard Chu's avatar
Howard Chu committed
793
					&attr->a_vals[i], &attr->a_nvals[i],
Pierangelo Masarati's avatar
Pierangelo Masarati committed
794
					NULL );
795
796

				if ( rc != LDAP_SUCCESS ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
797
798
799
800
801
802
803
					LBER_FREE( attr->a_vals[i].bv_val );
					if ( --last == i ) {
						BER_BVZERO( &attr->a_vals[i] );
						break;
					}
					attr->a_vals[i] = attr->a_vals[last];
					BER_BVZERO( &attr->a_vals[last] );
804
				}
Howard Chu's avatar
Howard Chu committed
805
			}
806
			BER_BVZERO( &attr->a_nvals[i] );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
807
808
809
810
			if ( last == 0 ) {
				attr_free( attr );
				goto next_attr;
			}
811

Howard Chu's avatar
Howard Chu committed
812
		} else {
813
			attr->a_nvals = attr->a_vals;
Howard Chu's avatar
Howard Chu committed
814
		}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
815
816

		attr->a_numvals = last;
817
818
		*attrp = attr;
		attrp = &attr->a_next;
819
820

next_attr:;
821
	}
822

823
824
825
826
827
828
829
	return LDAP_SUCCESS;
}

/* return 0 IFF we can retrieve the entry with ndn
 */
int
ldap_back_entry_get(
830
831
832
833
834
		Operation		*op,
		struct berval		*ndn,
		ObjectClass		*oc,
		AttributeDescription	*at,
		int			rw,
835
		Entry			**ent )
836
{
837
838
	ldapinfo_t	*li = (ldapinfo_t *) op->o_bd->be_private;

839
	ldapconn_t	*lc = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
840
	int		rc,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
841
			do_not_cache;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
842
	ber_tag_t	tag;
843
844
845
	struct berval	bdn;
	LDAPMessage	*result = NULL,
			*e = NULL;
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
846
	char		*attr[3], **attrp = NULL;
847
848
849
	char		*filter = NULL;
	SlapReply	rs;
	int		do_retry = 1;
850
	LDAPControl	**ctrls = NULL;
851

Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
852
853
	*ent = NULL;

854
	/* Tell getconn this is a privileged op */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
855
	do_not_cache = op->o_do_not_cache;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
856
857
	tag = op->o_tag;
	/* do not cache */
858
	op->o_do_not_cache = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
859
860
861
862
863
864
865
	/* ldap_back_entry_get() is an entry lookup, so it does not need
	 * to know what the entry is being looked up for */
	op->o_tag = LDAP_REQ_SEARCH;
	rc = ldap_back_dobind( &lc, op, &rs, LDAP_BACK_DONTSEND );
	op->o_do_not_cache = do_not_cache;
	op->o_tag = tag;
	if ( !rc ) {
866
		return rs.sr_err;
867
868
	}

869
	if ( at ) {
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
870
		attrp = attr;
871
		if ( oc && at != slap_schema.si_ad_objectClass ) {
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
872
873
874
			attr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
			attr[1] = at->ad_cname.bv_val;
			attr[2] = NULL;
875

876
		} else {
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
877
878
			attr[0] = at->ad_cname.bv_val;
			attr[1] = NULL;
879
		}
880
881
	}

882
883
884
	if ( oc ) {
		char	*ptr;

885
886
887
		filter = op->o_tmpalloc( STRLENOF( "(objectClass=" ")" ) 
				+ oc->soc_cname.bv_len + 1, op->o_tmpmemctx );
		ptr = lutil_strcopy( filter, "(objectClass=" );
888
		ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
889
890
891
		*ptr++ = ')';
		*ptr++ = '\0';
	}
892

893
retry:
894
	ctrls = op->o_ctrls;
895
	rc = ldap_back_controls_add( op, &rs, lc, &ctrls );
896
897
898
	if ( rc != LDAP_SUCCESS ) {
		goto cleanup;
	}
899
900

	/* TODO: timeout? */
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
901
	rc = ldap_pvt_search_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
902
				attrp, 0, ctrls, NULL,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
903
				NULL, LDAP_NO_LIMIT, op->ors_deref, &result );
904
	if ( rc != LDAP_SUCCESS ) {
905
906
		if ( rc == LDAP_SERVER_DOWN && do_retry ) {
			do_retry = 0;
907
			if ( ldap_back_retry( &lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
908
				/* if the identity changed, there might be need to re-authz */
909
				(void)ldap_back_controls_free( op, &rs, &ctrls );
910
911
				goto retry;
			}
912
		}
913
914
915
		goto cleanup;
	}

916
	e = ldap_first_entry( lc->lc_ld, result );
917
	if ( e == NULL ) {
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
918
		/* the entry exists, but it doesn't match the filter? */
919
920
921
		goto cleanup;
	}

922
	*ent = entry_alloc();
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
923
924
925
926
	if ( *ent == NULL ) {
		rc = LDAP_NO_MEMORY;
		goto cleanup;
	}
927

928
	rc = ldap_build_entry( op, e, *ent, &bdn );
929

930
	if ( rc != LDAP_SUCCESS ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
931
		entry_free( *ent );
932
933
934
935
		*ent = NULL;
	}

cleanup:
936
	(void)ldap_back_controls_free( op, &rs, &ctrls );
937

938
939
	if ( result ) {
		ldap_msgfree( result );
940
941
	}

942
	if ( filter ) {
943
		op->o_tmpfree( filter, op->o_tmpmemctx );