5d379ec3a5b566d9db353fe7a4d3a3712bf6b962
[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 (IS_IPC(smbreq->conn)) {
217                 const char *pipe_name = in_name;
218
219                 if (!lp_nt_pipe_support()) {
220                         return NT_STATUS_ACCESS_DENIED;
221                 }
222
223                 /* Strip \\ off the name. */
224                 if (pipe_name[0] == '\\') {
225                         pipe_name++;
226                 }
227
228                 status = open_np_file(smbreq, pipe_name, &result);
229                 if (!NT_STATUS_IS_OK(status)) {
230                         return status;
231                 }
232                 info = 0;
233                 ZERO_STRUCT(sbuf);
234         } else if (CAN_PRINT(smbreq->conn)) {
235                 status = file_new(smbreq, smbreq->conn, &result);
236                 if(!NT_STATUS_IS_OK(status)) {
237                         return status;
238                 }
239
240                 status = print_fsp_open(smbreq,
241                                         smbreq->conn,
242                                         in_name,
243                                         smbreq->vuid,
244                                         result,
245                                         &sbuf);
246                 if (!NT_STATUS_IS_OK(status)) {
247                         file_free(smbreq, result);
248                         return status;
249                 }
250                 info = FILE_WAS_CREATED;
251         } else {
252                 /* these are ignored for SMB2 */
253                 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
254                 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
255
256                 status = SMB_VFS_CREATE_FILE(req->tcon->compat_conn,
257                                              smbreq,
258                                              0, /* root_dir_fid */
259                                              in_name,
260                                              CFF_DOS_PATH, /* create_file_flags */
261                                              in_desired_access,
262                                              in_share_access,
263                                              in_create_disposition,
264                                              in_create_options,
265                                              in_file_attributes,
266                                              0, /* oplock_request */
267                                              0, /* allocation_size */
268                                              NULL, /* security_descriptor */
269                                              NULL, /* ea_list */
270                                              &result,
271                                              &info,
272                                              &sbuf);
273                 if (!NT_STATUS_IS_OK(status)) {
274                         return status;
275                 }
276         }
277
278         *out_oplock_level       = 0;
279         if ((in_create_disposition == FILE_SUPERSEDE)
280             && (info == FILE_WAS_OVERWRITTEN)) {
281                 *out_create_action = FILE_WAS_SUPERSEDED;
282         } else {
283                 *out_create_action = info;
284         }
285         unix_timespec_to_nt_time(out_creation_time, sbuf.st_ex_btime);
286         unix_timespec_to_nt_time(out_last_access_time, sbuf.st_ex_atime);
287         unix_timespec_to_nt_time(out_last_write_time,sbuf.st_ex_mtime);
288         unix_timespec_to_nt_time(out_change_time, sbuf.st_ex_ctime);
289         *out_allocation_size    = sbuf.st_ex_blksize * sbuf.st_ex_blocks;
290         *out_end_of_file        = sbuf.st_ex_size;
291         *out_file_attributes    = dos_mode(result->conn,result->fsp_name,&sbuf);
292         if (*out_file_attributes == 0) {
293                 *out_file_attributes = FILE_ATTRIBUTE_NORMAL;
294         }
295         *out_file_id_volatile = result->fnum;
296         return NT_STATUS_OK;
297 }