]> git.sur5r.net Git - openldap/blob - servers/slapd/delete.c
fix ITS#3834
[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 "slap.h"
34
35 #include "lutil.h"
36
37 #ifdef LDAP_SLAPI
38 #include "slapi/slapi.h"
39 #endif
40
41 int
42 do_delete(
43     Operation   *op,
44     SlapReply   *rs )
45 {
46         struct berval dn = BER_BVNULL;
47
48         Debug( LDAP_DEBUG_TRACE, "do_delete\n", 0, 0, 0 );
49
50         /*
51          * Parse the delete request.  It looks like this:
52          *
53          *      DelRequest := DistinguishedName
54          */
55
56         if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
57                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
58                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
59                 return SLAPD_DISCONNECT;
60         }
61
62         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
63                 Debug( LDAP_DEBUG_ANY, "do_delete: get_ctrls failed\n", 0, 0, 0 );
64                 goto cleanup;
65         } 
66
67         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
68                 op->o_tmpmemctx );
69         if( rs->sr_err != LDAP_SUCCESS ) {
70                 Debug( LDAP_DEBUG_ANY,
71                         "do_delete: invalid dn (%s)\n", dn.bv_val, 0, 0 );
72                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
73                 goto cleanup;
74         }
75
76         if( op->o_req_ndn.bv_len == 0 ) {
77                 Debug( LDAP_DEBUG_ANY, "do_delete: root dse!\n", 0, 0, 0 );
78                 /* protocolError would likely be a more appropriate error */
79                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
80                         "cannot delete the root DSE" );
81                 goto cleanup;
82
83         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
84                 Debug( LDAP_DEBUG_ANY, "do_delete: subschema subentry!\n", 0, 0, 0 );
85                 /* protocolError would likely be a more appropriate error */
86                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
87                         "cannot delete the root DSE" );
88                 goto cleanup;
89         }
90
91         Statslog( LDAP_DEBUG_STATS, "%s DEL dn=\"%s\"\n",
92                 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
93
94         op->o_bd = frontendDB;
95         rs->sr_err = frontendDB->be_delete( op, rs );
96
97 cleanup:;
98         slap_graduate_commit_csn( op );
99
100         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
101         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
102         return rs->sr_err;
103 }
104
105 int
106 fe_op_delete( Operation *op, SlapReply *rs )
107 {
108         struct berval   pdn = BER_BVNULL;
109         int             manageDSAit;
110         BackendDB *op_be;
111         
112         manageDSAit = get_manageDSAit( op );
113
114         /*
115          * We could be serving multiple database backends.  Select the
116          * appropriate one, or send a referral to our "referral server"
117          * if we don't hold it.
118          */
119         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
120         if ( op->o_bd == NULL ) {
121                 rs->sr_ref = referral_rewrite( default_referral,
122                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
123
124                 if (!rs->sr_ref) rs->sr_ref = default_referral;
125                 if ( rs->sr_ref != NULL ) {
126                         rs->sr_err = LDAP_REFERRAL;
127
128                         op->o_bd = frontendDB;
129                         send_ldap_result( op, rs );
130                         op->o_bd = NULL;
131
132                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
133                 } else {
134                         op->o_bd = frontendDB;
135                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
136                                 "no global superior knowledge" );
137                         op->o_bd = NULL;
138                 }
139                 goto cleanup;
140         }
141
142         /* If we've got a glued backend, check the real backend */
143         op_be = op->o_bd;
144         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
145                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
146         }
147
148         /* check restrictions */
149         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
150                 send_ldap_result( op, rs );
151                 goto cleanup;
152         }
153
154         /* check for referrals */
155         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
156                 goto cleanup;
157         }
158
159 #if defined( LDAP_SLAPI )
160 #define pb op->o_pb
161         if ( pb ) {
162                 slapi_int_pblock_set_operation( pb, op );
163                 slapi_pblock_set( pb, SLAPI_DELETE_TARGET, (void *)op->o_req_dn.bv_val );
164                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
165
166                 rs->sr_err = slapi_int_call_plugins( op->o_bd,
167                         SLAPI_PLUGIN_PRE_DELETE_FN, pb );
168                 if ( rs->sr_err < 0 ) {
169                         /*
170                          * A preoperation plugin failure will abort the
171                          * entire operation.
172                          */
173                         Debug (LDAP_DEBUG_TRACE, "do_delete: "
174                                 "delete preoperation plugin failed.\n", 0, 0, 0);
175                         if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE,
176                                 (void *)&rs->sr_err ) != 0 ) ||
177                                 rs->sr_err == LDAP_SUCCESS )
178                         {
179                                 rs->sr_err = LDAP_OTHER;
180                         }
181                         goto cleanup;
182                 }
183         }
184 #endif /* defined( LDAP_SLAPI ) */
185
186         /*
187          * do the delete if 1 && (2 || 3)
188          * 1) there is a delete function implemented in this backend;
189          * 2) this backend is master for what it holds;
190          * 3) it's a replica and the dn supplied is the update_ndn.
191          */
192         if ( op->o_bd->be_delete ) {
193                 /* do the update here */
194                 int repl_user = be_isupdate( op );
195 #ifndef SLAPD_MULTIMASTER
196                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
197 #endif
198                 {
199                         struct berval   org_req_dn = BER_BVNULL;
200                         struct berval   org_req_ndn = BER_BVNULL;
201                         struct berval   org_dn = BER_BVNULL;
202                         struct berval   org_ndn = BER_BVNULL;
203                         int             org_managedsait;
204                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
205
206                         op->o_bd = op_be;
207
208                         if ( !repl_user ) {
209                                 struct berval csn = BER_BVNULL;
210                                 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
211                                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
212                         }
213
214 #ifdef SLAPD_MULTIMASTER
215                         if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
216 #endif
217                         {
218                                 cb.sc_next = op->o_callback;
219                                 op->o_callback = &cb;
220                         }
221
222                         op->o_bd->be_delete( op, rs );
223
224                         org_req_dn = op->o_req_dn;
225                         org_req_ndn = op->o_req_ndn;
226                         org_dn = op->o_dn;
227                         org_ndn = op->o_ndn;
228                         org_managedsait = get_manageDSAit( op );
229                         op->o_dn = op->o_bd->be_rootdn;
230                         op->o_ndn = op->o_bd->be_rootndn;
231                         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
232
233                         while ( rs->sr_err == LDAP_SUCCESS &&
234                                 op->o_delete_glue_parent )
235                         {
236                                 op->o_delete_glue_parent = 0;
237                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
238                                         slap_callback cb = { NULL };
239                                         cb.sc_response = slap_null_cb;
240                                         dnParent( &op->o_req_ndn, &pdn );
241                                         op->o_req_dn = pdn;
242                                         op->o_req_ndn = pdn;
243                                         op->o_callback = &cb;
244                                         op->o_bd->be_delete( op, rs );
245                                 } else {
246                                         break;
247                                 }
248                         }
249
250                         op->o_managedsait = org_managedsait;
251                         op->o_dn = org_dn;
252                         op->o_ndn = org_ndn;
253                         op->o_req_dn = org_req_dn;
254                         op->o_req_ndn = org_req_ndn;
255                         op->o_delete_glue_parent = 0;
256
257 #ifndef SLAPD_MULTIMASTER
258                 } else {
259                         BerVarray defref = op->o_bd->be_update_refs
260                                 ? op->o_bd->be_update_refs : default_referral;
261
262                         if ( defref != NULL ) {
263                                 rs->sr_ref = referral_rewrite( defref,
264                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
265                                 if (!rs->sr_ref) rs->sr_ref = defref;
266                                 rs->sr_err = LDAP_REFERRAL;
267                                 send_ldap_result( op, rs );
268
269                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
270
271                         } else {
272                                 send_ldap_error( op, rs,
273                                         LDAP_UNWILLING_TO_PERFORM,
274                                         "shadow context; no update referral" );
275                         }
276 #endif
277                 }
278
279         } else {
280                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
281                         "operation not supported within namingContext" );
282         }
283
284 #if defined( LDAP_SLAPI )
285         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
286                 SLAPI_PLUGIN_POST_DELETE_FN, pb ) < 0)
287         {
288                 Debug(LDAP_DEBUG_TRACE,
289                         "do_delete: delete postoperation plugins failed\n",
290                         0, 0, 0 );
291         }
292 #endif /* defined( LDAP_SLAPI ) */
293
294 cleanup:;
295         return rs->sr_err;
296 }