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/socket.h>
18 #include <ac/string.h>
23 static void add_lastmods( Operation *op, LDAPMod **mods );
24 static void modlist_free(LDAPMod *mods);
35 unsigned long tag, len;
40 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
43 * Parse the modify request. It looks like this:
45 * ModifyRequest := [APPLICATION 6] SEQUENCE {
46 * name DistinguishedName,
47 * mods SEQUENCE OF SEQUENCE {
48 * operation ENUMERATED {
53 * modification SEQUENCE {
55 * values SET OF AttributeValue
61 if ( ber_scanf( op->o_ber, "{a" /*}*/, &ndn ) == LBER_ERROR ) {
62 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
63 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
67 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
69 (void) dn_normalize_case( ndn );
71 /* collect modifications & save for later */
74 for ( tag = ber_first_element( op->o_ber, &len, &last );
76 tag = ber_next_element( op->o_ber, &len, last ) )
78 (*modtail) = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
80 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &(*modtail)->mod_op,
81 &(*modtail)->mod_type, &(*modtail)->mod_bvalues )
84 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
93 if ( (*modtail)->mod_op != LDAP_MOD_ADD &&
94 (*modtail)->mod_op != LDAP_MOD_DELETE &&
95 (*modtail)->mod_op != LDAP_MOD_REPLACE )
97 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
98 "unrecognized modify operation" );
100 modlist_free( mods );
104 if ( (*modtail)->mod_bvalues == NULL && (*modtail)->mod_op
105 != LDAP_MOD_DELETE ) {
106 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
109 modlist_free( mods );
112 attr_normalize( (*modtail)->mod_type );
114 modtail = &(*modtail)->mod_next;
119 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
120 for ( tmp = mods; tmp != NULL; tmp = tmp->mod_next ) {
121 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n", tmp->mod_op
122 == LDAP_MOD_ADD ? "add" : (tmp->mod_op == LDAP_MOD_DELETE ?
123 "delete" : "replace"), tmp->mod_type, 0 );
127 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
128 conn->c_connid, op->o_opid, ndn, 0, 0 );
131 * We could be serving multiple database backends. Select the
132 * appropriate one, or send a referral to our "referral server"
133 * if we don't hold it.
135 if ( (be = select_backend( ndn )) == NULL ) {
137 modlist_free( mods );
138 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
143 /* alias suffix if approp */
144 ndn = suffixAlias ( ndn, op, be );
147 * do the modify if 1 && (2 || 3)
148 * 1) there is a modify function implemented in this backend;
149 * 2) this backend is master for what it holds;
150 * 3) it's a replica and the dn supplied is the update_ndn.
152 if ( be->be_modify != NULL ) {
153 /* do the update here */
154 if ( be->be_update_ndn == NULL ||
155 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
157 if(( be->be_lastmod == ON || (be->be_lastmod == UNDEFINED
158 && global_lastmod == ON )) && be->be_update_ndn == NULL )
160 add_lastmods( op, &mods );
163 if ( (*be->be_modify)( be, conn, op, ndn, mods ) == 0 ) {
164 replog( be, LDAP_REQ_MODIFY, ndn, mods, 0 );
167 /* send a referral */
169 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
173 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
174 "Function not implemented" );
178 modlist_free( mods );
182 add_lastmods( Operation *op, LDAPMod **mods )
186 struct berval *bvals[2];
191 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
196 /* remove any attempts by the user to modify these attrs */
197 for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
198 if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 ||
199 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
200 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 ||
201 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
203 Debug( LDAP_DEBUG_TRACE,
204 "add_lastmods: found lastmod attr: %s\n",
205 (*m)->mod_type, 0, 0 );
208 free( tmp->mod_type );
209 if ( tmp->mod_bvalues != NULL ) {
210 ber_bvecfree( tmp->mod_bvalues );
218 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
219 bv.bv_val = "NULLDN";
220 bv.bv_len = strlen( bv.bv_val );
222 bv.bv_val = op->o_dn;
223 bv.bv_len = strlen( bv.bv_val );
225 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
226 tmp->mod_type = ch_strdup( "modifiersname" );
227 tmp->mod_op = LDAP_MOD_REPLACE;
228 tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
229 2 * sizeof(struct berval *) );
230 tmp->mod_bvalues[0] = ber_bvdup( &bv );
231 tmp->mod_next = *mods;
234 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
235 #ifndef LDAP_LOCALTIME
236 ltm = gmtime( ¤ttime );
237 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
239 ltm = localtime( ¤ttime );
240 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
242 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
244 bv.bv_len = strlen( bv.bv_val );
245 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
246 tmp->mod_type = ch_strdup( "modifytimestamp" );
247 tmp->mod_op = LDAP_MOD_REPLACE;
248 tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
249 tmp->mod_bvalues[0] = ber_bvdup( &bv );
250 tmp->mod_next = *mods;
262 for ( ; mods != NULL; mods = next ) {
263 next = mods->mod_next;
264 free( mods->mod_type );
265 if ( mods->mod_bvalues != NULL )
266 ber_bvecfree( mods->mod_bvalues );