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 AttributeDescription *children = slap_schema.si_ad_children;
30 struct bdb_op_info opinfo;
32 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
34 /* check entry's schema */
35 rc = entry_schema_check( e, NULL, &text );
36 if ( rc != LDAP_SUCCESS ) {
37 Debug( LDAP_DEBUG_TRACE,
38 "bdb_add: entry failed schema check: %s (%d)\n",
44 * acquire an ID outside of the operation transaction
45 * to avoid serializing adds.
47 rc = bdb_next_id( be, NULL, &e->e_id );
49 Debug( LDAP_DEBUG_TRACE,
50 "bdb_add: next_id failed (%d)\n",
53 text = "internal error";
58 /* transaction retry */
59 retry: rc = txn_abort( ltid );
64 text = "internal error";
69 /* begin transaction */
70 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
73 Debug( LDAP_DEBUG_TRACE,
74 "bdb_add: txn_begin failed: %s (%d)\n",
75 db_strerror(rc), rc, 0 );
77 text = "internal error";
82 opinfo.boi_txn = ltid;
84 op->o_private = &opinfo;
87 * Get the parent dn and see if the corresponding entry exists.
88 * If the parent does not exist, only allow the "root" user to
91 pdn = dn_parent( be, e->e_ndn );
93 if( pdn != NULL && *pdn != '\0' ) {
94 Entry *matched = NULL;
97 rc = bdb_dn2entry( be, ltid, pdn, &p, &matched, 0 );
104 case DB_LOCK_DEADLOCK:
105 case DB_LOCK_NOTGRANTED:
109 text = "internal error";
115 struct berval **refs;
117 if ( matched != NULL ) {
118 matched_dn = ch_strdup( matched->e_dn );
119 refs = is_entry_referral( matched )
120 ? get_entry_referrals( be, conn, op, matched )
122 bdb_entry_return( be, matched );
127 refs = default_referral;
130 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
133 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
134 matched_dn, NULL, refs, NULL );
136 if( matched != NULL ) {
137 ber_bvecfree( refs );
138 ch_free( matched_dn );
144 if ( ! access_allowed( be, conn, op, p,
145 children, NULL, ACL_WRITE ) )
147 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
149 rc = LDAP_INSUFFICIENT_ACCESS;
150 text = "no write access to parent", NULL, NULL;
151 goto return_results;;
154 if ( is_entry_alias( p ) ) {
155 /* parent is an alias, don't allow add */
156 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
158 rc = LDAP_ALIAS_PROBLEM;
159 text = "parent is an alias";
160 goto return_results;;
163 if ( is_entry_referral( p ) ) {
164 /* parent is a referral, don't allow add */
165 char *matched_dn = ch_strdup( p->e_dn );
166 struct berval **refs = is_entry_referral( p )
167 ? get_entry_referrals( be, conn, op, p )
170 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
173 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
174 matched_dn, NULL, refs, NULL );
176 ber_bvecfree( refs );
181 /* free parent and writer lock */
182 bdb_entry_return( be, p );
192 * must be adding entry to at suffix
195 if ( !be_isroot( be, op->o_ndn )) {
196 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
197 pdn == NULL ? "suffix" : "entry at root",
199 rc = LDAP_INSUFFICIENT_ACCESS;
205 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
207 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
208 db_strerror(rc), rc, 0 );
211 case DB_LOCK_DEADLOCK:
212 case DB_LOCK_NOTGRANTED:
215 rc = LDAP_ALREADY_EXISTS;
224 rc = bdb_id2entry_add( be, ltid, e );
226 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
229 case DB_LOCK_DEADLOCK:
230 case DB_LOCK_NOTGRANTED:
235 text = "entry store failed";
240 /* attribute indexes */
241 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
242 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
245 case DB_LOCK_DEADLOCK:
246 case DB_LOCK_NOTGRANTED:
251 text = "index generation failed";
256 rc = txn_commit( ltid, 0 );
258 op->o_private = NULL;
261 Debug( LDAP_DEBUG_TRACE,
262 "bdb_add: txn_commit failed: %s (%d)\n",
263 db_strerror(rc), rc, 0 );
265 text = "commit failed";
267 Debug( LDAP_DEBUG_TRACE,
268 "bdb_add: added id=%08x dn=\"%s\"\n",
269 e->e_id, e->e_dn, 0 );
275 send_ldap_result( conn, op, rc,
276 NULL, text, NULL, NULL );
278 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
279 ldap_pvt_thread_yield();
280 txn_checkpoint( bdb->bi_dbenv,
281 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
286 /* free parent and writer lock */
287 bdb_entry_return( be, p );
292 op->o_private = NULL;