1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | general name resolution interface
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 2005
|
---|
7 | Copyright (C) Jelmer Vernooij 2007
|
---|
8 |
|
---|
9 | This program is free software; you can redistribute it and/or modify
|
---|
10 | it under the terms of the GNU General Public License as published by
|
---|
11 | the Free Software Foundation; either version 3 of the License, or
|
---|
12 | (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful,
|
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | GNU General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU General Public License
|
---|
20 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "includes.h"
|
---|
24 | #include "libcli/composite/composite.h"
|
---|
25 | #include "libcli/resolve/resolve.h"
|
---|
26 | #include "librpc/gen_ndr/ndr_nbt.h"
|
---|
27 | #include "system/network.h"
|
---|
28 | #include "lib/socket/socket.h"
|
---|
29 | #include "../lib/util/dlinklist.h"
|
---|
30 | #include "lib/tsocket/tsocket.h"
|
---|
31 | #include "lib/util/util_net.h"
|
---|
32 |
|
---|
33 | struct resolve_state {
|
---|
34 | struct resolve_context *ctx;
|
---|
35 | struct resolve_method *method;
|
---|
36 | uint32_t flags;
|
---|
37 | uint16_t port;
|
---|
38 | struct nbt_name name;
|
---|
39 | struct composite_context *creq;
|
---|
40 | struct socket_address **addrs;
|
---|
41 | char **names;
|
---|
42 | };
|
---|
43 |
|
---|
44 | static struct composite_context *setup_next_method(struct composite_context *c);
|
---|
45 |
|
---|
46 |
|
---|
47 | struct resolve_context {
|
---|
48 | struct resolve_method {
|
---|
49 | resolve_name_send_fn send_fn;
|
---|
50 | resolve_name_recv_fn recv_fn;
|
---|
51 | void *privdata;
|
---|
52 | struct resolve_method *prev, *next;
|
---|
53 | } *methods;
|
---|
54 | };
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Initialize a resolve context
|
---|
58 | */
|
---|
59 | struct resolve_context *resolve_context_init(TALLOC_CTX *mem_ctx)
|
---|
60 | {
|
---|
61 | return talloc_zero(mem_ctx, struct resolve_context);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Add a resolve method
|
---|
66 | */
|
---|
67 | bool resolve_context_add_method(struct resolve_context *ctx, resolve_name_send_fn send_fn,
|
---|
68 | resolve_name_recv_fn recv_fn, void *userdata)
|
---|
69 | {
|
---|
70 | struct resolve_method *method = talloc_zero(ctx, struct resolve_method);
|
---|
71 |
|
---|
72 | if (method == NULL)
|
---|
73 | return false;
|
---|
74 |
|
---|
75 | method->send_fn = send_fn;
|
---|
76 | method->recv_fn = recv_fn;
|
---|
77 | method->privdata = userdata;
|
---|
78 | DLIST_ADD_END(ctx->methods, method);
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | handle completion of one name resolve method
|
---|
84 | */
|
---|
85 | static void resolve_handler(struct composite_context *creq)
|
---|
86 | {
|
---|
87 | struct composite_context *c = (struct composite_context *)creq->async.private_data;
|
---|
88 | struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
|
---|
89 | const struct resolve_method *method = state->method;
|
---|
90 |
|
---|
91 | c->status = method->recv_fn(creq, state, &state->addrs, &state->names);
|
---|
92 |
|
---|
93 | if (!NT_STATUS_IS_OK(c->status)) {
|
---|
94 | state->method = state->method->next;
|
---|
95 | state->creq = setup_next_method(c);
|
---|
96 | if (state->creq != NULL) {
|
---|
97 | return;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (!NT_STATUS_IS_OK(c->status)) {
|
---|
102 | c->state = COMPOSITE_STATE_ERROR;
|
---|
103 | } else {
|
---|
104 | c->state = COMPOSITE_STATE_DONE;
|
---|
105 | }
|
---|
106 | if (c->async.fn) {
|
---|
107 | c->async.fn(c);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | static struct composite_context *setup_next_method(struct composite_context *c)
|
---|
113 | {
|
---|
114 | struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
|
---|
115 | struct composite_context *creq = NULL;
|
---|
116 |
|
---|
117 | do {
|
---|
118 | if (state->method) {
|
---|
119 | creq = state->method->send_fn(c, c->event_ctx,
|
---|
120 | state->method->privdata,
|
---|
121 | state->flags,
|
---|
122 | state->port,
|
---|
123 | &state->name);
|
---|
124 | }
|
---|
125 | if (creq == NULL && state->method) state->method = state->method->next;
|
---|
126 |
|
---|
127 | } while (!creq && state->method);
|
---|
128 |
|
---|
129 | if (creq) {
|
---|
130 | creq->async.fn = resolve_handler;
|
---|
131 | creq->async.private_data = c;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return creq;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*
|
---|
138 | general name resolution - async send
|
---|
139 | */
|
---|
140 | struct composite_context *resolve_name_all_send(struct resolve_context *ctx,
|
---|
141 | TALLOC_CTX *mem_ctx,
|
---|
142 | uint32_t flags, /* RESOLVE_NAME_FLAG_* */
|
---|
143 | uint16_t port,
|
---|
144 | struct nbt_name *name,
|
---|
145 | struct tevent_context *event_ctx)
|
---|
146 | {
|
---|
147 | struct composite_context *c;
|
---|
148 | struct resolve_state *state;
|
---|
149 |
|
---|
150 | if (event_ctx == NULL) {
|
---|
151 | return NULL;
|
---|
152 | }
|
---|
153 |
|
---|
154 | c = composite_create(mem_ctx, event_ctx);
|
---|
155 | if (c == NULL) return NULL;
|
---|
156 |
|
---|
157 | if (composite_nomem(c->event_ctx, c)) return c;
|
---|
158 |
|
---|
159 | state = talloc(c, struct resolve_state);
|
---|
160 | if (composite_nomem(state, c)) return c;
|
---|
161 | c->private_data = state;
|
---|
162 |
|
---|
163 | state->flags = flags;
|
---|
164 | state->port = port;
|
---|
165 |
|
---|
166 | c->status = nbt_name_dup(state, name, &state->name);
|
---|
167 | if (!composite_is_ok(c)) return c;
|
---|
168 |
|
---|
169 | state->ctx = talloc_reference(state, ctx);
|
---|
170 | if (composite_nomem(state->ctx, c)) return c;
|
---|
171 |
|
---|
172 | if (is_ipaddress(state->name.name) ||
|
---|
173 | strcasecmp(state->name.name, "localhost") == 0) {
|
---|
174 | state->addrs = talloc_array(state, struct socket_address *, 2);
|
---|
175 | if (composite_nomem(state->addrs, c)) return c;
|
---|
176 | state->addrs[0] = socket_address_from_strings(state->addrs, "ip",
|
---|
177 | state->name.name, 0);
|
---|
178 | if (composite_nomem(state->addrs[0], c)) return c;
|
---|
179 | state->addrs[1] = NULL;
|
---|
180 | state->names = talloc_array(state, char *, 2);
|
---|
181 | if (composite_nomem(state->names, c)) return c;
|
---|
182 | state->names[0] = talloc_strdup(state->names, state->name.name);
|
---|
183 | if (composite_nomem(state->names[0], c)) return c;
|
---|
184 | state->names[1] = NULL;
|
---|
185 | composite_done(c);
|
---|
186 | return c;
|
---|
187 | }
|
---|
188 |
|
---|
189 | state->method = ctx->methods;
|
---|
190 | if (state->method == NULL) {
|
---|
191 | composite_error(c, NT_STATUS_BAD_NETWORK_NAME);
|
---|
192 | return c;
|
---|
193 | }
|
---|
194 | state->creq = setup_next_method(c);
|
---|
195 | if (composite_nomem(state->creq, c)) return c;
|
---|
196 |
|
---|
197 | return c;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | general name resolution method - recv side
|
---|
202 | */
|
---|
203 | NTSTATUS resolve_name_all_recv(struct composite_context *c,
|
---|
204 | TALLOC_CTX *mem_ctx,
|
---|
205 | struct socket_address ***addrs,
|
---|
206 | char ***names)
|
---|
207 | {
|
---|
208 | NTSTATUS status;
|
---|
209 |
|
---|
210 | status = composite_wait(c);
|
---|
211 |
|
---|
212 | if (NT_STATUS_IS_OK(status)) {
|
---|
213 | struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
|
---|
214 | *addrs = talloc_steal(mem_ctx, state->addrs);
|
---|
215 | if (names) {
|
---|
216 | *names = talloc_steal(mem_ctx, state->names);
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | talloc_free(c);
|
---|
221 | return status;
|
---|
222 | }
|
---|
223 |
|
---|
224 | struct composite_context *resolve_name_ex_send(struct resolve_context *ctx,
|
---|
225 | TALLOC_CTX *mem_ctx,
|
---|
226 | uint32_t flags, /* RESOLVE_NAME_FLAG_* */
|
---|
227 | uint16_t port,
|
---|
228 | struct nbt_name *name,
|
---|
229 | struct tevent_context *event_ctx)
|
---|
230 | {
|
---|
231 | return resolve_name_all_send(ctx, mem_ctx, flags, port, name, event_ctx);
|
---|
232 | }
|
---|
233 |
|
---|
234 | struct composite_context *resolve_name_send(struct resolve_context *ctx,
|
---|
235 | TALLOC_CTX *mem_ctx,
|
---|
236 | struct nbt_name *name,
|
---|
237 | struct tevent_context *event_ctx)
|
---|
238 | {
|
---|
239 | return resolve_name_ex_send(ctx, mem_ctx, 0, 0, name, event_ctx);
|
---|
240 | }
|
---|
241 |
|
---|
242 | NTSTATUS resolve_name_recv(struct composite_context *c,
|
---|
243 | TALLOC_CTX *mem_ctx,
|
---|
244 | const char **reply_addr)
|
---|
245 | {
|
---|
246 | NTSTATUS status;
|
---|
247 | struct socket_address **addrs = NULL;
|
---|
248 |
|
---|
249 | status = resolve_name_all_recv(c, mem_ctx, &addrs, NULL);
|
---|
250 |
|
---|
251 | if (NT_STATUS_IS_OK(status)) {
|
---|
252 | struct tsocket_address *t_addr = socket_address_to_tsocket_address(addrs, addrs[0]);
|
---|
253 | if (!t_addr) {
|
---|
254 | return NT_STATUS_NO_MEMORY;
|
---|
255 | }
|
---|
256 |
|
---|
257 | *reply_addr = tsocket_address_inet_addr_string(t_addr, mem_ctx);
|
---|
258 | talloc_free(addrs);
|
---|
259 | if (!*reply_addr) {
|
---|
260 | return NT_STATUS_NO_MEMORY;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | return status;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /*
|
---|
268 | receive multiple responses from resolve_name_send()
|
---|
269 | */
|
---|
270 | NTSTATUS resolve_name_multiple_recv(struct composite_context *c,
|
---|
271 | TALLOC_CTX *mem_ctx,
|
---|
272 | const char ***reply_addrs)
|
---|
273 | {
|
---|
274 | NTSTATUS status;
|
---|
275 | struct socket_address **addrs = NULL;
|
---|
276 | int i;
|
---|
277 |
|
---|
278 | status = resolve_name_all_recv(c, mem_ctx, &addrs, NULL);
|
---|
279 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
280 |
|
---|
281 | /* count the addresses */
|
---|
282 | for (i=0; addrs[i]; i++) ;
|
---|
283 |
|
---|
284 | *reply_addrs = talloc_array(mem_ctx, const char *, i+1);
|
---|
285 | NT_STATUS_HAVE_NO_MEMORY(*reply_addrs);
|
---|
286 |
|
---|
287 | for (i=0; addrs[i]; i++) {
|
---|
288 | struct tsocket_address *t_addr = socket_address_to_tsocket_address(addrs, addrs[i]);
|
---|
289 | NT_STATUS_HAVE_NO_MEMORY(t_addr);
|
---|
290 |
|
---|
291 | (*reply_addrs)[i] = tsocket_address_inet_addr_string(t_addr, *reply_addrs);
|
---|
292 | NT_STATUS_HAVE_NO_MEMORY((*reply_addrs)[i]);
|
---|
293 | }
|
---|
294 | (*reply_addrs)[i] = NULL;
|
---|
295 |
|
---|
296 | talloc_free(addrs);
|
---|
297 |
|
---|
298 | return status;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /*
|
---|
302 | general name resolution - sync call
|
---|
303 | */
|
---|
304 | NTSTATUS resolve_name_ex(struct resolve_context *ctx,
|
---|
305 | uint32_t flags, /* RESOLVE_NAME_FLAG_* */
|
---|
306 | uint16_t port,
|
---|
307 | struct nbt_name *name,
|
---|
308 | TALLOC_CTX *mem_ctx,
|
---|
309 | const char **reply_addr,
|
---|
310 | struct tevent_context *ev)
|
---|
311 | {
|
---|
312 | struct composite_context *c = resolve_name_ex_send(ctx, mem_ctx, flags, port, name, ev);
|
---|
313 | return resolve_name_recv(c, mem_ctx, reply_addr);
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | /* Initialise a struct nbt_name with a NULL scope */
|
---|
318 |
|
---|
319 | void make_nbt_name(struct nbt_name *nbt, const char *name, int type)
|
---|
320 | {
|
---|
321 | nbt->name = name;
|
---|
322 | nbt->scope = NULL;
|
---|
323 | nbt->type = type;
|
---|
324 | }
|
---|
325 |
|
---|
326 | /* Initialise a struct nbt_name with a NBT_NAME_CLIENT (0x00) name */
|
---|
327 |
|
---|
328 | void make_nbt_name_client(struct nbt_name *nbt, const char *name)
|
---|
329 | {
|
---|
330 | make_nbt_name(nbt, name, NBT_NAME_CLIENT);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /* Initialise a struct nbt_name with a NBT_NAME_SERVER (0x20) name */
|
---|
334 |
|
---|
335 | void make_nbt_name_server(struct nbt_name *nbt, const char *name)
|
---|
336 | {
|
---|
337 | make_nbt_name(nbt, name, NBT_NAME_SERVER);
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|