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/>.
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"
30 #include "../libcli/security/security.h"
31 #include "libsmb/libsmb.h"
32 #include "lib/param/param.h"
33 #include "auth/gensec/gensec.h"
35 NTSTATUS net_rpc_lookup_name(struct net_context *c,
36 TALLOC_CTX *mem_ctx, struct cli_state *cli,
37 const char *name, const char **ret_domain,
38 const char **ret_name, struct dom_sid *ret_sid,
39 enum lsa_SidType *ret_type)
41 struct rpc_pipe_client *lsa_pipe = NULL;
42 struct policy_handle pol;
43 NTSTATUS status, result;
44 const char **dom_names;
46 enum lsa_SidType *types;
47 struct dcerpc_binding_handle *b;
51 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
53 if (!NT_STATUS_IS_OK(status)) {
54 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
58 b = lsa_pipe->binding_handle;
60 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
61 SEC_FLAG_MAXIMUM_ALLOWED,
63 if (!NT_STATUS_IS_OK(status)) {
64 d_fprintf(stderr, "open_policy %s: %s\n", _("failed"),
69 status = rpccli_lsa_lookup_names(lsa_pipe, mem_ctx, &pol, 1,
70 &name, &dom_names, 1, &sids, &types);
72 if (!NT_STATUS_IS_OK(status)) {
73 /* This can happen easily, don't log an error */
77 if (ret_domain != NULL) {
78 *ret_domain = dom_names[0];
80 if (ret_name != NULL) {
81 *ret_name = talloc_strdup(mem_ctx, name);
83 if (ret_sid != NULL) {
84 sid_copy(ret_sid, &sids[0]);
86 if (ret_type != NULL) {
91 if (is_valid_policy_hnd(&pol)) {
92 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
94 TALLOC_FREE(lsa_pipe);
99 /****************************************************************************
100 Connect to \\server\service.
101 ****************************************************************************/
103 NTSTATUS connect_to_service(struct net_context *c,
104 struct cli_state **cli_ctx,
105 const struct sockaddr_storage *server_ss,
106 const char *server_name,
107 const char *service_name,
108 const char *service_type)
111 enum smb_signing_setting signing_setting = SMB_SIGNING_DEFAULT;
112 struct cli_credentials *creds = NULL;
114 creds = net_context_creds(c, c);
116 d_fprintf(stderr, "net_context_creds() failed.\n");
117 return NT_STATUS_INTERNAL_ERROR;
120 if (strequal(service_type, "IPC")) {
121 signing_setting = SMB_SIGNING_IPC_DEFAULT;
124 nt_status = cli_full_connection_creds(cli_ctx, NULL, server_name,
125 server_ss, c->opt_port,
126 service_name, service_type,
129 if (!NT_STATUS_IS_OK(nt_status)) {
130 d_fprintf(stderr, _("Could not connect to server %s\n"),
133 /* Display a nicer message depending on the result */
135 if (NT_STATUS_V(nt_status) ==
136 NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
138 _("The username or password was not "
141 if (NT_STATUS_V(nt_status) ==
142 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
143 d_fprintf(stderr, _("The account was locked out.\n"));
145 if (NT_STATUS_V(nt_status) ==
146 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
147 d_fprintf(stderr, _("The account was disabled.\n"));
151 if (c->smb_encrypt) {
152 nt_status = cli_cm_force_encryption_creds(*cli_ctx,
155 if (!NT_STATUS_IS_OK(nt_status)) {
156 cli_shutdown(*cli_ctx);
164 /****************************************************************************
165 Connect to \\server\ipc$.
166 ****************************************************************************/
168 NTSTATUS connect_to_ipc(struct net_context *c,
169 struct cli_state **cli_ctx,
170 const struct sockaddr_storage *server_ss,
171 const char *server_name)
173 return connect_to_service(c, cli_ctx, server_ss, server_name, "IPC$",
177 /****************************************************************************
178 Connect to \\server\ipc$ anonymously.
179 ****************************************************************************/
181 NTSTATUS connect_to_ipc_anonymous(struct net_context *c,
182 struct cli_state **cli_ctx,
183 const struct sockaddr_storage *server_ss,
184 const char *server_name)
187 struct cli_credentials *anon_creds = NULL;
189 anon_creds = cli_credentials_init_anon(c);
190 if (anon_creds == NULL) {
191 DBG_ERR("cli_credentials_init_anon() failed\n");
192 return NT_STATUS_NO_MEMORY;
195 nt_status = cli_full_connection_creds(cli_ctx, c->opt_requester_name,
196 server_name, server_ss, c->opt_port,
198 anon_creds, 0, SMB_SIGNING_OFF);
200 if (NT_STATUS_IS_OK(nt_status)) {
203 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status)));
209 * Connect a server and open a given pipe
211 * @param cli_dst A cli_state
212 * @param pipe The pipe to open
213 * @param got_pipe boolean that stores if we got a pipe
215 * @return Normal NTSTATUS return.
217 NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
218 struct rpc_pipe_client **pp_pipe_hnd,
219 const struct ndr_interface_table *table)
222 char *server_name = SMB_STRDUP("127.0.0.1");
223 struct cli_state *cli_tmp = NULL;
224 struct rpc_pipe_client *pipe_hnd = NULL;
226 if (server_name == NULL) {
227 return NT_STATUS_NO_MEMORY;
230 if (c->opt_destination) {
231 SAFE_FREE(server_name);
232 if ((server_name = SMB_STRDUP(c->opt_destination)) == NULL) {
233 return NT_STATUS_NO_MEMORY;
237 /* make a connection to a named pipe */
238 nt_status = connect_to_ipc(c, &cli_tmp, NULL, server_name);
239 if (!NT_STATUS_IS_OK(nt_status)) {
240 SAFE_FREE(server_name);
244 nt_status = cli_rpc_pipe_open_noauth(cli_tmp, table,
246 if (!NT_STATUS_IS_OK(nt_status)) {
247 DEBUG(0, ("couldn't not initialize pipe\n"));
248 cli_shutdown(cli_tmp);
249 SAFE_FREE(server_name);
254 *pp_pipe_hnd = pipe_hnd;
255 SAFE_FREE(server_name);
260 /****************************************************************************
261 Use the local machine account (krb) and password for this session.
262 ****************************************************************************/
264 int net_use_krb_machine_account(struct net_context *c)
266 char *user_name = NULL;
268 if (!secrets_init()) {
269 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
273 c->opt_password = secrets_fetch_machine_password(
274 c->opt_target_workgroup, NULL, NULL);
275 if (asprintf(&user_name, "%s$@%s", lp_netbios_name(), lp_realm()) == -1) {
278 c->opt_user_name = user_name;
279 c->opt_user_specified = true;
283 bool net_find_server(struct net_context *c,
286 struct sockaddr_storage *server_ss,
289 const char *d = domain ? domain : c->opt_target_workgroup;
292 *server_name = SMB_STRDUP(c->opt_host);
295 if (c->opt_have_ip) {
296 *server_ss = c->opt_dest_ip;
298 char addr[INET6_ADDRSTRLEN];
299 print_sockaddr(addr, sizeof(addr), &c->opt_dest_ip);
300 *server_name = SMB_STRDUP(addr);
302 } else if (*server_name) {
303 /* resolve the IP address */
304 if (!resolve_name(*server_name, server_ss, 0x20, false)) {
305 DEBUG(1,("Unable to resolve server name\n"));
308 } else if (flags & NET_FLAGS_PDC) {
310 struct sockaddr_storage pdc_ss;
312 if (!get_pdc_ip(d, &pdc_ss)) {
313 DEBUG(1,("Unable to resolve PDC server address\n"));
317 if (is_zero_addr(&pdc_ss)) {
321 if (!name_status_find(d, 0x1b, 0x20, &pdc_ss, dc_name)) {
325 *server_name = SMB_STRDUP(dc_name);
327 } else if (flags & NET_FLAGS_DMB) {
328 struct sockaddr_storage msbrow_ss;
329 char addr[INET6_ADDRSTRLEN];
331 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
332 if (!resolve_name(d, &msbrow_ss, 0x1B, false)) {
333 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
336 *server_ss = msbrow_ss;
337 print_sockaddr(addr, sizeof(addr), server_ss);
338 *server_name = SMB_STRDUP(addr);
339 } else if (flags & NET_FLAGS_MASTER) {
340 struct sockaddr_storage brow_ss;
341 char addr[INET6_ADDRSTRLEN];
342 if (!resolve_name(d, &brow_ss, 0x1D, false)) {
343 /* go looking for workgroups */
344 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
347 *server_ss = brow_ss;
348 print_sockaddr(addr, sizeof(addr), server_ss);
349 *server_name = SMB_STRDUP(addr);
350 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
351 if (!interpret_string_addr(server_ss,
352 "127.0.0.1", AI_NUMERICHOST)) {
353 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
356 *server_name = SMB_STRDUP("127.0.0.1");
360 DEBUG(1,("no server to connect to\n"));
367 bool net_find_pdc(struct sockaddr_storage *server_ss,
369 const char *domain_name)
371 if (!get_pdc_ip(domain_name, server_ss)) {
374 if (is_zero_addr(server_ss)) {
378 if (!name_status_find(domain_name, 0x1b, 0x20, server_ss, server_name)) {
385 NTSTATUS net_make_ipc_connection(struct net_context *c, unsigned flags,
386 struct cli_state **pcli)
388 return net_make_ipc_connection_ex(c, c->opt_workgroup, NULL, NULL, flags, pcli);
391 NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
393 const struct sockaddr_storage *pss,
394 unsigned flags, struct cli_state **pcli)
396 char *server_name = NULL;
397 struct sockaddr_storage server_ss;
398 struct cli_state *cli = NULL;
401 if ( !server || !pss ) {
402 if (!net_find_server(c, domain, flags, &server_ss,
404 d_fprintf(stderr, _("Unable to find a suitable server "
405 "for domain %s\n"), domain);
406 nt_status = NT_STATUS_UNSUCCESSFUL;
410 server_name = SMB_STRDUP( server );
414 if (flags & NET_FLAGS_ANONYMOUS) {
415 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
418 nt_status = connect_to_ipc(c, &cli, &server_ss,
422 /* store the server in the affinity cache if it was a PDC */
424 if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
425 saf_store(cli->server_domain, server_name);
427 SAFE_FREE(server_name);
428 if (!NT_STATUS_IS_OK(nt_status)) {
429 d_fprintf(stderr, _("Connection failed: %s\n"),
430 nt_errstr(nt_status));
432 } else if (c->opt_request_timeout) {
433 cli_set_timeout(cli, c->opt_request_timeout * 1000);
443 /****************************************************************************
444 ****************************************************************************/
446 const char *net_prompt_pass(struct net_context *c, const char *user)
452 if (c->opt_password) {
453 return c->opt_password;
456 if (c->opt_machine_pass) {
460 if (c->opt_kerberos && !c->opt_user_specified) {
468 if (asprintf(&prompt, _("Enter %s's password:"), user) == -1) {
472 rc = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
478 return SMB_STRDUP(pwd);
481 struct cli_credentials *net_context_creds(struct net_context *c,
484 struct cli_credentials *creds = NULL;
485 struct loadparm_context *lp_ctx = NULL;
487 c->opt_password = net_prompt_pass(c, c->opt_user_name);
489 creds = cli_credentials_init(mem_ctx);
491 d_printf("ERROR: Unable to allocate memory!\n");
495 lp_ctx = loadparm_init_s3(creds, loadparm_s3_helpers());
496 if (lp_ctx == NULL) {
497 d_printf("loadparm_init_s3 failed\n");
501 cli_credentials_guess(creds, lp_ctx);
503 if (c->opt_kerberos && c->opt_user_specified) {
504 cli_credentials_set_kerberos_state(creds,
505 CRED_AUTO_USE_KERBEROS);
506 } else if (c->opt_kerberos) {
507 cli_credentials_set_kerberos_state(creds,
508 CRED_MUST_USE_KERBEROS);
510 cli_credentials_set_kerberos_state(creds,
511 CRED_DONT_USE_KERBEROS);
517 features = cli_credentials_get_gensec_features(creds);
518 features |= GENSEC_FEATURE_NTLM_CCACHE;
519 cli_credentials_set_gensec_features(creds, features);
521 if (c->opt_password != NULL && strlen(c->opt_password) == 0) {
523 * some callers pass "" as no password
525 * GENSEC_FEATURE_NTLM_CCACHE only handles
526 * NULL as no password.
528 c->opt_password = NULL;
532 if (c->opt_user_specified) {
533 const char *default_domain =
534 cli_credentials_get_domain(creds);
535 char *username = NULL;
536 const char *domain = NULL;
541 tmp = talloc_strdup(creds, c->opt_user_name);
547 /* allow for workgroups as part of the username */
548 if ((p = strchr_m(tmp, '\\')) ||
549 (p = strchr_m(tmp, '/')) ||
550 (p = strchr_m(tmp, *lp_winbind_separator()))) {
556 if (domain == NULL) {
557 domain = c->opt_workgroup;
561 * Don't overwrite the value from cli_credentials_guess()
562 * with CRED_SPECIFIED, unless we have to.
564 is_default = strequal_m(domain, default_domain);
566 cli_credentials_set_domain(creds,
571 cli_credentials_set_username(creds,
574 cli_credentials_set_password(creds,
582 int net_run_function(struct net_context *c, int argc, const char **argv,
583 const char *whoami, struct functable *table)
588 for (i=0; table[i].funcname != NULL; i++) {
589 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
590 return table[i].fn(c, argc-1, argv+1);
594 if (c->display_usage == false) {
595 d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
596 (argc > 0)?argv[0]:"");
598 d_printf(_("Usage:\n"));
599 for (i=0; table[i].funcname != NULL; i++) {
600 if(c->display_usage == false)
601 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
602 _(table[i].description));
604 d_printf("%s\n", _(table[i].usage));
607 return c->display_usage?0:-1;
610 void net_display_usage_from_functable(struct functable *table)
613 for (i=0; table[i].funcname != NULL; i++) {
614 d_printf("%s\n", _(table[i].usage));
618 const char *net_share_type_str(int num_type)
621 case 0: return _("Disk");
622 case 1: return _("Print");
623 case 2: return _("Dev");
624 case 3: return _("IPC");
625 default: return _("Unknown");
629 static NTSTATUS net_scan_dc_noad(struct net_context *c,
630 struct cli_state *cli,
631 struct net_dc_info *dc_info)
633 TALLOC_CTX *mem_ctx = talloc_tos();
634 struct rpc_pipe_client *pipe_hnd = NULL;
635 struct dcerpc_binding_handle *b;
636 NTSTATUS status, result;
637 struct policy_handle pol;
638 union lsa_PolicyInformation *info;
640 ZERO_STRUCTP(dc_info);
643 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
645 if (!NT_STATUS_IS_OK(status)) {
649 b = pipe_hnd->binding_handle;
651 status = dcerpc_lsa_open_policy(b, mem_ctx,
653 SEC_FLAG_MAXIMUM_ALLOWED,
656 if (!NT_STATUS_IS_OK(status)) {
659 if (!NT_STATUS_IS_OK(result)) {
664 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
666 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
669 if (!NT_STATUS_IS_OK(status)) {
672 if (!NT_STATUS_IS_OK(result)) {
677 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info->account_domain.name.string);
678 if (dc_info->netbios_domain_name == NULL) {
679 status = NT_STATUS_NO_MEMORY;
684 if (is_valid_policy_hnd(&pol)) {
685 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
688 TALLOC_FREE(pipe_hnd);
693 NTSTATUS net_scan_dc(struct net_context *c,
694 struct cli_state *cli,
695 struct net_dc_info *dc_info)
697 TALLOC_CTX *mem_ctx = talloc_tos();
698 struct rpc_pipe_client *dssetup_pipe = NULL;
699 struct dcerpc_binding_handle *dssetup_handle = NULL;
700 union dssetup_DsRoleInfo info;
704 ZERO_STRUCTP(dc_info);
706 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_dssetup,
708 if (!NT_STATUS_IS_OK(status)) {
709 DEBUG(10,("net_scan_dc: failed to open dssetup pipe with %s, "
710 "retrying with lsa pipe\n", nt_errstr(status)));
711 return net_scan_dc_noad(c, cli, dc_info);
713 dssetup_handle = dssetup_pipe->binding_handle;
715 status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_handle, mem_ctx,
716 DS_ROLE_BASIC_INFORMATION,
719 TALLOC_FREE(dssetup_pipe);
721 if (NT_STATUS_IS_OK(status)) {
722 status = werror_to_ntstatus(werr);
724 if (!NT_STATUS_IS_OK(status)) {
728 dc_info->is_dc = (info.basic.role & (DS_ROLE_PRIMARY_DC|DS_ROLE_BACKUP_DC));
729 dc_info->is_pdc = (info.basic.role & DS_ROLE_PRIMARY_DC);
730 dc_info->is_ad = (info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING);
731 dc_info->is_mixed_mode = (info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE);
732 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info.basic.domain);
733 dc_info->dns_domain_name = talloc_strdup(mem_ctx, info.basic.dns_domain);
734 dc_info->forest_name = talloc_strdup(mem_ctx, info.basic.forest);