This is a security audit change of the main source.
[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   BOOL add_slashes = False;
45   pstring outbuf;
46   int code,reply_code;
47   char   unknown_byte = 0;
48   uint16 request_count = 0;
49   uint16 token = 0;
50
51   uint32 ntversion;
52   uint16 lmnttoken;
53   uint16 lm20token;
54   uint32 allowableaccount; /* Control bits, i.e. 0x80 == workstation trust a/c. */
55   uint32 domainsidsize;
56   uint16 requestcount;
57   char *domainsid;
58   char *getdc;
59   char *uniuser; /* Unicode user name. */
60   pstring ascuser;
61   char *unicomp; /* Unicode computer name. */
62   struct smb_passwd *smb_pass; /* To check if machine account exists */
63
64   if (!lp_domain_logons())
65   {
66     DEBUG(3,("process_logon_packet: Logon packet received from IP %s and domain \
67 logons are not enabled.\n", inet_ntoa(p->ip) ));
68     return;
69   }
70
71   pstrcpy(my_name, global_myname);
72   strupper(my_name);
73
74   code = SVAL(buf,0);
75   DEBUG(1,("process_logon_packet: Logon from %s: code = %x\n", inet_ntoa(p->ip), code));
76
77   switch (code)
78   {
79     case 0:    
80     {
81       char *q = buf + 2;
82       char *machine = q;
83       char *user = skip_string(machine,1);
84
85       getdc = skip_string(user,1);
86       q = skip_string(getdc,1);
87       unknown_byte = CVAL(q,0);
88       request_count = SVAL(q,1);
89       token = SVAL(q,3);
90
91       reply_code = 0x6;
92       fstrcpy(reply_name,my_name); 
93       add_slashes = True;
94
95       DEBUG(3,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
96              machine,inet_ntoa(p->ip),user,token));
97
98       q = outbuf;
99       SSVAL(q, 0, 6); q += 2;
100
101       fstrcpy(reply_name, "\\\\");
102       fstrcat(reply_name, my_name);
103       fstrcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
104
105       SSVAL(q, 0, token); q += 2;
106
107       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
108
109       send_mailslot(True, getdc, 
110                     outbuf,PTR_DIFF(q,outbuf),
111                     dgram->dest_name.name,
112                     dgram->dest_name.name_type,
113                     dgram->source_name.name,
114                     dgram->source_name.name_type,
115                     p->ip, *iface_ip(p->ip));  
116       break;
117     }
118
119     case QUERYFORPDC:
120     {
121       char *q = buf + 2;
122       char *machine = q;
123
124       getdc = skip_string(machine,1);
125       unicomp = skip_string(getdc,1);
126
127       q = align2(unicomp, buf);
128
129       q = skip_unicode_string(q, 1);
130
131       ntversion = IVAL(q, 0); q += 4;
132       lmnttoken = SVAL(q, 0); q += 2;
133       lm20token = SVAL(q, 0); q += 2;
134
135       /* Construct reply. */
136
137       q = outbuf;
138       SSVAL(q, 0, QUERYFORPDC_R); q += 2;
139
140       fstrcpy(reply_name,my_name);
141       fstrcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
142
143       if (strcmp(mailslot, NT_LOGON_MAILSLOT)==0) {
144         q = align2(q, buf);
145
146         PutUniCode(q, my_name); /* PDC name */
147         q = skip_unicode_string(q, 1); 
148         PutUniCode(q, global_myworkgroup); /* Domain name*/
149         q = skip_unicode_string(q, 1); 
150
151         SIVAL(q, 0, ntversion); q += 4;
152         SSVAL(q, 0, lmnttoken); q += 2;
153         SSVAL(q, 0, lm20token); q += 2;
154       }
155
156       DEBUG(3,("process_logon_packet: GETDC request from %s at IP %s, \
157 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(True, getdc,
165                   outbuf,PTR_DIFF(q,outbuf),
166                   dgram->dest_name.name,
167                   dgram->dest_name.name_type,
168                   dgram->source_name.name,
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,("process_logon_packet: 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       pstrcpy(ascuser, unistr(uniuser));
199       DEBUG(3,("process_logon_packet: SAMLOGON user %s\n", ascuser));
200
201       fstrcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER. */
202       fstrcpy(reply_name+2,my_name); 
203
204       smb_pass = getsampwnam(ascuser);
205
206       if(!smb_pass )
207       {
208         DEBUG(3,("process_logon_packet: 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 if(smb_pass->acct_ctrl & ACB_DISABLED)
213       {
214         DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, accound disabled.\n",
215            unistr(unicomp),inet_ntoa(p->ip), ascuser));
216         return;
217       }
218       else
219       {
220         DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
221            unistr(unicomp),inet_ntoa(p->ip), ascuser, reply_name, global_myworkgroup,
222            SAMLOGON_R ,lmnttoken));
223       }
224
225       /* Construct reply. */
226
227       q = outbuf;
228       SSVAL(q, 0, SAMLOGON_R); q += 2;
229
230       PutUniCode(q, reply_name); q = skip_unicode_string(q, 1);
231       unistrcpy(q, uniuser); q = skip_unicode_string(q, 1); /* User name (workstation trust account) */
232       PutUniCode(q, lp_workgroup()); q = skip_unicode_string(q, 1); /* Domain name. */
233
234       SIVAL(q, 0, ntversion); q += 4;
235       SSVAL(q, 0, lmnttoken); q += 2;
236       SSVAL(q, 0, lm20token); q += 2;
237
238       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
239
240       send_mailslot(True, getdc,
241                    outbuf,PTR_DIFF(q,outbuf),
242                    dgram->dest_name.name,
243                    dgram->dest_name.name_type,
244                    dgram->source_name.name,
245                    dgram->source_name.name_type,
246                    p->ip, *iface_ip(p->ip));  
247       break;
248     }
249
250     default:
251     {
252       DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
253       return;
254     }
255   }
256 }