2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>
36 #include "slapi/slapi.h"
38 static void init_add_pblock( Operation *op, struct berval *dn, Entry *e,
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 = BER_BVNULL;
53 Modifications *modlist = NULL;
54 Modifications **modtail = &modlist;
56 char textbuf[ SLAP_TEXT_BUFLEN ];
57 size_t textlen = sizeof( textbuf );
60 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
62 * Parse the add request. It looks like this:
64 * AddRequest := [APPLICATION 14] SEQUENCE {
65 * name DistinguishedName,
66 * attrs SEQUENCE OF SEQUENCE {
68 * values SET OF AttributeValue
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" );
77 return SLAPD_DISCONNECT;
80 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
82 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
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" );
91 ber_dupbv( &e->e_name, &op->o_req_dn );
92 ber_dupbv( &e->e_nname, &op->o_req_ndn );
94 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
97 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
98 tag = ber_next_element( ber, &len, last ) )
103 tmp.sml_nvalues = NULL;
105 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
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" );
110 rs->sr_err = SLAPD_DISCONNECT;
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 );
117 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
118 "no values for attribute type" );
122 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
123 mod->sml_op = LDAP_MOD_ADD;
124 mod->sml_next = NULL;
125 mod->sml_desc = NULL;
126 mod->sml_type = tmp.sml_type;
127 mod->sml_values = tmp.sml_values;
128 mod->sml_nvalues = NULL;
131 modtail = &mod->sml_next;
134 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
135 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
136 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
137 rs->sr_err = SLAPD_DISCONNECT;
141 if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
142 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
146 if ( modlist == NULL ) {
147 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
148 "no attributes provided" );
152 Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
153 op->o_log_prefix, e->e_name.bv_val, 0, 0, 0 );
155 if ( dn_match( &e->e_nname, &slap_empty_bv ) ) {
156 /* protocolError may be a more appropriate error */
157 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
158 "root DSE already exists" );
161 } else if ( dn_match( &e->e_nname, &frontendDB->be_schemandn ) ) {
162 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
163 "subschema subentry already exists" );
167 rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
168 textbuf, textlen, NULL );
170 if ( rs->sr_err != LDAP_SUCCESS ) {
171 send_ldap_result( op, rs );
175 /* temporary; remove if not invoking backend function */
177 op->ora_modlist = modlist;
179 op->o_bd = frontendDB;
180 rc = frontendDB->be_add( op, rs );
186 slap_graduate_commit_csn( op );
188 if ( modlist != NULL ) {
189 slap_mods_free( modlist );
194 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
195 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
201 fe_op_add( Operation *op, SlapReply *rs )
204 Entry *e = op->ora_e;
205 Modifications *modlist = op->ora_modlist;
206 Modifications **modtail = &modlist;
209 manageDSAit = get_manageDSAit( op );
212 * We could be serving multiple database backends. Select the
213 * appropriate one, or send a referral to our "referral server"
214 * if we don't hold it.
216 op->o_bd = select_backend( &e->e_nname, manageDSAit, 1 );
217 if ( op->o_bd == NULL ) {
218 rs->sr_ref = referral_rewrite( default_referral,
219 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
220 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
222 rs->sr_err = LDAP_REFERRAL;
223 send_ldap_result( op, rs );
225 if ( rs->sr_ref != default_referral ) {
226 ber_bvarray_free( rs->sr_ref );
229 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
230 "no global superior knowledge" );
235 /* check restrictions */
236 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
237 send_ldap_result( op, rs );
241 /* check for referrals */
242 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
247 if ( op->o_pb ) init_add_pblock( op, &op->o_req_dn, e, manageDSAit );
248 #endif /* LDAP_SLAPI */
251 * do the add if 1 && (2 || 3)
252 * 1) there is an add function implemented in this backend;
253 * 2) this backend is master for what it holds;
254 * 3) it's a replica and the dn supplied is the updatedn.
256 if ( op->o_bd->be_add ) {
257 /* do the update here */
258 int repl_user = be_isupdate( op );
259 #ifndef SLAPD_MULTIMASTER
260 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
263 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
264 char textbuf[ SLAP_TEXT_BUFLEN ];
265 size_t textlen = sizeof( textbuf );
266 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
269 rs->sr_err = slap_mods_no_update_check( modlist,
273 if ( rs->sr_err != LDAP_SUCCESS ) {
274 send_ldap_result( op, rs );
280 /* go to the last mod */
281 for ( modtail = &modlist;
283 modtail = &(*modtail)->sml_next )
285 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
286 assert( (*modtail)->sml_desc != NULL );
289 rs->sr_err = slap_mods_opattrs( op, modlist,
290 modtail, &rs->sr_text,
291 textbuf, textlen, 1 );
292 if ( rs->sr_err != LDAP_SUCCESS ) {
293 send_ldap_result( op, rs );
298 rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
299 &rs->sr_text, textbuf, textlen );
300 if ( rs->sr_err != LDAP_SUCCESS ) {
301 send_ldap_result( op, rs );
307 * Call the preoperation plugin here, because the entry
308 * will actually contain something.
311 rs->sr_err = call_add_preop_plugins( op );
312 if ( rs->sr_err != LDAP_SUCCESS ) {
313 /* plugin will have sent result */
317 #endif /* LDAP_SLAPI */
320 #ifdef SLAPD_MULTIMASTER
324 cb.sc_next = op->o_callback;
325 op->o_callback = &cb;
327 rc = (op->o_bd->be_add)( op, rs );
329 /* FIXME: be_entry_release_w() should be
330 * called by do_add(), so that global
331 * overlays on the way back can
332 * at least read the entry */
333 be_entry_release_w( op, e );
338 #ifndef SLAPD_MULTIMASTER
340 BerVarray defref = NULL;
343 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
344 * on replicas (for now, it involves the minimum code intrusion).
347 rs->sr_err = call_add_preop_plugins( op );
348 if ( rs->sr_err != LDAP_SUCCESS ) {
349 /* plugin will have sent result */
353 #endif /* LDAP_SLAPI */
355 defref = op->o_bd->be_update_refs
356 ? op->o_bd->be_update_refs : default_referral;
358 if ( defref != NULL ) {
359 rs->sr_ref = referral_rewrite( defref,
360 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
361 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
362 rs->sr_err = LDAP_REFERRAL;
363 if (!rs->sr_ref) rs->sr_ref = default_referral;
364 send_ldap_result( op, rs );
366 if ( rs->sr_ref != default_referral ) {
367 ber_bvarray_free( rs->sr_ref );
370 send_ldap_error( op, rs,
371 LDAP_UNWILLING_TO_PERFORM,
372 "shadow context; no update referral" );
374 #endif /* SLAPD_MULTIMASTER */
379 rs->sr_err = call_add_preop_plugins( op );
380 if ( rs->sr_err != LDAP_SUCCESS ) {
381 /* plugin will have sent result */
386 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
387 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
388 "operation not supported within namingContext" );
392 if ( op->o_pb ) call_add_postop_plugins( op );
393 #endif /* LDAP_SLAPI */
406 char *textbuf, size_t textlen )
408 Attribute **tail = &(*e)->e_attrs;
409 assert( *tail == NULL );
413 for( ; mods != NULL; mods = mods->sml_next ) {
417 assert( mods->sml_op == LDAP_MOD_ADD );
419 assert( mods->sml_desc != NULL );
421 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
424 #define SLURPD_FRIENDLY
425 #ifdef SLURPD_FRIENDLY
429 snprintf( textbuf, textlen,
430 "attribute '%s' provided more than once",
431 mods->sml_desc->ad_cname.bv_val );
432 return LDAP_TYPE_OR_VALUE_EXISTS;
435 for( i=0; attr->a_vals[i].bv_val; i++ ) {
438 for( j=0; mods->sml_values[j].bv_val; j++ ) {
443 attr->a_vals = ch_realloc( attr->a_vals,
444 sizeof( struct berval ) * (i+j) );
446 /* should check for duplicates */
449 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
450 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
452 BER_BVZERO( &attr->a_vals[i+j] );
454 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
455 sizeof( struct berval ) * j );
456 ch_free( mods->sml_values );
457 mods->sml_values = NULL;
460 if( mods->sml_nvalues ) {
461 attr->a_nvals = ch_realloc( attr->a_nvals,
462 sizeof( struct berval ) * (i+j) );
464 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
465 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
467 BER_BVZERO( &attr->a_nvals[i+j] );
469 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
470 sizeof( struct berval ) * j );
471 ch_free( mods->sml_nvalues );
472 mods->sml_nvalues = NULL;
475 attr->a_nvals = attr->a_vals;
480 snprintf( textbuf, textlen,
481 "attribute '%s' provided more than once",
482 mods->sml_desc->ad_cname.bv_val );
483 return LDAP_TYPE_OR_VALUE_EXISTS;
487 if( mods->sml_values[1].bv_val != NULL ) {
488 /* check for duplicates */
490 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
492 /* check if the values we're adding already exist */
493 if( mr == NULL || !mr->smr_match ) {
494 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
495 /* test asserted values against themselves */
496 for( j = 0; j < i; j++ ) {
497 if ( bvmatch( &mods->sml_values[i],
498 &mods->sml_values[j] ) )
500 /* value exists already */
501 snprintf( textbuf, textlen,
502 "%s: value #%d provided more than once",
503 mods->sml_desc->ad_cname.bv_val, j );
504 return LDAP_TYPE_OR_VALUE_EXISTS;
513 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
514 /* test asserted values against themselves */
515 for( j = 0; j < i; j++ ) {
516 rc = value_match( &match, mods->sml_desc, mr,
518 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
519 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
520 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
522 ? &mods->sml_nvalues[i]
523 : &mods->sml_values[i],
525 ? &mods->sml_nvalues[j]
526 : &mods->sml_values[j],
529 if ( rc == LDAP_SUCCESS && match == 0 ) {
530 /* value exists already */
531 snprintf( textbuf, textlen,
532 "%s: value #%d provided more than once",
533 mods->sml_desc->ad_cname.bv_val, j );
534 return LDAP_TYPE_OR_VALUE_EXISTS;
536 } else if ( rc != LDAP_SUCCESS ) {
544 attr = ch_calloc( 1, sizeof(Attribute) );
546 /* move ad to attr structure */
547 attr->a_desc = mods->sml_desc;
548 if ( !dup ) mods->sml_desc = NULL;
550 /* move values to attr structure */
551 /* should check for duplicates */
554 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
555 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
556 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
557 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
559 BER_BVZERO( &attr->a_vals[i] );
561 attr->a_vals = mods->sml_values;
562 mods->sml_values = NULL;
565 if ( mods->sml_nvalues ) {
568 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
569 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
570 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
571 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
573 BER_BVZERO( &attr->a_nvals[i] );
575 attr->a_nvals = mods->sml_nvalues;
576 mods->sml_nvalues = NULL;
579 attr->a_nvals = attr->a_vals;
583 tail = &attr->a_next;
594 Modifications **mods,
596 char *textbuf, size_t textlen )
598 Modifications *modhead = NULL;
600 Modifications **modtail = &modhead;
602 AttributeDescription *a_new_desc;
607 while ( a_new != NULL ) {
608 a_new_desc = a_new->a_desc;
609 mod = (Modifications *) malloc( sizeof( Modifications ));
611 mod->sml_op = LDAP_MOD_REPLACE;
613 mod->sml_type = a_new_desc->ad_cname;
615 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
617 mod->sml_values = (struct berval*) malloc(
618 (count+1) * sizeof( struct berval) );
620 /* see slap_mods_check() comments...
621 * if a_vals == a_nvals, there is no normalizer.
622 * in this case, mod->sml_nvalues must be left NULL.
624 if ( a_new->a_vals != a_new->a_nvals ) {
625 mod->sml_nvalues = (struct berval*) malloc(
626 (count+1) * sizeof( struct berval) );
628 mod->sml_nvalues = NULL;
631 for ( i = 0; i < count; i++ ) {
632 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
633 if ( mod->sml_nvalues ) {
634 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
638 mod->sml_values[count].bv_val = NULL;
639 mod->sml_values[count].bv_len = 0;
641 if ( mod->sml_nvalues ) {
642 mod->sml_nvalues[count].bv_val = NULL;
643 mod->sml_nvalues[count].bv_len = 0;
646 mod->sml_desc = a_new_desc;
649 modtail = &mod->sml_next;
650 a_new = a_new->a_next;
659 static void init_add_pblock( Operation *op,
660 struct berval *dn, Entry *e, int manageDSAit )
662 slapi_int_pblock_set_operation( op->o_pb, op );
663 slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
664 slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
665 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
668 static int call_add_preop_plugins( Operation *op )
672 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
675 * A preoperation plugin failure will abort the
678 Debug(LDAP_DEBUG_TRACE,
679 "do_add: add preoperation plugin failed.\n",
682 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
683 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
694 static void call_add_postop_plugins( Operation *op )
698 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
700 Debug(LDAP_DEBUG_TRACE,
701 "do_add: add postoperation plugin failed\n",
705 #endif /* LDAP_SLAPI */