added server side SMB2 signing
[kai/samba-autobuild/.git] / source4 / smb_server / smb2 / negprot.c
1 /* 
2    Unix SMB2 implementation.
3    
4    Copyright (C) Andrew Bartlett        2001-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 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 "auth/credentials/credentials.h"
23 #include "auth/gensec/gensec.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "smb_server/smb_server.h"
29 #include "smb_server/service_smb_proto.h"
30 #include "smb_server/smb2/smb2_server.h"
31 #include "smbd/service_stream.h"
32 #include "param/param.h"
33 #include "librpc/ndr/libndr.h"
34
35 static NTSTATUS smb2srv_negprot_secblob(struct smb2srv_request *req, DATA_BLOB *_blob)
36 {
37         struct gensec_security *gensec_security;
38         DATA_BLOB null_data_blob = data_blob(NULL, 0);
39         DATA_BLOB blob;
40         NTSTATUS nt_status;
41         struct cli_credentials *server_credentials;
42
43         nt_status = gensec_server_start(req,
44                                         req->smb_conn->connection->event.ctx,
45                                         req->smb_conn->lp_ctx,
46                                         req->smb_conn->connection->msg_ctx,
47                                         &gensec_security);
48         if (!NT_STATUS_IS_OK(nt_status)) {
49                 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
50                 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
51                 return nt_status;
52         }
53
54         server_credentials = cli_credentials_init(req);
55         if (!server_credentials) {
56                 smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
57                 return NT_STATUS_NO_MEMORY;
58         }
59
60         cli_credentials_set_conf(server_credentials, req->smb_conn->lp_ctx);
61         nt_status = cli_credentials_set_machine_account(server_credentials, req->smb_conn->lp_ctx);
62         if (!NT_STATUS_IS_OK(nt_status)) {
63                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
64                 talloc_free(server_credentials);
65                 server_credentials = NULL;
66         }
67
68         req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
69
70         gensec_set_target_service(gensec_security, "cifs");
71
72         gensec_set_credentials(gensec_security, server_credentials);
73
74         nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
75         if (!NT_STATUS_IS_OK(nt_status)) {
76                 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
77                 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
78                 return nt_status;
79         }
80
81         nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
82         if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
83                 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
84                 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
85                 return nt_status;
86         }
87
88         *_blob = blob;
89         return NT_STATUS_OK;
90 }
91
92 static NTSTATUS smb2srv_negprot_backend(struct smb2srv_request *req, struct smb2_negprot *io)
93 {
94         NTSTATUS status;
95         struct timeval current_time;
96         struct timeval boot_time;
97
98         /* we only do one dialect for now */
99         if (io->in.dialect_count < 1) {
100                 return NT_STATUS_NOT_SUPPORTED;
101         }
102         if (io->in.dialects[0] != 0 &&
103             io->in.dialects[0] != SMB2_DIALECT_REVISION) {
104                 DEBUG(0,("Got unexpected SMB2 dialect %u\n", io->in.dialects[0]));
105                 return NT_STATUS_NOT_SUPPORTED;
106         }
107
108         req->smb_conn->negotiate.protocol = PROTOCOL_SMB2;
109
110         current_time = timeval_current(); /* TODO: handle timezone?! */
111         boot_time = timeval_current(); /* TODO: fix me */
112
113         ZERO_STRUCT(io->out);
114         switch (lp_server_signing(req->smb_conn->lp_ctx)) {
115         case SMB_SIGNING_OFF:
116                 io->out.security_mode = 0;
117                 break;
118         case SMB_SIGNING_SUPPORTED:
119         case SMB_SIGNING_AUTO:
120                 io->out.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
121                 break;
122         case SMB_SIGNING_REQUIRED:
123                 io->out.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
124                 break;
125         }
126         io->out.dialect_revision   = SMB2_DIALECT_REVISION;
127         io->out.capabilities       = 0;
128         io->out.max_transact_size  = lp_parm_ulong(req->smb_conn->lp_ctx, NULL, 
129                                                    "smb2", "max transaction size", 0x10000);
130         io->out.max_read_size      = lp_parm_ulong(req->smb_conn->lp_ctx, NULL, 
131                                                    "smb2", "max read size", 0x10000);
132         io->out.max_write_size     = lp_parm_ulong(req->smb_conn->lp_ctx, NULL, 
133                                                    "smb2", "max write size", 0x10000);
134         io->out.system_time        = timeval_to_nttime(&current_time);
135         io->out.server_start_time  = timeval_to_nttime(&boot_time);
136         io->out.reserved2          = 0;
137         status = smb2srv_negprot_secblob(req, &io->out.secblob);
138         NT_STATUS_NOT_OK_RETURN(status);
139
140         return NT_STATUS_OK;
141 }
142
143 static void smb2srv_negprot_send(struct smb2srv_request *req, struct smb2_negprot *io)
144 {
145         NTSTATUS status;
146         enum ndr_err_code ndr_err;
147
148         if (NT_STATUS_IS_ERR(req->status)) {
149                 smb2srv_send_error(req, req->status); /* TODO: is this correct? */
150                 return;
151         }
152
153         status = smb2srv_setup_reply(req, 0x40, true, io->out.secblob.length);
154         if (!NT_STATUS_IS_OK(status)) {
155                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
156                 talloc_free(req);
157                 return;
158         }
159
160         SSVAL(req->out.body, 0x02, io->out.security_mode);
161         SIVAL(req->out.body, 0x04, io->out.dialect_revision);
162         SIVAL(req->out.body, 0x06, io->out.reserved);
163         ndr_err = smbcli_push_guid(req->out.body, 0x08, &io->out.server_guid);
164         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
165                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
166                 talloc_free(req);
167                 return;
168         }
169         SIVAL(req->out.body, 0x18, io->out.capabilities);
170         SIVAL(req->out.body, 0x1C, io->out.max_transact_size);
171         SIVAL(req->out.body, 0x20, io->out.max_read_size);
172         SIVAL(req->out.body, 0x24, io->out.max_write_size);
173         push_nttime(req->out.body, 0x28, io->out.system_time);
174         push_nttime(req->out.body, 0x30, io->out.server_start_time);
175         SIVAL(req->out.body, 0x3C, io->out.reserved2);
176         status = smb2_push_o16s16_blob(&req->out, 0x38, io->out.secblob);
177         if (!NT_STATUS_IS_OK(status)) {
178                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
179                 talloc_free(req);
180                 return;
181         }
182
183         smb2srv_send_reply(req);
184 }
185
186 void smb2srv_negprot_recv(struct smb2srv_request *req)
187 {
188         struct smb2_negprot *io;
189         int i;
190         enum ndr_err_code ndr_err;
191
192         if (req->in.body_size < 0x26) {
193                 smb2srv_send_error(req,  NT_STATUS_FOOBAR);
194                 return;
195         }
196
197         io = talloc(req, struct smb2_negprot);
198         if (!io) {
199                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
200                 talloc_free(req);
201                 return;
202         }
203
204         io->in.dialect_count = SVAL(req->in.body, 0x02);
205         io->in.security_mode = SVAL(req->in.body, 0x04);
206         io->in.reserved      = SVAL(req->in.body, 0x06);
207         io->in.capabilities  = IVAL(req->in.body, 0x08);
208         ndr_err = smbcli_pull_guid(req->in.body, 0xC, &io->in.client_guid);
209         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
210                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_FOOBAR));
211                 talloc_free(req);
212                 return;
213         }
214         io->in.start_time = smbcli_pull_nttime(req->in.body, 0x1C);
215
216         io->in.dialects = talloc_array(req, uint16_t, io->in.dialect_count);
217         if (io->in.dialects == NULL) {
218                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
219                 talloc_free(req);
220                 return;
221         }
222         for (i=0;i<io->in.dialect_count;i++) {
223                 io->in.dialects[i] = SVAL(req->in.body, 0x24+i*2);
224         }
225
226         req->status = smb2srv_negprot_backend(req, io);
227
228         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
229                 talloc_free(req);
230                 return;
231         }
232         smb2srv_negprot_send(req, io);
233 }
234
235 /*
236  * reply to a SMB negprot request with dialect "SMB 2.002"
237  */
238 void smb2srv_reply_smb_negprot(struct smbsrv_request *smb_req)
239 {
240         struct smb2srv_request *req;
241         uint32_t body_fixed_size = 0x26;
242
243         req = talloc_zero(smb_req->smb_conn, struct smb2srv_request);
244         if (!req) goto nomem;
245         req->smb_conn           = smb_req->smb_conn;
246         req->request_time       = smb_req->request_time;
247         talloc_steal(req, smb_req);
248
249         req->in.size      = NBT_HDR_SIZE+SMB2_HDR_BODY+body_fixed_size;
250         req->in.allocated = req->in.size;
251         req->in.buffer    = talloc_array(req, uint8_t, req->in.allocated);
252         if (!req->in.buffer) goto nomem;
253         req->in.hdr       = req->in.buffer + NBT_HDR_SIZE;
254         req->in.body      = req->in.hdr + SMB2_HDR_BODY;
255         req->in.body_size = body_fixed_size;
256         req->in.dynamic   = NULL;
257
258         smb2srv_setup_bufinfo(req);
259
260         SIVAL(req->in.hdr, 0,                           SMB2_MAGIC);
261         SSVAL(req->in.hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
262         SSVAL(req->in.hdr, SMB2_HDR_EPOCH,              0);
263         SIVAL(req->in.hdr, SMB2_HDR_STATUS,             0);
264         SSVAL(req->in.hdr, SMB2_HDR_OPCODE,             SMB2_OP_NEGPROT);
265         SSVAL(req->in.hdr, SMB2_HDR_CREDIT,             0);
266         SIVAL(req->in.hdr, SMB2_HDR_FLAGS,              0);
267         SIVAL(req->in.hdr, SMB2_HDR_NEXT_COMMAND,       0);
268         SBVAL(req->in.hdr, SMB2_HDR_MESSAGE_ID,         0);
269         SIVAL(req->in.hdr, SMB2_HDR_PID,                0);
270         SIVAL(req->in.hdr, SMB2_HDR_TID,                0);
271         SBVAL(req->in.hdr, SMB2_HDR_SESSION_ID,         0);
272         memset(req->in.hdr+SMB2_HDR_SIGNATURE, 0, 16);
273
274         /* this seems to be a bug, they use 0x24 but the length is 0x26 */
275         SSVAL(req->in.body, 0x00, 0x24);
276
277         SSVAL(req->in.body, 0x02, 1);
278         memset(req->in.body+0x04, 0, 32);
279         SSVAL(req->in.body, 0x24, 0);
280
281         smb2srv_negprot_recv(req);
282         return;
283 nomem:
284         smbsrv_terminate_connection(smb_req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
285         talloc_free(req);
286         return;
287 }