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/>.
22 extern fstring remote_proto;
23 extern enum protocol_types Protocol;
26 BOOL global_encrypted_passwords_negotiated = False;
27 BOOL global_spnego_negotiated = False;
28 struct auth_context *negprot_global_auth_context = NULL;
30 static void get_challenge(uint8 buff[8])
33 const uint8 *cryptkey;
35 /* We might be called more than once, multiple negprots are
37 if (negprot_global_auth_context) {
38 DEBUG(3, ("get challenge: is this a secondary negprot? negprot_global_auth_context is non-NULL!\n"));
39 (negprot_global_auth_context->free)(&negprot_global_auth_context);
42 DEBUG(10, ("get challenge: creating negprot_global_auth_context\n"));
43 if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&negprot_global_auth_context))) {
44 DEBUG(0, ("make_auth_context_subsystem returned %s", nt_errstr(nt_status)));
45 smb_panic("cannot make_negprot_global_auth_context!");
47 DEBUG(10, ("get challenge: getting challenge\n"));
48 cryptkey = negprot_global_auth_context->get_ntlm_challenge(negprot_global_auth_context);
49 memcpy(buff, cryptkey, 8);
52 /****************************************************************************
53 Reply for the core protocol.
54 ****************************************************************************/
56 static void reply_corep(struct smb_request *req, uint16 choice)
58 reply_outbuf(req, 1, 0);
59 SSVAL(req->outbuf, smb_vwv0, choice);
61 Protocol = PROTOCOL_CORE;
64 /****************************************************************************
65 Reply for the coreplus protocol.
66 ****************************************************************************/
68 static void reply_coreplus(struct smb_request *req, uint16 choice)
70 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
72 reply_outbuf(req, 13, 0);
74 SSVAL(req->outbuf,smb_vwv0,choice);
75 SSVAL(req->outbuf,smb_vwv5,raw); /* tell redirector we support
76 readbraw and writebraw (possibly) */
77 /* Reply, SMBlockread, SMBwritelock supported. */
78 SCVAL(req->outbuf,smb_flg,FLAG_REPLY|FLAG_SUPPORT_LOCKREAD);
79 SSVAL(req->outbuf,smb_vwv1,0x1); /* user level security, don't
81 Protocol = PROTOCOL_COREPLUS;
84 /****************************************************************************
85 Reply for the lanman 1.0 protocol.
86 ****************************************************************************/
88 static void reply_lanman1(struct smb_request *req, uint16 choice)
90 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
92 time_t t = time(NULL);
94 global_encrypted_passwords_negotiated = lp_encrypted_passwords();
96 if (lp_security()>=SEC_USER)
97 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
98 if (global_encrypted_passwords_negotiated)
99 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
101 reply_outbuf(req, 13, global_encrypted_passwords_negotiated?8:0);
103 SSVAL(req->outbuf,smb_vwv0,choice);
104 SSVAL(req->outbuf,smb_vwv1,secword);
105 /* Create a token value and add it to the outgoing packet. */
106 if (global_encrypted_passwords_negotiated) {
107 get_challenge((uint8 *)smb_buf(req->outbuf));
108 SSVAL(req->outbuf,smb_vwv11, 8);
111 Protocol = PROTOCOL_LANMAN1;
113 /* Reply, SMBlockread, SMBwritelock supported. */
114 SCVAL(req->outbuf,smb_flg,FLAG_REPLY|FLAG_SUPPORT_LOCKREAD);
115 SSVAL(req->outbuf,smb_vwv2,max_recv);
116 SSVAL(req->outbuf,smb_vwv3,lp_maxmux()); /* maxmux */
117 SSVAL(req->outbuf,smb_vwv4,1);
118 SSVAL(req->outbuf,smb_vwv5,raw); /* tell redirector we support
119 readbraw writebraw (possibly) */
120 SIVAL(req->outbuf,smb_vwv6,sys_getpid());
121 SSVAL(req->outbuf,smb_vwv10, set_server_zone_offset(t)/60);
123 srv_put_dos_date((char *)req->outbuf,smb_vwv8,t);
128 /****************************************************************************
129 Reply for the lanman 2.0 protocol.
130 ****************************************************************************/
132 static void reply_lanman2(struct smb_request *req, uint16 choice)
134 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
136 time_t t = time(NULL);
138 global_encrypted_passwords_negotiated = lp_encrypted_passwords();
140 if (lp_security()>=SEC_USER)
141 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
142 if (global_encrypted_passwords_negotiated)
143 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
145 reply_outbuf(req, 13, global_encrypted_passwords_negotiated?8:0);
147 SSVAL(req->outbuf,smb_vwv0,choice);
148 SSVAL(req->outbuf,smb_vwv1,secword);
149 SIVAL(req->outbuf,smb_vwv6,sys_getpid());
151 /* Create a token value and add it to the outgoing packet. */
152 if (global_encrypted_passwords_negotiated) {
153 get_challenge((uint8 *)smb_buf(req->outbuf));
154 SSVAL(req->outbuf,smb_vwv11, 8);
157 Protocol = PROTOCOL_LANMAN2;
159 /* Reply, SMBlockread, SMBwritelock supported. */
160 SCVAL(req->outbuf,smb_flg,FLAG_REPLY|FLAG_SUPPORT_LOCKREAD);
161 SSVAL(req->outbuf,smb_vwv2,max_recv);
162 SSVAL(req->outbuf,smb_vwv3,lp_maxmux());
163 SSVAL(req->outbuf,smb_vwv4,1);
164 SSVAL(req->outbuf,smb_vwv5,raw); /* readbraw and/or writebraw */
165 SSVAL(req->outbuf,smb_vwv10, set_server_zone_offset(t)/60);
166 srv_put_dos_date((char *)req->outbuf,smb_vwv8,t);
169 /****************************************************************************
170 Generate the spnego negprot reply blob. Return the number of bytes used.
171 ****************************************************************************/
173 static DATA_BLOB negprot_spnego(void)
182 const char *OIDs_krb5[] = {OID_KERBEROS5,
186 const char *OIDs_plain[] = {OID_NTLMSSP, NULL};
188 global_spnego_negotiated = True;
190 memset(guid, '\0', sizeof(guid));
192 safe_strcpy(unix_name, global_myname(), sizeof(unix_name)-1);
193 strlower_m(unix_name);
194 push_ascii_nstring(dos_name, unix_name);
195 safe_strcpy(guid, dos_name, sizeof(guid)-1);
198 /* Fix valgrind 'uninitialized bytes' issue. */
199 slen = strlen(dos_name);
200 if (slen < sizeof(guid)) {
201 memset(guid+slen, '\0', sizeof(guid) - slen);
205 /* strangely enough, NT does not sent the single OID NTLMSSP when
206 not a ADS member, it sends no OIDs at all
208 OLD COMMENT : "we can't do this until we teach our sesssion setup parser to know
209 about raw NTLMSSP (clients send no ASN.1 wrapping if we do this)"
211 Our sessionsetup code now handles raw NTLMSSP connects, so we can go
212 back to doing what W2K3 does here. This is needed to make PocketPC 2003
213 CIFS connections work with SPNEGO. See bugzilla bugs #1828 and #3133
218 if (lp_security() != SEC_ADS && !lp_use_kerberos_keytab()) {
220 /* Code for PocketPC client */
221 blob = data_blob(guid, 16);
223 /* Code for standalone WXP client */
224 blob = spnego_gen_negTokenInit(guid, OIDs_plain, "NONE");
228 char *host_princ_s = NULL;
229 name_to_fqdn(myname, global_myname());
231 asprintf(&host_princ_s, "cifs/%s@%s", myname, lp_realm());
232 if (host_princ_s == NULL) {
233 blob = data_blob_null;
236 blob = spnego_gen_negTokenInit(guid, OIDs_krb5, host_princ_s);
237 SAFE_FREE(host_princ_s);
243 /****************************************************************************
244 Reply for the nt protocol.
245 ****************************************************************************/
247 static void reply_nt1(struct smb_request *req, uint16 choice)
249 /* dual names + lock_and_read + nt SMBs + remote API calls */
250 int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ|
251 CAP_LEVEL_II_OPLOCKS;
255 BOOL negotiate_spnego = False;
256 time_t t = time(NULL);
259 global_encrypted_passwords_negotiated = lp_encrypted_passwords();
261 /* Check the flags field to see if this is Vista.
262 WinXP sets it and Vista does not. But we have to
263 distinguish from NT which doesn't set it either. */
265 if ( (req->flags2 & FLAGS2_EXTENDED_SECURITY) &&
266 ((req->flags2 & FLAGS2_UNKNOWN_BIT4) == 0) )
268 if (get_remote_arch() != RA_SAMBA) {
269 set_remote_arch( RA_VISTA );
273 reply_outbuf(req,17,0);
275 /* do spnego in user level security if the client
276 supports it and we can do encrypted passwords */
278 if (global_encrypted_passwords_negotiated &&
279 (lp_security() != SEC_SHARE) &&
281 (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
282 negotiate_spnego = True;
283 capabilities |= CAP_EXTENDED_SECURITY;
284 add_to_common_flags2(FLAGS2_EXTENDED_SECURITY);
285 /* Ensure FLAGS2_EXTENDED_SECURITY gets set in this reply
286 (already partially constructed. */
287 SSVAL(req->outbuf, smb_flg2,
288 req->flags2 | FLAGS2_EXTENDED_SECURITY);
291 capabilities |= CAP_NT_SMBS|CAP_RPC_REMOTE_APIS|CAP_UNICODE;
293 if (lp_unix_extensions()) {
294 capabilities |= CAP_UNIX;
297 if (lp_large_readwrite() && (SMB_OFF_T_BITS == 64))
298 capabilities |= CAP_LARGE_READX|CAP_LARGE_WRITEX|CAP_W2K_SMBS;
300 if (SMB_OFF_T_BITS == 64)
301 capabilities |= CAP_LARGE_FILES;
303 if (lp_readraw() && lp_writeraw())
304 capabilities |= CAP_RAW_MODE;
306 if (lp_nt_status_support())
307 capabilities |= CAP_STATUS32;
310 capabilities |= CAP_DFS;
312 if (lp_security() >= SEC_USER)
313 secword |= NEGOTIATE_SECURITY_USER_LEVEL;
314 if (global_encrypted_passwords_negotiated)
315 secword |= NEGOTIATE_SECURITY_CHALLENGE_RESPONSE;
317 if (lp_server_signing()) {
318 if (lp_security() >= SEC_USER) {
319 secword |= NEGOTIATE_SECURITY_SIGNATURES_ENABLED;
320 /* No raw mode with smb signing. */
321 capabilities &= ~CAP_RAW_MODE;
322 if (lp_server_signing() == Required)
323 secword |=NEGOTIATE_SECURITY_SIGNATURES_REQUIRED;
324 srv_set_signing_negotiated();
326 DEBUG(0,("reply_nt1: smb signing is incompatible with share level security !\n"));
327 if (lp_server_signing() == Required) {
328 exit_server_cleanly("reply_nt1: smb signing required and share level security selected.");
333 SSVAL(req->outbuf,smb_vwv0,choice);
334 SCVAL(req->outbuf,smb_vwv1,secword);
336 Protocol = PROTOCOL_NT1;
338 SSVAL(req->outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
339 SSVAL(req->outbuf,smb_vwv2+1,1); /* num vcs */
340 SIVAL(req->outbuf,smb_vwv3+1,max_recv); /* max buffer. LOTS! */
341 SIVAL(req->outbuf,smb_vwv5+1,0x10000); /* raw size. full 64k */
342 SIVAL(req->outbuf,smb_vwv7+1,sys_getpid()); /* session key */
343 SIVAL(req->outbuf,smb_vwv9+1,capabilities); /* capabilities */
344 put_long_date((char *)req->outbuf+smb_vwv11+1,t);
345 SSVALS(req->outbuf,smb_vwv15+1,set_server_zone_offset(t)/60);
347 p = q = smb_buf(req->outbuf);
348 if (!negotiate_spnego) {
349 /* Create a token value and add it to the outgoing packet. */
350 if (global_encrypted_passwords_negotiated) {
352 /* note that we do not send a challenge at all if
353 we are using plaintext */
355 ret = message_push_blob(
356 &req->outbuf, data_blob_const(chal, sizeof(chal)));
358 DEBUG(0, ("Could not push challenge\n"));
359 reply_nterror(req, NT_STATUS_NO_MEMORY);
362 SCVAL(req->outbuf, smb_vwv16+1, ret);
365 ret = message_push_string(&req->outbuf, lp_workgroup(),
366 STR_UNICODE|STR_TERMINATE
369 DEBUG(0, ("Could not push challenge\n"));
370 reply_nterror(req, NT_STATUS_NO_MEMORY);
373 DEBUG(3,("not using SPNEGO\n"));
375 DATA_BLOB spnego_blob = negprot_spnego();
377 if (spnego_blob.data == NULL) {
378 reply_nterror(req, NT_STATUS_NO_MEMORY);
382 ret = message_push_blob(&req->outbuf, spnego_blob);
384 DEBUG(0, ("Could not push spnego blob\n"));
385 reply_nterror(req, NT_STATUS_NO_MEMORY);
389 data_blob_free(&spnego_blob);
391 SCVAL(req->outbuf,smb_vwv16+1, 0);
392 DEBUG(3,("using SPNEGO\n"));
395 SSVAL(req->outbuf,smb_vwv17, p - q); /* length of challenge+domain
401 /* these are the protocol lists used for auto architecture detection:
404 protocol [PC NETWORK PROGRAM 1.0]
405 protocol [XENIX CORE]
406 protocol [MICROSOFT NETWORKS 1.03]
408 protocol [Windows for Workgroups 3.1a]
411 protocol [NT LM 0.12]
414 protocol [PC NETWORK PROGRAM 1.0]
415 protocol [XENIX CORE]
416 protocol [MICROSOFT NETWORKS 1.03]
418 protocol [Windows for Workgroups 3.1a]
421 protocol [NT LM 0.12]
424 protocol [PC NETWORK PROGRAM 1.0]
426 protocol [Windows for Workgroups 3.1a]
429 protocol [NT LM 0.12]
432 protocol [PC NETWORK PROGRAM 1.0]
434 protocol [Windows for Workgroups 3.1a]
437 protocol [NT LM 0.12]
441 protocol [PC NETWORK PROGRAM 1.0]
442 protocol [XENIX CORE]
449 * Modified to recognize the architecture of the remote machine better.
451 * This appears to be the matrix of which protocol is used by which
453 Protocol WfWg Win95 WinNT Win2K OS/2 Vista
454 PC NETWORK PROGRAM 1.0 1 1 1 1 1 1
456 MICROSOFT NETWORKS 3.0 2 2
458 MICROSOFT NETWORKS 1.03 3
461 Windows for Workgroups 3.1a 5 5 5 3 3
467 * tim@fsg.com 09/29/95
468 * Win2K added by matty 17/7/99
471 #define ARCH_WFWG 0x3 /* This is a fudge because WfWg is like Win95 */
472 #define ARCH_WIN95 0x2
473 #define ARCH_WINNT 0x4
474 #define ARCH_WIN2K 0xC /* Win2K is like NT */
475 #define ARCH_OS2 0x14 /* Again OS/2 is like NT */
476 #define ARCH_SAMBA 0x20
477 #define ARCH_CIFSFS 0x40
478 #define ARCH_VISTA 0x8C /* Vista is like XP/2K */
480 #define ARCH_ALL 0x7F
482 /* List of supported protocols, most desired first */
483 static const struct {
484 const char *proto_name;
485 const char *short_name;
486 void (*proto_reply_fn)(struct smb_request *req, uint16 choice);
488 } supported_protocols[] = {
489 {"NT LANMAN 1.0", "NT1", reply_nt1, PROTOCOL_NT1},
490 {"NT LM 0.12", "NT1", reply_nt1, PROTOCOL_NT1},
491 {"POSIX 2", "NT1", reply_nt1, PROTOCOL_NT1},
492 {"LANMAN2.1", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
493 {"LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
494 {"Samba", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
495 {"DOS LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
496 {"LANMAN1.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
497 {"MICROSOFT NETWORKS 3.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
498 {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
499 {"PC NETWORK PROGRAM 1.0", "CORE", reply_corep, PROTOCOL_CORE},
503 /****************************************************************************
505 conn POINTER CAN BE NULL HERE !
506 ****************************************************************************/
508 void reply_negprot(connection_struct *conn, struct smb_request *req)
510 size_t size = smb_len(req->inbuf) + 4;
519 static BOOL done_negprot = False;
521 START_PROFILE(SMBnegprot);
524 END_PROFILE(SMBnegprot);
525 exit_server_cleanly("multiple negprot's are not permitted");
529 if (req->inbuf[size-1] != '\0') {
530 DEBUG(0, ("negprot protocols not 0-terminated\n"));
531 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
532 END_PROFILE(SMBnegprot);
536 p = smb_buf(req->inbuf);
541 while (smb_bufrem(req->inbuf, p) > 0) {
545 DEBUG(3, ("Invalid string specifier %x, expected "
546 "0x02\n", (int)p[0]));
547 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
548 END_PROFILE(SMBnegprot);
552 p += 1; /* Skip the "0x02" */
554 tmp = TALLOC_REALLOC_ARRAY(tmp_talloc_ctx(), cliprotos, char *,
557 DEBUG(0, ("talloc failed\n"));
558 TALLOC_FREE(cliprotos);
559 reply_nterror(req, NT_STATUS_NO_MEMORY);
560 END_PROFILE(SMBnegprot);
566 if (pull_ascii_talloc(cliprotos, &cliprotos[num_cliprotos], p)
568 DEBUG(0, ("pull_ascii_talloc failed\n"));
569 TALLOC_FREE(cliprotos);
570 reply_nterror(req, NT_STATUS_NO_MEMORY);
571 END_PROFILE(SMBnegprot);
575 DEBUG(3, ("Requested protocol [%s]\n",
576 cliprotos[num_cliprotos]));
582 for (i=0; i<num_cliprotos; i++) {
583 if (strcsequal(cliprotos[i], "Windows for Workgroups 3.1a"))
584 arch &= ( ARCH_WFWG | ARCH_WIN95 | ARCH_WINNT
586 else if (strcsequal(cliprotos[i], "DOS LM1.2X002"))
587 arch &= ( ARCH_WFWG | ARCH_WIN95 );
588 else if (strcsequal(cliprotos[i], "DOS LANMAN2.1"))
589 arch &= ( ARCH_WFWG | ARCH_WIN95 );
590 else if (strcsequal(cliprotos[i], "NT LM 0.12"))
591 arch &= ( ARCH_WIN95 | ARCH_WINNT | ARCH_WIN2K
593 else if (strcsequal(cliprotos[i], "SMB 2.001"))
595 else if (strcsequal(cliprotos[i], "LANMAN2.1"))
596 arch &= ( ARCH_WINNT | ARCH_WIN2K | ARCH_OS2 );
597 else if (strcsequal(cliprotos[i], "LM1.2X002"))
598 arch &= ( ARCH_WINNT | ARCH_WIN2K | ARCH_OS2 );
599 else if (strcsequal(cliprotos[i], "MICROSOFT NETWORKS 1.03"))
601 else if (strcsequal(cliprotos[i], "XENIX CORE"))
602 arch &= ( ARCH_WINNT | ARCH_OS2 );
603 else if (strcsequal(cliprotos[i], "Samba")) {
606 } else if (strcsequal(cliprotos[i], "POSIX 2")) {
612 /* CIFSFS can send one arch only, NT LM 0.12. */
613 if (i == 1 && (arch & ARCH_CIFSFS)) {
619 set_remote_arch(RA_CIFSFS);
622 set_remote_arch(RA_SAMBA);
625 set_remote_arch(RA_WFWG);
628 set_remote_arch(RA_WIN95);
631 if(req->flags2 == FLAGS2_WIN2K_SIGNATURE)
632 set_remote_arch(RA_WIN2K);
634 set_remote_arch(RA_WINNT);
637 /* Vista may have been set in the negprot so don't
639 if ( get_remote_arch() != RA_VISTA )
640 set_remote_arch(RA_WIN2K);
643 set_remote_arch(RA_VISTA);
646 set_remote_arch(RA_OS2);
649 set_remote_arch(RA_UNKNOWN);
653 /* possibly reload - change of architecture */
654 reload_services(True);
656 /* moved from the netbios session setup code since we don't have that
657 when the client connects to port 445. Of course there is a small
658 window where we are listening to messages -- jerry */
661 NULL,"",FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_PRINT_GENERAL);
663 /* Check for protocols, most desirable first */
664 for (protocol = 0; supported_protocols[protocol].proto_name; protocol++) {
666 if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
667 (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
668 while (i < num_cliprotos) {
669 if (strequal(cliprotos[i],supported_protocols[protocol].proto_name))
678 fstrcpy(remote_proto,supported_protocols[protocol].short_name);
679 reload_services(True);
680 supported_protocols[protocol].proto_reply_fn(req, choice);
681 DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
683 DEBUG(0,("No protocol supported !\n"));
686 DEBUG( 5, ( "negprot index=%d\n", choice ) );
688 if ((lp_server_signing() == Required) && (Protocol < PROTOCOL_NT1)) {
689 exit_server_cleanly("SMB signing is required and "
690 "client negotiated a downlevel protocol");
693 TALLOC_FREE(cliprotos);
694 END_PROFILE(SMBnegprot);