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