]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
Fix the ITS#179 fix.
[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         Attribute       *save_attrs;
112
113         if ( ((be->be_lastmod == ON)
114               || ((be->be_lastmod == UNDEFINED)&&(global_lastmod == ON)))
115              && (be->be_update_ndn == NULL)) {
116
117                 /* XXX: It may be wrong, it changes mod time even if 
118                  * mod fails! I also Think this is leaking memory...
119                  */
120                 add_lastmods( op, &mods );
121
122         }
123
124         if ( (err = acl_check_mods( be, conn, op, e, mods )) != LDAP_SUCCESS ) {
125                 send_ldap_result( conn, op, err, NULL, NULL );
126                 return -1;
127         }
128
129         save_attrs = e->e_attrs;
130         e->e_attrs = attrs_dup( e->e_attrs );
131
132         for ( mod = mods; mod != NULL; mod = mod->mod_next ) {
133                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
134                 case LDAP_MOD_ADD:
135                         err = add_values( e, mod, op->o_ndn );
136                         break;
137
138                 case LDAP_MOD_DELETE:
139                         err = delete_values( e, mod, op->o_ndn );
140                         break;
141
142                 case LDAP_MOD_REPLACE:
143                         err = replace_values( e, mod, op->o_ndn );
144                         break;
145
146                 case LDAP_MOD_SOFTADD:
147                         /* Avoid problems in index_add_mods()
148                          * We need to add index if necessary.
149                          */
150                         mod->mod_op = LDAP_MOD_ADD;
151                         if ( (err = add_values( e, mod, op->o_ndn ))
152                                 ==  LDAP_TYPE_OR_VALUE_EXISTS ) {
153  
154                                 err = LDAP_SUCCESS;
155                                 mod->mod_op = LDAP_MOD_SOFTADD;
156  
157                         }
158                         break;
159                 }
160
161                 if ( err != LDAP_SUCCESS ) {
162                         attrs_free( e->e_attrs );
163                         e->e_attrs = save_attrs;
164                         /* unlock entry, delete from cache */
165                         send_ldap_result( conn, op, err, NULL, NULL );
166                         return -1;
167                 }
168         }
169
170         /* check for abandon */
171         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
172         if ( op->o_abandon ) {
173                 attrs_free( e->e_attrs );
174                 e->e_attrs = save_attrs;
175                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
176                 return -1;
177         }
178         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
179
180         /* check that the entry still obeys the schema */
181         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
182                 attrs_free( e->e_attrs );
183                 e->e_attrs = save_attrs;
184                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
185                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
186                 return -1;
187         }
188
189         /* check for abandon */
190         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
191         if ( op->o_abandon ) {
192                 attrs_free( e->e_attrs );
193                 e->e_attrs = save_attrs;
194                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
195                 return -1;
196         }
197         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
198
199         /* remove old indices */
200         if( save_attrs != NULL ) {
201                 for ( mod = mods; mod != NULL; mod = mod->mod_next ) {
202                         if( ( mod->mod_op & ~LDAP_MOD_BVALUES )
203                                 == LDAP_MOD_REPLACE )
204                         {
205                                 /* Need to remove all values from indexes */
206                                 a = attr_find( save_attrs, mod->mod_type );
207
208                                 if( a != NULL ) {
209                                         (void) index_change_values( be,
210                                                 mod->mod_type,
211                                                 a->a_vals,
212                                                 e->e_id,
213                                                 __INDEX_DEL_OP);
214                                 }
215
216                         }
217                 }
218                 attrs_free( save_attrs );
219         }
220
221         /* modify indexes */
222         if ( index_add_mods( be, mods, e->e_id ) != 0 ) {
223                 /* our indices are likely hosed */
224                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
225                         NULL, NULL );
226                 return -1;
227         }
228
229         /* check for abandon */
230         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
231         if ( op->o_abandon ) {
232                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
233                 return -1;
234         }
235         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
236
237         return 0;
238 }
239
240
241 int
242 ldbm_back_modify(
243     Backend     *be,
244     Connection  *conn,
245     Operation   *op,
246     char        *dn,
247     LDAPMod     *mods
248 )
249 {
250         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
251         char            *matched;
252         Entry           *e;
253
254         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
255
256         /* acquire and lock entry */
257         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
258                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
259                     NULL );
260                 if ( matched != NULL ) {
261                         free( matched );
262                 }
263                 return( -1 );
264         }
265
266         /* Modify the entry */
267         if ( ldbm_internal_modify( be, conn, op, dn, mods, e ) != 0 ) {
268
269                 goto error_return;
270
271         }
272
273         /* change the entry itself */
274         if ( id2entry_add( be, e ) != 0 ) {
275                 entry_free( e );
276                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
277                 return -1;
278         }
279
280         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
281         cache_return_entry_w( &li->li_cache, e );
282         return( 0 );
283
284 error_return:;
285         cache_return_entry_w( &li->li_cache, e );
286         return( -1 );
287 }
288
289 static int
290 add_values(
291     Entry       *e,
292     LDAPMod     *mod,
293     char        *dn
294 )
295 {
296         int             i;
297         Attribute       *a;
298
299         /* check if the values we're adding already exist */
300         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
301                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
302                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
303                             a->a_syntax, 3 ) == 0 ) {
304                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
305                         }
306                 }
307         }
308
309         /* no - add them */
310         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
311                 return( LDAP_CONSTRAINT_VIOLATION );
312         }
313
314         return( LDAP_SUCCESS );
315 }
316
317 static int
318 delete_values(
319     Entry       *e,
320     LDAPMod     *mod,
321     char        *dn
322 )
323 {
324         int             i, j, k, found;
325         Attribute       *a;
326
327         /* delete the entire attribute */
328         if ( mod->mod_bvalues == NULL ) {
329                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
330                     mod->mod_type, 0, 0 );
331                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
332                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
333         }
334
335         /* delete specific values - find the attribute first */
336         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
337                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
338                     mod->mod_type, 0, 0 );
339                 return( LDAP_NO_SUCH_ATTRIBUTE );
340         }
341
342         /* find each value to delete */
343         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
344                 found = 0;
345                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
346                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
347                             a->a_syntax, 3 ) != 0 ) {
348                                 continue;
349                         }
350                         found = 1;
351
352                         /* found a matching value - delete it */
353                         ber_bvfree( a->a_vals[j] );
354                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
355                                 a->a_vals[k - 1] = a->a_vals[k];
356                         }
357                         a->a_vals[k - 1] = NULL;
358
359                         /* delete the entire attribute, if no values remain */
360                         if ( a->a_vals[0] == NULL) {
361                                 Debug( LDAP_DEBUG_ARGS,
362                                         "removing entire attribute %s\n",
363                                         mod->mod_type, 0, 0 );
364                                 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
365                                         return LDAP_NO_SUCH_ATTRIBUTE;
366                                 }
367                         }
368
369                         break;
370                 }
371
372                 /* looked through them all w/o finding it */
373                 if ( ! found ) {
374                         Debug( LDAP_DEBUG_ARGS,
375                             "could not find value for attr %s\n",
376                             mod->mod_type, 0, 0 );
377                         return( LDAP_NO_SUCH_ATTRIBUTE );
378                 }
379         }
380
381         return( LDAP_SUCCESS );
382 }
383
384 static int
385 replace_values(
386     Entry       *e,
387     LDAPMod     *mod,
388     char        *dn
389 )
390 {
391         (void) attr_delete( &e->e_attrs, mod->mod_type );
392
393         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
394                 return( LDAP_CONSTRAINT_VIOLATION );
395         }
396
397         return( LDAP_SUCCESS );
398 }