r22866: handle incoming chained smb2 requests in our server code to let
[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 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 "auth/credentials/credentials.h"
24 #include "auth/gensec/gensec.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
32 static NTSTATUS smb2srv_negprot_secblob(struct smb2srv_request *req, DATA_BLOB *_blob)
33 {
34         struct gensec_security *gensec_security;
35         DATA_BLOB null_data_blob = data_blob(NULL, 0);
36         DATA_BLOB blob;
37         NTSTATUS nt_status;
38         struct cli_credentials *server_credentials;
39
40         nt_status = gensec_server_start(req,
41                                         req->smb_conn->connection->event.ctx,
42                                         req->smb_conn->connection->msg_ctx,
43                                         &gensec_security);
44         if (!NT_STATUS_IS_OK(nt_status)) {
45                 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
46                 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
47                 return nt_status;
48         }
49
50         server_credentials = cli_credentials_init(req);
51         if (!server_credentials) {
52                 smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
53                 return NT_STATUS_NO_MEMORY;
54         }
55
56         cli_credentials_set_conf(server_credentials);
57         nt_status = cli_credentials_set_machine_account(server_credentials);
58         if (!NT_STATUS_IS_OK(nt_status)) {
59                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
60                 talloc_free(server_credentials);
61                 server_credentials = NULL;
62         }
63
64         req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
65
66         gensec_set_target_service(gensec_security, "cifs");
67
68         gensec_set_credentials(gensec_security, server_credentials);
69
70         nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
71         if (!NT_STATUS_IS_OK(nt_status)) {
72                 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
73                 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
74                 return nt_status;
75         }
76
77         nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
78         if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
79                 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
80                 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
81                 return nt_status;
82         }
83
84         *_blob = blob;
85         return NT_STATUS_OK;
86 }
87
88 static NTSTATUS smb2srv_negprot_backend(struct smb2srv_request *req, struct smb2_negprot *io)
89 {
90         NTSTATUS status;
91         struct timeval current_time;
92         struct timeval boot_time;
93
94         req->smb_conn->negotiate.protocol = PROTOCOL_SMB2;
95
96         current_time = timeval_current(); /* TODO: handle timezone?! */
97         boot_time = timeval_current(); /* TODO: fix me */
98
99         io->out._pad            = 0;
100         io->out.unknown2        = 0x06;
101         ZERO_STRUCT(io->out.sessid);
102         io->out.unknown3        = 0x0d;
103         io->out.unknown4        = 0x00;
104         io->out.unknown5        = 0x01;
105         io->out.unknown6        = 0x01;
106         io->out.unknown7        = 0x01;
107         io->out.current_time    = timeval_to_nttime(&current_time);
108         io->out.boot_time       = timeval_to_nttime(&boot_time);
109         status = smb2srv_negprot_secblob(req, &io->out.secblob);
110         NT_STATUS_NOT_OK_RETURN(status);
111         io->out.unknown9        = 0x204d4c20;
112
113         return NT_STATUS_OK;
114 }
115
116 static void smb2srv_negprot_send(struct smb2srv_request *req, struct smb2_negprot *io)
117 {
118         NTSTATUS status;
119
120         if (NT_STATUS_IS_ERR(req->status)) {
121                 smb2srv_send_error(req, req->status); /* TODO: is this correct? */
122                 return;
123         }
124
125         status = smb2srv_setup_reply(req, 0x40, True, io->out.secblob.length);
126         if (!NT_STATUS_IS_OK(status)) {
127                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
128                 talloc_free(req);
129                 return;
130         }
131
132         SSVAL(req->out.body, 0x02, io->out._pad);
133         SIVAL(req->out.body, 0x04, io->out.unknown2);
134         memcpy(req->out.body+0x08, io->out.sessid, 16);
135         SIVAL(req->out.body, 0x18, io->out.unknown3);
136         SSVAL(req->out.body, 0x1C, io->out.unknown4);
137         SIVAL(req->out.body, 0x1E, io->out.unknown5);
138         SIVAL(req->out.body, 0x22, io->out.unknown6);
139         SSVAL(req->out.body, 0x26, io->out.unknown7);
140         push_nttime(req->out.body, 0x28, io->out.current_time);
141         push_nttime(req->out.body, 0x30, io->out.boot_time);
142         status = smb2_push_o16s16_blob(&req->out, 0x38, io->out.secblob);
143         if (!NT_STATUS_IS_OK(status)) {
144                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
145                 talloc_free(req);
146                 return;
147         }
148
149         SIVAL(req->out.body, 0x3C, io->out.unknown9);
150
151         smb2srv_send_reply(req);
152 }
153
154 void smb2srv_negprot_recv(struct smb2srv_request *req)
155 {
156         struct smb2_negprot *io;
157
158         if (req->in.body_size < 0x26) {
159                 smb2srv_send_error(req,  NT_STATUS_FOOBAR);
160                 return;
161         }
162
163         io = talloc(req, struct smb2_negprot);
164         if (!io) {
165                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
166                 talloc_free(req);
167                 return;
168         }
169
170         io->in.unknown1 = SVAL(req->in.body, 0x02);
171         memcpy(io->in.unknown2, req->in.body + 0x04, 0x20);
172         io->in.unknown3 = SVAL(req->in.body, 0x24);
173
174         req->status = smb2srv_negprot_backend(req, io);
175
176         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
177                 talloc_free(req);
178                 return;
179         }
180         smb2srv_negprot_send(req, io);
181 }
182
183 /*
184  * reply to a SMB negprot request with dialect "SMB 2.001"
185  */
186 void smb2srv_reply_smb_negprot(struct smbsrv_request *smb_req)
187 {
188         struct smb2srv_request *req;
189         uint32_t body_fixed_size = 0x26;
190
191         /* create a fake SMB2 negprot request */
192         req = talloc_zero(smb_req->smb_conn, struct smb2srv_request);
193         if (!req) goto nomem;
194         req->smb_conn           = smb_req->smb_conn;
195         req->request_time       = smb_req->request_time;
196         talloc_steal(req, smb_req);
197
198         req->in.size      = NBT_HDR_SIZE+SMB2_HDR_BODY+body_fixed_size;
199         req->in.allocated = req->in.size;
200         req->in.buffer    = talloc_size(req, req->in.allocated);
201         if (!req->in.buffer) goto nomem;
202         req->in.hdr       = req->in.buffer + NBT_HDR_SIZE;
203         req->in.body      = req->in.hdr + SMB2_HDR_BODY;
204         req->in.body_size = body_fixed_size;
205         req->in.dynamic   = NULL;
206
207         SIVAL(req->in.hdr, 0,                           SMB2_MAGIC);
208         SSVAL(req->in.hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
209         SSVAL(req->in.hdr, SMB2_HDR_PAD1,               0);
210         SIVAL(req->in.hdr, SMB2_HDR_STATUS,             0);
211         SSVAL(req->in.hdr, SMB2_HDR_OPCODE,             SMB2_OP_NEGPROT);
212         SSVAL(req->in.hdr, SMB2_HDR_UNKNOWN1,           0);
213         SIVAL(req->in.hdr, SMB2_HDR_FLAGS,              0);
214         SIVAL(req->in.hdr, SMB2_HDR_CHAIN_OFFSET,       0);
215         SBVAL(req->in.hdr, SMB2_HDR_SEQNUM,             0);
216         SIVAL(req->in.hdr, SMB2_HDR_PID,                0);
217         SIVAL(req->in.hdr, SMB2_HDR_TID,                0);
218         SBVAL(req->in.hdr, SMB2_HDR_UID,                0);
219         memset(req->in.hdr+SMB2_HDR_SIG, 0, 16);
220
221         /* this seems to be a bug, they use 0x24 but the length is 0x26 */
222         SSVAL(req->in.body, 0x00, 0x24);
223
224         SSVAL(req->in.body, 0x02, 1);
225         memset(req->in.body+0x04, 0, 32);
226         SSVAL(req->in.body, 0x24, 0);
227
228         smb2srv_negprot_recv(req);
229         return;
230 nomem:
231         smbsrv_terminate_connection(smb_req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
232         talloc_free(req);
233         return;
234 }