]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
07b8265283f9d5c50b15fd41bcf8813eac4600a3
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23 #include "external.h"
24
25 int
26 bdb_add(Operation *op, SlapReply *rs )
27 {
28         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
29         struct berval   pdn;
30         Entry           *p = NULL;
31         EntryInfo       *ei;
32         char textbuf[SLAP_TEXT_BUFLEN];
33         size_t textlen = sizeof textbuf;
34         AttributeDescription *children = slap_schema.si_ad_children;
35         AttributeDescription *entry = slap_schema.si_ad_entry;
36         DB_TXN          *ltid = NULL, *lt2;
37         struct bdb_op_info opinfo;
38 #ifdef BDB_SUBENTRIES
39         int subentry;
40 #endif
41         u_int32_t       locker = 0;
42         DB_LOCK         lock;
43
44         int             num_retries = 0;
45
46         Operation* ps_list;
47         int             rc;
48         EntryInfo       *suffix_ei = NULL;
49         Entry           *ctxcsn_e;
50         int                     ctxcsn_added = 0;
51
52         LDAPControl **postread_ctrl = NULL;
53         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
54         int num_ctrls = 0;
55
56         Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n",
57                 op->oq_add.rs_e->e_name.bv_val, 0, 0);
58
59         ctrls[num_ctrls] = 0;
60
61         /* check entry's schema */
62         rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e,
63                 NULL, &rs->sr_text, textbuf, textlen );
64         if ( rs->sr_err != LDAP_SUCCESS ) {
65                 Debug( LDAP_DEBUG_TRACE,
66                         "bdb_add: entry failed schema check: %s (%d)\n",
67                         rs->sr_text, rs->sr_err, 0 );
68                 goto return_results;
69         }
70
71 #ifdef BDB_SUBENTRIES
72         subentry = is_entry_subentry( op->oq_add.rs_e );
73 #endif
74
75         /*
76          * acquire an ID outside of the operation transaction
77          * to avoid serializing adds.
78          */
79         rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
80         if( rs->sr_err != 0 ) {
81                 Debug( LDAP_DEBUG_TRACE,
82                         "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
83                 rs->sr_err = LDAP_OTHER;
84                 rs->sr_text = "internal error";
85                 goto return_results;
86         }
87
88         if( 0 ) {
89 retry:  /* transaction retry */
90                 if( p ) {
91                         /* free parent and reader lock */
92                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
93                         p = NULL;
94                 }
95                 rs->sr_err = TXN_ABORT( ltid );
96                 ltid = NULL;
97                 op->o_private = NULL;
98                 op->o_do_not_cache = opinfo.boi_acl_cache;
99                 if( rs->sr_err != 0 ) {
100                         rs->sr_err = LDAP_OTHER;
101                         rs->sr_text = "internal error";
102                         goto return_results;
103                 }
104                 ldap_pvt_thread_yield();
105                 bdb_trans_backoff( ++num_retries );
106         }
107
108         /* begin transaction */
109         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
110                 bdb->bi_db_opflags );
111         rs->sr_text = NULL;
112         if( rs->sr_err != 0 ) {
113                 Debug( LDAP_DEBUG_TRACE,
114                         "bdb_add: txn_begin failed: %s (%d)\n",
115                         db_strerror(rs->sr_err), rs->sr_err, 0 );
116                 rs->sr_err = LDAP_OTHER;
117                 rs->sr_text = "internal error";
118                 goto return_results;
119         }
120
121         locker = TXN_ID ( ltid );
122
123         opinfo.boi_bdb = op->o_bd;
124         opinfo.boi_txn = ltid;
125         opinfo.boi_locker = locker;
126         opinfo.boi_err = 0;
127         opinfo.boi_acl_cache = op->o_do_not_cache;
128         op->o_private = &opinfo;
129         
130         /*
131          * Get the parent dn and see if the corresponding entry exists.
132          */
133         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
134                 pdn = slap_empty_bv;
135         } else {
136                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
137         }
138
139         /* get entry or parent */
140         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
141                 1, locker, &lock );
142         switch( rs->sr_err ) {
143         case 0:
144                 rs->sr_err = LDAP_ALREADY_EXISTS;
145                 goto return_results;
146         case DB_NOTFOUND:
147                 break;
148         case DB_LOCK_DEADLOCK:
149         case DB_LOCK_NOTGRANTED:
150                 goto retry;
151         case LDAP_BUSY:
152                 rs->sr_text = "ldap server busy";
153                 goto return_results;
154         default:
155                 rs->sr_err = LDAP_OTHER;
156                 rs->sr_text = "internal error";
157                 goto return_results;
158         }
159
160         p = ei->bei_e;
161         if ( p ) {
162                 if ( !bvmatch( &pdn, &p->e_nname ) ) {
163                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
164                                 op->o_tmpmemctx );
165                         rs->sr_ref = is_entry_referral( p )
166                                 ? get_entry_referrals( op, p )
167                                 : NULL;
168                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
169                         p = NULL;
170                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
171                                 0, 0, 0 );
172
173                         rs->sr_err = LDAP_REFERRAL;
174                         send_ldap_result( op, rs );
175
176                         ber_bvarray_free( rs->sr_ref );
177                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
178                         rs->sr_ref = NULL;
179                         rs->sr_matched = NULL;
180
181                         goto done;
182                 }
183
184                 rs->sr_err = access_allowed( op, p,
185                         children, NULL, ACL_WRITE, NULL );
186
187                 if ( ! rs->sr_err ) {
188                         switch( opinfo.boi_err ) {
189                         case DB_LOCK_DEADLOCK:
190                         case DB_LOCK_NOTGRANTED:
191                                 goto retry;
192                         }
193
194                         Debug( LDAP_DEBUG_TRACE,
195                                 "bdb_add: no write access to parent\n", 0, 0, 0 );
196                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
197                         rs->sr_text = "no write access to parent";
198                         goto return_results;;
199                 }
200
201 #ifdef BDB_SUBENTRIES
202                 if ( is_entry_subentry( p ) ) {
203                         /* parent is a subentry, don't allow add */
204                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
205                                 0, 0, 0 );
206                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
207                         rs->sr_text = "parent is a subentry";
208                         goto return_results;;
209                 }
210 #endif
211                 if ( is_entry_alias( p ) ) {
212                         /* parent is an alias, don't allow add */
213                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
214                                 0, 0, 0 );
215                         rs->sr_err = LDAP_ALIAS_PROBLEM;
216                         rs->sr_text = "parent is an alias";
217                         goto return_results;;
218                 }
219
220                 if ( is_entry_referral( p ) ) {
221                         /* parent is a referral, don't allow add */
222                         rs->sr_matched = p->e_name.bv_val;
223                         rs->sr_ref = get_entry_referrals( op, p );
224
225                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
226                                 0, 0, 0 );
227
228                         rs->sr_err = LDAP_REFERRAL;
229                         send_ldap_result( op, rs );
230
231                         ber_bvarray_free( rs->sr_ref );
232                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
233                         rs->sr_ref = NULL;
234                         rs->sr_matched = NULL;
235                         p = NULL;
236                         goto done;
237                 }
238
239 #ifdef BDB_SUBENTRIES
240                 if ( subentry ) {
241                         /* FIXME: */
242                         /* parent must be an administrative point of the required kind */
243                 }
244 #endif
245
246                 /* free parent and reader lock */
247                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
248                 p = NULL;
249
250         } else {
251                 /*
252                  * no parent!
253                  *  if not attempting to add entry at suffix or with parent ""
254                  */
255                 if ((( !be_isroot( op ) && !be_shadow_update(op) )
256                         || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
257                 {
258                         Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
259                                 pdn.bv_len == 0 ? "suffix" : "entry at root",
260                                 0, 0 );
261                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
262                         goto return_results;
263                 }
264         }
265
266         if ( get_assert( op ) &&
267                 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
268                         != LDAP_COMPARE_TRUE ))
269         {
270                 rs->sr_err = LDAP_ASSERTION_FAILED;
271                 goto return_results;
272         }
273
274         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
275                 entry, NULL, ACL_WRITE, NULL );
276
277         if ( ! rs->sr_err ) {
278                 switch( opinfo.boi_err ) {
279                 case DB_LOCK_DEADLOCK:
280                 case DB_LOCK_NOTGRANTED:
281                         goto retry;
282                 }
283
284                 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
285                         0, 0, 0 );
286                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
287                 rs->sr_text = "no write access to entry";
288                 goto return_results;;
289         }
290
291         /* nested transaction */
292         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
293                 bdb->bi_db_opflags );
294         rs->sr_text = NULL;
295         if( rs->sr_err != 0 ) {
296                 Debug( LDAP_DEBUG_TRACE,
297                         "bdb_add: txn_begin(2) failed: %s (%d)\n",
298                         db_strerror(rs->sr_err), rs->sr_err, 0 );
299                 rs->sr_err = LDAP_OTHER;
300                 rs->sr_text = "internal error";
301                 goto return_results;
302         }
303
304         /* dn2id index */
305         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
306         if ( rs->sr_err != 0 ) {
307                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
308                         db_strerror(rs->sr_err), rs->sr_err, 0 );
309
310                 switch( rs->sr_err ) {
311                 case DB_LOCK_DEADLOCK:
312                 case DB_LOCK_NOTGRANTED:
313                         goto retry;
314                 case DB_KEYEXIST:
315                         rs->sr_err = LDAP_ALREADY_EXISTS;
316                         break;
317                 default:
318                         rs->sr_err = LDAP_OTHER;
319                 }
320                 goto return_results;
321         }
322
323         /* id2entry index */
324         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
325         if ( rs->sr_err != 0 ) {
326                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
327                         0, 0, 0 );
328                 switch( rs->sr_err ) {
329                 case DB_LOCK_DEADLOCK:
330                 case DB_LOCK_NOTGRANTED:
331                         goto retry;
332                 default:
333                         rs->sr_err = LDAP_OTHER;
334                 }
335                 rs->sr_text = "entry store failed";
336                 goto return_results;
337         }
338
339         /* attribute indexes */
340         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
341         if ( rs->sr_err != LDAP_SUCCESS ) {
342                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
343                         0, 0, 0 );
344                 switch( rs->sr_err ) {
345                 case DB_LOCK_DEADLOCK:
346                 case DB_LOCK_NOTGRANTED:
347                         goto retry;
348                 default:
349                         rs->sr_err = LDAP_OTHER;
350                 }
351                 rs->sr_text = "index generation failed";
352                 goto return_results;
353         }
354         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
355                 rs->sr_err = LDAP_OTHER;
356                 rs->sr_text = "txn_commit(2) failed";
357                 goto return_results;
358         }
359
360         if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
361                 rc = bdb_csn_commit( op, rs, ltid, ei, &suffix_ei,
362                         &ctxcsn_e, &ctxcsn_added, locker );
363                 switch ( rc ) {
364                 case BDB_CSN_ABORT :
365                         goto return_results;
366                 case BDB_CSN_RETRY :
367                         goto retry;
368                 }
369         }
370
371         /* post-read */
372         if( op->o_postread ) {
373                 if( postread_ctrl == NULL ) {
374                         postread_ctrl = &ctrls[num_ctrls++];
375                         ctrls[num_ctrls] = NULL;
376                 }
377                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
378                         &slap_post_read_bv, postread_ctrl ) )
379                 {
380                         Debug( LDAP_DEBUG_TRACE,
381                                 "<=- bdb_add: post-read failed!\n", 0, 0, 0 );
382                         goto return_results;
383                 }
384         }
385
386         if ( op->o_noop ) {
387                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
388                         rs->sr_text = "txn_abort (no-op) failed";
389                 } else {
390                         rs->sr_err = LDAP_NO_OPERATION;
391                         goto return_results;
392                 }
393
394         } else {
395                 struct berval nrdn;
396                 Entry *e = entry_dup( op->ora_e );
397
398                 if (pdn.bv_len) {
399                         nrdn.bv_val = e->e_nname.bv_val;
400                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
401                 } else {
402                         nrdn = e->e_nname;
403                 }
404
405                 bdb_cache_add( bdb, ei, e, &nrdn, locker );
406
407                 if ( suffix_ei == NULL ) {
408                         suffix_ei = BEI(e);
409                 }
410
411                 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
412                         if ( ctxcsn_added ) {
413                                 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
414                                         (struct berval *)&slap_ldapsync_cn_bv, locker );
415                         }
416                 }
417
418                 if ( rs->sr_err == LDAP_SUCCESS && !op->o_no_psearch ) {
419                         ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
420                         assert( BEI(e) );
421                         LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
422                                 rc = bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_ADD );
423                                 if ( rc ) {
424                                         Debug( LDAP_DEBUG_TRACE,
425                                                 "bdb_add: persistent search failed (%d,%d)\n",
426                                                 rc, rs->sr_err, 0 );
427                                 }
428                         }
429                         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
430                 }
431
432                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
433                         rs->sr_text = "txn_commit failed";
434                 } else {
435                         rs->sr_err = LDAP_SUCCESS;
436                 }
437         }
438
439         ltid = NULL;
440         op->o_private = NULL;
441
442         if ( rs->sr_err != LDAP_SUCCESS ) {
443                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
444                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
445                 rs->sr_err = LDAP_OTHER;
446                 goto return_results;
447         }
448
449         Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
450                 op->o_noop ? " (no-op)" : "",
451                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
452
453         rs->sr_text = NULL;
454         if( num_ctrls ) rs->sr_ctrls = ctrls;
455
456 return_results:
457         send_ldap_result( op, rs );
458
459         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
460                 ldap_pvt_thread_yield();
461                 TXN_CHECKPOINT( bdb->bi_dbenv,
462                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
463         }
464
465 done:
466         if( ltid != NULL ) {
467                 TXN_ABORT( ltid );
468                 op->o_private = NULL;
469         }
470
471         if( postread_ctrl != NULL ) {
472                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
473                 slap_sl_free( *postread_ctrl, &op->o_tmpmemctx );
474         }
475         return rs->sr_err;
476 }