08e0f036077321a8b4345a580c39d04305006f14
[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 void process_logon_packet(struct packet_struct *p,char *buf,int len, 
41                           char *mailslot)
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 *getdc;
61         char *uniuser; /* Unicode user name */
62         pstring ascuser;
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 from %s: %x\n", inet_ntoa(p->ip), 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                         getdc = skip_string(user,1);
87                         q = skip_string(getdc,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, getdc, ClientDGRAM,
111                                             outbuf,PTR_DIFF(q,outbuf),
112                                             dgram->dest_name.name,
113                                             dgram->source_name.name,
114                                             dgram->dest_name.name_type,
115                                             dgram->source_name.name_type,
116                                             p->ip, *iface_ip(p->ip));  
117                         break;
118                 }
119
120                 case QUERYFORPDC:
121                 {
122                         char *q = buf + 2;
123                         char *machine = q;
124
125                         getdc = skip_string(machine,1);
126                         unicomp = skip_string(getdc,1);
127
128                         q = align2(unicomp, buf);
129
130                         q = skip_unicode_string(q, 1);
131
132                         ntversion = IVAL(q, 0); q += 4;
133                         lmnttoken = SVAL(q, 0); q += 2;
134                         lm20token = SVAL(q, 0); q += 2;
135
136                         /* construct reply */
137
138                         q = outbuf;
139                         SSVAL(q, 0, QUERYFORPDC_R); q += 2;
140
141                         strcpy(reply_name,my_name);
142                         strcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
143
144                         if (strcmp(mailslot, NT_LOGON_MAILSLOT)==0) {
145                                 q = align2(q, buf);
146
147                                 PutUniCode(q, my_name); /* PDC name */
148                                 q = skip_unicode_string(q, 1); 
149                                 PutUniCode(q, lp_workgroup()); /* Domain name*/
150                                 q = skip_unicode_string(q, 1); 
151
152                                 SIVAL(q, 0, ntversion); q += 4;
153                                 SSVAL(q, 0, lmnttoken); q += 2;
154                                 SSVAL(q, 0, lm20token); q += 2;
155                         }
156
157                         DEBUG(3,("GETDC request from %s(%s), reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
158                                   machine,inet_ntoa(p->ip), reply_name, lp_workgroup(),
159                                   QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
160                                   (uint32)lm20token));
161
162                         dump_data(4, outbuf, PTR_DIFF(q, outbuf));
163
164                         send_mailslot_reply(True, getdc,ClientDGRAM,
165                                             outbuf,PTR_DIFF(q,outbuf),
166                                             dgram->dest_name.name,
167                                             dgram->source_name.name,
168                                             dgram->dest_name.name_type,
169                                             dgram->source_name.name_type,
170                                             p->ip, *iface_ip(p->ip));  
171                         return;
172                 }
173
174                 case SAMLOGON:
175                 {
176                         char *q = buf + 2;
177
178                         requestcount = SVAL(q, 0); q += 2;
179                         unicomp = q;
180                         uniuser = skip_unicode_string(unicomp,1);
181                         getdc = skip_unicode_string(uniuser,1);
182                         q = skip_string(getdc,1);
183                         allowableaccount = IVAL(q, 0); q += 4;
184                         domainsidsize = IVAL(q, 0); q += 4;
185                         domainsid = q;
186                         q += domainsidsize + 3;
187                         ntversion = IVAL(q, 0); q += 4;
188                         lmnttoken = SVAL(q, 0); q += 2;
189                         lm20token = SVAL(q, 0); q += 2;
190
191                         DEBUG(3,("SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
192
193                         /*
194                           If MACHINE$ is in our password database then respond, else ignore.
195                           Let's ignore the SID.
196                         */
197
198                         strcpy(ascuser, unistr(uniuser));
199                         DEBUG(3,("SAMLOGON user %s\n", ascuser));
200
201                         strcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER */
202                         strcpy(reply_name+2,my_name); 
203
204                         smb_pass = get_smbpwd_entry(ascuser, 0);
205
206                         if(!smb_pass)
207                         {
208                                 DEBUG(3,("SAMLOGON request from %s(%s) for %s, not in password file\n",
209                                           unistr(unicomp),inet_ntoa(p->ip), ascuser));
210                                 return;
211                         }
212                         else
213                         {
214                                 DEBUG(3,("SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
215                                           unistr(unicomp),inet_ntoa(p->ip), ascuser, reply_name, lp_workgroup(),
216                                           SAMLOGON_R ,lmnttoken));
217                         }
218
219                         /* construct reply */
220
221                         q = outbuf;
222                         SSVAL(q, 0, SAMLOGON_R); q += 2;
223
224                         PutUniCode(q, reply_name); q = skip_unicode_string(q, 1);
225                         unistrcpy(q, uniuser); q = skip_unicode_string(q, 1); /* User name (workstation trust account) */
226                         PutUniCode(q, lp_workgroup()); q = skip_unicode_string(q, 1); /* Domain name. */
227
228                         SIVAL(q, 0, ntversion); q += 4;
229                         SSVAL(q, 0, lmnttoken); q += 2;
230                         SSVAL(q, 0, lm20token); q += 2;
231
232                         dump_data(4, outbuf, PTR_DIFF(q, outbuf));
233
234                         send_mailslot_reply(True, getdc,ClientDGRAM,
235                                             outbuf,PTR_DIFF(q,outbuf),
236                                             dgram->dest_name.name,
237                                             dgram->source_name.name,
238                                             dgram->dest_name.name_type,
239                                             dgram->source_name.name_type,
240                                             p->ip, *iface_ip(p->ip));  
241                         break;
242                 }
243
244                 default:
245                 {
246                 DEBUG(3,("Unknown domain request %d\n",code));
247                 return;
248                 }
249         }
250
251 }