]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
Fixed a memory leak and getting ready to reuse some code between
[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 void     add_lastmods(Operation *op, LDAPModList **ml);
15
16
17 static void
18 add_lastmods( Operation *op, LDAPModList **modlist )
19 {
20         char            buf[22];
21         struct berval   bv;
22         struct berval   *bvals[2];
23         LDAPModList             **m;
24         LDAPModList             *tmp;
25         struct tm       *ltm;
26         time_t          currenttime;
27
28         Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
29
30         bvals[0] = &bv;
31         bvals[1] = NULL;
32
33         /* remove any attempts by the user to modify these attrs */
34         for ( m = modlist; *m != NULL; m = &(*m)->ml_next ) {
35             if ( strcasecmp( (*m)->ml_type, "modifytimestamp" ) == 0 || 
36                                 strcasecmp( (*m)->ml_type, "modifiersname" ) == 0 ||
37                                 strcasecmp( (*m)->ml_type, "createtimestamp" ) == 0 || 
38                                 strcasecmp( (*m)->ml_type, "creatorsname" ) == 0 ) {
39
40                 Debug( LDAP_DEBUG_TRACE,
41                                         "add_lastmods: found lastmod attr: %s\n",
42                                         (*m)->ml_type, 0, 0 );
43                 tmp = *m;
44                 *m = (*m)->ml_next;
45                 free( tmp->ml_type );
46                 if ( tmp->ml_bvalues != NULL ) {
47                     ber_bvecfree( tmp->ml_bvalues );
48                 }
49                 free( tmp );
50                 if (!*m)
51                     break;
52             }
53         }
54
55         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
56                 bv.bv_val = "NULLDN";
57                 bv.bv_len = strlen( bv.bv_val );
58         } else {
59                 bv.bv_val = op->o_dn;
60                 bv.bv_len = strlen( bv.bv_val );
61         }
62         tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
63         tmp->ml_type = ch_strdup( "modifiersname" );
64         tmp->ml_op = LDAP_MOD_REPLACE;
65         tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
66         tmp->ml_bvalues[0] = ber_bvdup( &bv );
67         tmp->ml_next = *modlist;
68         *modlist = tmp;
69
70         currenttime = slap_get_time();
71         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
72 #ifndef LDAP_LOCALTIME
73         ltm = gmtime( &currenttime );
74         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
75 #else
76         ltm = localtime( &currenttime );
77         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
78 #endif
79         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
80
81         bv.bv_val = buf;
82         bv.bv_len = strlen( bv.bv_val );
83         tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
84         tmp->ml_type = ch_strdup( "modifytimestamp" );
85         tmp->ml_op = LDAP_MOD_REPLACE;
86         tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
87         tmp->ml_bvalues[0] = ber_bvdup( &bv );
88         tmp->ml_next = *modlist;
89         *modlist = tmp;
90
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     LDAPModList *modlist,
105     Entry       *e 
106 )
107 {
108         int             i, err;
109         LDAPMod         *mod;
110         LDAPModList     *ml;
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!
118                  */
119                 add_lastmods( op, &modlist );
120
121         }
122
123
124         if ( (err = acl_check_modlist( be, conn, op, e, modlist ))
125              != LDAP_SUCCESS ) {
126                 send_ldap_result( conn, op, err, NULL, NULL );
127                 return -1;
128         }
129
130         for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
131
132                 mod = &ml->ml_mod;
133
134                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
135                 case LDAP_MOD_ADD:
136                         err = add_values( e, mod, op->o_ndn );
137                         break;
138
139                 case LDAP_MOD_DELETE:
140                         err = delete_values( e, mod, op->o_ndn );
141                         break;
142
143                 case LDAP_MOD_REPLACE:
144                         err = replace_values( e, mod, op->o_ndn );
145                         break;
146                 }
147
148                 if ( err != LDAP_SUCCESS ) {
149                         /* unlock entry, delete from cache */
150                         send_ldap_result( conn, op, err, NULL, NULL );
151                         return -1;
152                 }
153         }
154
155         /* check that the entry still obeys the schema */
156         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
157                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
158                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
159                 return -1;
160         }
161
162         /* check for abandon */
163         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
164         if ( op->o_abandon ) {
165                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
166                 return -1;
167         }
168         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
169
170         /* modify indexes */
171         if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
172                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
173                 return -1;
174         }
175
176         /* check for abandon */
177         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
178         if ( op->o_abandon ) {
179                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
180                 return -1;
181         }
182         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
183
184         return 0;
185
186 }/* int ldbm_internal_modify() */
187
188
189 int
190 ldbm_back_modify(
191     Backend     *be,
192     Connection  *conn,
193     Operation   *op,
194     char        *dn,
195     LDAPModList *modlist
196 )
197 {
198         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
199         char            *matched;
200         Entry           *e;
201         int             err;
202
203         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
204
205         /* acquire and lock entry */
206         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
207                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
208                     NULL );
209                 if ( matched != NULL ) {
210                         free( matched );
211                 }
212                 return( -1 );
213         }
214
215         /* Modify the entry */
216         if ( ldbm_internal_modify( be, conn, op, dn, modlist, e ) != 0 ) {
217
218                 goto error_return;
219
220         }
221
222
223
224         /* change the entry itself */
225         if ( id2entry_add( be, e ) != 0 ) {
226                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
227                 goto error_return;
228         }
229
230         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
231         cache_return_entry_w( &li->li_cache, e );
232         return( 0 );
233
234 error_return:;
235         cache_return_entry_w( &li->li_cache, e );
236         return( -1 );
237 }
238
239 int
240 add_values(
241     Entry       *e,
242     LDAPMod     *mod,
243     char        *dn
244 )
245 {
246         int             i;
247         Attribute       *a;
248
249         /* check if the values we're adding already exist */
250         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
251                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
252                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
253                             a->a_syntax, 3 ) == 0 ) {
254                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
255                         }
256                 }
257         }
258
259         /* no - add them */
260         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
261                 return( LDAP_CONSTRAINT_VIOLATION );
262         }
263
264         return( LDAP_SUCCESS );
265 }
266
267 int
268 delete_values(
269     Entry       *e,
270     LDAPMod     *mod,
271     char        *dn
272 )
273 {
274         int             i, j, k, found;
275         Attribute       *a;
276
277         /* delete the entire attribute */
278         if ( mod->mod_bvalues == NULL ) {
279                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
280                     mod->mod_type, 0, 0 );
281                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
282                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
283         }
284
285         /* delete specific values - find the attribute first */
286         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
287                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
288                     mod->mod_type, 0, 0 );
289                 return( LDAP_NO_SUCH_ATTRIBUTE );
290         }
291
292         /* find each value to delete */
293         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
294                 found = 0;
295                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
296                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
297                             a->a_syntax, 3 ) != 0 ) {
298                                 continue;
299                         }
300                         found = 1;
301
302                         /* found a matching value - delete it */
303                         ber_bvfree( a->a_vals[j] );
304                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
305                                 a->a_vals[k - 1] = a->a_vals[k];
306                         }
307                         a->a_vals[k - 1] = NULL;
308                         break;
309                 }
310
311                 /* looked through them all w/o finding it */
312                 if ( ! found ) {
313                         Debug( LDAP_DEBUG_ARGS,
314                             "could not find value for attr %s\n",
315                             mod->mod_type, 0, 0 );
316                         return( LDAP_NO_SUCH_ATTRIBUTE );
317                 }
318         }
319
320         return( LDAP_SUCCESS );
321 }
322
323 int
324 replace_values(
325     Entry       *e,
326     LDAPMod     *mod,
327     char        *dn
328 )
329 {
330         (void) attr_delete( &e->e_attrs, mod->mod_type );
331
332         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
333                 return( LDAP_CONSTRAINT_VIOLATION );
334         }
335
336         return( LDAP_SUCCESS );
337 }