memberof.c 55.1 KB
Newer Older
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* memberof.c - back-reference for group membership */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2005-2007 Pierangelo Masarati <ando@sys-net.it>
 * 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>.
 */
/* ACKNOWLEDGMENTS:
 * This work was initially developed by Pierangelo Masarati for inclusion
 * in OpenLDAP Software, sponsored by SysNet s.r.l.
 */

#include "portable.h"

#ifdef SLAPD_OVER_MEMBEROF

#include <stdio.h>

#include "ac/string.h"
#include "ac/socket.h"

#include "slap.h"
#include "config.h"
#include "lutil.h"

/*
 *	Glossary:
 *
 *		GROUP		a group object (an entry with GROUP_OC
 *				objectClass)
 *		MEMBER		a member object (an entry whose DN is
 *				listed as MEMBER_AT value of a GROUP)
 *		GROUP_OC	the objectClass of the group object
 *				(default: groupOfNames)
 *		MEMBER_AT	the membership attribute, DN-valued;
 *				note: nameAndOptionalUID is tolerated
 *				as soon as the optionalUID is absent
 *				(default: member)
 *		MEMBER_OF	reverse membership attribute
 *				(default: memberOf)
 *
 * 	- add:
 *		- if the entry that is being added is a GROUP,
 *		  the MEMBER_AT defined as values of the add operation
 *		  get the MEMBER_OF value directly from the request.
 *
 *		  if configured to do so, the MEMBER objects do not exist,
 *		  and no relax control is issued, either:
 *			- fail
 *			- drop non-existing members
 *		  (by default: don't muck with values)
 *
 *		- if (configured to do so,) the referenced GROUP exists,
 *		  the relax control is set and the user has
 *		  "manage" privileges, allow to add MEMBER_OF values to
 *		  generic entries.
 *
 *	- modify:
 *		- if the entry being modified is a GROUP_OC and the 
 *		  MEMBER_AT attribute is modified, the MEMBER_OF value
 *		  of the (existing) MEMBER_AT entries that are affected
 *		  is modified according to the request:
 *			- if a MEMBER is removed from the group,
 *			  delete the corresponding MEMBER_OF
 *			- if a MEMBER is added to a group,
 *			  add the corresponding MEMBER_OF
 *
 *		  We need to determine, from the database, if it is
 *		  a GROUP_OC, and we need to check, from the
 *		  modification list, if the MEMBER_AT attribute is being
 *		  affected, and what MEMBER_AT values are affected.
 *
 *		  if configured to do so, the entries corresponding to
 *		  the MEMBER_AT values do not exist, and no relax control
 *		  is issued, either:
 *			- fail
 *			- drop non-existing members
 *		  (by default: don't muck with values)
 *
 *		- if configured to do so, the referenced GROUP exists,
 *		  (the relax control is set) and the user has
 *		  "manage" privileges, allow to add MEMBER_OF values to
 *		  generic entries; the change is NOT automatically reflected
 *		  in the MEMBER attribute of the GROUP referenced
 *		  by the value of MEMBER_OF; a separate modification,
 *		  with or without relax control, needs to be performed.
 *
 *	- modrdn:
 *		- if the entry being renamed is a GROUP, the MEMBER_OF
 *		  value of the (existing) MEMBER objects is modified
 *		  accordingly based on the newDN of the GROUP.
 *
 *		  We need to determine, from the database, if it is
 *		  a GROUP; the list of MEMBER objects is obtained from
 *		  the database.
 *
 *		  Non-existing MEMBER objects are ignored, since the
 *		  MEMBER_AT is not being addressed by the operation.
 *
 *		- if the entry being renamed has the MEMBER_OF attribute,
 *		  the corresponding MEMBER value must be modified in the
 *		  respective group entries.
 *		
 *
 *	- delete:
 *		- if the entry being deleted is a GROUP, the (existing)
 *		  MEMBER objects are modified accordingly; a copy of the 
 *		  values of the MEMBER_AT is saved and, if the delete 
 *		  succeeds, the MEMBER_OF value of the (existing) MEMBER
 *		  objects is deleted.
 *
 *		  We need to determine, from the database, if it is
 *		  a GROUP.
 *
 *		  Non-existing MEMBER objects are ignored, since the entry
 *		  is being deleted.
 *
 *		- if the entry being deleted has the MEMBER_OF attribute,
 *		  the corresponding value of the MEMBER_AT must be deleted
 *		  from the respective GROUP entries.
 */

#define	SLAPD_MEMBEROF_ATTR	"memberOf"

133
134
135
136
137
static AttributeDescription	*ad_member;
static AttributeDescription	*ad_memberOf;

static ObjectClass			*oc_group;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
static slap_overinst		memberof;

typedef struct memberof_t {
	struct berval		mo_dn;
	struct berval		mo_ndn;

	ObjectClass		*mo_oc_group;
	AttributeDescription	*mo_ad_member;
	AttributeDescription	*mo_ad_memberof;
	
	struct berval		mo_groupFilterstr;
	AttributeAssertion	mo_groupAVA;
	Filter			mo_groupFilter;

	struct berval		mo_memberFilterstr;
	Filter			mo_memberFilter;

	unsigned		mo_flags;
#define	MEMBEROF_NONE		0x00U
#define	MEMBEROF_FDANGLING_DROP	0x01U
#define	MEMBEROF_FDANGLING_ERROR	0x02U
#define	MEMBEROF_FDANGLING_MASK	(MEMBEROF_FDANGLING_DROP|MEMBEROF_FDANGLING_ERROR)
#define	MEMBEROF_FREFINT	0x04U
#define	MEMBEROF_FREVERSE	0x08U

163
164
	ber_int_t		mo_dangling_err;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#define MEMBEROF_CHK(mo,f) \
	(((mo)->mo_flags & (f)) == (f))
#define MEMBEROF_DANGLING_CHECK(mo) \
	((mo)->mo_flags & MEMBEROF_FDANGLING_MASK)
#define MEMBEROF_DANGLING_DROP(mo) \
	MEMBEROF_CHK((mo),MEMBEROF_FDANGLING_DROP)
#define MEMBEROF_DANGLING_ERROR(mo) \
	MEMBEROF_CHK((mo),MEMBEROF_FDANGLING_ERROR)
#define MEMBEROF_REFINT(mo) \
	MEMBEROF_CHK((mo),MEMBEROF_FREFINT)
#define MEMBEROF_REVERSE(mo) \
	MEMBEROF_CHK((mo),MEMBEROF_FREVERSE)
} memberof_t;

typedef enum memberof_is_t {
	MEMBEROF_IS_NONE = 0x00,
	MEMBEROF_IS_GROUP = 0x01,
	MEMBEROF_IS_MEMBER = 0x02,
	MEMBEROF_IS_BOTH = (MEMBEROF_IS_GROUP|MEMBEROF_IS_MEMBER)
} memberof_is_t;

typedef struct memberof_cookie_t {
	AttributeDescription	*ad;
Howard Chu's avatar
Howard Chu committed
188
	BerVarray		vals;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
189
190
191
	int			foundit;
} memberof_cookie_t;

Howard Chu's avatar
Howard Chu committed
192
193
194
195
typedef struct memberof_cbinfo_t {
	slap_overinst *on;
	BerVarray member;
	BerVarray memberof;
196
	memberof_is_t what;
Howard Chu's avatar
Howard Chu committed
197
} memberof_cbinfo_t;
198
199
200
201
202
203
204
205
206
207

static void
memberof_set_backend( Operation *op_target, Operation *op, slap_overinst *on )
{
    BackendInfo *bi = op->o_bd->bd_info;

    if ( bi->bi_type == memberof.on_bi.bi_type )
        op_target->o_bd->bd_info = (BackendInfo *)on->on_info;
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
static int
memberof_isGroupOrMember_cb( Operation *op, SlapReply *rs )
{
	if ( rs->sr_type == REP_SEARCH ) {
		memberof_cookie_t	*mc;

		mc = (memberof_cookie_t *)op->o_callback->sc_private;
		mc->foundit = 1;
	}

	return 0;
}

/*
 * callback for internal search that saves the member attribute values
 * of groups being deleted.
 */
static int
memberof_saveMember_cb( Operation *op, SlapReply *rs )
{
	if ( rs->sr_type == REP_SEARCH ) {
		memberof_cookie_t	*mc;
		Attribute		*a;

		mc = (memberof_cookie_t *)op->o_callback->sc_private;
		mc->foundit = 1;

235
236
		assert( rs->sr_entry != NULL );
		assert( rs->sr_entry->e_attrs != NULL );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
237
238

		a = attr_find( rs->sr_entry->e_attrs, mc->ad );
239
		if ( a != NULL ) {
Howard Chu's avatar
Howard Chu committed
240
			ber_bvarray_dup_x( &mc->vals, a->a_nvals, op->o_tmpmemctx );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
241

Pierangelo Masarati's avatar
Pierangelo Masarati committed
242
			assert( attr_find( a->a_next, mc->ad ) == NULL );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
243
244
245
246
247
248
249
250
251
252
253
		}
	}

	return 0;
}

/*
 * the delete hook performs an internal search that saves the member
 * attribute values of groups being deleted.
 */
static int
254
memberof_isGroupOrMember( Operation *op, memberof_cbinfo_t *mci )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
255
{
Howard Chu's avatar
Howard Chu committed
256
	slap_overinst		*on = mci->on;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
257
258
259
260
	memberof_t		*mo = (memberof_t *)on->on_bi.bi_private;

	Operation		op2 = *op;
	slap_callback		cb = { 0 };
Howard Chu's avatar
Howard Chu committed
261
	BackendInfo	*bi = op->o_bd->bd_info;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
262
263
264
	AttributeName		an[ 2 ];

	memberof_is_t		iswhat = MEMBEROF_IS_NONE;
Howard Chu's avatar
Howard Chu committed
265
	memberof_cookie_t	mc;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
266

267
	assert( mci->what != MEMBEROF_IS_NONE );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

	cb.sc_private = &mc;
	if ( op->o_tag == LDAP_REQ_DELETE ) {
		cb.sc_response = memberof_saveMember_cb;

	} else {
		cb.sc_response = memberof_isGroupOrMember_cb;
	}

	op2.o_tag = LDAP_REQ_SEARCH;
	op2.o_callback = &cb;
	op2.o_dn = op->o_bd->be_rootdn;
	op2.o_ndn = op->o_bd->be_rootndn;

	op2.ors_scope = LDAP_SCOPE_BASE;
	op2.ors_deref = LDAP_DEREF_NEVER;
	BER_BVZERO( &an[ 1 ].an_name );
	op2.ors_attrs = an;
	op2.ors_attrsonly = 0;
	op2.ors_limit = NULL;
	op2.ors_slimit = 1;
	op2.ors_tlimit = SLAP_NO_LIMIT;

291
	if ( mci->what & MEMBEROF_IS_GROUP ) {
292
293
		SlapReply	rs2 = { REP_RESULT };

Pierangelo Masarati's avatar
Pierangelo Masarati committed
294
295
		mc.ad = mo->mo_ad_member;
		mc.foundit = 0;
Howard Chu's avatar
Howard Chu committed
296
		mc.vals = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
297
298
299
300
		an[ 0 ].an_desc = mo->mo_ad_member;
		an[ 0 ].an_name = an[ 0 ].an_desc->ad_cname;
		op2.ors_filterstr = mo->mo_groupFilterstr;
		op2.ors_filter = &mo->mo_groupFilter;
301
		op2.o_do_not_cache = 1;	/* internal search, don't log */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
302

303
		memberof_set_backend( &op2, op, on );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
304
		(void)op->o_bd->be_search( &op2, &rs2 );
Howard Chu's avatar
Howard Chu committed
305
		op2.o_bd->bd_info = bi;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
306
307
308

		if ( mc.foundit ) {
			iswhat |= MEMBEROF_IS_GROUP;
Howard Chu's avatar
Howard Chu committed
309
			if ( mc.vals ) mci->member = mc.vals;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
310
311
312
313

		}
	}

314
	if ( mci->what & MEMBEROF_IS_MEMBER ) {
315
316
		SlapReply	rs2 = { REP_RESULT };

Pierangelo Masarati's avatar
Pierangelo Masarati committed
317
318
		mc.ad = mo->mo_ad_memberof;
		mc.foundit = 0;
Howard Chu's avatar
Howard Chu committed
319
		mc.vals = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
320
321
322
323
		an[ 0 ].an_desc = mo->mo_ad_memberof;
		an[ 0 ].an_name = an[ 0 ].an_desc->ad_cname;
		op2.ors_filterstr = mo->mo_memberFilterstr;
		op2.ors_filter = &mo->mo_memberFilter;
324
		op2.o_do_not_cache = 1;	/* internal search, don't log */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
325

326
		memberof_set_backend( &op2, op, on );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
327
		(void)op->o_bd->be_search( &op2, &rs2 );
Howard Chu's avatar
Howard Chu committed
328
		op2.o_bd->bd_info = bi;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
329
330
331

		if ( mc.foundit ) {
			iswhat |= MEMBEROF_IS_MEMBER;
Howard Chu's avatar
Howard Chu committed
332
			if ( mc.vals ) mci->memberof = mc.vals;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
333
334
335
336

		}
	}

337
	mci->what = iswhat;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
338
339
340
341
342
343
344

	return LDAP_SUCCESS;
}

/*
 * response callback that adds memberof values when a group is modified.
 */
345
static void
Pierangelo Masarati's avatar
Pierangelo Masarati committed
346
347
348
349
350
351
352
353
354
memberof_value_modify(
	Operation		*op,
	struct berval		*ndn,
	AttributeDescription	*ad,
	struct berval		*old_dn,
	struct berval		*old_ndn,
	struct berval		*new_dn,
	struct berval		*new_ndn )
{
Howard Chu's avatar
Howard Chu committed
355
356
	memberof_cbinfo_t *mci = op->o_callback->sc_private;
	slap_overinst	*on = mci->on;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
357
358
359
	memberof_t	*mo = (memberof_t *)on->on_bi.bi_private;

	Operation	op2 = *op;
360
	unsigned long opid = op->o_opid;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
361
362
363
364
	SlapReply	rs2 = { REP_RESULT };
	slap_callback	cb = { NULL, slap_null_cb, NULL, NULL };
	Modifications	mod[ 2 ] = { { { 0 } } }, *ml;
	struct berval	values[ 4 ], nvalues[ 4 ];
365
	int		mcnt = 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
366

367
368
369
370
371
372
	if ( old_ndn != NULL && new_ndn != NULL &&
		ber_bvcmp( old_ndn, new_ndn ) == 0 ) {
	    /* DNs compare equal, it's a noop */
	    return;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
373
374
375
376
377
378
379
380
	op2.o_tag = LDAP_REQ_MODIFY;

	op2.o_req_dn = *ndn;
	op2.o_req_ndn = *ndn;

	op2.o_callback = &cb;
	op2.o_dn = op->o_bd->be_rootdn;
	op2.o_ndn = op->o_bd->be_rootndn;
381
382
	op2.orm_modlist = NULL;

383
	/* Internal ops, never replicate these */
384
	op2.o_opid = 0;		/* shared with op, saved above */
385
386
387
	op2.orm_no_opattrs = 1;
	op2.o_dont_replicate = 1;

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
	if ( !BER_BVISNULL( &mo->mo_ndn ) ) {
		ml = &mod[ mcnt ];
		ml->sml_numvals = 1;
		ml->sml_values = &values[ 0 ];
		ml->sml_values[ 0 ] = mo->mo_dn;
		BER_BVZERO( &ml->sml_values[ 1 ] );
		ml->sml_nvalues = &nvalues[ 0 ];
		ml->sml_nvalues[ 0 ] = mo->mo_ndn;
		BER_BVZERO( &ml->sml_nvalues[ 1 ] );
		ml->sml_desc = slap_schema.si_ad_modifiersName;
		ml->sml_type = ml->sml_desc->ad_cname;
		ml->sml_op = LDAP_MOD_REPLACE;
		ml->sml_flags = SLAP_MOD_INTERNAL;
		ml->sml_next = op2.orm_modlist;
		op2.orm_modlist = ml;

		mcnt++;
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
406

407
	ml = &mod[ mcnt ];
Howard Chu's avatar
Howard Chu committed
408
	ml->sml_numvals = 1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
409
410
411
412
413
414
415
	ml->sml_values = &values[ 2 ];
	BER_BVZERO( &ml->sml_values[ 1 ] );
	ml->sml_nvalues = &nvalues[ 2 ];
	BER_BVZERO( &ml->sml_nvalues[ 1 ] );
	ml->sml_desc = ad;
	ml->sml_type = ml->sml_desc->ad_cname;
	ml->sml_flags = SLAP_MOD_INTERNAL;
416
417
	ml->sml_next = op2.orm_modlist;
	op2.orm_modlist = ml;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
418
419

	if ( new_ndn != NULL ) {
420
		BackendInfo *bi = op2.o_bd->bd_info;
421
		OpExtra	oex;
422

Pierangelo Masarati's avatar
Pierangelo Masarati committed
423
424
425
		assert( !BER_BVISNULL( new_dn ) );
		assert( !BER_BVISNULL( new_ndn ) );

426
		ml = &mod[ mcnt ];
Pierangelo Masarati's avatar
Pierangelo Masarati committed
427
428
429
430
431
		ml->sml_op = LDAP_MOD_ADD;

		ml->sml_values[ 0 ] = *new_dn;
		ml->sml_nvalues[ 0 ] = *new_ndn;

432
433
		oex.oe_key = (void *)&memberof;
		LDAP_SLIST_INSERT_HEAD(&op2.o_extra, &oex, oe_next);
434
		memberof_set_backend( &op2, op, on );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
435
		(void)op->o_bd->be_modify( &op2, &rs2 );
436
		op2.o_bd->bd_info = bi;
437
		LDAP_SLIST_REMOVE(&op2.o_extra, &oex, OpExtra, oe_next);
438
		if ( rs2.sr_err != LDAP_SUCCESS ) {
439
440
441
442
			Debug(LDAP_DEBUG_ANY,
			      "%s: memberof_value_modify DN=\"%s\" add %s=\"%s\" failed err=%d\n",
			      op->o_log_prefix, op2.o_req_dn.bv_val,
			      ad->ad_cname.bv_val, new_dn->bv_val, rs2.sr_err );
443
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
444

445
446
447
448
449
450
451
		assert( op2.orm_modlist == &mod[ mcnt ] );
		assert( mcnt == 0 || op2.orm_modlist->sml_next == &mod[ 0 ] );
		ml = op2.orm_modlist->sml_next;
		if ( mcnt == 1 ) {
			assert( ml == &mod[ 0 ] );
			ml = ml->sml_next;
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
452
453
454
		if ( ml != NULL ) {
			slap_mods_free( ml, 1 );
		}
455
456

		mod[ 0 ].sml_next = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
457
458
459
	}

	if ( old_ndn != NULL ) {
460
		BackendInfo *bi = op2.o_bd->bd_info;
461
		OpExtra	oex;
462

Pierangelo Masarati's avatar
Pierangelo Masarati committed
463
464
465
		assert( !BER_BVISNULL( old_dn ) );
		assert( !BER_BVISNULL( old_ndn ) );

466
		ml = &mod[ mcnt ];
Pierangelo Masarati's avatar
Pierangelo Masarati committed
467
468
469
470
471
		ml->sml_op = LDAP_MOD_DELETE;

		ml->sml_values[ 0 ] = *old_dn;
		ml->sml_nvalues[ 0 ] = *old_ndn;

472
473
		oex.oe_key = (void *)&memberof;
		LDAP_SLIST_INSERT_HEAD(&op2.o_extra, &oex, oe_next);
474
		memberof_set_backend( &op2, op, on );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
475
		(void)op->o_bd->be_modify( &op2, &rs2 );
476
		op2.o_bd->bd_info = bi;
477
		LDAP_SLIST_REMOVE(&op2.o_extra, &oex, OpExtra, oe_next);
478
		if ( rs2.sr_err != LDAP_SUCCESS ) {
479
480
481
482
			Debug(LDAP_DEBUG_ANY,
			      "%s: memberof_value_modify DN=\"%s\" delete %s=\"%s\" failed err=%d\n",
			      op->o_log_prefix, op2.o_req_dn.bv_val,
			      ad->ad_cname.bv_val, old_dn->bv_val, rs2.sr_err );
483
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
484

485
486
487
488
489
490
		assert( op2.orm_modlist == &mod[ mcnt ] );
		ml = op2.orm_modlist->sml_next;
		if ( mcnt == 1 ) {
			assert( ml == &mod[ 0 ] );
			ml = ml->sml_next;
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
491
492
493
494
		if ( ml != NULL ) {
			slap_mods_free( ml, 1 );
		}
	}
495
496
	/* restore original opid */
	op->o_opid = opid;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
497
498
499
500
501
502
503

	/* FIXME: if old_group_ndn doesn't exist, both delete __and__
	 * add will fail; better split in two operations, although
	 * not optimal in terms of performance.  At least it would
	 * move towards self-repairing capabilities. */
}

Howard Chu's avatar
Howard Chu committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
static int
memberof_cleanup( Operation *op, SlapReply *rs )
{
	slap_callback *sc = op->o_callback;
	memberof_cbinfo_t *mci = sc->sc_private;

	op->o_callback = sc->sc_next;
	if ( mci->memberof )
		ber_bvarray_free_x( mci->memberof, op->o_tmpmemctx );
	if ( mci->member )
		ber_bvarray_free_x( mci->member, op->o_tmpmemctx );
	op->o_tmpfree( sc, op->o_tmpmemctx );
	return 0;
}

static int memberof_res_add( Operation *op, SlapReply *rs );
static int memberof_res_delete( Operation *op, SlapReply *rs );
static int memberof_res_modify( Operation *op, SlapReply *rs );
static int memberof_res_modrdn( Operation *op, SlapReply *rs );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
524
525
526
527
528
529
530
531
532
533
static int
memberof_op_add( Operation *op, SlapReply *rs )
{
	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
	memberof_t	*mo = (memberof_t *)on->on_bi.bi_private;

	Attribute	**ap, **map = NULL;
	int		rc = SLAP_CB_CONTINUE;
	int		i;
	struct berval	save_dn, save_ndn;
Howard Chu's avatar
Howard Chu committed
534
535
	slap_callback *sc;
	memberof_cbinfo_t *mci;
536
537
538
539
540
541
	OpExtra		*oex;

	LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
		if ( oex->oe_key == (void *)&memberof )
			return SLAP_CB_CONTINUE;
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
542
543
544
545
546
547

	if ( op->ora_e->e_attrs == NULL ) {
		/* FIXME: global overlay; need to deal with */
		Debug( LDAP_DEBUG_ANY, "%s: memberof_op_add(\"%s\"): "
			"consistency checks not implemented when overlay "
			"is instantiated as global.\n",
548
			op->o_log_prefix, op->o_req_dn.bv_val );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
		return SLAP_CB_CONTINUE;
	}

	if ( MEMBEROF_REVERSE( mo ) ) {
		for ( ap = &op->ora_e->e_attrs; *ap; ap = &(*ap)->a_next ) {
			Attribute	*a = *ap;

			if ( a->a_desc == mo->mo_ad_memberof ) {
				map = ap;
				break;
			}
		}
	}

	save_dn = op->o_dn;
	save_ndn = op->o_ndn;

	if ( MEMBEROF_DANGLING_CHECK( mo )
			&& !get_relax( op )
568
			&& is_entry_objectclass_or_sub( op->ora_e, mo->mo_oc_group ) )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
569
570
	{
		op->o_dn = op->o_bd->be_rootdn;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
571
		op->o_ndn = op->o_bd->be_rootndn;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
572
573
574
575
576
577
578
579
580
581
582
583
584
		op->o_bd->bd_info = (BackendInfo *)on->on_info;

		for ( ap = &op->ora_e->e_attrs; *ap; ) {
			Attribute	*a = *ap;

			if ( !is_ad_subtype( a->a_desc, mo->mo_ad_member ) ) {
				ap = &a->a_next;
				continue;
			}

			assert( a->a_nvals != NULL );

			for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
585
				Entry		*e = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
586

587
588
589
590
				/* ITS#6670 Ignore member pointing to this entry */
				if ( dn_match( &a->a_nvals[i], &save_ndn ))
					continue;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
591
592
593
594
595
596
597
598
				rc = be_entry_get_rw( op, &a->a_nvals[ i ],
						NULL, NULL, 0, &e );
				if ( rc == LDAP_SUCCESS ) {
					be_entry_release_r( op, e );
					continue;
				}

				if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
599
					rc = rs->sr_err = mo->mo_dangling_err;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
					rs->sr_text = "adding non-existing object "
						"as group member";
					send_ldap_result( op, rs );
					goto done;
				}

				if ( MEMBEROF_DANGLING_DROP( mo ) ) {
					int	j;
	
					Debug( LDAP_DEBUG_ANY, "%s: memberof_op_add(\"%s\"): "
						"member=\"%s\" does not exist (stripping...)\n",
						op->o_log_prefix, op->ora_e->e_name.bv_val,
						a->a_vals[ i ].bv_val );
	
					for ( j = i + 1; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ );
					ber_memfree( a->a_vals[ i ].bv_val );
					BER_BVZERO( &a->a_vals[ i ] );
					if ( a->a_nvals != a->a_vals ) {
						ber_memfree( a->a_nvals[ i ].bv_val );
						BER_BVZERO( &a->a_nvals[ i ] );
					}
Howard Chu's avatar
Howard Chu committed
621
					a->a_numvals--;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
					if ( j - i == 1 ) {
						break;
					}
		
					AC_MEMCPY( &a->a_vals[ i ], &a->a_vals[ i + 1 ],
						sizeof( struct berval ) * ( j - i ) );
					if ( a->a_nvals != a->a_vals ) {
						AC_MEMCPY( &a->a_nvals[ i ], &a->a_nvals[ i + 1 ],
							sizeof( struct berval ) * ( j - i ) );
					}
					i--;
				}
			}

			/* If all values have been removed,
			 * remove the attribute itself. */
			if ( BER_BVISNULL( &a->a_nvals[ 0 ] ) ) {
				*ap = a->a_next;
				attr_free( a );
	
			} else {
				ap = &a->a_next;
			}
		}
		op->o_dn = save_dn;
		op->o_ndn = save_ndn;
		op->o_bd->bd_info = (BackendInfo *)on;
	}

	if ( map != NULL ) {
		Attribute		*a = *map;
		AccessControlState	acl_state = ACL_STATE_INIT;

		for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
			Entry		*e;

			op->o_bd->bd_info = (BackendInfo *)on->on_info;
			/* access is checked with the original identity */
			rc = access_allowed( op, op->ora_e, mo->mo_ad_memberof,
					&a->a_nvals[ i ], ACL_WADD,
					&acl_state );
			if ( rc == 0 ) {
				rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
				rs->sr_text = NULL;
				send_ldap_result( op, rs );
				goto done;
			}
669
670
671
672
			/* ITS#6670 Ignore member pointing to this entry */
			if ( dn_match( &a->a_nvals[i], &save_ndn ))
				continue;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
673
674
675
676
677
678
679
680
681
			rc = be_entry_get_rw( op, &a->a_nvals[ i ],
					NULL, NULL, 0, &e );
			op->o_bd->bd_info = (BackendInfo *)on;
			if ( rc != LDAP_SUCCESS ) {
				if ( get_relax( op ) ) {
					continue;
				}

				if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
682
					rc = rs->sr_err = mo->mo_dangling_err;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
					rs->sr_text = "adding non-existing object "
						"as memberof";
					send_ldap_result( op, rs );
					goto done;
				}

				if ( MEMBEROF_DANGLING_DROP( mo ) ) {
					int	j;
	
					Debug( LDAP_DEBUG_ANY, "%s: memberof_op_add(\"%s\"): "
						"memberof=\"%s\" does not exist (stripping...)\n",
						op->o_log_prefix, op->ora_e->e_name.bv_val,
						a->a_nvals[ i ].bv_val );
	
					for ( j = i + 1; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ );
					ber_memfree( a->a_vals[ i ].bv_val );
					BER_BVZERO( &a->a_vals[ i ] );
					if ( a->a_nvals != a->a_vals ) {
						ber_memfree( a->a_nvals[ i ].bv_val );
						BER_BVZERO( &a->a_nvals[ i ] );
					}
					if ( j - i == 1 ) {
						break;
					}
		
					AC_MEMCPY( &a->a_vals[ i ], &a->a_vals[ i + 1 ],
						sizeof( struct berval ) * ( j - i ) );
					if ( a->a_nvals != a->a_vals ) {
						AC_MEMCPY( &a->a_nvals[ i ], &a->a_nvals[ i + 1 ],
							sizeof( struct berval ) * ( j - i ) );
					}
					i--;
				}
				
				continue;
			}

			/* access is checked with the original identity */
			op->o_bd->bd_info = (BackendInfo *)on->on_info;
			rc = access_allowed( op, e, mo->mo_ad_member,
					&op->o_req_ndn, ACL_WADD, NULL );
			be_entry_release_r( op, e );
			op->o_bd->bd_info = (BackendInfo *)on;

			if ( !rc ) {
				rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
				rs->sr_text = "insufficient access to object referenced by memberof";
				send_ldap_result( op, rs );
				goto done;
			}
		}

		if ( BER_BVISNULL( &a->a_nvals[ 0 ] ) ) {
			*map = a->a_next;
			attr_free( a );
		}
	}

	rc = SLAP_CB_CONTINUE;
Howard Chu's avatar
Howard Chu committed
742
743
744
745
746

	sc = op->o_tmpalloc( sizeof(slap_callback)+sizeof(*mci), op->o_tmpmemctx );
	sc->sc_private = sc+1;
	sc->sc_response = memberof_res_add;
	sc->sc_cleanup = memberof_cleanup;
747
	sc->sc_writewait = 0;
Howard Chu's avatar
Howard Chu committed
748
749
750
751
752
753
754
	mci = sc->sc_private;
	mci->on = on;
	mci->member = NULL;
	mci->memberof = NULL;
	sc->sc_next = op->o_callback;
	op->o_callback = sc;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
755
756
757
758
759
760
761
762
763
764
765
766
767
768
done:;
	op->o_dn = save_dn;
	op->o_ndn = save_ndn;
	op->o_bd->bd_info = (BackendInfo *)on;

	return rc;
}

static int
memberof_op_delete( Operation *op, SlapReply *rs )
{
	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
	memberof_t	*mo = (memberof_t *)on->on_bi.bi_private;

Howard Chu's avatar
Howard Chu committed
769
770
	slap_callback *sc;
	memberof_cbinfo_t *mci;
771
	OpExtra		*oex;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
772

773
774
775
776
	LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
		if ( oex->oe_key == (void *)&memberof )
			return SLAP_CB_CONTINUE;
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
777

Howard Chu's avatar
Howard Chu committed
778
779
780
781
	sc = op->o_tmpalloc( sizeof(slap_callback)+sizeof(*mci), op->o_tmpmemctx );
	sc->sc_private = sc+1;
	sc->sc_response = memberof_res_delete;
	sc->sc_cleanup = memberof_cleanup;
782
	sc->sc_writewait = 0;
Howard Chu's avatar
Howard Chu committed
783
784
785
786
	mci = sc->sc_private;
	mci->on = on;
	mci->member = NULL;
	mci->memberof = NULL;
787
788
789
790
	mci->what = MEMBEROF_IS_GROUP;
	if ( MEMBEROF_REFINT( mo ) ) {
		mci->what = MEMBEROF_IS_BOTH;
	}
Howard Chu's avatar
Howard Chu committed
791

792
	memberof_isGroupOrMember( op, mci );
Howard Chu's avatar
Howard Chu committed
793
794
795

	sc->sc_next = op->o_callback;
	op->o_callback = sc;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
796
797
798
799
800
801
802
803
804
805
806

	return SLAP_CB_CONTINUE;
}

static int
memberof_op_modify( Operation *op, SlapReply *rs )
{
	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
	memberof_t	*mo = (memberof_t *)on->on_bi.bi_private;

	Modifications	**mlp, **mmlp = NULL;
Howard Chu's avatar
Howard Chu committed
807
	int		rc = SLAP_CB_CONTINUE, save_member = 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
808
	struct berval	save_dn, save_ndn;
Howard Chu's avatar
Howard Chu committed
809
810
	slap_callback *sc;
	memberof_cbinfo_t *mci, mcis;
811
812
813
814
815
816
	OpExtra		*oex;

	LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
		if ( oex->oe_key == (void *)&memberof )
			return SLAP_CB_CONTINUE;
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
817
818
819
820
821
822
823
824
825
826
827
828
829
830

	if ( MEMBEROF_REVERSE( mo ) ) {
		for ( mlp = &op->orm_modlist; *mlp; mlp = &(*mlp)->sml_next ) {
			Modifications	*ml = *mlp;

			if ( ml->sml_desc == mo->mo_ad_memberof ) {
				mmlp = mlp;
				break;
			}
		}
	}

	save_dn = op->o_dn;
	save_ndn = op->o_ndn;
Howard Chu's avatar
Howard Chu committed
831
	mcis.on = on;
832
	mcis.what = MEMBEROF_IS_GROUP;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
833

834
835
	if ( memberof_isGroupOrMember( op, &mcis ) == LDAP_SUCCESS
		&& ( mcis.what & MEMBEROF_IS_GROUP ) )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
836
	{
837
838
839
840
841
842
843
		Modifications *ml;

		for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
			if ( ml->sml_desc == mo->mo_ad_member ) {
				switch ( ml->sml_op ) {
				case LDAP_MOD_DELETE:
				case LDAP_MOD_REPLACE:
Pierangelo Masarati's avatar
Pierangelo Masarati committed
844
				case SLAP_MOD_SOFTDEL: /* ITS#7487: can be used by syncrepl (in mirror mode?) */
845
846
847
					save_member = 1;
					break;
				}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
848
			}
849
850
851
852
853
854
855
		}


		if ( MEMBEROF_DANGLING_CHECK( mo )
				&& !get_relax( op ) )
		{
			op->o_dn = op->o_bd->be_rootdn;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
856
			op->o_ndn = op->o_bd->be_rootndn;
857
858
859
860
861
862
863
864
865
866
867
			op->o_bd->bd_info = (BackendInfo *)on->on_info;
		
			assert( op->orm_modlist != NULL );
		
			for ( mlp = &op->orm_modlist; *mlp; ) {
				Modifications	*ml = *mlp;
				int		i;
		
				if ( !is_ad_subtype( ml->sml_desc, mo->mo_ad_member ) ) {
					mlp = &ml->sml_next;
					continue;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
868
				}
869
870
871
		
				switch ( ml->sml_op ) {
				case LDAP_MOD_DELETE:
Pierangelo Masarati's avatar
Pierangelo Masarati committed
872
				case SLAP_MOD_SOFTDEL: /* ITS#7487: can be used by syncrepl (in mirror mode?) */
873
874
875
					/* we don't care about cancellations: if the value
					 * exists, fine; if it doesn't, we let the underlying
					 * database fail as appropriate; */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
876
					mlp = &ml->sml_next;
877
878
879
					break;
		
				case LDAP_MOD_REPLACE:
880
881
882
883
884
885
 					/* Handle this just like a delete (see above) */
 					if ( !ml->sml_values ) {
 						mlp = &ml->sml_next;
 						break;
 					}
 
886
				case LDAP_MOD_ADD:
Pierangelo Masarati's avatar
Pierangelo Masarati committed
887
888
				case SLAP_MOD_SOFTADD: /* ITS#7487 */
				case SLAP_MOD_ADD_IF_NOT_PRESENT: /* ITS#7487 */
889
890
891
892
893
894
895
					/* NOTE: right now, the attributeType we use
					 * for member must have a normalized value */
					assert( ml->sml_nvalues != NULL );
		
					for ( i = 0; !BER_BVISNULL( &ml->sml_nvalues[ i ] ); i++ ) {
						Entry		*e;
		
896
897
898
899
						/* ITS#6670 Ignore member pointing to this entry */
						if ( dn_match( &ml->sml_nvalues[i], &save_ndn ))
							continue;

900
901
902
903
904
905
906
907
						if ( be_entry_get_rw( op, &ml->sml_nvalues[ i ],
								NULL, NULL, 0, &e ) == LDAP_SUCCESS )
						{
							be_entry_release_r( op, e );
							continue;
						}
		
						if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
908
							rc = rs->sr_err = mo->mo_dangling_err;
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
							rs->sr_text = "adding non-existing object "
								"as group member";
							send_ldap_result( op, rs );
							goto done;
						}
		
						if ( MEMBEROF_DANGLING_DROP( mo ) ) {
							int	j;
		
							Debug( LDAP_DEBUG_ANY, "%s: memberof_op_modify(\"%s\"): "
								"member=\"%s\" does not exist (stripping...)\n",
								op->o_log_prefix, op->o_req_dn.bv_val,
								ml->sml_nvalues[ i ].bv_val );
		
							for ( j = i + 1; !BER_BVISNULL( &ml->sml_nvalues[ j ] ); j++ );
							ber_memfree( ml->sml_values[ i ].bv_val );
							BER_BVZERO( &ml->sml_values[ i ] );
							ber_memfree( ml->sml_nvalues[ i ].bv_val );
							BER_BVZERO( &ml->sml_nvalues[ i ] );
							ml->sml_numvals--;
							if ( j - i == 1 ) {
								break;
							}
		
							AC_MEMCPY( &ml->sml_values[ i ], &ml->sml_values[ i + 1 ],
								sizeof( struct berval ) * ( j - i ) );
							AC_MEMCPY( &ml->sml_nvalues[ i ], &ml->sml_nvalues[ i + 1 ],
								sizeof( struct berval ) * ( j - i ) );
							i--;
						}
					}
		
					if ( BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
						*mlp = ml->sml_next;
						slap_mod_free( &ml->sml_mod, 0 );
						free( ml );
		
					} else {
						mlp = &ml->sml_next;
					}
		
					break;
		
				default:
					assert( 0 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
954
955
956
957
				}
			}
		}
	}
958
	
Pierangelo Masarati's avatar
Pierangelo Masarati committed
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
	if ( mmlp != NULL ) {
		Modifications	*ml = *mmlp;
		int		i;
		Entry		*target;

		op->o_bd->bd_info = (BackendInfo *)on->on_info;
		rc = be_entry_get_rw( op, &op->o_req_ndn,
				NULL, NULL, 0, &target );
		op->o_bd->bd_info = (BackendInfo *)on;
		if ( rc != LDAP_SUCCESS ) {
			rc = rs->sr_err = LDAP_NO_SUCH_OBJECT;
			send_ldap_result( op, rs );
			goto done;
		}

		switch ( ml->sml_op ) {
		case LDAP_MOD_DELETE:
Pierangelo Masarati's avatar
Pierangelo Masarati committed
976
		case SLAP_MOD_SOFTDEL: /* ITS#7487: can be used by syncrepl (in mirror mode?) */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
			if ( ml->sml_nvalues != NULL ) {
				AccessControlState	acl_state = ACL_STATE_INIT;

				for ( i = 0; !BER_BVISNULL( &ml->sml_nvalues[ i ] ); i++ ) {
					Entry		*e;

					op->o_bd->bd_info = (BackendInfo *)on->on_info;
					/* access is checked with the original identity */
					rc = access_allowed( op, target,
							mo->mo_ad_memberof,
							&ml->sml_nvalues[ i ],
							ACL_WDEL,
							&acl_state );
					if ( rc == 0 ) {
						rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
						rs->sr_text = NULL;
						send_ldap_result( op, rs );
						goto done2;
					}

					rc = be_entry_get_rw( op, &ml->sml_nvalues[ i ],
							NULL, NULL, 0, &e );
					op->o_bd->bd_info = (BackendInfo *)on;
					if ( rc != LDAP_SUCCESS ) {
						if ( get_relax( op ) ) {
							continue;
						}

						if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
1006
							rc = rs->sr_err = mo->mo_dangling_err;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
							rs->sr_text = "deleting non-existing object "
								"as memberof";
							send_ldap_result( op, rs );
							goto done2;
						}

						if ( MEMBEROF_DANGLING_DROP( mo ) ) {
							int	j;
	
							Debug( LDAP_DEBUG_ANY, "%s: memberof_op_modify(\"%s\"): "
								"memberof=\"%s\" does not exist (stripping...)\n",
								op->o_log_prefix, op->o_req_ndn.bv_val,
								ml->sml_nvalues[ i ].bv_val );
	
							for ( j = i + 1; !BER_BVISNULL( &ml->sml_nvalues[ j ] ); j++ );
							ber_memfree( ml->sml_values[ i ].bv_val );
							BER_BVZERO( &ml->sml_values[ i ] );
							if ( ml->sml_nvalues != ml->sml_values ) {
								ber_memfree( ml->sml_nvalues[ i ].bv_val );
								BER_BVZERO( &ml->sml_nvalues[ i ] );
							}
Howard Chu's avatar
Howard Chu committed
1028
							ml->sml_numvals--;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
							if ( j - i == 1 ) {
								break;
							}
		
							AC_MEMCPY( &ml->sml_values[ i ], &ml->sml_values[ i + 1 ],
								sizeof( struct berval ) * ( j - i ) );
							if ( ml->sml_nvalues != ml->sml_values ) {
								AC_MEMCPY( &ml->sml_nvalues[ i ], &ml->sml_nvalues[ i + 1 ],
									sizeof( struct berval ) * ( j - i ) );
							}
							i--;
						}

						continue;
					}

					/* access is checked with the original identity */
					op->o_bd->bd_info = (BackendInfo *)on->on_info;
					rc = access_allowed( op, e, mo->mo_ad_member,
							&op->o_req_ndn,
							ACL_WDEL, NULL );
					be_entry_release_r( op, e );
					op->o_bd->bd_info = (BackendInfo *)on;

					if ( !rc ) {
						rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
						rs->sr_text = "insufficient access to object referenced by memberof";
						send_ldap_result( op, rs );
						goto done;
					}
				}

				if ( BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
					*mmlp = ml->sml_next;
					slap_mod_free( &ml->sml_mod, 0 );
					free( ml );
				}

				break;
			}
			/* fall thru */

		case LDAP_MOD_REPLACE:

			op->o_bd->bd_info = (BackendInfo *)on->on_info;
			/* access is checked with the original identity */
			rc = access_allowed( op, target,
					mo->mo_ad_memberof,
					NULL,
					ACL_WDEL, NULL );
			op->o_bd->bd_info = (BackendInfo *)on;
			if ( rc == 0 ) {
				rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
				rs->sr_text = NULL;
				send_ldap_result( op, rs );
				goto done2;
			}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1087
			if ( ml->sml_op == LDAP_MOD_DELETE || ml->sml_op == SLAP_MOD_SOFTDEL || !ml->sml_values ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1088
1089
1090
1091
				break;
			}
			/* fall thru */

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1092
1093
1094
1095
		case LDAP_MOD_ADD:
		case SLAP_MOD_SOFTADD: /* ITS#7487 */
		case SLAP_MOD_ADD_IF_NOT_PRESENT: /* ITS#7487 */
			{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
			AccessControlState	acl_state = ACL_STATE_INIT;

			for ( i = 0; !BER_BVISNULL( &ml->sml_nvalues[ i ] ); i++ ) {
				Entry		*e;

				op->o_bd->bd_info = (BackendInfo *)on->on_info;
				/* access is checked with the original identity */
				rc = access_allowed( op, target,
						mo->mo_ad_memberof,
						&ml->sml_nvalues[ i ],
						ACL_WADD,
						&acl_state );
				if ( rc == 0 ) {
					rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
					rs->sr_text = NULL;
					send_ldap_result( op, rs );
					goto done2;
				}

1115
1116
1117
1118
				/* ITS#6670 Ignore member pointing to this entry */
				if ( dn_match( &ml->sml_nvalues[i], &save_ndn ))
					continue;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1119
1120
1121
1122
1123
				rc = be_entry_get_rw( op, &ml->sml_nvalues[ i ],
						NULL, NULL, 0, &e );
				op->o_bd->bd_info = (BackendInfo *)on;
				if ( rc != LDAP_SUCCESS ) {
					if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
1124
						rc = rs->sr_err = mo->mo_dangling_err;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
						rs->sr_text = "adding non-existing object "
							"as memberof";
						send_ldap_result( op, rs );
						goto done2;
					}

					if ( MEMBEROF_DANGLING_DROP( mo ) ) {
						int	j;

						Debug( LDAP_DEBUG_ANY, "%s: memberof_op_modify(\"%s\"): "
							"memberof=\"%s\" does not exist (stripping...)\n",
							op->o_log_prefix, op->o_req_ndn.bv_val,
							ml->sml_nvalues[ i ].bv_val );

						for ( j = i + 1; !BER_BVISNULL( &ml->sml_nvalues[ j ] ); j++ );
						ber_memfree( ml->sml_values[ i ].bv_val );
						BER_BVZERO( &ml->sml_values[ i ] );
						if ( ml->sml_nvalues != ml->sml_values ) {
							ber_memfree( ml->sml_nvalues[ i ].bv_val );
							BER_BVZERO( &ml->sml_nvalues[ i ] );
						}
Howard Chu's avatar
Howard Chu committed
1146
						ml->sml_numvals--;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
						if ( j - i == 1 ) {
							break;
						}
	
						AC_MEMCPY( &ml->sml_values[ i ], &ml->sml_values[ i + 1 ],
							sizeof( struct berval ) * ( j - i ) );
						if ( ml->sml_nvalues != ml->sml_values ) {
							AC_MEMCPY( &ml->sml_nvalues[ i ], &ml->sml_nvalues[ i + 1 ],
								sizeof( struct berval ) * ( j - i ) );
						}
						i--;
					}

					continue;
				}

				/* access is checked with the original identity */
				op->o_bd->bd_info = (BackendInfo *)on->on_info;
				rc = access_allowed( op, e, mo->mo_ad_member,
						&op->o_req_ndn,
						ACL_WDEL, NULL );
				be_entry_release_r( op, e );
				op->o_bd->bd_info = (BackendInfo *)on;

				if ( !rc ) {
					rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
					rs->sr_text = "insufficient access to object referenced by memberof";
					send_ldap_result( op, rs );
					goto done;
				}
			}

			if ( BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
				*mmlp = ml->sml_next;
				slap_mod_free( &ml->sml_mod, 0 );
				free( ml );
			}

			} break;

		default:
			assert( 0 );
		}

done2:;
		op->o_bd->bd_info = (BackendInfo *)on->on_info;
		be_entry_release_r( op, target );
		op->o_bd->bd_info = (BackendInfo *)on;
	}

Howard Chu's avatar
Howard Chu committed
1197
1198
1199
1200
	sc = op->o_tmpalloc( sizeof(slap_callback)+sizeof(*mci), op->o_tmpmemctx );
	sc->sc_private = sc+1;
	sc->sc_response = memberof_res_modify;
	sc->sc_cleanup = memberof_cleanup;
1201
	sc->sc_writewait = 0;
Howard Chu's avatar
Howard Chu committed
1202
1203
1204
1205
	mci = sc->sc_private;
	mci->on = on;
	mci->member = NULL;
	mci->memberof = NULL;
1206
	mci->what = mcis.what;
Howard Chu's avatar
Howard Chu committed
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219

	if ( save_member ) {
		op->o_dn = op->o_bd->be_rootdn;
		op->o_ndn = op->o_bd->be_rootndn;
		op->o_bd->bd_info = (BackendInfo *)on->on_info;
		rc = backend_attribute( op, NULL, &op->o_req_ndn,
				mo->mo_ad_member, &mci->member, ACL_READ );
		op->o_bd->bd_info = (BackendInfo *)on;
	}

	sc->sc_next = op->o_callback;
	op->o_callback = sc;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
	rc = SLAP_CB_CONTINUE;

done:;
	op->o_dn = save_dn;
	op->o_ndn = save_ndn;
	op->o_bd->bd_info = (BackendInfo *)on;

	return rc;
}

Howard Chu's avatar
Howard Chu committed
1230
1231
1232
1233
1234
1235
static int
memberof_op_modrdn( Operation *op, SlapReply *rs )
{
	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
	slap_callback *sc;
	memberof_cbinfo_t *mci;
1236
1237
1238
1239
1240
1241
	OpExtra		*oex;

	LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
		if ( oex->oe_key == (void *)&memberof )
			return SLAP_CB_CONTINUE;
	}
Howard Chu's avatar
Howard Chu committed
1242
1243
1244
1245
1246

	sc = op->o_tmpalloc( sizeof(slap_callback)+sizeof(*mci), op->o_tmpmemctx );
	sc->sc_private = sc+1;
	sc->sc_response = memberof_res_modrdn;
	sc->sc_cleanup = memberof_cleanup;
1247
	sc->sc_writewait = 0;
Howard Chu's avatar
Howard Chu committed
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
	mci = sc->sc_private;
	mci->on = on;
	mci->member = NULL;
	mci->memberof = NULL;

	sc->sc_next = op->o_callback;
	op->o_callback = sc;

	return SLAP_CB_CONTINUE;
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1259
1260
1261
1262
1263
1264
/*
 * response callback that adds memberof values when a group is added.
 */
static int
memberof_res_add( Operation *op, SlapReply *rs )
{
Howard Chu's avatar
Howard Chu committed
1265
1266
	memberof_cbinfo_t *mci = op->o_callback->sc_private;
	slap_overinst	*on = mci->on;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1267
1268
1269
1270
	memberof_t	*mo = (memberof_t *)on->on_bi.bi_private;

	int		i;

Howard Chu's avatar
Howard Chu committed
1271
1272
1273
1274
	if ( rs->sr_err != LDAP_SUCCESS ) {
		return SLAP_CB_CONTINUE;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1275
1276
1277
1278
1279
1280
1281
	if ( MEMBEROF_REVERSE( mo ) ) {
		Attribute	*ma;

		ma = attr_find( op->ora_e->e_attrs, mo->mo_ad_memberof );
		if ( ma != NULL ) {
			/* relax is required to allow to add
			 * a non-existing member */
Howard Chu's avatar
Howard Chu committed
1282
			op->o_relax = SLAP_CONTROL_CRITICAL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1283
1284
1285

			for ( i = 0; !BER_BVISNULL( &ma->a_nvals[ i ] ); i++ ) {
		
1286
1287
1288
1289
				/* ITS#6670 Ignore member pointing to this entry */
				if ( dn_match( &ma->a_nvals[i], &op->o_req_ndn ))
					continue;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1290
1291
				/* the modification is attempted
				 * with the original identity */
1292
				memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1293
1294
1295
1296
1297
1298
					&ma->a_nvals[ i ], mo->mo_ad_member,
					NULL, NULL, &op->o_req_dn, &op->o_req_ndn );
			}
		}
	}

1299
	if ( is_entry_objectclass_or_sub( op->ora_e, mo->mo_oc_group ) ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1300
1301
1302
1303
1304
1305
1306
		Attribute	*a;

		for ( a = attrs_find( op->ora_e->e_attrs, mo->mo_ad_member );
				a != NULL;
				a = attrs_find( a->a_next, mo->mo_ad_member ) )
		{
			for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
1307
1308
1309
1310
				/* ITS#6670 Ignore member pointing to this entry */
				if ( dn_match( &a->a_nvals[i], &op->o_req_ndn ))
					continue;

1311
				memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
						&a->a_nvals[ i ],
						mo->mo_ad_memberof,
						NULL, NULL,
						&op->o_req_dn,
						&op->o_req_ndn );
			}
		}
	}

	return SLAP_CB_CONTINUE;
}

/*
 * response callback that deletes memberof values when a group is deleted.
 */
static int
memberof_res_delete( Operation *op, SlapReply *rs )
{
Howard Chu's avatar
Howard Chu committed
1330
1331
	memberof_cbinfo_t *mci = op->o_callback->sc_private;
	slap_overinst	*on = mci->on;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1332
1333
1334
1335
1336
	memberof_t	*mo = (memberof_t *)on->on_bi.bi_private;

 	BerVarray	vals;
	int		i;

Howard Chu's avatar
Howard Chu committed
1337
1338
1339
1340
1341
	if ( rs->sr_err != LDAP_SUCCESS ) {
		return SLAP_CB_CONTINUE;
	}

	vals = mci->member;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1342
1343
	if ( vals != NULL ) {
		for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1344
			memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1345
1346
1347
1348
1349
1350
1351
					&vals[ i ], mo->mo_ad_memberof,
					&op->o_req_dn, &op->o_req_ndn,
					NULL, NULL );
		}
	}

	if ( MEMBEROF_REFINT( mo ) ) {
Howard Chu's avatar
Howard Chu committed
1352
		vals = mci->memberof;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1353
1354
		if ( vals != NULL ) {
			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1355
				memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
						&vals[ i ], mo->mo_ad_member,
						&op->o_req_dn, &op->o_req_ndn,
						NULL, NULL );
			}
		}
	}

	return SLAP_CB_CONTINUE;
}

/*
 * response callback that adds/deletes memberof values when a group
 * is modified.
 */
static int
memberof_res_modify( Operation *op, SlapReply *rs )
{
Howard Chu's avatar
Howard Chu committed
1373
1374
	memberof_cbinfo_t *mci = op->o_callback->sc_private;
	slap_overinst	*on = mci->on;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1375
1376
1377
1378
1379
1380
	memberof_t	*mo = (memberof_t *)on->on_bi.bi_private;

	int		i, rc;
	Modifications	*ml, *mml = NULL;
	BerVarray	vals;

Howard Chu's avatar
Howard Chu committed
1381
1382
1383
1384
	if ( rs->sr_err != LDAP_SUCCESS ) {
		return SLAP_CB_CONTINUE;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
	if ( MEMBEROF_REVERSE( mo ) ) {
		for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
			if ( ml->sml_desc == mo->mo_ad_memberof ) {
				mml = ml;
				break;
			}
		}
	}

	if ( mml != NULL ) {
		BerVarray	vals = mml->sml_nvalues;

		switch ( mml->sml_op ) {
		case LDAP_MOD_DELETE:
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1399
		case SLAP_MOD_SOFTDEL: /* ITS#7487: can be used by syncrepl (in mirror mode?) */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1400
1401
			if ( vals != NULL ) {
				for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1402
					memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
							&vals[ i ], mo->mo_ad_member,
							&op->o_req_dn, &op->o_req_ndn,
							NULL, NULL );
				}
				break;
			}
			/* fall thru */

		case LDAP_MOD_REPLACE:
			/* delete all ... */
			op->o_bd->bd_info = (BackendInfo *)on->on_info;
			rc = backend_attribute( op, NULL, &op->o_req_ndn,
					mo->mo_ad_memberof, &vals, ACL_READ );
			op->o_bd->bd_info = (BackendInfo *)on;
			if ( rc == LDAP_SUCCESS ) {
				for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1419
					memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1420
1421
1422
1423
1424
1425
1426
							&vals[ i ], mo->mo_ad_member,
							&op->o_req_dn, &op->o_req_ndn,
							NULL, NULL );
				}
				ber_bvarray_free_x( vals, op->o_tmpmemctx );
			}

1427
			if ( ml->sml_op == LDAP_MOD_DELETE || !mml->sml_values ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1428
1429
1430
1431
1432
				break;
			}
			/* fall thru */

		case LDAP_MOD_ADD:
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1433
1434
		case SLAP_MOD_SOFTADD: /* ITS#7487 */
		case SLAP_MOD_ADD_IF_NOT_PRESENT: /* ITS#7487 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1435
1436
1437
			assert( vals != NULL );

			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1438
				memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
						&vals[ i ], mo->mo_ad_member,
						NULL, NULL,
						&op->o_req_dn, &op->o_req_ndn );
			}
			break;

		default:
			assert( 0 );
		}
	}

1450
	if ( mci->what & MEMBEROF_IS_GROUP )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1451
1452
1453
1454
1455
1456
1457
1458
	{
		for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
			if ( ml->sml_desc != mo->mo_ad_member ) {
				continue;
			}

			switch ( ml->sml_op ) {
			case LDAP_MOD_DELETE:
1459
			case SLAP_MOD_SOFTDEL: /* ITS#7487: can be used by syncrepl (in mirror mode?) */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1460
1461
1462
				vals = ml->sml_nvalues;
				if ( vals != NULL ) {
					for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1463
						memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1464
1465
1466
1467
1468
1469
1470
1471
1472
								&vals[ i ], mo->mo_ad_memberof,
								&op->o_req_dn, &op->o_req_ndn,
								NULL, NULL );
					}
					break;
				}
				/* fall thru */
	
			case LDAP_MOD_REPLACE:
Howard Chu's avatar
Howard Chu committed
1473
				vals = mci->member;
1474

Pierangelo Masarati's avatar
Pierangelo Masarati committed
1475
				/* delete all ... */
1476
				if ( vals != NULL ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1477
					for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1478
						memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1479
1480
1481
1482
1483
1484
								&vals[ i ], mo->mo_ad_memberof,
								&op->o_req_dn, &op->o_req_ndn,
								NULL, NULL );
					}
				}
	
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1485
				if ( ml->sml_op == LDAP_MOD_DELETE || ml->sml_op == SLAP_MOD_SOFTDEL || !ml->sml_values ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1486
1487
1488
1489
1490
					break;
				}
				/* fall thru */
	
			case LDAP_MOD_ADD:
1491
1492
			case SLAP_MOD_SOFTADD: /* ITS#7487 */
			case SLAP_MOD_ADD_IF_NOT_PRESENT : /* ITS#7487 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1493
1494
1495
				assert( ml->sml_nvalues != NULL );
				vals = ml->sml_nvalues;
				for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1496
					memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
							&vals[ i ], mo->mo_ad_memberof,
							NULL, NULL,
							&op->o_req_dn, &op->o_req_ndn );
				}
				break;
	
			default:
				assert( 0 );
			}
		}
	}

	return SLAP_CB_CONTINUE;
}

/*
 * response callback that adds/deletes member values when a group member
Howard Chu's avatar
Howard Chu committed
1514
 * is renamed.
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1515
1516
 */
static int
Howard Chu's avatar
Howard Chu committed
1517
memberof_res_modrdn( Operation *op, SlapReply *rs )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1518
{
Howard Chu's avatar
Howard Chu committed
1519
1520
	memberof_cbinfo_t *mci = op->o_callback->sc_private;
	slap_overinst	*on = mci->on;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1521
1522
1523
1524
1525
1526
1527
1528
	memberof_t	*mo = (memberof_t *)on->on_bi.bi_private;

	struct berval	newPDN, newDN = BER_BVNULL, newPNDN, newNDN;
	int		i, rc;
	BerVarray	vals;

	struct berval	save_dn, save_ndn;

Howard Chu's avatar
Howard Chu committed
1529
1530
1531
1532
	if ( rs->sr_err != LDAP_SUCCESS ) {
		return SLAP_CB_CONTINUE;
	}

1533
	mci->what = MEMBEROF_IS_GROUP;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1534
	if ( MEMBEROF_REFINT( mo ) ) {
1535
		mci->what |= MEMBEROF_IS_MEMBER;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
	}

	if ( op->orr_nnewSup ) {
		newPNDN = *op->orr_nnewSup;

	} else {
		dnParent( &op->o_req_ndn, &newPNDN );
	}

	build_new_dn( &newNDN, &newPNDN, &op->orr_nnewrdn, op->o_tmpmemctx ); 

	save_dn = op->o_req_dn;
	save_ndn = op->o_req_ndn;

	op->o_req_dn = newNDN;
	op->o_req_ndn = newNDN;
1552
	rc = memberof_isGroupOrMember( op, mci );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1553
1554
1555
	op->o_req_dn = save_dn;
	op->o_req_ndn = save_ndn;

1556
	if ( rc != LDAP_SUCCESS || mci->what == MEMBEROF_IS_NONE ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
		goto done;
	}

	if ( op->orr_newSup ) {
		newPDN = *op->orr_newSup;

	} else {
		dnParent( &op->o_req_dn, &newPDN );
	}

	build_new_dn( &newDN, &newPDN, &op->orr_newrdn, op->o_tmpmemctx ); 

1569
	if ( mci->what & MEMBEROF_IS_GROUP ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1570
1571
1572
1573
1574
1575
1576
		op->o_bd->bd_info = (BackendInfo *)on->on_info;
		rc = backend_attribute( op, NULL, &newNDN,
				mo->mo_ad_member, &vals, ACL_READ );
		op->o_bd->bd_info = (BackendInfo *)on;

		if ( rc == LDAP_SUCCESS ) {
			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1577
				memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1578
1579
1580
1581
1582
1583
1584
1585
						&vals[ i ], mo->mo_ad_memberof,
						&op->o_req_dn, &op->o_req_ndn,
						&newDN, &newNDN );
			}
			ber_bvarray_free_x( vals, op->o_tmpmemctx );
		}
	}

1586
	if ( MEMBEROF_REFINT( mo ) && ( mci->what & MEMBEROF_IS_MEMBER ) ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1587
1588
1589
1590
1591
1592
1593
		op->o_bd->bd_info = (BackendInfo *)on->on_info;
		rc = backend_attribute( op, NULL, &newNDN,
				mo->mo_ad_memberof, &vals, ACL_READ );
		op->o_bd->bd_info = (BackendInfo *)on;

		if ( rc == LDAP_SUCCESS ) {
			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1594
				memberof_value_modify( op,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
						&vals[ i ], mo->mo_ad_member,
						&op->o_req_dn, &op->o_req_ndn,
						&newDN, &newNDN );
			}
			ber_bvarray_free_x( vals, op->o_tmpmemctx );
		}
	}

done:;
	if ( !BER_BVISNULL( &newDN ) ) {
		op->o_tmpfree( newDN.bv_val, op->o_tmpmemctx );
	}
	op->o_tmpfree( newNDN.bv_val, op->o_tmpmemctx );

	return SLAP_CB_CONTINUE;
}


static int
memberof_db_init(
	BackendDB	*be,
	ConfigReply	*cr )
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;