spec files needed for redhat rpm building
[kai/samba.git] / source / 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-1995
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    08aug96 lkcl@pires.co.uk
43    reply_code == 0xC courtesy of jim@oxfordcc.co.uk forwarded by
44                                  lewis2@server.uwindsor.ca
45    **************************************************************************/
46 void process_logon_packet(struct packet_struct *p,char *buf,int len)
47 {
48   struct dgram_packet *dgram = &p->packet.dgram;
49   struct in_addr ip = dgram->header.source_ip;
50   struct subnet_record *d = find_subnet(ip);
51   char *logname,*q;
52   fstring reply_name;
53   BOOL add_slashes = False;
54   pstring outbuf;
55   int code,reply_code;
56   struct work_record *work;
57   
58   if (!d) return;
59   
60   if (!(work = find_workgroupstruct(d,dgram->dest_name.name, False))) 
61     return;
62   
63   if (!lp_domain_logons()) {
64     DEBUG(3,("No domain logons\n"));
65     return;
66   }
67   
68   code = SVAL(buf,0);
69   switch (code) {
70   case 0:    
71     {
72       char *machine = buf+2;
73       char *user = skip_string(machine,1);
74       logname = skip_string(user,1);
75       reply_code = 6;
76       strcpy(reply_name,myname); 
77       strupper(reply_name);
78       add_slashes = True;
79       DEBUG(3,("Domain login request from %s(%s) user=%s\n",
80                machine,inet_ntoa(p->ip),user));
81     }
82     break;
83   case 7:    
84     {
85       char *machine = buf+2;
86       logname = skip_string(machine,1);
87       reply_code = 7;
88       strcpy(reply_name,lp_domain_controller()); 
89       if (!*reply_name) {
90         strcpy(reply_name,myname); 
91         reply_code = 0xC;
92       }
93       strupper(reply_name);
94       DEBUG(3,("GETDC request from %s(%s), reporting %s 0x%2x\n",
95                machine,inet_ntoa(p->ip), reply_name, reply_code));
96     }
97     break;
98   default:
99     DEBUG(3,("Unknown domain request %d\n",code));
100     return;
101   }
102   
103   bzero(outbuf,sizeof(outbuf));
104   q = outbuf;
105   SSVAL(q,0,reply_code);
106   q += 2;
107   if (add_slashes) {
108     strcpy(q,"\\\\");
109     q += 2;
110   }
111   StrnCpy(q,reply_name,16);
112   q = skip_string(q,1);
113
114   if (reply_code == 0xC)
115   {
116    if ( PTR_DIFF (q,outbuf) & 1 )
117    {
118        q++;
119    }
120
121    PutUniCode(q,reply_name);
122    q += 2*(strlen(reply_name) + 1);
123
124    PutUniCode(q,lp_workgroup());
125    q += 2*(strlen(lp_workgroup()) + 1);
126
127    SIVAL(q,0,1);
128    q += 4;
129    SSVAL(q,0,0xFFFF);
130    q += 2;
131   }
132
133   SSVAL(q,0,0xFFFF);
134   q += 2;
135   
136   send_mailslot_reply(logname,ClientDGRAM,outbuf,PTR_DIFF(q,outbuf),
137                       myname,&dgram->source_name.name[0],0x20,0,p->ip,
138                       *iface_ip(p->ip));  
139 }