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/>.
27 #include "../libcli/netlogon/netlogon.h"
28 #include "../libcli/cldap/cldap.h"
29 #include "../lib/tsocket/tsocket.h"
30 #include "../libcli/security/security.h"
32 #include "nmbd/nmbd.h"
34 struct sam_database_info
{
36 uint32 serial_lo
, serial_hi
;
37 uint32 date_lo
, date_hi
;
41 * check whether the client belongs to the hosts
42 * for which initial logon should be delayed...
44 static bool delay_logon(const char *peer_name
, const char *peer_addr
)
46 const char **delay_list
= lp_init_logon_delayed_hosts();
49 if (delay_list
== NULL
) {
56 return list_match(delay_list
, (const char *)peer
, client_match
);
59 static void delayed_init_logon_handler(struct event_context
*event_ctx
,
60 struct timed_event
*te
,
64 struct packet_struct
*p
= (struct packet_struct
*)private_data
;
66 DEBUG(10, ("delayed_init_logon_handler (%lx): re-queuing packet.\n",
74 struct nmbd_proxy_logon_context
{
75 struct cldap_socket
*cldap_sock
;
78 static struct nmbd_proxy_logon_context
*global_nmbd_proxy_logon
;
80 bool initialize_nmbd_proxy_logon(void)
82 const char *cldap_server
= lp_parm_const_string(-1, "nmbd_proxy_logon",
83 "cldap_server", NULL
);
84 struct nmbd_proxy_logon_context
*ctx
;
87 char addrstr
[INET_ADDRSTRLEN
];
88 const char *server_str
;
90 struct tsocket_address
*server_addr
;
96 addr
= interpret_addr2(cldap_server
);
97 server_str
= inet_ntop(AF_INET
, &addr
,
98 addrstr
, sizeof(addrstr
));
99 if (!server_str
|| strcmp("0.0.0.0", server_str
) == 0) {
100 DEBUG(0,("Failed to resolve[%s] for nmbd_proxy_logon\n",
105 ctx
= talloc_zero(nmbd_event_context(),
106 struct nmbd_proxy_logon_context
);
111 ret
= tsocket_address_inet_from_strings(ctx
, "ipv4",
112 server_str
, LDAP_PORT
,
116 status
= map_nt_error_from_unix(errno
);
117 DEBUG(0,("Failed to create cldap tsocket_address for %s - %s\n",
118 server_str
, nt_errstr(status
)));
122 /* we create a connected udp socket */
123 status
= cldap_socket_init(ctx
, NULL
, server_addr
, &ctx
->cldap_sock
);
124 TALLOC_FREE(server_addr
);
125 if (!NT_STATUS_IS_OK(status
)) {
127 DEBUG(0,("failed to create cldap socket for %s: %s\n",
128 server_str
, nt_errstr(status
)));
132 global_nmbd_proxy_logon
= ctx
;
136 struct nmbd_proxy_logon_state
{
137 struct in_addr local_ip
;
138 struct packet_struct
*p
;
139 const char *remote_name
;
140 uint8_t remote_name_type
;
141 const char *remote_mailslot
;
142 struct nbt_netlogon_packet req
;
143 struct nbt_netlogon_response resp
;
144 struct cldap_netlogon io
;
147 static int nmbd_proxy_logon_state_destructor(struct nmbd_proxy_logon_state
*s
)
149 s
->p
->locked
= false;
154 static void nmbd_proxy_logon_done(struct tevent_req
*subreq
);
156 static void nmbd_proxy_logon(struct nmbd_proxy_logon_context
*ctx
,
157 struct in_addr local_ip
,
158 struct packet_struct
*p
,
162 struct nmbd_proxy_logon_state
*state
;
163 enum ndr_err_code ndr_err
;
164 DATA_BLOB blob
= data_blob_const(buf
, len
);
165 const char *computer_name
= NULL
;
166 const char *mailslot_name
= NULL
;
167 const char *user_name
= NULL
;
168 const char *domain_sid
= NULL
;
169 uint32_t acct_control
= 0;
170 uint32_t nt_version
= 0;
171 struct tevent_req
*subreq
;
173 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
175 state
= talloc_zero(ctx
, struct nmbd_proxy_logon_state
);
177 DEBUG(0,("failed to allocate nmbd_proxy_logon_state\n"));
181 pull_ascii_nstring(source_name
, sizeof(source_name
), dgram
->source_name
.name
);
182 state
->remote_name
= talloc_strdup(state
, source_name
);
183 state
->remote_name_type
= dgram
->source_name
.name_type
,
184 state
->local_ip
= local_ip
;
187 ndr_err
= ndr_pull_struct_blob(
188 &blob
, state
, &state
->req
,
189 (ndr_pull_flags_fn_t
)ndr_pull_nbt_netlogon_packet
);
190 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
191 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
192 DEBUG(0,("failed parse nbt_netlogon_packet: %s\n",
198 if (DEBUGLEVEL
>= 10) {
199 DEBUG(10, ("nmbd_proxy_logon:\n"));
200 NDR_PRINT_DEBUG(nbt_netlogon_packet
, &state
->req
);
203 switch (state
->req
.command
) {
204 case LOGON_SAM_LOGON_REQUEST
:
205 computer_name
= state
->req
.req
.logon
.computer_name
;
206 user_name
= state
->req
.req
.logon
.user_name
;
207 mailslot_name
= state
->req
.req
.logon
.mailslot_name
;
208 acct_control
= state
->req
.req
.logon
.acct_control
;
209 if (state
->req
.req
.logon
.sid_size
> 0) {
210 domain_sid
= dom_sid_string(state
,
211 &state
->req
.req
.logon
.sid
);
213 DEBUG(0,("failed to get a string for sid\n"));
218 nt_version
= state
->req
.req
.logon
.nt_version
;
222 /* this can't happen as the caller already checks the command */
226 state
->remote_mailslot
= mailslot_name
;
228 if (user_name
&& strlen(user_name
) == 0) {
232 if (computer_name
&& strlen(computer_name
) == 0) {
233 computer_name
= NULL
;
237 * as the socket is connected,
238 * we don't need to specify the destination
240 state
->io
.in
.dest_address
= NULL
;
241 state
->io
.in
.dest_port
= 0;
242 state
->io
.in
.realm
= NULL
;
243 state
->io
.in
.host
= computer_name
;
244 state
->io
.in
.user
= user_name
;
245 state
->io
.in
.domain_guid
= NULL
;
246 state
->io
.in
.domain_sid
= domain_sid
;
247 state
->io
.in
.acct_control
= acct_control
;
248 state
->io
.in
.version
= nt_version
;
249 state
->io
.in
.map_response
= false;
251 subreq
= cldap_netlogon_send(state
, nmbd_event_context(),
255 DEBUG(0,("failed to send cldap netlogon call\n"));
259 tevent_req_set_callback(subreq
, nmbd_proxy_logon_done
, state
);
262 state
->p
->locked
= true;
263 talloc_set_destructor(state
, nmbd_proxy_logon_state_destructor
);
266 static void nmbd_proxy_logon_done(struct tevent_req
*subreq
)
268 struct nmbd_proxy_logon_state
*state
=
269 tevent_req_callback_data(subreq
,
270 struct nmbd_proxy_logon_state
);
272 DATA_BLOB response
= data_blob_null
;
274 status
= cldap_netlogon_recv(subreq
, state
, &state
->io
);
275 if (!NT_STATUS_IS_OK(status
)) {
276 DEBUG(0,("failed to recv cldap netlogon call: %s\n",
282 status
= push_netlogon_samlogon_response(&response
, state
,
283 &state
->io
.out
.netlogon
);
284 if (!NT_STATUS_IS_OK(status
)) {
285 DEBUG(0,("failed to push netlogon_samlogon_response: %s\n",
291 send_mailslot(true, state
->remote_mailslot
,
292 (char *)response
.data
, response
.length
,
293 lp_netbios_name(), 0x0,
295 state
->remote_name_type
,
302 /****************************************************************************
303 Process a domain logon packet
304 **************************************************************************/
306 void process_logon_packet(struct packet_struct
*p
, const char *buf
,int len
,
307 const char *mailslot
)
310 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
311 struct sockaddr_storage ss
;
312 const struct sockaddr_storage
*pss
;
315 DATA_BLOB blob_in
, blob_out
;
316 enum ndr_err_code ndr_err
;
317 struct nbt_netlogon_packet request
;
318 struct nbt_netlogon_response response
;
320 const char *pdc_name
;
322 in_addr_to_sockaddr_storage(&ss
, p
->ip
);
323 pss
= iface_ip((struct sockaddr
*)&ss
);
325 DEBUG(5,("process_logon_packet:can't find outgoing interface "
326 "for packet from IP %s\n",
330 ip
= ((const struct sockaddr_in
*)pss
)->sin_addr
;
332 if (!lp_domain_logons()) {
333 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
334 logons are not enabled.\n", inet_ntoa(p
->ip
) ));
338 pull_ascii_nstring(source_name
, sizeof(source_name
), dgram
->source_name
.name
);
340 pdc_name
= talloc_asprintf(talloc_tos(), "\\\\%s", lp_netbios_name());
345 ZERO_STRUCT(request
);
347 blob_in
= data_blob_const(buf
, len
);
349 ndr_err
= ndr_pull_struct_blob(&blob_in
, talloc_tos(), &request
,
350 (ndr_pull_flags_fn_t
)ndr_pull_nbt_netlogon_packet
);
351 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
352 DEBUG(1,("process_logon_packet: Failed to pull logon packet\n"));
356 if (DEBUGLEVEL
>= 10) {
357 NDR_PRINT_DEBUG(nbt_netlogon_packet
, &request
);
360 DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n",
361 inet_ntoa(p
->ip
), request
.command
));
363 switch (request
.command
) {
364 case LOGON_REQUEST
: {
366 struct nbt_netlogon_response2 response2
;
368 DEBUG(5,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
369 request
.req
.logon0
.computer_name
, inet_ntoa(p
->ip
),
370 request
.req
.logon0
.user_name
,
371 request
.req
.logon0
.lm20_token
));
373 response2
.command
= LOGON_RESPONSE2
;
374 response2
.pdc_name
= pdc_name
;
375 response2
.lm20_token
= 0xffff;
377 response
.response_type
= NETLOGON_RESPONSE2
;
378 response
.data
.response2
= response2
;
380 status
= push_nbt_netlogon_response(&blob_out
, talloc_tos(), &response
);
381 if (!NT_STATUS_IS_OK(status
)) {
382 DEBUG(0,("process_logon_packet: failed to push packet\n"));
386 if (DEBUGLEVEL
>= 10) {
387 NDR_PRINT_DEBUG(nbt_netlogon_response2
, &response
.data
.response2
);
390 send_mailslot(True
, request
.req
.logon0
.mailslot_name
,
391 (char *)blob_out
.data
,
393 lp_netbios_name(), 0x0,
395 dgram
->source_name
.name_type
,
400 case LOGON_PRIMARY_QUERY
: {
402 struct nbt_netlogon_response_from_pdc get_pdc
;
404 if (!lp_domain_master()) {
405 /* We're not Primary Domain Controller -- ignore this */
409 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, "
410 "reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
411 request
.req
.pdc
.computer_name
,
415 NETLOGON_RESPONSE_FROM_PDC
,
416 request
.req
.pdc
.nt_version
,
417 request
.req
.pdc
.lmnt_token
,
418 request
.req
.pdc
.lm20_token
));
420 get_pdc
.command
= NETLOGON_RESPONSE_FROM_PDC
;
421 get_pdc
.pdc_name
= lp_netbios_name();
422 get_pdc
._pad
= data_blob_null
;
423 get_pdc
.unicode_pdc_name
= lp_netbios_name();
424 get_pdc
.domain_name
= lp_workgroup();
425 get_pdc
.nt_version
= NETLOGON_NT_VERSION_1
;
426 get_pdc
.lmnt_token
= 0xffff;
427 get_pdc
.lm20_token
= 0xffff;
429 response
.response_type
= NETLOGON_GET_PDC
;
430 response
.data
.get_pdc
= get_pdc
;
432 status
= push_nbt_netlogon_response(&blob_out
, talloc_tos(), &response
);
433 if (!NT_STATUS_IS_OK(status
)) {
434 DEBUG(0,("process_logon_packet: failed to push packet\n"));
438 if (DEBUGLEVEL
>= 10) {
439 NDR_PRINT_DEBUG(nbt_netlogon_response_from_pdc
, &response
.data
.get_pdc
);
442 send_mailslot(True
, request
.req
.pdc
.mailslot_name
,
443 (char *)blob_out
.data
,
445 lp_netbios_name(), 0x0,
447 dgram
->source_name
.name_type
,
453 case LOGON_SAM_LOGON_REQUEST
: {
455 bool user_unknown
= false;
457 struct netlogon_samlogon_response samlogon
;
459 if (global_nmbd_proxy_logon
) {
460 nmbd_proxy_logon(global_nmbd_proxy_logon
,
461 ip
, p
, (const uint8_t *)buf
, len
);
465 source_addr
= SMB_STRDUP(inet_ntoa(dgram
->header
.source_ip
));
466 if (source_addr
== NULL
) {
467 DEBUG(3, ("out of memory copying client"
468 " address string\n"));
472 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",
473 request
.req
.logon
.computer_name
,
475 request
.req
.logon
.user_name
,
478 LOGON_SAM_LOGON_RESPONSE
,
479 request
.req
.logon
.lmnt_token
));
481 if (!request
.req
.logon
.user_name
) {
485 /* we want the simple version unless we are an ADS PDC..which means */
486 /* never, at least for now */
488 if ((request
.req
.logon
.nt_version
< (NETLOGON_NT_VERSION_1
| NETLOGON_NT_VERSION_5
| NETLOGON_NT_VERSION_5EX_WITH_IP
)) ||
489 (SEC_ADS
!= lp_security()) || (ROLE_DOMAIN_PDC
!= lp_server_role())) {
491 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 nt4
;
493 nt4
.command
= user_unknown
? LOGON_SAM_LOGON_USER_UNKNOWN
:
494 LOGON_SAM_LOGON_RESPONSE
;
495 nt4
.pdc_name
= pdc_name
;
496 nt4
.user_name
= request
.req
.logon
.user_name
;
497 nt4
.domain_name
= lp_workgroup();
498 nt4
.nt_version
= NETLOGON_NT_VERSION_1
;
499 nt4
.lmnt_token
= 0xffff;
500 nt4
.lm20_token
= 0xffff;
502 samlogon
.ntver
= NETLOGON_NT_VERSION_1
;
503 samlogon
.data
.nt4
= nt4
;
505 if (DEBUGLEVEL
>= 10) {
506 NDR_PRINT_DEBUG(NETLOGON_SAM_LOGON_RESPONSE_NT40
, &nt4
);
512 struct NETLOGON_SAM_LOGON_RESPONSE_EX nt5_ex
;
513 struct GUID domain_guid
;
514 struct nbt_sockaddr saddr
;
516 const char *hostname
;
518 saddr
.sockaddr_family
= 2; /* AF_INET */
519 saddr
.pdc_ip
= inet_ntoa(ip
);
520 saddr
.remaining
= data_blob_talloc_zero(talloc_tos(), 8); /* ??? */
522 domain
= get_mydnsdomname(talloc_tos());
524 DEBUG(2,("get_mydnsdomname failed.\n"));
528 hostname
= get_mydnsfullname();
530 DEBUG(2,("get_mydnsfullname failed.\n"));
534 if (!secrets_fetch_domain_guid(domain
, &domain_guid
)) {
535 DEBUG(2,("Could not fetch DomainGUID for %s\n", domain
));
539 nt5_ex
.command
= user_unknown
? LOGON_SAM_LOGON_USER_UNKNOWN_EX
:
540 LOGON_SAM_LOGON_RESPONSE_EX
;
542 nt5_ex
.server_type
= NBT_SERVER_PDC
|
547 NBT_SERVER_TIMESERV
|
550 nt5_ex
.domain_uuid
= domain_guid
;
551 nt5_ex
.forest
= domain
;
552 nt5_ex
.dns_domain
= domain
;
553 nt5_ex
.pdc_dns_name
= hostname
;
554 nt5_ex
.domain_name
= lp_workgroup();
555 nt5_ex
.pdc_name
= lp_netbios_name();
556 nt5_ex
.user_name
= request
.req
.logon
.user_name
;
557 nt5_ex
.server_site
= "Default-First-Site-Name";
558 nt5_ex
.client_site
= "Default-First-Site-Name";
559 nt5_ex
.sockaddr_size
= 0x10; /* the w32 winsock addr size */
560 nt5_ex
.sockaddr
= saddr
;
561 nt5_ex
.next_closest_site
= NULL
;
562 nt5_ex
.nt_version
= NETLOGON_NT_VERSION_1
|
563 NETLOGON_NT_VERSION_5EX
|
564 NETLOGON_NT_VERSION_5EX_WITH_IP
;
565 nt5_ex
.lmnt_token
= 0xffff;
566 nt5_ex
.lm20_token
= 0xffff;
568 samlogon
.ntver
= NETLOGON_NT_VERSION_1
|
569 NETLOGON_NT_VERSION_5EX
|
570 NETLOGON_NT_VERSION_5EX_WITH_IP
;
571 samlogon
.data
.nt5_ex
= nt5_ex
;
573 if (DEBUGLEVEL
>= 10) {
574 NDR_PRINT_DEBUG(NETLOGON_SAM_LOGON_RESPONSE_EX
, &nt5_ex
);
577 #endif /* HAVE_ADS */
579 response
.response_type
= NETLOGON_SAMLOGON
;
580 response
.data
.samlogon
= samlogon
;
582 status
= push_nbt_netlogon_response(&blob_out
, talloc_tos(), &response
);
583 if (!NT_STATUS_IS_OK(status
)) {
584 DEBUG(0,("process_logon_packet: failed to push packet\n"));
590 * packets requeued after delay are marked as
593 if ((p
->locked
== False
) &&
594 (strlen(request
.req
.logon
.user_name
) == 0) &&
595 delay_logon(source_name
, source_addr
))
599 DEBUG(3, ("process_logon_packet: "
600 "delaying initial logon "
601 "reply for client %s(%s) for "
603 source_name
, source_addr
,
604 lp_init_logon_delay()));
606 when
= timeval_current_ofs_msec(lp_init_logon_delay());
608 event_add_timed(nmbd_event_context(),
611 delayed_init_logon_handler
,
614 DEBUG(3, ("process_logon_packet: "
615 "processing delayed initial "
616 "logon reply for client "
618 source_name
, source_addr
));
620 send_mailslot(true, request
.req
.logon
.mailslot_name
,
621 (char *)blob_out
.data
,
623 lp_netbios_name(), 0x0,
625 dgram
->source_name
.name_type
,
629 SAFE_FREE(source_addr
);
634 /* Announce change to UAS or SAM. Send by the domain controller when a
635 replication event is required. */
637 case NETLOGON_ANNOUNCE_UAS
:
638 DEBUG(5, ("Got NETLOGON_ANNOUNCE_UAS\n"));
642 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",