s4-drs: additional delete test cases
[Samba/eduardoll.git] / source3 / utils / net_util.c
blobbfa56a7016ea4e4b6d5c7c439d9dba9d1860910f
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 "../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;
37 DOM_SID *sids;
38 enum lsa_SidType *types;
40 ZERO_STRUCT(pol);
42 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
43 &lsa_pipe);
44 if (!NT_STATUS_IS_OK(result)) {
45 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
46 return result;
49 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
50 SEC_FLAG_MAXIMUM_ALLOWED,
51 &pol);
52 if (!NT_STATUS_IS_OK(result)) {
53 d_fprintf(stderr, "open_policy %s: %s\n", _("failed"),
54 nt_errstr(result));
55 return result;
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 */
63 goto done;
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) {
76 *ret_type = types[0];
79 done:
80 if (is_valid_policy_hnd(&pol)) {
81 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
83 TALLOC_FREE(lsa_pipe);
85 return result;
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)
99 NTSTATUS nt_status;
100 int flags = 0;
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 if (c->opt_ccache) {
113 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
116 nt_status = cli_full_connection(cli_ctx, NULL, server_name,
117 server_ss, c->opt_port,
118 service_name, service_type,
119 c->opt_user_name, c->opt_workgroup,
120 c->opt_password, flags, Undefined, NULL);
121 if (!NT_STATUS_IS_OK(nt_status)) {
122 d_fprintf(stderr, _("Could not connect to server %s\n"),
123 server_name);
125 /* Display a nicer message depending on the result */
127 if (NT_STATUS_V(nt_status) ==
128 NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
129 d_fprintf(stderr,
130 _("The username or password was not "
131 "correct.\n"));
133 if (NT_STATUS_V(nt_status) ==
134 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
135 d_fprintf(stderr, _("The account was locked out.\n"));
137 if (NT_STATUS_V(nt_status) ==
138 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
139 d_fprintf(stderr, _("The account was disabled.\n"));
140 return nt_status;
143 if (c->smb_encrypt) {
144 nt_status = cli_force_encryption(*cli_ctx,
145 c->opt_user_name,
146 c->opt_password,
147 c->opt_workgroup);
149 if (NT_STATUS_EQUAL(nt_status,NT_STATUS_NOT_SUPPORTED)) {
150 d_printf(_("Encryption required and "
151 "server that doesn't support "
152 "UNIX extensions - failing connect\n"));
153 } else if (NT_STATUS_EQUAL(nt_status,NT_STATUS_UNKNOWN_REVISION)) {
154 d_printf(_("Encryption required and "
155 "can't get UNIX CIFS extensions "
156 "version from server.\n"));
157 } else if (NT_STATUS_EQUAL(nt_status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
158 d_printf(_("Encryption required and "
159 "share %s doesn't support "
160 "encryption.\n"), service_name);
161 } else if (!NT_STATUS_IS_OK(nt_status)) {
162 d_printf(_("Encryption required and "
163 "setup failed with error %s.\n"),
164 nt_errstr(nt_status));
167 if (!NT_STATUS_IS_OK(nt_status)) {
168 cli_shutdown(*cli_ctx);
169 *cli_ctx = NULL;
173 return nt_status;
176 /****************************************************************************
177 Connect to \\server\ipc$.
178 ****************************************************************************/
180 NTSTATUS connect_to_ipc(struct net_context *c,
181 struct cli_state **cli_ctx,
182 struct sockaddr_storage *server_ss,
183 const char *server_name)
185 return connect_to_service(c, cli_ctx, server_ss, server_name, "IPC$",
186 "IPC");
189 /****************************************************************************
190 Connect to \\server\ipc$ anonymously.
191 ****************************************************************************/
193 NTSTATUS connect_to_ipc_anonymous(struct net_context *c,
194 struct cli_state **cli_ctx,
195 struct sockaddr_storage *server_ss,
196 const char *server_name)
198 NTSTATUS nt_status;
200 nt_status = cli_full_connection(cli_ctx, c->opt_requester_name,
201 server_name, server_ss, c->opt_port,
202 "IPC$", "IPC",
203 "", "",
204 "", 0, Undefined, NULL);
206 if (NT_STATUS_IS_OK(nt_status)) {
207 return nt_status;
208 } else {
209 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status)));
210 return nt_status;
214 /****************************************************************************
215 Return malloced user@realm for krb5 login.
216 ****************************************************************************/
218 static char *get_user_and_realm(const char *username)
220 char *user_and_realm = NULL;
222 if (!username) {
223 return NULL;
225 if (strchr_m(username, '@')) {
226 user_and_realm = SMB_STRDUP(username);
227 } else {
228 if (asprintf(&user_and_realm, "%s@%s", username, lp_realm()) == -1) {
229 user_and_realm = NULL;
232 return user_and_realm;
235 /****************************************************************************
236 Connect to \\server\ipc$ using KRB5.
237 ****************************************************************************/
239 NTSTATUS connect_to_ipc_krb5(struct net_context *c,
240 struct cli_state **cli_ctx,
241 struct sockaddr_storage *server_ss,
242 const char *server_name)
244 NTSTATUS nt_status;
245 char *user_and_realm = NULL;
247 /* FIXME: Should get existing kerberos ticket if possible. */
248 c->opt_password = net_prompt_pass(c, c->opt_user_name);
249 if (!c->opt_password) {
250 return NT_STATUS_NO_MEMORY;
253 user_and_realm = get_user_and_realm(c->opt_user_name);
254 if (!user_and_realm) {
255 return NT_STATUS_NO_MEMORY;
258 nt_status = cli_full_connection(cli_ctx, NULL, server_name,
259 server_ss, c->opt_port,
260 "IPC$", "IPC",
261 user_and_realm, c->opt_workgroup,
262 c->opt_password,
263 CLI_FULL_CONNECTION_USE_KERBEROS,
264 Undefined, NULL);
266 SAFE_FREE(user_and_realm);
268 if (!NT_STATUS_IS_OK(nt_status)) {
269 DEBUG(1,("Cannot connect to server using kerberos. Error was %s\n", nt_errstr(nt_status)));
270 return nt_status;
273 if (c->smb_encrypt) {
274 nt_status = cli_cm_force_encryption(*cli_ctx,
275 user_and_realm,
276 c->opt_password,
277 c->opt_workgroup,
278 "IPC$");
279 if (!NT_STATUS_IS_OK(nt_status)) {
280 cli_shutdown(*cli_ctx);
281 *cli_ctx = NULL;
285 return nt_status;
289 * Connect a server and open a given pipe
291 * @param cli_dst A cli_state
292 * @param pipe The pipe to open
293 * @param got_pipe boolean that stores if we got a pipe
295 * @return Normal NTSTATUS return.
297 NTSTATUS connect_dst_pipe(struct net_context *c, struct cli_state **cli_dst,
298 struct rpc_pipe_client **pp_pipe_hnd,
299 const struct ndr_syntax_id *interface)
301 NTSTATUS nt_status;
302 char *server_name = SMB_STRDUP("127.0.0.1");
303 struct cli_state *cli_tmp = NULL;
304 struct rpc_pipe_client *pipe_hnd = NULL;
306 if (server_name == NULL) {
307 return NT_STATUS_NO_MEMORY;
310 if (c->opt_destination) {
311 SAFE_FREE(server_name);
312 if ((server_name = SMB_STRDUP(c->opt_destination)) == NULL) {
313 return NT_STATUS_NO_MEMORY;
317 /* make a connection to a named pipe */
318 nt_status = connect_to_ipc(c, &cli_tmp, NULL, server_name);
319 if (!NT_STATUS_IS_OK(nt_status)) {
320 SAFE_FREE(server_name);
321 return nt_status;
324 nt_status = cli_rpc_pipe_open_noauth(cli_tmp, interface,
325 &pipe_hnd);
326 if (!NT_STATUS_IS_OK(nt_status)) {
327 DEBUG(0, ("couldn't not initialize pipe\n"));
328 cli_shutdown(cli_tmp);
329 SAFE_FREE(server_name);
330 return nt_status;
333 *cli_dst = cli_tmp;
334 *pp_pipe_hnd = pipe_hnd;
335 SAFE_FREE(server_name);
337 return nt_status;
340 /****************************************************************************
341 Use the local machine account (krb) and password for this session.
342 ****************************************************************************/
344 int net_use_krb_machine_account(struct net_context *c)
346 char *user_name = NULL;
348 if (!secrets_init()) {
349 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
350 exit(1);
353 c->opt_password = secrets_fetch_machine_password(
354 c->opt_target_workgroup, NULL, NULL);
355 if (asprintf(&user_name, "%s$@%s", global_myname(), lp_realm()) == -1) {
356 return -1;
358 c->opt_user_name = user_name;
359 return 0;
362 /****************************************************************************
363 Use the machine account name and password for this session.
364 ****************************************************************************/
366 int net_use_machine_account(struct net_context *c)
368 char *user_name = NULL;
370 if (!secrets_init()) {
371 d_fprintf(stderr,_("ERROR: Unable to open secrets database\n"));
372 exit(1);
375 c->opt_password = secrets_fetch_machine_password(
376 c->opt_target_workgroup, NULL, NULL);
377 if (asprintf(&user_name, "%s$", global_myname()) == -1) {
378 return -1;
380 c->opt_user_name = user_name;
381 return 0;
384 bool net_find_server(struct net_context *c,
385 const char *domain,
386 unsigned flags,
387 struct sockaddr_storage *server_ss,
388 char **server_name)
390 const char *d = domain ? domain : c->opt_target_workgroup;
392 if (c->opt_host) {
393 *server_name = SMB_STRDUP(c->opt_host);
396 if (c->opt_have_ip) {
397 *server_ss = c->opt_dest_ip;
398 if (!*server_name) {
399 char addr[INET6_ADDRSTRLEN];
400 print_sockaddr(addr, sizeof(addr), &c->opt_dest_ip);
401 *server_name = SMB_STRDUP(addr);
403 } else if (*server_name) {
404 /* resolve the IP address */
405 if (!resolve_name(*server_name, server_ss, 0x20, false)) {
406 DEBUG(1,("Unable to resolve server name\n"));
407 return false;
409 } else if (flags & NET_FLAGS_PDC) {
410 fstring dc_name;
411 struct sockaddr_storage pdc_ss;
413 if (!get_pdc_ip(d, &pdc_ss)) {
414 DEBUG(1,("Unable to resolve PDC server address\n"));
415 return false;
418 if (is_zero_addr((struct sockaddr *)&pdc_ss)) {
419 return false;
422 if (!name_status_find(d, 0x1b, 0x20, &pdc_ss, dc_name)) {
423 return false;
426 *server_name = SMB_STRDUP(dc_name);
427 *server_ss = pdc_ss;
428 } else if (flags & NET_FLAGS_DMB) {
429 struct sockaddr_storage msbrow_ss;
430 char addr[INET6_ADDRSTRLEN];
432 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1, false)) */
433 if (!resolve_name(d, &msbrow_ss, 0x1B, false)) {
434 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
435 return false;
437 *server_ss = msbrow_ss;
438 print_sockaddr(addr, sizeof(addr), server_ss);
439 *server_name = SMB_STRDUP(addr);
440 } else if (flags & NET_FLAGS_MASTER) {
441 struct sockaddr_storage brow_ss;
442 char addr[INET6_ADDRSTRLEN];
443 if (!resolve_name(d, &brow_ss, 0x1D, false)) {
444 /* go looking for workgroups */
445 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
446 return false;
448 *server_ss = brow_ss;
449 print_sockaddr(addr, sizeof(addr), server_ss);
450 *server_name = SMB_STRDUP(addr);
451 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
452 if (!interpret_string_addr(server_ss,
453 "127.0.0.1", AI_NUMERICHOST)) {
454 DEBUG(1,("Unable to resolve 127.0.0.1\n"));
455 return false;
457 *server_name = SMB_STRDUP("127.0.0.1");
460 if (!*server_name) {
461 DEBUG(1,("no server to connect to\n"));
462 return false;
465 return true;
468 bool net_find_pdc(struct sockaddr_storage *server_ss,
469 fstring server_name,
470 const char *domain_name)
472 if (!get_pdc_ip(domain_name, server_ss)) {
473 return false;
475 if (is_zero_addr((struct sockaddr *)server_ss)) {
476 return false;
479 if (!name_status_find(domain_name, 0x1b, 0x20, server_ss, server_name)) {
480 return false;
483 return true;
486 NTSTATUS net_make_ipc_connection(struct net_context *c, unsigned flags,
487 struct cli_state **pcli)
489 return net_make_ipc_connection_ex(c, c->opt_workgroup, NULL, NULL, flags, pcli);
492 NTSTATUS net_make_ipc_connection_ex(struct net_context *c ,const char *domain,
493 const char *server,
494 struct sockaddr_storage *pss,
495 unsigned flags, struct cli_state **pcli)
497 char *server_name = NULL;
498 struct sockaddr_storage server_ss;
499 struct cli_state *cli = NULL;
500 NTSTATUS nt_status;
502 if ( !server || !pss ) {
503 if (!net_find_server(c, domain, flags, &server_ss,
504 &server_name)) {
505 d_fprintf(stderr, _("Unable to find a suitable server "
506 "for domain %s\n"), domain);
507 nt_status = NT_STATUS_UNSUCCESSFUL;
508 goto done;
510 } else {
511 server_name = SMB_STRDUP( server );
512 server_ss = *pss;
515 if (flags & NET_FLAGS_ANONYMOUS) {
516 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
517 server_name);
518 } else {
519 nt_status = connect_to_ipc(c, &cli, &server_ss,
520 server_name);
523 /* store the server in the affinity cache if it was a PDC */
525 if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
526 saf_store( cli->server_domain, cli->desthost );
528 SAFE_FREE(server_name);
529 if (!NT_STATUS_IS_OK(nt_status)) {
530 d_fprintf(stderr, _("Connection failed: %s\n"),
531 nt_errstr(nt_status));
532 cli = NULL;
533 } else if (c->opt_request_timeout) {
534 cli_set_timeout(cli, c->opt_request_timeout * 1000);
537 done:
538 if (pcli != NULL) {
539 *pcli = cli;
541 return nt_status;
544 /****************************************************************************
545 ****************************************************************************/
547 const char *net_prompt_pass(struct net_context *c, const char *user)
549 char *prompt = NULL;
550 const char *pass = NULL;
552 if (c->opt_password) {
553 return c->opt_password;
556 if (c->opt_machine_pass) {
557 return NULL;
560 if (c->opt_kerberos && !c->opt_user_specified) {
561 return NULL;
564 if (asprintf(&prompt, _("Enter %s's password:"), user) == -1) {
565 return NULL;
568 pass = getpass(prompt);
569 SAFE_FREE(prompt);
571 return pass;
574 int net_run_function(struct net_context *c, int argc, const char **argv,
575 const char *whoami, struct functable *table)
577 int i;
579 if (argc != 0) {
580 for (i=0; table[i].funcname != NULL; i++) {
581 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
582 return table[i].fn(c, argc-1, argv+1);
586 if (c->display_usage == false) {
587 d_fprintf(stderr, _("Invalid command: %s %s\n"), whoami,
588 (argc > 0)?argv[0]:"");
590 d_printf(_("Usage:\n"));
591 for (i=0; table[i].funcname != NULL; i++) {
592 if(c->display_usage == false)
593 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
594 _(table[i].description));
595 else
596 d_printf("%s\n", _(table[i].usage));
599 return c->display_usage?0:-1;
602 void net_display_usage_from_functable(struct functable *table)
604 int i;
605 for (i=0; table[i].funcname != NULL; i++) {
606 d_printf("%s\n", _(table[i].usage));
610 const char *net_share_type_str(int num_type)
612 switch(num_type) {
613 case 0: return _("Disk");
614 case 1: return _("Print");
615 case 2: return _("Dev");
616 case 3: return _("IPC");
617 default: return _("Unknown");
621 NTSTATUS net_scan_dc(struct net_context *c,
622 struct cli_state *cli,
623 struct net_dc_info *dc_info)
625 TALLOC_CTX *mem_ctx = talloc_tos();
626 struct rpc_pipe_client *dssetup_pipe = NULL;
627 union dssetup_DsRoleInfo info;
628 NTSTATUS status;
630 ZERO_STRUCTP(dc_info);
632 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_dssetup.syntax_id,
633 &dssetup_pipe);
634 if (!NT_STATUS_IS_OK(status)) {
635 return status;
638 status = rpccli_dssetup_DsRoleGetPrimaryDomainInformation(dssetup_pipe, mem_ctx,
639 DS_ROLE_BASIC_INFORMATION,
640 &info,
641 NULL);
642 TALLOC_FREE(dssetup_pipe);
644 if (!NT_STATUS_IS_OK(status)) {
645 return status;
648 dc_info->is_dc = (info.basic.role & (DS_ROLE_PRIMARY_DC|DS_ROLE_BACKUP_DC));
649 dc_info->is_pdc = (info.basic.role & DS_ROLE_PRIMARY_DC);
650 dc_info->is_ad = (info.basic.flags & DS_ROLE_PRIMARY_DS_RUNNING);
651 dc_info->is_mixed_mode = (info.basic.flags & DS_ROLE_PRIMARY_DS_MIXED_MODE);
652 dc_info->netbios_domain_name = talloc_strdup(mem_ctx, info.basic.domain);
653 dc_info->dns_domain_name = talloc_strdup(mem_ctx, info.basic.dns_domain);
654 dc_info->forest_name = talloc_strdup(mem_ctx, info.basic.forest);
656 return NT_STATUS_OK;