]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
Fix previous commit
[openldap] / servers / slapd / back-bdb / modify.c
1 /* modify.c - bdb backend modify routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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 #include <ac/string.h>
12 #include <ac/time.h>
13
14 #include "back-bdb.h"
15 #include "external.h"
16
17 int bdb_modify_internal(
18         BackendDB *be,
19         Connection *conn,
20         Operation *op,
21         DB_TXN *tid,
22         Modifications *modlist,
23         Entry *e,
24         const char **text,
25         char *textbuf,
26         size_t textlen )
27 {
28         int rc, err;
29         Modification    *mod;
30         Modifications   *ml;
31         Attribute       *save_attrs;
32         Attribute       *ap;
33
34         Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
35                 e->e_id, e->e_dn, 0);
36
37         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
38                 return LDAP_INSUFFICIENT_ACCESS;
39         }
40
41         save_attrs = e->e_attrs;
42         e->e_attrs = attrs_dup( e->e_attrs );
43
44         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
45                 mod = &ml->sml_mod;
46
47                 switch ( mod->sm_op ) {
48                 case LDAP_MOD_ADD:
49                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
50                         err = modify_add_values( e, mod, text, textbuf, textlen );
51                         if( err != LDAP_SUCCESS ) {
52                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
53                                         err, *text, 0);
54                         }
55                         break;
56
57                 case LDAP_MOD_DELETE:
58                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
59                         err = modify_delete_values( e, mod, text, textbuf, textlen );
60                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
61                         if( err != LDAP_SUCCESS ) {
62                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
63                                         err, *text, 0);
64                         }
65                         break;
66
67                 case LDAP_MOD_REPLACE:
68                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
69                         err = modify_replace_values( e, mod, text, textbuf, textlen );
70                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
71                         if( err != LDAP_SUCCESS ) {
72                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
73                                         err, *text, 0);
74                         }
75                         break;
76
77                 case SLAP_MOD_SOFTADD:
78                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
79                         /* Avoid problems in index_add_mods()
80                          * We need to add index if necessary.
81                          */
82                         mod->sm_op = LDAP_MOD_ADD;
83
84                         err = modify_add_values( e, mod, text, textbuf, textlen );
85                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
86                                 err = LDAP_SUCCESS;
87                         }
88
89                         if( err != LDAP_SUCCESS ) {
90                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
91                                         err, *text, 0);
92                         }
93                         break;
94
95                 default:
96                         Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
97                                 mod->sm_op, 0, 0);
98                         *text = "Invalid modify operation";
99                         err = LDAP_OTHER;
100                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
101                                 err, *text, 0);
102                 }
103
104                 if ( err != LDAP_SUCCESS ) {
105                         attrs_free( e->e_attrs );
106                         e->e_attrs = save_attrs;
107                         /* unlock entry, delete from cache */
108                         return err; 
109                 }
110
111                 /* If objectClass was modified, reset the flags */
112                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
113                         e->e_ocflags = 0;
114                 }
115
116                 /* check if modified attribute was indexed */
117                 err = bdb_index_is_indexed( be, mod->sm_desc );
118                 if ( err == LDAP_SUCCESS ) {
119                         ap = attr_find( save_attrs, mod->sm_desc );
120                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
121
122                         ap = attr_find( e->e_attrs, mod->sm_desc );
123                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
124                 }
125         }
126
127         /* check that the entry still obeys the schema */
128         rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
129         if ( rc != LDAP_SUCCESS ) {
130                 attrs_free( e->e_attrs );
131                 e->e_attrs = save_attrs;
132                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
133                         *text, 0, 0 );
134                 return rc;
135         }
136
137         /* update the indices of the modified attributes */
138
139         /* start with deleting the old index entries */
140         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
141                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
142                         rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
143                                                e->e_id, SLAP_INDEX_DELETE_OP );
144                         if ( rc != LDAP_SUCCESS ) {
145                                 attrs_free( e->e_attrs );
146                                 e->e_attrs = save_attrs;
147                                 Debug( LDAP_DEBUG_ANY,
148                                        "Attribute index delete failure",
149                                        0, 0, 0 );
150                                 return rc;
151                         }
152                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
153                 }
154         }
155
156         /* add the new index entries */
157         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
158                 if (ap->a_flags & SLAP_ATTR_IXADD) {
159                         rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
160                                                e->e_id, SLAP_INDEX_ADD_OP );
161                         if ( rc != LDAP_SUCCESS ) {
162                                 attrs_free( e->e_attrs );
163                                 e->e_attrs = save_attrs;
164                                 Debug( LDAP_DEBUG_ANY,
165                                        "Attribute index add failure",
166                                        0, 0, 0 );
167                                 return rc;
168                         }
169                         ap->a_flags &= ~SLAP_ATTR_IXADD;
170                 }
171         }
172
173         return rc;
174 }
175
176
177 int
178 bdb_modify(
179         BackendDB       *be,
180         Connection      *conn,
181         Operation       *op,
182         struct berval   *dn,
183         struct berval   *ndn,
184         Modifications   *modlist )
185 {
186         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
187         int rc;
188         Entry           *matched;
189         Entry           *e;
190         int             manageDSAit = get_manageDSAit( op );
191         const char *text = NULL;
192         char textbuf[SLAP_TEXT_BUFLEN];
193         size_t textlen = sizeof textbuf;
194         DB_TXN  *ltid = NULL;
195         struct bdb_op_info opinfo;
196
197         Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
198
199         if( 0 ) {
200 retry:  /* transaction retry */
201                 if( e != NULL ) {
202                         bdb_cache_delete_entry(&bdb->bi_cache, e);
203                         bdb_cache_return_entry_w(&bdb->bi_cache, e);
204                 }
205                 Debug(LDAP_DEBUG_TRACE,
206                         "bdb_modify: retrying...\n", 0, 0, 0);
207                 rc = txn_abort( ltid );
208                 ltid = NULL;
209                 op->o_private = NULL;
210                 if( rc != 0 ) {
211                         rc = LDAP_OTHER;
212                         text = "internal error";
213                         goto return_results;
214                 }
215                 ldap_pvt_thread_yield();
216         }
217
218         if( bdb->bi_txn ) {
219                 /* begin transaction */
220                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
221                         bdb->bi_db_opflags );
222                 text = NULL;
223                 if( rc != 0 ) {
224                         Debug( LDAP_DEBUG_TRACE,
225                                 "bdb_modify: txn_begin failed: %s (%d)\n",
226                                 db_strerror(rc), rc, 0 );
227                         rc = LDAP_OTHER;
228                         text = "internal error";
229                         goto return_results;
230                 }
231         }
232
233         opinfo.boi_bdb = be;
234         opinfo.boi_txn = ltid;
235         opinfo.boi_err = 0;
236         op->o_private = &opinfo;
237
238         /* get entry */
239         rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0 );
240
241         if ( rc != 0 ) {
242                 Debug( LDAP_DEBUG_TRACE,
243                         "bdb_modify: dn2entry failed (%d)\n",
244                         rc, 0, 0 );
245                 switch( rc ) {
246                 case DB_LOCK_DEADLOCK:
247                 case DB_LOCK_NOTGRANTED:
248                         goto retry;
249                 case DB_NOTFOUND:
250                         break;
251                 default:
252                         rc = LDAP_OTHER;
253                 }
254                 text = "internal error";
255                 goto return_results;
256         }
257
258         /* acquire and lock entry */
259         if ( e == NULL ) {
260                 char* matched_dn = NULL;
261                 BerVarray refs;
262
263                 if ( matched != NULL ) {
264                         matched_dn = ch_strdup( matched->e_dn );
265                         refs = is_entry_referral( matched )
266                                 ? get_entry_referrals( be, conn, op, matched )
267                                 : NULL;
268                         bdb_cache_return_entry_r (&bdb->bi_cache, matched);
269                         matched = NULL;
270
271                 } else {
272                         refs = referral_rewrite( default_referral,
273                                 NULL, dn, LDAP_SCOPE_DEFAULT );
274                 }
275
276                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
277                         matched_dn, NULL, refs, NULL );
278
279                 ber_bvarray_free( refs );
280                 free( matched_dn );
281
282                 return rc;
283         }
284
285         if ( !manageDSAit && is_entry_referral( e ) ) {
286                 /* parent is a referral, don't allow add */
287                 /* parent is an alias, don't allow add */
288                 BerVarray refs = get_entry_referrals( be,
289                         conn, op, e );
290
291                 Debug( LDAP_DEBUG_TRACE,
292                         "bdb_modify: entry is referral\n",
293                         0, 0, 0 );
294
295                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
296                         e->e_dn, NULL, refs, NULL );
297
298                 ber_bvarray_free( refs );
299                 goto done;
300         }
301         
302         /* Modify the entry */
303         rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
304                 &text, textbuf, textlen );
305
306         if( rc != LDAP_SUCCESS ) {
307                 Debug( LDAP_DEBUG_TRACE,
308                         "bdb_modify: modify failed (%d)\n",
309                         rc, 0, 0 );
310                 switch( rc ) {
311                 case DB_LOCK_DEADLOCK:
312                 case DB_LOCK_NOTGRANTED:
313                         goto retry;
314                 }
315                 goto return_results;
316         }
317
318         /* change the entry itself */
319         rc = bdb_id2entry_update( be, ltid, e );
320         if ( rc != 0 ) {
321                 Debug( LDAP_DEBUG_TRACE,
322                         "bdb_modify: id2entry update failed (%d)\n",
323                         rc, 0, 0 );
324                 switch( rc ) {
325                 case DB_LOCK_DEADLOCK:
326                 case DB_LOCK_NOTGRANTED:
327                         goto retry;
328                 }
329                 text = "entry update failed";
330                 goto return_results;
331         }
332
333         if( bdb->bi_txn ) {
334                 rc = txn_commit( ltid, 0 );
335         }
336         ltid = NULL;
337         op->o_private = NULL;
338
339         if( rc != 0 ) {
340                 Debug( LDAP_DEBUG_TRACE,
341                         "bdb_modify: txn_commit failed: %s (%d)\n",
342                         db_strerror(rc), rc, 0 );
343                 rc = LDAP_OTHER;
344                 text = "commit failed";
345
346         } else {
347                 Debug( LDAP_DEBUG_TRACE,
348                         "bdb_modify: updated id=%08lx dn=\"%s\"\n",
349                         e->e_id, e->e_dn, 0 );
350                 rc = LDAP_SUCCESS;
351                 text = NULL;
352         }
353
354 return_results:
355         send_ldap_result( conn, op, rc,
356                 NULL, text, NULL, NULL );
357
358         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
359                 ldap_pvt_thread_yield();
360                 TXN_CHECKPOINT( bdb->bi_dbenv,
361                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
362         }
363
364 done:
365         if( ltid != NULL ) {
366                 txn_abort( ltid );
367                 op->o_private = NULL;
368         }
369
370         if( e != NULL ) {
371                 bdb_cache_return_entry_w (&bdb->bi_cache, e);
372         }
373         return rc;
374 }