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, rpc_pipe_np_smb_conn(pipe_hnd),
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 TALLOC_CTX *mem_ctx,
2280 const struct dom_sid *alias_sid,
2283 struct policy_handle connect_pol, domain_pol;
2284 NTSTATUS status, result;
2286 struct policy_handle alias_pol;
2287 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2289 struct dom_sid member_sid;
2290 enum lsa_SidType member_type;
2294 sid_copy(&sid, alias_sid);
2296 if (!sid_split_rid(&sid, &alias_rid)) {
2297 return NT_STATUS_UNSUCCESSFUL;
2300 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2301 member, &member_sid, &member_type);
2303 if (!NT_STATUS_IS_OK(result)) {
2304 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2309 /* Get sam policy handle */
2310 status = dcerpc_samr_Connect2(b, mem_ctx,
2312 MAXIMUM_ALLOWED_ACCESS,
2315 if (!NT_STATUS_IS_OK(status)) {
2318 if (!NT_STATUS_IS_OK(result)) {
2323 /* Get domain policy handle */
2324 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2326 MAXIMUM_ALLOWED_ACCESS,
2330 if (!NT_STATUS_IS_OK(status)) {
2333 if (!NT_STATUS_IS_OK(result)) {
2338 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2340 MAXIMUM_ALLOWED_ACCESS,
2344 if (!NT_STATUS_IS_OK(status)) {
2347 if (!NT_STATUS_IS_OK(result)) {
2351 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2355 if (!NT_STATUS_IS_OK(status)) {
2362 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2366 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2367 const struct dom_sid *domain_sid,
2368 const char *domain_name,
2369 struct cli_state *cli,
2370 struct rpc_pipe_client *pipe_hnd,
2371 TALLOC_CTX *mem_ctx,
2375 struct dom_sid group_sid;
2376 enum lsa_SidType group_type;
2378 if (argc != 2 || c->display_usage) {
2381 _("net rpc group addmem <group> <member>\n"
2382 " Add a member to a group\n"
2383 " group\tGroup to add member to\n"
2384 " member\tMember to add to group\n"));
2385 return NT_STATUS_UNSUCCESSFUL;
2388 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2389 &group_sid, &group_type))) {
2390 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2392 return NT_STATUS_UNSUCCESSFUL;
2395 if (group_type == SID_NAME_DOM_GRP) {
2396 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2397 &group_sid, argv[1]);
2399 if (!NT_STATUS_IS_OK(result)) {
2400 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2401 argv[1], argv[0], nt_errstr(result));
2406 if (group_type == SID_NAME_ALIAS) {
2407 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2408 &group_sid, argv[1]);
2410 if (!NT_STATUS_IS_OK(result)) {
2411 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2412 argv[1], argv[0], nt_errstr(result));
2417 d_fprintf(stderr, _("Can only add members to global or local groups "
2418 "which %s is not\n"), argv[0]);
2420 return NT_STATUS_UNSUCCESSFUL;
2423 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2425 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2426 rpc_group_addmem_internals,
2430 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2431 struct rpc_pipe_client *pipe_hnd,
2432 TALLOC_CTX *mem_ctx,
2433 const struct dom_sid *group_sid,
2436 struct policy_handle connect_pol, domain_pol;
2437 NTSTATUS status, result;
2439 struct policy_handle group_pol;
2440 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2442 struct samr_Ids rids, rid_types;
2443 struct lsa_String lsa_acct_name;
2447 sid_copy(&sid, group_sid);
2449 if (!sid_split_rid(&sid, &group_rid))
2450 return NT_STATUS_UNSUCCESSFUL;
2452 /* Get sam policy handle */
2453 status = dcerpc_samr_Connect2(b, mem_ctx,
2455 MAXIMUM_ALLOWED_ACCESS,
2458 if (!NT_STATUS_IS_OK(status)) {
2461 if (!NT_STATUS_IS_OK(result)) {
2466 /* Get domain policy handle */
2467 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2469 MAXIMUM_ALLOWED_ACCESS,
2473 if (!NT_STATUS_IS_OK(status)) {
2476 if (!NT_STATUS_IS_OK(result)) {
2480 init_lsa_String(&lsa_acct_name, member);
2482 status = dcerpc_samr_LookupNames(b, mem_ctx,
2489 if (!NT_STATUS_IS_OK(status)) {
2490 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2495 if (!NT_STATUS_IS_OK(result)) {
2497 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2502 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2504 MAXIMUM_ALLOWED_ACCESS,
2508 if (!NT_STATUS_IS_OK(status)) {
2511 if (!NT_STATUS_IS_OK(result)) {
2516 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2520 if (!NT_STATUS_IS_OK(status)) {
2526 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2530 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2531 TALLOC_CTX *mem_ctx,
2532 const struct dom_sid *alias_sid,
2535 struct policy_handle connect_pol, domain_pol;
2536 NTSTATUS status, result;
2538 struct policy_handle alias_pol;
2539 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2541 struct dom_sid member_sid;
2542 enum lsa_SidType member_type;
2546 sid_copy(&sid, alias_sid);
2548 if (!sid_split_rid(&sid, &alias_rid))
2549 return NT_STATUS_UNSUCCESSFUL;
2551 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2552 member, &member_sid, &member_type);
2554 if (!NT_STATUS_IS_OK(result)) {
2555 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2560 /* Get sam policy handle */
2561 status = dcerpc_samr_Connect2(b, mem_ctx,
2563 MAXIMUM_ALLOWED_ACCESS,
2566 if (!NT_STATUS_IS_OK(status)) {
2569 if (!NT_STATUS_IS_OK(result)) {
2574 /* Get domain policy handle */
2575 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2577 MAXIMUM_ALLOWED_ACCESS,
2581 if (!NT_STATUS_IS_OK(status)) {
2584 if (!NT_STATUS_IS_OK(result)) {
2589 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2591 MAXIMUM_ALLOWED_ACCESS,
2595 if (!NT_STATUS_IS_OK(status)) {
2599 if (!NT_STATUS_IS_OK(result)) {
2603 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2608 if (!NT_STATUS_IS_OK(status)) {
2615 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2619 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2620 const struct dom_sid *domain_sid,
2621 const char *domain_name,
2622 struct cli_state *cli,
2623 struct rpc_pipe_client *pipe_hnd,
2624 TALLOC_CTX *mem_ctx,
2628 struct dom_sid group_sid;
2629 enum lsa_SidType group_type;
2631 if (argc != 2 || c->display_usage) {
2634 _("net rpc group delmem <group> <member>\n"
2635 " Delete a member from a group\n"
2636 " group\tGroup to delete member from\n"
2637 " member\tMember to delete from group\n"));
2638 return NT_STATUS_UNSUCCESSFUL;
2641 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2642 &group_sid, &group_type))) {
2643 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2645 return NT_STATUS_UNSUCCESSFUL;
2648 if (group_type == SID_NAME_DOM_GRP) {
2649 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2650 &group_sid, argv[1]);
2652 if (!NT_STATUS_IS_OK(result)) {
2653 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2654 argv[1], argv[0], nt_errstr(result));
2659 if (group_type == SID_NAME_ALIAS) {
2660 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2661 &group_sid, argv[1]);
2663 if (!NT_STATUS_IS_OK(result)) {
2664 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2665 argv[1], argv[0], nt_errstr(result));
2670 d_fprintf(stderr, _("Can only delete members from global or local "
2671 "groups which %s is not\n"), argv[0]);
2673 return NT_STATUS_UNSUCCESSFUL;
2676 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2678 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2679 rpc_group_delmem_internals,
2684 * List groups on a remote RPC server.
2686 * All parameters are provided by the run_rpc_command function, except for
2687 * argc, argv which are passes through.
2689 * @param domain_sid The domain sid acquired from the remote server.
2690 * @param cli A cli_state connected to the server.
2691 * @param mem_ctx Talloc context, destroyed on completion of the function.
2692 * @param argc Standard main() style argc.
2693 * @param argv Standard main() style argv. Initial components are already
2696 * @return Normal NTSTATUS return.
2699 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2700 const struct dom_sid *domain_sid,
2701 const char *domain_name,
2702 struct cli_state *cli,
2703 struct rpc_pipe_client *pipe_hnd,
2704 TALLOC_CTX *mem_ctx,
2708 struct policy_handle connect_pol, domain_pol;
2709 NTSTATUS status, result;
2710 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2711 struct samr_SamArray *groups = NULL;
2712 bool global = false;
2714 bool builtin = false;
2715 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2717 if (c->display_usage) {
2720 _("net rpc group list [global] [local] [builtin]\n"
2721 " List groups on RPC server\n"
2722 " global\tList global groups\n"
2723 " local\tList local groups\n"
2724 " builtin\tList builtin groups\n"
2725 " If none of global, local or builtin is "
2726 "specified, all three options are considered "
2728 return NT_STATUS_OK;
2737 for (i=0; i<argc; i++) {
2738 if (strequal(argv[i], "global"))
2741 if (strequal(argv[i], "local"))
2744 if (strequal(argv[i], "builtin"))
2748 /* Get sam policy handle */
2750 status = dcerpc_samr_Connect2(b, mem_ctx,
2752 MAXIMUM_ALLOWED_ACCESS,
2755 if (!NT_STATUS_IS_OK(status)) {
2758 if (!NT_STATUS_IS_OK(result)) {
2763 /* Get domain policy handle */
2765 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2767 MAXIMUM_ALLOWED_ACCESS,
2768 discard_const_p(struct dom_sid2, domain_sid),
2771 if (!NT_STATUS_IS_OK(status)) {
2774 if (!NT_STATUS_IS_OK(result)) {
2779 /* Query domain groups */
2780 if (c->opt_long_list_entries)
2781 d_printf(_("\nGroup name Comment"
2782 "\n-----------------------------\n"));
2784 uint32_t max_size, total_size, returned_size;
2785 union samr_DispInfo info;
2789 dcerpc_get_query_dispinfo_params(
2790 loop_count, &max_entries, &max_size);
2792 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2802 if (!NT_STATUS_IS_OK(status)) {
2805 num_entries = info.info3.count;
2806 start_idx += info.info3.count;
2808 if (!NT_STATUS_IS_OK(result) &&
2809 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2812 for (i = 0; i < num_entries; i++) {
2814 const char *group = NULL;
2815 const char *desc = NULL;
2817 group = info.info3.entries[i].account_name.string;
2818 desc = info.info3.entries[i].description.string;
2820 if (c->opt_long_list_entries)
2821 printf("%-21.21s %-50.50s\n",
2824 printf("%s\n", group);
2826 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2827 /* query domain aliases */
2832 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2839 if (!NT_STATUS_IS_OK(status)) {
2842 if (!NT_STATUS_IS_OK(result) &&
2843 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2846 for (i = 0; i < num_entries; i++) {
2848 const char *description = NULL;
2850 if (c->opt_long_list_entries) {
2852 struct policy_handle alias_pol;
2853 union samr_AliasInfo *info = NULL;
2856 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2859 groups->entries[i].idx,
2862 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2863 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2868 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2869 status = dcerpc_samr_Close(b, mem_ctx,
2872 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2873 description = info->description.string;
2879 if (description != NULL) {
2880 printf("%-21.21s %-50.50s\n",
2881 groups->entries[i].name.string,
2884 printf("%s\n", groups->entries[i].name.string);
2887 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2888 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2889 /* Get builtin policy handle */
2891 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2893 MAXIMUM_ALLOWED_ACCESS,
2894 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2897 if (!NT_STATUS_IS_OK(status)) {
2900 if (!NT_STATUS_IS_OK(result)) {
2905 /* query builtin aliases */
2908 if (!builtin) break;
2910 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2917 if (!NT_STATUS_IS_OK(status)) {
2920 if (!NT_STATUS_IS_OK(result) &&
2921 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2926 for (i = 0; i < num_entries; i++) {
2928 const char *description = NULL;
2930 if (c->opt_long_list_entries) {
2932 struct policy_handle alias_pol;
2933 union samr_AliasInfo *info = NULL;
2936 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2939 groups->entries[i].idx,
2942 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2943 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2948 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2949 status = dcerpc_samr_Close(b, mem_ctx,
2952 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2953 description = info->description.string;
2959 if (description != NULL) {
2960 printf("%-21.21s %-50.50s\n",
2961 groups->entries[i].name.string,
2964 printf("%s\n", groups->entries[i].name.string);
2967 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2975 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2977 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2978 rpc_group_list_internals,
2982 static NTSTATUS rpc_list_group_members(struct net_context *c,
2983 struct rpc_pipe_client *pipe_hnd,
2984 TALLOC_CTX *mem_ctx,
2985 const char *domain_name,
2986 const struct dom_sid *domain_sid,
2987 struct policy_handle *domain_pol,
2990 NTSTATUS result, status;
2991 struct policy_handle group_pol;
2992 uint32 num_members, *group_rids;
2994 struct samr_RidAttrArray *rids = NULL;
2995 struct lsa_Strings names;
2996 struct samr_Ids types;
2997 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3000 sid_to_fstring(sid_str, domain_sid);
3002 status = dcerpc_samr_OpenGroup(b, mem_ctx,
3004 MAXIMUM_ALLOWED_ACCESS,
3008 if (!NT_STATUS_IS_OK(status)) {
3011 if (!NT_STATUS_IS_OK(result)) {
3015 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
3019 if (!NT_STATUS_IS_OK(status)) {
3022 if (!NT_STATUS_IS_OK(result)) {
3026 num_members = rids->count;
3027 group_rids = rids->rids;
3029 while (num_members > 0) {
3030 int this_time = 512;
3032 if (num_members < this_time)
3033 this_time = num_members;
3035 status = dcerpc_samr_LookupRids(b, mem_ctx,
3042 if (!NT_STATUS_IS_OK(status)) {
3045 if (!NT_STATUS_IS_OK(result)) {
3049 /* We only have users as members, but make the output
3050 the same as the output of alias members */
3052 for (i = 0; i < this_time; i++) {
3054 if (c->opt_long_list_entries) {
3055 printf("%s-%d %s\\%s %d\n", sid_str,
3056 group_rids[i], domain_name,
3057 names.names[i].string,
3060 printf("%s\\%s\n", domain_name,
3061 names.names[i].string);
3065 num_members -= this_time;
3069 return NT_STATUS_OK;
3072 static NTSTATUS rpc_list_alias_members(struct net_context *c,
3073 struct rpc_pipe_client *pipe_hnd,
3074 TALLOC_CTX *mem_ctx,
3075 struct policy_handle *domain_pol,
3078 NTSTATUS result, status;
3079 struct rpc_pipe_client *lsa_pipe;
3080 struct policy_handle alias_pol, lsa_pol;
3082 struct dom_sid *alias_sids;
3085 enum lsa_SidType *types;
3087 struct lsa_SidArray sid_array;
3088 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3090 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3092 MAXIMUM_ALLOWED_ACCESS,
3096 if (!NT_STATUS_IS_OK(status)) {
3099 if (!NT_STATUS_IS_OK(result)) {
3103 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
3107 if (!NT_STATUS_IS_OK(status)) {
3108 d_fprintf(stderr, _("Couldn't list alias members\n"));
3111 if (!NT_STATUS_IS_OK(result)) {
3112 d_fprintf(stderr, _("Couldn't list alias members\n"));
3116 num_members = sid_array.num_sids;
3118 if (num_members == 0) {
3119 return NT_STATUS_OK;
3122 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
3125 if (!NT_STATUS_IS_OK(result)) {
3126 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
3127 nt_errstr(result) );
3131 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
3132 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
3134 if (!NT_STATUS_IS_OK(result)) {
3135 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
3136 TALLOC_FREE(lsa_pipe);
3140 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
3142 d_fprintf(stderr, _("Out of memory\n"));
3143 TALLOC_FREE(lsa_pipe);
3144 return NT_STATUS_NO_MEMORY;
3147 for (i=0; i<num_members; i++) {
3148 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
3151 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
3152 num_members, alias_sids,
3153 &domains, &names, &types);
3155 if (!NT_STATUS_IS_OK(result) &&
3156 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3157 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3158 TALLOC_FREE(lsa_pipe);
3162 for (i = 0; i < num_members; i++) {
3164 sid_to_fstring(sid_str, &alias_sids[i]);
3166 if (c->opt_long_list_entries) {
3167 printf("%s %s\\%s %d\n", sid_str,
3168 domains[i] ? domains[i] : _("*unknown*"),
3169 names[i] ? names[i] : _("*unknown*"), types[i]);
3172 printf("%s\\%s\n", domains[i], names[i]);
3174 printf("%s\n", sid_str);
3178 TALLOC_FREE(lsa_pipe);
3179 return NT_STATUS_OK;
3182 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3183 const struct dom_sid *domain_sid,
3184 const char *domain_name,
3185 struct cli_state *cli,
3186 struct rpc_pipe_client *pipe_hnd,
3187 TALLOC_CTX *mem_ctx,
3191 NTSTATUS result, status;
3192 struct policy_handle connect_pol, domain_pol;
3193 struct samr_Ids rids, rid_types;
3194 struct lsa_String lsa_acct_name;
3195 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3197 /* Get sam policy handle */
3199 status = dcerpc_samr_Connect2(b, mem_ctx,
3201 MAXIMUM_ALLOWED_ACCESS,
3204 if (!NT_STATUS_IS_OK(status)) {
3207 if (!NT_STATUS_IS_OK(result)) {
3211 /* Get domain policy handle */
3213 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3215 MAXIMUM_ALLOWED_ACCESS,
3216 discard_const_p(struct dom_sid2, domain_sid),
3219 if (!NT_STATUS_IS_OK(status)) {
3222 if (!NT_STATUS_IS_OK(result)) {
3226 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3228 status = dcerpc_samr_LookupNames(b, mem_ctx,
3235 if (!NT_STATUS_IS_OK(status)) {
3239 if (!NT_STATUS_IS_OK(result)) {
3241 /* Ok, did not find it in the global sam, try with builtin */
3243 struct dom_sid sid_Builtin;
3245 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3247 sid_copy(&sid_Builtin, &global_sid_Builtin);
3249 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3251 MAXIMUM_ALLOWED_ACCESS,
3255 if (!NT_STATUS_IS_OK(status)) {
3258 if (!NT_STATUS_IS_OK(result)) {
3259 d_fprintf(stderr, _("Couldn't find group %s\n"),
3264 status = dcerpc_samr_LookupNames(b, mem_ctx,
3271 if (!NT_STATUS_IS_OK(status)) {
3274 if (!NT_STATUS_IS_OK(result)) {
3275 d_fprintf(stderr, _("Couldn't find group %s\n"),
3281 if (rids.count != 1) {
3282 d_fprintf(stderr, _("Couldn't find group %s\n"),
3287 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3288 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3289 domain_sid, &domain_pol,
3293 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3294 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3298 return NT_STATUS_NO_SUCH_GROUP;
3301 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3303 if (argc != 1 || c->display_usage) {
3304 return rpc_group_usage(c, argc, argv);
3307 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3308 rpc_group_members_internals,
3312 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3314 NET_API_STATUS status;
3315 struct GROUP_INFO_0 g0;
3319 d_printf(_("Usage:\n"));
3320 d_printf("net rpc group rename group newname\n");
3324 g0.grpi0_name = argv[1];
3326 status = NetGroupSetInfo(c->opt_host,
3333 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3334 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3342 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3344 if (argc != 2 || c->display_usage) {
3345 return rpc_group_usage(c, argc, argv);
3348 return rpc_group_rename_internals(c, argc, argv);
3352 * 'net rpc group' entrypoint.
3353 * @param argc Standard main() style argc.
3354 * @param argv Standard main() style argv. Initial components are already
3358 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3360 NET_API_STATUS status;
3362 struct functable func[] = {
3367 N_("Create specified group"),
3368 N_("net rpc group add\n"
3369 " Create specified group")
3375 N_("Delete specified group"),
3376 N_("net rpc group delete\n"
3377 " Delete specified group")
3383 N_("Add member to group"),
3384 N_("net rpc group addmem\n"
3385 " Add member to group")
3391 N_("Remove member from group"),
3392 N_("net rpc group delmem\n"
3393 " Remove member from group")
3400 N_("net rpc group list\n"
3407 N_("List group members"),
3408 N_("net rpc group members\n"
3409 " List group members")
3416 N_("net rpc group rename\n"
3419 {NULL, NULL, 0, NULL, NULL}
3422 status = libnetapi_net_init(&c->netapi_ctx);
3426 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3427 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3428 if (c->opt_kerberos) {
3429 libnetapi_set_use_kerberos(c->netapi_ctx);
3433 if (c->display_usage) {
3434 d_printf(_("Usage:\n"));
3435 d_printf(_("net rpc group\n"
3436 " Alias for net rpc group list global "
3437 "local builtin\n"));
3438 net_display_usage_from_functable(func);
3442 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3443 rpc_group_list_internals,
3447 return net_run_function(c, argc, argv, "net rpc group", func);
3450 /****************************************************************************/
3452 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3454 return net_share_usage(c, argc, argv);
3458 * Add a share on a remote RPC server.
3460 * @param argc Standard main() style argc.
3461 * @param argv Standard main() style argv. Initial components are already
3464 * @return A shell status integer (0 for success).
3467 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3469 NET_API_STATUS status;
3472 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3473 uint32 num_users=0, perms=0;
3474 char *password=NULL; /* don't allow a share password */
3475 struct SHARE_INFO_2 i2;
3476 uint32_t parm_error = 0;
3478 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3479 return rpc_share_usage(c, argc, argv);
3482 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3486 path = strchr(sharename, '=');
3493 i2.shi2_netname = sharename;
3494 i2.shi2_type = type;
3495 i2.shi2_remark = c->opt_comment;
3496 i2.shi2_permissions = perms;
3497 i2.shi2_max_uses = c->opt_maxusers;
3498 i2.shi2_current_uses = num_users;
3499 i2.shi2_path = path;
3500 i2.shi2_passwd = password;
3502 status = NetShareAdd(c->opt_host,
3507 printf(_("NetShareAdd failed with: %s\n"),
3508 libnetapi_get_error_string(c->netapi_ctx, status));
3515 * Delete a share on a remote RPC server.
3517 * @param domain_sid The domain sid acquired from the remote server.
3518 * @param argc Standard main() style argc.
3519 * @param argv Standard main() style argv. Initial components are already
3522 * @return A shell status integer (0 for success).
3524 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3526 if (argc < 1 || c->display_usage) {
3527 return rpc_share_usage(c, argc, argv);
3530 return NetShareDel(c->opt_host, argv[0], 0);
3534 * Formatted print of share info
3536 * @param r pointer to SHARE_INFO_1 to format
3539 static void display_share_info_1(struct net_context *c,
3540 struct SHARE_INFO_1 *r)
3542 if (c->opt_long_list_entries) {
3543 d_printf("%-12s %-8.8s %-50s\n",
3545 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3548 d_printf("%s\n", r->shi1_netname);
3552 static WERROR get_share_info(struct net_context *c,
3553 struct rpc_pipe_client *pipe_hnd,
3554 TALLOC_CTX *mem_ctx,
3558 struct srvsvc_NetShareInfoCtr *info_ctr)
3562 union srvsvc_NetShareInfo info;
3563 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3565 /* no specific share requested, enumerate all */
3568 uint32_t preferred_len = 0xffffffff;
3569 uint32_t total_entries = 0;
3570 uint32_t resume_handle = 0;
3572 info_ctr->level = level;
3574 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3581 if (!NT_STATUS_IS_OK(status)) {
3582 return ntstatus_to_werror(status);
3587 /* request just one share */
3588 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3595 if (!NT_STATUS_IS_OK(status)) {
3596 result = ntstatus_to_werror(status);
3600 if (!W_ERROR_IS_OK(result)) {
3605 ZERO_STRUCTP(info_ctr);
3607 info_ctr->level = level;
3612 struct srvsvc_NetShareCtr1 *ctr1;
3614 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3615 W_ERROR_HAVE_NO_MEMORY(ctr1);
3618 ctr1->array = info.info1;
3620 info_ctr->ctr.ctr1 = ctr1;
3626 struct srvsvc_NetShareCtr2 *ctr2;
3628 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3629 W_ERROR_HAVE_NO_MEMORY(ctr2);
3632 ctr2->array = info.info2;
3634 info_ctr->ctr.ctr2 = ctr2;
3640 struct srvsvc_NetShareCtr502 *ctr502;
3642 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3643 W_ERROR_HAVE_NO_MEMORY(ctr502);
3646 ctr502->array = info.info502;
3648 info_ctr->ctr.ctr502 = ctr502;
3658 * 'net rpc share list' entrypoint.
3659 * @param argc Standard main() style argc.
3660 * @param argv Standard main() style argv. Initial components are already
3663 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3665 NET_API_STATUS status;
3666 struct SHARE_INFO_1 *i1 = NULL;
3667 uint32_t entries_read = 0;
3668 uint32_t total_entries = 0;
3669 uint32_t resume_handle = 0;
3670 uint32_t i, level = 1;
3672 if (c->display_usage) {
3674 "net rpc share list\n"
3677 _("List shares on remote server"));
3681 status = NetShareEnum(c->opt_host,
3683 (uint8_t **)(void *)&i1,
3692 /* Display results */
3694 if (c->opt_long_list_entries) {
3696 "\nEnumerating shared resources (exports) on remote server:\n\n"
3697 "\nShare name Type Description\n"
3698 "---------- ---- -----------\n"));
3700 for (i = 0; i < entries_read; i++)
3701 display_share_info_1(c, &i1[i]);
3706 static bool check_share_availability(struct cli_state *cli, const char *netname)
3710 status = cli_tree_connect(cli, netname, "A:", "", 0);
3711 if (!NT_STATUS_IS_OK(status)) {
3712 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3716 status = cli_tdis(cli);
3717 if (!NT_STATUS_IS_OK(status)) {
3718 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3725 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3726 const char *netname, uint32 type)
3728 /* only support disk shares */
3729 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3730 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3735 /* skip builtin shares */
3736 /* FIXME: should print$ be added too ? */
3737 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3738 strequal(netname,"global"))
3741 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3742 printf(_("excluding [%s]\n"), netname);
3746 return check_share_availability(cli, netname);
3750 * Migrate shares from a remote RPC server to the local RPC server.
3752 * All parameters are provided by the run_rpc_command function, except for
3753 * argc, argv which are passed through.
3755 * @param domain_sid The domain sid acquired from the remote server.
3756 * @param cli A cli_state connected to the server.
3757 * @param mem_ctx Talloc context, destroyed on completion of the function.
3758 * @param argc Standard main() style argc.
3759 * @param argv Standard main() style argv. Initial components are already
3762 * @return Normal NTSTATUS return.
3765 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3766 const struct dom_sid *domain_sid,
3767 const char *domain_name,
3768 struct cli_state *cli,
3769 struct rpc_pipe_client *pipe_hnd,
3770 TALLOC_CTX *mem_ctx,
3775 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3776 struct srvsvc_NetShareInfoCtr ctr_src;
3778 struct rpc_pipe_client *srvsvc_pipe = NULL;
3779 struct cli_state *cli_dst = NULL;
3780 uint32 level = 502; /* includes secdesc */
3781 uint32_t parm_error = 0;
3782 struct dcerpc_binding_handle *b;
3784 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3786 if (!W_ERROR_IS_OK(result))
3789 /* connect destination PI_SRVSVC */
3790 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3792 if (!NT_STATUS_IS_OK(nt_status))
3795 b = srvsvc_pipe->binding_handle;
3797 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3799 union srvsvc_NetShareInfo info;
3800 struct srvsvc_NetShareInfo502 info502 =
3801 ctr_src.ctr.ctr502->array[i];
3803 /* reset error-code */
3804 nt_status = NT_STATUS_UNSUCCESSFUL;
3806 if (!check_share_sanity(c, cli, info502.name, info502.type))
3809 /* finally add the share on the dst server */
3811 printf(_("migrating: [%s], path: %s, comment: %s, without "
3813 info502.name, info502.path, info502.comment);
3815 info.info502 = &info502;
3817 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3818 srvsvc_pipe->desthost,
3823 if (!NT_STATUS_IS_OK(nt_status)) {
3824 printf(_("cannot add share: %s\n"),
3825 nt_errstr(nt_status));
3828 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3829 printf(_(" [%s] does already exist\n"),
3834 if (!W_ERROR_IS_OK(result)) {
3835 nt_status = werror_to_ntstatus(result);
3836 printf(_("cannot add share: %s\n"),
3837 win_errstr(result));
3843 nt_status = NT_STATUS_OK;
3847 cli_shutdown(cli_dst);
3855 * Migrate shares from a RPC server to another.
3857 * @param argc Standard main() style argc.
3858 * @param argv Standard main() style argv. Initial components are already
3861 * @return A shell status integer (0 for success).
3863 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3866 if (c->display_usage) {
3868 "net rpc share migrate shares\n"
3871 _("Migrate shares to local server"));
3876 printf(_("no server to migrate\n"));
3880 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3881 rpc_share_migrate_shares_internals,
3888 * @param f file_info
3889 * @param mask current search mask
3890 * @param state arg-pointer
3893 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3894 const char *mask, void *state)
3896 static NTSTATUS nt_status;
3897 static struct copy_clistate *local_state;
3898 static fstring filename, new_mask;
3901 struct net_context *c;
3903 local_state = (struct copy_clistate *)state;
3904 nt_status = NT_STATUS_UNSUCCESSFUL;
3908 if (strequal(f->name, ".") || strequal(f->name, ".."))
3909 return NT_STATUS_OK;
3911 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3914 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3916 DEBUG(3,("got dir: %s\n", f->name));
3918 fstrcpy(dir, local_state->cwd);
3920 fstrcat(dir, f->name);
3922 switch (net_mode_share)
3924 case NET_MODE_SHARE_MIGRATE:
3925 /* create that directory */
3926 nt_status = net_copy_file(c, local_state->mem_ctx,
3927 local_state->cli_share_src,
3928 local_state->cli_share_dst,
3930 c->opt_acls? true : false,
3931 c->opt_attrs? true : false,
3932 c->opt_timestamps? true:false,
3936 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3937 return NT_STATUS_INTERNAL_ERROR;
3940 if (!NT_STATUS_IS_OK(nt_status)) {
3941 printf(_("could not handle dir %s: %s\n"),
3942 dir, nt_errstr(nt_status));
3946 /* search below that directory */
3947 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
3948 return NT_STATUS_NO_MEMORY;
3950 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
3951 return NT_STATUS_NO_MEMORY;
3954 old_dir = local_state->cwd;
3955 local_state->cwd = dir;
3956 nt_status = sync_files(local_state, new_mask);
3957 if (!NT_STATUS_IS_OK(nt_status)) {
3958 printf(_("could not handle files\n"));
3960 local_state->cwd = old_dir;
3967 fstrcpy(filename, local_state->cwd);
3968 fstrcat(filename, "\\");
3969 fstrcat(filename, f->name);
3971 DEBUG(3,("got file: %s\n", filename));
3973 switch (net_mode_share)
3975 case NET_MODE_SHARE_MIGRATE:
3976 nt_status = net_copy_file(c, local_state->mem_ctx,
3977 local_state->cli_share_src,
3978 local_state->cli_share_dst,
3980 c->opt_acls? true : false,
3981 c->opt_attrs? true : false,
3982 c->opt_timestamps? true: false,
3986 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3988 return NT_STATUS_INTERNAL_ERROR;
3991 if (!NT_STATUS_IS_OK(nt_status))
3992 printf(_("could not handle file %s: %s\n"),
3993 filename, nt_errstr(nt_status));
3998 * sync files, can be called recursivly to list files
3999 * and then call copy_fn for each file
4001 * @param cp_clistate pointer to the copy_clistate we work with
4002 * @param mask the current search mask
4004 * @return Boolean result
4006 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
4008 struct cli_state *targetcli;
4009 char *targetpath = NULL;
4012 DEBUG(3,("calling cli_list with mask: %s\n", mask));
4014 status = cli_resolve_path(talloc_tos(), "", NULL,
4015 cp_clistate->cli_share_src,
4016 mask, &targetcli, &targetpath);
4017 if (!NT_STATUS_IS_OK(status)) {
4018 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
4020 mask, nt_errstr(status));
4024 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
4025 copy_fn, cp_clistate);
4026 if (!NT_STATUS_IS_OK(status)) {
4027 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
4028 mask, nt_errstr(status));
4036 * Set the top level directory permissions before we do any further copies.
4037 * Should set up ACL inheritance.
4040 bool copy_top_level_perms(struct net_context *c,
4041 struct copy_clistate *cp_clistate,
4042 const char *sharename)
4044 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4046 switch (net_mode_share) {
4047 case NET_MODE_SHARE_MIGRATE:
4048 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
4049 nt_status = net_copy_fileattr(c,
4050 cp_clistate->mem_ctx,
4051 cp_clistate->cli_share_src,
4052 cp_clistate->cli_share_dst,
4054 c->opt_acls? true : false,
4055 c->opt_attrs? true : false,
4056 c->opt_timestamps? true: false,
4060 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4064 if (!NT_STATUS_IS_OK(nt_status)) {
4065 printf(_("Could handle directory attributes for top level "
4066 "directory of share %s. Error %s\n"),
4067 sharename, nt_errstr(nt_status));
4075 * Sync all files inside a remote share to another share (over smb).
4077 * All parameters are provided by the run_rpc_command function, except for
4078 * argc, argv which are passed through.
4080 * @param domain_sid The domain sid acquired from the remote server.
4081 * @param cli A cli_state connected to the server.
4082 * @param mem_ctx Talloc context, destroyed on completion of the function.
4083 * @param argc Standard main() style argc.
4084 * @param argv Standard main() style argv. Initial components are already
4087 * @return Normal NTSTATUS return.
4090 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
4091 const struct dom_sid *domain_sid,
4092 const char *domain_name,
4093 struct cli_state *cli,
4094 struct rpc_pipe_client *pipe_hnd,
4095 TALLOC_CTX *mem_ctx,
4100 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4101 struct srvsvc_NetShareInfoCtr ctr_src;
4104 struct copy_clistate cp_clistate;
4105 bool got_src_share = false;
4106 bool got_dst_share = false;
4107 const char *mask = "\\*";
4110 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
4112 nt_status = NT_STATUS_NO_MEMORY;
4116 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4119 if (!W_ERROR_IS_OK(result))
4122 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4124 struct srvsvc_NetShareInfo502 info502 =
4125 ctr_src.ctr.ctr502->array[i];
4127 if (!check_share_sanity(c, cli, info502.name, info502.type))
4130 /* one might not want to mirror whole discs :) */
4131 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
4132 d_printf(_("skipping [%s]: builtin/hidden share\n"),
4137 switch (net_mode_share)
4139 case NET_MODE_SHARE_MIGRATE:
4143 d_fprintf(stderr, _("Unsupported mode %d\n"),
4147 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4150 c->opt_acls ? _("including") : _("without"),
4151 c->opt_attrs ? _("including") : _("without"),
4152 c->opt_timestamps ? _("(preserving timestamps)") : "");
4154 cp_clistate.mem_ctx = mem_ctx;
4155 cp_clistate.cli_share_src = NULL;
4156 cp_clistate.cli_share_dst = NULL;
4157 cp_clistate.cwd = NULL;
4158 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4161 /* open share source */
4162 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4163 smbXcli_conn_remote_sockaddr(cli->conn),
4164 smbXcli_conn_remote_name(cli->conn),
4165 info502.name, "A:");
4166 if (!NT_STATUS_IS_OK(nt_status))
4169 got_src_share = true;
4171 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4172 /* open share destination */
4173 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4174 NULL, dst, info502.name, "A:");
4175 if (!NT_STATUS_IS_OK(nt_status))
4178 got_dst_share = true;
4181 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4182 d_fprintf(stderr, _("Could not handle the top level "
4183 "directory permissions for the "
4184 "share: %s\n"), info502.name);
4185 nt_status = NT_STATUS_UNSUCCESSFUL;
4189 nt_status = sync_files(&cp_clistate, mask);
4190 if (!NT_STATUS_IS_OK(nt_status)) {
4191 d_fprintf(stderr, _("could not handle files for share: "
4192 "%s\n"), info502.name);
4197 nt_status = NT_STATUS_OK;
4202 cli_shutdown(cp_clistate.cli_share_src);
4205 cli_shutdown(cp_clistate.cli_share_dst);
4212 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4214 if (c->display_usage) {
4216 "net share migrate files\n"
4219 _("Migrate files to local server"));
4224 d_printf(_("no server to migrate\n"));
4228 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4229 rpc_share_migrate_files_internals,
4234 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4236 * All parameters are provided by the run_rpc_command function, except for
4237 * argc, argv which are passed through.
4239 * @param domain_sid The domain sid acquired from the remote server.
4240 * @param cli A cli_state connected to the server.
4241 * @param mem_ctx Talloc context, destroyed on completion of the function.
4242 * @param argc Standard main() style argc.
4243 * @param argv Standard main() style argv. Initial components are already
4246 * @return Normal NTSTATUS return.
4249 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4250 const struct dom_sid *domain_sid,
4251 const char *domain_name,
4252 struct cli_state *cli,
4253 struct rpc_pipe_client *pipe_hnd,
4254 TALLOC_CTX *mem_ctx,
4259 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4260 struct srvsvc_NetShareInfoCtr ctr_src;
4261 union srvsvc_NetShareInfo info;
4263 struct rpc_pipe_client *srvsvc_pipe = NULL;
4264 struct cli_state *cli_dst = NULL;
4265 uint32 level = 502; /* includes secdesc */
4266 uint32_t parm_error = 0;
4267 struct dcerpc_binding_handle *b;
4269 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4272 if (!W_ERROR_IS_OK(result))
4275 /* connect destination PI_SRVSVC */
4276 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4278 if (!NT_STATUS_IS_OK(nt_status))
4281 b = srvsvc_pipe->binding_handle;
4283 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4285 struct srvsvc_NetShareInfo502 info502 =
4286 ctr_src.ctr.ctr502->array[i];
4288 /* reset error-code */
4289 nt_status = NT_STATUS_UNSUCCESSFUL;
4291 if (!check_share_sanity(c, cli, info502.name, info502.type))
4294 printf(_("migrating: [%s], path: %s, comment: %s, including "
4296 info502.name, info502.path, info502.comment);
4299 display_sec_desc(info502.sd_buf.sd);
4301 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4302 info.info502 = &info502;
4304 /* finally modify the share on the dst server */
4305 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4306 srvsvc_pipe->desthost,
4312 if (!NT_STATUS_IS_OK(nt_status)) {
4313 printf(_("cannot set share-acl: %s\n"),
4314 nt_errstr(nt_status));
4317 if (!W_ERROR_IS_OK(result)) {
4318 nt_status = werror_to_ntstatus(result);
4319 printf(_("cannot set share-acl: %s\n"),
4320 win_errstr(result));
4326 nt_status = NT_STATUS_OK;
4330 cli_shutdown(cli_dst);
4338 * Migrate share-acls from a RPC server to another.
4340 * @param argc Standard main() style argc.
4341 * @param argv Standard main() style argv. Initial components are already
4344 * @return A shell status integer (0 for success).
4346 static int rpc_share_migrate_security(struct net_context *c, int argc,
4349 if (c->display_usage) {
4351 "net rpc share migrate security\n"
4354 _("Migrate share-acls to local server"));
4359 d_printf(_("no server to migrate\n"));
4363 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4364 rpc_share_migrate_security_internals,
4369 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4370 * from one server to another.
4372 * @param argc Standard main() style argc.
4373 * @param argv Standard main() style argv. Initial components are already
4376 * @return A shell status integer (0 for success).
4379 static int rpc_share_migrate_all(struct net_context *c, int argc,
4384 if (c->display_usage) {
4386 "net rpc share migrate all\n"
4389 _("Migrates shares including all share settings"));
4394 d_printf(_("no server to migrate\n"));
4398 /* order is important. we don't want to be locked out by the share-acl
4399 * before copying files - gd */
4401 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4402 rpc_share_migrate_shares_internals, argc, argv);
4406 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4407 rpc_share_migrate_files_internals, argc, argv);
4411 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4412 rpc_share_migrate_security_internals, argc,
4418 * 'net rpc share migrate' entrypoint.
4419 * @param argc Standard main() style argc.
4420 * @param argv Standard main() style argv. Initial components are already
4423 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4426 struct functable func[] = {
4429 rpc_share_migrate_all,
4431 N_("Migrate shares from remote to local server"),
4432 N_("net rpc share migrate all\n"
4433 " Migrate shares from remote to local server")
4437 rpc_share_migrate_files,
4439 N_("Migrate files from remote to local server"),
4440 N_("net rpc share migrate files\n"
4441 " Migrate files from remote to local server")
4445 rpc_share_migrate_security,
4447 N_("Migrate share-ACLs from remote to local server"),
4448 N_("net rpc share migrate security\n"
4449 " Migrate share-ACLs from remote to local server")
4453 rpc_share_migrate_shares,
4455 N_("Migrate shares from remote to local server"),
4456 N_("net rpc share migrate shares\n"
4457 " Migrate shares from remote to local server")
4459 {NULL, NULL, 0, NULL, NULL}
4462 net_mode_share = NET_MODE_SHARE_MIGRATE;
4464 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4470 struct dom_sid *members;
4473 static int num_server_aliases;
4474 static struct full_alias *server_aliases;
4477 * Add an alias to the static list.
4479 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4481 if (server_aliases == NULL)
4482 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4484 server_aliases[num_server_aliases] = *alias;
4485 num_server_aliases += 1;
4489 * For a specific domain on the server, fetch all the aliases
4490 * and their members. Add all of them to the server_aliases.
4493 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4494 TALLOC_CTX *mem_ctx,
4495 struct policy_handle *connect_pol,
4496 const struct dom_sid *domain_sid)
4498 uint32 start_idx, max_entries, num_entries, i;
4499 struct samr_SamArray *groups = NULL;
4500 NTSTATUS result, status;
4501 struct policy_handle domain_pol;
4502 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4504 /* Get domain policy handle */
4506 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4508 MAXIMUM_ALLOWED_ACCESS,
4509 discard_const_p(struct dom_sid2, domain_sid),
4512 if (!NT_STATUS_IS_OK(status)) {
4515 if (!NT_STATUS_IS_OK(result)) {
4523 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4530 if (!NT_STATUS_IS_OK(status)) {
4533 for (i = 0; i < num_entries; i++) {
4535 struct policy_handle alias_pol;
4536 struct full_alias alias;
4537 struct lsa_SidArray sid_array;
4541 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4543 MAXIMUM_ALLOWED_ACCESS,
4544 groups->entries[i].idx,
4547 if (!NT_STATUS_IS_OK(status)) {
4550 if (!NT_STATUS_IS_OK(_result)) {
4555 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4559 if (!NT_STATUS_IS_OK(status)) {
4562 if (!NT_STATUS_IS_OK(_result)) {
4567 alias.num_members = sid_array.num_sids;
4569 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4570 if (!NT_STATUS_IS_OK(status)) {
4573 if (!NT_STATUS_IS_OK(_result)) {
4578 alias.members = NULL;
4580 if (alias.num_members > 0) {
4581 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4583 for (j = 0; j < alias.num_members; j++)
4584 sid_copy(&alias.members[j],
4585 sid_array.sids[j].sid);
4588 sid_compose(&alias.sid, domain_sid,
4589 groups->entries[i].idx);
4591 push_alias(mem_ctx, &alias);
4593 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4595 status = NT_STATUS_OK;
4598 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4604 * Dump server_aliases as names for debugging purposes.
4607 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4608 const struct dom_sid *domain_sid,
4609 const char *domain_name,
4610 struct cli_state *cli,
4611 struct rpc_pipe_client *pipe_hnd,
4612 TALLOC_CTX *mem_ctx,
4618 struct policy_handle lsa_pol;
4619 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4621 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4622 SEC_FLAG_MAXIMUM_ALLOWED,
4624 if (!NT_STATUS_IS_OK(result))
4627 for (i=0; i<num_server_aliases; i++) {
4630 enum lsa_SidType *types;
4633 struct full_alias *alias = &server_aliases[i];
4635 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4637 &domains, &names, &types);
4638 if (!NT_STATUS_IS_OK(result))
4641 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4643 if (alias->num_members == 0) {
4648 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4651 &domains, &names, &types);
4653 if (!NT_STATUS_IS_OK(result) &&
4654 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4657 for (j=0; j<alias->num_members; j++)
4658 DEBUG(1, ("%s\\%s (%d); ",
4659 domains[j] ? domains[j] : "*unknown*",
4660 names[j] ? names[j] : "*unknown*",types[j]));
4664 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4666 return NT_STATUS_OK;
4670 * Fetch a list of all server aliases and their members into
4674 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4675 const struct dom_sid *domain_sid,
4676 const char *domain_name,
4677 struct cli_state *cli,
4678 struct rpc_pipe_client *pipe_hnd,
4679 TALLOC_CTX *mem_ctx,
4683 NTSTATUS result, status;
4684 struct policy_handle connect_pol;
4685 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4687 status = dcerpc_samr_Connect2(b, mem_ctx,
4689 MAXIMUM_ALLOWED_ACCESS,
4692 if (!NT_STATUS_IS_OK(status)) {
4695 if (!NT_STATUS_IS_OK(result)) {
4700 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4701 &global_sid_Builtin);
4702 if (!NT_STATUS_IS_OK(status)) {
4706 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4709 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4714 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4716 token->num_sids = 4;
4718 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4719 d_fprintf(stderr, "malloc %s\n",_("failed"));
4720 token->num_sids = 0;
4724 token->sids[0] = *user_sid;
4725 sid_copy(&token->sids[1], &global_sid_World);
4726 sid_copy(&token->sids[2], &global_sid_Network);
4727 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4730 static void free_user_token(struct security_token *token)
4732 SAFE_FREE(token->sids);
4735 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4737 if (security_token_has_sid(token, sid))
4740 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4745 sid_copy(&token->sids[token->num_sids], sid);
4747 token->num_sids += 1;
4752 struct security_token token;
4755 static void dump_user_token(struct user_token *token)
4759 d_printf("%s\n", token->name);
4761 for (i=0; i<token->token.num_sids; i++) {
4762 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4766 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4770 for (i=0; i<alias->num_members; i++) {
4771 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4778 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4782 for (i=0; i<num_server_aliases; i++) {
4783 if (is_alias_member(&sid, &server_aliases[i]))
4784 add_sid_to_token(token, &server_aliases[i].sid);
4789 * We got a user token with all the SIDs we can know about without asking the
4790 * server directly. These are the user and domain group sids. All of these can
4791 * be members of aliases. So scan the list of aliases for each of the SIDs and
4792 * add them to the token.
4795 static void collect_alias_memberships(struct security_token *token)
4797 int num_global_sids = token->num_sids;
4800 for (i=0; i<num_global_sids; i++) {
4801 collect_sid_memberships(token, token->sids[i]);
4805 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4807 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4808 enum wbcSidType type;
4810 struct wbcDomainSid wsid;
4811 char sid_str[WBC_SID_STRING_BUFLEN];
4812 struct dom_sid user_sid;
4813 uint32_t num_groups;
4814 gid_t *groups = NULL;
4817 fstr_sprintf(full_name, "%s%c%s",
4818 domain, *lp_winbind_separator(), user);
4820 /* First let's find out the user sid */
4822 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4824 if (!WBC_ERROR_IS_OK(wbc_status)) {
4825 DEBUG(1, ("winbind could not find %s: %s\n",
4826 full_name, wbcErrorString(wbc_status)));
4830 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4832 if (type != WBC_SID_NAME_USER) {
4833 DEBUG(1, ("%s is not a user\n", full_name));
4837 if (!string_to_sid(&user_sid, sid_str)) {
4838 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4842 init_user_token(token, &user_sid);
4844 /* And now the groups winbind knows about */
4846 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4847 if (!WBC_ERROR_IS_OK(wbc_status)) {
4848 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4849 full_name, wbcErrorString(wbc_status)));
4853 for (i = 0; i < num_groups; i++) {
4854 gid_t gid = groups[i];
4858 wbc_status = wbcGidToSid(gid, &wsid);
4859 if (!WBC_ERROR_IS_OK(wbc_status)) {
4860 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4861 (unsigned int)gid, wbcErrorString(wbc_status)));
4862 wbcFreeMemory(groups);
4866 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4868 DEBUG(3, (" %s\n", sid_str));
4870 ok = string_to_sid(&sid, sid_str);
4872 DEBUG(1, ("Failed to convert string to SID\n"));
4873 wbcFreeMemory(groups);
4876 add_sid_to_token(token, &sid);
4878 wbcFreeMemory(groups);
4884 * Get a list of all user tokens we want to look at
4887 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4888 struct user_token **user_tokens)
4890 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4891 uint32_t i, num_users;
4893 struct user_token *result;
4894 TALLOC_CTX *frame = NULL;
4896 if (lp_winbind_use_default_domain() &&
4897 (c->opt_target_workgroup == NULL)) {
4898 d_fprintf(stderr, _("winbind use default domain = yes set, "
4899 "please specify a workgroup\n"));
4903 /* Send request to winbind daemon */
4905 wbc_status = wbcListUsers(NULL, &num_users, &users);
4906 if (!WBC_ERROR_IS_OK(wbc_status)) {
4907 DEBUG(1, (_("winbind could not list users: %s\n"),
4908 wbcErrorString(wbc_status)));
4912 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4914 if (result == NULL) {
4915 DEBUG(1, ("Could not malloc sid array\n"));
4916 wbcFreeMemory(users);
4920 frame = talloc_stackframe();
4921 for (i=0; i < num_users; i++) {
4922 fstring domain, user;
4925 fstrcpy(result[i].name, users[i]);
4927 p = strchr(users[i], *lp_winbind_separator());
4929 DEBUG(3, ("%s\n", users[i]));
4932 fstrcpy(domain, c->opt_target_workgroup);
4933 fstrcpy(user, users[i]);
4936 fstrcpy(domain, users[i]);
4937 if (!strupper_m(domain)) {
4938 DEBUG(1, ("strupper_m %s failed\n", domain));
4939 wbcFreeMemory(users);
4945 get_user_sids(domain, user, &(result[i].token));
4948 wbcFreeMemory(users);
4950 *num_tokens = num_users;
4951 *user_tokens = result;
4956 static bool get_user_tokens_from_file(FILE *f,
4958 struct user_token **tokens)
4960 struct user_token *token = NULL;
4965 if (fgets(line, sizeof(line)-1, f) == NULL) {
4969 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4970 line[strlen(line)-1] = '\0';
4973 if (line[0] == ' ') {
4977 if(!string_to_sid(&sid, &line[1])) {
4978 DEBUG(1,("get_user_tokens_from_file: Could "
4979 "not convert sid %s \n",&line[1]));
4983 if (token == NULL) {
4984 DEBUG(0, ("File does not begin with username"));
4988 add_sid_to_token(&token->token, &sid);
4992 /* And a new user... */
4995 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4996 if (*tokens == NULL) {
4997 DEBUG(0, ("Could not realloc tokens\n"));
5001 token = &((*tokens)[*num_tokens-1]);
5003 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
5006 token->token.num_sids = 0;
5007 token->token.sids = NULL;
5016 * Show the list of all users that have access to a share
5019 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
5020 TALLOC_CTX *mem_ctx,
5021 const char *netname,
5023 struct user_token *tokens)
5026 struct security_descriptor *share_sd = NULL;
5027 struct security_descriptor *root_sd = NULL;
5028 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
5030 union srvsvc_NetShareInfo info;
5034 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5036 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5043 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5044 DEBUG(1, ("Coult not query secdesc for share %s\n",
5049 share_sd = info.info502->sd_buf.sd;
5050 if (share_sd == NULL) {
5051 DEBUG(1, ("Got no secdesc for share %s\n",
5055 cnum = cli_state_get_tid(cli);
5057 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", "", 0))) {
5061 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
5062 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
5063 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
5066 for (i=0; i<num_tokens; i++) {
5069 if (share_sd != NULL) {
5070 status = se_access_check(share_sd, &tokens[i].token,
5073 if (!NT_STATUS_IS_OK(status)) {
5074 DEBUG(1, ("Could not check share_sd for "
5081 if (root_sd == NULL) {
5082 d_printf(" %s\n", tokens[i].name);
5086 status = se_access_check(root_sd, &tokens[i].token,
5088 if (!NT_STATUS_IS_OK(status)) {
5089 DEBUG(1, ("Could not check root_sd for user %s\n",
5093 d_printf(" %s\n", tokens[i].name);
5096 if (fnum != (uint16_t)-1)
5097 cli_close(cli, fnum);
5099 cli_state_set_tid(cli, cnum);
5105 * List shares on a remote RPC server, including the security descriptors.
5107 * All parameters are provided by the run_rpc_command function, except for
5108 * argc, argv which are passed through.
5110 * @param domain_sid The domain sid acquired from the remote server.
5111 * @param cli A cli_state connected to the server.
5112 * @param mem_ctx Talloc context, destroyed on completion of the function.
5113 * @param argc Standard main() style argc.
5114 * @param argv Standard main() style argv. Initial components are already
5117 * @return Normal NTSTATUS return.
5120 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
5121 const struct dom_sid *domain_sid,
5122 const char *domain_name,
5123 struct cli_state *cli,
5124 struct rpc_pipe_client *pipe_hnd,
5125 TALLOC_CTX *mem_ctx,
5131 NTSTATUS nt_status = NT_STATUS_OK;
5132 uint32_t total_entries = 0;
5133 uint32_t resume_handle = 0;
5134 uint32_t preferred_len = 0xffffffff;
5136 struct dcerpc_binding_handle *b = NULL;
5137 struct srvsvc_NetShareInfoCtr info_ctr;
5138 struct srvsvc_NetShareCtr1 ctr1;
5141 struct user_token *tokens = NULL;
5147 f = fopen(argv[0], "r");
5151 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
5152 return NT_STATUS_UNSUCCESSFUL;
5155 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5161 DEBUG(0, ("Could not read users from file\n"));
5162 return NT_STATUS_UNSUCCESSFUL;
5165 for (i=0; i<num_tokens; i++)
5166 collect_alias_memberships(&tokens[i].token);
5168 ZERO_STRUCT(info_ctr);
5172 info_ctr.ctr.ctr1 = &ctr1;
5174 b = pipe_hnd->binding_handle;
5176 /* Issue the NetShareEnum RPC call and retrieve the response */
5177 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5186 /* Was it successful? */
5187 if (!NT_STATUS_IS_OK(nt_status)) {
5188 /* Nope. Go clean up. */
5192 if (!W_ERROR_IS_OK(result)) {
5193 /* Nope. Go clean up. */
5194 nt_status = werror_to_ntstatus(result);
5198 if (total_entries == 0) {
5202 /* For each returned entry... */
5203 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5204 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5206 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5210 d_printf("%s\n", netname);
5212 show_userlist(pipe_hnd, mem_ctx, netname,
5213 num_tokens, tokens);
5216 for (i=0; i<num_tokens; i++) {
5217 free_user_token(&tokens[i].token);
5224 static int rpc_share_allowedusers(struct net_context *c, int argc,
5229 if (c->display_usage) {
5231 "net rpc share allowedusers\n"
5234 _("List allowed users"));
5238 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5239 rpc_aliaslist_internals,
5244 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5250 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5251 rpc_share_allowedusers_internals,
5255 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5258 struct user_token *tokens = NULL;
5262 net_usersidlist_usage(c, argc, argv);
5266 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5267 DEBUG(0, ("Could not get the user/sid list\n"));
5271 for (i=0; i<num_tokens; i++) {
5272 dump_user_token(&tokens[i]);
5273 free_user_token(&tokens[i].token);
5280 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5282 d_printf(_("net usersidlist\n"
5283 "\tprints out a list of all users the running winbind knows\n"
5284 "\tabout, together with all their SIDs. This is used as\n"
5285 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5287 net_common_flags_usage(c, argc, argv);
5292 * 'net rpc share' entrypoint.
5293 * @param argc Standard main() style argc.
5294 * @param argv Standard main() style argv. Initial components are already
5298 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5300 NET_API_STATUS status;
5302 struct functable func[] = {
5308 N_("net rpc share add\n"
5316 N_("net rpc share delete\n"
5321 rpc_share_allowedusers,
5323 N_("Modify allowed users"),
5324 N_("net rpc share allowedusers\n"
5325 " Modify allowed users")
5331 N_("Migrate share to local server"),
5332 N_("net rpc share migrate\n"
5333 " Migrate share to local server")
5340 N_("net rpc share list\n"
5343 {NULL, NULL, 0, NULL, NULL}
5346 status = libnetapi_net_init(&c->netapi_ctx);
5350 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5351 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5352 if (c->opt_kerberos) {
5353 libnetapi_set_use_kerberos(c->netapi_ctx);
5357 if (c->display_usage) {
5362 " Alias for net rpc share list\n"));
5363 net_display_usage_from_functable(func);
5367 return rpc_share_list(c, argc, argv);
5370 return net_run_function(c, argc, argv, "net rpc share", func);
5373 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5374 TALLOC_CTX *mem_ctx,
5375 struct rpc_sh_ctx *ctx,
5376 struct rpc_pipe_client *pipe_hnd,
5377 int argc, const char **argv)
5380 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5383 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5384 TALLOC_CTX *mem_ctx,
5385 struct rpc_sh_ctx *ctx,
5386 struct rpc_pipe_client *pipe_hnd,
5387 int argc, const char **argv)
5389 NET_API_STATUS status;
5390 uint32_t parm_err = 0;
5391 struct SHARE_INFO_2 i2;
5393 if ((argc < 2) || (argc > 3)) {
5394 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5396 return NT_STATUS_INVALID_PARAMETER;
5399 i2.shi2_netname = argv[0];
5400 i2.shi2_type = STYPE_DISKTREE;
5401 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5402 i2.shi2_permissions = 0;
5403 i2.shi2_max_uses = 0;
5404 i2.shi2_current_uses = 0;
5405 i2.shi2_path = argv[1];
5406 i2.shi2_passwd = NULL;
5408 status = NetShareAdd(pipe_hnd->desthost,
5413 return werror_to_ntstatus(W_ERROR(status));
5416 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5417 TALLOC_CTX *mem_ctx,
5418 struct rpc_sh_ctx *ctx,
5419 struct rpc_pipe_client *pipe_hnd,
5420 int argc, const char **argv)
5423 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5424 return NT_STATUS_INVALID_PARAMETER;
5427 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5430 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5431 TALLOC_CTX *mem_ctx,
5432 struct rpc_sh_ctx *ctx,
5433 struct rpc_pipe_client *pipe_hnd,
5434 int argc, const char **argv)
5436 union srvsvc_NetShareInfo info;
5439 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5442 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5443 return NT_STATUS_INVALID_PARAMETER;
5446 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5452 if (!NT_STATUS_IS_OK(status)) {
5453 result = ntstatus_to_werror(status);
5456 if (!W_ERROR_IS_OK(result)) {
5460 d_printf(_("Name: %s\n"), info.info2->name);
5461 d_printf(_("Comment: %s\n"), info.info2->comment);
5462 d_printf(_("Path: %s\n"), info.info2->path);
5463 d_printf(_("Password: %s\n"), info.info2->password);
5466 return werror_to_ntstatus(result);
5469 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5470 struct rpc_sh_ctx *ctx)
5472 static struct rpc_sh_cmd cmds[] = {
5474 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5475 N_("List available shares") },
5477 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5478 N_("Add a share") },
5480 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5481 N_("Delete a share") },
5483 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5484 N_("Get information about a share") },
5486 { NULL, NULL, 0, NULL, NULL }
5492 /****************************************************************************/
5494 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5496 return net_file_usage(c, argc, argv);
5500 * Close a file on a remote RPC server.
5502 * @param argc Standard main() style argc.
5503 * @param argv Standard main() style argv. Initial components are already
5506 * @return A shell status integer (0 for success).
5508 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5510 if (argc < 1 || c->display_usage) {
5511 return rpc_file_usage(c, argc, argv);
5514 return NetFileClose(c->opt_host, atoi(argv[0]));
5518 * Formatted print of open file info
5520 * @param r struct FILE_INFO_3 contents
5523 static void display_file_info_3(struct FILE_INFO_3 *r)
5525 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5526 r->fi3_id, r->fi3_username, r->fi3_permissions,
5527 r->fi3_num_locks, r->fi3_pathname);
5531 * List files for a user on a remote RPC server.
5533 * @param argc Standard main() style argc.
5534 * @param argv Standard main() style argv. Initial components are already
5537 * @return A shell status integer (0 for success)..
5540 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5542 NET_API_STATUS status;
5543 uint32 preferred_len = 0xffffffff, i;
5544 char *username=NULL;
5545 uint32_t total_entries = 0;
5546 uint32_t entries_read = 0;
5547 uint32_t resume_handle = 0;
5548 struct FILE_INFO_3 *i3 = NULL;
5550 if (c->display_usage) {
5551 return rpc_file_usage(c, argc, argv);
5554 /* if argc > 0, must be user command */
5556 username = smb_xstrdup(argv[0]);
5559 status = NetFileEnum(c->opt_host,
5563 (uint8_t **)(void *)&i3,
5573 /* Display results */
5576 "\nEnumerating open files on remote server:\n\n"
5577 "\nFileId Opened by Perms Locks Path"
5578 "\n------ --------- ----- ----- ---- \n"));
5579 for (i = 0; i < entries_read; i++) {
5580 display_file_info_3(&i3[i]);
5583 SAFE_FREE(username);
5588 * 'net rpc file' entrypoint.
5589 * @param argc Standard main() style argc.
5590 * @param argv Standard main() style argv. Initial components are already
5594 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5596 NET_API_STATUS status;
5598 struct functable func[] = {
5603 N_("Close opened file"),
5604 N_("net rpc file close\n"
5605 " Close opened file")
5611 N_("List files opened by user"),
5612 N_("net rpc file user\n"
5613 " List files opened by user")
5620 N_("Display information about opened file"),
5621 N_("net rpc file info\n"
5622 " Display information about opened file")
5625 {NULL, NULL, 0, NULL, NULL}
5628 status = libnetapi_net_init(&c->netapi_ctx);
5632 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5633 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5634 if (c->opt_kerberos) {
5635 libnetapi_set_use_kerberos(c->netapi_ctx);
5639 if (c->display_usage) {
5640 d_printf(_("Usage:\n"));
5641 d_printf(_("net rpc file\n"
5642 " List opened files\n"));
5643 net_display_usage_from_functable(func);
5647 return rpc_file_user(c, argc, argv);
5650 return net_run_function(c, argc, argv, "net rpc file", func);
5654 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5656 * All parameters are provided by the run_rpc_command function, except for
5657 * argc, argv which are passed through.
5659 * @param c A net_context structure.
5660 * @param domain_sid The domain sid acquired from the remote server.
5661 * @param cli A cli_state connected to the server.
5662 * @param mem_ctx Talloc context, destroyed on completion of the function.
5663 * @param argc Standard main() style argc.
5664 * @param argv Standard main() style argv. Initial components are already
5667 * @return Normal NTSTATUS return.
5670 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5671 const struct dom_sid *domain_sid,
5672 const char *domain_name,
5673 struct cli_state *cli,
5674 struct rpc_pipe_client *pipe_hnd,
5675 TALLOC_CTX *mem_ctx,
5679 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5681 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5683 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5684 if (!NT_STATUS_IS_OK(status)) {
5687 if (W_ERROR_IS_OK(result)) {
5688 d_printf(_("\nShutdown successfully aborted\n"));
5689 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5691 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5693 return werror_to_ntstatus(result);
5697 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5699 * All parameters are provided by the run_rpc_command function, except for
5700 * argc, argv which are passed through.
5702 * @param c A net_context structure.
5703 * @param domain_sid The domain sid acquired from the remote server.
5704 * @param cli A cli_state connected to the server.
5705 * @param mem_ctx Talloc context, destroyed on completion of the function.
5706 * @param argc Standard main() style argc.
5707 * @param argv Standard main() style argv. Initial components are already
5710 * @return Normal NTSTATUS return.
5713 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5714 const struct dom_sid *domain_sid,
5715 const char *domain_name,
5716 struct cli_state *cli,
5717 struct rpc_pipe_client *pipe_hnd,
5718 TALLOC_CTX *mem_ctx,
5722 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5724 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5726 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5728 if (!NT_STATUS_IS_OK(result)) {
5729 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5732 if (W_ERROR_IS_OK(werr)) {
5733 d_printf(_("\nShutdown successfully aborted\n"));
5734 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5736 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5738 return werror_to_ntstatus(werr);
5742 * ABORT the shutdown of a remote RPC server.
5744 * @param argc Standard main() style argc.
5745 * @param argv Standard main() style argv. Initial components are already
5748 * @return A shell status integer (0 for success).
5751 static int rpc_shutdown_abort(struct net_context *c, int argc,
5756 if (c->display_usage) {
5758 "net rpc abortshutdown\n"
5761 _("Abort a scheduled shutdown"));
5765 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5766 rpc_shutdown_abort_internals, argc, argv);
5771 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5773 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5774 rpc_reg_shutdown_abort_internals,
5779 * Shut down a remote RPC Server via initshutdown pipe.
5781 * All parameters are provided by the run_rpc_command function, except for
5782 * argc, argv which are passed through.
5784 * @param c A net_context structure.
5785 * @param domain_sid The domain sid acquired from the remote server.
5786 * @param cli A cli_state connected to the server.
5787 * @param mem_ctx Talloc context, destroyed on completion of the function.
5788 * @param argc Standard main() style argc.
5789 * @param argv Standard main() style argv. Initial components are already
5792 * @return Normal NTSTATUS return.
5795 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5796 const struct dom_sid *domain_sid,
5797 const char *domain_name,
5798 struct cli_state *cli,
5799 struct rpc_pipe_client *pipe_hnd,
5800 TALLOC_CTX *mem_ctx,
5804 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5806 const char *msg = N_("This machine will be shutdown shortly");
5807 uint32 timeout = 20;
5808 struct lsa_StringLarge msg_string;
5809 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5811 if (c->opt_comment) {
5812 msg = c->opt_comment;
5814 if (c->opt_timeout) {
5815 timeout = c->opt_timeout;
5818 msg_string.string = msg;
5820 /* create an entry */
5821 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5822 &msg_string, timeout, c->opt_force, c->opt_reboot,
5824 if (!NT_STATUS_IS_OK(status)) {
5827 if (W_ERROR_IS_OK(result)) {
5828 d_printf(_("\nShutdown of remote machine succeeded\n"));
5829 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5831 DEBUG(1,("Shutdown of remote machine failed!\n"));
5833 return werror_to_ntstatus(result);
5837 * Shut down a remote RPC Server via winreg pipe.
5839 * All parameters are provided by the run_rpc_command function, except for
5840 * argc, argv which are passed through.
5842 * @param c A net_context structure.
5843 * @param domain_sid The domain sid acquired from the remote server.
5844 * @param cli A cli_state connected to the server.
5845 * @param mem_ctx Talloc context, destroyed on completion of the function.
5846 * @param argc Standard main() style argc.
5847 * @param argv Standard main() style argv. Initial components are already
5850 * @return Normal NTSTATUS return.
5853 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5854 const struct dom_sid *domain_sid,
5855 const char *domain_name,
5856 struct cli_state *cli,
5857 struct rpc_pipe_client *pipe_hnd,
5858 TALLOC_CTX *mem_ctx,
5862 const char *msg = N_("This machine will be shutdown shortly");
5863 uint32 timeout = 20;
5864 struct lsa_StringLarge msg_string;
5867 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5869 if (c->opt_comment) {
5870 msg = c->opt_comment;
5872 msg_string.string = msg;
5874 if (c->opt_timeout) {
5875 timeout = c->opt_timeout;
5878 /* create an entry */
5879 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5880 &msg_string, timeout, c->opt_force, c->opt_reboot,
5882 if (!NT_STATUS_IS_OK(result)) {
5883 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5887 if (W_ERROR_IS_OK(werr)) {
5888 d_printf(_("\nShutdown of remote machine succeeded\n"));
5890 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5891 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5892 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5894 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5897 return werror_to_ntstatus(werr);
5901 * Shut down a remote RPC server.
5903 * @param argc Standard main() style argc.
5904 * @param argv Standard main() style argv. Initial components are already
5907 * @return A shell status integer (0 for success).
5910 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5914 if (c->display_usage) {
5916 "net rpc shutdown\n"
5919 _("Shut down a remote RPC server"));
5923 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5924 rpc_init_shutdown_internals, argc, argv);
5927 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5928 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5929 rpc_reg_shutdown_internals, argc, argv);
5935 /***************************************************************************
5936 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5937 ***************************************************************************/
5940 * Add interdomain trust account to the RPC server.
5941 * All parameters (except for argc and argv) are passed by run_rpc_command
5944 * @param c A net_context structure.
5945 * @param domain_sid The domain sid acquired from the server.
5946 * @param cli A cli_state connected to the server.
5947 * @param mem_ctx Talloc context, destroyed on completion of the function.
5948 * @param argc Standard main() style argc.
5949 * @param argv Standard main() style argv. Initial components are already
5952 * @return normal NTSTATUS return code.
5955 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5956 const struct dom_sid *domain_sid,
5957 const char *domain_name,
5958 struct cli_state *cli,
5959 struct rpc_pipe_client *pipe_hnd,
5960 TALLOC_CTX *mem_ctx,
5964 struct policy_handle connect_pol, domain_pol, user_pol;
5965 NTSTATUS status, result;
5967 struct lsa_String lsa_acct_name;
5969 uint32 acct_flags=0;
5971 uint32_t access_granted = 0;
5972 union samr_UserInfo info;
5973 unsigned int orig_timeout;
5974 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5975 DATA_BLOB session_key = data_blob_null;
5980 _(" net rpc trustdom add <domain_name> "
5981 "<trust password>\n"));
5982 return NT_STATUS_INVALID_PARAMETER;
5986 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5989 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5990 return NT_STATUS_NO_MEMORY;
5993 if (!strupper_m(acct_name)) {
5994 SAFE_FREE(acct_name);
5995 return NT_STATUS_INVALID_PARAMETER;
5998 init_lsa_String(&lsa_acct_name, acct_name);
6000 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6001 if (!NT_STATUS_IS_OK(status)) {
6002 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
6003 nt_errstr(status)));
6007 /* Get samr policy handle */
6008 status = dcerpc_samr_Connect2(b, mem_ctx,
6010 MAXIMUM_ALLOWED_ACCESS,
6013 if (!NT_STATUS_IS_OK(status)) {
6016 if (!NT_STATUS_IS_OK(result)) {
6021 /* Get domain policy handle */
6022 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6024 MAXIMUM_ALLOWED_ACCESS,
6025 discard_const_p(struct dom_sid2, domain_sid),
6028 if (!NT_STATUS_IS_OK(status)) {
6031 if (!NT_STATUS_IS_OK(result)) {
6036 /* This call can take a long time - allow the server to time out.
6037 * 35 seconds should do it. */
6039 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
6041 /* Create trusting domain's account */
6042 acb_info = ACB_NORMAL;
6043 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
6044 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
6045 SAMR_USER_ACCESS_SET_PASSWORD |
6046 SAMR_USER_ACCESS_GET_ATTRIBUTES |
6047 SAMR_USER_ACCESS_SET_ATTRIBUTES;
6049 status = dcerpc_samr_CreateUser2(b, mem_ctx,
6058 if (!NT_STATUS_IS_OK(status)) {
6061 /* And restore our original timeout. */
6062 rpccli_set_timeout(pipe_hnd, orig_timeout);
6064 if (!NT_STATUS_IS_OK(result)) {
6066 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
6067 acct_name, nt_errstr(result));
6072 struct samr_CryptPassword crypt_pwd;
6074 ZERO_STRUCT(info.info23);
6076 init_samr_CryptPassword(argv[1],
6080 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
6081 SAMR_FIELD_NT_PASSWORD_PRESENT;
6082 info.info23.info.acct_flags = ACB_DOMTRUST;
6083 info.info23.password = crypt_pwd;
6085 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
6090 if (!NT_STATUS_IS_OK(status)) {
6094 if (!NT_STATUS_IS_OK(result)) {
6096 DEBUG(0,("Could not set trust account password: %s\n",
6097 nt_errstr(result)));
6103 SAFE_FREE(acct_name);
6104 data_blob_clear_free(&session_key);
6109 * Create interdomain trust account for a remote domain.
6111 * @param argc Standard argc.
6112 * @param argv Standard argv without initial components.
6114 * @return Integer status (0 means success).
6117 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
6119 if (argc > 0 && !c->display_usage) {
6120 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6121 rpc_trustdom_add_internals, argc, argv);
6125 _("net rpc trustdom add <domain_name> <trust "
6133 * Remove interdomain trust account from the RPC server.
6134 * All parameters (except for argc and argv) are passed by run_rpc_command
6137 * @param c A net_context structure.
6138 * @param domain_sid The domain sid acquired from the server.
6139 * @param cli A cli_state connected to the server.
6140 * @param mem_ctx Talloc context, destroyed on completion of the function.
6141 * @param argc Standard main() style argc.
6142 * @param argv Standard main() style argv. Initial components are already
6145 * @return normal NTSTATUS return code.
6148 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6149 const struct dom_sid *domain_sid,
6150 const char *domain_name,
6151 struct cli_state *cli,
6152 struct rpc_pipe_client *pipe_hnd,
6153 TALLOC_CTX *mem_ctx,
6157 struct policy_handle connect_pol, domain_pol, user_pol;
6158 NTSTATUS status, result;
6160 struct dom_sid trust_acct_sid;
6161 struct samr_Ids user_rids, name_types;
6162 struct lsa_String lsa_acct_name;
6163 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6168 _(" net rpc trustdom del <domain_name>\n"));
6169 return NT_STATUS_INVALID_PARAMETER;
6173 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6175 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6177 if (acct_name == NULL)
6178 return NT_STATUS_NO_MEMORY;
6180 if (!strupper_m(acct_name)) {
6181 TALLOC_FREE(acct_name);
6182 return NT_STATUS_INVALID_PARAMETER;
6185 /* Get samr policy handle */
6186 status = dcerpc_samr_Connect2(b, mem_ctx,
6188 MAXIMUM_ALLOWED_ACCESS,
6191 if (!NT_STATUS_IS_OK(status)) {
6194 if (!NT_STATUS_IS_OK(result)) {
6199 /* Get domain policy handle */
6200 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6202 MAXIMUM_ALLOWED_ACCESS,
6203 discard_const_p(struct dom_sid2, domain_sid),
6206 if (!NT_STATUS_IS_OK(status)) {
6209 if (!NT_STATUS_IS_OK(result)) {
6214 init_lsa_String(&lsa_acct_name, acct_name);
6216 status = dcerpc_samr_LookupNames(b, mem_ctx,
6223 if (!NT_STATUS_IS_OK(status)) {
6224 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6226 acct_name, nt_errstr(status));
6229 if (!NT_STATUS_IS_OK(result)) {
6231 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6233 acct_name, nt_errstr(result) );
6237 status = dcerpc_samr_OpenUser(b, mem_ctx,
6239 MAXIMUM_ALLOWED_ACCESS,
6243 if (!NT_STATUS_IS_OK(status)) {
6244 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6246 acct_name, nt_errstr(status) );
6250 if (!NT_STATUS_IS_OK(result)) {
6252 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6254 acct_name, nt_errstr(result) );
6258 /* append the rid to the domain sid */
6259 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6263 /* remove the sid */
6265 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6269 if (!NT_STATUS_IS_OK(status)) {
6270 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6271 " on user %s failed %s\n"),
6272 acct_name, nt_errstr(status));
6275 if (!NT_STATUS_IS_OK(result)) {
6277 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6278 " on user %s failed %s\n"),
6279 acct_name, nt_errstr(result) );
6286 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6289 if (!NT_STATUS_IS_OK(status)) {
6290 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6292 acct_name, nt_errstr(status));
6296 if (!NT_STATUS_IS_OK(result)) {
6298 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6300 acct_name, nt_errstr(result) );
6304 if (!NT_STATUS_IS_OK(result)) {
6305 d_printf(_("Could not set trust account password: %s\n"),
6315 * Delete interdomain trust account for a remote domain.
6317 * @param argc Standard argc.
6318 * @param argv Standard argv without initial components.
6320 * @return Integer status (0 means success).
6323 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6325 if (argc > 0 && !c->display_usage) {
6326 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6327 rpc_trustdom_del_internals, argc, argv);
6331 _("net rpc trustdom del <domain>\n"));
6336 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6337 struct cli_state *cli,
6338 TALLOC_CTX *mem_ctx,
6339 const char *domain_name)
6341 char *dc_name = NULL;
6342 const char *buffer = NULL;
6343 struct rpc_pipe_client *netr;
6346 struct dcerpc_binding_handle *b;
6348 /* Use NetServerEnum2 */
6350 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6352 return NT_STATUS_OK;
6355 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6356 for domain %s\n", domain_name));
6358 /* Try netr_GetDcName */
6360 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
6362 if (!NT_STATUS_IS_OK(status)) {
6366 b = netr->binding_handle;
6368 status = dcerpc_netr_GetDcName(b, mem_ctx,
6375 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6379 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6380 for domain %s\n", domain_name));
6382 if (!NT_STATUS_IS_OK(status)) {
6386 return werror_to_ntstatus(result);
6390 * Establish trust relationship to a trusting domain.
6391 * Interdomain account must already be created on remote PDC.
6393 * @param c A net_context structure.
6394 * @param argc Standard argc.
6395 * @param argv Standard argv without initial components.
6397 * @return Integer status (0 means success).
6400 static int rpc_trustdom_establish(struct net_context *c, int argc,
6403 struct cli_state *cli = NULL;
6404 struct sockaddr_storage server_ss;
6405 struct rpc_pipe_client *pipe_hnd = NULL;
6406 struct policy_handle connect_hnd;
6407 TALLOC_CTX *mem_ctx;
6408 NTSTATUS nt_status, result;
6409 struct dom_sid *domain_sid;
6414 union lsa_PolicyInformation *info = NULL;
6415 struct dcerpc_binding_handle *b;
6418 * Connect to \\server\ipc$ as 'our domain' account with password
6421 if (argc != 1 || c->display_usage) {
6424 _("net rpc trustdom establish <domain_name>\n"));
6428 domain_name = smb_xstrdup(argv[0]);
6429 if (!strupper_m(domain_name)) {
6430 SAFE_FREE(domain_name);
6434 /* account name used at first is our domain's name with '$' */
6435 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6438 if (!strupper_m(acct_name)) {
6439 SAFE_FREE(domain_name);
6440 SAFE_FREE(acct_name);
6445 * opt_workgroup will be used by connection functions further,
6446 * hence it should be set to remote domain name instead of ours
6448 if (c->opt_workgroup) {
6449 c->opt_workgroup = smb_xstrdup(domain_name);
6452 c->opt_user_name = acct_name;
6454 /* find the domain controller */
6455 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6456 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6460 /* connect to ipc$ as username/password */
6461 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6462 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6464 /* Is it trusting domain account for sure ? */
6465 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6466 nt_errstr(nt_status)));
6470 /* store who we connected to */
6472 saf_store( domain_name, pdc_name );
6475 * Connect to \\server\ipc$ again (this time anonymously)
6478 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6481 if (NT_STATUS_IS_ERR(nt_status)) {
6482 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6483 domain_name, nt_errstr(nt_status)));
6487 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6488 "domain %s", domain_name))) {
6489 DEBUG(0, ("talloc_init() failed\n"));
6494 /* Make sure we're talking to a proper server */
6496 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6497 if (!NT_STATUS_IS_OK(nt_status)) {
6499 talloc_destroy(mem_ctx);
6504 * Call LsaOpenPolicy and LsaQueryInfo
6507 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6509 if (!NT_STATUS_IS_OK(nt_status)) {
6510 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6512 talloc_destroy(mem_ctx);
6516 b = pipe_hnd->binding_handle;
6518 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6520 if (NT_STATUS_IS_ERR(nt_status)) {
6521 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6522 nt_errstr(nt_status)));
6524 talloc_destroy(mem_ctx);
6528 /* Querying info level 5 */
6530 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6532 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6535 if (NT_STATUS_IS_ERR(nt_status)) {
6536 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6537 nt_errstr(nt_status)));
6539 talloc_destroy(mem_ctx);
6542 if (NT_STATUS_IS_ERR(result)) {
6543 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6544 nt_errstr(result)));
6546 talloc_destroy(mem_ctx);
6550 domain_sid = info->account_domain.sid;
6552 /* There should be actually query info level 3 (following nt serv behaviour),
6553 but I still don't know if it's _really_ necessary */
6556 * Store the password in secrets db
6559 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6560 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6562 talloc_destroy(mem_ctx);
6567 * Close the pipes and clean up
6570 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6571 if (NT_STATUS_IS_ERR(nt_status)) {
6572 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6573 nt_errstr(nt_status)));
6575 talloc_destroy(mem_ctx);
6581 talloc_destroy(mem_ctx);
6583 d_printf(_("Trust to domain %s established\n"), domain_name);
6588 * Revoke trust relationship to the remote domain.
6590 * @param c A net_context structure.
6591 * @param argc Standard argc.
6592 * @param argv Standard argv without initial components.
6594 * @return Integer status (0 means success).
6597 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6603 if (argc < 1 || c->display_usage) {
6606 _("net rpc trustdom revoke <domain_name>\n"
6607 " Revoke trust relationship\n"
6608 " domain_name\tName of domain to revoke trust\n"));
6612 /* generate upper cased domain name */
6613 domain_name = smb_xstrdup(argv[0]);
6614 if (!strupper_m(domain_name)) {
6615 SAFE_FREE(domain_name);
6619 /* delete password of the trust */
6620 if (!pdb_del_trusteddom_pw(domain_name)) {
6621 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6628 SAFE_FREE(domain_name);
6632 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6633 const struct dom_sid *domain_sid,
6634 const char *domain_name,
6635 struct cli_state *cli,
6636 struct rpc_pipe_client *pipe_hnd,
6637 TALLOC_CTX *mem_ctx,
6642 if (!sid_to_fstring(str_sid, domain_sid)) {
6643 return NT_STATUS_UNSUCCESSFUL;
6645 d_printf("%s\n", str_sid);
6646 return NT_STATUS_OK;
6649 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6653 /* convert sid into ascii string */
6654 sid_to_fstring(ascii_sid, dom_sid);
6656 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6659 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6660 TALLOC_CTX *mem_ctx,
6661 struct policy_handle *pol,
6662 struct dom_sid dom_sid,
6663 const char *trusted_dom_name)
6665 NTSTATUS nt_status, result;
6666 union lsa_TrustedDomainInfo *info = NULL;
6667 char *cleartextpwd = NULL;
6668 DATA_BLOB session_key;
6669 DATA_BLOB data = data_blob_null;
6670 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6672 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6675 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6678 if (NT_STATUS_IS_ERR(nt_status)) {
6679 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6680 nt_errstr(nt_status)));
6683 if (NT_STATUS_IS_ERR(result)) {
6685 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6686 nt_errstr(result)));
6690 data = data_blob(info->password.password->data,
6691 info->password.password->length);
6693 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6694 if (!NT_STATUS_IS_OK(nt_status)) {
6695 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6699 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6700 data_blob_free(&session_key);
6702 if (cleartextpwd == NULL) {
6703 DEBUG(0,("retrieved NULL password\n"));
6704 nt_status = NT_STATUS_UNSUCCESSFUL;
6708 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6709 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6710 nt_status = NT_STATUS_UNSUCCESSFUL;
6714 #ifdef DEBUG_PASSWORD
6715 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6716 "password: [%s]\n", trusted_dom_name,
6717 sid_string_dbg(&dom_sid), cleartextpwd));
6721 SAFE_FREE(cleartextpwd);
6722 data_blob_free(&data);
6727 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6730 /* common variables */
6731 TALLOC_CTX* mem_ctx;
6732 struct cli_state *cli = NULL;
6733 struct rpc_pipe_client *pipe_hnd = NULL;
6734 NTSTATUS nt_status, result;
6735 const char *domain_name = NULL;
6736 struct policy_handle connect_hnd;
6737 union lsa_PolicyInformation *info = NULL;
6739 /* trusted domains listing variables */
6740 unsigned int enum_ctx = 0;
6742 struct lsa_DomainList dom_list;
6744 struct dcerpc_binding_handle *b;
6746 if (c->display_usage) {
6748 "net rpc trustdom vampire\n"
6751 _("Vampire trust relationship from remote server"));
6756 * Listing trusted domains (stored in secrets.tdb, if local)
6759 mem_ctx = talloc_init("trust relationships vampire");
6762 * set domain and pdc name to local samba server (default)
6763 * or to remote one given in command line
6766 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6767 domain_name = c->opt_workgroup;
6768 c->opt_target_workgroup = c->opt_workgroup;
6770 fstrcpy(pdc_name, lp_netbios_name());
6771 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6772 c->opt_target_workgroup = domain_name;
6775 /* open \PIPE\lsarpc and open policy handle */
6776 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6777 if (!NT_STATUS_IS_OK(nt_status)) {
6778 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6779 nt_errstr(nt_status)));
6780 talloc_destroy(mem_ctx);
6784 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6786 if (!NT_STATUS_IS_OK(nt_status)) {
6787 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6788 nt_errstr(nt_status) ));
6790 talloc_destroy(mem_ctx);
6794 b = pipe_hnd->binding_handle;
6796 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6798 if (NT_STATUS_IS_ERR(nt_status)) {
6799 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6800 nt_errstr(nt_status)));
6802 talloc_destroy(mem_ctx);
6806 /* query info level 5 to obtain sid of a domain being queried */
6807 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6809 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6813 if (NT_STATUS_IS_ERR(nt_status)) {
6814 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6815 nt_errstr(nt_status)));
6817 talloc_destroy(mem_ctx);
6820 if (NT_STATUS_IS_ERR(result)) {
6821 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6822 nt_errstr(result)));
6824 talloc_destroy(mem_ctx);
6829 * Keep calling LsaEnumTrustdom over opened pipe until
6830 * the end of enumeration is reached
6833 d_printf(_("Vampire trusted domains:\n\n"));
6836 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6842 if (NT_STATUS_IS_ERR(nt_status)) {
6843 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6844 nt_errstr(nt_status)));
6846 talloc_destroy(mem_ctx);
6849 if (NT_STATUS_IS_ERR(result)) {
6851 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6852 nt_errstr(result)));
6854 talloc_destroy(mem_ctx);
6859 for (i = 0; i < dom_list.count; i++) {
6861 print_trusted_domain(dom_list.domains[i].sid,
6862 dom_list.domains[i].name.string);
6864 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6865 *dom_list.domains[i].sid,
6866 dom_list.domains[i].name.string);
6867 if (!NT_STATUS_IS_OK(nt_status)) {
6869 talloc_destroy(mem_ctx);
6875 * in case of no trusted domains say something rather
6876 * than just display blank line
6878 if (!dom_list.count) d_printf(_("none\n"));
6880 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6882 /* close this connection before doing next one */
6883 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6884 if (NT_STATUS_IS_ERR(nt_status)) {
6885 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6886 nt_errstr(nt_status)));
6888 talloc_destroy(mem_ctx);
6892 /* close lsarpc pipe and connection to IPC$ */
6895 talloc_destroy(mem_ctx);
6899 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6901 /* common variables */
6902 TALLOC_CTX* mem_ctx;
6903 struct cli_state *cli = NULL, *remote_cli = NULL;
6904 struct rpc_pipe_client *pipe_hnd = NULL;
6905 NTSTATUS nt_status, result;
6906 const char *domain_name = NULL;
6907 struct dom_sid *queried_dom_sid;
6908 int ascii_dom_name_len;
6909 struct policy_handle connect_hnd;
6910 union lsa_PolicyInformation *info = NULL;
6911 struct dcerpc_binding_handle *b = NULL;
6913 /* trusted domains listing variables */
6914 unsigned int num_domains, enum_ctx = 0;
6916 struct lsa_DomainList dom_list;
6920 /* trusting domains listing variables */
6921 struct policy_handle domain_hnd;
6922 struct samr_SamArray *trusts = NULL;
6924 if (c->display_usage) {
6926 "net rpc trustdom list\n"
6929 _("List incoming and outgoing trust relationships"));
6934 * Listing trusted domains (stored in secrets.tdb, if local)
6937 mem_ctx = talloc_init("trust relationships listing");
6940 * set domain and pdc name to local samba server (default)
6941 * or to remote one given in command line
6944 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6945 domain_name = c->opt_workgroup;
6946 c->opt_target_workgroup = c->opt_workgroup;
6948 fstrcpy(pdc_name, lp_netbios_name());
6949 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6950 c->opt_target_workgroup = domain_name;
6953 /* open \PIPE\lsarpc and open policy handle */
6954 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6955 if (!NT_STATUS_IS_OK(nt_status)) {
6956 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6957 nt_errstr(nt_status)));
6958 talloc_destroy(mem_ctx);
6962 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6964 if (!NT_STATUS_IS_OK(nt_status)) {
6965 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6966 nt_errstr(nt_status) ));
6968 talloc_destroy(mem_ctx);
6972 b = pipe_hnd->binding_handle;
6974 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6976 if (NT_STATUS_IS_ERR(nt_status)) {
6977 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6978 nt_errstr(nt_status)));
6980 talloc_destroy(mem_ctx);
6984 /* query info level 5 to obtain sid of a domain being queried */
6985 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6987 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6991 if (NT_STATUS_IS_ERR(nt_status)) {
6992 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6993 nt_errstr(nt_status)));
6995 talloc_destroy(mem_ctx);
6998 if (NT_STATUS_IS_ERR(result)) {
6999 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7000 nt_errstr(result)));
7002 talloc_destroy(mem_ctx);
7006 queried_dom_sid = info->account_domain.sid;
7009 * Keep calling LsaEnumTrustdom over opened pipe until
7010 * the end of enumeration is reached
7013 d_printf(_("Trusted domains list:\n\n"));
7015 found_domain = false;
7018 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
7024 if (NT_STATUS_IS_ERR(nt_status)) {
7025 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7026 nt_errstr(nt_status)));
7028 talloc_destroy(mem_ctx);
7031 if (NT_STATUS_IS_ERR(result)) {
7032 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7033 nt_errstr(result)));
7035 talloc_destroy(mem_ctx);
7040 for (i = 0; i < dom_list.count; i++) {
7041 print_trusted_domain(dom_list.domains[i].sid,
7042 dom_list.domains[i].name.string);
7043 found_domain = true;
7047 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7050 * in case of no trusted domains say something rather
7051 * than just display blank line
7053 if (!found_domain) {
7054 d_printf(_("none\n"));
7057 /* close this connection before doing next one */
7058 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7059 if (NT_STATUS_IS_ERR(nt_status)) {
7060 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7061 nt_errstr(nt_status)));
7063 talloc_destroy(mem_ctx);
7067 TALLOC_FREE(pipe_hnd);
7070 * Listing trusting domains (stored in passdb backend, if local)
7073 d_printf(_("\nTrusting domains list:\n\n"));
7076 * Open \PIPE\samr and get needed policy handles
7078 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
7080 if (!NT_STATUS_IS_OK(nt_status)) {
7081 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
7083 talloc_destroy(mem_ctx);
7087 b = pipe_hnd->binding_handle;
7090 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
7092 SAMR_ACCESS_LOOKUP_DOMAIN,
7095 if (!NT_STATUS_IS_OK(nt_status)) {
7096 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7097 nt_errstr(nt_status)));
7099 talloc_destroy(mem_ctx);
7102 if (!NT_STATUS_IS_OK(result)) {
7104 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7105 nt_errstr(result)));
7107 talloc_destroy(mem_ctx);
7111 /* SamrOpenDomain - we have to open domain policy handle in order to be
7112 able to enumerate accounts*/
7113 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
7115 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
7119 if (!NT_STATUS_IS_OK(nt_status)) {
7120 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7121 nt_errstr(nt_status)));
7123 talloc_destroy(mem_ctx);
7126 if (!NT_STATUS_IS_OK(result)) {
7128 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7129 nt_errstr(result)));
7131 talloc_destroy(mem_ctx);
7136 * perform actual enumeration
7139 found_domain = false;
7141 enum_ctx = 0; /* reset enumeration context from last enumeration */
7144 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
7152 if (NT_STATUS_IS_ERR(nt_status)) {
7153 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7154 nt_errstr(nt_status)));
7156 talloc_destroy(mem_ctx);
7159 if (NT_STATUS_IS_ERR(result)) {
7161 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7162 nt_errstr(result)));
7164 talloc_destroy(mem_ctx);
7168 for (i = 0; i < num_domains; i++) {
7170 char *str = discard_const_p(char, trusts->entries[i].name.string);
7172 found_domain = true;
7175 * get each single domain's sid (do we _really_ need this ?):
7176 * 1) connect to domain's pdc
7177 * 2) query the pdc for domain's sid
7180 /* get rid of '$' tail */
7181 ascii_dom_name_len = strlen(str);
7182 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7183 str[ascii_dom_name_len - 1] = '\0';
7185 /* set opt_* variables to remote domain */
7186 if (!strupper_m(str)) {
7188 talloc_destroy(mem_ctx);
7191 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7192 c->opt_target_workgroup = c->opt_workgroup;
7194 d_printf("%-20s", str);
7196 /* connect to remote domain controller */
7197 nt_status = net_make_ipc_connection(c,
7198 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7200 if (NT_STATUS_IS_OK(nt_status)) {
7201 /* query for domain's sid */
7202 if (run_rpc_command(
7204 &ndr_table_lsarpc, 0,
7205 rpc_query_domain_sid, argc,
7207 d_printf(_("strange - couldn't get domain's sid\n"));
7209 cli_shutdown(remote_cli);
7212 d_fprintf(stderr, _("domain controller is not "
7213 "responding: %s\n"),
7214 nt_errstr(nt_status));
7215 d_printf(_("couldn't get domain's sid\n"));
7219 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7221 if (!found_domain) {
7225 /* close opened samr and domain policy handles */
7226 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7227 if (!NT_STATUS_IS_OK(nt_status)) {
7228 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7231 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7232 if (!NT_STATUS_IS_OK(nt_status)) {
7233 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7236 /* close samr pipe and connection to IPC$ */
7239 talloc_destroy(mem_ctx);
7244 * Entrypoint for 'net rpc trustdom' code.
7246 * @param argc Standard argc.
7247 * @param argv Standard argv without initial components.
7249 * @return Integer status (0 means success).
7252 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7254 struct functable func[] = {
7259 N_("Add trusting domain's account"),
7260 N_("net rpc trustdom add\n"
7261 " Add trusting domain's account")
7267 N_("Remove trusting domain's account"),
7268 N_("net rpc trustdom del\n"
7269 " Remove trusting domain's account")
7273 rpc_trustdom_establish,
7275 N_("Establish outgoing trust relationship"),
7276 N_("net rpc trustdom establish\n"
7277 " Establish outgoing trust relationship")
7281 rpc_trustdom_revoke,
7283 N_("Revoke outgoing trust relationship"),
7284 N_("net rpc trustdom revoke\n"
7285 " Revoke outgoing trust relationship")
7291 N_("List in- and outgoing domain trusts"),
7292 N_("net rpc trustdom list\n"
7293 " List in- and outgoing domain trusts")
7297 rpc_trustdom_vampire,
7299 N_("Vampire trusts from remote server"),
7300 N_("net rpc trustdom vampire\n"
7301 " Vampire trusts from remote server")
7303 {NULL, NULL, 0, NULL, NULL}
7306 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7310 * Check if a server will take rpc commands
7311 * @param flags Type of server to connect to (PDC, DMB, localhost)
7312 * if the host is not explicitly specified
7313 * @return bool (true means rpc supported)
7315 bool net_rpc_check(struct net_context *c, unsigned flags)
7317 struct cli_state *cli;
7319 struct sockaddr_storage server_ss;
7320 char *server_name = NULL;
7323 /* flags (i.e. server type) may depend on command */
7324 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7327 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7328 lp_netbios_name(), SMB_SIGNING_DEFAULT,
7330 if (!NT_STATUS_IS_OK(status)) {
7333 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7335 if (!NT_STATUS_IS_OK(status))
7337 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7346 /* dump sam database via samsync rpc calls */
7347 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7348 if (c->display_usage) {
7353 _("Dump remote SAM database"));
7357 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7358 NET_FLAGS_ANONYMOUS,
7359 rpc_samdump_internals, argc, argv);
7362 /* syncronise sam database via samsync rpc calls */
7363 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7365 struct functable func[] = {
7370 N_("Dump remote SAM database to ldif"),
7371 N_("net rpc vampire ldif\n"
7372 " Dump remote SAM database to LDIF file or "
7379 N_("Dump remote SAM database to Kerberos Keytab"),
7380 N_("net rpc vampire keytab\n"
7381 " Dump remote SAM database to Kerberos keytab "
7388 N_("Dump remote SAM database to passdb"),
7389 N_("net rpc vampire passdb\n"
7390 " Dump remote SAM database to passdb")
7393 {NULL, NULL, 0, NULL, NULL}
7397 if (c->display_usage) {
7402 _("Vampire remote SAM database"));
7406 return rpc_vampire_passdb(c, argc, argv);
7409 return net_run_function(c, argc, argv, "net rpc vampire", func);
7413 * Migrate everything from a print server.
7415 * @param c A net_context structure.
7416 * @param argc Standard main() style argc.
7417 * @param argv Standard main() style argv. Initial components are already
7420 * @return A shell status integer (0 for success).
7422 * The order is important !
7423 * To successfully add drivers the print queues have to exist !
7424 * Applying ACLs should be the last step, because you're easily locked out.
7427 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7432 if (c->display_usage) {
7434 "net rpc printer migrate all\n"
7437 _("Migrate everything from a print server"));
7442 d_printf(_("no server to migrate\n"));
7446 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7447 rpc_printer_migrate_printers_internals, argc,
7452 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7453 rpc_printer_migrate_drivers_internals, argc,
7458 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7459 rpc_printer_migrate_forms_internals, argc, argv);
7463 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7464 rpc_printer_migrate_settings_internals, argc,
7469 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7470 rpc_printer_migrate_security_internals, argc,
7476 * Migrate print drivers from a print server.
7478 * @param c A net_context structure.
7479 * @param argc Standard main() style argc.
7480 * @param argv Standard main() style argv. Initial components are already
7483 * @return A shell status integer (0 for success).
7485 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7488 if (c->display_usage) {
7490 "net rpc printer migrate drivers\n"
7493 _("Migrate print-drivers from a print-server"));
7498 d_printf(_("no server to migrate\n"));
7502 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7503 rpc_printer_migrate_drivers_internals,
7508 * Migrate print-forms from a print-server.
7510 * @param c A net_context structure.
7511 * @param argc Standard main() style argc.
7512 * @param argv Standard main() style argv. Initial components are already
7515 * @return A shell status integer (0 for success).
7517 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7520 if (c->display_usage) {
7522 "net rpc printer migrate forms\n"
7525 _("Migrate print-forms from a print-server"));
7530 d_printf(_("no server to migrate\n"));
7534 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7535 rpc_printer_migrate_forms_internals,
7540 * Migrate printers from a print-server.
7542 * @param c A net_context structure.
7543 * @param argc Standard main() style argc.
7544 * @param argv Standard main() style argv. Initial components are already
7547 * @return A shell status integer (0 for success).
7549 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7552 if (c->display_usage) {
7554 "net rpc printer migrate printers\n"
7557 _("Migrate printers from a print-server"));
7562 d_printf(_("no server to migrate\n"));
7566 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7567 rpc_printer_migrate_printers_internals,
7572 * Migrate printer-ACLs from a print-server
7574 * @param c A net_context structure.
7575 * @param argc Standard main() style argc.
7576 * @param argv Standard main() style argv. Initial components are already
7579 * @return A shell status integer (0 for success).
7581 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7584 if (c->display_usage) {
7586 "net rpc printer migrate security\n"
7589 _("Migrate printer-ACLs from a print-server"));
7594 d_printf(_("no server to migrate\n"));
7598 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7599 rpc_printer_migrate_security_internals,
7604 * Migrate printer-settings from a print-server.
7606 * @param c A net_context structure.
7607 * @param argc Standard main() style argc.
7608 * @param argv Standard main() style argv. Initial components are already
7611 * @return A shell status integer (0 for success).
7613 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7616 if (c->display_usage) {
7618 "net rpc printer migrate settings\n"
7621 _("Migrate printer-settings from a "
7627 d_printf(_("no server to migrate\n"));
7631 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7632 rpc_printer_migrate_settings_internals,
7637 * 'net rpc printer' entrypoint.
7639 * @param c A net_context structure.
7640 * @param argc Standard main() style argc.
7641 * @param argv Standard main() style argv. Initial components are already
7645 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7648 /* ouch: when addriver and setdriver are called from within
7649 rpc_printer_migrate_drivers_internals, the printer-queue already
7652 struct functable func[] = {
7655 rpc_printer_migrate_all,
7657 N_("Migrate all from remote to local print server"),
7658 N_("net rpc printer migrate all\n"
7659 " Migrate all from remote to local print server")
7663 rpc_printer_migrate_drivers,
7665 N_("Migrate drivers to local server"),
7666 N_("net rpc printer migrate drivers\n"
7667 " Migrate drivers to local server")
7671 rpc_printer_migrate_forms,
7673 N_("Migrate froms to local server"),
7674 N_("net rpc printer migrate forms\n"
7675 " Migrate froms to local server")
7679 rpc_printer_migrate_printers,
7681 N_("Migrate printers to local server"),
7682 N_("net rpc printer migrate printers\n"
7683 " Migrate printers to local server")
7687 rpc_printer_migrate_security,
7689 N_("Mirgate printer ACLs to local server"),
7690 N_("net rpc printer migrate security\n"
7691 " Mirgate printer ACLs to local server")
7695 rpc_printer_migrate_settings,
7697 N_("Migrate printer settings to local server"),
7698 N_("net rpc printer migrate settings\n"
7699 " Migrate printer settings to local server")
7701 {NULL, NULL, 0, NULL, NULL}
7704 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7709 * List printers on a remote RPC server.
7711 * @param c A net_context structure.
7712 * @param argc Standard main() style argc.
7713 * @param argv Standard main() style argv. Initial components are already
7716 * @return A shell status integer (0 for success).
7718 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7720 if (c->display_usage) {
7722 "net rpc printer list\n"
7725 _("List printers on a remote RPC server"));
7729 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7730 rpc_printer_list_internals,
7735 * List printer-drivers on a remote RPC server.
7737 * @param c A net_context structure.
7738 * @param argc Standard main() style argc.
7739 * @param argv Standard main() style argv. Initial components are already
7742 * @return A shell status integer (0 for success).
7744 static int rpc_printer_driver_list(struct net_context *c, int argc,
7747 if (c->display_usage) {
7749 "net rpc printer driver\n"
7752 _("List printer-drivers on a remote RPC server"));
7756 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7757 rpc_printer_driver_list_internals,
7762 * Publish printer in ADS via MSRPC.
7764 * @param c A net_context structure.
7765 * @param argc Standard main() style argc.
7766 * @param argv Standard main() style argv. Initial components are already
7769 * @return A shell status integer (0 for success).
7771 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7774 if (c->display_usage) {
7776 "net rpc printer publish publish\n"
7779 _("Publish printer in ADS via MSRPC"));
7783 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7784 rpc_printer_publish_publish_internals,
7789 * Update printer in ADS via MSRPC.
7791 * @param c A net_context structure.
7792 * @param argc Standard main() style argc.
7793 * @param argv Standard main() style argv. Initial components are already
7796 * @return A shell status integer (0 for success).
7798 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7800 if (c->display_usage) {
7802 "net rpc printer publish update\n"
7805 _("Update printer in ADS via MSRPC"));
7809 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7810 rpc_printer_publish_update_internals,
7815 * UnPublish printer in ADS via MSRPC.
7817 * @param c A net_context structure.
7818 * @param argc Standard main() style argc.
7819 * @param argv Standard main() style argv. Initial components are already
7822 * @return A shell status integer (0 for success).
7824 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7827 if (c->display_usage) {
7829 "net rpc printer publish unpublish\n"
7832 _("UnPublish printer in ADS via MSRPC"));
7836 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7837 rpc_printer_publish_unpublish_internals,
7842 * List published printers via MSRPC.
7844 * @param c A net_context structure.
7845 * @param argc Standard main() style argc.
7846 * @param argv Standard main() style argv. Initial components are already
7849 * @return A shell status integer (0 for success).
7851 static int rpc_printer_publish_list(struct net_context *c, int argc,
7854 if (c->display_usage) {
7856 "net rpc printer publish list\n"
7859 _("List published printers via MSRPC"));
7863 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7864 rpc_printer_publish_list_internals,
7870 * Publish printer in ADS.
7872 * @param c A net_context structure.
7873 * @param argc Standard main() style argc.
7874 * @param argv Standard main() style argv. Initial components are already
7877 * @return A shell status integer (0 for success).
7879 static int rpc_printer_publish(struct net_context *c, int argc,
7883 struct functable func[] = {
7886 rpc_printer_publish_publish,
7888 N_("Publish printer in AD"),
7889 N_("net rpc printer publish publish\n"
7890 " Publish printer in AD")
7894 rpc_printer_publish_update,
7896 N_("Update printer in AD"),
7897 N_("net rpc printer publish update\n"
7898 " Update printer in AD")
7902 rpc_printer_publish_unpublish,
7904 N_("Unpublish printer"),
7905 N_("net rpc printer publish unpublish\n"
7906 " Unpublish printer")
7910 rpc_printer_publish_list,
7912 N_("List published printers"),
7913 N_("net rpc printer publish list\n"
7914 " List published printers")
7916 {NULL, NULL, 0, NULL, NULL}
7920 if (c->display_usage) {
7921 d_printf(_("Usage:\n"));
7922 d_printf(_("net rpc printer publish\n"
7923 " List published printers\n"
7924 " Alias of net rpc printer publish "
7926 net_display_usage_from_functable(func);
7929 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7930 rpc_printer_publish_list_internals,
7934 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7940 * Display rpc printer help page.
7942 * @param c A net_context structure.
7943 * @param argc Standard main() style argc.
7944 * @param argv Standard main() style argv. Initial components are already
7947 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7949 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7950 "\tlists all printers on print-server\n\n"));
7951 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7952 "\tlists all printer-drivers on print-server\n\n"));
7953 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7954 "\tpublishes printer settings in Active Directory\n"
7955 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7956 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7957 "\n\tmigrates printers from remote to local server\n\n"));
7958 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7959 "\n\tmigrates printer-settings from remote to local server\n\n"));
7960 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7961 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7962 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7963 "\n\tmigrates printer-forms from remote to local server\n\n"));
7964 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7965 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7966 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7967 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7968 "\tremote to local print-server\n\n"));
7969 net_common_methods_usage(c, argc, argv);
7970 net_common_flags_usage(c, argc, argv);
7972 "\t-v or --verbose\t\t\tgive verbose output\n"
7973 "\t --destination\t\tmigration target server (default: localhost)\n"));
7979 * 'net rpc printer' entrypoint.
7981 * @param c A net_context structure.
7982 * @param argc Standard main() style argc.
7983 * @param argv Standard main() style argv. Initial components are already
7986 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7988 struct functable func[] = {
7993 N_("List all printers on print server"),
7994 N_("net rpc printer list\n"
7995 " List all printers on print server")
7999 rpc_printer_migrate,
8001 N_("Migrate printer to local server"),
8002 N_("net rpc printer migrate\n"
8003 " Migrate printer to local server")
8007 rpc_printer_driver_list,
8009 N_("List printer drivers"),
8010 N_("net rpc printer driver\n"
8011 " List printer drivers")
8015 rpc_printer_publish,
8017 N_("Publish printer in AD"),
8018 N_("net rpc printer publish\n"
8019 " Publish printer in AD")
8021 {NULL, NULL, 0, NULL, NULL}
8025 if (c->display_usage) {
8026 d_printf(_("Usage:\n"));
8027 d_printf(_("net rpc printer\n"
8028 " List printers\n"));
8029 net_display_usage_from_functable(func);
8032 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8033 rpc_printer_list_internals,
8037 return net_run_function(c, argc, argv, "net rpc printer", func);
8041 * 'net rpc' entrypoint.
8043 * @param c A net_context structure.
8044 * @param argc Standard main() style argc.
8045 * @param argv Standard main() style argv. Initial components are already
8049 int net_rpc(struct net_context *c, int argc, const char **argv)
8051 NET_API_STATUS status;
8053 struct functable func[] = {
8058 N_("Modify global audit settings"),
8059 N_("net rpc audit\n"
8060 " Modify global audit settings")
8066 N_("Show basic info about a domain"),
8068 " Show basic info about a domain")
8074 N_("Join a domain"),
8082 N_("Join a domain created in server manager"),
8083 N_("net rpc oldjoin\n"
8084 " Join a domain created in server manager")
8090 N_("Test that a join is valid"),
8091 N_("net rpc testjoin\n"
8092 " Test that a join is valid")
8098 N_("List/modify users"),
8100 " List/modify users")
8106 N_("Change a user password"),
8107 N_("net rpc password\n"
8108 " Change a user password\n"
8109 " Alias for net rpc user password")
8115 N_("List/modify groups"),
8116 N_("net rpc group\n"
8117 " List/modify groups")
8123 N_("List/modify shares"),
8124 N_("net rpc share\n"
8125 " List/modify shares")
8131 N_("List open files"),
8139 N_("List/modify printers"),
8140 N_("net rpc printer\n"
8141 " List/modify printers")
8145 net_rpc_changetrustpw,
8147 N_("Change trust account password"),
8148 N_("net rpc changetrustpw\n"
8149 " Change trust account password")
8155 N_("Modify domain trusts"),
8156 N_("net rpc trustdom\n"
8157 " Modify domain trusts")
8163 N_("Abort a remote shutdown"),
8164 N_("net rpc abortshutdown\n"
8165 " Abort a remote shutdown")
8171 N_("Shutdown a remote server"),
8172 N_("net rpc shutdown\n"
8173 " Shutdown a remote server")
8179 N_("Dump SAM data of remote NT PDC"),
8180 N_("net rpc samdump\n"
8181 " Dump SAM data of remote NT PDC")
8187 N_("Sync a remote NT PDC's data into local passdb"),
8188 N_("net rpc vampire\n"
8189 " Sync a remote NT PDC's data into local passdb")
8195 N_("Fetch the domain sid into local secrets.tdb"),
8196 N_("net rpc getsid\n"
8197 " Fetch the domain sid into local secrets.tdb")
8203 N_("Manage privileges assigned to SID"),
8204 N_("net rpc rights\n"
8205 " Manage privileges assigned to SID")
8211 N_("Start/stop/query remote services"),
8212 N_("net rpc service\n"
8213 " Start/stop/query remote services")
8219 N_("Manage registry hives"),
8220 N_("net rpc registry\n"
8221 " Manage registry hives")
8227 N_("Open interactive shell on remote server"),
8228 N_("net rpc shell\n"
8229 " Open interactive shell on remote server")
8235 N_("Manage trusts"),
8236 N_("net rpc trust\n"
8243 N_("Configure a remote samba server"),
8245 " Configure a remote samba server")
8247 {NULL, NULL, 0, NULL, NULL}
8250 status = libnetapi_net_init(&c->netapi_ctx);
8254 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8255 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8256 if (c->opt_kerberos) {
8257 libnetapi_set_use_kerberos(c->netapi_ctx);
8259 if (c->opt_ccache) {
8260 libnetapi_set_use_ccache(c->netapi_ctx);
8263 return net_run_function(c, argc, argv, "net rpc", func);