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
;
35 * check whether the client belongs to the hosts
36 * for which initial logon should be delayed...
38 static bool delay_logon(const char *peer_name
, const char *peer_addr
)
40 const char **delay_list
= lp_init_logon_delayed_hosts();
43 if (delay_list
== NULL
) {
50 return list_match(delay_list
, (const char *)peer
, client_match
);
53 static void delayed_init_logon_handler(struct event_context
*event_ctx
,
54 struct timed_event
*te
,
58 struct packet_struct
*p
= (struct packet_struct
*)private_data
;
60 DEBUG(10, ("delayed_init_logon_handler (%lx): re-queuing packet.\n",
68 /****************************************************************************
69 Process a domain logon packet
70 **************************************************************************/
72 void process_logon_packet(struct packet_struct
*p
, char *buf
,int len
,
75 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
85 bool short_request
= False
;
87 char *uniuser
; /* Unicode user name. */
89 char *unicomp
; /* Unicode computer name. */
91 struct sockaddr_storage ss
;
92 const struct sockaddr_storage
*pss
;
95 in_addr_to_sockaddr_storage(&ss
, p
->ip
);
96 pss
= iface_ip((struct sockaddr
*)&ss
);
98 DEBUG(5,("process_logon_packet:can't find outgoing interface "
99 "for packet from IP %s\n",
103 ip
= ((struct sockaddr_in
*)pss
)->sin_addr
;
105 memset(outbuf
, 0, sizeof(outbuf
));
107 if (!lp_domain_logons()) {
108 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
109 logons are not enabled.\n", inet_ntoa(p
->ip
) ));
113 fstrcpy(my_name
, global_myname());
115 code
= get_safe_SVAL(buf
,len
,buf
,0,-1);
116 DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p
->ip
), code
));
121 fstring mach_str
, user_str
, getdc_str
;
124 char *user
= skip_string(buf
,len
,machine
);
126 if (!user
|| PTR_DIFF(user
, buf
) >= len
) {
127 DEBUG(0,("process_logon_packet: bad packet\n"));
130 getdc
= skip_string(buf
,len
,user
);
132 if (!getdc
|| PTR_DIFF(getdc
, buf
) >= len
) {
133 DEBUG(0,("process_logon_packet: bad packet\n"));
136 q
= skip_string(buf
,len
,getdc
);
138 if (!q
|| PTR_DIFF(q
+ 5, buf
) > len
) {
139 DEBUG(0,("process_logon_packet: bad packet\n"));
144 fstrcpy(reply_name
,my_name
);
146 pull_ascii_fstring(mach_str
, machine
);
147 pull_ascii_fstring(user_str
, user
);
148 pull_ascii_fstring(getdc_str
, getdc
);
150 DEBUG(5,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
151 mach_str
,inet_ntoa(p
->ip
),user_str
,token
));
157 fstrcpy(reply_name
, "\\\\");
158 fstrcat(reply_name
, my_name
);
159 size
= push_ascii(q
,reply_name
,
160 sizeof(outbuf
)-PTR_DIFF(q
, outbuf
),
162 if (size
== (size_t)-1) {
165 q
= skip_string(outbuf
,sizeof(outbuf
),q
); /* PDC name */
170 dump_data(4, (uint8
*)outbuf
, PTR_DIFF(q
, outbuf
));
172 send_mailslot(True
, getdc_str
,
173 outbuf
,PTR_DIFF(q
,outbuf
),
174 global_myname(), 0x0,
176 dgram
->source_name
.name_type
,
181 case LOGON_PRIMARY_QUERY
:
183 fstring mach_str
, getdc_str
;
188 if (!lp_domain_master()) {
189 /* We're not Primary Domain Controller -- ignore this */
193 getdc
= skip_string(buf
,len
,machine
);
195 if (!getdc
|| PTR_DIFF(getdc
, buf
) >= len
) {
196 DEBUG(0,("process_logon_packet: bad packet\n"));
199 q
= skip_string(buf
,len
,getdc
);
201 if (!q
|| PTR_DIFF(q
, buf
) >= len
) {
202 DEBUG(0,("process_logon_packet: bad packet\n"));
207 /* At this point we can work out if this is a W9X or NT style
208 request. Experiments show that the difference is wether the
209 packet ends here. For a W9X request we now end with a pair of
210 bytes (usually 0xFE 0xFF) whereas with NT we have two further
211 strings - the following is a simple way of detecting this */
213 if (len
- PTR_DIFF(q
, buf
) <= 3) {
214 short_request
= True
;
218 if (PTR_DIFF(q
, buf
) >= len
) {
219 DEBUG(0,("process_logon_packet: bad packet\n"));
223 /* A full length (NT style) request */
224 q
= skip_unibuf(unicomp
, PTR_DIFF(buf
+ len
, unicomp
));
226 if (PTR_DIFF(q
, buf
) >= len
) {
227 DEBUG(0,("process_logon_packet: bad packet\n"));
231 if (len
- PTR_DIFF(q
, buf
) > 8) {
232 /* with NT5 clients we can sometimes
233 get additional data - a length specificed string
234 containing the domain name, then 16 bytes of
235 data (no idea what it is) */
236 int dom_len
= CVAL(q
, 0);
244 if (PTR_DIFF(q
+ 8, buf
) > len
) {
245 DEBUG(0,("process_logon_packet: bad packet\n"));
249 ntversion
= IVAL(q
, 0);
250 lmnttoken
= SVAL(q
, 4);
251 lm20token
= SVAL(q
, 6);
254 /* Construct reply. */
256 SSVAL(q
, 0, NETLOGON_RESPONSE_FROM_PDC
);
259 fstrcpy(reply_name
,my_name
);
260 size
= push_ascii(q
, reply_name
,
261 sizeof(outbuf
)-PTR_DIFF(q
, outbuf
),
263 if (size
== (size_t)-1) {
266 q
= skip_string(outbuf
,sizeof(outbuf
),q
); /* PDC name */
268 /* PDC and domain name */
269 if (!short_request
) {
270 /* Make a full reply */
271 q
= ALIGN2(q
, outbuf
);
273 q
+= dos_PutUniCode(q
, my_name
,
274 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
275 True
); /* PDC name */
276 q
+= dos_PutUniCode(q
, lp_workgroup(),
277 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
278 True
); /* Domain name*/
279 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 8) {
282 SIVAL(q
, 0, 1); /* our nt version */
283 SSVAL(q
, 4, 0xffff); /* our lmnttoken */
284 SSVAL(q
, 6, 0xffff); /* our lm20token */
288 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
290 pull_ascii_fstring(mach_str
, machine
);
292 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, \
293 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
294 mach_str
,inet_ntoa(p
->ip
), reply_name
, lp_workgroup(),
295 NETLOGON_RESPONSE_FROM_PDC
, (uint32
)ntversion
, (uint32
)lmnttoken
,
296 (uint32
)lm20token
));
298 dump_data(4, (uint8
*)outbuf
, PTR_DIFF(q
, outbuf
));
300 pull_ascii_fstring(getdc_str
, getdc
);
301 pull_ascii_nstring(source_name
, sizeof(source_name
), dgram
->source_name
.name
);
303 send_mailslot(True
, getdc_str
,
304 outbuf
,PTR_DIFF(q
,outbuf
),
305 global_myname(), 0x0,
307 dgram
->source_name
.name_type
,
312 case LOGON_SAM_LOGON_REQUEST
:
323 if (PTR_DIFF(q
, buf
) >= len
) {
324 DEBUG(0,("process_logon_packet: bad packet\n"));
329 uniuser
= skip_unibuf(unicomp
, PTR_DIFF(buf
+len
, unicomp
));
331 if (PTR_DIFF(uniuser
, buf
) >= len
) {
332 DEBUG(0,("process_logon_packet: bad packet\n"));
336 getdc
= skip_unibuf(uniuser
,PTR_DIFF(buf
+len
, uniuser
));
338 if (PTR_DIFF(getdc
, buf
) >= len
) {
339 DEBUG(0,("process_logon_packet: bad packet\n"));
343 q
= skip_string(buf
,len
,getdc
);
345 if (!q
|| PTR_DIFF(q
+ 8, buf
) >= len
) {
346 DEBUG(0,("process_logon_packet: bad packet\n"));
350 q
+= 4; /* Account Control Bits - indicating username type */
351 domainsidsize
= IVAL(q
, 0);
354 DEBUG(5,("process_logon_packet: LOGON_SAM_LOGON_REQUEST sidsize %d, len = %d\n", domainsidsize
, len
));
356 if (domainsidsize
< (len
- PTR_DIFF(q
, buf
)) && (domainsidsize
!= 0)) {
361 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len
, (unsigned long)PTR_DIFF(q
, buf
) ));
363 if (len
- PTR_DIFF(q
, buf
) > 8) {
364 /* with NT5 clients we can sometimes
365 get additional data - a length specificed string
366 containing the domain name, then 16 bytes of
367 data (no idea what it is) */
368 int dom_len
= CVAL(q
, 0);
370 if (dom_len
< (len
- PTR_DIFF(q
, buf
)) && (dom_len
!= 0)) {
376 if (PTR_DIFF(q
+ 8, buf
) > len
) {
377 DEBUG(0,("process_logon_packet: bad packet\n"));
381 ntversion
= IVAL(q
, 0);
382 lmnttoken
= SVAL(q
, 4);
383 lm20token
= SVAL(q
, 6);
386 DEBUG(3,("process_logon_packet: LOGON_SAM_LOGON_REQUEST sidsize %d ntv %d\n", domainsidsize
, ntversion
));
389 * we respond regadless of whether the machine is in our password
390 * database. If it isn't then we let smbd send an appropriate error.
391 * Let's ignore the SID.
393 pull_ucs2_fstring(ascuser
, uniuser
);
394 pull_ucs2_fstring(asccomp
, unicomp
);
395 DEBUG(5,("process_logon_packet: LOGON_SAM_LOGON_REQUEST user %s\n", ascuser
));
397 fstrcpy(reply_name
, "\\\\"); /* Here it wants \\LOGONSERVER. */
398 fstrcat(reply_name
, my_name
);
400 DEBUG(5,("process_logon_packet: LOGON_SAM_LOGON_REQUEST request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
401 asccomp
,inet_ntoa(p
->ip
), ascuser
, reply_name
, lp_workgroup(),
402 LOGON_SAM_LOGON_RESPONSE
,lmnttoken
));
404 /* Construct reply. */
407 /* we want the simple version unless we are an ADS PDC..which means */
408 /* never, at least for now */
409 if ((ntversion
< 11) || (SEC_ADS
!= lp_security()) || (ROLE_DOMAIN_PDC
!= lp_server_role())) {
410 if (SVAL(uniuser
, 0) == 0) {
411 SSVAL(q
, 0, LOGON_SAM_LOGON_USER_UNKNOWN
); /* user unknown */
413 SSVAL(q
, 0, LOGON_SAM_LOGON_RESPONSE
);
418 q
+= dos_PutUniCode(q
, reply_name
,
419 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
421 q
+= dos_PutUniCode(q
, ascuser
,
422 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
424 q
+= dos_PutUniCode(q
, lp_workgroup(),
425 sizeof(outbuf
) - PTR_DIFF(q
, outbuf
),
430 struct GUID domain_guid
;
434 char *component
, *dc
, *q1
;
437 char *saveptr
= NULL
;
439 domain
= get_mydnsdomname(talloc_tos());
442 ("get_mydnsdomname failed.\n"));
445 hostname
= get_myname(talloc_tos());
448 ("get_myname failed.\n"));
452 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 8) {
455 if (SVAL(uniuser
, 0) == 0) {
456 SIVAL(q
, 0, LOGON_SAM_LOGON_USER_UNKNOWN_EX
); /* user unknown */
458 SIVAL(q
, 0, LOGON_SAM_LOGON_RESPONSE_EX
);
462 SIVAL(q
, 0, NBT_SERVER_PDC
|NBT_SERVER_GC
|NBT_SERVER_LDAP
|NBT_SERVER_DS
|
463 NBT_SERVER_KDC
|NBT_SERVER_TIMESERV
|NBT_SERVER_CLOSEST
|NBT_SERVER_WRITABLE
);
466 /* Push Domain GUID */
467 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < UUID_FLAT_SIZE
) {
470 if (False
== secrets_fetch_domain_guid(domain
, &domain_guid
)) {
471 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain
));
475 smb_uuid_pack(domain_guid
, &flat_guid
);
476 memcpy(q
, &flat_guid
.info
, UUID_FLAT_SIZE
);
480 str_offset
= q
- q_orig
;
483 while ((component
= strtok_r(dc
, ".", &saveptr
)) != NULL
) {
485 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 1) {
488 size
= push_ascii(&q
[1], component
,
489 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
491 if (size
== (size_t)-1 || size
> 0xff) {
499 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 4) {
506 SCVAL(q
, 0, 0xc0 | ((str_offset
>> 8) & 0x3F));
507 SCVAL(q
, 1, str_offset
& 0xFF);
511 size
= push_ascii(&q
[1], hostname
,
512 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
514 if (size
== (size_t)-1 || size
> 0xff) {
520 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 3) {
524 SCVAL(q
, 0, 0xc0 | ((str_offset
>> 8) & 0x3F));
525 SCVAL(q
, 1, str_offset
& 0xFF);
528 /* NETBIOS of domain */
529 size
= push_ascii(&q
[1], lp_workgroup(),
530 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
532 if (size
== (size_t)-1 || size
> 0xff) {
539 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 2) {
546 /* NETBIOS of hostname */
547 size
= push_ascii(&q
[1], my_name
,
548 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
550 if (size
== (size_t)-1 || size
> 0xff) {
557 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 4) {
565 if (SVAL(uniuser
, 0) != 0) {
566 size
= push_ascii(&q
[1], ascuser
,
567 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
569 if (size
== (size_t)-1 || size
> 0xff) {
578 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 1) {
581 size
= push_ascii(&q
[1], "Default-First-Site-Name",
582 sizeof(outbuf
) - PTR_DIFF(q
+1, outbuf
),
584 if (size
== (size_t)-1 || size
> 0xff) {
590 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 18) {
595 str_offset
= q
- q_orig
;
596 SCVAL(q
, 0, 0xc0 | ((str_offset
>> 8) & 0x3F));
597 SCVAL(q
, 1, str_offset
& 0xFF);
600 SCVAL(q
, 0, PTR_DIFF(q
,q1
));
601 SCVAL(q
, 1, 0x10); /* unknown */
603 SIVAL(q
, 0, 0x00000002);
604 q
+= 4; /* unknown */
605 SIVAL(q
, 0, ntohl(ip
.s_addr
));
607 SIVAL(q
, 0, 0x00000000);
608 q
+= 4; /* unknown */
609 SIVAL(q
, 0, 0x00000000);
610 q
+= 4; /* unknown */
614 if (sizeof(outbuf
) - PTR_DIFF(q
, outbuf
) < 8) {
618 /* tell the client what version we are */
619 SIVAL(q
, 0, ((ntversion
< 11) || (SEC_ADS
!= lp_security())) ? 1 : 13);
621 SSVAL(q
, 4, 0xffff); /* our lmnttoken */
622 SSVAL(q
, 6, 0xffff); /* our lm20token */
625 dump_data(4, (uint8
*)outbuf
, PTR_DIFF(q
, outbuf
));
627 pull_ascii_fstring(getdc_str
, getdc
);
628 pull_ascii_nstring(source_name
, sizeof(source_name
), dgram
->source_name
.name
);
629 source_addr
= SMB_STRDUP(inet_ntoa(dgram
->header
.source_ip
));
630 if (source_addr
== NULL
) {
631 DEBUG(3, ("out of memory copying client"
632 " address string\n"));
638 * packets requeued after delay are marked as
641 if ((p
->locked
== False
) &&
642 (strlen(ascuser
) == 0) &&
643 delay_logon(source_name
, source_addr
))
647 DEBUG(3, ("process_logon_packet: "
648 "delaying initial logon "
649 "reply for client %s(%s) for "
651 source_name
, source_addr
,
652 lp_init_logon_delay()));
654 when
= timeval_current_ofs(0,
655 lp_init_logon_delay() * 1000);
657 event_add_timed(nmbd_event_context(),
660 delayed_init_logon_handler
,
663 DEBUG(3, ("process_logon_packet: "
664 "processing delayed initial "
665 "logon reply for client "
667 source_name
, source_addr
));
670 send_mailslot(true, getdc
,
671 outbuf
,PTR_DIFF(q
,outbuf
),
672 global_myname(), 0x0,
674 dgram
->source_name
.name_type
,
678 SAFE_FREE(source_addr
);
683 /* Announce change to UAS or SAM. Send by the domain controller when a
684 replication event is required. */
686 case NETLOGON_ANNOUNCE_UAS
:
687 DEBUG(5, ("Got NETLOGON_ANNOUNCE_UAS\n"));
691 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code
));