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