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