2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/cli_samr.h"
29 #include "rpc_client/init_samr.h"
30 #include "../librpc/gen_ndr/ndr_lsa_c.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
34 #include "../librpc/gen_ndr/ndr_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
36 #include "../librpc/gen_ndr/ndr_winreg_c.h"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "librpc/gen_ndr/libnet_join.h"
41 #include "libnet/libnet_join.h"
42 #include "rpc_client/init_lsa.h"
43 #include "../libcli/security/security.h"
44 #include "libsmb/libsmb.h"
45 #include "libsmb/clirap.h"
46 #include "nsswitch/libwbclient/wbclient.h"
48 #include "../libcli/smb/smbXcli_base.h"
50 static int net_mode_share;
51 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
56 * @brief RPC based subcommands for the 'net' utility.
58 * This file should contain much of the functionality that used to
59 * be found in rpcclient, execpt that the commands should change
60 * less often, and the fucntionality should be sane (the user is not
61 * expected to know a rid/sid before they conduct an operation etc.)
63 * @todo Perhaps eventually these should be split out into a number
64 * of files, as this could get quite big.
69 * Many of the RPC functions need the domain sid. This function gets
70 * it at the start of every run
72 * @param cli A cli_state already connected to the remote machine
74 * @return The Domain SID of the remote machine.
77 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
78 struct dom_sid **domain_sid,
79 const char **domain_name)
81 struct rpc_pipe_client *lsa_pipe = NULL;
82 struct policy_handle pol;
83 NTSTATUS status, result;
84 union lsa_PolicyInformation *info = NULL;
85 struct dcerpc_binding_handle *b;
87 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
89 if (!NT_STATUS_IS_OK(status)) {
90 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
94 b = lsa_pipe->binding_handle;
96 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
97 SEC_FLAG_MAXIMUM_ALLOWED,
99 if (!NT_STATUS_IS_OK(status)) {
100 d_fprintf(stderr, "open_policy %s: %s\n",
106 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
108 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
111 if (!NT_STATUS_IS_OK(status)) {
112 d_fprintf(stderr, "lsaquery %s: %s\n",
117 if (!NT_STATUS_IS_OK(result)) {
118 d_fprintf(stderr, "lsaquery %s: %s\n",
124 *domain_name = info->account_domain.name.string;
125 *domain_sid = info->account_domain.sid;
127 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
128 TALLOC_FREE(lsa_pipe);
134 * Run a single RPC command, from start to finish.
136 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
137 * @param conn_flag a NET_FLAG_ combination. Passed to
138 * net_make_ipc_connection.
139 * @param argc Standard main() style argc.
140 * @param argv Standard main() style argv. Initial components are already
142 * @return A shell status integer (0 for success).
145 int run_rpc_command(struct net_context *c,
146 struct cli_state *cli_arg,
147 const struct ndr_interface_table *table,
153 struct cli_state *cli = NULL;
154 struct rpc_pipe_client *pipe_hnd = NULL;
157 struct dom_sid *domain_sid;
158 const char *domain_name;
161 /* make use of cli_state handed over as an argument, if possible */
163 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 DEBUG(1, ("failed to make ipc connection: %s\n",
166 nt_errstr(nt_status)));
179 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
180 DEBUG(0, ("talloc_init() failed\n"));
184 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
186 if (!NT_STATUS_IS_OK(nt_status)) {
190 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
191 if (lp_client_schannel()
192 && (ndr_syntax_id_equal(&table->syntax_id,
193 &ndr_table_netlogon.syntax_id))) {
194 /* Always try and create an schannel netlogon pipe. */
195 nt_status = cli_rpc_pipe_open_schannel(
196 cli, table, NCACN_NP,
197 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
199 if (!NT_STATUS_IS_OK(nt_status)) {
200 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
201 nt_errstr(nt_status) ));
205 if (conn_flags & NET_FLAGS_SEAL) {
206 nt_status = cli_rpc_pipe_open_generic_auth(
208 (conn_flags & NET_FLAGS_TCP) ?
209 NCACN_IP_TCP : NCACN_NP,
210 DCERPC_AUTH_TYPE_NTLMSSP,
211 DCERPC_AUTH_LEVEL_PRIVACY,
212 smbXcli_conn_remote_name(cli->conn),
213 lp_workgroup(), c->opt_user_name,
214 c->opt_password, &pipe_hnd);
216 nt_status = cli_rpc_pipe_open_noauth(
220 if (!NT_STATUS_IS_OK(nt_status)) {
221 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
223 nt_errstr(nt_status) ));
229 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
231 if (!NT_STATUS_IS_OK(nt_status)) {
232 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
235 DEBUG(5, ("rpc command function succedded\n"));
238 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
240 TALLOC_FREE(pipe_hnd);
245 /* close the connection only if it was opened here */
250 talloc_destroy(mem_ctx);
255 * Force a change of the trust acccount password.
257 * All parameters are provided by the run_rpc_command function, except for
258 * argc, argv which are passed through.
260 * @param domain_sid The domain sid acquired from the remote server.
261 * @param cli A cli_state connected to the server.
262 * @param mem_ctx Talloc context, destroyed on completion of the function.
263 * @param argc Standard main() style argc.
264 * @param argv Standard main() style argv. Initial components are already
267 * @return Normal NTSTATUS return.
270 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
271 const struct dom_sid *domain_sid,
272 const char *domain_name,
273 struct cli_state *cli,
274 struct rpc_pipe_client *pipe_hnd,
281 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
282 if (!NT_STATUS_IS_OK(status)) {
283 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
292 * Force a change of the trust acccount password.
294 * @param argc Standard main() style argc.
295 * @param argv Standard main() style argv. Initial components are already
298 * @return A shell status integer (0 for success).
301 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
303 if (c->display_usage) {
305 "net rpc changetrustpw\n"
308 _("Change the machine trust password"));
312 return run_rpc_command(c, NULL, &ndr_table_netlogon,
313 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
314 rpc_changetrustpw_internals,
319 * Join a domain, the old way. This function exists to allow
320 * the message to be displayed when oldjoin was explicitly
321 * requested, but not when it was implied by "net rpc join".
323 * This uses 'machinename' as the inital password, and changes it.
325 * The password should be created with 'server manager' or equiv first.
327 * @param argc Standard main() style argc.
328 * @param argv Standard main() style argv. Initial components are already
331 * @return A shell status integer (0 for success).
334 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
336 struct libnet_JoinCtx *r = NULL;
339 const char *domain = lp_workgroup(); /* FIXME */
340 bool modify_config = lp_config_backend_is_registry();
341 enum netr_SchannelType sec_chan_type;
344 if (c->display_usage) {
347 " Join a domain the old way\n");
351 mem_ctx = talloc_init("net_rpc_oldjoin");
356 werr = libnet_init_JoinCtx(mem_ctx, &r);
357 if (!W_ERROR_IS_OK(werr)) {
362 check what type of join - if the user want's to join as
363 a BDC, the server must agree that we are a BDC.
366 sec_chan_type = get_sec_channel_type(argv[0]);
368 sec_chan_type = get_sec_channel_type(NULL);
372 d_fprintf(stderr, _("Could not initialise message context. "
373 "Try running as root\n"));
374 werr = WERR_ACCESS_DENIED;
378 pw = talloc_strndup(r, lp_netbios_name(), 14);
384 r->in.msg_ctx = c->msg_ctx;
385 r->in.domain_name = domain;
386 r->in.secure_channel_type = sec_chan_type;
387 r->in.dc_name = c->opt_host;
388 r->in.admin_account = "";
389 r->in.admin_password = strlower_talloc(r, pw);
390 if (r->in.admin_password == NULL) {
395 r->in.modify_config = modify_config;
396 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
397 WKSSVC_JOIN_FLAGS_JOIN_UNSECURE |
398 WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED;
400 werr = libnet_Join(mem_ctx, r);
401 if (!W_ERROR_IS_OK(werr)) {
405 /* Check the short name of the domain */
407 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
408 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
409 d_printf("domain name obtained from the server.\n");
410 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
411 d_printf("You should set \"workgroup = %s\" in %s.\n",
412 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
415 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
417 if (r->out.dns_domain_name) {
418 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
419 r->out.dns_domain_name);
421 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
422 r->out.netbios_domain_name);
425 TALLOC_FREE(mem_ctx);
430 if (c->opt_flags & NET_FLAGS_EXPECT_FALLBACK) {
434 /* issue an overall failure message at the end. */
435 d_fprintf(stderr, _("Failed to join domain: %s\n"),
436 r && r->out.error_string ? r->out.error_string :
437 get_friendly_werror_msg(werr));
440 TALLOC_FREE(mem_ctx);
446 * check that a join is OK
448 * @return A shell status integer (0 for success)
451 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
455 const char *domain = c->opt_target_workgroup;
456 const char *dc = c->opt_host;
458 if (c->display_usage) {
461 " Test if a join is OK\n");
465 mem_ctx = talloc_init("net_rpc_testjoin");
471 struct netr_DsRGetDCNameInfo *info;
474 d_fprintf(stderr, _("Could not initialise message context. "
475 "Try running as root\n"));
476 talloc_destroy(mem_ctx);
480 status = dsgetdcname(mem_ctx,
487 if (!NT_STATUS_IS_OK(status)) {
488 talloc_destroy(mem_ctx);
492 dc = strip_hostname(info->dc_unc);
495 /* Display success or failure */
496 status = libnet_join_ok(c->opt_workgroup, lp_netbios_name(), dc,
498 if (!NT_STATUS_IS_OK(status)) {
499 fprintf(stderr,"Join to domain '%s' is not valid: %s\n",
500 domain, nt_errstr(status));
501 talloc_destroy(mem_ctx);
505 printf("Join to '%s' is OK\n",domain);
506 talloc_destroy(mem_ctx);
512 * Join a domain using the administrator username and password
514 * @param argc Standard main() style argc
515 * @param argc Standard main() style argv. Initial components are already
516 * stripped. Currently not used.
517 * @return A shell status integer (0 for success)
521 static int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
523 struct libnet_JoinCtx *r = NULL;
526 const char *domain = lp_workgroup(); /* FIXME */
527 bool modify_config = lp_config_backend_is_registry();
528 enum netr_SchannelType sec_chan_type;
530 if (c->display_usage) {
533 " Join a domain the new way\n");
537 mem_ctx = talloc_init("net_rpc_join_newstyle");
542 werr = libnet_init_JoinCtx(mem_ctx, &r);
543 if (!W_ERROR_IS_OK(werr)) {
548 check what type of join - if the user want's to join as
549 a BDC, the server must agree that we are a BDC.
552 sec_chan_type = get_sec_channel_type(argv[0]);
554 sec_chan_type = get_sec_channel_type(NULL);
558 d_fprintf(stderr, _("Could not initialise message context. "
559 "Try running as root\n"));
560 werr = WERR_ACCESS_DENIED;
564 r->in.msg_ctx = c->msg_ctx;
565 r->in.domain_name = domain;
566 r->in.secure_channel_type = sec_chan_type;
567 r->in.dc_name = c->opt_host;
568 r->in.admin_account = c->opt_user_name;
569 r->in.admin_password = net_prompt_pass(c, c->opt_user_name);
571 r->in.use_kerberos = c->opt_kerberos;
572 r->in.modify_config = modify_config;
573 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
574 WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
575 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
577 werr = libnet_Join(mem_ctx, r);
578 if (!W_ERROR_IS_OK(werr)) {
582 /* Check the short name of the domain */
584 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
585 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
586 d_printf("domain name obtained from the server.\n");
587 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
588 d_printf("You should set \"workgroup = %s\" in %s.\n",
589 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
592 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
594 if (r->out.dns_domain_name) {
595 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
596 r->out.dns_domain_name);
598 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
599 r->out.netbios_domain_name);
602 TALLOC_FREE(mem_ctx);
607 /* issue an overall failure message at the end. */
608 d_printf("Failed to join domain: %s\n",
609 r && r->out.error_string ? r->out.error_string :
610 get_friendly_werror_msg(werr));
612 TALLOC_FREE(mem_ctx);
618 * 'net rpc join' entrypoint.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
623 * Main 'net_rpc_join()' (where the admin username/password is used) is
625 * Try to just change the password, but if that doesn't work, use/prompt
626 * for a username/password.
629 int net_rpc_join(struct net_context *c, int argc, const char **argv)
633 if (c->display_usage) {
636 _("net rpc join -U <username>[%%password] <type>\n"
638 " username\tName of the admin user"
639 " password\tPassword of the admin user, will "
640 "prompt if not specified\n"
641 " type\tCan be one of the following:\n"
642 "\t\tMEMBER\tJoin as member server (default)\n"
643 "\t\tBDC\tJoin as BDC\n"
644 "\t\tPDC\tJoin as PDC\n"));
648 if (lp_server_role() == ROLE_STANDALONE) {
649 d_printf(_("cannot join as standalone machine\n"));
653 if (strlen(lp_netbios_name()) > 15) {
654 d_printf(_("Our netbios name can be at most 15 chars long, "
655 "\"%s\" is %u chars long\n"),
656 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
660 c->opt_flags |= NET_FLAGS_EXPECT_FALLBACK;
661 ret = net_rpc_oldjoin(c, argc, argv);
662 c->opt_flags &= ~NET_FLAGS_EXPECT_FALLBACK;
667 return net_rpc_join_newstyle(c, argc, argv);
671 * display info about a rpc domain
673 * All parameters are provided by the run_rpc_command function, except for
674 * argc, argv which are passed through.
676 * @param domain_sid The domain sid acquired from the remote server
677 * @param cli A cli_state connected to the server.
678 * @param mem_ctx Talloc context, destroyed on completion of the function.
679 * @param argc Standard main() style argc.
680 * @param argv Standard main() style argv. Initial components are already
683 * @return Normal NTSTATUS return.
686 NTSTATUS rpc_info_internals(struct net_context *c,
687 const struct dom_sid *domain_sid,
688 const char *domain_name,
689 struct cli_state *cli,
690 struct rpc_pipe_client *pipe_hnd,
695 struct policy_handle connect_pol, domain_pol;
696 NTSTATUS status, result;
697 union samr_DomainInfo *info = NULL;
699 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
701 sid_to_fstring(sid_str, domain_sid);
703 /* Get sam policy handle */
704 status = dcerpc_samr_Connect2(b, mem_ctx,
706 MAXIMUM_ALLOWED_ACCESS,
709 if (!NT_STATUS_IS_OK(status)) {
710 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
715 if (!NT_STATUS_IS_OK(result)) {
717 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
722 /* Get domain policy handle */
723 status = dcerpc_samr_OpenDomain(b, mem_ctx,
725 MAXIMUM_ALLOWED_ACCESS,
726 discard_const_p(struct dom_sid2, domain_sid),
729 if (!NT_STATUS_IS_OK(status)) {
730 d_fprintf(stderr, _("Could not open domain: %s\n"),
734 if (!NT_STATUS_IS_OK(result)) {
736 d_fprintf(stderr, _("Could not open domain: %s\n"),
741 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
746 if (!NT_STATUS_IS_OK(status)) {
750 if (NT_STATUS_IS_OK(result)) {
751 d_printf(_("Domain Name: %s\n"),
752 info->general.domain_name.string);
753 d_printf(_("Domain SID: %s\n"), sid_str);
754 d_printf(_("Sequence number: %llu\n"),
755 (unsigned long long)info->general.sequence_num);
756 d_printf(_("Num users: %u\n"), info->general.num_users);
757 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
758 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
766 * 'net rpc info' entrypoint.
767 * @param argc Standard main() style argc.
768 * @param argv Standard main() style argv. Initial components are already
772 int net_rpc_info(struct net_context *c, int argc, const char **argv)
774 if (c->display_usage) {
779 _("Display information about the domain"));
783 return run_rpc_command(c, NULL, &ndr_table_samr,
784 NET_FLAGS_PDC, rpc_info_internals,
789 * Fetch domain SID into the local secrets.tdb.
791 * All parameters are provided by the run_rpc_command function, except for
792 * argc, argv which are passed through.
794 * @param domain_sid The domain sid acquired from the remote server.
795 * @param cli A cli_state connected to the server.
796 * @param mem_ctx Talloc context, destroyed on completion of the function.
797 * @param argc Standard main() style argc.
798 * @param argv Standard main() style argv. Initial components are already
801 * @return Normal NTSTATUS return.
804 static NTSTATUS rpc_getsid_internals(struct net_context *c,
805 const struct dom_sid *domain_sid,
806 const char *domain_name,
807 struct cli_state *cli,
808 struct rpc_pipe_client *pipe_hnd,
815 sid_to_fstring(sid_str, domain_sid);
816 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
817 sid_str, domain_name);
819 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
820 DEBUG(0,("Can't store domain SID\n"));
821 return NT_STATUS_UNSUCCESSFUL;
828 * 'net rpc getsid' entrypoint.
829 * @param argc Standard main() style argc.
830 * @param argv Standard main() style argv. Initial components are already
834 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
836 int conn_flags = NET_FLAGS_PDC;
838 if (!c->opt_user_specified) {
839 conn_flags |= NET_FLAGS_ANONYMOUS;
842 if (c->display_usage) {
847 _("Fetch domain SID into local secrets.tdb"));
851 return run_rpc_command(c, NULL, &ndr_table_samr,
853 rpc_getsid_internals,
857 /****************************************************************************/
860 * Basic usage function for 'net rpc user'.
861 * @param argc Standard main() style argc.
862 * @param argv Standard main() style argv. Initial components are already
866 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
868 return net_user_usage(c, argc, argv);
872 * Add a new user to a remote RPC server.
874 * @param argc Standard main() style argc.
875 * @param argv Standard main() style argv. Initial components are already
878 * @return A shell status integer (0 for success).
881 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
883 NET_API_STATUS status;
884 struct USER_INFO_1 info1;
885 uint32_t parm_error = 0;
887 if (argc < 1 || c->display_usage) {
888 rpc_user_usage(c, argc, argv);
894 info1.usri1_name = argv[0];
896 info1.usri1_password = argv[1];
899 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
902 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
903 argv[0], libnetapi_get_error_string(c->netapi_ctx,
907 d_printf(_("Added user '%s'.\n"), argv[0]);
914 * Rename a user on a remote RPC server.
916 * @param argc Standard main() style argc.
917 * @param argv Standard main() style argv. Initial components are already
920 * @return A shell status integer (0 for success).
923 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
925 NET_API_STATUS status;
926 struct USER_INFO_0 u0;
927 uint32_t parm_err = 0;
929 if (argc != 2 || c->display_usage) {
930 rpc_user_usage(c, argc, argv);
934 u0.usri0_name = argv[1];
936 status = NetUserSetInfo(c->opt_host, argv[0],
937 0, (uint8_t *)&u0, &parm_err);
940 _("Failed to rename user from %s to %s - %s\n"),
942 libnetapi_get_error_string(c->netapi_ctx, status));
944 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
951 * Set a user's primary group
953 * @param argc Standard main() style argc.
954 * @param argv Standard main() style argv. Initial components are already
957 * @return A shell status integer (0 for success).
960 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
963 NET_API_STATUS status;
965 struct GROUP_INFO_2 *g2;
966 struct USER_INFO_1051 u1051;
967 uint32_t parm_err = 0;
969 if (argc != 2 || c->display_usage) {
970 rpc_user_usage(c, argc, argv);
974 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
976 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
978 libnetapi_get_error_string(c->netapi_ctx, status));
981 g2 = (struct GROUP_INFO_2 *)buffer;
983 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
985 NetApiBufferFree(buffer);
987 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
988 (uint8_t *)&u1051, &parm_err);
991 _("Failed to set user's primary group %s to %s - "
992 "%s\n"), argv[0], argv[1],
993 libnetapi_get_error_string(c->netapi_ctx, status));
995 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
1002 * Delete a user from a remote RPC server.
1004 * @param argc Standard main() style argc.
1005 * @param argv Standard main() style argv. Initial components are already
1008 * @return A shell status integer (0 for success).
1011 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
1013 NET_API_STATUS status;
1015 if (argc < 1 || c->display_usage) {
1016 rpc_user_usage(c, argc, argv);
1020 status = NetUserDel(c->opt_host, argv[0]);
1023 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
1025 libnetapi_get_error_string(c->netapi_ctx, status));
1028 d_printf(_("Deleted user '%s'.\n"), argv[0]);
1035 * Set a user's password on a remote RPC server.
1037 * @param argc Standard main() style argc.
1038 * @param argv Standard main() style argv. Initial components are already
1041 * @return A shell status integer (0 for success).
1044 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
1046 NET_API_STATUS status;
1047 char *prompt = NULL;
1048 struct USER_INFO_1003 u1003;
1049 uint32_t parm_err = 0;
1052 if (argc < 1 || c->display_usage) {
1053 rpc_user_usage(c, argc, argv);
1058 u1003.usri1003_password = argv[1];
1060 char pwd[256] = {0};
1061 ret = asprintf(&prompt, _("Enter new password for %s:"),
1067 ret = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
1073 u1003.usri1003_password = talloc_strdup(c, pwd);
1074 if (u1003.usri1003_password == NULL) {
1079 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
1081 /* Display results */
1084 _("Failed to set password for '%s' with error: %s.\n"),
1085 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1094 * List a user's groups from a remote RPC server.
1096 * @param argc Standard main() style argc.
1097 * @param argv Standard main() style argv. Initial components are already
1100 * @return A shell status integer (0 for success)
1103 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1106 NET_API_STATUS status;
1107 struct GROUP_USERS_INFO_0 *u0 = NULL;
1108 uint32_t entries_read = 0;
1109 uint32_t total_entries = 0;
1113 if (argc < 1 || c->display_usage) {
1114 rpc_user_usage(c, argc, argv);
1118 status = NetUserGetGroups(c->opt_host,
1121 (uint8_t **)(void *)&u0,
1127 _("Failed to get groups for '%s' with error: %s.\n"),
1128 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1133 for (i=0; i < entries_read; i++) {
1134 printf("%s\n", u0->grui0_name);
1142 * List users on a remote RPC server.
1144 * All parameters are provided by the run_rpc_command function, except for
1145 * argc, argv which are passed through.
1147 * @param domain_sid The domain sid acquired from the remote server.
1148 * @param cli A cli_state connected to the server.
1149 * @param mem_ctx Talloc context, destroyed on completion of the function.
1150 * @param argc Standard main() style argc.
1151 * @param argv Standard main() style argv. Initial components are already
1154 * @return Normal NTSTATUS return.
1157 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
1159 NET_API_STATUS status;
1160 uint32_t start_idx=0, num_entries, i, loop_count = 0;
1161 struct NET_DISPLAY_USER *info = NULL;
1162 void *buffer = NULL;
1164 /* Query domain users */
1165 if (c->opt_long_list_entries)
1166 d_printf(_("\nUser name Comment"
1167 "\n-----------------------------\n"));
1169 uint32_t max_entries, max_size;
1171 dcerpc_get_query_dispinfo_params(
1172 loop_count, &max_entries, &max_size);
1174 status = NetQueryDisplayInformation(c->opt_host,
1181 if (status != 0 && status != ERROR_MORE_DATA) {
1185 info = (struct NET_DISPLAY_USER *)buffer;
1187 for (i = 0; i < num_entries; i++) {
1189 if (c->opt_long_list_entries)
1190 printf("%-21.21s %s\n", info->usri1_name,
1191 info->usri1_comment);
1193 printf("%s\n", info->usri1_name);
1197 NetApiBufferFree(buffer);
1200 start_idx += num_entries;
1202 } while (status == ERROR_MORE_DATA);
1208 * 'net rpc user' entrypoint.
1209 * @param argc Standard main() style argc.
1210 * @param argv Standard main() style argv. Initial components are already
1214 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1216 NET_API_STATUS status;
1218 struct functable func[] = {
1223 N_("Add specified user"),
1224 N_("net rpc user add\n"
1225 " Add specified user")
1231 N_("List domain groups of user"),
1232 N_("net rpc user info\n"
1233 " List domain groups of user")
1239 N_("Remove specified user"),
1240 N_("net rpc user delete\n"
1241 " Remove specified user")
1247 N_("Change user password"),
1248 N_("net rpc user password\n"
1249 " Change user password")
1255 N_("Rename specified user"),
1256 N_("net rpc user rename\n"
1257 " Rename specified user")
1261 rpc_user_setprimarygroup,
1263 "Set a user's primary group",
1264 "net rpc user setprimarygroup\n"
1265 " Set a user's primary group"
1267 {NULL, NULL, 0, NULL, NULL}
1270 status = libnetapi_net_init(&c->netapi_ctx);
1274 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1275 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1276 if (c->opt_kerberos) {
1277 libnetapi_set_use_kerberos(c->netapi_ctx);
1281 if (c->display_usage) {
1286 _("List all users"));
1287 net_display_usage_from_functable(func);
1291 return rpc_user_list(c, argc, argv);
1294 return net_run_function(c, argc, argv, "net rpc user", func);
1297 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1298 TALLOC_CTX *mem_ctx,
1299 struct rpc_sh_ctx *ctx,
1300 struct rpc_pipe_client *pipe_hnd,
1301 int argc, const char **argv)
1303 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1306 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1307 TALLOC_CTX *mem_ctx,
1308 struct rpc_sh_ctx *ctx,
1309 struct rpc_pipe_client *pipe_hnd,
1310 int argc, const char **argv)
1312 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1315 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1316 TALLOC_CTX *mem_ctx,
1317 struct rpc_sh_ctx *ctx,
1318 struct rpc_pipe_client *pipe_hnd,
1319 int argc, const char **argv,
1321 struct net_context *c,
1322 TALLOC_CTX *mem_ctx,
1323 struct rpc_sh_ctx *ctx,
1324 struct rpc_pipe_client *pipe_hnd,
1325 struct policy_handle *user_hnd,
1326 int argc, const char **argv))
1328 struct policy_handle connect_pol, domain_pol, user_pol;
1329 NTSTATUS status, result;
1332 enum lsa_SidType type;
1333 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1336 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1338 return NT_STATUS_INVALID_PARAMETER;
1341 ZERO_STRUCT(connect_pol);
1342 ZERO_STRUCT(domain_pol);
1343 ZERO_STRUCT(user_pol);
1345 status = net_rpc_lookup_name(c, mem_ctx, ctx->cli,
1346 argv[0], NULL, NULL, &sid, &type);
1347 if (!NT_STATUS_IS_OK(status)) {
1348 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1353 if (type != SID_NAME_USER) {
1354 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1355 sid_type_lookup(type));
1356 status = NT_STATUS_NO_SUCH_USER;
1360 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1361 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1362 status = NT_STATUS_NO_SUCH_USER;
1366 status = dcerpc_samr_Connect2(b, mem_ctx,
1368 MAXIMUM_ALLOWED_ACCESS,
1371 if (!NT_STATUS_IS_OK(status)) {
1374 if (!NT_STATUS_IS_OK(result)) {
1379 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1381 MAXIMUM_ALLOWED_ACCESS,
1385 if (!NT_STATUS_IS_OK(status)) {
1388 if (!NT_STATUS_IS_OK(result)) {
1393 status = dcerpc_samr_OpenUser(b, mem_ctx,
1395 MAXIMUM_ALLOWED_ACCESS,
1399 if (!NT_STATUS_IS_OK(status)) {
1402 if (!NT_STATUS_IS_OK(result)) {
1407 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1410 if (is_valid_policy_hnd(&user_pol)) {
1411 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1413 if (is_valid_policy_hnd(&domain_pol)) {
1414 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1416 if (is_valid_policy_hnd(&connect_pol)) {
1417 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1422 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1423 TALLOC_CTX *mem_ctx,
1424 struct rpc_sh_ctx *ctx,
1425 struct rpc_pipe_client *pipe_hnd,
1426 struct policy_handle *user_hnd,
1427 int argc, const char **argv)
1429 NTSTATUS status, result;
1430 union samr_UserInfo *info = NULL;
1431 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1434 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1436 return NT_STATUS_INVALID_PARAMETER;
1439 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1444 if (!NT_STATUS_IS_OK(status)) {
1447 if (!NT_STATUS_IS_OK(result)) {
1451 d_printf(_("user rid: %d, group rid: %d\n"),
1453 info->info21.primary_gid);
1458 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1459 TALLOC_CTX *mem_ctx,
1460 struct rpc_sh_ctx *ctx,
1461 struct rpc_pipe_client *pipe_hnd,
1462 int argc, const char **argv)
1464 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1465 rpc_sh_user_show_internals);
1468 #define FETCHSTR(name, rec) \
1469 do { if (strequal(ctx->thiscmd, name)) { \
1470 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1473 #define SETSTR(name, rec, flag) \
1474 do { if (strequal(ctx->thiscmd, name)) { \
1475 init_lsa_String(&(info->info21.rec), argv[0]); \
1476 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1479 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1480 TALLOC_CTX *mem_ctx,
1481 struct rpc_sh_ctx *ctx,
1482 struct rpc_pipe_client *pipe_hnd,
1483 struct policy_handle *user_hnd,
1484 int argc, const char **argv)
1486 NTSTATUS status, result;
1487 const char *username;
1488 const char *oldval = "";
1489 union samr_UserInfo *info = NULL;
1490 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1493 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1494 _("Usage:"), ctx->whoami);
1495 return NT_STATUS_INVALID_PARAMETER;
1498 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1503 if (!NT_STATUS_IS_OK(status)) {
1506 if (!NT_STATUS_IS_OK(result)) {
1510 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1512 FETCHSTR("fullname", full_name);
1513 FETCHSTR("homedir", home_directory);
1514 FETCHSTR("homedrive", home_drive);
1515 FETCHSTR("logonscript", logon_script);
1516 FETCHSTR("profilepath", profile_path);
1517 FETCHSTR("description", description);
1520 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1524 if (strcmp(argv[0], "NULL") == 0) {
1528 ZERO_STRUCT(info->info21);
1530 SETSTR("fullname", full_name, FULL_NAME);
1531 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1532 SETSTR("homedrive", home_drive, HOME_DRIVE);
1533 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1534 SETSTR("profilepath", profile_path, PROFILE_PATH);
1535 SETSTR("description", description, DESCRIPTION);
1537 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1542 if (!NT_STATUS_IS_OK(status)) {
1548 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1549 ctx->thiscmd, oldval, argv[0]);
1556 #define HANDLEFLG(name, rec) \
1557 do { if (strequal(ctx->thiscmd, name)) { \
1558 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1560 newflags = oldflags | ACB_##rec; \
1562 newflags = oldflags & ~ACB_##rec; \
1565 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1566 TALLOC_CTX *mem_ctx,
1567 struct rpc_sh_ctx *ctx,
1568 struct rpc_pipe_client *pipe_hnd,
1569 int argc, const char **argv)
1571 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1572 rpc_sh_user_str_edit_internals);
1575 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1576 TALLOC_CTX *mem_ctx,
1577 struct rpc_sh_ctx *ctx,
1578 struct rpc_pipe_client *pipe_hnd,
1579 struct policy_handle *user_hnd,
1580 int argc, const char **argv)
1582 NTSTATUS status, result;
1583 const char *username;
1584 const char *oldval = "unknown";
1585 uint32 oldflags, newflags;
1587 union samr_UserInfo *info = NULL;
1588 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1591 ((argc == 1) && !strequal(argv[0], "yes") &&
1592 !strequal(argv[0], "no"))) {
1593 /* TRANSATORS: The yes|no here are program keywords. Please do
1595 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1597 return NT_STATUS_INVALID_PARAMETER;
1600 newval = strequal(argv[0], "yes");
1602 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1607 if (!NT_STATUS_IS_OK(status)) {
1610 if (!NT_STATUS_IS_OK(result)) {
1614 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1615 oldflags = info->info21.acct_flags;
1616 newflags = info->info21.acct_flags;
1618 HANDLEFLG("disabled", DISABLED);
1619 HANDLEFLG("pwnotreq", PWNOTREQ);
1620 HANDLEFLG("autolock", AUTOLOCK);
1621 HANDLEFLG("pwnoexp", PWNOEXP);
1624 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1629 ZERO_STRUCT(info->info21);
1631 info->info21.acct_flags = newflags;
1632 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1634 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1639 if (!NT_STATUS_IS_OK(status)) {
1643 if (NT_STATUS_IS_OK(result)) {
1644 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1645 ctx->thiscmd, oldval, argv[0]);
1653 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1654 TALLOC_CTX *mem_ctx,
1655 struct rpc_sh_ctx *ctx,
1656 struct rpc_pipe_client *pipe_hnd,
1657 int argc, const char **argv)
1659 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1660 rpc_sh_user_flag_edit_internals);
1663 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1664 TALLOC_CTX *mem_ctx,
1665 struct rpc_sh_ctx *ctx)
1667 static struct rpc_sh_cmd cmds[] = {
1669 { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1670 N_("Show/Set a user's full name") },
1672 { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1673 N_("Show/Set a user's home directory") },
1675 { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1676 N_("Show/Set a user's home drive") },
1678 { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1679 N_("Show/Set a user's logon script") },
1681 { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1682 N_("Show/Set a user's profile path") },
1684 { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1685 N_("Show/Set a user's description") },
1687 { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1688 N_("Show/Set whether a user is disabled") },
1690 { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1691 N_("Show/Set whether a user locked out") },
1693 { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1694 N_("Show/Set whether a user does not need a password") },
1696 { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1697 N_("Show/Set whether a user's password does not expire") },
1699 { NULL, NULL, 0, NULL, NULL }
1705 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1706 TALLOC_CTX *mem_ctx,
1707 struct rpc_sh_ctx *ctx)
1709 static struct rpc_sh_cmd cmds[] = {
1711 { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
1712 N_("List available users") },
1714 { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
1715 N_("List the domain groups a user is member of") },
1717 { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
1718 N_("Show info about a user") },
1720 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1721 N_("Show/Modify a user's fields") },
1723 { NULL, NULL, 0, NULL, NULL }
1729 /****************************************************************************/
1732 * Basic usage function for 'net rpc group'.
1733 * @param argc Standard main() style argc.
1734 * @param argv Standard main() style argv. Initial components are already
1738 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1740 return net_group_usage(c, argc, argv);
1744 * Delete group on a remote RPC server.
1746 * All parameters are provided by the run_rpc_command function, except for
1747 * argc, argv which are passed through.
1749 * @param domain_sid The domain sid acquired from the remote server.
1750 * @param cli A cli_state connected to the server.
1751 * @param mem_ctx Talloc context, destroyed on completion of the function.
1752 * @param argc Standard main() style argc.
1753 * @param argv Standard main() style argv. Initial components are already
1756 * @return Normal NTSTATUS return.
1759 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1760 const struct dom_sid *domain_sid,
1761 const char *domain_name,
1762 struct cli_state *cli,
1763 struct rpc_pipe_client *pipe_hnd,
1764 TALLOC_CTX *mem_ctx,
1768 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1769 bool group_is_primary = false;
1770 NTSTATUS status, result;
1772 struct samr_RidAttrArray *rids = NULL;
1775 /* struct samr_RidWithAttribute *user_gids; */
1776 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1778 struct samr_Ids group_rids, name_types;
1779 struct lsa_String lsa_acct_name;
1780 union samr_UserInfo *info = NULL;
1782 if (argc < 1 || c->display_usage) {
1783 rpc_group_usage(c, argc,argv);
1784 return NT_STATUS_OK; /* ok? */
1787 status = dcerpc_samr_Connect2(b, mem_ctx,
1789 MAXIMUM_ALLOWED_ACCESS,
1792 if (!NT_STATUS_IS_OK(status)) {
1793 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1797 if (!NT_STATUS_IS_OK(result)) {
1799 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1803 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1805 MAXIMUM_ALLOWED_ACCESS,
1806 discard_const_p(struct dom_sid2, domain_sid),
1809 if (!NT_STATUS_IS_OK(status)) {
1810 d_fprintf(stderr, _("Request open_domain failed\n"));
1814 if (!NT_STATUS_IS_OK(result)) {
1816 d_fprintf(stderr, _("Request open_domain failed\n"));
1820 init_lsa_String(&lsa_acct_name, argv[0]);
1822 status = dcerpc_samr_LookupNames(b, mem_ctx,
1829 if (!NT_STATUS_IS_OK(status)) {
1830 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1834 if (!NT_STATUS_IS_OK(result)) {
1836 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1839 if (group_rids.count != 1) {
1840 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1843 if (name_types.count != 1) {
1844 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1848 switch (name_types.ids[0])
1850 case SID_NAME_DOM_GRP:
1851 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1853 MAXIMUM_ALLOWED_ACCESS,
1857 if (!NT_STATUS_IS_OK(status)) {
1858 d_fprintf(stderr, _("Request open_group failed"));
1862 if (!NT_STATUS_IS_OK(result)) {
1864 d_fprintf(stderr, _("Request open_group failed"));
1868 group_rid = group_rids.ids[0];
1870 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1874 if (!NT_STATUS_IS_OK(status)) {
1876 _("Unable to query group members of %s"),
1881 if (!NT_STATUS_IS_OK(result)) {
1884 _("Unable to query group members of %s"),
1889 if (c->opt_verbose) {
1891 _("Domain Group %s (rid: %d) has %d members\n"),
1892 argv[0],group_rid, rids->count);
1895 /* Check if group is anyone's primary group */
1896 for (i = 0; i < rids->count; i++)
1898 status = dcerpc_samr_OpenUser(b, mem_ctx,
1900 MAXIMUM_ALLOWED_ACCESS,
1904 if (!NT_STATUS_IS_OK(status)) {
1906 _("Unable to open group member %d\n"),
1911 if (!NT_STATUS_IS_OK(result)) {
1914 _("Unable to open group member %d\n"),
1919 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1924 if (!NT_STATUS_IS_OK(status)) {
1926 _("Unable to lookup userinfo for group "
1932 if (!NT_STATUS_IS_OK(result)) {
1935 _("Unable to lookup userinfo for group "
1941 if (info->info21.primary_gid == group_rid) {
1942 if (c->opt_verbose) {
1943 d_printf(_("Group is primary group "
1945 info->info21.account_name.string);
1947 group_is_primary = true;
1950 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1953 if (group_is_primary) {
1954 d_fprintf(stderr, _("Unable to delete group because "
1955 "some of it's members have it as primary "
1957 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1961 /* remove all group members */
1962 for (i = 0; i < rids->count; i++)
1965 d_printf(_("Remove group member %d..."),
1967 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1971 if (!NT_STATUS_IS_OK(status)) {
1975 if (NT_STATUS_IS_OK(result)) {
1977 d_printf(_("ok\n"));
1980 d_printf("%s\n", _("failed"));
1985 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1988 if (!NT_STATUS_IS_OK(status)) {
1995 /* removing a local group is easier... */
1996 case SID_NAME_ALIAS:
1997 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1999 MAXIMUM_ALLOWED_ACCESS,
2003 if (!NT_STATUS_IS_OK(status)) {
2004 d_fprintf(stderr, _("Request open_alias failed\n"));
2007 if (!NT_STATUS_IS_OK(result)) {
2009 d_fprintf(stderr, _("Request open_alias failed\n"));
2013 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
2016 if (!NT_STATUS_IS_OK(status)) {
2024 d_fprintf(stderr, _("%s is of type %s. This command is only "
2025 "for deleting local or global groups\n"),
2026 argv[0],sid_type_lookup(name_types.ids[0]));
2027 status = NT_STATUS_UNSUCCESSFUL;
2031 if (NT_STATUS_IS_OK(status)) {
2033 d_printf(_("Deleted %s '%s'\n"),
2034 sid_type_lookup(name_types.ids[0]), argv[0]);
2036 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
2037 get_friendly_nt_error_msg(status));
2045 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
2047 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2048 rpc_group_delete_internals, argc,argv);
2051 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
2053 NET_API_STATUS status;
2054 struct GROUP_INFO_1 info1;
2055 uint32_t parm_error = 0;
2057 if (argc != 1 || c->display_usage) {
2058 rpc_group_usage(c, argc, argv);
2064 info1.grpi1_name = argv[0];
2065 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2066 info1.grpi1_comment = c->opt_comment;
2069 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2073 _("Failed to add group '%s' with error: %s.\n"),
2074 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2078 d_printf(_("Added group '%s'.\n"), argv[0]);
2084 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
2086 NET_API_STATUS status;
2087 struct LOCALGROUP_INFO_1 info1;
2088 uint32_t parm_error = 0;
2090 if (argc != 1 || c->display_usage) {
2091 rpc_group_usage(c, argc, argv);
2097 info1.lgrpi1_name = argv[0];
2098 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2099 info1.lgrpi1_comment = c->opt_comment;
2102 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2106 _("Failed to add alias '%s' with error: %s.\n"),
2107 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2111 d_printf(_("Added alias '%s'.\n"), argv[0]);
2117 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
2119 if (c->opt_localgroup)
2120 return rpc_alias_add_internals(c, argc, argv);
2122 return rpc_group_add_internals(c, argc, argv);
2125 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2126 TALLOC_CTX *mem_ctx,
2128 struct dom_sid *sid,
2129 enum lsa_SidType *type)
2131 struct dom_sid *sids = NULL;
2132 enum lsa_SidType *types = NULL;
2133 struct rpc_pipe_client *pipe_hnd = NULL;
2134 struct policy_handle lsa_pol;
2135 NTSTATUS status, result;
2136 struct dcerpc_binding_handle *b;
2138 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
2140 if (!NT_STATUS_IS_OK(status)) {
2144 b = pipe_hnd->binding_handle;
2146 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2147 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2149 if (!NT_STATUS_IS_OK(status)) {
2153 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2154 &name, NULL, 1, &sids, &types);
2156 if (NT_STATUS_IS_OK(status)) {
2157 sid_copy(sid, &sids[0]);
2161 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
2165 TALLOC_FREE(pipe_hnd);
2168 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
2170 /* Try as S-1-5-whatever */
2172 struct dom_sid tmp_sid;
2174 if (string_to_sid(&tmp_sid, name)) {
2175 sid_copy(sid, &tmp_sid);
2176 *type = SID_NAME_UNKNOWN;
2177 status = NT_STATUS_OK;
2184 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2185 TALLOC_CTX *mem_ctx,
2186 const struct dom_sid *group_sid,
2189 struct policy_handle connect_pol, domain_pol;
2190 NTSTATUS status, result;
2192 struct policy_handle group_pol;
2193 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2195 struct samr_Ids rids, rid_types;
2196 struct lsa_String lsa_acct_name;
2200 sid_copy(&sid, group_sid);
2202 if (!sid_split_rid(&sid, &group_rid)) {
2203 return NT_STATUS_UNSUCCESSFUL;
2206 /* Get sam policy handle */
2207 status = dcerpc_samr_Connect2(b, mem_ctx,
2209 MAXIMUM_ALLOWED_ACCESS,
2212 if (!NT_STATUS_IS_OK(status)) {
2215 if (!NT_STATUS_IS_OK(result)) {
2219 /* Get domain policy handle */
2220 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2222 MAXIMUM_ALLOWED_ACCESS,
2226 if (!NT_STATUS_IS_OK(status)) {
2229 if (!NT_STATUS_IS_OK(result)) {
2233 init_lsa_String(&lsa_acct_name, member);
2235 status = dcerpc_samr_LookupNames(b, mem_ctx,
2242 if (!NT_STATUS_IS_OK(status)) {
2243 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2248 if (!NT_STATUS_IS_OK(result)) {
2250 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2254 if (rids.count != 1) {
2255 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2258 if (rid_types.count != 1) {
2259 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2263 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2265 MAXIMUM_ALLOWED_ACCESS,
2269 if (!NT_STATUS_IS_OK(status)) {
2273 if (!NT_STATUS_IS_OK(result)) {
2278 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2281 0x0005, /* unknown flags */
2283 if (!NT_STATUS_IS_OK(status)) {
2290 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2294 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2295 struct cli_state *cli,
2296 TALLOC_CTX *mem_ctx,
2297 const struct dom_sid *alias_sid,
2300 struct policy_handle connect_pol, domain_pol;
2301 NTSTATUS status, result;
2303 struct policy_handle alias_pol;
2304 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2306 struct dom_sid member_sid;
2307 enum lsa_SidType member_type;
2311 sid_copy(&sid, alias_sid);
2313 if (!sid_split_rid(&sid, &alias_rid)) {
2314 return NT_STATUS_UNSUCCESSFUL;
2317 result = get_sid_from_name(cli, mem_ctx,
2318 member, &member_sid, &member_type);
2320 if (!NT_STATUS_IS_OK(result)) {
2321 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2326 /* Get sam policy handle */
2327 status = dcerpc_samr_Connect2(b, mem_ctx,
2329 MAXIMUM_ALLOWED_ACCESS,
2332 if (!NT_STATUS_IS_OK(status)) {
2335 if (!NT_STATUS_IS_OK(result)) {
2340 /* Get domain policy handle */
2341 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2343 MAXIMUM_ALLOWED_ACCESS,
2347 if (!NT_STATUS_IS_OK(status)) {
2350 if (!NT_STATUS_IS_OK(result)) {
2355 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2357 MAXIMUM_ALLOWED_ACCESS,
2361 if (!NT_STATUS_IS_OK(status)) {
2364 if (!NT_STATUS_IS_OK(result)) {
2368 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2372 if (!NT_STATUS_IS_OK(status)) {
2379 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2383 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2384 const struct dom_sid *domain_sid,
2385 const char *domain_name,
2386 struct cli_state *cli,
2387 struct rpc_pipe_client *pipe_hnd,
2388 TALLOC_CTX *mem_ctx,
2392 struct dom_sid group_sid;
2393 enum lsa_SidType group_type;
2395 if (argc != 2 || c->display_usage) {
2398 _("net rpc group addmem <group> <member>\n"
2399 " Add a member to a group\n"
2400 " group\tGroup to add member to\n"
2401 " member\tMember to add to group\n"));
2402 return NT_STATUS_UNSUCCESSFUL;
2405 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2406 &group_sid, &group_type))) {
2407 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2409 return NT_STATUS_UNSUCCESSFUL;
2412 if (group_type == SID_NAME_DOM_GRP) {
2413 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2414 &group_sid, argv[1]);
2416 if (!NT_STATUS_IS_OK(result)) {
2417 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2418 argv[1], argv[0], nt_errstr(result));
2423 if (group_type == SID_NAME_ALIAS) {
2424 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, cli, mem_ctx,
2425 &group_sid, argv[1]);
2427 if (!NT_STATUS_IS_OK(result)) {
2428 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2429 argv[1], argv[0], nt_errstr(result));
2434 d_fprintf(stderr, _("Can only add members to global or local groups "
2435 "which %s is not\n"), argv[0]);
2437 return NT_STATUS_UNSUCCESSFUL;
2440 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2442 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2443 rpc_group_addmem_internals,
2447 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2448 struct rpc_pipe_client *pipe_hnd,
2449 TALLOC_CTX *mem_ctx,
2450 const struct dom_sid *group_sid,
2453 struct policy_handle connect_pol, domain_pol;
2454 NTSTATUS status, result;
2456 struct policy_handle group_pol;
2457 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2459 struct samr_Ids rids, rid_types;
2460 struct lsa_String lsa_acct_name;
2464 sid_copy(&sid, group_sid);
2466 if (!sid_split_rid(&sid, &group_rid))
2467 return NT_STATUS_UNSUCCESSFUL;
2469 /* Get sam policy handle */
2470 status = dcerpc_samr_Connect2(b, mem_ctx,
2472 MAXIMUM_ALLOWED_ACCESS,
2475 if (!NT_STATUS_IS_OK(status)) {
2478 if (!NT_STATUS_IS_OK(result)) {
2483 /* Get domain policy handle */
2484 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2486 MAXIMUM_ALLOWED_ACCESS,
2490 if (!NT_STATUS_IS_OK(status)) {
2493 if (!NT_STATUS_IS_OK(result)) {
2497 init_lsa_String(&lsa_acct_name, member);
2499 status = dcerpc_samr_LookupNames(b, mem_ctx,
2506 if (!NT_STATUS_IS_OK(status)) {
2507 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2512 if (!NT_STATUS_IS_OK(result)) {
2514 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2518 if (rids.count != 1) {
2519 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2522 if (rid_types.count != 1) {
2523 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2527 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2529 MAXIMUM_ALLOWED_ACCESS,
2533 if (!NT_STATUS_IS_OK(status)) {
2536 if (!NT_STATUS_IS_OK(result)) {
2541 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2545 if (!NT_STATUS_IS_OK(status)) {
2551 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2555 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2556 struct cli_state *cli,
2557 TALLOC_CTX *mem_ctx,
2558 const struct dom_sid *alias_sid,
2561 struct policy_handle connect_pol, domain_pol;
2562 NTSTATUS status, result;
2564 struct policy_handle alias_pol;
2565 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2567 struct dom_sid member_sid;
2568 enum lsa_SidType member_type;
2572 sid_copy(&sid, alias_sid);
2574 if (!sid_split_rid(&sid, &alias_rid))
2575 return NT_STATUS_UNSUCCESSFUL;
2577 result = get_sid_from_name(cli, mem_ctx,
2578 member, &member_sid, &member_type);
2580 if (!NT_STATUS_IS_OK(result)) {
2581 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2586 /* Get sam policy handle */
2587 status = dcerpc_samr_Connect2(b, mem_ctx,
2589 MAXIMUM_ALLOWED_ACCESS,
2592 if (!NT_STATUS_IS_OK(status)) {
2595 if (!NT_STATUS_IS_OK(result)) {
2600 /* Get domain policy handle */
2601 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2603 MAXIMUM_ALLOWED_ACCESS,
2607 if (!NT_STATUS_IS_OK(status)) {
2610 if (!NT_STATUS_IS_OK(result)) {
2615 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2617 MAXIMUM_ALLOWED_ACCESS,
2621 if (!NT_STATUS_IS_OK(status)) {
2625 if (!NT_STATUS_IS_OK(result)) {
2629 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2634 if (!NT_STATUS_IS_OK(status)) {
2641 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2645 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2646 const struct dom_sid *domain_sid,
2647 const char *domain_name,
2648 struct cli_state *cli,
2649 struct rpc_pipe_client *pipe_hnd,
2650 TALLOC_CTX *mem_ctx,
2654 struct dom_sid group_sid;
2655 enum lsa_SidType group_type;
2657 if (argc != 2 || c->display_usage) {
2660 _("net rpc group delmem <group> <member>\n"
2661 " Delete a member from a group\n"
2662 " group\tGroup to delete member from\n"
2663 " member\tMember to delete from group\n"));
2664 return NT_STATUS_UNSUCCESSFUL;
2667 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2668 &group_sid, &group_type))) {
2669 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2671 return NT_STATUS_UNSUCCESSFUL;
2674 if (group_type == SID_NAME_DOM_GRP) {
2675 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2676 &group_sid, argv[1]);
2678 if (!NT_STATUS_IS_OK(result)) {
2679 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2680 argv[1], argv[0], nt_errstr(result));
2685 if (group_type == SID_NAME_ALIAS) {
2686 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, cli, mem_ctx,
2687 &group_sid, argv[1]);
2689 if (!NT_STATUS_IS_OK(result)) {
2690 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2691 argv[1], argv[0], nt_errstr(result));
2696 d_fprintf(stderr, _("Can only delete members from global or local "
2697 "groups which %s is not\n"), argv[0]);
2699 return NT_STATUS_UNSUCCESSFUL;
2702 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2704 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2705 rpc_group_delmem_internals,
2710 * List groups on a remote RPC server.
2712 * All parameters are provided by the run_rpc_command function, except for
2713 * argc, argv which are passes through.
2715 * @param domain_sid The domain sid acquired from the remote server.
2716 * @param cli A cli_state connected to the server.
2717 * @param mem_ctx Talloc context, destroyed on completion of the function.
2718 * @param argc Standard main() style argc.
2719 * @param argv Standard main() style argv. Initial components are already
2722 * @return Normal NTSTATUS return.
2725 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2726 const struct dom_sid *domain_sid,
2727 const char *domain_name,
2728 struct cli_state *cli,
2729 struct rpc_pipe_client *pipe_hnd,
2730 TALLOC_CTX *mem_ctx,
2734 struct policy_handle connect_pol, domain_pol;
2735 NTSTATUS status, result;
2736 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2737 struct samr_SamArray *groups = NULL;
2738 bool global = false;
2740 bool builtin = false;
2741 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2743 if (c->display_usage) {
2746 _("net rpc group list [global] [local] [builtin]\n"
2747 " List groups on RPC server\n"
2748 " global\tList global groups\n"
2749 " local\tList local groups\n"
2750 " builtin\tList builtin groups\n"
2751 " If none of global, local or builtin is "
2752 "specified, all three options are considered "
2754 return NT_STATUS_OK;
2763 for (i=0; i<argc; i++) {
2764 if (strequal(argv[i], "global"))
2767 if (strequal(argv[i], "local"))
2770 if (strequal(argv[i], "builtin"))
2774 /* Get sam policy handle */
2776 status = dcerpc_samr_Connect2(b, mem_ctx,
2778 MAXIMUM_ALLOWED_ACCESS,
2781 if (!NT_STATUS_IS_OK(status)) {
2784 if (!NT_STATUS_IS_OK(result)) {
2789 /* Get domain policy handle */
2791 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2793 MAXIMUM_ALLOWED_ACCESS,
2794 discard_const_p(struct dom_sid2, domain_sid),
2797 if (!NT_STATUS_IS_OK(status)) {
2800 if (!NT_STATUS_IS_OK(result)) {
2805 /* Query domain groups */
2806 if (c->opt_long_list_entries)
2807 d_printf(_("\nGroup name Comment"
2808 "\n-----------------------------\n"));
2810 uint32_t max_size, total_size, returned_size;
2811 union samr_DispInfo info;
2815 dcerpc_get_query_dispinfo_params(
2816 loop_count, &max_entries, &max_size);
2818 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2828 if (!NT_STATUS_IS_OK(status)) {
2831 num_entries = info.info3.count;
2832 start_idx += info.info3.count;
2834 if (!NT_STATUS_IS_OK(result) &&
2835 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2838 for (i = 0; i < num_entries; i++) {
2840 const char *group = NULL;
2841 const char *desc = NULL;
2843 group = info.info3.entries[i].account_name.string;
2844 desc = info.info3.entries[i].description.string;
2846 if (c->opt_long_list_entries)
2847 printf("%-21.21s %-50.50s\n",
2850 printf("%s\n", group);
2852 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2853 /* query domain aliases */
2858 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2865 if (!NT_STATUS_IS_OK(status)) {
2868 if (!NT_STATUS_IS_OK(result) &&
2869 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2872 for (i = 0; i < num_entries; i++) {
2874 const char *description = NULL;
2876 if (c->opt_long_list_entries) {
2878 struct policy_handle alias_pol;
2879 union samr_AliasInfo *info = NULL;
2882 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2885 groups->entries[i].idx,
2888 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2889 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2894 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2895 status = dcerpc_samr_Close(b, mem_ctx,
2898 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2899 description = info->description.string;
2905 if (description != NULL) {
2906 printf("%-21.21s %-50.50s\n",
2907 groups->entries[i].name.string,
2910 printf("%s\n", groups->entries[i].name.string);
2913 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2914 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2915 /* Get builtin policy handle */
2917 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2919 MAXIMUM_ALLOWED_ACCESS,
2920 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2923 if (!NT_STATUS_IS_OK(status)) {
2926 if (!NT_STATUS_IS_OK(result)) {
2931 /* query builtin aliases */
2934 if (!builtin) break;
2936 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2943 if (!NT_STATUS_IS_OK(status)) {
2946 if (!NT_STATUS_IS_OK(result) &&
2947 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2952 for (i = 0; i < num_entries; i++) {
2954 const char *description = NULL;
2956 if (c->opt_long_list_entries) {
2958 struct policy_handle alias_pol;
2959 union samr_AliasInfo *info = NULL;
2962 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2965 groups->entries[i].idx,
2968 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2969 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2974 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2975 status = dcerpc_samr_Close(b, mem_ctx,
2978 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2979 description = info->description.string;
2985 if (description != NULL) {
2986 printf("%-21.21s %-50.50s\n",
2987 groups->entries[i].name.string,
2990 printf("%s\n", groups->entries[i].name.string);
2993 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3001 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
3003 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3004 rpc_group_list_internals,
3008 static NTSTATUS rpc_list_group_members(struct net_context *c,
3009 struct rpc_pipe_client *pipe_hnd,
3010 TALLOC_CTX *mem_ctx,
3011 const char *domain_name,
3012 const struct dom_sid *domain_sid,
3013 struct policy_handle *domain_pol,
3016 NTSTATUS result, status;
3017 struct policy_handle group_pol;
3018 uint32 num_members, *group_rids;
3020 struct samr_RidAttrArray *rids = NULL;
3021 struct lsa_Strings names;
3022 struct samr_Ids types;
3023 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3026 sid_to_fstring(sid_str, domain_sid);
3028 status = dcerpc_samr_OpenGroup(b, mem_ctx,
3030 MAXIMUM_ALLOWED_ACCESS,
3034 if (!NT_STATUS_IS_OK(status)) {
3037 if (!NT_STATUS_IS_OK(result)) {
3041 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
3045 if (!NT_STATUS_IS_OK(status)) {
3048 if (!NT_STATUS_IS_OK(result)) {
3052 num_members = rids->count;
3053 group_rids = rids->rids;
3055 while (num_members > 0) {
3056 int this_time = 512;
3058 if (num_members < this_time)
3059 this_time = num_members;
3061 status = dcerpc_samr_LookupRids(b, mem_ctx,
3068 if (!NT_STATUS_IS_OK(status)) {
3071 if (!NT_STATUS_IS_OK(result)) {
3075 /* We only have users as members, but make the output
3076 the same as the output of alias members */
3078 for (i = 0; i < this_time; i++) {
3080 if (c->opt_long_list_entries) {
3081 printf("%s-%d %s\\%s %d\n", sid_str,
3082 group_rids[i], domain_name,
3083 names.names[i].string,
3086 printf("%s\\%s\n", domain_name,
3087 names.names[i].string);
3091 num_members -= this_time;
3095 return NT_STATUS_OK;
3098 static NTSTATUS rpc_list_alias_members(struct net_context *c,
3099 struct rpc_pipe_client *pipe_hnd,
3100 struct cli_state *cli,
3101 TALLOC_CTX *mem_ctx,
3102 struct policy_handle *domain_pol,
3105 NTSTATUS result, status;
3106 struct rpc_pipe_client *lsa_pipe;
3107 struct policy_handle alias_pol, lsa_pol;
3109 struct dom_sid *alias_sids;
3112 enum lsa_SidType *types;
3114 struct lsa_SidArray sid_array;
3115 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3117 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3119 MAXIMUM_ALLOWED_ACCESS,
3123 if (!NT_STATUS_IS_OK(status)) {
3126 if (!NT_STATUS_IS_OK(result)) {
3130 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
3134 if (!NT_STATUS_IS_OK(status)) {
3135 d_fprintf(stderr, _("Couldn't list alias members\n"));
3138 if (!NT_STATUS_IS_OK(result)) {
3139 d_fprintf(stderr, _("Couldn't list alias members\n"));
3143 num_members = sid_array.num_sids;
3145 if (num_members == 0) {
3146 return NT_STATUS_OK;
3149 result = cli_rpc_pipe_open_noauth(cli,
3152 if (!NT_STATUS_IS_OK(result)) {
3153 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
3154 nt_errstr(result) );
3158 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
3159 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
3161 if (!NT_STATUS_IS_OK(result)) {
3162 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
3163 TALLOC_FREE(lsa_pipe);
3167 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
3169 d_fprintf(stderr, _("Out of memory\n"));
3170 TALLOC_FREE(lsa_pipe);
3171 return NT_STATUS_NO_MEMORY;
3174 for (i=0; i<num_members; i++) {
3175 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
3178 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
3179 num_members, alias_sids,
3180 &domains, &names, &types);
3182 if (!NT_STATUS_IS_OK(result) &&
3183 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3184 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3185 TALLOC_FREE(lsa_pipe);
3189 for (i = 0; i < num_members; i++) {
3191 sid_to_fstring(sid_str, &alias_sids[i]);
3193 if (c->opt_long_list_entries) {
3194 printf("%s %s\\%s %d\n", sid_str,
3195 domains[i] ? domains[i] : _("*unknown*"),
3196 names[i] ? names[i] : _("*unknown*"), types[i]);
3199 printf("%s\\%s\n", domains[i], names[i]);
3201 printf("%s\n", sid_str);
3205 TALLOC_FREE(lsa_pipe);
3206 return NT_STATUS_OK;
3209 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3210 const struct dom_sid *domain_sid,
3211 const char *domain_name,
3212 struct cli_state *cli,
3213 struct rpc_pipe_client *pipe_hnd,
3214 TALLOC_CTX *mem_ctx,
3218 NTSTATUS result, status;
3219 struct policy_handle connect_pol, domain_pol;
3220 struct samr_Ids rids, rid_types;
3221 struct lsa_String lsa_acct_name;
3222 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3224 /* Get sam policy handle */
3226 status = dcerpc_samr_Connect2(b, mem_ctx,
3228 MAXIMUM_ALLOWED_ACCESS,
3231 if (!NT_STATUS_IS_OK(status)) {
3234 if (!NT_STATUS_IS_OK(result)) {
3238 /* Get domain policy handle */
3240 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3242 MAXIMUM_ALLOWED_ACCESS,
3243 discard_const_p(struct dom_sid2, domain_sid),
3246 if (!NT_STATUS_IS_OK(status)) {
3249 if (!NT_STATUS_IS_OK(result)) {
3253 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3255 status = dcerpc_samr_LookupNames(b, mem_ctx,
3262 if (!NT_STATUS_IS_OK(status)) {
3266 if (!NT_STATUS_IS_OK(result)) {
3268 /* Ok, did not find it in the global sam, try with builtin */
3270 struct dom_sid sid_Builtin;
3272 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3274 sid_copy(&sid_Builtin, &global_sid_Builtin);
3276 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3278 MAXIMUM_ALLOWED_ACCESS,
3282 if (!NT_STATUS_IS_OK(status)) {
3285 if (!NT_STATUS_IS_OK(result)) {
3286 d_fprintf(stderr, _("Couldn't find group %s\n"),
3291 status = dcerpc_samr_LookupNames(b, mem_ctx,
3298 if (!NT_STATUS_IS_OK(status)) {
3301 if (!NT_STATUS_IS_OK(result)) {
3302 d_fprintf(stderr, _("Couldn't find group %s\n"),
3308 if (rids.count != 1) {
3309 d_fprintf(stderr, _("Couldn't find group %s\n"),
3311 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3313 if (rid_types.count != 1) {
3314 d_fprintf(stderr, _("Couldn't find group %s\n"),
3316 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3320 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3321 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3322 domain_sid, &domain_pol,
3326 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3327 return rpc_list_alias_members(c, pipe_hnd, cli, mem_ctx, &domain_pol,
3331 return NT_STATUS_NO_SUCH_GROUP;
3334 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3336 if (argc != 1 || c->display_usage) {
3337 return rpc_group_usage(c, argc, argv);
3340 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3341 rpc_group_members_internals,
3345 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3347 NET_API_STATUS status;
3348 struct GROUP_INFO_0 g0;
3352 d_printf(_("Usage:\n"));
3353 d_printf("net rpc group rename group newname\n");
3357 g0.grpi0_name = argv[1];
3359 status = NetGroupSetInfo(c->opt_host,
3366 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3367 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3375 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3377 if (argc != 2 || c->display_usage) {
3378 return rpc_group_usage(c, argc, argv);
3381 return rpc_group_rename_internals(c, argc, argv);
3385 * 'net rpc group' entrypoint.
3386 * @param argc Standard main() style argc.
3387 * @param argv Standard main() style argv. Initial components are already
3391 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3393 NET_API_STATUS status;
3395 struct functable func[] = {
3400 N_("Create specified group"),
3401 N_("net rpc group add\n"
3402 " Create specified group")
3408 N_("Delete specified group"),
3409 N_("net rpc group delete\n"
3410 " Delete specified group")
3416 N_("Add member to group"),
3417 N_("net rpc group addmem\n"
3418 " Add member to group")
3424 N_("Remove member from group"),
3425 N_("net rpc group delmem\n"
3426 " Remove member from group")
3433 N_("net rpc group list\n"
3440 N_("List group members"),
3441 N_("net rpc group members\n"
3442 " List group members")
3449 N_("net rpc group rename\n"
3452 {NULL, NULL, 0, NULL, NULL}
3455 status = libnetapi_net_init(&c->netapi_ctx);
3459 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3460 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3461 if (c->opt_kerberos) {
3462 libnetapi_set_use_kerberos(c->netapi_ctx);
3466 if (c->display_usage) {
3467 d_printf(_("Usage:\n"));
3468 d_printf(_("net rpc group\n"
3469 " Alias for net rpc group list global "
3470 "local builtin\n"));
3471 net_display_usage_from_functable(func);
3475 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3476 rpc_group_list_internals,
3480 return net_run_function(c, argc, argv, "net rpc group", func);
3483 /****************************************************************************/
3485 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3487 return net_share_usage(c, argc, argv);
3491 * Add a share on a remote RPC server.
3493 * @param argc Standard main() style argc.
3494 * @param argv Standard main() style argv. Initial components are already
3497 * @return A shell status integer (0 for success).
3500 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3502 NET_API_STATUS status;
3505 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3506 uint32 num_users=0, perms=0;
3507 char *password=NULL; /* don't allow a share password */
3508 struct SHARE_INFO_2 i2;
3509 uint32_t parm_error = 0;
3511 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3512 return rpc_share_usage(c, argc, argv);
3515 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3519 path = strchr(sharename, '=');
3526 i2.shi2_netname = sharename;
3527 i2.shi2_type = type;
3528 i2.shi2_remark = c->opt_comment;
3529 i2.shi2_permissions = perms;
3530 i2.shi2_max_uses = c->opt_maxusers;
3531 i2.shi2_current_uses = num_users;
3532 i2.shi2_path = path;
3533 i2.shi2_passwd = password;
3535 status = NetShareAdd(c->opt_host,
3540 printf(_("NetShareAdd failed with: %s\n"),
3541 libnetapi_get_error_string(c->netapi_ctx, status));
3548 * Delete a share on a remote RPC server.
3550 * @param domain_sid The domain sid acquired from the remote server.
3551 * @param argc Standard main() style argc.
3552 * @param argv Standard main() style argv. Initial components are already
3555 * @return A shell status integer (0 for success).
3557 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3559 if (argc < 1 || c->display_usage) {
3560 return rpc_share_usage(c, argc, argv);
3563 return NetShareDel(c->opt_host, argv[0], 0);
3567 * Formatted print of share info
3569 * @param r pointer to SHARE_INFO_1 to format
3572 static void display_share_info_1(struct net_context *c,
3573 struct SHARE_INFO_1 *r)
3575 if (c->opt_long_list_entries) {
3576 d_printf("%-12s %-8.8s %-50s\n",
3578 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3581 d_printf("%s\n", r->shi1_netname);
3585 static WERROR get_share_info(struct net_context *c,
3586 struct rpc_pipe_client *pipe_hnd,
3587 TALLOC_CTX *mem_ctx,
3591 struct srvsvc_NetShareInfoCtr *info_ctr)
3595 union srvsvc_NetShareInfo info;
3596 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3598 /* no specific share requested, enumerate all */
3601 uint32_t preferred_len = 0xffffffff;
3602 uint32_t total_entries = 0;
3603 uint32_t resume_handle = 0;
3605 info_ctr->level = level;
3607 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3614 if (!NT_STATUS_IS_OK(status)) {
3615 return ntstatus_to_werror(status);
3620 /* request just one share */
3621 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3628 if (!NT_STATUS_IS_OK(status)) {
3629 result = ntstatus_to_werror(status);
3633 if (!W_ERROR_IS_OK(result)) {
3638 ZERO_STRUCTP(info_ctr);
3640 info_ctr->level = level;
3645 struct srvsvc_NetShareCtr1 *ctr1;
3647 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3648 W_ERROR_HAVE_NO_MEMORY(ctr1);
3651 ctr1->array = info.info1;
3653 info_ctr->ctr.ctr1 = ctr1;
3659 struct srvsvc_NetShareCtr2 *ctr2;
3661 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3662 W_ERROR_HAVE_NO_MEMORY(ctr2);
3665 ctr2->array = info.info2;
3667 info_ctr->ctr.ctr2 = ctr2;
3673 struct srvsvc_NetShareCtr502 *ctr502;
3675 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3676 W_ERROR_HAVE_NO_MEMORY(ctr502);
3679 ctr502->array = info.info502;
3681 info_ctr->ctr.ctr502 = ctr502;
3691 * 'net rpc share list' entrypoint.
3692 * @param argc Standard main() style argc.
3693 * @param argv Standard main() style argv. Initial components are already
3696 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3698 NET_API_STATUS status;
3699 struct SHARE_INFO_1 *i1 = NULL;
3700 uint32_t entries_read = 0;
3701 uint32_t total_entries = 0;
3702 uint32_t resume_handle = 0;
3703 uint32_t i, level = 1;
3705 if (c->display_usage) {
3707 "net rpc share list\n"
3710 _("List shares on remote server"));
3714 status = NetShareEnum(c->opt_host,
3716 (uint8_t **)(void *)&i1,
3725 /* Display results */
3727 if (c->opt_long_list_entries) {
3729 "\nEnumerating shared resources (exports) on remote server:\n\n"
3730 "\nShare name Type Description\n"
3731 "---------- ---- -----------\n"));
3733 for (i = 0; i < entries_read; i++)
3734 display_share_info_1(c, &i1[i]);
3739 static bool check_share_availability(struct cli_state *cli, const char *netname)
3743 status = cli_tree_connect(cli, netname, "A:", "", 0);
3744 if (!NT_STATUS_IS_OK(status)) {
3745 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3749 status = cli_tdis(cli);
3750 if (!NT_STATUS_IS_OK(status)) {
3751 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3758 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3759 const char *netname, uint32 type)
3761 /* only support disk shares */
3762 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3763 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3768 /* skip builtin shares */
3769 /* FIXME: should print$ be added too ? */
3770 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3771 strequal(netname,"global"))
3774 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3775 printf(_("excluding [%s]\n"), netname);
3779 return check_share_availability(cli, netname);
3783 * Migrate shares from a remote RPC server to the local RPC server.
3785 * All parameters are provided by the run_rpc_command function, except for
3786 * argc, argv which are passed through.
3788 * @param domain_sid The domain sid acquired from the remote server.
3789 * @param cli A cli_state connected to the server.
3790 * @param mem_ctx Talloc context, destroyed on completion of the function.
3791 * @param argc Standard main() style argc.
3792 * @param argv Standard main() style argv. Initial components are already
3795 * @return Normal NTSTATUS return.
3798 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3799 const struct dom_sid *domain_sid,
3800 const char *domain_name,
3801 struct cli_state *cli,
3802 struct rpc_pipe_client *pipe_hnd,
3803 TALLOC_CTX *mem_ctx,
3808 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3809 struct srvsvc_NetShareInfoCtr ctr_src;
3811 struct rpc_pipe_client *srvsvc_pipe = NULL;
3812 struct cli_state *cli_dst = NULL;
3813 uint32 level = 502; /* includes secdesc */
3814 uint32_t parm_error = 0;
3815 struct dcerpc_binding_handle *b;
3817 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3819 if (!W_ERROR_IS_OK(result))
3822 /* connect destination PI_SRVSVC */
3823 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3825 if (!NT_STATUS_IS_OK(nt_status))
3828 b = srvsvc_pipe->binding_handle;
3830 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3832 union srvsvc_NetShareInfo info;
3833 struct srvsvc_NetShareInfo502 info502 =
3834 ctr_src.ctr.ctr502->array[i];
3836 /* reset error-code */
3837 nt_status = NT_STATUS_UNSUCCESSFUL;
3839 if (!check_share_sanity(c, cli, info502.name, info502.type))
3842 /* finally add the share on the dst server */
3844 printf(_("migrating: [%s], path: %s, comment: %s, without "
3846 info502.name, info502.path, info502.comment);
3848 info.info502 = &info502;
3850 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3851 srvsvc_pipe->desthost,
3856 if (!NT_STATUS_IS_OK(nt_status)) {
3857 printf(_("cannot add share: %s\n"),
3858 nt_errstr(nt_status));
3861 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3862 printf(_(" [%s] does already exist\n"),
3867 if (!W_ERROR_IS_OK(result)) {
3868 nt_status = werror_to_ntstatus(result);
3869 printf(_("cannot add share: %s\n"),
3870 win_errstr(result));
3876 nt_status = NT_STATUS_OK;
3880 cli_shutdown(cli_dst);
3888 * Migrate shares from a RPC server to another.
3890 * @param argc Standard main() style argc.
3891 * @param argv Standard main() style argv. Initial components are already
3894 * @return A shell status integer (0 for success).
3896 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3899 if (c->display_usage) {
3901 "net rpc share migrate shares\n"
3904 _("Migrate shares to local server"));
3909 printf(_("no server to migrate\n"));
3913 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3914 rpc_share_migrate_shares_internals,
3921 * @param f file_info
3922 * @param mask current search mask
3923 * @param state arg-pointer
3926 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3927 const char *mask, void *state)
3929 static NTSTATUS nt_status;
3930 static struct copy_clistate *local_state;
3931 static fstring filename, new_mask;
3934 struct net_context *c;
3936 local_state = (struct copy_clistate *)state;
3937 nt_status = NT_STATUS_UNSUCCESSFUL;
3941 if (strequal(f->name, ".") || strequal(f->name, ".."))
3942 return NT_STATUS_OK;
3944 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3947 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3949 DEBUG(3,("got dir: %s\n", f->name));
3951 fstrcpy(dir, local_state->cwd);
3953 fstrcat(dir, f->name);
3955 switch (net_mode_share)
3957 case NET_MODE_SHARE_MIGRATE:
3958 /* create that directory */
3959 nt_status = net_copy_file(c, local_state->mem_ctx,
3960 local_state->cli_share_src,
3961 local_state->cli_share_dst,
3963 c->opt_acls? true : false,
3964 c->opt_attrs? true : false,
3965 c->opt_timestamps? true:false,
3969 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3970 return NT_STATUS_INTERNAL_ERROR;
3973 if (!NT_STATUS_IS_OK(nt_status)) {
3974 printf(_("could not handle dir %s: %s\n"),
3975 dir, nt_errstr(nt_status));
3979 /* search below that directory */
3980 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
3981 return NT_STATUS_NO_MEMORY;
3983 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
3984 return NT_STATUS_NO_MEMORY;
3987 old_dir = local_state->cwd;
3988 local_state->cwd = dir;
3989 nt_status = sync_files(local_state, new_mask);
3990 if (!NT_STATUS_IS_OK(nt_status)) {
3991 printf(_("could not handle files\n"));
3993 local_state->cwd = old_dir;
4000 fstrcpy(filename, local_state->cwd);
4001 fstrcat(filename, "\\");
4002 fstrcat(filename, f->name);
4004 DEBUG(3,("got file: %s\n", filename));
4006 switch (net_mode_share)
4008 case NET_MODE_SHARE_MIGRATE:
4009 nt_status = net_copy_file(c, local_state->mem_ctx,
4010 local_state->cli_share_src,
4011 local_state->cli_share_dst,
4013 c->opt_acls? true : false,
4014 c->opt_attrs? true : false,
4015 c->opt_timestamps? true: false,
4019 d_fprintf(stderr, _("Unsupported file mode %d\n"),
4021 return NT_STATUS_INTERNAL_ERROR;
4024 if (!NT_STATUS_IS_OK(nt_status))
4025 printf(_("could not handle file %s: %s\n"),
4026 filename, nt_errstr(nt_status));
4031 * sync files, can be called recursivly to list files
4032 * and then call copy_fn for each file
4034 * @param cp_clistate pointer to the copy_clistate we work with
4035 * @param mask the current search mask
4037 * @return Boolean result
4039 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
4041 struct cli_state *targetcli;
4042 char *targetpath = NULL;
4045 DEBUG(3,("calling cli_list with mask: %s\n", mask));
4047 status = cli_resolve_path(talloc_tos(), "", NULL,
4048 cp_clistate->cli_share_src,
4049 mask, &targetcli, &targetpath);
4050 if (!NT_STATUS_IS_OK(status)) {
4051 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
4053 mask, nt_errstr(status));
4057 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
4058 copy_fn, cp_clistate);
4059 if (!NT_STATUS_IS_OK(status)) {
4060 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
4061 mask, nt_errstr(status));
4069 * Set the top level directory permissions before we do any further copies.
4070 * Should set up ACL inheritance.
4073 bool copy_top_level_perms(struct net_context *c,
4074 struct copy_clistate *cp_clistate,
4075 const char *sharename)
4077 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4079 switch (net_mode_share) {
4080 case NET_MODE_SHARE_MIGRATE:
4081 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
4082 nt_status = net_copy_fileattr(c,
4083 cp_clistate->mem_ctx,
4084 cp_clistate->cli_share_src,
4085 cp_clistate->cli_share_dst,
4087 c->opt_acls? true : false,
4088 c->opt_attrs? true : false,
4089 c->opt_timestamps? true: false,
4093 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4097 if (!NT_STATUS_IS_OK(nt_status)) {
4098 printf(_("Could handle directory attributes for top level "
4099 "directory of share %s. Error %s\n"),
4100 sharename, nt_errstr(nt_status));
4108 * Sync all files inside a remote share to another share (over smb).
4110 * All parameters are provided by the run_rpc_command function, except for
4111 * argc, argv which are passed through.
4113 * @param domain_sid The domain sid acquired from the remote server.
4114 * @param cli A cli_state connected to the server.
4115 * @param mem_ctx Talloc context, destroyed on completion of the function.
4116 * @param argc Standard main() style argc.
4117 * @param argv Standard main() style argv. Initial components are already
4120 * @return Normal NTSTATUS return.
4123 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
4124 const struct dom_sid *domain_sid,
4125 const char *domain_name,
4126 struct cli_state *cli,
4127 struct rpc_pipe_client *pipe_hnd,
4128 TALLOC_CTX *mem_ctx,
4133 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4134 struct srvsvc_NetShareInfoCtr ctr_src;
4137 struct copy_clistate cp_clistate;
4138 bool got_src_share = false;
4139 bool got_dst_share = false;
4140 const char *mask = "\\*";
4143 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
4145 nt_status = NT_STATUS_NO_MEMORY;
4149 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4152 if (!W_ERROR_IS_OK(result))
4155 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4157 struct srvsvc_NetShareInfo502 info502 =
4158 ctr_src.ctr.ctr502->array[i];
4160 if (!check_share_sanity(c, cli, info502.name, info502.type))
4163 /* one might not want to mirror whole discs :) */
4164 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
4165 d_printf(_("skipping [%s]: builtin/hidden share\n"),
4170 switch (net_mode_share)
4172 case NET_MODE_SHARE_MIGRATE:
4176 d_fprintf(stderr, _("Unsupported mode %d\n"),
4180 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4183 c->opt_acls ? _("including") : _("without"),
4184 c->opt_attrs ? _("including") : _("without"),
4185 c->opt_timestamps ? _("(preserving timestamps)") : "");
4187 cp_clistate.mem_ctx = mem_ctx;
4188 cp_clistate.cli_share_src = NULL;
4189 cp_clistate.cli_share_dst = NULL;
4190 cp_clistate.cwd = NULL;
4191 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4194 /* open share source */
4195 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4196 smbXcli_conn_remote_sockaddr(cli->conn),
4197 smbXcli_conn_remote_name(cli->conn),
4198 info502.name, "A:");
4199 if (!NT_STATUS_IS_OK(nt_status))
4202 got_src_share = true;
4204 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4205 /* open share destination */
4206 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4207 NULL, dst, info502.name, "A:");
4208 if (!NT_STATUS_IS_OK(nt_status))
4211 got_dst_share = true;
4214 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4215 d_fprintf(stderr, _("Could not handle the top level "
4216 "directory permissions for the "
4217 "share: %s\n"), info502.name);
4218 nt_status = NT_STATUS_UNSUCCESSFUL;
4222 nt_status = sync_files(&cp_clistate, mask);
4223 if (!NT_STATUS_IS_OK(nt_status)) {
4224 d_fprintf(stderr, _("could not handle files for share: "
4225 "%s\n"), info502.name);
4230 nt_status = NT_STATUS_OK;
4235 cli_shutdown(cp_clistate.cli_share_src);
4238 cli_shutdown(cp_clistate.cli_share_dst);
4245 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4247 if (c->display_usage) {
4249 "net share migrate files\n"
4252 _("Migrate files to local server"));
4257 d_printf(_("no server to migrate\n"));
4261 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4262 rpc_share_migrate_files_internals,
4267 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4269 * All parameters are provided by the run_rpc_command function, except for
4270 * argc, argv which are passed through.
4272 * @param domain_sid The domain sid acquired from the remote server.
4273 * @param cli A cli_state connected to the server.
4274 * @param mem_ctx Talloc context, destroyed on completion of the function.
4275 * @param argc Standard main() style argc.
4276 * @param argv Standard main() style argv. Initial components are already
4279 * @return Normal NTSTATUS return.
4282 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4283 const struct dom_sid *domain_sid,
4284 const char *domain_name,
4285 struct cli_state *cli,
4286 struct rpc_pipe_client *pipe_hnd,
4287 TALLOC_CTX *mem_ctx,
4292 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4293 struct srvsvc_NetShareInfoCtr ctr_src;
4294 union srvsvc_NetShareInfo info;
4296 struct rpc_pipe_client *srvsvc_pipe = NULL;
4297 struct cli_state *cli_dst = NULL;
4298 uint32 level = 502; /* includes secdesc */
4299 uint32_t parm_error = 0;
4300 struct dcerpc_binding_handle *b;
4302 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4305 if (!W_ERROR_IS_OK(result))
4308 /* connect destination PI_SRVSVC */
4309 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4311 if (!NT_STATUS_IS_OK(nt_status))
4314 b = srvsvc_pipe->binding_handle;
4316 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4318 struct srvsvc_NetShareInfo502 info502 =
4319 ctr_src.ctr.ctr502->array[i];
4321 /* reset error-code */
4322 nt_status = NT_STATUS_UNSUCCESSFUL;
4324 if (!check_share_sanity(c, cli, info502.name, info502.type))
4327 printf(_("migrating: [%s], path: %s, comment: %s, including "
4329 info502.name, info502.path, info502.comment);
4332 display_sec_desc(info502.sd_buf.sd);
4334 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4335 info.info502 = &info502;
4337 /* finally modify the share on the dst server */
4338 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4339 srvsvc_pipe->desthost,
4345 if (!NT_STATUS_IS_OK(nt_status)) {
4346 printf(_("cannot set share-acl: %s\n"),
4347 nt_errstr(nt_status));
4350 if (!W_ERROR_IS_OK(result)) {
4351 nt_status = werror_to_ntstatus(result);
4352 printf(_("cannot set share-acl: %s\n"),
4353 win_errstr(result));
4359 nt_status = NT_STATUS_OK;
4363 cli_shutdown(cli_dst);
4371 * Migrate share-acls from a RPC server to another.
4373 * @param argc Standard main() style argc.
4374 * @param argv Standard main() style argv. Initial components are already
4377 * @return A shell status integer (0 for success).
4379 static int rpc_share_migrate_security(struct net_context *c, int argc,
4382 if (c->display_usage) {
4384 "net rpc share migrate security\n"
4387 _("Migrate share-acls to local server"));
4392 d_printf(_("no server to migrate\n"));
4396 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4397 rpc_share_migrate_security_internals,
4402 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4403 * from one server to another.
4405 * @param argc Standard main() style argc.
4406 * @param argv Standard main() style argv. Initial components are already
4409 * @return A shell status integer (0 for success).
4412 static int rpc_share_migrate_all(struct net_context *c, int argc,
4417 if (c->display_usage) {
4419 "net rpc share migrate all\n"
4422 _("Migrates shares including all share settings"));
4427 d_printf(_("no server to migrate\n"));
4431 /* order is important. we don't want to be locked out by the share-acl
4432 * before copying files - gd */
4434 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4435 rpc_share_migrate_shares_internals, argc, argv);
4439 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4440 rpc_share_migrate_files_internals, argc, argv);
4444 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4445 rpc_share_migrate_security_internals, argc,
4451 * 'net rpc share migrate' entrypoint.
4452 * @param argc Standard main() style argc.
4453 * @param argv Standard main() style argv. Initial components are already
4456 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4459 struct functable func[] = {
4462 rpc_share_migrate_all,
4464 N_("Migrate shares from remote to local server"),
4465 N_("net rpc share migrate all\n"
4466 " Migrate shares from remote to local server")
4470 rpc_share_migrate_files,
4472 N_("Migrate files from remote to local server"),
4473 N_("net rpc share migrate files\n"
4474 " Migrate files from remote to local server")
4478 rpc_share_migrate_security,
4480 N_("Migrate share-ACLs from remote to local server"),
4481 N_("net rpc share migrate security\n"
4482 " Migrate share-ACLs from remote to local server")
4486 rpc_share_migrate_shares,
4488 N_("Migrate shares from remote to local server"),
4489 N_("net rpc share migrate shares\n"
4490 " Migrate shares from remote to local server")
4492 {NULL, NULL, 0, NULL, NULL}
4495 net_mode_share = NET_MODE_SHARE_MIGRATE;
4497 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4503 struct dom_sid *members;
4506 static int num_server_aliases;
4507 static struct full_alias *server_aliases;
4510 * Add an alias to the static list.
4512 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4514 if (server_aliases == NULL)
4515 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4517 server_aliases[num_server_aliases] = *alias;
4518 num_server_aliases += 1;
4522 * For a specific domain on the server, fetch all the aliases
4523 * and their members. Add all of them to the server_aliases.
4526 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4527 TALLOC_CTX *mem_ctx,
4528 struct policy_handle *connect_pol,
4529 const struct dom_sid *domain_sid)
4531 uint32 start_idx, max_entries, num_entries, i;
4532 struct samr_SamArray *groups = NULL;
4533 NTSTATUS result, status;
4534 struct policy_handle domain_pol;
4535 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4537 /* Get domain policy handle */
4539 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4541 MAXIMUM_ALLOWED_ACCESS,
4542 discard_const_p(struct dom_sid2, domain_sid),
4545 if (!NT_STATUS_IS_OK(status)) {
4548 if (!NT_STATUS_IS_OK(result)) {
4556 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4563 if (!NT_STATUS_IS_OK(status)) {
4566 for (i = 0; i < num_entries; i++) {
4568 struct policy_handle alias_pol;
4569 struct full_alias alias;
4570 struct lsa_SidArray sid_array;
4574 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4576 MAXIMUM_ALLOWED_ACCESS,
4577 groups->entries[i].idx,
4580 if (!NT_STATUS_IS_OK(status)) {
4583 if (!NT_STATUS_IS_OK(_result)) {
4588 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4592 if (!NT_STATUS_IS_OK(status)) {
4595 if (!NT_STATUS_IS_OK(_result)) {
4600 alias.num_members = sid_array.num_sids;
4602 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4603 if (!NT_STATUS_IS_OK(status)) {
4606 if (!NT_STATUS_IS_OK(_result)) {
4611 alias.members = NULL;
4613 if (alias.num_members > 0) {
4614 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4616 for (j = 0; j < alias.num_members; j++)
4617 sid_copy(&alias.members[j],
4618 sid_array.sids[j].sid);
4621 sid_compose(&alias.sid, domain_sid,
4622 groups->entries[i].idx);
4624 push_alias(mem_ctx, &alias);
4626 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4628 status = NT_STATUS_OK;
4631 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4637 * Dump server_aliases as names for debugging purposes.
4640 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4641 const struct dom_sid *domain_sid,
4642 const char *domain_name,
4643 struct cli_state *cli,
4644 struct rpc_pipe_client *pipe_hnd,
4645 TALLOC_CTX *mem_ctx,
4651 struct policy_handle lsa_pol;
4652 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4654 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4655 SEC_FLAG_MAXIMUM_ALLOWED,
4657 if (!NT_STATUS_IS_OK(result))
4660 for (i=0; i<num_server_aliases; i++) {
4663 enum lsa_SidType *types;
4666 struct full_alias *alias = &server_aliases[i];
4668 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4670 &domains, &names, &types);
4671 if (!NT_STATUS_IS_OK(result))
4674 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4676 if (alias->num_members == 0) {
4681 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4684 &domains, &names, &types);
4686 if (!NT_STATUS_IS_OK(result) &&
4687 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4690 for (j=0; j<alias->num_members; j++)
4691 DEBUG(1, ("%s\\%s (%d); ",
4692 domains[j] ? domains[j] : "*unknown*",
4693 names[j] ? names[j] : "*unknown*",types[j]));
4697 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4699 return NT_STATUS_OK;
4703 * Fetch a list of all server aliases and their members into
4707 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4708 const struct dom_sid *domain_sid,
4709 const char *domain_name,
4710 struct cli_state *cli,
4711 struct rpc_pipe_client *pipe_hnd,
4712 TALLOC_CTX *mem_ctx,
4716 NTSTATUS result, status;
4717 struct policy_handle connect_pol;
4718 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4720 status = dcerpc_samr_Connect2(b, mem_ctx,
4722 MAXIMUM_ALLOWED_ACCESS,
4725 if (!NT_STATUS_IS_OK(status)) {
4728 if (!NT_STATUS_IS_OK(result)) {
4733 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4734 &global_sid_Builtin);
4735 if (!NT_STATUS_IS_OK(status)) {
4739 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4742 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4747 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4749 token->num_sids = 4;
4751 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4752 d_fprintf(stderr, "malloc %s\n",_("failed"));
4753 token->num_sids = 0;
4757 token->sids[0] = *user_sid;
4758 sid_copy(&token->sids[1], &global_sid_World);
4759 sid_copy(&token->sids[2], &global_sid_Network);
4760 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4763 static void free_user_token(struct security_token *token)
4765 SAFE_FREE(token->sids);
4768 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4770 if (security_token_has_sid(token, sid))
4773 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4778 sid_copy(&token->sids[token->num_sids], sid);
4780 token->num_sids += 1;
4785 struct security_token token;
4788 static void dump_user_token(struct user_token *token)
4792 d_printf("%s\n", token->name);
4794 for (i=0; i<token->token.num_sids; i++) {
4795 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4799 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4803 for (i=0; i<alias->num_members; i++) {
4804 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4811 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4815 for (i=0; i<num_server_aliases; i++) {
4816 if (is_alias_member(&sid, &server_aliases[i]))
4817 add_sid_to_token(token, &server_aliases[i].sid);
4822 * We got a user token with all the SIDs we can know about without asking the
4823 * server directly. These are the user and domain group sids. All of these can
4824 * be members of aliases. So scan the list of aliases for each of the SIDs and
4825 * add them to the token.
4828 static void collect_alias_memberships(struct security_token *token)
4830 int num_global_sids = token->num_sids;
4833 for (i=0; i<num_global_sids; i++) {
4834 collect_sid_memberships(token, token->sids[i]);
4838 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4840 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4841 enum wbcSidType type;
4843 struct wbcDomainSid wsid;
4844 char sid_str[WBC_SID_STRING_BUFLEN];
4845 struct dom_sid user_sid;
4846 uint32_t num_groups;
4847 gid_t *groups = NULL;
4850 fstr_sprintf(full_name, "%s%c%s",
4851 domain, *lp_winbind_separator(), user);
4853 /* First let's find out the user sid */
4855 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4857 if (!WBC_ERROR_IS_OK(wbc_status)) {
4858 DEBUG(1, ("winbind could not find %s: %s\n",
4859 full_name, wbcErrorString(wbc_status)));
4863 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4865 if (type != WBC_SID_NAME_USER) {
4866 DEBUG(1, ("%s is not a user\n", full_name));
4870 if (!string_to_sid(&user_sid, sid_str)) {
4871 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4875 init_user_token(token, &user_sid);
4877 /* And now the groups winbind knows about */
4879 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4880 if (!WBC_ERROR_IS_OK(wbc_status)) {
4881 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4882 full_name, wbcErrorString(wbc_status)));
4886 for (i = 0; i < num_groups; i++) {
4887 gid_t gid = groups[i];
4891 wbc_status = wbcGidToSid(gid, &wsid);
4892 if (!WBC_ERROR_IS_OK(wbc_status)) {
4893 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4894 (unsigned int)gid, wbcErrorString(wbc_status)));
4895 wbcFreeMemory(groups);
4899 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4901 DEBUG(3, (" %s\n", sid_str));
4903 ok = string_to_sid(&sid, sid_str);
4905 DEBUG(1, ("Failed to convert string to SID\n"));
4906 wbcFreeMemory(groups);
4909 add_sid_to_token(token, &sid);
4911 wbcFreeMemory(groups);
4917 * Get a list of all user tokens we want to look at
4920 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4921 struct user_token **user_tokens)
4923 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4924 uint32_t i, num_users;
4926 struct user_token *result;
4927 TALLOC_CTX *frame = NULL;
4929 if (lp_winbind_use_default_domain() &&
4930 (c->opt_target_workgroup == NULL)) {
4931 d_fprintf(stderr, _("winbind use default domain = yes set, "
4932 "please specify a workgroup\n"));
4936 /* Send request to winbind daemon */
4938 wbc_status = wbcListUsers(NULL, &num_users, &users);
4939 if (!WBC_ERROR_IS_OK(wbc_status)) {
4940 DEBUG(1, (_("winbind could not list users: %s\n"),
4941 wbcErrorString(wbc_status)));
4945 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4947 if (result == NULL) {
4948 DEBUG(1, ("Could not malloc sid array\n"));
4949 wbcFreeMemory(users);
4953 frame = talloc_stackframe();
4954 for (i=0; i < num_users; i++) {
4955 fstring domain, user;
4958 fstrcpy(result[i].name, users[i]);
4960 p = strchr(users[i], *lp_winbind_separator());
4962 DEBUG(3, ("%s\n", users[i]));
4965 fstrcpy(domain, c->opt_target_workgroup);
4966 fstrcpy(user, users[i]);
4969 fstrcpy(domain, users[i]);
4970 if (!strupper_m(domain)) {
4971 DEBUG(1, ("strupper_m %s failed\n", domain));
4972 wbcFreeMemory(users);
4978 get_user_sids(domain, user, &(result[i].token));
4981 wbcFreeMemory(users);
4983 *num_tokens = num_users;
4984 *user_tokens = result;
4989 static bool get_user_tokens_from_file(FILE *f,
4991 struct user_token **tokens)
4993 struct user_token *token = NULL;
4998 if (fgets(line, sizeof(line)-1, f) == NULL) {
5002 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
5003 line[strlen(line)-1] = '\0';
5006 if (line[0] == ' ') {
5010 if(!string_to_sid(&sid, &line[1])) {
5011 DEBUG(1,("get_user_tokens_from_file: Could "
5012 "not convert sid %s \n",&line[1]));
5016 if (token == NULL) {
5017 DEBUG(0, ("File does not begin with username"));
5021 add_sid_to_token(&token->token, &sid);
5025 /* And a new user... */
5028 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
5029 if (*tokens == NULL) {
5030 DEBUG(0, ("Could not realloc tokens\n"));
5034 token = &((*tokens)[*num_tokens-1]);
5036 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
5039 token->token.num_sids = 0;
5040 token->token.sids = NULL;
5049 * Show the list of all users that have access to a share
5052 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
5053 struct cli_state *cli,
5054 TALLOC_CTX *mem_ctx,
5055 const char *netname,
5057 struct user_token *tokens)
5060 struct security_descriptor *share_sd = NULL;
5061 struct security_descriptor *root_sd = NULL;
5063 union srvsvc_NetShareInfo info;
5067 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5069 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5076 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5077 DEBUG(1, ("Coult not query secdesc for share %s\n",
5082 share_sd = info.info502->sd_buf.sd;
5083 if (share_sd == NULL) {
5084 DEBUG(1, ("Got no secdesc for share %s\n",
5088 cnum = cli_state_get_tid(cli);
5090 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", "", 0))) {
5094 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
5095 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
5096 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
5099 for (i=0; i<num_tokens; i++) {
5102 if (share_sd != NULL) {
5103 status = se_access_check(share_sd, &tokens[i].token,
5106 if (!NT_STATUS_IS_OK(status)) {
5107 DEBUG(1, ("Could not check share_sd for "
5114 if (root_sd == NULL) {
5115 d_printf(" %s\n", tokens[i].name);
5119 status = se_access_check(root_sd, &tokens[i].token,
5121 if (!NT_STATUS_IS_OK(status)) {
5122 DEBUG(1, ("Could not check root_sd for user %s\n",
5126 d_printf(" %s\n", tokens[i].name);
5129 if (fnum != (uint16_t)-1)
5130 cli_close(cli, fnum);
5132 cli_state_set_tid(cli, cnum);
5138 * List shares on a remote RPC server, including the security descriptors.
5140 * All parameters are provided by the run_rpc_command function, except for
5141 * argc, argv which are passed through.
5143 * @param domain_sid The domain sid acquired from the remote server.
5144 * @param cli A cli_state connected to the server.
5145 * @param mem_ctx Talloc context, destroyed on completion of the function.
5146 * @param argc Standard main() style argc.
5147 * @param argv Standard main() style argv. Initial components are already
5150 * @return Normal NTSTATUS return.
5153 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
5154 const struct dom_sid *domain_sid,
5155 const char *domain_name,
5156 struct cli_state *cli,
5157 struct rpc_pipe_client *pipe_hnd,
5158 TALLOC_CTX *mem_ctx,
5164 NTSTATUS nt_status = NT_STATUS_OK;
5165 uint32_t total_entries = 0;
5166 uint32_t resume_handle = 0;
5167 uint32_t preferred_len = 0xffffffff;
5169 struct dcerpc_binding_handle *b = NULL;
5170 struct srvsvc_NetShareInfoCtr info_ctr;
5171 struct srvsvc_NetShareCtr1 ctr1;
5174 struct user_token *tokens = NULL;
5180 f = fopen(argv[0], "r");
5184 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
5185 return NT_STATUS_UNSUCCESSFUL;
5188 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5194 DEBUG(0, ("Could not read users from file\n"));
5195 return NT_STATUS_UNSUCCESSFUL;
5198 for (i=0; i<num_tokens; i++)
5199 collect_alias_memberships(&tokens[i].token);
5201 ZERO_STRUCT(info_ctr);
5205 info_ctr.ctr.ctr1 = &ctr1;
5207 b = pipe_hnd->binding_handle;
5209 /* Issue the NetShareEnum RPC call and retrieve the response */
5210 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5219 /* Was it successful? */
5220 if (!NT_STATUS_IS_OK(nt_status)) {
5221 /* Nope. Go clean up. */
5225 if (!W_ERROR_IS_OK(result)) {
5226 /* Nope. Go clean up. */
5227 nt_status = werror_to_ntstatus(result);
5231 if (total_entries == 0) {
5235 /* For each returned entry... */
5236 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5237 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5239 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5243 d_printf("%s\n", netname);
5245 show_userlist(pipe_hnd, cli, mem_ctx, netname,
5246 num_tokens, tokens);
5249 for (i=0; i<num_tokens; i++) {
5250 free_user_token(&tokens[i].token);
5257 static int rpc_share_allowedusers(struct net_context *c, int argc,
5262 if (c->display_usage) {
5264 "net rpc share allowedusers\n"
5267 _("List allowed users"));
5271 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5272 rpc_aliaslist_internals,
5277 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5283 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5284 rpc_share_allowedusers_internals,
5288 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5291 struct user_token *tokens = NULL;
5295 net_usersidlist_usage(c, argc, argv);
5299 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5300 DEBUG(0, ("Could not get the user/sid list\n"));
5304 for (i=0; i<num_tokens; i++) {
5305 dump_user_token(&tokens[i]);
5306 free_user_token(&tokens[i].token);
5313 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5315 d_printf(_("net usersidlist\n"
5316 "\tprints out a list of all users the running winbind knows\n"
5317 "\tabout, together with all their SIDs. This is used as\n"
5318 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5320 net_common_flags_usage(c, argc, argv);
5325 * 'net rpc share' entrypoint.
5326 * @param argc Standard main() style argc.
5327 * @param argv Standard main() style argv. Initial components are already
5331 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5333 NET_API_STATUS status;
5335 struct functable func[] = {
5341 N_("net rpc share add\n"
5349 N_("net rpc share delete\n"
5354 rpc_share_allowedusers,
5356 N_("Modify allowed users"),
5357 N_("net rpc share allowedusers\n"
5358 " Modify allowed users")
5364 N_("Migrate share to local server"),
5365 N_("net rpc share migrate\n"
5366 " Migrate share to local server")
5373 N_("net rpc share list\n"
5376 {NULL, NULL, 0, NULL, NULL}
5379 status = libnetapi_net_init(&c->netapi_ctx);
5383 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5384 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5385 if (c->opt_kerberos) {
5386 libnetapi_set_use_kerberos(c->netapi_ctx);
5390 if (c->display_usage) {
5395 " Alias for net rpc share list\n"));
5396 net_display_usage_from_functable(func);
5400 return rpc_share_list(c, argc, argv);
5403 return net_run_function(c, argc, argv, "net rpc share", func);
5406 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5407 TALLOC_CTX *mem_ctx,
5408 struct rpc_sh_ctx *ctx,
5409 struct rpc_pipe_client *pipe_hnd,
5410 int argc, const char **argv)
5413 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5416 static NTSTATUS rpc_sh_share_add(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)
5422 NET_API_STATUS status;
5423 uint32_t parm_err = 0;
5424 struct SHARE_INFO_2 i2;
5426 if ((argc < 2) || (argc > 3)) {
5427 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5429 return NT_STATUS_INVALID_PARAMETER;
5432 i2.shi2_netname = argv[0];
5433 i2.shi2_type = STYPE_DISKTREE;
5434 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5435 i2.shi2_permissions = 0;
5436 i2.shi2_max_uses = 0;
5437 i2.shi2_current_uses = 0;
5438 i2.shi2_path = argv[1];
5439 i2.shi2_passwd = NULL;
5441 status = NetShareAdd(pipe_hnd->desthost,
5446 return werror_to_ntstatus(W_ERROR(status));
5449 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5450 TALLOC_CTX *mem_ctx,
5451 struct rpc_sh_ctx *ctx,
5452 struct rpc_pipe_client *pipe_hnd,
5453 int argc, const char **argv)
5456 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5457 return NT_STATUS_INVALID_PARAMETER;
5460 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5463 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5464 TALLOC_CTX *mem_ctx,
5465 struct rpc_sh_ctx *ctx,
5466 struct rpc_pipe_client *pipe_hnd,
5467 int argc, const char **argv)
5469 union srvsvc_NetShareInfo info;
5472 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5475 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5476 return NT_STATUS_INVALID_PARAMETER;
5479 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5485 if (!NT_STATUS_IS_OK(status)) {
5486 result = ntstatus_to_werror(status);
5489 if (!W_ERROR_IS_OK(result)) {
5493 d_printf(_("Name: %s\n"), info.info2->name);
5494 d_printf(_("Comment: %s\n"), info.info2->comment);
5495 d_printf(_("Path: %s\n"), info.info2->path);
5496 d_printf(_("Password: %s\n"), info.info2->password);
5499 return werror_to_ntstatus(result);
5502 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5503 struct rpc_sh_ctx *ctx)
5505 static struct rpc_sh_cmd cmds[] = {
5507 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5508 N_("List available shares") },
5510 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5511 N_("Add a share") },
5513 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5514 N_("Delete a share") },
5516 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5517 N_("Get information about a share") },
5519 { NULL, NULL, 0, NULL, NULL }
5525 /****************************************************************************/
5527 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5529 return net_file_usage(c, argc, argv);
5533 * Close a file on a remote RPC server.
5535 * @param argc Standard main() style argc.
5536 * @param argv Standard main() style argv. Initial components are already
5539 * @return A shell status integer (0 for success).
5541 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5543 if (argc < 1 || c->display_usage) {
5544 return rpc_file_usage(c, argc, argv);
5547 return NetFileClose(c->opt_host, atoi(argv[0]));
5551 * Formatted print of open file info
5553 * @param r struct FILE_INFO_3 contents
5556 static void display_file_info_3(struct FILE_INFO_3 *r)
5558 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5559 r->fi3_id, r->fi3_username, r->fi3_permissions,
5560 r->fi3_num_locks, r->fi3_pathname);
5564 * List files for a user on a remote RPC server.
5566 * @param argc Standard main() style argc.
5567 * @param argv Standard main() style argv. Initial components are already
5570 * @return A shell status integer (0 for success)..
5573 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5575 NET_API_STATUS status;
5576 uint32 preferred_len = 0xffffffff, i;
5577 char *username=NULL;
5578 uint32_t total_entries = 0;
5579 uint32_t entries_read = 0;
5580 uint32_t resume_handle = 0;
5581 struct FILE_INFO_3 *i3 = NULL;
5583 if (c->display_usage) {
5584 return rpc_file_usage(c, argc, argv);
5587 /* if argc > 0, must be user command */
5589 username = smb_xstrdup(argv[0]);
5592 status = NetFileEnum(c->opt_host,
5596 (uint8_t **)(void *)&i3,
5606 /* Display results */
5609 "\nEnumerating open files on remote server:\n\n"
5610 "\nFileId Opened by Perms Locks Path"
5611 "\n------ --------- ----- ----- ---- \n"));
5612 for (i = 0; i < entries_read; i++) {
5613 display_file_info_3(&i3[i]);
5616 SAFE_FREE(username);
5621 * 'net rpc file' entrypoint.
5622 * @param argc Standard main() style argc.
5623 * @param argv Standard main() style argv. Initial components are already
5627 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5629 NET_API_STATUS status;
5631 struct functable func[] = {
5636 N_("Close opened file"),
5637 N_("net rpc file close\n"
5638 " Close opened file")
5644 N_("List files opened by user"),
5645 N_("net rpc file user\n"
5646 " List files opened by user")
5653 N_("Display information about opened file"),
5654 N_("net rpc file info\n"
5655 " Display information about opened file")
5658 {NULL, NULL, 0, NULL, NULL}
5661 status = libnetapi_net_init(&c->netapi_ctx);
5665 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5666 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5667 if (c->opt_kerberos) {
5668 libnetapi_set_use_kerberos(c->netapi_ctx);
5672 if (c->display_usage) {
5673 d_printf(_("Usage:\n"));
5674 d_printf(_("net rpc file\n"
5675 " List opened files\n"));
5676 net_display_usage_from_functable(func);
5680 return rpc_file_user(c, argc, argv);
5683 return net_run_function(c, argc, argv, "net rpc file", func);
5687 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5689 * All parameters are provided by the run_rpc_command function, except for
5690 * argc, argv which are passed through.
5692 * @param c A net_context structure.
5693 * @param domain_sid The domain sid acquired from the remote server.
5694 * @param cli A cli_state connected to the server.
5695 * @param mem_ctx Talloc context, destroyed on completion of the function.
5696 * @param argc Standard main() style argc.
5697 * @param argv Standard main() style argv. Initial components are already
5700 * @return Normal NTSTATUS return.
5703 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5704 const struct dom_sid *domain_sid,
5705 const char *domain_name,
5706 struct cli_state *cli,
5707 struct rpc_pipe_client *pipe_hnd,
5708 TALLOC_CTX *mem_ctx,
5712 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5714 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5716 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5717 if (!NT_STATUS_IS_OK(status)) {
5720 if (W_ERROR_IS_OK(result)) {
5721 d_printf(_("\nShutdown successfully aborted\n"));
5722 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5724 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5726 return werror_to_ntstatus(result);
5730 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5732 * All parameters are provided by the run_rpc_command function, except for
5733 * argc, argv which are passed through.
5735 * @param c A net_context structure.
5736 * @param domain_sid The domain sid acquired from the remote server.
5737 * @param cli A cli_state connected to the server.
5738 * @param mem_ctx Talloc context, destroyed on completion of the function.
5739 * @param argc Standard main() style argc.
5740 * @param argv Standard main() style argv. Initial components are already
5743 * @return Normal NTSTATUS return.
5746 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5747 const struct dom_sid *domain_sid,
5748 const char *domain_name,
5749 struct cli_state *cli,
5750 struct rpc_pipe_client *pipe_hnd,
5751 TALLOC_CTX *mem_ctx,
5755 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5757 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5759 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5761 if (!NT_STATUS_IS_OK(result)) {
5762 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5765 if (W_ERROR_IS_OK(werr)) {
5766 d_printf(_("\nShutdown successfully aborted\n"));
5767 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5769 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5771 return werror_to_ntstatus(werr);
5775 * ABORT the shutdown of a remote RPC server.
5777 * @param argc Standard main() style argc.
5778 * @param argv Standard main() style argv. Initial components are already
5781 * @return A shell status integer (0 for success).
5784 static int rpc_shutdown_abort(struct net_context *c, int argc,
5789 if (c->display_usage) {
5791 "net rpc abortshutdown\n"
5794 _("Abort a scheduled shutdown"));
5798 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5799 rpc_shutdown_abort_internals, argc, argv);
5804 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5806 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5807 rpc_reg_shutdown_abort_internals,
5812 * Shut down a remote RPC Server via initshutdown pipe.
5814 * All parameters are provided by the run_rpc_command function, except for
5815 * argc, argv which are passed through.
5817 * @param c A net_context structure.
5818 * @param domain_sid The domain sid acquired from the remote server.
5819 * @param cli A cli_state connected to the server.
5820 * @param mem_ctx Talloc context, destroyed on completion of the function.
5821 * @param argc Standard main() style argc.
5822 * @param argv Standard main() style argv. Initial components are already
5825 * @return Normal NTSTATUS return.
5828 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5829 const struct dom_sid *domain_sid,
5830 const char *domain_name,
5831 struct cli_state *cli,
5832 struct rpc_pipe_client *pipe_hnd,
5833 TALLOC_CTX *mem_ctx,
5837 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5839 const char *msg = N_("This machine will be shutdown shortly");
5840 uint32 timeout = 20;
5841 struct lsa_StringLarge msg_string;
5842 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5844 if (c->opt_comment) {
5845 msg = c->opt_comment;
5847 if (c->opt_timeout) {
5848 timeout = c->opt_timeout;
5851 msg_string.string = msg;
5853 /* create an entry */
5854 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5855 &msg_string, timeout, c->opt_force, c->opt_reboot,
5857 if (!NT_STATUS_IS_OK(status)) {
5860 if (W_ERROR_IS_OK(result)) {
5861 d_printf(_("\nShutdown of remote machine succeeded\n"));
5862 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5864 DEBUG(1,("Shutdown of remote machine failed!\n"));
5866 return werror_to_ntstatus(result);
5870 * Shut down a remote RPC Server via winreg pipe.
5872 * All parameters are provided by the run_rpc_command function, except for
5873 * argc, argv which are passed through.
5875 * @param c A net_context structure.
5876 * @param domain_sid The domain sid acquired from the remote server.
5877 * @param cli A cli_state connected to the server.
5878 * @param mem_ctx Talloc context, destroyed on completion of the function.
5879 * @param argc Standard main() style argc.
5880 * @param argv Standard main() style argv. Initial components are already
5883 * @return Normal NTSTATUS return.
5886 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5887 const struct dom_sid *domain_sid,
5888 const char *domain_name,
5889 struct cli_state *cli,
5890 struct rpc_pipe_client *pipe_hnd,
5891 TALLOC_CTX *mem_ctx,
5895 const char *msg = N_("This machine will be shutdown shortly");
5896 uint32 timeout = 20;
5897 struct lsa_StringLarge msg_string;
5900 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5902 if (c->opt_comment) {
5903 msg = c->opt_comment;
5905 msg_string.string = msg;
5907 if (c->opt_timeout) {
5908 timeout = c->opt_timeout;
5911 /* create an entry */
5912 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5913 &msg_string, timeout, c->opt_force, c->opt_reboot,
5915 if (!NT_STATUS_IS_OK(result)) {
5916 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5920 if (W_ERROR_IS_OK(werr)) {
5921 d_printf(_("\nShutdown of remote machine succeeded\n"));
5923 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5924 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5925 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5927 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5930 return werror_to_ntstatus(werr);
5934 * Shut down a remote RPC server.
5936 * @param argc Standard main() style argc.
5937 * @param argv Standard main() style argv. Initial components are already
5940 * @return A shell status integer (0 for success).
5943 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5947 if (c->display_usage) {
5949 "net rpc shutdown\n"
5952 _("Shut down a remote RPC server"));
5956 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5957 rpc_init_shutdown_internals, argc, argv);
5960 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5961 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5962 rpc_reg_shutdown_internals, argc, argv);
5968 /***************************************************************************
5969 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5970 ***************************************************************************/
5973 * Add interdomain trust account to the RPC server.
5974 * All parameters (except for argc and argv) are passed by run_rpc_command
5977 * @param c A net_context structure.
5978 * @param domain_sid The domain sid acquired from the server.
5979 * @param cli A cli_state connected to the server.
5980 * @param mem_ctx Talloc context, destroyed on completion of the function.
5981 * @param argc Standard main() style argc.
5982 * @param argv Standard main() style argv. Initial components are already
5985 * @return normal NTSTATUS return code.
5988 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5989 const struct dom_sid *domain_sid,
5990 const char *domain_name,
5991 struct cli_state *cli,
5992 struct rpc_pipe_client *pipe_hnd,
5993 TALLOC_CTX *mem_ctx,
5997 struct policy_handle connect_pol, domain_pol, user_pol;
5998 NTSTATUS status, result;
6000 struct lsa_String lsa_acct_name;
6002 uint32 acct_flags=0;
6004 uint32_t access_granted = 0;
6005 union samr_UserInfo info;
6006 unsigned int orig_timeout;
6007 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6008 DATA_BLOB session_key = data_blob_null;
6013 _(" net rpc trustdom add <domain_name> "
6014 "<trust password>\n"));
6015 return NT_STATUS_INVALID_PARAMETER;
6019 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6022 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
6023 return NT_STATUS_NO_MEMORY;
6026 if (!strupper_m(acct_name)) {
6027 SAFE_FREE(acct_name);
6028 return NT_STATUS_INVALID_PARAMETER;
6031 init_lsa_String(&lsa_acct_name, acct_name);
6033 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6034 if (!NT_STATUS_IS_OK(status)) {
6035 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
6036 nt_errstr(status)));
6040 /* Get samr policy handle */
6041 status = dcerpc_samr_Connect2(b, mem_ctx,
6043 MAXIMUM_ALLOWED_ACCESS,
6046 if (!NT_STATUS_IS_OK(status)) {
6049 if (!NT_STATUS_IS_OK(result)) {
6054 /* Get domain policy handle */
6055 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6057 MAXIMUM_ALLOWED_ACCESS,
6058 discard_const_p(struct dom_sid2, domain_sid),
6061 if (!NT_STATUS_IS_OK(status)) {
6064 if (!NT_STATUS_IS_OK(result)) {
6069 /* This call can take a long time - allow the server to time out.
6070 * 35 seconds should do it. */
6072 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
6074 /* Create trusting domain's account */
6075 acb_info = ACB_NORMAL;
6076 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
6077 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
6078 SAMR_USER_ACCESS_SET_PASSWORD |
6079 SAMR_USER_ACCESS_GET_ATTRIBUTES |
6080 SAMR_USER_ACCESS_SET_ATTRIBUTES;
6082 status = dcerpc_samr_CreateUser2(b, mem_ctx,
6091 if (!NT_STATUS_IS_OK(status)) {
6094 /* And restore our original timeout. */
6095 rpccli_set_timeout(pipe_hnd, orig_timeout);
6097 if (!NT_STATUS_IS_OK(result)) {
6099 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
6100 acct_name, nt_errstr(result));
6105 struct samr_CryptPassword crypt_pwd;
6107 ZERO_STRUCT(info.info23);
6109 init_samr_CryptPassword(argv[1],
6113 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
6114 SAMR_FIELD_NT_PASSWORD_PRESENT;
6115 info.info23.info.acct_flags = ACB_DOMTRUST;
6116 info.info23.password = crypt_pwd;
6118 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
6123 if (!NT_STATUS_IS_OK(status)) {
6127 if (!NT_STATUS_IS_OK(result)) {
6129 DEBUG(0,("Could not set trust account password: %s\n",
6130 nt_errstr(result)));
6136 SAFE_FREE(acct_name);
6137 data_blob_clear_free(&session_key);
6142 * Create interdomain trust account for a remote domain.
6144 * @param argc Standard argc.
6145 * @param argv Standard argv without initial components.
6147 * @return Integer status (0 means success).
6150 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
6152 if (argc > 0 && !c->display_usage) {
6153 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6154 rpc_trustdom_add_internals, argc, argv);
6158 _("net rpc trustdom add <domain_name> <trust "
6166 * Remove interdomain trust account from the RPC server.
6167 * All parameters (except for argc and argv) are passed by run_rpc_command
6170 * @param c A net_context structure.
6171 * @param domain_sid The domain sid acquired from the server.
6172 * @param cli A cli_state connected to the server.
6173 * @param mem_ctx Talloc context, destroyed on completion of the function.
6174 * @param argc Standard main() style argc.
6175 * @param argv Standard main() style argv. Initial components are already
6178 * @return normal NTSTATUS return code.
6181 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6182 const struct dom_sid *domain_sid,
6183 const char *domain_name,
6184 struct cli_state *cli,
6185 struct rpc_pipe_client *pipe_hnd,
6186 TALLOC_CTX *mem_ctx,
6190 struct policy_handle connect_pol, domain_pol, user_pol;
6191 NTSTATUS status, result;
6193 struct dom_sid trust_acct_sid;
6194 struct samr_Ids user_rids, name_types;
6195 struct lsa_String lsa_acct_name;
6196 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6201 _(" net rpc trustdom del <domain_name>\n"));
6202 return NT_STATUS_INVALID_PARAMETER;
6206 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6208 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6210 if (acct_name == NULL)
6211 return NT_STATUS_NO_MEMORY;
6213 if (!strupper_m(acct_name)) {
6214 TALLOC_FREE(acct_name);
6215 return NT_STATUS_INVALID_PARAMETER;
6218 /* Get samr policy handle */
6219 status = dcerpc_samr_Connect2(b, mem_ctx,
6221 MAXIMUM_ALLOWED_ACCESS,
6224 if (!NT_STATUS_IS_OK(status)) {
6227 if (!NT_STATUS_IS_OK(result)) {
6232 /* Get domain policy handle */
6233 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6235 MAXIMUM_ALLOWED_ACCESS,
6236 discard_const_p(struct dom_sid2, domain_sid),
6239 if (!NT_STATUS_IS_OK(status)) {
6242 if (!NT_STATUS_IS_OK(result)) {
6247 init_lsa_String(&lsa_acct_name, acct_name);
6249 status = dcerpc_samr_LookupNames(b, mem_ctx,
6256 if (!NT_STATUS_IS_OK(status)) {
6257 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6259 acct_name, nt_errstr(status));
6262 if (!NT_STATUS_IS_OK(result)) {
6264 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6266 acct_name, nt_errstr(result) );
6269 if (user_rids.count != 1) {
6270 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6273 if (name_types.count != 1) {
6274 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6278 status = dcerpc_samr_OpenUser(b, mem_ctx,
6280 MAXIMUM_ALLOWED_ACCESS,
6284 if (!NT_STATUS_IS_OK(status)) {
6285 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6287 acct_name, nt_errstr(status) );
6291 if (!NT_STATUS_IS_OK(result)) {
6293 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6295 acct_name, nt_errstr(result) );
6299 /* append the rid to the domain sid */
6300 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6304 /* remove the sid */
6306 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6310 if (!NT_STATUS_IS_OK(status)) {
6311 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6312 " on user %s failed %s\n"),
6313 acct_name, nt_errstr(status));
6316 if (!NT_STATUS_IS_OK(result)) {
6318 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6319 " on user %s failed %s\n"),
6320 acct_name, nt_errstr(result) );
6327 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6330 if (!NT_STATUS_IS_OK(status)) {
6331 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6333 acct_name, nt_errstr(status));
6337 if (!NT_STATUS_IS_OK(result)) {
6339 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6341 acct_name, nt_errstr(result) );
6345 if (!NT_STATUS_IS_OK(result)) {
6346 d_printf(_("Could not set trust account password: %s\n"),
6356 * Delete interdomain trust account for a remote domain.
6358 * @param argc Standard argc.
6359 * @param argv Standard argv without initial components.
6361 * @return Integer status (0 means success).
6364 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6366 if (argc > 0 && !c->display_usage) {
6367 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6368 rpc_trustdom_del_internals, argc, argv);
6372 _("net rpc trustdom del <domain>\n"));
6377 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6378 struct cli_state *cli,
6379 TALLOC_CTX *mem_ctx,
6380 const char *domain_name)
6382 char *dc_name = NULL;
6383 const char *buffer = NULL;
6384 struct rpc_pipe_client *netr;
6387 struct dcerpc_binding_handle *b;
6389 /* Use NetServerEnum2 */
6391 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6393 return NT_STATUS_OK;
6396 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6397 for domain %s\n", domain_name));
6399 /* Try netr_GetDcName */
6401 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
6403 if (!NT_STATUS_IS_OK(status)) {
6407 b = netr->binding_handle;
6409 status = dcerpc_netr_GetDcName(b, mem_ctx,
6416 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6420 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6421 for domain %s\n", domain_name));
6423 if (!NT_STATUS_IS_OK(status)) {
6427 return werror_to_ntstatus(result);
6431 * Establish trust relationship to a trusting domain.
6432 * Interdomain account must already be created on remote PDC.
6434 * @param c A net_context structure.
6435 * @param argc Standard argc.
6436 * @param argv Standard argv without initial components.
6438 * @return Integer status (0 means success).
6441 static int rpc_trustdom_establish(struct net_context *c, int argc,
6444 struct cli_state *cli = NULL;
6445 struct sockaddr_storage server_ss;
6446 struct rpc_pipe_client *pipe_hnd = NULL;
6447 struct policy_handle connect_hnd;
6448 TALLOC_CTX *mem_ctx;
6449 NTSTATUS nt_status, result;
6450 struct dom_sid *domain_sid;
6455 union lsa_PolicyInformation *info = NULL;
6456 struct dcerpc_binding_handle *b;
6459 * Connect to \\server\ipc$ as 'our domain' account with password
6462 if (argc != 1 || c->display_usage) {
6465 _("net rpc trustdom establish <domain_name>\n"));
6469 domain_name = smb_xstrdup(argv[0]);
6470 if (!strupper_m(domain_name)) {
6471 SAFE_FREE(domain_name);
6475 /* account name used at first is our domain's name with '$' */
6476 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6479 if (!strupper_m(acct_name)) {
6480 SAFE_FREE(domain_name);
6481 SAFE_FREE(acct_name);
6486 * opt_workgroup will be used by connection functions further,
6487 * hence it should be set to remote domain name instead of ours
6489 if (c->opt_workgroup) {
6490 c->opt_workgroup = smb_xstrdup(domain_name);
6493 c->opt_user_name = acct_name;
6495 /* find the domain controller */
6496 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6497 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6501 /* connect to ipc$ as username/password */
6502 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6503 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6505 /* Is it trusting domain account for sure ? */
6506 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6507 nt_errstr(nt_status)));
6511 /* store who we connected to */
6513 saf_store( domain_name, pdc_name );
6516 * Connect to \\server\ipc$ again (this time anonymously)
6519 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6522 if (NT_STATUS_IS_ERR(nt_status)) {
6523 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6524 domain_name, nt_errstr(nt_status)));
6528 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6529 "domain %s", domain_name))) {
6530 DEBUG(0, ("talloc_init() failed\n"));
6535 /* Make sure we're talking to a proper server */
6537 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6538 if (!NT_STATUS_IS_OK(nt_status)) {
6540 talloc_destroy(mem_ctx);
6545 * Call LsaOpenPolicy and LsaQueryInfo
6548 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6550 if (!NT_STATUS_IS_OK(nt_status)) {
6551 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6553 talloc_destroy(mem_ctx);
6557 b = pipe_hnd->binding_handle;
6559 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6561 if (NT_STATUS_IS_ERR(nt_status)) {
6562 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6563 nt_errstr(nt_status)));
6565 talloc_destroy(mem_ctx);
6569 /* Querying info level 5 */
6571 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6573 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6576 if (NT_STATUS_IS_ERR(nt_status)) {
6577 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6578 nt_errstr(nt_status)));
6580 talloc_destroy(mem_ctx);
6583 if (NT_STATUS_IS_ERR(result)) {
6584 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6585 nt_errstr(result)));
6587 talloc_destroy(mem_ctx);
6591 domain_sid = info->account_domain.sid;
6593 /* There should be actually query info level 3 (following nt serv behaviour),
6594 but I still don't know if it's _really_ necessary */
6597 * Store the password in secrets db
6600 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6601 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6603 talloc_destroy(mem_ctx);
6608 * Close the pipes and clean up
6611 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6612 if (NT_STATUS_IS_ERR(nt_status)) {
6613 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6614 nt_errstr(nt_status)));
6616 talloc_destroy(mem_ctx);
6622 talloc_destroy(mem_ctx);
6624 d_printf(_("Trust to domain %s established\n"), domain_name);
6629 * Revoke trust relationship to the remote domain.
6631 * @param c A net_context structure.
6632 * @param argc Standard argc.
6633 * @param argv Standard argv without initial components.
6635 * @return Integer status (0 means success).
6638 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6644 if (argc < 1 || c->display_usage) {
6647 _("net rpc trustdom revoke <domain_name>\n"
6648 " Revoke trust relationship\n"
6649 " domain_name\tName of domain to revoke trust\n"));
6653 /* generate upper cased domain name */
6654 domain_name = smb_xstrdup(argv[0]);
6655 if (!strupper_m(domain_name)) {
6656 SAFE_FREE(domain_name);
6660 /* delete password of the trust */
6661 if (!pdb_del_trusteddom_pw(domain_name)) {
6662 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6669 SAFE_FREE(domain_name);
6673 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6674 const struct dom_sid *domain_sid,
6675 const char *domain_name,
6676 struct cli_state *cli,
6677 struct rpc_pipe_client *pipe_hnd,
6678 TALLOC_CTX *mem_ctx,
6683 if (!sid_to_fstring(str_sid, domain_sid)) {
6684 return NT_STATUS_UNSUCCESSFUL;
6686 d_printf("%s\n", str_sid);
6687 return NT_STATUS_OK;
6690 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6694 /* convert sid into ascii string */
6695 sid_to_fstring(ascii_sid, dom_sid);
6697 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6700 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6701 TALLOC_CTX *mem_ctx,
6702 struct policy_handle *pol,
6703 struct dom_sid dom_sid,
6704 const char *trusted_dom_name)
6706 NTSTATUS nt_status, result;
6707 union lsa_TrustedDomainInfo *info = NULL;
6708 char *cleartextpwd = NULL;
6709 DATA_BLOB session_key;
6710 DATA_BLOB data = data_blob_null;
6711 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6713 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6716 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6719 if (NT_STATUS_IS_ERR(nt_status)) {
6720 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6721 nt_errstr(nt_status)));
6724 if (NT_STATUS_IS_ERR(result)) {
6726 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6727 nt_errstr(result)));
6731 data = data_blob(info->password.password->data,
6732 info->password.password->length);
6734 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6735 if (!NT_STATUS_IS_OK(nt_status)) {
6736 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6740 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6741 data_blob_free(&session_key);
6743 if (cleartextpwd == NULL) {
6744 DEBUG(0,("retrieved NULL password\n"));
6745 nt_status = NT_STATUS_UNSUCCESSFUL;
6749 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6750 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6751 nt_status = NT_STATUS_UNSUCCESSFUL;
6755 #ifdef DEBUG_PASSWORD
6756 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6757 "password: [%s]\n", trusted_dom_name,
6758 sid_string_dbg(&dom_sid), cleartextpwd));
6762 SAFE_FREE(cleartextpwd);
6763 data_blob_free(&data);
6768 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6771 /* common variables */
6772 TALLOC_CTX* mem_ctx;
6773 struct cli_state *cli = NULL;
6774 struct rpc_pipe_client *pipe_hnd = NULL;
6775 NTSTATUS nt_status, result;
6776 const char *domain_name = NULL;
6777 struct policy_handle connect_hnd;
6778 union lsa_PolicyInformation *info = NULL;
6780 /* trusted domains listing variables */
6781 unsigned int enum_ctx = 0;
6783 struct lsa_DomainList dom_list;
6785 struct dcerpc_binding_handle *b;
6787 if (c->display_usage) {
6789 "net rpc trustdom vampire\n"
6792 _("Vampire trust relationship from remote server"));
6797 * Listing trusted domains (stored in secrets.tdb, if local)
6800 mem_ctx = talloc_init("trust relationships vampire");
6803 * set domain and pdc name to local samba server (default)
6804 * or to remote one given in command line
6807 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6808 domain_name = c->opt_workgroup;
6809 c->opt_target_workgroup = c->opt_workgroup;
6811 fstrcpy(pdc_name, lp_netbios_name());
6812 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6813 c->opt_target_workgroup = domain_name;
6816 /* open \PIPE\lsarpc and open policy handle */
6817 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6818 if (!NT_STATUS_IS_OK(nt_status)) {
6819 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6820 nt_errstr(nt_status)));
6821 talloc_destroy(mem_ctx);
6825 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6827 if (!NT_STATUS_IS_OK(nt_status)) {
6828 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6829 nt_errstr(nt_status) ));
6831 talloc_destroy(mem_ctx);
6835 b = pipe_hnd->binding_handle;
6837 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6839 if (NT_STATUS_IS_ERR(nt_status)) {
6840 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6841 nt_errstr(nt_status)));
6843 talloc_destroy(mem_ctx);
6847 /* query info level 5 to obtain sid of a domain being queried */
6848 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6850 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6854 if (NT_STATUS_IS_ERR(nt_status)) {
6855 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6856 nt_errstr(nt_status)));
6858 talloc_destroy(mem_ctx);
6861 if (NT_STATUS_IS_ERR(result)) {
6862 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6863 nt_errstr(result)));
6865 talloc_destroy(mem_ctx);
6870 * Keep calling LsaEnumTrustdom over opened pipe until
6871 * the end of enumeration is reached
6874 d_printf(_("Vampire trusted domains:\n\n"));
6877 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6883 if (NT_STATUS_IS_ERR(nt_status)) {
6884 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6885 nt_errstr(nt_status)));
6887 talloc_destroy(mem_ctx);
6890 if (NT_STATUS_IS_ERR(result)) {
6892 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6893 nt_errstr(result)));
6895 talloc_destroy(mem_ctx);
6900 for (i = 0; i < dom_list.count; i++) {
6902 print_trusted_domain(dom_list.domains[i].sid,
6903 dom_list.domains[i].name.string);
6905 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6906 *dom_list.domains[i].sid,
6907 dom_list.domains[i].name.string);
6908 if (!NT_STATUS_IS_OK(nt_status)) {
6910 talloc_destroy(mem_ctx);
6916 * in case of no trusted domains say something rather
6917 * than just display blank line
6919 if (!dom_list.count) d_printf(_("none\n"));
6921 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6923 /* close this connection before doing next one */
6924 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6925 if (NT_STATUS_IS_ERR(nt_status)) {
6926 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6927 nt_errstr(nt_status)));
6929 talloc_destroy(mem_ctx);
6933 /* close lsarpc pipe and connection to IPC$ */
6936 talloc_destroy(mem_ctx);
6940 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6942 /* common variables */
6943 TALLOC_CTX* mem_ctx;
6944 struct cli_state *cli = NULL, *remote_cli = NULL;
6945 struct rpc_pipe_client *pipe_hnd = NULL;
6946 NTSTATUS nt_status, result;
6947 const char *domain_name = NULL;
6948 struct dom_sid *queried_dom_sid;
6949 int ascii_dom_name_len;
6950 struct policy_handle connect_hnd;
6951 union lsa_PolicyInformation *info = NULL;
6952 struct dcerpc_binding_handle *b = NULL;
6954 /* trusted domains listing variables */
6955 unsigned int num_domains, enum_ctx = 0;
6957 struct lsa_DomainList dom_list;
6961 /* trusting domains listing variables */
6962 struct policy_handle domain_hnd;
6963 struct samr_SamArray *trusts = NULL;
6965 if (c->display_usage) {
6967 "net rpc trustdom list\n"
6970 _("List incoming and outgoing trust relationships"));
6975 * Listing trusted domains (stored in secrets.tdb, if local)
6978 mem_ctx = talloc_init("trust relationships listing");
6981 * set domain and pdc name to local samba server (default)
6982 * or to remote one given in command line
6985 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6986 domain_name = c->opt_workgroup;
6987 c->opt_target_workgroup = c->opt_workgroup;
6989 fstrcpy(pdc_name, lp_netbios_name());
6990 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6991 c->opt_target_workgroup = domain_name;
6994 /* open \PIPE\lsarpc and open policy handle */
6995 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6996 if (!NT_STATUS_IS_OK(nt_status)) {
6997 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6998 nt_errstr(nt_status)));
6999 talloc_destroy(mem_ctx);
7003 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
7005 if (!NT_STATUS_IS_OK(nt_status)) {
7006 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
7007 nt_errstr(nt_status) ));
7009 talloc_destroy(mem_ctx);
7013 b = pipe_hnd->binding_handle;
7015 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
7017 if (NT_STATUS_IS_ERR(nt_status)) {
7018 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
7019 nt_errstr(nt_status)));
7021 talloc_destroy(mem_ctx);
7025 /* query info level 5 to obtain sid of a domain being queried */
7026 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
7028 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
7032 if (NT_STATUS_IS_ERR(nt_status)) {
7033 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7034 nt_errstr(nt_status)));
7036 talloc_destroy(mem_ctx);
7039 if (NT_STATUS_IS_ERR(result)) {
7040 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7041 nt_errstr(result)));
7043 talloc_destroy(mem_ctx);
7047 queried_dom_sid = info->account_domain.sid;
7050 * Keep calling LsaEnumTrustdom over opened pipe until
7051 * the end of enumeration is reached
7054 d_printf(_("Trusted domains list:\n\n"));
7056 found_domain = false;
7059 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
7065 if (NT_STATUS_IS_ERR(nt_status)) {
7066 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7067 nt_errstr(nt_status)));
7069 talloc_destroy(mem_ctx);
7072 if (NT_STATUS_IS_ERR(result)) {
7073 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7074 nt_errstr(result)));
7076 talloc_destroy(mem_ctx);
7081 for (i = 0; i < dom_list.count; i++) {
7082 print_trusted_domain(dom_list.domains[i].sid,
7083 dom_list.domains[i].name.string);
7084 found_domain = true;
7088 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7091 * in case of no trusted domains say something rather
7092 * than just display blank line
7094 if (!found_domain) {
7095 d_printf(_("none\n"));
7098 /* close this connection before doing next one */
7099 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7100 if (NT_STATUS_IS_ERR(nt_status)) {
7101 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7102 nt_errstr(nt_status)));
7104 talloc_destroy(mem_ctx);
7108 TALLOC_FREE(pipe_hnd);
7111 * Listing trusting domains (stored in passdb backend, if local)
7114 d_printf(_("\nTrusting domains list:\n\n"));
7117 * Open \PIPE\samr and get needed policy handles
7119 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
7121 if (!NT_STATUS_IS_OK(nt_status)) {
7122 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
7124 talloc_destroy(mem_ctx);
7128 b = pipe_hnd->binding_handle;
7131 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
7133 SAMR_ACCESS_LOOKUP_DOMAIN,
7136 if (!NT_STATUS_IS_OK(nt_status)) {
7137 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7138 nt_errstr(nt_status)));
7140 talloc_destroy(mem_ctx);
7143 if (!NT_STATUS_IS_OK(result)) {
7145 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7146 nt_errstr(result)));
7148 talloc_destroy(mem_ctx);
7152 /* SamrOpenDomain - we have to open domain policy handle in order to be
7153 able to enumerate accounts*/
7154 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
7156 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
7160 if (!NT_STATUS_IS_OK(nt_status)) {
7161 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7162 nt_errstr(nt_status)));
7164 talloc_destroy(mem_ctx);
7167 if (!NT_STATUS_IS_OK(result)) {
7169 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7170 nt_errstr(result)));
7172 talloc_destroy(mem_ctx);
7177 * perform actual enumeration
7180 found_domain = false;
7182 enum_ctx = 0; /* reset enumeration context from last enumeration */
7185 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
7193 if (NT_STATUS_IS_ERR(nt_status)) {
7194 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7195 nt_errstr(nt_status)));
7197 talloc_destroy(mem_ctx);
7200 if (NT_STATUS_IS_ERR(result)) {
7202 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7203 nt_errstr(result)));
7205 talloc_destroy(mem_ctx);
7209 for (i = 0; i < num_domains; i++) {
7211 char *str = discard_const_p(char, trusts->entries[i].name.string);
7213 found_domain = true;
7216 * get each single domain's sid (do we _really_ need this ?):
7217 * 1) connect to domain's pdc
7218 * 2) query the pdc for domain's sid
7221 /* get rid of '$' tail */
7222 ascii_dom_name_len = strlen(str);
7223 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7224 str[ascii_dom_name_len - 1] = '\0';
7226 /* set opt_* variables to remote domain */
7227 if (!strupper_m(str)) {
7229 talloc_destroy(mem_ctx);
7232 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7233 c->opt_target_workgroup = c->opt_workgroup;
7235 d_printf("%-20s", str);
7237 /* connect to remote domain controller */
7238 nt_status = net_make_ipc_connection(c,
7239 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7241 if (NT_STATUS_IS_OK(nt_status)) {
7242 /* query for domain's sid */
7243 if (run_rpc_command(
7245 &ndr_table_lsarpc, 0,
7246 rpc_query_domain_sid, argc,
7248 d_printf(_("strange - couldn't get domain's sid\n"));
7250 cli_shutdown(remote_cli);
7253 d_fprintf(stderr, _("domain controller is not "
7254 "responding: %s\n"),
7255 nt_errstr(nt_status));
7256 d_printf(_("couldn't get domain's sid\n"));
7260 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7262 if (!found_domain) {
7266 /* close opened samr and domain policy handles */
7267 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7268 if (!NT_STATUS_IS_OK(nt_status)) {
7269 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7272 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7273 if (!NT_STATUS_IS_OK(nt_status)) {
7274 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7277 /* close samr pipe and connection to IPC$ */
7280 talloc_destroy(mem_ctx);
7285 * Entrypoint for 'net rpc trustdom' code.
7287 * @param argc Standard argc.
7288 * @param argv Standard argv without initial components.
7290 * @return Integer status (0 means success).
7293 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7295 struct functable func[] = {
7300 N_("Add trusting domain's account"),
7301 N_("net rpc trustdom add\n"
7302 " Add trusting domain's account")
7308 N_("Remove trusting domain's account"),
7309 N_("net rpc trustdom del\n"
7310 " Remove trusting domain's account")
7314 rpc_trustdom_establish,
7316 N_("Establish outgoing trust relationship"),
7317 N_("net rpc trustdom establish\n"
7318 " Establish outgoing trust relationship")
7322 rpc_trustdom_revoke,
7324 N_("Revoke outgoing trust relationship"),
7325 N_("net rpc trustdom revoke\n"
7326 " Revoke outgoing trust relationship")
7332 N_("List in- and outgoing domain trusts"),
7333 N_("net rpc trustdom list\n"
7334 " List in- and outgoing domain trusts")
7338 rpc_trustdom_vampire,
7340 N_("Vampire trusts from remote server"),
7341 N_("net rpc trustdom vampire\n"
7342 " Vampire trusts from remote server")
7344 {NULL, NULL, 0, NULL, NULL}
7347 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7351 * Check if a server will take rpc commands
7352 * @param flags Type of server to connect to (PDC, DMB, localhost)
7353 * if the host is not explicitly specified
7354 * @return bool (true means rpc supported)
7356 bool net_rpc_check(struct net_context *c, unsigned flags)
7358 struct cli_state *cli;
7360 struct sockaddr_storage server_ss;
7361 char *server_name = NULL;
7364 /* flags (i.e. server type) may depend on command */
7365 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7368 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7369 lp_netbios_name(), SMB_SIGNING_DEFAULT,
7371 if (!NT_STATUS_IS_OK(status)) {
7374 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7376 if (!NT_STATUS_IS_OK(status))
7378 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7387 /* dump sam database via samsync rpc calls */
7388 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7389 if (c->display_usage) {
7394 _("Dump remote SAM database"));
7398 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7399 NET_FLAGS_ANONYMOUS,
7400 rpc_samdump_internals, argc, argv);
7403 /* syncronise sam database via samsync rpc calls */
7404 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7406 struct functable func[] = {
7411 N_("Dump remote SAM database to ldif"),
7412 N_("net rpc vampire ldif\n"
7413 " Dump remote SAM database to LDIF file or "
7420 N_("Dump remote SAM database to Kerberos Keytab"),
7421 N_("net rpc vampire keytab\n"
7422 " Dump remote SAM database to Kerberos keytab "
7429 N_("Dump remote SAM database to passdb"),
7430 N_("net rpc vampire passdb\n"
7431 " Dump remote SAM database to passdb")
7434 {NULL, NULL, 0, NULL, NULL}
7438 if (c->display_usage) {
7443 _("Vampire remote SAM database"));
7447 return rpc_vampire_passdb(c, argc, argv);
7450 return net_run_function(c, argc, argv, "net rpc vampire", func);
7454 * Migrate everything from a print server.
7456 * @param c A net_context structure.
7457 * @param argc Standard main() style argc.
7458 * @param argv Standard main() style argv. Initial components are already
7461 * @return A shell status integer (0 for success).
7463 * The order is important !
7464 * To successfully add drivers the print queues have to exist !
7465 * Applying ACLs should be the last step, because you're easily locked out.
7468 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7473 if (c->display_usage) {
7475 "net rpc printer migrate all\n"
7478 _("Migrate everything from a print server"));
7483 d_printf(_("no server to migrate\n"));
7487 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7488 rpc_printer_migrate_printers_internals, argc,
7493 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7494 rpc_printer_migrate_drivers_internals, argc,
7499 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7500 rpc_printer_migrate_forms_internals, argc, argv);
7504 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7505 rpc_printer_migrate_settings_internals, argc,
7510 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7511 rpc_printer_migrate_security_internals, argc,
7517 * Migrate print drivers from a print server.
7519 * @param c A net_context structure.
7520 * @param argc Standard main() style argc.
7521 * @param argv Standard main() style argv. Initial components are already
7524 * @return A shell status integer (0 for success).
7526 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7529 if (c->display_usage) {
7531 "net rpc printer migrate drivers\n"
7534 _("Migrate print-drivers from a print-server"));
7539 d_printf(_("no server to migrate\n"));
7543 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7544 rpc_printer_migrate_drivers_internals,
7549 * Migrate print-forms from a print-server.
7551 * @param c A net_context structure.
7552 * @param argc Standard main() style argc.
7553 * @param argv Standard main() style argv. Initial components are already
7556 * @return A shell status integer (0 for success).
7558 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7561 if (c->display_usage) {
7563 "net rpc printer migrate forms\n"
7566 _("Migrate print-forms from a print-server"));
7571 d_printf(_("no server to migrate\n"));
7575 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7576 rpc_printer_migrate_forms_internals,
7581 * Migrate printers from a print-server.
7583 * @param c A net_context structure.
7584 * @param argc Standard main() style argc.
7585 * @param argv Standard main() style argv. Initial components are already
7588 * @return A shell status integer (0 for success).
7590 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7593 if (c->display_usage) {
7595 "net rpc printer migrate printers\n"
7598 _("Migrate printers from a print-server"));
7603 d_printf(_("no server to migrate\n"));
7607 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7608 rpc_printer_migrate_printers_internals,
7613 * Migrate printer-ACLs from a print-server
7615 * @param c A net_context structure.
7616 * @param argc Standard main() style argc.
7617 * @param argv Standard main() style argv. Initial components are already
7620 * @return A shell status integer (0 for success).
7622 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7625 if (c->display_usage) {
7627 "net rpc printer migrate security\n"
7630 _("Migrate printer-ACLs from a print-server"));
7635 d_printf(_("no server to migrate\n"));
7639 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7640 rpc_printer_migrate_security_internals,
7645 * Migrate printer-settings from a print-server.
7647 * @param c A net_context structure.
7648 * @param argc Standard main() style argc.
7649 * @param argv Standard main() style argv. Initial components are already
7652 * @return A shell status integer (0 for success).
7654 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7657 if (c->display_usage) {
7659 "net rpc printer migrate settings\n"
7662 _("Migrate printer-settings from a "
7668 d_printf(_("no server to migrate\n"));
7672 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7673 rpc_printer_migrate_settings_internals,
7678 * 'net rpc printer' entrypoint.
7680 * @param c A net_context structure.
7681 * @param argc Standard main() style argc.
7682 * @param argv Standard main() style argv. Initial components are already
7686 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7689 /* ouch: when addriver and setdriver are called from within
7690 rpc_printer_migrate_drivers_internals, the printer-queue already
7693 struct functable func[] = {
7696 rpc_printer_migrate_all,
7698 N_("Migrate all from remote to local print server"),
7699 N_("net rpc printer migrate all\n"
7700 " Migrate all from remote to local print server")
7704 rpc_printer_migrate_drivers,
7706 N_("Migrate drivers to local server"),
7707 N_("net rpc printer migrate drivers\n"
7708 " Migrate drivers to local server")
7712 rpc_printer_migrate_forms,
7714 N_("Migrate froms to local server"),
7715 N_("net rpc printer migrate forms\n"
7716 " Migrate froms to local server")
7720 rpc_printer_migrate_printers,
7722 N_("Migrate printers to local server"),
7723 N_("net rpc printer migrate printers\n"
7724 " Migrate printers to local server")
7728 rpc_printer_migrate_security,
7730 N_("Mirgate printer ACLs to local server"),
7731 N_("net rpc printer migrate security\n"
7732 " Mirgate printer ACLs to local server")
7736 rpc_printer_migrate_settings,
7738 N_("Migrate printer settings to local server"),
7739 N_("net rpc printer migrate settings\n"
7740 " Migrate printer settings to local server")
7742 {NULL, NULL, 0, NULL, NULL}
7745 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7750 * List printers on a remote RPC server.
7752 * @param c A net_context structure.
7753 * @param argc Standard main() style argc.
7754 * @param argv Standard main() style argv. Initial components are already
7757 * @return A shell status integer (0 for success).
7759 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7761 if (c->display_usage) {
7763 "net rpc printer list\n"
7766 _("List printers on a remote RPC server"));
7770 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7771 rpc_printer_list_internals,
7776 * List printer-drivers on a remote RPC server.
7778 * @param c A net_context structure.
7779 * @param argc Standard main() style argc.
7780 * @param argv Standard main() style argv. Initial components are already
7783 * @return A shell status integer (0 for success).
7785 static int rpc_printer_driver_list(struct net_context *c, int argc,
7788 if (c->display_usage) {
7790 "net rpc printer driver\n"
7793 _("List printer-drivers on a remote RPC server"));
7797 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7798 rpc_printer_driver_list_internals,
7803 * Publish printer in ADS via MSRPC.
7805 * @param c A net_context structure.
7806 * @param argc Standard main() style argc.
7807 * @param argv Standard main() style argv. Initial components are already
7810 * @return A shell status integer (0 for success).
7812 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7815 if (c->display_usage) {
7817 "net rpc printer publish publish\n"
7820 _("Publish printer in ADS via MSRPC"));
7824 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7825 rpc_printer_publish_publish_internals,
7830 * Update printer in ADS via MSRPC.
7832 * @param c A net_context structure.
7833 * @param argc Standard main() style argc.
7834 * @param argv Standard main() style argv. Initial components are already
7837 * @return A shell status integer (0 for success).
7839 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7841 if (c->display_usage) {
7843 "net rpc printer publish update\n"
7846 _("Update printer in ADS via MSRPC"));
7850 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7851 rpc_printer_publish_update_internals,
7856 * UnPublish printer in ADS via MSRPC.
7858 * @param c A net_context structure.
7859 * @param argc Standard main() style argc.
7860 * @param argv Standard main() style argv. Initial components are already
7863 * @return A shell status integer (0 for success).
7865 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7868 if (c->display_usage) {
7870 "net rpc printer publish unpublish\n"
7873 _("UnPublish printer in ADS via MSRPC"));
7877 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7878 rpc_printer_publish_unpublish_internals,
7883 * List published printers via MSRPC.
7885 * @param c A net_context structure.
7886 * @param argc Standard main() style argc.
7887 * @param argv Standard main() style argv. Initial components are already
7890 * @return A shell status integer (0 for success).
7892 static int rpc_printer_publish_list(struct net_context *c, int argc,
7895 if (c->display_usage) {
7897 "net rpc printer publish list\n"
7900 _("List published printers via MSRPC"));
7904 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7905 rpc_printer_publish_list_internals,
7911 * Publish printer in ADS.
7913 * @param c A net_context structure.
7914 * @param argc Standard main() style argc.
7915 * @param argv Standard main() style argv. Initial components are already
7918 * @return A shell status integer (0 for success).
7920 static int rpc_printer_publish(struct net_context *c, int argc,
7924 struct functable func[] = {
7927 rpc_printer_publish_publish,
7929 N_("Publish printer in AD"),
7930 N_("net rpc printer publish publish\n"
7931 " Publish printer in AD")
7935 rpc_printer_publish_update,
7937 N_("Update printer in AD"),
7938 N_("net rpc printer publish update\n"
7939 " Update printer in AD")
7943 rpc_printer_publish_unpublish,
7945 N_("Unpublish printer"),
7946 N_("net rpc printer publish unpublish\n"
7947 " Unpublish printer")
7951 rpc_printer_publish_list,
7953 N_("List published printers"),
7954 N_("net rpc printer publish list\n"
7955 " List published printers")
7957 {NULL, NULL, 0, NULL, NULL}
7961 if (c->display_usage) {
7962 d_printf(_("Usage:\n"));
7963 d_printf(_("net rpc printer publish\n"
7964 " List published printers\n"
7965 " Alias of net rpc printer publish "
7967 net_display_usage_from_functable(func);
7970 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7971 rpc_printer_publish_list_internals,
7975 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7981 * Display rpc printer help page.
7983 * @param c A net_context structure.
7984 * @param argc Standard main() style argc.
7985 * @param argv Standard main() style argv. Initial components are already
7988 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7990 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7991 "\tlists all printers on print-server\n\n"));
7992 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7993 "\tlists all printer-drivers on print-server\n\n"));
7994 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7995 "\tpublishes printer settings in Active Directory\n"
7996 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7997 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7998 "\n\tmigrates printers from remote to local server\n\n"));
7999 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
8000 "\n\tmigrates printer-settings from remote to local server\n\n"));
8001 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
8002 "\n\tmigrates printer-drivers from remote to local server\n\n"));
8003 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
8004 "\n\tmigrates printer-forms from remote to local server\n\n"));
8005 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
8006 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
8007 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
8008 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
8009 "\tremote to local print-server\n\n"));
8010 net_common_methods_usage(c, argc, argv);
8011 net_common_flags_usage(c, argc, argv);
8013 "\t-v or --verbose\t\t\tgive verbose output\n"
8014 "\t --destination\t\tmigration target server (default: localhost)\n"));
8020 * 'net rpc printer' entrypoint.
8022 * @param c A net_context structure.
8023 * @param argc Standard main() style argc.
8024 * @param argv Standard main() style argv. Initial components are already
8027 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
8029 struct functable func[] = {
8034 N_("List all printers on print server"),
8035 N_("net rpc printer list\n"
8036 " List all printers on print server")
8040 rpc_printer_migrate,
8042 N_("Migrate printer to local server"),
8043 N_("net rpc printer migrate\n"
8044 " Migrate printer to local server")
8048 rpc_printer_driver_list,
8050 N_("List printer drivers"),
8051 N_("net rpc printer driver\n"
8052 " List printer drivers")
8056 rpc_printer_publish,
8058 N_("Publish printer in AD"),
8059 N_("net rpc printer publish\n"
8060 " Publish printer in AD")
8062 {NULL, NULL, 0, NULL, NULL}
8066 if (c->display_usage) {
8067 d_printf(_("Usage:\n"));
8068 d_printf(_("net rpc printer\n"
8069 " List printers\n"));
8070 net_display_usage_from_functable(func);
8073 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8074 rpc_printer_list_internals,
8078 return net_run_function(c, argc, argv, "net rpc printer", func);
8082 * 'net rpc' entrypoint.
8084 * @param c A net_context structure.
8085 * @param argc Standard main() style argc.
8086 * @param argv Standard main() style argv. Initial components are already
8090 int net_rpc(struct net_context *c, int argc, const char **argv)
8092 NET_API_STATUS status;
8094 struct functable func[] = {
8099 N_("Modify global audit settings"),
8100 N_("net rpc audit\n"
8101 " Modify global audit settings")
8107 N_("Show basic info about a domain"),
8109 " Show basic info about a domain")
8115 N_("Join a domain"),
8123 N_("Join a domain created in server manager"),
8124 N_("net rpc oldjoin\n"
8125 " Join a domain created in server manager")
8131 N_("Test that a join is valid"),
8132 N_("net rpc testjoin\n"
8133 " Test that a join is valid")
8139 N_("List/modify users"),
8141 " List/modify users")
8147 N_("Change a user password"),
8148 N_("net rpc password\n"
8149 " Change a user password\n"
8150 " Alias for net rpc user password")
8156 N_("List/modify groups"),
8157 N_("net rpc group\n"
8158 " List/modify groups")
8164 N_("List/modify shares"),
8165 N_("net rpc share\n"
8166 " List/modify shares")
8172 N_("List open files"),
8180 N_("List/modify printers"),
8181 N_("net rpc printer\n"
8182 " List/modify printers")
8186 net_rpc_changetrustpw,
8188 N_("Change trust account password"),
8189 N_("net rpc changetrustpw\n"
8190 " Change trust account password")
8196 N_("Modify domain trusts"),
8197 N_("net rpc trustdom\n"
8198 " Modify domain trusts")
8204 N_("Abort a remote shutdown"),
8205 N_("net rpc abortshutdown\n"
8206 " Abort a remote shutdown")
8212 N_("Shutdown a remote server"),
8213 N_("net rpc shutdown\n"
8214 " Shutdown a remote server")
8220 N_("Dump SAM data of remote NT PDC"),
8221 N_("net rpc samdump\n"
8222 " Dump SAM data of remote NT PDC")
8228 N_("Sync a remote NT PDC's data into local passdb"),
8229 N_("net rpc vampire\n"
8230 " Sync a remote NT PDC's data into local passdb")
8236 N_("Fetch the domain sid into local secrets.tdb"),
8237 N_("net rpc getsid\n"
8238 " Fetch the domain sid into local secrets.tdb")
8244 N_("Manage privileges assigned to SID"),
8245 N_("net rpc rights\n"
8246 " Manage privileges assigned to SID")
8252 N_("Start/stop/query remote services"),
8253 N_("net rpc service\n"
8254 " Start/stop/query remote services")
8260 N_("Manage registry hives"),
8261 N_("net rpc registry\n"
8262 " Manage registry hives")
8268 N_("Open interactive shell on remote server"),
8269 N_("net rpc shell\n"
8270 " Open interactive shell on remote server")
8276 N_("Manage trusts"),
8277 N_("net rpc trust\n"
8284 N_("Configure a remote samba server"),
8286 " Configure a remote samba server")
8288 {NULL, NULL, 0, NULL, NULL}
8291 status = libnetapi_net_init(&c->netapi_ctx);
8295 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8296 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8297 if (c->opt_kerberos) {
8298 libnetapi_set_use_kerberos(c->netapi_ctx);
8300 if (c->opt_ccache) {
8301 libnetapi_set_use_ccache(c->netapi_ctx);
8304 return net_run_function(c, argc, argv, "net rpc", func);