vfs_io_uring: move error handling out of vfs_io_uring_pread_recv()
[Samba.git] / source3 / libsmb / namequery.c
blobe27b60b0398e90d2b5db28c31dc7b0594fbbaea3
1 /*
2 Unix SMB/CIFS implementation.
3 name query routines
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/>.
21 #include "includes.h"
22 #include "libsmb/namequery.h"
23 #include "../lib/util/tevent_ntstatus.h"
24 #include "libads/sitename_cache.h"
25 #include "../lib/addns/dnsquery.h"
26 #include "../libcli/netlogon/netlogon.h"
27 #include "lib/async_req/async_sock.h"
28 #include "lib/tsocket/tsocket.h"
29 #include "libsmb/nmblib.h"
30 #include "libsmb/unexpected.h"
31 #include "../libcli/nbt/libnbt.h"
32 #include "libads/kerberos_proto.h"
33 #include "lib/gencache.h"
35 /* nmbd.c sets this to True. */
36 bool global_in_nmbd = False;
38 /****************************
39 * SERVER AFFINITY ROUTINES *
40 ****************************/
42 /* Server affinity is the concept of preferring the last domain
43 controller with whom you had a successful conversation */
45 /****************************************************************************
46 ****************************************************************************/
47 #define SAFKEY_FMT "SAF/DOMAIN/%s"
48 #define SAF_TTL 900
49 #define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
50 #define SAFJOIN_TTL 3600
52 static char *saf_key(TALLOC_CTX *mem_ctx, const char *domain)
54 return talloc_asprintf_strupper_m(mem_ctx, SAFKEY_FMT, domain);
57 static char *saf_join_key(TALLOC_CTX *mem_ctx, const char *domain)
59 return talloc_asprintf_strupper_m(mem_ctx, SAFJOINKEY_FMT, domain);
62 /****************************************************************************
63 ****************************************************************************/
65 bool saf_store( const char *domain, const char *servername )
67 char *key;
68 time_t expire;
69 bool ret = False;
71 if ( !domain || !servername ) {
72 DEBUG(2,("saf_store: "
73 "Refusing to store empty domain or servername!\n"));
74 return False;
77 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
78 DEBUG(0,("saf_store: "
79 "refusing to store 0 length domain or servername!\n"));
80 return False;
83 key = saf_key(talloc_tos(), domain);
84 if (key == NULL) {
85 DEBUG(1, ("saf_key() failed\n"));
86 return false;
88 expire = time( NULL ) + lp_parm_int(-1, "saf","ttl", SAF_TTL);
90 DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
91 domain, servername, (unsigned int)expire ));
93 ret = gencache_set( key, servername, expire );
95 TALLOC_FREE( key );
97 return ret;
100 bool saf_join_store( const char *domain, const char *servername )
102 char *key;
103 time_t expire;
104 bool ret = False;
106 if ( !domain || !servername ) {
107 DEBUG(2,("saf_join_store: Refusing to store empty domain or servername!\n"));
108 return False;
111 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
112 DEBUG(0,("saf_join_store: refusing to store 0 length domain or servername!\n"));
113 return False;
116 key = saf_join_key(talloc_tos(), domain);
117 if (key == NULL) {
118 DEBUG(1, ("saf_join_key() failed\n"));
119 return false;
121 expire = time( NULL ) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL);
123 DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
124 domain, servername, (unsigned int)expire ));
126 ret = gencache_set( key, servername, expire );
128 TALLOC_FREE( key );
130 return ret;
133 bool saf_delete( const char *domain )
135 char *key;
136 bool ret = False;
138 if ( !domain ) {
139 DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
140 return False;
143 key = saf_join_key(talloc_tos(), domain);
144 if (key == NULL) {
145 DEBUG(1, ("saf_join_key() failed\n"));
146 return false;
148 ret = gencache_del(key);
149 TALLOC_FREE(key);
151 if (ret) {
152 DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain ));
155 key = saf_key(talloc_tos(), domain);
156 if (key == NULL) {
157 DEBUG(1, ("saf_key() failed\n"));
158 return false;
160 ret = gencache_del(key);
161 TALLOC_FREE(key);
163 if (ret) {
164 DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
167 return ret;
170 /****************************************************************************
171 ****************************************************************************/
173 char *saf_fetch(TALLOC_CTX *mem_ctx, const char *domain )
175 char *server = NULL;
176 time_t timeout;
177 bool ret = False;
178 char *key = NULL;
180 if ( !domain || strlen(domain) == 0) {
181 DEBUG(2,("saf_fetch: Empty domain name!\n"));
182 return NULL;
185 key = saf_join_key(talloc_tos(), domain);
186 if (key == NULL) {
187 DEBUG(1, ("saf_join_key() failed\n"));
188 return NULL;
191 ret = gencache_get( key, mem_ctx, &server, &timeout );
193 TALLOC_FREE( key );
195 if ( ret ) {
196 DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
197 server, domain ));
198 return server;
201 key = saf_key(talloc_tos(), domain);
202 if (key == NULL) {
203 DEBUG(1, ("saf_key() failed\n"));
204 return NULL;
207 ret = gencache_get( key, mem_ctx, &server, &timeout );
209 TALLOC_FREE( key );
211 if ( !ret ) {
212 DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
213 domain ));
214 } else {
215 DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
216 server, domain ));
219 return server;
222 static void set_socket_addr_v4(struct sockaddr_storage *addr)
224 if (!interpret_string_addr(addr, lp_nbt_client_socket_address(),
225 AI_NUMERICHOST|AI_PASSIVE)) {
226 zero_sockaddr(addr);
228 if (addr->ss_family != AF_INET) {
229 zero_sockaddr(addr);
233 static struct in_addr my_socket_addr_v4(void)
235 struct sockaddr_storage my_addr;
236 struct sockaddr_in *in_addr = (struct sockaddr_in *)((char *)&my_addr);
238 set_socket_addr_v4(&my_addr);
239 return in_addr->sin_addr;
242 /****************************************************************************
243 Generate a random trn_id.
244 ****************************************************************************/
246 static int generate_trn_id(void)
248 uint16_t id;
250 generate_random_buffer((uint8_t *)&id, sizeof(id));
252 return id % (unsigned)0x7FFF;
255 /****************************************************************************
256 Parse a node status response into an array of structures.
257 ****************************************************************************/
259 static struct node_status *parse_node_status(TALLOC_CTX *mem_ctx, char *p,
260 int *num_names,
261 struct node_status_extra *extra)
263 struct node_status *ret;
264 int i;
266 *num_names = CVAL(p,0);
268 if (*num_names == 0)
269 return NULL;
271 ret = talloc_array(mem_ctx, struct node_status,*num_names);
272 if (!ret)
273 return NULL;
275 p++;
276 for (i=0;i< *num_names;i++) {
277 strlcpy(ret[i].name,p,16);
278 trim_char(ret[i].name,'\0',' ');
279 ret[i].type = CVAL(p,15);
280 ret[i].flags = p[16];
281 p += 18;
282 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
283 ret[i].type, ret[i].flags));
286 * Also, pick up the MAC address ...
288 if (extra) {
289 memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
291 return ret;
294 struct sock_packet_read_state {
295 struct tevent_context *ev;
296 enum packet_type type;
297 int trn_id;
299 struct nb_packet_reader *reader;
300 struct tevent_req *reader_req;
302 struct tdgram_context *sock;
303 struct tevent_req *socket_req;
304 uint8_t *buf;
305 struct tsocket_address *addr;
307 bool (*validator)(struct packet_struct *p,
308 void *private_data);
309 void *private_data;
311 struct packet_struct *packet;
314 static void sock_packet_read_got_packet(struct tevent_req *subreq);
315 static void sock_packet_read_got_socket(struct tevent_req *subreq);
317 static struct tevent_req *sock_packet_read_send(
318 TALLOC_CTX *mem_ctx,
319 struct tevent_context *ev,
320 struct tdgram_context *sock,
321 struct nb_packet_reader *reader,
322 enum packet_type type,
323 int trn_id,
324 bool (*validator)(struct packet_struct *p, void *private_data),
325 void *private_data)
327 struct tevent_req *req;
328 struct sock_packet_read_state *state;
330 req = tevent_req_create(mem_ctx, &state,
331 struct sock_packet_read_state);
332 if (req == NULL) {
333 return NULL;
335 state->ev = ev;
336 state->reader = reader;
337 state->sock = sock;
338 state->type = type;
339 state->trn_id = trn_id;
340 state->validator = validator;
341 state->private_data = private_data;
343 if (reader != NULL) {
344 state->reader_req = nb_packet_read_send(state, ev, reader);
345 if (tevent_req_nomem(state->reader_req, req)) {
346 return tevent_req_post(req, ev);
348 tevent_req_set_callback(
349 state->reader_req, sock_packet_read_got_packet, req);
352 state->socket_req = tdgram_recvfrom_send(state, ev, state->sock);
353 if (tevent_req_nomem(state->socket_req, req)) {
354 return tevent_req_post(req, ev);
356 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
357 req);
359 return req;
362 static void sock_packet_read_got_packet(struct tevent_req *subreq)
364 struct tevent_req *req = tevent_req_callback_data(
365 subreq, struct tevent_req);
366 struct sock_packet_read_state *state = tevent_req_data(
367 req, struct sock_packet_read_state);
368 NTSTATUS status;
370 status = nb_packet_read_recv(subreq, state, &state->packet);
372 TALLOC_FREE(state->reader_req);
374 if (!NT_STATUS_IS_OK(status)) {
375 if (state->socket_req != NULL) {
377 * Still waiting for socket
379 return;
382 * Both socket and packet reader failed
384 tevent_req_nterror(req, status);
385 return;
388 if ((state->validator != NULL) &&
389 !state->validator(state->packet, state->private_data)) {
390 DEBUG(10, ("validator failed\n"));
392 TALLOC_FREE(state->packet);
394 state->reader_req = nb_packet_read_send(state, state->ev,
395 state->reader);
396 if (tevent_req_nomem(state->reader_req, req)) {
397 return;
399 tevent_req_set_callback(
400 state->reader_req, sock_packet_read_got_packet, req);
401 return;
404 TALLOC_FREE(state->socket_req);
405 tevent_req_done(req);
408 static void sock_packet_read_got_socket(struct tevent_req *subreq)
410 struct tevent_req *req = tevent_req_callback_data(
411 subreq, struct tevent_req);
412 struct sock_packet_read_state *state = tevent_req_data(
413 req, struct sock_packet_read_state);
414 union {
415 struct sockaddr sa;
416 struct sockaddr_in sin;
417 } addr;
418 ssize_t ret;
419 ssize_t received;
420 int err;
421 bool ok;
423 received = tdgram_recvfrom_recv(subreq, &err, state,
424 &state->buf, &state->addr);
426 TALLOC_FREE(state->socket_req);
428 if (received == -1) {
429 if (state->reader_req != NULL) {
431 * Still waiting for reader
433 return;
436 * Both socket and reader failed
438 tevent_req_nterror(req, map_nt_error_from_unix(err));
439 return;
441 ok = tsocket_address_is_inet(state->addr, "ipv4");
442 if (!ok) {
443 goto retry;
445 ret = tsocket_address_bsd_sockaddr(state->addr,
446 &addr.sa,
447 sizeof(addr.sin));
448 if (ret == -1) {
449 tevent_req_nterror(req, map_nt_error_from_unix(errno));
450 return;
453 state->packet = parse_packet_talloc(
454 state, (char *)state->buf, received, state->type,
455 addr.sin.sin_addr, addr.sin.sin_port);
456 if (state->packet == NULL) {
457 DEBUG(10, ("parse_packet failed\n"));
458 goto retry;
460 if ((state->trn_id != -1) &&
461 (state->trn_id != packet_trn_id(state->packet))) {
462 DEBUG(10, ("Expected transaction id %d, got %d\n",
463 state->trn_id, packet_trn_id(state->packet)));
464 goto retry;
467 if ((state->validator != NULL) &&
468 !state->validator(state->packet, state->private_data)) {
469 DEBUG(10, ("validator failed\n"));
470 goto retry;
473 tevent_req_done(req);
474 return;
476 retry:
477 TALLOC_FREE(state->packet);
478 TALLOC_FREE(state->buf);
479 TALLOC_FREE(state->addr);
481 state->socket_req = tdgram_recvfrom_send(state, state->ev, state->sock);
482 if (tevent_req_nomem(state->socket_req, req)) {
483 return;
485 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
486 req);
489 static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
490 TALLOC_CTX *mem_ctx,
491 struct packet_struct **ppacket)
493 struct sock_packet_read_state *state = tevent_req_data(
494 req, struct sock_packet_read_state);
495 NTSTATUS status;
497 if (tevent_req_is_nterror(req, &status)) {
498 return status;
500 *ppacket = talloc_move(mem_ctx, &state->packet);
501 return NT_STATUS_OK;
504 struct nb_trans_state {
505 struct tevent_context *ev;
506 struct tdgram_context *sock;
507 struct nb_packet_reader *reader;
509 struct tsocket_address *src_addr;
510 struct tsocket_address *dst_addr;
511 uint8_t *buf;
512 size_t buflen;
513 enum packet_type type;
514 int trn_id;
516 bool (*validator)(struct packet_struct *p,
517 void *private_data);
518 void *private_data;
520 struct packet_struct *packet;
523 static void nb_trans_got_reader(struct tevent_req *subreq);
524 static void nb_trans_done(struct tevent_req *subreq);
525 static void nb_trans_sent(struct tevent_req *subreq);
526 static void nb_trans_send_next(struct tevent_req *subreq);
528 static struct tevent_req *nb_trans_send(
529 TALLOC_CTX *mem_ctx,
530 struct tevent_context *ev,
531 const struct sockaddr_storage *_my_addr,
532 const struct sockaddr_storage *_dst_addr,
533 bool bcast,
534 uint8_t *buf, size_t buflen,
535 enum packet_type type, int trn_id,
536 bool (*validator)(struct packet_struct *p,
537 void *private_data),
538 void *private_data)
540 const struct sockaddr *my_addr =
541 discard_const_p(const struct sockaddr, _my_addr);
542 size_t my_addr_len = sizeof(*_my_addr);
543 const struct sockaddr *dst_addr =
544 discard_const_p(const struct sockaddr, _dst_addr);
545 size_t dst_addr_len = sizeof(*_dst_addr);
546 struct tevent_req *req, *subreq;
547 struct nb_trans_state *state;
548 int ret;
550 req = tevent_req_create(mem_ctx, &state, struct nb_trans_state);
551 if (req == NULL) {
552 return NULL;
554 state->ev = ev;
555 state->buf = buf;
556 state->buflen = buflen;
557 state->type = type;
558 state->trn_id = trn_id;
559 state->validator = validator;
560 state->private_data = private_data;
562 ret = tsocket_address_bsd_from_sockaddr(state,
563 my_addr, my_addr_len,
564 &state->src_addr);
565 if (ret == -1) {
566 tevent_req_nterror(req, map_nt_error_from_unix(errno));
567 return tevent_req_post(req, ev);
570 ret = tsocket_address_bsd_from_sockaddr(state,
571 dst_addr, dst_addr_len,
572 &state->dst_addr);
573 if (ret == -1) {
574 tevent_req_nterror(req, map_nt_error_from_unix(errno));
575 return tevent_req_post(req, ev);
578 ret = tdgram_inet_udp_broadcast_socket(state->src_addr, state,
579 &state->sock);
580 if (ret == -1) {
581 tevent_req_nterror(req, map_nt_error_from_unix(errno));
582 return tevent_req_post(req, ev);
585 subreq = nb_packet_reader_send(state, ev, type, state->trn_id, NULL);
586 if (tevent_req_nomem(subreq, req)) {
587 return tevent_req_post(req, ev);
589 tevent_req_set_callback(subreq, nb_trans_got_reader, req);
590 return req;
593 static void nb_trans_got_reader(struct tevent_req *subreq)
595 struct tevent_req *req = tevent_req_callback_data(
596 subreq, struct tevent_req);
597 struct nb_trans_state *state = tevent_req_data(
598 req, struct nb_trans_state);
599 NTSTATUS status;
601 status = nb_packet_reader_recv(subreq, state, &state->reader);
602 TALLOC_FREE(subreq);
604 if (!NT_STATUS_IS_OK(status)) {
605 DEBUG(10, ("nmbd not around\n"));
606 state->reader = NULL;
609 subreq = sock_packet_read_send(
610 state, state->ev, state->sock,
611 state->reader, state->type, state->trn_id,
612 state->validator, state->private_data);
613 if (tevent_req_nomem(subreq, req)) {
614 return;
616 tevent_req_set_callback(subreq, nb_trans_done, req);
618 subreq = tdgram_sendto_send(state, state->ev,
619 state->sock,
620 state->buf, state->buflen,
621 state->dst_addr);
622 if (tevent_req_nomem(subreq, req)) {
623 return;
625 tevent_req_set_callback(subreq, nb_trans_sent, req);
628 static void nb_trans_sent(struct tevent_req *subreq)
630 struct tevent_req *req = tevent_req_callback_data(
631 subreq, struct tevent_req);
632 struct nb_trans_state *state = tevent_req_data(
633 req, struct nb_trans_state);
634 ssize_t sent;
635 int err;
637 sent = tdgram_sendto_recv(subreq, &err);
638 TALLOC_FREE(subreq);
639 if (sent == -1) {
640 DEBUG(10, ("sendto failed: %s\n", strerror(err)));
641 tevent_req_nterror(req, map_nt_error_from_unix(err));
642 return;
644 subreq = tevent_wakeup_send(state, state->ev,
645 timeval_current_ofs(1, 0));
646 if (tevent_req_nomem(subreq, req)) {
647 return;
649 tevent_req_set_callback(subreq, nb_trans_send_next, req);
652 static void nb_trans_send_next(struct tevent_req *subreq)
654 struct tevent_req *req = tevent_req_callback_data(
655 subreq, struct tevent_req);
656 struct nb_trans_state *state = tevent_req_data(
657 req, struct nb_trans_state);
658 bool ret;
660 ret = tevent_wakeup_recv(subreq);
661 TALLOC_FREE(subreq);
662 if (!ret) {
663 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
664 return;
666 subreq = tdgram_sendto_send(state, state->ev,
667 state->sock,
668 state->buf, state->buflen,
669 state->dst_addr);
670 if (tevent_req_nomem(subreq, req)) {
671 return;
673 tevent_req_set_callback(subreq, nb_trans_sent, req);
676 static void nb_trans_done(struct tevent_req *subreq)
678 struct tevent_req *req = tevent_req_callback_data(
679 subreq, struct tevent_req);
680 struct nb_trans_state *state = tevent_req_data(
681 req, struct nb_trans_state);
682 NTSTATUS status;
684 status = sock_packet_read_recv(subreq, state, &state->packet);
685 TALLOC_FREE(subreq);
686 if (tevent_req_nterror(req, status)) {
687 return;
689 tevent_req_done(req);
692 static NTSTATUS nb_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
693 struct packet_struct **ppacket)
695 struct nb_trans_state *state = tevent_req_data(
696 req, struct nb_trans_state);
697 NTSTATUS status;
699 if (tevent_req_is_nterror(req, &status)) {
700 return status;
702 *ppacket = talloc_move(mem_ctx, &state->packet);
703 return NT_STATUS_OK;
706 /****************************************************************************
707 Do a NBT node status query on an open socket and return an array of
708 structures holding the returned names or NULL if the query failed.
709 **************************************************************************/
711 struct node_status_query_state {
712 struct sockaddr_storage my_addr;
713 struct sockaddr_storage addr;
714 uint8_t buf[1024];
715 ssize_t buflen;
716 struct packet_struct *packet;
719 static bool node_status_query_validator(struct packet_struct *p,
720 void *private_data);
721 static void node_status_query_done(struct tevent_req *subreq);
723 struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
724 struct tevent_context *ev,
725 struct nmb_name *name,
726 const struct sockaddr_storage *addr)
728 struct tevent_req *req, *subreq;
729 struct node_status_query_state *state;
730 struct packet_struct p;
731 struct nmb_packet *nmb = &p.packet.nmb;
732 struct sockaddr_in *in_addr;
734 req = tevent_req_create(mem_ctx, &state,
735 struct node_status_query_state);
736 if (req == NULL) {
737 return NULL;
740 if (addr->ss_family != AF_INET) {
741 /* Can't do node status to IPv6 */
742 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
743 return tevent_req_post(req, ev);
746 state->addr = *addr;
747 in_addr = (struct sockaddr_in *)(void *)&state->addr;
748 in_addr->sin_port = htons(NMB_PORT);
750 set_socket_addr_v4(&state->my_addr);
752 ZERO_STRUCT(p);
753 nmb->header.name_trn_id = generate_trn_id();
754 nmb->header.opcode = 0;
755 nmb->header.response = false;
756 nmb->header.nm_flags.bcast = false;
757 nmb->header.nm_flags.recursion_available = false;
758 nmb->header.nm_flags.recursion_desired = false;
759 nmb->header.nm_flags.trunc = false;
760 nmb->header.nm_flags.authoritative = false;
761 nmb->header.rcode = 0;
762 nmb->header.qdcount = 1;
763 nmb->header.ancount = 0;
764 nmb->header.nscount = 0;
765 nmb->header.arcount = 0;
766 nmb->question.question_name = *name;
767 nmb->question.question_type = 0x21;
768 nmb->question.question_class = 0x1;
770 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
771 &p);
772 if (state->buflen == 0) {
773 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
774 DEBUG(10, ("build_packet failed\n"));
775 return tevent_req_post(req, ev);
778 subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, false,
779 state->buf, state->buflen,
780 NMB_PACKET, nmb->header.name_trn_id,
781 node_status_query_validator, NULL);
782 if (tevent_req_nomem(subreq, req)) {
783 DEBUG(10, ("nb_trans_send failed\n"));
784 return tevent_req_post(req, ev);
786 if (!tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0))) {
787 return tevent_req_post(req, ev);
789 tevent_req_set_callback(subreq, node_status_query_done, req);
790 return req;
793 static bool node_status_query_validator(struct packet_struct *p,
794 void *private_data)
796 struct nmb_packet *nmb = &p->packet.nmb;
797 debug_nmb_packet(p);
799 if (nmb->header.opcode != 0 ||
800 nmb->header.nm_flags.bcast ||
801 nmb->header.rcode ||
802 !nmb->header.ancount ||
803 nmb->answers->rr_type != 0x21) {
805 * XXXX what do we do with this? could be a redirect,
806 * but we'll discard it for the moment
808 return false;
810 return true;
813 static void node_status_query_done(struct tevent_req *subreq)
815 struct tevent_req *req = tevent_req_callback_data(
816 subreq, struct tevent_req);
817 struct node_status_query_state *state = tevent_req_data(
818 req, struct node_status_query_state);
819 NTSTATUS status;
821 status = nb_trans_recv(subreq, state, &state->packet);
822 TALLOC_FREE(subreq);
823 if (tevent_req_nterror(req, status)) {
824 return;
826 tevent_req_done(req);
829 NTSTATUS node_status_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
830 struct node_status **pnode_status,
831 int *pnum_names,
832 struct node_status_extra *extra)
834 struct node_status_query_state *state = tevent_req_data(
835 req, struct node_status_query_state);
836 struct node_status *node_status;
837 int num_names;
838 NTSTATUS status;
840 if (tevent_req_is_nterror(req, &status)) {
841 return status;
843 node_status = parse_node_status(
844 mem_ctx, &state->packet->packet.nmb.answers->rdata[0],
845 &num_names, extra);
846 if (node_status == NULL) {
847 return NT_STATUS_NO_MEMORY;
849 *pnode_status = node_status;
850 *pnum_names = num_names;
851 return NT_STATUS_OK;
854 NTSTATUS node_status_query(TALLOC_CTX *mem_ctx, struct nmb_name *name,
855 const struct sockaddr_storage *addr,
856 struct node_status **pnode_status,
857 int *pnum_names,
858 struct node_status_extra *extra)
860 TALLOC_CTX *frame = talloc_stackframe();
861 struct tevent_context *ev;
862 struct tevent_req *req;
863 NTSTATUS status = NT_STATUS_NO_MEMORY;
865 ev = samba_tevent_context_init(frame);
866 if (ev == NULL) {
867 goto fail;
869 req = node_status_query_send(ev, ev, name, addr);
870 if (req == NULL) {
871 goto fail;
873 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
874 goto fail;
876 status = node_status_query_recv(req, mem_ctx, pnode_status,
877 pnum_names, extra);
878 fail:
879 TALLOC_FREE(frame);
880 return status;
883 static bool name_status_lmhosts(const struct sockaddr_storage *paddr,
884 int qname_type, fstring pname)
886 FILE *f;
887 char *name;
888 int name_type;
889 struct sockaddr_storage addr;
891 if (paddr->ss_family != AF_INET) {
892 return false;
895 f = startlmhosts(get_dyn_LMHOSTSFILE());
896 if (f == NULL) {
897 return false;
900 while (getlmhostsent(talloc_tos(), f, &name, &name_type, &addr)) {
901 if (addr.ss_family != AF_INET) {
902 continue;
904 if (name_type != qname_type) {
905 continue;
907 if (memcmp(&((const struct sockaddr_in *)paddr)->sin_addr,
908 &((const struct sockaddr_in *)&addr)->sin_addr,
909 sizeof(struct in_addr)) == 0) {
910 fstrcpy(pname, name);
911 endlmhosts(f);
912 return true;
915 endlmhosts(f);
916 return false;
919 /****************************************************************************
920 Find the first type XX name in a node status reply - used for finding
921 a servers name given its IP. Return the matched name in *name.
922 **************************************************************************/
924 bool name_status_find(const char *q_name,
925 int q_type,
926 int type,
927 const struct sockaddr_storage *to_ss,
928 fstring name)
930 char addr[INET6_ADDRSTRLEN];
931 struct sockaddr_storage ss;
932 struct node_status *addrs = NULL;
933 struct nmb_name nname;
934 int count = 0, i;
935 bool result = false;
936 NTSTATUS status;
938 if (lp_disable_netbios()) {
939 DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n",
940 q_name, q_type));
941 return False;
944 print_sockaddr(addr, sizeof(addr), to_ss);
946 DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
947 q_type, addr));
949 /* Check the cache first. */
951 if (namecache_status_fetch(q_name, q_type, type, to_ss, name)) {
952 return True;
955 if (to_ss->ss_family != AF_INET) {
956 /* Can't do node status to IPv6 */
957 return false;
960 result = name_status_lmhosts(to_ss, type, name);
961 if (result) {
962 DBG_DEBUG("Found name %s in lmhosts\n", name);
963 namecache_status_store(q_name, q_type, type, to_ss, name);
964 return true;
967 set_socket_addr_v4(&ss);
969 /* W2K PDC's seem not to respond to '*'#0. JRA */
970 make_nmb_name(&nname, q_name, q_type);
971 status = node_status_query(talloc_tos(), &nname, to_ss,
972 &addrs, &count, NULL);
973 if (!NT_STATUS_IS_OK(status)) {
974 goto done;
977 for (i=0;i<count;i++) {
978 /* Find first one of the requested type that's not a GROUP. */
979 if (addrs[i].type == type && ! (addrs[i].flags & 0x80))
980 break;
982 if (i == count)
983 goto done;
985 pull_ascii_nstring(name, sizeof(fstring), addrs[i].name);
987 /* Store the result in the cache. */
988 /* but don't store an entry for 0x1c names here. Here we have
989 a single host and DOMAIN<0x1c> names should be a list of hosts */
991 if ( q_type != 0x1c ) {
992 namecache_status_store(q_name, q_type, type, to_ss, name);
995 result = true;
997 done:
998 TALLOC_FREE(addrs);
1000 DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
1002 if (result)
1003 DEBUGADD(10, (", name %s ip address is %s", name, addr));
1005 DEBUG(10, ("\n"));
1007 return result;
1011 comparison function used by sort_addr_list
1014 static int addr_compare(const struct sockaddr_storage *ss1,
1015 const struct sockaddr_storage *ss2)
1017 int max_bits1=0, max_bits2=0;
1018 int num_interfaces = iface_count();
1019 int i;
1021 /* Sort IPv4 addresses first. */
1022 if (ss1->ss_family != ss2->ss_family) {
1023 if (ss2->ss_family == AF_INET) {
1024 return 1;
1025 } else {
1026 return -1;
1030 /* Here we know both addresses are of the same
1031 * family. */
1033 for (i=0;i<num_interfaces;i++) {
1034 const struct sockaddr_storage *pss = iface_n_bcast(i);
1035 const unsigned char *p_ss1 = NULL;
1036 const unsigned char *p_ss2 = NULL;
1037 const unsigned char *p_if = NULL;
1038 size_t len = 0;
1039 int bits1, bits2;
1041 if (pss->ss_family != ss1->ss_family) {
1042 /* Ignore interfaces of the wrong type. */
1043 continue;
1045 if (pss->ss_family == AF_INET) {
1046 p_if = (const unsigned char *)
1047 &((const struct sockaddr_in *)pss)->sin_addr;
1048 p_ss1 = (const unsigned char *)
1049 &((const struct sockaddr_in *)ss1)->sin_addr;
1050 p_ss2 = (const unsigned char *)
1051 &((const struct sockaddr_in *)ss2)->sin_addr;
1052 len = 4;
1054 #if defined(HAVE_IPV6)
1055 if (pss->ss_family == AF_INET6) {
1056 p_if = (const unsigned char *)
1057 &((const struct sockaddr_in6 *)pss)->sin6_addr;
1058 p_ss1 = (const unsigned char *)
1059 &((const struct sockaddr_in6 *)ss1)->sin6_addr;
1060 p_ss2 = (const unsigned char *)
1061 &((const struct sockaddr_in6 *)ss2)->sin6_addr;
1062 len = 16;
1064 #endif
1065 if (!p_ss1 || !p_ss2 || !p_if || len == 0) {
1066 continue;
1068 bits1 = matching_len_bits(p_ss1, p_if, len);
1069 bits2 = matching_len_bits(p_ss2, p_if, len);
1070 max_bits1 = MAX(bits1, max_bits1);
1071 max_bits2 = MAX(bits2, max_bits2);
1074 /* Bias towards directly reachable IPs */
1075 if (iface_local((const struct sockaddr *)ss1)) {
1076 if (ss1->ss_family == AF_INET) {
1077 max_bits1 += 32;
1078 } else {
1079 max_bits1 += 128;
1082 if (iface_local((const struct sockaddr *)ss2)) {
1083 if (ss2->ss_family == AF_INET) {
1084 max_bits2 += 32;
1085 } else {
1086 max_bits2 += 128;
1089 return max_bits2 - max_bits1;
1092 /*******************************************************************
1093 compare 2 ldap IPs by nearness to our interfaces - used in qsort
1094 *******************************************************************/
1096 static int ip_service_compare(struct ip_service *ss1, struct ip_service *ss2)
1098 int result;
1100 if ((result = addr_compare(&ss1->ss, &ss2->ss)) != 0) {
1101 return result;
1104 if (ss1->port > ss2->port) {
1105 return 1;
1108 if (ss1->port < ss2->port) {
1109 return -1;
1112 return 0;
1116 sort an IP list so that names that are close to one of our interfaces
1117 are at the top. This prevents the problem where a WINS server returns an IP
1118 that is not reachable from our subnet as the first match
1121 static void sort_addr_list(struct sockaddr_storage *sslist, int count)
1123 if (count <= 1) {
1124 return;
1127 TYPESAFE_QSORT(sslist, count, addr_compare);
1130 static void sort_service_list(struct ip_service *servlist, int count)
1132 if (count <= 1) {
1133 return;
1136 TYPESAFE_QSORT(servlist, count, ip_service_compare);
1139 /**********************************************************************
1140 Remove any duplicate address/port pairs in the list
1141 *********************************************************************/
1143 int remove_duplicate_addrs2(struct ip_service *iplist, int count )
1145 int i, j;
1147 DEBUG(10,("remove_duplicate_addrs2: "
1148 "looking for duplicate address/port pairs\n"));
1150 /* One loop to set duplicates to a zero addr. */
1151 for ( i=0; i<count; i++ ) {
1152 if ( is_zero_addr(&iplist[i].ss)) {
1153 continue;
1156 for ( j=i+1; j<count; j++ ) {
1157 if (sockaddr_equal((struct sockaddr *)(void *)&iplist[i].ss,
1158 (struct sockaddr *)(void *)&iplist[j].ss) &&
1159 iplist[i].port == iplist[j].port) {
1160 zero_sockaddr(&iplist[j].ss);
1165 /* Now remove any addresses set to zero above. */
1166 for (i = 0; i < count; i++) {
1167 while (i < count &&
1168 is_zero_addr(&iplist[i].ss)) {
1169 if (count-i-1>0) {
1170 memmove(&iplist[i],
1171 &iplist[i+1],
1172 (count-i-1)*sizeof(struct ip_service));
1174 count--;
1178 return count;
1181 static bool prioritize_ipv4_list(struct ip_service *iplist, int count)
1183 TALLOC_CTX *frame = talloc_stackframe();
1184 struct ip_service *iplist_new = talloc_array(frame, struct ip_service, count);
1185 int i, j;
1187 if (iplist_new == NULL) {
1188 TALLOC_FREE(frame);
1189 return false;
1192 j = 0;
1194 /* Copy IPv4 first. */
1195 for (i = 0; i < count; i++) {
1196 if (iplist[i].ss.ss_family == AF_INET) {
1197 iplist_new[j++] = iplist[i];
1201 /* Copy IPv6. */
1202 for (i = 0; i < count; i++) {
1203 if (iplist[i].ss.ss_family != AF_INET) {
1204 iplist_new[j++] = iplist[i];
1208 memcpy(iplist, iplist_new, sizeof(struct ip_service)*count);
1209 TALLOC_FREE(frame);
1210 return true;
1213 /****************************************************************************
1214 Do a netbios name query to find someones IP.
1215 Returns an array of IP addresses or NULL if none.
1216 *count will be set to the number of addresses returned.
1217 *timed_out is set if we failed by timing out
1218 ****************************************************************************/
1220 struct name_query_state {
1221 struct sockaddr_storage my_addr;
1222 struct sockaddr_storage addr;
1223 bool bcast;
1224 bool bcast_star_query;
1227 uint8_t buf[1024];
1228 ssize_t buflen;
1230 NTSTATUS validate_error;
1231 uint8_t flags;
1233 struct sockaddr_storage *addrs;
1234 int num_addrs;
1237 static bool name_query_validator(struct packet_struct *p, void *private_data);
1238 static void name_query_done(struct tevent_req *subreq);
1240 struct tevent_req *name_query_send(TALLOC_CTX *mem_ctx,
1241 struct tevent_context *ev,
1242 const char *name, int name_type,
1243 bool bcast, bool recurse,
1244 const struct sockaddr_storage *addr)
1246 struct tevent_req *req, *subreq;
1247 struct name_query_state *state;
1248 struct packet_struct p;
1249 struct nmb_packet *nmb = &p.packet.nmb;
1250 struct sockaddr_in *in_addr;
1252 req = tevent_req_create(mem_ctx, &state, struct name_query_state);
1253 if (req == NULL) {
1254 return NULL;
1256 state->bcast = bcast;
1258 if (addr->ss_family != AF_INET) {
1259 /* Can't do node status to IPv6 */
1260 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1261 return tevent_req_post(req, ev);
1264 if (lp_disable_netbios()) {
1265 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
1266 name, name_type));
1267 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
1268 return tevent_req_post(req, ev);
1271 state->addr = *addr;
1272 in_addr = (struct sockaddr_in *)(void *)&state->addr;
1273 in_addr->sin_port = htons(NMB_PORT);
1275 set_socket_addr_v4(&state->my_addr);
1277 ZERO_STRUCT(p);
1278 nmb->header.name_trn_id = generate_trn_id();
1279 nmb->header.opcode = 0;
1280 nmb->header.response = false;
1281 nmb->header.nm_flags.bcast = bcast;
1282 nmb->header.nm_flags.recursion_available = false;
1283 nmb->header.nm_flags.recursion_desired = recurse;
1284 nmb->header.nm_flags.trunc = false;
1285 nmb->header.nm_flags.authoritative = false;
1286 nmb->header.rcode = 0;
1287 nmb->header.qdcount = 1;
1288 nmb->header.ancount = 0;
1289 nmb->header.nscount = 0;
1290 nmb->header.arcount = 0;
1292 if (bcast && (strcmp(name, "*")==0)) {
1294 * We're doing a broadcast query for all
1295 * names in the area. Remember this so
1296 * we will wait for all names within
1297 * the timeout period.
1299 state->bcast_star_query = true;
1302 make_nmb_name(&nmb->question.question_name,name,name_type);
1304 nmb->question.question_type = 0x20;
1305 nmb->question.question_class = 0x1;
1307 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
1308 &p);
1309 if (state->buflen == 0) {
1310 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1311 DEBUG(10, ("build_packet failed\n"));
1312 return tevent_req_post(req, ev);
1315 subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, bcast,
1316 state->buf, state->buflen,
1317 NMB_PACKET, nmb->header.name_trn_id,
1318 name_query_validator, state);
1319 if (tevent_req_nomem(subreq, req)) {
1320 DEBUG(10, ("nb_trans_send failed\n"));
1321 return tevent_req_post(req, ev);
1323 tevent_req_set_callback(subreq, name_query_done, req);
1324 return req;
1327 static bool name_query_validator(struct packet_struct *p, void *private_data)
1329 struct name_query_state *state = talloc_get_type_abort(
1330 private_data, struct name_query_state);
1331 struct nmb_packet *nmb = &p->packet.nmb;
1332 struct sockaddr_storage *tmp_addrs;
1333 bool got_unique_netbios_name = false;
1334 int i;
1336 debug_nmb_packet(p);
1339 * If we get a Negative Name Query Response from a WINS
1340 * server, we should report it and give up.
1342 if( 0 == nmb->header.opcode /* A query response */
1343 && !state->bcast /* from a WINS server */
1344 && nmb->header.rcode /* Error returned */
1347 if( DEBUGLVL( 3 ) ) {
1348 /* Only executed if DEBUGLEVEL >= 3 */
1349 dbgtext( "Negative name query "
1350 "response, rcode 0x%02x: ",
1351 nmb->header.rcode );
1352 switch( nmb->header.rcode ) {
1353 case 0x01:
1354 dbgtext("Request was invalidly formatted.\n");
1355 break;
1356 case 0x02:
1357 dbgtext("Problem with NBNS, cannot process "
1358 "name.\n");
1359 break;
1360 case 0x03:
1361 dbgtext("The name requested does not "
1362 "exist.\n");
1363 break;
1364 case 0x04:
1365 dbgtext("Unsupported request error.\n");
1366 break;
1367 case 0x05:
1368 dbgtext("Query refused error.\n");
1369 break;
1370 default:
1371 dbgtext("Unrecognized error code.\n" );
1372 break;
1377 * We accept this packet as valid, but tell the upper
1378 * layers that it's a negative response.
1380 state->validate_error = NT_STATUS_NOT_FOUND;
1381 return true;
1384 if (nmb->header.opcode != 0 ||
1385 nmb->header.nm_flags.bcast ||
1386 nmb->header.rcode ||
1387 !nmb->header.ancount) {
1389 * XXXX what do we do with this? Could be a redirect,
1390 * but we'll discard it for the moment.
1392 return false;
1395 tmp_addrs = talloc_realloc(
1396 state, state->addrs, struct sockaddr_storage,
1397 state->num_addrs + nmb->answers->rdlength/6);
1398 if (tmp_addrs == NULL) {
1399 state->validate_error = NT_STATUS_NO_MEMORY;
1400 return true;
1402 state->addrs = tmp_addrs;
1404 DEBUG(2,("Got a positive name query response "
1405 "from %s ( ", inet_ntoa(p->ip)));
1407 for (i=0; i<nmb->answers->rdlength/6; i++) {
1408 uint16_t flags;
1409 struct in_addr ip;
1410 struct sockaddr_storage addr;
1411 int j;
1413 flags = RSVAL(&nmb->answers->rdata[i*6], 0);
1414 got_unique_netbios_name |= ((flags & 0x8000) == 0);
1416 putip((char *)&ip,&nmb->answers->rdata[2+i*6]);
1417 in_addr_to_sockaddr_storage(&addr, ip);
1419 if (is_zero_addr(&addr)) {
1420 continue;
1423 for (j=0; j<state->num_addrs; j++) {
1424 if (sockaddr_equal(
1425 (struct sockaddr *)(void *)&addr,
1426 (struct sockaddr *)(void *)&state->addrs[j])) {
1427 break;
1430 if (j < state->num_addrs) {
1431 /* Already got it */
1432 continue;
1435 DEBUGADD(2,("%s ",inet_ntoa(ip)));
1437 state->addrs[state->num_addrs] = addr;
1438 state->num_addrs += 1;
1440 DEBUGADD(2,(")\n"));
1442 /* We add the flags back ... */
1443 if (nmb->header.response)
1444 state->flags |= NM_FLAGS_RS;
1445 if (nmb->header.nm_flags.authoritative)
1446 state->flags |= NM_FLAGS_AA;
1447 if (nmb->header.nm_flags.trunc)
1448 state->flags |= NM_FLAGS_TC;
1449 if (nmb->header.nm_flags.recursion_desired)
1450 state->flags |= NM_FLAGS_RD;
1451 if (nmb->header.nm_flags.recursion_available)
1452 state->flags |= NM_FLAGS_RA;
1453 if (nmb->header.nm_flags.bcast)
1454 state->flags |= NM_FLAGS_B;
1456 if (state->bcast) {
1458 * We have to collect all entries coming in from broadcast
1459 * queries. If we got a unique name and we are not querying
1460 * all names registered within broadcast area (query
1461 * for the name '*', so state->bcast_star_query is set),
1462 * we're done.
1464 return (got_unique_netbios_name && !state->bcast_star_query);
1467 * WINS responses are accepted when they are received
1469 return true;
1472 static void name_query_done(struct tevent_req *subreq)
1474 struct tevent_req *req = tevent_req_callback_data(
1475 subreq, struct tevent_req);
1476 struct name_query_state *state = tevent_req_data(
1477 req, struct name_query_state);
1478 NTSTATUS status;
1479 struct packet_struct *p = NULL;
1481 status = nb_trans_recv(subreq, state, &p);
1482 TALLOC_FREE(subreq);
1483 if (tevent_req_nterror(req, status)) {
1484 return;
1486 if (!NT_STATUS_IS_OK(state->validate_error)) {
1487 tevent_req_nterror(req, state->validate_error);
1488 return;
1490 tevent_req_done(req);
1493 NTSTATUS name_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1494 struct sockaddr_storage **addrs, int *num_addrs,
1495 uint8_t *flags)
1497 struct name_query_state *state = tevent_req_data(
1498 req, struct name_query_state);
1499 NTSTATUS status;
1501 if (tevent_req_is_nterror(req, &status)) {
1502 if (state->bcast &&
1503 NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
1505 * In the broadcast case we collect replies until the
1506 * timeout.
1508 status = NT_STATUS_OK;
1510 if (!NT_STATUS_IS_OK(status)) {
1511 return status;
1514 if (state->num_addrs == 0) {
1515 return NT_STATUS_NOT_FOUND;
1517 *addrs = talloc_move(mem_ctx, &state->addrs);
1518 sort_addr_list(*addrs, state->num_addrs);
1519 *num_addrs = state->num_addrs;
1520 if (flags != NULL) {
1521 *flags = state->flags;
1523 return NT_STATUS_OK;
1526 NTSTATUS name_query(const char *name, int name_type,
1527 bool bcast, bool recurse,
1528 const struct sockaddr_storage *to_ss,
1529 TALLOC_CTX *mem_ctx,
1530 struct sockaddr_storage **addrs,
1531 int *num_addrs, uint8_t *flags)
1533 TALLOC_CTX *frame = talloc_stackframe();
1534 struct tevent_context *ev;
1535 struct tevent_req *req;
1536 struct timeval timeout;
1537 NTSTATUS status = NT_STATUS_NO_MEMORY;
1539 ev = samba_tevent_context_init(frame);
1540 if (ev == NULL) {
1541 goto fail;
1543 req = name_query_send(ev, ev, name, name_type, bcast, recurse, to_ss);
1544 if (req == NULL) {
1545 goto fail;
1547 if (bcast) {
1548 timeout = timeval_current_ofs(0, 250000);
1549 } else {
1550 timeout = timeval_current_ofs(2, 0);
1552 if (!tevent_req_set_endtime(req, ev, timeout)) {
1553 goto fail;
1555 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1556 goto fail;
1558 status = name_query_recv(req, mem_ctx, addrs, num_addrs, flags);
1559 fail:
1560 TALLOC_FREE(frame);
1561 return status;
1564 /********************************************************
1565 Convert an array if struct sockaddr_storage to struct ip_service
1566 return false on failure. Port is set to PORT_NONE;
1567 pcount is [in/out] - it is the length of ss_list on input,
1568 and the length of return_iplist on output as we remove any
1569 zero addresses from ss_list.
1570 *********************************************************/
1572 static bool convert_ss2service(struct ip_service **return_iplist,
1573 const struct sockaddr_storage *ss_list,
1574 int *pcount)
1576 int i;
1577 int orig_count = *pcount;
1578 int real_count = 0;
1580 if (orig_count==0 || !ss_list )
1581 return False;
1583 /* Filter out zero addrs. */
1584 for ( i=0; i<orig_count; i++ ) {
1585 if (is_zero_addr(&ss_list[i])) {
1586 continue;
1588 real_count++;
1590 if (real_count==0) {
1591 return false;
1594 /* copy the ip address; port will be PORT_NONE */
1595 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, real_count)) ==
1596 NULL) {
1597 DEBUG(0,("convert_ip2service: malloc failed "
1598 "for %d enetries!\n", real_count ));
1599 return False;
1602 for ( i=0, real_count = 0; i<orig_count; i++ ) {
1603 if (is_zero_addr(&ss_list[i])) {
1604 continue;
1606 (*return_iplist)[real_count].ss = ss_list[i];
1607 (*return_iplist)[real_count].port = PORT_NONE;
1608 real_count++;
1611 *pcount = real_count;
1612 return true;
1615 struct name_queries_state {
1616 struct tevent_context *ev;
1617 const char *name;
1618 int name_type;
1619 bool bcast;
1620 bool recurse;
1621 const struct sockaddr_storage *addrs;
1622 int num_addrs;
1623 int wait_msec;
1624 int timeout_msec;
1626 struct tevent_req **subreqs;
1627 int num_received;
1628 int num_sent;
1630 int received_index;
1631 struct sockaddr_storage *result_addrs;
1632 int num_result_addrs;
1633 uint8_t flags;
1636 static void name_queries_done(struct tevent_req *subreq);
1637 static void name_queries_next(struct tevent_req *subreq);
1640 * Send a name query to multiple destinations with a wait time in between
1643 static struct tevent_req *name_queries_send(
1644 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1645 const char *name, int name_type,
1646 bool bcast, bool recurse,
1647 const struct sockaddr_storage *addrs,
1648 int num_addrs, int wait_msec, int timeout_msec)
1650 struct tevent_req *req, *subreq;
1651 struct name_queries_state *state;
1653 req = tevent_req_create(mem_ctx, &state,
1654 struct name_queries_state);
1655 if (req == NULL) {
1656 return NULL;
1658 state->ev = ev;
1659 state->name = name;
1660 state->name_type = name_type;
1661 state->bcast = bcast;
1662 state->recurse = recurse;
1663 state->addrs = addrs;
1664 state->num_addrs = num_addrs;
1665 state->wait_msec = wait_msec;
1666 state->timeout_msec = timeout_msec;
1668 state->subreqs = talloc_zero_array(
1669 state, struct tevent_req *, num_addrs);
1670 if (tevent_req_nomem(state->subreqs, req)) {
1671 return tevent_req_post(req, ev);
1673 state->num_sent = 0;
1675 subreq = name_query_send(
1676 state->subreqs, state->ev, name, name_type, bcast, recurse,
1677 &state->addrs[state->num_sent]);
1678 if (tevent_req_nomem(subreq, req)) {
1679 return tevent_req_post(req, ev);
1681 if (!tevent_req_set_endtime(
1682 subreq, state->ev,
1683 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1684 return tevent_req_post(req, ev);
1686 tevent_req_set_callback(subreq, name_queries_done, req);
1688 state->subreqs[state->num_sent] = subreq;
1689 state->num_sent += 1;
1691 if (state->num_sent < state->num_addrs) {
1692 subreq = tevent_wakeup_send(
1693 state, state->ev,
1694 timeval_current_ofs(0, state->wait_msec * 1000));
1695 if (tevent_req_nomem(subreq, req)) {
1696 return tevent_req_post(req, ev);
1698 tevent_req_set_callback(subreq, name_queries_next, req);
1700 return req;
1703 static void name_queries_done(struct tevent_req *subreq)
1705 struct tevent_req *req = tevent_req_callback_data(
1706 subreq, struct tevent_req);
1707 struct name_queries_state *state = tevent_req_data(
1708 req, struct name_queries_state);
1709 int i;
1710 NTSTATUS status;
1712 status = name_query_recv(subreq, state, &state->result_addrs,
1713 &state->num_result_addrs, &state->flags);
1715 for (i=0; i<state->num_sent; i++) {
1716 if (state->subreqs[i] == subreq) {
1717 break;
1720 if (i == state->num_sent) {
1721 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1722 return;
1724 TALLOC_FREE(state->subreqs[i]);
1726 state->num_received += 1;
1728 if (!NT_STATUS_IS_OK(status)) {
1730 if (state->num_received >= state->num_addrs) {
1731 tevent_req_nterror(req, status);
1732 return;
1735 * Still outstanding requests, just wait
1737 return;
1739 state->received_index = i;
1740 tevent_req_done(req);
1743 static void name_queries_next(struct tevent_req *subreq)
1745 struct tevent_req *req = tevent_req_callback_data(
1746 subreq, struct tevent_req);
1747 struct name_queries_state *state = tevent_req_data(
1748 req, struct name_queries_state);
1750 if (!tevent_wakeup_recv(subreq)) {
1751 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1752 return;
1755 subreq = name_query_send(
1756 state->subreqs, state->ev,
1757 state->name, state->name_type, state->bcast, state->recurse,
1758 &state->addrs[state->num_sent]);
1759 if (tevent_req_nomem(subreq, req)) {
1760 return;
1762 tevent_req_set_callback(subreq, name_queries_done, req);
1763 if (!tevent_req_set_endtime(
1764 subreq, state->ev,
1765 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1766 return;
1768 state->subreqs[state->num_sent] = subreq;
1769 state->num_sent += 1;
1771 if (state->num_sent < state->num_addrs) {
1772 subreq = tevent_wakeup_send(
1773 state, state->ev,
1774 timeval_current_ofs(0, state->wait_msec * 1000));
1775 if (tevent_req_nomem(subreq, req)) {
1776 return;
1778 tevent_req_set_callback(subreq, name_queries_next, req);
1782 static NTSTATUS name_queries_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1783 struct sockaddr_storage **result_addrs,
1784 int *num_result_addrs, uint8_t *flags,
1785 int *received_index)
1787 struct name_queries_state *state = tevent_req_data(
1788 req, struct name_queries_state);
1789 NTSTATUS status;
1791 if (tevent_req_is_nterror(req, &status)) {
1792 return status;
1795 if (result_addrs != NULL) {
1796 *result_addrs = talloc_move(mem_ctx, &state->result_addrs);
1798 if (num_result_addrs != NULL) {
1799 *num_result_addrs = state->num_result_addrs;
1801 if (flags != NULL) {
1802 *flags = state->flags;
1804 if (received_index != NULL) {
1805 *received_index = state->received_index;
1807 return NT_STATUS_OK;
1810 /********************************************************
1811 Resolve via "bcast" method.
1812 *********************************************************/
1814 struct name_resolve_bcast_state {
1815 struct sockaddr_storage *addrs;
1816 int num_addrs;
1819 static void name_resolve_bcast_done(struct tevent_req *subreq);
1821 struct tevent_req *name_resolve_bcast_send(TALLOC_CTX *mem_ctx,
1822 struct tevent_context *ev,
1823 const char *name,
1824 int name_type)
1826 struct tevent_req *req, *subreq;
1827 struct name_resolve_bcast_state *state;
1828 struct sockaddr_storage *bcast_addrs;
1829 int i, num_addrs, num_bcast_addrs;
1831 req = tevent_req_create(mem_ctx, &state,
1832 struct name_resolve_bcast_state);
1833 if (req == NULL) {
1834 return NULL;
1837 if (lp_disable_netbios()) {
1838 DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
1839 name, name_type));
1840 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1841 return tevent_req_post(req, ev);
1845 * "bcast" means do a broadcast lookup on all the local interfaces.
1848 DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
1849 "for name %s<0x%x>\n", name, name_type));
1851 num_addrs = iface_count();
1852 bcast_addrs = talloc_array(state, struct sockaddr_storage, num_addrs);
1853 if (tevent_req_nomem(bcast_addrs, req)) {
1854 return tevent_req_post(req, ev);
1858 * Lookup the name on all the interfaces, return on
1859 * the first successful match.
1861 num_bcast_addrs = 0;
1863 for (i=0; i<num_addrs; i++) {
1864 const struct sockaddr_storage *pss = iface_n_bcast(i);
1866 if (pss->ss_family != AF_INET) {
1867 continue;
1869 bcast_addrs[num_bcast_addrs] = *pss;
1870 num_bcast_addrs += 1;
1873 subreq = name_queries_send(state, ev, name, name_type, true, true,
1874 bcast_addrs, num_bcast_addrs, 0, 1000);
1875 if (tevent_req_nomem(subreq, req)) {
1876 return tevent_req_post(req, ev);
1878 tevent_req_set_callback(subreq, name_resolve_bcast_done, req);
1879 return req;
1882 static void name_resolve_bcast_done(struct tevent_req *subreq)
1884 struct tevent_req *req = tevent_req_callback_data(
1885 subreq, struct tevent_req);
1886 struct name_resolve_bcast_state *state = tevent_req_data(
1887 req, struct name_resolve_bcast_state);
1888 NTSTATUS status;
1890 status = name_queries_recv(subreq, state,
1891 &state->addrs, &state->num_addrs,
1892 NULL, NULL);
1893 TALLOC_FREE(subreq);
1894 if (tevent_req_nterror(req, status)) {
1895 return;
1897 tevent_req_done(req);
1900 NTSTATUS name_resolve_bcast_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1901 struct sockaddr_storage **addrs,
1902 int *num_addrs)
1904 struct name_resolve_bcast_state *state = tevent_req_data(
1905 req, struct name_resolve_bcast_state);
1906 NTSTATUS status;
1908 if (tevent_req_is_nterror(req, &status)) {
1909 return status;
1911 *addrs = talloc_move(mem_ctx, &state->addrs);
1912 *num_addrs = state->num_addrs;
1913 return NT_STATUS_OK;
1916 NTSTATUS name_resolve_bcast(const char *name,
1917 int name_type,
1918 TALLOC_CTX *mem_ctx,
1919 struct sockaddr_storage **return_iplist,
1920 int *return_count)
1922 TALLOC_CTX *frame = talloc_stackframe();
1923 struct tevent_context *ev;
1924 struct tevent_req *req;
1925 NTSTATUS status = NT_STATUS_NO_MEMORY;
1927 ev = samba_tevent_context_init(frame);
1928 if (ev == NULL) {
1929 goto fail;
1931 req = name_resolve_bcast_send(frame, ev, name, name_type);
1932 if (req == NULL) {
1933 goto fail;
1935 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1936 goto fail;
1938 status = name_resolve_bcast_recv(req, mem_ctx, return_iplist,
1939 return_count);
1940 fail:
1941 TALLOC_FREE(frame);
1942 return status;
1945 struct query_wins_list_state {
1946 struct tevent_context *ev;
1947 const char *name;
1948 uint8_t name_type;
1949 struct in_addr *servers;
1950 uint32_t num_servers;
1951 struct sockaddr_storage server;
1952 uint32_t num_sent;
1954 struct sockaddr_storage *addrs;
1955 int num_addrs;
1956 uint8_t flags;
1959 static void query_wins_list_done(struct tevent_req *subreq);
1962 * Query a list of (replicating) wins servers in sequence, call them
1963 * dead if they don't reply
1966 static struct tevent_req *query_wins_list_send(
1967 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1968 struct in_addr src_ip, const char *name, uint8_t name_type,
1969 struct in_addr *servers, int num_servers)
1971 struct tevent_req *req, *subreq;
1972 struct query_wins_list_state *state;
1974 req = tevent_req_create(mem_ctx, &state,
1975 struct query_wins_list_state);
1976 if (req == NULL) {
1977 return NULL;
1979 state->ev = ev;
1980 state->name = name;
1981 state->name_type = name_type;
1982 state->servers = servers;
1983 state->num_servers = num_servers;
1985 if (state->num_servers == 0) {
1986 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
1987 return tevent_req_post(req, ev);
1990 in_addr_to_sockaddr_storage(
1991 &state->server, state->servers[state->num_sent]);
1993 subreq = name_query_send(state, state->ev,
1994 state->name, state->name_type,
1995 false, true, &state->server);
1996 state->num_sent += 1;
1997 if (tevent_req_nomem(subreq, req)) {
1998 return tevent_req_post(req, ev);
2000 if (!tevent_req_set_endtime(subreq, state->ev,
2001 timeval_current_ofs(2, 0))) {
2002 return tevent_req_post(req, ev);
2004 tevent_req_set_callback(subreq, query_wins_list_done, req);
2005 return req;
2008 static void query_wins_list_done(struct tevent_req *subreq)
2010 struct tevent_req *req = tevent_req_callback_data(
2011 subreq, struct tevent_req);
2012 struct query_wins_list_state *state = tevent_req_data(
2013 req, struct query_wins_list_state);
2014 NTSTATUS status;
2016 status = name_query_recv(subreq, state,
2017 &state->addrs, &state->num_addrs,
2018 &state->flags);
2019 TALLOC_FREE(subreq);
2020 if (NT_STATUS_IS_OK(status)) {
2021 tevent_req_done(req);
2022 return;
2024 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
2025 tevent_req_nterror(req, status);
2026 return;
2028 wins_srv_died(state->servers[state->num_sent-1],
2029 my_socket_addr_v4());
2031 if (state->num_sent == state->num_servers) {
2032 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2033 return;
2036 in_addr_to_sockaddr_storage(
2037 &state->server, state->servers[state->num_sent]);
2039 subreq = name_query_send(state, state->ev,
2040 state->name, state->name_type,
2041 false, true, &state->server);
2042 state->num_sent += 1;
2043 if (tevent_req_nomem(subreq, req)) {
2044 return;
2046 if (!tevent_req_set_endtime(subreq, state->ev,
2047 timeval_current_ofs(2, 0))) {
2048 return;
2050 tevent_req_set_callback(subreq, query_wins_list_done, req);
2053 static NTSTATUS query_wins_list_recv(struct tevent_req *req,
2054 TALLOC_CTX *mem_ctx,
2055 struct sockaddr_storage **addrs,
2056 int *num_addrs,
2057 uint8_t *flags)
2059 struct query_wins_list_state *state = tevent_req_data(
2060 req, struct query_wins_list_state);
2061 NTSTATUS status;
2063 if (tevent_req_is_nterror(req, &status)) {
2064 return status;
2066 if (addrs != NULL) {
2067 *addrs = talloc_move(mem_ctx, &state->addrs);
2069 if (num_addrs != NULL) {
2070 *num_addrs = state->num_addrs;
2072 if (flags != NULL) {
2073 *flags = state->flags;
2075 return NT_STATUS_OK;
2078 struct resolve_wins_state {
2079 int num_sent;
2080 int num_received;
2082 struct sockaddr_storage *addrs;
2083 int num_addrs;
2084 uint8_t flags;
2087 static void resolve_wins_done(struct tevent_req *subreq);
2089 struct tevent_req *resolve_wins_send(TALLOC_CTX *mem_ctx,
2090 struct tevent_context *ev,
2091 const char *name,
2092 int name_type)
2094 struct tevent_req *req, *subreq;
2095 struct resolve_wins_state *state;
2096 char **wins_tags = NULL;
2097 struct sockaddr_storage src_ss;
2098 struct in_addr src_ip;
2099 int i, num_wins_tags;
2101 req = tevent_req_create(mem_ctx, &state,
2102 struct resolve_wins_state);
2103 if (req == NULL) {
2104 return NULL;
2107 if (wins_srv_count() < 1) {
2108 DEBUG(3,("resolve_wins: WINS server resolution selected "
2109 "and no WINS servers listed.\n"));
2110 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2111 goto fail;
2114 /* the address we will be sending from */
2115 if (!interpret_string_addr(&src_ss, lp_nbt_client_socket_address(),
2116 AI_NUMERICHOST|AI_PASSIVE)) {
2117 zero_sockaddr(&src_ss);
2120 if (src_ss.ss_family != AF_INET) {
2121 char addr[INET6_ADDRSTRLEN];
2122 print_sockaddr(addr, sizeof(addr), &src_ss);
2123 DEBUG(3,("resolve_wins: cannot receive WINS replies "
2124 "on IPv6 address %s\n",
2125 addr));
2126 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2127 goto fail;
2130 src_ip = ((const struct sockaddr_in *)(void *)&src_ss)->sin_addr;
2132 wins_tags = wins_srv_tags();
2133 if (wins_tags == NULL) {
2134 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2135 goto fail;
2138 num_wins_tags = 0;
2139 while (wins_tags[num_wins_tags] != NULL) {
2140 num_wins_tags += 1;
2143 for (i=0; i<num_wins_tags; i++) {
2144 int num_servers, num_alive;
2145 struct in_addr *servers, *alive;
2146 int j;
2148 if (!wins_server_tag_ips(wins_tags[i], talloc_tos(),
2149 &servers, &num_servers)) {
2150 DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
2151 wins_tags[i]));
2152 continue;
2155 alive = talloc_array(state, struct in_addr, num_servers);
2156 if (tevent_req_nomem(alive, req)) {
2157 goto fail;
2160 num_alive = 0;
2161 for (j=0; j<num_servers; j++) {
2162 struct in_addr wins_ip = servers[j];
2164 if (global_in_nmbd && ismyip_v4(wins_ip)) {
2165 /* yikes! we'll loop forever */
2166 continue;
2168 /* skip any that have been unresponsive lately */
2169 if (wins_srv_is_dead(wins_ip, src_ip)) {
2170 continue;
2172 DEBUG(3, ("resolve_wins: using WINS server %s "
2173 "and tag '%s'\n",
2174 inet_ntoa(wins_ip), wins_tags[i]));
2175 alive[num_alive] = wins_ip;
2176 num_alive += 1;
2178 TALLOC_FREE(servers);
2180 if (num_alive == 0) {
2181 continue;
2184 subreq = query_wins_list_send(
2185 state, ev, src_ip, name, name_type,
2186 alive, num_alive);
2187 if (tevent_req_nomem(subreq, req)) {
2188 goto fail;
2190 tevent_req_set_callback(subreq, resolve_wins_done, req);
2191 state->num_sent += 1;
2194 if (state->num_sent == 0) {
2195 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2196 goto fail;
2199 wins_srv_tags_free(wins_tags);
2200 return req;
2201 fail:
2202 wins_srv_tags_free(wins_tags);
2203 return tevent_req_post(req, ev);
2206 static void resolve_wins_done(struct tevent_req *subreq)
2208 struct tevent_req *req = tevent_req_callback_data(
2209 subreq, struct tevent_req);
2210 struct resolve_wins_state *state = tevent_req_data(
2211 req, struct resolve_wins_state);
2212 NTSTATUS status;
2214 status = query_wins_list_recv(subreq, state, &state->addrs,
2215 &state->num_addrs, &state->flags);
2216 if (NT_STATUS_IS_OK(status)) {
2217 tevent_req_done(req);
2218 return;
2221 state->num_received += 1;
2223 if (state->num_received < state->num_sent) {
2225 * Wait for the others
2227 return;
2229 tevent_req_nterror(req, status);
2232 NTSTATUS resolve_wins_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2233 struct sockaddr_storage **addrs,
2234 int *num_addrs, uint8_t *flags)
2236 struct resolve_wins_state *state = tevent_req_data(
2237 req, struct resolve_wins_state);
2238 NTSTATUS status;
2240 if (tevent_req_is_nterror(req, &status)) {
2241 return status;
2243 if (addrs != NULL) {
2244 *addrs = talloc_move(mem_ctx, &state->addrs);
2246 if (num_addrs != NULL) {
2247 *num_addrs = state->num_addrs;
2249 if (flags != NULL) {
2250 *flags = state->flags;
2252 return NT_STATUS_OK;
2255 /********************************************************
2256 Resolve via "wins" method.
2257 *********************************************************/
2259 NTSTATUS resolve_wins(const char *name,
2260 int name_type,
2261 TALLOC_CTX *mem_ctx,
2262 struct sockaddr_storage **return_iplist,
2263 int *return_count)
2265 struct tevent_context *ev;
2266 struct tevent_req *req;
2267 NTSTATUS status = NT_STATUS_NO_MEMORY;
2269 ev = samba_tevent_context_init(talloc_tos());
2270 if (ev == NULL) {
2271 goto fail;
2273 req = resolve_wins_send(ev, ev, name, name_type);
2274 if (req == NULL) {
2275 goto fail;
2277 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2278 goto fail;
2280 status = resolve_wins_recv(req, mem_ctx, return_iplist, return_count,
2281 NULL);
2282 fail:
2283 TALLOC_FREE(ev);
2284 return status;
2287 /********************************************************
2288 Resolve via "hosts" method.
2289 *********************************************************/
2291 static NTSTATUS resolve_hosts(const char *name, int name_type,
2292 TALLOC_CTX *mem_ctx,
2293 struct sockaddr_storage **return_iplist,
2294 int *return_count)
2297 * "host" means do a localhost, or dns lookup.
2299 struct addrinfo hints;
2300 struct addrinfo *ailist = NULL;
2301 struct addrinfo *res = NULL;
2302 int ret = -1;
2303 int i = 0;
2305 if ( name_type != 0x20 && name_type != 0x0) {
2306 DEBUG(5, ("resolve_hosts: not appropriate "
2307 "for name type <0x%x>\n",
2308 name_type));
2309 return NT_STATUS_INVALID_PARAMETER;
2312 *return_iplist = NULL;
2313 *return_count = 0;
2315 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
2316 name, name_type));
2318 ZERO_STRUCT(hints);
2319 /* By default make sure it supports TCP. */
2320 hints.ai_socktype = SOCK_STREAM;
2321 hints.ai_flags = AI_ADDRCONFIG;
2323 #if !defined(HAVE_IPV6)
2324 /* Unless we have IPv6, we really only want IPv4 addresses back. */
2325 hints.ai_family = AF_INET;
2326 #endif
2328 ret = getaddrinfo(name,
2329 NULL,
2330 &hints,
2331 &ailist);
2332 if (ret) {
2333 DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
2334 name,
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) {
2342 continue;
2345 ZERO_STRUCT(ss);
2346 memcpy(&ss, res->ai_addr, res->ai_addrlen);
2348 if (is_zero_addr(&ss)) {
2349 continue;
2352 *return_count += 1;
2354 *return_iplist = talloc_realloc(
2355 mem_ctx, *return_iplist, struct sockaddr_storage,
2356 *return_count);
2357 if (!*return_iplist) {
2358 DEBUG(3,("resolve_hosts: malloc fail !\n"));
2359 freeaddrinfo(ailist);
2360 return NT_STATUS_NO_MEMORY;
2362 (*return_iplist)[i] = ss;
2363 i++;
2365 if (ailist) {
2366 freeaddrinfo(ailist);
2368 if (*return_count) {
2369 return NT_STATUS_OK;
2371 return NT_STATUS_UNSUCCESSFUL;
2374 /********************************************************
2375 Resolve via "ADS" method.
2376 *********************************************************/
2378 /* Special name type used to cause a _kerberos DNS lookup. */
2379 #define KDC_NAME_TYPE 0xDCDC
2381 static NTSTATUS resolve_ads(const char *name,
2382 int name_type,
2383 const char *sitename,
2384 struct ip_service **return_iplist,
2385 int *return_count)
2387 int i;
2388 NTSTATUS status;
2389 TALLOC_CTX *ctx;
2390 struct dns_rr_srv *dcs = NULL;
2391 int numdcs = 0;
2392 int numaddrs = 0;
2394 if ((name_type != 0x1c) && (name_type != KDC_NAME_TYPE) &&
2395 (name_type != 0x1b)) {
2396 return NT_STATUS_INVALID_PARAMETER;
2399 if ( (ctx = talloc_init("resolve_ads")) == NULL ) {
2400 DEBUG(0,("resolve_ads: talloc_init() failed!\n"));
2401 return NT_STATUS_NO_MEMORY;
2404 /* The DNS code needs fixing to find IPv6 addresses... JRA. */
2405 switch (name_type) {
2406 case 0x1b:
2407 DEBUG(5,("resolve_ads: Attempting to resolve "
2408 "PDC for %s using DNS\n", name));
2409 status = ads_dns_query_pdc(ctx,
2410 name,
2411 &dcs,
2412 &numdcs);
2413 break;
2415 case 0x1c:
2416 DEBUG(5,("resolve_ads: Attempting to resolve "
2417 "DCs for %s using DNS\n", name));
2418 status = ads_dns_query_dcs(ctx,
2419 name,
2420 sitename,
2421 &dcs,
2422 &numdcs);
2423 break;
2424 case KDC_NAME_TYPE:
2425 DEBUG(5,("resolve_ads: Attempting to resolve "
2426 "KDCs for %s using DNS\n", name));
2427 status = ads_dns_query_kdcs(ctx,
2428 name,
2429 sitename,
2430 &dcs,
2431 &numdcs);
2432 break;
2433 default:
2434 status = NT_STATUS_INVALID_PARAMETER;
2435 break;
2438 if ( !NT_STATUS_IS_OK( status ) ) {
2439 talloc_destroy(ctx);
2440 return status;
2443 if (numdcs == 0) {
2444 *return_iplist = NULL;
2445 *return_count = 0;
2446 talloc_destroy(ctx);
2447 return NT_STATUS_OK;
2450 for (i=0;i<numdcs;i++) {
2451 if (!dcs[i].ss_s) {
2452 numaddrs += 1;
2453 } else {
2454 numaddrs += dcs[i].num_ips;
2458 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, numaddrs)) ==
2459 NULL ) {
2460 DEBUG(0,("resolve_ads: malloc failed for %d entries\n",
2461 numaddrs ));
2462 talloc_destroy(ctx);
2463 return NT_STATUS_NO_MEMORY;
2466 /* now unroll the list of IP addresses */
2468 *return_count = 0;
2470 for (i = 0; i < numdcs && (*return_count<numaddrs); i++ ) {
2471 /* If we don't have an IP list for a name, lookup it up */
2472 if (!dcs[i].ss_s) {
2473 /* We need to get all IP addresses here. */
2474 struct addrinfo *res = NULL;
2475 struct addrinfo *p;
2476 int extra_addrs = 0;
2478 if (!interpret_string_addr_internal(&res,
2479 dcs[i].hostname,
2480 0)) {
2481 continue;
2483 /* Add in every IP from the lookup. How
2484 many is that ? */
2485 for (p = res; p; p = p->ai_next) {
2486 struct sockaddr_storage ss;
2487 memcpy(&ss, p->ai_addr, p->ai_addrlen);
2488 if (is_zero_addr(&ss)) {
2489 continue;
2491 extra_addrs++;
2493 if (extra_addrs > 1) {
2494 /* We need to expand the return_iplist array
2495 as we only budgeted for one address. */
2496 numaddrs += (extra_addrs-1);
2497 *return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
2498 struct ip_service,
2499 numaddrs);
2500 if (*return_iplist == NULL) {
2501 if (res) {
2502 freeaddrinfo(res);
2504 talloc_destroy(ctx);
2505 return NT_STATUS_NO_MEMORY;
2508 for (p = res; p; p = p->ai_next) {
2509 (*return_iplist)[*return_count].port = dcs[i].port;
2510 memcpy(&(*return_iplist)[*return_count].ss,
2511 p->ai_addr,
2512 p->ai_addrlen);
2513 if (is_zero_addr(&(*return_iplist)[*return_count].ss)) {
2514 continue;
2516 (*return_count)++;
2517 /* Should never happen, but still... */
2518 if (*return_count>=numaddrs) {
2519 break;
2522 if (res) {
2523 freeaddrinfo(res);
2525 } else {
2526 /* use all the IP addresses from the SRV response */
2527 size_t j;
2528 for (j = 0; j < dcs[i].num_ips; j++) {
2529 (*return_iplist)[*return_count].port = dcs[i].port;
2530 (*return_iplist)[*return_count].ss = dcs[i].ss_s[j];
2531 if (is_zero_addr(&(*return_iplist)[*return_count].ss)) {
2532 continue;
2534 (*return_count)++;
2535 /* Should never happen, but still... */
2536 if (*return_count>=numaddrs) {
2537 break;
2543 talloc_destroy(ctx);
2544 return NT_STATUS_OK;
2547 static const char **filter_out_nbt_lookup(TALLOC_CTX *mem_ctx,
2548 const char **resolve_order)
2550 size_t i, len, result_idx;
2551 const char **result;
2553 len = 0;
2554 while (resolve_order[len] != NULL) {
2555 len += 1;
2558 result = talloc_array(mem_ctx, const char *, len+1);
2559 if (result == NULL) {
2560 return NULL;
2563 result_idx = 0;
2565 for (i=0; i<len; i++) {
2566 const char *tok = resolve_order[i];
2568 if (strequal(tok, "lmhosts") || strequal(tok, "wins") ||
2569 strequal(tok, "bcast")) {
2570 continue;
2572 result[result_idx++] = tok;
2574 result[result_idx] = NULL;
2576 return result;
2579 /*******************************************************************
2580 Internal interface to resolve a name into an IP address.
2581 Use this function if the string is either an IP address, DNS
2582 or host name or NetBIOS name. This uses the name switch in the
2583 smb.conf to determine the order of name resolution.
2585 Added support for ip addr/port to support ADS ldap servers.
2586 the only place we currently care about the port is in the
2587 resolve_hosts() when looking up DC's via SRV RR entries in DNS
2588 **********************************************************************/
2590 NTSTATUS internal_resolve_name(const char *name,
2591 int name_type,
2592 const char *sitename,
2593 struct ip_service **return_iplist,
2594 int *return_count,
2595 const char **resolve_order)
2597 const char *tok;
2598 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2599 int i;
2600 TALLOC_CTX *frame = NULL;
2602 *return_iplist = NULL;
2603 *return_count = 0;
2605 DEBUG(10, ("internal_resolve_name: looking up %s#%x (sitename %s)\n",
2606 name, name_type, sitename ? sitename : "(null)"));
2608 if (is_ipaddress(name)) {
2609 if ((*return_iplist = SMB_MALLOC_P(struct ip_service)) ==
2610 NULL) {
2611 DEBUG(0,("internal_resolve_name: malloc fail !\n"));
2612 return NT_STATUS_NO_MEMORY;
2615 /* ignore the port here */
2616 (*return_iplist)->port = PORT_NONE;
2618 /* if it's in the form of an IP address then get the lib to interpret it */
2619 if (!interpret_string_addr(&(*return_iplist)->ss,
2620 name, AI_NUMERICHOST)) {
2621 DEBUG(1,("internal_resolve_name: interpret_string_addr "
2622 "failed on %s\n",
2623 name));
2624 SAFE_FREE(*return_iplist);
2625 return NT_STATUS_INVALID_PARAMETER;
2627 if (is_zero_addr(&(*return_iplist)->ss)) {
2628 SAFE_FREE(*return_iplist);
2629 return NT_STATUS_UNSUCCESSFUL;
2631 *return_count = 1;
2632 return NT_STATUS_OK;
2635 /* Check name cache */
2637 if (namecache_fetch(name, name_type, return_iplist, return_count)) {
2638 *return_count = remove_duplicate_addrs2(*return_iplist,
2639 *return_count );
2640 /* This could be a negative response */
2641 if (*return_count > 0) {
2642 return NT_STATUS_OK;
2643 } else {
2644 return NT_STATUS_UNSUCCESSFUL;
2648 /* set the name resolution order */
2650 if (resolve_order && strcmp(resolve_order[0], "NULL") == 0) {
2651 DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
2652 return NT_STATUS_INVALID_PARAMETER;
2655 if (!resolve_order || !resolve_order[0]) {
2656 static const char *host_order[] = { "host", NULL };
2657 resolve_order = host_order;
2660 frame = talloc_stackframe();
2662 if ((strlen(name) > MAX_NETBIOSNAME_LEN - 1) ||
2663 (strchr(name, '.') != NULL)) {
2665 * Don't do NBT lookup, the name would not fit anyway
2667 resolve_order = filter_out_nbt_lookup(frame, resolve_order);
2668 if (resolve_order == NULL) {
2669 TALLOC_FREE(frame);
2670 return NT_STATUS_NO_MEMORY;
2674 /* iterate through the name resolution backends */
2676 for (i=0; resolve_order[i]; i++) {
2677 tok = resolve_order[i];
2679 if((strequal(tok, "host") || strequal(tok, "hosts"))) {
2680 struct sockaddr_storage *ss_list;
2681 status = resolve_hosts(name, name_type,
2682 talloc_tos(), &ss_list,
2683 return_count);
2684 if (NT_STATUS_IS_OK(status)) {
2685 if (!convert_ss2service(return_iplist,
2686 ss_list,
2687 return_count)) {
2688 status = NT_STATUS_NO_MEMORY;
2690 goto done;
2692 } else if(strequal( tok, "kdc")) {
2693 /* deal with KDC_NAME_TYPE names here.
2694 * This will result in a SRV record lookup */
2695 status = resolve_ads(name, KDC_NAME_TYPE, sitename,
2696 return_iplist, return_count);
2697 if (NT_STATUS_IS_OK(status)) {
2698 /* Ensure we don't namecache
2699 * this with the KDC port. */
2700 name_type = KDC_NAME_TYPE;
2701 goto done;
2703 } else if(strequal( tok, "ads")) {
2704 /* deal with 0x1c and 0x1b names here.
2705 * This will result in a SRV record lookup */
2706 status = resolve_ads(name, name_type, sitename,
2707 return_iplist, return_count);
2708 if (NT_STATUS_IS_OK(status)) {
2709 goto done;
2711 } else if (strequal(tok, "lmhosts")) {
2712 struct sockaddr_storage *ss_list;
2713 status = resolve_lmhosts_file_as_sockaddr(
2714 get_dyn_LMHOSTSFILE(), name, name_type,
2715 talloc_tos(), &ss_list, return_count);
2716 if (NT_STATUS_IS_OK(status)) {
2717 if (!convert_ss2service(return_iplist,
2718 ss_list,
2719 return_count)) {
2720 status = NT_STATUS_NO_MEMORY;
2722 goto done;
2724 } else if (strequal(tok, "wins")) {
2725 /* don't resolve 1D via WINS */
2726 struct sockaddr_storage *ss_list = NULL;
2727 if (name_type != 0x1D) {
2728 status = resolve_wins(name, name_type,
2729 talloc_tos(),
2730 &ss_list,
2731 return_count);
2732 if (NT_STATUS_IS_OK(status)) {
2733 if (!convert_ss2service(return_iplist,
2734 ss_list,
2735 return_count)) {
2736 status = NT_STATUS_NO_MEMORY;
2738 goto done;
2741 } else if (strequal(tok, "bcast")) {
2742 struct sockaddr_storage *ss_list = NULL;
2743 status = name_resolve_bcast(
2744 name, name_type, talloc_tos(),
2745 &ss_list, return_count);
2746 if (NT_STATUS_IS_OK(status)) {
2747 if (!convert_ss2service(return_iplist,
2748 ss_list,
2749 return_count)) {
2750 status = NT_STATUS_NO_MEMORY;
2752 goto done;
2754 } else {
2755 DEBUG(0,("resolve_name: unknown name switch type %s\n",
2756 tok));
2760 /* All of the resolve_* functions above have returned false. */
2762 TALLOC_FREE(frame);
2763 SAFE_FREE(*return_iplist);
2764 *return_count = 0;
2766 return status;
2768 done:
2770 /* Remove duplicate entries. Some queries, notably #1c (domain
2771 controllers) return the PDC in iplist[0] and then all domain
2772 controllers including the PDC in iplist[1..n]. Iterating over
2773 the iplist when the PDC is down will cause two sets of timeouts. */
2775 *return_count = remove_duplicate_addrs2(*return_iplist, *return_count );
2777 /* Save in name cache */
2778 if ( DEBUGLEVEL >= 100 ) {
2779 for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++) {
2780 char addr[INET6_ADDRSTRLEN];
2781 print_sockaddr(addr, sizeof(addr),
2782 &(*return_iplist)[i].ss);
2783 DEBUG(100, ("Storing name %s of type %d (%s:%d)\n",
2784 name,
2785 name_type,
2786 addr,
2787 (*return_iplist)[i].port));
2791 if (*return_count) {
2792 namecache_store(name, name_type, *return_count, *return_iplist);
2795 /* Display some debugging info */
2797 if ( DEBUGLEVEL >= 10 ) {
2798 DEBUG(10, ("internal_resolve_name: returning %d addresses: ",
2799 *return_count));
2801 for (i = 0; i < *return_count; i++) {
2802 char addr[INET6_ADDRSTRLEN];
2803 print_sockaddr(addr, sizeof(addr),
2804 &(*return_iplist)[i].ss);
2805 DEBUGADD(10, ("%s:%d ",
2806 addr,
2807 (*return_iplist)[i].port));
2809 DEBUG(10, ("\n"));
2812 TALLOC_FREE(frame);
2813 return status;
2816 /********************************************************
2817 Internal interface to resolve a name into one IP address.
2818 Use this function if the string is either an IP address, DNS
2819 or host name or NetBIOS name. This uses the name switch in the
2820 smb.conf to determine the order of name resolution.
2821 *********************************************************/
2823 bool resolve_name(const char *name,
2824 struct sockaddr_storage *return_ss,
2825 int name_type,
2826 bool prefer_ipv4)
2828 struct ip_service *ss_list = NULL;
2829 char *sitename = NULL;
2830 int count = 0;
2831 NTSTATUS status;
2832 TALLOC_CTX *frame = NULL;
2834 if (is_ipaddress(name)) {
2835 return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
2838 frame = talloc_stackframe();
2840 sitename = sitename_fetch(frame, lp_realm()); /* wild guess */
2842 status = internal_resolve_name(name, name_type, sitename,
2843 &ss_list, &count,
2844 lp_name_resolve_order());
2845 if (NT_STATUS_IS_OK(status)) {
2846 int i;
2848 if (prefer_ipv4) {
2849 for (i=0; i<count; i++) {
2850 if (!is_zero_addr(&ss_list[i].ss) &&
2851 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss) &&
2852 (ss_list[i].ss.ss_family == AF_INET)) {
2853 *return_ss = ss_list[i].ss;
2854 SAFE_FREE(ss_list);
2855 TALLOC_FREE(frame);
2856 return True;
2861 /* only return valid addresses for TCP connections */
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 *return_ss = ss_list[i].ss;
2866 SAFE_FREE(ss_list);
2867 TALLOC_FREE(frame);
2868 return True;
2873 SAFE_FREE(ss_list);
2874 TALLOC_FREE(frame);
2875 return False;
2878 /********************************************************
2879 Internal interface to resolve a name into a list of IP addresses.
2880 Use this function if the string is either an IP address, DNS
2881 or host name or NetBIOS name. This uses the name switch in the
2882 smb.conf to determine the order of name resolution.
2883 *********************************************************/
2885 NTSTATUS resolve_name_list(TALLOC_CTX *ctx,
2886 const char *name,
2887 int name_type,
2888 struct sockaddr_storage **return_ss_arr,
2889 unsigned int *p_num_entries)
2891 struct ip_service *ss_list = NULL;
2892 char *sitename = NULL;
2893 int count = 0;
2894 int i;
2895 unsigned int num_entries;
2896 NTSTATUS status;
2898 *p_num_entries = 0;
2899 *return_ss_arr = NULL;
2901 if (is_ipaddress(name)) {
2902 *return_ss_arr = talloc(ctx, struct sockaddr_storage);
2903 if (!*return_ss_arr) {
2904 return NT_STATUS_NO_MEMORY;
2906 if (!interpret_string_addr(*return_ss_arr, name, AI_NUMERICHOST)) {
2907 TALLOC_FREE(*return_ss_arr);
2908 return NT_STATUS_BAD_NETWORK_NAME;
2910 *p_num_entries = 1;
2911 return NT_STATUS_OK;
2914 sitename = sitename_fetch(ctx, lp_realm()); /* wild guess */
2916 status = internal_resolve_name(name, name_type, sitename,
2917 &ss_list, &count,
2918 lp_name_resolve_order());
2919 TALLOC_FREE(sitename);
2921 if (!NT_STATUS_IS_OK(status)) {
2922 return status;
2925 /* only return valid addresses for TCP connections */
2926 for (i=0, num_entries = 0; i<count; i++) {
2927 if (!is_zero_addr(&ss_list[i].ss) &&
2928 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2929 num_entries++;
2932 if (num_entries == 0) {
2933 status = NT_STATUS_BAD_NETWORK_NAME;
2934 goto done;
2937 *return_ss_arr = talloc_array(ctx,
2938 struct sockaddr_storage,
2939 num_entries);
2940 if (!(*return_ss_arr)) {
2941 status = NT_STATUS_NO_MEMORY;
2942 goto done;
2945 for (i=0, num_entries = 0; i<count; i++) {
2946 if (!is_zero_addr(&ss_list[i].ss) &&
2947 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2948 (*return_ss_arr)[num_entries++] = ss_list[i].ss;
2952 status = NT_STATUS_OK;
2953 *p_num_entries = num_entries;
2954 done:
2955 SAFE_FREE(ss_list);
2956 return status;
2959 /********************************************************
2960 Find the IP address of the master browser or DMB for a workgroup.
2961 *********************************************************/
2963 bool find_master_ip(const char *group, struct sockaddr_storage *master_ss)
2965 struct ip_service *ip_list = NULL;
2966 int count = 0;
2967 NTSTATUS status;
2969 if (lp_disable_netbios()) {
2970 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
2971 return false;
2974 status = internal_resolve_name(group, 0x1D, NULL, &ip_list, &count,
2975 lp_name_resolve_order());
2976 if (NT_STATUS_IS_OK(status)) {
2977 *master_ss = ip_list[0].ss;
2978 SAFE_FREE(ip_list);
2979 return true;
2982 status = internal_resolve_name(group, 0x1B, NULL, &ip_list, &count,
2983 lp_name_resolve_order());
2984 if (NT_STATUS_IS_OK(status)) {
2985 *master_ss = ip_list[0].ss;
2986 SAFE_FREE(ip_list);
2987 return true;
2990 SAFE_FREE(ip_list);
2991 return false;
2994 /********************************************************
2995 Get the IP address list of the primary domain controller
2996 for a domain.
2997 *********************************************************/
2999 bool get_pdc_ip(const char *domain, struct sockaddr_storage *pss)
3001 struct ip_service *ip_list = NULL;
3002 int count = 0;
3003 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
3004 static const char *ads_order[] = { "ads", NULL };
3005 /* Look up #1B name */
3007 if (lp_security() == SEC_ADS) {
3008 status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
3009 &count, ads_order);
3012 if (!NT_STATUS_IS_OK(status) || count == 0) {
3013 status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
3014 &count,
3015 lp_name_resolve_order());
3016 if (!NT_STATUS_IS_OK(status)) {
3017 SAFE_FREE(ip_list);
3018 return false;
3022 /* if we get more than 1 IP back we have to assume it is a
3023 multi-homed PDC and not a mess up */
3025 if ( count > 1 ) {
3026 DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count));
3027 sort_service_list(ip_list, count);
3030 *pss = ip_list[0].ss;
3031 SAFE_FREE(ip_list);
3032 return true;
3035 /* Private enum type for lookups. */
3037 enum dc_lookup_type { DC_NORMAL_LOOKUP, DC_ADS_ONLY, DC_KDC_ONLY };
3039 /********************************************************
3040 Get the IP address list of the domain controllers for
3041 a domain.
3042 *********************************************************/
3044 static NTSTATUS get_dc_list(const char *domain,
3045 const char *sitename,
3046 struct ip_service **ip_list,
3047 int *count,
3048 enum dc_lookup_type lookup_type,
3049 bool *ordered)
3051 const char **resolve_order = NULL;
3052 char *saf_servername = NULL;
3053 char *pserver = NULL;
3054 const char *p;
3055 char *port_str = NULL;
3056 int port;
3057 char *name;
3058 size_t num_addresses = 0;
3059 size_t local_count, i;
3060 struct ip_service *return_iplist = NULL;
3061 struct ip_service *auto_ip_list = NULL;
3062 bool done_auto_lookup = false;
3063 int auto_count = 0;
3064 NTSTATUS status;
3065 TALLOC_CTX *ctx = talloc_stackframe();
3066 int auto_name_type = 0x1C;
3068 *ip_list = NULL;
3069 *count = 0;
3071 *ordered = False;
3073 /* if we are restricted to solely using DNS for looking
3074 up a domain controller, make sure that host lookups
3075 are enabled for the 'name resolve order'. If host lookups
3076 are disabled and ads_only is True, then set the string to
3077 NULL. */
3079 resolve_order = lp_name_resolve_order();
3080 if (!resolve_order) {
3081 status = NT_STATUS_NO_MEMORY;
3082 goto out;
3084 if (lookup_type == DC_ADS_ONLY) {
3085 if (str_list_check_ci(resolve_order, "host")) {
3086 static const char *ads_order[] = { "ads", NULL };
3087 resolve_order = ads_order;
3089 /* DNS SRV lookups used by the ads resolver
3090 are already sorted by priority and weight */
3091 *ordered = true;
3092 } else {
3093 /* this is quite bizarre! */
3094 static const char *null_order[] = { "NULL", NULL };
3095 resolve_order = null_order;
3097 } else if (lookup_type == DC_KDC_ONLY) {
3098 static const char *kdc_order[] = { "kdc", NULL };
3099 /* DNS SRV lookups used by the ads/kdc resolver
3100 are already sorted by priority and weight */
3101 *ordered = true;
3102 resolve_order = kdc_order;
3103 auto_name_type = KDC_NAME_TYPE;
3106 /* fetch the server we have affinity for. Add the
3107 'password server' list to a search for our domain controllers */
3109 saf_servername = saf_fetch(ctx, domain);
3111 if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
3112 pserver = talloc_asprintf(ctx, "%s, %s",
3113 saf_servername ? saf_servername : "",
3114 lp_password_server());
3115 } else {
3116 pserver = talloc_asprintf(ctx, "%s, *",
3117 saf_servername ? saf_servername : "");
3120 TALLOC_FREE(saf_servername);
3121 if (!pserver) {
3122 status = NT_STATUS_NO_MEMORY;
3123 goto out;
3126 DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver ));
3129 * if '*' appears in the "password server" list then add
3130 * an auto lookup to the list of manually configured
3131 * DC's. If any DC is listed by name, then the list should be
3132 * considered to be ordered
3135 p = pserver;
3136 while (next_token_talloc(ctx, &p, &name, LIST_SEP)) {
3137 if (!done_auto_lookup && strequal(name, "*")) {
3138 status = internal_resolve_name(domain, auto_name_type,
3139 sitename,
3140 &auto_ip_list,
3141 &auto_count,
3142 resolve_order);
3143 if (NT_STATUS_IS_OK(status)) {
3144 num_addresses += auto_count;
3146 done_auto_lookup = true;
3147 DEBUG(8,("Adding %d DC's from auto lookup\n",
3148 auto_count));
3149 } else {
3150 num_addresses++;
3154 /* if we have no addresses and haven't done the auto lookup, then
3155 just return the list of DC's. Or maybe we just failed. */
3157 if (num_addresses == 0) {
3158 if (done_auto_lookup) {
3159 DEBUG(4,("get_dc_list: no servers found\n"));
3160 status = NT_STATUS_NO_LOGON_SERVERS;
3161 goto out;
3163 status = internal_resolve_name(domain, auto_name_type,
3164 sitename, ip_list,
3165 count, resolve_order);
3166 goto out;
3169 if ((return_iplist = SMB_MALLOC_ARRAY(struct ip_service,
3170 num_addresses)) == NULL) {
3171 DEBUG(3,("get_dc_list: malloc fail !\n"));
3172 status = NT_STATUS_NO_MEMORY;
3173 goto out;
3176 p = pserver;
3177 local_count = 0;
3179 /* fill in the return list now with real IP's */
3181 while ((local_count<num_addresses) &&
3182 next_token_talloc(ctx, &p, &name, LIST_SEP)) {
3183 struct sockaddr_storage name_ss;
3185 /* copy any addresses from the auto lookup */
3187 if (strequal(name, "*")) {
3188 int j;
3189 for (j=0; j<auto_count; j++) {
3190 char addr[INET6_ADDRSTRLEN];
3191 print_sockaddr(addr,
3192 sizeof(addr),
3193 &auto_ip_list[j].ss);
3194 /* Check for and don't copy any
3195 * known bad DC IP's. */
3196 if(!NT_STATUS_IS_OK(check_negative_conn_cache(
3197 domain,
3198 addr))) {
3199 DEBUG(5,("get_dc_list: "
3200 "negative entry %s removed "
3201 "from DC list\n",
3202 addr));
3203 continue;
3205 return_iplist[local_count].ss =
3206 auto_ip_list[j].ss;
3207 return_iplist[local_count].port =
3208 auto_ip_list[j].port;
3209 local_count++;
3211 continue;
3214 /* added support for address:port syntax for ads
3215 * (not that I think anyone will ever run the LDAP
3216 * server in an AD domain on something other than
3217 * port 389
3218 * However, the port should not be used for kerberos
3221 port = (lookup_type == DC_ADS_ONLY) ? LDAP_PORT :
3222 ((lookup_type == DC_KDC_ONLY) ? DEFAULT_KRB5_PORT :
3223 PORT_NONE);
3224 if ((port_str=strchr(name, ':')) != NULL) {
3225 *port_str = '\0';
3226 if (lookup_type != DC_KDC_ONLY) {
3227 port_str++;
3228 port = atoi(port_str);
3232 /* explicit lookup; resolve_name() will
3233 * handle names & IP addresses */
3234 if (resolve_name( name, &name_ss, 0x20, true )) {
3235 char addr[INET6_ADDRSTRLEN];
3236 print_sockaddr(addr,
3237 sizeof(addr),
3238 &name_ss);
3240 /* Check for and don't copy any known bad DC IP's. */
3241 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain,
3242 addr)) ) {
3243 DEBUG(5,("get_dc_list: negative entry %s "
3244 "removed from DC list\n",
3245 name ));
3246 continue;
3249 return_iplist[local_count].ss = name_ss;
3250 return_iplist[local_count].port = port;
3251 local_count++;
3252 *ordered = true;
3256 /* need to remove duplicates in the list if we have any
3257 explicit password servers */
3259 local_count = remove_duplicate_addrs2(return_iplist, local_count );
3261 /* For DC's we always prioritize IPv4 due to W2K3 not
3262 * supporting LDAP, KRB5 or CLDAP over IPv6. */
3264 if (local_count && return_iplist) {
3265 prioritize_ipv4_list(return_iplist, local_count);
3268 if ( DEBUGLEVEL >= 4 ) {
3269 DEBUG(4,("get_dc_list: returning %zu ip addresses "
3270 "in an %sordered list\n",
3271 local_count,
3272 *ordered ? "":"un"));
3273 DEBUG(4,("get_dc_list: "));
3274 for ( i=0; i<local_count; i++ ) {
3275 char addr[INET6_ADDRSTRLEN];
3276 print_sockaddr(addr,
3277 sizeof(addr),
3278 &return_iplist[i].ss);
3279 DEBUGADD(4,("%s:%d ", addr, return_iplist[i].port ));
3281 DEBUGADD(4,("\n"));
3284 *ip_list = return_iplist;
3285 *count = local_count;
3287 status = ( *count != 0 ? NT_STATUS_OK : NT_STATUS_NO_LOGON_SERVERS );
3289 out:
3291 if (!NT_STATUS_IS_OK(status)) {
3292 SAFE_FREE(return_iplist);
3293 *ip_list = NULL;
3294 *count = 0;
3297 SAFE_FREE(auto_ip_list);
3298 TALLOC_FREE(ctx);
3299 return status;
3302 /*********************************************************************
3303 Small wrapper function to get the DC list and sort it if neccessary.
3304 *********************************************************************/
3306 NTSTATUS get_sorted_dc_list( const char *domain,
3307 const char *sitename,
3308 struct ip_service **ip_list,
3309 int *count,
3310 bool ads_only )
3312 bool ordered = false;
3313 NTSTATUS status;
3314 enum dc_lookup_type lookup_type = DC_NORMAL_LOOKUP;
3316 *ip_list = NULL;
3317 *count = 0;
3319 DEBUG(8,("get_sorted_dc_list: attempting lookup "
3320 "for name %s (sitename %s)\n",
3321 domain,
3322 sitename ? sitename : "NULL"));
3324 if (ads_only) {
3325 lookup_type = DC_ADS_ONLY;
3328 status = get_dc_list(domain, sitename, ip_list,
3329 count, lookup_type, &ordered);
3330 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_LOGON_SERVERS)
3331 && sitename) {
3332 DEBUG(3,("get_sorted_dc_list: no server for name %s available"
3333 " in site %s, fallback to all servers\n",
3334 domain, sitename));
3335 status = get_dc_list(domain, NULL, ip_list,
3336 count, lookup_type, &ordered);
3339 if (!NT_STATUS_IS_OK(status)) {
3340 SAFE_FREE(*ip_list);
3341 *count = 0;
3342 return status;
3345 /* only sort if we don't already have an ordered list */
3346 if (!ordered) {
3347 sort_service_list(*ip_list, *count);
3350 return NT_STATUS_OK;
3353 /*********************************************************************
3354 Get the KDC list - re-use all the logic in get_dc_list.
3355 *********************************************************************/
3357 NTSTATUS get_kdc_list( const char *realm,
3358 const char *sitename,
3359 struct ip_service **ip_list,
3360 int *count)
3362 bool ordered;
3363 NTSTATUS status;
3365 *count = 0;
3366 *ip_list = NULL;
3368 status = get_dc_list(realm, sitename, ip_list,
3369 count, DC_KDC_ONLY, &ordered);
3371 if (!NT_STATUS_IS_OK(status)) {
3372 SAFE_FREE(*ip_list);
3373 *count = 0;
3374 return status;
3377 /* only sort if we don't already have an ordered list */
3378 if ( !ordered ) {
3379 sort_service_list(*ip_list, *count);
3382 return NT_STATUS_OK;