r12725: some minor updates
[kai/samba.git] / source4 / smb_server / smb2 / receive.c
1 /* 
2    Unix SMB2 implementation.
3    
4    Copyright (C) Andrew Tridgell        2005
5    Copyright (C) Stefan Metzmacher      2005
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "system/time.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "smb_server/smb_server.h"
27 #include "smb_server/smb2/smb2_server.h"
28 #include "lib/stream/packet.h"
29
30
31 static struct smb2srv_request *smb2srv_init_request(struct smbsrv_connection *smb_conn)
32 {
33         struct smb2srv_request *req;
34
35         req = talloc_zero(smb_conn, struct smb2srv_request);
36         if (!req) return NULL;
37
38         req->smb_conn = smb_conn;
39
40         return req;
41 }
42
43 NTSTATUS smb2srv_setup_reply(struct smb2srv_request *req, uint_t body_fixed_size, uint_t body_dynamic_size)
44 {
45         req->out.size           = SMB2_HDR_BODY+NBT_HDR_SIZE+body_fixed_size;
46
47         req->out.allocated      = req->out.size + body_dynamic_size;
48         req->out.buffer         = talloc_size(req, req->out.allocated);
49         NT_STATUS_HAVE_NO_MEMORY(req->out.buffer);
50
51         req->out.hdr            = req->out.buffer       + NBT_HDR_SIZE;
52         req->out.body           = req->out.hdr          + SMB2_HDR_BODY;
53         req->out.body_size      = body_fixed_size;
54         req->out.dynamic        = (body_dynamic_size ? req->out.body + body_fixed_size : NULL);
55
56         SIVAL(req->out.hdr, 0,                SMB2_MAGIC);
57         SSVAL(req->out.hdr, SMB2_HDR_LENGTH,  SMB2_HDR_BODY);
58         SSVAL(req->out.hdr, SMB2_HDR_PAD1,    0);
59         SIVAL(req->out.hdr, SMB2_HDR_STATUS,  NT_STATUS_V(req->status));
60         SSVAL(req->out.hdr, SMB2_HDR_OPCODE,  SVAL(req->in.hdr, SMB2_HDR_OPCODE));
61         SSVAL(req->out.hdr, SMB2_HDR_PAD2,    0);
62         SIVAL(req->out.hdr, SMB2_HDR_FLAGS,   0x00000001);
63         SIVAL(req->out.hdr, SMB2_HDR_UNKNOWN, 0);
64         SBVAL(req->out.hdr, SMB2_HDR_SEQNUM,  req->seqnum);
65         SIVAL(req->out.hdr, SMB2_HDR_PID,     IVAL(req->in.hdr, SMB2_HDR_PID));
66         SIVAL(req->out.hdr, SMB2_HDR_TID,     IVAL(req->in.hdr, SMB2_HDR_TID));
67         SBVAL(req->out.hdr, SMB2_HDR_UID,     BVAL(req->in.hdr, SMB2_HDR_UID));
68         memset(req->out.hdr+SMB2_HDR_SIG, 0, 16);
69
70         /* set the length of the fixed body part and +1 if there's a dynamic part also */
71         SSVAL(req->out.body, 0, body_fixed_size + (body_dynamic_size?1:0));
72
73         /* 
74          * if we have a dynamic part, make sure the first byte
75          * which is always be part of the packet is initialized
76          */
77         if (body_dynamic_size) {
78                 SCVAL(req->out.dynamic, 0, 0);
79         }
80
81         return NT_STATUS_OK;
82 }
83
84 void smb2srv_send_reply(struct smb2srv_request *req)
85 {
86         DATA_BLOB blob;
87         NTSTATUS status;
88
89         if (req->out.size > NBT_HDR_SIZE) {
90                 _smb2_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
91         }
92
93         blob = data_blob_const(req->out.buffer, req->out.size);
94         status = packet_send(req->smb_conn->packet, blob);
95         if (!NT_STATUS_IS_OK(status)) {
96                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
97         }
98         talloc_free(req);
99 }
100
101 void smb2srv_send_error(struct smb2srv_request *req, NTSTATUS error)
102 {
103         NTSTATUS status;
104
105         status = smb2srv_setup_reply(req, 8, 1);
106         if (!NT_STATUS_IS_OK(status)) {
107                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
108                 talloc_free(req);
109                 return;
110         }
111
112         SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(error));
113
114         SSVAL(req->out.body, 0x02, 0);
115         SIVAL(req->out.body, 0x04, 0);
116
117         smb2srv_send_reply(req);
118 }
119
120 static NTSTATUS smb2srv_reply(struct smb2srv_request *req)
121 {
122         uint16_t opcode;
123         uint32_t tid;
124         uint64_t uid;
125
126         opcode          = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
127         req->seqnum     = BVAL(req->in.hdr, SMB2_HDR_SEQNUM);
128         tid             = IVAL(req->in.hdr, SMB2_HDR_TID);
129         uid             = BVAL(req->in.hdr, SMB2_HDR_UID);
130
131         req->session    = smbsrv_session_find(req->smb_conn, uid);
132         req->tcon       = smbsrv_smb2_tcon_find(req->session, tid);
133
134         errno = 0;
135
136         /* TODO: check the seqnum */
137
138         switch (opcode) {
139         case SMB2_OP_NEGPROT:
140                 smb2srv_negprot_recv(req);
141                 return NT_STATUS_OK;
142         case SMB2_OP_SESSSETUP:
143                 smb2srv_sesssetup_recv(req);
144                 return NT_STATUS_OK;
145         case SMB2_OP_LOGOFF:
146                 if (!req->session) goto nosession;
147                 smb2srv_logoff_recv(req);
148                 return NT_STATUS_OK;
149         case SMB2_OP_TCON:
150                 if (!req->session) goto nosession;
151                 smb2srv_tcon_recv(req);
152                 return NT_STATUS_OK;
153         case SMB2_OP_TDIS:
154                 if (!req->session) goto nosession;
155                 if (!req->tcon) goto notcon;
156                 smb2srv_tdis_recv(req);
157                 return NT_STATUS_OK;
158         case SMB2_OP_CREATE:
159                 if (!req->session) goto nosession;
160                 if (!req->tcon) goto notcon;
161                 smb2srv_create_recv(req);
162                 return NT_STATUS_OK;
163         case SMB2_OP_CLOSE:
164                 if (!req->session) goto nosession;
165                 if (!req->tcon) goto notcon;
166                 smb2srv_close_recv(req);
167                 return NT_STATUS_OK;
168         case SMB2_OP_FLUSH:
169                 if (!req->session) goto nosession;
170                 if (!req->tcon) goto notcon;
171                 smb2srv_flush_recv(req);
172                 return NT_STATUS_OK;
173         case SMB2_OP_READ:
174                 if (!req->session) goto nosession;
175                 if (!req->tcon) goto notcon;
176                 smb2srv_read_recv(req);
177                 return NT_STATUS_OK;
178         case SMB2_OP_WRITE:
179                 if (!req->session) goto nosession;
180                 if (!req->tcon) goto notcon;
181                 smb2srv_write_recv(req);
182                 return NT_STATUS_OK;
183         case SMB2_OP_LOCK:
184                 if (!req->session) goto nosession;
185                 if (!req->tcon) goto notcon;
186                 smb2srv_lock_recv(req);
187                 return NT_STATUS_OK;
188         case SMB2_OP_IOCTL:
189                 if (!req->session) goto nosession;
190                 if (!req->tcon) goto notcon;
191                 smb2srv_ioctl_recv(req);
192                 return NT_STATUS_OK;
193         case SMB2_OP_CANCEL:
194                 if (!req->session) goto nosession;
195                 if (!req->tcon) goto notcon;
196                 smb2srv_cancel_recv(req);
197                 return NT_STATUS_OK;
198         case SMB2_OP_KEEPALIVE:
199                 smb2srv_keepalive_recv(req);
200                 return NT_STATUS_OK;
201         case SMB2_OP_FIND:
202                 if (!req->session) goto nosession;
203                 if (!req->tcon) goto notcon;
204                 smb2srv_find_recv(req);
205                 return NT_STATUS_OK;
206         case SMB2_OP_NOTIFY:
207                 if (!req->session) goto nosession;
208                 if (!req->tcon) goto notcon;
209                 smb2srv_notify_recv(req);
210                 return NT_STATUS_OK;
211         case SMB2_OP_GETINFO:
212                 if (!req->session) goto nosession;
213                 if (!req->tcon) goto notcon;
214                 smb2srv_getinfo_recv(req);
215                 return NT_STATUS_OK;
216         case SMB2_OP_SETINFO:
217                 if (!req->session) goto nosession;
218                 if (!req->tcon) goto notcon;
219                 smb2srv_setinfo_recv(req);
220                 return NT_STATUS_OK;
221         case SMB2_OP_BREAK:
222                 if (!req->session) goto nosession;
223                 if (!req->tcon) goto notcon;
224                 smb2srv_break_recv(req);
225                 return NT_STATUS_OK;
226         }
227
228         DEBUG(1,("Invalid SMB2 opcode: 0x%04X\n", opcode));
229         smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 opcode");
230         return NT_STATUS_OK;
231
232 nosession:
233         smb2srv_send_error(req, NT_STATUS_USER_SESSION_DELETED);
234         return NT_STATUS_OK;
235 notcon:
236         smb2srv_send_error(req, NT_STATUS_NETWORK_NAME_DELETED);
237         return NT_STATUS_OK;
238 }
239
240 NTSTATUS smbsrv_recv_smb2_request(void *private, DATA_BLOB blob)
241 {
242         struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
243         struct smb2srv_request *req;
244         uint32_t protocol_version;
245         uint16_t buffer_code;
246         uint32_t dynamic_size;
247
248         /* see if its a special NBT packet */
249         if (CVAL(blob.data,0) != 0) {
250                 DEBUG(2,("Special NBT packet on SMB2 connection"));
251                 smbsrv_terminate_connection(smb_conn, "Special NBT packet on SMB2 connection");
252                 return NT_STATUS_OK;
253         }
254
255         if (blob.length < (NBT_HDR_SIZE + SMB2_MIN_SIZE)) {
256                 DEBUG(2,("Invalid SMB2 packet length count %ld\n", (long)blob.length));
257                 smbsrv_terminate_connection(smb_conn, "Invalid SMB2 packet");
258                 return NT_STATUS_OK;
259         }
260
261         protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
262
263         if (protocol_version != SMB2_MAGIC) {
264                 DEBUG(2,("Invalid SMB packet: protocl prefix: 0x%08X\n", protocol_version));
265                 smbsrv_terminate_connection(smb_conn, "NON-SMB2 packet");
266                 return NT_STATUS_OK;
267         }
268
269         req = smb2srv_init_request(smb_conn);
270         NT_STATUS_HAVE_NO_MEMORY(req);
271
272         req->in.buffer          = talloc_steal(req, blob.data);
273         req->in.size            = blob.length;
274         req->request_time       = timeval_current();
275         req->in.allocated       = req->in.size;
276
277         req->in.hdr             = req->in.buffer+ NBT_HDR_SIZE;
278         req->in.body            = req->in.hdr   + SMB2_HDR_BODY;
279         req->in.body_size       = req->in.size  - (SMB2_HDR_BODY+NBT_HDR_SIZE);
280         req->in.dynamic         = NULL;
281
282         buffer_code             = SVAL(req->in.body, 0);
283         dynamic_size            = req->in.body_size - (buffer_code & ~1);
284
285         if (dynamic_size != 0 && (buffer_code & 1)) {
286                 req->in.dynamic = req->in.body + (buffer_code & ~1);
287                 if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
288                         DEBUG(1,("SMB2 request invalid dynamic size 0x%x\n", 
289                                  dynamic_size));
290                         smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
291                         return NT_STATUS_OK;
292                 }
293         }
294
295         /* 
296          * TODO: - make sure the length field is 64
297          *       - make sure it's a request
298          */
299
300         return smb2srv_reply(req);
301 }
302
303 /*
304  * init the SMB2 protocol related stuff
305  */
306 NTSTATUS smbsrv_init_smb2_connection(struct smbsrv_connection *smb_conn)
307 {
308         NTSTATUS status;
309
310         /* now initialise a few default values associated with this smb socket */
311         smb_conn->negotiate.max_send = 0xFFFF;
312
313         /* this is the size that w2k uses, and it appears to be important for
314            good performance */
315         smb_conn->negotiate.max_recv = lp_max_xmit();
316
317         smb_conn->negotiate.zone_offset = get_time_zone(time(NULL));
318
319         smb_conn->config.security = SEC_USER;
320         smb_conn->config.nt_status_support = True;
321
322         status = smbsrv_init_sessions(smb_conn, UINT64_MAX);
323         NT_STATUS_NOT_OK_RETURN(status);
324
325         return NT_STATUS_OK;
326         
327 }