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>
29 static void modlist_free(LDAPModList *ml);
31 static int add_modified_attrs( Operation *op, LDAPModList **modlist );
43 LDAPModList *modlist = NULL;
44 LDAPModList **modtail = &modlist;
51 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
53 if( op->o_bind_in_progress ) {
54 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
56 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
57 NULL, "SASL bind in progress", NULL, NULL );
58 return LDAP_SASL_BIND_IN_PROGRESS;
62 * Parse the modify request. It looks like this:
64 * ModifyRequest := [APPLICATION 6] SEQUENCE {
65 * name DistinguishedName,
66 * mods SEQUENCE OF SEQUENCE {
67 * operation ENUMERATED {
72 * modification SEQUENCE {
74 * values SET OF AttributeValue
80 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
81 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
82 send_ldap_disconnect( conn, op,
83 LDAP_PROTOCOL_ERROR, "decoding error" );
87 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
89 ndn = ch_strdup( dn );
91 if( dn_normalize( ndn ) == NULL ) {
92 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
93 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
94 "invalid DN", NULL, NULL );
98 /* 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" );
118 (*modtail)->ml_op = mop;
120 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
121 (*modtail)->ml_op != LDAP_MOD_DELETE &&
122 (*modtail)->ml_op != LDAP_MOD_REPLACE )
124 Debug( LDAP_DEBUG_ANY,
125 "do_modify: invalid modify operation (%ld)\n",
126 (long) (*modtail)->ml_op, 0, 0 );
127 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
128 NULL, "unrecognized modify operation", NULL, NULL );
129 rc = LDAP_PROTOCOL_ERROR;
133 if ( (*modtail)->ml_bvalues == NULL && (
134 (*modtail)->ml_op != LDAP_MOD_REPLACE &&
135 (*modtail)->ml_op != LDAP_MOD_DELETE ) )
137 Debug( LDAP_DEBUG_ANY,
138 "do_modify: invalid modify operation (%ld) without values\n",
139 (long) (*modtail)->ml_op, 0, 0 );
140 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
141 NULL, "unrecognized modify operation without values",
143 rc = LDAP_PROTOCOL_ERROR;
147 #ifndef SLAPD_SCHEMA_NOT_COMPAT
148 attr_normalize( (*modtail)->ml_type );
151 modtail = &(*modtail)->ml_next;
156 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
157 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
158 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
159 tmp->ml_op == LDAP_MOD_ADD
160 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
161 ? "delete" : "replace"), tmp->ml_type, 0 );
165 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
166 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
170 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
171 op->o_connid, op->o_opid, dn, 0, 0 );
174 * We could be serving multiple database backends. Select the
175 * appropriate one, or send a referral to our "referral server"
176 * if we don't hold it.
178 if ( (be = select_backend( ndn )) == NULL ) {
179 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
180 NULL, NULL, default_referral, NULL );
184 /* make sure this backend recongizes critical controls */
185 rc = backend_check_controls( be, conn, op ) ;
187 if( rc != LDAP_SUCCESS ) {
188 send_ldap_result( conn, op, rc,
189 NULL, NULL, NULL, NULL );
193 if ( global_readonly || be->be_readonly ) {
194 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
196 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
197 NULL, "database is read-only", NULL, NULL );
201 /* deref suffix alias if appropriate */
202 ndn = suffix_alias( be, ndn );
205 * do the modify if 1 && (2 || 3)
206 * 1) there is a modify function implemented in this backend;
207 * 2) this backend is master for what it holds;
208 * 3) it's a replica and the dn supplied is the update_ndn.
210 if ( be->be_modify ) {
211 /* do the update here */
212 #ifndef SLAPD_MULTIMASTER
213 /* we don't have to check for replicator dn
214 * because we accept each modify request
216 if ( be->be_update_ndn == NULL ||
217 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
220 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
221 global_lastmod == ON)) && be->be_update_ndn == NULL )
223 rc = add_modified_attrs( op, &modlist );
225 if( rc != LDAP_SUCCESS ) {
226 send_ldap_result( conn, op, rc,
227 NULL, "no-user-modification attribute type",
233 if ( (*be->be_modify)( be, conn, op, dn, ndn, modlist ) == 0
234 #ifdef SLAPD_MULTIMASTER
235 && ( be->be_update_ndn == NULL ||
236 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
239 /* but we log only the ones not from a replicator user */
240 replog( be, op, dn, modlist );
243 #ifndef SLAPD_MULTIMASTER
244 /* send a referral */
246 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
247 be->be_update_refs ? be->be_update_refs : default_referral,
252 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
253 NULL, "Function not implemented", NULL, NULL );
259 if ( modlist != NULL )
260 modlist_free( modlist );
265 add_modified_attrs( Operation *op, LDAPModList **modlist )
269 struct berval *bvals[2];
277 /* remove any attempts by the user to modify these attrs */
278 for ( m = *modlist; m != NULL; m = m->ml_next ) {
279 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
280 return LDAP_CONSTRAINT_VIOLATION;
284 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
285 bv.bv_val = "NULLDN";
286 bv.bv_len = strlen( bv.bv_val );
288 bv.bv_val = op->o_dn;
289 bv.bv_len = strlen( bv.bv_val );
291 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
292 m->ml_type = ch_strdup( "modifiersname" );
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;
299 currenttime = slap_get_time();
300 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
301 ltm = gmtime( ¤ttime );
302 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
303 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
306 bv.bv_len = strlen( bv.bv_val );
307 m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
308 m->ml_type = ch_strdup( "modifytimestamp" );
309 m->ml_op = LDAP_MOD_REPLACE;
310 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
311 m->ml_bvalues[0] = ber_bvdup( &bv );
312 m->ml_next = *modlist;
325 for ( ; ml != NULL; ml = next ) {
331 if ( ml->ml_bvalues != NULL )
332 ber_bvecfree( ml->ml_bvalues );