]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
Experiment with busy loop protection...
[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
45         /* get entry with writer lock */
46         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
47                 char *matched_dn = NULL;
48                 struct berval **refs = NULL;
49
50 #ifdef NEW_LOGGING
51                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
52                            "ldbm_back_delete: no such object %s\n", dn ));
53 #else
54                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: no such object %s\n",
55                         dn, 0, 0);
56 #endif
57
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                 } else {
66                         refs = default_referral;
67                 }
68
69                 send_ldap_result( conn, op, LDAP_REFERRAL,
70                         matched_dn, NULL, refs, NULL );
71
72                 if ( matched != NULL ) {
73                         ber_bvecfree( refs );
74                         free( matched_dn );
75                 }
76
77                 return( -1 );
78         }
79
80     if ( !manageDSAit && is_entry_referral( e ) ) {
81                 /* parent is a referral, don't allow add */
82                 /* parent is an alias, don't allow add */
83                 struct berval **refs = get_entry_referrals( be,
84                         conn, op, e );
85
86 #ifdef NEW_LOGGING
87                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
88                            "ldbm_back_delete: entry (%s) is a referral.\n",
89                            e->e_dn ));
90 #else
91                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
92                     0, 0 );
93 #endif
94
95
96                 send_ldap_result( conn, op, LDAP_REFERRAL,
97                     e->e_dn, NULL, refs, NULL );
98
99                 ber_bvecfree( refs );
100
101                 rc = 1;
102                 goto return_results;
103         }
104
105
106         if ( has_children( be, e ) ) {
107 #ifdef NEW_LOGGING
108                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
109                            "ldbm_back_delete: (%s) is a non-leaf node.\n", dn ));
110 #else
111                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
112                         dn, 0, 0);
113 #endif
114
115                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
116                         NULL, "subtree delete not supported", NULL, NULL );
117                 goto return_results;
118         }
119
120         /* delete from parent's id2children entry */
121         if( (pdn = dn_parent( be, e->e_ndn )) != NULL ) {
122                 if( (p = dn2entry_w( be, pdn, NULL )) == NULL) {
123 #ifdef NEW_LOGGING
124                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
125                                    "ldbm_back_delete: parent of (%s) does not exist\n", dn ));
126 #else
127                         Debug( LDAP_DEBUG_TRACE,
128                                 "<=- ldbm_back_delete: parent does not exist\n",
129                                 0, 0, 0);
130 #endif
131
132                         send_ldap_result( conn, op, LDAP_OTHER,
133                                 NULL, "could not locate parent of entry", NULL, NULL );
134                         goto return_results;
135                 }
136
137                 /* check parent for "children" acl */
138                 if ( ! access_allowed( be, conn, op, p,
139                         children, NULL, ACL_WRITE ) )
140                 {
141 #ifdef NEW_LOGGING
142                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
143                                    "ldbm_back_delete: no access to parent of (%s)\n", dn ));
144 #else
145                         Debug( LDAP_DEBUG_TRACE,
146                                 "<=- ldbm_back_delete: no access to parent\n", 0,
147                                 0, 0 );
148 #endif
149
150                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
151                                 NULL, NULL, NULL, NULL );
152                         goto return_results;
153                 }
154
155         } else {
156                 /* no parent, must be root to delete */
157                 if( ! be_isroot( be, op->o_ndn ) ) {
158 #ifdef NEW_LOGGING
159                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
160                                    "ldbm_back_delete: (%s) has no parent & not a root.\n",
161                                    dn ));
162 #else
163                         Debug( LDAP_DEBUG_TRACE,
164                                 "<=- ldbm_back_delete: no parent & not root\n",
165                                 0, 0, 0);
166 #endif
167
168                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
169                                 NULL, NULL, NULL, NULL );
170                         goto return_results;
171                 }
172
173                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
174                 rootlock = 1;
175         }
176
177         /* delete from dn2id mapping */
178         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
179 #ifdef NEW_LOGGING
180                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
181                            "ldbm_back_delete: (%s) operations error\n", dn ));
182 #else
183                 Debug(LDAP_DEBUG_ARGS,
184                         "<=- ldbm_back_delete: operations error %s\n",
185                         dn, 0, 0);
186 #endif
187
188                 send_ldap_result( conn, op, LDAP_OTHER,
189                         NULL, "DN index delete failed", NULL, NULL );
190                 goto return_results;
191         }
192
193         /* delete from disk and cache */
194         if ( id2entry_delete( be, e ) != 0 ) {
195 #ifdef NEW_LOGGING
196                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
197                            "ldbm_back_delete: (%s) operations error\n",
198                            dn ));
199 #else
200                 Debug(LDAP_DEBUG_ARGS,
201                         "<=- ldbm_back_delete: operations error %s\n",
202                         dn, 0, 0);
203 #endif
204
205                 send_ldap_result( conn, op, LDAP_OTHER,
206                         NULL, "entry delete failed", NULL, NULL );
207                 goto return_results;
208         }
209
210         /* delete attribute indices */
211         (void) index_entry_del( be, e, e->e_attrs );
212
213         send_ldap_result( conn, op, LDAP_SUCCESS,
214                 NULL, NULL, NULL, NULL );
215         rc = 0;
216
217 return_results:;
218         if ( pdn != NULL ) free(pdn);
219
220         if( p != NULL ) {
221                 /* free parent and writer lock */
222                 cache_return_entry_w( &li->li_cache, p );
223         }
224
225         if ( rootlock ) {
226                 /* release root lock */
227                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
228         }
229
230         /* free entry and writer lock */
231         cache_return_entry_w( &li->li_cache, e );
232
233         return rc;
234 }