CVE-2013-4408:s3:Ensure LookupNames replies arrays are range checked.
[Samba.git] / source3 / utils / net_rpc.c
blob6a49d673ee2bc178ef9cfcfde2e6e5f135632864
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "includes.h"
24 #include "utils/net.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/cli_samr.h"
29 #include "rpc_client/init_samr.h"
30 #include "../librpc/gen_ndr/ndr_lsa_c.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
34 #include "../librpc/gen_ndr/ndr_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
36 #include "../librpc/gen_ndr/ndr_winreg_c.h"
37 #include "secrets.h"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "rpc_client/init_lsa.h"
41 #include "../libcli/security/security.h"
42 #include "libsmb/libsmb.h"
43 #include "libsmb/clirap.h"
44 #include "nsswitch/libwbclient/wbclient.h"
45 #include "passdb.h"
46 #include "../libcli/smb/smbXcli_base.h"
48 static int net_mode_share;
49 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
51 /**
52 * @file net_rpc.c
54 * @brief RPC based subcommands for the 'net' utility.
56 * This file should contain much of the functionality that used to
57 * be found in rpcclient, execpt that the commands should change
58 * less often, and the fucntionality should be sane (the user is not
59 * expected to know a rid/sid before they conduct an operation etc.)
61 * @todo Perhaps eventually these should be split out into a number
62 * of files, as this could get quite big.
63 **/
66 /**
67 * Many of the RPC functions need the domain sid. This function gets
68 * it at the start of every run
70 * @param cli A cli_state already connected to the remote machine
72 * @return The Domain SID of the remote machine.
73 **/
75 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
76 struct dom_sid **domain_sid,
77 const char **domain_name)
79 struct rpc_pipe_client *lsa_pipe = NULL;
80 struct policy_handle pol;
81 NTSTATUS status, result;
82 union lsa_PolicyInformation *info = NULL;
83 struct dcerpc_binding_handle *b;
85 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
86 &lsa_pipe);
87 if (!NT_STATUS_IS_OK(status)) {
88 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
89 return status;
92 b = lsa_pipe->binding_handle;
94 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
95 SEC_FLAG_MAXIMUM_ALLOWED,
96 &pol);
97 if (!NT_STATUS_IS_OK(status)) {
98 d_fprintf(stderr, "open_policy %s: %s\n",
99 _("failed"),
100 nt_errstr(status));
101 return status;
104 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
105 &pol,
106 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
107 &info,
108 &result);
109 if (!NT_STATUS_IS_OK(status)) {
110 d_fprintf(stderr, "lsaquery %s: %s\n",
111 _("failed"),
112 nt_errstr(status));
113 return status;
115 if (!NT_STATUS_IS_OK(result)) {
116 d_fprintf(stderr, "lsaquery %s: %s\n",
117 _("failed"),
118 nt_errstr(result));
119 return result;
122 *domain_name = info->account_domain.name.string;
123 *domain_sid = info->account_domain.sid;
125 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
126 TALLOC_FREE(lsa_pipe);
128 return NT_STATUS_OK;
132 * Run a single RPC command, from start to finish.
134 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
135 * @param conn_flag a NET_FLAG_ combination. Passed to
136 * net_make_ipc_connection.
137 * @param argc Standard main() style argc.
138 * @param argv Standard main() style argv. Initial components are already
139 * stripped.
140 * @return A shell status integer (0 for success).
143 int run_rpc_command(struct net_context *c,
144 struct cli_state *cli_arg,
145 const struct ndr_interface_table *table,
146 int conn_flags,
147 rpc_command_fn fn,
148 int argc,
149 const char **argv)
151 struct cli_state *cli = NULL;
152 struct rpc_pipe_client *pipe_hnd = NULL;
153 TALLOC_CTX *mem_ctx;
154 NTSTATUS nt_status;
155 struct dom_sid *domain_sid;
156 const char *domain_name;
157 int ret = -1;
159 /* make use of cli_state handed over as an argument, if possible */
160 if (!cli_arg) {
161 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
162 if (!NT_STATUS_IS_OK(nt_status)) {
163 DEBUG(1, ("failed to make ipc connection: %s\n",
164 nt_errstr(nt_status)));
165 return -1;
167 } else {
168 cli = cli_arg;
171 if (!cli) {
172 return -1;
175 /* Create mem_ctx */
177 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
178 DEBUG(0, ("talloc_init() failed\n"));
179 goto fail;
182 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
183 &domain_name);
184 if (!NT_STATUS_IS_OK(nt_status)) {
185 goto fail;
188 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
189 if (lp_client_schannel()
190 && (ndr_syntax_id_equal(&table->syntax_id,
191 &ndr_table_netlogon.syntax_id))) {
192 /* Always try and create an schannel netlogon pipe. */
193 nt_status = cli_rpc_pipe_open_schannel(
194 cli, &table->syntax_id, NCACN_NP,
195 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
196 &pipe_hnd);
197 if (!NT_STATUS_IS_OK(nt_status)) {
198 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
199 nt_errstr(nt_status) ));
200 goto fail;
202 } else {
203 if (conn_flags & NET_FLAGS_SEAL) {
204 nt_status = cli_rpc_pipe_open_generic_auth(
205 cli, table,
206 (conn_flags & NET_FLAGS_TCP) ?
207 NCACN_IP_TCP : NCACN_NP,
208 DCERPC_AUTH_TYPE_NTLMSSP,
209 DCERPC_AUTH_LEVEL_PRIVACY,
210 smbXcli_conn_remote_name(cli->conn),
211 lp_workgroup(), c->opt_user_name,
212 c->opt_password, &pipe_hnd);
213 } else {
214 nt_status = cli_rpc_pipe_open_noauth(
215 cli, &table->syntax_id,
216 &pipe_hnd);
218 if (!NT_STATUS_IS_OK(nt_status)) {
219 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
220 table->name,
221 nt_errstr(nt_status) ));
222 goto fail;
227 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
229 if (!NT_STATUS_IS_OK(nt_status)) {
230 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
231 } else {
232 ret = 0;
233 DEBUG(5, ("rpc command function succedded\n"));
236 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
237 if (pipe_hnd) {
238 TALLOC_FREE(pipe_hnd);
242 fail:
243 /* close the connection only if it was opened here */
244 if (!cli_arg) {
245 cli_shutdown(cli);
248 talloc_destroy(mem_ctx);
249 return ret;
253 * Force a change of the trust acccount password.
255 * All parameters are provided by the run_rpc_command function, except for
256 * argc, argv which are passed through.
258 * @param domain_sid The domain sid acquired from the remote server.
259 * @param cli A cli_state connected to the server.
260 * @param mem_ctx Talloc context, destroyed on completion of the function.
261 * @param argc Standard main() style argc.
262 * @param argv Standard main() style argv. Initial components are already
263 * stripped.
265 * @return Normal NTSTATUS return.
268 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
269 const struct dom_sid *domain_sid,
270 const char *domain_name,
271 struct cli_state *cli,
272 struct rpc_pipe_client *pipe_hnd,
273 TALLOC_CTX *mem_ctx,
274 int argc,
275 const char **argv)
277 NTSTATUS status;
279 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
280 if (!NT_STATUS_IS_OK(status)) {
281 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
282 nt_errstr(status));
283 return status;
286 return NT_STATUS_OK;
290 * Force a change of the trust acccount password.
292 * @param argc Standard main() style argc.
293 * @param argv Standard main() style argv. Initial components are already
294 * stripped.
296 * @return A shell status integer (0 for success).
299 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
301 if (c->display_usage) {
302 d_printf( "%s\n"
303 "net rpc changetrustpw\n"
304 " %s\n",
305 _("Usage:"),
306 _("Change the machine trust password"));
307 return 0;
310 return run_rpc_command(c, NULL, &ndr_table_netlogon,
311 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
312 rpc_changetrustpw_internals,
313 argc, argv);
317 * Join a domain, the old way.
319 * This uses 'machinename' as the inital password, and changes it.
321 * The password should be created with 'server manager' or equiv first.
323 * All parameters are provided by the run_rpc_command function, except for
324 * argc, argv which are passed through.
326 * @param domain_sid The domain sid acquired from the remote server.
327 * @param cli A cli_state connected to the server.
328 * @param mem_ctx Talloc context, destroyed on completion of the function.
329 * @param argc Standard main() style argc.
330 * @param argv Standard main() style argv. Initial components are already
331 * stripped.
333 * @return Normal NTSTATUS return.
336 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
337 const struct dom_sid *domain_sid,
338 const char *domain_name,
339 struct cli_state *cli,
340 struct rpc_pipe_client *pipe_hnd,
341 TALLOC_CTX *mem_ctx,
342 int argc,
343 const char **argv)
346 fstring trust_passwd;
347 unsigned char orig_trust_passwd_hash[16];
348 NTSTATUS result;
349 enum netr_SchannelType sec_channel_type;
351 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
352 &pipe_hnd);
353 if (!NT_STATUS_IS_OK(result)) {
354 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
355 "error was %s\n",
356 smbXcli_conn_remote_name(cli->conn),
357 nt_errstr(result) ));
358 return result;
362 check what type of join - if the user want's to join as
363 a BDC, the server must agree that we are a BDC.
365 if (argc >= 0) {
366 sec_channel_type = get_sec_channel_type(argv[0]);
367 } else {
368 sec_channel_type = get_sec_channel_type(NULL);
371 fstrcpy(trust_passwd, lp_netbios_name());
372 strlower_m(trust_passwd);
375 * Machine names can be 15 characters, but the max length on
376 * a password is 14. --jerry
379 trust_passwd[14] = '\0';
381 E_md4hash(trust_passwd, orig_trust_passwd_hash);
383 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
384 lp_netbios_name(),
385 orig_trust_passwd_hash,
386 sec_channel_type);
388 if (NT_STATUS_IS_OK(result))
389 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
392 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
393 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
394 result = NT_STATUS_UNSUCCESSFUL;
397 return result;
401 * Join a domain, the old way.
403 * @param argc Standard main() style argc.
404 * @param argv Standard main() style argv. Initial components are already
405 * stripped.
407 * @return A shell status integer (0 for success).
410 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
412 return run_rpc_command(c, NULL, &ndr_table_netlogon,
413 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
414 rpc_oldjoin_internals,
415 argc, argv);
419 * Join a domain, the old way. This function exists to allow
420 * the message to be displayed when oldjoin was explicitly
421 * requested, but not when it was implied by "net rpc join".
423 * @param argc Standard main() style argc.
424 * @param argv Standard main() style argv. Initial components are already
425 * stripped.
427 * @return A shell status integer (0 for success).
430 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
432 int rc = -1;
434 if (c->display_usage) {
435 d_printf( "%s\n"
436 "net rpc oldjoin\n"
437 " %s\n",
438 _("Usage:"),
439 _("Join a domain the old way"));
440 return 0;
443 rc = net_rpc_perform_oldjoin(c, argc, argv);
445 if (rc) {
446 d_fprintf(stderr, _("Failed to join domain\n"));
449 return rc;
453 * 'net rpc join' entrypoint.
454 * @param argc Standard main() style argc.
455 * @param argv Standard main() style argv. Initial components are already
456 * stripped
458 * Main 'net_rpc_join()' (where the admin username/password is used) is
459 * in net_rpc_join.c.
460 * Try to just change the password, but if that doesn't work, use/prompt
461 * for a username/password.
464 int net_rpc_join(struct net_context *c, int argc, const char **argv)
466 if (c->display_usage) {
467 d_printf("%s\n%s",
468 _("Usage:"),
469 _("net rpc join -U <username>[%%password] <type>\n"
470 " Join a domain\n"
471 " username\tName of the admin user"
472 " password\tPassword of the admin user, will "
473 "prompt if not specified\n"
474 " type\tCan be one of the following:\n"
475 "\t\tMEMBER\tJoin as member server (default)\n"
476 "\t\tBDC\tJoin as BDC\n"
477 "\t\tPDC\tJoin as PDC\n"));
478 return 0;
481 if (lp_server_role() == ROLE_STANDALONE) {
482 d_printf(_("cannot join as standalone machine\n"));
483 return -1;
486 if (strlen(lp_netbios_name()) > 15) {
487 d_printf(_("Our netbios name can be at most 15 chars long, "
488 "\"%s\" is %u chars long\n"),
489 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
490 return -1;
493 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
494 return 0;
496 return net_rpc_join_newstyle(c, argc, argv);
500 * display info about a rpc domain
502 * All parameters are provided by the run_rpc_command function, except for
503 * argc, argv which are passed through.
505 * @param domain_sid The domain sid acquired from the remote server
506 * @param cli A cli_state connected to the server.
507 * @param mem_ctx Talloc context, destroyed on completion of the function.
508 * @param argc Standard main() style argc.
509 * @param argv Standard main() style argv. Initial components are already
510 * stripped.
512 * @return Normal NTSTATUS return.
515 NTSTATUS rpc_info_internals(struct net_context *c,
516 const struct dom_sid *domain_sid,
517 const char *domain_name,
518 struct cli_state *cli,
519 struct rpc_pipe_client *pipe_hnd,
520 TALLOC_CTX *mem_ctx,
521 int argc,
522 const char **argv)
524 struct policy_handle connect_pol, domain_pol;
525 NTSTATUS status, result;
526 union samr_DomainInfo *info = NULL;
527 fstring sid_str;
528 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
530 sid_to_fstring(sid_str, domain_sid);
532 /* Get sam policy handle */
533 status = dcerpc_samr_Connect2(b, mem_ctx,
534 pipe_hnd->desthost,
535 MAXIMUM_ALLOWED_ACCESS,
536 &connect_pol,
537 &result);
538 if (!NT_STATUS_IS_OK(status)) {
539 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
540 nt_errstr(status));
541 goto done;
544 if (!NT_STATUS_IS_OK(result)) {
545 status = result;
546 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
547 nt_errstr(result));
548 goto done;
551 /* Get domain policy handle */
552 status = dcerpc_samr_OpenDomain(b, mem_ctx,
553 &connect_pol,
554 MAXIMUM_ALLOWED_ACCESS,
555 discard_const_p(struct dom_sid2, domain_sid),
556 &domain_pol,
557 &result);
558 if (!NT_STATUS_IS_OK(status)) {
559 d_fprintf(stderr, _("Could not open domain: %s\n"),
560 nt_errstr(status));
561 goto done;
563 if (!NT_STATUS_IS_OK(result)) {
564 status = result;
565 d_fprintf(stderr, _("Could not open domain: %s\n"),
566 nt_errstr(result));
567 goto done;
570 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
571 &domain_pol,
573 &info,
574 &result);
575 if (!NT_STATUS_IS_OK(status)) {
576 goto done;
578 status = result;
579 if (NT_STATUS_IS_OK(result)) {
580 d_printf(_("Domain Name: %s\n"),
581 info->general.domain_name.string);
582 d_printf(_("Domain SID: %s\n"), sid_str);
583 d_printf(_("Sequence number: %llu\n"),
584 (unsigned long long)info->general.sequence_num);
585 d_printf(_("Num users: %u\n"), info->general.num_users);
586 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
587 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
590 done:
591 return status;
595 * 'net rpc info' entrypoint.
596 * @param argc Standard main() style argc.
597 * @param argv Standard main() style argv. Initial components are already
598 * stripped.
601 int net_rpc_info(struct net_context *c, int argc, const char **argv)
603 if (c->display_usage) {
604 d_printf( "%s\n"
605 "net rpc info\n"
606 " %s\n",
607 _("Usage:"),
608 _("Display information about the domain"));
609 return 0;
612 return run_rpc_command(c, NULL, &ndr_table_samr,
613 NET_FLAGS_PDC, rpc_info_internals,
614 argc, argv);
618 * Fetch domain SID into the local secrets.tdb.
620 * All parameters are provided by the run_rpc_command function, except for
621 * argc, argv which are passed through.
623 * @param domain_sid The domain sid acquired from the remote server.
624 * @param cli A cli_state connected to the server.
625 * @param mem_ctx Talloc context, destroyed on completion of the function.
626 * @param argc Standard main() style argc.
627 * @param argv Standard main() style argv. Initial components are already
628 * stripped.
630 * @return Normal NTSTATUS return.
633 static NTSTATUS rpc_getsid_internals(struct net_context *c,
634 const struct dom_sid *domain_sid,
635 const char *domain_name,
636 struct cli_state *cli,
637 struct rpc_pipe_client *pipe_hnd,
638 TALLOC_CTX *mem_ctx,
639 int argc,
640 const char **argv)
642 fstring sid_str;
644 sid_to_fstring(sid_str, domain_sid);
645 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
646 sid_str, domain_name);
648 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
649 DEBUG(0,("Can't store domain SID\n"));
650 return NT_STATUS_UNSUCCESSFUL;
653 return NT_STATUS_OK;
657 * 'net rpc getsid' entrypoint.
658 * @param argc Standard main() style argc.
659 * @param argv Standard main() style argv. Initial components are already
660 * stripped.
663 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
665 int conn_flags = NET_FLAGS_PDC;
667 if (!c->opt_user_specified) {
668 conn_flags |= NET_FLAGS_ANONYMOUS;
671 if (c->display_usage) {
672 d_printf( "%s\n"
673 "net rpc getsid\n"
674 " %s\n",
675 _("Usage:"),
676 _("Fetch domain SID into local secrets.tdb"));
677 return 0;
680 return run_rpc_command(c, NULL, &ndr_table_samr,
681 conn_flags,
682 rpc_getsid_internals,
683 argc, argv);
686 /****************************************************************************/
689 * Basic usage function for 'net rpc user'.
690 * @param argc Standard main() style argc.
691 * @param argv Standard main() style argv. Initial components are already
692 * stripped.
695 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
697 return net_user_usage(c, argc, argv);
701 * Add a new user to a remote RPC server.
703 * @param argc Standard main() style argc.
704 * @param argv Standard main() style argv. Initial components are already
705 * stripped.
707 * @return A shell status integer (0 for success).
710 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
712 NET_API_STATUS status;
713 struct USER_INFO_1 info1;
714 uint32_t parm_error = 0;
716 if (argc < 1 || c->display_usage) {
717 rpc_user_usage(c, argc, argv);
718 return 0;
721 ZERO_STRUCT(info1);
723 info1.usri1_name = argv[0];
724 if (argc == 2) {
725 info1.usri1_password = argv[1];
728 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
730 if (status != 0) {
731 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
732 argv[0], libnetapi_get_error_string(c->netapi_ctx,
733 status));
734 return -1;
735 } else {
736 d_printf(_("Added user '%s'.\n"), argv[0]);
739 return 0;
743 * Rename a user on a remote RPC server.
745 * @param argc Standard main() style argc.
746 * @param argv Standard main() style argv. Initial components are already
747 * stripped.
749 * @return A shell status integer (0 for success).
752 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
754 NET_API_STATUS status;
755 struct USER_INFO_0 u0;
756 uint32_t parm_err = 0;
758 if (argc != 2 || c->display_usage) {
759 rpc_user_usage(c, argc, argv);
760 return 0;
763 u0.usri0_name = argv[1];
765 status = NetUserSetInfo(c->opt_host, argv[0],
766 0, (uint8_t *)&u0, &parm_err);
767 if (status) {
768 d_fprintf(stderr,
769 _("Failed to rename user from %s to %s - %s\n"),
770 argv[0], argv[1],
771 libnetapi_get_error_string(c->netapi_ctx, status));
772 } else {
773 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
776 return status;
780 * Set a user's primary group
782 * @param argc Standard main() style argc.
783 * @param argv Standard main() style argv. Initial components are already
784 * stripped.
786 * @return A shell status integer (0 for success).
789 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
790 const char **argv)
792 NET_API_STATUS status;
793 uint8_t *buffer;
794 struct GROUP_INFO_2 *g2;
795 struct USER_INFO_1051 u1051;
796 uint32_t parm_err = 0;
798 if (argc != 2 || c->display_usage) {
799 rpc_user_usage(c, argc, argv);
800 return 0;
803 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
804 if (status) {
805 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
806 argv[1],
807 libnetapi_get_error_string(c->netapi_ctx, status));
808 return status;
810 g2 = (struct GROUP_INFO_2 *)buffer;
812 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
814 NetApiBufferFree(buffer);
816 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
817 (uint8_t *)&u1051, &parm_err);
818 if (status) {
819 d_fprintf(stderr,
820 _("Failed to set user's primary group %s to %s - "
821 "%s\n"), argv[0], argv[1],
822 libnetapi_get_error_string(c->netapi_ctx, status));
823 } else {
824 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
825 argv[1]);
827 return status;
831 * Delete a user from a remote RPC server.
833 * @param argc Standard main() style argc.
834 * @param argv Standard main() style argv. Initial components are already
835 * stripped.
837 * @return A shell status integer (0 for success).
840 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
842 NET_API_STATUS status;
844 if (argc < 1 || c->display_usage) {
845 rpc_user_usage(c, argc, argv);
846 return 0;
849 status = NetUserDel(c->opt_host, argv[0]);
851 if (status != 0) {
852 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
853 argv[0],
854 libnetapi_get_error_string(c->netapi_ctx, status));
855 return -1;
856 } else {
857 d_printf(_("Deleted user '%s'.\n"), argv[0]);
860 return 0;
864 * Set a user's password on a remote RPC server.
866 * @param argc Standard main() style argc.
867 * @param argv Standard main() style argv. Initial components are already
868 * stripped.
870 * @return A shell status integer (0 for success).
873 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
875 NET_API_STATUS status;
876 char *prompt = NULL;
877 struct USER_INFO_1003 u1003;
878 uint32_t parm_err = 0;
879 int ret;
881 if (argc < 1 || c->display_usage) {
882 rpc_user_usage(c, argc, argv);
883 return 0;
886 if (argv[1]) {
887 u1003.usri1003_password = argv[1];
888 } else {
889 ret = asprintf(&prompt, _("Enter new password for %s:"),
890 argv[0]);
891 if (ret == -1) {
892 return -1;
894 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
895 SAFE_FREE(prompt);
896 if (u1003.usri1003_password == NULL) {
897 return -1;
901 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
903 /* Display results */
904 if (status != 0) {
905 d_fprintf(stderr,
906 _("Failed to set password for '%s' with error: %s.\n"),
907 argv[0], libnetapi_get_error_string(c->netapi_ctx,
908 status));
909 return -1;
912 return 0;
916 * List a user's groups from a remote RPC server.
918 * @param argc Standard main() style argc.
919 * @param argv Standard main() style argv. Initial components are already
920 * stripped.
922 * @return A shell status integer (0 for success)
925 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
928 NET_API_STATUS status;
929 struct GROUP_USERS_INFO_0 *u0 = NULL;
930 uint32_t entries_read = 0;
931 uint32_t total_entries = 0;
932 int i;
935 if (argc < 1 || c->display_usage) {
936 rpc_user_usage(c, argc, argv);
937 return 0;
940 status = NetUserGetGroups(c->opt_host,
941 argv[0],
943 (uint8_t **)(void *)&u0,
944 (uint32_t)-1,
945 &entries_read,
946 &total_entries);
947 if (status != 0) {
948 d_fprintf(stderr,
949 _("Failed to get groups for '%s' with error: %s.\n"),
950 argv[0], libnetapi_get_error_string(c->netapi_ctx,
951 status));
952 return -1;
955 for (i=0; i < entries_read; i++) {
956 printf("%s\n", u0->grui0_name);
957 u0++;
960 return 0;
964 * List users on a remote RPC server.
966 * All parameters are provided by the run_rpc_command function, except for
967 * argc, argv which are passed through.
969 * @param domain_sid The domain sid acquired from the remote server.
970 * @param cli A cli_state connected to the server.
971 * @param mem_ctx Talloc context, destroyed on completion of the function.
972 * @param argc Standard main() style argc.
973 * @param argv Standard main() style argv. Initial components are already
974 * stripped.
976 * @return Normal NTSTATUS return.
979 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
981 NET_API_STATUS status;
982 uint32_t start_idx=0, num_entries, i, loop_count = 0;
983 struct NET_DISPLAY_USER *info = NULL;
984 void *buffer = NULL;
986 /* Query domain users */
987 if (c->opt_long_list_entries)
988 d_printf(_("\nUser name Comment"
989 "\n-----------------------------\n"));
990 do {
991 uint32_t max_entries, max_size;
993 dcerpc_get_query_dispinfo_params(
994 loop_count, &max_entries, &max_size);
996 status = NetQueryDisplayInformation(c->opt_host,
998 start_idx,
999 max_entries,
1000 max_size,
1001 &num_entries,
1002 &buffer);
1003 if (status != 0 && status != ERROR_MORE_DATA) {
1004 return status;
1007 info = (struct NET_DISPLAY_USER *)buffer;
1009 for (i = 0; i < num_entries; i++) {
1011 if (c->opt_long_list_entries)
1012 printf("%-21.21s %s\n", info->usri1_name,
1013 info->usri1_comment);
1014 else
1015 printf("%s\n", info->usri1_name);
1016 info++;
1019 NetApiBufferFree(buffer);
1021 loop_count++;
1022 start_idx += num_entries;
1024 } while (status == ERROR_MORE_DATA);
1026 return status;
1030 * 'net rpc user' entrypoint.
1031 * @param argc Standard main() style argc.
1032 * @param argv Standard main() style argv. Initial components are already
1033 * stripped.
1036 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1038 NET_API_STATUS status;
1040 struct functable func[] = {
1042 "add",
1043 rpc_user_add,
1044 NET_TRANSPORT_RPC,
1045 N_("Add specified user"),
1046 N_("net rpc user add\n"
1047 " Add specified user")
1050 "info",
1051 rpc_user_info,
1052 NET_TRANSPORT_RPC,
1053 N_("List domain groups of user"),
1054 N_("net rpc user info\n"
1055 " List domain groups of user")
1058 "delete",
1059 rpc_user_delete,
1060 NET_TRANSPORT_RPC,
1061 N_("Remove specified user"),
1062 N_("net rpc user delete\n"
1063 " Remove specified user")
1066 "password",
1067 rpc_user_password,
1068 NET_TRANSPORT_RPC,
1069 N_("Change user password"),
1070 N_("net rpc user password\n"
1071 " Change user password")
1074 "rename",
1075 rpc_user_rename,
1076 NET_TRANSPORT_RPC,
1077 N_("Rename specified user"),
1078 N_("net rpc user rename\n"
1079 " Rename specified user")
1082 "setprimarygroup",
1083 rpc_user_setprimarygroup,
1084 NET_TRANSPORT_RPC,
1085 "Set a user's primary group",
1086 "net rpc user setprimarygroup\n"
1087 " Set a user's primary group"
1089 {NULL, NULL, 0, NULL, NULL}
1092 status = libnetapi_net_init(&c->netapi_ctx);
1093 if (status != 0) {
1094 return -1;
1096 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1097 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1098 if (c->opt_kerberos) {
1099 libnetapi_set_use_kerberos(c->netapi_ctx);
1102 if (argc == 0) {
1103 if (c->display_usage) {
1104 d_printf( "%s\n"
1105 "net rpc user\n"
1106 " %s\n",
1107 _("Usage:"),
1108 _("List all users"));
1109 net_display_usage_from_functable(func);
1110 return 0;
1113 return rpc_user_list(c, argc, argv);
1116 return net_run_function(c, argc, argv, "net rpc user", func);
1119 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1120 TALLOC_CTX *mem_ctx,
1121 struct rpc_sh_ctx *ctx,
1122 struct rpc_pipe_client *pipe_hnd,
1123 int argc, const char **argv)
1125 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1128 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1129 TALLOC_CTX *mem_ctx,
1130 struct rpc_sh_ctx *ctx,
1131 struct rpc_pipe_client *pipe_hnd,
1132 int argc, const char **argv)
1134 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1137 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1138 TALLOC_CTX *mem_ctx,
1139 struct rpc_sh_ctx *ctx,
1140 struct rpc_pipe_client *pipe_hnd,
1141 int argc, const char **argv,
1142 NTSTATUS (*fn)(
1143 struct net_context *c,
1144 TALLOC_CTX *mem_ctx,
1145 struct rpc_sh_ctx *ctx,
1146 struct rpc_pipe_client *pipe_hnd,
1147 struct policy_handle *user_hnd,
1148 int argc, const char **argv))
1150 struct policy_handle connect_pol, domain_pol, user_pol;
1151 NTSTATUS status, result;
1152 struct dom_sid sid;
1153 uint32 rid;
1154 enum lsa_SidType type;
1155 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1157 if (argc == 0) {
1158 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1159 ctx->whoami);
1160 return NT_STATUS_INVALID_PARAMETER;
1163 ZERO_STRUCT(connect_pol);
1164 ZERO_STRUCT(domain_pol);
1165 ZERO_STRUCT(user_pol);
1167 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1168 argv[0], NULL, NULL, &sid, &type);
1169 if (!NT_STATUS_IS_OK(status)) {
1170 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1171 nt_errstr(status));
1172 goto done;
1175 if (type != SID_NAME_USER) {
1176 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1177 sid_type_lookup(type));
1178 status = NT_STATUS_NO_SUCH_USER;
1179 goto done;
1182 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1183 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1184 status = NT_STATUS_NO_SUCH_USER;
1185 goto done;
1188 status = dcerpc_samr_Connect2(b, mem_ctx,
1189 pipe_hnd->desthost,
1190 MAXIMUM_ALLOWED_ACCESS,
1191 &connect_pol,
1192 &result);
1193 if (!NT_STATUS_IS_OK(status)) {
1194 goto done;
1196 if (!NT_STATUS_IS_OK(result)) {
1197 status = result;
1198 goto done;
1201 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1202 &connect_pol,
1203 MAXIMUM_ALLOWED_ACCESS,
1204 ctx->domain_sid,
1205 &domain_pol,
1206 &result);
1207 if (!NT_STATUS_IS_OK(status)) {
1208 goto done;
1210 if (!NT_STATUS_IS_OK(result)) {
1211 status = result;
1212 goto done;
1215 status = dcerpc_samr_OpenUser(b, mem_ctx,
1216 &domain_pol,
1217 MAXIMUM_ALLOWED_ACCESS,
1218 rid,
1219 &user_pol,
1220 &result);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 goto done;
1224 if (!NT_STATUS_IS_OK(result)) {
1225 status = result;
1226 goto done;
1229 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1231 done:
1232 if (is_valid_policy_hnd(&user_pol)) {
1233 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1235 if (is_valid_policy_hnd(&domain_pol)) {
1236 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1238 if (is_valid_policy_hnd(&connect_pol)) {
1239 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1241 return status;
1244 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1245 TALLOC_CTX *mem_ctx,
1246 struct rpc_sh_ctx *ctx,
1247 struct rpc_pipe_client *pipe_hnd,
1248 struct policy_handle *user_hnd,
1249 int argc, const char **argv)
1251 NTSTATUS status, result;
1252 union samr_UserInfo *info = NULL;
1253 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1255 if (argc != 0) {
1256 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1257 ctx->whoami);
1258 return NT_STATUS_INVALID_PARAMETER;
1261 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1262 user_hnd,
1264 &info,
1265 &result);
1266 if (!NT_STATUS_IS_OK(status)) {
1267 return status;
1269 if (!NT_STATUS_IS_OK(result)) {
1270 return result;
1273 d_printf(_("user rid: %d, group rid: %d\n"),
1274 info->info21.rid,
1275 info->info21.primary_gid);
1277 return result;
1280 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1281 TALLOC_CTX *mem_ctx,
1282 struct rpc_sh_ctx *ctx,
1283 struct rpc_pipe_client *pipe_hnd,
1284 int argc, const char **argv)
1286 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1287 rpc_sh_user_show_internals);
1290 #define FETCHSTR(name, rec) \
1291 do { if (strequal(ctx->thiscmd, name)) { \
1292 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1293 } while (0);
1295 #define SETSTR(name, rec, flag) \
1296 do { if (strequal(ctx->thiscmd, name)) { \
1297 init_lsa_String(&(info->info21.rec), argv[0]); \
1298 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1299 } while (0);
1301 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1302 TALLOC_CTX *mem_ctx,
1303 struct rpc_sh_ctx *ctx,
1304 struct rpc_pipe_client *pipe_hnd,
1305 struct policy_handle *user_hnd,
1306 int argc, const char **argv)
1308 NTSTATUS status, result;
1309 const char *username;
1310 const char *oldval = "";
1311 union samr_UserInfo *info = NULL;
1312 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1314 if (argc > 1) {
1315 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1316 _("Usage:"), ctx->whoami);
1317 return NT_STATUS_INVALID_PARAMETER;
1320 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1321 user_hnd,
1323 &info,
1324 &result);
1325 if (!NT_STATUS_IS_OK(status)) {
1326 return status;
1328 if (!NT_STATUS_IS_OK(result)) {
1329 return result;
1332 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1334 FETCHSTR("fullname", full_name);
1335 FETCHSTR("homedir", home_directory);
1336 FETCHSTR("homedrive", home_drive);
1337 FETCHSTR("logonscript", logon_script);
1338 FETCHSTR("profilepath", profile_path);
1339 FETCHSTR("description", description);
1341 if (argc == 0) {
1342 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1343 goto done;
1346 if (strcmp(argv[0], "NULL") == 0) {
1347 argv[0] = "";
1350 ZERO_STRUCT(info->info21);
1352 SETSTR("fullname", full_name, FULL_NAME);
1353 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1354 SETSTR("homedrive", home_drive, HOME_DRIVE);
1355 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1356 SETSTR("profilepath", profile_path, PROFILE_PATH);
1357 SETSTR("description", description, DESCRIPTION);
1359 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1360 user_hnd,
1362 info,
1363 &result);
1364 if (!NT_STATUS_IS_OK(status)) {
1365 return status;
1368 status = result;
1370 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1371 ctx->thiscmd, oldval, argv[0]);
1373 done:
1375 return status;
1378 #define HANDLEFLG(name, rec) \
1379 do { if (strequal(ctx->thiscmd, name)) { \
1380 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1381 if (newval) { \
1382 newflags = oldflags | ACB_##rec; \
1383 } else { \
1384 newflags = oldflags & ~ACB_##rec; \
1385 } } } while (0);
1387 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1388 TALLOC_CTX *mem_ctx,
1389 struct rpc_sh_ctx *ctx,
1390 struct rpc_pipe_client *pipe_hnd,
1391 int argc, const char **argv)
1393 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1394 rpc_sh_user_str_edit_internals);
1397 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1398 TALLOC_CTX *mem_ctx,
1399 struct rpc_sh_ctx *ctx,
1400 struct rpc_pipe_client *pipe_hnd,
1401 struct policy_handle *user_hnd,
1402 int argc, const char **argv)
1404 NTSTATUS status, result;
1405 const char *username;
1406 const char *oldval = "unknown";
1407 uint32 oldflags, newflags;
1408 bool newval;
1409 union samr_UserInfo *info = NULL;
1410 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1412 if ((argc > 1) ||
1413 ((argc == 1) && !strequal(argv[0], "yes") &&
1414 !strequal(argv[0], "no"))) {
1415 /* TRANSATORS: The yes|no here are program keywords. Please do
1416 not translate. */
1417 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1418 ctx->whoami);
1419 return NT_STATUS_INVALID_PARAMETER;
1422 newval = strequal(argv[0], "yes");
1424 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1425 user_hnd,
1427 &info,
1428 &result);
1429 if (!NT_STATUS_IS_OK(status)) {
1430 return status;
1432 if (!NT_STATUS_IS_OK(result)) {
1433 return result;
1436 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1437 oldflags = info->info21.acct_flags;
1438 newflags = info->info21.acct_flags;
1440 HANDLEFLG("disabled", DISABLED);
1441 HANDLEFLG("pwnotreq", PWNOTREQ);
1442 HANDLEFLG("autolock", AUTOLOCK);
1443 HANDLEFLG("pwnoexp", PWNOEXP);
1445 if (argc == 0) {
1446 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1447 oldval);
1448 goto done;
1451 ZERO_STRUCT(info->info21);
1453 info->info21.acct_flags = newflags;
1454 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1456 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1457 user_hnd,
1459 info,
1460 &result);
1461 if (!NT_STATUS_IS_OK(status)) {
1462 goto done;
1464 status = result;
1465 if (NT_STATUS_IS_OK(result)) {
1466 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1467 ctx->thiscmd, oldval, argv[0]);
1470 done:
1472 return status;
1475 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1476 TALLOC_CTX *mem_ctx,
1477 struct rpc_sh_ctx *ctx,
1478 struct rpc_pipe_client *pipe_hnd,
1479 int argc, const char **argv)
1481 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1482 rpc_sh_user_flag_edit_internals);
1485 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1486 TALLOC_CTX *mem_ctx,
1487 struct rpc_sh_ctx *ctx)
1489 static struct rpc_sh_cmd cmds[] = {
1491 { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1492 N_("Show/Set a user's full name") },
1494 { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1495 N_("Show/Set a user's home directory") },
1497 { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1498 N_("Show/Set a user's home drive") },
1500 { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1501 N_("Show/Set a user's logon script") },
1503 { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1504 N_("Show/Set a user's profile path") },
1506 { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1507 N_("Show/Set a user's description") },
1509 { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1510 N_("Show/Set whether a user is disabled") },
1512 { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1513 N_("Show/Set whether a user locked out") },
1515 { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1516 N_("Show/Set whether a user does not need a password") },
1518 { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1519 N_("Show/Set whether a user's password does not expire") },
1521 { NULL, NULL, 0, NULL, NULL }
1524 return cmds;
1527 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1528 TALLOC_CTX *mem_ctx,
1529 struct rpc_sh_ctx *ctx)
1531 static struct rpc_sh_cmd cmds[] = {
1533 { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
1534 N_("List available users") },
1536 { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
1537 N_("List the domain groups a user is member of") },
1539 { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
1540 N_("Show info about a user") },
1542 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1543 N_("Show/Modify a user's fields") },
1545 { NULL, NULL, 0, NULL, NULL }
1548 return cmds;
1551 /****************************************************************************/
1554 * Basic usage function for 'net rpc group'.
1555 * @param argc Standard main() style argc.
1556 * @param argv Standard main() style argv. Initial components are already
1557 * stripped.
1560 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1562 return net_group_usage(c, argc, argv);
1566 * Delete group on a remote RPC server.
1568 * All parameters are provided by the run_rpc_command function, except for
1569 * argc, argv which are passed through.
1571 * @param domain_sid The domain sid acquired from the remote server.
1572 * @param cli A cli_state connected to the server.
1573 * @param mem_ctx Talloc context, destroyed on completion of the function.
1574 * @param argc Standard main() style argc.
1575 * @param argv Standard main() style argv. Initial components are already
1576 * stripped.
1578 * @return Normal NTSTATUS return.
1581 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1582 const struct dom_sid *domain_sid,
1583 const char *domain_name,
1584 struct cli_state *cli,
1585 struct rpc_pipe_client *pipe_hnd,
1586 TALLOC_CTX *mem_ctx,
1587 int argc,
1588 const char **argv)
1590 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1591 bool group_is_primary = false;
1592 NTSTATUS status, result;
1593 uint32_t group_rid;
1594 struct samr_RidAttrArray *rids = NULL;
1595 /* char **names; */
1596 int i;
1597 /* struct samr_RidWithAttribute *user_gids; */
1598 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1600 struct samr_Ids group_rids, name_types;
1601 struct lsa_String lsa_acct_name;
1602 union samr_UserInfo *info = NULL;
1604 if (argc < 1 || c->display_usage) {
1605 rpc_group_usage(c, argc,argv);
1606 return NT_STATUS_OK; /* ok? */
1609 status = dcerpc_samr_Connect2(b, mem_ctx,
1610 pipe_hnd->desthost,
1611 MAXIMUM_ALLOWED_ACCESS,
1612 &connect_pol,
1613 &result);
1614 if (!NT_STATUS_IS_OK(status)) {
1615 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1616 goto done;
1619 if (!NT_STATUS_IS_OK(result)) {
1620 status = result;
1621 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1622 goto done;
1625 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1626 &connect_pol,
1627 MAXIMUM_ALLOWED_ACCESS,
1628 discard_const_p(struct dom_sid2, domain_sid),
1629 &domain_pol,
1630 &result);
1631 if (!NT_STATUS_IS_OK(status)) {
1632 d_fprintf(stderr, _("Request open_domain failed\n"));
1633 goto done;
1636 if (!NT_STATUS_IS_OK(result)) {
1637 status = result;
1638 d_fprintf(stderr, _("Request open_domain failed\n"));
1639 goto done;
1642 init_lsa_String(&lsa_acct_name, argv[0]);
1644 status = dcerpc_samr_LookupNames(b, mem_ctx,
1645 &domain_pol,
1647 &lsa_acct_name,
1648 &group_rids,
1649 &name_types,
1650 &result);
1651 if (!NT_STATUS_IS_OK(status)) {
1652 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1653 goto done;
1656 if (!NT_STATUS_IS_OK(result)) {
1657 status = result;
1658 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1659 goto done;
1661 if (group_rids.count != 1) {
1662 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1663 goto done;
1665 if (name_types.count != 1) {
1666 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1667 goto done;
1670 switch (name_types.ids[0])
1672 case SID_NAME_DOM_GRP:
1673 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1674 &domain_pol,
1675 MAXIMUM_ALLOWED_ACCESS,
1676 group_rids.ids[0],
1677 &group_pol,
1678 &result);
1679 if (!NT_STATUS_IS_OK(status)) {
1680 d_fprintf(stderr, _("Request open_group failed"));
1681 goto done;
1684 if (!NT_STATUS_IS_OK(result)) {
1685 status = result;
1686 d_fprintf(stderr, _("Request open_group failed"));
1687 goto done;
1690 group_rid = group_rids.ids[0];
1692 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1693 &group_pol,
1694 &rids,
1695 &result);
1696 if (!NT_STATUS_IS_OK(status)) {
1697 d_fprintf(stderr,
1698 _("Unable to query group members of %s"),
1699 argv[0]);
1700 goto done;
1703 if (!NT_STATUS_IS_OK(result)) {
1704 status = result;
1705 d_fprintf(stderr,
1706 _("Unable to query group members of %s"),
1707 argv[0]);
1708 goto done;
1711 if (c->opt_verbose) {
1712 d_printf(
1713 _("Domain Group %s (rid: %d) has %d members\n"),
1714 argv[0],group_rid, rids->count);
1717 /* Check if group is anyone's primary group */
1718 for (i = 0; i < rids->count; i++)
1720 status = dcerpc_samr_OpenUser(b, mem_ctx,
1721 &domain_pol,
1722 MAXIMUM_ALLOWED_ACCESS,
1723 rids->rids[i],
1724 &user_pol,
1725 &result);
1726 if (!NT_STATUS_IS_OK(status)) {
1727 d_fprintf(stderr,
1728 _("Unable to open group member %d\n"),
1729 rids->rids[i]);
1730 goto done;
1733 if (!NT_STATUS_IS_OK(result)) {
1734 status = result;
1735 d_fprintf(stderr,
1736 _("Unable to open group member %d\n"),
1737 rids->rids[i]);
1738 goto done;
1741 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1742 &user_pol,
1744 &info,
1745 &result);
1746 if (!NT_STATUS_IS_OK(status)) {
1747 d_fprintf(stderr,
1748 _("Unable to lookup userinfo for group "
1749 "member %d\n"),
1750 rids->rids[i]);
1751 goto done;
1754 if (!NT_STATUS_IS_OK(result)) {
1755 status = result;
1756 d_fprintf(stderr,
1757 _("Unable to lookup userinfo for group "
1758 "member %d\n"),
1759 rids->rids[i]);
1760 goto done;
1763 if (info->info21.primary_gid == group_rid) {
1764 if (c->opt_verbose) {
1765 d_printf(_("Group is primary group "
1766 "of %s\n"),
1767 info->info21.account_name.string);
1769 group_is_primary = true;
1772 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1775 if (group_is_primary) {
1776 d_fprintf(stderr, _("Unable to delete group because "
1777 "some of it's members have it as primary "
1778 "group\n"));
1779 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1780 goto done;
1783 /* remove all group members */
1784 for (i = 0; i < rids->count; i++)
1786 if (c->opt_verbose)
1787 d_printf(_("Remove group member %d..."),
1788 rids->rids[i]);
1789 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1790 &group_pol,
1791 rids->rids[i],
1792 &result);
1793 if (!NT_STATUS_IS_OK(status)) {
1794 goto done;
1796 status = result;
1797 if (NT_STATUS_IS_OK(result)) {
1798 if (c->opt_verbose)
1799 d_printf(_("ok\n"));
1800 } else {
1801 if (c->opt_verbose)
1802 d_printf("%s\n", _("failed"));
1803 goto done;
1807 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1808 &group_pol,
1809 &result);
1810 if (!NT_STATUS_IS_OK(status)) {
1811 break;
1814 status = result;
1816 break;
1817 /* removing a local group is easier... */
1818 case SID_NAME_ALIAS:
1819 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1820 &domain_pol,
1821 MAXIMUM_ALLOWED_ACCESS,
1822 group_rids.ids[0],
1823 &group_pol,
1824 &result);
1825 if (!NT_STATUS_IS_OK(status)) {
1826 d_fprintf(stderr, _("Request open_alias failed\n"));
1827 goto done;
1829 if (!NT_STATUS_IS_OK(result)) {
1830 status = result;
1831 d_fprintf(stderr, _("Request open_alias failed\n"));
1832 goto done;
1835 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1836 &group_pol,
1837 &result);
1838 if (!NT_STATUS_IS_OK(status)) {
1839 break;
1842 status = result;
1844 break;
1845 default:
1846 d_fprintf(stderr, _("%s is of type %s. This command is only "
1847 "for deleting local or global groups\n"),
1848 argv[0],sid_type_lookup(name_types.ids[0]));
1849 status = NT_STATUS_UNSUCCESSFUL;
1850 goto done;
1853 if (NT_STATUS_IS_OK(status)) {
1854 if (c->opt_verbose)
1855 d_printf(_("Deleted %s '%s'\n"),
1856 sid_type_lookup(name_types.ids[0]), argv[0]);
1857 } else {
1858 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1859 get_friendly_nt_error_msg(status));
1862 done:
1863 return status;
1867 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1869 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
1870 rpc_group_delete_internals, argc,argv);
1873 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1875 NET_API_STATUS status;
1876 struct GROUP_INFO_1 info1;
1877 uint32_t parm_error = 0;
1879 if (argc != 1 || c->display_usage) {
1880 rpc_group_usage(c, argc, argv);
1881 return 0;
1884 ZERO_STRUCT(info1);
1886 info1.grpi1_name = argv[0];
1887 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1888 info1.grpi1_comment = c->opt_comment;
1891 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1893 if (status != 0) {
1894 d_fprintf(stderr,
1895 _("Failed to add group '%s' with error: %s.\n"),
1896 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1897 status));
1898 return -1;
1899 } else {
1900 d_printf(_("Added group '%s'.\n"), argv[0]);
1903 return 0;
1906 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1908 NET_API_STATUS status;
1909 struct LOCALGROUP_INFO_1 info1;
1910 uint32_t parm_error = 0;
1912 if (argc != 1 || c->display_usage) {
1913 rpc_group_usage(c, argc, argv);
1914 return 0;
1917 ZERO_STRUCT(info1);
1919 info1.lgrpi1_name = argv[0];
1920 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1921 info1.lgrpi1_comment = c->opt_comment;
1924 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1926 if (status != 0) {
1927 d_fprintf(stderr,
1928 _("Failed to add alias '%s' with error: %s.\n"),
1929 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1930 status));
1931 return -1;
1932 } else {
1933 d_printf(_("Added alias '%s'.\n"), argv[0]);
1936 return 0;
1939 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1941 if (c->opt_localgroup)
1942 return rpc_alias_add_internals(c, argc, argv);
1944 return rpc_group_add_internals(c, argc, argv);
1947 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1948 TALLOC_CTX *mem_ctx,
1949 const char *name,
1950 struct dom_sid *sid,
1951 enum lsa_SidType *type)
1953 struct dom_sid *sids = NULL;
1954 enum lsa_SidType *types = NULL;
1955 struct rpc_pipe_client *pipe_hnd = NULL;
1956 struct policy_handle lsa_pol;
1957 NTSTATUS status, result;
1958 struct dcerpc_binding_handle *b;
1960 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1961 &pipe_hnd);
1962 if (!NT_STATUS_IS_OK(status)) {
1963 goto done;
1966 b = pipe_hnd->binding_handle;
1968 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1969 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1971 if (!NT_STATUS_IS_OK(status)) {
1972 goto done;
1975 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1976 &name, NULL, 1, &sids, &types);
1978 if (NT_STATUS_IS_OK(status)) {
1979 sid_copy(sid, &sids[0]);
1980 *type = types[0];
1983 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1985 done:
1986 if (pipe_hnd) {
1987 TALLOC_FREE(pipe_hnd);
1990 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
1992 /* Try as S-1-5-whatever */
1994 struct dom_sid tmp_sid;
1996 if (string_to_sid(&tmp_sid, name)) {
1997 sid_copy(sid, &tmp_sid);
1998 *type = SID_NAME_UNKNOWN;
1999 status = NT_STATUS_OK;
2003 return status;
2006 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2007 TALLOC_CTX *mem_ctx,
2008 const struct dom_sid *group_sid,
2009 const char *member)
2011 struct policy_handle connect_pol, domain_pol;
2012 NTSTATUS status, result;
2013 uint32 group_rid;
2014 struct policy_handle group_pol;
2015 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2017 struct samr_Ids rids, rid_types;
2018 struct lsa_String lsa_acct_name;
2020 struct dom_sid sid;
2022 sid_copy(&sid, group_sid);
2024 if (!sid_split_rid(&sid, &group_rid)) {
2025 return NT_STATUS_UNSUCCESSFUL;
2028 /* Get sam policy handle */
2029 status = dcerpc_samr_Connect2(b, mem_ctx,
2030 pipe_hnd->desthost,
2031 MAXIMUM_ALLOWED_ACCESS,
2032 &connect_pol,
2033 &result);
2034 if (!NT_STATUS_IS_OK(status)) {
2035 return status;
2037 if (!NT_STATUS_IS_OK(result)) {
2038 return result;
2041 /* Get domain policy handle */
2042 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2043 &connect_pol,
2044 MAXIMUM_ALLOWED_ACCESS,
2045 &sid,
2046 &domain_pol,
2047 &result);
2048 if (!NT_STATUS_IS_OK(status)) {
2049 return status;
2051 if (!NT_STATUS_IS_OK(result)) {
2052 return result;
2055 init_lsa_String(&lsa_acct_name, member);
2057 status = dcerpc_samr_LookupNames(b, mem_ctx,
2058 &domain_pol,
2060 &lsa_acct_name,
2061 &rids,
2062 &rid_types,
2063 &result);
2064 if (!NT_STATUS_IS_OK(status)) {
2065 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2066 member);
2067 goto done;
2070 if (!NT_STATUS_IS_OK(result)) {
2071 status = result;
2072 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2073 member);
2074 goto done;
2076 if (rids.count != 1) {
2077 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2078 goto done;
2080 if (rid_types.count != 1) {
2081 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2082 goto done;
2085 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2086 &domain_pol,
2087 MAXIMUM_ALLOWED_ACCESS,
2088 group_rid,
2089 &group_pol,
2090 &result);
2091 if (!NT_STATUS_IS_OK(status)) {
2092 goto done;
2095 if (!NT_STATUS_IS_OK(result)) {
2096 status = result;
2097 goto done;
2100 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2101 &group_pol,
2102 rids.ids[0],
2103 0x0005, /* unknown flags */
2104 &result);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 goto done;
2109 status = result;
2111 done:
2112 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2113 return status;
2116 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2117 TALLOC_CTX *mem_ctx,
2118 const struct dom_sid *alias_sid,
2119 const char *member)
2121 struct policy_handle connect_pol, domain_pol;
2122 NTSTATUS status, result;
2123 uint32 alias_rid;
2124 struct policy_handle alias_pol;
2125 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2127 struct dom_sid member_sid;
2128 enum lsa_SidType member_type;
2130 struct dom_sid sid;
2132 sid_copy(&sid, alias_sid);
2134 if (!sid_split_rid(&sid, &alias_rid)) {
2135 return NT_STATUS_UNSUCCESSFUL;
2138 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2139 member, &member_sid, &member_type);
2141 if (!NT_STATUS_IS_OK(result)) {
2142 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2143 member);
2144 return result;
2147 /* Get sam policy handle */
2148 status = dcerpc_samr_Connect2(b, mem_ctx,
2149 pipe_hnd->desthost,
2150 MAXIMUM_ALLOWED_ACCESS,
2151 &connect_pol,
2152 &result);
2153 if (!NT_STATUS_IS_OK(status)) {
2154 goto done;
2156 if (!NT_STATUS_IS_OK(result)) {
2157 status = result;
2158 goto done;
2161 /* Get domain policy handle */
2162 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2163 &connect_pol,
2164 MAXIMUM_ALLOWED_ACCESS,
2165 &sid,
2166 &domain_pol,
2167 &result);
2168 if (!NT_STATUS_IS_OK(status)) {
2169 goto done;
2171 if (!NT_STATUS_IS_OK(result)) {
2172 status = result;
2173 goto done;
2176 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2177 &domain_pol,
2178 MAXIMUM_ALLOWED_ACCESS,
2179 alias_rid,
2180 &alias_pol,
2181 &result);
2182 if (!NT_STATUS_IS_OK(status)) {
2183 return status;
2185 if (!NT_STATUS_IS_OK(result)) {
2186 return result;
2189 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2190 &alias_pol,
2191 &member_sid,
2192 &result);
2193 if (!NT_STATUS_IS_OK(status)) {
2194 return status;
2197 status = result;
2199 done:
2200 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2201 return status;
2204 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2205 const struct dom_sid *domain_sid,
2206 const char *domain_name,
2207 struct cli_state *cli,
2208 struct rpc_pipe_client *pipe_hnd,
2209 TALLOC_CTX *mem_ctx,
2210 int argc,
2211 const char **argv)
2213 struct dom_sid group_sid;
2214 enum lsa_SidType group_type;
2216 if (argc != 2 || c->display_usage) {
2217 d_printf("%s\n%s",
2218 _("Usage:"),
2219 _("net rpc group addmem <group> <member>\n"
2220 " Add a member to a group\n"
2221 " group\tGroup to add member to\n"
2222 " member\tMember to add to group\n"));
2223 return NT_STATUS_UNSUCCESSFUL;
2226 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2227 &group_sid, &group_type))) {
2228 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2229 argv[0]);
2230 return NT_STATUS_UNSUCCESSFUL;
2233 if (group_type == SID_NAME_DOM_GRP) {
2234 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2235 &group_sid, argv[1]);
2237 if (!NT_STATUS_IS_OK(result)) {
2238 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2239 argv[1], argv[0], nt_errstr(result));
2241 return result;
2244 if (group_type == SID_NAME_ALIAS) {
2245 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2246 &group_sid, argv[1]);
2248 if (!NT_STATUS_IS_OK(result)) {
2249 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2250 argv[1], argv[0], nt_errstr(result));
2252 return result;
2255 d_fprintf(stderr, _("Can only add members to global or local groups "
2256 "which %s is not\n"), argv[0]);
2258 return NT_STATUS_UNSUCCESSFUL;
2261 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2263 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2264 rpc_group_addmem_internals,
2265 argc, argv);
2268 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2269 struct rpc_pipe_client *pipe_hnd,
2270 TALLOC_CTX *mem_ctx,
2271 const struct dom_sid *group_sid,
2272 const char *member)
2274 struct policy_handle connect_pol, domain_pol;
2275 NTSTATUS status, result;
2276 uint32 group_rid;
2277 struct policy_handle group_pol;
2278 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2280 struct samr_Ids rids, rid_types;
2281 struct lsa_String lsa_acct_name;
2283 struct dom_sid sid;
2285 sid_copy(&sid, group_sid);
2287 if (!sid_split_rid(&sid, &group_rid))
2288 return NT_STATUS_UNSUCCESSFUL;
2290 /* Get sam policy handle */
2291 status = dcerpc_samr_Connect2(b, mem_ctx,
2292 pipe_hnd->desthost,
2293 MAXIMUM_ALLOWED_ACCESS,
2294 &connect_pol,
2295 &result);
2296 if (!NT_STATUS_IS_OK(status)) {
2297 return status;
2299 if (!NT_STATUS_IS_OK(result)) {
2300 return result;
2304 /* Get domain policy handle */
2305 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2306 &connect_pol,
2307 MAXIMUM_ALLOWED_ACCESS,
2308 &sid,
2309 &domain_pol,
2310 &result);
2311 if (!NT_STATUS_IS_OK(status)) {
2312 return status;
2314 if (!NT_STATUS_IS_OK(result)) {
2315 return result;
2318 init_lsa_String(&lsa_acct_name, member);
2320 status = dcerpc_samr_LookupNames(b, mem_ctx,
2321 &domain_pol,
2323 &lsa_acct_name,
2324 &rids,
2325 &rid_types,
2326 &result);
2327 if (!NT_STATUS_IS_OK(status)) {
2328 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2329 member);
2330 goto done;
2333 if (!NT_STATUS_IS_OK(result)) {
2334 status = result;
2335 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2336 member);
2337 goto done;
2339 if (rids.count != 1) {
2340 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2341 goto done;
2343 if (rid_types.count != 1) {
2344 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2345 goto done;
2348 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2349 &domain_pol,
2350 MAXIMUM_ALLOWED_ACCESS,
2351 group_rid,
2352 &group_pol,
2353 &result);
2354 if (!NT_STATUS_IS_OK(status)) {
2355 goto done;
2357 if (!NT_STATUS_IS_OK(result)) {
2358 status = result;
2359 goto done;
2362 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2363 &group_pol,
2364 rids.ids[0],
2365 &result);
2366 if (!NT_STATUS_IS_OK(status)) {
2367 goto done;
2370 status = result;
2371 done:
2372 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2373 return status;
2376 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2377 TALLOC_CTX *mem_ctx,
2378 const struct dom_sid *alias_sid,
2379 const char *member)
2381 struct policy_handle connect_pol, domain_pol;
2382 NTSTATUS status, result;
2383 uint32 alias_rid;
2384 struct policy_handle alias_pol;
2385 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2387 struct dom_sid member_sid;
2388 enum lsa_SidType member_type;
2390 struct dom_sid sid;
2392 sid_copy(&sid, alias_sid);
2394 if (!sid_split_rid(&sid, &alias_rid))
2395 return NT_STATUS_UNSUCCESSFUL;
2397 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2398 member, &member_sid, &member_type);
2400 if (!NT_STATUS_IS_OK(result)) {
2401 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2402 member);
2403 return result;
2406 /* Get sam policy handle */
2407 status = dcerpc_samr_Connect2(b, mem_ctx,
2408 pipe_hnd->desthost,
2409 MAXIMUM_ALLOWED_ACCESS,
2410 &connect_pol,
2411 &result);
2412 if (!NT_STATUS_IS_OK(status)) {
2413 goto done;
2415 if (!NT_STATUS_IS_OK(result)) {
2416 status = result;
2417 goto done;
2420 /* Get domain policy handle */
2421 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2422 &connect_pol,
2423 MAXIMUM_ALLOWED_ACCESS,
2424 &sid,
2425 &domain_pol,
2426 &result);
2427 if (!NT_STATUS_IS_OK(status)) {
2428 goto done;
2430 if (!NT_STATUS_IS_OK(result)) {
2431 status = result;
2432 goto done;
2435 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2436 &domain_pol,
2437 MAXIMUM_ALLOWED_ACCESS,
2438 alias_rid,
2439 &alias_pol,
2440 &result);
2441 if (!NT_STATUS_IS_OK(status)) {
2442 return status;
2445 if (!NT_STATUS_IS_OK(result)) {
2446 return result;
2449 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2450 &alias_pol,
2451 &member_sid,
2452 &result);
2454 if (!NT_STATUS_IS_OK(status)) {
2455 return status;
2458 status = result;
2460 done:
2461 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2462 return status;
2465 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2466 const struct dom_sid *domain_sid,
2467 const char *domain_name,
2468 struct cli_state *cli,
2469 struct rpc_pipe_client *pipe_hnd,
2470 TALLOC_CTX *mem_ctx,
2471 int argc,
2472 const char **argv)
2474 struct dom_sid group_sid;
2475 enum lsa_SidType group_type;
2477 if (argc != 2 || c->display_usage) {
2478 d_printf("%s\n%s",
2479 _("Usage:"),
2480 _("net rpc group delmem <group> <member>\n"
2481 " Delete a member from a group\n"
2482 " group\tGroup to delete member from\n"
2483 " member\tMember to delete from group\n"));
2484 return NT_STATUS_UNSUCCESSFUL;
2487 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2488 &group_sid, &group_type))) {
2489 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2490 argv[0]);
2491 return NT_STATUS_UNSUCCESSFUL;
2494 if (group_type == SID_NAME_DOM_GRP) {
2495 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2496 &group_sid, argv[1]);
2498 if (!NT_STATUS_IS_OK(result)) {
2499 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2500 argv[1], argv[0], nt_errstr(result));
2502 return result;
2505 if (group_type == SID_NAME_ALIAS) {
2506 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2507 &group_sid, argv[1]);
2509 if (!NT_STATUS_IS_OK(result)) {
2510 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2511 argv[1], argv[0], nt_errstr(result));
2513 return result;
2516 d_fprintf(stderr, _("Can only delete members from global or local "
2517 "groups which %s is not\n"), argv[0]);
2519 return NT_STATUS_UNSUCCESSFUL;
2522 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2524 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2525 rpc_group_delmem_internals,
2526 argc, argv);
2530 * List groups on a remote RPC server.
2532 * All parameters are provided by the run_rpc_command function, except for
2533 * argc, argv which are passes through.
2535 * @param domain_sid The domain sid acquired from the remote server.
2536 * @param cli A cli_state connected to the server.
2537 * @param mem_ctx Talloc context, destroyed on completion of the function.
2538 * @param argc Standard main() style argc.
2539 * @param argv Standard main() style argv. Initial components are already
2540 * stripped.
2542 * @return Normal NTSTATUS return.
2545 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2546 const struct dom_sid *domain_sid,
2547 const char *domain_name,
2548 struct cli_state *cli,
2549 struct rpc_pipe_client *pipe_hnd,
2550 TALLOC_CTX *mem_ctx,
2551 int argc,
2552 const char **argv)
2554 struct policy_handle connect_pol, domain_pol;
2555 NTSTATUS status, result;
2556 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2557 struct samr_SamArray *groups = NULL;
2558 bool global = false;
2559 bool local = false;
2560 bool builtin = false;
2561 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2563 if (c->display_usage) {
2564 d_printf("%s\n%s",
2565 _("Usage:"),
2566 _("net rpc group list [global] [local] [builtin]\n"
2567 " List groups on RPC server\n"
2568 " global\tList global groups\n"
2569 " local\tList local groups\n"
2570 " builtin\tList builtin groups\n"
2571 " If none of global, local or builtin is "
2572 "specified, all three options are considered "
2573 "set\n"));
2574 return NT_STATUS_OK;
2577 if (argc == 0) {
2578 global = true;
2579 local = true;
2580 builtin = true;
2583 for (i=0; i<argc; i++) {
2584 if (strequal(argv[i], "global"))
2585 global = true;
2587 if (strequal(argv[i], "local"))
2588 local = true;
2590 if (strequal(argv[i], "builtin"))
2591 builtin = true;
2594 /* Get sam policy handle */
2596 status = dcerpc_samr_Connect2(b, mem_ctx,
2597 pipe_hnd->desthost,
2598 MAXIMUM_ALLOWED_ACCESS,
2599 &connect_pol,
2600 &result);
2601 if (!NT_STATUS_IS_OK(status)) {
2602 goto done;
2604 if (!NT_STATUS_IS_OK(result)) {
2605 status = result;
2606 goto done;
2609 /* Get domain policy handle */
2611 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2612 &connect_pol,
2613 MAXIMUM_ALLOWED_ACCESS,
2614 discard_const_p(struct dom_sid2, domain_sid),
2615 &domain_pol,
2616 &result);
2617 if (!NT_STATUS_IS_OK(status)) {
2618 goto done;
2620 if (!NT_STATUS_IS_OK(result)) {
2621 status = result;
2622 goto done;
2625 /* Query domain groups */
2626 if (c->opt_long_list_entries)
2627 d_printf(_("\nGroup name Comment"
2628 "\n-----------------------------\n"));
2629 do {
2630 uint32_t max_size, total_size, returned_size;
2631 union samr_DispInfo info;
2633 if (!global) break;
2635 dcerpc_get_query_dispinfo_params(
2636 loop_count, &max_entries, &max_size);
2638 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2639 &domain_pol,
2641 start_idx,
2642 max_entries,
2643 max_size,
2644 &total_size,
2645 &returned_size,
2646 &info,
2647 &result);
2648 if (!NT_STATUS_IS_OK(status)) {
2649 goto done;
2651 num_entries = info.info3.count;
2652 start_idx += info.info3.count;
2654 if (!NT_STATUS_IS_OK(result) &&
2655 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2656 break;
2658 for (i = 0; i < num_entries; i++) {
2660 const char *group = NULL;
2661 const char *desc = NULL;
2663 group = info.info3.entries[i].account_name.string;
2664 desc = info.info3.entries[i].description.string;
2666 if (c->opt_long_list_entries)
2667 printf("%-21.21s %-50.50s\n",
2668 group, desc);
2669 else
2670 printf("%s\n", group);
2672 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2673 /* query domain aliases */
2674 start_idx = 0;
2675 do {
2676 if (!local) break;
2678 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2679 &domain_pol,
2680 &start_idx,
2681 &groups,
2682 0xffff,
2683 &num_entries,
2684 &result);
2685 if (!NT_STATUS_IS_OK(status)) {
2686 goto done;
2688 if (!NT_STATUS_IS_OK(result) &&
2689 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2690 break;
2692 for (i = 0; i < num_entries; i++) {
2694 const char *description = NULL;
2696 if (c->opt_long_list_entries) {
2698 struct policy_handle alias_pol;
2699 union samr_AliasInfo *info = NULL;
2700 NTSTATUS _result;
2702 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2703 &domain_pol,
2704 0x8,
2705 groups->entries[i].idx,
2706 &alias_pol,
2707 &_result);
2708 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2709 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2710 &alias_pol,
2712 &info,
2713 &_result);
2714 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2715 status = dcerpc_samr_Close(b, mem_ctx,
2716 &alias_pol,
2717 &_result);
2718 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2719 description = info->description.string;
2725 if (description != NULL) {
2726 printf("%-21.21s %-50.50s\n",
2727 groups->entries[i].name.string,
2728 description);
2729 } else {
2730 printf("%s\n", groups->entries[i].name.string);
2733 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2734 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2735 /* Get builtin policy handle */
2737 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2738 &connect_pol,
2739 MAXIMUM_ALLOWED_ACCESS,
2740 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2741 &domain_pol,
2742 &result);
2743 if (!NT_STATUS_IS_OK(status)) {
2744 goto done;
2746 if (!NT_STATUS_IS_OK(result)) {
2747 status = result;
2748 goto done;
2751 /* query builtin aliases */
2752 start_idx = 0;
2753 do {
2754 if (!builtin) break;
2756 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2757 &domain_pol,
2758 &start_idx,
2759 &groups,
2760 max_entries,
2761 &num_entries,
2762 &result);
2763 if (!NT_STATUS_IS_OK(status)) {
2764 break;
2766 if (!NT_STATUS_IS_OK(result) &&
2767 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2768 status = result;
2769 break;
2772 for (i = 0; i < num_entries; i++) {
2774 const char *description = NULL;
2776 if (c->opt_long_list_entries) {
2778 struct policy_handle alias_pol;
2779 union samr_AliasInfo *info = NULL;
2780 NTSTATUS _result;
2782 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2783 &domain_pol,
2784 0x8,
2785 groups->entries[i].idx,
2786 &alias_pol,
2787 &_result);
2788 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2789 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2790 &alias_pol,
2792 &info,
2793 &_result);
2794 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2795 status = dcerpc_samr_Close(b, mem_ctx,
2796 &alias_pol,
2797 &_result);
2798 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2799 description = info->description.string;
2805 if (description != NULL) {
2806 printf("%-21.21s %-50.50s\n",
2807 groups->entries[i].name.string,
2808 description);
2809 } else {
2810 printf("%s\n", groups->entries[i].name.string);
2813 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2815 status = result;
2817 done:
2818 return status;
2821 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2823 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2824 rpc_group_list_internals,
2825 argc, argv);
2828 static NTSTATUS rpc_list_group_members(struct net_context *c,
2829 struct rpc_pipe_client *pipe_hnd,
2830 TALLOC_CTX *mem_ctx,
2831 const char *domain_name,
2832 const struct dom_sid *domain_sid,
2833 struct policy_handle *domain_pol,
2834 uint32 rid)
2836 NTSTATUS result, status;
2837 struct policy_handle group_pol;
2838 uint32 num_members, *group_rids;
2839 int i;
2840 struct samr_RidAttrArray *rids = NULL;
2841 struct lsa_Strings names;
2842 struct samr_Ids types;
2843 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2845 fstring sid_str;
2846 sid_to_fstring(sid_str, domain_sid);
2848 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2849 domain_pol,
2850 MAXIMUM_ALLOWED_ACCESS,
2851 rid,
2852 &group_pol,
2853 &result);
2854 if (!NT_STATUS_IS_OK(status)) {
2855 return status;
2857 if (!NT_STATUS_IS_OK(result)) {
2858 return result;
2861 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
2862 &group_pol,
2863 &rids,
2864 &result);
2865 if (!NT_STATUS_IS_OK(status)) {
2866 return status;
2868 if (!NT_STATUS_IS_OK(result)) {
2869 return result;
2872 num_members = rids->count;
2873 group_rids = rids->rids;
2875 while (num_members > 0) {
2876 int this_time = 512;
2878 if (num_members < this_time)
2879 this_time = num_members;
2881 status = dcerpc_samr_LookupRids(b, mem_ctx,
2882 domain_pol,
2883 this_time,
2884 group_rids,
2885 &names,
2886 &types,
2887 &result);
2888 if (!NT_STATUS_IS_OK(status)) {
2889 return status;
2891 if (!NT_STATUS_IS_OK(result)) {
2892 return result;
2895 /* We only have users as members, but make the output
2896 the same as the output of alias members */
2898 for (i = 0; i < this_time; i++) {
2900 if (c->opt_long_list_entries) {
2901 printf("%s-%d %s\\%s %d\n", sid_str,
2902 group_rids[i], domain_name,
2903 names.names[i].string,
2904 SID_NAME_USER);
2905 } else {
2906 printf("%s\\%s\n", domain_name,
2907 names.names[i].string);
2911 num_members -= this_time;
2912 group_rids += 512;
2915 return NT_STATUS_OK;
2918 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2919 struct rpc_pipe_client *pipe_hnd,
2920 TALLOC_CTX *mem_ctx,
2921 struct policy_handle *domain_pol,
2922 uint32 rid)
2924 NTSTATUS result, status;
2925 struct rpc_pipe_client *lsa_pipe;
2926 struct policy_handle alias_pol, lsa_pol;
2927 uint32 num_members;
2928 struct dom_sid *alias_sids;
2929 char **domains;
2930 char **names;
2931 enum lsa_SidType *types;
2932 int i;
2933 struct lsa_SidArray sid_array;
2934 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2936 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2937 domain_pol,
2938 MAXIMUM_ALLOWED_ACCESS,
2939 rid,
2940 &alias_pol,
2941 &result);
2942 if (!NT_STATUS_IS_OK(status)) {
2943 return status;
2945 if (!NT_STATUS_IS_OK(result)) {
2946 return result;
2949 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
2950 &alias_pol,
2951 &sid_array,
2952 &result);
2953 if (!NT_STATUS_IS_OK(status)) {
2954 d_fprintf(stderr, _("Couldn't list alias members\n"));
2955 return status;
2957 if (!NT_STATUS_IS_OK(result)) {
2958 d_fprintf(stderr, _("Couldn't list alias members\n"));
2959 return result;
2962 num_members = sid_array.num_sids;
2964 if (num_members == 0) {
2965 return NT_STATUS_OK;
2968 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2969 &ndr_table_lsarpc.syntax_id,
2970 &lsa_pipe);
2971 if (!NT_STATUS_IS_OK(result)) {
2972 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2973 nt_errstr(result) );
2974 return result;
2977 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2978 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2980 if (!NT_STATUS_IS_OK(result)) {
2981 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2982 TALLOC_FREE(lsa_pipe);
2983 return result;
2986 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
2987 if (!alias_sids) {
2988 d_fprintf(stderr, _("Out of memory\n"));
2989 TALLOC_FREE(lsa_pipe);
2990 return NT_STATUS_NO_MEMORY;
2993 for (i=0; i<num_members; i++) {
2994 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2997 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2998 num_members, alias_sids,
2999 &domains, &names, &types);
3001 if (!NT_STATUS_IS_OK(result) &&
3002 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3003 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3004 TALLOC_FREE(lsa_pipe);
3005 return result;
3008 for (i = 0; i < num_members; i++) {
3009 fstring sid_str;
3010 sid_to_fstring(sid_str, &alias_sids[i]);
3012 if (c->opt_long_list_entries) {
3013 printf("%s %s\\%s %d\n", sid_str,
3014 domains[i] ? domains[i] : _("*unknown*"),
3015 names[i] ? names[i] : _("*unknown*"), types[i]);
3016 } else {
3017 if (domains[i])
3018 printf("%s\\%s\n", domains[i], names[i]);
3019 else
3020 printf("%s\n", sid_str);
3024 TALLOC_FREE(lsa_pipe);
3025 return NT_STATUS_OK;
3028 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3029 const struct dom_sid *domain_sid,
3030 const char *domain_name,
3031 struct cli_state *cli,
3032 struct rpc_pipe_client *pipe_hnd,
3033 TALLOC_CTX *mem_ctx,
3034 int argc,
3035 const char **argv)
3037 NTSTATUS result, status;
3038 struct policy_handle connect_pol, domain_pol;
3039 struct samr_Ids rids, rid_types;
3040 struct lsa_String lsa_acct_name;
3041 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3043 /* Get sam policy handle */
3045 status = dcerpc_samr_Connect2(b, mem_ctx,
3046 pipe_hnd->desthost,
3047 MAXIMUM_ALLOWED_ACCESS,
3048 &connect_pol,
3049 &result);
3050 if (!NT_STATUS_IS_OK(status)) {
3051 return status;
3053 if (!NT_STATUS_IS_OK(result)) {
3054 return result;
3057 /* Get domain policy handle */
3059 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3060 &connect_pol,
3061 MAXIMUM_ALLOWED_ACCESS,
3062 discard_const_p(struct dom_sid2, domain_sid),
3063 &domain_pol,
3064 &result);
3065 if (!NT_STATUS_IS_OK(status)) {
3066 return status;
3068 if (!NT_STATUS_IS_OK(result)) {
3069 return result;
3072 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3074 status = dcerpc_samr_LookupNames(b, mem_ctx,
3075 &domain_pol,
3077 &lsa_acct_name,
3078 &rids,
3079 &rid_types,
3080 &result);
3081 if (!NT_STATUS_IS_OK(status)) {
3082 return status;
3085 if (!NT_STATUS_IS_OK(result)) {
3087 /* Ok, did not find it in the global sam, try with builtin */
3089 struct dom_sid sid_Builtin;
3091 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3093 sid_copy(&sid_Builtin, &global_sid_Builtin);
3095 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3096 &connect_pol,
3097 MAXIMUM_ALLOWED_ACCESS,
3098 &sid_Builtin,
3099 &domain_pol,
3100 &result);
3101 if (!NT_STATUS_IS_OK(status)) {
3102 return status;
3104 if (!NT_STATUS_IS_OK(result)) {
3105 d_fprintf(stderr, _("Couldn't find group %s\n"),
3106 argv[0]);
3107 return result;
3110 status = dcerpc_samr_LookupNames(b, mem_ctx,
3111 &domain_pol,
3113 &lsa_acct_name,
3114 &rids,
3115 &rid_types,
3116 &result);
3117 if (!NT_STATUS_IS_OK(status)) {
3118 return status;
3120 if (!NT_STATUS_IS_OK(result)) {
3121 d_fprintf(stderr, _("Couldn't find group %s\n"),
3122 argv[0]);
3123 return result;
3127 if (rids.count != 1) {
3128 d_fprintf(stderr, _("Couldn't find group %s\n"),
3129 argv[0]);
3130 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3132 if (rid_types.count != 1) {
3133 d_fprintf(stderr, _("Couldn't find group %s\n"),
3134 argv[0]);
3135 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3139 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3140 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3141 domain_sid, &domain_pol,
3142 rids.ids[0]);
3145 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3146 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3147 rids.ids[0]);
3150 return NT_STATUS_NO_SUCH_GROUP;
3153 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3155 if (argc != 1 || c->display_usage) {
3156 return rpc_group_usage(c, argc, argv);
3159 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3160 rpc_group_members_internals,
3161 argc, argv);
3164 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3166 NET_API_STATUS status;
3167 struct GROUP_INFO_0 g0;
3168 uint32_t parm_err;
3170 if (argc != 2) {
3171 d_printf(_("Usage:\n"));
3172 d_printf("net rpc group rename group newname\n");
3173 return -1;
3176 g0.grpi0_name = argv[1];
3178 status = NetGroupSetInfo(c->opt_host,
3179 argv[0],
3181 (uint8_t *)&g0,
3182 &parm_err);
3184 if (status != 0) {
3185 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3186 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3187 status));
3188 return -1;
3191 return 0;
3194 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3196 if (argc != 2 || c->display_usage) {
3197 return rpc_group_usage(c, argc, argv);
3200 return rpc_group_rename_internals(c, argc, argv);
3204 * 'net rpc group' entrypoint.
3205 * @param argc Standard main() style argc.
3206 * @param argv Standard main() style argv. Initial components are already
3207 * stripped.
3210 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3212 NET_API_STATUS status;
3214 struct functable func[] = {
3216 "add",
3217 rpc_group_add,
3218 NET_TRANSPORT_RPC,
3219 N_("Create specified group"),
3220 N_("net rpc group add\n"
3221 " Create specified group")
3224 "delete",
3225 rpc_group_delete,
3226 NET_TRANSPORT_RPC,
3227 N_("Delete specified group"),
3228 N_("net rpc group delete\n"
3229 " Delete specified group")
3232 "addmem",
3233 rpc_group_addmem,
3234 NET_TRANSPORT_RPC,
3235 N_("Add member to group"),
3236 N_("net rpc group addmem\n"
3237 " Add member to group")
3240 "delmem",
3241 rpc_group_delmem,
3242 NET_TRANSPORT_RPC,
3243 N_("Remove member from group"),
3244 N_("net rpc group delmem\n"
3245 " Remove member from group")
3248 "list",
3249 rpc_group_list,
3250 NET_TRANSPORT_RPC,
3251 N_("List groups"),
3252 N_("net rpc group list\n"
3253 " List groups")
3256 "members",
3257 rpc_group_members,
3258 NET_TRANSPORT_RPC,
3259 N_("List group members"),
3260 N_("net rpc group members\n"
3261 " List group members")
3264 "rename",
3265 rpc_group_rename,
3266 NET_TRANSPORT_RPC,
3267 N_("Rename group"),
3268 N_("net rpc group rename\n"
3269 " Rename group")
3271 {NULL, NULL, 0, NULL, NULL}
3274 status = libnetapi_net_init(&c->netapi_ctx);
3275 if (status != 0) {
3276 return -1;
3278 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3279 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3280 if (c->opt_kerberos) {
3281 libnetapi_set_use_kerberos(c->netapi_ctx);
3284 if (argc == 0) {
3285 if (c->display_usage) {
3286 d_printf(_("Usage:\n"));
3287 d_printf(_("net rpc group\n"
3288 " Alias for net rpc group list global "
3289 "local builtin\n"));
3290 net_display_usage_from_functable(func);
3291 return 0;
3294 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3295 rpc_group_list_internals,
3296 argc, argv);
3299 return net_run_function(c, argc, argv, "net rpc group", func);
3302 /****************************************************************************/
3304 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3306 return net_share_usage(c, argc, argv);
3310 * Add a share on a remote RPC server.
3312 * @param argc Standard main() style argc.
3313 * @param argv Standard main() style argv. Initial components are already
3314 * stripped.
3316 * @return A shell status integer (0 for success).
3319 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3321 NET_API_STATUS status;
3322 char *sharename;
3323 char *path;
3324 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3325 uint32 num_users=0, perms=0;
3326 char *password=NULL; /* don't allow a share password */
3327 struct SHARE_INFO_2 i2;
3328 uint32_t parm_error = 0;
3330 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3331 return rpc_share_usage(c, argc, argv);
3334 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3335 return -1;
3338 path = strchr(sharename, '=');
3339 if (!path) {
3340 return -1;
3343 *path++ = '\0';
3345 i2.shi2_netname = sharename;
3346 i2.shi2_type = type;
3347 i2.shi2_remark = c->opt_comment;
3348 i2.shi2_permissions = perms;
3349 i2.shi2_max_uses = c->opt_maxusers;
3350 i2.shi2_current_uses = num_users;
3351 i2.shi2_path = path;
3352 i2.shi2_passwd = password;
3354 status = NetShareAdd(c->opt_host,
3356 (uint8_t *)&i2,
3357 &parm_error);
3358 if (status != 0) {
3359 printf(_("NetShareAdd failed with: %s\n"),
3360 libnetapi_get_error_string(c->netapi_ctx, status));
3363 return status;
3367 * Delete a share on a remote RPC server.
3369 * @param domain_sid The domain sid acquired from the remote server.
3370 * @param argc Standard main() style argc.
3371 * @param argv Standard main() style argv. Initial components are already
3372 * stripped.
3374 * @return A shell status integer (0 for success).
3376 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3378 if (argc < 1 || c->display_usage) {
3379 return rpc_share_usage(c, argc, argv);
3382 return NetShareDel(c->opt_host, argv[0], 0);
3386 * Formatted print of share info
3388 * @param r pointer to SHARE_INFO_1 to format
3391 static void display_share_info_1(struct net_context *c,
3392 struct SHARE_INFO_1 *r)
3394 if (c->opt_long_list_entries) {
3395 d_printf("%-12s %-8.8s %-50s\n",
3396 r->shi1_netname,
3397 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3398 r->shi1_remark);
3399 } else {
3400 d_printf("%s\n", r->shi1_netname);
3404 static WERROR get_share_info(struct net_context *c,
3405 struct rpc_pipe_client *pipe_hnd,
3406 TALLOC_CTX *mem_ctx,
3407 uint32 level,
3408 int argc,
3409 const char **argv,
3410 struct srvsvc_NetShareInfoCtr *info_ctr)
3412 WERROR result;
3413 NTSTATUS status;
3414 union srvsvc_NetShareInfo info;
3415 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3417 /* no specific share requested, enumerate all */
3418 if (argc == 0) {
3420 uint32_t preferred_len = 0xffffffff;
3421 uint32_t total_entries = 0;
3422 uint32_t resume_handle = 0;
3424 info_ctr->level = level;
3426 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3427 pipe_hnd->desthost,
3428 info_ctr,
3429 preferred_len,
3430 &total_entries,
3431 &resume_handle,
3432 &result);
3433 if (!NT_STATUS_IS_OK(status)) {
3434 return ntstatus_to_werror(status);
3436 return result;
3439 /* request just one share */
3440 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3441 pipe_hnd->desthost,
3442 argv[0],
3443 level,
3444 &info,
3445 &result);
3447 if (!NT_STATUS_IS_OK(status)) {
3448 result = ntstatus_to_werror(status);
3449 goto done;
3452 if (!W_ERROR_IS_OK(result)) {
3453 goto done;
3456 /* construct ctr */
3457 ZERO_STRUCTP(info_ctr);
3459 info_ctr->level = level;
3461 switch (level) {
3462 case 1:
3464 struct srvsvc_NetShareCtr1 *ctr1;
3466 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3467 W_ERROR_HAVE_NO_MEMORY(ctr1);
3469 ctr1->count = 1;
3470 ctr1->array = info.info1;
3472 info_ctr->ctr.ctr1 = ctr1;
3474 break;
3476 case 2:
3478 struct srvsvc_NetShareCtr2 *ctr2;
3480 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3481 W_ERROR_HAVE_NO_MEMORY(ctr2);
3483 ctr2->count = 1;
3484 ctr2->array = info.info2;
3486 info_ctr->ctr.ctr2 = ctr2;
3488 break;
3490 case 502:
3492 struct srvsvc_NetShareCtr502 *ctr502;
3494 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3495 W_ERROR_HAVE_NO_MEMORY(ctr502);
3497 ctr502->count = 1;
3498 ctr502->array = info.info502;
3500 info_ctr->ctr.ctr502 = ctr502;
3502 break;
3504 } /* switch */
3505 done:
3506 return result;
3509 /***
3510 * 'net rpc share list' entrypoint.
3511 * @param argc Standard main() style argc.
3512 * @param argv Standard main() style argv. Initial components are already
3513 * stripped.
3515 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3517 NET_API_STATUS status;
3518 struct SHARE_INFO_1 *i1 = NULL;
3519 uint32_t entries_read = 0;
3520 uint32_t total_entries = 0;
3521 uint32_t resume_handle = 0;
3522 uint32_t i, level = 1;
3524 if (c->display_usage) {
3525 d_printf( "%s\n"
3526 "net rpc share list\n"
3527 " %s\n",
3528 _("Usage:"),
3529 _("List shares on remote server"));
3530 return 0;
3533 status = NetShareEnum(c->opt_host,
3534 level,
3535 (uint8_t **)(void *)&i1,
3536 (uint32_t)-1,
3537 &entries_read,
3538 &total_entries,
3539 &resume_handle);
3540 if (status != 0) {
3541 goto done;
3544 /* Display results */
3546 if (c->opt_long_list_entries) {
3547 d_printf(_(
3548 "\nEnumerating shared resources (exports) on remote server:\n\n"
3549 "\nShare name Type Description\n"
3550 "---------- ---- -----------\n"));
3552 for (i = 0; i < entries_read; i++)
3553 display_share_info_1(c, &i1[i]);
3554 done:
3555 return status;
3558 static bool check_share_availability(struct cli_state *cli, const char *netname)
3560 NTSTATUS status;
3562 status = cli_tree_connect(cli, netname, "A:", "", 0);
3563 if (!NT_STATUS_IS_OK(status)) {
3564 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3565 return false;
3568 status = cli_tdis(cli);
3569 if (!NT_STATUS_IS_OK(status)) {
3570 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3571 return false;
3574 return true;
3577 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3578 const char *netname, uint32 type)
3580 /* only support disk shares */
3581 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3582 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3583 type);
3584 return false;
3587 /* skip builtin shares */
3588 /* FIXME: should print$ be added too ? */
3589 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3590 strequal(netname,"global"))
3591 return false;
3593 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3594 printf(_("excluding [%s]\n"), netname);
3595 return false;
3598 return check_share_availability(cli, netname);
3602 * Migrate shares from a remote RPC server to the local RPC server.
3604 * All parameters are provided by the run_rpc_command function, except for
3605 * argc, argv which are passed through.
3607 * @param domain_sid The domain sid acquired from the remote server.
3608 * @param cli A cli_state connected to the server.
3609 * @param mem_ctx Talloc context, destroyed on completion of the function.
3610 * @param argc Standard main() style argc.
3611 * @param argv Standard main() style argv. Initial components are already
3612 * stripped.
3614 * @return Normal NTSTATUS return.
3617 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3618 const struct dom_sid *domain_sid,
3619 const char *domain_name,
3620 struct cli_state *cli,
3621 struct rpc_pipe_client *pipe_hnd,
3622 TALLOC_CTX *mem_ctx,
3623 int argc,
3624 const char **argv)
3626 WERROR result;
3627 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3628 struct srvsvc_NetShareInfoCtr ctr_src;
3629 uint32 i;
3630 struct rpc_pipe_client *srvsvc_pipe = NULL;
3631 struct cli_state *cli_dst = NULL;
3632 uint32 level = 502; /* includes secdesc */
3633 uint32_t parm_error = 0;
3634 struct dcerpc_binding_handle *b;
3636 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3637 &ctr_src);
3638 if (!W_ERROR_IS_OK(result))
3639 goto done;
3641 /* connect destination PI_SRVSVC */
3642 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3643 &ndr_table_srvsvc.syntax_id);
3644 if (!NT_STATUS_IS_OK(nt_status))
3645 return nt_status;
3647 b = srvsvc_pipe->binding_handle;
3649 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3651 union srvsvc_NetShareInfo info;
3652 struct srvsvc_NetShareInfo502 info502 =
3653 ctr_src.ctr.ctr502->array[i];
3655 /* reset error-code */
3656 nt_status = NT_STATUS_UNSUCCESSFUL;
3658 if (!check_share_sanity(c, cli, info502.name, info502.type))
3659 continue;
3661 /* finally add the share on the dst server */
3663 printf(_("migrating: [%s], path: %s, comment: %s, without "
3664 "share-ACLs\n"),
3665 info502.name, info502.path, info502.comment);
3667 info.info502 = &info502;
3669 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3670 srvsvc_pipe->desthost,
3671 502,
3672 &info,
3673 &parm_error,
3674 &result);
3675 if (!NT_STATUS_IS_OK(nt_status)) {
3676 printf(_("cannot add share: %s\n"),
3677 nt_errstr(nt_status));
3678 goto done;
3680 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3681 printf(_(" [%s] does already exist\n"),
3682 info502.name);
3683 continue;
3686 if (!W_ERROR_IS_OK(result)) {
3687 nt_status = werror_to_ntstatus(result);
3688 printf(_("cannot add share: %s\n"),
3689 win_errstr(result));
3690 goto done;
3695 nt_status = NT_STATUS_OK;
3697 done:
3698 if (cli_dst) {
3699 cli_shutdown(cli_dst);
3702 return nt_status;
3707 * Migrate shares from a RPC server to another.
3709 * @param argc Standard main() style argc.
3710 * @param argv Standard main() style argv. Initial components are already
3711 * stripped.
3713 * @return A shell status integer (0 for success).
3715 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3716 const char **argv)
3718 if (c->display_usage) {
3719 d_printf( "%s\n"
3720 "net rpc share migrate shares\n"
3721 " %s\n",
3722 _("Usage:"),
3723 _("Migrate shares to local server"));
3724 return 0;
3727 if (!c->opt_host) {
3728 printf(_("no server to migrate\n"));
3729 return -1;
3732 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3733 rpc_share_migrate_shares_internals,
3734 argc, argv);
3738 * Copy a file/dir
3740 * @param f file_info
3741 * @param mask current search mask
3742 * @param state arg-pointer
3745 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3746 const char *mask, void *state)
3748 static NTSTATUS nt_status;
3749 static struct copy_clistate *local_state;
3750 static fstring filename, new_mask;
3751 fstring dir;
3752 char *old_dir;
3753 struct net_context *c;
3755 local_state = (struct copy_clistate *)state;
3756 nt_status = NT_STATUS_UNSUCCESSFUL;
3758 c = local_state->c;
3760 if (strequal(f->name, ".") || strequal(f->name, ".."))
3761 return NT_STATUS_OK;
3763 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3765 /* DIRECTORY */
3766 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3768 DEBUG(3,("got dir: %s\n", f->name));
3770 fstrcpy(dir, local_state->cwd);
3771 fstrcat(dir, "\\");
3772 fstrcat(dir, f->name);
3774 switch (net_mode_share)
3776 case NET_MODE_SHARE_MIGRATE:
3777 /* create that directory */
3778 nt_status = net_copy_file(c, local_state->mem_ctx,
3779 local_state->cli_share_src,
3780 local_state->cli_share_dst,
3781 dir, dir,
3782 c->opt_acls? true : false,
3783 c->opt_attrs? true : false,
3784 c->opt_timestamps? true:false,
3785 false);
3786 break;
3787 default:
3788 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3789 return NT_STATUS_INTERNAL_ERROR;
3792 if (!NT_STATUS_IS_OK(nt_status)) {
3793 printf(_("could not handle dir %s: %s\n"),
3794 dir, nt_errstr(nt_status));
3795 return nt_status;
3798 /* search below that directory */
3799 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
3800 return NT_STATUS_NO_MEMORY;
3802 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
3803 return NT_STATUS_NO_MEMORY;
3806 old_dir = local_state->cwd;
3807 local_state->cwd = dir;
3808 nt_status = sync_files(local_state, new_mask);
3809 if (!NT_STATUS_IS_OK(nt_status)) {
3810 printf(_("could not handle files\n"));
3812 local_state->cwd = old_dir;
3814 return nt_status;
3818 /* FILE */
3819 fstrcpy(filename, local_state->cwd);
3820 fstrcat(filename, "\\");
3821 fstrcat(filename, f->name);
3823 DEBUG(3,("got file: %s\n", filename));
3825 switch (net_mode_share)
3827 case NET_MODE_SHARE_MIGRATE:
3828 nt_status = net_copy_file(c, local_state->mem_ctx,
3829 local_state->cli_share_src,
3830 local_state->cli_share_dst,
3831 filename, filename,
3832 c->opt_acls? true : false,
3833 c->opt_attrs? true : false,
3834 c->opt_timestamps? true: false,
3835 true);
3836 break;
3837 default:
3838 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3839 net_mode_share);
3840 return NT_STATUS_INTERNAL_ERROR;
3843 if (!NT_STATUS_IS_OK(nt_status))
3844 printf(_("could not handle file %s: %s\n"),
3845 filename, nt_errstr(nt_status));
3846 return nt_status;
3850 * sync files, can be called recursivly to list files
3851 * and then call copy_fn for each file
3853 * @param cp_clistate pointer to the copy_clistate we work with
3854 * @param mask the current search mask
3856 * @return Boolean result
3858 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3860 struct cli_state *targetcli;
3861 char *targetpath = NULL;
3862 NTSTATUS status;
3864 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3866 status = cli_resolve_path(talloc_tos(), "", NULL,
3867 cp_clistate->cli_share_src,
3868 mask, &targetcli, &targetpath);
3869 if (!NT_STATUS_IS_OK(status)) {
3870 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3871 "%s\n"),
3872 mask, nt_errstr(status));
3873 return status;
3876 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3877 copy_fn, cp_clistate);
3878 if (!NT_STATUS_IS_OK(status)) {
3879 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3880 mask, nt_errstr(status));
3883 return status;
3888 * Set the top level directory permissions before we do any further copies.
3889 * Should set up ACL inheritance.
3892 bool copy_top_level_perms(struct net_context *c,
3893 struct copy_clistate *cp_clistate,
3894 const char *sharename)
3896 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3898 switch (net_mode_share) {
3899 case NET_MODE_SHARE_MIGRATE:
3900 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3901 nt_status = net_copy_fileattr(c,
3902 cp_clistate->mem_ctx,
3903 cp_clistate->cli_share_src,
3904 cp_clistate->cli_share_dst,
3905 "\\", "\\",
3906 c->opt_acls? true : false,
3907 c->opt_attrs? true : false,
3908 c->opt_timestamps? true: false,
3909 false);
3910 break;
3911 default:
3912 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3913 break;
3916 if (!NT_STATUS_IS_OK(nt_status)) {
3917 printf(_("Could handle directory attributes for top level "
3918 "directory of share %s. Error %s\n"),
3919 sharename, nt_errstr(nt_status));
3920 return false;
3923 return true;
3927 * Sync all files inside a remote share to another share (over smb).
3929 * All parameters are provided by the run_rpc_command function, except for
3930 * argc, argv which are passed through.
3932 * @param domain_sid The domain sid acquired from the remote server.
3933 * @param cli A cli_state connected to the server.
3934 * @param mem_ctx Talloc context, destroyed on completion of the function.
3935 * @param argc Standard main() style argc.
3936 * @param argv Standard main() style argv. Initial components are already
3937 * stripped.
3939 * @return Normal NTSTATUS return.
3942 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3943 const struct dom_sid *domain_sid,
3944 const char *domain_name,
3945 struct cli_state *cli,
3946 struct rpc_pipe_client *pipe_hnd,
3947 TALLOC_CTX *mem_ctx,
3948 int argc,
3949 const char **argv)
3951 WERROR result;
3952 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3953 struct srvsvc_NetShareInfoCtr ctr_src;
3954 uint32 i;
3955 uint32 level = 502;
3956 struct copy_clistate cp_clistate;
3957 bool got_src_share = false;
3958 bool got_dst_share = false;
3959 const char *mask = "\\*";
3960 char *dst = NULL;
3962 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3963 if (dst == NULL) {
3964 nt_status = NT_STATUS_NO_MEMORY;
3965 goto done;
3968 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3969 &ctr_src);
3971 if (!W_ERROR_IS_OK(result))
3972 goto done;
3974 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3976 struct srvsvc_NetShareInfo502 info502 =
3977 ctr_src.ctr.ctr502->array[i];
3979 if (!check_share_sanity(c, cli, info502.name, info502.type))
3980 continue;
3982 /* one might not want to mirror whole discs :) */
3983 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3984 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3985 info502.name);
3986 continue;
3989 switch (net_mode_share)
3991 case NET_MODE_SHARE_MIGRATE:
3992 printf("syncing");
3993 break;
3994 default:
3995 d_fprintf(stderr, _("Unsupported mode %d\n"),
3996 net_mode_share);
3997 break;
3999 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4000 "Attributes %s\n"),
4001 info502.name,
4002 c->opt_acls ? _("including") : _("without"),
4003 c->opt_attrs ? _("including") : _("without"),
4004 c->opt_timestamps ? _("(preserving timestamps)") : "");
4006 cp_clistate.mem_ctx = mem_ctx;
4007 cp_clistate.cli_share_src = NULL;
4008 cp_clistate.cli_share_dst = NULL;
4009 cp_clistate.cwd = NULL;
4010 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4011 cp_clistate.c = c;
4013 /* open share source */
4014 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4015 smbXcli_conn_remote_sockaddr(cli->conn),
4016 smbXcli_conn_remote_name(cli->conn),
4017 info502.name, "A:");
4018 if (!NT_STATUS_IS_OK(nt_status))
4019 goto done;
4021 got_src_share = true;
4023 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4024 /* open share destination */
4025 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4026 NULL, dst, info502.name, "A:");
4027 if (!NT_STATUS_IS_OK(nt_status))
4028 goto done;
4030 got_dst_share = true;
4033 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4034 d_fprintf(stderr, _("Could not handle the top level "
4035 "directory permissions for the "
4036 "share: %s\n"), info502.name);
4037 nt_status = NT_STATUS_UNSUCCESSFUL;
4038 goto done;
4041 nt_status = sync_files(&cp_clistate, mask);
4042 if (!NT_STATUS_IS_OK(nt_status)) {
4043 d_fprintf(stderr, _("could not handle files for share: "
4044 "%s\n"), info502.name);
4045 goto done;
4049 nt_status = NT_STATUS_OK;
4051 done:
4053 if (got_src_share)
4054 cli_shutdown(cp_clistate.cli_share_src);
4056 if (got_dst_share)
4057 cli_shutdown(cp_clistate.cli_share_dst);
4059 SAFE_FREE(dst);
4060 return nt_status;
4064 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4066 if (c->display_usage) {
4067 d_printf( "%s\n"
4068 "net share migrate files\n"
4069 " %s\n",
4070 _("Usage:"),
4071 _("Migrate files to local server"));
4072 return 0;
4075 if (!c->opt_host) {
4076 d_printf(_("no server to migrate\n"));
4077 return -1;
4080 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4081 rpc_share_migrate_files_internals,
4082 argc, argv);
4086 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4088 * All parameters are provided by the run_rpc_command function, except for
4089 * argc, argv which are passed through.
4091 * @param domain_sid The domain sid acquired from the remote server.
4092 * @param cli A cli_state connected to the server.
4093 * @param mem_ctx Talloc context, destroyed on completion of the function.
4094 * @param argc Standard main() style argc.
4095 * @param argv Standard main() style argv. Initial components are already
4096 * stripped.
4098 * @return Normal NTSTATUS return.
4101 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4102 const struct dom_sid *domain_sid,
4103 const char *domain_name,
4104 struct cli_state *cli,
4105 struct rpc_pipe_client *pipe_hnd,
4106 TALLOC_CTX *mem_ctx,
4107 int argc,
4108 const char **argv)
4110 WERROR result;
4111 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4112 struct srvsvc_NetShareInfoCtr ctr_src;
4113 union srvsvc_NetShareInfo info;
4114 uint32 i;
4115 struct rpc_pipe_client *srvsvc_pipe = NULL;
4116 struct cli_state *cli_dst = NULL;
4117 uint32 level = 502; /* includes secdesc */
4118 uint32_t parm_error = 0;
4119 struct dcerpc_binding_handle *b;
4121 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4122 &ctr_src);
4124 if (!W_ERROR_IS_OK(result))
4125 goto done;
4127 /* connect destination PI_SRVSVC */
4128 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4129 &ndr_table_srvsvc.syntax_id);
4130 if (!NT_STATUS_IS_OK(nt_status))
4131 return nt_status;
4133 b = srvsvc_pipe->binding_handle;
4135 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4137 struct srvsvc_NetShareInfo502 info502 =
4138 ctr_src.ctr.ctr502->array[i];
4140 /* reset error-code */
4141 nt_status = NT_STATUS_UNSUCCESSFUL;
4143 if (!check_share_sanity(c, cli, info502.name, info502.type))
4144 continue;
4146 printf(_("migrating: [%s], path: %s, comment: %s, including "
4147 "share-ACLs\n"),
4148 info502.name, info502.path, info502.comment);
4150 if (c->opt_verbose)
4151 display_sec_desc(info502.sd_buf.sd);
4153 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4154 info.info502 = &info502;
4156 /* finally modify the share on the dst server */
4157 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4158 srvsvc_pipe->desthost,
4159 info502.name,
4160 level,
4161 &info,
4162 &parm_error,
4163 &result);
4164 if (!NT_STATUS_IS_OK(nt_status)) {
4165 printf(_("cannot set share-acl: %s\n"),
4166 nt_errstr(nt_status));
4167 goto done;
4169 if (!W_ERROR_IS_OK(result)) {
4170 nt_status = werror_to_ntstatus(result);
4171 printf(_("cannot set share-acl: %s\n"),
4172 win_errstr(result));
4173 goto done;
4178 nt_status = NT_STATUS_OK;
4180 done:
4181 if (cli_dst) {
4182 cli_shutdown(cli_dst);
4185 return nt_status;
4190 * Migrate share-acls from a RPC server to another.
4192 * @param argc Standard main() style argc.
4193 * @param argv Standard main() style argv. Initial components are already
4194 * stripped.
4196 * @return A shell status integer (0 for success).
4198 static int rpc_share_migrate_security(struct net_context *c, int argc,
4199 const char **argv)
4201 if (c->display_usage) {
4202 d_printf( "%s\n"
4203 "net rpc share migrate security\n"
4204 " %s\n",
4205 _("Usage:"),
4206 _("Migrate share-acls to local server"));
4207 return 0;
4210 if (!c->opt_host) {
4211 d_printf(_("no server to migrate\n"));
4212 return -1;
4215 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4216 rpc_share_migrate_security_internals,
4217 argc, argv);
4221 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4222 * from one server to another.
4224 * @param argc Standard main() style argc.
4225 * @param argv Standard main() style argv. Initial components are already
4226 * stripped.
4228 * @return A shell status integer (0 for success).
4231 static int rpc_share_migrate_all(struct net_context *c, int argc,
4232 const char **argv)
4234 int ret;
4236 if (c->display_usage) {
4237 d_printf( "%s\n"
4238 "net rpc share migrate all\n"
4239 " %s\n",
4240 _("Usage:"),
4241 _("Migrates shares including all share settings"));
4242 return 0;
4245 if (!c->opt_host) {
4246 d_printf(_("no server to migrate\n"));
4247 return -1;
4250 /* order is important. we don't want to be locked out by the share-acl
4251 * before copying files - gd */
4253 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4254 rpc_share_migrate_shares_internals, argc, argv);
4255 if (ret)
4256 return ret;
4258 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4259 rpc_share_migrate_files_internals, argc, argv);
4260 if (ret)
4261 return ret;
4263 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4264 rpc_share_migrate_security_internals, argc,
4265 argv);
4270 * 'net rpc share migrate' entrypoint.
4271 * @param argc Standard main() style argc.
4272 * @param argv Standard main() style argv. Initial components are already
4273 * stripped.
4275 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4278 struct functable func[] = {
4280 "all",
4281 rpc_share_migrate_all,
4282 NET_TRANSPORT_RPC,
4283 N_("Migrate shares from remote to local server"),
4284 N_("net rpc share migrate all\n"
4285 " Migrate shares from remote to local server")
4288 "files",
4289 rpc_share_migrate_files,
4290 NET_TRANSPORT_RPC,
4291 N_("Migrate files from remote to local server"),
4292 N_("net rpc share migrate files\n"
4293 " Migrate files from remote to local server")
4296 "security",
4297 rpc_share_migrate_security,
4298 NET_TRANSPORT_RPC,
4299 N_("Migrate share-ACLs from remote to local server"),
4300 N_("net rpc share migrate security\n"
4301 " Migrate share-ACLs from remote to local server")
4304 "shares",
4305 rpc_share_migrate_shares,
4306 NET_TRANSPORT_RPC,
4307 N_("Migrate shares from remote to local server"),
4308 N_("net rpc share migrate shares\n"
4309 " Migrate shares from remote to local server")
4311 {NULL, NULL, 0, NULL, NULL}
4314 net_mode_share = NET_MODE_SHARE_MIGRATE;
4316 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4319 struct full_alias {
4320 struct dom_sid sid;
4321 uint32 num_members;
4322 struct dom_sid *members;
4325 static int num_server_aliases;
4326 static struct full_alias *server_aliases;
4329 * Add an alias to the static list.
4331 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4333 if (server_aliases == NULL)
4334 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4336 server_aliases[num_server_aliases] = *alias;
4337 num_server_aliases += 1;
4341 * For a specific domain on the server, fetch all the aliases
4342 * and their members. Add all of them to the server_aliases.
4345 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4346 TALLOC_CTX *mem_ctx,
4347 struct policy_handle *connect_pol,
4348 const struct dom_sid *domain_sid)
4350 uint32 start_idx, max_entries, num_entries, i;
4351 struct samr_SamArray *groups = NULL;
4352 NTSTATUS result, status;
4353 struct policy_handle domain_pol;
4354 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4356 /* Get domain policy handle */
4358 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4359 connect_pol,
4360 MAXIMUM_ALLOWED_ACCESS,
4361 discard_const_p(struct dom_sid2, domain_sid),
4362 &domain_pol,
4363 &result);
4364 if (!NT_STATUS_IS_OK(status)) {
4365 return status;
4367 if (!NT_STATUS_IS_OK(result)) {
4368 return result;
4371 start_idx = 0;
4372 max_entries = 250;
4374 do {
4375 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4376 &domain_pol,
4377 &start_idx,
4378 &groups,
4379 max_entries,
4380 &num_entries,
4381 &result);
4382 if (!NT_STATUS_IS_OK(status)) {
4383 goto done;
4385 for (i = 0; i < num_entries; i++) {
4387 struct policy_handle alias_pol;
4388 struct full_alias alias;
4389 struct lsa_SidArray sid_array;
4390 int j;
4391 NTSTATUS _result;
4393 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4394 &domain_pol,
4395 MAXIMUM_ALLOWED_ACCESS,
4396 groups->entries[i].idx,
4397 &alias_pol,
4398 &_result);
4399 if (!NT_STATUS_IS_OK(status)) {
4400 goto done;
4402 if (!NT_STATUS_IS_OK(_result)) {
4403 status = _result;
4404 goto done;
4407 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4408 &alias_pol,
4409 &sid_array,
4410 &_result);
4411 if (!NT_STATUS_IS_OK(status)) {
4412 goto done;
4414 if (!NT_STATUS_IS_OK(_result)) {
4415 status = _result;
4416 goto done;
4419 alias.num_members = sid_array.num_sids;
4421 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4422 if (!NT_STATUS_IS_OK(status)) {
4423 goto done;
4425 if (!NT_STATUS_IS_OK(_result)) {
4426 status = _result;
4427 goto done;
4430 alias.members = NULL;
4432 if (alias.num_members > 0) {
4433 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4435 for (j = 0; j < alias.num_members; j++)
4436 sid_copy(&alias.members[j],
4437 sid_array.sids[j].sid);
4440 sid_compose(&alias.sid, domain_sid,
4441 groups->entries[i].idx);
4443 push_alias(mem_ctx, &alias);
4445 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4447 status = NT_STATUS_OK;
4449 done:
4450 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4452 return status;
4456 * Dump server_aliases as names for debugging purposes.
4459 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4460 const struct dom_sid *domain_sid,
4461 const char *domain_name,
4462 struct cli_state *cli,
4463 struct rpc_pipe_client *pipe_hnd,
4464 TALLOC_CTX *mem_ctx,
4465 int argc,
4466 const char **argv)
4468 int i;
4469 NTSTATUS result;
4470 struct policy_handle lsa_pol;
4471 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4473 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4474 SEC_FLAG_MAXIMUM_ALLOWED,
4475 &lsa_pol);
4476 if (!NT_STATUS_IS_OK(result))
4477 return result;
4479 for (i=0; i<num_server_aliases; i++) {
4480 char **names;
4481 char **domains;
4482 enum lsa_SidType *types;
4483 int j;
4485 struct full_alias *alias = &server_aliases[i];
4487 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4488 &alias->sid,
4489 &domains, &names, &types);
4490 if (!NT_STATUS_IS_OK(result))
4491 continue;
4493 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4495 if (alias->num_members == 0) {
4496 DEBUG(1, ("\n"));
4497 continue;
4500 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4501 alias->num_members,
4502 alias->members,
4503 &domains, &names, &types);
4505 if (!NT_STATUS_IS_OK(result) &&
4506 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4507 continue;
4509 for (j=0; j<alias->num_members; j++)
4510 DEBUG(1, ("%s\\%s (%d); ",
4511 domains[j] ? domains[j] : "*unknown*",
4512 names[j] ? names[j] : "*unknown*",types[j]));
4513 DEBUG(1, ("\n"));
4516 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4518 return NT_STATUS_OK;
4522 * Fetch a list of all server aliases and their members into
4523 * server_aliases.
4526 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4527 const struct dom_sid *domain_sid,
4528 const char *domain_name,
4529 struct cli_state *cli,
4530 struct rpc_pipe_client *pipe_hnd,
4531 TALLOC_CTX *mem_ctx,
4532 int argc,
4533 const char **argv)
4535 NTSTATUS result, status;
4536 struct policy_handle connect_pol;
4537 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4539 status = dcerpc_samr_Connect2(b, mem_ctx,
4540 pipe_hnd->desthost,
4541 MAXIMUM_ALLOWED_ACCESS,
4542 &connect_pol,
4543 &result);
4544 if (!NT_STATUS_IS_OK(status)) {
4545 goto done;
4547 if (!NT_STATUS_IS_OK(result)) {
4548 status = result;
4549 goto done;
4552 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4553 &global_sid_Builtin);
4554 if (!NT_STATUS_IS_OK(status)) {
4555 goto done;
4558 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4559 domain_sid);
4561 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4562 done:
4563 return status;
4566 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4568 token->num_sids = 4;
4570 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4571 d_fprintf(stderr, "malloc %s\n",_("failed"));
4572 token->num_sids = 0;
4573 return;
4576 token->sids[0] = *user_sid;
4577 sid_copy(&token->sids[1], &global_sid_World);
4578 sid_copy(&token->sids[2], &global_sid_Network);
4579 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4582 static void free_user_token(struct security_token *token)
4584 SAFE_FREE(token->sids);
4587 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4589 if (security_token_has_sid(token, sid))
4590 return;
4592 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4593 if (!token->sids) {
4594 return;
4597 sid_copy(&token->sids[token->num_sids], sid);
4599 token->num_sids += 1;
4602 struct user_token {
4603 fstring name;
4604 struct security_token token;
4607 static void dump_user_token(struct user_token *token)
4609 int i;
4611 d_printf("%s\n", token->name);
4613 for (i=0; i<token->token.num_sids; i++) {
4614 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4618 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4620 int i;
4622 for (i=0; i<alias->num_members; i++) {
4623 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4624 return true;
4627 return false;
4630 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4632 int i;
4634 for (i=0; i<num_server_aliases; i++) {
4635 if (is_alias_member(&sid, &server_aliases[i]))
4636 add_sid_to_token(token, &server_aliases[i].sid);
4641 * We got a user token with all the SIDs we can know about without asking the
4642 * server directly. These are the user and domain group sids. All of these can
4643 * be members of aliases. So scan the list of aliases for each of the SIDs and
4644 * add them to the token.
4647 static void collect_alias_memberships(struct security_token *token)
4649 int num_global_sids = token->num_sids;
4650 int i;
4652 for (i=0; i<num_global_sids; i++) {
4653 collect_sid_memberships(token, token->sids[i]);
4657 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4659 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4660 enum wbcSidType type;
4661 fstring full_name;
4662 struct wbcDomainSid wsid;
4663 char sid_str[WBC_SID_STRING_BUFLEN];
4664 struct dom_sid user_sid;
4665 uint32_t num_groups;
4666 gid_t *groups = NULL;
4667 uint32_t i;
4669 fstr_sprintf(full_name, "%s%c%s",
4670 domain, *lp_winbind_separator(), user);
4672 /* First let's find out the user sid */
4674 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4676 if (!WBC_ERROR_IS_OK(wbc_status)) {
4677 DEBUG(1, ("winbind could not find %s: %s\n",
4678 full_name, wbcErrorString(wbc_status)));
4679 return false;
4682 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4684 if (type != WBC_SID_NAME_USER) {
4685 DEBUG(1, ("%s is not a user\n", full_name));
4686 return false;
4689 if (!string_to_sid(&user_sid, sid_str)) {
4690 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4691 return false;
4694 init_user_token(token, &user_sid);
4696 /* And now the groups winbind knows about */
4698 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4699 if (!WBC_ERROR_IS_OK(wbc_status)) {
4700 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4701 full_name, wbcErrorString(wbc_status)));
4702 return false;
4705 for (i = 0; i < num_groups; i++) {
4706 gid_t gid = groups[i];
4707 struct dom_sid sid;
4709 wbc_status = wbcGidToSid(gid, &wsid);
4710 if (!WBC_ERROR_IS_OK(wbc_status)) {
4711 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4712 (unsigned int)gid, wbcErrorString(wbc_status)));
4713 wbcFreeMemory(groups);
4714 return false;
4717 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4719 DEBUG(3, (" %s\n", sid_str));
4721 string_to_sid(&sid, sid_str);
4722 add_sid_to_token(token, &sid);
4724 wbcFreeMemory(groups);
4726 return true;
4730 * Get a list of all user tokens we want to look at
4733 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4734 struct user_token **user_tokens)
4736 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4737 uint32_t i, num_users;
4738 const char **users;
4739 struct user_token *result;
4740 TALLOC_CTX *frame = NULL;
4742 if (lp_winbind_use_default_domain() &&
4743 (c->opt_target_workgroup == NULL)) {
4744 d_fprintf(stderr, _("winbind use default domain = yes set, "
4745 "please specify a workgroup\n"));
4746 return false;
4749 /* Send request to winbind daemon */
4751 wbc_status = wbcListUsers(NULL, &num_users, &users);
4752 if (!WBC_ERROR_IS_OK(wbc_status)) {
4753 DEBUG(1, (_("winbind could not list users: %s\n"),
4754 wbcErrorString(wbc_status)));
4755 return false;
4758 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4760 if (result == NULL) {
4761 DEBUG(1, ("Could not malloc sid array\n"));
4762 wbcFreeMemory(users);
4763 return false;
4766 frame = talloc_stackframe();
4767 for (i=0; i < num_users; i++) {
4768 fstring domain, user;
4769 char *p;
4771 fstrcpy(result[i].name, users[i]);
4773 p = strchr(users[i], *lp_winbind_separator());
4775 DEBUG(3, ("%s\n", users[i]));
4777 if (p == NULL) {
4778 fstrcpy(domain, c->opt_target_workgroup);
4779 fstrcpy(user, users[i]);
4780 } else {
4781 *p++ = '\0';
4782 fstrcpy(domain, users[i]);
4783 if (!strupper_m(domain)) {
4784 DEBUG(1, ("strupper_m %s failed\n", domain));
4785 wbcFreeMemory(users);
4786 return false;
4788 fstrcpy(user, p);
4791 get_user_sids(domain, user, &(result[i].token));
4793 TALLOC_FREE(frame);
4794 wbcFreeMemory(users);
4796 *num_tokens = num_users;
4797 *user_tokens = result;
4799 return true;
4802 static bool get_user_tokens_from_file(FILE *f,
4803 int *num_tokens,
4804 struct user_token **tokens)
4806 struct user_token *token = NULL;
4808 while (!feof(f)) {
4809 fstring line;
4811 if (fgets(line, sizeof(line)-1, f) == NULL) {
4812 return true;
4815 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4816 line[strlen(line)-1] = '\0';
4819 if (line[0] == ' ') {
4820 /* We have a SID */
4822 struct dom_sid sid;
4823 if(!string_to_sid(&sid, &line[1])) {
4824 DEBUG(1,("get_user_tokens_from_file: Could "
4825 "not convert sid %s \n",&line[1]));
4826 return false;
4829 if (token == NULL) {
4830 DEBUG(0, ("File does not begin with username"));
4831 return false;
4834 add_sid_to_token(&token->token, &sid);
4835 continue;
4838 /* And a new user... */
4840 *num_tokens += 1;
4841 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4842 if (*tokens == NULL) {
4843 DEBUG(0, ("Could not realloc tokens\n"));
4844 return false;
4847 token = &((*tokens)[*num_tokens-1]);
4849 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
4850 return false;
4852 token->token.num_sids = 0;
4853 token->token.sids = NULL;
4854 continue;
4857 return false;
4862 * Show the list of all users that have access to a share
4865 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4866 TALLOC_CTX *mem_ctx,
4867 const char *netname,
4868 int num_tokens,
4869 struct user_token *tokens)
4871 uint16_t fnum;
4872 struct security_descriptor *share_sd = NULL;
4873 struct security_descriptor *root_sd = NULL;
4874 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4875 int i;
4876 union srvsvc_NetShareInfo info;
4877 WERROR result;
4878 NTSTATUS status;
4879 uint16 cnum;
4880 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4882 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4883 pipe_hnd->desthost,
4884 netname,
4885 502,
4886 &info,
4887 &result);
4889 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4890 DEBUG(1, ("Coult not query secdesc for share %s\n",
4891 netname));
4892 return;
4895 share_sd = info.info502->sd_buf.sd;
4896 if (share_sd == NULL) {
4897 DEBUG(1, ("Got no secdesc for share %s\n",
4898 netname));
4901 cnum = cli_state_get_tid(cli);
4903 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", "", 0))) {
4904 return;
4907 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4908 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4909 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
4912 for (i=0; i<num_tokens; i++) {
4913 uint32 acc_granted;
4915 if (share_sd != NULL) {
4916 status = se_access_check(share_sd, &tokens[i].token,
4917 1, &acc_granted);
4919 if (!NT_STATUS_IS_OK(status)) {
4920 DEBUG(1, ("Could not check share_sd for "
4921 "user %s\n",
4922 tokens[i].name));
4923 continue;
4927 if (root_sd == NULL) {
4928 d_printf(" %s\n", tokens[i].name);
4929 continue;
4932 status = se_access_check(root_sd, &tokens[i].token,
4933 1, &acc_granted);
4934 if (!NT_STATUS_IS_OK(status)) {
4935 DEBUG(1, ("Could not check root_sd for user %s\n",
4936 tokens[i].name));
4937 continue;
4939 d_printf(" %s\n", tokens[i].name);
4942 if (fnum != (uint16_t)-1)
4943 cli_close(cli, fnum);
4944 cli_tdis(cli);
4945 cli_state_set_tid(cli, cnum);
4947 return;
4951 * List shares on a remote RPC server, including the security descriptors.
4953 * All parameters are provided by the run_rpc_command function, except for
4954 * argc, argv which are passed through.
4956 * @param domain_sid The domain sid acquired from the remote server.
4957 * @param cli A cli_state connected to the server.
4958 * @param mem_ctx Talloc context, destroyed on completion of the function.
4959 * @param argc Standard main() style argc.
4960 * @param argv Standard main() style argv. Initial components are already
4961 * stripped.
4963 * @return Normal NTSTATUS return.
4966 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4967 const struct dom_sid *domain_sid,
4968 const char *domain_name,
4969 struct cli_state *cli,
4970 struct rpc_pipe_client *pipe_hnd,
4971 TALLOC_CTX *mem_ctx,
4972 int argc,
4973 const char **argv)
4975 bool r;
4976 FILE *f;
4977 NTSTATUS nt_status = NT_STATUS_OK;
4978 uint32_t total_entries = 0;
4979 uint32_t resume_handle = 0;
4980 uint32_t preferred_len = 0xffffffff;
4981 uint32_t i;
4982 struct dcerpc_binding_handle *b = NULL;
4983 struct srvsvc_NetShareInfoCtr info_ctr;
4984 struct srvsvc_NetShareCtr1 ctr1;
4985 WERROR result;
4987 struct user_token *tokens = NULL;
4988 int num_tokens = 0;
4990 if (argc == 0) {
4991 f = stdin;
4992 } else {
4993 f = fopen(argv[0], "r");
4996 if (f == NULL) {
4997 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4998 return NT_STATUS_UNSUCCESSFUL;
5001 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5003 if (f != stdin)
5004 fclose(f);
5006 if (!r) {
5007 DEBUG(0, ("Could not read users from file\n"));
5008 return NT_STATUS_UNSUCCESSFUL;
5011 for (i=0; i<num_tokens; i++)
5012 collect_alias_memberships(&tokens[i].token);
5014 ZERO_STRUCT(info_ctr);
5015 ZERO_STRUCT(ctr1);
5017 info_ctr.level = 1;
5018 info_ctr.ctr.ctr1 = &ctr1;
5020 b = pipe_hnd->binding_handle;
5022 /* Issue the NetShareEnum RPC call and retrieve the response */
5023 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5024 talloc_tos(),
5025 pipe_hnd->desthost,
5026 &info_ctr,
5027 preferred_len,
5028 &total_entries,
5029 &resume_handle,
5030 &result);
5032 /* Was it successful? */
5033 if (!NT_STATUS_IS_OK(nt_status)) {
5034 /* Nope. Go clean up. */
5035 goto done;
5038 if (!W_ERROR_IS_OK(result)) {
5039 /* Nope. Go clean up. */
5040 nt_status = werror_to_ntstatus(result);
5041 goto done;
5044 if (total_entries == 0) {
5045 goto done;
5048 /* For each returned entry... */
5049 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5050 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5052 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5053 continue;
5056 d_printf("%s\n", netname);
5058 show_userlist(pipe_hnd, mem_ctx, netname,
5059 num_tokens, tokens);
5061 done:
5062 for (i=0; i<num_tokens; i++) {
5063 free_user_token(&tokens[i].token);
5065 SAFE_FREE(tokens);
5067 return nt_status;
5070 static int rpc_share_allowedusers(struct net_context *c, int argc,
5071 const char **argv)
5073 int result;
5075 if (c->display_usage) {
5076 d_printf( "%s\n"
5077 "net rpc share allowedusers\n"
5078 " %s\n",
5079 _("Usage:"),
5080 _("List allowed users"));
5081 return 0;
5084 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5085 rpc_aliaslist_internals,
5086 argc, argv);
5087 if (result != 0)
5088 return result;
5090 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5091 rpc_aliaslist_dump,
5092 argc, argv);
5093 if (result != 0)
5094 return result;
5096 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5097 rpc_share_allowedusers_internals,
5098 argc, argv);
5101 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5103 int num_tokens = 0;
5104 struct user_token *tokens = NULL;
5105 int i;
5107 if (argc != 0) {
5108 net_usersidlist_usage(c, argc, argv);
5109 return 0;
5112 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5113 DEBUG(0, ("Could not get the user/sid list\n"));
5114 return -1;
5117 for (i=0; i<num_tokens; i++) {
5118 dump_user_token(&tokens[i]);
5119 free_user_token(&tokens[i].token);
5122 SAFE_FREE(tokens);
5123 return 0;
5126 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5128 d_printf(_("net usersidlist\n"
5129 "\tprints out a list of all users the running winbind knows\n"
5130 "\tabout, together with all their SIDs. This is used as\n"
5131 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5133 net_common_flags_usage(c, argc, argv);
5134 return -1;
5138 * 'net rpc share' entrypoint.
5139 * @param argc Standard main() style argc.
5140 * @param argv Standard main() style argv. Initial components are already
5141 * stripped.
5144 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5146 NET_API_STATUS status;
5148 struct functable func[] = {
5150 "add",
5151 rpc_share_add,
5152 NET_TRANSPORT_RPC,
5153 N_("Add share"),
5154 N_("net rpc share add\n"
5155 " Add share")
5158 "delete",
5159 rpc_share_delete,
5160 NET_TRANSPORT_RPC,
5161 N_("Remove share"),
5162 N_("net rpc share delete\n"
5163 " Remove share")
5166 "allowedusers",
5167 rpc_share_allowedusers,
5168 NET_TRANSPORT_RPC,
5169 N_("Modify allowed users"),
5170 N_("net rpc share allowedusers\n"
5171 " Modify allowed users")
5174 "migrate",
5175 rpc_share_migrate,
5176 NET_TRANSPORT_RPC,
5177 N_("Migrate share to local server"),
5178 N_("net rpc share migrate\n"
5179 " Migrate share to local server")
5182 "list",
5183 rpc_share_list,
5184 NET_TRANSPORT_RPC,
5185 N_("List shares"),
5186 N_("net rpc share list\n"
5187 " List shares")
5189 {NULL, NULL, 0, NULL, NULL}
5192 status = libnetapi_net_init(&c->netapi_ctx);
5193 if (status != 0) {
5194 return -1;
5196 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5197 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5198 if (c->opt_kerberos) {
5199 libnetapi_set_use_kerberos(c->netapi_ctx);
5202 if (argc == 0) {
5203 if (c->display_usage) {
5204 d_printf("%s\n%s",
5205 _("Usage:"),
5206 _("net rpc share\n"
5207 " List shares\n"
5208 " Alias for net rpc share list\n"));
5209 net_display_usage_from_functable(func);
5210 return 0;
5213 return rpc_share_list(c, argc, argv);
5216 return net_run_function(c, argc, argv, "net rpc share", func);
5219 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5220 TALLOC_CTX *mem_ctx,
5221 struct rpc_sh_ctx *ctx,
5222 struct rpc_pipe_client *pipe_hnd,
5223 int argc, const char **argv)
5226 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5229 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5230 TALLOC_CTX *mem_ctx,
5231 struct rpc_sh_ctx *ctx,
5232 struct rpc_pipe_client *pipe_hnd,
5233 int argc, const char **argv)
5235 NET_API_STATUS status;
5236 uint32_t parm_err = 0;
5237 struct SHARE_INFO_2 i2;
5239 if ((argc < 2) || (argc > 3)) {
5240 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5241 ctx->whoami);
5242 return NT_STATUS_INVALID_PARAMETER;
5245 i2.shi2_netname = argv[0];
5246 i2.shi2_type = STYPE_DISKTREE;
5247 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5248 i2.shi2_permissions = 0;
5249 i2.shi2_max_uses = 0;
5250 i2.shi2_current_uses = 0;
5251 i2.shi2_path = argv[1];
5252 i2.shi2_passwd = NULL;
5254 status = NetShareAdd(pipe_hnd->desthost,
5256 (uint8_t *)&i2,
5257 &parm_err);
5259 return werror_to_ntstatus(W_ERROR(status));
5262 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5263 TALLOC_CTX *mem_ctx,
5264 struct rpc_sh_ctx *ctx,
5265 struct rpc_pipe_client *pipe_hnd,
5266 int argc, const char **argv)
5268 if (argc != 1) {
5269 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5270 return NT_STATUS_INVALID_PARAMETER;
5273 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5276 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5277 TALLOC_CTX *mem_ctx,
5278 struct rpc_sh_ctx *ctx,
5279 struct rpc_pipe_client *pipe_hnd,
5280 int argc, const char **argv)
5282 union srvsvc_NetShareInfo info;
5283 WERROR result;
5284 NTSTATUS status;
5285 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5287 if (argc != 1) {
5288 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5289 return NT_STATUS_INVALID_PARAMETER;
5292 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5293 pipe_hnd->desthost,
5294 argv[0],
5296 &info,
5297 &result);
5298 if (!NT_STATUS_IS_OK(status)) {
5299 result = ntstatus_to_werror(status);
5300 goto done;
5302 if (!W_ERROR_IS_OK(result)) {
5303 goto done;
5306 d_printf(_("Name: %s\n"), info.info2->name);
5307 d_printf(_("Comment: %s\n"), info.info2->comment);
5308 d_printf(_("Path: %s\n"), info.info2->path);
5309 d_printf(_("Password: %s\n"), info.info2->password);
5311 done:
5312 return werror_to_ntstatus(result);
5315 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5316 struct rpc_sh_ctx *ctx)
5318 static struct rpc_sh_cmd cmds[] = {
5320 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5321 N_("List available shares") },
5323 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5324 N_("Add a share") },
5326 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5327 N_("Delete a share") },
5329 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5330 N_("Get information about a share") },
5332 { NULL, NULL, 0, NULL, NULL }
5335 return cmds;
5338 /****************************************************************************/
5340 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5342 return net_file_usage(c, argc, argv);
5346 * Close a file on a remote RPC server.
5348 * @param argc Standard main() style argc.
5349 * @param argv Standard main() style argv. Initial components are already
5350 * stripped.
5352 * @return A shell status integer (0 for success).
5354 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5356 if (argc < 1 || c->display_usage) {
5357 return rpc_file_usage(c, argc, argv);
5360 return NetFileClose(c->opt_host, atoi(argv[0]));
5364 * Formatted print of open file info
5366 * @param r struct FILE_INFO_3 contents
5369 static void display_file_info_3(struct FILE_INFO_3 *r)
5371 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5372 r->fi3_id, r->fi3_username, r->fi3_permissions,
5373 r->fi3_num_locks, r->fi3_pathname);
5377 * List files for a user on a remote RPC server.
5379 * @param argc Standard main() style argc.
5380 * @param argv Standard main() style argv. Initial components are already
5381 * stripped.
5383 * @return A shell status integer (0 for success)..
5386 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5388 NET_API_STATUS status;
5389 uint32 preferred_len = 0xffffffff, i;
5390 const char *username=NULL;
5391 uint32_t total_entries = 0;
5392 uint32_t entries_read = 0;
5393 uint32_t resume_handle = 0;
5394 struct FILE_INFO_3 *i3 = NULL;
5396 if (c->display_usage) {
5397 return rpc_file_usage(c, argc, argv);
5400 /* if argc > 0, must be user command */
5401 if (argc > 0) {
5402 username = smb_xstrdup(argv[0]);
5405 status = NetFileEnum(c->opt_host,
5406 NULL,
5407 username,
5409 (uint8_t **)(void *)&i3,
5410 preferred_len,
5411 &entries_read,
5412 &total_entries,
5413 &resume_handle);
5415 if (status != 0) {
5416 goto done;
5419 /* Display results */
5421 d_printf(_(
5422 "\nEnumerating open files on remote server:\n\n"
5423 "\nFileId Opened by Perms Locks Path"
5424 "\n------ --------- ----- ----- ---- \n"));
5425 for (i = 0; i < entries_read; i++) {
5426 display_file_info_3(&i3[i]);
5428 done:
5429 return status;
5433 * 'net rpc file' entrypoint.
5434 * @param argc Standard main() style argc.
5435 * @param argv Standard main() style argv. Initial components are already
5436 * stripped.
5439 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5441 NET_API_STATUS status;
5443 struct functable func[] = {
5445 "close",
5446 rpc_file_close,
5447 NET_TRANSPORT_RPC,
5448 N_("Close opened file"),
5449 N_("net rpc file close\n"
5450 " Close opened file")
5453 "user",
5454 rpc_file_user,
5455 NET_TRANSPORT_RPC,
5456 N_("List files opened by user"),
5457 N_("net rpc file user\n"
5458 " List files opened by user")
5460 #if 0
5462 "info",
5463 rpc_file_info,
5464 NET_TRANSPORT_RPC,
5465 N_("Display information about opened file"),
5466 N_("net rpc file info\n"
5467 " Display information about opened file")
5469 #endif
5470 {NULL, NULL, 0, NULL, NULL}
5473 status = libnetapi_net_init(&c->netapi_ctx);
5474 if (status != 0) {
5475 return -1;
5477 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5478 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5479 if (c->opt_kerberos) {
5480 libnetapi_set_use_kerberos(c->netapi_ctx);
5483 if (argc == 0) {
5484 if (c->display_usage) {
5485 d_printf(_("Usage:\n"));
5486 d_printf(_("net rpc file\n"
5487 " List opened files\n"));
5488 net_display_usage_from_functable(func);
5489 return 0;
5492 return rpc_file_user(c, argc, argv);
5495 return net_run_function(c, argc, argv, "net rpc file", func);
5499 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5501 * All parameters are provided by the run_rpc_command function, except for
5502 * argc, argv which are passed through.
5504 * @param c A net_context structure.
5505 * @param domain_sid The domain sid acquired from the remote server.
5506 * @param cli A cli_state connected to the server.
5507 * @param mem_ctx Talloc context, destroyed on completion of the function.
5508 * @param argc Standard main() style argc.
5509 * @param argv Standard main() style argv. Initial components are already
5510 * stripped.
5512 * @return Normal NTSTATUS return.
5515 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5516 const struct dom_sid *domain_sid,
5517 const char *domain_name,
5518 struct cli_state *cli,
5519 struct rpc_pipe_client *pipe_hnd,
5520 TALLOC_CTX *mem_ctx,
5521 int argc,
5522 const char **argv)
5524 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5525 WERROR result;
5526 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5528 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5529 if (!NT_STATUS_IS_OK(status)) {
5530 return status;
5532 if (W_ERROR_IS_OK(result)) {
5533 d_printf(_("\nShutdown successfully aborted\n"));
5534 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5535 } else
5536 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5538 return werror_to_ntstatus(result);
5542 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5544 * All parameters are provided by the run_rpc_command function, except for
5545 * argc, argv which are passed through.
5547 * @param c A net_context structure.
5548 * @param domain_sid The domain sid acquired from the remote server.
5549 * @param cli A cli_state connected to the server.
5550 * @param mem_ctx Talloc context, destroyed on completion of the function.
5551 * @param argc Standard main() style argc.
5552 * @param argv Standard main() style argv. Initial components are already
5553 * stripped.
5555 * @return Normal NTSTATUS return.
5558 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5559 const struct dom_sid *domain_sid,
5560 const char *domain_name,
5561 struct cli_state *cli,
5562 struct rpc_pipe_client *pipe_hnd,
5563 TALLOC_CTX *mem_ctx,
5564 int argc,
5565 const char **argv)
5567 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5568 WERROR werr;
5569 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5571 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5573 if (!NT_STATUS_IS_OK(result)) {
5574 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5575 return result;
5577 if (W_ERROR_IS_OK(werr)) {
5578 d_printf(_("\nShutdown successfully aborted\n"));
5579 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5580 } else
5581 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5583 return werror_to_ntstatus(werr);
5587 * ABORT the shutdown of a remote RPC server.
5589 * @param argc Standard main() style argc.
5590 * @param argv Standard main() style argv. Initial components are already
5591 * stripped.
5593 * @return A shell status integer (0 for success).
5596 static int rpc_shutdown_abort(struct net_context *c, int argc,
5597 const char **argv)
5599 int rc = -1;
5601 if (c->display_usage) {
5602 d_printf( "%s\n"
5603 "net rpc abortshutdown\n"
5604 " %s\n",
5605 _("Usage:"),
5606 _("Abort a scheduled shutdown"));
5607 return 0;
5610 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5611 rpc_shutdown_abort_internals, argc, argv);
5613 if (rc == 0)
5614 return rc;
5616 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5618 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5619 rpc_reg_shutdown_abort_internals,
5620 argc, argv);
5624 * Shut down a remote RPC Server via initshutdown pipe.
5626 * All parameters are provided by the run_rpc_command function, except for
5627 * argc, argv which are passed through.
5629 * @param c A net_context structure.
5630 * @param domain_sid The domain sid acquired from the remote server.
5631 * @param cli A cli_state connected to the server.
5632 * @param mem_ctx Talloc context, destroyed on completion of the function.
5633 * @param argc Standard main() style argc.
5634 * @param argv Standard main() style argv. Initial components are already
5635 * stripped.
5637 * @return Normal NTSTATUS return.
5640 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5641 const struct dom_sid *domain_sid,
5642 const char *domain_name,
5643 struct cli_state *cli,
5644 struct rpc_pipe_client *pipe_hnd,
5645 TALLOC_CTX *mem_ctx,
5646 int argc,
5647 const char **argv)
5649 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5650 WERROR result;
5651 const char *msg = N_("This machine will be shutdown shortly");
5652 uint32 timeout = 20;
5653 struct lsa_StringLarge msg_string;
5654 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5656 if (c->opt_comment) {
5657 msg = c->opt_comment;
5659 if (c->opt_timeout) {
5660 timeout = c->opt_timeout;
5663 msg_string.string = msg;
5665 /* create an entry */
5666 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5667 &msg_string, timeout, c->opt_force, c->opt_reboot,
5668 &result);
5669 if (!NT_STATUS_IS_OK(status)) {
5670 return status;
5672 if (W_ERROR_IS_OK(result)) {
5673 d_printf(_("\nShutdown of remote machine succeeded\n"));
5674 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5675 } else {
5676 DEBUG(1,("Shutdown of remote machine failed!\n"));
5678 return werror_to_ntstatus(result);
5682 * Shut down a remote RPC Server via winreg pipe.
5684 * All parameters are provided by the run_rpc_command function, except for
5685 * argc, argv which are passed through.
5687 * @param c A net_context structure.
5688 * @param domain_sid The domain sid acquired from the remote server.
5689 * @param cli A cli_state connected to the server.
5690 * @param mem_ctx Talloc context, destroyed on completion of the function.
5691 * @param argc Standard main() style argc.
5692 * @param argv Standard main() style argv. Initial components are already
5693 * stripped.
5695 * @return Normal NTSTATUS return.
5698 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5699 const struct dom_sid *domain_sid,
5700 const char *domain_name,
5701 struct cli_state *cli,
5702 struct rpc_pipe_client *pipe_hnd,
5703 TALLOC_CTX *mem_ctx,
5704 int argc,
5705 const char **argv)
5707 const char *msg = N_("This machine will be shutdown shortly");
5708 uint32 timeout = 20;
5709 struct lsa_StringLarge msg_string;
5710 NTSTATUS result;
5711 WERROR werr;
5712 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5714 if (c->opt_comment) {
5715 msg = c->opt_comment;
5717 msg_string.string = msg;
5719 if (c->opt_timeout) {
5720 timeout = c->opt_timeout;
5723 /* create an entry */
5724 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5725 &msg_string, timeout, c->opt_force, c->opt_reboot,
5726 &werr);
5727 if (!NT_STATUS_IS_OK(result)) {
5728 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5729 return result;
5732 if (W_ERROR_IS_OK(werr)) {
5733 d_printf(_("\nShutdown of remote machine succeeded\n"));
5734 } else {
5735 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5736 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5737 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5738 else
5739 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5742 return werror_to_ntstatus(werr);
5746 * Shut down a remote RPC server.
5748 * @param argc Standard main() style argc.
5749 * @param argv Standard main() style argv. Initial components are already
5750 * stripped.
5752 * @return A shell status integer (0 for success).
5755 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5757 int rc = -1;
5759 if (c->display_usage) {
5760 d_printf( "%s\n"
5761 "net rpc shutdown\n"
5762 " %s\n",
5763 _("Usage:"),
5764 _("Shut down a remote RPC server"));
5765 return 0;
5768 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5769 rpc_init_shutdown_internals, argc, argv);
5771 if (rc) {
5772 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5773 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5774 rpc_reg_shutdown_internals, argc, argv);
5777 return rc;
5780 /***************************************************************************
5781 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5782 ***************************************************************************/
5785 * Add interdomain trust account to the RPC server.
5786 * All parameters (except for argc and argv) are passed by run_rpc_command
5787 * function.
5789 * @param c A net_context structure.
5790 * @param domain_sid The domain sid acquired from the server.
5791 * @param cli A cli_state connected to the server.
5792 * @param mem_ctx Talloc context, destroyed on completion of the function.
5793 * @param argc Standard main() style argc.
5794 * @param argv Standard main() style argv. Initial components are already
5795 * stripped.
5797 * @return normal NTSTATUS return code.
5800 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5801 const struct dom_sid *domain_sid,
5802 const char *domain_name,
5803 struct cli_state *cli,
5804 struct rpc_pipe_client *pipe_hnd,
5805 TALLOC_CTX *mem_ctx,
5806 int argc,
5807 const char **argv)
5809 struct policy_handle connect_pol, domain_pol, user_pol;
5810 NTSTATUS status, result;
5811 char *acct_name;
5812 struct lsa_String lsa_acct_name;
5813 uint32 acb_info;
5814 uint32 acct_flags=0;
5815 uint32 user_rid;
5816 uint32_t access_granted = 0;
5817 union samr_UserInfo info;
5818 unsigned int orig_timeout;
5819 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5820 DATA_BLOB session_key = data_blob_null;
5822 if (argc != 2) {
5823 d_printf("%s\n%s",
5824 _("Usage:"),
5825 _(" net rpc trustdom add <domain_name> "
5826 "<trust password>\n"));
5827 return NT_STATUS_INVALID_PARAMETER;
5831 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5834 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5835 return NT_STATUS_NO_MEMORY;
5838 if (!strupper_m(acct_name)) {
5839 SAFE_FREE(acct_name);
5840 return NT_STATUS_INVALID_PARAMETER;
5843 init_lsa_String(&lsa_acct_name, acct_name);
5845 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
5846 if (!NT_STATUS_IS_OK(status)) {
5847 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
5848 nt_errstr(status)));
5849 goto done;
5852 /* Get samr policy handle */
5853 status = dcerpc_samr_Connect2(b, mem_ctx,
5854 pipe_hnd->desthost,
5855 MAXIMUM_ALLOWED_ACCESS,
5856 &connect_pol,
5857 &result);
5858 if (!NT_STATUS_IS_OK(status)) {
5859 goto done;
5861 if (!NT_STATUS_IS_OK(result)) {
5862 status = result;
5863 goto done;
5866 /* Get domain policy handle */
5867 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5868 &connect_pol,
5869 MAXIMUM_ALLOWED_ACCESS,
5870 discard_const_p(struct dom_sid2, domain_sid),
5871 &domain_pol,
5872 &result);
5873 if (!NT_STATUS_IS_OK(status)) {
5874 goto done;
5876 if (!NT_STATUS_IS_OK(result)) {
5877 status = result;
5878 goto done;
5881 /* This call can take a long time - allow the server to time out.
5882 * 35 seconds should do it. */
5884 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5886 /* Create trusting domain's account */
5887 acb_info = ACB_NORMAL;
5888 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5889 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5890 SAMR_USER_ACCESS_SET_PASSWORD |
5891 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5892 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5894 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5895 &domain_pol,
5896 &lsa_acct_name,
5897 acb_info,
5898 acct_flags,
5899 &user_pol,
5900 &access_granted,
5901 &user_rid,
5902 &result);
5903 if (!NT_STATUS_IS_OK(status)) {
5904 goto done;
5906 /* And restore our original timeout. */
5907 rpccli_set_timeout(pipe_hnd, orig_timeout);
5909 if (!NT_STATUS_IS_OK(result)) {
5910 status = result;
5911 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5912 acct_name, nt_errstr(result));
5913 goto done;
5917 struct samr_CryptPassword crypt_pwd;
5919 ZERO_STRUCT(info.info23);
5921 init_samr_CryptPassword(argv[1],
5922 &session_key,
5923 &crypt_pwd);
5925 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5926 SAMR_FIELD_NT_PASSWORD_PRESENT;
5927 info.info23.info.acct_flags = ACB_DOMTRUST;
5928 info.info23.password = crypt_pwd;
5930 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5931 &user_pol,
5933 &info,
5934 &result);
5935 if (!NT_STATUS_IS_OK(status)) {
5936 goto done;
5939 if (!NT_STATUS_IS_OK(result)) {
5940 status = result;
5941 DEBUG(0,("Could not set trust account password: %s\n",
5942 nt_errstr(result)));
5943 goto done;
5947 done:
5948 SAFE_FREE(acct_name);
5949 data_blob_clear_free(&session_key);
5950 return status;
5954 * Create interdomain trust account for a remote domain.
5956 * @param argc Standard argc.
5957 * @param argv Standard argv without initial components.
5959 * @return Integer status (0 means success).
5962 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5964 if (argc > 0 && !c->display_usage) {
5965 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
5966 rpc_trustdom_add_internals, argc, argv);
5967 } else {
5968 d_printf("%s\n%s",
5969 _("Usage:"),
5970 _("net rpc trustdom add <domain_name> <trust "
5971 "password>\n"));
5972 return -1;
5978 * Remove interdomain trust account from the RPC server.
5979 * All parameters (except for argc and argv) are passed by run_rpc_command
5980 * function.
5982 * @param c A net_context structure.
5983 * @param domain_sid The domain sid acquired from the server.
5984 * @param cli A cli_state connected to the server.
5985 * @param mem_ctx Talloc context, destroyed on completion of the function.
5986 * @param argc Standard main() style argc.
5987 * @param argv Standard main() style argv. Initial components are already
5988 * stripped.
5990 * @return normal NTSTATUS return code.
5993 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5994 const struct dom_sid *domain_sid,
5995 const char *domain_name,
5996 struct cli_state *cli,
5997 struct rpc_pipe_client *pipe_hnd,
5998 TALLOC_CTX *mem_ctx,
5999 int argc,
6000 const char **argv)
6002 struct policy_handle connect_pol, domain_pol, user_pol;
6003 NTSTATUS status, result;
6004 char *acct_name;
6005 struct dom_sid trust_acct_sid;
6006 struct samr_Ids user_rids, name_types;
6007 struct lsa_String lsa_acct_name;
6008 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6010 if (argc != 1) {
6011 d_printf("%s\n%s",
6012 _("Usage:"),
6013 _(" net rpc trustdom del <domain_name>\n"));
6014 return NT_STATUS_INVALID_PARAMETER;
6018 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6020 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6022 if (acct_name == NULL)
6023 return NT_STATUS_NO_MEMORY;
6025 if (!strupper_m(acct_name)) {
6026 TALLOC_FREE(acct_name);
6027 return NT_STATUS_INVALID_PARAMETER;
6030 /* Get samr policy handle */
6031 status = dcerpc_samr_Connect2(b, mem_ctx,
6032 pipe_hnd->desthost,
6033 MAXIMUM_ALLOWED_ACCESS,
6034 &connect_pol,
6035 &result);
6036 if (!NT_STATUS_IS_OK(status)) {
6037 goto done;
6039 if (!NT_STATUS_IS_OK(result)) {
6040 status = result;
6041 goto done;
6044 /* Get domain policy handle */
6045 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6046 &connect_pol,
6047 MAXIMUM_ALLOWED_ACCESS,
6048 discard_const_p(struct dom_sid2, domain_sid),
6049 &domain_pol,
6050 &result);
6051 if (!NT_STATUS_IS_OK(status)) {
6052 goto done;
6054 if (!NT_STATUS_IS_OK(result)) {
6055 status = result;
6056 goto done;
6059 init_lsa_String(&lsa_acct_name, acct_name);
6061 status = dcerpc_samr_LookupNames(b, mem_ctx,
6062 &domain_pol,
6064 &lsa_acct_name,
6065 &user_rids,
6066 &name_types,
6067 &result);
6068 if (!NT_STATUS_IS_OK(status)) {
6069 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6070 "failed %s\n"),
6071 acct_name, nt_errstr(status));
6072 goto done;
6074 if (!NT_STATUS_IS_OK(result)) {
6075 status = result;
6076 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6077 "failed %s\n"),
6078 acct_name, nt_errstr(result) );
6079 goto done;
6081 if (user_rids.count != 1) {
6082 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6083 goto done;
6085 if (name_types.count != 1) {
6086 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6087 goto done;
6090 status = dcerpc_samr_OpenUser(b, mem_ctx,
6091 &domain_pol,
6092 MAXIMUM_ALLOWED_ACCESS,
6093 user_rids.ids[0],
6094 &user_pol,
6095 &result);
6096 if (!NT_STATUS_IS_OK(status)) {
6097 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6098 "%s\n"),
6099 acct_name, nt_errstr(status) );
6100 goto done;
6103 if (!NT_STATUS_IS_OK(result)) {
6104 status = result;
6105 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6106 "%s\n"),
6107 acct_name, nt_errstr(result) );
6108 goto done;
6111 /* append the rid to the domain sid */
6112 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6113 goto done;
6116 /* remove the sid */
6118 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6119 &user_pol,
6120 &trust_acct_sid,
6121 &result);
6122 if (!NT_STATUS_IS_OK(status)) {
6123 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6124 " on user %s failed %s\n"),
6125 acct_name, nt_errstr(status));
6126 goto done;
6128 if (!NT_STATUS_IS_OK(result)) {
6129 status = result;
6130 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6131 " on user %s failed %s\n"),
6132 acct_name, nt_errstr(result) );
6133 goto done;
6137 /* Delete user */
6139 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6140 &user_pol,
6141 &result);
6142 if (!NT_STATUS_IS_OK(status)) {
6143 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6144 "%s\n"),
6145 acct_name, nt_errstr(status));
6146 goto done;
6149 if (!NT_STATUS_IS_OK(result)) {
6150 result = status;
6151 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6152 "%s\n"),
6153 acct_name, nt_errstr(result) );
6154 goto done;
6157 if (!NT_STATUS_IS_OK(result)) {
6158 d_printf(_("Could not set trust account password: %s\n"),
6159 nt_errstr(result));
6160 goto done;
6163 done:
6164 return status;
6168 * Delete interdomain trust account for a remote domain.
6170 * @param argc Standard argc.
6171 * @param argv Standard argv without initial components.
6173 * @return Integer status (0 means success).
6176 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6178 if (argc > 0 && !c->display_usage) {
6179 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6180 rpc_trustdom_del_internals, argc, argv);
6181 } else {
6182 d_printf("%s\n%s",
6183 _("Usage:"),
6184 _("net rpc trustdom del <domain>\n"));
6185 return -1;
6189 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6190 struct cli_state *cli,
6191 TALLOC_CTX *mem_ctx,
6192 const char *domain_name)
6194 char *dc_name = NULL;
6195 const char *buffer = NULL;
6196 struct rpc_pipe_client *netr;
6197 NTSTATUS status;
6198 WERROR result;
6199 struct dcerpc_binding_handle *b;
6201 /* Use NetServerEnum2 */
6203 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6204 SAFE_FREE(dc_name);
6205 return NT_STATUS_OK;
6208 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6209 for domain %s\n", domain_name));
6211 /* Try netr_GetDcName */
6213 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
6214 &netr);
6215 if (!NT_STATUS_IS_OK(status)) {
6216 return status;
6219 b = netr->binding_handle;
6221 status = dcerpc_netr_GetDcName(b, mem_ctx,
6222 netr->desthost,
6223 domain_name,
6224 &buffer,
6225 &result);
6226 TALLOC_FREE(netr);
6228 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6229 return status;
6232 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6233 for domain %s\n", domain_name));
6235 if (!NT_STATUS_IS_OK(status)) {
6236 return status;
6239 return werror_to_ntstatus(result);
6243 * Establish trust relationship to a trusting domain.
6244 * Interdomain account must already be created on remote PDC.
6246 * @param c A net_context structure.
6247 * @param argc Standard argc.
6248 * @param argv Standard argv without initial components.
6250 * @return Integer status (0 means success).
6253 static int rpc_trustdom_establish(struct net_context *c, int argc,
6254 const char **argv)
6256 struct cli_state *cli = NULL;
6257 struct sockaddr_storage server_ss;
6258 struct rpc_pipe_client *pipe_hnd = NULL;
6259 struct policy_handle connect_hnd;
6260 TALLOC_CTX *mem_ctx;
6261 NTSTATUS nt_status, result;
6262 struct dom_sid *domain_sid;
6264 char* domain_name;
6265 char* acct_name;
6266 fstring pdc_name;
6267 union lsa_PolicyInformation *info = NULL;
6268 struct dcerpc_binding_handle *b;
6271 * Connect to \\server\ipc$ as 'our domain' account with password
6274 if (argc != 1 || c->display_usage) {
6275 d_printf("%s\n%s",
6276 _("Usage:"),
6277 _("net rpc trustdom establish <domain_name>\n"));
6278 return -1;
6281 domain_name = smb_xstrdup(argv[0]);
6282 if (!strupper_m(domain_name)) {
6283 SAFE_FREE(domain_name);
6284 return -1;
6287 /* account name used at first is our domain's name with '$' */
6288 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6289 return -1;
6291 if (!strupper_m(acct_name)) {
6292 SAFE_FREE(domain_name);
6293 SAFE_FREE(acct_name);
6294 return -1;
6298 * opt_workgroup will be used by connection functions further,
6299 * hence it should be set to remote domain name instead of ours
6301 if (c->opt_workgroup) {
6302 c->opt_workgroup = smb_xstrdup(domain_name);
6305 c->opt_user_name = acct_name;
6307 /* find the domain controller */
6308 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6309 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6310 return -1;
6313 /* connect to ipc$ as username/password */
6314 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6315 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6317 /* Is it trusting domain account for sure ? */
6318 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6319 nt_errstr(nt_status)));
6320 return -1;
6323 /* store who we connected to */
6325 saf_store( domain_name, pdc_name );
6328 * Connect to \\server\ipc$ again (this time anonymously)
6331 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6332 (char*)pdc_name);
6334 if (NT_STATUS_IS_ERR(nt_status)) {
6335 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6336 domain_name, nt_errstr(nt_status)));
6337 return -1;
6340 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6341 "domain %s", domain_name))) {
6342 DEBUG(0, ("talloc_init() failed\n"));
6343 cli_shutdown(cli);
6344 return -1;
6347 /* Make sure we're talking to a proper server */
6349 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6350 if (!NT_STATUS_IS_OK(nt_status)) {
6351 cli_shutdown(cli);
6352 talloc_destroy(mem_ctx);
6353 return -1;
6357 * Call LsaOpenPolicy and LsaQueryInfo
6360 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6361 &pipe_hnd);
6362 if (!NT_STATUS_IS_OK(nt_status)) {
6363 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6364 cli_shutdown(cli);
6365 talloc_destroy(mem_ctx);
6366 return -1;
6369 b = pipe_hnd->binding_handle;
6371 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6372 &connect_hnd);
6373 if (NT_STATUS_IS_ERR(nt_status)) {
6374 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6375 nt_errstr(nt_status)));
6376 cli_shutdown(cli);
6377 talloc_destroy(mem_ctx);
6378 return -1;
6381 /* Querying info level 5 */
6383 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6384 &connect_hnd,
6385 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6386 &info,
6387 &result);
6388 if (NT_STATUS_IS_ERR(nt_status)) {
6389 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6390 nt_errstr(nt_status)));
6391 cli_shutdown(cli);
6392 talloc_destroy(mem_ctx);
6393 return -1;
6395 if (NT_STATUS_IS_ERR(result)) {
6396 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6397 nt_errstr(result)));
6398 cli_shutdown(cli);
6399 talloc_destroy(mem_ctx);
6400 return -1;
6403 domain_sid = info->account_domain.sid;
6405 /* There should be actually query info level 3 (following nt serv behaviour),
6406 but I still don't know if it's _really_ necessary */
6409 * Store the password in secrets db
6412 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6413 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6414 cli_shutdown(cli);
6415 talloc_destroy(mem_ctx);
6416 return -1;
6420 * Close the pipes and clean up
6423 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6424 if (NT_STATUS_IS_ERR(nt_status)) {
6425 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6426 nt_errstr(nt_status)));
6427 cli_shutdown(cli);
6428 talloc_destroy(mem_ctx);
6429 return -1;
6432 cli_shutdown(cli);
6434 talloc_destroy(mem_ctx);
6436 d_printf(_("Trust to domain %s established\n"), domain_name);
6437 return 0;
6441 * Revoke trust relationship to the remote domain.
6443 * @param c A net_context structure.
6444 * @param argc Standard argc.
6445 * @param argv Standard argv without initial components.
6447 * @return Integer status (0 means success).
6450 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6451 const char **argv)
6453 char* domain_name;
6454 int rc = -1;
6456 if (argc < 1 || c->display_usage) {
6457 d_printf("%s\n%s",
6458 _("Usage:"),
6459 _("net rpc trustdom revoke <domain_name>\n"
6460 " Revoke trust relationship\n"
6461 " domain_name\tName of domain to revoke trust\n"));
6462 return -1;
6465 /* generate upper cased domain name */
6466 domain_name = smb_xstrdup(argv[0]);
6467 if (!strupper_m(domain_name)) {
6468 SAFE_FREE(domain_name);
6469 return -1;
6472 /* delete password of the trust */
6473 if (!pdb_del_trusteddom_pw(domain_name)) {
6474 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6475 domain_name));
6476 goto done;
6479 rc = 0;
6480 done:
6481 SAFE_FREE(domain_name);
6482 return rc;
6485 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6486 const struct dom_sid *domain_sid,
6487 const char *domain_name,
6488 struct cli_state *cli,
6489 struct rpc_pipe_client *pipe_hnd,
6490 TALLOC_CTX *mem_ctx,
6491 int argc,
6492 const char **argv)
6494 fstring str_sid;
6495 if (!sid_to_fstring(str_sid, domain_sid)) {
6496 return NT_STATUS_UNSUCCESSFUL;
6498 d_printf("%s\n", str_sid);
6499 return NT_STATUS_OK;
6502 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6504 fstring ascii_sid;
6506 /* convert sid into ascii string */
6507 sid_to_fstring(ascii_sid, dom_sid);
6509 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6512 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6513 TALLOC_CTX *mem_ctx,
6514 struct policy_handle *pol,
6515 struct dom_sid dom_sid,
6516 const char *trusted_dom_name)
6518 NTSTATUS nt_status, result;
6519 union lsa_TrustedDomainInfo *info = NULL;
6520 char *cleartextpwd = NULL;
6521 DATA_BLOB session_key;
6522 DATA_BLOB data = data_blob_null;
6523 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6525 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6526 pol,
6527 &dom_sid,
6528 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6529 &info,
6530 &result);
6531 if (NT_STATUS_IS_ERR(nt_status)) {
6532 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6533 nt_errstr(nt_status)));
6534 goto done;
6536 if (NT_STATUS_IS_ERR(result)) {
6537 nt_status = result;
6538 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6539 nt_errstr(result)));
6540 goto done;
6543 data = data_blob(info->password.password->data,
6544 info->password.password->length);
6546 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6547 if (!NT_STATUS_IS_OK(nt_status)) {
6548 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6549 goto done;
6552 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6553 data_blob_free(&session_key);
6555 if (cleartextpwd == NULL) {
6556 DEBUG(0,("retrieved NULL password\n"));
6557 nt_status = NT_STATUS_UNSUCCESSFUL;
6558 goto done;
6561 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6562 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6563 nt_status = NT_STATUS_UNSUCCESSFUL;
6564 goto done;
6567 #ifdef DEBUG_PASSWORD
6568 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6569 "password: [%s]\n", trusted_dom_name,
6570 sid_string_dbg(&dom_sid), cleartextpwd));
6571 #endif
6573 done:
6574 SAFE_FREE(cleartextpwd);
6575 data_blob_free(&data);
6577 return nt_status;
6580 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6581 const char **argv)
6583 /* common variables */
6584 TALLOC_CTX* mem_ctx;
6585 struct cli_state *cli = NULL;
6586 struct rpc_pipe_client *pipe_hnd = NULL;
6587 NTSTATUS nt_status, result;
6588 const char *domain_name = NULL;
6589 struct policy_handle connect_hnd;
6590 union lsa_PolicyInformation *info = NULL;
6592 /* trusted domains listing variables */
6593 unsigned int enum_ctx = 0;
6594 int i;
6595 struct lsa_DomainList dom_list;
6596 fstring pdc_name;
6597 struct dcerpc_binding_handle *b;
6599 if (c->display_usage) {
6600 d_printf( "%s\n"
6601 "net rpc trustdom vampire\n"
6602 " %s\n",
6603 _("Usage:"),
6604 _("Vampire trust relationship from remote server"));
6605 return 0;
6609 * Listing trusted domains (stored in secrets.tdb, if local)
6612 mem_ctx = talloc_init("trust relationships vampire");
6615 * set domain and pdc name to local samba server (default)
6616 * or to remote one given in command line
6619 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6620 domain_name = c->opt_workgroup;
6621 c->opt_target_workgroup = c->opt_workgroup;
6622 } else {
6623 fstrcpy(pdc_name, lp_netbios_name());
6624 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6625 c->opt_target_workgroup = domain_name;
6628 /* open \PIPE\lsarpc and open policy handle */
6629 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6630 if (!NT_STATUS_IS_OK(nt_status)) {
6631 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6632 nt_errstr(nt_status)));
6633 talloc_destroy(mem_ctx);
6634 return -1;
6637 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6638 &pipe_hnd);
6639 if (!NT_STATUS_IS_OK(nt_status)) {
6640 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6641 nt_errstr(nt_status) ));
6642 cli_shutdown(cli);
6643 talloc_destroy(mem_ctx);
6644 return -1;
6647 b = pipe_hnd->binding_handle;
6649 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6650 &connect_hnd);
6651 if (NT_STATUS_IS_ERR(nt_status)) {
6652 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6653 nt_errstr(nt_status)));
6654 cli_shutdown(cli);
6655 talloc_destroy(mem_ctx);
6656 return -1;
6659 /* query info level 5 to obtain sid of a domain being queried */
6660 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6661 &connect_hnd,
6662 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6663 &info,
6664 &result);
6666 if (NT_STATUS_IS_ERR(nt_status)) {
6667 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6668 nt_errstr(nt_status)));
6669 cli_shutdown(cli);
6670 talloc_destroy(mem_ctx);
6671 return -1;
6673 if (NT_STATUS_IS_ERR(result)) {
6674 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6675 nt_errstr(result)));
6676 cli_shutdown(cli);
6677 talloc_destroy(mem_ctx);
6678 return -1;
6682 * Keep calling LsaEnumTrustdom over opened pipe until
6683 * the end of enumeration is reached
6686 d_printf(_("Vampire trusted domains:\n\n"));
6688 do {
6689 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6690 &connect_hnd,
6691 &enum_ctx,
6692 &dom_list,
6693 (uint32_t)-1,
6694 &result);
6695 if (NT_STATUS_IS_ERR(nt_status)) {
6696 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6697 nt_errstr(nt_status)));
6698 cli_shutdown(cli);
6699 talloc_destroy(mem_ctx);
6700 return -1;
6702 if (NT_STATUS_IS_ERR(result)) {
6703 nt_status = result;
6704 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6705 nt_errstr(result)));
6706 cli_shutdown(cli);
6707 talloc_destroy(mem_ctx);
6708 return -1;
6712 for (i = 0; i < dom_list.count; i++) {
6714 print_trusted_domain(dom_list.domains[i].sid,
6715 dom_list.domains[i].name.string);
6717 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6718 *dom_list.domains[i].sid,
6719 dom_list.domains[i].name.string);
6720 if (!NT_STATUS_IS_OK(nt_status)) {
6721 cli_shutdown(cli);
6722 talloc_destroy(mem_ctx);
6723 return -1;
6728 * in case of no trusted domains say something rather
6729 * than just display blank line
6731 if (!dom_list.count) d_printf(_("none\n"));
6733 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6735 /* close this connection before doing next one */
6736 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6737 if (NT_STATUS_IS_ERR(nt_status)) {
6738 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6739 nt_errstr(nt_status)));
6740 cli_shutdown(cli);
6741 talloc_destroy(mem_ctx);
6742 return -1;
6745 /* close lsarpc pipe and connection to IPC$ */
6746 cli_shutdown(cli);
6748 talloc_destroy(mem_ctx);
6749 return 0;
6752 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6754 /* common variables */
6755 TALLOC_CTX* mem_ctx;
6756 struct cli_state *cli = NULL, *remote_cli = NULL;
6757 struct rpc_pipe_client *pipe_hnd = NULL;
6758 NTSTATUS nt_status, result;
6759 const char *domain_name = NULL;
6760 struct dom_sid *queried_dom_sid;
6761 int ascii_dom_name_len;
6762 struct policy_handle connect_hnd;
6763 union lsa_PolicyInformation *info = NULL;
6764 struct dcerpc_binding_handle *b = NULL;
6766 /* trusted domains listing variables */
6767 unsigned int num_domains, enum_ctx = 0;
6768 int i;
6769 struct lsa_DomainList dom_list;
6770 fstring pdc_name;
6771 bool found_domain;
6773 /* trusting domains listing variables */
6774 struct policy_handle domain_hnd;
6775 struct samr_SamArray *trusts = NULL;
6777 if (c->display_usage) {
6778 d_printf( "%s\n"
6779 "net rpc trustdom list\n"
6780 " %s\n",
6781 _("Usage:"),
6782 _("List incoming and outgoing trust relationships"));
6783 return 0;
6787 * Listing trusted domains (stored in secrets.tdb, if local)
6790 mem_ctx = talloc_init("trust relationships listing");
6793 * set domain and pdc name to local samba server (default)
6794 * or to remote one given in command line
6797 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6798 domain_name = c->opt_workgroup;
6799 c->opt_target_workgroup = c->opt_workgroup;
6800 } else {
6801 fstrcpy(pdc_name, lp_netbios_name());
6802 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6803 c->opt_target_workgroup = domain_name;
6806 /* open \PIPE\lsarpc and open policy handle */
6807 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6808 if (!NT_STATUS_IS_OK(nt_status)) {
6809 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6810 nt_errstr(nt_status)));
6811 talloc_destroy(mem_ctx);
6812 return -1;
6815 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6816 &pipe_hnd);
6817 if (!NT_STATUS_IS_OK(nt_status)) {
6818 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6819 nt_errstr(nt_status) ));
6820 cli_shutdown(cli);
6821 talloc_destroy(mem_ctx);
6822 return -1;
6825 b = pipe_hnd->binding_handle;
6827 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6828 &connect_hnd);
6829 if (NT_STATUS_IS_ERR(nt_status)) {
6830 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6831 nt_errstr(nt_status)));
6832 cli_shutdown(cli);
6833 talloc_destroy(mem_ctx);
6834 return -1;
6837 /* query info level 5 to obtain sid of a domain being queried */
6838 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6839 &connect_hnd,
6840 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6841 &info,
6842 &result);
6844 if (NT_STATUS_IS_ERR(nt_status)) {
6845 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6846 nt_errstr(nt_status)));
6847 cli_shutdown(cli);
6848 talloc_destroy(mem_ctx);
6849 return -1;
6851 if (NT_STATUS_IS_ERR(result)) {
6852 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6853 nt_errstr(result)));
6854 cli_shutdown(cli);
6855 talloc_destroy(mem_ctx);
6856 return -1;
6859 queried_dom_sid = info->account_domain.sid;
6862 * Keep calling LsaEnumTrustdom over opened pipe until
6863 * the end of enumeration is reached
6866 d_printf(_("Trusted domains list:\n\n"));
6868 found_domain = false;
6870 do {
6871 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6872 &connect_hnd,
6873 &enum_ctx,
6874 &dom_list,
6875 (uint32_t)-1,
6876 &result);
6877 if (NT_STATUS_IS_ERR(nt_status)) {
6878 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6879 nt_errstr(nt_status)));
6880 cli_shutdown(cli);
6881 talloc_destroy(mem_ctx);
6882 return -1;
6884 if (NT_STATUS_IS_ERR(result)) {
6885 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6886 nt_errstr(result)));
6887 cli_shutdown(cli);
6888 talloc_destroy(mem_ctx);
6889 return -1;
6893 for (i = 0; i < dom_list.count; i++) {
6894 print_trusted_domain(dom_list.domains[i].sid,
6895 dom_list.domains[i].name.string);
6896 found_domain = true;
6900 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6903 * in case of no trusted domains say something rather
6904 * than just display blank line
6906 if (!found_domain) {
6907 d_printf(_("none\n"));
6910 /* close this connection before doing next one */
6911 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6912 if (NT_STATUS_IS_ERR(nt_status)) {
6913 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6914 nt_errstr(nt_status)));
6915 cli_shutdown(cli);
6916 talloc_destroy(mem_ctx);
6917 return -1;
6920 TALLOC_FREE(pipe_hnd);
6923 * Listing trusting domains (stored in passdb backend, if local)
6926 d_printf(_("\nTrusting domains list:\n\n"));
6929 * Open \PIPE\samr and get needed policy handles
6931 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6932 &pipe_hnd);
6933 if (!NT_STATUS_IS_OK(nt_status)) {
6934 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6935 cli_shutdown(cli);
6936 talloc_destroy(mem_ctx);
6937 return -1;
6940 b = pipe_hnd->binding_handle;
6942 /* SamrConnect2 */
6943 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6944 pipe_hnd->desthost,
6945 SAMR_ACCESS_LOOKUP_DOMAIN,
6946 &connect_hnd,
6947 &result);
6948 if (!NT_STATUS_IS_OK(nt_status)) {
6949 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6950 nt_errstr(nt_status)));
6951 cli_shutdown(cli);
6952 talloc_destroy(mem_ctx);
6953 return -1;
6955 if (!NT_STATUS_IS_OK(result)) {
6956 nt_status = result;
6957 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6958 nt_errstr(result)));
6959 cli_shutdown(cli);
6960 talloc_destroy(mem_ctx);
6961 return -1;
6964 /* SamrOpenDomain - we have to open domain policy handle in order to be
6965 able to enumerate accounts*/
6966 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6967 &connect_hnd,
6968 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6969 queried_dom_sid,
6970 &domain_hnd,
6971 &result);
6972 if (!NT_STATUS_IS_OK(nt_status)) {
6973 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6974 nt_errstr(nt_status)));
6975 cli_shutdown(cli);
6976 talloc_destroy(mem_ctx);
6977 return -1;
6979 if (!NT_STATUS_IS_OK(result)) {
6980 nt_status = result;
6981 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6982 nt_errstr(result)));
6983 cli_shutdown(cli);
6984 talloc_destroy(mem_ctx);
6985 return -1;
6989 * perform actual enumeration
6992 found_domain = false;
6994 enum_ctx = 0; /* reset enumeration context from last enumeration */
6995 do {
6997 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
6998 &domain_hnd,
6999 &enum_ctx,
7000 ACB_DOMTRUST,
7001 &trusts,
7002 0xffff,
7003 &num_domains,
7004 &result);
7005 if (NT_STATUS_IS_ERR(nt_status)) {
7006 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7007 nt_errstr(nt_status)));
7008 cli_shutdown(cli);
7009 talloc_destroy(mem_ctx);
7010 return -1;
7012 if (NT_STATUS_IS_ERR(result)) {
7013 nt_status = result;
7014 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7015 nt_errstr(result)));
7016 cli_shutdown(cli);
7017 talloc_destroy(mem_ctx);
7018 return -1;
7021 for (i = 0; i < num_domains; i++) {
7023 char *str = discard_const_p(char, trusts->entries[i].name.string);
7025 found_domain = true;
7028 * get each single domain's sid (do we _really_ need this ?):
7029 * 1) connect to domain's pdc
7030 * 2) query the pdc for domain's sid
7033 /* get rid of '$' tail */
7034 ascii_dom_name_len = strlen(str);
7035 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7036 str[ascii_dom_name_len - 1] = '\0';
7038 /* set opt_* variables to remote domain */
7039 if (!strupper_m(str)) {
7040 cli_shutdown(cli);
7041 talloc_destroy(mem_ctx);
7042 return -1;
7044 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7045 c->opt_target_workgroup = c->opt_workgroup;
7047 d_printf("%-20s", str);
7049 /* connect to remote domain controller */
7050 nt_status = net_make_ipc_connection(c,
7051 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7052 &remote_cli);
7053 if (NT_STATUS_IS_OK(nt_status)) {
7054 /* query for domain's sid */
7055 if (run_rpc_command(
7056 c, remote_cli,
7057 &ndr_table_lsarpc, 0,
7058 rpc_query_domain_sid, argc,
7059 argv))
7060 d_printf(_("strange - couldn't get domain's sid\n"));
7062 cli_shutdown(remote_cli);
7064 } else {
7065 d_fprintf(stderr, _("domain controller is not "
7066 "responding: %s\n"),
7067 nt_errstr(nt_status));
7068 d_printf(_("couldn't get domain's sid\n"));
7072 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7074 if (!found_domain) {
7075 d_printf("none\n");
7078 /* close opened samr and domain policy handles */
7079 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7080 if (!NT_STATUS_IS_OK(nt_status)) {
7081 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7084 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7085 if (!NT_STATUS_IS_OK(nt_status)) {
7086 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7089 /* close samr pipe and connection to IPC$ */
7090 cli_shutdown(cli);
7092 talloc_destroy(mem_ctx);
7093 return 0;
7097 * Entrypoint for 'net rpc trustdom' code.
7099 * @param argc Standard argc.
7100 * @param argv Standard argv without initial components.
7102 * @return Integer status (0 means success).
7105 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7107 struct functable func[] = {
7109 "add",
7110 rpc_trustdom_add,
7111 NET_TRANSPORT_RPC,
7112 N_("Add trusting domain's account"),
7113 N_("net rpc trustdom add\n"
7114 " Add trusting domain's account")
7117 "del",
7118 rpc_trustdom_del,
7119 NET_TRANSPORT_RPC,
7120 N_("Remove trusting domain's account"),
7121 N_("net rpc trustdom del\n"
7122 " Remove trusting domain's account")
7125 "establish",
7126 rpc_trustdom_establish,
7127 NET_TRANSPORT_RPC,
7128 N_("Establish outgoing trust relationship"),
7129 N_("net rpc trustdom establish\n"
7130 " Establish outgoing trust relationship")
7133 "revoke",
7134 rpc_trustdom_revoke,
7135 NET_TRANSPORT_RPC,
7136 N_("Revoke outgoing trust relationship"),
7137 N_("net rpc trustdom revoke\n"
7138 " Revoke outgoing trust relationship")
7141 "list",
7142 rpc_trustdom_list,
7143 NET_TRANSPORT_RPC,
7144 N_("List in- and outgoing domain trusts"),
7145 N_("net rpc trustdom list\n"
7146 " List in- and outgoing domain trusts")
7149 "vampire",
7150 rpc_trustdom_vampire,
7151 NET_TRANSPORT_RPC,
7152 N_("Vampire trusts from remote server"),
7153 N_("net rpc trustdom vampire\n"
7154 " Vampire trusts from remote server")
7156 {NULL, NULL, 0, NULL, NULL}
7159 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7163 * Check if a server will take rpc commands
7164 * @param flags Type of server to connect to (PDC, DMB, localhost)
7165 * if the host is not explicitly specified
7166 * @return bool (true means rpc supported)
7168 bool net_rpc_check(struct net_context *c, unsigned flags)
7170 struct cli_state *cli;
7171 bool ret = false;
7172 struct sockaddr_storage server_ss;
7173 char *server_name = NULL;
7174 NTSTATUS status;
7176 /* flags (i.e. server type) may depend on command */
7177 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7178 return false;
7180 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7181 lp_netbios_name(), SMB_SIGNING_DEFAULT,
7182 0, &cli);
7183 if (!NT_STATUS_IS_OK(status)) {
7184 return false;
7186 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7187 PROTOCOL_NT1);
7188 if (!NT_STATUS_IS_OK(status))
7189 goto done;
7190 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7191 goto done;
7193 ret = true;
7194 done:
7195 cli_shutdown(cli);
7196 return ret;
7199 /* dump sam database via samsync rpc calls */
7200 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7201 if (c->display_usage) {
7202 d_printf( "%s\n"
7203 "net rpc samdump\n"
7204 " %s\n",
7205 _("Usage:"),
7206 _("Dump remote SAM database"));
7207 return 0;
7210 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7211 NET_FLAGS_ANONYMOUS,
7212 rpc_samdump_internals, argc, argv);
7215 /* syncronise sam database via samsync rpc calls */
7216 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7218 struct functable func[] = {
7220 "ldif",
7221 rpc_vampire_ldif,
7222 NET_TRANSPORT_RPC,
7223 N_("Dump remote SAM database to ldif"),
7224 N_("net rpc vampire ldif\n"
7225 " Dump remote SAM database to LDIF file or "
7226 "stdout")
7229 "keytab",
7230 rpc_vampire_keytab,
7231 NET_TRANSPORT_RPC,
7232 N_("Dump remote SAM database to Kerberos Keytab"),
7233 N_("net rpc vampire keytab\n"
7234 " Dump remote SAM database to Kerberos keytab "
7235 "file")
7238 "passdb",
7239 rpc_vampire_passdb,
7240 NET_TRANSPORT_RPC,
7241 N_("Dump remote SAM database to passdb"),
7242 N_("net rpc vampire passdb\n"
7243 " Dump remote SAM database to passdb")
7246 {NULL, NULL, 0, NULL, NULL}
7249 if (argc == 0) {
7250 if (c->display_usage) {
7251 d_printf( "%s\n"
7252 "net rpc vampire\n"
7253 " %s\n",
7254 _("Usage:"),
7255 _("Vampire remote SAM database"));
7256 return 0;
7259 return rpc_vampire_passdb(c, argc, argv);
7262 return net_run_function(c, argc, argv, "net rpc vampire", func);
7266 * Migrate everything from a print server.
7268 * @param c A net_context structure.
7269 * @param argc Standard main() style argc.
7270 * @param argv Standard main() style argv. Initial components are already
7271 * stripped.
7273 * @return A shell status integer (0 for success).
7275 * The order is important !
7276 * To successfully add drivers the print queues have to exist !
7277 * Applying ACLs should be the last step, because you're easily locked out.
7280 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7281 const char **argv)
7283 int ret;
7285 if (c->display_usage) {
7286 d_printf( "%s\n"
7287 "net rpc printer migrate all\n"
7288 " %s\n",
7289 _("Usage:"),
7290 _("Migrate everything from a print server"));
7291 return 0;
7294 if (!c->opt_host) {
7295 d_printf(_("no server to migrate\n"));
7296 return -1;
7299 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7300 rpc_printer_migrate_printers_internals, argc,
7301 argv);
7302 if (ret)
7303 return ret;
7305 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7306 rpc_printer_migrate_drivers_internals, argc,
7307 argv);
7308 if (ret)
7309 return ret;
7311 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7312 rpc_printer_migrate_forms_internals, argc, argv);
7313 if (ret)
7314 return ret;
7316 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7317 rpc_printer_migrate_settings_internals, argc,
7318 argv);
7319 if (ret)
7320 return ret;
7322 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7323 rpc_printer_migrate_security_internals, argc,
7324 argv);
7329 * Migrate print drivers from a print server.
7331 * @param c A net_context structure.
7332 * @param argc Standard main() style argc.
7333 * @param argv Standard main() style argv. Initial components are already
7334 * stripped.
7336 * @return A shell status integer (0 for success).
7338 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7339 const char **argv)
7341 if (c->display_usage) {
7342 d_printf( "%s\n"
7343 "net rpc printer migrate drivers\n"
7344 " %s\n",
7345 _("Usage:"),
7346 _("Migrate print-drivers from a print-server"));
7347 return 0;
7350 if (!c->opt_host) {
7351 d_printf(_("no server to migrate\n"));
7352 return -1;
7355 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7356 rpc_printer_migrate_drivers_internals,
7357 argc, argv);
7361 * Migrate print-forms from a print-server.
7363 * @param c A net_context structure.
7364 * @param argc Standard main() style argc.
7365 * @param argv Standard main() style argv. Initial components are already
7366 * stripped.
7368 * @return A shell status integer (0 for success).
7370 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7371 const char **argv)
7373 if (c->display_usage) {
7374 d_printf( "%s\n"
7375 "net rpc printer migrate forms\n"
7376 " %s\n",
7377 _("Usage:"),
7378 _("Migrate print-forms from a print-server"));
7379 return 0;
7382 if (!c->opt_host) {
7383 d_printf(_("no server to migrate\n"));
7384 return -1;
7387 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7388 rpc_printer_migrate_forms_internals,
7389 argc, argv);
7393 * Migrate printers from a print-server.
7395 * @param c A net_context structure.
7396 * @param argc Standard main() style argc.
7397 * @param argv Standard main() style argv. Initial components are already
7398 * stripped.
7400 * @return A shell status integer (0 for success).
7402 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7403 const char **argv)
7405 if (c->display_usage) {
7406 d_printf( "%s\n"
7407 "net rpc printer migrate printers\n"
7408 " %s\n",
7409 _("Usage:"),
7410 _("Migrate printers from a print-server"));
7411 return 0;
7414 if (!c->opt_host) {
7415 d_printf(_("no server to migrate\n"));
7416 return -1;
7419 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7420 rpc_printer_migrate_printers_internals,
7421 argc, argv);
7425 * Migrate printer-ACLs from a print-server
7427 * @param c A net_context structure.
7428 * @param argc Standard main() style argc.
7429 * @param argv Standard main() style argv. Initial components are already
7430 * stripped.
7432 * @return A shell status integer (0 for success).
7434 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7435 const char **argv)
7437 if (c->display_usage) {
7438 d_printf( "%s\n"
7439 "net rpc printer migrate security\n"
7440 " %s\n",
7441 _("Usage:"),
7442 _("Migrate printer-ACLs from a print-server"));
7443 return 0;
7446 if (!c->opt_host) {
7447 d_printf(_("no server to migrate\n"));
7448 return -1;
7451 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7452 rpc_printer_migrate_security_internals,
7453 argc, argv);
7457 * Migrate printer-settings from a print-server.
7459 * @param c A net_context structure.
7460 * @param argc Standard main() style argc.
7461 * @param argv Standard main() style argv. Initial components are already
7462 * stripped.
7464 * @return A shell status integer (0 for success).
7466 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7467 const char **argv)
7469 if (c->display_usage) {
7470 d_printf( "%s\n"
7471 "net rpc printer migrate settings\n"
7472 " %s\n",
7473 _("Usage:"),
7474 _("Migrate printer-settings from a "
7475 "print-server"));
7476 return 0;
7479 if (!c->opt_host) {
7480 d_printf(_("no server to migrate\n"));
7481 return -1;
7484 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7485 rpc_printer_migrate_settings_internals,
7486 argc, argv);
7490 * 'net rpc printer' entrypoint.
7492 * @param c A net_context structure.
7493 * @param argc Standard main() style argc.
7494 * @param argv Standard main() style argv. Initial components are already
7495 * stripped.
7498 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7501 /* ouch: when addriver and setdriver are called from within
7502 rpc_printer_migrate_drivers_internals, the printer-queue already
7503 *has* to exist */
7505 struct functable func[] = {
7507 "all",
7508 rpc_printer_migrate_all,
7509 NET_TRANSPORT_RPC,
7510 N_("Migrate all from remote to local print server"),
7511 N_("net rpc printer migrate all\n"
7512 " Migrate all from remote to local print server")
7515 "drivers",
7516 rpc_printer_migrate_drivers,
7517 NET_TRANSPORT_RPC,
7518 N_("Migrate drivers to local server"),
7519 N_("net rpc printer migrate drivers\n"
7520 " Migrate drivers to local server")
7523 "forms",
7524 rpc_printer_migrate_forms,
7525 NET_TRANSPORT_RPC,
7526 N_("Migrate froms to local server"),
7527 N_("net rpc printer migrate forms\n"
7528 " Migrate froms to local server")
7531 "printers",
7532 rpc_printer_migrate_printers,
7533 NET_TRANSPORT_RPC,
7534 N_("Migrate printers to local server"),
7535 N_("net rpc printer migrate printers\n"
7536 " Migrate printers to local server")
7539 "security",
7540 rpc_printer_migrate_security,
7541 NET_TRANSPORT_RPC,
7542 N_("Mirgate printer ACLs to local server"),
7543 N_("net rpc printer migrate security\n"
7544 " Mirgate printer ACLs to local server")
7547 "settings",
7548 rpc_printer_migrate_settings,
7549 NET_TRANSPORT_RPC,
7550 N_("Migrate printer settings to local server"),
7551 N_("net rpc printer migrate settings\n"
7552 " Migrate printer settings to local server")
7554 {NULL, NULL, 0, NULL, NULL}
7557 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7562 * List printers on a remote RPC server.
7564 * @param c A net_context structure.
7565 * @param argc Standard main() style argc.
7566 * @param argv Standard main() style argv. Initial components are already
7567 * stripped.
7569 * @return A shell status integer (0 for success).
7571 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7573 if (c->display_usage) {
7574 d_printf( "%s\n"
7575 "net rpc printer list\n"
7576 " %s\n",
7577 _("Usage:"),
7578 _("List printers on a remote RPC server"));
7579 return 0;
7582 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7583 rpc_printer_list_internals,
7584 argc, argv);
7588 * List printer-drivers on a remote RPC server.
7590 * @param c A net_context structure.
7591 * @param argc Standard main() style argc.
7592 * @param argv Standard main() style argv. Initial components are already
7593 * stripped.
7595 * @return A shell status integer (0 for success).
7597 static int rpc_printer_driver_list(struct net_context *c, int argc,
7598 const char **argv)
7600 if (c->display_usage) {
7601 d_printf( "%s\n"
7602 "net rpc printer driver\n"
7603 " %s\n",
7604 _("Usage:"),
7605 _("List printer-drivers on a remote RPC server"));
7606 return 0;
7609 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7610 rpc_printer_driver_list_internals,
7611 argc, argv);
7615 * Publish printer in ADS via MSRPC.
7617 * @param c A net_context structure.
7618 * @param argc Standard main() style argc.
7619 * @param argv Standard main() style argv. Initial components are already
7620 * stripped.
7622 * @return A shell status integer (0 for success).
7624 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7625 const char **argv)
7627 if (c->display_usage) {
7628 d_printf( "%s\n"
7629 "net rpc printer publish publish\n"
7630 " %s\n",
7631 _("Usage:"),
7632 _("Publish printer in ADS via MSRPC"));
7633 return 0;
7636 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7637 rpc_printer_publish_publish_internals,
7638 argc, argv);
7642 * Update printer in ADS via MSRPC.
7644 * @param c A net_context structure.
7645 * @param argc Standard main() style argc.
7646 * @param argv Standard main() style argv. Initial components are already
7647 * stripped.
7649 * @return A shell status integer (0 for success).
7651 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7653 if (c->display_usage) {
7654 d_printf( "%s\n"
7655 "net rpc printer publish update\n"
7656 " %s\n",
7657 _("Usage:"),
7658 _("Update printer in ADS via MSRPC"));
7659 return 0;
7662 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7663 rpc_printer_publish_update_internals,
7664 argc, argv);
7668 * UnPublish printer in ADS via MSRPC.
7670 * @param c A net_context structure.
7671 * @param argc Standard main() style argc.
7672 * @param argv Standard main() style argv. Initial components are already
7673 * stripped.
7675 * @return A shell status integer (0 for success).
7677 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7678 const char **argv)
7680 if (c->display_usage) {
7681 d_printf( "%s\n"
7682 "net rpc printer publish unpublish\n"
7683 " %s\n",
7684 _("Usage:\n"),
7685 _("UnPublish printer in ADS via MSRPC"));
7686 return 0;
7689 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7690 rpc_printer_publish_unpublish_internals,
7691 argc, argv);
7695 * List published printers via MSRPC.
7697 * @param c A net_context structure.
7698 * @param argc Standard main() style argc.
7699 * @param argv Standard main() style argv. Initial components are already
7700 * stripped.
7702 * @return A shell status integer (0 for success).
7704 static int rpc_printer_publish_list(struct net_context *c, int argc,
7705 const char **argv)
7707 if (c->display_usage) {
7708 d_printf( "%s\n"
7709 "net rpc printer publish list\n"
7710 " %s\n",
7711 _("Usage:"),
7712 _("List published printers via MSRPC"));
7713 return 0;
7716 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7717 rpc_printer_publish_list_internals,
7718 argc, argv);
7723 * Publish printer in ADS.
7725 * @param c A net_context structure.
7726 * @param argc Standard main() style argc.
7727 * @param argv Standard main() style argv. Initial components are already
7728 * stripped.
7730 * @return A shell status integer (0 for success).
7732 static int rpc_printer_publish(struct net_context *c, int argc,
7733 const char **argv)
7736 struct functable func[] = {
7738 "publish",
7739 rpc_printer_publish_publish,
7740 NET_TRANSPORT_RPC,
7741 N_("Publish printer in AD"),
7742 N_("net rpc printer publish publish\n"
7743 " Publish printer in AD")
7746 "update",
7747 rpc_printer_publish_update,
7748 NET_TRANSPORT_RPC,
7749 N_("Update printer in AD"),
7750 N_("net rpc printer publish update\n"
7751 " Update printer in AD")
7754 "unpublish",
7755 rpc_printer_publish_unpublish,
7756 NET_TRANSPORT_RPC,
7757 N_("Unpublish printer"),
7758 N_("net rpc printer publish unpublish\n"
7759 " Unpublish printer")
7762 "list",
7763 rpc_printer_publish_list,
7764 NET_TRANSPORT_RPC,
7765 N_("List published printers"),
7766 N_("net rpc printer publish list\n"
7767 " List published printers")
7769 {NULL, NULL, 0, NULL, NULL}
7772 if (argc == 0) {
7773 if (c->display_usage) {
7774 d_printf(_("Usage:\n"));
7775 d_printf(_("net rpc printer publish\n"
7776 " List published printers\n"
7777 " Alias of net rpc printer publish "
7778 "list\n"));
7779 net_display_usage_from_functable(func);
7780 return 0;
7782 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7783 rpc_printer_publish_list_internals,
7784 argc, argv);
7787 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7793 * Display rpc printer help page.
7795 * @param c A net_context structure.
7796 * @param argc Standard main() style argc.
7797 * @param argv Standard main() style argv. Initial components are already
7798 * stripped.
7800 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7802 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7803 "\tlists all printers on print-server\n\n"));
7804 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7805 "\tlists all printer-drivers on print-server\n\n"));
7806 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7807 "\tpublishes printer settings in Active Directory\n"
7808 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7809 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7810 "\n\tmigrates printers from remote to local server\n\n"));
7811 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7812 "\n\tmigrates printer-settings from remote to local server\n\n"));
7813 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7814 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7815 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7816 "\n\tmigrates printer-forms from remote to local server\n\n"));
7817 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7818 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7819 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7820 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7821 "\tremote to local print-server\n\n"));
7822 net_common_methods_usage(c, argc, argv);
7823 net_common_flags_usage(c, argc, argv);
7824 d_printf(_(
7825 "\t-v or --verbose\t\t\tgive verbose output\n"
7826 "\t --destination\t\tmigration target server (default: localhost)\n"));
7828 return -1;
7832 * 'net rpc printer' entrypoint.
7834 * @param c A net_context structure.
7835 * @param argc Standard main() style argc.
7836 * @param argv Standard main() style argv. Initial components are already
7837 * stripped.
7839 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7841 struct functable func[] = {
7843 "list",
7844 rpc_printer_list,
7845 NET_TRANSPORT_RPC,
7846 N_("List all printers on print server"),
7847 N_("net rpc printer list\n"
7848 " List all printers on print server")
7851 "migrate",
7852 rpc_printer_migrate,
7853 NET_TRANSPORT_RPC,
7854 N_("Migrate printer to local server"),
7855 N_("net rpc printer migrate\n"
7856 " Migrate printer to local server")
7859 "driver",
7860 rpc_printer_driver_list,
7861 NET_TRANSPORT_RPC,
7862 N_("List printer drivers"),
7863 N_("net rpc printer driver\n"
7864 " List printer drivers")
7867 "publish",
7868 rpc_printer_publish,
7869 NET_TRANSPORT_RPC,
7870 N_("Publish printer in AD"),
7871 N_("net rpc printer publish\n"
7872 " Publish printer in AD")
7874 {NULL, NULL, 0, NULL, NULL}
7877 if (argc == 0) {
7878 if (c->display_usage) {
7879 d_printf(_("Usage:\n"));
7880 d_printf(_("net rpc printer\n"
7881 " List printers\n"));
7882 net_display_usage_from_functable(func);
7883 return 0;
7885 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7886 rpc_printer_list_internals,
7887 argc, argv);
7890 return net_run_function(c, argc, argv, "net rpc printer", func);
7894 * 'net rpc' entrypoint.
7896 * @param c A net_context structure.
7897 * @param argc Standard main() style argc.
7898 * @param argv Standard main() style argv. Initial components are already
7899 * stripped.
7902 int net_rpc(struct net_context *c, int argc, const char **argv)
7904 NET_API_STATUS status;
7906 struct functable func[] = {
7908 "audit",
7909 net_rpc_audit,
7910 NET_TRANSPORT_RPC,
7911 N_("Modify global audit settings"),
7912 N_("net rpc audit\n"
7913 " Modify global audit settings")
7916 "info",
7917 net_rpc_info,
7918 NET_TRANSPORT_RPC,
7919 N_("Show basic info about a domain"),
7920 N_("net rpc info\n"
7921 " Show basic info about a domain")
7924 "join",
7925 net_rpc_join,
7926 NET_TRANSPORT_RPC,
7927 N_("Join a domain"),
7928 N_("net rpc join\n"
7929 " Join a domain")
7932 "oldjoin",
7933 net_rpc_oldjoin,
7934 NET_TRANSPORT_RPC,
7935 N_("Join a domain created in server manager"),
7936 N_("net rpc oldjoin\n"
7937 " Join a domain created in server manager")
7940 "testjoin",
7941 net_rpc_testjoin,
7942 NET_TRANSPORT_RPC,
7943 N_("Test that a join is valid"),
7944 N_("net rpc testjoin\n"
7945 " Test that a join is valid")
7948 "user",
7949 net_rpc_user,
7950 NET_TRANSPORT_RPC,
7951 N_("List/modify users"),
7952 N_("net rpc user\n"
7953 " List/modify users")
7956 "password",
7957 rpc_user_password,
7958 NET_TRANSPORT_RPC,
7959 N_("Change a user password"),
7960 N_("net rpc password\n"
7961 " Change a user password\n"
7962 " Alias for net rpc user password")
7965 "group",
7966 net_rpc_group,
7967 NET_TRANSPORT_RPC,
7968 N_("List/modify groups"),
7969 N_("net rpc group\n"
7970 " List/modify groups")
7973 "share",
7974 net_rpc_share,
7975 NET_TRANSPORT_RPC,
7976 N_("List/modify shares"),
7977 N_("net rpc share\n"
7978 " List/modify shares")
7981 "file",
7982 net_rpc_file,
7983 NET_TRANSPORT_RPC,
7984 N_("List open files"),
7985 N_("net rpc file\n"
7986 " List open files")
7989 "printer",
7990 net_rpc_printer,
7991 NET_TRANSPORT_RPC,
7992 N_("List/modify printers"),
7993 N_("net rpc printer\n"
7994 " List/modify printers")
7997 "changetrustpw",
7998 net_rpc_changetrustpw,
7999 NET_TRANSPORT_RPC,
8000 N_("Change trust account password"),
8001 N_("net rpc changetrustpw\n"
8002 " Change trust account password")
8005 "trustdom",
8006 rpc_trustdom,
8007 NET_TRANSPORT_RPC,
8008 N_("Modify domain trusts"),
8009 N_("net rpc trustdom\n"
8010 " Modify domain trusts")
8013 "abortshutdown",
8014 rpc_shutdown_abort,
8015 NET_TRANSPORT_RPC,
8016 N_("Abort a remote shutdown"),
8017 N_("net rpc abortshutdown\n"
8018 " Abort a remote shutdown")
8021 "shutdown",
8022 rpc_shutdown,
8023 NET_TRANSPORT_RPC,
8024 N_("Shutdown a remote server"),
8025 N_("net rpc shutdown\n"
8026 " Shutdown a remote server")
8029 "samdump",
8030 rpc_samdump,
8031 NET_TRANSPORT_RPC,
8032 N_("Dump SAM data of remote NT PDC"),
8033 N_("net rpc samdump\n"
8034 " Dump SAM data of remote NT PDC")
8037 "vampire",
8038 rpc_vampire,
8039 NET_TRANSPORT_RPC,
8040 N_("Sync a remote NT PDC's data into local passdb"),
8041 N_("net rpc vampire\n"
8042 " Sync a remote NT PDC's data into local passdb")
8045 "getsid",
8046 net_rpc_getsid,
8047 NET_TRANSPORT_RPC,
8048 N_("Fetch the domain sid into local secrets.tdb"),
8049 N_("net rpc getsid\n"
8050 " Fetch the domain sid into local secrets.tdb")
8053 "rights",
8054 net_rpc_rights,
8055 NET_TRANSPORT_RPC,
8056 N_("Manage privileges assigned to SID"),
8057 N_("net rpc rights\n"
8058 " Manage privileges assigned to SID")
8061 "service",
8062 net_rpc_service,
8063 NET_TRANSPORT_RPC,
8064 N_("Start/stop/query remote services"),
8065 N_("net rpc service\n"
8066 " Start/stop/query remote services")
8069 "registry",
8070 net_rpc_registry,
8071 NET_TRANSPORT_RPC,
8072 N_("Manage registry hives"),
8073 N_("net rpc registry\n"
8074 " Manage registry hives")
8077 "shell",
8078 net_rpc_shell,
8079 NET_TRANSPORT_RPC,
8080 N_("Open interactive shell on remote server"),
8081 N_("net rpc shell\n"
8082 " Open interactive shell on remote server")
8085 "trust",
8086 net_rpc_trust,
8087 NET_TRANSPORT_RPC,
8088 N_("Manage trusts"),
8089 N_("net rpc trust\n"
8090 " Manage trusts")
8093 "conf",
8094 net_rpc_conf,
8095 NET_TRANSPORT_RPC,
8096 N_("Configure a remote samba server"),
8097 N_("net rpc conf\n"
8098 " Configure a remote samba server")
8100 {NULL, NULL, 0, NULL, NULL}
8103 status = libnetapi_net_init(&c->netapi_ctx);
8104 if (status != 0) {
8105 return -1;
8107 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8108 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8109 if (c->opt_kerberos) {
8110 libnetapi_set_use_kerberos(c->netapi_ctx);
8112 if (c->opt_ccache) {
8113 libnetapi_set_use_ccache(c->netapi_ctx);
8116 return net_run_function(c, argc, argv, "net rpc", func);