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