xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_nt_transact_create.c (revision bea83d026ee1bd1b2a2419e1d0232f107a5d7d9b)
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 int
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 		smbsr_decode_error(sr);
99 		/* NOTREACHED */
100 	}
101 
102 	/*
103 	 * If name length is zero, interpret as "\".
104 	 */
105 	if (NameLength == 0) {
106 		op->fqi.path = "\\";
107 	} else {
108 		rc = smb_decode_mbc(&xa->req_param_mb, "%#u",
109 		    sr, NameLength, &op->fqi.path);
110 		if (rc != 0) {
111 			smbsr_decode_error(sr);
112 			/* NOTREACHED */
113 		}
114 	}
115 
116 	if ((op->create_options & FILE_DELETE_ON_CLOSE) &&
117 	    !(op->desired_access & DELETE)) {
118 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
119 		/* NOTREACHED */
120 	}
121 
122 	if (sd_len) {
123 		status = smb_decode_sd(xa, &sd);
124 		if (status != NT_STATUS_SUCCESS) {
125 			smbsr_error(sr, status, 0, 0);
126 			/* NOTREACHED */
127 		}
128 		op->sd = &sd;
129 	} else {
130 		op->sd = NULL;
131 	}
132 
133 	op->fqi.srch_attr = 0;
134 	op->omode = 0;
135 
136 	op->utime.tv_sec = op->utime.tv_nsec = 0;
137 	op->my_flags = 0;
138 
139 	op->dattr = ExtFileAttributes;
140 
141 	if (Flags) {
142 		if (Flags & NT_CREATE_FLAG_REQUEST_OPLOCK) {
143 			if (Flags & NT_CREATE_FLAG_REQUEST_OPBATCH) {
144 				op->my_flags = MYF_BATCH_OPLOCK;
145 			} else {
146 				op->my_flags = MYF_EXCLUSIVE_OPLOCK;
147 			}
148 		}
149 		if (Flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)
150 			op->my_flags |= MYF_MUST_BE_DIRECTORY;
151 	}
152 
153 	if (ExtFileAttributes & FILE_FLAG_WRITE_THROUGH)
154 		op->create_options |= FILE_WRITE_THROUGH;
155 
156 	if (ExtFileAttributes & FILE_FLAG_DELETE_ON_CLOSE)
157 		op->create_options |= FILE_DELETE_ON_CLOSE;
158 
159 	if (RootDirFid == 0) {
160 		op->fqi.dir_snode = sr->tid_tree->t_snode;
161 	} else {
162 		sr->smb_fid = (ushort_t)RootDirFid;
163 		sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree,
164 		    sr->smb_fid);
165 		/*
166 		 * XXX: ASSERT() for now but we should understand if the test
167 		 *	of the return value is missing because it cannot happen.
168 		 */
169 		ASSERT(sr->fid_ofile != NULL);
170 		op->fqi.dir_snode = sr->fid_ofile->f_node;
171 		smbsr_disconnect_file(sr);
172 	}
173 
174 	status = smb_open_subr(sr);
175 	if (op->sd)
176 		smb_sd_term(op->sd);
177 
178 	if (status != NT_STATUS_SUCCESS) {
179 		if (status == NT_STATUS_SHARING_VIOLATION)
180 			smbsr_error(sr, NT_STATUS_SHARING_VIOLATION,
181 			    ERRDOS, ERROR_SHARING_VIOLATION);
182 		else
183 			smbsr_error(sr, status, 0, 0);
184 
185 		/* NOTREACHED */
186 	}
187 
188 	if (STYPE_ISDSK(sr->tid_tree->t_res_type)) {
189 		switch (MYF_OPLOCK_TYPE(op->my_flags)) {
190 		case MYF_EXCLUSIVE_OPLOCK :
191 			OplockLevel = 1;
192 			break;
193 		case MYF_BATCH_OPLOCK :
194 			OplockLevel = 2;
195 			break;
196 		case MYF_LEVEL_II_OPLOCK :
197 			OplockLevel = 3;
198 			break;
199 		case MYF_OPLOCK_NONE :
200 		default:
201 			OplockLevel = 0;
202 			break;
203 		}
204 
205 		if (op->create_options & FILE_DELETE_ON_CLOSE)
206 			smb_preset_delete_on_close(sr->fid_ofile);
207 
208 		/*
209 		 * Set up the directory flag and ensure that
210 		 * we don't return a stale file size.
211 		 */
212 		node = sr->fid_ofile->f_node;
213 		if (node->attr.sa_vattr.va_type == VDIR) {
214 			DirFlag = 1;
215 			new_attr.sa_vattr.va_size = 0;
216 		} else {
217 			DirFlag = 0;
218 			new_attr.sa_mask = SMB_AT_SIZE;
219 			(void) smb_fsop_getattr(sr, kcred, node, &new_attr);
220 			node->attr.sa_vattr.va_size = new_attr.sa_vattr.va_size;
221 		}
222 
223 		(void) smb_encode_mbc(&xa->rep_param_mb, "b.wllTTTTlqqwwb",
224 		    OplockLevel,
225 		    sr->smb_fid,
226 		    op->action_taken,
227 		    0,	/* EaErrorOffset */
228 		    &node->attr.sa_crtime,
229 		    &node->attr.sa_vattr.va_atime,
230 		    &node->attr.sa_vattr.va_mtime,
231 		    &node->attr.sa_vattr.va_ctime,
232 		    op->dattr & FILE_ATTRIBUTE_MASK,
233 		    new_attr.sa_vattr.va_size,
234 		    new_attr.sa_vattr.va_size,
235 		    op->ftype,
236 		    op->devstate,
237 		    DirFlag);
238 	} else {
239 		/* Named PIPE */
240 		(void) smb_encode_mbc(&xa->rep_param_mb, "b.wllTTTTlqqwwb",
241 		    0,
242 		    sr->smb_fid,
243 		    op->action_taken,
244 		    0,	/* EaErrorOffset */
245 		    0LL,
246 		    0LL,
247 		    0LL,
248 		    0LL,
249 		    op->dattr,
250 		    0x1000LL,
251 		    0LL,
252 		    op->ftype,
253 		    op->devstate,
254 		    0);
255 	}
256 
257 	return (SDRC_NORMAL_REPLY);
258 }
259