2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1995 Regents of the University of Michigan.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that this notice is preserved and that due credit is given
11 * to the University of Michigan at Ann Arbor. The name of the University
12 * may not be used to endorse or promote products derived from this
13 * software without specific prior written permission. This software
14 * is provided ``as is'' without express or implied warranty.
21 #include <ac/socket.h>
22 #include <ac/string.h>
27 static void modlist_free(LDAPModList *ml);
29 static int add_modified_attrs( Operation *op, LDAPModList **modlist );
42 LDAPModList **modtail;
49 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
51 if( op->o_bind_in_progress ) {
52 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
54 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
55 NULL, "SASL bind in progress", NULL, NULL );
56 return LDAP_SASL_BIND_IN_PROGRESS;
60 * Parse the modify request. It looks like this:
62 * ModifyRequest := [APPLICATION 6] SEQUENCE {
63 * name DistinguishedName,
64 * mods SEQUENCE OF SEQUENCE {
65 * operation ENUMERATED {
70 * modification SEQUENCE {
72 * values SET OF AttributeValue
78 if ( ber_scanf( op->o_ber, "{a" /*}*/, &ndn ) == LBER_ERROR ) {
79 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
80 send_ldap_disconnect( conn, op,
81 LDAP_PROTOCOL_ERROR, "decoding error" );
85 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
87 if( dn_normalize_case( ndn ) == NULL ) {
88 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", ndn, 0, 0 );
89 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
90 "invalid DN", NULL, NULL );
95 /* collect modifications & save for later */
99 for ( tag = ber_first_element( op->o_ber, &len, &last );
101 tag = ber_next_element( op->o_ber, &len, last ) )
105 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
107 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
108 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
111 send_ldap_disconnect( conn, op,
112 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
116 modlist_free( modlist );
120 (*modtail)->ml_op = mop;
122 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
123 (*modtail)->ml_op != LDAP_MOD_DELETE &&
124 (*modtail)->ml_op != LDAP_MOD_REPLACE )
126 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
127 NULL, "unrecognized modify operation", NULL, NULL );
129 modlist_free( modlist );
130 return LDAP_PROTOCOL_ERROR;
133 if ( (*modtail)->ml_bvalues == NULL
134 && (*modtail)->ml_op != LDAP_MOD_DELETE )
136 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
137 NULL, "unrecognized modify operation", NULL, NULL );
139 modlist_free( modlist );
140 return LDAP_PROTOCOL_ERROR;
142 attr_normalize( (*modtail)->ml_type );
144 modtail = &(*modtail)->ml_next;
149 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
150 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
151 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
152 tmp->ml_op == LDAP_MOD_ADD
153 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
154 ? "delete" : "replace"), tmp->ml_type, 0 );
158 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
160 modlist_free( modlist );
161 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
165 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
166 op->o_connid, op->o_opid, ndn, 0, 0 );
169 * We could be serving multiple database backends. Select the
170 * appropriate one, or send a referral to our "referral server"
171 * if we don't hold it.
173 if ( (be = select_backend( ndn )) == NULL ) {
175 modlist_free( modlist );
176 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
177 NULL, NULL, default_referral, NULL );
181 if ( global_readonly || be->be_readonly ) {
182 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
184 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
185 NULL, "database is read-only", NULL, NULL );
189 /* deref suffix alias if appropriate */
190 ndn = suffix_alias( be, ndn );
193 * do the modify if 1 && (2 || 3)
194 * 1) there is a modify function implemented in this backend;
195 * 2) this backend is master for what it holds;
196 * 3) it's a replica and the dn supplied is the update_ndn.
198 if ( be->be_modify ) {
199 /* do the update here */
200 #ifndef SLAPD_MULTIMASTER
201 /* we don't have to check for replicator dn
202 * because we accept each modify request
204 if ( be->be_update_ndn == NULL ||
205 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
208 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
209 global_lastmod == ON)) && be->be_update_ndn == NULL )
211 rc = add_modified_attrs( op, &modlist );
213 if( rc != LDAP_SUCCESS ) {
215 modlist_free( modlist );
216 send_ldap_result( conn, op, rc,
217 NULL, "no-user-modification attribute type",
223 if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0
224 #ifdef SLAPD_MULTIMASTER
225 && ( be->be_update_ndn == NULL ||
226 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
229 /* but we log only the ones not from a replicator user */
230 replog( be, op, ndn, modlist );
233 #ifndef SLAPD_MULTIMASTER
234 /* send a referral */
236 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
237 be->be_update_refs ? be->be_update_refs : default_referral,
242 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
243 NULL, "Function not implemented", NULL, NULL );
248 modlist_free( modlist );
253 add_modified_attrs( Operation *op, LDAPModList **modlist )
257 struct berval *bvals[2];
265 /* remove any attempts by the user to modify these attrs */
266 for ( m = *modlist; m != NULL; m = m->ml_next ) {
267 if ( oc_check_no_usermod_attr( m->ml_type ) ) {
268 return LDAP_CONSTRAINT_VIOLATION;
272 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
273 bv.bv_val = "NULLDN";
274 bv.bv_len = strlen( bv.bv_val );
276 bv.bv_val = op->o_dn;
277 bv.bv_len = strlen( bv.bv_val );
279 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
280 m->ml_type = ch_strdup( "modifiersname" );
281 m->ml_op = LDAP_MOD_REPLACE;
282 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
283 m->ml_bvalues[0] = ber_bvdup( &bv );
284 m->ml_next = *modlist;
287 currenttime = slap_get_time();
288 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
289 #ifndef LDAP_LOCALTIME
290 ltm = gmtime( ¤ttime );
291 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
293 ltm = localtime( ¤ttime );
294 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
296 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
299 bv.bv_len = strlen( bv.bv_val );
300 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
301 m->ml_type = ch_strdup( "modifytimestamp" );
302 m->ml_op = LDAP_MOD_REPLACE;
303 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
304 m->ml_bvalues[0] = ber_bvdup( &bv );
305 m->ml_next = *modlist;
318 for ( ; ml != NULL; ml = next ) {
322 if ( ml->ml_bvalues != NULL )
323 ber_bvecfree( ml->ml_bvalues );