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 a MALLOC'ed struct ip_serivce array
69 * to a talloc'ed one. This function will go away once all ip_service
70 * arrays are talloc'ed.
73 static NTSTATUS dup_ip_service_array(TALLOC_CTX *ctx,
74 struct ip_service **array_out,
75 const struct ip_service *array_in,
78 struct ip_service *array_copy = NULL;
80 array_copy = talloc_memdup(ctx,
82 count * sizeof(array_in[0]));
83 if (array_copy == NULL) {
84 return NT_STATUS_NO_MEMORY;
86 *array_out = array_copy;
90 /****************************
91 * SERVER AFFINITY ROUTINES *
92 ****************************/
94 /* Server affinity is the concept of preferring the last domain
95 controller with whom you had a successful conversation */
97 /****************************************************************************
98 ****************************************************************************/
99 #define SAFKEY_FMT "SAF/DOMAIN/%s"
101 #define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
102 #define SAFJOIN_TTL 3600
104 static char *saf_key(TALLOC_CTX *mem_ctx, const char *domain)
106 return talloc_asprintf_strupper_m(mem_ctx, SAFKEY_FMT, domain);
109 static char *saf_join_key(TALLOC_CTX *mem_ctx, const char *domain)
111 return talloc_asprintf_strupper_m(mem_ctx, SAFJOINKEY_FMT, domain);
114 /****************************************************************************
115 ****************************************************************************/
117 bool saf_store( const char *domain, const char *servername )
123 if ( !domain || !servername ) {
124 DEBUG(2,("saf_store: "
125 "Refusing to store empty domain or servername!\n"));
129 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
130 DEBUG(0,("saf_store: "
131 "refusing to store 0 length domain or servername!\n"));
135 key = saf_key(talloc_tos(), domain);
137 DEBUG(1, ("saf_key() failed\n"));
140 expire = time( NULL ) + lp_parm_int(-1, "saf","ttl", SAF_TTL);
142 DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
143 domain, servername, (unsigned int)expire ));
145 ret = gencache_set( key, servername, expire );
152 bool saf_join_store( const char *domain, const char *servername )
158 if ( !domain || !servername ) {
159 DEBUG(2,("saf_join_store: Refusing to store empty domain or servername!\n"));
163 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
164 DEBUG(0,("saf_join_store: refusing to store 0 length domain or servername!\n"));
168 key = saf_join_key(talloc_tos(), domain);
170 DEBUG(1, ("saf_join_key() failed\n"));
173 expire = time( NULL ) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL);
175 DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
176 domain, servername, (unsigned int)expire ));
178 ret = gencache_set( key, servername, expire );
185 bool saf_delete( const char *domain )
191 DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
195 key = saf_join_key(talloc_tos(), domain);
197 DEBUG(1, ("saf_join_key() failed\n"));
200 ret = gencache_del(key);
204 DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain ));
207 key = saf_key(talloc_tos(), domain);
209 DEBUG(1, ("saf_key() failed\n"));
212 ret = gencache_del(key);
216 DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
222 /****************************************************************************
223 ****************************************************************************/
225 char *saf_fetch(TALLOC_CTX *mem_ctx, const char *domain )
232 if ( !domain || strlen(domain) == 0) {
233 DEBUG(2,("saf_fetch: Empty domain name!\n"));
237 key = saf_join_key(talloc_tos(), domain);
239 DEBUG(1, ("saf_join_key() failed\n"));
243 ret = gencache_get( key, mem_ctx, &server, &timeout );
248 DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
253 key = saf_key(talloc_tos(), domain);
255 DEBUG(1, ("saf_key() failed\n"));
259 ret = gencache_get( key, mem_ctx, &server, &timeout );
264 DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
267 DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
274 static void set_socket_addr_v4(struct samba_sockaddr *addr)
276 if (!interpret_string_addr(&addr->u.ss, lp_nbt_client_socket_address(),
277 AI_NUMERICHOST|AI_PASSIVE)) {
278 zero_sockaddr(&addr->u.ss);
279 /* zero_sockaddr sets family to AF_INET. */
280 addr->sa_socklen = sizeof(struct sockaddr_in);
282 if (addr->u.ss.ss_family != AF_INET) {
283 zero_sockaddr(&addr->u.ss);
284 /* zero_sockaddr sets family to AF_INET. */
285 addr->sa_socklen = sizeof(struct sockaddr_in);
289 static struct in_addr my_socket_addr_v4(void)
291 struct samba_sockaddr my_addr = {0};
293 set_socket_addr_v4(&my_addr);
294 return my_addr.u.in.sin_addr;
297 /****************************************************************************
298 Generate a random trn_id.
299 ****************************************************************************/
301 static int generate_trn_id(void)
305 generate_random_buffer((uint8_t *)&id, sizeof(id));
307 return id % (unsigned)0x7FFF;
310 /****************************************************************************
311 Parse a node status response into an array of structures.
312 ****************************************************************************/
314 static struct node_status *parse_node_status(TALLOC_CTX *mem_ctx, char *p,
316 struct node_status_extra *extra)
318 struct node_status *ret;
321 *num_names = CVAL(p,0);
326 ret = talloc_array(mem_ctx, struct node_status,*num_names);
331 for (i=0;i< *num_names;i++) {
332 strlcpy(ret[i].name,p,16);
333 trim_char(ret[i].name,'\0',' ');
334 ret[i].type = CVAL(p,15);
335 ret[i].flags = p[16];
337 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
338 ret[i].type, ret[i].flags));
341 * Also, pick up the MAC address ...
344 memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
349 struct sock_packet_read_state {
350 struct tevent_context *ev;
351 enum packet_type type;
354 struct nb_packet_reader *reader;
355 struct tevent_req *reader_req;
357 struct tdgram_context *sock;
358 struct tevent_req *socket_req;
360 struct tsocket_address *addr;
362 bool (*validator)(struct packet_struct *p,
366 struct packet_struct *packet;
369 static void sock_packet_read_got_packet(struct tevent_req *subreq);
370 static void sock_packet_read_got_socket(struct tevent_req *subreq);
372 static struct tevent_req *sock_packet_read_send(
374 struct tevent_context *ev,
375 struct tdgram_context *sock,
376 struct nb_packet_reader *reader,
377 enum packet_type type,
379 bool (*validator)(struct packet_struct *p, void *private_data),
382 struct tevent_req *req;
383 struct sock_packet_read_state *state;
385 req = tevent_req_create(mem_ctx, &state,
386 struct sock_packet_read_state);
391 state->reader = reader;
394 state->trn_id = trn_id;
395 state->validator = validator;
396 state->private_data = private_data;
398 if (reader != NULL) {
399 state->reader_req = nb_packet_read_send(state, ev, reader);
400 if (tevent_req_nomem(state->reader_req, req)) {
401 return tevent_req_post(req, ev);
403 tevent_req_set_callback(
404 state->reader_req, sock_packet_read_got_packet, req);
407 state->socket_req = tdgram_recvfrom_send(state, ev, state->sock);
408 if (tevent_req_nomem(state->socket_req, req)) {
409 return tevent_req_post(req, ev);
411 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
417 static void sock_packet_read_got_packet(struct tevent_req *subreq)
419 struct tevent_req *req = tevent_req_callback_data(
420 subreq, struct tevent_req);
421 struct sock_packet_read_state *state = tevent_req_data(
422 req, struct sock_packet_read_state);
425 status = nb_packet_read_recv(subreq, state, &state->packet);
427 TALLOC_FREE(state->reader_req);
429 if (!NT_STATUS_IS_OK(status)) {
430 if (state->socket_req != NULL) {
432 * Still waiting for socket
437 * Both socket and packet reader failed
439 tevent_req_nterror(req, status);
443 if ((state->validator != NULL) &&
444 !state->validator(state->packet, state->private_data)) {
445 DEBUG(10, ("validator failed\n"));
447 TALLOC_FREE(state->packet);
449 state->reader_req = nb_packet_read_send(state, state->ev,
451 if (tevent_req_nomem(state->reader_req, req)) {
454 tevent_req_set_callback(
455 state->reader_req, sock_packet_read_got_packet, req);
459 TALLOC_FREE(state->socket_req);
460 tevent_req_done(req);
463 static void sock_packet_read_got_socket(struct tevent_req *subreq)
465 struct tevent_req *req = tevent_req_callback_data(
466 subreq, struct tevent_req);
467 struct sock_packet_read_state *state = tevent_req_data(
468 req, struct sock_packet_read_state);
469 struct samba_sockaddr addr = {0};
475 received = tdgram_recvfrom_recv(subreq, &err, state,
476 &state->buf, &state->addr);
478 TALLOC_FREE(state->socket_req);
480 if (received == -1) {
481 if (state->reader_req != NULL) {
483 * Still waiting for reader
488 * Both socket and reader failed
490 tevent_req_nterror(req, map_nt_error_from_unix(err));
493 ok = tsocket_address_is_inet(state->addr, "ipv4");
497 ret = tsocket_address_bsd_sockaddr(state->addr,
501 tevent_req_nterror(req, map_nt_error_from_unix(errno));
505 state->packet = parse_packet_talloc(
506 state, (char *)state->buf, received, state->type,
507 addr.u.in.sin_addr, addr.u.in.sin_port);
508 if (state->packet == NULL) {
509 DEBUG(10, ("parse_packet failed\n"));
512 if ((state->trn_id != -1) &&
513 (state->trn_id != packet_trn_id(state->packet))) {
514 DEBUG(10, ("Expected transaction id %d, got %d\n",
515 state->trn_id, packet_trn_id(state->packet)));
519 if ((state->validator != NULL) &&
520 !state->validator(state->packet, state->private_data)) {
521 DEBUG(10, ("validator failed\n"));
525 tevent_req_done(req);
529 TALLOC_FREE(state->packet);
530 TALLOC_FREE(state->buf);
531 TALLOC_FREE(state->addr);
533 state->socket_req = tdgram_recvfrom_send(state, state->ev, state->sock);
534 if (tevent_req_nomem(state->socket_req, req)) {
537 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
541 static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
543 struct packet_struct **ppacket)
545 struct sock_packet_read_state *state = tevent_req_data(
546 req, struct sock_packet_read_state);
549 if (tevent_req_is_nterror(req, &status)) {
552 *ppacket = talloc_move(mem_ctx, &state->packet);
556 struct nb_trans_state {
557 struct tevent_context *ev;
558 struct tdgram_context *sock;
559 struct nb_packet_reader *reader;
561 struct tsocket_address *src_addr;
562 struct tsocket_address *dst_addr;
565 enum packet_type type;
568 bool (*validator)(struct packet_struct *p,
572 struct packet_struct *packet;
575 static void nb_trans_got_reader(struct tevent_req *subreq);
576 static void nb_trans_done(struct tevent_req *subreq);
577 static void nb_trans_sent(struct tevent_req *subreq);
578 static void nb_trans_send_next(struct tevent_req *subreq);
580 static struct tevent_req *nb_trans_send(
582 struct tevent_context *ev,
583 const struct samba_sockaddr *_my_addr,
584 const struct samba_sockaddr *_dst_addr,
586 uint8_t *buf, size_t buflen,
587 enum packet_type type, int trn_id,
588 bool (*validator)(struct packet_struct *p,
592 const struct sockaddr *my_addr = &_my_addr->u.sa;
593 size_t my_addr_len = sizeof(_my_addr->u.in); /*We know it's AF_INET.*/
594 const struct sockaddr *dst_addr = &_dst_addr->u.sa;
595 size_t dst_addr_len = sizeof(_dst_addr->u.in); /*We know it's AF_INET.*/
596 struct tevent_req *req, *subreq;
597 struct nb_trans_state *state;
600 req = tevent_req_create(mem_ctx, &state, struct nb_trans_state);
606 state->buflen = buflen;
608 state->trn_id = trn_id;
609 state->validator = validator;
610 state->private_data = private_data;
612 ret = tsocket_address_bsd_from_sockaddr(state,
613 my_addr, my_addr_len,
616 tevent_req_nterror(req, map_nt_error_from_unix(errno));
617 return tevent_req_post(req, ev);
620 ret = tsocket_address_bsd_from_sockaddr(state,
621 dst_addr, dst_addr_len,
624 tevent_req_nterror(req, map_nt_error_from_unix(errno));
625 return tevent_req_post(req, ev);
628 ret = tdgram_inet_udp_broadcast_socket(state->src_addr, state,
631 tevent_req_nterror(req, map_nt_error_from_unix(errno));
632 return tevent_req_post(req, ev);
635 subreq = nb_packet_reader_send(state, ev, type, state->trn_id, NULL);
636 if (tevent_req_nomem(subreq, req)) {
637 return tevent_req_post(req, ev);
639 tevent_req_set_callback(subreq, nb_trans_got_reader, req);
643 static void nb_trans_got_reader(struct tevent_req *subreq)
645 struct tevent_req *req = tevent_req_callback_data(
646 subreq, struct tevent_req);
647 struct nb_trans_state *state = tevent_req_data(
648 req, struct nb_trans_state);
651 status = nb_packet_reader_recv(subreq, state, &state->reader);
654 if (!NT_STATUS_IS_OK(status)) {
655 DEBUG(10, ("nmbd not around\n"));
656 state->reader = NULL;
659 subreq = sock_packet_read_send(
660 state, state->ev, state->sock,
661 state->reader, state->type, state->trn_id,
662 state->validator, state->private_data);
663 if (tevent_req_nomem(subreq, req)) {
666 tevent_req_set_callback(subreq, nb_trans_done, req);
668 subreq = tdgram_sendto_send(state, state->ev,
670 state->buf, state->buflen,
672 if (tevent_req_nomem(subreq, req)) {
675 tevent_req_set_callback(subreq, nb_trans_sent, req);
678 static void nb_trans_sent(struct tevent_req *subreq)
680 struct tevent_req *req = tevent_req_callback_data(
681 subreq, struct tevent_req);
682 struct nb_trans_state *state = tevent_req_data(
683 req, struct nb_trans_state);
687 sent = tdgram_sendto_recv(subreq, &err);
690 DEBUG(10, ("sendto failed: %s\n", strerror(err)));
691 tevent_req_nterror(req, map_nt_error_from_unix(err));
694 subreq = tevent_wakeup_send(state, state->ev,
695 timeval_current_ofs(1, 0));
696 if (tevent_req_nomem(subreq, req)) {
699 tevent_req_set_callback(subreq, nb_trans_send_next, req);
702 static void nb_trans_send_next(struct tevent_req *subreq)
704 struct tevent_req *req = tevent_req_callback_data(
705 subreq, struct tevent_req);
706 struct nb_trans_state *state = tevent_req_data(
707 req, struct nb_trans_state);
710 ret = tevent_wakeup_recv(subreq);
713 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
716 subreq = tdgram_sendto_send(state, state->ev,
718 state->buf, state->buflen,
720 if (tevent_req_nomem(subreq, req)) {
723 tevent_req_set_callback(subreq, nb_trans_sent, req);
726 static void nb_trans_done(struct tevent_req *subreq)
728 struct tevent_req *req = tevent_req_callback_data(
729 subreq, struct tevent_req);
730 struct nb_trans_state *state = tevent_req_data(
731 req, struct nb_trans_state);
734 status = sock_packet_read_recv(subreq, state, &state->packet);
736 if (tevent_req_nterror(req, status)) {
739 tevent_req_done(req);
742 static NTSTATUS nb_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
743 struct packet_struct **ppacket)
745 struct nb_trans_state *state = tevent_req_data(
746 req, struct nb_trans_state);
749 if (tevent_req_is_nterror(req, &status)) {
752 *ppacket = talloc_move(mem_ctx, &state->packet);
756 /****************************************************************************
757 Do a NBT node status query on an open socket and return an array of
758 structures holding the returned names or NULL if the query failed.
759 **************************************************************************/
761 struct node_status_query_state {
762 struct samba_sockaddr my_addr;
763 struct samba_sockaddr addr;
766 struct packet_struct *packet;
769 static bool node_status_query_validator(struct packet_struct *p,
771 static void node_status_query_done(struct tevent_req *subreq);
773 struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
774 struct tevent_context *ev,
775 struct nmb_name *name,
776 const struct sockaddr_storage *addr)
778 struct tevent_req *req, *subreq;
779 struct node_status_query_state *state;
780 struct packet_struct p;
781 struct nmb_packet *nmb = &p.packet.nmb;
784 req = tevent_req_create(mem_ctx, &state,
785 struct node_status_query_state);
790 if (addr->ss_family != AF_INET) {
791 /* Can't do node status to IPv6 */
792 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
793 return tevent_req_post(req, ev);
796 ok = sockaddr_storage_to_samba_sockaddr(&state->addr, addr);
798 /* node status must be IPv4 */
799 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
800 return tevent_req_post(req, ev);
802 state->addr.u.in.sin_port = htons(NMB_PORT);
804 set_socket_addr_v4(&state->my_addr);
807 nmb->header.name_trn_id = generate_trn_id();
808 nmb->header.opcode = 0;
809 nmb->header.response = false;
810 nmb->header.nm_flags.bcast = false;
811 nmb->header.nm_flags.recursion_available = false;
812 nmb->header.nm_flags.recursion_desired = false;
813 nmb->header.nm_flags.trunc = false;
814 nmb->header.nm_flags.authoritative = false;
815 nmb->header.rcode = 0;
816 nmb->header.qdcount = 1;
817 nmb->header.ancount = 0;
818 nmb->header.nscount = 0;
819 nmb->header.arcount = 0;
820 nmb->question.question_name = *name;
821 nmb->question.question_type = 0x21;
822 nmb->question.question_class = 0x1;
824 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
826 if (state->buflen == 0) {
827 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
828 DEBUG(10, ("build_packet failed\n"));
829 return tevent_req_post(req, ev);
832 subreq = nb_trans_send(state,
840 nmb->header.name_trn_id,
841 node_status_query_validator,
843 if (tevent_req_nomem(subreq, req)) {
844 DEBUG(10, ("nb_trans_send failed\n"));
845 return tevent_req_post(req, ev);
847 if (!tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0))) {
848 return tevent_req_post(req, ev);
850 tevent_req_set_callback(subreq, node_status_query_done, req);
854 static bool node_status_query_validator(struct packet_struct *p,
857 struct nmb_packet *nmb = &p->packet.nmb;
860 if (nmb->header.opcode != 0 ||
861 nmb->header.nm_flags.bcast ||
863 !nmb->header.ancount ||
864 nmb->answers->rr_type != 0x21) {
866 * XXXX what do we do with this? could be a redirect,
867 * but we'll discard it for the moment
874 static void node_status_query_done(struct tevent_req *subreq)
876 struct tevent_req *req = tevent_req_callback_data(
877 subreq, struct tevent_req);
878 struct node_status_query_state *state = tevent_req_data(
879 req, struct node_status_query_state);
882 status = nb_trans_recv(subreq, state, &state->packet);
884 if (tevent_req_nterror(req, status)) {
887 tevent_req_done(req);
890 NTSTATUS node_status_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
891 struct node_status **pnode_status,
893 struct node_status_extra *extra)
895 struct node_status_query_state *state = tevent_req_data(
896 req, struct node_status_query_state);
897 struct node_status *node_status;
901 if (tevent_req_is_nterror(req, &status)) {
904 node_status = parse_node_status(
905 mem_ctx, &state->packet->packet.nmb.answers->rdata[0],
907 if (node_status == NULL) {
908 return NT_STATUS_NO_MEMORY;
910 *pnode_status = node_status;
911 *pnum_names = num_names;
915 NTSTATUS node_status_query(TALLOC_CTX *mem_ctx, struct nmb_name *name,
916 const struct sockaddr_storage *addr,
917 struct node_status **pnode_status,
919 struct node_status_extra *extra)
921 TALLOC_CTX *frame = talloc_stackframe();
922 struct tevent_context *ev;
923 struct tevent_req *req;
924 NTSTATUS status = NT_STATUS_NO_MEMORY;
926 ev = samba_tevent_context_init(frame);
930 req = node_status_query_send(ev, ev, name, addr);
934 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
937 status = node_status_query_recv(req, mem_ctx, pnode_status,
944 static bool name_status_lmhosts(const struct sockaddr_storage *paddr,
945 int qname_type, fstring pname)
950 struct samba_sockaddr addr_in = {0};
951 struct samba_sockaddr addr = {0};
954 ok = sockaddr_storage_to_samba_sockaddr(&addr_in, paddr);
958 if (addr_in.u.ss.ss_family != AF_INET) {
962 f = startlmhosts(get_dyn_LMHOSTSFILE());
967 while (getlmhostsent(talloc_tos(), f, &name, &name_type, &addr.u.ss)) {
968 if (addr.u.ss.ss_family != AF_INET) {
971 if (name_type != qname_type) {
974 if (sockaddr_equal(&addr_in.u.sa, &addr.u.sa)) {
975 fstrcpy(pname, name);
984 /****************************************************************************
985 Find the first type XX name in a node status reply - used for finding
986 a servers name given its IP. Return the matched name in *name.
987 **************************************************************************/
989 bool name_status_find(const char *q_name,
992 const struct sockaddr_storage *to_ss,
995 char addr[INET6_ADDRSTRLEN];
996 struct node_status *addrs = NULL;
997 struct nmb_name nname;
1002 if (lp_disable_netbios()) {
1003 DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n",
1008 print_sockaddr(addr, sizeof(addr), to_ss);
1010 DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
1013 /* Check the cache first. */
1015 if (namecache_status_fetch(q_name, q_type, type, to_ss, name)) {
1019 if (to_ss->ss_family != AF_INET) {
1020 /* Can't do node status to IPv6 */
1024 result = name_status_lmhosts(to_ss, type, name);
1026 DBG_DEBUG("Found name %s in lmhosts\n", name);
1027 namecache_status_store(q_name, q_type, type, to_ss, name);
1031 /* W2K PDC's seem not to respond to '*'#0. JRA */
1032 make_nmb_name(&nname, q_name, q_type);
1033 status = node_status_query(talloc_tos(), &nname, to_ss,
1034 &addrs, &count, NULL);
1035 if (!NT_STATUS_IS_OK(status)) {
1039 for (i=0;i<count;i++) {
1040 /* Find first one of the requested type that's not a GROUP. */
1041 if (addrs[i].type == type && ! (addrs[i].flags & 0x80))
1047 pull_ascii_nstring(name, sizeof(fstring), addrs[i].name);
1049 /* Store the result in the cache. */
1050 /* but don't store an entry for 0x1c names here. Here we have
1051 a single host and DOMAIN<0x1c> names should be a list of hosts */
1053 if ( q_type != 0x1c ) {
1054 namecache_status_store(q_name, q_type, type, to_ss, name);
1062 DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
1065 DEBUGADD(10, (", name %s ip address is %s", name, addr));
1073 comparison function used by sort_addr_list
1076 static int addr_compare(const struct sockaddr_storage *ss1,
1077 const struct sockaddr_storage *ss2)
1079 int max_bits1=0, max_bits2=0;
1080 int num_interfaces = iface_count();
1082 struct samba_sockaddr sa1;
1083 struct samba_sockaddr sa2;
1086 ok = sockaddr_storage_to_samba_sockaddr(&sa1, ss1);
1088 return 0; /* No change. */
1091 ok = sockaddr_storage_to_samba_sockaddr(&sa2, ss2);
1093 return 0; /* No change. */
1096 /* Sort IPv4 addresses first. */
1097 if (sa1.u.ss.ss_family != sa2.u.ss.ss_family) {
1098 if (sa2.u.ss.ss_family == AF_INET) {
1105 /* Here we know both addresses are of the same
1108 for (i=0;i<num_interfaces;i++) {
1109 struct samba_sockaddr sif = {0};
1110 const unsigned char *p_ss1 = NULL;
1111 const unsigned char *p_ss2 = NULL;
1112 const unsigned char *p_if = NULL;
1116 ok = sockaddr_storage_to_samba_sockaddr(&sif, iface_n_bcast(i));
1118 return 0; /* No change. */
1120 if (sif.u.ss.ss_family != sa1.u.ss.ss_family) {
1121 /* Ignore interfaces of the wrong type. */
1124 if (sif.u.ss.ss_family == AF_INET) {
1125 p_if = (const unsigned char *)&sif.u.in.sin_addr;
1126 p_ss1 = (const unsigned char *)&sa1.u.in.sin_addr;
1127 p_ss2 = (const unsigned char *)&sa2.u.in.sin_addr;
1130 #if defined(HAVE_IPV6)
1131 if (sif.u.ss.ss_family == AF_INET6) {
1132 p_if = (const unsigned char *)&sif.u.in6.sin6_addr;
1133 p_ss1 = (const unsigned char *)&sa1.u.in6.sin6_addr;
1134 p_ss2 = (const unsigned char *)&sa2.u.in6.sin6_addr;
1138 if (!p_ss1 || !p_ss2 || !p_if || len == 0) {
1141 bits1 = matching_len_bits(p_ss1, p_if, len);
1142 bits2 = matching_len_bits(p_ss2, p_if, len);
1143 max_bits1 = MAX(bits1, max_bits1);
1144 max_bits2 = MAX(bits2, max_bits2);
1147 /* Bias towards directly reachable IPs */
1148 if (iface_local(&sa1.u.sa)) {
1149 if (sa1.u.ss.ss_family == AF_INET) {
1155 if (iface_local(&sa2.u.sa)) {
1156 if (sa2.u.ss.ss_family == AF_INET) {
1162 return max_bits2 - max_bits1;
1165 /*******************************************************************
1166 compare 2 ldap IPs by nearness to our interfaces - used in qsort
1167 *******************************************************************/
1169 static int ip_service_compare(struct ip_service *ss1, struct ip_service *ss2)
1173 if ((result = addr_compare(&ss1->ss, &ss2->ss)) != 0) {
1177 if (ss1->port > ss2->port) {
1181 if (ss1->port < ss2->port) {
1189 sort an IP list so that names that are close to one of our interfaces
1190 are at the top. This prevents the problem where a WINS server returns an IP
1191 that is not reachable from our subnet as the first match
1194 static void sort_addr_list(struct sockaddr_storage *sslist, int count)
1200 TYPESAFE_QSORT(sslist, count, addr_compare);
1203 static void sort_service_list(struct ip_service *servlist, int count)
1209 TYPESAFE_QSORT(servlist, count, ip_service_compare);
1212 /**********************************************************************
1213 Remove any duplicate address/port pairs in the list
1214 *********************************************************************/
1216 int remove_duplicate_addrs2(struct ip_service *iplist, int count )
1220 DEBUG(10,("remove_duplicate_addrs2: "
1221 "looking for duplicate address/port pairs\n"));
1223 /* One loop to set duplicates to a zero addr. */
1224 for ( i=0; i<count; i++ ) {
1226 struct samba_sockaddr sa_i = {0};
1228 ok = sockaddr_storage_to_samba_sockaddr(&sa_i, &iplist[i].ss);
1233 if (is_zero_addr(&sa_i.u.ss)) {
1237 for ( j=i+1; j<count; j++ ) {
1238 struct samba_sockaddr sa_j = {0};
1240 ok = sockaddr_storage_to_samba_sockaddr(&sa_j,
1246 if (sockaddr_equal(&sa_i.u.sa, &sa_j.u.sa) &&
1247 iplist[i].port == iplist[j].port) {
1248 zero_sockaddr(&iplist[j].ss);
1253 /* Now remove any addresses set to zero above. */
1254 for (i = 0; i < count; i++) {
1256 is_zero_addr(&iplist[i].ss)) {
1257 ARRAY_DEL_ELEMENT(iplist, i, count);
1265 static bool prioritize_ipv4_list(struct ip_service *iplist, int count)
1267 TALLOC_CTX *frame = talloc_stackframe();
1268 struct ip_service *iplist_new = talloc_array(frame, struct ip_service, count);
1271 if (iplist_new == NULL) {
1278 /* Copy IPv4 first. */
1279 for (i = 0; i < count; i++) {
1280 if (iplist[i].ss.ss_family == AF_INET) {
1281 iplist_new[j++] = iplist[i];
1286 for (i = 0; i < count; i++) {
1287 if (iplist[i].ss.ss_family != AF_INET) {
1288 iplist_new[j++] = iplist[i];
1292 memcpy(iplist, iplist_new, sizeof(struct ip_service)*count);
1297 /****************************************************************************
1298 Do a netbios name query to find someones IP.
1299 Returns an array of IP addresses or NULL if none.
1300 *count will be set to the number of addresses returned.
1301 *timed_out is set if we failed by timing out
1302 ****************************************************************************/
1304 struct name_query_state {
1305 struct samba_sockaddr my_addr;
1306 struct samba_sockaddr addr;
1308 bool bcast_star_query;
1314 NTSTATUS validate_error;
1317 struct sockaddr_storage *addrs;
1321 static bool name_query_validator(struct packet_struct *p, void *private_data);
1322 static void name_query_done(struct tevent_req *subreq);
1324 struct tevent_req *name_query_send(TALLOC_CTX *mem_ctx,
1325 struct tevent_context *ev,
1326 const char *name, int name_type,
1327 bool bcast, bool recurse,
1328 const struct sockaddr_storage *addr)
1330 struct tevent_req *req, *subreq;
1331 struct name_query_state *state;
1332 struct packet_struct p;
1333 struct nmb_packet *nmb = &p.packet.nmb;
1336 req = tevent_req_create(mem_ctx, &state, struct name_query_state);
1340 state->bcast = bcast;
1342 if (addr->ss_family != AF_INET) {
1343 /* Can't do node status to IPv6 */
1344 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1345 return tevent_req_post(req, ev);
1348 if (lp_disable_netbios()) {
1349 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
1351 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
1352 return tevent_req_post(req, ev);
1355 ok = sockaddr_storage_to_samba_sockaddr(&state->addr, addr);
1357 /* Node status must be IPv4 */
1358 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1359 return tevent_req_post(req, ev);
1361 state->addr.u.in.sin_port = htons(NMB_PORT);
1363 set_socket_addr_v4(&state->my_addr);
1366 nmb->header.name_trn_id = generate_trn_id();
1367 nmb->header.opcode = 0;
1368 nmb->header.response = false;
1369 nmb->header.nm_flags.bcast = bcast;
1370 nmb->header.nm_flags.recursion_available = false;
1371 nmb->header.nm_flags.recursion_desired = recurse;
1372 nmb->header.nm_flags.trunc = false;
1373 nmb->header.nm_flags.authoritative = false;
1374 nmb->header.rcode = 0;
1375 nmb->header.qdcount = 1;
1376 nmb->header.ancount = 0;
1377 nmb->header.nscount = 0;
1378 nmb->header.arcount = 0;
1380 if (bcast && (strcmp(name, "*")==0)) {
1382 * We're doing a broadcast query for all
1383 * names in the area. Remember this so
1384 * we will wait for all names within
1385 * the timeout period.
1387 state->bcast_star_query = true;
1390 make_nmb_name(&nmb->question.question_name,name,name_type);
1392 nmb->question.question_type = 0x20;
1393 nmb->question.question_class = 0x1;
1395 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
1397 if (state->buflen == 0) {
1398 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1399 DEBUG(10, ("build_packet failed\n"));
1400 return tevent_req_post(req, ev);
1403 subreq = nb_trans_send(state,
1411 nmb->header.name_trn_id,
1412 name_query_validator,
1414 if (tevent_req_nomem(subreq, req)) {
1415 DEBUG(10, ("nb_trans_send failed\n"));
1416 return tevent_req_post(req, ev);
1418 tevent_req_set_callback(subreq, name_query_done, req);
1422 static bool name_query_validator(struct packet_struct *p, void *private_data)
1424 struct name_query_state *state = talloc_get_type_abort(
1425 private_data, struct name_query_state);
1426 struct nmb_packet *nmb = &p->packet.nmb;
1427 struct sockaddr_storage *tmp_addrs;
1428 bool got_unique_netbios_name = false;
1431 debug_nmb_packet(p);
1434 * If we get a Negative Name Query Response from a WINS
1435 * server, we should report it and give up.
1437 if( 0 == nmb->header.opcode /* A query response */
1438 && !state->bcast /* from a WINS server */
1439 && nmb->header.rcode /* Error returned */
1442 if( DEBUGLVL( 3 ) ) {
1443 /* Only executed if DEBUGLEVEL >= 3 */
1444 dbgtext( "Negative name query "
1445 "response, rcode 0x%02x: ",
1446 nmb->header.rcode );
1447 switch( nmb->header.rcode ) {
1449 dbgtext("Request was invalidly formatted.\n");
1452 dbgtext("Problem with NBNS, cannot process "
1456 dbgtext("The name requested does not "
1460 dbgtext("Unsupported request error.\n");
1463 dbgtext("Query refused error.\n");
1466 dbgtext("Unrecognized error code.\n" );
1472 * We accept this packet as valid, but tell the upper
1473 * layers that it's a negative response.
1475 state->validate_error = NT_STATUS_NOT_FOUND;
1479 if (nmb->header.opcode != 0 ||
1480 nmb->header.nm_flags.bcast ||
1481 nmb->header.rcode ||
1482 !nmb->header.ancount) {
1484 * XXXX what do we do with this? Could be a redirect,
1485 * but we'll discard it for the moment.
1490 tmp_addrs = talloc_realloc(
1491 state, state->addrs, struct sockaddr_storage,
1492 state->num_addrs + nmb->answers->rdlength/6);
1493 if (tmp_addrs == NULL) {
1494 state->validate_error = NT_STATUS_NO_MEMORY;
1497 state->addrs = tmp_addrs;
1499 DEBUG(2,("Got a positive name query response "
1500 "from %s ( ", inet_ntoa(p->ip)));
1502 for (i=0; i<nmb->answers->rdlength/6; i++) {
1505 struct sockaddr_storage addr;
1506 struct samba_sockaddr sa = {0};
1510 flags = RSVAL(&nmb->answers->rdata[i*6], 0);
1511 got_unique_netbios_name |= ((flags & 0x8000) == 0);
1513 putip((char *)&ip,&nmb->answers->rdata[2+i*6]);
1514 in_addr_to_sockaddr_storage(&addr, ip);
1516 ok = sockaddr_storage_to_samba_sockaddr(&sa, &addr);
1521 if (is_zero_addr(&sa.u.ss)) {
1525 for (j=0; j<state->num_addrs; j++) {
1526 struct samba_sockaddr sa_j = {0};
1528 ok = sockaddr_storage_to_samba_sockaddr(&sa_j,
1533 if (sockaddr_equal(&sa.u.sa, &sa_j.u.sa)) {
1537 if (j < state->num_addrs) {
1538 /* Already got it */
1542 DEBUGADD(2,("%s ",inet_ntoa(ip)));
1544 state->addrs[state->num_addrs] = addr;
1545 state->num_addrs += 1;
1547 DEBUGADD(2,(")\n"));
1549 /* We add the flags back ... */
1550 if (nmb->header.response)
1551 state->flags |= NM_FLAGS_RS;
1552 if (nmb->header.nm_flags.authoritative)
1553 state->flags |= NM_FLAGS_AA;
1554 if (nmb->header.nm_flags.trunc)
1555 state->flags |= NM_FLAGS_TC;
1556 if (nmb->header.nm_flags.recursion_desired)
1557 state->flags |= NM_FLAGS_RD;
1558 if (nmb->header.nm_flags.recursion_available)
1559 state->flags |= NM_FLAGS_RA;
1560 if (nmb->header.nm_flags.bcast)
1561 state->flags |= NM_FLAGS_B;
1565 * We have to collect all entries coming in from broadcast
1566 * queries. If we got a unique name and we are not querying
1567 * all names registered within broadcast area (query
1568 * for the name '*', so state->bcast_star_query is set),
1571 return (got_unique_netbios_name && !state->bcast_star_query);
1574 * WINS responses are accepted when they are received
1579 static void name_query_done(struct tevent_req *subreq)
1581 struct tevent_req *req = tevent_req_callback_data(
1582 subreq, struct tevent_req);
1583 struct name_query_state *state = tevent_req_data(
1584 req, struct name_query_state);
1586 struct packet_struct *p = NULL;
1588 status = nb_trans_recv(subreq, state, &p);
1589 TALLOC_FREE(subreq);
1590 if (tevent_req_nterror(req, status)) {
1593 if (!NT_STATUS_IS_OK(state->validate_error)) {
1594 tevent_req_nterror(req, state->validate_error);
1597 tevent_req_done(req);
1600 NTSTATUS name_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1601 struct sockaddr_storage **addrs, int *num_addrs,
1604 struct name_query_state *state = tevent_req_data(
1605 req, struct name_query_state);
1608 if (tevent_req_is_nterror(req, &status)) {
1610 NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
1612 * In the broadcast case we collect replies until the
1615 status = NT_STATUS_OK;
1617 if (!NT_STATUS_IS_OK(status)) {
1621 if (state->num_addrs == 0) {
1622 return NT_STATUS_NOT_FOUND;
1624 *addrs = talloc_move(mem_ctx, &state->addrs);
1625 sort_addr_list(*addrs, state->num_addrs);
1626 *num_addrs = state->num_addrs;
1627 if (flags != NULL) {
1628 *flags = state->flags;
1630 return NT_STATUS_OK;
1633 NTSTATUS name_query(const char *name, int name_type,
1634 bool bcast, bool recurse,
1635 const struct sockaddr_storage *to_ss,
1636 TALLOC_CTX *mem_ctx,
1637 struct sockaddr_storage **addrs,
1638 int *num_addrs, uint8_t *flags)
1640 TALLOC_CTX *frame = talloc_stackframe();
1641 struct tevent_context *ev;
1642 struct tevent_req *req;
1643 struct timeval timeout;
1644 NTSTATUS status = NT_STATUS_NO_MEMORY;
1646 ev = samba_tevent_context_init(frame);
1650 req = name_query_send(ev, ev, name, name_type, bcast, recurse, to_ss);
1655 timeout = timeval_current_ofs(0, 250000);
1657 timeout = timeval_current_ofs(2, 0);
1659 if (!tevent_req_set_endtime(req, ev, timeout)) {
1662 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1665 status = name_query_recv(req, mem_ctx, addrs, num_addrs, flags);
1671 /********************************************************
1672 Convert an array if struct sockaddr_storage to struct ip_service
1673 return false on failure. Port is set to PORT_NONE;
1674 pcount is [in/out] - it is the length of ss_list on input,
1675 and the length of return_iplist on output as we remove any
1676 zero addresses from ss_list.
1677 *********************************************************/
1679 static bool convert_ss2service(struct ip_service **return_iplist,
1680 const struct sockaddr_storage *ss_list,
1684 int orig_count = *pcount;
1687 if (orig_count==0 || !ss_list )
1690 /* Filter out zero addrs. */
1691 for ( i=0; i<orig_count; i++ ) {
1692 if (is_zero_addr(&ss_list[i])) {
1697 if (real_count==0) {
1701 /* copy the ip address; port will be PORT_NONE */
1702 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, real_count)) ==
1704 DEBUG(0,("convert_ip2service: malloc failed "
1705 "for %d enetries!\n", real_count ));
1709 for ( i=0, real_count = 0; i<orig_count; i++ ) {
1710 if (is_zero_addr(&ss_list[i])) {
1713 (*return_iplist)[real_count].ss = ss_list[i];
1714 (*return_iplist)[real_count].port = PORT_NONE;
1718 *pcount = real_count;
1722 struct name_queries_state {
1723 struct tevent_context *ev;
1728 const struct sockaddr_storage *addrs;
1733 struct tevent_req **subreqs;
1738 struct sockaddr_storage *result_addrs;
1739 int num_result_addrs;
1743 static void name_queries_done(struct tevent_req *subreq);
1744 static void name_queries_next(struct tevent_req *subreq);
1747 * Send a name query to multiple destinations with a wait time in between
1750 static struct tevent_req *name_queries_send(
1751 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1752 const char *name, int name_type,
1753 bool bcast, bool recurse,
1754 const struct sockaddr_storage *addrs,
1755 int num_addrs, int wait_msec, int timeout_msec)
1757 struct tevent_req *req, *subreq;
1758 struct name_queries_state *state;
1760 req = tevent_req_create(mem_ctx, &state,
1761 struct name_queries_state);
1767 state->name_type = name_type;
1768 state->bcast = bcast;
1769 state->recurse = recurse;
1770 state->addrs = addrs;
1771 state->num_addrs = num_addrs;
1772 state->wait_msec = wait_msec;
1773 state->timeout_msec = timeout_msec;
1775 state->subreqs = talloc_zero_array(
1776 state, struct tevent_req *, num_addrs);
1777 if (tevent_req_nomem(state->subreqs, req)) {
1778 return tevent_req_post(req, ev);
1780 state->num_sent = 0;
1782 subreq = name_query_send(
1783 state->subreqs, state->ev, name, name_type, bcast, recurse,
1784 &state->addrs[state->num_sent]);
1785 if (tevent_req_nomem(subreq, req)) {
1786 return tevent_req_post(req, ev);
1788 if (!tevent_req_set_endtime(
1790 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1791 return tevent_req_post(req, ev);
1793 tevent_req_set_callback(subreq, name_queries_done, req);
1795 state->subreqs[state->num_sent] = subreq;
1796 state->num_sent += 1;
1798 if (state->num_sent < state->num_addrs) {
1799 subreq = tevent_wakeup_send(
1801 timeval_current_ofs(0, state->wait_msec * 1000));
1802 if (tevent_req_nomem(subreq, req)) {
1803 return tevent_req_post(req, ev);
1805 tevent_req_set_callback(subreq, name_queries_next, req);
1810 static void name_queries_done(struct tevent_req *subreq)
1812 struct tevent_req *req = tevent_req_callback_data(
1813 subreq, struct tevent_req);
1814 struct name_queries_state *state = tevent_req_data(
1815 req, struct name_queries_state);
1819 status = name_query_recv(subreq, state, &state->result_addrs,
1820 &state->num_result_addrs, &state->flags);
1822 for (i=0; i<state->num_sent; i++) {
1823 if (state->subreqs[i] == subreq) {
1827 if (i == state->num_sent) {
1828 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1831 TALLOC_FREE(state->subreqs[i]);
1833 state->num_received += 1;
1835 if (!NT_STATUS_IS_OK(status)) {
1837 if (state->num_received >= state->num_addrs) {
1838 tevent_req_nterror(req, status);
1842 * Still outstanding requests, just wait
1846 state->received_index = i;
1847 tevent_req_done(req);
1850 static void name_queries_next(struct tevent_req *subreq)
1852 struct tevent_req *req = tevent_req_callback_data(
1853 subreq, struct tevent_req);
1854 struct name_queries_state *state = tevent_req_data(
1855 req, struct name_queries_state);
1857 if (!tevent_wakeup_recv(subreq)) {
1858 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1862 subreq = name_query_send(
1863 state->subreqs, state->ev,
1864 state->name, state->name_type, state->bcast, state->recurse,
1865 &state->addrs[state->num_sent]);
1866 if (tevent_req_nomem(subreq, req)) {
1869 tevent_req_set_callback(subreq, name_queries_done, req);
1870 if (!tevent_req_set_endtime(
1872 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1875 state->subreqs[state->num_sent] = subreq;
1876 state->num_sent += 1;
1878 if (state->num_sent < state->num_addrs) {
1879 subreq = tevent_wakeup_send(
1881 timeval_current_ofs(0, state->wait_msec * 1000));
1882 if (tevent_req_nomem(subreq, req)) {
1885 tevent_req_set_callback(subreq, name_queries_next, req);
1889 static NTSTATUS name_queries_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1890 struct sockaddr_storage **result_addrs,
1891 int *num_result_addrs, uint8_t *flags,
1892 int *received_index)
1894 struct name_queries_state *state = tevent_req_data(
1895 req, struct name_queries_state);
1898 if (tevent_req_is_nterror(req, &status)) {
1902 if (result_addrs != NULL) {
1903 *result_addrs = talloc_move(mem_ctx, &state->result_addrs);
1905 if (num_result_addrs != NULL) {
1906 *num_result_addrs = state->num_result_addrs;
1908 if (flags != NULL) {
1909 *flags = state->flags;
1911 if (received_index != NULL) {
1912 *received_index = state->received_index;
1914 return NT_STATUS_OK;
1917 /********************************************************
1918 Resolve via "bcast" method.
1919 *********************************************************/
1921 struct name_resolve_bcast_state {
1922 struct sockaddr_storage *addrs;
1926 static void name_resolve_bcast_done(struct tevent_req *subreq);
1928 struct tevent_req *name_resolve_bcast_send(TALLOC_CTX *mem_ctx,
1929 struct tevent_context *ev,
1933 struct tevent_req *req, *subreq;
1934 struct name_resolve_bcast_state *state;
1935 struct sockaddr_storage *bcast_addrs;
1936 int i, num_addrs, num_bcast_addrs;
1938 req = tevent_req_create(mem_ctx, &state,
1939 struct name_resolve_bcast_state);
1944 if (lp_disable_netbios()) {
1945 DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
1947 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1948 return tevent_req_post(req, ev);
1952 * "bcast" means do a broadcast lookup on all the local interfaces.
1955 DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
1956 "for name %s<0x%x>\n", name, name_type));
1958 num_addrs = iface_count();
1959 bcast_addrs = talloc_array(state, struct sockaddr_storage, num_addrs);
1960 if (tevent_req_nomem(bcast_addrs, req)) {
1961 return tevent_req_post(req, ev);
1965 * Lookup the name on all the interfaces, return on
1966 * the first successful match.
1968 num_bcast_addrs = 0;
1970 for (i=0; i<num_addrs; i++) {
1971 const struct sockaddr_storage *pss = iface_n_bcast(i);
1973 if (pss->ss_family != AF_INET) {
1976 bcast_addrs[num_bcast_addrs] = *pss;
1977 num_bcast_addrs += 1;
1980 subreq = name_queries_send(state, ev, name, name_type, true, true,
1981 bcast_addrs, num_bcast_addrs, 0, 250);
1982 if (tevent_req_nomem(subreq, req)) {
1983 return tevent_req_post(req, ev);
1985 tevent_req_set_callback(subreq, name_resolve_bcast_done, req);
1989 static void name_resolve_bcast_done(struct tevent_req *subreq)
1991 struct tevent_req *req = tevent_req_callback_data(
1992 subreq, struct tevent_req);
1993 struct name_resolve_bcast_state *state = tevent_req_data(
1994 req, struct name_resolve_bcast_state);
1997 status = name_queries_recv(subreq, state,
1998 &state->addrs, &state->num_addrs,
2000 TALLOC_FREE(subreq);
2001 if (tevent_req_nterror(req, status)) {
2004 tevent_req_done(req);
2007 NTSTATUS name_resolve_bcast_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2008 struct sockaddr_storage **addrs,
2011 struct name_resolve_bcast_state *state = tevent_req_data(
2012 req, struct name_resolve_bcast_state);
2015 if (tevent_req_is_nterror(req, &status)) {
2018 *addrs = talloc_move(mem_ctx, &state->addrs);
2019 *num_addrs = state->num_addrs;
2020 return NT_STATUS_OK;
2023 NTSTATUS name_resolve_bcast(TALLOC_CTX *mem_ctx,
2026 struct sockaddr_storage **return_iplist,
2029 TALLOC_CTX *frame = talloc_stackframe();
2030 struct tevent_context *ev;
2031 struct tevent_req *req;
2032 NTSTATUS status = NT_STATUS_NO_MEMORY;
2034 ev = samba_tevent_context_init(frame);
2038 req = name_resolve_bcast_send(frame, ev, name, name_type);
2042 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2045 status = name_resolve_bcast_recv(req, mem_ctx, return_iplist,
2052 struct query_wins_list_state {
2053 struct tevent_context *ev;
2056 struct in_addr *servers;
2057 uint32_t num_servers;
2058 struct sockaddr_storage server;
2061 struct sockaddr_storage *addrs;
2066 static void query_wins_list_done(struct tevent_req *subreq);
2069 * Query a list of (replicating) wins servers in sequence, call them
2070 * dead if they don't reply
2073 static struct tevent_req *query_wins_list_send(
2074 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2075 struct in_addr src_ip, const char *name, uint8_t name_type,
2076 struct in_addr *servers, int num_servers)
2078 struct tevent_req *req, *subreq;
2079 struct query_wins_list_state *state;
2081 req = tevent_req_create(mem_ctx, &state,
2082 struct query_wins_list_state);
2088 state->name_type = name_type;
2089 state->servers = servers;
2090 state->num_servers = num_servers;
2092 if (state->num_servers == 0) {
2093 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2094 return tevent_req_post(req, ev);
2097 in_addr_to_sockaddr_storage(
2098 &state->server, state->servers[state->num_sent]);
2100 subreq = name_query_send(state, state->ev,
2101 state->name, state->name_type,
2102 false, true, &state->server);
2103 state->num_sent += 1;
2104 if (tevent_req_nomem(subreq, req)) {
2105 return tevent_req_post(req, ev);
2107 if (!tevent_req_set_endtime(subreq, state->ev,
2108 timeval_current_ofs(2, 0))) {
2109 return tevent_req_post(req, ev);
2111 tevent_req_set_callback(subreq, query_wins_list_done, req);
2115 static void query_wins_list_done(struct tevent_req *subreq)
2117 struct tevent_req *req = tevent_req_callback_data(
2118 subreq, struct tevent_req);
2119 struct query_wins_list_state *state = tevent_req_data(
2120 req, struct query_wins_list_state);
2123 status = name_query_recv(subreq, state,
2124 &state->addrs, &state->num_addrs,
2126 TALLOC_FREE(subreq);
2127 if (NT_STATUS_IS_OK(status)) {
2128 tevent_req_done(req);
2131 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
2132 tevent_req_nterror(req, status);
2135 wins_srv_died(state->servers[state->num_sent-1],
2136 my_socket_addr_v4());
2138 if (state->num_sent == state->num_servers) {
2139 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2143 in_addr_to_sockaddr_storage(
2144 &state->server, state->servers[state->num_sent]);
2146 subreq = name_query_send(state, state->ev,
2147 state->name, state->name_type,
2148 false, true, &state->server);
2149 state->num_sent += 1;
2150 if (tevent_req_nomem(subreq, req)) {
2153 if (!tevent_req_set_endtime(subreq, state->ev,
2154 timeval_current_ofs(2, 0))) {
2157 tevent_req_set_callback(subreq, query_wins_list_done, req);
2160 static NTSTATUS query_wins_list_recv(struct tevent_req *req,
2161 TALLOC_CTX *mem_ctx,
2162 struct sockaddr_storage **addrs,
2166 struct query_wins_list_state *state = tevent_req_data(
2167 req, struct query_wins_list_state);
2170 if (tevent_req_is_nterror(req, &status)) {
2173 if (addrs != NULL) {
2174 *addrs = talloc_move(mem_ctx, &state->addrs);
2176 if (num_addrs != NULL) {
2177 *num_addrs = state->num_addrs;
2179 if (flags != NULL) {
2180 *flags = state->flags;
2182 return NT_STATUS_OK;
2185 struct resolve_wins_state {
2189 struct sockaddr_storage *addrs;
2194 static void resolve_wins_done(struct tevent_req *subreq);
2196 struct tevent_req *resolve_wins_send(TALLOC_CTX *mem_ctx,
2197 struct tevent_context *ev,
2201 struct tevent_req *req, *subreq;
2202 struct resolve_wins_state *state;
2203 char **wins_tags = NULL;
2204 struct sockaddr_storage src_ss;
2205 struct samba_sockaddr src_sa = {0};
2206 struct in_addr src_ip;
2207 int i, num_wins_tags;
2210 req = tevent_req_create(mem_ctx, &state,
2211 struct resolve_wins_state);
2216 if (wins_srv_count() < 1) {
2217 DEBUG(3,("resolve_wins: WINS server resolution selected "
2218 "and no WINS servers listed.\n"));
2219 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2223 /* the address we will be sending from */
2224 if (!interpret_string_addr(&src_ss, lp_nbt_client_socket_address(),
2225 AI_NUMERICHOST|AI_PASSIVE)) {
2226 zero_sockaddr(&src_ss);
2229 ok = sockaddr_storage_to_samba_sockaddr(&src_sa, &src_ss);
2231 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2235 if (src_sa.u.ss.ss_family != AF_INET) {
2236 char addr[INET6_ADDRSTRLEN];
2237 print_sockaddr(addr, sizeof(addr), &src_sa.u.ss);
2238 DEBUG(3,("resolve_wins: cannot receive WINS replies "
2239 "on IPv6 address %s\n",
2241 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2245 src_ip = src_sa.u.in.sin_addr;
2247 wins_tags = wins_srv_tags();
2248 if (wins_tags == NULL) {
2249 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2254 while (wins_tags[num_wins_tags] != NULL) {
2258 for (i=0; i<num_wins_tags; i++) {
2259 int num_servers, num_alive;
2260 struct in_addr *servers, *alive;
2263 if (!wins_server_tag_ips(wins_tags[i], talloc_tos(),
2264 &servers, &num_servers)) {
2265 DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
2270 alive = talloc_array(state, struct in_addr, num_servers);
2271 if (tevent_req_nomem(alive, req)) {
2276 for (j=0; j<num_servers; j++) {
2277 struct in_addr wins_ip = servers[j];
2279 if (global_in_nmbd && ismyip_v4(wins_ip)) {
2280 /* yikes! we'll loop forever */
2283 /* skip any that have been unresponsive lately */
2284 if (wins_srv_is_dead(wins_ip, src_ip)) {
2287 DEBUG(3, ("resolve_wins: using WINS server %s "
2289 inet_ntoa(wins_ip), wins_tags[i]));
2290 alive[num_alive] = wins_ip;
2293 TALLOC_FREE(servers);
2295 if (num_alive == 0) {
2299 subreq = query_wins_list_send(
2300 state, ev, src_ip, name, name_type,
2302 if (tevent_req_nomem(subreq, req)) {
2305 tevent_req_set_callback(subreq, resolve_wins_done, req);
2306 state->num_sent += 1;
2309 if (state->num_sent == 0) {
2310 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2314 wins_srv_tags_free(wins_tags);
2317 wins_srv_tags_free(wins_tags);
2318 return tevent_req_post(req, ev);
2321 static void resolve_wins_done(struct tevent_req *subreq)
2323 struct tevent_req *req = tevent_req_callback_data(
2324 subreq, struct tevent_req);
2325 struct resolve_wins_state *state = tevent_req_data(
2326 req, struct resolve_wins_state);
2329 status = query_wins_list_recv(subreq, state, &state->addrs,
2330 &state->num_addrs, &state->flags);
2331 if (NT_STATUS_IS_OK(status)) {
2332 tevent_req_done(req);
2336 state->num_received += 1;
2338 if (state->num_received < state->num_sent) {
2340 * Wait for the others
2344 tevent_req_nterror(req, status);
2347 NTSTATUS resolve_wins_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2348 struct sockaddr_storage **addrs,
2349 int *num_addrs, uint8_t *flags)
2351 struct resolve_wins_state *state = tevent_req_data(
2352 req, struct resolve_wins_state);
2355 if (tevent_req_is_nterror(req, &status)) {
2358 if (addrs != NULL) {
2359 *addrs = talloc_move(mem_ctx, &state->addrs);
2361 if (num_addrs != NULL) {
2362 *num_addrs = state->num_addrs;
2364 if (flags != NULL) {
2365 *flags = state->flags;
2367 return NT_STATUS_OK;
2370 /********************************************************
2371 Resolve via "wins" method.
2372 *********************************************************/
2374 NTSTATUS resolve_wins(TALLOC_CTX *mem_ctx,
2377 struct sockaddr_storage **return_iplist,
2380 struct tevent_context *ev;
2381 struct tevent_req *req;
2382 NTSTATUS status = NT_STATUS_NO_MEMORY;
2384 ev = samba_tevent_context_init(talloc_tos());
2388 req = resolve_wins_send(ev, ev, name, name_type);
2392 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2395 status = resolve_wins_recv(req, mem_ctx, return_iplist, return_count,
2402 /********************************************************
2403 Use ads_dns_lookup_[a|aaaa]_send() calls to look up a
2404 list of names asynchronously.
2405 *********************************************************/
2407 struct dns_query_state {
2408 /* Used to tell our parent we've completed. */
2409 struct dns_lookup_list_async_state *p_async_state;
2410 const char *query_name; /* For debugging only. */
2412 struct samba_sockaddr *addrs;
2415 struct dns_lookup_list_async_state {
2417 size_t num_query_returns;
2418 struct dns_query_state *queries;
2421 /* Called on query timeout. */
2422 static void dns_lookup_send_timeout_handler(struct tevent_context *ev,
2423 struct tevent_timer *te,
2427 bool *timed_out = (bool *)private_data;
2431 static void dns_lookup_list_a_done(struct tevent_req *req);
2432 #if defined(HAVE_IPV6)
2433 static void dns_lookup_list_aaaa_done(struct tevent_req *req);
2436 NTSTATUS dns_lookup_list_async(TALLOC_CTX *ctx,
2437 size_t num_dns_names,
2438 const char **dns_lookup_names,
2439 size_t *p_num_addrs,
2440 struct samba_sockaddr **pp_addrs,
2441 char ***pp_dns_names)
2443 struct dns_lookup_list_async_state *state = NULL;
2444 struct tevent_context *ev = NULL;
2445 struct tevent_req *req = NULL;
2446 struct tevent_timer *timer = NULL;
2447 size_t num_queries_sent = 0;
2448 size_t queries_size = num_dns_names;
2450 size_t num_addrs = 0;
2451 struct samba_sockaddr *addr_out = NULL;
2452 char **dns_names_ret = NULL;
2453 NTSTATUS status = NT_STATUS_NO_MEMORY;
2455 /* Nothing to do. */
2456 if (num_dns_names == 0) {
2459 if (pp_dns_names != NULL) {
2460 *pp_dns_names = NULL;
2462 return NT_STATUS_OK;
2465 state = talloc_zero(ctx, struct dns_lookup_list_async_state);
2466 if (state == NULL) {
2469 ev = samba_tevent_context_init(ctx);
2474 #if defined(HAVE_IPV6)
2475 queries_size = 2 * num_dns_names;
2476 /* Wrap protection. */
2477 if (queries_size < num_dns_names) {
2481 queries_size = num_dns_names;
2484 state->queries = talloc_zero_array(state,
2485 struct dns_query_state,
2487 if (state->queries == NULL) {
2491 /* Hit all the DNS servers with asnyc lookups for all the names. */
2492 for (i = 0; i < num_dns_names; i++) {
2493 DBG_INFO("async DNS lookup A record for %s\n",
2494 dns_lookup_names[i]);
2496 /* Setup the query state. */
2497 state->queries[num_queries_sent].query_name =
2498 dns_lookup_names[i];
2499 state->queries[num_queries_sent].p_async_state = state;
2501 req = ads_dns_lookup_a_send(ev, ev, dns_lookup_names[i]);
2505 tevent_req_set_callback(req,
2506 dns_lookup_list_a_done,
2507 &state->queries[num_queries_sent]);
2510 #if defined(HAVE_IPV6)
2511 /* If we're IPv6 capable ask for that too. */
2512 state->queries[num_queries_sent].query_name =
2513 dns_lookup_names[i];
2514 state->queries[num_queries_sent].p_async_state = state;
2516 DBG_INFO("async DNS lookup AAAA record for %s\n",
2517 dns_lookup_names[i]);
2519 req = ads_dns_lookup_aaaa_send(ev, ev, dns_lookup_names[i]);
2523 tevent_req_set_callback(req,
2524 dns_lookup_list_aaaa_done,
2525 &state->queries[num_queries_sent]);
2530 /* We must always have a timeout. */
2531 timer = tevent_add_timer(ev,
2533 timeval_current_ofs(lp_get_async_dns_timeout(),
2535 dns_lookup_send_timeout_handler,
2537 if (timer == NULL) {
2541 /* Loop until timed out or got all replies. */
2545 if (state->timed_out) {
2548 if (state->num_query_returns == num_queries_sent) {
2551 ret = tevent_loop_once(ev);
2557 /* Count what we got back. */
2558 for (i = 0; i < num_queries_sent; i++) {
2559 struct dns_query_state *query = &state->queries[i];
2562 if (num_addrs + query->num_addrs < num_addrs) {
2565 num_addrs += query->num_addrs;
2568 if (state->timed_out) {
2569 DBG_INFO("async DNS lookup timed out after %zu entries "
2574 addr_out = talloc_zero_array(ctx,
2575 struct samba_sockaddr,
2577 if (addr_out == NULL) {
2582 * Did the caller want an array of names back
2583 * that match the IP addresses ? If we provide
2584 * this, dsgetdcname() internals can now use this
2585 * async lookup code also.
2587 if (pp_dns_names != NULL) {
2588 dns_names_ret = talloc_zero_array(ctx,
2591 if (dns_names_ret == NULL) {
2596 /* Copy what we got back. */
2598 for (i = 0; i < num_queries_sent; i++) {
2600 struct dns_query_state *query = &state->queries[i];
2602 if (query->num_addrs == 0) {
2606 if (dns_names_ret != NULL) {
2608 * If caller wants a name array matched with
2609 * the addrs array, copy the same queried name for
2610 * each IP address returned.
2612 for (j = 0; j < query->num_addrs; j++) {
2613 dns_names_ret[num_addrs + j] = talloc_strdup(
2616 if (dns_names_ret[num_addrs + j] == NULL) {
2622 for (j = 0; j < query->num_addrs; j++) {
2623 addr_out[num_addrs] = query->addrs[j];
2625 num_addrs += query->num_addrs;
2628 *p_num_addrs = num_addrs;
2629 *pp_addrs = addr_out;
2630 if (pp_dns_names != NULL) {
2631 *pp_dns_names = dns_names_ret;
2634 status = NT_STATUS_OK;
2644 Called when an A record lookup completes.
2647 static void dns_lookup_list_a_done(struct tevent_req *req)
2650 * Callback data is an element of a talloc'ed array,
2651 * not a talloc object in its own right. So use the
2652 * tevent_req_callback_data_void() void * cast function.
2654 struct dns_query_state *state = (struct dns_query_state *)
2655 tevent_req_callback_data_void(req);
2658 char **hostnames_out = NULL;
2659 struct samba_sockaddr *addrs = NULL;
2660 size_t num_addrs = 0;
2663 /* For good or ill, tell the parent we're finished. */
2664 state->p_async_state->num_query_returns++;
2666 status = ads_dns_lookup_a_recv(req,
2667 state->p_async_state,
2672 if (!NT_STATUS_IS_OK(status)) {
2673 DBG_INFO("async DNS A lookup for %s returned %s\n",
2679 if (rcode != DNS_RCODE_OK) {
2680 DBG_INFO("async DNS A lookup for %s returned DNS code %u\n",
2682 (unsigned int)rcode);
2686 if (num_addrs == 0) {
2687 DBG_INFO("async DNS A lookup for %s returned 0 addresses.\n",
2692 /* Copy data out. */
2693 state->addrs = talloc_zero_array(state->p_async_state,
2694 struct samba_sockaddr,
2696 if (state->addrs == NULL) {
2700 for (i = 0; i < num_addrs; i++) {
2701 char addr[INET6_ADDRSTRLEN];
2702 DBG_INFO("async DNS A lookup for %s [%zu] got %s -> %s\n",
2706 print_sockaddr(addr,
2710 state->addrs[i] = addrs[i];
2712 state->num_addrs = num_addrs;
2715 #if defined(HAVE_IPV6)
2717 Called when an AAAA record lookup completes.
2720 static void dns_lookup_list_aaaa_done(struct tevent_req *req)
2723 * Callback data is an element of a talloc'ed array,
2724 * not a talloc object in its own right. So use the
2725 * tevent_req_callback_data_void() void * cast function.
2727 struct dns_query_state *state = (struct dns_query_state *)
2728 tevent_req_callback_data_void(req);
2731 char **hostnames_out = NULL;
2732 struct samba_sockaddr *addrs = NULL;
2733 size_t num_addrs = 0;
2736 /* For good or ill, tell the parent we're finished. */
2737 state->p_async_state->num_query_returns++;
2739 status = ads_dns_lookup_aaaa_recv(req,
2740 state->p_async_state,
2745 if (!NT_STATUS_IS_OK(status)) {
2746 DBG_INFO("async DNS AAAA lookup for %s returned %s\n",
2752 if (rcode != DNS_RCODE_OK) {
2753 DBG_INFO("async DNS AAAA lookup for %s returned DNS code %u\n",
2755 (unsigned int)rcode);
2759 if (num_addrs == 0) {
2760 DBG_INFO("async DNS AAAA lookup for %s returned 0 addresses.\n",
2765 /* Copy data out. */
2766 state->addrs = talloc_zero_array(state->p_async_state,
2767 struct samba_sockaddr,
2769 if (state->addrs == NULL) {
2773 for (i = 0; i < num_addrs; i++) {
2774 char addr[INET6_ADDRSTRLEN];
2775 DBG_INFO("async DNS AAAA lookup for %s [%zu] got %s -> %s\n",
2779 print_sockaddr(addr,
2782 state->addrs[i] = addrs[i];
2784 state->num_addrs = num_addrs;
2788 /********************************************************
2789 Resolve via "hosts" method.
2790 *********************************************************/
2792 static NTSTATUS resolve_hosts(TALLOC_CTX *mem_ctx,
2795 struct sockaddr_storage **return_iplist,
2799 * "host" means do a localhost, or dns lookup.
2801 struct addrinfo hints;
2802 struct addrinfo *ailist = NULL;
2803 struct addrinfo *res = NULL;
2807 if ( name_type != 0x20 && name_type != 0x0) {
2808 DEBUG(5, ("resolve_hosts: not appropriate "
2809 "for name type <0x%x>\n",
2811 return NT_STATUS_INVALID_PARAMETER;
2814 *return_iplist = NULL;
2817 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
2821 /* By default make sure it supports TCP. */
2822 hints.ai_socktype = SOCK_STREAM;
2823 hints.ai_flags = AI_ADDRCONFIG;
2825 #if !defined(HAVE_IPV6)
2826 /* Unless we have IPv6, we really only want IPv4 addresses back. */
2827 hints.ai_family = AF_INET;
2830 ret = getaddrinfo(name,
2835 DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
2837 gai_strerror(ret) ));
2840 for (res = ailist; res; res = res->ai_next) {
2841 struct sockaddr_storage ss;
2843 if (!res->ai_addr || res->ai_addrlen == 0) {
2848 memcpy(&ss, res->ai_addr, res->ai_addrlen);
2850 if (is_zero_addr(&ss)) {
2856 *return_iplist = talloc_realloc(
2857 mem_ctx, *return_iplist, struct sockaddr_storage,
2859 if (!*return_iplist) {
2860 DEBUG(3,("resolve_hosts: malloc fail !\n"));
2861 freeaddrinfo(ailist);
2862 return NT_STATUS_NO_MEMORY;
2864 (*return_iplist)[i] = ss;
2868 freeaddrinfo(ailist);
2870 if (*return_count) {
2871 return NT_STATUS_OK;
2873 return NT_STATUS_UNSUCCESSFUL;
2876 /********************************************************
2877 Resolve via "ADS" method.
2878 *********************************************************/
2880 /* Special name type used to cause a _kerberos DNS lookup. */
2881 #define KDC_NAME_TYPE 0xDCDC
2883 static NTSTATUS resolve_ads(TALLOC_CTX *ctx,
2886 const char *sitename,
2887 struct sockaddr_storage **return_addrs,
2892 struct dns_rr_srv *dcs = NULL;
2895 size_t num_srv_addrs = 0;
2896 struct sockaddr_storage *srv_addrs = NULL;
2897 size_t num_dns_addrs = 0;
2898 struct samba_sockaddr *dns_addrs = NULL;
2899 size_t num_dns_names = 0;
2900 const char **dns_lookup_names = NULL;
2901 struct sockaddr_storage *ret_addrs = NULL;
2903 if ((name_type != 0x1c) && (name_type != KDC_NAME_TYPE) &&
2904 (name_type != 0x1b)) {
2905 return NT_STATUS_INVALID_PARAMETER;
2908 switch (name_type) {
2910 DEBUG(5,("resolve_ads: Attempting to resolve "
2911 "PDC for %s using DNS\n", name));
2912 status = ads_dns_query_pdc(ctx,
2919 DEBUG(5,("resolve_ads: Attempting to resolve "
2920 "DCs for %s using DNS\n", name));
2921 status = ads_dns_query_dcs(ctx,
2928 DEBUG(5,("resolve_ads: Attempting to resolve "
2929 "KDCs for %s using DNS\n", name));
2930 status = ads_dns_query_kdcs(ctx,
2937 status = NT_STATUS_INVALID_PARAMETER;
2941 if (!NT_STATUS_IS_OK(status)) {
2947 *return_addrs = NULL;
2950 return NT_STATUS_OK;
2956 return NT_STATUS_INVALID_PARAMETER;
2960 * Split the returned values into 2 arrays. First one
2961 * is a struct sockaddr_storage array that contains results
2962 * from the SRV record lookup that contain both hostnames
2963 * and IP addresses. We only need to copy out the IP
2964 * addresses. This is srv_addrs.
2966 * Second array contains the results from the SRV record
2967 * lookup that only contain hostnames - no IP addresses.
2968 * We must then call dns_lookup_list() to lookup
2969 * hostnames -> IP address. This is dns_addrs.
2971 * Finally we will merge these two arrays to create the
2972 * return sockaddr_storage array.
2975 /* First count the sizes of each array. */
2976 for(i = 0; i < numdcs; i++) {
2977 if (dcs[i].ss_s != NULL) {
2978 /* IP address returned in SRV record. */
2979 if (num_srv_addrs + dcs[i].num_ips < num_srv_addrs) {
2982 return NT_STATUS_INVALID_PARAMETER;
2984 /* Add in the number of addresses we got. */
2985 num_srv_addrs += dcs[i].num_ips;
2987 * If we got any IP addresses zero out
2988 * the hostname so we know we've already
2989 * processed this entry and won't add it
2990 * to the dns_lookup_names array we use
2991 * to do DNS queries below.
2993 dcs[i].hostname = NULL;
2995 /* Ensure we have a hostname to lookup. */
2996 if (dcs[i].hostname == NULL) {
2999 /* No IP address returned in SRV record. */
3000 if (num_dns_names + 1 < num_dns_names) {
3003 return NT_STATUS_INVALID_PARAMETER;
3005 /* One more name to lookup. */
3010 /* Allocate the list of IP addresses we already have. */
3011 srv_addrs = talloc_zero_array(ctx,
3012 struct sockaddr_storage,
3014 if (srv_addrs == NULL) {
3016 return NT_STATUS_NO_MEMORY;
3019 /* Copy the addresses we already have. */
3021 for(i = 0; i < numdcs; i++) {
3022 /* Copy all the IP addresses from the SRV response */
3024 for (j = 0; j < dcs[i].num_ips; j++) {
3025 char addr[INET6_ADDRSTRLEN];
3027 srv_addrs[num_srv_addrs] = dcs[i].ss_s[j];
3028 if (is_zero_addr(&srv_addrs[num_srv_addrs])) {
3032 DBG_DEBUG("SRV lookup %s got IP[%zu] %s\n",
3035 print_sockaddr(addr,
3037 &srv_addrs[num_srv_addrs]));
3043 /* Allocate the array of hostnames we must look up. */
3044 dns_lookup_names = talloc_zero_array(ctx,
3047 if (dns_lookup_names == NULL) {
3049 TALLOC_FREE(srv_addrs);
3050 return NT_STATUS_NO_MEMORY;
3054 for(i = 0; i < numdcs; i++) {
3055 if (dcs[i].hostname == NULL) {
3057 * Must have been a SRV return with an IP address.
3058 * We don't need to look up this hostname.
3062 dns_lookup_names[num_dns_names] = dcs[i].hostname;
3066 /* Lookup the addresses on the dns_lookup_list. */
3067 status = dns_lookup_list_async(ctx,
3074 if (!NT_STATUS_IS_OK(status)) {
3076 TALLOC_FREE(srv_addrs);
3077 TALLOC_FREE(dns_lookup_names);
3078 TALLOC_FREE(dns_addrs);
3083 * Combine the two sockaddr_storage arrays into a talloc'ed
3084 * struct sockaddr_storage array return.
3087 numaddrs = num_srv_addrs + num_dns_addrs;
3088 /* Wrap check + bloody int conversion check :-(. */
3089 if (numaddrs < num_srv_addrs ||
3092 TALLOC_FREE(srv_addrs);
3093 TALLOC_FREE(dns_addrs);
3094 TALLOC_FREE(dns_lookup_names);
3095 return NT_STATUS_INVALID_PARAMETER;
3098 if (numaddrs == 0) {
3099 /* Keep the same semantics for zero names. */
3101 TALLOC_FREE(srv_addrs);
3102 TALLOC_FREE(dns_addrs);
3103 TALLOC_FREE(dns_lookup_names);
3104 *return_addrs = NULL;
3106 return NT_STATUS_OK;
3109 ret_addrs = talloc_zero_array(ctx,
3110 struct sockaddr_storage,
3112 if (ret_addrs == NULL) {
3114 TALLOC_FREE(srv_addrs);
3115 TALLOC_FREE(dns_addrs);
3116 TALLOC_FREE(dns_lookup_names);
3117 return NT_STATUS_NO_MEMORY;
3120 for (i = 0; i < num_srv_addrs; i++) {
3121 ret_addrs[i] = srv_addrs[i];
3123 for (i = 0; i < num_dns_addrs; i++) {
3124 ret_addrs[num_srv_addrs+i] = dns_addrs[i].u.ss;
3128 TALLOC_FREE(srv_addrs);
3129 TALLOC_FREE(dns_addrs);
3130 TALLOC_FREE(dns_lookup_names);
3132 *return_addrs = ret_addrs;
3133 *return_count = numaddrs;
3134 return NT_STATUS_OK;
3137 static const char **filter_out_nbt_lookup(TALLOC_CTX *mem_ctx,
3138 const char **resolve_order)
3140 size_t i, len, result_idx;
3141 const char **result;
3144 while (resolve_order[len] != NULL) {
3148 result = talloc_array(mem_ctx, const char *, len+1);
3149 if (result == NULL) {
3155 for (i=0; i<len; i++) {
3156 const char *tok = resolve_order[i];
3158 if (strequal(tok, "lmhosts") || strequal(tok, "wins") ||
3159 strequal(tok, "bcast")) {
3162 result[result_idx++] = tok;
3164 result[result_idx] = NULL;
3169 /*******************************************************************
3170 Internal interface to resolve a name into an IP address.
3171 Use this function if the string is either an IP address, DNS
3172 or host name or NetBIOS name. This uses the name switch in the
3173 smb.conf to determine the order of name resolution.
3175 Added support for ip addr/port to support ADS ldap servers.
3176 the only place we currently care about the port is in the
3177 resolve_hosts() when looking up DC's via SRV RR entries in DNS
3178 **********************************************************************/
3180 static NTSTATUS _internal_resolve_name(const char *name,
3182 const char *sitename,
3183 struct ip_service **return_iplist,
3185 const char **resolve_order)
3188 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
3191 struct sockaddr_storage *ss_list = NULL;
3192 TALLOC_CTX *frame = talloc_stackframe();
3194 *return_iplist = NULL;
3197 DBG_DEBUG("looking up %s#%x (sitename %s)\n",
3198 name, name_type, sitename ? sitename : "(null)");
3200 if (is_ipaddress(name)) {
3201 *return_iplist = SMB_MALLOC_P(struct ip_service);
3202 if (*return_iplist == NULL) {
3203 DBG_ERR("malloc fail !\n");
3205 return NT_STATUS_NO_MEMORY;
3208 /* ignore the port here */
3209 (*return_iplist)->port = PORT_NONE;
3211 /* if it's in the form of an IP address then get the lib to interpret it */
3212 ok = interpret_string_addr(&(*return_iplist)->ss,
3213 name, AI_NUMERICHOST);
3215 DBG_WARNING("interpret_string_addr failed on %s\n",
3217 SAFE_FREE(*return_iplist);
3219 return NT_STATUS_INVALID_PARAMETER;
3221 if (is_zero_addr(&(*return_iplist)->ss)) {
3222 SAFE_FREE(*return_iplist);
3224 return NT_STATUS_UNSUCCESSFUL;
3228 return NT_STATUS_OK;
3231 /* Check name cache */
3233 ok = namecache_fetch(name, name_type, return_iplist, return_count);
3235 *return_count = remove_duplicate_addrs2(*return_iplist,
3237 /* This could be a negative response */
3238 if (*return_count > 0) {
3240 return NT_STATUS_OK;
3243 return NT_STATUS_UNSUCCESSFUL;
3247 /* set the name resolution order */
3249 if (resolve_order && strcmp(resolve_order[0], "NULL") == 0) {
3250 DBG_DEBUG("all lookups disabled\n");
3252 return NT_STATUS_INVALID_PARAMETER;
3255 if (!resolve_order || !resolve_order[0]) {
3256 static const char *host_order[] = { "host", NULL };
3257 resolve_order = host_order;
3260 if ((strlen(name) > MAX_NETBIOSNAME_LEN - 1) ||
3261 (strchr(name, '.') != NULL)) {
3263 * Don't do NBT lookup, the name would not fit anyway
3265 resolve_order = filter_out_nbt_lookup(frame, resolve_order);
3266 if (resolve_order == NULL) {
3268 return NT_STATUS_NO_MEMORY;
3272 /* iterate through the name resolution backends */
3274 for (i=0; resolve_order[i]; i++) {
3275 tok = resolve_order[i];
3277 if((strequal(tok, "host") || strequal(tok, "hosts"))) {
3278 status = resolve_hosts(talloc_tos(),
3283 if (!NT_STATUS_IS_OK(status)) {
3287 } else if(strequal( tok, "kdc")) {
3288 /* deal with KDC_NAME_TYPE names here.
3289 * This will result in a SRV record lookup */
3290 status = resolve_ads(talloc_tos(),
3296 if (!NT_STATUS_IS_OK(status)) {
3299 /* Ensure we don't namecache
3300 * this with the KDC port. */
3301 name_type = KDC_NAME_TYPE;
3303 } else if(strequal( tok, "ads")) {
3304 /* deal with 0x1c and 0x1b names here.
3305 * This will result in a SRV record lookup */
3306 status = resolve_ads(talloc_tos(),
3312 if (!NT_STATUS_IS_OK(status)) {
3316 } else if (strequal(tok, "lmhosts")) {
3317 status = resolve_lmhosts_file_as_sockaddr(
3319 get_dyn_LMHOSTSFILE(),
3324 if (!NT_STATUS_IS_OK(status)) {
3328 } else if (strequal(tok, "wins")) {
3329 /* don't resolve 1D via WINS */
3330 if (name_type == 0x1D) {
3333 status = resolve_wins(talloc_tos(),
3338 if (!NT_STATUS_IS_OK(status)) {
3342 } else if (strequal(tok, "bcast")) {
3343 status = name_resolve_bcast(
3349 if (!NT_STATUS_IS_OK(status)) {
3354 DBG_ERR("unknown name switch type %s\n",
3359 /* All of the resolve_* functions above have returned false. */
3362 SAFE_FREE(*return_iplist);
3369 ok = convert_ss2service(return_iplist, ss_list, return_count);
3372 SAFE_FREE(*return_iplist);
3374 return NT_STATUS_NO_MEMORY;
3377 /* Remove duplicate entries. Some queries, notably #1c (domain
3378 controllers) return the PDC in iplist[0] and then all domain
3379 controllers including the PDC in iplist[1..n]. Iterating over
3380 the iplist when the PDC is down will cause two sets of timeouts. */
3382 *return_count = remove_duplicate_addrs2(*return_iplist, *return_count );
3384 /* Save in name cache */
3385 if ( DEBUGLEVEL >= 100 ) {
3386 for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++) {
3387 char addr[INET6_ADDRSTRLEN];
3388 print_sockaddr(addr, sizeof(addr),
3389 &(*return_iplist)[i].ss);
3390 DEBUG(100, ("Storing name %s of type %d (%s:%d)\n",
3394 (*return_iplist)[i].port));
3398 if (*return_count) {
3399 namecache_store(name, name_type, *return_count, *return_iplist);
3402 /* Display some debugging info */
3404 if ( DEBUGLEVEL >= 10 ) {
3405 DBG_DEBUG("returning %d addresses: ",
3408 for (i = 0; i < *return_count; i++) {
3409 char addr[INET6_ADDRSTRLEN];
3410 print_sockaddr(addr, sizeof(addr),
3411 &(*return_iplist)[i].ss);
3412 DEBUGADD(10, ("%s:%d ",
3414 (*return_iplist)[i].port));
3423 /********************************************************
3424 Wrapper function for _internal_resolve_name() that returns
3426 ********************************************************/
3428 NTSTATUS internal_resolve_name(TALLOC_CTX *ctx,
3431 const char *sitename,
3432 struct ip_service **return_iplist,
3434 const char **resolve_order)
3436 struct ip_service *iplist_malloc = NULL;
3437 struct ip_service *iplist = NULL;
3441 status = _internal_resolve_name(name,
3447 if (!NT_STATUS_IS_OK(status)) {
3448 SAFE_FREE(iplist_malloc);
3454 SAFE_FREE(iplist_malloc);
3455 return NT_STATUS_INVALID_PARAMETER;
3458 status = dup_ip_service_array(ctx,
3462 SAFE_FREE(iplist_malloc);
3463 if (!NT_STATUS_IS_OK(status)) {
3466 *ret_count = (size_t)count;
3467 *return_iplist = iplist;
3468 return NT_STATUS_OK;
3471 /********************************************************
3472 Internal interface to resolve a name into one IP address.
3473 Use this function if the string is either an IP address, DNS
3474 or host name or NetBIOS name. This uses the name switch in the
3475 smb.conf to determine the order of name resolution.
3476 *********************************************************/
3478 bool resolve_name(const char *name,
3479 struct sockaddr_storage *return_ss,
3483 struct ip_service *ss_list = NULL;
3484 char *sitename = NULL;
3487 TALLOC_CTX *frame = NULL;
3489 if (is_ipaddress(name)) {
3490 return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
3493 frame = talloc_stackframe();
3495 sitename = sitename_fetch(frame, lp_realm()); /* wild guess */
3497 status = internal_resolve_name(frame,
3503 lp_name_resolve_order());
3504 if (NT_STATUS_IS_OK(status)) {
3508 for (i=0; i<count; i++) {
3509 struct samba_sockaddr sa = {0};
3512 ok = sockaddr_storage_to_samba_sockaddr(&sa,
3515 TALLOC_FREE(ss_list);
3519 if (!is_zero_addr(&sa.u.ss) &&
3520 !is_broadcast_addr(&sa.u.sa) &&
3521 (sa.u.ss.ss_family == AF_INET)) {
3522 *return_ss = ss_list[i].ss;
3523 TALLOC_FREE(ss_list);
3530 /* only return valid addresses for TCP connections */
3531 for (i=0; i<count; i++) {
3532 struct samba_sockaddr sa = {0};
3535 ok = sockaddr_storage_to_samba_sockaddr(&sa,
3538 TALLOC_FREE(ss_list);
3542 if (!is_zero_addr(&sa.u.ss) &&
3543 !is_broadcast_addr(&sa.u.sa)) {
3544 *return_ss = ss_list[i].ss;
3545 TALLOC_FREE(ss_list);
3552 TALLOC_FREE(ss_list);
3557 /********************************************************
3558 Internal interface to resolve a name into a list of IP addresses.
3559 Use this function if the string is either an IP address, DNS
3560 or host name or NetBIOS name. This uses the name switch in the
3561 smb.conf to determine the order of name resolution.
3562 *********************************************************/
3564 NTSTATUS resolve_name_list(TALLOC_CTX *ctx,
3567 struct sockaddr_storage **return_ss_arr,
3568 unsigned int *p_num_entries)
3570 struct ip_service *ss_list = NULL;
3571 char *sitename = NULL;
3574 unsigned int num_entries = 0;
3575 struct sockaddr_storage *result_arr = NULL;
3578 if (is_ipaddress(name)) {
3579 result_arr = talloc(ctx, struct sockaddr_storage);
3580 if (result_arr == NULL) {
3581 return NT_STATUS_NO_MEMORY;
3583 if (!interpret_string_addr(result_arr, name, AI_NUMERICHOST)) {
3584 TALLOC_FREE(result_arr);
3585 return NT_STATUS_BAD_NETWORK_NAME;
3588 *return_ss_arr = result_arr;
3589 return NT_STATUS_OK;
3592 sitename = sitename_fetch(ctx, lp_realm()); /* wild guess */
3594 status = internal_resolve_name(ctx,
3600 lp_name_resolve_order());
3601 TALLOC_FREE(sitename);
3603 if (!NT_STATUS_IS_OK(status)) {
3607 /* only return valid addresses for TCP connections */
3608 for (i=0, num_entries = 0; i<count; i++) {
3609 struct samba_sockaddr sa = {0};
3612 ok = sockaddr_storage_to_samba_sockaddr(&sa,
3617 if (!is_zero_addr(&sa.u.ss) &&
3618 !is_broadcast_addr(&sa.u.sa)) {
3622 if (num_entries == 0) {
3623 status = NT_STATUS_BAD_NETWORK_NAME;
3627 result_arr = talloc_array(ctx,
3628 struct sockaddr_storage,
3630 if (result_arr == NULL) {
3631 status = NT_STATUS_NO_MEMORY;
3635 for (i=0, num_entries = 0; i<count; i++) {
3636 struct samba_sockaddr sa = {0};
3639 ok = sockaddr_storage_to_samba_sockaddr(&sa,
3644 if (!is_zero_addr(&sa.u.ss) &&
3645 !is_broadcast_addr(&sa.u.sa)) {
3646 result_arr[num_entries++] = ss_list[i].ss;
3650 if (num_entries == 0) {
3651 TALLOC_FREE(result_arr);
3652 status = NT_STATUS_BAD_NETWORK_NAME;
3656 status = NT_STATUS_OK;
3657 *p_num_entries = num_entries;
3658 *return_ss_arr = result_arr;
3660 TALLOC_FREE(ss_list);
3664 /********************************************************
3665 Find the IP address of the master browser or DMB for a workgroup.
3666 *********************************************************/
3668 bool find_master_ip(const char *group, struct sockaddr_storage *master_ss)
3670 struct ip_service *ip_list = NULL;
3674 if (lp_disable_netbios()) {
3675 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
3679 status = internal_resolve_name(talloc_tos(),
3685 lp_name_resolve_order());
3686 if (NT_STATUS_IS_OK(status)) {
3687 *master_ss = ip_list[0].ss;
3688 TALLOC_FREE(ip_list);
3692 TALLOC_FREE(ip_list);
3694 status = internal_resolve_name(talloc_tos(),
3700 lp_name_resolve_order());
3701 if (NT_STATUS_IS_OK(status)) {
3702 *master_ss = ip_list[0].ss;
3703 TALLOC_FREE(ip_list);
3707 TALLOC_FREE(ip_list);
3711 /********************************************************
3712 Get the IP address list of the primary domain controller
3714 *********************************************************/
3716 bool get_pdc_ip(const char *domain, struct sockaddr_storage *pss)
3718 struct ip_service *ip_list = NULL;
3720 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
3721 static const char *ads_order[] = { "ads", NULL };
3722 /* Look up #1B name */
3724 if (lp_security() == SEC_ADS) {
3725 status = internal_resolve_name(talloc_tos(),
3734 if (!NT_STATUS_IS_OK(status) || count == 0) {
3735 TALLOC_FREE(ip_list);
3736 status = internal_resolve_name(talloc_tos(),
3742 lp_name_resolve_order());
3743 if (!NT_STATUS_IS_OK(status)) {
3744 TALLOC_FREE(ip_list);
3749 /* if we get more than 1 IP back we have to assume it is a
3750 multi-homed PDC and not a mess up */
3753 DBG_INFO("PDC has %zu IP addresses!\n", count);
3754 sort_service_list(ip_list, count);
3757 *pss = ip_list[0].ss;
3758 TALLOC_FREE(ip_list);
3762 /* Private enum type for lookups. */
3764 enum dc_lookup_type { DC_NORMAL_LOOKUP, DC_ADS_ONLY, DC_KDC_ONLY };
3766 /********************************************************
3767 Get the IP address list of the domain controllers for
3769 *********************************************************/
3771 static NTSTATUS get_dc_list(TALLOC_CTX *ctx,
3773 const char *sitename,
3774 struct ip_service **ip_list,
3776 enum dc_lookup_type lookup_type,
3779 const char **resolve_order = NULL;
3780 char *saf_servername = NULL;
3781 char *pserver = NULL;
3783 char *port_str = NULL;
3786 size_t num_addresses = 0;
3787 size_t local_count, i;
3788 struct ip_service *return_iplist = NULL;
3789 struct ip_service *auto_ip_list = NULL;
3790 bool done_auto_lookup = false;
3791 size_t auto_count = 0;
3793 TALLOC_CTX *frame = talloc_stackframe();
3794 int auto_name_type = 0x1C;
3801 /* if we are restricted to solely using DNS for looking
3802 up a domain controller, make sure that host lookups
3803 are enabled for the 'name resolve order'. If host lookups
3804 are disabled and ads_only is True, then set the string to
3807 resolve_order = lp_name_resolve_order();
3808 if (!resolve_order) {
3809 status = NT_STATUS_NO_MEMORY;
3812 if (lookup_type == DC_ADS_ONLY) {
3813 if (str_list_check_ci(resolve_order, "host")) {
3814 static const char *ads_order[] = { "ads", NULL };
3815 resolve_order = ads_order;
3817 /* DNS SRV lookups used by the ads resolver
3818 are already sorted by priority and weight */
3821 /* this is quite bizarre! */
3822 static const char *null_order[] = { "NULL", NULL };
3823 resolve_order = null_order;
3825 } else if (lookup_type == DC_KDC_ONLY) {
3826 static const char *kdc_order[] = { "kdc", NULL };
3827 /* DNS SRV lookups used by the ads/kdc resolver
3828 are already sorted by priority and weight */
3830 resolve_order = kdc_order;
3831 auto_name_type = KDC_NAME_TYPE;
3834 /* fetch the server we have affinity for. Add the
3835 'password server' list to a search for our domain controllers */
3837 saf_servername = saf_fetch(frame, domain);
3839 if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
3840 pserver = talloc_asprintf(frame, "%s, %s",
3841 saf_servername ? saf_servername : "",
3842 lp_password_server());
3844 pserver = talloc_asprintf(frame, "%s, *",
3845 saf_servername ? saf_servername : "");
3848 TALLOC_FREE(saf_servername);
3850 status = NT_STATUS_NO_MEMORY;
3854 DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver ));
3857 * if '*' appears in the "password server" list then add
3858 * an auto lookup to the list of manually configured
3859 * DC's. If any DC is listed by name, then the list should be
3860 * considered to be ordered
3864 while (next_token_talloc(frame, &p, &name, LIST_SEP)) {
3865 if (!done_auto_lookup && strequal(name, "*")) {
3866 done_auto_lookup = true;
3868 status = internal_resolve_name(frame,
3875 if (!NT_STATUS_IS_OK(status)) {
3879 if (num_addresses + auto_count < num_addresses) {
3880 TALLOC_FREE(auto_ip_list);
3881 status = NT_STATUS_INVALID_PARAMETER;
3884 num_addresses += auto_count;
3885 DBG_DEBUG("Adding %zu DC's from auto lookup\n",
3889 if (num_addresses + 1 < num_addresses) {
3890 TALLOC_FREE(auto_ip_list);
3891 status = NT_STATUS_INVALID_PARAMETER;
3898 /* if we have no addresses and haven't done the auto lookup, then
3899 just return the list of DC's. Or maybe we just failed. */
3901 if (num_addresses == 0) {
3902 struct ip_service *dc_iplist = NULL;
3903 size_t dc_count = 0;
3905 if (done_auto_lookup) {
3906 DEBUG(4,("get_dc_list: no servers found\n"));
3907 status = NT_STATUS_NO_LOGON_SERVERS;
3910 /* talloc off frame, only move to ctx on success. */
3911 status = internal_resolve_name(frame,
3918 if (NT_STATUS_IS_OK(status)) {
3919 *ip_list = talloc_move(ctx, &dc_iplist);
3920 *ret_count = dc_count;
3922 TALLOC_FREE(dc_iplist);
3926 return_iplist = talloc_zero_array(ctx,
3929 if (return_iplist == NULL) {
3930 DEBUG(3,("get_dc_list: malloc fail !\n"));
3931 status = NT_STATUS_NO_MEMORY;
3938 /* fill in the return list now with real IP's */
3940 while ((local_count<num_addresses) &&
3941 next_token_talloc(frame, &p, &name, LIST_SEP)) {
3942 struct samba_sockaddr name_sa = {0};
3944 /* copy any addresses from the auto lookup */
3946 if (strequal(name, "*")) {
3948 for (j=0; j<auto_count; j++) {
3949 char addr[INET6_ADDRSTRLEN];
3950 print_sockaddr(addr,
3952 &auto_ip_list[j].ss);
3953 /* Check for and don't copy any
3954 * known bad DC IP's. */
3955 if(!NT_STATUS_IS_OK(check_negative_conn_cache(
3958 DEBUG(5,("get_dc_list: "
3959 "negative entry %s removed "
3964 return_iplist[local_count].ss =
3966 return_iplist[local_count].port =
3967 auto_ip_list[j].port;
3973 /* added support for address:port syntax for ads
3974 * (not that I think anyone will ever run the LDAP
3975 * server in an AD domain on something other than
3977 * However, the port should not be used for kerberos
3980 port = (lookup_type == DC_ADS_ONLY) ? LDAP_PORT :
3981 ((lookup_type == DC_KDC_ONLY) ? DEFAULT_KRB5_PORT :
3983 if ((port_str=strchr(name, ':')) != NULL) {
3985 if (lookup_type != DC_KDC_ONLY) {
3987 port = atoi(port_str);
3991 /* explicit lookup; resolve_name() will
3992 * handle names & IP addresses */
3993 if (resolve_name(name, &name_sa.u.ss, 0x20, true)) {
3994 char addr[INET6_ADDRSTRLEN];
3998 * Ensure we set sa_socklen correctly.
3999 * Doesn't matter now, but eventually we
4000 * will remove ip_service and return samba_sockaddr
4003 ok = sockaddr_storage_to_samba_sockaddr(
4007 status = NT_STATUS_INVALID_ADDRESS;
4011 print_sockaddr(addr,
4015 /* Check for and don't copy any known bad DC IP's. */
4016 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain,
4018 DEBUG(5,("get_dc_list: negative entry %s "
4019 "removed from DC list\n",
4024 return_iplist[local_count].ss = name_sa.u.ss;
4025 return_iplist[local_count].port = port;
4031 /* need to remove duplicates in the list if we have any
4032 explicit password servers */
4034 local_count = remove_duplicate_addrs2(return_iplist, local_count );
4036 /* For DC's we always prioritize IPv4 due to W2K3 not
4037 * supporting LDAP, KRB5 or CLDAP over IPv6. */
4039 if (local_count && return_iplist) {
4040 prioritize_ipv4_list(return_iplist, local_count);
4043 if ( DEBUGLEVEL >= 4 ) {
4044 DEBUG(4,("get_dc_list: returning %zu ip addresses "
4045 "in an %sordered list\n",
4047 *ordered ? "":"un"));
4048 DEBUG(4,("get_dc_list: "));
4049 for ( i=0; i<local_count; i++ ) {
4050 char addr[INET6_ADDRSTRLEN];
4051 print_sockaddr(addr,
4053 &return_iplist[i].ss);
4054 DEBUGADD(4,("%s:%d ", addr, return_iplist[i].port ));
4059 *ip_list = return_iplist;
4060 *ret_count = local_count;
4062 status = (*ret_count != 0 ? NT_STATUS_OK : NT_STATUS_NO_LOGON_SERVERS);
4066 if (!NT_STATUS_IS_OK(status)) {
4067 TALLOC_FREE(return_iplist);
4072 TALLOC_FREE(auto_ip_list);
4077 /*********************************************************************
4079 Small wrapper function to get the DC list and sort it if neccessary.
4080 *********************************************************************/
4082 NTSTATUS get_sorted_dc_list(TALLOC_CTX *ctx,
4084 const char *sitename,
4085 struct ip_service **ip_list_ret,
4089 bool ordered = false;
4091 enum dc_lookup_type lookup_type = DC_NORMAL_LOOKUP;
4092 struct ip_service *ip_list = NULL;
4095 DBG_INFO("attempting lookup for name %s (sitename %s)\n",
4097 sitename ? sitename : "NULL");
4100 lookup_type = DC_ADS_ONLY;
4103 status = get_dc_list(ctx,
4110 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_LOGON_SERVERS)
4112 DBG_NOTICE("no server for name %s available"
4113 " in site %s, fallback to all servers\n",
4116 status = get_dc_list(ctx,
4125 if (!NT_STATUS_IS_OK(status)) {
4129 /* only sort if we don't already have an ordered list */
4131 sort_service_list(ip_list, count);
4135 *ip_list_ret = ip_list;
4139 /*********************************************************************
4141 Get the KDC list - re-use all the logic in get_dc_list.
4142 *********************************************************************/
4144 NTSTATUS get_kdc_list(TALLOC_CTX *ctx,
4146 const char *sitename,
4147 struct ip_service **ip_list_ret,
4151 struct ip_service *ip_list = NULL;
4152 bool ordered = false;
4155 status = get_dc_list(ctx,
4163 if (!NT_STATUS_IS_OK(status)) {
4167 /* only sort if we don't already have an ordered list */
4169 sort_service_list(ip_list, count);
4173 *ip_list_ret = ip_list;