s4-libnet: Add mem_ctx to libnet_DomainOpen calls (bug #8889)
[kai/samba-autobuild/.git] / source4 / libnet / libnet_lookup.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Rafal Szczesniak 2005
5    
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.
10    
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.
15    
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/>.
18 */
19
20 /*
21   a composite function for name resolving
22 */
23
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"
33
34 struct lookup_state {
35         struct nbt_name hostname;
36         const char *address;
37 };
38
39
40 static void continue_name_resolved(struct composite_context *ctx);
41
42
43 /**
44  * Sends asynchronous Lookup request
45  *
46  * @param io arguments and result of the call
47  */
48
49 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
50                                              TALLOC_CTX *mem_ctx,
51                                              struct libnet_Lookup *io)
52 {
53         struct composite_context *c;
54         struct lookup_state *s;
55         struct composite_context *cresolve_req;
56         struct resolve_context *resolve_ctx;
57
58         /* allocate context and state structures */
59         c = composite_create(mem_ctx, ctx->event_ctx);
60         if (c == NULL) return NULL;
61
62         s = talloc_zero(c, struct lookup_state);
63         if (composite_nomem(s, c)) return c;
64
65         c->private_data = s;
66
67         if (io == NULL || io->in.hostname == NULL) {
68                 composite_error(c, NT_STATUS_INVALID_PARAMETER);
69                 return c;
70         }
71
72         /* parameters */
73         s->hostname.name   = talloc_strdup(s, io->in.hostname);
74         if (composite_nomem(s->hostname.name, c)) return c;
75
76         s->hostname.type   = io->in.type;
77         s->hostname.scope  = NULL;
78
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;
84         }
85
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;
89
90         composite_continue(c, cresolve_req, continue_name_resolved, c);
91         return c;
92 }
93
94
95 static void continue_name_resolved(struct composite_context *ctx)
96 {
97         struct composite_context *c;
98         struct lookup_state *s;
99
100         c = talloc_get_type(ctx->async.private_data, struct composite_context);
101         s = talloc_get_type(c->private_data, struct lookup_state);
102
103         c->status = resolve_name_recv(ctx, s, &s->address);
104         
105         composite_done(c);
106 }
107
108
109 /**
110  * Waits for and receives results of asynchronous Lookup call
111  *
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
116  */
117
118 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
119                             struct libnet_Lookup *io)
120 {
121         NTSTATUS status;
122         struct lookup_state *s;
123
124         status = composite_wait(c);
125         if (NT_STATUS_IS_OK(status)) {
126                 s = talloc_get_type(c->private_data, struct lookup_state);
127
128                 io->out.address = (const char **)str_list_make_single(mem_ctx, s->address);
129                 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
130         }
131
132         talloc_free(c);
133         return status;
134 }
135
136
137 /**
138  * Synchronous version of Lookup call
139  *
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
143  */
144
145 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
146                        struct libnet_Lookup *io)
147 {
148         struct composite_context *c = libnet_Lookup_send(ctx, mem_ctx, io);
149         return libnet_Lookup_recv(c, mem_ctx, io);
150 }
151
152
153 /*
154  * Shortcut functions to find common types of name
155  * (and skip nbt name type argument)
156  */
157
158
159 /**
160  * Sends asynchronous LookupHost request
161  */
162 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
163                                                  TALLOC_CTX *mem_ctx,
164                                                  struct libnet_Lookup *io)
165 {
166         io->in.type = NBT_NAME_SERVER;
167         return libnet_Lookup_send(ctx, mem_ctx, io);
168 }
169
170
171
172 /**
173  * Synchronous version of LookupHost call
174  */
175 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
176                            struct libnet_Lookup *io)
177 {
178         struct composite_context *c = libnet_LookupHost_send(ctx, mem_ctx, io);
179         return libnet_Lookup_recv(c, mem_ctx, io);
180 }
181
182
183 /**
184  * Sends asynchronous LookupDCs request
185  */
186 struct tevent_req *libnet_LookupDCs_send(struct libnet_context *ctx,
187                                          TALLOC_CTX *mem_ctx,
188                                          struct libnet_LookupDCs *io)
189 {
190         struct tevent_req *req;
191         struct finddcs finddcs_io;
192
193         ZERO_STRUCT(finddcs_io);
194
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;
199         }
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;
202
203         req = finddcs_cldap_send(mem_ctx, &finddcs_io, ctx->resolve_ctx, ctx->event_ctx);
204         return req;
205 }
206
207 /**
208  * Waits for and receives results of asynchronous Lookup call
209  *
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
214  */
215
216 NTSTATUS libnet_LookupDCs_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
217                                struct libnet_LookupDCs *io)
218 {
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;
229 }
230
231
232 /**
233  * Synchronous version of LookupDCs
234  */
235 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
236                           struct libnet_LookupDCs *io)
237 {
238         struct tevent_req *req = libnet_LookupDCs_send(ctx, mem_ctx, io);
239         return libnet_LookupDCs_recv(req, mem_ctx, io);
240 }
241
242
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;
251
252         /* information about the progress */
253         void (*monitor_fn)(struct monitor_msg *);
254 };
255
256
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);
262
263
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*))
268 {
269         struct composite_context *c;
270         struct lookup_name_state *s;
271         struct tevent_req *subreq;
272         bool prereq_met = false;
273
274         c = composite_create(mem_ctx, ctx->event_ctx);
275         if (c == NULL) return NULL;
276
277         s = talloc_zero(c, struct lookup_name_state);
278         if (composite_nomem(s, c)) return c;
279
280         c->private_data = s;
281         
282         s->name = talloc_strdup(c, io->in.name);
283         s->monitor_fn = monitor;
284         s->ctx = ctx;
285
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;
289
290         if (!prepare_lookup_params(ctx, c, s)) return c;
291
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;
296
297         tevent_req_set_callback(subreq, continue_name_found, c);
298         return c;
299 }
300
301
302 static bool prepare_lookup_params(struct libnet_context *ctx,
303                                   struct composite_context *c,
304                                   struct lookup_name_state *s)
305 {
306         const int single_name = 1;
307
308         s->sids.count = 0;
309         s->sids.sids  = NULL;
310         
311         s->names = talloc_array(ctx, struct lsa_String, single_name);
312         if (composite_nomem(s->names, c)) return false;
313         s->names[0].string = s->name;
314         
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(ctx, struct lsa_RefDomainList *);
324         if (composite_nomem(s->lookup.out.domains, c)) return false;
325         
326         return true;
327 }
328
329
330 static void continue_lookup_name(struct composite_context *ctx)
331 {
332         struct composite_context *c;
333         struct lookup_name_state *s;
334         struct tevent_req *subreq;
335
336         c = talloc_get_type(ctx->async.private_data, struct composite_context);
337         s = talloc_get_type(c->private_data, struct lookup_name_state);
338
339         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
340         if (!composite_is_ok(c)) return;
341         
342         if (!prepare_lookup_params(s->ctx, c, s)) return;
343
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;
348         
349         tevent_req_set_callback(subreq, continue_name_found, c);
350 }
351
352
353 static void continue_name_found(struct tevent_req *subreq)
354 {
355         struct composite_context *c;
356         struct lookup_name_state *s;
357
358         c = tevent_req_callback_data(subreq, struct composite_context);
359         s = talloc_get_type(c->private_data, struct lookup_name_state);
360
361         c->status = dcerpc_lsa_LookupNames_r_recv(subreq, s);
362         TALLOC_FREE(subreq);
363         if (!composite_is_ok(c)) return;
364
365         c->status = s->lookup.out.result;
366         if (!composite_is_ok(c)) return;
367
368         composite_done(c);
369 }
370
371
372 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
373                                 struct libnet_LookupName *io)
374 {
375         NTSTATUS status;
376         struct lookup_name_state *s;
377
378         status = composite_wait(c);
379
380         if (NT_STATUS_IS_OK(status)) {
381                 s = talloc_get_type(c->private_data, struct lookup_name_state);
382
383                 io->out.rid = 0;
384                 io->out.sid = NULL;
385                 io->out.sidstr = NULL;
386
387                 if (*s->lookup.out.count > 0) {
388                         struct lsa_RefDomainList *domains = *s->lookup.out.domains;
389                         struct lsa_TransSidArray *sids = s->lookup.out.sids;
390
391                         if (domains == NULL || sids == NULL) {
392                                 status = NT_STATUS_UNSUCCESSFUL;
393                                 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
394                                 goto done;
395                         }
396
397                         if (sids->count > 0) {
398                                 io->out.rid        = sids->sids[0].rid;
399                                 io->out.sid_type   = sids->sids[0].sid_type;
400                                 if (domains->count > 0) {
401                                         io->out.sid = dom_sid_add_rid(mem_ctx, domains->domains[0].sid, io->out.rid);
402                                         NT_STATUS_HAVE_NO_MEMORY(io->out.sid);
403                                         io->out.sidstr = dom_sid_string(mem_ctx, io->out.sid);
404                                         NT_STATUS_HAVE_NO_MEMORY(io->out.sidstr);
405                                 }
406                         }
407                 }
408
409                 io->out.error_string = talloc_strdup(mem_ctx, "Success");
410
411         } else if (!NT_STATUS_IS_OK(status)) {
412                 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
413         }
414
415 done:
416         talloc_free(c);
417         return status;
418 }
419
420
421 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
422                            struct libnet_LookupName *io)
423 {
424         struct composite_context *c;
425         
426         c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
427         return libnet_LookupName_recv(c, mem_ctx, io);
428 }