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, &e->e_name, &e->e_nname );
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" );
95 LDAP_LOG( OPERATION, ARGS,
96 "do_add: conn %d dn (%s)\n", op->o_connid, e->e_dn, 0 );
98 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
101 op->o_req_dn = e->e_name;
102 op->o_req_ndn = e->e_nname;
105 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
106 tag = ber_next_element( ber, &len, last ) )
112 tmp.sml_nvalues = NULL;
115 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
117 if ( rtag == LBER_ERROR ) {
119 LDAP_LOG( OPERATION, ERR,
120 "do_add: conn %d decoding error \n", op->o_connid, 0, 0 );
122 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
124 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
129 if ( tmp.sml_values == NULL ) {
131 LDAP_LOG( OPERATION, INFO,
132 "do_add: conn %d no values for type %s\n",
133 op->o_connid, tmp.sml_type.bv_val, 0 );
135 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
136 tmp.sml_type.bv_val, 0, 0 );
138 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "no values for attribute type" );
142 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
143 mod->sml_op = LDAP_MOD_ADD;
144 mod->sml_next = NULL;
145 mod->sml_desc = NULL;
146 mod->sml_type = tmp.sml_type;
147 mod->sml_values = tmp.sml_values;
149 mod->sml_nvalues = NULL;
153 modtail = &mod->sml_next;
156 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
158 LDAP_LOG( OPERATION, ERR,
159 "do_add: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
161 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
163 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
168 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
170 LDAP_LOG( OPERATION, INFO,
171 "do_add: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
173 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
178 if ( modlist == NULL ) {
179 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "no attributes provided" );
183 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
184 op->o_connid, op->o_opid, e->e_name.bv_val, 0, 0 );
186 if( e->e_nname.bv_len == 0 ) {
187 /* protocolError may be a more appropriate error */
188 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS, "root DSE already exists" );
191 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
192 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS, "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 );
208 rs->sr_err = LDAP_REFERRAL;
209 if (!rs->sr_ref) rs->sr_ref = default_referral;
210 send_ldap_result( op, rs );
212 if ( rs->sr_ref != default_referral ) ber_bvarray_free( rs->sr_ref );
216 /* check restrictions */
217 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
218 send_ldap_result( op, rs );
222 /* check for referrals */
223 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
228 initAddPlugin( op, &dn, e, manageDSAit );
229 #endif /* LDAP_SLAPI */
232 * do the add if 1 && (2 || 3)
233 * 1) there is an add function implemented in this backend;
234 * 2) this backend is master for what it holds;
235 * 3) it's a replica and the dn supplied is the updatedn.
237 if ( op->o_bd->be_add ) {
238 /* do the update here */
239 int repl_user = be_isupdate(op->o_bd, &op->o_ndn );
240 #ifndef SLAPD_MULTIMASTER
241 if ( !op->o_bd->be_update_ndn.bv_len || repl_user )
244 int update = op->o_bd->be_update_ndn.bv_len;
245 char textbuf[SLAP_TEXT_BUFLEN];
246 size_t textlen = sizeof textbuf;
248 rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
251 if( rs->sr_err != LDAP_SUCCESS ) {
252 send_ldap_result( op, rs );
257 for( modtail = &modlist;
259 modtail = &(*modtail)->sml_next )
261 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
262 assert( (*modtail)->sml_desc != NULL );
264 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
265 &rs->sr_text, textbuf, textlen );
266 if( rs->sr_err != LDAP_SUCCESS ) {
267 send_ldap_result( op, rs );
272 rs->sr_err = slap_mods2entry( modlist, &e, repl_user, &rs->sr_text,
274 if( rs->sr_err != LDAP_SUCCESS ) {
275 send_ldap_result( op, rs );
281 * Call the preoperation plugin here, because the entry
282 * will actually contain something.
284 rs->sr_err = doPreAddPluginFNs( op );
285 if ( rs->sr_err != LDAP_SUCCESS ) {
286 /* plugin will have sent result */
289 #endif /* LDAP_SLAPI */
292 if ( (op->o_bd->be_add)( op, rs ) == 0 ) {
293 #ifdef SLAPD_MULTIMASTER
299 be_entry_release_w( op, e );
303 #ifndef SLAPD_MULTIMASTER
309 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
310 * on replicas (for now, it involves the minimum code intrusion).
312 rs->sr_err = doPreAddPluginFNs( op );
313 if ( rs->sr_err != LDAP_SUCCESS ) {
314 /* plugin will have sent result */
317 #endif /* LDAP_SLAPI */
319 defref = op->o_bd->be_update_refs
320 ? op->o_bd->be_update_refs : default_referral;
321 rs->sr_ref = referral_rewrite( defref,
322 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
324 rs->sr_err = LDAP_REFERRAL;
325 if (!rs->sr_ref) rs->sr_ref = default_referral;
326 send_ldap_result( op, rs );
328 if ( rs->sr_ref != default_referral ) ber_bvarray_free( rs->sr_ref );
329 #endif /* SLAPD_MULTIMASTER */
333 rs->sr_err = doPreAddPluginFNs( op );
334 if ( rs->sr_err != LDAP_SUCCESS ) {
335 /* plugin will have sent result */
340 LDAP_LOG( OPERATION, INFO,
341 "do_add: conn %d no backend support\n", op->o_connid, 0, 0 );
343 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
345 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
346 "operation not supported within namingContext" );
350 doPostAddPluginFNs( op );
351 #endif /* LDAP_SLAPI */
354 if( modlist != NULL ) {
355 slap_mods_free( modlist );
370 char *textbuf, size_t textlen )
372 Attribute **tail = &(*e)->e_attrs;
373 assert( *tail == NULL );
377 for( ; mods != NULL; mods = mods->sml_next ) {
380 assert( mods->sml_op == LDAP_MOD_ADD );
381 assert( mods->sml_desc != NULL );
383 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
386 #define SLURPD_FRIENDLY
387 #ifdef SLURPD_FRIENDLY
391 snprintf( textbuf, textlen,
392 "attribute '%s' provided more than once",
393 mods->sml_desc->ad_cname.bv_val );
394 return LDAP_TYPE_OR_VALUE_EXISTS;
397 for( i=0; attr->a_vals[i].bv_val; i++ ) {
400 for( j=0; mods->sml_values[j].bv_val; j++ ) {
405 attr->a_vals = ch_realloc( attr->a_vals,
406 sizeof( struct berval ) * (i+j) );
408 /* should check for duplicates */
410 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
411 sizeof( struct berval ) * j );
413 /* trim the mods array */
414 ch_free( mods->sml_values );
415 mods->sml_values = NULL;
418 if( mods->sml_nvalues ) {
419 attr->a_nvals = ch_realloc( attr->a_nvals,
420 sizeof( struct berval ) * (i+j) );
422 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
423 sizeof( struct berval ) * j );
425 /* trim the mods array */
426 ch_free( mods->sml_nvalues );
427 mods->sml_nvalues = NULL;
429 attr->a_nvals = attr->a_vals;
435 snprintf( textbuf, textlen,
436 "attribute '%s' provided more than once",
437 mods->sml_desc->ad_cname.bv_val );
438 return LDAP_TYPE_OR_VALUE_EXISTS;
442 if( mods->sml_values[1].bv_val != NULL ) {
443 /* check for duplicates */
445 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
447 /* check if the values we're adding already exist */
448 if( mr == NULL || !mr->smr_match ) {
449 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
450 /* test asserted values against themselves */
451 for( j = 0; j < i; j++ ) {
452 if ( bvmatch( &mods->sml_bvalues[i],
453 &mods->sml_bvalues[j] ) ) {
454 /* value exists already */
455 snprintf( textbuf, textlen,
456 "%s: value #%d provided more than once",
457 mods->sml_desc->ad_cname.bv_val, j );
458 return LDAP_TYPE_OR_VALUE_EXISTS;
468 for ( i = 0; mods->sml_nvalues[i].bv_val != NULL; i++ ) {
469 /* test asserted values against themselves */
470 for( j = 0; j < i; j++ ) {
471 rc = value_match( &match, mods->sml_desc, mr,
472 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
473 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
474 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
475 &mods->sml_nvalues[i], &mods->sml_nvalues[j], text );
476 if ( rc == LDAP_SUCCESS && match == 0 ) {
477 /* value exists already */
478 snprintf( textbuf, textlen,
479 "%s: value #%d provided more than once",
480 mods->sml_desc->ad_cname.bv_val, j );
481 return LDAP_TYPE_OR_VALUE_EXISTS;
486 rc = modify_check_duplicates( mods->sml_desc, mr,
487 NULL, mods->sml_bvalues, 0,
488 text, textbuf, textlen );
490 if ( rc != LDAP_SUCCESS ) {
496 attr = ch_calloc( 1, sizeof(Attribute) );
498 /* move ad to attr structure */
499 attr->a_desc = mods->sml_desc;
500 mods->sml_desc = NULL;
502 /* move values to attr structure */
503 /* should check for duplicates */
504 attr->a_vals = mods->sml_values;
505 mods->sml_values = NULL;
508 if ( mods->sml_nvalues ) {
509 attr->a_nvals = mods->sml_nvalues;
510 mods->sml_nvalues = NULL;
512 attr->a_nvals = attr->a_vals;
517 tail = &attr->a_next;
524 static void initAddPlugin( Operation *op,
525 struct berval *dn, Entry *e, int manageDSAit )
527 slapi_x_pblock_set_operation( op->o_pb, op );
528 slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
529 slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
530 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
533 static int doPreAddPluginFNs( Operation *op )
537 rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
540 * A preoperation plugin failure will abort the
544 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
547 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
549 if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
559 static void doPostAddPluginFNs( Operation *op )
563 rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
566 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
569 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
574 #endif /* LDAP_SLAPI */