1 /* add.c - ldap BerkeleyDB back-end add routine */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 bdb_add(Operation *op, SlapReply *rs )
19 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
23 char textbuf[SLAP_TEXT_BUFLEN];
24 size_t textlen = sizeof textbuf;
25 AttributeDescription *children = slap_schema.si_ad_children;
26 AttributeDescription *entry = slap_schema.si_ad_entry;
27 DB_TXN *ltid = NULL, *lt2;
28 struct bdb_op_info opinfo;
47 LDAP_LOG ( OPERATION, ARGS, "==> bdb_add: %s\n", op->oq_add.rs_e->e_name.bv_val, 0, 0 );
49 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", op->oq_add.rs_e->e_name.bv_val, 0, 0);
52 /* check entry's schema */
53 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
54 if ( rs->sr_err != LDAP_SUCCESS ) {
56 LDAP_LOG ( OPERATION, ERR,
57 "bdb_add: entry failed schema check: %s (%d)\n", rs->sr_text, rs->sr_err, 0 );
59 Debug( LDAP_DEBUG_TRACE,
60 "bdb_add: entry failed schema check: %s (%d)\n",
61 rs->sr_text, rs->sr_err, 0 );
67 subentry = is_entry_subentry( op->oq_add.rs_e );
71 * acquire an ID outside of the operation transaction
72 * to avoid serializing adds.
74 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
75 if( rs->sr_err != 0 ) {
77 LDAP_LOG ( OPERATION, ERR,
78 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
80 Debug( LDAP_DEBUG_TRACE,
81 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
83 rs->sr_err = LDAP_OTHER;
84 rs->sr_text = "internal error";
89 retry: /* transaction retry */
91 /* free parent and reader lock */
92 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
95 rs->sr_err = TXN_ABORT( ltid );
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";
104 bdb_trans_backoff( ++num_retries );
105 ldap_pvt_thread_yield();
108 /* begin transaction */
109 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
110 bdb->bi_db_opflags );
112 if( rs->sr_err != 0 ) {
114 LDAP_LOG ( OPERATION, ERR,
115 "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
117 Debug( LDAP_DEBUG_TRACE,
118 "bdb_add: txn_begin failed: %s (%d)\n",
119 db_strerror(rs->sr_err), rs->sr_err, 0 );
121 rs->sr_err = LDAP_OTHER;
122 rs->sr_text = "internal error";
126 locker = TXN_ID ( ltid );
128 opinfo.boi_bdb = op->o_bd;
129 opinfo.boi_txn = ltid;
130 opinfo.boi_locker = locker;
132 opinfo.boi_acl_cache = op->o_do_not_cache;
133 op->o_private = &opinfo;
136 * Get the parent dn and see if the corresponding entry exists.
137 * If the parent does not exist, only allow the "root" user to
140 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
143 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
146 /* get entry or parent */
147 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
149 switch( rs->sr_err ) {
151 rs->sr_err = LDAP_ALREADY_EXISTS;
155 case DB_LOCK_DEADLOCK:
156 case DB_LOCK_NOTGRANTED:
159 rs->sr_text = "ldap server busy";
162 rs->sr_err = LDAP_OTHER;
163 rs->sr_text = "internal error";
169 if ( !bvmatch( &pdn, &p->e_nname ) ) {
170 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
172 rs->sr_ref = is_entry_referral( p )
173 ? get_entry_referrals( op, p )
175 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
178 LDAP_LOG ( OPERATION, DETAIL1,
179 "bdb_add: parent does not exist\n", 0, 0, 0 );
181 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
185 rs->sr_err = LDAP_REFERRAL;
186 send_ldap_result( op, rs );
188 ber_bvarray_free( rs->sr_ref );
189 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
191 rs->sr_matched = NULL;
196 rs->sr_err = access_allowed( op, p,
197 children, NULL, ACL_WRITE, NULL );
199 if ( ! rs->sr_err ) {
200 switch( opinfo.boi_err ) {
201 case DB_LOCK_DEADLOCK:
202 case DB_LOCK_NOTGRANTED:
207 LDAP_LOG ( OPERATION, DETAIL1,
208 "bdb_add: no write access to parent\n", 0, 0, 0 );
210 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
213 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
214 rs->sr_text = "no write access to parent";
215 goto return_results;;
218 #ifdef BDB_SUBENTRIES
219 if ( is_entry_subentry( p ) ) {
220 /* parent is a subentry, don't allow add */
222 LDAP_LOG ( OPERATION, DETAIL1,
223 "bdb_add: parent is subentry\n", 0, 0, 0 );
225 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
228 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
229 rs->sr_text = "parent is a subentry";
230 goto return_results;;
234 if ( is_entry_alias( p ) ) {
235 /* parent is an alias, don't allow add */
237 LDAP_LOG ( OPERATION, DETAIL1,
238 "bdb_add: parent is alias\n", 0, 0, 0 );
240 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
243 rs->sr_err = LDAP_ALIAS_PROBLEM;
244 rs->sr_text = "parent is an alias";
245 goto return_results;;
249 if ( is_entry_referral( p ) ) {
250 /* parent is a referral, don't allow add */
251 rs->sr_matched = p->e_name.bv_val;
252 rs->sr_ref = get_entry_referrals( op, p );
255 LDAP_LOG ( OPERATION, DETAIL1,
256 "bdb_add: parent is referral\n", 0, 0, 0 );
258 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
262 rs->sr_err = LDAP_REFERRAL;
263 send_ldap_result( op, rs );
265 ber_bvarray_free( rs->sr_ref );
266 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
268 rs->sr_matched = NULL;
273 #ifdef BDB_SUBENTRIES
276 /* parent must be an administrative point of the required kind */
280 /* free parent and reader lock */
281 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
287 * must be adding entry at suffix or with parent ""
289 if ( !be_isroot( op->o_bd, &op->o_ndn )) {
290 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
291 || be_isupdate( op->o_bd, &op->o_ndn ) )
293 p = (Entry *)&slap_entry_root;
295 /* check parent for "children" acl */
296 rs->sr_err = access_allowed( op, p,
297 children, NULL, ACL_WRITE, NULL );
301 if ( ! rs->sr_err ) {
302 switch( opinfo.boi_err ) {
303 case DB_LOCK_DEADLOCK:
304 case DB_LOCK_NOTGRANTED:
309 LDAP_LOG ( OPERATION, DETAIL1,
310 "bdb_add: no write access to parent\n", 0, 0, 0 );
312 Debug( LDAP_DEBUG_TRACE,
313 "bdb_add: no write access to parent\n",
316 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
317 rs->sr_text = "no write access to parent";
321 } else if ( !is_entry_glue( op->oq_add.rs_e )) {
326 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
327 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
329 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
330 pdn.bv_len == 0 ? "suffix" : "entry at root",
333 rs->sr_err = LDAP_NO_SUCH_OBJECT;
338 #ifdef BDB_SUBENTRIES
341 LDAP_LOG ( OPERATION, DETAIL1,
342 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
344 Debug( LDAP_DEBUG_TRACE,
345 "bdb_add: no parent, cannot add subentry\n",
348 rs->sr_err = LDAP_NO_SUCH_OBJECT;
349 rs->sr_text = "no parent, cannot add subentry";
355 if ( get_assert( op ) &&
356 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
357 != LDAP_COMPARE_TRUE ))
359 rs->sr_err = LDAP_ASSERTION_FAILED;
363 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
364 entry, NULL, ACL_WRITE, NULL );
366 if ( ! rs->sr_err ) {
367 switch( opinfo.boi_err ) {
368 case DB_LOCK_DEADLOCK:
369 case DB_LOCK_NOTGRANTED:
374 LDAP_LOG ( OPERATION, DETAIL1,
375 "bdb_add: no write access to entry\n", 0, 0, 0 );
377 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
380 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
381 rs->sr_text = "no write access to entry";
382 goto return_results;;
385 /* nested transaction */
386 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
387 bdb->bi_db_opflags );
389 if( rs->sr_err != 0 ) {
391 LDAP_LOG ( OPERATION, ERR,
392 "bdb_add: txn_begin(2) failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
394 Debug( LDAP_DEBUG_TRACE,
395 "bdb_add: txn_begin(2) failed: %s (%d)\n",
396 db_strerror(rs->sr_err), rs->sr_err, 0 );
398 rs->sr_err = LDAP_OTHER;
399 rs->sr_text = "internal error";
404 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
405 if ( rs->sr_err != 0 ) {
407 LDAP_LOG ( OPERATION, ERR,
408 "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
410 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
411 db_strerror(rs->sr_err), rs->sr_err, 0 );
414 switch( rs->sr_err ) {
415 case DB_LOCK_DEADLOCK:
416 case DB_LOCK_NOTGRANTED:
419 rs->sr_err = LDAP_ALREADY_EXISTS;
422 rs->sr_err = LDAP_OTHER;
428 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
429 if ( rs->sr_err != 0 ) {
431 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
433 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
436 switch( rs->sr_err ) {
437 case DB_LOCK_DEADLOCK:
438 case DB_LOCK_NOTGRANTED:
441 rs->sr_err = LDAP_OTHER;
443 rs->sr_text = "entry store failed";
447 /* attribute indexes */
448 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
449 if ( rs->sr_err != LDAP_SUCCESS ) {
451 LDAP_LOG ( OPERATION, ERR,
452 "bdb_add: index_entry_add failed\n", 0, 0, 0 );
454 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
457 switch( rs->sr_err ) {
458 case DB_LOCK_DEADLOCK:
459 case DB_LOCK_NOTGRANTED:
462 rs->sr_err = LDAP_OTHER;
464 rs->sr_text = "index generation failed";
467 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
468 rs->sr_err = LDAP_OTHER;
469 rs->sr_text = "txn_commit(2) failed";
474 rc = bdb_csn_commit( op, rs, ltid, ei, &suffix_ei, &ctxcsn_e, &ctxcsn_added, locker );
484 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
485 rs->sr_text = "txn_abort (no-op) failed";
488 rs->sr_err = LDAP_SUCCESS;
492 char gid[DB_XIDDATASIZE];
494 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
495 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
497 if (( rs->sr_err=TXN_PREPARE( ltid, gid )) != 0 ) {
498 rs->sr_text = "txn_prepare failed";
503 struct berval ctx_nrdn;
507 nrdn.bv_val = op->ora_e->e_nname.bv_val;
508 nrdn.bv_len = pdn.bv_val - nrdn.bv_val - 1;
510 nrdn = op->ora_e->e_nname;
513 bdb_cache_add( bdb, ei, op->oq_add.rs_e, &nrdn, locker );
516 if ( ctxcsn_added ) {
517 ctx_nrdn.bv_val = "cn=ldapsync";
518 ctx_nrdn.bv_len = strlen( ctx_nrdn.bv_val );
519 bdb_cache_add( bdb, suffix_ei, ctxcsn_e, &ctx_nrdn, locker );
523 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
524 rs->sr_text = "txn_commit failed";
526 rs->sr_err = LDAP_SUCCESS;
532 op->o_private = NULL;
534 if (rs->sr_err == LDAP_SUCCESS) {
536 LDAP_LOG ( OPERATION, RESULTS,
537 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
538 op->o_noop ? " (no-op)" : "", op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
540 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
541 op->o_noop ? " (no-op)" : "", op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
547 LDAP_LOG ( OPERATION, ERR,
548 "bdb_add: %s : %s (%d)\n", rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
550 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
551 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
553 rs->sr_err = LDAP_OTHER;
557 send_ldap_result( op, rs );
560 if ( rs->sr_err == LDAP_SUCCESS && !noop ) {
561 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
562 bdb_psearch( op, rs, ps_list, op->oq_add.rs_e, LDAP_PSEARCH_BY_ADD );
567 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
568 ldap_pvt_thread_yield();
569 TXN_CHECKPOINT( bdb->bi_dbenv,
570 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
577 op->o_private = NULL;
580 return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );