3 * Copyright 1998-2000 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;
37 char *dn, *ndn, *last;
42 LDAPModList *modlist = NULL;
43 LDAPModList **modtail = &modlist;
44 Modifications *mods = NULL;
46 int rc = LDAP_SUCCESS;
50 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
51 "do_add: conn %d enter\n", conn->c_connid ));
53 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
56 * Parse the add request. It looks like this:
58 * AddRequest := [APPLICATION 14] SEQUENCE {
59 * name DistinguishedName,
60 * attrs SEQUENCE OF SEQUENCE {
62 * values SET OF AttributeValue
68 if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
70 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
71 "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
73 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
75 send_ldap_disconnect( conn, op,
76 LDAP_PROTOCOL_ERROR, "decoding error" );
80 ndn = ch_strdup( dn );
82 if ( dn_normalize( ndn ) == NULL ) {
84 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
85 "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn ));
87 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
89 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
90 "invalid DN", NULL, NULL );
93 return LDAP_INVALID_DN_SYNTAX;
96 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
104 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
105 "do_add: conn %d ndn (%s)\n", conn->c_connid, e->e_ndn ));
107 Debug( LDAP_DEBUG_ARGS, "do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
111 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
112 tag = ber_next_element( ber, &len, last ) )
114 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
115 mod->ml_op = LDAP_MOD_ADD;
118 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_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" );
134 if ( mod->ml_bvalues == NULL ) {
136 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
137 "do_add: conn %d no values for type %s\n",
138 conn->c_connid, mod->ml_type ));
140 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
141 mod->ml_type, 0, 0 );
143 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
144 NULL, "no values for attribute type", NULL, NULL );
145 free( mod->ml_type );
151 modtail = &mod->ml_next;
154 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
156 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
157 "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
159 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
161 send_ldap_disconnect( conn, op,
162 LDAP_PROTOCOL_ERROR, "decoding error" );
167 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
169 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
170 "do_add: conn %d get_ctrls failed\n", conn->c_connid ));
172 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
177 if ( modlist == NULL ) {
178 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
179 NULL, "no attributes provided", NULL, NULL );
183 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
184 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
186 if( e->e_ndn == NULL || *e->e_ndn == '\0' ) {
187 /* protocolError may be a more appropriate error */
188 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
189 NULL, "root DSE exists", NULL, NULL );
193 manageDSAit = get_manageDSAit( op ) &&
194 is_entry_referral( e );
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_ndn, manageDSAit );
203 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
204 NULL, NULL, default_referral, NULL );
208 /* check restrictions */
209 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
210 if( rc != LDAP_SUCCESS ) {
211 send_ldap_result( conn, op, rc,
212 NULL, text, NULL, NULL );
216 /* check for referrals */
217 rc = backend_check_referrals( be, conn, op, e->e_dn, e->e_ndn );
218 if ( rc != LDAP_SUCCESS ) {
223 * do the add if 1 && (2 || 3)
224 * 1) there is an add function implemented in this backend;
225 * 2) this backend is master for what it holds;
226 * 3) it's a replica and the dn supplied is the updatedn.
229 /* do the update here */
230 int repl_user = (be->be_update_ndn != NULL &&
231 strcmp( be->be_update_ndn, op->o_ndn ) == 0);
232 #ifndef SLAPD_MULTIMASTER
233 if ( be->be_update_ndn == NULL || repl_user )
236 int update = be->be_update_ndn != NULL;
238 rc = slap_modlist2mods( modlist, update, &mods, &text );
239 if( rc != LDAP_SUCCESS ) {
240 send_ldap_result( conn, op, rc,
241 NULL, text, NULL, NULL );
245 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
246 global_lastmod == ON)) && !repl_user )
248 Modifications **modstail;
249 for( modstail = &mods;
251 modstail = &(*modstail)->sml_next )
253 assert( (*modstail)->sml_op == LDAP_MOD_ADD );
254 assert( (*modstail)->sml_desc != NULL );
256 rc = slap_mods_opattrs( op, modstail, &text );
257 if( rc != LDAP_SUCCESS ) {
258 send_ldap_result( conn, op, rc,
259 NULL, text, NULL, NULL );
264 rc = slap_mods2entry( mods, &e, &text );
265 if( rc != LDAP_SUCCESS ) {
266 send_ldap_result( conn, op, rc,
267 NULL, text, NULL, NULL );
271 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
272 #ifdef SLAPD_MULTIMASTER
276 replog( be, op, e->e_dn, e );
278 be_entry_release_w( be, conn, op, e );
282 #ifndef SLAPD_MULTIMASTER
284 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
285 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
290 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
291 "do_add: conn %d no backend support\n", conn->c_connid ));
293 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
295 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
296 NULL, "operation not supported within namingContext", NULL, NULL );
300 if( modlist != NULL ) {
301 slap_modlist_free( modlist );
304 slap_mods_free( mods );
313 static int slap_mods2entry(
318 Attribute **tail = &(*e)->e_attrs;
319 assert( *tail == NULL );
321 for( ; mods != NULL; mods = mods->sml_next ) {
324 assert( mods->sml_op == LDAP_MOD_ADD );
325 assert( mods->sml_desc != NULL );
327 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
330 #define SLURPD_FRIENDLY
331 #ifdef SLURPD_FRIENDLY
334 for( i=0; attr->a_vals[i]; i++ ) {
337 for( j=0; mods->sml_bvalues[j]; j++ ) {
342 attr->a_vals = ch_realloc( attr->a_vals,
343 sizeof( struct berval * ) * (i+j) );
345 /* should check for duplicates */
346 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
347 sizeof( struct berval * ) * j );
349 /* trim the mods array */
350 ch_free( mods->sml_bvalues );
351 mods->sml_bvalues = NULL;
355 *text = "attribute provided more than once";
356 return LDAP_TYPE_OR_VALUE_EXISTS;
360 attr = ch_calloc( 1, sizeof(Attribute) );
362 /* move ad to attr structure */
363 attr->a_desc = mods->sml_desc;
364 mods->sml_desc = NULL;
366 /* move values to attr structure */
367 /* should check for duplicates */
368 attr->a_vals = mods->sml_bvalues;
369 mods->sml_bvalues = NULL;
372 tail = &attr->a_next;