1 /* add.c - ldap bdb2 back-end add routine */
11 #include "back-bdb2.h"
12 #include "proto-back-bdb2.h"
18 bdb2i_back_add_internal(
25 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
31 Debug(LDAP_DEBUG_ARGS, "==> bdb2i_back_add: %s\n", e->e_dn, 0, 0);
33 if ( ( bdb2i_dn2id( be, e->e_ndn ) ) != NOID ) {
35 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
36 NULL, NULL, NULL, NULL );
40 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
41 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
45 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
46 NULL, NULL, NULL, NULL );
51 * Get the parent dn and see if the corresponding entry exists.
52 * If the parent does not exist, only allow the "root" user to
56 pdn = dn_parent( be, e->e_ndn );
58 if( pdn != NULL && *pdn != '\0' && !be_issuffix(be, "") ) {
59 Entry *matched = NULL;
61 assert( *pdn != '\0' );
63 /* get parent with writer lock */
64 if ( (p = bdb2i_dn2entry_w( be, pdn, &matched )) == NULL ) {
68 if( matched != NULL ) {
69 matched_dn = ch_strdup( matched->e_dn );
70 refs = is_entry_referral( matched )
71 ? get_entry_referrals( be, conn, op, matched )
74 bdb2i_cache_return_entry_w( &li->li_cache, matched );
78 refs = default_referral;
81 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
84 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
85 matched_dn, NULL, NULL, NULL );
87 if ( matched != NULL ) {
99 if ( ! access_allowed( be, conn, op, p,
100 "children", NULL, ACL_WRITE ) )
102 /* free parent and writer lock */
103 bdb2i_cache_return_entry_w( &li->li_cache, p );
105 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
107 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
108 NULL, NULL, NULL, NULL );
115 if ( is_entry_alias( p ) ) {
116 /* parent is an alias, don't allow add */
118 /* free parent and writer lock */
119 bdb2i_cache_return_entry_w( &li->li_cache, p );
121 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
123 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
124 NULL, NULL, NULL, NULL );
130 if ( is_entry_referral( p ) ) {
131 /* parent is an referral, don't allow add */
132 char *matched_dn = ch_strdup( matched->e_dn );
133 struct berval **refs = is_entry_referral( matched )
134 ? get_entry_referrals( be, conn, op, matched )
137 /* free parent and writer lock */
138 bdb2i_cache_return_entry_w( &li->li_cache, p );
140 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
142 send_ldap_result( conn, op, LDAP_REFERRAL,
143 matched_dn, NULL, refs, NULL );
145 ber_bvecfree( refs );
153 assert( *pdn == '\0' );
157 /* no parent, must be adding entry to root */
158 if ( ! be_isroot( be, op->o_ndn ) ) {
159 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
160 pdn == NULL ? "suffix" : "entry at root",
163 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
164 NULL, NULL, NULL, NULL );
171 e->e_id = bdb2i_next_id( be );
174 * Try to add the entry to the cache, assign it a new dnid.
176 bdb2i_start_timing( be->bd_info, &time1 );
178 rc = bdb2i_cache_add_entry_rw( &li->li_cache, e, CACHE_WRITE_LOCK );
180 bdb2i_stop_timing( be->bd_info, time1, "ADD-CACHE", conn, op );
184 /* free parent and writer lock */
185 bdb2i_cache_return_entry_w( &li->li_cache, p );
188 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
192 bdb2i_next_id_return( be, e->e_id );
197 send_ldap_result( conn, op,
198 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OPERATIONS_ERROR,
199 NULL, NULL, NULL, NULL );
207 * add it to the id2children index for the parent
210 bdb2i_start_timing( be->bd_info, &time1 );
212 if ( bdb2i_id2children_add( be, p, e ) != 0 ) {
213 Debug( LDAP_DEBUG_TRACE, "bdb2i_id2children_add failed\n", 0,
215 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
216 NULL, NULL, NULL, NULL );
218 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2CHILDREN", conn, op );
223 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2CHILDREN", conn, op );
226 * Add the entry to the attribute indexes, then add it to
227 * the id2children index, dn2id index, and the id2entry index.
230 bdb2i_start_timing( be->bd_info, &time1 );
232 /* attribute indexes */
233 if ( bdb2i_index_add_entry( be, e ) != 0 ) {
234 Debug( LDAP_DEBUG_TRACE, "bdb2i_index_add_entry failed\n", 0,
236 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
237 NULL, NULL, NULL, NULL );
239 bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
244 bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
246 bdb2i_start_timing( be->bd_info, &time1 );
249 if ( bdb2i_dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
250 Debug( LDAP_DEBUG_TRACE, "bdb2i_dn2id_add failed\n", 0,
252 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
253 NULL, NULL, NULL, NULL );
255 bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
260 bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
262 bdb2i_start_timing( be->bd_info, &time1 );
265 if ( bdb2i_id2entry_add( be, e ) != 0 ) {
266 Debug( LDAP_DEBUG_TRACE, "bdb2i_id2entry_add failed\n", 0,
268 (void) bdb2i_dn2id_delete( be, e->e_ndn );
269 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
270 NULL, NULL, NULL, NULL );
272 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
277 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
279 send_ldap_result( conn, op, LDAP_SUCCESS,
280 NULL, NULL, NULL, NULL );
285 /* free parent and writer lock */
286 bdb2i_cache_return_entry_w( &li->li_cache, p );
290 /* free entry and writer lock */
291 bdb2i_cache_return_entry_w( &li->li_cache, e );
306 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
307 struct timeval time1;
310 bdb2i_start_timing( be->bd_info, &time1 );
312 if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
313 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
314 NULL, NULL, NULL, NULL );
318 /* check, if a new default attribute index will be created,
319 in which case we have to open the index file BEFORE TP */
320 switch ( slapMode ) {
321 case SLAP_SERVER_MODE:
322 case SLAP_TIMEDSERVER_MODE:
324 case SLAP_TOOLID_MODE:
325 bdb2i_check_default_attr_index_add( li, e );
329 ret = bdb2i_back_add_internal( be, conn, op, e );
331 /* if the operation was successful, we will delay the unlock */
333 (void) bdb2i_leave_backend_w( lock );
335 bdb2i_stop_timing( be->bd_info, time1, "ADD", conn, op );
342 bdb2i_release_add_lock( void )
344 (void) bdb2i_leave_backend_w( lock );