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;
46 LDAP_LOG ( OPERATION, ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0 );
48 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
51 /* check entry's schema */
52 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
53 if ( rc != LDAP_SUCCESS ) {
55 LDAP_LOG ( OPERATION, ERR,
56 "bdb_add: entry failed schema check: %s (%d)\n", text, rc, 0 );
58 Debug( LDAP_DEBUG_TRACE,
59 "bdb_add: entry failed schema check: %s (%d)\n",
66 subentry = is_entry_subentry( e );
70 * acquire an ID outside of the operation transaction
71 * to avoid serializing adds.
73 rc = bdb_next_id( be, NULL, &e->e_id );
76 LDAP_LOG ( OPERATION, ERR,
77 "bdb_add: next_id failed (%d)\n", rc, 0, 0 );
79 Debug( LDAP_DEBUG_TRACE,
80 "bdb_add: next_id failed (%d)\n",
84 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 rc = TXN_ABORT( ltid );
100 text = "internal error";
103 ldap_pvt_thread_yield();
106 /* begin transaction */
107 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
108 bdb->bi_db_opflags );
112 LDAP_LOG ( OPERATION, ERR,
113 "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rc), rc, 0 );
115 Debug( LDAP_DEBUG_TRACE,
116 "bdb_add: txn_begin failed: %s (%d)\n",
117 db_strerror(rc), rc, 0 );
120 text = "internal error";
124 locker = TXN_ID ( ltid );
126 lockid = TXN_ID( ltid );
130 opinfo.boi_txn = ltid;
132 op->o_private = &opinfo;
135 * Get the parent dn and see if the corresponding entry exists.
136 * If the parent does not exist, only allow the "root" user to
139 if ( be_issuffix( be, &e->e_nname ) ) {
142 dnParent( &e->e_nname, &pdn );
145 if( pdn.bv_len != 0 ) {
146 Entry *matched = NULL;
151 obj.data = pdn.bv_val-1;
152 obj.size = pdn.bv_len+1;
153 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
154 DB_LOCK_WRITE, &lock);
159 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0, locker, &lock );
165 case DB_LOCK_DEADLOCK:
166 case DB_LOCK_NOTGRANTED:
169 text = "ldap server busy";
173 text = "internal error";
178 char *matched_dn = NULL;
181 if ( matched != NULL ) {
182 matched_dn = ch_strdup( matched->e_dn );
183 refs = is_entry_referral( matched )
184 ? get_entry_referrals( be, conn, op, matched )
186 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched );
190 refs = referral_rewrite( default_referral,
191 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
195 LDAP_LOG ( OPERATION, DETAIL1,
196 "bdb_add: parent does not exist\n", 0, 0, 0 );
198 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
202 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
203 matched_dn, NULL, refs, NULL );
205 ber_bvarray_free( refs );
206 ch_free( matched_dn );
211 rc = access_allowed( be, conn, op, p,
212 children, NULL, ACL_WRITE, NULL );
214 switch( opinfo.boi_err ) {
215 case DB_LOCK_DEADLOCK:
216 case DB_LOCK_NOTGRANTED:
222 LDAP_LOG ( OPERATION, DETAIL1,
223 "bdb_add: no write access to parent\n", 0, 0, 0 );
225 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
228 rc = LDAP_INSUFFICIENT_ACCESS;
229 text = "no write access to parent";
230 goto return_results;;
233 #ifdef BDB_SUBENTRIES
234 if ( is_entry_subentry( p ) ) {
235 /* parent is a subentry, don't allow add */
237 LDAP_LOG ( OPERATION, DETAIL1,
238 "bdb_add: parent is subentry\n", 0, 0, 0 );
240 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
243 rc = LDAP_OBJECT_CLASS_VIOLATION;
244 text = "parent is a subentry";
245 goto return_results;;
249 if ( is_entry_alias( p ) ) {
250 /* parent is an alias, don't allow add */
252 LDAP_LOG ( OPERATION, DETAIL1,
253 "bdb_add: parent is alias\n", 0, 0, 0 );
255 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
258 rc = LDAP_ALIAS_PROBLEM;
259 text = "parent is an alias";
260 goto return_results;;
264 if ( is_entry_referral( p ) ) {
265 /* parent is a referral, don't allow add */
266 char *matched_dn = p->e_dn;
267 BerVarray refs = get_entry_referrals( be, conn, op, p );
270 LDAP_LOG ( OPERATION, DETAIL1,
271 "bdb_add: parent is referral\n", 0, 0, 0 );
273 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
277 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
278 matched_dn, NULL, refs, NULL );
280 ber_bvarray_free( refs );
281 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
286 #ifdef BDB_SUBENTRIES
289 /* parent must be an administrative point of the required kind */
293 /* free parent and reader lock */
294 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
300 * must be adding entry at suffix or with parent ""
302 if ( !be_isroot( be, &op->o_ndn )) {
303 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
304 || be_isupdate( be, &op->o_ndn ) )
306 p = (Entry *)&slap_entry_root;
308 /* check parent for "children" acl */
309 rc = access_allowed( be, conn, op, p,
310 children, NULL, ACL_WRITE, NULL );
314 switch( opinfo.boi_err ) {
315 case DB_LOCK_DEADLOCK:
316 case DB_LOCK_NOTGRANTED:
322 LDAP_LOG ( OPERATION, DETAIL1,
323 "bdb_add: no write access to parent\n", 0, 0, 0 );
325 Debug( LDAP_DEBUG_TRACE,
326 "bdb_add: no write access to parent\n",
329 rc = LDAP_INSUFFICIENT_ACCESS;
330 text = "no write access to parent";
331 goto return_results;;
336 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
337 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
339 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
340 pdn.bv_len == 0 ? "suffix" : "entry at root",
343 rc = LDAP_INSUFFICIENT_ACCESS;
348 #ifdef BDB_SUBENTRIES
351 LDAP_LOG ( OPERATION, DETAIL1,
352 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
354 Debug( LDAP_DEBUG_TRACE,
355 "bdb_add: no parent, cannot add subentry\n",
358 rc = LDAP_INSUFFICIENT_ACCESS;
359 text = "no parent, cannot add subentry";
360 goto return_results;;
368 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
369 DB_LOCK_WRITE, &lock);
374 rc = access_allowed( be, conn, op, e,
375 entry, NULL, ACL_WRITE, NULL );
377 switch( opinfo.boi_err ) {
378 case DB_LOCK_DEADLOCK:
379 case DB_LOCK_NOTGRANTED:
385 LDAP_LOG ( OPERATION, DETAIL1,
386 "bdb_add: no write access to entry\n", 0, 0, 0 );
388 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
391 rc = LDAP_INSUFFICIENT_ACCESS;
392 text = "no write access to entry";
393 goto return_results;;
397 rc = bdb_dn2id_add( be, ltid, &pdn, e );
400 LDAP_LOG ( OPERATION, ERR,
401 "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rc), rc, 0 );
403 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
404 db_strerror(rc), rc, 0 );
408 case DB_LOCK_DEADLOCK:
409 case DB_LOCK_NOTGRANTED:
412 rc = LDAP_ALREADY_EXISTS;
421 rc = bdb_id2entry_add( be, ltid, e );
424 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
426 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
430 case DB_LOCK_DEADLOCK:
431 case DB_LOCK_NOTGRANTED:
436 text = "entry store failed";
440 /* attribute indexes */
441 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
442 if ( rc != LDAP_SUCCESS ) {
444 LDAP_LOG ( OPERATION, ERR,
445 "bdb_add: index_entry_add failed\n", 0, 0, 0 );
447 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
451 case DB_LOCK_DEADLOCK:
452 case DB_LOCK_NOTGRANTED:
457 text = "index generation failed";
462 if (( rc=TXN_ABORT( ltid )) != 0 ) {
463 text = "txn_abort (no-op) failed";
470 char gid[DB_XIDDATASIZE];
472 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
473 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
475 if (( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
476 text = "txn_prepare failed";
479 int ret = bdb_cache_add_entry_rw(bdb->bi_dbenv,
480 &bdb->bi_cache, e, CACHE_WRITE_LOCK,
483 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
484 e, CACHE_WRITE_LOCK) != 0 )
489 case DB_LOCK_DEADLOCK:
490 case DB_LOCK_NOTGRANTED:
497 if(( rc=TXN_ABORT( ltid )) != 0 ) {
498 text = "cache add & txn_abort failed";
501 text = "cache add failed";
504 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
505 text = "txn_commit failed";
514 op->o_private = NULL;
516 if (rc == LDAP_SUCCESS) {
518 LDAP_LOG ( OPERATION, RESULTS,
519 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
520 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
522 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
523 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
527 bdb_cache_entry_commit( e );
532 LDAP_LOG ( OPERATION, ERR,
533 "bdb_add: %s : %s (%d)\n", text, db_strerror(rc), rc );
535 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
536 text, db_strerror(rc), rc );
542 send_ldap_result( conn, op, rc,
543 NULL, text, NULL, NULL );
545 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
546 ldap_pvt_thread_yield();
547 TXN_CHECKPOINT( bdb->bi_dbenv,
548 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
555 op->o_private = NULL;
558 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );