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>
30 #ifndef SLAPD_SCHEMA_NOT_COMPAT
31 static int add_modified_attrs( Operation *op, Modifications **modlist );
44 LDAPModList *modlist = NULL;
45 LDAPModList **modtail = &modlist;
49 Modifications *mods = NULL;
53 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
55 if( op->o_bind_in_progress ) {
56 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
58 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
59 NULL, "SASL bind in progress", NULL, NULL );
60 return LDAP_SASL_BIND_IN_PROGRESS;
64 * Parse the modify request. It looks like this:
66 * ModifyRequest := [APPLICATION 6] SEQUENCE {
67 * name DistinguishedName,
68 * mods SEQUENCE OF SEQUENCE {
69 * operation ENUMERATED {
74 * modification SEQUENCE {
76 * values SET OF AttributeValue
82 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
83 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
84 send_ldap_disconnect( conn, op,
85 LDAP_PROTOCOL_ERROR, "decoding error" );
89 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
91 ndn = ch_strdup( dn );
93 if( dn_normalize( ndn ) == NULL ) {
94 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
95 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
96 "invalid DN", NULL, NULL );
100 /* collect modifications & save for later */
102 for ( tag = ber_first_element( op->o_ber, &len, &last );
104 tag = ber_next_element( op->o_ber, &len, last ) )
108 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
110 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
111 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
114 send_ldap_disconnect( conn, op,
115 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
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 Debug( LDAP_DEBUG_ANY,
127 "do_modify: invalid modify operation (%ld)\n",
128 (long) (*modtail)->ml_op, 0, 0 );
129 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
130 NULL, "unrecognized modify operation", NULL, NULL );
131 rc = LDAP_PROTOCOL_ERROR;
135 if ( (*modtail)->ml_bvalues == NULL && (
136 (*modtail)->ml_op != LDAP_MOD_REPLACE &&
137 (*modtail)->ml_op != LDAP_MOD_DELETE ) )
139 Debug( LDAP_DEBUG_ANY,
140 "do_modify: invalid modify operation (%ld) without values\n",
141 (long) (*modtail)->ml_op, 0, 0 );
142 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
143 NULL, "unrecognized modify operation without values",
145 rc = LDAP_PROTOCOL_ERROR;
149 #ifndef SLAPD_SCHEMA_NOT_COMPAT
150 attr_normalize( (*modtail)->ml_type );
153 modtail = &(*modtail)->ml_next;
157 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
158 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
163 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
164 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
165 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
166 tmp->ml_op == LDAP_MOD_ADD
167 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
168 ? "delete" : "replace"), tmp->ml_type, 0 );
173 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
174 op->o_connid, op->o_opid, dn, 0, 0 );
177 * We could be serving multiple database backends. Select the
178 * appropriate one, or send a referral to our "referral server"
179 * if we don't hold it.
181 if ( (be = select_backend( ndn )) == NULL ) {
182 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
183 NULL, NULL, default_referral, NULL );
187 /* make sure this backend recongizes critical controls */
188 rc = backend_check_controls( be, conn, op ) ;
190 if( rc != LDAP_SUCCESS ) {
191 send_ldap_result( conn, op, rc,
192 NULL, NULL, NULL, NULL );
196 if ( global_readonly || be->be_readonly ) {
197 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
199 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
200 NULL, "database is read-only", NULL, NULL );
204 /* deref suffix alias if appropriate */
205 ndn = suffix_alias( be, ndn );
208 * do the modify if 1 && (2 || 3)
209 * 1) there is a modify function implemented in this backend;
210 * 2) this backend is master for what it holds;
211 * 3) it's a replica and the dn supplied is the update_ndn.
213 if ( be->be_modify ) {
214 /* do the update here */
215 #ifndef SLAPD_MULTIMASTER
216 /* we don't have to check for replicator dn
217 * because we accept each modify request
219 if ( be->be_update_ndn == NULL ||
220 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
223 int update = be->be_update_ndn != NULL;
224 #ifdef SLAPD_SCHEMA_NOT_COMPAT
226 rc = slap_modlist2mods( modlist, update, &mods, &text );
228 if( rc != LDAP_SUCCESS ) {
229 send_ldap_result( conn, op, rc,
230 NULL, text, NULL, NULL );
238 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
239 global_lastmod == ON)) && !update )
241 #ifdef SLAPD_SCHEMA_NOT_COMPAT
242 rc = slap_mods_opattrs( op, &mods, &text );
244 char *text = "no-user-modification attribute type";
245 rc = add_modified_attrs( op, &mods );
248 if( rc != LDAP_SUCCESS ) {
249 send_ldap_result( conn, op, rc,
256 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
257 #ifdef SLAPD_MULTIMASTER
258 && ( be->be_update_ndn == NULL ||
259 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
262 /* but we log only the ones not from a replicator user */
263 replog( be, op, dn, mods );
266 #ifndef SLAPD_MULTIMASTER
267 /* send a referral */
269 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
270 be->be_update_refs ? be->be_update_refs : default_referral,
275 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
276 NULL, "Function not implemented", NULL, NULL );
282 if ( modlist != NULL )
283 slap_modlist_free( modlist );
285 slap_mods_free( mods );
289 #ifdef SLAPD_SCHEMA_NOT_COMPAT
291 * convert a raw list of modifications to internal format
292 * Do basic attribute type checking and syntax validation.
294 int slap_modlist2mods(
297 Modifications **mods,
301 Modifications **modtail = mods;
303 for( ; ml != NULL; ml = ml->ml_next ) {
305 AttributeDescription *ad;
307 mod = (Modifications *)
308 ch_calloc( 1, sizeof(Modifications) );
312 /* convert to attribute description */
313 rc = slap_str2ad( ml->ml_type, &ad, text );
315 if( rc != LDAP_SUCCESS ) {
316 slap_mods_free( mod );
320 if((ad->ad_type->sat_syntax->ssyn_flags & SLAP_SYNTAX_BINARY)
321 && !( ad->ad_flags & SLAP_DESC_BINARY ))
323 /* attribute requires binary transfer */
324 slap_mods_free( mod );
325 *text = "attribute requires ;binary transfer";
326 return LDAP_UNDEFINED_TYPE;
329 if (!update && is_at_no_user_mod( ad->ad_type )) {
330 /* user modification disallowed */
331 slap_mods_free( mod );
332 *text = "no user modification allowed";
333 return LDAP_CONSTRAINT_VIOLATION;
339 if( ml->ml_bvalues != NULL ) {
341 slap_syntax_validate_func *validate =
342 ad->ad_type->sat_syntax->ssyn_validate;
345 * check that each value is valid per syntax
347 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
348 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
351 slap_mods_free( mod );
352 *text = "value contains invalid data";
353 return LDAP_INVALID_SYNTAX;
358 * a rough single value check... an additional check is needed
359 * to catch add of single value to existing single valued attribute
361 if( ( ml->ml_op == LDAP_MOD_ADD || ml->ml_op == LDAP_MOD_REPLACE )
362 && nvals > 1 && is_at_single_value( ad->ad_type ))
364 slap_mods_free( mod );
365 *text = "multiple values provided";
366 return LDAP_INVALID_SYNTAX;
370 mod->sml_bvalues = ml->ml_bvalues;
371 ml->ml_values = NULL;
374 modtail = &mod->sml_next;
380 int slap_mods_opattrs(
382 Modifications **modlist,
385 /* not yet implemented */
391 add_modified_attrs( Operation *op, Modifications **modlist )
395 struct berval *bvals[2];
403 /* remove any attempts by the user to modify these attrs */
404 for ( m = *modlist; m != NULL; m = m->ml_next ) {
405 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
406 return LDAP_CONSTRAINT_VIOLATION;
410 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
411 bv.bv_val = "<anonymous>";
412 bv.bv_len = sizeof("<anonymous>")-1;
414 bv.bv_val = op->o_dn;
415 bv.bv_len = strlen( bv.bv_val );
417 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
418 m->ml_type = ch_strdup( "modifiersname" );
419 m->ml_op = LDAP_MOD_REPLACE;
420 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
421 m->ml_bvalues[0] = ber_bvdup( &bv );
422 m->ml_next = *modlist;
425 currenttime = slap_get_time();
426 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
427 ltm = gmtime( ¤ttime );
428 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
429 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
432 bv.bv_len = strlen( bv.bv_val );
433 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
434 m->ml_type = ch_strdup( "modifytimestamp" );
435 m->ml_op = LDAP_MOD_REPLACE;
436 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
437 m->ml_bvalues[0] = ber_bvdup( &bv );
438 m->ml_next = *modlist;
451 #ifdef SLAPD_SCHEMA_NOT_COMPAT
452 ad_free( &mod->sm_desc, 0 );
455 free( mod->sm_desc );
459 if ( mod->sm_bvalues != NULL )
460 ber_bvecfree( mod->sm_bvalues );
473 for ( ; ml != NULL; ml = next ) {
476 slap_mod_free( &ml->sml_mod, 0 );
488 for ( ; ml != NULL; ml = next ) {
494 if ( ml->ml_bvalues != NULL )
495 ber_bvecfree( ml->ml_bvalues );