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 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
192 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
193 NULL, "subschema subentry already exists",
198 manageDSAit = get_manageDSAit( op );
201 * We could be serving multiple database backends. Select the
202 * appropriate one, or send a referral to our "referral server"
203 * if we don't hold it.
205 be = select_backend( &e->e_nname, manageDSAit, 0 );
207 BerVarray ref = referral_rewrite( default_referral,
208 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
210 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
211 NULL, NULL, ref ? ref : default_referral, NULL );
213 if ( ref ) ber_bvarray_free( ref );
217 /* check restrictions */
218 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
219 if( rc != LDAP_SUCCESS ) {
220 send_ldap_result( conn, op, rc,
221 NULL, text, NULL, NULL );
225 /* check for referrals */
226 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
227 if ( rc != LDAP_SUCCESS ) {
232 * do the add if 1 && (2 || 3)
233 * 1) there is an add function implemented in this backend;
234 * 2) this backend is master for what it holds;
235 * 3) it's a replica and the dn supplied is the updatedn.
238 /* do the update here */
239 int repl_user = be_isupdate(be, &op->o_ndn );
240 #ifndef SLAPD_MULTIMASTER
241 if ( !be->be_update_ndn.bv_len || repl_user )
244 int update = be->be_update_ndn.bv_len;
245 char textbuf[SLAP_TEXT_BUFLEN];
246 size_t textlen = sizeof textbuf;
248 rc = slap_mods_check( modlist, update, &text,
251 if( rc != LDAP_SUCCESS ) {
252 send_ldap_result( conn, op, rc,
253 NULL, text, NULL, NULL );
258 for( modtail = &modlist;
260 modtail = &(*modtail)->sml_next )
262 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
263 assert( (*modtail)->sml_desc != NULL );
265 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
267 if( rc != LDAP_SUCCESS ) {
268 send_ldap_result( conn, op, rc,
269 NULL, text, NULL, NULL );
274 rc = slap_mods2entry( modlist, &e, repl_user, &text,
276 if( rc != LDAP_SUCCESS ) {
277 send_ldap_result( conn, op, rc,
278 NULL, text, NULL, NULL );
282 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
283 #ifdef SLAPD_MULTIMASTER
287 replog( be, op, &e->e_name, &e->e_nname, e );
289 be_entry_release_w( be, conn, op, e );
293 #ifndef SLAPD_MULTIMASTER
295 BerVarray defref = be->be_update_refs
296 ? be->be_update_refs : default_referral;
297 BerVarray ref = referral_rewrite( defref,
298 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
300 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
301 ref ? ref : defref, NULL );
303 if ( ref ) ber_bvarray_free( ref );
308 LDAP_LOG( OPERATION, INFO,
309 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
311 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
313 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
314 NULL, "operation not supported within namingContext", NULL, NULL );
318 if( modlist != NULL ) {
319 slap_mods_free( modlist );
328 static int slap_mods2entry(
333 char *textbuf, size_t textlen )
335 Attribute **tail = &(*e)->e_attrs;
336 assert( *tail == NULL );
340 for( ; mods != NULL; mods = mods->sml_next ) {
343 assert( mods->sml_op == LDAP_MOD_ADD );
344 assert( mods->sml_desc != NULL );
346 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
349 #define SLURPD_FRIENDLY
350 #ifdef SLURPD_FRIENDLY
354 snprintf( textbuf, textlen,
355 "attribute '%s' provided more than once",
356 mods->sml_desc->ad_cname.bv_val );
357 return LDAP_TYPE_OR_VALUE_EXISTS;
360 for( i=0; attr->a_vals[i].bv_val; i++ ) {
363 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
368 attr->a_vals = ch_realloc( attr->a_vals,
369 sizeof( struct berval ) * (i+j) );
371 /* should check for duplicates */
373 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
374 sizeof( struct berval ) * j );
376 /* trim the mods array */
377 ch_free( mods->sml_bvalues );
378 mods->sml_bvalues = NULL;
382 snprintf( textbuf, textlen,
383 "attribute '%s' provided more than once",
384 mods->sml_desc->ad_cname.bv_val );
385 return LDAP_TYPE_OR_VALUE_EXISTS;
389 if( mods->sml_bvalues[1].bv_val != NULL ) {
390 /* check for duplicates */
392 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
394 /* check if the values we're adding already exist */
395 if( mr == NULL || !mr->smr_match ) {
396 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
397 /* test asserted values against themselves */
398 for( j = 0; j < i; j++ ) {
399 if ( bvmatch( &mods->sml_bvalues[i],
400 &mods->sml_bvalues[j] ) ) {
401 /* value exists already */
402 snprintf( textbuf, textlen,
403 "%s: value #%d provided more than once",
404 mods->sml_desc->ad_cname.bv_val, j );
405 return LDAP_TYPE_OR_VALUE_EXISTS;
412 const char *text = NULL;
413 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
415 rc = modify_check_duplicates( mods->sml_desc, mr,
416 NULL, mods->sml_bvalues,
417 &text, textbuf, sizeof( textbuf ) );
419 if ( rc != LDAP_SUCCESS ) {
425 attr = ch_calloc( 1, sizeof(Attribute) );
427 /* move ad to attr structure */
428 attr->a_desc = mods->sml_desc;
429 mods->sml_desc = NULL;
431 /* move values to attr structure */
432 /* should check for duplicates */
433 attr->a_vals = mods->sml_bvalues;
434 mods->sml_bvalues = NULL;
437 tail = &attr->a_next;