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 /* transaction retry */
68 retry: 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.bv_val ) ) {
111 rc = dnParent( e->e_nname.bv_val, (const char **)&pdn.bv_val );
112 if ( rc != LDAP_SUCCESS ) {
113 text = "internal error";
116 pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_nname.bv_val);
119 if( pdn.bv_len != 0 ) {
120 Entry *matched = NULL;
125 obj.data = pdn.bv_val-1;
126 obj.size = pdn.bv_len+1;
127 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
128 DB_LOCK_WRITE, &lock);
133 rc = bdb_dn2entry( be, ltid, &pdn, &p, &matched, 0 );
139 case DB_LOCK_DEADLOCK:
140 case DB_LOCK_NOTGRANTED:
144 text = "internal error";
149 char *matched_dn = NULL;
152 if ( matched != NULL ) {
153 matched_dn = ch_strdup( matched->e_dn );
154 refs = is_entry_referral( matched )
155 ? get_entry_referrals( be, conn, op, matched )
157 bdb_entry_return( be, matched );
161 refs = referral_rewrite( default_referral,
162 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
165 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
168 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
169 matched_dn, NULL, refs, NULL );
171 ber_bvarray_free( refs );
172 ch_free( matched_dn );
177 if ( ! access_allowed( be, conn, op, p,
178 children, NULL, ACL_WRITE ) )
180 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
182 rc = LDAP_INSUFFICIENT_ACCESS;
183 text = "no write access to parent";
184 goto return_results;;
187 if ( is_entry_subentry( p ) ) {
188 /* parent is a subentry, don't allow add */
189 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
191 rc = LDAP_OBJECT_CLASS_VIOLATION;
192 text = "parent is a subentry";
193 goto return_results;;
196 if ( is_entry_alias( p ) ) {
197 /* parent is an alias, don't allow add */
198 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
200 rc = LDAP_ALIAS_PROBLEM;
201 text = "parent is an alias";
202 goto return_results;;
205 if ( is_entry_referral( p ) ) {
206 /* parent is a referral, don't allow add */
207 char *matched_dn = ch_strdup( p->e_dn );
208 BerVarray refs = is_entry_referral( p )
209 ? 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 );
225 /* parent must be an administrative point of the required kind */
228 /* free parent and writer lock */
229 bdb_entry_return( be, p );
235 * must be adding entry at suffix or with parent ""
237 if ( !be_isroot( be, &op->o_ndn )) {
238 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
239 p = (Entry *)&slap_entry_root;
241 /* check parent for "children" acl */
242 rc = access_allowed( be, conn, op, p,
243 children, NULL, ACL_WRITE );
247 Debug( LDAP_DEBUG_TRACE,
248 "bdb_add: no write access to parent\n",
250 rc = LDAP_INSUFFICIENT_ACCESS;
251 text = "no write access to parent";
252 goto return_results;;
256 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
257 pdn.bv_len == 0 ? "suffix" : "entry at root",
259 rc = LDAP_INSUFFICIENT_ACCESS;
265 Debug( LDAP_DEBUG_TRACE,
266 "bdb_add: no parent, cannot add subentry\n",
268 rc = LDAP_INSUFFICIENT_ACCESS;
269 text = "no parent, cannot add subentry";
270 goto return_results;;
277 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
278 DB_LOCK_WRITE, &lock);
284 rc = bdb_dn2id_add( be, ltid, &pdn, e );
286 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
287 db_strerror(rc), rc, 0 );
290 case DB_LOCK_DEADLOCK:
291 case DB_LOCK_NOTGRANTED:
294 rc = LDAP_ALREADY_EXISTS;
303 rc = bdb_id2entry_add( be, ltid, e );
305 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
308 case DB_LOCK_DEADLOCK:
309 case DB_LOCK_NOTGRANTED:
314 text = "entry store failed";
318 /* attribute indexes */
319 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
320 if ( rc != LDAP_SUCCESS ) {
321 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
324 case DB_LOCK_DEADLOCK:
325 case DB_LOCK_NOTGRANTED:
330 text = "index generation failed";
335 rc = txn_commit( ltid, 0 );
338 op->o_private = NULL;
341 Debug( LDAP_DEBUG_TRACE,
342 "bdb_add: txn_commit failed: %s (%d)\n",
343 db_strerror(rc), rc, 0 );
345 text = "commit failed";
348 Debug( LDAP_DEBUG_TRACE,
349 "bdb_add: added id=%08lx dn=\"%s\"\n",
350 e->e_id, e->e_dn, 0 );
356 send_ldap_result( conn, op, rc,
357 NULL, text, NULL, NULL );
359 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
360 ldap_pvt_thread_yield();
361 TXN_CHECKPOINT( bdb->bi_dbenv,
362 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
367 /* free parent and writer lock */
368 bdb_entry_return( be, p );
373 op->o_private = NULL;