Add context for libcli_resolve.
[samba-svnmirror.git] / source / 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 "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"
36
37 #include "param/param.h"
38
39 struct lookup_state {
40         struct nbt_name hostname;
41         const char *address;
42 };
43
44
45 static void continue_name_resolved(struct composite_context *ctx);
46
47
48 /**
49  * Sends asynchronous Lookup request
50  *
51  * @param io arguments and result of the call
52  */
53
54 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
55                                              struct libnet_Lookup *io)
56 {
57         struct composite_context *c;
58         struct lookup_state *s;
59         struct composite_context *cresolve_req;
60         struct resolve_context *resolve_ctx;
61
62         /* allocate context and state structures */
63         c = composite_create(ctx, ctx->event_ctx);
64         if (c == NULL) return NULL;
65
66         s = talloc_zero(c, struct lookup_state);
67         if (composite_nomem(s, c)) return c;
68
69         c->private_data = s;
70
71         if (io == NULL || io->in.hostname == NULL) {
72                 composite_error(c, NT_STATUS_INVALID_PARAMETER);
73                 return c;
74         }
75
76         /* parameters */
77         s->hostname.name   = talloc_strdup(s, io->in.hostname);
78         if (composite_nomem(s->hostname.name, c)) return c;
79
80         s->hostname.type   = io->in.type;
81         s->hostname.scope  = NULL;
82
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;
88         }
89
90         /* send resolve request */
91         cresolve_req = resolve_name_send(resolve_ctx, &s->hostname, c->event_ctx);
92         if (composite_nomem(cresolve_req, c)) return c;
93
94         composite_continue(c, cresolve_req, continue_name_resolved, c);
95         return c;
96 }
97
98
99 static void continue_name_resolved(struct composite_context *ctx)
100 {
101         struct composite_context *c;
102         struct lookup_state *s;
103
104         c = talloc_get_type(ctx->async.private_data, struct composite_context);
105         s = talloc_get_type(c->private_data, struct lookup_state);
106
107         c->status = resolve_name_recv(ctx, s, &s->address);
108         
109         composite_done(c);
110 }
111
112
113 /**
114  * Waits for and receives results of asynchronous Lookup call
115  *
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
120  */
121
122 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
123                             struct libnet_Lookup *io)
124 {
125         NTSTATUS status;
126         struct lookup_state *s;
127
128         status = composite_wait(c);
129         if (NT_STATUS_IS_OK(status)) {
130                 s = talloc_get_type(c->private_data, struct lookup_state);
131
132                 io->out.address = str_list_make(mem_ctx, s->address, NULL);
133                 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
134         }
135
136         talloc_free(c);
137         return status;
138 }
139
140
141 /**
142  * Synchronous version of Lookup call
143  *
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
147  */
148
149 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
150                        struct libnet_Lookup *io)
151 {
152         struct composite_context *c = libnet_Lookup_send(ctx, io);
153         return libnet_Lookup_recv(c, mem_ctx, io);
154 }
155
156
157 /*
158  * Shortcut functions to find common types of name
159  * (and skip nbt name type argument)
160  */
161
162
163 /**
164  * Sends asynchronous LookupHost request
165  */
166 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
167                                                  struct libnet_Lookup *io)
168 {
169         io->in.type = NBT_NAME_SERVER;
170         return libnet_Lookup_send(ctx, io);
171 }
172
173
174
175 /**
176  * Synchronous version of LookupHost call
177  */
178 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
179                            struct libnet_Lookup *io)
180 {
181         struct composite_context *c = libnet_LookupHost_send(ctx, io);
182         return libnet_Lookup_recv(c, mem_ctx, io);
183 }
184
185
186 /**
187  * Sends asynchronous LookupDCs request
188  */
189 struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
190                                                 TALLOC_CTX *mem_ctx,
191                                                 struct libnet_LookupDCs *io)
192 {
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);
196
197         c = finddcs_send(mem_ctx, lp_netbios_name(ctx->lp_ctx),
198                          io->in.domain_name, io->in.name_type,
199                          NULL, ctx->resolve_ctx, ctx->event_ctx, msg_ctx);
200         return c;
201 }
202
203 /**
204  * Waits for and receives results of asynchronous Lookup call
205  *
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
210  */
211
212 NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
213                                struct libnet_LookupDCs *io)
214 {
215         NTSTATUS status;
216         status = finddcs_recv(c, mem_ctx, &io->out.num_dcs, &io->out.dcs);
217         if (!NT_STATUS_IS_OK(status)) {
218                 return status;
219         }
220         return status;
221 }
222
223
224 /**
225  * Synchronous version of LookupDCs
226  */
227 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
228                           struct libnet_LookupDCs *io)
229 {
230         struct composite_context *c = libnet_LookupDCs_send(ctx, mem_ctx, io);
231         return libnet_LookupDCs_recv(c, mem_ctx, io);
232 }
233
234
235 struct lookup_name_state {
236         struct libnet_context *ctx;
237         const char *name;
238         uint32_t count;
239         struct libnet_DomainOpen domopen;
240         struct lsa_LookupNames lookup;
241         struct lsa_TransSidArray sids;
242         struct lsa_String *names;
243
244         /* information about the progress */
245         void (*monitor_fn)(struct monitor_msg *);
246 };
247
248
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);
254
255
256 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
257                                                  TALLOC_CTX *mem_ctx,
258                                                  struct libnet_LookupName *io,
259                                                  void (*monitor)(struct monitor_msg*))
260 {
261         struct composite_context *c;
262         struct lookup_name_state *s;
263         struct rpc_request *lookup_req;
264         bool prereq_met = false;
265
266         c = composite_create(mem_ctx, ctx->event_ctx);
267         if (c == NULL) return NULL;
268
269         s = talloc_zero(c, struct lookup_name_state);
270         if (composite_nomem(s, c)) return c;
271
272         c->private_data = s;
273         
274         s->name = talloc_strdup(c, io->in.name);
275         s->monitor_fn = monitor;
276         s->ctx = ctx;
277
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;
281
282         if (!prepare_lookup_params(ctx, c, s)) return c;
283
284         lookup_req = dcerpc_lsa_LookupNames_send(ctx->lsa.pipe, c, &s->lookup);
285         if (composite_nomem(lookup_req, c)) return c;
286
287         composite_continue_rpc(c, lookup_req, continue_name_found, c);
288         return c;
289 }
290
291
292 static bool prepare_lookup_params(struct libnet_context *ctx,
293                                   struct composite_context *c,
294                                   struct lookup_name_state *s)
295 {
296         const int single_name = 1;
297
298         s->sids.count = 0;
299         s->sids.sids  = NULL;
300         
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;
304         
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;
313         
314         return true;
315 }
316
317
318 static void continue_lookup_name(struct composite_context *ctx)
319 {
320         struct composite_context *c;
321         struct lookup_name_state *s;
322         struct rpc_request *lookup_req;
323
324         c = talloc_get_type(ctx->async.private_data, struct composite_context);
325         s = talloc_get_type(c->private_data, struct lookup_name_state);
326
327         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
328         if (!composite_is_ok(c)) return;
329         
330         if (!prepare_lookup_params(s->ctx, c, s)) return;
331
332         lookup_req = dcerpc_lsa_LookupNames_send(s->ctx->lsa.pipe, c, &s->lookup);
333         if (composite_nomem(lookup_req, c)) return;
334         
335         composite_continue_rpc(c, lookup_req, continue_name_found, c);
336 }
337
338
339 static void continue_name_found(struct rpc_request *req)
340 {
341         struct composite_context *c;
342         struct lookup_name_state *s;
343
344         c = talloc_get_type(req->async.private_data, struct composite_context);
345         s = talloc_get_type(c->private_data, struct lookup_name_state);
346
347         c->status = dcerpc_ndr_request_recv(req);
348         if (!composite_is_ok(c)) return;
349
350         c->status = s->lookup.out.result;
351         if (!composite_is_ok(c)) return;
352
353         composite_done(c);
354 }
355
356
357 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
358                                 struct libnet_LookupName *io)
359 {
360         NTSTATUS status;
361         struct lookup_name_state *s;
362
363         status = composite_wait(c);
364
365         if (NT_STATUS_IS_OK(status)) {
366                 s = talloc_get_type(c->private_data, struct lookup_name_state);
367
368                 io->out.rid = 0;
369                 io->out.sid = NULL;
370                 io->out.sidstr = NULL;
371
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;
375
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));
379                                 goto done;
380                         }
381
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);
390                                 }
391                         }
392                 }
393
394                 io->out.error_string = talloc_strdup(mem_ctx, "Success");
395
396         } else if (!NT_STATUS_IS_OK(status)) {
397                 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
398         }
399
400 done:
401         talloc_free(c);
402         return status;
403 }
404
405
406 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
407                            struct libnet_LookupName *io)
408 {
409         struct composite_context *c;
410         
411         c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
412         return libnet_LookupName_recv(c, mem_ctx, io);
413 }