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