2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2007.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "libads/sitename_cache.h"
24 #include "libads/dns.h"
25 #include "../libcli/netlogon/netlogon.h"
26 #include "lib/async_req/async_sock.h"
27 #include "libsmb/nmblib.h"
29 /* nmbd.c sets this to True. */
30 bool global_in_nmbd = False;
32 /****************************
33 * SERVER AFFINITY ROUTINES *
34 ****************************/
36 /* Server affinity is the concept of preferring the last domain
37 controller with whom you had a successful conversation */
39 /****************************************************************************
40 ****************************************************************************/
41 #define SAFKEY_FMT "SAF/DOMAIN/%s"
43 #define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
44 #define SAFJOIN_TTL 3600
46 static char *saf_key(TALLOC_CTX *mem_ctx, const char *domain)
48 return talloc_asprintf_strupper_m(mem_ctx, SAFKEY_FMT, domain);
51 static char *saf_join_key(const char *domain)
55 asprintf_strupper_m(&keystr, SAFJOINKEY_FMT, domain);
60 /****************************************************************************
61 ****************************************************************************/
63 bool saf_store( const char *domain, const char *servername )
69 if ( !domain || !servername ) {
70 DEBUG(2,("saf_store: "
71 "Refusing to store empty domain or servername!\n"));
75 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
76 DEBUG(0,("saf_store: "
77 "refusing to store 0 length domain or servername!\n"));
81 key = saf_key(talloc_tos(), domain);
83 DEBUG(1, ("saf_key() failed\n"));
86 expire = time( NULL ) + lp_parm_int(-1, "saf","ttl", SAF_TTL);
88 DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
89 domain, servername, (unsigned int)expire ));
91 ret = gencache_set( key, servername, expire );
98 bool saf_join_store( const char *domain, const char *servername )
104 if ( !domain || !servername ) {
105 DEBUG(2,("saf_join_store: Refusing to store empty domain or servername!\n"));
109 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
110 DEBUG(0,("saf_join_store: refusing to store 0 length domain or servername!\n"));
114 key = saf_join_key( domain );
115 expire = time( NULL ) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL);
117 DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
118 domain, servername, (unsigned int)expire ));
120 ret = gencache_set( key, servername, expire );
127 bool saf_delete( const char *domain )
133 DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
137 key = saf_join_key(domain);
138 ret = gencache_del(key);
142 DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain ));
145 key = saf_key(talloc_tos(), domain);
147 DEBUG(1, ("saf_key() failed\n"));
150 ret = gencache_del(key);
154 DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
160 /****************************************************************************
161 ****************************************************************************/
163 char *saf_fetch( const char *domain )
170 if ( !domain || strlen(domain) == 0) {
171 DEBUG(2,("saf_fetch: Empty domain name!\n"));
175 key = saf_join_key( domain );
177 ret = gencache_get( key, &server, &timeout );
182 DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
187 key = saf_key(talloc_tos(), domain);
189 DEBUG(1, ("saf_key() failed\n"));
193 ret = gencache_get( key, &server, &timeout );
198 DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
201 DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
208 static void set_socket_addr_v4(struct sockaddr_storage *addr)
210 if (!interpret_string_addr(addr, lp_socket_address(),
211 AI_NUMERICHOST|AI_PASSIVE)) {
214 if (addr->ss_family != AF_INET) {
219 static struct in_addr my_socket_addr_v4(void)
221 struct sockaddr_storage my_addr;
222 struct sockaddr_in *in_addr = (struct sockaddr_in *)((char *)&my_addr);
224 set_socket_addr_v4(&my_addr);
225 return in_addr->sin_addr;
228 /****************************************************************************
229 Generate a random trn_id.
230 ****************************************************************************/
232 static int generate_trn_id(void)
236 generate_random_buffer((uint8 *)&id, sizeof(id));
238 return id % (unsigned)0x7FFF;
241 /****************************************************************************
242 Parse a node status response into an array of structures.
243 ****************************************************************************/
245 static struct node_status *parse_node_status(TALLOC_CTX *mem_ctx, char *p,
247 struct node_status_extra *extra)
249 struct node_status *ret;
252 *num_names = CVAL(p,0);
257 ret = talloc_array(mem_ctx, struct node_status,*num_names);
262 for (i=0;i< *num_names;i++) {
263 StrnCpy(ret[i].name,p,15);
264 trim_char(ret[i].name,'\0',' ');
265 ret[i].type = CVAL(p,15);
266 ret[i].flags = p[16];
268 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
269 ret[i].type, ret[i].flags));
272 * Also, pick up the MAC address ...
275 memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
280 struct sock_packet_read_state {
281 struct tevent_context *ev;
282 enum packet_type type;
285 struct nb_packet_reader *reader;
286 struct tevent_req *reader_req;
289 struct tevent_req *socket_req;
291 struct sockaddr_storage addr;
294 bool (*validator)(struct packet_struct *p,
298 struct packet_struct *packet;
301 static int sock_packet_read_state_destructor(struct sock_packet_read_state *s);
302 static void sock_packet_read_got_packet(struct tevent_req *subreq);
303 static void sock_packet_read_got_socket(struct tevent_req *subreq);
305 static struct tevent_req *sock_packet_read_send(
307 struct tevent_context *ev,
308 int sock, /* dgram socket */
309 struct nb_packet_reader *reader,
310 enum packet_type type,
312 bool (*validator)(struct packet_struct *p, void *private_data),
315 struct tevent_req *req;
316 struct sock_packet_read_state *state;
318 req = tevent_req_create(mem_ctx, &state,
319 struct sock_packet_read_state);
323 talloc_set_destructor(state, sock_packet_read_state_destructor);
325 state->reader = reader;
328 state->trn_id = trn_id;
329 state->validator = validator;
330 state->private_data = private_data;
332 if (reader != NULL) {
333 state->reader_req = nb_packet_read_send(state, ev, reader);
334 if (tevent_req_nomem(state->reader_req, req)) {
335 return tevent_req_post(req, ev);
337 tevent_req_set_callback(
338 state->reader_req, sock_packet_read_got_packet, req);
341 state->addr_len = sizeof(state->addr);
342 state->socket_req = recvfrom_send(state, ev, sock,
343 state->buf, sizeof(state->buf), 0,
344 &state->addr, &state->addr_len);
345 if (tevent_req_nomem(state->socket_req, req)) {
346 return tevent_req_post(req, ev);
348 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
354 static int sock_packet_read_state_destructor(struct sock_packet_read_state *s)
356 if (s->packet != NULL) {
357 free_packet(s->packet);
363 static void sock_packet_read_got_packet(struct tevent_req *subreq)
365 struct tevent_req *req = tevent_req_callback_data(
366 subreq, struct tevent_req);
367 struct sock_packet_read_state *state = tevent_req_data(
368 req, struct sock_packet_read_state);
371 status = nb_packet_read_recv(subreq, &state->packet);
373 TALLOC_FREE(state->reader_req);
375 if (!NT_STATUS_IS_OK(status)) {
376 if (state->socket_req != NULL) {
378 * Still waiting for socket
383 * Both socket and packet reader failed
385 tevent_req_nterror(req, status);
389 if ((state->validator != NULL) &&
390 !state->validator(state->packet, state->private_data)) {
391 DEBUG(10, ("validator failed\n"));
393 free_packet(state->packet);
394 state->packet = NULL;
396 state->reader_req = nb_packet_read_send(state, state->ev,
398 if (tevent_req_nomem(state->reader_req, req)) {
401 tevent_req_set_callback(
402 state->reader_req, sock_packet_read_got_packet, req);
406 TALLOC_FREE(state->socket_req);
407 tevent_req_done(req);
410 static void sock_packet_read_got_socket(struct tevent_req *subreq)
412 struct tevent_req *req = tevent_req_callback_data(
413 subreq, struct tevent_req);
414 struct sock_packet_read_state *state = tevent_req_data(
415 req, struct sock_packet_read_state);
416 struct sockaddr_in *in_addr;
420 received = recvfrom_recv(subreq, &err);
422 TALLOC_FREE(state->socket_req);
424 if (received == -1) {
425 if (state->reader_req != NULL) {
427 * Still waiting for reader
432 * Both socket and reader failed
434 tevent_req_nterror(req, map_nt_error_from_unix(err));
437 if (state->addr.ss_family != AF_INET) {
440 in_addr = (struct sockaddr_in *)(void *)&state->addr;
442 state->packet = parse_packet((char *)state->buf, received, state->type,
443 in_addr->sin_addr, in_addr->sin_port);
444 if (state->packet == NULL) {
445 DEBUG(10, ("parse_packet failed\n"));
448 if ((state->trn_id != -1) &&
449 (state->trn_id != packet_trn_id(state->packet))) {
450 DEBUG(10, ("Expected transaction id %d, got %d\n",
451 state->trn_id, packet_trn_id(state->packet)));
455 if ((state->validator != NULL) &&
456 !state->validator(state->packet, state->private_data)) {
457 DEBUG(10, ("validator failed\n"));
461 tevent_req_done(req);
465 if (state->packet != NULL) {
466 free_packet(state->packet);
467 state->packet = NULL;
469 state->socket_req = recvfrom_send(state, state->ev, state->sock,
470 state->buf, sizeof(state->buf), 0,
471 &state->addr, &state->addr_len);
472 if (tevent_req_nomem(state->socket_req, req)) {
475 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
479 static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
480 struct packet_struct **ppacket)
482 struct sock_packet_read_state *state = tevent_req_data(
483 req, struct sock_packet_read_state);
486 if (tevent_req_is_nterror(req, &status)) {
489 *ppacket = state->packet;
490 state->packet = NULL;
494 struct nb_trans_state {
495 struct tevent_context *ev;
497 struct nb_packet_reader *reader;
499 const struct sockaddr_storage *dst_addr;
502 enum packet_type type;
505 bool (*validator)(struct packet_struct *p,
509 struct packet_struct *packet;
512 static int nb_trans_state_destructor(struct nb_trans_state *s);
513 static void nb_trans_got_reader(struct tevent_req *subreq);
514 static void nb_trans_done(struct tevent_req *subreq);
515 static void nb_trans_sent(struct tevent_req *subreq);
516 static void nb_trans_send_next(struct tevent_req *subreq);
518 static struct tevent_req *nb_trans_send(
520 struct tevent_context *ev,
521 const struct sockaddr_storage *my_addr,
522 const struct sockaddr_storage *dst_addr,
524 uint8_t *buf, size_t buflen,
525 enum packet_type type, int trn_id,
526 bool (*validator)(struct packet_struct *p,
530 struct tevent_req *req, *subreq;
531 struct nb_trans_state *state;
533 req = tevent_req_create(mem_ctx, &state, struct nb_trans_state);
537 talloc_set_destructor(state, nb_trans_state_destructor);
539 state->dst_addr = dst_addr;
541 state->buflen = buflen;
543 state->trn_id = trn_id;
544 state->validator = validator;
545 state->private_data = private_data;
547 state->sock = open_socket_in(SOCK_DGRAM, 0, 3, my_addr, True);
548 if (state->sock == -1) {
549 tevent_req_nterror(req, map_nt_error_from_unix(errno));
550 DEBUG(10, ("open_socket_in failed: %s\n", strerror(errno)));
551 return tevent_req_post(req, ev);
555 set_socket_options(state->sock,"SO_BROADCAST");
558 subreq = nb_packet_reader_send(state, ev, type, state->trn_id, NULL);
559 if (tevent_req_nomem(subreq, req)) {
560 return tevent_req_post(req, ev);
562 tevent_req_set_callback(subreq, nb_trans_got_reader, req);
566 static int nb_trans_state_destructor(struct nb_trans_state *s)
572 if (s->packet != NULL) {
573 free_packet(s->packet);
579 static void nb_trans_got_reader(struct tevent_req *subreq)
581 struct tevent_req *req = tevent_req_callback_data(
582 subreq, struct tevent_req);
583 struct nb_trans_state *state = tevent_req_data(
584 req, struct nb_trans_state);
587 status = nb_packet_reader_recv(subreq, state, &state->reader);
590 if (!NT_STATUS_IS_OK(status)) {
591 DEBUG(10, ("nmbd not around\n"));
592 state->reader = NULL;
595 subreq = sock_packet_read_send(
596 state, state->ev, state->sock,
597 state->reader, state->type, state->trn_id,
598 state->validator, state->private_data);
599 if (tevent_req_nomem(subreq, req)) {
602 tevent_req_set_callback(subreq, nb_trans_done, req);
604 subreq = sendto_send(state, state->ev, state->sock,
605 state->buf, state->buflen, 0, state->dst_addr);
606 if (tevent_req_nomem(subreq, req)) {
609 tevent_req_set_callback(subreq, nb_trans_sent, req);
612 static void nb_trans_sent(struct tevent_req *subreq)
614 struct tevent_req *req = tevent_req_callback_data(
615 subreq, struct tevent_req);
616 struct nb_trans_state *state = tevent_req_data(
617 req, struct nb_trans_state);
621 sent = sendto_recv(subreq, &err);
624 DEBUG(10, ("sendto failed: %s\n", strerror(err)));
625 tevent_req_nterror(req, map_nt_error_from_unix(err));
628 subreq = tevent_wakeup_send(state, state->ev,
629 timeval_current_ofs(1, 0));
630 if (tevent_req_nomem(subreq, req)) {
633 tevent_req_set_callback(subreq, nb_trans_send_next, req);
636 static void nb_trans_send_next(struct tevent_req *subreq)
638 struct tevent_req *req = tevent_req_callback_data(
639 subreq, struct tevent_req);
640 struct nb_trans_state *state = tevent_req_data(
641 req, struct nb_trans_state);
644 ret = tevent_wakeup_recv(subreq);
647 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
650 subreq = sendto_send(state, state->ev, state->sock,
651 state->buf, state->buflen, 0, state->dst_addr);
652 if (tevent_req_nomem(subreq, req)) {
655 tevent_req_set_callback(subreq, nb_trans_sent, req);
658 static void nb_trans_done(struct tevent_req *subreq)
660 struct tevent_req *req = tevent_req_callback_data(
661 subreq, struct tevent_req);
662 struct nb_trans_state *state = tevent_req_data(
663 req, struct nb_trans_state);
666 status = sock_packet_read_recv(subreq, &state->packet);
668 if (tevent_req_nterror(req, status)) {
671 tevent_req_done(req);
674 static NTSTATUS nb_trans_recv(struct tevent_req *req,
675 struct packet_struct **ppacket)
677 struct nb_trans_state *state = tevent_req_data(
678 req, struct nb_trans_state);
681 if (tevent_req_is_nterror(req, &status)) {
684 *ppacket = state->packet;
685 state->packet = NULL;
689 /****************************************************************************
690 Do a NBT node status query on an open socket and return an array of
691 structures holding the returned names or NULL if the query failed.
692 **************************************************************************/
694 struct node_status_query_state {
695 struct sockaddr_storage my_addr;
696 struct sockaddr_storage addr;
699 struct packet_struct *packet;
702 static int node_status_query_state_destructor(
703 struct node_status_query_state *s);
704 static bool node_status_query_validator(struct packet_struct *p,
706 static void node_status_query_done(struct tevent_req *subreq);
708 struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
709 struct tevent_context *ev,
710 struct nmb_name *name,
711 const struct sockaddr_storage *addr)
713 struct tevent_req *req, *subreq;
714 struct node_status_query_state *state;
715 struct packet_struct p;
716 struct nmb_packet *nmb = &p.packet.nmb;
717 struct sockaddr_in *in_addr;
719 req = tevent_req_create(mem_ctx, &state,
720 struct node_status_query_state);
724 talloc_set_destructor(state, node_status_query_state_destructor);
726 if (addr->ss_family != AF_INET) {
727 /* Can't do node status to IPv6 */
728 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
729 return tevent_req_post(req, ev);
733 in_addr = (struct sockaddr_in *)(void *)&state->addr;
734 in_addr->sin_port = htons(NMB_PORT);
736 set_socket_addr_v4(&state->my_addr);
739 nmb->header.name_trn_id = generate_trn_id();
740 nmb->header.opcode = 0;
741 nmb->header.response = false;
742 nmb->header.nm_flags.bcast = false;
743 nmb->header.nm_flags.recursion_available = false;
744 nmb->header.nm_flags.recursion_desired = false;
745 nmb->header.nm_flags.trunc = false;
746 nmb->header.nm_flags.authoritative = false;
747 nmb->header.rcode = 0;
748 nmb->header.qdcount = 1;
749 nmb->header.ancount = 0;
750 nmb->header.nscount = 0;
751 nmb->header.arcount = 0;
752 nmb->question.question_name = *name;
753 nmb->question.question_type = 0x21;
754 nmb->question.question_class = 0x1;
756 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
758 if (state->buflen == 0) {
759 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
760 DEBUG(10, ("build_packet failed\n"));
761 return tevent_req_post(req, ev);
764 subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, false,
765 state->buf, state->buflen,
766 NMB_PACKET, nmb->header.name_trn_id,
767 node_status_query_validator, NULL);
768 if (tevent_req_nomem(subreq, req)) {
769 DEBUG(10, ("nb_trans_send failed\n"));
770 return tevent_req_post(req, ev);
772 if (!tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0))) {
773 return tevent_req_post(req, ev);
775 tevent_req_set_callback(subreq, node_status_query_done, req);
779 static bool node_status_query_validator(struct packet_struct *p,
782 struct nmb_packet *nmb = &p->packet.nmb;
785 if (nmb->header.opcode != 0 ||
786 nmb->header.nm_flags.bcast ||
788 !nmb->header.ancount ||
789 nmb->answers->rr_type != 0x21) {
791 * XXXX what do we do with this? could be a redirect,
792 * but we'll discard it for the moment
799 static int node_status_query_state_destructor(
800 struct node_status_query_state *s)
802 if (s->packet != NULL) {
803 free_packet(s->packet);
809 static void node_status_query_done(struct tevent_req *subreq)
811 struct tevent_req *req = tevent_req_callback_data(
812 subreq, struct tevent_req);
813 struct node_status_query_state *state = tevent_req_data(
814 req, struct node_status_query_state);
817 status = nb_trans_recv(subreq, &state->packet);
819 if (tevent_req_nterror(req, status)) {
822 tevent_req_done(req);
825 NTSTATUS node_status_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
826 struct node_status **pnode_status,
828 struct node_status_extra *extra)
830 struct node_status_query_state *state = tevent_req_data(
831 req, struct node_status_query_state);
832 struct node_status *node_status;
836 if (tevent_req_is_nterror(req, &status)) {
839 node_status = parse_node_status(
840 mem_ctx, &state->packet->packet.nmb.answers->rdata[0],
842 if (node_status == NULL) {
843 return NT_STATUS_NO_MEMORY;
845 *pnode_status = node_status;
846 *pnum_names = num_names;
850 NTSTATUS node_status_query(TALLOC_CTX *mem_ctx, struct nmb_name *name,
851 const struct sockaddr_storage *addr,
852 struct node_status **pnode_status,
854 struct node_status_extra *extra)
856 TALLOC_CTX *frame = talloc_stackframe();
857 struct tevent_context *ev;
858 struct tevent_req *req;
859 NTSTATUS status = NT_STATUS_NO_MEMORY;
861 ev = tevent_context_init(frame);
865 req = node_status_query_send(ev, ev, name, addr);
869 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
872 status = node_status_query_recv(req, mem_ctx, pnode_status,
879 /****************************************************************************
880 Find the first type XX name in a node status reply - used for finding
881 a servers name given its IP. Return the matched name in *name.
882 **************************************************************************/
884 bool name_status_find(const char *q_name,
887 const struct sockaddr_storage *to_ss,
890 char addr[INET6_ADDRSTRLEN];
891 struct sockaddr_storage ss;
892 struct node_status *addrs = NULL;
893 struct nmb_name nname;
898 if (lp_disable_netbios()) {
899 DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n",
904 print_sockaddr(addr, sizeof(addr), to_ss);
906 DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
909 /* Check the cache first. */
911 if (namecache_status_fetch(q_name, q_type, type, to_ss, name)) {
915 if (to_ss->ss_family != AF_INET) {
916 /* Can't do node status to IPv6 */
920 set_socket_addr_v4(&ss);
922 /* W2K PDC's seem not to respond to '*'#0. JRA */
923 make_nmb_name(&nname, q_name, q_type);
924 status = node_status_query(talloc_tos(), &nname, to_ss,
925 &addrs, &count, NULL);
926 if (!NT_STATUS_IS_OK(status)) {
930 for (i=0;i<count;i++) {
931 /* Find first one of the requested type that's not a GROUP. */
932 if (addrs[i].type == type && ! (addrs[i].flags & 0x80))
938 pull_ascii_nstring(name, sizeof(fstring), addrs[i].name);
940 /* Store the result in the cache. */
941 /* but don't store an entry for 0x1c names here. Here we have
942 a single host and DOMAIN<0x1c> names should be a list of hosts */
944 if ( q_type != 0x1c ) {
945 namecache_status_store(q_name, q_type, type, to_ss, name);
953 DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
956 DEBUGADD(10, (", name %s ip address is %s", name, addr));
964 comparison function used by sort_addr_list
967 static int addr_compare(const struct sockaddr_storage *ss1,
968 const struct sockaddr_storage *ss2)
970 int max_bits1=0, max_bits2=0;
971 int num_interfaces = iface_count();
974 /* Sort IPv4 addresses first. */
975 if (ss1->ss_family != ss2->ss_family) {
976 if (ss2->ss_family == AF_INET) {
983 /* Here we know both addresses are of the same
986 for (i=0;i<num_interfaces;i++) {
987 const struct sockaddr_storage *pss = iface_n_bcast(i);
988 const unsigned char *p_ss1 = NULL;
989 const unsigned char *p_ss2 = NULL;
990 const unsigned char *p_if = NULL;
994 if (pss->ss_family != ss1->ss_family) {
995 /* Ignore interfaces of the wrong type. */
998 if (pss->ss_family == AF_INET) {
999 p_if = (const unsigned char *)
1000 &((const struct sockaddr_in *)pss)->sin_addr;
1001 p_ss1 = (const unsigned char *)
1002 &((const struct sockaddr_in *)ss1)->sin_addr;
1003 p_ss2 = (const unsigned char *)
1004 &((const struct sockaddr_in *)ss2)->sin_addr;
1007 #if defined(HAVE_IPV6)
1008 if (pss->ss_family == AF_INET6) {
1009 p_if = (const unsigned char *)
1010 &((const struct sockaddr_in6 *)pss)->sin6_addr;
1011 p_ss1 = (const unsigned char *)
1012 &((const struct sockaddr_in6 *)ss1)->sin6_addr;
1013 p_ss2 = (const unsigned char *)
1014 &((const struct sockaddr_in6 *)ss2)->sin6_addr;
1018 if (!p_ss1 || !p_ss2 || !p_if || len == 0) {
1021 bits1 = matching_len_bits(p_ss1, p_if, len);
1022 bits2 = matching_len_bits(p_ss2, p_if, len);
1023 max_bits1 = MAX(bits1, max_bits1);
1024 max_bits2 = MAX(bits2, max_bits2);
1027 /* Bias towards directly reachable IPs */
1028 if (iface_local((const struct sockaddr *)ss1)) {
1029 if (ss1->ss_family == AF_INET) {
1035 if (iface_local((const struct sockaddr *)ss2)) {
1036 if (ss2->ss_family == AF_INET) {
1042 return max_bits2 - max_bits1;
1045 /*******************************************************************
1046 compare 2 ldap IPs by nearness to our interfaces - used in qsort
1047 *******************************************************************/
1049 int ip_service_compare(struct ip_service *ss1, struct ip_service *ss2)
1053 if ((result = addr_compare(&ss1->ss, &ss2->ss)) != 0) {
1057 if (ss1->port > ss2->port) {
1061 if (ss1->port < ss2->port) {
1069 sort an IP list so that names that are close to one of our interfaces
1070 are at the top. This prevents the problem where a WINS server returns an IP
1071 that is not reachable from our subnet as the first match
1074 static void sort_addr_list(struct sockaddr_storage *sslist, int count)
1080 TYPESAFE_QSORT(sslist, count, addr_compare);
1083 static void sort_service_list(struct ip_service *servlist, int count)
1089 TYPESAFE_QSORT(servlist, count, ip_service_compare);
1092 /**********************************************************************
1093 Remove any duplicate address/port pairs in the list
1094 *********************************************************************/
1096 static int remove_duplicate_addrs2(struct ip_service *iplist, int count )
1100 DEBUG(10,("remove_duplicate_addrs2: "
1101 "looking for duplicate address/port pairs\n"));
1103 /* one loop to remove duplicates */
1104 for ( i=0; i<count; i++ ) {
1105 if ( is_zero_addr(&iplist[i].ss)) {
1109 for ( j=i+1; j<count; j++ ) {
1110 if (sockaddr_equal((struct sockaddr *)(void *)&iplist[i].ss,
1111 (struct sockaddr *)(void *)&iplist[j].ss) &&
1112 iplist[i].port == iplist[j].port) {
1113 zero_sockaddr(&iplist[j].ss);
1118 /* one loop to clean up any holes we left */
1119 /* first ip should never be a zero_ip() */
1120 for (i = 0; i<count; ) {
1121 if (is_zero_addr(&iplist[i].ss) ) {
1123 memmove(&iplist[i], &iplist[i+1],
1124 (count - i - 1)*sizeof(iplist[i]));
1135 static bool prioritize_ipv4_list(struct ip_service *iplist, int count)
1137 TALLOC_CTX *frame = talloc_stackframe();
1138 struct ip_service *iplist_new = talloc_array(frame, struct ip_service, count);
1141 if (iplist_new == NULL) {
1148 /* Copy IPv4 first. */
1149 for (i = 0; i < count; i++) {
1150 if (iplist[i].ss.ss_family == AF_INET) {
1151 iplist_new[j++] = iplist[i];
1156 for (i = 0; i < count; i++) {
1157 if (iplist[i].ss.ss_family != AF_INET) {
1158 iplist_new[j++] = iplist[i];
1162 memcpy(iplist, iplist_new, sizeof(struct ip_service)*count);
1167 /****************************************************************************
1168 Do a netbios name query to find someones IP.
1169 Returns an array of IP addresses or NULL if none.
1170 *count will be set to the number of addresses returned.
1171 *timed_out is set if we failed by timing out
1172 ****************************************************************************/
1174 struct name_query_state {
1175 struct sockaddr_storage my_addr;
1176 struct sockaddr_storage addr;
1183 NTSTATUS validate_error;
1186 struct sockaddr_storage *addrs;
1190 static bool name_query_validator(struct packet_struct *p, void *private_data);
1191 static void name_query_done(struct tevent_req *subreq);
1193 struct tevent_req *name_query_send(TALLOC_CTX *mem_ctx,
1194 struct tevent_context *ev,
1195 const char *name, int name_type,
1196 bool bcast, bool recurse,
1197 const struct sockaddr_storage *addr)
1199 struct tevent_req *req, *subreq;
1200 struct name_query_state *state;
1201 struct packet_struct p;
1202 struct nmb_packet *nmb = &p.packet.nmb;
1203 struct sockaddr_in *in_addr;
1205 req = tevent_req_create(mem_ctx, &state, struct name_query_state);
1209 state->bcast = bcast;
1211 if (addr->ss_family != AF_INET) {
1212 /* Can't do node status to IPv6 */
1213 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1214 return tevent_req_post(req, ev);
1217 if (lp_disable_netbios()) {
1218 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
1220 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
1221 return tevent_req_post(req, ev);
1224 state->addr = *addr;
1225 in_addr = (struct sockaddr_in *)(void *)&state->addr;
1226 in_addr->sin_port = htons(NMB_PORT);
1228 set_socket_addr_v4(&state->my_addr);
1231 nmb->header.name_trn_id = generate_trn_id();
1232 nmb->header.opcode = 0;
1233 nmb->header.response = false;
1234 nmb->header.nm_flags.bcast = bcast;
1235 nmb->header.nm_flags.recursion_available = false;
1236 nmb->header.nm_flags.recursion_desired = recurse;
1237 nmb->header.nm_flags.trunc = false;
1238 nmb->header.nm_flags.authoritative = false;
1239 nmb->header.rcode = 0;
1240 nmb->header.qdcount = 1;
1241 nmb->header.ancount = 0;
1242 nmb->header.nscount = 0;
1243 nmb->header.arcount = 0;
1245 make_nmb_name(&nmb->question.question_name,name,name_type);
1247 nmb->question.question_type = 0x20;
1248 nmb->question.question_class = 0x1;
1250 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
1252 if (state->buflen == 0) {
1253 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1254 DEBUG(10, ("build_packet failed\n"));
1255 return tevent_req_post(req, ev);
1258 subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, bcast,
1259 state->buf, state->buflen,
1260 NMB_PACKET, nmb->header.name_trn_id,
1261 name_query_validator, state);
1262 if (tevent_req_nomem(subreq, req)) {
1263 DEBUG(10, ("nb_trans_send failed\n"));
1264 return tevent_req_post(req, ev);
1266 tevent_req_set_callback(subreq, name_query_done, req);
1270 static bool name_query_validator(struct packet_struct *p, void *private_data)
1272 struct name_query_state *state = talloc_get_type_abort(
1273 private_data, struct name_query_state);
1274 struct nmb_packet *nmb = &p->packet.nmb;
1275 struct sockaddr_storage *tmp_addrs;
1276 bool got_unique_netbios_name = false;
1279 debug_nmb_packet(p);
1282 * If we get a Negative Name Query Response from a WINS
1283 * server, we should report it and give up.
1285 if( 0 == nmb->header.opcode /* A query response */
1286 && !state->bcast /* from a WINS server */
1287 && nmb->header.rcode /* Error returned */
1290 if( DEBUGLVL( 3 ) ) {
1291 /* Only executed if DEBUGLEVEL >= 3 */
1292 dbgtext( "Negative name query "
1293 "response, rcode 0x%02x: ",
1294 nmb->header.rcode );
1295 switch( nmb->header.rcode ) {
1297 dbgtext("Request was invalidly formatted.\n");
1300 dbgtext("Problem with NBNS, cannot process "
1304 dbgtext("The name requested does not "
1308 dbgtext("Unsupported request error.\n");
1311 dbgtext("Query refused error.\n");
1314 dbgtext("Unrecognized error code.\n" );
1320 * We accept this packet as valid, but tell the upper
1321 * layers that it's a negative response.
1323 state->validate_error = NT_STATUS_NOT_FOUND;
1327 if (nmb->header.opcode != 0 ||
1328 nmb->header.nm_flags.bcast ||
1329 nmb->header.rcode ||
1330 !nmb->header.ancount) {
1332 * XXXX what do we do with this? Could be a redirect,
1333 * but we'll discard it for the moment.
1338 tmp_addrs = talloc_realloc(
1339 state, state->addrs, struct sockaddr_storage,
1340 state->num_addrs + nmb->answers->rdlength/6);
1341 if (tmp_addrs == NULL) {
1342 state->validate_error = NT_STATUS_NO_MEMORY;
1345 state->addrs = tmp_addrs;
1347 DEBUG(2,("Got a positive name query response "
1348 "from %s ( ", inet_ntoa(p->ip)));
1350 for (i=0; i<nmb->answers->rdlength/6; i++) {
1353 struct sockaddr_storage addr;
1356 flags = RSVAL(&nmb->answers->rdata[i*6], 0);
1357 got_unique_netbios_name |= ((flags & 0x8000) == 0);
1359 putip((char *)&ip,&nmb->answers->rdata[2+i*6]);
1360 in_addr_to_sockaddr_storage(&addr, ip);
1362 for (j=0; j<state->num_addrs; j++) {
1364 (struct sockaddr *)(void *)&addr,
1365 (struct sockaddr *)(void *)&state->addrs[j])) {
1369 if (j < state->num_addrs) {
1370 /* Already got it */
1374 DEBUGADD(2,("%s ",inet_ntoa(ip)));
1376 state->addrs[state->num_addrs] = addr;
1377 state->num_addrs += 1;
1379 DEBUGADD(2,(")\n"));
1381 /* We add the flags back ... */
1382 if (nmb->header.response)
1383 state->flags |= NM_FLAGS_RS;
1384 if (nmb->header.nm_flags.authoritative)
1385 state->flags |= NM_FLAGS_AA;
1386 if (nmb->header.nm_flags.trunc)
1387 state->flags |= NM_FLAGS_TC;
1388 if (nmb->header.nm_flags.recursion_desired)
1389 state->flags |= NM_FLAGS_RD;
1390 if (nmb->header.nm_flags.recursion_available)
1391 state->flags |= NM_FLAGS_RA;
1392 if (nmb->header.nm_flags.bcast)
1393 state->flags |= NM_FLAGS_B;
1397 * We have to collect all entries coming in from broadcast
1398 * queries. If we got a unique name, we're done.
1400 return got_unique_netbios_name;
1403 * WINS responses are accepted when they are received
1408 static void name_query_done(struct tevent_req *subreq)
1410 struct tevent_req *req = tevent_req_callback_data(
1411 subreq, struct tevent_req);
1412 struct name_query_state *state = tevent_req_data(
1413 req, struct name_query_state);
1415 struct packet_struct *p = NULL;
1417 status = nb_trans_recv(subreq, &p);
1418 TALLOC_FREE(subreq);
1419 if (tevent_req_nterror(req, status)) {
1422 if (!NT_STATUS_IS_OK(state->validate_error)) {
1423 tevent_req_nterror(req, state->validate_error);
1428 * Free the packet here, we've collected the response in the
1433 tevent_req_done(req);
1436 NTSTATUS name_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1437 struct sockaddr_storage **addrs, int *num_addrs,
1440 struct name_query_state *state = tevent_req_data(
1441 req, struct name_query_state);
1444 if (tevent_req_is_nterror(req, &status)) {
1446 NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
1448 * In the broadcast case we collect replies until the
1451 status = NT_STATUS_OK;
1453 if (!NT_STATUS_IS_OK(status)) {
1457 if (state->num_addrs == 0) {
1458 return NT_STATUS_NOT_FOUND;
1460 *addrs = talloc_move(mem_ctx, &state->addrs);
1461 sort_addr_list(*addrs, state->num_addrs);
1462 *num_addrs = state->num_addrs;
1463 if (flags != NULL) {
1464 *flags = state->flags;
1466 return NT_STATUS_OK;
1469 NTSTATUS name_query(const char *name, int name_type,
1470 bool bcast, bool recurse,
1471 const struct sockaddr_storage *to_ss,
1472 TALLOC_CTX *mem_ctx,
1473 struct sockaddr_storage **addrs,
1474 int *num_addrs, uint8_t *flags)
1476 TALLOC_CTX *frame = talloc_stackframe();
1477 struct tevent_context *ev;
1478 struct tevent_req *req;
1479 struct timeval timeout;
1480 NTSTATUS status = NT_STATUS_NO_MEMORY;
1482 ev = tevent_context_init(frame);
1486 req = name_query_send(ev, ev, name, name_type, bcast, recurse, to_ss);
1491 timeout = timeval_current_ofs(0, 250000);
1493 timeout = timeval_current_ofs(2, 0);
1495 if (!tevent_req_set_endtime(req, ev, timeout)) {
1498 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1501 status = name_query_recv(req, mem_ctx, addrs, num_addrs, flags);
1507 /********************************************************
1508 convert an array if struct sockaddr_storage to struct ip_service
1509 return false on failure. Port is set to PORT_NONE;
1510 *********************************************************/
1512 static bool convert_ss2service(struct ip_service **return_iplist,
1513 const struct sockaddr_storage *ss_list,
1518 if ( count==0 || !ss_list )
1521 /* copy the ip address; port will be PORT_NONE */
1522 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, count)) ==
1524 DEBUG(0,("convert_ip2service: malloc failed "
1525 "for %d enetries!\n", count ));
1529 for ( i=0; i<count; i++ ) {
1530 (*return_iplist)[i].ss = ss_list[i];
1531 (*return_iplist)[i].port = PORT_NONE;
1537 struct name_queries_state {
1538 struct tevent_context *ev;
1543 const struct sockaddr_storage *addrs;
1548 struct tevent_req **subreqs;
1553 struct sockaddr_storage *result_addrs;
1554 int num_result_addrs;
1558 static void name_queries_done(struct tevent_req *subreq);
1559 static void name_queries_next(struct tevent_req *subreq);
1562 * Send a name query to multiple destinations with a wait time in between
1565 static struct tevent_req *name_queries_send(
1566 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1567 const char *name, int name_type,
1568 bool bcast, bool recurse,
1569 const struct sockaddr_storage *addrs,
1570 int num_addrs, int wait_msec, int timeout_msec)
1572 struct tevent_req *req, *subreq;
1573 struct name_queries_state *state;
1575 req = tevent_req_create(mem_ctx, &state,
1576 struct name_queries_state);
1582 state->name_type = name_type;
1583 state->bcast = bcast;
1584 state->recurse = recurse;
1585 state->addrs = addrs;
1586 state->num_addrs = num_addrs;
1587 state->wait_msec = wait_msec;
1588 state->timeout_msec = timeout_msec;
1590 state->subreqs = talloc_zero_array(
1591 state, struct tevent_req *, num_addrs);
1592 if (tevent_req_nomem(state->subreqs, req)) {
1593 return tevent_req_post(req, ev);
1595 state->num_sent = 0;
1597 subreq = name_query_send(
1598 state->subreqs, state->ev, name, name_type, bcast, recurse,
1599 &state->addrs[state->num_sent]);
1600 if (tevent_req_nomem(subreq, req)) {
1601 return tevent_req_post(req, ev);
1603 if (!tevent_req_set_endtime(
1605 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1606 tevent_req_oom(req);
1607 return tevent_req_post(req, ev);
1609 tevent_req_set_callback(subreq, name_queries_done, req);
1611 state->subreqs[state->num_sent] = subreq;
1612 state->num_sent += 1;
1614 if (state->num_sent < state->num_addrs) {
1615 subreq = tevent_wakeup_send(
1617 timeval_current_ofs(0, state->wait_msec * 1000));
1618 if (tevent_req_nomem(subreq, req)) {
1619 return tevent_req_post(req, ev);
1621 tevent_req_set_callback(subreq, name_queries_next, req);
1626 static void name_queries_done(struct tevent_req *subreq)
1628 struct tevent_req *req = tevent_req_callback_data(
1629 subreq, struct tevent_req);
1630 struct name_queries_state *state = tevent_req_data(
1631 req, struct name_queries_state);
1635 status = name_query_recv(subreq, state, &state->result_addrs,
1636 &state->num_result_addrs, &state->flags);
1638 for (i=0; i<state->num_sent; i++) {
1639 if (state->subreqs[i] == subreq) {
1643 if (i == state->num_sent) {
1644 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1647 TALLOC_FREE(state->subreqs[i]);
1649 state->num_received += 1;
1651 if (!NT_STATUS_IS_OK(status)) {
1653 if (state->num_received >= state->num_addrs) {
1654 tevent_req_nterror(req, status);
1658 * Still outstanding requests, just wait
1662 state->received_index = i;
1663 tevent_req_done(req);
1666 static void name_queries_next(struct tevent_req *subreq)
1668 struct tevent_req *req = tevent_req_callback_data(
1669 subreq, struct tevent_req);
1670 struct name_queries_state *state = tevent_req_data(
1671 req, struct name_queries_state);
1673 if (!tevent_wakeup_recv(subreq)) {
1674 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1678 subreq = name_query_send(
1679 state->subreqs, state->ev,
1680 state->name, state->name_type, state->bcast, state->recurse,
1681 &state->addrs[state->num_sent]);
1682 if (tevent_req_nomem(subreq, req)) {
1685 tevent_req_set_callback(subreq, name_queries_done, req);
1686 if (!tevent_req_set_endtime(
1688 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1689 tevent_req_oom(req);
1692 state->subreqs[state->num_sent] = subreq;
1693 state->num_sent += 1;
1695 if (state->num_sent < state->num_addrs) {
1696 subreq = tevent_wakeup_send(
1698 timeval_current_ofs(0, state->wait_msec * 1000));
1699 if (tevent_req_nomem(subreq, req)) {
1702 tevent_req_set_callback(subreq, name_queries_next, req);
1706 static NTSTATUS name_queries_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1707 struct sockaddr_storage **result_addrs,
1708 int *num_result_addrs, uint8_t *flags,
1709 int *received_index)
1711 struct name_queries_state *state = tevent_req_data(
1712 req, struct name_queries_state);
1715 if (tevent_req_is_nterror(req, &status)) {
1719 if (result_addrs != NULL) {
1720 *result_addrs = talloc_move(mem_ctx, &state->result_addrs);
1722 if (num_result_addrs != NULL) {
1723 *num_result_addrs = state->num_result_addrs;
1725 if (flags != NULL) {
1726 *flags = state->flags;
1728 if (received_index != NULL) {
1729 *received_index = state->received_index;
1731 return NT_STATUS_OK;
1734 /********************************************************
1735 Resolve via "bcast" method.
1736 *********************************************************/
1738 struct name_resolve_bcast_state {
1739 struct sockaddr_storage *addrs;
1743 static void name_resolve_bcast_done(struct tevent_req *subreq);
1745 struct tevent_req *name_resolve_bcast_send(TALLOC_CTX *mem_ctx,
1746 struct tevent_context *ev,
1750 struct tevent_req *req, *subreq;
1751 struct name_resolve_bcast_state *state;
1752 struct sockaddr_storage *bcast_addrs;
1753 int i, num_addrs, num_bcast_addrs;
1755 req = tevent_req_create(mem_ctx, &state,
1756 struct name_resolve_bcast_state);
1761 if (lp_disable_netbios()) {
1762 DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
1764 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1765 return tevent_req_post(req, ev);
1769 * "bcast" means do a broadcast lookup on all the local interfaces.
1772 DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
1773 "for name %s<0x%x>\n", name, name_type));
1775 num_addrs = iface_count();
1776 bcast_addrs = talloc_array(state, struct sockaddr_storage, num_addrs);
1777 if (tevent_req_nomem(bcast_addrs, req)) {
1778 return tevent_req_post(req, ev);
1782 * Lookup the name on all the interfaces, return on
1783 * the first successful match.
1785 num_bcast_addrs = 0;
1787 for (i=0; i<num_addrs; i++) {
1788 const struct sockaddr_storage *pss = iface_n_bcast(i);
1790 if (pss->ss_family != AF_INET) {
1793 bcast_addrs[num_bcast_addrs] = *pss;
1794 num_bcast_addrs += 1;
1797 subreq = name_queries_send(state, ev, name, name_type, true, true,
1798 bcast_addrs, num_bcast_addrs, 0, 1000);
1799 if (tevent_req_nomem(subreq, req)) {
1800 return tevent_req_post(req, ev);
1802 tevent_req_set_callback(subreq, name_resolve_bcast_done, req);
1806 static void name_resolve_bcast_done(struct tevent_req *subreq)
1808 struct tevent_req *req = tevent_req_callback_data(
1809 subreq, struct tevent_req);
1810 struct name_resolve_bcast_state *state = tevent_req_data(
1811 req, struct name_resolve_bcast_state);
1814 status = name_queries_recv(subreq, state,
1815 &state->addrs, &state->num_addrs,
1817 TALLOC_FREE(subreq);
1818 if (tevent_req_nterror(req, status)) {
1821 tevent_req_done(req);
1824 NTSTATUS name_resolve_bcast_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1825 struct sockaddr_storage **addrs,
1828 struct name_resolve_bcast_state *state = tevent_req_data(
1829 req, struct name_resolve_bcast_state);
1832 if (tevent_req_is_nterror(req, &status)) {
1835 *addrs = talloc_move(mem_ctx, &state->addrs);
1836 *num_addrs = state->num_addrs;
1837 return NT_STATUS_OK;
1840 NTSTATUS name_resolve_bcast(const char *name,
1842 TALLOC_CTX *mem_ctx,
1843 struct sockaddr_storage **return_iplist,
1846 TALLOC_CTX *frame = talloc_stackframe();
1847 struct event_context *ev;
1848 struct tevent_req *req;
1849 NTSTATUS status = NT_STATUS_NO_MEMORY;
1851 ev = event_context_init(frame);
1855 req = name_resolve_bcast_send(frame, ev, name, name_type);
1859 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1862 status = name_resolve_bcast_recv(req, mem_ctx, return_iplist,
1869 struct query_wins_list_state {
1870 struct tevent_context *ev;
1873 struct in_addr *servers;
1874 uint32_t num_servers;
1875 struct sockaddr_storage server;
1878 struct sockaddr_storage *addrs;
1883 static void query_wins_list_done(struct tevent_req *subreq);
1886 * Query a list of (replicating) wins servers in sequence, call them
1887 * dead if they don't reply
1890 static struct tevent_req *query_wins_list_send(
1891 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1892 struct in_addr src_ip, const char *name, uint8_t name_type,
1893 struct in_addr *servers, int num_servers)
1895 struct tevent_req *req, *subreq;
1896 struct query_wins_list_state *state;
1898 req = tevent_req_create(mem_ctx, &state,
1899 struct query_wins_list_state);
1905 state->name_type = name_type;
1906 state->servers = servers;
1907 state->num_servers = num_servers;
1909 if (state->num_servers == 0) {
1910 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
1911 return tevent_req_post(req, ev);
1914 in_addr_to_sockaddr_storage(
1915 &state->server, state->servers[state->num_sent]);
1917 subreq = name_query_send(state, state->ev,
1918 state->name, state->name_type,
1919 false, true, &state->server);
1920 state->num_sent += 1;
1921 if (tevent_req_nomem(subreq, req)) {
1922 return tevent_req_post(req, ev);
1924 if (!tevent_req_set_endtime(subreq, state->ev,
1925 timeval_current_ofs(2, 0))) {
1926 tevent_req_oom(req);
1927 return tevent_req_post(req, ev);
1929 tevent_req_set_callback(subreq, query_wins_list_done, req);
1933 static void query_wins_list_done(struct tevent_req *subreq)
1935 struct tevent_req *req = tevent_req_callback_data(
1936 subreq, struct tevent_req);
1937 struct query_wins_list_state *state = tevent_req_data(
1938 req, struct query_wins_list_state);
1941 status = name_query_recv(subreq, state,
1942 &state->addrs, &state->num_addrs,
1944 TALLOC_FREE(subreq);
1945 if (NT_STATUS_IS_OK(status)) {
1946 tevent_req_done(req);
1949 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
1950 tevent_req_nterror(req, status);
1953 wins_srv_died(state->servers[state->num_sent-1],
1954 my_socket_addr_v4());
1956 if (state->num_sent == state->num_servers) {
1957 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
1961 in_addr_to_sockaddr_storage(
1962 &state->server, state->servers[state->num_sent]);
1964 subreq = name_query_send(state, state->ev,
1965 state->name, state->name_type,
1966 false, true, &state->server);
1967 state->num_sent += 1;
1968 if (tevent_req_nomem(subreq, req)) {
1971 if (!tevent_req_set_endtime(subreq, state->ev,
1972 timeval_current_ofs(2, 0))) {
1973 tevent_req_oom(req);
1976 tevent_req_set_callback(subreq, query_wins_list_done, req);
1979 static NTSTATUS query_wins_list_recv(struct tevent_req *req,
1980 TALLOC_CTX *mem_ctx,
1981 struct sockaddr_storage **addrs,
1985 struct query_wins_list_state *state = tevent_req_data(
1986 req, struct query_wins_list_state);
1989 if (tevent_req_is_nterror(req, &status)) {
1992 if (addrs != NULL) {
1993 *addrs = talloc_move(mem_ctx, &state->addrs);
1995 if (num_addrs != NULL) {
1996 *num_addrs = state->num_addrs;
1998 if (flags != NULL) {
1999 *flags = state->flags;
2001 return NT_STATUS_OK;
2004 struct resolve_wins_state {
2008 struct sockaddr_storage *addrs;
2013 static void resolve_wins_done(struct tevent_req *subreq);
2015 struct tevent_req *resolve_wins_send(TALLOC_CTX *mem_ctx,
2016 struct tevent_context *ev,
2020 struct tevent_req *req, *subreq;
2021 struct resolve_wins_state *state;
2022 char **wins_tags = NULL;
2023 struct sockaddr_storage src_ss;
2024 struct in_addr src_ip;
2025 int i, num_wins_tags;
2027 req = tevent_req_create(mem_ctx, &state,
2028 struct resolve_wins_state);
2033 if (wins_srv_count() < 1) {
2034 DEBUG(3,("resolve_wins: WINS server resolution selected "
2035 "and no WINS servers listed.\n"));
2036 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2040 /* the address we will be sending from */
2041 if (!interpret_string_addr(&src_ss, lp_socket_address(),
2042 AI_NUMERICHOST|AI_PASSIVE)) {
2043 zero_sockaddr(&src_ss);
2046 if (src_ss.ss_family != AF_INET) {
2047 char addr[INET6_ADDRSTRLEN];
2048 print_sockaddr(addr, sizeof(addr), &src_ss);
2049 DEBUG(3,("resolve_wins: cannot receive WINS replies "
2050 "on IPv6 address %s\n",
2052 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2056 src_ip = ((const struct sockaddr_in *)(void *)&src_ss)->sin_addr;
2058 wins_tags = wins_srv_tags();
2059 if (wins_tags == NULL) {
2060 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2065 while (wins_tags[num_wins_tags] != NULL) {
2069 for (i=0; i<num_wins_tags; i++) {
2070 int num_servers, num_alive;
2071 struct in_addr *servers, *alive;
2074 if (!wins_server_tag_ips(wins_tags[i], talloc_tos(),
2075 &servers, &num_servers)) {
2076 DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
2081 alive = talloc_array(state, struct in_addr, num_servers);
2082 if (tevent_req_nomem(alive, req)) {
2087 for (j=0; j<num_servers; j++) {
2088 struct in_addr wins_ip = servers[j];
2090 if (global_in_nmbd && ismyip_v4(wins_ip)) {
2091 /* yikes! we'll loop forever */
2094 /* skip any that have been unresponsive lately */
2095 if (wins_srv_is_dead(wins_ip, src_ip)) {
2098 DEBUG(3, ("resolve_wins: using WINS server %s "
2100 inet_ntoa(wins_ip), wins_tags[i]));
2101 alive[num_alive] = wins_ip;
2104 TALLOC_FREE(servers);
2106 if (num_alive == 0) {
2110 subreq = query_wins_list_send(
2111 state, ev, src_ip, name, name_type,
2113 if (tevent_req_nomem(subreq, req)) {
2116 tevent_req_set_callback(subreq, resolve_wins_done, req);
2117 state->num_sent += 1;
2120 if (state->num_sent == 0) {
2121 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2125 wins_srv_tags_free(wins_tags);
2128 wins_srv_tags_free(wins_tags);
2129 return tevent_req_post(req, ev);
2132 static void resolve_wins_done(struct tevent_req *subreq)
2134 struct tevent_req *req = tevent_req_callback_data(
2135 subreq, struct tevent_req);
2136 struct resolve_wins_state *state = tevent_req_data(
2137 req, struct resolve_wins_state);
2140 status = query_wins_list_recv(subreq, state, &state->addrs,
2141 &state->num_addrs, &state->flags);
2142 if (NT_STATUS_IS_OK(status)) {
2143 tevent_req_done(req);
2147 state->num_received += 1;
2149 if (state->num_received < state->num_sent) {
2151 * Wait for the others
2155 tevent_req_nterror(req, status);
2158 NTSTATUS resolve_wins_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2159 struct sockaddr_storage **addrs,
2160 int *num_addrs, uint8_t *flags)
2162 struct resolve_wins_state *state = tevent_req_data(
2163 req, struct resolve_wins_state);
2166 if (tevent_req_is_nterror(req, &status)) {
2169 if (addrs != NULL) {
2170 *addrs = talloc_move(mem_ctx, &state->addrs);
2172 if (num_addrs != NULL) {
2173 *num_addrs = state->num_addrs;
2175 if (flags != NULL) {
2176 *flags = state->flags;
2178 return NT_STATUS_OK;
2181 /********************************************************
2182 Resolve via "wins" method.
2183 *********************************************************/
2185 NTSTATUS resolve_wins(const char *name,
2187 TALLOC_CTX *mem_ctx,
2188 struct sockaddr_storage **return_iplist,
2191 struct tevent_context *ev;
2192 struct tevent_req *req;
2193 NTSTATUS status = NT_STATUS_NO_MEMORY;
2195 ev = tevent_context_init(talloc_tos());
2199 req = resolve_wins_send(ev, ev, name, name_type);
2203 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2206 status = resolve_wins_recv(req, mem_ctx, return_iplist, return_count,
2213 /********************************************************
2214 Resolve via "lmhosts" method.
2215 *********************************************************/
2217 static NTSTATUS resolve_lmhosts(const char *name, int name_type,
2218 struct ip_service **return_iplist,
2222 * "lmhosts" means parse the local lmhosts file.
2224 struct sockaddr_storage *ss_list;
2225 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
2226 TALLOC_CTX *ctx = NULL;
2228 *return_iplist = NULL;
2231 DEBUG(3,("resolve_lmhosts: "
2232 "Attempting lmhosts lookup for name %s<0x%x>\n",
2235 ctx = talloc_init("resolve_lmhosts");
2237 return NT_STATUS_NO_MEMORY;
2240 status = resolve_lmhosts_file_as_sockaddr(get_dyn_LMHOSTSFILE(),
2245 if (NT_STATUS_IS_OK(status)) {
2246 if (convert_ss2service(return_iplist,
2250 return NT_STATUS_OK;
2253 return NT_STATUS_NO_MEMORY;
2261 /********************************************************
2262 Resolve via "hosts" method.
2263 *********************************************************/
2265 static NTSTATUS resolve_hosts(const char *name, int name_type,
2266 struct ip_service **return_iplist,
2270 * "host" means do a localhost, or dns lookup.
2272 struct addrinfo hints;
2273 struct addrinfo *ailist = NULL;
2274 struct addrinfo *res = NULL;
2277 const char *dns_hosts_file;
2279 if ( name_type != 0x20 && name_type != 0x0) {
2280 DEBUG(5, ("resolve_hosts: not appropriate "
2281 "for name type <0x%x>\n",
2283 return NT_STATUS_INVALID_PARAMETER;
2286 *return_iplist = NULL;
2289 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
2293 /* By default make sure it supports TCP. */
2294 hints.ai_socktype = SOCK_STREAM;
2295 hints.ai_flags = AI_ADDRCONFIG;
2297 #if !defined(HAVE_IPV6)
2298 /* Unless we have IPv6, we really only want IPv4 addresses back. */
2299 hints.ai_family = AF_INET;
2302 dns_hosts_file = lp_parm_const_string(-1, "resolv", "host file", NULL);
2303 if (dns_hosts_file) {
2304 struct sockaddr_storage *ss_list;
2306 TALLOC_CTX *ctx = talloc_stackframe();
2308 return NT_STATUS_NO_MEMORY;
2311 status = resolve_dns_hosts_file_as_sockaddr(dns_hosts_file, name, false,
2312 ctx, &ss_list, return_count);
2313 if (NT_STATUS_IS_OK(status)) {
2314 if (convert_ss2service(return_iplist,
2318 return NT_STATUS_OK;
2321 return NT_STATUS_NO_MEMORY;
2325 return NT_STATUS_UNSUCCESSFUL;
2328 ret = getaddrinfo(name,
2333 DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
2335 gai_strerror(ret) ));
2338 for (res = ailist; res; res = res->ai_next) {
2339 struct sockaddr_storage ss;
2341 if (!res->ai_addr || res->ai_addrlen == 0) {
2346 memcpy(&ss, res->ai_addr, res->ai_addrlen);
2350 *return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
2353 if (!*return_iplist) {
2354 DEBUG(3,("resolve_hosts: malloc fail !\n"));
2355 freeaddrinfo(ailist);
2356 return NT_STATUS_NO_MEMORY;
2358 (*return_iplist)[i].ss = ss;
2359 (*return_iplist)[i].port = PORT_NONE;
2363 freeaddrinfo(ailist);
2365 if (*return_count) {
2366 return NT_STATUS_OK;
2368 return NT_STATUS_UNSUCCESSFUL;
2371 /********************************************************
2372 Resolve via "ADS" method.
2373 *********************************************************/
2375 /* Special name type used to cause a _kerberos DNS lookup. */
2376 #define KDC_NAME_TYPE 0xDCDC
2378 static NTSTATUS resolve_ads(const char *name,
2380 const char *sitename,
2381 struct ip_service **return_iplist,
2387 struct dns_rr_srv *dcs = NULL;
2391 if ((name_type != 0x1c) && (name_type != KDC_NAME_TYPE) &&
2392 (name_type != 0x1b)) {
2393 return NT_STATUS_INVALID_PARAMETER;
2396 if ( (ctx = talloc_init("resolve_ads")) == NULL ) {
2397 DEBUG(0,("resolve_ads: talloc_init() failed!\n"));
2398 return NT_STATUS_NO_MEMORY;
2401 /* The DNS code needs fixing to find IPv6 addresses... JRA. */
2403 switch (name_type) {
2405 DEBUG(5,("resolve_ads: Attempting to resolve "
2406 "PDC for %s using DNS\n", name));
2407 status = ads_dns_query_pdc(ctx, name, &dcs, &numdcs);
2411 DEBUG(5,("resolve_ads: Attempting to resolve "
2412 "DCs for %s using DNS\n", name));
2413 status = ads_dns_query_dcs(ctx, name, sitename, &dcs,
2417 DEBUG(5,("resolve_ads: Attempting to resolve "
2418 "KDCs for %s using DNS\n", name));
2419 status = ads_dns_query_kdcs(ctx, name, sitename, &dcs,
2423 status = NT_STATUS_INVALID_PARAMETER;
2427 if ( !NT_STATUS_IS_OK( status ) ) {
2428 talloc_destroy(ctx);
2432 for (i=0;i<numdcs;i++) {
2433 numaddrs += MAX(dcs[i].num_ips,1);
2436 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, numaddrs)) ==
2438 DEBUG(0,("resolve_ads: malloc failed for %d entries\n",
2440 talloc_destroy(ctx);
2441 return NT_STATUS_NO_MEMORY;
2444 /* now unroll the list of IP addresses */
2449 while ( i < numdcs && (*return_count<numaddrs) ) {
2450 struct ip_service *r = &(*return_iplist)[*return_count];
2452 r->port = dcs[i].port;
2454 /* If we don't have an IP list for a name, lookup it up */
2457 interpret_string_addr(&r->ss, dcs[i].hostname, 0);
2461 /* use the IP addresses from the SRV sresponse */
2463 if ( j >= dcs[i].num_ips ) {
2469 r->ss = dcs[i].ss_s[j];
2473 /* make sure it is a valid IP. I considered checking the
2474 * negative connection cache, but this is the wrong place
2475 * for it. Maybe only as a hack. After think about it, if
2476 * all of the IP addresses returned from DNS are dead, what
2477 * hope does a netbios name lookup have ? The standard reason
2478 * for falling back to netbios lookups is that our DNS server
2479 * doesn't know anything about the DC's -- jerry */
2481 if (!is_zero_addr(&r->ss)) {
2486 talloc_destroy(ctx);
2487 return NT_STATUS_OK;
2490 /*******************************************************************
2491 Internal interface to resolve a name into an IP address.
2492 Use this function if the string is either an IP address, DNS
2493 or host name or NetBIOS name. This uses the name switch in the
2494 smb.conf to determine the order of name resolution.
2496 Added support for ip addr/port to support ADS ldap servers.
2497 the only place we currently care about the port is in the
2498 resolve_hosts() when looking up DC's via SRV RR entries in DNS
2499 **********************************************************************/
2501 NTSTATUS internal_resolve_name(const char *name,
2503 const char *sitename,
2504 struct ip_service **return_iplist,
2506 const char *resolve_order)
2510 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2512 TALLOC_CTX *frame = NULL;
2514 *return_iplist = NULL;
2517 DEBUG(10, ("internal_resolve_name: looking up %s#%x (sitename %s)\n",
2518 name, name_type, sitename ? sitename : "(null)"));
2520 if (is_ipaddress(name)) {
2521 if ((*return_iplist = SMB_MALLOC_P(struct ip_service)) ==
2523 DEBUG(0,("internal_resolve_name: malloc fail !\n"));
2524 return NT_STATUS_NO_MEMORY;
2527 /* ignore the port here */
2528 (*return_iplist)->port = PORT_NONE;
2530 /* if it's in the form of an IP address then get the lib to interpret it */
2531 if (!interpret_string_addr(&(*return_iplist)->ss,
2532 name, AI_NUMERICHOST)) {
2533 DEBUG(1,("internal_resolve_name: interpret_string_addr "
2536 SAFE_FREE(*return_iplist);
2537 return NT_STATUS_INVALID_PARAMETER;
2540 return NT_STATUS_OK;
2543 /* Check name cache */
2545 if (namecache_fetch(name, name_type, return_iplist, return_count)) {
2546 /* This could be a negative response */
2547 if (*return_count > 0) {
2548 return NT_STATUS_OK;
2550 return NT_STATUS_UNSUCCESSFUL;
2554 /* set the name resolution order */
2556 if (strcmp( resolve_order, "NULL") == 0) {
2557 DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
2558 return NT_STATUS_INVALID_PARAMETER;
2561 if (!resolve_order[0]) {
2564 ptr = resolve_order;
2567 /* iterate through the name resolution backends */
2569 frame = talloc_stackframe();
2570 while (next_token_talloc(frame, &ptr, &tok, LIST_SEP)) {
2571 if((strequal(tok, "host") || strequal(tok, "hosts"))) {
2572 status = resolve_hosts(name, name_type, return_iplist,
2574 if (NT_STATUS_IS_OK(status)) {
2577 } else if(strequal( tok, "kdc")) {
2578 /* deal with KDC_NAME_TYPE names here.
2579 * This will result in a SRV record lookup */
2580 status = resolve_ads(name, KDC_NAME_TYPE, sitename,
2581 return_iplist, return_count);
2582 if (NT_STATUS_IS_OK(status)) {
2583 /* Ensure we don't namecache
2584 * this with the KDC port. */
2585 name_type = KDC_NAME_TYPE;
2588 } else if(strequal( tok, "ads")) {
2589 /* deal with 0x1c and 0x1b names here.
2590 * This will result in a SRV record lookup */
2591 status = resolve_ads(name, name_type, sitename,
2592 return_iplist, return_count);
2593 if (NT_STATUS_IS_OK(status)) {
2596 } else if(strequal( tok, "lmhosts")) {
2597 status = resolve_lmhosts(name, name_type,
2598 return_iplist, return_count);
2599 if (NT_STATUS_IS_OK(status)) {
2602 } else if(strequal( tok, "wins")) {
2603 /* don't resolve 1D via WINS */
2604 struct sockaddr_storage *ss_list;
2605 if (name_type != 0x1D) {
2606 status = resolve_wins(name, name_type,
2610 if (NT_STATUS_IS_OK(status)) {
2611 if (!convert_ss2service(return_iplist,
2614 status = NT_STATUS_NO_MEMORY;
2619 } else if(strequal( tok, "bcast")) {
2620 struct sockaddr_storage *ss_list;
2621 status = name_resolve_bcast(
2622 name, name_type, talloc_tos(),
2623 &ss_list, return_count);
2624 if (NT_STATUS_IS_OK(status)) {
2625 if (!convert_ss2service(return_iplist,
2628 status = NT_STATUS_NO_MEMORY;
2633 DEBUG(0,("resolve_name: unknown name switch type %s\n",
2638 /* All of the resolve_* functions above have returned false. */
2641 SAFE_FREE(*return_iplist);
2644 return NT_STATUS_UNSUCCESSFUL;
2648 /* Remove duplicate entries. Some queries, notably #1c (domain
2649 controllers) return the PDC in iplist[0] and then all domain
2650 controllers including the PDC in iplist[1..n]. Iterating over
2651 the iplist when the PDC is down will cause two sets of timeouts. */
2653 if ( *return_count ) {
2654 *return_count = remove_duplicate_addrs2(*return_iplist,
2658 /* Save in name cache */
2659 if ( DEBUGLEVEL >= 100 ) {
2660 for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++) {
2661 char addr[INET6_ADDRSTRLEN];
2662 print_sockaddr(addr, sizeof(addr),
2663 &(*return_iplist)[i].ss);
2664 DEBUG(100, ("Storing name %s of type %d (%s:%d)\n",
2668 (*return_iplist)[i].port));
2672 namecache_store(name, name_type, *return_count, *return_iplist);
2674 /* Display some debugging info */
2676 if ( DEBUGLEVEL >= 10 ) {
2677 DEBUG(10, ("internal_resolve_name: returning %d addresses: ",
2680 for (i = 0; i < *return_count; i++) {
2681 char addr[INET6_ADDRSTRLEN];
2682 print_sockaddr(addr, sizeof(addr),
2683 &(*return_iplist)[i].ss);
2684 DEBUGADD(10, ("%s:%d ",
2686 (*return_iplist)[i].port));
2695 /********************************************************
2696 Internal interface to resolve a name into one IP address.
2697 Use this function if the string is either an IP address, DNS
2698 or host name or NetBIOS name. This uses the name switch in the
2699 smb.conf to determine the order of name resolution.
2700 *********************************************************/
2702 bool resolve_name(const char *name,
2703 struct sockaddr_storage *return_ss,
2707 struct ip_service *ss_list = NULL;
2708 char *sitename = NULL;
2712 if (is_ipaddress(name)) {
2713 return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
2716 sitename = sitename_fetch(lp_realm()); /* wild guess */
2718 status = internal_resolve_name(name, name_type, sitename,
2720 lp_name_resolve_order());
2721 if (NT_STATUS_IS_OK(status)) {
2725 for (i=0; i<count; i++) {
2726 if (!is_zero_addr(&ss_list[i].ss) &&
2727 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss) &&
2728 (ss_list[i].ss.ss_family == AF_INET)) {
2729 *return_ss = ss_list[i].ss;
2731 SAFE_FREE(sitename);
2737 /* only return valid addresses for TCP connections */
2738 for (i=0; i<count; i++) {
2739 if (!is_zero_addr(&ss_list[i].ss) &&
2740 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2741 *return_ss = ss_list[i].ss;
2743 SAFE_FREE(sitename);
2750 SAFE_FREE(sitename);
2754 /********************************************************
2755 Internal interface to resolve a name into a list of IP addresses.
2756 Use this function if the string is either an IP address, DNS
2757 or host name or NetBIOS name. This uses the name switch in the
2758 smb.conf to determine the order of name resolution.
2759 *********************************************************/
2761 NTSTATUS resolve_name_list(TALLOC_CTX *ctx,
2764 struct sockaddr_storage **return_ss_arr,
2765 unsigned int *p_num_entries)
2767 struct ip_service *ss_list = NULL;
2768 char *sitename = NULL;
2771 unsigned int num_entries;
2775 *return_ss_arr = NULL;
2777 if (is_ipaddress(name)) {
2778 *return_ss_arr = talloc(ctx, struct sockaddr_storage);
2779 if (!*return_ss_arr) {
2780 return NT_STATUS_NO_MEMORY;
2782 if (!interpret_string_addr(*return_ss_arr, name, AI_NUMERICHOST)) {
2783 TALLOC_FREE(*return_ss_arr);
2784 return NT_STATUS_BAD_NETWORK_NAME;
2787 return NT_STATUS_OK;
2790 sitename = sitename_fetch(lp_realm()); /* wild guess */
2792 status = internal_resolve_name(name, name_type, sitename,
2794 lp_name_resolve_order());
2795 SAFE_FREE(sitename);
2797 if (!NT_STATUS_IS_OK(status)) {
2801 /* only return valid addresses for TCP connections */
2802 for (i=0, num_entries = 0; i<count; i++) {
2803 if (!is_zero_addr(&ss_list[i].ss) &&
2804 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2808 if (num_entries == 0) {
2810 return NT_STATUS_BAD_NETWORK_NAME;
2813 *return_ss_arr = talloc_array(ctx,
2814 struct sockaddr_storage,
2816 if (!(*return_ss_arr)) {
2818 return NT_STATUS_NO_MEMORY;
2821 for (i=0, num_entries = 0; i<count; i++) {
2822 if (!is_zero_addr(&ss_list[i].ss) &&
2823 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2824 (*return_ss_arr)[num_entries++] = ss_list[i].ss;
2828 status = NT_STATUS_OK;
2829 *p_num_entries = num_entries;
2832 return NT_STATUS_OK;
2835 /********************************************************
2836 Find the IP address of the master browser or DMB for a workgroup.
2837 *********************************************************/
2839 bool find_master_ip(const char *group, struct sockaddr_storage *master_ss)
2841 struct ip_service *ip_list = NULL;
2845 if (lp_disable_netbios()) {
2846 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
2850 status = internal_resolve_name(group, 0x1D, NULL, &ip_list, &count,
2851 lp_name_resolve_order());
2852 if (NT_STATUS_IS_OK(status)) {
2853 *master_ss = ip_list[0].ss;
2858 status = internal_resolve_name(group, 0x1B, NULL, &ip_list, &count,
2859 lp_name_resolve_order());
2860 if (NT_STATUS_IS_OK(status)) {
2861 *master_ss = ip_list[0].ss;
2870 /********************************************************
2871 Get the IP address list of the primary domain controller
2873 *********************************************************/
2875 bool get_pdc_ip(const char *domain, struct sockaddr_storage *pss)
2877 struct ip_service *ip_list = NULL;
2879 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
2881 /* Look up #1B name */
2883 if (lp_security() == SEC_ADS) {
2884 status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
2888 if (!NT_STATUS_IS_OK(status) || count == 0) {
2889 status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
2891 lp_name_resolve_order());
2892 if (!NT_STATUS_IS_OK(status)) {
2897 /* if we get more than 1 IP back we have to assume it is a
2898 multi-homed PDC and not a mess up */
2901 DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count));
2902 sort_service_list(ip_list, count);
2905 *pss = ip_list[0].ss;
2910 /* Private enum type for lookups. */
2912 enum dc_lookup_type { DC_NORMAL_LOOKUP, DC_ADS_ONLY, DC_KDC_ONLY };
2914 /********************************************************
2915 Get the IP address list of the domain controllers for
2917 *********************************************************/
2919 static NTSTATUS get_dc_list(const char *domain,
2920 const char *sitename,
2921 struct ip_service **ip_list,
2923 enum dc_lookup_type lookup_type,
2926 char *resolve_order = NULL;
2927 char *saf_servername = NULL;
2928 char *pserver = NULL;
2930 char *port_str = NULL;
2933 int num_addresses = 0;
2934 int local_count, i, j;
2935 struct ip_service *return_iplist = NULL;
2936 struct ip_service *auto_ip_list = NULL;
2937 bool done_auto_lookup = false;
2940 TALLOC_CTX *ctx = talloc_init("get_dc_list");
2946 return NT_STATUS_NO_MEMORY;
2951 /* if we are restricted to solely using DNS for looking
2952 up a domain controller, make sure that host lookups
2953 are enabled for the 'name resolve order'. If host lookups
2954 are disabled and ads_only is True, then set the string to
2957 resolve_order = talloc_strdup(ctx, lp_name_resolve_order());
2958 if (!resolve_order) {
2959 status = NT_STATUS_NO_MEMORY;
2962 strlower_m(resolve_order);
2963 if (lookup_type == DC_ADS_ONLY) {
2964 if (strstr( resolve_order, "host")) {
2965 resolve_order = talloc_strdup(ctx, "ads");
2967 /* DNS SRV lookups used by the ads resolver
2968 are already sorted by priority and weight */
2971 resolve_order = talloc_strdup(ctx, "NULL");
2973 } else if (lookup_type == DC_KDC_ONLY) {
2974 /* DNS SRV lookups used by the ads/kdc resolver
2975 are already sorted by priority and weight */
2977 resolve_order = talloc_strdup(ctx, "kdc");
2979 if (!resolve_order) {
2980 status = NT_STATUS_NO_MEMORY;
2984 /* fetch the server we have affinity for. Add the
2985 'password server' list to a search for our domain controllers */
2987 saf_servername = saf_fetch( domain);
2989 if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
2990 pserver = talloc_asprintf(ctx, "%s, %s",
2991 saf_servername ? saf_servername : "",
2992 lp_passwordserver());
2994 pserver = talloc_asprintf(ctx, "%s, *",
2995 saf_servername ? saf_servername : "");
2998 SAFE_FREE(saf_servername);
3000 status = NT_STATUS_NO_MEMORY;
3004 /* if we are starting from scratch, just lookup DOMAIN<0x1c> */
3007 DEBUG(10,("get_dc_list: no preferred domain controllers.\n"));
3008 status = internal_resolve_name(domain, 0x1C, sitename, ip_list,
3009 count, resolve_order);
3013 DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver ));
3016 * if '*' appears in the "password server" list then add
3017 * an auto lookup to the list of manually configured
3018 * DC's. If any DC is listed by name, then the list should be
3019 * considered to be ordered
3023 while (next_token_talloc(ctx, &p, &name, LIST_SEP)) {
3024 if (!done_auto_lookup && strequal(name, "*")) {
3025 status = internal_resolve_name(domain, 0x1C, sitename,
3029 if (NT_STATUS_IS_OK(status)) {
3030 num_addresses += auto_count;
3032 done_auto_lookup = true;
3033 DEBUG(8,("Adding %d DC's from auto lookup\n",
3040 /* if we have no addresses and haven't done the auto lookup, then
3041 just return the list of DC's. Or maybe we just failed. */
3043 if ((num_addresses == 0)) {
3044 if (done_auto_lookup) {
3045 DEBUG(4,("get_dc_list: no servers found\n"));
3046 status = NT_STATUS_NO_LOGON_SERVERS;
3049 status = internal_resolve_name(domain, 0x1C, sitename, ip_list,
3050 count, resolve_order);
3054 if ((return_iplist = SMB_MALLOC_ARRAY(struct ip_service,
3055 num_addresses)) == NULL) {
3056 DEBUG(3,("get_dc_list: malloc fail !\n"));
3057 status = NT_STATUS_NO_MEMORY;
3064 /* fill in the return list now with real IP's */
3066 while ((local_count<num_addresses) &&
3067 next_token_talloc(ctx, &p, &name, LIST_SEP)) {
3068 struct sockaddr_storage name_ss;
3070 /* copy any addersses from the auto lookup */
3072 if (strequal(name, "*")) {
3073 for (j=0; j<auto_count; j++) {
3074 char addr[INET6_ADDRSTRLEN];
3075 print_sockaddr(addr,
3077 &auto_ip_list[j].ss);
3078 /* Check for and don't copy any
3079 * known bad DC IP's. */
3080 if(!NT_STATUS_IS_OK(check_negative_conn_cache(
3083 DEBUG(5,("get_dc_list: "
3084 "negative entry %s removed "
3089 return_iplist[local_count].ss =
3091 return_iplist[local_count].port =
3092 auto_ip_list[j].port;
3098 /* added support for address:port syntax for ads
3099 * (not that I think anyone will ever run the LDAP
3100 * server in an AD domain on something other than
3103 port = (lp_security() == SEC_ADS) ? LDAP_PORT : PORT_NONE;
3104 if ((port_str=strchr(name, ':')) != NULL) {
3107 port = atoi(port_str);
3110 /* explicit lookup; resolve_name() will
3111 * handle names & IP addresses */
3112 if (resolve_name( name, &name_ss, 0x20, true )) {
3113 char addr[INET6_ADDRSTRLEN];
3114 print_sockaddr(addr,
3118 /* Check for and don't copy any known bad DC IP's. */
3119 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain,
3121 DEBUG(5,("get_dc_list: negative entry %s "
3122 "removed from DC list\n",
3127 return_iplist[local_count].ss = name_ss;
3128 return_iplist[local_count].port = port;
3134 /* need to remove duplicates in the list if we have any
3135 explicit password servers */
3138 local_count = remove_duplicate_addrs2(return_iplist,
3142 /* For DC's we always prioritize IPv4 due to W2K3 not
3143 * supporting LDAP, KRB5 or CLDAP over IPv6. */
3145 if (local_count && return_iplist) {
3146 prioritize_ipv4_list(return_iplist, local_count);
3149 if ( DEBUGLEVEL >= 4 ) {
3150 DEBUG(4,("get_dc_list: returning %d ip addresses "
3151 "in an %sordered list\n",
3153 *ordered ? "":"un"));
3154 DEBUG(4,("get_dc_list: "));
3155 for ( i=0; i<local_count; i++ ) {
3156 char addr[INET6_ADDRSTRLEN];
3157 print_sockaddr(addr,
3159 &return_iplist[i].ss);
3160 DEBUGADD(4,("%s:%d ", addr, return_iplist[i].port ));
3165 *ip_list = return_iplist;
3166 *count = local_count;
3168 status = ( *count != 0 ? NT_STATUS_OK : NT_STATUS_NO_LOGON_SERVERS );
3172 if (!NT_STATUS_IS_OK(status)) {
3173 SAFE_FREE(return_iplist);
3178 SAFE_FREE(auto_ip_list);
3183 /*********************************************************************
3184 Small wrapper function to get the DC list and sort it if neccessary.
3185 *********************************************************************/
3187 NTSTATUS get_sorted_dc_list( const char *domain,
3188 const char *sitename,
3189 struct ip_service **ip_list,
3193 bool ordered = false;
3195 enum dc_lookup_type lookup_type = DC_NORMAL_LOOKUP;
3200 DEBUG(8,("get_sorted_dc_list: attempting lookup "
3201 "for name %s (sitename %s) using [%s]\n",
3203 sitename ? sitename : "NULL",
3204 (ads_only ? "ads" : lp_name_resolve_order())));
3207 lookup_type = DC_ADS_ONLY;
3210 status = get_dc_list(domain, sitename, ip_list,
3211 count, lookup_type, &ordered);
3212 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_LOGON_SERVERS)
3214 DEBUG(3,("get_sorted_dc_list: no server for name %s available"
3215 " in site %s, fallback to all servers\n",
3217 status = get_dc_list(domain, NULL, ip_list,
3218 count, lookup_type, &ordered);
3221 if (!NT_STATUS_IS_OK(status)) {
3222 SAFE_FREE(*ip_list);
3227 /* only sort if we don't already have an ordered list */
3229 sort_service_list(*ip_list, *count);
3232 return NT_STATUS_OK;
3235 /*********************************************************************
3236 Get the KDC list - re-use all the logic in get_dc_list.
3237 *********************************************************************/
3239 NTSTATUS get_kdc_list( const char *realm,
3240 const char *sitename,
3241 struct ip_service **ip_list,
3250 status = get_dc_list(realm, sitename, ip_list,
3251 count, DC_KDC_ONLY, &ordered);
3253 if (!NT_STATUS_IS_OK(status)) {
3254 SAFE_FREE(*ip_list);
3259 /* only sort if we don't already have an ordered list */
3261 sort_service_list(*ip_list, *count);
3264 return NT_STATUS_OK;