CVE-2016-2113: docs-xml: let "tls verify peer" default to "as_strict_as_possible"
[Samba.git] / libcli / nbt / namerefresh.c
blobb525356c7470ce05f6ca04d2528796e4f9336065
1 /*
2 Unix SMB/CIFS implementation.
4 send out a name refresh request
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include <tevent.h>
24 #include "../libcli/nbt/libnbt.h"
25 #include "../libcli/nbt/nbt_proto.h"
26 #include "lib/socket/socket.h"
27 #include "lib/util/tevent_ntstatus.h"
30 send a nbt name refresh request
32 struct nbt_name_request *nbt_name_refresh_send(struct nbt_name_socket *nbtsock,
33 struct nbt_name_refresh *io)
35 struct nbt_name_request *req;
36 struct nbt_name_packet *packet;
37 struct socket_address *dest;
39 packet = talloc_zero(nbtsock, struct nbt_name_packet);
40 if (packet == NULL) return NULL;
42 packet->qdcount = 1;
43 packet->arcount = 1;
44 packet->operation = NBT_OPCODE_REFRESH;
45 if (io->in.broadcast) {
46 packet->operation |= NBT_FLAG_BROADCAST;
49 packet->questions = talloc_array(packet, struct nbt_name_question, 1);
50 if (packet->questions == NULL) goto failed;
52 packet->questions[0].name = io->in.name;
53 packet->questions[0].question_type = NBT_QTYPE_NETBIOS;
54 packet->questions[0].question_class = NBT_QCLASS_IP;
56 packet->additional = talloc_array(packet, struct nbt_res_rec, 1);
57 if (packet->additional == NULL) goto failed;
59 packet->additional[0].name = io->in.name;
60 packet->additional[0].rr_type = NBT_QTYPE_NETBIOS;
61 packet->additional[0].rr_class = NBT_QCLASS_IP;
62 packet->additional[0].ttl = io->in.ttl;
63 packet->additional[0].rdata.netbios.length = 6;
64 packet->additional[0].rdata.netbios.addresses = talloc_array(packet->additional,
65 struct nbt_rdata_address, 1);
66 if (packet->additional[0].rdata.netbios.addresses == NULL) goto failed;
67 packet->additional[0].rdata.netbios.addresses[0].nb_flags = io->in.nb_flags;
68 packet->additional[0].rdata.netbios.addresses[0].ipaddr =
69 talloc_strdup(packet->additional, io->in.address);
71 dest = socket_address_from_strings(nbtsock,
72 nbtsock->sock->backend_name,
73 io->in.dest_addr, io->in.dest_port);
74 if (dest == NULL) goto failed;
75 req = nbt_name_request_send(nbtsock, dest, packet,
76 io->in.timeout, io->in.retries, false);
77 if (req == NULL) goto failed;
79 talloc_free(packet);
80 return req;
82 failed:
83 talloc_free(packet);
84 return NULL;
88 wait for a refresh reply
90 _PUBLIC_ NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req,
91 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
93 NTSTATUS status;
94 struct nbt_name_packet *packet;
96 status = nbt_name_request_recv(req);
97 if (!NT_STATUS_IS_OK(status) ||
98 req->num_replies == 0) {
99 talloc_free(req);
100 return status;
103 packet = req->replies[0].packet;
104 io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
106 if (packet->ancount != 1 ||
107 packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
108 packet->answers[0].rr_class != NBT_QCLASS_IP) {
109 talloc_free(req);
110 return NT_STATUS_INVALID_NETWORK_RESPONSE;
113 io->out.rcode = packet->operation & NBT_RCODE;
114 io->out.name = packet->answers[0].name;
115 if (packet->answers[0].rdata.netbios.length < 6) {
116 talloc_free(req);
117 return NT_STATUS_INVALID_NETWORK_RESPONSE;
119 io->out.reply_addr = talloc_steal(mem_ctx,
120 packet->answers[0].rdata.netbios.addresses[0].ipaddr);
121 talloc_steal(mem_ctx, io->out.name.name);
122 talloc_steal(mem_ctx, io->out.name.scope);
124 talloc_free(req);
126 return NT_STATUS_OK;
130 synchronous name refresh request
132 _PUBLIC_ NTSTATUS nbt_name_refresh(struct nbt_name_socket *nbtsock,
133 TALLOC_CTX *mem_ctx, struct nbt_name_refresh *io)
135 struct nbt_name_request *req = nbt_name_refresh_send(nbtsock, io);
136 return nbt_name_refresh_recv(req, mem_ctx, io);
142 a wins name refresh with multiple WINS servers and multiple
143 addresses to refresh. Try each WINS server in turn, until we get a
144 reply for each address
146 struct nbt_name_refresh_wins_state {
147 struct nbt_name_socket *nbtsock;
148 struct nbt_name_refresh *io;
149 char **wins_servers;
150 uint16_t wins_port;
151 char **addresses;
152 int address_idx;
155 static void nbt_name_refresh_wins_handler(struct nbt_name_request *subreq);
158 the async send call for a multi-server WINS refresh
160 _PUBLIC_ struct tevent_req *nbt_name_refresh_wins_send(TALLOC_CTX *mem_ctx,
161 struct tevent_context *ev,
162 struct nbt_name_socket *nbtsock,
163 struct nbt_name_refresh_wins *io)
165 struct tevent_req *req;
166 struct nbt_name_refresh_wins_state *state;
167 struct nbt_name_request *subreq;
169 req = tevent_req_create(mem_ctx, &state,
170 struct nbt_name_refresh_wins_state);
171 if (req == NULL) {
172 return NULL;
175 state->io = talloc(state, struct nbt_name_refresh);
176 if (tevent_req_nomem(state->io, req)) {
177 return tevent_req_post(req, ev);
180 if (io->in.wins_servers == NULL) {
181 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
182 return tevent_req_post(req, ev);
185 if (io->in.wins_servers[0] == NULL) {
186 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
187 return tevent_req_post(req, ev);
190 if (io->in.addresses == NULL) {
191 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
192 return tevent_req_post(req, ev);
195 if (io->in.addresses[0] == NULL) {
196 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
197 return tevent_req_post(req, ev);
200 state->wins_port = io->in.wins_port;
201 state->wins_servers = str_list_copy(state, io->in.wins_servers);
202 if (tevent_req_nomem(state->wins_servers, req)) {
203 return tevent_req_post(req, ev);
206 state->addresses = str_list_copy(state, io->in.addresses);
207 if (tevent_req_nomem(state->addresses, req)) {
208 return tevent_req_post(req, ev);
211 state->io->in.name = io->in.name;
212 state->io->in.dest_addr = state->wins_servers[0];
213 state->io->in.dest_port = state->wins_port;
214 state->io->in.address = io->in.addresses[0];
215 state->io->in.nb_flags = io->in.nb_flags;
216 state->io->in.broadcast = false;
217 state->io->in.ttl = io->in.ttl;
218 state->io->in.timeout = 2;
219 state->io->in.retries = 2;
221 state->nbtsock = nbtsock;
222 state->address_idx = 0;
224 subreq = nbt_name_refresh_send(nbtsock, state->io);
225 if (tevent_req_nomem(subreq, req)) {
226 return tevent_req_post(req, ev);
229 subreq->async.fn = nbt_name_refresh_wins_handler;
230 subreq->async.private_data = req;
232 return req;
235 static void nbt_name_refresh_wins_handler(struct nbt_name_request *subreq)
237 struct tevent_req *req =
238 talloc_get_type_abort(subreq->async.private_data,
239 struct tevent_req);
240 struct nbt_name_refresh_wins_state *state =
241 tevent_req_data(req,
242 struct nbt_name_refresh_wins_state);
243 NTSTATUS status;
245 status = nbt_name_refresh_recv(subreq, state, state->io);
246 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
247 /* the refresh timed out - try the next WINS server */
248 state->wins_servers++;
249 if (state->wins_servers[0] == NULL) {
250 tevent_req_nterror(req, status);
251 return;
254 state->address_idx = 0;
255 state->io->in.dest_addr = state->wins_servers[0];
256 state->io->in.dest_port = state->wins_port;
257 state->io->in.address = state->addresses[0];
259 subreq = nbt_name_refresh_send(state->nbtsock, state->io);
260 if (tevent_req_nomem(subreq, req)) {
261 return;
263 subreq->async.fn = nbt_name_refresh_wins_handler;
264 subreq->async.private_data = req;
265 } else if (!NT_STATUS_IS_OK(status)) {
266 tevent_req_nterror(req, status);
267 return;
270 if (state->io->out.rcode == 0 &&
271 state->addresses[state->address_idx+1] != NULL) {
272 /* refresh our next address */
273 state->io->in.address = state->addresses[++(state->address_idx)];
274 subreq = nbt_name_refresh_send(state->nbtsock, state->io);
275 if (tevent_req_nomem(subreq, req)) {
276 return;
278 subreq->async.fn = nbt_name_refresh_wins_handler;
279 subreq->async.private_data = req;
280 return;
283 tevent_req_done(req);
287 multi-homed WINS name refresh - recv side
289 _PUBLIC_ NTSTATUS nbt_name_refresh_wins_recv(struct tevent_req *req,
290 TALLOC_CTX *mem_ctx,
291 struct nbt_name_refresh_wins *io)
293 struct nbt_name_refresh_wins_state *state =
294 tevent_req_data(req,
295 struct nbt_name_refresh_wins_state);
296 NTSTATUS status;
298 if (tevent_req_is_nterror(req, &status)) {
299 tevent_req_received(req);
300 return status;
303 io->out.wins_server = talloc_move(mem_ctx, &state->wins_servers[0]);
304 io->out.rcode = state->io->out.rcode;
306 tevent_req_received(req);
307 return NT_STATUS_OK;
311 multi-homed WINS refresh - sync interface
313 _PUBLIC_ NTSTATUS nbt_name_refresh_wins(struct nbt_name_socket *nbtsock,
314 TALLOC_CTX *mem_ctx,
315 struct nbt_name_refresh_wins *io)
317 TALLOC_CTX *frame = talloc_stackframe();
318 struct tevent_context *ev;
319 struct tevent_req *subreq;
320 NTSTATUS status;
323 * TODO: create a temporary event context
325 ev = nbtsock->event_ctx;
327 subreq = nbt_name_refresh_wins_send(frame, ev, nbtsock, io);
328 if (subreq == NULL) {
329 talloc_free(frame);
330 return NT_STATUS_NO_MEMORY;
333 if (!tevent_req_poll(subreq, ev)) {
334 status = map_nt_error_from_unix_common(errno);
335 talloc_free(frame);
336 return status;
339 status = nbt_name_refresh_wins_recv(subreq, mem_ctx, io);
340 if (!NT_STATUS_IS_OK(status)) {
341 talloc_free(frame);
342 return status;
345 TALLOC_FREE(frame);
346 return NT_STATUS_OK;