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 = { 0, NULL };
38 struct berval ndn = { 0, 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 = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
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 if( ndn.bv_len == 0 ) {
197 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
198 "do_modify: attempt to modify root DSE.\n" ));
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 ( strcasecmp( ndn.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
210 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
211 "do_modify: attempt to modify subschema subentry.\n" ));
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", LDAP_LEVEL_DETAIL1,
226 "do_modify: modifications:\n" ));
228 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
231 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
233 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
234 "\t%s: %s\n", tmp->ml_op == LDAP_MOD_ADD ?
235 "add" : (tmp->ml_op == LDAP_MOD_DELETE ?
236 "delete" : "replace"), tmp->ml_type ));
238 if ( tmp->ml_bvalues == NULL ) {
239 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
241 } else if ( tmp->ml_bvalues[0] == NULL ) {
242 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
243 "\t\tzero values" ));
244 } else if ( tmp->ml_bvalues[1] == NULL ) {
245 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
248 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
249 "\t\tmultiple values" ));
253 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
254 tmp->ml_op == LDAP_MOD_ADD
255 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
256 ? "delete" : "replace"), tmp->ml_type, 0 );
258 if ( tmp->ml_bvalues == NULL ) {
259 Debug( LDAP_DEBUG_ARGS, "%s\n",
260 "\t\tno values", NULL, NULL );
261 } else if ( tmp->ml_bvalues[0] == NULL ) {
262 Debug( LDAP_DEBUG_ARGS, "%s\n",
263 "\t\tzero values", NULL, NULL );
264 } else if ( tmp->ml_bvalues[1] == NULL ) {
265 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
266 "\t\tone value", (long) tmp->ml_bvalues[0]->bv_len, NULL );
268 Debug( LDAP_DEBUG_ARGS, "%s\n",
269 "\t\tmultiple values", NULL, NULL );
275 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
276 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
278 manageDSAit = get_manageDSAit( op );
281 * We could be serving multiple database backends. Select the
282 * appropriate one, or send a referral to our "referral server"
283 * if we don't hold it.
285 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
286 struct berval **ref = referral_rewrite( default_referral,
287 NULL, &pdn, LDAP_SCOPE_DEFAULT );
289 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
290 NULL, NULL, ref ? ref : default_referral, NULL );
296 /* check restrictions */
297 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
298 if( rc != LDAP_SUCCESS ) {
299 send_ldap_result( conn, op, rc,
300 NULL, text, NULL, NULL );
304 /* check for referrals */
305 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
306 if ( rc != LDAP_SUCCESS ) {
310 /* deref suffix alias if appropriate */
311 suffix_alias( be, &ndn );
314 * do the modify if 1 && (2 || 3)
315 * 1) there is a modify function implemented in this backend;
316 * 2) this backend is master for what it holds;
317 * 3) it's a replica and the dn supplied is the update_ndn.
319 if ( be->be_modify ) {
320 /* do the update here */
321 int repl_user = be_isupdate( be, &op->o_ndn );
322 #ifndef SLAPD_MULTIMASTER
323 /* Multimaster slapd does not have to check for replicator dn
324 * because it accepts each modify request
326 if ( !be->be_update_ndn.bv_len || repl_user )
329 int update = be->be_update_ndn.bv_len;
331 char textbuf[SLAP_TEXT_BUFLEN];
332 size_t textlen = sizeof textbuf;
334 rc = slap_modlist2mods( modlist, update, &mods, &text,
337 if( rc != LDAP_SUCCESS ) {
338 send_ldap_result( conn, op, rc,
339 NULL, text, NULL, NULL );
343 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
344 global_lastmod == ON)) && !repl_user )
346 Modifications **modstail;
347 for( modstail = &mods;
349 modstail = &(*modstail)->sml_next )
354 rc = slap_mods_opattrs( op, mods, modstail, &text,
356 if( rc != LDAP_SUCCESS ) {
357 send_ldap_result( conn, op, rc,
364 if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, mods ) == 0
365 #ifdef SLAPD_MULTIMASTER
369 /* but we log only the ones not from a replicator user */
370 replog( be, op, &pdn, &ndn, mods );
373 #ifndef SLAPD_MULTIMASTER
374 /* send a referral */
376 struct berval **defref = be->be_update_refs
377 ? be->be_update_refs : default_referral;
378 struct berval **ref = referral_rewrite( defref,
379 NULL, &pdn, LDAP_SCOPE_DEFAULT );
381 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
382 ref ? ref : defref, NULL );
388 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
389 NULL, "operation not supported within namingContext",
397 if ( modlist != NULL )
398 slap_modlist_free( modlist );
400 slap_mods_free( mods );
405 * convert a raw list of modifications to internal format
406 * Do basic attribute type checking and syntax validation.
408 int slap_modlist2mods(
411 Modifications **mods,
417 Modifications **modtail = mods;
419 for( ; ml != NULL; ml = ml->ml_next ) {
421 AttributeDescription *ad = NULL;
423 mod = (Modifications *)
424 ch_calloc( 1, sizeof(Modifications) );
427 mod->sml_op = ml->ml_op;
429 /* convert to attribute description */
430 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
432 if( rc != LDAP_SUCCESS ) {
433 slap_mods_free( mod );
434 snprintf( textbuf, textlen, "%s: %s",
435 ml->ml_type, *text );
442 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
443 && !slap_ad_is_binary( ad ))
445 /* attribute requires binary transfer */
446 slap_mods_free( mod );
448 snprintf( textbuf, textlen,
449 "%s: requires ;binary transfer",
452 return LDAP_UNDEFINED_TYPE;
455 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
456 && slap_ad_is_binary( ad ))
458 /* attribute requires binary transfer */
459 slap_mods_free( mod );
460 snprintf( textbuf, textlen,
461 "%s: disallows ;binary transfer",
464 return LDAP_UNDEFINED_TYPE;
467 if (!update && is_at_no_user_mod( ad->ad_type )) {
468 /* user modification disallowed */
469 slap_mods_free( mod );
470 snprintf( textbuf, textlen,
471 "%s: no user modification allowed",
474 return LDAP_CONSTRAINT_VIOLATION;
477 if ( is_at_obsolete( ad->ad_type ) &&
478 ( mod->sml_op == LDAP_MOD_ADD || ml->ml_bvalues != NULL ) )
481 * attribute is obsolete,
482 * only allow replace/delete with no values
484 slap_mods_free( mod );
485 snprintf( textbuf, textlen,
486 "%s: attribute is obsolete",
489 return LDAP_CONSTRAINT_VIOLATION;
495 if( ml->ml_bvalues != NULL ) {
497 slap_syntax_validate_func *validate =
498 ad->ad_type->sat_syntax->ssyn_validate;
499 slap_syntax_transform_func *pretty =
500 ad->ad_type->sat_syntax->ssyn_pretty;
502 if( !pretty && !validate ) {
503 slap_mods_free( mod );
504 *text = "no validator for syntax";
505 snprintf( textbuf, textlen,
506 "%s: no validator for syntax %s",
508 ad->ad_type->sat_syntax->ssyn_oid );
510 return LDAP_INVALID_SYNTAX;
514 * check that each value is valid per syntax
515 * and pretty if appropriate
517 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
518 struct berval *pval = NULL;
520 rc = pretty( ad->ad_type->sat_syntax,
521 ml->ml_bvalues[nvals], &pval );
523 rc = validate( ad->ad_type->sat_syntax,
524 ml->ml_bvalues[nvals] );
528 slap_mods_free( mod );
529 snprintf( textbuf, textlen,
530 "%s: value #%ld invalid per syntax",
531 ml->ml_type, (long) nvals );
533 return LDAP_INVALID_SYNTAX;
537 ber_memfree( ml->ml_bvalues[nvals]->bv_val );
538 *ml->ml_bvalues[nvals] = *pval;
544 * a rough single value check... an additional check is needed
545 * to catch add of single value to existing single valued attribute
547 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
548 && nvals > 1 && is_at_single_value( ad->ad_type ))
550 slap_mods_free( mod );
551 snprintf( textbuf, textlen,
552 "%s: multiple value provided",
555 return LDAP_CONSTRAINT_VIOLATION;
559 mod->sml_bvalues = ml->ml_bvalues;
560 ml->ml_values = NULL;
563 modtail = &mod->sml_next;
569 int slap_mods_opattrs(
572 Modifications **modtail,
574 char *textbuf, size_t textlen )
576 struct berval name, timestamp, csn;
577 time_t now = slap_get_time();
583 int mop = op->o_tag == LDAP_REQ_ADD
584 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
586 assert( modtail != NULL );
587 assert( *modtail == NULL );
589 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
590 ltm = gmtime( &now );
591 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
593 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
594 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
597 timestamp.bv_val = timebuf;
598 timestamp.bv_len = strlen(timebuf);
600 if( op->o_dn.bv_len == 0 ) {
601 name.bv_val = SLAPD_ANONYMOUS;
602 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
607 if( op->o_tag == LDAP_REQ_ADD ) {
608 struct berval tmpval;
612 rc = mods_structural_class( mods, &tmpval, text, textbuf, textlen );
613 if( rc != LDAP_SUCCESS ) {
616 if ( tmpval.bv_len ) {
617 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
619 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
620 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
621 mod->sml_bvalues[0] = ber_bvdup( &tmpval );
622 mod->sml_bvalues[1] = NULL;
623 assert( mod->sml_bvalues[0] );
625 modtail = &mod->sml_next;
628 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
629 tmpval.bv_val = uuidbuf;
631 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
633 mod->sml_desc = slap_schema.si_ad_entryUUID;
634 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
635 mod->sml_bvalues[0] = ber_bvdup( &tmpval );
636 mod->sml_bvalues[1] = NULL;
637 assert( mod->sml_bvalues[0] );
639 modtail = &mod->sml_next;
641 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
643 mod->sml_desc = slap_schema.si_ad_creatorsName;
644 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
645 mod->sml_bvalues[0] = ber_bvdup( &name );
646 mod->sml_bvalues[1] = NULL;
647 assert( mod->sml_bvalues[0] );
649 modtail = &mod->sml_next;
651 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
653 mod->sml_desc = slap_schema.si_ad_createTimestamp;
654 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
655 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
656 mod->sml_bvalues[1] = NULL;
657 assert( mod->sml_bvalues[0] );
659 modtail = &mod->sml_next;
662 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
664 mod->sml_desc = slap_schema.si_ad_entryCSN;
665 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
666 mod->sml_bvalues[0] = ber_bvdup( &csn );
667 mod->sml_bvalues[1] = NULL;
668 assert( mod->sml_bvalues[0] );
670 modtail = &mod->sml_next;
672 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
674 mod->sml_desc = slap_schema.si_ad_modifiersName;
675 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
676 mod->sml_bvalues[0] = ber_bvdup( &name );
677 mod->sml_bvalues[1] = NULL;
678 assert( mod->sml_bvalues[0] );
680 modtail = &mod->sml_next;
682 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
684 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
685 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
686 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
687 mod->sml_bvalues[1] = NULL;
688 assert( mod->sml_bvalues[0] );
690 modtail = &mod->sml_next;