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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "auth/credentials/credentials.h"
22 #include "auth/gensec/gensec.h"
23 #include "auth/auth.h"
24 #include "smb_server/smb_server.h"
25 #include "libcli/smb2/smb2.h"
26 #include "smb_server/smb2/smb2_server.h"
27 #include "smb_server/service_smb_proto.h"
28 #include "smbd/service_stream.h"
29 #include "lib/stream/packet.h"
32 /* initialise the auth_context for this server and return the cryptkey */
33 static NTSTATUS get_challenge(struct smbsrv_connection *smb_conn, uint8_t buff[8])
36 const uint8_t *challenge;
38 /* muliple negprots are not premitted */
39 if (smb_conn->negotiate.auth_context) {
40 DEBUG(3,("get challenge: is this a secondary negprot? auth_context is non-NULL!\n"));
41 return NT_STATUS_FOOBAR;
44 DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
46 nt_status = auth_context_create(smb_conn,
47 smb_conn->connection->event.ctx,
48 smb_conn->connection->msg_ctx,
49 &smb_conn->negotiate.auth_context);
50 if (!NT_STATUS_IS_OK(nt_status)) {
51 DEBUG(0, ("auth_context_create() returned %s", nt_errstr(nt_status)));
55 nt_status = auth_get_challenge(smb_conn->negotiate.auth_context, &challenge);
56 if (!NT_STATUS_IS_OK(nt_status)) {
57 DEBUG(0, ("auth_get_challenge() returned %s", nt_errstr(nt_status)));
61 memcpy(buff, challenge, 8);
66 /****************************************************************************
67 Reply for the core protocol.
68 ****************************************************************************/
69 static void reply_corep(struct smbsrv_request *req, uint16_t choice)
71 smbsrv_setup_reply(req, 1, 0);
73 SSVAL(req->out.vwv, VWV(0), choice);
75 req->smb_conn->negotiate.protocol = PROTOCOL_CORE;
77 if (req->smb_conn->signing.mandatory_signing) {
78 smbsrv_terminate_connection(req->smb_conn,
79 "CORE does not support SMB signing, and it is mandatory\n");
83 smbsrv_send_reply(req);
86 /****************************************************************************
87 Reply for the coreplus protocol.
88 this is quite incomplete - we only fill in a small part of the reply, but as nobody uses
89 this any more it probably doesn't matter
90 ****************************************************************************/
91 static void reply_coreplus(struct smbsrv_request *req, uint16_t choice)
93 uint16_t raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
95 smbsrv_setup_reply(req, 13, 0);
97 /* Reply, SMBlockread, SMBwritelock supported. */
98 SCVAL(req->out.hdr,HDR_FLG,
99 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
101 SSVAL(req->out.vwv, VWV(0), choice);
102 SSVAL(req->out.vwv, VWV(1), 0x1); /* user level security, don't encrypt */
104 /* tell redirector we support
105 readbraw and writebraw (possibly) */
106 SSVAL(req->out.vwv, VWV(5), raw);
108 req->smb_conn->negotiate.protocol = PROTOCOL_COREPLUS;
110 if (req->smb_conn->signing.mandatory_signing) {
111 smbsrv_terminate_connection(req->smb_conn,
112 "COREPLUS does not support SMB signing, and it is mandatory\n");
116 smbsrv_send_reply(req);
119 /****************************************************************************
120 Reply for the lanman 1.0 protocol.
121 ****************************************************************************/
122 static void reply_lanman1(struct smbsrv_request *req, uint16_t choice)
124 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
126 time_t t = req->request_time.tv_sec;
128 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
130 if (lp_security() != SEC_SHARE)
131 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
133 if (req->smb_conn->negotiate.encrypted_passwords)
134 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
136 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN1;
138 smbsrv_setup_reply(req, 13, req->smb_conn->negotiate.encrypted_passwords ? 8 : 0);
140 /* SMBlockread, SMBwritelock supported. */
141 SCVAL(req->out.hdr,HDR_FLG,
142 CVAL(req->out.hdr, HDR_FLG) | FLAG_SUPPORT_LOCKREAD);
144 SSVAL(req->out.vwv, VWV(0), choice);
145 SSVAL(req->out.vwv, VWV(1), secword);
146 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
147 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
148 SSVAL(req->out.vwv, VWV(4), 1);
149 SSVAL(req->out.vwv, VWV(5), raw);
150 SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id.id);
151 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
152 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
153 SIVAL(req->out.vwv, VWV(11), 0); /* reserved */
155 /* Create a token value and add it to the outgoing packet. */
156 if (req->smb_conn->negotiate.encrypted_passwords) {
159 SSVAL(req->out.vwv, VWV(11), 8);
161 nt_status = get_challenge(req->smb_conn, req->out.data);
162 if (!NT_STATUS_IS_OK(nt_status)) {
163 smbsrv_terminate_connection(req->smb_conn, "LANMAN1 get_challenge failed\n");
168 if (req->smb_conn->signing.mandatory_signing) {
169 smbsrv_terminate_connection(req->smb_conn,
170 "LANMAN1 does not support SMB signing, and it is mandatory\n");
174 smbsrv_send_reply(req);
177 /****************************************************************************
178 Reply for the lanman 2.0 protocol.
179 ****************************************************************************/
180 static void reply_lanman2(struct smbsrv_request *req, uint16_t choice)
182 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
184 time_t t = req->request_time.tv_sec;
186 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
188 if (lp_security() != SEC_SHARE)
189 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
191 if (req->smb_conn->negotiate.encrypted_passwords)
192 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
194 req->smb_conn->negotiate.protocol = PROTOCOL_LANMAN2;
196 smbsrv_setup_reply(req, 13, 0);
198 SSVAL(req->out.vwv, VWV(0), choice);
199 SSVAL(req->out.vwv, VWV(1), secword);
200 SSVAL(req->out.vwv, VWV(2), req->smb_conn->negotiate.max_recv);
201 SSVAL(req->out.vwv, VWV(3), lp_maxmux());
202 SSVAL(req->out.vwv, VWV(4), 1);
203 SSVAL(req->out.vwv, VWV(5), raw);
204 SIVAL(req->out.vwv, VWV(6), req->smb_conn->connection->server_id.id);
205 srv_push_dos_date(req->smb_conn, req->out.vwv, VWV(8), t);
206 SSVAL(req->out.vwv, VWV(10), req->smb_conn->negotiate.zone_offset/60);
207 SIVAL(req->out.vwv, VWV(11), 0);
209 /* Create a token value and add it to the outgoing packet. */
210 if (req->smb_conn->negotiate.encrypted_passwords) {
211 SSVAL(req->out.vwv, VWV(11), 8);
212 req_grow_data(req, 8);
213 get_challenge(req->smb_conn, req->out.data);
216 req_push_str(req, NULL, lp_workgroup(), -1, STR_TERMINATE);
218 if (req->smb_conn->signing.mandatory_signing) {
219 smbsrv_terminate_connection(req->smb_conn,
220 "LANMAN2 does not support SMB signing, and it is mandatory\n");
224 smbsrv_send_reply(req);
227 static void reply_nt1_orig(struct smbsrv_request *req)
229 /* Create a token value and add it to the outgoing packet. */
230 if (req->smb_conn->negotiate.encrypted_passwords) {
231 req_grow_data(req, 8);
232 /* note that we do not send a challenge at all if
233 we are using plaintext */
234 get_challenge(req->smb_conn, req->out.ptr);
236 SCVAL(req->out.vwv+1, VWV(16), 8);
238 req_push_str(req, NULL, lp_workgroup(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
239 req_push_str(req, NULL, lp_netbios_name(), -1, STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
240 DEBUG(3,("not using extended security (SPNEGO or NTLMSSP)\n"));
243 /****************************************************************************
244 Reply for the nt protocol.
245 ****************************************************************************/
246 static void reply_nt1(struct smbsrv_request *req, uint16_t choice)
248 /* dual names + lock_and_read + nt SMBs + remote API calls */
251 time_t t = req->request_time.tv_sec;
253 BOOL negotiate_spnego = False;
254 char *large_test_path;
256 unix_to_nt_time(&nttime, t);
259 CAP_NT_FIND | CAP_LOCK_AND_READ |
260 CAP_LEVEL_II_OPLOCKS | CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
262 req->smb_conn->negotiate.encrypted_passwords = lp_encrypted_passwords();
264 /* do spnego in user level security if the client
265 supports it and we can do encrypted passwords */
267 if (req->smb_conn->negotiate.encrypted_passwords &&
268 (lp_security() != SEC_SHARE) &&
270 (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
271 negotiate_spnego = True;
272 capabilities |= CAP_EXTENDED_SECURITY;
275 if (lp_unix_extensions()) {
276 capabilities |= CAP_UNIX;
279 if (lp_large_readwrite()) {
280 capabilities |= CAP_LARGE_READX | CAP_LARGE_WRITEX | CAP_W2K_SMBS;
283 large_test_path = lock_path(req, "large_test.dat");
284 if (large_file_support(large_test_path)) {
285 capabilities |= CAP_LARGE_FILES;
288 if (lp_readraw() && lp_writeraw()) {
289 capabilities |= CAP_RAW_MODE;
292 /* allow for disabling unicode */
294 capabilities |= CAP_UNICODE;
297 if (lp_nt_status_support()) {
298 capabilities |= CAP_STATUS32;
301 if (lp_host_msdfs()) {
302 capabilities |= CAP_DFS;
305 if (lp_security() != SEC_SHARE) {
306 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
309 if (req->smb_conn->negotiate.encrypted_passwords) {
310 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
313 if (req->smb_conn->signing.allow_smb_signing) {
314 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
317 if (req->smb_conn->signing.mandatory_signing) {
318 secword |= NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
321 req->smb_conn->negotiate.protocol = PROTOCOL_NT1;
323 smbsrv_setup_reply(req, 17, 0);
325 SSVAL(req->out.vwv, VWV(0), choice);
326 SCVAL(req->out.vwv, VWV(1), secword);
328 /* notice the strange +1 on vwv here? That's because
329 this is the one and only SMB packet that is malformed in
330 the specification - all the command words after the secword
331 are offset by 1 byte */
332 SSVAL(req->out.vwv+1, VWV(1), lp_maxmux());
333 SSVAL(req->out.vwv+1, VWV(2), 1); /* num vcs */
334 SIVAL(req->out.vwv+1, VWV(3), req->smb_conn->negotiate.max_recv);
335 SIVAL(req->out.vwv+1, VWV(5), 0x10000); /* raw size. full 64k */
336 SIVAL(req->out.vwv+1, VWV(7), req->smb_conn->connection->server_id.id); /* session key */
337 SIVAL(req->out.vwv+1, VWV(9), capabilities);
338 push_nttime(req->out.vwv+1, VWV(11), nttime);
339 SSVALS(req->out.vwv+1,VWV(15), req->smb_conn->negotiate.zone_offset/60);
341 if (!negotiate_spnego) {
344 struct cli_credentials *server_credentials;
345 struct gensec_security *gensec_security;
346 DATA_BLOB null_data_blob = data_blob(NULL, 0);
351 nt_status = gensec_server_start(req->smb_conn,
352 req->smb_conn->connection->event.ctx,
353 req->smb_conn->connection->msg_ctx,
355 if (!NT_STATUS_IS_OK(nt_status)) {
356 DEBUG(0, ("Failed to start GENSEC: %s\n", nt_errstr(nt_status)));
357 smbsrv_terminate_connection(req->smb_conn, "Failed to start GENSEC\n");
361 if (req->smb_conn->negotiate.auth_context) {
362 smbsrv_terminate_connection(req->smb_conn, "reply_nt1: is this a secondary negprot? auth_context is non-NULL!\n");
367 = cli_credentials_init(req);
368 if (!server_credentials) {
369 smbsrv_terminate_connection(req->smb_conn, "Failed to init server credentials\n");
373 cli_credentials_set_conf(server_credentials);
374 nt_status = cli_credentials_set_machine_account(server_credentials);
375 if (!NT_STATUS_IS_OK(nt_status)) {
376 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(nt_status)));
377 talloc_free(server_credentials);
378 server_credentials = NULL;
381 req->smb_conn->negotiate.server_credentials = talloc_steal(req->smb_conn, server_credentials);
383 gensec_set_target_service(gensec_security, "cifs");
385 gensec_set_credentials(gensec_security, server_credentials);
387 oid = GENSEC_OID_SPNEGO;
388 nt_status = gensec_start_mech_by_oid(gensec_security, oid);
390 if (NT_STATUS_IS_OK(nt_status)) {
391 /* Get and push the proposed OID list into the packets */
392 nt_status = gensec_update(gensec_security, req, null_data_blob, &blob);
394 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
395 DEBUG(1, ("Failed to get SPNEGO to give us the first token: %s\n", nt_errstr(nt_status)));
399 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
400 DEBUG(3,("using SPNEGO\n"));
402 DEBUG(5, ("Failed to start SPNEGO, falling back to NTLMSSP only: %s\n", nt_errstr(nt_status)));
403 oid = GENSEC_OID_NTLMSSP;
404 nt_status = gensec_start_mech_by_oid(gensec_security, oid);
406 if (!NT_STATUS_IS_OK(nt_status)) {
407 DEBUG(0, ("Failed to start SPNEGO as well as NTLMSSP fallback: %s\n", nt_errstr(nt_status)));
411 /* NTLMSSP is a client-first exchange */
412 blob = data_blob(NULL, 0);
413 DEBUG(3,("using raw-NTLMSSP\n"));
416 req->smb_conn->negotiate.oid = oid;
418 req_grow_data(req, blob.length + 16);
419 /* a NOT very random guid, perhaps we should get it
420 * from the credentials (kitchen sink...) */
421 memset(req->out.ptr, '\0', 16);
424 memcpy(req->out.ptr, blob.data, blob.length);
425 SCVAL(req->out.vwv+1, VWV(16), blob.length + 16);
426 req->out.ptr += blob.length;
429 smbsrv_send_reply_nosign(req);
432 /****************************************************************************
433 Reply for the SMB2 2.001 protocol
434 ****************************************************************************/
435 static void reply_smb2(struct smbsrv_request *req, uint16_t choice)
437 struct smbsrv_connection *smb_conn = req->smb_conn;
440 talloc_free(smb_conn->sessions.idtree_vuid);
441 ZERO_STRUCT(smb_conn->sessions);
442 talloc_free(smb_conn->smb_tcons.idtree_tid);
443 ZERO_STRUCT(smb_conn->smb_tcons);
444 ZERO_STRUCT(smb_conn->signing);
446 /* reply with a SMB2 packet */
447 status = smbsrv_init_smb2_connection(smb_conn);
448 if (!NT_STATUS_IS_OK(status)) {
449 smbsrv_terminate_connection(smb_conn, nt_errstr(status));
453 packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
454 smb2srv_reply_smb_negprot(req);
458 /* List of supported protocols, most desired first */
459 static const struct {
460 const char *proto_name;
461 const char *short_name;
462 void (*proto_reply_fn)(struct smbsrv_request *req, uint16_t choice);
464 } supported_protocols[] = {
465 {"SMB 2.001", "SMB2", reply_smb2, PROTOCOL_SMB2},
466 {"NT LANMAN 1.0", "NT1", reply_nt1, PROTOCOL_NT1},
467 {"NT LM 0.12", "NT1", reply_nt1, PROTOCOL_NT1},
468 {"LANMAN2.1", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
469 {"LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
470 {"Samba", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
471 {"DOS LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
472 {"Windows for Workgroups 3.1a", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
473 {"LANMAN1.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
474 {"MICROSOFT NETWORKS 3.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
475 {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
476 {"PC NETWORK PROGRAM 1.0", "CORE", reply_corep, PROTOCOL_CORE},
480 /****************************************************************************
482 ****************************************************************************/
484 void smbsrv_reply_negprot(struct smbsrv_request *req)
488 uint32_t protos_count = 0;
489 char **protos = NULL;
491 if (req->smb_conn->negotiate.done_negprot) {
492 smbsrv_terminate_connection(req->smb_conn, "multiple negprot's are not permitted");
495 req->smb_conn->negotiate.done_negprot = True;
501 protos = talloc_realloc(req, protos, char *, protos_count + 1);
503 smbsrv_terminate_connection(req->smb_conn, nt_errstr(NT_STATUS_NO_MEMORY));
506 protos[protos_count] = NULL;
507 len = req_pull_ascii4(req, (const char **)&protos[protos_count], p, STR_ASCII|STR_TERMINATE);
509 if (len == 0 || !protos[protos_count]) break;
511 DEBUG(5,("Requested protocol [%d][%s]\n", protos_count, protos[protos_count]));
515 /* Check for protocols, most desirable first */
516 for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
519 if (supported_protocols[protocol].protocol_level > lp_srv_maxprotocol()) continue;
520 if (supported_protocols[protocol].protocol_level < lp_srv_minprotocol()) continue;
522 for (i = 0; i < protos_count; i++) {
523 if (strcmp(supported_protocols[protocol].proto_name, protos[i]) != 0) continue;
525 supported_protocols[protocol].proto_reply_fn(req, i);
526 DEBUG(3,("Selected protocol [%d][%s]\n",
527 i, supported_protocols[protocol].proto_name));
532 smbsrv_terminate_connection(req->smb_conn, "No protocol supported !");