xref: /titanic_52/usr/src/uts/common/fs/smbsrv/smb_delete.c (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <smbsrv/smb_incl.h>
29 #include <smbsrv/smb_fsops.h>
30 #include <smbsrv/smbinfo.h>
31 #include <sys/nbmlock.h>
32 
33 static uint32_t smb_delete_check(smb_request_t *, smb_node_t *);
34 
35 /*
36  * smb_com_delete
37  *
38  * The delete file message is sent to delete a data file. The appropriate
39  * Tid and additional pathname are passed. Read only files may not be
40  * deleted, the read-only attribute must be reset prior to file deletion.
41  *
42  * NT supports a hidden permission known as File Delete Child (FDC). If
43  * the user has FullControl access to a directory, the user is permitted
44  * to delete any object in the directory regardless of the permissions
45  * on the object.
46  *
47  * Client Request                     Description
48  * ================================== =================================
49  * UCHAR WordCount;                   Count of parameter words = 1
50  * USHORT SearchAttributes;
51  * USHORT ByteCount;                  Count of data bytes; min = 2
52  * UCHAR BufferFormat;                0x04
53  * STRING FileName[];                 File name
54  *
55  * Multiple files may be deleted in response to a single request as
56  * SMB_COM_DELETE supports wildcards
57  *
58  * SearchAttributes indicates the attributes that the target file(s) must
59  * have. If the attribute is zero then only normal files are deleted. If
60  * the system file or hidden attributes are specified then the delete is
61  * inclusive -both the specified type(s) of files and normal files are
62  * deleted. Attributes are described in the "Attribute Encoding" section
63  * of this document.
64  *
65  * If bit0 of the Flags2 field of the SMB header is set, a pattern is
66  * passed in, and the file has a long name, then the passed pattern  much
67  * match the long file name for the delete to succeed. If bit0 is clear, a
68  * pattern is passed in, and the file has a long name, then the passed
69  * pattern must match the file's short name for the deletion to succeed.
70  *
71  * Server Response                    Description
72  * ================================== =================================
73  * UCHAR WordCount;                   Count of parameter words = 0
74  * USHORT ByteCount;                  Count of data bytes = 0
75  *
76  * 4.2.10.1  Errors
77  *
78  * ERRDOS/ERRbadpath
79  * ERRDOS/ERRbadfile
80  * ERRDOS/ERRnoaccess
81  * ERRDOS/ERRbadshare	# returned by NT for files that are already open
82  * ERRHRD/ERRnowrite
83  * ERRSRV/ERRaccess
84  * ERRSRV/ERRinvdevice
85  * ERRSRV/ERRinvid
86  * ERRSRV/ERRbaduid
87  */
88 smb_sdrc_t
89 smb_pre_delete(smb_request_t *sr)
90 {
91 	struct smb_fqi *fqi = &sr->arg.dirop.fqi;
92 	int rc;
93 
94 	if ((rc = smbsr_decode_vwv(sr, "w", &fqi->srch_attr)) == 0)
95 		rc = smbsr_decode_data(sr, "%S", sr, &fqi->path);
96 
97 	DTRACE_SMB_2(op__Delete__start, smb_request_t *, sr,
98 	    struct smb_fqi *, fqi);
99 
100 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
101 }
102 
103 void
104 smb_post_delete(smb_request_t *sr)
105 {
106 	DTRACE_SMB_1(op__Delete__done, smb_request_t *, sr);
107 }
108 
109 smb_sdrc_t
110 smb_com_delete(smb_request_t *sr)
111 {
112 	struct smb_fqi *fqi = &sr->arg.dirop.fqi;
113 	int	rc;
114 	int	deleted = 0;
115 	struct smb_node *node = NULL;
116 	smb_odir_context_t *pc;
117 
118 	if (smb_rdir_open(sr, fqi->path, fqi->srch_attr) != 0)
119 		return (SDRC_ERROR);
120 
121 	pc = kmem_zalloc(sizeof (*pc), KM_SLEEP);
122 
123 	/*
124 	 * This while loop is meant to deal with wildcards.
125 	 * It is not expected that wildcards will exist for
126 	 * streams.  For the streams case, it is expected
127 	 * that the below loop will be executed only once.
128 	 */
129 
130 	while ((rc = smb_rdir_next(sr, &node, pc)) == 0) {
131 
132 		if (pc->dc_dattr & FILE_ATTRIBUTE_DIRECTORY) {
133 			smbsr_error(sr, NT_STATUS_FILE_IS_A_DIRECTORY,
134 			    ERRDOS, ERROR_ACCESS_DENIED);
135 			smb_node_release(node);
136 			goto delete_error;
137 		}
138 
139 		if ((pc->dc_dattr & FILE_ATTRIBUTE_READONLY) ||
140 		    (node->flags & NODE_CREATED_READONLY)) {
141 			smbsr_error(sr, NT_STATUS_CANNOT_DELETE,
142 			    ERRDOS, ERROR_ACCESS_DENIED);
143 			smb_node_release(node);
144 			goto delete_error;
145 		}
146 
147 		/*
148 		 * NT does not always close a file immediately, which
149 		 * can cause the share and access checking to fail
150 		 * (the node refcnt is greater than one), and the file
151 		 * doesn't get deleted. Breaking the oplock before
152 		 * share and access checking gives the client a chance
153 		 * to close the file.
154 		 */
155 
156 		smb_oplock_break(node);
157 
158 		smb_node_start_crit(node, RW_READER);
159 
160 		if (smb_delete_check(sr, node) != NT_STATUS_SUCCESS) {
161 			smb_node_end_crit(node);
162 			smb_node_release(node);
163 			goto delete_error;
164 		}
165 
166 		/*
167 		 * Use node->od_name so as to skip mangle checks and
168 		 * stream processing (which have already been done in
169 		 * smb_rdir_next()).
170 		 * Use node->dir_snode to obtain the correct parent node
171 		 * (especially for streams).
172 		 */
173 		rc = smb_fsop_remove(sr, sr->user_cr, node->dir_snode,
174 		    node->od_name, 1);
175 
176 		smb_node_end_crit(node);
177 		smb_node_release(node);
178 		node = NULL;
179 
180 		if (rc != 0) {
181 			if (rc != ENOENT) {
182 				smbsr_errno(sr, rc);
183 				goto delete_error;
184 			}
185 		} else {
186 			deleted++;
187 		}
188 	}
189 
190 	if ((rc != 0) && (rc != ENOENT)) {
191 		smbsr_errno(sr, rc);
192 		goto delete_error;
193 	}
194 
195 	if (deleted == 0) {
196 		if (sr->sid_odir->d_wildcards == 0)
197 			smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND,
198 			    ERRDOS, ERROR_FILE_NOT_FOUND);
199 		else
200 			smbsr_error(sr, NT_STATUS_NO_SUCH_FILE,
201 			    ERRDOS, ERROR_FILE_NOT_FOUND);
202 		goto delete_error;
203 	}
204 
205 	smb_rdir_close(sr);
206 	kmem_free(pc, sizeof (*pc));
207 
208 	rc = smbsr_encode_empty_result(sr);
209 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
210 
211 delete_error:
212 	smb_rdir_close(sr);
213 	kmem_free(pc, sizeof (*pc));
214 	return (SDRC_ERROR);
215 }
216 
217 /*
218  * For consistency with Windows 2000, the range check should be done
219  * after checking for sharing violations.  Attempting to delete a
220  * locked file will result in sharing violation, which is the same
221  * thing that will happen if you try to delete a non-locked open file.
222  *
223  * Note that windows 2000 rejects lock requests on open files that
224  * have been opened with metadata open modes.  The error is
225  * STATUS_ACCESS_DENIED.
226  */
227 static uint32_t
228 smb_delete_check(smb_request_t *sr, smb_node_t *node)
229 {
230 	uint32_t status;
231 
232 	status = smb_node_delete_check(node);
233 
234 	if (status == NT_STATUS_SHARING_VIOLATION) {
235 		smbsr_error(sr, NT_STATUS_SHARING_VIOLATION,
236 		    ERRDOS, ERROR_SHARING_VIOLATION);
237 		return (status);
238 	}
239 
240 	status = smb_range_check(sr, sr->user_cr, node, 0, UINT64_MAX, B_TRUE);
241 
242 	if (status != NT_STATUS_SUCCESS) {
243 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
244 		    ERRDOS, ERROR_ACCESS_DENIED);
245 	}
246 
247 	return (status);
248 }
249