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.
19 #include "slapi_common.h"
22 #include <ac/string.h>
24 #include <ac/socket.h>
31 do_add( Connection *conn, Operation *op )
33 BerElement *ber = op->o_ber;
35 struct berval dn = { 0, NULL };
40 Modifications *modlist = NULL;
41 Modifications **modtail = &modlist;
44 int rc = LDAP_SUCCESS;
47 Slapi_PBlock *pb = op->o_pb;
50 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
52 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
55 * Parse the add request. It looks like this:
57 * AddRequest := [APPLICATION 14] SEQUENCE {
58 * name DistinguishedName,
59 * attrs SEQUENCE OF SEQUENCE {
61 * values SET OF AttributeValue
67 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
69 LDAP_LOG( OPERATION, ERR,
70 "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
72 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
74 send_ldap_disconnect( conn, op,
75 LDAP_PROTOCOL_ERROR, "decoding error" );
79 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
81 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
83 if( rc != LDAP_SUCCESS ) {
85 LDAP_LOG( OPERATION, ERR,
86 "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
88 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
90 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
91 "invalid DN", NULL, NULL );
96 LDAP_LOG( OPERATION, ARGS,
97 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn, 0 );
99 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
103 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
104 tag = ber_next_element( ber, &len, last ) )
109 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
111 if ( rtag == LBER_ERROR ) {
113 LDAP_LOG( OPERATION, ERR,
114 "do_add: conn %d decoding error \n", conn->c_connid, 0, 0 );
116 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
118 send_ldap_disconnect( conn, op,
119 LDAP_PROTOCOL_ERROR, "decoding error" );
124 if ( tmp.sml_bvalues == NULL ) {
126 LDAP_LOG( OPERATION, INFO,
127 "do_add: conn %d no values for type %s\n",
128 conn->c_connid, tmp.sml_type.bv_val, 0 );
130 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
131 tmp.sml_type.bv_val, 0, 0 );
133 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
134 NULL, "no values for attribute type", NULL, NULL );
137 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
139 mod->sml_op = LDAP_MOD_ADD;
140 mod->sml_next = NULL;
141 mod->sml_desc = NULL;
142 mod->sml_type = tmp.sml_type;
143 mod->sml_bvalues = tmp.sml_bvalues;
146 modtail = &mod->sml_next;
149 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
151 LDAP_LOG( OPERATION, ERR,
152 "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
154 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
156 send_ldap_disconnect( conn, op,
157 LDAP_PROTOCOL_ERROR, "decoding error" );
162 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
164 LDAP_LOG( OPERATION, INFO,
165 "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
167 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
172 if ( modlist == NULL ) {
173 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
174 NULL, "no attributes provided", NULL, NULL );
178 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
179 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
181 if( e->e_nname.bv_len == 0 ) {
182 /* protocolError may be a more appropriate error */
183 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
184 NULL, "root DSE already exists",
188 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
189 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
190 NULL, "subschema subentry already exists",
195 manageDSAit = get_manageDSAit( op );
198 * We could be serving multiple database backends. Select the
199 * appropriate one, or send a referral to our "referral server"
200 * if we don't hold it.
202 be = select_backend( &e->e_nname, manageDSAit, 0 );
204 BerVarray ref = referral_rewrite( default_referral,
205 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
207 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
208 NULL, NULL, ref ? ref : default_referral, NULL );
210 if ( ref ) ber_bvarray_free( ref );
214 /* check restrictions */
215 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
216 if( rc != LDAP_SUCCESS ) {
217 send_ldap_result( conn, op, rc,
218 NULL, text, NULL, NULL );
222 /* check for referrals */
223 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
224 if ( rc != LDAP_SUCCESS ) {
228 #if defined( LDAP_SLAPI )
229 slapi_x_backend_set_pb( pb, be );
230 slapi_x_connection_set_pb( pb, conn );
231 slapi_x_operation_set_pb( pb, op );
232 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
233 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn.bv_val );
234 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(1) );
236 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
239 * A preoperation plugin failure will abort the
243 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
246 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
248 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
253 #endif /* defined( LDAP_SLAPI ) */
256 * do the add if 1 && (2 || 3)
257 * 1) there is an add function implemented in this backend;
258 * 2) this backend is master for what it holds;
259 * 3) it's a replica and the dn supplied is the updatedn.
262 /* do the update here */
263 int repl_user = be_isupdate(be, &op->o_ndn );
264 #ifndef SLAPD_MULTIMASTER
265 if ( !be->be_update_ndn.bv_len || repl_user )
268 int update = be->be_update_ndn.bv_len;
269 char textbuf[SLAP_TEXT_BUFLEN];
270 size_t textlen = sizeof textbuf;
272 rc = slap_mods_check( modlist, update, &text,
275 if( rc != LDAP_SUCCESS ) {
276 send_ldap_result( conn, op, rc,
277 NULL, text, NULL, NULL );
282 for( modtail = &modlist;
284 modtail = &(*modtail)->sml_next )
286 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
287 assert( (*modtail)->sml_desc != NULL );
289 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
291 if( rc != LDAP_SUCCESS ) {
292 send_ldap_result( conn, op, rc,
293 NULL, text, NULL, NULL );
298 rc = slap_mods2entry( modlist, &e, repl_user, &text,
300 if( rc != LDAP_SUCCESS ) {
301 send_ldap_result( conn, op, rc,
302 NULL, text, NULL, NULL );
306 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
307 #ifdef SLAPD_MULTIMASTER
311 replog( be, op, &e->e_name, &e->e_nname, e );
313 be_entry_release_w( be, conn, op, e );
317 #ifndef SLAPD_MULTIMASTER
319 BerVarray defref = be->be_update_refs
320 ? be->be_update_refs : default_referral;
321 BerVarray ref = referral_rewrite( defref,
322 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
324 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
325 ref ? ref : defref, NULL );
327 if ( ref ) ber_bvarray_free( ref );
332 LDAP_LOG( OPERATION, INFO,
333 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
335 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
337 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
338 NULL, "operation not supported within namingContext", NULL, NULL );
341 #if defined( LDAP_SLAPI )
343 * Postoperation errors are silently ignored; the work has
346 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb ) != 0) {
348 LDAP_LOG( OPERATION, INFO, "do_add: Add postoperation plugins failed\n",
351 Debug(LDAP_DEBUG_TRACE, "do_add: Add postoperation plugins failed.\n",
355 #endif /* defined( LDAP_SLAPI ) */
358 if( modlist != NULL ) {
359 slap_mods_free( modlist );
374 char *textbuf, size_t textlen )
376 Attribute **tail = &(*e)->e_attrs;
377 assert( *tail == NULL );
381 for( ; mods != NULL; mods = mods->sml_next ) {
384 assert( mods->sml_op == LDAP_MOD_ADD );
385 assert( mods->sml_desc != NULL );
387 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
390 #define SLURPD_FRIENDLY
391 #ifdef SLURPD_FRIENDLY
395 snprintf( textbuf, textlen,
396 "attribute '%s' provided more than once",
397 mods->sml_desc->ad_cname.bv_val );
398 return LDAP_TYPE_OR_VALUE_EXISTS;
401 for( i=0; attr->a_vals[i].bv_val; i++ ) {
404 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
409 attr->a_vals = ch_realloc( attr->a_vals,
410 sizeof( struct berval ) * (i+j) );
412 /* should check for duplicates */
414 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
415 sizeof( struct berval ) * j );
417 /* trim the mods array */
418 ch_free( mods->sml_bvalues );
419 mods->sml_bvalues = NULL;
423 snprintf( textbuf, textlen,
424 "attribute '%s' provided more than once",
425 mods->sml_desc->ad_cname.bv_val );
426 return LDAP_TYPE_OR_VALUE_EXISTS;
430 if( mods->sml_bvalues[1].bv_val != NULL ) {
431 /* check for duplicates */
433 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
435 /* check if the values we're adding already exist */
436 if( mr == NULL || !mr->smr_match ) {
437 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
438 /* test asserted values against themselves */
439 for( j = 0; j < i; j++ ) {
440 if ( bvmatch( &mods->sml_bvalues[i],
441 &mods->sml_bvalues[j] ) ) {
442 /* value exists already */
443 snprintf( textbuf, textlen,
444 "%s: value #%d provided more than once",
445 mods->sml_desc->ad_cname.bv_val, j );
446 return LDAP_TYPE_OR_VALUE_EXISTS;
453 const char *text = NULL;
454 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
456 rc = modify_check_duplicates( mods->sml_desc, mr,
457 NULL, mods->sml_bvalues,
458 &text, textbuf, sizeof( textbuf ) );
460 if ( rc != LDAP_SUCCESS ) {
466 attr = ch_calloc( 1, sizeof(Attribute) );
468 /* move ad to attr structure */
469 attr->a_desc = mods->sml_desc;
470 mods->sml_desc = NULL;
472 /* move values to attr structure */
473 /* should check for duplicates */
474 attr->a_vals = mods->sml_bvalues;
475 mods->sml_bvalues = NULL;
478 tail = &attr->a_next;