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