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);
30 static void mods_free(Modifications *mods);
32 #ifdef SLAPD_SCHEMA_NOT_COMPAT
33 /* static */ int modlist2mods(
39 static int add_modified_attrs( Operation *op, Modifications **modlist );
51 LDAPModList *modlist = NULL;
52 LDAPModList **modtail = &modlist;
56 Modifications *mods = NULL;
60 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
62 if( op->o_bind_in_progress ) {
63 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
65 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
66 NULL, "SASL bind in progress", NULL, NULL );
67 return LDAP_SASL_BIND_IN_PROGRESS;
71 * Parse the modify request. It looks like this:
73 * ModifyRequest := [APPLICATION 6] SEQUENCE {
74 * name DistinguishedName,
75 * mods SEQUENCE OF SEQUENCE {
76 * operation ENUMERATED {
81 * modification SEQUENCE {
83 * values SET OF AttributeValue
89 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
90 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
91 send_ldap_disconnect( conn, op,
92 LDAP_PROTOCOL_ERROR, "decoding error" );
96 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
98 ndn = ch_strdup( dn );
100 if( dn_normalize( ndn ) == NULL ) {
101 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
102 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
103 "invalid DN", NULL, NULL );
107 /* collect modifications & save for later */
109 for ( tag = ber_first_element( op->o_ber, &len, &last );
111 tag = ber_next_element( op->o_ber, &len, last ) )
115 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
117 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
118 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
121 send_ldap_disconnect( conn, op,
122 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
127 (*modtail)->ml_op = mop;
129 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
130 (*modtail)->ml_op != LDAP_MOD_DELETE &&
131 (*modtail)->ml_op != LDAP_MOD_REPLACE )
133 Debug( LDAP_DEBUG_ANY,
134 "do_modify: invalid modify operation (%ld)\n",
135 (long) (*modtail)->ml_op, 0, 0 );
136 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
137 NULL, "unrecognized modify operation", NULL, NULL );
138 rc = LDAP_PROTOCOL_ERROR;
142 if ( (*modtail)->ml_bvalues == NULL && (
143 (*modtail)->ml_op != LDAP_MOD_REPLACE &&
144 (*modtail)->ml_op != LDAP_MOD_DELETE ) )
146 Debug( LDAP_DEBUG_ANY,
147 "do_modify: invalid modify operation (%ld) without values\n",
148 (long) (*modtail)->ml_op, 0, 0 );
149 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
150 NULL, "unrecognized modify operation without values",
152 rc = LDAP_PROTOCOL_ERROR;
156 #ifndef SLAPD_SCHEMA_NOT_COMPAT
157 attr_normalize( (*modtail)->ml_type );
160 modtail = &(*modtail)->ml_next;
164 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
165 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
170 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
171 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
172 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
173 tmp->ml_op == LDAP_MOD_ADD
174 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
175 ? "delete" : "replace"), tmp->ml_type, 0 );
181 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
182 op->o_connid, op->o_opid, dn, 0, 0 );
185 * We could be serving multiple database backends. Select the
186 * appropriate one, or send a referral to our "referral server"
187 * if we don't hold it.
189 if ( (be = select_backend( ndn )) == NULL ) {
190 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
191 NULL, NULL, default_referral, NULL );
195 /* make sure this backend recongizes critical controls */
196 rc = backend_check_controls( be, conn, op ) ;
198 if( rc != LDAP_SUCCESS ) {
199 send_ldap_result( conn, op, rc,
200 NULL, NULL, NULL, NULL );
204 if ( global_readonly || be->be_readonly ) {
205 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
207 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
208 NULL, "database is read-only", NULL, NULL );
212 /* deref suffix alias if appropriate */
213 ndn = suffix_alias( be, ndn );
216 * do the modify if 1 && (2 || 3)
217 * 1) there is a modify function implemented in this backend;
218 * 2) this backend is master for what it holds;
219 * 3) it's a replica and the dn supplied is the update_ndn.
221 if ( be->be_modify ) {
222 /* do the update here */
223 #ifndef SLAPD_MULTIMASTER
224 /* we don't have to check for replicator dn
225 * because we accept each modify request
227 if ( be->be_update_ndn == NULL ||
228 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
231 #ifdef SLAPD_SCHEMA_NOT_COMPAT
233 rc = modlist2mods( modlist, &mods, &text );
235 if( rc != LDAP_SUCCESS ) {
236 send_ldap_result( conn, op, rc,
237 NULL, text, NULL, NULL );
245 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
246 global_lastmod == ON)) && be->be_update_ndn == NULL )
248 rc = add_modified_attrs( op, &mods );
250 if( rc != LDAP_SUCCESS ) {
251 send_ldap_result( conn, op, rc,
252 NULL, "no-user-modification attribute type",
258 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
259 #ifdef SLAPD_MULTIMASTER
260 && ( be->be_update_ndn == NULL ||
261 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
264 /* but we log only the ones not from a replicator user */
265 replog( be, op, dn, mods );
268 #ifndef SLAPD_MULTIMASTER
269 /* send a referral */
271 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
272 be->be_update_refs ? be->be_update_refs : default_referral,
277 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
278 NULL, "Function not implemented", NULL, NULL );
284 if ( modlist != NULL )
285 modlist_free( modlist );
292 add_modified_attrs( Operation *op, Modifications **modlist )
294 #ifdef SLAPD_SCHEMA_NOT_COMPAT
295 /* not yet implemented */
299 struct berval *bvals[2];
307 /* remove any attempts by the user to modify these attrs */
308 for ( m = *modlist; m != NULL; m = m->ml_next ) {
309 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
310 return LDAP_CONSTRAINT_VIOLATION;
314 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
315 bv.bv_val = "<anonymous>";
316 bv.bv_len = sizeof("<anonymous>")-1;
318 bv.bv_val = op->o_dn;
319 bv.bv_len = strlen( bv.bv_val );
321 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
322 m->ml_type = ch_strdup( "modifiersname" );
323 m->ml_op = LDAP_MOD_REPLACE;
324 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
325 m->ml_bvalues[0] = ber_bvdup( &bv );
326 m->ml_next = *modlist;
329 currenttime = slap_get_time();
330 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
331 ltm = gmtime( ¤ttime );
332 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
333 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
336 bv.bv_len = strlen( bv.bv_val );
337 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
338 m->ml_type = ch_strdup( "modifytimestamp" );
339 m->ml_op = LDAP_MOD_REPLACE;
340 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
341 m->ml_bvalues[0] = ber_bvdup( &bv );
342 m->ml_next = *modlist;
355 #ifdef SLAPD_SCHEMA_NOT_COMPAT
356 ad_free( &mod->sm_desc, 0 );
359 free( mod->sm_desc );
363 if ( mod->sm_bvalues != NULL )
364 ber_bvecfree( mod->sm_bvalues );
377 for ( ; ml != NULL; ml = next ) {
380 mod_free( &ml->sml_mod, 0 );
392 for ( ; ml != NULL; ml = next ) {
398 if ( ml->ml_bvalues != NULL )
399 ber_bvecfree( ml->ml_bvalues );