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 /* deref suffix alias if appropriate */
182 ndn = suffix_alias( be, ndn );
185 * do the modify if 1 && (2 || 3)
186 * 1) there is a modify function implemented in this backend;
187 * 2) this backend is master for what it holds;
188 * 3) it's a replica and the dn supplied is the update_ndn.
190 if ( be->be_modify ) {
191 /* do the update here */
192 #ifndef SLAPD_MULTIMASTER
193 /* we don't have to check for replicator dn
194 * because we accept each modify request
196 if ( be->be_update_ndn == NULL ||
197 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
200 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
201 global_lastmod == ON)) && be->be_update_ndn == NULL )
203 rc = add_modified_attrs( op, &modlist );
205 if( rc != LDAP_SUCCESS ) {
207 modlist_free( modlist );
208 send_ldap_result( conn, op, rc,
209 NULL, "no-user-modification attribute type",
215 if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0
216 #ifdef SLAPD_MULTIMASTER
217 && ( be->be_update_ndn == NULL ||
218 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
221 /* but we log only the ones not from a replicator user */
222 replog( be, op, ndn, modlist );
225 #ifndef SLAPD_MULTIMASTER
226 /* send a referral */
228 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
229 be->be_update_refs ? be->be_update_refs : default_referral,
234 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
235 NULL, "Function not implemented", NULL, NULL );
239 modlist_free( modlist );
244 add_modified_attrs( Operation *op, LDAPModList **modlist )
248 struct berval *bvals[2];
256 /* remove any attempts by the user to modify these attrs */
257 for ( m = *modlist; m != NULL; m = m->ml_next ) {
258 if ( oc_check_no_usermod_attr( m->ml_type ) ) {
259 return LDAP_CONSTRAINT_VIOLATION;
263 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
264 bv.bv_val = "NULLDN";
265 bv.bv_len = strlen( bv.bv_val );
267 bv.bv_val = op->o_dn;
268 bv.bv_len = strlen( bv.bv_val );
270 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
271 m->ml_type = ch_strdup( "modifiersname" );
272 m->ml_op = LDAP_MOD_REPLACE;
273 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
274 m->ml_bvalues[0] = ber_bvdup( &bv );
275 m->ml_next = *modlist;
278 currenttime = slap_get_time();
279 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
280 #ifndef LDAP_LOCALTIME
281 ltm = gmtime( ¤ttime );
282 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
284 ltm = localtime( ¤ttime );
285 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
287 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
290 bv.bv_len = strlen( bv.bv_val );
291 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
292 m->ml_type = ch_strdup( "modifytimestamp" );
293 m->ml_op = LDAP_MOD_REPLACE;
294 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
295 m->ml_bvalues[0] = ber_bvdup( &bv );
296 m->ml_next = *modlist;
309 for ( ; ml != NULL; ml = next ) {
313 if ( ml->ml_bvalues != NULL )
314 ber_bvecfree( ml->ml_bvalues );