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>
28 static int add_created_attrs(Operation *op, Entry *e);
31 do_add( Connection *conn, Operation *op )
33 BerElement *ber = op->o_ber;
34 char *dn, *ndn, *last;
39 int rc = LDAP_SUCCESS;
41 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
43 if( op->o_bind_in_progress ) {
44 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
45 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
46 "SASL bind in progress", NULL, NULL );
47 return LDAP_SASL_BIND_IN_PROGRESS;
51 * Parse the add request. It looks like this:
53 * AddRequest := [APPLICATION 14] SEQUENCE {
54 * name DistinguishedName,
55 * attrs SEQUENCE OF SEQUENCE {
57 * values SET OF AttributeValue
63 if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
64 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
65 send_ldap_disconnect( conn, op,
66 LDAP_PROTOCOL_ERROR, "decoding error" );
70 if ( dn_normalize( dn ) == NULL ) {
71 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
72 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
73 "invalid DN", NULL, NULL );
75 return LDAP_INVALID_DN_SYNTAX;
78 ndn = ch_strdup( dn );
79 ldap_pvt_str2upper( ndn );
81 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
87 Debug( LDAP_DEBUG_ARGS, " do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
91 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
92 tag = ber_next_element( ber, &len, last ) ) {
96 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
97 send_ldap_disconnect( conn, op,
98 LDAP_PROTOCOL_ERROR, "decoding error" );
103 if ( vals == NULL ) {
104 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
106 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
107 NULL, "no values for type", NULL, NULL );
110 return LDAP_PROTOCOL_ERROR;
113 attr_merge( e, type, vals );
116 ber_bvecfree( vals );
119 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
121 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
122 send_ldap_disconnect( conn, op,
123 LDAP_PROTOCOL_ERROR, "decoding error" );
127 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
129 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
133 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
134 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
137 * We could be serving multiple database backends. Select the
138 * appropriate one, or send a referral to our "referral server"
139 * if we don't hold it.
141 be = select_backend( e->e_ndn );
144 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
145 NULL, default_referral, NULL );
149 if ( global_readonly || be->be_readonly ) {
150 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
153 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
154 NULL, "database is read-only", NULL, NULL );
155 return LDAP_UNWILLING_TO_PERFORM;
159 * do the add if 1 && (2 || 3)
160 * 1) there is an add function implemented in this backend;
161 * 2) this backend is master for what it holds;
162 * 3) it's a replica and the dn supplied is the updatedn.
165 /* do the update here */
166 #ifdef SLAPD_MULTIMASTER
167 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
168 global_lastmod == ON)) && (be->be_update_ndn == NULL ||
169 strcmp( be->be_update_ndn, op->o_ndn )) )
171 if ( be->be_update_ndn == NULL ||
172 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
175 #ifndef SLAPD_MULTIMASTER
176 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
177 global_lastmod == ON)) && be->be_update_ndn == NULL )
180 rc = add_created_attrs( op, e );
182 if( rc != LDAP_SUCCESS ) {
184 send_ldap_result( conn, op, rc,
185 NULL, "no-user-modification attribute type",
191 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
192 #ifdef SLAPD_MULTIMASTER
193 if (be->be_update_ndn == NULL ||
194 strcmp( be->be_update_ndn, op->o_ndn ))
197 replog( be, op, e->e_dn, e );
199 be_entry_release_w( be, e );
202 #ifndef SLAPD_MULTIMASTER
205 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
206 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
210 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
212 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
213 NULL, "Function not implemented", NULL, NULL );
220 add_created_attrs( Operation *op, Entry *e )
224 struct berval *bvals[2];
229 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
234 /* return error on any attempts by the user to add these attrs */
235 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
236 if ( oc_check_no_usermod_attr( a->a_type ) ) {
237 return LDAP_CONSTRAINT_VIOLATION;
241 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
242 bv.bv_val = "<anonymous>";
243 bv.bv_len = strlen( bv.bv_val );
245 bv.bv_val = op->o_dn;
246 bv.bv_len = strlen( bv.bv_val );
248 attr_merge( e, "creatorsname", bvals );
250 currenttime = slap_get_time();
251 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
252 #ifndef LDAP_LOCALTIME
253 ltm = gmtime( ¤ttime );
254 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
256 ltm = localtime( ¤ttime );
257 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
259 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
262 bv.bv_len = strlen( bv.bv_val );
263 attr_merge( e, "createtimestamp", bvals );