Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[kai/samba.git] / source / smb_server / smb2 / fileio.c
1 /* 
2    Unix SMB2 implementation.
3    
4    Copyright (C) Stefan Metzmacher      2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libcli/smb2/smb2.h"
22 #include "libcli/smb2/smb2_calls.h"
23 #include "smb_server/smb_server.h"
24 #include "smb_server/service_smb_proto.h"
25 #include "smb_server/smb2/smb2_server.h"
26 #include "ntvfs/ntvfs.h"
27 #include "param/param.h"
28
29 static void smb2srv_create_send(struct ntvfs_request *ntvfs)
30 {
31         struct smb2srv_request *req;
32         union smb_open *io;
33
34         SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_open);
35         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x58, true, io->smb2.out.blob.length));
36
37         SCVAL(req->out.body,    0x02,   io->smb2.out.oplock_level);
38         SCVAL(req->out.body,    0x03,   io->smb2.out.reserved);
39         SIVAL(req->out.body,    0x04,   io->smb2.out.create_action);
40         SBVAL(req->out.body,    0x08,   io->smb2.out.create_time);
41         SBVAL(req->out.body,    0x10,   io->smb2.out.access_time);
42         SBVAL(req->out.body,    0x18,   io->smb2.out.write_time);
43         SBVAL(req->out.body,    0x20,   io->smb2.out.change_time);
44         SBVAL(req->out.body,    0x28,   io->smb2.out.alloc_size);
45         SBVAL(req->out.body,    0x30,   io->smb2.out.size);
46         SIVAL(req->out.body,    0x38,   io->smb2.out.file_attr);
47         SIVAL(req->out.body,    0x3C,   io->smb2.out.reserved2);
48         smb2srv_push_handle(req->out.body, 0x40, io->smb2.out.file.ntvfs);
49         SMB2SRV_CHECK(smb2_push_o32s32_blob(&req->out, 0x50, io->smb2.out.blob));
50
51         /* also setup the chained file handle */
52         req->chained_file_handle = req->_chained_file_handle;
53         smb2srv_push_handle(req->chained_file_handle, 0, io->smb2.out.file.ntvfs);
54
55         smb2srv_send_reply(req);
56 }
57
58 void smb2srv_create_recv(struct smb2srv_request *req)
59 {
60         union smb_open *io;
61         DATA_BLOB blob;
62
63         SMB2SRV_CHECK_BODY_SIZE(req, 0x38, true);
64         SMB2SRV_TALLOC_IO_PTR(io, union smb_open);
65         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_create_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
66
67         io->smb2.level                  = RAW_OPEN_SMB2;
68         io->smb2.in.security_flags      = CVAL(req->in.body, 0x02);
69         io->smb2.in.oplock_level        = CVAL(req->in.body, 0x03);
70         io->smb2.in.impersonation_level = IVAL(req->in.body, 0x04);
71         io->smb2.in.create_flags        = BVAL(req->in.body, 0x08);
72         io->smb2.in.reserved            = BVAL(req->in.body, 0x10);
73         io->smb2.in.desired_access      = IVAL(req->in.body, 0x18);
74         io->smb2.in.file_attributes     = IVAL(req->in.body, 0x1C);
75         io->smb2.in.share_access        = IVAL(req->in.body, 0x20);
76         io->smb2.in.create_disposition  = IVAL(req->in.body, 0x24);
77         io->smb2.in.create_options      = IVAL(req->in.body, 0x28);
78         SMB2SRV_CHECK(smb2_pull_o16s16_string(&req->in, io, req->in.body+0x2C, &io->smb2.in.fname));
79         SMB2SRV_CHECK(smb2_pull_o32s32_blob(&req->in, io, req->in.body+0x30, &blob));
80         /* TODO: parse the blob */
81         ZERO_STRUCT(io->smb2.in.eas);
82         ZERO_STRUCT(io->smb2.in.blobs);
83
84         /* the VFS backend does not yet handle NULL filenames */
85         if (io->smb2.in.fname == NULL) {
86                 io->smb2.in.fname = "";
87         }
88
89         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_open(req->ntvfs, io));
90 }
91
92 static void smb2srv_close_send(struct ntvfs_request *ntvfs)
93 {
94         struct smb2srv_request *req;
95         union smb_close *io;
96
97         SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_close);
98         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x3C, false, 0));
99
100         SSVAL(req->out.body,    0x02,   io->smb2.out.flags);
101         SIVAL(req->out.body,    0x04,   io->smb2.out._pad);
102         SBVAL(req->out.body,    0x08,   io->smb2.out.create_time);
103         SBVAL(req->out.body,    0x10,   io->smb2.out.access_time);
104         SBVAL(req->out.body,    0x18,   io->smb2.out.write_time);
105         SBVAL(req->out.body,    0x20,   io->smb2.out.change_time);
106         SBVAL(req->out.body,    0x28,   io->smb2.out.alloc_size);
107         SBVAL(req->out.body,    0x30,   io->smb2.out.size);
108         SIVAL(req->out.body,    0x38,   io->smb2.out.file_attr);
109
110         smb2srv_send_reply(req);
111 }
112
113 void smb2srv_close_recv(struct smb2srv_request *req)
114 {
115         union smb_close *io;
116
117         SMB2SRV_CHECK_BODY_SIZE(req, 0x18, false);
118         SMB2SRV_TALLOC_IO_PTR(io, union smb_close);
119         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_close_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
120
121         io->smb2.level                  = RAW_CLOSE_SMB2;
122         io->smb2.in.flags               = SVAL(req->in.body, 0x02);
123         io->smb2.in._pad                = IVAL(req->in.body, 0x04);
124         io->smb2.in.file.ntvfs          = smb2srv_pull_handle(req, req->in.body, 0x08);
125
126         SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
127         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_close(req->ntvfs, io));
128 }
129
130 static void smb2srv_flush_send(struct ntvfs_request *ntvfs)
131 {
132         struct smb2srv_request *req;
133         union smb_flush *io;
134
135         SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_flush);
136         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x04, false, 0));
137
138         SSVAL(req->out.body,    0x02,   io->smb2.out.reserved);
139
140         smb2srv_send_reply(req);
141 }
142
143 void smb2srv_flush_recv(struct smb2srv_request *req)
144 {
145         union smb_flush *io;
146
147         SMB2SRV_CHECK_BODY_SIZE(req, 0x18, false);
148         SMB2SRV_TALLOC_IO_PTR(io, union smb_flush);
149         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_flush_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
150
151         io->smb2.level                  = RAW_FLUSH_SMB2;
152         io->smb2.in.reserved1           = SVAL(req->in.body, 0x02);
153         io->smb2.in.reserved2           = IVAL(req->in.body, 0x04);
154         io->smb2.in.file.ntvfs          = smb2srv_pull_handle(req, req->in.body, 0x08);
155
156         SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
157         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_flush(req->ntvfs, io));
158 }
159
160 static void smb2srv_read_send(struct ntvfs_request *ntvfs)
161 {
162         struct smb2srv_request *req;
163         union smb_read *io;
164
165         SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_read);
166         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x10, true, io->smb2.out.data.length));
167
168         /* TODO: avoid the memcpy */
169         SMB2SRV_CHECK(smb2_push_o16s32_blob(&req->out, 0x02, io->smb2.out.data));
170         SIVAL(req->out.body,    0x08,   io->smb2.out.remaining);
171         SIVAL(req->out.body,    0x0C,   io->smb2.out.reserved);
172
173         smb2srv_send_reply(req);
174 }
175
176 void smb2srv_read_recv(struct smb2srv_request *req)
177 {
178         union smb_read *io;
179
180         SMB2SRV_CHECK_BODY_SIZE(req, 0x30, true);
181         SMB2SRV_TALLOC_IO_PTR(io, union smb_read);
182         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_read_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
183
184         io->smb2.level                  = RAW_READ_SMB2;
185         io->smb2.in._pad                = SVAL(req->in.body, 0x02);
186         io->smb2.in.length              = IVAL(req->in.body, 0x04);
187         io->smb2.in.offset              = BVAL(req->in.body, 0x08);
188         io->smb2.in.file.ntvfs          = smb2srv_pull_handle(req, req->in.body, 0x10);
189         io->smb2.in.min_count           = IVAL(req->in.body, 0x20);
190         io->smb2.in.channel             = IVAL(req->in.body, 0x24);
191         io->smb2.in.remaining           = IVAL(req->in.body, 0x28);
192         io->smb2.in.channel_offset      = SVAL(req->in.body, 0x2C);
193         io->smb2.in.channel_length      = SVAL(req->in.body, 0x2E);
194
195         SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
196
197         /* preallocate the buffer for the backends */
198         io->smb2.out.data = data_blob_talloc(io, NULL, io->smb2.in.length);
199         if (io->smb2.out.data.length != io->smb2.in.length) {
200                 SMB2SRV_CHECK(NT_STATUS_NO_MEMORY);
201         }
202
203         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_read(req->ntvfs, io));
204 }
205
206 static void smb2srv_write_send(struct ntvfs_request *ntvfs)
207 {
208         struct smb2srv_request *req;
209         union smb_write *io;
210
211         SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_write);
212         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x10, true, 0));
213
214         SSVAL(req->out.body,    0x02,   io->smb2.out._pad);
215         SIVAL(req->out.body,    0x04,   io->smb2.out.nwritten);
216         SBVAL(req->out.body,    0x08,   io->smb2.out.unknown1);
217
218         smb2srv_send_reply(req);
219 }
220
221 void smb2srv_write_recv(struct smb2srv_request *req)
222 {
223         union smb_write *io;
224
225         SMB2SRV_CHECK_BODY_SIZE(req, 0x30, true);
226         SMB2SRV_TALLOC_IO_PTR(io, union smb_write);
227         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_write_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
228
229         /* TODO: avoid the memcpy */
230         io->smb2.level                  = RAW_WRITE_SMB2;
231         SMB2SRV_CHECK(smb2_pull_o16s32_blob(&req->in, io, req->in.body+0x02, &io->smb2.in.data));
232         io->smb2.in.offset              = BVAL(req->in.body, 0x08);
233         io->smb2.in.file.ntvfs          = smb2srv_pull_handle(req, req->in.body, 0x10);
234         io->smb2.in.unknown1            = BVAL(req->in.body, 0x20);
235         io->smb2.in.unknown2            = BVAL(req->in.body, 0x28);
236
237         SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
238         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_write(req->ntvfs, io));
239 }
240
241 static void smb2srv_lock_send(struct ntvfs_request *ntvfs)
242 {
243         struct smb2srv_request *req;
244         union smb_lock *io;
245
246         SMB2SRV_CHECK_ASYNC_STATUS_ERR(io, union smb_lock);
247         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x04, false, 0));
248
249         SSVAL(req->out.body,    0x02,   io->smb2.out.reserved);
250
251         smb2srv_send_reply(req);
252 }
253
254 void smb2srv_lock_recv(struct smb2srv_request *req)
255 {
256         union smb_lock *io;
257         int i;
258
259         SMB2SRV_CHECK_BODY_SIZE(req, 0x30, false);
260         SMB2SRV_TALLOC_IO_PTR(io, union smb_lock);
261         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_lock_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
262
263         io->smb2.level                  = RAW_LOCK_SMB2;
264         io->smb2.in.lock_count          = SVAL(req->in.body, 0x02);
265         io->smb2.in.reserved            = IVAL(req->in.body, 0x04);
266         io->smb2.in.file.ntvfs          = smb2srv_pull_handle(req, req->in.body, 0x08);
267         if (req->in.body_size < 24 + 24*(uint64_t)io->smb2.in.lock_count) {
268                 DEBUG(0,("%s: lock buffer too small\n", __location__));
269                 smb2srv_send_error(req,  NT_STATUS_FOOBAR);
270                 return;
271         }
272         io->smb2.in.locks = talloc_array(io, struct smb2_lock_element, 
273                                          io->smb2.in.lock_count);
274         if (io->smb2.in.locks == NULL) {
275                 smb2srv_send_error(req, NT_STATUS_NO_MEMORY);
276                 return;
277         }
278
279         for (i=0;i<io->smb2.in.lock_count;i++) {
280                 io->smb2.in.locks[i].offset     = BVAL(req->in.body, 24 + i*24);
281                 io->smb2.in.locks[i].length     = BVAL(req->in.body, 32 + i*24);
282                 io->smb2.in.locks[i].flags      = IVAL(req->in.body, 40 + i*24);
283                 io->smb2.in.locks[i].reserved   = IVAL(req->in.body, 44 + i*24);
284         }
285
286         SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
287         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_lock(req->ntvfs, io));
288 }
289
290 static void smb2srv_ioctl_send(struct ntvfs_request *ntvfs)
291 {
292         struct smb2srv_request *req;
293         union smb_ioctl *io;
294
295         SMB2SRV_CHECK_ASYNC_STATUS_ERR(io, union smb_ioctl);
296         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x30, true, 0));
297
298         SSVAL(req->out.body,    0x02,   io->smb2.out._pad);
299         SIVAL(req->out.body,    0x04,   io->smb2.out.function);
300         if (io->smb2.level == RAW_IOCTL_SMB2_NO_HANDLE) {
301                 struct smb2_handle h;
302                 h.data[0] = UINT64_MAX;
303                 h.data[1] = UINT64_MAX;
304                 smb2_push_handle(req->out.body + 0x08, &h);
305         } else {
306                 smb2srv_push_handle(req->out.body, 0x08,io->smb2.in.file.ntvfs);
307         }
308         SMB2SRV_CHECK(smb2_push_o32s32_blob(&req->out, 0x18, io->smb2.out.in));
309         SMB2SRV_CHECK(smb2_push_o32s32_blob(&req->out, 0x20, io->smb2.out.out));
310         SIVAL(req->out.body,    0x28,   io->smb2.out.unknown2);
311         SIVAL(req->out.body,    0x2C,   io->smb2.out.unknown3);
312
313         smb2srv_send_reply(req);
314 }
315
316 void smb2srv_ioctl_recv(struct smb2srv_request *req)
317 {
318         union smb_ioctl *io;
319         struct smb2_handle h;
320
321         SMB2SRV_CHECK_BODY_SIZE(req, 0x38, true);
322         SMB2SRV_TALLOC_IO_PTR(io, union smb_ioctl);
323         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_ioctl_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
324
325         /* TODO: avoid the memcpy */
326         io->smb2.in._pad                = SVAL(req->in.body, 0x02);
327         io->smb2.in.function            = IVAL(req->in.body, 0x04);
328         /* file handle ... */
329         SMB2SRV_CHECK(smb2_pull_o32s32_blob(&req->in, io, req->in.body+0x18, &io->smb2.in.out));
330         io->smb2.in.unknown2            = IVAL(req->in.body, 0x20);
331         SMB2SRV_CHECK(smb2_pull_o32s32_blob(&req->in, io, req->in.body+0x24, &io->smb2.in.in));
332         io->smb2.in.max_response_size   = IVAL(req->in.body, 0x2C);
333         io->smb2.in.flags               = BVAL(req->in.body, 0x30);
334
335         smb2_pull_handle(req->in.body + 0x08, &h);
336         if (h.data[0] == UINT64_MAX && h.data[1] == UINT64_MAX) {
337                 io->smb2.level          = RAW_IOCTL_SMB2_NO_HANDLE;
338         } else {
339                 io->smb2.level          = RAW_IOCTL_SMB2;
340                 io->smb2.in.file.ntvfs  = smb2srv_pull_handle(req, req->in.body, 0x08);
341                 SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
342         }
343
344         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_ioctl(req->ntvfs, io));
345 }
346
347 static void smb2srv_notify_send(struct ntvfs_request *ntvfs)
348 {
349         struct smb2srv_request *req;
350         union smb_notify *io;
351         size_t size = 0;
352         int i;
353         uint8_t *p;
354         DATA_BLOB blob = data_blob(NULL, 0);
355
356         SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_notify);
357         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, true, 0));
358
359 #define MAX_BYTES_PER_CHAR 3
360         
361         /* work out how big the reply buffer could be */
362         for (i=0;i<io->smb2.out.num_changes;i++) {
363                 size += 12 + 3 + (1+strlen(io->smb2.out.changes[i].name.s)) * MAX_BYTES_PER_CHAR;
364         }
365
366         blob = data_blob_talloc(req, NULL, size);
367         if (size > 0 && !blob.data) {
368                 SMB2SRV_CHECK(NT_STATUS_NO_MEMORY);
369         }
370
371         p = blob.data;
372
373         /* construct the changes buffer */
374         for (i=0;i<io->smb2.out.num_changes;i++) {
375                 uint32_t ofs;
376                 ssize_t len;
377
378                 SIVAL(p, 4, io->smb2.out.changes[i].action);
379                 len = push_string(lp_iconv_convenience(ntvfs->ctx->lp_ctx), p + 12, io->smb2.out.changes[i].name.s, 
380                                   blob.length - (p+12 - blob.data), STR_UNICODE);
381                 SIVAL(p, 8, len);
382
383                 ofs = len + 12;
384
385                 if (ofs & 3) {
386                         int pad = 4 - (ofs & 3);
387                         memset(p+ofs, 0, pad);
388                         ofs += pad;
389                 }
390
391                 if (i == io->smb2.out.num_changes-1) {
392                         SIVAL(p, 0, 0);
393                 } else {
394                         SIVAL(p, 0, ofs);
395                 }
396
397                 p += ofs;
398         }
399
400         blob.length = p - blob.data;
401
402         SMB2SRV_CHECK(smb2_push_o16s32_blob(&req->out, 0x02, blob));
403
404         smb2srv_send_reply(req);
405 }
406
407 void smb2srv_notify_recv(struct smb2srv_request *req)
408 {
409         union smb_notify *io;
410
411         SMB2SRV_CHECK_BODY_SIZE(req, 0x20, false);
412         SMB2SRV_TALLOC_IO_PTR(io, union smb_notify);
413         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_notify_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
414
415         io->smb2.level                  = RAW_NOTIFY_SMB2;
416         io->smb2.in.recursive           = SVAL(req->in.body, 0x02);
417         io->smb2.in.buffer_size         = IVAL(req->in.body, 0x04);
418         io->smb2.in.file.ntvfs          = smb2srv_pull_handle(req, req->in.body, 0x08);
419         io->smb2.in.completion_filter   = IVAL(req->in.body, 0x18);
420         io->smb2.in.unknown             = BVAL(req->in.body, 0x1C);
421
422         SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
423         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_notify(req->ntvfs, io));
424 }
425
426 static void smb2srv_break_send(struct ntvfs_request *ntvfs)
427 {
428         struct smb2srv_request *req;
429         union smb_lock *io;
430
431         SMB2SRV_CHECK_ASYNC_STATUS_ERR(io, union smb_lock);
432         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x18, false, 0));
433
434         SCVAL(req->out.body,    0x02,   io->smb2_break.out.oplock_level);
435         SCVAL(req->out.body,    0x03,   io->smb2_break.out.reserved);
436         SIVAL(req->out.body,    0x04,   io->smb2_break.out.reserved2);
437         smb2srv_push_handle(req->out.body, 0x08,io->smb2_break.out.file.ntvfs);
438
439         smb2srv_send_reply(req);
440 }
441
442 void smb2srv_break_recv(struct smb2srv_request *req)
443 {
444         union smb_lock *io;
445
446         SMB2SRV_CHECK_BODY_SIZE(req, 0x18, false);
447         SMB2SRV_TALLOC_IO_PTR(io, union smb_lock);
448         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_break_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
449
450         io->smb2_break.level            = RAW_LOCK_SMB2_BREAK;
451         io->smb2_break.in.oplock_level  = CVAL(req->in.body, 0x02);
452         io->smb2_break.in.reserved      = CVAL(req->in.body, 0x03);
453         io->smb2_break.in.reserved2     = IVAL(req->in.body, 0x04);
454         io->smb2_break.in.file.ntvfs    = smb2srv_pull_handle(req, req->in.body, 0x08);
455
456         SMB2SRV_CHECK_FILE_HANDLE(io->smb2_break.in.file.ntvfs);
457         SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_lock(req->ntvfs, io));
458 }