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 rc = slap_mods_opattrs( op, &mods, &text );
240 char *text = "no-user-modification attribute type";
241 rc = add_modified_attrs( op, &mods );
244 if( rc != LDAP_SUCCESS ) {
245 send_ldap_result( conn, op, rc,
252 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
253 #ifdef SLAPD_MULTIMASTER
254 && ( be->be_update_ndn == NULL ||
255 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
258 /* but we log only the ones not from a replicator user */
259 replog( be, op, dn, mods );
262 #ifndef SLAPD_MULTIMASTER
263 /* send a referral */
265 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
266 be->be_update_refs ? be->be_update_refs : default_referral,
271 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
272 NULL, "operation not supported within namingContext", NULL, NULL );
277 if( ndn != NULL ) free( ndn );
278 if ( modlist != NULL )
279 slap_modlist_free( modlist );
281 slap_mods_free( mods );
285 #ifdef SLAPD_SCHEMA_NOT_COMPAT
287 * convert a raw list of modifications to internal format
288 * Do basic attribute type checking and syntax validation.
290 int slap_modlist2mods(
293 Modifications **mods,
297 Modifications **modtail = mods;
299 for( ; ml != NULL; ml = ml->ml_next ) {
301 AttributeDescription *ad = NULL;
303 mod = (Modifications *)
304 ch_calloc( 1, sizeof(Modifications) );
308 /* convert to attribute description */
309 rc = slap_str2ad( ml->ml_type, &ad, text );
311 if( rc != LDAP_SUCCESS ) {
312 slap_mods_free( mod );
316 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
317 && !slap_ad_is_binary( ad ))
319 /* attribute requires binary transfer */
320 slap_mods_free( mod );
321 *text = "attribute requires ;binary transfer";
322 return LDAP_UNDEFINED_TYPE;
325 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
326 && slap_ad_is_binary( ad ))
328 /* attribute requires binary transfer */
329 slap_mods_free( mod );
330 *text = "attribute disallows ;binary transfer";
331 return LDAP_UNDEFINED_TYPE;
334 if (!update && is_at_no_user_mod( ad->ad_type )) {
335 /* user modification disallowed */
336 slap_mods_free( mod );
337 *text = "no user modification allowed";
338 return LDAP_CONSTRAINT_VIOLATION;
344 if( ml->ml_bvalues != NULL ) {
346 slap_syntax_validate_func *validate =
347 ad->ad_type->sat_syntax->ssyn_validate;
350 Debug( LDAP_DEBUG_TRACE,
351 "modlist2mods: no validator for syntax %s\n",
352 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
353 slap_mods_free( mod );
354 *text = "no validator for syntax";
355 return LDAP_INVALID_SYNTAX;
359 * check that each value is valid per syntax
361 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
362 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
365 slap_mods_free( mod );
366 *text = "value contains invalid data";
367 return LDAP_INVALID_SYNTAX;
372 * a rough single value check... an additional check is needed
373 * to catch add of single value to existing single valued attribute
375 if( ( ml->ml_op == LDAP_MOD_ADD || ml->ml_op == LDAP_MOD_REPLACE )
376 && nvals > 1 && is_at_single_value( ad->ad_type ))
378 slap_mods_free( mod );
379 *text = "multiple values provided";
380 return LDAP_INVALID_SYNTAX;
384 mod->sml_bvalues = ml->ml_bvalues;
385 ml->ml_values = NULL;
388 modtail = &mod->sml_next;
394 int slap_mods_opattrs(
396 Modifications **modtail,
399 struct berval name, timestamp;
400 time_t now = slap_get_time();
405 int mop = op->o_tag == LDAP_REQ_ADD
406 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
408 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
409 ltm = gmtime( &now );
410 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
411 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
412 timestamp.bv_val = timebuf;
413 timestamp.bv_len = strlen(timebuf);
415 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
416 name.bv_val = SLAPD_ANONYMOUS;
417 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
419 name.bv_val = op->o_dn;
420 name.bv_len = strlen( op->o_dn );
423 if( op->o_tag == LDAP_REQ_ADD ) {
424 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
426 mod->sml_desc = slap_schema.si_ad_creatorsName;
427 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
428 mod->sml_bvalues[0] = ber_bvdup( &name );
429 mod->sml_bvalues[1] = NULL;
432 modtail = &mod->sml_next;
434 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
436 mod->sml_desc = slap_schema.si_ad_createTimestamp;
437 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
438 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
439 mod->sml_bvalues[1] = NULL;
441 modtail = &mod->sml_next;
444 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
446 mod->sml_desc = slap_schema.si_ad_modifiersName;
447 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
448 mod->sml_bvalues[0] = ber_bvdup( &name );
449 mod->sml_bvalues[1] = NULL;
451 modtail = &mod->sml_next;
453 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
455 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
456 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
457 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
458 mod->sml_bvalues[1] = NULL;
460 modtail = &mod->sml_next;
467 add_modified_attrs( Operation *op, Modifications **modlist )
471 struct berval *bvals[2];
479 /* remove any attempts by the user to modify these attrs */
480 for ( m = *modlist; m != NULL; m = m->ml_next ) {
481 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
482 return LDAP_CONSTRAINT_VIOLATION;
486 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
487 bv.bv_val = SLAPD_ANONYMOUS;
488 bv.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
490 bv.bv_val = op->o_dn;
491 bv.bv_len = strlen( bv.bv_val );
493 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
494 m->ml_type = ch_strdup( "modifiersname" );
495 m->ml_op = LDAP_MOD_REPLACE;
496 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
497 m->ml_bvalues[0] = ber_bvdup( &bv );
498 m->ml_next = *modlist;
501 currenttime = slap_get_time();
502 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
503 ltm = gmtime( ¤ttime );
504 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
505 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
508 bv.bv_len = strlen( bv.bv_val );
509 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
510 m->ml_type = ch_strdup( "modifytimestamp" );
511 m->ml_op = LDAP_MOD_REPLACE;
512 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
513 m->ml_bvalues[0] = ber_bvdup( &bv );
514 m->ml_next = *modlist;
527 #ifdef SLAPD_SCHEMA_NOT_COMPAT
528 ad_free( mod->sm_desc, 1 );
531 free( mod->sm_desc );
535 if ( mod->sm_bvalues != NULL )
536 ber_bvecfree( mod->sm_bvalues );
549 for ( ; ml != NULL; ml = next ) {
552 slap_mod_free( &ml->sml_mod, 0 );
564 for ( ; ml != NULL; ml = next ) {
570 if ( ml->ml_bvalues != NULL )
571 ber_bvecfree( ml->ml_bvalues );