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_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
62 return LDAP_PROTOCOL_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_result( conn, op, LDAP_PROTOCOL_ERROR,
84 NULL, "decoding error" );
86 return LDAP_PROTOCOL_ERROR;
90 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
92 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
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_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
110 return LDAP_PROTOCOL_ERROR;
114 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
116 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
121 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
122 conn->c_connid, op->o_opid, e->e_ndn, 0, 0 );
125 * We could be serving multiple database backends. Select the
126 * appropriate one, or send a referral to our "referral server"
127 * if we don't hold it.
129 be = select_backend( e->e_ndn );
132 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
138 * do the add if 1 && (2 || 3)
139 * 1) there is an add function implemented in this backend;
140 * 2) this backend is master for what it holds;
141 * 3) it's a replica and the dn supplied is the updatedn.
144 /* do the update here */
145 if ( be->be_update_ndn == NULL ||
146 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
148 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
149 global_lastmod == ON)) && be->be_update_ndn == NULL ) {
151 add_created_attrs( op, e );
153 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
154 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
155 be_entry_release_w( be, e );
160 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
164 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
166 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
167 "Function not implemented" );
174 add_created_attrs( Operation *op, Entry *e )
178 struct berval *bvals[2];
179 Attribute **a, **next;
184 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
189 /* remove any attempts by the user to add these attrs */
190 for ( a = &e->e_attrs; *a != NULL; a = next ) {
191 if ( oc_check_operational( (*a)->a_type ) ) {
197 next = &(*a)->a_next;
201 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
202 bv.bv_val = "NULLDN";
203 bv.bv_len = strlen( bv.bv_val );
205 bv.bv_val = op->o_dn;
206 bv.bv_len = strlen( bv.bv_val );
208 attr_merge( e, "creatorsname", bvals );
210 currenttime = slap_get_time();
211 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
212 #ifndef LDAP_LOCALTIME
213 ltm = gmtime( ¤ttime );
214 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
216 ltm = localtime( ¤ttime );
217 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
219 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
222 bv.bv_len = strlen( bv.bv_val );
223 attr_merge( e, "createtimestamp", bvals );