]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
fix controls propagation (ITS#3813)
[openldap] / servers / slapd / delete.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "ldap_pvt.h"
34 #include "slap.h"
35
36 #include "lutil.h"
37
38 #ifdef LDAP_SLAPI
39 #include "slapi/slapi.h"
40 #endif
41
42 int
43 do_delete(
44     Operation   *op,
45     SlapReply   *rs
46 )
47 {
48         struct berval dn = BER_BVNULL;
49         struct berval pdn = BER_BVNULL;
50         struct berval org_req_dn = BER_BVNULL;
51         struct berval org_req_ndn = BER_BVNULL;
52         struct berval org_dn = BER_BVNULL;
53         struct berval org_ndn = BER_BVNULL;
54         int     org_managedsait;
55         int manageDSAit;
56
57 #ifdef NEW_LOGGING
58         LDAP_LOG( OPERATION, ENTRY, 
59                 "do_delete: conn %d\n", op->o_connid, 0, 0 );
60 #else
61         Debug( LDAP_DEBUG_TRACE, "do_delete\n", 0, 0, 0 );
62 #endif
63
64         /*
65          * Parse the delete request.  It looks like this:
66          *
67          *      DelRequest := DistinguishedName
68          */
69
70         if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
71 #ifdef NEW_LOGGING
72                 LDAP_LOG( OPERATION, ERR, 
73                         "do_delete: conn: %d  ber_scanf failed\n", op->o_connid, 0, 0 );
74 #else
75                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
76 #endif
77                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
78                 return SLAPD_DISCONNECT;
79         }
80
81         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
82 #ifdef NEW_LOGGING
83                 LDAP_LOG( OPERATION, ERR, 
84                         "do_delete: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
85 #else
86                 Debug( LDAP_DEBUG_ANY, "do_delete: get_ctrls failed\n", 0, 0, 0 );
87 #endif
88                 goto cleanup;
89         } 
90
91         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
92         if( rs->sr_err != LDAP_SUCCESS ) {
93 #ifdef NEW_LOGGING
94                 LDAP_LOG( OPERATION, INFO, 
95                         "do_delete: conn %d  invalid dn (%s)\n",
96                         op->o_connid, dn.bv_val, 0 );
97 #else
98                 Debug( LDAP_DEBUG_ANY,
99                         "do_delete: invalid dn (%s)\n", dn.bv_val, 0, 0 );
100 #endif
101                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
102                 goto cleanup;
103         }
104
105         if( op->o_req_ndn.bv_len == 0 ) {
106 #ifdef NEW_LOGGING
107                 LDAP_LOG( OPERATION, INFO, 
108                         "do_delete: conn %d: Attempt to delete root DSE.\n", 
109                         op->o_connid, 0, 0 );
110 #else
111                 Debug( LDAP_DEBUG_ANY, "do_delete: root dse!\n", 0, 0, 0 );
112 #endif
113                 /* protocolError would likely be a more appropriate error */
114                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
115                         "cannot delete the root DSE" );
116                 goto cleanup;
117
118         } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
119 #ifdef NEW_LOGGING
120                 LDAP_LOG( OPERATION, INFO, "do_delete: conn %d: "
121                         "Attempt to delete subschema subentry.\n", op->o_connid, 0, 0 );
122 #else
123                 Debug( LDAP_DEBUG_ANY, "do_delete: subschema subentry!\n", 0, 0, 0 );
124 #endif
125                 /* protocolError would likely be a more appropriate error */
126                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
127                         "cannot delete the root DSE" );
128                 goto cleanup;
129         }
130
131         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu DEL dn=\"%s\"\n",
132                 op->o_connid, op->o_opid, op->o_req_dn.bv_val, 0, 0 );
133
134         manageDSAit = get_manageDSAit( op );
135
136         /*
137          * We could be serving multiple database backends.  Select the
138          * appropriate one, or send a referral to our "referral server"
139          * if we don't hold it.
140          */
141         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
142         if ( op->o_bd == NULL ) {
143                 rs->sr_ref = referral_rewrite( default_referral,
144                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
145
146                 if (!rs->sr_ref) rs->sr_ref = default_referral;
147                 if ( rs->sr_ref != NULL ) {
148                         rs->sr_err = LDAP_REFERRAL;
149
150                         send_ldap_result( op, rs );
151
152                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
153                 } else {
154                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
155                                 "no global superior knowledge" );
156                 }
157                 goto cleanup;
158         }
159
160         /* check restrictions */
161         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
162                 send_ldap_result( op, rs );
163                 goto cleanup;
164         }
165
166         /* check for referrals */
167         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
168                 goto cleanup;
169         }
170
171 #if defined( LDAP_SLAPI )
172 #define pb op->o_pb
173         if ( pb ) {
174                 slapi_int_pblock_set_operation( pb, op );
175                 slapi_pblock_set( pb, SLAPI_DELETE_TARGET, (void *)dn.bv_val );
176                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
177
178                 rs->sr_err = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_DELETE_FN, pb );
179                 if ( rs->sr_err < 0 ) {
180                         /*
181                          * A preoperation plugin failure will abort the
182                          * entire operation.
183                          */
184 #ifdef NEW_LOGGING
185                         LDAP_LOG( OPERATION, INFO, "do_delete: delete preoperation plugin "
186                                         "failed\n", 0, 0, 0 );
187 #else
188                         Debug (LDAP_DEBUG_TRACE, "do_delete: delete preoperation plugin failed.\n",
189                                         0, 0, 0);
190 #endif
191                         if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 )  ||
192                                  rs->sr_err == LDAP_SUCCESS ) {
193                                 rs->sr_err = LDAP_OTHER;
194                         }
195                         goto cleanup;
196                 }
197         }
198 #endif /* defined( LDAP_SLAPI ) */
199
200         /*
201          * do the delete if 1 && (2 || 3)
202          * 1) there is a delete function implemented in this backend;
203          * 2) this backend is master for what it holds;
204          * 3) it's a replica and the dn supplied is the update_ndn.
205          */
206         if ( op->o_bd->be_delete ) {
207                 /* do the update here */
208                 int repl_user = be_isupdate( op );
209 #ifndef SLAPD_MULTIMASTER
210                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
211 #endif
212                 {
213                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
214                         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
215
216                         if ( !repl_user ) {
217                                 struct berval csn = BER_BVNULL;
218                                 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
219                                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
220                         }
221
222 #ifdef SLAPD_MULTIMASTER
223                         if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
224 #endif
225                         {
226                                 cb.sc_next = op->o_callback;
227                                 op->o_callback = &cb;
228                         }
229
230                         op->o_bd->be_delete( op, rs );
231
232                         org_req_dn = op->o_req_dn;
233                         org_req_ndn = op->o_req_ndn;
234                         org_dn = op->o_dn;
235                         org_ndn = op->o_ndn;
236                         org_managedsait = get_manageDSAit( op );
237                         op->o_dn = op->o_bd->be_rootdn;
238                         op->o_ndn = op->o_bd->be_rootndn;
239                         op->o_managedsait = 1;
240
241                         while ( rs->sr_err == LDAP_SUCCESS &&
242                                 op->o_delete_glue_parent )
243                         {
244                                 op->o_delete_glue_parent = 0;
245                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
246                                         slap_callback cb = { NULL };
247                                         cb.sc_response = slap_null_cb;
248                                         dnParent( &op->o_req_ndn, &pdn );
249                                         op->o_req_dn = pdn;
250                                         op->o_req_ndn = pdn;
251                                         op->o_callback = &cb;
252                                         op->o_bd->be_delete( op, rs );
253                                 } else {
254                                         break;
255                                 }
256                         }
257
258                         op->o_managedsait = org_managedsait;
259                         op->o_dn = org_dn;
260                         op->o_ndn = org_ndn;
261                         op->o_req_dn = org_req_dn;
262                         op->o_req_ndn = org_req_ndn;
263                         op->o_delete_glue_parent = 0;
264
265 #ifndef SLAPD_MULTIMASTER
266                 } else {
267                         BerVarray defref = op->o_bd->be_update_refs
268                                 ? op->o_bd->be_update_refs : default_referral;
269
270                         if ( defref != NULL ) {
271                                 rs->sr_ref = referral_rewrite( defref,
272                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
273                                 if (!rs->sr_ref) rs->sr_ref = defref;
274                                 rs->sr_err = LDAP_REFERRAL;
275                                 send_ldap_result( op, rs );
276
277                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
278
279                         } else {
280                                 send_ldap_error( op, rs,
281                                         LDAP_UNWILLING_TO_PERFORM,
282                                         "shadow context; no update referral" );
283                         }
284 #endif
285                 }
286
287         } else {
288                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
289                         "operation not supported within namingContext" );
290         }
291
292 #if defined( LDAP_SLAPI )
293         if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_DELETE_FN, pb ) < 0) {
294 #ifdef NEW_LOGGING
295                 LDAP_LOG( OPERATION, INFO, "do_delete: delete postoperation plugins "
296                                 "failed\n", 0, 0, 0 );
297 #else
298                 Debug(LDAP_DEBUG_TRACE, "do_delete: delete postoperation plugins "
299                                 "failed.\n", 0, 0, 0);
300 #endif
301         }
302 #endif /* defined( LDAP_SLAPI ) */
303
304 cleanup:
305
306         slap_graduate_commit_csn( op );
307
308         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
309         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
310         return rs->sr_err;
311 }