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/auth.h"
24 #include "auth/gensec/gensec.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/raw/raw_proto.h"
27 #include "libcli/smb2/smb2.h"
28 #include "libcli/smb2/smb2_calls.h"
29 #include "smb_server/smb_server.h"
30 #include "smb_server/smb2/smb2_server.h"
31 #include "smbd/service_stream.h"
32 #include "param/param.h"
34 static NTSTATUS
smb2srv_negprot_secblob(struct smb2srv_request
*req
, DATA_BLOB
*_blob
)
36 struct gensec_security
*gensec_security
;
37 DATA_BLOB null_data_blob
= data_blob(NULL
, 0);
40 struct cli_credentials
*server_credentials
;
42 server_credentials
= cli_credentials_init(req
);
43 if (!server_credentials
) {
44 smbsrv_terminate_connection(req
->smb_conn
, "Failed to init server credentials\n");
45 return NT_STATUS_NO_MEMORY
;
48 cli_credentials_set_conf(server_credentials
, req
->smb_conn
->lp_ctx
);
49 nt_status
= cli_credentials_set_machine_account(server_credentials
, req
->smb_conn
->lp_ctx
);
50 if (!NT_STATUS_IS_OK(nt_status
)) {
51 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status
)));
52 talloc_free(server_credentials
);
53 server_credentials
= NULL
;
56 req
->smb_conn
->negotiate
.server_credentials
= talloc_steal(req
->smb_conn
, server_credentials
);
58 nt_status
= samba_server_gensec_start(req
,
59 req
->smb_conn
->connection
->event
.ctx
,
60 req
->smb_conn
->connection
->msg_ctx
,
61 req
->smb_conn
->lp_ctx
,
65 if (!NT_STATUS_IS_OK(nt_status
)) {
66 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status
)));
67 smbsrv_terminate_connection(req
->smb_conn
, "Failed to start GENSEC\n");
71 gensec_set_target_service(gensec_security
, "cifs");
73 gensec_set_credentials(gensec_security
, server_credentials
);
75 nt_status
= gensec_start_mech_by_oid(gensec_security
, GENSEC_OID_SPNEGO
);
76 if (!NT_STATUS_IS_OK(nt_status
)) {
77 DEBUG(0, ("Failed to start SPNEGO: %s\n", nt_errstr(nt_status
)));
78 smbsrv_terminate_connection(req
->smb_conn
, "Failed to start SPNEGO\n");
82 nt_status
= gensec_update_ev(gensec_security
, req
, req
->smb_conn
->connection
->event
.ctx
, null_data_blob
, &blob
);
83 if (!NT_STATUS_IS_OK(nt_status
) && !NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
84 DEBUG(0, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status
)));
85 smbsrv_terminate_connection(req
->smb_conn
, "Failed to start SPNEGO - no first token\n");
93 static NTSTATUS
smb2srv_negprot_backend(struct smb2srv_request
*req
, struct smb2_negprot
*io
)
96 struct timeval current_time
;
97 struct timeval boot_time
;
100 enum smb_signing_setting signing_setting
;
101 struct loadparm_context
*lp_ctx
= req
->smb_conn
->lp_ctx
;
103 /* we only do one dialect for now */
104 if (io
->in
.dialect_count
< 1) {
105 return NT_STATUS_NOT_SUPPORTED
;
107 for (i
=0; i
< io
->in
.dialect_count
; i
++) {
108 dialect
= io
->in
.dialects
[i
];
109 if (dialect
== SMB2_DIALECT_REVISION_202
) {
113 if (dialect
!= SMB2_DIALECT_REVISION_202
) {
114 DEBUG(0,("Got unexpected SMB2 dialect %u\n", dialect
));
115 return NT_STATUS_NOT_SUPPORTED
;
118 req
->smb_conn
->negotiate
.protocol
= PROTOCOL_SMB2_02
;
120 current_time
= timeval_current(); /* TODO: handle timezone?! */
121 boot_time
= timeval_current(); /* TODO: fix me */
123 ZERO_STRUCT(io
->out
);
125 signing_setting
= lpcfg_server_signing(lp_ctx
);
126 if (signing_setting
== SMB_SIGNING_DEFAULT
) {
128 * If we are a domain controller, SMB signing is
129 * really important, as it can prevent a number of
130 * attacks on communications between us and the
133 * However, it really sucks (no sendfile, CPU
134 * overhead) performance-wise when used on a
135 * file server, so disable it by default
139 if (lpcfg_server_role(lp_ctx
) >= ROLE_ACTIVE_DIRECTORY_DC
) {
140 signing_setting
= SMB_SIGNING_REQUIRED
;
142 signing_setting
= SMB_SIGNING_OFF
;
146 switch (signing_setting
) {
147 case SMB_SIGNING_DEFAULT
:
148 smb_panic(__location__
);
150 case SMB_SIGNING_OFF
:
151 io
->out
.security_mode
= 0;
153 case SMB_SIGNING_DESIRED
:
154 case SMB_SIGNING_IF_REQUIRED
:
155 io
->out
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
157 case SMB_SIGNING_REQUIRED
:
158 io
->out
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
;
159 /* force signing on immediately */
160 req
->smb_conn
->smb2_signing_required
= true;
163 io
->out
.dialect_revision
= dialect
;
164 io
->out
.capabilities
= 0;
165 io
->out
.max_transact_size
= lpcfg_parm_ulong(req
->smb_conn
->lp_ctx
, NULL
,
166 "smb2", "max transaction size", 0x10000);
167 io
->out
.max_read_size
= lpcfg_parm_ulong(req
->smb_conn
->lp_ctx
, NULL
,
168 "smb2", "max read size", 0x10000);
169 io
->out
.max_write_size
= lpcfg_parm_ulong(req
->smb_conn
->lp_ctx
, NULL
,
170 "smb2", "max write size", 0x10000);
171 io
->out
.system_time
= timeval_to_nttime(¤t_time
);
172 io
->out
.server_start_time
= timeval_to_nttime(&boot_time
);
173 io
->out
.reserved2
= 0;
174 status
= smb2srv_negprot_secblob(req
, &io
->out
.secblob
);
175 NT_STATUS_NOT_OK_RETURN(status
);
180 static void smb2srv_negprot_send(struct smb2srv_request
*req
, struct smb2_negprot
*io
)
184 if (NT_STATUS_IS_ERR(req
->status
)) {
185 smb2srv_send_error(req
, req
->status
); /* TODO: is this correct? */
189 status
= smb2srv_setup_reply(req
, 0x40, true, io
->out
.secblob
.length
);
190 if (!NT_STATUS_IS_OK(status
)) {
191 smbsrv_terminate_connection(req
->smb_conn
, nt_errstr(status
));
196 SSVAL(req
->out
.body
, 0x02, io
->out
.security_mode
);
197 SIVAL(req
->out
.body
, 0x04, io
->out
.dialect_revision
);
198 SIVAL(req
->out
.body
, 0x06, io
->out
.reserved
);
199 status
= smbcli_push_guid(req
->out
.body
, 0x08, &io
->out
.server_guid
);
200 if (!NT_STATUS_IS_OK(status
)) {
201 smbsrv_terminate_connection(req
->smb_conn
, nt_errstr(status
));
205 SIVAL(req
->out
.body
, 0x18, io
->out
.capabilities
);
206 SIVAL(req
->out
.body
, 0x1C, io
->out
.max_transact_size
);
207 SIVAL(req
->out
.body
, 0x20, io
->out
.max_read_size
);
208 SIVAL(req
->out
.body
, 0x24, io
->out
.max_write_size
);
209 push_nttime(req
->out
.body
, 0x28, io
->out
.system_time
);
210 push_nttime(req
->out
.body
, 0x30, io
->out
.server_start_time
);
211 SIVAL(req
->out
.body
, 0x3C, io
->out
.reserved2
);
212 status
= smb2_push_o16s16_blob(&req
->out
, 0x38, io
->out
.secblob
);
213 if (!NT_STATUS_IS_OK(status
)) {
214 smbsrv_terminate_connection(req
->smb_conn
, nt_errstr(status
));
219 smb2srv_send_reply(req
);
222 void smb2srv_negprot_recv(struct smb2srv_request
*req
)
224 struct smb2_negprot
*io
;
227 if (req
->in
.body_size
< 0x26) {
228 smbsrv_terminate_connection(req
->smb_conn
, "Bad body size in SMB2 negprot");
232 io
= talloc(req
, struct smb2_negprot
);
234 smbsrv_terminate_connection(req
->smb_conn
, nt_errstr(NT_STATUS_NO_MEMORY
));
239 io
->in
.dialect_count
= SVAL(req
->in
.body
, 0x02);
240 io
->in
.security_mode
= SVAL(req
->in
.body
, 0x04);
241 io
->in
.reserved
= SVAL(req
->in
.body
, 0x06);
242 io
->in
.capabilities
= IVAL(req
->in
.body
, 0x08);
243 req
->status
= smbcli_pull_guid(req
->in
.body
, 0xC, &io
->in
.client_guid
);
244 if (!NT_STATUS_IS_OK(req
->status
)) {
245 smbsrv_terminate_connection(req
->smb_conn
, "Bad GUID in SMB2 negprot");
249 io
->in
.start_time
= smbcli_pull_nttime(req
->in
.body
, 0x1C);
251 io
->in
.dialects
= talloc_array(req
, uint16_t, io
->in
.dialect_count
);
252 if (io
->in
.dialects
== NULL
) {
253 smbsrv_terminate_connection(req
->smb_conn
, nt_errstr(NT_STATUS_NO_MEMORY
));
257 for (i
=0;i
<io
->in
.dialect_count
;i
++) {
258 io
->in
.dialects
[i
] = SVAL(req
->in
.body
, 0x24+i
*2);
261 req
->status
= smb2srv_negprot_backend(req
, io
);
263 if (req
->control_flags
& SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY
) {
267 smb2srv_negprot_send(req
, io
);
271 * reply to a SMB negprot request with dialect "SMB 2.002"
273 void smb2srv_reply_smb_negprot(struct smbsrv_request
*smb_req
)
275 struct smb2srv_request
*req
;
276 uint32_t body_fixed_size
= 0x26;
278 req
= talloc_zero(smb_req
->smb_conn
, struct smb2srv_request
);
279 if (!req
) goto nomem
;
280 req
->smb_conn
= smb_req
->smb_conn
;
281 req
->request_time
= smb_req
->request_time
;
282 talloc_steal(req
, smb_req
);
284 req
->in
.size
= NBT_HDR_SIZE
+SMB2_HDR_BODY
+body_fixed_size
;
285 req
->in
.allocated
= req
->in
.size
;
286 req
->in
.buffer
= talloc_array(req
, uint8_t, req
->in
.allocated
);
287 if (!req
->in
.buffer
) goto nomem
;
288 req
->in
.hdr
= req
->in
.buffer
+ NBT_HDR_SIZE
;
289 req
->in
.body
= req
->in
.hdr
+ SMB2_HDR_BODY
;
290 req
->in
.body_size
= body_fixed_size
;
291 req
->in
.dynamic
= NULL
;
293 smb2srv_setup_bufinfo(req
);
295 SIVAL(req
->in
.hdr
, 0, SMB2_MAGIC
);
296 SSVAL(req
->in
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
297 SSVAL(req
->in
.hdr
, SMB2_HDR_EPOCH
, 0);
298 SIVAL(req
->in
.hdr
, SMB2_HDR_STATUS
, 0);
299 SSVAL(req
->in
.hdr
, SMB2_HDR_OPCODE
, SMB2_OP_NEGPROT
);
300 SSVAL(req
->in
.hdr
, SMB2_HDR_CREDIT
, 0);
301 SIVAL(req
->in
.hdr
, SMB2_HDR_FLAGS
, 0);
302 SIVAL(req
->in
.hdr
, SMB2_HDR_NEXT_COMMAND
, 0);
303 SBVAL(req
->in
.hdr
, SMB2_HDR_MESSAGE_ID
, 0);
304 SIVAL(req
->in
.hdr
, SMB2_HDR_PID
, 0);
305 SIVAL(req
->in
.hdr
, SMB2_HDR_TID
, 0);
306 SBVAL(req
->in
.hdr
, SMB2_HDR_SESSION_ID
, 0);
307 memset(req
->in
.hdr
+SMB2_HDR_SIGNATURE
, 0, 16);
309 /* this seems to be a bug, they use 0x24 but the length is 0x26 */
310 SSVAL(req
->in
.body
, 0x00, 0x24);
312 SSVAL(req
->in
.body
, 0x02, 1);
313 memset(req
->in
.body
+0x04, 0, 32);
314 SSVAL(req
->in
.body
, 0x24, SMB2_DIALECT_REVISION_202
);
316 smb2srv_negprot_recv(req
);
319 smbsrv_terminate_connection(smb_req
->smb_conn
, nt_errstr(NT_STATUS_NO_MEMORY
));