2 Unix SMB2 implementation.
4 Copyright (C) Andrew Tridgell 2005
5 Copyright (C) Stefan Metzmacher 2005
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 "system/time.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "smb_server/smb_server.h"
26 #include "smb_server/service_smb_proto.h"
27 #include "smb_server/smb2/smb2_server.h"
28 #include "smbd/service_stream.h"
29 #include "lib/stream/packet.h"
30 #include "ntvfs/ntvfs.h"
32 static int smb2srv_request_destructor(struct smb2srv_request *req)
34 DLIST_REMOVE(req->smb_conn->requests2.list, req);
35 if (req->pending_id) {
36 idr_remove(req->smb_conn->requests2.idtree_req, req->pending_id);
41 static int smb2srv_request_deny_destructor(struct smb2srv_request *req)
46 struct smb2srv_request *smb2srv_init_request(struct smbsrv_connection *smb_conn)
48 struct smb2srv_request *req;
50 req = talloc_zero(smb_conn, struct smb2srv_request);
51 if (!req) return NULL;
53 req->smb_conn = smb_conn;
55 talloc_set_destructor(req, smb2srv_request_destructor);
60 NTSTATUS smb2srv_setup_reply(struct smb2srv_request *req, uint16_t body_fixed_size,
61 BOOL body_dynamic_present, uint32_t body_dynamic_size)
63 uint32_t flags = 0x00000001;
64 uint32_t pid = IVAL(req->in.hdr, SMB2_HDR_PID);
65 uint32_t tid = IVAL(req->in.hdr, SMB2_HDR_TID);
67 if (req->pending_id) {
69 pid = req->pending_id;
73 if (body_dynamic_present) {
74 if (body_dynamic_size == 0) {
75 body_dynamic_size = 1;
78 body_dynamic_size = 0;
81 req->out.size = SMB2_HDR_BODY+NBT_HDR_SIZE+body_fixed_size;
83 req->out.allocated = req->out.size + body_dynamic_size;
84 req->out.buffer = talloc_size(req, req->out.allocated);
85 NT_STATUS_HAVE_NO_MEMORY(req->out.buffer);
87 req->out.hdr = req->out.buffer + NBT_HDR_SIZE;
88 req->out.body = req->out.hdr + SMB2_HDR_BODY;
89 req->out.body_fixed = body_fixed_size;
90 req->out.body_size = body_fixed_size;
91 req->out.dynamic = (body_dynamic_size ? req->out.body + body_fixed_size : NULL);
93 SIVAL(req->out.hdr, 0, SMB2_MAGIC);
94 SSVAL(req->out.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
95 SSVAL(req->out.hdr, SMB2_HDR_PAD1, 0);
96 SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(req->status));
97 SSVAL(req->out.hdr, SMB2_HDR_OPCODE, SVAL(req->in.hdr, SMB2_HDR_OPCODE));
98 SSVAL(req->out.hdr, SMB2_HDR_UNKNOWN1, 0x0001);
99 SIVAL(req->out.hdr, SMB2_HDR_FLAGS, flags);
100 SIVAL(req->out.hdr, SMB2_HDR_CHAIN_OFFSET, 0);
101 SBVAL(req->out.hdr, SMB2_HDR_SEQNUM, req->seqnum);
102 SIVAL(req->out.hdr, SMB2_HDR_PID, pid);
103 SIVAL(req->out.hdr, SMB2_HDR_TID, tid);
104 SBVAL(req->out.hdr, SMB2_HDR_UID, BVAL(req->in.hdr, SMB2_HDR_UID));
105 memset(req->out.hdr+SMB2_HDR_SIG, 0, 16);
107 /* set the length of the fixed body part and +1 if there's a dynamic part also */
108 SSVAL(req->out.body, 0, body_fixed_size + (body_dynamic_size?1:0));
111 * if we have a dynamic part, make sure the first byte
112 * which is always be part of the packet is initialized
114 if (body_dynamic_size) {
116 SCVAL(req->out.dynamic, 0, 0);
122 static NTSTATUS smb2srv_reply(struct smb2srv_request *req);
124 static void smb2srv_chain_reply(struct smb2srv_request *p_req)
127 struct smb2srv_request *req;
128 uint32_t chain_offset;
129 uint32_t protocol_version;
130 uint16_t buffer_code;
131 uint32_t dynamic_size;
133 chain_offset = p_req->chain_offset;
134 p_req->chain_offset = 0;
136 if (p_req->in.size < (NBT_HDR_SIZE + chain_offset + SMB2_MIN_SIZE)) {
137 DEBUG(2,("Invalid SMB2 chained packet at offset 0x%X\n",
139 smbsrv_terminate_connection(p_req->smb_conn, "Invalid SMB2 chained packet");
143 protocol_version = IVAL(p_req->in.buffer, NBT_HDR_SIZE + chain_offset);
144 if (protocol_version != SMB2_MAGIC) {
145 DEBUG(2,("Invalid SMB chained packet: protocol prefix: 0x%08X\n",
147 smbsrv_terminate_connection(p_req->smb_conn, "NON-SMB2 chained packet");
151 req = smb2srv_init_request(p_req->smb_conn);
153 smbsrv_terminate_connection(p_req->smb_conn, "SMB2 chained packet - no memory");
157 req->in.buffer = talloc_steal(req, p_req->in.buffer);
158 req->in.size = p_req->in.size;
159 req->request_time = p_req->request_time;
160 req->in.allocated = req->in.size;
162 req->in.hdr = req->in.buffer+ NBT_HDR_SIZE + chain_offset;
163 req->in.body = req->in.hdr + SMB2_HDR_BODY;
164 req->in.body_size = req->in.size - (NBT_HDR_SIZE+ chain_offset + SMB2_HDR_BODY);
165 req->in.dynamic = NULL;
167 buffer_code = SVAL(req->in.body, 0);
168 req->in.body_fixed = (buffer_code & ~1);
169 dynamic_size = req->in.body_size - req->in.body_fixed;
171 if (dynamic_size != 0 && (buffer_code & 1)) {
172 req->in.dynamic = req->in.body + req->in.body_fixed;
173 if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
174 DEBUG(1,("SMB2 chained request invalid dynamic size 0x%x\n",
176 smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
181 if (p_req->chained_file_handle) {
182 memcpy(req->_chained_file_handle,
183 p_req->_chained_file_handle,
184 sizeof(req->_chained_file_handle));
185 req->chained_file_handle = req->_chained_file_handle;
189 * TODO: - make sure the length field is 64
190 * - make sure it's a request
193 status = smb2srv_reply(req);
194 if (!NT_STATUS_IS_OK(status)) {
195 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
201 void smb2srv_send_reply(struct smb2srv_request *req)
206 if (req->smb_conn->connection->event.fde == NULL) {
207 /* the socket has been destroyed - no point trying to send a reply! */
212 if (req->out.size > NBT_HDR_SIZE) {
213 _smb2_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
216 blob = data_blob_const(req->out.buffer, req->out.size);
217 status = packet_send(req->smb_conn->packet, blob);
218 if (!NT_STATUS_IS_OK(status)) {
219 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
221 if (req->chain_offset) {
222 smb2srv_chain_reply(req);
228 void smb2srv_send_error(struct smb2srv_request *req, NTSTATUS error)
232 if (req->smb_conn->connection->event.fde == NULL) {
233 /* the socket has been destroyed - no point trying to send an error! */
238 status = smb2srv_setup_reply(req, 8, True, 0);
239 if (!NT_STATUS_IS_OK(status)) {
240 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
245 SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(error));
247 SSVAL(req->out.body, 0x02, 0);
248 SIVAL(req->out.body, 0x04, 0);
250 smb2srv_send_reply(req);
253 static NTSTATUS smb2srv_reply(struct smb2srv_request *req)
259 opcode = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
260 req->chain_offset = IVAL(req->in.hdr, SMB2_HDR_CHAIN_OFFSET);
261 req->seqnum = BVAL(req->in.hdr, SMB2_HDR_SEQNUM);
262 tid = IVAL(req->in.hdr, SMB2_HDR_TID);
263 uid = BVAL(req->in.hdr, SMB2_HDR_UID);
265 req->session = smbsrv_session_find(req->smb_conn, uid, req->request_time);
266 req->tcon = smbsrv_smb2_tcon_find(req->session, tid, req->request_time);
270 /* TODO: check the seqnum */
273 case SMB2_OP_NEGPROT:
274 smb2srv_negprot_recv(req);
276 case SMB2_OP_SESSSETUP:
277 smb2srv_sesssetup_recv(req);
280 if (!req->session) goto nosession;
281 smb2srv_logoff_recv(req);
284 if (!req->session) goto nosession;
285 smb2srv_tcon_recv(req);
288 if (!req->session) goto nosession;
289 if (!req->tcon) goto notcon;
290 smb2srv_tdis_recv(req);
293 if (!req->session) goto nosession;
294 if (!req->tcon) goto notcon;
295 smb2srv_create_recv(req);
298 if (!req->session) goto nosession;
299 if (!req->tcon) goto notcon;
300 smb2srv_close_recv(req);
303 if (!req->session) goto nosession;
304 if (!req->tcon) goto notcon;
305 smb2srv_flush_recv(req);
308 if (!req->session) goto nosession;
309 if (!req->tcon) goto notcon;
310 smb2srv_read_recv(req);
313 if (!req->session) goto nosession;
314 if (!req->tcon) goto notcon;
315 smb2srv_write_recv(req);
318 if (!req->session) goto nosession;
319 if (!req->tcon) goto notcon;
320 smb2srv_lock_recv(req);
323 if (!req->session) goto nosession;
324 if (!req->tcon) goto notcon;
325 smb2srv_ioctl_recv(req);
328 smb2srv_cancel_recv(req);
330 case SMB2_OP_KEEPALIVE:
331 smb2srv_keepalive_recv(req);
334 if (!req->session) goto nosession;
335 if (!req->tcon) goto notcon;
336 smb2srv_find_recv(req);
339 if (!req->session) goto nosession;
340 if (!req->tcon) goto notcon;
341 smb2srv_notify_recv(req);
343 case SMB2_OP_GETINFO:
344 if (!req->session) goto nosession;
345 if (!req->tcon) goto notcon;
346 smb2srv_getinfo_recv(req);
348 case SMB2_OP_SETINFO:
349 if (!req->session) goto nosession;
350 if (!req->tcon) goto notcon;
351 smb2srv_setinfo_recv(req);
354 if (!req->session) goto nosession;
355 if (!req->tcon) goto notcon;
356 smb2srv_break_recv(req);
360 DEBUG(1,("Invalid SMB2 opcode: 0x%04X\n", opcode));
361 smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 opcode");
365 smb2srv_send_error(req, NT_STATUS_USER_SESSION_DELETED);
368 smb2srv_send_error(req, NT_STATUS_NETWORK_NAME_DELETED);
372 NTSTATUS smbsrv_recv_smb2_request(void *private, DATA_BLOB blob)
374 struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
375 struct smb2srv_request *req;
376 struct timeval cur_time = timeval_current();
377 uint32_t protocol_version;
378 uint16_t buffer_code;
379 uint32_t dynamic_size;
381 smb_conn->statistics.last_request_time = cur_time;
383 /* see if its a special NBT packet */
384 if (CVAL(blob.data,0) != 0) {
385 DEBUG(2,("Special NBT packet on SMB2 connection"));
386 smbsrv_terminate_connection(smb_conn, "Special NBT packet on SMB2 connection");
390 if (blob.length < (NBT_HDR_SIZE + SMB2_MIN_SIZE)) {
391 DEBUG(2,("Invalid SMB2 packet length count %ld\n", (long)blob.length));
392 smbsrv_terminate_connection(smb_conn, "Invalid SMB2 packet");
396 protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
397 if (protocol_version != SMB2_MAGIC) {
398 DEBUG(2,("Invalid SMB packet: protocol prefix: 0x%08X\n",
400 smbsrv_terminate_connection(smb_conn, "NON-SMB2 packet");
404 req = smb2srv_init_request(smb_conn);
405 NT_STATUS_HAVE_NO_MEMORY(req);
407 req->in.buffer = talloc_steal(req, blob.data);
408 req->in.size = blob.length;
409 req->request_time = cur_time;
410 req->in.allocated = req->in.size;
412 req->in.hdr = req->in.buffer+ NBT_HDR_SIZE;
413 req->in.body = req->in.hdr + SMB2_HDR_BODY;
414 req->in.body_size = req->in.size - (SMB2_HDR_BODY+NBT_HDR_SIZE);
415 req->in.dynamic = NULL;
417 buffer_code = SVAL(req->in.body, 0);
418 req->in.body_fixed = (buffer_code & ~1);
419 dynamic_size = req->in.body_size - req->in.body_fixed;
421 if (dynamic_size != 0 && (buffer_code & 1)) {
422 req->in.dynamic = req->in.body + req->in.body_fixed;
423 if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
424 DEBUG(1,("SMB2 request invalid dynamic size 0x%x\n",
426 smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
432 * TODO: - make sure the length field is 64
433 * - make sure it's a request
436 return smb2srv_reply(req);
439 static NTSTATUS smb2srv_init_pending(struct smbsrv_connection *smb_conn)
441 smb_conn->requests2.idtree_req = idr_init(smb_conn);
442 NT_STATUS_HAVE_NO_MEMORY(smb_conn->requests2.idtree_req);
443 smb_conn->requests2.idtree_limit = 0x00FFFFFF & (UINT32_MAX - 1);
444 smb_conn->requests2.list = NULL;
449 NTSTATUS smb2srv_queue_pending(struct smb2srv_request *req)
453 if (req->pending_id) {
454 return NT_STATUS_INTERNAL_ERROR;
457 id = idr_get_new_above(req->smb_conn->requests2.idtree_req, req,
458 1, req->smb_conn->requests2.idtree_limit);
460 return NT_STATUS_INSUFFICIENT_RESOURCES;
463 DLIST_ADD_END(req->smb_conn->requests2.list, req, struct smb2srv_request *);
464 req->pending_id = id;
466 talloc_set_destructor(req, smb2srv_request_deny_destructor);
467 smb2srv_send_error(req, STATUS_PENDING);
468 talloc_set_destructor(req, smb2srv_request_destructor);
473 void smb2srv_cancel_recv(struct smb2srv_request *req)
478 struct smb2srv_request *r;
480 if (!req->session) goto done;
482 flags = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
483 pending_id = IVAL(req->in.hdr, SMB2_HDR_PID);
485 if (!(flags & 0x00000002)) {
486 /* TODO: what to do here? */
490 p = idr_find(req->smb_conn->requests2.idtree_req, pending_id);
493 r = talloc_get_type(p, struct smb2srv_request);
496 if (!r->ntvfs) goto done;
498 ntvfs_cancel(r->ntvfs);
501 /* we never generate a reply for a SMB2 Cancel */
506 * init the SMB2 protocol related stuff
508 NTSTATUS smbsrv_init_smb2_connection(struct smbsrv_connection *smb_conn)
512 /* now initialise a few default values associated with this smb socket */
513 smb_conn->negotiate.max_send = 0xFFFF;
515 /* this is the size that w2k uses, and it appears to be important for
517 smb_conn->negotiate.max_recv = lp_max_xmit();
519 smb_conn->negotiate.zone_offset = get_time_zone(time(NULL));
521 smb_conn->config.security = SEC_USER;
522 smb_conn->config.nt_status_support = True;
524 status = smbsrv_init_sessions(smb_conn, UINT64_MAX);
525 NT_STATUS_NOT_OK_RETURN(status);
527 status = smb2srv_init_pending(smb_conn);
528 NT_STATUS_NOT_OK_RETURN(status);