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>
35 char *dn, *ndn = NULL;
39 LDAPModList *modlist = NULL;
40 LDAPModList **modtail = &modlist;
44 Modifications *mods = NULL;
51 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
52 "do_modify: enter\n" ));
54 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
58 * Parse the modify request. It looks like this:
60 * ModifyRequest := [APPLICATION 6] SEQUENCE {
61 * name DistinguishedName,
62 * mods SEQUENCE OF SEQUENCE {
63 * operation ENUMERATED {
68 * modification SEQUENCE {
70 * values SET OF AttributeValue
76 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
78 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
79 "do_modify: ber_scanf failed\n" ));
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", LDAP_LEVEL_ARGS,
91 "do_modify: dn (%s)\n", dn ));
93 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
97 /* collect modifications & save for later */
99 for ( tag = ber_first_element( op->o_ber, &len, &last );
101 tag = ber_next_element( op->o_ber, &len, last ) )
105 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
107 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
108 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
111 send_ldap_disconnect( conn, op,
112 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
113 rc = SLAPD_DISCONNECT;
119 if ( (*modtail)->ml_bvalues == NULL ) {
121 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
122 "do_modify: modify/add operation (%ld) requires values\n",
125 Debug( LDAP_DEBUG_ANY,
126 "do_modify: modify/add operation (%ld) requires values\n",
130 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
131 NULL, "modify/add operation requires values",
133 rc = LDAP_PROTOCOL_ERROR;
139 case LDAP_MOD_DELETE:
140 case LDAP_MOD_REPLACE:
145 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
146 "do_modify: invalid modify operation (%ld)\n",
149 Debug( LDAP_DEBUG_ANY,
150 "do_modify: invalid modify operation (%ld)\n",
154 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
155 NULL, "unrecognized modify operation", NULL, NULL );
156 rc = LDAP_PROTOCOL_ERROR;
161 (*modtail)->ml_op = mop;
162 modtail = &(*modtail)->ml_next;
166 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
168 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
169 "do_modify: get_ctrls failed\n" ));
171 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
177 ndn = ch_strdup( dn );
179 if( dn_normalize( ndn ) == NULL ) {
181 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
182 "do_modify: invalid dn (%s)\n", dn ));
184 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
187 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
188 "invalid DN", NULL, NULL );
194 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
195 "do_modify: attempt to modify root DSE.\n" ));
197 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
200 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
201 NULL, "modify upon the root DSE not supported", NULL, NULL );
204 #if defined( SLAPD_SCHEMA_DN )
205 } else if ( strcasecmp( ndn, SLAPD_SCHEMA_DN ) == 0 ) {
207 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
208 "do_modify: attempt to modify subschema subentry.\n" ));
210 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
213 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
214 NULL, "modification of subschema subentry not supported",
222 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
223 "do_modify: modifications:\n" ));
225 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
228 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
230 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
231 "\t%s: %s\n", tmp->ml_op == LDAP_MOD_ADD ?
232 "add" : (tmp->ml_op == LDAP_MOD_DELETE ?
233 "delete" : "replace"), tmp->ml_type ));
235 if ( tmp->ml_bvalues == NULL ) {
236 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
238 } else if ( tmp->ml_bvalues[0] == NULL ) {
239 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
240 "\t\tzero values" ));
241 } else if ( tmp->ml_bvalues[1] == NULL ) {
242 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
245 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
246 "\t\tmultiple values" ));
250 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
251 tmp->ml_op == LDAP_MOD_ADD
252 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
253 ? "delete" : "replace"), tmp->ml_type, 0 );
255 if ( tmp->ml_bvalues == NULL ) {
256 Debug( LDAP_DEBUG_ARGS, "%s\n",
257 "\t\tno values", NULL, NULL );
258 } else if ( tmp->ml_bvalues[0] == NULL ) {
259 Debug( LDAP_DEBUG_ARGS, "%s\n",
260 "\t\tzero values", NULL, NULL );
261 } else if ( tmp->ml_bvalues[1] == NULL ) {
262 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
263 "\t\tone value", (long) tmp->ml_bvalues[0]->bv_len, NULL );
265 Debug( LDAP_DEBUG_ARGS, "%s\n",
266 "\t\tmultiple values", NULL, NULL );
272 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
273 op->o_connid, op->o_opid, dn, 0, 0 );
275 manageDSAit = get_manageDSAit( op );
278 * We could be serving multiple database backends. Select the
279 * appropriate one, or send a referral to our "referral server"
280 * if we don't hold it.
282 if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
283 struct berval **ref = referral_rewrite( default_referral,
284 NULL, dn, LDAP_SCOPE_DEFAULT );
286 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
287 NULL, NULL, ref ? ref : default_referral, NULL );
293 /* check restrictions */
294 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
295 if( rc != LDAP_SUCCESS ) {
296 send_ldap_result( conn, op, rc,
297 NULL, text, NULL, NULL );
301 /* check for referrals */
302 rc = backend_check_referrals( be, conn, op, dn, ndn );
303 if ( rc != LDAP_SUCCESS ) {
307 /* deref suffix alias if appropriate */
308 ndn = suffix_alias( be, ndn );
311 * do the modify if 1 && (2 || 3)
312 * 1) there is a modify function implemented in this backend;
313 * 2) this backend is master for what it holds;
314 * 3) it's a replica and the dn supplied is the update_ndn.
316 if ( be->be_modify ) {
317 /* do the update here */
318 int repl_user = be_isupdate( be, op->o_ndn );
319 #ifndef SLAPD_MULTIMASTER
320 /* Multimaster slapd does not have to check for replicator dn
321 * because it accepts each modify request
323 if ( be->be_update_ndn == NULL || repl_user )
326 int update = be->be_update_ndn != NULL;
328 char textbuf[SLAP_TEXT_BUFLEN];
329 size_t textlen = sizeof textbuf;
331 rc = slap_modlist2mods( modlist, update, &mods, &text,
334 if( rc != LDAP_SUCCESS ) {
335 send_ldap_result( conn, op, rc,
336 NULL, text, NULL, NULL );
340 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
341 global_lastmod == ON)) && !repl_user )
343 Modifications **modstail;
344 for( modstail = &mods;
346 modstail = &(*modstail)->sml_next )
350 rc = slap_mods_opattrs( op, modstail, &text );
352 if( rc != LDAP_SUCCESS ) {
353 send_ldap_result( conn, op, rc,
360 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
361 #ifdef SLAPD_MULTIMASTER
365 /* but we log only the ones not from a replicator user */
366 replog( be, op, dn, mods );
369 #ifndef SLAPD_MULTIMASTER
370 /* send a referral */
372 struct berval **defref = be->be_update_refs
373 ? be->be_update_refs : default_referral;
374 struct berval **ref = referral_rewrite( defref,
375 NULL, dn, LDAP_SCOPE_DEFAULT );
377 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
378 ref ? ref : defref, NULL );
384 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
385 NULL, "operation not supported within namingContext", NULL, NULL );
390 if( ndn != NULL ) free( ndn );
391 if ( modlist != NULL )
392 slap_modlist_free( modlist );
394 slap_mods_free( mods );
399 * convert a raw list of modifications to internal format
400 * Do basic attribute type checking and syntax validation.
402 int slap_modlist2mods(
405 Modifications **mods,
411 Modifications **modtail = mods;
413 for( ; ml != NULL; ml = ml->ml_next ) {
415 AttributeDescription *ad = NULL;
417 mod = (Modifications *)
418 ch_calloc( 1, sizeof(Modifications) );
421 mod->sml_op = ml->ml_op;
423 /* convert to attribute description */
424 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
426 if( rc != LDAP_SUCCESS ) {
427 slap_mods_free( mod );
428 snprintf( textbuf, textlen, "%s: %s",
429 ml->ml_type, *text );
436 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
437 && !slap_ad_is_binary( ad ))
439 /* attribute requires binary transfer */
440 slap_mods_free( mod );
442 snprintf( textbuf, textlen,
443 "%s: requires ;binary transfer",
446 return LDAP_UNDEFINED_TYPE;
449 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
450 && slap_ad_is_binary( ad ))
452 /* attribute requires binary transfer */
453 slap_mods_free( mod );
454 snprintf( textbuf, textlen,
455 "%s: disallows ;binary transfer",
458 return LDAP_UNDEFINED_TYPE;
461 if (!update && is_at_no_user_mod( ad->ad_type )) {
462 /* user modification disallowed */
463 slap_mods_free( mod );
464 snprintf( textbuf, textlen,
465 "%s: no user modification allowed",
468 return LDAP_CONSTRAINT_VIOLATION;
471 if ( is_at_obsolete( ad->ad_type ) &&
472 ( mod->sml_op == LDAP_MOD_ADD || ml->ml_bvalues != NULL ) )
475 * attribute is obsolete,
476 * only allow replace/delete with no values
478 slap_mods_free( mod );
479 snprintf( textbuf, textlen,
480 "%s: attribute is obsolete",
483 return LDAP_CONSTRAINT_VIOLATION;
489 if( ml->ml_bvalues != NULL ) {
491 slap_syntax_validate_func *validate =
492 ad->ad_type->sat_syntax->ssyn_validate;
493 slap_syntax_transform_func *pretty =
494 ad->ad_type->sat_syntax->ssyn_pretty;
496 if( !pretty && !validate ) {
497 slap_mods_free( mod );
498 *text = "no validator for syntax";
499 snprintf( textbuf, textlen,
500 "%s: no validator for syntax %s",
502 ad->ad_type->sat_syntax->ssyn_oid );
504 return LDAP_INVALID_SYNTAX;
508 * check that each value is valid per syntax
509 * and pretty if appropriate
511 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
514 rc = pretty( ad->ad_type->sat_syntax,
515 ml->ml_bvalues[nvals], &pval );
517 rc = validate( ad->ad_type->sat_syntax,
518 ml->ml_bvalues[nvals] );
522 slap_mods_free( mod );
523 snprintf( textbuf, textlen,
524 "%s: value #%ld invalid per syntax",
525 ml->ml_type, (long) nvals );
527 return LDAP_INVALID_SYNTAX;
531 ber_memfree( ml->ml_bvalues[nvals]->bv_val );
532 *ml->ml_bvalues[nvals] = *pval;
538 * a rough single value check... an additional check is needed
539 * to catch add of single value to existing single valued attribute
541 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
542 && nvals > 1 && is_at_single_value( ad->ad_type ))
544 slap_mods_free( mod );
545 snprintf( textbuf, textlen,
546 "%s: multiple value provided",
549 return LDAP_CONSTRAINT_VIOLATION;
553 mod->sml_bvalues = ml->ml_bvalues;
554 ml->ml_values = NULL;
557 modtail = &mod->sml_next;
563 int slap_mods_opattrs(
565 Modifications **modtail,
568 struct berval name, timestamp, csn;
569 time_t now = slap_get_time();
575 int mop = op->o_tag == LDAP_REQ_ADD
576 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
578 assert( modtail != NULL );
579 assert( *modtail == NULL );
581 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
582 ltm = gmtime( &now );
583 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
585 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
586 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
589 timestamp.bv_val = timebuf;
590 timestamp.bv_len = strlen(timebuf);
592 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
593 name.bv_val = SLAPD_ANONYMOUS;
594 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
596 name.bv_val = op->o_dn;
597 name.bv_len = strlen( op->o_dn );
600 if( op->o_tag == LDAP_REQ_ADD ) {
604 uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
605 uuid.bv_val = uuidbuf;
607 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
609 mod->sml_desc = slap_schema.si_ad_entryUUID;
610 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
611 mod->sml_bvalues[0] = ber_bvdup( &uuid );
612 mod->sml_bvalues[1] = NULL;
613 assert( mod->sml_bvalues[0] );
615 modtail = &mod->sml_next;
617 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
619 mod->sml_desc = slap_schema.si_ad_creatorsName;
620 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
621 mod->sml_bvalues[0] = ber_bvdup( &name );
622 mod->sml_bvalues[1] = NULL;
623 assert( mod->sml_bvalues[0] );
625 modtail = &mod->sml_next;
627 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
629 mod->sml_desc = slap_schema.si_ad_createTimestamp;
630 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
631 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
632 mod->sml_bvalues[1] = NULL;
633 assert( mod->sml_bvalues[0] );
635 modtail = &mod->sml_next;
638 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
640 mod->sml_desc = slap_schema.si_ad_entryCSN;
641 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
642 mod->sml_bvalues[0] = ber_bvdup( &csn );
643 mod->sml_bvalues[1] = NULL;
644 assert( mod->sml_bvalues[0] );
646 modtail = &mod->sml_next;
648 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
650 mod->sml_desc = slap_schema.si_ad_modifiersName;
651 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
652 mod->sml_bvalues[0] = ber_bvdup( &name );
653 mod->sml_bvalues[1] = NULL;
654 assert( mod->sml_bvalues[0] );
656 modtail = &mod->sml_next;
658 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
660 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
661 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof(struct berval *) );
662 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
663 mod->sml_bvalues[1] = NULL;
664 assert( mod->sml_bvalues[0] );
666 modtail = &mod->sml_next;