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 do_add( Connection *conn, Operation *op )
32 BerElement *ber = op->o_ber;
34 struct berval dn = { 0, NULL };
39 Modifications *modlist = NULL;
40 Modifications **modtail = &modlist;
45 int rc = LDAP_SUCCESS;
48 Slapi_PBlock *pb = op->o_pb;
51 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_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", conn->c_connid,0,0 );
73 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
75 send_ldap_disconnect( conn, op,
76 LDAP_PROTOCOL_ERROR, "decoding error" );
80 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
82 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
84 if( rc != LDAP_SUCCESS ) {
86 LDAP_LOG( OPERATION, ERR,
87 "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
89 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
91 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
92 "invalid DN", NULL, NULL );
97 LDAP_LOG( OPERATION, ARGS,
98 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn, 0 );
100 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
104 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
105 tag = ber_next_element( ber, &len, last ) )
110 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
112 if ( rtag == LBER_ERROR ) {
114 LDAP_LOG( OPERATION, ERR,
115 "do_add: conn %d decoding error \n", conn->c_connid, 0, 0 );
117 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
119 send_ldap_disconnect( conn, op,
120 LDAP_PROTOCOL_ERROR, "decoding error" );
125 if ( tmp.sml_bvalues == NULL ) {
127 LDAP_LOG( OPERATION, INFO,
128 "do_add: conn %d no values for type %s\n",
129 conn->c_connid, tmp.sml_type.bv_val, 0 );
131 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
132 tmp.sml_type.bv_val, 0, 0 );
134 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
135 NULL, "no values for attribute type", NULL, NULL );
138 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
140 mod->sml_op = LDAP_MOD_ADD;
141 mod->sml_next = NULL;
142 mod->sml_desc = NULL;
143 mod->sml_type = tmp.sml_type;
144 mod->sml_bvalues = tmp.sml_bvalues;
147 modtail = &mod->sml_next;
150 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
152 LDAP_LOG( OPERATION, ERR,
153 "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
155 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
157 send_ldap_disconnect( conn, op,
158 LDAP_PROTOCOL_ERROR, "decoding error" );
163 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
165 LDAP_LOG( OPERATION, INFO,
166 "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
168 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
173 if ( modlist == NULL ) {
174 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
175 NULL, "no attributes provided", NULL, NULL );
179 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
180 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
182 if( e->e_nname.bv_len == 0 ) {
183 /* protocolError may be a more appropriate error */
184 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
185 NULL, "root DSE already exists",
189 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
190 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
191 NULL, "subschema subentry already exists",
197 * Get attribute type(s) and attribute value(s) of our rdn,
199 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&text,
200 LDAP_DN_FORMAT_LDAP ) )
202 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
203 NULL, "unknown type(s) used in RDN",
208 /* Check for RDN attrs in entry */
209 for ( a_cnt = 0; rdn[ 0 ][ a_cnt ]; a_cnt++ ) {
210 AttributeDescription *desc = NULL;
215 rc = slap_bv2ad( &rdn[ 0 ][ a_cnt ]->la_attr,
218 if ( rc != LDAP_SUCCESS ) {
219 send_ldap_result( conn, op, rc,
220 NULL, text, NULL, NULL );
224 for (mod = modlist; mod; mod = mod->sml_next) {
225 AttributeDescription *mod_desc = NULL;
227 rc = slap_bv2ad( &mod->sml_type,
229 if ( rc != LDAP_SUCCESS ) {
230 send_ldap_result( conn, op, rc,
231 NULL, text, NULL, NULL );
235 if (mod_desc == desc) {
242 /* for now, bail out; we might end up
243 * adding the missing value, as iPlanet
245 send_ldap_result( conn, op,
246 rc = LDAP_CONSTRAINT_VIOLATION,
248 "attribute in RDN not listed in entry",
252 #else /* ! BAILOUT */
255 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
257 mod->sml_op = LDAP_MOD_ADD;
258 mod->sml_next = NULL;
259 mod->sml_desc = NULL;
261 ber_dupbv( &mod->sml_type,
262 &rdn[ 0 ][ a_cnt ]->la_attr );
264 mod->sml_bvalues = NULL;
265 ber_dupbv( &bv, &rdn[ 0 ][ a_cnt ]->la_value );
266 ber_bvarray_add( &mod->sml_bvalues, &bv );
269 modtail = &mod->sml_next;
271 #endif /* ! BAILOUT */
274 mr = desc->ad_type->sat_equality;
275 if (mr == NULL || !mr->smr_match ) {
280 for (i = 0; mod->sml_bvalues[ i ].bv_val; i++) {
283 rc = value_match(&match, desc, mr,
284 SLAP_MR_VALUE_SYNTAX_MATCH,
285 &mod->sml_bvalues[ i ],
286 &rdn[ 0 ][ a_cnt ]->la_value, &text);
288 if ( rc != LDAP_SUCCESS ) {
289 send_ldap_result( conn, op, rc,
290 NULL, text, NULL, NULL);
300 if (mod->sml_bvalues[ i ].bv_val == NULL) {
302 send_ldap_result( conn, op,
303 rc = LDAP_CONSTRAINT_VIOLATION,
305 "value in RDN not listed in entry",
308 #else /* ! BAILOUT */
311 ber_dupbv( &bv, &rdn[ 0 ][ a_cnt ]->la_value );
312 ber_bvarray_add( &mod->sml_bvalues, &bv );
314 #endif /* ! BAILOUT */
318 manageDSAit = get_manageDSAit( op );
321 * We could be serving multiple database backends. Select the
322 * appropriate one, or send a referral to our "referral server"
323 * if we don't hold it.
325 be = select_backend( &e->e_nname, manageDSAit, 0 );
327 BerVarray ref = referral_rewrite( default_referral,
328 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
330 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
331 NULL, NULL, ref ? ref : default_referral, NULL );
333 if ( ref ) ber_bvarray_free( ref );
337 /* check restrictions */
338 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
339 if( rc != LDAP_SUCCESS ) {
340 send_ldap_result( conn, op, rc,
341 NULL, text, NULL, NULL );
345 /* check for referrals */
346 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
347 if ( rc != LDAP_SUCCESS ) {
351 #if defined( LDAP_SLAPI )
352 slapi_x_backend_set_pb( pb, be );
353 slapi_x_connection_set_pb( pb, conn );
354 slapi_x_operation_set_pb( pb, op );
355 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
356 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn.bv_val );
357 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
359 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
362 * A preoperation plugin failure will abort the
366 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
369 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
371 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
376 #endif /* defined( LDAP_SLAPI ) */
379 * do the add if 1 && (2 || 3)
380 * 1) there is an add function implemented in this backend;
381 * 2) this backend is master for what it holds;
382 * 3) it's a replica and the dn supplied is the updatedn.
385 /* do the update here */
386 int repl_user = be_isupdate(be, &op->o_ndn );
387 #ifndef SLAPD_MULTIMASTER
388 if ( !be->be_update_ndn.bv_len || repl_user )
391 int update = be->be_update_ndn.bv_len;
392 char textbuf[SLAP_TEXT_BUFLEN];
393 size_t textlen = sizeof textbuf;
395 rc = slap_mods_check( modlist, update, &text,
398 if( rc != LDAP_SUCCESS ) {
399 send_ldap_result( conn, op, rc,
400 NULL, text, NULL, NULL );
405 for( modtail = &modlist;
407 modtail = &(*modtail)->sml_next )
409 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
410 assert( (*modtail)->sml_desc != NULL );
412 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
414 if( rc != LDAP_SUCCESS ) {
415 send_ldap_result( conn, op, rc,
416 NULL, text, NULL, NULL );
421 rc = slap_mods2entry( modlist, &e, repl_user, &text,
423 if( rc != LDAP_SUCCESS ) {
424 send_ldap_result( conn, op, rc,
425 NULL, text, NULL, NULL );
429 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
430 #ifdef SLAPD_MULTIMASTER
434 replog( be, op, &e->e_name, &e->e_nname, e );
436 be_entry_release_w( be, conn, op, e );
440 #ifndef SLAPD_MULTIMASTER
442 BerVarray defref = be->be_update_refs
443 ? be->be_update_refs : default_referral;
444 BerVarray ref = referral_rewrite( defref,
445 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
447 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
448 ref ? ref : defref, NULL );
450 if ( ref ) ber_bvarray_free( ref );
455 LDAP_LOG( OPERATION, INFO,
456 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
458 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
460 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
461 NULL, "operation not supported within namingContext", NULL, NULL );
464 #if defined( LDAP_SLAPI )
466 * Postoperation errors are silently ignored; the work has
469 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb ) != 0) {
471 LDAP_LOG( OPERATION, INFO, "do_add: Add postoperation plugins failed\n",
474 Debug(LDAP_DEBUG_TRACE, "do_add: Add postoperation plugins failed.\n",
478 #endif /* defined( LDAP_SLAPI ) */
481 if( modlist != NULL ) {
482 slap_mods_free( modlist );
497 char *textbuf, size_t textlen )
499 Attribute **tail = &(*e)->e_attrs;
500 assert( *tail == NULL );
504 for( ; mods != NULL; mods = mods->sml_next ) {
507 assert( mods->sml_op == LDAP_MOD_ADD );
508 assert( mods->sml_desc != NULL );
510 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
513 #define SLURPD_FRIENDLY
514 #ifdef SLURPD_FRIENDLY
518 snprintf( textbuf, textlen,
519 "attribute '%s' provided more than once",
520 mods->sml_desc->ad_cname.bv_val );
521 return LDAP_TYPE_OR_VALUE_EXISTS;
524 for( i=0; attr->a_vals[i].bv_val; i++ ) {
527 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
532 attr->a_vals = ch_realloc( attr->a_vals,
533 sizeof( struct berval ) * (i+j) );
535 /* should check for duplicates */
537 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
538 sizeof( struct berval ) * j );
540 /* trim the mods array */
541 ch_free( mods->sml_bvalues );
542 mods->sml_bvalues = NULL;
546 snprintf( textbuf, textlen,
547 "attribute '%s' provided more than once",
548 mods->sml_desc->ad_cname.bv_val );
549 return LDAP_TYPE_OR_VALUE_EXISTS;
553 if( mods->sml_bvalues[1].bv_val != NULL ) {
554 /* check for duplicates */
556 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
558 /* check if the values we're adding already exist */
559 if( mr == NULL || !mr->smr_match ) {
560 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
561 /* test asserted values against themselves */
562 for( j = 0; j < i; j++ ) {
563 if ( bvmatch( &mods->sml_bvalues[i],
564 &mods->sml_bvalues[j] ) ) {
565 /* value exists already */
566 snprintf( textbuf, textlen,
567 "%s: value #%d provided more than once",
568 mods->sml_desc->ad_cname.bv_val, j );
569 return LDAP_TYPE_OR_VALUE_EXISTS;
576 const char *text = NULL;
577 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
579 rc = modify_check_duplicates( mods->sml_desc, mr,
580 NULL, mods->sml_bvalues, 0,
581 &text, textbuf, sizeof( textbuf ) );
583 if ( rc != LDAP_SUCCESS ) {
589 attr = ch_calloc( 1, sizeof(Attribute) );
591 /* move ad to attr structure */
592 attr->a_desc = mods->sml_desc;
593 mods->sml_desc = NULL;
595 /* move values to attr structure */
596 /* should check for duplicates */
597 attr->a_vals = mods->sml_bvalues;
598 mods->sml_bvalues = NULL;
601 tail = &attr->a_next;