namequery: correctly merge kdc ip address list
[Samba.git] / source3 / libsmb / namequery.c
blob7ef555200750a0acb665455ccede3b75604cb10a
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 "../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"
30 #include "libads/kerberos_proto.h"
32 /* nmbd.c sets this to True. */
33 bool global_in_nmbd = False;
35 /****************************
36 * SERVER AFFINITY ROUTINES *
37 ****************************/
39 /* Server affinity is the concept of preferring the last domain
40 controller with whom you had a successful conversation */
42 /****************************************************************************
43 ****************************************************************************/
44 #define SAFKEY_FMT "SAF/DOMAIN/%s"
45 #define SAF_TTL 900
46 #define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
47 #define SAFJOIN_TTL 3600
49 static char *saf_key(TALLOC_CTX *mem_ctx, const char *domain)
51 return talloc_asprintf_strupper_m(mem_ctx, SAFKEY_FMT, domain);
54 static char *saf_join_key(TALLOC_CTX *mem_ctx, const char *domain)
56 return talloc_asprintf_strupper_m(mem_ctx, SAFJOINKEY_FMT, domain);
59 /****************************************************************************
60 ****************************************************************************/
62 bool saf_store( const char *domain, const char *servername )
64 char *key;
65 time_t expire;
66 bool ret = False;
68 if ( !domain || !servername ) {
69 DEBUG(2,("saf_store: "
70 "Refusing to store empty domain or servername!\n"));
71 return False;
74 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
75 DEBUG(0,("saf_store: "
76 "refusing to store 0 length domain or servername!\n"));
77 return False;
80 key = saf_key(talloc_tos(), domain);
81 if (key == NULL) {
82 DEBUG(1, ("saf_key() failed\n"));
83 return false;
85 expire = time( NULL ) + lp_parm_int(-1, "saf","ttl", SAF_TTL);
87 DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
88 domain, servername, (unsigned int)expire ));
90 ret = gencache_set( key, servername, expire );
92 TALLOC_FREE( key );
94 return ret;
97 bool saf_join_store( const char *domain, const char *servername )
99 char *key;
100 time_t expire;
101 bool ret = False;
103 if ( !domain || !servername ) {
104 DEBUG(2,("saf_join_store: Refusing to store empty domain or servername!\n"));
105 return False;
108 if ( (strlen(domain) == 0) || (strlen(servername) == 0) ) {
109 DEBUG(0,("saf_join_store: refusing to store 0 length domain or servername!\n"));
110 return False;
113 key = saf_join_key(talloc_tos(), domain);
114 if (key == NULL) {
115 DEBUG(1, ("saf_join_key() failed\n"));
116 return false;
118 expire = time( NULL ) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL);
120 DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
121 domain, servername, (unsigned int)expire ));
123 ret = gencache_set( key, servername, expire );
125 TALLOC_FREE( key );
127 return ret;
130 bool saf_delete( const char *domain )
132 char *key;
133 bool ret = False;
135 if ( !domain ) {
136 DEBUG(2,("saf_delete: Refusing to delete empty domain\n"));
137 return False;
140 key = saf_join_key(talloc_tos(), domain);
141 if (key == NULL) {
142 DEBUG(1, ("saf_join_key() failed\n"));
143 return false;
145 ret = gencache_del(key);
146 TALLOC_FREE(key);
148 if (ret) {
149 DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain ));
152 key = saf_key(talloc_tos(), domain);
153 if (key == NULL) {
154 DEBUG(1, ("saf_key() failed\n"));
155 return false;
157 ret = gencache_del(key);
158 TALLOC_FREE(key);
160 if (ret) {
161 DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
164 return ret;
167 /****************************************************************************
168 ****************************************************************************/
170 char *saf_fetch(TALLOC_CTX *mem_ctx, const char *domain )
172 char *server = NULL;
173 time_t timeout;
174 bool ret = False;
175 char *key = NULL;
177 if ( !domain || strlen(domain) == 0) {
178 DEBUG(2,("saf_fetch: Empty domain name!\n"));
179 return NULL;
182 key = saf_join_key(talloc_tos(), domain);
183 if (key == NULL) {
184 DEBUG(1, ("saf_join_key() failed\n"));
185 return NULL;
188 ret = gencache_get( key, mem_ctx, &server, &timeout );
190 TALLOC_FREE( key );
192 if ( ret ) {
193 DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
194 server, domain ));
195 return server;
198 key = saf_key(talloc_tos(), domain);
199 if (key == NULL) {
200 DEBUG(1, ("saf_key() failed\n"));
201 return NULL;
204 ret = gencache_get( key, mem_ctx, &server, &timeout );
206 TALLOC_FREE( key );
208 if ( !ret ) {
209 DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
210 domain ));
211 } else {
212 DEBUG(5,("saf_fetch: Returning \"%s\" for \"%s\" domain\n",
213 server, domain ));
216 return server;
219 static void set_socket_addr_v4(struct sockaddr_storage *addr)
221 if (!interpret_string_addr(addr, lp_nbt_client_socket_address(),
222 AI_NUMERICHOST|AI_PASSIVE)) {
223 zero_sockaddr(addr);
225 if (addr->ss_family != AF_INET) {
226 zero_sockaddr(addr);
230 static struct in_addr my_socket_addr_v4(void)
232 struct sockaddr_storage my_addr;
233 struct sockaddr_in *in_addr = (struct sockaddr_in *)((char *)&my_addr);
235 set_socket_addr_v4(&my_addr);
236 return in_addr->sin_addr;
239 /****************************************************************************
240 Generate a random trn_id.
241 ****************************************************************************/
243 static int generate_trn_id(void)
245 uint16_t id;
247 generate_random_buffer((uint8_t *)&id, sizeof(id));
249 return id % (unsigned)0x7FFF;
252 /****************************************************************************
253 Parse a node status response into an array of structures.
254 ****************************************************************************/
256 static struct node_status *parse_node_status(TALLOC_CTX *mem_ctx, char *p,
257 int *num_names,
258 struct node_status_extra *extra)
260 struct node_status *ret;
261 int i;
263 *num_names = CVAL(p,0);
265 if (*num_names == 0)
266 return NULL;
268 ret = talloc_array(mem_ctx, struct node_status,*num_names);
269 if (!ret)
270 return NULL;
272 p++;
273 for (i=0;i< *num_names;i++) {
274 StrnCpy(ret[i].name,p,15);
275 trim_char(ret[i].name,'\0',' ');
276 ret[i].type = CVAL(p,15);
277 ret[i].flags = p[16];
278 p += 18;
279 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
280 ret[i].type, ret[i].flags));
283 * Also, pick up the MAC address ...
285 if (extra) {
286 memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
288 return ret;
291 struct sock_packet_read_state {
292 struct tevent_context *ev;
293 enum packet_type type;
294 int trn_id;
296 struct nb_packet_reader *reader;
297 struct tevent_req *reader_req;
299 struct tdgram_context *sock;
300 struct tevent_req *socket_req;
301 uint8_t *buf;
302 struct tsocket_address *addr;
304 bool (*validator)(struct packet_struct *p,
305 void *private_data);
306 void *private_data;
308 struct packet_struct *packet;
311 static int sock_packet_read_state_destructor(struct sock_packet_read_state *s);
312 static void sock_packet_read_got_packet(struct tevent_req *subreq);
313 static void sock_packet_read_got_socket(struct tevent_req *subreq);
315 static struct tevent_req *sock_packet_read_send(
316 TALLOC_CTX *mem_ctx,
317 struct tevent_context *ev,
318 struct tdgram_context *sock,
319 struct nb_packet_reader *reader,
320 enum packet_type type,
321 int trn_id,
322 bool (*validator)(struct packet_struct *p, void *private_data),
323 void *private_data)
325 struct tevent_req *req;
326 struct sock_packet_read_state *state;
328 req = tevent_req_create(mem_ctx, &state,
329 struct sock_packet_read_state);
330 if (req == NULL) {
331 return NULL;
333 talloc_set_destructor(state, sock_packet_read_state_destructor);
334 state->ev = ev;
335 state->reader = reader;
336 state->sock = sock;
337 state->type = type;
338 state->trn_id = trn_id;
339 state->validator = validator;
340 state->private_data = private_data;
342 if (reader != NULL) {
343 state->reader_req = nb_packet_read_send(state, ev, reader);
344 if (tevent_req_nomem(state->reader_req, req)) {
345 return tevent_req_post(req, ev);
347 tevent_req_set_callback(
348 state->reader_req, sock_packet_read_got_packet, req);
351 state->socket_req = tdgram_recvfrom_send(state, ev, state->sock);
352 if (tevent_req_nomem(state->socket_req, req)) {
353 return tevent_req_post(req, ev);
355 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
356 req);
358 return req;
361 static int sock_packet_read_state_destructor(struct sock_packet_read_state *s)
363 if (s->packet != NULL) {
364 free_packet(s->packet);
365 s->packet = NULL;
367 return 0;
370 static void sock_packet_read_got_packet(struct tevent_req *subreq)
372 struct tevent_req *req = tevent_req_callback_data(
373 subreq, struct tevent_req);
374 struct sock_packet_read_state *state = tevent_req_data(
375 req, struct sock_packet_read_state);
376 NTSTATUS status;
378 status = nb_packet_read_recv(subreq, &state->packet);
380 TALLOC_FREE(state->reader_req);
382 if (!NT_STATUS_IS_OK(status)) {
383 if (state->socket_req != NULL) {
385 * Still waiting for socket
387 return;
390 * Both socket and packet reader failed
392 tevent_req_nterror(req, status);
393 return;
396 if ((state->validator != NULL) &&
397 !state->validator(state->packet, state->private_data)) {
398 DEBUG(10, ("validator failed\n"));
400 free_packet(state->packet);
401 state->packet = NULL;
403 state->reader_req = nb_packet_read_send(state, state->ev,
404 state->reader);
405 if (tevent_req_nomem(state->reader_req, req)) {
406 return;
408 tevent_req_set_callback(
409 state->reader_req, sock_packet_read_got_packet, req);
410 return;
413 TALLOC_FREE(state->socket_req);
414 tevent_req_done(req);
417 static void sock_packet_read_got_socket(struct tevent_req *subreq)
419 struct tevent_req *req = tevent_req_callback_data(
420 subreq, struct tevent_req);
421 struct sock_packet_read_state *state = tevent_req_data(
422 req, struct sock_packet_read_state);
423 union {
424 struct sockaddr sa;
425 struct sockaddr_in sin;
426 } addr;
427 ssize_t ret;
428 ssize_t received;
429 int err;
430 bool ok;
432 received = tdgram_recvfrom_recv(subreq, &err, state,
433 &state->buf, &state->addr);
435 TALLOC_FREE(state->socket_req);
437 if (received == -1) {
438 if (state->reader_req != NULL) {
440 * Still waiting for reader
442 return;
445 * Both socket and reader failed
447 tevent_req_nterror(req, map_nt_error_from_unix(err));
448 return;
450 ok = tsocket_address_is_inet(state->addr, "ipv4");
451 if (!ok) {
452 goto retry;
454 ret = tsocket_address_bsd_sockaddr(state->addr,
455 &addr.sa,
456 sizeof(addr.sin));
457 if (ret == -1) {
458 tevent_req_nterror(req, map_nt_error_from_unix(errno));
459 return;
462 state->packet = parse_packet((char *)state->buf, received, state->type,
463 addr.sin.sin_addr, addr.sin.sin_port);
464 if (state->packet == NULL) {
465 DEBUG(10, ("parse_packet failed\n"));
466 goto retry;
468 if ((state->trn_id != -1) &&
469 (state->trn_id != packet_trn_id(state->packet))) {
470 DEBUG(10, ("Expected transaction id %d, got %d\n",
471 state->trn_id, packet_trn_id(state->packet)));
472 goto retry;
475 if ((state->validator != NULL) &&
476 !state->validator(state->packet, state->private_data)) {
477 DEBUG(10, ("validator failed\n"));
478 goto retry;
481 tevent_req_done(req);
482 return;
484 retry:
485 if (state->packet != NULL) {
486 free_packet(state->packet);
487 state->packet = NULL;
489 TALLOC_FREE(state->buf);
490 TALLOC_FREE(state->addr);
492 state->socket_req = tdgram_recvfrom_send(state, state->ev, state->sock);
493 if (tevent_req_nomem(state->socket_req, req)) {
494 return;
496 tevent_req_set_callback(state->socket_req, sock_packet_read_got_socket,
497 req);
500 static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
501 struct packet_struct **ppacket)
503 struct sock_packet_read_state *state = tevent_req_data(
504 req, struct sock_packet_read_state);
505 NTSTATUS status;
507 if (tevent_req_is_nterror(req, &status)) {
508 return status;
510 *ppacket = state->packet;
511 state->packet = NULL;
512 return NT_STATUS_OK;
515 struct nb_trans_state {
516 struct tevent_context *ev;
517 struct tdgram_context *sock;
518 struct nb_packet_reader *reader;
520 struct tsocket_address *src_addr;
521 struct tsocket_address *dst_addr;
522 uint8_t *buf;
523 size_t buflen;
524 enum packet_type type;
525 int trn_id;
527 bool (*validator)(struct packet_struct *p,
528 void *private_data);
529 void *private_data;
531 struct packet_struct *packet;
534 static int nb_trans_state_destructor(struct nb_trans_state *s);
535 static void nb_trans_got_reader(struct tevent_req *subreq);
536 static void nb_trans_done(struct tevent_req *subreq);
537 static void nb_trans_sent(struct tevent_req *subreq);
538 static void nb_trans_send_next(struct tevent_req *subreq);
540 static struct tevent_req *nb_trans_send(
541 TALLOC_CTX *mem_ctx,
542 struct tevent_context *ev,
543 const struct sockaddr_storage *_my_addr,
544 const struct sockaddr_storage *_dst_addr,
545 bool bcast,
546 uint8_t *buf, size_t buflen,
547 enum packet_type type, int trn_id,
548 bool (*validator)(struct packet_struct *p,
549 void *private_data),
550 void *private_data)
552 const struct sockaddr *my_addr =
553 discard_const_p(const struct sockaddr, _my_addr);
554 size_t my_addr_len = sizeof(*_my_addr);
555 const struct sockaddr *dst_addr =
556 discard_const_p(const struct sockaddr, _dst_addr);
557 size_t dst_addr_len = sizeof(*_dst_addr);
558 struct tevent_req *req, *subreq;
559 struct nb_trans_state *state;
560 int ret;
562 req = tevent_req_create(mem_ctx, &state, struct nb_trans_state);
563 if (req == NULL) {
564 return NULL;
566 talloc_set_destructor(state, nb_trans_state_destructor);
567 state->ev = ev;
568 state->buf = buf;
569 state->buflen = buflen;
570 state->type = type;
571 state->trn_id = trn_id;
572 state->validator = validator;
573 state->private_data = private_data;
575 ret = tsocket_address_bsd_from_sockaddr(state,
576 my_addr, my_addr_len,
577 &state->src_addr);
578 if (ret == -1) {
579 tevent_req_nterror(req, map_nt_error_from_unix(errno));
580 return tevent_req_post(req, ev);
583 ret = tsocket_address_bsd_from_sockaddr(state,
584 dst_addr, dst_addr_len,
585 &state->dst_addr);
586 if (ret == -1) {
587 tevent_req_nterror(req, map_nt_error_from_unix(errno));
588 return tevent_req_post(req, ev);
591 ret = tdgram_inet_udp_broadcast_socket(state->src_addr, state,
592 &state->sock);
593 if (ret == -1) {
594 tevent_req_nterror(req, map_nt_error_from_unix(errno));
595 return tevent_req_post(req, ev);
598 subreq = nb_packet_reader_send(state, ev, type, state->trn_id, NULL);
599 if (tevent_req_nomem(subreq, req)) {
600 return tevent_req_post(req, ev);
602 tevent_req_set_callback(subreq, nb_trans_got_reader, req);
603 return req;
606 static int nb_trans_state_destructor(struct nb_trans_state *s)
608 if (s->packet != NULL) {
609 free_packet(s->packet);
610 s->packet = NULL;
612 return 0;
615 static void nb_trans_got_reader(struct tevent_req *subreq)
617 struct tevent_req *req = tevent_req_callback_data(
618 subreq, struct tevent_req);
619 struct nb_trans_state *state = tevent_req_data(
620 req, struct nb_trans_state);
621 NTSTATUS status;
623 status = nb_packet_reader_recv(subreq, state, &state->reader);
624 TALLOC_FREE(subreq);
626 if (!NT_STATUS_IS_OK(status)) {
627 DEBUG(10, ("nmbd not around\n"));
628 state->reader = NULL;
631 subreq = sock_packet_read_send(
632 state, state->ev, state->sock,
633 state->reader, state->type, state->trn_id,
634 state->validator, state->private_data);
635 if (tevent_req_nomem(subreq, req)) {
636 return;
638 tevent_req_set_callback(subreq, nb_trans_done, req);
640 subreq = tdgram_sendto_send(state, state->ev,
641 state->sock,
642 state->buf, state->buflen,
643 state->dst_addr);
644 if (tevent_req_nomem(subreq, req)) {
645 return;
647 tevent_req_set_callback(subreq, nb_trans_sent, req);
650 static void nb_trans_sent(struct tevent_req *subreq)
652 struct tevent_req *req = tevent_req_callback_data(
653 subreq, struct tevent_req);
654 struct nb_trans_state *state = tevent_req_data(
655 req, struct nb_trans_state);
656 ssize_t sent;
657 int err;
659 sent = tdgram_sendto_recv(subreq, &err);
660 TALLOC_FREE(subreq);
661 if (sent == -1) {
662 DEBUG(10, ("sendto failed: %s\n", strerror(err)));
663 tevent_req_nterror(req, map_nt_error_from_unix(err));
664 return;
666 subreq = tevent_wakeup_send(state, state->ev,
667 timeval_current_ofs(1, 0));
668 if (tevent_req_nomem(subreq, req)) {
669 return;
671 tevent_req_set_callback(subreq, nb_trans_send_next, req);
674 static void nb_trans_send_next(struct tevent_req *subreq)
676 struct tevent_req *req = tevent_req_callback_data(
677 subreq, struct tevent_req);
678 struct nb_trans_state *state = tevent_req_data(
679 req, struct nb_trans_state);
680 bool ret;
682 ret = tevent_wakeup_recv(subreq);
683 TALLOC_FREE(subreq);
684 if (!ret) {
685 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
686 return;
688 subreq = tdgram_sendto_send(state, state->ev,
689 state->sock,
690 state->buf, state->buflen,
691 state->dst_addr);
692 if (tevent_req_nomem(subreq, req)) {
693 return;
695 tevent_req_set_callback(subreq, nb_trans_sent, req);
698 static void nb_trans_done(struct tevent_req *subreq)
700 struct tevent_req *req = tevent_req_callback_data(
701 subreq, struct tevent_req);
702 struct nb_trans_state *state = tevent_req_data(
703 req, struct nb_trans_state);
704 NTSTATUS status;
706 status = sock_packet_read_recv(subreq, &state->packet);
707 TALLOC_FREE(subreq);
708 if (tevent_req_nterror(req, status)) {
709 return;
711 tevent_req_done(req);
714 static NTSTATUS nb_trans_recv(struct tevent_req *req,
715 struct packet_struct **ppacket)
717 struct nb_trans_state *state = tevent_req_data(
718 req, struct nb_trans_state);
719 NTSTATUS status;
721 if (tevent_req_is_nterror(req, &status)) {
722 return status;
724 *ppacket = state->packet;
725 state->packet = NULL;
726 return NT_STATUS_OK;
729 /****************************************************************************
730 Do a NBT node status query on an open socket and return an array of
731 structures holding the returned names or NULL if the query failed.
732 **************************************************************************/
734 struct node_status_query_state {
735 struct sockaddr_storage my_addr;
736 struct sockaddr_storage addr;
737 uint8_t buf[1024];
738 ssize_t buflen;
739 struct packet_struct *packet;
742 static int node_status_query_state_destructor(
743 struct node_status_query_state *s);
744 static bool node_status_query_validator(struct packet_struct *p,
745 void *private_data);
746 static void node_status_query_done(struct tevent_req *subreq);
748 struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
749 struct tevent_context *ev,
750 struct nmb_name *name,
751 const struct sockaddr_storage *addr)
753 struct tevent_req *req, *subreq;
754 struct node_status_query_state *state;
755 struct packet_struct p;
756 struct nmb_packet *nmb = &p.packet.nmb;
757 struct sockaddr_in *in_addr;
759 req = tevent_req_create(mem_ctx, &state,
760 struct node_status_query_state);
761 if (req == NULL) {
762 return NULL;
764 talloc_set_destructor(state, node_status_query_state_destructor);
766 if (addr->ss_family != AF_INET) {
767 /* Can't do node status to IPv6 */
768 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
769 return tevent_req_post(req, ev);
772 state->addr = *addr;
773 in_addr = (struct sockaddr_in *)(void *)&state->addr;
774 in_addr->sin_port = htons(NMB_PORT);
776 set_socket_addr_v4(&state->my_addr);
778 ZERO_STRUCT(p);
779 nmb->header.name_trn_id = generate_trn_id();
780 nmb->header.opcode = 0;
781 nmb->header.response = false;
782 nmb->header.nm_flags.bcast = false;
783 nmb->header.nm_flags.recursion_available = false;
784 nmb->header.nm_flags.recursion_desired = false;
785 nmb->header.nm_flags.trunc = false;
786 nmb->header.nm_flags.authoritative = false;
787 nmb->header.rcode = 0;
788 nmb->header.qdcount = 1;
789 nmb->header.ancount = 0;
790 nmb->header.nscount = 0;
791 nmb->header.arcount = 0;
792 nmb->question.question_name = *name;
793 nmb->question.question_type = 0x21;
794 nmb->question.question_class = 0x1;
796 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
797 &p);
798 if (state->buflen == 0) {
799 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
800 DEBUG(10, ("build_packet failed\n"));
801 return tevent_req_post(req, ev);
804 subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, false,
805 state->buf, state->buflen,
806 NMB_PACKET, nmb->header.name_trn_id,
807 node_status_query_validator, NULL);
808 if (tevent_req_nomem(subreq, req)) {
809 DEBUG(10, ("nb_trans_send failed\n"));
810 return tevent_req_post(req, ev);
812 if (!tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0))) {
813 return tevent_req_post(req, ev);
815 tevent_req_set_callback(subreq, node_status_query_done, req);
816 return req;
819 static bool node_status_query_validator(struct packet_struct *p,
820 void *private_data)
822 struct nmb_packet *nmb = &p->packet.nmb;
823 debug_nmb_packet(p);
825 if (nmb->header.opcode != 0 ||
826 nmb->header.nm_flags.bcast ||
827 nmb->header.rcode ||
828 !nmb->header.ancount ||
829 nmb->answers->rr_type != 0x21) {
831 * XXXX what do we do with this? could be a redirect,
832 * but we'll discard it for the moment
834 return false;
836 return true;
839 static int node_status_query_state_destructor(
840 struct node_status_query_state *s)
842 if (s->packet != NULL) {
843 free_packet(s->packet);
844 s->packet = NULL;
846 return 0;
849 static void node_status_query_done(struct tevent_req *subreq)
851 struct tevent_req *req = tevent_req_callback_data(
852 subreq, struct tevent_req);
853 struct node_status_query_state *state = tevent_req_data(
854 req, struct node_status_query_state);
855 NTSTATUS status;
857 status = nb_trans_recv(subreq, &state->packet);
858 TALLOC_FREE(subreq);
859 if (tevent_req_nterror(req, status)) {
860 return;
862 tevent_req_done(req);
865 NTSTATUS node_status_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
866 struct node_status **pnode_status,
867 int *pnum_names,
868 struct node_status_extra *extra)
870 struct node_status_query_state *state = tevent_req_data(
871 req, struct node_status_query_state);
872 struct node_status *node_status;
873 int num_names;
874 NTSTATUS status;
876 if (tevent_req_is_nterror(req, &status)) {
877 return status;
879 node_status = parse_node_status(
880 mem_ctx, &state->packet->packet.nmb.answers->rdata[0],
881 &num_names, extra);
882 if (node_status == NULL) {
883 return NT_STATUS_NO_MEMORY;
885 *pnode_status = node_status;
886 *pnum_names = num_names;
887 return NT_STATUS_OK;
890 NTSTATUS node_status_query(TALLOC_CTX *mem_ctx, struct nmb_name *name,
891 const struct sockaddr_storage *addr,
892 struct node_status **pnode_status,
893 int *pnum_names,
894 struct node_status_extra *extra)
896 TALLOC_CTX *frame = talloc_stackframe();
897 struct tevent_context *ev;
898 struct tevent_req *req;
899 NTSTATUS status = NT_STATUS_NO_MEMORY;
901 ev = samba_tevent_context_init(frame);
902 if (ev == NULL) {
903 goto fail;
905 req = node_status_query_send(ev, ev, name, addr);
906 if (req == NULL) {
907 goto fail;
909 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
910 goto fail;
912 status = node_status_query_recv(req, mem_ctx, pnode_status,
913 pnum_names, extra);
914 fail:
915 TALLOC_FREE(frame);
916 return status;
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, 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 set_socket_addr_v4(&ss);
962 /* W2K PDC's seem not to respond to '*'#0. JRA */
963 make_nmb_name(&nname, q_name, q_type);
964 status = node_status_query(talloc_tos(), &nname, to_ss,
965 &addrs, &count, NULL);
966 if (!NT_STATUS_IS_OK(status)) {
967 goto done;
970 for (i=0;i<count;i++) {
971 /* Find first one of the requested type that's not a GROUP. */
972 if (addrs[i].type == type && ! (addrs[i].flags & 0x80))
973 break;
975 if (i == count)
976 goto done;
978 pull_ascii_nstring(name, sizeof(fstring), addrs[i].name);
980 /* Store the result in the cache. */
981 /* but don't store an entry for 0x1c names here. Here we have
982 a single host and DOMAIN<0x1c> names should be a list of hosts */
984 if ( q_type != 0x1c ) {
985 namecache_status_store(q_name, q_type, type, to_ss, name);
988 result = true;
990 done:
991 TALLOC_FREE(addrs);
993 DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
995 if (result)
996 DEBUGADD(10, (", name %s ip address is %s", name, addr));
998 DEBUG(10, ("\n"));
1000 return result;
1004 comparison function used by sort_addr_list
1007 static int addr_compare(const struct sockaddr_storage *ss1,
1008 const struct sockaddr_storage *ss2)
1010 int max_bits1=0, max_bits2=0;
1011 int num_interfaces = iface_count();
1012 int i;
1014 /* Sort IPv4 addresses first. */
1015 if (ss1->ss_family != ss2->ss_family) {
1016 if (ss2->ss_family == AF_INET) {
1017 return 1;
1018 } else {
1019 return -1;
1023 /* Here we know both addresses are of the same
1024 * family. */
1026 for (i=0;i<num_interfaces;i++) {
1027 const struct sockaddr_storage *pss = iface_n_bcast(i);
1028 const unsigned char *p_ss1 = NULL;
1029 const unsigned char *p_ss2 = NULL;
1030 const unsigned char *p_if = NULL;
1031 size_t len = 0;
1032 int bits1, bits2;
1034 if (pss->ss_family != ss1->ss_family) {
1035 /* Ignore interfaces of the wrong type. */
1036 continue;
1038 if (pss->ss_family == AF_INET) {
1039 p_if = (const unsigned char *)
1040 &((const struct sockaddr_in *)pss)->sin_addr;
1041 p_ss1 = (const unsigned char *)
1042 &((const struct sockaddr_in *)ss1)->sin_addr;
1043 p_ss2 = (const unsigned char *)
1044 &((const struct sockaddr_in *)ss2)->sin_addr;
1045 len = 4;
1047 #if defined(HAVE_IPV6)
1048 if (pss->ss_family == AF_INET6) {
1049 p_if = (const unsigned char *)
1050 &((const struct sockaddr_in6 *)pss)->sin6_addr;
1051 p_ss1 = (const unsigned char *)
1052 &((const struct sockaddr_in6 *)ss1)->sin6_addr;
1053 p_ss2 = (const unsigned char *)
1054 &((const struct sockaddr_in6 *)ss2)->sin6_addr;
1055 len = 16;
1057 #endif
1058 if (!p_ss1 || !p_ss2 || !p_if || len == 0) {
1059 continue;
1061 bits1 = matching_len_bits(p_ss1, p_if, len);
1062 bits2 = matching_len_bits(p_ss2, p_if, len);
1063 max_bits1 = MAX(bits1, max_bits1);
1064 max_bits2 = MAX(bits2, max_bits2);
1067 /* Bias towards directly reachable IPs */
1068 if (iface_local((const struct sockaddr *)ss1)) {
1069 if (ss1->ss_family == AF_INET) {
1070 max_bits1 += 32;
1071 } else {
1072 max_bits1 += 128;
1075 if (iface_local((const struct sockaddr *)ss2)) {
1076 if (ss2->ss_family == AF_INET) {
1077 max_bits2 += 32;
1078 } else {
1079 max_bits2 += 128;
1082 return max_bits2 - max_bits1;
1085 /*******************************************************************
1086 compare 2 ldap IPs by nearness to our interfaces - used in qsort
1087 *******************************************************************/
1089 static int ip_service_compare(struct ip_service *ss1, struct ip_service *ss2)
1091 int result;
1093 if ((result = addr_compare(&ss1->ss, &ss2->ss)) != 0) {
1094 return result;
1097 if (ss1->port > ss2->port) {
1098 return 1;
1101 if (ss1->port < ss2->port) {
1102 return -1;
1105 return 0;
1109 sort an IP list so that names that are close to one of our interfaces
1110 are at the top. This prevents the problem where a WINS server returns an IP
1111 that is not reachable from our subnet as the first match
1114 static void sort_addr_list(struct sockaddr_storage *sslist, int count)
1116 if (count <= 1) {
1117 return;
1120 TYPESAFE_QSORT(sslist, count, addr_compare);
1123 static void sort_service_list(struct ip_service *servlist, int count)
1125 if (count <= 1) {
1126 return;
1129 TYPESAFE_QSORT(servlist, count, ip_service_compare);
1132 /**********************************************************************
1133 Remove any duplicate address/port pairs in the list
1134 *********************************************************************/
1136 int remove_duplicate_addrs2(struct ip_service *iplist, int count )
1138 int i, j;
1140 DEBUG(10,("remove_duplicate_addrs2: "
1141 "looking for duplicate address/port pairs\n"));
1143 /* One loop to set duplicates to a zero addr. */
1144 for ( i=0; i<count; i++ ) {
1145 if ( is_zero_addr(&iplist[i].ss)) {
1146 continue;
1149 for ( j=i+1; j<count; j++ ) {
1150 if (sockaddr_equal((struct sockaddr *)(void *)&iplist[i].ss,
1151 (struct sockaddr *)(void *)&iplist[j].ss) &&
1152 iplist[i].port == iplist[j].port) {
1153 zero_sockaddr(&iplist[j].ss);
1158 /* Now remove any addresses set to zero above. */
1159 for (i = 0; i < count; i++) {
1160 while (i < count &&
1161 is_zero_addr(&iplist[i].ss)) {
1162 if (count-i-1>0) {
1163 memmove(&iplist[i],
1164 &iplist[i+1],
1165 (count-i-1)*sizeof(struct ip_service));
1167 count--;
1171 return count;
1174 static bool prioritize_ipv4_list(struct ip_service *iplist, int count)
1176 TALLOC_CTX *frame = talloc_stackframe();
1177 struct ip_service *iplist_new = talloc_array(frame, struct ip_service, count);
1178 int i, j;
1180 if (iplist_new == NULL) {
1181 TALLOC_FREE(frame);
1182 return false;
1185 j = 0;
1187 /* Copy IPv4 first. */
1188 for (i = 0; i < count; i++) {
1189 if (iplist[i].ss.ss_family == AF_INET) {
1190 iplist_new[j++] = iplist[i];
1194 /* Copy IPv6. */
1195 for (i = 0; i < count; i++) {
1196 if (iplist[i].ss.ss_family != AF_INET) {
1197 iplist_new[j++] = iplist[i];
1201 memcpy(iplist, iplist_new, sizeof(struct ip_service)*count);
1202 TALLOC_FREE(frame);
1203 return true;
1206 /****************************************************************************
1207 Do a netbios name query to find someones IP.
1208 Returns an array of IP addresses or NULL if none.
1209 *count will be set to the number of addresses returned.
1210 *timed_out is set if we failed by timing out
1211 ****************************************************************************/
1213 struct name_query_state {
1214 struct sockaddr_storage my_addr;
1215 struct sockaddr_storage addr;
1216 bool bcast;
1219 uint8_t buf[1024];
1220 ssize_t buflen;
1222 NTSTATUS validate_error;
1223 uint8_t flags;
1225 struct sockaddr_storage *addrs;
1226 int num_addrs;
1229 static bool name_query_validator(struct packet_struct *p, void *private_data);
1230 static void name_query_done(struct tevent_req *subreq);
1232 struct tevent_req *name_query_send(TALLOC_CTX *mem_ctx,
1233 struct tevent_context *ev,
1234 const char *name, int name_type,
1235 bool bcast, bool recurse,
1236 const struct sockaddr_storage *addr)
1238 struct tevent_req *req, *subreq;
1239 struct name_query_state *state;
1240 struct packet_struct p;
1241 struct nmb_packet *nmb = &p.packet.nmb;
1242 struct sockaddr_in *in_addr;
1244 req = tevent_req_create(mem_ctx, &state, struct name_query_state);
1245 if (req == NULL) {
1246 return NULL;
1248 state->bcast = bcast;
1250 if (addr->ss_family != AF_INET) {
1251 /* Can't do node status to IPv6 */
1252 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
1253 return tevent_req_post(req, ev);
1256 if (lp_disable_netbios()) {
1257 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
1258 name, name_type));
1259 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
1260 return tevent_req_post(req, ev);
1263 state->addr = *addr;
1264 in_addr = (struct sockaddr_in *)(void *)&state->addr;
1265 in_addr->sin_port = htons(NMB_PORT);
1267 set_socket_addr_v4(&state->my_addr);
1269 ZERO_STRUCT(p);
1270 nmb->header.name_trn_id = generate_trn_id();
1271 nmb->header.opcode = 0;
1272 nmb->header.response = false;
1273 nmb->header.nm_flags.bcast = bcast;
1274 nmb->header.nm_flags.recursion_available = false;
1275 nmb->header.nm_flags.recursion_desired = recurse;
1276 nmb->header.nm_flags.trunc = false;
1277 nmb->header.nm_flags.authoritative = false;
1278 nmb->header.rcode = 0;
1279 nmb->header.qdcount = 1;
1280 nmb->header.ancount = 0;
1281 nmb->header.nscount = 0;
1282 nmb->header.arcount = 0;
1284 make_nmb_name(&nmb->question.question_name,name,name_type);
1286 nmb->question.question_type = 0x20;
1287 nmb->question.question_class = 0x1;
1289 state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
1290 &p);
1291 if (state->buflen == 0) {
1292 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1293 DEBUG(10, ("build_packet failed\n"));
1294 return tevent_req_post(req, ev);
1297 subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, bcast,
1298 state->buf, state->buflen,
1299 NMB_PACKET, nmb->header.name_trn_id,
1300 name_query_validator, state);
1301 if (tevent_req_nomem(subreq, req)) {
1302 DEBUG(10, ("nb_trans_send failed\n"));
1303 return tevent_req_post(req, ev);
1305 tevent_req_set_callback(subreq, name_query_done, req);
1306 return req;
1309 static bool name_query_validator(struct packet_struct *p, void *private_data)
1311 struct name_query_state *state = talloc_get_type_abort(
1312 private_data, struct name_query_state);
1313 struct nmb_packet *nmb = &p->packet.nmb;
1314 struct sockaddr_storage *tmp_addrs;
1315 bool got_unique_netbios_name = false;
1316 int i;
1318 debug_nmb_packet(p);
1321 * If we get a Negative Name Query Response from a WINS
1322 * server, we should report it and give up.
1324 if( 0 == nmb->header.opcode /* A query response */
1325 && !state->bcast /* from a WINS server */
1326 && nmb->header.rcode /* Error returned */
1329 if( DEBUGLVL( 3 ) ) {
1330 /* Only executed if DEBUGLEVEL >= 3 */
1331 dbgtext( "Negative name query "
1332 "response, rcode 0x%02x: ",
1333 nmb->header.rcode );
1334 switch( nmb->header.rcode ) {
1335 case 0x01:
1336 dbgtext("Request was invalidly formatted.\n");
1337 break;
1338 case 0x02:
1339 dbgtext("Problem with NBNS, cannot process "
1340 "name.\n");
1341 break;
1342 case 0x03:
1343 dbgtext("The name requested does not "
1344 "exist.\n");
1345 break;
1346 case 0x04:
1347 dbgtext("Unsupported request error.\n");
1348 break;
1349 case 0x05:
1350 dbgtext("Query refused error.\n");
1351 break;
1352 default:
1353 dbgtext("Unrecognized error code.\n" );
1354 break;
1359 * We accept this packet as valid, but tell the upper
1360 * layers that it's a negative response.
1362 state->validate_error = NT_STATUS_NOT_FOUND;
1363 return true;
1366 if (nmb->header.opcode != 0 ||
1367 nmb->header.nm_flags.bcast ||
1368 nmb->header.rcode ||
1369 !nmb->header.ancount) {
1371 * XXXX what do we do with this? Could be a redirect,
1372 * but we'll discard it for the moment.
1374 return false;
1377 tmp_addrs = talloc_realloc(
1378 state, state->addrs, struct sockaddr_storage,
1379 state->num_addrs + nmb->answers->rdlength/6);
1380 if (tmp_addrs == NULL) {
1381 state->validate_error = NT_STATUS_NO_MEMORY;
1382 return true;
1384 state->addrs = tmp_addrs;
1386 DEBUG(2,("Got a positive name query response "
1387 "from %s ( ", inet_ntoa(p->ip)));
1389 for (i=0; i<nmb->answers->rdlength/6; i++) {
1390 uint16_t flags;
1391 struct in_addr ip;
1392 struct sockaddr_storage addr;
1393 int j;
1395 flags = RSVAL(&nmb->answers->rdata[i*6], 0);
1396 got_unique_netbios_name |= ((flags & 0x8000) == 0);
1398 putip((char *)&ip,&nmb->answers->rdata[2+i*6]);
1399 in_addr_to_sockaddr_storage(&addr, ip);
1401 if (is_zero_addr(&addr)) {
1402 continue;
1405 for (j=0; j<state->num_addrs; j++) {
1406 if (sockaddr_equal(
1407 (struct sockaddr *)(void *)&addr,
1408 (struct sockaddr *)(void *)&state->addrs[j])) {
1409 break;
1412 if (j < state->num_addrs) {
1413 /* Already got it */
1414 continue;
1417 DEBUGADD(2,("%s ",inet_ntoa(ip)));
1419 state->addrs[state->num_addrs] = addr;
1420 state->num_addrs += 1;
1422 DEBUGADD(2,(")\n"));
1424 /* We add the flags back ... */
1425 if (nmb->header.response)
1426 state->flags |= NM_FLAGS_RS;
1427 if (nmb->header.nm_flags.authoritative)
1428 state->flags |= NM_FLAGS_AA;
1429 if (nmb->header.nm_flags.trunc)
1430 state->flags |= NM_FLAGS_TC;
1431 if (nmb->header.nm_flags.recursion_desired)
1432 state->flags |= NM_FLAGS_RD;
1433 if (nmb->header.nm_flags.recursion_available)
1434 state->flags |= NM_FLAGS_RA;
1435 if (nmb->header.nm_flags.bcast)
1436 state->flags |= NM_FLAGS_B;
1438 if (state->bcast) {
1440 * We have to collect all entries coming in from broadcast
1441 * queries. If we got a unique name, we're done.
1443 return got_unique_netbios_name;
1446 * WINS responses are accepted when they are received
1448 return true;
1451 static void name_query_done(struct tevent_req *subreq)
1453 struct tevent_req *req = tevent_req_callback_data(
1454 subreq, struct tevent_req);
1455 struct name_query_state *state = tevent_req_data(
1456 req, struct name_query_state);
1457 NTSTATUS status;
1458 struct packet_struct *p = NULL;
1460 status = nb_trans_recv(subreq, &p);
1461 TALLOC_FREE(subreq);
1462 if (tevent_req_nterror(req, status)) {
1463 return;
1465 if (!NT_STATUS_IS_OK(state->validate_error)) {
1466 tevent_req_nterror(req, state->validate_error);
1467 return;
1469 if (p != NULL) {
1471 * Free the packet here, we've collected the response in the
1472 * validator
1474 free_packet(p);
1476 tevent_req_done(req);
1479 NTSTATUS name_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1480 struct sockaddr_storage **addrs, int *num_addrs,
1481 uint8_t *flags)
1483 struct name_query_state *state = tevent_req_data(
1484 req, struct name_query_state);
1485 NTSTATUS status;
1487 if (tevent_req_is_nterror(req, &status)) {
1488 if (state->bcast &&
1489 NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
1491 * In the broadcast case we collect replies until the
1492 * timeout.
1494 status = NT_STATUS_OK;
1496 if (!NT_STATUS_IS_OK(status)) {
1497 return status;
1500 if (state->num_addrs == 0) {
1501 return NT_STATUS_NOT_FOUND;
1503 *addrs = talloc_move(mem_ctx, &state->addrs);
1504 sort_addr_list(*addrs, state->num_addrs);
1505 *num_addrs = state->num_addrs;
1506 if (flags != NULL) {
1507 *flags = state->flags;
1509 return NT_STATUS_OK;
1512 NTSTATUS name_query(const char *name, int name_type,
1513 bool bcast, bool recurse,
1514 const struct sockaddr_storage *to_ss,
1515 TALLOC_CTX *mem_ctx,
1516 struct sockaddr_storage **addrs,
1517 int *num_addrs, uint8_t *flags)
1519 TALLOC_CTX *frame = talloc_stackframe();
1520 struct tevent_context *ev;
1521 struct tevent_req *req;
1522 struct timeval timeout;
1523 NTSTATUS status = NT_STATUS_NO_MEMORY;
1525 ev = samba_tevent_context_init(frame);
1526 if (ev == NULL) {
1527 goto fail;
1529 req = name_query_send(ev, ev, name, name_type, bcast, recurse, to_ss);
1530 if (req == NULL) {
1531 goto fail;
1533 if (bcast) {
1534 timeout = timeval_current_ofs(0, 250000);
1535 } else {
1536 timeout = timeval_current_ofs(2, 0);
1538 if (!tevent_req_set_endtime(req, ev, timeout)) {
1539 goto fail;
1541 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1542 goto fail;
1544 status = name_query_recv(req, mem_ctx, addrs, num_addrs, flags);
1545 fail:
1546 TALLOC_FREE(frame);
1547 return status;
1550 /********************************************************
1551 Convert an array if struct sockaddr_storage to struct ip_service
1552 return false on failure. Port is set to PORT_NONE;
1553 pcount is [in/out] - it is the length of ss_list on input,
1554 and the length of return_iplist on output as we remove any
1555 zero addresses from ss_list.
1556 *********************************************************/
1558 static bool convert_ss2service(struct ip_service **return_iplist,
1559 const struct sockaddr_storage *ss_list,
1560 int *pcount)
1562 int i;
1563 int orig_count = *pcount;
1564 int real_count = 0;
1566 if (orig_count==0 || !ss_list )
1567 return False;
1569 /* Filter out zero addrs. */
1570 for ( i=0; i<orig_count; i++ ) {
1571 if (is_zero_addr(&ss_list[i])) {
1572 continue;
1574 real_count++;
1576 if (real_count==0) {
1577 return false;
1580 /* copy the ip address; port will be PORT_NONE */
1581 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, real_count)) ==
1582 NULL) {
1583 DEBUG(0,("convert_ip2service: malloc failed "
1584 "for %d enetries!\n", real_count ));
1585 return False;
1588 for ( i=0, real_count = 0; i<orig_count; i++ ) {
1589 if (is_zero_addr(&ss_list[i])) {
1590 continue;
1592 (*return_iplist)[real_count].ss = ss_list[i];
1593 (*return_iplist)[real_count].port = PORT_NONE;
1594 real_count++;
1597 *pcount = real_count;
1598 return true;
1601 struct name_queries_state {
1602 struct tevent_context *ev;
1603 const char *name;
1604 int name_type;
1605 bool bcast;
1606 bool recurse;
1607 const struct sockaddr_storage *addrs;
1608 int num_addrs;
1609 int wait_msec;
1610 int timeout_msec;
1612 struct tevent_req **subreqs;
1613 int num_received;
1614 int num_sent;
1616 int received_index;
1617 struct sockaddr_storage *result_addrs;
1618 int num_result_addrs;
1619 uint8_t flags;
1622 static void name_queries_done(struct tevent_req *subreq);
1623 static void name_queries_next(struct tevent_req *subreq);
1626 * Send a name query to multiple destinations with a wait time in between
1629 static struct tevent_req *name_queries_send(
1630 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1631 const char *name, int name_type,
1632 bool bcast, bool recurse,
1633 const struct sockaddr_storage *addrs,
1634 int num_addrs, int wait_msec, int timeout_msec)
1636 struct tevent_req *req, *subreq;
1637 struct name_queries_state *state;
1639 req = tevent_req_create(mem_ctx, &state,
1640 struct name_queries_state);
1641 if (req == NULL) {
1642 return NULL;
1644 state->ev = ev;
1645 state->name = name;
1646 state->name_type = name_type;
1647 state->bcast = bcast;
1648 state->recurse = recurse;
1649 state->addrs = addrs;
1650 state->num_addrs = num_addrs;
1651 state->wait_msec = wait_msec;
1652 state->timeout_msec = timeout_msec;
1654 state->subreqs = talloc_zero_array(
1655 state, struct tevent_req *, num_addrs);
1656 if (tevent_req_nomem(state->subreqs, req)) {
1657 return tevent_req_post(req, ev);
1659 state->num_sent = 0;
1661 subreq = name_query_send(
1662 state->subreqs, state->ev, name, name_type, bcast, recurse,
1663 &state->addrs[state->num_sent]);
1664 if (tevent_req_nomem(subreq, req)) {
1665 return tevent_req_post(req, ev);
1667 if (!tevent_req_set_endtime(
1668 subreq, state->ev,
1669 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1670 tevent_req_oom(req);
1671 return tevent_req_post(req, ev);
1673 tevent_req_set_callback(subreq, name_queries_done, req);
1675 state->subreqs[state->num_sent] = subreq;
1676 state->num_sent += 1;
1678 if (state->num_sent < state->num_addrs) {
1679 subreq = tevent_wakeup_send(
1680 state, state->ev,
1681 timeval_current_ofs(0, state->wait_msec * 1000));
1682 if (tevent_req_nomem(subreq, req)) {
1683 return tevent_req_post(req, ev);
1685 tevent_req_set_callback(subreq, name_queries_next, req);
1687 return req;
1690 static void name_queries_done(struct tevent_req *subreq)
1692 struct tevent_req *req = tevent_req_callback_data(
1693 subreq, struct tevent_req);
1694 struct name_queries_state *state = tevent_req_data(
1695 req, struct name_queries_state);
1696 int i;
1697 NTSTATUS status;
1699 status = name_query_recv(subreq, state, &state->result_addrs,
1700 &state->num_result_addrs, &state->flags);
1702 for (i=0; i<state->num_sent; i++) {
1703 if (state->subreqs[i] == subreq) {
1704 break;
1707 if (i == state->num_sent) {
1708 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1709 return;
1711 TALLOC_FREE(state->subreqs[i]);
1713 state->num_received += 1;
1715 if (!NT_STATUS_IS_OK(status)) {
1717 if (state->num_received >= state->num_addrs) {
1718 tevent_req_nterror(req, status);
1719 return;
1722 * Still outstanding requests, just wait
1724 return;
1726 state->received_index = i;
1727 tevent_req_done(req);
1730 static void name_queries_next(struct tevent_req *subreq)
1732 struct tevent_req *req = tevent_req_callback_data(
1733 subreq, struct tevent_req);
1734 struct name_queries_state *state = tevent_req_data(
1735 req, struct name_queries_state);
1737 if (!tevent_wakeup_recv(subreq)) {
1738 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
1739 return;
1742 subreq = name_query_send(
1743 state->subreqs, state->ev,
1744 state->name, state->name_type, state->bcast, state->recurse,
1745 &state->addrs[state->num_sent]);
1746 if (tevent_req_nomem(subreq, req)) {
1747 return;
1749 tevent_req_set_callback(subreq, name_queries_done, req);
1750 if (!tevent_req_set_endtime(
1751 subreq, state->ev,
1752 timeval_current_ofs(0, state->timeout_msec * 1000))) {
1753 tevent_req_oom(req);
1754 return;
1756 state->subreqs[state->num_sent] = subreq;
1757 state->num_sent += 1;
1759 if (state->num_sent < state->num_addrs) {
1760 subreq = tevent_wakeup_send(
1761 state, state->ev,
1762 timeval_current_ofs(0, state->wait_msec * 1000));
1763 if (tevent_req_nomem(subreq, req)) {
1764 return;
1766 tevent_req_set_callback(subreq, name_queries_next, req);
1770 static NTSTATUS name_queries_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1771 struct sockaddr_storage **result_addrs,
1772 int *num_result_addrs, uint8_t *flags,
1773 int *received_index)
1775 struct name_queries_state *state = tevent_req_data(
1776 req, struct name_queries_state);
1777 NTSTATUS status;
1779 if (tevent_req_is_nterror(req, &status)) {
1780 return status;
1783 if (result_addrs != NULL) {
1784 *result_addrs = talloc_move(mem_ctx, &state->result_addrs);
1786 if (num_result_addrs != NULL) {
1787 *num_result_addrs = state->num_result_addrs;
1789 if (flags != NULL) {
1790 *flags = state->flags;
1792 if (received_index != NULL) {
1793 *received_index = state->received_index;
1795 return NT_STATUS_OK;
1798 /********************************************************
1799 Resolve via "bcast" method.
1800 *********************************************************/
1802 struct name_resolve_bcast_state {
1803 struct sockaddr_storage *addrs;
1804 int num_addrs;
1807 static void name_resolve_bcast_done(struct tevent_req *subreq);
1809 struct tevent_req *name_resolve_bcast_send(TALLOC_CTX *mem_ctx,
1810 struct tevent_context *ev,
1811 const char *name,
1812 int name_type)
1814 struct tevent_req *req, *subreq;
1815 struct name_resolve_bcast_state *state;
1816 struct sockaddr_storage *bcast_addrs;
1817 int i, num_addrs, num_bcast_addrs;
1819 req = tevent_req_create(mem_ctx, &state,
1820 struct name_resolve_bcast_state);
1821 if (req == NULL) {
1822 return NULL;
1825 if (lp_disable_netbios()) {
1826 DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
1827 name, name_type));
1828 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1829 return tevent_req_post(req, ev);
1833 * "bcast" means do a broadcast lookup on all the local interfaces.
1836 DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
1837 "for name %s<0x%x>\n", name, name_type));
1839 num_addrs = iface_count();
1840 bcast_addrs = talloc_array(state, struct sockaddr_storage, num_addrs);
1841 if (tevent_req_nomem(bcast_addrs, req)) {
1842 return tevent_req_post(req, ev);
1846 * Lookup the name on all the interfaces, return on
1847 * the first successful match.
1849 num_bcast_addrs = 0;
1851 for (i=0; i<num_addrs; i++) {
1852 const struct sockaddr_storage *pss = iface_n_bcast(i);
1854 if (pss->ss_family != AF_INET) {
1855 continue;
1857 bcast_addrs[num_bcast_addrs] = *pss;
1858 num_bcast_addrs += 1;
1861 subreq = name_queries_send(state, ev, name, name_type, true, true,
1862 bcast_addrs, num_bcast_addrs, 0, 1000);
1863 if (tevent_req_nomem(subreq, req)) {
1864 return tevent_req_post(req, ev);
1866 tevent_req_set_callback(subreq, name_resolve_bcast_done, req);
1867 return req;
1870 static void name_resolve_bcast_done(struct tevent_req *subreq)
1872 struct tevent_req *req = tevent_req_callback_data(
1873 subreq, struct tevent_req);
1874 struct name_resolve_bcast_state *state = tevent_req_data(
1875 req, struct name_resolve_bcast_state);
1876 NTSTATUS status;
1878 status = name_queries_recv(subreq, state,
1879 &state->addrs, &state->num_addrs,
1880 NULL, NULL);
1881 TALLOC_FREE(subreq);
1882 if (tevent_req_nterror(req, status)) {
1883 return;
1885 tevent_req_done(req);
1888 NTSTATUS name_resolve_bcast_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1889 struct sockaddr_storage **addrs,
1890 int *num_addrs)
1892 struct name_resolve_bcast_state *state = tevent_req_data(
1893 req, struct name_resolve_bcast_state);
1894 NTSTATUS status;
1896 if (tevent_req_is_nterror(req, &status)) {
1897 return status;
1899 *addrs = talloc_move(mem_ctx, &state->addrs);
1900 *num_addrs = state->num_addrs;
1901 return NT_STATUS_OK;
1904 NTSTATUS name_resolve_bcast(const char *name,
1905 int name_type,
1906 TALLOC_CTX *mem_ctx,
1907 struct sockaddr_storage **return_iplist,
1908 int *return_count)
1910 TALLOC_CTX *frame = talloc_stackframe();
1911 struct tevent_context *ev;
1912 struct tevent_req *req;
1913 NTSTATUS status = NT_STATUS_NO_MEMORY;
1915 ev = samba_tevent_context_init(frame);
1916 if (ev == NULL) {
1917 goto fail;
1919 req = name_resolve_bcast_send(frame, ev, name, name_type);
1920 if (req == NULL) {
1921 goto fail;
1923 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1924 goto fail;
1926 status = name_resolve_bcast_recv(req, mem_ctx, return_iplist,
1927 return_count);
1928 fail:
1929 TALLOC_FREE(frame);
1930 return status;
1933 struct query_wins_list_state {
1934 struct tevent_context *ev;
1935 const char *name;
1936 uint8_t name_type;
1937 struct in_addr *servers;
1938 uint32_t num_servers;
1939 struct sockaddr_storage server;
1940 uint32_t num_sent;
1942 struct sockaddr_storage *addrs;
1943 int num_addrs;
1944 uint8_t flags;
1947 static void query_wins_list_done(struct tevent_req *subreq);
1950 * Query a list of (replicating) wins servers in sequence, call them
1951 * dead if they don't reply
1954 static struct tevent_req *query_wins_list_send(
1955 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1956 struct in_addr src_ip, const char *name, uint8_t name_type,
1957 struct in_addr *servers, int num_servers)
1959 struct tevent_req *req, *subreq;
1960 struct query_wins_list_state *state;
1962 req = tevent_req_create(mem_ctx, &state,
1963 struct query_wins_list_state);
1964 if (req == NULL) {
1965 return NULL;
1967 state->ev = ev;
1968 state->name = name;
1969 state->name_type = name_type;
1970 state->servers = servers;
1971 state->num_servers = num_servers;
1973 if (state->num_servers == 0) {
1974 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
1975 return tevent_req_post(req, ev);
1978 in_addr_to_sockaddr_storage(
1979 &state->server, state->servers[state->num_sent]);
1981 subreq = name_query_send(state, state->ev,
1982 state->name, state->name_type,
1983 false, true, &state->server);
1984 state->num_sent += 1;
1985 if (tevent_req_nomem(subreq, req)) {
1986 return tevent_req_post(req, ev);
1988 if (!tevent_req_set_endtime(subreq, state->ev,
1989 timeval_current_ofs(2, 0))) {
1990 tevent_req_oom(req);
1991 return tevent_req_post(req, ev);
1993 tevent_req_set_callback(subreq, query_wins_list_done, req);
1994 return req;
1997 static void query_wins_list_done(struct tevent_req *subreq)
1999 struct tevent_req *req = tevent_req_callback_data(
2000 subreq, struct tevent_req);
2001 struct query_wins_list_state *state = tevent_req_data(
2002 req, struct query_wins_list_state);
2003 NTSTATUS status;
2005 status = name_query_recv(subreq, state,
2006 &state->addrs, &state->num_addrs,
2007 &state->flags);
2008 TALLOC_FREE(subreq);
2009 if (NT_STATUS_IS_OK(status)) {
2010 tevent_req_done(req);
2011 return;
2013 if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
2014 tevent_req_nterror(req, status);
2015 return;
2017 wins_srv_died(state->servers[state->num_sent-1],
2018 my_socket_addr_v4());
2020 if (state->num_sent == state->num_servers) {
2021 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2022 return;
2025 in_addr_to_sockaddr_storage(
2026 &state->server, state->servers[state->num_sent]);
2028 subreq = name_query_send(state, state->ev,
2029 state->name, state->name_type,
2030 false, true, &state->server);
2031 state->num_sent += 1;
2032 if (tevent_req_nomem(subreq, req)) {
2033 return;
2035 if (!tevent_req_set_endtime(subreq, state->ev,
2036 timeval_current_ofs(2, 0))) {
2037 tevent_req_oom(req);
2038 return;
2040 tevent_req_set_callback(subreq, query_wins_list_done, req);
2043 static NTSTATUS query_wins_list_recv(struct tevent_req *req,
2044 TALLOC_CTX *mem_ctx,
2045 struct sockaddr_storage **addrs,
2046 int *num_addrs,
2047 uint8_t *flags)
2049 struct query_wins_list_state *state = tevent_req_data(
2050 req, struct query_wins_list_state);
2051 NTSTATUS status;
2053 if (tevent_req_is_nterror(req, &status)) {
2054 return status;
2056 if (addrs != NULL) {
2057 *addrs = talloc_move(mem_ctx, &state->addrs);
2059 if (num_addrs != NULL) {
2060 *num_addrs = state->num_addrs;
2062 if (flags != NULL) {
2063 *flags = state->flags;
2065 return NT_STATUS_OK;
2068 struct resolve_wins_state {
2069 int num_sent;
2070 int num_received;
2072 struct sockaddr_storage *addrs;
2073 int num_addrs;
2074 uint8_t flags;
2077 static void resolve_wins_done(struct tevent_req *subreq);
2079 struct tevent_req *resolve_wins_send(TALLOC_CTX *mem_ctx,
2080 struct tevent_context *ev,
2081 const char *name,
2082 int name_type)
2084 struct tevent_req *req, *subreq;
2085 struct resolve_wins_state *state;
2086 char **wins_tags = NULL;
2087 struct sockaddr_storage src_ss;
2088 struct in_addr src_ip;
2089 int i, num_wins_tags;
2091 req = tevent_req_create(mem_ctx, &state,
2092 struct resolve_wins_state);
2093 if (req == NULL) {
2094 return NULL;
2097 if (wins_srv_count() < 1) {
2098 DEBUG(3,("resolve_wins: WINS server resolution selected "
2099 "and no WINS servers listed.\n"));
2100 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2101 goto fail;
2104 /* the address we will be sending from */
2105 if (!interpret_string_addr(&src_ss, lp_nbt_client_socket_address(),
2106 AI_NUMERICHOST|AI_PASSIVE)) {
2107 zero_sockaddr(&src_ss);
2110 if (src_ss.ss_family != AF_INET) {
2111 char addr[INET6_ADDRSTRLEN];
2112 print_sockaddr(addr, sizeof(addr), &src_ss);
2113 DEBUG(3,("resolve_wins: cannot receive WINS replies "
2114 "on IPv6 address %s\n",
2115 addr));
2116 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2117 goto fail;
2120 src_ip = ((const struct sockaddr_in *)(void *)&src_ss)->sin_addr;
2122 wins_tags = wins_srv_tags();
2123 if (wins_tags == NULL) {
2124 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
2125 goto fail;
2128 num_wins_tags = 0;
2129 while (wins_tags[num_wins_tags] != NULL) {
2130 num_wins_tags += 1;
2133 for (i=0; i<num_wins_tags; i++) {
2134 int num_servers, num_alive;
2135 struct in_addr *servers, *alive;
2136 int j;
2138 if (!wins_server_tag_ips(wins_tags[i], talloc_tos(),
2139 &servers, &num_servers)) {
2140 DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
2141 wins_tags[i]));
2142 continue;
2145 alive = talloc_array(state, struct in_addr, num_servers);
2146 if (tevent_req_nomem(alive, req)) {
2147 goto fail;
2150 num_alive = 0;
2151 for (j=0; j<num_servers; j++) {
2152 struct in_addr wins_ip = servers[j];
2154 if (global_in_nmbd && ismyip_v4(wins_ip)) {
2155 /* yikes! we'll loop forever */
2156 continue;
2158 /* skip any that have been unresponsive lately */
2159 if (wins_srv_is_dead(wins_ip, src_ip)) {
2160 continue;
2162 DEBUG(3, ("resolve_wins: using WINS server %s "
2163 "and tag '%s'\n",
2164 inet_ntoa(wins_ip), wins_tags[i]));
2165 alive[num_alive] = wins_ip;
2166 num_alive += 1;
2168 TALLOC_FREE(servers);
2170 if (num_alive == 0) {
2171 continue;
2174 subreq = query_wins_list_send(
2175 state, ev, src_ip, name, name_type,
2176 alive, num_alive);
2177 if (tevent_req_nomem(subreq, req)) {
2178 goto fail;
2180 tevent_req_set_callback(subreq, resolve_wins_done, req);
2181 state->num_sent += 1;
2184 if (state->num_sent == 0) {
2185 tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
2186 goto fail;
2189 wins_srv_tags_free(wins_tags);
2190 return req;
2191 fail:
2192 wins_srv_tags_free(wins_tags);
2193 return tevent_req_post(req, ev);
2196 static void resolve_wins_done(struct tevent_req *subreq)
2198 struct tevent_req *req = tevent_req_callback_data(
2199 subreq, struct tevent_req);
2200 struct resolve_wins_state *state = tevent_req_data(
2201 req, struct resolve_wins_state);
2202 NTSTATUS status;
2204 status = query_wins_list_recv(subreq, state, &state->addrs,
2205 &state->num_addrs, &state->flags);
2206 if (NT_STATUS_IS_OK(status)) {
2207 tevent_req_done(req);
2208 return;
2211 state->num_received += 1;
2213 if (state->num_received < state->num_sent) {
2215 * Wait for the others
2217 return;
2219 tevent_req_nterror(req, status);
2222 NTSTATUS resolve_wins_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2223 struct sockaddr_storage **addrs,
2224 int *num_addrs, uint8_t *flags)
2226 struct resolve_wins_state *state = tevent_req_data(
2227 req, struct resolve_wins_state);
2228 NTSTATUS status;
2230 if (tevent_req_is_nterror(req, &status)) {
2231 return status;
2233 if (addrs != NULL) {
2234 *addrs = talloc_move(mem_ctx, &state->addrs);
2236 if (num_addrs != NULL) {
2237 *num_addrs = state->num_addrs;
2239 if (flags != NULL) {
2240 *flags = state->flags;
2242 return NT_STATUS_OK;
2245 /********************************************************
2246 Resolve via "wins" method.
2247 *********************************************************/
2249 NTSTATUS resolve_wins(const char *name,
2250 int name_type,
2251 TALLOC_CTX *mem_ctx,
2252 struct sockaddr_storage **return_iplist,
2253 int *return_count)
2255 struct tevent_context *ev;
2256 struct tevent_req *req;
2257 NTSTATUS status = NT_STATUS_NO_MEMORY;
2259 ev = samba_tevent_context_init(talloc_tos());
2260 if (ev == NULL) {
2261 goto fail;
2263 req = resolve_wins_send(ev, ev, name, name_type);
2264 if (req == NULL) {
2265 goto fail;
2267 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2268 goto fail;
2270 status = resolve_wins_recv(req, mem_ctx, return_iplist, return_count,
2271 NULL);
2272 fail:
2273 TALLOC_FREE(ev);
2274 return status;
2277 /********************************************************
2278 Resolve via "lmhosts" method.
2279 *********************************************************/
2281 static NTSTATUS resolve_lmhosts(const char *name, int name_type,
2282 struct ip_service **return_iplist,
2283 int *return_count)
2286 * "lmhosts" means parse the local lmhosts file.
2288 struct sockaddr_storage *ss_list;
2289 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
2290 TALLOC_CTX *ctx = NULL;
2292 *return_iplist = NULL;
2293 *return_count = 0;
2295 DEBUG(3,("resolve_lmhosts: "
2296 "Attempting lmhosts lookup for name %s<0x%x>\n",
2297 name, name_type));
2299 ctx = talloc_init("resolve_lmhosts");
2300 if (!ctx) {
2301 return NT_STATUS_NO_MEMORY;
2304 status = resolve_lmhosts_file_as_sockaddr(get_dyn_LMHOSTSFILE(),
2305 name, name_type,
2306 ctx,
2307 &ss_list,
2308 return_count);
2309 if (NT_STATUS_IS_OK(status)) {
2310 if (convert_ss2service(return_iplist,
2311 ss_list,
2312 return_count)) {
2313 talloc_free(ctx);
2314 return NT_STATUS_OK;
2315 } else {
2316 talloc_free(ctx);
2317 return NT_STATUS_NO_MEMORY;
2320 talloc_free(ctx);
2321 return status;
2325 /********************************************************
2326 Resolve via "hosts" method.
2327 *********************************************************/
2329 static NTSTATUS resolve_hosts(const char *name, int name_type,
2330 struct ip_service **return_iplist,
2331 int *return_count)
2334 * "host" means do a localhost, or dns lookup.
2336 struct addrinfo hints;
2337 struct addrinfo *ailist = NULL;
2338 struct addrinfo *res = NULL;
2339 int ret = -1;
2340 int i = 0;
2342 if ( name_type != 0x20 && name_type != 0x0) {
2343 DEBUG(5, ("resolve_hosts: not appropriate "
2344 "for name type <0x%x>\n",
2345 name_type));
2346 return NT_STATUS_INVALID_PARAMETER;
2349 *return_iplist = NULL;
2350 *return_count = 0;
2352 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n",
2353 name, name_type));
2355 ZERO_STRUCT(hints);
2356 /* By default make sure it supports TCP. */
2357 hints.ai_socktype = SOCK_STREAM;
2358 hints.ai_flags = AI_ADDRCONFIG;
2360 #if !defined(HAVE_IPV6)
2361 /* Unless we have IPv6, we really only want IPv4 addresses back. */
2362 hints.ai_family = AF_INET;
2363 #endif
2365 ret = getaddrinfo(name,
2366 NULL,
2367 &hints,
2368 &ailist);
2369 if (ret) {
2370 DEBUG(3,("resolve_hosts: getaddrinfo failed for name %s [%s]\n",
2371 name,
2372 gai_strerror(ret) ));
2375 for (res = ailist; res; res = res->ai_next) {
2376 struct sockaddr_storage ss;
2378 if (!res->ai_addr || res->ai_addrlen == 0) {
2379 continue;
2382 ZERO_STRUCT(ss);
2383 memcpy(&ss, res->ai_addr, res->ai_addrlen);
2385 if (is_zero_addr(&ss)) {
2386 continue;
2389 *return_count += 1;
2391 *return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
2392 struct ip_service,
2393 *return_count);
2394 if (!*return_iplist) {
2395 DEBUG(3,("resolve_hosts: malloc fail !\n"));
2396 freeaddrinfo(ailist);
2397 return NT_STATUS_NO_MEMORY;
2399 (*return_iplist)[i].ss = ss;
2400 (*return_iplist)[i].port = PORT_NONE;
2401 i++;
2403 if (ailist) {
2404 freeaddrinfo(ailist);
2406 if (*return_count) {
2407 return NT_STATUS_OK;
2409 return NT_STATUS_UNSUCCESSFUL;
2412 /********************************************************
2413 Resolve via "ADS" method.
2414 *********************************************************/
2416 /* Special name type used to cause a _kerberos DNS lookup. */
2417 #define KDC_NAME_TYPE 0xDCDC
2419 static NTSTATUS resolve_ads(const char *name,
2420 int name_type,
2421 const char *sitename,
2422 struct ip_service **return_iplist,
2423 int *return_count)
2425 int i;
2426 NTSTATUS status;
2427 TALLOC_CTX *ctx;
2428 struct dns_rr_srv *dcs = NULL;
2429 int numdcs = 0;
2430 int numaddrs = 0;
2432 if ((name_type != 0x1c) && (name_type != KDC_NAME_TYPE) &&
2433 (name_type != 0x1b)) {
2434 return NT_STATUS_INVALID_PARAMETER;
2437 if ( (ctx = talloc_init("resolve_ads")) == NULL ) {
2438 DEBUG(0,("resolve_ads: talloc_init() failed!\n"));
2439 return NT_STATUS_NO_MEMORY;
2442 /* The DNS code needs fixing to find IPv6 addresses... JRA. */
2443 switch (name_type) {
2444 case 0x1b:
2445 DEBUG(5,("resolve_ads: Attempting to resolve "
2446 "PDC for %s using DNS\n", name));
2447 status = ads_dns_query_pdc(ctx,
2448 name,
2449 &dcs,
2450 &numdcs);
2451 break;
2453 case 0x1c:
2454 DEBUG(5,("resolve_ads: Attempting to resolve "
2455 "DCs for %s using DNS\n", name));
2456 status = ads_dns_query_dcs(ctx,
2457 name,
2458 sitename,
2459 &dcs,
2460 &numdcs);
2461 break;
2462 case KDC_NAME_TYPE:
2463 DEBUG(5,("resolve_ads: Attempting to resolve "
2464 "KDCs for %s using DNS\n", name));
2465 status = ads_dns_query_kdcs(ctx,
2466 name,
2467 sitename,
2468 &dcs,
2469 &numdcs);
2470 break;
2471 default:
2472 status = NT_STATUS_INVALID_PARAMETER;
2473 break;
2476 if ( !NT_STATUS_IS_OK( status ) ) {
2477 talloc_destroy(ctx);
2478 return status;
2481 for (i=0;i<numdcs;i++) {
2482 if (!dcs[i].ss_s) {
2483 numaddrs += 1;
2484 } else {
2485 numaddrs += dcs[i].num_ips;
2489 if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, numaddrs)) ==
2490 NULL ) {
2491 DEBUG(0,("resolve_ads: malloc failed for %d entries\n",
2492 numaddrs ));
2493 talloc_destroy(ctx);
2494 return NT_STATUS_NO_MEMORY;
2497 /* now unroll the list of IP addresses */
2499 *return_count = 0;
2501 for (i = 0; i < numdcs && (*return_count<numaddrs); i++ ) {
2502 /* If we don't have an IP list for a name, lookup it up */
2503 if (!dcs[i].ss_s) {
2504 /* We need to get all IP addresses here. */
2505 struct addrinfo *res = NULL;
2506 struct addrinfo *p;
2507 int extra_addrs = 0;
2509 if (!interpret_string_addr_internal(&res,
2510 dcs[i].hostname,
2511 0)) {
2512 continue;
2514 /* Add in every IP from the lookup. How
2515 many is that ? */
2516 for (p = res; p; p = p->ai_next) {
2517 struct sockaddr_storage ss;
2518 memcpy(&ss, p->ai_addr, p->ai_addrlen);
2519 if (is_zero_addr(&ss)) {
2520 continue;
2522 extra_addrs++;
2524 if (extra_addrs > 1) {
2525 /* We need to expand the return_iplist array
2526 as we only budgeted for one address. */
2527 numaddrs += (extra_addrs-1);
2528 *return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
2529 struct ip_service,
2530 numaddrs);
2531 if (*return_iplist == NULL) {
2532 if (res) {
2533 freeaddrinfo(res);
2535 talloc_destroy(ctx);
2536 return NT_STATUS_NO_MEMORY;
2539 for (p = res; p; p = p->ai_next) {
2540 (*return_iplist)[*return_count].port = dcs[i].port;
2541 memcpy(&(*return_iplist)[*return_count].ss,
2542 p->ai_addr,
2543 p->ai_addrlen);
2544 if (is_zero_addr(&(*return_iplist)[*return_count].ss)) {
2545 continue;
2547 (*return_count)++;
2548 /* Should never happen, but still... */
2549 if (*return_count>=numaddrs) {
2550 break;
2553 if (res) {
2554 freeaddrinfo(res);
2556 } else {
2557 /* use all the IP addresses from the SRV sresponse */
2558 int j;
2559 for (j = 0; j < dcs[i].num_ips; j++) {
2560 (*return_iplist)[*return_count].port = dcs[i].port;
2561 (*return_iplist)[*return_count].ss = dcs[i].ss_s[j];
2562 if (is_zero_addr(&(*return_iplist)[*return_count].ss)) {
2563 continue;
2565 (*return_count)++;
2566 /* Should never happen, but still... */
2567 if (*return_count>=numaddrs) {
2568 break;
2574 talloc_destroy(ctx);
2575 return NT_STATUS_OK;
2578 static const char **filter_out_nbt_lookup(TALLOC_CTX *mem_ctx,
2579 const char **resolve_order)
2581 size_t i, len, result_idx;
2582 const char **result;
2584 len = 0;
2585 while (resolve_order[len] != NULL) {
2586 len += 1;
2589 result = talloc_array(mem_ctx, const char *, len+1);
2590 if (result == NULL) {
2591 return NULL;
2594 result_idx = 0;
2596 for (i=0; i<len; i++) {
2597 const char *tok = resolve_order[i];
2599 if (strequal(tok, "lmhosts") || strequal(tok, "wins") ||
2600 strequal(tok, "bcast")) {
2601 continue;
2603 result[result_idx++] = tok;
2605 result[result_idx] = NULL;
2607 return result;
2610 /*******************************************************************
2611 Internal interface to resolve a name into an IP address.
2612 Use this function if the string is either an IP address, DNS
2613 or host name or NetBIOS name. This uses the name switch in the
2614 smb.conf to determine the order of name resolution.
2616 Added support for ip addr/port to support ADS ldap servers.
2617 the only place we currently care about the port is in the
2618 resolve_hosts() when looking up DC's via SRV RR entries in DNS
2619 **********************************************************************/
2621 NTSTATUS internal_resolve_name(const char *name,
2622 int name_type,
2623 const char *sitename,
2624 struct ip_service **return_iplist,
2625 int *return_count,
2626 const char **resolve_order)
2628 const char *tok;
2629 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2630 int i;
2631 TALLOC_CTX *frame = NULL;
2633 *return_iplist = NULL;
2634 *return_count = 0;
2636 DEBUG(10, ("internal_resolve_name: looking up %s#%x (sitename %s)\n",
2637 name, name_type, sitename ? sitename : "(null)"));
2639 if (is_ipaddress(name)) {
2640 if ((*return_iplist = SMB_MALLOC_P(struct ip_service)) ==
2641 NULL) {
2642 DEBUG(0,("internal_resolve_name: malloc fail !\n"));
2643 return NT_STATUS_NO_MEMORY;
2646 /* ignore the port here */
2647 (*return_iplist)->port = PORT_NONE;
2649 /* if it's in the form of an IP address then get the lib to interpret it */
2650 if (!interpret_string_addr(&(*return_iplist)->ss,
2651 name, AI_NUMERICHOST)) {
2652 DEBUG(1,("internal_resolve_name: interpret_string_addr "
2653 "failed on %s\n",
2654 name));
2655 SAFE_FREE(*return_iplist);
2656 return NT_STATUS_INVALID_PARAMETER;
2658 if (is_zero_addr(&(*return_iplist)->ss)) {
2659 SAFE_FREE(*return_iplist);
2660 return NT_STATUS_UNSUCCESSFUL;
2662 *return_count = 1;
2663 return NT_STATUS_OK;
2666 /* Check name cache */
2668 if (namecache_fetch(name, name_type, return_iplist, return_count)) {
2669 *return_count = remove_duplicate_addrs2(*return_iplist,
2670 *return_count );
2671 /* This could be a negative response */
2672 if (*return_count > 0) {
2673 return NT_STATUS_OK;
2674 } else {
2675 return NT_STATUS_UNSUCCESSFUL;
2679 /* set the name resolution order */
2681 if (resolve_order && strcmp(resolve_order[0], "NULL") == 0) {
2682 DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
2683 return NT_STATUS_INVALID_PARAMETER;
2686 if (!resolve_order || !resolve_order[0]) {
2687 static const char *host_order[] = { "host", NULL };
2688 resolve_order = host_order;
2691 frame = talloc_stackframe();
2693 if ((strlen(name) > MAX_NETBIOSNAME_LEN - 1) ||
2694 (strchr(name, '.') != NULL)) {
2696 * Don't do NBT lookup, the name would not fit anyway
2698 resolve_order = filter_out_nbt_lookup(frame, resolve_order);
2699 if (resolve_order == NULL) {
2700 TALLOC_FREE(frame);
2701 return NT_STATUS_NO_MEMORY;
2705 /* iterate through the name resolution backends */
2707 for (i=0; resolve_order[i]; i++) {
2708 tok = resolve_order[i];
2710 if((strequal(tok, "host") || strequal(tok, "hosts"))) {
2711 status = resolve_hosts(name, name_type, return_iplist,
2712 return_count);
2713 if (NT_STATUS_IS_OK(status)) {
2714 goto done;
2716 } else if(strequal( tok, "kdc")) {
2717 /* deal with KDC_NAME_TYPE names here.
2718 * This will result in a SRV record lookup */
2719 status = resolve_ads(name, KDC_NAME_TYPE, sitename,
2720 return_iplist, return_count);
2721 if (NT_STATUS_IS_OK(status)) {
2722 /* Ensure we don't namecache
2723 * this with the KDC port. */
2724 name_type = KDC_NAME_TYPE;
2725 goto done;
2727 } else if(strequal( tok, "ads")) {
2728 /* deal with 0x1c and 0x1b names here.
2729 * This will result in a SRV record lookup */
2730 status = resolve_ads(name, name_type, sitename,
2731 return_iplist, return_count);
2732 if (NT_STATUS_IS_OK(status)) {
2733 goto done;
2735 } else if (strequal(tok, "lmhosts")) {
2736 status = resolve_lmhosts(name, name_type,
2737 return_iplist, return_count);
2738 if (NT_STATUS_IS_OK(status)) {
2739 goto done;
2741 } else if (strequal(tok, "wins")) {
2742 /* don't resolve 1D via WINS */
2743 struct sockaddr_storage *ss_list;
2744 if (name_type != 0x1D) {
2745 status = resolve_wins(name, name_type,
2746 talloc_tos(),
2747 &ss_list,
2748 return_count);
2749 if (NT_STATUS_IS_OK(status)) {
2750 if (!convert_ss2service(return_iplist,
2751 ss_list,
2752 return_count)) {
2753 status = NT_STATUS_NO_MEMORY;
2755 goto done;
2758 } else if (strequal(tok, "bcast")) {
2759 struct sockaddr_storage *ss_list;
2760 status = name_resolve_bcast(
2761 name, name_type, talloc_tos(),
2762 &ss_list, return_count);
2763 if (NT_STATUS_IS_OK(status)) {
2764 if (!convert_ss2service(return_iplist,
2765 ss_list,
2766 return_count)) {
2767 status = NT_STATUS_NO_MEMORY;
2769 goto done;
2771 } else {
2772 DEBUG(0,("resolve_name: unknown name switch type %s\n",
2773 tok));
2777 /* All of the resolve_* functions above have returned false. */
2779 TALLOC_FREE(frame);
2780 SAFE_FREE(*return_iplist);
2781 *return_count = 0;
2783 return NT_STATUS_UNSUCCESSFUL;
2785 done:
2787 /* Remove duplicate entries. Some queries, notably #1c (domain
2788 controllers) return the PDC in iplist[0] and then all domain
2789 controllers including the PDC in iplist[1..n]. Iterating over
2790 the iplist when the PDC is down will cause two sets of timeouts. */
2792 *return_count = remove_duplicate_addrs2(*return_iplist, *return_count );
2794 /* Save in name cache */
2795 if ( DEBUGLEVEL >= 100 ) {
2796 for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++) {
2797 char addr[INET6_ADDRSTRLEN];
2798 print_sockaddr(addr, sizeof(addr),
2799 &(*return_iplist)[i].ss);
2800 DEBUG(100, ("Storing name %s of type %d (%s:%d)\n",
2801 name,
2802 name_type,
2803 addr,
2804 (*return_iplist)[i].port));
2808 if (*return_count) {
2809 namecache_store(name, name_type, *return_count, *return_iplist);
2812 /* Display some debugging info */
2814 if ( DEBUGLEVEL >= 10 ) {
2815 DEBUG(10, ("internal_resolve_name: returning %d addresses: ",
2816 *return_count));
2818 for (i = 0; i < *return_count; i++) {
2819 char addr[INET6_ADDRSTRLEN];
2820 print_sockaddr(addr, sizeof(addr),
2821 &(*return_iplist)[i].ss);
2822 DEBUGADD(10, ("%s:%d ",
2823 addr,
2824 (*return_iplist)[i].port));
2826 DEBUG(10, ("\n"));
2829 TALLOC_FREE(frame);
2830 return status;
2833 /********************************************************
2834 Internal interface to resolve a name into one IP address.
2835 Use this function if the string is either an IP address, DNS
2836 or host name or NetBIOS name. This uses the name switch in the
2837 smb.conf to determine the order of name resolution.
2838 *********************************************************/
2840 bool resolve_name(const char *name,
2841 struct sockaddr_storage *return_ss,
2842 int name_type,
2843 bool prefer_ipv4)
2845 struct ip_service *ss_list = NULL;
2846 char *sitename = NULL;
2847 int count = 0;
2848 NTSTATUS status;
2850 if (is_ipaddress(name)) {
2851 return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
2854 sitename = sitename_fetch(talloc_tos(), lp_realm()); /* wild guess */
2856 status = internal_resolve_name(name, name_type, sitename,
2857 &ss_list, &count,
2858 lp_name_resolve_order());
2859 if (NT_STATUS_IS_OK(status)) {
2860 int i;
2862 if (prefer_ipv4) {
2863 for (i=0; i<count; i++) {
2864 if (!is_zero_addr(&ss_list[i].ss) &&
2865 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss) &&
2866 (ss_list[i].ss.ss_family == AF_INET)) {
2867 *return_ss = ss_list[i].ss;
2868 SAFE_FREE(ss_list);
2869 TALLOC_FREE(sitename);
2870 return True;
2875 /* only return valid addresses for TCP connections */
2876 for (i=0; i<count; i++) {
2877 if (!is_zero_addr(&ss_list[i].ss) &&
2878 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2879 *return_ss = ss_list[i].ss;
2880 SAFE_FREE(ss_list);
2881 TALLOC_FREE(sitename);
2882 return True;
2887 SAFE_FREE(ss_list);
2888 TALLOC_FREE(sitename);
2889 return False;
2892 /********************************************************
2893 Internal interface to resolve a name into a list of IP addresses.
2894 Use this function if the string is either an IP address, DNS
2895 or host name or NetBIOS name. This uses the name switch in the
2896 smb.conf to determine the order of name resolution.
2897 *********************************************************/
2899 NTSTATUS resolve_name_list(TALLOC_CTX *ctx,
2900 const char *name,
2901 int name_type,
2902 struct sockaddr_storage **return_ss_arr,
2903 unsigned int *p_num_entries)
2905 struct ip_service *ss_list = NULL;
2906 char *sitename = NULL;
2907 int count = 0;
2908 int i;
2909 unsigned int num_entries;
2910 NTSTATUS status;
2912 *p_num_entries = 0;
2913 *return_ss_arr = NULL;
2915 if (is_ipaddress(name)) {
2916 *return_ss_arr = talloc(ctx, struct sockaddr_storage);
2917 if (!*return_ss_arr) {
2918 return NT_STATUS_NO_MEMORY;
2920 if (!interpret_string_addr(*return_ss_arr, name, AI_NUMERICHOST)) {
2921 TALLOC_FREE(*return_ss_arr);
2922 return NT_STATUS_BAD_NETWORK_NAME;
2924 *p_num_entries = 1;
2925 return NT_STATUS_OK;
2928 sitename = sitename_fetch(ctx, lp_realm()); /* wild guess */
2930 status = internal_resolve_name(name, name_type, sitename,
2931 &ss_list, &count,
2932 lp_name_resolve_order());
2933 TALLOC_FREE(sitename);
2935 if (!NT_STATUS_IS_OK(status)) {
2936 return status;
2939 /* only return valid addresses for TCP connections */
2940 for (i=0, num_entries = 0; i<count; i++) {
2941 if (!is_zero_addr(&ss_list[i].ss) &&
2942 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2943 num_entries++;
2946 if (num_entries == 0) {
2947 SAFE_FREE(ss_list);
2948 return NT_STATUS_BAD_NETWORK_NAME;
2951 *return_ss_arr = talloc_array(ctx,
2952 struct sockaddr_storage,
2953 num_entries);
2954 if (!(*return_ss_arr)) {
2955 SAFE_FREE(ss_list);
2956 return NT_STATUS_NO_MEMORY;
2959 for (i=0, num_entries = 0; i<count; i++) {
2960 if (!is_zero_addr(&ss_list[i].ss) &&
2961 !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
2962 (*return_ss_arr)[num_entries++] = ss_list[i].ss;
2966 status = NT_STATUS_OK;
2967 *p_num_entries = num_entries;
2969 SAFE_FREE(ss_list);
2970 return NT_STATUS_OK;
2973 /********************************************************
2974 Find the IP address of the master browser or DMB for a workgroup.
2975 *********************************************************/
2977 bool find_master_ip(const char *group, struct sockaddr_storage *master_ss)
2979 struct ip_service *ip_list = NULL;
2980 int count = 0;
2981 NTSTATUS status;
2983 if (lp_disable_netbios()) {
2984 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
2985 return false;
2988 status = internal_resolve_name(group, 0x1D, NULL, &ip_list, &count,
2989 lp_name_resolve_order());
2990 if (NT_STATUS_IS_OK(status)) {
2991 *master_ss = ip_list[0].ss;
2992 SAFE_FREE(ip_list);
2993 return true;
2996 status = internal_resolve_name(group, 0x1B, NULL, &ip_list, &count,
2997 lp_name_resolve_order());
2998 if (NT_STATUS_IS_OK(status)) {
2999 *master_ss = ip_list[0].ss;
3000 SAFE_FREE(ip_list);
3001 return true;
3004 SAFE_FREE(ip_list);
3005 return false;
3008 /********************************************************
3009 Get the IP address list of the primary domain controller
3010 for a domain.
3011 *********************************************************/
3013 bool get_pdc_ip(const char *domain, struct sockaddr_storage *pss)
3015 struct ip_service *ip_list = NULL;
3016 int count = 0;
3017 NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
3018 static const char *ads_order[] = { "ads", NULL };
3019 /* Look up #1B name */
3021 if (lp_security() == SEC_ADS) {
3022 status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
3023 &count, ads_order);
3026 if (!NT_STATUS_IS_OK(status) || count == 0) {
3027 status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
3028 &count,
3029 lp_name_resolve_order());
3030 if (!NT_STATUS_IS_OK(status)) {
3031 SAFE_FREE(ip_list);
3032 return false;
3036 /* if we get more than 1 IP back we have to assume it is a
3037 multi-homed PDC and not a mess up */
3039 if ( count > 1 ) {
3040 DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count));
3041 sort_service_list(ip_list, count);
3044 *pss = ip_list[0].ss;
3045 SAFE_FREE(ip_list);
3046 return true;
3049 /* Private enum type for lookups. */
3051 enum dc_lookup_type { DC_NORMAL_LOOKUP, DC_ADS_ONLY, DC_KDC_ONLY };
3053 /********************************************************
3054 Get the IP address list of the domain controllers for
3055 a domain.
3056 *********************************************************/
3058 static NTSTATUS get_dc_list(const char *domain,
3059 const char *sitename,
3060 struct ip_service **ip_list,
3061 int *count,
3062 enum dc_lookup_type lookup_type,
3063 bool *ordered)
3065 const char **resolve_order = NULL;
3066 char *saf_servername = NULL;
3067 char *pserver = NULL;
3068 const char *p;
3069 char *port_str = NULL;
3070 int port;
3071 char *name;
3072 int num_addresses = 0;
3073 int local_count, i, j;
3074 struct ip_service *return_iplist = NULL;
3075 struct ip_service *auto_ip_list = NULL;
3076 bool done_auto_lookup = false;
3077 int auto_count = 0;
3078 NTSTATUS status;
3079 TALLOC_CTX *ctx = talloc_init("get_dc_list");
3080 int auto_name_type = 0x1C;
3082 *ip_list = NULL;
3083 *count = 0;
3085 if (!ctx) {
3086 return NT_STATUS_NO_MEMORY;
3089 *ordered = False;
3091 /* if we are restricted to solely using DNS for looking
3092 up a domain controller, make sure that host lookups
3093 are enabled for the 'name resolve order'. If host lookups
3094 are disabled and ads_only is True, then set the string to
3095 NULL. */
3097 resolve_order = lp_name_resolve_order();
3098 if (!resolve_order) {
3099 status = NT_STATUS_NO_MEMORY;
3100 goto out;
3102 if (lookup_type == DC_ADS_ONLY) {
3103 if (str_list_check_ci(resolve_order, "host")) {
3104 static const char *ads_order[] = { "ads", NULL };
3105 resolve_order = ads_order;
3107 /* DNS SRV lookups used by the ads resolver
3108 are already sorted by priority and weight */
3109 *ordered = true;
3110 } else {
3111 /* this is quite bizarre! */
3112 static const char *null_order[] = { "NULL", NULL };
3113 resolve_order = null_order;
3115 } else if (lookup_type == DC_KDC_ONLY) {
3116 static const char *kdc_order[] = { "kdc", NULL };
3117 /* DNS SRV lookups used by the ads/kdc resolver
3118 are already sorted by priority and weight */
3119 *ordered = true;
3120 resolve_order = kdc_order;
3121 auto_name_type = KDC_NAME_TYPE;
3124 /* fetch the server we have affinity for. Add the
3125 'password server' list to a search for our domain controllers */
3127 saf_servername = saf_fetch(ctx, domain);
3129 if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
3130 pserver = talloc_asprintf(ctx, "%s, %s",
3131 saf_servername ? saf_servername : "",
3132 lp_password_server());
3133 } else {
3134 pserver = talloc_asprintf(ctx, "%s, *",
3135 saf_servername ? saf_servername : "");
3138 TALLOC_FREE(saf_servername);
3139 if (!pserver) {
3140 status = NT_STATUS_NO_MEMORY;
3141 goto out;
3144 /* if we are starting from scratch, just lookup DOMAIN<0x1c> */
3146 if (!*pserver ) {
3147 DEBUG(10,("get_dc_list: no preferred domain controllers.\n"));
3148 status = internal_resolve_name(domain, 0x1C, sitename, ip_list,
3149 count, resolve_order);
3150 goto out;
3153 DEBUG(3,("get_dc_list: preferred server list: \"%s\"\n", pserver ));
3156 * if '*' appears in the "password server" list then add
3157 * an auto lookup to the list of manually configured
3158 * DC's. If any DC is listed by name, then the list should be
3159 * considered to be ordered
3162 p = pserver;
3163 while (next_token_talloc(ctx, &p, &name, LIST_SEP)) {
3164 if (!done_auto_lookup && strequal(name, "*")) {
3165 status = internal_resolve_name(domain, auto_name_type,
3166 sitename,
3167 &auto_ip_list,
3168 &auto_count,
3169 resolve_order);
3170 if (NT_STATUS_IS_OK(status)) {
3171 num_addresses += auto_count;
3173 done_auto_lookup = true;
3174 DEBUG(8,("Adding %d DC's from auto lookup\n",
3175 auto_count));
3176 } else {
3177 num_addresses++;
3181 /* if we have no addresses and haven't done the auto lookup, then
3182 just return the list of DC's. Or maybe we just failed. */
3184 if (num_addresses == 0) {
3185 if (done_auto_lookup) {
3186 DEBUG(4,("get_dc_list: no servers found\n"));
3187 status = NT_STATUS_NO_LOGON_SERVERS;
3188 goto out;
3190 status = internal_resolve_name(domain, auto_name_type,
3191 sitename, ip_list,
3192 count, resolve_order);
3193 goto out;
3196 if ((return_iplist = SMB_MALLOC_ARRAY(struct ip_service,
3197 num_addresses)) == NULL) {
3198 DEBUG(3,("get_dc_list: malloc fail !\n"));
3199 status = NT_STATUS_NO_MEMORY;
3200 goto out;
3203 p = pserver;
3204 local_count = 0;
3206 /* fill in the return list now with real IP's */
3208 while ((local_count<num_addresses) &&
3209 next_token_talloc(ctx, &p, &name, LIST_SEP)) {
3210 struct sockaddr_storage name_ss;
3212 /* copy any addersses from the auto lookup */
3214 if (strequal(name, "*")) {
3215 for (j=0; j<auto_count; j++) {
3216 char addr[INET6_ADDRSTRLEN];
3217 print_sockaddr(addr,
3218 sizeof(addr),
3219 &auto_ip_list[j].ss);
3220 /* Check for and don't copy any
3221 * known bad DC IP's. */
3222 if(!NT_STATUS_IS_OK(check_negative_conn_cache(
3223 domain,
3224 addr))) {
3225 DEBUG(5,("get_dc_list: "
3226 "negative entry %s removed "
3227 "from DC list\n",
3228 addr));
3229 continue;
3231 return_iplist[local_count].ss =
3232 auto_ip_list[j].ss;
3233 return_iplist[local_count].port =
3234 auto_ip_list[j].port;
3235 local_count++;
3237 continue;
3240 /* added support for address:port syntax for ads
3241 * (not that I think anyone will ever run the LDAP
3242 * server in an AD domain on something other than
3243 * port 389
3244 * However, the port should not be used for kerberos
3247 port = (lookup_type == DC_ADS_ONLY) ? LDAP_PORT :
3248 ((lookup_type == DC_KDC_ONLY) ? DEFAULT_KRB5_PORT :
3249 PORT_NONE);
3250 if ((port_str=strchr(name, ':')) != NULL) {
3251 *port_str = '\0';
3252 if (lookup_type != DC_KDC_ONLY) {
3253 port_str++;
3254 port = atoi(port_str);
3258 /* explicit lookup; resolve_name() will
3259 * handle names & IP addresses */
3260 if (resolve_name( name, &name_ss, 0x20, true )) {
3261 char addr[INET6_ADDRSTRLEN];
3262 print_sockaddr(addr,
3263 sizeof(addr),
3264 &name_ss);
3266 /* Check for and don't copy any known bad DC IP's. */
3267 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain,
3268 addr)) ) {
3269 DEBUG(5,("get_dc_list: negative entry %s "
3270 "removed from DC list\n",
3271 name ));
3272 continue;
3275 return_iplist[local_count].ss = name_ss;
3276 return_iplist[local_count].port = port;
3277 local_count++;
3278 *ordered = true;
3282 /* need to remove duplicates in the list if we have any
3283 explicit password servers */
3285 local_count = remove_duplicate_addrs2(return_iplist, local_count );
3287 /* For DC's we always prioritize IPv4 due to W2K3 not
3288 * supporting LDAP, KRB5 or CLDAP over IPv6. */
3290 if (local_count && return_iplist) {
3291 prioritize_ipv4_list(return_iplist, local_count);
3294 if ( DEBUGLEVEL >= 4 ) {
3295 DEBUG(4,("get_dc_list: returning %d ip addresses "
3296 "in an %sordered list\n",
3297 local_count,
3298 *ordered ? "":"un"));
3299 DEBUG(4,("get_dc_list: "));
3300 for ( i=0; i<local_count; i++ ) {
3301 char addr[INET6_ADDRSTRLEN];
3302 print_sockaddr(addr,
3303 sizeof(addr),
3304 &return_iplist[i].ss);
3305 DEBUGADD(4,("%s:%d ", addr, return_iplist[i].port ));
3307 DEBUGADD(4,("\n"));
3310 *ip_list = return_iplist;
3311 *count = local_count;
3313 status = ( *count != 0 ? NT_STATUS_OK : NT_STATUS_NO_LOGON_SERVERS );
3315 out:
3317 if (!NT_STATUS_IS_OK(status)) {
3318 SAFE_FREE(return_iplist);
3319 *ip_list = NULL;
3320 *count = 0;
3323 SAFE_FREE(auto_ip_list);
3324 TALLOC_FREE(ctx);
3325 return status;
3328 /*********************************************************************
3329 Small wrapper function to get the DC list and sort it if neccessary.
3330 *********************************************************************/
3332 NTSTATUS get_sorted_dc_list( const char *domain,
3333 const char *sitename,
3334 struct ip_service **ip_list,
3335 int *count,
3336 bool ads_only )
3338 bool ordered = false;
3339 NTSTATUS status;
3340 enum dc_lookup_type lookup_type = DC_NORMAL_LOOKUP;
3342 *ip_list = NULL;
3343 *count = 0;
3345 DEBUG(8,("get_sorted_dc_list: attempting lookup "
3346 "for name %s (sitename %s)\n",
3347 domain,
3348 sitename ? sitename : "NULL"));
3350 if (ads_only) {
3351 lookup_type = DC_ADS_ONLY;
3354 status = get_dc_list(domain, sitename, ip_list,
3355 count, lookup_type, &ordered);
3356 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_LOGON_SERVERS)
3357 && sitename) {
3358 DEBUG(3,("get_sorted_dc_list: no server for name %s available"
3359 " in site %s, fallback to all servers\n",
3360 domain, sitename));
3361 status = get_dc_list(domain, NULL, ip_list,
3362 count, lookup_type, &ordered);
3365 if (!NT_STATUS_IS_OK(status)) {
3366 SAFE_FREE(*ip_list);
3367 *count = 0;
3368 return status;
3371 /* only sort if we don't already have an ordered list */
3372 if (!ordered) {
3373 sort_service_list(*ip_list, *count);
3376 return NT_STATUS_OK;
3379 /*********************************************************************
3380 Get the KDC list - re-use all the logic in get_dc_list.
3381 *********************************************************************/
3383 NTSTATUS get_kdc_list( const char *realm,
3384 const char *sitename,
3385 struct ip_service **ip_list,
3386 int *count)
3388 bool ordered;
3389 NTSTATUS status;
3391 *count = 0;
3392 *ip_list = NULL;
3394 status = get_dc_list(realm, sitename, ip_list,
3395 count, DC_KDC_ONLY, &ordered);
3397 if (!NT_STATUS_IS_OK(status)) {
3398 SAFE_FREE(*ip_list);
3399 *count = 0;
3400 return status;
3403 /* only sort if we don't already have an ordered list */
3404 if ( !ordered ) {
3405 sort_service_list(*ip_list, *count);
3408 return NT_STATUS_OK;