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/cli_lsa.h"
25 #include "../librpc/gen_ndr/cli_dssetup.h"
27 NTSTATUS
net_rpc_lookup_name(struct net_context
*c
,
28 TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
29 const char *name
, const char **ret_domain
,
30 const char **ret_name
, DOM_SID
*ret_sid
,
31 enum lsa_SidType
*ret_type
)
33 struct rpc_pipe_client
*lsa_pipe
= NULL
;
34 struct policy_handle pol
;
35 NTSTATUS result
= NT_STATUS_OK
;
36 const char **dom_names
;
38 enum lsa_SidType
*types
;
42 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
44 if (!NT_STATUS_IS_OK(result
)) {
45 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
49 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
50 SEC_FLAG_MAXIMUM_ALLOWED
,
52 if (!NT_STATUS_IS_OK(result
)) {
53 d_fprintf(stderr
, _("open_policy failed: %s\n"),
58 result
= rpccli_lsa_lookup_names(lsa_pipe
, mem_ctx
, &pol
, 1,
59 &name
, &dom_names
, 1, &sids
, &types
);
61 if (!NT_STATUS_IS_OK(result
)) {
62 /* This can happen easily, don't log an error */
66 if (ret_domain
!= NULL
) {
67 *ret_domain
= dom_names
[0];
69 if (ret_name
!= NULL
) {
70 *ret_name
= talloc_strdup(mem_ctx
, name
);
72 if (ret_sid
!= NULL
) {
73 sid_copy(ret_sid
, &sids
[0]);
75 if (ret_type
!= NULL
) {
80 if (is_valid_policy_hnd(&pol
)) {
81 rpccli_lsa_Close(lsa_pipe
, mem_ctx
, &pol
);
83 TALLOC_FREE(lsa_pipe
);
88 /****************************************************************************
89 Connect to \\server\service.
90 ****************************************************************************/
92 NTSTATUS
connect_to_service(struct net_context
*c
,
93 struct cli_state
**cli_ctx
,
94 struct sockaddr_storage
*server_ss
,
95 const char *server_name
,
96 const char *service_name
,
97 const char *service_type
)
102 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
104 if (c
->opt_kerberos
) {
105 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
108 if (c
->opt_kerberos
&& c
->opt_password
) {
109 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
112 nt_status
= cli_full_connection(cli_ctx
, NULL
, server_name
,
113 server_ss
, c
->opt_port
,
114 service_name
, service_type
,
115 c
->opt_user_name
, c
->opt_workgroup
,
116 c
->opt_password
, flags
, Undefined
, NULL
);
117 if (!NT_STATUS_IS_OK(nt_status
)) {
118 d_fprintf(stderr
, _("Could not connect to server %s\n"),
121 /* Display a nicer message depending on the result */
123 if (NT_STATUS_V(nt_status
) ==
124 NT_STATUS_V(NT_STATUS_LOGON_FAILURE
))
126 _("The username or password was not "
129 if (NT_STATUS_V(nt_status
) ==
130 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT
))
131 d_fprintf(stderr
, _("The account was locked out.\n"));
133 if (NT_STATUS_V(nt_status
) ==
134 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED
))
135 d_fprintf(stderr
, _("The account was disabled.\n"));
139 if (c
->smb_encrypt
) {
140 nt_status
= cli_force_encryption(*cli_ctx
,
145 if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_NOT_SUPPORTED
)) {
146 d_printf(_("Encryption required and "
147 "server that doesn't support "
148 "UNIX extensions - failing connect\n"));
149 } else if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_UNKNOWN_REVISION
)) {
150 d_printf(_("Encryption required and "
151 "can't get UNIX CIFS extensions "
152 "version from server.\n"));
153 } else if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_UNSUPPORTED_COMPRESSION
)) {
154 d_printf(_("Encryption required and "
155 "share %s doesn't support "
156 "encryption.\n"), service_name
);
157 } else if (!NT_STATUS_IS_OK(nt_status
)) {
158 d_printf(_("Encryption required and "
159 "setup failed with error %s.\n"),
160 nt_errstr(nt_status
));
163 if (!NT_STATUS_IS_OK(nt_status
)) {
164 cli_shutdown(*cli_ctx
);
172 /****************************************************************************
173 Connect to \\server\ipc$.
174 ****************************************************************************/
176 NTSTATUS
connect_to_ipc(struct net_context
*c
,
177 struct cli_state
**cli_ctx
,
178 struct sockaddr_storage
*server_ss
,
179 const char *server_name
)
181 return connect_to_service(c
, cli_ctx
, server_ss
, server_name
, "IPC$",
185 /****************************************************************************
186 Connect to \\server\ipc$ anonymously.
187 ****************************************************************************/
189 NTSTATUS
connect_to_ipc_anonymous(struct net_context
*c
,
190 struct cli_state
**cli_ctx
,
191 struct sockaddr_storage
*server_ss
,
192 const char *server_name
)
196 nt_status
= cli_full_connection(cli_ctx
, c
->opt_requester_name
,
197 server_name
, server_ss
, c
->opt_port
,
200 "", 0, Undefined
, NULL
);
202 if (NT_STATUS_IS_OK(nt_status
)) {
205 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status
)));
210 /****************************************************************************
211 Return malloced user@realm for krb5 login.
212 ****************************************************************************/
214 static char *get_user_and_realm(const char *username
)
216 char *user_and_realm
= NULL
;
221 if (strchr_m(username
, '@')) {
222 user_and_realm
= SMB_STRDUP(username
);
224 if (asprintf(&user_and_realm
, "%s@%s", username
, lp_realm()) == -1) {
225 user_and_realm
= NULL
;
228 return user_and_realm
;
231 /****************************************************************************
232 Connect to \\server\ipc$ using KRB5.
233 ****************************************************************************/
235 NTSTATUS
connect_to_ipc_krb5(struct net_context
*c
,
236 struct cli_state
**cli_ctx
,
237 struct sockaddr_storage
*server_ss
,
238 const char *server_name
)
241 char *user_and_realm
= NULL
;
243 /* FIXME: Should get existing kerberos ticket if possible. */
244 c
->opt_password
= net_prompt_pass(c
, c
->opt_user_name
);
245 if (!c
->opt_password
) {
246 return NT_STATUS_NO_MEMORY
;
249 user_and_realm
= get_user_and_realm(c
->opt_user_name
);
250 if (!user_and_realm
) {
251 return NT_STATUS_NO_MEMORY
;
254 nt_status
= cli_full_connection(cli_ctx
, NULL
, server_name
,
255 server_ss
, c
->opt_port
,
257 user_and_realm
, c
->opt_workgroup
,
259 CLI_FULL_CONNECTION_USE_KERBEROS
,
262 SAFE_FREE(user_and_realm
);
264 if (!NT_STATUS_IS_OK(nt_status
)) {
265 DEBUG(1,("Cannot connect to server using kerberos. Error was %s\n", nt_errstr(nt_status
)));
269 if (c
->smb_encrypt
) {
270 nt_status
= cli_cm_force_encryption(*cli_ctx
,
275 if (!NT_STATUS_IS_OK(nt_status
)) {
276 cli_shutdown(*cli_ctx
);
285 * Connect a server and open a given pipe
287 * @param cli_dst A cli_state
288 * @param pipe The pipe to open
289 * @param got_pipe boolean that stores if we got a pipe
291 * @return Normal NTSTATUS return.
293 NTSTATUS
connect_dst_pipe(struct net_context
*c
, struct cli_state
**cli_dst
,
294 struct rpc_pipe_client
**pp_pipe_hnd
,
295 const struct ndr_syntax_id
*interface
)
298 char *server_name
= SMB_STRDUP("127.0.0.1");
299 struct cli_state
*cli_tmp
= NULL
;
300 struct rpc_pipe_client
*pipe_hnd
= NULL
;
302 if (server_name
== NULL
) {
303 return NT_STATUS_NO_MEMORY
;
306 if (c
->opt_destination
) {
307 SAFE_FREE(server_name
);
308 if ((server_name
= SMB_STRDUP(c
->opt_destination
)) == NULL
) {
309 return NT_STATUS_NO_MEMORY
;
313 /* make a connection to a named pipe */
314 nt_status
= connect_to_ipc(c
, &cli_tmp
, NULL
, server_name
);
315 if (!NT_STATUS_IS_OK(nt_status
)) {
316 SAFE_FREE(server_name
);
320 nt_status
= cli_rpc_pipe_open_noauth(cli_tmp
, interface
,
322 if (!NT_STATUS_IS_OK(nt_status
)) {
323 DEBUG(0, ("couldn't not initialize pipe\n"));
324 cli_shutdown(cli_tmp
);
325 SAFE_FREE(server_name
);
330 *pp_pipe_hnd
= pipe_hnd
;
331 SAFE_FREE(server_name
);
336 /****************************************************************************
337 Use the local machine account (krb) and password for this session.
338 ****************************************************************************/
340 int net_use_krb_machine_account(struct net_context
*c
)
342 char *user_name
= NULL
;
344 if (!secrets_init()) {
345 d_fprintf(stderr
,_("ERROR: Unable to open secrets database\n"));
349 c
->opt_password
= secrets_fetch_machine_password(
350 c
->opt_target_workgroup
, NULL
, NULL
);
351 if (asprintf(&user_name
, "%s$@%s", global_myname(), lp_realm()) == -1) {
354 c
->opt_user_name
= user_name
;
358 /****************************************************************************
359 Use the machine account name and password for this session.
360 ****************************************************************************/
362 int net_use_machine_account(struct net_context
*c
)
364 char *user_name
= NULL
;
366 if (!secrets_init()) {
367 d_fprintf(stderr
,_("ERROR: Unable to open secrets database\n"));
371 c
->opt_password
= secrets_fetch_machine_password(
372 c
->opt_target_workgroup
, NULL
, NULL
);
373 if (asprintf(&user_name
, "%s$", global_myname()) == -1) {
376 c
->opt_user_name
= user_name
;
380 bool net_find_server(struct net_context
*c
,
383 struct sockaddr_storage
*server_ss
,
386 const char *d
= domain
? domain
: c
->opt_target_workgroup
;
389 *server_name
= SMB_STRDUP(c
->opt_host
);
392 if (c
->opt_have_ip
) {
393 *server_ss
= c
->opt_dest_ip
;
395 char addr
[INET6_ADDRSTRLEN
];
396 print_sockaddr(addr
, sizeof(addr
), &c
->opt_dest_ip
);
397 *server_name
= SMB_STRDUP(addr
);
399 } else if (*server_name
) {
400 /* resolve the IP address */
401 if (!resolve_name(*server_name
, server_ss
, 0x20, false)) {
402 DEBUG(1,("Unable to resolve server name\n"));
405 } else if (flags
& NET_FLAGS_PDC
) {
407 struct sockaddr_storage pdc_ss
;
409 if (!get_pdc_ip(d
, &pdc_ss
)) {
410 DEBUG(1,("Unable to resolve PDC server address\n"));
414 if (is_zero_addr((struct sockaddr
*)&pdc_ss
)) {
418 if (!name_status_find(d
, 0x1b, 0x20, &pdc_ss
, dc_name
)) {
422 *server_name
= SMB_STRDUP(dc_name
);
424 } else if (flags
& NET_FLAGS_DMB
) {
425 struct sockaddr_storage msbrow_ss
;
426 char addr
[INET6_ADDRSTRLEN
];
428 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
429 if (!resolve_name(d
, &msbrow_ss
, 0x1B, false)) {
430 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
433 *server_ss
= msbrow_ss
;
434 print_sockaddr(addr
, sizeof(addr
), server_ss
);
435 *server_name
= SMB_STRDUP(addr
);
436 } else if (flags
& NET_FLAGS_MASTER
) {
437 struct sockaddr_storage brow_ss
;
438 char addr
[INET6_ADDRSTRLEN
];
439 if (!resolve_name(d
, &brow_ss
, 0x1D, false)) {
440 /* go looking for workgroups */
441 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
444 *server_ss
= brow_ss
;
445 print_sockaddr(addr
, sizeof(addr
), server_ss
);
446 *server_name
= SMB_STRDUP(addr
);
447 } else if (!(flags
& NET_FLAGS_LOCALHOST_DEFAULT_INSANE
)) {
448 if (!interpret_string_addr(server_ss
,
449 "127.0.0.1", AI_NUMERICHOST
)) {
450 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
453 *server_name
= SMB_STRDUP("127.0.0.1");
457 DEBUG(1,("no server to connect to\n"));
464 bool net_find_pdc(struct sockaddr_storage
*server_ss
,
466 const char *domain_name
)
468 if (!get_pdc_ip(domain_name
, server_ss
)) {
471 if (is_zero_addr((struct sockaddr
*)server_ss
)) {
475 if (!name_status_find(domain_name
, 0x1b, 0x20, server_ss
, server_name
)) {
482 NTSTATUS
net_make_ipc_connection(struct net_context
*c
, unsigned flags
,
483 struct cli_state
**pcli
)
485 return net_make_ipc_connection_ex(c
, c
->opt_workgroup
, NULL
, NULL
, flags
, pcli
);
488 NTSTATUS
net_make_ipc_connection_ex(struct net_context
*c
,const char *domain
,
490 struct sockaddr_storage
*pss
,
491 unsigned flags
, struct cli_state
**pcli
)
493 char *server_name
= NULL
;
494 struct sockaddr_storage server_ss
;
495 struct cli_state
*cli
= NULL
;
498 if ( !server
|| !pss
) {
499 if (!net_find_server(c
, domain
, flags
, &server_ss
,
501 d_fprintf(stderr
, _("Unable to find a suitable server "
502 "for domain %s\n"), domain
);
503 nt_status
= NT_STATUS_UNSUCCESSFUL
;
507 server_name
= SMB_STRDUP( server
);
511 if (flags
& NET_FLAGS_ANONYMOUS
) {
512 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
515 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
,
519 /* store the server in the affinity cache if it was a PDC */
521 if ( (flags
& NET_FLAGS_PDC
) && NT_STATUS_IS_OK(nt_status
) )
522 saf_store( cli
->server_domain
, cli
->desthost
);
524 SAFE_FREE(server_name
);
525 if (!NT_STATUS_IS_OK(nt_status
)) {
526 d_fprintf(stderr
, _("Connection failed: %s\n"),
527 nt_errstr(nt_status
));
529 } else if (c
->opt_request_timeout
) {
530 cli_set_timeout(cli
, c
->opt_request_timeout
* 1000);
540 /****************************************************************************
541 ****************************************************************************/
543 const char *net_prompt_pass(struct net_context
*c
, const char *user
)
546 const char *pass
= NULL
;
548 if (c
->opt_password
) {
549 return c
->opt_password
;
552 if (c
->opt_machine_pass
) {
556 if (c
->opt_kerberos
&& !c
->opt_user_specified
) {
560 if (asprintf(&prompt
, _("Enter %s's password:"), user
) == -1) {
564 pass
= getpass(prompt
);
570 int net_run_function(struct net_context
*c
, int argc
, const char **argv
,
571 const char *whoami
, struct functable
*table
)
576 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
577 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
578 return table
[i
].fn(c
, argc
-1, argv
+1);
582 if (c
->display_usage
== false) {
583 d_fprintf(stderr
, _("Invalid command: %s %s\n"), whoami
,
584 (argc
> 0)?argv
[0]:"");
586 d_printf(_("Usage:\n"));
587 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
588 if(c
->display_usage
== false)
589 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
590 _(table
[i
].description
));
592 d_printf("%s\n", _(table
[i
].usage
));
595 return c
->display_usage
?0:-1;
598 void net_display_usage_from_functable(struct functable
*table
)
601 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
602 d_printf("%s\n", _(table
[i
].usage
));
606 const char *net_share_type_str(int num_type
)
609 case 0: return _("Disk");
610 case 1: return _("Print");
611 case 2: return _("Dev");
612 case 3: return _("IPC");
613 default: return _("Unknown");
617 NTSTATUS
net_scan_dc(struct net_context
*c
,
618 struct cli_state
*cli
,
619 struct net_dc_info
*dc_info
)
621 TALLOC_CTX
*mem_ctx
= talloc_tos();
622 struct rpc_pipe_client
*dssetup_pipe
= NULL
;
623 union dssetup_DsRoleInfo info
;
626 ZERO_STRUCTP(dc_info
);
628 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_dssetup
.syntax_id
,
630 if (!NT_STATUS_IS_OK(status
)) {
634 status
= rpccli_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_pipe
, mem_ctx
,
635 DS_ROLE_BASIC_INFORMATION
,
638 TALLOC_FREE(dssetup_pipe
);
640 if (!NT_STATUS_IS_OK(status
)) {
644 dc_info
->is_dc
= (info
.basic
.role
& (DS_ROLE_PRIMARY_DC
|DS_ROLE_BACKUP_DC
));
645 dc_info
->is_pdc
= (info
.basic
.role
& DS_ROLE_PRIMARY_DC
);
646 dc_info
->is_ad
= (info
.basic
.flags
& DS_ROLE_PRIMARY_DS_RUNNING
);
647 dc_info
->is_mixed_mode
= (info
.basic
.flags
& DS_ROLE_PRIMARY_DS_MIXED_MODE
);
648 dc_info
->netbios_domain_name
= talloc_strdup(mem_ctx
, info
.basic
.domain
);
649 dc_info
->dns_domain_name
= talloc_strdup(mem_ctx
, info
.basic
.dns_domain
);
650 dc_info
->forest_name
= talloc_strdup(mem_ctx
, info
.basic
.forest
);