fixed error check which caused domain logons to fail
[Samba.git] / source / nmbd / nmbd_processlogon.c
blobc9130fe478cb902f4fad26b9e1603211449019b7
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
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.
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.
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.
23 Revision History:
27 #include "includes.h"
29 extern pstring global_myname;
30 extern fstring global_myworkgroup;
32 /****************************************************************************
33 Process a domain logon packet
34 **************************************************************************/
36 void process_logon_packet(struct packet_struct *p,char *buf,int len,
37 char *mailslot)
39 struct dgram_packet *dgram = &p->packet.dgram;
40 pstring my_name;
41 fstring reply_name;
42 pstring outbuf;
43 int code;
44 uint16 token = 0;
45 uint32 ntversion = 0;
46 uint16 lmnttoken = 0;
47 uint16 lm20token = 0;
48 uint32 domainsidsize;
49 BOOL short_request = False;
50 char *getdc;
51 char *uniuser; /* Unicode user name. */
52 pstring ascuser;
53 char *unicomp; /* Unicode computer name. */
55 START_PROFILE(domain_logon);
56 memset(outbuf, 0, sizeof(outbuf));
58 if (!lp_domain_logons())
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 goto done;
65 pstrcpy(my_name, global_myname);
66 strupper(my_name);
68 code = SVAL(buf,0);
69 DEBUG(1,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p->ip), code));
71 switch (code)
73 case 0:
75 char *q = buf + 2;
76 char *machine = q;
77 char *user = skip_string(machine,1);
79 getdc = skip_string(user,1);
80 q = skip_string(getdc,1);
81 token = SVAL(q,3);
83 fstrcpy(reply_name,my_name);
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));
88 q = outbuf;
89 SSVAL(q, 0, 6);
90 q += 2;
92 fstrcpy(reply_name, "\\\\");
93 fstrcat(reply_name, my_name);
94 fstrcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
96 SSVAL(q, 0, token);
97 q += 2;
99 dump_data(4, outbuf, PTR_DIFF(q, outbuf));
101 send_mailslot(True, getdc,
102 outbuf,PTR_DIFF(q,outbuf),
103 global_myname, 0x0,
104 machine,
105 dgram->source_name.name_type,
106 p->ip, *iface_ip(p->ip), p->port);
107 break;
110 case QUERYFORPDC:
112 char *q = buf + 2;
113 char *machine = q;
115 if (!lp_domain_master())
117 /* We're not Primary Domain Controller -- ignore this */
118 goto done;
121 getdc = skip_string(machine,1);
122 q = skip_string(getdc,1);
123 q = ALIGN2(q, buf);
125 /* at this point we can work out if this is a W9X or NT style
126 request. Experiments show that the difference is wether the
127 packet ends here. For a W9X request we now end with a pair of
128 bytes (usually 0xFE 0xFF) whereas with NT we have two further
129 strings - the following is a simple way of detecting this */
130 if (len - PTR_DIFF(q, buf) <= 3) {
131 short_request = True;
132 } else {
133 unicomp = q;
135 /* A full length (NT style) request */
136 q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
138 if (len - PTR_DIFF(q, buf) > 8) {
139 /* with NT5 clients we can sometimes
140 get additional data - a length specificed string
141 containing the domain name, then 16 bytes of
142 data (no idea what it is) */
143 int dom_len = CVAL(q, 0);
144 q++;
145 if (dom_len != 0) {
146 q += dom_len + 1;
148 q += 16;
150 ntversion = IVAL(q, 0);
151 lmnttoken = SVAL(q, 4);
152 lm20token = SVAL(q, 6);
155 /* Construct reply. */
156 q = outbuf;
157 SSVAL(q, 0, QUERYFORPDC_R);
158 q += 2;
160 fstrcpy(reply_name,my_name);
161 fstrcpy(q, reply_name);
162 q = skip_string(q, 1); /* PDC name */
164 /* PDC and domain name */
165 if (!short_request) /* Make a full reply */
167 q = ALIGN2(q, outbuf);
169 q += dos_PutUniCode(q, my_name, sizeof(pstring), True); /* PDC name */
170 q += dos_PutUniCode(q, global_myworkgroup,sizeof(pstring), True); /* Domain name*/
172 SIVAL(q, 0, 1); /* our nt version */
173 SSVAL(q, 4, 0xffff); /* our lmnttoken */
174 SSVAL(q, 6, 0xffff); /* our lm20token */
175 q += 8;
178 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
180 DEBUG(3,("process_logon_packet: GETDC request from %s at IP %s, \
181 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
182 machine,inet_ntoa(p->ip), reply_name, global_myworkgroup,
183 QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
184 (uint32)lm20token ));
186 dump_data(4, outbuf, PTR_DIFF(q, outbuf));
188 send_mailslot(True, getdc,
189 outbuf,PTR_DIFF(q,outbuf),
190 global_myname, 0x0,
191 dgram->source_name.name,
192 dgram->source_name.name_type,
193 p->ip, *iface_ip(p->ip), p->port);
194 goto done;
197 case SAMLOGON:
199 char *q = buf + 2;
201 q += 2;
202 unicomp = q;
203 uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
204 getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
205 q = skip_string(getdc,1);
206 q += 4; /* Account Control Bits - indicating username type */
207 domainsidsize = IVAL(q, 0);
208 q += 4;
210 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize, len));
212 if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
213 q += domainsidsize;
214 q = ALIGN4(q, buf);
217 DEBUG(3,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %d\n", len, PTR_DIFF(q, buf) ));
219 if (len - PTR_DIFF(q, buf) > 8) {
220 /* with NT5 clients we can sometimes
221 get additional data - a length specificed string
222 containing the domain name, then 16 bytes of
223 data (no idea what it is) */
224 int dom_len = CVAL(q, 0);
225 q++;
226 if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
227 q += dom_len + 1;
229 q += 16;
232 ntversion = IVAL(q, 0);
233 lmnttoken = SVAL(q, 4);
234 lm20token = SVAL(q, 6);
235 q += 8;
237 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
240 * we respond regadless of whether the machine is in our password
241 * database. If it isn't then we let smbd send an appropriate error.
242 * Let's ignore the SID.
245 pstrcpy(ascuser, dos_unistr(uniuser));
246 DEBUG(3,("process_logon_packet: SAMLOGON user %s\n", ascuser));
248 fstrcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER. */
249 fstrcpy(reply_name+2,my_name);
251 DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
252 dos_unistr(unicomp),inet_ntoa(p->ip), ascuser, reply_name, global_myworkgroup,
253 SAMLOGON_R ,lmnttoken));
255 /* Construct reply. */
257 q = outbuf;
258 if (SVAL(uniuser, 0) == 0) {
259 SSVAL(q, 0, SAMLOGON_UNK_R); /* user unknown */
260 } else {
261 SSVAL(q, 0, SAMLOGON_R);
263 q += 2;
265 q += dos_PutUniCode(q, reply_name,sizeof(pstring), True);
266 q += dos_PutUniCode(q, ascuser, sizeof(pstring), True);
267 q += dos_PutUniCode(q, global_myworkgroup,sizeof(pstring), True);
269 /* tell the client what version we are */
270 SIVAL(q, 0, 1); /* our ntversion */
271 SSVAL(q, 4, 0xffff); /* our lmnttoken */
272 SSVAL(q, 6, 0xffff); /* our lm20token */
273 q += 8;
275 dump_data(4, outbuf, PTR_DIFF(q, outbuf));
277 send_mailslot(True, getdc,
278 outbuf,PTR_DIFF(q,outbuf),
279 global_myname, 0x0,
280 dgram->source_name.name,
281 dgram->source_name.name_type,
282 p->ip, *iface_ip(p->ip), p->port);
283 break;
286 default:
288 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
289 goto done;
292 done:
293 END_PROFILE(domain_logon);