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>
37 struct berval dn = { 0, NULL };
38 struct berval pdn = { 0, NULL };
39 struct berval ndn = { 0, NULL };
43 Modifications *modlist = NULL;
44 Modifications **modtail = &modlist;
49 LDAPMod **modv = NULL;
50 Slapi_PBlock *pb = op->o_pb;
58 LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
60 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
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, "{m" /*}*/, &dn ) == LBER_ERROR ) {
84 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
86 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
89 send_ldap_disconnect( conn, op,
90 LDAP_PROTOCOL_ERROR, "decoding error" );
91 return SLAPD_DISCONNECT;
95 LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
97 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
101 /* collect modifications & save for later */
103 for ( tag = ber_first_element( op->o_ber, &len, &last );
105 tag = ber_next_element( op->o_ber, &len, last ) )
108 Modifications tmp, *mod;
111 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
112 &tmp.sml_type, &tmp.sml_bvalues )
115 send_ldap_disconnect( conn, op,
116 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
117 rc = SLAPD_DISCONNECT;
121 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
123 mod->sml_type = tmp.sml_type;
124 mod->sml_bvalues = tmp.sml_bvalues;
125 mod->sml_desc = NULL;
126 mod->sml_next = NULL;
131 if ( mod->sml_bvalues == NULL ) {
133 LDAP_LOG( OPERATION, ERR,
134 "do_modify: modify/add operation (%ld) requires values\n",
137 Debug( LDAP_DEBUG_ANY,
138 "do_modify: modify/add operation (%ld) requires values\n",
142 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
143 NULL, "modify/add operation requires values",
145 rc = LDAP_PROTOCOL_ERROR;
151 case LDAP_MOD_DELETE:
152 case LDAP_MOD_REPLACE:
157 LDAP_LOG( OPERATION, ERR,
158 "do_modify: invalid modify operation (%ld)\n", (long)mop, 0, 0 );
160 Debug( LDAP_DEBUG_ANY,
161 "do_modify: invalid modify operation (%ld)\n",
165 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
166 NULL, "unrecognized modify operation", NULL, NULL );
167 rc = LDAP_PROTOCOL_ERROR;
172 modtail = &mod->sml_next;
176 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
178 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
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, INFO, "do_modify: conn %d invalid dn (%s)\n",
190 conn->c_connid, dn.bv_val, 0 );
192 Debug( LDAP_DEBUG_ANY,
193 "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
195 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
196 "invalid DN", NULL, NULL );
200 if( ndn.bv_len == 0 ) {
202 LDAP_LOG( OPERATION, ERR,
203 "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
205 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
208 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
209 NULL, "modify upon the root DSE not supported", NULL, NULL );
212 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
214 LDAP_LOG( OPERATION, ERR,
215 "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0 );
217 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
220 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
221 NULL, "modification of subschema subentry not supported",
228 LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0 );
230 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
233 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
235 LDAP_LOG( OPERATION, DETAIL1, "\t%s: %s\n",
236 tmp->sml_op == LDAP_MOD_ADD ?
237 "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
238 "delete" : "replace"), tmp->sml_type.bv_val, 0 );
240 if ( tmp->sml_bvalues == NULL ) {
241 LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
242 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
243 LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
244 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
245 LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
247 LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
251 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
252 tmp->sml_op == LDAP_MOD_ADD
253 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
254 ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
256 if ( tmp->sml_bvalues == NULL ) {
257 Debug( LDAP_DEBUG_ARGS, "%s\n",
258 "\t\tno values", NULL, NULL );
259 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
260 Debug( LDAP_DEBUG_ARGS, "%s\n",
261 "\t\tzero values", NULL, NULL );
262 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
263 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
264 "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
266 Debug( LDAP_DEBUG_ARGS, "%s\n",
267 "\t\tmultiple values", NULL, NULL );
273 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
274 char abuf[BUFSIZ/2], *ptr = abuf;
277 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
278 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
280 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
281 if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
282 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
283 op->o_connid, op->o_opid, abuf, 0, 0 );
291 ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
292 len += tmp->sml_type.bv_len;
295 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
296 op->o_connid, op->o_opid, abuf, 0, 0 );
300 manageDSAit = get_manageDSAit( op );
303 * We could be serving multiple database backends. Select the
304 * appropriate one, or send a referral to our "referral server"
305 * if we don't hold it.
307 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
308 BerVarray ref = referral_rewrite( default_referral,
309 NULL, &pdn, LDAP_SCOPE_DEFAULT );
311 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
312 NULL, NULL, ref ? ref : default_referral, NULL );
314 ber_bvarray_free( ref );
318 /* check restrictions */
319 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
320 if( rc != LDAP_SUCCESS ) {
321 send_ldap_result( conn, op, rc,
322 NULL, text, NULL, NULL );
326 /* check for referrals */
327 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
328 if ( rc != LDAP_SUCCESS ) {
332 #if defined( LDAP_SLAPI )
333 slapi_x_backend_set_pb( pb, be );
334 slapi_x_connection_set_pb( pb, conn );
335 slapi_x_operation_set_pb( pb, op );
336 slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
337 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
338 modv = slapi_x_modifications2ldapmods( &modlist );
339 slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
341 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
344 * A preoperation plugin failure will abort the
348 LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
349 "failed\n", 0, 0, 0 );
351 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
354 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0) {
357 ldap_mods_free( modv, 1 );
363 * It's possible that the preoperation plugin changed the
364 * modification array, so we need to convert it back to
365 * a Modification list.
367 * Calling slapi_x_modifications2ldapmods() destroyed modlist so
368 * we don't need to free it.
370 slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
371 modlist = slapi_x_ldapmods2modifications( modv );
372 #endif /* defined( LDAP_SLAPI ) */
375 * do the modify if 1 && (2 || 3)
376 * 1) there is a modify function implemented in this backend;
377 * 2) this backend is master for what it holds;
378 * 3) it's a replica and the dn supplied is the update_ndn.
380 if ( be->be_modify ) {
381 /* do the update here */
382 int repl_user = be_isupdate( be, &op->o_ndn );
383 #ifndef SLAPD_MULTIMASTER
384 /* Multimaster slapd does not have to check for replicator dn
385 * because it accepts each modify request
387 if ( !be->be_update_ndn.bv_len || repl_user )
390 int update = be->be_update_ndn.bv_len;
392 char textbuf[SLAP_TEXT_BUFLEN];
393 size_t textlen = sizeof textbuf;
395 rc = slap_mods_check( modlist, update, &text,
398 if( rc != LDAP_SUCCESS ) {
399 send_ldap_result( conn, op, rc,
400 NULL, text, NULL, NULL );
405 for( modtail = &modlist;
407 modtail = &(*modtail)->sml_next )
412 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
414 if( rc != LDAP_SUCCESS ) {
415 send_ldap_result( conn, op, rc,
422 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
423 #ifdef SLAPD_MULTIMASTER
427 /* but we log only the ones not from a replicator user */
428 replog( be, op, &pdn, &ndn, modlist );
431 #ifndef SLAPD_MULTIMASTER
432 /* send a referral */
434 BerVarray defref = be->be_update_refs
435 ? be->be_update_refs : default_referral;
436 BerVarray ref = referral_rewrite( defref,
437 NULL, &pdn, LDAP_SCOPE_DEFAULT );
439 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
440 ref ? ref : defref, NULL );
442 ber_bvarray_free( ref );
446 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
447 NULL, "operation not supported within namingContext",
451 #if defined( LDAP_SLAPI )
452 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) != 0 ) {
454 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
455 "failed\n", 0, 0, 0 );
457 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
458 "failed.\n", 0, 0, 0);
461 #endif /* defined( LDAP_SLAPI ) */
466 if ( modlist != NULL ) slap_mods_free( modlist );
467 #if defined( LDAP_SLAPI )
468 if ( modv != NULL ) slapi_x_free_ldapmods( modv );
474 * Do basic attribute type checking and syntax validation.
485 for( ; ml != NULL; ml = ml->sml_next ) {
486 AttributeDescription *ad = NULL;
488 /* convert to attribute description */
489 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
491 if( rc != LDAP_SUCCESS ) {
492 snprintf( textbuf, textlen, "%s: %s",
493 ml->sml_type.bv_val, *text );
500 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
501 && !slap_ad_is_binary( ad ))
503 /* attribute requires binary transfer */
504 snprintf( textbuf, textlen,
505 "%s: requires ;binary transfer",
506 ml->sml_type.bv_val );
508 return LDAP_UNDEFINED_TYPE;
511 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
512 && slap_ad_is_binary( ad ))
514 /* attribute requires binary transfer */
515 snprintf( textbuf, textlen,
516 "%s: disallows ;binary transfer",
517 ml->sml_type.bv_val );
519 return LDAP_UNDEFINED_TYPE;
522 if( slap_ad_is_tag_range( ad )) {
523 /* attribute requires binary transfer */
524 snprintf( textbuf, textlen,
525 "%s: inappropriate use of tag range option",
526 ml->sml_type.bv_val );
528 return LDAP_UNDEFINED_TYPE;
531 if (!update && is_at_no_user_mod( ad->ad_type )) {
532 /* user modification disallowed */
533 snprintf( textbuf, textlen,
534 "%s: no user modification allowed",
535 ml->sml_type.bv_val );
537 return LDAP_CONSTRAINT_VIOLATION;
540 if ( is_at_obsolete( ad->ad_type ) &&
541 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
544 * attribute is obsolete,
545 * only allow replace/delete with no values
547 snprintf( textbuf, textlen,
548 "%s: attribute is obsolete",
549 ml->sml_type.bv_val );
551 return LDAP_CONSTRAINT_VIOLATION;
557 if( ml->sml_bvalues != NULL ) {
559 slap_syntax_validate_func *validate =
560 ad->ad_type->sat_syntax->ssyn_validate;
561 slap_syntax_transform_func *pretty =
562 ad->ad_type->sat_syntax->ssyn_pretty;
564 if( !pretty && !validate ) {
565 *text = "no validator for syntax";
566 snprintf( textbuf, textlen,
567 "%s: no validator for syntax %s",
569 ad->ad_type->sat_syntax->ssyn_oid );
571 return LDAP_INVALID_SYNTAX;
575 * check that each value is valid per syntax
576 * and pretty if appropriate
578 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
581 rc = pretty( ad->ad_type->sat_syntax,
582 &ml->sml_bvalues[nvals], &pval );
584 rc = validate( ad->ad_type->sat_syntax,
585 &ml->sml_bvalues[nvals] );
589 snprintf( textbuf, textlen,
590 "%s: value #%ld invalid per syntax",
591 ml->sml_type.bv_val, (long) nvals );
593 return LDAP_INVALID_SYNTAX;
597 ber_memfree( ml->sml_bvalues[nvals].bv_val );
598 ml->sml_bvalues[nvals] = pval;
603 * a rough single value check... an additional check is needed
604 * to catch add of single value to existing single valued attribute
606 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
607 && nvals > 1 && is_at_single_value( ad->ad_type ))
609 snprintf( textbuf, textlen,
610 "%s: multiple value provided",
611 ml->sml_type.bv_val );
613 return LDAP_CONSTRAINT_VIOLATION;
621 int slap_mods_opattrs(
625 Modifications **modtail,
627 char *textbuf, size_t textlen )
629 struct berval name, timestamp, csn;
630 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
631 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
634 int mop = op->o_tag == LDAP_REQ_ADD
635 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
637 assert( modtail != NULL );
638 assert( *modtail == NULL );
640 if( SLAP_LASTMOD(be) ) {
642 time_t now = slap_get_time();
644 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
645 ltm = gmtime( &now );
646 lutil_gentime( timebuf, sizeof(timebuf), ltm );
648 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
649 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
652 timestamp.bv_val = timebuf;
653 timestamp.bv_len = strlen(timebuf);
655 if( op->o_dn.bv_len == 0 ) {
656 name.bv_val = SLAPD_ANONYMOUS;
657 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
663 if( op->o_tag == LDAP_REQ_ADD ) {
664 struct berval tmpval;
666 if( global_schemacheck ) {
667 int rc = mods_structural_class( mods, &tmpval,
668 text, textbuf, textlen );
669 if( rc != LDAP_SUCCESS ) {
673 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
675 mod->sml_type.bv_val = NULL;
676 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
677 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
678 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
679 mod->sml_bvalues[1].bv_val = NULL;
680 assert( mod->sml_bvalues[0].bv_val );
682 modtail = &mod->sml_next;
685 if( SLAP_LASTMOD(be) ) {
686 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
688 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
689 tmpval.bv_val = uuidbuf;
691 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
693 mod->sml_type.bv_val = NULL;
694 mod->sml_desc = slap_schema.si_ad_entryUUID;
695 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
696 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
697 mod->sml_bvalues[1].bv_val = NULL;
698 assert( mod->sml_bvalues[0].bv_val );
700 modtail = &mod->sml_next;
702 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
704 mod->sml_type.bv_val = NULL;
705 mod->sml_desc = slap_schema.si_ad_creatorsName;
706 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
707 ber_dupbv( &mod->sml_bvalues[0], &name );
708 mod->sml_bvalues[1].bv_val = NULL;
709 assert( mod->sml_bvalues[0].bv_val );
711 modtail = &mod->sml_next;
713 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
715 mod->sml_type.bv_val = NULL;
716 mod->sml_desc = slap_schema.si_ad_createTimestamp;
717 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
718 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
719 mod->sml_bvalues[1].bv_val = NULL;
720 assert( mod->sml_bvalues[0].bv_val );
722 modtail = &mod->sml_next;
726 if( SLAP_LASTMOD(be) ) {
727 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
729 mod->sml_type.bv_val = NULL;
730 mod->sml_desc = slap_schema.si_ad_entryCSN;
731 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
732 ber_dupbv( &mod->sml_bvalues[0], &csn );
733 mod->sml_bvalues[1].bv_val = NULL;
734 assert( mod->sml_bvalues[0].bv_val );
736 modtail = &mod->sml_next;
738 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
740 mod->sml_type.bv_val = NULL;
741 mod->sml_desc = slap_schema.si_ad_modifiersName;
742 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
743 ber_dupbv( &mod->sml_bvalues[0], &name );
744 mod->sml_bvalues[1].bv_val = NULL;
745 assert( mod->sml_bvalues[0].bv_val );
747 modtail = &mod->sml_next;
749 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
751 mod->sml_type.bv_val = NULL;
752 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
753 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
754 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
755 mod->sml_bvalues[1].bv_val = NULL;
756 assert( mod->sml_bvalues[0].bv_val );
758 modtail = &mod->sml_next;