]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
Code clean-up.
[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         char    *matched = NULL;
24         char    *pdn = NULL;
25         Entry   *e, *p = NULL;
26         int rootlock = 0;
27         int     rc = -1;
28
29         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_delete: %s\n", dn, 0, 0);
30
31         /* get entry with writer lock */
32         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
33                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: no such object %s\n",
34                         dn, 0, 0);
35                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
36                 if ( matched != NULL ) {
37                         free( matched );
38                 }
39                 return( -1 );
40         }
41
42         /* check for deleted */
43
44         if ( has_children( be, e ) ) {
45                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
46                         dn, 0, 0);
47                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF, "",
48                     "" );
49                 goto return_results;
50         }
51
52 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
53         if ( ! access_allowed( be, conn, op, e,
54                 "entry", NULL, ACL_WRITE ) )
55         {
56                 Debug(LDAP_DEBUG_ARGS,
57                         "<=- ldbm_back_delete: insufficient access %s\n",
58                         dn, 0, 0);
59                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, "", "" );
60                 goto return_results;
61         }
62 #endif
63
64         /* delete from parent's id2children entry */
65         if( (pdn = dn_parent( be, e->e_ndn )) != NULL ) {
66                 if( (p = dn2entry_w( be, pdn, &matched )) == NULL) {
67                         Debug( LDAP_DEBUG_TRACE,
68                                 "<=- ldbm_back_delete: parent does not exist\n",
69                                 0, 0, 0);
70                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
71                                 "", "");
72                         goto return_results;
73                 }
74
75 #ifndef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
76                 /* check parent for "children" acl */
77                 if ( ! access_allowed( be, conn, op, p,
78                         "children", NULL, ACL_WRITE ) )
79                 {
80                         Debug( LDAP_DEBUG_TRACE,
81                                 "<=- ldbm_back_delete: no access to parent\n", 0,
82                                 0, 0 );
83                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
84                                 "", "" );
85                         goto return_results;
86                 }
87 #endif
88
89         } else {
90                 /* no parent, must be root to delete */
91                 if( ! be_isroot( be, op->o_ndn ) ) {
92                         Debug( LDAP_DEBUG_TRACE,
93                                 "<=- ldbm_back_delete: no parent & not root\n",
94                                 0, 0, 0);
95                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
96                                 "", "");
97                         goto return_results;
98                 }
99
100                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
101                 rootlock = 1;
102         }
103
104         if ( id2children_remove( be, p, e ) != 0 ) {
105                 Debug(LDAP_DEBUG_ARGS,
106                         "<=- ldbm_back_delete: operations error %s\n",
107                         dn, 0, 0);
108                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "","" );
109                 goto return_results;
110         }
111
112         /* delete from dn2id mapping */
113         if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
114                 Debug(LDAP_DEBUG_ARGS,
115                         "<=- ldbm_back_delete: operations error %s\n",
116                         dn, 0, 0);
117                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
118                 goto return_results;
119         }
120
121         /* delete from disk and cache */
122         if ( id2entry_delete( be, e ) != 0 ) {
123                 Debug(LDAP_DEBUG_ARGS,
124                         "<=- ldbm_back_delete: operations error %s\n",
125                         dn, 0, 0);
126                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
127                 goto return_results;
128         }
129
130         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
131         rc = 0;
132
133 return_results:;
134         if ( pdn != NULL ) free(pdn);
135
136         if( p != NULL ) {
137                 /* free parent and writer lock */
138                 cache_return_entry_w( &li->li_cache, p );
139         }
140
141         if ( rootlock ) {
142                 /* release root lock */
143                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
144         }
145
146         /* free entry and writer lock */
147         cache_return_entry_w( &li->li_cache, e );
148
149         if ( matched != NULL ) free(matched);
150
151         return rc;
152 }