CVE-2018-16852 dcerpc dnsserver: Verification tests
[Samba.git] / source3 / utils / net_util.c
bloba84b4f5500e61e1af0cd2adf23907ba42b1e37ab
1 /*
2 * Unix SMB/CIFS implementation.
3 * Helper routines for net
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Kai Blin 2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "utils/net.h"
24 #include "libsmb/namequery.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../librpc/gen_ndr/ndr_lsa_c.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "../librpc/gen_ndr/ndr_dssetup_c.h"
29 #include "secrets.h"
30 #include "../libcli/security/security.h"
31 #include "libsmb/libsmb.h"
33 NTSTATUS net_rpc_lookup_name(struct net_context *c,
34 TALLOC_CTX *mem_ctx, struct cli_state *cli,
35 const char *name, const char **ret_domain,
36 const char **ret_name, struct dom_sid *ret_sid,
37 enum lsa_SidType *ret_type)
39 struct rpc_pipe_client *lsa_pipe = NULL;
40 struct policy_handle pol;
41 NTSTATUS status, result;
42 const char **dom_names;
43 struct dom_sid *sids;
44 enum lsa_SidType *types;
45 struct dcerpc_binding_handle *b;
47 ZERO_STRUCT(pol);
49 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
50 &lsa_pipe);
51 if (!NT_STATUS_IS_OK(status)) {
52 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
53 return status;
56 b = lsa_pipe->binding_handle;
58 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
59 SEC_FLAG_MAXIMUM_ALLOWED,
60 &pol);
61 if (!NT_STATUS_IS_OK(status)) {
62 d_fprintf(stderr, "open_policy %s: %s\n", _("failed"),
63 nt_errstr(status));
64 return status;
67 status = rpccli_lsa_lookup_names(lsa_pipe, mem_ctx, &pol, 1,
68 &name, &dom_names, 1, &sids, &types);
70 if (!NT_STATUS_IS_OK(status)) {
71 /* This can happen easily, don't log an error */
72 goto done;
75 if (ret_domain != NULL) {
76 *ret_domain = dom_names[0];
78 if (ret_name != NULL) {
79 *ret_name = talloc_strdup(mem_ctx, name);
81 if (ret_sid != NULL) {
82 sid_copy(ret_sid, &sids[0]);
84 if (ret_type != NULL) {
85 *ret_type = types[0];
88 done:
89 if (is_valid_policy_hnd(&pol)) {
90 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
92 TALLOC_FREE(lsa_pipe);
94 return status;
97 /****************************************************************************
98 Connect to \\server\service.
99 ****************************************************************************/
101 NTSTATUS connect_to_service(struct net_context *c,
102 struct cli_state **cli_ctx,
103 const struct sockaddr_storage *server_ss,
104 const char *server_name,
105 const char *service_name,
106 const char *service_type)
108 NTSTATUS nt_status;
109 int flags = 0;
110 enum smb_signing_setting signing_setting = SMB_SIGNING_DEFAULT;
112 c->opt_password = net_prompt_pass(c, c->opt_user_name);
114 if (c->opt_kerberos) {
115 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
118 if (c->opt_kerberos && c->opt_password) {
119 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
122 if (c->opt_ccache) {
123 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
126 if (strequal(service_type, "IPC")) {
127 signing_setting = SMB_SIGNING_IPC_DEFAULT;
130 nt_status = cli_full_connection(cli_ctx, NULL, server_name,
131 server_ss, c->opt_port,
132 service_name, service_type,
133 c->opt_user_name, c->opt_workgroup,
134 c->opt_password, flags,
135 signing_setting);
136 if (!NT_STATUS_IS_OK(nt_status)) {
137 d_fprintf(stderr, _("Could not connect to server %s\n"),
138 server_name);
140 /* Display a nicer message depending on the result */
142 if (NT_STATUS_V(nt_status) ==
143 NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
144 d_fprintf(stderr,
145 _("The username or password was not "
146 "correct.\n"));
148 if (NT_STATUS_V(nt_status) ==
149 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
150 d_fprintf(stderr, _("The account was locked out.\n"));
152 if (NT_STATUS_V(nt_status) ==
153 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
154 d_fprintf(stderr, _("The account was disabled.\n"));
155 return nt_status;
158 if (c->smb_encrypt) {
159 nt_status = cli_cm_force_encryption(*cli_ctx,
160 c->opt_user_name,
161 c->opt_password,
162 c->opt_workgroup,
163 service_name);
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 cli_shutdown(*cli_ctx);
166 *cli_ctx = NULL;
170 return nt_status;
173 /****************************************************************************
174 Connect to \\server\ipc$.
175 ****************************************************************************/
177 NTSTATUS connect_to_ipc(struct net_context *c,
178 struct cli_state **cli_ctx,
179 const struct sockaddr_storage *server_ss,
180 const char *server_name)
182 return connect_to_service(c, cli_ctx, server_ss, server_name, "IPC$",
183 "IPC");
186 /****************************************************************************
187 Connect to \\server\ipc$ anonymously.
188 ****************************************************************************/
190 NTSTATUS connect_to_ipc_anonymous(struct net_context *c,
191 struct cli_state **cli_ctx,
192 const struct sockaddr_storage *server_ss,
193 const char *server_name)
195 NTSTATUS nt_status;
197 nt_status = cli_full_connection(cli_ctx, c->opt_requester_name,
198 server_name, server_ss, c->opt_port,
199 "IPC$", "IPC",
200 "", "",
201 "", 0, SMB_SIGNING_DEFAULT);
203 if (NT_STATUS_IS_OK(nt_status)) {
204 return nt_status;
205 } else {
206 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status)));
207 return nt_status;
212 * Connect a server and open a given pipe
214 * @param cli_dst A cli_state
215 * @param pipe The pipe to open
216 * @param got_pipe boolean that stores if we got a pipe
218 * @return Normal NTSTATUS return.
220 NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
221 struct rpc_pipe_client **pp_pipe_hnd,
222 const struct ndr_interface_table *table)
224 NTSTATUS nt_status;
225 char *server_name = SMB_STRDUP("127.0.0.1");
226 struct cli_state *cli_tmp = NULL;
227 struct rpc_pipe_client *pipe_hnd = NULL;
229 if (server_name == NULL) {
230 return NT_STATUS_NO_MEMORY;
233 if (c->opt_destination) {
234 SAFE_FREE(server_name);
235 if ((server_name = SMB_STRDUP(c->opt_destination)) == NULL) {
236 return NT_STATUS_NO_MEMORY;
240 /* make a connection to a named pipe */
241 nt_status = connect_to_ipc(c, &cli_tmp, NULL, server_name);
242 if (!NT_STATUS_IS_OK(nt_status)) {
243 SAFE_FREE(server_name);
244 return nt_status;
247 nt_status = cli_rpc_pipe_open_noauth(cli_tmp, table,
248 &pipe_hnd);
249 if (!NT_STATUS_IS_OK(nt_status)) {
250 DEBUG(0, ("couldn't not initialize pipe\n"));
251 cli_shutdown(cli_tmp);
252 SAFE_FREE(server_name);
253 return nt_status;
256 *cli_dst = cli_tmp;
257 *pp_pipe_hnd = pipe_hnd;
258 SAFE_FREE(server_name);
260 return nt_status;
263 /****************************************************************************
264 Use the local machine account (krb) and password for this session.
265 ****************************************************************************/
267 int net_use_krb_machine_account(struct net_context *c)
269 char *user_name = NULL;
271 if (!secrets_init()) {
272 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
273 exit(1);
276 c->opt_password = secrets_fetch_machine_password(
277 c->opt_target_workgroup, NULL, NULL);
278 if (asprintf(&user_name, "%s$@%s", lp_netbios_name(), lp_realm()) == -1) {
279 return -1;
281 c->opt_user_name = user_name;
282 return 0;
285 /****************************************************************************
286 Use the machine account name and password for this session.
287 ****************************************************************************/
289 int net_use_machine_account(struct net_context *c)
291 char *user_name = NULL;
293 if (!secrets_init()) {
294 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
295 exit(1);
298 c->opt_password = secrets_fetch_machine_password(
299 c->opt_target_workgroup, NULL, NULL);
300 if (asprintf(&user_name, "%s$", lp_netbios_name()) == -1) {
301 return -1;
303 c->opt_user_name = user_name;
304 return 0;
307 bool net_find_server(struct net_context *c,
308 const char *domain,
309 unsigned flags,
310 struct sockaddr_storage *server_ss,
311 char **server_name)
313 const char *d = domain ? domain : c->opt_target_workgroup;
315 if (c->opt_host) {
316 *server_name = SMB_STRDUP(c->opt_host);
319 if (c->opt_have_ip) {
320 *server_ss = c->opt_dest_ip;
321 if (!*server_name) {
322 char addr[INET6_ADDRSTRLEN];
323 print_sockaddr(addr, sizeof(addr), &c->opt_dest_ip);
324 *server_name = SMB_STRDUP(addr);
326 } else if (*server_name) {
327 /* resolve the IP address */
328 if (!resolve_name(*server_name, server_ss, 0x20, false)) {
329 DEBUG(1,("Unable to resolve server name\n"));
330 return false;
332 } else if (flags & NET_FLAGS_PDC) {
333 fstring dc_name;
334 struct sockaddr_storage pdc_ss;
336 if (!get_pdc_ip(d, &pdc_ss)) {
337 DEBUG(1,("Unable to resolve PDC server address\n"));
338 return false;
341 if (is_zero_addr(&pdc_ss)) {
342 return false;
345 if (!name_status_find(d, 0x1b, 0x20, &pdc_ss, dc_name)) {
346 return false;
349 *server_name = SMB_STRDUP(dc_name);
350 *server_ss = pdc_ss;
351 } else if (flags & NET_FLAGS_DMB) {
352 struct sockaddr_storage msbrow_ss;
353 char addr[INET6_ADDRSTRLEN];
355 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
356 if (!resolve_name(d, &msbrow_ss, 0x1B, false)) {
357 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
358 return false;
360 *server_ss = msbrow_ss;
361 print_sockaddr(addr, sizeof(addr), server_ss);
362 *server_name = SMB_STRDUP(addr);
363 } else if (flags & NET_FLAGS_MASTER) {
364 struct sockaddr_storage brow_ss;
365 char addr[INET6_ADDRSTRLEN];
366 if (!resolve_name(d, &brow_ss, 0x1D, false)) {
367 /* go looking for workgroups */
368 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
369 return false;
371 *server_ss = brow_ss;
372 print_sockaddr(addr, sizeof(addr), server_ss);
373 *server_name = SMB_STRDUP(addr);
374 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
375 if (!interpret_string_addr(server_ss,
376 "127.0.0.1", AI_NUMERICHOST)) {
377 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
378 return false;
380 *server_name = SMB_STRDUP("127.0.0.1");
383 if (!*server_name) {
384 DEBUG(1,("no server to connect to\n"));
385 return false;
388 return true;
391 bool net_find_pdc(struct sockaddr_storage *server_ss,
392 fstring server_name,
393 const char *domain_name)
395 if (!get_pdc_ip(domain_name, server_ss)) {
396 return false;
398 if (is_zero_addr(server_ss)) {
399 return false;
402 if (!name_status_find(domain_name, 0x1b, 0x20, server_ss, server_name)) {
403 return false;
406 return true;
409 NTSTATUS net_make_ipc_connection(struct net_context *c, unsigned flags,
410 struct cli_state **pcli)
412 return net_make_ipc_connection_ex(c, c->opt_workgroup, NULL, NULL, flags, pcli);
415 NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
416 const char *server,
417 const struct sockaddr_storage *pss,
418 unsigned flags, struct cli_state **pcli)
420 char *server_name = NULL;
421 struct sockaddr_storage server_ss;
422 struct cli_state *cli = NULL;
423 NTSTATUS nt_status;
425 if ( !server || !pss ) {
426 if (!net_find_server(c, domain, flags, &server_ss,
427 &server_name)) {
428 d_fprintf(stderr, _("Unable to find a suitable server "
429 "for domain %s\n"), domain);
430 nt_status = NT_STATUS_UNSUCCESSFUL;
431 goto done;
433 } else {
434 server_name = SMB_STRDUP( server );
435 server_ss = *pss;
438 if (flags & NET_FLAGS_ANONYMOUS) {
439 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
440 server_name);
441 } else {
442 nt_status = connect_to_ipc(c, &cli, &server_ss,
443 server_name);
446 /* store the server in the affinity cache if it was a PDC */
448 if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
449 saf_store(cli->server_domain, server_name);
451 SAFE_FREE(server_name);
452 if (!NT_STATUS_IS_OK(nt_status)) {
453 d_fprintf(stderr, _("Connection failed: %s\n"),
454 nt_errstr(nt_status));
455 cli = NULL;
456 } else if (c->opt_request_timeout) {
457 cli_set_timeout(cli, c->opt_request_timeout * 1000);
460 done:
461 if (pcli != NULL) {
462 *pcli = cli;
464 return nt_status;
467 /****************************************************************************
468 ****************************************************************************/
470 const char *net_prompt_pass(struct net_context *c, const char *user)
472 char *prompt = NULL;
473 char pwd[256] = {0};
474 int rc;
476 if (c->opt_password) {
477 return c->opt_password;
480 if (c->opt_machine_pass) {
481 return NULL;
484 if (c->opt_kerberos && !c->opt_user_specified) {
485 return NULL;
488 if (asprintf(&prompt, _("Enter %s's password:"), user) == -1) {
489 return NULL;
492 rc = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
493 SAFE_FREE(prompt);
494 if (rc < 0) {
495 return NULL;
498 return SMB_STRDUP(pwd);
501 int net_run_function(struct net_context *c, int argc, const char **argv,
502 const char *whoami, struct functable *table)
504 int i;
506 if (argc != 0) {
507 for (i=0; table[i].funcname != NULL; i++) {
508 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
509 return table[i].fn(c, argc-1, argv+1);
513 if (c->display_usage == false) {
514 d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
515 (argc > 0)?argv[0]:"");
517 d_printf(_("Usage:\n"));
518 for (i=0; table[i].funcname != NULL; i++) {
519 if(c->display_usage == false)
520 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
521 _(table[i].description));
522 else
523 d_printf("%s\n", _(table[i].usage));
526 return c->display_usage?0:-1;
529 void net_display_usage_from_functable(struct functable *table)
531 int i;
532 for (i=0; table[i].funcname != NULL; i++) {
533 d_printf("%s\n", _(table[i].usage));
537 const char *net_share_type_str(int num_type)
539 switch(num_type) {
540 case 0: return _("Disk");
541 case 1: return _("Print");
542 case 2: return _("Dev");
543 case 3: return _("IPC");
544 default: return _("Unknown");
548 static NTSTATUS net_scan_dc_noad(struct net_context *c,
549 struct cli_state *cli,
550 struct net_dc_info *dc_info)
552 TALLOC_CTX *mem_ctx = talloc_tos();
553 struct rpc_pipe_client *pipe_hnd = NULL;
554 struct dcerpc_binding_handle *b;
555 NTSTATUS status, result;
556 struct policy_handle pol;
557 union lsa_PolicyInformation *info;
559 ZERO_STRUCTP(dc_info);
560 ZERO_STRUCT(pol);
562 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
563 &pipe_hnd);
564 if (!NT_STATUS_IS_OK(status)) {
565 return status;
568 b = pipe_hnd->binding_handle;
570 status = dcerpc_lsa_open_policy(b, mem_ctx,
571 false,
572 SEC_FLAG_MAXIMUM_ALLOWED,
573 &pol,
574 &result);
575 if (!NT_STATUS_IS_OK(status)) {
576 goto done;
578 if (!NT_STATUS_IS_OK(result)) {
579 status = result;
580 goto done;
583 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
584 &pol,
585 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
586 &info,
587 &result);
588 if (!NT_STATUS_IS_OK(status)) {
589 goto done;
591 if (!NT_STATUS_IS_OK(result)) {
592 status = result;
593 goto done;
596 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info->account_domain.name.string);
597 if (dc_info->netbios_domain_name == NULL) {
598 status = NT_STATUS_NO_MEMORY;
599 goto done;
602 done:
603 if (is_valid_policy_hnd(&pol)) {
604 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
607 TALLOC_FREE(pipe_hnd);
609 return status;
612 NTSTATUS net_scan_dc(struct net_context *c,
613 struct cli_state *cli,
614 struct net_dc_info *dc_info)
616 TALLOC_CTX *mem_ctx = talloc_tos();
617 struct rpc_pipe_client *dssetup_pipe = NULL;
618 struct dcerpc_binding_handle *dssetup_handle = NULL;
619 union dssetup_DsRoleInfo info;
620 NTSTATUS status;
621 WERROR werr;
623 ZERO_STRUCTP(dc_info);
625 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_dssetup,
626 &dssetup_pipe);
627 if (!NT_STATUS_IS_OK(status)) {
628 DEBUG(10,("net_scan_dc: failed to open dssetup pipe with %s, "
629 "retrying with lsa pipe\n", nt_errstr(status)));
630 return net_scan_dc_noad(c, cli, dc_info);
632 dssetup_handle = dssetup_pipe->binding_handle;
634 status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_handle, mem_ctx,
635 DS_ROLE_BASIC_INFORMATION,
636 &info,
637 &werr);
638 TALLOC_FREE(dssetup_pipe);
640 if (NT_STATUS_IS_OK(status)) {
641 status = werror_to_ntstatus(werr);
643 if (!NT_STATUS_IS_OK(status)) {
644 return status;
647 dc_info->is_dc = (info.basic.role & (DS_ROLE_PRIMARY_DC|DS_ROLE_BACKUP_DC));
648 dc_info->is_pdc = (info.basic.role & DS_ROLE_PRIMARY_DC);
649 dc_info->is_ad = (info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING);
650 dc_info->is_mixed_mode = (info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE);
651 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info.basic.domain);
652 dc_info->dns_domain_name = talloc_strdup(mem_ctx, info.basic.dns_domain);
653 dc_info->forest_name = talloc_strdup(mem_ctx, info.basic.forest);
655 return NT_STATUS_OK;