2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 Copyright (C) Jeremy Allison 1994-2003
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
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 3 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, see <http://www.gnu.org/licenses/>.
28 struct sam_database_info
{
30 uint32 serial_lo
, serial_hi
;
31 uint32 date_lo
, date_hi
;
34 /****************************************************************************
35 Process a domain logon packet
36 **************************************************************************/
38 void process_logon_packet(struct packet_struct
*p
, char *buf
,int len
,
41 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
51 bool short_request
= False
;
53 char *uniuser
; /* Unicode user name. */
55 char *unicomp
; /* Unicode computer name. */
57 struct sockaddr_storage ss
;
58 const struct sockaddr_storage
*pss
;
61 in_addr_to_sockaddr_storage(&ss
, p
->ip
);
64 DEBUG(5,("process_logon_packet:can't find outgoing interface "
65 "for packet from IP %s\n",
69 ip
= ((struct sockaddr_in
*)pss
)->sin_addr
;
71 memset(outbuf
, 0, sizeof(outbuf
));
73 if (!lp_domain_logons()) {
74 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
75 logons are not enabled.\n", inet_ntoa(p
->ip
) ));
79 fstrcpy(my_name
, global_myname());
81 code
= get_safe_SVAL(buf
,len
,buf
,0,-1);
82 DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p
->ip
), code
));
87 fstring mach_str
, user_str
, getdc_str
;
90 char *user
= skip_string(buf
,len
,machine
);
92 if (!user
|| PTR_DIFF(user
, buf
) >= len
) {
93 DEBUG(0,("process_logon_packet: bad packet\n"));
96 getdc
= skip_string(buf
,len
,user
);
98 if (!getdc
|| PTR_DIFF(getdc
, buf
) >= len
) {
99 DEBUG(0,("process_logon_packet: bad packet\n"));
102 q
= skip_string(buf
,len
,getdc
);
104 if (!q
|| PTR_DIFF(q
+ 5, buf
) > len
) {
105 DEBUG(0,("process_logon_packet: bad packet\n"));
110 fstrcpy(reply_name
,my_name
);
112 pull_ascii_fstring(mach_str
, machine
);
113 pull_ascii_fstring(user_str
, user
);
114 pull_ascii_fstring(getdc_str
, getdc
);
116 DEBUG(5,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
117 mach_str
,inet_ntoa(p
->ip
),user_str
,token
));
123 fstrcpy(reply_name
, "\\\\");
124 fstrcat(reply_name
, my_name
);
125 size
= push_ascii(q
,reply_name
,
126 sizeof(outbuf
)-PTR_DIFF(q
, outbuf
),
128 if (size
== (size_t)-1) {
131 q
= skip_string(outbuf
,sizeof(outbuf
),q
); /* PDC name */
136 dump_data(4, (uint8
*)outbuf
, PTR_DIFF(q
, outbuf
));
138 send_mailslot(True
, getdc_str
,
139 outbuf
,PTR_DIFF(q
,outbuf
),
140 global_myname(), 0x0,
142 dgram
->source_name
.name_type
,
149 fstring mach_str
, getdc_str
;
154 if (!lp_domain_master()) {
155 /* We're not Primary Domain Controller -- ignore this */
159 getdc
= skip_string(buf
,len
,machine
);
161 if (!getdc
|| PTR_DIFF(getdc
, buf
) >= len
) {
162 DEBUG(0,("process_logon_packet: bad packet\n"));
165 q
= skip_string(buf
,len
,getdc
);
167 if (!q
|| PTR_DIFF(q
, buf
) >= len
) {
168 DEBUG(0,("process_logon_packet: bad packet\n"));
173 /* At this point we can work out if this is a W9X or NT style
174 request. Experiments show that the difference is wether the
175 packet ends here. For a W9X request we now end with a pair of
176 bytes (usually 0xFE 0xFF) whereas with NT we have two further
177 strings - the following is a simple way of detecting this */
179 if (len
- PTR_DIFF(q
, buf
) <= 3) {
180 short_request
= True
;
184 if (PTR_DIFF(q
, buf
) >= len
) {
185 DEBUG(0,("process_logon_packet: bad packet\n"));
189 /* A full length (NT style) request */
190 q
= skip_unibuf(unicomp
, PTR_DIFF(buf
+ len
, unicomp
));
192 if (PTR_DIFF(q
, buf
) >= len
) {
193 DEBUG(0,("process_logon_packet: bad packet\n"));
197 if (len
- PTR_DIFF(q
, buf
) > 8) {
198 /* with NT5 clients we can sometimes
199 get additional data - a length specificed string
200 containing the domain name, then 16 bytes of
201 data (no idea what it is) */
202 int dom_len
= CVAL(q
, 0);
210 if (PTR_DIFF(q
+ 8, buf
) > len
) {
211 DEBUG(0,("process_logon_packet: bad packet\n"));
215 ntversion
= IVAL(q
, 0);
216 lmnttoken
= SVAL(q
, 4);
217 lm20token
= SVAL(q
, 6);
220 /* Construct reply. */
222 SSVAL(q
, 0, QUERYFORPDC_R
);
225 fstrcpy(reply_name
,my_name
);
226 size
= push_ascii(q
, reply_name
,
227 sizeof(outbuf
)-PTR_DIFF(q
, outbuf
),
229 if (size
== (size_t)-1) {
232 q
= skip_string(outbuf
,sizeof(outbuf
),q
); /* PDC name */
234 /* PDC and domain name */
235 if (!short_request
) {
236 /* Make a full reply */
237 q
= ALIGN2(q
, outbuf
);
239 q
+= dos_PutUniCode(q
, my_name
,
240 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
241 True
); /* PDC name */
242 q
+= dos_PutUniCode(q
, lp_workgroup(),
243 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
244 True
); /* Domain name*/
245 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 8) {
248 SIVAL(q
, 0, 1); /* our nt version */
249 SSVAL(q
, 4, 0xffff); /* our lmnttoken */
250 SSVAL(q
, 6, 0xffff); /* our lm20token */
254 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
256 pull_ascii_fstring(mach_str
, machine
);
258 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, \
259 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
260 mach_str
,inet_ntoa(p
->ip
), reply_name
, lp_workgroup(),
261 QUERYFORPDC_R
, (uint32
)ntversion
, (uint32
)lmnttoken
,
262 (uint32
)lm20token
));
264 dump_data(4, (uint8
*)outbuf
, PTR_DIFF(q
, outbuf
));
266 pull_ascii_fstring(getdc_str
, getdc
);
267 pull_ascii_nstring(source_name
, sizeof(source_name
), dgram
->source_name
.name
);
269 send_mailslot(True
, getdc_str
,
270 outbuf
,PTR_DIFF(q
,outbuf
),
271 global_myname(), 0x0,
273 dgram
->source_name
.name_type
,
288 if (PTR_DIFF(q
, buf
) >= len
) {
289 DEBUG(0,("process_logon_packet: bad packet\n"));
294 uniuser
= skip_unibuf(unicomp
, PTR_DIFF(buf
+len
, unicomp
));
296 if (PTR_DIFF(uniuser
, buf
) >= len
) {
297 DEBUG(0,("process_logon_packet: bad packet\n"));
301 getdc
= skip_unibuf(uniuser
,PTR_DIFF(buf
+len
, uniuser
));
303 if (PTR_DIFF(getdc
, buf
) >= len
) {
304 DEBUG(0,("process_logon_packet: bad packet\n"));
308 q
= skip_string(buf
,len
,getdc
);
310 if (!q
|| PTR_DIFF(q
+ 8, buf
) >= len
) {
311 DEBUG(0,("process_logon_packet: bad packet\n"));
315 q
+= 4; /* Account Control Bits - indicating username type */
316 domainsidsize
= IVAL(q
, 0);
319 DEBUG(5,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize
, len
));
321 if (domainsidsize
< (len
- PTR_DIFF(q
, buf
)) && (domainsidsize
!= 0)) {
326 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len
, (unsigned long)PTR_DIFF(q
, buf
) ));
328 if (len
- PTR_DIFF(q
, buf
) > 8) {
329 /* with NT5 clients we can sometimes
330 get additional data - a length specificed string
331 containing the domain name, then 16 bytes of
332 data (no idea what it is) */
333 int dom_len
= CVAL(q
, 0);
335 if (dom_len
< (len
- PTR_DIFF(q
, buf
)) && (dom_len
!= 0)) {
341 if (PTR_DIFF(q
+ 8, buf
) > len
) {
342 DEBUG(0,("process_logon_packet: bad packet\n"));
346 ntversion
= IVAL(q
, 0);
347 lmnttoken
= SVAL(q
, 4);
348 lm20token
= SVAL(q
, 6);
351 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize
, ntversion
));
354 * we respond regadless of whether the machine is in our password
355 * database. If it isn't then we let smbd send an appropriate error.
356 * Let's ignore the SID.
358 pull_ucs2_fstring(ascuser
, uniuser
);
359 pull_ucs2_fstring(asccomp
, unicomp
);
360 DEBUG(5,("process_logon_packet: SAMLOGON user %s\n", ascuser
));
362 fstrcpy(reply_name
, "\\\\"); /* Here it wants \\LOGONSERVER. */
363 fstrcat(reply_name
, my_name
);
365 DEBUG(5,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
366 asccomp
,inet_ntoa(p
->ip
), ascuser
, reply_name
, lp_workgroup(),
367 SAMLOGON_R
,lmnttoken
));
369 /* Construct reply. */
372 /* we want the simple version unless we are an ADS PDC..which means */
373 /* never, at least for now */
374 if ((ntversion
< 11) || (SEC_ADS
!= lp_security()) || (ROLE_DOMAIN_PDC
!= lp_server_role())) {
375 if (SVAL(uniuser
, 0) == 0) {
376 SSVAL(q
, 0, SAMLOGON_UNK_R
); /* user unknown */
378 SSVAL(q
, 0, SAMLOGON_R
);
383 q
+= dos_PutUniCode(q
, reply_name
,
384 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
386 q
+= dos_PutUniCode(q
, ascuser
,
387 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
389 q
+= dos_PutUniCode(q
, lp_workgroup(),
390 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
395 struct GUID domain_guid
;
399 char *component
, *dc
, *q1
;
404 domain
= get_mydnsdomname(talloc_tos());
407 ("get_mydnsdomname failed.\n"));
410 hostname
= get_myname(talloc_tos());
413 ("get_myname failed.\n"));
417 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 8) {
420 if (SVAL(uniuser
, 0) == 0) {
421 SIVAL(q
, 0, SAMLOGON_AD_UNK_R
); /* user unknown */
423 SIVAL(q
, 0, SAMLOGON_AD_R
);
427 SIVAL(q
, 0, ADS_PDC
|ADS_GC
|ADS_LDAP
|ADS_DS
|
428 ADS_KDC
|ADS_TIMESERV
|ADS_CLOSEST
|ADS_WRITABLE
);
431 /* Push Domain GUID */
432 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < UUID_FLAT_SIZE
) {
435 if (False
== secrets_fetch_domain_guid(domain
, &domain_guid
)) {
436 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain
));
440 smb_uuid_pack(domain_guid
, &flat_guid
);
441 memcpy(q
, &flat_guid
.info
, UUID_FLAT_SIZE
);
445 str_offset
= q
- q_orig
;
448 while ((component
= strtok_r(dc
, ".", &saveptr
)) != NULL
) {
450 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 1) {
453 size
= push_ascii(&q
[1], component
,
454 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
456 if (size
== (size_t)-1 || size
> 0xff) {
464 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 4) {
471 SCVAL(q
, 0, 0xc0 | ((str_offset
>> 8) & 0x3F));
472 SCVAL(q
, 1, str_offset
& 0xFF);
476 size
= push_ascii(&q
[1], hostname
,
477 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
479 if (size
== (size_t)-1 || size
> 0xff) {
485 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 3) {
489 SCVAL(q
, 0, 0xc0 | ((str_offset
>> 8) & 0x3F));
490 SCVAL(q
, 1, str_offset
& 0xFF);
493 /* NETBIOS of domain */
494 size
= push_ascii(&q
[1], lp_workgroup(),
495 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
497 if (size
== (size_t)-1 || size
> 0xff) {
504 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 2) {
511 /* NETBIOS of hostname */
512 size
= push_ascii(&q
[1], my_name
,
513 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
515 if (size
== (size_t)-1 || size
> 0xff) {
522 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 4) {
530 if (SVAL(uniuser
, 0) != 0) {
531 size
= push_ascii(&q
[1], ascuser
,
532 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
534 if (size
== (size_t)-1 || size
> 0xff) {
543 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 1) {
546 size
= push_ascii(&q
[1], "Default-First-Site-Name",
547 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
549 if (size
== (size_t)-1 || size
> 0xff) {
555 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 18) {
560 str_offset
= q
- q_orig
;
561 SCVAL(q
, 0, 0xc0 | ((str_offset
>> 8) & 0x3F));
562 SCVAL(q
, 1, str_offset
& 0xFF);
565 SCVAL(q
, 0, PTR_DIFF(q
,q1
));
566 SCVAL(q
, 1, 0x10); /* unknown */
568 SIVAL(q
, 0, 0x00000002);
569 q
+= 4; /* unknown */
570 SIVAL(q
, 0, ntohl(ip
.s_addr
));
572 SIVAL(q
, 0, 0x00000000);
573 q
+= 4; /* unknown */
574 SIVAL(q
, 0, 0x00000000);
575 q
+= 4; /* unknown */
579 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 8) {
583 /* tell the client what version we are */
584 SIVAL(q
, 0, ((ntversion
< 11) || (SEC_ADS
!= lp_security())) ? 1 : 13);
586 SSVAL(q
, 4, 0xffff); /* our lmnttoken */
587 SSVAL(q
, 6, 0xffff); /* our lm20token */
590 dump_data(4, (uint8
*)outbuf
, PTR_DIFF(q
, outbuf
));
592 pull_ascii_fstring(getdc_str
, getdc
);
593 pull_ascii_nstring(source_name
, sizeof(source_name
), dgram
->source_name
.name
);
595 send_mailslot(True
, getdc
,
596 outbuf
,PTR_DIFF(q
,outbuf
),
597 global_myname(), 0x0,
599 dgram
->source_name
.name_type
,
604 /* Announce change to UAS or SAM. Send by the domain controller when a
605 replication event is required. */
608 DEBUG(5, ("Got SAM_UAS_CHANGE\n"));
612 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code
));