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(
34 do_add( Connection *conn, Operation *op )
36 BerElement *ber = op->o_ber;
38 struct berval dn = { 0, NULL };
43 Modifications *modlist = NULL;
44 Modifications **modtail = &modlist;
47 int rc = LDAP_SUCCESS;
51 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
52 "do_add: conn %d enter\n", conn->c_connid ));
54 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
57 * Parse the add request. It looks like this:
59 * AddRequest := [APPLICATION 14] SEQUENCE {
60 * name DistinguishedName,
61 * attrs SEQUENCE OF SEQUENCE {
63 * values SET OF AttributeValue
69 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
71 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
72 "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
74 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
76 send_ldap_disconnect( conn, op,
77 LDAP_PROTOCOL_ERROR, "decoding error" );
81 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
82 e->e_name.bv_val = NULL;
84 e->e_nname.bv_val = NULL;
85 e->e_nname.bv_len = 0;
89 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
91 if( rc != LDAP_SUCCESS ) {
93 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
94 "do_add: conn %d invalid dn (%s)\n", conn->c_connid,
97 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
99 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
100 "invalid DN", NULL, NULL );
105 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
106 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn ));
108 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
112 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
113 tag = ber_next_element( ber, &len, last ) )
117 rc = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
119 if ( rc == LBER_ERROR ) {
121 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
122 "do_add: conn %d decoding error \n", conn->c_connid ));
124 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
126 send_ldap_disconnect( conn, op,
127 LDAP_PROTOCOL_ERROR, "decoding error" );
132 if ( tmp.sml_bvalues == NULL ) {
134 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
135 "do_add: conn %d no values for type %s\n",
136 conn->c_connid, tmp.sml_type.bv_val ));
138 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
139 tmp.sml_type.bv_val, 0, 0 );
141 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
142 NULL, "no values for attribute type", NULL, NULL );
143 free( tmp.sml_type.bv_val );
146 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
148 mod->sml_op = LDAP_MOD_ADD;
149 mod->sml_next = NULL;
150 mod->sml_desc = NULL;
151 mod->sml_type = tmp.sml_type;
152 mod->sml_bvalues = tmp.sml_bvalues;
155 modtail = &mod->sml_next;
158 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
160 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
161 "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
163 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
165 send_ldap_disconnect( conn, op,
166 LDAP_PROTOCOL_ERROR, "decoding error" );
171 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
173 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
174 "do_add: conn %d get_ctrls failed\n", conn->c_connid ));
176 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
181 if ( modlist == NULL ) {
182 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
183 NULL, "no attributes provided", NULL, NULL );
187 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
188 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
190 if( e->e_nname.bv_len == 0 ) {
191 /* protocolError may be a more appropriate error */
192 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
193 NULL, "root DSE already exists",
197 #if defined( SLAPD_SCHEMA_DN )
198 } else if ( strcasecmp( e->e_ndn, SLAPD_SCHEMA_DN ) == 0 ) {
199 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
200 NULL, "subschema subentry already exists",
206 manageDSAit = get_manageDSAit( op );
209 * We could be serving multiple database backends. Select the
210 * appropriate one, or send a referral to our "referral server"
211 * if we don't hold it.
213 be = select_backend( &e->e_nname, manageDSAit, 0 );
215 BVarray ref = referral_rewrite( default_referral,
216 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
218 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
219 NULL, NULL, ref ? ref : default_referral, NULL );
221 if ( ref ) bvarray_free( ref );
225 /* check restrictions */
226 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
227 if( rc != LDAP_SUCCESS ) {
228 send_ldap_result( conn, op, rc,
229 NULL, text, NULL, NULL );
233 /* check for referrals */
234 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
235 if ( rc != LDAP_SUCCESS ) {
240 * do the add if 1 && (2 || 3)
241 * 1) there is an add function implemented in this backend;
242 * 2) this backend is master for what it holds;
243 * 3) it's a replica and the dn supplied is the updatedn.
246 /* do the update here */
247 int repl_user = be_isupdate(be, &op->o_ndn );
248 #ifndef SLAPD_MULTIMASTER
249 if ( !be->be_update_ndn.bv_len || repl_user )
252 int update = be->be_update_ndn.bv_len;
253 char textbuf[SLAP_TEXT_BUFLEN];
254 size_t textlen = sizeof textbuf;
256 rc = slap_mods_check( modlist, update, &text,
259 if( rc != LDAP_SUCCESS ) {
260 send_ldap_result( conn, op, rc,
261 NULL, text, NULL, NULL );
265 if ( SLAP_LASTMOD(be) && !repl_user ) {
266 for( modtail = &modlist;
268 modtail = &(*modtail)->sml_next )
270 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
271 assert( (*modtail)->sml_desc != NULL );
273 rc = slap_mods_opattrs( op, modlist, modtail, &text,
275 if( rc != LDAP_SUCCESS ) {
276 send_ldap_result( conn, op, rc,
277 NULL, text, NULL, NULL );
282 rc = slap_mods2entry( modlist, &e, &text );
283 if( rc != LDAP_SUCCESS ) {
284 send_ldap_result( conn, op, rc,
285 NULL, text, NULL, NULL );
289 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
290 #ifdef SLAPD_MULTIMASTER
294 replog( be, op, &e->e_name, &e->e_nname, e );
296 be_entry_release_w( be, conn, op, e );
300 #ifndef SLAPD_MULTIMASTER
302 BVarray defref = be->be_update_refs
303 ? be->be_update_refs : default_referral;
304 BVarray ref = referral_rewrite( defref,
305 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
307 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
308 ref ? ref : defref, NULL );
310 if ( ref ) bvarray_free( ref );
315 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
316 "do_add: conn %d no backend support\n", conn->c_connid ));
318 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
320 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
321 NULL, "operation not supported within namingContext", NULL, NULL );
325 if( modlist != NULL ) {
326 slap_mods_free( modlist );
335 static int slap_mods2entry(
340 Attribute **tail = &(*e)->e_attrs;
341 assert( *tail == NULL );
343 for( ; mods != NULL; mods = mods->sml_next ) {
346 assert( mods->sml_op == LDAP_MOD_ADD );
347 assert( mods->sml_desc != NULL );
349 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
352 #define SLURPD_FRIENDLY
353 #ifdef SLURPD_FRIENDLY
356 for( i=0; attr->a_vals[i].bv_val; i++ ) {
359 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
364 attr->a_vals = ch_realloc( attr->a_vals,
365 sizeof( struct berval ) * (i+j) );
367 /* should check for duplicates */
368 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
369 sizeof( struct berval ) * j );
371 /* trim the mods array */
372 ch_free( mods->sml_bvalues );
373 mods->sml_bvalues = NULL;
377 *text = "attribute provided more than once";
378 return LDAP_TYPE_OR_VALUE_EXISTS;
382 attr = ch_calloc( 1, sizeof(Attribute) );
384 /* move ad to attr structure */
385 attr->a_desc = mods->sml_desc;
386 mods->sml_desc = NULL;
388 /* move values to attr structure */
389 /* should check for duplicates */
390 attr->a_vals = mods->sml_bvalues;
391 mods->sml_bvalues = NULL;
394 tail = &attr->a_next;