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