2 Unix SMB2 implementation.
4 Copyright (C) Stefan Metzmacher 2005
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.
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.
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/>.
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"
29 static void smb2srv_create_send(struct ntvfs_request *ntvfs)
31 struct smb2srv_request *req;
34 SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_open);
35 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x58, true, io->smb2.out.blob.length));
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));
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);
55 smb2srv_send_reply(req);
58 void smb2srv_create_recv(struct smb2srv_request *req)
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);
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);
84 /* the VFS backend does not yet handle NULL filenames */
85 if (io->smb2.in.fname == NULL) {
86 io->smb2.in.fname = "";
89 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_open(req->ntvfs, io));
92 static void smb2srv_close_send(struct ntvfs_request *ntvfs)
94 struct smb2srv_request *req;
97 SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_close);
98 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x3C, false, 0));
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);
110 smb2srv_send_reply(req);
113 void smb2srv_close_recv(struct smb2srv_request *req)
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);
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);
126 SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
127 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_close(req->ntvfs, io));
130 static void smb2srv_flush_send(struct ntvfs_request *ntvfs)
132 struct smb2srv_request *req;
135 SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_flush);
136 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x04, false, 0));
138 SSVAL(req->out.body, 0x02, io->smb2.out.reserved);
140 smb2srv_send_reply(req);
143 void smb2srv_flush_recv(struct smb2srv_request *req)
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);
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);
156 SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
157 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_flush(req->ntvfs, io));
160 static void smb2srv_read_send(struct ntvfs_request *ntvfs)
162 struct smb2srv_request *req;
165 SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_read);
166 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x10, true, io->smb2.out.data.length));
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);
173 smb2srv_send_reply(req);
176 void smb2srv_read_recv(struct smb2srv_request *req)
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);
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);
195 SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
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);
203 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_read(req->ntvfs, io));
206 static void smb2srv_write_send(struct ntvfs_request *ntvfs)
208 struct smb2srv_request *req;
211 SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_write);
212 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x10, true, 0));
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);
218 smb2srv_send_reply(req);
221 void smb2srv_write_recv(struct smb2srv_request *req)
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);
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);
237 SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
238 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_write(req->ntvfs, io));
241 static void smb2srv_lock_send(struct ntvfs_request *ntvfs)
243 struct smb2srv_request *req;
246 SMB2SRV_CHECK_ASYNC_STATUS_ERR(io, union smb_lock);
247 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x04, false, 0));
249 SSVAL(req->out.body, 0x02, io->smb2.out.reserved);
251 smb2srv_send_reply(req);
254 void smb2srv_lock_recv(struct smb2srv_request *req)
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);
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);
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);
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);
286 SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
287 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_lock(req->ntvfs, io));
290 static void smb2srv_ioctl_send(struct ntvfs_request *ntvfs)
292 struct smb2srv_request *req;
295 SMB2SRV_CHECK_ASYNC_STATUS_ERR(io, union smb_ioctl);
296 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x30, true, 0));
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);
306 smb2srv_push_handle(req->out.body, 0x08,io->smb2.in.file.ntvfs);
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);
313 smb2srv_send_reply(req);
316 void smb2srv_ioctl_recv(struct smb2srv_request *req)
319 struct smb2_handle h;
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);
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);
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;
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);
344 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_ioctl(req->ntvfs, io));
347 static void smb2srv_notify_send(struct ntvfs_request *ntvfs)
349 struct smb2srv_request *req;
350 union smb_notify *io;
354 DATA_BLOB blob = data_blob(NULL, 0);
356 SMB2SRV_CHECK_ASYNC_STATUS(io, union smb_notify);
357 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, true, 0));
359 #define MAX_BYTES_PER_CHAR 3
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;
366 blob = data_blob_talloc(req, NULL, size);
367 if (size > 0 && !blob.data) {
368 SMB2SRV_CHECK(NT_STATUS_NO_MEMORY);
373 /* construct the changes buffer */
374 for (i=0;i<io->smb2.out.num_changes;i++) {
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);
386 int pad = 4 - (ofs & 3);
387 memset(p+ofs, 0, pad);
391 if (i == io->smb2.out.num_changes-1) {
400 blob.length = p - blob.data;
402 SMB2SRV_CHECK(smb2_push_o16s32_blob(&req->out, 0x02, blob));
404 smb2srv_send_reply(req);
407 void smb2srv_notify_recv(struct smb2srv_request *req)
409 union smb_notify *io;
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);
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);
422 SMB2SRV_CHECK_FILE_HANDLE(io->smb2.in.file.ntvfs);
423 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_notify(req->ntvfs, io));
426 static void smb2srv_break_send(struct ntvfs_request *ntvfs)
428 struct smb2srv_request *req;
431 SMB2SRV_CHECK_ASYNC_STATUS_ERR(io, union smb_lock);
432 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x18, false, 0));
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);
439 smb2srv_send_reply(req);
442 void smb2srv_break_recv(struct smb2srv_request *req)
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);
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);
456 SMB2SRV_CHECK_FILE_HANDLE(io->smb2_break.in.file.ntvfs);
457 SMB2SRV_CALL_NTVFS_BACKEND(ntvfs_lock(req->ntvfs, io));