1/*	$NetBSD: delete.c,v 1.1.1.3 2010/12/12 15:22:29 adam Exp $	*/
2
3/* OpenLDAP: pkg/ldap/servers/slapd/delete.c,v 1.138.2.5 2010/04/13 20:23:14 kurt Exp */
4/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 *
6 * Copyright 1998-2010 The OpenLDAP Foundation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17/* Portions Copyright (c) 1995 Regents of the University of Michigan.
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms are permitted
21 * provided that this notice is preserved and that due credit is given
22 * to the University of Michigan at Ann Arbor. The name of the University
23 * may not be used to endorse or promote products derived from this
24 * software without specific prior written permission. This software
25 * is provided ``as is'' without express or implied warranty.
26 */
27
28#include "portable.h"
29
30#include <stdio.h>
31
32#include <ac/string.h>
33#include <ac/socket.h>
34
35#include "slap.h"
36
37#include "lutil.h"
38
39int
40do_delete(
41    Operation	*op,
42    SlapReply	*rs )
43{
44	struct berval dn = BER_BVNULL;
45
46	Debug( LDAP_DEBUG_TRACE, "%s do_delete\n",
47		op->o_log_prefix, 0, 0 );
48	/*
49	 * Parse the delete request.  It looks like this:
50	 *
51	 *	DelRequest := DistinguishedName
52	 */
53
54	if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
55		Debug( LDAP_DEBUG_ANY, "%s do_delete: ber_scanf failed\n",
56			op->o_log_prefix, 0, 0 );
57		send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
58		return SLAPD_DISCONNECT;
59	}
60
61	if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
62		Debug( LDAP_DEBUG_ANY, "%s do_delete: get_ctrls failed\n",
63			op->o_log_prefix, 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, "%s do_delete: invalid dn (%s)\n",
71			op->o_log_prefix, dn.bv_val, 0 );
72		send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
73		goto cleanup;
74	}
75
76	Statslog( LDAP_DEBUG_STATS, "%s DEL dn=\"%s\"\n",
77		op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
78
79	if( op->o_req_ndn.bv_len == 0 ) {
80		Debug( LDAP_DEBUG_ANY, "%s do_delete: root dse!\n",
81			op->o_log_prefix, 0, 0 );
82		/* protocolError would likely be a more appropriate error */
83		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
84			"cannot delete the root DSE" );
85		goto cleanup;
86
87	} else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
88		Debug( LDAP_DEBUG_ANY, "%s do_delete: subschema subentry!\n",
89			op->o_log_prefix, 0, 0 );
90		/* protocolError would likely be a more appropriate error */
91		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
92			"cannot delete the root DSE" );
93		goto cleanup;
94	}
95
96	op->o_bd = frontendDB;
97	rs->sr_err = frontendDB->be_delete( op, rs );
98
99#ifdef LDAP_X_TXN
100	if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
101		/* skip cleanup */
102		return rs->sr_err;
103	}
104#endif
105
106cleanup:;
107	op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
108	op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
109	return rs->sr_err;
110}
111
112int
113fe_op_delete( Operation *op, SlapReply *rs )
114{
115	struct berval	pdn = BER_BVNULL;
116	BackendDB	*op_be, *bd = op->o_bd;
117
118	/*
119	 * We could be serving multiple database backends.  Select the
120	 * appropriate one, or send a referral to our "referral server"
121	 * if we don't hold it.
122	 */
123	op->o_bd = select_backend( &op->o_req_ndn, 1 );
124	if ( op->o_bd == NULL ) {
125		op->o_bd = bd;
126		rs->sr_ref = referral_rewrite( default_referral,
127			NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
128
129		if (!rs->sr_ref) rs->sr_ref = default_referral;
130		if ( rs->sr_ref != NULL ) {
131			rs->sr_err = LDAP_REFERRAL;
132			send_ldap_result( op, rs );
133
134			if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
135		} else {
136			send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
137				"no global superior knowledge" );
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, 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	/*
160	 * do the delete if 1 && (2 || 3)
161	 * 1) there is a delete function implemented in this backend;
162	 * 2) this backend is master for what it holds;
163	 * 3) it's a replica and the dn supplied is the update_ndn.
164	 */
165	if ( op->o_bd->be_delete ) {
166		/* do the update here */
167		int repl_user = be_isupdate( op );
168		if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
169			struct berval	org_req_dn = BER_BVNULL;
170			struct berval	org_req_ndn = BER_BVNULL;
171			struct berval	org_dn = BER_BVNULL;
172			struct berval	org_ndn = BER_BVNULL;
173			int		org_managedsait;
174
175			op->o_bd = op_be;
176			op->o_bd->be_delete( op, rs );
177
178			org_req_dn = op->o_req_dn;
179			org_req_ndn = op->o_req_ndn;
180			org_dn = op->o_dn;
181			org_ndn = op->o_ndn;
182			org_managedsait = get_manageDSAit( op );
183			op->o_dn = op->o_bd->be_rootdn;
184			op->o_ndn = op->o_bd->be_rootndn;
185			op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
186
187			while ( rs->sr_err == LDAP_SUCCESS &&
188				op->o_delete_glue_parent )
189			{
190				op->o_delete_glue_parent = 0;
191				if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
192					slap_callback cb = { NULL, NULL, NULL, NULL };
193					cb.sc_response = slap_null_cb;
194					dnParent( &op->o_req_ndn, &pdn );
195					op->o_req_dn = pdn;
196					op->o_req_ndn = pdn;
197					op->o_callback = &cb;
198					op->o_bd->be_delete( op, rs );
199				} else {
200					break;
201				}
202			}
203
204			op->o_managedsait = org_managedsait;
205			op->o_dn = org_dn;
206			op->o_ndn = org_ndn;
207			op->o_req_dn = org_req_dn;
208			op->o_req_ndn = org_req_ndn;
209			op->o_delete_glue_parent = 0;
210
211		} else {
212			BerVarray defref = op->o_bd->be_update_refs
213				? op->o_bd->be_update_refs : default_referral;
214
215			if ( defref != NULL ) {
216				rs->sr_ref = referral_rewrite( defref,
217					NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
218				if (!rs->sr_ref) rs->sr_ref = defref;
219				rs->sr_err = LDAP_REFERRAL;
220				send_ldap_result( op, rs );
221
222				if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
223
224			} else {
225				send_ldap_error( op, rs,
226					LDAP_UNWILLING_TO_PERFORM,
227					"shadow context; no update referral" );
228			}
229		}
230
231	} else {
232		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
233			"operation not supported within namingContext" );
234	}
235
236cleanup:;
237	op->o_bd = bd;
238	return rs->sr_err;
239}
240