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 "../libcli/netlogon/netlogon.h"
27 #include "lib/async_req/async_sock.h"
28 #include "lib/tsocket/tsocket.h"
29 #include "libsmb/nmblib.h"
30 #include "libsmb/unexpected.h"
31 #include "../libcli/nbt/libnbt.h"
32 #include "libads/kerberos_proto.h"
33 #include "lib/gencache.h"
34 #include "librpc/gen_ndr/dns.h"
35 #include "lib/util/util_net.h"
36 #include "lib/util/string_wrappers.h"
38 /* nmbd.c sets this to True. */
39 bool global_in_nmbd = False;
42 * Utility function that copes only with AF_INET and AF_INET6
43 * as that's all we're going to get out of DNS / NetBIOS / WINS
44 * name resolution functions.
47 bool sockaddr_storage_to_samba_sockaddr(struct samba_sockaddr *sa,
48 const struct sockaddr_storage *ss)
52 switch (ss->ss_family) {
54 sa->sa_socklen = sizeof(struct sockaddr_in);
58 sa->sa_socklen = sizeof(struct sockaddr_in6);
68 * Utility function to convert from a struct ip_service
69 * array to a struct samba_sockaddr array. Will go away
70 * once ip_service is gone.
73 static NTSTATUS ip_service_to_samba_sockaddr(TALLOC_CTX *ctx,
74 struct samba_sockaddr **sa_out,
75 const struct ip_service *iplist_in,
78 struct samba_sockaddr *sa = NULL;
84 * Zero length arrays are returned as NULL.
85 * in the name resolution code.
90 sa = talloc_zero_array(ctx,
91 struct samba_sockaddr,
94 return NT_STATUS_NO_MEMORY;
96 for (i = 0; i < count; i++) {
97 ok = sockaddr_storage_to_samba_sockaddr(&sa[i],
101 return NT_STATUS_INVALID_PARAMETER;
108 /****************************
109 * SERVER AFFINITY ROUTINES *
110 ****************************/
112 /* Server affinity is the concept of preferring the last domain
113 controller with whom you had a successful conversation */
115 /****************************************************************************
116 ****************************************************************************/
117 #define SAFKEY_FMT "SAF/DOMAIN/%s"
119 #define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
120 #define SAFJOIN_TTL 3600
122 static char *saf_key(TALLOC_CTX *mem_ctx, const char *domain)
124 return talloc_asprintf_strupper_m(mem_ctx, SAFKEY_FMT, domain);
127 static char *saf_join_key(TALLOC_CTX *mem_ctx, const char *domain)
129 return talloc_asprintf_strupper_m(mem_ctx, SAFJOINKEY_FMT, domain);
132 /****************************************************************************
133 ****************************************************************************/
135 bool saf_store( const char *domain, const char *servername )
141 if ( !domain || !servername ) {
142 DEBUG(2,("saf_store: "
143 "Refusing to store empty domain or servername!\n"));
147 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
148 DEBUG(0,("saf_store: "
149 "refusing to store 0 length domain or servername!\n"));
153 key = saf_key(talloc_tos(), domain);
155 DEBUG(1, ("saf_key() failed\n"));
158 expire = time( NULL ) + lp_parm_int(-1, "saf","ttl", SAF_TTL);
160 DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
161 domain, servername, (unsigned int)expire ));
163 ret = gencache_set( key, servername, expire );
170 bool saf_join_store( const char *domain, const char *servername )
176 if ( !domain || !servername ) {
177 DEBUG(2,("saf_join_store: Refusing to store empty domain or servername!\n"));
181 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
182 DEBUG(0,("saf_join_store: refusing to store 0 length domain or servername!\n"));
186 key = saf_join_key(talloc_tos(), domain);
188 DEBUG(1, ("saf_join_key() failed\n"));
191 expire = time( NULL ) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL);
193 DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
194 domain, servername, (unsigned int)expire ));
196 ret = gencache_set( key, servername, expire );
203 bool saf_delete( const char *domain )
209 DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
213 key = saf_join_key(talloc_tos(), domain);
215 DEBUG(1, ("saf_join_key() failed\n"));
218 ret = gencache_del(key);
222 DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain ));
225 key = saf_key(talloc_tos(), domain);
227 DEBUG(1, ("saf_key() failed\n"));
230 ret = gencache_del(key);
234 DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
240 /****************************************************************************
241 ****************************************************************************/
243 char *saf_fetch(TALLOC_CTX *mem_ctx, const char *domain )
250 if ( !domain || strlen(domain) == 0) {
251 DEBUG(2,("saf_fetch: Empty domain name!\n"));
255 key = saf_join_key(talloc_tos(), domain);
257 DEBUG(1, ("saf_join_key() failed\n"));
261 ret = gencache_get( key, mem_ctx, &server, &timeout );
266 DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
271 key = saf_key(talloc_tos(), domain);
273 DEBUG(1, ("saf_key() failed\n"));
277 ret = gencache_get( key, mem_ctx, &server, &timeout );
282 DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
285 DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
292 static void set_socket_addr_v4(struct samba_sockaddr *addr)
294 if (!interpret_string_addr(&addr->u.ss, lp_nbt_client_socket_address(),
295 AI_NUMERICHOST|AI_PASSIVE)) {
296 zero_sockaddr(&addr->u.ss);
297 /* zero_sockaddr sets family to AF_INET. */
298 addr->sa_socklen = sizeof(struct sockaddr_in);
300 if (addr->u.ss.ss_family != AF_INET) {
301 zero_sockaddr(&addr->u.ss);
302 /* zero_sockaddr sets family to AF_INET. */
303 addr->sa_socklen = sizeof(struct sockaddr_in);
307 static struct in_addr my_socket_addr_v4(void)
309 struct samba_sockaddr my_addr = {0};
311 set_socket_addr_v4(&my_addr);
312 return my_addr.u.in.sin_addr;
315 /****************************************************************************
316 Generate a random trn_id.
317 ****************************************************************************/
319 static int generate_trn_id(void)
323 generate_random_buffer((uint8_t *)&id, sizeof(id));
325 return id % (unsigned)0x7FFF;
328 /****************************************************************************
329 Parse a node status response into an array of structures.
330 ****************************************************************************/
332 static struct node_status *parse_node_status(TALLOC_CTX *mem_ctx, char *p,
334 struct node_status_extra *extra)
336 struct node_status *ret;
338 size_t result_count = 0;
340 result_count = CVAL(p,0);
342 if (result_count == 0)
345 ret = talloc_array(mem_ctx, struct node_status,result_count);
350 for (i=0;i< result_count;i++) {
351 strlcpy(ret[i].name,p,16);
352 trim_char(ret[i].name,'\0',' ');
353 ret[i].type = CVAL(p,15);
354 ret[i].flags = p[16];
356 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
357 ret[i].type, ret[i].flags));
360 * Also, pick up the MAC address ...
363 memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
365 *num_names = result_count;
369 struct sock_packet_read_state {
370 struct tevent_context *ev;
371 enum packet_type type;
374 struct nb_packet_reader *reader;
375 struct tevent_req *reader_req;
377 struct tdgram_context *sock;
378 struct tevent_req *socket_req;
380 struct tsocket_address *addr;
382 bool (*validator)(struct packet_struct *p,
386 struct packet_struct *packet;
389 static void sock_packet_read_got_packet(struct tevent_req *subreq);
390 static void sock_packet_read_got_socket(struct tevent_req *subreq);
392 static struct tevent_req *sock_packet_read_send(
394 struct tevent_context *ev,
395 struct tdgram_context *sock,
396 struct nb_packet_reader *reader,
397 enum packet_type type,
399 bool (*validator)(struct packet_struct *p, void *private_data),
402 struct tevent_req *req;
403 struct sock_packet_read_state *state;
405 req = tevent_req_create(mem_ctx, &state,
406 struct sock_packet_read_state);
411 state->reader = reader;
414 state->trn_id = trn_id;
415 state->validator = validator;
416 state->private_data = private_data;
418 if (reader != NULL) {
419 state->reader_req = nb_packet_read_send(state, ev, reader);
420 if (tevent_req_nomem(state->reader_req, req)) {
421 return tevent_req_post(req, ev);
423 tevent_req_set_callback(
424 state->reader_req, sock_packet_read_got_packet, req);
427 state->socket_req = tdgram_recvfrom_send(state, ev, state->sock);
428 if (tevent_req_nomem(state->socket_req, req)) {
429 return tevent_req_post(req, ev);
431 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
437 static void sock_packet_read_got_packet(struct tevent_req *subreq)
439 struct tevent_req *req = tevent_req_callback_data(
440 subreq, struct tevent_req);
441 struct sock_packet_read_state *state = tevent_req_data(
442 req, struct sock_packet_read_state);
445 status = nb_packet_read_recv(subreq, state, &state->packet);
447 TALLOC_FREE(state->reader_req);
449 if (!NT_STATUS_IS_OK(status)) {
450 if (state->socket_req != NULL) {
452 * Still waiting for socket
457 * Both socket and packet reader failed
459 tevent_req_nterror(req, status);
463 if ((state->validator != NULL) &&
464 !state->validator(state->packet, state->private_data)) {
465 DEBUG(10, ("validator failed\n"));
467 TALLOC_FREE(state->packet);
469 state->reader_req = nb_packet_read_send(state, state->ev,
471 if (tevent_req_nomem(state->reader_req, req)) {
474 tevent_req_set_callback(
475 state->reader_req, sock_packet_read_got_packet, req);
479 TALLOC_FREE(state->socket_req);
480 tevent_req_done(req);
483 static void sock_packet_read_got_socket(struct tevent_req *subreq)
485 struct tevent_req *req = tevent_req_callback_data(
486 subreq, struct tevent_req);
487 struct sock_packet_read_state *state = tevent_req_data(
488 req, struct sock_packet_read_state);
489 struct samba_sockaddr addr = {0};
495 received = tdgram_recvfrom_recv(subreq, &err, state,
496 &state->buf, &state->addr);
498 TALLOC_FREE(state->socket_req);
500 if (received == -1) {
501 if (state->reader_req != NULL) {
503 * Still waiting for reader
508 * Both socket and reader failed
510 tevent_req_nterror(req, map_nt_error_from_unix(err));
513 ok = tsocket_address_is_inet(state->addr, "ipv4");
517 ret = tsocket_address_bsd_sockaddr(state->addr,
521 tevent_req_nterror(req, map_nt_error_from_unix(errno));
525 state->packet = parse_packet_talloc(
526 state, (char *)state->buf, received, state->type,
527 addr.u.in.sin_addr, addr.u.in.sin_port);
528 if (state->packet == NULL) {
529 DEBUG(10, ("parse_packet failed\n"));
532 if ((state->trn_id != -1) &&
533 (state->trn_id != packet_trn_id(state->packet))) {
534 DEBUG(10, ("Expected transaction id %d, got %d\n",
535 state->trn_id, packet_trn_id(state->packet)));
539 if ((state->validator != NULL) &&
540 !state->validator(state->packet, state->private_data)) {
541 DEBUG(10, ("validator failed\n"));
545 tevent_req_done(req);
549 TALLOC_FREE(state->packet);
550 TALLOC_FREE(state->buf);
551 TALLOC_FREE(state->addr);
553 state->socket_req = tdgram_recvfrom_send(state, state->ev, state->sock);
554 if (tevent_req_nomem(state->socket_req, req)) {
557 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
561 static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
563 struct packet_struct **ppacket)
565 struct sock_packet_read_state *state = tevent_req_data(
566 req, struct sock_packet_read_state);
569 if (tevent_req_is_nterror(req, &status)) {
572 *ppacket = talloc_move(mem_ctx, &state->packet);
576 struct nb_trans_state {
577 struct tevent_context *ev;
578 struct tdgram_context *sock;
579 struct nb_packet_reader *reader;
581 struct tsocket_address *src_addr;
582 struct tsocket_address *dst_addr;
585 enum packet_type type;
588 bool (*validator)(struct packet_struct *p,
592 struct packet_struct *packet;
595 static void nb_trans_got_reader(struct tevent_req *subreq);
596 static void nb_trans_done(struct tevent_req *subreq);
597 static void nb_trans_sent(struct tevent_req *subreq);
598 static void nb_trans_send_next(struct tevent_req *subreq);
600 static struct tevent_req *nb_trans_send(
602 struct tevent_context *ev,
603 const struct samba_sockaddr *_my_addr,
604 const struct samba_sockaddr *_dst_addr,
606 uint8_t *buf, size_t buflen,
607 enum packet_type type, int trn_id,
608 bool (*validator)(struct packet_struct *p,
612 const struct sockaddr *my_addr = &_my_addr->u.sa;
613 size_t my_addr_len = sizeof(_my_addr->u.in); /*We know it's AF_INET.*/
614 const struct sockaddr *dst_addr = &_dst_addr->u.sa;
615 size_t dst_addr_len = sizeof(_dst_addr->u.in); /*We know it's AF_INET.*/
616 struct tevent_req *req, *subreq;
617 struct nb_trans_state *state;
620 req = tevent_req_create(mem_ctx, &state, struct nb_trans_state);
626 state->buflen = buflen;
628 state->trn_id = trn_id;
629 state->validator = validator;
630 state->private_data = private_data;
632 ret = tsocket_address_bsd_from_sockaddr(state,
633 my_addr, my_addr_len,
636 tevent_req_nterror(req, map_nt_error_from_unix(errno));
637 return tevent_req_post(req, ev);
640 ret = tsocket_address_bsd_from_sockaddr(state,
641 dst_addr, dst_addr_len,
644 tevent_req_nterror(req, map_nt_error_from_unix(errno));
645 return tevent_req_post(req, ev);
648 ret = tdgram_inet_udp_broadcast_socket(state->src_addr, state,
651 tevent_req_nterror(req, map_nt_error_from_unix(errno));
652 return tevent_req_post(req, ev);
655 subreq = nb_packet_reader_send(state, ev, type, state->trn_id, NULL);
656 if (tevent_req_nomem(subreq, req)) {
657 return tevent_req_post(req, ev);
659 tevent_req_set_callback(subreq, nb_trans_got_reader, req);
663 static void nb_trans_got_reader(struct tevent_req *subreq)
665 struct tevent_req *req = tevent_req_callback_data(
666 subreq, struct tevent_req);
667 struct nb_trans_state *state = tevent_req_data(
668 req, struct nb_trans_state);
671 status = nb_packet_reader_recv(subreq, state, &state->reader);
674 if (!NT_STATUS_IS_OK(status)) {
675 DEBUG(10, ("nmbd not around\n"));
676 state->reader = NULL;
679 subreq = sock_packet_read_send(
680 state, state->ev, state->sock,
681 state->reader, state->type, state->trn_id,
682 state->validator, state->private_data);
683 if (tevent_req_nomem(subreq, req)) {
686 tevent_req_set_callback(subreq, nb_trans_done, req);
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_sent(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);
707 sent = tdgram_sendto_recv(subreq, &err);
710 DEBUG(10, ("sendto failed: %s\n", strerror(err)));
711 tevent_req_nterror(req, map_nt_error_from_unix(err));
714 subreq = tevent_wakeup_send(state, state->ev,
715 timeval_current_ofs(1, 0));
716 if (tevent_req_nomem(subreq, req)) {
719 tevent_req_set_callback(subreq, nb_trans_send_next, req);
722 static void nb_trans_send_next(struct tevent_req *subreq)
724 struct tevent_req *req = tevent_req_callback_data(
725 subreq, struct tevent_req);
726 struct nb_trans_state *state = tevent_req_data(
727 req, struct nb_trans_state);
730 ret = tevent_wakeup_recv(subreq);
733 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
736 subreq = tdgram_sendto_send(state, state->ev,
738 state->buf, state->buflen,
740 if (tevent_req_nomem(subreq, req)) {
743 tevent_req_set_callback(subreq, nb_trans_sent, req);
746 static void nb_trans_done(struct tevent_req *subreq)
748 struct tevent_req *req = tevent_req_callback_data(
749 subreq, struct tevent_req);
750 struct nb_trans_state *state = tevent_req_data(
751 req, struct nb_trans_state);
754 status = sock_packet_read_recv(subreq, state, &state->packet);
756 if (tevent_req_nterror(req, status)) {
759 tevent_req_done(req);
762 static NTSTATUS nb_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
763 struct packet_struct **ppacket)
765 struct nb_trans_state *state = tevent_req_data(
766 req, struct nb_trans_state);
769 if (tevent_req_is_nterror(req, &status)) {
772 *ppacket = talloc_move(mem_ctx, &state->packet);
776 /****************************************************************************
777 Do a NBT node status query on an open socket and return an array of
778 structures holding the returned names or NULL if the query failed.
779 **************************************************************************/
781 struct node_status_query_state {
782 struct samba_sockaddr my_addr;
783 struct samba_sockaddr addr;
786 struct packet_struct *packet;
789 static bool node_status_query_validator(struct packet_struct *p,
791 static void node_status_query_done(struct tevent_req *subreq);
793 struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
794 struct tevent_context *ev,
795 struct nmb_name *name,
796 const struct sockaddr_storage *addr)
798 struct tevent_req *req, *subreq;
799 struct node_status_query_state *state;
800 struct packet_struct p;
801 struct nmb_packet *nmb = &p.packet.nmb;
804 req = tevent_req_create(mem_ctx, &state,
805 struct node_status_query_state);
810 if (addr->ss_family != AF_INET) {
811 /* Can't do node status to IPv6 */
812 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
813 return tevent_req_post(req, ev);
816 ok = sockaddr_storage_to_samba_sockaddr(&state->addr, addr);
818 /* node status must be IPv4 */
819 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
820 return tevent_req_post(req, ev);
822 state->addr.u.in.sin_port = htons(NMB_PORT);
824 set_socket_addr_v4(&state->my_addr);
827 nmb->header.name_trn_id = generate_trn_id();
828 nmb->header.opcode = 0;
829 nmb->header.response = false;
830 nmb->header.nm_flags.bcast = false;
831 nmb->header.nm_flags.recursion_available = false;
832 nmb->header.nm_flags.recursion_desired = false;
833 nmb->header.nm_flags.trunc = false;
834 nmb->header.nm_flags.authoritative = false;
835 nmb->header.rcode = 0;
836 nmb->header.qdcount = 1;
837 nmb->header.ancount = 0;
838 nmb->header.nscount = 0;
839 nmb->header.arcount = 0;
840 nmb->question.question_name = *name;
841 nmb->question.question_type = 0x21;
842 nmb->question.question_class = 0x1;
844 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
846 if (state->buflen == 0) {
847 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
848 DEBUG(10, ("build_packet failed\n"));
849 return tevent_req_post(req, ev);
852 subreq = nb_trans_send(state,
860 nmb->header.name_trn_id,
861 node_status_query_validator,
863 if (tevent_req_nomem(subreq, req)) {
864 DEBUG(10, ("nb_trans_send failed\n"));
865 return tevent_req_post(req, ev);
867 if (!tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0))) {
868 return tevent_req_post(req, ev);
870 tevent_req_set_callback(subreq, node_status_query_done, req);
874 static bool node_status_query_validator(struct packet_struct *p,
877 struct nmb_packet *nmb = &p->packet.nmb;
880 if (nmb->header.opcode != 0 ||
881 nmb->header.nm_flags.bcast ||
883 !nmb->header.ancount ||
884 nmb->answers->rr_type != 0x21) {
886 * XXXX what do we do with this? could be a redirect,
887 * but we'll discard it for the moment
894 static void node_status_query_done(struct tevent_req *subreq)
896 struct tevent_req *req = tevent_req_callback_data(
897 subreq, struct tevent_req);
898 struct node_status_query_state *state = tevent_req_data(
899 req, struct node_status_query_state);
902 status = nb_trans_recv(subreq, state, &state->packet);
904 if (tevent_req_nterror(req, status)) {
907 tevent_req_done(req);
910 NTSTATUS node_status_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
911 struct node_status **pnode_status,
913 struct node_status_extra *extra)
915 struct node_status_query_state *state = tevent_req_data(
916 req, struct node_status_query_state);
917 struct node_status *node_status;
918 size_t num_names = 0;
921 if (tevent_req_is_nterror(req, &status)) {
924 node_status = parse_node_status(
925 mem_ctx, &state->packet->packet.nmb.answers->rdata[0],
927 if (node_status == NULL) {
928 return NT_STATUS_NO_MEMORY;
930 *pnode_status = node_status;
931 *pnum_names = num_names;
935 NTSTATUS node_status_query(TALLOC_CTX *mem_ctx, struct nmb_name *name,
936 const struct sockaddr_storage *addr,
937 struct node_status **pnode_status,
939 struct node_status_extra *extra)
941 TALLOC_CTX *frame = talloc_stackframe();
942 struct tevent_context *ev;
943 struct tevent_req *req;
944 NTSTATUS status = NT_STATUS_NO_MEMORY;
946 ev = samba_tevent_context_init(frame);
950 req = node_status_query_send(ev, ev, name, addr);
954 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
957 status = node_status_query_recv(req, mem_ctx, pnode_status,
964 static bool name_status_lmhosts(const struct sockaddr_storage *paddr,
965 int qname_type, fstring pname)
970 struct samba_sockaddr addr_in = {0};
971 struct samba_sockaddr addr = {0};
974 ok = sockaddr_storage_to_samba_sockaddr(&addr_in, paddr);
978 if (addr_in.u.ss.ss_family != AF_INET) {
982 f = startlmhosts(get_dyn_LMHOSTSFILE());
987 while (getlmhostsent(talloc_tos(), f, &name, &name_type, &addr.u.ss)) {
988 if (addr.u.ss.ss_family != AF_INET) {
991 if (name_type != qname_type) {
994 if (sockaddr_equal(&addr_in.u.sa, &addr.u.sa)) {
995 fstrcpy(pname, name);
1004 /****************************************************************************
1005 Find the first type XX name in a node status reply - used for finding
1006 a servers name given its IP. Return the matched name in *name.
1007 **************************************************************************/
1009 bool name_status_find(const char *q_name,
1012 const struct sockaddr_storage *to_ss,
1015 char addr[INET6_ADDRSTRLEN];
1016 struct node_status *addrs = NULL;
1017 struct nmb_name nname;
1018 size_t count = 0, i;
1019 bool result = false;
1022 if (lp_disable_netbios()) {
1023 DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n",
1028 print_sockaddr(addr, sizeof(addr), to_ss);
1030 DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
1033 /* Check the cache first. */
1035 if (namecache_status_fetch(q_name, q_type, type, to_ss, name)) {
1039 if (to_ss->ss_family != AF_INET) {
1040 /* Can't do node status to IPv6 */
1044 result = name_status_lmhosts(to_ss, type, name);
1046 DBG_DEBUG("Found name %s in lmhosts\n", name);
1047 namecache_status_store(q_name, q_type, type, to_ss, name);
1051 /* W2K PDC's seem not to respond to '*'#0. JRA */
1052 make_nmb_name(&nname, q_name, q_type);
1053 status = node_status_query(talloc_tos(), &nname, to_ss,
1054 &addrs, &count, NULL);
1055 if (!NT_STATUS_IS_OK(status)) {
1059 for (i=0;i<count;i++) {
1060 /* Find first one of the requested type that's not a GROUP. */
1061 if (addrs[i].type == type && ! (addrs[i].flags & 0x80))
1067 pull_ascii_nstring(name, sizeof(fstring), addrs[i].name);
1069 /* Store the result in the cache. */
1070 /* but don't store an entry for 0x1c names here. Here we have
1071 a single host and DOMAIN<0x1c> names should be a list of hosts */
1073 if ( q_type != 0x1c ) {
1074 namecache_status_store(q_name, q_type, type, to_ss, name);
1082 DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
1085 DEBUGADD(10, (", name %s ip address is %s", name, addr));
1093 comparison function used by sort_addr_list
1096 static int addr_compare(const struct sockaddr_storage *ss1,
1097 const struct sockaddr_storage *ss2)
1099 int max_bits1=0, max_bits2=0;
1100 int num_interfaces = iface_count();
1102 struct samba_sockaddr sa1;
1103 struct samba_sockaddr sa2;
1106 ok = sockaddr_storage_to_samba_sockaddr(&sa1, ss1);
1108 return 0; /* No change. */
1111 ok = sockaddr_storage_to_samba_sockaddr(&sa2, ss2);
1113 return 0; /* No change. */
1116 /* Sort IPv4 addresses first. */
1117 if (sa1.u.ss.ss_family != sa2.u.ss.ss_family) {
1118 if (sa2.u.ss.ss_family == AF_INET) {
1125 /* Here we know both addresses are of the same
1128 for (i=0;i<num_interfaces;i++) {
1129 struct samba_sockaddr sif = {0};
1130 const unsigned char *p_ss1 = NULL;
1131 const unsigned char *p_ss2 = NULL;
1132 const unsigned char *p_if = NULL;
1136 ok = sockaddr_storage_to_samba_sockaddr(&sif, iface_n_bcast(i));
1138 return 0; /* No change. */
1140 if (sif.u.ss.ss_family != sa1.u.ss.ss_family) {
1141 /* Ignore interfaces of the wrong type. */
1144 if (sif.u.ss.ss_family == AF_INET) {
1145 p_if = (const unsigned char *)&sif.u.in.sin_addr;
1146 p_ss1 = (const unsigned char *)&sa1.u.in.sin_addr;
1147 p_ss2 = (const unsigned char *)&sa2.u.in.sin_addr;
1150 #if defined(HAVE_IPV6)
1151 if (sif.u.ss.ss_family == AF_INET6) {
1152 p_if = (const unsigned char *)&sif.u.in6.sin6_addr;
1153 p_ss1 = (const unsigned char *)&sa1.u.in6.sin6_addr;
1154 p_ss2 = (const unsigned char *)&sa2.u.in6.sin6_addr;
1158 if (!p_ss1 || !p_ss2 || !p_if || len == 0) {
1161 bits1 = matching_len_bits(p_ss1, p_if, len);
1162 bits2 = matching_len_bits(p_ss2, p_if, len);
1163 max_bits1 = MAX(bits1, max_bits1);
1164 max_bits2 = MAX(bits2, max_bits2);
1167 /* Bias towards directly reachable IPs */
1168 if (iface_local(&sa1.u.sa)) {
1169 if (sa1.u.ss.ss_family == AF_INET) {
1175 if (iface_local(&sa2.u.sa)) {
1176 if (sa2.u.ss.ss_family == AF_INET) {
1182 return max_bits2 - max_bits1;
1185 /*******************************************************************
1186 compare 2 ldap IPs by nearness to our interfaces - used in qsort
1187 *******************************************************************/
1189 static int ip_service_compare(struct ip_service *ss1, struct ip_service *ss2)
1193 if ((result = addr_compare(&ss1->ss, &ss2->ss)) != 0) {
1197 if (ss1->port > ss2->port) {
1201 if (ss1->port < ss2->port) {
1209 sort an IP list so that names that are close to one of our interfaces
1210 are at the top. This prevents the problem where a WINS server returns an IP
1211 that is not reachable from our subnet as the first match
1214 static void sort_addr_list(struct sockaddr_storage *sslist, int count)
1220 TYPESAFE_QSORT(sslist, count, addr_compare);
1223 static void sort_service_list(struct ip_service *servlist, int count)
1229 TYPESAFE_QSORT(servlist, count, ip_service_compare);
1232 /**********************************************************************
1233 Remove any duplicate address/port pairs in the list
1234 *********************************************************************/
1236 size_t remove_duplicate_addrs2(struct ip_service *iplist, size_t count )
1240 DEBUG(10,("remove_duplicate_addrs2: "
1241 "looking for duplicate address/port pairs\n"));
1243 /* One loop to set duplicates to a zero addr. */
1244 for ( i=0; i<count; i++ ) {
1246 struct samba_sockaddr sa_i = {0};
1248 ok = sockaddr_storage_to_samba_sockaddr(&sa_i, &iplist[i].ss);
1253 if (is_zero_addr(&sa_i.u.ss)) {
1257 for ( j=i+1; j<count; j++ ) {
1258 struct samba_sockaddr sa_j = {0};
1260 ok = sockaddr_storage_to_samba_sockaddr(&sa_j,
1266 if (sockaddr_equal(&sa_i.u.sa, &sa_j.u.sa) &&
1267 iplist[i].port == iplist[j].port) {
1268 zero_sockaddr(&iplist[j].ss);
1273 /* Now remove any addresses set to zero above. */
1274 for (i = 0; i < count; i++) {
1276 is_zero_addr(&iplist[i].ss)) {
1277 ARRAY_DEL_ELEMENT(iplist, i, count);
1285 static bool prioritize_ipv4_list(struct ip_service *iplist, int count)
1287 TALLOC_CTX *frame = talloc_stackframe();
1288 struct ip_service *iplist_new = talloc_array(frame, struct ip_service, count);
1291 if (iplist_new == NULL) {
1298 /* Copy IPv4 first. */
1299 for (i = 0; i < count; i++) {
1300 if (iplist[i].ss.ss_family == AF_INET) {
1301 iplist_new[j++] = iplist[i];
1306 for (i = 0; i < count; i++) {
1307 if (iplist[i].ss.ss_family != AF_INET) {
1308 iplist_new[j++] = iplist[i];
1312 memcpy(iplist, iplist_new, sizeof(struct ip_service)*count);
1317 /****************************************************************************
1318 Do a netbios name query to find someones IP.
1319 Returns an array of IP addresses or NULL if none.
1320 *count will be set to the number of addresses returned.
1321 *timed_out is set if we failed by timing out
1322 ****************************************************************************/
1324 struct name_query_state {
1325 struct samba_sockaddr my_addr;
1326 struct samba_sockaddr addr;
1328 bool bcast_star_query;
1334 NTSTATUS validate_error;
1337 struct sockaddr_storage *addrs;
1341 static bool name_query_validator(struct packet_struct *p, void *private_data);
1342 static void name_query_done(struct tevent_req *subreq);
1344 struct tevent_req *name_query_send(TALLOC_CTX *mem_ctx,
1345 struct tevent_context *ev,
1346 const char *name, int name_type,
1347 bool bcast, bool recurse,
1348 const struct sockaddr_storage *addr)
1350 struct tevent_req *req, *subreq;
1351 struct name_query_state *state;
1352 struct packet_struct p;
1353 struct nmb_packet *nmb = &p.packet.nmb;
1356 req = tevent_req_create(mem_ctx, &state, struct name_query_state);
1360 state->bcast = bcast;
1362 if (addr->ss_family != AF_INET) {
1363 /* Can't do node status to IPv6 */
1364 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1365 return tevent_req_post(req, ev);
1368 if (lp_disable_netbios()) {
1369 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
1371 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
1372 return tevent_req_post(req, ev);
1375 ok = sockaddr_storage_to_samba_sockaddr(&state->addr, addr);
1377 /* Node status must be IPv4 */
1378 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1379 return tevent_req_post(req, ev);
1381 state->addr.u.in.sin_port = htons(NMB_PORT);
1383 set_socket_addr_v4(&state->my_addr);
1386 nmb->header.name_trn_id = generate_trn_id();
1387 nmb->header.opcode = 0;
1388 nmb->header.response = false;
1389 nmb->header.nm_flags.bcast = bcast;
1390 nmb->header.nm_flags.recursion_available = false;
1391 nmb->header.nm_flags.recursion_desired = recurse;
1392 nmb->header.nm_flags.trunc = false;
1393 nmb->header.nm_flags.authoritative = false;
1394 nmb->header.rcode = 0;
1395 nmb->header.qdcount = 1;
1396 nmb->header.ancount = 0;
1397 nmb->header.nscount = 0;
1398 nmb->header.arcount = 0;
1400 if (bcast && (strcmp(name, "*")==0)) {
1402 * We're doing a broadcast query for all
1403 * names in the area. Remember this so
1404 * we will wait for all names within
1405 * the timeout period.
1407 state->bcast_star_query = true;
1410 make_nmb_name(&nmb->question.question_name,name,name_type);
1412 nmb->question.question_type = 0x20;
1413 nmb->question.question_class = 0x1;
1415 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
1417 if (state->buflen == 0) {
1418 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1419 DEBUG(10, ("build_packet failed\n"));
1420 return tevent_req_post(req, ev);
1423 subreq = nb_trans_send(state,
1431 nmb->header.name_trn_id,
1432 name_query_validator,
1434 if (tevent_req_nomem(subreq, req)) {
1435 DEBUG(10, ("nb_trans_send failed\n"));
1436 return tevent_req_post(req, ev);
1438 tevent_req_set_callback(subreq, name_query_done, req);
1442 static bool name_query_validator(struct packet_struct *p, void *private_data)
1444 struct name_query_state *state = talloc_get_type_abort(
1445 private_data, struct name_query_state);
1446 struct nmb_packet *nmb = &p->packet.nmb;
1447 struct sockaddr_storage *tmp_addrs;
1448 bool got_unique_netbios_name = false;
1451 debug_nmb_packet(p);
1454 * If we get a Negative Name Query Response from a WINS
1455 * server, we should report it and give up.
1457 if( 0 == nmb->header.opcode /* A query response */
1458 && !state->bcast /* from a WINS server */
1459 && nmb->header.rcode /* Error returned */
1462 if( DEBUGLVL( 3 ) ) {
1463 /* Only executed if DEBUGLEVEL >= 3 */
1464 dbgtext( "Negative name query "
1465 "response, rcode 0x%02x: ",
1466 nmb->header.rcode );
1467 switch( nmb->header.rcode ) {
1469 dbgtext("Request was invalidly formatted.\n");
1472 dbgtext("Problem with NBNS, cannot process "
1476 dbgtext("The name requested does not "
1480 dbgtext("Unsupported request error.\n");
1483 dbgtext("Query refused error.\n");
1486 dbgtext("Unrecognized error code.\n" );
1492 * We accept this packet as valid, but tell the upper
1493 * layers that it's a negative response.
1495 state->validate_error = NT_STATUS_NOT_FOUND;
1499 if (nmb->header.opcode != 0 ||
1500 nmb->header.nm_flags.bcast ||
1501 nmb->header.rcode ||
1502 !nmb->header.ancount) {
1504 * XXXX what do we do with this? Could be a redirect,
1505 * but we'll discard it for the moment.
1510 tmp_addrs = talloc_realloc(
1511 state, state->addrs, struct sockaddr_storage,
1512 state->num_addrs + nmb->answers->rdlength/6);
1513 if (tmp_addrs == NULL) {
1514 state->validate_error = NT_STATUS_NO_MEMORY;
1517 state->addrs = tmp_addrs;
1519 DEBUG(2,("Got a positive name query response "
1520 "from %s ( ", inet_ntoa(p->ip)));
1522 for (i=0; i<nmb->answers->rdlength/6; i++) {
1525 struct sockaddr_storage addr;
1526 struct samba_sockaddr sa = {0};
1530 flags = RSVAL(&nmb->answers->rdata[i*6], 0);
1531 got_unique_netbios_name |= ((flags & 0x8000) == 0);
1533 putip((char *)&ip,&nmb->answers->rdata[2+i*6]);
1534 in_addr_to_sockaddr_storage(&addr, ip);
1536 ok = sockaddr_storage_to_samba_sockaddr(&sa, &addr);
1541 if (is_zero_addr(&sa.u.ss)) {
1545 for (j=0; j<state->num_addrs; j++) {
1546 struct samba_sockaddr sa_j = {0};
1548 ok = sockaddr_storage_to_samba_sockaddr(&sa_j,
1553 if (sockaddr_equal(&sa.u.sa, &sa_j.u.sa)) {
1557 if (j < state->num_addrs) {
1558 /* Already got it */
1562 DEBUGADD(2,("%s ",inet_ntoa(ip)));
1564 state->addrs[state->num_addrs] = addr;
1566 if (state->num_addrs + 1 < state->num_addrs) {
1569 state->num_addrs += 1;
1571 DEBUGADD(2,(")\n"));
1573 /* We add the flags back ... */
1574 if (nmb->header.response)
1575 state->flags |= NM_FLAGS_RS;
1576 if (nmb->header.nm_flags.authoritative)
1577 state->flags |= NM_FLAGS_AA;
1578 if (nmb->header.nm_flags.trunc)
1579 state->flags |= NM_FLAGS_TC;
1580 if (nmb->header.nm_flags.recursion_desired)
1581 state->flags |= NM_FLAGS_RD;
1582 if (nmb->header.nm_flags.recursion_available)
1583 state->flags |= NM_FLAGS_RA;
1584 if (nmb->header.nm_flags.bcast)
1585 state->flags |= NM_FLAGS_B;
1589 * We have to collect all entries coming in from broadcast
1590 * queries. If we got a unique name and we are not querying
1591 * all names registered within broadcast area (query
1592 * for the name '*', so state->bcast_star_query is set),
1595 return (got_unique_netbios_name && !state->bcast_star_query);
1598 * WINS responses are accepted when they are received
1603 static void name_query_done(struct tevent_req *subreq)
1605 struct tevent_req *req = tevent_req_callback_data(
1606 subreq, struct tevent_req);
1607 struct name_query_state *state = tevent_req_data(
1608 req, struct name_query_state);
1610 struct packet_struct *p = NULL;
1612 status = nb_trans_recv(subreq, state, &p);
1613 TALLOC_FREE(subreq);
1614 if (tevent_req_nterror(req, status)) {
1617 if (!NT_STATUS_IS_OK(state->validate_error)) {
1618 tevent_req_nterror(req, state->validate_error);
1621 tevent_req_done(req);
1624 NTSTATUS name_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1625 struct sockaddr_storage **addrs, size_t *num_addrs,
1628 struct name_query_state *state = tevent_req_data(
1629 req, struct name_query_state);
1632 if (tevent_req_is_nterror(req, &status)) {
1634 NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
1636 * In the broadcast case we collect replies until the
1639 status = NT_STATUS_OK;
1641 if (!NT_STATUS_IS_OK(status)) {
1645 if (state->num_addrs == 0) {
1646 return NT_STATUS_NOT_FOUND;
1648 *addrs = talloc_move(mem_ctx, &state->addrs);
1649 sort_addr_list(*addrs, state->num_addrs);
1650 *num_addrs = state->num_addrs;
1651 if (flags != NULL) {
1652 *flags = state->flags;
1654 return NT_STATUS_OK;
1657 NTSTATUS name_query(const char *name, int name_type,
1658 bool bcast, bool recurse,
1659 const struct sockaddr_storage *to_ss,
1660 TALLOC_CTX *mem_ctx,
1661 struct sockaddr_storage **addrs,
1662 size_t *num_addrs, uint8_t *flags)
1664 TALLOC_CTX *frame = talloc_stackframe();
1665 struct tevent_context *ev;
1666 struct tevent_req *req;
1667 struct timeval timeout;
1668 NTSTATUS status = NT_STATUS_NO_MEMORY;
1670 ev = samba_tevent_context_init(frame);
1674 req = name_query_send(ev, ev, name, name_type, bcast, recurse, to_ss);
1679 timeout = timeval_current_ofs(0, 250000);
1681 timeout = timeval_current_ofs(2, 0);
1683 if (!tevent_req_set_endtime(req, ev, timeout)) {
1686 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1689 status = name_query_recv(req, mem_ctx, addrs, num_addrs, flags);
1695 /********************************************************
1696 Convert an array if struct sockaddr_storage to struct ip_service
1697 return false on failure. Port is set to PORT_NONE;
1698 orig_count is the length of ss_list on input,
1699 *count_out is the length of return_iplist on output as we remove any
1700 zero addresses from ss_list.
1701 *********************************************************/
1703 static bool convert_ss2service(TALLOC_CTX *ctx,
1704 struct ip_service **return_iplist,
1705 const struct sockaddr_storage *ss_list,
1710 size_t real_count = 0;
1711 struct ip_service *iplist = NULL;
1713 if (orig_count == 0 || ss_list == NULL) {
1717 /* Filter out zero addrs. */
1718 for (i = 0; i < orig_count; i++ ) {
1719 if (is_zero_addr(&ss_list[i])) {
1724 if (real_count == 0) {
1728 /* copy the ip address; port will be PORT_NONE */
1729 iplist = talloc_zero_array(ctx, struct ip_service, real_count);
1730 if (iplist == NULL) {
1731 DBG_ERR("talloc failed for %zu enetries!\n", real_count);
1736 for (i=0; i < orig_count; i++ ) {
1737 if (is_zero_addr(&ss_list[i])) {
1740 iplist[real_count].ss = ss_list[i];
1741 iplist[real_count].port = PORT_NONE;
1745 *return_iplist = iplist;
1746 *count_out = real_count;
1750 struct name_queries_state {
1751 struct tevent_context *ev;
1756 const struct sockaddr_storage *addrs;
1761 struct tevent_req **subreqs;
1762 size_t num_received;
1765 size_t received_index;
1766 struct sockaddr_storage *result_addrs;
1767 size_t num_result_addrs;
1771 static void name_queries_done(struct tevent_req *subreq);
1772 static void name_queries_next(struct tevent_req *subreq);
1775 * Send a name query to multiple destinations with a wait time in between
1778 static struct tevent_req *name_queries_send(
1779 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1780 const char *name, int name_type,
1781 bool bcast, bool recurse,
1782 const struct sockaddr_storage *addrs,
1783 size_t num_addrs, int wait_msec, int timeout_msec)
1785 struct tevent_req *req, *subreq;
1786 struct name_queries_state *state;
1788 req = tevent_req_create(mem_ctx, &state,
1789 struct name_queries_state);
1795 state->name_type = name_type;
1796 state->bcast = bcast;
1797 state->recurse = recurse;
1798 state->addrs = addrs;
1799 state->num_addrs = num_addrs;
1800 state->wait_msec = wait_msec;
1801 state->timeout_msec = timeout_msec;
1803 state->subreqs = talloc_zero_array(
1804 state, struct tevent_req *, num_addrs);
1805 if (tevent_req_nomem(state->subreqs, req)) {
1806 return tevent_req_post(req, ev);
1808 state->num_sent = 0;
1810 subreq = name_query_send(
1811 state->subreqs, state->ev, name, name_type, bcast, recurse,
1812 &state->addrs[state->num_sent]);
1813 if (tevent_req_nomem(subreq, req)) {
1814 return tevent_req_post(req, ev);
1816 if (!tevent_req_set_endtime(
1818 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1819 return tevent_req_post(req, ev);
1821 tevent_req_set_callback(subreq, name_queries_done, req);
1823 state->subreqs[state->num_sent] = subreq;
1824 state->num_sent += 1;
1826 if (state->num_sent < state->num_addrs) {
1827 subreq = tevent_wakeup_send(
1829 timeval_current_ofs(0, state->wait_msec * 1000));
1830 if (tevent_req_nomem(subreq, req)) {
1831 return tevent_req_post(req, ev);
1833 tevent_req_set_callback(subreq, name_queries_next, req);
1838 static void name_queries_done(struct tevent_req *subreq)
1840 struct tevent_req *req = tevent_req_callback_data(
1841 subreq, struct tevent_req);
1842 struct name_queries_state *state = tevent_req_data(
1843 req, struct name_queries_state);
1847 status = name_query_recv(subreq, state, &state->result_addrs,
1848 &state->num_result_addrs, &state->flags);
1850 for (i=0; i<state->num_sent; i++) {
1851 if (state->subreqs[i] == subreq) {
1855 if (i == state->num_sent) {
1856 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1859 TALLOC_FREE(state->subreqs[i]);
1862 if (state->num_received + 1 < state->num_received) {
1863 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1866 state->num_received += 1;
1868 if (!NT_STATUS_IS_OK(status)) {
1870 if (state->num_received >= state->num_addrs) {
1871 tevent_req_nterror(req, status);
1875 * Still outstanding requests, just wait
1879 state->received_index = i;
1880 tevent_req_done(req);
1883 static void name_queries_next(struct tevent_req *subreq)
1885 struct tevent_req *req = tevent_req_callback_data(
1886 subreq, struct tevent_req);
1887 struct name_queries_state *state = tevent_req_data(
1888 req, struct name_queries_state);
1890 if (!tevent_wakeup_recv(subreq)) {
1891 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1895 subreq = name_query_send(
1896 state->subreqs, state->ev,
1897 state->name, state->name_type, state->bcast, state->recurse,
1898 &state->addrs[state->num_sent]);
1899 if (tevent_req_nomem(subreq, req)) {
1902 tevent_req_set_callback(subreq, name_queries_done, req);
1903 if (!tevent_req_set_endtime(
1905 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1908 state->subreqs[state->num_sent] = subreq;
1909 state->num_sent += 1;
1911 if (state->num_sent < state->num_addrs) {
1912 subreq = tevent_wakeup_send(
1914 timeval_current_ofs(0, state->wait_msec * 1000));
1915 if (tevent_req_nomem(subreq, req)) {
1918 tevent_req_set_callback(subreq, name_queries_next, req);
1922 static NTSTATUS name_queries_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1923 struct sockaddr_storage **result_addrs,
1924 size_t *num_result_addrs, uint8_t *flags,
1925 size_t *received_index)
1927 struct name_queries_state *state = tevent_req_data(
1928 req, struct name_queries_state);
1931 if (tevent_req_is_nterror(req, &status)) {
1935 if (result_addrs != NULL) {
1936 *result_addrs = talloc_move(mem_ctx, &state->result_addrs);
1938 if (num_result_addrs != NULL) {
1939 *num_result_addrs = state->num_result_addrs;
1941 if (flags != NULL) {
1942 *flags = state->flags;
1944 if (received_index != NULL) {
1945 *received_index = state->received_index;
1947 return NT_STATUS_OK;
1950 /********************************************************
1951 Resolve via "bcast" method.
1952 *********************************************************/
1954 struct name_resolve_bcast_state {
1955 struct sockaddr_storage *addrs;
1959 static void name_resolve_bcast_done(struct tevent_req *subreq);
1961 struct tevent_req *name_resolve_bcast_send(TALLOC_CTX *mem_ctx,
1962 struct tevent_context *ev,
1966 struct tevent_req *req, *subreq;
1967 struct name_resolve_bcast_state *state;
1968 struct sockaddr_storage *bcast_addrs;
1969 size_t i, num_addrs, num_bcast_addrs;
1971 req = tevent_req_create(mem_ctx, &state,
1972 struct name_resolve_bcast_state);
1977 if (lp_disable_netbios()) {
1978 DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
1980 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1981 return tevent_req_post(req, ev);
1985 * "bcast" means do a broadcast lookup on all the local interfaces.
1988 DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
1989 "for name %s<0x%x>\n", name, name_type));
1991 num_addrs = iface_count();
1992 bcast_addrs = talloc_array(state, struct sockaddr_storage, num_addrs);
1993 if (tevent_req_nomem(bcast_addrs, req)) {
1994 return tevent_req_post(req, ev);
1998 * Lookup the name on all the interfaces, return on
1999 * the first successful match.
2001 num_bcast_addrs = 0;
2003 for (i=0; i<num_addrs; i++) {
2004 const struct sockaddr_storage *pss = iface_n_bcast(i);
2006 if (pss->ss_family != AF_INET) {
2009 bcast_addrs[num_bcast_addrs] = *pss;
2010 num_bcast_addrs += 1;
2013 subreq = name_queries_send(state, ev, name, name_type, true, true,
2014 bcast_addrs, num_bcast_addrs, 0, 250);
2015 if (tevent_req_nomem(subreq, req)) {
2016 return tevent_req_post(req, ev);
2018 tevent_req_set_callback(subreq, name_resolve_bcast_done, req);
2022 static void name_resolve_bcast_done(struct tevent_req *subreq)
2024 struct tevent_req *req = tevent_req_callback_data(
2025 subreq, struct tevent_req);
2026 struct name_resolve_bcast_state *state = tevent_req_data(
2027 req, struct name_resolve_bcast_state);
2030 status = name_queries_recv(subreq, state,
2031 &state->addrs, &state->num_addrs,
2033 TALLOC_FREE(subreq);
2034 if (tevent_req_nterror(req, status)) {
2037 tevent_req_done(req);
2040 NTSTATUS name_resolve_bcast_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2041 struct sockaddr_storage **addrs,
2044 struct name_resolve_bcast_state *state = tevent_req_data(
2045 req, struct name_resolve_bcast_state);
2048 if (tevent_req_is_nterror(req, &status)) {
2051 *addrs = talloc_move(mem_ctx, &state->addrs);
2052 *num_addrs = state->num_addrs;
2053 return NT_STATUS_OK;
2056 NTSTATUS name_resolve_bcast(TALLOC_CTX *mem_ctx,
2059 struct sockaddr_storage **return_iplist,
2060 size_t *return_count)
2062 TALLOC_CTX *frame = talloc_stackframe();
2063 struct tevent_context *ev;
2064 struct tevent_req *req;
2065 NTSTATUS status = NT_STATUS_NO_MEMORY;
2067 ev = samba_tevent_context_init(frame);
2071 req = name_resolve_bcast_send(frame, ev, name, name_type);
2075 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2078 status = name_resolve_bcast_recv(req, mem_ctx, return_iplist,
2085 struct query_wins_list_state {
2086 struct tevent_context *ev;
2089 struct in_addr *servers;
2091 struct sockaddr_storage server;
2094 struct sockaddr_storage *addrs;
2099 static void query_wins_list_done(struct tevent_req *subreq);
2102 * Query a list of (replicating) wins servers in sequence, call them
2103 * dead if they don't reply
2106 static struct tevent_req *query_wins_list_send(
2107 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2108 struct in_addr src_ip, const char *name, uint8_t name_type,
2109 struct in_addr *servers, size_t num_servers)
2111 struct tevent_req *req, *subreq;
2112 struct query_wins_list_state *state;
2114 req = tevent_req_create(mem_ctx, &state,
2115 struct query_wins_list_state);
2121 state->name_type = name_type;
2122 state->servers = servers;
2123 state->num_servers = num_servers;
2125 if (state->num_servers == 0) {
2126 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2127 return tevent_req_post(req, ev);
2130 in_addr_to_sockaddr_storage(
2131 &state->server, state->servers[state->num_sent]);
2133 subreq = name_query_send(state, state->ev,
2134 state->name, state->name_type,
2135 false, true, &state->server);
2137 if (tevent_req_nomem(subreq, req)) {
2138 return tevent_req_post(req, ev);
2142 if (state->num_sent + 1 < state->num_sent) {
2143 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2144 return tevent_req_post(req, ev);
2147 state->num_sent += 1;
2148 if (!tevent_req_set_endtime(subreq, state->ev,
2149 timeval_current_ofs(2, 0))) {
2150 return tevent_req_post(req, ev);
2152 tevent_req_set_callback(subreq, query_wins_list_done, req);
2156 static void query_wins_list_done(struct tevent_req *subreq)
2158 struct tevent_req *req = tevent_req_callback_data(
2159 subreq, struct tevent_req);
2160 struct query_wins_list_state *state = tevent_req_data(
2161 req, struct query_wins_list_state);
2164 status = name_query_recv(subreq, state,
2165 &state->addrs, &state->num_addrs,
2167 TALLOC_FREE(subreq);
2168 if (NT_STATUS_IS_OK(status)) {
2169 tevent_req_done(req);
2172 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
2173 tevent_req_nterror(req, status);
2176 wins_srv_died(state->servers[state->num_sent-1],
2177 my_socket_addr_v4());
2179 if (state->num_sent == state->num_servers) {
2180 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2184 in_addr_to_sockaddr_storage(
2185 &state->server, state->servers[state->num_sent]);
2187 subreq = name_query_send(state, state->ev,
2188 state->name, state->name_type,
2189 false, true, &state->server);
2190 state->num_sent += 1;
2191 if (tevent_req_nomem(subreq, req)) {
2194 if (!tevent_req_set_endtime(subreq, state->ev,
2195 timeval_current_ofs(2, 0))) {
2198 tevent_req_set_callback(subreq, query_wins_list_done, req);
2201 static NTSTATUS query_wins_list_recv(struct tevent_req *req,
2202 TALLOC_CTX *mem_ctx,
2203 struct sockaddr_storage **addrs,
2207 struct query_wins_list_state *state = tevent_req_data(
2208 req, struct query_wins_list_state);
2211 if (tevent_req_is_nterror(req, &status)) {
2214 if (addrs != NULL) {
2215 *addrs = talloc_move(mem_ctx, &state->addrs);
2217 if (num_addrs != NULL) {
2218 *num_addrs = state->num_addrs;
2220 if (flags != NULL) {
2221 *flags = state->flags;
2223 return NT_STATUS_OK;
2226 struct resolve_wins_state {
2228 size_t num_received;
2230 struct sockaddr_storage *addrs;
2235 static void resolve_wins_done(struct tevent_req *subreq);
2237 struct tevent_req *resolve_wins_send(TALLOC_CTX *mem_ctx,
2238 struct tevent_context *ev,
2242 struct tevent_req *req, *subreq;
2243 struct resolve_wins_state *state;
2244 char **wins_tags = NULL;
2245 struct sockaddr_storage src_ss;
2246 struct samba_sockaddr src_sa = {0};
2247 struct in_addr src_ip;
2248 size_t i, num_wins_tags;
2251 req = tevent_req_create(mem_ctx, &state,
2252 struct resolve_wins_state);
2257 if (wins_srv_count() < 1) {
2258 DEBUG(3,("resolve_wins: WINS server resolution selected "
2259 "and no WINS servers listed.\n"));
2260 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2264 /* the address we will be sending from */
2265 if (!interpret_string_addr(&src_ss, lp_nbt_client_socket_address(),
2266 AI_NUMERICHOST|AI_PASSIVE)) {
2267 zero_sockaddr(&src_ss);
2270 ok = sockaddr_storage_to_samba_sockaddr(&src_sa, &src_ss);
2272 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2276 if (src_sa.u.ss.ss_family != AF_INET) {
2277 char addr[INET6_ADDRSTRLEN];
2278 print_sockaddr(addr, sizeof(addr), &src_sa.u.ss);
2279 DEBUG(3,("resolve_wins: cannot receive WINS replies "
2280 "on IPv6 address %s\n",
2282 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2286 src_ip = src_sa.u.in.sin_addr;
2288 wins_tags = wins_srv_tags();
2289 if (wins_tags == NULL) {
2290 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2295 while (wins_tags[num_wins_tags] != NULL) {
2297 if (num_wins_tags + 1 < num_wins_tags) {
2298 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2304 for (i=0; i<num_wins_tags; i++) {
2305 size_t num_servers, num_alive;
2306 struct in_addr *servers, *alive;
2309 if (!wins_server_tag_ips(wins_tags[i], talloc_tos(),
2310 &servers, &num_servers)) {
2311 DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
2316 alive = talloc_array(state, struct in_addr, num_servers);
2317 if (tevent_req_nomem(alive, req)) {
2322 for (j=0; j<num_servers; j++) {
2323 struct in_addr wins_ip = servers[j];
2325 if (global_in_nmbd && ismyip_v4(wins_ip)) {
2326 /* yikes! we'll loop forever */
2329 /* skip any that have been unresponsive lately */
2330 if (wins_srv_is_dead(wins_ip, src_ip)) {
2333 DEBUG(3, ("resolve_wins: using WINS server %s "
2335 inet_ntoa(wins_ip), wins_tags[i]));
2336 alive[num_alive] = wins_ip;
2339 TALLOC_FREE(servers);
2341 if (num_alive == 0) {
2345 subreq = query_wins_list_send(
2346 state, ev, src_ip, name, name_type,
2348 if (tevent_req_nomem(subreq, req)) {
2351 tevent_req_set_callback(subreq, resolve_wins_done, req);
2352 state->num_sent += 1;
2355 if (state->num_sent == 0) {
2356 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2360 wins_srv_tags_free(wins_tags);
2363 wins_srv_tags_free(wins_tags);
2364 return tevent_req_post(req, ev);
2367 static void resolve_wins_done(struct tevent_req *subreq)
2369 struct tevent_req *req = tevent_req_callback_data(
2370 subreq, struct tevent_req);
2371 struct resolve_wins_state *state = tevent_req_data(
2372 req, struct resolve_wins_state);
2375 status = query_wins_list_recv(subreq, state, &state->addrs,
2376 &state->num_addrs, &state->flags);
2377 if (NT_STATUS_IS_OK(status)) {
2378 tevent_req_done(req);
2383 if (state->num_received + 1 < state->num_received) {
2384 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2388 state->num_received += 1;
2390 if (state->num_received < state->num_sent) {
2392 * Wait for the others
2396 tevent_req_nterror(req, status);
2399 NTSTATUS resolve_wins_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2400 struct sockaddr_storage **addrs,
2401 size_t *num_addrs, uint8_t *flags)
2403 struct resolve_wins_state *state = tevent_req_data(
2404 req, struct resolve_wins_state);
2407 if (tevent_req_is_nterror(req, &status)) {
2410 if (addrs != NULL) {
2411 *addrs = talloc_move(mem_ctx, &state->addrs);
2413 if (num_addrs != NULL) {
2414 *num_addrs = state->num_addrs;
2416 if (flags != NULL) {
2417 *flags = state->flags;
2419 return NT_STATUS_OK;
2422 /********************************************************
2423 Resolve via "wins" method.
2424 *********************************************************/
2426 NTSTATUS resolve_wins(TALLOC_CTX *mem_ctx,
2429 struct sockaddr_storage **return_iplist,
2430 size_t *return_count)
2432 struct tevent_context *ev;
2433 struct tevent_req *req;
2434 NTSTATUS status = NT_STATUS_NO_MEMORY;
2436 ev = samba_tevent_context_init(talloc_tos());
2440 req = resolve_wins_send(ev, ev, name, name_type);
2444 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2447 status = resolve_wins_recv(req, mem_ctx, return_iplist, return_count,
2454 /********************************************************
2455 Use ads_dns_lookup_[a|aaaa]_send() calls to look up a
2456 list of names asynchronously.
2457 *********************************************************/
2459 struct dns_query_state {
2460 /* Used to tell our parent we've completed. */
2461 struct dns_lookup_list_async_state *p_async_state;
2462 const char *query_name; /* For debugging only. */
2464 struct samba_sockaddr *addrs;
2467 struct dns_lookup_list_async_state {
2469 size_t num_query_returns;
2470 struct dns_query_state *queries;
2473 /* Called on query timeout. */
2474 static void dns_lookup_send_timeout_handler(struct tevent_context *ev,
2475 struct tevent_timer *te,
2479 bool *timed_out = (bool *)private_data;
2483 static void dns_lookup_list_a_done(struct tevent_req *req);
2484 #if defined(HAVE_IPV6)
2485 static void dns_lookup_list_aaaa_done(struct tevent_req *req);
2488 NTSTATUS dns_lookup_list_async(TALLOC_CTX *ctx,
2489 size_t num_dns_names,
2490 const char **dns_lookup_names,
2491 size_t *p_num_addrs,
2492 struct samba_sockaddr **pp_addrs,
2493 char ***pp_dns_names)
2495 struct dns_lookup_list_async_state *state = NULL;
2496 struct tevent_context *ev = NULL;
2497 struct tevent_req *req = NULL;
2498 struct tevent_timer *timer = NULL;
2499 size_t num_queries_sent = 0;
2500 size_t queries_size = num_dns_names;
2502 size_t num_addrs = 0;
2503 struct samba_sockaddr *addr_out = NULL;
2504 char **dns_names_ret = NULL;
2505 NTSTATUS status = NT_STATUS_NO_MEMORY;
2507 /* Nothing to do. */
2508 if (num_dns_names == 0) {
2511 if (pp_dns_names != NULL) {
2512 *pp_dns_names = NULL;
2514 return NT_STATUS_OK;
2517 state = talloc_zero(ctx, struct dns_lookup_list_async_state);
2518 if (state == NULL) {
2521 ev = samba_tevent_context_init(ctx);
2526 #if defined(HAVE_IPV6)
2527 queries_size = 2 * num_dns_names;
2528 /* Wrap protection. */
2529 if (queries_size < num_dns_names) {
2533 queries_size = num_dns_names;
2536 state->queries = talloc_zero_array(state,
2537 struct dns_query_state,
2539 if (state->queries == NULL) {
2543 /* Hit all the DNS servers with asnyc lookups for all the names. */
2544 for (i = 0; i < num_dns_names; i++) {
2545 DBG_INFO("async DNS lookup A record for %s\n",
2546 dns_lookup_names[i]);
2548 /* Setup the query state. */
2549 state->queries[num_queries_sent].query_name =
2550 dns_lookup_names[i];
2551 state->queries[num_queries_sent].p_async_state = state;
2553 req = ads_dns_lookup_a_send(ev, ev, dns_lookup_names[i]);
2557 tevent_req_set_callback(req,
2558 dns_lookup_list_a_done,
2559 &state->queries[num_queries_sent]);
2562 #if defined(HAVE_IPV6)
2563 /* If we're IPv6 capable ask for that too. */
2564 state->queries[num_queries_sent].query_name =
2565 dns_lookup_names[i];
2566 state->queries[num_queries_sent].p_async_state = state;
2568 DBG_INFO("async DNS lookup AAAA record for %s\n",
2569 dns_lookup_names[i]);
2571 req = ads_dns_lookup_aaaa_send(ev, ev, dns_lookup_names[i]);
2575 tevent_req_set_callback(req,
2576 dns_lookup_list_aaaa_done,
2577 &state->queries[num_queries_sent]);
2582 /* We must always have a timeout. */
2583 timer = tevent_add_timer(ev,
2585 timeval_current_ofs(lp_get_async_dns_timeout(),
2587 dns_lookup_send_timeout_handler,
2589 if (timer == NULL) {
2593 /* Loop until timed out or got all replies. */
2597 if (state->timed_out) {
2600 if (state->num_query_returns == num_queries_sent) {
2603 ret = tevent_loop_once(ev);
2609 /* Count what we got back. */
2610 for (i = 0; i < num_queries_sent; i++) {
2611 struct dns_query_state *query = &state->queries[i];
2614 if (num_addrs + query->num_addrs < num_addrs) {
2617 num_addrs += query->num_addrs;
2620 if (state->timed_out) {
2621 DBG_INFO("async DNS lookup timed out after %zu entries "
2626 addr_out = talloc_zero_array(ctx,
2627 struct samba_sockaddr,
2629 if (addr_out == NULL) {
2634 * Did the caller want an array of names back
2635 * that match the IP addresses ? If we provide
2636 * this, dsgetdcname() internals can now use this
2637 * async lookup code also.
2639 if (pp_dns_names != NULL) {
2640 dns_names_ret = talloc_zero_array(ctx,
2643 if (dns_names_ret == NULL) {
2648 /* Copy what we got back. */
2650 for (i = 0; i < num_queries_sent; i++) {
2652 struct dns_query_state *query = &state->queries[i];
2654 if (query->num_addrs == 0) {
2658 if (dns_names_ret != NULL) {
2660 * If caller wants a name array matched with
2661 * the addrs array, copy the same queried name for
2662 * each IP address returned.
2664 for (j = 0; j < query->num_addrs; j++) {
2665 dns_names_ret[num_addrs + j] = talloc_strdup(
2668 if (dns_names_ret[num_addrs + j] == NULL) {
2674 for (j = 0; j < query->num_addrs; j++) {
2675 addr_out[num_addrs] = query->addrs[j];
2677 num_addrs += query->num_addrs;
2680 *p_num_addrs = num_addrs;
2681 *pp_addrs = addr_out;
2682 if (pp_dns_names != NULL) {
2683 *pp_dns_names = dns_names_ret;
2686 status = NT_STATUS_OK;
2696 Called when an A record lookup completes.
2699 static void dns_lookup_list_a_done(struct tevent_req *req)
2702 * Callback data is an element of a talloc'ed array,
2703 * not a talloc object in its own right. So use the
2704 * tevent_req_callback_data_void() void * cast function.
2706 struct dns_query_state *state = (struct dns_query_state *)
2707 tevent_req_callback_data_void(req);
2710 char **hostnames_out = NULL;
2711 struct samba_sockaddr *addrs = NULL;
2712 size_t num_addrs = 0;
2715 /* For good or ill, tell the parent we're finished. */
2716 state->p_async_state->num_query_returns++;
2718 status = ads_dns_lookup_a_recv(req,
2719 state->p_async_state,
2724 if (!NT_STATUS_IS_OK(status)) {
2725 DBG_INFO("async DNS A lookup for %s returned %s\n",
2731 if (rcode != DNS_RCODE_OK) {
2732 DBG_INFO("async DNS A lookup for %s returned DNS code %u\n",
2734 (unsigned int)rcode);
2738 if (num_addrs == 0) {
2739 DBG_INFO("async DNS A lookup for %s returned 0 addresses.\n",
2744 /* Copy data out. */
2745 state->addrs = talloc_zero_array(state->p_async_state,
2746 struct samba_sockaddr,
2748 if (state->addrs == NULL) {
2752 for (i = 0; i < num_addrs; i++) {
2753 char addr[INET6_ADDRSTRLEN];
2754 DBG_INFO("async DNS A lookup for %s [%zu] got %s -> %s\n",
2758 print_sockaddr(addr,
2762 state->addrs[i] = addrs[i];
2764 state->num_addrs = num_addrs;
2767 #if defined(HAVE_IPV6)
2769 Called when an AAAA record lookup completes.
2772 static void dns_lookup_list_aaaa_done(struct tevent_req *req)
2775 * Callback data is an element of a talloc'ed array,
2776 * not a talloc object in its own right. So use the
2777 * tevent_req_callback_data_void() void * cast function.
2779 struct dns_query_state *state = (struct dns_query_state *)
2780 tevent_req_callback_data_void(req);
2783 char **hostnames_out = NULL;
2784 struct samba_sockaddr *addrs = NULL;
2785 size_t num_addrs = 0;
2788 /* For good or ill, tell the parent we're finished. */
2789 state->p_async_state->num_query_returns++;
2791 status = ads_dns_lookup_aaaa_recv(req,
2792 state->p_async_state,
2797 if (!NT_STATUS_IS_OK(status)) {
2798 DBG_INFO("async DNS AAAA lookup for %s returned %s\n",
2804 if (rcode != DNS_RCODE_OK) {
2805 DBG_INFO("async DNS AAAA lookup for %s returned DNS code %u\n",
2807 (unsigned int)rcode);
2811 if (num_addrs == 0) {
2812 DBG_INFO("async DNS AAAA lookup for %s returned 0 addresses.\n",
2817 /* Copy data out. */
2818 state->addrs = talloc_zero_array(state->p_async_state,
2819 struct samba_sockaddr,
2821 if (state->addrs == NULL) {
2825 for (i = 0; i < num_addrs; i++) {
2826 char addr[INET6_ADDRSTRLEN];
2827 DBG_INFO("async DNS AAAA lookup for %s [%zu] got %s -> %s\n",
2831 print_sockaddr(addr,
2834 state->addrs[i] = addrs[i];
2836 state->num_addrs = num_addrs;
2840 /********************************************************
2841 Resolve via "hosts" method.
2842 *********************************************************/
2844 static NTSTATUS resolve_hosts(TALLOC_CTX *mem_ctx,
2847 struct sockaddr_storage **return_iplist,
2851 * "host" means do a localhost, or dns lookup.
2853 struct addrinfo hints;
2854 struct addrinfo *ailist = NULL;
2855 struct addrinfo *res = NULL;
2859 if ( name_type != 0x20 && name_type != 0x0) {
2860 DEBUG(5, ("resolve_hosts: not appropriate "
2861 "for name type <0x%x>\n",
2863 return NT_STATUS_INVALID_PARAMETER;
2866 *return_iplist = NULL;
2869 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
2873 /* By default make sure it supports TCP. */
2874 hints.ai_socktype = SOCK_STREAM;
2875 hints.ai_flags = AI_ADDRCONFIG;
2877 #if !defined(HAVE_IPV6)
2878 /* Unless we have IPv6, we really only want IPv4 addresses back. */
2879 hints.ai_family = AF_INET;
2882 ret = getaddrinfo(name,
2887 DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
2889 gai_strerror(ret) ));
2892 for (res = ailist; res; res = res->ai_next) {
2893 struct sockaddr_storage ss;
2895 if (!res->ai_addr || res->ai_addrlen == 0) {
2900 memcpy(&ss, res->ai_addr, res->ai_addrlen);
2902 if (is_zero_addr(&ss)) {
2908 *return_iplist = talloc_realloc(
2909 mem_ctx, *return_iplist, struct sockaddr_storage,
2911 if (!*return_iplist) {
2912 DEBUG(3,("resolve_hosts: malloc fail !\n"));
2913 freeaddrinfo(ailist);
2914 return NT_STATUS_NO_MEMORY;
2916 (*return_iplist)[i] = ss;
2920 freeaddrinfo(ailist);
2922 if (*return_count) {
2923 return NT_STATUS_OK;
2925 return NT_STATUS_UNSUCCESSFUL;
2928 /********************************************************
2929 Resolve via "ADS" method.
2930 *********************************************************/
2932 /* Special name type used to cause a _kerberos DNS lookup. */
2933 #define KDC_NAME_TYPE 0xDCDC
2935 static NTSTATUS resolve_ads(TALLOC_CTX *ctx,
2938 const char *sitename,
2939 struct sockaddr_storage **return_addrs,
2944 struct dns_rr_srv *dcs = NULL;
2947 size_t num_srv_addrs = 0;
2948 struct sockaddr_storage *srv_addrs = NULL;
2949 size_t num_dns_addrs = 0;
2950 struct samba_sockaddr *dns_addrs = NULL;
2951 size_t num_dns_names = 0;
2952 const char **dns_lookup_names = NULL;
2953 struct sockaddr_storage *ret_addrs = NULL;
2955 if ((name_type != 0x1c) && (name_type != KDC_NAME_TYPE) &&
2956 (name_type != 0x1b)) {
2957 return NT_STATUS_INVALID_PARAMETER;
2960 switch (name_type) {
2962 DEBUG(5,("resolve_ads: Attempting to resolve "
2963 "PDC for %s using DNS\n", name));
2964 status = ads_dns_query_pdc(ctx,
2971 DEBUG(5,("resolve_ads: Attempting to resolve "
2972 "DCs for %s using DNS\n", name));
2973 status = ads_dns_query_dcs(ctx,
2980 DEBUG(5,("resolve_ads: Attempting to resolve "
2981 "KDCs for %s using DNS\n", name));
2982 status = ads_dns_query_kdcs(ctx,
2989 status = NT_STATUS_INVALID_PARAMETER;
2993 if (!NT_STATUS_IS_OK(status)) {
2999 *return_addrs = NULL;
3002 return NT_STATUS_OK;
3008 return NT_STATUS_INVALID_PARAMETER;
3012 * Split the returned values into 2 arrays. First one
3013 * is a struct sockaddr_storage array that contains results
3014 * from the SRV record lookup that contain both hostnames
3015 * and IP addresses. We only need to copy out the IP
3016 * addresses. This is srv_addrs.
3018 * Second array contains the results from the SRV record
3019 * lookup that only contain hostnames - no IP addresses.
3020 * We must then call dns_lookup_list() to lookup
3021 * hostnames -> IP address. This is dns_addrs.
3023 * Finally we will merge these two arrays to create the
3024 * return sockaddr_storage array.
3027 /* First count the sizes of each array. */
3028 for(i = 0; i < numdcs; i++) {
3029 if (dcs[i].ss_s != NULL) {
3030 /* IP address returned in SRV record. */
3031 if (num_srv_addrs + dcs[i].num_ips < num_srv_addrs) {
3034 return NT_STATUS_INVALID_PARAMETER;
3036 /* Add in the number of addresses we got. */
3037 num_srv_addrs += dcs[i].num_ips;
3039 * If we got any IP addresses zero out
3040 * the hostname so we know we've already
3041 * processed this entry and won't add it
3042 * to the dns_lookup_names array we use
3043 * to do DNS queries below.
3045 dcs[i].hostname = NULL;
3047 /* Ensure we have a hostname to lookup. */
3048 if (dcs[i].hostname == NULL) {
3051 /* No IP address returned in SRV record. */
3052 if (num_dns_names + 1 < num_dns_names) {
3055 return NT_STATUS_INVALID_PARAMETER;
3057 /* One more name to lookup. */
3062 /* Allocate the list of IP addresses we already have. */
3063 srv_addrs = talloc_zero_array(ctx,
3064 struct sockaddr_storage,
3066 if (srv_addrs == NULL) {
3068 return NT_STATUS_NO_MEMORY;
3071 /* Copy the addresses we already have. */
3073 for(i = 0; i < numdcs; i++) {
3074 /* Copy all the IP addresses from the SRV response */
3076 for (j = 0; j < dcs[i].num_ips; j++) {
3077 char addr[INET6_ADDRSTRLEN];
3079 srv_addrs[num_srv_addrs] = dcs[i].ss_s[j];
3080 if (is_zero_addr(&srv_addrs[num_srv_addrs])) {
3084 DBG_DEBUG("SRV lookup %s got IP[%zu] %s\n",
3087 print_sockaddr(addr,
3089 &srv_addrs[num_srv_addrs]));
3095 /* Allocate the array of hostnames we must look up. */
3096 dns_lookup_names = talloc_zero_array(ctx,
3099 if (dns_lookup_names == NULL) {
3101 TALLOC_FREE(srv_addrs);
3102 return NT_STATUS_NO_MEMORY;
3106 for(i = 0; i < numdcs; i++) {
3107 if (dcs[i].hostname == NULL) {
3109 * Must have been a SRV return with an IP address.
3110 * We don't need to look up this hostname.
3114 dns_lookup_names[num_dns_names] = dcs[i].hostname;
3118 /* Lookup the addresses on the dns_lookup_list. */
3119 status = dns_lookup_list_async(ctx,
3126 if (!NT_STATUS_IS_OK(status)) {
3128 TALLOC_FREE(srv_addrs);
3129 TALLOC_FREE(dns_lookup_names);
3130 TALLOC_FREE(dns_addrs);
3135 * Combine the two sockaddr_storage arrays into a talloc'ed
3136 * struct sockaddr_storage array return.
3139 numaddrs = num_srv_addrs + num_dns_addrs;
3140 /* Wrap check + bloody int conversion check :-(. */
3141 if (numaddrs < num_srv_addrs ||
3144 TALLOC_FREE(srv_addrs);
3145 TALLOC_FREE(dns_addrs);
3146 TALLOC_FREE(dns_lookup_names);
3147 return NT_STATUS_INVALID_PARAMETER;
3150 if (numaddrs == 0) {
3151 /* Keep the same semantics for zero names. */
3153 TALLOC_FREE(srv_addrs);
3154 TALLOC_FREE(dns_addrs);
3155 TALLOC_FREE(dns_lookup_names);
3156 *return_addrs = NULL;
3158 return NT_STATUS_OK;
3161 ret_addrs = talloc_zero_array(ctx,
3162 struct sockaddr_storage,
3164 if (ret_addrs == NULL) {
3166 TALLOC_FREE(srv_addrs);
3167 TALLOC_FREE(dns_addrs);
3168 TALLOC_FREE(dns_lookup_names);
3169 return NT_STATUS_NO_MEMORY;
3172 for (i = 0; i < num_srv_addrs; i++) {
3173 ret_addrs[i] = srv_addrs[i];
3175 for (i = 0; i < num_dns_addrs; i++) {
3176 ret_addrs[num_srv_addrs+i] = dns_addrs[i].u.ss;
3180 TALLOC_FREE(srv_addrs);
3181 TALLOC_FREE(dns_addrs);
3182 TALLOC_FREE(dns_lookup_names);
3184 *return_addrs = ret_addrs;
3185 *return_count = numaddrs;
3186 return NT_STATUS_OK;
3189 static const char **filter_out_nbt_lookup(TALLOC_CTX *mem_ctx,
3190 const char **resolve_order)
3192 size_t i, len, result_idx;
3193 const char **result;
3196 while (resolve_order[len] != NULL) {
3200 result = talloc_array(mem_ctx, const char *, len+1);
3201 if (result == NULL) {
3207 for (i=0; i<len; i++) {
3208 const char *tok = resolve_order[i];
3210 if (strequal(tok, "lmhosts") || strequal(tok, "wins") ||
3211 strequal(tok, "bcast")) {
3214 result[result_idx++] = tok;
3216 result[result_idx] = NULL;
3221 /*******************************************************************
3222 Samba interface to resolve a name into an IP address.
3223 Use this function if the string is either an IP address, DNS
3224 or host name or NetBIOS name. This uses the name switch in the
3225 smb.conf to determine the order of name resolution.
3227 Added support for ip addr/port to support ADS ldap servers.
3228 the only place we currently care about the port is in the
3229 resolve_hosts() when looking up DC's via SRV RR entries in DNS
3230 **********************************************************************/
3232 NTSTATUS internal_resolve_name(TALLOC_CTX *ctx,
3235 const char *sitename,
3236 struct ip_service **return_iplist,
3237 size_t *return_count,
3238 const char **resolve_order)
3241 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
3243 size_t nc_count = 0;
3244 size_t ret_count = 0;
3246 * Integer count of addresses returned from resolve_XXX()
3247 * functions. This will go away when all of them return
3252 struct sockaddr_storage *ss_list = NULL;
3253 struct samba_sockaddr *sa_list = NULL;
3254 struct ip_service *iplist = NULL;
3255 TALLOC_CTX *frame = talloc_stackframe();
3257 *return_iplist = NULL;
3260 DBG_DEBUG("looking up %s#%x (sitename %s)\n",
3261 name, name_type, sitename ? sitename : "(null)");
3263 if (is_ipaddress(name)) {
3264 struct sockaddr_storage ss;
3266 /* if it's in the form of an IP address then get the lib to interpret it */
3267 ok = interpret_string_addr(&ss, name, AI_NUMERICHOST);
3269 DBG_WARNING("interpret_string_addr failed on %s\n",
3272 return NT_STATUS_INVALID_PARAMETER;
3274 if (is_zero_addr(&ss)) {
3276 return NT_STATUS_UNSUCCESSFUL;
3279 iplist = talloc_zero_array(frame,
3282 if (iplist == NULL) {
3284 return NT_STATUS_NO_MEMORY;
3288 /* ignore the port here */
3289 iplist[0].port = PORT_NONE;
3291 *return_iplist = talloc_move(ctx, &iplist);
3294 return NT_STATUS_OK;
3297 /* Check name cache */
3299 ok = namecache_fetch(frame,
3306 * Create a struct ip_service list from the
3307 * returned samba_sockaddrs.
3311 iplist = talloc_zero_array(frame,
3314 if (iplist == NULL) {
3316 return NT_STATUS_NO_MEMORY;
3319 for (i = 0; i < nc_count; i++) {
3320 if (is_zero_addr(&sa_list[i].u.ss)) {
3323 iplist[count].ss = sa_list[i].u.ss;
3324 iplist[count].port = 0;
3327 count = remove_duplicate_addrs2(iplist, count);
3329 TALLOC_FREE(iplist);
3331 return NT_STATUS_UNSUCCESSFUL;
3333 *return_count = count;
3334 *return_iplist = talloc_move(ctx, &iplist);
3336 return NT_STATUS_OK;
3339 /* set the name resolution order */
3341 if (resolve_order && strcmp(resolve_order[0], "NULL") == 0) {
3342 DBG_DEBUG("all lookups disabled\n");
3344 return NT_STATUS_INVALID_PARAMETER;
3347 if (!resolve_order || !resolve_order[0]) {
3348 static const char *host_order[] = { "host", NULL };
3349 resolve_order = host_order;
3352 if ((strlen(name) > MAX_NETBIOSNAME_LEN - 1) ||
3353 (strchr(name, '.') != NULL)) {
3355 * Don't do NBT lookup, the name would not fit anyway
3357 resolve_order = filter_out_nbt_lookup(frame, resolve_order);
3358 if (resolve_order == NULL) {
3360 return NT_STATUS_NO_MEMORY;
3364 /* iterate through the name resolution backends */
3366 for (i=0; resolve_order[i]; i++) {
3367 tok = resolve_order[i];
3369 if((strequal(tok, "host") || strequal(tok, "hosts"))) {
3370 status = resolve_hosts(talloc_tos(),
3375 if (!NT_STATUS_IS_OK(status)) {
3379 } else if(strequal( tok, "kdc")) {
3380 /* deal with KDC_NAME_TYPE names here.
3381 * This will result in a SRV record lookup */
3382 status = resolve_ads(talloc_tos(),
3388 if (!NT_STATUS_IS_OK(status)) {
3391 /* Ensure we don't namecache
3392 * this with the KDC port. */
3393 name_type = KDC_NAME_TYPE;
3395 } else if(strequal( tok, "ads")) {
3396 /* deal with 0x1c and 0x1b names here.
3397 * This will result in a SRV record lookup */
3398 status = resolve_ads(talloc_tos(),
3404 if (!NT_STATUS_IS_OK(status)) {
3408 } else if (strequal(tok, "lmhosts")) {
3410 status = resolve_lmhosts_file_as_sockaddr(
3412 get_dyn_LMHOSTSFILE(),
3417 if (!NT_STATUS_IS_OK(status)) {
3421 * This uglyness will go away once
3422 * all resolve_XXX() return size_t *
3423 * number of addresses.
3425 icount = (int)lmcount;
3427 } else if (strequal(tok, "wins")) {
3429 /* don't resolve 1D via WINS */
3430 if (name_type == 0x1D) {
3433 status = resolve_wins(talloc_tos(),
3438 if (!NT_STATUS_IS_OK(status)) {
3442 * This uglyness will go away once
3443 * all resolve_XXX() return size_t *
3444 * number of addresses.
3446 icount = (int)wcount;
3448 } else if (strequal(tok, "bcast")) {
3450 status = name_resolve_bcast(
3456 if (!NT_STATUS_IS_OK(status)) {
3460 * This uglyness will go away once
3461 * all resolve_XXX() return size_t *
3462 * number of addresses.
3464 icount = (int)bcount;
3467 DBG_ERR("unknown name switch type %s\n",
3472 /* All of the resolve_* functions above have returned false. */
3484 return NT_STATUS_INVALID_PARAMETER;
3488 * convert_ss2service() leaves the correct
3489 * count to return after removing zero addresses
3492 ok = convert_ss2service(frame,
3498 TALLOC_FREE(iplist);
3500 return NT_STATUS_NO_MEMORY;
3503 /* Remove duplicate entries. Some queries, notably #1c (domain
3504 controllers) return the PDC in iplist[0] and then all domain
3505 controllers including the PDC in iplist[1..n]. Iterating over
3506 the iplist when the PDC is down will cause two sets of timeouts. */
3508 ret_count = remove_duplicate_addrs2(iplist, ret_count);
3510 /* Save in name cache */
3511 if ( DEBUGLEVEL >= 100 ) {
3512 for (i = 0; i < ret_count && DEBUGLEVEL == 100; i++) {
3513 char addr[INET6_ADDRSTRLEN];
3514 print_sockaddr(addr, sizeof(addr),
3516 DEBUG(100, ("Storing name %s of type %d (%s:%d)\n",
3526 * Convert the ip_service list to a samba_sockaddr array
3527 * to store in the namecache. This conversion
3528 * will go away once ip_service is gone.
3530 struct samba_sockaddr *sa_converted_list = NULL;
3531 status = ip_service_to_samba_sockaddr(talloc_tos(),
3535 if (!NT_STATUS_IS_OK(status)) {
3536 TALLOC_FREE(iplist);
3540 namecache_store(name,
3544 TALLOC_FREE(sa_converted_list);
3547 /* Display some debugging info */
3549 if ( DEBUGLEVEL >= 10 ) {
3550 DBG_DEBUG("returning %zu addresses: ",
3553 for (i = 0; i < ret_count; i++) {
3554 char addr[INET6_ADDRSTRLEN];
3555 print_sockaddr(addr, sizeof(addr),
3557 DEBUGADD(10, ("%s:%d ",
3564 *return_count = ret_count;
3565 *return_iplist = talloc_move(ctx, &iplist);
3571 /********************************************************
3572 Internal interface to resolve a name into one IP address.
3573 Use this function if the string is either an IP address, DNS
3574 or host name or NetBIOS name. This uses the name switch in the
3575 smb.conf to determine the order of name resolution.
3576 *********************************************************/
3578 bool resolve_name(const char *name,
3579 struct sockaddr_storage *return_ss,
3583 struct ip_service *ss_list = NULL;
3584 char *sitename = NULL;
3587 TALLOC_CTX *frame = NULL;
3589 if (is_ipaddress(name)) {
3590 return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
3593 frame = talloc_stackframe();
3595 sitename = sitename_fetch(frame, lp_realm()); /* wild guess */
3597 status = internal_resolve_name(frame,
3603 lp_name_resolve_order());
3604 if (NT_STATUS_IS_OK(status)) {
3608 for (i=0; i<count; i++) {
3609 struct samba_sockaddr sa = {0};
3612 ok = sockaddr_storage_to_samba_sockaddr(&sa,
3615 TALLOC_FREE(ss_list);
3619 if (!is_broadcast_addr(&sa.u.sa) &&
3620 (sa.u.ss.ss_family == AF_INET)) {
3621 *return_ss = ss_list[i].ss;
3622 TALLOC_FREE(ss_list);
3629 /* only return valid addresses for TCP connections */
3630 for (i=0; i<count; i++) {
3631 struct samba_sockaddr sa = {0};
3634 ok = sockaddr_storage_to_samba_sockaddr(&sa,
3637 TALLOC_FREE(ss_list);
3641 if (!is_broadcast_addr(&sa.u.sa)) {
3642 *return_ss = ss_list[i].ss;
3643 TALLOC_FREE(ss_list);
3650 TALLOC_FREE(ss_list);
3655 /********************************************************
3656 Internal interface to resolve a name into a list of IP addresses.
3657 Use this function if the string is either an IP address, DNS
3658 or host name or NetBIOS name. This uses the name switch in the
3659 smb.conf to determine the order of name resolution.
3660 *********************************************************/
3662 NTSTATUS resolve_name_list(TALLOC_CTX *ctx,
3665 struct sockaddr_storage **return_ss_arr,
3666 unsigned int *p_num_entries)
3668 struct ip_service *ss_list = NULL;
3669 char *sitename = NULL;
3672 unsigned int num_entries = 0;
3673 struct sockaddr_storage *result_arr = NULL;
3676 if (is_ipaddress(name)) {
3677 result_arr = talloc(ctx, struct sockaddr_storage);
3678 if (result_arr == NULL) {
3679 return NT_STATUS_NO_MEMORY;
3681 if (!interpret_string_addr(result_arr, name, AI_NUMERICHOST)) {
3682 TALLOC_FREE(result_arr);
3683 return NT_STATUS_BAD_NETWORK_NAME;
3686 *return_ss_arr = result_arr;
3687 return NT_STATUS_OK;
3690 sitename = sitename_fetch(ctx, lp_realm()); /* wild guess */
3692 status = internal_resolve_name(ctx,
3698 lp_name_resolve_order());
3699 TALLOC_FREE(sitename);
3701 if (!NT_STATUS_IS_OK(status)) {
3705 /* only return valid addresses for TCP connections */
3706 for (i=0, num_entries = 0; i<count; i++) {
3707 struct samba_sockaddr sa = {0};
3710 ok = sockaddr_storage_to_samba_sockaddr(&sa,
3715 if (!is_zero_addr(&sa.u.ss) &&
3716 !is_broadcast_addr(&sa.u.sa)) {
3720 if (num_entries == 0) {
3721 status = NT_STATUS_BAD_NETWORK_NAME;
3725 result_arr = talloc_array(ctx,
3726 struct sockaddr_storage,
3728 if (result_arr == NULL) {
3729 status = NT_STATUS_NO_MEMORY;
3733 for (i=0, num_entries = 0; i<count; i++) {
3734 struct samba_sockaddr sa = {0};
3737 ok = sockaddr_storage_to_samba_sockaddr(&sa,
3742 if (!is_zero_addr(&sa.u.ss) &&
3743 !is_broadcast_addr(&sa.u.sa)) {
3744 result_arr[num_entries++] = ss_list[i].ss;
3748 if (num_entries == 0) {
3749 TALLOC_FREE(result_arr);
3750 status = NT_STATUS_BAD_NETWORK_NAME;
3754 status = NT_STATUS_OK;
3755 *p_num_entries = num_entries;
3756 *return_ss_arr = result_arr;
3758 TALLOC_FREE(ss_list);
3762 /********************************************************
3763 Find the IP address of the master browser or DMB for a workgroup.
3764 *********************************************************/
3766 bool find_master_ip(const char *group, struct sockaddr_storage *master_ss)
3768 struct ip_service *ip_list = NULL;
3772 if (lp_disable_netbios()) {
3773 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
3777 status = internal_resolve_name(talloc_tos(),
3783 lp_name_resolve_order());
3784 if (NT_STATUS_IS_OK(status)) {
3785 *master_ss = ip_list[0].ss;
3786 TALLOC_FREE(ip_list);
3790 TALLOC_FREE(ip_list);
3792 status = internal_resolve_name(talloc_tos(),
3798 lp_name_resolve_order());
3799 if (NT_STATUS_IS_OK(status)) {
3800 *master_ss = ip_list[0].ss;
3801 TALLOC_FREE(ip_list);
3805 TALLOC_FREE(ip_list);
3809 /********************************************************
3810 Get the IP address list of the primary domain controller
3812 *********************************************************/
3814 bool get_pdc_ip(const char *domain, struct sockaddr_storage *pss)
3816 struct ip_service *ip_list = NULL;
3818 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
3819 static const char *ads_order[] = { "ads", NULL };
3820 /* Look up #1B name */
3822 if (lp_security() == SEC_ADS) {
3823 status = internal_resolve_name(talloc_tos(),
3832 if (!NT_STATUS_IS_OK(status) || count == 0) {
3833 TALLOC_FREE(ip_list);
3834 status = internal_resolve_name(talloc_tos(),
3840 lp_name_resolve_order());
3841 if (!NT_STATUS_IS_OK(status)) {
3842 TALLOC_FREE(ip_list);
3847 /* if we get more than 1 IP back we have to assume it is a
3848 multi-homed PDC and not a mess up */
3851 DBG_INFO("PDC has %zu IP addresses!\n", count);
3852 sort_service_list(ip_list, count);
3855 *pss = ip_list[0].ss;
3856 TALLOC_FREE(ip_list);
3860 /* Private enum type for lookups. */
3862 enum dc_lookup_type { DC_NORMAL_LOOKUP, DC_ADS_ONLY, DC_KDC_ONLY };
3864 /********************************************************
3865 Get the IP address list of the domain controllers for
3867 *********************************************************/
3869 static NTSTATUS get_dc_list(TALLOC_CTX *ctx,
3871 const char *sitename,
3872 struct ip_service **ip_list,
3874 enum dc_lookup_type lookup_type,
3877 const char **resolve_order = NULL;
3878 char *saf_servername = NULL;
3879 char *pserver = NULL;
3881 char *port_str = NULL;
3884 size_t num_addresses = 0;
3885 size_t local_count = 0;
3887 struct ip_service *return_iplist = NULL;
3888 struct ip_service *auto_ip_list = NULL;
3889 bool done_auto_lookup = false;
3890 size_t auto_count = 0;
3892 TALLOC_CTX *frame = talloc_stackframe();
3893 int auto_name_type = 0x1C;
3897 /* if we are restricted to solely using DNS for looking
3898 up a domain controller, make sure that host lookups
3899 are enabled for the 'name resolve order'. If host lookups
3900 are disabled and ads_only is True, then set the string to
3903 resolve_order = lp_name_resolve_order();
3904 if (!resolve_order) {
3905 status = NT_STATUS_NO_MEMORY;
3908 if (lookup_type == DC_ADS_ONLY) {
3909 if (str_list_check_ci(resolve_order, "host")) {
3910 static const char *ads_order[] = { "ads", NULL };
3911 resolve_order = ads_order;
3913 /* DNS SRV lookups used by the ads resolver
3914 are already sorted by priority and weight */
3917 /* this is quite bizarre! */
3918 static const char *null_order[] = { "NULL", NULL };
3919 resolve_order = null_order;
3921 } else if (lookup_type == DC_KDC_ONLY) {
3922 static const char *kdc_order[] = { "kdc", NULL };
3923 /* DNS SRV lookups used by the ads/kdc resolver
3924 are already sorted by priority and weight */
3926 resolve_order = kdc_order;
3927 auto_name_type = KDC_NAME_TYPE;
3930 /* fetch the server we have affinity for. Add the
3931 'password server' list to a search for our domain controllers */
3933 saf_servername = saf_fetch(frame, domain);
3935 if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
3936 pserver = talloc_asprintf(frame, "%s, %s",
3937 saf_servername ? saf_servername : "",
3938 lp_password_server());
3940 pserver = talloc_asprintf(frame, "%s, *",
3941 saf_servername ? saf_servername : "");
3944 TALLOC_FREE(saf_servername);
3946 status = NT_STATUS_NO_MEMORY;
3950 DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver ));
3953 * if '*' appears in the "password server" list then add
3954 * an auto lookup to the list of manually configured
3955 * DC's. If any DC is listed by name, then the list should be
3956 * considered to be ordered
3960 while (next_token_talloc(frame, &p, &name, LIST_SEP)) {
3961 if (!done_auto_lookup && strequal(name, "*")) {
3962 done_auto_lookup = true;
3964 status = internal_resolve_name(frame,
3971 if (!NT_STATUS_IS_OK(status)) {
3975 if (num_addresses + auto_count < num_addresses) {
3976 TALLOC_FREE(auto_ip_list);
3977 status = NT_STATUS_INVALID_PARAMETER;
3980 num_addresses += auto_count;
3981 DBG_DEBUG("Adding %zu DC's from auto lookup\n",
3985 if (num_addresses + 1 < num_addresses) {
3986 TALLOC_FREE(auto_ip_list);
3987 status = NT_STATUS_INVALID_PARAMETER;
3994 /* if we have no addresses and haven't done the auto lookup, then
3995 just return the list of DC's. Or maybe we just failed. */
3997 if (num_addresses == 0) {
3998 struct ip_service *dc_iplist = NULL;
3999 size_t dc_count = 0;
4001 if (done_auto_lookup) {
4002 DEBUG(4,("get_dc_list: no servers found\n"));
4003 status = NT_STATUS_NO_LOGON_SERVERS;
4006 /* talloc off frame, only move to ctx on success. */
4007 status = internal_resolve_name(frame,
4014 if (!NT_STATUS_IS_OK(status)) {
4017 return_iplist = talloc_move(ctx, &dc_iplist);
4018 local_count = dc_count;
4022 return_iplist = talloc_zero_array(ctx,
4025 if (return_iplist == NULL) {
4026 DEBUG(3,("get_dc_list: malloc fail !\n"));
4027 status = NT_STATUS_NO_MEMORY;
4034 /* fill in the return list now with real IP's */
4036 while ((local_count<num_addresses) &&
4037 next_token_talloc(frame, &p, &name, LIST_SEP)) {
4038 struct samba_sockaddr name_sa = {0};
4040 /* copy any addresses from the auto lookup */
4042 if (strequal(name, "*")) {
4044 for (j=0; j<auto_count; j++) {
4045 char addr[INET6_ADDRSTRLEN];
4046 print_sockaddr(addr,
4048 &auto_ip_list[j].ss);
4049 /* Check for and don't copy any
4050 * known bad DC IP's. */
4051 if(!NT_STATUS_IS_OK(check_negative_conn_cache(
4054 DEBUG(5,("get_dc_list: "
4055 "negative entry %s removed "
4060 return_iplist[local_count].ss =
4062 return_iplist[local_count].port =
4063 auto_ip_list[j].port;
4069 /* added support for address:port syntax for ads
4070 * (not that I think anyone will ever run the LDAP
4071 * server in an AD domain on something other than
4073 * However, the port should not be used for kerberos
4076 port = (lookup_type == DC_ADS_ONLY) ? LDAP_PORT :
4077 ((lookup_type == DC_KDC_ONLY) ? DEFAULT_KRB5_PORT :
4079 if ((port_str=strchr(name, ':')) != NULL) {
4081 if (lookup_type != DC_KDC_ONLY) {
4083 port = atoi(port_str);
4087 /* explicit lookup; resolve_name() will
4088 * handle names & IP addresses */
4089 if (resolve_name(name, &name_sa.u.ss, 0x20, true)) {
4090 char addr[INET6_ADDRSTRLEN];
4094 * Ensure we set sa_socklen correctly.
4095 * Doesn't matter now, but eventually we
4096 * will remove ip_service and return samba_sockaddr
4099 ok = sockaddr_storage_to_samba_sockaddr(
4103 status = NT_STATUS_INVALID_ADDRESS;
4107 print_sockaddr(addr,
4111 /* Check for and don't copy any known bad DC IP's. */
4112 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain,
4114 DEBUG(5,("get_dc_list: negative entry %s "
4115 "removed from DC list\n",
4120 return_iplist[local_count].ss = name_sa.u.ss;
4121 return_iplist[local_count].port = port;
4127 /* need to remove duplicates in the list if we have any
4128 explicit password servers */
4130 local_count = remove_duplicate_addrs2(return_iplist, local_count );
4132 /* For DC's we always prioritize IPv4 due to W2K3 not
4133 * supporting LDAP, KRB5 or CLDAP over IPv6. */
4135 if (local_count && return_iplist) {
4136 prioritize_ipv4_list(return_iplist, local_count);
4139 if ( DEBUGLEVEL >= 4 ) {
4140 DEBUG(4,("get_dc_list: returning %zu ip addresses "
4141 "in an %sordered list\n",
4143 *ordered ? "":"un"));
4144 DEBUG(4,("get_dc_list: "));
4145 for ( i=0; i<local_count; i++ ) {
4146 char addr[INET6_ADDRSTRLEN];
4147 print_sockaddr(addr,
4149 &return_iplist[i].ss);
4150 DEBUGADD(4,("%s:%d ", addr, return_iplist[i].port ));
4155 status = (local_count != 0 ? NT_STATUS_OK : NT_STATUS_NO_LOGON_SERVERS);
4159 if (NT_STATUS_IS_OK(status)) {
4160 *ip_list = return_iplist;
4161 *ret_count = local_count;
4163 TALLOC_FREE(return_iplist);
4166 TALLOC_FREE(auto_ip_list);
4171 /*********************************************************************
4173 Small wrapper function to get the DC list and sort it if neccessary.
4174 *********************************************************************/
4176 NTSTATUS get_sorted_dc_list(TALLOC_CTX *ctx,
4178 const char *sitename,
4179 struct ip_service **ip_list_ret,
4183 bool ordered = false;
4185 enum dc_lookup_type lookup_type = DC_NORMAL_LOOKUP;
4186 struct ip_service *ip_list = NULL;
4189 DBG_INFO("attempting lookup for name %s (sitename %s)\n",
4191 sitename ? sitename : "NULL");
4194 lookup_type = DC_ADS_ONLY;
4197 status = get_dc_list(ctx,
4204 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_LOGON_SERVERS)
4206 DBG_NOTICE("no server for name %s available"
4207 " in site %s, fallback to all servers\n",
4210 status = get_dc_list(ctx,
4219 if (!NT_STATUS_IS_OK(status)) {
4223 /* only sort if we don't already have an ordered list */
4225 sort_service_list(ip_list, count);
4229 *ip_list_ret = ip_list;
4233 /*********************************************************************
4235 Get the KDC list - re-use all the logic in get_dc_list.
4236 *********************************************************************/
4238 NTSTATUS get_kdc_list(TALLOC_CTX *ctx,
4240 const char *sitename,
4241 struct ip_service **ip_list_ret,
4245 struct ip_service *ip_list = NULL;
4246 bool ordered = false;
4249 status = get_dc_list(ctx,
4257 if (!NT_STATUS_IS_OK(status)) {
4261 /* only sort if we don't already have an ordered list */
4263 sort_service_list(ip_list, count);
4267 *ip_list_ret = ip_list;