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 );
158 if ( global_readonly || be->be_readonly ) {
159 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
162 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
163 NULL, "database is read-only", NULL, NULL );
164 return LDAP_UNWILLING_TO_PERFORM;
168 * do the add if 1 && (2 || 3)
169 * 1) there is an add function implemented in this backend;
170 * 2) this backend is master for what it holds;
171 * 3) it's a replica and the dn supplied is the updatedn.
174 /* do the update here */
175 #ifdef SLAPD_MULTIMASTER
176 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
177 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
178 strcmp( be->be_update_ndn, op->o_ndn )) )
180 if ( be->be_update_ndn == NULL ||
181 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
184 #ifndef SLAPD_MULTIMASTER
185 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
186 global_lastmod == ON)) && be->be_update_ndn == NULL )
189 rc = add_created_attrs( op, e );
191 if( rc != LDAP_SUCCESS ) {
193 send_ldap_result( conn, op, rc,
194 NULL, "no-user-modification attribute type",
200 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
201 #ifdef SLAPD_MULTIMASTER
202 if (be->be_update_ndn == NULL ||
203 strcmp( be->be_update_ndn, op->o_ndn ))
206 replog( be, op, e->e_dn, e );
208 be_entry_release_w( be, e );
211 #ifndef SLAPD_MULTIMASTER
214 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
215 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
219 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
221 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
222 NULL, "Function not implemented", NULL, NULL );
229 add_created_attrs( Operation *op, Entry *e )
233 struct berval *bvals[2];
238 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
243 /* return error on any attempts by the user to add these attrs */
244 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
245 if ( oc_check_op_no_usermod_attr( a->a_type ) ) {
246 return LDAP_CONSTRAINT_VIOLATION;
250 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
251 bv.bv_val = "<anonymous>";
252 bv.bv_len = strlen( bv.bv_val );
254 bv.bv_val = op->o_dn;
255 bv.bv_len = strlen( bv.bv_val );
257 attr_merge( e, "creatorsname", bvals );
258 attr_merge( e, "modifiersname", bvals );
260 currenttime = slap_get_time();
261 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
262 ltm = gmtime( ¤ttime );
263 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
264 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
267 bv.bv_len = strlen( bv.bv_val );
268 attr_merge( e, "createtimestamp", bvals );
269 attr_merge( e, "modifytimestamp", bvals );