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