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;
31 AttributeDescription *entry = slap_schema.si_ad_entry;
33 struct bdb_op_info opinfo;
45 #ifdef LDAP_CLIENT_UPDATE
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;
104 text = "internal error";
107 ldap_pvt_thread_yield();
110 /* begin transaction */
111 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
112 bdb->bi_db_opflags );
116 LDAP_LOG ( OPERATION, ERR,
117 "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
119 Debug( LDAP_DEBUG_TRACE,
120 "bdb_add: txn_begin failed: %s (%d)\n",
121 db_strerror(rc), rc, 0 );
124 text = "internal error";
128 locker = TXN_ID ( ltid );
130 lockid = TXN_ID( ltid );
134 opinfo.boi_txn = ltid;
136 op->o_private = &opinfo;
139 * Get the parent dn and see if the corresponding entry exists.
140 * If the parent does not exist, only allow the "root" user to
143 if ( be_issuffix( be, &e->e_nname ) ) {
146 dnParent( &e->e_nname, &pdn );
149 if( pdn.bv_len != 0 ) {
150 Entry *matched = NULL;
155 obj.data = pdn.bv_val-1;
156 obj.size = pdn.bv_len+1;
157 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
158 DB_LOCK_WRITE, &lock);
163 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0, locker, &lock );
169 case DB_LOCK_DEADLOCK:
170 case DB_LOCK_NOTGRANTED:
173 text = "ldap server busy";
177 text = "internal error";
182 char *matched_dn = NULL;
185 if ( matched != NULL ) {
186 matched_dn = ch_strdup( matched->e_dn );
187 refs = is_entry_referral( matched )
188 ? get_entry_referrals( be, conn, op, matched )
190 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched );
194 refs = referral_rewrite( default_referral,
195 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
199 LDAP_LOG ( OPERATION, DETAIL1,
200 "bdb_add: parent does not exist\n", 0, 0, 0 );
202 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
206 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
207 matched_dn, NULL, refs, NULL );
209 ber_bvarray_free( refs );
210 ch_free( matched_dn );
215 rc = access_allowed( be, conn, op, p,
216 children, NULL, ACL_WRITE, NULL );
218 switch( opinfo.boi_err ) {
219 case DB_LOCK_DEADLOCK:
220 case DB_LOCK_NOTGRANTED:
226 LDAP_LOG ( OPERATION, DETAIL1,
227 "bdb_add: no write access to parent\n", 0, 0, 0 );
229 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
232 rc = LDAP_INSUFFICIENT_ACCESS;
233 text = "no write access to parent";
234 goto return_results;;
237 #ifdef BDB_SUBENTRIES
238 if ( is_entry_subentry( p ) ) {
239 /* parent is a subentry, don't allow add */
241 LDAP_LOG ( OPERATION, DETAIL1,
242 "bdb_add: parent is subentry\n", 0, 0, 0 );
244 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
247 rc = LDAP_OBJECT_CLASS_VIOLATION;
248 text = "parent is a subentry";
249 goto return_results;;
253 if ( is_entry_alias( p ) ) {
254 /* parent is an alias, don't allow add */
256 LDAP_LOG ( OPERATION, DETAIL1,
257 "bdb_add: parent is alias\n", 0, 0, 0 );
259 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
262 rc = LDAP_ALIAS_PROBLEM;
263 text = "parent is an alias";
264 goto return_results;;
268 if ( is_entry_referral( p ) ) {
269 /* parent is a referral, don't allow add */
270 char *matched_dn = p->e_dn;
271 BerVarray refs = get_entry_referrals( be, conn, op, p );
274 LDAP_LOG ( OPERATION, DETAIL1,
275 "bdb_add: parent is referral\n", 0, 0, 0 );
277 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
281 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
282 matched_dn, NULL, refs, NULL );
284 ber_bvarray_free( refs );
285 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
290 #ifdef BDB_SUBENTRIES
293 /* parent must be an administrative point of the required kind */
297 /* free parent and reader lock */
298 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
304 * must be adding entry at suffix or with parent ""
306 if ( !be_isroot( be, &op->o_ndn )) {
307 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
308 || be_isupdate( be, &op->o_ndn ) )
310 p = (Entry *)&slap_entry_root;
312 /* check parent for "children" acl */
313 rc = access_allowed( be, conn, op, p,
314 children, NULL, ACL_WRITE, NULL );
318 switch( opinfo.boi_err ) {
319 case DB_LOCK_DEADLOCK:
320 case DB_LOCK_NOTGRANTED:
326 LDAP_LOG ( OPERATION, DETAIL1,
327 "bdb_add: no write access to parent\n", 0, 0, 0 );
329 Debug( LDAP_DEBUG_TRACE,
330 "bdb_add: no write access to parent\n",
333 rc = LDAP_INSUFFICIENT_ACCESS;
334 text = "no write access to parent";
335 goto return_results;;
340 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
341 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
343 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
344 pdn.bv_len == 0 ? "suffix" : "entry at root",
347 rc = LDAP_INSUFFICIENT_ACCESS;
352 #ifdef BDB_SUBENTRIES
355 LDAP_LOG ( OPERATION, DETAIL1,
356 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
358 Debug( LDAP_DEBUG_TRACE,
359 "bdb_add: no parent, cannot add subentry\n",
362 rc = LDAP_INSUFFICIENT_ACCESS;
363 text = "no parent, cannot add subentry";
364 goto return_results;;
372 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
373 DB_LOCK_WRITE, &lock);
378 rc = access_allowed( be, conn, op, e,
379 entry, NULL, ACL_WRITE, NULL );
381 switch( opinfo.boi_err ) {
382 case DB_LOCK_DEADLOCK:
383 case DB_LOCK_NOTGRANTED:
389 LDAP_LOG ( OPERATION, DETAIL1,
390 "bdb_add: no write access to entry\n", 0, 0, 0 );
392 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
395 rc = LDAP_INSUFFICIENT_ACCESS;
396 text = "no write access to entry";
397 goto return_results;;
401 rc = bdb_dn2id_add( be, ltid, &pdn, e );
404 LDAP_LOG ( OPERATION, ERR,
405 "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rc), rc, 0 );
407 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
408 db_strerror(rc), rc, 0 );
412 case DB_LOCK_DEADLOCK:
413 case DB_LOCK_NOTGRANTED:
416 rc = LDAP_ALREADY_EXISTS;
425 rc = bdb_id2entry_add( be, ltid, e );
428 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
430 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
434 case DB_LOCK_DEADLOCK:
435 case DB_LOCK_NOTGRANTED:
440 text = "entry store failed";
444 /* attribute indexes */
445 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
446 if ( rc != LDAP_SUCCESS ) {
448 LDAP_LOG ( OPERATION, ERR,
449 "bdb_add: index_entry_add failed\n", 0, 0, 0 );
451 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
455 case DB_LOCK_DEADLOCK:
456 case DB_LOCK_NOTGRANTED:
461 text = "index generation failed";
467 if (( rc=TXN_ABORT( ltid )) != 0 ) {
468 text = "txn_abort (no-op) failed";
475 char gid[DB_XIDDATASIZE];
477 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
478 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
480 if (( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
481 text = "txn_prepare failed";
484 int ret = bdb_cache_add_entry_rw(bdb->bi_dbenv,
485 &bdb->bi_cache, e, CACHE_WRITE_LOCK,
488 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
489 e, CACHE_WRITE_LOCK) != 0 )
494 case DB_LOCK_DEADLOCK:
495 case DB_LOCK_NOTGRANTED:
502 if(( rc=TXN_ABORT( ltid )) != 0 ) {
503 text = "cache add & txn_abort failed";
506 text = "cache add failed";
509 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
510 text = "txn_commit failed";
519 op->o_private = NULL;
521 if (rc == LDAP_SUCCESS) {
523 LDAP_LOG ( OPERATION, RESULTS,
524 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
525 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
527 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
528 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
532 bdb_cache_entry_commit( e );
537 LDAP_LOG ( OPERATION, ERR,
538 "bdb_add: %s : %s (%d)\n", text, db_strerror(rc), rc );
540 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
541 text, db_strerror(rc), rc );
547 send_ldap_result( conn, op, rc,
548 NULL, text, NULL, NULL );
550 #ifdef LDAP_CLIENT_UPDATE
551 if ( rc == LDAP_SUCCESS && !noop ) {
552 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
553 bdb_psearch( be, conn, op, ps_list, e, LCUP_PSEARCH_BY_ADD );
556 #endif /* LDAP_CLIENT_UPDATE */
558 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
559 ldap_pvt_thread_yield();
560 TXN_CHECKPOINT( bdb->bi_dbenv,
561 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
568 op->o_private = NULL;
571 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );