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