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 void add_created_attrs(Operation *op, Entry *e);
26 do_add( Connection *conn, Operation *op )
28 BerElement *ber = op->o_ber;
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" );
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 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
68 e->e_ndn = dn_normalize_case( ch_strdup( dn ) );
73 Debug( LDAP_DEBUG_ARGS, " do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
77 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
78 tag = ber_next_element( ber, &len, last ) ) {
82 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
83 send_ldap_disconnect( conn, op,
84 LDAP_PROTOCOL_ERROR, "decoding error" );
90 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
92 send_ldap_result( conn, op,
93 LDAP_PROTOCOL_ERROR, NULL, "no values for type" );
96 return LDAP_PROTOCOL_ERROR;
99 attr_merge( e, type, vals );
102 ber_bvecfree( vals );
105 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
107 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
108 send_ldap_disconnect( conn, op,
109 LDAP_PROTOCOL_ERROR, "decoding error" );
113 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
115 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
119 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
120 conn->c_connid, op->o_opid, e->e_ndn, 0, 0 );
123 * We could be serving multiple database backends. Select the
124 * appropriate one, or send a referral to our "referral server"
125 * if we don't hold it.
127 be = select_backend( e->e_ndn );
130 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
136 * do the add if 1 && (2 || 3)
137 * 1) there is an add function implemented in this backend;
138 * 2) this backend is master for what it holds;
139 * 3) it's a replica and the dn supplied is the updatedn.
142 /* do the update here */
143 if ( be->be_update_ndn == NULL ||
144 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
146 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
147 global_lastmod == ON)) && be->be_update_ndn == NULL ) {
149 add_created_attrs( op, e );
151 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
152 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
153 be_entry_release_w( be, e );
158 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
162 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
164 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
165 "Function not implemented" );
172 add_created_attrs( Operation *op, Entry *e )
176 struct berval *bvals[2];
177 Attribute **a, **next;
182 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
187 /* remove any attempts by the user to add these attrs */
188 for ( a = &e->e_attrs; *a != NULL; a = next ) {
189 if ( oc_check_no_usermod_attr( (*a)->a_type ) ) {
195 next = &(*a)->a_next;
199 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
200 bv.bv_val = "<anonymous>";
201 bv.bv_len = strlen( bv.bv_val );
203 bv.bv_val = op->o_dn;
204 bv.bv_len = strlen( bv.bv_val );
206 attr_merge( e, "creatorsname", bvals );
208 currenttime = slap_get_time();
209 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
210 #ifndef LDAP_LOCALTIME
211 ltm = gmtime( ¤ttime );
212 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
214 ltm = localtime( ¤ttime );
215 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
217 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
220 bv.bv_len = strlen( bv.bv_val );
221 attr_merge( e, "createtimestamp", bvals );