add.c 17.9 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2005 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
7
8
9
10
11
12
13
 * 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>.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
14
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
16
17
18
19
20
21
22
23
24
25
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
27
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
28
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
30
31
32
#include <ac/string.h>
#include <ac/time.h>
#include <ac/socket.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
33
34
#include "slap.h"

35
#ifdef LDAP_SLAPI
36
37
#include "slapi/slapi.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
38
39
static void init_add_pblock( Operation *op, struct berval *dn, Entry *e,
	int manageDSAit );
Luke Howard's avatar
Luke Howard committed
40
41
static int call_add_preop_plugins( Operation *op );
static void call_add_postop_plugins( Operation *op );
42
43
#endif /* LDAP_SLAPI */

44
int
45
do_add( Operation *op, SlapReply *rs )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
46
47
{
	BerElement	*ber = op->o_ber;
48
	char		*last;
49
	struct berval	dn = BER_BVNULL;
50
51
	ber_len_t	len;
	ber_tag_t	tag;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
52
	Entry		*e;
53
54
	Modifications	*modlist = NULL;
	Modifications	**modtail = &modlist;
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
55
	Modifications	tmp;
56
57
58
	char		textbuf[ SLAP_TEXT_BUFLEN ];
	size_t		textlen = sizeof( textbuf );
	int		rc = 0;
59

Kurt Zeilenga's avatar
Kurt Zeilenga committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
	/*
	 * Parse the add request.  It looks like this:
	 *
	 *	AddRequest := [APPLICATION 14] SEQUENCE {
	 *		name	DistinguishedName,
	 *		attrs	SEQUENCE OF SEQUENCE {
	 *			type	AttributeType,
	 *			values	SET OF AttributeValue
	 *		}
	 *	}
	 */

	/* get the name */
74
	if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
75
		Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
76
		send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
77
		return SLAPD_DISCONNECT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
78
	}
79

80
81
	e = (Entry *) ch_calloc( 1, sizeof(Entry) );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
82
83
	rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
		op->o_tmpmemctx );
84

85
	if ( rs->sr_err != LDAP_SUCCESS ) {
86
		Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
87
		send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
88
		goto done;
89
90
	}

91
92
93
	ber_dupbv( &e->e_name, &op->o_req_dn );
	ber_dupbv( &e->e_nname, &op->o_req_ndn );

94
	Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
95
96
97

	/* get the attrs */
	for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
98
99
	    tag = ber_next_element( ber, &len, last ) )
	{
100
		Modifications *mod;
101
		ber_tag_t rtag;
102

Kurt Zeilenga's avatar
Kurt Zeilenga committed
103
104
105
		tmp.sml_nvalues = NULL;

		rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
106

107
		if ( rtag == LBER_ERROR ) {
108
			Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
109
			send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
110
			rs->sr_err = SLAPD_DISCONNECT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
111
			goto done;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
112
113
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
114
		if ( tmp.sml_values == NULL ) {
115
			Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
116
				tmp.sml_type.bv_val, 0, 0 );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
117
118
			send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
				"no values for attribute type" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
119
			goto done;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
120
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
121

Howard Chu's avatar
Howard Chu committed
122
		mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
123
124
125
		mod->sml_op = LDAP_MOD_ADD;
		mod->sml_next = NULL;
		mod->sml_desc = NULL;
Howard Chu's avatar
Howard Chu committed
126
		mod->sml_type = tmp.sml_type;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
127
128
		mod->sml_values = tmp.sml_values;
		mod->sml_nvalues = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
129

130
		*modtail = mod;
131
		modtail = &mod->sml_next;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
132
133
	}

134
135
	if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
		Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
136
		send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137
		rs->sr_err = SLAPD_DISCONNECT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
138
		goto done;
139
140
	}

141
	if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
142
		Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
143
		goto done;
144
145
	} 

146
	if ( modlist == NULL ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
147
148
		send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
			"no attributes provided" );
149
150
151
		goto done;
	}

152
153
	Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
	    op->o_log_prefix, e->e_name.bv_val, 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
154

155
	if ( dn_match( &e->e_nname, &slap_empty_bv ) ) {
156
		/* protocolError may be a more appropriate error */
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
157
158
		send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
			"root DSE already exists" );
159
		goto done;
160

161
	} else if ( dn_match( &e->e_nname, &frontendDB->be_schemandn ) ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
162
163
		send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
			"subschema subentry already exists" );
164
		goto done;
165
166
	}

167
	rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
168
		textbuf, textlen, NULL );
169
170
171
172
173
174

	if ( rs->sr_err != LDAP_SUCCESS ) {
		send_ldap_result( op, rs );
		goto done;
	}

175
176
177
178
179
	/* temporary; remove if not invoking backend function */
	op->ora_e = e;
	op->ora_modlist = modlist;

	op->o_bd = frontendDB;
180
181
	rc = frontendDB->be_add( op, rs );
	if ( rc == 0 ) {
182
183
184
185
186
187
		e = NULL;
	}

done:;
	slap_graduate_commit_csn( op );

188
	if ( modlist != NULL ) {
189
190
		slap_mods_free( modlist );
	}
191
	if ( e != NULL ) {
192
193
194
195
196
		entry_free( e );
	}
	op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
	op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );

197
	return rc;
198
199
200
201
202
203
204
205
206
}

int
fe_op_add( Operation *op, SlapReply *rs )
{
	int		manageDSAit;
	Entry		*e = op->ora_e;
	Modifications	*modlist = op->ora_modlist;
	Modifications	**modtail = &modlist;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
207
	int		rc = 0;
208
	BackendDB *op_be;
209

Kurt Zeilenga's avatar
Kurt Zeilenga committed
210
	manageDSAit = get_manageDSAit( op );
211

Kurt Zeilenga's avatar
Kurt Zeilenga committed
212
213
214
215
216
	/*
	 * We could be serving multiple database backends.  Select the
	 * appropriate one, or send a referral to our "referral server"
	 * if we don't hold it.
	 */
217
	op->o_bd = select_backend( &e->e_nname, manageDSAit, 1 );
218
	if ( op->o_bd == NULL ) {
219
		rs->sr_ref = referral_rewrite( default_referral,
220
			NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
221
		if ( !rs->sr_ref ) rs->sr_ref = default_referral;
222
		if ( rs->sr_ref ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
223
			rs->sr_err = LDAP_REFERRAL;
224
			op->o_bd = frontendDB;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
225
			send_ldap_result( op, rs );
226
			op->o_bd = NULL;
227

228
			if ( rs->sr_ref != default_referral ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
229
230
				ber_bvarray_free( rs->sr_ref );
			}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
231
		} else {
232
			op->o_bd = frontendDB;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
233
			send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
234
				"no global superior knowledge" );
235
			op->o_bd = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
236
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
237
		goto done;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238
239
	}

240
241
242
243
244
245
	/* If we've got a glued backend, check the real backend */
	op_be = op->o_bd;
	if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
		op->o_bd = select_backend( &e->e_nname, manageDSAit, 0 );
	}

246
	/* check restrictions */
247
248
	if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
		send_ldap_result( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
249
		goto done;
250
251
	}

252
	/* check for referrals */
253
	if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
254
255
256
		goto done;
	}

257
#ifdef LDAP_SLAPI
258
	if ( op->o_pb ) init_add_pblock( op, &op->o_req_dn, e, manageDSAit );
259
#endif /* LDAP_SLAPI */
260

Kurt Zeilenga's avatar
Kurt Zeilenga committed
261
262
263
264
265
266
	/*
	 * do the add if 1 && (2 || 3)
	 * 1) there is an add function implemented in this backend;
	 * 2) this backend is master for what it holds;
	 * 3) it's a replica and the dn supplied is the updatedn.
	 */
267
	if ( op->o_bd->be_add ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
268
		/* do the update here */
269
		int repl_user = be_isupdate( op );
270
#ifndef SLAPD_MULTIMASTER
271
		if ( !SLAP_SHADOW(op->o_bd) || repl_user )
272
#endif
273
		{
274
275
276
277
			int		update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
			char		textbuf[ SLAP_TEXT_BUFLEN ];
			size_t		textlen = sizeof( textbuf );
			slap_callback	cb = { NULL, slap_replog_cb, NULL, NULL };
278

279
280
			op->o_bd = op_be;

281
282
283
284
			if ( !update ) {
				rs->sr_err = slap_mods_no_update_check( modlist,
						&rs->sr_text,
				  		textbuf, textlen );
285

286
287
288
289
				if ( rs->sr_err != LDAP_SUCCESS ) {
					send_ldap_result( op, rs );
					goto done;
				}
290
291
			}

292
			if ( !repl_user ) {
293
294
295
296
				/* go to the last mod */
				for ( modtail = &modlist;
						*modtail != NULL;
						modtail = &(*modtail)->sml_next )
297
				{
298
299
					assert( (*modtail)->sml_op == LDAP_MOD_ADD );
					assert( (*modtail)->sml_desc != NULL );
300
				}
301
302
303
304
305

				rs->sr_err = slap_mods_opattrs( op, modlist,
						modtail, &rs->sr_text,
						textbuf, textlen, 1 );
				if ( rs->sr_err != LDAP_SUCCESS ) {
306
					send_ldap_result( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
307
					goto done;
308
				}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
309
			}
310

311
			rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
312
				&rs->sr_text, textbuf, textlen );
313
			if ( rs->sr_err != LDAP_SUCCESS ) {
314
				send_ldap_result( op, rs );
315
316
317
				goto done;
			}

318
319
320
321
322
#ifdef LDAP_SLAPI
			/*
			 * Call the preoperation plugin here, because the entry
			 * will actually contain something.
			 */
323
			if ( op->o_pb ) {
Luke Howard's avatar
Luke Howard committed
324
				rs->sr_err = call_add_preop_plugins( op );
325
326
327
328
				if ( rs->sr_err != LDAP_SUCCESS ) {
					/* plugin will have sent result */
					goto done;
				}
329
330
331
			}
#endif /* LDAP_SLAPI */

Howard Chu's avatar
Howard Chu committed
332
			op->ora_e = e;
333
#ifdef SLAPD_MULTIMASTER
334
			if ( !repl_user )
335
#endif
336
337
338
339
			{
				cb.sc_next = op->o_callback;
				op->o_callback = &cb;
			}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
340
341
342
343
344
345
			rc = (op->o_bd->be_add)( op, rs );
			if ( rc == 0 ) {
				/* FIXME: be_entry_release_w() should be
				 * called by do_add(), so that global
				 * overlays on the way back can
				 * at least read the entry */
346
				be_entry_release_w( op, e );
347
				e = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348
			}
349
			op->ora_e = NULL;
350

351
#ifndef SLAPD_MULTIMASTER
Kurt Zeilenga's avatar
Kurt Zeilenga committed
352
		} else {
353
			BerVarray defref = NULL;
354
355
356
357
358
#ifdef LDAP_SLAPI
			/*
			 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
			 * on replicas (for now, it involves the minimum code intrusion).
			 */
359
			if ( op->o_pb ) {
Luke Howard's avatar
Luke Howard committed
360
				rs->sr_err = call_add_preop_plugins( op );
361
362
363
364
				if ( rs->sr_err != LDAP_SUCCESS ) {
					/* plugin will have sent result */
					goto done;
				}
365
366
367
			}
#endif /* LDAP_SLAPI */

368
			defref = op->o_bd->be_update_refs
369
				? op->o_bd->be_update_refs : default_referral;
370

371
372
373
			if ( defref != NULL ) {
				rs->sr_ref = referral_rewrite( defref,
					NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
374
				if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
375
				rs->sr_err = LDAP_REFERRAL;
376
				if (!rs->sr_ref) rs->sr_ref = default_referral;
377
				send_ldap_result( op, rs );
378

379
				if ( rs->sr_ref != default_referral ) {
380
381
					ber_bvarray_free( rs->sr_ref );
				}
382
383
			} else {
				send_ldap_error( op, rs,
384
385
					LDAP_UNWILLING_TO_PERFORM,
					"shadow context; no update referral" );
386
			}
387
#endif /* SLAPD_MULTIMASTER */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
388
389
		}
	} else {
Howard Chu's avatar
Howard Chu committed
390
#ifdef LDAP_SLAPI
391
		if ( op->o_pb ) {
Luke Howard's avatar
Luke Howard committed
392
			rs->sr_err = call_add_preop_plugins( op );
393
394
395
396
			if ( rs->sr_err != LDAP_SUCCESS ) {
				/* plugin will have sent result */
				goto done;
			}
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
397
		}
Howard Chu's avatar
Howard Chu committed
398
#endif
Gary Williams's avatar
Gary Williams committed
399
	    Debug( LDAP_DEBUG_ARGS, "	 do_add: no backend support\n", 0, 0, 0 );
400
	    send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
401
			"operation not supported within namingContext" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
402
	}
403

404
#ifdef LDAP_SLAPI
Luke Howard's avatar
Luke Howard committed
405
	if ( op->o_pb ) call_add_postop_plugins( op );
406
#endif /* LDAP_SLAPI */
407

408
done:;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
409
	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
410
411
}

412
413
int
slap_mods2entry(
414
415
	Modifications *mods,
	Entry **e,
416
	int repl_user,
417
	int dup,
418
419
	const char **text,
	char *textbuf, size_t textlen )
420
{
421
422
423
	Attribute **tail = &(*e)->e_attrs;
	assert( *tail == NULL );

424
425
	*text = textbuf;

426
427
428
	for( ; mods != NULL; mods = mods->sml_next ) {
		Attribute *attr;

429
		if ( !repl_user ) {
430
431
			assert( mods->sml_op == LDAP_MOD_ADD );
		}
432
		assert( mods->sml_desc != NULL );
433
434
435
436

		attr = attr_find( (*e)->e_attrs, mods->sml_desc );

		if( attr != NULL ) {
437
438
439
440
#define SLURPD_FRIENDLY
#ifdef SLURPD_FRIENDLY
			ber_len_t i,j;

441
			if( !repl_user ) {
442
443
444
				snprintf( textbuf, textlen,
					"attribute '%s' provided more than once",
					mods->sml_desc->ad_cname.bv_val );
445
446
447
				return LDAP_TYPE_OR_VALUE_EXISTS;
			}

448
			for( i=0; attr->a_vals[i].bv_val; i++ ) {
449
450
				/* count them */
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
451
			for( j=0; mods->sml_values[j].bv_val; j++ ) {
452
453
454
455
456
				/* count them */
			}
			j++;	/* NULL */
			
			attr->a_vals = ch_realloc( attr->a_vals,
457
				sizeof( struct berval ) * (i+j) );
458
459

			/* should check for duplicates */
460

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
461
462
463
464
465
466
467
468
469
470
471
			if ( dup ) {
				for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
					ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
				}
				BER_BVZERO( &attr->a_vals[i+j] );	
			} else {
				AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
					sizeof( struct berval ) * j );
				ch_free( mods->sml_values );
				mods->sml_values = NULL;
			}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
472

473
			if( mods->sml_nvalues ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
474
475
				attr->a_nvals = ch_realloc( attr->a_nvals,
					sizeof( struct berval ) * (i+j) );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
476
477
478
479
480
481
482
483
484
485
486
				if ( dup ) {
					for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
						ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
					}
					BER_BVZERO( &attr->a_nvals[i+j] );	
				} else {
					AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
						sizeof( struct berval ) * j );
					ch_free( mods->sml_nvalues );
					mods->sml_nvalues = NULL;
				}
487
488
			} else {
				attr->a_nvals = attr->a_vals;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
489
			}
490
491
492

			continue;
#else
493
494
495
			snprintf( textbuf, textlen,
				"attribute '%s' provided more than once",
				mods->sml_desc->ad_cname.bv_val );
496
497
			return LDAP_TYPE_OR_VALUE_EXISTS;
#endif
498
499
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
500
		if( mods->sml_values[1].bv_val != NULL ) {
501
502
503
504
505
506
			/* check for duplicates */
			int		i, j;
			MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;

			/* check if the values we're adding already exist */
			if( mr == NULL || !mr->smr_match ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
507
				for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
508
509
					/* test asserted values against themselves */
					for( j = 0; j < i; j++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
510
511
512
						if ( bvmatch( &mods->sml_values[i],
							&mods->sml_values[j] ) )
						{
513
							/* value exists already */
514
515
516
							snprintf( textbuf, textlen,
								"%s: value #%d provided more than once",
								mods->sml_desc->ad_cname.bv_val, j );
517
518
519
520
521
522
							return LDAP_TYPE_OR_VALUE_EXISTS;
						}
					}
				}

			} else {
523
				int	rc;
524
525
				int match;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
526
				for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
527
528
529
					/* test asserted values against themselves */
					for( j = 0; j < i; j++ ) {
						rc = value_match( &match, mods->sml_desc, mr,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
530
531
							SLAP_MR_EQUALITY
							| SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
532
533
							| SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
							| SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
534
535
536
537
538
539
540
							mods->sml_nvalues
								? &mods->sml_nvalues[i]
								: &mods->sml_values[i],
							mods->sml_nvalues
								? &mods->sml_nvalues[j]
								: &mods->sml_values[j],
							text );
541

542
543
544
545
546
547
						if ( rc == LDAP_SUCCESS && match == 0 ) {
							/* value exists already */
							snprintf( textbuf, textlen,
								"%s: value #%d provided more than once",
								mods->sml_desc->ad_cname.bv_val, j );
							return LDAP_TYPE_OR_VALUE_EXISTS;
548
549
550

						} else if ( rc != LDAP_SUCCESS ) {
							return rc;
551
552
553
						}
					}
				}
554
555
556
			}
		}

557
558
		attr = ch_calloc( 1, sizeof(Attribute) );

559
560
		/* move ad to attr structure */
		attr->a_desc = mods->sml_desc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
561
		if ( !dup ) mods->sml_desc = NULL;
562
563
564

		/* move values to attr structure */
		/*	should check for duplicates */
565
566
		if ( dup ) { 
			int i;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
567
			for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
568
			attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
569
			for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
570
				ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
571
			}
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
572
			BER_BVZERO( &attr->a_vals[i] );
573
574
575
576
		} else {
			attr->a_vals = mods->sml_values;
			mods->sml_values = NULL;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
577

578
		if ( mods->sml_nvalues ) {
579
580
			if ( dup ) {
				int i;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
581
				for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
582
				attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
583
				for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
584
					ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
585
				}
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
586
				BER_BVZERO( &attr->a_nvals[i] );
587
588
589
590
			} else {
				attr->a_nvals = mods->sml_nvalues;
				mods->sml_nvalues = NULL;
			}
591
592
593
		} else {
			attr->a_nvals = attr->a_vals;
		}
594
595
596
597
598

		*tail = attr;
		tail = &attr->a_next;
	}

599
600
	*text = NULL;

601
	return LDAP_SUCCESS;
602
}
603

604
605
606
607
int
slap_entry2mods(
	Entry *e,
	Modifications **mods,
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
608
609
	const char **text,
	char *textbuf, size_t textlen )
610
611
612
613
614
615
{
	Modifications	*modhead = NULL;
	Modifications	*mod;
	Modifications	**modtail = &modhead;
	Attribute		*a_new;
	AttributeDescription	*a_new_desc;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
616
617
	int				i, count;

618
619
620
621
622
623
	a_new = e->e_attrs;

	while ( a_new != NULL ) {
		a_new_desc = a_new->a_desc;
		mod = (Modifications *) malloc( sizeof( Modifications ));
		
624
		mod->sml_op = LDAP_MOD_REPLACE;
625

626
		mod->sml_type = a_new_desc->ad_cname;
627

Kurt Zeilenga's avatar
Kurt Zeilenga committed
628
		for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
629

Kurt Zeilenga's avatar
Kurt Zeilenga committed
630
631
		mod->sml_values = (struct berval*) malloc(
			(count+1) * sizeof( struct berval) );
632

633
634
635
636
637
638
		/* see slap_mods_check() comments...
		 * if a_vals == a_nvals, there is no normalizer.
		 * in this case, mod->sml_nvalues must be left NULL.
		 */
		if ( a_new->a_vals != a_new->a_nvals ) {
			mod->sml_nvalues = (struct berval*) malloc(
639
				(count+1) * sizeof( struct berval) );
640
641
642
		} else {
			mod->sml_nvalues = NULL;
		}
643
644

		for ( i = 0; i < count; i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
645
			ber_dupbv(mod->sml_values+i, a_new->a_vals+i); 
646
			if ( mod->sml_nvalues ) {
647
				ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i ); 
648
649
650
			} 
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
651
652
		mod->sml_values[count].bv_val = NULL; 
		mod->sml_values[count].bv_len = 0; 
653

654
		if ( mod->sml_nvalues ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
655
			mod->sml_nvalues[count].bv_val = NULL; 
656
657
			mod->sml_nvalues[count].bv_len = 0; 
		}
658

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
659
		mod->sml_desc = a_new_desc;
660
661
662
663
664
665
		mod->sml_next =NULL;
		*modtail = mod;
		modtail = &mod->sml_next;
		a_new = a_new->a_next; 
	}

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
666
	*mods = modhead;
667
668
669
670

	return LDAP_SUCCESS;
}

671
#ifdef LDAP_SLAPI
Luke Howard's avatar
Luke Howard committed
672
static void init_add_pblock( Operation *op,
673
674
	struct berval *dn, Entry *e, int manageDSAit )
{
675
	slapi_int_pblock_set_operation( op->o_pb, op );
676
677
678
	slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
	slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
	slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
679
680
}

Luke Howard's avatar
Luke Howard committed
681
static int call_add_preop_plugins( Operation *op )
682
683
684
{
	int rc;

Luke Howard's avatar
Luke Howard committed
685
	rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
Howard Chu's avatar
Howard Chu committed
686
	if ( rc < 0 ) {
687
688
689
690
		/*
		 * A preoperation plugin failure will abort the
		 * entire operation.
		 */
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
691
692
693
694
695
696
697
		Debug(LDAP_DEBUG_TRACE,
			"do_add: add preoperation plugin failed.\n",
			0, 0, 0);

		if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
			(void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
		{
698
			rc = LDAP_OTHER;
699
		}
700
701
702
703
704
705
706
	} else {
		rc = LDAP_SUCCESS;
	}

	return rc;
}

Luke Howard's avatar
Luke Howard committed
707
static void call_add_postop_plugins( Operation *op )
708
709
710
{
	int rc;

Luke Howard's avatar
Luke Howard committed
711
	rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
Howard Chu's avatar
Howard Chu committed
712
	if ( rc < 0 ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
713
714
715
		Debug(LDAP_DEBUG_TRACE,
			"do_add: add postoperation plugin failed\n",
			0, 0, 0);
716
717
718
	}
}
#endif /* LDAP_SLAPI */