]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/modify.c
expose oc_check_operational from schema.c
[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 ( oc_check_operational( (*m)->ml_type ) ) {
37                 Debug( LDAP_DEBUG_TRACE,
38                                         "add_lastmods: found operational 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 int
91 bdb2i_back_modify_internal(
92     BackendDB   *be,
93     Connection  *conn,
94     Operation   *op,
95     char        *dn,
96     LDAPModList *modlist,
97     Entry        *e
98 )
99 {
100         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
101         LDAPModList     *ml;
102         int             err;
103
104         Debug(LDAP_DEBUG_ARGS, "bdb2i_back_modify:\n", 0, 0, 0);
105
106         if ( (err = acl_check_modlist( be, conn, op, e, modlist )) != LDAP_SUCCESS ) {
107                 send_ldap_result( conn, op, err, NULL, NULL );
108                 goto error_return;
109         }
110
111         for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
112                 LDAPMod *mod = &ml->ml_mod;
113
114                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
115                 case LDAP_MOD_ADD:
116                         err = bdb2i_add_values( e, mod, op->o_ndn );
117                         break;
118
119                 case LDAP_MOD_DELETE:
120                         err = bdb2i_delete_values( e, mod, op->o_ndn );
121                         break;
122
123                 case LDAP_MOD_REPLACE:
124                         err = bdb2i_replace_values( e, mod, op->o_ndn );
125                         break;
126                 
127                 case LDAP_MOD_SOFTADD:
128                         /* Avoid problems in index_add_mods()
129                          * We need to add index if necessary.
130                          */
131                         mod->mod_op = LDAP_MOD_ADD;
132                         if ( (err = bdb2i_add_values( e, mod, op->o_ndn ))
133                                 ==  LDAP_TYPE_OR_VALUE_EXISTS ) {
134  
135                                 err = LDAP_SUCCESS;
136                                 mod->mod_op = LDAP_MOD_SOFTADD;
137  
138                         }
139                         break;
140                 }
141
142                 if ( err != LDAP_SUCCESS ) {
143                         /* unlock entry, delete from cache */
144                         send_ldap_result( conn, op, err, NULL, NULL );
145                         goto error_return;
146                 }
147         }
148
149         /* check that the entry still obeys the schema */
150         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
151                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
152                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
153                 goto error_return;
154         }
155
156         /* check for abandon */
157         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
158         if ( op->o_abandon ) {
159                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
160                 goto error_return;
161         }
162         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
163
164         /* modify indexes */
165         if ( bdb2i_index_add_mods( be, modlist, e->e_id ) != 0 ) {
166                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
167                 goto error_return;
168         }
169
170         /* check for abandon */
171         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
172         if ( op->o_abandon ) {
173                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
174                 goto error_return;
175         }
176         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
177
178         /* change the entry itself */
179         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
180                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
181                 goto error_return;
182         }
183
184         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
185         bdb2i_cache_return_entry_w( &li->li_cache, e );
186         return( 0 );
187
188 error_return:;
189         bdb2i_cache_return_entry_w( &li->li_cache, e );
190         return( -1 );
191 }
192
193
194 int
195 bdb2_back_modify(
196     BackendDB   *be,
197     Connection  *conn,
198     Operation   *op,
199     char        *dn,
200     LDAPModList *modlist
201 )
202 {
203         DB_LOCK         lock;
204         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
205         struct timeval  time1;
206         int             ret;
207         char            *matched;
208         Entry           *e;
209
210         bdb2i_start_timing( be->bd_info, &time1 );
211
212         if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
213
214                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
215                 return( -1 );
216
217         }
218
219         /*  check, if a new default attribute index will be created,
220                 in which case we have to open the index file BEFORE TP  */
221         switch ( slapMode ) {
222                 case SLAP_SERVER_MODE:
223                 case SLAP_TIMEDSERVER_MODE:
224                 case SLAP_TOOL_MODE:
225                 case SLAP_TOOLID_MODE:
226                         bdb2i_check_default_attr_index_mod( li, modlist );
227                         break;
228         }
229
230         if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
231                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
232                     NULL );
233                 if ( matched != NULL ) {
234                         free( matched );
235                 }
236                 return( -1 );
237         }
238
239          ret = bdb2i_back_modify_internal( be, conn, op, dn, modlist, e );
240         (void) bdb2i_leave_backend_w( lock );
241         bdb2i_stop_timing( be->bd_info, time1, "MOD", conn, op );
242
243         return( ret );
244 }
245
246
247 int
248 bdb2i_add_values(
249     Entry       *e,
250     LDAPMod     *mod,
251     char        *dn
252 )
253 {
254         int             i;
255         Attribute       *a;
256
257         /* check if the values we're adding already exist */
258         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
259                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
260                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
261                             a->a_syntax, 3 ) == 0 ) {
262                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
263                         }
264                 }
265         }
266
267         /* no - add them */
268         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
269                 return( LDAP_CONSTRAINT_VIOLATION );
270         }
271
272         return( LDAP_SUCCESS );
273 }
274
275 int
276 bdb2i_delete_values(
277     Entry       *e,
278     LDAPMod     *mod,
279     char        *dn
280 )
281 {
282         int             i, j, k, found;
283         Attribute       *a;
284
285         /* delete the entire attribute */
286         if ( mod->mod_bvalues == NULL ) {
287                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
288                     mod->mod_type, 0, 0 );
289                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
290                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
291         }
292
293         /* delete specific values - find the attribute first */
294         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
295                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
296                     mod->mod_type, 0, 0 );
297                 return( LDAP_NO_SUCH_ATTRIBUTE );
298         }
299
300         /* find each value to delete */
301         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
302                 found = 0;
303                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
304                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
305                             a->a_syntax, 3 ) != 0 ) {
306                                 continue;
307                         }
308                         found = 1;
309
310                         /* found a matching value - delete it */
311                         ber_bvfree( a->a_vals[j] );
312                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
313                                 a->a_vals[k - 1] = a->a_vals[k];
314                         }
315                         a->a_vals[k - 1] = NULL;
316                         break;
317                 }
318
319                 /* looked through them all w/o finding it */
320                 if ( ! found ) {
321                         Debug( LDAP_DEBUG_ARGS,
322                             "could not find value for attr %s\n",
323                             mod->mod_type, 0, 0 );
324                         return( LDAP_NO_SUCH_ATTRIBUTE );
325                 }
326         }
327
328         return( LDAP_SUCCESS );
329 }
330
331 int
332 bdb2i_replace_values(
333     Entry       *e,
334     LDAPMod     *mod,
335     char        *dn
336 )
337 {
338         (void) attr_delete( &e->e_attrs, mod->mod_type );
339
340         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
341                 return( LDAP_CONSTRAINT_VIOLATION );
342         }
343
344         return( LDAP_SUCCESS );
345 }