]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/modify.c
Fixed segfault in a ldif2ldbm-bdb2 child
[openldap] / servers / slapd / back-bdb2 / modify.c
1 /* modify.c - bdb2 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-bdb2.h"
12 #include "proto-back-bdb2.h"
13
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 ( 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 ) {
40
41                 Debug( LDAP_DEBUG_TRACE,
42                                         "add_lastmods: found lastmod attr: %s\n",
43                                         (*m)->ml_type, 0, 0 );
44                 tmp = *m;
45                 *m = (*m)->ml_next;
46                 free( tmp->ml_type );
47                 if ( tmp->ml_bvalues != NULL ) {
48                     ber_bvecfree( tmp->ml_bvalues );
49                 }
50                 free( tmp );
51                 if (!*m)
52                     break;
53             }
54         }
55
56         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
57                 bv.bv_val = "NULLDN";
58                 bv.bv_len = strlen( bv.bv_val );
59         } else {
60                 bv.bv_val = op->o_dn;
61                 bv.bv_len = strlen( bv.bv_val );
62         }
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;
69         *modlist = tmp;
70
71         currenttime = slap_get_time();
72         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
73 #ifndef LDAP_LOCALTIME
74         ltm = gmtime( &currenttime );
75         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
76 #else
77         ltm = localtime( &currenttime );
78         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
79 #endif
80         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
81
82         bv.bv_val = buf;
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;
90         *modlist = tmp;
91
92 }
93
94 static int
95 bdb2i_back_modify_internal(
96     BackendDB   *be,
97     Connection  *conn,
98     Operation   *op,
99     char        *dn,
100     LDAPModList *modlist,
101     Entry        *e
102 )
103 {
104         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
105         LDAPModList     *ml;
106         int             err;
107
108         Debug(LDAP_DEBUG_ARGS, "bdb2i_back_modify:\n", 0, 0, 0);
109
110         if ( (err = acl_check_modlist( be, conn, op, e, modlist )) != LDAP_SUCCESS ) {
111                 send_ldap_result( conn, op, err, NULL, NULL );
112                 goto error_return;
113         }
114
115         for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
116                 LDAPMod *mod = &ml->ml_mod;
117
118                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
119                 case LDAP_MOD_ADD:
120                         err = bdb2i_add_values( e, mod, op->o_ndn );
121                         break;
122
123                 case LDAP_MOD_DELETE:
124                         err = bdb2i_delete_values( e, mod, op->o_ndn );
125                         break;
126
127                 case LDAP_MOD_REPLACE:
128                         err = bdb2i_replace_values( e, mod, op->o_ndn );
129                         break;
130                 }
131
132                 if ( err != LDAP_SUCCESS ) {
133                         /* unlock entry, delete from cache */
134                         send_ldap_result( conn, op, err, NULL, NULL );
135                         goto error_return;
136                 }
137         }
138
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 );
143                 goto error_return;
144         }
145
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 );
150                 goto error_return;
151         }
152         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
153
154         /* modify indexes */
155         if ( bdb2i_index_add_mods( be, modlist, e->e_id ) != 0 ) {
156                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
157                 goto error_return;
158         }
159
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 );
164                 goto error_return;
165         }
166         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
167
168         /* change the entry itself */
169         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
170                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
171                 goto error_return;
172         }
173
174         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
175         bdb2i_cache_return_entry_w( &li->li_cache, e );
176         return( 0 );
177
178 error_return:;
179         bdb2i_cache_return_entry_w( &li->li_cache, e );
180         return( -1 );
181 }
182
183
184 int
185 bdb2_back_modify(
186     BackendDB   *be,
187     Connection  *conn,
188     Operation   *op,
189     char        *dn,
190     LDAPModList *modlist
191 )
192 {
193         DB_LOCK         lock;
194         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
195         struct timeval  time1;
196         int             ret;
197         char            *matched;
198         Entry           *e;
199
200         bdb2i_start_timing( be->bd_info, &time1 );
201
202         if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
203
204                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
205                 return( -1 );
206
207         }
208
209         /*  check, if a new default attribute index will be created,
210                 in which case we have to open the index file BEFORE TP  */
211         switch ( slapMode ) {
212                 case SLAP_SERVER_MODE:
213                 case SLAP_TIMEDSERVER_MODE:
214                 case SLAP_TOOL_MODE:
215                 case SLAP_TOOLID_MODE:
216                         bdb2i_check_default_attr_index_mod( li, modlist );
217                         break;
218         }
219
220         if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
221                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
222                     NULL );
223                 if ( matched != NULL ) {
224                         free( matched );
225                 }
226                 return( -1 );
227         }
228
229          ret = bdb2i_back_modify_internal( be, conn, op, dn, modlist, e );
230         (void) bdb2i_leave_backend_w( lock );
231         bdb2i_stop_timing( be->bd_info, time1, "MOD", conn, op );
232
233         return( ret );
234 }
235
236
237 int
238 bdb2i_add_values(
239     Entry       *e,
240     LDAPMod     *mod,
241     char        *dn
242 )
243 {
244         int             i;
245         Attribute       *a;
246
247         /* check if the values we're adding already exist */
248         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
249                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
250                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
251                             a->a_syntax, 3 ) == 0 ) {
252                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
253                         }
254                 }
255         }
256
257         /* no - add them */
258         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
259                 return( LDAP_CONSTRAINT_VIOLATION );
260         }
261
262         return( LDAP_SUCCESS );
263 }
264
265 int
266 bdb2i_delete_values(
267     Entry       *e,
268     LDAPMod     *mod,
269     char        *dn
270 )
271 {
272         int             i, j, k, found;
273         Attribute       *a;
274
275         /* delete the entire attribute */
276         if ( mod->mod_bvalues == NULL ) {
277                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
278                     mod->mod_type, 0, 0 );
279                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
280                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
281         }
282
283         /* delete specific values - find the attribute first */
284         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
285                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
286                     mod->mod_type, 0, 0 );
287                 return( LDAP_NO_SUCH_ATTRIBUTE );
288         }
289
290         /* find each value to delete */
291         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
292                 found = 0;
293                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
294                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
295                             a->a_syntax, 3 ) != 0 ) {
296                                 continue;
297                         }
298                         found = 1;
299
300                         /* found a matching value - delete it */
301                         ber_bvfree( a->a_vals[j] );
302                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
303                                 a->a_vals[k - 1] = a->a_vals[k];
304                         }
305                         a->a_vals[k - 1] = NULL;
306                         break;
307                 }
308
309                 /* looked through them all w/o finding it */
310                 if ( ! found ) {
311                         Debug( LDAP_DEBUG_ARGS,
312                             "could not find value for attr %s\n",
313                             mod->mod_type, 0, 0 );
314                         return( LDAP_NO_SUCH_ATTRIBUTE );
315                 }
316         }
317
318         return( LDAP_SUCCESS );
319 }
320
321 int
322 bdb2i_replace_values(
323     Entry       *e,
324     LDAPMod     *mod,
325     char        *dn
326 )
327 {
328         (void) attr_delete( &e->e_attrs, mod->mod_type );
329
330         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
331                 return( LDAP_CONSTRAINT_VIOLATION );
332         }
333
334         return( LDAP_SUCCESS );
335 }