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 #if defined( SLAPD_SCHEMA_DN )
208 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
210 LDAP_LOG( OPERATION, ERR,
211 "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0 );
213 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
216 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
217 NULL, "modification of subschema subentry not supported",
225 LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0 );
227 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
230 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
232 LDAP_LOG( OPERATION, DETAIL1, "\t%s: %s\n",
233 tmp->sml_op == LDAP_MOD_ADD ?
234 "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
235 "delete" : "replace"), tmp->sml_type.bv_val, 0 );
237 if ( tmp->sml_bvalues == NULL ) {
238 LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
239 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
240 LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
241 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
242 LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
244 LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
248 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
249 tmp->sml_op == LDAP_MOD_ADD
250 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
251 ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
253 if ( tmp->sml_bvalues == NULL ) {
254 Debug( LDAP_DEBUG_ARGS, "%s\n",
255 "\t\tno values", NULL, NULL );
256 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
257 Debug( LDAP_DEBUG_ARGS, "%s\n",
258 "\t\tzero values", NULL, NULL );
259 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
260 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
261 "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
263 Debug( LDAP_DEBUG_ARGS, "%s\n",
264 "\t\tmultiple values", NULL, NULL );
270 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
271 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
273 manageDSAit = get_manageDSAit( op );
276 * We could be serving multiple database backends. Select the
277 * appropriate one, or send a referral to our "referral server"
278 * if we don't hold it.
280 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
281 BerVarray ref = referral_rewrite( default_referral,
282 NULL, &pdn, LDAP_SCOPE_DEFAULT );
284 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
285 NULL, NULL, ref ? ref : default_referral, NULL );
287 ber_bvarray_free( ref );
291 /* check restrictions */
292 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
293 if( rc != LDAP_SUCCESS ) {
294 send_ldap_result( conn, op, rc,
295 NULL, text, NULL, NULL );
299 /* check for referrals */
300 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
301 if ( rc != LDAP_SUCCESS ) {
305 /* deref suffix alias if appropriate */
306 suffix_alias( be, &ndn );
309 * do the modify if 1 && (2 || 3)
310 * 1) there is a modify function implemented in this backend;
311 * 2) this backend is master for what it holds;
312 * 3) it's a replica and the dn supplied is the update_ndn.
314 if ( be->be_modify ) {
315 /* do the update here */
316 int repl_user = be_isupdate( be, &op->o_ndn );
317 #ifndef SLAPD_MULTIMASTER
318 /* Multimaster slapd does not have to check for replicator dn
319 * because it accepts each modify request
321 if ( !be->be_update_ndn.bv_len || repl_user )
324 int update = be->be_update_ndn.bv_len;
326 char textbuf[SLAP_TEXT_BUFLEN];
327 size_t textlen = sizeof textbuf;
329 rc = slap_mods_check( modlist, update, &text,
332 if( rc != LDAP_SUCCESS ) {
333 send_ldap_result( conn, op, rc,
334 NULL, text, NULL, NULL );
339 for( modtail = &modlist;
341 modtail = &(*modtail)->sml_next )
346 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
348 if( rc != LDAP_SUCCESS ) {
349 send_ldap_result( conn, op, rc,
356 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
357 #ifdef SLAPD_MULTIMASTER
361 /* but we log only the ones not from a replicator user */
362 replog( be, op, &pdn, &ndn, modlist );
365 #ifndef SLAPD_MULTIMASTER
366 /* send a referral */
368 BerVarray defref = be->be_update_refs
369 ? be->be_update_refs : default_referral;
370 BerVarray ref = referral_rewrite( defref,
371 NULL, &pdn, LDAP_SCOPE_DEFAULT );
373 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
374 ref ? ref : defref, NULL );
376 ber_bvarray_free( ref );
380 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
381 NULL, "operation not supported within namingContext",
388 if ( modlist != NULL )
389 slap_mods_free( modlist );
394 * Do basic attribute type checking and syntax validation.
405 for( ; ml != NULL; ml = ml->sml_next ) {
406 AttributeDescription *ad = NULL;
408 /* convert to attribute description */
409 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
411 if( rc != LDAP_SUCCESS ) {
412 snprintf( textbuf, textlen, "%s: %s",
413 ml->sml_type.bv_val, *text );
420 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
421 && !slap_ad_is_binary( ad ))
423 /* attribute requires binary transfer */
424 snprintf( textbuf, textlen,
425 "%s: requires ;binary transfer",
426 ml->sml_type.bv_val );
428 return LDAP_UNDEFINED_TYPE;
431 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
432 && slap_ad_is_binary( ad ))
434 /* attribute requires binary transfer */
435 snprintf( textbuf, textlen,
436 "%s: disallows ;binary transfer",
437 ml->sml_type.bv_val );
439 return LDAP_UNDEFINED_TYPE;
442 if( slap_ad_is_lang_range( ad )) {
443 /* attribute requires binary transfer */
444 snprintf( textbuf, textlen,
445 "%s: inappropriate use of language range option",
446 ml->sml_type.bv_val );
448 return LDAP_UNDEFINED_TYPE;
451 if (!update && is_at_no_user_mod( ad->ad_type )) {
452 /* user modification disallowed */
453 snprintf( textbuf, textlen,
454 "%s: no user modification allowed",
455 ml->sml_type.bv_val );
457 return LDAP_CONSTRAINT_VIOLATION;
460 if ( is_at_obsolete( ad->ad_type ) &&
461 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
464 * attribute is obsolete,
465 * only allow replace/delete with no values
467 snprintf( textbuf, textlen,
468 "%s: attribute is obsolete",
469 ml->sml_type.bv_val );
471 return LDAP_CONSTRAINT_VIOLATION;
477 if( ml->sml_bvalues != NULL ) {
479 slap_syntax_validate_func *validate =
480 ad->ad_type->sat_syntax->ssyn_validate;
481 slap_syntax_transform_func *pretty =
482 ad->ad_type->sat_syntax->ssyn_pretty;
484 if( !pretty && !validate ) {
485 *text = "no validator for syntax";
486 snprintf( textbuf, textlen,
487 "%s: no validator for syntax %s",
489 ad->ad_type->sat_syntax->ssyn_oid );
491 return LDAP_INVALID_SYNTAX;
495 * check that each value is valid per syntax
496 * and pretty if appropriate
498 for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
501 rc = pretty( ad->ad_type->sat_syntax,
502 &ml->sml_bvalues[nvals], &pval );
504 rc = validate( ad->ad_type->sat_syntax,
505 &ml->sml_bvalues[nvals] );
509 snprintf( textbuf, textlen,
510 "%s: value #%ld invalid per syntax",
511 ml->sml_type.bv_val, (long) nvals );
513 return LDAP_INVALID_SYNTAX;
517 ber_memfree( ml->sml_bvalues[nvals].bv_val );
518 ml->sml_bvalues[nvals] = pval;
523 * a rough single value check... an additional check is needed
524 * to catch add of single value to existing single valued attribute
526 if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
527 && nvals > 1 && is_at_single_value( ad->ad_type ))
529 snprintf( textbuf, textlen,
530 "%s: multiple value provided",
531 ml->sml_type.bv_val );
533 return LDAP_CONSTRAINT_VIOLATION;
541 int slap_mods_opattrs(
545 Modifications **modtail,
547 char *textbuf, size_t textlen )
549 struct berval name, timestamp, csn;
554 int mop = op->o_tag == LDAP_REQ_ADD
555 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
557 assert( modtail != NULL );
558 assert( *modtail == NULL );
560 if( SLAP_LASTMOD(be) ) {
562 time_t now = slap_get_time();
564 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
565 ltm = gmtime( &now );
566 lutil_gentime( timebuf, sizeof(timebuf), ltm );
568 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
569 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
572 timestamp.bv_val = timebuf;
573 timestamp.bv_len = strlen(timebuf);
575 if( op->o_dn.bv_len == 0 ) {
576 name.bv_val = SLAPD_ANONYMOUS;
577 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
583 if( op->o_tag == LDAP_REQ_ADD ) {
584 struct berval tmpval;
586 if( global_schemacheck ) {
587 int rc = mods_structural_class( mods, &tmpval,
588 text, textbuf, textlen );
589 if( rc != LDAP_SUCCESS ) {
593 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
595 mod->sml_type.bv_val = NULL;
596 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
597 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
598 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
599 mod->sml_bvalues[1].bv_val = NULL;
600 assert( mod->sml_bvalues[0].bv_val );
602 modtail = &mod->sml_next;
605 if( SLAP_LASTMOD(be) ) {
608 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
609 tmpval.bv_val = uuidbuf;
611 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
613 mod->sml_type.bv_val = NULL;
614 mod->sml_desc = slap_schema.si_ad_entryUUID;
615 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
616 ber_dupbv( &mod->sml_bvalues[0], &tmpval );
617 mod->sml_bvalues[1].bv_val = NULL;
618 assert( mod->sml_bvalues[0].bv_val );
620 modtail = &mod->sml_next;
622 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
624 mod->sml_type.bv_val = NULL;
625 mod->sml_desc = slap_schema.si_ad_creatorsName;
626 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
627 ber_dupbv( &mod->sml_bvalues[0], &name );
628 mod->sml_bvalues[1].bv_val = NULL;
629 assert( mod->sml_bvalues[0].bv_val );
631 modtail = &mod->sml_next;
633 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
635 mod->sml_type.bv_val = NULL;
636 mod->sml_desc = slap_schema.si_ad_createTimestamp;
637 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
638 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
639 mod->sml_bvalues[1].bv_val = NULL;
640 assert( mod->sml_bvalues[0].bv_val );
642 modtail = &mod->sml_next;
646 if( SLAP_LASTMOD(be) ) {
647 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
649 mod->sml_type.bv_val = NULL;
650 mod->sml_desc = slap_schema.si_ad_entryCSN;
651 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
652 ber_dupbv( &mod->sml_bvalues[0], &csn );
653 mod->sml_bvalues[1].bv_val = NULL;
654 assert( mod->sml_bvalues[0].bv_val );
656 modtail = &mod->sml_next;
658 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
660 mod->sml_type.bv_val = NULL;
661 mod->sml_desc = slap_schema.si_ad_modifiersName;
662 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
663 ber_dupbv( &mod->sml_bvalues[0], &name );
664 mod->sml_bvalues[1].bv_val = NULL;
665 assert( mod->sml_bvalues[0].bv_val );
667 modtail = &mod->sml_next;
669 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
671 mod->sml_type.bv_val = NULL;
672 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
673 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
674 ber_dupbv( &mod->sml_bvalues[0], ×tamp );
675 mod->sml_bvalues[1].bv_val = NULL;
676 assert( mod->sml_bvalues[0].bv_val );
678 modtail = &mod->sml_next;