fix unstrcpy
[Samba.git] / source4 / libnet / libnet_lookup.c
blob77072b7ecce75dcb99551e73b1edc81986590557
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 s = talloc_get_type(c->private_data, struct lookup_state);
128 io->out.address = (const char **)str_list_make_single(mem_ctx, s->address);
129 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
132 talloc_free(c);
133 return status;
138 * Synchronous version of Lookup call
140 * @param mem_ctx memory context for the call
141 * @param io arguments and results of the call
142 * @return nt status code of execution
145 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
146 struct libnet_Lookup *io)
148 struct composite_context *c = libnet_Lookup_send(ctx, mem_ctx, io);
149 return libnet_Lookup_recv(c, mem_ctx, io);
154 * Shortcut functions to find common types of name
155 * (and skip nbt name type argument)
160 * Sends asynchronous LookupHost request
162 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
163 TALLOC_CTX *mem_ctx,
164 struct libnet_Lookup *io)
166 io->in.type = NBT_NAME_SERVER;
167 return libnet_Lookup_send(ctx, mem_ctx, io);
173 * Synchronous version of LookupHost call
175 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
176 struct libnet_Lookup *io)
178 struct composite_context *c = libnet_LookupHost_send(ctx, mem_ctx, io);
179 return libnet_Lookup_recv(c, mem_ctx, io);
184 * Sends asynchronous LookupDCs request
186 struct tevent_req *libnet_LookupDCs_send(struct libnet_context *ctx,
187 TALLOC_CTX *mem_ctx,
188 struct libnet_LookupDCs *io)
190 struct tevent_req *req;
191 struct finddcs finddcs_io;
193 ZERO_STRUCT(finddcs_io);
195 if (strcasecmp_m(io->in.domain_name, lpcfg_workgroup(ctx->lp_ctx)) == 0) {
196 finddcs_io.in.domain_name = lpcfg_dnsdomain(ctx->lp_ctx);
197 } else {
198 finddcs_io.in.domain_name = io->in.domain_name;
200 finddcs_io.in.minimum_dc_flags = NBT_SERVER_LDAP | NBT_SERVER_DS | NBT_SERVER_WRITABLE;
201 finddcs_io.in.server_address = ctx->server_address;
203 req = finddcs_cldap_send(mem_ctx, &finddcs_io, ctx->resolve_ctx, ctx->event_ctx);
204 return req;
208 * Waits for and receives results of asynchronous Lookup call
210 * @param c composite context returned by asynchronous Lookup call
211 * @param mem_ctx memory context of the call
212 * @param io pointer to results (and arguments) of the call
213 * @return nt status code of execution
216 NTSTATUS libnet_LookupDCs_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
217 struct libnet_LookupDCs *io)
219 NTSTATUS status;
220 struct finddcs finddcs_io;
221 status = finddcs_cldap_recv(req, mem_ctx, &finddcs_io);
222 talloc_free(req);
223 io->out.num_dcs = 1;
224 io->out.dcs = talloc(mem_ctx, struct nbt_dc_name);
225 NT_STATUS_HAVE_NO_MEMORY(io->out.dcs);
226 io->out.dcs[0].address = finddcs_io.out.address;
227 io->out.dcs[0].name = finddcs_io.out.netlogon.data.nt5_ex.pdc_dns_name;
228 return status;
233 * Synchronous version of LookupDCs
235 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
236 struct libnet_LookupDCs *io)
238 struct tevent_req *req = libnet_LookupDCs_send(ctx, mem_ctx, io);
239 return libnet_LookupDCs_recv(req, mem_ctx, io);
243 struct lookup_name_state {
244 struct libnet_context *ctx;
245 const char *name;
246 uint32_t count;
247 struct libnet_DomainOpen domopen;
248 struct lsa_LookupNames lookup;
249 struct lsa_TransSidArray sids;
250 struct lsa_String *names;
252 /* information about the progress */
253 void (*monitor_fn)(struct monitor_msg *);
257 static bool prepare_lookup_params(struct libnet_context *ctx,
258 struct composite_context *c,
259 struct lookup_name_state *s);
260 static void continue_lookup_name(struct composite_context *ctx);
261 static void continue_name_found(struct tevent_req *subreq);
264 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
265 TALLOC_CTX *mem_ctx,
266 struct libnet_LookupName *io,
267 void (*monitor)(struct monitor_msg*))
269 struct composite_context *c;
270 struct lookup_name_state *s;
271 struct tevent_req *subreq;
272 bool prereq_met = false;
274 c = composite_create(mem_ctx, ctx->event_ctx);
275 if (c == NULL) return NULL;
277 s = talloc_zero(c, struct lookup_name_state);
278 if (composite_nomem(s, c)) return c;
280 c->private_data = s;
282 s->name = talloc_strdup(c, io->in.name);
283 s->monitor_fn = monitor;
284 s->ctx = ctx;
286 prereq_met = lsa_domain_opened(ctx, c, io->in.domain_name, &c, &s->domopen,
287 continue_lookup_name, monitor);
288 if (!prereq_met) return c;
290 if (!prepare_lookup_params(ctx, c, s)) return c;
292 subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
293 ctx->lsa.pipe->binding_handle,
294 &s->lookup);
295 if (composite_nomem(subreq, c)) return c;
297 tevent_req_set_callback(subreq, continue_name_found, c);
298 return c;
302 static bool prepare_lookup_params(struct libnet_context *ctx,
303 struct composite_context *c,
304 struct lookup_name_state *s)
306 const int single_name = 1;
308 s->sids.count = 0;
309 s->sids.sids = NULL;
311 s->names = talloc_array(s, struct lsa_String, single_name);
312 if (composite_nomem(s->names, c)) return false;
313 s->names[0].string = s->name;
315 s->lookup.in.handle = &ctx->lsa.handle;
316 s->lookup.in.num_names = single_name;
317 s->lookup.in.names = s->names;
318 s->lookup.in.sids = &s->sids;
319 s->lookup.in.level = 1;
320 s->lookup.in.count = &s->count;
321 s->lookup.out.count = &s->count;
322 s->lookup.out.sids = &s->sids;
323 s->lookup.out.domains = talloc_zero(s, struct lsa_RefDomainList *);
324 if (composite_nomem(s->lookup.out.domains, c)) return false;
326 return true;
330 static void continue_lookup_name(struct composite_context *ctx)
332 struct composite_context *c;
333 struct lookup_name_state *s;
334 struct tevent_req *subreq;
336 c = talloc_get_type(ctx->async.private_data, struct composite_context);
337 s = talloc_get_type(c->private_data, struct lookup_name_state);
339 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
340 if (!composite_is_ok(c)) return;
342 if (!prepare_lookup_params(s->ctx, c, s)) return;
344 subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
345 s->ctx->lsa.pipe->binding_handle,
346 &s->lookup);
347 if (composite_nomem(subreq, c)) return;
349 tevent_req_set_callback(subreq, continue_name_found, c);
353 static void continue_name_found(struct tevent_req *subreq)
355 struct composite_context *c;
356 struct lookup_name_state *s;
358 c = tevent_req_callback_data(subreq, struct composite_context);
359 s = talloc_get_type(c->private_data, struct lookup_name_state);
361 c->status = dcerpc_lsa_LookupNames_r_recv(subreq, s);
362 TALLOC_FREE(subreq);
363 if (!composite_is_ok(c)) return;
365 c->status = s->lookup.out.result;
366 if (!composite_is_ok(c)) return;
368 if (s->lookup.out.sids->count != s->lookup.in.num_names) {
369 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
370 return;
373 composite_done(c);
377 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
378 struct libnet_LookupName *io)
380 NTSTATUS status;
381 struct lookup_name_state *s;
383 status = composite_wait(c);
385 if (NT_STATUS_IS_OK(status)) {
386 s = talloc_get_type(c->private_data, struct lookup_name_state);
388 io->out.rid = 0;
389 io->out.sid = NULL;
390 io->out.sidstr = NULL;
392 if (*s->lookup.out.count > 0) {
393 struct lsa_RefDomainList *domains = *s->lookup.out.domains;
394 struct lsa_TransSidArray *sids = s->lookup.out.sids;
396 if (domains == NULL || sids == NULL) {
397 status = NT_STATUS_UNSUCCESSFUL;
398 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
399 goto done;
402 if (sids->count > 0) {
403 io->out.rid = sids->sids[0].rid;
404 io->out.sid_type = sids->sids[0].sid_type;
405 if (domains->count > 0) {
406 io->out.sid = dom_sid_add_rid(mem_ctx, domains->domains[0].sid, io->out.rid);
407 NT_STATUS_HAVE_NO_MEMORY(io->out.sid);
408 io->out.sidstr = dom_sid_string(mem_ctx, io->out.sid);
409 NT_STATUS_HAVE_NO_MEMORY(io->out.sidstr);
414 io->out.error_string = talloc_strdup(mem_ctx, "Success");
416 } else if (!NT_STATUS_IS_OK(status)) {
417 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
420 done:
421 talloc_free(c);
422 return status;
426 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
427 struct libnet_LookupName *io)
429 struct composite_context *c;
431 c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
432 return libnet_LookupName_recv(c, mem_ctx, io);