1 /* modify.c - ldbm backend modify routine */
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
14 static void add_lastmods(Operation *op, LDAPModList **ml);
18 add_lastmods( Operation *op, LDAPModList **modlist )
22 struct berval *bvals[2];
28 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
33 /* remove any attempts by the user to modify these attrs */
34 for ( m = modlist; *m != NULL; m = &(*m)->ml_next ) {
35 if ( strcasecmp( (*m)->ml_type, "modifytimestamp" ) == 0 ||
36 strcasecmp( (*m)->ml_type, "modifiersname" ) == 0 ||
37 strcasecmp( (*m)->ml_type, "createtimestamp" ) == 0 ||
38 strcasecmp( (*m)->ml_type, "creatorsname" ) == 0 ) {
40 Debug( LDAP_DEBUG_TRACE,
41 "add_lastmods: found lastmod attr: %s\n",
42 (*m)->ml_type, 0, 0 );
46 if ( tmp->ml_bvalues != NULL ) {
47 ber_bvecfree( tmp->ml_bvalues );
55 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
57 bv.bv_len = strlen( bv.bv_val );
60 bv.bv_len = strlen( bv.bv_val );
62 tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
63 tmp->ml_type = ch_strdup( "modifiersname" );
64 tmp->ml_op = LDAP_MOD_REPLACE;
65 tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
66 tmp->ml_bvalues[0] = ber_bvdup( &bv );
67 tmp->ml_next = *modlist;
70 currenttime = slap_get_time();
71 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
72 #ifndef LDAP_LOCALTIME
73 ltm = gmtime( ¤ttime );
74 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
76 ltm = localtime( ¤ttime );
77 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
79 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
82 bv.bv_len = strlen( bv.bv_val );
83 tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
84 tmp->ml_type = ch_strdup( "modifytimestamp" );
85 tmp->ml_op = LDAP_MOD_REPLACE;
86 tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
87 tmp->ml_bvalues[0] = ber_bvdup( &bv );
88 tmp->ml_next = *modlist;
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(
104 LDAPModList *modlist,
112 if ( ((be->be_lastmod == ON)
113 || ((be->be_lastmod == UNDEFINED)&&(global_lastmod == ON)))
114 && (be->be_update_ndn == NULL)) {
116 /* XXX: It may be wrong, it changes mod time even if
119 add_lastmods( op, &modlist );
124 if ( (err = acl_check_modlist( be, conn, op, e, modlist ))
126 send_ldap_result( conn, op, err, NULL, NULL );
130 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
134 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
136 err = add_values( e, mod, op->o_ndn );
139 case LDAP_MOD_DELETE:
140 err = delete_values( e, mod, op->o_ndn );
143 case LDAP_MOD_REPLACE:
144 err = replace_values( e, mod, op->o_ndn );
148 if ( err != LDAP_SUCCESS ) {
149 /* unlock entry, delete from cache */
150 send_ldap_result( conn, op, err, NULL, NULL );
155 /* check that the entry still obeys the schema */
156 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
157 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
158 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
162 /* check for abandon */
163 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
164 if ( op->o_abandon ) {
165 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
168 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
171 if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
172 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
176 /* check for abandon */
177 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
178 if ( op->o_abandon ) {
179 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
182 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
186 }/* int ldbm_internal_modify() */
198 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
203 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
205 /* acquire and lock entry */
206 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
207 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
209 if ( matched != NULL ) {
215 /* Modify the entry */
216 if ( ldbm_internal_modify( be, conn, op, dn, modlist, e ) != 0 ) {
224 /* change the entry itself */
225 if ( id2entry_add( be, e ) != 0 ) {
226 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
230 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
231 cache_return_entry_w( &li->li_cache, e );
235 cache_return_entry_w( &li->li_cache, e );
249 /* check if the values we're adding already exist */
250 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
251 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
252 if ( value_find( a->a_vals, mod->mod_bvalues[i],
253 a->a_syntax, 3 ) == 0 ) {
254 return( LDAP_TYPE_OR_VALUE_EXISTS );
260 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
261 return( LDAP_CONSTRAINT_VIOLATION );
264 return( LDAP_SUCCESS );
277 /* delete the entire attribute */
278 if ( mod->mod_bvalues == NULL ) {
279 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
280 mod->mod_type, 0, 0 );
281 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
282 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
285 /* delete specific values - find the attribute first */
286 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
287 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
288 mod->mod_type, 0, 0 );
289 return( LDAP_NO_SUCH_ATTRIBUTE );
292 /* find each value to delete */
293 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
295 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
296 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
297 a->a_syntax, 3 ) != 0 ) {
302 /* found a matching value - delete it */
303 ber_bvfree( a->a_vals[j] );
304 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
305 a->a_vals[k - 1] = a->a_vals[k];
307 a->a_vals[k - 1] = NULL;
311 /* looked through them all w/o finding it */
313 Debug( LDAP_DEBUG_ARGS,
314 "could not find value for attr %s\n",
315 mod->mod_type, 0, 0 );
316 return( LDAP_NO_SUCH_ATTRIBUTE );
320 return( LDAP_SUCCESS );
330 (void) attr_delete( &e->e_attrs, mod->mod_type );
332 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
333 return( LDAP_CONSTRAINT_VIOLATION );
336 return( LDAP_SUCCESS );