2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
26 #include <sys/types.h>
27 #include "ac/string.h"
31 #include "proto-sql.h"
34 backsql_delete( Operation *op, SlapReply *rs )
36 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
40 backsql_oc_map_rec *oc = NULL;
41 backsql_entryID e_id = BACKSQL_ENTRYID_INIT;
43 /* first parameter no */
46 Debug( LDAP_DEBUG_TRACE, "==>backsql_delete(): deleting entry \"%s\"\n",
47 op->o_req_ndn.bv_val, 0, 0 );
49 dnParent( &op->o_req_dn, &e.e_name );
50 dnParent( &op->o_req_ndn, &e.e_nname );
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",
59 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
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",
69 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
70 ? "SQL-backend error" : NULL;
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 );
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";
91 case LDAP_COMPARE_FALSE:
98 oc = backsql_id2oc( bi, e_id.eid_oc_id );
100 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
101 "cannot determine objectclass of entry -- aborting\n",
103 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
104 rs->sr_text = "operation not permitted within namingContext";
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";
117 SQLAllocStmt( dbh, &sth );
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",
125 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
127 rs->sr_err = LDAP_OTHER;
128 rs->sr_text = "SQL-backend error";
132 if ( BACKSQL_IS_DEL( oc->bom_expect_return ) ) {
134 SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, SQL_C_ULONG,
135 SQL_INTEGER, 0, 0, &rc, 0, 0 );
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,
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,
148 #endif /* ! BACKSQL_ARBITRARY_KEY */
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";
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 */
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",
175 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
177 rs->sr_err = LDAP_OTHER;
178 rs->sr_text = "SQL-backend error";
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 );
192 /* apparently there were no "auxiliary" objectClasses
193 * for this entry... */
198 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
199 "failed to delete record from ldap_entry_objclasses\n",
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";
207 SQLFreeStmt( sth, SQL_DROP );
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",
215 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
217 rs->sr_err = LDAP_OTHER;
218 rs->sr_text = "SQL-backend error";
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 );
232 /* apparently there were no referrals
233 * for this entry... */
238 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
239 "failed to delete record from ldap_referrals\n",
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";
247 SQLFreeStmt( sth, SQL_DROP );
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",
255 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
257 rs->sr_err = LDAP_OTHER;
258 rs->sr_text = "SQL-backend error";
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",
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";
280 SQLFreeStmt( sth, SQL_DROP );
283 * Commit only if all operations succeed
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
291 SQLTransact( SQL_NULL_HENV, dbh,
292 op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
294 rs->sr_err = LDAP_SUCCESS;
297 send_ldap_result( op, rs );
299 Debug( LDAP_DEBUG_TRACE, "<==backsql_delete()\n", 0, 0, 0 );
301 return ( ( rs->sr_err == LDAP_SUCCESS ) ? op->o_noop : 1 );
304 #endif /* SLAPD_SQL */