Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-test
[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/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "smb_server/smb_server.h"
28 #include "smb_server/service_smb_proto.h"
29 #include "smb_server/smb2/smb2_server.h"
30 #include "smbd/service_stream.h"
31 #include "param/param.h"
32
33 static NTSTATUS smb2srv_negprot_secblob(struct smb2srv_request *req, DATA_BLOB *_blob)
34 {
35         struct gensec_security *gensec_security;
36         DATA_BLOB null_data_blob = data_blob(NULL, 0);
37         DATA_BLOB blob;
38         NTSTATUS nt_status;
39         struct cli_credentials *server_credentials;
40
41         nt_status = gensec_server_start(req,
42                                         req->smb_conn->connection->event.ctx,
43                                         req->smb_conn->lp_ctx,
44                                         req->smb_conn->connection->msg_ctx,
45                                         &gensec_security);
46         if (!NT_STATUS_IS_OK(nt_status)) {
47                 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
48                 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
49                 return nt_status;
50         }
51
52         server_credentials = cli_credentials_init(req);
53         if (!server_credentials) {
54                 smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
55                 return NT_STATUS_NO_MEMORY;
56         }
57
58         cli_credentials_set_conf(server_credentials, req->smb_conn->lp_ctx);
59         nt_status = cli_credentials_set_machine_account(server_credentials, req->smb_conn->lp_ctx);
60         if (!NT_STATUS_IS_OK(nt_status)) {
61                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
62                 talloc_free(server_credentials);
63                 server_credentials = NULL;
64         }
65
66         req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
67
68         gensec_set_target_service(gensec_security, "cifs");
69
70         gensec_set_credentials(gensec_security, server_credentials);
71
72         nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
73         if (!NT_STATUS_IS_OK(nt_status)) {
74                 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
75                 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
76                 return nt_status;
77         }
78
79         nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
80         if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
81                 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
82                 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
83                 return nt_status;
84         }
85
86         *_blob = blob;
87         return NT_STATUS_OK;
88 }
89
90 static NTSTATUS smb2srv_negprot_backend(struct smb2srv_request *req, struct smb2_negprot *io)
91 {
92         NTSTATUS status;
93         struct timeval current_time;
94         struct timeval boot_time;
95
96         /* we only do dialect 0 for now */
97         if (io->in.dialect_count < 1 ||
98             io->in.dialects[0] != 0) {
99                 return NT_STATUS_NOT_SUPPORTED;
100         }
101
102         req->smb_conn->negotiate.protocol = PROTOCOL_SMB2;
103
104         current_time = timeval_current(); /* TODO: handle timezone?! */
105         boot_time = timeval_current(); /* TODO: fix me */
106
107         io->out._pad            = 0;
108         io->out.unknown2        = 0x06;
109         ZERO_STRUCT(io->out.sessid);
110         io->out.unknown3        = 0x0d;
111         io->out.unknown4        = 0x00;
112         io->out.unknown5        = 0x01;
113         io->out.unknown6        = 0x01;
114         io->out.unknown7        = 0x01;
115         io->out.current_time    = timeval_to_nttime(&current_time);
116         io->out.boot_time       = timeval_to_nttime(&boot_time);
117         status = smb2srv_negprot_secblob(req, &io->out.secblob);
118         NT_STATUS_NOT_OK_RETURN(status);
119         io->out.unknown9        = 0x204d4c20;
120
121         return NT_STATUS_OK;
122 }
123
124 static void smb2srv_negprot_send(struct smb2srv_request *req, struct smb2_negprot *io)
125 {
126         NTSTATUS status;
127
128         if (NT_STATUS_IS_ERR(req->status)) {
129                 smb2srv_send_error(req, req->status); /* TODO: is this correct? */
130                 return;
131         }
132
133         status = smb2srv_setup_reply(req, 0x40, true, io->out.secblob.length);
134         if (!NT_STATUS_IS_OK(status)) {
135                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
136                 talloc_free(req);
137                 return;
138         }
139
140         SSVAL(req->out.body, 0x02, io->out._pad);
141         SIVAL(req->out.body, 0x04, io->out.unknown2);
142         memcpy(req->out.body+0x08, io->out.sessid, 16);
143         SIVAL(req->out.body, 0x18, io->out.unknown3);
144         SSVAL(req->out.body, 0x1C, io->out.unknown4);
145         SIVAL(req->out.body, 0x1E, io->out.unknown5);
146         SIVAL(req->out.body, 0x22, io->out.unknown6);
147         SSVAL(req->out.body, 0x26, io->out.unknown7);
148         push_nttime(req->out.body, 0x28, io->out.current_time);
149         push_nttime(req->out.body, 0x30, io->out.boot_time);
150         status = smb2_push_o16s16_blob(&req->out, 0x38, io->out.secblob);
151         if (!NT_STATUS_IS_OK(status)) {
152                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
153                 talloc_free(req);
154                 return;
155         }
156
157         SIVAL(req->out.body, 0x3C, io->out.unknown9);
158
159         smb2srv_send_reply(req);
160 }
161
162 void smb2srv_negprot_recv(struct smb2srv_request *req)
163 {
164         struct smb2_negprot *io;
165         int i;
166         DATA_BLOB guid_blob;
167         enum ndr_err_code ndr_err;
168
169         if (req->in.body_size < 0x26) {
170                 smb2srv_send_error(req,  NT_STATUS_FOOBAR);
171                 return;
172         }
173
174         io = talloc(req, struct smb2_negprot);
175         if (!io) {
176                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
177                 talloc_free(req);
178                 return;
179         }
180
181         io->in.dialect_count = SVAL(req->in.body, 0x02);
182         io->in.security_mode = SVAL(req->in.body, 0x04);
183         io->in.reserved      = SVAL(req->in.body, 0x06);
184         io->in.capabilities  = IVAL(req->in.body, 0x08);
185         guid_blob.data       = req->in.body + 0xC;
186         guid_blob.length     = 16;
187         ndr_err = ndr_pull_struct_blob(&guid_blob, req, NULL, &io->in.client_guid, 
188                                        (ndr_pull_flags_fn_t)ndr_pull_GUID);
189         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
190                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_FOOBAR));
191                 talloc_free(req);
192                 return;
193         }
194         io->in.start_time = smbcli_pull_nttime(req->in.body, 0x1C);
195
196         io->in.dialects = talloc_array(req, uint16_t, io->in.dialect_count);
197         if (io->in.dialects == NULL) {
198                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
199                 talloc_free(req);
200                 return;
201         }
202         for (i=0;i<io->in.dialect_count;i++) {
203                 io->in.dialects[i] = SVAL(req->in.body, 0x24+i*2);
204         }
205
206         req->status = smb2srv_negprot_backend(req, io);
207
208         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
209                 talloc_free(req);
210                 return;
211         }
212         smb2srv_negprot_send(req, io);
213 }
214
215 /*
216  * reply to a SMB negprot request with dialect "SMB 2.002"
217  */
218 void smb2srv_reply_smb_negprot(struct smbsrv_request *smb_req)
219 {
220         struct smb2srv_request *req;
221         uint32_t body_fixed_size = 0x26;
222
223         req = talloc_zero(smb_req->smb_conn, struct smb2srv_request);
224         if (!req) goto nomem;
225         req->smb_conn           = smb_req->smb_conn;
226         req->request_time       = smb_req->request_time;
227         talloc_steal(req, smb_req);
228
229         req->in.size      = NBT_HDR_SIZE+SMB2_HDR_BODY+body_fixed_size;
230         req->in.allocated = req->in.size;
231         req->in.buffer    = talloc_array(req, uint8_t, req->in.allocated);
232         if (!req->in.buffer) goto nomem;
233         req->in.hdr       = req->in.buffer + NBT_HDR_SIZE;
234         req->in.body      = req->in.hdr + SMB2_HDR_BODY;
235         req->in.body_size = body_fixed_size;
236         req->in.dynamic   = NULL;
237
238         SIVAL(req->in.hdr, 0,                           SMB2_MAGIC);
239         SSVAL(req->in.hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
240         SSVAL(req->in.hdr, SMB2_HDR_PAD1,               0);
241         SIVAL(req->in.hdr, SMB2_HDR_STATUS,             0);
242         SSVAL(req->in.hdr, SMB2_HDR_OPCODE,             SMB2_OP_NEGPROT);
243         SSVAL(req->in.hdr, SMB2_HDR_UNKNOWN1,           0);
244         SIVAL(req->in.hdr, SMB2_HDR_FLAGS,              0);
245         SIVAL(req->in.hdr, SMB2_HDR_CHAIN_OFFSET,       0);
246         SBVAL(req->in.hdr, SMB2_HDR_SEQNUM,             0);
247         SIVAL(req->in.hdr, SMB2_HDR_PID,                0);
248         SIVAL(req->in.hdr, SMB2_HDR_TID,                0);
249         SBVAL(req->in.hdr, SMB2_HDR_UID,                0);
250         memset(req->in.hdr+SMB2_HDR_SIG, 0, 16);
251
252         /* this seems to be a bug, they use 0x24 but the length is 0x26 */
253         SSVAL(req->in.body, 0x00, 0x24);
254
255         SSVAL(req->in.body, 0x02, 1);
256         memset(req->in.body+0x04, 0, 32);
257         SSVAL(req->in.body, 0x24, 0);
258
259         smb2srv_negprot_recv(req);
260         return;
261 nomem:
262         smbsrv_terminate_connection(smb_req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
263         talloc_free(req);
264         return;
265 }