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 );
275 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
276 char abuf[BUFSIZ/2], *ptr = abuf;
279 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
280 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
282 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
283 if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
284 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
285 op->o_connid, op->o_opid, abuf, 0, 0 );
293 ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
294 len += tmp->sml_type.bv_len;
297 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
298 op->o_connid, op->o_opid, abuf, 0, 0 );
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 );
313 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
314 NULL, NULL, ref ? ref : default_referral, NULL );
316 ber_bvarray_free( ref );
320 /* check restrictions */
321 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
322 if( rc != LDAP_SUCCESS ) {
323 send_ldap_result( conn, op, rc,
324 NULL, text, NULL, NULL );
328 /* check for referrals */
329 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
330 if ( rc != LDAP_SUCCESS ) {
334 #if defined( LDAP_SLAPI )
335 slapi_x_backend_set_pb( pb, be );
336 slapi_x_connection_set_pb( pb, conn );
337 slapi_x_operation_set_pb( pb, op );
338 slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
339 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
340 modv = slapi_x_modifications2ldapmods( &modlist );
341 slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
343 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
346 * A preoperation plugin failure will abort the
350 LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
351 "failed\n", 0, 0, 0 );
353 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
356 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0) {
359 ldap_mods_free( modv, 1 );
365 * It's possible that the preoperation plugin changed the
366 * modification array, so we need to convert it back to
367 * a Modification list.
369 * Calling slapi_x_modifications2ldapmods() destroyed modlist so
370 * we don't need to free it.
372 slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
373 modlist = slapi_x_ldapmods2modifications( modv );
374 #endif /* defined( LDAP_SLAPI ) */
377 * do the modify if 1 && (2 || 3)
378 * 1) there is a modify function implemented in this backend;
379 * 2) this backend is master for what it holds;
380 * 3) it's a replica and the dn supplied is the update_ndn.
382 if ( be->be_modify ) {
383 /* do the update here */
384 int repl_user = be_isupdate( be, &op->o_ndn );
385 #ifndef SLAPD_MULTIMASTER
386 /* Multimaster slapd does not have to check for replicator dn
387 * because it accepts each modify request
389 if ( !be->be_update_ndn.bv_len || repl_user )
392 int update = be->be_update_ndn.bv_len;
394 char textbuf[SLAP_TEXT_BUFLEN];
395 size_t textlen = sizeof textbuf;
397 rc = slap_mods_check( modlist, update, &text,
400 if( rc != LDAP_SUCCESS ) {
401 send_ldap_result( conn, op, rc,
402 NULL, text, NULL, NULL );
407 for( modtail = &modlist;
409 modtail = &(*modtail)->sml_next )
414 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
416 if( rc != LDAP_SUCCESS ) {
417 send_ldap_result( conn, op, rc,
424 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
425 #ifdef SLAPD_MULTIMASTER
429 /* but we log only the ones not from a replicator user */
430 replog( be, op, &pdn, &ndn, modlist );
433 #ifndef SLAPD_MULTIMASTER
434 /* send a referral */
436 BerVarray defref = be->be_update_refs
437 ? be->be_update_refs : default_referral;
438 BerVarray ref = referral_rewrite( defref,
439 NULL, &pdn, LDAP_SCOPE_DEFAULT );
441 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
442 ref ? ref : defref, NULL );
444 ber_bvarray_free( ref );
448 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
449 NULL, "operation not supported within namingContext",
453 #if defined( LDAP_SLAPI )
454 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) != 0 ) {
456 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
457 "failed\n", 0, 0, 0 );
459 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
460 "failed.\n", 0, 0, 0);
463 #endif /* defined( LDAP_SLAPI ) */
468 if ( modlist != NULL ) slap_mods_free( modlist );
469 #if defined( LDAP_SLAPI )
470 if ( modv != NULL ) slapi_x_free_ldapmods( modv );
476 * Do basic attribute type checking and syntax validation.
487 for( ; ml != NULL; ml = ml->sml_next ) {
488 AttributeDescription *ad = NULL;
490 /* convert to attribute description */
491 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
493 if( rc != LDAP_SUCCESS ) {
494 snprintf( textbuf, textlen, "%s: %s",
495 ml->sml_type.bv_val, *text );
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: requires ;binary transfer",
508 ml->sml_type.bv_val );
510 return LDAP_UNDEFINED_TYPE;
513 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
514 && slap_ad_is_binary( ad ))
516 /* attribute requires binary transfer */
517 snprintf( textbuf, textlen,
518 "%s: disallows ;binary transfer",
519 ml->sml_type.bv_val );
521 return LDAP_UNDEFINED_TYPE;
524 if( slap_ad_is_tag_range( ad )) {
525 /* attribute requires binary transfer */
526 snprintf( textbuf, textlen,
527 "%s: inappropriate use of tag range option",
528 ml->sml_type.bv_val );
530 return LDAP_UNDEFINED_TYPE;
533 if (!update && is_at_no_user_mod( ad->ad_type )) {
534 /* user modification disallowed */
535 snprintf( textbuf, textlen,
536 "%s: no user modification allowed",
537 ml->sml_type.bv_val );
539 return LDAP_CONSTRAINT_VIOLATION;
542 if ( is_at_obsolete( ad->ad_type ) &&
543 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
546 * attribute is obsolete,
547 * only allow replace/delete with no values
549 snprintf( textbuf, textlen,
550 "%s: attribute is obsolete",
551 ml->sml_type.bv_val );
553 return LDAP_CONSTRAINT_VIOLATION;
559 if( ml->sml_bvalues != NULL ) {
561 slap_syntax_validate_func *validate =
562 ad->ad_type->sat_syntax->ssyn_validate;
563 slap_syntax_transform_func *pretty =
564 ad->ad_type->sat_syntax->ssyn_pretty;
566 if( !pretty && !validate ) {
567 *text = "no validator for syntax";
568 snprintf( textbuf, textlen,
569 "%s: no validator for syntax %s",
571 ad->ad_type->sat_syntax->ssyn_oid );
573 return LDAP_INVALID_SYNTAX;
577 * check that each value is valid per syntax
578 * and pretty if appropriate
580 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
583 rc = pretty( ad->ad_type->sat_syntax,
584 &ml->sml_bvalues[nvals], &pval );
586 rc = validate( ad->ad_type->sat_syntax,
587 &ml->sml_bvalues[nvals] );
591 snprintf( textbuf, textlen,
592 "%s: value #%ld invalid per syntax",
593 ml->sml_type.bv_val, (long) nvals );
595 return LDAP_INVALID_SYNTAX;
599 ber_memfree( ml->sml_bvalues[nvals].bv_val );
600 ml->sml_bvalues[nvals] = pval;
605 * a rough single value check... an additional check is needed
606 * to catch add of single value to existing single valued attribute
608 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
609 && nvals > 1 && is_at_single_value( ad->ad_type ))
611 snprintf( textbuf, textlen,
612 "%s: multiple value provided",
613 ml->sml_type.bv_val );
615 return LDAP_CONSTRAINT_VIOLATION;
623 int slap_mods_opattrs(
627 Modifications **modtail,
629 char *textbuf, size_t textlen )
631 struct berval name, timestamp, csn;
632 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
633 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
636 int mop = op->o_tag == LDAP_REQ_ADD
637 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
639 assert( modtail != NULL );
640 assert( *modtail == NULL );
642 if( SLAP_LASTMOD(be) ) {
644 time_t now = slap_get_time();
646 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
647 ltm = gmtime( &now );
648 lutil_gentime( timebuf, sizeof(timebuf), ltm );
650 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
651 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
654 timestamp.bv_val = timebuf;
655 timestamp.bv_len = strlen(timebuf);
657 if( op->o_dn.bv_len == 0 ) {
658 name.bv_val = SLAPD_ANONYMOUS;
659 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
665 if( op->o_tag == LDAP_REQ_ADD ) {
666 struct berval tmpval;
668 if( global_schemacheck ) {
669 int rc = mods_structural_class( mods, &tmpval,
670 text, textbuf, textlen );
671 if( rc != LDAP_SUCCESS ) {
675 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
677 mod->sml_type.bv_val = NULL;
678 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
679 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
680 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
681 mod->sml_bvalues[1].bv_val = NULL;
682 assert( mod->sml_bvalues[0].bv_val );
684 modtail = &mod->sml_next;
687 if( SLAP_LASTMOD(be) ) {
688 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
690 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
691 tmpval.bv_val = uuidbuf;
693 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
695 mod->sml_type.bv_val = NULL;
696 mod->sml_desc = slap_schema.si_ad_entryUUID;
697 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
698 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
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_creatorsName;
708 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
709 ber_dupbv( &mod->sml_bvalues[0], &name );
710 mod->sml_bvalues[1].bv_val = NULL;
711 assert( mod->sml_bvalues[0].bv_val );
713 modtail = &mod->sml_next;
715 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
717 mod->sml_type.bv_val = NULL;
718 mod->sml_desc = slap_schema.si_ad_createTimestamp;
719 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
720 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
721 mod->sml_bvalues[1].bv_val = NULL;
722 assert( mod->sml_bvalues[0].bv_val );
724 modtail = &mod->sml_next;
728 if( SLAP_LASTMOD(be) ) {
729 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
731 mod->sml_type.bv_val = NULL;
732 mod->sml_desc = slap_schema.si_ad_entryCSN;
733 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
734 ber_dupbv( &mod->sml_bvalues[0], &csn );
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_modifiersName;
744 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
745 ber_dupbv( &mod->sml_bvalues[0], &name );
746 mod->sml_bvalues[1].bv_val = NULL;
747 assert( mod->sml_bvalues[0].bv_val );
749 modtail = &mod->sml_next;
751 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
753 mod->sml_type.bv_val = NULL;
754 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
755 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
756 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
757 mod->sml_bvalues[1].bv_val = NULL;
758 assert( mod->sml_bvalues[0].bv_val );
760 modtail = &mod->sml_next;