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