s3:smbd: add support for SMB2 signing
[kai/samba.git] / source3 / smbd / smb2_sesssetup.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_session_setup(struct smbd_smb2_request *req,
26                                         uint64_t in_session_id,
27                                         uint8_t in_security_mode,
28                                         DATA_BLOB in_security_buffer,
29                                         uint16_t *out_session_flags,
30                                         DATA_BLOB *out_security_buffer,
31                                         uint64_t *out_session_id);
32
33 NTSTATUS smbd_smb2_request_process_sesssetup(struct smbd_smb2_request *req)
34 {
35         const uint8_t *inhdr;
36         const uint8_t *inbody;
37         int i = req->current_idx;
38         uint8_t *outhdr;
39         DATA_BLOB outbody;
40         DATA_BLOB outdyn;
41         size_t expected_body_size = 0x19;
42         size_t body_size;
43         uint64_t in_session_id;
44         uint8_t in_security_mode;
45         uint16_t in_security_offset;
46         uint16_t in_security_length;
47         DATA_BLOB in_security_buffer;
48         uint16_t out_session_flags;
49         uint64_t out_session_id;
50         uint16_t out_security_offset;
51         DATA_BLOB out_security_buffer;
52         NTSTATUS status;
53
54         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
55
56         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
57                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
58         }
59
60         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
61
62         body_size = SVAL(inbody, 0x00);
63         if (body_size != expected_body_size) {
64                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
65         }
66
67         in_security_offset = SVAL(inbody, 0x0C);
68         in_security_length = SVAL(inbody, 0x0E);
69
70         if (in_security_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
71                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
72         }
73
74         if (in_security_length > req->in.vector[i+2].iov_len) {
75                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
76         }
77
78         in_session_id = BVAL(inhdr, SMB2_HDR_SESSION_ID);
79         in_security_mode = CVAL(inbody, 0x03);
80         in_security_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
81         in_security_buffer.length = in_security_length;
82
83         status = smbd_smb2_session_setup(req,
84                                          in_session_id,
85                                          in_security_mode,
86                                          in_security_buffer,
87                                          &out_session_flags,
88                                          &out_security_buffer,
89                                          &out_session_id);
90         if (!NT_STATUS_IS_OK(status) &&
91             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
92                 status = nt_status_squash(status);
93                 return smbd_smb2_request_error(req, status);
94         }
95
96         out_security_offset = SMB2_HDR_BODY + 0x08;
97
98         outhdr = (uint8_t *)req->out.vector[i].iov_base;
99
100         outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
101         if (outbody.data == NULL) {
102                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
103         }
104
105         SBVAL(outhdr, SMB2_HDR_SESSION_ID, out_session_id);
106
107         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
108         SSVAL(outbody.data, 0x02,
109               out_session_flags);               /* session flags */
110         SSVAL(outbody.data, 0x04,
111               out_security_offset);             /* security buffer offset */
112         SSVAL(outbody.data, 0x06,
113               out_security_buffer.length);      /* security buffer length */
114
115         outdyn = out_security_buffer;
116
117         return smbd_smb2_request_done_ex(req, status, outbody, &outdyn);
118 }
119
120 static int smbd_smb2_session_destructor(struct smbd_smb2_session *session)
121 {
122         if (session->conn == NULL) {
123                 return 0;
124         }
125
126         /* first free all tcons */
127         while (session->tcons.list) {
128                 talloc_free(session->tcons.list);
129         }
130
131         idr_remove(session->conn->smb2.sessions.idtree, session->vuid);
132         DLIST_REMOVE(session->conn->smb2.sessions.list, session);
133
134         session->vuid = 0;
135         session->status = NT_STATUS_USER_SESSION_DELETED;
136         session->conn = NULL;
137
138         return 0;
139 }
140
141 static NTSTATUS smbd_smb2_session_setup(struct smbd_smb2_request *req,
142                                         uint64_t in_session_id,
143                                         uint8_t in_security_mode,
144                                         DATA_BLOB in_security_buffer,
145                                         uint16_t *out_session_flags,
146                                         DATA_BLOB *out_security_buffer,
147                                         uint64_t *out_session_id)
148 {
149         struct smbd_smb2_session *session;
150         NTSTATUS status;
151
152         *out_session_flags = 0;
153
154         if (in_session_id == 0) {
155                 int id;
156
157                 /* create a new session */
158                 session = talloc_zero(req->conn, struct smbd_smb2_session);
159                 if (session == NULL) {
160                         return NT_STATUS_NO_MEMORY;
161                 }
162                 session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
163                 id = idr_get_new_random(req->conn->smb2.sessions.idtree,
164                                         session,
165                                         req->conn->smb2.sessions.limit);
166                 if (id == -1) {
167                         return NT_STATUS_INSUFFICIENT_RESOURCES;
168                 }
169                 session->vuid = id;
170
171                 session->tcons.idtree = idr_init(session);
172                 if (session->tcons.idtree == NULL) {
173                         return NT_STATUS_NO_MEMORY;
174                 }
175                 session->tcons.limit = 0x00FFFFFF;
176                 session->tcons.list = NULL;
177
178                 DLIST_ADD_END(req->conn->smb2.sessions.list, session,
179                               struct smbd_smb2_session *);
180                 session->conn = req->conn;
181                 talloc_set_destructor(session, smbd_smb2_session_destructor);
182         } else {
183                 void *p;
184
185                 /* lookup an existing session */
186                 p = idr_find(req->conn->smb2.sessions.idtree, in_session_id);
187                 if (p == NULL) {
188                         return NT_STATUS_USER_SESSION_DELETED;
189                 }
190                 session = talloc_get_type_abort(p, struct smbd_smb2_session);
191         }
192
193         if (NT_STATUS_IS_OK(session->status)) {
194                 return NT_STATUS_REQUEST_NOT_ACCEPTED;
195         }
196
197         if (session->auth_ntlmssp_state == NULL) {
198                 status = auth_ntlmssp_start(&session->auth_ntlmssp_state);
199                 if (!NT_STATUS_IS_OK(status)) {
200                         TALLOC_FREE(session);
201                         return status;
202                 }
203         }
204
205         status = auth_ntlmssp_update(session->auth_ntlmssp_state,
206                                      in_security_buffer,
207                                      out_security_buffer);
208         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
209                 *out_session_id = session->vuid;
210                 return status;
211         } else if (!NT_STATUS_IS_OK(status)) {
212                 TALLOC_FREE(session);
213                 return status;
214         }
215
216         /* TODO: setup session key for signing */
217
218         if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
219             lp_server_signing() == Required) {
220                 session->do_signing = true;
221         }
222
223         if (session->auth_ntlmssp_state->server_info->guest) {
224                 /* we map anonymous to guest internally */
225                 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
226                 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
227                 /* force no signing */
228                 session->do_signing = false;
229         }
230
231         session->server_info = session->auth_ntlmssp_state->server_info;
232         data_blob_free(&session->server_info->user_session_key);
233         session->server_info->user_session_key =
234                         data_blob_talloc(
235                         session->server_info,
236                         session->auth_ntlmssp_state->ntlmssp_state->session_key.data,
237                         session->auth_ntlmssp_state->ntlmssp_state->session_key.length);
238         if (session->auth_ntlmssp_state->ntlmssp_state->session_key.length > 0) {
239                 if (session->server_info->user_session_key.data == NULL) {
240                         TALLOC_FREE(session);
241                         return NT_STATUS_NO_MEMORY;
242                 }
243         }
244         session->session_key = session->server_info->user_session_key;
245
246         session->status = NT_STATUS_OK;
247
248         /*
249          * we attach the session to the request
250          * so that the response can be signed
251          */
252         req->session = session;
253         if (session->do_signing) {
254                 req->do_signing = true;
255         }
256
257         *out_session_id = session->vuid;
258         return status;
259 }
260
261 NTSTATUS smbd_smb2_request_check_session(struct smbd_smb2_request *req)
262 {
263         const uint8_t *inhdr;
264         int i = req->current_idx;
265         uint64_t in_session_id;
266         void *p;
267         struct smbd_smb2_session *session;
268
269         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
270
271         in_session_id = BVAL(inhdr, SMB2_HDR_SESSION_ID);
272
273         /* lookup an existing session */
274         p = idr_find(req->conn->smb2.sessions.idtree, in_session_id);
275         if (p == NULL) {
276                 return NT_STATUS_USER_SESSION_DELETED;
277         }
278         session = talloc_get_type_abort(p, struct smbd_smb2_session);
279
280         if (!NT_STATUS_IS_OK(session->status)) {
281                 return NT_STATUS_ACCESS_DENIED;
282         }
283
284         req->session = session;
285         return NT_STATUS_OK;
286 }
287
288 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
289 {
290         const uint8_t *inbody;
291         int i = req->current_idx;
292         DATA_BLOB outbody;
293         size_t expected_body_size = 0x04;
294         size_t body_size;
295
296         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
297                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
298         }
299
300         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
301
302         body_size = SVAL(inbody, 0x00);
303         if (body_size != expected_body_size) {
304                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
305         }
306
307         /*
308          * TODO: cancel all outstanding requests on the session
309          *       and delete all tree connections.
310          */
311         smbd_smb2_session_destructor(req->session);
312         /*
313          * we may need to sign the response, so we need to keep
314          * the session until the response is sent to the wire.
315          */
316         talloc_steal(req, req->session);
317
318         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
319         if (outbody.data == NULL) {
320                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
321         }
322
323         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
324         SSVAL(outbody.data, 0x02, 0);           /* reserved */
325
326         return smbd_smb2_request_done(req, outbody, NULL);
327 }