3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
21 #include <ac/string.h>
23 #include <ac/socket.h>
30 static void initAddPlugin( Operation *op,
31 struct berval *dn, Entry *e, int manageDSAit );
32 static int doPreAddPluginFNs( Operation *op );
33 static void doPostAddPluginFNs( Operation *op );
34 #endif /* LDAP_SLAPI */
37 do_add( Operation *op, SlapReply *rs )
39 BerElement *ber = op->o_ber;
41 struct berval dn = { 0, NULL };
45 Modifications *modlist = NULL;
46 Modifications **modtail = &modlist;
51 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", op->o_connid,0,0 );
53 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
56 * Parse the add request. It looks like this:
58 * AddRequest := [APPLICATION 14] SEQUENCE {
59 * name DistinguishedName,
60 * attrs SEQUENCE OF SEQUENCE {
62 * values SET OF AttributeValue
68 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
70 LDAP_LOG( OPERATION, ERR,
71 "do_add: conn %d ber_scanf failed\n", op->o_connid,0,0 );
73 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
75 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
79 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
81 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
83 if( rs->sr_err != LDAP_SUCCESS ) {
85 LDAP_LOG( OPERATION, ERR,
86 "do_add: conn %d invalid dn (%s)\n", op->o_connid, dn.bv_val, 0 );
88 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
90 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
94 ber_dupbv( &e->e_name, &op->o_req_dn );
95 ber_dupbv( &e->e_nname, &op->o_req_ndn );
98 LDAP_LOG( OPERATION, ARGS,
99 "do_add: conn %d dn (%s)\n", op->o_connid, e->e_dn, 0 );
101 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
105 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
106 tag = ber_next_element( ber, &len, last ) )
111 tmp.sml_nvalues = NULL;
113 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
115 if ( rtag == LBER_ERROR ) {
117 LDAP_LOG( OPERATION, ERR,
118 "do_add: conn %d decoding error \n", op->o_connid, 0, 0 );
120 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
122 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
127 if ( tmp.sml_values == NULL ) {
129 LDAP_LOG( OPERATION, INFO,
130 "do_add: conn %d no values for type %s\n",
131 op->o_connid, tmp.sml_type.bv_val, 0 );
133 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
134 tmp.sml_type.bv_val, 0, 0 );
136 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "no values for attribute type" );
140 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
141 mod->sml_op = LDAP_MOD_ADD;
142 mod->sml_next = NULL;
143 mod->sml_desc = NULL;
144 mod->sml_type = tmp.sml_type;
145 mod->sml_values = tmp.sml_values;
146 mod->sml_nvalues = NULL;
149 modtail = &mod->sml_next;
152 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
154 LDAP_LOG( OPERATION, ERR,
155 "do_add: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
157 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
159 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
164 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
166 LDAP_LOG( OPERATION, INFO,
167 "do_add: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
169 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
174 if ( modlist == NULL ) {
175 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "no attributes provided" );
179 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
180 op->o_connid, op->o_opid, e->e_name.bv_val, 0, 0 );
182 if( e->e_nname.bv_len == 0 ) {
183 /* protocolError may be a more appropriate error */
184 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS, "root DSE already exists" );
187 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
188 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS, "subschema subentry already exists" );
192 manageDSAit = get_manageDSAit( op );
195 * We could be serving multiple database backends. Select the
196 * appropriate one, or send a referral to our "referral server"
197 * if we don't hold it.
199 op->o_bd = select_backend( &e->e_nname, manageDSAit, 0 );
200 if ( op->o_bd == NULL ) {
201 rs->sr_ref = referral_rewrite( default_referral,
202 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
204 rs->sr_err = LDAP_REFERRAL;
205 if (!rs->sr_ref) rs->sr_ref = default_referral;
206 send_ldap_result( op, rs );
208 if ( rs->sr_ref != default_referral ) ber_bvarray_free( rs->sr_ref );
212 /* check restrictions */
213 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
214 send_ldap_result( op, rs );
218 /* check for referrals */
219 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
224 initAddPlugin( op, &dn, e, manageDSAit );
225 #endif /* LDAP_SLAPI */
228 * do the add if 1 && (2 || 3)
229 * 1) there is an add function implemented in this backend;
230 * 2) this backend is master for what it holds;
231 * 3) it's a replica and the dn supplied is the updatedn.
233 if ( op->o_bd->be_add ) {
234 /* do the update here */
235 int repl_user = be_isupdate(op->o_bd, &op->o_ndn );
236 #ifndef SLAPD_MULTIMASTER
237 if ( !op->o_bd->be_update_ndn.bv_len || repl_user )
240 int update = op->o_bd->be_update_ndn.bv_len;
241 char textbuf[SLAP_TEXT_BUFLEN];
242 size_t textlen = sizeof textbuf;
244 rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
245 textbuf, textlen, NULL );
247 if( rs->sr_err != LDAP_SUCCESS ) {
248 send_ldap_result( op, rs );
253 for( modtail = &modlist;
255 modtail = &(*modtail)->sml_next )
257 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
258 assert( (*modtail)->sml_desc != NULL );
260 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
261 &rs->sr_text, textbuf, textlen );
262 if( rs->sr_err != LDAP_SUCCESS ) {
263 send_ldap_result( op, rs );
268 rs->sr_err = slap_mods2entry( modlist, &e, repl_user, &rs->sr_text,
270 if( rs->sr_err != LDAP_SUCCESS ) {
271 send_ldap_result( op, rs );
277 * Call the preoperation plugin here, because the entry
278 * will actually contain something.
280 rs->sr_err = doPreAddPluginFNs( op );
281 if ( rs->sr_err != LDAP_SUCCESS ) {
282 /* plugin will have sent result */
285 #endif /* LDAP_SLAPI */
288 if ( (op->o_bd->be_add)( op, rs ) == 0 ) {
289 #ifdef SLAPD_MULTIMASTER
295 be_entry_release_w( op, e );
299 #ifndef SLAPD_MULTIMASTER
304 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
305 * on replicas (for now, it involves the minimum code intrusion).
307 rs->sr_err = doPreAddPluginFNs( op );
308 if ( rs->sr_err != LDAP_SUCCESS ) {
309 /* plugin will have sent result */
312 #endif /* LDAP_SLAPI */
314 defref = op->o_bd->be_update_refs
315 ? op->o_bd->be_update_refs : default_referral;
316 rs->sr_ref = referral_rewrite( defref,
317 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
319 rs->sr_err = LDAP_REFERRAL;
320 if (!rs->sr_ref) rs->sr_ref = default_referral;
321 send_ldap_result( op, rs );
323 if ( rs->sr_ref != default_referral ) ber_bvarray_free( rs->sr_ref );
324 #endif /* SLAPD_MULTIMASTER */
328 rs->sr_err = doPreAddPluginFNs( op );
329 if ( rs->sr_err != LDAP_SUCCESS ) {
330 /* plugin will have sent result */
335 LDAP_LOG( OPERATION, INFO,
336 "do_add: conn %d no backend support\n", op->o_connid, 0, 0 );
338 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
340 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
341 "operation not supported within namingContext" );
345 doPostAddPluginFNs( op );
346 #endif /* LDAP_SLAPI */
349 if( modlist != NULL ) {
350 slap_mods_free( modlist );
355 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
356 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
367 char *textbuf, size_t textlen )
369 Attribute **tail = &(*e)->e_attrs;
370 assert( *tail == NULL );
374 for( ; mods != NULL; mods = mods->sml_next ) {
377 assert( mods->sml_op == LDAP_MOD_ADD );
378 assert( mods->sml_desc != NULL );
380 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
383 #define SLURPD_FRIENDLY
384 #ifdef SLURPD_FRIENDLY
388 snprintf( textbuf, textlen,
389 "attribute '%s' provided more than once",
390 mods->sml_desc->ad_cname.bv_val );
391 return LDAP_TYPE_OR_VALUE_EXISTS;
394 for( i=0; attr->a_vals[i].bv_val; i++ ) {
397 for( j=0; mods->sml_values[j].bv_val; j++ ) {
402 attr->a_vals = ch_realloc( attr->a_vals,
403 sizeof( struct berval ) * (i+j) );
405 /* should check for duplicates */
407 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
408 sizeof( struct berval ) * j );
410 /* trim the mods array */
411 ch_free( mods->sml_values );
412 mods->sml_values = NULL;
414 if( mods->sml_nvalues ) {
415 attr->a_nvals = ch_realloc( attr->a_nvals,
416 sizeof( struct berval ) * (i+j) );
418 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
419 sizeof( struct berval ) * j );
421 /* trim the mods array */
422 ch_free( mods->sml_nvalues );
423 mods->sml_nvalues = NULL;
425 attr->a_nvals = attr->a_vals;
430 snprintf( textbuf, textlen,
431 "attribute '%s' provided more than once",
432 mods->sml_desc->ad_cname.bv_val );
433 return LDAP_TYPE_OR_VALUE_EXISTS;
437 if( mods->sml_values[1].bv_val != NULL ) {
438 /* check for duplicates */
440 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
442 /* check if the values we're adding already exist */
443 if( mr == NULL || !mr->smr_match ) {
444 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
445 /* test asserted values against themselves */
446 for( j = 0; j < i; j++ ) {
447 if ( bvmatch( &mods->sml_bvalues[i],
448 &mods->sml_bvalues[j] ) ) {
449 /* value exists already */
450 snprintf( textbuf, textlen,
451 "%s: value #%d provided more than once",
452 mods->sml_desc->ad_cname.bv_val, j );
453 return LDAP_TYPE_OR_VALUE_EXISTS;
459 int rc = LDAP_SUCCESS;
462 for ( i = 0; mods->sml_nvalues[i].bv_val != NULL; i++ ) {
463 /* test asserted values against themselves */
464 for( j = 0; j < i; j++ ) {
465 rc = value_match( &match, mods->sml_desc, mr,
466 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
467 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
468 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
469 &mods->sml_nvalues[i], &mods->sml_nvalues[j], text );
470 if ( rc == LDAP_SUCCESS && match == 0 ) {
471 /* value exists already */
472 snprintf( textbuf, textlen,
473 "%s: value #%d provided more than once",
474 mods->sml_desc->ad_cname.bv_val, j );
475 return LDAP_TYPE_OR_VALUE_EXISTS;
479 if ( rc != LDAP_SUCCESS ) {
485 attr = ch_calloc( 1, sizeof(Attribute) );
487 /* move ad to attr structure */
488 attr->a_desc = mods->sml_desc;
489 mods->sml_desc = NULL;
491 /* move values to attr structure */
492 /* should check for duplicates */
493 attr->a_vals = mods->sml_values;
494 mods->sml_values = NULL;
496 if ( mods->sml_nvalues ) {
497 attr->a_nvals = mods->sml_nvalues;
498 mods->sml_nvalues = NULL;
500 attr->a_nvals = attr->a_vals;
504 tail = &attr->a_next;
511 static void initAddPlugin( Operation *op,
512 struct berval *dn, Entry *e, int manageDSAit )
514 slapi_x_pblock_set_operation( op->o_pb, op );
515 slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
516 slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
517 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
520 static int doPreAddPluginFNs( Operation *op )
524 rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
527 * A preoperation plugin failure will abort the
531 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
534 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
536 if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
546 static void doPostAddPluginFNs( Operation *op )
550 rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
553 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
556 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
561 #endif /* LDAP_SLAPI */