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"
37 #include "param/param.h"
40 struct nbt_name hostname;
45 static void continue_name_resolved(struct composite_context *ctx);
49 * Sends asynchronous Lookup request
51 * @param io arguments and result of the call
54 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
55 struct libnet_Lookup *io)
57 struct composite_context *c;
58 struct lookup_state *s;
59 struct composite_context *cresolve_req;
62 /* allocate context and state structures */
63 c = composite_create(ctx, ctx->event_ctx);
64 if (c == NULL) return NULL;
66 s = talloc_zero(c, struct lookup_state);
67 if (composite_nomem(s, c)) return c;
71 if (io == NULL || io->in.hostname == NULL) {
72 composite_error(c, NT_STATUS_INVALID_PARAMETER);
77 s->hostname.name = talloc_strdup(s, io->in.hostname);
78 if (composite_nomem(s->hostname.name, c)) return c;
80 s->hostname.type = io->in.type;
81 s->hostname.scope = NULL;
83 /* name resolution methods */
85 methods = io->in.methods;
87 methods = ctx->name_res_methods;
90 /* send resolve request */
91 cresolve_req = resolve_name_send(&s->hostname, c->event_ctx, methods);
92 if (composite_nomem(cresolve_req, c)) return c;
94 composite_continue(c, cresolve_req, continue_name_resolved, c);
99 static void continue_name_resolved(struct composite_context *ctx)
101 struct composite_context *c;
102 struct lookup_state *s;
104 c = talloc_get_type(ctx->async.private_data, struct composite_context);
105 s = talloc_get_type(c->private_data, struct lookup_state);
107 c->status = resolve_name_recv(ctx, s, &s->address);
114 * Waits for and receives results of asynchronous Lookup call
116 * @param c composite context returned by asynchronous Lookup call
117 * @param mem_ctx memory context of the call
118 * @param io pointer to results (and arguments) of the call
119 * @return nt status code of execution
122 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
123 struct libnet_Lookup *io)
126 struct lookup_state *s;
128 status = composite_wait(c);
129 if (NT_STATUS_IS_OK(status)) {
130 s = talloc_get_type(c->private_data, struct lookup_state);
132 io->out.address = str_list_make(mem_ctx, s->address, NULL);
133 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
142 * Synchronous version of Lookup call
144 * @param mem_ctx memory context for the call
145 * @param io arguments and results of the call
146 * @return nt status code of execution
149 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
150 struct libnet_Lookup *io)
152 struct composite_context *c = libnet_Lookup_send(ctx, io);
153 return libnet_Lookup_recv(c, mem_ctx, io);
158 * Shortcut functions to find common types of name
159 * (and skip nbt name type argument)
164 * Sends asynchronous LookupHost request
166 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
167 struct libnet_Lookup *io)
169 io->in.type = NBT_NAME_SERVER;
170 return libnet_Lookup_send(ctx, io);
176 * Synchronous version of LookupHost call
178 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
179 struct libnet_Lookup *io)
181 struct composite_context *c = libnet_LookupHost_send(ctx, io);
182 return libnet_Lookup_recv(c, mem_ctx, io);
187 * Sends asynchronous LookupDCs request
189 struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
191 struct libnet_LookupDCs *io)
193 struct composite_context *c;
194 struct messaging_context *msg_ctx =
195 messaging_client_init(mem_ctx, lp_messaging_path(mem_ctx, ctx->lp_ctx), ctx->event_ctx);
197 c = finddcs_send(mem_ctx, lp_netbios_name(ctx->lp_ctx),
198 io->in.domain_name, io->in.name_type,
199 NULL, ctx->name_res_methods, ctx->event_ctx, msg_ctx);
204 * Waits for and receives results of asynchronous Lookup call
206 * @param c composite context returned by asynchronous Lookup call
207 * @param mem_ctx memory context of the call
208 * @param io pointer to results (and arguments) of the call
209 * @return nt status code of execution
212 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
213 struct libnet_LookupDCs *io)
216 status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
217 if (!NT_STATUS_IS_OK(status)) {
225 * Synchronous version of LookupDCs
227 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
228 struct libnet_LookupDCs *io)
230 struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
231 return libnet_LookupDCs_recv(c, mem_ctx, io);
235 struct lookup_name_state {
236 struct libnet_context *ctx;
239 struct libnet_DomainOpen domopen;
240 struct lsa_LookupNames lookup;
241 struct lsa_TransSidArray sids;
242 struct lsa_String *names;
244 /* information about the progress */
245 void (*monitor_fn)(struct monitor_msg *);
249 static bool prepare_lookup_params(struct libnet_context *ctx,
250 struct composite_context *c,
251 struct lookup_name_state *s);
252 static void continue_lookup_name(struct composite_context *ctx);
253 static void continue_name_found(struct rpc_request *req);
256 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
258 struct libnet_LookupName *io,
259 void (*monitor)(struct monitor_msg*))
261 struct composite_context *c;
262 struct lookup_name_state *s;
263 struct rpc_request *lookup_req;
264 bool prereq_met = false;
266 c = composite_create(mem_ctx, ctx->event_ctx);
267 if (c == NULL) return NULL;
269 s = talloc_zero(c, struct lookup_name_state);
270 if (composite_nomem(s, c)) return c;
274 s->name = talloc_strdup(c, io->in.name);
275 s->monitor_fn = monitor;
278 prereq_met = lsa_domain_opened(ctx, io->in.domain_name, &c, &s->domopen,
279 continue_lookup_name, monitor);
280 if (!prereq_met) return c;
282 if (!prepare_lookup_params(ctx, c, s)) return c;
284 lookup_req = dcerpc_lsa_LookupNames_send(ctx->lsa.pipe, c, &s->lookup);
285 if (composite_nomem(lookup_req, c)) return c;
287 composite_continue_rpc(c, lookup_req, continue_name_found, c);
292 static bool prepare_lookup_params(struct libnet_context *ctx,
293 struct composite_context *c,
294 struct lookup_name_state *s)
296 const int single_name = 1;
301 s->names = talloc_array(ctx, struct lsa_String, single_name);
302 if (composite_nomem(s->names, c)) return false;
303 s->names[0].string = s->name;
305 s->lookup.in.handle = &ctx->lsa.handle;
306 s->lookup.in.num_names = single_name;
307 s->lookup.in.names = s->names;
308 s->lookup.in.sids = &s->sids;
309 s->lookup.in.level = 1;
310 s->lookup.in.count = &s->count;
311 s->lookup.out.count = &s->count;
312 s->lookup.out.sids = &s->sids;
318 static void continue_lookup_name(struct composite_context *ctx)
320 struct composite_context *c;
321 struct lookup_name_state *s;
322 struct rpc_request *lookup_req;
324 c = talloc_get_type(ctx->async.private_data, struct composite_context);
325 s = talloc_get_type(c->private_data, struct lookup_name_state);
327 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
328 if (!composite_is_ok(c)) return;
330 if (!prepare_lookup_params(s->ctx, c, s)) return;
332 lookup_req = dcerpc_lsa_LookupNames_send(s->ctx->lsa.pipe, c, &s->lookup);
333 if (composite_nomem(lookup_req, c)) return;
335 composite_continue_rpc(c, lookup_req, continue_name_found, c);
339 static void continue_name_found(struct rpc_request *req)
341 struct composite_context *c;
342 struct lookup_name_state *s;
344 c = talloc_get_type(req->async.private_data, struct composite_context);
345 s = talloc_get_type(c->private_data, struct lookup_name_state);
347 c->status = dcerpc_ndr_request_recv(req);
348 if (!composite_is_ok(c)) return;
350 c->status = s->lookup.out.result;
351 if (!composite_is_ok(c)) return;
357 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
358 struct libnet_LookupName *io)
361 struct lookup_name_state *s;
363 status = composite_wait(c);
365 if (NT_STATUS_IS_OK(status)) {
366 s = talloc_get_type(c->private_data, struct lookup_name_state);
370 io->out.sidstr = NULL;
372 if (*s->lookup.out.count > 0) {
373 struct lsa_RefDomainList *domains = s->lookup.out.domains;
374 struct lsa_TransSidArray *sids = s->lookup.out.sids;
376 if (domains == NULL || sids == NULL) {
377 status = NT_STATUS_UNSUCCESSFUL;
378 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
382 if (sids->count > 0) {
383 io->out.rid = sids->sids[0].rid;
384 io->out.sid_type = sids->sids[0].sid_type;
385 if (domains->count > 0) {
386 io->out.sid = dom_sid_add_rid(mem_ctx, domains->domains[0].sid, io->out.rid);
387 NT_STATUS_HAVE_NO_MEMORY(io->out.sid);
388 io->out.sidstr = dom_sid_string(mem_ctx, io->out.sid);
389 NT_STATUS_HAVE_NO_MEMORY(io->out.sidstr);
394 io->out.error_string = talloc_strdup(mem_ctx, "Success");
396 } else if (!NT_STATUS_IS_OK(status)) {
397 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
406 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
407 struct libnet_LookupName *io)
409 struct composite_context *c;
411 c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
412 return libnet_LookupName_recv(c, mem_ctx, io);