]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
Experiment with busy loop protection...
[openldap] / servers / slapd / back-bdb / modify.c
1 /* modify.c - bdb backend modify routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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 static int add_values( Entry *e, Modification *mod, char *dn );
18 static int delete_values( Entry *e, Modification *mod, char *dn );
19 static int replace_values( Entry *e, Modification *mod, char *dn );
20
21 int bdb_modify_internal(
22         BackendDB *be,
23         Connection *conn,
24         Operation *op,
25         DB_TXN *tid,
26         Modifications *modlist,
27         Entry *e,
28         const char **text,
29         char *textbuf,
30         size_t textlen )
31 {
32         int rc, err;
33         Modification    *mod;
34         Modifications   *ml;
35         Attribute       *save_attrs;
36
37         Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
38                 e->e_id, e->e_dn, 0);
39
40         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
41                 return LDAP_INSUFFICIENT_ACCESS;
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->sml_next ) {
48                 mod = &ml->sml_mod;
49
50                 switch ( mod->sm_op ) {
51                 case LDAP_MOD_ADD:
52                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
53                         err = add_values( e, mod, op->o_ndn );
54
55                         if( err != LDAP_SUCCESS ) {
56                                 *text = "modify: add values failed";
57                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
58                                         err, *text, 0);
59                         }
60                         break;
61
62                 case LDAP_MOD_DELETE:
63                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
64                         err = delete_values( e, mod, op->o_ndn );
65                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
66                         if( err != LDAP_SUCCESS ) {
67                                 *text = "modify: delete values failed";
68                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
69                                         err, *text, 0);
70                         }
71                         break;
72
73                 case LDAP_MOD_REPLACE:
74                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
75                         err = replace_values( e, mod, op->o_ndn );
76                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
77                         if( err != LDAP_SUCCESS ) {
78                                 *text = "modify: replace values failed";
79                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
80                                         err, *text, 0);
81                         }
82                         break;
83
84                 case SLAP_MOD_SOFTADD:
85                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
86                         /* Avoid problems in index_add_mods()
87                          * We need to add index if necessary.
88                          */
89                         mod->sm_op = LDAP_MOD_ADD;
90                         err = add_values( e, mod, op->o_ndn );
91
92                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
93                                 err = LDAP_SUCCESS;
94                         }
95
96                         if( err != LDAP_SUCCESS ) {
97                                 *text = "modify: (soft)add values failed";
98                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
99                                         err, *text, 0);
100                         }
101                         break;
102
103                 default:
104                         Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
105                                 mod->sm_op, 0, 0);
106                         *text = "Invalid modify operation";
107                         err = LDAP_OTHER;
108                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
109                                 err, *text, 0);
110                 }
111
112                 if ( err != LDAP_SUCCESS ) {
113                         attrs_free( e->e_attrs );
114                         e->e_attrs = save_attrs;
115                         /* unlock entry, delete from cache */
116                         return err; 
117                 }
118         }
119
120         /* check that the entry still obeys the schema */
121         rc = entry_schema_check( e, save_attrs, text, textbuf, textlen );
122         if ( rc != LDAP_SUCCESS ) {
123                 attrs_free( e->e_attrs );
124                 e->e_attrs = save_attrs;
125                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
126                         *text, 0, 0 );
127                 return rc;
128         }
129
130         /* delete indices for old attributes */
131         rc = bdb_index_entry_del( be, tid, e, save_attrs);
132         if ( rc != LDAP_SUCCESS ) {
133                 attrs_free( e->e_attrs );
134                 e->e_attrs = save_attrs;
135                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
136                         0, 0, 0 );
137                 return rc;
138         }
139
140         /* add indices for new attributes */
141         rc = bdb_index_entry_add( be, tid, e, e->e_attrs); 
142         if ( rc != LDAP_SUCCESS ) {
143                 attrs_free( e->e_attrs );
144                 e->e_attrs = save_attrs;
145                 Debug( LDAP_DEBUG_ANY, "entry index add failed!\n",
146                         0, 0, 0 );
147                 return rc;
148         }
149
150 #ifndef BDB_USE_BINARY_RW
151         /* cannot free individual elements of the entry */
152         attrs_free( save_attrs );
153 #endif
154         return rc;
155 }
156
157
158 int
159 bdb_modify(
160         BackendDB       *be,
161         Connection      *conn,
162         Operation       *op,
163         const char      *dn,
164         const char      *ndn,
165         Modifications   *modlist )
166 {
167         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
168         int rc;
169         Entry           *matched;
170         Entry           *e;
171         int             manageDSAit = get_manageDSAit( op );
172         const char *text = NULL;
173         char textbuf[SLAP_TEXT_BUFLEN];
174         size_t textlen = sizeof textbuf;
175         DB_TXN  *ltid;
176         struct bdb_op_info opinfo;
177
178         Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn, 0, 0 );
179
180         if (0) {
181 retry:  /* transaction retry */
182                 Debug(LDAP_DEBUG_TRACE,
183                         "bdb_modify: retrying...\n", 0, 0, 0);
184                 rc = txn_abort( ltid );
185                 ltid = NULL;
186                 op->o_private = NULL;
187                 if( rc != 0 ) {
188                         rc = LDAP_OTHER;
189                         text = "internal error";
190                         goto return_results;
191                 }
192         }
193
194         /* begin transaction */
195         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
196         text = NULL;
197         if( rc != 0 ) {
198                 Debug( LDAP_DEBUG_TRACE,
199                         "bdb_modify: txn_begin failed: %s (%d)\n",
200                         db_strerror(rc), rc, 0 );
201                 rc = LDAP_OTHER;
202                 text = "internal error";
203                 goto return_results;
204         }
205
206         opinfo.boi_bdb = be;
207         opinfo.boi_txn = ltid;
208         opinfo.boi_err = 0;
209         op->o_private = &opinfo;
210
211         /* get entry */
212         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
213
214         if ( rc != 0 ) {
215                 Debug( LDAP_DEBUG_TRACE,
216                         "bdb_modify: dn2entry failed (%d)\n",
217                         rc, 0, 0 );
218                 switch( rc ) {
219                 case DB_LOCK_DEADLOCK:
220                 case DB_LOCK_NOTGRANTED:
221                         goto retry;
222                 case DB_NOTFOUND:
223                         break;
224                 default:
225                         rc = LDAP_OTHER;
226                 }
227                 text = "internal error";
228                 goto return_results;
229         }
230
231         /* acquire and lock entry */
232         if ( e == NULL ) {
233                 char* matched_dn = NULL;
234                 struct berval **refs = NULL;
235
236                 if ( matched != NULL ) {
237                         matched_dn = ch_strdup( matched->e_dn );
238                         refs = is_entry_referral( matched )
239                                 ? get_entry_referrals( be, conn, op, matched )
240                                 : NULL;
241                         bdb_entry_return( be, matched );
242                         matched = NULL;
243
244                 } else {
245                         refs = default_referral;
246                 }
247
248                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
249                         matched_dn, NULL, refs, NULL );
250
251                 if ( matched != NULL ) {
252                         ber_bvecfree( refs );
253                         free( matched_dn );
254                 }
255
256                 return rc;
257         }
258
259         if ( !manageDSAit && is_entry_referral( e ) ) {
260                 /* parent is a referral, don't allow add */
261                 /* parent is an alias, don't allow add */
262                 struct berval **refs = get_entry_referrals( be,
263                         conn, op, e );
264
265                 Debug( LDAP_DEBUG_TRACE,
266                         "bdb_modify: entry is referral\n",
267                         0, 0, 0 );
268
269                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
270                         e->e_dn, NULL, refs, NULL );
271
272                 ber_bvecfree( refs );
273                 goto done;
274         }
275         
276         /* Modify the entry */
277         rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
278                 &text, textbuf, textlen );
279
280         if( rc != LDAP_SUCCESS ) {
281                 Debug( LDAP_DEBUG_TRACE,
282                         "bdb_modify: modify failed (%d)\n",
283                         rc, 0, 0 );
284                 switch( rc ) {
285                 case DB_LOCK_DEADLOCK:
286                 case DB_LOCK_NOTGRANTED:
287                         goto retry;
288                 }
289                 goto return_results;
290         }
291
292         /* change the entry itself */
293         rc = bdb_id2entry_update( be, ltid, e );
294         if ( rc != 0 ) {
295                 Debug( LDAP_DEBUG_TRACE,
296                         "bdb_modify: id2entry update failed (%d)\n",
297                         rc, 0, 0 );
298                 switch( rc ) {
299                 case DB_LOCK_DEADLOCK:
300                 case DB_LOCK_NOTGRANTED:
301                         goto retry;
302                 }
303                 text = "entry update failed";
304                 goto return_results;
305         }
306
307         rc = txn_commit( ltid, 0 );
308         ltid = NULL;
309         op->o_private = NULL;
310
311         if( rc != 0 ) {
312                 Debug( LDAP_DEBUG_TRACE,
313                         "bdb_modify: txn_commit failed: %s (%d)\n",
314                         db_strerror(rc), rc, 0 );
315                 rc = LDAP_OTHER;
316                 text = "commit failed";
317         } else {
318                 Debug( LDAP_DEBUG_TRACE,
319                         "bdb_modify: updated id=%08x dn=\"%s\"\n",
320                         e->e_id, e->e_dn, 0 );
321                 rc = LDAP_SUCCESS;
322                 text = NULL;
323         }
324
325 return_results:
326         send_ldap_result( conn, op, rc,
327                 NULL, text, NULL, NULL );
328
329         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
330                 ldap_pvt_thread_yield();
331                 txn_checkpoint( bdb->bi_dbenv,
332                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
333         }
334
335 done:
336         if( ltid != NULL ) {
337                 txn_abort( ltid );
338                 op->o_private = NULL;
339         }
340
341         if( e != NULL ) {
342                 bdb_entry_return( be, e );
343         }
344         return rc;
345 }
346
347 static int
348 add_values(
349         Entry   *e,
350         Modification    *mod,
351         char    *dn
352 )
353 {
354         int             i;
355         Attribute       *a;
356
357         /* char *desc = mod->sm_desc->ad_cname->bv_val; */
358         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
359
360         a = attr_find( e->e_attrs, mod->sm_desc );
361
362         /* check if the values we're adding already exist */
363         if ( a != NULL ) {
364                 if( mr == NULL || !mr->smr_match ) {
365                         /* do not allow add of additional attribute
366                                 if no equality rule exists */
367                         return LDAP_INAPPROPRIATE_MATCHING;
368                 }
369
370                 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
371                         int rc;
372                         int j;
373                         const char *text = NULL;
374                         struct berval *asserted;
375
376                         rc = value_normalize( mod->sm_desc,
377                                 SLAP_MR_EQUALITY,
378                                 mod->sm_bvalues[i],
379                                 &asserted,
380                                 &text );
381
382                         if( rc != LDAP_SUCCESS ) return rc;
383
384                         for ( j = 0; a->a_vals[j] != NULL; j++ ) {
385                                 int match;
386                                 int rc = value_match( &match, mod->sm_desc, mr,
387                                         SLAP_MR_MODIFY_MATCHING,
388                                         a->a_vals[j], asserted, &text );
389
390                                 if( rc == LDAP_SUCCESS && match == 0 ) {
391                                         ber_bvfree( asserted );
392                                         return LDAP_TYPE_OR_VALUE_EXISTS;
393                                 }
394                         }
395
396                         ber_bvfree( asserted );
397                 }
398         }
399
400         /* no - add them */
401         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
402                 /* this should return result return of attr_merge */
403                 return LDAP_OTHER;
404         }
405
406         return LDAP_SUCCESS;
407 }
408
409 static int
410 delete_values(
411         Entry   *e,
412         Modification    *mod,
413         char    *dn
414 )
415 {
416         int             i, j, k, found;
417         Attribute       *a;
418         char *desc = mod->sm_desc->ad_cname->bv_val;
419         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
420
421         /* delete the entire attribute */
422         if ( mod->sm_bvalues == NULL ) {
423                 Debug( LDAP_DEBUG_TRACE,
424                         "bdb_modify_delete: removing entire attribute %s\n",
425                         desc, 0, 0 );
426                 return attr_delete( &e->e_attrs, mod->sm_desc )
427                         ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
428         }
429
430         if( mr == NULL || !mr->smr_match ) {
431                 /* disallow specific attributes from being deleted if
432                         no equality rule */
433                 return LDAP_INAPPROPRIATE_MATCHING;
434         }
435
436         /* delete specific values - find the attribute first */
437         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
438                 Debug( LDAP_DEBUG_TRACE,
439                         "bdb_modify_delete: could not find attribute %s\n",
440                         desc, 0, 0 );
441                 return LDAP_NO_SUCH_ATTRIBUTE;
442         }
443
444         /* find each value to delete */
445         for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
446                 int rc;
447                 const char *text = NULL;
448
449                 struct berval *asserted;
450
451                 rc = value_normalize( mod->sm_desc,
452                         SLAP_MR_EQUALITY,
453                         mod->sm_bvalues[i],
454                         &asserted,
455                         &text );
456
457                 if( rc != LDAP_SUCCESS ) return rc;
458
459                 found = 0;
460                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
461                         int match;
462                         int rc = value_match( &match, mod->sm_desc, mr,
463                                 SLAP_MR_MODIFY_MATCHING,
464                                 a->a_vals[j], asserted, &text );
465
466                         if( rc == LDAP_SUCCESS && match != 0 ) {
467                                 continue;
468                         }
469
470                         /* found a matching value */
471                         found = 1;
472
473                         /* delete it */
474                         ber_bvfree( a->a_vals[j] );
475                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
476                                 a->a_vals[k - 1] = a->a_vals[k];
477                         }
478                         a->a_vals[k - 1] = NULL;
479
480                         break;
481                 }
482
483                 ber_bvfree( asserted );
484
485                 /* looked through them all w/o finding it */
486                 if ( ! found ) {
487                         Debug( LDAP_DEBUG_TRACE,
488                                 "bdb_modify_delete: could not find value for attr %s\n",
489                                 desc, 0, 0 );
490                         return LDAP_NO_SUCH_ATTRIBUTE;
491                 }
492         }
493
494         /* if no values remain, delete the entire attribute */
495         if ( a->a_vals[0] == NULL ) {
496                 Debug( LDAP_DEBUG_TRACE,
497                         "bdb_modify_delete: removing entire attribute %s\n",
498                         desc, 0, 0 );
499                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
500                         return LDAP_NO_SUCH_ATTRIBUTE;
501                 }
502         }
503
504         return LDAP_SUCCESS;
505 }
506
507 static int
508 replace_values(
509         Entry   *e,
510         Modification    *mod,
511         char    *dn
512 )
513 {
514         int rc = attr_delete( &e->e_attrs, mod->sm_desc );
515
516         if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
517                 return rc;
518         }
519
520         if ( mod->sm_bvalues != NULL &&
521                 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
522         {
523                 return LDAP_OTHER;
524         }
525
526         return LDAP_SUCCESS;
527 }