xref: /titanic_41/usr/src/uts/common/fs/smbsrv/smb_delete.c (revision f52e84dfce89ba2ea0ebb6a191b4466da3012efd)
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	od = 0;
115 	int	deleted = 0;
116 	struct smb_node *dir_snode;
117 	struct smb_node *node = 0;
118 	char *name;
119 	char *fname;
120 	char *sname;
121 	char *fullname;
122 	int is_stream;
123 	smb_odir_context_t *pc;
124 
125 	if (smb_rdir_open(sr, fqi->path, fqi->srch_attr) != 0)
126 		return (SDRC_ERROR);
127 
128 	pc = kmem_zalloc(sizeof (*pc), KM_SLEEP);
129 	fname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
130 	sname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
131 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
132 	fullname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
133 
134 	is_stream = smb_stream_parse_name(fqi->path, fname, sname);
135 	dir_snode = sr->sid_odir->d_dir_snode;
136 
137 	/*
138 	 * This while loop is meant to deal with wildcards.
139 	 * It is not expected that wildcards will exist for
140 	 * streams.  For the streams case, it is expected
141 	 * that the below loop will be executed only once.
142 	 */
143 
144 	while ((rc = smb_rdir_next(sr, &node, pc)) == 0) {
145 		(void) strlcpy(name, pc->dc_name, MAXNAMELEN);
146 
147 		if (pc->dc_dattr & SMB_FA_DIRECTORY) {
148 			smbsr_error(sr, NT_STATUS_FILE_IS_A_DIRECTORY,
149 			    ERRDOS, ERROR_ACCESS_DENIED);
150 			smb_node_release(node);
151 			goto delete_error;
152 		}
153 
154 		if ((pc->dc_dattr & SMB_FA_READONLY) ||
155 		    (node->flags & NODE_CREATED_READONLY)) {
156 			smbsr_error(sr, NT_STATUS_CANNOT_DELETE,
157 			    ERRDOS, ERROR_ACCESS_DENIED);
158 			smb_node_release(node);
159 			goto delete_error;
160 		}
161 
162 		/*
163 		 * NT does not always close a file immediately, which
164 		 * can cause the share and access checking to fail
165 		 * (the node refcnt is greater than one), and the file
166 		 * doesn't get deleted. Breaking the oplock before
167 		 * share and access checking gives the client a chance
168 		 * to close the file.
169 		 */
170 
171 		smb_oplock_break(node);
172 
173 		smb_node_start_crit(node, RW_READER);
174 
175 		if (smb_delete_check(sr, node) != NT_STATUS_SUCCESS) {
176 			smb_node_end_crit(node);
177 			smb_node_release(node);
178 			goto delete_error;
179 		}
180 
181 		if (is_stream) {
182 			/*
183 			 * It is assumed that fname does not contain
184 			 * any wildcards .
185 			 * smb_fsop_remove() requires filename+streamname
186 			 */
187 			(void) snprintf(fullname, MAXPATHLEN, "%s%s",
188 			    fname, sname);
189 			rc = smb_fsop_remove(sr, sr->user_cr, dir_snode,
190 			    fullname, 0);
191 		} else {
192 			/*
193 			 * name (i.e. pc->dc_name) is the on-disk name
194 			 * unless there is a case collision, in which
195 			 * case readdir will have returned a mangled name.
196 			 */
197 			if (smb_maybe_mangled_name(name) == 0)
198 				od = 1;
199 
200 			rc = smb_fsop_remove(sr, sr->user_cr, dir_snode,
201 			    name, od);
202 		}
203 
204 		smb_node_end_crit(node);
205 		smb_node_release(node);
206 		node = NULL;
207 
208 		if (rc != 0) {
209 			if (rc != ENOENT) {
210 				smbsr_errno(sr, rc);
211 				goto delete_error;
212 			}
213 		} else {
214 			deleted++;
215 		}
216 	}
217 
218 	if ((rc != 0) && (rc != ENOENT)) {
219 		smbsr_errno(sr, rc);
220 		goto delete_error;
221 	}
222 
223 	if (deleted == 0) {
224 		if (sr->sid_odir->d_wildcards == 0)
225 			smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND,
226 			    ERRDOS, ERROR_FILE_NOT_FOUND);
227 		else
228 			smbsr_error(sr, NT_STATUS_NO_SUCH_FILE,
229 			    ERRDOS, ERROR_FILE_NOT_FOUND);
230 		goto delete_error;
231 	}
232 
233 	smb_rdir_close(sr);
234 	kmem_free(pc, sizeof (*pc));
235 	kmem_free(name, MAXNAMELEN);
236 	kmem_free(fname, MAXNAMELEN);
237 	kmem_free(sname, MAXNAMELEN);
238 	kmem_free(fullname, MAXPATHLEN);
239 
240 	rc = smbsr_encode_empty_result(sr);
241 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
242 
243 delete_error:
244 	smb_rdir_close(sr);
245 	kmem_free(pc, sizeof (*pc));
246 	kmem_free(name, MAXNAMELEN);
247 	kmem_free(fname, MAXNAMELEN);
248 	kmem_free(sname, MAXNAMELEN);
249 	kmem_free(fullname, MAXPATHLEN);
250 	return (SDRC_ERROR);
251 }
252 
253 /*
254  * For consistency with Windows 2000, the range check should be done
255  * after checking for sharing violations.  Attempting to delete a
256  * locked file will result in sharing violation, which is the same
257  * thing that will happen if you try to delete a non-locked open file.
258  *
259  * Note that windows 2000 rejects lock requests on open files that
260  * have been opened with metadata open modes.  The error is
261  * STATUS_ACCESS_DENIED.
262  */
263 static uint32_t
264 smb_delete_check(smb_request_t *sr, smb_node_t *node)
265 {
266 	uint32_t status;
267 
268 	status = smb_node_delete_check(node);
269 
270 	if (status == NT_STATUS_SHARING_VIOLATION) {
271 		smbsr_error(sr, NT_STATUS_SHARING_VIOLATION,
272 		    ERRDOS, ERROR_SHARING_VIOLATION);
273 		return (status);
274 	}
275 
276 	status = smb_range_check(sr, sr->user_cr, node, 0, UINT64_MAX, B_TRUE);
277 
278 	if (status != NT_STATUS_SUCCESS) {
279 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
280 		    ERRDOS, ERROR_ACCESS_DENIED);
281 	}
282 
283 	return (status);
284 }
285