3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
22 #include <ac/socket.h>
23 #include <ac/string.h>
28 static void modlist_free(LDAPModList *ml);
30 static int add_modified_attrs( Operation *op, LDAPModList **modlist );
43 LDAPModList **modtail;
50 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
52 if( op->o_bind_in_progress ) {
53 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
55 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
56 NULL, "SASL bind in progress", NULL, NULL );
57 return LDAP_SASL_BIND_IN_PROGRESS;
61 * Parse the modify request. It looks like this:
63 * ModifyRequest := [APPLICATION 6] SEQUENCE {
64 * name DistinguishedName,
65 * mods SEQUENCE OF SEQUENCE {
66 * operation ENUMERATED {
71 * modification SEQUENCE {
73 * values SET OF AttributeValue
79 if ( ber_scanf( op->o_ber, "{a" /*}*/, &ndn ) == LBER_ERROR ) {
80 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
81 send_ldap_disconnect( conn, op,
82 LDAP_PROTOCOL_ERROR, "decoding error" );
86 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
88 if( dn_normalize_case( ndn ) == NULL ) {
89 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", ndn, 0, 0 );
90 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
91 "invalid DN", NULL, NULL );
96 /* collect modifications & save for later */
100 for ( tag = ber_first_element( op->o_ber, &len, &last );
102 tag = ber_next_element( op->o_ber, &len, last ) )
106 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
108 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
109 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
112 send_ldap_disconnect( conn, op,
113 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
117 modlist_free( modlist );
121 (*modtail)->ml_op = mop;
123 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
124 (*modtail)->ml_op != LDAP_MOD_DELETE &&
125 (*modtail)->ml_op != LDAP_MOD_REPLACE )
127 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
128 NULL, "unrecognized modify operation", NULL, NULL );
130 modlist_free( modlist );
131 return LDAP_PROTOCOL_ERROR;
134 if ( (*modtail)->ml_bvalues == NULL
135 && (*modtail)->ml_op != LDAP_MOD_DELETE )
137 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
138 NULL, "unrecognized modify operation", NULL, NULL );
140 modlist_free( modlist );
141 return LDAP_PROTOCOL_ERROR;
143 attr_normalize( (*modtail)->ml_type );
145 modtail = &(*modtail)->ml_next;
150 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
151 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
152 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
153 tmp->ml_op == LDAP_MOD_ADD
154 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
155 ? "delete" : "replace"), tmp->ml_type, 0 );
159 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
161 modlist_free( modlist );
162 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
166 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
167 op->o_connid, op->o_opid, ndn, 0, 0 );
170 * We could be serving multiple database backends. Select the
171 * appropriate one, or send a referral to our "referral server"
172 * if we don't hold it.
174 if ( (be = select_backend( ndn )) == NULL ) {
176 modlist_free( modlist );
177 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
178 NULL, NULL, default_referral, NULL );
182 if ( global_readonly || be->be_readonly ) {
183 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
185 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
186 NULL, "database is read-only", NULL, NULL );
190 /* deref suffix alias if appropriate */
191 ndn = suffix_alias( be, ndn );
194 * do the modify if 1 && (2 || 3)
195 * 1) there is a modify function implemented in this backend;
196 * 2) this backend is master for what it holds;
197 * 3) it's a replica and the dn supplied is the update_ndn.
199 if ( be->be_modify ) {
200 /* do the update here */
201 #ifndef SLAPD_MULTIMASTER
202 /* we don't have to check for replicator dn
203 * because we accept each modify request
205 if ( be->be_update_ndn == NULL ||
206 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
209 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
210 global_lastmod == ON)) && be->be_update_ndn == NULL )
212 rc = add_modified_attrs( op, &modlist );
214 if( rc != LDAP_SUCCESS ) {
216 modlist_free( modlist );
217 send_ldap_result( conn, op, rc,
218 NULL, "no-user-modification attribute type",
224 if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0
225 #ifdef SLAPD_MULTIMASTER
226 && ( be->be_update_ndn == NULL ||
227 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
230 /* but we log only the ones not from a replicator user */
231 replog( be, op, ndn, modlist );
234 #ifndef SLAPD_MULTIMASTER
235 /* send a referral */
237 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
238 be->be_update_refs ? be->be_update_refs : default_referral,
243 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
244 NULL, "Function not implemented", NULL, NULL );
249 modlist_free( modlist );
254 add_modified_attrs( Operation *op, LDAPModList **modlist )
258 struct berval *bvals[2];
266 /* remove any attempts by the user to modify these attrs */
267 for ( m = *modlist; m != NULL; m = m->ml_next ) {
268 if ( oc_check_no_usermod_attr( m->ml_type ) ) {
269 return LDAP_CONSTRAINT_VIOLATION;
273 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
274 bv.bv_val = "NULLDN";
275 bv.bv_len = strlen( bv.bv_val );
277 bv.bv_val = op->o_dn;
278 bv.bv_len = strlen( bv.bv_val );
280 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
281 m->ml_type = ch_strdup( "modifiersname" );
282 m->ml_op = LDAP_MOD_REPLACE;
283 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
284 m->ml_bvalues[0] = ber_bvdup( &bv );
285 m->ml_next = *modlist;
288 currenttime = slap_get_time();
289 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
290 #ifndef LDAP_LOCALTIME
291 ltm = gmtime( ¤ttime );
292 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
294 ltm = localtime( ¤ttime );
295 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
297 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
300 bv.bv_len = strlen( bv.bv_val );
301 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
302 m->ml_type = ch_strdup( "modifytimestamp" );
303 m->ml_op = LDAP_MOD_REPLACE;
304 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
305 m->ml_bvalues[0] = ber_bvdup( &bv );
306 m->ml_next = *modlist;
319 for ( ; ml != NULL; ml = next ) {
323 if ( ml->ml_bvalues != NULL )
324 ber_bvecfree( ml->ml_bvalues );