3 * Copyright 1998-1999 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 );
57 if( op->o_bind_in_progress ) {
58 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
59 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
60 "SASL bind in progress", NULL, NULL );
61 return LDAP_SASL_BIND_IN_PROGRESS;
65 * Parse the add request. It looks like this:
67 * AddRequest := [APPLICATION 14] SEQUENCE {
68 * name DistinguishedName,
69 * attrs SEQUENCE OF SEQUENCE {
71 * values SET OF AttributeValue
77 if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
78 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
79 send_ldap_disconnect( conn, op,
80 LDAP_PROTOCOL_ERROR, "decoding error" );
84 ndn = ch_strdup( dn );
86 if ( dn_normalize( ndn ) == NULL ) {
87 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
88 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
89 "invalid DN", NULL, NULL );
92 return LDAP_INVALID_DN_SYNTAX;
95 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
102 Debug( LDAP_DEBUG_ARGS, " do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
105 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
106 tag = ber_next_element( ber, &len, last ) ) {
107 #ifdef SLAPD_SCHEMA_NOT_COMPAT
108 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
111 LDAPModList *mod = &tmpmod;
113 mod->ml_op = LDAP_MOD_ADD;
116 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
118 if ( rc == LBER_ERROR ) {
119 send_ldap_disconnect( conn, op,
120 LDAP_PROTOCOL_ERROR, "decoding error" );
122 #ifdef SLAPD_SCHEMA_NOT_COMPAT
128 if ( mod->ml_bvalues == NULL ) {
129 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
130 mod->ml_type, 0, 0 );
131 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
132 NULL, "no values for type", NULL, NULL );
133 free( mod->ml_type );
134 #ifdef SLAPD_SCHEMA_NOT_COMPAT
140 #ifdef SLAPD_SCHEMA_NOT_COMPAT
141 (*modtail)->ml_next = mod;
142 modtail = &mod->ml_next;
144 attr_merge( e, mod->ml_type, mod->ml_bvalues );
146 free( mod->ml_type );
147 ber_bvecfree( mod->ml_bvalues );
151 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
152 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
153 send_ldap_disconnect( conn, op,
154 LDAP_PROTOCOL_ERROR, "decoding error" );
159 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
160 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
164 #ifdef SLAPD_SCHEMA_NOT_COMPAT
165 if ( modlist == NULL )
167 if ( e->e_attrs == NULL )
170 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
171 NULL, "No attributes provided", NULL, NULL );
175 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
176 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
179 * We could be serving multiple database backends. Select the
180 * appropriate one, or send a referral to our "referral server"
181 * if we don't hold it.
183 be = select_backend( e->e_ndn );
185 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
186 NULL, NULL, default_referral, NULL );
190 /* make sure this backend recongizes critical controls */
191 rc = backend_check_controls( be, conn, op ) ;
193 if( rc != LDAP_SUCCESS ) {
194 send_ldap_result( conn, op, rc,
195 NULL, NULL, NULL, NULL );
199 if ( global_readonly || be->be_readonly ) {
200 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
202 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
203 NULL, "database is read-only", NULL, NULL );
208 * do the add if 1 && (2 || 3)
209 * 1) there is an add function implemented in this backend;
210 * 2) this backend is master for what it holds;
211 * 3) it's a replica and the dn supplied is the updatedn.
214 /* do the update here */
215 #ifdef SLAPD_MULTIMASTER
216 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
217 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
218 strcmp( be->be_update_ndn, op->o_ndn )) )
220 if ( be->be_update_ndn == NULL ||
221 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
224 int update = be->be_update_ndn != NULL;
226 #ifdef SLAPD_SCHEMA_NOT_COMPAT
227 rc = slap_modlist2mods( modlist, update, &mods, &text );
228 if( rc != LDAP_SUCCESS ) {
229 send_ldap_result( conn, op, rc,
230 NULL, text, NULL, NULL );
235 #ifndef SLAPD_MULTIMASTER
236 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
237 global_lastmod == ON)) && !update )
240 #ifdef SLAPD_SCHEMA_NOT_COMPAT
241 rc = slap_mods_opattrs( op, &mods, &text );
243 char *text = "no-user-modification attribute type";
244 rc = add_created_attrs( op, e );
246 if( rc != LDAP_SUCCESS ) {
247 send_ldap_result( conn, op, rc,
254 #ifdef SLAPD_SCHEMA_NOT_COMPAT
255 rc = slap_mods2entry( mods, &e, &text );
256 if( rc != LDAP_SUCCESS ) {
257 send_ldap_result( conn, op, rc,
258 NULL, text, NULL, NULL );
263 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
264 #ifdef SLAPD_MULTIMASTER
265 if (be->be_update_ndn == NULL ||
266 strcmp( be->be_update_ndn, op->o_ndn ))
269 replog( be, op, e->e_dn, e );
271 be_entry_release_w( be, e );
275 #ifndef SLAPD_MULTIMASTER
277 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
278 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
282 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
283 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
284 NULL, "Function not implemented", NULL, NULL );
288 #ifdef SLAPD_SCHEMA_NOT_COMPAT
289 if( modlist != NULL ) {
290 slap_modlist_free( modlist );
293 slap_mods_free( mods );
303 #ifdef SLAPD_SCHEMA_NOT_COMPAT
304 static int slap_mods2entry(
309 Attribute **tail = &(*e)->e_attrs;
310 assert( *tail == NULL );
312 for( ; mods != NULL; mods = mods->sml_next ) {
315 assert( mods->sml_op == LDAP_MOD_ADD );
317 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
320 *text = "Attribute provided more than once";
321 return LDAP_OPERATIONS_ERROR;
324 attr = ch_calloc( 1, sizeof(Attribute) );
326 /* should check for duplicates */
327 attr->a_vals = mods->sml_bvalues;
328 mods->sml_bvalues = NULL;
331 tail = &attr->a_next;
339 add_created_attrs( Operation *op, Entry *e )
343 struct berval *bvals[2];
348 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
353 /* return error on any attempts by the user to add these attrs */
354 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
355 #ifdef SLAPD_SCHEMA_NOT_COMPAT
356 if ( is_at_no_user_mod( a->a_desc.ad_type ))
358 if ( oc_check_op_no_usermod_attr( a->a_type ) )
361 return LDAP_CONSTRAINT_VIOLATION;
365 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
366 bv.bv_val = "<anonymous>";
367 bv.bv_len = sizeof("<anonymous>")-1;
370 bv.bv_val = op->o_dn;
371 bv.bv_len = strlen( bv.bv_val );
373 attr_merge( e, "creatorsname", bvals );
374 attr_merge( e, "modifiersname", bvals );
376 currenttime = slap_get_time();
377 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
378 ltm = gmtime( ¤ttime );
379 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
380 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
383 bv.bv_len = strlen( bv.bv_val );
384 attr_merge( e, "createtimestamp", bvals );
385 attr_merge( e, "modifytimestamp", bvals );