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;
43 int rc = LDAP_SUCCESS;
46 Slapi_PBlock *pb = op->o_pb;
49 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
51 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
54 * Parse the add request. It looks like this:
56 * AddRequest := [APPLICATION 14] SEQUENCE {
57 * name DistinguishedName,
58 * attrs SEQUENCE OF SEQUENCE {
60 * values SET OF AttributeValue
66 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
68 LDAP_LOG( OPERATION, ERR,
69 "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
71 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
73 send_ldap_disconnect( conn, op,
74 LDAP_PROTOCOL_ERROR, "decoding error" );
78 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
80 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
82 if( rc != LDAP_SUCCESS ) {
84 LDAP_LOG( OPERATION, ERR,
85 "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
87 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
89 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
90 "invalid DN", NULL, NULL );
95 LDAP_LOG( OPERATION, ARGS,
96 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn, 0 );
98 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
102 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
103 tag = ber_next_element( ber, &len, last ) )
108 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
110 if ( rtag == LBER_ERROR ) {
112 LDAP_LOG( OPERATION, ERR,
113 "do_add: conn %d decoding error \n", conn->c_connid, 0, 0 );
115 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
117 send_ldap_disconnect( conn, op,
118 LDAP_PROTOCOL_ERROR, "decoding error" );
123 if ( tmp.sml_bvalues == NULL ) {
125 LDAP_LOG( OPERATION, INFO,
126 "do_add: conn %d no values for type %s\n",
127 conn->c_connid, tmp.sml_type.bv_val, 0 );
129 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
130 tmp.sml_type.bv_val, 0, 0 );
132 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
133 NULL, "no values for attribute type", NULL, NULL );
136 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
138 mod->sml_op = LDAP_MOD_ADD;
139 mod->sml_next = NULL;
140 mod->sml_desc = NULL;
141 mod->sml_type = tmp.sml_type;
142 mod->sml_bvalues = tmp.sml_bvalues;
145 modtail = &mod->sml_next;
148 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
150 LDAP_LOG( OPERATION, ERR,
151 "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
153 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
155 send_ldap_disconnect( conn, op,
156 LDAP_PROTOCOL_ERROR, "decoding error" );
161 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
163 LDAP_LOG( OPERATION, INFO,
164 "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
166 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
171 if ( modlist == NULL ) {
172 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
173 NULL, "no attributes provided", NULL, NULL );
177 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
178 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
180 if( e->e_nname.bv_len == 0 ) {
181 /* protocolError may be a more appropriate error */
182 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
183 NULL, "root DSE already exists",
187 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
188 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
189 NULL, "subschema subentry already exists",
194 manageDSAit = get_manageDSAit( op );
197 * We could be serving multiple database backends. Select the
198 * appropriate one, or send a referral to our "referral server"
199 * if we don't hold it.
201 be = select_backend( &e->e_nname, manageDSAit, 0 );
203 BerVarray ref = referral_rewrite( default_referral,
204 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
206 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
207 NULL, NULL, ref ? ref : default_referral, NULL );
209 if ( ref ) ber_bvarray_free( ref );
213 /* check restrictions */
214 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
215 if( rc != LDAP_SUCCESS ) {
216 send_ldap_result( conn, op, rc,
217 NULL, text, NULL, NULL );
221 /* check for referrals */
222 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
223 if ( rc != LDAP_SUCCESS ) {
227 #if defined( LDAP_SLAPI )
228 slapi_x_backend_set_pb( pb, be );
229 slapi_x_connection_set_pb( pb, conn );
230 slapi_x_operation_set_pb( pb, op );
231 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
232 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn.bv_val );
233 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
235 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
238 * A preoperation plugin failure will abort the
242 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
245 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
247 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
252 #endif /* defined( LDAP_SLAPI ) */
255 * do the add if 1 && (2 || 3)
256 * 1) there is an add function implemented in this backend;
257 * 2) this backend is master for what it holds;
258 * 3) it's a replica and the dn supplied is the updatedn.
261 /* do the update here */
262 int repl_user = be_isupdate(be, &op->o_ndn );
263 #ifndef SLAPD_MULTIMASTER
264 if ( !be->be_update_ndn.bv_len || repl_user )
267 int update = be->be_update_ndn.bv_len;
268 char textbuf[SLAP_TEXT_BUFLEN];
269 size_t textlen = sizeof textbuf;
271 rc = slap_mods_check( modlist, update, &text,
274 if( rc != LDAP_SUCCESS ) {
275 send_ldap_result( conn, op, rc,
276 NULL, text, NULL, NULL );
281 for( modtail = &modlist;
283 modtail = &(*modtail)->sml_next )
285 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
286 assert( (*modtail)->sml_desc != NULL );
288 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
290 if( rc != LDAP_SUCCESS ) {
291 send_ldap_result( conn, op, rc,
292 NULL, text, NULL, NULL );
297 rc = slap_mods2entry( modlist, &e, repl_user, &text,
299 if( rc != LDAP_SUCCESS ) {
300 send_ldap_result( conn, op, rc,
301 NULL, text, NULL, NULL );
305 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
306 #ifdef SLAPD_MULTIMASTER
310 replog( be, op, &e->e_name, &e->e_nname, e );
312 be_entry_release_w( be, conn, op, e );
316 #ifndef SLAPD_MULTIMASTER
318 BerVarray defref = be->be_update_refs
319 ? be->be_update_refs : default_referral;
320 BerVarray ref = referral_rewrite( defref,
321 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
323 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
324 ref ? ref : defref, NULL );
326 if ( ref ) ber_bvarray_free( ref );
331 LDAP_LOG( OPERATION, INFO,
332 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
334 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
336 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
337 NULL, "operation not supported within namingContext", NULL, NULL );
340 #if defined( LDAP_SLAPI )
342 * Postoperation errors are silently ignored; the work has
345 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb ) != 0) {
347 LDAP_LOG( OPERATION, INFO, "do_add: Add postoperation plugins failed\n",
350 Debug(LDAP_DEBUG_TRACE, "do_add: Add postoperation plugins failed.\n",
354 #endif /* defined( LDAP_SLAPI ) */
357 if( modlist != NULL ) {
358 slap_mods_free( modlist );
373 char *textbuf, size_t textlen )
375 Attribute **tail = &(*e)->e_attrs;
376 assert( *tail == NULL );
380 for( ; mods != NULL; mods = mods->sml_next ) {
383 assert( mods->sml_op == LDAP_MOD_ADD );
384 assert( mods->sml_desc != NULL );
386 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
389 #define SLURPD_FRIENDLY
390 #ifdef SLURPD_FRIENDLY
394 snprintf( textbuf, textlen,
395 "attribute '%s' provided more than once",
396 mods->sml_desc->ad_cname.bv_val );
397 return LDAP_TYPE_OR_VALUE_EXISTS;
400 for( i=0; attr->a_vals[i].bv_val; i++ ) {
403 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
408 attr->a_vals = ch_realloc( attr->a_vals,
409 sizeof( struct berval ) * (i+j) );
411 /* should check for duplicates */
413 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
414 sizeof( struct berval ) * j );
416 /* trim the mods array */
417 ch_free( mods->sml_bvalues );
418 mods->sml_bvalues = NULL;
422 snprintf( textbuf, textlen,
423 "attribute '%s' provided more than once",
424 mods->sml_desc->ad_cname.bv_val );
425 return LDAP_TYPE_OR_VALUE_EXISTS;
429 if( mods->sml_bvalues[1].bv_val != NULL ) {
430 /* check for duplicates */
432 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
434 /* check if the values we're adding already exist */
435 if( mr == NULL || !mr->smr_match ) {
436 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
437 /* test asserted values against themselves */
438 for( j = 0; j < i; j++ ) {
439 if ( bvmatch( &mods->sml_bvalues[i],
440 &mods->sml_bvalues[j] ) ) {
441 /* value exists already */
442 snprintf( textbuf, textlen,
443 "%s: value #%d provided more than once",
444 mods->sml_desc->ad_cname.bv_val, j );
445 return LDAP_TYPE_OR_VALUE_EXISTS;
452 const char *text = NULL;
453 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
455 rc = modify_check_duplicates( mods->sml_desc, mr,
456 NULL, mods->sml_bvalues, 0,
457 &text, textbuf, sizeof( textbuf ) );
459 if ( rc != LDAP_SUCCESS ) {
465 attr = ch_calloc( 1, sizeof(Attribute) );
467 /* move ad to attr structure */
468 attr->a_desc = mods->sml_desc;
469 mods->sml_desc = NULL;
471 /* move values to attr structure */
472 /* should check for duplicates */
473 attr->a_vals = mods->sml_bvalues;
474 mods->sml_bvalues = NULL;
477 tail = &attr->a_next;