3 * Copyright 1998-1999 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 );
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, "database 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, "Function not implemented", NULL, NULL );
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;
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((ad->ad_type->sat_syntax->ssyn_flags & SLAP_SYNTAX_BINARY)
317 && !( ad->ad_flags & SLAP_DESC_BINARY ))
319 /* attribute requires binary transfer */
320 slap_mods_free( mod );
321 *text = "attribute requires ;binary transfer";
322 return LDAP_UNDEFINED_TYPE;
325 if (!update && is_at_no_user_mod( ad->ad_type )) {
326 /* user modification disallowed */
327 slap_mods_free( mod );
328 *text = "no user modification allowed";
329 return LDAP_CONSTRAINT_VIOLATION;
335 if( ml->ml_bvalues != NULL ) {
337 slap_syntax_validate_func *validate =
338 ad->ad_type->sat_syntax->ssyn_validate;
341 * check that each value is valid per syntax
343 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
344 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
347 slap_mods_free( mod );
348 *text = "value contains invalid data";
349 return LDAP_INVALID_SYNTAX;
354 * a rough single value check... an additional check is needed
355 * to catch add of single value to existing single valued attribute
357 if( ( ml->ml_op == LDAP_MOD_ADD || ml->ml_op == LDAP_MOD_REPLACE )
358 && nvals > 1 && is_at_single_value( ad->ad_type ))
360 slap_mods_free( mod );
361 *text = "multiple values provided";
362 return LDAP_INVALID_SYNTAX;
366 mod->sml_bvalues = ml->ml_bvalues;
367 ml->ml_values = NULL;
370 modtail = &mod->sml_next;
376 int slap_mods_opattrs(
378 Modifications **modtail,
382 struct berval name, timestamp;
383 time_t now = slap_get_time();
387 AttributeDescription *ad;
389 int mop = op->o_tag == LDAP_REQ_ADD
390 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
392 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
393 ltm = gmtime( &now );
394 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
395 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
396 timestamp.bv_val = timebuf;
397 timestamp.bv_len = strlen(timebuf);
399 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
400 name.bv_val = "<anonymous>";
401 name.bv_len = sizeof("<anonymous>")-1;
403 name.bv_val = op->o_dn;
404 name.bv_len = strlen( op->o_dn );
407 if( op->o_tag == LDAP_REQ_ADD ) {
408 rc = slap_str2ad( "creatorsName", &ad, text );
409 if( rc == LDAP_SUCCESS ) {
410 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
413 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
414 mod->sml_bvalues[0] = ber_bvdup( &name );
415 mod->sml_bvalues[1] = NULL;
418 modtail = &mod->sml_next;
421 rc = slap_str2ad( "createTimeStamp", &ad, text );
422 if( rc == LDAP_SUCCESS ) {
423 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
426 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
427 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
428 mod->sml_bvalues[1] = NULL;
430 modtail = &mod->sml_next;
434 rc = slap_str2ad( "modifiersName", &ad, text );
435 if( rc == LDAP_SUCCESS ) {
436 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
439 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
440 mod->sml_bvalues[0] = ber_bvdup( &name );
441 mod->sml_bvalues[1] = NULL;
443 modtail = &mod->sml_next;
446 rc = slap_str2ad( "modifyTimeStamp", &ad, text );
447 if( rc == LDAP_SUCCESS ) {
448 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
451 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
452 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
453 mod->sml_bvalues[1] = NULL;
455 modtail = &mod->sml_next;
463 add_modified_attrs( Operation *op, Modifications **modlist )
467 struct berval *bvals[2];
475 /* remove any attempts by the user to modify these attrs */
476 for ( m = *modlist; m != NULL; m = m->ml_next ) {
477 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
478 return LDAP_CONSTRAINT_VIOLATION;
482 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
483 bv.bv_val = "<anonymous>";
484 bv.bv_len = sizeof("<anonymous>")-1;
486 bv.bv_val = op->o_dn;
487 bv.bv_len = strlen( bv.bv_val );
489 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
490 m->ml_type = ch_strdup( "modifiersname" );
491 m->ml_op = LDAP_MOD_REPLACE;
492 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
493 m->ml_bvalues[0] = ber_bvdup( &bv );
494 m->ml_next = *modlist;
497 currenttime = slap_get_time();
498 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
499 ltm = gmtime( ¤ttime );
500 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
501 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
504 bv.bv_len = strlen( bv.bv_val );
505 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
506 m->ml_type = ch_strdup( "modifytimestamp" );
507 m->ml_op = LDAP_MOD_REPLACE;
508 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
509 m->ml_bvalues[0] = ber_bvdup( &bv );
510 m->ml_next = *modlist;
523 #ifdef SLAPD_SCHEMA_NOT_COMPAT
524 ad_free( mod->sm_desc, 1 );
527 free( mod->sm_desc );
531 if ( mod->sm_bvalues != NULL )
532 ber_bvecfree( mod->sm_bvalues );
545 for ( ; ml != NULL; ml = next ) {
548 slap_mod_free( &ml->sml_mod, 0 );
560 for ( ; ml != NULL; ml = next ) {
566 if ( ml->ml_bvalues != NULL )
567 ber_bvecfree( ml->ml_bvalues );