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>
30 #ifndef SLAPD_SCHEMA_NOT_COMPAT
31 static int add_modified_attrs( Operation *op, Modifications **modlist );
40 char *dn, *ndn = NULL;
44 LDAPModList *modlist = NULL;
45 LDAPModList **modtail = &modlist;
49 Modifications *mods = NULL;
54 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
57 * Parse the modify request. It looks like this:
59 * ModifyRequest := [APPLICATION 6] SEQUENCE {
60 * name DistinguishedName,
61 * mods SEQUENCE OF SEQUENCE {
62 * operation ENUMERATED {
67 * modification SEQUENCE {
69 * values SET OF AttributeValue
75 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
76 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
77 send_ldap_disconnect( conn, op,
78 LDAP_PROTOCOL_ERROR, "decoding error" );
79 return SLAPD_DISCONNECT;
82 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
84 /* collect modifications & save for later */
86 for ( tag = ber_first_element( op->o_ber, &len, &last );
88 tag = ber_next_element( op->o_ber, &len, last ) )
92 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
94 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
95 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
98 send_ldap_disconnect( conn, op,
99 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
100 rc = SLAPD_DISCONNECT;
106 if ( (*modtail)->ml_bvalues == NULL ) {
107 Debug( LDAP_DEBUG_ANY,
108 "do_modify: modify/add operation (%ld) requires values\n",
110 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
111 NULL, "modify/add operation requires values",
113 rc = LDAP_PROTOCOL_ERROR;
119 case LDAP_MOD_DELETE:
120 case LDAP_MOD_REPLACE:
124 Debug( LDAP_DEBUG_ANY,
125 "do_modify: invalid modify operation (%ld)\n",
127 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
128 NULL, "unrecognized modify operation", NULL, NULL );
129 rc = LDAP_PROTOCOL_ERROR;
134 (*modtail)->ml_op = mop;
136 #ifndef SLAPD_SCHEMA_NOT_COMPAT
137 attr_normalize( (*modtail)->ml_type );
140 modtail = &(*modtail)->ml_next;
144 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
145 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
149 ndn = ch_strdup( dn );
151 if( dn_normalize( ndn ) == NULL ) {
152 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
153 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
154 "invalid DN", NULL, NULL );
159 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
160 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
161 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
162 tmp->ml_op == LDAP_MOD_ADD
163 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
164 ? "delete" : "replace"), tmp->ml_type, 0 );
169 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
170 op->o_connid, op->o_opid, dn, 0, 0 );
173 * We could be serving multiple database backends. Select the
174 * appropriate one, or send a referral to our "referral server"
175 * if we don't hold it.
177 if ( (be = select_backend( ndn )) == NULL ) {
178 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
179 NULL, NULL, default_referral, NULL );
183 /* make sure this backend recongizes critical controls */
184 rc = backend_check_controls( be, conn, op, &text ) ;
186 if( rc != LDAP_SUCCESS ) {
187 send_ldap_result( conn, op, rc,
188 NULL, text, NULL, NULL );
192 if ( global_readonly || be->be_readonly ) {
193 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
195 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
196 NULL, "directory is read-only", NULL, NULL );
200 /* deref suffix alias if appropriate */
201 ndn = suffix_alias( be, ndn );
204 * do the modify if 1 && (2 || 3)
205 * 1) there is a modify function implemented in this backend;
206 * 2) this backend is master for what it holds;
207 * 3) it's a replica and the dn supplied is the update_ndn.
209 if ( be->be_modify ) {
210 /* do the update here */
211 #ifndef SLAPD_MULTIMASTER
212 /* we don't have to check for replicator dn
213 * because we accept each modify request
215 if ( be->be_update_ndn == NULL ||
216 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
219 int update = be->be_update_ndn != NULL;
220 #ifdef SLAPD_SCHEMA_NOT_COMPAT
222 rc = slap_modlist2mods( modlist, update, &mods, &text );
224 if( rc != LDAP_SUCCESS ) {
225 send_ldap_result( conn, op, rc,
226 NULL, text, NULL, NULL );
234 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
235 global_lastmod == ON)) && !update )
237 #ifdef SLAPD_SCHEMA_NOT_COMPAT
238 Modifications **modstail;
239 for( modstail = &mods;
241 modstail = &(*modstail)->sml_next )
245 rc = slap_mods_opattrs( op, modstail, &text );
247 char *text = "no-user-modification attribute type";
248 rc = add_modified_attrs( op, &mods );
251 if( rc != LDAP_SUCCESS ) {
252 send_ldap_result( conn, op, rc,
259 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
260 #ifdef SLAPD_MULTIMASTER
261 && ( be->be_update_ndn == NULL ||
262 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
265 /* but we log only the ones not from a replicator user */
266 replog( be, op, dn, mods );
269 #ifndef SLAPD_MULTIMASTER
270 /* send a referral */
272 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
273 be->be_update_refs ? be->be_update_refs : default_referral,
278 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
279 NULL, "operation not supported within namingContext", NULL, NULL );
284 if( ndn != NULL ) free( ndn );
285 if ( modlist != NULL )
286 slap_modlist_free( modlist );
288 slap_mods_free( mods );
292 #ifdef SLAPD_SCHEMA_NOT_COMPAT
294 * convert a raw list of modifications to internal format
295 * Do basic attribute type checking and syntax validation.
297 int slap_modlist2mods(
300 Modifications **mods,
304 Modifications **modtail = mods;
306 for( ; ml != NULL; ml = ml->ml_next ) {
308 AttributeDescription *ad = NULL;
310 mod = (Modifications *)
311 ch_calloc( 1, sizeof(Modifications) );
314 mod->sml_op = ml->ml_op;
316 /* convert to attribute description */
317 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
319 if( rc != LDAP_SUCCESS ) {
320 slap_mods_free( mod );
326 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
327 && !slap_ad_is_binary( ad ))
329 /* attribute requires binary transfer */
330 slap_mods_free( mod );
331 *text = "attribute requires ;binary transfer";
332 return LDAP_UNDEFINED_TYPE;
335 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
336 && slap_ad_is_binary( ad ))
338 /* attribute requires binary transfer */
339 slap_mods_free( mod );
340 *text = "attribute disallows ;binary transfer";
341 return LDAP_UNDEFINED_TYPE;
344 if (!update && is_at_no_user_mod( ad->ad_type )) {
345 /* user modification disallowed */
346 slap_mods_free( mod );
347 *text = "no user modification allowed";
348 return LDAP_CONSTRAINT_VIOLATION;
354 if( ml->ml_bvalues != NULL ) {
356 slap_syntax_validate_func *validate =
357 ad->ad_type->sat_syntax->ssyn_validate;
360 Debug( LDAP_DEBUG_TRACE,
361 "modlist2mods: no validator for syntax %s\n",
362 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
363 slap_mods_free( mod );
364 *text = "no validator for syntax";
365 return LDAP_INVALID_SYNTAX;
369 * check that each value is valid per syntax
371 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
372 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
375 slap_mods_free( mod );
376 *text = "value contains invalid data";
377 return LDAP_INVALID_SYNTAX;
382 * a rough single value check... an additional check is needed
383 * to catch add of single value to existing single valued attribute
385 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
386 && nvals > 1 && is_at_single_value( ad->ad_type ))
388 slap_mods_free( mod );
389 *text = "multiple values provided";
390 return LDAP_INVALID_SYNTAX;
394 mod->sml_bvalues = ml->ml_bvalues;
395 ml->ml_values = NULL;
398 modtail = &mod->sml_next;
404 int slap_mods_opattrs(
406 Modifications **modtail,
409 struct berval name, timestamp;
410 time_t now = slap_get_time();
415 int mop = op->o_tag == LDAP_REQ_ADD
416 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
418 assert( modtail != NULL );
419 assert( *modtail == NULL );
421 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
422 ltm = gmtime( &now );
423 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
424 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
425 timestamp.bv_val = timebuf;
426 timestamp.bv_len = strlen(timebuf);
428 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
429 name.bv_val = SLAPD_ANONYMOUS;
430 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
432 name.bv_val = op->o_dn;
433 name.bv_len = strlen( op->o_dn );
436 if( op->o_tag == LDAP_REQ_ADD ) {
437 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
439 mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
440 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
441 mod->sml_bvalues[0] = ber_bvdup( &name );
442 mod->sml_bvalues[1] = NULL;
445 modtail = &mod->sml_next;
447 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
449 mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
450 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
451 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
452 mod->sml_bvalues[1] = NULL;
454 modtail = &mod->sml_next;
457 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
459 mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
460 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
461 mod->sml_bvalues[0] = ber_bvdup( &name );
462 mod->sml_bvalues[1] = NULL;
464 modtail = &mod->sml_next;
466 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
468 mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
469 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
470 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
471 mod->sml_bvalues[1] = NULL;
473 modtail = &mod->sml_next;
480 add_modified_attrs( Operation *op, Modifications **modlist )
484 struct berval *bvals[2];
492 /* remove any attempts by the user to modify these attrs */
493 for ( m = *modlist; m != NULL; m = m->ml_next ) {
494 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
495 return LDAP_CONSTRAINT_VIOLATION;
499 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
500 bv.bv_val = SLAPD_ANONYMOUS;
501 bv.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
503 bv.bv_val = op->o_dn;
504 bv.bv_len = strlen( bv.bv_val );
506 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
507 m->ml_type = ch_strdup( "modifiersname" );
508 m->ml_op = LDAP_MOD_REPLACE;
509 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
510 m->ml_bvalues[0] = ber_bvdup( &bv );
511 m->ml_next = *modlist;
514 currenttime = slap_get_time();
515 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
516 ltm = gmtime( ¤ttime );
517 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
518 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
521 bv.bv_len = strlen( bv.bv_val );
522 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
523 m->ml_type = ch_strdup( "modifytimestamp" );
524 m->ml_op = LDAP_MOD_REPLACE;
525 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
526 m->ml_bvalues[0] = ber_bvdup( &bv );
527 m->ml_next = *modlist;
540 #ifdef SLAPD_SCHEMA_NOT_COMPAT
541 ad_free( mod->sm_desc, 1 );
544 free( mod->sm_desc );
548 if ( mod->sm_bvalues != NULL )
549 ber_bvecfree( mod->sm_bvalues );
562 for ( ; ml != NULL; ml = next ) {
565 slap_mod_free( &ml->sml_mod, 0 );
577 for ( ; ml != NULL; ml = next ) {
583 if ( ml->ml_bvalues != NULL )
584 ber_bvecfree( ml->ml_bvalues );