2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
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.
26 extern fstring global_myworkgroup;
27 extern fstring remote_machine;
28 BOOL global_encrypted_passwords_negotiated;
30 /****************************************************************************
31 reply for the core protocol
32 ****************************************************************************/
33 static int reply_corep(char *inbuf, char *outbuf)
35 int outsize = set_message(outbuf,1,0,True);
37 Protocol = PROTOCOL_CORE;
43 /****************************************************************************
44 reply for the coreplus protocol
45 ****************************************************************************/
46 static int reply_coreplus(char *inbuf, char *outbuf)
48 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
49 int outsize = set_message(outbuf,13,0,True);
50 SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
51 readbraw and writebraw (possibly) */
52 /* Reply, SMBlockread, SMBwritelock supported. */
53 SCVAL(outbuf,smb_flg,FLAG_REPLY|FLAG_SUPPORT_LOCKREAD);
54 SSVAL(outbuf,smb_vwv1,0x1); /* user level security, don't encrypt */
56 Protocol = PROTOCOL_COREPLUS;
62 /****************************************************************************
63 reply for the lanman 1.0 protocol
64 ****************************************************************************/
65 static int reply_lanman1(char *inbuf, char *outbuf)
67 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
69 time_t t = time(NULL);
71 global_encrypted_passwords_negotiated = lp_encrypted_passwords();
73 if (lp_security()>=SEC_USER) secword |= 1;
74 if (global_encrypted_passwords_negotiated) secword |= 2;
76 set_message(outbuf,13,global_encrypted_passwords_negotiated?8:0,True);
77 SSVAL(outbuf,smb_vwv1,secword);
78 /* Create a token value and add it to the outgoing packet. */
79 if (global_encrypted_passwords_negotiated)
80 generate_next_challenge(smb_buf(outbuf));
82 Protocol = PROTOCOL_LANMAN1;
84 /* Reply, SMBlockread, SMBwritelock supported. */
85 SCVAL(outbuf,smb_flg,FLAG_REPLY|FLAG_SUPPORT_LOCKREAD);
86 SSVAL(outbuf,smb_vwv2,max_recv);
87 SSVAL(outbuf,smb_vwv3,lp_maxmux()); /* maxmux */
88 SSVAL(outbuf,smb_vwv4,1);
89 SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
90 readbraw writebraw (possibly) */
91 SIVAL(outbuf,smb_vwv6,sys_getpid());
92 SSVAL(outbuf,smb_vwv10, TimeDiff(t)/60);
94 put_dos_date(outbuf,smb_vwv8,t);
96 return (smb_len(outbuf)+4);
100 /****************************************************************************
101 reply for the lanman 2.0 protocol
102 ****************************************************************************/
103 static int reply_lanman2(char *inbuf, char *outbuf)
105 int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
107 time_t t = time(NULL);
108 struct cli_state *cli = NULL;
112 global_encrypted_passwords_negotiated = lp_encrypted_passwords();
114 if (lp_security() == SEC_SERVER) {
115 cli = server_cryptkey();
119 DEBUG(3,("using password server validation\n"));
120 global_encrypted_passwords_negotiated = ((cli->sec_mode & 2) != 0);
123 if (lp_security()>=SEC_USER) secword |= 1;
124 if (global_encrypted_passwords_negotiated) secword |= 2;
126 if (global_encrypted_passwords_negotiated) {
129 generate_next_challenge(cryptkey);
131 memcpy(cryptkey, cli->secblob.data, 8);
132 set_challenge(cryptkey);
136 set_message(outbuf,13,crypt_len,True);
137 SSVAL(outbuf,smb_vwv1,secword);
138 SIVAL(outbuf,smb_vwv6,sys_getpid());
139 if (global_encrypted_passwords_negotiated)
140 memcpy(smb_buf(outbuf), cryptkey, 8);
142 Protocol = PROTOCOL_LANMAN2;
144 /* Reply, SMBlockread, SMBwritelock supported. */
145 SCVAL(outbuf,smb_flg,FLAG_REPLY|FLAG_SUPPORT_LOCKREAD);
146 SSVAL(outbuf,smb_vwv2,max_recv);
147 SSVAL(outbuf,smb_vwv3,lp_maxmux());
148 SSVAL(outbuf,smb_vwv4,1);
149 SSVAL(outbuf,smb_vwv5,raw); /* readbraw and/or writebraw */
150 SSVAL(outbuf,smb_vwv10, TimeDiff(t)/60);
151 put_dos_date(outbuf,smb_vwv8,t);
153 return (smb_len(outbuf)+4);
159 generate the spnego negprot reply blob. Return the number of bytes used
161 static int negprot_spnego(char *p, uint8 cryptkey[8])
164 extern pstring global_myname;
166 const char *OIDs[] = {OID_NTLMSSP,
173 safe_strcpy(guid, global_myname, 16);
176 /* win2000 uses host$@REALM, which we will probably use eventually,
177 but for now this works */
178 asprintf(&principle, "HOST/%s@%s", guid, lp_realm());
179 blob = spnego_gen_negTokenInit(guid, OIDs, principle);
182 memcpy(p, blob.data, blob.length);
184 data_blob_free(&blob);
190 /****************************************************************************
191 reply for the nt protocol
192 ****************************************************************************/
193 static int reply_nt1(char *inbuf, char *outbuf)
195 /* dual names + lock_and_read + nt SMBs + remote API calls */
196 int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ|
197 CAP_LEVEL_II_OPLOCKS|CAP_STATUS32;
200 time_t t = time(NULL);
201 struct cli_state *cli = NULL;
204 BOOL negotiate_spnego = False;
206 global_encrypted_passwords_negotiated = lp_encrypted_passwords();
208 if (lp_security() == SEC_SERVER) {
209 DEBUG(5,("attempting password server validation\n"));
210 cli = server_cryptkey();
212 DEBUG(5,("not attempting password server validation\n"));
213 /* do spnego in user level security if the client
214 supports it and we can do encrypted passwords */
215 if (global_encrypted_passwords_negotiated &&
216 lp_security() == SEC_USER &&
217 (SVAL(inbuf, smb_flg2) & FLAGS2_EXTENDED_SECURITY)) {
218 negotiate_spnego = True;
219 capabilities |= CAP_EXTENDED_SECURITY;
224 DEBUG(3,("using password server validation\n"));
225 global_encrypted_passwords_negotiated = ((cli->sec_mode & 2) != 0);
227 DEBUG(3,("not using password server validation\n"));
230 if (global_encrypted_passwords_negotiated) {
232 generate_next_challenge(cryptkey);
234 memcpy(cryptkey, cli->secblob.data, 8);
235 set_challenge(cryptkey);
239 capabilities |= CAP_NT_SMBS|CAP_RPC_REMOTE_APIS;
241 if (lp_large_readwrite() && (SMB_OFF_T_BITS == 64)) {
242 capabilities |= CAP_LARGE_READX|CAP_LARGE_WRITEX|CAP_W2K_SMBS;
245 if (SMB_OFF_T_BITS == 64) {
246 capabilities |= CAP_LARGE_FILES;
249 if (lp_readraw() && lp_writeraw()) {
250 capabilities |= CAP_RAW_MODE;
253 /* allow for disabling unicode */
255 capabilities |= CAP_UNICODE;
259 capabilities |= CAP_DFS;
261 if (lp_security() >= SEC_USER) secword |= 1;
262 if (global_encrypted_passwords_negotiated) secword |= 2;
264 set_message(outbuf,17,0,True);
266 CVAL(outbuf,smb_vwv1) = secword;
268 Protocol = PROTOCOL_NT1;
270 SSVAL(outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
271 SSVAL(outbuf,smb_vwv2+1,1); /* num vcs */
272 SIVAL(outbuf,smb_vwv3+1,0xffff); /* max buffer. LOTS! */
273 SIVAL(outbuf,smb_vwv5+1,0x10000); /* raw size. full 64k */
274 SIVAL(outbuf,smb_vwv7+1,sys_getpid()); /* session key */
275 SIVAL(outbuf,smb_vwv9+1,capabilities); /* capabilities */
276 put_long_date(outbuf+smb_vwv11+1,t);
277 SSVALS(outbuf,smb_vwv15+1,TimeDiff(t)/60);
279 p = q = smb_buf(outbuf);
280 if (!negotiate_spnego) {
281 if (global_encrypted_passwords_negotiated) memcpy(p, cryptkey, 8);
282 SSVALS(outbuf,smb_vwv16+1,8);
285 int len = negprot_spnego(p, cryptkey);
286 SSVALS(outbuf,smb_vwv16+1,len);
289 p += srvstr_push(outbuf, p, global_myworkgroup, -1,
290 STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
292 SSVAL(outbuf,smb_vwv17, p - q); /* length of challenge+domain strings */
293 set_message_end(outbuf, p);
295 return (smb_len(outbuf)+4);
298 /* these are the protocol lists used for auto architecture detection:
301 protocol [PC NETWORK PROGRAM 1.0]
302 protocol [XENIX CORE]
303 protocol [MICROSOFT NETWORKS 1.03]
305 protocol [Windows for Workgroups 3.1a]
308 protocol [NT LM 0.12]
311 protocol [PC NETWORK PROGRAM 1.0]
312 protocol [XENIX CORE]
313 protocol [MICROSOFT NETWORKS 1.03]
315 protocol [Windows for Workgroups 3.1a]
318 protocol [NT LM 0.12]
321 protocol [PC NETWORK PROGRAM 1.0]
323 protocol [Windows for Workgroups 3.1a]
326 protocol [NT LM 0.12]
329 protocol [PC NETWORK PROGRAM 1.0]
330 protocol [XENIX CORE]
337 * Modified to recognize the architecture of the remote machine better.
339 * This appears to be the matrix of which protocol is used by which
341 Protocol WfWg Win95 WinNT Win2K OS/2
342 PC NETWORK PROGRAM 1.0 1 1 1 1 1
344 MICROSOFT NETWORKS 3.0 2 2
346 MICROSOFT NETWORKS 1.03 3
349 Windows for Workgroups 3.1a 5 5 5 3
354 * tim@fsg.com 09/29/95
355 * Win2K added by matty 17/7/99
358 #define ARCH_WFWG 0x3 /* This is a fudge because WfWg is like Win95 */
359 #define ARCH_WIN95 0x2
360 #define ARCH_WINNT 0x4
361 #define ARCH_WIN2K 0xC /* Win2K is like NT */
362 #define ARCH_OS2 0x14 /* Again OS/2 is like NT */
363 #define ARCH_SAMBA 0x20
365 #define ARCH_ALL 0x3F
367 /* List of supported protocols, most desired first */
371 int (*proto_reply_fn)(char *, char *);
373 } supported_protocols[] = {
374 {"NT LANMAN 1.0", "NT1", reply_nt1, PROTOCOL_NT1},
375 {"NT LM 0.12", "NT1", reply_nt1, PROTOCOL_NT1},
376 {"LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
377 {"Samba", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
378 {"DOS LM1.2X002", "LANMAN2", reply_lanman2, PROTOCOL_LANMAN2},
379 {"LANMAN1.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
380 {"MICROSOFT NETWORKS 3.0", "LANMAN1", reply_lanman1, PROTOCOL_LANMAN1},
381 {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
382 {"PC NETWORK PROGRAM 1.0", "CORE", reply_corep, PROTOCOL_CORE},
387 /****************************************************************************
389 ****************************************************************************/
390 int reply_negprot(connection_struct *conn,
391 char *inbuf,char *outbuf, int dum_size,
394 int outsize = set_message(outbuf,1,0,True);
399 int bcc = SVAL(smb_buf(inbuf),-2);
401 START_PROFILE(SMBnegprot);
403 p = smb_buf(inbuf)+1;
404 while (p < (smb_buf(inbuf) + bcc))
407 DEBUG(3,("Requested protocol [%s]\n",p));
408 if (strcsequal(p,"Windows for Workgroups 3.1a"))
409 arch &= ( ARCH_WFWG | ARCH_WIN95 | ARCH_WINNT | ARCH_WIN2K );
410 else if (strcsequal(p,"DOS LM1.2X002"))
411 arch &= ( ARCH_WFWG | ARCH_WIN95 );
412 else if (strcsequal(p,"DOS LANMAN2.1"))
413 arch &= ( ARCH_WFWG | ARCH_WIN95 );
414 else if (strcsequal(p,"NT LM 0.12"))
415 arch &= ( ARCH_WIN95 | ARCH_WINNT | ARCH_WIN2K );
416 else if (strcsequal(p,"LANMAN2.1"))
417 arch &= ( ARCH_WINNT | ARCH_WIN2K | ARCH_OS2 );
418 else if (strcsequal(p,"LM1.2X002"))
419 arch &= ( ARCH_WINNT | ARCH_WIN2K | ARCH_OS2 );
420 else if (strcsequal(p,"MICROSOFT NETWORKS 1.03"))
422 else if (strcsequal(p,"XENIX CORE"))
423 arch &= ( ARCH_WINNT | ARCH_OS2 );
424 else if (strcsequal(p,"Samba")) {
434 set_remote_arch(RA_SAMBA);
437 set_remote_arch(RA_WFWG);
440 set_remote_arch(RA_WIN95);
443 if(SVAL(inbuf,smb_flg2)==FLAGS2_WIN2K_SIGNATURE)
444 set_remote_arch(RA_WIN2K);
446 set_remote_arch(RA_WINNT);
449 set_remote_arch(RA_WIN2K);
452 set_remote_arch(RA_OS2);
455 set_remote_arch(RA_UNKNOWN);
459 /* possibly reload - change of architecture */
460 reload_services(True);
462 /* a special case to stop password server loops */
463 if (Index == 1 && strequal(remote_machine,myhostname()) &&
464 (lp_security()==SEC_SERVER || lp_security()==SEC_DOMAIN))
465 exit_server("Password server loop!");
467 /* Check for protocols, most desirable first */
468 for (protocol = 0; supported_protocols[protocol].proto_name; protocol++)
470 p = smb_buf(inbuf)+1;
472 if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
473 (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
474 while (p < (smb_buf(inbuf) + bcc))
476 if (strequal(p,supported_protocols[protocol].proto_name))
485 SSVAL(outbuf,smb_vwv0,choice);
487 extern fstring remote_proto;
488 fstrcpy(remote_proto,supported_protocols[protocol].short_name);
489 reload_services(True);
490 outsize = supported_protocols[protocol].proto_reply_fn(inbuf, outbuf);
491 DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
494 DEBUG(0,("No protocol supported !\n"));
496 SSVAL(outbuf,smb_vwv0,choice);
498 DEBUG( 5, ( "negprot index=%d\n", choice ) );
500 END_PROFILE(SMBnegprot);