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(
35 do_add( Connection *conn, Operation *op )
37 BerElement *ber = op->o_ber;
39 struct berval dn = { 0, NULL };
44 Modifications *modlist = NULL;
45 Modifications **modtail = &modlist;
48 int rc = LDAP_SUCCESS;
52 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
53 "do_add: conn %d enter\n", conn->c_connid ));
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", LDAP_LEVEL_ERR,
73 "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
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) );
83 e->e_name.bv_val = NULL;
85 e->e_nname.bv_val = NULL;
86 e->e_nname.bv_len = 0;
90 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
92 if( rc != LDAP_SUCCESS ) {
94 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
95 "do_add: conn %d invalid dn (%s)\n", conn->c_connid,
98 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
100 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
101 "invalid DN", NULL, NULL );
106 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
107 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn ));
109 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
113 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
114 tag = ber_next_element( ber, &len, last ) )
118 rc = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
120 if ( rc == LBER_ERROR ) {
122 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
123 "do_add: conn %d decoding error \n", conn->c_connid ));
125 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
127 send_ldap_disconnect( conn, op,
128 LDAP_PROTOCOL_ERROR, "decoding error" );
133 if ( tmp.sml_bvalues == NULL ) {
135 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
136 "do_add: conn %d no values for type %s\n",
137 conn->c_connid, tmp.sml_type.bv_val ));
139 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
140 tmp.sml_type.bv_val, 0, 0 );
142 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
143 NULL, "no values for attribute type", NULL, NULL );
144 free( tmp.sml_type.bv_val );
147 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
149 mod->sml_op = LDAP_MOD_ADD;
150 mod->sml_next = NULL;
151 mod->sml_desc = NULL;
152 mod->sml_type = tmp.sml_type;
153 mod->sml_bvalues = tmp.sml_bvalues;
156 modtail = &mod->sml_next;
159 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
161 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
162 "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
164 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
166 send_ldap_disconnect( conn, op,
167 LDAP_PROTOCOL_ERROR, "decoding error" );
172 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
174 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
175 "do_add: conn %d get_ctrls failed\n", conn->c_connid ));
177 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
182 if ( modlist == NULL ) {
183 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
184 NULL, "no attributes provided", NULL, NULL );
188 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
189 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
191 if( e->e_nname.bv_len == 0 ) {
192 /* protocolError may be a more appropriate error */
193 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
194 NULL, "root DSE already exists",
198 #if defined( SLAPD_SCHEMA_DN )
199 } else if ( strcasecmp( e->e_ndn, SLAPD_SCHEMA_DN ) == 0 ) {
200 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
201 NULL, "subschema subentry already exists",
207 manageDSAit = get_manageDSAit( op );
210 * We could be serving multiple database backends. Select the
211 * appropriate one, or send a referral to our "referral server"
212 * if we don't hold it.
214 be = select_backend( &e->e_nname, manageDSAit, 0 );
216 BerVarray ref = referral_rewrite( default_referral,
217 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
219 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
220 NULL, NULL, ref ? ref : default_referral, NULL );
222 if ( ref ) ber_bvarray_free( ref );
226 /* check restrictions */
227 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
228 if( rc != LDAP_SUCCESS ) {
229 send_ldap_result( conn, op, rc,
230 NULL, text, NULL, NULL );
234 /* check for referrals */
235 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
236 if ( rc != LDAP_SUCCESS ) {
241 * do the add if 1 && (2 || 3)
242 * 1) there is an add function implemented in this backend;
243 * 2) this backend is master for what it holds;
244 * 3) it's a replica and the dn supplied is the updatedn.
247 /* do the update here */
248 int repl_user = be_isupdate(be, &op->o_ndn );
249 #ifndef SLAPD_MULTIMASTER
250 if ( !be->be_update_ndn.bv_len || repl_user )
253 int update = be->be_update_ndn.bv_len;
254 char textbuf[SLAP_TEXT_BUFLEN];
255 size_t textlen = sizeof textbuf;
257 rc = slap_mods_check( modlist, update, &text,
260 if( rc != LDAP_SUCCESS ) {
261 send_ldap_result( conn, op, rc,
262 NULL, text, NULL, NULL );
266 if ( SLAP_LASTMOD(be) && !repl_user ) {
267 for( modtail = &modlist;
269 modtail = &(*modtail)->sml_next )
271 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
272 assert( (*modtail)->sml_desc != NULL );
274 rc = slap_mods_opattrs( op, modlist, modtail, &text,
276 if( rc != LDAP_SUCCESS ) {
277 send_ldap_result( conn, op, rc,
278 NULL, text, NULL, NULL );
283 rc = slap_mods2entry( modlist, &e, repl_user, &text );
284 if( rc != LDAP_SUCCESS ) {
285 send_ldap_result( conn, op, rc,
286 NULL, text, NULL, NULL );
290 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
291 #ifdef SLAPD_MULTIMASTER
295 replog( be, op, &e->e_name, &e->e_nname, e );
297 be_entry_release_w( be, conn, op, e );
301 #ifndef SLAPD_MULTIMASTER
303 BerVarray defref = be->be_update_refs
304 ? be->be_update_refs : default_referral;
305 BerVarray ref = referral_rewrite( defref,
306 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
308 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
309 ref ? ref : defref, NULL );
311 if ( ref ) ber_bvarray_free( ref );
316 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
317 "do_add: conn %d no backend support\n", conn->c_connid ));
319 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
321 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
322 NULL, "operation not supported within namingContext", NULL, NULL );
326 if( modlist != NULL ) {
327 slap_mods_free( modlist );
336 static int slap_mods2entry(
342 Attribute **tail = &(*e)->e_attrs;
343 assert( *tail == NULL );
345 for( ; mods != NULL; mods = mods->sml_next ) {
348 assert( mods->sml_op == LDAP_MOD_ADD );
349 assert( mods->sml_desc != NULL );
351 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
354 #define SLURPD_FRIENDLY
355 #ifdef SLURPD_FRIENDLY
359 *text = "attribute provided more than once";
360 return LDAP_TYPE_OR_VALUE_EXISTS;
363 for( i=0; attr->a_vals[i].bv_val; i++ ) {
366 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
371 attr->a_vals = ch_realloc( attr->a_vals,
372 sizeof( struct berval ) * (i+j) );
374 /* should check for duplicates */
376 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
377 sizeof( struct berval ) * j );
379 /* trim the mods array */
380 ch_free( mods->sml_bvalues );
381 mods->sml_bvalues = NULL;
385 *text = "attribute provided more than once";
386 return LDAP_TYPE_OR_VALUE_EXISTS;
390 if( mods->sml_bvalues[1].bv_val != NULL ) {
391 /* check for duplicates */
393 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
395 /* check if the values we're adding already exist */
396 if( mr == NULL || !mr->smr_match ) {
397 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
398 /* test asserted values against themselves */
399 for( j = 0; j < i; j++ ) {
400 int rc = ber_bvcmp( &mods->sml_bvalues[i],
401 &mods->sml_bvalues[j] );
404 /* value exists already */
405 return LDAP_TYPE_OR_VALUE_EXISTS;
411 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
413 const char *text = NULL;
414 struct berval asserted;
416 rc = value_normalize( mods->sml_desc,
418 &mods->sml_bvalues[i],
422 if( rc != LDAP_SUCCESS ) return rc;
424 for ( j = 0; j < i; j++ ) {
425 int rc = value_match( &match, mods->sml_desc, mr,
426 SLAP_MR_VALUE_SYNTAX_MATCH,
427 &mods->sml_bvalues[j], &asserted, &text );
429 if( rc == LDAP_SUCCESS && match == 0 ) {
430 free( asserted.bv_val );
431 return LDAP_TYPE_OR_VALUE_EXISTS;
435 free( asserted.bv_val );
440 attr = ch_calloc( 1, sizeof(Attribute) );
442 /* move ad to attr structure */
443 attr->a_desc = mods->sml_desc;
444 mods->sml_desc = NULL;
446 /* move values to attr structure */
447 /* should check for duplicates */
448 attr->a_vals = mods->sml_bvalues;
449 mods->sml_bvalues = NULL;
452 tail = &attr->a_next;