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 (( "add", LDAP_LEVEL_ARGS, "==> bdb_add: %s\n", e->e_dn ));
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 (( "add", LDAP_LEVEL_ERR, "bdb_add: entry failed schema check: %s (%d)\n", text, rc ));
55 Debug( LDAP_DEBUG_TRACE,
56 "bdb_add: entry failed schema check: %s (%d)\n",
63 subentry = is_entry_subentry( e );
67 * acquire an ID outside of the operation transaction
68 * to avoid serializing adds.
70 rc = bdb_next_id( be, NULL, &e->e_id );
73 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: next_id failed (%d)\n", rc ));
75 Debug( LDAP_DEBUG_TRACE,
76 "bdb_add: next_id failed (%d)\n",
80 text = "internal error";
85 retry: /* transaction retry */
86 rc = TXN_ABORT( ltid );
91 text = "internal error";
94 ldap_pvt_thread_yield();
97 /* begin transaction */
98 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
103 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rc), rc ));
105 Debug( LDAP_DEBUG_TRACE,
106 "bdb_add: txn_begin failed: %s (%d)\n",
107 db_strerror(rc), rc, 0 );
110 text = "internal error";
114 locker = TXN_ID ( ltid );
116 lockid = TXN_ID( ltid );
120 opinfo.boi_txn = ltid;
122 op->o_private = &opinfo;
125 * Get the parent dn and see if the corresponding entry exists.
126 * If the parent does not exist, only allow the "root" user to
129 if ( be_issuffix( be, &e->e_nname ) ) {
132 dnParent( &e->e_nname, &pdn );
135 if( pdn.bv_len != 0 ) {
136 Entry *matched = NULL;
141 obj.data = pdn.bv_val-1;
142 obj.size = pdn.bv_len+1;
143 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
144 DB_LOCK_WRITE, &lock);
149 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0, locker, &lock );
155 case DB_LOCK_DEADLOCK:
156 case DB_LOCK_NOTGRANTED:
159 text = "ldap server busy";
163 text = "internal error";
168 char *matched_dn = NULL;
171 if ( matched != NULL ) {
172 matched_dn = ch_strdup( matched->e_dn );
173 refs = is_entry_referral( matched )
174 ? get_entry_referrals( be, conn, op, matched )
176 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched );
180 refs = referral_rewrite( default_referral,
181 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
185 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: parent does not exist\n" ));
187 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
191 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
192 matched_dn, NULL, refs, NULL );
194 ber_bvarray_free( refs );
195 ch_free( matched_dn );
200 rc = access_allowed( be, conn, op, p,
201 children, NULL, ACL_WRITE, NULL );
203 switch( opinfo.boi_err ) {
204 case DB_LOCK_DEADLOCK:
205 case DB_LOCK_NOTGRANTED:
206 /* free parent and reader lock */
207 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
214 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: no write access to parent\n" ));
216 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
219 rc = LDAP_INSUFFICIENT_ACCESS;
220 text = "no write access to parent";
221 goto return_results;;
224 #ifdef BDB_SUBENTRIES
225 if ( is_entry_subentry( p ) ) {
226 /* parent is a subentry, don't allow add */
228 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: parent is subentry\n" ));
230 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
233 rc = LDAP_OBJECT_CLASS_VIOLATION;
234 text = "parent is a subentry";
235 goto return_results;;
239 if ( is_entry_alias( p ) ) {
240 /* parent is an alias, don't allow add */
242 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: parent is alias\n" ));
244 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
247 rc = LDAP_ALIAS_PROBLEM;
248 text = "parent is an alias";
249 goto return_results;;
253 if ( is_entry_referral( p ) ) {
254 /* parent is a referral, don't allow add */
255 char *matched_dn = p->e_dn;
256 BerVarray refs = get_entry_referrals( be, conn, op, p );
259 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: parent is referral\n" ));
261 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
265 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
266 matched_dn, NULL, refs, NULL );
268 ber_bvarray_free( refs );
269 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
274 #ifdef BDB_SUBENTRIES
277 /* parent must be an administrative point of the required kind */
281 /* free parent and reader lock */
282 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
288 * must be adding entry at suffix or with parent ""
290 if ( !be_isroot( be, &op->o_ndn )) {
291 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
292 || be_isupdate( be, &op->o_ndn ) ) {
293 p = (Entry *)&slap_entry_root;
295 /* check parent for "children" acl */
296 rc = access_allowed( be, conn, op, p,
297 children, NULL, ACL_WRITE, NULL );
300 switch( opinfo.boi_err ) {
301 case DB_LOCK_DEADLOCK:
302 case DB_LOCK_NOTGRANTED:
308 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: no write access to parent\n" ));
310 Debug( LDAP_DEBUG_TRACE,
311 "bdb_add: no write access to parent\n",
314 rc = LDAP_INSUFFICIENT_ACCESS;
315 text = "no write access to parent";
316 goto return_results;;
321 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: %s denied\n", pdn.bv_len == 0 ? "suffix" : "entry at root" ));
323 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
324 pdn.bv_len == 0 ? "suffix" : "entry at root",
327 rc = LDAP_INSUFFICIENT_ACCESS;
332 #ifdef BDB_SUBENTRIES
335 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: no parent, cannot add subentry\n" ));
337 Debug( LDAP_DEBUG_TRACE,
338 "bdb_add: no parent, cannot add subentry\n",
341 rc = LDAP_INSUFFICIENT_ACCESS;
342 text = "no parent, cannot add subentry";
343 goto return_results;;
351 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
352 DB_LOCK_WRITE, &lock);
358 rc = bdb_dn2id_add( be, ltid, &pdn, e );
361 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rc), rc ));
363 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
364 db_strerror(rc), rc, 0 );
368 case DB_LOCK_DEADLOCK:
369 case DB_LOCK_NOTGRANTED:
372 rc = LDAP_ALREADY_EXISTS;
381 rc = bdb_id2entry_add( be, ltid, e );
384 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: id2entry_add failed\n" ));
386 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
390 case DB_LOCK_DEADLOCK:
391 case DB_LOCK_NOTGRANTED:
396 text = "entry store failed";
400 /* attribute indexes */
401 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
402 if ( rc != LDAP_SUCCESS ) {
404 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: index_entry_add failed\n" ));
406 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
410 case DB_LOCK_DEADLOCK:
411 case DB_LOCK_NOTGRANTED:
416 text = "index generation failed";
421 if (( rc=TXN_ABORT( ltid )) != 0 ) {
422 text = "txn_abort (no-op) failed";
428 char gid[DB_XIDDATASIZE];
430 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
431 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
433 if (( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
434 text = "txn_prepare failed";
437 ret = bdb_cache_add_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, CACHE_WRITE_LOCK, locker, &lock);
439 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
440 e, CACHE_WRITE_LOCK) != 0 )
445 case DB_LOCK_DEADLOCK:
446 case DB_LOCK_NOTGRANTED:
453 if(( rc=TXN_ABORT( ltid )) != 0 ) {
454 text = "cache add & txn_abort failed";
457 text = "cache add failed";
460 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
461 text = "txn_commit failed";
470 op->o_private = NULL;
472 if (rc == LDAP_SUCCESS) {
474 LDAP_LOG (( "add", LDAP_LEVEL_RESULTS, "bdb_add: added%s id=%08lx dn=\"%s\"\n", op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn ));
476 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
477 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
480 bdb_cache_entry_commit( e );
484 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: %s : %s (%d)\n", text, db_strerror(rc), rc ));
486 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
487 text, db_strerror(rc), rc );
493 send_ldap_result( conn, op, rc,
494 NULL, text, NULL, NULL );
496 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
497 ldap_pvt_thread_yield();
498 TXN_CHECKPOINT( bdb->bi_dbenv,
499 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
506 op->o_private = NULL;