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, when EAs
30  * or an SD must be applied to the file. The functionality is similar
31  * to SmbNtCreateAndx with the option to supply extended attributes or
32  * a security descriptor.
33  *
34  * Note: we don't decode the extended attributes because we don't
35  * support them at this time.
36  */
37 
38 #include <smbsrv/smbvar.h>
39 #include <smbsrv/smb_kproto.h>
40 #include <smbsrv/smb_fsops.h>
41 #include <smbsrv/ntstatus.h>
42 #include <smbsrv/ntaccess.h>
43 #include <smbsrv/nterror.h>
44 #include <smbsrv/ntifs.h>
45 #include <smbsrv/cifs.h>
46 #include <smbsrv/doserror.h>
47 
48 /*
49  * smb_nt_transact_create
50  *
51  * This command is used to create or open a file or directory, when EAs
52  * or an SD must be applied to the file. The request parameter block
53  * encoding, data block encoding and output parameter block encoding are
54  * described in CIFS section 4.2.2.
55  *
56  * The format of the command is SmbNtTransact but it is basically the same
57  * as SmbNtCreateAndx with the option to supply extended attributes or a
58  * security descriptor. For information not defined in CIFS section 4.2.2
59  * see section 4.2.1 (NT_CREATE_ANDX).
60  */
61 smb_sdrc_t
62 smb_nt_transact_create(struct smb_request *sr, struct smb_xa *xa)
63 {
64 	struct open_param *op = &sr->arg.open;
65 	uint8_t			OplockLevel;
66 	uint8_t			DirFlag;
67 	uint8_t			SecurityFlags;
68 	uint32_t		ExtFileAttributes;
69 	uint32_t		sd_len;
70 	uint32_t		EaLength;
71 	uint32_t		Flags;
72 	uint32_t		ImpersonationLevel;
73 	uint32_t		RootDirFid;
74 	uint32_t		NameLength;
75 	smb_attr_t		new_attr;
76 	smb_node_t		*node;
77 	smb_sd_t		sd;
78 	DWORD status;
79 	int rc;
80 
81 	rc = smb_decode_mbc(&xa->req_param_mb, "%lllqllllllllb",
82 	    sr,
83 	    &Flags,
84 	    &RootDirFid,
85 	    &op->desired_access,
86 	    &op->dsize,
87 	    &ExtFileAttributes,
88 	    &op->share_access,
89 	    &op->create_disposition,
90 	    &op->create_options,
91 	    &sd_len,
92 	    &EaLength,
93 	    &NameLength,
94 	    &ImpersonationLevel,
95 	    &SecurityFlags);
96 
97 	if (rc != 0)
98 		return (SDRC_ERROR_REPLY);
99 
100 	/*
101 	 * If name length is zero, interpret as "\".
102 	 */
103 	if (NameLength == 0) {
104 		op->fqi.path = "\\";
105 	} else {
106 		rc = smb_decode_mbc(&xa->req_param_mb, "%#u",
107 		    sr, NameLength, &op->fqi.path);
108 		if (rc != 0)
109 			return (SDRC_ERROR_REPLY);
110 	}
111 
112 	if ((op->create_options & FILE_DELETE_ON_CLOSE) &&
113 	    !(op->desired_access & DELETE)) {
114 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
115 		return (SDRC_ERROR_REPLY);
116 	}
117 
118 	if (sd_len) {
119 		status = smb_decode_sd(xa, &sd);
120 		if (status != NT_STATUS_SUCCESS) {
121 			smbsr_error(sr, status, 0, 0);
122 			return (SDRC_ERROR_REPLY);
123 		}
124 		op->sd = &sd;
125 	} else {
126 		op->sd = NULL;
127 	}
128 
129 	op->fqi.srch_attr = 0;
130 	op->omode = 0;
131 
132 	op->utime.tv_sec = op->utime.tv_nsec = 0;
133 	op->my_flags = 0;
134 
135 	op->dattr = ExtFileAttributes;
136 
137 	if (Flags) {
138 		if (Flags & NT_CREATE_FLAG_REQUEST_OPLOCK) {
139 			if (Flags & NT_CREATE_FLAG_REQUEST_OPBATCH) {
140 				op->my_flags = MYF_BATCH_OPLOCK;
141 			} else {
142 				op->my_flags = MYF_EXCLUSIVE_OPLOCK;
143 			}
144 		}
145 		if (Flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)
146 			op->my_flags |= MYF_MUST_BE_DIRECTORY;
147 	}
148 
149 	if (ExtFileAttributes & FILE_FLAG_WRITE_THROUGH)
150 		op->create_options |= FILE_WRITE_THROUGH;
151 
152 	if (ExtFileAttributes & FILE_FLAG_DELETE_ON_CLOSE)
153 		op->create_options |= FILE_DELETE_ON_CLOSE;
154 
155 	if (RootDirFid == 0) {
156 		op->fqi.dir_snode = sr->tid_tree->t_snode;
157 	} else {
158 		sr->smb_fid = (ushort_t)RootDirFid;
159 		sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree,
160 		    sr->smb_fid);
161 		/*
162 		 * XXX: ASSERT() for now but we should understand if the test
163 		 *	of the return value is missing because it cannot happen.
164 		 */
165 		ASSERT(sr->fid_ofile != NULL);
166 		op->fqi.dir_snode = sr->fid_ofile->f_node;
167 		smbsr_disconnect_file(sr);
168 	}
169 
170 	status = smb_common_open(sr);
171 
172 	if (op->sd)
173 		smb_sd_term(op->sd);
174 
175 	if (status != NT_STATUS_SUCCESS)
176 		return (SDRC_ERROR_REPLY);
177 
178 	if (STYPE_ISDSK(sr->tid_tree->t_res_type)) {
179 		switch (MYF_OPLOCK_TYPE(op->my_flags)) {
180 		case MYF_EXCLUSIVE_OPLOCK :
181 			OplockLevel = 1;
182 			break;
183 		case MYF_BATCH_OPLOCK :
184 			OplockLevel = 2;
185 			break;
186 		case MYF_LEVEL_II_OPLOCK :
187 			OplockLevel = 3;
188 			break;
189 		case MYF_OPLOCK_NONE :
190 		default:
191 			OplockLevel = 0;
192 			break;
193 		}
194 
195 		if (op->create_options & FILE_DELETE_ON_CLOSE)
196 			smb_preset_delete_on_close(sr->fid_ofile);
197 
198 		/*
199 		 * Set up the directory flag and ensure that
200 		 * we don't return a stale file size.
201 		 */
202 		node = sr->fid_ofile->f_node;
203 		if (node->attr.sa_vattr.va_type == VDIR) {
204 			DirFlag = 1;
205 			new_attr.sa_vattr.va_size = 0;
206 		} else {
207 			DirFlag = 0;
208 			new_attr.sa_mask = SMB_AT_SIZE;
209 			(void) smb_fsop_getattr(sr, kcred, node, &new_attr);
210 			node->attr.sa_vattr.va_size = new_attr.sa_vattr.va_size;
211 		}
212 
213 		(void) smb_encode_mbc(&xa->rep_param_mb, "b.wllTTTTlqqwwb",
214 		    OplockLevel,
215 		    sr->smb_fid,
216 		    op->action_taken,
217 		    0,	/* EaErrorOffset */
218 		    &node->attr.sa_crtime,
219 		    &node->attr.sa_vattr.va_atime,
220 		    &node->attr.sa_vattr.va_mtime,
221 		    &node->attr.sa_vattr.va_ctime,
222 		    op->dattr & FILE_ATTRIBUTE_MASK,
223 		    new_attr.sa_vattr.va_size,
224 		    new_attr.sa_vattr.va_size,
225 		    op->ftype,
226 		    op->devstate,
227 		    DirFlag);
228 	} else {
229 		/* Named PIPE */
230 		(void) smb_encode_mbc(&xa->rep_param_mb, "b.wllTTTTlqqwwb",
231 		    0,
232 		    sr->smb_fid,
233 		    op->action_taken,
234 		    0,	/* EaErrorOffset */
235 		    0LL,
236 		    0LL,
237 		    0LL,
238 		    0LL,
239 		    op->dattr,
240 		    0x1000LL,
241 		    0LL,
242 		    op->ftype,
243 		    op->devstate,
244 		    0);
245 	}
246 
247 	return (SDRC_NORMAL_REPLY);
248 }
249