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>
35 char *dn, *ndn = NULL;
39 LDAPModList *modlist = NULL;
40 LDAPModList **modtail = &modlist;
44 Modifications *mods = NULL;
49 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
52 * Parse the modify request. It looks like this:
54 * ModifyRequest := [APPLICATION 6] SEQUENCE {
55 * name DistinguishedName,
56 * mods SEQUENCE OF SEQUENCE {
57 * operation ENUMERATED {
62 * modification SEQUENCE {
64 * values SET OF AttributeValue
70 if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
71 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
72 send_ldap_disconnect( conn, op,
73 LDAP_PROTOCOL_ERROR, "decoding error" );
74 return SLAPD_DISCONNECT;
77 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
79 /* collect modifications & save for later */
81 for ( tag = ber_first_element( op->o_ber, &len, &last );
83 tag = ber_next_element( op->o_ber, &len, last ) )
87 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
89 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
90 &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
93 send_ldap_disconnect( conn, op,
94 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
95 rc = SLAPD_DISCONNECT;
101 if ( (*modtail)->ml_bvalues == NULL ) {
102 Debug( LDAP_DEBUG_ANY,
103 "do_modify: modify/add operation (%ld) requires values\n",
105 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
106 NULL, "modify/add operation requires values",
108 rc = LDAP_PROTOCOL_ERROR;
114 case LDAP_MOD_DELETE:
115 case LDAP_MOD_REPLACE:
119 Debug( LDAP_DEBUG_ANY,
120 "do_modify: invalid modify operation (%ld)\n",
122 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
123 NULL, "unrecognized modify operation", NULL, NULL );
124 rc = LDAP_PROTOCOL_ERROR;
129 (*modtail)->ml_op = mop;
130 modtail = &(*modtail)->ml_next;
134 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
135 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
139 ndn = ch_strdup( dn );
141 if( dn_normalize( ndn ) == NULL ) {
142 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
143 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
144 "invalid DN", NULL, NULL );
149 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
150 for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
151 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
152 tmp->ml_op == LDAP_MOD_ADD
153 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
154 ? "delete" : "replace"), tmp->ml_type, 0 );
158 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
159 op->o_connid, op->o_opid, dn, 0, 0 );
162 * We could be serving multiple database backends. Select the
163 * appropriate one, or send a referral to our "referral server"
164 * if we don't hold it.
166 if ( (be = select_backend( ndn )) == NULL ) {
167 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
168 NULL, NULL, default_referral, NULL );
172 /* check restrictions */
173 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
174 if( rc != LDAP_SUCCESS ) {
175 send_ldap_result( conn, op, rc,
176 NULL, text, NULL, NULL );
180 /* check for referrals */
181 rc = backend_check_referrals( be, conn, op, dn, ndn );
182 if ( rc != LDAP_SUCCESS ) {
186 /* deref suffix alias if appropriate */
187 ndn = suffix_alias( be, ndn );
190 * do the modify if 1 && (2 || 3)
191 * 1) there is a modify function implemented in this backend;
192 * 2) this backend is master for what it holds;
193 * 3) it's a replica and the dn supplied is the update_ndn.
195 if ( be->be_modify ) {
196 /* do the update here */
197 #ifndef SLAPD_MULTIMASTER
198 /* we don't have to check for replicator dn
199 * because we accept each modify request
201 if ( be->be_update_ndn == NULL ||
202 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
205 int update = be->be_update_ndn != NULL;
207 rc = slap_modlist2mods( modlist, update, &mods, &text );
209 if( rc != LDAP_SUCCESS ) {
210 send_ldap_result( conn, op, rc,
211 NULL, text, NULL, NULL );
215 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
216 global_lastmod == ON)) && !update )
218 Modifications **modstail;
219 for( modstail = &mods;
221 modstail = &(*modstail)->sml_next )
225 rc = slap_mods_opattrs( op, modstail, &text );
227 if( rc != LDAP_SUCCESS ) {
228 send_ldap_result( conn, op, rc,
235 if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0
236 #ifdef SLAPD_MULTIMASTER
237 && ( be->be_update_ndn == NULL ||
238 strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
241 /* but we log only the ones not from a replicator user */
242 replog( be, op, dn, mods );
245 #ifndef SLAPD_MULTIMASTER
246 /* send a referral */
248 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
249 be->be_update_refs ? be->be_update_refs : default_referral,
254 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
255 NULL, "operation not supported within namingContext", NULL, NULL );
260 if( ndn != NULL ) free( ndn );
261 if ( modlist != NULL )
262 slap_modlist_free( modlist );
264 slap_mods_free( mods );
269 * convert a raw list of modifications to internal format
270 * Do basic attribute type checking and syntax validation.
272 int slap_modlist2mods(
275 Modifications **mods,
279 Modifications **modtail = mods;
281 for( ; ml != NULL; ml = ml->ml_next ) {
283 AttributeDescription *ad = NULL;
285 mod = (Modifications *)
286 ch_calloc( 1, sizeof(Modifications) );
289 mod->sml_op = ml->ml_op;
291 /* convert to attribute description */
292 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
294 if( rc != LDAP_SUCCESS ) {
295 slap_mods_free( mod );
301 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
302 && !slap_ad_is_binary( ad ))
304 /* attribute requires binary transfer */
305 slap_mods_free( mod );
306 *text = "attribute requires ;binary transfer";
307 return LDAP_UNDEFINED_TYPE;
310 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
311 && slap_ad_is_binary( ad ))
313 /* attribute requires binary transfer */
314 slap_mods_free( mod );
315 *text = "attribute disallows ;binary transfer";
316 return LDAP_UNDEFINED_TYPE;
319 if (!update && is_at_no_user_mod( ad->ad_type )) {
320 /* user modification disallowed */
321 slap_mods_free( mod );
322 *text = "no user modification allowed";
323 return LDAP_CONSTRAINT_VIOLATION;
329 if( ml->ml_bvalues != NULL ) {
331 slap_syntax_validate_func *validate =
332 ad->ad_type->sat_syntax->ssyn_validate;
335 Debug( LDAP_DEBUG_TRACE,
336 "modlist2mods: no validator for syntax %s\n",
337 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
338 slap_mods_free( mod );
339 *text = "no validator for syntax";
340 return LDAP_INVALID_SYNTAX;
344 * check that each value is valid per syntax
346 for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
347 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
350 slap_mods_free( mod );
351 *text = "value contains invalid data";
352 return LDAP_INVALID_SYNTAX;
357 * a rough single value check... an additional check is needed
358 * to catch add of single value to existing single valued attribute
360 if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
361 && nvals > 1 && is_at_single_value( ad->ad_type ))
363 slap_mods_free( mod );
364 *text = "multiple values provided";
365 return LDAP_INVALID_SYNTAX;
369 mod->sml_bvalues = ml->ml_bvalues;
370 ml->ml_values = NULL;
373 modtail = &mod->sml_next;
379 int slap_mods_opattrs(
381 Modifications **modtail,
384 struct berval name, timestamp;
385 time_t now = slap_get_time();
390 int mop = op->o_tag == LDAP_REQ_ADD
391 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
393 assert( modtail != NULL );
394 assert( *modtail == NULL );
396 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
397 ltm = gmtime( &now );
398 strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
399 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
400 timestamp.bv_val = timebuf;
401 timestamp.bv_len = strlen(timebuf);
403 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
404 name.bv_val = SLAPD_ANONYMOUS;
405 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
407 name.bv_val = op->o_dn;
408 name.bv_len = strlen( op->o_dn );
411 if( op->o_tag == LDAP_REQ_ADD ) {
412 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
414 mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
415 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
416 mod->sml_bvalues[0] = ber_bvdup( &name );
417 mod->sml_bvalues[1] = NULL;
420 modtail = &mod->sml_next;
422 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
424 mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
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;
432 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
434 mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
435 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
436 mod->sml_bvalues[0] = ber_bvdup( &name );
437 mod->sml_bvalues[1] = NULL;
439 modtail = &mod->sml_next;
441 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
443 mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
444 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
445 mod->sml_bvalues[0] = ber_bvdup( ×tamp );
446 mod->sml_bvalues[1] = NULL;
448 modtail = &mod->sml_next;
460 ad_free( mod->sm_desc, 1 );
462 if ( mod->sm_bvalues != NULL )
463 ber_bvecfree( mod->sm_bvalues );
476 for ( ; ml != NULL; ml = next ) {
479 slap_mod_free( &ml->sml_mod, 0 );
491 for ( ; ml != NULL; ml = next ) {
497 if ( ml->ml_bvalues != NULL )
498 ber_bvecfree( ml->ml_bvalues );