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"
25 NTSTATUS
net_rpc_lookup_name(struct net_context
*c
,
26 TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
27 const char *name
, const char **ret_domain
,
28 const char **ret_name
, DOM_SID
*ret_sid
,
29 enum lsa_SidType
*ret_type
)
31 struct rpc_pipe_client
*lsa_pipe
;
32 struct policy_handle pol
;
33 NTSTATUS result
= NT_STATUS_OK
;
34 const char **dom_names
;
36 enum lsa_SidType
*types
;
40 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
42 if (!NT_STATUS_IS_OK(result
)) {
43 d_fprintf(stderr
, "Could not initialise lsa pipe\n");
47 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
48 SEC_FLAG_MAXIMUM_ALLOWED
,
50 if (!NT_STATUS_IS_OK(result
)) {
51 d_fprintf(stderr
, "open_policy failed: %s\n",
56 result
= rpccli_lsa_lookup_names(lsa_pipe
, mem_ctx
, &pol
, 1,
57 &name
, &dom_names
, 1, &sids
, &types
);
59 if (!NT_STATUS_IS_OK(result
)) {
60 /* This can happen easily, don't log an error */
64 if (ret_domain
!= NULL
) {
65 *ret_domain
= dom_names
[0];
67 if (ret_name
!= NULL
) {
68 *ret_name
= talloc_strdup(mem_ctx
, name
);
70 if (ret_sid
!= NULL
) {
71 sid_copy(ret_sid
, &sids
[0]);
73 if (ret_type
!= NULL
) {
78 if (is_valid_policy_hnd(&pol
)) {
79 rpccli_lsa_Close(lsa_pipe
, mem_ctx
, &pol
);
81 TALLOC_FREE(lsa_pipe
);
86 /****************************************************************************
87 Connect to \\server\service.
88 ****************************************************************************/
90 NTSTATUS
connect_to_service(struct net_context
*c
,
91 struct cli_state
**cli_ctx
,
92 struct sockaddr_storage
*server_ss
,
93 const char *server_name
,
94 const char *service_name
,
95 const char *service_type
)
99 struct user_auth_info
*ai
= c
->auth_info
;
101 set_cmdline_auth_info_getpass(ai
);
103 if (get_cmdline_auth_info_use_kerberos(ai
)) {
104 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
105 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
108 nt_status
= cli_full_connection(cli_ctx
, NULL
, server_name
,
109 server_ss
, c
->opt_port
,
110 service_name
, service_type
,
111 get_cmdline_auth_info_username(ai
),
113 get_cmdline_auth_info_password(ai
),
114 flags
, Undefined
, NULL
);
115 if (!NT_STATUS_IS_OK(nt_status
)) {
116 d_fprintf(stderr
, "Could not connect to server %s\n", server_name
);
118 /* Display a nicer message depending on the result */
120 if (NT_STATUS_V(nt_status
) ==
121 NT_STATUS_V(NT_STATUS_LOGON_FAILURE
))
122 d_fprintf(stderr
, "The username or password was not correct.\n");
124 if (NT_STATUS_V(nt_status
) ==
125 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT
))
126 d_fprintf(stderr
, "The account was locked out.\n");
128 if (NT_STATUS_V(nt_status
) ==
129 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED
))
130 d_fprintf(stderr
, "The account was disabled.\n");
134 if (get_cmdline_auth_info_smb_encrypt(ai
)) {
135 nt_status
= cli_force_encryption(*cli_ctx
,
136 get_cmdline_auth_info_username(ai
),
137 get_cmdline_auth_info_password(ai
),
140 if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_NOT_SUPPORTED
)) {
141 d_printf("Encryption required and "
142 "server that doesn't support "
143 "UNIX extensions - failing connect\n");
144 } else if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_UNKNOWN_REVISION
)) {
145 d_printf("Encryption required and "
146 "can't get UNIX CIFS extensions "
147 "version from server.\n");
148 } else if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_UNSUPPORTED_COMPRESSION
)) {
149 d_printf("Encryption required and "
150 "share %s doesn't support "
151 "encryption.\n", service_name
);
152 } else if (!NT_STATUS_IS_OK(nt_status
)) {
153 d_printf("Encryption required and "
154 "setup failed with error %s.\n",
155 nt_errstr(nt_status
));
158 if (!NT_STATUS_IS_OK(nt_status
)) {
159 cli_shutdown(*cli_ctx
);
167 /****************************************************************************
168 Connect to \\server\ipc$.
169 ****************************************************************************/
171 NTSTATUS
connect_to_ipc(struct net_context
*c
,
172 struct cli_state
**cli_ctx
,
173 struct sockaddr_storage
*server_ss
,
174 const char *server_name
)
176 return connect_to_service(c
, cli_ctx
, server_ss
, server_name
, "IPC$",
180 /****************************************************************************
181 Connect to \\server\ipc$ anonymously.
182 ****************************************************************************/
184 NTSTATUS
connect_to_ipc_anonymous(struct net_context
*c
,
185 struct cli_state
**cli_ctx
,
186 struct sockaddr_storage
*server_ss
,
187 const char *server_name
)
191 nt_status
= cli_full_connection(cli_ctx
, c
->opt_requester_name
,
192 server_name
, server_ss
, c
->opt_port
,
195 "", 0, Undefined
, NULL
);
197 if (NT_STATUS_IS_OK(nt_status
)) {
200 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status
)));
205 /****************************************************************************
206 Return malloced user@realm for krb5 login.
207 ****************************************************************************/
209 static char *get_user_and_realm(const char *username
)
211 char *user_and_realm
= NULL
;
216 if (strchr_m(username
, '@')) {
217 user_and_realm
= SMB_STRDUP(username
);
219 if (asprintf(&user_and_realm
, "%s@%s", username
, lp_realm()) == -1) {
220 user_and_realm
= NULL
;
223 return user_and_realm
;
226 /****************************************************************************
227 Connect to \\server\ipc$ using KRB5.
228 ****************************************************************************/
230 NTSTATUS
connect_to_ipc_krb5(struct net_context
*c
,
231 struct cli_state
**cli_ctx
,
232 struct sockaddr_storage
*server_ss
,
233 const char *server_name
)
236 char *user_and_realm
= NULL
;
237 struct user_auth_info
*ai
= c
->auth_info
;
239 /* FIXME: Should get existing kerberos ticket if possible. */
240 set_cmdline_auth_info_getpass(ai
);
242 user_and_realm
= get_user_and_realm(get_cmdline_auth_info_username(ai
));
243 if (!user_and_realm
) {
244 return NT_STATUS_NO_MEMORY
;
247 nt_status
= cli_full_connection(cli_ctx
, NULL
, server_name
,
248 server_ss
, c
->opt_port
,
250 user_and_realm
, c
->opt_workgroup
,
251 get_cmdline_auth_info_password(ai
),
252 CLI_FULL_CONNECTION_USE_KERBEROS
,
255 SAFE_FREE(user_and_realm
);
257 if (!NT_STATUS_IS_OK(nt_status
)) {
258 DEBUG(1,("Cannot connect to server using kerberos. Error was %s\n", nt_errstr(nt_status
)));
262 if (get_cmdline_auth_info_smb_encrypt(ai
)) {
263 nt_status
= cli_cm_force_encryption(*cli_ctx
,
265 get_cmdline_auth_info_password(ai
),
268 if (!NT_STATUS_IS_OK(nt_status
)) {
269 cli_shutdown(*cli_ctx
);
278 * Connect a server and open a given pipe
280 * @param cli_dst A cli_state
281 * @param pipe The pipe to open
282 * @param got_pipe boolean that stores if we got a pipe
284 * @return Normal NTSTATUS return.
286 NTSTATUS
connect_dst_pipe(struct net_context
*c
, struct cli_state
**cli_dst
,
287 struct rpc_pipe_client
**pp_pipe_hnd
,
288 const struct ndr_syntax_id
*interface
)
291 char *server_name
= SMB_STRDUP("127.0.0.1");
292 struct cli_state
*cli_tmp
= NULL
;
293 struct rpc_pipe_client
*pipe_hnd
= NULL
;
295 if (server_name
== NULL
) {
296 return NT_STATUS_NO_MEMORY
;
299 if (c
->opt_destination
) {
300 SAFE_FREE(server_name
);
301 if ((server_name
= SMB_STRDUP(c
->opt_destination
)) == NULL
) {
302 return NT_STATUS_NO_MEMORY
;
306 /* make a connection to a named pipe */
307 nt_status
= connect_to_ipc(c
, &cli_tmp
, NULL
, server_name
);
308 if (!NT_STATUS_IS_OK(nt_status
)) {
309 SAFE_FREE(server_name
);
313 nt_status
= cli_rpc_pipe_open_noauth(cli_tmp
, interface
,
315 if (!NT_STATUS_IS_OK(nt_status
)) {
316 DEBUG(0, ("couldn't not initialize pipe\n"));
317 cli_shutdown(cli_tmp
);
318 SAFE_FREE(server_name
);
323 *pp_pipe_hnd
= pipe_hnd
;
324 SAFE_FREE(server_name
);
329 bool net_find_server(struct net_context
*c
,
332 struct sockaddr_storage
*server_ss
,
335 const char *d
= domain
? domain
: c
->opt_target_workgroup
;
338 *server_name
= SMB_STRDUP(c
->opt_host
);
341 if (c
->opt_have_ip
) {
342 *server_ss
= c
->opt_dest_ip
;
344 char addr
[INET6_ADDRSTRLEN
];
345 print_sockaddr(addr
, sizeof(addr
), &c
->opt_dest_ip
);
346 *server_name
= SMB_STRDUP(addr
);
348 } else if (*server_name
) {
349 /* resolve the IP address */
350 if (!resolve_name(*server_name
, server_ss
, 0x20)) {
351 DEBUG(1,("Unable to resolve server name\n"));
354 } else if (flags
& NET_FLAGS_PDC
) {
356 struct sockaddr_storage pdc_ss
;
358 if (!get_pdc_ip(d
, &pdc_ss
)) {
359 DEBUG(1,("Unable to resolve PDC server address\n"));
363 if (is_zero_addr((struct sockaddr
*)&pdc_ss
)) {
367 if (!name_status_find(d
, 0x1b, 0x20, &pdc_ss
, dc_name
)) {
371 *server_name
= SMB_STRDUP(dc_name
);
373 } else if (flags
& NET_FLAGS_DMB
) {
374 struct sockaddr_storage msbrow_ss
;
375 char addr
[INET6_ADDRSTRLEN
];
377 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
378 if (!resolve_name(d
, &msbrow_ss
, 0x1B)) {
379 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
382 *server_ss
= msbrow_ss
;
383 print_sockaddr(addr
, sizeof(addr
), server_ss
);
384 *server_name
= SMB_STRDUP(addr
);
385 } else if (flags
& NET_FLAGS_MASTER
) {
386 struct sockaddr_storage brow_ss
;
387 char addr
[INET6_ADDRSTRLEN
];
388 if (!resolve_name(d
, &brow_ss
, 0x1D)) {
389 /* go looking for workgroups */
390 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
393 *server_ss
= brow_ss
;
394 print_sockaddr(addr
, sizeof(addr
), server_ss
);
395 *server_name
= SMB_STRDUP(addr
);
396 } else if (!(flags
& NET_FLAGS_LOCALHOST_DEFAULT_INSANE
)) {
397 if (!interpret_string_addr(server_ss
,
398 "127.0.0.1", AI_NUMERICHOST
)) {
399 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
402 *server_name
= SMB_STRDUP("127.0.0.1");
406 DEBUG(1,("no server to connect to\n"));
413 bool net_find_pdc(struct sockaddr_storage
*server_ss
,
415 const char *domain_name
)
417 if (!get_pdc_ip(domain_name
, server_ss
)) {
420 if (is_zero_addr((struct sockaddr
*)server_ss
)) {
424 if (!name_status_find(domain_name
, 0x1b, 0x20, server_ss
, server_name
)) {
431 NTSTATUS
net_make_ipc_connection(struct net_context
*c
, unsigned flags
,
432 struct cli_state
**pcli
)
434 return net_make_ipc_connection_ex(c
, c
->opt_workgroup
, NULL
, NULL
, flags
, pcli
);
437 NTSTATUS
net_make_ipc_connection_ex(struct net_context
*c
,const char *domain
,
439 struct sockaddr_storage
*pss
,
440 unsigned flags
, struct cli_state
**pcli
)
442 char *server_name
= NULL
;
443 struct sockaddr_storage server_ss
;
444 struct cli_state
*cli
= NULL
;
447 if ( !server
|| !pss
) {
448 if (!net_find_server(c
, domain
, flags
, &server_ss
,
450 d_fprintf(stderr
, "Unable to find a suitable server "
451 "for domain %s\n", domain
);
452 nt_status
= NT_STATUS_UNSUCCESSFUL
;
456 server_name
= SMB_STRDUP( server
);
460 if (flags
& NET_FLAGS_ANONYMOUS
) {
461 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
464 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
,
468 /* store the server in the affinity cache if it was a PDC */
470 if ( (flags
& NET_FLAGS_PDC
) && NT_STATUS_IS_OK(nt_status
) )
471 saf_store( cli
->server_domain
, cli
->desthost
);
473 SAFE_FREE(server_name
);
474 if (!NT_STATUS_IS_OK(nt_status
)) {
475 d_fprintf(stderr
, "Connection failed: %s\n",
476 nt_errstr(nt_status
));
478 } else if (c
->opt_request_timeout
) {
479 cli_set_timeout(cli
, c
->opt_request_timeout
* 1000);
489 /****************************************************************************
490 ****************************************************************************/
492 int net_run_function(struct net_context
*c
, int argc
, const char **argv
,
493 const char *whoami
, struct functable
*table
)
498 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
499 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
500 return table
[i
].fn(c
, argc
-1, argv
+1);
504 if (c
->display_usage
== false) {
505 d_fprintf(stderr
, "Invalid command: %s %s\n", whoami
,
506 (argc
> 0)?argv
[0]:"");
508 d_printf("Usage:\n");
509 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
510 if(c
->display_usage
== false)
511 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
512 table
[i
].description
);
514 d_printf("%s\n", table
[i
].usage
);
517 return c
->display_usage
?0:-1;
520 void net_display_usage_from_functable(struct functable
*table
)
523 for (i
=0; table
[i
].funcname
!= NULL
; i
++) {
524 d_printf("%s\n", table
[i
].usage
);
528 const char *net_share_type_str(int num_type
)
531 case 0: return "Disk";
532 case 1: return "Print";
533 case 2: return "Dev";
534 case 3: return "IPC";
535 default: return "Unknown";
539 NTSTATUS
net_scan_dc(struct net_context
*c
,
540 struct cli_state
*cli
,
541 struct net_dc_info
*dc_info
)
543 TALLOC_CTX
*mem_ctx
= talloc_tos();
544 struct rpc_pipe_client
*dssetup_pipe
= NULL
;
545 union dssetup_DsRoleInfo info
;
548 ZERO_STRUCTP(dc_info
);
550 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_dssetup
.syntax_id
,
552 if (!NT_STATUS_IS_OK(status
)) {
556 status
= rpccli_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_pipe
, mem_ctx
,
557 DS_ROLE_BASIC_INFORMATION
,
560 TALLOC_FREE(dssetup_pipe
);
562 if (!NT_STATUS_IS_OK(status
)) {
566 dc_info
->is_dc
= (info
.basic
.role
& (DS_ROLE_PRIMARY_DC
|DS_ROLE_BACKUP_DC
));
567 dc_info
->is_pdc
= (info
.basic
.role
& DS_ROLE_PRIMARY_DC
);
568 dc_info
->is_ad
= (info
.basic
.flags
& DS_ROLE_PRIMARY_DS_RUNNING
);
569 dc_info
->is_mixed_mode
= (info
.basic
.flags
& DS_ROLE_PRIMARY_DS_MIXED_MODE
);
570 dc_info
->netbios_domain_name
= talloc_strdup(mem_ctx
, info
.basic
.domain
);
571 dc_info
->dns_domain_name
= talloc_strdup(mem_ctx
, info
.basic
.dns_domain
);
572 dc_info
->forest_name
= talloc_strdup(mem_ctx
, info
.basic
.forest
);