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;
27 const char *text = NULL;
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 );
72 Debug( LDAP_DEBUG_TRACE,
73 "bdb_add: txn_begin failed: %s (%d)\n",
74 db_strerror(rc), rc, 0 );
76 text = "internal error";
81 opinfo.boi_txn = ltid;
83 op->o_private = &opinfo;
86 * Get the parent dn and see if the corresponding entry exists.
87 * If the parent does not exist, only allow the "root" user to
90 pdn = dn_parent( be, e->e_ndn );
92 if( pdn != NULL && *pdn != '\0' ) {
93 Entry *matched = NULL;
96 rc = bdb_dn2entry( be, ltid, pdn, &p, &matched, 0 );
103 case DB_LOCK_DEADLOCK:
104 case DB_LOCK_NOTGRANTED:
108 text = "internal error";
114 struct berval **refs;
116 if ( matched != NULL ) {
117 matched_dn = ch_strdup( matched->e_dn );
118 refs = is_entry_referral( matched )
119 ? get_entry_referrals( be, conn, op, matched )
121 bdb_entry_return( be, matched );
125 refs = default_referral;
128 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
131 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
132 matched_dn, NULL, refs, NULL );
134 if( matched != NULL ) {
135 ber_bvecfree( refs );
136 ch_free( matched_dn );
142 if ( ! access_allowed( be, conn, op, p,
143 children, NULL, ACL_WRITE ) )
145 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
147 rc = LDAP_INSUFFICIENT_ACCESS;
148 text = "no write access to parent", NULL, NULL;
149 goto return_results;;
152 if ( is_entry_alias( p ) ) {
153 /* parent is an alias, don't allow add */
154 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
156 rc = LDAP_ALIAS_PROBLEM;
157 text = "parent is an alias";
158 goto return_results;;
161 if ( is_entry_referral( p ) ) {
162 /* parent is a referral, don't allow add */
163 char *matched_dn = ch_strdup( p->e_dn );
164 struct berval **refs = is_entry_referral( p )
165 ? get_entry_referrals( be, conn, op, p )
168 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
171 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
172 matched_dn, NULL, refs, NULL );
174 ber_bvecfree( refs );
179 /* free parent and writer lock */
180 bdb_entry_return( be, p );
190 * must be adding entry to at suffix
193 if ( !be_isroot( be, op->o_ndn )) {
194 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
195 pdn == NULL ? "suffix" : "entry at root",
197 rc = LDAP_INSUFFICIENT_ACCESS;
203 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
205 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
206 db_strerror(rc), rc, 0 );
209 case DB_LOCK_DEADLOCK:
210 case DB_LOCK_NOTGRANTED:
213 rc = LDAP_ALREADY_EXISTS;
222 rc = bdb_id2entry_add( be, ltid, e );
224 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
227 case DB_LOCK_DEADLOCK:
228 case DB_LOCK_NOTGRANTED:
233 text = "entry store failed";
238 /* attribute indexes */
239 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
240 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
243 case DB_LOCK_DEADLOCK:
244 case DB_LOCK_NOTGRANTED:
249 text = "index generation failed";
254 rc = txn_commit( ltid, 0 );
256 op->o_private = NULL;
259 Debug( LDAP_DEBUG_TRACE,
260 "bdb_add: txn_commit failed: %s (%d)\n",
261 db_strerror(rc), rc, 0 );
263 text = "commit failed";
265 Debug( LDAP_DEBUG_TRACE,
266 "bdb_add: added id=%08x dn=\"%s\"\n",
267 e->e_id, e->e_dn, 0 );
273 send_ldap_result( conn, op, rc,
274 NULL, text, NULL, NULL );
278 /* free parent and writer lock */
279 bdb_entry_return( be, p );
284 op->o_private = NULL;