2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1995 Regents of the University of Michigan.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that this notice is preserved and that due credit is given
11 * to the University of Michigan at Ann Arbor. The name of the University
12 * may not be used to endorse or promote products derived from this
13 * software without specific prior written permission. This software
14 * is provided ``as is'' without express or implied warranty.
21 #include <ac/string.h>
23 #include <ac/socket.h>
27 static int add_created_attrs(Operation *op, Entry *e);
30 do_add( Connection *conn, Operation *op )
32 BerElement *ber = op->o_ber;
33 char *dn, *ndn, *last;
38 int rc = LDAP_SUCCESS;
40 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
42 if( op->o_bind_in_progress ) {
43 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
44 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
45 "SASL bind in progress", NULL, NULL );
46 return LDAP_SASL_BIND_IN_PROGRESS;
50 * Parse the add request. It looks like this:
52 * AddRequest := [APPLICATION 14] SEQUENCE {
53 * name DistinguishedName,
54 * attrs SEQUENCE OF SEQUENCE {
56 * values SET OF AttributeValue
62 if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
63 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
64 send_ldap_disconnect( conn, op,
65 LDAP_PROTOCOL_ERROR, "decoding error" );
69 ndn = ch_strdup( dn );
71 if ( dn_normalize_case( ndn ) == NULL ) {
72 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
73 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
74 "invalid DN", NULL, NULL );
77 return LDAP_INVALID_DN_SYNTAX;
80 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
86 Debug( LDAP_DEBUG_ARGS, " do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
90 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
91 tag = ber_next_element( ber, &len, last ) ) {
95 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
96 send_ldap_disconnect( conn, op,
97 LDAP_PROTOCOL_ERROR, "decoding error" );
102 if ( vals == NULL ) {
103 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
105 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
106 NULL, "no values for type", NULL, NULL );
109 return LDAP_PROTOCOL_ERROR;
112 attr_merge( e, type, vals );
115 ber_bvecfree( vals );
118 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" );
126 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 );
143 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
144 NULL, default_referral, NULL );
149 * do the add if 1 && (2 || 3)
150 * 1) there is an add function implemented in this backend;
151 * 2) this backend is master for what it holds;
152 * 3) it's a replica and the dn supplied is the updatedn.
155 /* do the update here */
156 #ifdef SLAPD_MULTIMASTER
157 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
158 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
159 strcmp( be->be_update_ndn, op->o_ndn )) )
161 if ( be->be_update_ndn == NULL ||
162 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
165 #ifndef SLAPD_MULTIMASTER
166 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
167 global_lastmod == ON)) && be->be_update_ndn == NULL )
170 rc = add_created_attrs( op, e );
172 if( rc != LDAP_SUCCESS ) {
174 send_ldap_result( conn, op, rc,
175 NULL, "no-user-modification attribute type",
181 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
182 #ifdef SLAPD_MULTIMASTER
183 if (be->be_update_ndn == NULL ||
184 strcmp( be->be_update_ndn, op->o_ndn ))
187 replog( be, op, e->e_dn, e );
189 be_entry_release_w( be, e );
192 #ifndef SLAPD_MULTIMASTER
195 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
196 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
200 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
202 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
203 NULL, "Function not implemented", NULL, NULL );
210 add_created_attrs( Operation *op, Entry *e )
214 struct berval *bvals[2];
219 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
224 /* return error on any attempts by the user to add these attrs */
225 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
226 if ( oc_check_no_usermod_attr( a->a_type ) ) {
227 return LDAP_CONSTRAINT_VIOLATION;
231 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
232 bv.bv_val = "<anonymous>";
233 bv.bv_len = strlen( bv.bv_val );
235 bv.bv_val = op->o_dn;
236 bv.bv_len = strlen( bv.bv_val );
238 attr_merge( e, "creatorsname", bvals );
240 currenttime = slap_get_time();
241 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
242 #ifndef LDAP_LOCALTIME
243 ltm = gmtime( ¤ttime );
244 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
246 ltm = localtime( ¤ttime );
247 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
249 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
252 bv.bv_len = strlen( bv.bv_val );
253 attr_merge( e, "createtimestamp", bvals );