dynlist.c 68.9 KB
Newer Older
1
/* dynlist.c - dynamic list overlay */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
2
/* $OpenLDAP$ */
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 2003-2020 The OpenLDAP Foundation.
6
 * Portions Copyright 2004-2005 Pierangelo Masarati.
7
 * Portions Copyright 2008 Emmanuel Dreyfus.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * 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 Pierangelo Masarati
 * for SysNet s.n.c., for inclusion in OpenLDAP Software.
 */

#include "portable.h"

#ifdef SLAPD_OVER_DYNLIST

27
#if SLAPD_OVER_DYNGROUP != SLAPD_MOD_STATIC
28
#define TAKEOVER_DYNGROUP
29
#endif
30

31
32
33
34
35
#include <stdio.h>

#include <ac/string.h>

#include "slap.h"
36
#include "config.h"
37
38
#include "lutil.h"

39
static AttributeDescription *ad_dgIdentity, *ad_dgAuthz;
40
static AttributeDescription *ad_memberOf;
Howard Chu's avatar
Howard Chu committed
41

42
typedef struct dynlist_map_t {
43
44
	AttributeDescription	*dlm_member_ad;
	AttributeDescription	*dlm_mapped_ad;
Howard Chu's avatar
Howard Chu committed
45
	AttributeDescription	*dlm_memberOf_ad;
46
47
	ObjectClass				*dlm_static_oc;
	int						 dlm_memberOf_nested;
Howard Chu's avatar
Howard Chu committed
48
49
	int						 dlm_member_oper;
	int						 dlm_memberOf_oper;
50
	struct dynlist_map_t	*dlm_next;
51
52
} dynlist_map_t;

53
typedef struct dynlist_info_t {
54
55
	ObjectClass		*dli_oc;
	AttributeDescription	*dli_ad;
56
	struct dynlist_map_t	*dli_dlm;
57
58
59
60
	struct berval		dli_uri;
	LDAPURLDesc		*dli_lud;
	struct berval		dli_uri_nbase;
	Filter			*dli_uri_filter;
61
	struct berval		dli_default_filter;
62
63
	struct dynlist_info_t	*dli_next;
} dynlist_info_t;
64

Howard Chu's avatar
Howard Chu committed
65
66
typedef struct dynlist_gen_t {
	dynlist_info_t	*dlg_dli;
67
	int				 dlg_memberOf;
Howard Chu's avatar
Howard Chu committed
68
69
} dynlist_gen_t;

70
#define DYNLIST_USAGE \
71
	"\"dynlist-attrset <oc> [uri] <URL-ad> [[<mapped-ad>:]<member-ad>[+<memberOf-ad>[@<static-oc>[*]] ...]\": "
72

73
74
static int
ad_infilter( AttributeDescription *ad, Filter *f )
75
{
76
77
	if ( !f )
		return 0;
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	switch( f->f_choice & SLAPD_FILTER_MASK ) {
	case SLAPD_FILTER_COMPUTED:
		return 0;
	case LDAP_FILTER_PRESENT:
		return f->f_desc == ad;
	case LDAP_FILTER_EQUALITY:
	case LDAP_FILTER_GE:
	case LDAP_FILTER_LE:
	case LDAP_FILTER_APPROX:
	case LDAP_FILTER_SUBSTRINGS:
	case LDAP_FILTER_EXT:
		return f->f_av_desc == ad;
	case LDAP_FILTER_AND:
	case LDAP_FILTER_OR:
	case LDAP_FILTER_NOT: {
		for ( f = f->f_list; f; f = f->f_next )
			if ( ad_infilter( ad, f ))
				return 1;
97
		}
98
	}
99
	return 0;
100
101
102
}

static int
Howard Chu's avatar
Howard Chu committed
103
dynlist_make_filter( Operation *op, Entry *e, dynlist_info_t *dli, const char *url, struct berval *oldf, struct berval *newf )
104
105
{
	char		*ptr;
106
	int		needBrackets = 0;
107

108
109
	assert( oldf != NULL );
	assert( newf != NULL );
110
111
112
	assert( !BER_BVISNULL( oldf ) );
	assert( !BER_BVISEMPTY( oldf ) );

113
	if ( oldf->bv_val[0] != '(' ) {
Howard Chu's avatar
Howard Chu committed
114
		Debug( LDAP_DEBUG_ANY, "%s: dynlist, DN=\"%s\": missing parentheses in URI=\"%s\" filter\n",
115
			op->o_log_prefix, e->e_name.bv_val, url );
116
117
118
		needBrackets = 2;
	}

119
	newf->bv_len = STRLENOF( "(&(!(objectClass=" "))" ")" )
120
		+ dli->dli_oc->soc_cname.bv_len + oldf->bv_len + needBrackets;
121
122
123
124
125
126
127
	newf->bv_val = op->o_tmpalloc( newf->bv_len + 1, op->o_tmpmemctx );
	if ( newf->bv_val == NULL ) {
		return -1;
	}
	ptr = lutil_strcopy( newf->bv_val, "(&(!(objectClass=" );
	ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
	ptr = lutil_strcopy( ptr, "))" );
128
	if ( needBrackets ) *ptr++ = '(';
129
	ptr = lutil_strcopy( ptr, oldf->bv_val );
130
	if ( needBrackets ) *ptr++ = ')';
131
132
133
134
135
136
	ptr = lutil_strcopy( ptr, ")" );
	newf->bv_len = ptr - newf->bv_val;

	return 0;
}

137
/* dynlist_sc_update() callback info set by dynlist_prepare_entry() */
138
typedef struct dynlist_sc_t {
139
	dynlist_info_t    *dlc_dli;
140
	Entry		*dlc_e;
141
	char		**dlc_attrs;
142
143
144
145
146
147
148
149
150
151
152
153
} dynlist_sc_t;

static int
dynlist_sc_update( Operation *op, SlapReply *rs )
{
	Entry			*e;
	Attribute		*a;
	int			opattrs,
				userattrs;
	AccessControlState	acl_state = ACL_STATE_INIT;

	dynlist_sc_t		*dlc;
154
	dynlist_map_t		*dlm;
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

	if ( rs->sr_type != REP_SEARCH ) {
		return 0;
	}

	dlc = (dynlist_sc_t *)op->o_callback->sc_private;
	e = dlc->dlc_e;

	assert( e != NULL );
	assert( rs->sr_entry != NULL );

	/* test access to entry */
	if ( !access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
				NULL, ACL_READ, NULL ) )
	{
		goto done;
	}

173
174
175
	/* if there is only one member_ad, and it's not mapped,
	 * consider it as old-style member listing */
	dlm = dlc->dlc_dli->dli_dlm;
176
	if ( dlm && dlm->dlm_mapped_ad == NULL && dlm->dlm_next == NULL && dlc->dlc_attrs == NULL ) {
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
		/* if access allowed, try to add values, emulating permissive
		 * control to silently ignore duplicates */
		if ( access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
					NULL, ACL_READ, NULL ) )
		{
			Modification	mod;
			const char	*text = NULL;
			char		textbuf[1024];
			struct berval	vals[ 2 ], nvals[ 2 ];

			vals[ 0 ] = rs->sr_entry->e_name;
			BER_BVZERO( &vals[ 1 ] );
			nvals[ 0 ] = rs->sr_entry->e_nname;
			BER_BVZERO( &nvals[ 1 ] );

			mod.sm_op = LDAP_MOD_ADD;
193
194
			mod.sm_desc = dlm->dlm_member_ad;
			mod.sm_type = dlm->dlm_member_ad->ad_cname;
195
196
			mod.sm_values = vals;
			mod.sm_nvalues = nvals;
Howard Chu's avatar
Howard Chu committed
197
			mod.sm_numvals = 1;
198
199
200
201
202
203
204
205
206
207
208
209
210

			(void)modify_add_values( e, &mod, /* permissive */ 1,
					&text, textbuf, sizeof( textbuf ) );
		}

		goto done;
	}

	opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
	userattrs = SLAP_USERATTRS( rs->sr_attr_flags );

	for ( a = rs->sr_entry->e_attrs; a != NULL; a = a->a_next ) {
		BerVarray	vals, nvals = NULL;
211
212
		int		i, j,
				is_oc = a->a_desc == slap_schema.si_ad_objectClass;
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

		/* if attribute is not requested, skip it */
		if ( rs->sr_attrs == NULL ) {
			if ( is_at_operational( a->a_desc->ad_type ) ) {
				continue;
			}

		} else {
			if ( is_at_operational( a->a_desc->ad_type ) ) {
				if ( !opattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
				{
					continue;
				}

			} else {
				if ( !userattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
				{
					continue;
				}
			}
		}

		/* test access to attribute */
		if ( op->ors_attrsonly ) {
			if ( !access_allowed( op, rs->sr_entry, a->a_desc, NULL,
						ACL_READ, &acl_state ) )
			{
				continue;
			}
		}

		/* single-value check: keep first only */
		if ( is_at_single_value( a->a_desc->ad_type ) ) {
			if ( attr_find( e->e_attrs, a->a_desc ) != NULL ) {
				continue;
			}
		}

		/* test access to attribute */
Howard Chu's avatar
Howard Chu committed
252
		i = a->a_numvals;
253
254
255
256
257
258
259

		vals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
		if ( a->a_nvals != a->a_vals ) {
			nvals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
		}

		for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
260
261
262
263
264
265
266
267
			if ( is_oc ) {
				ObjectClass	*soc = oc_bvfind( &a->a_vals[i] );

				if ( soc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
					continue;
				}
			}

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
			if ( access_allowed( op, rs->sr_entry, a->a_desc,
						&a->a_nvals[i], ACL_READ, &acl_state ) )
			{
				vals[j] = a->a_vals[i];
				if ( nvals ) {
					nvals[j] = a->a_nvals[i];
				}
				j++;
			}
		}

		/* if access allowed, try to add values, emulating permissive
		 * control to silently ignore duplicates */
		if ( j != 0 ) {
			Modification	mod;
			const char	*text = NULL;
			char		textbuf[1024];
285
286
			dynlist_map_t	*dlm;
			AttributeDescription *ad;
287
288
289
290
291
292

			BER_BVZERO( &vals[j] );
			if ( nvals ) {
				BER_BVZERO( &nvals[j] );
			}

293
294
295
			ad = a->a_desc;
			for ( dlm = dlc->dlc_dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
				if ( dlm->dlm_member_ad == a->a_desc ) {
296
297
298
					if ( dlm->dlm_mapped_ad ) {
						ad = dlm->dlm_mapped_ad;
					}
299
300
301
302
					break;
				}
			}

303
			mod.sm_op = LDAP_MOD_ADD;
304
305
			mod.sm_desc = ad;
			mod.sm_type = ad->ad_cname;
306
307
			mod.sm_values = vals;
			mod.sm_nvalues = nvals;
Howard Chu's avatar
Howard Chu committed
308
			mod.sm_numvals = j;
309
310
311
312
313
314
315
316
317
318
319
320
321
322

			(void)modify_add_values( e, &mod, /* permissive */ 1,
					&text, textbuf, sizeof( textbuf ) );
		}

		op->o_tmpfree( vals, op->o_tmpmemctx );
		if ( nvals ) {
			op->o_tmpfree( nvals, op->o_tmpmemctx );
		}
	}

done:;
	if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
		entry_free( rs->sr_entry );
323
		rs->sr_entry = NULL;
324
		rs->sr_flags &= ~REP_ENTRY_MASK;
325
326
327
328
	}

	return 0;
}
Howard Chu's avatar
Howard Chu committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

typedef struct dynlist_name_t {
	struct berval dy_name;
	dynlist_info_t *dy_dli;
	AttributeDescription *dy_staticmember;
	int dy_seen;
	int dy_numuris;
	TAvlnode *dy_subs;
	TAvlnode *dy_sups;
	LDAPURLDesc *dy_uris[];
} dynlist_name_t;

static void
dynlist_urlmembers( Operation *op, dynlist_name_t *dyn, slap_callback *sc )
{
	Operation o = *op;
	LDAPURLDesc *ludp;
	int i;

	o.ors_deref = LDAP_DEREF_NEVER;
	o.ors_limit = NULL;
	o.ors_tlimit = SLAP_NO_LIMIT;
	o.ors_slimit = SLAP_NO_LIMIT;
	o.ors_attrs = NULL;
	o.o_callback = sc;

	for (i=0; i<dyn->dy_numuris; i++) {
		ludp = dyn->dy_uris[i];
		if ( ludp->lud_attrs )
			continue;
		o.o_req_dn.bv_val = ludp->lud_dn;
		o.o_req_dn.bv_len = ludp->lud_port;
		o.o_req_ndn = o.o_req_dn;
		o.ors_scope = ludp->lud_scope;
		o.ors_filter = (Filter *)ludp->lud_filter;
		filter2bv_x( op, o.ors_filter, &o.ors_filterstr );
		o.o_bd = select_backend( &o.o_req_ndn, 1 );
		if ( o.o_bd && o.o_bd->be_search ) {
			SlapReply r = { REP_SEARCH };
			r.sr_attr_flags = slap_attr_flags( o.ors_attrs );
			o.o_managedsait = SLAP_CONTROL_CRITICAL;
			(void)o.o_bd->be_search( &o, &r );
		}
		op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
	}
}

static void
dynlist_nested_memberOf( Entry *e, AttributeDescription *ad, TAvlnode *sups )
{
	TAvlnode *ptr;
	dynlist_name_t *dyn;
	Attribute *a;

	a = attr_find( e->e_attrs, ad );
	for ( ptr = tavl_end( sups, TAVL_DIR_LEFT ); ptr;
		ptr = tavl_next( ptr, TAVL_DIR_RIGHT )) {
		dyn = ptr->avl_data;
		if ( a ) {
			unsigned slot;
			if ( attr_valfind( a, SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX |
				SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
				SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
				&dyn->dy_name, &slot, NULL ) == LDAP_SUCCESS )
				continue;
		}
		attr_merge_one( e, ad, &dyn->dy_name, &dyn->dy_name );
		if ( !a )
			a = attr_find( e->e_attrs, ad );
		if ( dyn->dy_sups )
			dynlist_nested_memberOf( e, ad, dyn->dy_sups );
	}
}

typedef struct dynlist_member_t {
	Entry *dm_e;
	AttributeDescription *dm_ad;
	Modification dm_mod;
	TAvlnode *dm_groups;
	struct berval dm_bv[2];
	struct berval dm_nbv[2];
	const char *dm_text;
	char dm_textbuf[1024];
} dynlist_member_t;

414
static int
Howard Chu's avatar
Howard Chu committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
dynlist_ptr_cmp( const void *c1, const void *c2 )
{
	return ( c1 < c2 ) ? -1 : c1 > c2;
}

static int
dynlist_nested_member_dg( Operation *op, SlapReply *rs )
{
	dynlist_member_t *dm = op->o_callback->sc_private;

	if ( rs->sr_type != REP_SEARCH )
		return LDAP_SUCCESS;

	dm->dm_bv[0] = rs->sr_entry->e_name;
	dm->dm_nbv[0] = rs->sr_entry->e_nname;
	modify_add_values( dm->dm_e, &dm->dm_mod, /* permissive */ 1,
		&dm->dm_text, dm->dm_textbuf, sizeof( dm->dm_textbuf ));

	return LDAP_SUCCESS;
}

static void
dynlist_nested_member( Operation *op, dynlist_member_t *dm, TAvlnode *subs )
{
	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
	TAvlnode *ptr;
	dynlist_name_t *dyn;
	Entry *ne;
	Attribute *a, *b;

	a = attr_find( dm->dm_e->e_attrs, dm->dm_ad );
	if ( !a )
		return;

	for ( ptr = tavl_end( subs, TAVL_DIR_LEFT ); ptr;
		ptr = tavl_next( ptr, TAVL_DIR_RIGHT )) {
		dyn = ptr->avl_data;
		if ( tavl_insert( &dm->dm_groups, dyn, dynlist_ptr_cmp, avl_dup_error ))
			continue;
		if ( overlay_entry_get_ov( op, &dyn->dy_name, NULL, NULL, 0, &ne, on ) != LDAP_SUCCESS || ne == NULL )
			continue;
		b = attr_find( ne->e_attrs, dm->dm_ad );
		if ( b ) {
			dm->dm_mod.sm_values = b->a_vals;
			dm->dm_mod.sm_nvalues = b->a_nvals;
			dm->dm_mod.sm_numvals = b->a_numvals;
			modify_add_values( dm->dm_e, &dm->dm_mod, /* permissive */ 1,
				&dm->dm_text, dm->dm_textbuf, sizeof( dm->dm_textbuf ));
		}
		overlay_entry_release_ov( op, ne, 0, on );
		if ( dyn->dy_numuris ) {
			slap_callback cb = { 0 };
			cb.sc_private = dm;
			BER_BVZERO( &dm->dm_bv[1] );
			BER_BVZERO( &dm->dm_nbv[1] );
			dm->dm_mod.sm_values = dm->dm_bv;
			dm->dm_mod.sm_nvalues = dm->dm_nbv;
			dm->dm_mod.sm_numvals = 1;
			cb.sc_response = dynlist_nested_member_dg;
			dynlist_urlmembers( op, dyn, &cb );
		}
		if ( dyn->dy_subs )
			dynlist_nested_member( op, dm, dyn->dy_subs );
	}
}

static int
dynlist_prepare_entry( Operation *op, SlapReply *rs, dynlist_info_t *dli, dynlist_name_t *dyn )
483
{
Howard Chu's avatar
Howard Chu committed
484
	Attribute	*a, *id = NULL;
485
	slap_callback	cb = { 0 };
486
487
488
489
490
491
	Operation	o = *op;
	struct berval	*url;
	Entry		*e;
	int		opattrs,
			userattrs;
	dynlist_sc_t	dlc = { 0 };
492
	dynlist_map_t	*dlm;
493

Howard Chu's avatar
Howard Chu committed
494
	e = rs->sr_entry;
495
496
497
	a = attrs_find( rs->sr_entry->e_attrs, dli->dli_ad );
	if ( a == NULL ) {
		/* FIXME: error? */
Howard Chu's avatar
Howard Chu committed
498
		goto checkdyn;
499
500
	}

501
502
503
504
	opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
	userattrs = SLAP_USERATTRS( rs->sr_attr_flags );

	/* Don't generate member list if it wasn't requested */
505
	for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
506
		AttributeDescription *ad = dlm->dlm_mapped_ad ? dlm->dlm_mapped_ad : dlm->dlm_member_ad;
507
508
		if ( userattrs || ad_inlist( ad, rs->sr_attrs )
			|| ad_infilter( ad, op->ors_filter ))
509
			break;
510
	}
511
	if ( dli->dli_dlm && !dlm )
Howard Chu's avatar
Howard Chu committed
512
		goto checkdyn;
513

514
515
516
517
518
519
520
521
522
523
524
	if ( ad_dgIdentity && ( id = attrs_find( rs->sr_entry->e_attrs, ad_dgIdentity ))) {
		Attribute *authz = NULL;

		/* if not rootdn and dgAuthz is present,
		 * check if user can be authorized as dgIdentity */
		if ( ad_dgAuthz && !BER_BVISEMPTY( &id->a_nvals[0] ) && !be_isroot( op )
			&& ( authz = attrs_find( rs->sr_entry->e_attrs, ad_dgAuthz ) ) )
		{
			if ( slap_sasl_matches( op, authz->a_nvals,
				&o.o_ndn, &o.o_ndn ) != LDAP_SUCCESS )
			{
Howard Chu's avatar
Howard Chu committed
525
				goto checkdyn;
526
527
528
529
530
531
532
533
			}
		}

		o.o_dn = id->a_vals[0];
		o.o_ndn = id->a_nvals[0];
		o.o_groups = NULL;
	}

534
535
	/* ensure e is modifiable, but do not replace
	 * sr_entry yet since we have pointers into it */
536
537
538
	if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
		e = entry_dup( rs->sr_entry );
	}
539
540
541
542
543
544
545

	dlc.dlc_e = e;
	dlc.dlc_dli = dli;
	cb.sc_private = &dlc;
	cb.sc_response = dynlist_sc_update;

	o.o_callback = &cb;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
546
	o.ors_deref = LDAP_DEREF_NEVER;
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
	o.ors_limit = NULL;
	o.ors_tlimit = SLAP_NO_LIMIT;
	o.ors_slimit = SLAP_NO_LIMIT;

	for ( url = a->a_nvals; !BER_BVISNULL( url ); url++ ) {
		LDAPURLDesc	*lud = NULL;
		int		i, j;
		struct berval	dn;
		int		rc;

		BER_BVZERO( &o.o_req_dn );
		BER_BVZERO( &o.o_req_ndn );
		o.ors_filter = NULL;
		o.ors_attrs = NULL;
		BER_BVZERO( &o.ors_filterstr );

		if ( ldap_url_parse( url->bv_val, &lud ) != LDAP_URL_SUCCESS ) {
			/* FIXME: error? */
			continue;
		}

568
		if ( lud->lud_host != NULL ) {
569
			/* FIXME: host not allowed; reject as illegal? */
570
			Debug( LDAP_DEBUG_ANY, "dynlist_prepare_entry(\"%s\"): "
571
				"illegal URI \"%s\"\n",
572
				e->e_name.bv_val, url->bv_val );
573
574
575
576
577
578
579
580
581
582
			goto cleanup;
		}

		if ( lud->lud_dn == NULL ) {
			/* note that an empty base is not honored in terms
			 * of defaultSearchBase, because select_backend()
			 * is not aware of the defaultSearchBase option;
			 * this can be useful in case of a database serving
			 * the empty suffix */
			BER_BVSTR( &dn, "" );
583

584
585
586
587
588
589
590
591
592
593
		} else {
			ber_str2bv( lud->lud_dn, 0, 0, &dn );
		}
		rc = dnPrettyNormal( NULL, &dn, &o.o_req_dn, &o.o_req_ndn, op->o_tmpmemctx );
		if ( rc != LDAP_SUCCESS ) {
			/* FIXME: error? */
			goto cleanup;
		}
		o.ors_scope = lud->lud_scope;

Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
594
595
		for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
			if ( dlm->dlm_mapped_ad != NULL ) {
596
				break;
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
597
598
599
			}
		}

600
		if ( dli->dli_dlm && !dlm ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
601
602
			/* if ( lud->lud_attrs != NULL ),
			 * the URL should be ignored */
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
			o.ors_attrs = slap_anlist_no_attrs;

		} else if ( lud->lud_attrs == NULL ) {
			o.ors_attrs = rs->sr_attrs;

		} else {
			for ( i = 0; lud->lud_attrs[i]; i++)
				/* just count */ ;

			o.ors_attrs = op->o_tmpcalloc( i + 1, sizeof( AttributeName ), op->o_tmpmemctx );
			for ( i = 0, j = 0; lud->lud_attrs[i]; i++) {
				const char	*text = NULL;
	
				ber_str2bv( lud->lud_attrs[i], 0, 0, &o.ors_attrs[j].an_name );
				o.ors_attrs[j].an_desc = NULL;
				(void)slap_bv2ad( &o.ors_attrs[j].an_name, &o.ors_attrs[j].an_desc, &text );
				/* FIXME: ignore errors... */

621
622
623
				if ( ad_infilter( o.ors_attrs[j].an_desc, op->ors_filter )) {
					/* if referenced in filter, must retrieve */
				} else if ( rs->sr_attrs == NULL ) {
624
625
626
627
628
629
630
631
632
633
					if ( o.ors_attrs[j].an_desc != NULL &&
							is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
					{
						continue;
					}

				} else {
					if ( o.ors_attrs[j].an_desc != NULL &&
							is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
					{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
634
						if ( !opattrs ) {
635
636
637
							continue;
						}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
						if ( !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) ) {
							/* lookup if mapped -- linear search,
							 * not very efficient unless list
							 * is very short */
							for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
								if ( dlm->dlm_member_ad == o.ors_attrs[j].an_desc ) {
									break;
								}
							}

							if ( dlm == NULL ) {
								continue;
							}
						}

653
654
655
656
657
					} else {
						if ( !userattrs && 
								o.ors_attrs[j].an_desc != NULL &&
								!ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
						{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
658
659
660
661
662
663
664
665
666
667
668
669
							/* lookup if mapped -- linear search,
							 * not very efficient unless list
							 * is very short */
							for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
								if ( dlm->dlm_member_ad == o.ors_attrs[j].an_desc ) {
									break;
								}
							}

							if ( dlm == NULL ) {
								continue;
							}
670
671
672
673
674
675
676
677
678
679
680
681
682
						}
					}
				}

				j++;
			}

			if ( j == 0 ) {
				goto cleanup;
			}
		
			BER_BVZERO( &o.ors_attrs[j].an_name );
		}
683
		dlc.dlc_attrs = lud->lud_attrs;
684
685
686
687

		if ( lud->lud_filter == NULL ) {
			ber_dupbv_x( &o.ors_filterstr,
					&dli->dli_default_filter, op->o_tmpmemctx );
688

689
		} else {
Howard Chu's avatar
Howard Chu committed
690
691
692
693
694
695
696
697
698
699
			/* don't allow recursion in lists */
			if ( lud->lud_attrs ) {
				struct berval	flt;
				ber_str2bv( lud->lud_filter, 0, 0, &flt );
				if ( dynlist_make_filter( op, rs->sr_entry, dli, url->bv_val, &flt, &o.ors_filterstr ) ) {
					/* error */
					goto cleanup;
				}
			} else {
				ber_str2bv( lud->lud_filter, 0, 0, &o.ors_filterstr );
700
701
702
703
704
705
706
			}
		}
		o.ors_filter = str2filter_x( op, o.ors_filterstr.bv_val );
		if ( o.ors_filter == NULL ) {
			goto cleanup;
		}
		
707
		o.o_bd = select_backend( &o.o_req_ndn, 1 );
708
		if ( o.o_bd && o.o_bd->be_search ) {
709
			SlapReply	r = { REP_SEARCH };
710
			r.sr_attr_flags = slap_attr_flags( o.ors_attrs );
Howard Chu's avatar
Howard Chu committed
711
			o.o_managedsait = SLAP_CONTROL_CRITICAL;
712
713
714
715
			(void)o.o_bd->be_search( &o, &r );
		}

cleanup:;
Howard Chu's avatar
Howard Chu committed
716
717
718
		if ( id ) {
			slap_op_groups_free( &o );
		}
719
		if ( o.ors_filter ) {
720
			filter_free_x( &o, o.ors_filter, 1 );
721
722
723
724
725
726
727
728
729
730
731
732
		}
		if ( o.ors_attrs && o.ors_attrs != rs->sr_attrs
				&& o.ors_attrs != slap_anlist_no_attrs )
		{
			op->o_tmpfree( o.ors_attrs, op->o_tmpmemctx );
		}
		if ( !BER_BVISNULL( &o.o_req_dn ) ) {
			op->o_tmpfree( o.o_req_dn.bv_val, op->o_tmpmemctx );
		}
		if ( !BER_BVISNULL( &o.o_req_ndn ) ) {
			op->o_tmpfree( o.o_req_ndn.bv_val, op->o_tmpmemctx );
		}
Howard Chu's avatar
Howard Chu committed
733
734
735
736
737
738
739
740
		if ( lud->lud_attrs ) {
			assert( BER_BVISNULL( &o.ors_filterstr )
				|| o.ors_filterstr.bv_val != lud->lud_filter );
			op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
		} else {
			if ( o.ors_filterstr.bv_val != lud->lud_filter )
				op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
		}
Howard Chu's avatar
Howard Chu committed
741
		ldap_free_urldesc( lud );
742
743
	}

Howard Chu's avatar
Howard Chu committed
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
checkdyn:
	/* handle nested groups */
	if ( dyn && ( dyn->dy_sups || dyn->dy_subs )) {
		/* ensure e is modifiable */
		if ( e == rs->sr_entry && !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
			e = entry_dup( rs->sr_entry );
			rs_replace_entry( op, rs, (slap_overinst *)op->o_bd->bd_info, e );
			rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
		}
		if ( dyn->dy_subs ) {
			for ( dlm = dyn->dy_dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
				if ( dlm->dlm_member_ad ) {
					dynlist_member_t dm;
					dm.dm_groups = NULL;
					dm.dm_mod.sm_op = LDAP_MOD_ADD;
					dm.dm_mod.sm_desc = dlm->dlm_member_ad;
					dm.dm_mod.sm_type = dlm->dlm_member_ad->ad_cname;
					dm.dm_e = e;
					dm.dm_ad = dlm->dlm_member_ad;
					dynlist_nested_member( op, &dm, dyn->dy_subs );
					if ( dm.dm_groups )
						tavl_free( dm.dm_groups, NULL );
				}
			}
		}
		if ( dyn->dy_sups ) {
			for ( dlm = dyn->dy_dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
				if ( dlm->dlm_memberOf_ad ) {
					dynlist_nested_memberOf( e, dlm->dlm_memberOf_ad, dyn->dy_sups );
				}
			}
		}
	}

778
779
780
781
	if ( e != rs->sr_entry ) {
		rs_replace_entry( op, rs, (slap_overinst *)op->o_bd->bd_info, e );
		rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
	}
782
783
784
785

	return SLAP_CB_CONTINUE;
}

786
787
788
789
790
791
792
/* dynlist_sc_compare_entry() callback set by dynlist_compare() */
typedef struct dynlist_cc_t {
	slap_callback dc_cb;
#	define dc_ava	dc_cb.sc_private /* attr:val to compare with */
	int *dc_res;
} dynlist_cc_t;

793
static int
794
dynlist_sc_compare_entry( Operation *op, SlapReply *rs )
795
{
796
797
798
799
800
801
802
803
804
805
806
807
808
809
	if ( rs->sr_type == REP_SEARCH && rs->sr_entry != NULL ) {
		dynlist_cc_t *dc = (dynlist_cc_t *)op->o_callback;
		AttributeAssertion *ava = dc->dc_ava;
		Attribute *a = attrs_find( rs->sr_entry->e_attrs, ava->aa_desc );

		if ( a != NULL ) {
			while ( LDAP_SUCCESS != attr_valfind( a,
					SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
						SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
					&ava->aa_value, NULL, op->o_tmpmemctx )
				&& (a = attrs_find( a->a_next, ava->aa_desc )) != NULL )
				;
			*dc->dc_res = a ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
		}
810
811
812
813
814
815
816
817
818
	}

	return 0;
}

static int
dynlist_compare( Operation *op, SlapReply *rs )
{
	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
Howard Chu's avatar
Howard Chu committed
819
820
	dynlist_gen_t	*dlg = (dynlist_gen_t *)on->on_bi.bi_private;
	dynlist_info_t	*dli = dlg->dlg_dli;
Howard Chu's avatar
Howard Chu committed
821
	Operation o = *op;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
822
	Entry *e = NULL;
823
	dynlist_map_t *dlm;
824
	BackendDB *be;
Howard Chu's avatar
Howard Chu committed
825
826
827
828
	int ret = SLAP_CB_CONTINUE;

	if ( get_manageDSAit( op ) )
		return SLAP_CB_CONTINUE;
829
830

	for ( ; dli != NULL; dli = dli->dli_next ) {
Howard Chu's avatar
Howard Chu committed
831
832
833
834
835
836
		for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
			AttributeDescription *ad = dlm->dlm_mapped_ad ? dlm->dlm_mapped_ad : dlm->dlm_member_ad;
			/* builtin dyngroup evaluator only works for DNs */
			if ( ad->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName )
				continue;
			if ( op->oq_compare.rs_ava->aa_desc == ad )
837
				break;
Howard Chu's avatar
Howard Chu committed
838
		}
839

840
		if ( dlm ) {
841
842
843
844
			/* This compare is for one of the attributes we're
			 * interested in. We'll use slapd's existing dyngroup
			 * evaluator to get the answer we want.
			 */
845
			BerVarray id = NULL, authz = NULL;
Howard Chu's avatar
Howard Chu committed
846
847
848
849

			o.o_do_not_cache = 1;

			if ( ad_dgIdentity && backend_attribute( &o, NULL, &o.o_req_ndn,
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
				ad_dgIdentity, &id, ACL_READ ) == LDAP_SUCCESS )
			{
				/* if not rootdn and dgAuthz is present,
				 * check if user can be authorized as dgIdentity */
				if ( ad_dgAuthz && !BER_BVISEMPTY( id ) && !be_isroot( op )
					&& backend_attribute( &o, NULL, &o.o_req_ndn,
						ad_dgAuthz, &authz, ACL_READ ) == LDAP_SUCCESS )
				{
					
					rs->sr_err = slap_sasl_matches( op, authz,
						&o.o_ndn, &o.o_ndn );
					ber_bvarray_free_x( authz, op->o_tmpmemctx );
					if ( rs->sr_err != LDAP_SUCCESS ) {
						goto done;
					}
				}

Howard Chu's avatar
Howard Chu committed
867
868
869
870
				o.o_dn = *id;
				o.o_ndn = *id;
				o.o_groups = NULL; /* authz changed, invalidate cached groups */
			}
871

Howard Chu's avatar
Howard Chu committed
872
873
			rs->sr_err = backend_group( &o, NULL, &o.o_req_ndn,
				&o.oq_compare.rs_ava->aa_value, dli->dli_oc, dli->dli_ad );
874
875
876
877
878
879
880
881
882
883
884
885
886
887
			switch ( rs->sr_err ) {
			case LDAP_SUCCESS:
				rs->sr_err = LDAP_COMPARE_TRUE;
				break;

			case LDAP_NO_SUCH_OBJECT:
				/* NOTE: backend_group() returns noSuchObject
				 * if op_ndn does not exist; however, since
				 * dynamic list expansion means that the
				 * member attribute is virtually present, the
				 * non-existence of the asserted value implies
				 * the assertion is FALSE rather than
				 * UNDEFINED */
				rs->sr_err = LDAP_COMPARE_FALSE;
888
				break;
889
			}
890

891
done:;
Howard Chu's avatar
Howard Chu committed
892
893
			if ( id ) ber_bvarray_free_x( id, o.o_tmpmemctx );

Howard Chu's avatar
Howard Chu committed
894
895
			send_ldap_result( op, rs );
			return rs->sr_err;
896
897
898
		}
	}

899
900
901
902
903
	be = select_backend( &o.o_req_ndn, 1 );
	if ( !be || !be->be_search ) {
		return SLAP_CB_CONTINUE;
	}

Howard Chu's avatar
Howard Chu committed
904
	if ( overlay_entry_get_ov( &o, &o.o_req_ndn, NULL, NULL, 0, &e, on ) !=
905
906
		LDAP_SUCCESS || e == NULL )
	{
Howard Chu's avatar
Howard Chu committed
907
908
		return SLAP_CB_CONTINUE;
	}
909

910
	/* check for dynlist objectClass; done if not found */
Howard Chu's avatar
Howard Chu committed
911
	dli = (dynlist_info_t *)dlg->dlg_dli;
912
913
914
915
916
917
918
	while ( dli != NULL && !is_entry_objectclass_or_sub( e, dli->dli_oc ) ) {
		dli = dli->dli_next;
	}
	if ( dli == NULL ) {
		goto release;
	}

Howard Chu's avatar
Howard Chu committed
919
920
921
	if ( ad_dgIdentity ) {
		Attribute *id = attrs_find( e->e_attrs, ad_dgIdentity );
		if ( id ) {
922
923
924
925
926
927
928
929
930
931
932
933
934
935
			Attribute *authz;

			/* if not rootdn and dgAuthz is present,
			 * check if user can be authorized as dgIdentity */
			if ( ad_dgAuthz && !BER_BVISEMPTY( &id->a_nvals[0] ) && !be_isroot( op )
				&& ( authz = attrs_find( e->e_attrs, ad_dgAuthz ) ) )
			{
				if ( slap_sasl_matches( op, authz->a_nvals,
					&o.o_ndn, &o.o_ndn ) != LDAP_SUCCESS )
				{
					goto release;
				}
			}

Howard Chu's avatar
Howard Chu committed
936
937
938
939
940
			o.o_dn = id->a_vals[0];
			o.o_ndn = id->a_nvals[0];
			o.o_groups = NULL;
		}
	}
941

942
943
	/* generate dynamic list with dynlist_response() and compare */
	{
944
		SlapReply	r = { REP_SEARCH };
Howard Chu's avatar
Howard Chu committed
945
946
		Attribute *a;
		AttributeName an[2];
947

948
949
950
951
		o.o_tag = LDAP_REQ_SEARCH;
		o.ors_limit = NULL;
		o.ors_tlimit = SLAP_NO_LIMIT;
		o.ors_slimit = SLAP_NO_LIMIT;
952

953
		o.ors_filterstr = *slap_filterstr_objectClass_pres;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
954
		o.ors_filter = (Filter *) slap_filter_objectClass_pres;
955

956
957
958
959
960
961
962
		o.ors_scope = LDAP_SCOPE_BASE;
		o.ors_deref = LDAP_DEREF_NEVER;
		an[0].an_name = op->orc_ava->aa_desc->ad_cname;
		an[0].an_desc = op->orc_ava->aa_desc;
		BER_BVZERO( &an[1].an_name );
		o.ors_attrs = an;
		o.ors_attrsonly = 0;
Howard Chu's avatar
Howard Chu committed
963
964
		r.sr_entry = e;
		r.sr_attrs = an;
965

966
		o.o_acl_priv = ACL_COMPARE;
Howard Chu's avatar
Howard Chu committed
967
		dynlist_prepare_entry( &o, &r, dli, NULL );
Howard Chu's avatar
Howard Chu committed
968
		a = attrs_find( r.sr_entry->e_attrs, op->orc_ava->aa_desc );
969

Howard Chu's avatar
Howard Chu committed
970
971
972
973
974
975
976
977
978
979
		ret = LDAP_NO_SUCH_ATTRIBUTE;
		for ( ; a ; a = attrs_find( a->a_next, op->orc_ava->aa_desc )) {
			ret = LDAP_COMPARE_FALSE;
			if ( attr_valfind( a,
					SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
						SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
					&op->orc_ava->aa_value, NULL, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
				ret = LDAP_COMPARE_TRUE;
				break;
			}
Howard Chu's avatar
Howard Chu committed
980
		}
Howard Chu's avatar
Howard Chu committed
981
982
983
984
985
		rs->sr_err = ret;

		if ( r.sr_entry != e )
			entry_free( r.sr_entry );
		send_ldap_result( op, rs );
986
987
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
988
989
release:;
	if ( e != NULL ) {
990
		overlay_entry_release_ov( &o, e, 0, on );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
991
992
	}

Howard Chu's avatar
Howard Chu committed
993
994
995
	return ret;
}

Howard Chu's avatar
Howard Chu committed
996
997
#define	WANT_MEMBEROF	1
#define	WANT_MEMBER	2
Howard Chu's avatar
Howard Chu committed
998
999
1000

typedef struct dynlist_search_t {
	TAvlnode *ds_names;
Howard Chu's avatar
Howard Chu committed
1001
	TAvlnode *ds_fnodes;
Howard Chu's avatar
Howard Chu committed
1002
	dynlist_info_t *ds_dli;
1003
	dynlist_map_t *ds_dlm;
1004
1005
	Filter *ds_origfilter;
	struct berval ds_origfilterbv;
Howard Chu's avatar
Howard Chu committed
1006
1007
	int ds_want;
	int ds_found;
Howard Chu's avatar
Howard Chu committed
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
} dynlist_search_t;

static int
dynlist_avl_cmp( const void *c1, const void *c2 )
{
	const dynlist_name_t *n1, *n2;
	int rc;
	n1 = c1; n2 = c2;

	rc = n1->dy_name.bv_len - n2->dy_name.bv_len;
	if ( rc ) return rc;
	return ber_bvcmp( &n1->dy_name, &n2->dy_name );
}

/* build a list of dynamic entries */
static int
dynlist_search1resp( Operation *op, SlapReply *rs )
{
	if ( rs->sr_type == REP_SEARCH && rs->sr_entry != NULL ) {
		dynlist_search_t *ds = op->o_callback->sc_private;
1028
1029
1030
1031
1032
1033
1034
1035
		Attribute *a, *b = NULL;

		if ( ds->ds_dlm && ds->ds_dlm->dlm_static_oc && is_entry_objectclass( rs->sr_entry, ds->ds_dlm->dlm_static_oc, 0 ))
			b = attr_find( rs->sr_entry->e_attrs, ds->ds_dlm->dlm_member_ad );
		a = attr_find( rs->sr_entry->e_attrs, ds->ds_dli->dli_ad );
		if ( a || b ) {
			unsigned len;
			dynlist_name_t *dyn;
1036
1037
			struct berval bv, nbase;
			LDAPURLDesc *ludp;
1038
1039
1040
1041
1042
1043
1044
1045
1046
			int i, j = 0;

			if ( a )
				len = a->a_numvals * sizeof(LDAPURLDesc *);
			else
				len = 0;

			dyn = ch_calloc(1, sizeof(dynlist_name_t)+rs->sr_entry->e_nname.bv_len + 1 + len);
			dyn->dy_name.bv_val = ((char *)(dyn+1)) + len;
1047
1048
			dyn->dy_dli = ds->ds_dli;
			dyn->dy_name.bv_len = rs->sr_entry->e_nname.bv_len;
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
			if ( a ) {
				/* parse and validate the URIs */
				for (i=0; i<a->a_numvals; i++) {
					if (ldap_url_parse( a->a_vals[i].bv_val, &ludp ) != LDAP_URL_SUCCESS )
						continue;
					if (( ludp->lud_host && *ludp->lud_host)
						|| ludp->lud_exts ) {
	skipit:
						ldap_free_urldesc( ludp );
						continue;
					}
					ber_str2bv( ludp->lud_dn, 0, 0, &bv );
					if ( dnNormalize( 0, NULL, NULL, &bv, &nbase, op->o_tmpmemctx ) != LDAP_SUCCESS )
1062
						goto skipit;
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
					ldap_memfree( ludp->lud_dn );
					ludp->lud_dn = ldap_strdup( nbase.bv_val );
					op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
					/* cheat here, reuse fields */
					ludp->lud_port = nbase.bv_len;
					if ( ludp->lud_filter && *ludp->lud_filter ) {
						Filter *f = str2filter( ludp->lud_filter );
						if ( f == NULL )
							goto skipit;
						ldap_memfree( ludp->lud_filter );
						ludp->lud_filter = (char *)f;
					}
					dyn->dy_uris[j] = ludp;
					j++;
1077
1078
1079
1080
				}
			}
			dyn->dy_numuris = j;
			memcpy(dyn->dy_name.bv_val, rs->sr_entry->e_nname.bv_val, rs->sr_entry->e_nname.bv_len );
1081
1082
			if ( b )
				dyn->dy_staticmember = ds->ds_dlm->dlm_member_ad;
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093

			if ( tavl_insert( &ds->ds_names, dyn, dynlist_avl_cmp, avl_dup_error )) {
				for (i=dyn->dy_numuris-1; i>=0; i--) {
					ludp = dyn->dy_uris[i];
					if ( ludp->lud_filter ) {
						filter_free( (Filter *)ludp->lud_filter );
						ludp->lud_filter = NULL;
					}
					ldap_free_urldesc( ludp );
				}
				ch_free( dyn );
Howard Chu's avatar
Howard Chu committed
1094
1095
			} else {
				ds->ds_found++;
1096
1097
			}
		}
Howard Chu's avatar
Howard Chu committed
1098
1099
1100
1101
	}
	return 0;
}

1102
1103
1104
/* replace a filter clause (memberOf=<groupDN>) with an expansion
 * of its dynamic members
 * using (&(entryDN=<groupURIbase>)<groupURIfilter>)
Howard Chu's avatar
Howard Chu committed
1105
1106
 */
static int
Howard Chu's avatar
Howard Chu committed
1107
dynlist_filter_dyngroup( Operation *op, Filter *n, Attribute *a )
Howard Chu's avatar
Howard Chu committed
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
{
	Filter *andf = NULL, *dnf, *urif, *orf = NULL;
	LDAPURLDesc *ludp;
	struct berval bv, nbase;
	int i;

	for (i=0; i<a->a_numvals; i++) {
		if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) != LDAP_URL_SUCCESS )
			continue;
		if (( ludp->lud_host && *ludp->lud_host )
			|| ludp->lud_attrs
			|| ludp->lud_exts ) {
	skip:
			ldap_free_urldesc( ludp );
			continue;
		}
		ber_str2bv( ludp->lud_dn, 0, 0, &bv );
		if ( dnNormalize( 0, NULL, NULL, &bv, &nbase, op->o_tmpmemctx ) != LDAP_SUCCESS )
			goto skip;
		if ( ludp->lud_filter && *ludp->lud_filter ) {
			urif = str2filter_x( op, ludp->lud_filter );
			if ( urif == NULL ) {
				op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
				goto skip;
			}
Howard Chu's avatar
Howard Chu committed
1133
1134
		} else {
			urif = NULL;
Howard Chu's avatar
Howard Chu committed
1135
		}
Howard Chu's avatar
Howard Chu committed
1136
		if ( !andf && n->f_choice == SLAPD_FILTER_COMPUTED ) {
Howard Chu's avatar
Howard Chu committed
1137
			andf = n;
Howard Chu's avatar
Howard Chu committed
1138
			andf->f_next = NULL;
Howard Chu's avatar
Howard Chu committed
1139
		} else {
Howard Chu's avatar
Howard Chu committed
1140
1141
			orf = n;
			if ( n->f_choice != LDAP_FILTER_OR ) {
Howard Chu's avatar
Howard Chu committed
1142
				andf = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
Howard Chu's avatar
Howard Chu committed
1143
				*andf = *n;
Howard Chu's avatar
Howard Chu committed
1144
				orf->f_choice = LDAP_FILTER_OR;
Howard Chu's avatar
Howard Chu committed
1145
				orf->f_next = NULL;
Howard Chu's avatar
Howard Chu committed
1146
1147
				orf->f_list = andf;
			}
Howard Chu's avatar
Howard Chu committed
1148
1149
1150
			andf = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
			andf->f_next = orf->f_list;
			orf->f_list = andf;
Howard Chu's avatar
Howard Chu committed
1151
1152
		}
		dnf = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
Howard Chu's avatar
Howard Chu committed
1153
1154
		andf->f_choice = LDAP_FILTER_AND;
		andf->f_list = dnf;
Howard Chu's avatar
Howard Chu committed
1155
1156
1157
		dnf->f_next = urif;
		if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
			dnf->f_choice = LDAP_FILTER_EQUALITY;
1158
			dnf->f_ava = op->o_tmpcalloc( 1, sizeof(AttributeAssertion), op->o_tmpmemctx );
Howard Chu's avatar
Howard Chu committed
1159
1160
1161
1162
			dnf->f_av_desc = slap_schema.si_ad_entryDN;
			dnf->f_av_value = nbase;
		} else {
			dnf->f_choice = LDAP_FILTER_EXT;
1163
			dnf->f_mra = op->o_tmpcalloc( 1, sizeof(MatchingRuleAssertion), op->o_tmpmemctx );
Howard Chu's avatar
Howard Chu committed
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
			dnf->f_mr_desc = slap_schema.si_ad_entryDN;
			dnf->f_mr_value = nbase;
			switch ( ludp->lud_scope ) {
			case LDAP_SCOPE_ONELEVEL:
				dnf->f_mr_rule = slap_schema.si_mr_dnOneLevelMatch;
				break;
			case LDAP_SCOPE_SUBTREE:
				dnf->f_mr_rule = slap_schema.si_mr_dnSubtreeMatch;
				break;
			case LDAP_SCOPE_SUBORDINATE:
				dnf->f_mr_rule = slap_schema.si_mr_dnSubordinateMatch;
				break;
			}
			ber_str2bv( dnf->f_mr_rule->smr_names[0], 0, 0, &dnf->f_mr_rule_text );
		}
		ldap_free_urldesc( ludp );
	}
Howard Chu's avatar
Howard Chu committed
1181
	if ( !andf )
Howard Chu's avatar
Howard Chu committed
1182
1183
1184
1185
		return -1;
	return 0;
}

1186
1187
1188
1189
1190
/* replace a filter clause (memberOf=<groupDN>) with an expansion
 * of its static members
 * using (|(entryDN=<memberN>)[...])
 */
static int
Howard Chu's avatar
Howard Chu committed
1191
dynlist_filter_stgroup( Operation *op, Filter *n, Attribute *a )
1192
{
Howard Chu's avatar
Howard Chu committed
1193
	Filter *dnf, *orf = NULL;
1194
1195
	int i;

Howard Chu's avatar
Howard Chu committed
1196
	if ( a->a_numvals == 1 && n->f_choice == SLAPD_FILTER_COMPUTED ) {
1197
		dnf = n;
Howard Chu's avatar
Howard Chu committed
1198
		dnf->f_next = NULL;
1199
1200
	} else {
		orf = n;
Howard Chu's avatar
Howard Chu committed
1201
1202
1203
1204
1205
1206
1207
		if ( n->f_choice != LDAP_FILTER_OR ) {
			dnf = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
			*dnf = *n;
			orf->f_choice = LDAP_FILTER_OR;
			orf->f_next = NULL;
			orf->f_list = dnf;
		}
1208
		dnf = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
Howard Chu's avatar
Howard Chu committed
1209
		dnf->f_next = orf->f_list;
1210
1211
1212
1213
		orf->f_list = dnf;
	}

	for (i=0; i<a->a_numvals; i++) {
Howard Chu's avatar
Howard Chu committed
1214
1215
1216
1217
		if ( i ) {
			dnf = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
			dnf->f_next = orf->f_list;
			orf->f_list = dnf;
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
		}
		dnf->f_choice = LDAP_FILTER_EQUALITY;
		dnf->f_ava = op->o_tmpcalloc( 1, sizeof(AttributeAssertion), op->o_tmpmemctx );
		dnf->f_av_desc = slap_schema.si_ad_entryDN;
		ber_dupbv_x( &dnf->f_av_value, &a->a_nvals[i], op->o_tmpmemctx );
	}
	return 0;
}

/* replace a filter clause (memberOf=<groupDN>) with an expansion of
 * its members.
 */
static int
Howard Chu's avatar
Howard Chu committed
1231
dynlist_filter_group( Operation *op, dynlist_name_t *dyn, Filter *n, dynlist_search_t *ds )
1232
1233
1234
1235
1236
1237
{
	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
	Entry *e;
	Attribute *a;
	int rc = -1;

Howard Chu's avatar
Howard Chu committed
1238
1239
1240
1241
	if ( tavl_insert( &ds->ds_fnodes, dyn, dynlist_ptr_cmp, avl_dup_error ))
		return 0;

	if ( overlay_entry_get_ov( op, &dyn->dy_name, NULL, NULL, 0, &e, on ) !=
1242
1243
1244
1245
1246
1247
		LDAP_SUCCESS || e == NULL ) {
		return -1;
	}
	if ( ds->ds_dlm->dlm_static_oc && is_entry_objectclass( e, ds->ds_dlm->dlm_static_oc, 0 )) {
		a = attr_find( e->e_attrs, ds->ds_dlm->dlm_member_ad );
		if ( a ) {
Howard Chu's avatar
Howard Chu committed
1248
			rc = dynlist_filter_stgroup( op, n, a );
1249
1250
1251
1252
		}
	} else {
		a = attr_find( e->e_attrs, ds->ds_dli->dli_ad );
		if ( a ) {
Howard Chu's avatar
Howard Chu committed
1253
			rc = dynlist_filter_dyngroup( op, n, a );
1254
1255
1256
		}
	}
	overlay_entry_release_ov( op, e, 0, on );
Howard Chu's avatar
Howard Chu committed
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
	if ( dyn->dy_subs && !rc ) {
		TAvlnode *ptr;
		for ( ptr = tavl_end( dyn->dy_subs, TAVL_DIR_LEFT ); ptr;
			ptr = tavl_next( ptr, TAVL_DIR_RIGHT )) {
			dyn = ptr->avl_data;
			rc = dynlist_filter_group( op, dyn, n, ds );
			if ( rc )
				break;
		}
	}
1267
1268
1269
1270
	return rc;
}

/* Dup the filter, replacing any references to given ad with group evaluation */
1271
static Filter *
1272
dynlist_filter_dup( Operation *op, Filter *f, AttributeDescription *ad, dynlist_search_t *ds )
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
{
	Filter *n = NULL;

	if ( !f )
		return NULL;

	n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
	n->f_next = NULL;
	switch( f->f_choice & SLAPD_FILTER_MASK ) {
	case SLAPD_FILTER_COMPUTED:
		n->f_choice = f->f_choice;
		n->f_result = f->f_result;
		break;

	case LDAP_FILTER_PRESENT:
Howard Chu's avatar
Howard Chu committed
1288
1289
		n->f_choice = f->f_choice;
		n->f_desc = f->f_desc;
1290
1291
1292
		break;

	case LDAP_FILTER_EQUALITY:
Howard Chu's avatar
Howard Chu committed
1293
1294
1295
1296
1297
1298
		n->f_choice = SLAPD_FILTER_COMPUTED;
		if ( f->f_av_desc == ad ) {
			dynlist_name_t *dyn = tavl_find( ds->ds_names, &f->f_av_value, dynlist_avl_cmp );
			if ( dyn && !dynlist_filter_group( op, dyn, n, ds ))
				break;
		}
Howard Chu's avatar
Howard Chu committed
1299
		/* FALLTHRU */
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
	case LDAP_FILTER_GE:
	case LDAP_FILTER_LE:
	case LDAP_FILTER_APPROX:
		n->f_choice = f->f_choice;
		n->f_ava = f->f_ava;
		break;

	case LDAP_FILTER_SUBSTRINGS:
		n->f_choice = f->f_choice;
		n->f_sub_initial = f->f_sub_initial;
		n->f_sub_final = f->f_sub_final;
		n->f_sub = f->f_sub;
		break;

	case LDAP_FILTER_EXT:
		n->f_choice = f->f_choice;
		n->f_mra = f->f_mra;
		break;

Howard Chu's avatar
Howard Chu committed
1319
	case LDAP_FILTER_NOT:
1320
	case LDAP_FILTER_AND:
Howard Chu's avatar
Howard Chu committed
1321
	case LDAP_FILTER_OR: {
1322
1323
1324
1325
1326
		Filter **p;

		n->f_choice = f->f_choice;

		for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
1327
			*p = dynlist_filter_dup( op, f, ad, ds );
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
			if ( !*p )
				continue;
			p = &(*p)->f_next;
		}
		}
		break;
	}
	return n;
}

static void
dynlist_filter_free( Operation *op, Filter *f )
{
	Filter *p, *next;

	if ( f == NULL )
		return;

	f->f_choice &= SLAPD_FILTER_MASK;
	switch( f->f_choice ) {
	case LDAP_FILTER_AND:
	case LDAP_FILTER_OR:
	case LDAP_FILTER_NOT:
		for ( p = f->f_list; p; p = next ) {
			next = p->f_next;
			op->o_tmpfree( p, op->o_tmpmemctx );
		}
		break;
	default:
		op->o_tmpfree( f, op->o_tmpmemctx );
	}
}

1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
static void
dynlist_search_free( void *ptr )
{
	dynlist_name_t *dyn = (dynlist_name_t *)ptr;
	LDAPURLDesc *ludp;
	int i;

	for (i=dyn->dy_numuris-1; i>=0; i--) {
		ludp = dyn->dy_uris[i];
		if ( ludp->lud_filter ) {
			filter_free( (Filter *)ludp->lud_filter );
			ludp->lud_filter = NULL;
		}
		ldap_free_urldesc( ludp );
	}
Howard Chu's avatar
Howard Chu committed
1376
1377
1378
1379
	if ( dyn->dy_subs )
		tavl_free( dyn->dy_subs, NULL );
	if ( dyn->dy_sups )
		tavl_free( dyn->dy_sups, NULL );
1380
1381
1382
	ch_free( ptr );
}

Howard Chu's avatar
Howard Chu committed
1383
1384
1385
1386
1387
1388
1389
static int