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 ) {
369 slap_mods_free( mod );
375 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
376 && !slap_ad_is_binary( ad ))
378 /* attribute requires binary transfer */
379 slap_mods_free( mod );
380 *text = "attribute requires ;binary transfer";
381 return LDAP_UNDEFINED_TYPE;
384 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
385 && slap_ad_is_binary( ad ))
387 /* attribute requires binary transfer */
388 slap_mods_free( mod );
389 *text = "attribute disallows ;binary transfer";
390 return LDAP_UNDEFINED_TYPE;
393 if (!update && is_at_no_user_mod( ad->ad_type )) {
394 /* user modification disallowed */
395 slap_mods_free( mod );
396 *text = "no user modification allowed";
397 return LDAP_CONSTRAINT_VIOLATION;
403 if( ml->ml_bvalues != NULL ) {
405 slap_syntax_validate_func *validate =
406 ad->ad_type->sat_syntax->ssyn_validate;
410 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
411 "modlist2mods: no validator for syntax %s\n",
412 ad->ad_type->sat_syntax->ssyn_oid ));
414 Debug( LDAP_DEBUG_TRACE,
415 "modlist2mods: no validator for syntax %s\n",
416 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
419 slap_mods_free( mod );
420 *text = "no validator for syntax";
421 return LDAP_INVALID_SYNTAX;
425 * check that each value is valid per syntax
427 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
428 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
431 slap_mods_free( mod );
432 *text = "value contains invalid data";
433 return LDAP_INVALID_SYNTAX;
438 * a rough single value check... an additional check is needed
439 * to catch add of single value to existing single valued attribute
441 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
442 && nvals > 1 && is_at_single_value( ad->ad_type ))
444 slap_mods_free( mod );
445 *text = "multiple values provided";
446 return LDAP_INVALID_SYNTAX;
450 mod->sml_bvalues = ml->ml_bvalues;
451 ml->ml_values = NULL;
454 modtail = &mod->sml_next;
460 int slap_mods_opattrs(
462 Modifications **modtail,
465 struct berval name, timestamp;
466 time_t now = slap_get_time();
471 int mop = op->o_tag == LDAP_REQ_ADD
472 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
474 assert( modtail != NULL );
475 assert( *modtail == NULL );
477 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
478 ltm = gmtime( &now );
479 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
480 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
481 timestamp.bv_val = timebuf;
482 timestamp.bv_len = strlen(timebuf);
484 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
485 name.bv_val = SLAPD_ANONYMOUS;
486 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
488 name.bv_val = op->o_dn;
489 name.bv_len = strlen( op->o_dn );
492 if( op->o_tag == LDAP_REQ_ADD ) {
493 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
495 mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
496 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
497 mod->sml_bvalues[0] = ber_bvdup( &name );
498 mod->sml_bvalues[1] = NULL;
501 modtail = &mod->sml_next;
503 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
505 mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
506 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
507 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
508 mod->sml_bvalues[1] = NULL;
510 modtail = &mod->sml_next;
513 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
515 mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
516 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
517 mod->sml_bvalues[0] = ber_bvdup( &name );
518 mod->sml_bvalues[1] = NULL;
520 modtail = &mod->sml_next;
522 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
524 mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
525 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
526 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
527 mod->sml_bvalues[1] = NULL;
529 modtail = &mod->sml_next;
541 ad_free( mod->sm_desc, 1 );
543 if ( mod->sm_bvalues != NULL )
544 ber_bvecfree( mod->sm_bvalues );
557 for ( ; ml != NULL; ml = next ) {
560 slap_mod_free( &ml->sml_mod, 0 );
572 for ( ; ml != NULL; ml = next ) {
578 if ( ml->ml_bvalues != NULL )
579 ber_bvecfree( ml->ml_bvalues );