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>
22 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
27 const char *text = NULL;
28 AttributeDescription *children = slap_schema.si_ad_children;
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";
57 /* begin transaction */
58 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
60 Debug( LDAP_DEBUG_TRACE,
61 "bdb_add: txn_begin failed: %s (%d)\n",
62 db_strerror(rc), rc, 0 );
64 text = "internal error";
69 * Get the parent dn and see if the corresponding entry exists.
70 * If the parent does not exist, only allow the "root" user to
73 pdn = dn_parent( be, e->e_ndn );
75 if( pdn != NULL && *pdn != '\0' ) {
76 Entry *matched = NULL;
78 /* get parent with reader lock */
79 p = dn2entry_r( be, ltid, pdn, &matched );
86 if ( matched != NULL ) {
87 matched_dn = ch_strdup( matched->e_dn );
88 refs = is_entry_referral( matched )
89 ? get_entry_referrals( be, conn, op, matched )
91 bdb_entry_return( be, matched );
95 refs = default_referral;
98 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
101 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
102 matched_dn, NULL, refs, NULL );
104 if( matched != NULL ) {
105 ber_bvecfree( refs );
106 ch_free( matched_dn );
114 if ( ! access_allowed( be, conn, op, p,
115 children, NULL, ACL_WRITE ) )
117 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
119 rc = LDAP_INSUFFICIENT_ACCESS;
120 text = "no write access to parent", NULL, NULL;
121 goto return_results;;
124 if ( is_entry_alias( p ) ) {
125 /* parent is an alias, don't allow add */
126 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
128 rc = LDAP_ALIAS_PROBLEM;
129 text = "parent is an alias";
130 goto return_results;;
133 if ( is_entry_referral( p ) ) {
134 /* parent is a referral, don't allow add */
135 char *matched_dn = ch_strdup( p->e_dn );
136 struct berval **refs = is_entry_referral( p )
137 ? get_entry_referrals( be, conn, op, p )
140 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
143 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
144 matched_dn, NULL, refs, NULL );
146 ber_bvecfree( refs );
158 * must be adding entry to at suffix
161 if ( !be_isroot( be, op->o_ndn )) {
162 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
163 pdn == NULL ? "suffix" : "entry at root",
165 rc = LDAP_INSUFFICIENT_ACCESS;
171 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
173 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
174 db_strerror(rc), rc, 0 );
175 if( rc == DB_KEYEXIST ) {
176 rc = LDAP_ALREADY_EXISTS;
184 rc = bdb_id2entry_add( be, ltid, e );
186 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
189 text = "entry store failed";
194 /* attribute indexes */
195 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
196 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
199 text = "index generation failed";
204 rc = txn_commit( ltid, 0 );
208 Debug( LDAP_DEBUG_TRACE,
209 "bdb_add: txn_commit failed: %s (%d)\n",
210 db_strerror(rc), rc, 0 );
212 text = "commit failed";
214 Debug( LDAP_DEBUG_TRACE,
215 "bdb_add: added id=%08x dn=\"%s\"\n",
216 e->e_id, e->e_dn, 0 );
222 send_ldap_result( conn, op, rc,
223 NULL, text, NULL, NULL );
228 /* free parent and writer lock */
229 bdb_entry_return( be, p );