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;
38 struct berval dn = { 0, NULL };
43 LDAPModList *modlist = NULL;
44 LDAPModList **modtail = &modlist;
45 Modifications *mods = NULL;
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, "{o", /*}*/ &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;
90 struct berval *pdn = NULL;
91 rc = dnPretty( NULL, &dn, &pdn );
93 if( rc != LDAP_SUCCESS ) {
95 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
96 "do_add: conn %d invalid dn (%s)\n", conn->c_connid,
99 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
101 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
102 "invalid DN", NULL, NULL );
111 struct berval *ndn = NULL;
112 rc = dnNormalize( NULL, &dn, &ndn );
114 if( rc != LDAP_SUCCESS ) {
116 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
117 "do_add: conn %d invalid dn (%s)\n", conn->c_connid,
120 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
122 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
123 "invalid DN", NULL, NULL );
132 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
133 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn ));
135 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
139 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
140 tag = ber_next_element( ber, &len, last ) )
142 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
143 mod->ml_op = LDAP_MOD_ADD;
146 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
148 if ( rc == LBER_ERROR ) {
150 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
151 "do_add: conn %d decoding error \n", conn->c_connid ));
153 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
155 send_ldap_disconnect( conn, op,
156 LDAP_PROTOCOL_ERROR, "decoding error" );
162 if ( mod->ml_bvalues == NULL ) {
164 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
165 "do_add: conn %d no values for type %s\n",
166 conn->c_connid, mod->ml_type ));
168 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
169 mod->ml_type, 0, 0 );
171 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
172 NULL, "no values for attribute type", NULL, NULL );
173 free( mod->ml_type );
179 modtail = &mod->ml_next;
182 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
184 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
185 "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
187 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
189 send_ldap_disconnect( conn, op,
190 LDAP_PROTOCOL_ERROR, "decoding error" );
195 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
197 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
198 "do_add: conn %d get_ctrls failed\n", conn->c_connid ));
200 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
205 if ( modlist == NULL ) {
206 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
207 NULL, "no attributes provided", NULL, NULL );
211 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
212 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
214 if( e->e_ndn == NULL || *e->e_ndn == '\0' ) {
215 /* protocolError may be a more appropriate error */
216 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
217 NULL, "root DSE already exists",
221 #if defined( SLAPD_SCHEMA_DN )
222 } else if ( strcasecmp( e->e_ndn, SLAPD_SCHEMA_DN ) == 0 ) {
223 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
224 NULL, "subschema subentry already exists",
230 manageDSAit = get_manageDSAit( op );
233 * We could be serving multiple database backends. Select the
234 * appropriate one, or send a referral to our "referral server"
235 * if we don't hold it.
237 be = select_backend( &e->e_nname, manageDSAit, 0 );
239 struct berval **ref = referral_rewrite( default_referral,
240 NULL, e->e_dn, LDAP_SCOPE_DEFAULT );
242 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
243 NULL, NULL, ref ? ref : default_referral, NULL );
249 /* check restrictions */
250 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
251 if( rc != LDAP_SUCCESS ) {
252 send_ldap_result( conn, op, rc,
253 NULL, text, NULL, NULL );
257 /* check for referrals */
258 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
259 if ( rc != LDAP_SUCCESS ) {
264 * do the add if 1 && (2 || 3)
265 * 1) there is an add function implemented in this backend;
266 * 2) this backend is master for what it holds;
267 * 3) it's a replica and the dn supplied is the updatedn.
270 /* do the update here */
271 int repl_user = be_isupdate(be, &op->o_ndn );
272 #ifndef SLAPD_MULTIMASTER
273 if ( !be->be_update_ndn.bv_len || repl_user )
276 int update = be->be_update_ndn.bv_len;
277 char textbuf[SLAP_TEXT_BUFLEN];
278 size_t textlen = sizeof textbuf;
280 rc = slap_modlist2mods( modlist, update, &mods, &text,
283 if( rc != LDAP_SUCCESS ) {
284 send_ldap_result( conn, op, rc,
285 NULL, text, NULL, NULL );
289 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
290 global_lastmod == ON)) && !repl_user )
292 Modifications **modstail;
293 for( modstail = &mods;
295 modstail = &(*modstail)->sml_next )
297 assert( (*modstail)->sml_op == LDAP_MOD_ADD );
298 assert( (*modstail)->sml_desc != NULL );
300 rc = slap_mods_opattrs( op, mods, modstail, &text,
302 if( rc != LDAP_SUCCESS ) {
303 send_ldap_result( conn, op, rc,
304 NULL, text, NULL, NULL );
309 rc = slap_mods2entry( mods, &e, &text );
310 if( rc != LDAP_SUCCESS ) {
311 send_ldap_result( conn, op, rc,
312 NULL, text, NULL, NULL );
316 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
317 #ifdef SLAPD_MULTIMASTER
321 replog( be, op, &e->e_name, &e->e_nname, e );
323 be_entry_release_w( be, conn, op, e );
327 #ifndef SLAPD_MULTIMASTER
329 struct berval **defref = be->be_update_refs
330 ? be->be_update_refs : default_referral;
331 struct berval **ref = referral_rewrite( defref,
332 NULL, e->e_dn, LDAP_SCOPE_DEFAULT );
334 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
335 ref ? ref : defref, NULL );
342 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
343 "do_add: conn %d no backend support\n", conn->c_connid ));
345 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
347 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
348 NULL, "operation not supported within namingContext", NULL, NULL );
354 if( modlist != NULL ) {
355 slap_modlist_free( modlist );
358 slap_mods_free( mods );
367 static int slap_mods2entry(
372 Attribute **tail = &(*e)->e_attrs;
373 assert( *tail == NULL );
375 for( ; mods != NULL; mods = mods->sml_next ) {
378 assert( mods->sml_op == LDAP_MOD_ADD );
379 assert( mods->sml_desc != NULL );
381 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
384 #define SLURPD_FRIENDLY
385 #ifdef SLURPD_FRIENDLY
388 for( i=0; attr->a_vals[i]; i++ ) {
391 for( j=0; mods->sml_bvalues[j]; j++ ) {
396 attr->a_vals = ch_realloc( attr->a_vals,
397 sizeof( struct berval * ) * (i+j) );
399 /* should check for duplicates */
400 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
401 sizeof( struct berval * ) * j );
403 /* trim the mods array */
404 ch_free( mods->sml_bvalues );
405 mods->sml_bvalues = NULL;
409 *text = "attribute provided more than once";
410 return LDAP_TYPE_OR_VALUE_EXISTS;
414 attr = ch_calloc( 1, sizeof(Attribute) );
416 /* move ad to attr structure */
417 attr->a_desc = mods->sml_desc;
418 mods->sml_desc = NULL;
420 /* move values to attr structure */
421 /* should check for duplicates */
422 attr->a_vals = mods->sml_bvalues;
423 mods->sml_bvalues = NULL;
426 tail = &attr->a_next;