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 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
236 tmp->ml_op == LDAP_MOD_ADD
237 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
238 ? "delete" : "replace"), tmp->ml_type, 0 );
244 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
245 op->o_connid, op->o_opid, dn, 0, 0 );
247 manageDSAit = get_manageDSAit( op );
250 * We could be serving multiple database backends. Select the
251 * appropriate one, or send a referral to our "referral server"
252 * if we don't hold it.
254 if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
255 struct berval **ref = referral_rewrite( default_referral,
256 NULL, dn, LDAP_SCOPE_DEFAULT );
258 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
259 NULL, NULL, ref ? ref : default_referral, NULL );
265 /* check restrictions */
266 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
267 if( rc != LDAP_SUCCESS ) {
268 send_ldap_result( conn, op, rc,
269 NULL, text, NULL, NULL );
273 /* check for referrals */
274 rc = backend_check_referrals( be, conn, op, dn, ndn );
275 if ( rc != LDAP_SUCCESS ) {
279 /* deref suffix alias if appropriate */
280 ndn = suffix_alias( be, ndn );
283 * do the modify if 1 && (2 || 3)
284 * 1) there is a modify function implemented in this backend;
285 * 2) this backend is master for what it holds;
286 * 3) it's a replica and the dn supplied is the update_ndn.
288 if ( be->be_modify ) {
289 /* do the update here */
290 int repl_user = be_isupdate( be, op->o_ndn );
291 #ifndef SLAPD_MULTIMASTER
292 /* Multimaster slapd does not have to check for replicator dn
293 * because it accepts each modify request
295 if ( be->be_update_ndn == NULL || repl_user )
298 int update = be->be_update_ndn != NULL;
300 char textbuf[SLAP_TEXT_BUFLEN];
301 size_t textlen = sizeof textbuf;
303 rc = slap_modlist2mods( modlist, update, &mods, &text,
306 if( rc != LDAP_SUCCESS ) {
307 send_ldap_result( conn, op, rc,
308 NULL, text, NULL, NULL );
312 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
313 global_lastmod == ON)) && !repl_user )
315 Modifications **modstail;
316 for( modstail = &mods;
318 modstail = &(*modstail)->sml_next )
322 rc = slap_mods_opattrs( op, modstail, &text );
324 if( rc != LDAP_SUCCESS ) {
325 send_ldap_result( conn, op, rc,
332 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
333 #ifdef SLAPD_MULTIMASTER
337 /* but we log only the ones not from a replicator user */
338 replog( be, op, dn, mods );
341 #ifndef SLAPD_MULTIMASTER
342 /* send a referral */
344 struct berval **defref = be->be_update_refs
345 ? be->be_update_refs : default_referral;
346 struct berval **ref = referral_rewrite( defref,
347 NULL, dn, LDAP_SCOPE_DEFAULT );
349 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
350 ref ? ref : defref, NULL );
356 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
357 NULL, "operation not supported within namingContext", NULL, NULL );
362 if( ndn != NULL ) free( ndn );
363 if ( modlist != NULL )
364 slap_modlist_free( modlist );
366 slap_mods_free( mods );
371 * convert a raw list of modifications to internal format
372 * Do basic attribute type checking and syntax validation.
374 int slap_modlist2mods(
377 Modifications **mods,
383 Modifications **modtail = mods;
385 for( ; ml != NULL; ml = ml->ml_next ) {
387 AttributeDescription *ad = NULL;
389 mod = (Modifications *)
390 ch_calloc( 1, sizeof(Modifications) );
393 mod->sml_op = ml->ml_op;
395 /* convert to attribute description */
396 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
398 if( rc != LDAP_SUCCESS ) {
399 slap_mods_free( mod );
400 snprintf( textbuf, textlen, "%s: %s",
401 ml->ml_type, *text );
408 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
409 && !slap_ad_is_binary( ad ))
411 /* attribute requires binary transfer */
412 slap_mods_free( mod );
414 snprintf( textbuf, textlen,
415 "%s: requires ;binary transfer",
418 return LDAP_UNDEFINED_TYPE;
421 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
422 && slap_ad_is_binary( ad ))
424 /* attribute requires binary transfer */
425 slap_mods_free( mod );
426 snprintf( textbuf, textlen,
427 "%s: disallows ;binary transfer",
430 return LDAP_UNDEFINED_TYPE;
433 if (!update && is_at_no_user_mod( ad->ad_type )) {
434 /* user modification disallowed */
435 slap_mods_free( mod );
436 snprintf( textbuf, textlen,
437 "%s: no user modification allowed",
440 return LDAP_CONSTRAINT_VIOLATION;
443 if ( is_at_obsolete( ad->ad_type ) &&
444 ( mod->sml_op == LDAP_MOD_ADD || ml->ml_bvalues != NULL ) )
447 * attribute is obsolete,
448 * only allow replace/delete with no values
450 slap_mods_free( mod );
451 snprintf( textbuf, textlen,
452 "%s: attribute is obsolete",
455 return LDAP_CONSTRAINT_VIOLATION;
461 if( ml->ml_bvalues != NULL ) {
463 slap_syntax_validate_func *validate =
464 ad->ad_type->sat_syntax->ssyn_validate;
467 slap_mods_free( mod );
468 *text = "no validator for syntax";
469 snprintf( textbuf, textlen,
470 "%s: no validator for syntax %s",
472 ad->ad_type->sat_syntax->ssyn_oid );
474 return LDAP_INVALID_SYNTAX;
478 * check that each value is valid per syntax
480 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
481 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
484 slap_mods_free( mod );
485 snprintf( textbuf, textlen,
486 "%s: value #%ld invalid per syntax",
487 ml->ml_type, (long) nvals );
489 return LDAP_INVALID_SYNTAX;
494 * a rough single value check... an additional check is needed
495 * to catch add of single value to existing single valued attribute
497 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
498 && nvals > 1 && is_at_single_value( ad->ad_type ))
500 slap_mods_free( mod );
501 snprintf( textbuf, textlen,
502 "%s: multiple value provided",
505 return LDAP_CONSTRAINT_VIOLATION;
509 mod->sml_bvalues = ml->ml_bvalues;
510 ml->ml_values = NULL;
513 modtail = &mod->sml_next;
519 int slap_mods_opattrs(
521 Modifications **modtail,
524 struct berval name, timestamp;
525 time_t now = slap_get_time();
530 int mop = op->o_tag == LDAP_REQ_ADD
531 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
533 assert( modtail != NULL );
534 assert( *modtail == NULL );
536 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
537 ltm = gmtime( &now );
538 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
539 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
540 timestamp.bv_val = timebuf;
541 timestamp.bv_len = strlen(timebuf);
543 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
544 name.bv_val = SLAPD_ANONYMOUS;
545 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
547 name.bv_val = op->o_dn;
548 name.bv_len = strlen( op->o_dn );
551 if( op->o_tag == LDAP_REQ_ADD ) {
552 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
554 mod->sml_desc = slap_schema.si_ad_creatorsName;
555 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
556 mod->sml_bvalues[0] = ber_bvdup( &name );
557 mod->sml_bvalues[1] = NULL;
560 modtail = &mod->sml_next;
562 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
564 mod->sml_desc = slap_schema.si_ad_createTimestamp;
565 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
566 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
567 mod->sml_bvalues[1] = NULL;
569 modtail = &mod->sml_next;
572 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
574 mod->sml_desc = slap_schema.si_ad_modifiersName;
575 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
576 mod->sml_bvalues[0] = ber_bvdup( &name );
577 mod->sml_bvalues[1] = NULL;
579 modtail = &mod->sml_next;
581 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
583 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
584 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
585 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
586 mod->sml_bvalues[1] = NULL;
588 modtail = &mod->sml_next;