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.
24 #include <sys/types.h>
25 #include "ac/string.h"
28 #include "proto-sql.h"
30 typedef struct backsql_delete_attr_t {
34 backsql_entryID *e_id;
35 } backsql_delete_attr_t;
38 backsql_delete_attr_f( void *v_at, void *v_bda )
40 backsql_at_map_rec *at = (backsql_at_map_rec *)v_at;
41 backsql_delete_attr_t *bda = (backsql_delete_attr_t *)v_bda;
44 rc = backsql_modify_delete_all_values( bda->op,
45 bda->rs, bda->dbh, bda->e_id, at );
47 if ( rc != LDAP_SUCCESS ) {
48 return BACKSQL_AVL_STOP;
51 return BACKSQL_AVL_CONTINUE;
55 backsql_delete_all_attrs(
59 backsql_entryID *e_id,
60 backsql_oc_map_rec *oc )
62 backsql_delete_attr_t bda;
70 rc = avl_apply( oc->bom_attrs, backsql_delete_attr_f, &bda,
71 BACKSQL_AVL_STOP, AVL_INORDER );
72 if ( rc == BACKSQL_AVL_STOP ) {
80 backsql_delete( Operation *op, SlapReply *rs )
82 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
87 backsql_oc_map_rec *oc = NULL;
88 backsql_entryID e_id = BACKSQL_ENTRYID_INIT;
90 /* first parameter no */
93 Debug( LDAP_DEBUG_TRACE, "==>backsql_delete(): deleting entry \"%s\"\n",
94 op->o_req_ndn.bv_val, 0, 0 );
96 dnParent( &op->o_req_dn, &e.e_name );
97 dnParent( &op->o_req_ndn, &e.e_nname );
100 /* check parent for "children" acl */
101 if ( !access_allowed( op, &e, slap_schema.si_ad_children,
102 NULL, ACL_WRITE, NULL ) ) {
103 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
104 "no write access to parent\n",
106 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
111 rs->sr_err = backsql_get_db_conn( op, &dbh );
112 if ( rs->sr_err != LDAP_SUCCESS ) {
113 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
114 "could not get connection handle - exiting\n",
116 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
117 ? "SQL-backend error" : NULL;
121 rs->sr_err = backsql_dn2id( op, rs, &e_id, dbh, &op->o_req_ndn, 1 );
122 if ( rs->sr_err != LDAP_SUCCESS ) {
123 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
124 "could not lookup entry id\n", 0, 0, 0 );
128 rs->sr_err = backsql_has_children( bi, dbh, &op->o_req_ndn );
129 switch ( rs->sr_err ) {
130 case LDAP_COMPARE_TRUE:
131 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
132 "entry \"%s\" has children\n",
133 op->o_req_dn.bv_val, 0, 0 );
134 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
135 rs->sr_text = "subtree delete not supported";
138 case LDAP_COMPARE_FALSE:
145 oc = backsql_id2oc( bi, e_id.eid_oc_id );
147 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
148 "cannot determine objectclass of entry -- aborting\n",
150 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
151 rs->sr_text = "operation not permitted within namingContext";
155 if ( oc->bom_delete_proc == NULL ) {
156 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
157 "delete procedure is not defined "
158 "for this objectclass - aborting\n", 0, 0, 0 );
159 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
160 rs->sr_text = "operation not permitted within namingContext";
165 rs->sr_err = backsql_delete_all_attrs( op, rs, dbh, &e_id, oc );
166 if ( rs->sr_err != LDAP_SUCCESS ) {
170 rc = backsql_Prepare( dbh, &sth, oc->bom_delete_proc, 0 );
171 if ( rc != SQL_SUCCESS ) {
172 Debug( LDAP_DEBUG_TRACE,
173 " backsql_delete(): "
174 "error preparing delete query\n",
176 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
178 rs->sr_err = LDAP_OTHER;
179 rs->sr_text = "SQL-backend error";
183 if ( BACKSQL_IS_DEL( oc->bom_expect_return ) ) {
185 rc = backsql_BindParamInt( sth, 1, SQL_PARAM_OUTPUT, &retval );
186 if ( rc != SQL_SUCCESS ) {
187 Debug( LDAP_DEBUG_TRACE,
188 " backsql_delete(): "
189 "error binding output parameter for objectClass %s\n",
190 oc->bom_oc->soc_cname.bv_val, 0, 0 );
191 backsql_PrintErrors( bi->sql_db_env, dbh,
193 SQLFreeStmt( sth, SQL_DROP );
195 rs->sr_text = "SQL-backend error";
196 rs->sr_err = LDAP_OTHER;
204 rc = backsql_BindParamID( sth, pno + 1, SQL_PARAM_INPUT, &e_id.eid_keyval );
205 if ( rc != SQL_SUCCESS ) {
206 Debug( LDAP_DEBUG_TRACE,
207 " backsql_delete(): "
208 "error binding keyval parameter for objectClass %s\n",
209 oc->bom_oc->soc_cname.bv_val, 0, 0 );
210 backsql_PrintErrors( bi->sql_db_env, dbh,
212 SQLFreeStmt( sth, SQL_DROP );
214 rs->sr_text = "SQL-backend error";
215 rs->sr_err = LDAP_OTHER;
219 rc = SQLExecute( sth );
220 if ( rc != SQL_SUCCESS ) {
221 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
222 "delete_proc execution failed\n", 0, 0, 0 );
223 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
224 SQLFreeStmt( sth, SQL_DROP );
225 rs->sr_err = LDAP_OTHER;
226 rs->sr_text = "SQL-backend error";
229 SQLFreeStmt( sth, SQL_DROP );
231 /* delete "auxiliary" objectClasses, if any... */
232 rc = backsql_Prepare( dbh, &sth, bi->sql_delobjclasses_query, 0 );
233 if ( rc != SQL_SUCCESS ) {
234 Debug( LDAP_DEBUG_TRACE,
235 " backsql_delete(): "
236 "error preparing ldap_entry_objclasses delete query\n",
238 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
240 rs->sr_err = LDAP_OTHER;
241 rs->sr_text = "SQL-backend error";
245 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT, &e_id.eid_id );
246 if ( rc != SQL_SUCCESS ) {
247 Debug( LDAP_DEBUG_TRACE,
248 " backsql_delete(): "
249 "error binding auxiliary objectClasses "
250 "entry ID parameter for objectClass %s\n",
251 oc->bom_oc->soc_cname.bv_val, 0, 0 );
252 backsql_PrintErrors( bi->sql_db_env, dbh,
254 SQLFreeStmt( sth, SQL_DROP );
256 rs->sr_text = "SQL-backend error";
257 rs->sr_err = LDAP_OTHER;
261 rc = SQLExecute( sth );
264 /* apparently there were no "auxiliary" objectClasses
265 * for this entry... */
270 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
271 "failed to delete record from ldap_entry_objclasses\n",
273 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
274 SQLFreeStmt( sth, SQL_DROP );
275 rs->sr_err = LDAP_OTHER;
276 rs->sr_text = "SQL-backend error";
279 SQLFreeStmt( sth, SQL_DROP );
281 /* delete referrals, if any... */
282 rc = backsql_Prepare( dbh, &sth, bi->sql_delreferrals_query, 0 );
283 if ( rc != SQL_SUCCESS ) {
284 Debug( LDAP_DEBUG_TRACE,
285 " backsql_delete(): "
286 "error preparing ldap_referrals delete query\n",
288 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
290 rs->sr_err = LDAP_OTHER;
291 rs->sr_text = "SQL-backend error";
295 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT, &e_id.eid_id );
296 if ( rc != SQL_SUCCESS ) {
297 Debug( LDAP_DEBUG_TRACE,
298 " backsql_delete(): "
299 "error binding referrals entry ID parameter "
300 "for objectClass %s\n",
301 oc->bom_oc->soc_cname.bv_val, 0, 0 );
302 backsql_PrintErrors( bi->sql_db_env, dbh,
304 SQLFreeStmt( sth, SQL_DROP );
306 rs->sr_text = "SQL-backend error";
307 rs->sr_err = LDAP_OTHER;
311 rc = SQLExecute( sth );
314 /* apparently there were no referrals
315 * for this entry... */
320 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
321 "failed to delete record from ldap_referrals\n",
323 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
324 SQLFreeStmt( sth, SQL_DROP );
325 rs->sr_err = LDAP_OTHER;
326 rs->sr_text = "SQL-backend error";
329 SQLFreeStmt( sth, SQL_DROP );
331 /* delete entry... */
332 rc = backsql_Prepare( dbh, &sth, bi->sql_delentry_query, 0 );
333 if ( rc != SQL_SUCCESS ) {
334 Debug( LDAP_DEBUG_TRACE,
335 " backsql_delete(): "
336 "error preparing ldap_entries delete query\n",
338 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
340 rs->sr_err = LDAP_OTHER;
341 rs->sr_text = "SQL-backend error";
345 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT, &e_id.eid_id );
346 if ( rc != SQL_SUCCESS ) {
347 Debug( LDAP_DEBUG_TRACE,
348 " backsql_delete(): "
349 "error binding entry ID parameter "
350 "for objectClass %s\n",
351 oc->bom_oc->soc_cname.bv_val, 0, 0 );
352 backsql_PrintErrors( bi->sql_db_env, dbh,
354 SQLFreeStmt( sth, SQL_DROP );
356 rs->sr_text = "SQL-backend error";
357 rs->sr_err = LDAP_OTHER;
361 rc = SQLExecute( sth );
362 if ( rc != SQL_SUCCESS ) {
363 Debug( LDAP_DEBUG_TRACE, " backsql_delete(): "
364 "failed to delete record from ldap_entries\n",
366 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
367 SQLFreeStmt( sth, SQL_DROP );
368 rs->sr_err = LDAP_OTHER;
369 rs->sr_text = "SQL-backend error";
372 SQLFreeStmt( sth, SQL_DROP );
375 * Commit only if all operations succeed
377 * FIXME: backsql_add() does not fail if add operations
378 * are not available for some attributes, or if
379 * a multiple value add actually results in a replace,
380 * or if a single operation on an attribute fails
383 SQLTransact( SQL_NULL_HENV, dbh,
384 op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
386 rs->sr_err = LDAP_SUCCESS;
389 send_ldap_result( op, rs );
391 Debug( LDAP_DEBUG_TRACE, "<==backsql_delete()\n", 0, 0, 0 );
393 return ( ( rs->sr_err == LDAP_SUCCESS ) ? op->o_noop : 1 );