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;
45 LDAP_LOG ( OPERATION, ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0 );
47 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
50 /* check entry's schema */
51 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
52 if ( rc != LDAP_SUCCESS ) {
54 LDAP_LOG ( OPERATION, ERR,
55 "bdb_add: entry failed schema check: %s (%d)\n", text, rc, 0 );
57 Debug( LDAP_DEBUG_TRACE,
58 "bdb_add: entry failed schema check: %s (%d)\n",
65 subentry = is_entry_subentry( e );
69 * acquire an ID outside of the operation transaction
70 * to avoid serializing adds.
72 rc = bdb_next_id( be, NULL, &e->e_id );
75 LDAP_LOG ( OPERATION, ERR,
76 "bdb_add: next_id failed (%d)\n", rc, 0, 0 );
78 Debug( LDAP_DEBUG_TRACE,
79 "bdb_add: next_id failed (%d)\n",
83 text = "internal error";
88 retry: /* transaction retry */
89 rc = TXN_ABORT( ltid );
94 text = "internal error";
97 ldap_pvt_thread_yield();
100 /* begin transaction */
101 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
102 bdb->bi_db_opflags );
106 LDAP_LOG ( OPERATION, ERR,
107 "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
109 Debug( LDAP_DEBUG_TRACE,
110 "bdb_add: txn_begin failed: %s (%d)\n",
111 db_strerror(rc), rc, 0 );
114 text = "internal error";
118 locker = TXN_ID ( ltid );
120 lockid = TXN_ID( ltid );
124 opinfo.boi_txn = ltid;
126 op->o_private = &opinfo;
129 * Get the parent dn and see if the corresponding entry exists.
130 * If the parent does not exist, only allow the "root" user to
133 if ( be_issuffix( be, &e->e_nname ) ) {
136 dnParent( &e->e_nname, &pdn );
139 if( pdn.bv_len != 0 ) {
140 Entry *matched = NULL;
145 obj.data = pdn.bv_val-1;
146 obj.size = pdn.bv_len+1;
147 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
148 DB_LOCK_WRITE, &lock);
153 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0, locker, &lock );
159 case DB_LOCK_DEADLOCK:
160 case DB_LOCK_NOTGRANTED:
163 text = "ldap server busy";
167 text = "internal error";
172 char *matched_dn = NULL;
175 if ( matched != NULL ) {
176 matched_dn = ch_strdup( matched->e_dn );
177 refs = is_entry_referral( matched )
178 ? get_entry_referrals( be, conn, op, matched )
180 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched );
184 refs = referral_rewrite( default_referral,
185 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
189 LDAP_LOG ( OPERATION, DETAIL1,
190 "bdb_add: parent does not exist\n", 0, 0, 0 );
192 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
196 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
197 matched_dn, NULL, refs, NULL );
199 ber_bvarray_free( refs );
200 ch_free( matched_dn );
205 rc = access_allowed( be, conn, op, p,
206 children, NULL, ACL_WRITE, NULL );
208 switch( opinfo.boi_err ) {
209 case DB_LOCK_DEADLOCK:
210 case DB_LOCK_NOTGRANTED:
211 /* free parent and reader lock */
212 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
219 LDAP_LOG ( OPERATION, DETAIL1,
220 "bdb_add: no write access to parent\n", 0, 0, 0 );
222 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
225 rc = LDAP_INSUFFICIENT_ACCESS;
226 text = "no write access to parent";
227 goto return_results;;
230 #ifdef BDB_SUBENTRIES
231 if ( is_entry_subentry( p ) ) {
232 /* parent is a subentry, don't allow add */
234 LDAP_LOG ( OPERATION, DETAIL1,
235 "bdb_add: parent is subentry\n", 0, 0, 0 );
237 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
240 rc = LDAP_OBJECT_CLASS_VIOLATION;
241 text = "parent is a subentry";
242 goto return_results;;
246 if ( is_entry_alias( p ) ) {
247 /* parent is an alias, don't allow add */
249 LDAP_LOG ( OPERATION, DETAIL1,
250 "bdb_add: parent is alias\n", 0, 0, 0 );
252 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
255 rc = LDAP_ALIAS_PROBLEM;
256 text = "parent is an alias";
257 goto return_results;;
261 if ( is_entry_referral( p ) ) {
262 /* parent is a referral, don't allow add */
263 char *matched_dn = p->e_dn;
264 BerVarray refs = get_entry_referrals( be, conn, op, p );
267 LDAP_LOG ( OPERATION, DETAIL1,
268 "bdb_add: parent is referral\n", 0, 0, 0 );
270 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
274 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
275 matched_dn, NULL, refs, NULL );
277 ber_bvarray_free( refs );
278 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
283 #ifdef BDB_SUBENTRIES
286 /* parent must be an administrative point of the required kind */
290 /* free parent and reader lock */
291 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
297 * must be adding entry at suffix or with parent ""
299 if ( !be_isroot( be, &op->o_ndn )) {
300 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
301 || be_isupdate( be, &op->o_ndn ) ) {
302 p = (Entry *)&slap_entry_root;
304 /* check parent for "children" acl */
305 rc = access_allowed( be, conn, op, p,
306 children, NULL, ACL_WRITE, NULL );
309 switch( opinfo.boi_err ) {
310 case DB_LOCK_DEADLOCK:
311 case DB_LOCK_NOTGRANTED:
317 LDAP_LOG ( OPERATION, DETAIL1,
318 "bdb_add: no write access to parent\n", 0, 0, 0 );
320 Debug( LDAP_DEBUG_TRACE,
321 "bdb_add: no write access to parent\n",
324 rc = LDAP_INSUFFICIENT_ACCESS;
325 text = "no write access to parent";
326 goto return_results;;
331 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
332 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
334 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
335 pdn.bv_len == 0 ? "suffix" : "entry at root",
338 rc = LDAP_INSUFFICIENT_ACCESS;
343 #ifdef BDB_SUBENTRIES
346 LDAP_LOG ( OPERATION, DETAIL1,
347 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
349 Debug( LDAP_DEBUG_TRACE,
350 "bdb_add: no parent, cannot add subentry\n",
353 rc = LDAP_INSUFFICIENT_ACCESS;
354 text = "no parent, cannot add subentry";
355 goto return_results;;
363 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
364 DB_LOCK_WRITE, &lock);
370 rc = bdb_dn2id_add( be, ltid, &pdn, e );
373 LDAP_LOG ( OPERATION, ERR,
374 "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rc), rc, 0 );
376 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
377 db_strerror(rc), rc, 0 );
381 case DB_LOCK_DEADLOCK:
382 case DB_LOCK_NOTGRANTED:
385 rc = LDAP_ALREADY_EXISTS;
394 rc = bdb_id2entry_add( be, ltid, e );
397 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
399 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
403 case DB_LOCK_DEADLOCK:
404 case DB_LOCK_NOTGRANTED:
409 text = "entry store failed";
413 /* attribute indexes */
414 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
415 if ( rc != LDAP_SUCCESS ) {
417 LDAP_LOG ( OPERATION, ERR,
418 "bdb_add: index_entry_add failed\n", 0, 0, 0 );
420 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
424 case DB_LOCK_DEADLOCK:
425 case DB_LOCK_NOTGRANTED:
430 text = "index generation failed";
435 if (( rc=TXN_ABORT( ltid )) != 0 ) {
436 text = "txn_abort (no-op) failed";
443 char gid[DB_XIDDATASIZE];
445 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
446 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
448 if (( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
449 text = "txn_prepare failed";
452 int ret = bdb_cache_add_entry_rw(bdb->bi_dbenv,
453 &bdb->bi_cache, e, CACHE_WRITE_LOCK,
456 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
457 e, CACHE_WRITE_LOCK) != 0 )
462 case DB_LOCK_DEADLOCK:
463 case DB_LOCK_NOTGRANTED:
470 if(( rc=TXN_ABORT( ltid )) != 0 ) {
471 text = "cache add & txn_abort failed";
474 text = "cache add failed";
477 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
478 text = "txn_commit failed";
487 op->o_private = NULL;
489 if (rc == LDAP_SUCCESS) {
491 LDAP_LOG ( OPERATION, RESULTS,
492 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
493 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
495 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
496 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
500 bdb_cache_entry_commit( e );
505 LDAP_LOG ( OPERATION, ERR,
506 "bdb_add: %s : %s (%d)\n", text, db_strerror(rc), rc );
508 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
509 text, db_strerror(rc), rc );
515 send_ldap_result( conn, op, rc,
516 NULL, text, NULL, NULL );
518 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
519 ldap_pvt_thread_yield();
520 TXN_CHECKPOINT( bdb->bi_dbenv,
521 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
528 op->o_private = NULL;
531 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );