2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "auth/auth.h"
23 #include "smb_server/smb_server.h"
24 #include "libcli/smb2/smb2.h"
25 #include "smb_server/smb2/smb2_server.h"
26 #include "smb_server/service_smb_proto.h"
27 #include "smbd/service_stream.h"
28 #include "lib/stream/packet.h"
31 /* initialise the auth_context for this server and return the cryptkey */
32 static NTSTATUS get_challenge(struct smbsrv_connection *smb_conn, uint8_t buff[8])
35 const uint8_t *challenge;
37 /* muliple negprots are not premitted */
38 if (smb_conn->negotiate.auth_context) {
39 DEBUG(3,("get challenge: is this a secondary negprot? auth_context is non-NULL!\n"));
40 return NT_STATUS_FOOBAR;
43 DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
45 nt_status = auth_context_create(smb_conn, lp_auth_methods(),
46 smb_conn->connection->event.ctx,
47 smb_conn->connection->msg_ctx,
48 &smb_conn->negotiate.auth_context);
49 if (!NT_STATUS_IS_OK(nt_status)) {
50 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
54 nt_status = auth_get_challenge(smb_conn->negotiate.auth_context, &challenge);
55 if (!NT_STATUS_IS_OK(nt_status)) {
56 DEBUG(0, ("auth_get_challenge() returned %s", nt_errstr(nt_status)));
60 memcpy(buff, challenge, 8);
65 /****************************************************************************
66 Reply for the core protocol.
67 ****************************************************************************/
68 static void reply_corep(struct smbsrv_request *req, uint16_t choice)
70 smbsrv_setup_reply(req, 1, 0);
72 SSVAL(req->out.vwv, VWV(0), choice);
74 req->smb_conn->negotiate.protocol = PROTOCOL_CORE;
76 if (req->smb_conn->signing.mandatory_signing) {
77 smbsrv_terminate_connection(req->smb_conn,
78 "CORE does not support SMB signing, and it is mandatory\n");
82 smbsrv_send_reply(req);
85 /****************************************************************************
86 Reply for the coreplus protocol.
87 this is quite incomplete - we only fill in a small part of the reply, but as nobody uses
88 this any more it probably doesn't matter
89 ****************************************************************************/
90 static void reply_coreplus(struct smbsrv_request *req, uint16_t choice)
92 uint16_t raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
94 smbsrv_setup_reply(req, 13, 0);
96 /* Reply, SMBlockread, SMBwritelock supported. */
97 SCVAL(req->out.hdr,HDR_FLG,
98 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
100 SSVAL(req->out.vwv, VWV(0), choice);
101 SSVAL(req->out.vwv, VWV(1), 0x1); /* user level security, don't encrypt */
103 /* tell redirector we support
104 readbraw and writebraw (possibly) */
105 SSVAL(req->out.vwv, VWV(5), raw);
107 req->smb_conn->negotiate.protocol = PROTOCOL_COREPLUS;
109 if (req->smb_conn->signing.mandatory_signing) {
110 smbsrv_terminate_connection(req->smb_conn,
111 "COREPLUS does not support SMB signing, and it is mandatory\n");
115 smbsrv_send_reply(req);
118 /****************************************************************************
119 Reply for the lanman 1.0 protocol.
120 ****************************************************************************/
121 static void reply_lanman1(struct smbsrv_request *req, uint16_t choice)
123 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
125 time_t t = req->request_time.tv_sec;
127 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
129 if (lp_security() != SEC_SHARE)
130 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
132 if (req->smb_conn->negotiate.encrypted_passwords)
133 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
135 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN1;
137 smbsrv_setup_reply(req, 13, req->smb_conn->negotiate.encrypted_passwords ? 8 : 0);
139 /* SMBlockread, SMBwritelock supported. */
140 SCVAL(req->out.hdr,HDR_FLG,
141 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
143 SSVAL(req->out.vwv, VWV(0), choice);
144 SSVAL(req->out.vwv, VWV(1), secword);
145 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
146 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
147 SSVAL(req->out.vwv, VWV(4), 1);
148 SSVAL(req->out.vwv, VWV(5), raw);
149 SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id);
150 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
151 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
152 SIVAL(req->out.vwv, VWV(11), 0); /* reserved */
154 /* Create a token value and add it to the outgoing packet. */
155 if (req->smb_conn->negotiate.encrypted_passwords) {
158 SSVAL(req->out.vwv, VWV(11), 8);
160 nt_status = get_challenge(req->smb_conn, req->out.data);
161 if (!NT_STATUS_IS_OK(nt_status)) {
162 smbsrv_terminate_connection(req->smb_conn, "LANMAN1 get_challenge failed\n");
167 if (req->smb_conn->signing.mandatory_signing) {
168 smbsrv_terminate_connection(req->smb_conn,
169 "LANMAN1 does not support SMB signing, and it is mandatory\n");
173 smbsrv_send_reply(req);
176 /****************************************************************************
177 Reply for the lanman 2.0 protocol.
178 ****************************************************************************/
179 static void reply_lanman2(struct smbsrv_request *req, uint16_t choice)
181 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
183 time_t t = req->request_time.tv_sec;
185 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
187 if (lp_security() != SEC_SHARE)
188 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
190 if (req->smb_conn->negotiate.encrypted_passwords)
191 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
193 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN2;
195 smbsrv_setup_reply(req, 13, 0);
197 SSVAL(req->out.vwv, VWV(0), choice);
198 SSVAL(req->out.vwv, VWV(1), secword);
199 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
200 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
201 SSVAL(req->out.vwv, VWV(4), 1);
202 SSVAL(req->out.vwv, VWV(5), raw);
203 SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id);
204 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
205 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
206 SIVAL(req->out.vwv, VWV(11), 0);
208 /* Create a token value and add it to the outgoing packet. */
209 if (req->smb_conn->negotiate.encrypted_passwords) {
210 SSVAL(req->out.vwv, VWV(11), 8);
211 req_grow_data(req, 8);
212 get_challenge(req->smb_conn, req->out.data);
215 req_push_str(req, NULL, lp_workgroup(), -1, STR_TERMINATE);
217 if (req->smb_conn->signing.mandatory_signing) {
218 smbsrv_terminate_connection(req->smb_conn,
219 "LANMAN2 does not support SMB signing, and it is mandatory\n");
223 smbsrv_send_reply(req);
226 static void reply_nt1_orig(struct smbsrv_request *req)
228 /* Create a token value and add it to the outgoing packet. */
229 if (req->smb_conn->negotiate.encrypted_passwords) {
230 req_grow_data(req, 8);
231 /* note that we do not send a challenge at all if
232 we are using plaintext */
233 get_challenge(req->smb_conn, req->out.ptr);
235 SCVAL(req->out.vwv+1, VWV(16), 8);
237 req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
238 req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
239 DEBUG(3,("not using extended security (SPNEGO or NTLMSSP)\n"));
242 /****************************************************************************
243 Reply for the nt protocol.
244 ****************************************************************************/
245 static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
247 /* dual names + lock_and_read + nt SMBs + remote API calls */
250 time_t t = req->request_time.tv_sec;
252 BOOL negotiate_spnego = False;
254 unix_to_nt_time(&nttime, t);
257 CAP_NT_FIND | CAP_LOCK_AND_READ |
258 CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
260 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
262 /* do spnego in user level security if the client
263 supports it and we can do encrypted passwords */
265 if (req->smb_conn->negotiate.encrypted_passwords &&
266 (lp_security() != SEC_SHARE) &&
268 (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
269 negotiate_spnego = True;
270 capabilities |= CAP_EXTENDED_SECURITY;
273 if (lp_unix_extensions()) {
274 capabilities |= CAP_UNIX;
277 if (lp_large_readwrite()) {
278 capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
281 capabilities |= CAP_LARGE_FILES;
283 if (lp_readraw() && lp_writeraw()) {
284 capabilities |= CAP_RAW_MODE;
287 /* allow for disabling unicode */
289 capabilities |= CAP_UNICODE;
292 if (lp_nt_status_support()) {
293 capabilities |= CAP_STATUS32;
296 if (lp_host_msdfs()) {
297 capabilities |= CAP_DFS;
300 if (lp_security() != SEC_SHARE) {
301 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
304 if (req->smb_conn->negotiate.encrypted_passwords) {
305 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
308 if (req->smb_conn->signing.allow_smb_signing) {
309 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
312 if (req->smb_conn->signing.mandatory_signing) {
313 secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
316 req->smb_conn->negotiate.protocol = PROTOCOL_NT1;
318 smbsrv_setup_reply(req, 17, 0);
320 SSVAL(req->out.vwv, VWV(0), choice);
321 SCVAL(req->out.vwv, VWV(1), secword);
323 /* notice the strange +1 on vwv here? That's because
324 this is the one and only SMB packet that is malformed in
325 the specification - all the command words after the secword
326 are offset by 1 byte */
327 SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
328 SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
329 SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
330 SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
331 SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->connection->server_id); /* session key */
332 SIVAL(req->out.vwv+1, VWV(9), capabilities);
333 push_nttime(req->out.vwv+1, VWV(11), nttime);
334 SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
336 if (!negotiate_spnego) {
339 struct cli_credentials *server_credentials;
340 struct gensec_security *gensec_security;
341 DATA_BLOB null_data_blob = data_blob(NULL, 0);
346 nt_status = gensec_server_start(req->smb_conn,
347 req->smb_conn->connection->event.ctx,
348 req->smb_conn->connection->msg_ctx,
350 if (!NT_STATUS_IS_OK(nt_status)) {
351 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
352 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
356 if (req->smb_conn->negotiate.auth_context) {
357 smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot? auth_context is non-NULL!\n");
362 = cli_credentials_init(req);
363 if (!server_credentials) {
364 smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
368 cli_credentials_set_conf(server_credentials);
369 nt_status = cli_credentials_set_machine_account(server_credentials);
370 if (!NT_STATUS_IS_OK(nt_status)) {
371 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
372 talloc_free(server_credentials);
373 server_credentials = NULL;
376 req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
378 gensec_set_target_service(gensec_security, "cifs");
380 gensec_set_credentials(gensec_security, server_credentials);
382 oid = GENSEC_OID_SPNEGO;
383 nt_status = gensec_start_mech_by_oid(gensec_security, oid);
385 if (NT_STATUS_IS_OK(nt_status)) {
386 /* Get and push the proposed OID list into the packets */
387 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
389 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
390 DEBUG(1, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
394 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
395 DEBUG(3,("using SPNEGO\n"));
397 DEBUG(5, ("Failed to start SPNEGO, falling back to NTLMSSP only: %s\n", nt_errstr(nt_status)));
398 oid = GENSEC_OID_NTLMSSP;
399 nt_status = gensec_start_mech_by_oid(gensec_security, oid);
401 if (!NT_STATUS_IS_OK(nt_status)) {
402 DEBUG(0, ("Failed to start SPNEGO as well as NTLMSSP fallback: %s\n", nt_errstr(nt_status)));
406 /* NTLMSSP is a client-first exchange */
407 blob = data_blob(NULL, 0);
408 DEBUG(3,("using raw-NTLMSSP\n"));
411 req->smb_conn->negotiate.oid = oid;
413 req_grow_data(req, blob.length + 16);
414 /* a NOT very random guid, perhaps we should get it
415 * from the credentials (kitchen sink...) */
416 memset(req->out.ptr, '\0', 16);
419 memcpy(req->out.ptr, blob.data, blob.length);
420 SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
421 req->out.ptr += blob.length;
424 smbsrv_send_reply_nosign(req);
427 /****************************************************************************
428 Reply for the SMB2 2.001 protocol
429 ****************************************************************************/
430 static void reply_smb2(struct smbsrv_request *req, uint16_t choice)
432 struct smbsrv_connection *smb_conn = req->smb_conn;
434 /* reply with a SMB2 packet */
435 packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
436 smb2srv_reply_smb_negprot(req);
440 /* List of supported protocols, most desired first */
441 static const struct {
442 const char *proto_name;
443 const char *short_name;
444 void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
446 } supported_protocols[] = {
447 {"SMB 2.001", "SMB2", reply_smb2, PROTOCOL_SMB2},
448 {"NT LANMAN 1.0", "NT1", reply_nt1, PROTOCOL_NT1},
449 {"NT LM 0.12", "NT1", reply_nt1, PROTOCOL_NT1},
450 {"LANMAN2.1", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
451 {"LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
452 {"Samba", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
453 {"DOS LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
454 {"Windows for Workgroups 3.1a", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
455 {"LANMAN1.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
456 {"MICROSOFT NETWORKS 3.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
457 {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
458 {"PC NETWORK PROGRAM 1.0", "CORE", reply_corep, PROTOCOL_CORE},
462 /****************************************************************************
464 ****************************************************************************/
466 void smbsrv_reply_negprot(struct smbsrv_request *req)
470 uint32_t protos_count = 0;
471 char **protos = NULL;
473 if (req->smb_conn->negotiate.done_negprot) {
474 smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
477 req->smb_conn->negotiate.done_negprot = True;
483 protos = talloc_realloc(req, protos, char *, protos_count + 1);
485 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
488 protos[protos_count] = NULL;
489 len = req_pull_ascii4(req, (const char **)&protos[protos_count], p, STR_ASCII|STR_TERMINATE);
491 if (len == 0 || !protos[protos_count]) break;
493 DEBUG(5,("Requested protocol [%d][%s]\n", protos_count, protos[protos_count]));
497 /* Check for protocols, most desirable first */
498 for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
501 if (supported_protocols[protocol].protocol_level > lp_srv_maxprotocol()) continue;
502 if (supported_protocols[protocol].protocol_level < lp_srv_minprotocol()) continue;
504 for (i = 0; i < protos_count; i++) {
505 if (strcmp(supported_protocols[protocol].proto_name, protos[i]) != 0) continue;
507 supported_protocols[protocol].proto_reply_fn(req, i);
508 DEBUG(3,("Selected protocol [%d][%s]\n",
509 i, supported_protocols[protocol].proto_name));
514 smbsrv_terminate_connection(req->smb_conn, "No protocol supported !");