]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/modify.c
Return LDAP_CONSTRAINT_VIOLATION if user attempts to modify
[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 int
15 bdb2i_back_modify_internal(
16     BackendDB   *be,
17     Connection  *conn,
18     Operation   *op,
19     char        *dn,
20     LDAPModList *modlist,
21     Entry        *e
22 )
23 {
24         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25         LDAPModList     *ml;
26         int             err;
27
28         Debug(LDAP_DEBUG_ARGS, "bdb2i_back_modify:\n", 0, 0, 0);
29
30         if ( (err = acl_check_modlist( be, conn, op, e, modlist )) != LDAP_SUCCESS ) {
31                 send_ldap_result( conn, op, err,
32                         NULL, NULL, NULL, NULL );
33                 goto error_return;
34         }
35
36         for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
37                 LDAPMod *mod = &ml->ml_mod;
38
39                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
40                 case LDAP_MOD_ADD:
41                         err = bdb2i_add_values( e, mod, op->o_ndn );
42                         break;
43
44                 case LDAP_MOD_DELETE:
45                         err = bdb2i_delete_values( e, mod, op->o_ndn );
46                         break;
47
48                 case LDAP_MOD_REPLACE:
49                         err = bdb2i_replace_values( e, mod, op->o_ndn );
50                         break;
51                 
52                 case LDAP_MOD_SOFTADD:
53                         /* Avoid problems in index_add_mods()
54                          * We need to add index if necessary.
55                          */
56                         mod->mod_op = LDAP_MOD_ADD;
57                         if ( (err = bdb2i_add_values( e, mod, op->o_ndn ))
58                                 ==  LDAP_TYPE_OR_VALUE_EXISTS ) {
59  
60                                 err = LDAP_SUCCESS;
61                                 mod->mod_op = LDAP_MOD_SOFTADD;
62  
63                         }
64                         break;
65                 }
66
67                 if ( err != LDAP_SUCCESS ) {
68                         /* unlock entry, delete from cache */
69                         send_ldap_result( conn, op, err,
70                                 NULL, NULL, NULL, NULL );
71                         goto error_return;
72                 }
73         }
74
75         /* check that the entry still obeys the schema */
76         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
77                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
78                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
79                         NULL, NULL, NULL, NULL );
80                 goto error_return;
81         }
82
83         /* check for abandon */
84         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
85         if ( op->o_abandon ) {
86                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
87                 goto error_return;
88         }
89         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
90
91         /* modify indexes */
92         if ( bdb2i_index_add_mods( be, modlist, e->e_id ) != 0 ) {
93                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
94                         NULL, NULL, NULL, NULL );
95                 goto error_return;
96         }
97
98         /* check for abandon */
99         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
100         if ( op->o_abandon ) {
101                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
102                 goto error_return;
103         }
104         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
105
106         /* change the entry itself */
107         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
108                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
109                         NULL, NULL, NULL, NULL );
110                 goto error_return;
111         }
112
113         send_ldap_result( conn, op, LDAP_SUCCESS,
114                 NULL, NULL, NULL, NULL );
115         return( 0 );
116
117 error_return:;
118         return( -1 );
119 }
120
121
122 int
123 bdb2_back_modify(
124     BackendDB   *be,
125     Connection  *conn,
126     Operation   *op,
127     char        *dn,
128     LDAPModList *modlist
129 )
130 {
131         DB_LOCK         lock;
132         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
133         struct timeval  time1;
134         int             ret, manageDSAit;
135         Entry           *matched;
136         Entry           *e;
137
138         bdb2i_start_timing( be->bd_info, &time1 );
139
140         if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
141                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
142                         NULL, NULL, NULL, NULL );
143                 return( -1 );
144         }
145
146         /*  check, if a new default attribute index will be created,
147                 in which case we have to open the index file BEFORE TP  */
148         switch ( slapMode ) {
149                 case SLAP_SERVER_MODE:
150                 case SLAP_TIMEDSERVER_MODE:
151                 case SLAP_TOOL_MODE:
152                 case SLAP_TOOLID_MODE:
153                         bdb2i_check_default_attr_index_mod( li, modlist );
154                         break;
155         }
156
157         if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
158                 char *matched_dn = NULL;
159                 struct berval **refs = NULL;
160
161                 if ( matched != NULL ) {
162                         matched_dn = ch_strdup( matched->e_dn );
163                         refs = is_entry_referral( matched )
164                                 ? get_entry_referrals( be, conn, op, matched )
165                                 : NULL;
166                         bdb2i_cache_return_entry_r( &li->li_cache, matched );
167                 } else {
168                         refs = default_referral;
169                 }
170
171                 send_ldap_result( conn, op, LDAP_REFERRAL,
172                         matched_dn, NULL, refs, NULL );
173
174                 if( matched != NULL ) {
175                         ber_bvecfree( refs );
176                         free( matched_dn );
177                 }
178
179                 ret = -1;
180                 goto done;
181         }
182
183         if (!manageDSAit && is_entry_referral( e ) ) {
184                 /* entry is a referral, don't allow add */
185                 struct berval **refs = get_entry_referrals( be,
186                         conn, op, e );
187
188                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
189                         0, 0 );
190
191                 send_ldap_result( conn, op, LDAP_REFERRAL,
192                         e->e_dn, NULL, refs, NULL );
193
194                 bdb2i_cache_return_entry_w( &li->li_cache, e );
195
196                 ber_bvecfree( refs );
197
198                 ret = -1;
199                 goto done;
200         }
201
202         ret = bdb2i_back_modify_internal( be, conn, op, dn, modlist, e );
203         bdb2i_cache_return_entry_w( &li->li_cache, e );
204
205 done:
206         (void) bdb2i_leave_backend_w( lock );
207         bdb2i_stop_timing( be->bd_info, time1, "MOD", conn, op );
208
209         return( ret );
210 }
211
212
213 int
214 bdb2i_add_values(
215     Entry       *e,
216     LDAPMod     *mod,
217     char        *dn
218 )
219 {
220         int             i;
221         Attribute       *a;
222
223         /* check if the values we're adding already exist */
224         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
225                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
226                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
227                             a->a_syntax, 3 ) == 0 ) {
228                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
229                         }
230                 }
231         }
232
233         /* no - add them */
234         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
235                 return( LDAP_CONSTRAINT_VIOLATION );
236         }
237
238         return( LDAP_SUCCESS );
239 }
240
241 int
242 bdb2i_delete_values(
243     Entry       *e,
244     LDAPMod     *mod,
245     char        *dn
246 )
247 {
248         int             i, j, k, found;
249         Attribute       *a;
250
251         /* delete the entire attribute */
252         if ( mod->mod_bvalues == NULL ) {
253                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
254                     mod->mod_type, 0, 0 );
255                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
256                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
257         }
258
259         /* delete specific values - find the attribute first */
260         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
261                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
262                     mod->mod_type, 0, 0 );
263                 return( LDAP_NO_SUCH_ATTRIBUTE );
264         }
265
266         /* find each value to delete */
267         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
268                 found = 0;
269                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
270                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
271                             a->a_syntax, 3 ) != 0 ) {
272                                 continue;
273                         }
274                         found = 1;
275
276                         /* found a matching value - delete it */
277                         ber_bvfree( a->a_vals[j] );
278                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
279                                 a->a_vals[k - 1] = a->a_vals[k];
280                         }
281                         a->a_vals[k - 1] = NULL;
282
283                         /* delete the entire attribute, if no values remain */
284                         if ( a->a_vals[0] == NULL) {
285                                 Debug( LDAP_DEBUG_ARGS,
286                                         "removing entire attribute %s\n",
287                                         mod->mod_type, 0, 0 );
288                                 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
289                                         return LDAP_NO_SUCH_ATTRIBUTE;
290                                 }
291                         }
292
293                         break;
294                 }
295
296                 /* looked through them all w/o finding it */
297                 if ( ! found ) {
298                         Debug( LDAP_DEBUG_ARGS,
299                             "could not find value for attr %s\n",
300                             mod->mod_type, 0, 0 );
301                         return( LDAP_NO_SUCH_ATTRIBUTE );
302                 }
303         }
304
305         return( LDAP_SUCCESS );
306 }
307
308 int
309 bdb2i_replace_values(
310     Entry       *e,
311     LDAPMod     *mod,
312     char        *dn
313 )
314 {
315         (void) attr_delete( &e->e_attrs, mod->mod_type );
316
317         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
318                 return( LDAP_CONSTRAINT_VIOLATION );
319         }
320
321         return( LDAP_SUCCESS );
322 }