]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
ITS#2440 unmunge init_module names
[openldap] / servers / slapd / back-ldbm / delete.c
1 /* delete.c - ldbm backend delete routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 int
20 ldbm_back_delete(
21     Operation   *op,
22     SlapReply   *rs )
23 {
24         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
25         Entry   *matched;
26         struct berval   pdn;
27         Entry   *e, *p = NULL;
28         int     rc = -1;
29         int             manageDSAit = get_manageDSAit( op );
30         AttributeDescription *children = slap_schema.si_ad_children;
31         AttributeDescription *entry = slap_schema.si_ad_entry;
32
33 #ifdef NEW_LOGGING
34         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_delete: %s\n", op->o_req_dn.bv_val, 0, 0 );
35 #else
36         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_delete: %s\n", op->o_req_dn.bv_val, 0, 0);
37 #endif
38
39         /* grab giant lock for writing */
40         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
41
42         /* get entry with writer lock */
43         if ( (e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
44 #ifdef NEW_LOGGING
45                 LDAP_LOG( BACK_LDBM, INFO, 
46                         "ldbm_back_delete: no such object %s\n", op->o_req_dn.bv_val, 0, 0 );
47 #else
48                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: no such object %s\n",
49                         op->o_req_dn.bv_val, 0, 0);
50 #endif
51
52                 if ( matched != NULL ) {
53                         rs->sr_matched = ch_strdup( matched->e_dn );
54                         rs->sr_ref = is_entry_referral( matched )
55                                 ? get_entry_referrals( op, matched )
56                                 : NULL;
57                         cache_return_entry_r( &li->li_cache, matched );
58
59                 } else {
60                         rs->sr_ref = referral_rewrite( default_referral,
61                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
62                 }
63
64                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
65
66                 rs->sr_err = LDAP_REFERRAL;
67                 send_ldap_result( op, rs );
68
69                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
70                 free( (char *)rs->sr_matched );
71
72                 return( -1 );
73         }
74
75         /* check entry for "entry" acl */
76 #ifdef LDAP_CACHING
77         if( !op->o_caching_on ) {
78 #endif /* LDAP_CACHING */
79         if ( ! access_allowed( op, e,
80                 entry, NULL, ACL_WRITE, NULL ) )
81         {
82 #ifdef NEW_LOGGING
83                 LDAP_LOG( BACK_LDBM, ERR, 
84                         "ldbm_back_delete: no write access to entry of (%s)\n", 
85                         op->o_req_dn.bv_val, 0, 0 );
86 #else
87                 Debug( LDAP_DEBUG_TRACE,
88                         "<=- ldbm_back_delete: no write access to entry\n", 0,
89                         0, 0 );
90 #endif
91
92                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
93                         "no write access to entry" );
94
95                 rc = 1;
96                 goto return_results;
97         }
98
99         if ( !manageDSAit && is_entry_referral( e ) ) {
100                 /* parent is a referral, don't allow add */
101                 /* parent is an alias, don't allow add */
102                 rs->sr_ref = get_entry_referrals( op, e );
103
104 #ifdef NEW_LOGGING
105                 LDAP_LOG( BACK_LDBM, INFO, 
106                         "ldbm_back_delete: entry (%s) is a referral.\n", e->e_dn, 0, 0 );
107 #else
108                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
109                     0, 0 );
110 #endif
111
112                 rs->sr_err = LDAP_REFERRAL;
113                 rs->sr_matched = e->e_name.bv_val;
114                 send_ldap_result( op, rs );
115
116                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
117
118                 rc = 1;
119                 goto return_results;
120         }
121
122         if ( has_children( op->o_bd, e ) ) {
123 #ifdef NEW_LOGGING
124                 LDAP_LOG( BACK_LDBM, ERR, 
125                            "ldbm_back_delete: (%s) is a non-leaf node.\n", op->o_req_dn.bv_val, 0,0);
126 #else
127                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
128                         op->o_req_dn.bv_val, 0, 0);
129 #endif
130
131                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
132                         "subtree delete not supported" );
133                 goto return_results;
134         }
135
136         /* delete from parent's id2children entry */
137         if( !be_issuffix( op->o_bd, &e->e_nname ) && (dnParent( &e->e_nname, &pdn ),
138                 pdn.bv_len) ) {
139                 if( (p = dn2entry_w( op->o_bd, &pdn, NULL )) == NULL) {
140 #ifdef NEW_LOGGING
141                         LDAP_LOG( BACK_LDBM, ERR, 
142                                 "ldbm_back_delete: parent of (%s) does not exist\n", op->o_req_dn, 0, 0 );
143 #else
144                         Debug( LDAP_DEBUG_TRACE,
145                                 "<=- ldbm_back_delete: parent does not exist\n",
146                                 0, 0, 0);
147 #endif
148
149                         send_ldap_error( op, rs, LDAP_OTHER,
150                                 "could not locate parent of entry" );
151                         goto return_results;
152                 }
153
154                 /* check parent for "children" acl */
155                 if ( ! access_allowed( op, p,
156                         children, NULL, ACL_WRITE, NULL ) )
157                 {
158 #ifdef NEW_LOGGING
159                         LDAP_LOG( BACK_LDBM, ERR, 
160                                 "ldbm_back_delete: no access to parent of (%s)\n", 
161                                 op->o_req_dn.bv_val, 0, 0 );
162 #else
163                         Debug( LDAP_DEBUG_TRACE,
164                                 "<=- ldbm_back_delete: no access to parent\n", 0,
165                                 0, 0 );
166 #endif
167
168                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
169                                 "no write access to parent" );
170                         goto return_results;
171                 }
172
173         } else {
174                 /* no parent, must be root to delete */
175                 if( ! be_isroot( op->o_bd, &op->o_ndn ) ) {
176                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
177                                 p = (Entry *)&slap_entry_root;
178                                 
179                                 rc = access_allowed( op, p,
180                                         children, NULL, ACL_WRITE, NULL );
181                                 p = NULL;
182                                                                 
183                                 /* check parent for "children" acl */
184                                 if ( ! rc ) {
185 #ifdef NEW_LOGGING
186                                         LDAP_LOG( BACK_LDBM, ERR,
187                                                 "ldbm_back_delete: no access "
188                                                 "to parent of ("")\n", 0, 0, 0 );
189 #else
190                                         Debug( LDAP_DEBUG_TRACE,
191                                                 "<=- ldbm_back_delete: no "
192                                                 "access to parent\n", 0, 0, 0 );
193 #endif
194
195                                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
196                                                 "no write access to parent" );
197                                         goto return_results;
198                                 }
199
200                         } else {
201 #ifdef NEW_LOGGING
202                                 LDAP_LOG( BACK_LDBM, ERR, 
203                                         "ldbm_back_delete: (%s) has no "
204                                         "parent & not a root.\n", op->o_ndn, 0, 0 );
205 #else
206                                 Debug( LDAP_DEBUG_TRACE,
207                                         "<=- ldbm_back_delete: no parent & "
208                                         "not root\n", 0, 0, 0);
209 #endif
210
211                                 send_ldap_error( op, rs,
212                                         LDAP_INSUFFICIENT_ACCESS,
213                                         NULL );
214                                 goto return_results;
215                         }
216                 }
217         }
218 #ifdef LDAP_CACHING
219         }
220 #endif /* LDAP_CACHING */
221
222         /* delete from dn2id mapping */
223         if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
224 #ifdef NEW_LOGGING
225                 LDAP_LOG( BACK_LDBM, ERR, 
226                         "ldbm_back_delete: (%s) operations error\n", op->o_req_dn.bv_val, 0, 0 );
227 #else
228                 Debug(LDAP_DEBUG_ARGS,
229                         "<=- ldbm_back_delete: operations error %s\n",
230                         op->o_req_dn.bv_val, 0, 0);
231 #endif
232
233                 send_ldap_error( op, rs, LDAP_OTHER,
234                         "DN index delete failed" );
235                 goto return_results;
236         }
237
238         /* delete from disk and cache */
239         if ( id2entry_delete( op->o_bd, e ) != 0 ) {
240 #ifdef NEW_LOGGING
241                 LDAP_LOG( BACK_LDBM, ERR, 
242                         "ldbm_back_delete: (%s) operations error\n", op->o_req_dn.bv_val, 0, 0 );
243 #else
244                 Debug(LDAP_DEBUG_ARGS,
245                         "<=- ldbm_back_delete: operations error %s\n",
246                         op->o_req_dn.bv_val, 0, 0);
247 #endif
248
249                 send_ldap_error( op, rs, LDAP_OTHER,
250                         "entry delete failed" );
251                 goto return_results;
252         }
253
254         /* delete attribute indices */
255         (void) index_entry_del( op, e );
256
257         rs->sr_err = LDAP_SUCCESS;
258         send_ldap_result( op, rs );
259         rc = 0;
260
261 return_results:;
262         if( p != NULL ) {
263                 /* free parent and writer lock */
264                 cache_return_entry_w( &li->li_cache, p );
265         }
266
267         /* free entry and writer lock */
268         cache_return_entry_w( &li->li_cache, e );
269
270         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
271
272         return rc;
273 }