2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
29 #include <ac/string.h>
31 #include <ac/socket.h>
37 #include "slapi/slapi.h"
39 static void init_add_pblock( Operation *op, struct berval *dn, Entry *e, int manageDSAit );
40 static int call_add_preop_plugins( Operation *op );
41 static void call_add_postop_plugins( Operation *op );
42 #endif /* LDAP_SLAPI */
45 do_add( Operation *op, SlapReply *rs )
47 BerElement *ber = op->o_ber;
49 struct berval dn = { 0, NULL };
53 Modifications *modlist = NULL;
54 Modifications **modtail = &modlist;
59 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", op->o_connid,0,0 );
61 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
64 * Parse the add request. It looks like this:
66 * AddRequest := [APPLICATION 14] SEQUENCE {
67 * name DistinguishedName,
68 * attrs SEQUENCE OF SEQUENCE {
70 * values SET OF AttributeValue
76 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
78 LDAP_LOG( OPERATION, ERR,
79 "do_add: conn %d ber_scanf failed\n", op->o_connid,0,0 );
81 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
83 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
87 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
89 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
91 if( rs->sr_err != LDAP_SUCCESS ) {
93 LDAP_LOG( OPERATION, ERR,
94 "do_add: conn %d invalid dn (%s)\n", op->o_connid, dn.bv_val, 0 );
96 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
98 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
102 ber_dupbv( &e->e_name, &op->o_req_dn );
103 ber_dupbv( &e->e_nname, &op->o_req_ndn );
106 LDAP_LOG( OPERATION, ARGS,
107 "do_add: conn %d dn (%s)\n", op->o_connid, e->e_dn, 0 );
109 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
113 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
114 tag = ber_next_element( ber, &len, last ) )
119 tmp.sml_nvalues = NULL;
121 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
123 if ( rtag == LBER_ERROR ) {
125 LDAP_LOG( OPERATION, ERR,
126 "do_add: conn %d decoding error \n", op->o_connid, 0, 0 );
128 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
130 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
135 if ( tmp.sml_values == NULL ) {
137 LDAP_LOG( OPERATION, INFO,
138 "do_add: conn %d no values for type %s\n",
139 op->o_connid, tmp.sml_type.bv_val, 0 );
141 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
142 tmp.sml_type.bv_val, 0, 0 );
144 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
145 "no values for attribute type" );
149 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
150 mod->sml_op = LDAP_MOD_ADD;
151 mod->sml_next = NULL;
152 mod->sml_desc = NULL;
153 mod->sml_type = tmp.sml_type;
154 mod->sml_values = tmp.sml_values;
155 mod->sml_nvalues = NULL;
158 modtail = &mod->sml_next;
161 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
163 LDAP_LOG( OPERATION, ERR,
164 "do_add: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
166 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
168 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
173 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
175 LDAP_LOG( OPERATION, INFO,
176 "do_add: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
178 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
183 if ( modlist == NULL ) {
184 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
185 "no attributes provided" );
189 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
190 op->o_connid, op->o_opid, e->e_name.bv_val, 0, 0 );
192 if( e->e_nname.bv_len == 0 ) {
193 /* protocolError may be a more appropriate error */
194 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
195 "root DSE already exists" );
198 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
199 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
200 "subschema subentry already exists" );
204 manageDSAit = get_manageDSAit( op );
207 * We could be serving multiple database backends. Select the
208 * appropriate one, or send a referral to our "referral server"
209 * if we don't hold it.
211 op->o_bd = select_backend( &e->e_nname, manageDSAit, 0 );
212 if ( op->o_bd == NULL ) {
213 rs->sr_ref = referral_rewrite( default_referral,
214 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
215 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
217 rs->sr_err = LDAP_REFERRAL;
218 send_ldap_result( op, rs );
220 if ( rs->sr_ref != default_referral ) {
221 ber_bvarray_free( rs->sr_ref );
224 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
225 "no global superior knowledge" );
230 /* check restrictions */
231 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
232 send_ldap_result( op, rs );
236 /* check for referrals */
237 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
242 if ( op->o_pb ) init_add_pblock( op, &dn, e, manageDSAit );
243 #endif /* LDAP_SLAPI */
246 * do the add if 1 && (2 || 3)
247 * 1) there is an add function implemented in this backend;
248 * 2) this backend is master for what it holds;
249 * 3) it's a replica and the dn supplied is the updatedn.
251 if ( op->o_bd->be_add ) {
252 /* do the update here */
253 int repl_user = be_isupdate(op->o_bd, &op->o_ndn );
254 #ifndef SLAPD_MULTIMASTER
255 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
257 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ))
260 int update = op->o_bd->be_update_ndn.bv_len;
261 char textbuf[SLAP_TEXT_BUFLEN];
262 size_t textlen = sizeof textbuf;
263 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
265 rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
266 textbuf, textlen, NULL );
268 if( rs->sr_err != LDAP_SUCCESS ) {
269 send_ldap_result( op, rs );
274 for( modtail = &modlist;
276 modtail = &(*modtail)->sml_next )
278 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
279 assert( (*modtail)->sml_desc != NULL );
281 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
282 &rs->sr_text, textbuf, textlen );
283 if( rs->sr_err != LDAP_SUCCESS ) {
284 send_ldap_result( op, rs );
289 rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
290 &rs->sr_text, textbuf, textlen );
291 if( rs->sr_err != LDAP_SUCCESS ) {
292 send_ldap_result( op, rs );
298 * Call the preoperation plugin here, because the entry
299 * will actually contain something.
302 rs->sr_err = call_add_preop_plugins( op );
303 if ( rs->sr_err != LDAP_SUCCESS ) {
304 /* plugin will have sent result */
308 #endif /* LDAP_SLAPI */
311 #ifdef SLAPD_MULTIMASTER
315 cb.sc_next = op->o_callback;
316 op->o_callback = &cb;
318 if ( (op->o_bd->be_add)( op, rs ) == 0 ) {
319 be_entry_release_w( op, e );
323 #ifndef SLAPD_MULTIMASTER
325 BerVarray defref = NULL;
328 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
329 * on replicas (for now, it involves the minimum code intrusion).
332 rs->sr_err = call_add_preop_plugins( op );
333 if ( rs->sr_err != LDAP_SUCCESS ) {
334 /* plugin will have sent result */
338 #endif /* LDAP_SLAPI */
340 defref = op->o_bd->be_update_refs
341 ? op->o_bd->be_update_refs : default_referral;
343 if ( defref != NULL ) {
344 rs->sr_ref = referral_rewrite( defref,
345 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
346 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
347 rs->sr_err = LDAP_REFERRAL;
348 if (!rs->sr_ref) rs->sr_ref = default_referral;
349 send_ldap_result( op, rs );
351 if ( rs->sr_ref != default_referral ) {
352 ber_bvarray_free( rs->sr_ref );
355 send_ldap_error( op, rs,
356 LDAP_UNWILLING_TO_PERFORM,
357 "shadow context; no update referral" );
359 #endif /* SLAPD_MULTIMASTER */
364 rs->sr_err = call_add_preop_plugins( op );
365 if ( rs->sr_err != LDAP_SUCCESS ) {
366 /* plugin will have sent result */
372 LDAP_LOG( OPERATION, INFO,
373 "do_add: conn %d no backend support\n", op->o_connid, 0, 0 );
375 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
377 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
378 "operation not supported within namingContext" );
382 if ( op->o_pb ) call_add_postop_plugins( op );
383 #endif /* LDAP_SLAPI */
387 slap_graduate_commit_csn( op );
389 if( modlist != NULL ) {
390 slap_mods_free( modlist );
395 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
396 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
408 char *textbuf, size_t textlen )
410 Attribute **tail = &(*e)->e_attrs;
411 assert( *tail == NULL );
415 for( ; mods != NULL; mods = mods->sml_next ) {
419 assert( mods->sml_op == LDAP_MOD_ADD );
421 assert( mods->sml_desc != NULL );
423 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
426 #define SLURPD_FRIENDLY
427 #ifdef SLURPD_FRIENDLY
431 snprintf( textbuf, textlen,
432 "attribute '%s' provided more than once",
433 mods->sml_desc->ad_cname.bv_val );
434 return LDAP_TYPE_OR_VALUE_EXISTS;
437 for( i=0; attr->a_vals[i].bv_val; i++ ) {
440 for( j=0; mods->sml_values[j].bv_val; j++ ) {
445 attr->a_vals = ch_realloc( attr->a_vals,
446 sizeof( struct berval ) * (i+j) );
448 /* should check for duplicates */
450 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
451 sizeof( struct berval ) * j );
453 /* trim the mods array */
454 ch_free( mods->sml_values );
455 mods->sml_values = NULL;
457 if( mods->sml_nvalues ) {
458 attr->a_nvals = ch_realloc( attr->a_nvals,
459 sizeof( struct berval ) * (i+j) );
461 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
462 sizeof( struct berval ) * j );
464 /* trim the mods array */
465 ch_free( mods->sml_nvalues );
466 mods->sml_nvalues = NULL;
469 attr->a_nvals = attr->a_vals;
474 snprintf( textbuf, textlen,
475 "attribute '%s' provided more than once",
476 mods->sml_desc->ad_cname.bv_val );
477 return LDAP_TYPE_OR_VALUE_EXISTS;
481 if( mods->sml_values[1].bv_val != NULL ) {
482 /* check for duplicates */
484 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
486 /* check if the values we're adding already exist */
487 if( mr == NULL || !mr->smr_match ) {
488 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
489 /* test asserted values against themselves */
490 for( j = 0; j < i; j++ ) {
491 if ( bvmatch( &mods->sml_bvalues[i],
492 &mods->sml_bvalues[j] ) ) {
493 /* value exists already */
494 snprintf( textbuf, textlen,
495 "%s: value #%d provided more than once",
496 mods->sml_desc->ad_cname.bv_val, j );
497 return LDAP_TYPE_OR_VALUE_EXISTS;
503 int rc = LDAP_SUCCESS;
506 for ( i = 0; mods->sml_values[i].bv_val != NULL; i++ ) {
507 /* test asserted values against themselves */
508 for( j = 0; j < i; j++ ) {
509 rc = value_match( &match, mods->sml_desc, mr,
510 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
511 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
512 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
514 ? &mods->sml_nvalues[i]
515 : &mods->sml_values[i],
517 ? &mods->sml_nvalues[j]
518 : &mods->sml_values[j],
520 if ( rc == LDAP_SUCCESS && match == 0 ) {
521 /* value exists already */
522 snprintf( textbuf, textlen,
523 "%s: value #%d provided more than once",
524 mods->sml_desc->ad_cname.bv_val, j );
525 return LDAP_TYPE_OR_VALUE_EXISTS;
529 if ( rc != LDAP_SUCCESS ) {
535 attr = ch_calloc( 1, sizeof(Attribute) );
537 /* move ad to attr structure */
538 attr->a_desc = mods->sml_desc;
540 mods->sml_desc = NULL;
542 /* move values to attr structure */
543 /* should check for duplicates */
546 for ( i = 0; mods->sml_values[i].bv_val; i++ ) ;
547 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
548 for ( i = 0; mods->sml_values[i].bv_val; i++ )
549 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
550 attr->a_vals[i].bv_len = 0;
551 attr->a_vals[i].bv_val = NULL;
553 attr->a_vals = mods->sml_values;
554 mods->sml_values = NULL;
557 if ( mods->sml_nvalues ) {
560 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) ;
561 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
562 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ )
563 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
564 attr->a_nvals[i].bv_len = 0;
565 attr->a_nvals[i].bv_val = NULL;
567 attr->a_nvals = mods->sml_nvalues;
568 mods->sml_nvalues = NULL;
571 attr->a_nvals = attr->a_vals;
575 tail = &attr->a_next;
584 Modifications **mods,
586 char *textbuf, size_t textlen )
588 Modifications *modhead = NULL;
590 Modifications **modtail = &modhead;
592 AttributeDescription *a_new_desc;
597 while ( a_new != NULL ) {
598 a_new_desc = a_new->a_desc;
599 mod = (Modifications *) malloc( sizeof( Modifications ));
601 mod->sml_op = LDAP_MOD_REPLACE;
603 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 /* see slap_mods_check() comments...
611 * if a_vals == a_nvals, there is no normalizer.
612 * in this case, mod->sml_nvalues must be left NULL.
614 if ( a_new->a_vals != a_new->a_nvals ) {
615 mod->sml_nvalues = (struct berval*) malloc(
616 (count+1) * sizeof( struct berval) );
618 mod->sml_nvalues = NULL;
621 for ( i = 0; i < count; i++ ) {
622 ber_dupbv(mod->sml_bvalues+i, a_new->a_vals+i);
623 if ( mod->sml_nvalues ) {
624 ber_dupbv( mod->sml_nvalues+i, a_new->a_vals+i );
628 mod->sml_bvalues[count].bv_val = 0;
629 mod->sml_bvalues[count].bv_len = 0;
631 if ( mod->sml_nvalues ) {
632 mod->sml_nvalues[count].bv_val = 0;
633 mod->sml_nvalues[count].bv_len = 0;
636 mod->sml_desc = a_new_desc;
639 modtail = &mod->sml_next;
640 a_new = a_new->a_next;
649 static void init_add_pblock( Operation *op,
650 struct berval *dn, Entry *e, int manageDSAit )
652 slapi_int_pblock_set_operation( op->o_pb, op );
653 slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
654 slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
655 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
658 static int call_add_preop_plugins( Operation *op )
662 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
665 * A preoperation plugin failure will abort the
669 LDAP_LOG( OPERATION, INFO,
670 "do_add: add preoperation plugin failed\n",
673 Debug(LDAP_DEBUG_TRACE,
674 "do_add: add preoperation plugin failed.\n",
678 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
679 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
690 static void call_add_postop_plugins( Operation *op )
694 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
697 LDAP_LOG( OPERATION, INFO,
698 "do_add: add postoperation plugin failed\n",
701 Debug(LDAP_DEBUG_TRACE,
702 "do_add: add postoperation plugin failed\n",
707 #endif /* LDAP_SLAPI */