]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/delete.c
9b5335b149032225cc03fae99658698a546f8e69
[openldap] / servers / slapd / back-bdb2 / delete.c
1 /* delete.c - bdb2 backend delete routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-bdb2.h"
12 #include "proto-back-bdb2.h"
13
14 static int
15 bdb2i_back_delete_internal(
16     BackendDB   *be,
17     Connection  *conn,
18     Operation   *op,
19     char        *dn
20 )
21 {
22         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
23         char    *matched = NULL;
24         char    *pdn = NULL;
25         Entry   *e, *p = NULL;
26         int rootlock = 0;
27         int     rc = -1;
28
29         Debug(LDAP_DEBUG_ARGS, "==> bdb2i_back_delete: %s\n", dn, 0, 0);
30
31         /* get entry with writer lock */
32         if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
33                 Debug(LDAP_DEBUG_ARGS, "<=- bdb2i_back_delete: no such object %s\n",
34                         dn, 0, 0);
35                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
36                 if ( matched != NULL ) {
37                         free( matched );
38                 }
39                 return( -1 );
40         }
41
42         /* check for deleted */
43
44         if ( bdb2i_has_children( be, e ) ) {
45                 Debug(LDAP_DEBUG_ARGS, "<=- bdb2i_back_delete: non leaf %s\n",
46                         dn, 0, 0);
47                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF, "",
48                     "" );
49                 goto return_results;
50         }
51
52 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
53         if ( ! access_allowed( be, conn, op, e,
54                 "entry", NULL, ACL_WRITE ) )
55         {
56                 Debug(LDAP_DEBUG_ARGS,
57                         "<=- bdb2i_back_delete: insufficient access %s\n",
58                         dn, 0, 0);
59                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, "", "" );
60                 goto return_results;
61         }
62 #endif
63
64         /* delete from parent's id2children entry */
65         if( (pdn = dn_parent( be, dn )) != NULL ) {
66                 if( (p = bdb2i_dn2entry_w( be, pdn, &matched )) == NULL) {
67                         Debug( LDAP_DEBUG_TRACE,
68                                 "<=- bdb2i_back_delete: parent does not exist\n", 0, 0, 0);
69                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
70                                 "", "");
71                         goto return_results;
72                 }
73
74 #ifndef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
75                 /* check parent for "children" acl */
76                 if ( ! access_allowed( be, conn, op, p,
77                         "children", NULL, ACL_WRITE ) )
78                 {
79                         Debug( LDAP_DEBUG_TRACE,
80                                 "<=- bdb2i_back_delete: no access to parent\n", 0, 0, 0 );
81                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
82                                 "", "" );
83                         goto return_results;
84                 }
85 #endif
86
87         } else {
88                 /* no parent, must be root to delete */
89                 if( ! be_isroot( be, op->o_ndn ) ) {
90                         Debug( LDAP_DEBUG_TRACE,
91                                 "<=- bdb2i_back_delete: no parent & not root\n", 0, 0, 0);
92                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
93                                 "", "");
94                         goto return_results;
95                 }
96
97                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
98                 rootlock = 1;
99         }
100
101         if ( bdb2i_id2children_remove( be, p, e ) != 0 ) {
102                 Debug(LDAP_DEBUG_ARGS,
103                         "<=- bdb2i_back_delete: operations error %s\n",
104                         dn, 0, 0);
105                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "","" );
106                 goto return_results;
107         }
108
109         /* delete from dn2id mapping */
110         if ( bdb2i_dn2id_delete( be, e->e_dn ) != 0 ) {
111                 Debug(LDAP_DEBUG_ARGS,
112                         "<=- bdb2i_back_delete: operations error %s\n",
113                         dn, 0, 0);
114                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
115                 goto return_results;
116         }
117
118         /* delete from disk and cache */
119         if ( bdb2i_id2entry_delete( be, e ) != 0 ) {
120                 Debug(LDAP_DEBUG_ARGS,
121                         "<=- bdb2i_back_delete: operations error %s\n",
122                         dn, 0, 0);
123                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
124                 goto return_results;
125         }
126
127         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
128         rc = 0;
129
130 return_results:;
131         if ( pdn != NULL ) free(pdn);
132
133         if( p != NULL ) {
134                 /* free parent and writer lock */
135                 bdb2i_cache_return_entry_w( &li->li_cache, p );
136
137         }
138
139         if ( rootlock ) {
140                 /* release root lock */
141                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
142         }
143
144         /* free entry and writer lock */
145         bdb2i_cache_return_entry_w( &li->li_cache, e );
146
147         if ( matched != NULL ) free(matched);
148
149         return rc;
150 }
151
152
153 int
154 bdb2_back_delete(
155     BackendDB   *be,
156     Connection  *conn,
157     Operation   *op,
158     char        *dn
159 )
160 {
161         DB_LOCK  lock;
162         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
163
164         struct timeval  time1, time2;
165         char   *elapsed_time;
166         int    ret;
167
168         gettimeofday( &time1, NULL );
169
170         if ( bdb2i_enter_backend_w( get_dbenv( be ), &lock ) != 0 ) {
171
172                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
173                 return( -1 );
174
175         }
176
177         ret = bdb2i_back_delete_internal( be, conn, op, dn );
178
179         (void) bdb2i_leave_backend( get_dbenv( be ), lock );
180
181         if ( bdb2i_do_timing ) {
182
183                 gettimeofday( &time2, NULL);
184                 elapsed_time = bdb2i_elapsed( time1, time2 );
185                 Debug( LDAP_DEBUG_ANY, "conn=%d op=%d DEL elapsed=%s\n",
186                                 conn->c_connid, op->o_opid, elapsed_time );
187                 free( elapsed_time );
188
189         }
190
191         return( ret );
192 }
193
194