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 "libsmb/namequery.h"
23 #include "../lib/util/tevent_ntstatus.h"
24 #include "libads/sitename_cache.h"
25 #include "../lib/addns/dnsquery.h"
26 #include "../lib/addns/dnsquery_srv.h"
27 #include "../libcli/netlogon/netlogon.h"
28 #include "lib/async_req/async_sock.h"
29 #include "lib/tsocket/tsocket.h"
30 #include "libsmb/nmblib.h"
31 #include "libsmb/unexpected.h"
32 #include "../libcli/nbt/libnbt.h"
33 #include "libads/kerberos_proto.h"
34 #include "lib/gencache.h"
35 #include "librpc/gen_ndr/dns.h"
36 #include "lib/util/util_net.h"
37 #include "lib/util/tsort.h"
38 #include "lib/util/string_wrappers.h"
40 /* nmbd.c sets this to True. */
41 bool global_in_nmbd = False;
44 * Utility function to convert from a sockaddr_storage
45 * array to a struct samba_sockaddr array.
48 static NTSTATUS sockaddr_array_to_samba_sockaddr_array(
50 struct samba_sockaddr **sa_out,
52 const struct sockaddr_storage *ss_in,
55 struct samba_sockaddr *sa = NULL;
61 * Zero length arrays are returned as NULL.
62 * in the name resolution code.
68 sa = talloc_zero_array(ctx,
69 struct samba_sockaddr,
72 return NT_STATUS_NO_MEMORY;
75 for (i = 0; i < count_in; i++) {
78 /* Filter out zero addresses. */
79 if (is_zero_addr(&ss_in[i])) {
82 ok = sockaddr_storage_to_samba_sockaddr(&sa[count],
91 * Zero length arrays are returned as NULL.
92 * in the name resolution code.
101 /****************************
102 * SERVER AFFINITY ROUTINES *
103 ****************************/
105 /* Server affinity is the concept of preferring the last domain
106 controller with whom you had a successful conversation */
108 /****************************************************************************
109 ****************************************************************************/
110 #define SAFKEY_FMT "SAF/DOMAIN/%s"
112 #define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
113 #define SAFJOIN_TTL 3600
115 static char *saf_key(TALLOC_CTX *mem_ctx, const char *domain)
117 return talloc_asprintf_strupper_m(mem_ctx, SAFKEY_FMT, domain);
120 static char *saf_join_key(TALLOC_CTX *mem_ctx, const char *domain)
122 return talloc_asprintf_strupper_m(mem_ctx, SAFJOINKEY_FMT, domain);
125 /****************************************************************************
126 ****************************************************************************/
128 bool saf_store( const char *domain, const char *servername )
134 if ( !domain || !servername ) {
135 DEBUG(2,("saf_store: "
136 "Refusing to store empty domain or servername!\n"));
140 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
141 DEBUG(0,("saf_store: "
142 "refusing to store 0 length domain or servername!\n"));
146 key = saf_key(talloc_tos(), domain);
148 DEBUG(1, ("saf_key() failed\n"));
151 expire = time( NULL ) + lp_parm_int(-1, "saf","ttl", SAF_TTL);
153 DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
154 domain, servername, (unsigned int)expire ));
156 ret = gencache_set( key, servername, expire );
163 bool saf_join_store( const char *domain, const char *servername )
169 if ( !domain || !servername ) {
170 DEBUG(2,("saf_join_store: Refusing to store empty domain or servername!\n"));
174 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
175 DEBUG(0,("saf_join_store: refusing to store 0 length domain or servername!\n"));
179 key = saf_join_key(talloc_tos(), domain);
181 DEBUG(1, ("saf_join_key() failed\n"));
184 expire = time( NULL ) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL);
186 DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
187 domain, servername, (unsigned int)expire ));
189 ret = gencache_set( key, servername, expire );
196 bool saf_delete( const char *domain )
202 DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
206 key = saf_join_key(talloc_tos(), domain);
208 DEBUG(1, ("saf_join_key() failed\n"));
211 ret = gencache_del(key);
215 DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain ));
218 key = saf_key(talloc_tos(), domain);
220 DEBUG(1, ("saf_key() failed\n"));
223 ret = gencache_del(key);
227 DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
233 /****************************************************************************
234 ****************************************************************************/
236 char *saf_fetch(TALLOC_CTX *mem_ctx, const char *domain )
243 if ( !domain || strlen(domain) == 0) {
244 DEBUG(2,("saf_fetch: Empty domain name!\n"));
248 key = saf_join_key(talloc_tos(), domain);
250 DEBUG(1, ("saf_join_key() failed\n"));
254 ret = gencache_get( key, mem_ctx, &server, &timeout );
259 DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
264 key = saf_key(talloc_tos(), domain);
266 DEBUG(1, ("saf_key() failed\n"));
270 ret = gencache_get( key, mem_ctx, &server, &timeout );
275 DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
278 DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
285 static void set_socket_addr_v4(struct samba_sockaddr *addr)
287 if (!interpret_string_addr(&addr->u.ss, lp_nbt_client_socket_address(),
288 AI_NUMERICHOST|AI_PASSIVE)) {
289 zero_sockaddr(&addr->u.ss);
290 /* zero_sockaddr sets family to AF_INET. */
291 addr->sa_socklen = sizeof(struct sockaddr_in);
293 if (addr->u.ss.ss_family != AF_INET) {
294 zero_sockaddr(&addr->u.ss);
295 /* zero_sockaddr sets family to AF_INET. */
296 addr->sa_socklen = sizeof(struct sockaddr_in);
300 static struct in_addr my_socket_addr_v4(void)
302 struct samba_sockaddr my_addr = {0};
304 set_socket_addr_v4(&my_addr);
305 return my_addr.u.in.sin_addr;
308 /****************************************************************************
309 Generate a random trn_id.
310 ****************************************************************************/
312 static int generate_trn_id(void)
316 generate_random_buffer((uint8_t *)&id, sizeof(id));
318 return id % (unsigned)0x7FFF;
321 /****************************************************************************
322 Parse a node status response into an array of structures.
323 ****************************************************************************/
325 static struct node_status *parse_node_status(TALLOC_CTX *mem_ctx, char *p,
327 struct node_status_extra *extra)
329 struct node_status *ret;
331 size_t result_count = 0;
333 result_count = CVAL(p,0);
335 if (result_count == 0)
338 ret = talloc_array(mem_ctx, struct node_status,result_count);
343 for (i=0;i< result_count;i++) {
344 strlcpy(ret[i].name,p,16);
345 trim_char(ret[i].name,'\0',' ');
346 ret[i].type = CVAL(p,15);
347 ret[i].flags = p[16];
349 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
350 ret[i].type, ret[i].flags));
353 * Also, pick up the MAC address ...
356 memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
358 *num_names = result_count;
362 struct sock_packet_read_state {
363 struct tevent_context *ev;
364 enum packet_type type;
367 struct nb_packet_reader *reader;
368 struct tevent_req *reader_req;
370 struct tdgram_context *sock;
371 struct tevent_req *socket_req;
373 struct tsocket_address *addr;
375 bool (*validator)(struct packet_struct *p,
379 struct packet_struct *packet;
382 static void sock_packet_read_got_packet(struct tevent_req *subreq);
383 static void sock_packet_read_got_socket(struct tevent_req *subreq);
385 static struct tevent_req *sock_packet_read_send(
387 struct tevent_context *ev,
388 struct tdgram_context *sock,
389 struct nb_packet_reader *reader,
390 enum packet_type type,
392 bool (*validator)(struct packet_struct *p, void *private_data),
395 struct tevent_req *req;
396 struct sock_packet_read_state *state;
398 req = tevent_req_create(mem_ctx, &state,
399 struct sock_packet_read_state);
404 state->reader = reader;
407 state->trn_id = trn_id;
408 state->validator = validator;
409 state->private_data = private_data;
411 if (reader != NULL) {
412 state->reader_req = nb_packet_read_send(state, ev, reader);
413 if (tevent_req_nomem(state->reader_req, req)) {
414 return tevent_req_post(req, ev);
416 tevent_req_set_callback(
417 state->reader_req, sock_packet_read_got_packet, req);
420 state->socket_req = tdgram_recvfrom_send(state, ev, state->sock);
421 if (tevent_req_nomem(state->socket_req, req)) {
422 return tevent_req_post(req, ev);
424 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
430 static void sock_packet_read_got_packet(struct tevent_req *subreq)
432 struct tevent_req *req = tevent_req_callback_data(
433 subreq, struct tevent_req);
434 struct sock_packet_read_state *state = tevent_req_data(
435 req, struct sock_packet_read_state);
438 status = nb_packet_read_recv(subreq, state, &state->packet);
440 TALLOC_FREE(state->reader_req);
442 if (!NT_STATUS_IS_OK(status)) {
443 if (state->socket_req != NULL) {
445 * Still waiting for socket
450 * Both socket and packet reader failed
452 tevent_req_nterror(req, status);
456 if ((state->validator != NULL) &&
457 !state->validator(state->packet, state->private_data)) {
458 DEBUG(10, ("validator failed\n"));
460 TALLOC_FREE(state->packet);
462 state->reader_req = nb_packet_read_send(state, state->ev,
464 if (tevent_req_nomem(state->reader_req, req)) {
467 tevent_req_set_callback(
468 state->reader_req, sock_packet_read_got_packet, req);
472 TALLOC_FREE(state->socket_req);
473 tevent_req_done(req);
476 static void sock_packet_read_got_socket(struct tevent_req *subreq)
478 struct tevent_req *req = tevent_req_callback_data(
479 subreq, struct tevent_req);
480 struct sock_packet_read_state *state = tevent_req_data(
481 req, struct sock_packet_read_state);
482 struct samba_sockaddr addr = {0};
488 received = tdgram_recvfrom_recv(subreq, &err, state,
489 &state->buf, &state->addr);
491 TALLOC_FREE(state->socket_req);
493 if (received == -1) {
494 if (state->reader_req != NULL) {
496 * Still waiting for reader
501 * Both socket and reader failed
503 tevent_req_nterror(req, map_nt_error_from_unix(err));
506 ok = tsocket_address_is_inet(state->addr, "ipv4");
510 ret = tsocket_address_bsd_sockaddr(state->addr,
514 tevent_req_nterror(req, map_nt_error_from_unix(errno));
518 state->packet = parse_packet_talloc(
519 state, (char *)state->buf, received, state->type,
520 addr.u.in.sin_addr, addr.u.in.sin_port);
521 if (state->packet == NULL) {
522 DEBUG(10, ("parse_packet failed\n"));
525 if ((state->trn_id != -1) &&
526 (state->trn_id != packet_trn_id(state->packet))) {
527 DEBUG(10, ("Expected transaction id %d, got %d\n",
528 state->trn_id, packet_trn_id(state->packet)));
532 if ((state->validator != NULL) &&
533 !state->validator(state->packet, state->private_data)) {
534 DEBUG(10, ("validator failed\n"));
538 tevent_req_done(req);
542 TALLOC_FREE(state->packet);
543 TALLOC_FREE(state->buf);
544 TALLOC_FREE(state->addr);
546 state->socket_req = tdgram_recvfrom_send(state, state->ev, state->sock);
547 if (tevent_req_nomem(state->socket_req, req)) {
550 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
554 static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
556 struct packet_struct **ppacket)
558 struct sock_packet_read_state *state = tevent_req_data(
559 req, struct sock_packet_read_state);
562 if (tevent_req_is_nterror(req, &status)) {
565 *ppacket = talloc_move(mem_ctx, &state->packet);
569 struct nb_trans_state {
570 struct tevent_context *ev;
571 struct tdgram_context *sock;
572 struct nb_packet_reader *reader;
574 struct tsocket_address *src_addr;
575 struct tsocket_address *dst_addr;
578 enum packet_type type;
581 bool (*validator)(struct packet_struct *p,
585 struct packet_struct *packet;
588 static void nb_trans_got_reader(struct tevent_req *subreq);
589 static void nb_trans_done(struct tevent_req *subreq);
590 static void nb_trans_sent(struct tevent_req *subreq);
591 static void nb_trans_send_next(struct tevent_req *subreq);
593 static struct tevent_req *nb_trans_send(
595 struct tevent_context *ev,
596 const struct samba_sockaddr *_my_addr,
597 const struct samba_sockaddr *_dst_addr,
599 uint8_t *buf, size_t buflen,
600 enum packet_type type, int trn_id,
601 bool (*validator)(struct packet_struct *p,
605 const struct sockaddr *my_addr = &_my_addr->u.sa;
606 size_t my_addr_len = sizeof(_my_addr->u.in); /*We know it's AF_INET.*/
607 const struct sockaddr *dst_addr = &_dst_addr->u.sa;
608 size_t dst_addr_len = sizeof(_dst_addr->u.in); /*We know it's AF_INET.*/
609 struct tevent_req *req, *subreq;
610 struct nb_trans_state *state;
613 req = tevent_req_create(mem_ctx, &state, struct nb_trans_state);
619 state->buflen = buflen;
621 state->trn_id = trn_id;
622 state->validator = validator;
623 state->private_data = private_data;
625 ret = tsocket_address_bsd_from_sockaddr(state,
626 my_addr, my_addr_len,
629 tevent_req_nterror(req, map_nt_error_from_unix(errno));
630 return tevent_req_post(req, ev);
633 ret = tsocket_address_bsd_from_sockaddr(state,
634 dst_addr, dst_addr_len,
637 tevent_req_nterror(req, map_nt_error_from_unix(errno));
638 return tevent_req_post(req, ev);
641 ret = tdgram_inet_udp_broadcast_socket(state->src_addr, state,
644 tevent_req_nterror(req, map_nt_error_from_unix(errno));
645 return tevent_req_post(req, ev);
648 subreq = nb_packet_reader_send(state,
650 global_nmbd_socket_dir(),
654 if (tevent_req_nomem(subreq, req)) {
655 return tevent_req_post(req, ev);
657 tevent_req_set_callback(subreq, nb_trans_got_reader, req);
661 static void nb_trans_got_reader(struct tevent_req *subreq)
663 struct tevent_req *req = tevent_req_callback_data(
664 subreq, struct tevent_req);
665 struct nb_trans_state *state = tevent_req_data(
666 req, struct nb_trans_state);
669 status = nb_packet_reader_recv(subreq, state, &state->reader);
672 if (!NT_STATUS_IS_OK(status)) {
673 DEBUG(10, ("nmbd not around\n"));
674 state->reader = NULL;
677 subreq = sock_packet_read_send(
678 state, state->ev, state->sock,
679 state->reader, state->type, state->trn_id,
680 state->validator, state->private_data);
681 if (tevent_req_nomem(subreq, req)) {
684 tevent_req_set_callback(subreq, nb_trans_done, req);
686 subreq = tdgram_sendto_send(state, state->ev,
688 state->buf, state->buflen,
690 if (tevent_req_nomem(subreq, req)) {
693 tevent_req_set_callback(subreq, nb_trans_sent, req);
696 static void nb_trans_sent(struct tevent_req *subreq)
698 struct tevent_req *req = tevent_req_callback_data(
699 subreq, struct tevent_req);
700 struct nb_trans_state *state = tevent_req_data(
701 req, struct nb_trans_state);
705 sent = tdgram_sendto_recv(subreq, &err);
708 DEBUG(10, ("sendto failed: %s\n", strerror(err)));
709 tevent_req_nterror(req, map_nt_error_from_unix(err));
712 subreq = tevent_wakeup_send(state, state->ev,
713 timeval_current_ofs(1, 0));
714 if (tevent_req_nomem(subreq, req)) {
717 tevent_req_set_callback(subreq, nb_trans_send_next, req);
720 static void nb_trans_send_next(struct tevent_req *subreq)
722 struct tevent_req *req = tevent_req_callback_data(
723 subreq, struct tevent_req);
724 struct nb_trans_state *state = tevent_req_data(
725 req, struct nb_trans_state);
728 ret = tevent_wakeup_recv(subreq);
731 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
734 subreq = tdgram_sendto_send(state, state->ev,
736 state->buf, state->buflen,
738 if (tevent_req_nomem(subreq, req)) {
741 tevent_req_set_callback(subreq, nb_trans_sent, req);
744 static void nb_trans_done(struct tevent_req *subreq)
746 struct tevent_req *req = tevent_req_callback_data(
747 subreq, struct tevent_req);
748 struct nb_trans_state *state = tevent_req_data(
749 req, struct nb_trans_state);
752 status = sock_packet_read_recv(subreq, state, &state->packet);
754 if (tevent_req_nterror(req, status)) {
757 tevent_req_done(req);
760 static NTSTATUS nb_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
761 struct packet_struct **ppacket)
763 struct nb_trans_state *state = tevent_req_data(
764 req, struct nb_trans_state);
767 if (tevent_req_is_nterror(req, &status)) {
770 *ppacket = talloc_move(mem_ctx, &state->packet);
774 /****************************************************************************
775 Do a NBT node status query on an open socket and return an array of
776 structures holding the returned names or NULL if the query failed.
777 **************************************************************************/
779 struct node_status_query_state {
780 struct samba_sockaddr my_addr;
781 struct samba_sockaddr addr;
784 struct packet_struct *packet;
787 static bool node_status_query_validator(struct packet_struct *p,
789 static void node_status_query_done(struct tevent_req *subreq);
791 struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
792 struct tevent_context *ev,
793 struct nmb_name *name,
794 const struct sockaddr_storage *addr)
796 struct tevent_req *req, *subreq;
797 struct node_status_query_state *state;
798 struct packet_struct p;
799 struct nmb_packet *nmb = &p.packet.nmb;
802 req = tevent_req_create(mem_ctx, &state,
803 struct node_status_query_state);
808 if (addr->ss_family != AF_INET) {
809 /* Can't do node status to IPv6 */
810 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
811 return tevent_req_post(req, ev);
814 ok = sockaddr_storage_to_samba_sockaddr(&state->addr, addr);
816 /* node status must be IPv4 */
817 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
818 return tevent_req_post(req, ev);
820 state->addr.u.in.sin_port = htons(NMB_PORT);
822 set_socket_addr_v4(&state->my_addr);
825 nmb->header.name_trn_id = generate_trn_id();
826 nmb->header.opcode = 0;
827 nmb->header.response = false;
828 nmb->header.nm_flags.bcast = false;
829 nmb->header.nm_flags.recursion_available = false;
830 nmb->header.nm_flags.recursion_desired = false;
831 nmb->header.nm_flags.trunc = false;
832 nmb->header.nm_flags.authoritative = false;
833 nmb->header.rcode = 0;
834 nmb->header.qdcount = 1;
835 nmb->header.ancount = 0;
836 nmb->header.nscount = 0;
837 nmb->header.arcount = 0;
838 nmb->question.question_name = *name;
839 nmb->question.question_type = 0x21;
840 nmb->question.question_class = 0x1;
842 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
844 if (state->buflen == 0) {
845 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
846 DEBUG(10, ("build_packet failed\n"));
847 return tevent_req_post(req, ev);
850 subreq = nb_trans_send(state,
858 nmb->header.name_trn_id,
859 node_status_query_validator,
861 if (tevent_req_nomem(subreq, req)) {
862 DEBUG(10, ("nb_trans_send failed\n"));
863 return tevent_req_post(req, ev);
865 if (!tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0))) {
866 return tevent_req_post(req, ev);
868 tevent_req_set_callback(subreq, node_status_query_done, req);
872 static bool node_status_query_validator(struct packet_struct *p,
875 struct nmb_packet *nmb = &p->packet.nmb;
878 if (nmb->header.opcode != 0 ||
879 nmb->header.nm_flags.bcast ||
881 !nmb->header.ancount ||
882 nmb->answers->rr_type != 0x21) {
884 * XXXX what do we do with this? could be a redirect,
885 * but we'll discard it for the moment
892 static void node_status_query_done(struct tevent_req *subreq)
894 struct tevent_req *req = tevent_req_callback_data(
895 subreq, struct tevent_req);
896 struct node_status_query_state *state = tevent_req_data(
897 req, struct node_status_query_state);
900 status = nb_trans_recv(subreq, state, &state->packet);
902 if (tevent_req_nterror(req, status)) {
905 tevent_req_done(req);
908 NTSTATUS node_status_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
909 struct node_status **pnode_status,
911 struct node_status_extra *extra)
913 struct node_status_query_state *state = tevent_req_data(
914 req, struct node_status_query_state);
915 struct node_status *node_status;
916 size_t num_names = 0;
919 if (tevent_req_is_nterror(req, &status)) {
922 node_status = parse_node_status(
923 mem_ctx, &state->packet->packet.nmb.answers->rdata[0],
925 if (node_status == NULL) {
926 return NT_STATUS_NO_MEMORY;
928 *pnode_status = node_status;
929 *pnum_names = num_names;
933 NTSTATUS node_status_query(TALLOC_CTX *mem_ctx, struct nmb_name *name,
934 const struct sockaddr_storage *addr,
935 struct node_status **pnode_status,
937 struct node_status_extra *extra)
939 TALLOC_CTX *frame = talloc_stackframe();
940 struct tevent_context *ev;
941 struct tevent_req *req;
942 NTSTATUS status = NT_STATUS_NO_MEMORY;
944 ev = samba_tevent_context_init(frame);
948 req = node_status_query_send(ev, ev, name, addr);
952 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
955 status = node_status_query_recv(req, mem_ctx, pnode_status,
962 static bool name_status_lmhosts(const struct sockaddr_storage *paddr,
963 int qname_type, fstring pname)
968 struct samba_sockaddr addr_in = {0};
969 struct samba_sockaddr addr = {0};
972 ok = sockaddr_storage_to_samba_sockaddr(&addr_in, paddr);
976 if (addr_in.u.ss.ss_family != AF_INET) {
980 f = startlmhosts(get_dyn_LMHOSTSFILE());
985 while (getlmhostsent(talloc_tos(), f, &name, &name_type, &addr.u.ss)) {
986 if (addr.u.ss.ss_family != AF_INET) {
989 if (name_type != qname_type) {
992 if (sockaddr_equal(&addr_in.u.sa, &addr.u.sa)) {
993 fstrcpy(pname, name);
1002 /****************************************************************************
1003 Find the first type XX name in a node status reply - used for finding
1004 a servers name given its IP. Return the matched name in *name.
1005 **************************************************************************/
1007 bool name_status_find(const char *q_name,
1010 const struct sockaddr_storage *to_ss,
1013 char addr[INET6_ADDRSTRLEN];
1014 struct node_status *addrs = NULL;
1015 struct nmb_name nname;
1016 size_t count = 0, i;
1017 bool result = false;
1020 if (lp_disable_netbios()) {
1021 DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n",
1026 print_sockaddr(addr, sizeof(addr), to_ss);
1028 DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
1031 /* Check the cache first. */
1033 if (namecache_status_fetch(q_name, q_type, type, to_ss, name)) {
1037 if (to_ss->ss_family != AF_INET) {
1038 /* Can't do node status to IPv6 */
1042 result = name_status_lmhosts(to_ss, type, name);
1044 DBG_DEBUG("Found name %s in lmhosts\n", name);
1045 namecache_status_store(q_name, q_type, type, to_ss, name);
1049 /* W2K PDC's seem not to respond to '*'#0. JRA */
1050 make_nmb_name(&nname, q_name, q_type);
1051 status = node_status_query(talloc_tos(), &nname, to_ss,
1052 &addrs, &count, NULL);
1053 if (!NT_STATUS_IS_OK(status)) {
1057 for (i=0;i<count;i++) {
1058 /* Find first one of the requested type that's not a GROUP. */
1059 if (addrs[i].type == type && ! (addrs[i].flags & 0x80))
1065 pull_ascii_nstring(name, sizeof(fstring), addrs[i].name);
1067 /* Store the result in the cache. */
1068 /* but don't store an entry for 0x1c names here. Here we have
1069 a single host and DOMAIN<0x1c> names should be a list of hosts */
1071 if ( q_type != 0x1c ) {
1072 namecache_status_store(q_name, q_type, type, to_ss, name);
1080 DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
1083 DEBUGADD(10, (", name %s ip address is %s", name, addr));
1091 * comparison function used by sort_addr_list
1093 * This comparison is intransitive in sort if a socket has an invalid
1094 * family (i.e., not IPv4 or IPv6), or an interface doesn't support
1095 * the family. Say we have sockaddrs with IP versions {4,5,6}, of
1096 * which 5 is invalid. By this function, 4 == 5 and 6 == 5, but 4 !=
1097 * 6. This is of course a consequence of cmp() being unable to
1098 * communicate error.
1101 static int addr_compare(const struct sockaddr_storage *ss1,
1102 const struct sockaddr_storage *ss2)
1104 int max_bits1=0, max_bits2=0;
1105 int num_interfaces = iface_count();
1107 struct samba_sockaddr sa1;
1108 struct samba_sockaddr sa2;
1111 ok = sockaddr_storage_to_samba_sockaddr(&sa1, ss1);
1113 return 0; /* No change. */
1116 ok = sockaddr_storage_to_samba_sockaddr(&sa2, ss2);
1118 return 0; /* No change. */
1121 /* Sort IPv4 addresses first. */
1122 if (sa1.u.ss.ss_family != sa2.u.ss.ss_family) {
1123 if (sa2.u.ss.ss_family == AF_INET) {
1130 /* Here we know both addresses are of the same
1133 for (i=0;i<num_interfaces;i++) {
1134 struct samba_sockaddr sif = {0};
1135 const unsigned char *p_ss1 = NULL;
1136 const unsigned char *p_ss2 = NULL;
1137 const unsigned char *p_if = NULL;
1141 ok = sockaddr_storage_to_samba_sockaddr(&sif, iface_n_bcast(i));
1143 return 0; /* No change. */
1145 if (sif.u.ss.ss_family != sa1.u.ss.ss_family) {
1146 /* Ignore interfaces of the wrong type. */
1149 if (sif.u.ss.ss_family == AF_INET) {
1150 p_if = (const unsigned char *)&sif.u.in.sin_addr;
1151 p_ss1 = (const unsigned char *)&sa1.u.in.sin_addr;
1152 p_ss2 = (const unsigned char *)&sa2.u.in.sin_addr;
1155 #if defined(HAVE_IPV6)
1156 if (sif.u.ss.ss_family == AF_INET6) {
1157 p_if = (const unsigned char *)&sif.u.in6.sin6_addr;
1158 p_ss1 = (const unsigned char *)&sa1.u.in6.sin6_addr;
1159 p_ss2 = (const unsigned char *)&sa2.u.in6.sin6_addr;
1163 if (!p_ss1 || !p_ss2 || !p_if || len == 0) {
1166 bits1 = matching_len_bits(p_ss1, p_if, len);
1167 bits2 = matching_len_bits(p_ss2, p_if, len);
1168 max_bits1 = MAX(bits1, max_bits1);
1169 max_bits2 = MAX(bits2, max_bits2);
1172 /* Bias towards directly reachable IPs */
1173 if (iface_local(&sa1.u.sa)) {
1174 if (sa1.u.ss.ss_family == AF_INET) {
1180 if (iface_local(&sa2.u.sa)) {
1181 if (sa2.u.ss.ss_family == AF_INET) {
1187 return NUMERIC_CMP(max_bits2, max_bits1);
1191 sort an IP list so that names that are close to one of our interfaces
1192 are at the top. This prevents the problem where a WINS server returns an IP
1193 that is not reachable from our subnet as the first match
1196 static void sort_addr_list(struct sockaddr_storage *sslist, size_t count)
1202 TYPESAFE_QSORT(sslist, count, addr_compare);
1205 static int samba_sockaddr_compare(struct samba_sockaddr *sa1,
1206 struct samba_sockaddr *sa2)
1208 return addr_compare(&sa1->u.ss, &sa2->u.ss);
1211 static void sort_sa_list(struct samba_sockaddr *salist, size_t count)
1217 TYPESAFE_QSORT(salist, count, samba_sockaddr_compare);
1220 /**********************************************************************
1221 Remove any duplicate address/port pairs in the samba_sockaddr array.
1222 *********************************************************************/
1224 size_t remove_duplicate_addrs2(struct samba_sockaddr *salist, size_t count )
1228 DBG_DEBUG("looking for duplicate address/port pairs\n");
1230 /* One loop to set duplicates to a zero addr. */
1231 for (i=0; i < count; i++) {
1232 if (is_zero_addr(&salist[i].u.ss)) {
1236 for (j=i+1; j<count; j++) {
1237 if (sockaddr_equal(&salist[i].u.sa, &salist[j].u.sa)) {
1238 zero_sockaddr(&salist[j].u.ss);
1243 /* Now remove any addresses set to zero above. */
1244 for (i = 0; i < count; i++) {
1246 is_zero_addr(&salist[i].u.ss)) {
1247 ARRAY_DEL_ELEMENT(salist, i, count);
1255 static bool prioritize_ipv4_list(struct samba_sockaddr *salist, size_t count)
1257 TALLOC_CTX *frame = talloc_stackframe();
1258 struct samba_sockaddr *salist_new = talloc_array(frame,
1259 struct samba_sockaddr,
1263 if (salist_new == NULL) {
1270 /* Copy IPv4 first. */
1271 for (i = 0; i < count; i++) {
1272 if (salist[i].u.ss.ss_family == AF_INET) {
1273 salist_new[j++] = salist[i];
1278 for (i = 0; i < count; i++) {
1279 if (salist[i].u.ss.ss_family != AF_INET) {
1280 salist_new[j++] = salist[i];
1284 memcpy(salist, salist_new, sizeof(struct samba_sockaddr)*count);
1289 /****************************************************************************
1290 Do a netbios name query to find someones IP.
1291 Returns an array of IP addresses or NULL if none.
1292 *count will be set to the number of addresses returned.
1293 *timed_out is set if we failed by timing out
1294 ****************************************************************************/
1296 struct name_query_state {
1297 struct samba_sockaddr my_addr;
1298 struct samba_sockaddr addr;
1300 bool bcast_star_query;
1306 NTSTATUS validate_error;
1309 struct sockaddr_storage *addrs;
1313 static bool name_query_validator(struct packet_struct *p, void *private_data);
1314 static void name_query_done(struct tevent_req *subreq);
1316 struct tevent_req *name_query_send(TALLOC_CTX *mem_ctx,
1317 struct tevent_context *ev,
1318 const char *name, int name_type,
1319 bool bcast, bool recurse,
1320 const struct sockaddr_storage *addr)
1322 struct tevent_req *req, *subreq;
1323 struct name_query_state *state;
1324 struct packet_struct p;
1325 struct nmb_packet *nmb = &p.packet.nmb;
1328 req = tevent_req_create(mem_ctx, &state, struct name_query_state);
1332 state->bcast = bcast;
1334 if (addr->ss_family != AF_INET) {
1335 /* Can't do node status to IPv6 */
1336 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1337 return tevent_req_post(req, ev);
1340 if (lp_disable_netbios()) {
1341 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
1343 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
1344 return tevent_req_post(req, ev);
1347 ok = sockaddr_storage_to_samba_sockaddr(&state->addr, addr);
1349 /* Node status must be IPv4 */
1350 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1351 return tevent_req_post(req, ev);
1353 state->addr.u.in.sin_port = htons(NMB_PORT);
1355 set_socket_addr_v4(&state->my_addr);
1358 nmb->header.name_trn_id = generate_trn_id();
1359 nmb->header.opcode = 0;
1360 nmb->header.response = false;
1361 nmb->header.nm_flags.bcast = bcast;
1362 nmb->header.nm_flags.recursion_available = false;
1363 nmb->header.nm_flags.recursion_desired = recurse;
1364 nmb->header.nm_flags.trunc = false;
1365 nmb->header.nm_flags.authoritative = false;
1366 nmb->header.rcode = 0;
1367 nmb->header.qdcount = 1;
1368 nmb->header.ancount = 0;
1369 nmb->header.nscount = 0;
1370 nmb->header.arcount = 0;
1372 if (bcast && (strcmp(name, "*")==0)) {
1374 * We're doing a broadcast query for all
1375 * names in the area. Remember this so
1376 * we will wait for all names within
1377 * the timeout period.
1379 state->bcast_star_query = true;
1382 make_nmb_name(&nmb->question.question_name,name,name_type);
1384 nmb->question.question_type = 0x20;
1385 nmb->question.question_class = 0x1;
1387 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
1389 if (state->buflen == 0) {
1390 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1391 DEBUG(10, ("build_packet failed\n"));
1392 return tevent_req_post(req, ev);
1395 subreq = nb_trans_send(state,
1403 nmb->header.name_trn_id,
1404 name_query_validator,
1406 if (tevent_req_nomem(subreq, req)) {
1407 DEBUG(10, ("nb_trans_send failed\n"));
1408 return tevent_req_post(req, ev);
1410 tevent_req_set_callback(subreq, name_query_done, req);
1414 static bool name_query_validator(struct packet_struct *p, void *private_data)
1416 struct name_query_state *state = talloc_get_type_abort(
1417 private_data, struct name_query_state);
1418 struct nmb_packet *nmb = &p->packet.nmb;
1419 struct sockaddr_storage *tmp_addrs;
1420 bool got_unique_netbios_name = false;
1423 debug_nmb_packet(p);
1426 * If we get a Negative Name Query Response from a WINS
1427 * server, we should report it and give up.
1429 if( 0 == nmb->header.opcode /* A query response */
1430 && !state->bcast /* from a WINS server */
1431 && nmb->header.rcode /* Error returned */
1434 if( DEBUGLVL( 3 ) ) {
1435 /* Only executed if DEBUGLEVEL >= 3 */
1436 dbgtext( "Negative name query "
1437 "response, rcode 0x%02x: ",
1438 nmb->header.rcode );
1439 switch( nmb->header.rcode ) {
1441 dbgtext("Request was invalidly formatted.\n");
1444 dbgtext("Problem with NBNS, cannot process "
1448 dbgtext("The name requested does not "
1452 dbgtext("Unsupported request error.\n");
1455 dbgtext("Query refused error.\n");
1458 dbgtext("Unrecognized error code.\n" );
1464 * We accept this packet as valid, but tell the upper
1465 * layers that it's a negative response.
1467 state->validate_error = NT_STATUS_NOT_FOUND;
1471 if (nmb->header.opcode != 0 ||
1472 nmb->header.nm_flags.bcast ||
1473 nmb->header.rcode ||
1474 !nmb->header.ancount) {
1476 * XXXX what do we do with this? Could be a redirect,
1477 * but we'll discard it for the moment.
1482 tmp_addrs = talloc_realloc(
1483 state, state->addrs, struct sockaddr_storage,
1484 state->num_addrs + nmb->answers->rdlength/6);
1485 if (tmp_addrs == NULL) {
1486 state->validate_error = NT_STATUS_NO_MEMORY;
1489 state->addrs = tmp_addrs;
1491 DEBUG(2,("Got a positive name query response "
1492 "from %s ( ", inet_ntoa(p->ip)));
1494 for (i=0; i<nmb->answers->rdlength/6; i++) {
1497 struct sockaddr_storage addr;
1498 struct samba_sockaddr sa = {0};
1502 flags = RSVAL(&nmb->answers->rdata[i*6], 0);
1503 got_unique_netbios_name |= ((flags & 0x8000) == 0);
1505 putip((char *)&ip,&nmb->answers->rdata[2+i*6]);
1506 in_addr_to_sockaddr_storage(&addr, ip);
1508 ok = sockaddr_storage_to_samba_sockaddr(&sa, &addr);
1513 if (is_zero_addr(&sa.u.ss)) {
1517 for (j=0; j<state->num_addrs; j++) {
1518 struct samba_sockaddr sa_j = {0};
1520 ok = sockaddr_storage_to_samba_sockaddr(&sa_j,
1525 if (sockaddr_equal(&sa.u.sa, &sa_j.u.sa)) {
1529 if (j < state->num_addrs) {
1530 /* Already got it */
1534 DEBUGADD(2,("%s ",inet_ntoa(ip)));
1536 state->addrs[state->num_addrs] = addr;
1538 if (state->num_addrs + 1 < state->num_addrs) {
1541 state->num_addrs += 1;
1543 DEBUGADD(2,(")\n"));
1545 /* We add the flags back ... */
1546 if (nmb->header.response)
1547 state->flags |= NM_FLAGS_RS;
1548 if (nmb->header.nm_flags.authoritative)
1549 state->flags |= NM_FLAGS_AA;
1550 if (nmb->header.nm_flags.trunc)
1551 state->flags |= NM_FLAGS_TC;
1552 if (nmb->header.nm_flags.recursion_desired)
1553 state->flags |= NM_FLAGS_RD;
1554 if (nmb->header.nm_flags.recursion_available)
1555 state->flags |= NM_FLAGS_RA;
1556 if (nmb->header.nm_flags.bcast)
1557 state->flags |= NM_FLAGS_B;
1561 * We have to collect all entries coming in from broadcast
1562 * queries. If we got a unique name and we are not querying
1563 * all names registered within broadcast area (query
1564 * for the name '*', so state->bcast_star_query is set),
1567 return (got_unique_netbios_name && !state->bcast_star_query);
1570 * WINS responses are accepted when they are received
1575 static void name_query_done(struct tevent_req *subreq)
1577 struct tevent_req *req = tevent_req_callback_data(
1578 subreq, struct tevent_req);
1579 struct name_query_state *state = tevent_req_data(
1580 req, struct name_query_state);
1582 struct packet_struct *p = NULL;
1584 status = nb_trans_recv(subreq, state, &p);
1585 TALLOC_FREE(subreq);
1586 if (tevent_req_nterror(req, status)) {
1589 if (!NT_STATUS_IS_OK(state->validate_error)) {
1590 tevent_req_nterror(req, state->validate_error);
1593 tevent_req_done(req);
1596 NTSTATUS name_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1597 struct sockaddr_storage **addrs, size_t *num_addrs,
1600 struct name_query_state *state = tevent_req_data(
1601 req, struct name_query_state);
1604 if (tevent_req_is_nterror(req, &status)) {
1606 NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
1608 * In the broadcast case we collect replies until the
1611 status = NT_STATUS_OK;
1613 if (!NT_STATUS_IS_OK(status)) {
1617 if (state->num_addrs == 0) {
1618 return NT_STATUS_NOT_FOUND;
1620 *addrs = talloc_move(mem_ctx, &state->addrs);
1621 sort_addr_list(*addrs, state->num_addrs);
1622 *num_addrs = state->num_addrs;
1623 if (flags != NULL) {
1624 *flags = state->flags;
1626 return NT_STATUS_OK;
1629 NTSTATUS name_query(const char *name, int name_type,
1630 bool bcast, bool recurse,
1631 const struct sockaddr_storage *to_ss,
1632 TALLOC_CTX *mem_ctx,
1633 struct sockaddr_storage **addrs,
1634 size_t *num_addrs, uint8_t *flags)
1636 TALLOC_CTX *frame = talloc_stackframe();
1637 struct tevent_context *ev;
1638 struct tevent_req *req;
1639 struct timeval timeout;
1640 NTSTATUS status = NT_STATUS_NO_MEMORY;
1642 ev = samba_tevent_context_init(frame);
1646 req = name_query_send(ev, ev, name, name_type, bcast, recurse, to_ss);
1651 timeout = timeval_current_ofs(0, 250000);
1653 timeout = timeval_current_ofs(2, 0);
1655 if (!tevent_req_set_endtime(req, ev, timeout)) {
1658 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1661 status = name_query_recv(req, mem_ctx, addrs, num_addrs, flags);
1667 struct name_queries_state {
1668 struct tevent_context *ev;
1673 const struct sockaddr_storage *addrs;
1678 struct tevent_req **subreqs;
1679 size_t num_received;
1682 size_t received_index;
1683 struct sockaddr_storage *result_addrs;
1684 size_t num_result_addrs;
1688 static void name_queries_done(struct tevent_req *subreq);
1689 static void name_queries_next(struct tevent_req *subreq);
1692 * Send a name query to multiple destinations with a wait time in between
1695 static struct tevent_req *name_queries_send(
1696 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1697 const char *name, int name_type,
1698 bool bcast, bool recurse,
1699 const struct sockaddr_storage *addrs,
1700 size_t num_addrs, int wait_msec, int timeout_msec)
1702 struct tevent_req *req, *subreq;
1703 struct name_queries_state *state;
1705 req = tevent_req_create(mem_ctx, &state,
1706 struct name_queries_state);
1712 state->name_type = name_type;
1713 state->bcast = bcast;
1714 state->recurse = recurse;
1715 state->addrs = addrs;
1716 state->num_addrs = num_addrs;
1717 state->wait_msec = wait_msec;
1718 state->timeout_msec = timeout_msec;
1720 state->subreqs = talloc_zero_array(
1721 state, struct tevent_req *, num_addrs);
1722 if (tevent_req_nomem(state->subreqs, req)) {
1723 return tevent_req_post(req, ev);
1725 state->num_sent = 0;
1727 subreq = name_query_send(
1728 state->subreqs, state->ev, name, name_type, bcast, recurse,
1729 &state->addrs[state->num_sent]);
1730 if (tevent_req_nomem(subreq, req)) {
1731 return tevent_req_post(req, ev);
1733 if (!tevent_req_set_endtime(
1735 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1736 return tevent_req_post(req, ev);
1738 tevent_req_set_callback(subreq, name_queries_done, req);
1740 state->subreqs[state->num_sent] = subreq;
1741 state->num_sent += 1;
1743 if (state->num_sent < state->num_addrs) {
1744 subreq = tevent_wakeup_send(
1746 timeval_current_ofs(0, state->wait_msec * 1000));
1747 if (tevent_req_nomem(subreq, req)) {
1748 return tevent_req_post(req, ev);
1750 tevent_req_set_callback(subreq, name_queries_next, req);
1755 static void name_queries_done(struct tevent_req *subreq)
1757 struct tevent_req *req = tevent_req_callback_data(
1758 subreq, struct tevent_req);
1759 struct name_queries_state *state = tevent_req_data(
1760 req, struct name_queries_state);
1764 status = name_query_recv(subreq, state, &state->result_addrs,
1765 &state->num_result_addrs, &state->flags);
1767 for (i=0; i<state->num_sent; i++) {
1768 if (state->subreqs[i] == subreq) {
1772 if (i == state->num_sent) {
1773 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1776 TALLOC_FREE(state->subreqs[i]);
1779 if (state->num_received + 1 < state->num_received) {
1780 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1783 state->num_received += 1;
1785 if (!NT_STATUS_IS_OK(status)) {
1787 if (state->num_received >= state->num_addrs) {
1788 tevent_req_nterror(req, status);
1792 * Still outstanding requests, just wait
1796 state->received_index = i;
1797 tevent_req_done(req);
1800 static void name_queries_next(struct tevent_req *subreq)
1802 struct tevent_req *req = tevent_req_callback_data(
1803 subreq, struct tevent_req);
1804 struct name_queries_state *state = tevent_req_data(
1805 req, struct name_queries_state);
1807 if (!tevent_wakeup_recv(subreq)) {
1808 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1812 subreq = name_query_send(
1813 state->subreqs, state->ev,
1814 state->name, state->name_type, state->bcast, state->recurse,
1815 &state->addrs[state->num_sent]);
1816 if (tevent_req_nomem(subreq, req)) {
1819 tevent_req_set_callback(subreq, name_queries_done, req);
1820 if (!tevent_req_set_endtime(
1822 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1825 state->subreqs[state->num_sent] = subreq;
1826 state->num_sent += 1;
1828 if (state->num_sent < state->num_addrs) {
1829 subreq = tevent_wakeup_send(
1831 timeval_current_ofs(0, state->wait_msec * 1000));
1832 if (tevent_req_nomem(subreq, req)) {
1835 tevent_req_set_callback(subreq, name_queries_next, req);
1839 static NTSTATUS name_queries_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1840 struct sockaddr_storage **result_addrs,
1841 size_t *num_result_addrs, uint8_t *flags,
1842 size_t *received_index)
1844 struct name_queries_state *state = tevent_req_data(
1845 req, struct name_queries_state);
1848 if (tevent_req_is_nterror(req, &status)) {
1852 if (result_addrs != NULL) {
1853 *result_addrs = talloc_move(mem_ctx, &state->result_addrs);
1855 if (num_result_addrs != NULL) {
1856 *num_result_addrs = state->num_result_addrs;
1858 if (flags != NULL) {
1859 *flags = state->flags;
1861 if (received_index != NULL) {
1862 *received_index = state->received_index;
1864 return NT_STATUS_OK;
1867 /********************************************************
1868 Resolve via "bcast" method.
1869 *********************************************************/
1871 struct name_resolve_bcast_state {
1872 struct sockaddr_storage *addrs;
1876 static void name_resolve_bcast_done(struct tevent_req *subreq);
1878 struct tevent_req *name_resolve_bcast_send(TALLOC_CTX *mem_ctx,
1879 struct tevent_context *ev,
1883 struct tevent_req *req, *subreq;
1884 struct name_resolve_bcast_state *state;
1885 struct sockaddr_storage *bcast_addrs;
1886 size_t i, num_addrs, num_bcast_addrs;
1888 req = tevent_req_create(mem_ctx, &state,
1889 struct name_resolve_bcast_state);
1894 if (lp_disable_netbios()) {
1895 DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
1897 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1898 return tevent_req_post(req, ev);
1902 * "bcast" means do a broadcast lookup on all the local interfaces.
1905 DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
1906 "for name %s<0x%x>\n", name, name_type));
1908 num_addrs = iface_count();
1909 bcast_addrs = talloc_array(state, struct sockaddr_storage, num_addrs);
1910 if (tevent_req_nomem(bcast_addrs, req)) {
1911 return tevent_req_post(req, ev);
1915 * Lookup the name on all the interfaces, return on
1916 * the first successful match.
1918 num_bcast_addrs = 0;
1920 for (i=0; i<num_addrs; i++) {
1921 const struct sockaddr_storage *pss = iface_n_bcast(i);
1923 if (pss->ss_family != AF_INET) {
1926 bcast_addrs[num_bcast_addrs] = *pss;
1927 num_bcast_addrs += 1;
1930 subreq = name_queries_send(state, ev, name, name_type, true, true,
1931 bcast_addrs, num_bcast_addrs, 0, 250);
1932 if (tevent_req_nomem(subreq, req)) {
1933 return tevent_req_post(req, ev);
1935 tevent_req_set_callback(subreq, name_resolve_bcast_done, req);
1939 static void name_resolve_bcast_done(struct tevent_req *subreq)
1941 struct tevent_req *req = tevent_req_callback_data(
1942 subreq, struct tevent_req);
1943 struct name_resolve_bcast_state *state = tevent_req_data(
1944 req, struct name_resolve_bcast_state);
1947 status = name_queries_recv(subreq, state,
1948 &state->addrs, &state->num_addrs,
1950 TALLOC_FREE(subreq);
1951 if (tevent_req_nterror(req, status)) {
1954 tevent_req_done(req);
1957 NTSTATUS name_resolve_bcast_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1958 struct sockaddr_storage **addrs,
1961 struct name_resolve_bcast_state *state = tevent_req_data(
1962 req, struct name_resolve_bcast_state);
1965 if (tevent_req_is_nterror(req, &status)) {
1968 *addrs = talloc_move(mem_ctx, &state->addrs);
1969 *num_addrs = state->num_addrs;
1970 return NT_STATUS_OK;
1973 NTSTATUS name_resolve_bcast(TALLOC_CTX *mem_ctx,
1976 struct sockaddr_storage **return_iplist,
1977 size_t *return_count)
1979 TALLOC_CTX *frame = talloc_stackframe();
1980 struct tevent_context *ev;
1981 struct tevent_req *req;
1982 NTSTATUS status = NT_STATUS_NO_MEMORY;
1984 ev = samba_tevent_context_init(frame);
1988 req = name_resolve_bcast_send(frame, ev, name, name_type);
1992 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1995 status = name_resolve_bcast_recv(req, mem_ctx, return_iplist,
2002 struct query_wins_list_state {
2003 struct tevent_context *ev;
2006 struct in_addr *servers;
2008 struct sockaddr_storage server;
2011 struct sockaddr_storage *addrs;
2016 static void query_wins_list_done(struct tevent_req *subreq);
2019 * Query a list of (replicating) wins servers in sequence, call them
2020 * dead if they don't reply
2023 static struct tevent_req *query_wins_list_send(
2024 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2025 struct in_addr src_ip, const char *name, uint8_t name_type,
2026 struct in_addr *servers, size_t num_servers)
2028 struct tevent_req *req, *subreq;
2029 struct query_wins_list_state *state;
2031 req = tevent_req_create(mem_ctx, &state,
2032 struct query_wins_list_state);
2038 state->name_type = name_type;
2039 state->servers = servers;
2040 state->num_servers = num_servers;
2042 if (state->num_servers == 0) {
2043 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2044 return tevent_req_post(req, ev);
2047 in_addr_to_sockaddr_storage(
2048 &state->server, state->servers[state->num_sent]);
2050 subreq = name_query_send(state, state->ev,
2051 state->name, state->name_type,
2052 false, true, &state->server);
2054 if (tevent_req_nomem(subreq, req)) {
2055 return tevent_req_post(req, ev);
2059 if (state->num_sent + 1 < state->num_sent) {
2060 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2061 return tevent_req_post(req, ev);
2064 state->num_sent += 1;
2065 if (!tevent_req_set_endtime(subreq, state->ev,
2066 timeval_current_ofs(2, 0))) {
2067 return tevent_req_post(req, ev);
2069 tevent_req_set_callback(subreq, query_wins_list_done, req);
2073 static void query_wins_list_done(struct tevent_req *subreq)
2075 struct tevent_req *req = tevent_req_callback_data(
2076 subreq, struct tevent_req);
2077 struct query_wins_list_state *state = tevent_req_data(
2078 req, struct query_wins_list_state);
2081 status = name_query_recv(subreq, state,
2082 &state->addrs, &state->num_addrs,
2084 TALLOC_FREE(subreq);
2085 if (NT_STATUS_IS_OK(status)) {
2086 tevent_req_done(req);
2089 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
2090 tevent_req_nterror(req, status);
2093 wins_srv_died(state->servers[state->num_sent-1],
2094 my_socket_addr_v4());
2096 if (state->num_sent == state->num_servers) {
2097 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2101 in_addr_to_sockaddr_storage(
2102 &state->server, state->servers[state->num_sent]);
2104 subreq = name_query_send(state, state->ev,
2105 state->name, state->name_type,
2106 false, true, &state->server);
2107 state->num_sent += 1;
2108 if (tevent_req_nomem(subreq, req)) {
2111 if (!tevent_req_set_endtime(subreq, state->ev,
2112 timeval_current_ofs(2, 0))) {
2115 tevent_req_set_callback(subreq, query_wins_list_done, req);
2118 static NTSTATUS query_wins_list_recv(struct tevent_req *req,
2119 TALLOC_CTX *mem_ctx,
2120 struct sockaddr_storage **addrs,
2124 struct query_wins_list_state *state = tevent_req_data(
2125 req, struct query_wins_list_state);
2128 if (tevent_req_is_nterror(req, &status)) {
2131 if (addrs != NULL) {
2132 *addrs = talloc_move(mem_ctx, &state->addrs);
2134 if (num_addrs != NULL) {
2135 *num_addrs = state->num_addrs;
2137 if (flags != NULL) {
2138 *flags = state->flags;
2140 return NT_STATUS_OK;
2143 struct resolve_wins_state {
2145 size_t num_received;
2147 struct sockaddr_storage *addrs;
2152 static void resolve_wins_done(struct tevent_req *subreq);
2154 struct tevent_req *resolve_wins_send(TALLOC_CTX *mem_ctx,
2155 struct tevent_context *ev,
2159 struct tevent_req *req, *subreq;
2160 struct resolve_wins_state *state;
2161 char **wins_tags = NULL;
2162 struct sockaddr_storage src_ss;
2163 struct samba_sockaddr src_sa = {0};
2164 struct in_addr src_ip;
2165 size_t i, num_wins_tags;
2168 req = tevent_req_create(mem_ctx, &state,
2169 struct resolve_wins_state);
2174 if (wins_srv_count() < 1) {
2175 DEBUG(3,("resolve_wins: WINS server resolution selected "
2176 "and no WINS servers listed.\n"));
2177 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2181 /* the address we will be sending from */
2182 if (!interpret_string_addr(&src_ss, lp_nbt_client_socket_address(),
2183 AI_NUMERICHOST|AI_PASSIVE)) {
2184 zero_sockaddr(&src_ss);
2187 ok = sockaddr_storage_to_samba_sockaddr(&src_sa, &src_ss);
2189 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2193 if (src_sa.u.ss.ss_family != AF_INET) {
2194 char addr[INET6_ADDRSTRLEN];
2195 print_sockaddr(addr, sizeof(addr), &src_sa.u.ss);
2196 DEBUG(3,("resolve_wins: cannot receive WINS replies "
2197 "on IPv6 address %s\n",
2199 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2203 src_ip = src_sa.u.in.sin_addr;
2205 wins_tags = wins_srv_tags();
2206 if (wins_tags == NULL) {
2207 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2212 while (wins_tags[num_wins_tags] != NULL) {
2214 if (num_wins_tags + 1 < num_wins_tags) {
2215 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2221 for (i=0; i<num_wins_tags; i++) {
2222 size_t num_servers, num_alive;
2223 struct in_addr *servers, *alive;
2226 if (!wins_server_tag_ips(wins_tags[i], talloc_tos(),
2227 &servers, &num_servers)) {
2228 DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
2233 alive = talloc_array(state, struct in_addr, num_servers);
2234 if (tevent_req_nomem(alive, req)) {
2239 for (j=0; j<num_servers; j++) {
2240 struct in_addr wins_ip = servers[j];
2242 if (global_in_nmbd && ismyip_v4(wins_ip)) {
2243 /* yikes! we'll loop forever */
2246 /* skip any that have been unresponsive lately */
2247 if (wins_srv_is_dead(wins_ip, src_ip)) {
2250 DEBUG(3, ("resolve_wins: using WINS server %s "
2252 inet_ntoa(wins_ip), wins_tags[i]));
2253 alive[num_alive] = wins_ip;
2256 TALLOC_FREE(servers);
2258 if (num_alive == 0) {
2262 subreq = query_wins_list_send(
2263 state, ev, src_ip, name, name_type,
2265 if (tevent_req_nomem(subreq, req)) {
2268 tevent_req_set_callback(subreq, resolve_wins_done, req);
2269 state->num_sent += 1;
2272 if (state->num_sent == 0) {
2273 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2277 wins_srv_tags_free(wins_tags);
2280 wins_srv_tags_free(wins_tags);
2281 return tevent_req_post(req, ev);
2284 static void resolve_wins_done(struct tevent_req *subreq)
2286 struct tevent_req *req = tevent_req_callback_data(
2287 subreq, struct tevent_req);
2288 struct resolve_wins_state *state = tevent_req_data(
2289 req, struct resolve_wins_state);
2292 status = query_wins_list_recv(subreq, state, &state->addrs,
2293 &state->num_addrs, &state->flags);
2294 if (NT_STATUS_IS_OK(status)) {
2295 tevent_req_done(req);
2300 if (state->num_received + 1 < state->num_received) {
2301 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2305 state->num_received += 1;
2307 if (state->num_received < state->num_sent) {
2309 * Wait for the others
2313 tevent_req_nterror(req, status);
2316 NTSTATUS resolve_wins_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2317 struct sockaddr_storage **addrs,
2318 size_t *num_addrs, uint8_t *flags)
2320 struct resolve_wins_state *state = tevent_req_data(
2321 req, struct resolve_wins_state);
2324 if (tevent_req_is_nterror(req, &status)) {
2327 if (addrs != NULL) {
2328 *addrs = talloc_move(mem_ctx, &state->addrs);
2330 if (num_addrs != NULL) {
2331 *num_addrs = state->num_addrs;
2333 if (flags != NULL) {
2334 *flags = state->flags;
2336 return NT_STATUS_OK;
2339 /********************************************************
2340 Resolve via "wins" method.
2341 *********************************************************/
2343 NTSTATUS resolve_wins(TALLOC_CTX *mem_ctx,
2346 struct sockaddr_storage **return_iplist,
2347 size_t *return_count)
2349 struct tevent_context *ev;
2350 struct tevent_req *req;
2351 NTSTATUS status = NT_STATUS_NO_MEMORY;
2353 ev = samba_tevent_context_init(talloc_tos());
2357 req = resolve_wins_send(ev, ev, name, name_type);
2361 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2364 status = resolve_wins_recv(req, mem_ctx, return_iplist, return_count,
2372 /********************************************************
2373 Resolve via "hosts" method.
2374 *********************************************************/
2376 static NTSTATUS resolve_hosts(TALLOC_CTX *mem_ctx,
2379 struct sockaddr_storage **return_iplist,
2380 size_t *return_count)
2383 * "host" means do a localhost, or dns lookup.
2385 struct addrinfo hints;
2386 struct addrinfo *ailist = NULL;
2387 struct addrinfo *res = NULL;
2390 size_t ret_count = 0;
2391 struct sockaddr_storage *iplist = NULL;
2393 if ( name_type != 0x20 && name_type != 0x0) {
2394 DEBUG(5, ("resolve_hosts: not appropriate "
2395 "for name type <0x%x>\n",
2397 return NT_STATUS_INVALID_PARAMETER;
2400 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
2404 /* By default make sure it supports TCP. */
2405 hints.ai_socktype = SOCK_STREAM;
2406 hints.ai_flags = AI_ADDRCONFIG;
2408 #if !defined(HAVE_IPV6)
2409 /* Unless we have IPv6, we really only want IPv4 addresses back. */
2410 hints.ai_family = AF_INET;
2413 ret = getaddrinfo(name,
2418 DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
2420 gai_strerror(ret) ));
2423 for (res = ailist; res; res = res->ai_next) {
2424 struct sockaddr_storage ss = {0};
2425 struct sockaddr_storage *tmp = NULL;
2427 if ((res->ai_addr == NULL) ||
2428 (res->ai_addrlen == 0) ||
2429 (res->ai_addrlen > sizeof(ss))) {
2433 memcpy(&ss, res->ai_addr, res->ai_addrlen);
2435 if (is_zero_addr(&ss)) {
2440 if (ret_count + 1 < ret_count) {
2441 freeaddrinfo(ailist);
2442 TALLOC_FREE(iplist);
2443 return NT_STATUS_INVALID_PARAMETER;
2447 tmp = talloc_realloc(
2448 mem_ctx, iplist, struct sockaddr_storage,
2451 DEBUG(3,("resolve_hosts: malloc fail !\n"));
2452 freeaddrinfo(ailist);
2453 TALLOC_FREE(iplist);
2454 return NT_STATUS_NO_MEMORY;
2461 freeaddrinfo(ailist);
2463 if (ret_count == 0) {
2464 return NT_STATUS_UNSUCCESSFUL;
2466 *return_count = ret_count;
2467 *return_iplist = iplist;
2468 return NT_STATUS_OK;
2471 /********************************************************
2472 Resolve via "ADS" method.
2473 *********************************************************/
2475 /* Special name type used to cause a _kerberos DNS lookup. */
2476 #define KDC_NAME_TYPE 0xDCDC
2478 static NTSTATUS resolve_ads(TALLOC_CTX *ctx,
2481 const char *sitename,
2482 struct sockaddr_storage **return_addrs,
2483 size_t *return_count)
2487 struct dns_rr_srv *dcs = NULL;
2489 size_t num_srv_addrs = 0;
2490 struct sockaddr_storage *srv_addrs = NULL;
2493 if ((name_type != 0x1c) && (name_type != KDC_NAME_TYPE) &&
2494 (name_type != 0x1b)) {
2495 return NT_STATUS_INVALID_PARAMETER;
2498 status = NT_STATUS_OK;
2500 switch (name_type) {
2502 DEBUG(5,("resolve_ads: Attempting to resolve "
2503 "PDC for %s using DNS\n", name));
2504 query = ads_dns_query_string_pdc(ctx, name);
2508 DEBUG(5,("resolve_ads: Attempting to resolve "
2509 "DCs for %s using DNS\n", name));
2510 query = ads_dns_query_string_dcs(ctx, name);
2513 DEBUG(5,("resolve_ads: Attempting to resolve "
2514 "KDCs for %s using DNS\n", name));
2515 query = ads_dns_query_string_kdcs(ctx, name);
2518 status = NT_STATUS_INVALID_PARAMETER;
2522 if (!NT_STATUS_IS_OK(status)) {
2525 if (query == NULL) {
2526 return NT_STATUS_NO_MEMORY;
2529 DBG_DEBUG("SRV query for %s\n", query);
2531 status = ads_dns_query_srv(
2533 lp_get_async_dns_timeout(),
2538 if (!NT_STATUS_IS_OK(status)) {
2543 *return_addrs = NULL;
2546 return NT_STATUS_OK;
2549 /* First count the sizes of each array. */
2550 for(i = 0; i < numdcs; i++) {
2551 if (dcs[i].ss_s == NULL) {
2553 * Nothing received or timeout in A/AAAA reqs
2558 if (num_srv_addrs + dcs[i].num_ips < num_srv_addrs) {
2561 return NT_STATUS_INVALID_PARAMETER;
2563 /* Add in the number of addresses we got. */
2564 num_srv_addrs += dcs[i].num_ips;
2567 /* Allocate the list of IP addresses we already have. */
2568 srv_addrs = talloc_zero_array(ctx,
2569 struct sockaddr_storage,
2571 if (srv_addrs == NULL) {
2573 return NT_STATUS_NO_MEMORY;
2577 for(i = 0; i < numdcs; i++) {
2578 /* Copy all the IP addresses from the SRV response */
2580 for (j = 0; j < dcs[i].num_ips; j++) {
2581 char addr[INET6_ADDRSTRLEN];
2583 srv_addrs[num_srv_addrs] = dcs[i].ss_s[j];
2584 if (is_zero_addr(&srv_addrs[num_srv_addrs])) {
2588 DBG_DEBUG("SRV lookup %s got IP[%zu] %s\n",
2591 print_sockaddr(addr,
2593 &srv_addrs[num_srv_addrs]));
2601 *return_addrs = srv_addrs;
2602 *return_count = num_srv_addrs;
2603 return NT_STATUS_OK;
2606 static const char **filter_out_nbt_lookup(TALLOC_CTX *mem_ctx,
2607 const char **resolve_order)
2609 size_t i, len, result_idx;
2610 const char **result;
2613 while (resolve_order[len] != NULL) {
2617 result = talloc_array(mem_ctx, const char *, len+1);
2618 if (result == NULL) {
2624 for (i=0; i<len; i++) {
2625 const char *tok = resolve_order[i];
2627 if (strequal(tok, "lmhosts") || strequal(tok, "wins") ||
2628 strequal(tok, "bcast")) {
2631 result[result_idx++] = tok;
2633 result[result_idx] = NULL;
2638 /*******************************************************************
2639 Samba interface to resolve a name into an IP address.
2640 Use this function if the string is either an IP address, DNS
2641 or host name or NetBIOS name. This uses the name switch in the
2642 smb.conf to determine the order of name resolution.
2644 Added support for ip addr/port to support ADS ldap servers.
2645 the only place we currently care about the port is in the
2646 resolve_hosts() when looking up DC's via SRV RR entries in DNS
2647 **********************************************************************/
2649 NTSTATUS internal_resolve_name(TALLOC_CTX *ctx,
2652 const char *sitename,
2653 struct samba_sockaddr **return_salist,
2654 size_t *return_count,
2655 const char **resolve_order)
2657 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2659 size_t nc_count = 0;
2660 size_t ret_count = 0;
2662 struct sockaddr_storage *ss_list = NULL;
2663 struct samba_sockaddr *sa_list = NULL;
2664 TALLOC_CTX *frame = talloc_stackframe();
2666 DBG_DEBUG("looking up %s#%x (sitename %s)\n",
2667 name, name_type, sitename ? sitename : "(null)");
2669 if (is_ipaddress(name)) {
2670 struct sockaddr_storage ss;
2672 /* if it's in the form of an IP address then get the lib to interpret it */
2673 ok = interpret_string_addr(&ss, name, AI_NUMERICHOST);
2675 DBG_WARNING("interpret_string_addr failed on %s\n",
2678 return NT_STATUS_INVALID_PARAMETER;
2680 if (is_zero_addr(&ss)) {
2682 return NT_STATUS_UNSUCCESSFUL;
2685 status = sockaddr_array_to_samba_sockaddr_array(frame,
2690 if (!NT_STATUS_IS_OK(status)) {
2695 *return_salist = talloc_move(ctx, &sa_list);
2698 return NT_STATUS_OK;
2701 /* Check name cache */
2703 ok = namecache_fetch(frame,
2710 * remove_duplicate_addrs2() has the
2711 * side effect of removing zero addresses,
2714 nc_count = remove_duplicate_addrs2(sa_list, nc_count);
2715 if (nc_count == 0) {
2716 TALLOC_FREE(sa_list);
2718 return NT_STATUS_UNSUCCESSFUL;
2720 *return_count = nc_count;
2721 *return_salist = talloc_move(ctx, &sa_list);
2723 return NT_STATUS_OK;
2726 /* set the name resolution order */
2728 if (resolve_order && strcmp(resolve_order[0], "NULL") == 0) {
2729 DBG_DEBUG("all lookups disabled\n");
2731 return NT_STATUS_INVALID_PARAMETER;
2734 if (!resolve_order || !resolve_order[0]) {
2735 static const char *host_order[] = { "host", NULL };
2736 resolve_order = host_order;
2739 if ((strlen(name) > MAX_NETBIOSNAME_LEN - 1) ||
2740 (strchr(name, '.') != NULL)) {
2742 * Don't do NBT lookup, the name would not fit anyway
2744 resolve_order = filter_out_nbt_lookup(frame, resolve_order);
2745 if (resolve_order == NULL) {
2747 return NT_STATUS_NO_MEMORY;
2751 /* iterate through the name resolution backends */
2753 for (i=0; resolve_order[i]; i++) {
2754 const char *tok = resolve_order[i];
2756 if ((strequal(tok, "host") || strequal(tok, "hosts"))) {
2757 status = resolve_hosts(talloc_tos(),
2762 if (!NT_STATUS_IS_OK(status)) {
2768 if (strequal(tok, "kdc")) {
2769 /* deal with KDC_NAME_TYPE names here.
2770 * This will result in a SRV record lookup */
2771 status = resolve_ads(talloc_tos(),
2777 if (!NT_STATUS_IS_OK(status)) {
2780 /* Ensure we don't namecache
2781 * this with the KDC port. */
2782 name_type = KDC_NAME_TYPE;
2786 if (strequal(tok, "ads")) {
2787 /* deal with 0x1c and 0x1b names here.
2788 * This will result in a SRV record lookup */
2789 status = resolve_ads(talloc_tos(),
2795 if (!NT_STATUS_IS_OK(status)) {
2801 if (strequal(tok, "lmhosts")) {
2802 status = resolve_lmhosts_file_as_sockaddr(
2804 get_dyn_LMHOSTSFILE(),
2809 if (!NT_STATUS_IS_OK(status)) {
2815 if (strequal(tok, "wins")) {
2816 /* don't resolve 1D via WINS */
2817 if (name_type == 0x1D) {
2820 status = resolve_wins(talloc_tos(),
2825 if (!NT_STATUS_IS_OK(status)) {
2831 if (strequal(tok, "bcast")) {
2832 status = name_resolve_bcast(
2838 if (!NT_STATUS_IS_OK(status)) {
2844 DBG_ERR("unknown name switch type %s\n", tok);
2847 /* All of the resolve_* functions above have returned false. */
2856 status = sockaddr_array_to_samba_sockaddr_array(frame,
2861 if (!NT_STATUS_IS_OK(status)) {
2863 return NT_STATUS_NO_MEMORY;
2866 /* Remove duplicate entries. Some queries, notably #1c (domain
2867 controllers) return the PDC in iplist[0] and then all domain
2868 controllers including the PDC in iplist[1..n]. Iterating over
2869 the iplist when the PDC is down will cause two sets of timeouts. */
2871 ret_count = remove_duplicate_addrs2(sa_list, ret_count);
2873 /* Save in name cache */
2874 if ( DEBUGLEVEL >= 100 ) {
2875 for (i = 0; i < ret_count && DEBUGLEVEL == 100; i++) {
2876 char addr[INET6_ADDRSTRLEN];
2877 print_sockaddr(addr, sizeof(addr),
2879 DEBUG(100, ("Storing name %s of type %d (%s:0)\n",
2887 namecache_store(name,
2893 /* Display some debugging info */
2895 if ( DEBUGLEVEL >= 10 ) {
2896 DBG_DEBUG("returning %zu addresses: ",
2899 for (i = 0; i < ret_count; i++) {
2900 char addr[INET6_ADDRSTRLEN];
2901 print_sockaddr(addr, sizeof(addr),
2903 DEBUGADD(10, ("%s ", addr));
2908 *return_count = ret_count;
2909 *return_salist = talloc_move(ctx, &sa_list);
2915 /********************************************************
2916 Internal interface to resolve a name into one IP address.
2917 Use this function if the string is either an IP address, DNS
2918 or host name or NetBIOS name. This uses the name switch in the
2919 smb.conf to determine the order of name resolution.
2920 *********************************************************/
2922 bool resolve_name(const char *name,
2923 struct sockaddr_storage *return_ss,
2927 struct samba_sockaddr *sa_list = NULL;
2928 char *sitename = NULL;
2931 TALLOC_CTX *frame = NULL;
2933 if (is_ipaddress(name)) {
2934 return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
2937 frame = talloc_stackframe();
2939 sitename = sitename_fetch(frame, lp_realm()); /* wild guess */
2941 status = internal_resolve_name(frame,
2947 lp_name_resolve_order());
2948 if (NT_STATUS_IS_OK(status)) {
2952 for (i=0; i<count; i++) {
2953 if (!is_broadcast_addr(&sa_list[i].u.sa) &&
2954 (sa_list[i].u.ss.ss_family == AF_INET)) {
2955 *return_ss = sa_list[i].u.ss;
2956 TALLOC_FREE(sa_list);
2963 /* only return valid addresses for TCP connections */
2964 for (i=0; i<count; i++) {
2965 if (!is_broadcast_addr(&sa_list[i].u.sa)) {
2966 *return_ss = sa_list[i].u.ss;
2967 TALLOC_FREE(sa_list);
2974 TALLOC_FREE(sa_list);
2979 /********************************************************
2980 Internal interface to resolve a name into a list of IP addresses.
2981 Use this function if the string is either an IP address, DNS
2982 or host name or NetBIOS name. This uses the name switch in the
2983 smb.conf to determine the order of name resolution.
2984 *********************************************************/
2986 NTSTATUS resolve_name_list(TALLOC_CTX *ctx,
2989 struct sockaddr_storage **return_ss_arr,
2990 unsigned int *p_num_entries)
2992 struct samba_sockaddr *sa_list = NULL;
2993 char *sitename = NULL;
2996 unsigned int num_entries = 0;
2997 struct sockaddr_storage *result_arr = NULL;
3000 if (is_ipaddress(name)) {
3001 result_arr = talloc(ctx, struct sockaddr_storage);
3002 if (result_arr == NULL) {
3003 return NT_STATUS_NO_MEMORY;
3005 if (!interpret_string_addr(result_arr, name, AI_NUMERICHOST)) {
3006 TALLOC_FREE(result_arr);
3007 return NT_STATUS_BAD_NETWORK_NAME;
3010 *return_ss_arr = result_arr;
3011 return NT_STATUS_OK;
3014 sitename = sitename_fetch(ctx, lp_realm()); /* wild guess */
3016 status = internal_resolve_name(ctx,
3022 lp_name_resolve_order());
3023 TALLOC_FREE(sitename);
3025 if (!NT_STATUS_IS_OK(status)) {
3029 /* only return valid addresses for TCP connections */
3030 for (i=0, num_entries = 0; i<count; i++) {
3031 if (!is_zero_addr(&sa_list[i].u.ss) &&
3032 !is_broadcast_addr(&sa_list[i].u.sa)) {
3036 if (num_entries == 0) {
3037 status = NT_STATUS_BAD_NETWORK_NAME;
3041 result_arr = talloc_array(ctx,
3042 struct sockaddr_storage,
3044 if (result_arr == NULL) {
3045 status = NT_STATUS_NO_MEMORY;
3049 for (i=0, num_entries = 0; i<count; i++) {
3050 if (!is_zero_addr(&sa_list[i].u.ss) &&
3051 !is_broadcast_addr(&sa_list[i].u.sa)) {
3052 result_arr[num_entries++] = sa_list[i].u.ss;
3056 if (num_entries == 0) {
3057 TALLOC_FREE(result_arr);
3058 status = NT_STATUS_BAD_NETWORK_NAME;
3062 status = NT_STATUS_OK;
3063 *p_num_entries = num_entries;
3064 *return_ss_arr = result_arr;
3066 TALLOC_FREE(sa_list);
3070 /********************************************************
3071 Find the IP address of the master browser or DMB for a workgroup.
3072 *********************************************************/
3074 bool find_master_ip(const char *group, struct sockaddr_storage *master_ss)
3076 struct samba_sockaddr *sa_list = NULL;
3080 if (lp_disable_netbios()) {
3081 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
3085 status = internal_resolve_name(talloc_tos(),
3091 lp_name_resolve_order());
3092 if (NT_STATUS_IS_OK(status)) {
3093 *master_ss = sa_list[0].u.ss;
3094 TALLOC_FREE(sa_list);
3098 TALLOC_FREE(sa_list);
3100 status = internal_resolve_name(talloc_tos(),
3106 lp_name_resolve_order());
3107 if (NT_STATUS_IS_OK(status)) {
3108 *master_ss = sa_list[0].u.ss;
3109 TALLOC_FREE(sa_list);
3113 TALLOC_FREE(sa_list);
3117 /********************************************************
3118 Get the IP address list of the primary domain controller
3120 *********************************************************/
3122 bool get_pdc_ip(const char *domain, struct sockaddr_storage *pss)
3124 struct samba_sockaddr *sa_list = NULL;
3126 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
3127 static const char *ads_order[] = { "ads", NULL };
3128 /* Look up #1B name */
3130 if (lp_security() == SEC_ADS) {
3131 status = internal_resolve_name(talloc_tos(),
3140 if (!NT_STATUS_IS_OK(status) || count == 0) {
3141 TALLOC_FREE(sa_list);
3142 status = internal_resolve_name(talloc_tos(),
3148 lp_name_resolve_order());
3149 if (!NT_STATUS_IS_OK(status)) {
3150 TALLOC_FREE(sa_list);
3155 /* if we get more than 1 IP back we have to assume it is a
3156 multi-homed PDC and not a mess up */
3159 DBG_INFO("PDC has %zu IP addresses!\n", count);
3160 sort_sa_list(sa_list, count);
3163 *pss = sa_list[0].u.ss;
3164 TALLOC_FREE(sa_list);
3168 /* Private enum type for lookups. */
3170 enum dc_lookup_type { DC_NORMAL_LOOKUP, DC_ADS_ONLY, DC_KDC_ONLY };
3172 /********************************************************
3173 Get the IP address list of the domain controllers for
3175 *********************************************************/
3177 static NTSTATUS get_dc_list(TALLOC_CTX *ctx,
3179 const char *sitename,
3180 struct samba_sockaddr **sa_list_ret,
3182 enum dc_lookup_type lookup_type,
3185 const char **resolve_order = NULL;
3186 char *saf_servername = NULL;
3187 char *pserver = NULL;
3190 size_t num_addresses = 0;
3191 size_t local_count = 0;
3193 struct samba_sockaddr *auto_sa_list = NULL;
3194 struct samba_sockaddr *return_salist = NULL;
3195 bool done_auto_lookup = false;
3196 size_t auto_count = 0;
3198 TALLOC_CTX *frame = talloc_stackframe();
3199 int auto_name_type = 0x1C;
3203 /* if we are restricted to solely using DNS for looking
3204 up a domain controller, make sure that host lookups
3205 are enabled for the 'name resolve order'. If host lookups
3206 are disabled and ads_only is True, then set the string to
3209 resolve_order = lp_name_resolve_order();
3210 if (!resolve_order) {
3211 status = NT_STATUS_NO_MEMORY;
3214 if (lookup_type == DC_ADS_ONLY) {
3215 if (str_list_check_ci(resolve_order, "host")) {
3216 static const char *ads_order[] = { "ads", NULL };
3217 resolve_order = ads_order;
3219 /* DNS SRV lookups used by the ads resolver
3220 are already sorted by priority and weight */
3223 /* this is quite bizarre! */
3224 static const char *null_order[] = { "NULL", NULL };
3225 resolve_order = null_order;
3227 } else if (lookup_type == DC_KDC_ONLY) {
3228 static const char *kdc_order[] = { "kdc", NULL };
3229 /* DNS SRV lookups used by the ads/kdc resolver
3230 are already sorted by priority and weight */
3232 resolve_order = kdc_order;
3233 auto_name_type = KDC_NAME_TYPE;
3236 /* fetch the server we have affinity for. Add the
3237 'password server' list to a search for our domain controllers */
3239 saf_servername = saf_fetch(frame, domain);
3241 if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
3242 pserver = talloc_asprintf(frame, "%s, %s",
3243 saf_servername ? saf_servername : "",
3244 lp_password_server());
3246 pserver = talloc_asprintf(frame, "%s, *",
3247 saf_servername ? saf_servername : "");
3250 TALLOC_FREE(saf_servername);
3252 status = NT_STATUS_NO_MEMORY;
3256 DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver ));
3259 * if '*' appears in the "password server" list then add
3260 * an auto lookup to the list of manually configured
3261 * DC's. If any DC is listed by name, then the list should be
3262 * considered to be ordered
3266 while (next_token_talloc(frame, &p, &name, LIST_SEP)) {
3267 if (!done_auto_lookup && strequal(name, "*")) {
3268 done_auto_lookup = true;
3270 status = internal_resolve_name(frame,
3277 if (!NT_STATUS_IS_OK(status)) {
3281 if (num_addresses + auto_count < num_addresses) {
3282 TALLOC_FREE(auto_sa_list);
3283 status = NT_STATUS_INVALID_PARAMETER;
3286 num_addresses += auto_count;
3287 DBG_DEBUG("Adding %zu DC's from auto lookup\n",
3291 if (num_addresses + 1 < num_addresses) {
3292 TALLOC_FREE(auto_sa_list);
3293 status = NT_STATUS_INVALID_PARAMETER;
3300 /* if we have no addresses and haven't done the auto lookup, then
3301 just return the list of DC's. Or maybe we just failed. */
3303 if (num_addresses == 0) {
3304 struct samba_sockaddr *dc_salist = NULL;
3305 size_t dc_count = 0;
3307 if (done_auto_lookup) {
3308 DEBUG(4,("get_dc_list: no servers found\n"));
3309 status = NT_STATUS_NO_LOGON_SERVERS;
3312 /* talloc off frame, only move to ctx on success. */
3313 status = internal_resolve_name(frame,
3320 if (!NT_STATUS_IS_OK(status)) {
3323 return_salist = dc_salist;
3324 local_count = dc_count;
3328 return_salist = talloc_zero_array(frame,
3329 struct samba_sockaddr,
3331 if (return_salist == NULL) {
3332 DEBUG(3,("get_dc_list: malloc fail !\n"));
3333 status = NT_STATUS_NO_MEMORY;
3340 /* fill in the return list now with real IP's */
3342 while ((local_count<num_addresses) &&
3343 next_token_talloc(frame, &p, &name, LIST_SEP)) {
3344 struct samba_sockaddr name_sa = {0};
3346 /* copy any addresses from the auto lookup */
3348 if (strequal(name, "*")) {
3350 for (j=0; j<auto_count; j++) {
3351 char addr[INET6_ADDRSTRLEN];
3352 print_sockaddr(addr,
3354 &auto_sa_list[j].u.ss);
3355 /* Check for and don't copy any
3356 * known bad DC IP's. */
3357 if(!NT_STATUS_IS_OK(check_negative_conn_cache(
3360 DEBUG(5,("get_dc_list: "
3361 "negative entry %s removed "
3366 return_salist[local_count] = auto_sa_list[j];
3372 /* explicit lookup; resolve_name() will
3373 * handle names & IP addresses */
3374 if (resolve_name(name, &name_sa.u.ss, 0x20, true)) {
3375 char addr[INET6_ADDRSTRLEN];
3379 * Ensure we set sa_socklen correctly.
3380 * Doesn't matter now, but eventually we
3381 * will remove ip_service and return samba_sockaddr
3384 ok = sockaddr_storage_to_samba_sockaddr(
3388 status = NT_STATUS_INVALID_ADDRESS;
3392 print_sockaddr(addr,
3396 /* Check for and don't copy any known bad DC IP's. */
3397 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain,
3399 DEBUG(5,("get_dc_list: negative entry %s "
3400 "removed from DC list\n",
3405 return_salist[local_count] = name_sa;
3411 /* need to remove duplicates in the list if we have any
3412 explicit password servers */
3414 local_count = remove_duplicate_addrs2(return_salist, local_count );
3416 /* For DC's we always prioritize IPv4 due to W2K3 not
3417 * supporting LDAP, KRB5 or CLDAP over IPv6. */
3419 if (local_count && return_salist != NULL) {
3420 prioritize_ipv4_list(return_salist, local_count);
3423 if ( DEBUGLEVEL >= 4 ) {
3424 DEBUG(4,("get_dc_list: returning %zu ip addresses "
3425 "in an %sordered list\n",
3427 *ordered ? "":"un"));
3428 DEBUG(4,("get_dc_list: "));
3429 for ( i=0; i<local_count; i++ ) {
3430 char addr[INET6_ADDRSTRLEN];
3431 print_sockaddr(addr,
3433 &return_salist[i].u.ss);
3434 DEBUGADD(4,("%s ", addr));
3439 status = (local_count != 0 ? NT_STATUS_OK : NT_STATUS_NO_LOGON_SERVERS);
3443 if (NT_STATUS_IS_OK(status)) {
3444 *sa_list_ret = talloc_move(ctx, &return_salist);
3445 *ret_count = local_count;
3447 TALLOC_FREE(return_salist);
3448 TALLOC_FREE(auto_sa_list);
3453 /*********************************************************************
3454 Small wrapper function to get the DC list and sort it if necessary.
3455 Returns a samba_sockaddr array.
3456 *********************************************************************/
3458 NTSTATUS get_sorted_dc_list(TALLOC_CTX *ctx,
3460 const char *sitename,
3461 struct samba_sockaddr **sa_list_ret,
3465 bool ordered = false;
3467 enum dc_lookup_type lookup_type = DC_NORMAL_LOOKUP;
3468 struct samba_sockaddr *sa_list = NULL;
3471 DBG_INFO("attempting lookup for name %s (sitename %s)\n",
3473 sitename ? sitename : "NULL");
3476 lookup_type = DC_ADS_ONLY;
3479 status = get_dc_list(ctx,
3486 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_LOGON_SERVERS)
3488 DBG_WARNING("No server for domain '%s' available"
3489 " in site '%s', fallback to all servers\n",
3492 status = get_dc_list(ctx,
3501 if (!NT_STATUS_IS_OK(status)) {
3505 /* only sort if we don't already have an ordered list */
3507 sort_sa_list(sa_list, count);
3511 *sa_list_ret = sa_list;
3515 /*********************************************************************
3516 Get the KDC list - re-use all the logic in get_dc_list.
3517 Returns a samba_sockaddr array.
3518 *********************************************************************/
3520 NTSTATUS get_kdc_list(TALLOC_CTX *ctx,
3522 const char *sitename,
3523 struct samba_sockaddr **sa_list_ret,
3527 struct samba_sockaddr *sa_list = NULL;
3528 bool ordered = false;
3531 status = get_dc_list(ctx,
3539 if (!NT_STATUS_IS_OK(status)) {
3543 /* only sort if we don't already have an ordered list */
3545 sort_sa_list(sa_list, count);
3549 *sa_list_ret = sa_list;