2 Unix SMB/CIFS implementation.
4 Copyright (C) Rafal Szczesniak 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 a composite function for name resolving
25 #include "lib/events/events.h"
26 #include "libnet/libnet.h"
27 #include "libcli/composite/composite.h"
28 #include "auth/credentials/credentials.h"
29 #include "lib/messaging/messaging.h"
30 #include "lib/messaging/irpc.h"
31 #include "libcli/resolve/resolve.h"
32 #include "libcli/finddcs.h"
33 #include "libcli/security/security.h"
34 #include "librpc/gen_ndr/lsa.h"
35 #include "librpc/gen_ndr/ndr_lsa_c.h"
38 struct nbt_name hostname;
43 static void continue_name_resolved(struct composite_context *ctx);
47 * Sends asynchronous Lookup request
49 * @param io arguments and result of the call
52 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
53 struct libnet_Lookup *io)
55 struct composite_context *c;
56 struct lookup_state *s;
57 struct composite_context *cresolve_req;
60 /* allocate context and state structures */
61 c = composite_create(ctx, ctx->event_ctx);
62 if (c == NULL) return NULL;
64 s = talloc_zero(c, struct lookup_state);
65 if (composite_nomem(s, c)) return c;
69 if (io == NULL || io->in.hostname == NULL) {
70 composite_error(c, NT_STATUS_INVALID_PARAMETER);
75 s->hostname.name = talloc_strdup(s, io->in.hostname);
76 if (composite_nomem(s->hostname.name, c)) return c;
78 s->hostname.type = io->in.type;
79 s->hostname.scope = NULL;
81 /* name resolution methods */
83 methods = io->in.methods;
85 methods = ctx->name_res_methods;
88 /* send resolve request */
89 cresolve_req = resolve_name_send(&s->hostname, c->event_ctx, methods);
90 if (composite_nomem(cresolve_req, c)) return c;
92 composite_continue(c, cresolve_req, continue_name_resolved, c);
97 static void continue_name_resolved(struct composite_context *ctx)
99 struct composite_context *c;
100 struct lookup_state *s;
102 c = talloc_get_type(ctx->async.private_data, struct composite_context);
103 s = talloc_get_type(c->private_data, struct lookup_state);
105 c->status = resolve_name_recv(ctx, s, &s->address);
112 * Waits for and receives results of asynchronous Lookup call
114 * @param c composite context returned by asynchronous Lookup call
115 * @param mem_ctx memory context of the call
116 * @param io pointer to results (and arguments) of the call
117 * @return nt status code of execution
120 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
121 struct libnet_Lookup *io)
124 struct lookup_state *s;
126 status = composite_wait(c);
127 if (NT_STATUS_IS_OK(status)) {
128 s = talloc_get_type(c->private_data, struct lookup_state);
130 io->out.address = str_list_make(mem_ctx, s->address, NULL);
131 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
140 * Synchronous version of Lookup call
142 * @param mem_ctx memory context for the call
143 * @param io arguments and results of the call
144 * @return nt status code of execution
147 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
148 struct libnet_Lookup *io)
150 struct composite_context *c = libnet_Lookup_send(ctx, io);
151 return libnet_Lookup_recv(c, mem_ctx, io);
156 * Shortcut functions to find common types of name
157 * (and skip nbt name type argument)
162 * Sends asynchronous LookupHost request
164 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
165 struct libnet_Lookup *io)
167 io->in.type = NBT_NAME_SERVER;
168 return libnet_Lookup_send(ctx, io);
174 * Synchronous version of LookupHost call
176 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
177 struct libnet_Lookup *io)
179 struct composite_context *c = libnet_LookupHost_send(ctx, io);
180 return libnet_Lookup_recv(c, mem_ctx, io);
185 * Sends asynchronous LookupDCs request
187 struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
189 struct libnet_LookupDCs *io)
191 struct composite_context *c;
192 struct messaging_context *msg_ctx = messaging_client_init(mem_ctx, ctx->event_ctx);
194 c = finddcs_send(mem_ctx, io->in.domain_name, io->in.name_type,
195 NULL, ctx->name_res_methods, ctx->event_ctx, msg_ctx);
200 * Waits for and receives results of asynchronous Lookup call
202 * @param c composite context returned by asynchronous Lookup call
203 * @param mem_ctx memory context of the call
204 * @param io pointer to results (and arguments) of the call
205 * @return nt status code of execution
208 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
209 struct libnet_LookupDCs *io)
212 status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
213 if (!NT_STATUS_IS_OK(status)) {
221 * Synchronous version of LookupDCs
223 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
224 struct libnet_LookupDCs *io)
226 struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
227 return libnet_LookupDCs_recv(c, mem_ctx, io);
231 struct lookup_name_state {
232 struct libnet_context *ctx;
235 struct libnet_DomainOpen domopen;
236 struct lsa_LookupNames lookup;
237 struct lsa_TransSidArray sids;
238 struct lsa_String *names;
240 /* information about the progress */
241 void (*monitor_fn)(struct monitor_msg *);
245 static bool prepare_lookup_params(struct libnet_context *ctx,
246 struct composite_context *c,
247 struct lookup_name_state *s);
248 static void continue_lookup_name(struct composite_context *ctx);
249 static void continue_name_found(struct rpc_request *req);
252 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
254 struct libnet_LookupName *io,
255 void (*monitor)(struct monitor_msg*))
257 struct composite_context *c;
258 struct lookup_name_state *s;
259 struct rpc_request *lookup_req;
260 BOOL prereq_met = False;
262 c = composite_create(mem_ctx, ctx->event_ctx);
263 if (c == NULL) return NULL;
265 s = talloc_zero(c, struct lookup_name_state);
266 if (composite_nomem(s, c)) return c;
270 s->name = talloc_strdup(c, io->in.name);
271 s->monitor_fn = monitor;
274 prereq_met = lsa_domain_opened(ctx, io->in.domain_name, &c, &s->domopen,
275 continue_lookup_name, monitor);
276 if (!prereq_met) return c;
278 if (!prepare_lookup_params(ctx, c, s)) return c;
280 lookup_req = dcerpc_lsa_LookupNames_send(ctx->lsa.pipe, c, &s->lookup);
281 if (composite_nomem(lookup_req, c)) return c;
283 composite_continue_rpc(c, lookup_req, continue_name_found, c);
288 static bool prepare_lookup_params(struct libnet_context *ctx,
289 struct composite_context *c,
290 struct lookup_name_state *s)
292 const int single_name = 1;
297 s->names = talloc_array(ctx, struct lsa_String, single_name);
298 if (composite_nomem(s->names, c)) return false;
299 s->names[0].string = s->name;
301 s->lookup.in.handle = &ctx->lsa.handle;
302 s->lookup.in.num_names = single_name;
303 s->lookup.in.names = s->names;
304 s->lookup.in.sids = &s->sids;
305 s->lookup.in.level = 1;
306 s->lookup.in.count = &s->count;
307 s->lookup.out.count = &s->count;
308 s->lookup.out.sids = &s->sids;
314 static void continue_lookup_name(struct composite_context *ctx)
316 struct composite_context *c;
317 struct lookup_name_state *s;
318 struct rpc_request *lookup_req;
320 c = talloc_get_type(ctx->async.private_data, struct composite_context);
321 s = talloc_get_type(c->private_data, struct lookup_name_state);
323 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
324 if (!composite_is_ok(c)) return;
326 if (!prepare_lookup_params(s->ctx, c, s)) return;
328 lookup_req = dcerpc_lsa_LookupNames_send(s->ctx->lsa.pipe, c, &s->lookup);
329 if (composite_nomem(lookup_req, c)) return;
331 composite_continue_rpc(c, lookup_req, continue_name_found, c);
335 static void continue_name_found(struct rpc_request *req)
337 struct composite_context *c;
338 struct lookup_name_state *s;
340 c = talloc_get_type(req->async.private_data, struct composite_context);
341 s = talloc_get_type(c->private_data, struct lookup_name_state);
343 c->status = dcerpc_ndr_request_recv(req);
344 if (!composite_is_ok(c)) return;
346 c->status = s->lookup.out.result;
347 if (!composite_is_ok(c)) return;
353 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
354 struct libnet_LookupName *io)
357 struct lookup_name_state *s;
359 status = composite_wait(c);
361 if (NT_STATUS_IS_OK(status)) {
362 s = talloc_get_type(c->private_data, struct lookup_name_state);
366 io->out.sidstr = NULL;
368 if (*s->lookup.out.count > 0) {
369 struct lsa_RefDomainList *domains = s->lookup.out.domains;
370 struct lsa_TransSidArray *sids = s->lookup.out.sids;
372 if (domains == NULL || sids == NULL) {
373 status = NT_STATUS_UNSUCCESSFUL;
374 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
378 if (sids->count > 0) {
379 io->out.rid = sids->sids[0].rid;
380 io->out.sid_type = sids->sids[0].sid_type;
381 if (domains->count > 0) {
382 io->out.sid = dom_sid_add_rid(mem_ctx, domains->domains[0].sid, io->out.rid);
383 NT_STATUS_HAVE_NO_MEMORY(io->out.sid);
384 io->out.sidstr = dom_sid_string(mem_ctx, io->out.sid);
385 NT_STATUS_HAVE_NO_MEMORY(io->out.sidstr);
390 io->out.error_string = talloc_strdup(mem_ctx, "Success");
392 } else if (!NT_STATUS_IS_OK(status)) {
393 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
402 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
403 struct libnet_LookupName *io)
405 struct composite_context *c;
407 c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
408 return libnet_LookupName_recv(c, mem_ctx, io);