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>
23 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
28 char textbuf[SLAP_TEXT_BUFLEN];
29 size_t textlen = sizeof textbuf;
30 AttributeDescription *children = slap_schema.si_ad_children;
31 AttributeDescription *entry = slap_schema.si_ad_entry;
33 struct bdb_op_info opinfo;
45 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
50 LDAP_LOG ( OPERATION, ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0 );
52 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
55 /* check entry's schema */
56 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
57 if ( rc != LDAP_SUCCESS ) {
59 LDAP_LOG ( OPERATION, ERR,
60 "bdb_add: entry failed schema check: %s (%d)\n", text, rc, 0 );
62 Debug( LDAP_DEBUG_TRACE,
63 "bdb_add: entry failed schema check: %s (%d)\n",
70 subentry = is_entry_subentry( e );
74 * acquire an ID outside of the operation transaction
75 * to avoid serializing adds.
77 rc = bdb_next_id( be, NULL, &e->e_id );
80 LDAP_LOG ( OPERATION, ERR,
81 "bdb_add: next_id failed (%d)\n", rc, 0, 0 );
83 Debug( LDAP_DEBUG_TRACE,
84 "bdb_add: next_id failed (%d)\n",
88 text = "internal error";
93 retry: /* transaction retry */
95 /* free parent and reader lock */
96 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
99 rc = TXN_ABORT( ltid );
101 op->o_private = NULL;
102 op->o_do_not_cache = opinfo.boi_acl_cache;
105 text = "internal error";
108 ldap_pvt_thread_yield();
111 /* begin transaction */
112 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
113 bdb->bi_db_opflags );
117 LDAP_LOG ( OPERATION, ERR,
118 "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
120 Debug( LDAP_DEBUG_TRACE,
121 "bdb_add: txn_begin failed: %s (%d)\n",
122 db_strerror(rc), rc, 0 );
125 text = "internal error";
129 locker = TXN_ID ( ltid );
131 lockid = TXN_ID( ltid );
135 opinfo.boi_txn = ltid;
136 opinfo.boi_locker = locker;
138 opinfo.boi_acl_cache = op->o_do_not_cache;
139 op->o_private = &opinfo;
142 * Get the parent dn and see if the corresponding entry exists.
143 * If the parent does not exist, only allow the "root" user to
146 if ( be_issuffix( be, &e->e_nname ) ) {
149 dnParent( &e->e_nname, &pdn );
152 if( pdn.bv_len != 0 ) {
153 Entry *matched = NULL;
158 obj.data = pdn.bv_val-1;
159 obj.size = pdn.bv_len+1;
160 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
161 DB_LOCK_WRITE, &lock);
166 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0, locker, &lock );
172 case DB_LOCK_DEADLOCK:
173 case DB_LOCK_NOTGRANTED:
176 text = "ldap server busy";
180 text = "internal error";
185 char *matched_dn = NULL;
188 if ( matched != NULL ) {
189 matched_dn = ch_strdup( matched->e_dn );
190 refs = is_entry_referral( matched )
191 ? get_entry_referrals( be, conn, op, matched )
193 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched );
197 refs = referral_rewrite( default_referral,
198 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
202 LDAP_LOG ( OPERATION, DETAIL1,
203 "bdb_add: parent does not exist\n", 0, 0, 0 );
205 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
209 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
210 matched_dn, NULL, refs, NULL );
212 ber_bvarray_free( refs );
213 ch_free( matched_dn );
218 rc = access_allowed( be, conn, op, p,
219 children, NULL, ACL_WRITE, NULL );
222 switch( opinfo.boi_err ) {
223 case DB_LOCK_DEADLOCK:
224 case DB_LOCK_NOTGRANTED:
229 LDAP_LOG ( OPERATION, DETAIL1,
230 "bdb_add: no write access to parent\n", 0, 0, 0 );
232 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
235 rc = LDAP_INSUFFICIENT_ACCESS;
236 text = "no write access to parent";
237 goto return_results;;
240 #ifdef BDB_SUBENTRIES
241 if ( is_entry_subentry( p ) ) {
242 /* parent is a subentry, don't allow add */
244 LDAP_LOG ( OPERATION, DETAIL1,
245 "bdb_add: parent is subentry\n", 0, 0, 0 );
247 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
250 rc = LDAP_OBJECT_CLASS_VIOLATION;
251 text = "parent is a subentry";
252 goto return_results;;
256 if ( is_entry_alias( p ) ) {
257 /* parent is an alias, don't allow add */
259 LDAP_LOG ( OPERATION, DETAIL1,
260 "bdb_add: parent is alias\n", 0, 0, 0 );
262 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
265 rc = LDAP_ALIAS_PROBLEM;
266 text = "parent is an alias";
267 goto return_results;;
271 if ( is_entry_referral( p ) ) {
272 /* parent is a referral, don't allow add */
273 char *matched_dn = p->e_dn;
274 BerVarray refs = get_entry_referrals( be, conn, op, p );
277 LDAP_LOG ( OPERATION, DETAIL1,
278 "bdb_add: parent is referral\n", 0, 0, 0 );
280 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
284 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
285 matched_dn, NULL, refs, NULL );
287 ber_bvarray_free( refs );
288 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
293 #ifdef BDB_SUBENTRIES
296 /* parent must be an administrative point of the required kind */
300 /* free parent and reader lock */
301 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
307 * must be adding entry at suffix or with parent ""
309 if ( !be_isroot( be, &op->o_ndn )) {
310 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
311 || be_isupdate( be, &op->o_ndn ) )
313 p = (Entry *)&slap_entry_root;
315 /* check parent for "children" acl */
316 rc = access_allowed( be, conn, op, p,
317 children, NULL, ACL_WRITE, NULL );
322 switch( opinfo.boi_err ) {
323 case DB_LOCK_DEADLOCK:
324 case DB_LOCK_NOTGRANTED:
329 LDAP_LOG ( OPERATION, DETAIL1,
330 "bdb_add: no write access to parent\n", 0, 0, 0 );
332 Debug( LDAP_DEBUG_TRACE,
333 "bdb_add: no write access to parent\n",
336 rc = LDAP_INSUFFICIENT_ACCESS;
337 text = "no write access to parent";
338 goto return_results;;
343 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
344 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
346 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
347 pdn.bv_len == 0 ? "suffix" : "entry at root",
350 rc = LDAP_INSUFFICIENT_ACCESS;
355 #ifdef BDB_SUBENTRIES
358 LDAP_LOG ( OPERATION, DETAIL1,
359 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
361 Debug( LDAP_DEBUG_TRACE,
362 "bdb_add: no parent, cannot add subentry\n",
365 rc = LDAP_INSUFFICIENT_ACCESS;
366 text = "no parent, cannot add subentry";
367 goto return_results;;
375 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
376 DB_LOCK_WRITE, &lock);
381 rc = access_allowed( be, conn, op, e,
382 entry, NULL, ACL_WRITE, NULL );
385 switch( opinfo.boi_err ) {
386 case DB_LOCK_DEADLOCK:
387 case DB_LOCK_NOTGRANTED:
392 LDAP_LOG ( OPERATION, DETAIL1,
393 "bdb_add: no write access to entry\n", 0, 0, 0 );
395 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
398 rc = LDAP_INSUFFICIENT_ACCESS;
399 text = "no write access to entry";
400 goto return_results;;
404 rc = bdb_dn2id_add( be, ltid, &pdn, e );
407 LDAP_LOG ( OPERATION, ERR,
408 "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rc), rc, 0 );
410 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
411 db_strerror(rc), rc, 0 );
415 case DB_LOCK_DEADLOCK:
416 case DB_LOCK_NOTGRANTED:
419 rc = LDAP_ALREADY_EXISTS;
428 rc = bdb_id2entry_add( be, ltid, e );
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",
437 case DB_LOCK_DEADLOCK:
438 case DB_LOCK_NOTGRANTED:
443 text = "entry store failed";
447 /* attribute indexes */
448 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
449 if ( rc != 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",
458 case DB_LOCK_DEADLOCK:
459 case DB_LOCK_NOTGRANTED:
464 text = "index generation failed";
470 if (( rc=TXN_ABORT( ltid )) != 0 ) {
471 text = "txn_abort (no-op) failed";
478 char gid[DB_XIDDATASIZE];
480 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
481 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
483 if (( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
484 text = "txn_prepare failed";
487 int ret = bdb_cache_add_entry_rw(bdb->bi_dbenv,
488 &bdb->bi_cache, e, CACHE_WRITE_LOCK,
491 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
492 e, CACHE_WRITE_LOCK) != 0 )
497 case DB_LOCK_DEADLOCK:
498 case DB_LOCK_NOTGRANTED:
505 if(( rc=TXN_ABORT( ltid )) != 0 ) {
506 text = "cache add & txn_abort failed";
509 text = "cache add failed";
512 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
513 text = "txn_commit failed";
522 op->o_private = NULL;
524 if (rc == LDAP_SUCCESS) {
526 LDAP_LOG ( OPERATION, RESULTS,
527 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
528 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
530 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
531 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
535 bdb_cache_entry_commit( e );
540 LDAP_LOG ( OPERATION, ERR,
541 "bdb_add: %s : %s (%d)\n", text, db_strerror(rc), rc );
543 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
544 text, db_strerror(rc), rc );
550 send_ldap_result( conn, op, rc,
551 NULL, text, NULL, NULL );
553 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
554 if ( rc == LDAP_SUCCESS && !noop ) {
555 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
556 bdb_psearch( be, conn, op, ps_list, e, LDAP_PSEARCH_BY_ADD );
559 #endif /* LDAP_CLIENT_UPDATE */
561 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
562 ldap_pvt_thread_yield();
563 TXN_CHECKPOINT( bdb->bi_dbenv,
564 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
571 op->o_private = NULL;
574 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );