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 "rpc_client/cli_pipe.h"
25 #include "../librpc/gen_ndr/ndr_lsa_c.h"
26 #include "rpc_client/cli_lsarpc.h"
27 #include "../librpc/gen_ndr/ndr_dssetup_c.h"
29 #include "../libcli/security/security.h"
30 #include "libsmb/libsmb.h"
32 NTSTATUS
net_rpc_lookup_name(struct net_context
*c
,
33 TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
34 const char *name
, const char **ret_domain
,
35 const char **ret_name
, struct dom_sid
*ret_sid
,
36 enum lsa_SidType
*ret_type
)
38 struct rpc_pipe_client
*lsa_pipe
= NULL
;
39 struct policy_handle pol
;
40 NTSTATUS status
, result
;
41 const char **dom_names
;
43 enum lsa_SidType
*types
;
44 struct dcerpc_binding_handle
*b
;
48 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
50 if (!NT_STATUS_IS_OK(status
)) {
51 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
55 b
= lsa_pipe
->binding_handle
;
57 status
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
58 SEC_FLAG_MAXIMUM_ALLOWED
,
60 if (!NT_STATUS_IS_OK(status
)) {
61 d_fprintf(stderr
, "open_policy %s: %s\n", _("failed"),
66 status
= rpccli_lsa_lookup_names(lsa_pipe
, mem_ctx
, &pol
, 1,
67 &name
, &dom_names
, 1, &sids
, &types
);
69 if (!NT_STATUS_IS_OK(status
)) {
70 /* This can happen easily, don't log an error */
74 if (ret_domain
!= NULL
) {
75 *ret_domain
= dom_names
[0];
77 if (ret_name
!= NULL
) {
78 *ret_name
= talloc_strdup(mem_ctx
, name
);
80 if (ret_sid
!= NULL
) {
81 sid_copy(ret_sid
, &sids
[0]);
83 if (ret_type
!= NULL
) {
88 if (is_valid_policy_hnd(&pol
)) {
89 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
91 TALLOC_FREE(lsa_pipe
);
96 /****************************************************************************
97 Connect to \\server\service.
98 ****************************************************************************/
100 NTSTATUS
connect_to_service(struct net_context
*c
,
101 struct cli_state
**cli_ctx
,
102 struct sockaddr_storage
*server_ss
,
103 const char *server_name
,
104 const char *service_name
,
105 const char *service_type
)
110 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
112 if (c
->opt_kerberos
) {
113 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
116 if (c
->opt_kerberos
&& c
->opt_password
) {
117 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
121 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
124 nt_status
= cli_full_connection(cli_ctx
, NULL
, server_name
,
125 server_ss
, c
->opt_port
,
126 service_name
, service_type
,
127 c
->opt_user_name
, c
->opt_workgroup
,
128 c
->opt_password
, flags
, Undefined
);
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_force_encryption(*cli_ctx
,
157 if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_NOT_SUPPORTED
)) {
158 d_printf(_("Encryption required and "
159 "server that doesn't support "
160 "UNIX extensions - failing connect\n"));
161 } else if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_UNKNOWN_REVISION
)) {
162 d_printf(_("Encryption required and "
163 "can't get UNIX CIFS extensions "
164 "version from server.\n"));
165 } else if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_UNSUPPORTED_COMPRESSION
)) {
166 d_printf(_("Encryption required and "
167 "share %s doesn't support "
168 "encryption.\n"), service_name
);
169 } else if (!NT_STATUS_IS_OK(nt_status
)) {
170 d_printf(_("Encryption required and "
171 "setup failed with error %s.\n"),
172 nt_errstr(nt_status
));
175 if (!NT_STATUS_IS_OK(nt_status
)) {
176 cli_shutdown(*cli_ctx
);
184 /****************************************************************************
185 Connect to \\server\ipc$.
186 ****************************************************************************/
188 NTSTATUS
connect_to_ipc(struct net_context
*c
,
189 struct cli_state
**cli_ctx
,
190 struct sockaddr_storage
*server_ss
,
191 const char *server_name
)
193 return connect_to_service(c
, cli_ctx
, server_ss
, server_name
, "IPC$",
197 /****************************************************************************
198 Connect to \\server\ipc$ anonymously.
199 ****************************************************************************/
201 NTSTATUS
connect_to_ipc_anonymous(struct net_context
*c
,
202 struct cli_state
**cli_ctx
,
203 struct sockaddr_storage
*server_ss
,
204 const char *server_name
)
208 nt_status
= cli_full_connection(cli_ctx
, c
->opt_requester_name
,
209 server_name
, server_ss
, c
->opt_port
,
214 if (NT_STATUS_IS_OK(nt_status
)) {
217 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status
)));
222 /****************************************************************************
223 Return malloced user@realm for krb5 login.
224 ****************************************************************************/
226 static char *get_user_and_realm(const char *username
)
228 char *user_and_realm
= NULL
;
233 if (strchr_m(username
, '@')) {
234 user_and_realm
= SMB_STRDUP(username
);
236 if (asprintf(&user_and_realm
, "%s@%s", username
, lp_realm()) == -1) {
237 user_and_realm
= NULL
;
240 return user_and_realm
;
243 /****************************************************************************
244 Connect to \\server\ipc$ using KRB5.
245 ****************************************************************************/
247 NTSTATUS
connect_to_ipc_krb5(struct net_context
*c
,
248 struct cli_state
**cli_ctx
,
249 struct sockaddr_storage
*server_ss
,
250 const char *server_name
)
253 char *user_and_realm
= NULL
;
255 /* FIXME: Should get existing kerberos ticket if possible. */
256 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
257 if (!c
->opt_password
) {
258 return NT_STATUS_NO_MEMORY
;
261 user_and_realm
= get_user_and_realm(c
->opt_user_name
);
262 if (!user_and_realm
) {
263 return NT_STATUS_NO_MEMORY
;
266 nt_status
= cli_full_connection(cli_ctx
, NULL
, server_name
,
267 server_ss
, c
->opt_port
,
269 user_and_realm
, c
->opt_workgroup
,
271 CLI_FULL_CONNECTION_USE_KERBEROS
,
274 SAFE_FREE(user_and_realm
);
276 if (!NT_STATUS_IS_OK(nt_status
)) {
277 DEBUG(1,("Cannot connect to server using kerberos. Error was %s\n", nt_errstr(nt_status
)));
281 if (c
->smb_encrypt
) {
282 nt_status
= cli_cm_force_encryption(*cli_ctx
,
287 if (!NT_STATUS_IS_OK(nt_status
)) {
288 cli_shutdown(*cli_ctx
);
297 * Connect a server and open a given pipe
299 * @param cli_dst A cli_state
300 * @param pipe The pipe to open
301 * @param got_pipe boolean that stores if we got a pipe
303 * @return Normal NTSTATUS return.
305 NTSTATUS
connect_dst_pipe(struct net_context
*c
, struct cli_state
**cli_dst
,
306 struct rpc_pipe_client
**pp_pipe_hnd
,
307 const struct ndr_syntax_id
*interface
)
310 char *server_name
= SMB_STRDUP("127.0.0.1");
311 struct cli_state
*cli_tmp
= NULL
;
312 struct rpc_pipe_client
*pipe_hnd
= NULL
;
314 if (server_name
== NULL
) {
315 return NT_STATUS_NO_MEMORY
;
318 if (c
->opt_destination
) {
319 SAFE_FREE(server_name
);
320 if ((server_name
= SMB_STRDUP(c
->opt_destination
)) == NULL
) {
321 return NT_STATUS_NO_MEMORY
;
325 /* make a connection to a named pipe */
326 nt_status
= connect_to_ipc(c
, &cli_tmp
, NULL
, server_name
);
327 if (!NT_STATUS_IS_OK(nt_status
)) {
328 SAFE_FREE(server_name
);
332 nt_status
= cli_rpc_pipe_open_noauth(cli_tmp
, interface
,
334 if (!NT_STATUS_IS_OK(nt_status
)) {
335 DEBUG(0, ("couldn't not initialize pipe\n"));
336 cli_shutdown(cli_tmp
);
337 SAFE_FREE(server_name
);
342 *pp_pipe_hnd
= pipe_hnd
;
343 SAFE_FREE(server_name
);
348 /****************************************************************************
349 Use the local machine account (krb) and password for this session.
350 ****************************************************************************/
352 int net_use_krb_machine_account(struct net_context
*c
)
354 char *user_name
= NULL
;
356 if (!secrets_init()) {
357 d_fprintf(stderr
,_("ERROR: Unable to open secrets database\n"));
361 c
->opt_password
= secrets_fetch_machine_password(
362 c
->opt_target_workgroup
, NULL
, NULL
);
363 if (asprintf(&user_name
, "%s$@%s", global_myname(), lp_realm()) == -1) {
366 c
->opt_user_name
= user_name
;
370 /****************************************************************************
371 Use the machine account name and password for this session.
372 ****************************************************************************/
374 int net_use_machine_account(struct net_context
*c
)
376 char *user_name
= NULL
;
378 if (!secrets_init()) {
379 d_fprintf(stderr
,_("ERROR: Unable to open secrets database\n"));
383 c
->opt_password
= secrets_fetch_machine_password(
384 c
->opt_target_workgroup
, NULL
, NULL
);
385 if (asprintf(&user_name
, "%s$", global_myname()) == -1) {
388 c
->opt_user_name
= user_name
;
392 bool net_find_server(struct net_context
*c
,
395 struct sockaddr_storage
*server_ss
,
398 const char *d
= domain
? domain
: c
->opt_target_workgroup
;
401 *server_name
= SMB_STRDUP(c
->opt_host
);
404 if (c
->opt_have_ip
) {
405 *server_ss
= c
->opt_dest_ip
;
407 char addr
[INET6_ADDRSTRLEN
];
408 print_sockaddr(addr
, sizeof(addr
), &c
->opt_dest_ip
);
409 *server_name
= SMB_STRDUP(addr
);
411 } else if (*server_name
) {
412 /* resolve the IP address */
413 if (!resolve_name(*server_name
, server_ss
, 0x20, false)) {
414 DEBUG(1,("Unable to resolve server name\n"));
417 } else if (flags
& NET_FLAGS_PDC
) {
419 struct sockaddr_storage pdc_ss
;
421 if (!get_pdc_ip(d
, &pdc_ss
)) {
422 DEBUG(1,("Unable to resolve PDC server address\n"));
426 if (is_zero_addr(&pdc_ss
)) {
430 if (!name_status_find(d
, 0x1b, 0x20, &pdc_ss
, dc_name
)) {
434 *server_name
= SMB_STRDUP(dc_name
);
436 } else if (flags
& NET_FLAGS_DMB
) {
437 struct sockaddr_storage msbrow_ss
;
438 char addr
[INET6_ADDRSTRLEN
];
440 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
441 if (!resolve_name(d
, &msbrow_ss
, 0x1B, false)) {
442 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
445 *server_ss
= msbrow_ss
;
446 print_sockaddr(addr
, sizeof(addr
), server_ss
);
447 *server_name
= SMB_STRDUP(addr
);
448 } else if (flags
& NET_FLAGS_MASTER
) {
449 struct sockaddr_storage brow_ss
;
450 char addr
[INET6_ADDRSTRLEN
];
451 if (!resolve_name(d
, &brow_ss
, 0x1D, false)) {
452 /* go looking for workgroups */
453 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
456 *server_ss
= brow_ss
;
457 print_sockaddr(addr
, sizeof(addr
), server_ss
);
458 *server_name
= SMB_STRDUP(addr
);
459 } else if (!(flags
& NET_FLAGS_LOCALHOST_DEFAULT_INSANE
)) {
460 if (!interpret_string_addr(server_ss
,
461 "127.0.0.1", AI_NUMERICHOST
)) {
462 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
465 *server_name
= SMB_STRDUP("127.0.0.1");
469 DEBUG(1,("no server to connect to\n"));
476 bool net_find_pdc(struct sockaddr_storage
*server_ss
,
478 const char *domain_name
)
480 if (!get_pdc_ip(domain_name
, server_ss
)) {
483 if (is_zero_addr(server_ss
)) {
487 if (!name_status_find(domain_name
, 0x1b, 0x20, server_ss
, server_name
)) {
494 NTSTATUS
net_make_ipc_connection(struct net_context
*c
, unsigned flags
,
495 struct cli_state
**pcli
)
497 return net_make_ipc_connection_ex(c
, c
->opt_workgroup
, NULL
, NULL
, flags
, pcli
);
500 NTSTATUS
net_make_ipc_connection_ex(struct net_context
*c
,const char *domain
,
502 struct sockaddr_storage
*pss
,
503 unsigned flags
, struct cli_state
**pcli
)
505 char *server_name
= NULL
;
506 struct sockaddr_storage server_ss
;
507 struct cli_state
*cli
= NULL
;
510 if ( !server
|| !pss
) {
511 if (!net_find_server(c
, domain
, flags
, &server_ss
,
513 d_fprintf(stderr
, _("Unable to find a suitable server "
514 "for domain %s\n"), domain
);
515 nt_status
= NT_STATUS_UNSUCCESSFUL
;
519 server_name
= SMB_STRDUP( server
);
523 if (flags
& NET_FLAGS_ANONYMOUS
) {
524 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
527 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
,
531 /* store the server in the affinity cache if it was a PDC */
533 if ( (flags
& NET_FLAGS_PDC
) && NT_STATUS_IS_OK(nt_status
) )
534 saf_store( cli
->server_domain
, cli
->desthost
);
536 SAFE_FREE(server_name
);
537 if (!NT_STATUS_IS_OK(nt_status
)) {
538 d_fprintf(stderr
, _("Connection failed: %s\n"),
539 nt_errstr(nt_status
));
541 } else if (c
->opt_request_timeout
) {
542 cli_set_timeout(cli
, c
->opt_request_timeout
* 1000);
552 /****************************************************************************
553 ****************************************************************************/
555 const char *net_prompt_pass(struct net_context
*c
, const char *user
)
558 const char *pass
= NULL
;
560 if (c
->opt_password
) {
561 return c
->opt_password
;
564 if (c
->opt_machine_pass
) {
568 if (c
->opt_kerberos
&& !c
->opt_user_specified
) {
572 if (asprintf(&prompt
, _("Enter %s's password:"), user
) == -1) {
576 pass
= getpass(prompt
);
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(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
.syntax_id
,
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
.syntax_id
,
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
);