]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
d0be72d6c62fcd9578bcae1c6f607f82220dd43c
[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                 /* check parent for "children" acl */
76                 if ( ! access_allowed( be, conn, op, p,
77                         "children", NULL, ACL_WRITE ) )
78                 {
79                         Debug( LDAP_DEBUG_TRACE,
80                                 "<=- ldbm_back_delete: no access to parent\n", 0,
81                                 0, 0 );
82                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
83                                 "", "" );
84                         goto return_results;
85                 }
86
87         } else {
88                 /* no parent, must be root to delete */
89                 if( ! be_isroot( be, op->o_ndn ) ) {
90                         Debug( LDAP_DEBUG_TRACE,
91                                 "<=- ldbm_back_delete: no parent & not root\n",
92                                 0, 0, 0);
93                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
94                                 "", "");
95                         goto return_results;
96                 }
97
98                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
99                 rootlock = 1;
100         }
101
102         if ( id2children_remove( be, p, e ) != 0 ) {
103                 Debug(LDAP_DEBUG_ARGS,
104                         "<=- ldbm_back_delete: operations error %s\n",
105                         dn, 0, 0);
106                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "","" );
107                 goto return_results;
108         }
109
110         /* delete from dn2id mapping */
111         if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
112                 Debug(LDAP_DEBUG_ARGS,
113                         "<=- ldbm_back_delete: operations error %s\n",
114                         dn, 0, 0);
115                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
116                 goto return_results;
117         }
118
119         /* delete from disk and cache */
120         if ( id2entry_delete( be, e ) != 0 ) {
121                 Debug(LDAP_DEBUG_ARGS,
122                         "<=- ldbm_back_delete: operations error %s\n",
123                         dn, 0, 0);
124                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
125                 goto return_results;
126         }
127
128         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
129         rc = 0;
130
131 return_results:;
132         if ( pdn != NULL ) free(pdn);
133
134         if( p != NULL ) {
135                 /* free parent and writer lock */
136                 cache_return_entry_w( &li->li_cache, p );
137         }
138
139         if ( rootlock ) {
140                 /* release root lock */
141                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
142         }
143
144         /* free entry and writer lock */
145         cache_return_entry_w( &li->li_cache, e );
146
147         if ( matched != NULL ) free(matched);
148
149         return rc;
150 }