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;
35 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
38 * Parse the add request. It looks like this:
40 * AddRequest := [APPLICATION 14] SEQUENCE {
41 * name DistinguishedName,
42 * attrs SEQUENCE OF SEQUENCE {
44 * values SET OF AttributeValue
50 if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
51 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
52 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
57 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
60 e->e_ndn = dn_normalize_case( ch_strdup( dn ) );
65 Debug( LDAP_DEBUG_ARGS, " do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
69 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
70 tag = ber_next_element( ber, &len, last ) ) {
74 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
75 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
76 NULL, "decoding error" );
82 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
84 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
91 attr_merge( e, type, vals );
97 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
98 conn->c_connid, op->o_opid, e->e_ndn, 0, 0 );
101 * We could be serving multiple database backends. Select the
102 * appropriate one, or send a referral to our "referral server"
103 * if we don't hold it.
105 be = select_backend( e->e_ndn );
108 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
114 * do the add if 1 && (2 || 3)
115 * 1) there is an add function implemented in this backend;
116 * 2) this backend is master for what it holds;
117 * 3) it's a replica and the dn supplied is the updatedn.
120 /* do the update here */
121 if ( be->be_update_ndn == NULL ||
122 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
124 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
125 global_lastmod == ON)) && be->be_update_ndn == NULL ) {
127 add_created_attrs( op, e );
129 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
130 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
131 be_entry_release_w( be, e );
136 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
140 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
142 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
143 "Function not implemented" );
148 add_created_attrs( Operation *op, Entry *e )
152 struct berval *bvals[2];
153 Attribute **a, **next;
158 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
163 /* remove any attempts by the user to add these attrs */
164 for ( a = &e->e_attrs; *a != NULL; a = next ) {
165 if ( oc_check_operational( (*a)->a_type ) ) {
171 next = &(*a)->a_next;
175 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
176 bv.bv_val = "NULLDN";
177 bv.bv_len = strlen( bv.bv_val );
179 bv.bv_val = op->o_dn;
180 bv.bv_len = strlen( bv.bv_val );
182 attr_merge( e, "creatorsname", bvals );
184 currenttime = slap_get_time();
185 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
186 #ifndef LDAP_LOCALTIME
187 ltm = gmtime( ¤ttime );
188 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
190 ltm = localtime( ¤ttime );
191 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
193 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
196 bv.bv_len = strlen( bv.bv_val );
197 attr_merge( e, "createtimestamp", bvals );