1 /* modify.c - bdb2 backend modify routine */
11 #include "back-bdb2.h"
12 #include "proto-back-bdb2.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;
95 bdb2i_back_modify_internal(
100 LDAPModList *modlist,
104 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
108 Debug(LDAP_DEBUG_ARGS, "bdb2i_back_modify:\n", 0, 0, 0);
110 if ( (err = acl_check_modlist( be, conn, op, e, modlist )) != LDAP_SUCCESS ) {
111 send_ldap_result( conn, op, err, NULL, NULL );
115 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
116 LDAPMod *mod = &ml->ml_mod;
118 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
120 err = bdb2i_add_values( e, mod, op->o_ndn );
123 case LDAP_MOD_DELETE:
124 err = bdb2i_delete_values( e, mod, op->o_ndn );
127 case LDAP_MOD_REPLACE:
128 err = bdb2i_replace_values( e, mod, op->o_ndn );
132 if ( err != LDAP_SUCCESS ) {
133 /* unlock entry, delete from cache */
134 send_ldap_result( conn, op, err, NULL, NULL );
139 /* check that the entry still obeys the schema */
140 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
141 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
142 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
146 /* check for abandon */
147 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
148 if ( op->o_abandon ) {
149 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
152 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
155 if ( bdb2i_index_add_mods( be, modlist, e->e_id ) != 0 ) {
156 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
160 /* check for abandon */
161 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
162 if ( op->o_abandon ) {
163 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
166 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
168 /* change the entry itself */
169 if ( bdb2i_id2entry_add( be, e ) != 0 ) {
170 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
174 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
175 bdb2i_cache_return_entry_w( &li->li_cache, e );
179 bdb2i_cache_return_entry_w( &li->li_cache, e );
194 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
195 struct timeval time1;
199 bdb2i_start_timing( be->bd_info, &time1 );
201 if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
203 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
208 /* check, if a new default attribute index will be created,
209 in which case we have to open the index file BEFORE TP */
210 switch ( slapMode ) {
211 case SLAP_SERVER_MODE:
212 case SLAP_TIMEDSERVER_MODE:
214 case SLAP_TOOLID_MODE:
215 bdb2i_check_default_attr_index_mod( li, modlist );
219 if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
220 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
222 if ( matched != NULL ) {
228 ret = bdb2i_back_modify_internal( be, conn, op, dn, modlist, e );
229 (void) bdb2i_leave_backend_w( lock );
230 bdb2i_stop_timing( be->bd_info, time1, "MOD", conn, op );
246 /* check if the values we're adding already exist */
247 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
248 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
249 if ( value_find( a->a_vals, mod->mod_bvalues[i],
250 a->a_syntax, 3 ) == 0 ) {
251 return( LDAP_TYPE_OR_VALUE_EXISTS );
257 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
258 return( LDAP_CONSTRAINT_VIOLATION );
261 return( LDAP_SUCCESS );
274 /* delete the entire attribute */
275 if ( mod->mod_bvalues == NULL ) {
276 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
277 mod->mod_type, 0, 0 );
278 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
279 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
282 /* delete specific values - find the attribute first */
283 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
284 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
285 mod->mod_type, 0, 0 );
286 return( LDAP_NO_SUCH_ATTRIBUTE );
289 /* find each value to delete */
290 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
292 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
293 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
294 a->a_syntax, 3 ) != 0 ) {
299 /* found a matching value - delete it */
300 ber_bvfree( a->a_vals[j] );
301 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
302 a->a_vals[k - 1] = a->a_vals[k];
304 a->a_vals[k - 1] = NULL;
308 /* looked through them all w/o finding it */
310 Debug( LDAP_DEBUG_ARGS,
311 "could not find value for attr %s\n",
312 mod->mod_type, 0, 0 );
313 return( LDAP_NO_SUCH_ATTRIBUTE );
317 return( LDAP_SUCCESS );
321 bdb2i_replace_values(
327 (void) attr_delete( &e->e_attrs, mod->mod_type );
329 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
330 return( LDAP_CONSTRAINT_VIOLATION );
333 return( LDAP_SUCCESS );