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>
16 static char bdb_gid[DB_XIDDATASIZE];
25 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30 char textbuf[SLAP_TEXT_BUFLEN];
31 size_t textlen = sizeof textbuf;
32 AttributeDescription *children = slap_schema.si_ad_children;
34 struct bdb_op_info opinfo;
43 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
45 /* check entry's schema */
46 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
47 if ( rc != LDAP_SUCCESS ) {
48 Debug( LDAP_DEBUG_TRACE,
49 "bdb_add: entry failed schema check: %s (%d)\n",
55 subentry = is_entry_subentry( e );
59 * acquire an ID outside of the operation transaction
60 * to avoid serializing adds.
62 rc = bdb_next_id( be, NULL, &e->e_id );
64 Debug( LDAP_DEBUG_TRACE,
65 "bdb_add: next_id failed (%d)\n",
68 text = "internal error";
73 retry: /* transaction retry */
74 rc = TXN_ABORT( ltid );
79 text = "internal error";
82 ldap_pvt_thread_yield();
85 /* begin transaction */
86 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
90 Debug( LDAP_DEBUG_TRACE,
91 "bdb_add: txn_begin failed: %s (%d)\n",
92 db_strerror(rc), rc, 0 );
94 text = "internal error";
98 lockid = TXN_ID( ltid );
102 opinfo.boi_txn = ltid;
104 op->o_private = &opinfo;
107 * Get the parent dn and see if the corresponding entry exists.
108 * If the parent does not exist, only allow the "root" user to
111 if ( be_issuffix( be, &e->e_nname ) ) {
114 dnParent( &e->e_nname, &pdn );
117 if( pdn.bv_len != 0 ) {
118 Entry *matched = NULL;
123 obj.data = pdn.bv_val-1;
124 obj.size = pdn.bv_len+1;
125 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
126 DB_LOCK_WRITE, &lock);
131 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0 );
137 case DB_LOCK_DEADLOCK:
138 case DB_LOCK_NOTGRANTED:
142 text = "internal error";
147 char *matched_dn = NULL;
150 if ( matched != NULL ) {
151 matched_dn = ch_strdup( matched->e_dn );
152 refs = is_entry_referral( matched )
153 ? get_entry_referrals( be, conn, op, matched )
155 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
159 refs = referral_rewrite( default_referral,
160 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
163 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
166 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
167 matched_dn, NULL, refs, NULL );
169 ber_bvarray_free( refs );
170 ch_free( matched_dn );
175 rc = access_allowed( be, conn, op, p,
176 children, NULL, ACL_WRITE );
178 switch( opinfo.boi_err ) {
179 case DB_LOCK_DEADLOCK:
180 case DB_LOCK_NOTGRANTED:
181 /* free parent and reader lock */
182 bdb_cache_return_entry_r( &bdb->bi_cache, p );
188 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
190 rc = LDAP_INSUFFICIENT_ACCESS;
191 text = "no write access to parent";
192 goto return_results;;
195 #ifdef BDB_SUBENTRIES
196 if ( is_entry_subentry( p ) ) {
197 /* parent is a subentry, don't allow add */
198 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
200 rc = LDAP_OBJECT_CLASS_VIOLATION;
201 text = "parent is a subentry";
202 goto return_results;;
206 if ( is_entry_alias( p ) ) {
207 /* parent is an alias, don't allow add */
208 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
210 rc = LDAP_ALIAS_PROBLEM;
211 text = "parent is an alias";
212 goto return_results;;
216 if ( is_entry_referral( p ) ) {
217 /* parent is a referral, don't allow add */
218 char *matched_dn = p->e_dn;
219 BerVarray refs = get_entry_referrals( be, conn, op, p );
221 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
224 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
225 matched_dn, NULL, refs, NULL );
227 ber_bvarray_free( refs );
228 bdb_cache_return_entry_r( &bdb->bi_cache, p );
233 #ifdef BDB_SUBENTRIES
236 /* parent must be an administrative point of the required kind */
240 /* free parent and reader lock */
241 bdb_cache_return_entry_r( &bdb->bi_cache, p );
247 * must be adding entry at suffix or with parent ""
249 if ( !be_isroot( be, &op->o_ndn )) {
250 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
251 || be_isupdate( be, &op->o_ndn ) ) {
252 p = (Entry *)&slap_entry_root;
254 /* check parent for "children" acl */
255 rc = access_allowed( be, conn, op, p,
256 children, NULL, ACL_WRITE );
259 switch( opinfo.boi_err ) {
260 case DB_LOCK_DEADLOCK:
261 case DB_LOCK_NOTGRANTED:
266 Debug( LDAP_DEBUG_TRACE,
267 "bdb_add: no write access to parent\n",
269 rc = LDAP_INSUFFICIENT_ACCESS;
270 text = "no write access to parent";
271 goto return_results;;
275 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
276 pdn.bv_len == 0 ? "suffix" : "entry at root",
278 rc = LDAP_INSUFFICIENT_ACCESS;
283 #ifdef BDB_SUBENTRIES
285 Debug( LDAP_DEBUG_TRACE,
286 "bdb_add: no parent, cannot add subentry\n",
288 rc = LDAP_INSUFFICIENT_ACCESS;
289 text = "no parent, cannot add subentry";
290 goto return_results;;
298 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
299 DB_LOCK_WRITE, &lock);
305 rc = bdb_dn2id_add( be, ltid, &pdn, e );
307 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
308 db_strerror(rc), rc, 0 );
311 case DB_LOCK_DEADLOCK:
312 case DB_LOCK_NOTGRANTED:
315 rc = LDAP_ALREADY_EXISTS;
324 rc = bdb_id2entry_add( be, ltid, e );
326 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
329 case DB_LOCK_DEADLOCK:
330 case DB_LOCK_NOTGRANTED:
335 text = "entry store failed";
339 /* attribute indexes */
340 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
341 if ( rc != LDAP_SUCCESS ) {
342 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
345 case DB_LOCK_DEADLOCK:
346 case DB_LOCK_NOTGRANTED:
351 text = "index generation failed";
356 if (( rc=TXN_ABORT( ltid )) != 0 ) {
357 text = "txn_abort (no-op) failed";
363 if (( rc=TXN_PREPARE( ltid, bdb_gid )) != 0 ) {
364 text = "txn_prepare failed";
367 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
368 e, CACHE_WRITE_LOCK) != 0 )
370 if(( rc=TXN_ABORT( ltid )) != 0 ) {
371 text = "cache add & txn_abort failed";
374 text = "cache add failed";
377 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
378 text = "txn_commit failed";
387 op->o_private = NULL;
389 if (rc == LDAP_SUCCESS) {
390 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
391 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
393 bdb_cache_entry_commit( e );
396 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
397 text, db_strerror(rc), rc );
402 send_ldap_result( conn, op, rc,
403 NULL, text, NULL, NULL );
405 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
406 ldap_pvt_thread_yield();
407 TXN_CHECKPOINT( bdb->bi_dbenv,
408 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
415 op->o_private = NULL;