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.
19 #include <sys/types.h>
20 #include <sys/socket.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();
41 unsigned long tag, len;
46 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
49 * Parse the modify request. It looks like this:
51 * ModifyRequest := [APPLICATION 6] SEQUENCE {
52 * name DistinguishedName,
53 * mods SEQUENCE OF SEQUENCE {
54 * operation ENUMERATED {
59 * modification SEQUENCE {
61 * values SET OF AttributeValue
67 if ( ber_scanf( op->o_ber, "{a", &dn ) == LBER_ERROR ) {
68 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
69 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
75 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
77 /* collect modifications & save for later */
80 for ( tag = ber_first_element( op->o_ber, &len, &last );
82 tag = ber_next_element( op->o_ber, &len, last ) )
84 (*modtail) = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
86 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &(*modtail)->mod_op,
87 &(*modtail)->mod_type, &(*modtail)->mod_bvalues )
90 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
99 if ( (*modtail)->mod_op != LDAP_MOD_ADD &&
100 (*modtail)->mod_op != LDAP_MOD_DELETE &&
101 (*modtail)->mod_op != LDAP_MOD_REPLACE )
103 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
104 "unrecognized modify operation" );
107 modlist_free( mods );
111 if ( (*modtail)->mod_bvalues == NULL && (*modtail)->mod_op
112 != LDAP_MOD_DELETE ) {
113 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
117 modlist_free( mods );
120 attr_normalize( (*modtail)->mod_type );
122 modtail = &(*modtail)->mod_next;
127 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
128 for ( tmp = mods; tmp != NULL; tmp = tmp->mod_next ) {
129 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n", tmp->mod_op
130 == LDAP_MOD_ADD ? "add" : (tmp->mod_op == LDAP_MOD_DELETE ?
131 "delete" : "replace"), tmp->mod_type, 0 );
135 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
136 conn->c_connid, op->o_opid, dn, 0, 0 );
139 * We could be serving multiple database backends. Select the
140 * appropriate one, or send a referral to our "referral server"
141 * if we don't hold it.
143 if ( (be = select_backend( dn )) == NULL ) {
146 modlist_free( mods );
147 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
153 * do the modify if 1 && (2 || 3)
154 * 1) there is a modify function implemented in this backend;
155 * 2) this backend is master for what it holds;
156 * 3) it's a replica and the dn supplied is the updatedn.
158 if ( be->be_modify != NULL ) {
159 /* do the update here */
160 if ( be->be_updatedn == NULL ||
161 strcasecmp( be->be_updatedn, op->o_dn ) == 0 ) {
163 if ( (be->be_lastmod == ON || ( be->be_lastmod == UNDEFINED &&
164 global_lastmod == ON ) ) && be->be_updatedn == NULL ) {
165 add_lastmods( op, &mods );
167 if ( (*be->be_modify)( be, conn, op, odn, mods ) == 0 ) {
168 replog( be, LDAP_REQ_MODIFY, dn, mods, 0 );
171 /* send a referral */
173 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
177 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
178 "Function not implemented" );
183 modlist_free( mods );
193 for ( ; mods != NULL; mods = next ) {
194 next = mods->mod_next;
195 free( mods->mod_type );
196 if ( mods->mod_bvalues != NULL )
197 ber_bvecfree( mods->mod_bvalues );
203 add_lastmods( Operation *op, LDAPMod **mods )
207 struct berval *bvals[2];
212 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
217 /* remove any attempts by the user to modify these attrs */
218 for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
219 if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 ||
220 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
221 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 ||
222 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
224 Debug( LDAP_DEBUG_TRACE,
225 "add_lastmods: found lastmod attr: %s\n",
226 (*m)->mod_type, 0, 0 );
229 free( tmp->mod_type );
230 if ( tmp->mod_bvalues != NULL ) {
231 ber_bvecfree( tmp->mod_bvalues );
239 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
240 bv.bv_val = "NULLDN";
241 bv.bv_len = strlen( bv.bv_val );
243 bv.bv_val = op->o_dn;
244 bv.bv_len = strlen( bv.bv_val );
246 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
247 tmp->mod_type = strdup( "modifiersname" );
248 tmp->mod_op = LDAP_MOD_REPLACE;
249 tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
250 2 * sizeof(struct berval *) );
251 tmp->mod_bvalues[0] = ber_bvdup( &bv );
252 tmp->mod_next = *mods;
255 pthread_mutex_lock( ¤ttime_mutex );
256 #ifndef LDAP_LOCALTIME
257 ltm = gmtime( ¤ttime );
258 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
260 ltm = localtime( ¤ttime );
261 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
263 pthread_mutex_unlock( ¤ttime_mutex );
265 bv.bv_len = strlen( bv.bv_val );
266 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
267 tmp->mod_type = strdup( "modifytimestamp" );
268 tmp->mod_op = LDAP_MOD_REPLACE;
269 tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
270 tmp->mod_bvalues[0] = ber_bvdup( &bv );
271 tmp->mod_next = *mods;