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