modify.c 21.1 KB
Newer Older
Howard Chu's avatar
Howard Chu committed
1
2
3
4
/* modify.c - mdb backend modify routine */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 2000-2021 The OpenLDAP Foundation.
Howard Chu's avatar
Howard Chu committed
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
 * 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>.
 */

#include "portable.h"

#include <stdio.h>
#include <ac/string.h>
#include <ac/time.h>

#include "back-mdb.h"

static struct berval scbva[] = {
	BER_BVC("glue"),
	BER_BVNULL
};

static void
mdb_modify_idxflags(
	Operation *op,
	AttributeDescription *desc,
	int got_delete,
	Attribute *newattrs,
	Attribute *oldattrs )
{
	struct berval	ix_at;
	AttrInfo	*ai;

	/* check if modified attribute was indexed
	 * but not in case of NOOP... */
	ai = mdb_index_mask( op->o_bd, desc, &ix_at );
	if ( ai ) {
		if ( got_delete ) {
			Attribute 	*ap;
			struct berval	ix2;

			ap = attr_find( oldattrs, desc );
			if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;

Howard Chu's avatar
Howard Chu committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
			/* ITS#8678 FIXME
			 * If using 32bit hashes, or substring index, must account for
			 * possible index collisions. If no substring index, and using
			 * 64bit hashes, assume we don't need to check for collisions.
			 *
			 * In 2.5 use refcounts and avoid all of this mess.
			 */
			if (!slap_hash64(-1) || (ai->ai_indexmask & SLAP_INDEX_SUBSTR)) {
				/* Find all other attrs that index to same slot */
				for ( ap = newattrs; ap; ap = ap->a_next ) {
					ai = mdb_index_mask( op->o_bd, ap->a_desc, &ix2 );
					if ( ai && ix2.bv_val == ix_at.bv_val )
						ap->a_flags |= SLAP_ATTR_IXADD;
				}
Howard Chu's avatar
Howard Chu committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
			}

		} else {
			Attribute 	*ap;

			ap = attr_find( newattrs, desc );
			if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
		}
	}
}

int mdb_modify_internal(
	Operation *op,
	MDB_txn *tid,
	Modifications *modlist,
	Entry *e,
	const char **text,
	char *textbuf,
	size_t textlen )
{
Howard Chu's avatar
Howard Chu committed
86
	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
Howard Chu's avatar
Howard Chu committed
87
88
89
90
	int rc, err;
	Modification	*mod;
	Modifications	*ml;
	Attribute	*save_attrs;
Howard Chu's avatar
Howard Chu committed
91
	Attribute 	*ap, *aold, *anew;
Howard Chu's avatar
Howard Chu committed
92
	int			glue_attr_delete = 0;
Howard Chu's avatar
Howard Chu committed
93
	int			softop, chkpresent;
Howard Chu's avatar
Howard Chu committed
94
	int			got_delete;
Howard Chu's avatar
Howard Chu committed
95
96
	int			a_flags;
	MDB_cursor	*mvc = NULL;
Howard Chu's avatar
Howard Chu committed
97
98

	Debug( LDAP_DEBUG_TRACE, "mdb_modify_internal: 0x%08lx: %s\n",
99
		e->e_id, e->e_dn );
Howard Chu's avatar
Howard Chu committed
100
101
102
103
104

	if ( !acl_check_modlist( op, e, modlist )) {
		return LDAP_INSUFFICIENT_ACCESS;
	}

Howard Chu's avatar
Howard Chu committed
105
	/* save_attrs will be disposed of by caller */
Howard Chu's avatar
Howard Chu committed
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
133
134
135
136
137
138
139
140
141
142
143
144
	save_attrs = e->e_attrs;
	e->e_attrs = attrs_dup( e->e_attrs );

	for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
		int match;
		mod = &ml->sml_mod;
		switch( mod->sm_op ) {
		case LDAP_MOD_ADD:
		case LDAP_MOD_REPLACE:
			if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
				value_match( &match, slap_schema.si_ad_structuralObjectClass,
					slap_schema.si_ad_structuralObjectClass->
						ad_type->sat_equality,
					SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
					&mod->sm_values[0], &scbva[0], text );
				if ( !match ) glue_attr_delete = 1;
			}
		}
		if ( glue_attr_delete )
			break;
	}

	if ( glue_attr_delete ) {
		Attribute	**app = &e->e_attrs;
		while ( *app != NULL ) {
			if ( !is_at_operational( (*app)->a_desc->ad_type )) {
				Attribute *save = *app;
				*app = (*app)->a_next;
				attr_free( save );
				continue;
			}
			app = &(*app)->a_next;
		}
	}

	for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
		mod = &ml->sml_mod;
		got_delete = 0;

Howard Chu's avatar
Howard Chu committed
145
146
147
148
149
150
		aold = attr_find( e->e_attrs, mod->sm_desc );
		if (aold)
			a_flags = aold->a_flags;
		else
			a_flags = 0;

Howard Chu's avatar
Howard Chu committed
151
152
		switch ( mod->sm_op ) {
		case LDAP_MOD_ADD:
Howard Chu's avatar
Howard Chu committed
153
154
			softop = 0;
			chkpresent = 0;
Howard Chu's avatar
Howard Chu committed
155
156
			Debug(LDAP_DEBUG_ARGS,
				"mdb_modify_internal: add %s\n",
157
				mod->sm_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
158
159

do_add:
Howard Chu's avatar
Howard Chu committed
160
161
			err = modify_add_values( e, mod, get_permissiveModify(op),
				text, textbuf, textlen );
Howard Chu's avatar
Howard Chu committed
162
163
164
165
166
167
168
169
170
171

			if( softop ) {
				mod->sm_op = SLAP_MOD_SOFTADD;
				if ( err == LDAP_TYPE_OR_VALUE_EXISTS )
					err = LDAP_SUCCESS;
			}
			if( chkpresent ) {
				mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
			}

Howard Chu's avatar
Howard Chu committed
172
173
			if( err != LDAP_SUCCESS ) {
				Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
174
					err, *text );
Howard Chu's avatar
Howard Chu committed
175
			} else {
Howard Chu's avatar
Howard Chu committed
176
				unsigned hi;
Howard Chu's avatar
Howard Chu committed
177
178
179
180
				if (!aold)
					anew = attr_find( e->e_attrs, mod->sm_desc );
				else
					anew = aold;
Howard Chu's avatar
Howard Chu committed
181
				mdb_attr_multi_thresh( mdb, mod->sm_desc, &hi, NULL );
Howard Chu's avatar
Howard Chu committed
182
				/* check for big multivalued attrs */
Howard Chu's avatar
Howard Chu committed
183
				if ( anew->a_numvals > hi )
Howard Chu's avatar
Howard Chu committed
184
185
186
187
					anew->a_flags |= SLAP_ATTR_BIG_MULTI;
				if ( anew->a_flags & SLAP_ATTR_BIG_MULTI ) {
					if (!mvc) {
						err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
Howard Chu's avatar
Howard Chu committed
188
189
190
						if (err) {
mval_fail:					strncpy( textbuf, mdb_strerror( err ), textlen );
							err = LDAP_OTHER;
Howard Chu's avatar
Howard Chu committed
191
							break;
Howard Chu's avatar
Howard Chu committed
192
						}
Howard Chu's avatar
Howard Chu committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
					}
					/* if prev was set, just add new values */
					if (a_flags & SLAP_ATTR_BIG_MULTI ) {
						anew = (Attribute *)mod;
						/* Tweak nvals */
						if (!anew->a_nvals)
							anew->a_nvals = anew->a_vals;
					}
					err = mdb_mval_put(op, mvc, e->e_id, anew);
					if (a_flags & SLAP_ATTR_BIG_MULTI ) {
						/* Undo nvals tweak */
						if (anew->a_nvals == anew->a_vals)
							anew->a_nvals = NULL;
					}
Howard Chu's avatar
Howard Chu committed
207
208
					if ( err )
						goto mval_fail;
Howard Chu's avatar
Howard Chu committed
209
				}
Howard Chu's avatar
Howard Chu committed
210
211
212
213
214
215
216
217
218
			}
			break;

		case LDAP_MOD_DELETE:
			if ( glue_attr_delete ) {
				err = LDAP_SUCCESS;
				break;
			}

Howard Chu's avatar
Howard Chu committed
219
			softop = 0;
Howard Chu's avatar
Howard Chu committed
220
221
			Debug(LDAP_DEBUG_ARGS,
				"mdb_modify_internal: delete %s\n",
222
				mod->sm_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
223
do_del:
Howard Chu's avatar
Howard Chu committed
224
225
			err = modify_delete_values( e, mod, get_permissiveModify(op),
				text, textbuf, textlen );
Howard Chu's avatar
Howard Chu committed
226
227
228
229
230
231
232
233
234

			if (softop) {
				mod->sm_op = SLAP_MOD_SOFTDEL;
				if ( err == LDAP_NO_SUCH_ATTRIBUTE ) {
					err = LDAP_SUCCESS;
					softop = 2;
				}
			}

Howard Chu's avatar
Howard Chu committed
235
236
			if( err != LDAP_SUCCESS ) {
				Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
237
					err, *text );
Howard Chu's avatar
Howard Chu committed
238
			} else {
Howard Chu's avatar
Howard Chu committed
239
240
241
242
243
244
245
246
				if (softop != 2)
					got_delete = 1;
				/* check for big multivalued attrs */
				if (a_flags & SLAP_ATTR_BIG_MULTI) {
					Attribute a_dummy;
					if (!mvc) {
						err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
						if (err)
Howard Chu's avatar
Howard Chu committed
247
							goto mval_fail;
Howard Chu's avatar
Howard Chu committed
248
249
250
					}
					if ( mod->sm_numvals ) {
						anew = attr_find( e->e_attrs, mod->sm_desc );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
251
						if ( anew ) {
Howard Chu's avatar
Howard Chu committed
252
253
254
							unsigned lo;
							mdb_attr_multi_thresh( mdb, mod->sm_desc, NULL, &lo );
							if ( anew->a_numvals < lo ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
255
256
257
258
259
								anew->a_flags ^= SLAP_ATTR_BIG_MULTI;
								anew = NULL;
							} else {
								anew = (Attribute *)mod;
							}
Howard Chu's avatar
Howard Chu committed
260
261
262
263
264
265
266
267
268
269
270
						}
					} else {
						anew = NULL;
					}
					if (!anew) {
					/* delete all values */
						anew = &a_dummy;
						anew->a_desc = mod->sm_desc;
						anew->a_numvals = 0;
					}
					err = mdb_mval_del( op, mvc, e->e_id, anew );
Howard Chu's avatar
Howard Chu committed
271
272
					if ( err )
						goto mval_fail;
Howard Chu's avatar
Howard Chu committed
273
				}
Howard Chu's avatar
Howard Chu committed
274
275
276
277
278
279
			}
			break;

		case LDAP_MOD_REPLACE:
			Debug(LDAP_DEBUG_ARGS,
				"mdb_modify_internal: replace %s\n",
280
				mod->sm_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
281
282
283
284
			err = modify_replace_values( e, mod, get_permissiveModify(op),
				text, textbuf, textlen );
			if( err != LDAP_SUCCESS ) {
				Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
285
					err, *text );
Howard Chu's avatar
Howard Chu committed
286
			} else {
Howard Chu's avatar
Howard Chu committed
287
				unsigned hi;
Howard Chu's avatar
Howard Chu committed
288
				got_delete = 1;
Howard Chu's avatar
Howard Chu committed
289
290
291
292
293
				if (a_flags & SLAP_ATTR_BIG_MULTI) {
					Attribute a_dummy;
					if (!mvc) {
						err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
						if (err)
Howard Chu's avatar
Howard Chu committed
294
							goto mval_fail;
Howard Chu's avatar
Howard Chu committed
295
296
297
298
299
300
301
					}
					/* delete all values */
					anew = &a_dummy;
					anew->a_desc = mod->sm_desc;
					anew->a_numvals = 0;
					err = mdb_mval_del( op, mvc, e->e_id, anew );
					if (err)
Howard Chu's avatar
Howard Chu committed
302
						goto mval_fail;
303
304
				}
				anew = attr_find( e->e_attrs, mod->sm_desc );
Howard Chu's avatar
Howard Chu committed
305
306
				mdb_attr_multi_thresh( mdb, mod->sm_desc, &hi, NULL );
				if (mod->sm_numvals > hi) {
307
308
309
310
					anew->a_flags |= SLAP_ATTR_BIG_MULTI;
					if (!mvc) {
						err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
						if (err)
Howard Chu's avatar
Howard Chu committed
311
							goto mval_fail;
Howard Chu's avatar
Howard Chu committed
312
					}
313
					err = mdb_mval_put(op, mvc, e->e_id, anew);
Howard Chu's avatar
Howard Chu committed
314
315
					if (err)
						goto mval_fail;
316
317
318
				} else if (anew) {
					/* revert back to normal attr */
					anew->a_flags &= ~SLAP_ATTR_BIG_MULTI;
Howard Chu's avatar
Howard Chu committed
319
				}
Howard Chu's avatar
Howard Chu committed
320
321
322
323
324
325
			}
			break;

		case LDAP_MOD_INCREMENT:
			Debug(LDAP_DEBUG_ARGS,
				"mdb_modify_internal: increment %s\n",
326
				mod->sm_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
327
328
329
330
331
			err = modify_increment_values( e, mod, get_permissiveModify(op),
				text, textbuf, textlen );
			if( err != LDAP_SUCCESS ) {
				Debug(LDAP_DEBUG_ARGS,
					"mdb_modify_internal: %d %s\n",
332
					err, *text );
Howard Chu's avatar
Howard Chu committed
333
334
335
336
337
338
339
340
			} else {
				got_delete = 1;
			}
			break;

		case SLAP_MOD_SOFTADD:
			Debug(LDAP_DEBUG_ARGS,
				"mdb_modify_internal: softadd %s\n",
341
				mod->sm_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
342
343
344
345
 			/* Avoid problems in index_add_mods()
 			 * We need to add index if necessary.
 			 */
 			mod->sm_op = LDAP_MOD_ADD;
Howard Chu's avatar
Howard Chu committed
346
			softop = 1;
Howard Chu's avatar
Howard Chu committed
347
			chkpresent = 0;
Howard Chu's avatar
Howard Chu committed
348
			goto do_add;
Howard Chu's avatar
Howard Chu committed
349
350
351
352

		case SLAP_MOD_SOFTDEL:
			Debug(LDAP_DEBUG_ARGS,
				"mdb_modify_internal: softdel %s\n",
353
				mod->sm_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
354
355
356
357
 			/* Avoid problems in index_delete_mods()
 			 * We need to add index if necessary.
 			 */
 			mod->sm_op = LDAP_MOD_DELETE;
Howard Chu's avatar
Howard Chu committed
358
359
			softop = 1;
			goto do_del;
Howard Chu's avatar
Howard Chu committed
360
361
362
363
364
365
366
367
368
369

		case SLAP_MOD_ADD_IF_NOT_PRESENT:
			if ( attr_find( e->e_attrs, mod->sm_desc ) != NULL ) {
				/* skip */
				err = LDAP_SUCCESS;
				break;
			}

			Debug(LDAP_DEBUG_ARGS,
				"mdb_modify_internal: add_if_not_present %s\n",
370
				mod->sm_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
371
372
373
374
 			/* Avoid problems in index_add_mods()
 			 * We need to add index if necessary.
 			 */
 			mod->sm_op = LDAP_MOD_ADD;
Howard Chu's avatar
Howard Chu committed
375
			softop = 0;
Howard Chu's avatar
Howard Chu committed
376
377
			chkpresent = 1;
			goto do_add;
Howard Chu's avatar
Howard Chu committed
378
379
380

		default:
			Debug(LDAP_DEBUG_ANY, "mdb_modify_internal: invalid op %d\n",
381
				mod->sm_op );
Howard Chu's avatar
Howard Chu committed
382
383
384
			*text = "Invalid modify operation";
			err = LDAP_OTHER;
			Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
385
				err, *text );
Howard Chu's avatar
Howard Chu committed
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
		}

		if ( err != LDAP_SUCCESS ) {
			attrs_free( e->e_attrs );
			e->e_attrs = save_attrs;
			/* unlock entry, delete from cache */
			return err; 
		}

		/* If objectClass was modified, reset the flags */
		if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
			e->e_ocflags = 0;
		}

		if ( glue_attr_delete ) e->e_ocflags = 0;


		/* check if modified attribute was indexed
		 * but not in case of NOOP... */
		if ( !op->o_noop ) {
			mdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
		}
	}

	/* check that the entry still obeys the schema */
	ap = NULL;
412
	rc = entry_schema_check( op, e, get_relax(op), 0, &ap,
Howard Chu's avatar
Howard Chu committed
413
414
415
416
417
418
419
420
421
422
423
424
		text, textbuf, textlen );
	if ( rc != LDAP_SUCCESS || op->o_noop ) {
		attrs_free( e->e_attrs );
		/* clear the indexing flags */
		for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
			ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
		}
		e->e_attrs = save_attrs;

		if ( rc != LDAP_SUCCESS ) {
			Debug( LDAP_DEBUG_ANY,
				"entry failed schema check: %s\n",
425
				*text );
Howard Chu's avatar
Howard Chu committed
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
		}

		/* if NOOP then silently revert to saved attrs */
		return rc;
	}

	/* structuralObjectClass modified! */
	if ( ap ) {
		assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
		if ( !op->o_noop ) {
			mdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
				1, e->e_attrs, save_attrs );
		}
	}

	/* update the indices of the modified attributes */

	/* start with deleting the old index entries */
	for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
		if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
			struct berval *vals;
			Attribute *a2;
			ap->a_flags &= ~SLAP_ATTR_IXDEL;
			a2 = attr_find( e->e_attrs, ap->a_desc );
			if ( a2 ) {
				/* need to detect which values were deleted */
452
				int i, j, k;
453
454
455
				/* let add know there were deletes */
				if ( a2->a_flags & SLAP_ATTR_IXADD )
					a2->a_flags |= SLAP_ATTR_IXDEL;
Howard Chu's avatar
Howard Chu committed
456
457
458
				vals = op->o_tmpalloc( (ap->a_numvals + 1) *
					sizeof(struct berval), op->o_tmpmemctx );
				j = 0;
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
				for ( i=k=0; i < ap->a_numvals; i++ ) {
					char found = 0;
					BerValue* current = &ap->a_nvals[i];
					int k2 = k;
					for (k2 = k ; k2 < a2->a_numvals; k2 ++) {
						int match = -1, rc;
						const char *text;

						rc = ordered_value_match( &match, a2->a_desc,
								ap->a_desc->ad_type->sat_equality, 0,
							&a2->a_nvals[k2], current, &text );
						if ( rc == LDAP_SUCCESS && match == 0 ) {
							found = 1;
							break;
						}
					}

					if (!found) {
						vals[j++] = *current;
					} else {
						k = k2 + 1;
					}
Howard Chu's avatar
Howard Chu committed
481
482
483
484
485
486
487
488
489
490
491
492
493
				}
				BER_BVZERO(vals+j);
			} else {
				/* attribute was completely deleted */
				vals = ap->a_nvals;
			}
			rc = 0;
			if ( !BER_BVISNULL( vals )) {
				rc = mdb_index_values( op, tid, ap->a_desc,
					vals, e->e_id, SLAP_INDEX_DELETE_OP );
				if ( rc != LDAP_SUCCESS ) {
					Debug( LDAP_DEBUG_ANY,
						"%s: attribute \"%s\" index delete failure\n",
494
						op->o_log_prefix, ap->a_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
					attrs_free( e->e_attrs );
					e->e_attrs = save_attrs;
				}
			}
			if ( vals != ap->a_nvals )
				op->o_tmpfree( vals, op->o_tmpmemctx );
			if ( rc ) return rc;
		}
	}

	/* add the new index entries */
	for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
		if (ap->a_flags & SLAP_ATTR_IXADD) {
			ap->a_flags &= ~SLAP_ATTR_IXADD;
509
510
511
512
513
514
515
516
517
			if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
				/* if any values were deleted, we must readd index
				 * for all remaining values.
				 */
				ap->a_flags &= ~SLAP_ATTR_IXDEL;
				rc = mdb_index_values( op, tid, ap->a_desc,
					ap->a_nvals,
					e->e_id, SLAP_INDEX_ADD_OP );
			} else {
518
				int found = 0;
519
520
521
522
523
524
525
				/* if this was only an add, we only need to index
				 * the added values.
				 */
				for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
					struct berval *vals;
					if ( ml->sml_desc != ap->a_desc || !ml->sml_numvals )
						continue;
526
					found = 1;
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
					switch( ml->sml_op ) {
					case LDAP_MOD_ADD:
					case LDAP_MOD_REPLACE:
					case LDAP_MOD_INCREMENT:
					case SLAP_MOD_SOFTADD:
					case SLAP_MOD_ADD_IF_NOT_PRESENT:
						if ( ml->sml_op == LDAP_MOD_INCREMENT )
							vals = ap->a_nvals;
						else if ( ml->sml_nvalues )
							vals = ml->sml_nvalues;
						else
							vals = ml->sml_values;
						rc = mdb_index_values( op, tid, ap->a_desc,
							vals, e->e_id, SLAP_INDEX_ADD_OP );
						break;
					}
					if ( rc )
						break;
				}
546
547
548
549
550
551
552
553
554
				/* This attr was affected by a modify of a subtype, so
				 * there was no direct match in the modlist. Just readd
				 * all of its values.
				 */
				if ( !found ) {
					rc = mdb_index_values( op, tid, ap->a_desc,
						ap->a_nvals,
						e->e_id, SLAP_INDEX_ADD_OP );
				}
555
			}
Howard Chu's avatar
Howard Chu committed
556
557
558
			if ( rc != LDAP_SUCCESS ) {
				Debug( LDAP_DEBUG_ANY,
				       "%s: attribute \"%s\" index add failure\n",
559
					op->o_log_prefix, ap->a_desc->ad_cname.bv_val );
Howard Chu's avatar
Howard Chu committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
				attrs_free( e->e_attrs );
				e->e_attrs = save_attrs;
				return rc;
			}
		}
	}

	return rc;
}


int
mdb_modify( Operation *op, SlapReply *rs )
{
	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
	Entry		*e = NULL;
	int		manageDSAit = get_manageDSAit( op );
	char textbuf[SLAP_TEXT_BUFLEN];
	size_t textlen = sizeof textbuf;
	MDB_txn	*txn = NULL;
	mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
	Entry		dummy = {0};

	LDAPControl **preread_ctrl = NULL;
	LDAPControl **postread_ctrl = NULL;
	LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
	int num_ctrls = 0;
587
	int numads = mdb->mi_numads;
Howard Chu's avatar
Howard Chu committed
588
589

	Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(mdb_modify) ": %s\n",
590
		op->o_req_dn.bv_val );
Howard Chu's avatar
Howard Chu committed
591
592
593
594
595
596
597
598
599

	ctrls[num_ctrls] = NULL;

	/* begin transaction */
	rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
	rs->sr_text = NULL;
	if( rs->sr_err != 0 ) {
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(mdb_modify) ": txn_begin failed: "
600
			"%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err );
Howard Chu's avatar
Howard Chu committed
601
602
603
604
605
606
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "internal error";
		goto return_results;
	}
	txn = moi->moi_txn;

607
608
609
610
611
612
613
614
	/* Don't touch the opattrs, if this is a contextCSN update
	 * initiated from updatedn */
	if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
		 op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {

		slap_mods_opattrs( op, &op->orm_modlist, 1 );
	}

Howard Chu's avatar
Howard Chu committed
615
	/* get entry or ancestor */
616
	rs->sr_err = mdb_dn2entry( op, txn, NULL, &op->o_req_ndn, &e, NULL, 1 );
Howard Chu's avatar
Howard Chu committed
617
618
619
620

	if ( rs->sr_err != 0 ) {
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(mdb_modify) ": dn2entry failed (%d)\n",
621
			rs->sr_err );
Howard Chu's avatar
Howard Chu 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
		switch( rs->sr_err ) {
		case MDB_NOTFOUND:
			break;
		case LDAP_BUSY:
			rs->sr_text = "ldap server busy";
			goto return_results;
		default:
			rs->sr_err = LDAP_OTHER;
			rs->sr_text = "internal error";
			goto return_results;
		}
	}

	/* acquire and lock entry */
	/* FIXME: dn2entry() should return non-glue entry */
	if (( rs->sr_err == MDB_NOTFOUND ) ||
		( !manageDSAit && e && is_entry_glue( e )))
	{
		if ( e != NULL ) {
			rs->sr_matched = ch_strdup( e->e_dn );
			if ( is_entry_referral( e )) {
				BerVarray ref = get_entry_referrals( op, e );
				rs->sr_ref = referral_rewrite( ref, &e->e_name,
					&op->o_req_dn, LDAP_SCOPE_DEFAULT );
				ber_bvarray_free( ref );
			} else {
				rs->sr_ref = NULL;
			}
Howard Chu's avatar
Howard Chu committed
650
			mdb_entry_return( op, e );
Howard Chu's avatar
Howard Chu committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
			e = NULL;

		} else {
			rs->sr_ref = referral_rewrite( default_referral, NULL,
				&op->o_req_dn, LDAP_SCOPE_DEFAULT );
		}

		rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
		rs->sr_err = LDAP_REFERRAL;
		send_ldap_result( op, rs );
		goto done;
	}

	if ( !manageDSAit && is_entry_referral( e ) ) {
		/* entry is a referral, don't allow modify */
		rs->sr_ref = get_entry_referrals( op, e );

		Debug( LDAP_DEBUG_TRACE,
669
			LDAP_XSTRING(mdb_modify) ": entry is referral\n" );
Howard Chu's avatar
Howard Chu committed
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

		rs->sr_err = LDAP_REFERRAL;
		rs->sr_matched = e->e_name.bv_val;
		rs->sr_flags = REP_REF_MUSTBEFREED;
		send_ldap_result( op, rs );
		rs->sr_matched = NULL;
		goto done;
	}

	if ( get_assert( op ) &&
		( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
	{
		rs->sr_err = LDAP_ASSERTION_FAILED;
		goto return_results;
	}

	if( op->o_preread ) {
		if( preread_ctrl == NULL ) {
			preread_ctrl = &ctrls[num_ctrls++];
			ctrls[num_ctrls] = NULL;
		}
		if ( slap_read_controls( op, rs, e,
			&slap_pre_read_bv, preread_ctrl ) )
		{
			Debug( LDAP_DEBUG_TRACE,
				"<=- " LDAP_XSTRING(mdb_modify) ": pre-read "
696
				"failed!\n" );
Howard Chu's avatar
Howard Chu committed
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
			if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
				/* FIXME: is it correct to abort
				 * operation if control fails? */
				goto return_results;
			}
		}
	}

	/* Modify the entry */
	dummy = *e;
	rs->sr_err = mdb_modify_internal( op, txn, op->orm_modlist,
		&dummy, &rs->sr_text, textbuf, textlen );

	if( rs->sr_err != LDAP_SUCCESS ) {
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(mdb_modify) ": modify failed (%d)\n",
713
			rs->sr_err );
Howard Chu's avatar
Howard Chu committed
714
715
716
717
718
719
		/* Only free attrs if they were dup'd.  */
		if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
		goto return_results;
	}

	/* change the entry itself */
Howard Chu's avatar
Howard Chu committed
720
	rs->sr_err = mdb_id2entry_update( op, txn, NULL, &dummy );
Howard Chu's avatar
Howard Chu committed
721
722
723
	if ( rs->sr_err != 0 ) {
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(mdb_modify) ": id2entry update failed " "(%d)\n",
724
			rs->sr_err );
725
726
727
728
729
730
		if ( rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ) {
			rs->sr_text = "entry too big";
		} else {
			rs->sr_err = LDAP_OTHER;
			rs->sr_text = "entry update failed";
		}
Howard Chu's avatar
Howard Chu committed
731
732
733
734
735
736
737
738
739
740
741
742
743
		goto return_results;
	}

	if( op->o_postread ) {
		if( postread_ctrl == NULL ) {
			postread_ctrl = &ctrls[num_ctrls++];
			ctrls[num_ctrls] = NULL;
		}
		if( slap_read_controls( op, rs, &dummy,
			&slap_post_read_bv, postread_ctrl ) )
		{
			Debug( LDAP_DEBUG_TRACE,
				"<=- " LDAP_XSTRING(mdb_modify)
744
				": post-read failed!\n" );
Howard Chu's avatar
Howard Chu committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
			if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
				/* FIXME: is it correct to abort
				 * operation if control fails? */
				goto return_results;
			}
		}
	}

	/* Only free attrs if they were dup'd.  */
	if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
	if( moi == &opinfo ) {
		LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
		opinfo.moi_oe.oe_key = NULL;
		if( op->o_noop ) {
759
			mdb->mi_numads = numads;
Howard Chu's avatar
Howard Chu committed
760
761
762
763
764
765
			mdb_txn_abort( txn );
			rs->sr_err = LDAP_X_NO_OPERATION;
			txn = NULL;
			goto return_results;
		} else {
			rs->sr_err = mdb_txn_commit( txn );
766
767
			if ( rs->sr_err )
				mdb->mi_numads = numads;
Howard Chu's avatar
Howard Chu committed
768
769
770
771
772
			txn = NULL;
		}
	}

	if( rs->sr_err != 0 ) {
773
		Debug( LDAP_DEBUG_ANY,
Howard Chu's avatar
Howard Chu committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
			LDAP_XSTRING(mdb_modify) ": txn_%s failed: %s (%d)\n",
			op->o_noop ? "abort (no-op)" : "commit",
			mdb_strerror(rs->sr_err), rs->sr_err );
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "commit failed";

		goto return_results;
	}

	Debug( LDAP_DEBUG_TRACE,
		LDAP_XSTRING(mdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
		op->o_noop ? " (no-op)" : "",
		dummy.e_id, op->o_req_dn.bv_val );

	rs->sr_err = LDAP_SUCCESS;
	rs->sr_text = NULL;
	if( num_ctrls ) rs->sr_ctrls = ctrls;

return_results:
	if( dummy.e_attrs ) {
		attrs_free( dummy.e_attrs );
	}
	send_ldap_result( op, rs );

#if 0
	if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) {
		TXN_CHECKPOINT( mdb->bi_dbenv,
			mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
	}
#endif

done:
	slap_graduate_commit_csn( op );

	if( moi == &opinfo ) {
		if( txn != NULL ) {
810
			mdb->mi_numads = numads;
Howard Chu's avatar
Howard Chu committed
811
812
813
814
815
			mdb_txn_abort( txn );
		}
		if ( opinfo.moi_oe.oe_key ) {
			LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
		}
816
817
	} else {
		moi->moi_ref--;
Howard Chu's avatar
Howard Chu committed
818
819
820
	}

	if( e != NULL ) {
Howard Chu's avatar
Howard Chu committed
821
		mdb_entry_return( op, e );
Howard Chu's avatar
Howard Chu committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
	}

	if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
		slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
		slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
	}
	if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
		slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
		slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
	}

	rs->sr_text = NULL;

	return rs->sr_err;
}