3 * Copyright 1998-2002 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>
36 struct berval dn = { 0, NULL };
37 struct berval pdn = { 0, NULL };
38 struct berval ndn = { 0, NULL };
42 Modifications *modlist = NULL;
43 Modifications **modtail = &modlist;
53 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
54 "do_modify: enter\n" ));
56 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
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, "{m" /*}*/, &dn ) == LBER_ERROR ) {
80 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
81 "do_modify: ber_scanf failed\n" ));
83 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
86 send_ldap_disconnect( conn, op,
87 LDAP_PROTOCOL_ERROR, "decoding error" );
88 return SLAPD_DISCONNECT;
92 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
93 "do_modify: dn (%s)\n", dn.bv_val ));
95 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
99 /* collect modifications & save for later */
101 for ( tag = ber_first_element( op->o_ber, &len, &last );
103 tag = ber_next_element( op->o_ber, &len, last ) )
106 Modifications tmp, *mod;
109 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
110 &tmp.sml_type, &tmp.sml_bvalues )
113 send_ldap_disconnect( conn, op,
114 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
115 rc = SLAPD_DISCONNECT;
119 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
121 mod->sml_type = tmp.sml_type;
122 mod->sml_bvalues = tmp.sml_bvalues;
123 mod->sml_desc = NULL;
129 if ( mod->sml_bvalues == NULL ) {
131 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
132 "do_modify: modify/add operation (%ld) requires values\n",
135 Debug( LDAP_DEBUG_ANY,
136 "do_modify: modify/add operation (%ld) requires values\n",
140 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
141 NULL, "modify/add operation requires values",
143 rc = LDAP_PROTOCOL_ERROR;
149 case LDAP_MOD_DELETE:
150 case LDAP_MOD_REPLACE:
155 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
156 "do_modify: invalid modify operation (%ld)\n",
159 Debug( LDAP_DEBUG_ANY,
160 "do_modify: invalid modify operation (%ld)\n",
164 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
165 NULL, "unrecognized modify operation", NULL, NULL );
166 rc = LDAP_PROTOCOL_ERROR;
171 modtail = &mod->sml_next;
175 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
177 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
178 "do_modify: get_ctrls failed\n" ));
180 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
186 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
187 if( rc != LDAP_SUCCESS ) {
189 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
190 "do_modify: conn %d invalid dn (%s)\n",
191 conn->c_connid, dn.bv_val ));
193 Debug( LDAP_DEBUG_ANY,
194 "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
196 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
197 "invalid DN", NULL, NULL );
201 if( ndn.bv_len == 0 ) {
203 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
204 "do_modify: attempt to modify root DSE.\n" ));
206 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
209 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
210 NULL, "modify upon the root DSE not supported", NULL, NULL );
213 #if defined( SLAPD_SCHEMA_DN )
214 } else if ( strcasecmp( ndn.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
216 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
217 "do_modify: attempt to modify subschema subentry.\n" ));
219 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
222 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
223 NULL, "modification of subschema subentry not supported",
231 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
232 "do_modify: modifications:\n" ));
234 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
237 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
239 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
240 "\t%s: %s\n", tmp->sml_op == LDAP_MOD_ADD ?
241 "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
242 "delete" : "replace"), tmp->sml_type.bv_val ));
244 if ( tmp->sml_bvalues == NULL ) {
245 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
247 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
248 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
249 "\t\tzero values" ));
250 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
251 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
254 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
255 "\t\tmultiple values" ));
259 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
260 tmp->sml_op == LDAP_MOD_ADD
261 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
262 ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
264 if ( tmp->sml_bvalues == NULL ) {
265 Debug( LDAP_DEBUG_ARGS, "%s\n",
266 "\t\tno values", NULL, NULL );
267 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
268 Debug( LDAP_DEBUG_ARGS, "%s\n",
269 "\t\tzero values", NULL, NULL );
270 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
271 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
272 "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
274 Debug( LDAP_DEBUG_ARGS, "%s\n",
275 "\t\tmultiple values", NULL, NULL );
281 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
282 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
284 manageDSAit = get_manageDSAit( op );
287 * We could be serving multiple database backends. Select the
288 * appropriate one, or send a referral to our "referral server"
289 * if we don't hold it.
291 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
292 BerVarray ref = referral_rewrite( default_referral,
293 NULL, &pdn, LDAP_SCOPE_DEFAULT );
295 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
296 NULL, NULL, ref ? ref : default_referral, NULL );
298 ber_bvarray_free( ref );
302 /* check restrictions */
303 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
304 if( rc != LDAP_SUCCESS ) {
305 send_ldap_result( conn, op, rc,
306 NULL, text, NULL, NULL );
310 /* check for referrals */
311 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
312 if ( rc != LDAP_SUCCESS ) {
316 /* deref suffix alias if appropriate */
317 suffix_alias( be, &ndn );
320 * do the modify if 1 && (2 || 3)
321 * 1) there is a modify function implemented in this backend;
322 * 2) this backend is master for what it holds;
323 * 3) it's a replica and the dn supplied is the update_ndn.
325 if ( be->be_modify ) {
326 /* do the update here */
327 int repl_user = be_isupdate( be, &op->o_ndn );
328 #ifndef SLAPD_MULTIMASTER
329 /* Multimaster slapd does not have to check for replicator dn
330 * because it accepts each modify request
332 if ( !be->be_update_ndn.bv_len || repl_user )
335 int update = be->be_update_ndn.bv_len;
337 char textbuf[SLAP_TEXT_BUFLEN];
338 size_t textlen = sizeof textbuf;
340 rc = slap_mods_check( modlist, update, &text,
343 if( rc != LDAP_SUCCESS ) {
344 send_ldap_result( conn, op, rc,
345 NULL, text, NULL, NULL );
349 if ( SLAP_LASTMOD(be) && !repl_user ) {
350 for( modtail = &modlist;
352 modtail = &(*modtail)->sml_next )
357 rc = slap_mods_opattrs( op, modlist, modtail, &text,
359 if( rc != LDAP_SUCCESS ) {
360 send_ldap_result( conn, op, rc,
367 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
368 #ifdef SLAPD_MULTIMASTER
372 /* but we log only the ones not from a replicator user */
373 replog( be, op, &pdn, &ndn, modlist );
376 #ifndef SLAPD_MULTIMASTER
377 /* send a referral */
379 BerVarray defref = be->be_update_refs
380 ? be->be_update_refs : default_referral;
381 BerVarray ref = referral_rewrite( defref,
382 NULL, &pdn, LDAP_SCOPE_DEFAULT );
384 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
385 ref ? ref : defref, NULL );
387 ber_bvarray_free( ref );
391 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
392 NULL, "operation not supported within namingContext",
399 if ( modlist != NULL )
400 slap_mods_free( modlist );
405 * Do basic attribute type checking and syntax validation.
416 for( ; ml != NULL; ml = ml->sml_next ) {
417 AttributeDescription *ad = NULL;
419 /* convert to attribute description */
420 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
422 if( rc != LDAP_SUCCESS ) {
423 snprintf( textbuf, textlen, "%s: %s",
424 ml->sml_type.bv_val, *text );
431 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
432 && !slap_ad_is_binary( ad ))
434 /* attribute requires binary transfer */
435 snprintf( textbuf, textlen,
436 "%s: requires ;binary transfer",
437 ml->sml_type.bv_val );
439 return LDAP_UNDEFINED_TYPE;
442 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
443 && slap_ad_is_binary( ad ))
445 /* attribute requires binary transfer */
446 snprintf( textbuf, textlen,
447 "%s: disallows ;binary transfer",
448 ml->sml_type.bv_val );
450 return LDAP_UNDEFINED_TYPE;
453 if( slap_ad_is_lang_range( ad )) {
454 /* attribute requires binary transfer */
455 snprintf( textbuf, textlen,
456 "%s: inappropriate use of language range option",
457 ml->sml_type.bv_val );
459 return LDAP_UNDEFINED_TYPE;
462 if (!update && is_at_no_user_mod( ad->ad_type )) {
463 /* user modification disallowed */
464 snprintf( textbuf, textlen,
465 "%s: no user modification allowed",
466 ml->sml_type.bv_val );
468 return LDAP_CONSTRAINT_VIOLATION;
471 if ( is_at_obsolete( ad->ad_type ) &&
472 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
475 * attribute is obsolete,
476 * only allow replace/delete with no values
478 snprintf( textbuf, textlen,
479 "%s: attribute is obsolete",
480 ml->sml_type.bv_val );
482 return LDAP_CONSTRAINT_VIOLATION;
488 if( ml->sml_bvalues != NULL ) {
490 slap_syntax_validate_func *validate =
491 ad->ad_type->sat_syntax->ssyn_validate;
492 slap_syntax_transform_func *pretty =
493 ad->ad_type->sat_syntax->ssyn_pretty;
495 if( !pretty && !validate ) {
496 *text = "no validator for syntax";
497 snprintf( textbuf, textlen,
498 "%s: no validator for syntax %s",
500 ad->ad_type->sat_syntax->ssyn_oid );
502 return LDAP_INVALID_SYNTAX;
506 * check that each value is valid per syntax
507 * and pretty if appropriate
509 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
512 rc = pretty( ad->ad_type->sat_syntax,
513 &ml->sml_bvalues[nvals], &pval );
515 rc = validate( ad->ad_type->sat_syntax,
516 &ml->sml_bvalues[nvals] );
520 snprintf( textbuf, textlen,
521 "%s: value #%ld invalid per syntax",
522 ml->sml_type.bv_val, (long) nvals );
524 return LDAP_INVALID_SYNTAX;
528 ber_memfree( ml->sml_bvalues[nvals].bv_val );
529 ml->sml_bvalues[nvals] = pval;
534 * a rough single value check... an additional check is needed
535 * to catch add of single value to existing single valued attribute
537 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
538 && nvals > 1 && is_at_single_value( ad->ad_type ))
540 snprintf( textbuf, textlen,
541 "%s: multiple value provided",
542 ml->sml_type.bv_val );
544 return LDAP_CONSTRAINT_VIOLATION;
552 int slap_mods_opattrs(
555 Modifications **modtail,
557 char *textbuf, size_t textlen )
559 struct berval name, timestamp, csn;
560 time_t now = slap_get_time();
566 int mop = op->o_tag == LDAP_REQ_ADD
567 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
569 assert( modtail != NULL );
570 assert( *modtail == NULL );
572 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
573 ltm = gmtime( &now );
574 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
576 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
577 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
580 timestamp.bv_val = timebuf;
581 timestamp.bv_len = strlen(timebuf);
583 if( op->o_dn.bv_len == 0 ) {
584 name.bv_val = SLAPD_ANONYMOUS;
585 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
590 if( op->o_tag == LDAP_REQ_ADD ) {
591 struct berval tmpval;
595 rc = mods_structural_class( mods, &tmpval, text, textbuf, textlen );
596 if( rc != LDAP_SUCCESS ) {
599 if ( tmpval.bv_len ) {
600 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
602 mod->sml_type.bv_val = NULL;
603 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
604 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
605 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
606 mod->sml_bvalues[1].bv_val = NULL;
607 assert( mod->sml_bvalues[0].bv_val );
609 modtail = &mod->sml_next;
612 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
613 tmpval.bv_val = uuidbuf;
615 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
617 mod->sml_type.bv_val = NULL;
618 mod->sml_desc = slap_schema.si_ad_entryUUID;
619 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
620 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
621 mod->sml_bvalues[1].bv_val = NULL;
622 assert( mod->sml_bvalues[0].bv_val );
624 modtail = &mod->sml_next;
626 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
628 mod->sml_type.bv_val = NULL;
629 mod->sml_desc = slap_schema.si_ad_creatorsName;
630 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
631 ber_dupbv( &mod->sml_bvalues[0], &name );
632 mod->sml_bvalues[1].bv_val = NULL;
633 assert( mod->sml_bvalues[0].bv_val );
635 modtail = &mod->sml_next;
637 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
639 mod->sml_type.bv_val = NULL;
640 mod->sml_desc = slap_schema.si_ad_createTimestamp;
641 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
642 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
643 mod->sml_bvalues[1].bv_val = NULL;
644 assert( mod->sml_bvalues[0].bv_val );
646 modtail = &mod->sml_next;
649 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
651 mod->sml_type.bv_val = NULL;
652 mod->sml_desc = slap_schema.si_ad_entryCSN;
653 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
654 ber_dupbv( &mod->sml_bvalues[0], &csn );
655 mod->sml_bvalues[1].bv_val = NULL;
656 assert( mod->sml_bvalues[0].bv_val );
658 modtail = &mod->sml_next;
660 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
662 mod->sml_type.bv_val = NULL;
663 mod->sml_desc = slap_schema.si_ad_modifiersName;
664 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
665 ber_dupbv( &mod->sml_bvalues[0], &name );
666 mod->sml_bvalues[1].bv_val = NULL;
667 assert( mod->sml_bvalues[0].bv_val );
669 modtail = &mod->sml_next;
671 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
673 mod->sml_type.bv_val = NULL;
674 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
675 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
676 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
677 mod->sml_bvalues[1].bv_val = NULL;
678 assert( mod->sml_bvalues[0].bv_val );
680 modtail = &mod->sml_next;