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