get rid of compiler warnings (casts and delete unused variables)
[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 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 *inbuf, 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 *inbuf, 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 *inbuf, 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 *inbuf, 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->secblob.data, 8);
132                   set_challenge((unsigned char *)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 /* 
159    generate the spnego negprot reply blob. Return the number of bytes used
160 */
161 static int negprot_spnego(char *p, uint8 cryptkey[8])
162 {
163         DATA_BLOB blob;
164         extern pstring global_myname;
165         uint8 guid[16];
166         const char *OIDs[] = {OID_NTLMSSP,
167                               OID_KERBEROS5,
168                               OID_KERBEROS5_OLD,
169                               NULL};
170         char *principal;
171         int len;
172
173         memset(guid, 0, 16);
174         safe_strcpy((char *)guid, global_myname, 16);
175         strlower((char *)guid);
176
177         /* win2000 uses host$@REALM, which we will probably use eventually,
178            but for now this works */
179         asprintf(&principal, "HOST/%s@%s", guid, lp_realm());
180         blob = spnego_gen_negTokenInit(guid, OIDs, principal);
181         free(principal);
182
183         memcpy(p, blob.data, blob.length);
184         len = blob.length;
185         data_blob_free(&blob);
186         return len;
187 }
188
189                 
190
191 /****************************************************************************
192 reply for the nt protocol
193 ****************************************************************************/
194 static int reply_nt1(char *inbuf, char *outbuf)
195 {
196         /* dual names + lock_and_read + nt SMBs + remote API calls */
197         int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ|
198                 CAP_LEVEL_II_OPLOCKS|CAP_STATUS32;
199
200         int secword=0;
201         time_t t = time(NULL);
202         struct cli_state *cli = NULL;
203         uint8 cryptkey[8];
204         char *p, *q;
205         BOOL negotiate_spnego = False;
206
207         global_encrypted_passwords_negotiated = lp_encrypted_passwords();
208
209         if (lp_security() == SEC_SERVER) {
210                 DEBUG(5,("attempting password server validation\n"));
211                 cli = server_cryptkey();
212         } else {
213                 DEBUG(5,("not attempting password server validation\n"));
214                 /* do spnego in user level security if the client
215                    supports it and we can do encrypted passwords */
216                 if (global_encrypted_passwords_negotiated && 
217                     lp_security() == SEC_USER &&
218                     (SVAL(inbuf, smb_flg2) & FLAGS2_EXTENDED_SECURITY)) {
219                         negotiate_spnego = True;
220                         capabilities |= CAP_EXTENDED_SECURITY;
221                 }
222         }
223         
224         if (cli) {
225                 DEBUG(3,("using password server validation\n"));
226                 global_encrypted_passwords_negotiated = ((cli->sec_mode & 2) != 0);
227         } else {
228                 DEBUG(3,("not using password server validation\n"));
229         }
230         
231         if (global_encrypted_passwords_negotiated) {
232                 if (!cli) {
233                         generate_next_challenge((char *)cryptkey);
234                 } else {
235                         memcpy(cryptkey, cli->secblob.data, 8);
236                         set_challenge(cryptkey);
237                 }
238         }
239
240         capabilities |= CAP_NT_SMBS|CAP_RPC_REMOTE_APIS;
241         
242         if (lp_large_readwrite() && (SMB_OFF_T_BITS == 64)) {
243                 capabilities |= CAP_LARGE_READX|CAP_LARGE_WRITEX|CAP_W2K_SMBS;
244         }
245         
246         if (SMB_OFF_T_BITS == 64) {
247                 capabilities |= CAP_LARGE_FILES;
248         }
249         
250         if (lp_readraw() && lp_writeraw()) {
251                 capabilities |= CAP_RAW_MODE;
252         }
253         
254         /* allow for disabling unicode */
255         if (lp_unicode()) {
256                 capabilities |= CAP_UNICODE;
257         }
258         
259         if (lp_host_msdfs())
260                 capabilities |= CAP_DFS;
261         
262         if (lp_security() >= SEC_USER) secword |= 1;
263         if (global_encrypted_passwords_negotiated) secword |= 2;
264         
265         set_message(outbuf,17,0,True);
266         
267         CVAL(outbuf,smb_vwv1) = secword;
268         
269         Protocol = PROTOCOL_NT1;
270         
271         SSVAL(outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
272         SSVAL(outbuf,smb_vwv2+1,1); /* num vcs */
273         SIVAL(outbuf,smb_vwv3+1,0xffff); /* max buffer. LOTS! */
274         SIVAL(outbuf,smb_vwv5+1,0x10000); /* raw size. full 64k */
275         SIVAL(outbuf,smb_vwv7+1,sys_getpid()); /* session key */
276         SIVAL(outbuf,smb_vwv9+1,capabilities); /* capabilities */
277         put_long_date(outbuf+smb_vwv11+1,t);
278         SSVALS(outbuf,smb_vwv15+1,TimeDiff(t)/60);
279         
280         p = q = smb_buf(outbuf);
281         if (!negotiate_spnego) {
282                 if (global_encrypted_passwords_negotiated) memcpy(p, cryptkey, 8);
283                 SSVALS(outbuf,smb_vwv16+1,8);
284                 p += 8;
285         } else {
286                 int len = negprot_spnego(p, cryptkey);
287                 SSVALS(outbuf,smb_vwv16+1,len);
288                 p += len;
289         }
290         p += srvstr_push(outbuf, p, global_myworkgroup, -1, 
291                          STR_UNICODE|STR_TERMINATE|STR_NOALIGN);
292         
293         SSVAL(outbuf,smb_vwv17, p - q); /* length of challenge+domain strings */
294         set_message_end(outbuf, p);
295         
296         return (smb_len(outbuf)+4);
297 }
298
299 /* these are the protocol lists used for auto architecture detection:
300
301 WinNT 3.51:
302 protocol [PC NETWORK PROGRAM 1.0]
303 protocol [XENIX CORE]
304 protocol [MICROSOFT NETWORKS 1.03]
305 protocol [LANMAN1.0]
306 protocol [Windows for Workgroups 3.1a]
307 protocol [LM1.2X002]
308 protocol [LANMAN2.1]
309 protocol [NT LM 0.12]
310
311 Win95:
312 protocol [PC NETWORK PROGRAM 1.0]
313 protocol [XENIX CORE]
314 protocol [MICROSOFT NETWORKS 1.03]
315 protocol [LANMAN1.0]
316 protocol [Windows for Workgroups 3.1a]
317 protocol [LM1.2X002]
318 protocol [LANMAN2.1]
319 protocol [NT LM 0.12]
320
321 Win2K:
322 protocol [PC NETWORK PROGRAM 1.0]
323 protocol [LANMAN1.0]
324 protocol [Windows for Workgroups 3.1a]
325 protocol [LM1.2X002]
326 protocol [LANMAN2.1]
327 protocol [NT LM 0.12]
328
329 OS/2:
330 protocol [PC NETWORK PROGRAM 1.0]
331 protocol [XENIX CORE]
332 protocol [LANMAN1.0]
333 protocol [LM1.2X002]
334 protocol [LANMAN2.1]
335 */
336
337 /*
338   * Modified to recognize the architecture of the remote machine better.
339   *
340   * This appears to be the matrix of which protocol is used by which
341   * MS product.
342        Protocol                       WfWg    Win95   WinNT  Win2K  OS/2
343        PC NETWORK PROGRAM 1.0          1       1       1      1      1
344        XENIX CORE                                      2             2
345        MICROSOFT NETWORKS 3.0          2       2       
346        DOS LM1.2X002                   3       3       
347        MICROSOFT NETWORKS 1.03                         3
348        DOS LANMAN2.1                   4       4       
349        LANMAN1.0                                       4      2      3
350        Windows for Workgroups 3.1a     5       5       5      3
351        LM1.2X002                                       6      4      4
352        LANMAN2.1                                       7      5      5
353        NT LM 0.12                              6       8      6
354   *
355   *  tim@fsg.com 09/29/95
356   *  Win2K added by matty 17/7/99
357   */
358   
359 #define ARCH_WFWG     0x3      /* This is a fudge because WfWg is like Win95 */
360 #define ARCH_WIN95    0x2
361 #define ARCH_WINNT    0x4
362 #define ARCH_WIN2K    0xC      /* Win2K is like NT */
363 #define ARCH_OS2      0x14     /* Again OS/2 is like NT */
364 #define ARCH_SAMBA    0x20
365  
366 #define ARCH_ALL      0x3F
367  
368 /* List of supported protocols, most desired first */
369 static struct {
370   char *proto_name;
371   char *short_name;
372   int (*proto_reply_fn)(char *, char *);
373   int protocol_level;
374 } supported_protocols[] = {
375   {"NT LANMAN 1.0",           "NT1",      reply_nt1,      PROTOCOL_NT1},
376   {"NT LM 0.12",              "NT1",      reply_nt1,      PROTOCOL_NT1},
377   {"LM1.2X002",               "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
378   {"Samba",                   "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
379   {"DOS LM1.2X002",           "LANMAN2",  reply_lanman2,  PROTOCOL_LANMAN2},
380   {"LANMAN1.0",               "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
381   {"MICROSOFT NETWORKS 3.0",  "LANMAN1",  reply_lanman1,  PROTOCOL_LANMAN1},
382   {"MICROSOFT NETWORKS 1.03", "COREPLUS", reply_coreplus, PROTOCOL_COREPLUS},
383   {"PC NETWORK PROGRAM 1.0",  "CORE",     reply_corep,    PROTOCOL_CORE}, 
384   {NULL,NULL,NULL,0},
385 };
386
387
388 /****************************************************************************
389   reply to a negprot
390 ****************************************************************************/
391 int reply_negprot(connection_struct *conn, 
392                   char *inbuf,char *outbuf, int dum_size, 
393                   int dum_buffsize)
394 {
395   int outsize = set_message(outbuf,1,0,True);
396   int Index=0;
397   int choice= -1;
398   int protocol;
399   char *p;
400   int bcc = SVAL(smb_buf(inbuf),-2);
401   int arch = ARCH_ALL;
402   START_PROFILE(SMBnegprot);
403
404   p = smb_buf(inbuf)+1;
405   while (p < (smb_buf(inbuf) + bcc))
406     { 
407       Index++;
408       DEBUG(3,("Requested protocol [%s]\n",p));
409       if (strcsequal(p,"Windows for Workgroups 3.1a"))
410         arch &= ( ARCH_WFWG | ARCH_WIN95 | ARCH_WINNT | ARCH_WIN2K );
411       else if (strcsequal(p,"DOS LM1.2X002"))
412         arch &= ( ARCH_WFWG | ARCH_WIN95 );
413       else if (strcsequal(p,"DOS LANMAN2.1"))
414         arch &= ( ARCH_WFWG | ARCH_WIN95 );
415       else if (strcsequal(p,"NT LM 0.12"))
416         arch &= ( ARCH_WIN95 | ARCH_WINNT | ARCH_WIN2K );
417       else if (strcsequal(p,"LANMAN2.1"))
418         arch &= ( ARCH_WINNT | ARCH_WIN2K | ARCH_OS2 );
419       else if (strcsequal(p,"LM1.2X002"))
420         arch &= ( ARCH_WINNT | ARCH_WIN2K | ARCH_OS2 );
421       else if (strcsequal(p,"MICROSOFT NETWORKS 1.03"))
422         arch &= ARCH_WINNT;
423       else if (strcsequal(p,"XENIX CORE"))
424         arch &= ( ARCH_WINNT | ARCH_OS2 );
425       else if (strcsequal(p,"Samba")) {
426         arch = ARCH_SAMBA;
427         break;
428       }
429  
430       p += strlen(p) + 2;
431     }
432     
433   switch ( arch ) {
434   case ARCH_SAMBA:
435     set_remote_arch(RA_SAMBA);
436     break;
437   case ARCH_WFWG:
438     set_remote_arch(RA_WFWG);
439     break;
440   case ARCH_WIN95:
441     set_remote_arch(RA_WIN95);
442     break;
443   case ARCH_WINNT:
444    if(SVAL(inbuf,smb_flg2)==FLAGS2_WIN2K_SIGNATURE)
445      set_remote_arch(RA_WIN2K);
446    else
447      set_remote_arch(RA_WINNT);
448     break;
449   case ARCH_WIN2K:
450     set_remote_arch(RA_WIN2K);
451     break;
452   case ARCH_OS2:
453     set_remote_arch(RA_OS2);
454     break;
455   default:
456     set_remote_arch(RA_UNKNOWN);
457     break;
458   }
459  
460   /* possibly reload - change of architecture */
461   reload_services(True);      
462     
463   /* a special case to stop password server loops */
464   if (Index == 1 && strequal(remote_machine,myhostname()) && 
465       (lp_security()==SEC_SERVER || lp_security()==SEC_DOMAIN))
466     exit_server("Password server loop!");
467   
468   /* Check for protocols, most desirable first */
469   for (protocol = 0; supported_protocols[protocol].proto_name; protocol++)
470     {
471       p = smb_buf(inbuf)+1;
472       Index = 0;
473       if ((supported_protocols[protocol].protocol_level <= lp_maxprotocol()) &&
474           (supported_protocols[protocol].protocol_level >= lp_minprotocol()))
475         while (p < (smb_buf(inbuf) + bcc))
476           { 
477             if (strequal(p,supported_protocols[protocol].proto_name))
478               choice = Index;
479             Index++;
480             p += strlen(p) + 2;
481           }
482       if(choice != -1)
483         break;
484     }
485   
486   SSVAL(outbuf,smb_vwv0,choice);
487   if(choice != -1) {
488     extern fstring remote_proto;
489     fstrcpy(remote_proto,supported_protocols[protocol].short_name);
490     reload_services(True);          
491     outsize = supported_protocols[protocol].proto_reply_fn(inbuf, outbuf);
492     DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
493   }
494   else {
495     DEBUG(0,("No protocol supported !\n"));
496   }
497   SSVAL(outbuf,smb_vwv0,choice);
498   
499   DEBUG( 5, ( "negprot index=%d\n", choice ) );
500
501   END_PROFILE(SMBnegprot);
502   return(outsize);
503 }