]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
Import LDBM giant lock code from HEAD.
[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     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     rc = -1;
33         int             manageDSAit = get_manageDSAit( op );
34         AttributeDescription *children = slap_schema.si_ad_children;
35
36         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_delete: %s\n", dn, 0, 0);
37
38         /* grab giant lock for writing */
39         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
40
41         /* get entry with writer lock */
42         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
43                 char *matched_dn = NULL;
44                 struct berval **refs = NULL;
45
46                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: no such object %s\n",
47                         dn, 0, 0);
48
49                 if ( matched != NULL ) {
50                         matched_dn = ch_strdup( matched->e_dn );
51                         refs = is_entry_referral( matched )
52                                 ? get_entry_referrals( be, conn, op, matched )
53                                 : NULL;
54                         cache_return_entry_r( &li->li_cache, matched );
55                 } else {
56                         refs = default_referral;
57                 }
58
59                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
60
61                 send_ldap_result( conn, op, LDAP_REFERRAL,
62                         matched_dn, NULL, refs, NULL );
63
64                 if ( matched != NULL ) {
65                         ber_bvecfree( refs );
66                         free( matched_dn );
67                 }
68
69                 return( -1 );
70         }
71
72     if ( !manageDSAit && is_entry_referral( e ) ) {
73                 /* parent is a referral, don't allow add */
74                 /* parent is an alias, don't allow add */
75                 struct berval **refs = get_entry_referrals( be,
76                         conn, op, e );
77
78                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
79                     0, 0 );
80
81                 send_ldap_result( conn, op, LDAP_REFERRAL,
82                     e->e_dn, NULL, refs, NULL );
83
84                 ber_bvecfree( refs );
85
86                 rc = 1;
87                 goto return_results;
88         }
89
90
91         if ( has_children( be, e ) ) {
92                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
93                         dn, 0, 0);
94                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
95                         NULL, "subtree delete not supported", NULL, NULL );
96                 goto return_results;
97         }
98
99         /* delete from parent's id2children entry */
100         if( (pdn = dn_parent( be, e->e_ndn )) != NULL && pdn[ 0 ] != '\0' ) {
101                 if( (p = dn2entry_w( be, pdn, NULL )) == NULL) {
102                         Debug( LDAP_DEBUG_TRACE,
103                                 "<=- ldbm_back_delete: parent does not exist\n",
104                                 0, 0, 0);
105                         send_ldap_result( conn, op, LDAP_OTHER,
106                                 NULL, "could not locate parent of entry", NULL, NULL );
107                         goto return_results;
108                 }
109
110                 /* check parent for "children" acl */
111                 if ( ! access_allowed( be, conn, op, p,
112                         children, NULL, ACL_WRITE ) )
113                 {
114                         Debug( LDAP_DEBUG_TRACE,
115                                 "<=- ldbm_back_delete: no access to parent\n", 0,
116                                 0, 0 );
117                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
118                                 NULL, NULL, NULL, NULL );
119                         goto return_results;
120                 }
121
122         } else {
123                 /* no parent, must be root to delete */
124                 if( ! be_isroot( be, op->o_ndn ) ) {
125                         if ( be_issuffix( be, "" ) 
126                                         || be_isupdate( be, op->o_ndn ) ) {
127                                 static const Entry rootp = { NOID, "", "", NULL, NULL };
128                                 p = (Entry *)&rootp;
129                                 
130                                 rc = access_allowed( be, conn, op, p,
131                                                 children, NULL, ACL_WRITE );
132                                 p = NULL;
133                                                                 
134                                 /* check parent for "children" acl */
135                                 if ( ! rc ) {
136                                         Debug( LDAP_DEBUG_TRACE,
137                                                 "<=- ldbm_back_delete: no "
138                                                 "access to parent\n", 0, 0, 0 );
139
140                                         send_ldap_result( conn, op, 
141                                                 LDAP_INSUFFICIENT_ACCESS,
142                                                 NULL, NULL, NULL, NULL );
143                                         goto return_results;
144                                 }
145
146                         } else {
147                                 Debug( LDAP_DEBUG_TRACE,
148                                         "<=- ldbm_back_delete: no parent & "
149                                         "not root\n", 0, 0, 0);
150
151                                 send_ldap_result( conn, op, 
152                                         LDAP_INSUFFICIENT_ACCESS,
153                                         NULL, NULL, NULL, NULL );
154                                 goto return_results;
155                         }
156                 }
157         }
158
159         /* delete from dn2id mapping */
160         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
161                 Debug(LDAP_DEBUG_ARGS,
162                         "<=- ldbm_back_delete: operations error %s\n",
163                         dn, 0, 0);
164                 send_ldap_result( conn, op, LDAP_OTHER,
165                         NULL, "DN index delete failed", NULL, NULL );
166                 goto return_results;
167         }
168
169         /* delete from disk and cache */
170         if ( id2entry_delete( be, e ) != 0 ) {
171                 Debug(LDAP_DEBUG_ARGS,
172                         "<=- ldbm_back_delete: operations error %s\n",
173                         dn, 0, 0);
174                 send_ldap_result( conn, op, LDAP_OTHER,
175                         NULL, "entry delete failed", NULL, NULL );
176                 goto return_results;
177         }
178
179         /* delete attribute indices */
180         (void) index_entry_del( be, e, e->e_attrs );
181
182         send_ldap_result( conn, op, LDAP_SUCCESS,
183                 NULL, NULL, NULL, NULL );
184         rc = 0;
185
186 return_results:;
187         if ( pdn != NULL ) free(pdn);
188
189         if( p != NULL ) {
190                 /* free parent and writer lock */
191                 cache_return_entry_w( &li->li_cache, p );
192         }
193
194         /* free entry and writer lock */
195         cache_return_entry_w( &li->li_cache, e );
196
197         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
198
199         return rc;
200 }