]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
e0f2e2330e9509675974d8c74053fd82aa005a9d
[openldap] / servers / slapd / back-ldbm / modify.c
1 /* modify.c - ldbm backend modify routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
13
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);
18
19
20 static void
21 add_lastmods( Operation *op, LDAPMod **mods )
22 {
23         char            buf[22];
24         struct berval   bv;
25         struct berval   *bvals[2];
26         LDAPMod         **m;
27         LDAPMod         *tmp;
28         struct tm       *ltm;
29
30         Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
31
32         bvals[0] = &bv;
33         bvals[1] = NULL;
34
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 ) {
41
42                 Debug( LDAP_DEBUG_TRACE,
43                                         "add_lastmods: found lastmod attr: %s\n",
44                                         (*m)->mod_type, 0, 0 );
45                 tmp = *m;
46                 *m = (*m)->mod_next;
47                 free( tmp->mod_type );
48                 if ( tmp->mod_bvalues != NULL ) {
49                     ber_bvecfree( tmp->mod_bvalues );
50                 }
51                 free( tmp );
52                 if (!*m)
53                     break;
54             }
55         }
56
57         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
58                 bv.bv_val = "NULLDN";
59                 bv.bv_len = strlen( bv.bv_val );
60         } else {
61                 bv.bv_val = op->o_dn;
62                 bv.bv_len = strlen( bv.bv_val );
63         }
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;
71         *mods = tmp;
72
73         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
74 #ifndef LDAP_LOCALTIME
75         ltm = gmtime( &currenttime );
76         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
77 #else
78         ltm = localtime( &currenttime );
79         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
80 #endif
81         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
82         bv.bv_val = buf;
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;
90         *mods = tmp;
91 }
92
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
97  */ 
98
99 int ldbm_internal_modify(
100     Backend     *be,
101     Connection  *conn,
102     Operation   *op,
103     char        *dn,
104     LDAPMod     *mods,
105     Entry       *e 
106 )
107 {
108         int             i, err;
109         LDAPMod         *mod;
110         Attribute       *a;
111
112         if ( ((be->be_lastmod == ON)
113               || ((be->be_lastmod == UNDEFINED)&&(global_lastmod == ON)))
114              && (be->be_update_ndn == NULL)) {
115
116                 /* XXX: It may be wrong, it changes mod time even if 
117                  * mod fails! I also Think this is leaking memory...
118                  */
119                 add_lastmods( op, &mods );
120
121         }
122
123         if ( (err = acl_check_mods( be, conn, op, e, mods )) != LDAP_SUCCESS ) {
124                 send_ldap_result( conn, op, err, NULL, NULL );
125                 return -1;
126         }
127
128         for ( mod = mods; mod != NULL; mod = mod->mod_next ) {
129                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
130                 case LDAP_MOD_ADD:
131                         err = add_values( e, mod, op->o_ndn );
132                         break;
133
134                 case LDAP_MOD_DELETE:
135                         err = delete_values( e, mod, op->o_ndn );
136                         break;
137
138                 case LDAP_MOD_REPLACE:
139                         /* Need to remove all values from indexes before they
140                          * are lost.
141                          */
142                         if( e->e_attrs
143                             && ((a = attr_find( e->e_attrs, mod->mod_type ))
144                            != NULL) ) {
145
146                             (void) index_change_values( be,
147                                                         mod->mod_type,
148                                                         a->a_vals,
149                                                         e->e_id,
150                                                         __INDEX_DEL_OP);
151                         }
152                         err = replace_values( e, mod, op->o_ndn );
153                         break;
154                 case LDAP_MOD_SOFTADD:
155                         /* Avoid problems in index_add_mods()
156                          * We need to add index if necessary.
157                          */
158                         mod->mod_op = LDAP_MOD_ADD;
159                         if ( (err = add_values( e, mod, op->o_ndn ))
160                                 ==  LDAP_TYPE_OR_VALUE_EXISTS ) {
161  
162                                 err = LDAP_SUCCESS;
163                                 mod->mod_op = LDAP_MOD_SOFTADD;
164  
165                         }
166                         break;
167                 }
168
169                 if ( err != LDAP_SUCCESS ) {
170                         /* unlock entry, delete from cache */
171                         send_ldap_result( conn, op, err, NULL, NULL );
172                         return -1;
173                 }
174         }
175
176         /* check that the entry still obeys the schema */
177         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
178                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
179                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
180                 return -1;
181         }
182
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 );
187                 return -1;
188         }
189         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
190
191         /* modify indexes */
192         if ( index_add_mods( be, mods, e->e_id ) != 0 ) {
193                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
194                 return -1;
195         }
196
197         /* check for abandon */
198         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
199         if ( op->o_abandon ) {
200                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
201                 return -1;
202         }
203         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
204
205         return 0;
206
207 }/* int ldbm_internal_modify() */
208
209
210 int
211 ldbm_back_modify(
212     Backend     *be,
213     Connection  *conn,
214     Operation   *op,
215     char        *dn,
216     LDAPMod     *mods
217 )
218 {
219         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
220         char            *matched;
221         Entry           *e;
222
223         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
224
225         /* acquire and lock entry */
226         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
227                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
228                     NULL );
229                 if ( matched != NULL ) {
230                         free( matched );
231                 }
232                 return( -1 );
233         }
234
235         /* Modify the entry */
236         if ( ldbm_internal_modify( be, conn, op, dn, mods, e ) != 0 ) {
237
238                 goto error_return;
239
240         }
241
242         /* change the entry itself */
243         if ( id2entry_add( be, e ) != 0 ) {
244                 entry_free( e );
245                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
246                 return -1;
247         }
248
249         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
250         cache_return_entry_w( &li->li_cache, e );
251         return( 0 );
252
253 error_return:;
254         cache_return_entry_w( &li->li_cache, e );
255         return( -1 );
256 }
257
258 static int
259 add_values(
260     Entry       *e,
261     LDAPMod     *mod,
262     char        *dn
263 )
264 {
265         int             i;
266         Attribute       *a;
267
268         /* check if the values we're adding already exist */
269         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
270                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
271                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
272                             a->a_syntax, 3 ) == 0 ) {
273                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
274                         }
275                 }
276         }
277
278         /* no - add them */
279         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
280                 return( LDAP_CONSTRAINT_VIOLATION );
281         }
282
283         return( LDAP_SUCCESS );
284 }
285
286 static int
287 delete_values(
288     Entry       *e,
289     LDAPMod     *mod,
290     char        *dn
291 )
292 {
293         int             i, j, k, found;
294         Attribute       *a;
295
296         /* delete the entire attribute */
297         if ( mod->mod_bvalues == NULL ) {
298                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
299                     mod->mod_type, 0, 0 );
300                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
301                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
302         }
303
304         /* delete specific values - find the attribute first */
305         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
306                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
307                     mod->mod_type, 0, 0 );
308                 return( LDAP_NO_SUCH_ATTRIBUTE );
309         }
310
311         /* find each value to delete */
312         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
313                 found = 0;
314                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
315                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
316                             a->a_syntax, 3 ) != 0 ) {
317                                 continue;
318                         }
319                         found = 1;
320
321                         /* found a matching value - delete it */
322                         ber_bvfree( a->a_vals[j] );
323                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
324                                 a->a_vals[k - 1] = a->a_vals[k];
325                         }
326                         a->a_vals[k - 1] = NULL;
327
328                         /* delete the entire attribute, if no values remain */
329                         if ( a->a_vals[0] == NULL) {
330                                 Debug( LDAP_DEBUG_ARGS,
331                                         "removing entire attribute %s\n",
332                                         mod->mod_type, 0, 0 );
333                                 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
334                                         return LDAP_NO_SUCH_ATTRIBUTE;
335                                 }
336                         }
337
338                         break;
339                 }
340
341                 /* looked through them all w/o finding it */
342                 if ( ! found ) {
343                         Debug( LDAP_DEBUG_ARGS,
344                             "could not find value for attr %s\n",
345                             mod->mod_type, 0, 0 );
346                         return( LDAP_NO_SUCH_ATTRIBUTE );
347                 }
348         }
349
350         return( LDAP_SUCCESS );
351 }
352
353 static int
354 replace_values(
355     Entry       *e,
356     LDAPMod     *mod,
357     char        *dn
358 )
359 {
360         (void) attr_delete( &e->e_attrs, mod->mod_type );
361
362         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
363                 return( LDAP_CONSTRAINT_VIOLATION );
364         }
365
366         return( LDAP_SUCCESS );
367 }