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 modlist_free(LDAPMod *mods);
24 static void add_lastmods(Operation *op, 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 ) {
159 add_lastmods( op, &mods );
161 if ( (*be->be_modify)( be, conn, op, ndn, mods ) == 0 ) {
162 replog( be, LDAP_REQ_MODIFY, ndn, mods, 0 );
165 /* send a referral */
167 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
171 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
172 "Function not implemented" );
176 modlist_free( mods );
186 for ( ; mods != NULL; mods = next ) {
187 next = mods->mod_next;
188 free( mods->mod_type );
189 if ( mods->mod_bvalues != NULL )
190 ber_bvecfree( mods->mod_bvalues );
196 add_lastmods( Operation *op, LDAPMod **mods )
200 struct berval *bvals[2];
205 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
210 /* remove any attempts by the user to modify these attrs */
211 for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
212 if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 ||
213 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
214 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 ||
215 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
217 Debug( LDAP_DEBUG_TRACE,
218 "add_lastmods: found lastmod attr: %s\n",
219 (*m)->mod_type, 0, 0 );
222 free( tmp->mod_type );
223 if ( tmp->mod_bvalues != NULL ) {
224 ber_bvecfree( tmp->mod_bvalues );
232 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
233 bv.bv_val = "NULLDN";
234 bv.bv_len = strlen( bv.bv_val );
236 bv.bv_val = op->o_dn;
237 bv.bv_len = strlen( bv.bv_val );
239 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
240 tmp->mod_type = ch_strdup( "modifiersname" );
241 tmp->mod_op = LDAP_MOD_REPLACE;
242 tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
243 2 * sizeof(struct berval *) );
244 tmp->mod_bvalues[0] = ber_bvdup( &bv );
245 tmp->mod_next = *mods;
248 pthread_mutex_lock( ¤ttime_mutex );
249 #ifndef LDAP_LOCALTIME
250 ltm = gmtime( ¤ttime );
251 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
253 ltm = localtime( ¤ttime );
254 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
256 pthread_mutex_unlock( ¤ttime_mutex );
258 bv.bv_len = strlen( bv.bv_val );
259 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
260 tmp->mod_type = ch_strdup( "modifytimestamp" );
261 tmp->mod_op = LDAP_MOD_REPLACE;
262 tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
263 tmp->mod_bvalues[0] = ber_bvdup( &bv );
264 tmp->mod_next = *mods;