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", LDAP_LEVEL_ENTRY,
54 "do_modify: enter\n" ));
56 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
60 * Parse the modify request. It looks like this:
62 * ModifyRequest := [APPLICATION 6] SEQUENCE {
63 * name DistinguishedName,
64 * mods SEQUENCE OF SEQUENCE {
65 * operation ENUMERATED {
70 * modification SEQUENCE {
72 * values SET OF AttributeValue
78 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
80 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
81 "do_modify: ber_scanf failed\n" ));
83 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
86 send_ldap_disconnect( conn, op,
87 LDAP_PROTOCOL_ERROR, "decoding error" );
88 return SLAPD_DISCONNECT;
92 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
93 "do_modify: dn (%s)\n", dn.bv_val ));
95 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
99 /* collect modifications & save for later */
101 for ( tag = ber_first_element( op->o_ber, &len, &last );
103 tag = ber_next_element( op->o_ber, &len, last ) )
106 Modifications tmp, *mod;
109 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
110 &tmp.sml_type, &tmp.sml_bvalues )
113 send_ldap_disconnect( conn, op,
114 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
115 rc = SLAPD_DISCONNECT;
119 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
120 mod->sml_type = tmp.sml_type;
121 mod->sml_bvalues = tmp.sml_bvalues;
122 mod->sml_desc = NULL;
127 if ( mod->sml_bvalues == NULL ) {
129 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
130 "do_modify: modify/add operation (%ld) requires values\n",
133 Debug( LDAP_DEBUG_ANY,
134 "do_modify: modify/add operation (%ld) requires values\n",
138 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
139 NULL, "modify/add operation requires values",
141 rc = LDAP_PROTOCOL_ERROR;
147 case LDAP_MOD_DELETE:
148 case LDAP_MOD_REPLACE:
153 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
154 "do_modify: invalid modify operation (%ld)\n",
157 Debug( LDAP_DEBUG_ANY,
158 "do_modify: invalid modify operation (%ld)\n",
162 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
163 NULL, "unrecognized modify operation", NULL, NULL );
164 rc = LDAP_PROTOCOL_ERROR;
170 modtail = &mod->sml_next;
174 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
176 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
177 "do_modify: get_ctrls failed\n" ));
179 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
185 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
186 if( rc != LDAP_SUCCESS ) {
188 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
189 "do_modify: conn %d invalid dn (%s)\n",
190 conn->c_connid, dn.bv_val ));
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", LDAP_LEVEL_ERR,
203 "do_modify: attempt to modify root DSE.\n" ));
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 #if defined( SLAPD_SCHEMA_DN )
213 } else if ( strcasecmp( ndn.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
215 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
216 "do_modify: attempt to modify subschema subentry.\n" ));
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",
230 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
231 "do_modify: modifications:\n" ));
233 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
236 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
238 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
239 "\t%s: %s\n", tmp->sml_op == LDAP_MOD_ADD ?
240 "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
241 "delete" : "replace"), tmp->sml_type.bv_val ));
243 if ( tmp->sml_bvalues == NULL ) {
244 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
246 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
247 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
248 "\t\tzero values" ));
249 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
250 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
253 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
254 "\t\tmultiple values" ));
258 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
259 tmp->sml_op == LDAP_MOD_ADD
260 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
261 ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
263 if ( tmp->sml_bvalues == NULL ) {
264 Debug( LDAP_DEBUG_ARGS, "%s\n",
265 "\t\tno values", NULL, NULL );
266 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
267 Debug( LDAP_DEBUG_ARGS, "%s\n",
268 "\t\tzero values", NULL, NULL );
269 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
270 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
271 "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
273 Debug( LDAP_DEBUG_ARGS, "%s\n",
274 "\t\tmultiple values", NULL, NULL );
280 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
281 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
283 manageDSAit = get_manageDSAit( op );
286 * We could be serving multiple database backends. Select the
287 * appropriate one, or send a referral to our "referral server"
288 * if we don't hold it.
290 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
291 BerVarray ref = referral_rewrite( default_referral,
292 NULL, &pdn, LDAP_SCOPE_DEFAULT );
294 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
295 NULL, NULL, ref ? ref : default_referral, NULL );
297 ber_bvarray_free( ref );
301 /* check restrictions */
302 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
303 if( rc != LDAP_SUCCESS ) {
304 send_ldap_result( conn, op, rc,
305 NULL, text, NULL, NULL );
309 /* check for referrals */
310 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
311 if ( rc != LDAP_SUCCESS ) {
315 /* deref suffix alias if appropriate */
316 suffix_alias( be, &ndn );
319 * do the modify if 1 && (2 || 3)
320 * 1) there is a modify function implemented in this backend;
321 * 2) this backend is master for what it holds;
322 * 3) it's a replica and the dn supplied is the update_ndn.
324 if ( be->be_modify ) {
325 /* do the update here */
326 int repl_user = be_isupdate( be, &op->o_ndn );
327 #ifndef SLAPD_MULTIMASTER
328 /* Multimaster slapd does not have to check for replicator dn
329 * because it accepts each modify request
331 if ( !be->be_update_ndn.bv_len || repl_user )
334 int update = be->be_update_ndn.bv_len;
336 char textbuf[SLAP_TEXT_BUFLEN];
337 size_t textlen = sizeof textbuf;
339 rc = slap_mods_check( modlist, update, &text,
342 if( rc != LDAP_SUCCESS ) {
343 send_ldap_result( conn, op, rc,
344 NULL, text, NULL, NULL );
348 if ( SLAP_LASTMOD(be) && !repl_user ) {
349 for( modtail = &modlist;
351 modtail = &(*modtail)->sml_next )
356 rc = slap_mods_opattrs( op, modlist, modtail, &text,
358 if( rc != LDAP_SUCCESS ) {
359 send_ldap_result( conn, op, rc,
366 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
367 #ifdef SLAPD_MULTIMASTER
371 /* but we log only the ones not from a replicator user */
372 replog( be, op, &pdn, &ndn, modlist );
375 #ifndef SLAPD_MULTIMASTER
376 /* send a referral */
378 BerVarray defref = be->be_update_refs
379 ? be->be_update_refs : default_referral;
380 BerVarray ref = referral_rewrite( defref,
381 NULL, &pdn, LDAP_SCOPE_DEFAULT );
383 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
384 ref ? ref : defref, NULL );
386 ber_bvarray_free( ref );
390 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
391 NULL, "operation not supported within namingContext",
398 if ( modlist != NULL )
399 slap_mods_free( modlist );
404 * Do basic attribute type checking and syntax validation.
415 for( ; ml != NULL; ml = ml->sml_next ) {
416 AttributeDescription *ad = NULL;
418 /* convert to attribute description */
419 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
421 if( rc != LDAP_SUCCESS ) {
422 snprintf( textbuf, textlen, "%s: %s",
423 ml->sml_type.bv_val, *text );
430 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
431 && !slap_ad_is_binary( ad ))
433 /* attribute requires binary transfer */
434 snprintf( textbuf, textlen,
435 "%s: requires ;binary transfer",
436 ml->sml_type.bv_val );
438 return LDAP_UNDEFINED_TYPE;
441 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
442 && slap_ad_is_binary( ad ))
444 /* attribute requires binary transfer */
445 snprintf( textbuf, textlen,
446 "%s: disallows ;binary transfer",
447 ml->sml_type.bv_val );
449 return LDAP_UNDEFINED_TYPE;
452 if( slap_ad_is_lang_range( ad )) {
453 /* attribute requires binary transfer */
454 snprintf( textbuf, textlen,
455 "%s: inappropriate use of language range option",
456 ml->sml_type.bv_val );
458 return LDAP_UNDEFINED_TYPE;
461 if (!update && is_at_no_user_mod( ad->ad_type )) {
462 /* user modification disallowed */
463 snprintf( textbuf, textlen,
464 "%s: no user modification allowed",
465 ml->sml_type.bv_val );
467 return LDAP_CONSTRAINT_VIOLATION;
470 if ( is_at_obsolete( ad->ad_type ) &&
471 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
474 * attribute is obsolete,
475 * only allow replace/delete with no values
477 snprintf( textbuf, textlen,
478 "%s: attribute is obsolete",
479 ml->sml_type.bv_val );
481 return LDAP_CONSTRAINT_VIOLATION;
487 if( ml->sml_bvalues != NULL ) {
489 slap_syntax_validate_func *validate =
490 ad->ad_type->sat_syntax->ssyn_validate;
491 slap_syntax_transform_func *pretty =
492 ad->ad_type->sat_syntax->ssyn_pretty;
494 if( !pretty && !validate ) {
495 *text = "no validator for syntax";
496 snprintf( textbuf, textlen,
497 "%s: no validator for syntax %s",
499 ad->ad_type->sat_syntax->ssyn_oid );
501 return LDAP_INVALID_SYNTAX;
505 * check that each value is valid per syntax
506 * and pretty if appropriate
508 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
511 rc = pretty( ad->ad_type->sat_syntax,
512 &ml->sml_bvalues[nvals], &pval );
514 rc = validate( ad->ad_type->sat_syntax,
515 &ml->sml_bvalues[nvals] );
519 snprintf( textbuf, textlen,
520 "%s: value #%ld invalid per syntax",
521 ml->sml_type.bv_val, (long) nvals );
523 return LDAP_INVALID_SYNTAX;
527 ber_memfree( ml->sml_bvalues[nvals].bv_val );
528 ml->sml_bvalues[nvals] = pval;
533 * a rough single value check... an additional check is needed
534 * to catch add of single value to existing single valued attribute
536 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
537 && nvals > 1 && is_at_single_value( ad->ad_type ))
539 snprintf( textbuf, textlen,
540 "%s: multiple value provided",
541 ml->sml_type.bv_val );
543 return LDAP_CONSTRAINT_VIOLATION;
551 int slap_mods_opattrs(
554 Modifications **modtail,
556 char *textbuf, size_t textlen )
558 struct berval name, timestamp, csn;
559 time_t now = slap_get_time();
565 int mop = op->o_tag == LDAP_REQ_ADD
566 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
568 assert( modtail != NULL );
569 assert( *modtail == NULL );
571 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
572 ltm = gmtime( &now );
573 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
575 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
576 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
579 timestamp.bv_val = timebuf;
580 timestamp.bv_len = strlen(timebuf);
582 if( op->o_dn.bv_len == 0 ) {
583 name.bv_val = SLAPD_ANONYMOUS;
584 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
589 if( op->o_tag == LDAP_REQ_ADD ) {
590 struct berval tmpval;
594 rc = mods_structural_class( mods, &tmpval, text, textbuf, textlen );
595 if( rc != LDAP_SUCCESS ) {
598 if ( tmpval.bv_len ) {
599 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
601 mod->sml_type.bv_val = NULL;
602 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
603 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
604 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
605 mod->sml_bvalues[1].bv_val = NULL;
606 assert( mod->sml_bvalues[0].bv_val );
608 modtail = &mod->sml_next;
611 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
612 tmpval.bv_val = uuidbuf;
614 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
616 mod->sml_type.bv_val = NULL;
617 mod->sml_desc = slap_schema.si_ad_entryUUID;
618 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
619 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
620 mod->sml_bvalues[1].bv_val = NULL;
621 assert( mod->sml_bvalues[0].bv_val );
623 modtail = &mod->sml_next;
625 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
627 mod->sml_type.bv_val = NULL;
628 mod->sml_desc = slap_schema.si_ad_creatorsName;
629 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
630 ber_dupbv( &mod->sml_bvalues[0], &name );
631 mod->sml_bvalues[1].bv_val = NULL;
632 assert( mod->sml_bvalues[0].bv_val );
634 modtail = &mod->sml_next;
636 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
638 mod->sml_type.bv_val = NULL;
639 mod->sml_desc = slap_schema.si_ad_createTimestamp;
640 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
641 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
642 mod->sml_bvalues[1].bv_val = NULL;
643 assert( mod->sml_bvalues[0].bv_val );
645 modtail = &mod->sml_next;
648 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
650 mod->sml_type.bv_val = NULL;
651 mod->sml_desc = slap_schema.si_ad_entryCSN;
652 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
653 ber_dupbv( &mod->sml_bvalues[0], &csn );
654 mod->sml_bvalues[1].bv_val = NULL;
655 assert( mod->sml_bvalues[0].bv_val );
657 modtail = &mod->sml_next;
659 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
661 mod->sml_type.bv_val = NULL;
662 mod->sml_desc = slap_schema.si_ad_modifiersName;
663 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
664 ber_dupbv( &mod->sml_bvalues[0], &name );
665 mod->sml_bvalues[1].bv_val = NULL;
666 assert( mod->sml_bvalues[0].bv_val );
668 modtail = &mod->sml_next;
670 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
672 mod->sml_type.bv_val = NULL;
673 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
674 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
675 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
676 mod->sml_bvalues[1].bv_val = NULL;
677 assert( mod->sml_bvalues[0].bv_val );
679 modtail = &mod->sml_next;