JHT ==> Just tidying up for Release.
[samba.git] / source3 / namelogon.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5    Copyright (C) Andrew Tridgell 1994-1997
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    Revision History:
22
23    14 jan 96: lkcl@pires.co.uk
24    added multiple workgroup domain master support
25
26 */
27
28 #include "includes.h"
29
30 extern int ClientDGRAM;
31
32 #define TEST_CODE /* want to debug unknown browse packets */
33
34 extern int DEBUGLEVEL;
35
36 extern pstring myname;
37
38
39 /****************************************************************************
40    process a domain logon packet
41
42    **************************************************************************/
43 void process_logon_packet(struct packet_struct *p,char *buf,int len)
44 {
45   struct dgram_packet *dgram = &p->packet.dgram;
46   char *logname,*q;
47   fstring reply_name;
48   BOOL add_slashes = False;
49   pstring outbuf;
50   int code,reply_code;
51   char   unknown_byte = 0;
52   uint16 request_count = 0;
53   uint16 token = 0;
54   
55   if (!lp_domain_logons())
56     {
57       DEBUG(3,("No domain logons\n"));
58       return;
59     }
60   
61  
62   code = SVAL(buf,0);
63   switch (code)
64     {
65     case 0:    
66       {
67         char *machine = buf+2;
68         char *user = skip_string(machine,1);
69         char *tmp;
70         logname = skip_string(user,1);
71         tmp = skip_string(logname,1);
72         unknown_byte = CVAL(tmp,0);
73         request_count = SVAL(tmp,1);
74         token = SVAL(tmp,3);
75         
76         reply_code = 0x6;
77         strcpy(reply_name,myname); 
78         strupper(reply_name);
79         add_slashes = True;
80         DEBUG(3,("Domain login request from %s(%s) user=%s token=%x\n",
81                  machine,inet_ntoa(p->ip),user,token));
82         break;
83       }
84     case 7:    
85       {
86         char *machine = buf+2;
87         logname = skip_string(machine,1);
88         token = SVAL(skip_string(logname,1),0);
89         
90         strcpy(reply_name,lp_domain_controller()); 
91         if (!*reply_name)
92           {
93             /* oo! no domain controller. must be us, then */
94             strcpy(reply_name,myname); 
95             reply_code = 0xC;
96           }
97         else
98           {
99             /* refer logon request to the domain controller */
100             reply_code = 0x7;
101           }
102
103         strupper(reply_name);
104         DEBUG(3,("GETDC request from %s(%s), reporting %s 0x%x token=%x\n",
105                  machine,inet_ntoa(p->ip), reply_name, reply_code,token));
106         break;
107       }
108     default:
109       {
110         DEBUG(3,("Unknown domain request %d\n",code));
111         return;
112       }
113     }
114   
115   bzero(outbuf,sizeof(outbuf));
116   q = outbuf;
117   SSVAL(q,0,reply_code);
118   q += 2;
119   
120   if (token == 0xffff || /* LM 2.0 or later */
121       token == 0xfffe) /* WfWg networking */
122     {
123       if (add_slashes)
124         {
125           strcpy(q,"\\\\");
126           q += 2;
127         }
128       strcpy(q, reply_name); 
129       strupper(q);
130       q = skip_string(q,1);
131       
132       if (token == 0xffff) /* LM 2.0 or later */
133         {
134           SSVAL(q,0,token);
135           q += 2;
136         }
137     }
138   
139   SSVAL(q,0,0xFFFF);
140   q += 2;
141   
142   send_mailslot_reply(True, logname,ClientDGRAM,outbuf,PTR_DIFF(q,outbuf),
143                       myname,&dgram->source_name.name[0],0x20,0,p->ip,
144                       *iface_ip(p->ip));  
145 }