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;
30 unsigned long len, tag;
34 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
37 * Parse the add request. It looks like this:
39 * AddRequest := [APPLICATION 14] SEQUENCE {
40 * name DistinguishedName,
41 * attrs SEQUENCE OF SEQUENCE {
43 * values SET OF AttributeValue
49 if ( ber_scanf( ber, "{a", &dn ) == LBER_ERROR ) {
50 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
51 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
56 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
57 /* initialize reader/writer lock */
61 dn = dn_normalize( ch_strdup( dn ) );
62 Debug( LDAP_DEBUG_ARGS, " do_add: dn (%s)\n", dn, 0, 0 );
66 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
67 tag = ber_next_element( ber, &len, last ) ) {
71 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
72 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
73 NULL, "decoding error" );
79 Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
81 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
87 attr_merge( e, type, vals );
93 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d ADD dn=\"%s\"\n",
94 conn->c_connid, op->o_opid, dn, 0, 0 );
97 * We could be serving multiple database backends. Select the
98 * appropriate one, or send a referral to our "referral server"
99 * if we don't hold it.
101 if ( (be = select_backend( dn )) == NULL ) {
103 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
109 * do the add if 1 && (2 || 3)
110 * 1) there is an add function implemented in this backend;
111 * 2) this backend is master for what it holds;
112 * 3) it's a replica and the dn supplied is the updatedn.
114 if ( be->be_add != NULL ) {
115 /* do the update here */
116 if ( be->be_updatedn == NULL ||
117 strcasecmp( be->be_updatedn, op->o_dn ) == 0 ) {
119 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
120 global_lastmod == ON)) && be->be_updatedn == NULL ) {
122 add_created_attrs( op, e );
124 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
125 replog( be, LDAP_REQ_ADD, e->e_dn, e, 0 );
130 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
134 Debug( LDAP_DEBUG_ARGS, " do_add: HHH\n", 0, 0, 0 );
136 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
137 "Function not implemented" );
142 add_created_attrs( Operation *op, Entry *e )
146 struct berval *bvals[2];
147 Attribute **a, **next;
151 Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
156 /* remove any attempts by the user to add these attrs */
157 for ( a = &e->e_attrs; *a != NULL; a = next ) {
158 if ( strcasecmp( (*a)->a_type, "modifiersname" ) == 0 ||
159 strcasecmp( (*a)->a_type, "modifytimestamp" ) == 0 ||
160 strcasecmp( (*a)->a_type, "creatorsname" ) == 0 ||
161 strcasecmp( (*a)->a_type, "createtimestamp" ) == 0 ) {
167 next = &(*a)->a_next;
171 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
172 bv.bv_val = "NULLDN";
173 bv.bv_len = strlen( bv.bv_val );
175 bv.bv_val = op->o_dn;
176 bv.bv_len = strlen( bv.bv_val );
178 attr_merge( e, "creatorsname", bvals );
180 pthread_mutex_lock( ¤ttime_mutex );
181 #ifndef LDAP_LOCALTIME
182 ltm = gmtime( ¤ttime );
183 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
185 ltm = localtime( ¤ttime );
186 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
188 pthread_mutex_unlock( ¤ttime_mutex );
191 bv.bv_len = strlen( bv.bv_val );
192 attr_merge( e, "createtimestamp", bvals );