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;
49 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
52 * Parse the add request. It looks like this:
54 * AddRequest := [APPLICATION 14] SEQUENCE {
55 * name DistinguishedName,
56 * attrs SEQUENCE OF SEQUENCE {
58 * values SET OF AttributeValue
64 if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
65 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
66 send_ldap_disconnect( conn, op,
67 LDAP_PROTOCOL_ERROR, "decoding error" );
71 ndn = ch_strdup( dn );
73 if ( dn_normalize( ndn ) == NULL ) {
74 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
75 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
76 "invalid DN", NULL, NULL );
79 return LDAP_INVALID_DN_SYNTAX;
82 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
89 Debug( LDAP_DEBUG_ARGS, "do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
92 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
93 tag = ber_next_element( ber, &len, last ) )
95 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
96 mod->ml_op = LDAP_MOD_ADD;
99 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
101 if ( rc == LBER_ERROR ) {
102 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
103 send_ldap_disconnect( conn, op,
104 LDAP_PROTOCOL_ERROR, "decoding error" );
110 if ( mod->ml_bvalues == NULL ) {
111 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
112 mod->ml_type, 0, 0 );
113 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
114 NULL, "no values for attribute type", NULL, NULL );
115 free( mod->ml_type );
121 modtail = &mod->ml_next;
124 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
125 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
126 send_ldap_disconnect( conn, op,
127 LDAP_PROTOCOL_ERROR, "decoding error" );
132 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
133 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
137 if ( modlist == NULL ) {
138 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
139 NULL, "no attributes provided", NULL, NULL );
143 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
144 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
146 if( e->e_ndn == NULL || *e->e_ndn == '\0' ) {
147 /* protocolError may be a more appropriate error */
148 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
149 NULL, "root DSE exists", NULL, NULL );
153 manageDSAit = get_manageDSAit( op ) &&
154 is_entry_referral( e );
157 * We could be serving multiple database backends. Select the
158 * appropriate one, or send a referral to our "referral server"
159 * if we don't hold it.
161 be = select_backend( e->e_ndn, manageDSAit );
163 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
164 NULL, NULL, default_referral, NULL );
168 /* check restrictions */
169 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
170 if( rc != LDAP_SUCCESS ) {
171 send_ldap_result( conn, op, rc,
172 NULL, text, NULL, NULL );
176 /* check for referrals */
177 rc = backend_check_referrals( be, conn, op, e->e_dn, e->e_ndn );
178 if ( rc != LDAP_SUCCESS ) {
183 * do the add if 1 && (2 || 3)
184 * 1) there is an add function implemented in this backend;
185 * 2) this backend is master for what it holds;
186 * 3) it's a replica and the dn supplied is the updatedn.
189 /* do the update here */
190 #ifdef SLAPD_MULTIMASTER
191 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
192 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
193 strcmp( be->be_update_ndn, op->o_ndn )) )
195 if ( be->be_update_ndn == NULL ||
196 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
199 int update = be->be_update_ndn != NULL;
201 rc = slap_modlist2mods( modlist, update, &mods, &text );
202 if( rc != LDAP_SUCCESS ) {
203 send_ldap_result( conn, op, rc,
204 NULL, text, NULL, NULL );
208 #ifndef SLAPD_MULTIMASTER
209 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
210 global_lastmod == ON)) && !update )
213 Modifications **modstail;
214 for( modstail = &mods;
216 modstail = &(*modstail)->sml_next )
218 assert( (*modstail)->sml_op == LDAP_MOD_ADD );
219 assert( (*modstail)->sml_desc != NULL );
221 rc = slap_mods_opattrs( op, modstail, &text );
222 if( rc != LDAP_SUCCESS ) {
223 send_ldap_result( conn, op, rc,
224 NULL, text, NULL, NULL );
229 rc = slap_mods2entry( mods, &e, &text );
230 if( rc != LDAP_SUCCESS ) {
231 send_ldap_result( conn, op, rc,
232 NULL, text, NULL, NULL );
236 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
237 #ifdef SLAPD_MULTIMASTER
238 if (be->be_update_ndn == NULL ||
239 strcmp( be->be_update_ndn, op->o_ndn ))
242 replog( be, op, e->e_dn, e );
244 be_entry_release_w( be, conn, op, e );
248 #ifndef SLAPD_MULTIMASTER
250 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
251 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
255 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
256 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
257 NULL, "operation not supported within namingContext", NULL, NULL );
261 if( modlist != NULL ) {
262 slap_modlist_free( modlist );
265 slap_mods_free( mods );
274 static int slap_mods2entry(
279 Attribute **tail = &(*e)->e_attrs;
280 assert( *tail == NULL );
282 for( ; mods != NULL; mods = mods->sml_next ) {
285 assert( mods->sml_op == LDAP_MOD_ADD );
286 assert( mods->sml_desc != NULL );
288 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
291 #define SLURPD_FRIENDLY
292 #ifdef SLURPD_FRIENDLY
295 for( i=0; attr->a_vals[i]; i++ ) {
298 for( j=0; mods->sml_bvalues[j]; j++ ) {
303 attr->a_vals = ch_realloc( attr->a_vals,
304 sizeof( struct berval * ) * (i+j) );
306 /* should check for duplicates */
307 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
308 sizeof( struct berval * ) * j );
310 /* trim the mods array */
311 ch_free( mods->sml_bvalues );
312 mods->sml_bvalues = NULL;
316 *text = "attribute provided more than once";
317 return LDAP_TYPE_OR_VALUE_EXISTS;
321 attr = ch_calloc( 1, sizeof(Attribute) );
323 /* move ad to attr structure */
324 attr->a_desc = mods->sml_desc;
325 mods->sml_desc = NULL;
327 /* move values to attr structure */
328 /* should check for duplicates */
329 attr->a_vals = mods->sml_bvalues;
330 mods->sml_bvalues = NULL;
333 tail = &attr->a_next;