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;
47 LDAPMod **modv = NULL;
56 Slapi_PBlock *pb = op->o_pb;
59 LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
61 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
65 * Parse the modify request. It looks like this:
67 * ModifyRequest := [APPLICATION 6] SEQUENCE {
68 * name DistinguishedName,
69 * mods SEQUENCE OF SEQUENCE {
70 * operation ENUMERATED {
75 * modification SEQUENCE {
77 * values SET OF AttributeValue
83 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
85 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
87 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
90 send_ldap_disconnect( conn, op,
91 LDAP_PROTOCOL_ERROR, "decoding error" );
92 return SLAPD_DISCONNECT;
96 LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
98 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
102 /* collect modifications & save for later */
104 for ( tag = ber_first_element( op->o_ber, &len, &last );
106 tag = ber_next_element( op->o_ber, &len, last ) )
109 Modifications tmp, *mod;
112 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
113 &tmp.sml_type, &tmp.sml_bvalues )
116 send_ldap_disconnect( conn, op,
117 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
118 rc = SLAPD_DISCONNECT;
122 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
124 mod->sml_type = tmp.sml_type;
125 mod->sml_bvalues = tmp.sml_bvalues;
126 mod->sml_desc = NULL;
127 mod->sml_next = NULL;
132 if ( mod->sml_bvalues == NULL ) {
134 LDAP_LOG( OPERATION, ERR,
135 "do_modify: modify/add operation (%ld) requires values\n",
138 Debug( LDAP_DEBUG_ANY,
139 "do_modify: modify/add operation (%ld) requires values\n",
143 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
144 NULL, "modify/add operation requires values",
146 rc = LDAP_PROTOCOL_ERROR;
152 case LDAP_MOD_DELETE:
153 case LDAP_MOD_REPLACE:
158 LDAP_LOG( OPERATION, ERR,
159 "do_modify: invalid modify operation (%ld)\n", (long)mop, 0, 0 );
161 Debug( LDAP_DEBUG_ANY,
162 "do_modify: invalid modify operation (%ld)\n",
166 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
167 NULL, "unrecognized modify operation", NULL, NULL );
168 rc = LDAP_PROTOCOL_ERROR;
173 modtail = &mod->sml_next;
177 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
179 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
181 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
187 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
188 if( rc != LDAP_SUCCESS ) {
190 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d invalid dn (%s)\n",
191 conn->c_connid, dn.bv_val, 0 );
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, ERR,
204 "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
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 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
215 LDAP_LOG( OPERATION, ERR,
216 "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0 );
218 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
221 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
222 NULL, "modification of subschema subentry not supported",
229 LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0 );
231 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
234 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
236 LDAP_LOG( OPERATION, DETAIL1, "\t%s: %s\n",
237 tmp->sml_op == LDAP_MOD_ADD ?
238 "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
239 "delete" : "replace"), tmp->sml_type.bv_val, 0 );
241 if ( tmp->sml_bvalues == NULL ) {
242 LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
243 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
244 LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
245 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
246 LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
248 LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
252 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
253 tmp->sml_op == LDAP_MOD_ADD
254 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
255 ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
257 if ( tmp->sml_bvalues == NULL ) {
258 Debug( LDAP_DEBUG_ARGS, "%s\n",
259 "\t\tno values", NULL, NULL );
260 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
261 Debug( LDAP_DEBUG_ARGS, "%s\n",
262 "\t\tzero values", NULL, NULL );
263 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
264 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
265 "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
267 Debug( LDAP_DEBUG_ARGS, "%s\n",
268 "\t\tmultiple values", NULL, NULL );
274 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
275 char abuf[BUFSIZ/2], *ptr = abuf;
278 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
279 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
281 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
282 if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
283 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
284 op->o_connid, op->o_opid, abuf, 0, 0 );
292 ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
293 len += tmp->sml_type.bv_len;
296 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
297 op->o_connid, op->o_opid, abuf, 0, 0 );
301 manageDSAit = get_manageDSAit( op );
304 * We could be serving multiple database backends. Select the
305 * appropriate one, or send a referral to our "referral server"
306 * if we don't hold it.
308 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
309 BerVarray ref = referral_rewrite( default_referral,
310 NULL, &pdn, LDAP_SCOPE_DEFAULT );
312 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
313 NULL, NULL, ref ? ref : default_referral, NULL );
315 ber_bvarray_free( ref );
319 /* check restrictions */
320 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
321 if( rc != LDAP_SUCCESS ) {
322 send_ldap_result( conn, op, rc,
323 NULL, text, NULL, NULL );
327 /* check for referrals */
328 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
329 if ( rc != LDAP_SUCCESS ) {
333 /* deref suffix alias if appropriate */
334 suffix_alias( be, &ndn );
336 #if defined( LDAP_SLAPI )
337 slapi_x_backend_set_pb( pb, be );
338 slapi_x_connection_set_pb( pb, conn );
339 slapi_x_operation_set_pb( pb, op );
340 slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
341 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
342 modv = slapi_x_modifications2ldapmods( &modlist );
343 slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
345 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
348 * A preoperation plugin failure will abort the
352 LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
353 "failed\n", 0, 0, 0 );
355 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
358 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0) {
361 ldap_mods_free( modv, 1 );
367 * It's possible that the preoperation plugin changed the
368 * modification array, so we need to convert it back to
369 * a Modification list.
371 * Calling slapi_x_modifications2ldapmods() destroyed modlist so
372 * we don't need to free it.
374 slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
375 modlist = slapi_x_ldapmods2modifications( modv );
376 #endif /* defined( LDAP_SLAPI ) */
379 * do the modify if 1 && (2 || 3)
380 * 1) there is a modify function implemented in this backend;
381 * 2) this backend is master for what it holds;
382 * 3) it's a replica and the dn supplied is the update_ndn.
384 if ( be->be_modify ) {
385 /* do the update here */
386 int repl_user = be_isupdate( be, &op->o_ndn );
387 #ifndef SLAPD_MULTIMASTER
388 /* Multimaster slapd does not have to check for replicator dn
389 * because it accepts each modify request
391 if ( !be->be_update_ndn.bv_len || repl_user )
394 int update = be->be_update_ndn.bv_len;
396 char textbuf[SLAP_TEXT_BUFLEN];
397 size_t textlen = sizeof textbuf;
399 rc = slap_mods_check( modlist, update, &text,
402 if( rc != LDAP_SUCCESS ) {
403 send_ldap_result( conn, op, rc,
404 NULL, text, NULL, NULL );
409 for( modtail = &modlist;
411 modtail = &(*modtail)->sml_next )
416 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
418 if( rc != LDAP_SUCCESS ) {
419 send_ldap_result( conn, op, rc,
426 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
427 #ifdef SLAPD_MULTIMASTER
431 /* but we log only the ones not from a replicator user */
432 replog( be, op, &pdn, &ndn, modlist );
435 #ifndef SLAPD_MULTIMASTER
436 /* send a referral */
438 BerVarray defref = be->be_update_refs
439 ? be->be_update_refs : default_referral;
440 BerVarray ref = referral_rewrite( defref,
441 NULL, &pdn, LDAP_SCOPE_DEFAULT );
443 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
444 ref ? ref : defref, NULL );
446 ber_bvarray_free( ref );
450 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
451 NULL, "operation not supported within namingContext",
455 #if defined( LDAP_SLAPI )
456 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) != 0 ) {
458 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
459 "failed\n", 0, 0, 0 );
461 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
462 "failed.\n", 0, 0, 0);
465 #endif /* defined( LDAP_SLAPI ) */
470 if ( modlist != NULL ) slap_mods_free( modlist );
471 #if defined( LDAP_SLAPI )
472 if ( modv != NULL ) slapi_x_free_ldapmods( modv );
478 * Do basic attribute type checking and syntax validation.
489 for( ; ml != NULL; ml = ml->sml_next ) {
490 AttributeDescription *ad = NULL;
492 /* convert to attribute description */
493 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
495 if( rc != LDAP_SUCCESS ) {
496 snprintf( textbuf, textlen, "%s: %s",
497 ml->sml_type.bv_val, *text );
504 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
505 && !slap_ad_is_binary( ad ))
507 /* attribute requires binary transfer */
508 snprintf( textbuf, textlen,
509 "%s: requires ;binary transfer",
510 ml->sml_type.bv_val );
512 return LDAP_UNDEFINED_TYPE;
515 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
516 && slap_ad_is_binary( ad ))
518 /* attribute requires binary transfer */
519 snprintf( textbuf, textlen,
520 "%s: disallows ;binary transfer",
521 ml->sml_type.bv_val );
523 return LDAP_UNDEFINED_TYPE;
526 if( slap_ad_is_tag_range( ad )) {
527 /* attribute requires binary transfer */
528 snprintf( textbuf, textlen,
529 "%s: inappropriate use of tag range option",
530 ml->sml_type.bv_val );
532 return LDAP_UNDEFINED_TYPE;
535 if (!update && is_at_no_user_mod( ad->ad_type )) {
536 /* user modification disallowed */
537 snprintf( textbuf, textlen,
538 "%s: no user modification allowed",
539 ml->sml_type.bv_val );
541 return LDAP_CONSTRAINT_VIOLATION;
544 if ( is_at_obsolete( ad->ad_type ) &&
545 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
548 * attribute is obsolete,
549 * only allow replace/delete with no values
551 snprintf( textbuf, textlen,
552 "%s: attribute is obsolete",
553 ml->sml_type.bv_val );
555 return LDAP_CONSTRAINT_VIOLATION;
561 if( ml->sml_bvalues != NULL ) {
563 slap_syntax_validate_func *validate =
564 ad->ad_type->sat_syntax->ssyn_validate;
565 slap_syntax_transform_func *pretty =
566 ad->ad_type->sat_syntax->ssyn_pretty;
568 if( !pretty && !validate ) {
569 *text = "no validator for syntax";
570 snprintf( textbuf, textlen,
571 "%s: no validator for syntax %s",
573 ad->ad_type->sat_syntax->ssyn_oid );
575 return LDAP_INVALID_SYNTAX;
579 * check that each value is valid per syntax
580 * and pretty if appropriate
582 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
585 rc = pretty( ad->ad_type->sat_syntax,
586 &ml->sml_bvalues[nvals], &pval );
588 rc = validate( ad->ad_type->sat_syntax,
589 &ml->sml_bvalues[nvals] );
593 snprintf( textbuf, textlen,
594 "%s: value #%ld invalid per syntax",
595 ml->sml_type.bv_val, (long) nvals );
597 return LDAP_INVALID_SYNTAX;
601 ber_memfree( ml->sml_bvalues[nvals].bv_val );
602 ml->sml_bvalues[nvals] = pval;
607 * a rough single value check... an additional check is needed
608 * to catch add of single value to existing single valued attribute
610 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
611 && nvals > 1 && is_at_single_value( ad->ad_type ))
613 snprintf( textbuf, textlen,
614 "%s: multiple value provided",
615 ml->sml_type.bv_val );
617 return LDAP_CONSTRAINT_VIOLATION;
625 int slap_mods_opattrs(
629 Modifications **modtail,
631 char *textbuf, size_t textlen )
633 struct berval name, timestamp, csn;
634 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
635 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
638 int mop = op->o_tag == LDAP_REQ_ADD
639 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
641 assert( modtail != NULL );
642 assert( *modtail == NULL );
644 if( SLAP_LASTMOD(be) ) {
646 time_t now = slap_get_time();
648 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
649 ltm = gmtime( &now );
650 lutil_gentime( timebuf, sizeof(timebuf), ltm );
652 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
653 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
656 timestamp.bv_val = timebuf;
657 timestamp.bv_len = strlen(timebuf);
659 if( op->o_dn.bv_len == 0 ) {
660 name.bv_val = SLAPD_ANONYMOUS;
661 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
667 if( op->o_tag == LDAP_REQ_ADD ) {
668 struct berval tmpval;
670 if( global_schemacheck ) {
671 int rc = mods_structural_class( mods, &tmpval,
672 text, textbuf, textlen );
673 if( rc != LDAP_SUCCESS ) {
677 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
679 mod->sml_type.bv_val = NULL;
680 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
681 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
682 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
683 mod->sml_bvalues[1].bv_val = NULL;
684 assert( mod->sml_bvalues[0].bv_val );
686 modtail = &mod->sml_next;
689 if( SLAP_LASTMOD(be) ) {
690 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
692 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
693 tmpval.bv_val = uuidbuf;
695 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
697 mod->sml_type.bv_val = NULL;
698 mod->sml_desc = slap_schema.si_ad_entryUUID;
699 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
700 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
701 mod->sml_bvalues[1].bv_val = NULL;
702 assert( mod->sml_bvalues[0].bv_val );
704 modtail = &mod->sml_next;
706 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
708 mod->sml_type.bv_val = NULL;
709 mod->sml_desc = slap_schema.si_ad_creatorsName;
710 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
711 ber_dupbv( &mod->sml_bvalues[0], &name );
712 mod->sml_bvalues[1].bv_val = NULL;
713 assert( mod->sml_bvalues[0].bv_val );
715 modtail = &mod->sml_next;
717 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
719 mod->sml_type.bv_val = NULL;
720 mod->sml_desc = slap_schema.si_ad_createTimestamp;
721 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
722 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
723 mod->sml_bvalues[1].bv_val = NULL;
724 assert( mod->sml_bvalues[0].bv_val );
726 modtail = &mod->sml_next;
730 if( SLAP_LASTMOD(be) ) {
731 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
733 mod->sml_type.bv_val = NULL;
734 mod->sml_desc = slap_schema.si_ad_entryCSN;
735 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
736 ber_dupbv( &mod->sml_bvalues[0], &csn );
737 mod->sml_bvalues[1].bv_val = NULL;
738 assert( mod->sml_bvalues[0].bv_val );
740 modtail = &mod->sml_next;
742 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
744 mod->sml_type.bv_val = NULL;
745 mod->sml_desc = slap_schema.si_ad_modifiersName;
746 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
747 ber_dupbv( &mod->sml_bvalues[0], &name );
748 mod->sml_bvalues[1].bv_val = NULL;
749 assert( mod->sml_bvalues[0].bv_val );
751 modtail = &mod->sml_next;
753 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
755 mod->sml_type.bv_val = NULL;
756 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
757 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
758 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
759 mod->sml_bvalues[1].bv_val = NULL;
760 assert( mod->sml_bvalues[0].bv_val );
762 modtail = &mod->sml_next;