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) {
244 send_ldap_result( conn, op,
245 rc = LDAP_NO_SUCH_ATTRIBUTE,
247 "attribute in RDN not listed in entry",
251 #else /* ! BAILOUT */
254 /* add attribute type and value to modlist */
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) {
303 send_ldap_result( conn, op,
304 rc = LDAP_NO_SUCH_ATTRIBUTE,
306 "value in RDN not listed in entry",
310 #else /* ! BAILOUT */
313 /* add attribute type and value to modlist */
314 ber_dupbv( &bv, &rdn[ 0 ][ a_cnt ]->la_value );
315 ber_bvarray_add( &mod->sml_bvalues, &bv );
317 #endif /* ! BAILOUT */
321 manageDSAit = get_manageDSAit( op );
324 * We could be serving multiple database backends. Select the
325 * appropriate one, or send a referral to our "referral server"
326 * if we don't hold it.
328 be = select_backend( &e->e_nname, manageDSAit, 0 );
330 BerVarray ref = referral_rewrite( default_referral,
331 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
333 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
334 NULL, NULL, ref ? ref : default_referral, NULL );
336 if ( ref ) ber_bvarray_free( ref );
340 /* check restrictions */
341 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
342 if( rc != LDAP_SUCCESS ) {
343 send_ldap_result( conn, op, rc,
344 NULL, text, NULL, NULL );
348 /* check for referrals */
349 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
350 if ( rc != LDAP_SUCCESS ) {
354 #if defined( LDAP_SLAPI )
355 slapi_x_backend_set_pb( pb, be );
356 slapi_x_connection_set_pb( pb, conn );
357 slapi_x_operation_set_pb( pb, op );
358 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
359 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn.bv_val );
360 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
362 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
365 * A preoperation plugin failure will abort the
369 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
372 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
374 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
379 #endif /* defined( LDAP_SLAPI ) */
382 * do the add if 1 && (2 || 3)
383 * 1) there is an add function implemented in this backend;
384 * 2) this backend is master for what it holds;
385 * 3) it's a replica and the dn supplied is the updatedn.
388 /* do the update here */
389 int repl_user = be_isupdate(be, &op->o_ndn );
390 #ifndef SLAPD_MULTIMASTER
391 if ( !be->be_update_ndn.bv_len || repl_user )
394 int update = be->be_update_ndn.bv_len;
395 char textbuf[SLAP_TEXT_BUFLEN];
396 size_t textlen = sizeof textbuf;
398 rc = slap_mods_check( modlist, update, &text,
401 if( rc != LDAP_SUCCESS ) {
402 send_ldap_result( conn, op, rc,
403 NULL, text, NULL, NULL );
408 for( modtail = &modlist;
410 modtail = &(*modtail)->sml_next )
412 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
413 assert( (*modtail)->sml_desc != NULL );
415 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
417 if( rc != LDAP_SUCCESS ) {
418 send_ldap_result( conn, op, rc,
419 NULL, text, NULL, NULL );
424 rc = slap_mods2entry( modlist, &e, repl_user, &text,
426 if( rc != LDAP_SUCCESS ) {
427 send_ldap_result( conn, op, rc,
428 NULL, text, NULL, NULL );
432 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
433 #ifdef SLAPD_MULTIMASTER
437 replog( be, op, &e->e_name, &e->e_nname, e );
439 be_entry_release_w( be, conn, op, e );
443 #ifndef SLAPD_MULTIMASTER
445 BerVarray defref = be->be_update_refs
446 ? be->be_update_refs : default_referral;
447 BerVarray ref = referral_rewrite( defref,
448 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
450 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
451 ref ? ref : defref, NULL );
453 if ( ref ) ber_bvarray_free( ref );
458 LDAP_LOG( OPERATION, INFO,
459 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
461 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
463 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
464 NULL, "operation not supported within namingContext", NULL, NULL );
467 #if defined( LDAP_SLAPI )
469 * Postoperation errors are silently ignored; the work has
472 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb ) != 0) {
474 LDAP_LOG( OPERATION, INFO, "do_add: Add postoperation plugins failed\n",
477 Debug(LDAP_DEBUG_TRACE, "do_add: Add postoperation plugins failed.\n",
481 #endif /* defined( LDAP_SLAPI ) */
484 if( modlist != NULL ) {
485 slap_mods_free( modlist );
500 char *textbuf, size_t textlen )
502 Attribute **tail = &(*e)->e_attrs;
503 assert( *tail == NULL );
507 for( ; mods != NULL; mods = mods->sml_next ) {
510 assert( mods->sml_op == LDAP_MOD_ADD );
511 assert( mods->sml_desc != NULL );
513 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
516 #define SLURPD_FRIENDLY
517 #ifdef SLURPD_FRIENDLY
521 snprintf( textbuf, textlen,
522 "attribute '%s' provided more than once",
523 mods->sml_desc->ad_cname.bv_val );
524 return LDAP_TYPE_OR_VALUE_EXISTS;
527 for( i=0; attr->a_vals[i].bv_val; i++ ) {
530 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
535 attr->a_vals = ch_realloc( attr->a_vals,
536 sizeof( struct berval ) * (i+j) );
538 /* should check for duplicates */
540 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
541 sizeof( struct berval ) * j );
543 /* trim the mods array */
544 ch_free( mods->sml_bvalues );
545 mods->sml_bvalues = NULL;
549 snprintf( textbuf, textlen,
550 "attribute '%s' provided more than once",
551 mods->sml_desc->ad_cname.bv_val );
552 return LDAP_TYPE_OR_VALUE_EXISTS;
556 if( mods->sml_bvalues[1].bv_val != NULL ) {
557 /* check for duplicates */
559 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
561 /* check if the values we're adding already exist */
562 if( mr == NULL || !mr->smr_match ) {
563 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
564 /* test asserted values against themselves */
565 for( j = 0; j < i; j++ ) {
566 if ( bvmatch( &mods->sml_bvalues[i],
567 &mods->sml_bvalues[j] ) ) {
568 /* value exists already */
569 snprintf( textbuf, textlen,
570 "%s: value #%d provided more than once",
571 mods->sml_desc->ad_cname.bv_val, j );
572 return LDAP_TYPE_OR_VALUE_EXISTS;
579 const char *text = NULL;
580 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
582 rc = modify_check_duplicates( mods->sml_desc, mr,
583 NULL, mods->sml_bvalues, 0,
584 &text, textbuf, sizeof( textbuf ) );
586 if ( rc != LDAP_SUCCESS ) {
592 attr = ch_calloc( 1, sizeof(Attribute) );
594 /* move ad to attr structure */
595 attr->a_desc = mods->sml_desc;
596 mods->sml_desc = NULL;
598 /* move values to attr structure */
599 /* should check for duplicates */
600 attr->a_vals = mods->sml_bvalues;
601 mods->sml_bvalues = NULL;
604 tail = &attr->a_next;