xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_nt_create_andx.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 2007 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 /*
29  * This command is used to create or open a file or directory.
30  */
31 
32 
33 #include <smbsrv/smb_incl.h>
34 #include <smbsrv/smb_fsops.h>
35 #include <smbsrv/smb_vops.h>
36 
37 /*
38  * smb_com_nt_create_andx
39  *
40  * This command is used to create or open a file or directory.
41  *
42  *  Client Request                     Description
43  *  =================================  ==================================
44  *
45  *  UCHAR WordCount;                   Count of parameter words = 24
46  *  UCHAR AndXCommand;                 Secondary command;  0xFF = None
47  *  UCHAR AndXReserved;                Reserved (must be 0)
48  *  USHORT AndXOffset;                 Offset to next command WordCount
49  *  UCHAR Reserved;                    Reserved (must be 0)
50  *  USHORT NameLength;                 Length of Name[] in bytes
51  *  ULONG Flags;                       Create bit set:
52  *                                     0x02 - Request an oplock
53  *                                     0x04 - Request a batch oplock
54  *                                     0x08 - Target of open must be
55  *                                     directory
56  *  ULONG RootDirectoryFid;            If non-zero, open is relative to
57  *                                     this directory
58  *  ACCESS_MASK DesiredAccess;         access desired
59  *  LARGE_INTEGER AllocationSize;      Initial allocation size
60  *  ULONG ExtFileAttributes;           File attributes
61  *  ULONG ShareAccess;                 Type of share access
62  *  ULONG CreateDisposition;           Action to take if file exists or
63  *                                     not
64  *  ULONG CreateOptions;               Options to use if creating a file
65  *  ULONG ImpersonationLevel;          Security QOS information
66  *  UCHAR SecurityFlags;               Security tracking mode flags:
67  *                                     0x1 - SECURITY_CONTEXT_TRACKING
68  *                                     0x2 - SECURITY_EFFECTIVE_ONLY
69  *  USHORT ByteCount;                  Length of byte parameters
70  *  STRING Name[];                     File to open or create
71  *
72  * The DesiredAccess parameter is specified in section 3.7 on  Access Mask
73  * Encoding.
74  *
75  * If no value is specified, it still allows an application to query
76  * attributes without actually accessing the file.
77  *
78  * The ExtFIleAttributes parameter specifies the file attributes and flags
79  * for the file. The parameter's value is the sum of allowed attributes and
80  * flags defined in section 3.11 on  Extended File Attribute Encoding
81  *
82  * The ShareAccess field Specifies how this file can be shared. This
83  * parameter must be some combination of the following values:
84  *
85  * Name              Value      Meaning
86  *                   0          Prevents the file from being shared.
87  * FILE_SHARE_READ   0x00000001 Other open operations can be performed on
88  *                               the file for read access.
89  * FILE_SHARE_WRITE  0x00000002 Other open operations can be performed on
90  *                               the file for write access.
91  * FILE_SHARE_DELETE 0x00000004 Other open operations can be performed on
92  *                               the file for delete access.
93  *
94  * The CreateDisposition parameter can contain one of the following values:
95  *
96  * CREATE_NEW        Creates a new file. The function fails if the
97  *                   specified file already exists.
98  * CREATE_ALWAYS     Creates a new file. The function overwrites the file
99  *                   if it exists.
100  * OPEN_EXISTING     Opens the file. The function fails if the file does
101  *                   not exist.
102  * OPEN_ALWAYS       Opens the file, if it exists. If the file does not
103  *                   exist, act like CREATE_NEW.
104  * TRUNCATE_EXISTING Opens the file. Once opened, the file is truncated so
105  *                   that its size is zero bytes. The calling process must
106  *                   open the file with at least GENERIC_WRITE access. The
107  *                   function fails if the file does not exist.
108  *
109  * The ImpersonationLevel parameter can contain one or more of the
110  * following values:
111  *
112  * SECURITY_ANONYMOUS        Specifies to impersonate the client at the
113  *                           Anonymous impersonation level.
114  * SECURITY_IDENTIFICATION   Specifies to impersonate the client at the
115  *                           Identification impersonation level.
116  * SECURITY_IMPERSONATION    Specifies to impersonate the client at the
117  *                           Impersonation impersonation level.
118  * SECURITY_DELEGATION       Specifies to impersonate the client at the
119  *                           Delegation impersonation level.
120  *
121  * The SecurityFlags parameter can have either of the following two flags
122  * set:
123  *
124  * SECURITY_CONTEXT_TRACKING  Specifies that the security tracking mode is
125  *                            dynamic. If this flag is not specified,
126  *                            Security Tracking Mode is static.
127  * SECURITY_EFFECTIVE_ONLY    Specifies that only the enabled aspects of
128  *                            the client's security context are available
129  *                            to the server. If you do not specify this
130  *                            flag, all aspects of the client's security
131  *                            context are available. This flag allows the
132  *                            client to limit the groups and privileges
133  *                            that a server can use while impersonating the
134  *                            client.
135  *
136  * The response is as follows:
137  *
138  *  Server Response                    Description
139  *  =================================  ==================================
140  *
141  *  UCHAR WordCount;                   Count of parameter words = 26
142  *  UCHAR AndXCommand;  Secondary      0xFF = None
143  *  command;
144  *  UCHAR AndXReserved;                MBZ
145  *  USHORT AndXOffset;                 Offset to next command WordCount
146  *  UCHAR OplockLevel;                 The oplock level granted
147  *                                     0 - No oplock granted
148  *                                     1 - Exclusive oplock granted
149  *                                     2 - Batch oplock granted
150  *                                     3 - Level II oplock granted
151  *  USHORT Fid;                        The file ID
152  *  ULONG CreateAction;                The action taken
153  *  TIME CreationTime;                 The time the file was created
154  *  TIME LastAccessTime;               The time the file was accessed
155  *  TIME LastWriteTime;                The time the file was last written
156  *  TIME ChangeTime;                   The time the file was last changed
157  *  ULONG ExtFileAttributes;           The file attributes
158  *  LARGE_INTEGER AllocationSize;      The number of bytes allocated
159  *  LARGE_INTEGER EndOfFile;           The end of file offset
160  *  USHORT FileType;
161  *  USHORT DeviceState;                state of IPC device (e.g. pipe)
162  *  BOOLEAN Directory;                 TRUE if this is a directory
163  *  USHORT ByteCount;                  = 0
164  *
165  * The following SMBs may follow SMB_COM_NT_CREATE_ANDX:
166  *
167  *    SMB_COM_READ    SMB_COM_READ_ANDX
168  *    SMB_COM_IOCTL
169  */
170 int
171 smb_com_nt_create_andx(struct smb_request *sr)
172 {
173 	struct open_param	*op = &sr->arg.open;
174 	unsigned char		OplockLevel;
175 	unsigned char		DirFlag;
176 	unsigned char		SecurityFlags;
177 	uint32_t		ExtFileAttributes;
178 	uint32_t		Flags;
179 	uint32_t		ImpersonationLevel;
180 	uint32_t		RootDirFid;
181 	unsigned short		NameLength;
182 	smb_attr_t		new_attr;
183 	smb_node_t		*node;
184 	DWORD status;
185 	int count;
186 	int rc;
187 
188 	op->dsize = 0;
189 
190 	rc = smbsr_decode_vwv(sr, "5.wlllqlllllb",
191 	    &NameLength,
192 	    &Flags,
193 	    &RootDirFid,
194 	    &op->desired_access,
195 	    &op->dsize,
196 	    &ExtFileAttributes,
197 	    &op->share_access,
198 	    &op->create_disposition,
199 	    &op->create_options,
200 	    &ImpersonationLevel,
201 	    &SecurityFlags);
202 
203 	if (rc != 0) {
204 		smbsr_decode_error(sr);
205 		/* NOTREACHED */
206 	}
207 
208 	if (NameLength >= MAXPATHLEN) {
209 		smbsr_raise_nt_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND);
210 		/* NOTREACHED */
211 	}
212 
213 	if (smbsr_decode_data(sr, "%#u", sr, NameLength, &op->fqi.path) != 0) {
214 		smbsr_decode_error(sr);
215 		/* NOTREACHED */
216 	}
217 
218 	if ((op->create_options & FILE_DELETE_ON_CLOSE) &&
219 	    !(op->desired_access & DELETE)) {
220 		smbsr_raise_nt_error(sr, NT_STATUS_INVALID_PARAMETER);
221 		/* NOTREACHED */
222 	}
223 
224 	op->fqi.srch_attr = 0;
225 	op->omode = 0;
226 	op->utime.tv_sec = op->utime.tv_nsec = 0;
227 	op->my_flags = 0;
228 	op->dattr = ExtFileAttributes;
229 
230 	if (Flags) {
231 		if (Flags & NT_CREATE_FLAG_REQUEST_OPLOCK) {
232 			if (Flags & NT_CREATE_FLAG_REQUEST_OPBATCH) {
233 				op->my_flags = MYF_BATCH_OPLOCK;
234 			} else {
235 				op->my_flags = MYF_EXCLUSIVE_OPLOCK;
236 			}
237 		}
238 		if (Flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)
239 			op->my_flags |= MYF_MUST_BE_DIRECTORY;
240 	}
241 
242 	if (ExtFileAttributes & FILE_FLAG_WRITE_THROUGH)
243 		op->create_options |= FILE_WRITE_THROUGH;
244 
245 	if (ExtFileAttributes & FILE_FLAG_DELETE_ON_CLOSE)
246 		op->create_options |= FILE_DELETE_ON_CLOSE;
247 
248 	if (RootDirFid == 0) {
249 		op->fqi.dir_snode = sr->tid_tree->t_snode;
250 	} else {
251 		sr->smb_fid = (ushort_t)RootDirFid;
252 
253 		sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree,
254 		    sr->smb_fid);
255 		if (sr->fid_ofile == NULL) {
256 			smbsr_raise_cifs_error(sr, NT_STATUS_INVALID_HANDLE,
257 			    ERRDOS, ERRbadfid);
258 			/* NOTREACHED */
259 		}
260 
261 		op->fqi.dir_snode = sr->fid_ofile->f_node;
262 		smbsr_disconnect_file(sr);
263 	}
264 
265 	status = NT_STATUS_SUCCESS;
266 	/*
267 	 * According to NT, when exclusive share access failed,
268 	 * instead of raising "access deny" error immediately,
269 	 * we should wait for the client holding the exclusive
270 	 * file to close the file. If the wait timed out, we
271 	 * report a sharing violation; otherwise, we grant access.
272 	 * smb_open_subr returns NT_STATUS_SHARING_VIOLATION when
273 	 * it encounters an exclusive share access deny: we wait
274 	 * and retry.
275 	 */
276 	for (count = 0; count <= 4; count++) {
277 		if (count) {
278 			delay(MSEC_TO_TICK(400));
279 		}
280 
281 		if ((status = smb_open_subr(sr)) == NT_STATUS_SUCCESS)
282 			break;
283 	}
284 
285 	if (status != NT_STATUS_SUCCESS) {
286 		if (status == NT_STATUS_SHARING_VIOLATION)
287 			smbsr_raise_cifs_error(sr,
288 			    NT_STATUS_SHARING_VIOLATION,
289 			    ERRDOS, ERROR_SHARING_VIOLATION);
290 		else
291 			smbsr_raise_nt_error(sr, status);
292 
293 		/* NOTREACHED */
294 	}
295 
296 	if (STYPE_ISDSK(sr->tid_tree->t_res_type)) {
297 		switch (MYF_OPLOCK_TYPE(op->my_flags)) {
298 		case MYF_EXCLUSIVE_OPLOCK :
299 			OplockLevel = 1;
300 			break;
301 		case MYF_BATCH_OPLOCK :
302 			OplockLevel = 2;
303 			break;
304 		case MYF_LEVEL_II_OPLOCK :
305 			OplockLevel = 3;
306 			break;
307 		case MYF_OPLOCK_NONE :
308 		default:
309 			OplockLevel = 0;
310 			break;
311 		}
312 
313 		if (op->create_options & FILE_DELETE_ON_CLOSE)
314 			smb_preset_delete_on_close(sr->fid_ofile);
315 
316 		/*
317 		 * Set up the directory flag and ensure that
318 		 * we don't return a stale file size.
319 		 */
320 		node = sr->fid_ofile->f_node;
321 		if (node->attr.sa_vattr.va_type == VDIR) {
322 			DirFlag = 1;
323 			new_attr.sa_vattr.va_size = 0;
324 		} else {
325 			DirFlag = 0;
326 			new_attr.sa_mask = SMB_AT_SIZE;
327 			(void) smb_fsop_getattr(sr, kcred, node, &new_attr);
328 			node->attr.sa_vattr.va_size = new_attr.sa_vattr.va_size;
329 		}
330 
331 		smbsr_encode_result(sr, 34, 0, "bb.wbwlTTTTlqqwwbw",
332 		    34,
333 		    sr->andx_com,
334 		    0x67,
335 		    OplockLevel,
336 		    sr->smb_fid,
337 		    op->action_taken,
338 		    &node->attr.sa_crtime,
339 		    &node->attr.sa_vattr.va_atime,
340 		    &node->attr.sa_vattr.va_mtime,
341 		    &node->attr.sa_vattr.va_ctime,
342 		    op->dattr & FILE_ATTRIBUTE_MASK,
343 		    new_attr.sa_vattr.va_size,
344 		    new_attr.sa_vattr.va_size,
345 		    op->ftype,
346 		    op->devstate,
347 		    DirFlag,
348 		    0);
349 	} else {
350 		/* Named PIPE */
351 		OplockLevel = 0;
352 		smbsr_encode_result(sr, 34, 0, "bb.wbwlqqqqlqqwwbw",
353 		    34,
354 		    sr->andx_com,
355 		    0x67,
356 		    OplockLevel,
357 		    sr->smb_fid,
358 		    op->action_taken,
359 		    0LL,
360 		    0LL,
361 		    0LL,
362 		    0LL,
363 		    SMB_FA_NORMAL,
364 		    0x1000LL,
365 		    0LL,
366 		    op->ftype,
367 		    op->devstate,
368 		    0,
369 		    0);
370 	}
371 
372 	return (SDRC_NORMAL_REPLY);
373 }
374