samba-tool: dbcheck avoid problems with deleted objects
[Samba/gebeck_regimport.git] / source3 / utils / net_util.c
bloba6edc3976deeeeaa56c626989e5f15c8f1781228
1 /*
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/>.
22 #include "includes.h"
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"
28 #include "secrets.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;
42 struct dom_sid *sids;
43 enum lsa_SidType *types;
44 struct dcerpc_binding_handle *b;
46 ZERO_STRUCT(pol);
48 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
49 &lsa_pipe);
50 if (!NT_STATUS_IS_OK(status)) {
51 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
52 return status;
55 b = lsa_pipe->binding_handle;
57 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
58 SEC_FLAG_MAXIMUM_ALLOWED,
59 &pol);
60 if (!NT_STATUS_IS_OK(status)) {
61 d_fprintf(stderr, "open_policy %s: %s\n", _("failed"),
62 nt_errstr(status));
63 return status;
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 */
71 goto done;
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) {
84 *ret_type = types[0];
87 done:
88 if (is_valid_policy_hnd(&pol)) {
89 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
91 TALLOC_FREE(lsa_pipe);
93 return status;
96 /****************************************************************************
97 Connect to \\server\service.
98 ****************************************************************************/
100 NTSTATUS connect_to_service(struct net_context *c,
101 struct cli_state **cli_ctx,
102 const struct sockaddr_storage *server_ss,
103 const char *server_name,
104 const char *service_name,
105 const char *service_type)
107 NTSTATUS nt_status;
108 int flags = 0;
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;
120 if (c->opt_ccache) {
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,
129 SMB_SIGNING_DEFAULT);
130 if (!NT_STATUS_IS_OK(nt_status)) {
131 d_fprintf(stderr, _("Could not connect to server %s\n"),
132 server_name);
134 /* Display a nicer message depending on the result */
136 if (NT_STATUS_V(nt_status) ==
137 NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
138 d_fprintf(stderr,
139 _("The username or password was not "
140 "correct.\n"));
142 if (NT_STATUS_V(nt_status) ==
143 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
144 d_fprintf(stderr, _("The account was locked out.\n"));
146 if (NT_STATUS_V(nt_status) ==
147 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
148 d_fprintf(stderr, _("The account was disabled.\n"));
149 return nt_status;
152 if (c->smb_encrypt) {
153 nt_status = cli_force_encryption(*cli_ctx,
154 c->opt_user_name,
155 c->opt_password,
156 c->opt_workgroup);
158 if (NT_STATUS_EQUAL(nt_status,NT_STATUS_NOT_SUPPORTED)) {
159 d_printf(_("Encryption required and "
160 "server that doesn't support "
161 "UNIX extensions - failing connect\n"));
162 } else if (NT_STATUS_EQUAL(nt_status,NT_STATUS_UNKNOWN_REVISION)) {
163 d_printf(_("Encryption required and "
164 "can't get UNIX CIFS extensions "
165 "version from server.\n"));
166 } else if (NT_STATUS_EQUAL(nt_status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
167 d_printf(_("Encryption required and "
168 "share %s doesn't support "
169 "encryption.\n"), service_name);
170 } else if (!NT_STATUS_IS_OK(nt_status)) {
171 d_printf(_("Encryption required and "
172 "setup failed with error %s.\n"),
173 nt_errstr(nt_status));
176 if (!NT_STATUS_IS_OK(nt_status)) {
177 cli_shutdown(*cli_ctx);
178 *cli_ctx = NULL;
182 return nt_status;
185 /****************************************************************************
186 Connect to \\server\ipc$.
187 ****************************************************************************/
189 NTSTATUS connect_to_ipc(struct net_context *c,
190 struct cli_state **cli_ctx,
191 const struct sockaddr_storage *server_ss,
192 const char *server_name)
194 return connect_to_service(c, cli_ctx, server_ss, server_name, "IPC$",
195 "IPC");
198 /****************************************************************************
199 Connect to \\server\ipc$ anonymously.
200 ****************************************************************************/
202 NTSTATUS connect_to_ipc_anonymous(struct net_context *c,
203 struct cli_state **cli_ctx,
204 const struct sockaddr_storage *server_ss,
205 const char *server_name)
207 NTSTATUS nt_status;
209 nt_status = cli_full_connection(cli_ctx, c->opt_requester_name,
210 server_name, server_ss, c->opt_port,
211 "IPC$", "IPC",
212 "", "",
213 "", 0, SMB_SIGNING_DEFAULT);
215 if (NT_STATUS_IS_OK(nt_status)) {
216 return nt_status;
217 } else {
218 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status)));
219 return nt_status;
223 /****************************************************************************
224 Return malloced user@realm for krb5 login.
225 ****************************************************************************/
227 static char *get_user_and_realm(const char *username)
229 char *user_and_realm = NULL;
231 if (!username) {
232 return NULL;
234 if (strchr_m(username, '@')) {
235 user_and_realm = SMB_STRDUP(username);
236 } else {
237 if (asprintf(&user_and_realm, "%s@%s", username, lp_realm()) == -1) {
238 user_and_realm = NULL;
241 return user_and_realm;
244 /****************************************************************************
245 Connect to \\server\ipc$ using KRB5.
246 ****************************************************************************/
248 NTSTATUS connect_to_ipc_krb5(struct net_context *c,
249 struct cli_state **cli_ctx,
250 const struct sockaddr_storage *server_ss,
251 const char *server_name)
253 NTSTATUS nt_status;
254 char *user_and_realm = NULL;
256 /* FIXME: Should get existing kerberos ticket if possible. */
257 c->opt_password = net_prompt_pass(c, c->opt_user_name);
258 if (!c->opt_password) {
259 return NT_STATUS_NO_MEMORY;
262 user_and_realm = get_user_and_realm(c->opt_user_name);
263 if (!user_and_realm) {
264 return NT_STATUS_NO_MEMORY;
267 nt_status = cli_full_connection(cli_ctx, NULL, server_name,
268 server_ss, c->opt_port,
269 "IPC$", "IPC",
270 user_and_realm, c->opt_workgroup,
271 c->opt_password,
272 CLI_FULL_CONNECTION_USE_KERBEROS,
273 SMB_SIGNING_DEFAULT);
275 SAFE_FREE(user_and_realm);
277 if (!NT_STATUS_IS_OK(nt_status)) {
278 DEBUG(1,("Cannot connect to server using kerberos. Error was %s\n", nt_errstr(nt_status)));
279 return nt_status;
282 if (c->smb_encrypt) {
283 nt_status = cli_cm_force_encryption(*cli_ctx,
284 user_and_realm,
285 c->opt_password,
286 c->opt_workgroup,
287 "IPC$");
288 if (!NT_STATUS_IS_OK(nt_status)) {
289 cli_shutdown(*cli_ctx);
290 *cli_ctx = NULL;
294 return nt_status;
298 * Connect a server and open a given pipe
300 * @param cli_dst A cli_state
301 * @param pipe The pipe to open
302 * @param got_pipe boolean that stores if we got a pipe
304 * @return Normal NTSTATUS return.
306 NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
307 struct rpc_pipe_client **pp_pipe_hnd,
308 const struct ndr_syntax_id *interface)
310 NTSTATUS nt_status;
311 char *server_name = SMB_STRDUP("127.0.0.1");
312 struct cli_state *cli_tmp = NULL;
313 struct rpc_pipe_client *pipe_hnd = NULL;
315 if (server_name == NULL) {
316 return NT_STATUS_NO_MEMORY;
319 if (c->opt_destination) {
320 SAFE_FREE(server_name);
321 if ((server_name = SMB_STRDUP(c->opt_destination)) == NULL) {
322 return NT_STATUS_NO_MEMORY;
326 /* make a connection to a named pipe */
327 nt_status = connect_to_ipc(c, &cli_tmp, NULL, server_name);
328 if (!NT_STATUS_IS_OK(nt_status)) {
329 SAFE_FREE(server_name);
330 return nt_status;
333 nt_status = cli_rpc_pipe_open_noauth(cli_tmp, interface,
334 &pipe_hnd);
335 if (!NT_STATUS_IS_OK(nt_status)) {
336 DEBUG(0, ("couldn't not initialize pipe\n"));
337 cli_shutdown(cli_tmp);
338 SAFE_FREE(server_name);
339 return nt_status;
342 *cli_dst = cli_tmp;
343 *pp_pipe_hnd = pipe_hnd;
344 SAFE_FREE(server_name);
346 return nt_status;
349 /****************************************************************************
350 Use the local machine account (krb) and password for this session.
351 ****************************************************************************/
353 int net_use_krb_machine_account(struct net_context *c)
355 char *user_name = NULL;
357 if (!secrets_init()) {
358 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
359 exit(1);
362 c->opt_password = secrets_fetch_machine_password(
363 c->opt_target_workgroup, NULL, NULL);
364 if (asprintf(&user_name, "%s$@%s", lp_netbios_name(), lp_realm()) == -1) {
365 return -1;
367 c->opt_user_name = user_name;
368 return 0;
371 /****************************************************************************
372 Use the machine account name and password for this session.
373 ****************************************************************************/
375 int net_use_machine_account(struct net_context *c)
377 char *user_name = NULL;
379 if (!secrets_init()) {
380 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
381 exit(1);
384 c->opt_password = secrets_fetch_machine_password(
385 c->opt_target_workgroup, NULL, NULL);
386 if (asprintf(&user_name, "%s$", lp_netbios_name()) == -1) {
387 return -1;
389 c->opt_user_name = user_name;
390 return 0;
393 bool net_find_server(struct net_context *c,
394 const char *domain,
395 unsigned flags,
396 struct sockaddr_storage *server_ss,
397 char **server_name)
399 const char *d = domain ? domain : c->opt_target_workgroup;
401 if (c->opt_host) {
402 *server_name = SMB_STRDUP(c->opt_host);
405 if (c->opt_have_ip) {
406 *server_ss = c->opt_dest_ip;
407 if (!*server_name) {
408 char addr[INET6_ADDRSTRLEN];
409 print_sockaddr(addr, sizeof(addr), &c->opt_dest_ip);
410 *server_name = SMB_STRDUP(addr);
412 } else if (*server_name) {
413 /* resolve the IP address */
414 if (!resolve_name(*server_name, server_ss, 0x20, false)) {
415 DEBUG(1,("Unable to resolve server name\n"));
416 return false;
418 } else if (flags & NET_FLAGS_PDC) {
419 fstring dc_name;
420 struct sockaddr_storage pdc_ss;
422 if (!get_pdc_ip(d, &pdc_ss)) {
423 DEBUG(1,("Unable to resolve PDC server address\n"));
424 return false;
427 if (is_zero_addr(&pdc_ss)) {
428 return false;
431 if (!name_status_find(d, 0x1b, 0x20, &pdc_ss, dc_name)) {
432 return false;
435 *server_name = SMB_STRDUP(dc_name);
436 *server_ss = pdc_ss;
437 } else if (flags & NET_FLAGS_DMB) {
438 struct sockaddr_storage msbrow_ss;
439 char addr[INET6_ADDRSTRLEN];
441 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
442 if (!resolve_name(d, &msbrow_ss, 0x1B, false)) {
443 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
444 return false;
446 *server_ss = msbrow_ss;
447 print_sockaddr(addr, sizeof(addr), server_ss);
448 *server_name = SMB_STRDUP(addr);
449 } else if (flags & NET_FLAGS_MASTER) {
450 struct sockaddr_storage brow_ss;
451 char addr[INET6_ADDRSTRLEN];
452 if (!resolve_name(d, &brow_ss, 0x1D, false)) {
453 /* go looking for workgroups */
454 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
455 return false;
457 *server_ss = brow_ss;
458 print_sockaddr(addr, sizeof(addr), server_ss);
459 *server_name = SMB_STRDUP(addr);
460 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
461 if (!interpret_string_addr(server_ss,
462 "127.0.0.1", AI_NUMERICHOST)) {
463 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
464 return false;
466 *server_name = SMB_STRDUP("127.0.0.1");
469 if (!*server_name) {
470 DEBUG(1,("no server to connect to\n"));
471 return false;
474 return true;
477 bool net_find_pdc(struct sockaddr_storage *server_ss,
478 fstring server_name,
479 const char *domain_name)
481 if (!get_pdc_ip(domain_name, server_ss)) {
482 return false;
484 if (is_zero_addr(server_ss)) {
485 return false;
488 if (!name_status_find(domain_name, 0x1b, 0x20, server_ss, server_name)) {
489 return false;
492 return true;
495 NTSTATUS net_make_ipc_connection(struct net_context *c, unsigned flags,
496 struct cli_state **pcli)
498 return net_make_ipc_connection_ex(c, c->opt_workgroup, NULL, NULL, flags, pcli);
501 NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
502 const char *server,
503 const struct sockaddr_storage *pss,
504 unsigned flags, struct cli_state **pcli)
506 char *server_name = NULL;
507 struct sockaddr_storage server_ss;
508 struct cli_state *cli = NULL;
509 NTSTATUS nt_status;
511 if ( !server || !pss ) {
512 if (!net_find_server(c, domain, flags, &server_ss,
513 &server_name)) {
514 d_fprintf(stderr, _("Unable to find a suitable server "
515 "for domain %s\n"), domain);
516 nt_status = NT_STATUS_UNSUCCESSFUL;
517 goto done;
519 } else {
520 server_name = SMB_STRDUP( server );
521 server_ss = *pss;
524 if (flags & NET_FLAGS_ANONYMOUS) {
525 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
526 server_name);
527 } else {
528 nt_status = connect_to_ipc(c, &cli, &server_ss,
529 server_name);
532 /* store the server in the affinity cache if it was a PDC */
534 if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
535 saf_store(cli->server_domain, server_name);
537 SAFE_FREE(server_name);
538 if (!NT_STATUS_IS_OK(nt_status)) {
539 d_fprintf(stderr, _("Connection failed: %s\n"),
540 nt_errstr(nt_status));
541 cli = NULL;
542 } else if (c->opt_request_timeout) {
543 cli_set_timeout(cli, c->opt_request_timeout * 1000);
546 done:
547 if (pcli != NULL) {
548 *pcli = cli;
550 return nt_status;
553 /****************************************************************************
554 ****************************************************************************/
556 const char *net_prompt_pass(struct net_context *c, const char *user)
558 char *prompt = NULL;
559 const char *pass = NULL;
561 if (c->opt_password) {
562 return c->opt_password;
565 if (c->opt_machine_pass) {
566 return NULL;
569 if (c->opt_kerberos && !c->opt_user_specified) {
570 return NULL;
573 if (asprintf(&prompt, _("Enter %s's password:"), user) == -1) {
574 return NULL;
577 pass = getpass(prompt);
578 SAFE_FREE(prompt);
580 return pass;
583 int net_run_function(struct net_context *c, int argc, const char **argv,
584 const char *whoami, struct functable *table)
586 int i;
588 if (argc != 0) {
589 for (i=0; table[i].funcname != NULL; i++) {
590 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
591 return table[i].fn(c, argc-1, argv+1);
595 if (c->display_usage == false) {
596 d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
597 (argc > 0)?argv[0]:"");
599 d_printf(_("Usage:\n"));
600 for (i=0; table[i].funcname != NULL; i++) {
601 if(c->display_usage == false)
602 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
603 _(table[i].description));
604 else
605 d_printf("%s\n", _(table[i].usage));
608 return c->display_usage?0:-1;
611 void net_display_usage_from_functable(struct functable *table)
613 int i;
614 for (i=0; table[i].funcname != NULL; i++) {
615 d_printf("%s\n", _(table[i].usage));
619 const char *net_share_type_str(int num_type)
621 switch(num_type) {
622 case 0: return _("Disk");
623 case 1: return _("Print");
624 case 2: return _("Dev");
625 case 3: return _("IPC");
626 default: return _("Unknown");
630 static NTSTATUS net_scan_dc_noad(struct net_context *c,
631 struct cli_state *cli,
632 struct net_dc_info *dc_info)
634 TALLOC_CTX *mem_ctx = talloc_tos();
635 struct rpc_pipe_client *pipe_hnd = NULL;
636 struct dcerpc_binding_handle *b;
637 NTSTATUS status, result;
638 struct policy_handle pol;
639 union lsa_PolicyInformation *info;
641 ZERO_STRUCTP(dc_info);
642 ZERO_STRUCT(pol);
644 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
645 &pipe_hnd);
646 if (!NT_STATUS_IS_OK(status)) {
647 return status;
650 b = pipe_hnd->binding_handle;
652 status = dcerpc_lsa_open_policy(b, mem_ctx,
653 false,
654 SEC_FLAG_MAXIMUM_ALLOWED,
655 &pol,
656 &result);
657 if (!NT_STATUS_IS_OK(status)) {
658 goto done;
660 if (!NT_STATUS_IS_OK(result)) {
661 status = result;
662 goto done;
665 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
666 &pol,
667 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
668 &info,
669 &result);
670 if (!NT_STATUS_IS_OK(status)) {
671 goto done;
673 if (!NT_STATUS_IS_OK(result)) {
674 status = result;
675 goto done;
678 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info->account_domain.name.string);
679 if (dc_info->netbios_domain_name == NULL) {
680 status = NT_STATUS_NO_MEMORY;
681 goto done;
684 done:
685 if (is_valid_policy_hnd(&pol)) {
686 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
689 TALLOC_FREE(pipe_hnd);
691 return status;
694 NTSTATUS net_scan_dc(struct net_context *c,
695 struct cli_state *cli,
696 struct net_dc_info *dc_info)
698 TALLOC_CTX *mem_ctx = talloc_tos();
699 struct rpc_pipe_client *dssetup_pipe = NULL;
700 struct dcerpc_binding_handle *dssetup_handle = NULL;
701 union dssetup_DsRoleInfo info;
702 NTSTATUS status;
703 WERROR werr;
705 ZERO_STRUCTP(dc_info);
707 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_dssetup.syntax_id,
708 &dssetup_pipe);
709 if (!NT_STATUS_IS_OK(status)) {
710 DEBUG(10,("net_scan_dc: failed to open dssetup pipe with %s, "
711 "retrying with lsa pipe\n", nt_errstr(status)));
712 return net_scan_dc_noad(c, cli, dc_info);
714 dssetup_handle = dssetup_pipe->binding_handle;
716 status = dcerpc_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_handle, mem_ctx,
717 DS_ROLE_BASIC_INFORMATION,
718 &info,
719 &werr);
720 TALLOC_FREE(dssetup_pipe);
722 if (NT_STATUS_IS_OK(status)) {
723 status = werror_to_ntstatus(werr);
725 if (!NT_STATUS_IS_OK(status)) {
726 return status;
729 dc_info->is_dc = (info.basic.role & (DS_ROLE_PRIMARY_DC|DS_ROLE_BACKUP_DC));
730 dc_info->is_pdc = (info.basic.role & DS_ROLE_PRIMARY_DC);
731 dc_info->is_ad = (info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING);
732 dc_info->is_mixed_mode = (info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE);
733 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info.basic.domain);
734 dc_info->dns_domain_name = talloc_strdup(mem_ctx, info.basic.dns_domain);
735 dc_info->forest_name = talloc_strdup(mem_ctx, info.basic.forest);
737 return NT_STATUS_OK;