1 /* add.c - ldap BerkeleyDB back-end add routine */
4 * Copyright 1998-2002 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;
32 struct bdb_op_info opinfo;
44 LDAP_LOG ( OPERATION, ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0 );
46 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
49 /* check entry's schema */
50 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
51 if ( rc != LDAP_SUCCESS ) {
53 LDAP_LOG ( OPERATION, ERR,
54 "bdb_add: entry failed schema check: %s (%d)\n", text, rc, 0 );
56 Debug( LDAP_DEBUG_TRACE,
57 "bdb_add: entry failed schema check: %s (%d)\n",
64 subentry = is_entry_subentry( e );
68 * acquire an ID outside of the operation transaction
69 * to avoid serializing adds.
71 rc = bdb_next_id( be, NULL, &e->e_id );
74 LDAP_LOG ( OPERATION, ERR,
75 "bdb_add: next_id failed (%d)\n", rc, 0, 0 );
77 Debug( LDAP_DEBUG_TRACE,
78 "bdb_add: next_id failed (%d)\n",
82 text = "internal error";
87 retry: /* transaction retry */
88 rc = TXN_ABORT( ltid );
93 text = "internal error";
96 ldap_pvt_thread_yield();
99 /* begin transaction */
100 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
101 bdb->bi_db_opflags );
105 LDAP_LOG ( OPERATION, ERR,
106 "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
108 Debug( LDAP_DEBUG_TRACE,
109 "bdb_add: txn_begin failed: %s (%d)\n",
110 db_strerror(rc), rc, 0 );
113 text = "internal error";
117 locker = TXN_ID ( ltid );
119 lockid = TXN_ID( ltid );
123 opinfo.boi_txn = ltid;
125 op->o_private = &opinfo;
128 * Get the parent dn and see if the corresponding entry exists.
129 * If the parent does not exist, only allow the "root" user to
132 if ( be_issuffix( be, &e->e_nname ) ) {
135 dnParent( &e->e_nname, &pdn );
138 if( pdn.bv_len != 0 ) {
139 Entry *matched = NULL;
144 obj.data = pdn.bv_val-1;
145 obj.size = pdn.bv_len+1;
146 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
147 DB_LOCK_WRITE, &lock);
152 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0, locker, &lock );
158 case DB_LOCK_DEADLOCK:
159 case DB_LOCK_NOTGRANTED:
162 text = "ldap server busy";
166 text = "internal error";
171 char *matched_dn = NULL;
174 if ( matched != NULL ) {
175 matched_dn = ch_strdup( matched->e_dn );
176 refs = is_entry_referral( matched )
177 ? get_entry_referrals( be, conn, op, matched )
179 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched );
183 refs = referral_rewrite( default_referral,
184 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
188 LDAP_LOG ( OPERATION, DETAIL1,
189 "bdb_add: parent does not exist\n", 0, 0, 0 );
191 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
195 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
196 matched_dn, NULL, refs, NULL );
198 ber_bvarray_free( refs );
199 ch_free( matched_dn );
204 rc = access_allowed( be, conn, op, p,
205 children, NULL, ACL_WRITE, NULL );
207 switch( opinfo.boi_err ) {
208 case DB_LOCK_DEADLOCK:
209 case DB_LOCK_NOTGRANTED:
210 /* free parent and reader lock */
211 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
218 LDAP_LOG ( OPERATION, DETAIL1,
219 "bdb_add: no write access to parent\n", 0, 0, 0 );
221 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
224 rc = LDAP_INSUFFICIENT_ACCESS;
225 text = "no write access to parent";
226 goto return_results;;
229 #ifdef BDB_SUBENTRIES
230 if ( is_entry_subentry( p ) ) {
231 /* parent is a subentry, don't allow add */
233 LDAP_LOG ( OPERATION, DETAIL1,
234 "bdb_add: parent is subentry\n", 0, 0, 0 );
236 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
239 rc = LDAP_OBJECT_CLASS_VIOLATION;
240 text = "parent is a subentry";
241 goto return_results;;
245 if ( is_entry_alias( p ) ) {
246 /* parent is an alias, don't allow add */
248 LDAP_LOG ( OPERATION, DETAIL1,
249 "bdb_add: parent is alias\n", 0, 0, 0 );
251 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
254 rc = LDAP_ALIAS_PROBLEM;
255 text = "parent is an alias";
256 goto return_results;;
260 if ( is_entry_referral( p ) ) {
261 /* parent is a referral, don't allow add */
262 char *matched_dn = p->e_dn;
263 BerVarray refs = get_entry_referrals( be, conn, op, p );
266 LDAP_LOG ( OPERATION, DETAIL1,
267 "bdb_add: parent is referral\n", 0, 0, 0 );
269 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
273 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
274 matched_dn, NULL, refs, NULL );
276 ber_bvarray_free( refs );
277 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
282 #ifdef BDB_SUBENTRIES
285 /* parent must be an administrative point of the required kind */
289 /* free parent and reader lock */
290 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
296 * must be adding entry at suffix or with parent ""
298 if ( !be_isroot( be, &op->o_ndn )) {
299 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
300 || be_isupdate( be, &op->o_ndn ) ) {
301 p = (Entry *)&slap_entry_root;
303 /* check parent for "children" acl */
304 rc = access_allowed( be, conn, op, p,
305 children, NULL, ACL_WRITE, NULL );
308 switch( opinfo.boi_err ) {
309 case DB_LOCK_DEADLOCK:
310 case DB_LOCK_NOTGRANTED:
316 LDAP_LOG ( OPERATION, DETAIL1,
317 "bdb_add: no write access to parent\n", 0, 0, 0 );
319 Debug( LDAP_DEBUG_TRACE,
320 "bdb_add: no write access to parent\n",
323 rc = LDAP_INSUFFICIENT_ACCESS;
324 text = "no write access to parent";
325 goto return_results;;
330 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
331 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
333 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
334 pdn.bv_len == 0 ? "suffix" : "entry at root",
337 rc = LDAP_INSUFFICIENT_ACCESS;
342 #ifdef BDB_SUBENTRIES
345 LDAP_LOG ( OPERATION, DETAIL1,
346 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
348 Debug( LDAP_DEBUG_TRACE,
349 "bdb_add: no parent, cannot add subentry\n",
352 rc = LDAP_INSUFFICIENT_ACCESS;
353 text = "no parent, cannot add subentry";
354 goto return_results;;
362 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
363 DB_LOCK_WRITE, &lock);
369 rc = bdb_dn2id_add( be, ltid, &pdn, e );
372 LDAP_LOG ( OPERATION, ERR,
373 "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rc), rc, 0 );
375 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
376 db_strerror(rc), rc, 0 );
380 case DB_LOCK_DEADLOCK:
381 case DB_LOCK_NOTGRANTED:
384 rc = LDAP_ALREADY_EXISTS;
393 rc = bdb_id2entry_add( be, ltid, e );
396 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
398 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
402 case DB_LOCK_DEADLOCK:
403 case DB_LOCK_NOTGRANTED:
408 text = "entry store failed";
412 /* attribute indexes */
413 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
414 if ( rc != LDAP_SUCCESS ) {
416 LDAP_LOG ( OPERATION, ERR,
417 "bdb_add: index_entry_add failed\n", 0, 0, 0 );
419 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
423 case DB_LOCK_DEADLOCK:
424 case DB_LOCK_NOTGRANTED:
429 text = "index generation failed";
434 if (( rc=TXN_ABORT( ltid )) != 0 ) {
435 text = "txn_abort (no-op) failed";
441 char gid[DB_XIDDATASIZE];
443 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
444 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
446 if (( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
447 text = "txn_prepare failed";
450 ret = bdb_cache_add_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, CACHE_WRITE_LOCK, locker, &lock);
452 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
453 e, CACHE_WRITE_LOCK) != 0 )
458 case DB_LOCK_DEADLOCK:
459 case DB_LOCK_NOTGRANTED:
466 if(( rc=TXN_ABORT( ltid )) != 0 ) {
467 text = "cache add & txn_abort failed";
470 text = "cache add failed";
473 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
474 text = "txn_commit failed";
483 op->o_private = NULL;
485 if (rc == LDAP_SUCCESS) {
487 LDAP_LOG ( OPERATION, RESULTS,
488 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
489 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
491 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
492 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
495 bdb_cache_entry_commit( e );
499 LDAP_LOG ( OPERATION, ERR,
500 "bdb_add: %s : %s (%d)\n", text, db_strerror(rc), rc );
502 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
503 text, db_strerror(rc), rc );
509 send_ldap_result( conn, op, rc,
510 NULL, text, NULL, NULL );
512 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
513 ldap_pvt_thread_yield();
514 TXN_CHECKPOINT( bdb->bi_dbenv,
515 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
522 op->o_private = NULL;