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;
35 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
37 /* check entry's schema */
38 rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
39 if ( rc != LDAP_SUCCESS ) {
40 Debug( LDAP_DEBUG_TRACE,
41 "bdb_add: entry failed schema check: %s (%d)\n",
46 subentry = is_entry_subentry( e );
49 * acquire an ID outside of the operation transaction
50 * to avoid serializing adds.
52 rc = bdb_next_id( be, NULL, &e->e_id );
54 Debug( LDAP_DEBUG_TRACE,
55 "bdb_add: next_id failed (%d)\n",
58 text = "internal error";
63 /* transaction retry */
64 retry: rc = txn_abort( ltid );
69 text = "internal error";
74 /* begin transaction */
76 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
80 Debug( LDAP_DEBUG_TRACE,
81 "bdb_add: txn_begin failed: %s (%d)\n",
82 db_strerror(rc), rc, 0 );
84 text = "internal error";
90 opinfo.boi_txn = ltid;
92 op->o_private = &opinfo;
95 * Get the parent dn and see if the corresponding entry exists.
96 * If the parent does not exist, only allow the "root" user to
99 pdn.bv_val = dn_parent( be, e->e_ndn );
100 if (pdn.bv_val && *pdn.bv_val) {
101 pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_ndn);
106 if( pdn.bv_len != 0 ) {
107 Entry *matched = NULL;
110 rc = bdb_dn2entry( be, ltid, &pdn, &p, &matched, 0 );
116 case DB_LOCK_DEADLOCK:
117 case DB_LOCK_NOTGRANTED:
121 text = "internal error";
126 char *matched_dn = NULL;
129 if ( matched != NULL ) {
130 matched_dn = ch_strdup( matched->e_dn );
131 refs = is_entry_referral( matched )
132 ? get_entry_referrals( be, conn, op, matched )
134 bdb_entry_return( be, matched );
138 refs = referral_rewrite( default_referral,
139 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
142 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
145 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
146 matched_dn, NULL, refs, NULL );
148 bvarray_free( refs );
149 ch_free( matched_dn );
154 if ( ! access_allowed( be, conn, op, p,
155 children, NULL, ACL_WRITE ) )
157 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
159 rc = LDAP_INSUFFICIENT_ACCESS;
160 text = "no write access to parent";
161 goto return_results;;
164 if ( is_entry_subentry( p ) ) {
165 /* parent is a subentry, don't allow add */
166 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
168 rc = LDAP_OBJECT_CLASS_VIOLATION;
169 text = "parent is a subentry";
170 goto return_results;;
173 if ( is_entry_alias( p ) ) {
174 /* parent is an alias, don't allow add */
175 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
177 rc = LDAP_ALIAS_PROBLEM;
178 text = "parent is an alias";
179 goto return_results;;
182 if ( is_entry_referral( p ) ) {
183 /* parent is a referral, don't allow add */
184 char *matched_dn = ch_strdup( p->e_dn );
185 BVarray refs = is_entry_referral( p )
186 ? get_entry_referrals( be, conn, op, p )
189 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
192 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
193 matched_dn, NULL, refs, NULL );
195 bvarray_free( refs );
202 /* parent must be an administrative point of the required kind */
205 /* free parent and writer lock */
206 bdb_entry_return( be, p );
212 * must be adding entry at suffix or with parent ""
214 if ( !be_isroot( be, &op->o_ndn )) {
215 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
216 p = (Entry *)&slap_entry_root;
218 /* check parent for "children" acl */
219 rc = access_allowed( be, conn, op, p,
220 children, NULL, ACL_WRITE );
224 Debug( LDAP_DEBUG_TRACE,
225 "bdb_add: no write access to parent\n",
227 rc = LDAP_INSUFFICIENT_ACCESS;
228 text = "no write access to parent";
229 goto return_results;;
233 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
234 pdn.bv_len == 0 ? "suffix" : "entry at root",
236 rc = LDAP_INSUFFICIENT_ACCESS;
242 Debug( LDAP_DEBUG_TRACE,
243 "bdb_add: no parent, cannot add subentry\n",
245 rc = LDAP_INSUFFICIENT_ACCESS;
246 text = "no parent, cannot add subentry";
247 goto return_results;;
252 rc = bdb_dn2id_add( be, ltid, &pdn, e );
254 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
255 db_strerror(rc), rc, 0 );
258 case DB_LOCK_DEADLOCK:
259 case DB_LOCK_NOTGRANTED:
262 rc = LDAP_ALREADY_EXISTS;
271 rc = bdb_id2entry_add( be, ltid, e );
273 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
276 case DB_LOCK_DEADLOCK:
277 case DB_LOCK_NOTGRANTED:
282 text = "entry store failed";
286 /* attribute indexes */
287 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
288 if ( rc != LDAP_SUCCESS ) {
289 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
292 case DB_LOCK_DEADLOCK:
293 case DB_LOCK_NOTGRANTED:
298 text = "index generation failed";
303 rc = txn_commit( ltid, 0 );
306 op->o_private = NULL;
309 Debug( LDAP_DEBUG_TRACE,
310 "bdb_add: txn_commit failed: %s (%d)\n",
311 db_strerror(rc), rc, 0 );
313 text = "commit failed";
316 Debug( LDAP_DEBUG_TRACE,
317 "bdb_add: added id=%08lx dn=\"%s\"\n",
318 e->e_id, e->e_dn, 0 );
324 send_ldap_result( conn, op, rc,
325 NULL, text, NULL, NULL );
327 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
328 ldap_pvt_thread_yield();
329 TXN_CHECKPOINT( bdb->bi_dbenv,
330 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
335 /* free parent and writer lock */
336 bdb_entry_return( be, p );
341 op->o_private = NULL;