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;
39 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
41 /* check entry's schema */
42 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
43 if ( rc != LDAP_SUCCESS ) {
44 Debug( LDAP_DEBUG_TRACE,
45 "bdb_add: entry failed schema check: %s (%d)\n",
50 subentry = is_entry_subentry( e );
53 * acquire an ID outside of the operation transaction
54 * to avoid serializing adds.
56 rc = bdb_next_id( be, NULL, &e->e_id );
58 Debug( LDAP_DEBUG_TRACE,
59 "bdb_add: next_id failed (%d)\n",
62 text = "internal error";
67 retry: /* transaction retry */
68 rc = txn_abort( ltid );
73 text = "internal error";
76 ldap_pvt_thread_yield();
79 /* begin transaction */
81 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
85 Debug( LDAP_DEBUG_TRACE,
86 "bdb_add: txn_begin failed: %s (%d)\n",
87 db_strerror(rc), rc, 0 );
89 text = "internal error";
93 lockid = TXN_ID( ltid );
98 opinfo.boi_txn = ltid;
100 op->o_private = &opinfo;
103 * Get the parent dn and see if the corresponding entry exists.
104 * If the parent does not exist, only allow the "root" user to
107 if ( be_issuffix( be, &e->e_nname ) ) {
110 rc = dnParent( &e->e_nname, &pdn );
111 if ( rc != LDAP_SUCCESS ) {
112 text = "internal error";
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 if ( is_entry_subentry( p ) ) {
196 /* parent is a subentry, don't allow add */
197 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
199 rc = LDAP_OBJECT_CLASS_VIOLATION;
200 text = "parent is a subentry";
201 goto return_results;;
204 if ( is_entry_alias( p ) ) {
205 /* parent is an alias, don't allow add */
206 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
208 rc = LDAP_ALIAS_PROBLEM;
209 text = "parent is an alias";
210 goto return_results;;
213 if ( is_entry_referral( p ) ) {
214 /* parent is a referral, don't allow add */
215 char *matched_dn = p->e_dn;
216 BerVarray refs = get_entry_referrals( be, conn, op, p );
218 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
221 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
222 matched_dn, NULL, refs, NULL );
224 ber_bvarray_free( refs );
225 bdb_cache_return_entry_r( &bdb->bi_cache, p );
232 /* parent must be an administrative point of the required kind */
235 /* free parent and reader lock */
236 bdb_cache_return_entry_r( &bdb->bi_cache, p );
242 * must be adding entry at suffix or with parent ""
244 if ( !be_isroot( be, &op->o_ndn )) {
245 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
246 || be_isupdate( be, &op->o_ndn ) ) {
247 p = (Entry *)&slap_entry_root;
249 /* check parent for "children" acl */
250 rc = access_allowed( be, conn, op, p,
251 children, NULL, ACL_WRITE );
254 switch( opinfo.boi_err ) {
255 case DB_LOCK_DEADLOCK:
256 case DB_LOCK_NOTGRANTED:
261 Debug( LDAP_DEBUG_TRACE,
262 "bdb_add: no write access to parent\n",
264 rc = LDAP_INSUFFICIENT_ACCESS;
265 text = "no write access to parent";
266 goto return_results;;
270 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
271 pdn.bv_len == 0 ? "suffix" : "entry at root",
273 rc = LDAP_INSUFFICIENT_ACCESS;
279 Debug( LDAP_DEBUG_TRACE,
280 "bdb_add: no parent, cannot add subentry\n",
282 rc = LDAP_INSUFFICIENT_ACCESS;
283 text = "no parent, cannot add subentry";
284 goto return_results;;
291 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
292 DB_LOCK_WRITE, &lock);
298 rc = bdb_dn2id_add( be, ltid, &pdn, e );
300 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
301 db_strerror(rc), rc, 0 );
304 case DB_LOCK_DEADLOCK:
305 case DB_LOCK_NOTGRANTED:
308 rc = LDAP_ALREADY_EXISTS;
317 rc = bdb_id2entry_add( be, ltid, e );
319 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
322 case DB_LOCK_DEADLOCK:
323 case DB_LOCK_NOTGRANTED:
328 text = "entry store failed";
332 /* attribute indexes */
333 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
334 if ( rc != LDAP_SUCCESS ) {
335 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
338 case DB_LOCK_DEADLOCK:
339 case DB_LOCK_NOTGRANTED:
344 text = "index generation failed";
349 rc = txn_commit( ltid, 0 );
352 op->o_private = NULL;
355 Debug( LDAP_DEBUG_TRACE,
356 "bdb_add: txn_commit failed: %s (%d)\n",
357 db_strerror(rc), rc, 0 );
359 text = "commit failed";
362 /* add the entry to the entry cache */
363 /* we should add to cache only upon free of txn-abort */
364 if (bdb_cache_add_entry_rw(&bdb->bi_cache, e, CACHE_WRITE_LOCK) != 0) {
365 text = "cache add failed";
369 Debug( LDAP_DEBUG_TRACE,
370 "bdb_add: added id=%08lx dn=\"%s\"\n",
371 e->e_id, e->e_dn, 0 );
376 bdb_cache_entry_commit (e);
379 send_ldap_result( conn, op, rc,
380 NULL, text, NULL, NULL );
382 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
383 ldap_pvt_thread_yield();
384 TXN_CHECKPOINT( bdb->bi_dbenv,
385 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
392 op->o_private = NULL;