*/
#include "includes.h"
+#include "../lib/util/tevent_ntstatus.h"
#include "libads/sitename_cache.h"
-#include "libads/dns.h"
-#include "../libcli/netlogon.h"
-#include "librpc/gen_ndr/messaging.h"
+#include "../lib/addns/dnsquery.h"
+#include "../libcli/netlogon/netlogon.h"
#include "lib/async_req/async_sock.h"
+#include "libsmb/nmblib.h"
+#include "../libcli/nbt/libnbt.h"
/* nmbd.c sets this to True. */
bool global_in_nmbd = False;
#define SAFJOINKEY_FMT "SAFJOIN/DOMAIN/%s"
#define SAFJOIN_TTL 3600
-static char *saf_key(const char *domain)
+static char *saf_key(TALLOC_CTX *mem_ctx, const char *domain)
{
- char *keystr;
-
- asprintf_strupper_m(&keystr, SAFKEY_FMT, domain);
-
- return keystr;
+ return talloc_asprintf_strupper_m(mem_ctx, SAFKEY_FMT, domain);
}
-static char *saf_join_key(const char *domain)
+static char *saf_join_key(TALLOC_CTX *mem_ctx, const char *domain)
{
- char *keystr;
-
- asprintf_strupper_m(&keystr, SAFJOINKEY_FMT, domain);
-
- return keystr;
+ return talloc_asprintf_strupper_m(mem_ctx, SAFJOINKEY_FMT, domain);
}
/****************************************************************************
return False;
}
- key = saf_key( domain );
+ key = saf_key(talloc_tos(), domain);
+ if (key == NULL) {
+ DEBUG(1, ("saf_key() failed\n"));
+ return false;
+ }
expire = time( NULL ) + lp_parm_int(-1, "saf","ttl", SAF_TTL);
DEBUG(10,("saf_store: domain = [%s], server = [%s], expire = [%u]\n",
ret = gencache_set( key, servername, expire );
- SAFE_FREE( key );
+ TALLOC_FREE( key );
return ret;
}
return False;
}
- key = saf_join_key( domain );
+ key = saf_join_key(talloc_tos(), domain);
+ if (key == NULL) {
+ DEBUG(1, ("saf_join_key() failed\n"));
+ return false;
+ }
expire = time( NULL ) + lp_parm_int(-1, "saf","join ttl", SAFJOIN_TTL);
DEBUG(10,("saf_join_store: domain = [%s], server = [%s], expire = [%u]\n",
ret = gencache_set( key, servername, expire );
- SAFE_FREE( key );
+ TALLOC_FREE( key );
return ret;
}
return False;
}
- key = saf_join_key(domain);
+ key = saf_join_key(talloc_tos(), domain);
+ if (key == NULL) {
+ DEBUG(1, ("saf_join_key() failed\n"));
+ return false;
+ }
ret = gencache_del(key);
- SAFE_FREE(key);
+ TALLOC_FREE(key);
if (ret) {
DEBUG(10,("saf_delete[join]: domain = [%s]\n", domain ));
}
- key = saf_key(domain);
+ key = saf_key(talloc_tos(), domain);
+ if (key == NULL) {
+ DEBUG(1, ("saf_key() failed\n"));
+ return false;
+ }
ret = gencache_del(key);
- SAFE_FREE(key);
+ TALLOC_FREE(key);
if (ret) {
DEBUG(10,("saf_delete: domain = [%s]\n", domain ));
/****************************************************************************
****************************************************************************/
-char *saf_fetch( const char *domain )
+char *saf_fetch(TALLOC_CTX *mem_ctx, const char *domain )
{
char *server = NULL;
time_t timeout;
return NULL;
}
- key = saf_join_key( domain );
+ key = saf_join_key(talloc_tos(), domain);
+ if (key == NULL) {
+ DEBUG(1, ("saf_join_key() failed\n"));
+ return NULL;
+ }
- ret = gencache_get( key, &server, &timeout );
+ ret = gencache_get( key, mem_ctx, &server, &timeout );
- SAFE_FREE( key );
+ TALLOC_FREE( key );
if ( ret ) {
DEBUG(5,("saf_fetch[join]: Returning \"%s\" for \"%s\" domain\n",
return server;
}
- key = saf_key( domain );
+ key = saf_key(talloc_tos(), domain);
+ if (key == NULL) {
+ DEBUG(1, ("saf_key() failed\n"));
+ return NULL;
+ }
- ret = gencache_get( key, &server, &timeout );
+ ret = gencache_get( key, mem_ctx, &server, &timeout );
- SAFE_FREE( key );
+ TALLOC_FREE( key );
if ( !ret ) {
DEBUG(5,("saf_fetch: failed to find server for \"%s\" domain\n",
return server;
}
+static void set_socket_addr_v4(struct sockaddr_storage *addr)
+{
+ if (!interpret_string_addr(addr, lp_nbt_client_socket_address(),
+ AI_NUMERICHOST|AI_PASSIVE)) {
+ zero_sockaddr(addr);
+ }
+ if (addr->ss_family != AF_INET) {
+ zero_sockaddr(addr);
+ }
+}
+
+static struct in_addr my_socket_addr_v4(void)
+{
+ struct sockaddr_storage my_addr;
+ struct sockaddr_in *in_addr = (struct sockaddr_in *)((char *)&my_addr);
+
+ set_socket_addr_v4(&my_addr);
+ return in_addr->sin_addr;
+}
+
/****************************************************************************
Generate a random trn_id.
****************************************************************************/
if (*num_names == 0)
return NULL;
- ret = TALLOC_ARRAY(mem_ctx, struct node_status,*num_names);
+ ret = talloc_array(mem_ctx, struct node_status,*num_names);
if (!ret)
return NULL;
}
tevent_req_set_callback(subreq, nb_trans_done, req);
-#if !DEBUG_UNEXPECTED
subreq = sendto_send(state, state->ev, state->sock,
state->buf, state->buflen, 0, state->dst_addr);
if (tevent_req_nomem(subreq, req)) {
return;
}
tevent_req_set_callback(subreq, nb_trans_sent, req);
-#endif
}
static void nb_trans_sent(struct tevent_req *subreq)
return NT_STATUS_OK;
}
-/****************************************************************************
- Try and send a request to nmbd to send a packet_struct packet first.
- If this fails, use send_packet().
-**************************************************************************/
-
-static bool send_packet_request(struct packet_struct *p)
-{
- struct messaging_context *msg_ctx = server_messaging_context();
- if (msg_ctx) {
- pid_t nmbd_pid = pidfile_pid("nmbd");
-
- if (nmbd_pid) {
- /* Try nmbd. */
- if (NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
- pid_to_procid(nmbd_pid),
- MSG_SEND_PACKET,
- (uint8_t *)p,
- sizeof(struct packet_struct)))) {
- return true;
- }
- }
- }
-
- return send_packet(p);
-}
-
/****************************************************************************
Do a NBT node status query on an open socket and return an array of
structures holding the returned names or NULL if the query failed.
**************************************************************************/
-NTSTATUS node_status_query(int fd,
- struct nmb_name *name,
- const struct sockaddr_storage *to_ss,
- TALLOC_CTX *mem_ctx,
- struct node_status **names,
- int *num_names,
- struct node_status_extra *extra)
+struct node_status_query_state {
+ struct sockaddr_storage my_addr;
+ struct sockaddr_storage addr;
+ uint8_t buf[1024];
+ ssize_t buflen;
+ struct packet_struct *packet;
+};
+
+static int node_status_query_state_destructor(
+ struct node_status_query_state *s);
+static bool node_status_query_validator(struct packet_struct *p,
+ void *private_data);
+static void node_status_query_done(struct tevent_req *subreq);
+
+struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct nmb_name *name,
+ const struct sockaddr_storage *addr)
{
- bool found=False;
- int retries = 2;
- int retry_time = 2000;
- struct timespec tp;
+ struct tevent_req *req, *subreq;
+ struct node_status_query_state *state;
struct packet_struct p;
- struct packet_struct *p2;
struct nmb_packet *nmb = &p.packet.nmb;
- struct node_status *ret;
+ struct sockaddr_in *in_addr;
- ZERO_STRUCT(p);
+ req = tevent_req_create(mem_ctx, &state,
+ struct node_status_query_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ talloc_set_destructor(state, node_status_query_state_destructor);
- if (to_ss->ss_family != AF_INET) {
+ if (addr->ss_family != AF_INET) {
/* Can't do node status to IPv6 */
- return NT_STATUS_INVALID_ADDRESS;
+ tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
+ return tevent_req_post(req, ev);
}
+
+ state->addr = *addr;
+ in_addr = (struct sockaddr_in *)(void *)&state->addr;
+ in_addr->sin_port = htons(NMB_PORT);
+
+ set_socket_addr_v4(&state->my_addr);
+
+ ZERO_STRUCT(p);
nmb->header.name_trn_id = generate_trn_id();
nmb->header.opcode = 0;
nmb->header.response = false;
nmb->question.question_type = 0x21;
nmb->question.question_class = 0x1;
- p.ip = ((const struct sockaddr_in *)to_ss)->sin_addr;
- p.port = NMB_PORT;
- p.recv_fd = -1;
- p.send_fd = fd;
- p.timestamp = time(NULL);
- p.packet_type = NMB_PACKET;
+ state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
+ &p);
+ if (state->buflen == 0) {
+ tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+ DEBUG(10, ("build_packet failed\n"));
+ return tevent_req_post(req, ev);
+ }
- clock_gettime_mono(&tp);
+ subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, false,
+ state->buf, state->buflen,
+ NMB_PACKET, nmb->header.name_trn_id,
+ node_status_query_validator, NULL);
+ if (tevent_req_nomem(subreq, req)) {
+ DEBUG(10, ("nb_trans_send failed\n"));
+ return tevent_req_post(req, ev);
+ }
+ if (!tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0))) {
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, node_status_query_done, req);
+ return req;
+}
- if (!send_packet_request(&p))
- return NT_STATUS_NOT_FOUND;
+static bool node_status_query_validator(struct packet_struct *p,
+ void *private_data)
+{
+ struct nmb_packet *nmb = &p->packet.nmb;
+ debug_nmb_packet(p);
+
+ if (nmb->header.opcode != 0 ||
+ nmb->header.nm_flags.bcast ||
+ nmb->header.rcode ||
+ !nmb->header.ancount ||
+ nmb->answers->rr_type != 0x21) {
+ /*
+ * XXXX what do we do with this? could be a redirect,
+ * but we'll discard it for the moment
+ */
+ return false;
+ }
+ return true;
+}
- retries--;
+static int node_status_query_state_destructor(
+ struct node_status_query_state *s)
+{
+ if (s->packet != NULL) {
+ free_packet(s->packet);
+ s->packet = NULL;
+ }
+ return 0;
+}
- while (1) {
- struct timespec tp2;
- clock_gettime_mono(&tp2);
- if (nsec_time_diff(&tp2,&tp)/1000000 > retry_time) {
- if (!retries)
- break;
- if (!found && !send_packet_request(&p))
- return NT_STATUS_NOT_FOUND;
- clock_gettime_mono(&tp);
- retries--;
- }
+static void node_status_query_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct node_status_query_state *state = tevent_req_data(
+ req, struct node_status_query_state);
+ NTSTATUS status;
- if ((p2=receive_nmb_packet(fd,90,nmb->header.name_trn_id))) {
- struct nmb_packet *nmb2 = &p2->packet.nmb;
- debug_nmb_packet(p2);
-
- if (nmb2->header.opcode != 0 ||
- nmb2->header.nm_flags.bcast ||
- nmb2->header.rcode ||
- !nmb2->header.ancount ||
- nmb2->answers->rr_type != 0x21) {
- /* XXXX what do we do with this? could be a
- redirect, but we'll discard it for the
- moment */
- free_packet(p2);
- continue;
- }
+ status = nb_trans_recv(subreq, &state->packet);
+ TALLOC_FREE(subreq);
+ if (tevent_req_nterror(req, status)) {
+ return;
+ }
+ tevent_req_done(req);
+}
- ret = parse_node_status(
- mem_ctx, &nmb2->answers->rdata[0], num_names,
- extra);
- free_packet(p2);
+NTSTATUS node_status_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ struct node_status **pnode_status,
+ int *pnum_names,
+ struct node_status_extra *extra)
+{
+ struct node_status_query_state *state = tevent_req_data(
+ req, struct node_status_query_state);
+ struct node_status *node_status;
+ int num_names;
+ NTSTATUS status;
- if (ret == NULL) {
- return NT_STATUS_NO_MEMORY;
- }
- *names = ret;
- return NT_STATUS_OK;
- }
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+ node_status = parse_node_status(
+ mem_ctx, &state->packet->packet.nmb.answers->rdata[0],
+ &num_names, extra);
+ if (node_status == NULL) {
+ return NT_STATUS_NO_MEMORY;
}
+ *pnode_status = node_status;
+ *pnum_names = num_names;
+ return NT_STATUS_OK;
+}
+
+NTSTATUS node_status_query(TALLOC_CTX *mem_ctx, struct nmb_name *name,
+ const struct sockaddr_storage *addr,
+ struct node_status **pnode_status,
+ int *pnum_names,
+ struct node_status_extra *extra)
+{
+ TALLOC_CTX *frame = talloc_stackframe();
+ struct tevent_context *ev;
+ struct tevent_req *req;
+ NTSTATUS status = NT_STATUS_NO_MEMORY;
- return NT_STATUS_IO_TIMEOUT;
+ ev = samba_tevent_context_init(frame);
+ if (ev == NULL) {
+ goto fail;
+ }
+ req = node_status_query_send(ev, ev, name, addr);
+ if (req == NULL) {
+ goto fail;
+ }
+ if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+ goto fail;
+ }
+ status = node_status_query_recv(req, mem_ctx, pnode_status,
+ pnum_names, extra);
+ fail:
+ TALLOC_FREE(frame);
+ return status;
}
/****************************************************************************
struct node_status *addrs = NULL;
struct nmb_name nname;
int count, i;
- int sock;
bool result = false;
NTSTATUS status;
return false;
}
- if (!interpret_string_addr(&ss, lp_socket_address(),
- AI_NUMERICHOST|AI_PASSIVE)) {
- zero_sockaddr(&ss);
- }
-
- sock = open_socket_in(SOCK_DGRAM, 0, 3, &ss, True);
- if (sock == -1)
- goto done;
+ set_socket_addr_v4(&ss);
/* W2K PDC's seem not to respond to '*'#0. JRA */
make_nmb_name(&nname, q_name, q_type);
- status = node_status_query(sock, &nname, to_ss, talloc_tos(),
+ status = node_status_query(talloc_tos(), &nname, to_ss,
&addrs, &count, NULL);
- close(sock);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
for (i=0;i<num_interfaces;i++) {
const struct sockaddr_storage *pss = iface_n_bcast(i);
- unsigned char *p_ss1 = NULL;
- unsigned char *p_ss2 = NULL;
- unsigned char *p_if = NULL;
+ const unsigned char *p_ss1 = NULL;
+ const unsigned char *p_ss2 = NULL;
+ const unsigned char *p_if = NULL;
size_t len = 0;
int bits1, bits2;
continue;
}
if (pss->ss_family == AF_INET) {
- p_if = (unsigned char *)
+ p_if = (const unsigned char *)
&((const struct sockaddr_in *)pss)->sin_addr;
- p_ss1 = (unsigned char *)
+ p_ss1 = (const unsigned char *)
&((const struct sockaddr_in *)ss1)->sin_addr;
- p_ss2 = (unsigned char *)
+ p_ss2 = (const unsigned char *)
&((const struct sockaddr_in *)ss2)->sin_addr;
len = 4;
}
#if defined(HAVE_IPV6)
if (pss->ss_family == AF_INET6) {
- p_if = (unsigned char *)
+ p_if = (const unsigned char *)
&((const struct sockaddr_in6 *)pss)->sin6_addr;
- p_ss1 = (unsigned char *)
+ p_ss1 = (const unsigned char *)
&((const struct sockaddr_in6 *)ss1)->sin6_addr;
- p_ss2 = (unsigned char *)
+ p_ss2 = (const unsigned char *)
&((const struct sockaddr_in6 *)ss2)->sin6_addr;
len = 16;
}
}
/* Bias towards directly reachable IPs */
- if (iface_local((struct sockaddr *)ss1)) {
+ if (iface_local((const struct sockaddr *)ss1)) {
if (ss1->ss_family == AF_INET) {
max_bits1 += 32;
} else {
max_bits1 += 128;
}
}
- if (iface_local((struct sockaddr *)ss2)) {
+ if (iface_local((const struct sockaddr *)ss2)) {
if (ss2->ss_family == AF_INET) {
max_bits2 += 32;
} else {
Remove any duplicate address/port pairs in the list
*********************************************************************/
-static int remove_duplicate_addrs2(struct ip_service *iplist, int count )
+int remove_duplicate_addrs2(struct ip_service *iplist, int count )
{
int i, j;
DEBUG(10,("remove_duplicate_addrs2: "
"looking for duplicate address/port pairs\n"));
- /* one loop to remove duplicates */
+ /* One loop to set duplicates to a zero addr. */
for ( i=0; i<count; i++ ) {
- if ( is_zero_addr((struct sockaddr *)&iplist[i].ss)) {
+ if ( is_zero_addr(&iplist[i].ss)) {
continue;
}
for ( j=i+1; j<count; j++ ) {
- if (sockaddr_equal((struct sockaddr *)&iplist[i].ss, (struct sockaddr *)&iplist[j].ss) &&
+ if (sockaddr_equal((struct sockaddr *)(void *)&iplist[i].ss,
+ (struct sockaddr *)(void *)&iplist[j].ss) &&
iplist[i].port == iplist[j].port) {
zero_sockaddr(&iplist[j].ss);
}
}
}
- /* one loop to clean up any holes we left */
- /* first ip should never be a zero_ip() */
- for (i = 0; i<count; ) {
- if (is_zero_addr((struct sockaddr *)&iplist[i].ss) ) {
- if (i != count-1) {
- memmove(&iplist[i], &iplist[i+1],
- (count - i - 1)*sizeof(iplist[i]));
+ /* Now remove any addresses set to zero above. */
+ for (i = 0; i < count; i++) {
+ while (i < count &&
+ is_zero_addr(&iplist[i].ss)) {
+ if (count-i-1>0) {
+ memmove(&iplist[i],
+ &iplist[i+1],
+ (count-i-1)*sizeof(struct ip_service));
}
count--;
- continue;
}
- i++;
}
return count;
static bool prioritize_ipv4_list(struct ip_service *iplist, int count)
{
TALLOC_CTX *frame = talloc_stackframe();
- struct ip_service *iplist_new = TALLOC_ARRAY(frame, struct ip_service, count);
+ struct ip_service *iplist_new = talloc_array(frame, struct ip_service, count);
int i, j;
if (iplist_new == NULL) {
*timed_out is set if we failed by timing out
****************************************************************************/
-NTSTATUS name_query(int fd,
- const char *name,
- int name_type,
- bool bcast,
- bool recurse,
- const struct sockaddr_storage *to_ss,
- TALLOC_CTX *mem_ctx,
- struct sockaddr_storage **addrs,
- int *count,
- int *flags,
- bool *timed_out)
+struct name_query_state {
+ struct sockaddr_storage my_addr;
+ struct sockaddr_storage addr;
+ bool bcast;
+
+
+ uint8_t buf[1024];
+ ssize_t buflen;
+
+ NTSTATUS validate_error;
+ uint8_t flags;
+
+ struct sockaddr_storage *addrs;
+ int num_addrs;
+};
+
+static bool name_query_validator(struct packet_struct *p, void *private_data);
+static void name_query_done(struct tevent_req *subreq);
+
+struct tevent_req *name_query_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ const char *name, int name_type,
+ bool bcast, bool recurse,
+ const struct sockaddr_storage *addr)
{
- bool found=false;
- int i, retries = 3;
- int retry_time = bcast?250:2000;
- struct timespec tp;
+ struct tevent_req *req, *subreq;
+ struct name_query_state *state;
struct packet_struct p;
- struct packet_struct *p2;
struct nmb_packet *nmb = &p.packet.nmb;
- struct sockaddr_storage *ss_list = NULL;
+ struct sockaddr_in *in_addr;
- if (lp_disable_netbios()) {
- DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
- name, name_type));
- return NT_STATUS_NOT_FOUND;
+ req = tevent_req_create(mem_ctx, &state, struct name_query_state);
+ if (req == NULL) {
+ return NULL;
}
+ state->bcast = bcast;
- if (to_ss->ss_family != AF_INET) {
- return NT_STATUS_INVALID_ADDRESS;
+ if (addr->ss_family != AF_INET) {
+ /* Can't do node status to IPv6 */
+ tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
+ return tevent_req_post(req, ev);
}
- if (timed_out) {
- *timed_out = false;
+ if (lp_disable_netbios()) {
+ DEBUG(5,("name_query(%s#%02x): netbios is disabled\n",
+ name, name_type));
+ tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
+ return tevent_req_post(req, ev);
}
- memset((char *)&p,'\0',sizeof(p));
- (*count) = 0;
- (*flags) = 0;
+ state->addr = *addr;
+ in_addr = (struct sockaddr_in *)(void *)&state->addr;
+ in_addr->sin_port = htons(NMB_PORT);
+
+ set_socket_addr_v4(&state->my_addr);
+ ZERO_STRUCT(p);
nmb->header.name_trn_id = generate_trn_id();
nmb->header.opcode = 0;
nmb->header.response = false;
nmb->question.question_type = 0x20;
nmb->question.question_class = 0x1;
- p.ip = ((struct sockaddr_in *)to_ss)->sin_addr;
- p.port = NMB_PORT;
- p.recv_fd = -1;
- p.send_fd = fd;
- p.timestamp = time(NULL);
- p.packet_type = NMB_PACKET;
-
- clock_gettime_mono(&tp);
+ state->buflen = build_packet((char *)state->buf, sizeof(state->buf),
+ &p);
+ if (state->buflen == 0) {
+ tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+ DEBUG(10, ("build_packet failed\n"));
+ return tevent_req_post(req, ev);
+ }
- if (!send_packet_request(&p))
- return NT_STATUS_NOT_FOUND;
+ subreq = nb_trans_send(state, ev, &state->my_addr, &state->addr, bcast,
+ state->buf, state->buflen,
+ NMB_PACKET, nmb->header.name_trn_id,
+ name_query_validator, state);
+ if (tevent_req_nomem(subreq, req)) {
+ DEBUG(10, ("nb_trans_send failed\n"));
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, name_query_done, req);
+ return req;
+}
- retries--;
+static bool name_query_validator(struct packet_struct *p, void *private_data)
+{
+ struct name_query_state *state = talloc_get_type_abort(
+ private_data, struct name_query_state);
+ struct nmb_packet *nmb = &p->packet.nmb;
+ struct sockaddr_storage *tmp_addrs;
+ bool got_unique_netbios_name = false;
+ int i;
- while (1) {
- struct timespec tp2;
+ debug_nmb_packet(p);
- clock_gettime_mono(&tp2);
- if (nsec_time_diff(&tp2,&tp)/1000000 > retry_time) {
- if (!retries)
+ /*
+ * If we get a Negative Name Query Response from a WINS
+ * server, we should report it and give up.
+ */
+ if( 0 == nmb->header.opcode /* A query response */
+ && !state->bcast /* from a WINS server */
+ && nmb->header.rcode /* Error returned */
+ ) {
+
+ if( DEBUGLVL( 3 ) ) {
+ /* Only executed if DEBUGLEVEL >= 3 */
+ dbgtext( "Negative name query "
+ "response, rcode 0x%02x: ",
+ nmb->header.rcode );
+ switch( nmb->header.rcode ) {
+ case 0x01:
+ dbgtext("Request was invalidly formatted.\n");
+ break;
+ case 0x02:
+ dbgtext("Problem with NBNS, cannot process "
+ "name.\n");
+ break;
+ case 0x03:
+ dbgtext("The name requested does not "
+ "exist.\n");
+ break;
+ case 0x04:
+ dbgtext("Unsupported request error.\n");
+ break;
+ case 0x05:
+ dbgtext("Query refused error.\n");
+ break;
+ default:
+ dbgtext("Unrecognized error code.\n" );
break;
- if (!found && !send_packet_request(&p))
- return NT_STATUS_NOT_FOUND;
- clock_gettime_mono(&tp);
- retries--;
- }
- if ((p2=receive_nmb_packet(fd,90,nmb->header.name_trn_id))) {
- struct nmb_packet *nmb2 = &p2->packet.nmb;
- debug_nmb_packet(p2);
-
- /* If we get a Negative Name Query Response from a WINS
- * server, we should report it and give up.
- */
- if( 0 == nmb2->header.opcode /* A query response */
- && !(bcast) /* from a WINS server */
- && nmb2->header.rcode /* Error returned */
- ) {
-
- if( DEBUGLVL( 3 ) ) {
- /* Only executed if DEBUGLEVEL >= 3 */
- dbgtext( "Negative name query "
- "response, rcode 0x%02x: ",
- nmb2->header.rcode );
- switch( nmb2->header.rcode ) {
- case 0x01:
- dbgtext( "Request "
- "was invalidly formatted.\n" );
- break;
- case 0x02:
- dbgtext( "Problem with NBNS, "
- "cannot process name.\n");
- break;
- case 0x03:
- dbgtext( "The name requested "
- "does not exist.\n" );
- break;
- case 0x04:
- dbgtext( "Unsupported request "
- "error.\n" );
- break;
- case 0x05:
- dbgtext( "Query refused "
- "error.\n" );
- break;
- default:
- dbgtext( "Unrecognized error "
- "code.\n" );
- break;
- }
- }
- free_packet(p2);
- return NT_STATUS_NOT_FOUND;
- }
-
- if (nmb2->header.opcode != 0 ||
- nmb2->header.nm_flags.bcast ||
- nmb2->header.rcode ||
- !nmb2->header.ancount) {
- /*
- * XXXX what do we do with this? Could be a
- * redirect, but we'll discard it for the
- * moment.
- */
- free_packet(p2);
- continue;
}
+ }
- ss_list = TALLOC_REALLOC_ARRAY(mem_ctx, ss_list,
- struct sockaddr_storage,
- (*count) +
- nmb2->answers->rdlength/6);
+ /*
+ * We accept this packet as valid, but tell the upper
+ * layers that it's a negative response.
+ */
+ state->validate_error = NT_STATUS_NOT_FOUND;
+ return true;
+ }
- if (!ss_list) {
- DEBUG(0,("name_query: Realloc failed.\n"));
- free_packet(p2);
- return NT_STATUS_NO_MEMORY;
- }
+ if (nmb->header.opcode != 0 ||
+ nmb->header.nm_flags.bcast ||
+ nmb->header.rcode ||
+ !nmb->header.ancount) {
+ /*
+ * XXXX what do we do with this? Could be a redirect,
+ * but we'll discard it for the moment.
+ */
+ return false;
+ }
- DEBUG(2,("Got a positive name query response "
- "from %s ( ",
- inet_ntoa(p2->ip)));
-
- for (i=0;i<nmb2->answers->rdlength/6;i++) {
- struct in_addr ip;
- putip((char *)&ip,&nmb2->answers->rdata[2+i*6]);
- in_addr_to_sockaddr_storage(&ss_list[(*count)],
- ip);
- DEBUGADD(2,("%s ",inet_ntoa(ip)));
- (*count)++;
- }
- DEBUGADD(2,(")\n"));
-
- found=true;
- retries=0;
- /* We add the flags back ... */
- if (nmb2->header.response)
- (*flags) |= NM_FLAGS_RS;
- if (nmb2->header.nm_flags.authoritative)
- (*flags) |= NM_FLAGS_AA;
- if (nmb2->header.nm_flags.trunc)
- (*flags) |= NM_FLAGS_TC;
- if (nmb2->header.nm_flags.recursion_desired)
- (*flags) |= NM_FLAGS_RD;
- if (nmb2->header.nm_flags.recursion_available)
- (*flags) |= NM_FLAGS_RA;
- if (nmb2->header.nm_flags.bcast)
- (*flags) |= NM_FLAGS_B;
- free_packet(p2);
- /*
- * If we're doing a unicast lookup we only
- * expect one reply. Don't wait the full 2
- * seconds if we got one. JRA.
- */
- if(!bcast && found)
- break;
- }
+ tmp_addrs = talloc_realloc(
+ state, state->addrs, struct sockaddr_storage,
+ state->num_addrs + nmb->answers->rdlength/6);
+ if (tmp_addrs == NULL) {
+ state->validate_error = NT_STATUS_NO_MEMORY;
+ return true;
}
+ state->addrs = tmp_addrs;
- /* only set timed_out if we didn't fund what we where looking for*/
+ DEBUG(2,("Got a positive name query response "
+ "from %s ( ", inet_ntoa(p->ip)));
- if ( !found && timed_out ) {
- *timed_out = true;
- }
+ for (i=0; i<nmb->answers->rdlength/6; i++) {
+ uint16_t flags;
+ struct in_addr ip;
+ struct sockaddr_storage addr;
+ int j;
- /* sort the ip list so we choose close servers first if possible */
- sort_addr_list(ss_list, *count);
+ flags = RSVAL(&nmb->answers->rdata[i*6], 0);
+ got_unique_netbios_name |= ((flags & 0x8000) == 0);
- *addrs = ss_list;
- return NT_STATUS_OK;
-}
+ putip((char *)&ip,&nmb->answers->rdata[2+i*6]);
+ in_addr_to_sockaddr_storage(&addr, ip);
-/********************************************************
- convert an array if struct sockaddr_storage to struct ip_service
- return false on failure. Port is set to PORT_NONE;
-*********************************************************/
+ if (is_zero_addr(&addr)) {
+ continue;
+ }
-static bool convert_ss2service(struct ip_service **return_iplist,
- const struct sockaddr_storage *ss_list,
- int count)
-{
- int i;
+ for (j=0; j<state->num_addrs; j++) {
+ if (sockaddr_equal(
+ (struct sockaddr *)(void *)&addr,
+ (struct sockaddr *)(void *)&state->addrs[j])) {
+ break;
+ }
+ }
+ if (j < state->num_addrs) {
+ /* Already got it */
+ continue;
+ }
- if ( count==0 || !ss_list )
- return False;
+ DEBUGADD(2,("%s ",inet_ntoa(ip)));
- /* copy the ip address; port will be PORT_NONE */
- if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, count)) ==
- NULL) {
- DEBUG(0,("convert_ip2service: malloc failed "
- "for %d enetries!\n", count ));
- return False;
+ state->addrs[state->num_addrs] = addr;
+ state->num_addrs += 1;
}
+ DEBUGADD(2,(")\n"));
- for ( i=0; i<count; i++ ) {
- (*return_iplist)[i].ss = ss_list[i];
- (*return_iplist)[i].port = PORT_NONE;
- }
+ /* We add the flags back ... */
+ if (nmb->header.response)
+ state->flags |= NM_FLAGS_RS;
+ if (nmb->header.nm_flags.authoritative)
+ state->flags |= NM_FLAGS_AA;
+ if (nmb->header.nm_flags.trunc)
+ state->flags |= NM_FLAGS_TC;
+ if (nmb->header.nm_flags.recursion_desired)
+ state->flags |= NM_FLAGS_RD;
+ if (nmb->header.nm_flags.recursion_available)
+ state->flags |= NM_FLAGS_RA;
+ if (nmb->header.nm_flags.bcast)
+ state->flags |= NM_FLAGS_B;
+ if (state->bcast) {
+ /*
+ * We have to collect all entries coming in from broadcast
+ * queries. If we got a unique name, we're done.
+ */
+ return got_unique_netbios_name;
+ }
+ /*
+ * WINS responses are accepted when they are received
+ */
return true;
}
-/********************************************************
- Resolve via "bcast" method.
-*********************************************************/
-
-NTSTATUS name_resolve_bcast(const char *name,
- int name_type,
- struct ip_service **return_iplist,
- int *return_count)
+static void name_query_done(struct tevent_req *subreq)
{
- int sock, i;
- int num_interfaces = iface_count();
- struct sockaddr_storage *ss_list;
- struct sockaddr_storage ss;
- NTSTATUS status = NT_STATUS_NOT_FOUND;
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct name_query_state *state = tevent_req_data(
+ req, struct name_query_state);
+ NTSTATUS status;
+ struct packet_struct *p = NULL;
- if (lp_disable_netbios()) {
- DEBUG(5,("name_resolve_bcast(%s#%02x): netbios is disabled\n",
- name, name_type));
- return NT_STATUS_INVALID_PARAMETER;
+ status = nb_trans_recv(subreq, &p);
+ TALLOC_FREE(subreq);
+ if (tevent_req_nterror(req, status)) {
+ return;
+ }
+ if (!NT_STATUS_IS_OK(state->validate_error)) {
+ tevent_req_nterror(req, state->validate_error);
+ return;
+ }
+ if (p != NULL) {
+ /*
+ * Free the packet here, we've collected the response in the
+ * validator
+ */
+ free_packet(p);
+ }
+ tevent_req_done(req);
+}
+
+NTSTATUS name_query_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ struct sockaddr_storage **addrs, int *num_addrs,
+ uint8_t *flags)
+{
+ struct name_query_state *state = tevent_req_data(
+ req, struct name_query_state);
+ NTSTATUS status;
+
+ if (tevent_req_is_nterror(req, &status)) {
+ if (state->bcast &&
+ NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
+ /*
+ * In the broadcast case we collect replies until the
+ * timeout.
+ */
+ status = NT_STATUS_OK;
+ }
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+ }
+ if (state->num_addrs == 0) {
+ return NT_STATUS_NOT_FOUND;
+ }
+ *addrs = talloc_move(mem_ctx, &state->addrs);
+ sort_addr_list(*addrs, state->num_addrs);
+ *num_addrs = state->num_addrs;
+ if (flags != NULL) {
+ *flags = state->flags;
+ }
+ return NT_STATUS_OK;
+}
+
+NTSTATUS name_query(const char *name, int name_type,
+ bool bcast, bool recurse,
+ const struct sockaddr_storage *to_ss,
+ TALLOC_CTX *mem_ctx,
+ struct sockaddr_storage **addrs,
+ int *num_addrs, uint8_t *flags)
+{
+ TALLOC_CTX *frame = talloc_stackframe();
+ struct tevent_context *ev;
+ struct tevent_req *req;
+ struct timeval timeout;
+ NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+ ev = samba_tevent_context_init(frame);
+ if (ev == NULL) {
+ goto fail;
+ }
+ req = name_query_send(ev, ev, name, name_type, bcast, recurse, to_ss);
+ if (req == NULL) {
+ goto fail;
+ }
+ if (bcast) {
+ timeout = timeval_current_ofs(0, 250000);
+ } else {
+ timeout = timeval_current_ofs(2, 0);
+ }
+ if (!tevent_req_set_endtime(req, ev, timeout)) {
+ goto fail;
+ }
+ if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+ goto fail;
+ }
+ status = name_query_recv(req, mem_ctx, addrs, num_addrs, flags);
+ fail:
+ TALLOC_FREE(frame);
+ return status;
+}
+
+/********************************************************
+ Convert an array if struct sockaddr_storage to struct ip_service
+ return false on failure. Port is set to PORT_NONE;
+ pcount is [in/out] - it is the length of ss_list on input,
+ and the length of return_iplist on output as we remove any
+ zero addresses from ss_list.
+*********************************************************/
+
+static bool convert_ss2service(struct ip_service **return_iplist,
+ const struct sockaddr_storage *ss_list,
+ int *pcount)
+{
+ int i;
+ int orig_count = *pcount;
+ int real_count = 0;
+
+ if (orig_count==0 || !ss_list )
+ return False;
+
+ /* Filter out zero addrs. */
+ for ( i=0; i<orig_count; i++ ) {
+ if (is_zero_addr(&ss_list[i])) {
+ continue;
+ }
+ real_count++;
+ }
+ if (real_count==0) {
+ return false;
+ }
+
+ /* copy the ip address; port will be PORT_NONE */
+ if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, real_count)) ==
+ NULL) {
+ DEBUG(0,("convert_ip2service: malloc failed "
+ "for %d enetries!\n", real_count ));
+ return False;
+ }
+
+ for ( i=0, real_count = 0; i<orig_count; i++ ) {
+ if (is_zero_addr(&ss_list[i])) {
+ continue;
+ }
+ (*return_iplist)[real_count].ss = ss_list[i];
+ (*return_iplist)[real_count].port = PORT_NONE;
+ real_count++;
+ }
+
+ *pcount = real_count;
+ return true;
+}
+
+struct name_queries_state {
+ struct tevent_context *ev;
+ const char *name;
+ int name_type;
+ bool bcast;
+ bool recurse;
+ const struct sockaddr_storage *addrs;
+ int num_addrs;
+ int wait_msec;
+ int timeout_msec;
+
+ struct tevent_req **subreqs;
+ int num_received;
+ int num_sent;
+
+ int received_index;
+ struct sockaddr_storage *result_addrs;
+ int num_result_addrs;
+ uint8_t flags;
+};
+
+static void name_queries_done(struct tevent_req *subreq);
+static void name_queries_next(struct tevent_req *subreq);
+
+/*
+ * Send a name query to multiple destinations with a wait time in between
+ */
+
+static struct tevent_req *name_queries_send(
+ TALLOC_CTX *mem_ctx, struct tevent_context *ev,
+ const char *name, int name_type,
+ bool bcast, bool recurse,
+ const struct sockaddr_storage *addrs,
+ int num_addrs, int wait_msec, int timeout_msec)
+{
+ struct tevent_req *req, *subreq;
+ struct name_queries_state *state;
+
+ req = tevent_req_create(mem_ctx, &state,
+ struct name_queries_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->ev = ev;
+ state->name = name;
+ state->name_type = name_type;
+ state->bcast = bcast;
+ state->recurse = recurse;
+ state->addrs = addrs;
+ state->num_addrs = num_addrs;
+ state->wait_msec = wait_msec;
+ state->timeout_msec = timeout_msec;
+
+ state->subreqs = talloc_zero_array(
+ state, struct tevent_req *, num_addrs);
+ if (tevent_req_nomem(state->subreqs, req)) {
+ return tevent_req_post(req, ev);
+ }
+ state->num_sent = 0;
+
+ subreq = name_query_send(
+ state->subreqs, state->ev, name, name_type, bcast, recurse,
+ &state->addrs[state->num_sent]);
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ if (!tevent_req_set_endtime(
+ subreq, state->ev,
+ timeval_current_ofs(0, state->timeout_msec * 1000))) {
+ tevent_req_oom(req);
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, name_queries_done, req);
+
+ state->subreqs[state->num_sent] = subreq;
+ state->num_sent += 1;
+
+ if (state->num_sent < state->num_addrs) {
+ subreq = tevent_wakeup_send(
+ state, state->ev,
+ timeval_current_ofs(0, state->wait_msec * 1000));
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, name_queries_next, req);
+ }
+ return req;
+}
+
+static void name_queries_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct name_queries_state *state = tevent_req_data(
+ req, struct name_queries_state);
+ int i;
+ NTSTATUS status;
+
+ status = name_query_recv(subreq, state, &state->result_addrs,
+ &state->num_result_addrs, &state->flags);
+
+ for (i=0; i<state->num_sent; i++) {
+ if (state->subreqs[i] == subreq) {
+ break;
+ }
+ }
+ if (i == state->num_sent) {
+ tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+ return;
+ }
+ TALLOC_FREE(state->subreqs[i]);
+
+ state->num_received += 1;
+
+ if (!NT_STATUS_IS_OK(status)) {
+
+ if (state->num_received >= state->num_addrs) {
+ tevent_req_nterror(req, status);
+ return;
+ }
+ /*
+ * Still outstanding requests, just wait
+ */
+ return;
+ }
+ state->received_index = i;
+ tevent_req_done(req);
+}
+
+static void name_queries_next(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct name_queries_state *state = tevent_req_data(
+ req, struct name_queries_state);
+
+ if (!tevent_wakeup_recv(subreq)) {
+ tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+ return;
+ }
+
+ subreq = name_query_send(
+ state->subreqs, state->ev,
+ state->name, state->name_type, state->bcast, state->recurse,
+ &state->addrs[state->num_sent]);
+ if (tevent_req_nomem(subreq, req)) {
+ return;
+ }
+ tevent_req_set_callback(subreq, name_queries_done, req);
+ if (!tevent_req_set_endtime(
+ subreq, state->ev,
+ timeval_current_ofs(0, state->timeout_msec * 1000))) {
+ tevent_req_oom(req);
+ return;
+ }
+ state->subreqs[state->num_sent] = subreq;
+ state->num_sent += 1;
+
+ if (state->num_sent < state->num_addrs) {
+ subreq = tevent_wakeup_send(
+ state, state->ev,
+ timeval_current_ofs(0, state->wait_msec * 1000));
+ if (tevent_req_nomem(subreq, req)) {
+ return;
+ }
+ tevent_req_set_callback(subreq, name_queries_next, req);
+ }
+}
+
+static NTSTATUS name_queries_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ struct sockaddr_storage **result_addrs,
+ int *num_result_addrs, uint8_t *flags,
+ int *received_index)
+{
+ struct name_queries_state *state = tevent_req_data(
+ req, struct name_queries_state);
+ NTSTATUS status;
+
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+
+ if (result_addrs != NULL) {
+ *result_addrs = talloc_move(mem_ctx, &state->result_addrs);
+ }
+ if (num_result_addrs != NULL) {
+ *num_result_addrs = state->num_result_addrs;
+ }
+ if (flags != NULL) {
+ *flags = state->flags;
+ }
+ if (received_index != NULL) {
+ *received_index = state->received_index;
+ }
+ return NT_STATUS_OK;
+}
+
+/********************************************************
+ Resolve via "bcast" method.
+*********************************************************/
+
+struct name_resolve_bcast_state {
+ struct sockaddr_storage *addrs;
+ int num_addrs;
+};
+
+static void name_resolve_bcast_done(struct tevent_req *subreq);
+
+struct tevent_req *name_resolve_bcast_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ const char *name,
+ int name_type)
+{
+ struct tevent_req *req, *subreq;
+ struct name_resolve_bcast_state *state;
+ struct sockaddr_storage *bcast_addrs;
+ int i, num_addrs, num_bcast_addrs;
+
+ req = tevent_req_create(mem_ctx, &state,
+ struct name_resolve_bcast_state);
+ if (req == NULL) {
+ return NULL;
+ }
+
+ if (lp_disable_netbios()) {
+ DEBUG(5, ("name_resolve_bcast(%s#%02x): netbios is disabled\n",
+ name, name_type));
+ tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+ return tevent_req_post(req, ev);
}
- *return_iplist = NULL;
- *return_count = 0;
-
/*
* "bcast" means do a broadcast lookup on all the local interfaces.
*/
- DEBUG(3,("name_resolve_bcast: Attempting broadcast lookup "
- "for name %s<0x%x>\n", name, name_type));
+ DEBUG(3, ("name_resolve_bcast: Attempting broadcast lookup "
+ "for name %s<0x%x>\n", name, name_type));
- if (!interpret_string_addr(&ss, lp_socket_address(),
- AI_NUMERICHOST|AI_PASSIVE)) {
- zero_sockaddr(&ss);
- }
-
- sock = open_socket_in( SOCK_DGRAM, 0, 3, &ss, true );
- if (sock == -1) {
- return map_nt_error_from_unix(errno);
+ num_addrs = iface_count();
+ bcast_addrs = talloc_array(state, struct sockaddr_storage, num_addrs);
+ if (tevent_req_nomem(bcast_addrs, req)) {
+ return tevent_req_post(req, ev);
}
- set_socket_options(sock,"SO_BROADCAST");
/*
* Lookup the name on all the interfaces, return on
* the first successful match.
*/
- for( i = num_interfaces-1; i >= 0; i--) {
+ num_bcast_addrs = 0;
+
+ for (i=0; i<num_addrs; i++) {
const struct sockaddr_storage *pss = iface_n_bcast(i);
- int flags;
- /* Done this way to fix compiler error on IRIX 5.x */
- if (!pss) {
+ if (pss->ss_family != AF_INET) {
continue;
}
- status = name_query(sock, name, name_type, true, true, pss,
- talloc_tos(), &ss_list, return_count,
- &flags, NULL);
- if (NT_STATUS_IS_OK(status)) {
- goto success;
- }
+ bcast_addrs[num_bcast_addrs] = *pss;
+ num_bcast_addrs += 1;
}
- /* failed - no response */
+ subreq = name_queries_send(state, ev, name, name_type, true, true,
+ bcast_addrs, num_bcast_addrs, 0, 1000);
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, name_resolve_bcast_done, req);
+ return req;
+}
- close(sock);
- return status;
+static void name_resolve_bcast_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct name_resolve_bcast_state *state = tevent_req_data(
+ req, struct name_resolve_bcast_state);
+ NTSTATUS status;
+
+ status = name_queries_recv(subreq, state,
+ &state->addrs, &state->num_addrs,
+ NULL, NULL);
+ TALLOC_FREE(subreq);
+ if (tevent_req_nterror(req, status)) {
+ return;
+ }
+ tevent_req_done(req);
+}
-success:
+NTSTATUS name_resolve_bcast_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ struct sockaddr_storage **addrs,
+ int *num_addrs)
+{
+ struct name_resolve_bcast_state *state = tevent_req_data(
+ req, struct name_resolve_bcast_state);
+ NTSTATUS status;
- if (!convert_ss2service(return_iplist, ss_list, *return_count) )
- status = NT_STATUS_NO_MEMORY;
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+ *addrs = talloc_move(mem_ctx, &state->addrs);
+ *num_addrs = state->num_addrs;
+ return NT_STATUS_OK;
+}
+
+NTSTATUS name_resolve_bcast(const char *name,
+ int name_type,
+ TALLOC_CTX *mem_ctx,
+ struct sockaddr_storage **return_iplist,
+ int *return_count)
+{
+ TALLOC_CTX *frame = talloc_stackframe();
+ struct tevent_context *ev;
+ struct tevent_req *req;
+ NTSTATUS status = NT_STATUS_NO_MEMORY;
- TALLOC_FREE(ss_list);
- close(sock);
+ ev = samba_tevent_context_init(frame);
+ if (ev == NULL) {
+ goto fail;
+ }
+ req = name_resolve_bcast_send(frame, ev, name, name_type);
+ if (req == NULL) {
+ goto fail;
+ }
+ if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+ goto fail;
+ }
+ status = name_resolve_bcast_recv(req, mem_ctx, return_iplist,
+ return_count);
+ fail:
+ TALLOC_FREE(frame);
return status;
}
-/********************************************************
- Resolve via "wins" method.
-*********************************************************/
+struct query_wins_list_state {
+ struct tevent_context *ev;
+ const char *name;
+ uint8_t name_type;
+ struct in_addr *servers;
+ uint32_t num_servers;
+ struct sockaddr_storage server;
+ uint32_t num_sent;
+
+ struct sockaddr_storage *addrs;
+ int num_addrs;
+ uint8_t flags;
+};
-NTSTATUS resolve_wins(const char *name,
- int name_type,
- struct ip_service **return_iplist,
- int *return_count)
+static void query_wins_list_done(struct tevent_req *subreq);
+
+/*
+ * Query a list of (replicating) wins servers in sequence, call them
+ * dead if they don't reply
+ */
+
+static struct tevent_req *query_wins_list_send(
+ TALLOC_CTX *mem_ctx, struct tevent_context *ev,
+ struct in_addr src_ip, const char *name, uint8_t name_type,
+ struct in_addr *servers, int num_servers)
{
- int sock, t, i;
- char **wins_tags;
- struct sockaddr_storage src_ss, *ss_list = NULL;
- struct in_addr src_ip;
+ struct tevent_req *req, *subreq;
+ struct query_wins_list_state *state;
+
+ req = tevent_req_create(mem_ctx, &state,
+ struct query_wins_list_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->ev = ev;
+ state->name = name;
+ state->name_type = name_type;
+ state->servers = servers;
+ state->num_servers = num_servers;
+
+ if (state->num_servers == 0) {
+ tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
+ return tevent_req_post(req, ev);
+ }
+
+ in_addr_to_sockaddr_storage(
+ &state->server, state->servers[state->num_sent]);
+
+ subreq = name_query_send(state, state->ev,
+ state->name, state->name_type,
+ false, true, &state->server);
+ state->num_sent += 1;
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ if (!tevent_req_set_endtime(subreq, state->ev,
+ timeval_current_ofs(2, 0))) {
+ tevent_req_oom(req);
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, query_wins_list_done, req);
+ return req;
+}
+
+static void query_wins_list_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct query_wins_list_state *state = tevent_req_data(
+ req, struct query_wins_list_state);
NTSTATUS status;
- if (lp_disable_netbios()) {
- DEBUG(5,("resolve_wins(%s#%02x): netbios is disabled\n",
- name, name_type));
- return NT_STATUS_INVALID_PARAMETER;
+ status = name_query_recv(subreq, state,
+ &state->addrs, &state->num_addrs,
+ &state->flags);
+ TALLOC_FREE(subreq);
+ if (NT_STATUS_IS_OK(status)) {
+ tevent_req_done(req);
+ return;
}
+ if (!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
+ tevent_req_nterror(req, status);
+ return;
+ }
+ wins_srv_died(state->servers[state->num_sent-1],
+ my_socket_addr_v4());
- *return_iplist = NULL;
- *return_count = 0;
+ if (state->num_sent == state->num_servers) {
+ tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
+ return;
+ }
- DEBUG(3,("resolve_wins: Attempting wins lookup for name %s<0x%x>\n",
- name, name_type));
+ in_addr_to_sockaddr_storage(
+ &state->server, state->servers[state->num_sent]);
- if (wins_srv_count() < 1) {
- DEBUG(3,("resolve_wins: WINS server resolution selected "
- "and no WINS servers listed.\n"));
- return NT_STATUS_INVALID_PARAMETER;
+ subreq = name_query_send(state, state->ev,
+ state->name, state->name_type,
+ false, true, &state->server);
+ state->num_sent += 1;
+ if (tevent_req_nomem(subreq, req)) {
+ return;
}
+ if (!tevent_req_set_endtime(subreq, state->ev,
+ timeval_current_ofs(2, 0))) {
+ tevent_req_oom(req);
+ return;
+ }
+ tevent_req_set_callback(subreq, query_wins_list_done, req);
+}
- /* we try a lookup on each of the WINS tags in turn */
- wins_tags = wins_srv_tags();
+static NTSTATUS query_wins_list_recv(struct tevent_req *req,
+ TALLOC_CTX *mem_ctx,
+ struct sockaddr_storage **addrs,
+ int *num_addrs,
+ uint8_t *flags)
+{
+ struct query_wins_list_state *state = tevent_req_data(
+ req, struct query_wins_list_state);
+ NTSTATUS status;
- if (!wins_tags) {
- /* huh? no tags?? give up in disgust */
- return NT_STATUS_INVALID_PARAMETER;
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+ if (addrs != NULL) {
+ *addrs = talloc_move(mem_ctx, &state->addrs);
+ }
+ if (num_addrs != NULL) {
+ *num_addrs = state->num_addrs;
+ }
+ if (flags != NULL) {
+ *flags = state->flags;
+ }
+ return NT_STATUS_OK;
+}
+
+struct resolve_wins_state {
+ int num_sent;
+ int num_received;
+
+ struct sockaddr_storage *addrs;
+ int num_addrs;
+ uint8_t flags;
+};
+
+static void resolve_wins_done(struct tevent_req *subreq);
+
+struct tevent_req *resolve_wins_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ const char *name,
+ int name_type)
+{
+ struct tevent_req *req, *subreq;
+ struct resolve_wins_state *state;
+ char **wins_tags = NULL;
+ struct sockaddr_storage src_ss;
+ struct in_addr src_ip;
+ int i, num_wins_tags;
+
+ req = tevent_req_create(mem_ctx, &state,
+ struct resolve_wins_state);
+ if (req == NULL) {
+ return NULL;
+ }
+
+ if (wins_srv_count() < 1) {
+ DEBUG(3,("resolve_wins: WINS server resolution selected "
+ "and no WINS servers listed.\n"));
+ tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+ goto fail;
}
/* the address we will be sending from */
- if (!interpret_string_addr(&src_ss, lp_socket_address(),
+ if (!interpret_string_addr(&src_ss, lp_nbt_client_socket_address(),
AI_NUMERICHOST|AI_PASSIVE)) {
zero_sockaddr(&src_ss);
}
DEBUG(3,("resolve_wins: cannot receive WINS replies "
"on IPv6 address %s\n",
addr));
- wins_srv_tags_free(wins_tags);
- return NT_STATUS_INVALID_PARAMETER;
+ tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+ goto fail;
}
- src_ip = ((struct sockaddr_in *)&src_ss)->sin_addr;
+ src_ip = ((const struct sockaddr_in *)(void *)&src_ss)->sin_addr;
- /* in the worst case we will try every wins server with every
- tag! */
- for (t=0; wins_tags && wins_tags[t]; t++) {
- int srv_count = wins_srv_count_tag(wins_tags[t]);
- for (i=0; i<srv_count; i++) {
- struct sockaddr_storage wins_ss;
- struct in_addr wins_ip;
- int flags;
- bool timed_out;
+ wins_tags = wins_srv_tags();
+ if (wins_tags == NULL) {
+ tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+ goto fail;
+ }
+
+ num_wins_tags = 0;
+ while (wins_tags[num_wins_tags] != NULL) {
+ num_wins_tags += 1;
+ }
- wins_ip = wins_srv_ip_tag(wins_tags[t], src_ip);
+ for (i=0; i<num_wins_tags; i++) {
+ int num_servers, num_alive;
+ struct in_addr *servers, *alive;
+ int j;
+
+ if (!wins_server_tag_ips(wins_tags[i], talloc_tos(),
+ &servers, &num_servers)) {
+ DEBUG(10, ("wins_server_tag_ips failed for tag %s\n",
+ wins_tags[i]));
+ continue;
+ }
+
+ alive = talloc_array(state, struct in_addr, num_servers);
+ if (tevent_req_nomem(alive, req)) {
+ goto fail;
+ }
+
+ num_alive = 0;
+ for (j=0; j<num_servers; j++) {
+ struct in_addr wins_ip = servers[j];
if (global_in_nmbd && ismyip_v4(wins_ip)) {
/* yikes! we'll loop forever */
continue;
}
-
/* skip any that have been unresponsive lately */
if (wins_srv_is_dead(wins_ip, src_ip)) {
continue;
}
+ DEBUG(3, ("resolve_wins: using WINS server %s "
+ "and tag '%s'\n",
+ inet_ntoa(wins_ip), wins_tags[i]));
+ alive[num_alive] = wins_ip;
+ num_alive += 1;
+ }
+ TALLOC_FREE(servers);
- DEBUG(3,("resolve_wins: using WINS server %s "
- "and tag '%s'\n",
- inet_ntoa(wins_ip), wins_tags[t]));
+ if (num_alive == 0) {
+ continue;
+ }
- sock = open_socket_in(SOCK_DGRAM, 0, 3, &src_ss, true);
- if (sock == -1) {
- continue;
- }
+ subreq = query_wins_list_send(
+ state, ev, src_ip, name, name_type,
+ alive, num_alive);
+ if (tevent_req_nomem(subreq, req)) {
+ goto fail;
+ }
+ tevent_req_set_callback(subreq, resolve_wins_done, req);
+ state->num_sent += 1;
+ }
- in_addr_to_sockaddr_storage(&wins_ss, wins_ip);
- status = name_query(sock,
- name,
- name_type,
- false,
- true,
- &wins_ss,
- talloc_tos(),
- &ss_list,
- return_count,
- &flags,
- &timed_out);
-
- /* exit loop if we got a list of addresses */
+ if (state->num_sent == 0) {
+ tevent_req_nterror(req, NT_STATUS_NOT_FOUND);
+ goto fail;
+ }
- if (NT_STATUS_IS_OK(status)) {
- goto success;
- }
+ wins_srv_tags_free(wins_tags);
+ return req;
+fail:
+ wins_srv_tags_free(wins_tags);
+ return tevent_req_post(req, ev);
+}
- close(sock);
-
- if (timed_out) {
- /* Timed out waiting for WINS server to
- * respond.
- * Mark it dead. */
- wins_srv_died(wins_ip, src_ip);
- } else {
- /* The name definitely isn't in this
- group of WINS servers.
- goto the next group */
- break;
- }
- }
+static void resolve_wins_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct resolve_wins_state *state = tevent_req_data(
+ req, struct resolve_wins_state);
+ NTSTATUS status;
+
+ status = query_wins_list_recv(subreq, state, &state->addrs,
+ &state->num_addrs, &state->flags);
+ if (NT_STATUS_IS_OK(status)) {
+ tevent_req_done(req);
+ return;
}
- wins_srv_tags_free(wins_tags);
- return NT_STATUS_NO_LOGON_SERVERS;
+ state->num_received += 1;
-success:
+ if (state->num_received < state->num_sent) {
+ /*
+ * Wait for the others
+ */
+ return;
+ }
+ tevent_req_nterror(req, status);
+}
- status = NT_STATUS_OK;
- if (!convert_ss2service(return_iplist, ss_list, *return_count))
- status = NT_STATUS_INVALID_PARAMETER;
+NTSTATUS resolve_wins_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ struct sockaddr_storage **addrs,
+ int *num_addrs, uint8_t *flags)
+{
+ struct resolve_wins_state *state = tevent_req_data(
+ req, struct resolve_wins_state);
+ NTSTATUS status;
- TALLOC_FREE(ss_list);
- wins_srv_tags_free(wins_tags);
- close(sock);
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+ if (addrs != NULL) {
+ *addrs = talloc_move(mem_ctx, &state->addrs);
+ }
+ if (num_addrs != NULL) {
+ *num_addrs = state->num_addrs;
+ }
+ if (flags != NULL) {
+ *flags = state->flags;
+ }
+ return NT_STATUS_OK;
+}
+/********************************************************
+ Resolve via "wins" method.
+*********************************************************/
+
+NTSTATUS resolve_wins(const char *name,
+ int name_type,
+ TALLOC_CTX *mem_ctx,
+ struct sockaddr_storage **return_iplist,
+ int *return_count)
+{
+ struct tevent_context *ev;
+ struct tevent_req *req;
+ NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+ ev = samba_tevent_context_init(talloc_tos());
+ if (ev == NULL) {
+ goto fail;
+ }
+ req = resolve_wins_send(ev, ev, name, name_type);
+ if (req == NULL) {
+ goto fail;
+ }
+ if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+ goto fail;
+ }
+ status = resolve_wins_recv(req, mem_ctx, return_iplist, return_count,
+ NULL);
+fail:
+ TALLOC_FREE(ev);
return status;
}
if (NT_STATUS_IS_OK(status)) {
if (convert_ss2service(return_iplist,
ss_list,
- *return_count)) {
+ return_count)) {
talloc_free(ctx);
return NT_STATUS_OK;
} else {
ZERO_STRUCT(ss);
memcpy(&ss, res->ai_addr, res->ai_addrlen);
+ if (is_zero_addr(&ss)) {
+ continue;
+ }
+
*return_count += 1;
*return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
Resolve via "ADS" method.
*********************************************************/
+/* Special name type used to cause a _kerberos DNS lookup. */
+#define KDC_NAME_TYPE 0xDCDC
+
static NTSTATUS resolve_ads(const char *name,
int name_type,
const char *sitename,
struct ip_service **return_iplist,
int *return_count)
{
- int i, j;
+ int i;
NTSTATUS status;
TALLOC_CTX *ctx;
struct dns_rr_srv *dcs = NULL;
}
/* The DNS code needs fixing to find IPv6 addresses... JRA. */
-
switch (name_type) {
case 0x1b:
DEBUG(5,("resolve_ads: Attempting to resolve "
"PDC for %s using DNS\n", name));
- status = ads_dns_query_pdc(ctx, name, &dcs, &numdcs);
+ status = ads_dns_query_pdc(ctx,
+ name,
+ &dcs,
+ &numdcs);
break;
case 0x1c:
DEBUG(5,("resolve_ads: Attempting to resolve "
"DCs for %s using DNS\n", name));
- status = ads_dns_query_dcs(ctx, name, sitename, &dcs,
+ status = ads_dns_query_dcs(ctx,
+ name,
+ sitename,
+ &dcs,
&numdcs);
break;
case KDC_NAME_TYPE:
DEBUG(5,("resolve_ads: Attempting to resolve "
"KDCs for %s using DNS\n", name));
- status = ads_dns_query_kdcs(ctx, name, sitename, &dcs,
+ status = ads_dns_query_kdcs(ctx,
+ name,
+ sitename,
+ &dcs,
&numdcs);
break;
default:
}
for (i=0;i<numdcs;i++) {
- numaddrs += MAX(dcs[i].num_ips,1);
- }
+ if (!dcs[i].ss_s) {
+ numaddrs += 1;
+ } else {
+ numaddrs += dcs[i].num_ips;
+ }
+ }
if ((*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, numaddrs)) ==
NULL ) {
/* now unroll the list of IP addresses */
*return_count = 0;
- i = 0;
- j = 0;
- while ( i < numdcs && (*return_count<numaddrs) ) {
- struct ip_service *r = &(*return_iplist)[*return_count];
-
- r->port = dcs[i].port;
+ for (i = 0; i < numdcs && (*return_count<numaddrs); i++ ) {
/* If we don't have an IP list for a name, lookup it up */
-
if (!dcs[i].ss_s) {
- interpret_string_addr(&r->ss, dcs[i].hostname, 0);
- i++;
- j = 0;
- } else {
- /* use the IP addresses from the SRV sresponse */
-
- if ( j >= dcs[i].num_ips ) {
- i++;
- j = 0;
+ /* We need to get all IP addresses here. */
+ struct addrinfo *res = NULL;
+ struct addrinfo *p;
+ int extra_addrs = 0;
+
+ if (!interpret_string_addr_internal(&res,
+ dcs[i].hostname,
+ 0)) {
continue;
}
-
- r->ss = dcs[i].ss_s[j];
- j++;
- }
-
- /* make sure it is a valid IP. I considered checking the
- * negative connection cache, but this is the wrong place
- * for it. Maybe only as a hack. After think about it, if
- * all of the IP addresses returned from DNS are dead, what
- * hope does a netbios name lookup have ? The standard reason
- * for falling back to netbios lookups is that our DNS server
- * doesn't know anything about the DC's -- jerry */
-
- if (!is_zero_addr((struct sockaddr *)&r->ss)) {
- (*return_count)++;
+ /* Add in every IP from the lookup. How
+ many is that ? */
+ for (p = res; p; p = p->ai_next) {
+ struct sockaddr_storage ss;
+ memcpy(&ss, p->ai_addr, p->ai_addrlen);
+ if (is_zero_addr(&ss)) {
+ continue;
+ }
+ extra_addrs++;
+ }
+ if (extra_addrs > 1) {
+ /* We need to expand the return_iplist array
+ as we only budgeted for one address. */
+ numaddrs += (extra_addrs-1);
+ *return_iplist = SMB_REALLOC_ARRAY(*return_iplist,
+ struct ip_service,
+ numaddrs);
+ if (*return_iplist == NULL) {
+ if (res) {
+ freeaddrinfo(res);
+ }
+ talloc_destroy(ctx);
+ return NT_STATUS_NO_MEMORY;
+ }
+ }
+ for (p = res; p; p = p->ai_next) {
+ (*return_iplist)[*return_count].port = dcs[i].port;
+ memcpy(&(*return_iplist)[*return_count].ss,
+ p->ai_addr,
+ p->ai_addrlen);
+ if (is_zero_addr(&(*return_iplist)[*return_count].ss)) {
+ continue;
+ }
+ (*return_count)++;
+ /* Should never happen, but still... */
+ if (*return_count>=numaddrs) {
+ break;
+ }
+ }
+ if (res) {
+ freeaddrinfo(res);
+ }
+ } else {
+ /* use all the IP addresses from the SRV sresponse */
+ int j;
+ for (j = 0; j < dcs[i].num_ips; j++) {
+ (*return_iplist)[*return_count].port = dcs[i].port;
+ (*return_iplist)[*return_count].ss = dcs[i].ss_s[j];
+ if (is_zero_addr(&(*return_iplist)[*return_count].ss)) {
+ continue;
+ }
+ (*return_count)++;
+ /* Should never happen, but still... */
+ if (*return_count>=numaddrs) {
+ break;
+ }
+ }
}
}
const char *sitename,
struct ip_service **return_iplist,
int *return_count,
- const char *resolve_order)
+ const char **resolve_order)
{
- char *tok;
- const char *ptr;
+ const char *tok;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
int i;
TALLOC_CTX *frame = NULL;
SAFE_FREE(*return_iplist);
return NT_STATUS_INVALID_PARAMETER;
}
+ if (is_zero_addr(&(*return_iplist)->ss)) {
+ SAFE_FREE(*return_iplist);
+ return NT_STATUS_UNSUCCESSFUL;
+ }
*return_count = 1;
return NT_STATUS_OK;
}
/* Check name cache */
if (namecache_fetch(name, name_type, return_iplist, return_count)) {
+ *return_count = remove_duplicate_addrs2(*return_iplist,
+ *return_count );
/* This could be a negative response */
if (*return_count > 0) {
return NT_STATUS_OK;
/* set the name resolution order */
- if (strcmp( resolve_order, "NULL") == 0) {
+ if (resolve_order && strcmp(resolve_order[0], "NULL") == 0) {
DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
return NT_STATUS_INVALID_PARAMETER;
}
- if (!resolve_order[0]) {
- ptr = "host";
- } else {
- ptr = resolve_order;
+ if (!resolve_order || !resolve_order[0]) {
+ static const char *host_order[] = { "host", NULL };
+ resolve_order = host_order;
}
/* iterate through the name resolution backends */
frame = talloc_stackframe();
- while (next_token_talloc(frame, &ptr, &tok, LIST_SEP)) {
+ for (i=0; resolve_order[i]; i++) {
+ tok = resolve_order[i];
+
if((strequal(tok, "host") || strequal(tok, "hosts"))) {
status = resolve_hosts(name, name_type, return_iplist,
return_count);
}
} else if(strequal( tok, "wins")) {
/* don't resolve 1D via WINS */
+ struct sockaddr_storage *ss_list;
if (name_type != 0x1D) {
status = resolve_wins(name, name_type,
- return_iplist,
+ talloc_tos(),
+ &ss_list,
return_count);
if (NT_STATUS_IS_OK(status)) {
+ if (!convert_ss2service(return_iplist,
+ ss_list,
+ return_count)) {
+ status = NT_STATUS_NO_MEMORY;
+ }
goto done;
}
}
} else if(strequal( tok, "bcast")) {
- status = name_resolve_bcast(name, name_type,
- return_iplist,
- return_count);
+ struct sockaddr_storage *ss_list;
+ status = name_resolve_bcast(
+ name, name_type, talloc_tos(),
+ &ss_list, return_count);
if (NT_STATUS_IS_OK(status)) {
+ if (!convert_ss2service(return_iplist,
+ ss_list,
+ return_count)) {
+ status = NT_STATUS_NO_MEMORY;
+ }
goto done;
}
} else {
controllers including the PDC in iplist[1..n]. Iterating over
the iplist when the PDC is down will cause two sets of timeouts. */
- if ( *return_count ) {
- *return_count = remove_duplicate_addrs2(*return_iplist,
- *return_count );
- }
+ *return_count = remove_duplicate_addrs2(*return_iplist, *return_count );
/* Save in name cache */
if ( DEBUGLEVEL >= 100 ) {
}
}
- namecache_store(name, name_type, *return_count, *return_iplist);
+ if (*return_count) {
+ namecache_store(name, name_type, *return_count, *return_iplist);
+ }
/* Display some debugging info */
struct ip_service *ss_list = NULL;
char *sitename = NULL;
int count = 0;
+ NTSTATUS status;
if (is_ipaddress(name)) {
return interpret_string_addr(return_ss, name, AI_NUMERICHOST);
}
- sitename = sitename_fetch(lp_realm()); /* wild guess */
+ sitename = sitename_fetch(talloc_tos(), lp_realm()); /* wild guess */
- if (NT_STATUS_IS_OK(internal_resolve_name(name, name_type, sitename,
- &ss_list, &count,
- lp_name_resolve_order()))) {
+ status = internal_resolve_name(name, name_type, sitename,
+ &ss_list, &count,
+ lp_name_resolve_order());
+ if (NT_STATUS_IS_OK(status)) {
int i;
if (prefer_ipv4) {
for (i=0; i<count; i++) {
- if (!is_zero_addr((struct sockaddr *)&ss_list[i].ss) &&
- !is_broadcast_addr((struct sockaddr *)&ss_list[i].ss) &&
+ if (!is_zero_addr(&ss_list[i].ss) &&
+ !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss) &&
(ss_list[i].ss.ss_family == AF_INET)) {
*return_ss = ss_list[i].ss;
SAFE_FREE(ss_list);
- SAFE_FREE(sitename);
+ TALLOC_FREE(sitename);
return True;
}
}
/* only return valid addresses for TCP connections */
for (i=0; i<count; i++) {
- if (!is_zero_addr((struct sockaddr *)&ss_list[i].ss) &&
- !is_broadcast_addr((struct sockaddr *)&ss_list[i].ss)) {
+ if (!is_zero_addr(&ss_list[i].ss) &&
+ !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
*return_ss = ss_list[i].ss;
SAFE_FREE(ss_list);
- SAFE_FREE(sitename);
+ TALLOC_FREE(sitename);
return True;
}
}
}
SAFE_FREE(ss_list);
- SAFE_FREE(sitename);
+ TALLOC_FREE(sitename);
return False;
}
*return_ss_arr = NULL;
if (is_ipaddress(name)) {
- *return_ss_arr = TALLOC_P(ctx, struct sockaddr_storage);
+ *return_ss_arr = talloc(ctx, struct sockaddr_storage);
if (!*return_ss_arr) {
return NT_STATUS_NO_MEMORY;
}
return NT_STATUS_OK;
}
- sitename = sitename_fetch(lp_realm()); /* wild guess */
+ sitename = sitename_fetch(ctx, lp_realm()); /* wild guess */
status = internal_resolve_name(name, name_type, sitename,
&ss_list, &count,
lp_name_resolve_order());
- SAFE_FREE(sitename);
+ TALLOC_FREE(sitename);
if (!NT_STATUS_IS_OK(status)) {
return status;
/* only return valid addresses for TCP connections */
for (i=0, num_entries = 0; i<count; i++) {
- if (!is_zero_addr((struct sockaddr *)&ss_list[i].ss) &&
- !is_broadcast_addr((struct sockaddr *)&ss_list[i].ss)) {
+ if (!is_zero_addr(&ss_list[i].ss) &&
+ !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
num_entries++;
}
}
return NT_STATUS_BAD_NETWORK_NAME;
}
- *return_ss_arr = TALLOC_ARRAY(ctx,
+ *return_ss_arr = talloc_array(ctx,
struct sockaddr_storage,
num_entries);
if (!(*return_ss_arr)) {
}
for (i=0, num_entries = 0; i<count; i++) {
- if (!is_zero_addr((struct sockaddr *)&ss_list[i].ss) &&
- !is_broadcast_addr((struct sockaddr *)&ss_list[i].ss)) {
+ if (!is_zero_addr(&ss_list[i].ss) &&
+ !is_broadcast_addr((struct sockaddr *)(void *)&ss_list[i].ss)) {
(*return_ss_arr)[num_entries++] = ss_list[i].ss;
}
}
struct ip_service *ip_list = NULL;
int count = 0;
NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
-
+ static const char *ads_order[] = { "ads", NULL };
/* Look up #1B name */
if (lp_security() == SEC_ADS) {
status = internal_resolve_name(domain, 0x1b, NULL, &ip_list,
- &count, "ads");
+ &count, ads_order);
}
if (!NT_STATUS_IS_OK(status) || count == 0) {
&count,
lp_name_resolve_order());
if (!NT_STATUS_IS_OK(status)) {
+ SAFE_FREE(ip_list);
return false;
}
}
enum dc_lookup_type lookup_type,
bool *ordered)
{
- char *resolve_order = NULL;
+ const char **resolve_order = NULL;
char *saf_servername = NULL;
char *pserver = NULL;
const char *p;
are disabled and ads_only is True, then set the string to
NULL. */
- resolve_order = talloc_strdup(ctx, lp_name_resolve_order());
+ resolve_order = lp_name_resolve_order();
if (!resolve_order) {
status = NT_STATUS_NO_MEMORY;
goto out;
}
- strlower_m(resolve_order);
if (lookup_type == DC_ADS_ONLY) {
- if (strstr( resolve_order, "host")) {
- resolve_order = talloc_strdup(ctx, "ads");
+ if (str_list_check_ci(resolve_order, "host")) {
+ static const char *ads_order[] = { "ads", NULL };
+ resolve_order = ads_order;
/* DNS SRV lookups used by the ads resolver
are already sorted by priority and weight */
*ordered = true;
} else {
- resolve_order = talloc_strdup(ctx, "NULL");
+ /* this is quite bizarre! */
+ static const char *null_order[] = { "NULL", NULL };
+ resolve_order = null_order;
}
} else if (lookup_type == DC_KDC_ONLY) {
+ static const char *kdc_order[] = { "kdc", NULL };
/* DNS SRV lookups used by the ads/kdc resolver
are already sorted by priority and weight */
*ordered = true;
- resolve_order = talloc_strdup(ctx, "kdc");
+ resolve_order = kdc_order;
}
if (!resolve_order) {
status = NT_STATUS_NO_MEMORY;
/* fetch the server we have affinity for. Add the
'password server' list to a search for our domain controllers */
- saf_servername = saf_fetch( domain);
+ saf_servername = saf_fetch(ctx, domain);
if (strequal(domain, lp_workgroup()) || strequal(domain, lp_realm())) {
pserver = talloc_asprintf(ctx, "%s, %s",
saf_servername ? saf_servername : "",
- lp_passwordserver());
+ lp_password_server());
} else {
pserver = talloc_asprintf(ctx, "%s, *",
saf_servername ? saf_servername : "");
}
- SAFE_FREE(saf_servername);
+ TALLOC_FREE(saf_servername);
if (!pserver) {
status = NT_STATUS_NO_MEMORY;
goto out;
/* if we have no addresses and haven't done the auto lookup, then
just return the list of DC's. Or maybe we just failed. */
- if ((num_addresses == 0)) {
+ if (num_addresses == 0) {
if (done_auto_lookup) {
DEBUG(4,("get_dc_list: no servers found\n"));
status = NT_STATUS_NO_LOGON_SERVERS;
/* need to remove duplicates in the list if we have any
explicit password servers */
- if (local_count) {
- local_count = remove_duplicate_addrs2(return_iplist,
- local_count );
- }
+ local_count = remove_duplicate_addrs2(return_iplist, local_count );
/* For DC's we always prioritize IPv4 due to W2K3 not
* supporting LDAP, KRB5 or CLDAP over IPv6. */
*count = 0;
DEBUG(8,("get_sorted_dc_list: attempting lookup "
- "for name %s (sitename %s) using [%s]\n",
+ "for name %s (sitename %s)\n",
domain,
- sitename ? sitename : "NULL",
- (ads_only ? "ads" : lp_name_resolve_order())));
+ sitename ? sitename : "NULL"));
if (ads_only) {
lookup_type = DC_ADS_ONLY;