search.c 16.2 KB
Newer Older
1
/* search.c - ldap backend search function */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
7
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
/* This is an altered version */
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
 * 
 * Permission is granted to anyone to use this software for any purpose
 * on any computer system, and to alter it and redistribute it, subject
 * to the following restrictions:
 * 
 * 1. The author is not responsible for the consequences of use of this
 *    software, no matter how awful, even if they arise from flaws in it.
 * 
 * 2. The origin of this software must not be misrepresented, either by
 *    explicit claim or by omission.  Since few users ever read sources,
 *    credits should appear in the documentation.
 * 
 * 3. Altered versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.  Since few users
 *    ever read sources, credits should appear in the documentation.
 * 
 * 4. This notice may not be removed or altered.
27
28
29
30
31
32
33
34
35
 *
 *
 *
 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
 * 
 * This software is being modified by Pierangelo Masarati.
 * The previously reported conditions apply to the modified code as well.
 * Changes in the original code are highlighted where required.
 * Credits for the original code go to the author, Howard Chu.
36
37
38
39
40
41
42
43
 */

#include "portable.h"

#include <stdio.h>

#include <ac/socket.h>
#include <ac/string.h>
44
#include <ac/time.h>
45
46
47

#include "slap.h"
#include "back-ldap.h"
48
#undef ldap_debug	/* silence a warning in ldap-int.h */
49
#include "../../../libraries/libldap/ldap-int.h"
50

51
52
#include "lutil.h"

53
54
55
56
57
58
static int
ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
	 struct berval *bdn, int flags );
#define LDAP_BUILD_ENTRY_PRIVATE	0x01
#define LDAP_BUILD_ENTRY_NORMALIZE	0x02

59
static struct berval dummy = { 0, NULL };
Hallvard Furuseth's avatar
Hallvard Furuseth committed
60

61
62
63
int
ldap_back_search(
    Operation	*op,
64
    SlapReply *rs )
65
{
66
	struct ldapinfo	*li = (struct ldapinfo *) op->o_bd->be_private;
67
68
69
	struct ldapconn *lc;
	struct timeval	tv;
	LDAPMessage		*res, *e;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
70
	int	rc = 0, msgid; 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
71
	struct berval match = { 0, NULL };
72
	char **mapped_attrs = NULL;
Howard Chu's avatar
Howard Chu committed
73
	struct berval mbase;
74
	struct berval mfilter = { 0, NULL };
75
76
	struct slap_limits_set *limit = NULL;
	int isroot = 0;
77
	dncookie dc;
78

Pierangelo Masarati's avatar
Pierangelo Masarati committed
79
	lc = ldap_back_getconn(op, rs);
80
	if ( !lc ) {
81
		return( -1 );
82
	}
83

84
85
86
87
	/*
	 * FIXME: in case of values return filter, we might want
	 * to map attrs and maybe rewrite value
	 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
88
	if ( !ldap_back_dobind( lc, op, rs ) ) {
89
90
91
		return( -1 );
	}

92
	/* if not root, get appropriate limits */
93
	if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
94
95
		isroot = 1;
	} else {
96
		( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
97
98
99
100
	}
	
	/* if no time limit requested, rely on remote server limits */
	/* if requested limit higher than hard limit, abort */
101
	if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
102
		/* no hard limit means use soft instead */
103
104
		if ( limit->lms_t_hard == 0
				&& limit->lms_t_soft > -1
105
106
				&& op->oq_search.rs_tlimit > limit->lms_t_soft ) {
			op->oq_search.rs_tlimit = limit->lms_t_soft;
107
108
109
			
		/* positive hard limit means abort */
		} else if ( limit->lms_t_hard > 0 ) {
110
			rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
111
112
113
114
115
116
117
118
119
			rc = 0;
			goto finish;
		}
		
		/* negative hard limit means no limit */
	}
	
	/* if no size limit requested, rely on remote server limits */
	/* if requested limit higher than hard limit, abort */
120
	if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
121
		/* no hard limit means use soft instead */
122
123
		if ( limit->lms_s_hard == 0
				&& limit->lms_s_soft > -1
124
125
				&& op->oq_search.rs_slimit > limit->lms_s_soft ) {
			op->oq_search.rs_slimit = limit->lms_s_soft;
126
127
128
			
		/* positive hard limit means abort */
		} else if ( limit->lms_s_hard > 0 ) {
129
			rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
130
131
132
133
134
135
136
			rc = 0;
			goto finish;
		}
		
		/* negative hard limit means no limit */
	}

137
	/* should we check return values? */
138
139
140
141
	if (op->oq_search.rs_deref != -1)
		ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->oq_search.rs_deref);
	if (op->oq_search.rs_tlimit != -1) {
		tv.tv_sec = op->oq_search.rs_tlimit;
142
143
144
145
		tv.tv_usec = 0;
	} else {
		tv.tv_sec = 0;
	}
146

Pierangelo Masarati's avatar
Pierangelo Masarati committed
147
148
149
	/*
	 * Rewrite the search base, if required
	 */
150
	dc.rwmap = &li->rwmap;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
151
#ifdef ENABLE_REWRITE
152
153
154
155
156
157
158
159
160
161
	dc.conn = op->o_conn;
	dc.rs = rs;
	dc.ctx = "searchBase";
#else
	dc.tofrom = 1;
	dc.normalized = 0;
#endif
	if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mbase ) ) {
		send_ldap_result( op, rs );
		return -1;
162
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
163

164
165
	rc = ldap_back_filter_map_rewrite( &dc, op->oq_search.rs_filter,
			&mfilter, BACKLDAP_MAP );
166
167

	if ( rc ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
168
169
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "Rewrite error";
170
171
		rc = -1;
		goto finish;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
172
173
	}

174
175
	rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
			op->oq_search.rs_attrs,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
176
177
178
179
180
181
182
			BACKLDAP_MAP, &mapped_attrs );
	if ( rs->sr_err ) {
		rc = -1;
		goto finish;
	}

#if 0
183
	if ( mapped_attrs == NULL && op->oq_search.rs_attrs) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
184
185
186
187
		int count;

		/* this can happen only if ch_calloc() fails
		 * in ldap_back_map_attrs() */
188
		for (count=0; op->oq_search.rs_attrs[count].an_name.bv_val; count++);
Howard Chu's avatar
Howard Chu committed
189
		mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
190
191
		for (count=0; op->oq_search.rs_attrs[count].an_name.bv_val; count++) {
			mapped_attrs[count] = op->oq_search.rs_attrs[count].an_name.bv_val;
Howard Chu's avatar
Howard Chu committed
192
193
		}
		mapped_attrs[count] = NULL;
194
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
195
196
197
198
199
200
201
202
#endif

	rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
			op->oq_search.rs_scope, mfilter.bv_val,
			mapped_attrs, op->oq_search.rs_attrsonly,
			op->o_ctrls, NULL,
			tv.tv_sec ? &tv : NULL, op->oq_search.rs_slimit,
			&msgid);
203
	if ( rs->sr_err != LDAP_SUCCESS ) {
204
fail:;
205
		rc = ldap_back_op_result(lc, op, rs, msgid, 0);
Pierangelo Masarati's avatar
Pierangelo Masarati committed
206
		goto finish;
207
	}
208
209
210
211
212

	/* 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
213

214
	for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
215
	{
216
		/* check for abandon */
Howard Chu's avatar
Howard Chu committed
217
		if (op->o_abandon) {
218
			ldap_abandon(lc->ld, msgid);
Pierangelo Masarati's avatar
Pierangelo Masarati committed
219
			rc = 0;
220
221
			goto finish;
		}
222

223
		if (rc == 0) {
224
225
226
			tv.tv_sec = 0;
			tv.tv_usec = 100000;
			ldap_pvt_thread_yield();
227

228
		} else if (rc == LDAP_RES_SEARCH_ENTRY) {
229
230
			Entry ent;
			struct berval bdn;
231
			e = ldap_first_entry(lc->ld,res);
232
233
			if ( ldap_build_entry(op, e, &ent, &bdn,
						LDAP_BUILD_ENTRY_PRIVATE) == LDAP_SUCCESS ) {
234
235
236
				rs->sr_entry = &ent;
				rs->sr_attrs = op->oq_search.rs_attrs;
				send_search_entry( op, rs );
237
				while (ent.e_attrs) {
238
239
240
					Attribute *a;
					BerVarray v;

241
242
					a = ent.e_attrs;
					ent.e_attrs = a->a_next;
243
244

					v = a->a_vals;
245
246
					if (a->a_vals != &dummy)
						ber_bvarray_free(a->a_vals);
247
					if (a->a_nvals != v)
Howard Chu's avatar
Howard Chu committed
248
						ber_bvarray_free(a->a_nvals);
249
250
251
252
253
254
255
					ch_free(a);
				}
				
				if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
					free( ent.e_dn );
				if ( ent.e_ndn )
					free( ent.e_ndn );
256
			}
257
			ldap_msgfree(res);
258
259
260
261
262
263

		} else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
			char		**references = NULL;
			int		cnt;

			rc = ldap_parse_reference( lc->ld, res,
264
					&references, &rs->sr_ctrls, 1 );
265
266
267
268
269
270
271
272
273
274
275
276

			if ( rc != LDAP_SUCCESS ) {
				continue;
			}

			if ( references == NULL ) {
				continue;
			}

			for ( cnt = 0; references[ cnt ]; cnt++ )
				/* NO OP */ ;
				
277
			rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
278
279

			for ( cnt = 0; references[ cnt ]; cnt++ ) {
280
281
				rs->sr_ref[ cnt ].bv_val = references[ cnt ];
				rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
282
283
284
			}

			/* ignore return value by now */
285
			( void )send_search_reference( op, rs );
286
287
288
289

			/* cleanup */
			if ( references ) {
				ldap_value_free( references );
290
291
				ch_free( rs->sr_ref );
				rs->sr_ref = NULL;
292
293
			}

294
295
296
			if ( rs->sr_ctrls ) {
				ldap_controls_free( rs->sr_ctrls );
				rs->sr_ctrls = NULL;
297
298
			}

299
		} else {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
300
301
302
			rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
					&match.bv_val, (char **)&rs->sr_text,
					NULL, NULL, 1);
303
			if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
304
			rs->sr_err = ldap_back_map_result(rs);
305
306
			rc = 0;
			break;
307
		}
308
309
310
311
312
	}

	if (rc == -1)
		goto fail;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
313
314
315
	/*
	 * Rewrite the matched portion of the search base, if required
	 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
316
317
	if ( match.bv_val && *match.bv_val ) {
		struct berval mdn;
318

319
320
321
322
323
324
#ifdef ENABLE_REWRITE
		dc.ctx = "matchedDn";
#else
		dc.tofrom = 0;
		dc.normalized = 0;
#endif
Pierangelo Masarati's avatar
Pierangelo Masarati committed
325
326
		match.bv_len = strlen( match.bv_val );
		ldap_back_dn_massage(&dc, &match, &mdn);
327
		rs->sr_matched = mdn.bv_val;
328
	}
329
330
331
	if ( rs->sr_v2ref ) {
		rs->sr_err = LDAP_REFERRAL;
	}
332

333
finish:;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
334
335
	send_ldap_result( op, rs );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
336
337
	if ( match.bv_val ) {
		if ( rs->sr_matched != match.bv_val ) {
338
			free( (char *)rs->sr_matched );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
339
		}
340
		rs->sr_matched = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
341
		LDAP_FREE( match.bv_val );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
342
	}
343
344
345
	if ( rs->sr_text ) {
		LDAP_FREE( (char *)rs->sr_text );
		rs->sr_text = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
346
	}
Howard Chu's avatar
Howard Chu committed
347
	if ( mapped_attrs ) {
348
		ch_free( mapped_attrs );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
349
	}
350
	if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
351
		ch_free( mfilter.bv_val );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
352
	}
353
	if ( mbase.bv_val != op->o_req_dn.bv_val ) {
Howard Chu's avatar
Howard Chu committed
354
		free( mbase.bv_val );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
355
356
357
	}
	
	return rc;
358
359
}

360
static int
361
ldap_build_entry(
Pierangelo Masarati's avatar
Pierangelo Masarati committed
362
	Operation *op,
363
	LDAPMessage *e,
364
365
	Entry *ent,
	struct berval *bdn,
366
	int flags
367
368
)
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
369
	struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
370
371
	struct berval a, mapped;
	BerElement ber = *e->lm_ber;
372
	Attribute *attr, **attrp;
373
	struct berval *bv;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
374
	const char *text;
Howard Chu's avatar
Howard Chu committed
375
	int last;
376
377
	int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
	int normalize = flags & LDAP_BUILD_ENTRY_NORMALIZE;
378
	dncookie dc;
379

380
381
382
383
	/* safe assumptions ... */
	assert( ent );
	ent->e_bv.bv_val = NULL;

384
	if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
385
		return LDAP_DECODING_ERROR;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
386
387
388
389
390
	}

	/*
	 * Rewrite the dn of the result, if needed
	 */
391
	dc.rwmap = &li->rwmap;
392
393
394
395
396
397
398
399
400
#ifdef ENABLE_REWRITE
	dc.conn = op->o_conn;
	dc.rs = NULL;
	dc.ctx = "searchResult";
#else
	dc.tofrom = 0;
	dc.normalized = 0;
#endif
	if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
401
		return LDAP_OTHER;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
402
403
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
404
405
406
407
408
	/*
	 * 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
409
	 * FIXME: should we log anything, or delegate to dnNormalize?
Pierangelo Masarati's avatar
Pierangelo Masarati committed
410
	 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
411
412
413
	if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
		op->o_tmpmemctx ) != LDAP_SUCCESS )
	{
414
		return LDAP_INVALID_DN_SYNTAX;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
415
416
	}
	
417
418
419
420
	ent->e_id = 0;
	ent->e_attrs = 0;
	ent->e_private = 0;
	attrp = &ent->e_attrs;
421

422
423
424
#ifdef ENABLE_REWRITE
	dc.ctx = "searchAttrDN";
#endif
425
	while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
426
		ldap_back_map(&li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP);
427
		if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
428
			continue;
429
		attr = (Attribute *)ch_malloc( sizeof(Attribute) );
430
431
		if (attr == NULL)
			continue;
432
		attr->a_flags = 0;
433
		attr->a_next = 0;
434
		attr->a_desc = NULL;
435
436
		if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
			if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text) 
437
438
					!= LDAP_SUCCESS) {
#ifdef NEW_LOGGING
Julius Enarusai's avatar
   
Julius Enarusai committed
439
440
				LDAP_LOG( BACK_LDAP, DETAIL1, 
					"slap_bv2undef_ad(%s):	%s\n", mapped.bv_val, text, 0 );
441
442
#else /* !NEW_LOGGING */
				Debug( LDAP_DEBUG_ANY, 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
443
						"slap_bv2undef_ad(%s):	"
444
 						"%s\n%s", mapped.bv_val, text, "" );
445
446
447
448
#endif /* !NEW_LOGGING */
				ch_free(attr);
				continue;
			}
449
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
450
451
452

		/* no subschemaSubentry */
		if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
453
454
455
456
457
458
459

			/* 
			 * 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.
460
			 */
461
			( void )ber_scanf( &ber, "x" /* [W] */ );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
462

Pierangelo Masarati's avatar
Pierangelo Masarati committed
463
464
465
466
			ch_free(attr);
			continue;
		}
		
467
468
469
470
471
472
		if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
				|| attr->a_vals == NULL ) {
			/*
			 * Note: attr->a_vals can be null when using
			 * values result filter
			 */
473
474
475
476
477
478
479
			if (private) {
				attr->a_vals = &dummy;
			} else {
				attr->a_vals = ch_malloc(sizeof(struct berval));
				attr->a_vals->bv_val = NULL;
				attr->a_vals->bv_len = 0;
			}
Howard Chu's avatar
Howard Chu committed
480
481
482
483
484
485
			last = 0;
		} else {
			for ( last = 0; attr->a_vals[last].bv_val; last++ );
		}
		if ( last == 0 ) {
			/* empty */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
486
487
		} else if ( attr->a_desc == slap_schema.si_ad_objectClass
				|| attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
488
			for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
489
				ldap_back_map(&li->rwmap.rwm_oc, bv, &mapped,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
490
						BACKLDAP_REMAP);
491
				if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
492
					LBER_FREE(bv->bv_val);
493
					bv->bv_val = NULL;
494
495
					if (--last < 0)
						break;
496
					*bv = attr->a_vals[last];
497
					attr->a_vals[last].bv_val = NULL;
498
499
					bv--;

500
				} else if ( mapped.bv_val != bv->bv_val ) {
501
502
503
504
505
506
					/*
					 * FIXME: after LBER_FREEing
					 * the value is replaced by
					 * ch_alloc'ed memory
					 */
					LBER_FREE(bv->bv_val);
507
					ber_dupbv( bv, &mapped );
508
509
				}
			}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
510
511
512
513
514

		/*
		 * It is necessary to try to rewrite attributes with
		 * dn syntax because they might be used in ACLs as
		 * members of groups; since ACLs are applied to the
515
		 * rewritten stuff, no dn-based subject clause could
Pierangelo Masarati's avatar
Pierangelo Masarati committed
516
517
518
519
520
521
		 * be used at the ldap backend side (see
		 * http://www.OpenLDAP.org/faq/data/cache/452.html)
		 * The problem can be overcome by moving the dn-based
		 * ACLs to the target directory server, and letting
		 * everything pass thru the ldap backend.
		 */
522
523
		} else if ( attr->a_desc->ad_type->sat_syntax ==
				slap_schema.si_syn_distinguishedName ) {
524
			ldap_dnattr_result_rewrite( &dc, attr->a_vals );
525

526
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
527

Howard Chu's avatar
Howard Chu committed
528
529
530
531
532
533
534
535
536
537
		if ( normalize && last && attr->a_desc->ad_type->sat_equality &&
			attr->a_desc->ad_type->sat_equality->smr_normalize ) {
			int i;

			attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
			for (i=0; i<last; i++) {
				attr->a_desc->ad_type->sat_equality->smr_normalize(
					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
538
539
					&attr->a_vals[i], &attr->a_nvals[i],
					op->o_tmpmemctx );
Howard Chu's avatar
Howard Chu committed
540
			}
Howard Chu's avatar
Howard Chu committed
541
542
			attr->a_nvals[i].bv_val = NULL;
			attr->a_nvals[i].bv_len = 0;
Howard Chu's avatar
Howard Chu committed
543
		} else {
544
			attr->a_nvals = attr->a_vals;
Howard Chu's avatar
Howard Chu committed
545
		}
546
547
		*attrp = attr;
		attrp = &attr->a_next;
548
	}
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
	/* make sure it's free'able */
	if (!private && ent->e_name.bv_val == bdn->bv_val)
		ber_dupbv( &ent->e_name, bdn );
	return LDAP_SUCCESS;
}

/* return 0 IFF we can retrieve the entry with ndn
 */
int
ldap_back_entry_get(
	Operation *op,
	struct berval	*ndn,
	ObjectClass *oc,
	AttributeDescription *at,
	int rw,
	Entry **ent
)
{
567
	struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;    
568
569
	struct ldapconn *lc;
	int rc = 1, is_oc;
Howard Chu's avatar
Howard Chu committed
570
	struct berval mapped = { 0, NULL }, bdn, mdn;
571
572
	LDAPMessage	*result = NULL, *e = NULL;
	char *gattr[3];
573
	char *filter = NULL;
574
575
	Connection *oconn;
	SlapReply rs;
576
	dncookie dc;
577
578
579
580

	/* Tell getconn this is a privileged op */
	is_oc = op->o_do_not_cache;
	op->o_do_not_cache = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
581
	lc = ldap_back_getconn(op, &rs);
582
583
	oconn = op->o_conn;
	op->o_conn = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
584
	if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
585
		op->o_do_not_cache = is_oc;
586
		op->o_conn = oconn;
587
588
589
		return 1;
	}
	op->o_do_not_cache = is_oc;
590
	op->o_conn = oconn;
591

592
593
594
	/*
	 * Rewrite the search base, if required
	 */
595
	dc.rwmap = &li->rwmap;
596
#ifdef ENABLE_REWRITE
597
598
599
600
601
602
603
604
	dc.conn = op->o_conn;
	dc.rs = &rs;
	dc.ctx = "searchBase";
#else
	dc.tofrom = 1;
	dc.normalized = 1;
#endif
	if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
Howard Chu's avatar
Howard Chu committed
605
		return 1;
606
607
	}

608
	ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
609
	if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
610
611
		rc = 1;
		goto cleanup;
612
613
	}

614
615
616
617
618
619
620
621
622
623
624
	is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
	if (oc && !is_oc) {
		gattr[0] = "objectclass";
		gattr[1] = mapped.bv_val;
		gattr[2] = NULL;
	} else {
		gattr[0] = mapped.bv_val;
		gattr[1] = NULL;
	}
	if (oc) {
		char *ptr;
625
		ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
Howard Chu's avatar
Howard Chu committed
626
627
						BACKLDAP_MAP);
		filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
628
		ptr = lutil_strcopy(filter, "(objectclass=");
Howard Chu's avatar
Howard Chu committed
629
		ptr = lutil_strcopy(ptr, mapped.bv_val);
630
631
632
633
		*ptr++ = ')';
		*ptr++ = '\0';
	}
		
634
	if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
635
636
637
638
639
640
641
642
643
644
				gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
				LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
	{
		goto cleanup;
	}

	if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
		goto cleanup;
	}

Howard Chu's avatar
Howard Chu committed
645
	*ent = ch_calloc(1,sizeof(Entry));
646

647
	rc = ldap_build_entry(op, e, *ent, &bdn, LDAP_BUILD_ENTRY_NORMALIZE);
648
649
650
651
652
653
654
655
656
657
658

	if (rc != LDAP_SUCCESS) {
		ch_free(*ent);
		*ent = NULL;
	}

cleanup:
	if (result) {
		ldap_msgfree(result);
	}

659
660
661
662
	if ( filter ) {
		ch_free( filter );
	}

663
	if ( mdn.bv_val != ndn->bv_val ) {
Howard Chu's avatar
Howard Chu committed
664
		ch_free( mdn.bv_val );
665
666
	}

667
	return(rc);
668
}
669