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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 a composite function for name resolving
26 #include "lib/events/events.h"
27 #include "libnet/libnet.h"
28 #include "libcli/composite/composite.h"
29 #include "auth/credentials/credentials.h"
30 #include "lib/messaging/messaging.h"
31 #include "lib/messaging/irpc.h"
32 #include "libcli/resolve/resolve.h"
33 #include "libcli/finddcs.h"
34 #include "libcli/security/security.h"
35 #include "librpc/gen_ndr/lsa.h"
36 #include "librpc/gen_ndr/ndr_lsa_c.h"
39 struct nbt_name hostname;
44 static void continue_name_resolved(struct composite_context *ctx);
48 * Sends asynchronous Lookup request
50 * @param io arguments and result of the call
53 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
54 struct libnet_Lookup *io)
56 struct composite_context *c;
57 struct lookup_state *s;
58 struct composite_context *cresolve_req;
61 /* allocate context and state structures */
62 c = talloc_zero(NULL, struct composite_context);
63 if (c == NULL) return NULL;
65 s = talloc_zero(c, struct lookup_state);
67 composite_error(c, NT_STATUS_NO_MEMORY);
71 /* prepare event context */
72 c->event_ctx = event_context_find(c);
73 if (c->event_ctx == NULL) {
74 composite_error(c, NT_STATUS_NO_MEMORY);
78 if (io == NULL || io->in.hostname == NULL) {
79 composite_error(c, NT_STATUS_INVALID_PARAMETER);
84 s->hostname.name = talloc_strdup(s, io->in.hostname);
85 s->hostname.type = io->in.type;
86 s->hostname.scope = NULL;
88 /* name resolution methods */
90 methods = io->in.methods;
92 methods = ctx->name_res_methods;
96 c->state = COMPOSITE_STATE_IN_PROGRESS;
98 /* send resolve request */
99 cresolve_req = resolve_name_send(&s->hostname, c->event_ctx, methods);
101 composite_continue(c, cresolve_req, continue_name_resolved, c);
107 static void continue_name_resolved(struct composite_context *ctx)
109 struct composite_context *c;
110 struct lookup_state *s;
112 c = talloc_get_type(ctx->async.private_data, struct composite_context);
113 s = talloc_get_type(c->private_data, struct lookup_state);
115 c->status = resolve_name_recv(ctx, s, &s->address);
122 * Waits for and receives results of asynchronous Lookup call
124 * @param c composite context returned by asynchronous Lookup call
125 * @param mem_ctx memory context of the call
126 * @param io pointer to results (and arguments) of the call
127 * @return nt status code of execution
130 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
131 struct libnet_Lookup *io)
134 struct lookup_state *s;
136 status = composite_wait(c);
137 if (NT_STATUS_IS_OK(status)) {
138 s = talloc_get_type(c->private_data, struct lookup_state);
140 io->out.address = str_list_make(mem_ctx, s->address, NULL);
141 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
150 * Synchronous version of Lookup call
152 * @param mem_ctx memory context for the call
153 * @param io arguments and results of the call
154 * @return nt status code of execution
157 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
158 struct libnet_Lookup *io)
160 struct composite_context *c = libnet_Lookup_send(ctx, io);
161 return libnet_Lookup_recv(c, mem_ctx, io);
166 * Shortcut functions to find common types of name
167 * (and skip nbt name type argument)
172 * Sends asynchronous LookupHost request
174 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
175 struct libnet_Lookup *io)
177 io->in.type = NBT_NAME_SERVER;
178 return libnet_Lookup_send(ctx, io);
184 * Synchronous version of LookupHost call
186 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
187 struct libnet_Lookup *io)
189 struct composite_context *c = libnet_LookupHost_send(ctx, io);
190 return libnet_Lookup_recv(c, mem_ctx, io);
195 * Sends asynchronous LookupDCs request
197 struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
199 struct libnet_LookupDCs *io)
201 struct composite_context *c;
202 struct messaging_context *msg_ctx = messaging_client_init(mem_ctx, ctx->event_ctx);
204 c = finddcs_send(mem_ctx, io->in.domain_name, io->in.name_type,
205 NULL, ctx->name_res_methods, ctx->event_ctx, msg_ctx);
210 * Waits for and receives results of asynchronous Lookup call
212 * @param c composite context returned by asynchronous Lookup call
213 * @param mem_ctx memory context of the call
214 * @param io pointer to results (and arguments) of the call
215 * @return nt status code of execution
218 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
219 struct libnet_LookupDCs *io)
222 status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
223 if (!NT_STATUS_IS_OK(status)) {
230 static struct composite_context* lsa_policy_opened(struct libnet_context *ctx,
231 const char *domain_name,
232 struct composite_context *parent_ctx,
233 struct libnet_DomainOpen *domain_open,
234 void (*continue_fn)(struct composite_context*),
235 void (*monitor)(struct monitor_msg*))
237 struct composite_context *domopen_req;
239 if (domain_name == NULL) {
240 if (policy_handle_empty(&ctx->lsa.handle)) {
241 domain_open->in.type = DOMAIN_LSA;
242 domain_open->in.domain_name = cli_credentials_get_domain(ctx->cred);
243 domain_open->in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
246 composite_error(parent_ctx, NT_STATUS_INVALID_PARAMETER);
250 if (policy_handle_empty(&ctx->lsa.handle) ||
251 !strequal(domain_name, ctx->lsa.name)) {
252 domain_open->in.type = DOMAIN_LSA;
253 domain_open->in.domain_name = domain_name;
254 domain_open->in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
261 domopen_req = libnet_DomainOpen_send(ctx, domain_open, monitor);
262 if (composite_nomem(domopen_req, parent_ctx)) return parent_ctx;
264 composite_continue(parent_ctx, domopen_req, continue_fn, parent_ctx);
270 * Synchronous version of LookupDCs
272 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
273 struct libnet_LookupDCs *io)
275 struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
276 return libnet_LookupDCs_recv(c, mem_ctx, io);
280 struct lookup_name_state {
281 struct libnet_context *ctx;
284 struct libnet_DomainOpen domopen;
285 struct lsa_LookupNames lookup;
286 struct lsa_TransSidArray sids;
287 struct lsa_String *names;
289 /* information about the progress */
290 void (*monitor_fn)(struct monitor_msg *);
294 static bool prepare_lookup_params(struct libnet_context *ctx,
295 struct composite_context *c,
296 struct lookup_name_state *s);
297 static void continue_lookup_name(struct composite_context *ctx);
298 static void continue_name_found(struct rpc_request *req);
301 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
303 struct libnet_LookupName *io,
304 void (*monitor)(struct monitor_msg*))
306 struct composite_context *c;
307 struct lookup_name_state *s;
308 struct composite_context *prereq_ctx;
309 struct rpc_request *lookup_req;
311 c = composite_create(mem_ctx, ctx->event_ctx);
312 if (c == NULL) return NULL;
314 s = talloc_zero(c, struct lookup_name_state);
315 if (composite_nomem(s, c)) return c;
319 s->name = talloc_strdup(c, io->in.name);
320 s->monitor_fn = monitor;
323 prereq_ctx = lsa_policy_opened(ctx, io->in.domain_name, c, &s->domopen,
324 continue_lookup_name, monitor);
325 if (prereq_ctx) return prereq_ctx;
327 if (!prepare_lookup_params(ctx, c, s)) return c;
329 lookup_req = dcerpc_lsa_LookupNames_send(ctx->lsa.pipe, c, &s->lookup);
330 if (composite_nomem(lookup_req, c)) return c;
332 composite_continue_rpc(c, lookup_req, continue_name_found, c);
337 static bool prepare_lookup_params(struct libnet_context *ctx,
338 struct composite_context *c,
339 struct lookup_name_state *s)
341 const int single_name = 1;
346 s->names = talloc_array(ctx, struct lsa_String, single_name);
347 if (composite_nomem(s->names, c)) return false;
348 s->names[0].string = s->name;
350 s->lookup.in.handle = &ctx->lsa.handle;
351 s->lookup.in.num_names = single_name;
352 s->lookup.in.names = s->names;
353 s->lookup.in.sids = &s->sids;
354 s->lookup.in.level = 1;
355 s->lookup.in.count = &s->count;
356 s->lookup.out.count = &s->count;
357 s->lookup.out.sids = &s->sids;
363 static void continue_lookup_name(struct composite_context *ctx)
365 struct composite_context *c;
366 struct lookup_name_state *s;
367 struct rpc_request *lookup_req;
369 c = talloc_get_type(ctx->async.private_data, struct composite_context);
370 s = talloc_get_type(c->private_data, struct lookup_name_state);
372 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
373 if (!composite_is_ok(c)) return;
375 if (!prepare_lookup_params(s->ctx, c, s)) return;
377 lookup_req = dcerpc_lsa_LookupNames_send(s->ctx->lsa.pipe, c, &s->lookup);
378 if (composite_nomem(lookup_req, c)) return;
380 composite_continue_rpc(c, lookup_req, continue_name_found, c);
384 static void continue_name_found(struct rpc_request *req)
386 struct composite_context *c;
387 struct lookup_name_state *s;
389 c = talloc_get_type(req->async.private, struct composite_context);
390 s = talloc_get_type(c->private_data, struct lookup_name_state);
392 c->status = dcerpc_ndr_request_recv(req);
393 if (!composite_is_ok(c)) return;
399 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
400 struct libnet_LookupName *io)
403 struct lookup_name_state *s;
405 status = composite_wait(c);
407 if (NT_STATUS_IS_OK(status)) {
408 s = talloc_get_type(c->private_data, struct lookup_name_state);
410 ZERO_STRUCT(io->out.domain_sid);
412 io->out.sidstr = NULL;
414 if (*s->lookup.out.count > 0) {
416 struct lsa_RefDomainList *domains = s->lookup.out.domains;
417 struct lsa_TransSidArray *sids = s->lookup.out.sids;
419 /* TODO: verify if returned pointers are non-null */
421 io->out.domain_sid = *domains->domains[0].sid;
422 io->out.rid = sids->sids[0].rid;
423 io->out.sid_type = sids->sids[0].sid_type;
425 num_auths = io->out.domain_sid.num_auths++;
426 io->out.domain_sid.sub_auths[num_auths] = io->out.rid;
428 io->out.sidstr = dom_sid_string(mem_ctx, &io->out.domain_sid);
431 io->out.error_string = talloc_strdup(mem_ctx, "Success");
433 } else if (!NT_STATUS_IS_OK(status)) {
434 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
441 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
442 struct libnet_LookupName *io)
444 struct composite_context *c;
446 c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
447 return libnet_LookupName_recv(c, mem_ctx, io);