a515c2706d4c18893bd7ff16072135db7e61b789
[jra/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   char *unicomp; /* Unicode computer name. */
54
55   memset(outbuf, 0, sizeof(outbuf));
56
57   if (!lp_domain_logons())
58   {
59     DEBUG(3,("process_logon_packet: Logon packet received from IP %s and domain \
60 logons are not enabled.\n", inet_ntoa(p->ip) ));
61     return;
62   }
63
64   pstrcpy(my_name, global_myname);
65   strupper(my_name);
66
67   code = SVAL(buf,0);
68   DEBUG(1,("process_logon_packet: Logon from %s: code = %x\n", inet_ntoa(p->ip), code));
69
70   switch (code)
71   {
72     case 0:    
73     {
74       char *q = buf + 2;
75       char *machine = q;
76       char *user = skip_string(machine,1);
77
78       getdc = skip_string(user,1);
79       q = skip_string(getdc,1);
80       token = SVAL(q,3);
81
82       fstrcpy(reply_name,my_name); 
83
84       DEBUG(3,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
85              machine,inet_ntoa(p->ip),user,token));
86
87       q = outbuf;
88       SSVAL(q, 0, 6);
89       q += 2;
90
91       fstrcpy(reply_name, "\\\\");
92       fstrcat(reply_name, my_name);
93       fstrcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
94
95       SSVAL(q, 0, token);
96       q += 2;
97
98       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
99
100       send_mailslot(True, getdc, 
101                     outbuf,PTR_DIFF(q,outbuf),
102                     dgram->dest_name.name,
103                     dgram->dest_name.name_type,
104                     dgram->source_name.name,
105                     dgram->source_name.name_type,
106                     p->ip, *iface_ip(p->ip), p->port);  
107       break;
108     }
109
110     case QUERYFORPDC:
111     {
112       char *q = buf + 2;
113       char *machine = q;
114
115       getdc = skip_string(machine,1);
116       unicomp = skip_string(getdc,1);
117
118       q = align2(unicomp, buf);
119
120       q = skip_unibuf(q, buf+len-q);
121
122       ntversion = IVAL(q, 0);
123       q += 4;
124       lmnttoken = SVAL(q, 0);
125       q += 2;
126       lm20token = SVAL(q, 0);
127       q += 2;
128
129       /* Construct reply. */
130
131       q = outbuf;
132       SSVAL(q, 0, QUERYFORPDC_R);
133       q += 2;
134
135       fstrcpy(reply_name,my_name);
136       fstrcpy(q, reply_name);
137       q = skip_string(q, 1); /* PDC name */
138
139       /* PDC and domain name */
140 #if 0
141       if (strcmp(mailslot, NT_LOGON_MAILSLOT)==0)
142 #endif
143       {
144         q = align2(q, buf);
145
146         q = ascii_to_unibuf(q, my_name, outbuf+sizeof(outbuf)-q-2);
147         q = ascii_to_unibuf(q, global_myworkgroup, outbuf+sizeof(outbuf)-q-2);
148
149         ntversion = 0x01;
150
151         SIVAL(q, 0, ntversion);
152         q += 4;
153         SSVAL(q, 0, lmnttoken);
154         q += 2;
155         SSVAL(q, 0, lm20token);
156         q += 2;
157       }
158
159       DEBUG(3,("process_logon_packet: GETDC request from %s at IP %s, \
160 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
161             machine,inet_ntoa(p->ip), reply_name, lp_workgroup(),
162             QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
163             (uint32)lm20token ));
164
165       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
166
167       send_mailslot(True, getdc,
168                   outbuf,PTR_DIFF(q,outbuf),
169                   my_name,
170                   0x0,
171                     dgram->source_name.name,
172                     dgram->source_name.name_type,
173                   p->ip, *iface_ip(p->ip), p->port);  
174       return;
175     }
176
177     case SAMLOGON:
178     {
179       char *q = buf + 2;
180
181       q += 2;
182       unicomp = q;
183       uniuser = skip_unibuf(unicomp, buf+len-q);
184       getdc = skip_unibuf(uniuser, buf+len-q);
185       q = skip_string(getdc,1);
186       q += 4; /* skip Account Control Bits */
187       domainsidsize = IVAL(q, 0);
188       q += 4;
189
190         if (domainsidsize != 0)
191         {
192                 q += domainsidsize;
193                 q += 2;
194                 q = align4(q, buf);
195         }
196
197       ntversion = IVAL(q, 0);
198       q += 4;
199       lmnttoken = SVAL(q, 0);
200       q += 2;
201       lm20token = SVAL(q, 0);
202       q += 2;
203
204       DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %x\n", domainsidsize, ntversion));
205
206       /*
207        * we respond regadless of whether the machine is in our password 
208        * database. If it isn't then we let smbd send an appropriate error.
209        * Let's ignore the SID.
210        */
211
212       fstrcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER. */
213       fstrcpy(reply_name+2,my_name); 
214
215         ntversion = 0x01;
216         lmnttoken = 0xffff;
217         lm20token = 0xffff;
218
219       if (DEBUGLVL(3))
220         {
221               fstring ascuser;
222               fstring asccomp;
223
224               unibuf_to_ascii(ascuser, uniuser, sizeof(ascuser)-1);
225               unibuf_to_ascii(asccomp, unicomp, sizeof(asccomp)-1);
226
227               DEBUGADD(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
228                           asccomp,inet_ntoa(p->ip), ascuser, reply_name,
229                           global_myworkgroup, SAMLOGON_R, lmnttoken));
230       }
231
232       /* Construct reply. */
233
234       q = outbuf;
235         if (uniuser[0] == 0)
236         {
237                 SSVAL(q, 0, SAMLOGON_UNK_R); /* user unknown */
238         }
239         else
240         {
241                 SSVAL(q, 0, SAMLOGON_R);
242         }
243       q += 2;
244
245       /* Logon server, trust account, domain */
246       q = ascii_to_unibuf(q, reply_name, outbuf+sizeof(outbuf)-q-2);
247       q = uni_strncpy(q, uniuser, outbuf+sizeof(outbuf)-q-2);
248       q = ascii_to_unibuf(q, lp_workgroup(), outbuf+sizeof(outbuf)-q-2);
249
250       SIVAL(q, 0, ntversion);
251       q += 4;
252       SSVAL(q, 0, lmnttoken);
253       q += 2;
254       SSVAL(q, 0, lm20token);
255       q += 2;
256
257       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
258
259       send_mailslot(True, getdc,
260                    outbuf,PTR_DIFF(q,outbuf),
261                   my_name,
262                   0x0,
263                     dgram->source_name.name,
264                     dgram->source_name.name_type,
265                    p->ip, *iface_ip(p->ip), p->port);  
266       break;
267     }
268
269     default:
270     {
271       DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
272       return;
273     }
274   }
275 }