2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
25 static NTSTATUS smbd_smb2_create(struct smbd_smb2_request *req,
26 uint8_t in_oplock_level,
27 uint32_t in_impersonation_level,
28 uint32_t in_desired_access,
29 uint32_t in_file_attributes,
30 uint32_t in_share_access,
31 uint32_t in_create_disposition,
32 uint32_t in_create_options,
34 uint8_t *out_oplock_level,
35 uint32_t *out_create_action,
36 NTTIME *out_creation_time,
37 NTTIME *out_last_access_time,
38 NTTIME *out_last_write_time,
39 NTTIME *out_change_time,
40 uint64_t *out_allocation_size,
41 uint64_t *out_end_of_file,
42 uint32_t *out_file_attributes,
43 uint64_t *out_file_id_volatile);
45 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *req)
47 const uint8_t *inbody;
48 int i = req->current_idx;
52 size_t expected_body_size = 0x39;
54 uint8_t in_oplock_level;
55 uint32_t in_impersonation_level;
56 uint32_t in_desired_access;
57 uint32_t in_file_attributes;
58 uint32_t in_share_access;
59 uint32_t in_create_disposition;
60 uint32_t in_create_options;
61 uint16_t in_name_offset;
62 uint16_t in_name_length;
63 DATA_BLOB in_name_buffer;
65 size_t in_name_string_size;
66 uint8_t out_oplock_level;
67 uint32_t out_create_action;
68 NTTIME out_creation_time;
69 NTTIME out_last_access_time;
70 NTTIME out_last_write_time;
71 NTTIME out_change_time;
72 uint64_t out_allocation_size;
73 uint64_t out_end_of_file;
74 uint32_t out_file_attributes;
75 uint64_t out_file_id_volatile;
79 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
80 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
85 body_size = SVAL(inbody, 0x00);
86 if (body_size != expected_body_size) {
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90 in_oplock_level = CVAL(inbody, 0x03);
91 in_impersonation_level = IVAL(inbody, 0x04);
92 in_desired_access = IVAL(inbody, 0x18);
93 in_file_attributes = IVAL(inbody, 0x1C);
94 in_share_access = IVAL(inbody, 0x20);
95 in_create_disposition = IVAL(inbody, 0x24);
96 in_create_options = IVAL(inbody, 0x28);
97 in_name_offset = SVAL(inbody, 0x2C);
98 in_name_length = SVAL(inbody, 0x2E);
100 if (in_name_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
101 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
104 if (in_name_length > req->in.vector[i+2].iov_len) {
105 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
108 in_name_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
109 in_name_buffer.length = in_name_length;
111 ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
113 in_name_buffer.length,
115 &in_name_string_size, false);
117 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
120 status = smbd_smb2_create(req,
122 in_impersonation_level,
126 in_create_disposition,
132 &out_last_access_time,
133 &out_last_write_time,
135 &out_allocation_size,
137 &out_file_attributes,
138 &out_file_id_volatile);
139 if (!NT_STATUS_IS_OK(status)) {
140 return smbd_smb2_request_error(req, status);
143 outhdr = (uint8_t *)req->out.vector[i].iov_base;
145 outbody = data_blob_talloc(req->out.vector, NULL, 0x58);
146 if (outbody.data == NULL) {
147 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
150 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
151 SCVAL(outbody.data, 0x02,
152 out_oplock_level); /* oplock level */
153 SCVAL(outbody.data, 0x03, 0); /* reserved */
154 SIVAL(outbody.data, 0x04,
155 out_create_action); /* create action */
156 SBVAL(outbody.data, 0x08,
157 out_creation_time); /* creation time */
158 SBVAL(outbody.data, 0x10,
159 out_last_access_time); /* last access time */
160 SBVAL(outbody.data, 0x18,
161 out_last_write_time); /* last write time */
162 SBVAL(outbody.data, 0x20,
163 out_change_time); /* change time */
164 SBVAL(outbody.data, 0x28,
165 out_allocation_size); /* allocation size */
166 SBVAL(outbody.data, 0x30,
167 out_end_of_file); /* end of file */
168 SIVAL(outbody.data, 0x38,
169 out_file_attributes); /* file attributes */
170 SIVAL(outbody.data, 0x3C, 0); /* reserved */
171 SBVAL(outbody.data, 0x40, 0); /* file id (persistent) */
172 SBVAL(outbody.data, 0x48,
173 out_file_id_volatile); /* file id (volatile) */
174 SIVAL(outbody.data, 0x50, 0); /* create contexts offset */
175 SIVAL(outbody.data, 0x54, 0); /* create contexts length */
177 outdyn = data_blob_const(NULL, 0);
179 return smbd_smb2_request_done(req, outbody, &outdyn);
182 static NTSTATUS smbd_smb2_create(struct smbd_smb2_request *req,
183 uint8_t in_oplock_level,
184 uint32_t in_impersonation_level,
185 uint32_t in_desired_access,
186 uint32_t in_file_attributes,
187 uint32_t in_share_access,
188 uint32_t in_create_disposition,
189 uint32_t in_create_options,
191 uint8_t *out_oplock_level,
192 uint32_t *out_create_action,
193 NTTIME *out_creation_time,
194 NTTIME *out_last_access_time,
195 NTTIME *out_last_write_time,
196 NTTIME *out_change_time,
197 uint64_t *out_allocation_size,
198 uint64_t *out_end_of_file,
199 uint32_t *out_file_attributes,
200 uint64_t *out_file_id_volatile)
203 struct smb_request *smbreq;
204 files_struct *result;
206 SMB_STRUCT_STAT sbuf;
208 DEBUG(10,("smbd_smb2_create: name[%s]\n",
211 smbreq = smbd_smb2_fake_smb_request(req);
212 if (smbreq == NULL) {
213 return NT_STATUS_NO_MEMORY;
216 if (IS_IPC(smbreq->conn)) {
217 return NT_STATUS_NOT_IMPLEMENTED;
218 } else if (CAN_PRINT(smbreq->conn)) {
219 status = file_new(smbreq, smbreq->conn, &result);
220 if(!NT_STATUS_IS_OK(status)) {
224 status = print_fsp_open(smbreq,
230 if (!NT_STATUS_IS_OK(status)) {
231 file_free(smbreq, result);
234 info = FILE_WAS_CREATED;
236 /* these are ignored for SMB2 */
237 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
238 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
240 status = SMB_VFS_CREATE_FILE(req->tcon->compat_conn,
242 0, /* root_dir_fid */
244 CFF_DOS_PATH, /* create_file_flags */
247 in_create_disposition,
250 0, /* oplock_request */
251 0, /* allocation_size */
252 NULL, /* security_descriptor */
257 if (!NT_STATUS_IS_OK(status)) {
262 *out_oplock_level = 0;
263 if ((in_create_disposition == FILE_SUPERSEDE)
264 && (info == FILE_WAS_OVERWRITTEN)) {
265 *out_create_action = FILE_WAS_SUPERSEDED;
267 *out_create_action = info;
269 unix_timespec_to_nt_time(out_creation_time, sbuf.st_ex_btime);
270 unix_timespec_to_nt_time(out_last_access_time, sbuf.st_ex_atime);
271 unix_timespec_to_nt_time(out_last_write_time,sbuf.st_ex_mtime);
272 unix_timespec_to_nt_time(out_change_time, sbuf.st_ex_ctime);
273 *out_allocation_size = sbuf.st_ex_blksize * sbuf.st_ex_blocks;
274 *out_end_of_file = sbuf.st_ex_size;
275 *out_file_attributes = dos_mode(result->conn,result->fsp_name,&sbuf);
276 if (*out_file_attributes == 0) {
277 *out_file_attributes = FILE_ATTRIBUTE_NORMAL;
279 *out_file_id_volatile = result->fnum;