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;
31 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
33 /* check entry's schema */
34 rc = entry_schema_check( e, NULL, &text );
35 if ( rc != LDAP_SUCCESS ) {
36 Debug( LDAP_DEBUG_TRACE,
37 "bdb_add: entry failed schema check: %s (%d)\n",
43 * acquire an ID outside of the operation transaction
44 * to avoid serializing adds.
46 rc = bdb_next_id( be, NULL, &e->e_id );
48 Debug( LDAP_DEBUG_TRACE,
49 "bdb_add: next_id failed (%d)\n",
52 text = "internal error";
57 /* transaction retry */
58 retry: rc = txn_abort( ltid );
63 text = "internal error";
68 /* begin transaction */
69 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
71 Debug( LDAP_DEBUG_TRACE,
72 "bdb_add: txn_begin failed: %s (%d)\n",
73 db_strerror(rc), rc, 0 );
75 text = "internal error";
82 * Get the parent dn and see if the corresponding entry exists.
83 * If the parent does not exist, only allow the "root" user to
86 pdn = dn_parent( be, e->e_ndn );
88 if( pdn != NULL && *pdn != '\0' ) {
89 Entry *matched = NULL;
91 /* get parent with reader lock */
92 rc = dn2entry_r( be, ltid, pdn, &p, &matched );
99 case DB_LOCK_DEADLOCK:
100 case DB_LOCK_NOTGRANTED:
104 text = "internal error";
110 struct berval **refs;
112 if ( matched != NULL ) {
113 matched_dn = ch_strdup( matched->e_dn );
114 refs = is_entry_referral( matched )
115 ? get_entry_referrals( be, conn, op, matched )
117 bdb_entry_return( be, matched );
121 refs = default_referral;
124 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
127 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
128 matched_dn, NULL, refs, NULL );
130 if( matched != NULL ) {
131 ber_bvecfree( refs );
132 ch_free( matched_dn );
138 if ( ! access_allowed( be, conn, op, p,
139 children, NULL, ACL_WRITE ) )
141 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
143 rc = LDAP_INSUFFICIENT_ACCESS;
144 text = "no write access to parent", NULL, NULL;
145 goto return_results;;
148 if ( is_entry_alias( p ) ) {
149 /* parent is an alias, don't allow add */
150 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
152 rc = LDAP_ALIAS_PROBLEM;
153 text = "parent is an alias";
154 goto return_results;;
157 if ( is_entry_referral( p ) ) {
158 /* parent is a referral, don't allow add */
159 char *matched_dn = ch_strdup( p->e_dn );
160 struct berval **refs = is_entry_referral( p )
161 ? get_entry_referrals( be, conn, op, p )
164 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
167 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
168 matched_dn, NULL, refs, NULL );
170 ber_bvecfree( refs );
175 /* free parent and writer lock */
176 bdb_entry_return( be, p );
186 * must be adding entry to at suffix
189 if ( !be_isroot( be, op->o_ndn )) {
190 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
191 pdn == NULL ? "suffix" : "entry at root",
193 rc = LDAP_INSUFFICIENT_ACCESS;
199 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
201 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
202 db_strerror(rc), rc, 0 );
205 case DB_LOCK_DEADLOCK:
206 case DB_LOCK_NOTGRANTED:
209 rc = LDAP_ALREADY_EXISTS;
218 rc = bdb_id2entry_add( be, ltid, e );
220 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
223 case DB_LOCK_DEADLOCK:
224 case DB_LOCK_NOTGRANTED:
229 text = "entry store failed";
234 /* attribute indexes */
235 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
236 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
239 case DB_LOCK_DEADLOCK:
240 case DB_LOCK_NOTGRANTED:
245 text = "index generation failed";
250 rc = txn_commit( ltid, 0 );
252 op->o_private = NULL;
255 Debug( LDAP_DEBUG_TRACE,
256 "bdb_add: txn_commit failed: %s (%d)\n",
257 db_strerror(rc), rc, 0 );
259 text = "commit failed";
261 Debug( LDAP_DEBUG_TRACE,
262 "bdb_add: added id=%08x dn=\"%s\"\n",
263 e->e_id, e->e_dn, 0 );
269 send_ldap_result( conn, op, rc,
270 NULL, text, NULL, NULL );
274 /* free parent and writer lock */
275 bdb_entry_return( be, p );
280 op->o_private = NULL;