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.
16 #include <sys/types.h>
17 #include <sys/socket.h>
20 extern Backend *select_backend();
21 extern char *dn_normalize();
23 extern char *default_referral;
24 extern time_t currenttime;
25 extern pthread_mutex_t currenttime_mutex;
26 extern int global_lastmod;
28 static void add_created_attrs();
35 BerElement *ber = op->o_ber;
37 unsigned long len, tag;
41 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
44 * Parse the add request. It looks like this:
46 * AddRequest := [APPLICATION 14] SEQUENCE {
47 * name DistinguishedName,
48 * attrs SEQUENCE OF SEQUENCE {
50 * values SET OF AttributeValue
55 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
56 /* initialize reader/writer lock */
60 if ( ber_scanf( ber, "{a", &dn ) == LBER_ERROR ) {
61 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
62 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
67 dn = dn_normalize( strdup( dn ) );
68 Debug( LDAP_DEBUG_ARGS, " do_add: dn (%s)\n", dn, 0, 0 );
72 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
73 tag = ber_next_element( ber, &len, last ) ) {
77 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
78 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
79 NULL, "decoding error" );
85 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
87 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
93 attr_merge( e, type, vals );
99 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
100 conn->c_connid, op->o_opid, dn, 0, 0 );
103 * We could be serving multiple database backends. Select the
104 * appropriate one, or send a referral to our "referral server"
105 * if we don't hold it.
107 if ( (be = select_backend( dn )) == NULL ) {
109 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
115 * do the add if 1 && (2 || 3)
116 * 1) there is an add function implemented in this backend;
117 * 2) this backend is master for what it holds;
118 * 3) it's a replica and the dn supplied is the updatedn.
120 if ( be->be_add != NULL ) {
121 /* do the update here */
122 if ( be->be_updatedn == NULL ||
123 strcasecmp( be->be_updatedn, op->o_dn ) == 0 ) {
125 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
126 global_lastmod == ON)) && be->be_updatedn == NULL ) {
128 add_created_attrs( op, e );
130 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
131 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
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;
157 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
162 /* remove any attempts by the user to add these attrs */
163 for ( a = &e->e_attrs; *a != NULL; a = next ) {
164 if ( strcasecmp( (*a)->a_type, "modifiersname" ) == 0 ||
165 strcasecmp( (*a)->a_type, "modifytimestamp" ) == 0 ||
166 strcasecmp( (*a)->a_type, "creatorsname" ) == 0 ||
167 strcasecmp( (*a)->a_type, "createtimestamp" ) == 0 ) {
173 next = &(*a)->a_next;
177 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
178 bv.bv_val = "NULLDN";
179 bv.bv_len = strlen( bv.bv_val );
181 bv.bv_val = op->o_dn;
182 bv.bv_len = strlen( bv.bv_val );
184 attr_merge( e, "creatorsname", bvals );
186 pthread_mutex_lock( ¤ttime_mutex );
187 ltm = localtime( ¤ttime );
189 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
191 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
193 pthread_mutex_unlock( ¤ttime_mutex );
196 bv.bv_len = strlen( bv.bv_val );
197 attr_merge( e, "createtimestamp", bvals );