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 );
42 LDAPModList *modlist = NULL;
43 LDAPModList **modtail = &modlist;
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" /*}*/, &dn ) == 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", dn, 0, 0 );
88 ndn = ch_strdup( ndn );
90 if( dn_normalize( ndn ) == NULL ) {
91 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
92 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
93 "invalid DN", NULL, NULL );
97 /* 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" );
117 (*modtail)->ml_op = mop;
119 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
120 (*modtail)->ml_op != LDAP_MOD_DELETE &&
121 (*modtail)->ml_op != LDAP_MOD_REPLACE )
123 Debug( LDAP_DEBUG_ANY,
124 "do_modify: invalid modify operation (%ld)\n",
125 (long) (*modtail)->ml_op, 0, 0 );
126 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
127 NULL, "unrecognized modify operation", NULL, NULL );
128 rc = LDAP_PROTOCOL_ERROR;
132 if ( (*modtail)->ml_bvalues == NULL && (
133 (*modtail)->ml_op != LDAP_MOD_REPLACE &&
134 (*modtail)->ml_op != LDAP_MOD_DELETE ) )
136 Debug( LDAP_DEBUG_ANY,
137 "do_modify: invalid modify operation (%ld) without values\n",
138 (long) (*modtail)->ml_op, 0, 0 );
139 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
140 NULL, "unrecognized modify operation without values",
142 rc = LDAP_PROTOCOL_ERROR;
145 attr_normalize( (*modtail)->ml_type );
147 modtail = &(*modtail)->ml_next;
152 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
153 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
154 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
155 tmp->ml_op == LDAP_MOD_ADD
156 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
157 ? "delete" : "replace"), tmp->ml_type, 0 );
161 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
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, dn, 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 ) {
175 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
176 NULL, NULL, default_referral, NULL );
180 if ( global_readonly || be->be_readonly ) {
181 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
183 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
184 NULL, "database is read-only", NULL, NULL );
188 /* deref suffix alias if appropriate */
189 ndn = suffix_alias( be, ndn );
192 * do the modify if 1 && (2 || 3)
193 * 1) there is a modify function implemented in this backend;
194 * 2) this backend is master for what it holds;
195 * 3) it's a replica and the dn supplied is the update_ndn.
197 if ( be->be_modify ) {
198 /* do the update here */
199 #ifndef SLAPD_MULTIMASTER
200 /* we don't have to check for replicator dn
201 * because we accept each modify request
203 if ( be->be_update_ndn == NULL ||
204 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
207 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
208 global_lastmod == ON)) && be->be_update_ndn == NULL )
210 rc = add_modified_attrs( op, &modlist );
212 if( rc != LDAP_SUCCESS ) {
213 send_ldap_result( conn, op, rc,
214 NULL, "no-user-modification attribute type",
220 if ( (*be->be_modify)( be, conn, op, dn, ndn, modlist ) == 0
221 #ifdef SLAPD_MULTIMASTER
222 && ( be->be_update_ndn == NULL ||
223 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
226 /* but we log only the ones not from a replicator user */
227 replog( be, op, dn, modlist );
230 #ifndef SLAPD_MULTIMASTER
231 /* send a referral */
233 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
234 be->be_update_refs ? be->be_update_refs : default_referral,
239 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
240 NULL, "Function not implemented", NULL, NULL );
246 if ( modtail != NULL && *modtail != NULL )
248 if ( modlist != 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 );