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,
137 "no values for attribute type" );
141 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
142 mod->sml_op = LDAP_MOD_ADD;
143 mod->sml_next = NULL;
144 mod->sml_desc = NULL;
145 mod->sml_type = tmp.sml_type;
146 mod->sml_values = tmp.sml_values;
147 mod->sml_nvalues = NULL;
150 modtail = &mod->sml_next;
153 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
155 LDAP_LOG( OPERATION, ERR,
156 "do_add: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
158 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
160 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
165 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
167 LDAP_LOG( OPERATION, INFO,
168 "do_add: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
170 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
175 if ( modlist == NULL ) {
176 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
177 "no attributes provided" );
181 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
182 op->o_connid, op->o_opid, e->e_name.bv_val, 0, 0 );
184 if( e->e_nname.bv_len == 0 ) {
185 /* protocolError may be a more appropriate error */
186 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
187 "root DSE already exists" );
190 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
191 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
192 "subschema subentry already exists" );
196 manageDSAit = get_manageDSAit( op );
199 * We could be serving multiple database backends. Select the
200 * appropriate one, or send a referral to our "referral server"
201 * if we don't hold it.
203 op->o_bd = select_backend( &e->e_nname, manageDSAit, 0 );
204 if ( op->o_bd == NULL ) {
205 rs->sr_ref = referral_rewrite( default_referral,
206 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
207 if (!rs->sr_ref) rs->sr_ref = default_referral;
208 if ( rs->sr_ref != NULL ) {
209 rs->sr_err = LDAP_REFERRAL;
210 send_ldap_result( op, rs );
212 if ( rs->sr_ref != default_referral ) {
213 ber_bvarray_free( rs->sr_ref );
216 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
217 "referral missing" );
222 /* check restrictions */
223 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
224 send_ldap_result( op, rs );
228 /* check for referrals */
229 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
234 initAddPlugin( op, &dn, e, manageDSAit );
235 #endif /* LDAP_SLAPI */
238 * do the add if 1 && (2 || 3)
239 * 1) there is an add function implemented in this backend;
240 * 2) this backend is master for what it holds;
241 * 3) it's a replica and the dn supplied is the updatedn.
243 if ( op->o_bd->be_add ) {
244 /* do the update here */
245 int repl_user = be_isupdate(op->o_bd, &op->o_ndn );
246 #if defined(LDAP_SYNCREPL) && !defined(SLAPD_MULTIMASTER)
247 if ( !op->o_bd->syncinfo &&
248 ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
249 #elif defined(LDAP_SYNCREPL) && defined(SLAPD_MULTIMASTER)
250 if ( !op->o_bd->syncinfo ) /* LDAP_SYNCREPL overrides MM */
251 #elif !defined(LDAP_SYNCREPL) && !defined(SLAPD_MULTIMASTER)
252 if ( !op->o_bd->be_update_ndn.bv_len || repl_user )
255 int update = op->o_bd->be_update_ndn.bv_len;
256 char textbuf[SLAP_TEXT_BUFLEN];
257 size_t textlen = sizeof textbuf;
259 rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
260 textbuf, textlen, NULL );
262 if( rs->sr_err != LDAP_SUCCESS ) {
263 send_ldap_result( op, rs );
268 for( modtail = &modlist;
270 modtail = &(*modtail)->sml_next )
272 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
273 assert( (*modtail)->sml_desc != NULL );
275 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
276 &rs->sr_text, textbuf, textlen );
277 if( rs->sr_err != LDAP_SUCCESS ) {
278 send_ldap_result( op, rs );
283 rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
284 &rs->sr_text, textbuf, textlen );
285 if( rs->sr_err != LDAP_SUCCESS ) {
286 send_ldap_result( op, rs );
292 * Call the preoperation plugin here, because the entry
293 * will actually contain something.
295 rs->sr_err = doPreAddPluginFNs( op );
296 if ( rs->sr_err != LDAP_SUCCESS ) {
297 /* plugin will have sent result */
300 #endif /* LDAP_SLAPI */
303 if ( (op->o_bd->be_add)( op, rs ) == 0 ) {
304 #ifdef SLAPD_MULTIMASTER
310 be_entry_release_w( op, e );
314 #if defined(LDAP_SYNCREPL) || !defined(SLAPD_MULTIMASTER)
316 BerVarray defref = NULL;
319 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
320 * on replicas (for now, it involves the minimum code intrusion).
322 rs->sr_err = doPreAddPluginFNs( op );
323 if ( rs->sr_err != LDAP_SUCCESS ) {
324 /* plugin will have sent result */
327 #endif /* LDAP_SLAPI */
330 if ( op->o_bd->syncinfo ) {
331 defref = op->o_bd->syncinfo->provideruri_bv;
335 defref = op->o_bd->be_update_refs
336 ? op->o_bd->be_update_refs : default_referral;
339 if ( defref != NULL ) {
340 rs->sr_ref = referral_rewrite( defref,
341 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
342 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
343 rs->sr_err = LDAP_REFERRAL;
344 if (!rs->sr_ref) rs->sr_ref = default_referral;
345 send_ldap_result( op, rs );
347 if ( rs->sr_ref != default_referral ) ber_bvarray_free( rs->sr_ref );
349 send_ldap_error( op, rs,
350 LDAP_UNWILLING_TO_PERFORM,
351 "referral missing" );
353 #endif /* SLAPD_MULTIMASTER */
357 rs->sr_err = doPreAddPluginFNs( op );
358 if ( rs->sr_err != LDAP_SUCCESS ) {
359 /* plugin will have sent result */
364 LDAP_LOG( OPERATION, INFO,
365 "do_add: conn %d no backend support\n", op->o_connid, 0, 0 );
367 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
369 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
370 "operation not supported within namingContext" );
374 doPostAddPluginFNs( op );
375 #endif /* LDAP_SLAPI */
380 slap_graduate_commit_csn( op );
383 if( modlist != NULL ) {
384 slap_mods_free( modlist );
389 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
390 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
402 char *textbuf, size_t textlen )
404 Attribute **tail = &(*e)->e_attrs;
405 assert( *tail == NULL );
409 for( ; mods != NULL; mods = mods->sml_next ) {
416 assert( mods->sml_op == LDAP_MOD_ADD );
418 assert( mods->sml_desc != NULL );
420 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
423 #define SLURPD_FRIENDLY
424 #ifdef SLURPD_FRIENDLY
428 snprintf( textbuf, textlen,
429 "attribute '%s' provided more than once",
430 mods->sml_desc->ad_cname.bv_val );
431 return LDAP_TYPE_OR_VALUE_EXISTS;
434 for( i=0; attr->a_vals[i].bv_val; i++ ) {
437 for( j=0; mods->sml_values[j].bv_val; j++ ) {
442 attr->a_vals = ch_realloc( attr->a_vals,
443 sizeof( struct berval ) * (i+j) );
445 /* should check for duplicates */
447 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
448 sizeof( struct berval ) * j );
450 /* trim the mods array */
451 ch_free( mods->sml_values );
452 mods->sml_values = NULL;
454 if( mods->sml_nvalues ) {
455 attr->a_nvals = ch_realloc( attr->a_nvals,
456 sizeof( struct berval ) * (i+j) );
458 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
459 sizeof( struct berval ) * j );
461 /* trim the mods array */
462 ch_free( mods->sml_nvalues );
463 mods->sml_nvalues = NULL;
466 attr->a_nvals = attr->a_vals;
471 snprintf( textbuf, textlen,
472 "attribute '%s' provided more than once",
473 mods->sml_desc->ad_cname.bv_val );
474 return LDAP_TYPE_OR_VALUE_EXISTS;
478 if( mods->sml_values[1].bv_val != NULL ) {
479 /* check for duplicates */
481 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
483 /* check if the values we're adding already exist */
484 if( mr == NULL || !mr->smr_match ) {
485 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
486 /* test asserted values against themselves */
487 for( j = 0; j < i; j++ ) {
488 if ( bvmatch( &mods->sml_bvalues[i],
489 &mods->sml_bvalues[j] ) ) {
490 /* value exists already */
491 snprintf( textbuf, textlen,
492 "%s: value #%d provided more than once",
493 mods->sml_desc->ad_cname.bv_val, j );
494 return LDAP_TYPE_OR_VALUE_EXISTS;
500 int rc = LDAP_SUCCESS;
503 for ( i = 0; mods->sml_values[i].bv_val != NULL; i++ ) {
504 /* test asserted values against themselves */
505 for( j = 0; j < i; j++ ) {
506 rc = value_match( &match, mods->sml_desc, mr,
507 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
508 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
509 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
511 ? &mods->sml_nvalues[i]
512 : &mods->sml_values[i],
514 ? &mods->sml_nvalues[j]
515 : &mods->sml_values[j],
517 if ( rc == LDAP_SUCCESS && match == 0 ) {
518 /* value exists already */
519 snprintf( textbuf, textlen,
520 "%s: value #%d provided more than once",
521 mods->sml_desc->ad_cname.bv_val, j );
522 return LDAP_TYPE_OR_VALUE_EXISTS;
526 if ( rc != LDAP_SUCCESS ) {
532 attr = ch_calloc( 1, sizeof(Attribute) );
534 /* move ad to attr structure */
535 attr->a_desc = mods->sml_desc;
537 mods->sml_desc = NULL;
539 /* move values to attr structure */
540 /* should check for duplicates */
543 for ( i = 0; mods->sml_values[i].bv_val; i++ ) ;
544 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
545 for ( i = 0; mods->sml_values[i].bv_val; i++ )
546 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
547 attr->a_vals[i].bv_len = 0;
548 attr->a_vals[i].bv_val = NULL;
550 attr->a_vals = mods->sml_values;
551 mods->sml_values = NULL;
554 if ( mods->sml_nvalues ) {
557 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) ;
558 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
559 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ )
560 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
561 attr->a_nvals[i].bv_len = 0;
562 attr->a_nvals[i].bv_val = NULL;
564 attr->a_nvals = mods->sml_nvalues;
565 mods->sml_nvalues = NULL;
568 attr->a_nvals = attr->a_vals;
572 tail = &attr->a_next;
581 Modifications **mods,
585 Modifications *modhead = NULL;
587 Modifications **modtail = &modhead;
589 AttributeDescription *a_new_desc;
594 while ( a_new != NULL ) {
595 a_new_desc = a_new->a_desc;
596 mod = (Modifications *) malloc( sizeof( Modifications ));
598 if ( a_new_desc != slap_schema.si_ad_queryid )
599 mod->sml_op = LDAP_MOD_REPLACE;
601 mod->sml_op = LDAP_MOD_ADD;
603 ber_dupbv( &mod->sml_type, &a_new_desc->ad_cname );
605 for ( count = 0; a_new->a_vals[count].bv_val; count++ );
607 mod->sml_bvalues = (struct berval*) malloc(
608 (count+1) * sizeof( struct berval) );
610 mod->sml_nvalues = (struct berval*) malloc(
611 (count+1) * sizeof( struct berval) );
613 for ( i = 0; i < count; i++ ) {
614 ber_dupbv(mod->sml_bvalues+i, a_new->a_vals+i);
615 if ( a_new->a_desc->ad_type->sat_equality &&
616 a_new->a_desc->ad_type->sat_equality->smr_normalize ) {
617 rc = a_new->a_desc->ad_type->sat_equality->smr_normalize(
619 a_new->a_desc->ad_type->sat_syntax,
620 a_new->a_desc->ad_type->sat_equality,
621 a_new->a_vals+i, mod->sml_nvalues+i, NULL );
627 ber_dupbv( mod->sml_nvalues+i, a_new->a_vals+i );
631 mod->sml_bvalues[count].bv_val = 0;
632 mod->sml_bvalues[count].bv_len = 0;
634 mod->sml_nvalues[count].bv_val = 0;
635 mod->sml_nvalues[count].bv_len = 0;
637 mod->sml_desc = NULL;
638 slap_bv2ad(&mod->sml_type, &mod->sml_desc, text);
641 modtail = &mod->sml_next;
642 a_new = a_new->a_next;
651 static void initAddPlugin( Operation *op,
652 struct berval *dn, Entry *e, int manageDSAit )
654 slapi_x_pblock_set_operation( op->o_pb, op );
655 slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
656 slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
657 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
660 static int doPreAddPluginFNs( Operation *op )
664 rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
667 * A preoperation plugin failure will abort the
671 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
674 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
676 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
677 rc == LDAP_SUCCESS ) {
688 static void doPostAddPluginFNs( Operation *op )
692 rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
695 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
698 Debug(LDAP_DEBUG_TRACE, "do_add: add postoperation plugin failed.\n",
703 #endif /* LDAP_SLAPI */