s3-security: use shared SECINFO_OWNER define.
[Samba/ekacnet.git] / source4 / libnet / libnet_lookup.c
blob21851d5ae89ef7616160ad2ea7714a1e3d16199d
1 /*
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
24 #include "includes.h"
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"
39 struct lookup_state {
40 struct nbt_name hostname;
41 const char *address;
45 static void continue_name_resolved(struct composite_context *ctx);
48 /**
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;
60 struct resolve_context *resolve_ctx;
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;
69 c->private_data = s;
71 if (io == NULL || io->in.hostname == NULL) {
72 composite_error(c, NT_STATUS_INVALID_PARAMETER);
73 return c;
76 /* parameters */
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 */
84 if (io->in.resolve_ctx) {
85 resolve_ctx = io->in.resolve_ctx;
86 } else {
87 resolve_ctx = ctx->resolve_ctx;
90 /* send resolve request */
91 cresolve_req = resolve_name_send(resolve_ctx, s, &s->hostname, c->event_ctx);
92 if (composite_nomem(cresolve_req, c)) return c;
94 composite_continue(c, cresolve_req, continue_name_resolved, c);
95 return 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);
109 composite_done(c);
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)
125 NTSTATUS status;
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 = (const char **)str_list_make_single(mem_ctx, s->address);
133 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
136 talloc_free(c);
137 return status;
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,
190 TALLOC_CTX *mem_ctx,
191 struct libnet_LookupDCs *io)
193 struct composite_context *c;
194 struct messaging_context *msg_ctx =
195 messaging_client_init(mem_ctx,
196 lp_messaging_path(mem_ctx, ctx->lp_ctx),
197 ctx->event_ctx);
199 c = finddcs_send(mem_ctx, lp_netbios_name(ctx->lp_ctx),
200 lp_nbt_port(ctx->lp_ctx), io->in.domain_name,
201 io->in.name_type, NULL, ctx->resolve_ctx,
202 ctx->event_ctx, msg_ctx);
203 return c;
207 * Waits for and receives results of asynchronous Lookup call
209 * @param c composite context returned by asynchronous Lookup call
210 * @param mem_ctx memory context of the call
211 * @param io pointer to results (and arguments) of the call
212 * @return nt status code of execution
215 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
216 struct libnet_LookupDCs *io)
218 NTSTATUS status;
219 status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
220 if (!NT_STATUS_IS_OK(status)) {
221 return status;
223 return status;
228 * Synchronous version of LookupDCs
230 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
231 struct libnet_LookupDCs *io)
233 struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
234 return libnet_LookupDCs_recv(c, mem_ctx, io);
238 struct lookup_name_state {
239 struct libnet_context *ctx;
240 const char *name;
241 uint32_t count;
242 struct libnet_DomainOpen domopen;
243 struct lsa_LookupNames lookup;
244 struct lsa_TransSidArray sids;
245 struct lsa_String *names;
247 /* information about the progress */
248 void (*monitor_fn)(struct monitor_msg *);
252 static bool prepare_lookup_params(struct libnet_context *ctx,
253 struct composite_context *c,
254 struct lookup_name_state *s);
255 static void continue_lookup_name(struct composite_context *ctx);
256 static void continue_name_found(struct tevent_req *subreq);
259 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
260 TALLOC_CTX *mem_ctx,
261 struct libnet_LookupName *io,
262 void (*monitor)(struct monitor_msg*))
264 struct composite_context *c;
265 struct lookup_name_state *s;
266 struct tevent_req *subreq;
267 bool prereq_met = false;
269 c = composite_create(mem_ctx, ctx->event_ctx);
270 if (c == NULL) return NULL;
272 s = talloc_zero(c, struct lookup_name_state);
273 if (composite_nomem(s, c)) return c;
275 c->private_data = s;
277 s->name = talloc_strdup(c, io->in.name);
278 s->monitor_fn = monitor;
279 s->ctx = ctx;
281 prereq_met = lsa_domain_opened(ctx, io->in.domain_name, &c, &s->domopen,
282 continue_lookup_name, monitor);
283 if (!prereq_met) return c;
285 if (!prepare_lookup_params(ctx, c, s)) return c;
287 subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
288 ctx->lsa.pipe->binding_handle,
289 &s->lookup);
290 if (composite_nomem(subreq, c)) return c;
292 tevent_req_set_callback(subreq, continue_name_found, c);
293 return c;
297 static bool prepare_lookup_params(struct libnet_context *ctx,
298 struct composite_context *c,
299 struct lookup_name_state *s)
301 const int single_name = 1;
303 s->sids.count = 0;
304 s->sids.sids = NULL;
306 s->names = talloc_array(ctx, struct lsa_String, single_name);
307 if (composite_nomem(s->names, c)) return false;
308 s->names[0].string = s->name;
310 s->lookup.in.handle = &ctx->lsa.handle;
311 s->lookup.in.num_names = single_name;
312 s->lookup.in.names = s->names;
313 s->lookup.in.sids = &s->sids;
314 s->lookup.in.level = 1;
315 s->lookup.in.count = &s->count;
316 s->lookup.out.count = &s->count;
317 s->lookup.out.sids = &s->sids;
318 s->lookup.out.domains = talloc_zero(ctx, struct lsa_RefDomainList *);
319 if (composite_nomem(s->lookup.out.domains, c)) return false;
321 return true;
325 static void continue_lookup_name(struct composite_context *ctx)
327 struct composite_context *c;
328 struct lookup_name_state *s;
329 struct tevent_req *subreq;
331 c = talloc_get_type(ctx->async.private_data, struct composite_context);
332 s = talloc_get_type(c->private_data, struct lookup_name_state);
334 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
335 if (!composite_is_ok(c)) return;
337 if (!prepare_lookup_params(s->ctx, c, s)) return;
339 subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
340 s->ctx->lsa.pipe->binding_handle,
341 &s->lookup);
342 if (composite_nomem(subreq, c)) return;
344 tevent_req_set_callback(subreq, continue_name_found, c);
348 static void continue_name_found(struct tevent_req *subreq)
350 struct composite_context *c;
351 struct lookup_name_state *s;
353 c = tevent_req_callback_data(subreq, struct composite_context);
354 s = talloc_get_type(c->private_data, struct lookup_name_state);
356 c->status = dcerpc_lsa_LookupNames_r_recv(subreq, s);
357 TALLOC_FREE(subreq);
358 if (!composite_is_ok(c)) return;
360 c->status = s->lookup.out.result;
361 if (!composite_is_ok(c)) return;
363 composite_done(c);
367 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
368 struct libnet_LookupName *io)
370 NTSTATUS status;
371 struct lookup_name_state *s;
373 status = composite_wait(c);
375 if (NT_STATUS_IS_OK(status)) {
376 s = talloc_get_type(c->private_data, struct lookup_name_state);
378 io->out.rid = 0;
379 io->out.sid = NULL;
380 io->out.sidstr = NULL;
382 if (*s->lookup.out.count > 0) {
383 struct lsa_RefDomainList *domains = *s->lookup.out.domains;
384 struct lsa_TransSidArray *sids = s->lookup.out.sids;
386 if (domains == NULL || sids == NULL) {
387 status = NT_STATUS_UNSUCCESSFUL;
388 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
389 goto done;
392 if (sids->count > 0) {
393 io->out.rid = sids->sids[0].rid;
394 io->out.sid_type = sids->sids[0].sid_type;
395 if (domains->count > 0) {
396 io->out.sid = dom_sid_add_rid(mem_ctx, domains->domains[0].sid, io->out.rid);
397 NT_STATUS_HAVE_NO_MEMORY(io->out.sid);
398 io->out.sidstr = dom_sid_string(mem_ctx, io->out.sid);
399 NT_STATUS_HAVE_NO_MEMORY(io->out.sidstr);
404 io->out.error_string = talloc_strdup(mem_ctx, "Success");
406 } else if (!NT_STATUS_IS_OK(status)) {
407 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
410 done:
411 talloc_free(c);
412 return status;
416 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
417 struct libnet_LookupName *io)
419 struct composite_context *c;
421 c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
422 return libnet_LookupName_recv(c, mem_ctx, io);