lib: Modernize a DEBUG
[samba4-gss.git] / source4 / libnet / libnet_lookup.c
blobf8477c7258cda4ac78ac42efd0e83736a00ee5c1
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 "libnet/libnet.h"
26 #include "libcli/composite/composite.h"
27 #include "auth/credentials/credentials.h"
28 #include "libcli/resolve/resolve.h"
29 #include "libcli/finddc.h"
30 #include "libcli/security/security.h"
31 #include "librpc/gen_ndr/ndr_lsa_c.h"
32 #include "param/param.h"
34 struct lookup_state {
35 struct nbt_name hostname;
36 const char *address;
40 static void continue_name_resolved(struct composite_context *ctx);
43 /**
44 * Sends asynchronous Lookup request
46 * @param io arguments and result of the call
49 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
50 TALLOC_CTX *mem_ctx,
51 struct libnet_Lookup *io)
53 struct composite_context *c;
54 struct lookup_state *s;
55 struct composite_context *cresolve_req;
56 struct resolve_context *resolve_ctx;
58 /* allocate context and state structures */
59 c = composite_create(mem_ctx, ctx->event_ctx);
60 if (c == NULL) return NULL;
62 s = talloc_zero(c, struct lookup_state);
63 if (composite_nomem(s, c)) return c;
65 c->private_data = s;
67 if (io == NULL || io->in.hostname == NULL) {
68 composite_error(c, NT_STATUS_INVALID_PARAMETER);
69 return c;
72 /* parameters */
73 s->hostname.name = talloc_strdup(s, io->in.hostname);
74 if (composite_nomem(s->hostname.name, c)) return c;
76 s->hostname.type = io->in.type;
77 s->hostname.scope = NULL;
79 /* name resolution methods */
80 if (io->in.resolve_ctx) {
81 resolve_ctx = io->in.resolve_ctx;
82 } else {
83 resolve_ctx = ctx->resolve_ctx;
86 /* send resolve request */
87 cresolve_req = resolve_name_send(resolve_ctx, s, &s->hostname, c->event_ctx);
88 if (composite_nomem(cresolve_req, c)) return c;
90 composite_continue(c, cresolve_req, continue_name_resolved, c);
91 return c;
95 static void continue_name_resolved(struct composite_context *ctx)
97 struct composite_context *c;
98 struct lookup_state *s;
100 c = talloc_get_type(ctx->async.private_data, struct composite_context);
101 s = talloc_get_type(c->private_data, struct lookup_state);
103 c->status = resolve_name_recv(ctx, s, &s->address);
105 composite_done(c);
110 * Waits for and receives results of asynchronous Lookup call
112 * @param c composite context returned by asynchronous Lookup call
113 * @param mem_ctx memory context of the call
114 * @param io pointer to results (and arguments) of the call
115 * @return nt status code of execution
118 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
119 struct libnet_Lookup *io)
121 NTSTATUS status;
122 struct lookup_state *s;
124 status = composite_wait(c);
125 if (NT_STATUS_IS_OK(status)) {
126 char **address;
128 s = talloc_get_type(c->private_data, struct lookup_state);
130 address = str_list_make_single(mem_ctx, s->address);
131 NT_STATUS_HAVE_NO_MEMORY(address);
132 io->out.address = discard_const_p(const char *, address);
135 talloc_free(c);
136 return status;
141 * Synchronous version of Lookup call
143 * @param mem_ctx memory context for the call
144 * @param io arguments and results of the call
145 * @return nt status code of execution
148 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
149 struct libnet_Lookup *io)
151 struct composite_context *c = libnet_Lookup_send(ctx, mem_ctx, io);
152 return libnet_Lookup_recv(c, mem_ctx, io);
157 * Shortcut functions to find common types of name
158 * (and skip nbt name type argument)
163 * Sends asynchronous LookupHost request
165 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
166 TALLOC_CTX *mem_ctx,
167 struct libnet_Lookup *io)
169 io->in.type = NBT_NAME_SERVER;
170 return libnet_Lookup_send(ctx, mem_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, mem_ctx, io);
182 return libnet_Lookup_recv(c, mem_ctx, io);
187 * Sends asynchronous LookupDCs request
189 struct tevent_req *libnet_LookupDCs_send(struct libnet_context *ctx,
190 TALLOC_CTX *mem_ctx,
191 struct libnet_LookupDCs *io)
193 struct tevent_req *req;
194 struct finddcs finddcs_io;
196 ZERO_STRUCT(finddcs_io);
198 if (strcasecmp_m(io->in.domain_name, lpcfg_workgroup(ctx->lp_ctx)) == 0) {
199 finddcs_io.in.domain_name = lpcfg_dnsdomain(ctx->lp_ctx);
200 } else {
201 finddcs_io.in.domain_name = io->in.domain_name;
203 finddcs_io.in.minimum_dc_flags = NBT_SERVER_LDAP | NBT_SERVER_DS | NBT_SERVER_WRITABLE;
204 finddcs_io.in.proto = lpcfg_client_netlogon_ping_protocol(ctx->lp_ctx);
205 finddcs_io.in.server_address = ctx->server_address;
207 req = finddcs_cldap_send(mem_ctx, &finddcs_io, ctx->resolve_ctx, ctx->event_ctx);
208 return req;
212 * Waits for and receives results of asynchronous Lookup call
214 * @param c composite context returned by asynchronous Lookup call
215 * @param mem_ctx memory context of the call
216 * @param io pointer to results (and arguments) of the call
217 * @return nt status code of execution
220 NTSTATUS libnet_LookupDCs_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
221 struct libnet_LookupDCs *io)
223 NTSTATUS status;
224 struct finddcs finddcs_io;
225 status = finddcs_cldap_recv(req, mem_ctx, &finddcs_io);
226 talloc_free(req);
227 io->out.num_dcs = 1;
228 io->out.dcs = talloc(mem_ctx, struct nbt_dc_name);
229 NT_STATUS_HAVE_NO_MEMORY(io->out.dcs);
230 io->out.dcs[0].address = finddcs_io.out.address;
231 io->out.dcs[0].name = finddcs_io.out.netlogon->data.nt5_ex.pdc_dns_name;
232 return status;
237 * Synchronous version of LookupDCs
239 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
240 struct libnet_LookupDCs *io)
242 struct tevent_req *req = libnet_LookupDCs_send(ctx, mem_ctx, io);
243 return libnet_LookupDCs_recv(req, mem_ctx, io);
247 struct lookup_name_state {
248 struct libnet_context *ctx;
249 const char *name;
250 uint32_t count;
251 struct libnet_DomainOpen domopen;
252 struct lsa_LookupNames lookup;
253 struct lsa_TransSidArray sids;
254 struct lsa_String *names;
256 /* information about the progress */
257 void (*monitor_fn)(struct monitor_msg *);
261 static bool prepare_lookup_params(struct libnet_context *ctx,
262 struct composite_context *c,
263 struct lookup_name_state *s);
264 static void continue_lookup_name(struct composite_context *ctx);
265 static void continue_name_found(struct tevent_req *subreq);
268 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
269 TALLOC_CTX *mem_ctx,
270 struct libnet_LookupName *io,
271 void (*monitor)(struct monitor_msg*))
273 struct composite_context *c;
274 struct lookup_name_state *s;
275 struct tevent_req *subreq;
276 bool prereq_met = false;
278 c = composite_create(mem_ctx, ctx->event_ctx);
279 if (c == NULL) return NULL;
281 s = talloc_zero(c, struct lookup_name_state);
282 if (composite_nomem(s, c)) return c;
284 c->private_data = s;
286 s->name = talloc_strdup(c, io->in.name);
287 s->monitor_fn = monitor;
288 s->ctx = ctx;
290 prereq_met = lsa_domain_opened(ctx, c, io->in.domain_name, &c, &s->domopen,
291 continue_lookup_name, monitor);
292 if (!prereq_met) return c;
294 if (!prepare_lookup_params(ctx, c, s)) return c;
296 subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
297 ctx->lsa.pipe->binding_handle,
298 &s->lookup);
299 if (composite_nomem(subreq, c)) return c;
301 tevent_req_set_callback(subreq, continue_name_found, c);
302 return c;
306 static bool prepare_lookup_params(struct libnet_context *ctx,
307 struct composite_context *c,
308 struct lookup_name_state *s)
310 const int single_name = 1;
312 s->sids.count = 0;
313 s->sids.sids = NULL;
315 s->names = talloc_array(s, struct lsa_String, single_name);
316 if (composite_nomem(s->names, c)) return false;
317 s->names[0].string = s->name;
319 s->lookup.in.handle = &ctx->lsa.handle;
320 s->lookup.in.num_names = single_name;
321 s->lookup.in.names = s->names;
322 s->lookup.in.sids = &s->sids;
323 s->lookup.in.level = 1;
324 s->lookup.in.count = &s->count;
325 s->lookup.out.count = &s->count;
326 s->lookup.out.sids = &s->sids;
327 s->lookup.out.domains = talloc_zero(s, struct lsa_RefDomainList *);
328 if (composite_nomem(s->lookup.out.domains, c)) return false;
330 return true;
334 static void continue_lookup_name(struct composite_context *ctx)
336 struct composite_context *c;
337 struct lookup_name_state *s;
338 struct tevent_req *subreq;
340 c = talloc_get_type(ctx->async.private_data, struct composite_context);
341 s = talloc_get_type(c->private_data, struct lookup_name_state);
343 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
344 if (!composite_is_ok(c)) return;
346 if (!prepare_lookup_params(s->ctx, c, s)) return;
348 subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
349 s->ctx->lsa.pipe->binding_handle,
350 &s->lookup);
351 if (composite_nomem(subreq, c)) return;
353 tevent_req_set_callback(subreq, continue_name_found, c);
357 static void continue_name_found(struct tevent_req *subreq)
359 struct composite_context *c;
360 struct lookup_name_state *s;
362 c = tevent_req_callback_data(subreq, struct composite_context);
363 s = talloc_get_type(c->private_data, struct lookup_name_state);
365 c->status = dcerpc_lsa_LookupNames_r_recv(subreq, s);
366 TALLOC_FREE(subreq);
367 if (!composite_is_ok(c)) return;
369 c->status = s->lookup.out.result;
370 if (!composite_is_ok(c)) return;
372 if (s->lookup.out.sids->count != s->lookup.in.num_names) {
373 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
374 return;
377 composite_done(c);
381 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
382 struct libnet_LookupName *io)
384 NTSTATUS status;
385 struct lookup_name_state *s = NULL;
386 struct lsa_RefDomainList *domains = NULL;
387 struct lsa_TransSidArray *sids = NULL;
389 status = composite_wait(c);
390 ZERO_STRUCT(io->out);
392 if (!NT_STATUS_IS_OK(status)) {
393 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s",
394 nt_errstr(status));
395 goto done;
398 s = talloc_get_type(c->private_data, struct lookup_name_state);
400 if (*s->lookup.out.count == 0) {
401 goto success;
404 domains = *s->lookup.out.domains;
405 sids = s->lookup.out.sids;
407 if (domains == NULL || sids == NULL) {
408 status = NT_STATUS_UNSUCCESSFUL;
409 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s",
410 nt_errstr(status));
411 goto done;
414 if (sids->count == 0) {
415 goto success;
418 io->out.rid = sids->sids[0].rid;
419 io->out.sid_type = sids->sids[0].sid_type;
420 if (domains->count > 0) {
421 io->out.sid = dom_sid_add_rid(mem_ctx, domains->domains[0].sid,
422 io->out.rid);
423 if (io->out.sid == NULL) {
424 status = NT_STATUS_NO_MEMORY;
425 goto done;
427 io->out.sidstr = dom_sid_string(mem_ctx, io->out.sid);
428 if (io->out.sidstr == NULL) {
429 status = NT_STATUS_NO_MEMORY;
430 goto done;
434 success:
435 io->out.error_string = talloc_strdup(mem_ctx, "Success");
436 done:
437 talloc_free(c);
438 return status;
442 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
443 struct libnet_LookupName *io)
445 struct composite_context *c;
447 c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
448 return libnet_LookupName_recv(c, mem_ctx, io);