]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
New dn2id format with base/one/subtree indices (ldbm/bdb2)
[openldap] / servers / slapd / back-ldbm / delete.c
1 /* delete.c - ldbm backend delete routine */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/string.h>
12 #include <ac/socket.h>
13
14 #include "slap.h"
15 #include "back-ldbm.h"
16 #include "proto-back-ldbm.h"
17
18 int
19 ldbm_back_delete(
20     Backend     *be,
21     Connection  *conn,
22     Operation   *op,
23     char        *dn
24 )
25 {
26         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
27         Entry   *matched = NULL;
28         char    *pdn = NULL;
29         Entry   *e, *p = NULL;
30         int rootlock = 0;
31         int     rc = -1;
32         int             manageDSAit = get_manageDSAit( op );
33
34         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_delete: %s\n", dn, 0, 0);
35
36         /* get entry with writer lock */
37         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
38                 char *matched_dn = NULL;
39                 struct berval **refs = NULL;
40
41                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: no such object %s\n",
42                         dn, 0, 0);
43
44                 if ( matched != NULL ) {
45                         matched_dn = ch_strdup( matched->e_dn );
46                         refs = is_entry_referral( matched )
47                                 ? get_entry_referrals( be, conn, op, matched )
48                                 : NULL;
49                         cache_return_entry_r( &li->li_cache, matched );
50                 } else {
51                         refs = default_referral;
52                 }
53
54                 send_ldap_result( conn, op, LDAP_REFERRAL,
55                         matched_dn, NULL, refs, NULL );
56
57                 if ( matched != NULL ) {
58                         ber_bvecfree( refs );
59                         free( matched_dn );
60                 }
61
62                 return( -1 );
63         }
64
65 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
66         if ( ! access_allowed( be, conn, op, e,
67                 "entry", NULL, ACL_WRITE ) )
68         {
69                 Debug(LDAP_DEBUG_ARGS,
70                         "<=- ldbm_back_delete: insufficient access %s\n",
71                         dn, 0, 0);
72                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
73                         NULL, NULL, NULL, NULL );
74                 goto return_results;
75         }
76 #endif
77
78     if ( !manageDSAit && is_entry_referral( e ) ) {
79                 /* parent is a referral, don't allow add */
80                 /* parent is an alias, don't allow add */
81                 struct berval **refs = get_entry_referrals( be,
82                         conn, op, e );
83
84                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
85                     0, 0 );
86
87                 send_ldap_result( conn, op, LDAP_REFERRAL,
88                     e->e_dn, NULL, refs, NULL );
89
90                 ber_bvecfree( refs );
91
92                 rc = 1;
93                 goto return_results;
94         }
95
96
97         if ( has_children( be, e ) ) {
98                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
99                         dn, 0, 0);
100                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
101                         NULL, NULL, NULL, NULL );
102                 goto return_results;
103         }
104
105         /* delete from parent's id2children entry */
106         if( (pdn = dn_parent( be, e->e_ndn )) != NULL ) {
107                 if( (p = dn2entry_w( be, pdn, &matched )) == NULL) {
108                         Debug( LDAP_DEBUG_TRACE,
109                                 "<=- ldbm_back_delete: parent does not exist\n",
110                                 0, 0, 0);
111                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
112                                 NULL, NULL, NULL, NULL );
113                         goto return_results;
114                 }
115
116                 /* check parent for "children" acl */
117                 if ( ! access_allowed( be, conn, op, p,
118                         "children", NULL, ACL_WRITE ) )
119                 {
120                         Debug( LDAP_DEBUG_TRACE,
121                                 "<=- ldbm_back_delete: no access to parent\n", 0,
122                                 0, 0 );
123                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
124                                 NULL, NULL, NULL, NULL );
125                         goto return_results;
126                 }
127
128         } else {
129                 /* no parent, must be root to delete */
130                 if( ! be_isroot( be, op->o_ndn ) ) {
131                         Debug( LDAP_DEBUG_TRACE,
132                                 "<=- ldbm_back_delete: no parent & not root\n",
133                                 0, 0, 0);
134                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
135                                 NULL, NULL, NULL, NULL );
136                         goto return_results;
137                 }
138
139                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
140                 rootlock = 1;
141         }
142
143         /* delete from dn2id mapping */
144         if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
145                 Debug(LDAP_DEBUG_ARGS,
146                         "<=- ldbm_back_delete: operations error %s\n",
147                         dn, 0, 0);
148                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
149                         NULL, NULL, NULL, NULL );
150                 goto return_results;
151         }
152
153         /* delete from disk and cache */
154         if ( id2entry_delete( be, e ) != 0 ) {
155                 Debug(LDAP_DEBUG_ARGS,
156                         "<=- ldbm_back_delete: operations error %s\n",
157                         dn, 0, 0);
158                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
159                         NULL, NULL, NULL, NULL );
160                 goto return_results;
161         }
162
163         send_ldap_result( conn, op, LDAP_SUCCESS,
164                 NULL, NULL, NULL, NULL );
165         rc = 0;
166
167 return_results:;
168         if ( pdn != NULL ) free(pdn);
169
170         if( p != NULL ) {
171                 /* free parent and writer lock */
172                 cache_return_entry_w( &li->li_cache, p );
173         }
174
175         if ( rootlock ) {
176                 /* release root lock */
177                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
178         }
179
180         /* free entry and writer lock */
181         cache_return_entry_w( &li->li_cache, e );
182
183         if ( matched != NULL ) free(matched);
184
185         return rc;
186 }