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 ) {
36 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
37 NULL, NULL, NULL, NULL );
41 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
42 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
46 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
47 NULL, NULL, NULL, NULL );
52 * Get the parent dn and see if the corresponding entry exists.
53 * If the parent does not exist, only allow the "root" user to
57 pdn = dn_parent( be, e->e_ndn );
59 if( pdn != NULL && *pdn != '\0' ) {
60 Entry *matched = NULL;
62 assert( *pdn != '\0' );
64 /* get parent with writer lock */
65 if ( (p = bdb2i_dn2entry_w( be, pdn, &matched )) == NULL ) {
69 if( matched != NULL ) {
70 matched_dn = ch_strdup( matched->e_dn );
71 refs = is_entry_referral( matched )
72 ? get_entry_referrals( be, conn, op, matched )
75 bdb2i_cache_return_entry_w( &li->li_cache, matched );
79 refs = default_referral;
82 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
85 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
86 matched_dn, NULL, NULL, NULL );
88 if ( matched != NULL ) {
100 if ( ! access_allowed( be, conn, op, p,
101 "children", NULL, ACL_WRITE ) )
103 /* free parent and writer lock */
104 bdb2i_cache_return_entry_w( &li->li_cache, p );
106 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
108 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
109 NULL, NULL, NULL, NULL );
116 if ( is_entry_alias( p ) ) {
117 /* parent is an alias, don't allow add */
119 /* free parent and writer lock */
120 bdb2i_cache_return_entry_w( &li->li_cache, p );
122 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
124 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
125 NULL, NULL, NULL, NULL );
131 if ( is_entry_referral( p ) ) {
132 /* parent is an referral, don't allow add */
133 char *matched_dn = ch_strdup( matched->e_dn );
134 struct berval **refs = is_entry_referral( matched )
135 ? get_entry_referrals( be, conn, op, matched )
138 /* free parent and writer lock */
139 bdb2i_cache_return_entry_w( &li->li_cache, p );
141 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
143 send_ldap_result( conn, op, LDAP_REFERRAL,
144 matched_dn, NULL, refs, NULL );
146 ber_bvecfree( refs );
154 assert( *pdn == '\0' );
158 /* no parent, must be adding entry to root */
159 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix(be, "") ) {
160 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
161 pdn == NULL ? "suffix" : "entry at root",
164 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
165 NULL, NULL, NULL, NULL );
172 e->e_id = bdb2i_next_id( be );
175 * Try to add the entry to the cache, assign it a new dnid.
177 bdb2i_start_timing( be->bd_info, &time1 );
179 rc = bdb2i_cache_add_entry_rw( &li->li_cache, e, CACHE_WRITE_LOCK );
181 bdb2i_stop_timing( be->bd_info, time1, "ADD-CACHE", conn, op );
185 /* free parent and writer lock */
186 bdb2i_cache_return_entry_w( &li->li_cache, p );
189 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
195 send_ldap_result( conn, op,
196 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OPERATIONS_ERROR,
197 NULL, NULL, NULL, NULL );
205 * Add the entry to the attribute indexes, then add it to
206 * the id2entry and dn2id index.
209 bdb2i_start_timing( be->bd_info, &time1 );
211 /* attribute indexes */
212 if ( bdb2i_index_add_entry( be, e ) != 0 ) {
213 Debug( LDAP_DEBUG_TRACE, "bdb2i_index_add_entry 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-INDEX", conn, op );
223 bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
225 bdb2i_start_timing( be->bd_info, &time1 );
228 if ( bdb2i_dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
229 Debug( LDAP_DEBUG_TRACE, "bdb2i_dn2id_add failed\n", 0,
231 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
232 NULL, NULL, NULL, NULL );
234 bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
239 bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
241 bdb2i_start_timing( be->bd_info, &time1 );
244 if ( bdb2i_id2entry_add( be, e ) != 0 ) {
245 Debug( LDAP_DEBUG_TRACE, "bdb2i_id2entry_add failed\n", 0,
247 (void) bdb2i_dn2id_delete( be, e->e_ndn, e->e_id );
248 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
249 NULL, NULL, NULL, NULL );
251 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
256 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
258 send_ldap_result( conn, op, LDAP_SUCCESS,
259 NULL, NULL, NULL, NULL );
264 /* free parent and writer lock */
265 bdb2i_cache_return_entry_w( &li->li_cache, p );
269 /* free entry and writer lock */
270 bdb2i_cache_return_entry_w( &li->li_cache, e );
285 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
286 struct timeval time1;
289 bdb2i_start_timing( be->bd_info, &time1 );
291 if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
292 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
293 NULL, NULL, NULL, NULL );
297 /* check, if a new default attribute index will be created,
298 in which case we have to open the index file BEFORE TP */
299 switch ( slapMode & SLAP_MODE ) {
300 case SLAP_SERVER_MODE:
302 bdb2i_check_default_attr_index_add( li, e );
306 ret = bdb2i_back_add_internal( be, conn, op, e );
308 /* if the operation was successful, we will delay the unlock */
310 (void) bdb2i_leave_backend_w( lock );
312 bdb2i_stop_timing( be->bd_info, time1, "ADD", conn, op );
319 bdb2i_release_add_lock( void )
321 (void) bdb2i_leave_backend_w( lock );