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(LDAPModList *ml);
25 static int add_modified_attrs( Operation *op, LDAPModList **modlist );
38 LDAPModList **modtail;
45 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
47 if( op->o_bind_in_progress ) {
48 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
50 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
51 NULL, "SASL bind in progress", NULL, NULL );
52 return LDAP_SASL_BIND_IN_PROGRESS;
56 * Parse the modify request. It looks like this:
58 * ModifyRequest := [APPLICATION 6] SEQUENCE {
59 * name DistinguishedName,
60 * mods SEQUENCE OF SEQUENCE {
61 * operation ENUMERATED {
66 * modification SEQUENCE {
68 * values SET OF AttributeValue
74 if ( ber_scanf( op->o_ber, "{a" /*}*/, &ndn ) == LBER_ERROR ) {
75 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
76 send_ldap_disconnect( conn, op,
77 LDAP_PROTOCOL_ERROR, "decoding error" );
81 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
83 if( dn_normalize_case( ndn ) == NULL ) {
84 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", ndn, 0, 0 );
85 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
86 "invalid DN", NULL, NULL );
91 /* collect modifications & save for later */
95 for ( tag = ber_first_element( op->o_ber, &len, &last );
97 tag = ber_next_element( op->o_ber, &len, last ) )
101 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
103 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
104 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
107 send_ldap_disconnect( conn, op,
108 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
112 modlist_free( modlist );
116 (*modtail)->ml_op = mop;
118 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
119 (*modtail)->ml_op != LDAP_MOD_DELETE &&
120 (*modtail)->ml_op != LDAP_MOD_REPLACE )
122 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
123 NULL, "unrecognized modify operation", NULL, NULL );
125 modlist_free( modlist );
126 return LDAP_PROTOCOL_ERROR;
129 if ( (*modtail)->ml_bvalues == NULL
130 && (*modtail)->ml_op != LDAP_MOD_DELETE )
132 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
133 NULL, "unrecognized modify operation", NULL, NULL );
135 modlist_free( modlist );
136 return LDAP_PROTOCOL_ERROR;
138 attr_normalize( (*modtail)->ml_type );
140 modtail = &(*modtail)->ml_next;
145 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
146 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
147 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
148 tmp->ml_op == LDAP_MOD_ADD
149 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
150 ? "delete" : "replace"), tmp->ml_type, 0 );
154 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
156 modlist_free( modlist );
157 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
161 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
162 op->o_connid, op->o_opid, ndn, 0, 0 );
165 * We could be serving multiple database backends. Select the
166 * appropriate one, or send a referral to our "referral server"
167 * if we don't hold it.
169 if ( (be = select_backend( ndn )) == NULL ) {
171 modlist_free( modlist );
172 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
173 NULL, NULL, default_referral, NULL );
177 /* deref suffix alias if appropriate */
178 ndn = suffix_alias( be, ndn );
181 * do the modify if 1 && (2 || 3)
182 * 1) there is a modify function implemented in this backend;
183 * 2) this backend is master for what it holds;
184 * 3) it's a replica and the dn supplied is the update_ndn.
186 if ( be->be_modify ) {
187 /* do the update here */
188 if ( be->be_update_ndn == NULL ||
189 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
191 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
192 global_lastmod == ON)) && be->be_update_ndn == NULL )
194 rc = add_modified_attrs( op, &modlist );
196 if( rc != LDAP_SUCCESS ) {
198 modlist_free( modlist );
199 send_ldap_result( conn, op, rc,
200 NULL, "no-user-modification attribute type",
206 if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0 ) {
207 replog( be, op, ndn, modlist );
210 /* send a referral */
212 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
213 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
216 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
217 NULL, "Function not implemented", NULL, NULL );
221 modlist_free( modlist );
226 add_modified_attrs( Operation *op, LDAPModList **modlist )
230 struct berval *bvals[2];
238 /* remove any attempts by the user to modify these attrs */
239 for ( m = *modlist; m != NULL; m = m->ml_next ) {
240 if ( oc_check_no_usermod_attr( m->ml_type ) ) {
241 return LDAP_CONSTRAINT_VIOLATION;
245 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
246 bv.bv_val = "NULLDN";
247 bv.bv_len = strlen( bv.bv_val );
249 bv.bv_val = op->o_dn;
250 bv.bv_len = strlen( bv.bv_val );
252 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
253 m->ml_type = ch_strdup( "modifiersname" );
254 m->ml_op = LDAP_MOD_REPLACE;
255 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
256 m->ml_bvalues[0] = ber_bvdup( &bv );
257 m->ml_next = *modlist;
260 currenttime = slap_get_time();
261 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
262 #ifndef LDAP_LOCALTIME
263 ltm = gmtime( ¤ttime );
264 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
266 ltm = localtime( ¤ttime );
267 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
269 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
272 bv.bv_len = strlen( bv.bv_val );
273 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
274 m->ml_type = ch_strdup( "modifytimestamp" );
275 m->ml_op = LDAP_MOD_REPLACE;
276 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
277 m->ml_bvalues[0] = ber_bvdup( &bv );
278 m->ml_next = *modlist;
291 for ( ; ml != NULL; ml = next ) {
295 if ( ml->ml_bvalues != NULL )
296 ber_bvecfree( ml->ml_bvalues );