Convert SMB and SMB2 code to use a common buffer handling structure
[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                 return NT_STATUS_NOT_SUPPORTED;
99         }
100         if (io->in.dialects[0] != 0) {
101                 DEBUG(0,("Got unexpected SMB2 dialect %u\n", io->in.dialects[0]));
102         }
103
104         req->smb_conn->negotiate.protocol = PROTOCOL_SMB2;
105
106         current_time = timeval_current(); /* TODO: handle timezone?! */
107         boot_time = timeval_current(); /* TODO: fix me */
108
109         ZERO_STRUCT(io->out);
110         io->out.security_mode      = 0; /* no signing yet */
111          /* choose the first dialect offered for now */
112         io->out.dialect_revision   = io->in.dialects[0];
113         io->out.capabilities       = 0;
114         io->out.max_transact_size  = 0x10000;
115         io->out.max_read_size      = 0x10000;
116         io->out.max_write_size     = 0x10000;
117         io->out.system_time        = timeval_to_nttime(&current_time);
118         io->out.server_start_time  = timeval_to_nttime(&boot_time);
119         io->out.reserved2          = 0;
120         status = smb2srv_negprot_secblob(req, &io->out.secblob);
121         NT_STATUS_NOT_OK_RETURN(status);
122
123         return NT_STATUS_OK;
124 }
125
126 static void smb2srv_negprot_send(struct smb2srv_request *req, struct smb2_negprot *io)
127 {
128         NTSTATUS status;
129         enum ndr_err_code ndr_err;
130
131         if (NT_STATUS_IS_ERR(req->status)) {
132                 smb2srv_send_error(req, req->status); /* TODO: is this correct? */
133                 return;
134         }
135
136         status = smb2srv_setup_reply(req, 0x40, true, io->out.secblob.length);
137         if (!NT_STATUS_IS_OK(status)) {
138                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
139                 talloc_free(req);
140                 return;
141         }
142
143         SSVAL(req->out.body, 0x02, io->out.security_mode);
144         SIVAL(req->out.body, 0x04, io->out.dialect_revision);
145         SIVAL(req->out.body, 0x06, io->out.reserved);
146         ndr_err = smbcli_push_guid(req->out.body, 0x08, &io->out.server_guid);
147         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
148                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
149                 talloc_free(req);
150                 return;
151         }
152         SIVAL(req->out.body, 0x18, io->out.capabilities);
153         SIVAL(req->out.body, 0x1C, io->out.max_transact_size);
154         SIVAL(req->out.body, 0x20, io->out.max_read_size);
155         SIVAL(req->out.body, 0x24, io->out.max_write_size);
156         push_nttime(req->out.body, 0x28, io->out.system_time);
157         push_nttime(req->out.body, 0x30, io->out.server_start_time);
158         SIVAL(req->out.body, 0x3C, io->out.reserved2);
159         status = smb2_push_o16s16_blob(&req->out, 0x38, io->out.secblob);
160         if (!NT_STATUS_IS_OK(status)) {
161                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
162                 talloc_free(req);
163                 return;
164         }
165
166         smb2srv_send_reply(req);
167 }
168
169 void smb2srv_negprot_recv(struct smb2srv_request *req)
170 {
171         struct smb2_negprot *io;
172         int i;
173         enum ndr_err_code ndr_err;
174
175         if (req->in.body_size < 0x26) {
176                 smb2srv_send_error(req,  NT_STATUS_FOOBAR);
177                 return;
178         }
179
180         io = talloc(req, struct smb2_negprot);
181         if (!io) {
182                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
183                 talloc_free(req);
184                 return;
185         }
186
187         io->in.dialect_count = SVAL(req->in.body, 0x02);
188         io->in.security_mode = SVAL(req->in.body, 0x04);
189         io->in.reserved      = SVAL(req->in.body, 0x06);
190         io->in.capabilities  = IVAL(req->in.body, 0x08);
191         ndr_err = smbcli_pull_guid(req->in.body, 0xC, &io->in.client_guid);
192         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
193                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_FOOBAR));
194                 talloc_free(req);
195                 return;
196         }
197         io->in.start_time = smbcli_pull_nttime(req->in.body, 0x1C);
198
199         io->in.dialects = talloc_array(req, uint16_t, io->in.dialect_count);
200         if (io->in.dialects == NULL) {
201                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
202                 talloc_free(req);
203                 return;
204         }
205         for (i=0;i<io->in.dialect_count;i++) {
206                 io->in.dialects[i] = SVAL(req->in.body, 0x24+i*2);
207         }
208
209         req->status = smb2srv_negprot_backend(req, io);
210
211         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
212                 talloc_free(req);
213                 return;
214         }
215         smb2srv_negprot_send(req, io);
216 }
217
218 /*
219  * reply to a SMB negprot request with dialect "SMB 2.002"
220  */
221 void smb2srv_reply_smb_negprot(struct smbsrv_request *smb_req)
222 {
223         struct smb2srv_request *req;
224         uint32_t body_fixed_size = 0x26;
225
226         req = talloc_zero(smb_req->smb_conn, struct smb2srv_request);
227         if (!req) goto nomem;
228         req->smb_conn           = smb_req->smb_conn;
229         req->request_time       = smb_req->request_time;
230         talloc_steal(req, smb_req);
231
232         req->in.size      = NBT_HDR_SIZE+SMB2_HDR_BODY+body_fixed_size;
233         req->in.allocated = req->in.size;
234         req->in.buffer    = talloc_array(req, uint8_t, req->in.allocated);
235         if (!req->in.buffer) goto nomem;
236         req->in.hdr       = req->in.buffer + NBT_HDR_SIZE;
237         req->in.body      = req->in.hdr + SMB2_HDR_BODY;
238         req->in.body_size = body_fixed_size;
239         req->in.dynamic   = NULL;
240
241         smb2srv_setup_bufinfo(req);
242
243         SIVAL(req->in.hdr, 0,                           SMB2_MAGIC);
244         SSVAL(req->in.hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
245         SSVAL(req->in.hdr, SMB2_HDR_EPOCH,              0);
246         SIVAL(req->in.hdr, SMB2_HDR_STATUS,             0);
247         SSVAL(req->in.hdr, SMB2_HDR_OPCODE,             SMB2_OP_NEGPROT);
248         SSVAL(req->in.hdr, SMB2_HDR_CREDIT,             0);
249         SIVAL(req->in.hdr, SMB2_HDR_FLAGS,              0);
250         SIVAL(req->in.hdr, SMB2_HDR_NEXT_COMMAND,       0);
251         SBVAL(req->in.hdr, SMB2_HDR_MESSAGE_ID,         0);
252         SIVAL(req->in.hdr, SMB2_HDR_PID,                0);
253         SIVAL(req->in.hdr, SMB2_HDR_TID,                0);
254         SBVAL(req->in.hdr, SMB2_HDR_SESSION_ID,         0);
255         memset(req->in.hdr+SMB2_HDR_SIGNATURE, 0, 16);
256
257         /* this seems to be a bug, they use 0x24 but the length is 0x26 */
258         SSVAL(req->in.body, 0x00, 0x24);
259
260         SSVAL(req->in.body, 0x02, 1);
261         memset(req->in.body+0x04, 0, 32);
262         SSVAL(req->in.body, 0x24, 0);
263
264         smb2srv_negprot_recv(req);
265         return;
266 nomem:
267         smbsrv_terminate_connection(smb_req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
268         talloc_free(req);
269         return;
270 }