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 */
80 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
84 Debug( LDAP_DEBUG_TRACE,
85 "bdb_add: txn_begin failed: %s (%d)\n",
86 db_strerror(rc), rc, 0 );
88 text = "internal error";
92 lockid = TXN_ID( ltid );
96 opinfo.boi_txn = ltid;
98 op->o_private = &opinfo;
101 * Get the parent dn and see if the corresponding entry exists.
102 * If the parent does not exist, only allow the "root" user to
105 if ( be_issuffix( be, &e->e_nname ) ) {
108 dnParent( &e->e_nname, &pdn );
111 if( pdn.bv_len != 0 ) {
112 Entry *matched = NULL;
117 obj.data = pdn.bv_val-1;
118 obj.size = pdn.bv_len+1;
119 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
120 DB_LOCK_WRITE, &lock);
125 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0 );
131 case DB_LOCK_DEADLOCK:
132 case DB_LOCK_NOTGRANTED:
136 text = "internal error";
141 char *matched_dn = NULL;
144 if ( matched != NULL ) {
145 matched_dn = ch_strdup( matched->e_dn );
146 refs = is_entry_referral( matched )
147 ? get_entry_referrals( be, conn, op, matched )
149 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
153 refs = referral_rewrite( default_referral,
154 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
157 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
160 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
161 matched_dn, NULL, refs, NULL );
163 ber_bvarray_free( refs );
164 ch_free( matched_dn );
169 rc = access_allowed( be, conn, op, p,
170 children, NULL, ACL_WRITE );
172 switch( opinfo.boi_err ) {
173 case DB_LOCK_DEADLOCK:
174 case DB_LOCK_NOTGRANTED:
175 /* free parent and reader lock */
176 bdb_cache_return_entry_r( &bdb->bi_cache, p );
182 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
184 rc = LDAP_INSUFFICIENT_ACCESS;
185 text = "no write access to parent";
186 goto return_results;;
189 if ( is_entry_subentry( p ) ) {
190 /* parent is a subentry, don't allow add */
191 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
193 rc = LDAP_OBJECT_CLASS_VIOLATION;
194 text = "parent is a subentry";
195 goto return_results;;
198 if ( is_entry_alias( p ) ) {
199 /* parent is an alias, don't allow add */
200 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
202 rc = LDAP_ALIAS_PROBLEM;
203 text = "parent is an alias";
204 goto return_results;;
207 if ( is_entry_referral( p ) ) {
208 /* parent is a referral, don't allow add */
209 char *matched_dn = p->e_dn;
210 BerVarray refs = get_entry_referrals( be, conn, op, p );
212 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
215 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
216 matched_dn, NULL, refs, NULL );
218 ber_bvarray_free( refs );
219 bdb_cache_return_entry_r( &bdb->bi_cache, p );
226 /* parent must be an administrative point of the required kind */
229 /* free parent and reader lock */
230 bdb_cache_return_entry_r( &bdb->bi_cache, p );
236 * must be adding entry at suffix or with parent ""
238 if ( !be_isroot( be, &op->o_ndn )) {
239 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
240 || be_isupdate( be, &op->o_ndn ) ) {
241 p = (Entry *)&slap_entry_root;
243 /* check parent for "children" acl */
244 rc = access_allowed( be, conn, op, p,
245 children, NULL, ACL_WRITE );
248 switch( opinfo.boi_err ) {
249 case DB_LOCK_DEADLOCK:
250 case DB_LOCK_NOTGRANTED:
255 Debug( LDAP_DEBUG_TRACE,
256 "bdb_add: no write access to parent\n",
258 rc = LDAP_INSUFFICIENT_ACCESS;
259 text = "no write access to parent";
260 goto return_results;;
264 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
265 pdn.bv_len == 0 ? "suffix" : "entry at root",
267 rc = LDAP_INSUFFICIENT_ACCESS;
273 Debug( LDAP_DEBUG_TRACE,
274 "bdb_add: no parent, cannot add subentry\n",
276 rc = LDAP_INSUFFICIENT_ACCESS;
277 text = "no parent, cannot add subentry";
278 goto return_results;;
285 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
286 DB_LOCK_WRITE, &lock);
292 rc = bdb_dn2id_add( be, ltid, &pdn, e );
294 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
295 db_strerror(rc), rc, 0 );
298 case DB_LOCK_DEADLOCK:
299 case DB_LOCK_NOTGRANTED:
302 rc = LDAP_ALREADY_EXISTS;
311 rc = bdb_id2entry_add( be, ltid, e );
313 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
316 case DB_LOCK_DEADLOCK:
317 case DB_LOCK_NOTGRANTED:
322 text = "entry store failed";
326 /* attribute indexes */
327 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
328 if ( rc != LDAP_SUCCESS ) {
329 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
332 case DB_LOCK_DEADLOCK:
333 case DB_LOCK_NOTGRANTED:
338 text = "index generation failed";
342 rc = txn_commit( ltid, 0 );
344 op->o_private = NULL;
347 Debug( LDAP_DEBUG_TRACE,
348 "bdb_add: txn_commit failed: %s (%d)\n",
349 db_strerror(rc), rc, 0 );
351 text = "commit failed";
354 /* add the entry to the entry cache */
355 /* we should add to cache only upon free of txn-abort */
356 if (bdb_cache_add_entry_rw(&bdb->bi_cache, e, CACHE_WRITE_LOCK) != 0) {
357 text = "cache add failed";
361 Debug( LDAP_DEBUG_TRACE,
362 "bdb_add: added id=%08lx dn=\"%s\"\n",
363 e->e_id, e->e_dn, 0 );
368 bdb_cache_entry_commit (e);
371 send_ldap_result( conn, op, rc,
372 NULL, text, NULL, NULL );
374 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
375 ldap_pvt_thread_yield();
376 TXN_CHECKPOINT( bdb->bi_dbenv,
377 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
384 op->o_private = NULL;