3 * Copyright 1998-2002 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>
36 struct berval dn = { 0, NULL };
37 struct berval pdn = { 0, NULL };
38 struct berval ndn = { 0, NULL };
42 Modifications *modlist = NULL;
43 Modifications **modtail = &modlist;
53 LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
55 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
59 * Parse the modify request. It looks like this:
61 * ModifyRequest := [APPLICATION 6] SEQUENCE {
62 * name DistinguishedName,
63 * mods SEQUENCE OF SEQUENCE {
64 * operation ENUMERATED {
69 * modification SEQUENCE {
71 * values SET OF AttributeValue
77 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
79 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
81 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
84 send_ldap_disconnect( conn, op,
85 LDAP_PROTOCOL_ERROR, "decoding error" );
86 return SLAPD_DISCONNECT;
90 LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
92 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
96 /* collect modifications & save for later */
98 for ( tag = ber_first_element( op->o_ber, &len, &last );
100 tag = ber_next_element( op->o_ber, &len, last ) )
103 Modifications tmp, *mod;
106 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
107 &tmp.sml_type, &tmp.sml_bvalues )
110 send_ldap_disconnect( conn, op,
111 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
112 rc = SLAPD_DISCONNECT;
116 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
118 mod->sml_type = tmp.sml_type;
119 mod->sml_bvalues = tmp.sml_bvalues;
120 mod->sml_desc = NULL;
126 if ( mod->sml_bvalues == NULL ) {
128 LDAP_LOG( OPERATION, ERR,
129 "do_modify: modify/add operation (%ld) requires values\n",
132 Debug( LDAP_DEBUG_ANY,
133 "do_modify: modify/add operation (%ld) requires values\n",
137 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
138 NULL, "modify/add operation requires values",
140 rc = LDAP_PROTOCOL_ERROR;
146 case LDAP_MOD_DELETE:
147 case LDAP_MOD_REPLACE:
152 LDAP_LOG( OPERATION, ERR,
153 "do_modify: invalid modify operation (%ld)\n", (long)mop, 0, 0 );
155 Debug( LDAP_DEBUG_ANY,
156 "do_modify: invalid modify operation (%ld)\n",
160 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
161 NULL, "unrecognized modify operation", NULL, NULL );
162 rc = LDAP_PROTOCOL_ERROR;
167 modtail = &mod->sml_next;
171 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
173 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
175 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
181 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
182 if( rc != LDAP_SUCCESS ) {
184 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d invalid dn (%s)\n",
185 conn->c_connid, dn.bv_val, 0 );
187 Debug( LDAP_DEBUG_ANY,
188 "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
190 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
191 "invalid DN", NULL, NULL );
195 if( ndn.bv_len == 0 ) {
197 LDAP_LOG( OPERATION, ERR,
198 "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
200 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
203 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
204 NULL, "modify upon the root DSE not supported", NULL, NULL );
207 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
209 LDAP_LOG( OPERATION, ERR,
210 "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0 );
212 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
215 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
216 NULL, "modification of subschema subentry not supported",
223 LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0 );
225 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
228 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
230 LDAP_LOG( OPERATION, DETAIL1, "\t%s: %s\n",
231 tmp->sml_op == LDAP_MOD_ADD ?
232 "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
233 "delete" : "replace"), tmp->sml_type.bv_val, 0 );
235 if ( tmp->sml_bvalues == NULL ) {
236 LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
237 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
238 LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
239 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
240 LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
242 LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
246 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
247 tmp->sml_op == LDAP_MOD_ADD
248 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
249 ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
251 if ( tmp->sml_bvalues == NULL ) {
252 Debug( LDAP_DEBUG_ARGS, "%s\n",
253 "\t\tno values", NULL, NULL );
254 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
255 Debug( LDAP_DEBUG_ARGS, "%s\n",
256 "\t\tzero values", NULL, NULL );
257 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
258 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
259 "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
261 Debug( LDAP_DEBUG_ARGS, "%s\n",
262 "\t\tmultiple values", NULL, NULL );
268 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
269 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
271 manageDSAit = get_manageDSAit( op );
274 * We could be serving multiple database backends. Select the
275 * appropriate one, or send a referral to our "referral server"
276 * if we don't hold it.
278 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
279 BerVarray ref = referral_rewrite( default_referral,
280 NULL, &pdn, LDAP_SCOPE_DEFAULT );
282 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
283 NULL, NULL, ref ? ref : default_referral, NULL );
285 ber_bvarray_free( ref );
289 /* check restrictions */
290 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
291 if( rc != LDAP_SUCCESS ) {
292 send_ldap_result( conn, op, rc,
293 NULL, text, NULL, NULL );
297 /* check for referrals */
298 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
299 if ( rc != LDAP_SUCCESS ) {
303 /* deref suffix alias if appropriate */
304 suffix_alias( be, &ndn );
307 * do the modify if 1 && (2 || 3)
308 * 1) there is a modify function implemented in this backend;
309 * 2) this backend is master for what it holds;
310 * 3) it's a replica and the dn supplied is the update_ndn.
312 if ( be->be_modify ) {
313 /* do the update here */
314 int repl_user = be_isupdate( be, &op->o_ndn );
315 #ifndef SLAPD_MULTIMASTER
316 /* Multimaster slapd does not have to check for replicator dn
317 * because it accepts each modify request
319 if ( !be->be_update_ndn.bv_len || repl_user )
322 int update = be->be_update_ndn.bv_len;
324 char textbuf[SLAP_TEXT_BUFLEN];
325 size_t textlen = sizeof textbuf;
327 rc = slap_mods_check( modlist, update, &text,
330 if( rc != LDAP_SUCCESS ) {
331 send_ldap_result( conn, op, rc,
332 NULL, text, NULL, NULL );
337 for( modtail = &modlist;
339 modtail = &(*modtail)->sml_next )
344 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
346 if( rc != LDAP_SUCCESS ) {
347 send_ldap_result( conn, op, rc,
354 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
355 #ifdef SLAPD_MULTIMASTER
359 /* but we log only the ones not from a replicator user */
360 replog( be, op, &pdn, &ndn, modlist );
363 #ifndef SLAPD_MULTIMASTER
364 /* send a referral */
366 BerVarray defref = be->be_update_refs
367 ? be->be_update_refs : default_referral;
368 BerVarray ref = referral_rewrite( defref,
369 NULL, &pdn, LDAP_SCOPE_DEFAULT );
371 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
372 ref ? ref : defref, NULL );
374 ber_bvarray_free( ref );
378 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
379 NULL, "operation not supported within namingContext",
386 if ( modlist != NULL )
387 slap_mods_free( modlist );
392 * Do basic attribute type checking and syntax validation.
403 for( ; ml != NULL; ml = ml->sml_next ) {
404 AttributeDescription *ad = NULL;
406 /* convert to attribute description */
407 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
409 if( rc != LDAP_SUCCESS ) {
410 snprintf( textbuf, textlen, "%s: %s",
411 ml->sml_type.bv_val, *text );
418 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
419 && !slap_ad_is_binary( ad ))
421 /* attribute requires binary transfer */
422 snprintf( textbuf, textlen,
423 "%s: requires ;binary transfer",
424 ml->sml_type.bv_val );
426 return LDAP_UNDEFINED_TYPE;
429 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
430 && slap_ad_is_binary( ad ))
432 /* attribute requires binary transfer */
433 snprintf( textbuf, textlen,
434 "%s: disallows ;binary transfer",
435 ml->sml_type.bv_val );
437 return LDAP_UNDEFINED_TYPE;
440 if( slap_ad_is_lang_range( ad )) {
441 /* attribute requires binary transfer */
442 snprintf( textbuf, textlen,
443 "%s: inappropriate use of language range option",
444 ml->sml_type.bv_val );
446 return LDAP_UNDEFINED_TYPE;
449 if (!update && is_at_no_user_mod( ad->ad_type )) {
450 /* user modification disallowed */
451 snprintf( textbuf, textlen,
452 "%s: no user modification allowed",
453 ml->sml_type.bv_val );
455 return LDAP_CONSTRAINT_VIOLATION;
458 if ( is_at_obsolete( ad->ad_type ) &&
459 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
462 * attribute is obsolete,
463 * only allow replace/delete with no values
465 snprintf( textbuf, textlen,
466 "%s: attribute is obsolete",
467 ml->sml_type.bv_val );
469 return LDAP_CONSTRAINT_VIOLATION;
475 if( ml->sml_bvalues != NULL ) {
477 slap_syntax_validate_func *validate =
478 ad->ad_type->sat_syntax->ssyn_validate;
479 slap_syntax_transform_func *pretty =
480 ad->ad_type->sat_syntax->ssyn_pretty;
482 if( !pretty && !validate ) {
483 *text = "no validator for syntax";
484 snprintf( textbuf, textlen,
485 "%s: no validator for syntax %s",
487 ad->ad_type->sat_syntax->ssyn_oid );
489 return LDAP_INVALID_SYNTAX;
493 * check that each value is valid per syntax
494 * and pretty if appropriate
496 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
499 rc = pretty( ad->ad_type->sat_syntax,
500 &ml->sml_bvalues[nvals], &pval );
502 rc = validate( ad->ad_type->sat_syntax,
503 &ml->sml_bvalues[nvals] );
507 snprintf( textbuf, textlen,
508 "%s: value #%ld invalid per syntax",
509 ml->sml_type.bv_val, (long) nvals );
511 return LDAP_INVALID_SYNTAX;
515 ber_memfree( ml->sml_bvalues[nvals].bv_val );
516 ml->sml_bvalues[nvals] = pval;
521 * a rough single value check... an additional check is needed
522 * to catch add of single value to existing single valued attribute
524 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
525 && nvals > 1 && is_at_single_value( ad->ad_type ))
527 snprintf( textbuf, textlen,
528 "%s: multiple value provided",
529 ml->sml_type.bv_val );
531 return LDAP_CONSTRAINT_VIOLATION;
539 int slap_mods_opattrs(
543 Modifications **modtail,
545 char *textbuf, size_t textlen )
547 struct berval name, timestamp, csn;
548 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
549 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
552 int mop = op->o_tag == LDAP_REQ_ADD
553 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
555 assert( modtail != NULL );
556 assert( *modtail == NULL );
558 if( SLAP_LASTMOD(be) ) {
560 time_t now = slap_get_time();
562 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
563 ltm = gmtime( &now );
564 lutil_gentime( timebuf, sizeof(timebuf), ltm );
566 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
567 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
570 timestamp.bv_val = timebuf;
571 timestamp.bv_len = strlen(timebuf);
573 if( op->o_dn.bv_len == 0 ) {
574 name.bv_val = SLAPD_ANONYMOUS;
575 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
581 if( op->o_tag == LDAP_REQ_ADD ) {
582 struct berval tmpval;
584 if( global_schemacheck ) {
585 int rc = mods_structural_class( mods, &tmpval,
586 text, textbuf, textlen );
587 if( rc != LDAP_SUCCESS ) {
591 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
593 mod->sml_type.bv_val = NULL;
594 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
595 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
596 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
597 mod->sml_bvalues[1].bv_val = NULL;
598 assert( mod->sml_bvalues[0].bv_val );
600 modtail = &mod->sml_next;
603 if( SLAP_LASTMOD(be) ) {
604 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
606 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
607 tmpval.bv_val = uuidbuf;
609 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
611 mod->sml_type.bv_val = NULL;
612 mod->sml_desc = slap_schema.si_ad_entryUUID;
613 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
614 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
615 mod->sml_bvalues[1].bv_val = NULL;
616 assert( mod->sml_bvalues[0].bv_val );
618 modtail = &mod->sml_next;
620 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
622 mod->sml_type.bv_val = NULL;
623 mod->sml_desc = slap_schema.si_ad_creatorsName;
624 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
625 ber_dupbv( &mod->sml_bvalues[0], &name );
626 mod->sml_bvalues[1].bv_val = NULL;
627 assert( mod->sml_bvalues[0].bv_val );
629 modtail = &mod->sml_next;
631 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
633 mod->sml_type.bv_val = NULL;
634 mod->sml_desc = slap_schema.si_ad_createTimestamp;
635 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
636 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
637 mod->sml_bvalues[1].bv_val = NULL;
638 assert( mod->sml_bvalues[0].bv_val );
640 modtail = &mod->sml_next;
644 if( SLAP_LASTMOD(be) ) {
645 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
647 mod->sml_type.bv_val = NULL;
648 mod->sml_desc = slap_schema.si_ad_entryCSN;
649 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
650 ber_dupbv( &mod->sml_bvalues[0], &csn );
651 mod->sml_bvalues[1].bv_val = NULL;
652 assert( mod->sml_bvalues[0].bv_val );
654 modtail = &mod->sml_next;
656 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
658 mod->sml_type.bv_val = NULL;
659 mod->sml_desc = slap_schema.si_ad_modifiersName;
660 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
661 ber_dupbv( &mod->sml_bvalues[0], &name );
662 mod->sml_bvalues[1].bv_val = NULL;
663 assert( mod->sml_bvalues[0].bv_val );
665 modtail = &mod->sml_next;
667 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
669 mod->sml_type.bv_val = NULL;
670 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
671 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
672 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
673 mod->sml_bvalues[1].bv_val = NULL;
674 assert( mod->sml_bvalues[0].bv_val );
676 modtail = &mod->sml_next;