]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
87271e8b06a92d85f78933d57854341376915af0
[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.bv_val );
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.bv_val );
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.bv_val );
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.bv_val );
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         return rc;
151 }
152
153
154 int
155 bdb_modify(
156         BackendDB       *be,
157         Connection      *conn,
158         Operation       *op,
159         const char      *dn,
160         const char      *ndn,
161         Modifications   *modlist )
162 {
163         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
164         int rc;
165         Entry           *matched;
166         Entry           *e;
167         int             manageDSAit = get_manageDSAit( op );
168         const char *text = NULL;
169         char textbuf[SLAP_TEXT_BUFLEN];
170         size_t textlen = sizeof textbuf;
171         DB_TXN  *ltid = NULL;
172         struct bdb_op_info opinfo;
173
174         Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn, 0, 0 );
175
176         if( 0 ) {
177 retry:  /* transaction retry */
178                 Debug(LDAP_DEBUG_TRACE,
179                         "bdb_modify: retrying...\n", 0, 0, 0);
180                 rc = txn_abort( ltid );
181                 ltid = NULL;
182                 op->o_private = NULL;
183                 if( rc != 0 ) {
184                         rc = LDAP_OTHER;
185                         text = "internal error";
186                         goto return_results;
187                 }
188         }
189
190         if( bdb->bi_txn ) {
191                 /* begin transaction */
192                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
193                         bdb->bi_db_opflags );
194                 text = NULL;
195                 if( rc != 0 ) {
196                         Debug( LDAP_DEBUG_TRACE,
197                                 "bdb_modify: txn_begin failed: %s (%d)\n",
198                                 db_strerror(rc), rc, 0 );
199                         rc = LDAP_OTHER;
200                         text = "internal error";
201                         goto return_results;
202                 }
203         }
204
205         opinfo.boi_bdb = be;
206         opinfo.boi_txn = ltid;
207         opinfo.boi_err = 0;
208         op->o_private = &opinfo;
209
210         /* get entry */
211         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
212
213         if ( rc != 0 ) {
214                 Debug( LDAP_DEBUG_TRACE,
215                         "bdb_modify: dn2entry failed (%d)\n",
216                         rc, 0, 0 );
217                 switch( rc ) {
218                 case DB_LOCK_DEADLOCK:
219                 case DB_LOCK_NOTGRANTED:
220                         goto retry;
221                 case DB_NOTFOUND:
222                         break;
223                 default:
224                         rc = LDAP_OTHER;
225                 }
226                 text = "internal error";
227                 goto return_results;
228         }
229
230         /* acquire and lock entry */
231         if ( e == NULL ) {
232                 char* matched_dn = NULL;
233                 struct berval **refs;
234
235                 if ( matched != NULL ) {
236                         matched_dn = ch_strdup( matched->e_dn );
237                         refs = is_entry_referral( matched )
238                                 ? get_entry_referrals( be, conn, op, matched,
239                                         dn, LDAP_SCOPE_DEFAULT )
240                                 : NULL;
241                         bdb_entry_return( be, matched );
242                         matched = NULL;
243
244                 } else {
245                         refs = referral_rewrite( default_referral,
246                                 NULL, dn, LDAP_SCOPE_DEFAULT );
247                 }
248
249                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
250                         matched_dn, NULL, refs, NULL );
251
252                 ber_bvecfree( refs );
253                 free( matched_dn );
254
255                 return rc;
256         }
257
258         if ( !manageDSAit && is_entry_referral( e ) ) {
259                 /* parent is a referral, don't allow add */
260                 /* parent is an alias, don't allow add */
261                 struct berval **refs = get_entry_referrals( be,
262                         conn, op, e, dn, LDAP_SCOPE_DEFAULT );
263
264                 Debug( LDAP_DEBUG_TRACE,
265                         "bdb_modify: entry is referral\n",
266                         0, 0, 0 );
267
268                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
269                         e->e_dn, NULL, refs, NULL );
270
271                 ber_bvecfree( refs );
272                 goto done;
273         }
274         
275         /* Modify the entry */
276         rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
277                 &text, textbuf, textlen );
278
279         if( rc != LDAP_SUCCESS ) {
280                 Debug( LDAP_DEBUG_TRACE,
281                         "bdb_modify: modify failed (%d)\n",
282                         rc, 0, 0 );
283                 switch( rc ) {
284                 case DB_LOCK_DEADLOCK:
285                 case DB_LOCK_NOTGRANTED:
286                         goto retry;
287                 }
288                 goto return_results;
289         }
290
291         /* change the entry itself */
292         rc = bdb_id2entry_update( be, ltid, e );
293         if ( rc != 0 ) {
294                 Debug( LDAP_DEBUG_TRACE,
295                         "bdb_modify: id2entry update failed (%d)\n",
296                         rc, 0, 0 );
297                 switch( rc ) {
298                 case DB_LOCK_DEADLOCK:
299                 case DB_LOCK_NOTGRANTED:
300                         goto retry;
301                 }
302                 text = "entry update failed";
303                 goto return_results;
304         }
305
306         if( bdb->bi_txn ) {
307                 rc = txn_commit( ltid, 0 );
308         }
309         ltid = NULL;
310         op->o_private = NULL;
311
312         if( rc != 0 ) {
313                 Debug( LDAP_DEBUG_TRACE,
314                         "bdb_modify: txn_commit failed: %s (%d)\n",
315                         db_strerror(rc), rc, 0 );
316                 rc = LDAP_OTHER;
317                 text = "commit failed";
318
319         } else {
320                 Debug( LDAP_DEBUG_TRACE,
321                         "bdb_modify: updated id=%08lx dn=\"%s\"\n",
322                         e->e_id, e->e_dn, 0 );
323                 rc = LDAP_SUCCESS;
324                 text = NULL;
325         }
326
327 return_results:
328         send_ldap_result( conn, op, rc,
329                 NULL, text, NULL, NULL );
330
331         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
332                 ldap_pvt_thread_yield();
333                 TXN_CHECKPOINT( bdb->bi_dbenv,
334                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
335         }
336
337 done:
338         if( ltid != NULL ) {
339                 txn_abort( ltid );
340                 op->o_private = NULL;
341         }
342
343         if( e != NULL ) {
344                 bdb_entry_return( be, e );
345         }
346         return rc;
347 }
348
349 static int
350 add_values(
351         Entry   *e,
352         Modification    *mod,
353         char    *dn
354 )
355 {
356         int             i;
357         Attribute       *a;
358
359         /* char *desc = mod->sm_desc->ad_cname.bv_val; */
360         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
361
362         a = attr_find( e->e_attrs, mod->sm_desc );
363
364         /* check if the values we're adding already exist */
365         if ( a != NULL ) {
366                 if( mr == NULL || !mr->smr_match ) {
367                         /* do not allow add of additional attribute
368                                 if no equality rule exists */
369                         return LDAP_INAPPROPRIATE_MATCHING;
370                 }
371
372                 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
373                         int rc;
374                         int j;
375                         const char *text = NULL;
376                         struct berval *asserted;
377
378                         rc = value_normalize( mod->sm_desc,
379                                 SLAP_MR_EQUALITY,
380                                 mod->sm_bvalues[i],
381                                 &asserted,
382                                 &text );
383
384                         if( rc != LDAP_SUCCESS ) return rc;
385
386                         for ( j = 0; a->a_vals[j] != NULL; j++ ) {
387                                 int match;
388                                 int rc = value_match( &match, mod->sm_desc, mr,
389                                         SLAP_MR_VALUE_SYNTAX_MATCH,
390                                         a->a_vals[j], asserted, &text );
391
392                                 if( rc == LDAP_SUCCESS && match == 0 ) {
393                                         ber_bvfree( asserted );
394                                         return LDAP_TYPE_OR_VALUE_EXISTS;
395                                 }
396                         }
397
398                         ber_bvfree( asserted );
399                 }
400         }
401
402         /* no - add them */
403         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
404                 /* this should return result return of attr_merge */
405                 return LDAP_OTHER;
406         }
407
408         return LDAP_SUCCESS;
409 }
410
411 static int
412 delete_values(
413         Entry   *e,
414         Modification    *mod,
415         char    *dn
416 )
417 {
418         int             i, j, k, found;
419         Attribute       *a;
420         char *desc = mod->sm_desc->ad_cname.bv_val;
421         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
422
423         /* delete the entire attribute */
424         if ( mod->sm_bvalues == NULL ) {
425                 Debug( LDAP_DEBUG_TRACE,
426                         "bdb_modify_delete: removing entire attribute %s\n",
427                         desc, 0, 0 );
428                 return attr_delete( &e->e_attrs, mod->sm_desc )
429                         ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
430         }
431
432         if( mr == NULL || !mr->smr_match ) {
433                 /* disallow specific attributes from being deleted if
434                         no equality rule */
435                 return LDAP_INAPPROPRIATE_MATCHING;
436         }
437
438         /* delete specific values - find the attribute first */
439         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
440                 Debug( LDAP_DEBUG_TRACE,
441                         "bdb_modify_delete: could not find attribute %s\n",
442                         desc, 0, 0 );
443                 return LDAP_NO_SUCH_ATTRIBUTE;
444         }
445
446         /* find each value to delete */
447         for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
448                 int rc;
449                 const char *text = NULL;
450
451                 struct berval *asserted;
452
453                 rc = value_normalize( mod->sm_desc,
454                         SLAP_MR_EQUALITY,
455                         mod->sm_bvalues[i],
456                         &asserted,
457                         &text );
458
459                 if( rc != LDAP_SUCCESS ) return rc;
460
461                 found = 0;
462                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
463                         int match;
464                         int rc = value_match( &match, mod->sm_desc, mr,
465                                 SLAP_MR_VALUE_SYNTAX_MATCH,
466                                 a->a_vals[j], asserted, &text );
467
468                         if( rc == LDAP_SUCCESS && match != 0 ) {
469                                 continue;
470                         }
471
472                         /* found a matching value */
473                         found = 1;
474
475                         /* delete it */
476                         ber_bvfree( a->a_vals[j] );
477                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
478                                 a->a_vals[k - 1] = a->a_vals[k];
479                         }
480                         a->a_vals[k - 1] = NULL;
481
482                         break;
483                 }
484
485                 ber_bvfree( asserted );
486
487                 /* looked through them all w/o finding it */
488                 if ( ! found ) {
489                         Debug( LDAP_DEBUG_TRACE,
490                                 "bdb_modify_delete: could not find value for attr %s\n",
491                                 desc, 0, 0 );
492                         return LDAP_NO_SUCH_ATTRIBUTE;
493                 }
494         }
495
496         /* if no values remain, delete the entire attribute */
497         if ( a->a_vals[0] == NULL ) {
498                 Debug( LDAP_DEBUG_TRACE,
499                         "bdb_modify_delete: removing entire attribute %s\n",
500                         desc, 0, 0 );
501                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
502                         return LDAP_NO_SUCH_ATTRIBUTE;
503                 }
504         }
505
506         return LDAP_SUCCESS;
507 }
508
509 static int
510 replace_values(
511         Entry   *e,
512         Modification    *mod,
513         char    *dn
514 )
515 {
516         int rc = attr_delete( &e->e_attrs, mod->sm_desc );
517
518         if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
519                 return rc;
520         }
521
522         if ( mod->sm_bvalues != NULL &&
523                 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
524         {
525                 return LDAP_OTHER;
526         }
527
528         return LDAP_SUCCESS;
529 }