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 "../librpc/gen_ndr/ndr_lsa_c.h"
25 #include "rpc_client/cli_lsarpc.h"
26 #include "../librpc/gen_ndr/ndr_dssetup_c.h"
28 #include "../libcli/security/security.h"
30 NTSTATUS
net_rpc_lookup_name(struct net_context
*c
,
31 TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
32 const char *name
, const char **ret_domain
,
33 const char **ret_name
, struct dom_sid
*ret_sid
,
34 enum lsa_SidType
*ret_type
)
36 struct rpc_pipe_client
*lsa_pipe
= NULL
;
37 struct policy_handle pol
;
38 NTSTATUS status
, result
;
39 const char **dom_names
;
41 enum lsa_SidType
*types
;
42 struct dcerpc_binding_handle
*b
;
46 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
48 if (!NT_STATUS_IS_OK(status
)) {
49 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
53 b
= lsa_pipe
->binding_handle
;
55 status
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
56 SEC_FLAG_MAXIMUM_ALLOWED
,
58 if (!NT_STATUS_IS_OK(status
)) {
59 d_fprintf(stderr
, "open_policy %s: %s\n", _("failed"),
64 status
= rpccli_lsa_lookup_names(lsa_pipe
, mem_ctx
, &pol
, 1,
65 &name
, &dom_names
, 1, &sids
, &types
);
67 if (!NT_STATUS_IS_OK(status
)) {
68 /* This can happen easily, don't log an error */
72 if (ret_domain
!= NULL
) {
73 *ret_domain
= dom_names
[0];
75 if (ret_name
!= NULL
) {
76 *ret_name
= talloc_strdup(mem_ctx
, name
);
78 if (ret_sid
!= NULL
) {
79 sid_copy(ret_sid
, &sids
[0]);
81 if (ret_type
!= NULL
) {
86 if (is_valid_policy_hnd(&pol
)) {
87 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
89 TALLOC_FREE(lsa_pipe
);
94 /****************************************************************************
95 Connect to \\server\service.
96 ****************************************************************************/
98 NTSTATUS
connect_to_service(struct net_context
*c
,
99 struct cli_state
**cli_ctx
,
100 struct sockaddr_storage
*server_ss
,
101 const char *server_name
,
102 const char *service_name
,
103 const char *service_type
)
108 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
110 if (c
->opt_kerberos
) {
111 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
114 if (c
->opt_kerberos
&& c
->opt_password
) {
115 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
119 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
122 nt_status
= cli_full_connection(cli_ctx
, NULL
, server_name
,
123 server_ss
, c
->opt_port
,
124 service_name
, service_type
,
125 c
->opt_user_name
, c
->opt_workgroup
,
126 c
->opt_password
, flags
, Undefined
);
127 if (!NT_STATUS_IS_OK(nt_status
)) {
128 d_fprintf(stderr
, _("Could not connect to server %s\n"),
131 /* Display a nicer message depending on the result */
133 if (NT_STATUS_V(nt_status
) ==
134 NT_STATUS_V(NT_STATUS_LOGON_FAILURE
))
136 _("The username or password was not "
139 if (NT_STATUS_V(nt_status
) ==
140 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT
))
141 d_fprintf(stderr
, _("The account was locked out.\n"));
143 if (NT_STATUS_V(nt_status
) ==
144 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED
))
145 d_fprintf(stderr
, _("The account was disabled.\n"));
149 if (c
->smb_encrypt
) {
150 nt_status
= cli_force_encryption(*cli_ctx
,
155 if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_NOT_SUPPORTED
)) {
156 d_printf(_("Encryption required and "
157 "server that doesn't support "
158 "UNIX extensions - failing connect\n"));
159 } else if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_UNKNOWN_REVISION
)) {
160 d_printf(_("Encryption required and "
161 "can't get UNIX CIFS extensions "
162 "version from server.\n"));
163 } else if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_UNSUPPORTED_COMPRESSION
)) {
164 d_printf(_("Encryption required and "
165 "share %s doesn't support "
166 "encryption.\n"), service_name
);
167 } else if (!NT_STATUS_IS_OK(nt_status
)) {
168 d_printf(_("Encryption required and "
169 "setup failed with error %s.\n"),
170 nt_errstr(nt_status
));
173 if (!NT_STATUS_IS_OK(nt_status
)) {
174 cli_shutdown(*cli_ctx
);
182 /****************************************************************************
183 Connect to \\server\ipc$.
184 ****************************************************************************/
186 NTSTATUS
connect_to_ipc(struct net_context
*c
,
187 struct cli_state
**cli_ctx
,
188 struct sockaddr_storage
*server_ss
,
189 const char *server_name
)
191 return connect_to_service(c
, cli_ctx
, server_ss
, server_name
, "IPC$",
195 /****************************************************************************
196 Connect to \\server\ipc$ anonymously.
197 ****************************************************************************/
199 NTSTATUS
connect_to_ipc_anonymous(struct net_context
*c
,
200 struct cli_state
**cli_ctx
,
201 struct sockaddr_storage
*server_ss
,
202 const char *server_name
)
206 nt_status
= cli_full_connection(cli_ctx
, c
->opt_requester_name
,
207 server_name
, server_ss
, c
->opt_port
,
212 if (NT_STATUS_IS_OK(nt_status
)) {
215 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status
)));
220 /****************************************************************************
221 Return malloced user@realm for krb5 login.
222 ****************************************************************************/
224 static char *get_user_and_realm(const char *username
)
226 char *user_and_realm
= NULL
;
231 if (strchr_m(username
, '@')) {
232 user_and_realm
= SMB_STRDUP(username
);
234 if (asprintf(&user_and_realm
, "%s@%s", username
, lp_realm()) == -1) {
235 user_and_realm
= NULL
;
238 return user_and_realm
;
241 /****************************************************************************
242 Connect to \\server\ipc$ using KRB5.
243 ****************************************************************************/
245 NTSTATUS
connect_to_ipc_krb5(struct net_context
*c
,
246 struct cli_state
**cli_ctx
,
247 struct sockaddr_storage
*server_ss
,
248 const char *server_name
)
251 char *user_and_realm
= NULL
;
253 /* FIXME: Should get existing kerberos ticket if possible. */
254 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
255 if (!c
->opt_password
) {
256 return NT_STATUS_NO_MEMORY
;
259 user_and_realm
= get_user_and_realm(c
->opt_user_name
);
260 if (!user_and_realm
) {
261 return NT_STATUS_NO_MEMORY
;
264 nt_status
= cli_full_connection(cli_ctx
, NULL
, server_name
,
265 server_ss
, c
->opt_port
,
267 user_and_realm
, c
->opt_workgroup
,
269 CLI_FULL_CONNECTION_USE_KERBEROS
,
272 SAFE_FREE(user_and_realm
);
274 if (!NT_STATUS_IS_OK(nt_status
)) {
275 DEBUG(1,("Cannot connect to server using kerberos. Error was %s\n", nt_errstr(nt_status
)));
279 if (c
->smb_encrypt
) {
280 nt_status
= cli_cm_force_encryption(*cli_ctx
,
285 if (!NT_STATUS_IS_OK(nt_status
)) {
286 cli_shutdown(*cli_ctx
);
295 * Connect a server and open a given pipe
297 * @param cli_dst A cli_state
298 * @param pipe The pipe to open
299 * @param got_pipe boolean that stores if we got a pipe
301 * @return Normal NTSTATUS return.
303 NTSTATUS
connect_dst_pipe(struct net_context
*c
, struct cli_state
**cli_dst
,
304 struct rpc_pipe_client
**pp_pipe_hnd
,
305 const struct ndr_syntax_id
*interface
)
308 char *server_name
= SMB_STRDUP("127.0.0.1");
309 struct cli_state
*cli_tmp
= NULL
;
310 struct rpc_pipe_client
*pipe_hnd
= NULL
;
312 if (server_name
== NULL
) {
313 return NT_STATUS_NO_MEMORY
;
316 if (c
->opt_destination
) {
317 SAFE_FREE(server_name
);
318 if ((server_name
= SMB_STRDUP(c
->opt_destination
)) == NULL
) {
319 return NT_STATUS_NO_MEMORY
;
323 /* make a connection to a named pipe */
324 nt_status
= connect_to_ipc(c
, &cli_tmp
, NULL
, server_name
);
325 if (!NT_STATUS_IS_OK(nt_status
)) {
326 SAFE_FREE(server_name
);
330 nt_status
= cli_rpc_pipe_open_noauth(cli_tmp
, interface
,
332 if (!NT_STATUS_IS_OK(nt_status
)) {
333 DEBUG(0, ("couldn't not initialize pipe\n"));
334 cli_shutdown(cli_tmp
);
335 SAFE_FREE(server_name
);
340 *pp_pipe_hnd
= pipe_hnd
;
341 SAFE_FREE(server_name
);
346 /****************************************************************************
347 Use the local machine account (krb) and password for this session.
348 ****************************************************************************/
350 int net_use_krb_machine_account(struct net_context
*c
)
352 char *user_name
= NULL
;
354 if (!secrets_init()) {
355 d_fprintf(stderr
,_("ERROR: Unable to open secrets database\n"));
359 c
->opt_password
= secrets_fetch_machine_password(
360 c
->opt_target_workgroup
, NULL
, NULL
);
361 if (asprintf(&user_name
, "%s$@%s", global_myname(), lp_realm()) == -1) {
364 c
->opt_user_name
= user_name
;
368 /****************************************************************************
369 Use the machine account name and password for this session.
370 ****************************************************************************/
372 int net_use_machine_account(struct net_context
*c
)
374 char *user_name
= NULL
;
376 if (!secrets_init()) {
377 d_fprintf(stderr
,_("ERROR: Unable to open secrets database\n"));
381 c
->opt_password
= secrets_fetch_machine_password(
382 c
->opt_target_workgroup
, NULL
, NULL
);
383 if (asprintf(&user_name
, "%s$", global_myname()) == -1) {
386 c
->opt_user_name
= user_name
;
390 bool net_find_server(struct net_context
*c
,
393 struct sockaddr_storage
*server_ss
,
396 const char *d
= domain
? domain
: c
->opt_target_workgroup
;
399 *server_name
= SMB_STRDUP(c
->opt_host
);
402 if (c
->opt_have_ip
) {
403 *server_ss
= c
->opt_dest_ip
;
405 char addr
[INET6_ADDRSTRLEN
];
406 print_sockaddr(addr
, sizeof(addr
), &c
->opt_dest_ip
);
407 *server_name
= SMB_STRDUP(addr
);
409 } else if (*server_name
) {
410 /* resolve the IP address */
411 if (!resolve_name(*server_name
, server_ss
, 0x20, false)) {
412 DEBUG(1,("Unable to resolve server name\n"));
415 } else if (flags
& NET_FLAGS_PDC
) {
417 struct sockaddr_storage pdc_ss
;
419 if (!get_pdc_ip(d
, &pdc_ss
)) {
420 DEBUG(1,("Unable to resolve PDC server address\n"));
424 if (is_zero_addr(&pdc_ss
)) {
428 if (!name_status_find(d
, 0x1b, 0x20, &pdc_ss
, dc_name
)) {
432 *server_name
= SMB_STRDUP(dc_name
);
434 } else if (flags
& NET_FLAGS_DMB
) {
435 struct sockaddr_storage msbrow_ss
;
436 char addr
[INET6_ADDRSTRLEN
];
438 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
439 if (!resolve_name(d
, &msbrow_ss
, 0x1B, false)) {
440 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
443 *server_ss
= msbrow_ss
;
444 print_sockaddr(addr
, sizeof(addr
), server_ss
);
445 *server_name
= SMB_STRDUP(addr
);
446 } else if (flags
& NET_FLAGS_MASTER
) {
447 struct sockaddr_storage brow_ss
;
448 char addr
[INET6_ADDRSTRLEN
];
449 if (!resolve_name(d
, &brow_ss
, 0x1D, false)) {
450 /* go looking for workgroups */
451 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
454 *server_ss
= brow_ss
;
455 print_sockaddr(addr
, sizeof(addr
), server_ss
);
456 *server_name
= SMB_STRDUP(addr
);
457 } else if (!(flags
& NET_FLAGS_LOCALHOST_DEFAULT_INSANE
)) {
458 if (!interpret_string_addr(server_ss
,
459 "127.0.0.1", AI_NUMERICHOST
)) {
460 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
463 *server_name
= SMB_STRDUP("127.0.0.1");
467 DEBUG(1,("no server to connect to\n"));
474 bool net_find_pdc(struct sockaddr_storage
*server_ss
,
476 const char *domain_name
)
478 if (!get_pdc_ip(domain_name
, server_ss
)) {
481 if (is_zero_addr(server_ss
)) {
485 if (!name_status_find(domain_name
, 0x1b, 0x20, server_ss
, server_name
)) {
492 NTSTATUS
net_make_ipc_connection(struct net_context
*c
, unsigned flags
,
493 struct cli_state
**pcli
)
495 return net_make_ipc_connection_ex(c
, c
->opt_workgroup
, NULL
, NULL
, flags
, pcli
);
498 NTSTATUS
net_make_ipc_connection_ex(struct net_context
*c
,const char *domain
,
500 struct sockaddr_storage
*pss
,
501 unsigned flags
, struct cli_state
**pcli
)
503 char *server_name
= NULL
;
504 struct sockaddr_storage server_ss
;
505 struct cli_state
*cli
= NULL
;
508 if ( !server
|| !pss
) {
509 if (!net_find_server(c
, domain
, flags
, &server_ss
,
511 d_fprintf(stderr
, _("Unable to find a suitable server "
512 "for domain %s\n"), domain
);
513 nt_status
= NT_STATUS_UNSUCCESSFUL
;
517 server_name
= SMB_STRDUP( server
);
521 if (flags
& NET_FLAGS_ANONYMOUS
) {
522 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
525 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
,
529 /* store the server in the affinity cache if it was a PDC */
531 if ( (flags
& NET_FLAGS_PDC
) && NT_STATUS_IS_OK(nt_status
) )
532 saf_store( cli
->server_domain
, cli
->desthost
);
534 SAFE_FREE(server_name
);
535 if (!NT_STATUS_IS_OK(nt_status
)) {
536 d_fprintf(stderr
, _("Connection failed: %s\n"),
537 nt_errstr(nt_status
));
539 } else if (c
->opt_request_timeout
) {
540 cli_set_timeout(cli
, c
->opt_request_timeout
* 1000);
550 /****************************************************************************
551 ****************************************************************************/
553 const char *net_prompt_pass(struct net_context
*c
, const char *user
)
556 const char *pass
= NULL
;
558 if (c
->opt_password
) {
559 return c
->opt_password
;
562 if (c
->opt_machine_pass
) {
566 if (c
->opt_kerberos
&& !c
->opt_user_specified
) {
570 if (asprintf(&prompt
, _("Enter %s's password:"), user
) == -1) {
574 pass
= getpass(prompt
);
580 int net_run_function(struct net_context
*c
, int argc
, const char **argv
,
581 const char *whoami
, struct functable
*table
)
586 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
587 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
588 return table
[i
].fn(c
, argc
-1, argv
+1);
592 if (c
->display_usage
== false) {
593 d_fprintf(stderr
, _("Invalid command: %s %s\n"), whoami
,
594 (argc
> 0)?argv
[0]:"");
596 d_printf(_("Usage:\n"));
597 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
598 if(c
->display_usage
== false)
599 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
600 _(table
[i
].description
));
602 d_printf("%s\n", _(table
[i
].usage
));
605 return c
->display_usage
?0:-1;
608 void net_display_usage_from_functable(struct functable
*table
)
611 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
612 d_printf("%s\n", _(table
[i
].usage
));
616 const char *net_share_type_str(int num_type
)
619 case 0: return _("Disk");
620 case 1: return _("Print");
621 case 2: return _("Dev");
622 case 3: return _("IPC");
623 default: return _("Unknown");
627 static NTSTATUS
net_scan_dc_noad(struct net_context
*c
,
628 struct cli_state
*cli
,
629 struct net_dc_info
*dc_info
)
631 TALLOC_CTX
*mem_ctx
= talloc_tos();
632 struct rpc_pipe_client
*pipe_hnd
= NULL
;
633 struct dcerpc_binding_handle
*b
;
634 NTSTATUS status
, result
;
635 struct policy_handle pol
;
636 union lsa_PolicyInformation
*info
;
638 ZERO_STRUCTP(dc_info
);
641 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
643 if (!NT_STATUS_IS_OK(status
)) {
647 b
= pipe_hnd
->binding_handle
;
649 status
= dcerpc_lsa_open_policy(b
, mem_ctx
,
651 SEC_FLAG_MAXIMUM_ALLOWED
,
654 if (!NT_STATUS_IS_OK(status
)) {
657 if (!NT_STATUS_IS_OK(result
)) {
662 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
664 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
667 if (!NT_STATUS_IS_OK(status
)) {
670 if (!NT_STATUS_IS_OK(result
)) {
675 dc_info
->netbios_domain_name
= talloc_strdup(mem_ctx
, info
->account_domain
.name
.string
);
676 if (dc_info
->netbios_domain_name
== NULL
) {
677 status
= NT_STATUS_NO_MEMORY
;
682 if (is_valid_policy_hnd(&pol
)) {
683 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
686 TALLOC_FREE(pipe_hnd
);
691 NTSTATUS
net_scan_dc(struct net_context
*c
,
692 struct cli_state
*cli
,
693 struct net_dc_info
*dc_info
)
695 TALLOC_CTX
*mem_ctx
= talloc_tos();
696 struct rpc_pipe_client
*dssetup_pipe
= NULL
;
697 struct dcerpc_binding_handle
*dssetup_handle
= NULL
;
698 union dssetup_DsRoleInfo info
;
702 ZERO_STRUCTP(dc_info
);
704 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_dssetup
.syntax_id
,
706 if (!NT_STATUS_IS_OK(status
)) {
707 DEBUG(10,("net_scan_dc: failed to open dssetup pipe with %s, "
708 "retrying with lsa pipe\n", nt_errstr(status
)));
709 return net_scan_dc_noad(c
, cli
, dc_info
);
711 dssetup_handle
= dssetup_pipe
->binding_handle
;
713 status
= dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_handle
, mem_ctx
,
714 DS_ROLE_BASIC_INFORMATION
,
717 TALLOC_FREE(dssetup_pipe
);
719 if (NT_STATUS_IS_OK(status
)) {
720 status
= werror_to_ntstatus(werr
);
722 if (!NT_STATUS_IS_OK(status
)) {
726 dc_info
->is_dc
= (info
.basic
.role
& (DS_ROLE_PRIMARY_DC
|DS_ROLE_BACKUP_DC
));
727 dc_info
->is_pdc
= (info
.basic
.role
& DS_ROLE_PRIMARY_DC
);
728 dc_info
->is_ad
= (info
.basic
.flags
& DS_ROLE_PRIMARY_DS_RUNNING
);
729 dc_info
->is_mixed_mode
= (info
.basic
.flags
& DS_ROLE_PRIMARY_DS_MIXED_MODE
);
730 dc_info
->netbios_domain_name
= talloc_strdup(mem_ctx
, info
.basic
.domain
);
731 dc_info
->dns_domain_name
= talloc_strdup(mem_ctx
, info
.basic
.dns_domain
);
732 dc_info
->forest_name
= talloc_strdup(mem_ctx
, info
.basic
.forest
);