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 );
55 if( op->o_bind_in_progress ) {
56 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
58 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
59 NULL, "SASL bind in progress", NULL, NULL );
60 return LDAP_SASL_BIND_IN_PROGRESS;
64 * Parse the modify request. It looks like this:
66 * ModifyRequest := [APPLICATION 6] SEQUENCE {
67 * name DistinguishedName,
68 * mods SEQUENCE OF SEQUENCE {
69 * operation ENUMERATED {
74 * modification SEQUENCE {
76 * values SET OF AttributeValue
82 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
83 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
84 send_ldap_disconnect( conn, op,
85 LDAP_PROTOCOL_ERROR, "decoding error" );
86 return SLAPD_DISCONNECT;
89 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
91 ndn = ch_strdup( dn );
93 if( dn_normalize( ndn ) == NULL ) {
94 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
95 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
96 "invalid DN", NULL, NULL );
100 /* collect modifications & save for later */
102 for ( tag = ber_first_element( op->o_ber, &len, &last );
104 tag = ber_next_element( op->o_ber, &len, last ) )
108 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
110 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
111 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
114 send_ldap_disconnect( conn, op,
115 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
116 rc = SLAPD_DISCONNECT;
122 if ( (*modtail)->ml_bvalues == NULL ) {
123 Debug( LDAP_DEBUG_ANY,
124 "do_modify: modify/add operation (%ld) requires values\n",
126 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
127 NULL, "modify/add operation requires values",
129 rc = LDAP_PROTOCOL_ERROR;
135 case LDAP_MOD_DELETE:
136 case LDAP_MOD_REPLACE:
140 Debug( LDAP_DEBUG_ANY,
141 "do_modify: invalid modify operation (%ld)\n",
143 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
144 NULL, "unrecognized modify operation", NULL, NULL );
145 rc = LDAP_PROTOCOL_ERROR;
150 (*modtail)->ml_op = mop;
152 #ifndef SLAPD_SCHEMA_NOT_COMPAT
153 attr_normalize( (*modtail)->ml_type );
156 modtail = &(*modtail)->ml_next;
160 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
161 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
166 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
167 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
168 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
169 tmp->ml_op == LDAP_MOD_ADD
170 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
171 ? "delete" : "replace"), tmp->ml_type, 0 );
176 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
177 op->o_connid, op->o_opid, dn, 0, 0 );
180 * We could be serving multiple database backends. Select the
181 * appropriate one, or send a referral to our "referral server"
182 * if we don't hold it.
184 if ( (be = select_backend( ndn )) == NULL ) {
185 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
186 NULL, NULL, default_referral, NULL );
190 /* make sure this backend recongizes critical controls */
191 rc = backend_check_controls( be, conn, op ) ;
193 if( rc != LDAP_SUCCESS ) {
194 send_ldap_result( conn, op, rc,
195 NULL, NULL, NULL, NULL );
199 if ( global_readonly || be->be_readonly ) {
200 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
202 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
203 NULL, "database is read-only", NULL, NULL );
207 /* deref suffix alias if appropriate */
208 ndn = suffix_alias( be, ndn );
211 * do the modify if 1 && (2 || 3)
212 * 1) there is a modify function implemented in this backend;
213 * 2) this backend is master for what it holds;
214 * 3) it's a replica and the dn supplied is the update_ndn.
216 if ( be->be_modify ) {
217 /* do the update here */
218 #ifndef SLAPD_MULTIMASTER
219 /* we don't have to check for replicator dn
220 * because we accept each modify request
222 if ( be->be_update_ndn == NULL ||
223 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
226 int update = be->be_update_ndn != NULL;
227 #ifdef SLAPD_SCHEMA_NOT_COMPAT
229 rc = slap_modlist2mods( modlist, update, &mods, &text );
231 if( rc != LDAP_SUCCESS ) {
232 send_ldap_result( conn, op, rc,
233 NULL, text, NULL, NULL );
241 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
242 global_lastmod == ON)) && !update )
244 #ifdef SLAPD_SCHEMA_NOT_COMPAT
245 rc = slap_mods_opattrs( op, &mods, &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, "Function not implemented", NULL, NULL );
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;
310 mod = (Modifications *)
311 ch_calloc( 1, sizeof(Modifications) );
315 /* convert to attribute description */
316 rc = slap_str2ad( ml->ml_type, &ad, text );
318 if( rc != LDAP_SUCCESS ) {
319 slap_mods_free( mod );
323 if((ad->ad_type->sat_syntax->ssyn_flags & SLAP_SYNTAX_BINARY)
324 && !( ad->ad_flags & SLAP_DESC_BINARY ))
326 /* attribute requires binary transfer */
327 slap_mods_free( mod );
328 *text = "attribute requires ;binary transfer";
329 return LDAP_UNDEFINED_TYPE;
332 if (!update && is_at_no_user_mod( ad->ad_type )) {
333 /* user modification disallowed */
334 slap_mods_free( mod );
335 *text = "no user modification allowed";
336 return LDAP_CONSTRAINT_VIOLATION;
342 if( ml->ml_bvalues != NULL ) {
344 slap_syntax_validate_func *validate =
345 ad->ad_type->sat_syntax->ssyn_validate;
348 * check that each value is valid per syntax
350 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
351 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
354 slap_mods_free( mod );
355 *text = "value contains invalid data";
356 return LDAP_INVALID_SYNTAX;
361 * a rough single value check... an additional check is needed
362 * to catch add of single value to existing single valued attribute
364 if( ( ml->ml_op == LDAP_MOD_ADD || ml->ml_op == LDAP_MOD_REPLACE )
365 && nvals > 1 && is_at_single_value( ad->ad_type ))
367 slap_mods_free( mod );
368 *text = "multiple values provided";
369 return LDAP_INVALID_SYNTAX;
373 mod->sml_bvalues = ml->ml_bvalues;
374 ml->ml_values = NULL;
377 modtail = &mod->sml_next;
383 int slap_mods_opattrs(
385 Modifications **modtail,
389 struct berval name, timestamp;
390 time_t now = slap_get_time();
394 AttributeDescription *ad;
396 int mop = op->o_tag == LDAP_REQ_ADD
397 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
399 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
400 ltm = gmtime( &now );
401 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
402 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
403 timestamp.bv_val = timebuf;
404 timestamp.bv_len = strlen(timebuf);
406 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
407 name.bv_val = "<anonymous>";
408 name.bv_len = sizeof("<anonymous>")-1;
410 name.bv_val = op->o_dn;
411 name.bv_len = strlen( op->o_dn );
414 if( op->o_tag == LDAP_REQ_ADD ) {
415 rc = slap_str2ad( "creatorsName", &ad, text );
416 if( rc == LDAP_SUCCESS ) {
417 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
420 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
421 mod->sml_bvalues[0] = ber_bvdup( &name );
422 mod->sml_bvalues[1] = NULL;
425 modtail = &mod->sml_next;
428 rc = slap_str2ad( "createTimeStamp", &ad, text );
429 if( rc == LDAP_SUCCESS ) {
430 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
433 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
434 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
435 mod->sml_bvalues[1] = NULL;
437 modtail = &mod->sml_next;
441 rc = slap_str2ad( "modifiersName", &ad, text );
442 if( rc == LDAP_SUCCESS ) {
443 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
446 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
447 mod->sml_bvalues[0] = ber_bvdup( &name );
448 mod->sml_bvalues[1] = NULL;
450 modtail = &mod->sml_next;
453 rc = slap_str2ad( "modifyTimeStamp", &ad, text );
454 if( rc == LDAP_SUCCESS ) {
455 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
458 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
459 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
460 mod->sml_bvalues[1] = NULL;
462 modtail = &mod->sml_next;
470 add_modified_attrs( Operation *op, Modifications **modlist )
474 struct berval *bvals[2];
482 /* remove any attempts by the user to modify these attrs */
483 for ( m = *modlist; m != NULL; m = m->ml_next ) {
484 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
485 return LDAP_CONSTRAINT_VIOLATION;
489 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
490 bv.bv_val = "<anonymous>";
491 bv.bv_len = sizeof("<anonymous>")-1;
493 bv.bv_val = op->o_dn;
494 bv.bv_len = strlen( bv.bv_val );
496 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
497 m->ml_type = ch_strdup( "modifiersname" );
498 m->ml_op = LDAP_MOD_REPLACE;
499 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
500 m->ml_bvalues[0] = ber_bvdup( &bv );
501 m->ml_next = *modlist;
504 currenttime = slap_get_time();
505 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
506 ltm = gmtime( ¤ttime );
507 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
508 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
511 bv.bv_len = strlen( bv.bv_val );
512 m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
513 m->ml_type = ch_strdup( "modifytimestamp" );
514 m->ml_op = LDAP_MOD_REPLACE;
515 m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
516 m->ml_bvalues[0] = ber_bvdup( &bv );
517 m->ml_next = *modlist;
530 #ifdef SLAPD_SCHEMA_NOT_COMPAT
531 ad_free( mod->sm_desc, 1 );
534 free( mod->sm_desc );
538 if ( mod->sm_bvalues != NULL )
539 ber_bvecfree( mod->sm_bvalues );
552 for ( ; ml != NULL; ml = next ) {
555 slap_mod_free( &ml->sml_mod, 0 );
567 for ( ; ml != NULL; ml = next ) {
573 if ( ml->ml_bvalues != NULL )
574 ber_bvecfree( ml->ml_bvalues );