1 /* add.c - ldap BerkeleyDB back-end add routine */
4 * Copyright 1998-2000 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;
34 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
36 /* check entry's schema */
37 rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
38 if ( rc != LDAP_SUCCESS ) {
39 Debug( LDAP_DEBUG_TRACE,
40 "bdb_add: entry failed schema check: %s (%d)\n",
46 * acquire an ID outside of the operation transaction
47 * to avoid serializing adds.
49 rc = bdb_next_id( be, NULL, &e->e_id );
51 Debug( LDAP_DEBUG_TRACE,
52 "bdb_add: next_id failed (%d)\n",
55 text = "internal error";
60 /* transaction retry */
61 retry: rc = txn_abort( ltid );
66 text = "internal error";
71 /* begin transaction */
73 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
77 Debug( LDAP_DEBUG_TRACE,
78 "bdb_add: txn_begin failed: %s (%d)\n",
79 db_strerror(rc), rc, 0 );
81 text = "internal error";
87 opinfo.boi_txn = ltid;
89 op->o_private = &opinfo;
92 * Get the parent dn and see if the corresponding entry exists.
93 * If the parent does not exist, only allow the "root" user to
96 pdn = dn_parent( be, e->e_nname.bv_val );
98 if( pdn != NULL && *pdn != '\0' ) {
99 Entry *matched = NULL;
103 pbv.bv_len = e->e_nname.bv_len - (pdn - e->e_nname.bv_val);
106 rc = bdb_dn2entry( be, ltid, &pbv, &p, &matched, 0 );
112 case DB_LOCK_DEADLOCK:
113 case DB_LOCK_NOTGRANTED:
117 text = "internal error";
122 char *matched_dn = NULL;
123 struct berval **refs;
125 if ( matched != NULL ) {
126 matched_dn = ch_strdup( matched->e_dn );
127 refs = is_entry_referral( matched )
128 ? get_entry_referrals( be, conn, op, matched )
130 bdb_entry_return( be, matched );
134 refs = referral_rewrite( default_referral,
135 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
138 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
141 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
142 matched_dn, NULL, refs, NULL );
144 ber_bvecfree( refs );
145 ch_free( matched_dn );
150 if ( ! access_allowed( be, conn, op, p,
151 children, NULL, ACL_WRITE ) )
153 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
155 rc = LDAP_INSUFFICIENT_ACCESS;
156 text = "no write access to parent";
157 goto return_results;;
160 if ( is_entry_alias( p ) ) {
161 /* parent is an alias, don't allow add */
162 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
164 rc = LDAP_ALIAS_PROBLEM;
165 text = "parent is an alias";
166 goto return_results;;
169 if ( is_entry_referral( p ) ) {
170 /* parent is a referral, don't allow add */
171 char *matched_dn = ch_strdup( p->e_dn );
172 struct berval **refs = is_entry_referral( p )
173 ? get_entry_referrals( be, conn, op, p )
176 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
179 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
180 matched_dn, NULL, refs, NULL );
182 ber_bvecfree( refs );
187 /* free parent and writer lock */
188 bdb_entry_return( be, p );
194 * must be adding entry to at suffix
197 if ( !be_isroot( be, &op->o_ndn )) {
198 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
199 p = (Entry *)&slap_entry_root;
201 /* check parent for "children" acl */
202 rc = access_allowed( be, conn, op, p,
203 children, NULL, ACL_WRITE );
207 Debug( LDAP_DEBUG_TRACE,
208 "bdb_add: no write access to parent\n",
210 rc = LDAP_INSUFFICIENT_ACCESS;
211 text = "no write access to parent";
212 goto return_results;;
216 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
217 pdn == NULL ? "suffix" : "entry at root",
219 rc = LDAP_INSUFFICIENT_ACCESS;
226 rc = bdb_dn2id_add( be, ltid, pdn, e );
228 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
229 db_strerror(rc), rc, 0 );
232 case DB_LOCK_DEADLOCK:
233 case DB_LOCK_NOTGRANTED:
236 rc = LDAP_ALREADY_EXISTS;
245 rc = bdb_id2entry_add( be, ltid, e );
247 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
250 case DB_LOCK_DEADLOCK:
251 case DB_LOCK_NOTGRANTED:
256 text = "entry store failed";
260 /* attribute indexes */
261 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
262 if ( rc != LDAP_SUCCESS ) {
263 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
266 case DB_LOCK_DEADLOCK:
267 case DB_LOCK_NOTGRANTED:
272 text = "index generation failed";
277 rc = txn_commit( ltid, 0 );
280 op->o_private = NULL;
283 Debug( LDAP_DEBUG_TRACE,
284 "bdb_add: txn_commit failed: %s (%d)\n",
285 db_strerror(rc), rc, 0 );
287 text = "commit failed";
290 Debug( LDAP_DEBUG_TRACE,
291 "bdb_add: added id=%08lx dn=\"%s\"\n",
292 e->e_id, e->e_dn, 0 );
298 send_ldap_result( conn, op, rc,
299 NULL, text, NULL, NULL );
301 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
302 ldap_pvt_thread_yield();
303 TXN_CHECKPOINT( bdb->bi_dbenv,
304 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
309 /* free parent and writer lock */
310 bdb_entry_return( be, p );
315 op->o_private = NULL;