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.
22 #include <ac/socket.h>
23 #include <ac/string.h>
39 struct berval dn = { 0, NULL };
40 struct berval pdn = { 0, NULL };
41 struct berval ndn = { 0, NULL };
45 Modifications *modlist = NULL;
46 Modifications **modtail = &modlist;
51 LDAPMod **modv = NULL;
52 Slapi_PBlock *pb = op->o_pb;
60 LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
62 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
66 * Parse the modify request. It looks like this:
68 * ModifyRequest := [APPLICATION 6] SEQUENCE {
69 * name DistinguishedName,
70 * mods SEQUENCE OF SEQUENCE {
71 * operation ENUMERATED {
76 * modification SEQUENCE {
78 * values SET OF AttributeValue
84 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
86 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
88 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" );
93 return SLAPD_DISCONNECT;
97 LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
99 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
103 /* collect modifications & save for later */
105 for ( tag = ber_first_element( op->o_ber, &len, &last );
107 tag = ber_next_element( op->o_ber, &len, last ) )
110 Modifications tmp, *mod;
113 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
114 &tmp.sml_type, &tmp.sml_bvalues )
117 send_ldap_disconnect( conn, op,
118 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
119 rc = SLAPD_DISCONNECT;
123 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
125 mod->sml_type = tmp.sml_type;
126 mod->sml_bvalues = tmp.sml_bvalues;
127 mod->sml_desc = NULL;
128 mod->sml_next = NULL;
133 if ( mod->sml_bvalues == NULL ) {
135 LDAP_LOG( OPERATION, ERR,
136 "do_modify: modify/add operation (%ld) requires values\n",
139 Debug( LDAP_DEBUG_ANY,
140 "do_modify: modify/add operation (%ld) requires values\n",
144 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
145 NULL, "modify/add operation requires values",
147 rc = LDAP_PROTOCOL_ERROR;
153 case LDAP_MOD_DELETE:
154 case LDAP_MOD_REPLACE:
159 LDAP_LOG( OPERATION, ERR,
160 "do_modify: invalid modify operation (%ld)\n", (long)mop, 0, 0 );
162 Debug( LDAP_DEBUG_ANY,
163 "do_modify: invalid modify operation (%ld)\n",
167 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
168 NULL, "unrecognized modify operation", NULL, NULL );
169 rc = LDAP_PROTOCOL_ERROR;
174 modtail = &mod->sml_next;
178 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
180 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
182 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
188 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
189 if( rc != LDAP_SUCCESS ) {
191 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d invalid dn (%s)\n",
192 conn->c_connid, dn.bv_val, 0 );
194 Debug( LDAP_DEBUG_ANY,
195 "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
197 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
198 "invalid DN", NULL, NULL );
202 if( ndn.bv_len == 0 ) {
204 LDAP_LOG( OPERATION, ERR,
205 "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
207 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
210 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
211 NULL, "modify upon the root DSE not supported", NULL, NULL );
214 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
216 LDAP_LOG( OPERATION, ERR,
217 "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0 );
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",
230 LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0 );
232 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
235 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
237 LDAP_LOG( OPERATION, DETAIL1, "\t%s: %s\n",
238 tmp->sml_op == LDAP_MOD_ADD ?
239 "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
240 "delete" : "replace"), tmp->sml_type.bv_val, 0 );
242 if ( tmp->sml_bvalues == NULL ) {
243 LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
244 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
245 LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
246 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
247 LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
249 LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
253 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
254 tmp->sml_op == LDAP_MOD_ADD
255 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
256 ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
258 if ( tmp->sml_bvalues == NULL ) {
259 Debug( LDAP_DEBUG_ARGS, "%s\n",
260 "\t\tno values", NULL, NULL );
261 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
262 Debug( LDAP_DEBUG_ARGS, "%s\n",
263 "\t\tzero values", NULL, NULL );
264 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
265 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
266 "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
268 Debug( LDAP_DEBUG_ARGS, "%s\n",
269 "\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 );
300 #endif /* LDAP_DEBUG */
302 manageDSAit = get_manageDSAit( op );
305 * We could be serving multiple database backends. Select the
306 * appropriate one, or send a referral to our "referral server"
307 * if we don't hold it.
309 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
310 BerVarray ref = referral_rewrite( default_referral,
311 NULL, &pdn, LDAP_SCOPE_DEFAULT );
312 if ( ref == NULL ) ref = default_referral;
314 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
315 NULL, NULL, ref, NULL );
317 if ( ref != default_referral ) ber_bvarray_free( ref );
319 send_ldap_result( conn, op,
320 rc = LDAP_UNWILLING_TO_PERFORM,
321 NULL, "referral missing", NULL, NULL );
326 /* check restrictions */
327 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
328 if( rc != LDAP_SUCCESS ) {
329 send_ldap_result( conn, op, rc,
330 NULL, text, NULL, NULL );
334 /* check for referrals */
335 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
336 if ( rc != LDAP_SUCCESS ) {
340 #if defined( LDAP_SLAPI )
341 slapi_x_backend_set_pb( pb, be );
342 slapi_x_connection_set_pb( pb, conn );
343 slapi_x_operation_set_pb( pb, op );
344 slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
345 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
346 modv = slapi_x_modifications2ldapmods( &modlist );
347 slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
349 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
352 * A preoperation plugin failure will abort the
356 LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
357 "failed\n", 0, 0, 0 );
359 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
362 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0) {
365 ldap_mods_free( modv, 1 );
371 * It's possible that the preoperation plugin changed the
372 * modification array, so we need to convert it back to
373 * a Modification list.
375 * Calling slapi_x_modifications2ldapmods() destroyed modlist so
376 * we don't need to free it.
378 slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
379 modlist = slapi_x_ldapmods2modifications( modv );
380 #endif /* defined( LDAP_SLAPI ) */
383 * do the modify if 1 && (2 || 3)
384 * 1) there is a modify function implemented in this backend;
385 * 2) this backend is master for what it holds;
386 * 3) it's a replica and the dn supplied is the update_ndn.
388 if ( be->be_modify ) {
389 /* do the update here */
390 int repl_user = be_isupdate( be, &op->o_ndn );
391 #ifndef SLAPD_MULTIMASTER
392 /* Multimaster slapd does not have to check for replicator dn
393 * because it accepts each modify request
395 if ( !be->be_update_ndn.bv_len || repl_user )
398 int update = be->be_update_ndn.bv_len;
400 char textbuf[SLAP_TEXT_BUFLEN];
401 size_t textlen = sizeof textbuf;
403 slap_callback cb = {0};
405 rc = slap_mods_check( modlist, update, &text,
408 if( rc != LDAP_SUCCESS ) {
409 send_ldap_result( conn, op, rc,
410 NULL, text, NULL, NULL );
415 for( modtail = &modlist;
417 modtail = &(*modtail)->sml_next )
422 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
424 if( rc != LDAP_SUCCESS ) {
425 send_ldap_result( conn, op, rc,
432 #ifdef SLAPD_MULTIMASTER
436 ctx.prev = op->o_callback;
440 ctx.change = modlist;
441 cb.sc_private = &ctx;
442 cb.sc_response = slap_replog_cb;
443 op->o_callback = &cb;
445 rc = (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist );
447 #ifndef SLAPD_MULTIMASTER
448 /* send a referral */
450 BerVarray defref = be->be_update_refs
451 ? be->be_update_refs : default_referral;
452 if ( defref != NULL ) {
453 BerVarray ref = referral_rewrite( defref,
454 NULL, &pdn, LDAP_SCOPE_DEFAULT );
456 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
458 ref ? ref : defref, NULL );
460 ber_bvarray_free( ref );
462 send_ldap_result( conn, op,
463 rc = LDAP_UNWILLING_TO_PERFORM,
464 NULL, "referral missing",
470 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
471 NULL, "operation not supported within namingContext",
475 #if defined( LDAP_SLAPI )
476 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) != 0 ) {
478 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
479 "failed\n", 0, 0, 0 );
481 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
482 "failed.\n", 0, 0, 0);
485 #endif /* defined( LDAP_SLAPI ) */
490 if ( modlist != NULL ) slap_mods_free( modlist );
491 #if defined( LDAP_SLAPI )
492 if ( modv != NULL ) slapi_x_free_ldapmods( modv );
498 * Do basic attribute type checking and syntax validation.
509 for( ; ml != NULL; ml = ml->sml_next ) {
510 AttributeDescription *ad = NULL;
512 /* convert to attribute description */
513 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
515 if( rc != LDAP_SUCCESS ) {
516 snprintf( textbuf, textlen, "%s: %s",
517 ml->sml_type.bv_val, *text );
524 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
525 && !slap_ad_is_binary( ad ))
527 /* attribute requires binary transfer */
528 snprintf( textbuf, textlen,
529 "%s: requires ;binary transfer",
530 ml->sml_type.bv_val );
532 return LDAP_UNDEFINED_TYPE;
535 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
536 && slap_ad_is_binary( ad ))
538 /* attribute requires binary transfer */
539 snprintf( textbuf, textlen,
540 "%s: disallows ;binary transfer",
541 ml->sml_type.bv_val );
543 return LDAP_UNDEFINED_TYPE;
546 if( slap_ad_is_tag_range( ad )) {
547 /* attribute requires binary transfer */
548 snprintf( textbuf, textlen,
549 "%s: inappropriate use of tag range option",
550 ml->sml_type.bv_val );
552 return LDAP_UNDEFINED_TYPE;
555 if (!update && is_at_no_user_mod( ad->ad_type )) {
556 /* user modification disallowed */
557 snprintf( textbuf, textlen,
558 "%s: no user modification allowed",
559 ml->sml_type.bv_val );
561 return LDAP_CONSTRAINT_VIOLATION;
564 if ( is_at_obsolete( ad->ad_type ) &&
565 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
568 * attribute is obsolete,
569 * only allow replace/delete with no values
571 snprintf( textbuf, textlen,
572 "%s: attribute is obsolete",
573 ml->sml_type.bv_val );
575 return LDAP_CONSTRAINT_VIOLATION;
581 if( ml->sml_bvalues != NULL ) {
583 slap_syntax_validate_func *validate =
584 ad->ad_type->sat_syntax->ssyn_validate;
585 slap_syntax_transform_func *pretty =
586 ad->ad_type->sat_syntax->ssyn_pretty;
588 if( !pretty && !validate ) {
589 *text = "no validator for syntax";
590 snprintf( textbuf, textlen,
591 "%s: no validator for syntax %s",
593 ad->ad_type->sat_syntax->ssyn_oid );
595 return LDAP_INVALID_SYNTAX;
599 * check that each value is valid per syntax
600 * and pretty if appropriate
602 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
605 rc = pretty( ad->ad_type->sat_syntax,
606 &ml->sml_bvalues[nvals], &pval );
608 rc = validate( ad->ad_type->sat_syntax,
609 &ml->sml_bvalues[nvals] );
613 snprintf( textbuf, textlen,
614 "%s: value #%ld invalid per syntax",
615 ml->sml_type.bv_val, (long) nvals );
617 return LDAP_INVALID_SYNTAX;
621 ber_memfree( ml->sml_bvalues[nvals].bv_val );
622 ml->sml_bvalues[nvals] = pval;
627 * a rough single value check... an additional check is needed
628 * to catch add of single value to existing single valued attribute
630 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
631 && nvals > 1 && is_at_single_value( ad->ad_type ))
633 snprintf( textbuf, textlen,
634 "%s: multiple value provided",
635 ml->sml_type.bv_val );
637 return LDAP_CONSTRAINT_VIOLATION;
645 int slap_mods_opattrs(
649 Modifications **modtail,
651 char *textbuf, size_t textlen )
653 struct berval name, timestamp, csn;
654 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
655 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
658 int mop = op->o_tag == LDAP_REQ_ADD
659 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
661 assert( modtail != NULL );
662 assert( *modtail == NULL );
664 if( SLAP_LASTMOD(be) ) {
666 time_t now = slap_get_time();
668 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
669 ltm = gmtime( &now );
670 lutil_gentime( timebuf, sizeof(timebuf), ltm );
672 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
673 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
676 timestamp.bv_val = timebuf;
677 timestamp.bv_len = strlen(timebuf);
679 if( op->o_dn.bv_len == 0 ) {
680 name.bv_val = SLAPD_ANONYMOUS;
681 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
687 if( op->o_tag == LDAP_REQ_ADD ) {
688 struct berval tmpval;
690 if( global_schemacheck ) {
691 int rc = mods_structural_class( mods, &tmpval,
692 text, textbuf, textlen );
693 if( rc != LDAP_SUCCESS ) {
697 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
699 mod->sml_type.bv_val = NULL;
700 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
701 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
702 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
703 mod->sml_bvalues[1].bv_val = NULL;
704 assert( mod->sml_bvalues[0].bv_val );
706 modtail = &mod->sml_next;
709 if( SLAP_LASTMOD(be) ) {
710 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
712 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
713 tmpval.bv_val = uuidbuf;
715 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
717 mod->sml_type.bv_val = NULL;
718 mod->sml_desc = slap_schema.si_ad_entryUUID;
719 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
720 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
721 mod->sml_bvalues[1].bv_val = NULL;
722 assert( mod->sml_bvalues[0].bv_val );
724 modtail = &mod->sml_next;
726 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
728 mod->sml_type.bv_val = NULL;
729 mod->sml_desc = slap_schema.si_ad_creatorsName;
730 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
731 ber_dupbv( &mod->sml_bvalues[0], &name );
732 mod->sml_bvalues[1].bv_val = NULL;
733 assert( mod->sml_bvalues[0].bv_val );
735 modtail = &mod->sml_next;
737 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
739 mod->sml_type.bv_val = NULL;
740 mod->sml_desc = slap_schema.si_ad_createTimestamp;
741 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
742 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
743 mod->sml_bvalues[1].bv_val = NULL;
744 assert( mod->sml_bvalues[0].bv_val );
746 modtail = &mod->sml_next;
750 if( SLAP_LASTMOD(be) ) {
751 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
753 mod->sml_type.bv_val = NULL;
754 mod->sml_desc = slap_schema.si_ad_entryCSN;
755 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
756 ber_dupbv( &mod->sml_bvalues[0], &csn );
757 mod->sml_bvalues[1].bv_val = NULL;
758 assert( mod->sml_bvalues[0].bv_val );
760 modtail = &mod->sml_next;
762 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
764 mod->sml_type.bv_val = NULL;
765 mod->sml_desc = slap_schema.si_ad_modifiersName;
766 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
767 ber_dupbv( &mod->sml_bvalues[0], &name );
768 mod->sml_bvalues[1].bv_val = NULL;
769 assert( mod->sml_bvalues[0].bv_val );
771 modtail = &mod->sml_next;
773 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
775 mod->sml_type.bv_val = NULL;
776 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
777 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
778 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
779 mod->sml_bvalues[1].bv_val = NULL;
780 assert( mod->sml_bvalues[0].bv_val );
782 modtail = &mod->sml_next;