1 /* modify.c - ldbm backend modify routine */
12 #include "back-ldbm.h"
13 #include "proto-back-ldbm.h"
15 static void add_lastmods(Operation *op, LDAPModList **ml);
19 add_lastmods( Operation *op, LDAPModList **modlist )
23 struct berval *bvals[2];
29 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
34 /* remove any attempts by the user to modify these attrs */
35 for ( m = modlist; *m != NULL; m = &(*m)->ml_next ) {
36 if ( strcasecmp( (*m)->ml_type, "modifytimestamp" ) == 0 ||
37 strcasecmp( (*m)->ml_type, "modifiersname" ) == 0 ||
38 strcasecmp( (*m)->ml_type, "createtimestamp" ) == 0 ||
39 strcasecmp( (*m)->ml_type, "creatorsname" ) == 0 ) {
41 Debug( LDAP_DEBUG_TRACE,
42 "add_lastmods: found lastmod attr: %s\n",
43 (*m)->ml_type, 0, 0 );
47 if ( tmp->ml_bvalues != NULL ) {
48 ber_bvecfree( tmp->ml_bvalues );
56 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
58 bv.bv_len = strlen( bv.bv_val );
61 bv.bv_len = strlen( bv.bv_val );
63 tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
64 tmp->ml_type = ch_strdup( "modifiersname" );
65 tmp->ml_op = LDAP_MOD_REPLACE;
66 tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
67 tmp->ml_bvalues[0] = ber_bvdup( &bv );
68 tmp->ml_next = *modlist;
71 currenttime = slap_get_time();
72 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
73 #ifndef LDAP_LOCALTIME
74 ltm = gmtime( ¤ttime );
75 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
77 ltm = localtime( ¤ttime );
78 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
80 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
83 bv.bv_len = strlen( bv.bv_val );
84 tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
85 tmp->ml_type = ch_strdup( "modifytimestamp" );
86 tmp->ml_op = LDAP_MOD_REPLACE;
87 tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
88 tmp->ml_bvalues[0] = ber_bvdup( &bv );
89 tmp->ml_next = *modlist;
94 /* We need this function because of LDAP modrdn. If we do not
95 * add this there would be a bunch of code replication here
96 * and there and of course the likelihood of bugs increases.
97 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
100 int ldbm_modify_internal(
105 LDAPModList *modlist,
113 if ( ((be->be_lastmod == ON)
114 || ((be->be_lastmod == UNDEFINED)&&(global_lastmod == ON)))
115 && (be->be_update_ndn == NULL)) {
117 /* XXX: It may be wrong, it changes mod time even if
120 add_lastmods( op, &modlist );
125 if ( (err = acl_check_modlist( be, conn, op, e, modlist ))
127 send_ldap_result( conn, op, err, NULL, NULL );
131 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
135 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
137 err = add_values( e, mod, op->o_ndn );
140 case LDAP_MOD_DELETE:
141 err = delete_values( e, mod, op->o_ndn );
144 case LDAP_MOD_REPLACE:
145 err = replace_values( e, mod, op->o_ndn );
149 if ( err != LDAP_SUCCESS ) {
150 /* unlock entry, delete from cache */
151 send_ldap_result( conn, op, err, NULL, NULL );
156 /* check that the entry still obeys the schema */
157 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
158 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
159 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
163 /* check for abandon */
164 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
165 if ( op->o_abandon ) {
166 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
169 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
172 if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
173 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
177 /* check for abandon */
178 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
179 if ( op->o_abandon ) {
180 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
183 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
187 }/* int ldbm_modify_internal() */
199 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
204 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
206 /* acquire and lock entry */
207 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
208 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
210 if ( matched != NULL ) {
216 /* Modify the entry */
217 if ( ldbm_modify_internal( be, conn, op, dn, modlist, e ) != 0 ) {
225 /* change the entry itself */
226 if ( id2entry_add( be, e ) != 0 ) {
227 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
231 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
232 cache_return_entry_w( &li->li_cache, e );
236 cache_return_entry_w( &li->li_cache, e );
250 /* check if the values we're adding already exist */
251 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
252 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
253 if ( value_find( a->a_vals, mod->mod_bvalues[i],
254 a->a_syntax, 3 ) == 0 ) {
255 return( LDAP_TYPE_OR_VALUE_EXISTS );
261 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
262 return( LDAP_CONSTRAINT_VIOLATION );
265 return( LDAP_SUCCESS );
278 /* delete the entire attribute */
279 if ( mod->mod_bvalues == NULL ) {
280 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
281 mod->mod_type, 0, 0 );
282 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
283 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
286 /* delete specific values - find the attribute first */
287 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
288 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
289 mod->mod_type, 0, 0 );
290 return( LDAP_NO_SUCH_ATTRIBUTE );
293 /* find each value to delete */
294 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
296 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
297 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
298 a->a_syntax, 3 ) != 0 ) {
303 /* found a matching value - delete it */
304 ber_bvfree( a->a_vals[j] );
305 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
306 a->a_vals[k - 1] = a->a_vals[k];
308 a->a_vals[k - 1] = NULL;
312 /* looked through them all w/o finding it */
314 Debug( LDAP_DEBUG_ARGS,
315 "could not find value for attr %s\n",
316 mod->mod_type, 0, 0 );
317 return( LDAP_NO_SUCH_ATTRIBUTE );
321 return( LDAP_SUCCESS );
331 (void) attr_delete( &e->e_attrs, mod->mod_type );
333 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
334 return( LDAP_CONSTRAINT_VIOLATION );
337 return( LDAP_SUCCESS );