]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/delete.c
00f42b22ace2130012db4e64b613b5e790ef5ec0
[openldap] / servers / slapd / back-sql / delete.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #ifdef SLAPD_SQL
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28
29 #include "slap.h"
30 #include "ldap_pvt.h"
31 #include "proto-sql.h"
32
33 int
34 backsql_delete( Operation *op, SlapReply *rs )
35 {
36         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
37         SQLHDBC                 dbh;
38         SQLHSTMT                sth;
39         RETCODE                 rc;
40         backsql_oc_map_rec      *oc = NULL;
41         backsql_entryID         e_id = BACKSQL_ENTRYID_INIT;
42         Entry                   e;
43         /* first parameter no */
44         SQLUSMALLINT            pno;
45
46         Debug( LDAP_DEBUG_TRACE, "==>backsql_delete(): deleting entry \"%s\"\n",
47                         op->o_req_ndn.bv_val, 0, 0 );
48
49         dnParent( &op->o_req_dn, &e.e_name );
50         dnParent( &op->o_req_ndn, &e.e_nname );
51         e.e_attrs = NULL;
52
53         /* check parent for "children" acl */
54         if ( !access_allowed( op, &e, slap_schema.si_ad_children, 
55                         NULL, ACL_WRITE, NULL ) ) {
56                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
57                         "no write access to parent\n", 
58                         0, 0, 0 );
59                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
60                 goto done;
61
62         }
63         
64         rs->sr_err = backsql_get_db_conn( op, &dbh );
65         if ( rs->sr_err != LDAP_SUCCESS ) {
66                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
67                         "could not get connection handle - exiting\n", 
68                         0, 0, 0 );
69                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
70                         ? "SQL-backend error" : NULL;
71                 goto done;
72         }
73         
74         rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->o_req_ndn );
75         if ( rs->sr_err != LDAP_SUCCESS ) {
76                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
77                         "could not lookup entry id\n", 0, 0, 0 );
78                 goto done;
79         }
80
81         rs->sr_err = backsql_has_children( bi, dbh, &op->o_req_ndn );
82         switch ( rs->sr_err ) {
83         case LDAP_COMPARE_TRUE:
84                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
85                         "entry \"%s\" has children\n",
86                         op->o_req_dn.bv_val, 0, 0 );
87                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
88                 rs->sr_text = "subtree delete not supported";
89                 goto done;
90
91         case LDAP_COMPARE_FALSE:
92                 break;
93
94         default:
95                 goto done;
96         }
97
98         oc = backsql_id2oc( bi, e_id.eid_oc_id );
99         if ( oc == NULL ) {
100                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
101                         "cannot determine objectclass of entry -- aborting\n",
102                         0, 0, 0 );
103                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
104                 rs->sr_text = "operation not permitted within namingContext";
105                 goto done;
106         }
107
108         if ( oc->bom_delete_proc == NULL ) {
109                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
110                         "delete procedure is not defined "
111                         "for this objectclass - aborting\n", 0, 0, 0 );
112                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
113                 rs->sr_text = "operation not permitted within namingContext";
114                 goto done;
115         }
116
117         SQLAllocStmt( dbh, &sth );
118
119         rc = backsql_Prepare( dbh, &sth, oc->bom_delete_proc, 0 );
120         if ( rc != SQL_SUCCESS ) {
121                 Debug( LDAP_DEBUG_TRACE,
122                         "   backsql_delete(): "
123                         "error preparing delete query\n", 
124                         0, 0, 0 );
125                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
126
127                 rs->sr_err = LDAP_OTHER;
128                 rs->sr_text = "SQL-backend error";
129                 goto done;
130         }
131
132         if ( BACKSQL_IS_DEL( oc->bom_expect_return ) ) {
133                 pno = 1;
134                 SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, SQL_C_ULONG,
135                                 SQL_INTEGER, 0, 0, &rc, 0, 0 );
136         } else {
137                 pno = 0;
138         }
139
140 #ifdef BACKSQL_ARBITRARY_KEY
141         SQLBindParameter( sth, pno + 1, SQL_PARAM_INPUT, 
142                         SQL_C_CHAR, SQL_VARCHAR, 0, 0, e_id.eid_keyval.bv_val,
143                         0, 0 );
144 #else /* ! BACKSQL_ARBITRARY_KEY */
145         SQLBindParameter( sth, pno + 1, SQL_PARAM_INPUT, 
146                         SQL_C_ULONG, SQL_INTEGER, 0, 0, &e_id.eid_keyval,
147                         0, 0 );
148 #endif /* ! BACKSQL_ARBITRARY_KEY */
149
150         rc = SQLExecute( sth );
151         if ( rc != SQL_SUCCESS ) {
152                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
153                         "delete_proc execution failed\n", 0, 0, 0 );
154                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
155                 SQLFreeStmt( sth, SQL_DROP );
156                 rs->sr_err = LDAP_OTHER;
157                 rs->sr_text = "SQL-backend error";
158                 goto done;
159         }
160 #ifndef BACKSQL_REALLOC_STMT
161         SQLFreeStmt( sth, SQL_RESET_PARAMS );
162 #else /* BACKSQL_REALLOC_STMT */
163         SQLFreeStmt( sth, SQL_DROP );
164         SQLAllocStmt( dbh, &sth );
165 #endif /* BACKSQL_REALLOC_STMT */
166
167         /* we should do the same for ldap_entry_objclasses and ldap_referrals,
168          * for those RDBMSes that do not allow stored procedures... */
169         rc = backsql_Prepare( dbh, &sth, bi->delobjclasses_query, 0 );
170         if ( rc != SQL_SUCCESS ) {
171                 Debug( LDAP_DEBUG_TRACE,
172                         "   backsql_delete(): "
173                         "error preparing ldap_entry_objclasses delete query\n", 
174                         0, 0, 0 );
175                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
176
177                 rs->sr_err = LDAP_OTHER;
178                 rs->sr_text = "SQL-backend error";
179                 goto done;
180         }
181
182 #ifdef BACKSQL_ARBITRARY_KEY
183         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
184                         0, 0, e_id.eid_id.bv_val, 0, 0 );
185 #else /* ! BACKSQL_ARBITRARY_KEY */
186         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
187                         0, 0, &e_id.eid_id, 0, 0 );
188 #endif /* ! BACKSQL_ARBITRARY_KEY */
189         rc = SQLExecute( sth );
190         switch ( rc ) {
191         case SQL_NO_DATA:
192                 /* apparently there were no "auxiliary" objectClasses
193                  * for this entry... */
194         case SQL_SUCCESS:
195                 break;
196
197         default:
198                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
199                         "failed to delete record from ldap_entry_objclasses\n", 
200                         0, 0, 0 );
201                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
202                 SQLFreeStmt( sth, SQL_DROP );
203                 rs->sr_err = LDAP_OTHER;
204                 rs->sr_text = "SQL-backend error";
205                 goto done;
206         }
207         SQLFreeStmt( sth, SQL_DROP );
208
209         rc = backsql_Prepare( dbh, &sth, bi->delreferrals_query, 0 );
210         if ( rc != SQL_SUCCESS ) {
211                 Debug( LDAP_DEBUG_TRACE,
212                         "   backsql_delete(): "
213                         "error preparing ldap_referrals delete query\n", 
214                         0, 0, 0 );
215                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
216
217                 rs->sr_err = LDAP_OTHER;
218                 rs->sr_text = "SQL-backend error";
219                 goto done;
220         }
221
222 #ifdef BACKSQL_ARBITRARY_KEY
223         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
224                         0, 0, e_id.eid_id.bv_val, 0, 0 );
225 #else /* ! BACKSQL_ARBITRARY_KEY */
226         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
227                         0, 0, &e_id.eid_id, 0, 0 );
228 #endif /* ! BACKSQL_ARBITRARY_KEY */
229         rc = SQLExecute( sth );
230         switch ( rc ) {
231         case SQL_NO_DATA:
232                 /* apparently there were no referrals
233                  * for this entry... */
234         case SQL_SUCCESS:
235                 break;
236
237         default:
238                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
239                         "failed to delete record from ldap_referrals\n", 
240                         0, 0, 0 );
241                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
242                 SQLFreeStmt( sth, SQL_DROP );
243                 rs->sr_err = LDAP_OTHER;
244                 rs->sr_text = "SQL-backend error";
245                 goto done;
246         }
247         SQLFreeStmt( sth, SQL_DROP );
248
249         rc = backsql_Prepare( dbh, &sth, bi->delentry_query, 0 );
250         if ( rc != SQL_SUCCESS ) {
251                 Debug( LDAP_DEBUG_TRACE,
252                         "   backsql_delete(): "
253                         "error preparing ldap_entries delete query\n", 
254                         0, 0, 0 );
255                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
256
257                 rs->sr_err = LDAP_OTHER;
258                 rs->sr_text = "SQL-backend error";
259                 goto done;
260         }
261
262 #ifdef BACKSQL_ARBITRARY_KEY
263         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
264                         0, 0, e_id.eid_id.bv_val, 0, 0 );
265 #else /* ! BACKSQL_ARBITRARY_KEY */
266         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
267                         0, 0, &e_id.eid_id, 0, 0 );
268 #endif /* ! BACKSQL_ARBITRARY_KEY */
269         rc = SQLExecute( sth );
270         if ( rc != SQL_SUCCESS ) {
271                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
272                         "failed to delete record from ldap_entries\n", 
273                         0, 0, 0 );
274                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
275                 SQLFreeStmt( sth, SQL_DROP );
276                 rs->sr_err = LDAP_OTHER;
277                 rs->sr_text = "SQL-backend error";
278                 goto done;
279         }
280         SQLFreeStmt( sth, SQL_DROP );
281
282         /*
283          * Commit only if all operations succeed
284          *
285          * FIXME: backsql_add() does not fail if add operations 
286          * are not available for some attributes, or if
287          * a multiple value add actually results in a replace, 
288          * or if a single operation on an attribute fails 
289          * for any reason
290          */
291         SQLTransact( SQL_NULL_HENV, dbh, 
292                         op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
293
294         rs->sr_err = LDAP_SUCCESS;
295
296 done:;
297         send_ldap_result( op, rs );
298
299         Debug( LDAP_DEBUG_TRACE, "<==backsql_delete()\n", 0, 0, 0 );
300
301         return ( ( rs->sr_err == LDAP_SUCCESS ) ? op->o_noop : 1 );
302 }
303
304 #endif /* SLAPD_SQL */
305