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) );
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=%d 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 );
151 * do the add if 1 && (2 || 3)
152 * 1) there is an add function implemented in this backend;
153 * 2) this backend is master for what it holds;
154 * 3) it's a replica and the dn supplied is the updatedn.
157 /* do the update here */
158 #ifdef SLAPD_MULTIMASTER
159 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
160 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
161 strcmp( be->be_update_ndn, op->o_ndn )) )
163 if ( be->be_update_ndn == NULL ||
164 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
167 #ifndef SLAPD_MULTIMASTER
168 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
169 global_lastmod == ON)) && be->be_update_ndn == NULL )
172 rc = add_created_attrs( op, e );
174 if( rc != LDAP_SUCCESS ) {
176 send_ldap_result( conn, op, rc,
177 NULL, "no-user-modification attribute type",
183 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
184 #ifdef SLAPD_MULTIMASTER
185 if (be->be_update_ndn == NULL ||
186 strcmp( be->be_update_ndn, op->o_ndn ))
189 replog( be, op, e->e_dn, e );
191 be_entry_release_w( be, e );
194 #ifndef SLAPD_MULTIMASTER
197 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
198 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
202 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
204 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
205 NULL, "Function not implemented", NULL, NULL );
212 add_created_attrs( Operation *op, Entry *e )
216 struct berval *bvals[2];
221 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
226 /* return error on any attempts by the user to add these attrs */
227 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
228 if ( oc_check_no_usermod_attr( a->a_type ) ) {
229 return LDAP_CONSTRAINT_VIOLATION;
233 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
234 bv.bv_val = "<anonymous>";
235 bv.bv_len = strlen( bv.bv_val );
237 bv.bv_val = op->o_dn;
238 bv.bv_len = strlen( bv.bv_val );
240 attr_merge( e, "creatorsname", bvals );
242 currenttime = slap_get_time();
243 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
244 #ifndef LDAP_LOCALTIME
245 ltm = gmtime( ¤ttime );
246 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
248 ltm = localtime( ¤ttime );
249 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
251 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
254 bv.bv_len = strlen( bv.bv_val );
255 attr_merge( e, "createtimestamp", bvals );