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;
53 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
56 * Parse the modify request. It looks like this:
58 * ModifyRequest := [APPLICATION 6] SEQUENCE {
59 * name DistinguishedName,
60 * mods SEQUENCE OF SEQUENCE {
61 * operation ENUMERATED {
66 * modification SEQUENCE {
68 * values SET OF AttributeValue
74 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
75 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
76 send_ldap_disconnect( conn, op,
77 LDAP_PROTOCOL_ERROR, "decoding error" );
78 return SLAPD_DISCONNECT;
81 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
83 /* collect modifications & save for later */
85 for ( tag = ber_first_element( op->o_ber, &len, &last );
87 tag = ber_next_element( op->o_ber, &len, last ) )
91 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
93 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
94 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
97 send_ldap_disconnect( conn, op,
98 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
99 rc = SLAPD_DISCONNECT;
105 if ( (*modtail)->ml_bvalues == NULL ) {
106 Debug( LDAP_DEBUG_ANY,
107 "do_modify: modify/add operation (%ld) requires values\n",
109 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
110 NULL, "modify/add operation requires values",
112 rc = LDAP_PROTOCOL_ERROR;
118 case LDAP_MOD_DELETE:
119 case LDAP_MOD_REPLACE:
123 Debug( LDAP_DEBUG_ANY,
124 "do_modify: invalid modify operation (%ld)\n",
126 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
127 NULL, "unrecognized modify operation", NULL, NULL );
128 rc = LDAP_PROTOCOL_ERROR;
133 (*modtail)->ml_op = mop;
135 #ifndef SLAPD_SCHEMA_NOT_COMPAT
136 attr_normalize( (*modtail)->ml_type );
139 modtail = &(*modtail)->ml_next;
143 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
144 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
148 ndn = ch_strdup( dn );
150 if( dn_normalize( ndn ) == NULL ) {
151 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
152 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
153 "invalid DN", NULL, NULL );
158 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
159 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
160 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
161 tmp->ml_op == LDAP_MOD_ADD
162 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
163 ? "delete" : "replace"), tmp->ml_type, 0 );
168 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
169 op->o_connid, op->o_opid, dn, 0, 0 );
172 * We could be serving multiple database backends. Select the
173 * appropriate one, or send a referral to our "referral server"
174 * if we don't hold it.
176 if ( (be = select_backend( ndn )) == NULL ) {
177 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
178 NULL, NULL, default_referral, NULL );
182 /* make sure this backend recongizes critical controls */
183 rc = backend_check_controls( be, conn, op ) ;
185 if( rc != LDAP_SUCCESS ) {
186 send_ldap_result( conn, op, rc,
187 NULL, NULL, NULL, NULL );
191 if ( global_readonly || be->be_readonly ) {
192 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
194 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
195 NULL, "database is read-only", NULL, NULL );
199 /* deref suffix alias if appropriate */
200 ndn = suffix_alias( be, ndn );
203 * do the modify if 1 && (2 || 3)
204 * 1) there is a modify function implemented in this backend;
205 * 2) this backend is master for what it holds;
206 * 3) it's a replica and the dn supplied is the update_ndn.
208 if ( be->be_modify ) {
209 /* do the update here */
210 #ifndef SLAPD_MULTIMASTER
211 /* we don't have to check for replicator dn
212 * because we accept each modify request
214 if ( be->be_update_ndn == NULL ||
215 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
218 int update = be->be_update_ndn != NULL;
219 #ifdef SLAPD_SCHEMA_NOT_COMPAT
221 rc = slap_modlist2mods( modlist, update, &mods, &text );
223 if( rc != LDAP_SUCCESS ) {
224 send_ldap_result( conn, op, rc,
225 NULL, text, NULL, NULL );
233 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
234 global_lastmod == ON)) && !update )
236 #ifdef SLAPD_SCHEMA_NOT_COMPAT
237 rc = slap_mods_opattrs( op, &mods, &text );
239 char *text = "no-user-modification attribute type";
240 rc = add_modified_attrs( op, &mods );
243 if( rc != LDAP_SUCCESS ) {
244 send_ldap_result( conn, op, rc,
251 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
252 #ifdef SLAPD_MULTIMASTER
253 && ( be->be_update_ndn == NULL ||
254 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
257 /* but we log only the ones not from a replicator user */
258 replog( be, op, dn, mods );
261 #ifndef SLAPD_MULTIMASTER
262 /* send a referral */
264 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
265 be->be_update_refs ? be->be_update_refs : default_referral,
270 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
271 NULL, "Function not implemented", NULL, NULL );
277 if ( modlist != NULL )
278 slap_modlist_free( modlist );
280 slap_mods_free( mods );
284 #ifdef SLAPD_SCHEMA_NOT_COMPAT
286 * convert a raw list of modifications to internal format
287 * Do basic attribute type checking and syntax validation.
289 int slap_modlist2mods(
292 Modifications **mods,
296 Modifications **modtail = mods;
298 for( ; ml != NULL; ml = ml->ml_next ) {
300 AttributeDescription *ad;
302 mod = (Modifications *)
303 ch_calloc( 1, sizeof(Modifications) );
307 /* convert to attribute description */
308 rc = slap_str2ad( ml->ml_type, &ad, text );
310 if( rc != LDAP_SUCCESS ) {
311 slap_mods_free( mod );
315 if((ad->ad_type->sat_syntax->ssyn_flags & SLAP_SYNTAX_BINARY)
316 && !( ad->ad_flags & SLAP_DESC_BINARY ))
318 /* attribute requires binary transfer */
319 slap_mods_free( mod );
320 *text = "attribute requires ;binary transfer";
321 return LDAP_UNDEFINED_TYPE;
324 if (!update && is_at_no_user_mod( ad->ad_type )) {
325 /* user modification disallowed */
326 slap_mods_free( mod );
327 *text = "no user modification allowed";
328 return LDAP_CONSTRAINT_VIOLATION;
334 if( ml->ml_bvalues != NULL ) {
336 slap_syntax_validate_func *validate =
337 ad->ad_type->sat_syntax->ssyn_validate;
340 * check that each value is valid per syntax
342 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
343 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
346 slap_mods_free( mod );
347 *text = "value contains invalid data";
348 return LDAP_INVALID_SYNTAX;
353 * a rough single value check... an additional check is needed
354 * to catch add of single value to existing single valued attribute
356 if( ( ml->ml_op == LDAP_MOD_ADD || ml->ml_op == LDAP_MOD_REPLACE )
357 && nvals > 1 && is_at_single_value( ad->ad_type ))
359 slap_mods_free( mod );
360 *text = "multiple values provided";
361 return LDAP_INVALID_SYNTAX;
365 mod->sml_bvalues = ml->ml_bvalues;
366 ml->ml_values = NULL;
369 modtail = &mod->sml_next;
375 int slap_mods_opattrs(
377 Modifications **modtail,
381 struct berval name, timestamp;
382 time_t now = slap_get_time();
386 AttributeDescription *ad;
388 int mop = op->o_tag == LDAP_REQ_ADD
389 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
391 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
392 ltm = gmtime( &now );
393 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
394 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
395 timestamp.bv_val = timebuf;
396 timestamp.bv_len = strlen(timebuf);
398 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
399 name.bv_val = "<anonymous>";
400 name.bv_len = sizeof("<anonymous>")-1;
402 name.bv_val = op->o_dn;
403 name.bv_len = strlen( op->o_dn );
406 if( op->o_tag == LDAP_REQ_ADD ) {
407 rc = slap_str2ad( "creatorsName", &ad, text );
408 if( rc == LDAP_SUCCESS ) {
409 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
412 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
413 mod->sml_bvalues[0] = ber_bvdup( &name );
414 mod->sml_bvalues[1] = NULL;
417 modtail = &mod->sml_next;
420 rc = slap_str2ad( "createTimeStamp", &ad, text );
421 if( rc == LDAP_SUCCESS ) {
422 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
425 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
426 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
427 mod->sml_bvalues[1] = NULL;
429 modtail = &mod->sml_next;
433 rc = slap_str2ad( "modifiersName", &ad, text );
434 if( rc == LDAP_SUCCESS ) {
435 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
438 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
439 mod->sml_bvalues[0] = ber_bvdup( &name );
440 mod->sml_bvalues[1] = NULL;
442 modtail = &mod->sml_next;
445 rc = slap_str2ad( "modifyTimeStamp", &ad, text );
446 if( rc == LDAP_SUCCESS ) {
447 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
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;
462 add_modified_attrs( Operation *op, Modifications **modlist )
466 struct berval *bvals[2];
474 /* remove any attempts by the user to modify these attrs */
475 for ( m = *modlist; m != NULL; m = m->ml_next ) {
476 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
477 return LDAP_CONSTRAINT_VIOLATION;
481 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
482 bv.bv_val = "<anonymous>";
483 bv.bv_len = sizeof("<anonymous>")-1;
485 bv.bv_val = op->o_dn;
486 bv.bv_len = strlen( bv.bv_val );
488 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
489 m->ml_type = ch_strdup( "modifiersname" );
490 m->ml_op = LDAP_MOD_REPLACE;
491 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
492 m->ml_bvalues[0] = ber_bvdup( &bv );
493 m->ml_next = *modlist;
496 currenttime = slap_get_time();
497 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
498 ltm = gmtime( ¤ttime );
499 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
500 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
503 bv.bv_len = strlen( bv.bv_val );
504 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
505 m->ml_type = ch_strdup( "modifytimestamp" );
506 m->ml_op = LDAP_MOD_REPLACE;
507 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
508 m->ml_bvalues[0] = ber_bvdup( &bv );
509 m->ml_next = *modlist;
522 #ifdef SLAPD_SCHEMA_NOT_COMPAT
523 ad_free( mod->sm_desc, 1 );
526 free( mod->sm_desc );
530 if ( mod->sm_bvalues != NULL )
531 ber_bvecfree( mod->sm_bvalues );
544 for ( ; ml != NULL; ml = next ) {
547 slap_mod_free( &ml->sml_mod, 0 );
559 for ( ; ml != NULL; ml = next ) {
565 if ( ml->ml_bvalues != NULL )
566 ber_bvecfree( ml->ml_bvalues );