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 Debug( LDAP_DEBUG_ANY,
128 "do_modify: invalid modify operation (%ld)\n",
129 (long) (*modtail)->ml_op, 0, 0 );
130 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
131 NULL, "unrecognized modify operation", NULL, NULL );
133 modlist_free( modlist );
134 return LDAP_PROTOCOL_ERROR;
137 if ( (*modtail)->ml_bvalues == NULL && (
138 (*modtail)->ml_op != LDAP_MOD_REPLACE &&
139 (*modtail)->ml_op != LDAP_MOD_DELETE ) )
141 Debug( LDAP_DEBUG_ANY,
142 "do_modify: invalid modify operation (%ld) without values\n",
143 (long) (*modtail)->ml_op, 0, 0 );
144 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
145 NULL, "unrecognized modify operation without values",
148 modlist_free( modlist );
149 return LDAP_PROTOCOL_ERROR;
151 attr_normalize( (*modtail)->ml_type );
153 modtail = &(*modtail)->ml_next;
158 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
159 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
160 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
161 tmp->ml_op == LDAP_MOD_ADD
162 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
163 ? "delete" : "replace"), tmp->ml_type, 0 );
167 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
169 modlist_free( modlist );
170 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
174 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
175 op->o_connid, op->o_opid, ndn, 0, 0 );
178 * We could be serving multiple database backends. Select the
179 * appropriate one, or send a referral to our "referral server"
180 * if we don't hold it.
182 if ( (be = select_backend( ndn )) == NULL ) {
184 modlist_free( modlist );
185 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
186 NULL, NULL, default_referral, NULL );
190 if ( global_readonly || be->be_readonly ) {
191 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
193 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
194 NULL, "database is read-only", NULL, NULL );
198 /* deref suffix alias if appropriate */
199 ndn = suffix_alias( be, ndn );
202 * do the modify if 1 && (2 || 3)
203 * 1) there is a modify function implemented in this backend;
204 * 2) this backend is master for what it holds;
205 * 3) it's a replica and the dn supplied is the update_ndn.
207 if ( be->be_modify ) {
208 /* do the update here */
209 #ifndef SLAPD_MULTIMASTER
210 /* we don't have to check for replicator dn
211 * because we accept each modify request
213 if ( be->be_update_ndn == NULL ||
214 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
217 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
218 global_lastmod == ON)) && be->be_update_ndn == NULL )
220 rc = add_modified_attrs( op, &modlist );
222 if( rc != LDAP_SUCCESS ) {
224 modlist_free( modlist );
225 send_ldap_result( conn, op, rc,
226 NULL, "no-user-modification attribute type",
232 if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0
233 #ifdef SLAPD_MULTIMASTER
234 && ( be->be_update_ndn == NULL ||
235 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
238 /* but we log only the ones not from a replicator user */
239 replog( be, op, ndn, modlist );
242 #ifndef SLAPD_MULTIMASTER
243 /* send a referral */
245 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
246 be->be_update_refs ? be->be_update_refs : default_referral,
251 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
252 NULL, "Function not implemented", NULL, NULL );
257 modlist_free( modlist );
262 add_modified_attrs( Operation *op, LDAPModList **modlist )
266 struct berval *bvals[2];
274 /* remove any attempts by the user to modify these attrs */
275 for ( m = *modlist; m != NULL; m = m->ml_next ) {
276 if ( oc_check_no_usermod_attr( m->ml_type ) ) {
277 return LDAP_CONSTRAINT_VIOLATION;
281 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
282 bv.bv_val = "NULLDN";
283 bv.bv_len = strlen( bv.bv_val );
285 bv.bv_val = op->o_dn;
286 bv.bv_len = strlen( bv.bv_val );
288 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
289 m->ml_type = ch_strdup( "modifiersname" );
290 m->ml_op = LDAP_MOD_REPLACE;
291 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
292 m->ml_bvalues[0] = ber_bvdup( &bv );
293 m->ml_next = *modlist;
296 currenttime = slap_get_time();
297 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
298 #ifndef LDAP_LOCALTIME
299 ltm = gmtime( ¤ttime );
300 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
302 ltm = localtime( ¤ttime );
303 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
305 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
308 bv.bv_len = strlen( bv.bv_val );
309 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
310 m->ml_type = ch_strdup( "modifytimestamp" );
311 m->ml_op = LDAP_MOD_REPLACE;
312 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
313 m->ml_bvalues[0] = ber_bvdup( &bv );
314 m->ml_next = *modlist;
327 for ( ; ml != NULL; ml = next ) {
331 if ( ml->ml_bvalues != NULL )
332 ber_bvecfree( ml->ml_bvalues );