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 static int add_created_attrs(Operation *op, Entry *e);
32 do_add( Connection *conn, Operation *op )
34 BerElement *ber = op->o_ber;
35 char *dn, *ndn, *last;
40 int rc = LDAP_SUCCESS;
42 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
44 if( op->o_bind_in_progress ) {
45 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
46 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
47 "SASL bind in progress", NULL, NULL );
48 return LDAP_SASL_BIND_IN_PROGRESS;
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) );
88 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 ) ) {
97 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
98 send_ldap_disconnect( conn, op,
99 LDAP_PROTOCOL_ERROR, "decoding error" );
104 if ( vals == NULL ) {
105 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
107 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
108 NULL, "no values for type", NULL, NULL );
111 return LDAP_PROTOCOL_ERROR;
114 attr_merge( e, type, vals );
117 ber_bvecfree( vals );
120 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
122 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
123 send_ldap_disconnect( conn, op,
124 LDAP_PROTOCOL_ERROR, "decoding error" );
128 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
130 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
134 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
135 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
138 * We could be serving multiple database backends. Select the
139 * appropriate one, or send a referral to our "referral server"
140 * if we don't hold it.
142 be = select_backend( e->e_ndn );
145 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
146 NULL, default_referral, NULL );
150 /* make sure this backend recongizes critical controls */
151 rc = backend_check_controls( be, conn, op ) ;
153 if( rc != LDAP_SUCCESS ) {
154 send_ldap_result( conn, op, rc,
155 NULL, NULL, NULL, NULL );
160 if ( global_readonly || be->be_readonly ) {
161 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
164 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
165 NULL, "database is read-only", NULL, NULL );
166 return LDAP_UNWILLING_TO_PERFORM;
170 * do the add if 1 && (2 || 3)
171 * 1) there is an add function implemented in this backend;
172 * 2) this backend is master for what it holds;
173 * 3) it's a replica and the dn supplied is the updatedn.
176 /* do the update here */
177 #ifdef SLAPD_MULTIMASTER
178 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
179 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
180 strcmp( be->be_update_ndn, op->o_ndn )) )
182 if ( be->be_update_ndn == NULL ||
183 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
186 #ifndef SLAPD_MULTIMASTER
187 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
188 global_lastmod == ON)) && be->be_update_ndn == NULL )
191 rc = add_created_attrs( op, e );
193 if( rc != LDAP_SUCCESS ) {
195 send_ldap_result( conn, op, rc,
196 NULL, "no-user-modification attribute type",
202 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
203 #ifdef SLAPD_MULTIMASTER
204 if (be->be_update_ndn == NULL ||
205 strcmp( be->be_update_ndn, op->o_ndn ))
208 replog( be, op, e->e_dn, e );
210 be_entry_release_w( be, e );
213 #ifndef SLAPD_MULTIMASTER
216 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
217 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
221 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
223 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
224 NULL, "Function not implemented", NULL, NULL );
231 add_created_attrs( Operation *op, Entry *e )
235 struct berval *bvals[2];
240 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
245 /* return error on any attempts by the user to add these attrs */
246 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
247 #ifdef SLAPD_SCHEMA_NOT_COMPAT
248 if ( is_at_no_user_mod( a->a_desc.ad_type ))
250 if ( oc_check_op_no_usermod_attr( a->a_type ) )
253 return LDAP_CONSTRAINT_VIOLATION;
257 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
258 bv.bv_val = "<anonymous>";
259 bv.bv_len = strlen( bv.bv_val );
261 bv.bv_val = op->o_dn;
262 bv.bv_len = strlen( bv.bv_val );
264 attr_merge( e, "creatorsname", bvals );
265 attr_merge( e, "modifiersname", bvals );
267 currenttime = slap_get_time();
268 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
269 ltm = gmtime( ¤ttime );
270 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
271 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
274 bv.bv_len = strlen( bv.bv_val );
275 attr_merge( e, "createtimestamp", bvals );
276 attr_merge( e, "modifytimestamp", bvals );