3 * Copyright 1998-2000 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 = NULL;
38 struct berval *ndn = NULL;
42 LDAPModList *modlist = NULL;
43 LDAPModList **modtail = &modlist;
47 Modifications *mods = NULL;
54 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
55 "do_modify: enter\n" ));
57 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
61 * Parse the modify request. It looks like this:
63 * ModifyRequest := [APPLICATION 6] SEQUENCE {
64 * name DistinguishedName,
65 * mods SEQUENCE OF SEQUENCE {
66 * operation ENUMERATED {
71 * modification SEQUENCE {
73 * values SET OF AttributeValue
79 if ( ber_scanf( op->o_ber, "{o" /*}*/, &dn ) == LBER_ERROR ) {
81 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
82 "do_modify: ber_scanf failed\n" ));
84 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
87 send_ldap_disconnect( conn, op,
88 LDAP_PROTOCOL_ERROR, "decoding error" );
89 return SLAPD_DISCONNECT;
93 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
94 "do_modify: dn (%s)\n", dn.bv_val ));
96 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
100 /* collect modifications & save for later */
102 for ( tag = ber_first_element( op->o_ber, &len, &last );
104 tag = ber_next_element( op->o_ber, &len, last ) )
108 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
110 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
111 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
114 send_ldap_disconnect( conn, op,
115 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
116 rc = SLAPD_DISCONNECT;
122 if ( (*modtail)->ml_bvalues == NULL ) {
124 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
125 "do_modify: modify/add operation (%ld) requires values\n",
128 Debug( LDAP_DEBUG_ANY,
129 "do_modify: modify/add operation (%ld) requires values\n",
133 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
134 NULL, "modify/add operation requires values",
136 rc = LDAP_PROTOCOL_ERROR;
142 case LDAP_MOD_DELETE:
143 case LDAP_MOD_REPLACE:
148 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
149 "do_modify: invalid modify operation (%ld)\n",
152 Debug( LDAP_DEBUG_ANY,
153 "do_modify: invalid modify operation (%ld)\n",
157 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
158 NULL, "unrecognized modify operation", NULL, NULL );
159 rc = LDAP_PROTOCOL_ERROR;
164 (*modtail)->ml_op = mop;
165 modtail = &(*modtail)->ml_next;
169 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
171 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
172 "do_modify: get_ctrls failed\n" ));
174 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
180 rc = dnPretty( NULL, &dn, &pdn );
181 if( rc != LDAP_SUCCESS ) {
183 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
184 "do_modify: conn %d invalid dn (%s)\n",
185 conn->c_connid, dn.bv_val ));
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 rc = dnNormalize( NULL, &dn, &ndn );
196 if( rc != LDAP_SUCCESS ) {
198 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
199 "do_modify: conn %d invalid dn (%s)\n",
200 conn->c_connid, dn.bv_val ));
202 Debug( LDAP_DEBUG_ANY,
203 "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
205 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
206 "invalid DN", NULL, NULL );
210 if( ndn->bv_len == 0 ) {
212 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
213 "do_modify: attempt to modify root DSE.\n" ));
215 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
218 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
219 NULL, "modify upon the root DSE not supported", NULL, NULL );
222 #if defined( SLAPD_SCHEMA_DN )
223 } else if ( strcasecmp( ndn->bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
225 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
226 "do_modify: attempt to modify subschema subentry.\n" ));
228 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
231 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
232 NULL, "modification of subschema subentry not supported",
240 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
241 "do_modify: modifications:\n" ));
243 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
246 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
248 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
249 "\t%s: %s\n", tmp->ml_op == LDAP_MOD_ADD ?
250 "add" : (tmp->ml_op == LDAP_MOD_DELETE ?
251 "delete" : "replace"), tmp->ml_type ));
253 if ( tmp->ml_bvalues == NULL ) {
254 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
256 } else if ( tmp->ml_bvalues[0] == NULL ) {
257 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
258 "\t\tzero values" ));
259 } else if ( tmp->ml_bvalues[1] == NULL ) {
260 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
263 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
264 "\t\tmultiple values" ));
268 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
269 tmp->ml_op == LDAP_MOD_ADD
270 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
271 ? "delete" : "replace"), tmp->ml_type, 0 );
273 if ( tmp->ml_bvalues == NULL ) {
274 Debug( LDAP_DEBUG_ARGS, "%s\n",
275 "\t\tno values", NULL, NULL );
276 } else if ( tmp->ml_bvalues[0] == NULL ) {
277 Debug( LDAP_DEBUG_ARGS, "%s\n",
278 "\t\tzero values", NULL, NULL );
279 } else if ( tmp->ml_bvalues[1] == NULL ) {
280 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
281 "\t\tone value", (long) tmp->ml_bvalues[0]->bv_len, NULL );
283 Debug( LDAP_DEBUG_ARGS, "%s\n",
284 "\t\tmultiple values", NULL, NULL );
290 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
291 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
293 manageDSAit = get_manageDSAit( op );
296 * We could be serving multiple database backends. Select the
297 * appropriate one, or send a referral to our "referral server"
298 * if we don't hold it.
300 if ( (be = select_backend( ndn->bv_val, manageDSAit, 0 )) == NULL ) {
301 struct berval **ref = referral_rewrite( default_referral,
302 NULL, pdn->bv_val, LDAP_SCOPE_DEFAULT );
304 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
305 NULL, NULL, ref ? ref : default_referral, NULL );
311 /* check restrictions */
312 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
313 if( rc != LDAP_SUCCESS ) {
314 send_ldap_result( conn, op, rc,
315 NULL, text, NULL, NULL );
319 /* check for referrals */
320 rc = backend_check_referrals( be, conn, op, pdn->bv_val, ndn->bv_val );
321 if ( rc != LDAP_SUCCESS ) {
325 /* deref suffix alias if appropriate */
326 suffix_alias( be, ndn );
329 * do the modify if 1 && (2 || 3)
330 * 1) there is a modify function implemented in this backend;
331 * 2) this backend is master for what it holds;
332 * 3) it's a replica and the dn supplied is the update_ndn.
334 if ( be->be_modify ) {
335 /* do the update here */
336 int repl_user = be_isupdate( be, op->o_ndn.bv_val );
337 #ifndef SLAPD_MULTIMASTER
338 /* Multimaster slapd does not have to check for replicator dn
339 * because it accepts each modify request
341 if ( be->be_update_ndn == NULL || repl_user )
344 int update = be->be_update_ndn != NULL;
346 char textbuf[SLAP_TEXT_BUFLEN];
347 size_t textlen = sizeof textbuf;
349 rc = slap_modlist2mods( modlist, update, &mods, &text,
352 if( rc != LDAP_SUCCESS ) {
353 send_ldap_result( conn, op, rc,
354 NULL, text, NULL, NULL );
358 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
359 global_lastmod == ON)) && !repl_user )
361 Modifications **modstail;
362 for( modstail = &mods;
364 modstail = &(*modstail)->sml_next )
369 rc = slap_mods_opattrs( op, mods, modstail, &text,
371 if( rc != LDAP_SUCCESS ) {
372 send_ldap_result( conn, op, rc,
379 if ( (*be->be_modify)( be, conn, op, pdn->bv_val, ndn->bv_val, mods ) == 0
380 #ifdef SLAPD_MULTIMASTER
384 /* but we log only the ones not from a replicator user */
385 replog( be, op, pdn->bv_val, ndn->bv_val, mods );
388 #ifndef SLAPD_MULTIMASTER
389 /* send a referral */
391 struct berval **defref = be->be_update_refs
392 ? be->be_update_refs : default_referral;
393 struct berval **ref = referral_rewrite( defref,
394 NULL, pdn->bv_val, LDAP_SCOPE_DEFAULT );
396 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
397 ref ? ref : defref, NULL );
403 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
404 NULL, "operation not supported within namingContext",
410 if( pdn != NULL ) ber_bvfree( pdn );
411 if( ndn != NULL ) ber_bvfree( ndn );
412 if ( modlist != NULL )
413 slap_modlist_free( modlist );
415 slap_mods_free( mods );
420 * convert a raw list of modifications to internal format
421 * Do basic attribute type checking and syntax validation.
423 int slap_modlist2mods(
426 Modifications **mods,
432 Modifications **modtail = mods;
434 for( ; ml != NULL; ml = ml->ml_next ) {
436 AttributeDescription *ad = NULL;
438 mod = (Modifications *)
439 ch_calloc( 1, sizeof(Modifications) );
442 mod->sml_op = ml->ml_op;
444 /* convert to attribute description */
445 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
447 if( rc != LDAP_SUCCESS ) {
448 slap_mods_free( mod );
449 snprintf( textbuf, textlen, "%s: %s",
450 ml->ml_type, *text );
457 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
458 && !slap_ad_is_binary( ad ))
460 /* attribute requires binary transfer */
461 slap_mods_free( mod );
463 snprintf( textbuf, textlen,
464 "%s: requires ;binary transfer",
467 return LDAP_UNDEFINED_TYPE;
470 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
471 && slap_ad_is_binary( ad ))
473 /* attribute requires binary transfer */
474 slap_mods_free( mod );
475 snprintf( textbuf, textlen,
476 "%s: disallows ;binary transfer",
479 return LDAP_UNDEFINED_TYPE;
482 if (!update && is_at_no_user_mod( ad->ad_type )) {
483 /* user modification disallowed */
484 slap_mods_free( mod );
485 snprintf( textbuf, textlen,
486 "%s: no user modification allowed",
489 return LDAP_CONSTRAINT_VIOLATION;
492 if ( is_at_obsolete( ad->ad_type ) &&
493 ( mod->sml_op == LDAP_MOD_ADD || ml->ml_bvalues != NULL ) )
496 * attribute is obsolete,
497 * only allow replace/delete with no values
499 slap_mods_free( mod );
500 snprintf( textbuf, textlen,
501 "%s: attribute is obsolete",
504 return LDAP_CONSTRAINT_VIOLATION;
510 if( ml->ml_bvalues != NULL ) {
512 slap_syntax_validate_func *validate =
513 ad->ad_type->sat_syntax->ssyn_validate;
514 slap_syntax_transform_func *pretty =
515 ad->ad_type->sat_syntax->ssyn_pretty;
517 if( !pretty && !validate ) {
518 slap_mods_free( mod );
519 *text = "no validator for syntax";
520 snprintf( textbuf, textlen,
521 "%s: no validator for syntax %s",
523 ad->ad_type->sat_syntax->ssyn_oid );
525 return LDAP_INVALID_SYNTAX;
529 * check that each value is valid per syntax
530 * and pretty if appropriate
532 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
533 struct berval *pval = NULL;
535 rc = pretty( ad->ad_type->sat_syntax,
536 ml->ml_bvalues[nvals], &pval );
538 rc = validate( ad->ad_type->sat_syntax,
539 ml->ml_bvalues[nvals] );
543 slap_mods_free( mod );
544 snprintf( textbuf, textlen,
545 "%s: value #%ld invalid per syntax",
546 ml->ml_type, (long) nvals );
548 return LDAP_INVALID_SYNTAX;
552 ber_memfree( ml->ml_bvalues[nvals]->bv_val );
553 *ml->ml_bvalues[nvals] = *pval;
559 * a rough single value check... an additional check is needed
560 * to catch add of single value to existing single valued attribute
562 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
563 && nvals > 1 && is_at_single_value( ad->ad_type ))
565 slap_mods_free( mod );
566 snprintf( textbuf, textlen,
567 "%s: multiple value provided",
570 return LDAP_CONSTRAINT_VIOLATION;
574 mod->sml_bvalues = ml->ml_bvalues;
575 ml->ml_values = NULL;
578 modtail = &mod->sml_next;
584 int slap_mods_opattrs(
587 Modifications **modtail,
589 char *textbuf, size_t textlen )
591 struct berval name, timestamp, csn;
592 time_t now = slap_get_time();
598 int mop = op->o_tag == LDAP_REQ_ADD
599 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
601 assert( modtail != NULL );
602 assert( *modtail == NULL );
604 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
605 ltm = gmtime( &now );
606 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
608 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
609 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
612 timestamp.bv_val = timebuf;
613 timestamp.bv_len = strlen(timebuf);
615 if( op->o_dn.bv_len == 0 ) {
616 name.bv_val = SLAPD_ANONYMOUS;
617 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
622 if( op->o_tag == LDAP_REQ_ADD ) {
623 struct berval tmpval;
627 rc = mods_structural_class( mods, &tmpval, text, textbuf, textlen );
628 if( rc != LDAP_SUCCESS ) {
631 if ( tmpval.bv_len ) {
632 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
634 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
635 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
636 mod->sml_bvalues[0] = ber_bvdup( &tmpval );
637 mod->sml_bvalues[1] = NULL;
638 assert( mod->sml_bvalues[0] );
640 modtail = &mod->sml_next;
643 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
644 tmpval.bv_val = uuidbuf;
646 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
648 mod->sml_desc = slap_schema.si_ad_entryUUID;
649 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
650 mod->sml_bvalues[0] = ber_bvdup( &tmpval );
651 mod->sml_bvalues[1] = NULL;
652 assert( mod->sml_bvalues[0] );
654 modtail = &mod->sml_next;
656 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
658 mod->sml_desc = slap_schema.si_ad_creatorsName;
659 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
660 mod->sml_bvalues[0] = ber_bvdup( &name );
661 mod->sml_bvalues[1] = NULL;
662 assert( mod->sml_bvalues[0] );
664 modtail = &mod->sml_next;
666 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
668 mod->sml_desc = slap_schema.si_ad_createTimestamp;
669 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
670 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
671 mod->sml_bvalues[1] = NULL;
672 assert( mod->sml_bvalues[0] );
674 modtail = &mod->sml_next;
677 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
679 mod->sml_desc = slap_schema.si_ad_entryCSN;
680 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
681 mod->sml_bvalues[0] = ber_bvdup( &csn );
682 mod->sml_bvalues[1] = NULL;
683 assert( mod->sml_bvalues[0] );
685 modtail = &mod->sml_next;
687 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
689 mod->sml_desc = slap_schema.si_ad_modifiersName;
690 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
691 mod->sml_bvalues[0] = ber_bvdup( &name );
692 mod->sml_bvalues[1] = NULL;
693 assert( mod->sml_bvalues[0] );
695 modtail = &mod->sml_next;
697 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
699 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
700 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
701 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
702 mod->sml_bvalues[1] = NULL;
703 assert( mod->sml_bvalues[0] );
705 modtail = &mod->sml_next;