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) );
89 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, rc = LDAP_PROTOCOL_ERROR,
108 NULL, "no values for type", NULL, NULL );
113 attr_merge( e, type, vals );
116 ber_bvecfree( vals );
119 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
120 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
121 send_ldap_disconnect( conn, op,
122 LDAP_PROTOCOL_ERROR, "decoding error" );
127 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
128 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
132 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
133 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
136 * We could be serving multiple database backends. Select the
137 * appropriate one, or send a referral to our "referral server"
138 * if we don't hold it.
140 be = select_backend( e->e_ndn );
142 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL,
143 NULL, default_referral, NULL );
147 /* make sure this backend recongizes critical controls */
148 rc = backend_check_controls( be, conn, op ) ;
150 if( rc != LDAP_SUCCESS ) {
151 send_ldap_result( conn, op, rc,
152 NULL, NULL, NULL, NULL );
156 if ( global_readonly || be->be_readonly ) {
157 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
159 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
160 NULL, "database is read-only", NULL, NULL );
165 * do the add if 1 && (2 || 3)
166 * 1) there is an add function implemented in this backend;
167 * 2) this backend is master for what it holds;
168 * 3) it's a replica and the dn supplied is the updatedn.
171 /* do the update here */
172 #ifdef SLAPD_MULTIMASTER
173 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
174 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
175 strcmp( be->be_update_ndn, op->o_ndn )) )
177 if ( be->be_update_ndn == NULL ||
178 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
181 #ifndef SLAPD_MULTIMASTER
182 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
183 global_lastmod == ON)) && be->be_update_ndn == NULL )
186 rc = add_created_attrs( op, e );
188 if( rc != LDAP_SUCCESS ) {
189 send_ldap_result( conn, op, rc,
190 NULL, "no-user-modification attribute type",
196 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
197 #ifdef SLAPD_MULTIMASTER
198 if (be->be_update_ndn == NULL ||
199 strcmp( be->be_update_ndn, op->o_ndn ))
202 replog( be, op, e->e_dn, e );
204 be_entry_release_w( be, e );
208 #ifndef SLAPD_MULTIMASTER
210 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
211 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
215 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
216 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
217 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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
246 if ( is_at_no_user_mod( a->a_desc.ad_type ))
248 if ( oc_check_op_no_usermod_attr( a->a_type ) )
251 return LDAP_CONSTRAINT_VIOLATION;
255 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
256 bv.bv_val = "<anonymous>";
257 bv.bv_len = sizeof("<anonymous>")-1;
260 bv.bv_val = op->o_dn;
261 bv.bv_len = strlen( bv.bv_val );
263 attr_merge( e, "creatorsname", bvals );
264 attr_merge( e, "modifiersname", bvals );
266 currenttime = slap_get_time();
267 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
268 ltm = gmtime( ¤ttime );
269 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
270 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
273 bv.bv_len = strlen( bv.bv_val );
274 attr_merge( e, "createtimestamp", bvals );
275 attr_merge( e, "modifytimestamp", bvals );