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 );
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 );
208 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
209 "do_modify: modifications:\n" ));
211 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
214 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
216 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
217 "\t%s: %s\n", tmp->ml_op == LDAP_MOD_ADD ?
218 "add" : (tmp->ml_op == LDAP_MOD_DELETE ?
219 "delete" : "replace"), tmp->ml_type ));
221 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
222 tmp->ml_op == LDAP_MOD_ADD
223 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
224 ? "delete" : "replace"), tmp->ml_type, 0 );
230 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
231 op->o_connid, op->o_opid, dn, 0, 0 );
233 manageDSAit = get_manageDSAit( op );
236 * We could be serving multiple database backends. Select the
237 * appropriate one, or send a referral to our "referral server"
238 * if we don't hold it.
240 if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
241 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
242 NULL, NULL, default_referral, NULL );
246 /* check restrictions */
247 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
248 if( rc != LDAP_SUCCESS ) {
249 send_ldap_result( conn, op, rc,
250 NULL, text, NULL, NULL );
254 /* check for referrals */
255 rc = backend_check_referrals( be, conn, op, dn, ndn );
256 if ( rc != LDAP_SUCCESS ) {
260 /* deref suffix alias if appropriate */
261 ndn = suffix_alias( be, ndn );
264 * do the modify if 1 && (2 || 3)
265 * 1) there is a modify function implemented in this backend;
266 * 2) this backend is master for what it holds;
267 * 3) it's a replica and the dn supplied is the update_ndn.
269 if ( be->be_modify ) {
270 /* do the update here */
271 int repl_user = (be->be_update_ndn != NULL &&
272 strcmp( be->be_update_ndn, op->o_ndn ) == 0);
273 #ifndef SLAPD_MULTIMASTER
274 /* Multimaster slapd does not have to check for replicator dn
275 * because it accepts each modify request
277 if ( be->be_update_ndn == NULL || repl_user )
280 int update = be->be_update_ndn != NULL;
282 rc = slap_modlist2mods( modlist, update, &mods, &text );
284 if( rc != LDAP_SUCCESS ) {
285 send_ldap_result( conn, op, rc,
286 NULL, text, NULL, NULL );
290 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
291 global_lastmod == ON)) && !repl_user )
293 Modifications **modstail;
294 for( modstail = &mods;
296 modstail = &(*modstail)->sml_next )
300 rc = slap_mods_opattrs( op, modstail, &text );
302 if( rc != LDAP_SUCCESS ) {
303 send_ldap_result( conn, op, rc,
310 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
311 #ifdef SLAPD_MULTIMASTER
315 /* but we log only the ones not from a replicator user */
316 replog( be, op, dn, mods );
319 #ifndef SLAPD_MULTIMASTER
320 /* send a referral */
322 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
323 be->be_update_refs ? be->be_update_refs : default_referral,
328 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
329 NULL, "operation not supported within namingContext", NULL, NULL );
334 if( ndn != NULL ) free( ndn );
335 if ( modlist != NULL )
336 slap_modlist_free( modlist );
338 slap_mods_free( mods );
343 * convert a raw list of modifications to internal format
344 * Do basic attribute type checking and syntax validation.
346 int slap_modlist2mods(
349 Modifications **mods,
353 Modifications **modtail = mods;
355 for( ; ml != NULL; ml = ml->ml_next ) {
357 AttributeDescription *ad = NULL;
359 mod = (Modifications *)
360 ch_calloc( 1, sizeof(Modifications) );
363 mod->sml_op = ml->ml_op;
365 /* convert to attribute description */
366 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
368 if( rc != LDAP_SUCCESS ) {
370 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
371 "modlist2mods: Attribute %s is undefined (%s)\n", ml->ml_type, text ));
373 slap_mods_free( mod );
379 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
380 && !slap_ad_is_binary( ad ))
382 /* attribute requires binary transfer */
383 slap_mods_free( mod );
384 *text = "attribute requires ;binary transfer";
386 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
387 "modlist2mods: Attribute %s %s\n", ml->ml_type, text ));
389 return LDAP_UNDEFINED_TYPE;
392 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
393 && slap_ad_is_binary( ad ))
395 /* attribute requires binary transfer */
396 slap_mods_free( mod );
397 *text = "attribute disallows ;binary transfer";
399 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
400 "modlist2mods: Attribute %s %s\n", ml->ml_type, text ));
402 return LDAP_UNDEFINED_TYPE;
405 if (!update && is_at_no_user_mod( ad->ad_type )) {
406 /* user modification disallowed */
407 slap_mods_free( mod );
408 *text = "no user modification allowed";
410 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
411 "modlist2mods: Attribute %s %s\n", ml->ml_type, text ));
413 return LDAP_CONSTRAINT_VIOLATION;
419 if( ml->ml_bvalues != NULL ) {
421 slap_syntax_validate_func *validate =
422 ad->ad_type->sat_syntax->ssyn_validate;
426 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
427 "modlist2mods: Attribute %s, no validator for syntax %s\n",
428 ml->ml_type, ad->ad_type->sat_syntax->ssyn_oid ));
430 Debug( LDAP_DEBUG_TRACE,
431 "modlist2mods: no validator for syntax %s\n",
432 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
435 slap_mods_free( mod );
436 *text = "no validator for syntax";
437 return LDAP_INVALID_SYNTAX;
441 * check that each value is valid per syntax
443 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
444 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
447 slap_mods_free( mod );
448 *text = "value contains invalid data";
450 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
451 "modlist2mods: value for attribute %s contains invalid data\n",
454 return LDAP_INVALID_SYNTAX;
459 * a rough single value check... an additional check is needed
460 * to catch add of single value to existing single valued attribute
462 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
463 && nvals > 1 && is_at_single_value( ad->ad_type ))
465 slap_mods_free( mod );
466 *text = "multiple values provided";
467 return LDAP_INVALID_SYNTAX;
471 mod->sml_bvalues = ml->ml_bvalues;
472 ml->ml_values = NULL;
475 modtail = &mod->sml_next;
481 int slap_mods_opattrs(
483 Modifications **modtail,
486 struct berval name, timestamp;
487 time_t now = slap_get_time();
492 int mop = op->o_tag == LDAP_REQ_ADD
493 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
495 assert( modtail != NULL );
496 assert( *modtail == NULL );
498 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
499 ltm = gmtime( &now );
500 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
501 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
502 timestamp.bv_val = timebuf;
503 timestamp.bv_len = strlen(timebuf);
505 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
506 name.bv_val = SLAPD_ANONYMOUS;
507 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
509 name.bv_val = op->o_dn;
510 name.bv_len = strlen( op->o_dn );
513 if( op->o_tag == LDAP_REQ_ADD ) {
514 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
516 mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
517 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
518 mod->sml_bvalues[0] = ber_bvdup( &name );
519 mod->sml_bvalues[1] = NULL;
522 modtail = &mod->sml_next;
524 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
526 mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
527 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
528 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
529 mod->sml_bvalues[1] = NULL;
531 modtail = &mod->sml_next;
534 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
536 mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
537 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
538 mod->sml_bvalues[0] = ber_bvdup( &name );
539 mod->sml_bvalues[1] = NULL;
541 modtail = &mod->sml_next;
543 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
545 mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
546 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
547 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
548 mod->sml_bvalues[1] = NULL;
550 modtail = &mod->sml_next;
562 ad_free( mod->sm_desc, 1 );
564 if ( mod->sm_bvalues != NULL )
565 ber_bvecfree( mod->sm_bvalues );
578 for ( ; ml != NULL; ml = next ) {
581 slap_mod_free( &ml->sml_mod, 0 );
593 for ( ; ml != NULL; ml = next ) {
599 if ( ml->ml_bvalues != NULL )
600 ber_bvecfree( ml->ml_bvalues );