r16734: the 2 bytes after the opcode and before the flags,
[Samba/aatanasov.git] / source / smb_server / smb2 / negprot.c
blob66ba9027464ae5af92960c68c54d6efe11ccb85c
1 /*
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #include "auth/auth.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"
31 static NTSTATUS smb2srv_negprot_secblob(struct smb2srv_request *req, DATA_BLOB *_blob)
33 struct gensec_security *gensec_security;
34 DATA_BLOB null_data_blob = data_blob(NULL, 0);
35 DATA_BLOB blob;
36 NTSTATUS nt_status;
37 struct cli_credentials *server_credentials;
39 nt_status = gensec_server_start(req, &gensec_security,
40 req->smb_conn->connection->event.ctx);
41 if (!NT_STATUS_IS_OK(nt_status)) {
42 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
43 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
44 return nt_status;
47 server_credentials = cli_credentials_init(req);
48 if (!server_credentials) {
49 smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
50 return NT_STATUS_NO_MEMORY;
53 cli_credentials_set_conf(server_credentials);
54 nt_status = cli_credentials_set_machine_account(server_credentials);
55 if (!NT_STATUS_IS_OK(nt_status)) {
56 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
57 talloc_free(server_credentials);
58 server_credentials = NULL;
61 req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
63 gensec_set_target_service(gensec_security, "cifs");
65 gensec_set_credentials(gensec_security, server_credentials);
67 nt_status = gensec_start_mech_by_oid(gensec_security, GENSEC_OID_SPNEGO);
68 if (!NT_STATUS_IS_OK(nt_status)) {
69 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status)));
70 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO\n");
71 return nt_status;
74 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
75 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
76 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
77 smbsrv_terminate_connection(req->smb_conn, "Failed to start SPNEGO - no first token\n");
78 return nt_status;
81 *_blob = blob;
82 return NT_STATUS_OK;
85 static NTSTATUS smb2srv_negprot_backend(struct smb2srv_request *req, struct smb2_negprot *io)
87 NTSTATUS status;
88 struct timeval current_time;
89 struct timeval boot_time;
91 current_time = timeval_current(); /* TODO: handle timezone?! */
92 boot_time = timeval_current(); /* TODO: fix me */
94 io->out._pad = 0;
95 io->out.unknown2 = 0x06;
96 ZERO_STRUCT(io->out.sessid);
97 io->out.unknown3 = 0x0d;
98 io->out.unknown4 = 0x00;
99 io->out.unknown5 = 0x01;
100 io->out.unknown6 = 0x01;
101 io->out.unknown7 = 0x01;
102 io->out.current_time = timeval_to_nttime(&current_time);
103 io->out.boot_time = timeval_to_nttime(&boot_time);
104 status = smb2srv_negprot_secblob(req, &io->out.secblob);
105 NT_STATUS_NOT_OK_RETURN(status);
106 io->out.unknown9 = 0x204d4c20;
108 return NT_STATUS_OK;
111 static void smb2srv_negprot_send(struct smb2srv_request *req, struct smb2_negprot *io)
113 NTSTATUS status;
115 if (NT_STATUS_IS_ERR(req->status)) {
116 smb2srv_send_error(req, req->status); /* TODO: is this correct? */
117 return;
120 status = smb2srv_setup_reply(req, 0x40, True, io->out.secblob.length);
121 if (!NT_STATUS_IS_OK(status)) {
122 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
123 talloc_free(req);
124 return;
127 SSVAL(req->out.body, 0x02, io->out._pad);
128 SIVAL(req->out.body, 0x04, io->out.unknown2);
129 memcpy(req->out.body+0x08, io->out.sessid, 16);
130 SIVAL(req->out.body, 0x18, io->out.unknown3);
131 SSVAL(req->out.body, 0x1C, io->out.unknown4);
132 SIVAL(req->out.body, 0x1E, io->out.unknown5);
133 SIVAL(req->out.body, 0x22, io->out.unknown6);
134 SSVAL(req->out.body, 0x26, io->out.unknown7);
135 push_nttime(req->out.body, 0x28, io->out.current_time);
136 push_nttime(req->out.body, 0x30, io->out.boot_time);
137 status = smb2_push_o16s16_blob(&req->out, 0x38, io->out.secblob);
138 if (!NT_STATUS_IS_OK(status)) {
139 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
140 talloc_free(req);
141 return;
144 SIVAL(req->out.body, 0x3C, io->out.unknown9);
146 smb2srv_send_reply(req);
149 void smb2srv_negprot_recv(struct smb2srv_request *req)
151 struct smb2_negprot *io;
153 if (req->in.body_size < 0x26) {
154 smb2srv_send_error(req, NT_STATUS_FOOBAR);
155 return;
158 io = talloc(req, struct smb2_negprot);
159 if (!io) {
160 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
161 talloc_free(req);
162 return;
165 io->in.unknown1 = SVAL(req->in.body, 0x02);
166 memcpy(io->in.unknown2, req->in.body + 0x04, 0x20);
167 io->in.unknown3 = SVAL(req->in.body, 0x24);
169 req->status = smb2srv_negprot_backend(req, io);
171 if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
172 talloc_free(req);
173 return;
175 smb2srv_negprot_send(req, io);
179 * reply to a SMB negprot request with dialect "SMB 2.001"
181 void smb2srv_reply_smb_negprot(struct smbsrv_request *smb_req)
183 struct smb2srv_request *req;
184 uint32_t body_fixed_size = 0x26;
186 /* create a fake SMB2 negprot request */
187 req = talloc_zero(smb_req->smb_conn, struct smb2srv_request);
188 if (!req) goto nomem;
189 req->smb_conn = smb_req->smb_conn;
190 req->request_time = smb_req->request_time;
191 talloc_steal(req, smb_req);
193 req->in.size = NBT_HDR_SIZE+SMB2_HDR_BODY+body_fixed_size;
194 req->in.allocated = req->in.size;
195 req->in.buffer = talloc_size(req, req->in.allocated);
196 if (!req->in.buffer) goto nomem;
197 req->in.hdr = req->in.buffer + NBT_HDR_SIZE;
198 req->in.body = req->in.hdr + SMB2_HDR_BODY;
199 req->in.body_size = body_fixed_size;
200 req->in.dynamic = NULL;
202 SIVAL(req->in.hdr, 0, SMB2_MAGIC);
203 SSVAL(req->in.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
204 SSVAL(req->in.hdr, SMB2_HDR_PAD1, 0);
205 SIVAL(req->in.hdr, SMB2_HDR_STATUS, 0);
206 SSVAL(req->in.hdr, SMB2_HDR_OPCODE, SMB2_OP_NEGPROT);
207 SSVAL(req->in.hdr, SMB2_HDR_UNKNOWN1,0);
208 SIVAL(req->in.hdr, SMB2_HDR_FLAGS, 0);
209 SIVAL(req->in.hdr, SMB2_HDR_UNKNOWN2,0);
210 SBVAL(req->in.hdr, SMB2_HDR_SEQNUM, 0);
211 SIVAL(req->in.hdr, SMB2_HDR_PID, 0);
212 SIVAL(req->in.hdr, SMB2_HDR_TID, 0);
213 SBVAL(req->in.hdr, SMB2_HDR_UID, 0);
214 memset(req->in.hdr+SMB2_HDR_SIG, 0, 16);
216 /* this seems to be a bug, they use 0x24 but the length is 0x26 */
217 SSVAL(req->in.body, 0x00, 0x24);
219 SSVAL(req->in.body, 0x02, 1);
220 memset(req->in.body+0x04, 0, 32);
221 SSVAL(req->in.body, 0x24, 0);
223 smb2srv_negprot_recv(req);
224 return;
225 nomem:
226 smbsrv_terminate_connection(smb_req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
227 talloc_free(req);
228 return;