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 ) ) {
100 #ifdef SLAPD_SCHEMA_NOT_COMPAT
101 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
104 LDAPModList *mod = &tmpmod;
106 mod->ml_op = LDAP_MOD_ADD;
109 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
111 if ( rc == LBER_ERROR ) {
112 send_ldap_disconnect( conn, op,
113 LDAP_PROTOCOL_ERROR, "decoding error" );
115 #ifdef SLAPD_SCHEMA_NOT_COMPAT
121 if ( mod->ml_bvalues == NULL ) {
122 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
123 mod->ml_type, 0, 0 );
124 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
125 NULL, "no values for attribute type", NULL, NULL );
126 free( mod->ml_type );
127 #ifdef SLAPD_SCHEMA_NOT_COMPAT
133 #ifdef SLAPD_SCHEMA_NOT_COMPAT
134 (*modtail)->ml_next = mod;
135 modtail = &mod->ml_next;
137 attr_merge( e, mod->ml_type, mod->ml_bvalues );
139 free( mod->ml_type );
140 ber_bvecfree( mod->ml_bvalues );
144 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
145 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
146 send_ldap_disconnect( conn, op,
147 LDAP_PROTOCOL_ERROR, "decoding error" );
152 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
153 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
157 #ifdef SLAPD_SCHEMA_NOT_COMPAT
158 if ( modlist == NULL )
160 if ( e->e_attrs == NULL )
163 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
164 NULL, "no attributes provided", NULL, NULL );
168 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
169 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
172 * We could be serving multiple database backends. Select the
173 * appropriate one, or send a referral to our "referral server"
174 * if we don't hold it.
176 be = select_backend( e->e_ndn );
178 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
179 NULL, NULL, default_referral, NULL );
183 /* make sure this backend recongizes critical controls */
184 rc = backend_check_controls( be, conn, op, &text ) ;
186 if( rc != LDAP_SUCCESS ) {
187 send_ldap_result( conn, op, rc,
188 NULL, text, NULL, NULL );
192 if ( global_readonly || be->be_readonly ) {
193 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
195 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
196 NULL, "directory is read-only", NULL, NULL );
201 * do the add if 1 && (2 || 3)
202 * 1) there is an add function implemented in this backend;
203 * 2) this backend is master for what it holds;
204 * 3) it's a replica and the dn supplied is the updatedn.
207 /* do the update here */
208 #ifdef SLAPD_MULTIMASTER
209 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
210 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
211 strcmp( be->be_update_ndn, op->o_ndn )) )
213 if ( be->be_update_ndn == NULL ||
214 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
217 int update = be->be_update_ndn != NULL;
219 #ifdef SLAPD_SCHEMA_NOT_COMPAT
220 rc = slap_modlist2mods( modlist, update, &mods, &text );
221 if( rc != LDAP_SUCCESS ) {
222 send_ldap_result( conn, op, rc,
223 NULL, text, NULL, NULL );
228 #ifndef SLAPD_MULTIMASTER
229 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
230 global_lastmod == ON)) && !update )
233 #ifdef SLAPD_SCHEMA_NOT_COMPAT
234 rc = slap_mods_opattrs( op, &mods, &text );
236 char *text = "no-user-modification attribute type";
237 rc = add_created_attrs( op, e );
239 if( rc != LDAP_SUCCESS ) {
240 send_ldap_result( conn, op, rc,
241 NULL, text, NULL, NULL );
246 #ifdef SLAPD_SCHEMA_NOT_COMPAT
247 rc = slap_mods2entry( mods, &e, &text );
248 if( rc != LDAP_SUCCESS ) {
249 send_ldap_result( conn, op, rc,
250 NULL, text, NULL, NULL );
255 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
256 #ifdef SLAPD_MULTIMASTER
257 if (be->be_update_ndn == NULL ||
258 strcmp( be->be_update_ndn, op->o_ndn ))
261 replog( be, op, e->e_dn, e );
263 be_entry_release_w( be, e );
267 #ifndef SLAPD_MULTIMASTER
269 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
270 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
274 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
275 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
276 NULL, "read function not implemented", NULL, NULL );
280 #ifdef SLAPD_SCHEMA_NOT_COMPAT
281 if( modlist != NULL ) {
282 slap_modlist_free( modlist );
285 slap_mods_free( mods );
295 #ifdef SLAPD_SCHEMA_NOT_COMPAT
296 static int slap_mods2entry(
301 Attribute **tail = &(*e)->e_attrs;
302 assert( *tail == NULL );
304 for( ; mods != NULL; mods = mods->sml_next ) {
307 assert( mods->sml_op == LDAP_MOD_ADD );
309 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
312 *text = "Attribute provided more than once";
313 return LDAP_OPERATIONS_ERROR;
316 attr = ch_calloc( 1, sizeof(Attribute) );
318 /* should check for duplicates */
319 attr->a_vals = mods->sml_bvalues;
320 mods->sml_bvalues = NULL;
323 tail = &attr->a_next;
331 add_created_attrs( Operation *op, Entry *e )
335 struct berval *bvals[2];
340 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
345 /* return error on any attempts by the user to add these attrs */
346 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
347 #ifdef SLAPD_SCHEMA_NOT_COMPAT
348 if ( is_at_no_user_mod( a->a_desc.ad_type ))
350 if ( oc_check_op_no_usermod_attr( a->a_type ) )
353 return LDAP_CONSTRAINT_VIOLATION;
357 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
358 bv.bv_val = "<anonymous>";
359 bv.bv_len = sizeof("<anonymous>")-1;
362 bv.bv_val = op->o_dn;
363 bv.bv_len = strlen( bv.bv_val );
365 attr_merge( e, "creatorsname", bvals );
366 attr_merge( e, "modifiersname", bvals );
368 currenttime = slap_get_time();
369 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
370 ltm = gmtime( ¤ttime );
371 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
372 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
375 bv.bv_len = strlen( bv.bv_val );
376 attr_merge( e, "createtimestamp", bvals );
377 attr_merge( e, "modifytimestamp", bvals );