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 char *dn, *ndn = NULL;
40 LDAPModList *modlist = NULL;
41 LDAPModList **modtail = &modlist;
45 Modifications *mods = NULL;
52 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
53 "do_modify: enter\n" ));
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, "{a" /*}*/, &dn ) == LBER_ERROR ) {
79 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
80 "do_modify: ber_scanf failed\n" ));
82 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
85 send_ldap_disconnect( conn, op,
86 LDAP_PROTOCOL_ERROR, "decoding error" );
87 return SLAPD_DISCONNECT;
91 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
92 "do_modify: dn (%s)\n", dn ));
94 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
98 /* collect modifications & save for later */
100 for ( tag = ber_first_element( op->o_ber, &len, &last );
102 tag = ber_next_element( op->o_ber, &len, last ) )
106 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
108 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
109 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
112 send_ldap_disconnect( conn, op,
113 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
114 rc = SLAPD_DISCONNECT;
120 if ( (*modtail)->ml_bvalues == NULL ) {
122 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
123 "do_modify: modify/add operation (%ld) requires values\n",
126 Debug( LDAP_DEBUG_ANY,
127 "do_modify: modify/add operation (%ld) requires values\n",
131 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
132 NULL, "modify/add operation requires values",
134 rc = LDAP_PROTOCOL_ERROR;
140 case LDAP_MOD_DELETE:
141 case LDAP_MOD_REPLACE:
146 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
147 "do_modify: invalid modify operation (%ld)\n",
150 Debug( LDAP_DEBUG_ANY,
151 "do_modify: invalid modify operation (%ld)\n",
155 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
156 NULL, "unrecognized modify operation", NULL, NULL );
157 rc = LDAP_PROTOCOL_ERROR;
162 (*modtail)->ml_op = mop;
163 modtail = &(*modtail)->ml_next;
167 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
169 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
170 "do_modify: get_ctrls failed\n" ));
172 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
178 ndn = ch_strdup( dn );
180 if( dn_normalize( ndn ) == NULL ) {
182 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
183 "do_modify: invalid dn (%s)\n", dn ));
185 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
188 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
189 "invalid DN", NULL, NULL );
195 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
196 "do_modify: attempt to modify root DSE.\n" ));
198 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
201 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
202 NULL, "modify upon the root DSE not supported", NULL, NULL );
205 #if defined( SLAPD_SCHEMA_DN )
206 } else if ( strcasecmp( ndn, SLAPD_SCHEMA_DN ) == 0 ) {
208 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
209 "do_modify: attempt to modify subschema subentry.\n" ));
211 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
214 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
215 NULL, "modification of subschema subentry not supported",
223 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
224 "do_modify: modifications:\n" ));
226 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
229 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
231 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
232 "\t%s: %s\n", tmp->ml_op == LDAP_MOD_ADD ?
233 "add" : (tmp->ml_op == LDAP_MOD_DELETE ?
234 "delete" : "replace"), tmp->ml_type ));
236 if ( tmp->ml_bvalues == NULL ) {
237 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
239 } else if ( tmp->ml_bvalues[0] == NULL ) {
240 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
241 "\t\tzero values" ));
242 } else if ( tmp->ml_bvalues[1] == NULL ) {
243 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
246 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
247 "\t\tmultiple values" ));
251 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
252 tmp->ml_op == LDAP_MOD_ADD
253 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
254 ? "delete" : "replace"), tmp->ml_type, 0 );
256 if ( tmp->ml_bvalues == NULL ) {
257 Debug( LDAP_DEBUG_ARGS, "%s\n",
258 "\t\tno values", NULL, NULL );
259 } else if ( tmp->ml_bvalues[0] == NULL ) {
260 Debug( LDAP_DEBUG_ARGS, "%s\n",
261 "\t\tzero values", NULL, NULL );
262 } else if ( tmp->ml_bvalues[1] == NULL ) {
263 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
264 "\t\tone value", (long) tmp->ml_bvalues[0]->bv_len, NULL );
266 Debug( LDAP_DEBUG_ARGS, "%s\n",
267 "\t\tmultiple values", NULL, NULL );
273 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
274 op->o_connid, op->o_opid, dn, 0, 0 );
276 manageDSAit = get_manageDSAit( op );
279 * We could be serving multiple database backends. Select the
280 * appropriate one, or send a referral to our "referral server"
281 * if we don't hold it.
283 if ( (be = select_backend( ndn, manageDSAit, 0 )) == NULL ) {
284 struct berval **ref = referral_rewrite( default_referral,
285 NULL, dn, LDAP_SCOPE_DEFAULT );
287 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
288 NULL, NULL, ref ? ref : default_referral, NULL );
294 /* check restrictions */
295 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
296 if( rc != LDAP_SUCCESS ) {
297 send_ldap_result( conn, op, rc,
298 NULL, text, NULL, NULL );
302 /* check for referrals */
303 rc = backend_check_referrals( be, conn, op, dn, ndn );
304 if ( rc != LDAP_SUCCESS ) {
308 /* deref suffix alias if appropriate */
309 ndn = suffix_alias( be, ndn );
312 * do the modify if 1 && (2 || 3)
313 * 1) there is a modify function implemented in this backend;
314 * 2) this backend is master for what it holds;
315 * 3) it's a replica and the dn supplied is the update_ndn.
317 if ( be->be_modify ) {
318 /* do the update here */
319 int repl_user = be_isupdate( be, op->o_ndn );
320 #ifndef SLAPD_MULTIMASTER
321 /* Multimaster slapd does not have to check for replicator dn
322 * because it accepts each modify request
324 if ( be->be_update_ndn == NULL || repl_user )
327 int update = be->be_update_ndn != NULL;
329 char textbuf[SLAP_TEXT_BUFLEN];
330 size_t textlen = sizeof textbuf;
332 rc = slap_modlist2mods( modlist, update, &mods, &text,
335 if( rc != LDAP_SUCCESS ) {
336 send_ldap_result( conn, op, rc,
337 NULL, text, NULL, NULL );
341 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
342 global_lastmod == ON)) && !repl_user )
344 Modifications **modstail;
345 for( modstail = &mods;
347 modstail = &(*modstail)->sml_next )
351 rc = slap_mods_opattrs( op, modstail, &text );
353 if( rc != LDAP_SUCCESS ) {
354 send_ldap_result( conn, op, rc,
361 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
362 #ifdef SLAPD_MULTIMASTER
366 /* but we log only the ones not from a replicator user */
367 replog( be, op, dn, ndn, mods );
370 #ifndef SLAPD_MULTIMASTER
371 /* send a referral */
373 struct berval **defref = be->be_update_refs
374 ? be->be_update_refs : default_referral;
375 struct berval **ref = referral_rewrite( defref,
376 NULL, dn, LDAP_SCOPE_DEFAULT );
378 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
379 ref ? ref : defref, NULL );
385 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
386 NULL, "operation not supported within namingContext", NULL, NULL );
391 if( ndn != NULL ) free( ndn );
392 if ( modlist != NULL )
393 slap_modlist_free( modlist );
395 slap_mods_free( mods );
400 * convert a raw list of modifications to internal format
401 * Do basic attribute type checking and syntax validation.
403 int slap_modlist2mods(
406 Modifications **mods,
412 Modifications **modtail = mods;
414 for( ; ml != NULL; ml = ml->ml_next ) {
416 AttributeDescription *ad = NULL;
418 mod = (Modifications *)
419 ch_calloc( 1, sizeof(Modifications) );
422 mod->sml_op = ml->ml_op;
424 /* convert to attribute description */
425 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
427 if( rc != LDAP_SUCCESS ) {
428 slap_mods_free( mod );
429 snprintf( textbuf, textlen, "%s: %s",
430 ml->ml_type, *text );
437 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
438 && !slap_ad_is_binary( ad ))
440 /* attribute requires binary transfer */
441 slap_mods_free( mod );
443 snprintf( textbuf, textlen,
444 "%s: requires ;binary transfer",
447 return LDAP_UNDEFINED_TYPE;
450 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
451 && slap_ad_is_binary( ad ))
453 /* attribute requires binary transfer */
454 slap_mods_free( mod );
455 snprintf( textbuf, textlen,
456 "%s: disallows ;binary transfer",
459 return LDAP_UNDEFINED_TYPE;
462 if (!update && is_at_no_user_mod( ad->ad_type )) {
463 /* user modification disallowed */
464 slap_mods_free( mod );
465 snprintf( textbuf, textlen,
466 "%s: no user modification allowed",
469 return LDAP_CONSTRAINT_VIOLATION;
472 if ( is_at_obsolete( ad->ad_type ) &&
473 ( mod->sml_op == LDAP_MOD_ADD || ml->ml_bvalues != NULL ) )
476 * attribute is obsolete,
477 * only allow replace/delete with no values
479 slap_mods_free( mod );
480 snprintf( textbuf, textlen,
481 "%s: attribute is obsolete",
484 return LDAP_CONSTRAINT_VIOLATION;
490 if( ml->ml_bvalues != NULL ) {
492 slap_syntax_validate_func *validate =
493 ad->ad_type->sat_syntax->ssyn_validate;
494 slap_syntax_transform_func *pretty =
495 ad->ad_type->sat_syntax->ssyn_pretty;
497 if( !pretty && !validate ) {
498 slap_mods_free( mod );
499 *text = "no validator for syntax";
500 snprintf( textbuf, textlen,
501 "%s: no validator for syntax %s",
503 ad->ad_type->sat_syntax->ssyn_oid );
505 return LDAP_INVALID_SYNTAX;
509 * check that each value is valid per syntax
510 * and pretty if appropriate
512 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
515 rc = pretty( ad->ad_type->sat_syntax,
516 ml->ml_bvalues[nvals], &pval );
518 rc = validate( ad->ad_type->sat_syntax,
519 ml->ml_bvalues[nvals] );
523 slap_mods_free( mod );
524 snprintf( textbuf, textlen,
525 "%s: value #%ld invalid per syntax",
526 ml->ml_type, (long) nvals );
528 return LDAP_INVALID_SYNTAX;
532 ber_memfree( ml->ml_bvalues[nvals]->bv_val );
533 *ml->ml_bvalues[nvals] = *pval;
539 * a rough single value check... an additional check is needed
540 * to catch add of single value to existing single valued attribute
542 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
543 && nvals > 1 && is_at_single_value( ad->ad_type ))
545 slap_mods_free( mod );
546 snprintf( textbuf, textlen,
547 "%s: multiple value provided",
550 return LDAP_CONSTRAINT_VIOLATION;
554 mod->sml_bvalues = ml->ml_bvalues;
555 ml->ml_values = NULL;
558 modtail = &mod->sml_next;
564 int slap_mods_opattrs(
566 Modifications **modtail,
569 struct berval name, timestamp, csn;
570 time_t now = slap_get_time();
576 int mop = op->o_tag == LDAP_REQ_ADD
577 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
579 assert( modtail != NULL );
580 assert( *modtail == NULL );
582 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
583 ltm = gmtime( &now );
584 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
586 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
587 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
590 timestamp.bv_val = timebuf;
591 timestamp.bv_len = strlen(timebuf);
593 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
594 name.bv_val = SLAPD_ANONYMOUS;
595 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
597 name.bv_val = op->o_dn;
598 name.bv_len = strlen( op->o_dn );
601 if( op->o_tag == LDAP_REQ_ADD ) {
605 uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
606 uuid.bv_val = uuidbuf;
608 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
610 mod->sml_desc = slap_schema.si_ad_entryUUID;
611 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
612 mod->sml_bvalues[0] = ber_bvdup( &uuid );
613 mod->sml_bvalues[1] = NULL;
614 assert( mod->sml_bvalues[0] );
616 modtail = &mod->sml_next;
618 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
620 mod->sml_desc = slap_schema.si_ad_creatorsName;
621 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
622 mod->sml_bvalues[0] = ber_bvdup( &name );
623 mod->sml_bvalues[1] = NULL;
624 assert( mod->sml_bvalues[0] );
626 modtail = &mod->sml_next;
628 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
630 mod->sml_desc = slap_schema.si_ad_createTimestamp;
631 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
632 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
633 mod->sml_bvalues[1] = NULL;
634 assert( mod->sml_bvalues[0] );
636 modtail = &mod->sml_next;
639 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
641 mod->sml_desc = slap_schema.si_ad_entryCSN;
642 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
643 mod->sml_bvalues[0] = ber_bvdup( &csn );
644 mod->sml_bvalues[1] = NULL;
645 assert( mod->sml_bvalues[0] );
647 modtail = &mod->sml_next;
649 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
651 mod->sml_desc = slap_schema.si_ad_modifiersName;
652 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
653 mod->sml_bvalues[0] = ber_bvdup( &name );
654 mod->sml_bvalues[1] = NULL;
655 assert( mod->sml_bvalues[0] );
657 modtail = &mod->sml_next;
659 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
661 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
662 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
663 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
664 mod->sml_bvalues[1] = NULL;
665 assert( mod->sml_bvalues[0] );
667 modtail = &mod->sml_next;