2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2007.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "libads/sitename_cache.h"
24 #include "../lib/addns/dnsquery.h"
25 #include "../libcli/netlogon/netlogon.h"
26 #include "lib/async_req/async_sock.h"
27 #include "lib/tsocket/tsocket.h"
28 #include "libsmb/nmblib.h"
29 #include "../libcli/nbt/libnbt.h"
31 /* nmbd.c sets this to True. */
32 bool global_in_nmbd = False;
34 /****************************
35 * SERVER AFFINITY ROUTINES *
36 ****************************/
38 /* Server affinity is the concept of preferring the last domain
39 controller with whom you had a successful conversation */
41 /****************************************************************************
42 ****************************************************************************/
43 #define SAFKEY_FMT "SAF/DOMAIN/%s"
45 #define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
46 #define SAFJOIN_TTL 3600
48 static char *saf_key(TALLOC_CTX *mem_ctx, const char *domain)
50 return talloc_asprintf_strupper_m(mem_ctx, SAFKEY_FMT, domain);
53 static char *saf_join_key(TALLOC_CTX *mem_ctx, const char *domain)
55 return talloc_asprintf_strupper_m(mem_ctx, SAFJOINKEY_FMT, domain);
58 /****************************************************************************
59 ****************************************************************************/
61 bool saf_store( const char *domain, const char *servername )
67 if ( !domain || !servername ) {
68 DEBUG(2,("saf_store: "
69 "Refusing to store empty domain or servername!\n"));
73 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
74 DEBUG(0,("saf_store: "
75 "refusing to store 0 length domain or servername!\n"));
79 key = saf_key(talloc_tos(), domain);
81 DEBUG(1, ("saf_key() failed\n"));
84 expire = time( NULL ) + lp_parm_int(-1, "saf","ttl", SAF_TTL);
86 DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
87 domain, servername, (unsigned int)expire ));
89 ret = gencache_set( key, servername, expire );
96 bool saf_join_store( const char *domain, const char *servername )
102 if ( !domain || !servername ) {
103 DEBUG(2,("saf_join_store: Refusing to store empty domain or servername!\n"));
107 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
108 DEBUG(0,("saf_join_store: refusing to store 0 length domain or servername!\n"));
112 key = saf_join_key(talloc_tos(), domain);
114 DEBUG(1, ("saf_join_key() failed\n"));
117 expire = time( NULL ) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL);
119 DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
120 domain, servername, (unsigned int)expire ));
122 ret = gencache_set( key, servername, expire );
129 bool saf_delete( const char *domain )
135 DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
139 key = saf_join_key(talloc_tos(), domain);
141 DEBUG(1, ("saf_join_key() failed\n"));
144 ret = gencache_del(key);
148 DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain ));
151 key = saf_key(talloc_tos(), domain);
153 DEBUG(1, ("saf_key() failed\n"));
156 ret = gencache_del(key);
160 DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
166 /****************************************************************************
167 ****************************************************************************/
169 char *saf_fetch(TALLOC_CTX *mem_ctx, const char *domain )
176 if ( !domain || strlen(domain) == 0) {
177 DEBUG(2,("saf_fetch: Empty domain name!\n"));
181 key = saf_join_key(talloc_tos(), domain);
183 DEBUG(1, ("saf_join_key() failed\n"));
187 ret = gencache_get( key, mem_ctx, &server, &timeout );
192 DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
197 key = saf_key(talloc_tos(), domain);
199 DEBUG(1, ("saf_key() failed\n"));
203 ret = gencache_get( key, mem_ctx, &server, &timeout );
208 DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
211 DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
218 static void set_socket_addr_v4(struct sockaddr_storage *addr)
220 if (!interpret_string_addr(addr, lp_nbt_client_socket_address(),
221 AI_NUMERICHOST|AI_PASSIVE)) {
224 if (addr->ss_family != AF_INET) {
229 static struct in_addr my_socket_addr_v4(void)
231 struct sockaddr_storage my_addr;
232 struct sockaddr_in *in_addr = (struct sockaddr_in *)((char *)&my_addr);
234 set_socket_addr_v4(&my_addr);
235 return in_addr->sin_addr;
238 /****************************************************************************
239 Generate a random trn_id.
240 ****************************************************************************/
242 static int generate_trn_id(void)
246 generate_random_buffer((uint8_t *)&id, sizeof(id));
248 return id % (unsigned)0x7FFF;
251 /****************************************************************************
252 Parse a node status response into an array of structures.
253 ****************************************************************************/
255 static struct node_status *parse_node_status(TALLOC_CTX *mem_ctx, char *p,
257 struct node_status_extra *extra)
259 struct node_status *ret;
262 *num_names = CVAL(p,0);
267 ret = talloc_array(mem_ctx, struct node_status,*num_names);
272 for (i=0;i< *num_names;i++) {
273 StrnCpy(ret[i].name,p,15);
274 trim_char(ret[i].name,'\0',' ');
275 ret[i].type = CVAL(p,15);
276 ret[i].flags = p[16];
278 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
279 ret[i].type, ret[i].flags));
282 * Also, pick up the MAC address ...
285 memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
290 struct sock_packet_read_state {
291 struct tevent_context *ev;
292 enum packet_type type;
295 struct nb_packet_reader *reader;
296 struct tevent_req *reader_req;
298 struct tdgram_context *sock;
299 struct tevent_req *socket_req;
301 struct tsocket_address *addr;
303 bool (*validator)(struct packet_struct *p,
307 struct packet_struct *packet;
310 static int sock_packet_read_state_destructor(struct sock_packet_read_state *s);
311 static void sock_packet_read_got_packet(struct tevent_req *subreq);
312 static void sock_packet_read_got_socket(struct tevent_req *subreq);
314 static struct tevent_req *sock_packet_read_send(
316 struct tevent_context *ev,
317 struct tdgram_context *sock,
318 struct nb_packet_reader *reader,
319 enum packet_type type,
321 bool (*validator)(struct packet_struct *p, void *private_data),
324 struct tevent_req *req;
325 struct sock_packet_read_state *state;
327 req = tevent_req_create(mem_ctx, &state,
328 struct sock_packet_read_state);
332 talloc_set_destructor(state, sock_packet_read_state_destructor);
334 state->reader = reader;
337 state->trn_id = trn_id;
338 state->validator = validator;
339 state->private_data = private_data;
341 if (reader != NULL) {
342 state->reader_req = nb_packet_read_send(state, ev, reader);
343 if (tevent_req_nomem(state->reader_req, req)) {
344 return tevent_req_post(req, ev);
346 tevent_req_set_callback(
347 state->reader_req, sock_packet_read_got_packet, req);
350 state->socket_req = tdgram_recvfrom_send(state, ev, state->sock);
351 if (tevent_req_nomem(state->socket_req, req)) {
352 return tevent_req_post(req, ev);
354 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
360 static int sock_packet_read_state_destructor(struct sock_packet_read_state *s)
362 if (s->packet != NULL) {
363 free_packet(s->packet);
369 static void sock_packet_read_got_packet(struct tevent_req *subreq)
371 struct tevent_req *req = tevent_req_callback_data(
372 subreq, struct tevent_req);
373 struct sock_packet_read_state *state = tevent_req_data(
374 req, struct sock_packet_read_state);
377 status = nb_packet_read_recv(subreq, &state->packet);
379 TALLOC_FREE(state->reader_req);
381 if (!NT_STATUS_IS_OK(status)) {
382 if (state->socket_req != NULL) {
384 * Still waiting for socket
389 * Both socket and packet reader failed
391 tevent_req_nterror(req, status);
395 if ((state->validator != NULL) &&
396 !state->validator(state->packet, state->private_data)) {
397 DEBUG(10, ("validator failed\n"));
399 free_packet(state->packet);
400 state->packet = NULL;
402 state->reader_req = nb_packet_read_send(state, state->ev,
404 if (tevent_req_nomem(state->reader_req, req)) {
407 tevent_req_set_callback(
408 state->reader_req, sock_packet_read_got_packet, req);
412 TALLOC_FREE(state->socket_req);
413 tevent_req_done(req);
416 static void sock_packet_read_got_socket(struct tevent_req *subreq)
418 struct tevent_req *req = tevent_req_callback_data(
419 subreq, struct tevent_req);
420 struct sock_packet_read_state *state = tevent_req_data(
421 req, struct sock_packet_read_state);
424 struct sockaddr_in sin;
431 received = tdgram_recvfrom_recv(subreq, &err, state,
432 &state->buf, &state->addr);
434 TALLOC_FREE(state->socket_req);
436 if (received == -1) {
437 if (state->reader_req != NULL) {
439 * Still waiting for reader
444 * Both socket and reader failed
446 tevent_req_nterror(req, map_nt_error_from_unix(err));
449 ok = tsocket_address_is_inet(state->addr, "ipv4");
453 ret = tsocket_address_bsd_sockaddr(state->addr,
457 tevent_req_nterror(req, map_nt_error_from_unix(errno));
461 state->packet = parse_packet((char *)state->buf, received, state->type,
462 addr.sin.sin_addr, addr.sin.sin_port);
463 if (state->packet == NULL) {
464 DEBUG(10, ("parse_packet failed\n"));
467 if ((state->trn_id != -1) &&
468 (state->trn_id != packet_trn_id(state->packet))) {
469 DEBUG(10, ("Expected transaction id %d, got %d\n",
470 state->trn_id, packet_trn_id(state->packet)));
474 if ((state->validator != NULL) &&
475 !state->validator(state->packet, state->private_data)) {
476 DEBUG(10, ("validator failed\n"));
480 tevent_req_done(req);
484 if (state->packet != NULL) {
485 free_packet(state->packet);
486 state->packet = NULL;
488 TALLOC_FREE(state->buf);
489 TALLOC_FREE(state->addr);
491 state->socket_req = tdgram_recvfrom_send(state, state->ev, state->sock);
492 if (tevent_req_nomem(state->socket_req, req)) {
495 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
499 static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
500 struct packet_struct **ppacket)
502 struct sock_packet_read_state *state = tevent_req_data(
503 req, struct sock_packet_read_state);
506 if (tevent_req_is_nterror(req, &status)) {
509 *ppacket = state->packet;
510 state->packet = NULL;
514 struct nb_trans_state {
515 struct tevent_context *ev;
516 struct tdgram_context *sock;
517 struct nb_packet_reader *reader;
519 struct tsocket_address *src_addr;
520 struct tsocket_address *dst_addr;
523 enum packet_type type;
526 bool (*validator)(struct packet_struct *p,
530 struct packet_struct *packet;
533 static int nb_trans_state_destructor(struct nb_trans_state *s);
534 static void nb_trans_got_reader(struct tevent_req *subreq);
535 static void nb_trans_done(struct tevent_req *subreq);
536 static void nb_trans_sent(struct tevent_req *subreq);
537 static void nb_trans_send_next(struct tevent_req *subreq);
539 static struct tevent_req *nb_trans_send(
541 struct tevent_context *ev,
542 const struct sockaddr_storage *_my_addr,
543 const struct sockaddr_storage *_dst_addr,
545 uint8_t *buf, size_t buflen,
546 enum packet_type type, int trn_id,
547 bool (*validator)(struct packet_struct *p,
551 const struct sockaddr *my_addr =
552 discard_const_p(const struct sockaddr, _my_addr);
553 size_t my_addr_len = sizeof(*_my_addr);
554 const struct sockaddr *dst_addr =
555 discard_const_p(const struct sockaddr, _dst_addr);
556 size_t dst_addr_len = sizeof(*_dst_addr);
557 struct tevent_req *req, *subreq;
558 struct nb_trans_state *state;
561 req = tevent_req_create(mem_ctx, &state, struct nb_trans_state);
565 talloc_set_destructor(state, nb_trans_state_destructor);
568 state->buflen = buflen;
570 state->trn_id = trn_id;
571 state->validator = validator;
572 state->private_data = private_data;
574 ret = tsocket_address_bsd_from_sockaddr(state,
575 my_addr, my_addr_len,
578 tevent_req_nterror(req, map_nt_error_from_unix(errno));
579 return tevent_req_post(req, ev);
582 ret = tsocket_address_bsd_from_sockaddr(state,
583 dst_addr, dst_addr_len,
586 tevent_req_nterror(req, map_nt_error_from_unix(errno));
587 return tevent_req_post(req, ev);
590 ret = tdgram_inet_udp_broadcast_socket(state->src_addr, state,
593 tevent_req_nterror(req, map_nt_error_from_unix(errno));
594 return tevent_req_post(req, ev);
597 subreq = nb_packet_reader_send(state, ev, type, state->trn_id, NULL);
598 if (tevent_req_nomem(subreq, req)) {
599 return tevent_req_post(req, ev);
601 tevent_req_set_callback(subreq, nb_trans_got_reader, req);
605 static int nb_trans_state_destructor(struct nb_trans_state *s)
607 if (s->packet != NULL) {
608 free_packet(s->packet);
614 static void nb_trans_got_reader(struct tevent_req *subreq)
616 struct tevent_req *req = tevent_req_callback_data(
617 subreq, struct tevent_req);
618 struct nb_trans_state *state = tevent_req_data(
619 req, struct nb_trans_state);
622 status = nb_packet_reader_recv(subreq, state, &state->reader);
625 if (!NT_STATUS_IS_OK(status)) {
626 DEBUG(10, ("nmbd not around\n"));
627 state->reader = NULL;
630 subreq = sock_packet_read_send(
631 state, state->ev, state->sock,
632 state->reader, state->type, state->trn_id,
633 state->validator, state->private_data);
634 if (tevent_req_nomem(subreq, req)) {
637 tevent_req_set_callback(subreq, nb_trans_done, req);
639 subreq = tdgram_sendto_send(state, state->ev,
641 state->buf, state->buflen,
643 if (tevent_req_nomem(subreq, req)) {
646 tevent_req_set_callback(subreq, nb_trans_sent, req);
649 static void nb_trans_sent(struct tevent_req *subreq)
651 struct tevent_req *req = tevent_req_callback_data(
652 subreq, struct tevent_req);
653 struct nb_trans_state *state = tevent_req_data(
654 req, struct nb_trans_state);
658 sent = tdgram_sendto_recv(subreq, &err);
661 DEBUG(10, ("sendto failed: %s\n", strerror(err)));
662 tevent_req_nterror(req, map_nt_error_from_unix(err));
665 subreq = tevent_wakeup_send(state, state->ev,
666 timeval_current_ofs(1, 0));
667 if (tevent_req_nomem(subreq, req)) {
670 tevent_req_set_callback(subreq, nb_trans_send_next, req);
673 static void nb_trans_send_next(struct tevent_req *subreq)
675 struct tevent_req *req = tevent_req_callback_data(
676 subreq, struct tevent_req);
677 struct nb_trans_state *state = tevent_req_data(
678 req, struct nb_trans_state);
681 ret = tevent_wakeup_recv(subreq);
684 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
687 subreq = tdgram_sendto_send(state, state->ev,
689 state->buf, state->buflen,
691 if (tevent_req_nomem(subreq, req)) {
694 tevent_req_set_callback(subreq, nb_trans_sent, req);
697 static void nb_trans_done(struct tevent_req *subreq)
699 struct tevent_req *req = tevent_req_callback_data(
700 subreq, struct tevent_req);
701 struct nb_trans_state *state = tevent_req_data(
702 req, struct nb_trans_state);
705 status = sock_packet_read_recv(subreq, &state->packet);
707 if (tevent_req_nterror(req, status)) {
710 tevent_req_done(req);
713 static NTSTATUS nb_trans_recv(struct tevent_req *req,
714 struct packet_struct **ppacket)
716 struct nb_trans_state *state = tevent_req_data(
717 req, struct nb_trans_state);
720 if (tevent_req_is_nterror(req, &status)) {
723 *ppacket = state->packet;
724 state->packet = NULL;
728 /****************************************************************************
729 Do a NBT node status query on an open socket and return an array of
730 structures holding the returned names or NULL if the query failed.
731 **************************************************************************/
733 struct node_status_query_state {
734 struct sockaddr_storage my_addr;
735 struct sockaddr_storage addr;
738 struct packet_struct *packet;
741 static int node_status_query_state_destructor(
742 struct node_status_query_state *s);
743 static bool node_status_query_validator(struct packet_struct *p,
745 static void node_status_query_done(struct tevent_req *subreq);
747 struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
748 struct tevent_context *ev,
749 struct nmb_name *name,
750 const struct sockaddr_storage *addr)
752 struct tevent_req *req, *subreq;
753 struct node_status_query_state *state;
754 struct packet_struct p;
755 struct nmb_packet *nmb = &p.packet.nmb;
756 struct sockaddr_in *in_addr;
758 req = tevent_req_create(mem_ctx, &state,
759 struct node_status_query_state);
763 talloc_set_destructor(state, node_status_query_state_destructor);
765 if (addr->ss_family != AF_INET) {
766 /* Can't do node status to IPv6 */
767 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
768 return tevent_req_post(req, ev);
772 in_addr = (struct sockaddr_in *)(void *)&state->addr;
773 in_addr->sin_port = htons(NMB_PORT);
775 set_socket_addr_v4(&state->my_addr);
778 nmb->header.name_trn_id = generate_trn_id();
779 nmb->header.opcode = 0;
780 nmb->header.response = false;
781 nmb->header.nm_flags.bcast = false;
782 nmb->header.nm_flags.recursion_available = false;
783 nmb->header.nm_flags.recursion_desired = false;
784 nmb->header.nm_flags.trunc = false;
785 nmb->header.nm_flags.authoritative = false;
786 nmb->header.rcode = 0;
787 nmb->header.qdcount = 1;
788 nmb->header.ancount = 0;
789 nmb->header.nscount = 0;
790 nmb->header.arcount = 0;
791 nmb->question.question_name = *name;
792 nmb->question.question_type = 0x21;
793 nmb->question.question_class = 0x1;
795 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
797 if (state->buflen == 0) {
798 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
799 DEBUG(10, ("build_packet failed\n"));
800 return tevent_req_post(req, ev);
803 subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, false,
804 state->buf, state->buflen,
805 NMB_PACKET, nmb->header.name_trn_id,
806 node_status_query_validator, NULL);
807 if (tevent_req_nomem(subreq, req)) {
808 DEBUG(10, ("nb_trans_send failed\n"));
809 return tevent_req_post(req, ev);
811 if (!tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0))) {
812 return tevent_req_post(req, ev);
814 tevent_req_set_callback(subreq, node_status_query_done, req);
818 static bool node_status_query_validator(struct packet_struct *p,
821 struct nmb_packet *nmb = &p->packet.nmb;
824 if (nmb->header.opcode != 0 ||
825 nmb->header.nm_flags.bcast ||
827 !nmb->header.ancount ||
828 nmb->answers->rr_type != 0x21) {
830 * XXXX what do we do with this? could be a redirect,
831 * but we'll discard it for the moment
838 static int node_status_query_state_destructor(
839 struct node_status_query_state *s)
841 if (s->packet != NULL) {
842 free_packet(s->packet);
848 static void node_status_query_done(struct tevent_req *subreq)
850 struct tevent_req *req = tevent_req_callback_data(
851 subreq, struct tevent_req);
852 struct node_status_query_state *state = tevent_req_data(
853 req, struct node_status_query_state);
856 status = nb_trans_recv(subreq, &state->packet);
858 if (tevent_req_nterror(req, status)) {
861 tevent_req_done(req);
864 NTSTATUS node_status_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
865 struct node_status **pnode_status,
867 struct node_status_extra *extra)
869 struct node_status_query_state *state = tevent_req_data(
870 req, struct node_status_query_state);
871 struct node_status *node_status;
875 if (tevent_req_is_nterror(req, &status)) {
878 node_status = parse_node_status(
879 mem_ctx, &state->packet->packet.nmb.answers->rdata[0],
881 if (node_status == NULL) {
882 return NT_STATUS_NO_MEMORY;
884 *pnode_status = node_status;
885 *pnum_names = num_names;
889 NTSTATUS node_status_query(TALLOC_CTX *mem_ctx, struct nmb_name *name,
890 const struct sockaddr_storage *addr,
891 struct node_status **pnode_status,
893 struct node_status_extra *extra)
895 TALLOC_CTX *frame = talloc_stackframe();
896 struct tevent_context *ev;
897 struct tevent_req *req;
898 NTSTATUS status = NT_STATUS_NO_MEMORY;
900 ev = samba_tevent_context_init(frame);
904 req = node_status_query_send(ev, ev, name, addr);
908 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
911 status = node_status_query_recv(req, mem_ctx, pnode_status,
918 /****************************************************************************
919 Find the first type XX name in a node status reply - used for finding
920 a servers name given its IP. Return the matched name in *name.
921 **************************************************************************/
923 bool name_status_find(const char *q_name,
926 const struct sockaddr_storage *to_ss,
929 char addr[INET6_ADDRSTRLEN];
930 struct sockaddr_storage ss;
931 struct node_status *addrs = NULL;
932 struct nmb_name nname;
937 if (lp_disable_netbios()) {
938 DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n",
943 print_sockaddr(addr, sizeof(addr), to_ss);
945 DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
948 /* Check the cache first. */
950 if (namecache_status_fetch(q_name, q_type, type, to_ss, name)) {
954 if (to_ss->ss_family != AF_INET) {
955 /* Can't do node status to IPv6 */
959 set_socket_addr_v4(&ss);
961 /* W2K PDC's seem not to respond to '*'#0. JRA */
962 make_nmb_name(&nname, q_name, q_type);
963 status = node_status_query(talloc_tos(), &nname, to_ss,
964 &addrs, &count, NULL);
965 if (!NT_STATUS_IS_OK(status)) {
969 for (i=0;i<count;i++) {
970 /* Find first one of the requested type that's not a GROUP. */
971 if (addrs[i].type == type && ! (addrs[i].flags & 0x80))
977 pull_ascii_nstring(name, sizeof(fstring), addrs[i].name);
979 /* Store the result in the cache. */
980 /* but don't store an entry for 0x1c names here. Here we have
981 a single host and DOMAIN<0x1c> names should be a list of hosts */
983 if ( q_type != 0x1c ) {
984 namecache_status_store(q_name, q_type, type, to_ss, name);
992 DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
995 DEBUGADD(10, (", name %s ip address is %s", name, addr));
1003 comparison function used by sort_addr_list
1006 static int addr_compare(const struct sockaddr_storage *ss1,
1007 const struct sockaddr_storage *ss2)
1009 int max_bits1=0, max_bits2=0;
1010 int num_interfaces = iface_count();
1013 /* Sort IPv4 addresses first. */
1014 if (ss1->ss_family != ss2->ss_family) {
1015 if (ss2->ss_family == AF_INET) {
1022 /* Here we know both addresses are of the same
1025 for (i=0;i<num_interfaces;i++) {
1026 const struct sockaddr_storage *pss = iface_n_bcast(i);
1027 const unsigned char *p_ss1 = NULL;
1028 const unsigned char *p_ss2 = NULL;
1029 const unsigned char *p_if = NULL;
1033 if (pss->ss_family != ss1->ss_family) {
1034 /* Ignore interfaces of the wrong type. */
1037 if (pss->ss_family == AF_INET) {
1038 p_if = (const unsigned char *)
1039 &((const struct sockaddr_in *)pss)->sin_addr;
1040 p_ss1 = (const unsigned char *)
1041 &((const struct sockaddr_in *)ss1)->sin_addr;
1042 p_ss2 = (const unsigned char *)
1043 &((const struct sockaddr_in *)ss2)->sin_addr;
1046 #if defined(HAVE_IPV6)
1047 if (pss->ss_family == AF_INET6) {
1048 p_if = (const unsigned char *)
1049 &((const struct sockaddr_in6 *)pss)->sin6_addr;
1050 p_ss1 = (const unsigned char *)
1051 &((const struct sockaddr_in6 *)ss1)->sin6_addr;
1052 p_ss2 = (const unsigned char *)
1053 &((const struct sockaddr_in6 *)ss2)->sin6_addr;
1057 if (!p_ss1 || !p_ss2 || !p_if || len == 0) {
1060 bits1 = matching_len_bits(p_ss1, p_if, len);
1061 bits2 = matching_len_bits(p_ss2, p_if, len);
1062 max_bits1 = MAX(bits1, max_bits1);
1063 max_bits2 = MAX(bits2, max_bits2);
1066 /* Bias towards directly reachable IPs */
1067 if (iface_local((const struct sockaddr *)ss1)) {
1068 if (ss1->ss_family == AF_INET) {
1074 if (iface_local((const struct sockaddr *)ss2)) {
1075 if (ss2->ss_family == AF_INET) {
1081 return max_bits2 - max_bits1;
1084 /*******************************************************************
1085 compare 2 ldap IPs by nearness to our interfaces - used in qsort
1086 *******************************************************************/
1088 static int ip_service_compare(struct ip_service *ss1, struct ip_service *ss2)
1092 if ((result = addr_compare(&ss1->ss, &ss2->ss)) != 0) {
1096 if (ss1->port > ss2->port) {
1100 if (ss1->port < ss2->port) {
1108 sort an IP list so that names that are close to one of our interfaces
1109 are at the top. This prevents the problem where a WINS server returns an IP
1110 that is not reachable from our subnet as the first match
1113 static void sort_addr_list(struct sockaddr_storage *sslist, int count)
1119 TYPESAFE_QSORT(sslist, count, addr_compare);
1122 static void sort_service_list(struct ip_service *servlist, int count)
1128 TYPESAFE_QSORT(servlist, count, ip_service_compare);
1131 /**********************************************************************
1132 Remove any duplicate address/port pairs in the list
1133 *********************************************************************/
1135 int remove_duplicate_addrs2(struct ip_service *iplist, int count )
1139 DEBUG(10,("remove_duplicate_addrs2: "
1140 "looking for duplicate address/port pairs\n"));
1142 /* One loop to set duplicates to a zero addr. */
1143 for ( i=0; i<count; i++ ) {
1144 if ( is_zero_addr(&iplist[i].ss)) {
1148 for ( j=i+1; j<count; j++ ) {
1149 if (sockaddr_equal((struct sockaddr *)(void *)&iplist[i].ss,
1150 (struct sockaddr *)(void *)&iplist[j].ss) &&
1151 iplist[i].port == iplist[j].port) {
1152 zero_sockaddr(&iplist[j].ss);
1157 /* Now remove any addresses set to zero above. */
1158 for (i = 0; i < count; i++) {
1160 is_zero_addr(&iplist[i].ss)) {
1164 (count-i-1)*sizeof(struct ip_service));
1173 static bool prioritize_ipv4_list(struct ip_service *iplist, int count)
1175 TALLOC_CTX *frame = talloc_stackframe();
1176 struct ip_service *iplist_new = talloc_array(frame, struct ip_service, count);
1179 if (iplist_new == NULL) {
1186 /* Copy IPv4 first. */
1187 for (i = 0; i < count; i++) {
1188 if (iplist[i].ss.ss_family == AF_INET) {
1189 iplist_new[j++] = iplist[i];
1194 for (i = 0; i < count; i++) {
1195 if (iplist[i].ss.ss_family != AF_INET) {
1196 iplist_new[j++] = iplist[i];
1200 memcpy(iplist, iplist_new, sizeof(struct ip_service)*count);
1205 /****************************************************************************
1206 Do a netbios name query to find someones IP.
1207 Returns an array of IP addresses or NULL if none.
1208 *count will be set to the number of addresses returned.
1209 *timed_out is set if we failed by timing out
1210 ****************************************************************************/
1212 struct name_query_state {
1213 struct sockaddr_storage my_addr;
1214 struct sockaddr_storage addr;
1221 NTSTATUS validate_error;
1224 struct sockaddr_storage *addrs;
1228 static bool name_query_validator(struct packet_struct *p, void *private_data);
1229 static void name_query_done(struct tevent_req *subreq);
1231 struct tevent_req *name_query_send(TALLOC_CTX *mem_ctx,
1232 struct tevent_context *ev,
1233 const char *name, int name_type,
1234 bool bcast, bool recurse,
1235 const struct sockaddr_storage *addr)
1237 struct tevent_req *req, *subreq;
1238 struct name_query_state *state;
1239 struct packet_struct p;
1240 struct nmb_packet *nmb = &p.packet.nmb;
1241 struct sockaddr_in *in_addr;
1243 req = tevent_req_create(mem_ctx, &state, struct name_query_state);
1247 state->bcast = bcast;
1249 if (addr->ss_family != AF_INET) {
1250 /* Can't do node status to IPv6 */
1251 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1252 return tevent_req_post(req, ev);
1255 if (lp_disable_netbios()) {
1256 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
1258 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
1259 return tevent_req_post(req, ev);
1262 state->addr = *addr;
1263 in_addr = (struct sockaddr_in *)(void *)&state->addr;
1264 in_addr->sin_port = htons(NMB_PORT);
1266 set_socket_addr_v4(&state->my_addr);
1269 nmb->header.name_trn_id = generate_trn_id();
1270 nmb->header.opcode = 0;
1271 nmb->header.response = false;
1272 nmb->header.nm_flags.bcast = bcast;
1273 nmb->header.nm_flags.recursion_available = false;
1274 nmb->header.nm_flags.recursion_desired = recurse;
1275 nmb->header.nm_flags.trunc = false;
1276 nmb->header.nm_flags.authoritative = false;
1277 nmb->header.rcode = 0;
1278 nmb->header.qdcount = 1;
1279 nmb->header.ancount = 0;
1280 nmb->header.nscount = 0;
1281 nmb->header.arcount = 0;
1283 make_nmb_name(&nmb->question.question_name,name,name_type);
1285 nmb->question.question_type = 0x20;
1286 nmb->question.question_class = 0x1;
1288 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
1290 if (state->buflen == 0) {
1291 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1292 DEBUG(10, ("build_packet failed\n"));
1293 return tevent_req_post(req, ev);
1296 subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, bcast,
1297 state->buf, state->buflen,
1298 NMB_PACKET, nmb->header.name_trn_id,
1299 name_query_validator, state);
1300 if (tevent_req_nomem(subreq, req)) {
1301 DEBUG(10, ("nb_trans_send failed\n"));
1302 return tevent_req_post(req, ev);
1304 tevent_req_set_callback(subreq, name_query_done, req);
1308 static bool name_query_validator(struct packet_struct *p, void *private_data)
1310 struct name_query_state *state = talloc_get_type_abort(
1311 private_data, struct name_query_state);
1312 struct nmb_packet *nmb = &p->packet.nmb;
1313 struct sockaddr_storage *tmp_addrs;
1314 bool got_unique_netbios_name = false;
1317 debug_nmb_packet(p);
1320 * If we get a Negative Name Query Response from a WINS
1321 * server, we should report it and give up.
1323 if( 0 == nmb->header.opcode /* A query response */
1324 && !state->bcast /* from a WINS server */
1325 && nmb->header.rcode /* Error returned */
1328 if( DEBUGLVL( 3 ) ) {
1329 /* Only executed if DEBUGLEVEL >= 3 */
1330 dbgtext( "Negative name query "
1331 "response, rcode 0x%02x: ",
1332 nmb->header.rcode );
1333 switch( nmb->header.rcode ) {
1335 dbgtext("Request was invalidly formatted.\n");
1338 dbgtext("Problem with NBNS, cannot process "
1342 dbgtext("The name requested does not "
1346 dbgtext("Unsupported request error.\n");
1349 dbgtext("Query refused error.\n");
1352 dbgtext("Unrecognized error code.\n" );
1358 * We accept this packet as valid, but tell the upper
1359 * layers that it's a negative response.
1361 state->validate_error = NT_STATUS_NOT_FOUND;
1365 if (nmb->header.opcode != 0 ||
1366 nmb->header.nm_flags.bcast ||
1367 nmb->header.rcode ||
1368 !nmb->header.ancount) {
1370 * XXXX what do we do with this? Could be a redirect,
1371 * but we'll discard it for the moment.
1376 tmp_addrs = talloc_realloc(
1377 state, state->addrs, struct sockaddr_storage,
1378 state->num_addrs + nmb->answers->rdlength/6);
1379 if (tmp_addrs == NULL) {
1380 state->validate_error = NT_STATUS_NO_MEMORY;
1383 state->addrs = tmp_addrs;
1385 DEBUG(2,("Got a positive name query response "
1386 "from %s ( ", inet_ntoa(p->ip)));
1388 for (i=0; i<nmb->answers->rdlength/6; i++) {
1391 struct sockaddr_storage addr;
1394 flags = RSVAL(&nmb->answers->rdata[i*6], 0);
1395 got_unique_netbios_name |= ((flags & 0x8000) == 0);
1397 putip((char *)&ip,&nmb->answers->rdata[2+i*6]);
1398 in_addr_to_sockaddr_storage(&addr, ip);
1400 if (is_zero_addr(&addr)) {
1404 for (j=0; j<state->num_addrs; j++) {
1406 (struct sockaddr *)(void *)&addr,
1407 (struct sockaddr *)(void *)&state->addrs[j])) {
1411 if (j < state->num_addrs) {
1412 /* Already got it */
1416 DEBUGADD(2,("%s ",inet_ntoa(ip)));
1418 state->addrs[state->num_addrs] = addr;
1419 state->num_addrs += 1;
1421 DEBUGADD(2,(")\n"));
1423 /* We add the flags back ... */
1424 if (nmb->header.response)
1425 state->flags |= NM_FLAGS_RS;
1426 if (nmb->header.nm_flags.authoritative)
1427 state->flags |= NM_FLAGS_AA;
1428 if (nmb->header.nm_flags.trunc)
1429 state->flags |= NM_FLAGS_TC;
1430 if (nmb->header.nm_flags.recursion_desired)
1431 state->flags |= NM_FLAGS_RD;
1432 if (nmb->header.nm_flags.recursion_available)
1433 state->flags |= NM_FLAGS_RA;
1434 if (nmb->header.nm_flags.bcast)
1435 state->flags |= NM_FLAGS_B;
1439 * We have to collect all entries coming in from broadcast
1440 * queries. If we got a unique name, we're done.
1442 return got_unique_netbios_name;
1445 * WINS responses are accepted when they are received
1450 static void name_query_done(struct tevent_req *subreq)
1452 struct tevent_req *req = tevent_req_callback_data(
1453 subreq, struct tevent_req);
1454 struct name_query_state *state = tevent_req_data(
1455 req, struct name_query_state);
1457 struct packet_struct *p = NULL;
1459 status = nb_trans_recv(subreq, &p);
1460 TALLOC_FREE(subreq);
1461 if (tevent_req_nterror(req, status)) {
1464 if (!NT_STATUS_IS_OK(state->validate_error)) {
1465 tevent_req_nterror(req, state->validate_error);
1470 * Free the packet here, we've collected the response in the
1475 tevent_req_done(req);
1478 NTSTATUS name_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1479 struct sockaddr_storage **addrs, int *num_addrs,
1482 struct name_query_state *state = tevent_req_data(
1483 req, struct name_query_state);
1486 if (tevent_req_is_nterror(req, &status)) {
1488 NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
1490 * In the broadcast case we collect replies until the
1493 status = NT_STATUS_OK;
1495 if (!NT_STATUS_IS_OK(status)) {
1499 if (state->num_addrs == 0) {
1500 return NT_STATUS_NOT_FOUND;
1502 *addrs = talloc_move(mem_ctx, &state->addrs);
1503 sort_addr_list(*addrs, state->num_addrs);
1504 *num_addrs = state->num_addrs;
1505 if (flags != NULL) {
1506 *flags = state->flags;
1508 return NT_STATUS_OK;
1511 NTSTATUS name_query(const char *name, int name_type,
1512 bool bcast, bool recurse,
1513 const struct sockaddr_storage *to_ss,
1514 TALLOC_CTX *mem_ctx,
1515 struct sockaddr_storage **addrs,
1516 int *num_addrs, uint8_t *flags)
1518 TALLOC_CTX *frame = talloc_stackframe();
1519 struct tevent_context *ev;
1520 struct tevent_req *req;
1521 struct timeval timeout;
1522 NTSTATUS status = NT_STATUS_NO_MEMORY;
1524 ev = samba_tevent_context_init(frame);
1528 req = name_query_send(ev, ev, name, name_type, bcast, recurse, to_ss);
1533 timeout = timeval_current_ofs(0, 250000);
1535 timeout = timeval_current_ofs(2, 0);
1537 if (!tevent_req_set_endtime(req, ev, timeout)) {
1540 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1543 status = name_query_recv(req, mem_ctx, addrs, num_addrs, flags);
1549 /********************************************************
1550 Convert an array if struct sockaddr_storage to struct ip_service
1551 return false on failure. Port is set to PORT_NONE;
1552 pcount is [in/out] - it is the length of ss_list on input,
1553 and the length of return_iplist on output as we remove any
1554 zero addresses from ss_list.
1555 *********************************************************/
1557 static bool convert_ss2service(struct ip_service **return_iplist,
1558 const struct sockaddr_storage *ss_list,
1562 int orig_count = *pcount;
1565 if (orig_count==0 || !ss_list )
1568 /* Filter out zero addrs. */
1569 for ( i=0; i<orig_count; i++ ) {
1570 if (is_zero_addr(&ss_list[i])) {
1575 if (real_count==0) {
1579 /* copy the ip address; port will be PORT_NONE */
1580 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, real_count)) ==
1582 DEBUG(0,("convert_ip2service: malloc failed "
1583 "for %d enetries!\n", real_count ));
1587 for ( i=0, real_count = 0; i<orig_count; i++ ) {
1588 if (is_zero_addr(&ss_list[i])) {
1591 (*return_iplist)[real_count].ss = ss_list[i];
1592 (*return_iplist)[real_count].port = PORT_NONE;
1596 *pcount = real_count;
1600 struct name_queries_state {
1601 struct tevent_context *ev;
1606 const struct sockaddr_storage *addrs;
1611 struct tevent_req **subreqs;
1616 struct sockaddr_storage *result_addrs;
1617 int num_result_addrs;
1621 static void name_queries_done(struct tevent_req *subreq);
1622 static void name_queries_next(struct tevent_req *subreq);
1625 * Send a name query to multiple destinations with a wait time in between
1628 static struct tevent_req *name_queries_send(
1629 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1630 const char *name, int name_type,
1631 bool bcast, bool recurse,
1632 const struct sockaddr_storage *addrs,
1633 int num_addrs, int wait_msec, int timeout_msec)
1635 struct tevent_req *req, *subreq;
1636 struct name_queries_state *state;
1638 req = tevent_req_create(mem_ctx, &state,
1639 struct name_queries_state);
1645 state->name_type = name_type;
1646 state->bcast = bcast;
1647 state->recurse = recurse;
1648 state->addrs = addrs;
1649 state->num_addrs = num_addrs;
1650 state->wait_msec = wait_msec;
1651 state->timeout_msec = timeout_msec;
1653 state->subreqs = talloc_zero_array(
1654 state, struct tevent_req *, num_addrs);
1655 if (tevent_req_nomem(state->subreqs, req)) {
1656 return tevent_req_post(req, ev);
1658 state->num_sent = 0;
1660 subreq = name_query_send(
1661 state->subreqs, state->ev, name, name_type, bcast, recurse,
1662 &state->addrs[state->num_sent]);
1663 if (tevent_req_nomem(subreq, req)) {
1664 return tevent_req_post(req, ev);
1666 if (!tevent_req_set_endtime(
1668 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1669 tevent_req_oom(req);
1670 return tevent_req_post(req, ev);
1672 tevent_req_set_callback(subreq, name_queries_done, req);
1674 state->subreqs[state->num_sent] = subreq;
1675 state->num_sent += 1;
1677 if (state->num_sent < state->num_addrs) {
1678 subreq = tevent_wakeup_send(
1680 timeval_current_ofs(0, state->wait_msec * 1000));
1681 if (tevent_req_nomem(subreq, req)) {
1682 return tevent_req_post(req, ev);
1684 tevent_req_set_callback(subreq, name_queries_next, req);
1689 static void name_queries_done(struct tevent_req *subreq)
1691 struct tevent_req *req = tevent_req_callback_data(
1692 subreq, struct tevent_req);
1693 struct name_queries_state *state = tevent_req_data(
1694 req, struct name_queries_state);
1698 status = name_query_recv(subreq, state, &state->result_addrs,
1699 &state->num_result_addrs, &state->flags);
1701 for (i=0; i<state->num_sent; i++) {
1702 if (state->subreqs[i] == subreq) {
1706 if (i == state->num_sent) {
1707 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1710 TALLOC_FREE(state->subreqs[i]);
1712 state->num_received += 1;
1714 if (!NT_STATUS_IS_OK(status)) {
1716 if (state->num_received >= state->num_addrs) {
1717 tevent_req_nterror(req, status);
1721 * Still outstanding requests, just wait
1725 state->received_index = i;
1726 tevent_req_done(req);
1729 static void name_queries_next(struct tevent_req *subreq)
1731 struct tevent_req *req = tevent_req_callback_data(
1732 subreq, struct tevent_req);
1733 struct name_queries_state *state = tevent_req_data(
1734 req, struct name_queries_state);
1736 if (!tevent_wakeup_recv(subreq)) {
1737 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1741 subreq = name_query_send(
1742 state->subreqs, state->ev,
1743 state->name, state->name_type, state->bcast, state->recurse,
1744 &state->addrs[state->num_sent]);
1745 if (tevent_req_nomem(subreq, req)) {
1748 tevent_req_set_callback(subreq, name_queries_done, req);
1749 if (!tevent_req_set_endtime(
1751 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1752 tevent_req_oom(req);
1755 state->subreqs[state->num_sent] = subreq;
1756 state->num_sent += 1;
1758 if (state->num_sent < state->num_addrs) {
1759 subreq = tevent_wakeup_send(
1761 timeval_current_ofs(0, state->wait_msec * 1000));
1762 if (tevent_req_nomem(subreq, req)) {
1765 tevent_req_set_callback(subreq, name_queries_next, req);
1769 static NTSTATUS name_queries_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1770 struct sockaddr_storage **result_addrs,
1771 int *num_result_addrs, uint8_t *flags,
1772 int *received_index)
1774 struct name_queries_state *state = tevent_req_data(
1775 req, struct name_queries_state);
1778 if (tevent_req_is_nterror(req, &status)) {
1782 if (result_addrs != NULL) {
1783 *result_addrs = talloc_move(mem_ctx, &state->result_addrs);
1785 if (num_result_addrs != NULL) {
1786 *num_result_addrs = state->num_result_addrs;
1788 if (flags != NULL) {
1789 *flags = state->flags;
1791 if (received_index != NULL) {
1792 *received_index = state->received_index;
1794 return NT_STATUS_OK;
1797 /********************************************************
1798 Resolve via "bcast" method.
1799 *********************************************************/
1801 struct name_resolve_bcast_state {
1802 struct sockaddr_storage *addrs;
1806 static void name_resolve_bcast_done(struct tevent_req *subreq);
1808 struct tevent_req *name_resolve_bcast_send(TALLOC_CTX *mem_ctx,
1809 struct tevent_context *ev,
1813 struct tevent_req *req, *subreq;
1814 struct name_resolve_bcast_state *state;
1815 struct sockaddr_storage *bcast_addrs;
1816 int i, num_addrs, num_bcast_addrs;
1818 req = tevent_req_create(mem_ctx, &state,
1819 struct name_resolve_bcast_state);
1824 if (lp_disable_netbios()) {
1825 DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
1827 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1828 return tevent_req_post(req, ev);
1832 * "bcast" means do a broadcast lookup on all the local interfaces.
1835 DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
1836 "for name %s<0x%x>\n", name, name_type));
1838 num_addrs = iface_count();
1839 bcast_addrs = talloc_array(state, struct sockaddr_storage, num_addrs);
1840 if (tevent_req_nomem(bcast_addrs, req)) {
1841 return tevent_req_post(req, ev);
1845 * Lookup the name on all the interfaces, return on
1846 * the first successful match.
1848 num_bcast_addrs = 0;
1850 for (i=0; i<num_addrs; i++) {
1851 const struct sockaddr_storage *pss = iface_n_bcast(i);
1853 if (pss->ss_family != AF_INET) {
1856 bcast_addrs[num_bcast_addrs] = *pss;
1857 num_bcast_addrs += 1;
1860 subreq = name_queries_send(state, ev, name, name_type, true, true,
1861 bcast_addrs, num_bcast_addrs, 0, 1000);
1862 if (tevent_req_nomem(subreq, req)) {
1863 return tevent_req_post(req, ev);
1865 tevent_req_set_callback(subreq, name_resolve_bcast_done, req);
1869 static void name_resolve_bcast_done(struct tevent_req *subreq)
1871 struct tevent_req *req = tevent_req_callback_data(
1872 subreq, struct tevent_req);
1873 struct name_resolve_bcast_state *state = tevent_req_data(
1874 req, struct name_resolve_bcast_state);
1877 status = name_queries_recv(subreq, state,
1878 &state->addrs, &state->num_addrs,
1880 TALLOC_FREE(subreq);
1881 if (tevent_req_nterror(req, status)) {
1884 tevent_req_done(req);
1887 NTSTATUS name_resolve_bcast_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1888 struct sockaddr_storage **addrs,
1891 struct name_resolve_bcast_state *state = tevent_req_data(
1892 req, struct name_resolve_bcast_state);
1895 if (tevent_req_is_nterror(req, &status)) {
1898 *addrs = talloc_move(mem_ctx, &state->addrs);
1899 *num_addrs = state->num_addrs;
1900 return NT_STATUS_OK;
1903 NTSTATUS name_resolve_bcast(const char *name,
1905 TALLOC_CTX *mem_ctx,
1906 struct sockaddr_storage **return_iplist,
1909 TALLOC_CTX *frame = talloc_stackframe();
1910 struct tevent_context *ev;
1911 struct tevent_req *req;
1912 NTSTATUS status = NT_STATUS_NO_MEMORY;
1914 ev = samba_tevent_context_init(frame);
1918 req = name_resolve_bcast_send(frame, ev, name, name_type);
1922 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1925 status = name_resolve_bcast_recv(req, mem_ctx, return_iplist,
1932 struct query_wins_list_state {
1933 struct tevent_context *ev;
1936 struct in_addr *servers;
1937 uint32_t num_servers;
1938 struct sockaddr_storage server;
1941 struct sockaddr_storage *addrs;
1946 static void query_wins_list_done(struct tevent_req *subreq);
1949 * Query a list of (replicating) wins servers in sequence, call them
1950 * dead if they don't reply
1953 static struct tevent_req *query_wins_list_send(
1954 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1955 struct in_addr src_ip, const char *name, uint8_t name_type,
1956 struct in_addr *servers, int num_servers)
1958 struct tevent_req *req, *subreq;
1959 struct query_wins_list_state *state;
1961 req = tevent_req_create(mem_ctx, &state,
1962 struct query_wins_list_state);
1968 state->name_type = name_type;
1969 state->servers = servers;
1970 state->num_servers = num_servers;
1972 if (state->num_servers == 0) {
1973 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
1974 return tevent_req_post(req, ev);
1977 in_addr_to_sockaddr_storage(
1978 &state->server, state->servers[state->num_sent]);
1980 subreq = name_query_send(state, state->ev,
1981 state->name, state->name_type,
1982 false, true, &state->server);
1983 state->num_sent += 1;
1984 if (tevent_req_nomem(subreq, req)) {
1985 return tevent_req_post(req, ev);
1987 if (!tevent_req_set_endtime(subreq, state->ev,
1988 timeval_current_ofs(2, 0))) {
1989 tevent_req_oom(req);
1990 return tevent_req_post(req, ev);
1992 tevent_req_set_callback(subreq, query_wins_list_done, req);
1996 static void query_wins_list_done(struct tevent_req *subreq)
1998 struct tevent_req *req = tevent_req_callback_data(
1999 subreq, struct tevent_req);
2000 struct query_wins_list_state *state = tevent_req_data(
2001 req, struct query_wins_list_state);
2004 status = name_query_recv(subreq, state,
2005 &state->addrs, &state->num_addrs,
2007 TALLOC_FREE(subreq);
2008 if (NT_STATUS_IS_OK(status)) {
2009 tevent_req_done(req);
2012 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
2013 tevent_req_nterror(req, status);
2016 wins_srv_died(state->servers[state->num_sent-1],
2017 my_socket_addr_v4());
2019 if (state->num_sent == state->num_servers) {
2020 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2024 in_addr_to_sockaddr_storage(
2025 &state->server, state->servers[state->num_sent]);
2027 subreq = name_query_send(state, state->ev,
2028 state->name, state->name_type,
2029 false, true, &state->server);
2030 state->num_sent += 1;
2031 if (tevent_req_nomem(subreq, req)) {
2034 if (!tevent_req_set_endtime(subreq, state->ev,
2035 timeval_current_ofs(2, 0))) {
2036 tevent_req_oom(req);
2039 tevent_req_set_callback(subreq, query_wins_list_done, req);
2042 static NTSTATUS query_wins_list_recv(struct tevent_req *req,
2043 TALLOC_CTX *mem_ctx,
2044 struct sockaddr_storage **addrs,
2048 struct query_wins_list_state *state = tevent_req_data(
2049 req, struct query_wins_list_state);
2052 if (tevent_req_is_nterror(req, &status)) {
2055 if (addrs != NULL) {
2056 *addrs = talloc_move(mem_ctx, &state->addrs);
2058 if (num_addrs != NULL) {
2059 *num_addrs = state->num_addrs;
2061 if (flags != NULL) {
2062 *flags = state->flags;
2064 return NT_STATUS_OK;
2067 struct resolve_wins_state {
2071 struct sockaddr_storage *addrs;
2076 static void resolve_wins_done(struct tevent_req *subreq);
2078 struct tevent_req *resolve_wins_send(TALLOC_CTX *mem_ctx,
2079 struct tevent_context *ev,
2083 struct tevent_req *req, *subreq;
2084 struct resolve_wins_state *state;
2085 char **wins_tags = NULL;
2086 struct sockaddr_storage src_ss;
2087 struct in_addr src_ip;
2088 int i, num_wins_tags;
2090 req = tevent_req_create(mem_ctx, &state,
2091 struct resolve_wins_state);
2096 if (wins_srv_count() < 1) {
2097 DEBUG(3,("resolve_wins: WINS server resolution selected "
2098 "and no WINS servers listed.\n"));
2099 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2103 /* the address we will be sending from */
2104 if (!interpret_string_addr(&src_ss, lp_nbt_client_socket_address(),
2105 AI_NUMERICHOST|AI_PASSIVE)) {
2106 zero_sockaddr(&src_ss);
2109 if (src_ss.ss_family != AF_INET) {
2110 char addr[INET6_ADDRSTRLEN];
2111 print_sockaddr(addr, sizeof(addr), &src_ss);
2112 DEBUG(3,("resolve_wins: cannot receive WINS replies "
2113 "on IPv6 address %s\n",
2115 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2119 src_ip = ((const struct sockaddr_in *)(void *)&src_ss)->sin_addr;
2121 wins_tags = wins_srv_tags();
2122 if (wins_tags == NULL) {
2123 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2128 while (wins_tags[num_wins_tags] != NULL) {
2132 for (i=0; i<num_wins_tags; i++) {
2133 int num_servers, num_alive;
2134 struct in_addr *servers, *alive;
2137 if (!wins_server_tag_ips(wins_tags[i], talloc_tos(),
2138 &servers, &num_servers)) {
2139 DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
2144 alive = talloc_array(state, struct in_addr, num_servers);
2145 if (tevent_req_nomem(alive, req)) {
2150 for (j=0; j<num_servers; j++) {
2151 struct in_addr wins_ip = servers[j];
2153 if (global_in_nmbd && ismyip_v4(wins_ip)) {
2154 /* yikes! we'll loop forever */
2157 /* skip any that have been unresponsive lately */
2158 if (wins_srv_is_dead(wins_ip, src_ip)) {
2161 DEBUG(3, ("resolve_wins: using WINS server %s "
2163 inet_ntoa(wins_ip), wins_tags[i]));
2164 alive[num_alive] = wins_ip;
2167 TALLOC_FREE(servers);
2169 if (num_alive == 0) {
2173 subreq = query_wins_list_send(
2174 state, ev, src_ip, name, name_type,
2176 if (tevent_req_nomem(subreq, req)) {
2179 tevent_req_set_callback(subreq, resolve_wins_done, req);
2180 state->num_sent += 1;
2183 if (state->num_sent == 0) {
2184 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2188 wins_srv_tags_free(wins_tags);
2191 wins_srv_tags_free(wins_tags);
2192 return tevent_req_post(req, ev);
2195 static void resolve_wins_done(struct tevent_req *subreq)
2197 struct tevent_req *req = tevent_req_callback_data(
2198 subreq, struct tevent_req);
2199 struct resolve_wins_state *state = tevent_req_data(
2200 req, struct resolve_wins_state);
2203 status = query_wins_list_recv(subreq, state, &state->addrs,
2204 &state->num_addrs, &state->flags);
2205 if (NT_STATUS_IS_OK(status)) {
2206 tevent_req_done(req);
2210 state->num_received += 1;
2212 if (state->num_received < state->num_sent) {
2214 * Wait for the others
2218 tevent_req_nterror(req, status);
2221 NTSTATUS resolve_wins_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2222 struct sockaddr_storage **addrs,
2223 int *num_addrs, uint8_t *flags)
2225 struct resolve_wins_state *state = tevent_req_data(
2226 req, struct resolve_wins_state);
2229 if (tevent_req_is_nterror(req, &status)) {
2232 if (addrs != NULL) {
2233 *addrs = talloc_move(mem_ctx, &state->addrs);
2235 if (num_addrs != NULL) {
2236 *num_addrs = state->num_addrs;
2238 if (flags != NULL) {
2239 *flags = state->flags;
2241 return NT_STATUS_OK;
2244 /********************************************************
2245 Resolve via "wins" method.
2246 *********************************************************/
2248 NTSTATUS resolve_wins(const char *name,
2250 TALLOC_CTX *mem_ctx,
2251 struct sockaddr_storage **return_iplist,
2254 struct tevent_context *ev;
2255 struct tevent_req *req;
2256 NTSTATUS status = NT_STATUS_NO_MEMORY;
2258 ev = samba_tevent_context_init(talloc_tos());
2262 req = resolve_wins_send(ev, ev, name, name_type);
2266 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2269 status = resolve_wins_recv(req, mem_ctx, return_iplist, return_count,
2276 /********************************************************
2277 Resolve via "lmhosts" method.
2278 *********************************************************/
2280 static NTSTATUS resolve_lmhosts(const char *name, int name_type,
2281 struct ip_service **return_iplist,
2285 * "lmhosts" means parse the local lmhosts file.
2287 struct sockaddr_storage *ss_list;
2288 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
2289 TALLOC_CTX *ctx = NULL;
2291 *return_iplist = NULL;
2294 DEBUG(3,("resolve_lmhosts: "
2295 "Attempting lmhosts lookup for name %s<0x%x>\n",
2298 ctx = talloc_init("resolve_lmhosts");
2300 return NT_STATUS_NO_MEMORY;
2303 status = resolve_lmhosts_file_as_sockaddr(get_dyn_LMHOSTSFILE(),
2308 if (NT_STATUS_IS_OK(status)) {
2309 if (convert_ss2service(return_iplist,
2313 return NT_STATUS_OK;
2316 return NT_STATUS_NO_MEMORY;
2324 /********************************************************
2325 Resolve via "hosts" method.
2326 *********************************************************/
2328 static NTSTATUS resolve_hosts(const char *name, int name_type,
2329 struct ip_service **return_iplist,
2333 * "host" means do a localhost, or dns lookup.
2335 struct addrinfo hints;
2336 struct addrinfo *ailist = NULL;
2337 struct addrinfo *res = NULL;
2341 if ( name_type != 0x20 && name_type != 0x0) {
2342 DEBUG(5, ("resolve_hosts: not appropriate "
2343 "for name type <0x%x>\n",
2345 return NT_STATUS_INVALID_PARAMETER;
2348 *return_iplist = NULL;
2351 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
2355 /* By default make sure it supports TCP. */
2356 hints.ai_socktype = SOCK_STREAM;
2357 hints.ai_flags = AI_ADDRCONFIG;
2359 #if !defined(HAVE_IPV6)
2360 /* Unless we have IPv6, we really only want IPv4 addresses back. */
2361 hints.ai_family = AF_INET;
2364 ret = getaddrinfo(name,
2369 DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
2371 gai_strerror(ret) ));
2374 for (res = ailist; res; res = res->ai_next) {
2375 struct sockaddr_storage ss;
2377 if (!res->ai_addr || res->ai_addrlen == 0) {
2382 memcpy(&ss, res->ai_addr, res->ai_addrlen);
2384 if (is_zero_addr(&ss)) {
2390 *return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
2393 if (!*return_iplist) {
2394 DEBUG(3,("resolve_hosts: malloc fail !\n"));
2395 freeaddrinfo(ailist);
2396 return NT_STATUS_NO_MEMORY;
2398 (*return_iplist)[i].ss = ss;
2399 (*return_iplist)[i].port = PORT_NONE;
2403 freeaddrinfo(ailist);
2405 if (*return_count) {
2406 return NT_STATUS_OK;
2408 return NT_STATUS_UNSUCCESSFUL;
2411 /********************************************************
2412 Resolve via "ADS" method.
2413 *********************************************************/
2415 /* Special name type used to cause a _kerberos DNS lookup. */
2416 #define KDC_NAME_TYPE 0xDCDC
2418 static NTSTATUS resolve_ads(const char *name,
2420 const char *sitename,
2421 struct ip_service **return_iplist,
2427 struct dns_rr_srv *dcs = NULL;
2431 if ((name_type != 0x1c) && (name_type != KDC_NAME_TYPE) &&
2432 (name_type != 0x1b)) {
2433 return NT_STATUS_INVALID_PARAMETER;
2436 if ( (ctx = talloc_init("resolve_ads")) == NULL ) {
2437 DEBUG(0,("resolve_ads: talloc_init() failed!\n"));
2438 return NT_STATUS_NO_MEMORY;
2441 /* The DNS code needs fixing to find IPv6 addresses... JRA. */
2442 switch (name_type) {
2444 DEBUG(5,("resolve_ads: Attempting to resolve "
2445 "PDC for %s using DNS\n", name));
2446 status = ads_dns_query_pdc(ctx,
2453 DEBUG(5,("resolve_ads: Attempting to resolve "
2454 "DCs for %s using DNS\n", name));
2455 status = ads_dns_query_dcs(ctx,
2462 DEBUG(5,("resolve_ads: Attempting to resolve "
2463 "KDCs for %s using DNS\n", name));
2464 status = ads_dns_query_kdcs(ctx,
2471 status = NT_STATUS_INVALID_PARAMETER;
2475 if ( !NT_STATUS_IS_OK( status ) ) {
2476 talloc_destroy(ctx);
2480 for (i=0;i<numdcs;i++) {
2484 numaddrs += dcs[i].num_ips;
2488 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, numaddrs)) ==
2490 DEBUG(0,("resolve_ads: malloc failed for %d entries\n",
2492 talloc_destroy(ctx);
2493 return NT_STATUS_NO_MEMORY;
2496 /* now unroll the list of IP addresses */
2500 for (i = 0; i < numdcs && (*return_count<numaddrs); i++ ) {
2501 /* If we don't have an IP list for a name, lookup it up */
2503 /* We need to get all IP addresses here. */
2504 struct addrinfo *res = NULL;
2506 int extra_addrs = 0;
2508 if (!interpret_string_addr_internal(&res,
2513 /* Add in every IP from the lookup. How
2515 for (p = res; p; p = p->ai_next) {
2516 struct sockaddr_storage ss;
2517 memcpy(&ss, p->ai_addr, p->ai_addrlen);
2518 if (is_zero_addr(&ss)) {
2523 if (extra_addrs > 1) {
2524 /* We need to expand the return_iplist array
2525 as we only budgeted for one address. */
2526 numaddrs += (extra_addrs-1);
2527 *return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
2530 if (*return_iplist == NULL) {
2534 talloc_destroy(ctx);
2535 return NT_STATUS_NO_MEMORY;
2538 for (p = res; p; p = p->ai_next) {
2539 (*return_iplist)[*return_count].port = dcs[i].port;
2540 memcpy(&(*return_iplist)[*return_count].ss,
2543 if (is_zero_addr(&(*return_iplist)[*return_count].ss)) {
2547 /* Should never happen, but still... */
2548 if (*return_count>=numaddrs) {
2556 /* use all the IP addresses from the SRV sresponse */
2558 for (j = 0; j < dcs[i].num_ips; j++) {
2559 (*return_iplist)[*return_count].port = dcs[i].port;
2560 (*return_iplist)[*return_count].ss = dcs[i].ss_s[j];
2561 if (is_zero_addr(&(*return_iplist)[*return_count].ss)) {
2565 /* Should never happen, but still... */
2566 if (*return_count>=numaddrs) {
2573 talloc_destroy(ctx);
2574 return NT_STATUS_OK;
2577 static const char **filter_out_nbt_lookup(TALLOC_CTX *mem_ctx,
2578 const char **resolve_order)
2580 size_t i, len, result_idx;
2581 const char **result;
2584 while (resolve_order[len] != NULL) {
2588 result = talloc_array(mem_ctx, const char *, len+1);
2589 if (result == NULL) {
2595 for (i=0; i<len; i++) {
2596 const char *tok = resolve_order[i];
2598 if (strequal(tok, "lmhosts") || strequal(tok, "wins") ||
2599 strequal(tok, "bcast")) {
2602 result[result_idx++] = tok;
2604 result[result_idx] = NULL;
2609 /*******************************************************************
2610 Internal interface to resolve a name into an IP address.
2611 Use this function if the string is either an IP address, DNS
2612 or host name or NetBIOS name. This uses the name switch in the
2613 smb.conf to determine the order of name resolution.
2615 Added support for ip addr/port to support ADS ldap servers.
2616 the only place we currently care about the port is in the
2617 resolve_hosts() when looking up DC's via SRV RR entries in DNS
2618 **********************************************************************/
2620 NTSTATUS internal_resolve_name(const char *name,
2622 const char *sitename,
2623 struct ip_service **return_iplist,
2625 const char **resolve_order)
2628 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2630 TALLOC_CTX *frame = NULL;
2632 *return_iplist = NULL;
2635 DEBUG(10, ("internal_resolve_name: looking up %s#%x (sitename %s)\n",
2636 name, name_type, sitename ? sitename : "(null)"));
2638 if (is_ipaddress(name)) {
2639 if ((*return_iplist = SMB_MALLOC_P(struct ip_service)) ==
2641 DEBUG(0,("internal_resolve_name: malloc fail !\n"));
2642 return NT_STATUS_NO_MEMORY;
2645 /* ignore the port here */
2646 (*return_iplist)->port = PORT_NONE;
2648 /* if it's in the form of an IP address then get the lib to interpret it */
2649 if (!interpret_string_addr(&(*return_iplist)->ss,
2650 name, AI_NUMERICHOST)) {
2651 DEBUG(1,("internal_resolve_name: interpret_string_addr "
2654 SAFE_FREE(*return_iplist);
2655 return NT_STATUS_INVALID_PARAMETER;
2657 if (is_zero_addr(&(*return_iplist)->ss)) {
2658 SAFE_FREE(*return_iplist);
2659 return NT_STATUS_UNSUCCESSFUL;
2662 return NT_STATUS_OK;
2665 /* Check name cache */
2667 if (namecache_fetch(name, name_type, return_iplist, return_count)) {
2668 *return_count = remove_duplicate_addrs2(*return_iplist,
2670 /* This could be a negative response */
2671 if (*return_count > 0) {
2672 return NT_STATUS_OK;
2674 return NT_STATUS_UNSUCCESSFUL;
2678 /* set the name resolution order */
2680 if (resolve_order && strcmp(resolve_order[0], "NULL") == 0) {
2681 DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
2682 return NT_STATUS_INVALID_PARAMETER;
2685 if (!resolve_order || !resolve_order[0]) {
2686 static const char *host_order[] = { "host", NULL };
2687 resolve_order = host_order;
2690 frame = talloc_stackframe();
2692 if ((strlen(name) > MAX_NETBIOSNAME_LEN - 1) ||
2693 (strchr(name, '.') != NULL)) {
2695 * Don't do NBT lookup, the name would not fit anyway
2697 resolve_order = filter_out_nbt_lookup(frame, resolve_order);
2698 if (resolve_order == NULL) {
2700 return NT_STATUS_NO_MEMORY;
2704 /* iterate through the name resolution backends */
2706 for (i=0; resolve_order[i]; i++) {
2707 tok = resolve_order[i];
2709 if((strequal(tok, "host") || strequal(tok, "hosts"))) {
2710 status = resolve_hosts(name, name_type, return_iplist,
2712 if (NT_STATUS_IS_OK(status)) {
2715 } else if(strequal( tok, "kdc")) {
2716 /* deal with KDC_NAME_TYPE names here.
2717 * This will result in a SRV record lookup */
2718 status = resolve_ads(name, KDC_NAME_TYPE, sitename,
2719 return_iplist, return_count);
2720 if (NT_STATUS_IS_OK(status)) {
2721 /* Ensure we don't namecache
2722 * this with the KDC port. */
2723 name_type = KDC_NAME_TYPE;
2726 } else if(strequal( tok, "ads")) {
2727 /* deal with 0x1c and 0x1b names here.
2728 * This will result in a SRV record lookup */
2729 status = resolve_ads(name, name_type, sitename,
2730 return_iplist, return_count);
2731 if (NT_STATUS_IS_OK(status)) {
2734 } else if (strequal(tok, "lmhosts")) {
2735 status = resolve_lmhosts(name, name_type,
2736 return_iplist, return_count);
2737 if (NT_STATUS_IS_OK(status)) {
2740 } else if (strequal(tok, "wins")) {
2741 /* don't resolve 1D via WINS */
2742 struct sockaddr_storage *ss_list;
2743 if (name_type != 0x1D) {
2744 status = resolve_wins(name, name_type,
2748 if (NT_STATUS_IS_OK(status)) {
2749 if (!convert_ss2service(return_iplist,
2752 status = NT_STATUS_NO_MEMORY;
2757 } else if (strequal(tok, "bcast")) {
2758 struct sockaddr_storage *ss_list;
2759 status = name_resolve_bcast(
2760 name, name_type, talloc_tos(),
2761 &ss_list, return_count);
2762 if (NT_STATUS_IS_OK(status)) {
2763 if (!convert_ss2service(return_iplist,
2766 status = NT_STATUS_NO_MEMORY;
2771 DEBUG(0,("resolve_name: unknown name switch type %s\n",
2776 /* All of the resolve_* functions above have returned false. */
2779 SAFE_FREE(*return_iplist);
2782 return NT_STATUS_UNSUCCESSFUL;
2786 /* Remove duplicate entries. Some queries, notably #1c (domain
2787 controllers) return the PDC in iplist[0] and then all domain
2788 controllers including the PDC in iplist[1..n]. Iterating over
2789 the iplist when the PDC is down will cause two sets of timeouts. */
2791 *return_count = remove_duplicate_addrs2(*return_iplist, *return_count );
2793 /* Save in name cache */
2794 if ( DEBUGLEVEL >= 100 ) {
2795 for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++) {
2796 char addr[INET6_ADDRSTRLEN];
2797 print_sockaddr(addr, sizeof(addr),
2798 &(*return_iplist)[i].ss);
2799 DEBUG(100, ("Storing name %s of type %d (%s:%d)\n",
2803 (*return_iplist)[i].port));
2807 if (*return_count) {
2808 namecache_store(name, name_type, *return_count, *return_iplist);
2811 /* Display some debugging info */
2813 if ( DEBUGLEVEL >= 10 ) {
2814 DEBUG(10, ("internal_resolve_name: returning %d addresses: ",
2817 for (i = 0; i < *return_count; i++) {
2818 char addr[INET6_ADDRSTRLEN];
2819 print_sockaddr(addr, sizeof(addr),
2820 &(*return_iplist)[i].ss);
2821 DEBUGADD(10, ("%s:%d ",
2823 (*return_iplist)[i].port));
2832 /********************************************************
2833 Internal interface to resolve a name into one IP address.
2834 Use this function if the string is either an IP address, DNS
2835 or host name or NetBIOS name. This uses the name switch in the
2836 smb.conf to determine the order of name resolution.
2837 *********************************************************/
2839 bool resolve_name(const char *name,
2840 struct sockaddr_storage *return_ss,
2844 struct ip_service *ss_list = NULL;
2845 char *sitename = NULL;
2849 if (is_ipaddress(name)) {
2850 return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
2853 sitename = sitename_fetch(talloc_tos(), lp_realm()); /* wild guess */
2855 status = internal_resolve_name(name, name_type, sitename,
2857 lp_name_resolve_order());
2858 if (NT_STATUS_IS_OK(status)) {
2862 for (i=0; i<count; i++) {
2863 if (!is_zero_addr(&ss_list[i].ss) &&
2864 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss) &&
2865 (ss_list[i].ss.ss_family == AF_INET)) {
2866 *return_ss = ss_list[i].ss;
2868 TALLOC_FREE(sitename);
2874 /* only return valid addresses for TCP connections */
2875 for (i=0; i<count; i++) {
2876 if (!is_zero_addr(&ss_list[i].ss) &&
2877 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2878 *return_ss = ss_list[i].ss;
2880 TALLOC_FREE(sitename);
2887 TALLOC_FREE(sitename);
2891 /********************************************************
2892 Internal interface to resolve a name into a list of IP addresses.
2893 Use this function if the string is either an IP address, DNS
2894 or host name or NetBIOS name. This uses the name switch in the
2895 smb.conf to determine the order of name resolution.
2896 *********************************************************/
2898 NTSTATUS resolve_name_list(TALLOC_CTX *ctx,
2901 struct sockaddr_storage **return_ss_arr,
2902 unsigned int *p_num_entries)
2904 struct ip_service *ss_list = NULL;
2905 char *sitename = NULL;
2908 unsigned int num_entries;
2912 *return_ss_arr = NULL;
2914 if (is_ipaddress(name)) {
2915 *return_ss_arr = talloc(ctx, struct sockaddr_storage);
2916 if (!*return_ss_arr) {
2917 return NT_STATUS_NO_MEMORY;
2919 if (!interpret_string_addr(*return_ss_arr, name, AI_NUMERICHOST)) {
2920 TALLOC_FREE(*return_ss_arr);
2921 return NT_STATUS_BAD_NETWORK_NAME;
2924 return NT_STATUS_OK;
2927 sitename = sitename_fetch(ctx, lp_realm()); /* wild guess */
2929 status = internal_resolve_name(name, name_type, sitename,
2931 lp_name_resolve_order());
2932 TALLOC_FREE(sitename);
2934 if (!NT_STATUS_IS_OK(status)) {
2938 /* only return valid addresses for TCP connections */
2939 for (i=0, num_entries = 0; i<count; i++) {
2940 if (!is_zero_addr(&ss_list[i].ss) &&
2941 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2945 if (num_entries == 0) {
2947 return NT_STATUS_BAD_NETWORK_NAME;
2950 *return_ss_arr = talloc_array(ctx,
2951 struct sockaddr_storage,
2953 if (!(*return_ss_arr)) {
2955 return NT_STATUS_NO_MEMORY;
2958 for (i=0, num_entries = 0; i<count; i++) {
2959 if (!is_zero_addr(&ss_list[i].ss) &&
2960 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2961 (*return_ss_arr)[num_entries++] = ss_list[i].ss;
2965 status = NT_STATUS_OK;
2966 *p_num_entries = num_entries;
2969 return NT_STATUS_OK;
2972 /********************************************************
2973 Find the IP address of the master browser or DMB for a workgroup.
2974 *********************************************************/
2976 bool find_master_ip(const char *group, struct sockaddr_storage *master_ss)
2978 struct ip_service *ip_list = NULL;
2982 if (lp_disable_netbios()) {
2983 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
2987 status = internal_resolve_name(group, 0x1D, NULL, &ip_list, &count,
2988 lp_name_resolve_order());
2989 if (NT_STATUS_IS_OK(status)) {
2990 *master_ss = ip_list[0].ss;
2995 status = internal_resolve_name(group, 0x1B, NULL, &ip_list, &count,
2996 lp_name_resolve_order());
2997 if (NT_STATUS_IS_OK(status)) {
2998 *master_ss = ip_list[0].ss;
3007 /********************************************************
3008 Get the IP address list of the primary domain controller
3010 *********************************************************/
3012 bool get_pdc_ip(const char *domain, struct sockaddr_storage *pss)
3014 struct ip_service *ip_list = NULL;
3016 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
3017 static const char *ads_order[] = { "ads", NULL };
3018 /* Look up #1B name */
3020 if (lp_security() == SEC_ADS) {
3021 status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
3025 if (!NT_STATUS_IS_OK(status) || count == 0) {
3026 status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
3028 lp_name_resolve_order());
3029 if (!NT_STATUS_IS_OK(status)) {
3035 /* if we get more than 1 IP back we have to assume it is a
3036 multi-homed PDC and not a mess up */
3039 DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count));
3040 sort_service_list(ip_list, count);
3043 *pss = ip_list[0].ss;
3048 /* Private enum type for lookups. */
3050 enum dc_lookup_type { DC_NORMAL_LOOKUP, DC_ADS_ONLY, DC_KDC_ONLY };
3052 /********************************************************
3053 Get the IP address list of the domain controllers for
3055 *********************************************************/
3057 static NTSTATUS get_dc_list(const char *domain,
3058 const char *sitename,
3059 struct ip_service **ip_list,
3061 enum dc_lookup_type lookup_type,
3064 const char **resolve_order = NULL;
3065 char *saf_servername = NULL;
3066 char *pserver = NULL;
3068 char *port_str = NULL;
3071 int num_addresses = 0;
3072 int local_count, i, j;
3073 struct ip_service *return_iplist = NULL;
3074 struct ip_service *auto_ip_list = NULL;
3075 bool done_auto_lookup = false;
3078 TALLOC_CTX *ctx = talloc_init("get_dc_list");
3084 return NT_STATUS_NO_MEMORY;
3089 /* if we are restricted to solely using DNS for looking
3090 up a domain controller, make sure that host lookups
3091 are enabled for the 'name resolve order'. If host lookups
3092 are disabled and ads_only is True, then set the string to
3095 resolve_order = lp_name_resolve_order();
3096 if (!resolve_order) {
3097 status = NT_STATUS_NO_MEMORY;
3100 if (lookup_type == DC_ADS_ONLY) {
3101 if (str_list_check_ci(resolve_order, "host")) {
3102 static const char *ads_order[] = { "ads", NULL };
3103 resolve_order = ads_order;
3105 /* DNS SRV lookups used by the ads resolver
3106 are already sorted by priority and weight */
3109 /* this is quite bizarre! */
3110 static const char *null_order[] = { "NULL", NULL };
3111 resolve_order = null_order;
3113 } else if (lookup_type == DC_KDC_ONLY) {
3114 static const char *kdc_order[] = { "kdc", NULL };
3115 /* DNS SRV lookups used by the ads/kdc resolver
3116 are already sorted by priority and weight */
3118 resolve_order = kdc_order;
3121 /* fetch the server we have affinity for. Add the
3122 'password server' list to a search for our domain controllers */
3124 saf_servername = saf_fetch(ctx, domain);
3126 if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
3127 pserver = talloc_asprintf(ctx, "%s, %s",
3128 saf_servername ? saf_servername : "",
3129 lp_password_server());
3131 pserver = talloc_asprintf(ctx, "%s, *",
3132 saf_servername ? saf_servername : "");
3135 TALLOC_FREE(saf_servername);
3137 status = NT_STATUS_NO_MEMORY;
3141 /* if we are starting from scratch, just lookup DOMAIN<0x1c> */
3144 DEBUG(10,("get_dc_list: no preferred domain controllers.\n"));
3145 status = internal_resolve_name(domain, 0x1C, sitename, ip_list,
3146 count, resolve_order);
3150 DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver ));
3153 * if '*' appears in the "password server" list then add
3154 * an auto lookup to the list of manually configured
3155 * DC's. If any DC is listed by name, then the list should be
3156 * considered to be ordered
3160 while (next_token_talloc(ctx, &p, &name, LIST_SEP)) {
3161 if (!done_auto_lookup && strequal(name, "*")) {
3162 status = internal_resolve_name(domain, 0x1C, sitename,
3166 if (NT_STATUS_IS_OK(status)) {
3167 num_addresses += auto_count;
3169 done_auto_lookup = true;
3170 DEBUG(8,("Adding %d DC's from auto lookup\n",
3177 /* if we have no addresses and haven't done the auto lookup, then
3178 just return the list of DC's. Or maybe we just failed. */
3180 if (num_addresses == 0) {
3181 if (done_auto_lookup) {
3182 DEBUG(4,("get_dc_list: no servers found\n"));
3183 status = NT_STATUS_NO_LOGON_SERVERS;
3186 status = internal_resolve_name(domain, 0x1C, sitename, ip_list,
3187 count, resolve_order);
3191 if ((return_iplist = SMB_MALLOC_ARRAY(struct ip_service,
3192 num_addresses)) == NULL) {
3193 DEBUG(3,("get_dc_list: malloc fail !\n"));
3194 status = NT_STATUS_NO_MEMORY;
3201 /* fill in the return list now with real IP's */
3203 while ((local_count<num_addresses) &&
3204 next_token_talloc(ctx, &p, &name, LIST_SEP)) {
3205 struct sockaddr_storage name_ss;
3207 /* copy any addersses from the auto lookup */
3209 if (strequal(name, "*")) {
3210 for (j=0; j<auto_count; j++) {
3211 char addr[INET6_ADDRSTRLEN];
3212 print_sockaddr(addr,
3214 &auto_ip_list[j].ss);
3215 /* Check for and don't copy any
3216 * known bad DC IP's. */
3217 if(!NT_STATUS_IS_OK(check_negative_conn_cache(
3220 DEBUG(5,("get_dc_list: "
3221 "negative entry %s removed "
3226 return_iplist[local_count].ss =
3228 return_iplist[local_count].port =
3229 auto_ip_list[j].port;
3235 /* added support for address:port syntax for ads
3236 * (not that I think anyone will ever run the LDAP
3237 * server in an AD domain on something other than
3240 port = (lp_security() == SEC_ADS) ? LDAP_PORT : PORT_NONE;
3241 if ((port_str=strchr(name, ':')) != NULL) {
3244 port = atoi(port_str);
3247 /* explicit lookup; resolve_name() will
3248 * handle names & IP addresses */
3249 if (resolve_name( name, &name_ss, 0x20, true )) {
3250 char addr[INET6_ADDRSTRLEN];
3251 print_sockaddr(addr,
3255 /* Check for and don't copy any known bad DC IP's. */
3256 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain,
3258 DEBUG(5,("get_dc_list: negative entry %s "
3259 "removed from DC list\n",
3264 return_iplist[local_count].ss = name_ss;
3265 return_iplist[local_count].port = port;
3271 /* need to remove duplicates in the list if we have any
3272 explicit password servers */
3274 local_count = remove_duplicate_addrs2(return_iplist, local_count );
3276 /* For DC's we always prioritize IPv4 due to W2K3 not
3277 * supporting LDAP, KRB5 or CLDAP over IPv6. */
3279 if (local_count && return_iplist) {
3280 prioritize_ipv4_list(return_iplist, local_count);
3283 if ( DEBUGLEVEL >= 4 ) {
3284 DEBUG(4,("get_dc_list: returning %d ip addresses "
3285 "in an %sordered list\n",
3287 *ordered ? "":"un"));
3288 DEBUG(4,("get_dc_list: "));
3289 for ( i=0; i<local_count; i++ ) {
3290 char addr[INET6_ADDRSTRLEN];
3291 print_sockaddr(addr,
3293 &return_iplist[i].ss);
3294 DEBUGADD(4,("%s:%d ", addr, return_iplist[i].port ));
3299 *ip_list = return_iplist;
3300 *count = local_count;
3302 status = ( *count != 0 ? NT_STATUS_OK : NT_STATUS_NO_LOGON_SERVERS );
3306 if (!NT_STATUS_IS_OK(status)) {
3307 SAFE_FREE(return_iplist);
3312 SAFE_FREE(auto_ip_list);
3317 /*********************************************************************
3318 Small wrapper function to get the DC list and sort it if neccessary.
3319 *********************************************************************/
3321 NTSTATUS get_sorted_dc_list( const char *domain,
3322 const char *sitename,
3323 struct ip_service **ip_list,
3327 bool ordered = false;
3329 enum dc_lookup_type lookup_type = DC_NORMAL_LOOKUP;
3334 DEBUG(8,("get_sorted_dc_list: attempting lookup "
3335 "for name %s (sitename %s)\n",
3337 sitename ? sitename : "NULL"));
3340 lookup_type = DC_ADS_ONLY;
3343 status = get_dc_list(domain, sitename, ip_list,
3344 count, lookup_type, &ordered);
3345 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_LOGON_SERVERS)
3347 DEBUG(3,("get_sorted_dc_list: no server for name %s available"
3348 " in site %s, fallback to all servers\n",
3350 status = get_dc_list(domain, NULL, ip_list,
3351 count, lookup_type, &ordered);
3354 if (!NT_STATUS_IS_OK(status)) {
3355 SAFE_FREE(*ip_list);
3360 /* only sort if we don't already have an ordered list */
3362 sort_service_list(*ip_list, *count);
3365 return NT_STATUS_OK;
3368 /*********************************************************************
3369 Get the KDC list - re-use all the logic in get_dc_list.
3370 *********************************************************************/
3372 NTSTATUS get_kdc_list( const char *realm,
3373 const char *sitename,
3374 struct ip_service **ip_list,
3383 status = get_dc_list(realm, sitename, ip_list,
3384 count, DC_KDC_ONLY, &ordered);
3386 if (!NT_STATUS_IS_OK(status)) {
3387 SAFE_FREE(*ip_list);
3392 /* only sort if we don't already have an ordered list */
3394 sort_service_list(*ip_list, *count);
3397 return NT_STATUS_OK;