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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
22 extern Backend *select_backend();
23 extern char *dn_normalize();
25 extern char *default_referral;
26 extern time_t currenttime;
27 extern pthread_mutex_t currenttime_mutex;
28 extern int global_lastmod;
30 static void add_created_attrs();
37 BerElement *ber = op->o_ber;
39 unsigned long len, tag;
43 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
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
57 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
58 /* initialize reader/writer lock */
62 if ( ber_scanf( ber, "{a", &dn ) == LBER_ERROR ) {
63 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
64 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
69 dn = dn_normalize( strdup( dn ) );
70 Debug( LDAP_DEBUG_ARGS, " do_add: dn (%s)\n", dn, 0, 0 );
74 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
75 tag = ber_next_element( ber, &len, last ) ) {
79 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
80 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
81 NULL, "decoding error" );
87 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
89 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
95 attr_merge( e, type, vals );
101 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
102 conn->c_connid, op->o_opid, dn, 0, 0 );
105 * We could be serving multiple database backends. Select the
106 * appropriate one, or send a referral to our "referral server"
107 * if we don't hold it.
109 if ( (be = select_backend( dn )) == NULL ) {
111 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
117 * do the add if 1 && (2 || 3)
118 * 1) there is an add function implemented in this backend;
119 * 2) this backend is master for what it holds;
120 * 3) it's a replica and the dn supplied is the updatedn.
122 if ( be->be_add != NULL ) {
123 /* do the update here */
124 if ( be->be_updatedn == NULL ||
125 strcasecmp( be->be_updatedn, op->o_dn ) == 0 ) {
127 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
128 global_lastmod == ON)) && be->be_updatedn == NULL ) {
130 add_created_attrs( op, e );
132 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
133 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
138 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
142 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
144 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
145 "Function not implemented" );
150 add_created_attrs( Operation *op, Entry *e )
154 struct berval *bvals[2];
155 Attribute **a, **next;
159 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
164 /* remove any attempts by the user to add these attrs */
165 for ( a = &e->e_attrs; *a != NULL; a = next ) {
166 if ( strcasecmp( (*a)->a_type, "modifiersname" ) == 0 ||
167 strcasecmp( (*a)->a_type, "modifytimestamp" ) == 0 ||
168 strcasecmp( (*a)->a_type, "creatorsname" ) == 0 ||
169 strcasecmp( (*a)->a_type, "createtimestamp" ) == 0 ) {
175 next = &(*a)->a_next;
179 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
180 bv.bv_val = "NULLDN";
181 bv.bv_len = strlen( bv.bv_val );
183 bv.bv_val = op->o_dn;
184 bv.bv_len = strlen( bv.bv_val );
186 attr_merge( e, "creatorsname", bvals );
188 pthread_mutex_lock( ¤ttime_mutex );
189 #ifndef LDAP_LOCALTIME
190 ltm = gmtime( ¤ttime );
191 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
193 ltm = localtime( ¤ttime );
194 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
196 pthread_mutex_unlock( ¤ttime_mutex );
199 bv.bv_len = strlen( bv.bv_val );
200 attr_merge( e, "createtimestamp", bvals );