2 * Copyright (c) 1995 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
17 #include <ac/string.h>
19 #include <ac/socket.h>
23 static int add_created_attrs(Operation *op, Entry *e);
26 do_add( Connection *conn, Operation *op )
28 BerElement *ber = op->o_ber;
29 char *dn, *ndn, *last;
34 int rc = LDAP_SUCCESS;
36 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
38 if( op->o_bind_in_progress ) {
39 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
40 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
41 "SASL bind in progress", NULL, NULL );
42 return LDAP_SASL_BIND_IN_PROGRESS;
46 * Parse the add request. It looks like this:
48 * AddRequest := [APPLICATION 14] SEQUENCE {
49 * name DistinguishedName,
50 * attrs SEQUENCE OF SEQUENCE {
52 * values SET OF AttributeValue
58 if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
59 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
60 send_ldap_disconnect( conn, op,
61 LDAP_PROTOCOL_ERROR, "decoding error" );
65 ndn = ch_strdup( dn );
67 if ( dn_normalize_case( ndn ) == NULL ) {
68 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
69 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
70 "invalid DN", NULL, NULL );
73 return LDAP_INVALID_DN_SYNTAX;
76 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
84 Debug( LDAP_DEBUG_ARGS, " do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
88 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
89 tag = ber_next_element( ber, &len, last ) ) {
93 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
94 send_ldap_disconnect( conn, op,
95 LDAP_PROTOCOL_ERROR, "decoding error" );
100 if ( vals == NULL ) {
101 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
103 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
104 NULL, "no values for type", NULL, NULL );
107 return LDAP_PROTOCOL_ERROR;
110 attr_merge( e, type, vals );
113 ber_bvecfree( vals );
116 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
118 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
119 send_ldap_disconnect( conn, op,
120 LDAP_PROTOCOL_ERROR, "decoding error" );
124 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
126 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
130 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
131 op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
134 * We could be serving multiple database backends. Select the
135 * appropriate one, or send a referral to our "referral server"
136 * if we don't hold it.
138 be = select_backend( e->e_ndn );
141 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
142 NULL, default_referral, NULL );
147 * do the add if 1 && (2 || 3)
148 * 1) there is an add function implemented in this backend;
149 * 2) this backend is master for what it holds;
150 * 3) it's a replica and the dn supplied is the updatedn.
153 /* do the update here */
154 if ( be->be_update_ndn == NULL ||
155 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
157 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
158 global_lastmod == ON)) && be->be_update_ndn == NULL )
160 rc = add_created_attrs( op, e );
162 if( rc != LDAP_SUCCESS ) {
164 send_ldap_result( conn, op, rc,
165 NULL, "no-user-modification attribute type",
171 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
172 replog( be, op, e->e_dn, e );
173 be_entry_release_w( be, e );
178 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
179 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
182 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
184 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
185 NULL, "Function not implemented", NULL, NULL );
192 add_created_attrs( Operation *op, Entry *e )
196 struct berval *bvals[2];
202 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
207 /* return error on any attempts by the user to add these attrs */
208 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
209 if ( oc_check_no_usermod_attr( a->a_type ) ) {
210 return LDAP_CONSTRAINT_VIOLATION;
214 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
215 bv.bv_val = "<anonymous>";
216 bv.bv_len = strlen( bv.bv_val );
218 bv.bv_val = op->o_dn;
219 bv.bv_len = strlen( bv.bv_val );
221 attr_merge( e, "creatorsname", bvals );
223 currenttime = slap_get_time();
224 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
225 #ifndef LDAP_LOCALTIME
226 ltm = gmtime( ¤ttime );
227 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
229 ltm = localtime( ¤ttime );
230 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
232 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
235 bv.bv_len = strlen( bv.bv_val );
236 attr_merge( e, "createtimestamp", bvals );