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