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