xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/delete.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: delete.c,v 1.1.1.6 2018/02/06 01:53:14 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2017 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 <sys/cdefs.h>
29 __RCSID("$NetBSD: delete.c,v 1.1.1.6 2018/02/06 01:53:14 christos Exp $");
30 
31 #include "portable.h"
32 
33 #include <stdio.h>
34 
35 #include <ac/string.h>
36 #include <ac/socket.h>
37 
38 #include "slap.h"
39 
40 #include "lutil.h"
41 
42 int
43 do_delete(
44     Operation	*op,
45     SlapReply	*rs )
46 {
47 	struct berval dn = BER_BVNULL;
48 
49 	Debug( LDAP_DEBUG_TRACE, "%s do_delete\n",
50 		op->o_log_prefix, 0, 0 );
51 	/*
52 	 * Parse the delete request.  It looks like this:
53 	 *
54 	 *	DelRequest := DistinguishedName
55 	 */
56 
57 	if ( ber_scanf( op->o_ber, "m", &dn ) == LBER_ERROR ) {
58 		Debug( LDAP_DEBUG_ANY, "%s do_delete: ber_scanf failed\n",
59 			op->o_log_prefix, 0, 0 );
60 		send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
61 		return SLAPD_DISCONNECT;
62 	}
63 
64 	if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
65 		Debug( LDAP_DEBUG_ANY, "%s do_delete: get_ctrls failed\n",
66 			op->o_log_prefix, 0, 0 );
67 		goto cleanup;
68 	}
69 
70 	rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
71 		op->o_tmpmemctx );
72 	if( rs->sr_err != LDAP_SUCCESS ) {
73 		Debug( LDAP_DEBUG_ANY, "%s do_delete: invalid dn (%s)\n",
74 			op->o_log_prefix, dn.bv_val, 0 );
75 		send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
76 		goto cleanup;
77 	}
78 
79 	Statslog( LDAP_DEBUG_STATS, "%s DEL dn=\"%s\"\n",
80 		op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
81 
82 	if( op->o_req_ndn.bv_len == 0 ) {
83 		Debug( LDAP_DEBUG_ANY, "%s do_delete: root dse!\n",
84 			op->o_log_prefix, 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 	} else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
91 		Debug( LDAP_DEBUG_ANY, "%s do_delete: subschema subentry!\n",
92 			op->o_log_prefix, 0, 0 );
93 		/* protocolError would likely be a more appropriate error */
94 		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
95 			"cannot delete the root DSE" );
96 		goto cleanup;
97 	}
98 
99 	op->o_bd = frontendDB;
100 	rs->sr_err = frontendDB->be_delete( op, rs );
101 
102 #ifdef LDAP_X_TXN
103 	if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
104 		/* skip cleanup */
105 		return rs->sr_err;
106 	}
107 #endif
108 
109 cleanup:;
110 	op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
111 	op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
112 	return rs->sr_err;
113 }
114 
115 int
116 fe_op_delete( Operation *op, SlapReply *rs )
117 {
118 	struct berval	pdn = BER_BVNULL;
119 	BackendDB	*op_be, *bd = op->o_bd;
120 
121 	/*
122 	 * We could be serving multiple database backends.  Select the
123 	 * appropriate one, or send a referral to our "referral server"
124 	 * if we don't hold it.
125 	 */
126 	op->o_bd = select_backend( &op->o_req_ndn, 1 );
127 	if ( op->o_bd == NULL ) {
128 		op->o_bd = bd;
129 		rs->sr_ref = referral_rewrite( default_referral,
130 			NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
131 
132 		if (!rs->sr_ref) rs->sr_ref = default_referral;
133 		if ( rs->sr_ref != NULL ) {
134 			rs->sr_err = LDAP_REFERRAL;
135 			send_ldap_result( op, rs );
136 
137 			if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
138 		} else {
139 			send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
140 				"no global superior knowledge" );
141 		}
142 		goto cleanup;
143 	}
144 
145 	/* If we've got a glued backend, check the real backend */
146 	op_be = op->o_bd;
147 	if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
148 		op->o_bd = select_backend( &op->o_req_ndn, 0 );
149 	}
150 
151 	/* check restrictions */
152 	if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
153 		send_ldap_result( op, rs );
154 		goto cleanup;
155 	}
156 
157 	/* check for referrals */
158 	if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
159 		goto cleanup;
160 	}
161 
162 	/*
163 	 * do the delete if 1 && (2 || 3)
164 	 * 1) there is a delete function implemented in this backend;
165 	 * 2) this backend is master for what it holds;
166 	 * 3) it's a replica and the dn supplied is the update_ndn.
167 	 */
168 	if ( op->o_bd->be_delete ) {
169 		/* do the update here */
170 		int repl_user = be_isupdate( op );
171 		if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
172 			struct berval	org_req_dn = BER_BVNULL;
173 			struct berval	org_req_ndn = BER_BVNULL;
174 			struct berval	org_dn = BER_BVNULL;
175 			struct berval	org_ndn = BER_BVNULL;
176 			int		org_managedsait;
177 
178 			op->o_bd = op_be;
179 			op->o_bd->be_delete( op, rs );
180 
181 			org_req_dn = op->o_req_dn;
182 			org_req_ndn = op->o_req_ndn;
183 			org_dn = op->o_dn;
184 			org_ndn = op->o_ndn;
185 			org_managedsait = get_manageDSAit( op );
186 			op->o_dn = op->o_bd->be_rootdn;
187 			op->o_ndn = op->o_bd->be_rootndn;
188 			op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
189 
190 			while ( rs->sr_err == LDAP_SUCCESS &&
191 				op->o_delete_glue_parent )
192 			{
193 				op->o_delete_glue_parent = 0;
194 				if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
195 					slap_callback cb = { NULL, NULL, NULL, NULL };
196 					cb.sc_response = slap_null_cb;
197 					dnParent( &op->o_req_ndn, &pdn );
198 					op->o_req_dn = pdn;
199 					op->o_req_ndn = pdn;
200 					op->o_callback = &cb;
201 					op->o_bd->be_delete( op, rs );
202 				} else {
203 					break;
204 				}
205 			}
206 
207 			op->o_managedsait = org_managedsait;
208 			op->o_dn = org_dn;
209 			op->o_ndn = org_ndn;
210 			op->o_req_dn = org_req_dn;
211 			op->o_req_ndn = org_req_ndn;
212 			op->o_delete_glue_parent = 0;
213 
214 		} else {
215 			BerVarray defref = op->o_bd->be_update_refs
216 				? op->o_bd->be_update_refs : default_referral;
217 
218 			if ( defref != NULL ) {
219 				rs->sr_ref = referral_rewrite( defref,
220 					NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
221 				if (!rs->sr_ref) rs->sr_ref = defref;
222 				rs->sr_err = LDAP_REFERRAL;
223 				send_ldap_result( op, rs );
224 
225 				if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
226 
227 			} else {
228 				send_ldap_error( op, rs,
229 					LDAP_UNWILLING_TO_PERFORM,
230 					"shadow context; no update referral" );
231 			}
232 		}
233 
234 	} else {
235 		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
236 			"operation not supported within namingContext" );
237 	}
238 
239 cleanup:;
240 	op->o_bd = bd;
241 	return rs->sr_err;
242 }
243