xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_nt_transact_create.c (revision ca9327a6de44d69ddab3668cc1e143ce781387a3)
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 = kmem_alloc(sizeof (smb_sd_t), KM_SLEEP);
124 		*op->sd = sd;
125 	} else {
126 		op->sd = NULL;
127 	}
128 
129 	DTRACE_SMB_2(op__NtTransactCreate__start, smb_request_t *, sr,
130 	    struct open_param *, op);
131 
132 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
133 }
134 
135 void
136 smb_post_nt_transact_create(smb_request_t *sr, smb_xa_t *xa)
137 {
138 	smb_sd_t *sd = sr->arg.open.sd;
139 
140 	DTRACE_SMB_2(op__NtTransactCreate__done, smb_request_t *, sr,
141 	    smb_xa_t *, xa);
142 
143 	if (sd) {
144 		smb_sd_term(sd);
145 		kmem_free(sd, sizeof (smb_sd_t));
146 	}
147 }
148 
149 smb_sdrc_t
150 smb_nt_transact_create(smb_request_t *sr, smb_xa_t *xa)
151 {
152 	struct open_param *op = &sr->arg.open;
153 	uint8_t			OplockLevel;
154 	uint8_t			DirFlag;
155 	smb_attr_t		new_attr;
156 	smb_node_t		*node;
157 	uint32_t status;
158 
159 	if ((op->create_options & FILE_DELETE_ON_CLOSE) &&
160 	    !(op->desired_access & DELETE)) {
161 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
162 		return (SDRC_ERROR);
163 	}
164 
165 	if (op->dattr & FILE_FLAG_WRITE_THROUGH)
166 		op->create_options |= FILE_WRITE_THROUGH;
167 
168 	if (op->dattr & FILE_FLAG_DELETE_ON_CLOSE)
169 		op->create_options |= FILE_DELETE_ON_CLOSE;
170 
171 	if (op->rootdirfid == 0) {
172 		op->fqi.dir_snode = sr->tid_tree->t_snode;
173 	} else {
174 		sr->smb_fid = (ushort_t)op->rootdirfid;
175 		sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree,
176 		    sr->smb_fid);
177 		if (sr->fid_ofile == NULL) {
178 			smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
179 			    ERRDOS, ERRbadfid);
180 			return (SDRC_ERROR);
181 		}
182 
183 		op->fqi.dir_snode = sr->fid_ofile->f_node;
184 		smbsr_disconnect_file(sr);
185 	}
186 
187 	status = smb_common_open(sr);
188 
189 	if (status != NT_STATUS_SUCCESS)
190 		return (SDRC_ERROR);
191 
192 	if (STYPE_ISDSK(sr->tid_tree->t_res_type)) {
193 		switch (MYF_OPLOCK_TYPE(op->my_flags)) {
194 		case MYF_EXCLUSIVE_OPLOCK :
195 			OplockLevel = 1;
196 			break;
197 		case MYF_BATCH_OPLOCK :
198 			OplockLevel = 2;
199 			break;
200 		case MYF_LEVEL_II_OPLOCK :
201 			OplockLevel = 3;
202 			break;
203 		case MYF_OPLOCK_NONE :
204 		default:
205 			OplockLevel = 0;
206 			break;
207 		}
208 
209 		if (op->create_options & FILE_DELETE_ON_CLOSE)
210 			smb_preset_delete_on_close(sr->fid_ofile);
211 
212 		/*
213 		 * Set up the directory flag and ensure that
214 		 * we don't return a stale file size.
215 		 */
216 		node = sr->fid_ofile->f_node;
217 		if (node->attr.sa_vattr.va_type == VDIR) {
218 			DirFlag = 1;
219 			new_attr.sa_vattr.va_size = 0;
220 		} else {
221 			DirFlag = 0;
222 			new_attr.sa_mask = SMB_AT_SIZE;
223 			(void) smb_fsop_getattr(sr, kcred, node, &new_attr);
224 			node->attr.sa_vattr.va_size = new_attr.sa_vattr.va_size;
225 		}
226 
227 		(void) smb_encode_mbc(&xa->rep_param_mb, "b.wllTTTTlqqwwb",
228 		    OplockLevel,
229 		    sr->smb_fid,
230 		    op->action_taken,
231 		    0,	/* EaErrorOffset */
232 		    &node->attr.sa_crtime,
233 		    &node->attr.sa_vattr.va_atime,
234 		    &node->attr.sa_vattr.va_mtime,
235 		    &node->attr.sa_vattr.va_ctime,
236 		    op->dattr & FILE_ATTRIBUTE_MASK,
237 		    new_attr.sa_vattr.va_size,
238 		    new_attr.sa_vattr.va_size,
239 		    op->ftype,
240 		    op->devstate,
241 		    DirFlag);
242 	} else {
243 		/* Named PIPE */
244 		bzero(&new_attr, sizeof (smb_attr_t));
245 		(void) smb_encode_mbc(&xa->rep_param_mb, "b.wllTTTTlqqwwb",
246 		    0,
247 		    sr->smb_fid,
248 		    op->action_taken,
249 		    0,	/* EaErrorOffset */
250 		    &new_attr.sa_crtime,
251 		    &new_attr.sa_vattr.va_atime,
252 		    &new_attr.sa_vattr.va_mtime,
253 		    &new_attr.sa_vattr.va_ctime,
254 		    op->dattr,
255 		    0x1000LL,
256 		    0LL,
257 		    op->ftype,
258 		    op->devstate,
259 		    0);
260 	}
261 
262 	return (SDRC_SUCCESS);
263 }
264