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 extern Backend *select_backend();
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 modlist_free();
31 static void add_lastmods();
32 extern char *suffixAlias();
43 unsigned long tag, len;
48 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
51 * Parse the modify request. It looks like this:
53 * ModifyRequest := [APPLICATION 6] SEQUENCE {
54 * name DistinguishedName,
55 * mods SEQUENCE OF SEQUENCE {
56 * operation ENUMERATED {
61 * modification SEQUENCE {
63 * values SET OF AttributeValue
69 if ( ber_scanf( op->o_ber, "{a", &dn ) == LBER_ERROR ) {
70 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
71 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
77 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
79 /* collect modifications & save for later */
82 for ( tag = ber_first_element( op->o_ber, &len, &last );
84 tag = ber_next_element( op->o_ber, &len, last ) )
86 (*modtail) = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
88 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &(*modtail)->mod_op,
89 &(*modtail)->mod_type, &(*modtail)->mod_bvalues )
92 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
101 if ( (*modtail)->mod_op != LDAP_MOD_ADD &&
102 (*modtail)->mod_op != LDAP_MOD_DELETE &&
103 (*modtail)->mod_op != LDAP_MOD_REPLACE )
105 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
106 "unrecognized modify operation" );
109 modlist_free( mods );
113 if ( (*modtail)->mod_bvalues == NULL && (*modtail)->mod_op
114 != LDAP_MOD_DELETE ) {
115 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
119 modlist_free( mods );
122 attr_normalize( (*modtail)->mod_type );
124 modtail = &(*modtail)->mod_next;
129 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
130 for ( tmp = mods; tmp != NULL; tmp = tmp->mod_next ) {
131 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n", tmp->mod_op
132 == LDAP_MOD_ADD ? "add" : (tmp->mod_op == LDAP_MOD_DELETE ?
133 "delete" : "replace"), tmp->mod_type, 0 );
137 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
138 conn->c_connid, op->o_opid, dn, 0, 0 );
141 * We could be serving multiple database backends. Select the
142 * appropriate one, or send a referral to our "referral server"
143 * if we don't hold it.
145 if ( (be = select_backend( dn )) == NULL ) {
148 modlist_free( mods );
149 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
154 /* alias suffix if approp */
155 dn = suffixAlias ( dn, op, be );
158 * do the modify if 1 && (2 || 3)
159 * 1) there is a modify function implemented in this backend;
160 * 2) this backend is master for what it holds;
161 * 3) it's a replica and the dn supplied is the updatedn.
163 if ( be->be_modify != NULL ) {
164 /* do the update here */
165 if ( be->be_updatedn == NULL ||
166 strcasecmp( be->be_updatedn, op->o_dn ) == 0 ) {
168 if ( (be->be_lastmod == ON || ( be->be_lastmod == UNDEFINED &&
169 global_lastmod == ON ) ) && be->be_updatedn == NULL ) {
170 add_lastmods( op, &mods );
172 if ( (*be->be_modify)( be, conn, op, odn, mods ) == 0 ) {
173 replog( be, LDAP_REQ_MODIFY, dn, mods, 0 );
176 /* send a referral */
178 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
182 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
183 "Function not implemented" );
188 modlist_free( mods );
198 for ( ; mods != NULL; mods = next ) {
199 next = mods->mod_next;
200 free( mods->mod_type );
201 if ( mods->mod_bvalues != NULL )
202 ber_bvecfree( mods->mod_bvalues );
208 add_lastmods( Operation *op, LDAPMod **mods )
212 struct berval *bvals[2];
217 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
222 /* remove any attempts by the user to modify these attrs */
223 for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
224 if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 ||
225 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
226 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 ||
227 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
229 Debug( LDAP_DEBUG_TRACE,
230 "add_lastmods: found lastmod attr: %s\n",
231 (*m)->mod_type, 0, 0 );
234 free( tmp->mod_type );
235 if ( tmp->mod_bvalues != NULL ) {
236 ber_bvecfree( tmp->mod_bvalues );
244 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
245 bv.bv_val = "NULLDN";
246 bv.bv_len = strlen( bv.bv_val );
248 bv.bv_val = op->o_dn;
249 bv.bv_len = strlen( bv.bv_val );
251 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
252 tmp->mod_type = strdup( "modifiersname" );
253 tmp->mod_op = LDAP_MOD_REPLACE;
254 tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
255 2 * sizeof(struct berval *) );
256 tmp->mod_bvalues[0] = ber_bvdup( &bv );
257 tmp->mod_next = *mods;
260 pthread_mutex_lock( ¤ttime_mutex );
261 #ifndef LDAP_LOCALTIME
262 ltm = gmtime( ¤ttime );
263 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
265 ltm = localtime( ¤ttime );
266 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
268 pthread_mutex_unlock( ¤ttime_mutex );
270 bv.bv_len = strlen( bv.bv_val );
271 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
272 tmp->mod_type = strdup( "modifytimestamp" );
273 tmp->mod_op = LDAP_MOD_REPLACE;
274 tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
275 tmp->mod_bvalues[0] = ber_bvdup( &bv );
276 tmp->mod_next = *mods;