3677c2a31e79125e242a4a0194d94226dfd9565b
[kai/samba.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 "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/libcli.h"
33 #include "libcli/finddc.h"
34 #include "libcli/security/security.h"
35 #include "librpc/gen_ndr/lsa.h"
36 #include "librpc/gen_ndr/ndr_lsa_c.h"
37
38 #include "param/param.h"
39
40 struct lookup_state {
41         struct nbt_name hostname;
42         const char *address;
43 };
44
45
46 static void continue_name_resolved(struct composite_context *ctx);
47
48
49 /**
50  * Sends asynchronous Lookup request
51  *
52  * @param io arguments and result of the call
53  */
54
55 struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
56                                              struct libnet_Lookup *io)
57 {
58         struct composite_context *c;
59         struct lookup_state *s;
60         struct composite_context *cresolve_req;
61         struct resolve_context *resolve_ctx;
62
63         /* allocate context and state structures */
64         c = composite_create(ctx, ctx->event_ctx);
65         if (c == NULL) return NULL;
66
67         s = talloc_zero(c, struct lookup_state);
68         if (composite_nomem(s, c)) return c;
69
70         c->private_data = s;
71
72         if (io == NULL || io->in.hostname == NULL) {
73                 composite_error(c, NT_STATUS_INVALID_PARAMETER);
74                 return c;
75         }
76
77         /* parameters */
78         s->hostname.name   = talloc_strdup(s, io->in.hostname);
79         if (composite_nomem(s->hostname.name, c)) return c;
80
81         s->hostname.type   = io->in.type;
82         s->hostname.scope  = NULL;
83
84         /* name resolution methods */
85         if (io->in.resolve_ctx) {
86                 resolve_ctx = io->in.resolve_ctx;
87         } else {
88                 resolve_ctx = ctx->resolve_ctx;
89         }
90
91         /* send resolve request */
92         cresolve_req = resolve_name_send(resolve_ctx, s, &s->hostname, c->event_ctx);
93         if (composite_nomem(cresolve_req, c)) return c;
94
95         composite_continue(c, cresolve_req, continue_name_resolved, c);
96         return c;
97 }
98
99
100 static void continue_name_resolved(struct composite_context *ctx)
101 {
102         struct composite_context *c;
103         struct lookup_state *s;
104
105         c = talloc_get_type(ctx->async.private_data, struct composite_context);
106         s = talloc_get_type(c->private_data, struct lookup_state);
107
108         c->status = resolve_name_recv(ctx, s, &s->address);
109         
110         composite_done(c);
111 }
112
113
114 /**
115  * Waits for and receives results of asynchronous Lookup call
116  *
117  * @param c composite context returned by asynchronous Lookup call
118  * @param mem_ctx memory context of the call
119  * @param io pointer to results (and arguments) of the call
120  * @return nt status code of execution
121  */
122
123 NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
124                             struct libnet_Lookup *io)
125 {
126         NTSTATUS status;
127         struct lookup_state *s;
128
129         status = composite_wait(c);
130         if (NT_STATUS_IS_OK(status)) {
131                 s = talloc_get_type(c->private_data, struct lookup_state);
132
133                 io->out.address = (const char **)str_list_make_single(mem_ctx, s->address);
134                 NT_STATUS_HAVE_NO_MEMORY(io->out.address);
135         }
136
137         talloc_free(c);
138         return status;
139 }
140
141
142 /**
143  * Synchronous version of Lookup call
144  *
145  * @param mem_ctx memory context for the call
146  * @param io arguments and results of the call
147  * @return nt status code of execution
148  */
149
150 NTSTATUS libnet_Lookup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
151                        struct libnet_Lookup *io)
152 {
153         struct composite_context *c = libnet_Lookup_send(ctx, io);
154         return libnet_Lookup_recv(c, mem_ctx, io);
155 }
156
157
158 /*
159  * Shortcut functions to find common types of name
160  * (and skip nbt name type argument)
161  */
162
163
164 /**
165  * Sends asynchronous LookupHost request
166  */
167 struct composite_context* libnet_LookupHost_send(struct libnet_context *ctx,
168                                                  struct libnet_Lookup *io)
169 {
170         io->in.type = NBT_NAME_SERVER;
171         return libnet_Lookup_send(ctx, io);
172 }
173
174
175
176 /**
177  * Synchronous version of LookupHost call
178  */
179 NTSTATUS libnet_LookupHost(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
180                            struct libnet_Lookup *io)
181 {
182         struct composite_context *c = libnet_LookupHost_send(ctx, io);
183         return libnet_Lookup_recv(c, mem_ctx, io);
184 }
185
186
187 /**
188  * Sends asynchronous LookupDCs request
189  */
190 struct tevent_req *libnet_LookupDCs_send(struct libnet_context *ctx,
191                                          TALLOC_CTX *mem_ctx,
192                                          struct libnet_LookupDCs *io)
193 {
194         struct tevent_req *req;
195         struct finddcs finddcs_io;
196
197         ZERO_STRUCT(finddcs_io);
198         finddcs_io.in.domain_name = lpcfg_realm(ctx->lp_ctx);
199         if (strcmp(finddcs_io.in.domain_name, "") == 0) {
200                 finddcs_io.in.domain_name = lpcfg_workgroup(ctx->lp_ctx);
201         }
202         finddcs_io.in.minimum_dc_flags = NBT_SERVER_LDAP | NBT_SERVER_DS | NBT_SERVER_WRITABLE;
203
204
205         req = finddcs_cldap_send(mem_ctx, &finddcs_io, ctx->resolve_ctx, ctx->event_ctx);
206         return req;
207 }
208
209 /**
210  * Waits for and receives results of asynchronous Lookup call
211  *
212  * @param c composite context returned by asynchronous Lookup call
213  * @param mem_ctx memory context of the call
214  * @param io pointer to results (and arguments) of the call
215  * @return nt status code of execution
216  */
217
218 NTSTATUS libnet_LookupDCs_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
219                                struct libnet_LookupDCs *io)
220 {
221         NTSTATUS status;
222         struct finddcs finddcs_io;
223         status = finddcs_cldap_recv(req, mem_ctx, &finddcs_io);
224         talloc_free(req);
225         io->out.num_dcs = 1;
226         io->out.dcs = talloc(mem_ctx, struct nbt_dc_name);
227         NT_STATUS_HAVE_NO_MEMORY(io->out.dcs);
228         io->out.dcs[0].address = finddcs_io.out.address;
229         io->out.dcs[0].name = finddcs_io.out.netlogon.data.nt5_ex.pdc_dns_name;
230         return status;
231 }
232
233
234 /**
235  * Synchronous version of LookupDCs
236  */
237 NTSTATUS libnet_LookupDCs(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
238                           struct libnet_LookupDCs *io)
239 {
240         struct tevent_req *req = libnet_LookupDCs_send(ctx, mem_ctx, io);
241         return libnet_LookupDCs_recv(req, mem_ctx, io);
242 }
243
244
245 struct lookup_name_state {
246         struct libnet_context *ctx;
247         const char *name;
248         uint32_t count;
249         struct libnet_DomainOpen domopen;
250         struct lsa_LookupNames lookup;
251         struct lsa_TransSidArray sids;
252         struct lsa_String *names;
253
254         /* information about the progress */
255         void (*monitor_fn)(struct monitor_msg *);
256 };
257
258
259 static bool prepare_lookup_params(struct libnet_context *ctx,
260                                   struct composite_context *c,
261                                   struct lookup_name_state *s);
262 static void continue_lookup_name(struct composite_context *ctx);
263 static void continue_name_found(struct tevent_req *subreq);
264
265
266 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
267                                                  TALLOC_CTX *mem_ctx,
268                                                  struct libnet_LookupName *io,
269                                                  void (*monitor)(struct monitor_msg*))
270 {
271         struct composite_context *c;
272         struct lookup_name_state *s;
273         struct tevent_req *subreq;
274         bool prereq_met = false;
275
276         c = composite_create(mem_ctx, ctx->event_ctx);
277         if (c == NULL) return NULL;
278
279         s = talloc_zero(c, struct lookup_name_state);
280         if (composite_nomem(s, c)) return c;
281
282         c->private_data = s;
283         
284         s->name = talloc_strdup(c, io->in.name);
285         s->monitor_fn = monitor;
286         s->ctx = ctx;
287
288         prereq_met = lsa_domain_opened(ctx, io->in.domain_name, &c, &s->domopen,
289                                        continue_lookup_name, monitor);
290         if (!prereq_met) return c;
291
292         if (!prepare_lookup_params(ctx, c, s)) return c;
293
294         subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
295                                                ctx->lsa.pipe->binding_handle,
296                                                &s->lookup);
297         if (composite_nomem(subreq, c)) return c;
298
299         tevent_req_set_callback(subreq, continue_name_found, c);
300         return c;
301 }
302
303
304 static bool prepare_lookup_params(struct libnet_context *ctx,
305                                   struct composite_context *c,
306                                   struct lookup_name_state *s)
307 {
308         const int single_name = 1;
309
310         s->sids.count = 0;
311         s->sids.sids  = NULL;
312         
313         s->names = talloc_array(ctx, struct lsa_String, single_name);
314         if (composite_nomem(s->names, c)) return false;
315         s->names[0].string = s->name;
316         
317         s->lookup.in.handle    = &ctx->lsa.handle;
318         s->lookup.in.num_names = single_name;
319         s->lookup.in.names     = s->names;
320         s->lookup.in.sids      = &s->sids;
321         s->lookup.in.level     = 1;
322         s->lookup.in.count     = &s->count;
323         s->lookup.out.count    = &s->count;
324         s->lookup.out.sids     = &s->sids;
325         s->lookup.out.domains  = talloc_zero(ctx, struct lsa_RefDomainList *);
326         if (composite_nomem(s->lookup.out.domains, c)) return false;
327         
328         return true;
329 }
330
331
332 static void continue_lookup_name(struct composite_context *ctx)
333 {
334         struct composite_context *c;
335         struct lookup_name_state *s;
336         struct tevent_req *subreq;
337
338         c = talloc_get_type(ctx->async.private_data, struct composite_context);
339         s = talloc_get_type(c->private_data, struct lookup_name_state);
340
341         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
342         if (!composite_is_ok(c)) return;
343         
344         if (!prepare_lookup_params(s->ctx, c, s)) return;
345
346         subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
347                                                s->ctx->lsa.pipe->binding_handle,
348                                                &s->lookup);
349         if (composite_nomem(subreq, c)) return;
350         
351         tevent_req_set_callback(subreq, continue_name_found, c);
352 }
353
354
355 static void continue_name_found(struct tevent_req *subreq)
356 {
357         struct composite_context *c;
358         struct lookup_name_state *s;
359
360         c = tevent_req_callback_data(subreq, struct composite_context);
361         s = talloc_get_type(c->private_data, struct lookup_name_state);
362
363         c->status = dcerpc_lsa_LookupNames_r_recv(subreq, s);
364         TALLOC_FREE(subreq);
365         if (!composite_is_ok(c)) return;
366
367         c->status = s->lookup.out.result;
368         if (!composite_is_ok(c)) return;
369
370         composite_done(c);
371 }
372
373
374 NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
375                                 struct libnet_LookupName *io)
376 {
377         NTSTATUS status;
378         struct lookup_name_state *s;
379
380         status = composite_wait(c);
381
382         if (NT_STATUS_IS_OK(status)) {
383                 s = talloc_get_type(c->private_data, struct lookup_name_state);
384
385                 io->out.rid = 0;
386                 io->out.sid = NULL;
387                 io->out.sidstr = NULL;
388
389                 if (*s->lookup.out.count > 0) {
390                         struct lsa_RefDomainList *domains = *s->lookup.out.domains;
391                         struct lsa_TransSidArray *sids = s->lookup.out.sids;
392
393                         if (domains == NULL || sids == NULL) {
394                                 status = NT_STATUS_UNSUCCESSFUL;
395                                 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
396                                 goto done;
397                         }
398
399                         if (sids->count > 0) {
400                                 io->out.rid        = sids->sids[0].rid;
401                                 io->out.sid_type   = sids->sids[0].sid_type;
402                                 if (domains->count > 0) {
403                                         io->out.sid = dom_sid_add_rid(mem_ctx, domains->domains[0].sid, io->out.rid);
404                                         NT_STATUS_HAVE_NO_MEMORY(io->out.sid);
405                                         io->out.sidstr = dom_sid_string(mem_ctx, io->out.sid);
406                                         NT_STATUS_HAVE_NO_MEMORY(io->out.sidstr);
407                                 }
408                         }
409                 }
410
411                 io->out.error_string = talloc_strdup(mem_ctx, "Success");
412
413         } else if (!NT_STATUS_IS_OK(status)) {
414                 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
415         }
416
417 done:
418         talloc_free(c);
419         return status;
420 }
421
422
423 NTSTATUS libnet_LookupName(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
424                            struct libnet_LookupName *io)
425 {
426         struct composite_context *c;
427         
428         c = libnet_LookupName_send(ctx, mem_ctx, io, NULL);
429         return libnet_LookupName_recv(c, mem_ctx, io);
430 }