3 * Copyright 1998-2003 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.
19 #include "slapi_common.h"
23 #include <ac/socket.h>
24 #include <ac/string.h>
38 struct berval dn = { 0, NULL };
39 struct berval pdn = { 0, NULL };
40 struct berval ndn = { 0, NULL };
44 Modifications *modlist = NULL;
45 Modifications **modtail = &modlist;
54 Slapi_PBlock *pb = op->o_pb;
57 LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
59 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
63 * Parse the modify request. It looks like this:
65 * ModifyRequest := [APPLICATION 6] SEQUENCE {
66 * name DistinguishedName,
67 * mods SEQUENCE OF SEQUENCE {
68 * operation ENUMERATED {
73 * modification SEQUENCE {
75 * values SET OF AttributeValue
81 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
83 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
85 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
88 send_ldap_disconnect( conn, op,
89 LDAP_PROTOCOL_ERROR, "decoding error" );
90 return SLAPD_DISCONNECT;
94 LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
96 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
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 ) )
107 Modifications tmp, *mod;
110 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
111 &tmp.sml_type, &tmp.sml_bvalues )
114 send_ldap_disconnect( conn, op,
115 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
116 rc = SLAPD_DISCONNECT;
120 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
122 mod->sml_type = tmp.sml_type;
123 mod->sml_bvalues = tmp.sml_bvalues;
124 mod->sml_desc = NULL;
130 if ( mod->sml_bvalues == NULL ) {
132 LDAP_LOG( OPERATION, ERR,
133 "do_modify: modify/add operation (%ld) requires values\n",
136 Debug( LDAP_DEBUG_ANY,
137 "do_modify: modify/add operation (%ld) requires values\n",
141 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
142 NULL, "modify/add operation requires values",
144 rc = LDAP_PROTOCOL_ERROR;
150 case LDAP_MOD_DELETE:
151 case LDAP_MOD_REPLACE:
156 LDAP_LOG( OPERATION, ERR,
157 "do_modify: invalid modify operation (%ld)\n", (long)mop, 0, 0 );
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, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
179 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
185 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
186 if( rc != LDAP_SUCCESS ) {
188 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d invalid dn (%s)\n",
189 conn->c_connid, dn.bv_val, 0 );
191 Debug( LDAP_DEBUG_ANY,
192 "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
194 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
195 "invalid DN", NULL, NULL );
199 if( ndn.bv_len == 0 ) {
201 LDAP_LOG( OPERATION, ERR,
202 "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
204 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
207 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
208 NULL, "modify upon the root DSE not supported", NULL, NULL );
211 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
213 LDAP_LOG( OPERATION, ERR,
214 "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0 );
216 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
219 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
220 NULL, "modification of subschema subentry not supported",
227 LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0 );
229 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
232 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
234 LDAP_LOG( OPERATION, DETAIL1, "\t%s: %s\n",
235 tmp->sml_op == LDAP_MOD_ADD ?
236 "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
237 "delete" : "replace"), tmp->sml_type.bv_val, 0 );
239 if ( tmp->sml_bvalues == NULL ) {
240 LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
241 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
242 LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
243 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
244 LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
246 LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
250 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
251 tmp->sml_op == LDAP_MOD_ADD
252 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
253 ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
255 if ( tmp->sml_bvalues == NULL ) {
256 Debug( LDAP_DEBUG_ARGS, "%s\n",
257 "\t\tno values", NULL, NULL );
258 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
259 Debug( LDAP_DEBUG_ARGS, "%s\n",
260 "\t\tzero values", NULL, NULL );
261 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
262 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
263 "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
265 Debug( LDAP_DEBUG_ARGS, "%s\n",
266 "\t\tmultiple values", NULL, NULL );
272 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
273 char abuf[BUFSIZ/2], *ptr = abuf;
276 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
277 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
279 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
280 if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
281 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
282 op->o_connid, op->o_opid, abuf, 0, 0 );
290 ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
291 len += tmp->sml_type.bv_len;
294 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
295 op->o_connid, op->o_opid, abuf, 0, 0 );
299 manageDSAit = get_manageDSAit( op );
302 * We could be serving multiple database backends. Select the
303 * appropriate one, or send a referral to our "referral server"
304 * if we don't hold it.
306 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
307 BerVarray ref = referral_rewrite( default_referral,
308 NULL, &pdn, LDAP_SCOPE_DEFAULT );
310 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
311 NULL, NULL, ref ? ref : default_referral, NULL );
313 ber_bvarray_free( ref );
317 /* check restrictions */
318 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
319 if( rc != LDAP_SUCCESS ) {
320 send_ldap_result( conn, op, rc,
321 NULL, text, NULL, NULL );
325 /* check for referrals */
326 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
327 if ( rc != LDAP_SUCCESS ) {
331 /* deref suffix alias if appropriate */
332 suffix_alias( be, &ndn );
334 #if defined( LDAP_SLAPI )
335 slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
336 slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
337 slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
338 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
339 slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
340 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(1) );
342 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
343 if ( rc != 0 && rc != LDAP_OTHER ) {
345 * either there is no preOp (modify) plugins
346 * or a plugin failed. Just log it
348 * FIXME: is this correct?
351 LDAP_LOG( OPERATION, INFO, "do_modify: modify preOps "
352 "failed\n", 0, 0, 0 );
354 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preOps failed.\n",
358 #endif /* defined( LDAP_SLAPI ) */
361 * do the modify if 1 && (2 || 3)
362 * 1) there is a modify function implemented in this backend;
363 * 2) this backend is master for what it holds;
364 * 3) it's a replica and the dn supplied is the update_ndn.
366 if ( be->be_modify ) {
367 /* do the update here */
368 int repl_user = be_isupdate( be, &op->o_ndn );
369 #ifndef SLAPD_MULTIMASTER
370 /* Multimaster slapd does not have to check for replicator dn
371 * because it accepts each modify request
373 if ( !be->be_update_ndn.bv_len || repl_user )
376 int update = be->be_update_ndn.bv_len;
378 char textbuf[SLAP_TEXT_BUFLEN];
379 size_t textlen = sizeof textbuf;
381 rc = slap_mods_check( modlist, update, &text,
384 if( rc != LDAP_SUCCESS ) {
385 send_ldap_result( conn, op, rc,
386 NULL, text, NULL, NULL );
391 for( modtail = &modlist;
393 modtail = &(*modtail)->sml_next )
398 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
400 if( rc != LDAP_SUCCESS ) {
401 send_ldap_result( conn, op, rc,
408 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
409 #ifdef SLAPD_MULTIMASTER
413 /* but we log only the ones not from a replicator user */
414 replog( be, op, &pdn, &ndn, modlist );
417 #ifndef SLAPD_MULTIMASTER
418 /* send a referral */
420 BerVarray defref = be->be_update_refs
421 ? be->be_update_refs : default_referral;
422 BerVarray ref = referral_rewrite( defref,
423 NULL, &pdn, LDAP_SCOPE_DEFAULT );
425 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
426 ref ? ref : defref, NULL );
428 ber_bvarray_free( ref );
432 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
433 NULL, "operation not supported within namingContext",
437 #if defined( LDAP_SLAPI )
438 rc = doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb );
439 if ( rc != 0 && rc != LDAP_OTHER ) {
441 * either there is no postOp (modify) plugins
442 * or a plugin failed. Just log it
444 * FIXME: is this correct?
447 LDAP_LOG( OPERATION, INFO, "do_modify: modify postOps "
448 "failed\n", 0, 0, 0 );
450 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postOps "
451 "failed.\n", 0, 0, 0);
454 #endif /* defined( LDAP_SLAPI ) */
459 if ( modlist != NULL )
460 slap_mods_free( modlist );
465 * Do basic attribute type checking and syntax validation.
476 for( ; ml != NULL; ml = ml->sml_next ) {
477 AttributeDescription *ad = NULL;
479 /* convert to attribute description */
480 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
482 if( rc != LDAP_SUCCESS ) {
483 snprintf( textbuf, textlen, "%s: %s",
484 ml->sml_type.bv_val, *text );
491 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
492 && !slap_ad_is_binary( ad ))
494 /* attribute requires binary transfer */
495 snprintf( textbuf, textlen,
496 "%s: requires ;binary transfer",
497 ml->sml_type.bv_val );
499 return LDAP_UNDEFINED_TYPE;
502 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
503 && slap_ad_is_binary( ad ))
505 /* attribute requires binary transfer */
506 snprintf( textbuf, textlen,
507 "%s: disallows ;binary transfer",
508 ml->sml_type.bv_val );
510 return LDAP_UNDEFINED_TYPE;
513 if( slap_ad_is_tag_range( ad )) {
514 /* attribute requires binary transfer */
515 snprintf( textbuf, textlen,
516 "%s: inappropriate use of tag range option",
517 ml->sml_type.bv_val );
519 return LDAP_UNDEFINED_TYPE;
522 if (!update && is_at_no_user_mod( ad->ad_type )) {
523 /* user modification disallowed */
524 snprintf( textbuf, textlen,
525 "%s: no user modification allowed",
526 ml->sml_type.bv_val );
528 return LDAP_CONSTRAINT_VIOLATION;
531 if ( is_at_obsolete( ad->ad_type ) &&
532 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
535 * attribute is obsolete,
536 * only allow replace/delete with no values
538 snprintf( textbuf, textlen,
539 "%s: attribute is obsolete",
540 ml->sml_type.bv_val );
542 return LDAP_CONSTRAINT_VIOLATION;
548 if( ml->sml_bvalues != NULL ) {
550 slap_syntax_validate_func *validate =
551 ad->ad_type->sat_syntax->ssyn_validate;
552 slap_syntax_transform_func *pretty =
553 ad->ad_type->sat_syntax->ssyn_pretty;
555 if( !pretty && !validate ) {
556 *text = "no validator for syntax";
557 snprintf( textbuf, textlen,
558 "%s: no validator for syntax %s",
560 ad->ad_type->sat_syntax->ssyn_oid );
562 return LDAP_INVALID_SYNTAX;
566 * check that each value is valid per syntax
567 * and pretty if appropriate
569 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
572 rc = pretty( ad->ad_type->sat_syntax,
573 &ml->sml_bvalues[nvals], &pval );
575 rc = validate( ad->ad_type->sat_syntax,
576 &ml->sml_bvalues[nvals] );
580 snprintf( textbuf, textlen,
581 "%s: value #%ld invalid per syntax",
582 ml->sml_type.bv_val, (long) nvals );
584 return LDAP_INVALID_SYNTAX;
588 ber_memfree( ml->sml_bvalues[nvals].bv_val );
589 ml->sml_bvalues[nvals] = pval;
594 * a rough single value check... an additional check is needed
595 * to catch add of single value to existing single valued attribute
597 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
598 && nvals > 1 && is_at_single_value( ad->ad_type ))
600 snprintf( textbuf, textlen,
601 "%s: multiple value provided",
602 ml->sml_type.bv_val );
604 return LDAP_CONSTRAINT_VIOLATION;
612 int slap_mods_opattrs(
616 Modifications **modtail,
618 char *textbuf, size_t textlen )
620 struct berval name, timestamp, csn;
621 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
622 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
625 int mop = op->o_tag == LDAP_REQ_ADD
626 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
628 assert( modtail != NULL );
629 assert( *modtail == NULL );
631 if( SLAP_LASTMOD(be) ) {
633 time_t now = slap_get_time();
635 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
636 ltm = gmtime( &now );
637 lutil_gentime( timebuf, sizeof(timebuf), ltm );
639 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
640 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
643 timestamp.bv_val = timebuf;
644 timestamp.bv_len = strlen(timebuf);
646 if( op->o_dn.bv_len == 0 ) {
647 name.bv_val = SLAPD_ANONYMOUS;
648 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
654 if( op->o_tag == LDAP_REQ_ADD ) {
655 struct berval tmpval;
657 if( global_schemacheck ) {
658 int rc = mods_structural_class( mods, &tmpval,
659 text, textbuf, textlen );
660 if( rc != LDAP_SUCCESS ) {
664 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
666 mod->sml_type.bv_val = NULL;
667 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
668 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
669 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
670 mod->sml_bvalues[1].bv_val = NULL;
671 assert( mod->sml_bvalues[0].bv_val );
673 modtail = &mod->sml_next;
676 if( SLAP_LASTMOD(be) ) {
677 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
679 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
680 tmpval.bv_val = uuidbuf;
682 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
684 mod->sml_type.bv_val = NULL;
685 mod->sml_desc = slap_schema.si_ad_entryUUID;
686 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
687 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
688 mod->sml_bvalues[1].bv_val = NULL;
689 assert( mod->sml_bvalues[0].bv_val );
691 modtail = &mod->sml_next;
693 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
695 mod->sml_type.bv_val = NULL;
696 mod->sml_desc = slap_schema.si_ad_creatorsName;
697 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
698 ber_dupbv( &mod->sml_bvalues[0], &name );
699 mod->sml_bvalues[1].bv_val = NULL;
700 assert( mod->sml_bvalues[0].bv_val );
702 modtail = &mod->sml_next;
704 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
706 mod->sml_type.bv_val = NULL;
707 mod->sml_desc = slap_schema.si_ad_createTimestamp;
708 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
709 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
710 mod->sml_bvalues[1].bv_val = NULL;
711 assert( mod->sml_bvalues[0].bv_val );
713 modtail = &mod->sml_next;
717 if( SLAP_LASTMOD(be) ) {
718 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
720 mod->sml_type.bv_val = NULL;
721 mod->sml_desc = slap_schema.si_ad_entryCSN;
722 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
723 ber_dupbv( &mod->sml_bvalues[0], &csn );
724 mod->sml_bvalues[1].bv_val = NULL;
725 assert( mod->sml_bvalues[0].bv_val );
727 modtail = &mod->sml_next;
729 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
731 mod->sml_type.bv_val = NULL;
732 mod->sml_desc = slap_schema.si_ad_modifiersName;
733 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
734 ber_dupbv( &mod->sml_bvalues[0], &name );
735 mod->sml_bvalues[1].bv_val = NULL;
736 assert( mod->sml_bvalues[0].bv_val );
738 modtail = &mod->sml_next;
740 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
742 mod->sml_type.bv_val = NULL;
743 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
744 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
745 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
746 mod->sml_bvalues[1].bv_val = NULL;
747 assert( mod->sml_bvalues[0].bv_val );
749 modtail = &mod->sml_next;