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