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.
22 #include <ac/string.h>
24 #include <ac/socket.h>
29 #ifdef SLAPD_SCHEMA_NOT_COMPAT
30 static int slap_mods2entry(
35 static int add_created_attrs(Operation *op, Entry *e);
39 do_add( Connection *conn, Operation *op )
41 BerElement *ber = op->o_ber;
42 char *dn, *ndn, *last;
47 #ifdef SLAPD_SCHEMA_NOT_COMPAT
48 LDAPModList *modlist = NULL;
49 LDAPModList **modtail = &modlist;
50 Modifications *mods = NULL;
53 int rc = LDAP_SUCCESS;
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, "{a", /*}*/ &dn ) == LBER_ERROR ) {
71 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
72 send_ldap_disconnect( conn, op,
73 LDAP_PROTOCOL_ERROR, "decoding error" );
77 ndn = ch_strdup( dn );
79 if ( dn_normalize( ndn ) == NULL ) {
80 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
81 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
82 "invalid DN", NULL, NULL );
85 return LDAP_INVALID_DN_SYNTAX;
88 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
95 Debug( LDAP_DEBUG_ARGS, " do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
98 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
99 tag = ber_next_element( ber, &len, last ) )
101 #ifdef SLAPD_SCHEMA_NOT_COMPAT
102 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
105 LDAPModList *mod = &tmpmod;
107 mod->ml_op = LDAP_MOD_ADD;
110 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
112 if ( rc == LBER_ERROR ) {
113 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
114 send_ldap_disconnect( conn, op,
115 LDAP_PROTOCOL_ERROR, "decoding error" );
117 #ifdef SLAPD_SCHEMA_NOT_COMPAT
123 if ( mod->ml_bvalues == NULL ) {
124 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
125 mod->ml_type, 0, 0 );
126 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
127 NULL, "no values for attribute type", NULL, NULL );
128 free( mod->ml_type );
129 #ifdef SLAPD_SCHEMA_NOT_COMPAT
135 #ifdef SLAPD_SCHEMA_NOT_COMPAT
137 modtail = &mod->ml_next;
139 attr_merge( e, mod->ml_type, mod->ml_bvalues );
141 free( mod->ml_type );
142 ber_bvecfree( mod->ml_bvalues );
146 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
147 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
148 send_ldap_disconnect( conn, op,
149 LDAP_PROTOCOL_ERROR, "decoding error" );
154 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
155 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
159 #ifdef SLAPD_SCHEMA_NOT_COMPAT
160 if ( modlist == NULL )
162 if ( e->e_attrs == NULL )
165 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
166 NULL, "no attributes provided", NULL, NULL );
170 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
171 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
174 * We could be serving multiple database backends. Select the
175 * appropriate one, or send a referral to our "referral server"
176 * if we don't hold it.
178 be = select_backend( e->e_ndn );
180 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
181 NULL, NULL, default_referral, NULL );
185 /* make sure this backend recongizes critical controls */
186 rc = backend_check_controls( be, conn, op, &text ) ;
188 if( rc != LDAP_SUCCESS ) {
189 send_ldap_result( conn, op, rc,
190 NULL, text, NULL, NULL );
194 if ( global_readonly || be->be_readonly ) {
195 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
197 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
198 NULL, "directory is read-only", NULL, NULL );
203 * do the add if 1 && (2 || 3)
204 * 1) there is an add function implemented in this backend;
205 * 2) this backend is master for what it holds;
206 * 3) it's a replica and the dn supplied is the updatedn.
209 /* do the update here */
210 #ifdef SLAPD_MULTIMASTER
211 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
212 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
213 strcmp( be->be_update_ndn, op->o_ndn )) )
215 if ( be->be_update_ndn == NULL ||
216 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
219 int update = be->be_update_ndn != NULL;
221 #ifdef SLAPD_SCHEMA_NOT_COMPAT
222 rc = slap_modlist2mods( modlist, update, &mods, &text );
223 if( rc != LDAP_SUCCESS ) {
224 send_ldap_result( conn, op, rc,
225 NULL, text, NULL, NULL );
230 #ifndef SLAPD_MULTIMASTER
231 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
232 global_lastmod == ON)) && !update )
235 #ifdef SLAPD_SCHEMA_NOT_COMPAT
236 Modifications **modstail;
237 for( modstail = &mods;
239 modstail = &(*modstail)->sml_next )
241 assert( (*modstail)->sml_op == LDAP_MOD_ADD );
242 assert( (*modstail)->sml_desc != NULL );
244 rc = slap_mods_opattrs( op, modstail, &text );
246 char *text = "no-user-modification attribute type";
247 rc = add_created_attrs( op, e );
249 if( rc != LDAP_SUCCESS ) {
250 send_ldap_result( conn, op, rc,
251 NULL, text, NULL, NULL );
256 #ifdef SLAPD_SCHEMA_NOT_COMPAT
257 rc = slap_mods2entry( mods, &e, &text );
258 if( rc != LDAP_SUCCESS ) {
259 send_ldap_result( conn, op, rc,
260 NULL, text, NULL, NULL );
265 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
266 #ifdef SLAPD_MULTIMASTER
267 if (be->be_update_ndn == NULL ||
268 strcmp( be->be_update_ndn, op->o_ndn ))
271 replog( be, op, e->e_dn, e );
273 be_entry_release_w( be, e );
277 #ifndef SLAPD_MULTIMASTER
279 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
280 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
284 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
285 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
286 NULL, "operation not supported within namingContext", NULL, NULL );
290 #ifdef SLAPD_SCHEMA_NOT_COMPAT
291 if( modlist != NULL ) {
292 slap_modlist_free( modlist );
295 slap_mods_free( mods );
305 #ifdef SLAPD_SCHEMA_NOT_COMPAT
306 static int slap_mods2entry(
311 Attribute **tail = &(*e)->e_attrs;
312 assert( *tail == NULL );
314 for( ; mods != NULL; mods = mods->sml_next ) {
317 assert( mods->sml_op == LDAP_MOD_ADD );
318 assert( mods->sml_desc != NULL );
320 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
323 *text = "attribute provided more than once";
324 return LDAP_OPERATIONS_ERROR;
327 attr = ch_calloc( 1, sizeof(Attribute) );
329 /* move ad to attr structure */
330 attr->a_desc = mods->sml_desc;
331 mods->sml_desc = NULL;
333 /* move values to attr structure */
334 /* should check for duplicates */
335 attr->a_vals = mods->sml_bvalues;
336 mods->sml_bvalues = NULL;
339 tail = &attr->a_next;
347 add_created_attrs( Operation *op, Entry *e )
351 struct berval *bvals[2];
356 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
361 /* return error on any attempts by the user to add these attrs */
362 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
363 if ( oc_check_op_no_usermod_attr( a->a_type ) ) {
364 return LDAP_CONSTRAINT_VIOLATION;
368 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
369 bv.bv_val = SLAPD_ANONYMOUS;
370 bv.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
373 bv.bv_val = op->o_dn;
374 bv.bv_len = strlen( bv.bv_val );
376 attr_merge( e, "creatorsname", bvals );
377 attr_merge( e, "modifiersname", bvals );
379 currenttime = slap_get_time();
380 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
381 ltm = gmtime( ¤ttime );
382 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
383 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
386 bv.bv_len = strlen( bv.bv_val );
387 attr_merge( e, "createtimestamp", bvals );
388 attr_merge( e, "modifytimestamp", bvals );