1 /* modify.c - ldbm backend modify routine */
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
14 static int add_values(Entry *e, LDAPMod *mod, char *dn);
15 static int delete_values(Entry *e, LDAPMod *mod, char *dn);
16 static int replace_values(Entry *e, LDAPMod *mod, char *dn);
17 static void add_lastmods(Operation *op, LDAPMod **mods);
21 add_lastmods( Operation *op, LDAPMod **mods )
25 struct berval *bvals[2];
30 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
35 /* remove any attempts by the user to modify these attrs */
36 for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
37 if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 ||
38 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
39 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 ||
40 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
42 Debug( LDAP_DEBUG_TRACE,
43 "add_lastmods: found lastmod attr: %s\n",
44 (*m)->mod_type, 0, 0 );
47 free( tmp->mod_type );
48 if ( tmp->mod_bvalues != NULL ) {
49 ber_bvecfree( tmp->mod_bvalues );
57 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
59 bv.bv_len = strlen( bv.bv_val );
62 bv.bv_len = strlen( bv.bv_val );
64 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
65 tmp->mod_type = ch_strdup( "modifiersname" );
66 tmp->mod_op = LDAP_MOD_REPLACE;
67 tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
68 2 * sizeof(struct berval *) );
69 tmp->mod_bvalues[0] = ber_bvdup( &bv );
70 tmp->mod_next = *mods;
73 ldap_pvt_thread_mutex_lock( ¤ttime_mutex );
74 #ifndef LDAP_LOCALTIME
75 ltm = gmtime( ¤ttime );
76 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
78 ltm = localtime( ¤ttime );
79 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
81 ldap_pvt_thread_mutex_unlock( ¤ttime_mutex );
83 bv.bv_len = strlen( bv.bv_val );
84 tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
85 tmp->mod_type = ch_strdup( "modifytimestamp" );
86 tmp->mod_op = LDAP_MOD_REPLACE;
87 tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
88 tmp->mod_bvalues[0] = ber_bvdup( &bv );
89 tmp->mod_next = *mods;
93 /* We need this function because of LDAP modrdn. If we do not
94 * add this there would be a bunch of code replication here
95 * and there and of course the likelihood of bugs increases.
96 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
99 int ldbm_internal_modify(
111 if ( ((be->be_lastmod == ON)
112 || ((be->be_lastmod == UNDEFINED)&&(global_lastmod == ON)))
113 && (be->be_update_ndn == NULL)) {
115 /* XXX: It may be wrong, it changes mod time even if
116 * mod fails! I also Think this is leaking memory...
118 add_lastmods( op, &mods );
122 if ( (err = acl_check_mods( be, conn, op, e, mods )) != LDAP_SUCCESS ) {
123 send_ldap_result( conn, op, err, NULL, NULL );
127 for ( mod = mods; mod != NULL; mod = mod->mod_next ) {
128 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
130 err = add_values( e, mod, op->o_ndn );
133 case LDAP_MOD_DELETE:
134 err = delete_values( e, mod, op->o_ndn );
137 case LDAP_MOD_REPLACE:
138 err = replace_values( e, mod, op->o_ndn );
140 case LDAP_MOD_SOFTADD:
141 /* Avoid problems in index_add_mods()
142 * We need to add index if necessary.
144 mod->mod_op = LDAP_MOD_ADD;
145 if ( (err = add_values( e, mod, op->o_ndn ))
146 == LDAP_TYPE_OR_VALUE_EXISTS ) {
149 mod->mod_op = LDAP_MOD_SOFTADD;
155 if ( err != LDAP_SUCCESS ) {
156 /* unlock entry, delete from cache */
157 send_ldap_result( conn, op, err, NULL, NULL );
162 /* check that the entry still obeys the schema */
163 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
164 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
165 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
169 /* check for abandon */
170 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
171 if ( op->o_abandon ) {
172 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
175 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
178 if ( index_add_mods( be, mods, e->e_id ) != 0 ) {
179 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
183 /* check for abandon */
184 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
185 if ( op->o_abandon ) {
186 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
189 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
193 }/* int ldbm_internal_modify() */
205 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
209 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
211 /* acquire and lock entry */
212 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
213 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
215 if ( matched != NULL ) {
221 /* Modify the entry */
222 if ( ldbm_internal_modify( be, conn, op, dn, mods, e ) != 0 ) {
228 /* change the entry itself */
229 if ( id2entry_add( be, e ) != 0 ) {
231 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
235 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
236 cache_return_entry_w( &li->li_cache, e );
240 cache_return_entry_w( &li->li_cache, e );
254 /* check if the values we're adding already exist */
255 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
256 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
257 if ( value_find( a->a_vals, mod->mod_bvalues[i],
258 a->a_syntax, 3 ) == 0 ) {
259 return( LDAP_TYPE_OR_VALUE_EXISTS );
265 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
266 return( LDAP_CONSTRAINT_VIOLATION );
269 return( LDAP_SUCCESS );
282 /* delete the entire attribute */
283 if ( mod->mod_bvalues == NULL ) {
284 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
285 mod->mod_type, 0, 0 );
286 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
287 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
290 /* delete specific values - find the attribute first */
291 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
292 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
293 mod->mod_type, 0, 0 );
294 return( LDAP_NO_SUCH_ATTRIBUTE );
297 /* find each value to delete */
298 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
300 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
301 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
302 a->a_syntax, 3 ) != 0 ) {
307 /* found a matching value - delete it */
308 ber_bvfree( a->a_vals[j] );
309 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
310 a->a_vals[k - 1] = a->a_vals[k];
312 a->a_vals[k - 1] = NULL;
316 /* looked through them all w/o finding it */
318 Debug( LDAP_DEBUG_ARGS,
319 "could not find value for attr %s\n",
320 mod->mod_type, 0, 0 );
321 return( LDAP_NO_SUCH_ATTRIBUTE );
325 return( LDAP_SUCCESS );
335 (void) attr_delete( &e->e_attrs, mod->mod_type );
337 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
338 return( LDAP_CONSTRAINT_VIOLATION );
341 return( LDAP_SUCCESS );