68a0ff40007e54015c0a78ff7590beec3ed2977b
[nivanova/samba-autobuild/.git] / source / 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
48   uint32 ntversion;
49   uint16 lmnttoken;
50   uint16 lm20token;
51   uint32 domainsidsize;
52   char *getdc;
53   char *uniuser; /* Unicode user name. */
54   pstring ascuser;
55   char *unicomp; /* Unicode computer name. */
56   struct smb_passwd *smb_pass; /* To check if machine account exists */
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); 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); q += 2;
96
97       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
98
99       send_mailslot(True, getdc, 
100                     outbuf,PTR_DIFF(q,outbuf),
101                     dgram->dest_name.name,
102                     dgram->dest_name.name_type,
103                     dgram->source_name.name,
104                     dgram->source_name.name_type,
105                     p->ip, *iface_ip(p->ip), p->port);  
106       break;
107     }
108
109     case QUERYFORPDC:
110     {
111       char *q = buf + 2;
112       char *machine = q;
113
114       getdc = skip_string(machine,1);
115       unicomp = skip_string(getdc,1);
116
117       q = align2(unicomp, buf);
118
119       q = skip_unicode_string(q, 1);
120
121       ntversion = IVAL(q, 0); q += 4;
122       lmnttoken = SVAL(q, 0); q += 2;
123       lm20token = SVAL(q, 0); q += 2;
124
125       /* Construct reply. */
126
127       q = outbuf;
128       SSVAL(q, 0, QUERYFORPDC_R); q += 2;
129
130       fstrcpy(reply_name,my_name);
131       fstrcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
132
133       if (strcmp(mailslot, NT_LOGON_MAILSLOT)==0) {
134         q = align2(q, buf);
135
136         PutUniCode(q, my_name); /* PDC name */
137         q = skip_unicode_string(q, 1); 
138         PutUniCode(q, global_myworkgroup); /* Domain name*/
139         q = skip_unicode_string(q, 1); 
140
141         SIVAL(q, 0, ntversion); q += 4;
142         SSVAL(q, 0, lmnttoken); q += 2;
143         SSVAL(q, 0, lm20token); q += 2;
144       }
145
146       DEBUG(3,("process_logon_packet: GETDC request from %s at IP %s, \
147 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
148             machine,inet_ntoa(p->ip), reply_name, lp_workgroup(),
149             QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
150             (uint32)lm20token ));
151
152       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
153
154       send_mailslot(True, getdc,
155                   outbuf,PTR_DIFF(q,outbuf),
156                   dgram->dest_name.name,
157                   dgram->dest_name.name_type,
158                   dgram->source_name.name,
159                   dgram->source_name.name_type,
160                   p->ip, *iface_ip(p->ip), p->port);  
161       return;
162     }
163
164     case SAMLOGON:
165     {
166       char *q = buf + 2;
167
168       unicomp = q;
169       uniuser = skip_unicode_string(unicomp,1);
170       getdc = skip_unicode_string(uniuser,1);
171       q = skip_string(getdc,1);
172       domainsidsize = IVAL(q, 0); q += 4;
173       q += domainsidsize + 3;
174       ntversion = IVAL(q, 0); q += 4;
175       lmnttoken = SVAL(q, 0); q += 2;
176       lm20token = SVAL(q, 0); q += 2;
177
178       DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
179
180       /*
181        * If MACHINE$ is in our password database then respond, else ignore.
182        * Let's ignore the SID.
183        */
184
185       pstrcpy(ascuser, unistr(uniuser));
186       DEBUG(3,("process_logon_packet: SAMLOGON user %s\n", ascuser));
187
188       fstrcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER. */
189       fstrcpy(reply_name+2,my_name); 
190
191       smb_pass = getsmbpwnam(ascuser);
192
193       if(!smb_pass )
194       {
195         DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, not in password file\n",
196            unistr(unicomp),inet_ntoa(p->ip), ascuser));
197         return;
198       }
199       else if(smb_pass->acct_ctrl & ACB_DISABLED)
200       {
201         DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, accound disabled.\n",
202            unistr(unicomp),inet_ntoa(p->ip), ascuser));
203         return;
204       }
205       else
206       {
207         DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
208            unistr(unicomp),inet_ntoa(p->ip), ascuser, reply_name, global_myworkgroup,
209            SAMLOGON_R ,lmnttoken));
210       }
211
212       /* Construct reply. */
213
214       q = outbuf;
215       SSVAL(q, 0, SAMLOGON_R); q += 2;
216
217       PutUniCode(q, reply_name); q = skip_unicode_string(q, 1);
218       unistrcpy(q, uniuser); q = skip_unicode_string(q, 1); /* User name (workstation trust account) */
219       PutUniCode(q, lp_workgroup()); q = skip_unicode_string(q, 1); /* Domain name. */
220
221       SIVAL(q, 0, ntversion); q += 4;
222       SSVAL(q, 0, lmnttoken); q += 2;
223       SSVAL(q, 0, lm20token); q += 2;
224
225       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
226
227       send_mailslot(True, getdc,
228                    outbuf,PTR_DIFF(q,outbuf),
229                    dgram->dest_name.name,
230                    dgram->dest_name.name_type,
231                    dgram->source_name.name,
232                    dgram->source_name.name_type,
233                    p->ip, *iface_ip(p->ip), p->port);  
234       break;
235     }
236
237     default:
238     {
239       DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
240       return;
241     }
242   }
243 }