2 Unix SMB2 implementation.
4 Copyright (C) Andrew Bartlett 2001-2005
5 Copyright (C) Stefan Metzmacher 2005
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.
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.
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/>.
22 #include "auth/credentials/credentials.h"
23 #include "auth/gensec/gensec.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "smb_server/smb_server.h"
27 #include "smb_server/service_smb_proto.h"
28 #include "smb_server/smb2/smb2_server.h"
29 #include "smbd/service_stream.h"
30 #include "param/param.h"
32 static NTSTATUS smb2srv_negprot_secblob(struct smb2srv_request *req, DATA_BLOB *_blob)
34 struct gensec_security *gensec_security;
35 DATA_BLOB null_data_blob = data_blob(NULL, 0);
38 struct cli_credentials *server_credentials;
40 nt_status = gensec_server_start(req,
41 req->smb_conn->connection->event.ctx,
42 req->smb_conn->lp_ctx,
43 req->smb_conn->connection->msg_ctx,
45 if (!NT_STATUS_IS_OK(nt_status)) {
46 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
47 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
51 server_credentials = cli_credentials_init(req);
52 if (!server_credentials) {
53 smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
54 return NT_STATUS_NO_MEMORY;
57 cli_credentials_set_conf(server_credentials, req->smb_conn->lp_ctx);
58 nt_status = cli_credentials_set_machine_account(server_credentials, req->smb_conn->lp_ctx);
59 if (!NT_STATUS_IS_OK(nt_status)) {
60 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
61 talloc_free(server_credentials);
62 server_credentials = NULL;
65 req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
67 gensec_set_target_service(gensec_security, "cifs");
69 gensec_set_credentials(gensec_security, server_credentials);
71 nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
72 if (!NT_STATUS_IS_OK(nt_status)) {
73 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
74 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
78 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
79 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
80 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
81 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
89 static NTSTATUS smb2srv_negprot_backend(struct smb2srv_request *req, struct smb2_negprot *io)
92 struct timeval current_time;
93 struct timeval boot_time;
95 req->smb_conn->negotiate.protocol = PROTOCOL_SMB2;
97 current_time = timeval_current(); /* TODO: handle timezone?! */
98 boot_time = timeval_current(); /* TODO: fix me */
101 io->out.unknown2 = 0x06;
102 ZERO_STRUCT(io->out.sessid);
103 io->out.unknown3 = 0x0d;
104 io->out.unknown4 = 0x00;
105 io->out.unknown5 = 0x01;
106 io->out.unknown6 = 0x01;
107 io->out.unknown7 = 0x01;
108 io->out.current_time = timeval_to_nttime(¤t_time);
109 io->out.boot_time = timeval_to_nttime(&boot_time);
110 status = smb2srv_negprot_secblob(req, &io->out.secblob);
111 NT_STATUS_NOT_OK_RETURN(status);
112 io->out.unknown9 = 0x204d4c20;
117 static void smb2srv_negprot_send(struct smb2srv_request *req, struct smb2_negprot *io)
121 if (NT_STATUS_IS_ERR(req->status)) {
122 smb2srv_send_error(req, req->status); /* TODO: is this correct? */
126 status = smb2srv_setup_reply(req, 0x40, true, io->out.secblob.length);
127 if (!NT_STATUS_IS_OK(status)) {
128 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
133 SSVAL(req->out.body, 0x02, io->out._pad);
134 SIVAL(req->out.body, 0x04, io->out.unknown2);
135 memcpy(req->out.body+0x08, io->out.sessid, 16);
136 SIVAL(req->out.body, 0x18, io->out.unknown3);
137 SSVAL(req->out.body, 0x1C, io->out.unknown4);
138 SIVAL(req->out.body, 0x1E, io->out.unknown5);
139 SIVAL(req->out.body, 0x22, io->out.unknown6);
140 SSVAL(req->out.body, 0x26, io->out.unknown7);
141 push_nttime(req->out.body, 0x28, io->out.current_time);
142 push_nttime(req->out.body, 0x30, io->out.boot_time);
143 status = smb2_push_o16s16_blob(&req->out, 0x38, io->out.secblob);
144 if (!NT_STATUS_IS_OK(status)) {
145 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
150 SIVAL(req->out.body, 0x3C, io->out.unknown9);
152 smb2srv_send_reply(req);
155 void smb2srv_negprot_recv(struct smb2srv_request *req)
157 struct smb2_negprot *io;
159 if (req->in.body_size < 0x26) {
160 smb2srv_send_error(req, NT_STATUS_FOOBAR);
164 io = talloc(req, struct smb2_negprot);
166 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
171 io->in.unknown1 = SVAL(req->in.body, 0x02);
172 memcpy(io->in.unknown2, req->in.body + 0x04, 0x20);
173 io->in.unknown3 = SVAL(req->in.body, 0x24);
175 req->status = smb2srv_negprot_backend(req, io);
177 if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
181 smb2srv_negprot_send(req, io);
185 * reply to a SMB negprot request with dialect "SMB 2.001"
187 void smb2srv_reply_smb_negprot(struct smbsrv_request *smb_req)
189 struct smb2srv_request *req;
190 uint32_t body_fixed_size = 0x26;
192 /* create a fake SMB2 negprot request */
193 req = talloc_zero(smb_req->smb_conn, struct smb2srv_request);
194 if (!req) goto nomem;
195 req->smb_conn = smb_req->smb_conn;
196 req->request_time = smb_req->request_time;
197 talloc_steal(req, smb_req);
199 req->in.size = NBT_HDR_SIZE+SMB2_HDR_BODY+body_fixed_size;
200 req->in.allocated = req->in.size;
201 req->in.buffer = talloc_array(req, uint8_t, req->in.allocated);
202 if (!req->in.buffer) goto nomem;
203 req->in.hdr = req->in.buffer + NBT_HDR_SIZE;
204 req->in.body = req->in.hdr + SMB2_HDR_BODY;
205 req->in.body_size = body_fixed_size;
206 req->in.dynamic = NULL;
208 SIVAL(req->in.hdr, 0, SMB2_MAGIC);
209 SSVAL(req->in.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
210 SSVAL(req->in.hdr, SMB2_HDR_PAD1, 0);
211 SIVAL(req->in.hdr, SMB2_HDR_STATUS, 0);
212 SSVAL(req->in.hdr, SMB2_HDR_OPCODE, SMB2_OP_NEGPROT);
213 SSVAL(req->in.hdr, SMB2_HDR_UNKNOWN1, 0);
214 SIVAL(req->in.hdr, SMB2_HDR_FLAGS, 0);
215 SIVAL(req->in.hdr, SMB2_HDR_CHAIN_OFFSET, 0);
216 SBVAL(req->in.hdr, SMB2_HDR_SEQNUM, 0);
217 SIVAL(req->in.hdr, SMB2_HDR_PID, 0);
218 SIVAL(req->in.hdr, SMB2_HDR_TID, 0);
219 SBVAL(req->in.hdr, SMB2_HDR_UID, 0);
220 memset(req->in.hdr+SMB2_HDR_SIG, 0, 16);
222 /* this seems to be a bug, they use 0x24 but the length is 0x26 */
223 SSVAL(req->in.body, 0x00, 0x24);
225 SSVAL(req->in.body, 0x02, 1);
226 memset(req->in.body+0x04, 0, 32);
227 SSVAL(req->in.body, 0x24, 0);
229 smb2srv_negprot_recv(req);
232 smbsrv_terminate_connection(smb_req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));