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