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