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/>. */
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"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "librpc/gen_ndr/libnet_join.h"
41 #include "libnet/libnet_join.h"
42 #include "rpc_client/init_lsa.h"
43 #include "../libcli/security/security.h"
44 #include "libsmb/libsmb.h"
45 #include "libsmb/clirap.h"
46 #include "nsswitch/libwbclient/wbclient.h"
48 #include "../libcli/smb/smbXcli_base.h"
50 static int net_mode_share;
51 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
56 * @brief RPC based subcommands for the 'net' utility.
58 * This file should contain much of the functionality that used to
59 * be found in rpcclient, execpt that the commands should change
60 * less often, and the fucntionality should be sane (the user is not
61 * expected to know a rid/sid before they conduct an operation etc.)
63 * @todo Perhaps eventually these should be split out into a number
64 * of files, as this could get quite big.
69 * Many of the RPC functions need the domain sid. This function gets
70 * it at the start of every run
72 * @param cli A cli_state already connected to the remote machine
74 * @return The Domain SID of the remote machine.
77 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
78 struct dom_sid **domain_sid,
79 const char **domain_name)
81 struct rpc_pipe_client *lsa_pipe = NULL;
82 struct policy_handle pol;
83 NTSTATUS status, result;
84 union lsa_PolicyInformation *info = NULL;
85 struct dcerpc_binding_handle *b;
87 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
89 if (!NT_STATUS_IS_OK(status)) {
90 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
94 b = lsa_pipe->binding_handle;
96 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
97 SEC_FLAG_MAXIMUM_ALLOWED,
99 if (!NT_STATUS_IS_OK(status)) {
100 d_fprintf(stderr, "open_policy %s: %s\n",
106 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
108 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
111 if (!NT_STATUS_IS_OK(status)) {
112 d_fprintf(stderr, "lsaquery %s: %s\n",
117 if (!NT_STATUS_IS_OK(result)) {
118 d_fprintf(stderr, "lsaquery %s: %s\n",
124 *domain_name = info->account_domain.name.string;
125 *domain_sid = info->account_domain.sid;
127 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
128 TALLOC_FREE(lsa_pipe);
134 * Run a single RPC command, from start to finish.
136 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
137 * @param conn_flag a NET_FLAG_ combination. Passed to
138 * net_make_ipc_connection.
139 * @param argc Standard main() style argc.
140 * @param argv Standard main() style argv. Initial components are already
142 * @return A shell status integer (0 for success).
145 int run_rpc_command(struct net_context *c,
146 struct cli_state *cli_arg,
147 const struct ndr_interface_table *table,
153 struct cli_state *cli = NULL;
154 struct rpc_pipe_client *pipe_hnd = NULL;
157 struct dom_sid *domain_sid;
158 const char *domain_name;
161 /* make use of cli_state handed over as an argument, if possible */
163 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 DEBUG(1, ("failed to make ipc connection: %s\n",
166 nt_errstr(nt_status)));
179 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
180 DEBUG(0, ("talloc_init() failed\n"));
184 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
186 if (!NT_STATUS_IS_OK(nt_status)) {
190 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
191 if (lp_client_schannel()
192 && (ndr_syntax_id_equal(&table->syntax_id,
193 &ndr_table_netlogon.syntax_id))) {
194 /* Always try and create an schannel netlogon pipe. */
195 nt_status = cli_rpc_pipe_open_schannel(
196 cli, table, NCACN_NP,
197 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
199 if (!NT_STATUS_IS_OK(nt_status)) {
200 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
201 nt_errstr(nt_status) ));
205 if (conn_flags & NET_FLAGS_SEAL) {
206 nt_status = cli_rpc_pipe_open_generic_auth(
208 (conn_flags & NET_FLAGS_TCP) ?
209 NCACN_IP_TCP : NCACN_NP,
210 DCERPC_AUTH_TYPE_NTLMSSP,
211 DCERPC_AUTH_LEVEL_PRIVACY,
212 smbXcli_conn_remote_name(cli->conn),
213 lp_workgroup(), c->opt_user_name,
214 c->opt_password, &pipe_hnd);
216 nt_status = cli_rpc_pipe_open_noauth(
220 if (!NT_STATUS_IS_OK(nt_status)) {
221 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
223 nt_errstr(nt_status) ));
229 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
231 if (!NT_STATUS_IS_OK(nt_status)) {
232 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
235 DEBUG(5, ("rpc command function succedded\n"));
238 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
240 TALLOC_FREE(pipe_hnd);
245 /* close the connection only if it was opened here */
250 talloc_destroy(mem_ctx);
255 * Force a change of the trust acccount password.
257 * All parameters are provided by the run_rpc_command function, except for
258 * argc, argv which are passed through.
260 * @param domain_sid The domain sid acquired from the remote server.
261 * @param cli A cli_state connected to the server.
262 * @param mem_ctx Talloc context, destroyed on completion of the function.
263 * @param argc Standard main() style argc.
264 * @param argv Standard main() style argv. Initial components are already
267 * @return Normal NTSTATUS return.
270 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
271 const struct dom_sid *domain_sid,
272 const char *domain_name,
273 struct cli_state *cli,
274 struct rpc_pipe_client *pipe_hnd,
281 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
282 if (!NT_STATUS_IS_OK(status)) {
283 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
292 * Force a change of the trust acccount password.
294 * @param argc Standard main() style argc.
295 * @param argv Standard main() style argv. Initial components are already
298 * @return A shell status integer (0 for success).
301 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
303 if (c->display_usage) {
305 "net rpc changetrustpw\n"
308 _("Change the machine trust password"));
312 return run_rpc_command(c, NULL, &ndr_table_netlogon,
313 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
314 rpc_changetrustpw_internals,
319 * Join a domain, the old way. This function exists to allow
320 * the message to be displayed when oldjoin was explicitly
321 * requested, but not when it was implied by "net rpc join".
323 * This uses 'machinename' as the inital password, and changes it.
325 * The password should be created with 'server manager' or equiv first.
327 * @param argc Standard main() style argc.
328 * @param argv Standard main() style argv. Initial components are already
331 * @return A shell status integer (0 for success).
334 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
336 struct libnet_JoinCtx *r = NULL;
339 const char *domain = lp_workgroup(); /* FIXME */
340 bool modify_config = lp_config_backend_is_registry();
341 enum netr_SchannelType sec_chan_type;
344 if (c->display_usage) {
347 " Join a domain the old way\n");
351 mem_ctx = talloc_init("net_rpc_oldjoin");
356 werr = libnet_init_JoinCtx(mem_ctx, &r);
357 if (!W_ERROR_IS_OK(werr)) {
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.
366 sec_chan_type = get_sec_channel_type(argv[0]);
368 sec_chan_type = get_sec_channel_type(NULL);
372 d_fprintf(stderr, _("Could not initialise message context. "
373 "Try running as root\n"));
374 werr = WERR_ACCESS_DENIED;
378 pw = talloc_strndup(r, lp_netbios_name(), 14);
384 r->in.msg_ctx = c->msg_ctx;
385 r->in.domain_name = domain;
386 r->in.secure_channel_type = sec_chan_type;
387 r->in.dc_name = c->opt_host;
388 r->in.admin_account = "";
389 r->in.admin_password = strlower_talloc(r, pw);
390 if (r->in.admin_password == NULL) {
395 r->in.modify_config = modify_config;
396 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
397 WKSSVC_JOIN_FLAGS_JOIN_UNSECURE |
398 WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED;
400 werr = libnet_Join(mem_ctx, r);
401 if (!W_ERROR_IS_OK(werr)) {
405 /* Check the short name of the domain */
407 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
408 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
409 d_printf("domain name obtained from the server.\n");
410 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
411 d_printf("You should set \"workgroup = %s\" in %s.\n",
412 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
415 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
417 if (r->out.dns_domain_name) {
418 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
419 r->out.dns_domain_name);
421 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
422 r->out.netbios_domain_name);
425 TALLOC_FREE(mem_ctx);
430 if (c->opt_flags & NET_FLAGS_EXPECT_FALLBACK) {
434 /* issue an overall failure message at the end. */
435 d_fprintf(stderr, _("Failed to join domain: %s\n"),
436 r && r->out.error_string ? r->out.error_string :
437 get_friendly_werror_msg(werr));
440 TALLOC_FREE(mem_ctx);
446 * check that a join is OK
448 * @return A shell status integer (0 for success)
451 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
455 const char *domain = c->opt_target_workgroup;
456 const char *dc = c->opt_host;
458 if (c->display_usage) {
461 " Test if a join is OK\n");
465 mem_ctx = talloc_init("net_rpc_testjoin");
471 struct netr_DsRGetDCNameInfo *info;
474 d_fprintf(stderr, _("Could not initialise message context. "
475 "Try running as root\n"));
476 talloc_destroy(mem_ctx);
480 status = dsgetdcname(mem_ctx,
487 if (!NT_STATUS_IS_OK(status)) {
488 talloc_destroy(mem_ctx);
492 dc = strip_hostname(info->dc_unc);
495 /* Display success or failure */
496 status = libnet_join_ok(c->opt_workgroup, lp_netbios_name(), dc,
498 if (!NT_STATUS_IS_OK(status)) {
499 fprintf(stderr,"Join to domain '%s' is not valid: %s\n",
500 domain, nt_errstr(status));
501 talloc_destroy(mem_ctx);
505 printf("Join to '%s' is OK\n",domain);
506 talloc_destroy(mem_ctx);
512 * Join a domain using the administrator username and password
514 * @param argc Standard main() style argc
515 * @param argc Standard main() style argv. Initial components are already
516 * stripped. Currently not used.
517 * @return A shell status integer (0 for success)
521 static int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
523 struct libnet_JoinCtx *r = NULL;
526 const char *domain = lp_workgroup(); /* FIXME */
527 bool modify_config = lp_config_backend_is_registry();
528 enum netr_SchannelType sec_chan_type;
530 if (c->display_usage) {
533 " Join a domain the new way\n");
537 mem_ctx = talloc_init("net_rpc_join_newstyle");
542 werr = libnet_init_JoinCtx(mem_ctx, &r);
543 if (!W_ERROR_IS_OK(werr)) {
548 check what type of join - if the user want's to join as
549 a BDC, the server must agree that we are a BDC.
552 sec_chan_type = get_sec_channel_type(argv[0]);
554 sec_chan_type = get_sec_channel_type(NULL);
558 d_fprintf(stderr, _("Could not initialise message context. "
559 "Try running as root\n"));
560 werr = WERR_ACCESS_DENIED;
564 r->in.msg_ctx = c->msg_ctx;
565 r->in.domain_name = domain;
566 r->in.secure_channel_type = sec_chan_type;
567 r->in.dc_name = c->opt_host;
568 r->in.admin_account = c->opt_user_name;
569 r->in.admin_password = net_prompt_pass(c, c->opt_user_name);
571 r->in.use_kerberos = c->opt_kerberos;
572 r->in.modify_config = modify_config;
573 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
574 WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
575 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
577 werr = libnet_Join(mem_ctx, r);
578 if (!W_ERROR_IS_OK(werr)) {
582 /* Check the short name of the domain */
584 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
585 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
586 d_printf("domain name obtained from the server.\n");
587 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
588 d_printf("You should set \"workgroup = %s\" in %s.\n",
589 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
592 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
594 if (r->out.dns_domain_name) {
595 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
596 r->out.dns_domain_name);
598 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
599 r->out.netbios_domain_name);
602 TALLOC_FREE(mem_ctx);
607 /* issue an overall failure message at the end. */
608 d_printf("Failed to join domain: %s\n",
609 r && r->out.error_string ? r->out.error_string :
610 get_friendly_werror_msg(werr));
612 TALLOC_FREE(mem_ctx);
618 * 'net rpc join' entrypoint.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
623 * Main 'net_rpc_join()' (where the admin username/password is used) is
625 * Try to just change the password, but if that doesn't work, use/prompt
626 * for a username/password.
629 int net_rpc_join(struct net_context *c, int argc, const char **argv)
633 if (c->display_usage) {
636 _("net rpc join -U <username>[%%password] <type>\n"
638 " username\tName of the admin user"
639 " password\tPassword of the admin user, will "
640 "prompt if not specified\n"
641 " type\tCan be one of the following:\n"
642 "\t\tMEMBER\tJoin as member server (default)\n"
643 "\t\tBDC\tJoin as BDC\n"
644 "\t\tPDC\tJoin as PDC\n"));
648 if (lp_server_role() == ROLE_STANDALONE) {
649 d_printf(_("cannot join as standalone machine\n"));
653 if (strlen(lp_netbios_name()) > 15) {
654 d_printf(_("Our netbios name can be at most 15 chars long, "
655 "\"%s\" is %u chars long\n"),
656 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
660 c->opt_flags |= NET_FLAGS_EXPECT_FALLBACK;
661 ret = net_rpc_oldjoin(c, argc, argv);
662 c->opt_flags &= ~NET_FLAGS_EXPECT_FALLBACK;
667 return net_rpc_join_newstyle(c, argc, argv);
671 * display info about a rpc domain
673 * All parameters are provided by the run_rpc_command function, except for
674 * argc, argv which are passed through.
676 * @param domain_sid The domain sid acquired from the remote server
677 * @param cli A cli_state connected to the server.
678 * @param mem_ctx Talloc context, destroyed on completion of the function.
679 * @param argc Standard main() style argc.
680 * @param argv Standard main() style argv. Initial components are already
683 * @return Normal NTSTATUS return.
686 NTSTATUS rpc_info_internals(struct net_context *c,
687 const struct dom_sid *domain_sid,
688 const char *domain_name,
689 struct cli_state *cli,
690 struct rpc_pipe_client *pipe_hnd,
695 struct policy_handle connect_pol, domain_pol;
696 NTSTATUS status, result;
697 union samr_DomainInfo *info = NULL;
699 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
701 sid_to_fstring(sid_str, domain_sid);
703 /* Get sam policy handle */
704 status = dcerpc_samr_Connect2(b, mem_ctx,
706 MAXIMUM_ALLOWED_ACCESS,
709 if (!NT_STATUS_IS_OK(status)) {
710 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
715 if (!NT_STATUS_IS_OK(result)) {
717 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
722 /* Get domain policy handle */
723 status = dcerpc_samr_OpenDomain(b, mem_ctx,
725 MAXIMUM_ALLOWED_ACCESS,
726 discard_const_p(struct dom_sid2, domain_sid),
729 if (!NT_STATUS_IS_OK(status)) {
730 d_fprintf(stderr, _("Could not open domain: %s\n"),
734 if (!NT_STATUS_IS_OK(result)) {
736 d_fprintf(stderr, _("Could not open domain: %s\n"),
741 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
746 if (!NT_STATUS_IS_OK(status)) {
750 if (NT_STATUS_IS_OK(result)) {
751 d_printf(_("Domain Name: %s\n"),
752 info->general.domain_name.string);
753 d_printf(_("Domain SID: %s\n"), sid_str);
754 d_printf(_("Sequence number: %llu\n"),
755 (unsigned long long)info->general.sequence_num);
756 d_printf(_("Num users: %u\n"), info->general.num_users);
757 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
758 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
766 * 'net rpc info' entrypoint.
767 * @param argc Standard main() style argc.
768 * @param argv Standard main() style argv. Initial components are already
772 int net_rpc_info(struct net_context *c, int argc, const char **argv)
774 if (c->display_usage) {
779 _("Display information about the domain"));
783 return run_rpc_command(c, NULL, &ndr_table_samr,
784 NET_FLAGS_PDC, rpc_info_internals,
789 * Fetch domain SID into the local secrets.tdb.
791 * All parameters are provided by the run_rpc_command function, except for
792 * argc, argv which are passed through.
794 * @param domain_sid The domain sid acquired from the remote server.
795 * @param cli A cli_state connected to the server.
796 * @param mem_ctx Talloc context, destroyed on completion of the function.
797 * @param argc Standard main() style argc.
798 * @param argv Standard main() style argv. Initial components are already
801 * @return Normal NTSTATUS return.
804 static NTSTATUS rpc_getsid_internals(struct net_context *c,
805 const struct dom_sid *domain_sid,
806 const char *domain_name,
807 struct cli_state *cli,
808 struct rpc_pipe_client *pipe_hnd,
815 sid_to_fstring(sid_str, domain_sid);
816 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
817 sid_str, domain_name);
819 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
820 DEBUG(0,("Can't store domain SID\n"));
821 return NT_STATUS_UNSUCCESSFUL;
828 * 'net rpc getsid' entrypoint.
829 * @param argc Standard main() style argc.
830 * @param argv Standard main() style argv. Initial components are already
834 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
836 int conn_flags = NET_FLAGS_PDC;
838 if (!c->opt_user_specified) {
839 conn_flags |= NET_FLAGS_ANONYMOUS;
842 if (c->display_usage) {
847 _("Fetch domain SID into local secrets.tdb"));
851 return run_rpc_command(c, NULL, &ndr_table_samr,
853 rpc_getsid_internals,
857 /****************************************************************************/
860 * Basic usage function for 'net rpc user'.
861 * @param argc Standard main() style argc.
862 * @param argv Standard main() style argv. Initial components are already
866 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
868 return net_user_usage(c, argc, argv);
872 * Add a new user to a remote RPC server.
874 * @param argc Standard main() style argc.
875 * @param argv Standard main() style argv. Initial components are already
878 * @return A shell status integer (0 for success).
881 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
883 NET_API_STATUS status;
884 struct USER_INFO_1 info1;
885 uint32_t parm_error = 0;
887 if (argc < 1 || c->display_usage) {
888 rpc_user_usage(c, argc, argv);
894 info1.usri1_name = argv[0];
896 info1.usri1_password = argv[1];
899 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
902 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
903 argv[0], libnetapi_get_error_string(c->netapi_ctx,
907 d_printf(_("Added user '%s'.\n"), argv[0]);
914 * Rename a user on a remote RPC server.
916 * @param argc Standard main() style argc.
917 * @param argv Standard main() style argv. Initial components are already
920 * @return A shell status integer (0 for success).
923 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
925 NET_API_STATUS status;
926 struct USER_INFO_0 u0;
927 uint32_t parm_err = 0;
929 if (argc != 2 || c->display_usage) {
930 rpc_user_usage(c, argc, argv);
934 u0.usri0_name = argv[1];
936 status = NetUserSetInfo(c->opt_host, argv[0],
937 0, (uint8_t *)&u0, &parm_err);
940 _("Failed to rename user from %s to %s - %s\n"),
942 libnetapi_get_error_string(c->netapi_ctx, status));
944 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
951 * Set a user's primary group
953 * @param argc Standard main() style argc.
954 * @param argv Standard main() style argv. Initial components are already
957 * @return A shell status integer (0 for success).
960 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
963 NET_API_STATUS status;
965 struct GROUP_INFO_2 *g2;
966 struct USER_INFO_1051 u1051;
967 uint32_t parm_err = 0;
969 if (argc != 2 || c->display_usage) {
970 rpc_user_usage(c, argc, argv);
974 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
976 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
978 libnetapi_get_error_string(c->netapi_ctx, status));
981 g2 = (struct GROUP_INFO_2 *)buffer;
983 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
985 NetApiBufferFree(buffer);
987 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
988 (uint8_t *)&u1051, &parm_err);
991 _("Failed to set user's primary group %s to %s - "
992 "%s\n"), argv[0], argv[1],
993 libnetapi_get_error_string(c->netapi_ctx, status));
995 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
1002 * Delete a user from a remote RPC server.
1004 * @param argc Standard main() style argc.
1005 * @param argv Standard main() style argv. Initial components are already
1008 * @return A shell status integer (0 for success).
1011 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
1013 NET_API_STATUS status;
1015 if (argc < 1 || c->display_usage) {
1016 rpc_user_usage(c, argc, argv);
1020 status = NetUserDel(c->opt_host, argv[0]);
1023 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
1025 libnetapi_get_error_string(c->netapi_ctx, status));
1028 d_printf(_("Deleted user '%s'.\n"), argv[0]);
1035 * Set a user's password on a remote RPC server.
1037 * @param argc Standard main() style argc.
1038 * @param argv Standard main() style argv. Initial components are already
1041 * @return A shell status integer (0 for success).
1044 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
1046 NET_API_STATUS status;
1047 char *prompt = NULL;
1048 struct USER_INFO_1003 u1003;
1049 uint32_t parm_err = 0;
1052 if (argc < 1 || c->display_usage) {
1053 rpc_user_usage(c, argc, argv);
1058 u1003.usri1003_password = argv[1];
1060 char pwd[256] = {0};
1061 ret = asprintf(&prompt, _("Enter new password for %s:"),
1067 ret = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
1073 u1003.usri1003_password = talloc_strdup(c, pwd);
1074 if (u1003.usri1003_password == NULL) {
1079 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
1081 /* Display results */
1084 _("Failed to set password for '%s' with error: %s.\n"),
1085 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1094 * List a user's groups from a remote RPC server.
1096 * @param argc Standard main() style argc.
1097 * @param argv Standard main() style argv. Initial components are already
1100 * @return A shell status integer (0 for success)
1103 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1106 NET_API_STATUS status;
1107 struct GROUP_USERS_INFO_0 *u0 = NULL;
1108 uint32_t entries_read = 0;
1109 uint32_t total_entries = 0;
1113 if (argc < 1 || c->display_usage) {
1114 rpc_user_usage(c, argc, argv);
1118 status = NetUserGetGroups(c->opt_host,
1121 (uint8_t **)(void *)&u0,
1127 _("Failed to get groups for '%s' with error: %s.\n"),
1128 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1133 for (i=0; i < entries_read; i++) {
1134 printf("%s\n", u0->grui0_name);
1142 * List users on a remote RPC server.
1144 * All parameters are provided by the run_rpc_command function, except for
1145 * argc, argv which are passed through.
1147 * @param domain_sid The domain sid acquired from the remote server.
1148 * @param cli A cli_state connected to the server.
1149 * @param mem_ctx Talloc context, destroyed on completion of the function.
1150 * @param argc Standard main() style argc.
1151 * @param argv Standard main() style argv. Initial components are already
1154 * @return Normal NTSTATUS return.
1157 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
1159 NET_API_STATUS status;
1160 uint32_t start_idx=0, num_entries, i, loop_count = 0;
1161 struct NET_DISPLAY_USER *info = NULL;
1162 void *buffer = NULL;
1164 /* Query domain users */
1165 if (c->opt_long_list_entries)
1166 d_printf(_("\nUser name Comment"
1167 "\n-----------------------------\n"));
1169 uint32_t max_entries, max_size;
1171 dcerpc_get_query_dispinfo_params(
1172 loop_count, &max_entries, &max_size);
1174 status = NetQueryDisplayInformation(c->opt_host,
1181 if (status != 0 && status != ERROR_MORE_DATA) {
1185 info = (struct NET_DISPLAY_USER *)buffer;
1187 for (i = 0; i < num_entries; i++) {
1189 if (c->opt_long_list_entries)
1190 printf("%-21.21s %s\n", info->usri1_name,
1191 info->usri1_comment);
1193 printf("%s\n", info->usri1_name);
1197 NetApiBufferFree(buffer);
1200 start_idx += num_entries;
1202 } while (status == ERROR_MORE_DATA);
1208 * 'net rpc user' entrypoint.
1209 * @param argc Standard main() style argc.
1210 * @param argv Standard main() style argv. Initial components are already
1214 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1216 NET_API_STATUS status;
1218 struct functable func[] = {
1223 N_("Add specified user"),
1224 N_("net rpc user add\n"
1225 " Add specified user")
1231 N_("List domain groups of user"),
1232 N_("net rpc user info\n"
1233 " List domain groups of user")
1239 N_("Remove specified user"),
1240 N_("net rpc user delete\n"
1241 " Remove specified user")
1247 N_("Change user password"),
1248 N_("net rpc user password\n"
1249 " Change user password")
1255 N_("Rename specified user"),
1256 N_("net rpc user rename\n"
1257 " Rename specified user")
1261 rpc_user_setprimarygroup,
1263 "Set a user's primary group",
1264 "net rpc user setprimarygroup\n"
1265 " Set a user's primary group"
1267 {NULL, NULL, 0, NULL, NULL}
1270 status = libnetapi_net_init(&c->netapi_ctx);
1274 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1275 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1276 if (c->opt_kerberos) {
1277 libnetapi_set_use_kerberos(c->netapi_ctx);
1281 if (c->display_usage) {
1286 _("List all users"));
1287 net_display_usage_from_functable(func);
1291 return rpc_user_list(c, argc, argv);
1294 return net_run_function(c, argc, argv, "net rpc user", func);
1297 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1298 TALLOC_CTX *mem_ctx,
1299 struct rpc_sh_ctx *ctx,
1300 struct rpc_pipe_client *pipe_hnd,
1301 int argc, const char **argv)
1303 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1306 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1307 TALLOC_CTX *mem_ctx,
1308 struct rpc_sh_ctx *ctx,
1309 struct rpc_pipe_client *pipe_hnd,
1310 int argc, const char **argv)
1312 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1315 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1316 TALLOC_CTX *mem_ctx,
1317 struct rpc_sh_ctx *ctx,
1318 struct rpc_pipe_client *pipe_hnd,
1319 int argc, const char **argv,
1321 struct net_context *c,
1322 TALLOC_CTX *mem_ctx,
1323 struct rpc_sh_ctx *ctx,
1324 struct rpc_pipe_client *pipe_hnd,
1325 struct policy_handle *user_hnd,
1326 int argc, const char **argv))
1328 struct policy_handle connect_pol, domain_pol, user_pol;
1329 NTSTATUS status, result;
1332 enum lsa_SidType type;
1333 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1336 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1338 return NT_STATUS_INVALID_PARAMETER;
1341 ZERO_STRUCT(connect_pol);
1342 ZERO_STRUCT(domain_pol);
1343 ZERO_STRUCT(user_pol);
1345 status = net_rpc_lookup_name(c, mem_ctx, ctx->cli,
1346 argv[0], NULL, NULL, &sid, &type);
1347 if (!NT_STATUS_IS_OK(status)) {
1348 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1353 if (type != SID_NAME_USER) {
1354 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1355 sid_type_lookup(type));
1356 status = NT_STATUS_NO_SUCH_USER;
1360 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1361 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1362 status = NT_STATUS_NO_SUCH_USER;
1366 status = dcerpc_samr_Connect2(b, mem_ctx,
1368 MAXIMUM_ALLOWED_ACCESS,
1371 if (!NT_STATUS_IS_OK(status)) {
1374 if (!NT_STATUS_IS_OK(result)) {
1379 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1381 MAXIMUM_ALLOWED_ACCESS,
1385 if (!NT_STATUS_IS_OK(status)) {
1388 if (!NT_STATUS_IS_OK(result)) {
1393 status = dcerpc_samr_OpenUser(b, mem_ctx,
1395 MAXIMUM_ALLOWED_ACCESS,
1399 if (!NT_STATUS_IS_OK(status)) {
1402 if (!NT_STATUS_IS_OK(result)) {
1407 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1410 if (is_valid_policy_hnd(&user_pol)) {
1411 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1413 if (is_valid_policy_hnd(&domain_pol)) {
1414 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1416 if (is_valid_policy_hnd(&connect_pol)) {
1417 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1422 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1423 TALLOC_CTX *mem_ctx,
1424 struct rpc_sh_ctx *ctx,
1425 struct rpc_pipe_client *pipe_hnd,
1426 struct policy_handle *user_hnd,
1427 int argc, const char **argv)
1429 NTSTATUS status, result;
1430 union samr_UserInfo *info = NULL;
1431 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1434 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1436 return NT_STATUS_INVALID_PARAMETER;
1439 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1444 if (!NT_STATUS_IS_OK(status)) {
1447 if (!NT_STATUS_IS_OK(result)) {
1451 d_printf(_("user rid: %d, group rid: %d\n"),
1453 info->info21.primary_gid);
1458 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1459 TALLOC_CTX *mem_ctx,
1460 struct rpc_sh_ctx *ctx,
1461 struct rpc_pipe_client *pipe_hnd,
1462 int argc, const char **argv)
1464 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1465 rpc_sh_user_show_internals);
1468 #define FETCHSTR(name, rec) \
1469 do { if (strequal(ctx->thiscmd, name)) { \
1470 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1473 #define SETSTR(name, rec, flag) \
1474 do { if (strequal(ctx->thiscmd, name)) { \
1475 init_lsa_String(&(info->info21.rec), argv[0]); \
1476 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1479 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1480 TALLOC_CTX *mem_ctx,
1481 struct rpc_sh_ctx *ctx,
1482 struct rpc_pipe_client *pipe_hnd,
1483 struct policy_handle *user_hnd,
1484 int argc, const char **argv)
1486 NTSTATUS status, result;
1487 const char *username;
1488 const char *oldval = "";
1489 union samr_UserInfo *info = NULL;
1490 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1493 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1494 _("Usage:"), ctx->whoami);
1495 return NT_STATUS_INVALID_PARAMETER;
1498 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1503 if (!NT_STATUS_IS_OK(status)) {
1506 if (!NT_STATUS_IS_OK(result)) {
1510 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1512 FETCHSTR("fullname", full_name);
1513 FETCHSTR("homedir", home_directory);
1514 FETCHSTR("homedrive", home_drive);
1515 FETCHSTR("logonscript", logon_script);
1516 FETCHSTR("profilepath", profile_path);
1517 FETCHSTR("description", description);
1520 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1524 if (strcmp(argv[0], "NULL") == 0) {
1528 ZERO_STRUCT(info->info21);
1530 SETSTR("fullname", full_name, FULL_NAME);
1531 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1532 SETSTR("homedrive", home_drive, HOME_DRIVE);
1533 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1534 SETSTR("profilepath", profile_path, PROFILE_PATH);
1535 SETSTR("description", description, DESCRIPTION);
1537 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1542 if (!NT_STATUS_IS_OK(status)) {
1548 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1549 ctx->thiscmd, oldval, argv[0]);
1556 #define HANDLEFLG(name, rec) \
1557 do { if (strequal(ctx->thiscmd, name)) { \
1558 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1560 newflags = oldflags | ACB_##rec; \
1562 newflags = oldflags & ~ACB_##rec; \
1565 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1566 TALLOC_CTX *mem_ctx,
1567 struct rpc_sh_ctx *ctx,
1568 struct rpc_pipe_client *pipe_hnd,
1569 int argc, const char **argv)
1571 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1572 rpc_sh_user_str_edit_internals);
1575 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1576 TALLOC_CTX *mem_ctx,
1577 struct rpc_sh_ctx *ctx,
1578 struct rpc_pipe_client *pipe_hnd,
1579 struct policy_handle *user_hnd,
1580 int argc, const char **argv)
1582 NTSTATUS status, result;
1583 const char *username;
1584 const char *oldval = "unknown";
1585 uint32 oldflags, newflags;
1587 union samr_UserInfo *info = NULL;
1588 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1591 ((argc == 1) && !strequal(argv[0], "yes") &&
1592 !strequal(argv[0], "no"))) {
1593 /* TRANSATORS: The yes|no here are program keywords. Please do
1595 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1597 return NT_STATUS_INVALID_PARAMETER;
1600 newval = strequal(argv[0], "yes");
1602 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1607 if (!NT_STATUS_IS_OK(status)) {
1610 if (!NT_STATUS_IS_OK(result)) {
1614 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1615 oldflags = info->info21.acct_flags;
1616 newflags = info->info21.acct_flags;
1618 HANDLEFLG("disabled", DISABLED);
1619 HANDLEFLG("pwnotreq", PWNOTREQ);
1620 HANDLEFLG("autolock", AUTOLOCK);
1621 HANDLEFLG("pwnoexp", PWNOEXP);
1624 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1629 ZERO_STRUCT(info->info21);
1631 info->info21.acct_flags = newflags;
1632 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1634 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1639 if (!NT_STATUS_IS_OK(status)) {
1643 if (NT_STATUS_IS_OK(result)) {
1644 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1645 ctx->thiscmd, oldval, argv[0]);
1653 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1654 TALLOC_CTX *mem_ctx,
1655 struct rpc_sh_ctx *ctx,
1656 struct rpc_pipe_client *pipe_hnd,
1657 int argc, const char **argv)
1659 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1660 rpc_sh_user_flag_edit_internals);
1663 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1664 TALLOC_CTX *mem_ctx,
1665 struct rpc_sh_ctx *ctx)
1667 static struct rpc_sh_cmd cmds[] = {
1669 { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1670 N_("Show/Set a user's full name") },
1672 { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1673 N_("Show/Set a user's home directory") },
1675 { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1676 N_("Show/Set a user's home drive") },
1678 { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1679 N_("Show/Set a user's logon script") },
1681 { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1682 N_("Show/Set a user's profile path") },
1684 { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1685 N_("Show/Set a user's description") },
1687 { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1688 N_("Show/Set whether a user is disabled") },
1690 { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1691 N_("Show/Set whether a user locked out") },
1693 { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1694 N_("Show/Set whether a user does not need a password") },
1696 { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1697 N_("Show/Set whether a user's password does not expire") },
1699 { NULL, NULL, 0, NULL, NULL }
1705 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1706 TALLOC_CTX *mem_ctx,
1707 struct rpc_sh_ctx *ctx)
1709 static struct rpc_sh_cmd cmds[] = {
1711 { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
1712 N_("List available users") },
1714 { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
1715 N_("List the domain groups a user is member of") },
1717 { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
1718 N_("Show info about a user") },
1720 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1721 N_("Show/Modify a user's fields") },
1723 { NULL, NULL, 0, NULL, NULL }
1729 /****************************************************************************/
1732 * Basic usage function for 'net rpc group'.
1733 * @param argc Standard main() style argc.
1734 * @param argv Standard main() style argv. Initial components are already
1738 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1740 return net_group_usage(c, argc, argv);
1744 * Delete group on a remote RPC server.
1746 * All parameters are provided by the run_rpc_command function, except for
1747 * argc, argv which are passed through.
1749 * @param domain_sid The domain sid acquired from the remote server.
1750 * @param cli A cli_state connected to the server.
1751 * @param mem_ctx Talloc context, destroyed on completion of the function.
1752 * @param argc Standard main() style argc.
1753 * @param argv Standard main() style argv. Initial components are already
1756 * @return Normal NTSTATUS return.
1759 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1760 const struct dom_sid *domain_sid,
1761 const char *domain_name,
1762 struct cli_state *cli,
1763 struct rpc_pipe_client *pipe_hnd,
1764 TALLOC_CTX *mem_ctx,
1768 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1769 bool group_is_primary = false;
1770 NTSTATUS status, result;
1772 struct samr_RidAttrArray *rids = NULL;
1775 /* struct samr_RidWithAttribute *user_gids; */
1776 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1778 struct samr_Ids group_rids, name_types;
1779 struct lsa_String lsa_acct_name;
1780 union samr_UserInfo *info = NULL;
1782 if (argc < 1 || c->display_usage) {
1783 rpc_group_usage(c, argc,argv);
1784 return NT_STATUS_OK; /* ok? */
1787 status = dcerpc_samr_Connect2(b, mem_ctx,
1789 MAXIMUM_ALLOWED_ACCESS,
1792 if (!NT_STATUS_IS_OK(status)) {
1793 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1797 if (!NT_STATUS_IS_OK(result)) {
1799 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1803 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1805 MAXIMUM_ALLOWED_ACCESS,
1806 discard_const_p(struct dom_sid2, domain_sid),
1809 if (!NT_STATUS_IS_OK(status)) {
1810 d_fprintf(stderr, _("Request open_domain failed\n"));
1814 if (!NT_STATUS_IS_OK(result)) {
1816 d_fprintf(stderr, _("Request open_domain failed\n"));
1820 init_lsa_String(&lsa_acct_name, argv[0]);
1822 status = dcerpc_samr_LookupNames(b, mem_ctx,
1829 if (!NT_STATUS_IS_OK(status)) {
1830 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1834 if (!NT_STATUS_IS_OK(result)) {
1836 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1840 switch (name_types.ids[0])
1842 case SID_NAME_DOM_GRP:
1843 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1845 MAXIMUM_ALLOWED_ACCESS,
1849 if (!NT_STATUS_IS_OK(status)) {
1850 d_fprintf(stderr, _("Request open_group failed"));
1854 if (!NT_STATUS_IS_OK(result)) {
1856 d_fprintf(stderr, _("Request open_group failed"));
1860 group_rid = group_rids.ids[0];
1862 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1866 if (!NT_STATUS_IS_OK(status)) {
1868 _("Unable to query group members of %s"),
1873 if (!NT_STATUS_IS_OK(result)) {
1876 _("Unable to query group members of %s"),
1881 if (c->opt_verbose) {
1883 _("Domain Group %s (rid: %d) has %d members\n"),
1884 argv[0],group_rid, rids->count);
1887 /* Check if group is anyone's primary group */
1888 for (i = 0; i < rids->count; i++)
1890 status = dcerpc_samr_OpenUser(b, mem_ctx,
1892 MAXIMUM_ALLOWED_ACCESS,
1896 if (!NT_STATUS_IS_OK(status)) {
1898 _("Unable to open group member %d\n"),
1903 if (!NT_STATUS_IS_OK(result)) {
1906 _("Unable to open group member %d\n"),
1911 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1916 if (!NT_STATUS_IS_OK(status)) {
1918 _("Unable to lookup userinfo for group "
1924 if (!NT_STATUS_IS_OK(result)) {
1927 _("Unable to lookup userinfo for group "
1933 if (info->info21.primary_gid == group_rid) {
1934 if (c->opt_verbose) {
1935 d_printf(_("Group is primary group "
1937 info->info21.account_name.string);
1939 group_is_primary = true;
1942 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1945 if (group_is_primary) {
1946 d_fprintf(stderr, _("Unable to delete group because "
1947 "some of it's members have it as primary "
1949 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1953 /* remove all group members */
1954 for (i = 0; i < rids->count; i++)
1957 d_printf(_("Remove group member %d..."),
1959 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1963 if (!NT_STATUS_IS_OK(status)) {
1967 if (NT_STATUS_IS_OK(result)) {
1969 d_printf(_("ok\n"));
1972 d_printf("%s\n", _("failed"));
1977 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1980 if (!NT_STATUS_IS_OK(status)) {
1987 /* removing a local group is easier... */
1988 case SID_NAME_ALIAS:
1989 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1991 MAXIMUM_ALLOWED_ACCESS,
1995 if (!NT_STATUS_IS_OK(status)) {
1996 d_fprintf(stderr, _("Request open_alias failed\n"));
1999 if (!NT_STATUS_IS_OK(result)) {
2001 d_fprintf(stderr, _("Request open_alias failed\n"));
2005 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
2008 if (!NT_STATUS_IS_OK(status)) {
2016 d_fprintf(stderr, _("%s is of type %s. This command is only "
2017 "for deleting local or global groups\n"),
2018 argv[0],sid_type_lookup(name_types.ids[0]));
2019 status = NT_STATUS_UNSUCCESSFUL;
2023 if (NT_STATUS_IS_OK(status)) {
2025 d_printf(_("Deleted %s '%s'\n"),
2026 sid_type_lookup(name_types.ids[0]), argv[0]);
2028 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
2029 get_friendly_nt_error_msg(status));
2037 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
2039 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2040 rpc_group_delete_internals, argc,argv);
2043 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
2045 NET_API_STATUS status;
2046 struct GROUP_INFO_1 info1;
2047 uint32_t parm_error = 0;
2049 if (argc != 1 || c->display_usage) {
2050 rpc_group_usage(c, argc, argv);
2056 info1.grpi1_name = argv[0];
2057 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2058 info1.grpi1_comment = c->opt_comment;
2061 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2065 _("Failed to add group '%s' with error: %s.\n"),
2066 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2070 d_printf(_("Added group '%s'.\n"), argv[0]);
2076 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
2078 NET_API_STATUS status;
2079 struct LOCALGROUP_INFO_1 info1;
2080 uint32_t parm_error = 0;
2082 if (argc != 1 || c->display_usage) {
2083 rpc_group_usage(c, argc, argv);
2089 info1.lgrpi1_name = argv[0];
2090 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2091 info1.lgrpi1_comment = c->opt_comment;
2094 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2098 _("Failed to add alias '%s' with error: %s.\n"),
2099 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2103 d_printf(_("Added alias '%s'.\n"), argv[0]);
2109 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
2111 if (c->opt_localgroup)
2112 return rpc_alias_add_internals(c, argc, argv);
2114 return rpc_group_add_internals(c, argc, argv);
2117 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2118 TALLOC_CTX *mem_ctx,
2120 struct dom_sid *sid,
2121 enum lsa_SidType *type)
2123 struct dom_sid *sids = NULL;
2124 enum lsa_SidType *types = NULL;
2125 struct rpc_pipe_client *pipe_hnd = NULL;
2126 struct policy_handle lsa_pol;
2127 NTSTATUS status, result;
2128 struct dcerpc_binding_handle *b;
2130 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
2132 if (!NT_STATUS_IS_OK(status)) {
2136 b = pipe_hnd->binding_handle;
2138 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2139 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2141 if (!NT_STATUS_IS_OK(status)) {
2145 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2146 &name, NULL, 1, &sids, &types);
2148 if (NT_STATUS_IS_OK(status)) {
2149 sid_copy(sid, &sids[0]);
2153 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
2157 TALLOC_FREE(pipe_hnd);
2160 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
2162 /* Try as S-1-5-whatever */
2164 struct dom_sid tmp_sid;
2166 if (string_to_sid(&tmp_sid, name)) {
2167 sid_copy(sid, &tmp_sid);
2168 *type = SID_NAME_UNKNOWN;
2169 status = NT_STATUS_OK;
2176 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2177 TALLOC_CTX *mem_ctx,
2178 const struct dom_sid *group_sid,
2181 struct policy_handle connect_pol, domain_pol;
2182 NTSTATUS status, result;
2184 struct policy_handle group_pol;
2185 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2187 struct samr_Ids rids, rid_types;
2188 struct lsa_String lsa_acct_name;
2192 sid_copy(&sid, group_sid);
2194 if (!sid_split_rid(&sid, &group_rid)) {
2195 return NT_STATUS_UNSUCCESSFUL;
2198 /* Get sam policy handle */
2199 status = dcerpc_samr_Connect2(b, mem_ctx,
2201 MAXIMUM_ALLOWED_ACCESS,
2204 if (!NT_STATUS_IS_OK(status)) {
2207 if (!NT_STATUS_IS_OK(result)) {
2211 /* Get domain policy handle */
2212 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2214 MAXIMUM_ALLOWED_ACCESS,
2218 if (!NT_STATUS_IS_OK(status)) {
2221 if (!NT_STATUS_IS_OK(result)) {
2225 init_lsa_String(&lsa_acct_name, member);
2227 status = dcerpc_samr_LookupNames(b, mem_ctx,
2234 if (!NT_STATUS_IS_OK(status)) {
2235 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2240 if (!NT_STATUS_IS_OK(result)) {
2242 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2247 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2249 MAXIMUM_ALLOWED_ACCESS,
2253 if (!NT_STATUS_IS_OK(status)) {
2257 if (!NT_STATUS_IS_OK(result)) {
2262 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2265 0x0005, /* unknown flags */
2267 if (!NT_STATUS_IS_OK(status)) {
2274 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2278 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2279 struct cli_state *cli,
2280 TALLOC_CTX *mem_ctx,
2281 const struct dom_sid *alias_sid,
2284 struct policy_handle connect_pol, domain_pol;
2285 NTSTATUS status, result;
2287 struct policy_handle alias_pol;
2288 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2290 struct dom_sid member_sid;
2291 enum lsa_SidType member_type;
2295 sid_copy(&sid, alias_sid);
2297 if (!sid_split_rid(&sid, &alias_rid)) {
2298 return NT_STATUS_UNSUCCESSFUL;
2301 result = get_sid_from_name(cli, mem_ctx,
2302 member, &member_sid, &member_type);
2304 if (!NT_STATUS_IS_OK(result)) {
2305 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2310 /* Get sam policy handle */
2311 status = dcerpc_samr_Connect2(b, mem_ctx,
2313 MAXIMUM_ALLOWED_ACCESS,
2316 if (!NT_STATUS_IS_OK(status)) {
2319 if (!NT_STATUS_IS_OK(result)) {
2324 /* Get domain policy handle */
2325 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2327 MAXIMUM_ALLOWED_ACCESS,
2331 if (!NT_STATUS_IS_OK(status)) {
2334 if (!NT_STATUS_IS_OK(result)) {
2339 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2341 MAXIMUM_ALLOWED_ACCESS,
2345 if (!NT_STATUS_IS_OK(status)) {
2348 if (!NT_STATUS_IS_OK(result)) {
2352 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2356 if (!NT_STATUS_IS_OK(status)) {
2363 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2367 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2368 const struct dom_sid *domain_sid,
2369 const char *domain_name,
2370 struct cli_state *cli,
2371 struct rpc_pipe_client *pipe_hnd,
2372 TALLOC_CTX *mem_ctx,
2376 struct dom_sid group_sid;
2377 enum lsa_SidType group_type;
2379 if (argc != 2 || c->display_usage) {
2382 _("net rpc group addmem <group> <member>\n"
2383 " Add a member to a group\n"
2384 " group\tGroup to add member to\n"
2385 " member\tMember to add to group\n"));
2386 return NT_STATUS_UNSUCCESSFUL;
2389 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2390 &group_sid, &group_type))) {
2391 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2393 return NT_STATUS_UNSUCCESSFUL;
2396 if (group_type == SID_NAME_DOM_GRP) {
2397 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2398 &group_sid, argv[1]);
2400 if (!NT_STATUS_IS_OK(result)) {
2401 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2402 argv[1], argv[0], nt_errstr(result));
2407 if (group_type == SID_NAME_ALIAS) {
2408 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, cli, mem_ctx,
2409 &group_sid, argv[1]);
2411 if (!NT_STATUS_IS_OK(result)) {
2412 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2413 argv[1], argv[0], nt_errstr(result));
2418 d_fprintf(stderr, _("Can only add members to global or local groups "
2419 "which %s is not\n"), argv[0]);
2421 return NT_STATUS_UNSUCCESSFUL;
2424 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2426 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2427 rpc_group_addmem_internals,
2431 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2432 struct rpc_pipe_client *pipe_hnd,
2433 TALLOC_CTX *mem_ctx,
2434 const struct dom_sid *group_sid,
2437 struct policy_handle connect_pol, domain_pol;
2438 NTSTATUS status, result;
2440 struct policy_handle group_pol;
2441 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2443 struct samr_Ids rids, rid_types;
2444 struct lsa_String lsa_acct_name;
2448 sid_copy(&sid, group_sid);
2450 if (!sid_split_rid(&sid, &group_rid))
2451 return NT_STATUS_UNSUCCESSFUL;
2453 /* Get sam policy handle */
2454 status = dcerpc_samr_Connect2(b, mem_ctx,
2456 MAXIMUM_ALLOWED_ACCESS,
2459 if (!NT_STATUS_IS_OK(status)) {
2462 if (!NT_STATUS_IS_OK(result)) {
2467 /* Get domain policy handle */
2468 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2470 MAXIMUM_ALLOWED_ACCESS,
2474 if (!NT_STATUS_IS_OK(status)) {
2477 if (!NT_STATUS_IS_OK(result)) {
2481 init_lsa_String(&lsa_acct_name, member);
2483 status = dcerpc_samr_LookupNames(b, mem_ctx,
2490 if (!NT_STATUS_IS_OK(status)) {
2491 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2496 if (!NT_STATUS_IS_OK(result)) {
2498 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2503 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2505 MAXIMUM_ALLOWED_ACCESS,
2509 if (!NT_STATUS_IS_OK(status)) {
2512 if (!NT_STATUS_IS_OK(result)) {
2517 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2521 if (!NT_STATUS_IS_OK(status)) {
2527 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2531 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2532 struct cli_state *cli,
2533 TALLOC_CTX *mem_ctx,
2534 const struct dom_sid *alias_sid,
2537 struct policy_handle connect_pol, domain_pol;
2538 NTSTATUS status, result;
2540 struct policy_handle alias_pol;
2541 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2543 struct dom_sid member_sid;
2544 enum lsa_SidType member_type;
2548 sid_copy(&sid, alias_sid);
2550 if (!sid_split_rid(&sid, &alias_rid))
2551 return NT_STATUS_UNSUCCESSFUL;
2553 result = get_sid_from_name(cli, mem_ctx,
2554 member, &member_sid, &member_type);
2556 if (!NT_STATUS_IS_OK(result)) {
2557 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2562 /* Get sam policy handle */
2563 status = dcerpc_samr_Connect2(b, mem_ctx,
2565 MAXIMUM_ALLOWED_ACCESS,
2568 if (!NT_STATUS_IS_OK(status)) {
2571 if (!NT_STATUS_IS_OK(result)) {
2576 /* Get domain policy handle */
2577 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2579 MAXIMUM_ALLOWED_ACCESS,
2583 if (!NT_STATUS_IS_OK(status)) {
2586 if (!NT_STATUS_IS_OK(result)) {
2591 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2593 MAXIMUM_ALLOWED_ACCESS,
2597 if (!NT_STATUS_IS_OK(status)) {
2601 if (!NT_STATUS_IS_OK(result)) {
2605 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2610 if (!NT_STATUS_IS_OK(status)) {
2617 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2621 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2622 const struct dom_sid *domain_sid,
2623 const char *domain_name,
2624 struct cli_state *cli,
2625 struct rpc_pipe_client *pipe_hnd,
2626 TALLOC_CTX *mem_ctx,
2630 struct dom_sid group_sid;
2631 enum lsa_SidType group_type;
2633 if (argc != 2 || c->display_usage) {
2636 _("net rpc group delmem <group> <member>\n"
2637 " Delete a member from a group\n"
2638 " group\tGroup to delete member from\n"
2639 " member\tMember to delete from group\n"));
2640 return NT_STATUS_UNSUCCESSFUL;
2643 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2644 &group_sid, &group_type))) {
2645 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2647 return NT_STATUS_UNSUCCESSFUL;
2650 if (group_type == SID_NAME_DOM_GRP) {
2651 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2652 &group_sid, argv[1]);
2654 if (!NT_STATUS_IS_OK(result)) {
2655 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2656 argv[1], argv[0], nt_errstr(result));
2661 if (group_type == SID_NAME_ALIAS) {
2662 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, cli, mem_ctx,
2663 &group_sid, argv[1]);
2665 if (!NT_STATUS_IS_OK(result)) {
2666 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2667 argv[1], argv[0], nt_errstr(result));
2672 d_fprintf(stderr, _("Can only delete members from global or local "
2673 "groups which %s is not\n"), argv[0]);
2675 return NT_STATUS_UNSUCCESSFUL;
2678 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2680 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2681 rpc_group_delmem_internals,
2686 * List groups on a remote RPC server.
2688 * All parameters are provided by the run_rpc_command function, except for
2689 * argc, argv which are passes through.
2691 * @param domain_sid The domain sid acquired from the remote server.
2692 * @param cli A cli_state connected to the server.
2693 * @param mem_ctx Talloc context, destroyed on completion of the function.
2694 * @param argc Standard main() style argc.
2695 * @param argv Standard main() style argv. Initial components are already
2698 * @return Normal NTSTATUS return.
2701 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2702 const struct dom_sid *domain_sid,
2703 const char *domain_name,
2704 struct cli_state *cli,
2705 struct rpc_pipe_client *pipe_hnd,
2706 TALLOC_CTX *mem_ctx,
2710 struct policy_handle connect_pol, domain_pol;
2711 NTSTATUS status, result;
2712 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2713 struct samr_SamArray *groups = NULL;
2714 bool global = false;
2716 bool builtin = false;
2717 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2719 if (c->display_usage) {
2722 _("net rpc group list [global] [local] [builtin]\n"
2723 " List groups on RPC server\n"
2724 " global\tList global groups\n"
2725 " local\tList local groups\n"
2726 " builtin\tList builtin groups\n"
2727 " If none of global, local or builtin is "
2728 "specified, all three options are considered "
2730 return NT_STATUS_OK;
2739 for (i=0; i<argc; i++) {
2740 if (strequal(argv[i], "global"))
2743 if (strequal(argv[i], "local"))
2746 if (strequal(argv[i], "builtin"))
2750 /* Get sam policy handle */
2752 status = dcerpc_samr_Connect2(b, mem_ctx,
2754 MAXIMUM_ALLOWED_ACCESS,
2757 if (!NT_STATUS_IS_OK(status)) {
2760 if (!NT_STATUS_IS_OK(result)) {
2765 /* Get domain policy handle */
2767 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2769 MAXIMUM_ALLOWED_ACCESS,
2770 discard_const_p(struct dom_sid2, domain_sid),
2773 if (!NT_STATUS_IS_OK(status)) {
2776 if (!NT_STATUS_IS_OK(result)) {
2781 /* Query domain groups */
2782 if (c->opt_long_list_entries)
2783 d_printf(_("\nGroup name Comment"
2784 "\n-----------------------------\n"));
2786 uint32_t max_size, total_size, returned_size;
2787 union samr_DispInfo info;
2791 dcerpc_get_query_dispinfo_params(
2792 loop_count, &max_entries, &max_size);
2794 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2804 if (!NT_STATUS_IS_OK(status)) {
2807 num_entries = info.info3.count;
2808 start_idx += info.info3.count;
2810 if (!NT_STATUS_IS_OK(result) &&
2811 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2814 for (i = 0; i < num_entries; i++) {
2816 const char *group = NULL;
2817 const char *desc = NULL;
2819 group = info.info3.entries[i].account_name.string;
2820 desc = info.info3.entries[i].description.string;
2822 if (c->opt_long_list_entries)
2823 printf("%-21.21s %-50.50s\n",
2826 printf("%s\n", group);
2828 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2829 /* query domain aliases */
2834 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2841 if (!NT_STATUS_IS_OK(status)) {
2844 if (!NT_STATUS_IS_OK(result) &&
2845 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2848 for (i = 0; i < num_entries; i++) {
2850 const char *description = NULL;
2852 if (c->opt_long_list_entries) {
2854 struct policy_handle alias_pol;
2855 union samr_AliasInfo *info = NULL;
2858 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2861 groups->entries[i].idx,
2864 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2865 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2870 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2871 status = dcerpc_samr_Close(b, mem_ctx,
2874 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2875 description = info->description.string;
2881 if (description != NULL) {
2882 printf("%-21.21s %-50.50s\n",
2883 groups->entries[i].name.string,
2886 printf("%s\n", groups->entries[i].name.string);
2889 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2890 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2891 /* Get builtin policy handle */
2893 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2895 MAXIMUM_ALLOWED_ACCESS,
2896 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2899 if (!NT_STATUS_IS_OK(status)) {
2902 if (!NT_STATUS_IS_OK(result)) {
2907 /* query builtin aliases */
2910 if (!builtin) break;
2912 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2919 if (!NT_STATUS_IS_OK(status)) {
2922 if (!NT_STATUS_IS_OK(result) &&
2923 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2928 for (i = 0; i < num_entries; i++) {
2930 const char *description = NULL;
2932 if (c->opt_long_list_entries) {
2934 struct policy_handle alias_pol;
2935 union samr_AliasInfo *info = NULL;
2938 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2941 groups->entries[i].idx,
2944 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2945 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2950 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2951 status = dcerpc_samr_Close(b, mem_ctx,
2954 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2955 description = info->description.string;
2961 if (description != NULL) {
2962 printf("%-21.21s %-50.50s\n",
2963 groups->entries[i].name.string,
2966 printf("%s\n", groups->entries[i].name.string);
2969 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2977 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2979 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2980 rpc_group_list_internals,
2984 static NTSTATUS rpc_list_group_members(struct net_context *c,
2985 struct rpc_pipe_client *pipe_hnd,
2986 TALLOC_CTX *mem_ctx,
2987 const char *domain_name,
2988 const struct dom_sid *domain_sid,
2989 struct policy_handle *domain_pol,
2992 NTSTATUS result, status;
2993 struct policy_handle group_pol;
2994 uint32 num_members, *group_rids;
2996 struct samr_RidAttrArray *rids = NULL;
2997 struct lsa_Strings names;
2998 struct samr_Ids types;
2999 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3002 sid_to_fstring(sid_str, domain_sid);
3004 status = dcerpc_samr_OpenGroup(b, mem_ctx,
3006 MAXIMUM_ALLOWED_ACCESS,
3010 if (!NT_STATUS_IS_OK(status)) {
3013 if (!NT_STATUS_IS_OK(result)) {
3017 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
3021 if (!NT_STATUS_IS_OK(status)) {
3024 if (!NT_STATUS_IS_OK(result)) {
3028 num_members = rids->count;
3029 group_rids = rids->rids;
3031 while (num_members > 0) {
3032 int this_time = 512;
3034 if (num_members < this_time)
3035 this_time = num_members;
3037 status = dcerpc_samr_LookupRids(b, mem_ctx,
3044 if (!NT_STATUS_IS_OK(status)) {
3047 if (!NT_STATUS_IS_OK(result)) {
3051 /* We only have users as members, but make the output
3052 the same as the output of alias members */
3054 for (i = 0; i < this_time; i++) {
3056 if (c->opt_long_list_entries) {
3057 printf("%s-%d %s\\%s %d\n", sid_str,
3058 group_rids[i], domain_name,
3059 names.names[i].string,
3062 printf("%s\\%s\n", domain_name,
3063 names.names[i].string);
3067 num_members -= this_time;
3071 return NT_STATUS_OK;
3074 static NTSTATUS rpc_list_alias_members(struct net_context *c,
3075 struct rpc_pipe_client *pipe_hnd,
3076 struct cli_state *cli,
3077 TALLOC_CTX *mem_ctx,
3078 struct policy_handle *domain_pol,
3081 NTSTATUS result, status;
3082 struct rpc_pipe_client *lsa_pipe;
3083 struct policy_handle alias_pol, lsa_pol;
3085 struct dom_sid *alias_sids;
3088 enum lsa_SidType *types;
3090 struct lsa_SidArray sid_array;
3091 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3093 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3095 MAXIMUM_ALLOWED_ACCESS,
3099 if (!NT_STATUS_IS_OK(status)) {
3102 if (!NT_STATUS_IS_OK(result)) {
3106 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
3110 if (!NT_STATUS_IS_OK(status)) {
3111 d_fprintf(stderr, _("Couldn't list alias members\n"));
3114 if (!NT_STATUS_IS_OK(result)) {
3115 d_fprintf(stderr, _("Couldn't list alias members\n"));
3119 num_members = sid_array.num_sids;
3121 if (num_members == 0) {
3122 return NT_STATUS_OK;
3125 result = cli_rpc_pipe_open_noauth(cli,
3128 if (!NT_STATUS_IS_OK(result)) {
3129 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
3130 nt_errstr(result) );
3134 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
3135 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
3137 if (!NT_STATUS_IS_OK(result)) {
3138 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
3139 TALLOC_FREE(lsa_pipe);
3143 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
3145 d_fprintf(stderr, _("Out of memory\n"));
3146 TALLOC_FREE(lsa_pipe);
3147 return NT_STATUS_NO_MEMORY;
3150 for (i=0; i<num_members; i++) {
3151 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
3154 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
3155 num_members, alias_sids,
3156 &domains, &names, &types);
3158 if (!NT_STATUS_IS_OK(result) &&
3159 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3160 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3161 TALLOC_FREE(lsa_pipe);
3165 for (i = 0; i < num_members; i++) {
3167 sid_to_fstring(sid_str, &alias_sids[i]);
3169 if (c->opt_long_list_entries) {
3170 printf("%s %s\\%s %d\n", sid_str,
3171 domains[i] ? domains[i] : _("*unknown*"),
3172 names[i] ? names[i] : _("*unknown*"), types[i]);
3175 printf("%s\\%s\n", domains[i], names[i]);
3177 printf("%s\n", sid_str);
3181 TALLOC_FREE(lsa_pipe);
3182 return NT_STATUS_OK;
3185 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3186 const struct dom_sid *domain_sid,
3187 const char *domain_name,
3188 struct cli_state *cli,
3189 struct rpc_pipe_client *pipe_hnd,
3190 TALLOC_CTX *mem_ctx,
3194 NTSTATUS result, status;
3195 struct policy_handle connect_pol, domain_pol;
3196 struct samr_Ids rids, rid_types;
3197 struct lsa_String lsa_acct_name;
3198 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3200 /* Get sam policy handle */
3202 status = dcerpc_samr_Connect2(b, mem_ctx,
3204 MAXIMUM_ALLOWED_ACCESS,
3207 if (!NT_STATUS_IS_OK(status)) {
3210 if (!NT_STATUS_IS_OK(result)) {
3214 /* Get domain policy handle */
3216 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3218 MAXIMUM_ALLOWED_ACCESS,
3219 discard_const_p(struct dom_sid2, domain_sid),
3222 if (!NT_STATUS_IS_OK(status)) {
3225 if (!NT_STATUS_IS_OK(result)) {
3229 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3231 status = dcerpc_samr_LookupNames(b, mem_ctx,
3238 if (!NT_STATUS_IS_OK(status)) {
3242 if (!NT_STATUS_IS_OK(result)) {
3244 /* Ok, did not find it in the global sam, try with builtin */
3246 struct dom_sid sid_Builtin;
3248 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3250 sid_copy(&sid_Builtin, &global_sid_Builtin);
3252 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3254 MAXIMUM_ALLOWED_ACCESS,
3258 if (!NT_STATUS_IS_OK(status)) {
3261 if (!NT_STATUS_IS_OK(result)) {
3262 d_fprintf(stderr, _("Couldn't find group %s\n"),
3267 status = dcerpc_samr_LookupNames(b, mem_ctx,
3274 if (!NT_STATUS_IS_OK(status)) {
3277 if (!NT_STATUS_IS_OK(result)) {
3278 d_fprintf(stderr, _("Couldn't find group %s\n"),
3284 if (rids.count != 1) {
3285 d_fprintf(stderr, _("Couldn't find group %s\n"),
3290 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3291 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3292 domain_sid, &domain_pol,
3296 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3297 return rpc_list_alias_members(c, pipe_hnd, cli, mem_ctx, &domain_pol,
3301 return NT_STATUS_NO_SUCH_GROUP;
3304 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3306 if (argc != 1 || c->display_usage) {
3307 return rpc_group_usage(c, argc, argv);
3310 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3311 rpc_group_members_internals,
3315 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3317 NET_API_STATUS status;
3318 struct GROUP_INFO_0 g0;
3322 d_printf(_("Usage:\n"));
3323 d_printf("net rpc group rename group newname\n");
3327 g0.grpi0_name = argv[1];
3329 status = NetGroupSetInfo(c->opt_host,
3336 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3337 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3345 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3347 if (argc != 2 || c->display_usage) {
3348 return rpc_group_usage(c, argc, argv);
3351 return rpc_group_rename_internals(c, argc, argv);
3355 * 'net rpc group' entrypoint.
3356 * @param argc Standard main() style argc.
3357 * @param argv Standard main() style argv. Initial components are already
3361 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3363 NET_API_STATUS status;
3365 struct functable func[] = {
3370 N_("Create specified group"),
3371 N_("net rpc group add\n"
3372 " Create specified group")
3378 N_("Delete specified group"),
3379 N_("net rpc group delete\n"
3380 " Delete specified group")
3386 N_("Add member to group"),
3387 N_("net rpc group addmem\n"
3388 " Add member to group")
3394 N_("Remove member from group"),
3395 N_("net rpc group delmem\n"
3396 " Remove member from group")
3403 N_("net rpc group list\n"
3410 N_("List group members"),
3411 N_("net rpc group members\n"
3412 " List group members")
3419 N_("net rpc group rename\n"
3422 {NULL, NULL, 0, NULL, NULL}
3425 status = libnetapi_net_init(&c->netapi_ctx);
3429 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3430 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3431 if (c->opt_kerberos) {
3432 libnetapi_set_use_kerberos(c->netapi_ctx);
3436 if (c->display_usage) {
3437 d_printf(_("Usage:\n"));
3438 d_printf(_("net rpc group\n"
3439 " Alias for net rpc group list global "
3440 "local builtin\n"));
3441 net_display_usage_from_functable(func);
3445 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3446 rpc_group_list_internals,
3450 return net_run_function(c, argc, argv, "net rpc group", func);
3453 /****************************************************************************/
3455 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3457 return net_share_usage(c, argc, argv);
3461 * Add a share on a remote RPC server.
3463 * @param argc Standard main() style argc.
3464 * @param argv Standard main() style argv. Initial components are already
3467 * @return A shell status integer (0 for success).
3470 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3472 NET_API_STATUS status;
3475 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3476 uint32 num_users=0, perms=0;
3477 char *password=NULL; /* don't allow a share password */
3478 struct SHARE_INFO_2 i2;
3479 uint32_t parm_error = 0;
3481 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3482 return rpc_share_usage(c, argc, argv);
3485 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3489 path = strchr(sharename, '=');
3496 i2.shi2_netname = sharename;
3497 i2.shi2_type = type;
3498 i2.shi2_remark = c->opt_comment;
3499 i2.shi2_permissions = perms;
3500 i2.shi2_max_uses = c->opt_maxusers;
3501 i2.shi2_current_uses = num_users;
3502 i2.shi2_path = path;
3503 i2.shi2_passwd = password;
3505 status = NetShareAdd(c->opt_host,
3510 printf(_("NetShareAdd failed with: %s\n"),
3511 libnetapi_get_error_string(c->netapi_ctx, status));
3518 * Delete a share on a remote RPC server.
3520 * @param domain_sid The domain sid acquired from the remote server.
3521 * @param argc Standard main() style argc.
3522 * @param argv Standard main() style argv. Initial components are already
3525 * @return A shell status integer (0 for success).
3527 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3529 if (argc < 1 || c->display_usage) {
3530 return rpc_share_usage(c, argc, argv);
3533 return NetShareDel(c->opt_host, argv[0], 0);
3537 * Formatted print of share info
3539 * @param r pointer to SHARE_INFO_1 to format
3542 static void display_share_info_1(struct net_context *c,
3543 struct SHARE_INFO_1 *r)
3545 if (c->opt_long_list_entries) {
3546 d_printf("%-12s %-8.8s %-50s\n",
3548 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3551 d_printf("%s\n", r->shi1_netname);
3555 static WERROR get_share_info(struct net_context *c,
3556 struct rpc_pipe_client *pipe_hnd,
3557 TALLOC_CTX *mem_ctx,
3561 struct srvsvc_NetShareInfoCtr *info_ctr)
3565 union srvsvc_NetShareInfo info;
3566 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3568 /* no specific share requested, enumerate all */
3571 uint32_t preferred_len = 0xffffffff;
3572 uint32_t total_entries = 0;
3573 uint32_t resume_handle = 0;
3575 info_ctr->level = level;
3577 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3584 if (!NT_STATUS_IS_OK(status)) {
3585 return ntstatus_to_werror(status);
3590 /* request just one share */
3591 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3598 if (!NT_STATUS_IS_OK(status)) {
3599 result = ntstatus_to_werror(status);
3603 if (!W_ERROR_IS_OK(result)) {
3608 ZERO_STRUCTP(info_ctr);
3610 info_ctr->level = level;
3615 struct srvsvc_NetShareCtr1 *ctr1;
3617 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3618 W_ERROR_HAVE_NO_MEMORY(ctr1);
3621 ctr1->array = info.info1;
3623 info_ctr->ctr.ctr1 = ctr1;
3629 struct srvsvc_NetShareCtr2 *ctr2;
3631 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3632 W_ERROR_HAVE_NO_MEMORY(ctr2);
3635 ctr2->array = info.info2;
3637 info_ctr->ctr.ctr2 = ctr2;
3643 struct srvsvc_NetShareCtr502 *ctr502;
3645 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3646 W_ERROR_HAVE_NO_MEMORY(ctr502);
3649 ctr502->array = info.info502;
3651 info_ctr->ctr.ctr502 = ctr502;
3661 * 'net rpc share list' entrypoint.
3662 * @param argc Standard main() style argc.
3663 * @param argv Standard main() style argv. Initial components are already
3666 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3668 NET_API_STATUS status;
3669 struct SHARE_INFO_1 *i1 = NULL;
3670 uint32_t entries_read = 0;
3671 uint32_t total_entries = 0;
3672 uint32_t resume_handle = 0;
3673 uint32_t i, level = 1;
3675 if (c->display_usage) {
3677 "net rpc share list\n"
3680 _("List shares on remote server"));
3684 status = NetShareEnum(c->opt_host,
3686 (uint8_t **)(void *)&i1,
3695 /* Display results */
3697 if (c->opt_long_list_entries) {
3699 "\nEnumerating shared resources (exports) on remote server:\n\n"
3700 "\nShare name Type Description\n"
3701 "---------- ---- -----------\n"));
3703 for (i = 0; i < entries_read; i++)
3704 display_share_info_1(c, &i1[i]);
3709 static bool check_share_availability(struct cli_state *cli, const char *netname)
3713 status = cli_tree_connect(cli, netname, "A:", "", 0);
3714 if (!NT_STATUS_IS_OK(status)) {
3715 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3719 status = cli_tdis(cli);
3720 if (!NT_STATUS_IS_OK(status)) {
3721 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3728 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3729 const char *netname, uint32 type)
3731 /* only support disk shares */
3732 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3733 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3738 /* skip builtin shares */
3739 /* FIXME: should print$ be added too ? */
3740 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3741 strequal(netname,"global"))
3744 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3745 printf(_("excluding [%s]\n"), netname);
3749 return check_share_availability(cli, netname);
3753 * Migrate shares from a remote RPC server to the local RPC server.
3755 * All parameters are provided by the run_rpc_command function, except for
3756 * argc, argv which are passed through.
3758 * @param domain_sid The domain sid acquired from the remote server.
3759 * @param cli A cli_state connected to the server.
3760 * @param mem_ctx Talloc context, destroyed on completion of the function.
3761 * @param argc Standard main() style argc.
3762 * @param argv Standard main() style argv. Initial components are already
3765 * @return Normal NTSTATUS return.
3768 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3769 const struct dom_sid *domain_sid,
3770 const char *domain_name,
3771 struct cli_state *cli,
3772 struct rpc_pipe_client *pipe_hnd,
3773 TALLOC_CTX *mem_ctx,
3778 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3779 struct srvsvc_NetShareInfoCtr ctr_src;
3781 struct rpc_pipe_client *srvsvc_pipe = NULL;
3782 struct cli_state *cli_dst = NULL;
3783 uint32 level = 502; /* includes secdesc */
3784 uint32_t parm_error = 0;
3785 struct dcerpc_binding_handle *b;
3787 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3789 if (!W_ERROR_IS_OK(result))
3792 /* connect destination PI_SRVSVC */
3793 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3795 if (!NT_STATUS_IS_OK(nt_status))
3798 b = srvsvc_pipe->binding_handle;
3800 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3802 union srvsvc_NetShareInfo info;
3803 struct srvsvc_NetShareInfo502 info502 =
3804 ctr_src.ctr.ctr502->array[i];
3806 /* reset error-code */
3807 nt_status = NT_STATUS_UNSUCCESSFUL;
3809 if (!check_share_sanity(c, cli, info502.name, info502.type))
3812 /* finally add the share on the dst server */
3814 printf(_("migrating: [%s], path: %s, comment: %s, without "
3816 info502.name, info502.path, info502.comment);
3818 info.info502 = &info502;
3820 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3821 srvsvc_pipe->desthost,
3826 if (!NT_STATUS_IS_OK(nt_status)) {
3827 printf(_("cannot add share: %s\n"),
3828 nt_errstr(nt_status));
3831 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3832 printf(_(" [%s] does already exist\n"),
3837 if (!W_ERROR_IS_OK(result)) {
3838 nt_status = werror_to_ntstatus(result);
3839 printf(_("cannot add share: %s\n"),
3840 win_errstr(result));
3846 nt_status = NT_STATUS_OK;
3850 cli_shutdown(cli_dst);
3858 * Migrate shares from a RPC server to another.
3860 * @param argc Standard main() style argc.
3861 * @param argv Standard main() style argv. Initial components are already
3864 * @return A shell status integer (0 for success).
3866 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3869 if (c->display_usage) {
3871 "net rpc share migrate shares\n"
3874 _("Migrate shares to local server"));
3879 printf(_("no server to migrate\n"));
3883 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3884 rpc_share_migrate_shares_internals,
3891 * @param f file_info
3892 * @param mask current search mask
3893 * @param state arg-pointer
3896 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3897 const char *mask, void *state)
3899 static NTSTATUS nt_status;
3900 static struct copy_clistate *local_state;
3901 static fstring filename, new_mask;
3904 struct net_context *c;
3906 local_state = (struct copy_clistate *)state;
3907 nt_status = NT_STATUS_UNSUCCESSFUL;
3911 if (strequal(f->name, ".") || strequal(f->name, ".."))
3912 return NT_STATUS_OK;
3914 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3917 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3919 DEBUG(3,("got dir: %s\n", f->name));
3921 fstrcpy(dir, local_state->cwd);
3923 fstrcat(dir, f->name);
3925 switch (net_mode_share)
3927 case NET_MODE_SHARE_MIGRATE:
3928 /* create that directory */
3929 nt_status = net_copy_file(c, local_state->mem_ctx,
3930 local_state->cli_share_src,
3931 local_state->cli_share_dst,
3933 c->opt_acls? true : false,
3934 c->opt_attrs? true : false,
3935 c->opt_timestamps? true:false,
3939 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3940 return NT_STATUS_INTERNAL_ERROR;
3943 if (!NT_STATUS_IS_OK(nt_status)) {
3944 printf(_("could not handle dir %s: %s\n"),
3945 dir, nt_errstr(nt_status));
3949 /* search below that directory */
3950 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
3951 return NT_STATUS_NO_MEMORY;
3953 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
3954 return NT_STATUS_NO_MEMORY;
3957 old_dir = local_state->cwd;
3958 local_state->cwd = dir;
3959 nt_status = sync_files(local_state, new_mask);
3960 if (!NT_STATUS_IS_OK(nt_status)) {
3961 printf(_("could not handle files\n"));
3963 local_state->cwd = old_dir;
3970 fstrcpy(filename, local_state->cwd);
3971 fstrcat(filename, "\\");
3972 fstrcat(filename, f->name);
3974 DEBUG(3,("got file: %s\n", filename));
3976 switch (net_mode_share)
3978 case NET_MODE_SHARE_MIGRATE:
3979 nt_status = net_copy_file(c, local_state->mem_ctx,
3980 local_state->cli_share_src,
3981 local_state->cli_share_dst,
3983 c->opt_acls? true : false,
3984 c->opt_attrs? true : false,
3985 c->opt_timestamps? true: false,
3989 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3991 return NT_STATUS_INTERNAL_ERROR;
3994 if (!NT_STATUS_IS_OK(nt_status))
3995 printf(_("could not handle file %s: %s\n"),
3996 filename, nt_errstr(nt_status));
4001 * sync files, can be called recursivly to list files
4002 * and then call copy_fn for each file
4004 * @param cp_clistate pointer to the copy_clistate we work with
4005 * @param mask the current search mask
4007 * @return Boolean result
4009 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
4011 struct cli_state *targetcli;
4012 char *targetpath = NULL;
4015 DEBUG(3,("calling cli_list with mask: %s\n", mask));
4017 status = cli_resolve_path(talloc_tos(), "", NULL,
4018 cp_clistate->cli_share_src,
4019 mask, &targetcli, &targetpath);
4020 if (!NT_STATUS_IS_OK(status)) {
4021 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
4023 mask, nt_errstr(status));
4027 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
4028 copy_fn, cp_clistate);
4029 if (!NT_STATUS_IS_OK(status)) {
4030 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
4031 mask, nt_errstr(status));
4039 * Set the top level directory permissions before we do any further copies.
4040 * Should set up ACL inheritance.
4043 bool copy_top_level_perms(struct net_context *c,
4044 struct copy_clistate *cp_clistate,
4045 const char *sharename)
4047 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4049 switch (net_mode_share) {
4050 case NET_MODE_SHARE_MIGRATE:
4051 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
4052 nt_status = net_copy_fileattr(c,
4053 cp_clistate->mem_ctx,
4054 cp_clistate->cli_share_src,
4055 cp_clistate->cli_share_dst,
4057 c->opt_acls? true : false,
4058 c->opt_attrs? true : false,
4059 c->opt_timestamps? true: false,
4063 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4067 if (!NT_STATUS_IS_OK(nt_status)) {
4068 printf(_("Could handle directory attributes for top level "
4069 "directory of share %s. Error %s\n"),
4070 sharename, nt_errstr(nt_status));
4078 * Sync all files inside a remote share to another share (over smb).
4080 * All parameters are provided by the run_rpc_command function, except for
4081 * argc, argv which are passed through.
4083 * @param domain_sid The domain sid acquired from the remote server.
4084 * @param cli A cli_state connected to the server.
4085 * @param mem_ctx Talloc context, destroyed on completion of the function.
4086 * @param argc Standard main() style argc.
4087 * @param argv Standard main() style argv. Initial components are already
4090 * @return Normal NTSTATUS return.
4093 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
4094 const struct dom_sid *domain_sid,
4095 const char *domain_name,
4096 struct cli_state *cli,
4097 struct rpc_pipe_client *pipe_hnd,
4098 TALLOC_CTX *mem_ctx,
4103 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4104 struct srvsvc_NetShareInfoCtr ctr_src;
4107 struct copy_clistate cp_clistate;
4108 bool got_src_share = false;
4109 bool got_dst_share = false;
4110 const char *mask = "\\*";
4113 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
4115 nt_status = NT_STATUS_NO_MEMORY;
4119 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4122 if (!W_ERROR_IS_OK(result))
4125 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4127 struct srvsvc_NetShareInfo502 info502 =
4128 ctr_src.ctr.ctr502->array[i];
4130 if (!check_share_sanity(c, cli, info502.name, info502.type))
4133 /* one might not want to mirror whole discs :) */
4134 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
4135 d_printf(_("skipping [%s]: builtin/hidden share\n"),
4140 switch (net_mode_share)
4142 case NET_MODE_SHARE_MIGRATE:
4146 d_fprintf(stderr, _("Unsupported mode %d\n"),
4150 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4153 c->opt_acls ? _("including") : _("without"),
4154 c->opt_attrs ? _("including") : _("without"),
4155 c->opt_timestamps ? _("(preserving timestamps)") : "");
4157 cp_clistate.mem_ctx = mem_ctx;
4158 cp_clistate.cli_share_src = NULL;
4159 cp_clistate.cli_share_dst = NULL;
4160 cp_clistate.cwd = NULL;
4161 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4164 /* open share source */
4165 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4166 smbXcli_conn_remote_sockaddr(cli->conn),
4167 smbXcli_conn_remote_name(cli->conn),
4168 info502.name, "A:");
4169 if (!NT_STATUS_IS_OK(nt_status))
4172 got_src_share = true;
4174 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4175 /* open share destination */
4176 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4177 NULL, dst, info502.name, "A:");
4178 if (!NT_STATUS_IS_OK(nt_status))
4181 got_dst_share = true;
4184 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4185 d_fprintf(stderr, _("Could not handle the top level "
4186 "directory permissions for the "
4187 "share: %s\n"), info502.name);
4188 nt_status = NT_STATUS_UNSUCCESSFUL;
4192 nt_status = sync_files(&cp_clistate, mask);
4193 if (!NT_STATUS_IS_OK(nt_status)) {
4194 d_fprintf(stderr, _("could not handle files for share: "
4195 "%s\n"), info502.name);
4200 nt_status = NT_STATUS_OK;
4205 cli_shutdown(cp_clistate.cli_share_src);
4208 cli_shutdown(cp_clistate.cli_share_dst);
4215 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4217 if (c->display_usage) {
4219 "net share migrate files\n"
4222 _("Migrate files to local server"));
4227 d_printf(_("no server to migrate\n"));
4231 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4232 rpc_share_migrate_files_internals,
4237 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4239 * All parameters are provided by the run_rpc_command function, except for
4240 * argc, argv which are passed through.
4242 * @param domain_sid The domain sid acquired from the remote server.
4243 * @param cli A cli_state connected to the server.
4244 * @param mem_ctx Talloc context, destroyed on completion of the function.
4245 * @param argc Standard main() style argc.
4246 * @param argv Standard main() style argv. Initial components are already
4249 * @return Normal NTSTATUS return.
4252 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4253 const struct dom_sid *domain_sid,
4254 const char *domain_name,
4255 struct cli_state *cli,
4256 struct rpc_pipe_client *pipe_hnd,
4257 TALLOC_CTX *mem_ctx,
4262 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4263 struct srvsvc_NetShareInfoCtr ctr_src;
4264 union srvsvc_NetShareInfo info;
4266 struct rpc_pipe_client *srvsvc_pipe = NULL;
4267 struct cli_state *cli_dst = NULL;
4268 uint32 level = 502; /* includes secdesc */
4269 uint32_t parm_error = 0;
4270 struct dcerpc_binding_handle *b;
4272 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4275 if (!W_ERROR_IS_OK(result))
4278 /* connect destination PI_SRVSVC */
4279 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4281 if (!NT_STATUS_IS_OK(nt_status))
4284 b = srvsvc_pipe->binding_handle;
4286 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4288 struct srvsvc_NetShareInfo502 info502 =
4289 ctr_src.ctr.ctr502->array[i];
4291 /* reset error-code */
4292 nt_status = NT_STATUS_UNSUCCESSFUL;
4294 if (!check_share_sanity(c, cli, info502.name, info502.type))
4297 printf(_("migrating: [%s], path: %s, comment: %s, including "
4299 info502.name, info502.path, info502.comment);
4302 display_sec_desc(info502.sd_buf.sd);
4304 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4305 info.info502 = &info502;
4307 /* finally modify the share on the dst server */
4308 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4309 srvsvc_pipe->desthost,
4315 if (!NT_STATUS_IS_OK(nt_status)) {
4316 printf(_("cannot set share-acl: %s\n"),
4317 nt_errstr(nt_status));
4320 if (!W_ERROR_IS_OK(result)) {
4321 nt_status = werror_to_ntstatus(result);
4322 printf(_("cannot set share-acl: %s\n"),
4323 win_errstr(result));
4329 nt_status = NT_STATUS_OK;
4333 cli_shutdown(cli_dst);
4341 * Migrate share-acls from a RPC server to another.
4343 * @param argc Standard main() style argc.
4344 * @param argv Standard main() style argv. Initial components are already
4347 * @return A shell status integer (0 for success).
4349 static int rpc_share_migrate_security(struct net_context *c, int argc,
4352 if (c->display_usage) {
4354 "net rpc share migrate security\n"
4357 _("Migrate share-acls to local server"));
4362 d_printf(_("no server to migrate\n"));
4366 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4367 rpc_share_migrate_security_internals,
4372 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4373 * from one server to another.
4375 * @param argc Standard main() style argc.
4376 * @param argv Standard main() style argv. Initial components are already
4379 * @return A shell status integer (0 for success).
4382 static int rpc_share_migrate_all(struct net_context *c, int argc,
4387 if (c->display_usage) {
4389 "net rpc share migrate all\n"
4392 _("Migrates shares including all share settings"));
4397 d_printf(_("no server to migrate\n"));
4401 /* order is important. we don't want to be locked out by the share-acl
4402 * before copying files - gd */
4404 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4405 rpc_share_migrate_shares_internals, argc, argv);
4409 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4410 rpc_share_migrate_files_internals, argc, argv);
4414 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4415 rpc_share_migrate_security_internals, argc,
4421 * 'net rpc share migrate' entrypoint.
4422 * @param argc Standard main() style argc.
4423 * @param argv Standard main() style argv. Initial components are already
4426 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4429 struct functable func[] = {
4432 rpc_share_migrate_all,
4434 N_("Migrate shares from remote to local server"),
4435 N_("net rpc share migrate all\n"
4436 " Migrate shares from remote to local server")
4440 rpc_share_migrate_files,
4442 N_("Migrate files from remote to local server"),
4443 N_("net rpc share migrate files\n"
4444 " Migrate files from remote to local server")
4448 rpc_share_migrate_security,
4450 N_("Migrate share-ACLs from remote to local server"),
4451 N_("net rpc share migrate security\n"
4452 " Migrate share-ACLs from remote to local server")
4456 rpc_share_migrate_shares,
4458 N_("Migrate shares from remote to local server"),
4459 N_("net rpc share migrate shares\n"
4460 " Migrate shares from remote to local server")
4462 {NULL, NULL, 0, NULL, NULL}
4465 net_mode_share = NET_MODE_SHARE_MIGRATE;
4467 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4473 struct dom_sid *members;
4476 static int num_server_aliases;
4477 static struct full_alias *server_aliases;
4480 * Add an alias to the static list.
4482 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4484 if (server_aliases == NULL)
4485 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4487 server_aliases[num_server_aliases] = *alias;
4488 num_server_aliases += 1;
4492 * For a specific domain on the server, fetch all the aliases
4493 * and their members. Add all of them to the server_aliases.
4496 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4497 TALLOC_CTX *mem_ctx,
4498 struct policy_handle *connect_pol,
4499 const struct dom_sid *domain_sid)
4501 uint32 start_idx, max_entries, num_entries, i;
4502 struct samr_SamArray *groups = NULL;
4503 NTSTATUS result, status;
4504 struct policy_handle domain_pol;
4505 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4507 /* Get domain policy handle */
4509 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4511 MAXIMUM_ALLOWED_ACCESS,
4512 discard_const_p(struct dom_sid2, domain_sid),
4515 if (!NT_STATUS_IS_OK(status)) {
4518 if (!NT_STATUS_IS_OK(result)) {
4526 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4533 if (!NT_STATUS_IS_OK(status)) {
4536 for (i = 0; i < num_entries; i++) {
4538 struct policy_handle alias_pol;
4539 struct full_alias alias;
4540 struct lsa_SidArray sid_array;
4544 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4546 MAXIMUM_ALLOWED_ACCESS,
4547 groups->entries[i].idx,
4550 if (!NT_STATUS_IS_OK(status)) {
4553 if (!NT_STATUS_IS_OK(_result)) {
4558 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4562 if (!NT_STATUS_IS_OK(status)) {
4565 if (!NT_STATUS_IS_OK(_result)) {
4570 alias.num_members = sid_array.num_sids;
4572 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4573 if (!NT_STATUS_IS_OK(status)) {
4576 if (!NT_STATUS_IS_OK(_result)) {
4581 alias.members = NULL;
4583 if (alias.num_members > 0) {
4584 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4586 for (j = 0; j < alias.num_members; j++)
4587 sid_copy(&alias.members[j],
4588 sid_array.sids[j].sid);
4591 sid_compose(&alias.sid, domain_sid,
4592 groups->entries[i].idx);
4594 push_alias(mem_ctx, &alias);
4596 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4598 status = NT_STATUS_OK;
4601 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4607 * Dump server_aliases as names for debugging purposes.
4610 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4611 const struct dom_sid *domain_sid,
4612 const char *domain_name,
4613 struct cli_state *cli,
4614 struct rpc_pipe_client *pipe_hnd,
4615 TALLOC_CTX *mem_ctx,
4621 struct policy_handle lsa_pol;
4622 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4624 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4625 SEC_FLAG_MAXIMUM_ALLOWED,
4627 if (!NT_STATUS_IS_OK(result))
4630 for (i=0; i<num_server_aliases; i++) {
4633 enum lsa_SidType *types;
4636 struct full_alias *alias = &server_aliases[i];
4638 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4640 &domains, &names, &types);
4641 if (!NT_STATUS_IS_OK(result))
4644 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4646 if (alias->num_members == 0) {
4651 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4654 &domains, &names, &types);
4656 if (!NT_STATUS_IS_OK(result) &&
4657 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4660 for (j=0; j<alias->num_members; j++)
4661 DEBUG(1, ("%s\\%s (%d); ",
4662 domains[j] ? domains[j] : "*unknown*",
4663 names[j] ? names[j] : "*unknown*",types[j]));
4667 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4669 return NT_STATUS_OK;
4673 * Fetch a list of all server aliases and their members into
4677 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4678 const struct dom_sid *domain_sid,
4679 const char *domain_name,
4680 struct cli_state *cli,
4681 struct rpc_pipe_client *pipe_hnd,
4682 TALLOC_CTX *mem_ctx,
4686 NTSTATUS result, status;
4687 struct policy_handle connect_pol;
4688 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4690 status = dcerpc_samr_Connect2(b, mem_ctx,
4692 MAXIMUM_ALLOWED_ACCESS,
4695 if (!NT_STATUS_IS_OK(status)) {
4698 if (!NT_STATUS_IS_OK(result)) {
4703 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4704 &global_sid_Builtin);
4705 if (!NT_STATUS_IS_OK(status)) {
4709 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4712 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4717 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4719 token->num_sids = 4;
4721 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4722 d_fprintf(stderr, "malloc %s\n",_("failed"));
4723 token->num_sids = 0;
4727 token->sids[0] = *user_sid;
4728 sid_copy(&token->sids[1], &global_sid_World);
4729 sid_copy(&token->sids[2], &global_sid_Network);
4730 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4733 static void free_user_token(struct security_token *token)
4735 SAFE_FREE(token->sids);
4738 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4740 if (security_token_has_sid(token, sid))
4743 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4748 sid_copy(&token->sids[token->num_sids], sid);
4750 token->num_sids += 1;
4755 struct security_token token;
4758 static void dump_user_token(struct user_token *token)
4762 d_printf("%s\n", token->name);
4764 for (i=0; i<token->token.num_sids; i++) {
4765 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4769 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4773 for (i=0; i<alias->num_members; i++) {
4774 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4781 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4785 for (i=0; i<num_server_aliases; i++) {
4786 if (is_alias_member(&sid, &server_aliases[i]))
4787 add_sid_to_token(token, &server_aliases[i].sid);
4792 * We got a user token with all the SIDs we can know about without asking the
4793 * server directly. These are the user and domain group sids. All of these can
4794 * be members of aliases. So scan the list of aliases for each of the SIDs and
4795 * add them to the token.
4798 static void collect_alias_memberships(struct security_token *token)
4800 int num_global_sids = token->num_sids;
4803 for (i=0; i<num_global_sids; i++) {
4804 collect_sid_memberships(token, token->sids[i]);
4808 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4810 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4811 enum wbcSidType type;
4813 struct wbcDomainSid wsid;
4814 char sid_str[WBC_SID_STRING_BUFLEN];
4815 struct dom_sid user_sid;
4816 uint32_t num_groups;
4817 gid_t *groups = NULL;
4820 fstr_sprintf(full_name, "%s%c%s",
4821 domain, *lp_winbind_separator(), user);
4823 /* First let's find out the user sid */
4825 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4827 if (!WBC_ERROR_IS_OK(wbc_status)) {
4828 DEBUG(1, ("winbind could not find %s: %s\n",
4829 full_name, wbcErrorString(wbc_status)));
4833 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4835 if (type != WBC_SID_NAME_USER) {
4836 DEBUG(1, ("%s is not a user\n", full_name));
4840 if (!string_to_sid(&user_sid, sid_str)) {
4841 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4845 init_user_token(token, &user_sid);
4847 /* And now the groups winbind knows about */
4849 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4850 if (!WBC_ERROR_IS_OK(wbc_status)) {
4851 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4852 full_name, wbcErrorString(wbc_status)));
4856 for (i = 0; i < num_groups; i++) {
4857 gid_t gid = groups[i];
4861 wbc_status = wbcGidToSid(gid, &wsid);
4862 if (!WBC_ERROR_IS_OK(wbc_status)) {
4863 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4864 (unsigned int)gid, wbcErrorString(wbc_status)));
4865 wbcFreeMemory(groups);
4869 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4871 DEBUG(3, (" %s\n", sid_str));
4873 ok = string_to_sid(&sid, sid_str);
4875 DEBUG(1, ("Failed to convert string to SID\n"));
4876 wbcFreeMemory(groups);
4879 add_sid_to_token(token, &sid);
4881 wbcFreeMemory(groups);
4887 * Get a list of all user tokens we want to look at
4890 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4891 struct user_token **user_tokens)
4893 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4894 uint32_t i, num_users;
4896 struct user_token *result;
4897 TALLOC_CTX *frame = NULL;
4899 if (lp_winbind_use_default_domain() &&
4900 (c->opt_target_workgroup == NULL)) {
4901 d_fprintf(stderr, _("winbind use default domain = yes set, "
4902 "please specify a workgroup\n"));
4906 /* Send request to winbind daemon */
4908 wbc_status = wbcListUsers(NULL, &num_users, &users);
4909 if (!WBC_ERROR_IS_OK(wbc_status)) {
4910 DEBUG(1, (_("winbind could not list users: %s\n"),
4911 wbcErrorString(wbc_status)));
4915 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4917 if (result == NULL) {
4918 DEBUG(1, ("Could not malloc sid array\n"));
4919 wbcFreeMemory(users);
4923 frame = talloc_stackframe();
4924 for (i=0; i < num_users; i++) {
4925 fstring domain, user;
4928 fstrcpy(result[i].name, users[i]);
4930 p = strchr(users[i], *lp_winbind_separator());
4932 DEBUG(3, ("%s\n", users[i]));
4935 fstrcpy(domain, c->opt_target_workgroup);
4936 fstrcpy(user, users[i]);
4939 fstrcpy(domain, users[i]);
4940 if (!strupper_m(domain)) {
4941 DEBUG(1, ("strupper_m %s failed\n", domain));
4942 wbcFreeMemory(users);
4948 get_user_sids(domain, user, &(result[i].token));
4951 wbcFreeMemory(users);
4953 *num_tokens = num_users;
4954 *user_tokens = result;
4959 static bool get_user_tokens_from_file(FILE *f,
4961 struct user_token **tokens)
4963 struct user_token *token = NULL;
4968 if (fgets(line, sizeof(line)-1, f) == NULL) {
4972 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4973 line[strlen(line)-1] = '\0';
4976 if (line[0] == ' ') {
4980 if(!string_to_sid(&sid, &line[1])) {
4981 DEBUG(1,("get_user_tokens_from_file: Could "
4982 "not convert sid %s \n",&line[1]));
4986 if (token == NULL) {
4987 DEBUG(0, ("File does not begin with username"));
4991 add_sid_to_token(&token->token, &sid);
4995 /* And a new user... */
4998 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4999 if (*tokens == NULL) {
5000 DEBUG(0, ("Could not realloc tokens\n"));
5004 token = &((*tokens)[*num_tokens-1]);
5006 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
5009 token->token.num_sids = 0;
5010 token->token.sids = NULL;
5019 * Show the list of all users that have access to a share
5022 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
5023 struct cli_state *cli,
5024 TALLOC_CTX *mem_ctx,
5025 const char *netname,
5027 struct user_token *tokens)
5030 struct security_descriptor *share_sd = NULL;
5031 struct security_descriptor *root_sd = NULL;
5033 union srvsvc_NetShareInfo info;
5037 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5039 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5046 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5047 DEBUG(1, ("Coult not query secdesc for share %s\n",
5052 share_sd = info.info502->sd_buf.sd;
5053 if (share_sd == NULL) {
5054 DEBUG(1, ("Got no secdesc for share %s\n",
5058 cnum = cli_state_get_tid(cli);
5060 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", "", 0))) {
5064 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
5065 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
5066 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
5069 for (i=0; i<num_tokens; i++) {
5072 if (share_sd != NULL) {
5073 status = se_access_check(share_sd, &tokens[i].token,
5076 if (!NT_STATUS_IS_OK(status)) {
5077 DEBUG(1, ("Could not check share_sd for "
5084 if (root_sd == NULL) {
5085 d_printf(" %s\n", tokens[i].name);
5089 status = se_access_check(root_sd, &tokens[i].token,
5091 if (!NT_STATUS_IS_OK(status)) {
5092 DEBUG(1, ("Could not check root_sd for user %s\n",
5096 d_printf(" %s\n", tokens[i].name);
5099 if (fnum != (uint16_t)-1)
5100 cli_close(cli, fnum);
5102 cli_state_set_tid(cli, cnum);
5108 * List shares on a remote RPC server, including the security descriptors.
5110 * All parameters are provided by the run_rpc_command function, except for
5111 * argc, argv which are passed through.
5113 * @param domain_sid The domain sid acquired from the remote server.
5114 * @param cli A cli_state connected to the server.
5115 * @param mem_ctx Talloc context, destroyed on completion of the function.
5116 * @param argc Standard main() style argc.
5117 * @param argv Standard main() style argv. Initial components are already
5120 * @return Normal NTSTATUS return.
5123 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
5124 const struct dom_sid *domain_sid,
5125 const char *domain_name,
5126 struct cli_state *cli,
5127 struct rpc_pipe_client *pipe_hnd,
5128 TALLOC_CTX *mem_ctx,
5134 NTSTATUS nt_status = NT_STATUS_OK;
5135 uint32_t total_entries = 0;
5136 uint32_t resume_handle = 0;
5137 uint32_t preferred_len = 0xffffffff;
5139 struct dcerpc_binding_handle *b = NULL;
5140 struct srvsvc_NetShareInfoCtr info_ctr;
5141 struct srvsvc_NetShareCtr1 ctr1;
5144 struct user_token *tokens = NULL;
5150 f = fopen(argv[0], "r");
5154 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
5155 return NT_STATUS_UNSUCCESSFUL;
5158 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5164 DEBUG(0, ("Could not read users from file\n"));
5165 return NT_STATUS_UNSUCCESSFUL;
5168 for (i=0; i<num_tokens; i++)
5169 collect_alias_memberships(&tokens[i].token);
5171 ZERO_STRUCT(info_ctr);
5175 info_ctr.ctr.ctr1 = &ctr1;
5177 b = pipe_hnd->binding_handle;
5179 /* Issue the NetShareEnum RPC call and retrieve the response */
5180 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5189 /* Was it successful? */
5190 if (!NT_STATUS_IS_OK(nt_status)) {
5191 /* Nope. Go clean up. */
5195 if (!W_ERROR_IS_OK(result)) {
5196 /* Nope. Go clean up. */
5197 nt_status = werror_to_ntstatus(result);
5201 if (total_entries == 0) {
5205 /* For each returned entry... */
5206 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5207 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5209 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5213 d_printf("%s\n", netname);
5215 show_userlist(pipe_hnd, cli, mem_ctx, netname,
5216 num_tokens, tokens);
5219 for (i=0; i<num_tokens; i++) {
5220 free_user_token(&tokens[i].token);
5227 static int rpc_share_allowedusers(struct net_context *c, int argc,
5232 if (c->display_usage) {
5234 "net rpc share allowedusers\n"
5237 _("List allowed users"));
5241 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5242 rpc_aliaslist_internals,
5247 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5253 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5254 rpc_share_allowedusers_internals,
5258 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5261 struct user_token *tokens = NULL;
5265 net_usersidlist_usage(c, argc, argv);
5269 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5270 DEBUG(0, ("Could not get the user/sid list\n"));
5274 for (i=0; i<num_tokens; i++) {
5275 dump_user_token(&tokens[i]);
5276 free_user_token(&tokens[i].token);
5283 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5285 d_printf(_("net usersidlist\n"
5286 "\tprints out a list of all users the running winbind knows\n"
5287 "\tabout, together with all their SIDs. This is used as\n"
5288 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5290 net_common_flags_usage(c, argc, argv);
5295 * 'net rpc share' entrypoint.
5296 * @param argc Standard main() style argc.
5297 * @param argv Standard main() style argv. Initial components are already
5301 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5303 NET_API_STATUS status;
5305 struct functable func[] = {
5311 N_("net rpc share add\n"
5319 N_("net rpc share delete\n"
5324 rpc_share_allowedusers,
5326 N_("Modify allowed users"),
5327 N_("net rpc share allowedusers\n"
5328 " Modify allowed users")
5334 N_("Migrate share to local server"),
5335 N_("net rpc share migrate\n"
5336 " Migrate share to local server")
5343 N_("net rpc share list\n"
5346 {NULL, NULL, 0, NULL, NULL}
5349 status = libnetapi_net_init(&c->netapi_ctx);
5353 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5354 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5355 if (c->opt_kerberos) {
5356 libnetapi_set_use_kerberos(c->netapi_ctx);
5360 if (c->display_usage) {
5365 " Alias for net rpc share list\n"));
5366 net_display_usage_from_functable(func);
5370 return rpc_share_list(c, argc, argv);
5373 return net_run_function(c, argc, argv, "net rpc share", func);
5376 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5377 TALLOC_CTX *mem_ctx,
5378 struct rpc_sh_ctx *ctx,
5379 struct rpc_pipe_client *pipe_hnd,
5380 int argc, const char **argv)
5383 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5386 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5387 TALLOC_CTX *mem_ctx,
5388 struct rpc_sh_ctx *ctx,
5389 struct rpc_pipe_client *pipe_hnd,
5390 int argc, const char **argv)
5392 NET_API_STATUS status;
5393 uint32_t parm_err = 0;
5394 struct SHARE_INFO_2 i2;
5396 if ((argc < 2) || (argc > 3)) {
5397 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5399 return NT_STATUS_INVALID_PARAMETER;
5402 i2.shi2_netname = argv[0];
5403 i2.shi2_type = STYPE_DISKTREE;
5404 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5405 i2.shi2_permissions = 0;
5406 i2.shi2_max_uses = 0;
5407 i2.shi2_current_uses = 0;
5408 i2.shi2_path = argv[1];
5409 i2.shi2_passwd = NULL;
5411 status = NetShareAdd(pipe_hnd->desthost,
5416 return werror_to_ntstatus(W_ERROR(status));
5419 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5420 TALLOC_CTX *mem_ctx,
5421 struct rpc_sh_ctx *ctx,
5422 struct rpc_pipe_client *pipe_hnd,
5423 int argc, const char **argv)
5426 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5427 return NT_STATUS_INVALID_PARAMETER;
5430 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5433 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5434 TALLOC_CTX *mem_ctx,
5435 struct rpc_sh_ctx *ctx,
5436 struct rpc_pipe_client *pipe_hnd,
5437 int argc, const char **argv)
5439 union srvsvc_NetShareInfo info;
5442 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5445 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5446 return NT_STATUS_INVALID_PARAMETER;
5449 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5455 if (!NT_STATUS_IS_OK(status)) {
5456 result = ntstatus_to_werror(status);
5459 if (!W_ERROR_IS_OK(result)) {
5463 d_printf(_("Name: %s\n"), info.info2->name);
5464 d_printf(_("Comment: %s\n"), info.info2->comment);
5465 d_printf(_("Path: %s\n"), info.info2->path);
5466 d_printf(_("Password: %s\n"), info.info2->password);
5469 return werror_to_ntstatus(result);
5472 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5473 struct rpc_sh_ctx *ctx)
5475 static struct rpc_sh_cmd cmds[] = {
5477 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5478 N_("List available shares") },
5480 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5481 N_("Add a share") },
5483 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5484 N_("Delete a share") },
5486 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5487 N_("Get information about a share") },
5489 { NULL, NULL, 0, NULL, NULL }
5495 /****************************************************************************/
5497 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5499 return net_file_usage(c, argc, argv);
5503 * Close a file on a remote RPC server.
5505 * @param argc Standard main() style argc.
5506 * @param argv Standard main() style argv. Initial components are already
5509 * @return A shell status integer (0 for success).
5511 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5513 if (argc < 1 || c->display_usage) {
5514 return rpc_file_usage(c, argc, argv);
5517 return NetFileClose(c->opt_host, atoi(argv[0]));
5521 * Formatted print of open file info
5523 * @param r struct FILE_INFO_3 contents
5526 static void display_file_info_3(struct FILE_INFO_3 *r)
5528 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5529 r->fi3_id, r->fi3_username, r->fi3_permissions,
5530 r->fi3_num_locks, r->fi3_pathname);
5534 * List files for a user on a remote RPC server.
5536 * @param argc Standard main() style argc.
5537 * @param argv Standard main() style argv. Initial components are already
5540 * @return A shell status integer (0 for success)..
5543 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5545 NET_API_STATUS status;
5546 uint32 preferred_len = 0xffffffff, i;
5547 char *username=NULL;
5548 uint32_t total_entries = 0;
5549 uint32_t entries_read = 0;
5550 uint32_t resume_handle = 0;
5551 struct FILE_INFO_3 *i3 = NULL;
5553 if (c->display_usage) {
5554 return rpc_file_usage(c, argc, argv);
5557 /* if argc > 0, must be user command */
5559 username = smb_xstrdup(argv[0]);
5562 status = NetFileEnum(c->opt_host,
5566 (uint8_t **)(void *)&i3,
5576 /* Display results */
5579 "\nEnumerating open files on remote server:\n\n"
5580 "\nFileId Opened by Perms Locks Path"
5581 "\n------ --------- ----- ----- ---- \n"));
5582 for (i = 0; i < entries_read; i++) {
5583 display_file_info_3(&i3[i]);
5586 SAFE_FREE(username);
5591 * 'net rpc file' entrypoint.
5592 * @param argc Standard main() style argc.
5593 * @param argv Standard main() style argv. Initial components are already
5597 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5599 NET_API_STATUS status;
5601 struct functable func[] = {
5606 N_("Close opened file"),
5607 N_("net rpc file close\n"
5608 " Close opened file")
5614 N_("List files opened by user"),
5615 N_("net rpc file user\n"
5616 " List files opened by user")
5623 N_("Display information about opened file"),
5624 N_("net rpc file info\n"
5625 " Display information about opened file")
5628 {NULL, NULL, 0, NULL, NULL}
5631 status = libnetapi_net_init(&c->netapi_ctx);
5635 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5636 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5637 if (c->opt_kerberos) {
5638 libnetapi_set_use_kerberos(c->netapi_ctx);
5642 if (c->display_usage) {
5643 d_printf(_("Usage:\n"));
5644 d_printf(_("net rpc file\n"
5645 " List opened files\n"));
5646 net_display_usage_from_functable(func);
5650 return rpc_file_user(c, argc, argv);
5653 return net_run_function(c, argc, argv, "net rpc file", func);
5657 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5659 * All parameters are provided by the run_rpc_command function, except for
5660 * argc, argv which are passed through.
5662 * @param c A net_context structure.
5663 * @param domain_sid The domain sid acquired from the remote server.
5664 * @param cli A cli_state connected to the server.
5665 * @param mem_ctx Talloc context, destroyed on completion of the function.
5666 * @param argc Standard main() style argc.
5667 * @param argv Standard main() style argv. Initial components are already
5670 * @return Normal NTSTATUS return.
5673 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5674 const struct dom_sid *domain_sid,
5675 const char *domain_name,
5676 struct cli_state *cli,
5677 struct rpc_pipe_client *pipe_hnd,
5678 TALLOC_CTX *mem_ctx,
5682 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5684 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5686 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5687 if (!NT_STATUS_IS_OK(status)) {
5690 if (W_ERROR_IS_OK(result)) {
5691 d_printf(_("\nShutdown successfully aborted\n"));
5692 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5694 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5696 return werror_to_ntstatus(result);
5700 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5702 * All parameters are provided by the run_rpc_command function, except for
5703 * argc, argv which are passed through.
5705 * @param c A net_context structure.
5706 * @param domain_sid The domain sid acquired from the remote server.
5707 * @param cli A cli_state connected to the server.
5708 * @param mem_ctx Talloc context, destroyed on completion of the function.
5709 * @param argc Standard main() style argc.
5710 * @param argv Standard main() style argv. Initial components are already
5713 * @return Normal NTSTATUS return.
5716 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5717 const struct dom_sid *domain_sid,
5718 const char *domain_name,
5719 struct cli_state *cli,
5720 struct rpc_pipe_client *pipe_hnd,
5721 TALLOC_CTX *mem_ctx,
5725 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5727 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5729 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5731 if (!NT_STATUS_IS_OK(result)) {
5732 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5735 if (W_ERROR_IS_OK(werr)) {
5736 d_printf(_("\nShutdown successfully aborted\n"));
5737 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5739 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5741 return werror_to_ntstatus(werr);
5745 * ABORT the shutdown of a remote RPC server.
5747 * @param argc Standard main() style argc.
5748 * @param argv Standard main() style argv. Initial components are already
5751 * @return A shell status integer (0 for success).
5754 static int rpc_shutdown_abort(struct net_context *c, int argc,
5759 if (c->display_usage) {
5761 "net rpc abortshutdown\n"
5764 _("Abort a scheduled shutdown"));
5768 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5769 rpc_shutdown_abort_internals, argc, argv);
5774 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5776 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5777 rpc_reg_shutdown_abort_internals,
5782 * Shut down a remote RPC Server via initshutdown pipe.
5784 * All parameters are provided by the run_rpc_command function, except for
5785 * argc, argv which are passed through.
5787 * @param c A net_context structure.
5788 * @param domain_sid The domain sid acquired from the remote server.
5789 * @param cli A cli_state connected to the server.
5790 * @param mem_ctx Talloc context, destroyed on completion of the function.
5791 * @param argc Standard main() style argc.
5792 * @param argv Standard main() style argv. Initial components are already
5795 * @return Normal NTSTATUS return.
5798 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5799 const struct dom_sid *domain_sid,
5800 const char *domain_name,
5801 struct cli_state *cli,
5802 struct rpc_pipe_client *pipe_hnd,
5803 TALLOC_CTX *mem_ctx,
5807 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5809 const char *msg = N_("This machine will be shutdown shortly");
5810 uint32 timeout = 20;
5811 struct lsa_StringLarge msg_string;
5812 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5814 if (c->opt_comment) {
5815 msg = c->opt_comment;
5817 if (c->opt_timeout) {
5818 timeout = c->opt_timeout;
5821 msg_string.string = msg;
5823 /* create an entry */
5824 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5825 &msg_string, timeout, c->opt_force, c->opt_reboot,
5827 if (!NT_STATUS_IS_OK(status)) {
5830 if (W_ERROR_IS_OK(result)) {
5831 d_printf(_("\nShutdown of remote machine succeeded\n"));
5832 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5834 DEBUG(1,("Shutdown of remote machine failed!\n"));
5836 return werror_to_ntstatus(result);
5840 * Shut down a remote RPC Server via winreg pipe.
5842 * All parameters are provided by the run_rpc_command function, except for
5843 * argc, argv which are passed through.
5845 * @param c A net_context structure.
5846 * @param domain_sid The domain sid acquired from the remote server.
5847 * @param cli A cli_state connected to the server.
5848 * @param mem_ctx Talloc context, destroyed on completion of the function.
5849 * @param argc Standard main() style argc.
5850 * @param argv Standard main() style argv. Initial components are already
5853 * @return Normal NTSTATUS return.
5856 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5857 const struct dom_sid *domain_sid,
5858 const char *domain_name,
5859 struct cli_state *cli,
5860 struct rpc_pipe_client *pipe_hnd,
5861 TALLOC_CTX *mem_ctx,
5865 const char *msg = N_("This machine will be shutdown shortly");
5866 uint32 timeout = 20;
5867 struct lsa_StringLarge msg_string;
5870 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5872 if (c->opt_comment) {
5873 msg = c->opt_comment;
5875 msg_string.string = msg;
5877 if (c->opt_timeout) {
5878 timeout = c->opt_timeout;
5881 /* create an entry */
5882 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5883 &msg_string, timeout, c->opt_force, c->opt_reboot,
5885 if (!NT_STATUS_IS_OK(result)) {
5886 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5890 if (W_ERROR_IS_OK(werr)) {
5891 d_printf(_("\nShutdown of remote machine succeeded\n"));
5893 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5894 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5895 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5897 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5900 return werror_to_ntstatus(werr);
5904 * Shut down a remote RPC server.
5906 * @param argc Standard main() style argc.
5907 * @param argv Standard main() style argv. Initial components are already
5910 * @return A shell status integer (0 for success).
5913 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5917 if (c->display_usage) {
5919 "net rpc shutdown\n"
5922 _("Shut down a remote RPC server"));
5926 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5927 rpc_init_shutdown_internals, argc, argv);
5930 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5931 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5932 rpc_reg_shutdown_internals, argc, argv);
5938 /***************************************************************************
5939 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5940 ***************************************************************************/
5943 * Add interdomain trust account to the RPC server.
5944 * All parameters (except for argc and argv) are passed by run_rpc_command
5947 * @param c A net_context structure.
5948 * @param domain_sid The domain sid acquired from the server.
5949 * @param cli A cli_state connected to the server.
5950 * @param mem_ctx Talloc context, destroyed on completion of the function.
5951 * @param argc Standard main() style argc.
5952 * @param argv Standard main() style argv. Initial components are already
5955 * @return normal NTSTATUS return code.
5958 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5959 const struct dom_sid *domain_sid,
5960 const char *domain_name,
5961 struct cli_state *cli,
5962 struct rpc_pipe_client *pipe_hnd,
5963 TALLOC_CTX *mem_ctx,
5967 struct policy_handle connect_pol, domain_pol, user_pol;
5968 NTSTATUS status, result;
5970 struct lsa_String lsa_acct_name;
5972 uint32 acct_flags=0;
5974 uint32_t access_granted = 0;
5975 union samr_UserInfo info;
5976 unsigned int orig_timeout;
5977 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5978 DATA_BLOB session_key = data_blob_null;
5983 _(" net rpc trustdom add <domain_name> "
5984 "<trust password>\n"));
5985 return NT_STATUS_INVALID_PARAMETER;
5989 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5992 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5993 return NT_STATUS_NO_MEMORY;
5996 if (!strupper_m(acct_name)) {
5997 SAFE_FREE(acct_name);
5998 return NT_STATUS_INVALID_PARAMETER;
6001 init_lsa_String(&lsa_acct_name, acct_name);
6003 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6004 if (!NT_STATUS_IS_OK(status)) {
6005 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
6006 nt_errstr(status)));
6010 /* Get samr policy handle */
6011 status = dcerpc_samr_Connect2(b, mem_ctx,
6013 MAXIMUM_ALLOWED_ACCESS,
6016 if (!NT_STATUS_IS_OK(status)) {
6019 if (!NT_STATUS_IS_OK(result)) {
6024 /* Get domain policy handle */
6025 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6027 MAXIMUM_ALLOWED_ACCESS,
6028 discard_const_p(struct dom_sid2, domain_sid),
6031 if (!NT_STATUS_IS_OK(status)) {
6034 if (!NT_STATUS_IS_OK(result)) {
6039 /* This call can take a long time - allow the server to time out.
6040 * 35 seconds should do it. */
6042 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
6044 /* Create trusting domain's account */
6045 acb_info = ACB_NORMAL;
6046 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
6047 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
6048 SAMR_USER_ACCESS_SET_PASSWORD |
6049 SAMR_USER_ACCESS_GET_ATTRIBUTES |
6050 SAMR_USER_ACCESS_SET_ATTRIBUTES;
6052 status = dcerpc_samr_CreateUser2(b, mem_ctx,
6061 if (!NT_STATUS_IS_OK(status)) {
6064 /* And restore our original timeout. */
6065 rpccli_set_timeout(pipe_hnd, orig_timeout);
6067 if (!NT_STATUS_IS_OK(result)) {
6069 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
6070 acct_name, nt_errstr(result));
6075 struct samr_CryptPassword crypt_pwd;
6077 ZERO_STRUCT(info.info23);
6079 init_samr_CryptPassword(argv[1],
6083 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
6084 SAMR_FIELD_NT_PASSWORD_PRESENT;
6085 info.info23.info.acct_flags = ACB_DOMTRUST;
6086 info.info23.password = crypt_pwd;
6088 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
6093 if (!NT_STATUS_IS_OK(status)) {
6097 if (!NT_STATUS_IS_OK(result)) {
6099 DEBUG(0,("Could not set trust account password: %s\n",
6100 nt_errstr(result)));
6106 SAFE_FREE(acct_name);
6107 data_blob_clear_free(&session_key);
6112 * Create interdomain trust account for a remote domain.
6114 * @param argc Standard argc.
6115 * @param argv Standard argv without initial components.
6117 * @return Integer status (0 means success).
6120 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
6122 if (argc > 0 && !c->display_usage) {
6123 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6124 rpc_trustdom_add_internals, argc, argv);
6128 _("net rpc trustdom add <domain_name> <trust "
6136 * Remove interdomain trust account from the RPC server.
6137 * All parameters (except for argc and argv) are passed by run_rpc_command
6140 * @param c A net_context structure.
6141 * @param domain_sid The domain sid acquired from the server.
6142 * @param cli A cli_state connected to the server.
6143 * @param mem_ctx Talloc context, destroyed on completion of the function.
6144 * @param argc Standard main() style argc.
6145 * @param argv Standard main() style argv. Initial components are already
6148 * @return normal NTSTATUS return code.
6151 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6152 const struct dom_sid *domain_sid,
6153 const char *domain_name,
6154 struct cli_state *cli,
6155 struct rpc_pipe_client *pipe_hnd,
6156 TALLOC_CTX *mem_ctx,
6160 struct policy_handle connect_pol, domain_pol, user_pol;
6161 NTSTATUS status, result;
6163 struct dom_sid trust_acct_sid;
6164 struct samr_Ids user_rids, name_types;
6165 struct lsa_String lsa_acct_name;
6166 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6171 _(" net rpc trustdom del <domain_name>\n"));
6172 return NT_STATUS_INVALID_PARAMETER;
6176 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6178 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6180 if (acct_name == NULL)
6181 return NT_STATUS_NO_MEMORY;
6183 if (!strupper_m(acct_name)) {
6184 TALLOC_FREE(acct_name);
6185 return NT_STATUS_INVALID_PARAMETER;
6188 /* Get samr policy handle */
6189 status = dcerpc_samr_Connect2(b, mem_ctx,
6191 MAXIMUM_ALLOWED_ACCESS,
6194 if (!NT_STATUS_IS_OK(status)) {
6197 if (!NT_STATUS_IS_OK(result)) {
6202 /* Get domain policy handle */
6203 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6205 MAXIMUM_ALLOWED_ACCESS,
6206 discard_const_p(struct dom_sid2, domain_sid),
6209 if (!NT_STATUS_IS_OK(status)) {
6212 if (!NT_STATUS_IS_OK(result)) {
6217 init_lsa_String(&lsa_acct_name, acct_name);
6219 status = dcerpc_samr_LookupNames(b, mem_ctx,
6226 if (!NT_STATUS_IS_OK(status)) {
6227 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6229 acct_name, nt_errstr(status));
6232 if (!NT_STATUS_IS_OK(result)) {
6234 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6236 acct_name, nt_errstr(result) );
6240 status = dcerpc_samr_OpenUser(b, mem_ctx,
6242 MAXIMUM_ALLOWED_ACCESS,
6246 if (!NT_STATUS_IS_OK(status)) {
6247 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6249 acct_name, nt_errstr(status) );
6253 if (!NT_STATUS_IS_OK(result)) {
6255 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6257 acct_name, nt_errstr(result) );
6261 /* append the rid to the domain sid */
6262 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6266 /* remove the sid */
6268 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6272 if (!NT_STATUS_IS_OK(status)) {
6273 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6274 " on user %s failed %s\n"),
6275 acct_name, nt_errstr(status));
6278 if (!NT_STATUS_IS_OK(result)) {
6280 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6281 " on user %s failed %s\n"),
6282 acct_name, nt_errstr(result) );
6289 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6292 if (!NT_STATUS_IS_OK(status)) {
6293 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6295 acct_name, nt_errstr(status));
6299 if (!NT_STATUS_IS_OK(result)) {
6301 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6303 acct_name, nt_errstr(result) );
6307 if (!NT_STATUS_IS_OK(result)) {
6308 d_printf(_("Could not set trust account password: %s\n"),
6318 * Delete interdomain trust account for a remote domain.
6320 * @param argc Standard argc.
6321 * @param argv Standard argv without initial components.
6323 * @return Integer status (0 means success).
6326 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6328 if (argc > 0 && !c->display_usage) {
6329 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6330 rpc_trustdom_del_internals, argc, argv);
6334 _("net rpc trustdom del <domain>\n"));
6339 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6340 struct cli_state *cli,
6341 TALLOC_CTX *mem_ctx,
6342 const char *domain_name)
6344 char *dc_name = NULL;
6345 const char *buffer = NULL;
6346 struct rpc_pipe_client *netr;
6349 struct dcerpc_binding_handle *b;
6351 /* Use NetServerEnum2 */
6353 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6355 return NT_STATUS_OK;
6358 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6359 for domain %s\n", domain_name));
6361 /* Try netr_GetDcName */
6363 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
6365 if (!NT_STATUS_IS_OK(status)) {
6369 b = netr->binding_handle;
6371 status = dcerpc_netr_GetDcName(b, mem_ctx,
6378 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6382 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6383 for domain %s\n", domain_name));
6385 if (!NT_STATUS_IS_OK(status)) {
6389 return werror_to_ntstatus(result);
6393 * Establish trust relationship to a trusting domain.
6394 * Interdomain account must already be created on remote PDC.
6396 * @param c A net_context structure.
6397 * @param argc Standard argc.
6398 * @param argv Standard argv without initial components.
6400 * @return Integer status (0 means success).
6403 static int rpc_trustdom_establish(struct net_context *c, int argc,
6406 struct cli_state *cli = NULL;
6407 struct sockaddr_storage server_ss;
6408 struct rpc_pipe_client *pipe_hnd = NULL;
6409 struct policy_handle connect_hnd;
6410 TALLOC_CTX *mem_ctx;
6411 NTSTATUS nt_status, result;
6412 struct dom_sid *domain_sid;
6417 union lsa_PolicyInformation *info = NULL;
6418 struct dcerpc_binding_handle *b;
6421 * Connect to \\server\ipc$ as 'our domain' account with password
6424 if (argc != 1 || c->display_usage) {
6427 _("net rpc trustdom establish <domain_name>\n"));
6431 domain_name = smb_xstrdup(argv[0]);
6432 if (!strupper_m(domain_name)) {
6433 SAFE_FREE(domain_name);
6437 /* account name used at first is our domain's name with '$' */
6438 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6441 if (!strupper_m(acct_name)) {
6442 SAFE_FREE(domain_name);
6443 SAFE_FREE(acct_name);
6448 * opt_workgroup will be used by connection functions further,
6449 * hence it should be set to remote domain name instead of ours
6451 if (c->opt_workgroup) {
6452 c->opt_workgroup = smb_xstrdup(domain_name);
6455 c->opt_user_name = acct_name;
6457 /* find the domain controller */
6458 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6459 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6463 /* connect to ipc$ as username/password */
6464 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6465 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6467 /* Is it trusting domain account for sure ? */
6468 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6469 nt_errstr(nt_status)));
6473 /* store who we connected to */
6475 saf_store( domain_name, pdc_name );
6478 * Connect to \\server\ipc$ again (this time anonymously)
6481 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6484 if (NT_STATUS_IS_ERR(nt_status)) {
6485 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6486 domain_name, nt_errstr(nt_status)));
6490 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6491 "domain %s", domain_name))) {
6492 DEBUG(0, ("talloc_init() failed\n"));
6497 /* Make sure we're talking to a proper server */
6499 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6500 if (!NT_STATUS_IS_OK(nt_status)) {
6502 talloc_destroy(mem_ctx);
6507 * Call LsaOpenPolicy and LsaQueryInfo
6510 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6512 if (!NT_STATUS_IS_OK(nt_status)) {
6513 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6515 talloc_destroy(mem_ctx);
6519 b = pipe_hnd->binding_handle;
6521 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6523 if (NT_STATUS_IS_ERR(nt_status)) {
6524 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6525 nt_errstr(nt_status)));
6527 talloc_destroy(mem_ctx);
6531 /* Querying info level 5 */
6533 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6535 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6538 if (NT_STATUS_IS_ERR(nt_status)) {
6539 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6540 nt_errstr(nt_status)));
6542 talloc_destroy(mem_ctx);
6545 if (NT_STATUS_IS_ERR(result)) {
6546 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6547 nt_errstr(result)));
6549 talloc_destroy(mem_ctx);
6553 domain_sid = info->account_domain.sid;
6555 /* There should be actually query info level 3 (following nt serv behaviour),
6556 but I still don't know if it's _really_ necessary */
6559 * Store the password in secrets db
6562 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6563 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6565 talloc_destroy(mem_ctx);
6570 * Close the pipes and clean up
6573 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6574 if (NT_STATUS_IS_ERR(nt_status)) {
6575 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6576 nt_errstr(nt_status)));
6578 talloc_destroy(mem_ctx);
6584 talloc_destroy(mem_ctx);
6586 d_printf(_("Trust to domain %s established\n"), domain_name);
6591 * Revoke trust relationship to the remote domain.
6593 * @param c A net_context structure.
6594 * @param argc Standard argc.
6595 * @param argv Standard argv without initial components.
6597 * @return Integer status (0 means success).
6600 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6606 if (argc < 1 || c->display_usage) {
6609 _("net rpc trustdom revoke <domain_name>\n"
6610 " Revoke trust relationship\n"
6611 " domain_name\tName of domain to revoke trust\n"));
6615 /* generate upper cased domain name */
6616 domain_name = smb_xstrdup(argv[0]);
6617 if (!strupper_m(domain_name)) {
6618 SAFE_FREE(domain_name);
6622 /* delete password of the trust */
6623 if (!pdb_del_trusteddom_pw(domain_name)) {
6624 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6631 SAFE_FREE(domain_name);
6635 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6636 const struct dom_sid *domain_sid,
6637 const char *domain_name,
6638 struct cli_state *cli,
6639 struct rpc_pipe_client *pipe_hnd,
6640 TALLOC_CTX *mem_ctx,
6645 if (!sid_to_fstring(str_sid, domain_sid)) {
6646 return NT_STATUS_UNSUCCESSFUL;
6648 d_printf("%s\n", str_sid);
6649 return NT_STATUS_OK;
6652 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6656 /* convert sid into ascii string */
6657 sid_to_fstring(ascii_sid, dom_sid);
6659 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6662 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6663 TALLOC_CTX *mem_ctx,
6664 struct policy_handle *pol,
6665 struct dom_sid dom_sid,
6666 const char *trusted_dom_name)
6668 NTSTATUS nt_status, result;
6669 union lsa_TrustedDomainInfo *info = NULL;
6670 char *cleartextpwd = NULL;
6671 DATA_BLOB session_key;
6672 DATA_BLOB data = data_blob_null;
6673 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6675 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6678 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6681 if (NT_STATUS_IS_ERR(nt_status)) {
6682 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6683 nt_errstr(nt_status)));
6686 if (NT_STATUS_IS_ERR(result)) {
6688 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6689 nt_errstr(result)));
6693 data = data_blob(info->password.password->data,
6694 info->password.password->length);
6696 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6697 if (!NT_STATUS_IS_OK(nt_status)) {
6698 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6702 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6703 data_blob_free(&session_key);
6705 if (cleartextpwd == NULL) {
6706 DEBUG(0,("retrieved NULL password\n"));
6707 nt_status = NT_STATUS_UNSUCCESSFUL;
6711 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6712 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6713 nt_status = NT_STATUS_UNSUCCESSFUL;
6717 #ifdef DEBUG_PASSWORD
6718 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6719 "password: [%s]\n", trusted_dom_name,
6720 sid_string_dbg(&dom_sid), cleartextpwd));
6724 SAFE_FREE(cleartextpwd);
6725 data_blob_free(&data);
6730 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6733 /* common variables */
6734 TALLOC_CTX* mem_ctx;
6735 struct cli_state *cli = NULL;
6736 struct rpc_pipe_client *pipe_hnd = NULL;
6737 NTSTATUS nt_status, result;
6738 const char *domain_name = NULL;
6739 struct policy_handle connect_hnd;
6740 union lsa_PolicyInformation *info = NULL;
6742 /* trusted domains listing variables */
6743 unsigned int enum_ctx = 0;
6745 struct lsa_DomainList dom_list;
6747 struct dcerpc_binding_handle *b;
6749 if (c->display_usage) {
6751 "net rpc trustdom vampire\n"
6754 _("Vampire trust relationship from remote server"));
6759 * Listing trusted domains (stored in secrets.tdb, if local)
6762 mem_ctx = talloc_init("trust relationships vampire");
6765 * set domain and pdc name to local samba server (default)
6766 * or to remote one given in command line
6769 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6770 domain_name = c->opt_workgroup;
6771 c->opt_target_workgroup = c->opt_workgroup;
6773 fstrcpy(pdc_name, lp_netbios_name());
6774 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6775 c->opt_target_workgroup = domain_name;
6778 /* open \PIPE\lsarpc and open policy handle */
6779 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6780 if (!NT_STATUS_IS_OK(nt_status)) {
6781 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6782 nt_errstr(nt_status)));
6783 talloc_destroy(mem_ctx);
6787 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6789 if (!NT_STATUS_IS_OK(nt_status)) {
6790 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6791 nt_errstr(nt_status) ));
6793 talloc_destroy(mem_ctx);
6797 b = pipe_hnd->binding_handle;
6799 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6801 if (NT_STATUS_IS_ERR(nt_status)) {
6802 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6803 nt_errstr(nt_status)));
6805 talloc_destroy(mem_ctx);
6809 /* query info level 5 to obtain sid of a domain being queried */
6810 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6812 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6816 if (NT_STATUS_IS_ERR(nt_status)) {
6817 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6818 nt_errstr(nt_status)));
6820 talloc_destroy(mem_ctx);
6823 if (NT_STATUS_IS_ERR(result)) {
6824 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6825 nt_errstr(result)));
6827 talloc_destroy(mem_ctx);
6832 * Keep calling LsaEnumTrustdom over opened pipe until
6833 * the end of enumeration is reached
6836 d_printf(_("Vampire trusted domains:\n\n"));
6839 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6845 if (NT_STATUS_IS_ERR(nt_status)) {
6846 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6847 nt_errstr(nt_status)));
6849 talloc_destroy(mem_ctx);
6852 if (NT_STATUS_IS_ERR(result)) {
6854 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6855 nt_errstr(result)));
6857 talloc_destroy(mem_ctx);
6862 for (i = 0; i < dom_list.count; i++) {
6864 print_trusted_domain(dom_list.domains[i].sid,
6865 dom_list.domains[i].name.string);
6867 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6868 *dom_list.domains[i].sid,
6869 dom_list.domains[i].name.string);
6870 if (!NT_STATUS_IS_OK(nt_status)) {
6872 talloc_destroy(mem_ctx);
6878 * in case of no trusted domains say something rather
6879 * than just display blank line
6881 if (!dom_list.count) d_printf(_("none\n"));
6883 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6885 /* close this connection before doing next one */
6886 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6887 if (NT_STATUS_IS_ERR(nt_status)) {
6888 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6889 nt_errstr(nt_status)));
6891 talloc_destroy(mem_ctx);
6895 /* close lsarpc pipe and connection to IPC$ */
6898 talloc_destroy(mem_ctx);
6902 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6904 /* common variables */
6905 TALLOC_CTX* mem_ctx;
6906 struct cli_state *cli = NULL, *remote_cli = NULL;
6907 struct rpc_pipe_client *pipe_hnd = NULL;
6908 NTSTATUS nt_status, result;
6909 const char *domain_name = NULL;
6910 struct dom_sid *queried_dom_sid;
6911 int ascii_dom_name_len;
6912 struct policy_handle connect_hnd;
6913 union lsa_PolicyInformation *info = NULL;
6914 struct dcerpc_binding_handle *b = NULL;
6916 /* trusted domains listing variables */
6917 unsigned int num_domains, enum_ctx = 0;
6919 struct lsa_DomainList dom_list;
6923 /* trusting domains listing variables */
6924 struct policy_handle domain_hnd;
6925 struct samr_SamArray *trusts = NULL;
6927 if (c->display_usage) {
6929 "net rpc trustdom list\n"
6932 _("List incoming and outgoing trust relationships"));
6937 * Listing trusted domains (stored in secrets.tdb, if local)
6940 mem_ctx = talloc_init("trust relationships listing");
6943 * set domain and pdc name to local samba server (default)
6944 * or to remote one given in command line
6947 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6948 domain_name = c->opt_workgroup;
6949 c->opt_target_workgroup = c->opt_workgroup;
6951 fstrcpy(pdc_name, lp_netbios_name());
6952 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6953 c->opt_target_workgroup = domain_name;
6956 /* open \PIPE\lsarpc and open policy handle */
6957 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6958 if (!NT_STATUS_IS_OK(nt_status)) {
6959 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6960 nt_errstr(nt_status)));
6961 talloc_destroy(mem_ctx);
6965 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6967 if (!NT_STATUS_IS_OK(nt_status)) {
6968 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6969 nt_errstr(nt_status) ));
6971 talloc_destroy(mem_ctx);
6975 b = pipe_hnd->binding_handle;
6977 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6979 if (NT_STATUS_IS_ERR(nt_status)) {
6980 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6981 nt_errstr(nt_status)));
6983 talloc_destroy(mem_ctx);
6987 /* query info level 5 to obtain sid of a domain being queried */
6988 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6990 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6994 if (NT_STATUS_IS_ERR(nt_status)) {
6995 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6996 nt_errstr(nt_status)));
6998 talloc_destroy(mem_ctx);
7001 if (NT_STATUS_IS_ERR(result)) {
7002 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7003 nt_errstr(result)));
7005 talloc_destroy(mem_ctx);
7009 queried_dom_sid = info->account_domain.sid;
7012 * Keep calling LsaEnumTrustdom over opened pipe until
7013 * the end of enumeration is reached
7016 d_printf(_("Trusted domains list:\n\n"));
7018 found_domain = false;
7021 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
7027 if (NT_STATUS_IS_ERR(nt_status)) {
7028 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7029 nt_errstr(nt_status)));
7031 talloc_destroy(mem_ctx);
7034 if (NT_STATUS_IS_ERR(result)) {
7035 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7036 nt_errstr(result)));
7038 talloc_destroy(mem_ctx);
7043 for (i = 0; i < dom_list.count; i++) {
7044 print_trusted_domain(dom_list.domains[i].sid,
7045 dom_list.domains[i].name.string);
7046 found_domain = true;
7050 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7053 * in case of no trusted domains say something rather
7054 * than just display blank line
7056 if (!found_domain) {
7057 d_printf(_("none\n"));
7060 /* close this connection before doing next one */
7061 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7062 if (NT_STATUS_IS_ERR(nt_status)) {
7063 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7064 nt_errstr(nt_status)));
7066 talloc_destroy(mem_ctx);
7070 TALLOC_FREE(pipe_hnd);
7073 * Listing trusting domains (stored in passdb backend, if local)
7076 d_printf(_("\nTrusting domains list:\n\n"));
7079 * Open \PIPE\samr and get needed policy handles
7081 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
7083 if (!NT_STATUS_IS_OK(nt_status)) {
7084 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
7086 talloc_destroy(mem_ctx);
7090 b = pipe_hnd->binding_handle;
7093 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
7095 SAMR_ACCESS_LOOKUP_DOMAIN,
7098 if (!NT_STATUS_IS_OK(nt_status)) {
7099 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7100 nt_errstr(nt_status)));
7102 talloc_destroy(mem_ctx);
7105 if (!NT_STATUS_IS_OK(result)) {
7107 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7108 nt_errstr(result)));
7110 talloc_destroy(mem_ctx);
7114 /* SamrOpenDomain - we have to open domain policy handle in order to be
7115 able to enumerate accounts*/
7116 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
7118 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
7122 if (!NT_STATUS_IS_OK(nt_status)) {
7123 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7124 nt_errstr(nt_status)));
7126 talloc_destroy(mem_ctx);
7129 if (!NT_STATUS_IS_OK(result)) {
7131 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7132 nt_errstr(result)));
7134 talloc_destroy(mem_ctx);
7139 * perform actual enumeration
7142 found_domain = false;
7144 enum_ctx = 0; /* reset enumeration context from last enumeration */
7147 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
7155 if (NT_STATUS_IS_ERR(nt_status)) {
7156 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7157 nt_errstr(nt_status)));
7159 talloc_destroy(mem_ctx);
7162 if (NT_STATUS_IS_ERR(result)) {
7164 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7165 nt_errstr(result)));
7167 talloc_destroy(mem_ctx);
7171 for (i = 0; i < num_domains; i++) {
7173 char *str = discard_const_p(char, trusts->entries[i].name.string);
7175 found_domain = true;
7178 * get each single domain's sid (do we _really_ need this ?):
7179 * 1) connect to domain's pdc
7180 * 2) query the pdc for domain's sid
7183 /* get rid of '$' tail */
7184 ascii_dom_name_len = strlen(str);
7185 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7186 str[ascii_dom_name_len - 1] = '\0';
7188 /* set opt_* variables to remote domain */
7189 if (!strupper_m(str)) {
7191 talloc_destroy(mem_ctx);
7194 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7195 c->opt_target_workgroup = c->opt_workgroup;
7197 d_printf("%-20s", str);
7199 /* connect to remote domain controller */
7200 nt_status = net_make_ipc_connection(c,
7201 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7203 if (NT_STATUS_IS_OK(nt_status)) {
7204 /* query for domain's sid */
7205 if (run_rpc_command(
7207 &ndr_table_lsarpc, 0,
7208 rpc_query_domain_sid, argc,
7210 d_printf(_("strange - couldn't get domain's sid\n"));
7212 cli_shutdown(remote_cli);
7215 d_fprintf(stderr, _("domain controller is not "
7216 "responding: %s\n"),
7217 nt_errstr(nt_status));
7218 d_printf(_("couldn't get domain's sid\n"));
7222 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7224 if (!found_domain) {
7228 /* close opened samr and domain policy handles */
7229 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7230 if (!NT_STATUS_IS_OK(nt_status)) {
7231 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7234 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7235 if (!NT_STATUS_IS_OK(nt_status)) {
7236 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7239 /* close samr pipe and connection to IPC$ */
7242 talloc_destroy(mem_ctx);
7247 * Entrypoint for 'net rpc trustdom' code.
7249 * @param argc Standard argc.
7250 * @param argv Standard argv without initial components.
7252 * @return Integer status (0 means success).
7255 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7257 struct functable func[] = {
7262 N_("Add trusting domain's account"),
7263 N_("net rpc trustdom add\n"
7264 " Add trusting domain's account")
7270 N_("Remove trusting domain's account"),
7271 N_("net rpc trustdom del\n"
7272 " Remove trusting domain's account")
7276 rpc_trustdom_establish,
7278 N_("Establish outgoing trust relationship"),
7279 N_("net rpc trustdom establish\n"
7280 " Establish outgoing trust relationship")
7284 rpc_trustdom_revoke,
7286 N_("Revoke outgoing trust relationship"),
7287 N_("net rpc trustdom revoke\n"
7288 " Revoke outgoing trust relationship")
7294 N_("List in- and outgoing domain trusts"),
7295 N_("net rpc trustdom list\n"
7296 " List in- and outgoing domain trusts")
7300 rpc_trustdom_vampire,
7302 N_("Vampire trusts from remote server"),
7303 N_("net rpc trustdom vampire\n"
7304 " Vampire trusts from remote server")
7306 {NULL, NULL, 0, NULL, NULL}
7309 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7313 * Check if a server will take rpc commands
7314 * @param flags Type of server to connect to (PDC, DMB, localhost)
7315 * if the host is not explicitly specified
7316 * @return bool (true means rpc supported)
7318 bool net_rpc_check(struct net_context *c, unsigned flags)
7320 struct cli_state *cli;
7322 struct sockaddr_storage server_ss;
7323 char *server_name = NULL;
7326 /* flags (i.e. server type) may depend on command */
7327 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7330 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7331 lp_netbios_name(), SMB_SIGNING_DEFAULT,
7333 if (!NT_STATUS_IS_OK(status)) {
7336 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7338 if (!NT_STATUS_IS_OK(status))
7340 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7349 /* dump sam database via samsync rpc calls */
7350 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7351 if (c->display_usage) {
7356 _("Dump remote SAM database"));
7360 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7361 NET_FLAGS_ANONYMOUS,
7362 rpc_samdump_internals, argc, argv);
7365 /* syncronise sam database via samsync rpc calls */
7366 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7368 struct functable func[] = {
7373 N_("Dump remote SAM database to ldif"),
7374 N_("net rpc vampire ldif\n"
7375 " Dump remote SAM database to LDIF file or "
7382 N_("Dump remote SAM database to Kerberos Keytab"),
7383 N_("net rpc vampire keytab\n"
7384 " Dump remote SAM database to Kerberos keytab "
7391 N_("Dump remote SAM database to passdb"),
7392 N_("net rpc vampire passdb\n"
7393 " Dump remote SAM database to passdb")
7396 {NULL, NULL, 0, NULL, NULL}
7400 if (c->display_usage) {
7405 _("Vampire remote SAM database"));
7409 return rpc_vampire_passdb(c, argc, argv);
7412 return net_run_function(c, argc, argv, "net rpc vampire", func);
7416 * Migrate everything from a print server.
7418 * @param c A net_context structure.
7419 * @param argc Standard main() style argc.
7420 * @param argv Standard main() style argv. Initial components are already
7423 * @return A shell status integer (0 for success).
7425 * The order is important !
7426 * To successfully add drivers the print queues have to exist !
7427 * Applying ACLs should be the last step, because you're easily locked out.
7430 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7435 if (c->display_usage) {
7437 "net rpc printer migrate all\n"
7440 _("Migrate everything from a print server"));
7445 d_printf(_("no server to migrate\n"));
7449 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7450 rpc_printer_migrate_printers_internals, argc,
7455 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7456 rpc_printer_migrate_drivers_internals, argc,
7461 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7462 rpc_printer_migrate_forms_internals, argc, argv);
7466 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7467 rpc_printer_migrate_settings_internals, argc,
7472 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7473 rpc_printer_migrate_security_internals, argc,
7479 * Migrate print drivers from a print server.
7481 * @param c A net_context structure.
7482 * @param argc Standard main() style argc.
7483 * @param argv Standard main() style argv. Initial components are already
7486 * @return A shell status integer (0 for success).
7488 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7491 if (c->display_usage) {
7493 "net rpc printer migrate drivers\n"
7496 _("Migrate print-drivers from a print-server"));
7501 d_printf(_("no server to migrate\n"));
7505 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7506 rpc_printer_migrate_drivers_internals,
7511 * Migrate print-forms from a print-server.
7513 * @param c A net_context structure.
7514 * @param argc Standard main() style argc.
7515 * @param argv Standard main() style argv. Initial components are already
7518 * @return A shell status integer (0 for success).
7520 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7523 if (c->display_usage) {
7525 "net rpc printer migrate forms\n"
7528 _("Migrate print-forms from a print-server"));
7533 d_printf(_("no server to migrate\n"));
7537 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7538 rpc_printer_migrate_forms_internals,
7543 * Migrate printers from a print-server.
7545 * @param c A net_context structure.
7546 * @param argc Standard main() style argc.
7547 * @param argv Standard main() style argv. Initial components are already
7550 * @return A shell status integer (0 for success).
7552 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7555 if (c->display_usage) {
7557 "net rpc printer migrate printers\n"
7560 _("Migrate printers from a print-server"));
7565 d_printf(_("no server to migrate\n"));
7569 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7570 rpc_printer_migrate_printers_internals,
7575 * Migrate printer-ACLs from a print-server
7577 * @param c A net_context structure.
7578 * @param argc Standard main() style argc.
7579 * @param argv Standard main() style argv. Initial components are already
7582 * @return A shell status integer (0 for success).
7584 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7587 if (c->display_usage) {
7589 "net rpc printer migrate security\n"
7592 _("Migrate printer-ACLs from a print-server"));
7597 d_printf(_("no server to migrate\n"));
7601 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7602 rpc_printer_migrate_security_internals,
7607 * Migrate printer-settings from a print-server.
7609 * @param c A net_context structure.
7610 * @param argc Standard main() style argc.
7611 * @param argv Standard main() style argv. Initial components are already
7614 * @return A shell status integer (0 for success).
7616 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7619 if (c->display_usage) {
7621 "net rpc printer migrate settings\n"
7624 _("Migrate printer-settings from a "
7630 d_printf(_("no server to migrate\n"));
7634 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7635 rpc_printer_migrate_settings_internals,
7640 * 'net rpc printer' entrypoint.
7642 * @param c A net_context structure.
7643 * @param argc Standard main() style argc.
7644 * @param argv Standard main() style argv. Initial components are already
7648 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7651 /* ouch: when addriver and setdriver are called from within
7652 rpc_printer_migrate_drivers_internals, the printer-queue already
7655 struct functable func[] = {
7658 rpc_printer_migrate_all,
7660 N_("Migrate all from remote to local print server"),
7661 N_("net rpc printer migrate all\n"
7662 " Migrate all from remote to local print server")
7666 rpc_printer_migrate_drivers,
7668 N_("Migrate drivers to local server"),
7669 N_("net rpc printer migrate drivers\n"
7670 " Migrate drivers to local server")
7674 rpc_printer_migrate_forms,
7676 N_("Migrate froms to local server"),
7677 N_("net rpc printer migrate forms\n"
7678 " Migrate froms to local server")
7682 rpc_printer_migrate_printers,
7684 N_("Migrate printers to local server"),
7685 N_("net rpc printer migrate printers\n"
7686 " Migrate printers to local server")
7690 rpc_printer_migrate_security,
7692 N_("Mirgate printer ACLs to local server"),
7693 N_("net rpc printer migrate security\n"
7694 " Mirgate printer ACLs to local server")
7698 rpc_printer_migrate_settings,
7700 N_("Migrate printer settings to local server"),
7701 N_("net rpc printer migrate settings\n"
7702 " Migrate printer settings to local server")
7704 {NULL, NULL, 0, NULL, NULL}
7707 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7712 * List printers on a remote RPC server.
7714 * @param c A net_context structure.
7715 * @param argc Standard main() style argc.
7716 * @param argv Standard main() style argv. Initial components are already
7719 * @return A shell status integer (0 for success).
7721 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7723 if (c->display_usage) {
7725 "net rpc printer list\n"
7728 _("List printers on a remote RPC server"));
7732 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7733 rpc_printer_list_internals,
7738 * List printer-drivers on a remote RPC server.
7740 * @param c A net_context structure.
7741 * @param argc Standard main() style argc.
7742 * @param argv Standard main() style argv. Initial components are already
7745 * @return A shell status integer (0 for success).
7747 static int rpc_printer_driver_list(struct net_context *c, int argc,
7750 if (c->display_usage) {
7752 "net rpc printer driver\n"
7755 _("List printer-drivers on a remote RPC server"));
7759 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7760 rpc_printer_driver_list_internals,
7765 * Publish printer in ADS via MSRPC.
7767 * @param c A net_context structure.
7768 * @param argc Standard main() style argc.
7769 * @param argv Standard main() style argv. Initial components are already
7772 * @return A shell status integer (0 for success).
7774 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7777 if (c->display_usage) {
7779 "net rpc printer publish publish\n"
7782 _("Publish printer in ADS via MSRPC"));
7786 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7787 rpc_printer_publish_publish_internals,
7792 * Update printer in ADS via MSRPC.
7794 * @param c A net_context structure.
7795 * @param argc Standard main() style argc.
7796 * @param argv Standard main() style argv. Initial components are already
7799 * @return A shell status integer (0 for success).
7801 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7803 if (c->display_usage) {
7805 "net rpc printer publish update\n"
7808 _("Update printer in ADS via MSRPC"));
7812 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7813 rpc_printer_publish_update_internals,
7818 * UnPublish printer in ADS via MSRPC.
7820 * @param c A net_context structure.
7821 * @param argc Standard main() style argc.
7822 * @param argv Standard main() style argv. Initial components are already
7825 * @return A shell status integer (0 for success).
7827 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7830 if (c->display_usage) {
7832 "net rpc printer publish unpublish\n"
7835 _("UnPublish printer in ADS via MSRPC"));
7839 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7840 rpc_printer_publish_unpublish_internals,
7845 * List published printers via MSRPC.
7847 * @param c A net_context structure.
7848 * @param argc Standard main() style argc.
7849 * @param argv Standard main() style argv. Initial components are already
7852 * @return A shell status integer (0 for success).
7854 static int rpc_printer_publish_list(struct net_context *c, int argc,
7857 if (c->display_usage) {
7859 "net rpc printer publish list\n"
7862 _("List published printers via MSRPC"));
7866 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7867 rpc_printer_publish_list_internals,
7873 * Publish printer in ADS.
7875 * @param c A net_context structure.
7876 * @param argc Standard main() style argc.
7877 * @param argv Standard main() style argv. Initial components are already
7880 * @return A shell status integer (0 for success).
7882 static int rpc_printer_publish(struct net_context *c, int argc,
7886 struct functable func[] = {
7889 rpc_printer_publish_publish,
7891 N_("Publish printer in AD"),
7892 N_("net rpc printer publish publish\n"
7893 " Publish printer in AD")
7897 rpc_printer_publish_update,
7899 N_("Update printer in AD"),
7900 N_("net rpc printer publish update\n"
7901 " Update printer in AD")
7905 rpc_printer_publish_unpublish,
7907 N_("Unpublish printer"),
7908 N_("net rpc printer publish unpublish\n"
7909 " Unpublish printer")
7913 rpc_printer_publish_list,
7915 N_("List published printers"),
7916 N_("net rpc printer publish list\n"
7917 " List published printers")
7919 {NULL, NULL, 0, NULL, NULL}
7923 if (c->display_usage) {
7924 d_printf(_("Usage:\n"));
7925 d_printf(_("net rpc printer publish\n"
7926 " List published printers\n"
7927 " Alias of net rpc printer publish "
7929 net_display_usage_from_functable(func);
7932 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7933 rpc_printer_publish_list_internals,
7937 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7943 * Display rpc printer help page.
7945 * @param c A net_context structure.
7946 * @param argc Standard main() style argc.
7947 * @param argv Standard main() style argv. Initial components are already
7950 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7952 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7953 "\tlists all printers on print-server\n\n"));
7954 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7955 "\tlists all printer-drivers on print-server\n\n"));
7956 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7957 "\tpublishes printer settings in Active Directory\n"
7958 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7959 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7960 "\n\tmigrates printers from remote to local server\n\n"));
7961 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7962 "\n\tmigrates printer-settings from remote to local server\n\n"));
7963 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7964 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7965 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7966 "\n\tmigrates printer-forms from remote to local server\n\n"));
7967 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7968 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7969 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7970 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7971 "\tremote to local print-server\n\n"));
7972 net_common_methods_usage(c, argc, argv);
7973 net_common_flags_usage(c, argc, argv);
7975 "\t-v or --verbose\t\t\tgive verbose output\n"
7976 "\t --destination\t\tmigration target server (default: localhost)\n"));
7982 * 'net rpc printer' entrypoint.
7984 * @param c A net_context structure.
7985 * @param argc Standard main() style argc.
7986 * @param argv Standard main() style argv. Initial components are already
7989 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7991 struct functable func[] = {
7996 N_("List all printers on print server"),
7997 N_("net rpc printer list\n"
7998 " List all printers on print server")
8002 rpc_printer_migrate,
8004 N_("Migrate printer to local server"),
8005 N_("net rpc printer migrate\n"
8006 " Migrate printer to local server")
8010 rpc_printer_driver_list,
8012 N_("List printer drivers"),
8013 N_("net rpc printer driver\n"
8014 " List printer drivers")
8018 rpc_printer_publish,
8020 N_("Publish printer in AD"),
8021 N_("net rpc printer publish\n"
8022 " Publish printer in AD")
8024 {NULL, NULL, 0, NULL, NULL}
8028 if (c->display_usage) {
8029 d_printf(_("Usage:\n"));
8030 d_printf(_("net rpc printer\n"
8031 " List printers\n"));
8032 net_display_usage_from_functable(func);
8035 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8036 rpc_printer_list_internals,
8040 return net_run_function(c, argc, argv, "net rpc printer", func);
8044 * 'net rpc' entrypoint.
8046 * @param c A net_context structure.
8047 * @param argc Standard main() style argc.
8048 * @param argv Standard main() style argv. Initial components are already
8052 int net_rpc(struct net_context *c, int argc, const char **argv)
8054 NET_API_STATUS status;
8056 struct functable func[] = {
8061 N_("Modify global audit settings"),
8062 N_("net rpc audit\n"
8063 " Modify global audit settings")
8069 N_("Show basic info about a domain"),
8071 " Show basic info about a domain")
8077 N_("Join a domain"),
8085 N_("Join a domain created in server manager"),
8086 N_("net rpc oldjoin\n"
8087 " Join a domain created in server manager")
8093 N_("Test that a join is valid"),
8094 N_("net rpc testjoin\n"
8095 " Test that a join is valid")
8101 N_("List/modify users"),
8103 " List/modify users")
8109 N_("Change a user password"),
8110 N_("net rpc password\n"
8111 " Change a user password\n"
8112 " Alias for net rpc user password")
8118 N_("List/modify groups"),
8119 N_("net rpc group\n"
8120 " List/modify groups")
8126 N_("List/modify shares"),
8127 N_("net rpc share\n"
8128 " List/modify shares")
8134 N_("List open files"),
8142 N_("List/modify printers"),
8143 N_("net rpc printer\n"
8144 " List/modify printers")
8148 net_rpc_changetrustpw,
8150 N_("Change trust account password"),
8151 N_("net rpc changetrustpw\n"
8152 " Change trust account password")
8158 N_("Modify domain trusts"),
8159 N_("net rpc trustdom\n"
8160 " Modify domain trusts")
8166 N_("Abort a remote shutdown"),
8167 N_("net rpc abortshutdown\n"
8168 " Abort a remote shutdown")
8174 N_("Shutdown a remote server"),
8175 N_("net rpc shutdown\n"
8176 " Shutdown a remote server")
8182 N_("Dump SAM data of remote NT PDC"),
8183 N_("net rpc samdump\n"
8184 " Dump SAM data of remote NT PDC")
8190 N_("Sync a remote NT PDC's data into local passdb"),
8191 N_("net rpc vampire\n"
8192 " Sync a remote NT PDC's data into local passdb")
8198 N_("Fetch the domain sid into local secrets.tdb"),
8199 N_("net rpc getsid\n"
8200 " Fetch the domain sid into local secrets.tdb")
8206 N_("Manage privileges assigned to SID"),
8207 N_("net rpc rights\n"
8208 " Manage privileges assigned to SID")
8214 N_("Start/stop/query remote services"),
8215 N_("net rpc service\n"
8216 " Start/stop/query remote services")
8222 N_("Manage registry hives"),
8223 N_("net rpc registry\n"
8224 " Manage registry hives")
8230 N_("Open interactive shell on remote server"),
8231 N_("net rpc shell\n"
8232 " Open interactive shell on remote server")
8238 N_("Manage trusts"),
8239 N_("net rpc trust\n"
8246 N_("Configure a remote samba server"),
8248 " Configure a remote samba server")
8250 {NULL, NULL, 0, NULL, NULL}
8253 status = libnetapi_net_init(&c->netapi_ctx);
8257 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8258 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8259 if (c->opt_kerberos) {
8260 libnetapi_set_use_kerberos(c->netapi_ctx);
8262 if (c->opt_ccache) {
8263 libnetapi_set_use_ccache(c->netapi_ctx);
8266 return net_run_function(c, argc, argv, "net rpc", func);