1 /* add.c - ldap bdb2 back-end add routine */
12 #include "back-bdb2.h"
13 #include "proto-back-bdb2.h"
19 bdb2i_back_add_internal(
26 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
32 Debug(LDAP_DEBUG_ARGS, "==> bdb2i_back_add: %s\n", e->e_dn, 0, 0);
34 if ( ( bdb2i_dn2id( be, e->e_ndn ) ) != NOID ) {
35 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
36 NULL, NULL, NULL, NULL );
40 if ( schema_check_entry( e ) != 0 ) {
41 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
44 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
45 NULL, NULL, NULL, NULL );
50 * Get the parent dn and see if the corresponding entry exists.
51 * If the parent does not exist, only allow the "root" user to
55 pdn = dn_parent( be, e->e_ndn );
57 if( pdn != NULL && *pdn != '\0' ) {
58 Entry *matched = NULL;
60 assert( *pdn != '\0' );
62 /* get parent with writer lock */
63 if ( (p = bdb2i_dn2entry_w( be, pdn, &matched )) == NULL ) {
67 if( matched != NULL ) {
68 matched_dn = ch_strdup( matched->e_dn );
69 refs = is_entry_referral( matched )
70 ? get_entry_referrals( be, conn, op, matched )
73 bdb2i_cache_return_entry_w( &li->li_cache, matched );
77 refs = default_referral;
80 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
83 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
84 matched_dn, NULL, NULL, NULL );
86 if ( matched != NULL ) {
97 if ( ! access_allowed( be, conn, op, p,
98 "children", NULL, ACL_WRITE ) )
100 /* free parent and writer lock */
101 bdb2i_cache_return_entry_w( &li->li_cache, p );
103 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
105 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
106 NULL, NULL, NULL, NULL );
112 if ( is_entry_alias( p ) ) {
113 /* parent is an alias, don't allow add */
115 /* free parent and writer lock */
116 bdb2i_cache_return_entry_w( &li->li_cache, p );
118 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
120 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
121 NULL, NULL, NULL, NULL );
126 if ( is_entry_referral( p ) ) {
127 /* parent is an referral, don't allow add */
128 char *matched_dn = ch_strdup( matched->e_dn );
129 struct berval **refs = is_entry_referral( matched )
130 ? get_entry_referrals( be, conn, op, matched )
133 /* free parent and writer lock */
134 bdb2i_cache_return_entry_w( &li->li_cache, p );
136 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
138 send_ldap_result( conn, op, LDAP_REFERRAL,
139 matched_dn, NULL, refs, NULL );
141 ber_bvecfree( refs );
148 assert( *pdn == '\0' );
152 /* no parent, must be adding entry to root */
153 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix(be, "") ) {
154 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
155 pdn == NULL ? "suffix" : "entry at root",
158 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
159 NULL, NULL, NULL, NULL );
165 e->e_id = bdb2i_next_id( be );
168 * Try to add the entry to the cache, assign it a new dnid.
170 bdb2i_start_timing( be->bd_info, &time1 );
172 rc = bdb2i_cache_add_entry_rw( &li->li_cache, e, CACHE_WRITE_LOCK );
174 bdb2i_stop_timing( be->bd_info, time1, "ADD-CACHE", conn, op );
178 /* free parent and writer lock */
179 bdb2i_cache_return_entry_w( &li->li_cache, p );
182 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
185 send_ldap_result( conn, op,
186 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OPERATIONS_ERROR,
187 NULL, NULL, NULL, NULL );
195 * Add the entry to the attribute indexes, then add it to
196 * the id2entry and dn2id index.
199 bdb2i_start_timing( be->bd_info, &time1 );
201 /* attribute indexes */
202 if ( bdb2i_index_add_entry( be, e ) != 0 ) {
203 Debug( LDAP_DEBUG_TRACE, "bdb2i_index_add_entry failed\n", 0,
205 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
206 NULL, NULL, NULL, NULL );
208 bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
213 bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
215 bdb2i_start_timing( be->bd_info, &time1 );
218 if ( bdb2i_dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
219 Debug( LDAP_DEBUG_TRACE, "bdb2i_dn2id_add failed\n", 0,
221 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
222 NULL, NULL, NULL, NULL );
224 bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
229 bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
231 bdb2i_start_timing( be->bd_info, &time1 );
234 if ( bdb2i_id2entry_add( be, e ) != 0 ) {
235 Debug( LDAP_DEBUG_TRACE, "bdb2i_id2entry_add failed\n", 0,
237 (void) bdb2i_dn2id_delete( be, e->e_ndn, e->e_id );
238 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
239 NULL, NULL, NULL, NULL );
241 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
246 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
248 send_ldap_result( conn, op, LDAP_SUCCESS,
249 NULL, NULL, NULL, NULL );
254 /* free parent and writer lock */
255 bdb2i_cache_return_entry_w( &li->li_cache, p );
259 /* free entry and writer lock */
260 bdb2i_cache_return_entry_w( &li->li_cache, e );
275 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
276 struct timeval time1;
279 bdb2i_start_timing( be->bd_info, &time1 );
281 if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
282 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
283 NULL, NULL, NULL, NULL );
287 /* check, if a new default attribute index will be created,
288 in which case we have to open the index file BEFORE TP */
289 switch ( slapMode & SLAP_MODE ) {
290 case SLAP_SERVER_MODE:
292 bdb2i_check_default_attr_index_add( li, e );
296 ret = bdb2i_back_add_internal( be, conn, op, e );
298 /* if the operation was successful, we will delay the unlock */
300 (void) bdb2i_leave_backend_w( lock );
302 bdb2i_stop_timing( be->bd_info, time1, "ADD", conn, op );
309 bdb2i_release_add_lock( void )
311 (void) bdb2i_leave_backend_w( lock );