namelogon.c :
[kai/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 extern int DEBUGLEVEL;
33
34 extern pstring myname;
35
36
37 /****************************************************************************
38    process a domain logon packet
39
40    **************************************************************************/
41 void process_logon_packet(struct packet_struct *p,char *buf,int len)
42 {
43         struct dgram_packet *dgram = &p->packet.dgram;
44         pstring my_name;
45         fstring reply_name;
46         BOOL add_slashes = False;
47         pstring outbuf;
48         int code,reply_code;
49         char   unknown_byte = 0;
50         uint16 request_count = 0;
51         uint16 token = 0;
52
53         uint32 ntversion;
54         uint16 lmnttoken;
55         uint16 lm20token;
56         uint32 allowableaccount; /* Control bits, i.e. 0x80 == workstation trust a/c */
57         uint32 domainsidsize;
58         uint16 requestcount;
59         char *domainsid;
60         char *uniuser; /* Unicode user name */
61         pstring ascuser;
62         char *mailslot;
63         char *unicomp; /* Unicode computer name */
64         struct smb_passwd *smb_pass; /* To check if machine account exists */
65
66         if (!lp_domain_logons())
67         {
68                 DEBUG(3,("No domain logons\n"));
69                 return;
70         }
71
72         strcpy(my_name, myname);
73         strupper(my_name);
74
75         code = SVAL(buf,0);
76         DEBUG(1,("namelogon: %x\n", code));
77
78         switch (code)
79         {
80                 case 0:    
81                 {
82                         char *q = buf + 2;
83                         char *machine = q;
84                         char *user = skip_string(machine,1);
85
86                         mailslot = skip_string(user,1);
87                         q = skip_string(mailslot,1);
88                         unknown_byte = CVAL(q,0);
89                         request_count = SVAL(q,1);
90                         token = SVAL(q,3);
91
92                         reply_code = 0x6;
93                         strcpy(reply_name,my_name); 
94                         add_slashes = True;
95
96                         DEBUG(3,("Domain login request from %s(%s) user=%s token=%x\n",
97                                   machine,inet_ntoa(p->ip),user,token));
98
99                         q = outbuf;
100                         SSVAL(q, 0, 6); q += 2;
101
102                         strcpy(reply_name, "\\\\");
103                         strcat(reply_name, my_name);
104                         strcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
105
106                         SSVAL(q, 0, token); q += 2;
107
108                         dump_data(4, outbuf, PTR_DIFF(q, outbuf));
109
110                         send_mailslot_reply(True, mailslot, ClientDGRAM,
111                                             outbuf,PTR_DIFF(q,outbuf),
112                                             my_name,&dgram->source_name.name[0],0x20,0,
113                                             p->ip, *iface_ip(p->ip));  
114                         break;
115                 }
116
117                 case QUERYFORPDC:
118                 {
119                         char *q = buf + 2;
120                         char *machine = q;
121                         mailslot = skip_string(machine,1);
122                         unicomp = skip_string(mailslot,1);
123
124                         q = align2(q, buf);
125
126                         q = skip_unicode_string(unicomp,1);
127
128                         ntversion = IVAL(q, 0); q += 4;
129                         lmnttoken = SVAL(q, 0); q += 2;
130                         lm20token = SVAL(q, 0); q += 2;
131
132                         /* construct reply */
133
134                         q = outbuf;
135                         SSVAL(q, 0, QUERYFORPDC_R); q += 2;
136
137                         strcpy(reply_name,my_name);
138                         strcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
139
140                         q = align2(q, buf);
141
142                         PutUniCode(q, my_name); q = skip_unicode_string(q, 1); /* PDC name */
143                         PutUniCode(q, lp_workgroup()); q = skip_unicode_string(q, 1); /* Domain name. */
144
145                         SIVAL(q, 0, ntversion); q += 4;
146                         SSVAL(q, 0, lmnttoken); q += 2;
147                         SSVAL(q, 0, lm20token); q += 2;
148
149                         DEBUG(3,("GETDC request from %s(%s), reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
150                                   machine,inet_ntoa(p->ip), reply_name, lp_workgroup(),
151                                   QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
152                                   (uint32)lm20token));
153
154                         dump_data(4, outbuf, PTR_DIFF(q, outbuf));
155
156                         send_mailslot_reply(True, mailslot,ClientDGRAM,
157                                             outbuf,PTR_DIFF(q,outbuf),
158                                             my_name,&dgram->source_name.name[0],0x20,0,
159                                             p->ip, *iface_ip(p->ip));  
160                         return;
161                 }
162
163                 case SAMLOGON:
164                 {
165                         char *q = buf + 2;
166
167                         requestcount = SVAL(q, 0); q += 2;
168                         unicomp = q;
169                         uniuser = skip_unicode_string(unicomp,1);
170                         mailslot = skip_unicode_string(uniuser,1);
171                         q = skip_string(mailslot,1);
172                         allowableaccount = IVAL(q, 0); q += 4;
173                         domainsidsize = IVAL(q, 0); q += 4;
174                         domainsid = q;
175                         q += domainsidsize + 3;
176                         ntversion = IVAL(q, 0); q += 4;
177                         lmnttoken = SVAL(q, 0); q += 2;
178                         lm20token = SVAL(q, 0); q += 2;
179                         DEBUG(3,("SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
180
181                         /*
182                           If MACHINE$ is in our password database then respond, else ignore.
183                           Let's ignore the SID.
184                         */
185                         strcpy(ascuser,unistr(uniuser));
186                         DEBUG(3,("SAMLOGON user %s\n", ascuser));
187                         strcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER */
188                         strcpy(reply_name+2,my_name); /* PAXX: Assuming we are logon svr */
189                         smb_pass = get_smbpwnam(ascuser);
190
191                         if(!smb_pass)
192                         {
193                                 DEBUG(3,("SAMLOGON request from %s(%s) for %s, not in password file\n",
194                                           unistr(unicomp),inet_ntoa(p->ip), ascuser));
195                                 return;
196                         }
197                         else
198                         {
199                                 DEBUG(3,("SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
200                                           unistr(unicomp),inet_ntoa(p->ip), ascuser, reply_name, lp_workgroup(),
201                                           SAMLOGON_R ,lmnttoken));
202                         }
203
204                         /* construct reply */
205
206                         q = outbuf;
207                         SSVAL(q, 0, SAMLOGON_R); q += 2;
208
209                         PutUniCode(q, reply_name); q = skip_unicode_string(q, 1);
210                         unistrcpy(q, uniuser); q = skip_unicode_string(q, 1); /* User name (workstation trust account) */
211                         PutUniCode(q, lp_workgroup()); q = skip_unicode_string(q, 1); /* Domain name. */
212
213                         SIVAL(q, 0, ntversion); q += 4;
214                         SSVAL(q, 0, lmnttoken); q += 2;
215                         SSVAL(q, 0, lm20token); q += 2;
216
217                         dump_data(4, outbuf, PTR_DIFF(q, outbuf));
218
219                         send_mailslot_reply(True, mailslot,ClientDGRAM,
220                                             outbuf,PTR_DIFF(q,outbuf),
221                                             my_name,&dgram->source_name.name[0],0x20,0,
222                                             p->ip, *iface_ip(p->ip));  
223                         break;
224                 }
225
226                 default:
227                 {
228                 DEBUG(3,("Unknown domain request %d\n",code));
229                 return;
230                 }
231         }
232
233 }