s3:smbd: ignore NTCREATEX_OPTIONS_SYNC_ALERT and NTCREATEX_OPTIONS_ASYNC_ALERT for...
[ira/wip.git] / source3 / smbd / smb2_create.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24
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,
33                                  const char *in_name,
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);
44
45 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *req)
46 {
47         const uint8_t *inbody;
48         int i = req->current_idx;
49         uint8_t *outhdr;
50         DATA_BLOB outbody;
51         DATA_BLOB outdyn;
52         size_t expected_body_size = 0x39;
53         size_t body_size;
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;
64         char *in_name_string;
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;
76         NTSTATUS status;
77         bool ok;
78
79         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
80                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81         }
82
83         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
84
85         body_size = SVAL(inbody, 0x00);
86         if (body_size != expected_body_size) {
87                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
88         }
89
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);
99
100         if (in_name_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
101                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
102         }
103
104         if (in_name_length > req->in.vector[i+2].iov_len) {
105                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
106         }
107
108         in_name_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
109         in_name_buffer.length = in_name_length;
110
111         ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
112                                    in_name_buffer.data,
113                                    in_name_buffer.length,
114                                    &in_name_string,
115                                    &in_name_string_size, false);
116         if (!ok) {
117                 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
118         }
119
120         status = smbd_smb2_create(req,
121                                   in_oplock_level,
122                                   in_impersonation_level,
123                                   in_desired_access,
124                                   in_file_attributes,
125                                   in_share_access,
126                                   in_create_disposition,
127                                   in_create_options,
128                                   in_name_string,
129                                   &out_oplock_level,
130                                   &out_create_action,
131                                   &out_creation_time,
132                                   &out_last_access_time,
133                                   &out_last_write_time,
134                                   &out_change_time,
135                                   &out_allocation_size,
136                                   &out_end_of_file,
137                                   &out_file_attributes,
138                                   &out_file_id_volatile);
139         if (!NT_STATUS_IS_OK(status)) {
140                 return smbd_smb2_request_error(req, status);
141         }
142
143         outhdr = (uint8_t *)req->out.vector[i].iov_base;
144
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);
148         }
149
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 */
176
177         outdyn = data_blob_const(NULL, 0);
178
179         return smbd_smb2_request_done(req, outbody, &outdyn);
180 }
181
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,
190                                  const char *in_name,
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)
201 {
202         NTSTATUS status;
203         struct smb_request *smbreq;
204         files_struct *result;
205         int info;
206         SMB_STRUCT_STAT sbuf;
207
208         DEBUG(10,("smbd_smb2_create: name[%s]\n",
209                   in_name));
210
211         smbreq = smbd_smb2_fake_smb_request(req);
212         if (smbreq == NULL) {
213                 return NT_STATUS_NO_MEMORY;
214         }
215
216         /* If it's an IPC, pass off the pipe handler. */
217         if (IS_IPC(req->tcon->compat_conn)) {
218                 return NT_STATUS_NOT_IMPLEMENTED;
219         }
220
221         if (CAN_PRINT(req->tcon->compat_conn)) {
222                 return NT_STATUS_NOT_IMPLEMENTED;
223         }
224
225         switch (in_oplock_level) {
226         case SMB2_OPLOCK_LEVEL_BATCH:
227                 break;
228         case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
229                 break;
230         default:
231                 break;
232         }
233
234         /* these are ignored for SMB2 */
235         in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
236         in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
237
238         status = SMB_VFS_CREATE_FILE(req->tcon->compat_conn,
239                                      smbreq,
240                                      0, /* root_dir_fid */
241                                      in_name,
242                                      CFF_DOS_PATH, /* create_file_flags */
243                                      in_desired_access,
244                                      in_share_access,
245                                      in_create_disposition,
246                                      in_create_options,
247                                      in_file_attributes,
248                                      0, /* oplock_request */
249                                      0, /* allocation_size */
250                                      NULL, /* security_descriptor */
251                                      NULL, /* ea_list */
252                                      &result,
253                                      &info,
254                                      &sbuf);
255         if (!NT_STATUS_IS_OK(status)) {
256                 return status;
257         }
258
259         *out_oplock_level       = 0;
260         if ((in_create_disposition == FILE_SUPERSEDE)
261             && (info == FILE_WAS_OVERWRITTEN)) {
262                 *out_create_action = FILE_WAS_SUPERSEDED;
263         } else {
264                 *out_create_action = info;
265         }
266         unix_timespec_to_nt_time(out_creation_time, sbuf.st_ex_btime);
267         unix_timespec_to_nt_time(out_last_access_time, sbuf.st_ex_atime);
268         unix_timespec_to_nt_time(out_last_write_time,sbuf.st_ex_mtime);
269         unix_timespec_to_nt_time(out_change_time, sbuf.st_ex_ctime);
270         *out_allocation_size    = sbuf.st_ex_blksize * sbuf.st_ex_blocks;
271         *out_end_of_file        = sbuf.st_ex_size;
272         *out_file_attributes    = dos_mode(result->conn,result->fsp_name,&sbuf);
273         if (*out_file_attributes == 0) {
274                 *out_file_attributes = FILE_ATTRIBUTE_NORMAL;
275         }
276         *out_file_id_volatile = result->fnum;
277         return NT_STATUS_OK;
278 }