]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/delete.c
Sync with HEAD
[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;
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.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         if ( BACKSQL_IS_DEL( oc->bom_expect_return ) ) {
119                 pno = 1;
120                 SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, SQL_C_ULONG,
121                                 SQL_INTEGER, 0, 0, &rc, 0, 0 );
122         } else {
123                 pno = 0;
124         }
125
126         SQLBindParameter( sth, pno + 1, SQL_PARAM_INPUT, 
127                         SQL_C_ULONG, SQL_INTEGER, 0, 0, &e_id.keyval, 0, 0 );
128
129         Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): executing \"%s\"\n",
130                         oc->bom_delete_proc, 0, 0 );
131         rc = SQLExecDirect( sth, oc->bom_delete_proc, SQL_NTS );
132         if ( rc != SQL_SUCCESS ) {
133                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
134                         "delete_proc execution failed\n", 0, 0, 0 );
135                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
136                 SQLFreeStmt( sth, SQL_DROP );
137                 rs->sr_err = LDAP_OTHER;
138                 rs->sr_text = "SQL-backend error";
139                 goto done;
140         }
141 #ifndef BACKSQL_REALLOC_STMT
142         SQLFreeStmt( sth, SQL_RESET_PARAMS );
143 #else /* BACKSQL_REALLOC_STMT */
144         SQLFreeStmt( sth, SQL_DROP );
145         SQLAllocStmt( dbh, &sth );
146 #endif /* BACKSQL_REALLOC_STMT */
147
148         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
149                         0, 0, &e_id.id, 0, 0 );
150         rc = SQLExecDirect( sth, bi->delentry_query, SQL_NTS );
151         if ( rc != SQL_SUCCESS ) {
152                 Debug( LDAP_DEBUG_TRACE, "   backsql_delete(): "
153                         "failed to delete record from ldap_entries\n", 
154                         0, 0, 0 );
155                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
156                 SQLFreeStmt( sth, SQL_DROP );
157                 rs->sr_err = LDAP_OTHER;
158                 rs->sr_text = "SQL-backend error";
159                 goto done;
160         }
161         
162         SQLFreeStmt( sth, SQL_DROP );
163
164         /*
165          * Commit only if all operations succeed
166          *
167          * FIXME: backsql_add() does not fail if add operations 
168          * are not available for some attributes, or if
169          * a multiple value add actually results in a replace, 
170          * or if a single operation on an attribute fails 
171          * for any reason
172          */
173         SQLTransact( SQL_NULL_HENV, dbh, 
174                         op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
175
176         rs->sr_err = LDAP_SUCCESS;
177
178 done:;
179         send_ldap_result( op, rs );
180
181         Debug( LDAP_DEBUG_TRACE, "<==backsql_delete()\n", 0, 0, 0 );
182
183         return ( ( rs->sr_err == LDAP_SUCCESS ) ? op->o_noop : 1 );
184 }
185
186 #endif /* SLAPD_SQL */
187