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