2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2007.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "libads/sitename_cache.h"
24 #include "../lib/addns/dnsquery.h"
25 #include "../libcli/netlogon/netlogon.h"
26 #include "lib/async_req/async_sock.h"
27 #include "lib/tsocket/tsocket.h"
28 #include "libsmb/nmblib.h"
29 #include "../libcli/nbt/libnbt.h"
30 #include "libads/kerberos_proto.h"
32 /* nmbd.c sets this to True. */
33 bool global_in_nmbd
= False
;
35 /****************************
36 * SERVER AFFINITY ROUTINES *
37 ****************************/
39 /* Server affinity is the concept of preferring the last domain
40 controller with whom you had a successful conversation */
42 /****************************************************************************
43 ****************************************************************************/
44 #define SAFKEY_FMT "SAF/DOMAIN/%s"
46 #define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
47 #define SAFJOIN_TTL 3600
49 static char *saf_key(TALLOC_CTX
*mem_ctx
, const char *domain
)
51 return talloc_asprintf_strupper_m(mem_ctx
, SAFKEY_FMT
, domain
);
54 static char *saf_join_key(TALLOC_CTX
*mem_ctx
, const char *domain
)
56 return talloc_asprintf_strupper_m(mem_ctx
, SAFJOINKEY_FMT
, domain
);
59 /****************************************************************************
60 ****************************************************************************/
62 bool saf_store( const char *domain
, const char *servername
)
68 if ( !domain
|| !servername
) {
69 DEBUG(2,("saf_store: "
70 "Refusing to store empty domain or servername!\n"));
74 if ( (strlen(domain
) == 0) || (strlen(servername
) == 0) ) {
75 DEBUG(0,("saf_store: "
76 "refusing to store 0 length domain or servername!\n"));
80 key
= saf_key(talloc_tos(), domain
);
82 DEBUG(1, ("saf_key() failed\n"));
85 expire
= time( NULL
) + lp_parm_int(-1, "saf","ttl", SAF_TTL
);
87 DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
88 domain
, servername
, (unsigned int)expire
));
90 ret
= gencache_set( key
, servername
, expire
);
97 bool saf_join_store( const char *domain
, const char *servername
)
103 if ( !domain
|| !servername
) {
104 DEBUG(2,("saf_join_store: Refusing to store empty domain or servername!\n"));
108 if ( (strlen(domain
) == 0) || (strlen(servername
) == 0) ) {
109 DEBUG(0,("saf_join_store: refusing to store 0 length domain or servername!\n"));
113 key
= saf_join_key(talloc_tos(), domain
);
115 DEBUG(1, ("saf_join_key() failed\n"));
118 expire
= time( NULL
) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL
);
120 DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
121 domain
, servername
, (unsigned int)expire
));
123 ret
= gencache_set( key
, servername
, expire
);
130 bool saf_delete( const char *domain
)
136 DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
140 key
= saf_join_key(talloc_tos(), domain
);
142 DEBUG(1, ("saf_join_key() failed\n"));
145 ret
= gencache_del(key
);
149 DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain
));
152 key
= saf_key(talloc_tos(), domain
);
154 DEBUG(1, ("saf_key() failed\n"));
157 ret
= gencache_del(key
);
161 DEBUG(10,("saf_delete: domain = [%s]\n", domain
));
167 /****************************************************************************
168 ****************************************************************************/
170 char *saf_fetch(TALLOC_CTX
*mem_ctx
, const char *domain
)
177 if ( !domain
|| strlen(domain
) == 0) {
178 DEBUG(2,("saf_fetch: Empty domain name!\n"));
182 key
= saf_join_key(talloc_tos(), domain
);
184 DEBUG(1, ("saf_join_key() failed\n"));
188 ret
= gencache_get( key
, mem_ctx
, &server
, &timeout
);
193 DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
198 key
= saf_key(talloc_tos(), domain
);
200 DEBUG(1, ("saf_key() failed\n"));
204 ret
= gencache_get( key
, mem_ctx
, &server
, &timeout
);
209 DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
212 DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
219 static void set_socket_addr_v4(struct sockaddr_storage
*addr
)
221 if (!interpret_string_addr(addr
, lp_nbt_client_socket_address(),
222 AI_NUMERICHOST
|AI_PASSIVE
)) {
225 if (addr
->ss_family
!= AF_INET
) {
230 static struct in_addr
my_socket_addr_v4(void)
232 struct sockaddr_storage my_addr
;
233 struct sockaddr_in
*in_addr
= (struct sockaddr_in
*)((char *)&my_addr
);
235 set_socket_addr_v4(&my_addr
);
236 return in_addr
->sin_addr
;
239 /****************************************************************************
240 Generate a random trn_id.
241 ****************************************************************************/
243 static int generate_trn_id(void)
247 generate_random_buffer((uint8_t *)&id
, sizeof(id
));
249 return id
% (unsigned)0x7FFF;
252 /****************************************************************************
253 Parse a node status response into an array of structures.
254 ****************************************************************************/
256 static struct node_status
*parse_node_status(TALLOC_CTX
*mem_ctx
, char *p
,
258 struct node_status_extra
*extra
)
260 struct node_status
*ret
;
263 *num_names
= CVAL(p
,0);
268 ret
= talloc_array(mem_ctx
, struct node_status
,*num_names
);
273 for (i
=0;i
< *num_names
;i
++) {
274 StrnCpy(ret
[i
].name
,p
,15);
275 trim_char(ret
[i
].name
,'\0',' ');
276 ret
[i
].type
= CVAL(p
,15);
277 ret
[i
].flags
= p
[16];
279 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret
[i
].name
,
280 ret
[i
].type
, ret
[i
].flags
));
283 * Also, pick up the MAC address ...
286 memcpy(&extra
->mac_addr
, p
, 6); /* Fill in the mac addr */
291 struct sock_packet_read_state
{
292 struct tevent_context
*ev
;
293 enum packet_type type
;
296 struct nb_packet_reader
*reader
;
297 struct tevent_req
*reader_req
;
299 struct tdgram_context
*sock
;
300 struct tevent_req
*socket_req
;
302 struct tsocket_address
*addr
;
304 bool (*validator
)(struct packet_struct
*p
,
308 struct packet_struct
*packet
;
311 static int sock_packet_read_state_destructor(struct sock_packet_read_state
*s
);
312 static void sock_packet_read_got_packet(struct tevent_req
*subreq
);
313 static void sock_packet_read_got_socket(struct tevent_req
*subreq
);
315 static struct tevent_req
*sock_packet_read_send(
317 struct tevent_context
*ev
,
318 struct tdgram_context
*sock
,
319 struct nb_packet_reader
*reader
,
320 enum packet_type type
,
322 bool (*validator
)(struct packet_struct
*p
, void *private_data
),
325 struct tevent_req
*req
;
326 struct sock_packet_read_state
*state
;
328 req
= tevent_req_create(mem_ctx
, &state
,
329 struct sock_packet_read_state
);
333 talloc_set_destructor(state
, sock_packet_read_state_destructor
);
335 state
->reader
= reader
;
338 state
->trn_id
= trn_id
;
339 state
->validator
= validator
;
340 state
->private_data
= private_data
;
342 if (reader
!= NULL
) {
343 state
->reader_req
= nb_packet_read_send(state
, ev
, reader
);
344 if (tevent_req_nomem(state
->reader_req
, req
)) {
345 return tevent_req_post(req
, ev
);
347 tevent_req_set_callback(
348 state
->reader_req
, sock_packet_read_got_packet
, req
);
351 state
->socket_req
= tdgram_recvfrom_send(state
, ev
, state
->sock
);
352 if (tevent_req_nomem(state
->socket_req
, req
)) {
353 return tevent_req_post(req
, ev
);
355 tevent_req_set_callback(state
->socket_req
, sock_packet_read_got_socket
,
361 static int sock_packet_read_state_destructor(struct sock_packet_read_state
*s
)
363 if (s
->packet
!= NULL
) {
364 free_packet(s
->packet
);
370 static void sock_packet_read_got_packet(struct tevent_req
*subreq
)
372 struct tevent_req
*req
= tevent_req_callback_data(
373 subreq
, struct tevent_req
);
374 struct sock_packet_read_state
*state
= tevent_req_data(
375 req
, struct sock_packet_read_state
);
378 status
= nb_packet_read_recv(subreq
, &state
->packet
);
380 TALLOC_FREE(state
->reader_req
);
382 if (!NT_STATUS_IS_OK(status
)) {
383 if (state
->socket_req
!= NULL
) {
385 * Still waiting for socket
390 * Both socket and packet reader failed
392 tevent_req_nterror(req
, status
);
396 if ((state
->validator
!= NULL
) &&
397 !state
->validator(state
->packet
, state
->private_data
)) {
398 DEBUG(10, ("validator failed\n"));
400 free_packet(state
->packet
);
401 state
->packet
= NULL
;
403 state
->reader_req
= nb_packet_read_send(state
, state
->ev
,
405 if (tevent_req_nomem(state
->reader_req
, req
)) {
408 tevent_req_set_callback(
409 state
->reader_req
, sock_packet_read_got_packet
, req
);
413 TALLOC_FREE(state
->socket_req
);
414 tevent_req_done(req
);
417 static void sock_packet_read_got_socket(struct tevent_req
*subreq
)
419 struct tevent_req
*req
= tevent_req_callback_data(
420 subreq
, struct tevent_req
);
421 struct sock_packet_read_state
*state
= tevent_req_data(
422 req
, struct sock_packet_read_state
);
425 struct sockaddr_in sin
;
432 received
= tdgram_recvfrom_recv(subreq
, &err
, state
,
433 &state
->buf
, &state
->addr
);
435 TALLOC_FREE(state
->socket_req
);
437 if (received
== -1) {
438 if (state
->reader_req
!= NULL
) {
440 * Still waiting for reader
445 * Both socket and reader failed
447 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
450 ok
= tsocket_address_is_inet(state
->addr
, "ipv4");
454 ret
= tsocket_address_bsd_sockaddr(state
->addr
,
458 tevent_req_nterror(req
, map_nt_error_from_unix(errno
));
462 state
->packet
= parse_packet((char *)state
->buf
, received
, state
->type
,
463 addr
.sin
.sin_addr
, addr
.sin
.sin_port
);
464 if (state
->packet
== NULL
) {
465 DEBUG(10, ("parse_packet failed\n"));
468 if ((state
->trn_id
!= -1) &&
469 (state
->trn_id
!= packet_trn_id(state
->packet
))) {
470 DEBUG(10, ("Expected transaction id %d, got %d\n",
471 state
->trn_id
, packet_trn_id(state
->packet
)));
475 if ((state
->validator
!= NULL
) &&
476 !state
->validator(state
->packet
, state
->private_data
)) {
477 DEBUG(10, ("validator failed\n"));
481 tevent_req_done(req
);
485 if (state
->packet
!= NULL
) {
486 free_packet(state
->packet
);
487 state
->packet
= NULL
;
489 TALLOC_FREE(state
->buf
);
490 TALLOC_FREE(state
->addr
);
492 state
->socket_req
= tdgram_recvfrom_send(state
, state
->ev
, state
->sock
);
493 if (tevent_req_nomem(state
->socket_req
, req
)) {
496 tevent_req_set_callback(state
->socket_req
, sock_packet_read_got_socket
,
500 static NTSTATUS
sock_packet_read_recv(struct tevent_req
*req
,
501 struct packet_struct
**ppacket
)
503 struct sock_packet_read_state
*state
= tevent_req_data(
504 req
, struct sock_packet_read_state
);
507 if (tevent_req_is_nterror(req
, &status
)) {
510 *ppacket
= state
->packet
;
511 state
->packet
= NULL
;
515 struct nb_trans_state
{
516 struct tevent_context
*ev
;
517 struct tdgram_context
*sock
;
518 struct nb_packet_reader
*reader
;
520 struct tsocket_address
*src_addr
;
521 struct tsocket_address
*dst_addr
;
524 enum packet_type type
;
527 bool (*validator
)(struct packet_struct
*p
,
531 struct packet_struct
*packet
;
534 static int nb_trans_state_destructor(struct nb_trans_state
*s
);
535 static void nb_trans_got_reader(struct tevent_req
*subreq
);
536 static void nb_trans_done(struct tevent_req
*subreq
);
537 static void nb_trans_sent(struct tevent_req
*subreq
);
538 static void nb_trans_send_next(struct tevent_req
*subreq
);
540 static struct tevent_req
*nb_trans_send(
542 struct tevent_context
*ev
,
543 const struct sockaddr_storage
*_my_addr
,
544 const struct sockaddr_storage
*_dst_addr
,
546 uint8_t *buf
, size_t buflen
,
547 enum packet_type type
, int trn_id
,
548 bool (*validator
)(struct packet_struct
*p
,
552 const struct sockaddr
*my_addr
=
553 discard_const_p(const struct sockaddr
, _my_addr
);
554 size_t my_addr_len
= sizeof(*_my_addr
);
555 const struct sockaddr
*dst_addr
=
556 discard_const_p(const struct sockaddr
, _dst_addr
);
557 size_t dst_addr_len
= sizeof(*_dst_addr
);
558 struct tevent_req
*req
, *subreq
;
559 struct nb_trans_state
*state
;
562 req
= tevent_req_create(mem_ctx
, &state
, struct nb_trans_state
);
566 talloc_set_destructor(state
, nb_trans_state_destructor
);
569 state
->buflen
= buflen
;
571 state
->trn_id
= trn_id
;
572 state
->validator
= validator
;
573 state
->private_data
= private_data
;
575 ret
= tsocket_address_bsd_from_sockaddr(state
,
576 my_addr
, my_addr_len
,
579 tevent_req_nterror(req
, map_nt_error_from_unix(errno
));
580 return tevent_req_post(req
, ev
);
583 ret
= tsocket_address_bsd_from_sockaddr(state
,
584 dst_addr
, dst_addr_len
,
587 tevent_req_nterror(req
, map_nt_error_from_unix(errno
));
588 return tevent_req_post(req
, ev
);
591 ret
= tdgram_inet_udp_broadcast_socket(state
->src_addr
, state
,
594 tevent_req_nterror(req
, map_nt_error_from_unix(errno
));
595 return tevent_req_post(req
, ev
);
598 subreq
= nb_packet_reader_send(state
, ev
, type
, state
->trn_id
, NULL
);
599 if (tevent_req_nomem(subreq
, req
)) {
600 return tevent_req_post(req
, ev
);
602 tevent_req_set_callback(subreq
, nb_trans_got_reader
, req
);
606 static int nb_trans_state_destructor(struct nb_trans_state
*s
)
608 if (s
->packet
!= NULL
) {
609 free_packet(s
->packet
);
615 static void nb_trans_got_reader(struct tevent_req
*subreq
)
617 struct tevent_req
*req
= tevent_req_callback_data(
618 subreq
, struct tevent_req
);
619 struct nb_trans_state
*state
= tevent_req_data(
620 req
, struct nb_trans_state
);
623 status
= nb_packet_reader_recv(subreq
, state
, &state
->reader
);
626 if (!NT_STATUS_IS_OK(status
)) {
627 DEBUG(10, ("nmbd not around\n"));
628 state
->reader
= NULL
;
631 subreq
= sock_packet_read_send(
632 state
, state
->ev
, state
->sock
,
633 state
->reader
, state
->type
, state
->trn_id
,
634 state
->validator
, state
->private_data
);
635 if (tevent_req_nomem(subreq
, req
)) {
638 tevent_req_set_callback(subreq
, nb_trans_done
, req
);
640 subreq
= tdgram_sendto_send(state
, state
->ev
,
642 state
->buf
, state
->buflen
,
644 if (tevent_req_nomem(subreq
, req
)) {
647 tevent_req_set_callback(subreq
, nb_trans_sent
, req
);
650 static void nb_trans_sent(struct tevent_req
*subreq
)
652 struct tevent_req
*req
= tevent_req_callback_data(
653 subreq
, struct tevent_req
);
654 struct nb_trans_state
*state
= tevent_req_data(
655 req
, struct nb_trans_state
);
659 sent
= tdgram_sendto_recv(subreq
, &err
);
662 DEBUG(10, ("sendto failed: %s\n", strerror(err
)));
663 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
666 subreq
= tevent_wakeup_send(state
, state
->ev
,
667 timeval_current_ofs(1, 0));
668 if (tevent_req_nomem(subreq
, req
)) {
671 tevent_req_set_callback(subreq
, nb_trans_send_next
, req
);
674 static void nb_trans_send_next(struct tevent_req
*subreq
)
676 struct tevent_req
*req
= tevent_req_callback_data(
677 subreq
, struct tevent_req
);
678 struct nb_trans_state
*state
= tevent_req_data(
679 req
, struct nb_trans_state
);
682 ret
= tevent_wakeup_recv(subreq
);
685 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
688 subreq
= tdgram_sendto_send(state
, state
->ev
,
690 state
->buf
, state
->buflen
,
692 if (tevent_req_nomem(subreq
, req
)) {
695 tevent_req_set_callback(subreq
, nb_trans_sent
, req
);
698 static void nb_trans_done(struct tevent_req
*subreq
)
700 struct tevent_req
*req
= tevent_req_callback_data(
701 subreq
, struct tevent_req
);
702 struct nb_trans_state
*state
= tevent_req_data(
703 req
, struct nb_trans_state
);
706 status
= sock_packet_read_recv(subreq
, &state
->packet
);
708 if (tevent_req_nterror(req
, status
)) {
711 tevent_req_done(req
);
714 static NTSTATUS
nb_trans_recv(struct tevent_req
*req
,
715 struct packet_struct
**ppacket
)
717 struct nb_trans_state
*state
= tevent_req_data(
718 req
, struct nb_trans_state
);
721 if (tevent_req_is_nterror(req
, &status
)) {
724 *ppacket
= state
->packet
;
725 state
->packet
= NULL
;
729 /****************************************************************************
730 Do a NBT node status query on an open socket and return an array of
731 structures holding the returned names or NULL if the query failed.
732 **************************************************************************/
734 struct node_status_query_state
{
735 struct sockaddr_storage my_addr
;
736 struct sockaddr_storage addr
;
739 struct packet_struct
*packet
;
742 static int node_status_query_state_destructor(
743 struct node_status_query_state
*s
);
744 static bool node_status_query_validator(struct packet_struct
*p
,
746 static void node_status_query_done(struct tevent_req
*subreq
);
748 struct tevent_req
*node_status_query_send(TALLOC_CTX
*mem_ctx
,
749 struct tevent_context
*ev
,
750 struct nmb_name
*name
,
751 const struct sockaddr_storage
*addr
)
753 struct tevent_req
*req
, *subreq
;
754 struct node_status_query_state
*state
;
755 struct packet_struct p
;
756 struct nmb_packet
*nmb
= &p
.packet
.nmb
;
757 struct sockaddr_in
*in_addr
;
759 req
= tevent_req_create(mem_ctx
, &state
,
760 struct node_status_query_state
);
764 talloc_set_destructor(state
, node_status_query_state_destructor
);
766 if (addr
->ss_family
!= AF_INET
) {
767 /* Can't do node status to IPv6 */
768 tevent_req_nterror(req
, NT_STATUS_INVALID_ADDRESS
);
769 return tevent_req_post(req
, ev
);
773 in_addr
= (struct sockaddr_in
*)(void *)&state
->addr
;
774 in_addr
->sin_port
= htons(NMB_PORT
);
776 set_socket_addr_v4(&state
->my_addr
);
779 nmb
->header
.name_trn_id
= generate_trn_id();
780 nmb
->header
.opcode
= 0;
781 nmb
->header
.response
= false;
782 nmb
->header
.nm_flags
.bcast
= false;
783 nmb
->header
.nm_flags
.recursion_available
= false;
784 nmb
->header
.nm_flags
.recursion_desired
= false;
785 nmb
->header
.nm_flags
.trunc
= false;
786 nmb
->header
.nm_flags
.authoritative
= false;
787 nmb
->header
.rcode
= 0;
788 nmb
->header
.qdcount
= 1;
789 nmb
->header
.ancount
= 0;
790 nmb
->header
.nscount
= 0;
791 nmb
->header
.arcount
= 0;
792 nmb
->question
.question_name
= *name
;
793 nmb
->question
.question_type
= 0x21;
794 nmb
->question
.question_class
= 0x1;
796 state
->buflen
= build_packet((char *)state
->buf
, sizeof(state
->buf
),
798 if (state
->buflen
== 0) {
799 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
800 DEBUG(10, ("build_packet failed\n"));
801 return tevent_req_post(req
, ev
);
804 subreq
= nb_trans_send(state
, ev
, &state
->my_addr
, &state
->addr
, false,
805 state
->buf
, state
->buflen
,
806 NMB_PACKET
, nmb
->header
.name_trn_id
,
807 node_status_query_validator
, NULL
);
808 if (tevent_req_nomem(subreq
, req
)) {
809 DEBUG(10, ("nb_trans_send failed\n"));
810 return tevent_req_post(req
, ev
);
812 if (!tevent_req_set_endtime(req
, ev
, timeval_current_ofs(10, 0))) {
813 return tevent_req_post(req
, ev
);
815 tevent_req_set_callback(subreq
, node_status_query_done
, req
);
819 static bool node_status_query_validator(struct packet_struct
*p
,
822 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
825 if (nmb
->header
.opcode
!= 0 ||
826 nmb
->header
.nm_flags
.bcast
||
828 !nmb
->header
.ancount
||
829 nmb
->answers
->rr_type
!= 0x21) {
831 * XXXX what do we do with this? could be a redirect,
832 * but we'll discard it for the moment
839 static int node_status_query_state_destructor(
840 struct node_status_query_state
*s
)
842 if (s
->packet
!= NULL
) {
843 free_packet(s
->packet
);
849 static void node_status_query_done(struct tevent_req
*subreq
)
851 struct tevent_req
*req
= tevent_req_callback_data(
852 subreq
, struct tevent_req
);
853 struct node_status_query_state
*state
= tevent_req_data(
854 req
, struct node_status_query_state
);
857 status
= nb_trans_recv(subreq
, &state
->packet
);
859 if (tevent_req_nterror(req
, status
)) {
862 tevent_req_done(req
);
865 NTSTATUS
node_status_query_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
866 struct node_status
**pnode_status
,
868 struct node_status_extra
*extra
)
870 struct node_status_query_state
*state
= tevent_req_data(
871 req
, struct node_status_query_state
);
872 struct node_status
*node_status
;
876 if (tevent_req_is_nterror(req
, &status
)) {
879 node_status
= parse_node_status(
880 mem_ctx
, &state
->packet
->packet
.nmb
.answers
->rdata
[0],
882 if (node_status
== NULL
) {
883 return NT_STATUS_NO_MEMORY
;
885 *pnode_status
= node_status
;
886 *pnum_names
= num_names
;
890 NTSTATUS
node_status_query(TALLOC_CTX
*mem_ctx
, struct nmb_name
*name
,
891 const struct sockaddr_storage
*addr
,
892 struct node_status
**pnode_status
,
894 struct node_status_extra
*extra
)
896 TALLOC_CTX
*frame
= talloc_stackframe();
897 struct tevent_context
*ev
;
898 struct tevent_req
*req
;
899 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
901 ev
= samba_tevent_context_init(frame
);
905 req
= node_status_query_send(ev
, ev
, name
, addr
);
909 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
912 status
= node_status_query_recv(req
, mem_ctx
, pnode_status
,
919 /****************************************************************************
920 Find the first type XX name in a node status reply - used for finding
921 a servers name given its IP. Return the matched name in *name.
922 **************************************************************************/
924 bool name_status_find(const char *q_name
,
927 const struct sockaddr_storage
*to_ss
,
930 char addr
[INET6_ADDRSTRLEN
];
931 struct sockaddr_storage ss
;
932 struct node_status
*addrs
= NULL
;
933 struct nmb_name nname
;
938 if (lp_disable_netbios()) {
939 DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n",
944 print_sockaddr(addr
, sizeof(addr
), to_ss
);
946 DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name
,
949 /* Check the cache first. */
951 if (namecache_status_fetch(q_name
, q_type
, type
, to_ss
, name
)) {
955 if (to_ss
->ss_family
!= AF_INET
) {
956 /* Can't do node status to IPv6 */
960 set_socket_addr_v4(&ss
);
962 /* W2K PDC's seem not to respond to '*'#0. JRA */
963 make_nmb_name(&nname
, q_name
, q_type
);
964 status
= node_status_query(talloc_tos(), &nname
, to_ss
,
965 &addrs
, &count
, NULL
);
966 if (!NT_STATUS_IS_OK(status
)) {
970 for (i
=0;i
<count
;i
++) {
971 /* Find first one of the requested type that's not a GROUP. */
972 if (addrs
[i
].type
== type
&& ! (addrs
[i
].flags
& 0x80))
978 pull_ascii_nstring(name
, sizeof(fstring
), addrs
[i
].name
);
980 /* Store the result in the cache. */
981 /* but don't store an entry for 0x1c names here. Here we have
982 a single host and DOMAIN<0x1c> names should be a list of hosts */
984 if ( q_type
!= 0x1c ) {
985 namecache_status_store(q_name
, q_type
, type
, to_ss
, name
);
993 DEBUG(10, ("name_status_find: name %sfound", result
? "" : "not "));
996 DEBUGADD(10, (", name %s ip address is %s", name
, addr
));
1004 comparison function used by sort_addr_list
1007 static int addr_compare(const struct sockaddr_storage
*ss1
,
1008 const struct sockaddr_storage
*ss2
)
1010 int max_bits1
=0, max_bits2
=0;
1011 int num_interfaces
= iface_count();
1014 /* Sort IPv4 addresses first. */
1015 if (ss1
->ss_family
!= ss2
->ss_family
) {
1016 if (ss2
->ss_family
== AF_INET
) {
1023 /* Here we know both addresses are of the same
1026 for (i
=0;i
<num_interfaces
;i
++) {
1027 const struct sockaddr_storage
*pss
= iface_n_bcast(i
);
1028 const unsigned char *p_ss1
= NULL
;
1029 const unsigned char *p_ss2
= NULL
;
1030 const unsigned char *p_if
= NULL
;
1034 if (pss
->ss_family
!= ss1
->ss_family
) {
1035 /* Ignore interfaces of the wrong type. */
1038 if (pss
->ss_family
== AF_INET
) {
1039 p_if
= (const unsigned char *)
1040 &((const struct sockaddr_in
*)pss
)->sin_addr
;
1041 p_ss1
= (const unsigned char *)
1042 &((const struct sockaddr_in
*)ss1
)->sin_addr
;
1043 p_ss2
= (const unsigned char *)
1044 &((const struct sockaddr_in
*)ss2
)->sin_addr
;
1047 #if defined(HAVE_IPV6)
1048 if (pss
->ss_family
== AF_INET6
) {
1049 p_if
= (const unsigned char *)
1050 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
1051 p_ss1
= (const unsigned char *)
1052 &((const struct sockaddr_in6
*)ss1
)->sin6_addr
;
1053 p_ss2
= (const unsigned char *)
1054 &((const struct sockaddr_in6
*)ss2
)->sin6_addr
;
1058 if (!p_ss1
|| !p_ss2
|| !p_if
|| len
== 0) {
1061 bits1
= matching_len_bits(p_ss1
, p_if
, len
);
1062 bits2
= matching_len_bits(p_ss2
, p_if
, len
);
1063 max_bits1
= MAX(bits1
, max_bits1
);
1064 max_bits2
= MAX(bits2
, max_bits2
);
1067 /* Bias towards directly reachable IPs */
1068 if (iface_local((const struct sockaddr
*)ss1
)) {
1069 if (ss1
->ss_family
== AF_INET
) {
1075 if (iface_local((const struct sockaddr
*)ss2
)) {
1076 if (ss2
->ss_family
== AF_INET
) {
1082 return max_bits2
- max_bits1
;
1085 /*******************************************************************
1086 compare 2 ldap IPs by nearness to our interfaces - used in qsort
1087 *******************************************************************/
1089 static int ip_service_compare(struct ip_service
*ss1
, struct ip_service
*ss2
)
1093 if ((result
= addr_compare(&ss1
->ss
, &ss2
->ss
)) != 0) {
1097 if (ss1
->port
> ss2
->port
) {
1101 if (ss1
->port
< ss2
->port
) {
1109 sort an IP list so that names that are close to one of our interfaces
1110 are at the top. This prevents the problem where a WINS server returns an IP
1111 that is not reachable from our subnet as the first match
1114 static void sort_addr_list(struct sockaddr_storage
*sslist
, int count
)
1120 TYPESAFE_QSORT(sslist
, count
, addr_compare
);
1123 static void sort_service_list(struct ip_service
*servlist
, int count
)
1129 TYPESAFE_QSORT(servlist
, count
, ip_service_compare
);
1132 /**********************************************************************
1133 Remove any duplicate address/port pairs in the list
1134 *********************************************************************/
1136 int remove_duplicate_addrs2(struct ip_service
*iplist
, int count
)
1140 DEBUG(10,("remove_duplicate_addrs2: "
1141 "looking for duplicate address/port pairs\n"));
1143 /* One loop to set duplicates to a zero addr. */
1144 for ( i
=0; i
<count
; i
++ ) {
1145 if ( is_zero_addr(&iplist
[i
].ss
)) {
1149 for ( j
=i
+1; j
<count
; j
++ ) {
1150 if (sockaddr_equal((struct sockaddr
*)(void *)&iplist
[i
].ss
,
1151 (struct sockaddr
*)(void *)&iplist
[j
].ss
) &&
1152 iplist
[i
].port
== iplist
[j
].port
) {
1153 zero_sockaddr(&iplist
[j
].ss
);
1158 /* Now remove any addresses set to zero above. */
1159 for (i
= 0; i
< count
; i
++) {
1161 is_zero_addr(&iplist
[i
].ss
)) {
1165 (count
-i
-1)*sizeof(struct ip_service
));
1174 static bool prioritize_ipv4_list(struct ip_service
*iplist
, int count
)
1176 TALLOC_CTX
*frame
= talloc_stackframe();
1177 struct ip_service
*iplist_new
= talloc_array(frame
, struct ip_service
, count
);
1180 if (iplist_new
== NULL
) {
1187 /* Copy IPv4 first. */
1188 for (i
= 0; i
< count
; i
++) {
1189 if (iplist
[i
].ss
.ss_family
== AF_INET
) {
1190 iplist_new
[j
++] = iplist
[i
];
1195 for (i
= 0; i
< count
; i
++) {
1196 if (iplist
[i
].ss
.ss_family
!= AF_INET
) {
1197 iplist_new
[j
++] = iplist
[i
];
1201 memcpy(iplist
, iplist_new
, sizeof(struct ip_service
)*count
);
1206 /****************************************************************************
1207 Do a netbios name query to find someones IP.
1208 Returns an array of IP addresses or NULL if none.
1209 *count will be set to the number of addresses returned.
1210 *timed_out is set if we failed by timing out
1211 ****************************************************************************/
1213 struct name_query_state
{
1214 struct sockaddr_storage my_addr
;
1215 struct sockaddr_storage addr
;
1222 NTSTATUS validate_error
;
1225 struct sockaddr_storage
*addrs
;
1229 static bool name_query_validator(struct packet_struct
*p
, void *private_data
);
1230 static void name_query_done(struct tevent_req
*subreq
);
1232 struct tevent_req
*name_query_send(TALLOC_CTX
*mem_ctx
,
1233 struct tevent_context
*ev
,
1234 const char *name
, int name_type
,
1235 bool bcast
, bool recurse
,
1236 const struct sockaddr_storage
*addr
)
1238 struct tevent_req
*req
, *subreq
;
1239 struct name_query_state
*state
;
1240 struct packet_struct p
;
1241 struct nmb_packet
*nmb
= &p
.packet
.nmb
;
1242 struct sockaddr_in
*in_addr
;
1244 req
= tevent_req_create(mem_ctx
, &state
, struct name_query_state
);
1248 state
->bcast
= bcast
;
1250 if (addr
->ss_family
!= AF_INET
) {
1251 /* Can't do node status to IPv6 */
1252 tevent_req_nterror(req
, NT_STATUS_INVALID_ADDRESS
);
1253 return tevent_req_post(req
, ev
);
1256 if (lp_disable_netbios()) {
1257 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
1259 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
1260 return tevent_req_post(req
, ev
);
1263 state
->addr
= *addr
;
1264 in_addr
= (struct sockaddr_in
*)(void *)&state
->addr
;
1265 in_addr
->sin_port
= htons(NMB_PORT
);
1267 set_socket_addr_v4(&state
->my_addr
);
1270 nmb
->header
.name_trn_id
= generate_trn_id();
1271 nmb
->header
.opcode
= 0;
1272 nmb
->header
.response
= false;
1273 nmb
->header
.nm_flags
.bcast
= bcast
;
1274 nmb
->header
.nm_flags
.recursion_available
= false;
1275 nmb
->header
.nm_flags
.recursion_desired
= recurse
;
1276 nmb
->header
.nm_flags
.trunc
= false;
1277 nmb
->header
.nm_flags
.authoritative
= false;
1278 nmb
->header
.rcode
= 0;
1279 nmb
->header
.qdcount
= 1;
1280 nmb
->header
.ancount
= 0;
1281 nmb
->header
.nscount
= 0;
1282 nmb
->header
.arcount
= 0;
1284 make_nmb_name(&nmb
->question
.question_name
,name
,name_type
);
1286 nmb
->question
.question_type
= 0x20;
1287 nmb
->question
.question_class
= 0x1;
1289 state
->buflen
= build_packet((char *)state
->buf
, sizeof(state
->buf
),
1291 if (state
->buflen
== 0) {
1292 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
1293 DEBUG(10, ("build_packet failed\n"));
1294 return tevent_req_post(req
, ev
);
1297 subreq
= nb_trans_send(state
, ev
, &state
->my_addr
, &state
->addr
, bcast
,
1298 state
->buf
, state
->buflen
,
1299 NMB_PACKET
, nmb
->header
.name_trn_id
,
1300 name_query_validator
, state
);
1301 if (tevent_req_nomem(subreq
, req
)) {
1302 DEBUG(10, ("nb_trans_send failed\n"));
1303 return tevent_req_post(req
, ev
);
1305 tevent_req_set_callback(subreq
, name_query_done
, req
);
1309 static bool name_query_validator(struct packet_struct
*p
, void *private_data
)
1311 struct name_query_state
*state
= talloc_get_type_abort(
1312 private_data
, struct name_query_state
);
1313 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
1314 struct sockaddr_storage
*tmp_addrs
;
1315 bool got_unique_netbios_name
= false;
1318 debug_nmb_packet(p
);
1321 * If we get a Negative Name Query Response from a WINS
1322 * server, we should report it and give up.
1324 if( 0 == nmb
->header
.opcode
/* A query response */
1325 && !state
->bcast
/* from a WINS server */
1326 && nmb
->header
.rcode
/* Error returned */
1329 if( DEBUGLVL( 3 ) ) {
1330 /* Only executed if DEBUGLEVEL >= 3 */
1331 dbgtext( "Negative name query "
1332 "response, rcode 0x%02x: ",
1333 nmb
->header
.rcode
);
1334 switch( nmb
->header
.rcode
) {
1336 dbgtext("Request was invalidly formatted.\n");
1339 dbgtext("Problem with NBNS, cannot process "
1343 dbgtext("The name requested does not "
1347 dbgtext("Unsupported request error.\n");
1350 dbgtext("Query refused error.\n");
1353 dbgtext("Unrecognized error code.\n" );
1359 * We accept this packet as valid, but tell the upper
1360 * layers that it's a negative response.
1362 state
->validate_error
= NT_STATUS_NOT_FOUND
;
1366 if (nmb
->header
.opcode
!= 0 ||
1367 nmb
->header
.nm_flags
.bcast
||
1368 nmb
->header
.rcode
||
1369 !nmb
->header
.ancount
) {
1371 * XXXX what do we do with this? Could be a redirect,
1372 * but we'll discard it for the moment.
1377 tmp_addrs
= talloc_realloc(
1378 state
, state
->addrs
, struct sockaddr_storage
,
1379 state
->num_addrs
+ nmb
->answers
->rdlength
/6);
1380 if (tmp_addrs
== NULL
) {
1381 state
->validate_error
= NT_STATUS_NO_MEMORY
;
1384 state
->addrs
= tmp_addrs
;
1386 DEBUG(2,("Got a positive name query response "
1387 "from %s ( ", inet_ntoa(p
->ip
)));
1389 for (i
=0; i
<nmb
->answers
->rdlength
/6; i
++) {
1392 struct sockaddr_storage addr
;
1395 flags
= RSVAL(&nmb
->answers
->rdata
[i
*6], 0);
1396 got_unique_netbios_name
|= ((flags
& 0x8000) == 0);
1398 putip((char *)&ip
,&nmb
->answers
->rdata
[2+i
*6]);
1399 in_addr_to_sockaddr_storage(&addr
, ip
);
1401 if (is_zero_addr(&addr
)) {
1405 for (j
=0; j
<state
->num_addrs
; j
++) {
1407 (struct sockaddr
*)(void *)&addr
,
1408 (struct sockaddr
*)(void *)&state
->addrs
[j
])) {
1412 if (j
< state
->num_addrs
) {
1413 /* Already got it */
1417 DEBUGADD(2,("%s ",inet_ntoa(ip
)));
1419 state
->addrs
[state
->num_addrs
] = addr
;
1420 state
->num_addrs
+= 1;
1422 DEBUGADD(2,(")\n"));
1424 /* We add the flags back ... */
1425 if (nmb
->header
.response
)
1426 state
->flags
|= NM_FLAGS_RS
;
1427 if (nmb
->header
.nm_flags
.authoritative
)
1428 state
->flags
|= NM_FLAGS_AA
;
1429 if (nmb
->header
.nm_flags
.trunc
)
1430 state
->flags
|= NM_FLAGS_TC
;
1431 if (nmb
->header
.nm_flags
.recursion_desired
)
1432 state
->flags
|= NM_FLAGS_RD
;
1433 if (nmb
->header
.nm_flags
.recursion_available
)
1434 state
->flags
|= NM_FLAGS_RA
;
1435 if (nmb
->header
.nm_flags
.bcast
)
1436 state
->flags
|= NM_FLAGS_B
;
1440 * We have to collect all entries coming in from broadcast
1441 * queries. If we got a unique name, we're done.
1443 return got_unique_netbios_name
;
1446 * WINS responses are accepted when they are received
1451 static void name_query_done(struct tevent_req
*subreq
)
1453 struct tevent_req
*req
= tevent_req_callback_data(
1454 subreq
, struct tevent_req
);
1455 struct name_query_state
*state
= tevent_req_data(
1456 req
, struct name_query_state
);
1458 struct packet_struct
*p
= NULL
;
1460 status
= nb_trans_recv(subreq
, &p
);
1461 TALLOC_FREE(subreq
);
1462 if (tevent_req_nterror(req
, status
)) {
1465 if (!NT_STATUS_IS_OK(state
->validate_error
)) {
1466 tevent_req_nterror(req
, state
->validate_error
);
1471 * Free the packet here, we've collected the response in the
1476 tevent_req_done(req
);
1479 NTSTATUS
name_query_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
1480 struct sockaddr_storage
**addrs
, int *num_addrs
,
1483 struct name_query_state
*state
= tevent_req_data(
1484 req
, struct name_query_state
);
1487 if (tevent_req_is_nterror(req
, &status
)) {
1489 NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
1491 * In the broadcast case we collect replies until the
1494 status
= NT_STATUS_OK
;
1496 if (!NT_STATUS_IS_OK(status
)) {
1500 if (state
->num_addrs
== 0) {
1501 return NT_STATUS_NOT_FOUND
;
1503 *addrs
= talloc_move(mem_ctx
, &state
->addrs
);
1504 sort_addr_list(*addrs
, state
->num_addrs
);
1505 *num_addrs
= state
->num_addrs
;
1506 if (flags
!= NULL
) {
1507 *flags
= state
->flags
;
1509 return NT_STATUS_OK
;
1512 NTSTATUS
name_query(const char *name
, int name_type
,
1513 bool bcast
, bool recurse
,
1514 const struct sockaddr_storage
*to_ss
,
1515 TALLOC_CTX
*mem_ctx
,
1516 struct sockaddr_storage
**addrs
,
1517 int *num_addrs
, uint8_t *flags
)
1519 TALLOC_CTX
*frame
= talloc_stackframe();
1520 struct tevent_context
*ev
;
1521 struct tevent_req
*req
;
1522 struct timeval timeout
;
1523 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1525 ev
= samba_tevent_context_init(frame
);
1529 req
= name_query_send(ev
, ev
, name
, name_type
, bcast
, recurse
, to_ss
);
1534 timeout
= timeval_current_ofs(0, 250000);
1536 timeout
= timeval_current_ofs(2, 0);
1538 if (!tevent_req_set_endtime(req
, ev
, timeout
)) {
1541 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1544 status
= name_query_recv(req
, mem_ctx
, addrs
, num_addrs
, flags
);
1550 /********************************************************
1551 Convert an array if struct sockaddr_storage to struct ip_service
1552 return false on failure. Port is set to PORT_NONE;
1553 pcount is [in/out] - it is the length of ss_list on input,
1554 and the length of return_iplist on output as we remove any
1555 zero addresses from ss_list.
1556 *********************************************************/
1558 static bool convert_ss2service(struct ip_service
**return_iplist
,
1559 const struct sockaddr_storage
*ss_list
,
1563 int orig_count
= *pcount
;
1566 if (orig_count
==0 || !ss_list
)
1569 /* Filter out zero addrs. */
1570 for ( i
=0; i
<orig_count
; i
++ ) {
1571 if (is_zero_addr(&ss_list
[i
])) {
1576 if (real_count
==0) {
1580 /* copy the ip address; port will be PORT_NONE */
1581 if ((*return_iplist
= SMB_MALLOC_ARRAY(struct ip_service
, real_count
)) ==
1583 DEBUG(0,("convert_ip2service: malloc failed "
1584 "for %d enetries!\n", real_count
));
1588 for ( i
=0, real_count
= 0; i
<orig_count
; i
++ ) {
1589 if (is_zero_addr(&ss_list
[i
])) {
1592 (*return_iplist
)[real_count
].ss
= ss_list
[i
];
1593 (*return_iplist
)[real_count
].port
= PORT_NONE
;
1597 *pcount
= real_count
;
1601 struct name_queries_state
{
1602 struct tevent_context
*ev
;
1607 const struct sockaddr_storage
*addrs
;
1612 struct tevent_req
**subreqs
;
1617 struct sockaddr_storage
*result_addrs
;
1618 int num_result_addrs
;
1622 static void name_queries_done(struct tevent_req
*subreq
);
1623 static void name_queries_next(struct tevent_req
*subreq
);
1626 * Send a name query to multiple destinations with a wait time in between
1629 static struct tevent_req
*name_queries_send(
1630 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
1631 const char *name
, int name_type
,
1632 bool bcast
, bool recurse
,
1633 const struct sockaddr_storage
*addrs
,
1634 int num_addrs
, int wait_msec
, int timeout_msec
)
1636 struct tevent_req
*req
, *subreq
;
1637 struct name_queries_state
*state
;
1639 req
= tevent_req_create(mem_ctx
, &state
,
1640 struct name_queries_state
);
1646 state
->name_type
= name_type
;
1647 state
->bcast
= bcast
;
1648 state
->recurse
= recurse
;
1649 state
->addrs
= addrs
;
1650 state
->num_addrs
= num_addrs
;
1651 state
->wait_msec
= wait_msec
;
1652 state
->timeout_msec
= timeout_msec
;
1654 state
->subreqs
= talloc_zero_array(
1655 state
, struct tevent_req
*, num_addrs
);
1656 if (tevent_req_nomem(state
->subreqs
, req
)) {
1657 return tevent_req_post(req
, ev
);
1659 state
->num_sent
= 0;
1661 subreq
= name_query_send(
1662 state
->subreqs
, state
->ev
, name
, name_type
, bcast
, recurse
,
1663 &state
->addrs
[state
->num_sent
]);
1664 if (tevent_req_nomem(subreq
, req
)) {
1665 return tevent_req_post(req
, ev
);
1667 if (!tevent_req_set_endtime(
1669 timeval_current_ofs(0, state
->timeout_msec
* 1000))) {
1670 tevent_req_oom(req
);
1671 return tevent_req_post(req
, ev
);
1673 tevent_req_set_callback(subreq
, name_queries_done
, req
);
1675 state
->subreqs
[state
->num_sent
] = subreq
;
1676 state
->num_sent
+= 1;
1678 if (state
->num_sent
< state
->num_addrs
) {
1679 subreq
= tevent_wakeup_send(
1681 timeval_current_ofs(0, state
->wait_msec
* 1000));
1682 if (tevent_req_nomem(subreq
, req
)) {
1683 return tevent_req_post(req
, ev
);
1685 tevent_req_set_callback(subreq
, name_queries_next
, req
);
1690 static void name_queries_done(struct tevent_req
*subreq
)
1692 struct tevent_req
*req
= tevent_req_callback_data(
1693 subreq
, struct tevent_req
);
1694 struct name_queries_state
*state
= tevent_req_data(
1695 req
, struct name_queries_state
);
1699 status
= name_query_recv(subreq
, state
, &state
->result_addrs
,
1700 &state
->num_result_addrs
, &state
->flags
);
1702 for (i
=0; i
<state
->num_sent
; i
++) {
1703 if (state
->subreqs
[i
] == subreq
) {
1707 if (i
== state
->num_sent
) {
1708 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
1711 TALLOC_FREE(state
->subreqs
[i
]);
1713 state
->num_received
+= 1;
1715 if (!NT_STATUS_IS_OK(status
)) {
1717 if (state
->num_received
>= state
->num_addrs
) {
1718 tevent_req_nterror(req
, status
);
1722 * Still outstanding requests, just wait
1726 state
->received_index
= i
;
1727 tevent_req_done(req
);
1730 static void name_queries_next(struct tevent_req
*subreq
)
1732 struct tevent_req
*req
= tevent_req_callback_data(
1733 subreq
, struct tevent_req
);
1734 struct name_queries_state
*state
= tevent_req_data(
1735 req
, struct name_queries_state
);
1737 if (!tevent_wakeup_recv(subreq
)) {
1738 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
1742 subreq
= name_query_send(
1743 state
->subreqs
, state
->ev
,
1744 state
->name
, state
->name_type
, state
->bcast
, state
->recurse
,
1745 &state
->addrs
[state
->num_sent
]);
1746 if (tevent_req_nomem(subreq
, req
)) {
1749 tevent_req_set_callback(subreq
, name_queries_done
, req
);
1750 if (!tevent_req_set_endtime(
1752 timeval_current_ofs(0, state
->timeout_msec
* 1000))) {
1753 tevent_req_oom(req
);
1756 state
->subreqs
[state
->num_sent
] = subreq
;
1757 state
->num_sent
+= 1;
1759 if (state
->num_sent
< state
->num_addrs
) {
1760 subreq
= tevent_wakeup_send(
1762 timeval_current_ofs(0, state
->wait_msec
* 1000));
1763 if (tevent_req_nomem(subreq
, req
)) {
1766 tevent_req_set_callback(subreq
, name_queries_next
, req
);
1770 static NTSTATUS
name_queries_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
1771 struct sockaddr_storage
**result_addrs
,
1772 int *num_result_addrs
, uint8_t *flags
,
1773 int *received_index
)
1775 struct name_queries_state
*state
= tevent_req_data(
1776 req
, struct name_queries_state
);
1779 if (tevent_req_is_nterror(req
, &status
)) {
1783 if (result_addrs
!= NULL
) {
1784 *result_addrs
= talloc_move(mem_ctx
, &state
->result_addrs
);
1786 if (num_result_addrs
!= NULL
) {
1787 *num_result_addrs
= state
->num_result_addrs
;
1789 if (flags
!= NULL
) {
1790 *flags
= state
->flags
;
1792 if (received_index
!= NULL
) {
1793 *received_index
= state
->received_index
;
1795 return NT_STATUS_OK
;
1798 /********************************************************
1799 Resolve via "bcast" method.
1800 *********************************************************/
1802 struct name_resolve_bcast_state
{
1803 struct sockaddr_storage
*addrs
;
1807 static void name_resolve_bcast_done(struct tevent_req
*subreq
);
1809 struct tevent_req
*name_resolve_bcast_send(TALLOC_CTX
*mem_ctx
,
1810 struct tevent_context
*ev
,
1814 struct tevent_req
*req
, *subreq
;
1815 struct name_resolve_bcast_state
*state
;
1816 struct sockaddr_storage
*bcast_addrs
;
1817 int i
, num_addrs
, num_bcast_addrs
;
1819 req
= tevent_req_create(mem_ctx
, &state
,
1820 struct name_resolve_bcast_state
);
1825 if (lp_disable_netbios()) {
1826 DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
1828 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1829 return tevent_req_post(req
, ev
);
1833 * "bcast" means do a broadcast lookup on all the local interfaces.
1836 DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
1837 "for name %s<0x%x>\n", name
, name_type
));
1839 num_addrs
= iface_count();
1840 bcast_addrs
= talloc_array(state
, struct sockaddr_storage
, num_addrs
);
1841 if (tevent_req_nomem(bcast_addrs
, req
)) {
1842 return tevent_req_post(req
, ev
);
1846 * Lookup the name on all the interfaces, return on
1847 * the first successful match.
1849 num_bcast_addrs
= 0;
1851 for (i
=0; i
<num_addrs
; i
++) {
1852 const struct sockaddr_storage
*pss
= iface_n_bcast(i
);
1854 if (pss
->ss_family
!= AF_INET
) {
1857 bcast_addrs
[num_bcast_addrs
] = *pss
;
1858 num_bcast_addrs
+= 1;
1861 subreq
= name_queries_send(state
, ev
, name
, name_type
, true, true,
1862 bcast_addrs
, num_bcast_addrs
, 0, 1000);
1863 if (tevent_req_nomem(subreq
, req
)) {
1864 return tevent_req_post(req
, ev
);
1866 tevent_req_set_callback(subreq
, name_resolve_bcast_done
, req
);
1870 static void name_resolve_bcast_done(struct tevent_req
*subreq
)
1872 struct tevent_req
*req
= tevent_req_callback_data(
1873 subreq
, struct tevent_req
);
1874 struct name_resolve_bcast_state
*state
= tevent_req_data(
1875 req
, struct name_resolve_bcast_state
);
1878 status
= name_queries_recv(subreq
, state
,
1879 &state
->addrs
, &state
->num_addrs
,
1881 TALLOC_FREE(subreq
);
1882 if (tevent_req_nterror(req
, status
)) {
1885 tevent_req_done(req
);
1888 NTSTATUS
name_resolve_bcast_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
1889 struct sockaddr_storage
**addrs
,
1892 struct name_resolve_bcast_state
*state
= tevent_req_data(
1893 req
, struct name_resolve_bcast_state
);
1896 if (tevent_req_is_nterror(req
, &status
)) {
1899 *addrs
= talloc_move(mem_ctx
, &state
->addrs
);
1900 *num_addrs
= state
->num_addrs
;
1901 return NT_STATUS_OK
;
1904 NTSTATUS
name_resolve_bcast(const char *name
,
1906 TALLOC_CTX
*mem_ctx
,
1907 struct sockaddr_storage
**return_iplist
,
1910 TALLOC_CTX
*frame
= talloc_stackframe();
1911 struct tevent_context
*ev
;
1912 struct tevent_req
*req
;
1913 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1915 ev
= samba_tevent_context_init(frame
);
1919 req
= name_resolve_bcast_send(frame
, ev
, name
, name_type
);
1923 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1926 status
= name_resolve_bcast_recv(req
, mem_ctx
, return_iplist
,
1933 struct query_wins_list_state
{
1934 struct tevent_context
*ev
;
1937 struct in_addr
*servers
;
1938 uint32_t num_servers
;
1939 struct sockaddr_storage server
;
1942 struct sockaddr_storage
*addrs
;
1947 static void query_wins_list_done(struct tevent_req
*subreq
);
1950 * Query a list of (replicating) wins servers in sequence, call them
1951 * dead if they don't reply
1954 static struct tevent_req
*query_wins_list_send(
1955 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
1956 struct in_addr src_ip
, const char *name
, uint8_t name_type
,
1957 struct in_addr
*servers
, int num_servers
)
1959 struct tevent_req
*req
, *subreq
;
1960 struct query_wins_list_state
*state
;
1962 req
= tevent_req_create(mem_ctx
, &state
,
1963 struct query_wins_list_state
);
1969 state
->name_type
= name_type
;
1970 state
->servers
= servers
;
1971 state
->num_servers
= num_servers
;
1973 if (state
->num_servers
== 0) {
1974 tevent_req_nterror(req
, NT_STATUS_NOT_FOUND
);
1975 return tevent_req_post(req
, ev
);
1978 in_addr_to_sockaddr_storage(
1979 &state
->server
, state
->servers
[state
->num_sent
]);
1981 subreq
= name_query_send(state
, state
->ev
,
1982 state
->name
, state
->name_type
,
1983 false, true, &state
->server
);
1984 state
->num_sent
+= 1;
1985 if (tevent_req_nomem(subreq
, req
)) {
1986 return tevent_req_post(req
, ev
);
1988 if (!tevent_req_set_endtime(subreq
, state
->ev
,
1989 timeval_current_ofs(2, 0))) {
1990 tevent_req_oom(req
);
1991 return tevent_req_post(req
, ev
);
1993 tevent_req_set_callback(subreq
, query_wins_list_done
, req
);
1997 static void query_wins_list_done(struct tevent_req
*subreq
)
1999 struct tevent_req
*req
= tevent_req_callback_data(
2000 subreq
, struct tevent_req
);
2001 struct query_wins_list_state
*state
= tevent_req_data(
2002 req
, struct query_wins_list_state
);
2005 status
= name_query_recv(subreq
, state
,
2006 &state
->addrs
, &state
->num_addrs
,
2008 TALLOC_FREE(subreq
);
2009 if (NT_STATUS_IS_OK(status
)) {
2010 tevent_req_done(req
);
2013 if (!NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
2014 tevent_req_nterror(req
, status
);
2017 wins_srv_died(state
->servers
[state
->num_sent
-1],
2018 my_socket_addr_v4());
2020 if (state
->num_sent
== state
->num_servers
) {
2021 tevent_req_nterror(req
, NT_STATUS_NOT_FOUND
);
2025 in_addr_to_sockaddr_storage(
2026 &state
->server
, state
->servers
[state
->num_sent
]);
2028 subreq
= name_query_send(state
, state
->ev
,
2029 state
->name
, state
->name_type
,
2030 false, true, &state
->server
);
2031 state
->num_sent
+= 1;
2032 if (tevent_req_nomem(subreq
, req
)) {
2035 if (!tevent_req_set_endtime(subreq
, state
->ev
,
2036 timeval_current_ofs(2, 0))) {
2037 tevent_req_oom(req
);
2040 tevent_req_set_callback(subreq
, query_wins_list_done
, req
);
2043 static NTSTATUS
query_wins_list_recv(struct tevent_req
*req
,
2044 TALLOC_CTX
*mem_ctx
,
2045 struct sockaddr_storage
**addrs
,
2049 struct query_wins_list_state
*state
= tevent_req_data(
2050 req
, struct query_wins_list_state
);
2053 if (tevent_req_is_nterror(req
, &status
)) {
2056 if (addrs
!= NULL
) {
2057 *addrs
= talloc_move(mem_ctx
, &state
->addrs
);
2059 if (num_addrs
!= NULL
) {
2060 *num_addrs
= state
->num_addrs
;
2062 if (flags
!= NULL
) {
2063 *flags
= state
->flags
;
2065 return NT_STATUS_OK
;
2068 struct resolve_wins_state
{
2072 struct sockaddr_storage
*addrs
;
2077 static void resolve_wins_done(struct tevent_req
*subreq
);
2079 struct tevent_req
*resolve_wins_send(TALLOC_CTX
*mem_ctx
,
2080 struct tevent_context
*ev
,
2084 struct tevent_req
*req
, *subreq
;
2085 struct resolve_wins_state
*state
;
2086 char **wins_tags
= NULL
;
2087 struct sockaddr_storage src_ss
;
2088 struct in_addr src_ip
;
2089 int i
, num_wins_tags
;
2091 req
= tevent_req_create(mem_ctx
, &state
,
2092 struct resolve_wins_state
);
2097 if (wins_srv_count() < 1) {
2098 DEBUG(3,("resolve_wins: WINS server resolution selected "
2099 "and no WINS servers listed.\n"));
2100 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2104 /* the address we will be sending from */
2105 if (!interpret_string_addr(&src_ss
, lp_nbt_client_socket_address(),
2106 AI_NUMERICHOST
|AI_PASSIVE
)) {
2107 zero_sockaddr(&src_ss
);
2110 if (src_ss
.ss_family
!= AF_INET
) {
2111 char addr
[INET6_ADDRSTRLEN
];
2112 print_sockaddr(addr
, sizeof(addr
), &src_ss
);
2113 DEBUG(3,("resolve_wins: cannot receive WINS replies "
2114 "on IPv6 address %s\n",
2116 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2120 src_ip
= ((const struct sockaddr_in
*)(void *)&src_ss
)->sin_addr
;
2122 wins_tags
= wins_srv_tags();
2123 if (wins_tags
== NULL
) {
2124 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2129 while (wins_tags
[num_wins_tags
] != NULL
) {
2133 for (i
=0; i
<num_wins_tags
; i
++) {
2134 int num_servers
, num_alive
;
2135 struct in_addr
*servers
, *alive
;
2138 if (!wins_server_tag_ips(wins_tags
[i
], talloc_tos(),
2139 &servers
, &num_servers
)) {
2140 DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
2145 alive
= talloc_array(state
, struct in_addr
, num_servers
);
2146 if (tevent_req_nomem(alive
, req
)) {
2151 for (j
=0; j
<num_servers
; j
++) {
2152 struct in_addr wins_ip
= servers
[j
];
2154 if (global_in_nmbd
&& ismyip_v4(wins_ip
)) {
2155 /* yikes! we'll loop forever */
2158 /* skip any that have been unresponsive lately */
2159 if (wins_srv_is_dead(wins_ip
, src_ip
)) {
2162 DEBUG(3, ("resolve_wins: using WINS server %s "
2164 inet_ntoa(wins_ip
), wins_tags
[i
]));
2165 alive
[num_alive
] = wins_ip
;
2168 TALLOC_FREE(servers
);
2170 if (num_alive
== 0) {
2174 subreq
= query_wins_list_send(
2175 state
, ev
, src_ip
, name
, name_type
,
2177 if (tevent_req_nomem(subreq
, req
)) {
2180 tevent_req_set_callback(subreq
, resolve_wins_done
, req
);
2181 state
->num_sent
+= 1;
2184 if (state
->num_sent
== 0) {
2185 tevent_req_nterror(req
, NT_STATUS_NOT_FOUND
);
2189 wins_srv_tags_free(wins_tags
);
2192 wins_srv_tags_free(wins_tags
);
2193 return tevent_req_post(req
, ev
);
2196 static void resolve_wins_done(struct tevent_req
*subreq
)
2198 struct tevent_req
*req
= tevent_req_callback_data(
2199 subreq
, struct tevent_req
);
2200 struct resolve_wins_state
*state
= tevent_req_data(
2201 req
, struct resolve_wins_state
);
2204 status
= query_wins_list_recv(subreq
, state
, &state
->addrs
,
2205 &state
->num_addrs
, &state
->flags
);
2206 if (NT_STATUS_IS_OK(status
)) {
2207 tevent_req_done(req
);
2211 state
->num_received
+= 1;
2213 if (state
->num_received
< state
->num_sent
) {
2215 * Wait for the others
2219 tevent_req_nterror(req
, status
);
2222 NTSTATUS
resolve_wins_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
2223 struct sockaddr_storage
**addrs
,
2224 int *num_addrs
, uint8_t *flags
)
2226 struct resolve_wins_state
*state
= tevent_req_data(
2227 req
, struct resolve_wins_state
);
2230 if (tevent_req_is_nterror(req
, &status
)) {
2233 if (addrs
!= NULL
) {
2234 *addrs
= talloc_move(mem_ctx
, &state
->addrs
);
2236 if (num_addrs
!= NULL
) {
2237 *num_addrs
= state
->num_addrs
;
2239 if (flags
!= NULL
) {
2240 *flags
= state
->flags
;
2242 return NT_STATUS_OK
;
2245 /********************************************************
2246 Resolve via "wins" method.
2247 *********************************************************/
2249 NTSTATUS
resolve_wins(const char *name
,
2251 TALLOC_CTX
*mem_ctx
,
2252 struct sockaddr_storage
**return_iplist
,
2255 struct tevent_context
*ev
;
2256 struct tevent_req
*req
;
2257 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2259 ev
= samba_tevent_context_init(talloc_tos());
2263 req
= resolve_wins_send(ev
, ev
, name
, name_type
);
2267 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2270 status
= resolve_wins_recv(req
, mem_ctx
, return_iplist
, return_count
,
2277 /********************************************************
2278 Resolve via "lmhosts" method.
2279 *********************************************************/
2281 static NTSTATUS
resolve_lmhosts(const char *name
, int name_type
,
2282 struct ip_service
**return_iplist
,
2286 * "lmhosts" means parse the local lmhosts file.
2288 struct sockaddr_storage
*ss_list
;
2289 NTSTATUS status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
2290 TALLOC_CTX
*ctx
= NULL
;
2292 *return_iplist
= NULL
;
2295 DEBUG(3,("resolve_lmhosts: "
2296 "Attempting lmhosts lookup for name %s<0x%x>\n",
2299 ctx
= talloc_init("resolve_lmhosts");
2301 return NT_STATUS_NO_MEMORY
;
2304 status
= resolve_lmhosts_file_as_sockaddr(get_dyn_LMHOSTSFILE(),
2309 if (NT_STATUS_IS_OK(status
)) {
2310 if (convert_ss2service(return_iplist
,
2314 return NT_STATUS_OK
;
2317 return NT_STATUS_NO_MEMORY
;
2325 /********************************************************
2326 Resolve via "hosts" method.
2327 *********************************************************/
2329 static NTSTATUS
resolve_hosts(const char *name
, int name_type
,
2330 struct ip_service
**return_iplist
,
2334 * "host" means do a localhost, or dns lookup.
2336 struct addrinfo hints
;
2337 struct addrinfo
*ailist
= NULL
;
2338 struct addrinfo
*res
= NULL
;
2342 if ( name_type
!= 0x20 && name_type
!= 0x0) {
2343 DEBUG(5, ("resolve_hosts: not appropriate "
2344 "for name type <0x%x>\n",
2346 return NT_STATUS_INVALID_PARAMETER
;
2349 *return_iplist
= NULL
;
2352 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
2356 /* By default make sure it supports TCP. */
2357 hints
.ai_socktype
= SOCK_STREAM
;
2358 hints
.ai_flags
= AI_ADDRCONFIG
;
2360 #if !defined(HAVE_IPV6)
2361 /* Unless we have IPv6, we really only want IPv4 addresses back. */
2362 hints
.ai_family
= AF_INET
;
2365 ret
= getaddrinfo(name
,
2370 DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
2372 gai_strerror(ret
) ));
2375 for (res
= ailist
; res
; res
= res
->ai_next
) {
2376 struct sockaddr_storage ss
;
2378 if (!res
->ai_addr
|| res
->ai_addrlen
== 0) {
2383 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
2385 if (is_zero_addr(&ss
)) {
2391 *return_iplist
= SMB_REALLOC_ARRAY(*return_iplist
,
2394 if (!*return_iplist
) {
2395 DEBUG(3,("resolve_hosts: malloc fail !\n"));
2396 freeaddrinfo(ailist
);
2397 return NT_STATUS_NO_MEMORY
;
2399 (*return_iplist
)[i
].ss
= ss
;
2400 (*return_iplist
)[i
].port
= PORT_NONE
;
2404 freeaddrinfo(ailist
);
2406 if (*return_count
) {
2407 return NT_STATUS_OK
;
2409 return NT_STATUS_UNSUCCESSFUL
;
2412 /********************************************************
2413 Resolve via "ADS" method.
2414 *********************************************************/
2416 /* Special name type used to cause a _kerberos DNS lookup. */
2417 #define KDC_NAME_TYPE 0xDCDC
2419 static NTSTATUS
resolve_ads(const char *name
,
2421 const char *sitename
,
2422 struct ip_service
**return_iplist
,
2428 struct dns_rr_srv
*dcs
= NULL
;
2432 if ((name_type
!= 0x1c) && (name_type
!= KDC_NAME_TYPE
) &&
2433 (name_type
!= 0x1b)) {
2434 return NT_STATUS_INVALID_PARAMETER
;
2437 if ( (ctx
= talloc_init("resolve_ads")) == NULL
) {
2438 DEBUG(0,("resolve_ads: talloc_init() failed!\n"));
2439 return NT_STATUS_NO_MEMORY
;
2442 /* The DNS code needs fixing to find IPv6 addresses... JRA. */
2443 switch (name_type
) {
2445 DEBUG(5,("resolve_ads: Attempting to resolve "
2446 "PDC for %s using DNS\n", name
));
2447 status
= ads_dns_query_pdc(ctx
,
2454 DEBUG(5,("resolve_ads: Attempting to resolve "
2455 "DCs for %s using DNS\n", name
));
2456 status
= ads_dns_query_dcs(ctx
,
2463 DEBUG(5,("resolve_ads: Attempting to resolve "
2464 "KDCs for %s using DNS\n", name
));
2465 status
= ads_dns_query_kdcs(ctx
,
2472 status
= NT_STATUS_INVALID_PARAMETER
;
2476 if ( !NT_STATUS_IS_OK( status
) ) {
2477 talloc_destroy(ctx
);
2481 for (i
=0;i
<numdcs
;i
++) {
2485 numaddrs
+= dcs
[i
].num_ips
;
2489 if ((*return_iplist
= SMB_MALLOC_ARRAY(struct ip_service
, numaddrs
)) ==
2491 DEBUG(0,("resolve_ads: malloc failed for %d entries\n",
2493 talloc_destroy(ctx
);
2494 return NT_STATUS_NO_MEMORY
;
2497 /* now unroll the list of IP addresses */
2501 for (i
= 0; i
< numdcs
&& (*return_count
<numaddrs
); i
++ ) {
2502 /* If we don't have an IP list for a name, lookup it up */
2504 /* We need to get all IP addresses here. */
2505 struct addrinfo
*res
= NULL
;
2507 int extra_addrs
= 0;
2509 if (!interpret_string_addr_internal(&res
,
2514 /* Add in every IP from the lookup. How
2516 for (p
= res
; p
; p
= p
->ai_next
) {
2517 struct sockaddr_storage ss
;
2518 memcpy(&ss
, p
->ai_addr
, p
->ai_addrlen
);
2519 if (is_zero_addr(&ss
)) {
2524 if (extra_addrs
> 1) {
2525 /* We need to expand the return_iplist array
2526 as we only budgeted for one address. */
2527 numaddrs
+= (extra_addrs
-1);
2528 *return_iplist
= SMB_REALLOC_ARRAY(*return_iplist
,
2531 if (*return_iplist
== NULL
) {
2535 talloc_destroy(ctx
);
2536 return NT_STATUS_NO_MEMORY
;
2539 for (p
= res
; p
; p
= p
->ai_next
) {
2540 (*return_iplist
)[*return_count
].port
= dcs
[i
].port
;
2541 memcpy(&(*return_iplist
)[*return_count
].ss
,
2544 if (is_zero_addr(&(*return_iplist
)[*return_count
].ss
)) {
2548 /* Should never happen, but still... */
2549 if (*return_count
>=numaddrs
) {
2557 /* use all the IP addresses from the SRV sresponse */
2559 for (j
= 0; j
< dcs
[i
].num_ips
; j
++) {
2560 (*return_iplist
)[*return_count
].port
= dcs
[i
].port
;
2561 (*return_iplist
)[*return_count
].ss
= dcs
[i
].ss_s
[j
];
2562 if (is_zero_addr(&(*return_iplist
)[*return_count
].ss
)) {
2566 /* Should never happen, but still... */
2567 if (*return_count
>=numaddrs
) {
2574 talloc_destroy(ctx
);
2575 return NT_STATUS_OK
;
2578 static const char **filter_out_nbt_lookup(TALLOC_CTX
*mem_ctx
,
2579 const char **resolve_order
)
2581 size_t i
, len
, result_idx
;
2582 const char **result
;
2585 while (resolve_order
[len
] != NULL
) {
2589 result
= talloc_array(mem_ctx
, const char *, len
+1);
2590 if (result
== NULL
) {
2596 for (i
=0; i
<len
; i
++) {
2597 const char *tok
= resolve_order
[i
];
2599 if (strequal(tok
, "lmhosts") || strequal(tok
, "wins") ||
2600 strequal(tok
, "bcast")) {
2603 result
[result_idx
++] = tok
;
2605 result
[result_idx
] = NULL
;
2610 /*******************************************************************
2611 Internal interface to resolve a name into an IP address.
2612 Use this function if the string is either an IP address, DNS
2613 or host name or NetBIOS name. This uses the name switch in the
2614 smb.conf to determine the order of name resolution.
2616 Added support for ip addr/port to support ADS ldap servers.
2617 the only place we currently care about the port is in the
2618 resolve_hosts() when looking up DC's via SRV RR entries in DNS
2619 **********************************************************************/
2621 NTSTATUS
internal_resolve_name(const char *name
,
2623 const char *sitename
,
2624 struct ip_service
**return_iplist
,
2626 const char **resolve_order
)
2629 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2631 TALLOC_CTX
*frame
= NULL
;
2633 *return_iplist
= NULL
;
2636 DEBUG(10, ("internal_resolve_name: looking up %s#%x (sitename %s)\n",
2637 name
, name_type
, sitename
? sitename
: "(null)"));
2639 if (is_ipaddress(name
)) {
2640 if ((*return_iplist
= SMB_MALLOC_P(struct ip_service
)) ==
2642 DEBUG(0,("internal_resolve_name: malloc fail !\n"));
2643 return NT_STATUS_NO_MEMORY
;
2646 /* ignore the port here */
2647 (*return_iplist
)->port
= PORT_NONE
;
2649 /* if it's in the form of an IP address then get the lib to interpret it */
2650 if (!interpret_string_addr(&(*return_iplist
)->ss
,
2651 name
, AI_NUMERICHOST
)) {
2652 DEBUG(1,("internal_resolve_name: interpret_string_addr "
2655 SAFE_FREE(*return_iplist
);
2656 return NT_STATUS_INVALID_PARAMETER
;
2658 if (is_zero_addr(&(*return_iplist
)->ss
)) {
2659 SAFE_FREE(*return_iplist
);
2660 return NT_STATUS_UNSUCCESSFUL
;
2663 return NT_STATUS_OK
;
2666 /* Check name cache */
2668 if (namecache_fetch(name
, name_type
, return_iplist
, return_count
)) {
2669 *return_count
= remove_duplicate_addrs2(*return_iplist
,
2671 /* This could be a negative response */
2672 if (*return_count
> 0) {
2673 return NT_STATUS_OK
;
2675 return NT_STATUS_UNSUCCESSFUL
;
2679 /* set the name resolution order */
2681 if (resolve_order
&& strcmp(resolve_order
[0], "NULL") == 0) {
2682 DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
2683 return NT_STATUS_INVALID_PARAMETER
;
2686 if (!resolve_order
|| !resolve_order
[0]) {
2687 static const char *host_order
[] = { "host", NULL
};
2688 resolve_order
= host_order
;
2691 frame
= talloc_stackframe();
2693 if ((strlen(name
) > MAX_NETBIOSNAME_LEN
- 1) ||
2694 (strchr(name
, '.') != NULL
)) {
2696 * Don't do NBT lookup, the name would not fit anyway
2698 resolve_order
= filter_out_nbt_lookup(frame
, resolve_order
);
2699 if (resolve_order
== NULL
) {
2701 return NT_STATUS_NO_MEMORY
;
2705 /* iterate through the name resolution backends */
2707 for (i
=0; resolve_order
[i
]; i
++) {
2708 tok
= resolve_order
[i
];
2710 if((strequal(tok
, "host") || strequal(tok
, "hosts"))) {
2711 status
= resolve_hosts(name
, name_type
, return_iplist
,
2713 if (NT_STATUS_IS_OK(status
)) {
2716 } else if(strequal( tok
, "kdc")) {
2717 /* deal with KDC_NAME_TYPE names here.
2718 * This will result in a SRV record lookup */
2719 status
= resolve_ads(name
, KDC_NAME_TYPE
, sitename
,
2720 return_iplist
, return_count
);
2721 if (NT_STATUS_IS_OK(status
)) {
2722 /* Ensure we don't namecache
2723 * this with the KDC port. */
2724 name_type
= KDC_NAME_TYPE
;
2727 } else if(strequal( tok
, "ads")) {
2728 /* deal with 0x1c and 0x1b names here.
2729 * This will result in a SRV record lookup */
2730 status
= resolve_ads(name
, name_type
, sitename
,
2731 return_iplist
, return_count
);
2732 if (NT_STATUS_IS_OK(status
)) {
2735 } else if (strequal(tok
, "lmhosts")) {
2736 status
= resolve_lmhosts(name
, name_type
,
2737 return_iplist
, return_count
);
2738 if (NT_STATUS_IS_OK(status
)) {
2741 } else if (strequal(tok
, "wins")) {
2742 /* don't resolve 1D via WINS */
2743 struct sockaddr_storage
*ss_list
;
2744 if (name_type
!= 0x1D) {
2745 status
= resolve_wins(name
, name_type
,
2749 if (NT_STATUS_IS_OK(status
)) {
2750 if (!convert_ss2service(return_iplist
,
2753 status
= NT_STATUS_NO_MEMORY
;
2758 } else if (strequal(tok
, "bcast")) {
2759 struct sockaddr_storage
*ss_list
;
2760 status
= name_resolve_bcast(
2761 name
, name_type
, talloc_tos(),
2762 &ss_list
, return_count
);
2763 if (NT_STATUS_IS_OK(status
)) {
2764 if (!convert_ss2service(return_iplist
,
2767 status
= NT_STATUS_NO_MEMORY
;
2772 DEBUG(0,("resolve_name: unknown name switch type %s\n",
2777 /* All of the resolve_* functions above have returned false. */
2780 SAFE_FREE(*return_iplist
);
2783 return NT_STATUS_UNSUCCESSFUL
;
2787 /* Remove duplicate entries. Some queries, notably #1c (domain
2788 controllers) return the PDC in iplist[0] and then all domain
2789 controllers including the PDC in iplist[1..n]. Iterating over
2790 the iplist when the PDC is down will cause two sets of timeouts. */
2792 *return_count
= remove_duplicate_addrs2(*return_iplist
, *return_count
);
2794 /* Save in name cache */
2795 if ( DEBUGLEVEL
>= 100 ) {
2796 for (i
= 0; i
< *return_count
&& DEBUGLEVEL
== 100; i
++) {
2797 char addr
[INET6_ADDRSTRLEN
];
2798 print_sockaddr(addr
, sizeof(addr
),
2799 &(*return_iplist
)[i
].ss
);
2800 DEBUG(100, ("Storing name %s of type %d (%s:%d)\n",
2804 (*return_iplist
)[i
].port
));
2808 if (*return_count
) {
2809 namecache_store(name
, name_type
, *return_count
, *return_iplist
);
2812 /* Display some debugging info */
2814 if ( DEBUGLEVEL
>= 10 ) {
2815 DEBUG(10, ("internal_resolve_name: returning %d addresses: ",
2818 for (i
= 0; i
< *return_count
; i
++) {
2819 char addr
[INET6_ADDRSTRLEN
];
2820 print_sockaddr(addr
, sizeof(addr
),
2821 &(*return_iplist
)[i
].ss
);
2822 DEBUGADD(10, ("%s:%d ",
2824 (*return_iplist
)[i
].port
));
2833 /********************************************************
2834 Internal interface to resolve a name into one IP address.
2835 Use this function if the string is either an IP address, DNS
2836 or host name or NetBIOS name. This uses the name switch in the
2837 smb.conf to determine the order of name resolution.
2838 *********************************************************/
2840 bool resolve_name(const char *name
,
2841 struct sockaddr_storage
*return_ss
,
2845 struct ip_service
*ss_list
= NULL
;
2846 char *sitename
= NULL
;
2850 if (is_ipaddress(name
)) {
2851 return interpret_string_addr(return_ss
, name
, AI_NUMERICHOST
);
2854 sitename
= sitename_fetch(talloc_tos(), lp_realm()); /* wild guess */
2856 status
= internal_resolve_name(name
, name_type
, sitename
,
2858 lp_name_resolve_order());
2859 if (NT_STATUS_IS_OK(status
)) {
2863 for (i
=0; i
<count
; i
++) {
2864 if (!is_zero_addr(&ss_list
[i
].ss
) &&
2865 !is_broadcast_addr((struct sockaddr
*)(void *)&ss_list
[i
].ss
) &&
2866 (ss_list
[i
].ss
.ss_family
== AF_INET
)) {
2867 *return_ss
= ss_list
[i
].ss
;
2869 TALLOC_FREE(sitename
);
2875 /* only return valid addresses for TCP connections */
2876 for (i
=0; i
<count
; i
++) {
2877 if (!is_zero_addr(&ss_list
[i
].ss
) &&
2878 !is_broadcast_addr((struct sockaddr
*)(void *)&ss_list
[i
].ss
)) {
2879 *return_ss
= ss_list
[i
].ss
;
2881 TALLOC_FREE(sitename
);
2888 TALLOC_FREE(sitename
);
2892 /********************************************************
2893 Internal interface to resolve a name into a list of IP addresses.
2894 Use this function if the string is either an IP address, DNS
2895 or host name or NetBIOS name. This uses the name switch in the
2896 smb.conf to determine the order of name resolution.
2897 *********************************************************/
2899 NTSTATUS
resolve_name_list(TALLOC_CTX
*ctx
,
2902 struct sockaddr_storage
**return_ss_arr
,
2903 unsigned int *p_num_entries
)
2905 struct ip_service
*ss_list
= NULL
;
2906 char *sitename
= NULL
;
2909 unsigned int num_entries
;
2913 *return_ss_arr
= NULL
;
2915 if (is_ipaddress(name
)) {
2916 *return_ss_arr
= talloc(ctx
, struct sockaddr_storage
);
2917 if (!*return_ss_arr
) {
2918 return NT_STATUS_NO_MEMORY
;
2920 if (!interpret_string_addr(*return_ss_arr
, name
, AI_NUMERICHOST
)) {
2921 TALLOC_FREE(*return_ss_arr
);
2922 return NT_STATUS_BAD_NETWORK_NAME
;
2925 return NT_STATUS_OK
;
2928 sitename
= sitename_fetch(ctx
, lp_realm()); /* wild guess */
2930 status
= internal_resolve_name(name
, name_type
, sitename
,
2932 lp_name_resolve_order());
2933 TALLOC_FREE(sitename
);
2935 if (!NT_STATUS_IS_OK(status
)) {
2939 /* only return valid addresses for TCP connections */
2940 for (i
=0, num_entries
= 0; i
<count
; i
++) {
2941 if (!is_zero_addr(&ss_list
[i
].ss
) &&
2942 !is_broadcast_addr((struct sockaddr
*)(void *)&ss_list
[i
].ss
)) {
2946 if (num_entries
== 0) {
2948 return NT_STATUS_BAD_NETWORK_NAME
;
2951 *return_ss_arr
= talloc_array(ctx
,
2952 struct sockaddr_storage
,
2954 if (!(*return_ss_arr
)) {
2956 return NT_STATUS_NO_MEMORY
;
2959 for (i
=0, num_entries
= 0; i
<count
; i
++) {
2960 if (!is_zero_addr(&ss_list
[i
].ss
) &&
2961 !is_broadcast_addr((struct sockaddr
*)(void *)&ss_list
[i
].ss
)) {
2962 (*return_ss_arr
)[num_entries
++] = ss_list
[i
].ss
;
2966 status
= NT_STATUS_OK
;
2967 *p_num_entries
= num_entries
;
2970 return NT_STATUS_OK
;
2973 /********************************************************
2974 Find the IP address of the master browser or DMB for a workgroup.
2975 *********************************************************/
2977 bool find_master_ip(const char *group
, struct sockaddr_storage
*master_ss
)
2979 struct ip_service
*ip_list
= NULL
;
2983 if (lp_disable_netbios()) {
2984 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group
));
2988 status
= internal_resolve_name(group
, 0x1D, NULL
, &ip_list
, &count
,
2989 lp_name_resolve_order());
2990 if (NT_STATUS_IS_OK(status
)) {
2991 *master_ss
= ip_list
[0].ss
;
2996 status
= internal_resolve_name(group
, 0x1B, NULL
, &ip_list
, &count
,
2997 lp_name_resolve_order());
2998 if (NT_STATUS_IS_OK(status
)) {
2999 *master_ss
= ip_list
[0].ss
;
3008 /********************************************************
3009 Get the IP address list of the primary domain controller
3011 *********************************************************/
3013 bool get_pdc_ip(const char *domain
, struct sockaddr_storage
*pss
)
3015 struct ip_service
*ip_list
= NULL
;
3017 NTSTATUS status
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
3018 static const char *ads_order
[] = { "ads", NULL
};
3019 /* Look up #1B name */
3021 if (lp_security() == SEC_ADS
) {
3022 status
= internal_resolve_name(domain
, 0x1b, NULL
, &ip_list
,
3026 if (!NT_STATUS_IS_OK(status
) || count
== 0) {
3027 status
= internal_resolve_name(domain
, 0x1b, NULL
, &ip_list
,
3029 lp_name_resolve_order());
3030 if (!NT_STATUS_IS_OK(status
)) {
3036 /* if we get more than 1 IP back we have to assume it is a
3037 multi-homed PDC and not a mess up */
3040 DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count
));
3041 sort_service_list(ip_list
, count
);
3044 *pss
= ip_list
[0].ss
;
3049 /* Private enum type for lookups. */
3051 enum dc_lookup_type
{ DC_NORMAL_LOOKUP
, DC_ADS_ONLY
, DC_KDC_ONLY
};
3053 /********************************************************
3054 Get the IP address list of the domain controllers for
3056 *********************************************************/
3058 static NTSTATUS
get_dc_list(const char *domain
,
3059 const char *sitename
,
3060 struct ip_service
**ip_list
,
3062 enum dc_lookup_type lookup_type
,
3065 const char **resolve_order
= NULL
;
3066 char *saf_servername
= NULL
;
3067 char *pserver
= NULL
;
3069 char *port_str
= NULL
;
3072 int num_addresses
= 0;
3073 int local_count
, i
, j
;
3074 struct ip_service
*return_iplist
= NULL
;
3075 struct ip_service
*auto_ip_list
= NULL
;
3076 bool done_auto_lookup
= false;
3079 TALLOC_CTX
*ctx
= talloc_init("get_dc_list");
3080 int auto_name_type
= 0x1C;
3086 return NT_STATUS_NO_MEMORY
;
3091 /* if we are restricted to solely using DNS for looking
3092 up a domain controller, make sure that host lookups
3093 are enabled for the 'name resolve order'. If host lookups
3094 are disabled and ads_only is True, then set the string to
3097 resolve_order
= lp_name_resolve_order();
3098 if (!resolve_order
) {
3099 status
= NT_STATUS_NO_MEMORY
;
3102 if (lookup_type
== DC_ADS_ONLY
) {
3103 if (str_list_check_ci(resolve_order
, "host")) {
3104 static const char *ads_order
[] = { "ads", NULL
};
3105 resolve_order
= ads_order
;
3107 /* DNS SRV lookups used by the ads resolver
3108 are already sorted by priority and weight */
3111 /* this is quite bizarre! */
3112 static const char *null_order
[] = { "NULL", NULL
};
3113 resolve_order
= null_order
;
3115 } else if (lookup_type
== DC_KDC_ONLY
) {
3116 static const char *kdc_order
[] = { "kdc", NULL
};
3117 /* DNS SRV lookups used by the ads/kdc resolver
3118 are already sorted by priority and weight */
3120 resolve_order
= kdc_order
;
3121 auto_name_type
= KDC_NAME_TYPE
;
3124 /* fetch the server we have affinity for. Add the
3125 'password server' list to a search for our domain controllers */
3127 saf_servername
= saf_fetch(ctx
, domain
);
3129 if (strequal(domain
, lp_workgroup()) || strequal(domain
, lp_realm())) {
3130 pserver
= talloc_asprintf(ctx
, "%s, %s",
3131 saf_servername
? saf_servername
: "",
3132 lp_password_server());
3134 pserver
= talloc_asprintf(ctx
, "%s, *",
3135 saf_servername
? saf_servername
: "");
3138 TALLOC_FREE(saf_servername
);
3140 status
= NT_STATUS_NO_MEMORY
;
3144 /* if we are starting from scratch, just lookup DOMAIN<0x1c> */
3147 DEBUG(10,("get_dc_list: no preferred domain controllers.\n"));
3148 status
= internal_resolve_name(domain
, 0x1C, sitename
, ip_list
,
3149 count
, resolve_order
);
3153 DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver
));
3156 * if '*' appears in the "password server" list then add
3157 * an auto lookup to the list of manually configured
3158 * DC's. If any DC is listed by name, then the list should be
3159 * considered to be ordered
3163 while (next_token_talloc(ctx
, &p
, &name
, LIST_SEP
)) {
3164 if (!done_auto_lookup
&& strequal(name
, "*")) {
3165 status
= internal_resolve_name(domain
, auto_name_type
,
3170 if (NT_STATUS_IS_OK(status
)) {
3171 num_addresses
+= auto_count
;
3173 done_auto_lookup
= true;
3174 DEBUG(8,("Adding %d DC's from auto lookup\n",
3181 /* if we have no addresses and haven't done the auto lookup, then
3182 just return the list of DC's. Or maybe we just failed. */
3184 if (num_addresses
== 0) {
3185 if (done_auto_lookup
) {
3186 DEBUG(4,("get_dc_list: no servers found\n"));
3187 status
= NT_STATUS_NO_LOGON_SERVERS
;
3190 status
= internal_resolve_name(domain
, auto_name_type
,
3192 count
, resolve_order
);
3196 if ((return_iplist
= SMB_MALLOC_ARRAY(struct ip_service
,
3197 num_addresses
)) == NULL
) {
3198 DEBUG(3,("get_dc_list: malloc fail !\n"));
3199 status
= NT_STATUS_NO_MEMORY
;
3206 /* fill in the return list now with real IP's */
3208 while ((local_count
<num_addresses
) &&
3209 next_token_talloc(ctx
, &p
, &name
, LIST_SEP
)) {
3210 struct sockaddr_storage name_ss
;
3212 /* copy any addersses from the auto lookup */
3214 if (strequal(name
, "*")) {
3215 for (j
=0; j
<auto_count
; j
++) {
3216 char addr
[INET6_ADDRSTRLEN
];
3217 print_sockaddr(addr
,
3219 &auto_ip_list
[j
].ss
);
3220 /* Check for and don't copy any
3221 * known bad DC IP's. */
3222 if(!NT_STATUS_IS_OK(check_negative_conn_cache(
3225 DEBUG(5,("get_dc_list: "
3226 "negative entry %s removed "
3231 return_iplist
[local_count
].ss
=
3233 return_iplist
[local_count
].port
=
3234 auto_ip_list
[j
].port
;
3240 /* added support for address:port syntax for ads
3241 * (not that I think anyone will ever run the LDAP
3242 * server in an AD domain on something other than
3244 * However, the port should not be used for kerberos
3247 port
= (lookup_type
== DC_ADS_ONLY
) ? LDAP_PORT
:
3248 ((lookup_type
== DC_KDC_ONLY
) ? DEFAULT_KRB5_PORT
:
3250 if ((port_str
=strchr(name
, ':')) != NULL
) {
3252 if (lookup_type
!= DC_KDC_ONLY
) {
3254 port
= atoi(port_str
);
3258 /* explicit lookup; resolve_name() will
3259 * handle names & IP addresses */
3260 if (resolve_name( name
, &name_ss
, 0x20, true )) {
3261 char addr
[INET6_ADDRSTRLEN
];
3262 print_sockaddr(addr
,
3266 /* Check for and don't copy any known bad DC IP's. */
3267 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain
,
3269 DEBUG(5,("get_dc_list: negative entry %s "
3270 "removed from DC list\n",
3275 return_iplist
[local_count
].ss
= name_ss
;
3276 return_iplist
[local_count
].port
= port
;
3282 /* need to remove duplicates in the list if we have any
3283 explicit password servers */
3285 local_count
= remove_duplicate_addrs2(return_iplist
, local_count
);
3287 /* For DC's we always prioritize IPv4 due to W2K3 not
3288 * supporting LDAP, KRB5 or CLDAP over IPv6. */
3290 if (local_count
&& return_iplist
) {
3291 prioritize_ipv4_list(return_iplist
, local_count
);
3294 if ( DEBUGLEVEL
>= 4 ) {
3295 DEBUG(4,("get_dc_list: returning %d ip addresses "
3296 "in an %sordered list\n",
3298 *ordered
? "":"un"));
3299 DEBUG(4,("get_dc_list: "));
3300 for ( i
=0; i
<local_count
; i
++ ) {
3301 char addr
[INET6_ADDRSTRLEN
];
3302 print_sockaddr(addr
,
3304 &return_iplist
[i
].ss
);
3305 DEBUGADD(4,("%s:%d ", addr
, return_iplist
[i
].port
));
3310 *ip_list
= return_iplist
;
3311 *count
= local_count
;
3313 status
= ( *count
!= 0 ? NT_STATUS_OK
: NT_STATUS_NO_LOGON_SERVERS
);
3317 if (!NT_STATUS_IS_OK(status
)) {
3318 SAFE_FREE(return_iplist
);
3323 SAFE_FREE(auto_ip_list
);
3328 /*********************************************************************
3329 Small wrapper function to get the DC list and sort it if neccessary.
3330 *********************************************************************/
3332 NTSTATUS
get_sorted_dc_list( const char *domain
,
3333 const char *sitename
,
3334 struct ip_service
**ip_list
,
3338 bool ordered
= false;
3340 enum dc_lookup_type lookup_type
= DC_NORMAL_LOOKUP
;
3345 DEBUG(8,("get_sorted_dc_list: attempting lookup "
3346 "for name %s (sitename %s)\n",
3348 sitename
? sitename
: "NULL"));
3351 lookup_type
= DC_ADS_ONLY
;
3354 status
= get_dc_list(domain
, sitename
, ip_list
,
3355 count
, lookup_type
, &ordered
);
3356 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_LOGON_SERVERS
)
3358 DEBUG(3,("get_sorted_dc_list: no server for name %s available"
3359 " in site %s, fallback to all servers\n",
3361 status
= get_dc_list(domain
, NULL
, ip_list
,
3362 count
, lookup_type
, &ordered
);
3365 if (!NT_STATUS_IS_OK(status
)) {
3366 SAFE_FREE(*ip_list
);
3371 /* only sort if we don't already have an ordered list */
3373 sort_service_list(*ip_list
, *count
);
3376 return NT_STATUS_OK
;
3379 /*********************************************************************
3380 Get the KDC list - re-use all the logic in get_dc_list.
3381 *********************************************************************/
3383 NTSTATUS
get_kdc_list( const char *realm
,
3384 const char *sitename
,
3385 struct ip_service
**ip_list
,
3394 status
= get_dc_list(realm
, sitename
, ip_list
,
3395 count
, DC_KDC_ONLY
, &ordered
);
3397 if (!NT_STATUS_IS_OK(status
)) {
3398 SAFE_FREE(*ip_list
);
3403 /* only sort if we don't already have an ordered list */
3405 sort_service_list(*ip_list
, *count
);
3408 return NT_STATUS_OK
;