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.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
, nmbd_event_context(), NULL
,
124 server_addr
, &ctx
->cldap_sock
);
125 TALLOC_FREE(server_addr
);
126 if (!NT_STATUS_IS_OK(status
)) {
128 DEBUG(0,("failed to create cldap socket for %s: %s\n",
129 server_str
, nt_errstr(status
)));
133 global_nmbd_proxy_logon
= ctx
;
137 struct nmbd_proxy_logon_state
{
138 struct in_addr local_ip
;
139 struct packet_struct
*p
;
140 const char *remote_name
;
141 uint8_t remote_name_type
;
142 const char *remote_mailslot
;
143 struct nbt_netlogon_packet req
;
144 struct nbt_netlogon_response resp
;
145 struct cldap_netlogon io
;
148 static int nmbd_proxy_logon_state_destructor(struct nmbd_proxy_logon_state
*s
)
150 s
->p
->locked
= false;
155 static void nmbd_proxy_logon_done(struct tevent_req
*subreq
);
157 static void nmbd_proxy_logon(struct nmbd_proxy_logon_context
*ctx
,
158 struct in_addr local_ip
,
159 struct packet_struct
*p
,
163 struct nmbd_proxy_logon_state
*state
;
164 enum ndr_err_code ndr_err
;
165 DATA_BLOB blob
= data_blob_const(buf
, len
);
166 const char *computer_name
= NULL
;
167 const char *mailslot_name
= NULL
;
168 const char *user_name
= NULL
;
169 const char *domain_sid
= NULL
;
170 uint32_t acct_control
= 0;
171 uint32_t nt_version
= 0;
172 struct tevent_req
*subreq
;
174 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
176 state
= TALLOC_ZERO_P(ctx
, struct nmbd_proxy_logon_state
);
178 DEBUG(0,("failed to allocate nmbd_proxy_logon_state\n"));
182 pull_ascii_nstring(source_name
, sizeof(source_name
), dgram
->source_name
.name
);
183 state
->remote_name
= talloc_strdup(state
, source_name
);
184 state
->remote_name_type
= dgram
->source_name
.name_type
,
185 state
->local_ip
= local_ip
;
188 ndr_err
= ndr_pull_struct_blob(
189 &blob
, state
, &state
->req
,
190 (ndr_pull_flags_fn_t
)ndr_pull_nbt_netlogon_packet
);
191 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
192 NTSTATUS status
= ndr_map_error2ntstatus(ndr_err
);
193 DEBUG(0,("failed parse nbt_netlogon_packet: %s\n",
199 if (DEBUGLEVEL
>= 10) {
200 DEBUG(10, ("nmbd_proxy_logon:\n"));
201 NDR_PRINT_DEBUG(nbt_netlogon_packet
, &state
->req
);
204 switch (state
->req
.command
) {
205 case LOGON_SAM_LOGON_REQUEST
:
206 computer_name
= state
->req
.req
.logon
.computer_name
;
207 user_name
= state
->req
.req
.logon
.user_name
;
208 mailslot_name
= state
->req
.req
.logon
.mailslot_name
;
209 acct_control
= state
->req
.req
.logon
.acct_control
;
210 if (state
->req
.req
.logon
.sid_size
> 0) {
211 domain_sid
= dom_sid_string(state
,
212 &state
->req
.req
.logon
.sid
);
214 DEBUG(0,("failed to get a string for sid\n"));
219 nt_version
= state
->req
.req
.logon
.nt_version
;
223 /* this can't happen as the caller already checks the command */
227 state
->remote_mailslot
= mailslot_name
;
229 if (user_name
&& strlen(user_name
) == 0) {
233 if (computer_name
&& strlen(computer_name
) == 0) {
234 computer_name
= NULL
;
238 * as the socket is connected,
239 * we don't need to specify the destination
241 state
->io
.in
.dest_address
= NULL
;
242 state
->io
.in
.dest_port
= 0;
243 state
->io
.in
.realm
= NULL
;
244 state
->io
.in
.host
= computer_name
;
245 state
->io
.in
.user
= user_name
;
246 state
->io
.in
.domain_guid
= NULL
;
247 state
->io
.in
.domain_sid
= domain_sid
;
248 state
->io
.in
.acct_control
= acct_control
;
249 state
->io
.in
.version
= nt_version
;
250 state
->io
.in
.map_response
= false;
252 subreq
= cldap_netlogon_send(state
,
256 DEBUG(0,("failed to send cldap netlogon call\n"));
260 tevent_req_set_callback(subreq
, nmbd_proxy_logon_done
, state
);
263 state
->p
->locked
= true;
264 talloc_set_destructor(state
, nmbd_proxy_logon_state_destructor
);
267 static void nmbd_proxy_logon_done(struct tevent_req
*subreq
)
269 struct nmbd_proxy_logon_state
*state
=
270 tevent_req_callback_data(subreq
,
271 struct nmbd_proxy_logon_state
);
273 DATA_BLOB response
= data_blob_null
;
275 status
= cldap_netlogon_recv(subreq
, state
, &state
->io
);
276 if (!NT_STATUS_IS_OK(status
)) {
277 DEBUG(0,("failed to recv cldap netlogon call: %s\n",
283 status
= push_netlogon_samlogon_response(&response
, state
,
284 &state
->io
.out
.netlogon
);
285 if (!NT_STATUS_IS_OK(status
)) {
286 DEBUG(0,("failed to push netlogon_samlogon_response: %s\n",
292 send_mailslot(true, state
->remote_mailslot
,
293 (char *)response
.data
, response
.length
,
294 global_myname(), 0x0,
296 state
->remote_name_type
,
303 /****************************************************************************
304 Process a domain logon packet
305 **************************************************************************/
307 void process_logon_packet(struct packet_struct
*p
, char *buf
,int len
,
308 const char *mailslot
)
311 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
312 struct sockaddr_storage ss
;
313 const struct sockaddr_storage
*pss
;
316 DATA_BLOB blob_in
, blob_out
;
317 enum ndr_err_code ndr_err
;
318 struct nbt_netlogon_packet request
;
319 struct nbt_netlogon_response response
;
321 const char *pdc_name
;
323 in_addr_to_sockaddr_storage(&ss
, p
->ip
);
324 pss
= iface_ip((struct sockaddr
*)&ss
);
326 DEBUG(5,("process_logon_packet:can't find outgoing interface "
327 "for packet from IP %s\n",
331 ip
= ((struct sockaddr_in
*)pss
)->sin_addr
;
333 if (!lp_domain_logons()) {
334 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
335 logons are not enabled.\n", inet_ntoa(p
->ip
) ));
339 pull_ascii_nstring(source_name
, sizeof(source_name
), dgram
->source_name
.name
);
341 pdc_name
= talloc_asprintf(talloc_tos(), "\\\\%s", global_myname());
346 ZERO_STRUCT(request
);
348 blob_in
= data_blob_const(buf
, len
);
350 ndr_err
= ndr_pull_struct_blob(&blob_in
, talloc_tos(), &request
,
351 (ndr_pull_flags_fn_t
)ndr_pull_nbt_netlogon_packet
);
352 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
353 DEBUG(1,("process_logon_packet: Failed to pull logon packet\n"));
357 if (DEBUGLEVEL
>= 10) {
358 NDR_PRINT_DEBUG(nbt_netlogon_packet
, &request
);
361 DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n",
362 inet_ntoa(p
->ip
), request
.command
));
364 switch (request
.command
) {
365 case LOGON_REQUEST
: {
367 struct nbt_netlogon_response2 response2
;
369 DEBUG(5,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
370 request
.req
.logon0
.computer_name
, inet_ntoa(p
->ip
),
371 request
.req
.logon0
.user_name
,
372 request
.req
.logon0
.lm20_token
));
374 response2
.command
= LOGON_RESPONSE2
;
375 response2
.pdc_name
= pdc_name
;
376 response2
.lm20_token
= 0xffff;
378 response
.response_type
= NETLOGON_RESPONSE2
;
379 response
.data
.response2
= response2
;
381 status
= push_nbt_netlogon_response(&blob_out
, talloc_tos(), &response
);
382 if (!NT_STATUS_IS_OK(status
)) {
383 DEBUG(0,("process_logon_packet: failed to push packet\n"));
387 if (DEBUGLEVEL
>= 10) {
388 NDR_PRINT_DEBUG(nbt_netlogon_response2
, &response
.data
.response2
);
391 send_mailslot(True
, request
.req
.logon0
.mailslot_name
,
392 (char *)blob_out
.data
,
394 global_myname(), 0x0,
396 dgram
->source_name
.name_type
,
401 case LOGON_PRIMARY_QUERY
: {
403 struct nbt_netlogon_response_from_pdc get_pdc
;
405 if (!lp_domain_master()) {
406 /* We're not Primary Domain Controller -- ignore this */
410 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, "
411 "reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
412 request
.req
.pdc
.computer_name
,
416 NETLOGON_RESPONSE_FROM_PDC
,
417 request
.req
.pdc
.nt_version
,
418 request
.req
.pdc
.lmnt_token
,
419 request
.req
.pdc
.lm20_token
));
421 get_pdc
.command
= NETLOGON_RESPONSE_FROM_PDC
;
422 get_pdc
.pdc_name
= global_myname();
423 get_pdc
._pad
= data_blob_null
;
424 get_pdc
.unicode_pdc_name
= global_myname();
425 get_pdc
.domain_name
= lp_workgroup();
426 get_pdc
.nt_version
= NETLOGON_NT_VERSION_1
;
427 get_pdc
.lmnt_token
= 0xffff;
428 get_pdc
.lm20_token
= 0xffff;
430 response
.response_type
= NETLOGON_GET_PDC
;
431 response
.data
.get_pdc
= get_pdc
;
433 status
= push_nbt_netlogon_response(&blob_out
, talloc_tos(), &response
);
434 if (!NT_STATUS_IS_OK(status
)) {
435 DEBUG(0,("process_logon_packet: failed to push packet\n"));
439 if (DEBUGLEVEL
>= 10) {
440 NDR_PRINT_DEBUG(nbt_netlogon_response_from_pdc
, &response
.data
.get_pdc
);
443 send_mailslot(True
, request
.req
.pdc
.mailslot_name
,
444 (char *)blob_out
.data
,
446 global_myname(), 0x0,
448 dgram
->source_name
.name_type
,
454 case LOGON_SAM_LOGON_REQUEST
: {
456 bool user_unknown
= false;
458 struct netlogon_samlogon_response samlogon
;
460 if (global_nmbd_proxy_logon
) {
461 nmbd_proxy_logon(global_nmbd_proxy_logon
,
462 ip
, p
, (uint8_t *)buf
, len
);
466 source_addr
= SMB_STRDUP(inet_ntoa(dgram
->header
.source_ip
));
467 if (source_addr
== NULL
) {
468 DEBUG(3, ("out of memory copying client"
469 " address string\n"));
473 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",
474 request
.req
.logon
.computer_name
,
476 request
.req
.logon
.user_name
,
479 LOGON_SAM_LOGON_RESPONSE
,
480 request
.req
.logon
.lmnt_token
));
482 if (!request
.req
.logon
.user_name
) {
486 /* we want the simple version unless we are an ADS PDC..which means */
487 /* never, at least for now */
489 if ((request
.req
.logon
.nt_version
< (NETLOGON_NT_VERSION_1
| NETLOGON_NT_VERSION_5
| NETLOGON_NT_VERSION_5EX_WITH_IP
)) ||
490 (SEC_ADS
!= lp_security()) || (ROLE_DOMAIN_PDC
!= lp_server_role())) {
492 struct NETLOGON_SAM_LOGON_RESPONSE_NT40 nt4
;
494 nt4
.command
= user_unknown
? LOGON_SAM_LOGON_USER_UNKNOWN
:
495 LOGON_SAM_LOGON_RESPONSE
;
496 nt4
.pdc_name
= pdc_name
;
497 nt4
.user_name
= request
.req
.logon
.user_name
;
498 nt4
.domain_name
= lp_workgroup();
499 nt4
.nt_version
= NETLOGON_NT_VERSION_1
;
500 nt4
.lmnt_token
= 0xffff;
501 nt4
.lm20_token
= 0xffff;
503 samlogon
.ntver
= NETLOGON_NT_VERSION_1
;
504 samlogon
.data
.nt4
= nt4
;
506 if (DEBUGLEVEL
>= 10) {
507 NDR_PRINT_DEBUG(NETLOGON_SAM_LOGON_RESPONSE_NT40
, &nt4
);
513 struct NETLOGON_SAM_LOGON_RESPONSE_EX nt5_ex
;
514 struct GUID domain_guid
;
515 struct nbt_sockaddr saddr
;
517 const char *hostname
;
519 saddr
.sockaddr_family
= 2; /* AF_INET */
520 saddr
.pdc_ip
= inet_ntoa(ip
);
521 saddr
.remaining
= data_blob_talloc_zero(talloc_tos(), 8); /* ??? */
523 domain
= get_mydnsdomname(talloc_tos());
525 DEBUG(2,("get_mydnsdomname failed.\n"));
529 hostname
= get_mydnsfullname();
531 DEBUG(2,("get_mydnsfullname failed.\n"));
535 if (!secrets_fetch_domain_guid(domain
, &domain_guid
)) {
536 DEBUG(2,("Could not fetch DomainGUID for %s\n", domain
));
540 nt5_ex
.command
= user_unknown
? LOGON_SAM_LOGON_USER_UNKNOWN_EX
:
541 LOGON_SAM_LOGON_RESPONSE_EX
;
543 nt5_ex
.server_type
= NBT_SERVER_PDC
|
548 NBT_SERVER_TIMESERV
|
551 nt5_ex
.domain_uuid
= domain_guid
;
552 nt5_ex
.forest
= domain
;
553 nt5_ex
.dns_domain
= domain
;
554 nt5_ex
.pdc_dns_name
= hostname
;
555 nt5_ex
.domain_name
= lp_workgroup();
556 nt5_ex
.pdc_name
= global_myname();
557 nt5_ex
.user_name
= request
.req
.logon
.user_name
;
558 nt5_ex
.server_site
= "Default-First-Site-Name";
559 nt5_ex
.client_site
= "Default-First-Site-Name";
560 nt5_ex
.sockaddr_size
= 0x10; /* the w32 winsock addr size */
561 nt5_ex
.sockaddr
= saddr
;
562 nt5_ex
.next_closest_site
= NULL
;
563 nt5_ex
.nt_version
= NETLOGON_NT_VERSION_1
|
564 NETLOGON_NT_VERSION_5EX
|
565 NETLOGON_NT_VERSION_5EX_WITH_IP
;
566 nt5_ex
.lmnt_token
= 0xffff;
567 nt5_ex
.lm20_token
= 0xffff;
569 samlogon
.ntver
= NETLOGON_NT_VERSION_1
|
570 NETLOGON_NT_VERSION_5EX
|
571 NETLOGON_NT_VERSION_5EX_WITH_IP
;
572 samlogon
.data
.nt5_ex
= nt5_ex
;
574 if (DEBUGLEVEL
>= 10) {
575 NDR_PRINT_DEBUG(NETLOGON_SAM_LOGON_RESPONSE_EX
, &nt5_ex
);
578 #endif /* HAVE_ADS */
580 response
.response_type
= NETLOGON_SAMLOGON
;
581 response
.data
.samlogon
= samlogon
;
583 status
= push_nbt_netlogon_response(&blob_out
, talloc_tos(), &response
);
584 if (!NT_STATUS_IS_OK(status
)) {
585 DEBUG(0,("process_logon_packet: failed to push packet\n"));
591 * packets requeued after delay are marked as
594 if ((p
->locked
== False
) &&
595 (strlen(request
.req
.logon
.user_name
) == 0) &&
596 delay_logon(source_name
, source_addr
))
600 DEBUG(3, ("process_logon_packet: "
601 "delaying initial logon "
602 "reply for client %s(%s) for "
604 source_name
, source_addr
,
605 lp_init_logon_delay()));
607 when
= timeval_current_ofs(0,
608 lp_init_logon_delay() * 1000);
610 event_add_timed(nmbd_event_context(),
613 delayed_init_logon_handler
,
616 DEBUG(3, ("process_logon_packet: "
617 "processing delayed initial "
618 "logon reply for client "
620 source_name
, source_addr
));
622 send_mailslot(true, request
.req
.logon
.mailslot_name
,
623 (char *)blob_out
.data
,
625 global_myname(), 0x0,
627 dgram
->source_name
.name_type
,
631 SAFE_FREE(source_addr
);
636 /* Announce change to UAS or SAM. Send by the domain controller when a
637 replication event is required. */
639 case NETLOGON_ANNOUNCE_UAS
:
640 DEBUG(5, ("Got NETLOGON_ANNOUNCE_UAS\n"));
644 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",