]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/delete.c
3f4cfb99a44f3b331720d3e3e1a028ddbceccc89
[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         rc = backsql_Prepare( dbh, &sth, bi->delentry_query, 0 );
168         if ( rc != SQL_SUCCESS ) {
169                 Debug( LDAP_DEBUG_TRACE,
170                         "   backsql_delete(): "
171                         "error preparing ldap_entries delete query\n", 
172                         0, 0, 0 );
173                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
174
175                 rs->sr_err = LDAP_OTHER;
176                 rs->sr_text = "SQL-backend error";
177                 goto done;
178         }
179
180 #ifdef BACKSQL_ARBITRARY_KEY
181         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
182                         0, 0, e_id.eid_id.bv_val, 0, 0 );
183 #else /* ! BACKSQL_ARBITRARY_KEY */
184         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
185                         0, 0, &e_id.eid_id, 0, 0 );
186 #endif /* ! BACKSQL_ARBITRARY_KEY */
187         rc = SQLExecute( sth );
188         if ( rc != SQL_SUCCESS ) {
189                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
190                         "failed to delete record from ldap_entries\n", 
191                         0, 0, 0 );
192                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
193                 SQLFreeStmt( sth, SQL_DROP );
194                 rs->sr_err = LDAP_OTHER;
195                 rs->sr_text = "SQL-backend error";
196                 goto done;
197         }
198
199         /*
200          * Commit only if all operations succeed
201          *
202          * FIXME: backsql_add() does not fail if add operations 
203          * are not available for some attributes, or if
204          * a multiple value add actually results in a replace, 
205          * or if a single operation on an attribute fails 
206          * for any reason
207          */
208         SQLTransact( SQL_NULL_HENV, dbh, 
209                         op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
210
211         rs->sr_err = LDAP_SUCCESS;
212
213 done:;
214         send_ldap_result( op, rs );
215
216         Debug( LDAP_DEBUG_TRACE, "<==backsql_delete()\n", 0, 0, 0 );
217
218         return ( ( rs->sr_err == LDAP_SUCCESS ) ? op->o_noop : 1 );
219 }
220
221 #endif /* SLAPD_SQL */
222