Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[tprouty/samba.git] / source / smbd / negprot.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    negprot reply code
5    Copyright (C) Andrew Tridgell 1992-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 extern int Protocol;
25 extern int max_recv;
26 extern fstring global_myworkgroup;
27 extern fstring remote_machine;
28 BOOL global_encrypted_passwords_negotiated;
29
30 /****************************************************************************
31 reply for the core protocol
32 ****************************************************************************/
33 static int reply_corep(char *outbuf)
34 {
35         int outsize = set_message(outbuf,1,0,True);
36
37         Protocol = PROTOCOL_CORE;
38         
39         return outsize;
40 }
41
42
43 /****************************************************************************
44 reply for the coreplus protocol
45 ****************************************************************************/
46 static int reply_coreplus(char *outbuf)
47 {
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 */  
55
56   Protocol = PROTOCOL_COREPLUS;
57
58   return outsize;
59 }
60
61
62 /****************************************************************************
63 reply for the lanman 1.0 protocol
64 ****************************************************************************/
65 static int reply_lanman1(char *outbuf)
66 {
67   int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
68   int secword=0;
69   time_t t = time(NULL);
70
71   global_encrypted_passwords_negotiated = lp_encrypted_passwords();
72
73   if (lp_security()>=SEC_USER) secword |= 1;
74   if (global_encrypted_passwords_negotiated) secword |= 2;
75
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));
81
82   Protocol = PROTOCOL_LANMAN1;
83
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);
93
94   put_dos_date(outbuf,smb_vwv8,t);
95
96   return (smb_len(outbuf)+4);
97 }
98
99
100 /****************************************************************************
101 reply for the lanman 2.0 protocol
102 ****************************************************************************/
103 static int reply_lanman2(char *outbuf)
104 {
105   int raw = (lp_readraw()?1:0) | (lp_writeraw()?2:0);
106   int secword=0;
107   time_t t = time(NULL);
108   struct cli_state *cli = NULL;
109   char cryptkey[8];
110   char crypt_len = 0;
111
112   global_encrypted_passwords_negotiated = lp_encrypted_passwords();
113
114   if (lp_security() == SEC_SERVER) {
115           cli = server_cryptkey();
116   }
117
118   if (cli) {
119           DEBUG(3,("using password server validation\n"));
120           global_encrypted_passwords_negotiated = ((cli->sec_mode & 2) != 0);
121   }
122
123   if (lp_security()>=SEC_USER) secword |= 1;
124   if (global_encrypted_passwords_negotiated) secword |= 2;
125
126   if (global_encrypted_passwords_negotiated) {
127           crypt_len = 8;
128           if (!cli) {
129                   generate_next_challenge(cryptkey);
130           } else {
131                   memcpy(cryptkey, cli->cryptkey, 8);
132                   set_challenge(cli->cryptkey);
133           }
134   }
135
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);
141
142   Protocol = PROTOCOL_LANMAN2;
143
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);
152
153   return (smb_len(outbuf)+4);
154 }
155
156
157 /****************************************************************************
158 reply for the nt protocol
159 ****************************************************************************/
160 static int reply_nt1(char *outbuf)
161 {
162         /* dual names + lock_and_read + nt SMBs + remote API calls */
163         int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ|
164                 CAP_LEVEL_II_OPLOCKS|CAP_STATUS32;
165
166         int secword=0;
167         time_t t = time(NULL);
168         struct cli_state *cli = NULL;
169         char cryptkey[8];
170         char crypt_len = 0;
171         char *p, *q;
172         
173         global_encrypted_passwords_negotiated = lp_encrypted_passwords();
174
175         if (lp_security() == SEC_SERVER) {
176                 DEBUG(5,("attempting password server validation\n"));
177                 cli = server_cryptkey();
178         } else {
179                 DEBUG(5,("not attempting password server validation\n"));
180         }
181         
182         if (cli) {
183                 DEBUG(3,("using password server validation\n"));
184                 global_encrypted_passwords_negotiated = ((cli->sec_mode & 2) != 0);
185         } else {
186                 DEBUG(3,("not using password server validation\n"));
187         }
188         
189         if (global_encrypted_passwords_negotiated) {
190                 crypt_len = 8;
191                 if (!cli) {
192                         generate_next_challenge(cryptkey);
193                 } else {
194                         memcpy(cryptkey, cli->cryptkey, 8);
195                         set_challenge(cli->cryptkey);
196                 }
197         }
198
199         capabilities |= CAP_NT_SMBS|CAP_RPC_REMOTE_APIS;
200         
201         if (lp_large_readwrite() && (SMB_OFF_T_BITS == 64)) {
202                 capabilities |= CAP_LARGE_READX|CAP_LARGE_WRITEX|CAP_W2K_SMBS;
203         }
204         
205         if (SMB_OFF_T_BITS == 64) {
206                 capabilities |= CAP_LARGE_FILES;
207         }
208         
209         if (lp_readraw() && lp_writeraw()) {
210                 capabilities |= CAP_RAW_MODE;
211         }
212         
213         /* allow for disabling unicode */
214         if (lp_unicode()) {
215                 capabilities |= CAP_UNICODE;
216         }
217         
218         if (lp_host_msdfs())
219                 capabilities |= CAP_DFS;
220         
221         if (lp_security() >= SEC_USER) secword |= 1;
222         if (global_encrypted_passwords_negotiated) secword |= 2;
223         
224         set_message(outbuf,17,0,True);
225         
226         CVAL(outbuf,smb_vwv1) = secword;
227         SSVALS(outbuf,smb_vwv16+1,crypt_len);
228         
229         Protocol = PROTOCOL_NT1;
230         
231         SSVAL(outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
232         SSVAL(outbuf,smb_vwv2+1,1); /* num vcs */
233         SIVAL(outbuf,smb_vwv3+1,0xffff); /* max buffer. LOTS! */
234         SIVAL(outbuf,smb_vwv5+1,0x10000); /* raw size. full 64k */
235         SIVAL(outbuf,smb_vwv7+1,sys_getpid()); /* session key */
236         SIVAL(outbuf,smb_vwv9+1,capabilities); /* capabilities */
237         put_long_date(outbuf+smb_vwv11+1,t);
238         SSVALS(outbuf,smb_vwv15+1,TimeDiff(t)/60);
239         
240         p = q = smb_buf(outbuf);
241         if (global_encrypted_passwords_negotiated) memcpy(p, cryptkey, 8);
242         p += 8;
243         p += srvstr_push(outbuf, p, global_myworkgroup, -1, 
244                          STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
245         
246         SSVAL(outbuf,smb_vwv17, p - q); /* length of challenge+domain strings */
247         set_message_end(outbuf, p);
248         
249         return (smb_len(outbuf)+4);
250 }
251
252 /* these are the protocol lists used for auto architecture detection:
253
254 WinNT 3.51:
255 protocol [PC NETWORK PROGRAM 1.0]
256 protocol [XENIX CORE]
257 protocol [MICROSOFT NETWORKS 1.03]
258 protocol [LANMAN1.0]
259 protocol [Windows for Workgroups 3.1a]
260 protocol [LM1.2X002]
261 protocol [LANMAN2.1]
262 protocol [NT LM 0.12]
263
264 Win95:
265 protocol [PC NETWORK PROGRAM 1.0]
266 protocol [XENIX CORE]
267 protocol [MICROSOFT NETWORKS 1.03]
268 protocol [LANMAN1.0]
269 protocol [Windows for Workgroups 3.1a]
270 protocol [LM1.2X002]
271 protocol [LANMAN2.1]
272 protocol [NT LM 0.12]
273
274 Win2K:
275 protocol [PC NETWORK PROGRAM 1.0]
276 protocol [LANMAN1.0]
277 protocol [Windows for Workgroups 3.1a]
278 protocol [LM1.2X002]
279 protocol [LANMAN2.1]
280 protocol [NT LM 0.12]
281
282 OS/2:
283 protocol [PC NETWORK PROGRAM 1.0]
284 protocol [XENIX CORE]
285 protocol [LANMAN1.0]
286 protocol [LM1.2X002]
287 protocol [LANMAN2.1]
288 */
289
290 /*
291   * Modified to recognize the architecture of the remote machine better.
292   *
293   * This appears to be the matrix of which protocol is used by which
294   * MS product.
295        Protocol                       WfWg    Win95   WinNT  Win2K  OS/2
296        PC NETWORK PROGRAM 1.0          1       1       1      1      1
297        XENIX CORE                                      2             2
298        MICROSOFT NETWORKS 3.0          2       2       
299        DOS LM1.2X002                   3       3       
300        MICROSOFT NETWORKS 1.03                         3
301        DOS LANMAN2.1                   4       4       
302        LANMAN1.0                                       4      2      3
303        Windows for Workgroups 3.1a     5       5       5      3
304        LM1.2X002                                       6      4      4
305        LANMAN2.1                                       7      5      5
306        NT LM 0.12                              6       8      6
307   *
308   *  tim@fsg.com 09/29/95
309   *  Win2K added by matty 17/7/99
310   */
311   
312 #define ARCH_WFWG     0x3      /* This is a fudge because WfWg is like Win95 */
313 #define ARCH_WIN95    0x2
314 #define ARCH_WINNT    0x4
315 #define ARCH_WIN2K    0xC      /* Win2K is like NT */
316 #define ARCH_OS2      0x14     /* Again OS/2 is like NT */
317 #define ARCH_SAMBA    0x20
318  
319 #define ARCH_ALL      0x3F
320  
321 /* List of supported protocols, most desired first */
322 static struct {
323   char *proto_name;
324   char *short_name;
325   int (*proto_reply_fn)(char *);
326   int protocol_level;
327 } supported_protocols[] = {
328   {"NT LANMAN 1.0",           "NT1",      reply_nt1,      PROTOCOL_NT1},
329   {"NT LM 0.12",              "NT1",      reply_nt1,      PROTOCOL_NT1},
330   {"LM1.2X002",               "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
331   {"Samba",                   "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
332   {"DOS LM1.2X002",           "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
333   {"LANMAN1.0",               "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
334   {"MICROSOFT NETWORKS 3.0",  "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
335   {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
336   {"PC NETWORK PROGRAM 1.0",  "CORE",     reply_corep,    PROTOCOL_CORE}, 
337   {NULL,NULL,NULL,0},
338 };
339
340
341 /****************************************************************************
342   reply to a negprot
343 ****************************************************************************/
344 int reply_negprot(connection_struct *conn, 
345                   char *inbuf,char *outbuf, int dum_size, 
346                   int dum_buffsize)
347 {
348   int outsize = set_message(outbuf,1,0,True);
349   int Index=0;
350   int choice= -1;
351   int protocol;
352   char *p;
353   int bcc = SVAL(smb_buf(inbuf),-2);
354   int arch = ARCH_ALL;
355   START_PROFILE(SMBnegprot);
356
357   p = smb_buf(inbuf)+1;
358   while (p < (smb_buf(inbuf) + bcc))
359     { 
360       Index++;
361       DEBUG(3,("Requested protocol [%s]\n",p));
362       if (strcsequal(p,"Windows for Workgroups 3.1a"))
363         arch &= ( ARCH_WFWG | ARCH_WIN95 | ARCH_WINNT | ARCH_WIN2K );
364       else if (strcsequal(p,"DOS LM1.2X002"))
365         arch &= ( ARCH_WFWG | ARCH_WIN95 );
366       else if (strcsequal(p,"DOS LANMAN2.1"))
367         arch &= ( ARCH_WFWG | ARCH_WIN95 );
368       else if (strcsequal(p,"NT LM 0.12"))
369         arch &= ( ARCH_WIN95 | ARCH_WINNT | ARCH_WIN2K );
370       else if (strcsequal(p,"LANMAN2.1"))
371         arch &= ( ARCH_WINNT | ARCH_WIN2K | ARCH_OS2 );
372       else if (strcsequal(p,"LM1.2X002"))
373         arch &= ( ARCH_WINNT | ARCH_WIN2K | ARCH_OS2 );
374       else if (strcsequal(p,"MICROSOFT NETWORKS 1.03"))
375         arch &= ARCH_WINNT;
376       else if (strcsequal(p,"XENIX CORE"))
377         arch &= ( ARCH_WINNT | ARCH_OS2 );
378       else if (strcsequal(p,"Samba")) {
379         arch = ARCH_SAMBA;
380         break;
381       }
382  
383       p += strlen(p) + 2;
384     }
385     
386   switch ( arch ) {
387   case ARCH_SAMBA:
388     set_remote_arch(RA_SAMBA);
389     break;
390   case ARCH_WFWG:
391     set_remote_arch(RA_WFWG);
392     break;
393   case ARCH_WIN95:
394     set_remote_arch(RA_WIN95);
395     break;
396   case ARCH_WINNT:
397    if(SVAL(inbuf,smb_flg2)==FLAGS2_WIN2K_SIGNATURE)
398      set_remote_arch(RA_WIN2K);
399    else
400      set_remote_arch(RA_WINNT);
401     break;
402   case ARCH_WIN2K:
403     set_remote_arch(RA_WIN2K);
404     break;
405   case ARCH_OS2:
406     set_remote_arch(RA_OS2);
407     break;
408   default:
409     set_remote_arch(RA_UNKNOWN);
410     break;
411   }
412  
413   /* possibly reload - change of architecture */
414   reload_services(True);      
415     
416   /* a special case to stop password server loops */
417   if (Index == 1 && strequal(remote_machine,myhostname()) && 
418       (lp_security()==SEC_SERVER || lp_security()==SEC_DOMAIN))
419     exit_server("Password server loop!");
420   
421   /* Check for protocols, most desirable first */
422   for (protocol = 0; supported_protocols[protocol].proto_name; protocol++)
423     {
424       p = smb_buf(inbuf)+1;
425       Index = 0;
426       if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
427           (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
428         while (p < (smb_buf(inbuf) + bcc))
429           { 
430             if (strequal(p,supported_protocols[protocol].proto_name))
431               choice = Index;
432             Index++;
433             p += strlen(p) + 2;
434           }
435       if(choice != -1)
436         break;
437     }
438   
439   SSVAL(outbuf,smb_vwv0,choice);
440   if(choice != -1) {
441     extern fstring remote_proto;
442     fstrcpy(remote_proto,supported_protocols[protocol].short_name);
443     reload_services(True);          
444     outsize = supported_protocols[protocol].proto_reply_fn(outbuf);
445     DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
446   }
447   else {
448     DEBUG(0,("No protocol supported !\n"));
449   }
450   SSVAL(outbuf,smb_vwv0,choice);
451   
452   DEBUG( 5, ( "negprot index=%d\n", choice ) );
453
454   END_PROFILE(SMBnegprot);
455   return(outsize);
456 }