3 * Copyright 1998-2002 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>
28 static int slap_mods2entry(
33 char *textbuf, size_t textlen );
36 do_add( Connection *conn, Operation *op )
38 BerElement *ber = op->o_ber;
40 struct berval dn = { 0, NULL };
45 Modifications *modlist = NULL;
46 Modifications **modtail = &modlist;
49 int rc = LDAP_SUCCESS;
53 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
55 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
58 * Parse the add request. It looks like this:
60 * AddRequest := [APPLICATION 14] SEQUENCE {
61 * name DistinguishedName,
62 * attrs SEQUENCE OF SEQUENCE {
64 * values SET OF AttributeValue
70 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
72 LDAP_LOG( OPERATION, ERR,
73 "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
75 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
77 send_ldap_disconnect( conn, op,
78 LDAP_PROTOCOL_ERROR, "decoding error" );
82 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
84 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
86 if( rc != LDAP_SUCCESS ) {
88 LDAP_LOG( OPERATION, ERR,
89 "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
91 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
93 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
94 "invalid DN", NULL, NULL );
99 LDAP_LOG( OPERATION, ARGS,
100 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn, 0 );
102 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
106 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
107 tag = ber_next_element( ber, &len, last ) )
112 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
114 if ( rtag == LBER_ERROR ) {
116 LDAP_LOG( OPERATION, ERR,
117 "do_add: conn %d decoding error \n", conn->c_connid, 0, 0 );
119 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
121 send_ldap_disconnect( conn, op,
122 LDAP_PROTOCOL_ERROR, "decoding error" );
127 if ( tmp.sml_bvalues == NULL ) {
129 LDAP_LOG( OPERATION, INFO,
130 "do_add: conn %d no values for type %s\n",
131 conn->c_connid, tmp.sml_type.bv_val, 0 );
133 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
134 tmp.sml_type.bv_val, 0, 0 );
136 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
137 NULL, "no values for attribute type", NULL, NULL );
140 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
142 mod->sml_op = LDAP_MOD_ADD;
143 mod->sml_next = NULL;
144 mod->sml_desc = NULL;
145 mod->sml_type = tmp.sml_type;
146 mod->sml_bvalues = tmp.sml_bvalues;
149 modtail = &mod->sml_next;
152 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
154 LDAP_LOG( OPERATION, ERR,
155 "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
157 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
159 send_ldap_disconnect( conn, op,
160 LDAP_PROTOCOL_ERROR, "decoding error" );
165 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
167 LDAP_LOG( OPERATION, INFO,
168 "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
170 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
175 if ( modlist == NULL ) {
176 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
177 NULL, "no attributes provided", NULL, NULL );
181 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
182 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
184 if( e->e_nname.bv_len == 0 ) {
185 /* protocolError may be a more appropriate error */
186 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
187 NULL, "root DSE already exists",
191 #if defined( SLAPD_SCHEMA_DN )
192 } else if ( strcasecmp( e->e_ndn, SLAPD_SCHEMA_DN ) == 0 ) {
193 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
194 NULL, "subschema subentry already exists",
200 manageDSAit = get_manageDSAit( op );
203 * We could be serving multiple database backends. Select the
204 * appropriate one, or send a referral to our "referral server"
205 * if we don't hold it.
207 be = select_backend( &e->e_nname, manageDSAit, 0 );
209 BerVarray ref = referral_rewrite( default_referral,
210 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
212 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
213 NULL, NULL, ref ? ref : default_referral, NULL );
215 if ( ref ) ber_bvarray_free( ref );
219 /* check restrictions */
220 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
221 if( rc != LDAP_SUCCESS ) {
222 send_ldap_result( conn, op, rc,
223 NULL, text, NULL, NULL );
227 /* check for referrals */
228 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
229 if ( rc != LDAP_SUCCESS ) {
234 * do the add if 1 && (2 || 3)
235 * 1) there is an add function implemented in this backend;
236 * 2) this backend is master for what it holds;
237 * 3) it's a replica and the dn supplied is the updatedn.
240 /* do the update here */
241 int repl_user = be_isupdate(be, &op->o_ndn );
242 #ifndef SLAPD_MULTIMASTER
243 if ( !be->be_update_ndn.bv_len || repl_user )
246 int update = be->be_update_ndn.bv_len;
247 char textbuf[SLAP_TEXT_BUFLEN];
248 size_t textlen = sizeof textbuf;
250 rc = slap_mods_check( modlist, update, &text,
253 if( rc != LDAP_SUCCESS ) {
254 send_ldap_result( conn, op, rc,
255 NULL, text, NULL, NULL );
260 for( modtail = &modlist;
262 modtail = &(*modtail)->sml_next )
264 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
265 assert( (*modtail)->sml_desc != NULL );
267 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
269 if( rc != LDAP_SUCCESS ) {
270 send_ldap_result( conn, op, rc,
271 NULL, text, NULL, NULL );
276 rc = slap_mods2entry( modlist, &e, repl_user, &text,
278 if( rc != LDAP_SUCCESS ) {
279 send_ldap_result( conn, op, rc,
280 NULL, text, NULL, NULL );
284 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
285 #ifdef SLAPD_MULTIMASTER
289 replog( be, op, &e->e_name, &e->e_nname, e );
291 be_entry_release_w( be, conn, op, e );
295 #ifndef SLAPD_MULTIMASTER
297 BerVarray defref = be->be_update_refs
298 ? be->be_update_refs : default_referral;
299 BerVarray ref = referral_rewrite( defref,
300 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
302 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
303 ref ? ref : defref, NULL );
305 if ( ref ) ber_bvarray_free( ref );
310 LDAP_LOG( OPERATION, INFO,
311 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
313 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
315 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
316 NULL, "operation not supported within namingContext", NULL, NULL );
320 if( modlist != NULL ) {
321 slap_mods_free( modlist );
330 static int slap_mods2entry(
335 char *textbuf, size_t textlen )
337 Attribute **tail = &(*e)->e_attrs;
338 assert( *tail == NULL );
342 for( ; mods != NULL; mods = mods->sml_next ) {
345 assert( mods->sml_op == LDAP_MOD_ADD );
346 assert( mods->sml_desc != NULL );
348 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
351 #define SLURPD_FRIENDLY
352 #ifdef SLURPD_FRIENDLY
356 snprintf( textbuf, textlen,
357 "attribute '%s' provided more than once",
358 mods->sml_desc->ad_cname.bv_val );
359 return LDAP_TYPE_OR_VALUE_EXISTS;
362 for( i=0; attr->a_vals[i].bv_val; i++ ) {
365 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
370 attr->a_vals = ch_realloc( attr->a_vals,
371 sizeof( struct berval ) * (i+j) );
373 /* should check for duplicates */
375 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
376 sizeof( struct berval ) * j );
378 /* trim the mods array */
379 ch_free( mods->sml_bvalues );
380 mods->sml_bvalues = NULL;
384 snprintf( textbuf, textlen,
385 "attribute '%s' provided more than once",
386 mods->sml_desc->ad_cname.bv_val );
387 return LDAP_TYPE_OR_VALUE_EXISTS;
391 if( mods->sml_bvalues[1].bv_val != NULL ) {
392 /* check for duplicates */
394 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
396 /* check if the values we're adding already exist */
397 if( mr == NULL || !mr->smr_match ) {
398 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
399 /* test asserted values against themselves */
400 for( j = 0; j < i; j++ ) {
401 int rc = ber_bvcmp( &mods->sml_bvalues[i],
402 &mods->sml_bvalues[j] );
405 /* value exists already */
406 snprintf( textbuf, textlen,
407 "%s: value #%d provided more than once",
408 mods->sml_desc->ad_cname.bv_val, j );
409 return LDAP_TYPE_OR_VALUE_EXISTS;
415 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
417 const char *text = NULL;
418 struct berval asserted;
420 rc = value_normalize( mods->sml_desc,
422 &mods->sml_bvalues[i],
426 if( rc != LDAP_SUCCESS ) return rc;
428 for ( j = 0; j < i; j++ ) {
429 int rc = value_match( &match, mods->sml_desc, mr,
430 SLAP_MR_VALUE_SYNTAX_MATCH,
431 &mods->sml_bvalues[j], &asserted, &text );
433 if( rc == LDAP_SUCCESS && match == 0 ) {
434 free( asserted.bv_val );
435 snprintf( textbuf, textlen,
436 "%s: value #%d provided more than once",
437 mods->sml_desc->ad_cname.bv_val, j );
438 return LDAP_TYPE_OR_VALUE_EXISTS;
442 free( asserted.bv_val );
447 attr = ch_calloc( 1, sizeof(Attribute) );
449 /* move ad to attr structure */
450 attr->a_desc = mods->sml_desc;
451 mods->sml_desc = NULL;
453 /* move values to attr structure */
454 /* should check for duplicates */
455 attr->a_vals = mods->sml_bvalues;
456 mods->sml_bvalues = NULL;
459 tail = &attr->a_next;