first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[tprouty/samba.git] / source3 / nmbd / nmbd_processlogon.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-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7    Copyright (C) Jeremy Allison 1994-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23    Revision History:
24
25 */
26
27 #include "includes.h"
28
29 extern int DEBUGLEVEL;
30
31 extern pstring global_myname;
32 extern fstring global_myworkgroup;
33
34 /****************************************************************************
35 Process a domain logon packet
36 **************************************************************************/
37
38 void process_logon_packet(struct packet_struct *p,char *buf,int len, 
39                           char *mailslot)
40 {
41   struct dgram_packet *dgram = &p->packet.dgram;
42   pstring my_name;
43   fstring reply_name;
44   pstring outbuf;
45   int code;
46   uint16 token = 0;
47   uint32 ntversion;
48   uint16 lmnttoken;
49   uint16 lm20token;
50   uint32 domainsidsize;
51   char *getdc;
52   char *uniuser; /* Unicode user name. */
53   pstring ascuser;
54   char *unicomp; /* Unicode computer name. */
55
56   memset(outbuf, 0, sizeof(outbuf));
57
58   if (!lp_domain_logons())
59   {
60     DEBUG(3,("process_logon_packet: Logon packet received from IP %s and domain \
61 logons are not enabled.\n", inet_ntoa(p->ip) ));
62     return;
63   }
64
65   pstrcpy(my_name, global_myname);
66   strupper(my_name);
67
68   code = SVAL(buf,0);
69   DEBUG(1,("process_logon_packet: Logon from %s: code = %x\n", inet_ntoa(p->ip), code));
70
71   switch (code)
72   {
73     case 0:    
74     {
75       char *q = buf + 2;
76       char *machine = q;
77       char *user = skip_string(machine,1);
78
79       getdc = skip_string(user,1);
80       q = skip_string(getdc,1);
81       token = SVAL(q,3);
82
83       fstrcpy(reply_name,my_name); 
84
85       DEBUG(3,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
86              machine,inet_ntoa(p->ip),user,token));
87
88       q = outbuf;
89       SSVAL(q, 0, 6);
90       q += 2;
91
92       fstrcpy(reply_name, "\\\\");
93       fstrcat(reply_name, my_name);
94       fstrcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
95
96       SSVAL(q, 0, token);
97       q += 2;
98
99       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
100
101       send_mailslot(True, getdc, 
102                     outbuf,PTR_DIFF(q,outbuf),
103                     dgram->dest_name.name,
104                     dgram->dest_name.name_type,
105                     dgram->source_name.name,
106                     dgram->source_name.name_type,
107                     p->ip, *iface_ip(p->ip), p->port);  
108       break;
109     }
110
111     case QUERYFORPDC:
112     {
113       char *q = buf + 2;
114       char *machine = q;
115
116       getdc = skip_string(machine,1);
117       unicomp = skip_string(getdc,1);
118
119       q = align2(unicomp, buf);
120
121       q = skip_unicode_string(q, 1);
122
123       ntversion = IVAL(q, 0);
124       q += 4;
125       lmnttoken = SVAL(q, 0);
126       q += 2;
127       lm20token = SVAL(q, 0);
128       q += 2;
129
130       /* Construct reply. */
131
132       q = outbuf;
133       SSVAL(q, 0, QUERYFORPDC_R);
134       q += 2;
135
136       fstrcpy(reply_name,my_name);
137       fstrcpy(q, reply_name);
138
139       q = skip_string(q, 1); /* PDC name */
140
141       if (strcmp(mailslot, NT_LOGON_MAILSLOT)==0) {
142         q = align2(q, buf);
143
144         dos_PutUniCode(q, my_name, sizeof(pstring)); /* PDC name */
145         q = skip_unicode_string(q, 1); 
146
147         dos_PutUniCode(q, global_myworkgroup,sizeof(pstring)); /* Domain name*/
148         q = skip_unicode_string(q, 1); 
149
150         SIVAL(q, 0, ntversion);
151         q += 4;
152         SSVAL(q, 0, lmnttoken);
153         q += 2;
154         SSVAL(q, 0, lm20token);
155         q += 2;
156       }
157
158       DEBUG(3,("process_logon_packet: GETDC request from %s at IP %s, \
159 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
160             machine,inet_ntoa(p->ip), reply_name, lp_workgroup(),
161             QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
162             (uint32)lm20token ));
163
164       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
165
166       send_mailslot(True, getdc,
167                   outbuf,PTR_DIFF(q,outbuf),
168                   dgram->dest_name.name,
169                   dgram->dest_name.name_type,
170                   dgram->source_name.name,
171                   dgram->source_name.name_type,
172                   p->ip, *iface_ip(p->ip), p->port);  
173       return;
174     }
175
176     case SAMLOGON:
177     {
178       char *q = buf + 2;
179
180       q += 2;
181       unicomp = q;
182       uniuser = skip_unicode_string(unicomp,1);
183       getdc = skip_unicode_string(uniuser,1);
184       q = skip_string(getdc,1);
185       q += 4;
186       domainsidsize = IVAL(q, 0);
187       q += 4;
188       q += domainsidsize + 3;
189       ntversion = IVAL(q, 0);
190       q += 4;
191       lmnttoken = SVAL(q, 0);
192       q += 2;
193       lm20token = SVAL(q, 0);
194       q += 2;
195
196       DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
197
198       /*
199        * we respond regadless of whether the machine is in our password 
200        * database. If it isn't then we let smbd send an appropriate error.
201        * Let's ignore the SID.
202        */
203
204       pstrcpy(ascuser, dos_unistr(uniuser));
205       DEBUG(3,("process_logon_packet: SAMLOGON user %s\n", ascuser));
206
207       fstrcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER. */
208       fstrcpy(reply_name+2,my_name); 
209
210       DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
211                dos_unistr(unicomp),inet_ntoa(p->ip), ascuser, reply_name, global_myworkgroup,
212                SAMLOGON_R ,lmnttoken));
213
214       /* Construct reply. */
215
216       q = outbuf;
217       SSVAL(q, 0, SAMLOGON_R);
218       q += 2;
219
220       dos_PutUniCode(q, reply_name,sizeof(pstring));
221       q = skip_unicode_string(q, 1);
222       unistrcpy(q, uniuser);
223       q = skip_unicode_string(q, 1); /* User name (workstation trust account) */
224       dos_PutUniCode(q, lp_workgroup(),sizeof(pstring));
225       q = skip_unicode_string(q, 1); /* Domain name. */
226
227       SIVAL(q, 0, ntversion);
228       q += 4;
229       SSVAL(q, 0, lmnttoken);
230       q += 2;
231       SSVAL(q, 0, lm20token);
232       q += 2;
233
234       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
235
236       send_mailslot(True, getdc,
237                    outbuf,PTR_DIFF(q,outbuf),
238                    dgram->dest_name.name,
239                    dgram->dest_name.name_type,
240                    dgram->source_name.name,
241                    dgram->source_name.name_type,
242                    p->ip, *iface_ip(p->ip), p->port);  
243       break;
244     }
245
246     default:
247     {
248       DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
249       return;
250     }
251   }
252 }