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 /*
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 smb_sdrc_t
171 smb_pre_nt_create_andx(smb_request_t *sr)
172 {
173 	struct open_param *op = &sr->arg.open;
174 	uint8_t SecurityFlags;
175 	uint32_t Flags;
176 	uint32_t ImpersonationLevel;
177 	uint16_t NameLength;
178 	int rc;
179 
180 	bzero(op, sizeof (sr->arg.open));
181 
182 	rc = smbsr_decode_vwv(sr, "5.wlllqlllllb",
183 	    &NameLength,
184 	    &Flags,
185 	    &op->rootdirfid,
186 	    &op->desired_access,
187 	    &op->dsize,
188 	    &op->dattr,
189 	    &op->share_access,
190 	    &op->create_disposition,
191 	    &op->create_options,
192 	    &ImpersonationLevel,
193 	    &SecurityFlags);
194 
195 	if (rc == 0) {
196 		if (NameLength == 0) {
197 			op->fqi.path = "\\";
198 		} else if (NameLength >= MAXPATHLEN) {
199 			smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND,
200 			    ERRDOS, ERROR_PATH_NOT_FOUND);
201 			rc = -1;
202 		} else {
203 			rc = smbsr_decode_data(sr, "%#u", sr, NameLength,
204 			    &op->fqi.path);
205 		}
206 	}
207 
208 	if (Flags) {
209 		if (Flags & NT_CREATE_FLAG_REQUEST_OPLOCK) {
210 			if (Flags & NT_CREATE_FLAG_REQUEST_OPBATCH) {
211 				op->my_flags = MYF_BATCH_OPLOCK;
212 			} else {
213 				op->my_flags = MYF_EXCLUSIVE_OPLOCK;
214 			}
215 		}
216 
217 		if (Flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)
218 			op->my_flags |= MYF_MUST_BE_DIRECTORY;
219 	}
220 
221 	DTRACE_SMB_2(op__NtCreateX__start, smb_request_t *, sr,
222 	    struct open_param *, op);
223 
224 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
225 }
226 
227 void
228 smb_post_nt_create_andx(smb_request_t *sr)
229 {
230 	DTRACE_SMB_1(op__NtCreateX__done, smb_request_t *, sr);
231 }
232 
233 smb_sdrc_t
234 smb_com_nt_create_andx(struct smb_request *sr)
235 {
236 	struct open_param	*op = &sr->arg.open;
237 	unsigned char		OplockLevel;
238 	unsigned char		DirFlag;
239 	smb_attr_t		new_attr;
240 	smb_node_t		*node;
241 	int rc;
242 
243 	if ((op->create_options & FILE_DELETE_ON_CLOSE) &&
244 	    !(op->desired_access & DELETE)) {
245 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
246 		return (SDRC_ERROR);
247 	}
248 
249 	if (op->dattr & FILE_FLAG_WRITE_THROUGH)
250 		op->create_options |= FILE_WRITE_THROUGH;
251 
252 	if (op->dattr & FILE_FLAG_DELETE_ON_CLOSE)
253 		op->create_options |= FILE_DELETE_ON_CLOSE;
254 
255 	if (op->rootdirfid == 0) {
256 		op->fqi.dir_snode = sr->tid_tree->t_snode;
257 	} else {
258 		sr->smb_fid = (ushort_t)op->rootdirfid;
259 		sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree,
260 		    sr->smb_fid);
261 		if (sr->fid_ofile == NULL) {
262 			smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
263 			    ERRDOS, ERRbadfid);
264 			return (SDRC_ERROR);
265 		}
266 
267 		op->fqi.dir_snode = sr->fid_ofile->f_node;
268 		smbsr_disconnect_file(sr);
269 	}
270 
271 	if (smb_common_open(sr) != NT_STATUS_SUCCESS)
272 		return (SDRC_ERROR);
273 
274 	if (STYPE_ISDSK(sr->tid_tree->t_res_type)) {
275 		switch (MYF_OPLOCK_TYPE(op->my_flags)) {
276 		case MYF_EXCLUSIVE_OPLOCK :
277 			OplockLevel = 1;
278 			break;
279 		case MYF_BATCH_OPLOCK :
280 			OplockLevel = 2;
281 			break;
282 		case MYF_LEVEL_II_OPLOCK :
283 			OplockLevel = 3;
284 			break;
285 		case MYF_OPLOCK_NONE :
286 		default:
287 			OplockLevel = 0;
288 			break;
289 		}
290 
291 		if (op->create_options & FILE_DELETE_ON_CLOSE)
292 			smb_preset_delete_on_close(sr->fid_ofile);
293 
294 		/*
295 		 * Set up the directory flag and ensure that
296 		 * we don't return a stale file size.
297 		 */
298 		node = sr->fid_ofile->f_node;
299 		if (node->attr.sa_vattr.va_type == VDIR) {
300 			DirFlag = 1;
301 			new_attr.sa_vattr.va_size = 0;
302 		} else {
303 			DirFlag = 0;
304 			new_attr.sa_mask = SMB_AT_SIZE;
305 			(void) smb_fsop_getattr(sr, kcred, node, &new_attr);
306 			node->attr.sa_vattr.va_size = new_attr.sa_vattr.va_size;
307 		}
308 
309 		rc = smbsr_encode_result(sr, 34, 0, "bb.wbwlTTTTlqqwwbw",
310 		    34,
311 		    sr->andx_com,
312 		    0x67,
313 		    OplockLevel,
314 		    sr->smb_fid,
315 		    op->action_taken,
316 		    &node->attr.sa_crtime,
317 		    &node->attr.sa_vattr.va_atime,
318 		    &node->attr.sa_vattr.va_mtime,
319 		    &node->attr.sa_vattr.va_ctime,
320 		    op->dattr & FILE_ATTRIBUTE_MASK,
321 		    new_attr.sa_vattr.va_size,
322 		    new_attr.sa_vattr.va_size,
323 		    op->ftype,
324 		    op->devstate,
325 		    DirFlag,
326 		    0);
327 	} else {
328 		/* Named PIPE */
329 		OplockLevel = 0;
330 		rc = smbsr_encode_result(sr, 34, 0, "bb.wbwlqqqqlqqwwbw",
331 		    34,
332 		    sr->andx_com,
333 		    0x67,
334 		    OplockLevel,
335 		    sr->smb_fid,
336 		    op->action_taken,
337 		    0LL,
338 		    0LL,
339 		    0LL,
340 		    0LL,
341 		    FILE_ATTRIBUTE_NORMAL,
342 		    0x1000LL,
343 		    0LL,
344 		    op->ftype,
345 		    op->devstate,
346 		    0,
347 		    0);
348 	}
349 
350 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
351 }
352