| 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 |
|
|---|
| 31 | struct resolve_state {
|
|---|
| 32 | struct resolve_context *ctx;
|
|---|
| 33 | struct resolve_method *method;
|
|---|
| 34 | uint32_t flags;
|
|---|
| 35 | uint16_t port;
|
|---|
| 36 | struct nbt_name name;
|
|---|
| 37 | struct composite_context *creq;
|
|---|
| 38 | struct socket_address **addrs;
|
|---|
| 39 | char **names;
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | static struct composite_context *setup_next_method(struct composite_context *c);
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | struct resolve_context {
|
|---|
| 46 | struct resolve_method {
|
|---|
| 47 | resolve_name_send_fn send_fn;
|
|---|
| 48 | resolve_name_recv_fn recv_fn;
|
|---|
| 49 | void *privdata;
|
|---|
| 50 | struct resolve_method *prev, *next;
|
|---|
| 51 | } *methods;
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Initialize a resolve context
|
|---|
| 56 | */
|
|---|
| 57 | struct resolve_context *resolve_context_init(TALLOC_CTX *mem_ctx)
|
|---|
| 58 | {
|
|---|
| 59 | return talloc_zero(mem_ctx, struct resolve_context);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Add a resolve method
|
|---|
| 64 | */
|
|---|
| 65 | bool resolve_context_add_method(struct resolve_context *ctx, resolve_name_send_fn send_fn,
|
|---|
| 66 | resolve_name_recv_fn recv_fn, void *userdata)
|
|---|
| 67 | {
|
|---|
| 68 | struct resolve_method *method = talloc_zero(ctx, struct resolve_method);
|
|---|
| 69 |
|
|---|
| 70 | if (method == NULL)
|
|---|
| 71 | return false;
|
|---|
| 72 |
|
|---|
| 73 | method->send_fn = send_fn;
|
|---|
| 74 | method->recv_fn = recv_fn;
|
|---|
| 75 | method->privdata = userdata;
|
|---|
| 76 | DLIST_ADD_END(ctx->methods, method, struct resolve_method *);
|
|---|
| 77 | return true;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | handle completion of one name resolve method
|
|---|
| 82 | */
|
|---|
| 83 | static void resolve_handler(struct composite_context *creq)
|
|---|
| 84 | {
|
|---|
| 85 | struct composite_context *c = (struct composite_context *)creq->async.private_data;
|
|---|
| 86 | struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
|
|---|
| 87 | const struct resolve_method *method = state->method;
|
|---|
| 88 |
|
|---|
| 89 | c->status = method->recv_fn(creq, state, &state->addrs, &state->names);
|
|---|
| 90 |
|
|---|
| 91 | if (!NT_STATUS_IS_OK(c->status)) {
|
|---|
| 92 | state->method = state->method->next;
|
|---|
| 93 | state->creq = setup_next_method(c);
|
|---|
| 94 | if (state->creq != NULL) {
|
|---|
| 95 | return;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | if (!NT_STATUS_IS_OK(c->status)) {
|
|---|
| 100 | c->state = COMPOSITE_STATE_ERROR;
|
|---|
| 101 | } else {
|
|---|
| 102 | c->state = COMPOSITE_STATE_DONE;
|
|---|
| 103 | }
|
|---|
| 104 | if (c->async.fn) {
|
|---|
| 105 | c->async.fn(c);
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | static struct composite_context *setup_next_method(struct composite_context *c)
|
|---|
| 111 | {
|
|---|
| 112 | struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
|
|---|
| 113 | struct composite_context *creq = NULL;
|
|---|
| 114 |
|
|---|
| 115 | do {
|
|---|
| 116 | if (state->method) {
|
|---|
| 117 | creq = state->method->send_fn(c, c->event_ctx,
|
|---|
| 118 | state->method->privdata,
|
|---|
| 119 | state->flags,
|
|---|
| 120 | state->port,
|
|---|
| 121 | &state->name);
|
|---|
| 122 | }
|
|---|
| 123 | if (creq == NULL && state->method) state->method = state->method->next;
|
|---|
| 124 |
|
|---|
| 125 | } while (!creq && state->method);
|
|---|
| 126 |
|
|---|
| 127 | if (creq) {
|
|---|
| 128 | creq->async.fn = resolve_handler;
|
|---|
| 129 | creq->async.private_data = c;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | return creq;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /*
|
|---|
| 136 | general name resolution - async send
|
|---|
| 137 | */
|
|---|
| 138 | struct composite_context *resolve_name_all_send(struct resolve_context *ctx,
|
|---|
| 139 | TALLOC_CTX *mem_ctx,
|
|---|
| 140 | uint32_t flags,
|
|---|
| 141 | uint16_t port,
|
|---|
| 142 | struct nbt_name *name,
|
|---|
| 143 | struct tevent_context *event_ctx)
|
|---|
| 144 | {
|
|---|
| 145 | struct composite_context *c;
|
|---|
| 146 | struct resolve_state *state;
|
|---|
| 147 |
|
|---|
| 148 | if (ctx == NULL || event_ctx == NULL) {
|
|---|
| 149 | return NULL;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | c = composite_create(mem_ctx, event_ctx);
|
|---|
| 153 | if (c == NULL) return NULL;
|
|---|
| 154 |
|
|---|
| 155 | if (composite_nomem(c->event_ctx, c)) return c;
|
|---|
| 156 |
|
|---|
| 157 | state = talloc(c, struct resolve_state);
|
|---|
| 158 | if (composite_nomem(state, c)) return c;
|
|---|
| 159 | c->private_data = state;
|
|---|
| 160 |
|
|---|
| 161 | state->flags = flags;
|
|---|
| 162 | state->port = port;
|
|---|
| 163 |
|
|---|
| 164 | c->status = nbt_name_dup(state, name, &state->name);
|
|---|
| 165 | if (!composite_is_ok(c)) return c;
|
|---|
| 166 |
|
|---|
| 167 | state->ctx = talloc_reference(state, ctx);
|
|---|
| 168 | if (composite_nomem(state->ctx, c)) return c;
|
|---|
| 169 |
|
|---|
| 170 | if (is_ipaddress(state->name.name) ||
|
|---|
| 171 | strcasecmp(state->name.name, "localhost") == 0) {
|
|---|
| 172 | struct in_addr ip = interpret_addr2(state->name.name);
|
|---|
| 173 |
|
|---|
| 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, "ipv4",
|
|---|
| 177 | inet_ntoa(ip), 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_send(struct resolve_context *ctx,
|
|---|
| 225 | TALLOC_CTX *mem_ctx,
|
|---|
| 226 | struct nbt_name *name,
|
|---|
| 227 | struct tevent_context *event_ctx)
|
|---|
| 228 | {
|
|---|
| 229 | return resolve_name_all_send(ctx, mem_ctx, 0, 0, name, event_ctx);
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | NTSTATUS resolve_name_recv(struct composite_context *c,
|
|---|
| 233 | TALLOC_CTX *mem_ctx,
|
|---|
| 234 | const char **reply_addr)
|
|---|
| 235 | {
|
|---|
| 236 | NTSTATUS status;
|
|---|
| 237 | struct socket_address **addrs = NULL;
|
|---|
| 238 |
|
|---|
| 239 | status = resolve_name_all_recv(c, mem_ctx, &addrs, NULL);
|
|---|
| 240 |
|
|---|
| 241 | if (NT_STATUS_IS_OK(status)) {
|
|---|
| 242 | *reply_addr = talloc_steal(mem_ctx, addrs[0]->addr);
|
|---|
| 243 | talloc_free(addrs);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | return status;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /*
|
|---|
| 250 | general name resolution - sync call
|
|---|
| 251 | */
|
|---|
| 252 | NTSTATUS resolve_name(struct resolve_context *ctx,
|
|---|
| 253 | struct nbt_name *name,
|
|---|
| 254 | TALLOC_CTX *mem_ctx,
|
|---|
| 255 | const char **reply_addr,
|
|---|
| 256 | struct tevent_context *ev)
|
|---|
| 257 | {
|
|---|
| 258 | struct composite_context *c = resolve_name_send(ctx, mem_ctx, name, ev);
|
|---|
| 259 | return resolve_name_recv(c, mem_ctx, reply_addr);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /* Initialise a struct nbt_name with a NULL scope */
|
|---|
| 263 |
|
|---|
| 264 | void make_nbt_name(struct nbt_name *nbt, const char *name, int type)
|
|---|
| 265 | {
|
|---|
| 266 | nbt->name = name;
|
|---|
| 267 | nbt->scope = NULL;
|
|---|
| 268 | nbt->type = type;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /* Initialise a struct nbt_name with a NBT_NAME_CLIENT (0x00) name */
|
|---|
| 272 |
|
|---|
| 273 | void make_nbt_name_client(struct nbt_name *nbt, const char *name)
|
|---|
| 274 | {
|
|---|
| 275 | make_nbt_name(nbt, name, NBT_NAME_CLIENT);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /* Initialise a struct nbt_name with a NBT_NAME_SERVER (0x20) name */
|
|---|
| 279 |
|
|---|
| 280 | void make_nbt_name_server(struct nbt_name *nbt, const char *name)
|
|---|
| 281 | {
|
|---|
| 282 | make_nbt_name(nbt, name, NBT_NAME_SERVER);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|