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)) {
3074 if (names.count != this_time) {
3075 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3077 if (types.count != this_time) {
3078 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3080 /* We only have users as members, but make the output
3081 the same as the output of alias members */
3083 for (i = 0; i < this_time; i++) {
3085 if (c->opt_long_list_entries) {
3086 printf("%s-%d %s\\%s %d\n", sid_str,
3087 group_rids[i], domain_name,
3088 names.names[i].string,
3091 printf("%s\\%s\n", domain_name,
3092 names.names[i].string);
3096 num_members -= this_time;
3100 return NT_STATUS_OK;
3103 static NTSTATUS rpc_list_alias_members(struct net_context *c,
3104 struct rpc_pipe_client *pipe_hnd,
3105 struct cli_state *cli,
3106 TALLOC_CTX *mem_ctx,
3107 struct policy_handle *domain_pol,
3110 NTSTATUS result, status;
3111 struct rpc_pipe_client *lsa_pipe;
3112 struct policy_handle alias_pol, lsa_pol;
3114 struct dom_sid *alias_sids;
3117 enum lsa_SidType *types;
3119 struct lsa_SidArray sid_array;
3120 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3122 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3124 MAXIMUM_ALLOWED_ACCESS,
3128 if (!NT_STATUS_IS_OK(status)) {
3131 if (!NT_STATUS_IS_OK(result)) {
3135 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
3139 if (!NT_STATUS_IS_OK(status)) {
3140 d_fprintf(stderr, _("Couldn't list alias members\n"));
3143 if (!NT_STATUS_IS_OK(result)) {
3144 d_fprintf(stderr, _("Couldn't list alias members\n"));
3148 num_members = sid_array.num_sids;
3150 if (num_members == 0) {
3151 return NT_STATUS_OK;
3154 result = cli_rpc_pipe_open_noauth(cli,
3157 if (!NT_STATUS_IS_OK(result)) {
3158 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
3159 nt_errstr(result) );
3163 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
3164 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
3166 if (!NT_STATUS_IS_OK(result)) {
3167 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
3168 TALLOC_FREE(lsa_pipe);
3172 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
3174 d_fprintf(stderr, _("Out of memory\n"));
3175 TALLOC_FREE(lsa_pipe);
3176 return NT_STATUS_NO_MEMORY;
3179 for (i=0; i<num_members; i++) {
3180 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
3183 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
3184 num_members, alias_sids,
3185 &domains, &names, &types);
3187 if (!NT_STATUS_IS_OK(result) &&
3188 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3189 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3190 TALLOC_FREE(lsa_pipe);
3194 for (i = 0; i < num_members; i++) {
3196 sid_to_fstring(sid_str, &alias_sids[i]);
3198 if (c->opt_long_list_entries) {
3199 printf("%s %s\\%s %d\n", sid_str,
3200 domains[i] ? domains[i] : _("*unknown*"),
3201 names[i] ? names[i] : _("*unknown*"), types[i]);
3204 printf("%s\\%s\n", domains[i], names[i]);
3206 printf("%s\n", sid_str);
3210 TALLOC_FREE(lsa_pipe);
3211 return NT_STATUS_OK;
3214 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3215 const struct dom_sid *domain_sid,
3216 const char *domain_name,
3217 struct cli_state *cli,
3218 struct rpc_pipe_client *pipe_hnd,
3219 TALLOC_CTX *mem_ctx,
3223 NTSTATUS result, status;
3224 struct policy_handle connect_pol, domain_pol;
3225 struct samr_Ids rids, rid_types;
3226 struct lsa_String lsa_acct_name;
3227 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3229 /* Get sam policy handle */
3231 status = dcerpc_samr_Connect2(b, mem_ctx,
3233 MAXIMUM_ALLOWED_ACCESS,
3236 if (!NT_STATUS_IS_OK(status)) {
3239 if (!NT_STATUS_IS_OK(result)) {
3243 /* Get domain policy handle */
3245 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3247 MAXIMUM_ALLOWED_ACCESS,
3248 discard_const_p(struct dom_sid2, domain_sid),
3251 if (!NT_STATUS_IS_OK(status)) {
3254 if (!NT_STATUS_IS_OK(result)) {
3258 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3260 status = dcerpc_samr_LookupNames(b, mem_ctx,
3267 if (!NT_STATUS_IS_OK(status)) {
3271 if (!NT_STATUS_IS_OK(result)) {
3273 /* Ok, did not find it in the global sam, try with builtin */
3275 struct dom_sid sid_Builtin;
3277 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3279 sid_copy(&sid_Builtin, &global_sid_Builtin);
3281 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3283 MAXIMUM_ALLOWED_ACCESS,
3287 if (!NT_STATUS_IS_OK(status)) {
3290 if (!NT_STATUS_IS_OK(result)) {
3291 d_fprintf(stderr, _("Couldn't find group %s\n"),
3296 status = dcerpc_samr_LookupNames(b, mem_ctx,
3303 if (!NT_STATUS_IS_OK(status)) {
3306 if (!NT_STATUS_IS_OK(result)) {
3307 d_fprintf(stderr, _("Couldn't find group %s\n"),
3313 if (rids.count != 1) {
3314 d_fprintf(stderr, _("Couldn't find group %s\n"),
3316 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3318 if (rid_types.count != 1) {
3319 d_fprintf(stderr, _("Couldn't find group %s\n"),
3321 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3325 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3326 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3327 domain_sid, &domain_pol,
3331 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3332 return rpc_list_alias_members(c, pipe_hnd, cli, mem_ctx, &domain_pol,
3336 return NT_STATUS_NO_SUCH_GROUP;
3339 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3341 if (argc != 1 || c->display_usage) {
3342 return rpc_group_usage(c, argc, argv);
3345 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3346 rpc_group_members_internals,
3350 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3352 NET_API_STATUS status;
3353 struct GROUP_INFO_0 g0;
3357 d_printf(_("Usage:\n"));
3358 d_printf("net rpc group rename group newname\n");
3362 g0.grpi0_name = argv[1];
3364 status = NetGroupSetInfo(c->opt_host,
3371 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3372 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3380 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3382 if (argc != 2 || c->display_usage) {
3383 return rpc_group_usage(c, argc, argv);
3386 return rpc_group_rename_internals(c, argc, argv);
3390 * 'net rpc group' entrypoint.
3391 * @param argc Standard main() style argc.
3392 * @param argv Standard main() style argv. Initial components are already
3396 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3398 NET_API_STATUS status;
3400 struct functable func[] = {
3405 N_("Create specified group"),
3406 N_("net rpc group add\n"
3407 " Create specified group")
3413 N_("Delete specified group"),
3414 N_("net rpc group delete\n"
3415 " Delete specified group")
3421 N_("Add member to group"),
3422 N_("net rpc group addmem\n"
3423 " Add member to group")
3429 N_("Remove member from group"),
3430 N_("net rpc group delmem\n"
3431 " Remove member from group")
3438 N_("net rpc group list\n"
3445 N_("List group members"),
3446 N_("net rpc group members\n"
3447 " List group members")
3454 N_("net rpc group rename\n"
3457 {NULL, NULL, 0, NULL, NULL}
3460 status = libnetapi_net_init(&c->netapi_ctx);
3464 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3465 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3466 if (c->opt_kerberos) {
3467 libnetapi_set_use_kerberos(c->netapi_ctx);
3471 if (c->display_usage) {
3472 d_printf(_("Usage:\n"));
3473 d_printf(_("net rpc group\n"
3474 " Alias for net rpc group list global "
3475 "local builtin\n"));
3476 net_display_usage_from_functable(func);
3480 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3481 rpc_group_list_internals,
3485 return net_run_function(c, argc, argv, "net rpc group", func);
3488 /****************************************************************************/
3490 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3492 return net_share_usage(c, argc, argv);
3496 * Add a share on a remote RPC server.
3498 * @param argc Standard main() style argc.
3499 * @param argv Standard main() style argv. Initial components are already
3502 * @return A shell status integer (0 for success).
3505 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3507 NET_API_STATUS status;
3510 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3511 uint32 num_users=0, perms=0;
3512 char *password=NULL; /* don't allow a share password */
3513 struct SHARE_INFO_2 i2;
3514 uint32_t parm_error = 0;
3516 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3517 return rpc_share_usage(c, argc, argv);
3520 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3524 path = strchr(sharename, '=');
3531 i2.shi2_netname = sharename;
3532 i2.shi2_type = type;
3533 i2.shi2_remark = c->opt_comment;
3534 i2.shi2_permissions = perms;
3535 i2.shi2_max_uses = c->opt_maxusers;
3536 i2.shi2_current_uses = num_users;
3537 i2.shi2_path = path;
3538 i2.shi2_passwd = password;
3540 status = NetShareAdd(c->opt_host,
3545 printf(_("NetShareAdd failed with: %s\n"),
3546 libnetapi_get_error_string(c->netapi_ctx, status));
3553 * Delete a share on a remote RPC server.
3555 * @param domain_sid The domain sid acquired from the remote server.
3556 * @param argc Standard main() style argc.
3557 * @param argv Standard main() style argv. Initial components are already
3560 * @return A shell status integer (0 for success).
3562 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3564 if (argc < 1 || c->display_usage) {
3565 return rpc_share_usage(c, argc, argv);
3568 return NetShareDel(c->opt_host, argv[0], 0);
3572 * Formatted print of share info
3574 * @param r pointer to SHARE_INFO_1 to format
3577 static void display_share_info_1(struct net_context *c,
3578 struct SHARE_INFO_1 *r)
3580 if (c->opt_long_list_entries) {
3581 d_printf("%-12s %-8.8s %-50s\n",
3583 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3586 d_printf("%s\n", r->shi1_netname);
3590 static WERROR get_share_info(struct net_context *c,
3591 struct rpc_pipe_client *pipe_hnd,
3592 TALLOC_CTX *mem_ctx,
3596 struct srvsvc_NetShareInfoCtr *info_ctr)
3600 union srvsvc_NetShareInfo info;
3601 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3603 /* no specific share requested, enumerate all */
3606 uint32_t preferred_len = 0xffffffff;
3607 uint32_t total_entries = 0;
3608 uint32_t resume_handle = 0;
3610 info_ctr->level = level;
3612 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3619 if (!NT_STATUS_IS_OK(status)) {
3620 return ntstatus_to_werror(status);
3625 /* request just one share */
3626 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3633 if (!NT_STATUS_IS_OK(status)) {
3634 result = ntstatus_to_werror(status);
3638 if (!W_ERROR_IS_OK(result)) {
3643 ZERO_STRUCTP(info_ctr);
3645 info_ctr->level = level;
3650 struct srvsvc_NetShareCtr1 *ctr1;
3652 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3653 W_ERROR_HAVE_NO_MEMORY(ctr1);
3656 ctr1->array = info.info1;
3658 info_ctr->ctr.ctr1 = ctr1;
3664 struct srvsvc_NetShareCtr2 *ctr2;
3666 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3667 W_ERROR_HAVE_NO_MEMORY(ctr2);
3670 ctr2->array = info.info2;
3672 info_ctr->ctr.ctr2 = ctr2;
3678 struct srvsvc_NetShareCtr502 *ctr502;
3680 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3681 W_ERROR_HAVE_NO_MEMORY(ctr502);
3684 ctr502->array = info.info502;
3686 info_ctr->ctr.ctr502 = ctr502;
3696 * 'net rpc share list' entrypoint.
3697 * @param argc Standard main() style argc.
3698 * @param argv Standard main() style argv. Initial components are already
3701 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3703 NET_API_STATUS status;
3704 struct SHARE_INFO_1 *i1 = NULL;
3705 uint32_t entries_read = 0;
3706 uint32_t total_entries = 0;
3707 uint32_t resume_handle = 0;
3708 uint32_t i, level = 1;
3710 if (c->display_usage) {
3712 "net rpc share list\n"
3715 _("List shares on remote server"));
3719 status = NetShareEnum(c->opt_host,
3721 (uint8_t **)(void *)&i1,
3730 /* Display results */
3732 if (c->opt_long_list_entries) {
3734 "\nEnumerating shared resources (exports) on remote server:\n\n"
3735 "\nShare name Type Description\n"
3736 "---------- ---- -----------\n"));
3738 for (i = 0; i < entries_read; i++)
3739 display_share_info_1(c, &i1[i]);
3744 static bool check_share_availability(struct cli_state *cli, const char *netname)
3748 status = cli_tree_connect(cli, netname, "A:", "", 0);
3749 if (!NT_STATUS_IS_OK(status)) {
3750 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3754 status = cli_tdis(cli);
3755 if (!NT_STATUS_IS_OK(status)) {
3756 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3763 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3764 const char *netname, uint32 type)
3766 /* only support disk shares */
3767 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3768 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3773 /* skip builtin shares */
3774 /* FIXME: should print$ be added too ? */
3775 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3776 strequal(netname,"global"))
3779 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3780 printf(_("excluding [%s]\n"), netname);
3784 return check_share_availability(cli, netname);
3788 * Migrate shares from a remote RPC server to the local RPC server.
3790 * All parameters are provided by the run_rpc_command function, except for
3791 * argc, argv which are passed through.
3793 * @param domain_sid The domain sid acquired from the remote server.
3794 * @param cli A cli_state connected to the server.
3795 * @param mem_ctx Talloc context, destroyed on completion of the function.
3796 * @param argc Standard main() style argc.
3797 * @param argv Standard main() style argv. Initial components are already
3800 * @return Normal NTSTATUS return.
3803 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3804 const struct dom_sid *domain_sid,
3805 const char *domain_name,
3806 struct cli_state *cli,
3807 struct rpc_pipe_client *pipe_hnd,
3808 TALLOC_CTX *mem_ctx,
3813 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3814 struct srvsvc_NetShareInfoCtr ctr_src;
3816 struct rpc_pipe_client *srvsvc_pipe = NULL;
3817 struct cli_state *cli_dst = NULL;
3818 uint32 level = 502; /* includes secdesc */
3819 uint32_t parm_error = 0;
3820 struct dcerpc_binding_handle *b;
3822 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3824 if (!W_ERROR_IS_OK(result))
3827 /* connect destination PI_SRVSVC */
3828 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3830 if (!NT_STATUS_IS_OK(nt_status))
3833 b = srvsvc_pipe->binding_handle;
3835 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3837 union srvsvc_NetShareInfo info;
3838 struct srvsvc_NetShareInfo502 info502 =
3839 ctr_src.ctr.ctr502->array[i];
3841 /* reset error-code */
3842 nt_status = NT_STATUS_UNSUCCESSFUL;
3844 if (!check_share_sanity(c, cli, info502.name, info502.type))
3847 /* finally add the share on the dst server */
3849 printf(_("migrating: [%s], path: %s, comment: %s, without "
3851 info502.name, info502.path, info502.comment);
3853 info.info502 = &info502;
3855 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3856 srvsvc_pipe->desthost,
3861 if (!NT_STATUS_IS_OK(nt_status)) {
3862 printf(_("cannot add share: %s\n"),
3863 nt_errstr(nt_status));
3866 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3867 printf(_(" [%s] does already exist\n"),
3872 if (!W_ERROR_IS_OK(result)) {
3873 nt_status = werror_to_ntstatus(result);
3874 printf(_("cannot add share: %s\n"),
3875 win_errstr(result));
3881 nt_status = NT_STATUS_OK;
3885 cli_shutdown(cli_dst);
3893 * Migrate shares from a RPC server to another.
3895 * @param argc Standard main() style argc.
3896 * @param argv Standard main() style argv. Initial components are already
3899 * @return A shell status integer (0 for success).
3901 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3904 if (c->display_usage) {
3906 "net rpc share migrate shares\n"
3909 _("Migrate shares to local server"));
3914 printf(_("no server to migrate\n"));
3918 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3919 rpc_share_migrate_shares_internals,
3926 * @param f file_info
3927 * @param mask current search mask
3928 * @param state arg-pointer
3931 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3932 const char *mask, void *state)
3934 static NTSTATUS nt_status;
3935 static struct copy_clistate *local_state;
3936 static fstring filename, new_mask;
3939 struct net_context *c;
3941 local_state = (struct copy_clistate *)state;
3942 nt_status = NT_STATUS_UNSUCCESSFUL;
3946 if (strequal(f->name, ".") || strequal(f->name, ".."))
3947 return NT_STATUS_OK;
3949 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3952 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3954 DEBUG(3,("got dir: %s\n", f->name));
3956 fstrcpy(dir, local_state->cwd);
3958 fstrcat(dir, f->name);
3960 switch (net_mode_share)
3962 case NET_MODE_SHARE_MIGRATE:
3963 /* create that directory */
3964 nt_status = net_copy_file(c, local_state->mem_ctx,
3965 local_state->cli_share_src,
3966 local_state->cli_share_dst,
3968 c->opt_acls? true : false,
3969 c->opt_attrs? true : false,
3970 c->opt_timestamps? true:false,
3974 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3975 return NT_STATUS_INTERNAL_ERROR;
3978 if (!NT_STATUS_IS_OK(nt_status)) {
3979 printf(_("could not handle dir %s: %s\n"),
3980 dir, nt_errstr(nt_status));
3984 /* search below that directory */
3985 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
3986 return NT_STATUS_NO_MEMORY;
3988 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
3989 return NT_STATUS_NO_MEMORY;
3992 old_dir = local_state->cwd;
3993 local_state->cwd = dir;
3994 nt_status = sync_files(local_state, new_mask);
3995 if (!NT_STATUS_IS_OK(nt_status)) {
3996 printf(_("could not handle files\n"));
3998 local_state->cwd = old_dir;
4005 fstrcpy(filename, local_state->cwd);
4006 fstrcat(filename, "\\");
4007 fstrcat(filename, f->name);
4009 DEBUG(3,("got file: %s\n", filename));
4011 switch (net_mode_share)
4013 case NET_MODE_SHARE_MIGRATE:
4014 nt_status = net_copy_file(c, local_state->mem_ctx,
4015 local_state->cli_share_src,
4016 local_state->cli_share_dst,
4018 c->opt_acls? true : false,
4019 c->opt_attrs? true : false,
4020 c->opt_timestamps? true: false,
4024 d_fprintf(stderr, _("Unsupported file mode %d\n"),
4026 return NT_STATUS_INTERNAL_ERROR;
4029 if (!NT_STATUS_IS_OK(nt_status))
4030 printf(_("could not handle file %s: %s\n"),
4031 filename, nt_errstr(nt_status));
4036 * sync files, can be called recursivly to list files
4037 * and then call copy_fn for each file
4039 * @param cp_clistate pointer to the copy_clistate we work with
4040 * @param mask the current search mask
4042 * @return Boolean result
4044 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
4046 struct cli_state *targetcli;
4047 char *targetpath = NULL;
4050 DEBUG(3,("calling cli_list with mask: %s\n", mask));
4052 status = cli_resolve_path(talloc_tos(), "", NULL,
4053 cp_clistate->cli_share_src,
4054 mask, &targetcli, &targetpath);
4055 if (!NT_STATUS_IS_OK(status)) {
4056 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
4058 mask, nt_errstr(status));
4062 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
4063 copy_fn, cp_clistate);
4064 if (!NT_STATUS_IS_OK(status)) {
4065 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
4066 mask, nt_errstr(status));
4074 * Set the top level directory permissions before we do any further copies.
4075 * Should set up ACL inheritance.
4078 bool copy_top_level_perms(struct net_context *c,
4079 struct copy_clistate *cp_clistate,
4080 const char *sharename)
4082 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4084 switch (net_mode_share) {
4085 case NET_MODE_SHARE_MIGRATE:
4086 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
4087 nt_status = net_copy_fileattr(c,
4088 cp_clistate->mem_ctx,
4089 cp_clistate->cli_share_src,
4090 cp_clistate->cli_share_dst,
4092 c->opt_acls? true : false,
4093 c->opt_attrs? true : false,
4094 c->opt_timestamps? true: false,
4098 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4102 if (!NT_STATUS_IS_OK(nt_status)) {
4103 printf(_("Could handle directory attributes for top level "
4104 "directory of share %s. Error %s\n"),
4105 sharename, nt_errstr(nt_status));
4113 * Sync all files inside a remote share to another share (over smb).
4115 * All parameters are provided by the run_rpc_command function, except for
4116 * argc, argv which are passed through.
4118 * @param domain_sid The domain sid acquired from the remote server.
4119 * @param cli A cli_state connected to the server.
4120 * @param mem_ctx Talloc context, destroyed on completion of the function.
4121 * @param argc Standard main() style argc.
4122 * @param argv Standard main() style argv. Initial components are already
4125 * @return Normal NTSTATUS return.
4128 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
4129 const struct dom_sid *domain_sid,
4130 const char *domain_name,
4131 struct cli_state *cli,
4132 struct rpc_pipe_client *pipe_hnd,
4133 TALLOC_CTX *mem_ctx,
4138 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4139 struct srvsvc_NetShareInfoCtr ctr_src;
4142 struct copy_clistate cp_clistate;
4143 bool got_src_share = false;
4144 bool got_dst_share = false;
4145 const char *mask = "\\*";
4148 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
4150 nt_status = NT_STATUS_NO_MEMORY;
4154 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4157 if (!W_ERROR_IS_OK(result))
4160 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4162 struct srvsvc_NetShareInfo502 info502 =
4163 ctr_src.ctr.ctr502->array[i];
4165 if (!check_share_sanity(c, cli, info502.name, info502.type))
4168 /* one might not want to mirror whole discs :) */
4169 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
4170 d_printf(_("skipping [%s]: builtin/hidden share\n"),
4175 switch (net_mode_share)
4177 case NET_MODE_SHARE_MIGRATE:
4181 d_fprintf(stderr, _("Unsupported mode %d\n"),
4185 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4188 c->opt_acls ? _("including") : _("without"),
4189 c->opt_attrs ? _("including") : _("without"),
4190 c->opt_timestamps ? _("(preserving timestamps)") : "");
4192 cp_clistate.mem_ctx = mem_ctx;
4193 cp_clistate.cli_share_src = NULL;
4194 cp_clistate.cli_share_dst = NULL;
4195 cp_clistate.cwd = NULL;
4196 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4199 /* open share source */
4200 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4201 smbXcli_conn_remote_sockaddr(cli->conn),
4202 smbXcli_conn_remote_name(cli->conn),
4203 info502.name, "A:");
4204 if (!NT_STATUS_IS_OK(nt_status))
4207 got_src_share = true;
4209 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4210 /* open share destination */
4211 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4212 NULL, dst, info502.name, "A:");
4213 if (!NT_STATUS_IS_OK(nt_status))
4216 got_dst_share = true;
4219 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4220 d_fprintf(stderr, _("Could not handle the top level "
4221 "directory permissions for the "
4222 "share: %s\n"), info502.name);
4223 nt_status = NT_STATUS_UNSUCCESSFUL;
4227 nt_status = sync_files(&cp_clistate, mask);
4228 if (!NT_STATUS_IS_OK(nt_status)) {
4229 d_fprintf(stderr, _("could not handle files for share: "
4230 "%s\n"), info502.name);
4235 nt_status = NT_STATUS_OK;
4240 cli_shutdown(cp_clistate.cli_share_src);
4243 cli_shutdown(cp_clistate.cli_share_dst);
4250 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4252 if (c->display_usage) {
4254 "net share migrate files\n"
4257 _("Migrate files to local server"));
4262 d_printf(_("no server to migrate\n"));
4266 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4267 rpc_share_migrate_files_internals,
4272 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4274 * All parameters are provided by the run_rpc_command function, except for
4275 * argc, argv which are passed through.
4277 * @param domain_sid The domain sid acquired from the remote server.
4278 * @param cli A cli_state connected to the server.
4279 * @param mem_ctx Talloc context, destroyed on completion of the function.
4280 * @param argc Standard main() style argc.
4281 * @param argv Standard main() style argv. Initial components are already
4284 * @return Normal NTSTATUS return.
4287 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4288 const struct dom_sid *domain_sid,
4289 const char *domain_name,
4290 struct cli_state *cli,
4291 struct rpc_pipe_client *pipe_hnd,
4292 TALLOC_CTX *mem_ctx,
4297 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4298 struct srvsvc_NetShareInfoCtr ctr_src;
4299 union srvsvc_NetShareInfo info;
4301 struct rpc_pipe_client *srvsvc_pipe = NULL;
4302 struct cli_state *cli_dst = NULL;
4303 uint32 level = 502; /* includes secdesc */
4304 uint32_t parm_error = 0;
4305 struct dcerpc_binding_handle *b;
4307 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4310 if (!W_ERROR_IS_OK(result))
4313 /* connect destination PI_SRVSVC */
4314 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4316 if (!NT_STATUS_IS_OK(nt_status))
4319 b = srvsvc_pipe->binding_handle;
4321 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4323 struct srvsvc_NetShareInfo502 info502 =
4324 ctr_src.ctr.ctr502->array[i];
4326 /* reset error-code */
4327 nt_status = NT_STATUS_UNSUCCESSFUL;
4329 if (!check_share_sanity(c, cli, info502.name, info502.type))
4332 printf(_("migrating: [%s], path: %s, comment: %s, including "
4334 info502.name, info502.path, info502.comment);
4337 display_sec_desc(info502.sd_buf.sd);
4339 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4340 info.info502 = &info502;
4342 /* finally modify the share on the dst server */
4343 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4344 srvsvc_pipe->desthost,
4350 if (!NT_STATUS_IS_OK(nt_status)) {
4351 printf(_("cannot set share-acl: %s\n"),
4352 nt_errstr(nt_status));
4355 if (!W_ERROR_IS_OK(result)) {
4356 nt_status = werror_to_ntstatus(result);
4357 printf(_("cannot set share-acl: %s\n"),
4358 win_errstr(result));
4364 nt_status = NT_STATUS_OK;
4368 cli_shutdown(cli_dst);
4376 * Migrate share-acls from a RPC server to another.
4378 * @param argc Standard main() style argc.
4379 * @param argv Standard main() style argv. Initial components are already
4382 * @return A shell status integer (0 for success).
4384 static int rpc_share_migrate_security(struct net_context *c, int argc,
4387 if (c->display_usage) {
4389 "net rpc share migrate security\n"
4392 _("Migrate share-acls to local server"));
4397 d_printf(_("no server to migrate\n"));
4401 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4402 rpc_share_migrate_security_internals,
4407 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4408 * from one server to another.
4410 * @param argc Standard main() style argc.
4411 * @param argv Standard main() style argv. Initial components are already
4414 * @return A shell status integer (0 for success).
4417 static int rpc_share_migrate_all(struct net_context *c, int argc,
4422 if (c->display_usage) {
4424 "net rpc share migrate all\n"
4427 _("Migrates shares including all share settings"));
4432 d_printf(_("no server to migrate\n"));
4436 /* order is important. we don't want to be locked out by the share-acl
4437 * before copying files - gd */
4439 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4440 rpc_share_migrate_shares_internals, argc, argv);
4444 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4445 rpc_share_migrate_files_internals, argc, argv);
4449 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4450 rpc_share_migrate_security_internals, argc,
4456 * 'net rpc share migrate' entrypoint.
4457 * @param argc Standard main() style argc.
4458 * @param argv Standard main() style argv. Initial components are already
4461 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4464 struct functable func[] = {
4467 rpc_share_migrate_all,
4469 N_("Migrate shares from remote to local server"),
4470 N_("net rpc share migrate all\n"
4471 " Migrate shares from remote to local server")
4475 rpc_share_migrate_files,
4477 N_("Migrate files from remote to local server"),
4478 N_("net rpc share migrate files\n"
4479 " Migrate files from remote to local server")
4483 rpc_share_migrate_security,
4485 N_("Migrate share-ACLs from remote to local server"),
4486 N_("net rpc share migrate security\n"
4487 " Migrate share-ACLs from remote to local server")
4491 rpc_share_migrate_shares,
4493 N_("Migrate shares from remote to local server"),
4494 N_("net rpc share migrate shares\n"
4495 " Migrate shares from remote to local server")
4497 {NULL, NULL, 0, NULL, NULL}
4500 net_mode_share = NET_MODE_SHARE_MIGRATE;
4502 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4508 struct dom_sid *members;
4511 static int num_server_aliases;
4512 static struct full_alias *server_aliases;
4515 * Add an alias to the static list.
4517 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4519 if (server_aliases == NULL)
4520 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4522 server_aliases[num_server_aliases] = *alias;
4523 num_server_aliases += 1;
4527 * For a specific domain on the server, fetch all the aliases
4528 * and their members. Add all of them to the server_aliases.
4531 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4532 TALLOC_CTX *mem_ctx,
4533 struct policy_handle *connect_pol,
4534 const struct dom_sid *domain_sid)
4536 uint32 start_idx, max_entries, num_entries, i;
4537 struct samr_SamArray *groups = NULL;
4538 NTSTATUS result, status;
4539 struct policy_handle domain_pol;
4540 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4542 /* Get domain policy handle */
4544 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4546 MAXIMUM_ALLOWED_ACCESS,
4547 discard_const_p(struct dom_sid2, domain_sid),
4550 if (!NT_STATUS_IS_OK(status)) {
4553 if (!NT_STATUS_IS_OK(result)) {
4561 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4568 if (!NT_STATUS_IS_OK(status)) {
4571 for (i = 0; i < num_entries; i++) {
4573 struct policy_handle alias_pol;
4574 struct full_alias alias;
4575 struct lsa_SidArray sid_array;
4579 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4581 MAXIMUM_ALLOWED_ACCESS,
4582 groups->entries[i].idx,
4585 if (!NT_STATUS_IS_OK(status)) {
4588 if (!NT_STATUS_IS_OK(_result)) {
4593 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4597 if (!NT_STATUS_IS_OK(status)) {
4600 if (!NT_STATUS_IS_OK(_result)) {
4605 alias.num_members = sid_array.num_sids;
4607 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4608 if (!NT_STATUS_IS_OK(status)) {
4611 if (!NT_STATUS_IS_OK(_result)) {
4616 alias.members = NULL;
4618 if (alias.num_members > 0) {
4619 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4621 for (j = 0; j < alias.num_members; j++)
4622 sid_copy(&alias.members[j],
4623 sid_array.sids[j].sid);
4626 sid_compose(&alias.sid, domain_sid,
4627 groups->entries[i].idx);
4629 push_alias(mem_ctx, &alias);
4631 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4633 status = NT_STATUS_OK;
4636 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4642 * Dump server_aliases as names for debugging purposes.
4645 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4646 const struct dom_sid *domain_sid,
4647 const char *domain_name,
4648 struct cli_state *cli,
4649 struct rpc_pipe_client *pipe_hnd,
4650 TALLOC_CTX *mem_ctx,
4656 struct policy_handle lsa_pol;
4657 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4659 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4660 SEC_FLAG_MAXIMUM_ALLOWED,
4662 if (!NT_STATUS_IS_OK(result))
4665 for (i=0; i<num_server_aliases; i++) {
4668 enum lsa_SidType *types;
4671 struct full_alias *alias = &server_aliases[i];
4673 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4675 &domains, &names, &types);
4676 if (!NT_STATUS_IS_OK(result))
4679 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4681 if (alias->num_members == 0) {
4686 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4689 &domains, &names, &types);
4691 if (!NT_STATUS_IS_OK(result) &&
4692 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4695 for (j=0; j<alias->num_members; j++)
4696 DEBUG(1, ("%s\\%s (%d); ",
4697 domains[j] ? domains[j] : "*unknown*",
4698 names[j] ? names[j] : "*unknown*",types[j]));
4702 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4704 return NT_STATUS_OK;
4708 * Fetch a list of all server aliases and their members into
4712 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4713 const struct dom_sid *domain_sid,
4714 const char *domain_name,
4715 struct cli_state *cli,
4716 struct rpc_pipe_client *pipe_hnd,
4717 TALLOC_CTX *mem_ctx,
4721 NTSTATUS result, status;
4722 struct policy_handle connect_pol;
4723 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4725 status = dcerpc_samr_Connect2(b, mem_ctx,
4727 MAXIMUM_ALLOWED_ACCESS,
4730 if (!NT_STATUS_IS_OK(status)) {
4733 if (!NT_STATUS_IS_OK(result)) {
4738 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4739 &global_sid_Builtin);
4740 if (!NT_STATUS_IS_OK(status)) {
4744 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4747 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4752 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4754 token->num_sids = 4;
4756 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4757 d_fprintf(stderr, "malloc %s\n",_("failed"));
4758 token->num_sids = 0;
4762 token->sids[0] = *user_sid;
4763 sid_copy(&token->sids[1], &global_sid_World);
4764 sid_copy(&token->sids[2], &global_sid_Network);
4765 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4768 static void free_user_token(struct security_token *token)
4770 SAFE_FREE(token->sids);
4773 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4775 if (security_token_has_sid(token, sid))
4778 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4783 sid_copy(&token->sids[token->num_sids], sid);
4785 token->num_sids += 1;
4790 struct security_token token;
4793 static void dump_user_token(struct user_token *token)
4797 d_printf("%s\n", token->name);
4799 for (i=0; i<token->token.num_sids; i++) {
4800 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4804 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4808 for (i=0; i<alias->num_members; i++) {
4809 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4816 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4820 for (i=0; i<num_server_aliases; i++) {
4821 if (is_alias_member(&sid, &server_aliases[i]))
4822 add_sid_to_token(token, &server_aliases[i].sid);
4827 * We got a user token with all the SIDs we can know about without asking the
4828 * server directly. These are the user and domain group sids. All of these can
4829 * be members of aliases. So scan the list of aliases for each of the SIDs and
4830 * add them to the token.
4833 static void collect_alias_memberships(struct security_token *token)
4835 int num_global_sids = token->num_sids;
4838 for (i=0; i<num_global_sids; i++) {
4839 collect_sid_memberships(token, token->sids[i]);
4843 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4845 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4846 enum wbcSidType type;
4848 struct wbcDomainSid wsid;
4849 char sid_str[WBC_SID_STRING_BUFLEN];
4850 struct dom_sid user_sid;
4851 uint32_t num_groups;
4852 gid_t *groups = NULL;
4855 fstr_sprintf(full_name, "%s%c%s",
4856 domain, *lp_winbind_separator(), user);
4858 /* First let's find out the user sid */
4860 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4862 if (!WBC_ERROR_IS_OK(wbc_status)) {
4863 DEBUG(1, ("winbind could not find %s: %s\n",
4864 full_name, wbcErrorString(wbc_status)));
4868 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4870 if (type != WBC_SID_NAME_USER) {
4871 DEBUG(1, ("%s is not a user\n", full_name));
4875 if (!string_to_sid(&user_sid, sid_str)) {
4876 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4880 init_user_token(token, &user_sid);
4882 /* And now the groups winbind knows about */
4884 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4885 if (!WBC_ERROR_IS_OK(wbc_status)) {
4886 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4887 full_name, wbcErrorString(wbc_status)));
4891 for (i = 0; i < num_groups; i++) {
4892 gid_t gid = groups[i];
4896 wbc_status = wbcGidToSid(gid, &wsid);
4897 if (!WBC_ERROR_IS_OK(wbc_status)) {
4898 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4899 (unsigned int)gid, wbcErrorString(wbc_status)));
4900 wbcFreeMemory(groups);
4904 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4906 DEBUG(3, (" %s\n", sid_str));
4908 ok = string_to_sid(&sid, sid_str);
4910 DEBUG(1, ("Failed to convert string to SID\n"));
4911 wbcFreeMemory(groups);
4914 add_sid_to_token(token, &sid);
4916 wbcFreeMemory(groups);
4922 * Get a list of all user tokens we want to look at
4925 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4926 struct user_token **user_tokens)
4928 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4929 uint32_t i, num_users;
4931 struct user_token *result;
4932 TALLOC_CTX *frame = NULL;
4934 if (lp_winbind_use_default_domain() &&
4935 (c->opt_target_workgroup == NULL)) {
4936 d_fprintf(stderr, _("winbind use default domain = yes set, "
4937 "please specify a workgroup\n"));
4941 /* Send request to winbind daemon */
4943 wbc_status = wbcListUsers(NULL, &num_users, &users);
4944 if (!WBC_ERROR_IS_OK(wbc_status)) {
4945 DEBUG(1, (_("winbind could not list users: %s\n"),
4946 wbcErrorString(wbc_status)));
4950 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4952 if (result == NULL) {
4953 DEBUG(1, ("Could not malloc sid array\n"));
4954 wbcFreeMemory(users);
4958 frame = talloc_stackframe();
4959 for (i=0; i < num_users; i++) {
4960 fstring domain, user;
4963 fstrcpy(result[i].name, users[i]);
4965 p = strchr(users[i], *lp_winbind_separator());
4967 DEBUG(3, ("%s\n", users[i]));
4970 fstrcpy(domain, c->opt_target_workgroup);
4971 fstrcpy(user, users[i]);
4974 fstrcpy(domain, users[i]);
4975 if (!strupper_m(domain)) {
4976 DEBUG(1, ("strupper_m %s failed\n", domain));
4977 wbcFreeMemory(users);
4983 get_user_sids(domain, user, &(result[i].token));
4986 wbcFreeMemory(users);
4988 *num_tokens = num_users;
4989 *user_tokens = result;
4994 static bool get_user_tokens_from_file(FILE *f,
4996 struct user_token **tokens)
4998 struct user_token *token = NULL;
5003 if (fgets(line, sizeof(line)-1, f) == NULL) {
5007 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
5008 line[strlen(line)-1] = '\0';
5011 if (line[0] == ' ') {
5015 if(!string_to_sid(&sid, &line[1])) {
5016 DEBUG(1,("get_user_tokens_from_file: Could "
5017 "not convert sid %s \n",&line[1]));
5021 if (token == NULL) {
5022 DEBUG(0, ("File does not begin with username"));
5026 add_sid_to_token(&token->token, &sid);
5030 /* And a new user... */
5033 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
5034 if (*tokens == NULL) {
5035 DEBUG(0, ("Could not realloc tokens\n"));
5039 token = &((*tokens)[*num_tokens-1]);
5041 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
5044 token->token.num_sids = 0;
5045 token->token.sids = NULL;
5054 * Show the list of all users that have access to a share
5057 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
5058 struct cli_state *cli,
5059 TALLOC_CTX *mem_ctx,
5060 const char *netname,
5062 struct user_token *tokens)
5065 struct security_descriptor *share_sd = NULL;
5066 struct security_descriptor *root_sd = NULL;
5068 union srvsvc_NetShareInfo info;
5072 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5074 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5081 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5082 DEBUG(1, ("Coult not query secdesc for share %s\n",
5087 share_sd = info.info502->sd_buf.sd;
5088 if (share_sd == NULL) {
5089 DEBUG(1, ("Got no secdesc for share %s\n",
5093 cnum = cli_state_get_tid(cli);
5095 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", "", 0))) {
5099 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
5100 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
5101 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
5104 for (i=0; i<num_tokens; i++) {
5107 if (share_sd != NULL) {
5108 status = se_access_check(share_sd, &tokens[i].token,
5111 if (!NT_STATUS_IS_OK(status)) {
5112 DEBUG(1, ("Could not check share_sd for "
5119 if (root_sd == NULL) {
5120 d_printf(" %s\n", tokens[i].name);
5124 status = se_access_check(root_sd, &tokens[i].token,
5126 if (!NT_STATUS_IS_OK(status)) {
5127 DEBUG(1, ("Could not check root_sd for user %s\n",
5131 d_printf(" %s\n", tokens[i].name);
5134 if (fnum != (uint16_t)-1)
5135 cli_close(cli, fnum);
5137 cli_state_set_tid(cli, cnum);
5143 * List shares on a remote RPC server, including the security descriptors.
5145 * All parameters are provided by the run_rpc_command function, except for
5146 * argc, argv which are passed through.
5148 * @param domain_sid The domain sid acquired from the remote server.
5149 * @param cli A cli_state connected to the server.
5150 * @param mem_ctx Talloc context, destroyed on completion of the function.
5151 * @param argc Standard main() style argc.
5152 * @param argv Standard main() style argv. Initial components are already
5155 * @return Normal NTSTATUS return.
5158 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
5159 const struct dom_sid *domain_sid,
5160 const char *domain_name,
5161 struct cli_state *cli,
5162 struct rpc_pipe_client *pipe_hnd,
5163 TALLOC_CTX *mem_ctx,
5169 NTSTATUS nt_status = NT_STATUS_OK;
5170 uint32_t total_entries = 0;
5171 uint32_t resume_handle = 0;
5172 uint32_t preferred_len = 0xffffffff;
5174 struct dcerpc_binding_handle *b = NULL;
5175 struct srvsvc_NetShareInfoCtr info_ctr;
5176 struct srvsvc_NetShareCtr1 ctr1;
5179 struct user_token *tokens = NULL;
5185 f = fopen(argv[0], "r");
5189 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
5190 return NT_STATUS_UNSUCCESSFUL;
5193 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5199 DEBUG(0, ("Could not read users from file\n"));
5200 return NT_STATUS_UNSUCCESSFUL;
5203 for (i=0; i<num_tokens; i++)
5204 collect_alias_memberships(&tokens[i].token);
5206 ZERO_STRUCT(info_ctr);
5210 info_ctr.ctr.ctr1 = &ctr1;
5212 b = pipe_hnd->binding_handle;
5214 /* Issue the NetShareEnum RPC call and retrieve the response */
5215 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5224 /* Was it successful? */
5225 if (!NT_STATUS_IS_OK(nt_status)) {
5226 /* Nope. Go clean up. */
5230 if (!W_ERROR_IS_OK(result)) {
5231 /* Nope. Go clean up. */
5232 nt_status = werror_to_ntstatus(result);
5236 if (total_entries == 0) {
5240 /* For each returned entry... */
5241 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5242 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5244 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5248 d_printf("%s\n", netname);
5250 show_userlist(pipe_hnd, cli, mem_ctx, netname,
5251 num_tokens, tokens);
5254 for (i=0; i<num_tokens; i++) {
5255 free_user_token(&tokens[i].token);
5262 static int rpc_share_allowedusers(struct net_context *c, int argc,
5267 if (c->display_usage) {
5269 "net rpc share allowedusers\n"
5272 _("List allowed users"));
5276 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5277 rpc_aliaslist_internals,
5282 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5288 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5289 rpc_share_allowedusers_internals,
5293 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5296 struct user_token *tokens = NULL;
5300 net_usersidlist_usage(c, argc, argv);
5304 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5305 DEBUG(0, ("Could not get the user/sid list\n"));
5309 for (i=0; i<num_tokens; i++) {
5310 dump_user_token(&tokens[i]);
5311 free_user_token(&tokens[i].token);
5318 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5320 d_printf(_("net usersidlist\n"
5321 "\tprints out a list of all users the running winbind knows\n"
5322 "\tabout, together with all their SIDs. This is used as\n"
5323 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5325 net_common_flags_usage(c, argc, argv);
5330 * 'net rpc share' entrypoint.
5331 * @param argc Standard main() style argc.
5332 * @param argv Standard main() style argv. Initial components are already
5336 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5338 NET_API_STATUS status;
5340 struct functable func[] = {
5346 N_("net rpc share add\n"
5354 N_("net rpc share delete\n"
5359 rpc_share_allowedusers,
5361 N_("Modify allowed users"),
5362 N_("net rpc share allowedusers\n"
5363 " Modify allowed users")
5369 N_("Migrate share to local server"),
5370 N_("net rpc share migrate\n"
5371 " Migrate share to local server")
5378 N_("net rpc share list\n"
5381 {NULL, NULL, 0, NULL, NULL}
5384 status = libnetapi_net_init(&c->netapi_ctx);
5388 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5389 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5390 if (c->opt_kerberos) {
5391 libnetapi_set_use_kerberos(c->netapi_ctx);
5395 if (c->display_usage) {
5400 " Alias for net rpc share list\n"));
5401 net_display_usage_from_functable(func);
5405 return rpc_share_list(c, argc, argv);
5408 return net_run_function(c, argc, argv, "net rpc share", func);
5411 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5412 TALLOC_CTX *mem_ctx,
5413 struct rpc_sh_ctx *ctx,
5414 struct rpc_pipe_client *pipe_hnd,
5415 int argc, const char **argv)
5418 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5421 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5422 TALLOC_CTX *mem_ctx,
5423 struct rpc_sh_ctx *ctx,
5424 struct rpc_pipe_client *pipe_hnd,
5425 int argc, const char **argv)
5427 NET_API_STATUS status;
5428 uint32_t parm_err = 0;
5429 struct SHARE_INFO_2 i2;
5431 if ((argc < 2) || (argc > 3)) {
5432 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5434 return NT_STATUS_INVALID_PARAMETER;
5437 i2.shi2_netname = argv[0];
5438 i2.shi2_type = STYPE_DISKTREE;
5439 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5440 i2.shi2_permissions = 0;
5441 i2.shi2_max_uses = 0;
5442 i2.shi2_current_uses = 0;
5443 i2.shi2_path = argv[1];
5444 i2.shi2_passwd = NULL;
5446 status = NetShareAdd(pipe_hnd->desthost,
5451 return werror_to_ntstatus(W_ERROR(status));
5454 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5455 TALLOC_CTX *mem_ctx,
5456 struct rpc_sh_ctx *ctx,
5457 struct rpc_pipe_client *pipe_hnd,
5458 int argc, const char **argv)
5461 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5462 return NT_STATUS_INVALID_PARAMETER;
5465 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5468 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5469 TALLOC_CTX *mem_ctx,
5470 struct rpc_sh_ctx *ctx,
5471 struct rpc_pipe_client *pipe_hnd,
5472 int argc, const char **argv)
5474 union srvsvc_NetShareInfo info;
5477 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5480 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5481 return NT_STATUS_INVALID_PARAMETER;
5484 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5490 if (!NT_STATUS_IS_OK(status)) {
5491 result = ntstatus_to_werror(status);
5494 if (!W_ERROR_IS_OK(result)) {
5498 d_printf(_("Name: %s\n"), info.info2->name);
5499 d_printf(_("Comment: %s\n"), info.info2->comment);
5500 d_printf(_("Path: %s\n"), info.info2->path);
5501 d_printf(_("Password: %s\n"), info.info2->password);
5504 return werror_to_ntstatus(result);
5507 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5508 struct rpc_sh_ctx *ctx)
5510 static struct rpc_sh_cmd cmds[] = {
5512 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5513 N_("List available shares") },
5515 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5516 N_("Add a share") },
5518 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5519 N_("Delete a share") },
5521 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5522 N_("Get information about a share") },
5524 { NULL, NULL, 0, NULL, NULL }
5530 /****************************************************************************/
5532 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5534 return net_file_usage(c, argc, argv);
5538 * Close a file on a remote RPC server.
5540 * @param argc Standard main() style argc.
5541 * @param argv Standard main() style argv. Initial components are already
5544 * @return A shell status integer (0 for success).
5546 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5548 if (argc < 1 || c->display_usage) {
5549 return rpc_file_usage(c, argc, argv);
5552 return NetFileClose(c->opt_host, atoi(argv[0]));
5556 * Formatted print of open file info
5558 * @param r struct FILE_INFO_3 contents
5561 static void display_file_info_3(struct FILE_INFO_3 *r)
5563 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5564 r->fi3_id, r->fi3_username, r->fi3_permissions,
5565 r->fi3_num_locks, r->fi3_pathname);
5569 * List files for a user on a remote RPC server.
5571 * @param argc Standard main() style argc.
5572 * @param argv Standard main() style argv. Initial components are already
5575 * @return A shell status integer (0 for success)..
5578 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5580 NET_API_STATUS status;
5581 uint32 preferred_len = 0xffffffff, i;
5582 char *username=NULL;
5583 uint32_t total_entries = 0;
5584 uint32_t entries_read = 0;
5585 uint32_t resume_handle = 0;
5586 struct FILE_INFO_3 *i3 = NULL;
5588 if (c->display_usage) {
5589 return rpc_file_usage(c, argc, argv);
5592 /* if argc > 0, must be user command */
5594 username = smb_xstrdup(argv[0]);
5597 status = NetFileEnum(c->opt_host,
5601 (uint8_t **)(void *)&i3,
5611 /* Display results */
5614 "\nEnumerating open files on remote server:\n\n"
5615 "\nFileId Opened by Perms Locks Path"
5616 "\n------ --------- ----- ----- ---- \n"));
5617 for (i = 0; i < entries_read; i++) {
5618 display_file_info_3(&i3[i]);
5621 SAFE_FREE(username);
5626 * 'net rpc file' entrypoint.
5627 * @param argc Standard main() style argc.
5628 * @param argv Standard main() style argv. Initial components are already
5632 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5634 NET_API_STATUS status;
5636 struct functable func[] = {
5641 N_("Close opened file"),
5642 N_("net rpc file close\n"
5643 " Close opened file")
5649 N_("List files opened by user"),
5650 N_("net rpc file user\n"
5651 " List files opened by user")
5658 N_("Display information about opened file"),
5659 N_("net rpc file info\n"
5660 " Display information about opened file")
5663 {NULL, NULL, 0, NULL, NULL}
5666 status = libnetapi_net_init(&c->netapi_ctx);
5670 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5671 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5672 if (c->opt_kerberos) {
5673 libnetapi_set_use_kerberos(c->netapi_ctx);
5677 if (c->display_usage) {
5678 d_printf(_("Usage:\n"));
5679 d_printf(_("net rpc file\n"
5680 " List opened files\n"));
5681 net_display_usage_from_functable(func);
5685 return rpc_file_user(c, argc, argv);
5688 return net_run_function(c, argc, argv, "net rpc file", func);
5692 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5694 * All parameters are provided by the run_rpc_command function, except for
5695 * argc, argv which are passed through.
5697 * @param c A net_context structure.
5698 * @param domain_sid The domain sid acquired from the remote server.
5699 * @param cli A cli_state connected to the server.
5700 * @param mem_ctx Talloc context, destroyed on completion of the function.
5701 * @param argc Standard main() style argc.
5702 * @param argv Standard main() style argv. Initial components are already
5705 * @return Normal NTSTATUS return.
5708 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5709 const struct dom_sid *domain_sid,
5710 const char *domain_name,
5711 struct cli_state *cli,
5712 struct rpc_pipe_client *pipe_hnd,
5713 TALLOC_CTX *mem_ctx,
5717 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5719 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5721 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5722 if (!NT_STATUS_IS_OK(status)) {
5725 if (W_ERROR_IS_OK(result)) {
5726 d_printf(_("\nShutdown successfully aborted\n"));
5727 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5729 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5731 return werror_to_ntstatus(result);
5735 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5737 * All parameters are provided by the run_rpc_command function, except for
5738 * argc, argv which are passed through.
5740 * @param c A net_context structure.
5741 * @param domain_sid The domain sid acquired from the remote server.
5742 * @param cli A cli_state connected to the server.
5743 * @param mem_ctx Talloc context, destroyed on completion of the function.
5744 * @param argc Standard main() style argc.
5745 * @param argv Standard main() style argv. Initial components are already
5748 * @return Normal NTSTATUS return.
5751 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5752 const struct dom_sid *domain_sid,
5753 const char *domain_name,
5754 struct cli_state *cli,
5755 struct rpc_pipe_client *pipe_hnd,
5756 TALLOC_CTX *mem_ctx,
5760 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5762 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5764 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5766 if (!NT_STATUS_IS_OK(result)) {
5767 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5770 if (W_ERROR_IS_OK(werr)) {
5771 d_printf(_("\nShutdown successfully aborted\n"));
5772 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5774 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5776 return werror_to_ntstatus(werr);
5780 * ABORT the shutdown of a remote RPC server.
5782 * @param argc Standard main() style argc.
5783 * @param argv Standard main() style argv. Initial components are already
5786 * @return A shell status integer (0 for success).
5789 static int rpc_shutdown_abort(struct net_context *c, int argc,
5794 if (c->display_usage) {
5796 "net rpc abortshutdown\n"
5799 _("Abort a scheduled shutdown"));
5803 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5804 rpc_shutdown_abort_internals, argc, argv);
5809 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5811 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5812 rpc_reg_shutdown_abort_internals,
5817 * Shut down a remote RPC Server via initshutdown pipe.
5819 * All parameters are provided by the run_rpc_command function, except for
5820 * argc, argv which are passed through.
5822 * @param c A net_context structure.
5823 * @param domain_sid The domain sid acquired from the remote server.
5824 * @param cli A cli_state connected to the server.
5825 * @param mem_ctx Talloc context, destroyed on completion of the function.
5826 * @param argc Standard main() style argc.
5827 * @param argv Standard main() style argv. Initial components are already
5830 * @return Normal NTSTATUS return.
5833 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5834 const struct dom_sid *domain_sid,
5835 const char *domain_name,
5836 struct cli_state *cli,
5837 struct rpc_pipe_client *pipe_hnd,
5838 TALLOC_CTX *mem_ctx,
5842 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5844 const char *msg = N_("This machine will be shutdown shortly");
5845 uint32 timeout = 20;
5846 struct lsa_StringLarge msg_string;
5847 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5849 if (c->opt_comment) {
5850 msg = c->opt_comment;
5852 if (c->opt_timeout) {
5853 timeout = c->opt_timeout;
5856 msg_string.string = msg;
5858 /* create an entry */
5859 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5860 &msg_string, timeout, c->opt_force, c->opt_reboot,
5862 if (!NT_STATUS_IS_OK(status)) {
5865 if (W_ERROR_IS_OK(result)) {
5866 d_printf(_("\nShutdown of remote machine succeeded\n"));
5867 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5869 DEBUG(1,("Shutdown of remote machine failed!\n"));
5871 return werror_to_ntstatus(result);
5875 * Shut down a remote RPC Server via winreg pipe.
5877 * All parameters are provided by the run_rpc_command function, except for
5878 * argc, argv which are passed through.
5880 * @param c A net_context structure.
5881 * @param domain_sid The domain sid acquired from the remote server.
5882 * @param cli A cli_state connected to the server.
5883 * @param mem_ctx Talloc context, destroyed on completion of the function.
5884 * @param argc Standard main() style argc.
5885 * @param argv Standard main() style argv. Initial components are already
5888 * @return Normal NTSTATUS return.
5891 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5892 const struct dom_sid *domain_sid,
5893 const char *domain_name,
5894 struct cli_state *cli,
5895 struct rpc_pipe_client *pipe_hnd,
5896 TALLOC_CTX *mem_ctx,
5900 const char *msg = N_("This machine will be shutdown shortly");
5901 uint32 timeout = 20;
5902 struct lsa_StringLarge msg_string;
5905 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5907 if (c->opt_comment) {
5908 msg = c->opt_comment;
5910 msg_string.string = msg;
5912 if (c->opt_timeout) {
5913 timeout = c->opt_timeout;
5916 /* create an entry */
5917 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5918 &msg_string, timeout, c->opt_force, c->opt_reboot,
5920 if (!NT_STATUS_IS_OK(result)) {
5921 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5925 if (W_ERROR_IS_OK(werr)) {
5926 d_printf(_("\nShutdown of remote machine succeeded\n"));
5928 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5929 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5930 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5932 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5935 return werror_to_ntstatus(werr);
5939 * Shut down a remote RPC server.
5941 * @param argc Standard main() style argc.
5942 * @param argv Standard main() style argv. Initial components are already
5945 * @return A shell status integer (0 for success).
5948 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5952 if (c->display_usage) {
5954 "net rpc shutdown\n"
5957 _("Shut down a remote RPC server"));
5961 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5962 rpc_init_shutdown_internals, argc, argv);
5965 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5966 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5967 rpc_reg_shutdown_internals, argc, argv);
5973 /***************************************************************************
5974 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5975 ***************************************************************************/
5978 * Add interdomain trust account to the RPC server.
5979 * All parameters (except for argc and argv) are passed by run_rpc_command
5982 * @param c A net_context structure.
5983 * @param domain_sid The domain sid acquired from the server.
5984 * @param cli A cli_state connected to the server.
5985 * @param mem_ctx Talloc context, destroyed on completion of the function.
5986 * @param argc Standard main() style argc.
5987 * @param argv Standard main() style argv. Initial components are already
5990 * @return normal NTSTATUS return code.
5993 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5994 const struct dom_sid *domain_sid,
5995 const char *domain_name,
5996 struct cli_state *cli,
5997 struct rpc_pipe_client *pipe_hnd,
5998 TALLOC_CTX *mem_ctx,
6002 struct policy_handle connect_pol, domain_pol, user_pol;
6003 NTSTATUS status, result;
6005 struct lsa_String lsa_acct_name;
6007 uint32 acct_flags=0;
6009 uint32_t access_granted = 0;
6010 union samr_UserInfo info;
6011 unsigned int orig_timeout;
6012 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6013 DATA_BLOB session_key = data_blob_null;
6018 _(" net rpc trustdom add <domain_name> "
6019 "<trust password>\n"));
6020 return NT_STATUS_INVALID_PARAMETER;
6024 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6027 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
6028 return NT_STATUS_NO_MEMORY;
6031 if (!strupper_m(acct_name)) {
6032 SAFE_FREE(acct_name);
6033 return NT_STATUS_INVALID_PARAMETER;
6036 init_lsa_String(&lsa_acct_name, acct_name);
6038 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6039 if (!NT_STATUS_IS_OK(status)) {
6040 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
6041 nt_errstr(status)));
6045 /* Get samr policy handle */
6046 status = dcerpc_samr_Connect2(b, mem_ctx,
6048 MAXIMUM_ALLOWED_ACCESS,
6051 if (!NT_STATUS_IS_OK(status)) {
6054 if (!NT_STATUS_IS_OK(result)) {
6059 /* Get domain policy handle */
6060 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6062 MAXIMUM_ALLOWED_ACCESS,
6063 discard_const_p(struct dom_sid2, domain_sid),
6066 if (!NT_STATUS_IS_OK(status)) {
6069 if (!NT_STATUS_IS_OK(result)) {
6074 /* This call can take a long time - allow the server to time out.
6075 * 35 seconds should do it. */
6077 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
6079 /* Create trusting domain's account */
6080 acb_info = ACB_NORMAL;
6081 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
6082 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
6083 SAMR_USER_ACCESS_SET_PASSWORD |
6084 SAMR_USER_ACCESS_GET_ATTRIBUTES |
6085 SAMR_USER_ACCESS_SET_ATTRIBUTES;
6087 status = dcerpc_samr_CreateUser2(b, mem_ctx,
6096 if (!NT_STATUS_IS_OK(status)) {
6099 /* And restore our original timeout. */
6100 rpccli_set_timeout(pipe_hnd, orig_timeout);
6102 if (!NT_STATUS_IS_OK(result)) {
6104 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
6105 acct_name, nt_errstr(result));
6110 struct samr_CryptPassword crypt_pwd;
6112 ZERO_STRUCT(info.info23);
6114 init_samr_CryptPassword(argv[1],
6118 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
6119 SAMR_FIELD_NT_PASSWORD_PRESENT;
6120 info.info23.info.acct_flags = ACB_DOMTRUST;
6121 info.info23.password = crypt_pwd;
6123 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
6128 if (!NT_STATUS_IS_OK(status)) {
6132 if (!NT_STATUS_IS_OK(result)) {
6134 DEBUG(0,("Could not set trust account password: %s\n",
6135 nt_errstr(result)));
6141 SAFE_FREE(acct_name);
6142 data_blob_clear_free(&session_key);
6147 * Create interdomain trust account for a remote domain.
6149 * @param argc Standard argc.
6150 * @param argv Standard argv without initial components.
6152 * @return Integer status (0 means success).
6155 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
6157 if (argc > 0 && !c->display_usage) {
6158 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6159 rpc_trustdom_add_internals, argc, argv);
6163 _("net rpc trustdom add <domain_name> <trust "
6171 * Remove interdomain trust account from the RPC server.
6172 * All parameters (except for argc and argv) are passed by run_rpc_command
6175 * @param c A net_context structure.
6176 * @param domain_sid The domain sid acquired from the server.
6177 * @param cli A cli_state connected to the server.
6178 * @param mem_ctx Talloc context, destroyed on completion of the function.
6179 * @param argc Standard main() style argc.
6180 * @param argv Standard main() style argv. Initial components are already
6183 * @return normal NTSTATUS return code.
6186 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6187 const struct dom_sid *domain_sid,
6188 const char *domain_name,
6189 struct cli_state *cli,
6190 struct rpc_pipe_client *pipe_hnd,
6191 TALLOC_CTX *mem_ctx,
6195 struct policy_handle connect_pol, domain_pol, user_pol;
6196 NTSTATUS status, result;
6198 struct dom_sid trust_acct_sid;
6199 struct samr_Ids user_rids, name_types;
6200 struct lsa_String lsa_acct_name;
6201 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6206 _(" net rpc trustdom del <domain_name>\n"));
6207 return NT_STATUS_INVALID_PARAMETER;
6211 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6213 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6215 if (acct_name == NULL)
6216 return NT_STATUS_NO_MEMORY;
6218 if (!strupper_m(acct_name)) {
6219 TALLOC_FREE(acct_name);
6220 return NT_STATUS_INVALID_PARAMETER;
6223 /* Get samr policy handle */
6224 status = dcerpc_samr_Connect2(b, mem_ctx,
6226 MAXIMUM_ALLOWED_ACCESS,
6229 if (!NT_STATUS_IS_OK(status)) {
6232 if (!NT_STATUS_IS_OK(result)) {
6237 /* Get domain policy handle */
6238 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6240 MAXIMUM_ALLOWED_ACCESS,
6241 discard_const_p(struct dom_sid2, domain_sid),
6244 if (!NT_STATUS_IS_OK(status)) {
6247 if (!NT_STATUS_IS_OK(result)) {
6252 init_lsa_String(&lsa_acct_name, acct_name);
6254 status = dcerpc_samr_LookupNames(b, mem_ctx,
6261 if (!NT_STATUS_IS_OK(status)) {
6262 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6264 acct_name, nt_errstr(status));
6267 if (!NT_STATUS_IS_OK(result)) {
6269 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6271 acct_name, nt_errstr(result) );
6274 if (user_rids.count != 1) {
6275 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6278 if (name_types.count != 1) {
6279 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6283 status = dcerpc_samr_OpenUser(b, mem_ctx,
6285 MAXIMUM_ALLOWED_ACCESS,
6289 if (!NT_STATUS_IS_OK(status)) {
6290 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6292 acct_name, nt_errstr(status) );
6296 if (!NT_STATUS_IS_OK(result)) {
6298 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6300 acct_name, nt_errstr(result) );
6304 /* append the rid to the domain sid */
6305 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6309 /* remove the sid */
6311 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6315 if (!NT_STATUS_IS_OK(status)) {
6316 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6317 " on user %s failed %s\n"),
6318 acct_name, nt_errstr(status));
6321 if (!NT_STATUS_IS_OK(result)) {
6323 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6324 " on user %s failed %s\n"),
6325 acct_name, nt_errstr(result) );
6332 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6335 if (!NT_STATUS_IS_OK(status)) {
6336 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6338 acct_name, nt_errstr(status));
6342 if (!NT_STATUS_IS_OK(result)) {
6344 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6346 acct_name, nt_errstr(result) );
6350 if (!NT_STATUS_IS_OK(result)) {
6351 d_printf(_("Could not set trust account password: %s\n"),
6361 * Delete interdomain trust account for a remote domain.
6363 * @param argc Standard argc.
6364 * @param argv Standard argv without initial components.
6366 * @return Integer status (0 means success).
6369 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6371 if (argc > 0 && !c->display_usage) {
6372 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6373 rpc_trustdom_del_internals, argc, argv);
6377 _("net rpc trustdom del <domain>\n"));
6382 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6383 struct cli_state *cli,
6384 TALLOC_CTX *mem_ctx,
6385 const char *domain_name)
6387 char *dc_name = NULL;
6388 const char *buffer = NULL;
6389 struct rpc_pipe_client *netr;
6392 struct dcerpc_binding_handle *b;
6394 /* Use NetServerEnum2 */
6396 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6398 return NT_STATUS_OK;
6401 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6402 for domain %s\n", domain_name));
6404 /* Try netr_GetDcName */
6406 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
6408 if (!NT_STATUS_IS_OK(status)) {
6412 b = netr->binding_handle;
6414 status = dcerpc_netr_GetDcName(b, mem_ctx,
6421 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6425 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6426 for domain %s\n", domain_name));
6428 if (!NT_STATUS_IS_OK(status)) {
6432 return werror_to_ntstatus(result);
6436 * Establish trust relationship to a trusting domain.
6437 * Interdomain account must already be created on remote PDC.
6439 * @param c A net_context structure.
6440 * @param argc Standard argc.
6441 * @param argv Standard argv without initial components.
6443 * @return Integer status (0 means success).
6446 static int rpc_trustdom_establish(struct net_context *c, int argc,
6449 struct cli_state *cli = NULL;
6450 struct sockaddr_storage server_ss;
6451 struct rpc_pipe_client *pipe_hnd = NULL;
6452 struct policy_handle connect_hnd;
6453 TALLOC_CTX *mem_ctx;
6454 NTSTATUS nt_status, result;
6455 struct dom_sid *domain_sid;
6460 union lsa_PolicyInformation *info = NULL;
6461 struct dcerpc_binding_handle *b;
6464 * Connect to \\server\ipc$ as 'our domain' account with password
6467 if (argc != 1 || c->display_usage) {
6470 _("net rpc trustdom establish <domain_name>\n"));
6474 domain_name = smb_xstrdup(argv[0]);
6475 if (!strupper_m(domain_name)) {
6476 SAFE_FREE(domain_name);
6480 /* account name used at first is our domain's name with '$' */
6481 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6484 if (!strupper_m(acct_name)) {
6485 SAFE_FREE(domain_name);
6486 SAFE_FREE(acct_name);
6491 * opt_workgroup will be used by connection functions further,
6492 * hence it should be set to remote domain name instead of ours
6494 if (c->opt_workgroup) {
6495 c->opt_workgroup = smb_xstrdup(domain_name);
6498 c->opt_user_name = acct_name;
6500 /* find the domain controller */
6501 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6502 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6506 /* connect to ipc$ as username/password */
6507 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6508 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6510 /* Is it trusting domain account for sure ? */
6511 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6512 nt_errstr(nt_status)));
6516 /* store who we connected to */
6518 saf_store( domain_name, pdc_name );
6521 * Connect to \\server\ipc$ again (this time anonymously)
6524 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6527 if (NT_STATUS_IS_ERR(nt_status)) {
6528 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6529 domain_name, nt_errstr(nt_status)));
6533 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6534 "domain %s", domain_name))) {
6535 DEBUG(0, ("talloc_init() failed\n"));
6540 /* Make sure we're talking to a proper server */
6542 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6543 if (!NT_STATUS_IS_OK(nt_status)) {
6545 talloc_destroy(mem_ctx);
6550 * Call LsaOpenPolicy and LsaQueryInfo
6553 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6555 if (!NT_STATUS_IS_OK(nt_status)) {
6556 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6558 talloc_destroy(mem_ctx);
6562 b = pipe_hnd->binding_handle;
6564 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6566 if (NT_STATUS_IS_ERR(nt_status)) {
6567 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6568 nt_errstr(nt_status)));
6570 talloc_destroy(mem_ctx);
6574 /* Querying info level 5 */
6576 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6578 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6581 if (NT_STATUS_IS_ERR(nt_status)) {
6582 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6583 nt_errstr(nt_status)));
6585 talloc_destroy(mem_ctx);
6588 if (NT_STATUS_IS_ERR(result)) {
6589 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6590 nt_errstr(result)));
6592 talloc_destroy(mem_ctx);
6596 domain_sid = info->account_domain.sid;
6598 /* There should be actually query info level 3 (following nt serv behaviour),
6599 but I still don't know if it's _really_ necessary */
6602 * Store the password in secrets db
6605 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6606 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6608 talloc_destroy(mem_ctx);
6613 * Close the pipes and clean up
6616 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6617 if (NT_STATUS_IS_ERR(nt_status)) {
6618 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6619 nt_errstr(nt_status)));
6621 talloc_destroy(mem_ctx);
6627 talloc_destroy(mem_ctx);
6629 d_printf(_("Trust to domain %s established\n"), domain_name);
6634 * Revoke trust relationship to the remote domain.
6636 * @param c A net_context structure.
6637 * @param argc Standard argc.
6638 * @param argv Standard argv without initial components.
6640 * @return Integer status (0 means success).
6643 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6649 if (argc < 1 || c->display_usage) {
6652 _("net rpc trustdom revoke <domain_name>\n"
6653 " Revoke trust relationship\n"
6654 " domain_name\tName of domain to revoke trust\n"));
6658 /* generate upper cased domain name */
6659 domain_name = smb_xstrdup(argv[0]);
6660 if (!strupper_m(domain_name)) {
6661 SAFE_FREE(domain_name);
6665 /* delete password of the trust */
6666 if (!pdb_del_trusteddom_pw(domain_name)) {
6667 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6674 SAFE_FREE(domain_name);
6678 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6679 const struct dom_sid *domain_sid,
6680 const char *domain_name,
6681 struct cli_state *cli,
6682 struct rpc_pipe_client *pipe_hnd,
6683 TALLOC_CTX *mem_ctx,
6688 if (!sid_to_fstring(str_sid, domain_sid)) {
6689 return NT_STATUS_UNSUCCESSFUL;
6691 d_printf("%s\n", str_sid);
6692 return NT_STATUS_OK;
6695 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6699 /* convert sid into ascii string */
6700 sid_to_fstring(ascii_sid, dom_sid);
6702 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6705 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6706 TALLOC_CTX *mem_ctx,
6707 struct policy_handle *pol,
6708 struct dom_sid dom_sid,
6709 const char *trusted_dom_name)
6711 NTSTATUS nt_status, result;
6712 union lsa_TrustedDomainInfo *info = NULL;
6713 char *cleartextpwd = NULL;
6714 DATA_BLOB session_key;
6715 DATA_BLOB data = data_blob_null;
6716 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6718 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6721 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6724 if (NT_STATUS_IS_ERR(nt_status)) {
6725 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6726 nt_errstr(nt_status)));
6729 if (NT_STATUS_IS_ERR(result)) {
6731 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6732 nt_errstr(result)));
6736 data = data_blob(info->password.password->data,
6737 info->password.password->length);
6739 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6740 if (!NT_STATUS_IS_OK(nt_status)) {
6741 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6745 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6746 data_blob_free(&session_key);
6748 if (cleartextpwd == NULL) {
6749 DEBUG(0,("retrieved NULL password\n"));
6750 nt_status = NT_STATUS_UNSUCCESSFUL;
6754 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6755 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6756 nt_status = NT_STATUS_UNSUCCESSFUL;
6760 #ifdef DEBUG_PASSWORD
6761 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6762 "password: [%s]\n", trusted_dom_name,
6763 sid_string_dbg(&dom_sid), cleartextpwd));
6767 SAFE_FREE(cleartextpwd);
6768 data_blob_free(&data);
6773 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6776 /* common variables */
6777 TALLOC_CTX* mem_ctx;
6778 struct cli_state *cli = NULL;
6779 struct rpc_pipe_client *pipe_hnd = NULL;
6780 NTSTATUS nt_status, result;
6781 const char *domain_name = NULL;
6782 struct policy_handle connect_hnd;
6783 union lsa_PolicyInformation *info = NULL;
6785 /* trusted domains listing variables */
6786 unsigned int enum_ctx = 0;
6788 struct lsa_DomainList dom_list;
6790 struct dcerpc_binding_handle *b;
6792 if (c->display_usage) {
6794 "net rpc trustdom vampire\n"
6797 _("Vampire trust relationship from remote server"));
6802 * Listing trusted domains (stored in secrets.tdb, if local)
6805 mem_ctx = talloc_init("trust relationships vampire");
6808 * set domain and pdc name to local samba server (default)
6809 * or to remote one given in command line
6812 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6813 domain_name = c->opt_workgroup;
6814 c->opt_target_workgroup = c->opt_workgroup;
6816 fstrcpy(pdc_name, lp_netbios_name());
6817 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6818 c->opt_target_workgroup = domain_name;
6821 /* open \PIPE\lsarpc and open policy handle */
6822 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6823 if (!NT_STATUS_IS_OK(nt_status)) {
6824 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6825 nt_errstr(nt_status)));
6826 talloc_destroy(mem_ctx);
6830 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6832 if (!NT_STATUS_IS_OK(nt_status)) {
6833 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6834 nt_errstr(nt_status) ));
6836 talloc_destroy(mem_ctx);
6840 b = pipe_hnd->binding_handle;
6842 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6844 if (NT_STATUS_IS_ERR(nt_status)) {
6845 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6846 nt_errstr(nt_status)));
6848 talloc_destroy(mem_ctx);
6852 /* query info level 5 to obtain sid of a domain being queried */
6853 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6855 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6859 if (NT_STATUS_IS_ERR(nt_status)) {
6860 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6861 nt_errstr(nt_status)));
6863 talloc_destroy(mem_ctx);
6866 if (NT_STATUS_IS_ERR(result)) {
6867 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6868 nt_errstr(result)));
6870 talloc_destroy(mem_ctx);
6875 * Keep calling LsaEnumTrustdom over opened pipe until
6876 * the end of enumeration is reached
6879 d_printf(_("Vampire trusted domains:\n\n"));
6882 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6888 if (NT_STATUS_IS_ERR(nt_status)) {
6889 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6890 nt_errstr(nt_status)));
6892 talloc_destroy(mem_ctx);
6895 if (NT_STATUS_IS_ERR(result)) {
6897 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6898 nt_errstr(result)));
6900 talloc_destroy(mem_ctx);
6905 for (i = 0; i < dom_list.count; i++) {
6907 print_trusted_domain(dom_list.domains[i].sid,
6908 dom_list.domains[i].name.string);
6910 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6911 *dom_list.domains[i].sid,
6912 dom_list.domains[i].name.string);
6913 if (!NT_STATUS_IS_OK(nt_status)) {
6915 talloc_destroy(mem_ctx);
6921 * in case of no trusted domains say something rather
6922 * than just display blank line
6924 if (!dom_list.count) d_printf(_("none\n"));
6926 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6928 /* close this connection before doing next one */
6929 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6930 if (NT_STATUS_IS_ERR(nt_status)) {
6931 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6932 nt_errstr(nt_status)));
6934 talloc_destroy(mem_ctx);
6938 /* close lsarpc pipe and connection to IPC$ */
6941 talloc_destroy(mem_ctx);
6945 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6947 /* common variables */
6948 TALLOC_CTX* mem_ctx;
6949 struct cli_state *cli = NULL, *remote_cli = NULL;
6950 struct rpc_pipe_client *pipe_hnd = NULL;
6951 NTSTATUS nt_status, result;
6952 const char *domain_name = NULL;
6953 struct dom_sid *queried_dom_sid;
6954 int ascii_dom_name_len;
6955 struct policy_handle connect_hnd;
6956 union lsa_PolicyInformation *info = NULL;
6957 struct dcerpc_binding_handle *b = NULL;
6959 /* trusted domains listing variables */
6960 unsigned int num_domains, enum_ctx = 0;
6962 struct lsa_DomainList dom_list;
6966 /* trusting domains listing variables */
6967 struct policy_handle domain_hnd;
6968 struct samr_SamArray *trusts = NULL;
6970 if (c->display_usage) {
6972 "net rpc trustdom list\n"
6975 _("List incoming and outgoing trust relationships"));
6980 * Listing trusted domains (stored in secrets.tdb, if local)
6983 mem_ctx = talloc_init("trust relationships listing");
6986 * set domain and pdc name to local samba server (default)
6987 * or to remote one given in command line
6990 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6991 domain_name = c->opt_workgroup;
6992 c->opt_target_workgroup = c->opt_workgroup;
6994 fstrcpy(pdc_name, lp_netbios_name());
6995 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6996 c->opt_target_workgroup = domain_name;
6999 /* open \PIPE\lsarpc and open policy handle */
7000 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
7001 if (!NT_STATUS_IS_OK(nt_status)) {
7002 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
7003 nt_errstr(nt_status)));
7004 talloc_destroy(mem_ctx);
7008 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
7010 if (!NT_STATUS_IS_OK(nt_status)) {
7011 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
7012 nt_errstr(nt_status) ));
7014 talloc_destroy(mem_ctx);
7018 b = pipe_hnd->binding_handle;
7020 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
7022 if (NT_STATUS_IS_ERR(nt_status)) {
7023 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
7024 nt_errstr(nt_status)));
7026 talloc_destroy(mem_ctx);
7030 /* query info level 5 to obtain sid of a domain being queried */
7031 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
7033 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
7037 if (NT_STATUS_IS_ERR(nt_status)) {
7038 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7039 nt_errstr(nt_status)));
7041 talloc_destroy(mem_ctx);
7044 if (NT_STATUS_IS_ERR(result)) {
7045 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7046 nt_errstr(result)));
7048 talloc_destroy(mem_ctx);
7052 queried_dom_sid = info->account_domain.sid;
7055 * Keep calling LsaEnumTrustdom over opened pipe until
7056 * the end of enumeration is reached
7059 d_printf(_("Trusted domains list:\n\n"));
7061 found_domain = false;
7064 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
7070 if (NT_STATUS_IS_ERR(nt_status)) {
7071 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7072 nt_errstr(nt_status)));
7074 talloc_destroy(mem_ctx);
7077 if (NT_STATUS_IS_ERR(result)) {
7078 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7079 nt_errstr(result)));
7081 talloc_destroy(mem_ctx);
7086 for (i = 0; i < dom_list.count; i++) {
7087 print_trusted_domain(dom_list.domains[i].sid,
7088 dom_list.domains[i].name.string);
7089 found_domain = true;
7093 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7096 * in case of no trusted domains say something rather
7097 * than just display blank line
7099 if (!found_domain) {
7100 d_printf(_("none\n"));
7103 /* close this connection before doing next one */
7104 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7105 if (NT_STATUS_IS_ERR(nt_status)) {
7106 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7107 nt_errstr(nt_status)));
7109 talloc_destroy(mem_ctx);
7113 TALLOC_FREE(pipe_hnd);
7116 * Listing trusting domains (stored in passdb backend, if local)
7119 d_printf(_("\nTrusting domains list:\n\n"));
7122 * Open \PIPE\samr and get needed policy handles
7124 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
7126 if (!NT_STATUS_IS_OK(nt_status)) {
7127 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
7129 talloc_destroy(mem_ctx);
7133 b = pipe_hnd->binding_handle;
7136 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
7138 SAMR_ACCESS_LOOKUP_DOMAIN,
7141 if (!NT_STATUS_IS_OK(nt_status)) {
7142 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7143 nt_errstr(nt_status)));
7145 talloc_destroy(mem_ctx);
7148 if (!NT_STATUS_IS_OK(result)) {
7150 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7151 nt_errstr(result)));
7153 talloc_destroy(mem_ctx);
7157 /* SamrOpenDomain - we have to open domain policy handle in order to be
7158 able to enumerate accounts*/
7159 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
7161 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
7165 if (!NT_STATUS_IS_OK(nt_status)) {
7166 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7167 nt_errstr(nt_status)));
7169 talloc_destroy(mem_ctx);
7172 if (!NT_STATUS_IS_OK(result)) {
7174 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7175 nt_errstr(result)));
7177 talloc_destroy(mem_ctx);
7182 * perform actual enumeration
7185 found_domain = false;
7187 enum_ctx = 0; /* reset enumeration context from last enumeration */
7190 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
7198 if (NT_STATUS_IS_ERR(nt_status)) {
7199 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7200 nt_errstr(nt_status)));
7202 talloc_destroy(mem_ctx);
7205 if (NT_STATUS_IS_ERR(result)) {
7207 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7208 nt_errstr(result)));
7210 talloc_destroy(mem_ctx);
7214 for (i = 0; i < num_domains; i++) {
7216 char *str = discard_const_p(char, trusts->entries[i].name.string);
7218 found_domain = true;
7221 * get each single domain's sid (do we _really_ need this ?):
7222 * 1) connect to domain's pdc
7223 * 2) query the pdc for domain's sid
7226 /* get rid of '$' tail */
7227 ascii_dom_name_len = strlen(str);
7228 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7229 str[ascii_dom_name_len - 1] = '\0';
7231 /* set opt_* variables to remote domain */
7232 if (!strupper_m(str)) {
7234 talloc_destroy(mem_ctx);
7237 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7238 c->opt_target_workgroup = c->opt_workgroup;
7240 d_printf("%-20s", str);
7242 /* connect to remote domain controller */
7243 nt_status = net_make_ipc_connection(c,
7244 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7246 if (NT_STATUS_IS_OK(nt_status)) {
7247 /* query for domain's sid */
7248 if (run_rpc_command(
7250 &ndr_table_lsarpc, 0,
7251 rpc_query_domain_sid, argc,
7253 d_printf(_("strange - couldn't get domain's sid\n"));
7255 cli_shutdown(remote_cli);
7258 d_fprintf(stderr, _("domain controller is not "
7259 "responding: %s\n"),
7260 nt_errstr(nt_status));
7261 d_printf(_("couldn't get domain's sid\n"));
7265 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7267 if (!found_domain) {
7271 /* close opened samr and domain policy handles */
7272 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7273 if (!NT_STATUS_IS_OK(nt_status)) {
7274 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7277 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7278 if (!NT_STATUS_IS_OK(nt_status)) {
7279 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7282 /* close samr pipe and connection to IPC$ */
7285 talloc_destroy(mem_ctx);
7290 * Entrypoint for 'net rpc trustdom' code.
7292 * @param argc Standard argc.
7293 * @param argv Standard argv without initial components.
7295 * @return Integer status (0 means success).
7298 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7300 struct functable func[] = {
7305 N_("Add trusting domain's account"),
7306 N_("net rpc trustdom add\n"
7307 " Add trusting domain's account")
7313 N_("Remove trusting domain's account"),
7314 N_("net rpc trustdom del\n"
7315 " Remove trusting domain's account")
7319 rpc_trustdom_establish,
7321 N_("Establish outgoing trust relationship"),
7322 N_("net rpc trustdom establish\n"
7323 " Establish outgoing trust relationship")
7327 rpc_trustdom_revoke,
7329 N_("Revoke outgoing trust relationship"),
7330 N_("net rpc trustdom revoke\n"
7331 " Revoke outgoing trust relationship")
7337 N_("List in- and outgoing domain trusts"),
7338 N_("net rpc trustdom list\n"
7339 " List in- and outgoing domain trusts")
7343 rpc_trustdom_vampire,
7345 N_("Vampire trusts from remote server"),
7346 N_("net rpc trustdom vampire\n"
7347 " Vampire trusts from remote server")
7349 {NULL, NULL, 0, NULL, NULL}
7352 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7356 * Check if a server will take rpc commands
7357 * @param flags Type of server to connect to (PDC, DMB, localhost)
7358 * if the host is not explicitly specified
7359 * @return bool (true means rpc supported)
7361 bool net_rpc_check(struct net_context *c, unsigned flags)
7363 struct cli_state *cli;
7365 struct sockaddr_storage server_ss;
7366 char *server_name = NULL;
7369 /* flags (i.e. server type) may depend on command */
7370 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7373 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7374 lp_netbios_name(), SMB_SIGNING_DEFAULT,
7376 if (!NT_STATUS_IS_OK(status)) {
7379 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7381 if (!NT_STATUS_IS_OK(status))
7383 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7392 /* dump sam database via samsync rpc calls */
7393 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7394 if (c->display_usage) {
7399 _("Dump remote SAM database"));
7403 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7404 NET_FLAGS_ANONYMOUS,
7405 rpc_samdump_internals, argc, argv);
7408 /* syncronise sam database via samsync rpc calls */
7409 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7411 struct functable func[] = {
7416 N_("Dump remote SAM database to ldif"),
7417 N_("net rpc vampire ldif\n"
7418 " Dump remote SAM database to LDIF file or "
7425 N_("Dump remote SAM database to Kerberos Keytab"),
7426 N_("net rpc vampire keytab\n"
7427 " Dump remote SAM database to Kerberos keytab "
7434 N_("Dump remote SAM database to passdb"),
7435 N_("net rpc vampire passdb\n"
7436 " Dump remote SAM database to passdb")
7439 {NULL, NULL, 0, NULL, NULL}
7443 if (c->display_usage) {
7448 _("Vampire remote SAM database"));
7452 return rpc_vampire_passdb(c, argc, argv);
7455 return net_run_function(c, argc, argv, "net rpc vampire", func);
7459 * Migrate everything from a print server.
7461 * @param c A net_context structure.
7462 * @param argc Standard main() style argc.
7463 * @param argv Standard main() style argv. Initial components are already
7466 * @return A shell status integer (0 for success).
7468 * The order is important !
7469 * To successfully add drivers the print queues have to exist !
7470 * Applying ACLs should be the last step, because you're easily locked out.
7473 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7478 if (c->display_usage) {
7480 "net rpc printer migrate all\n"
7483 _("Migrate everything from a print server"));
7488 d_printf(_("no server to migrate\n"));
7492 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7493 rpc_printer_migrate_printers_internals, argc,
7498 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7499 rpc_printer_migrate_drivers_internals, argc,
7504 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7505 rpc_printer_migrate_forms_internals, argc, argv);
7509 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7510 rpc_printer_migrate_settings_internals, argc,
7515 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7516 rpc_printer_migrate_security_internals, argc,
7522 * Migrate print drivers from a print server.
7524 * @param c A net_context structure.
7525 * @param argc Standard main() style argc.
7526 * @param argv Standard main() style argv. Initial components are already
7529 * @return A shell status integer (0 for success).
7531 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7534 if (c->display_usage) {
7536 "net rpc printer migrate drivers\n"
7539 _("Migrate print-drivers from a print-server"));
7544 d_printf(_("no server to migrate\n"));
7548 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7549 rpc_printer_migrate_drivers_internals,
7554 * Migrate print-forms from a print-server.
7556 * @param c A net_context structure.
7557 * @param argc Standard main() style argc.
7558 * @param argv Standard main() style argv. Initial components are already
7561 * @return A shell status integer (0 for success).
7563 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7566 if (c->display_usage) {
7568 "net rpc printer migrate forms\n"
7571 _("Migrate print-forms from a print-server"));
7576 d_printf(_("no server to migrate\n"));
7580 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7581 rpc_printer_migrate_forms_internals,
7586 * Migrate printers from a print-server.
7588 * @param c A net_context structure.
7589 * @param argc Standard main() style argc.
7590 * @param argv Standard main() style argv. Initial components are already
7593 * @return A shell status integer (0 for success).
7595 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7598 if (c->display_usage) {
7600 "net rpc printer migrate printers\n"
7603 _("Migrate printers from a print-server"));
7608 d_printf(_("no server to migrate\n"));
7612 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7613 rpc_printer_migrate_printers_internals,
7618 * Migrate printer-ACLs from a print-server
7620 * @param c A net_context structure.
7621 * @param argc Standard main() style argc.
7622 * @param argv Standard main() style argv. Initial components are already
7625 * @return A shell status integer (0 for success).
7627 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7630 if (c->display_usage) {
7632 "net rpc printer migrate security\n"
7635 _("Migrate printer-ACLs from a print-server"));
7640 d_printf(_("no server to migrate\n"));
7644 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7645 rpc_printer_migrate_security_internals,
7650 * Migrate printer-settings from a print-server.
7652 * @param c A net_context structure.
7653 * @param argc Standard main() style argc.
7654 * @param argv Standard main() style argv. Initial components are already
7657 * @return A shell status integer (0 for success).
7659 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7662 if (c->display_usage) {
7664 "net rpc printer migrate settings\n"
7667 _("Migrate printer-settings from a "
7673 d_printf(_("no server to migrate\n"));
7677 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7678 rpc_printer_migrate_settings_internals,
7683 * 'net rpc printer' entrypoint.
7685 * @param c A net_context structure.
7686 * @param argc Standard main() style argc.
7687 * @param argv Standard main() style argv. Initial components are already
7691 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7694 /* ouch: when addriver and setdriver are called from within
7695 rpc_printer_migrate_drivers_internals, the printer-queue already
7698 struct functable func[] = {
7701 rpc_printer_migrate_all,
7703 N_("Migrate all from remote to local print server"),
7704 N_("net rpc printer migrate all\n"
7705 " Migrate all from remote to local print server")
7709 rpc_printer_migrate_drivers,
7711 N_("Migrate drivers to local server"),
7712 N_("net rpc printer migrate drivers\n"
7713 " Migrate drivers to local server")
7717 rpc_printer_migrate_forms,
7719 N_("Migrate froms to local server"),
7720 N_("net rpc printer migrate forms\n"
7721 " Migrate froms to local server")
7725 rpc_printer_migrate_printers,
7727 N_("Migrate printers to local server"),
7728 N_("net rpc printer migrate printers\n"
7729 " Migrate printers to local server")
7733 rpc_printer_migrate_security,
7735 N_("Mirgate printer ACLs to local server"),
7736 N_("net rpc printer migrate security\n"
7737 " Mirgate printer ACLs to local server")
7741 rpc_printer_migrate_settings,
7743 N_("Migrate printer settings to local server"),
7744 N_("net rpc printer migrate settings\n"
7745 " Migrate printer settings to local server")
7747 {NULL, NULL, 0, NULL, NULL}
7750 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7755 * List printers on a remote RPC server.
7757 * @param c A net_context structure.
7758 * @param argc Standard main() style argc.
7759 * @param argv Standard main() style argv. Initial components are already
7762 * @return A shell status integer (0 for success).
7764 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7766 if (c->display_usage) {
7768 "net rpc printer list\n"
7771 _("List printers on a remote RPC server"));
7775 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7776 rpc_printer_list_internals,
7781 * List printer-drivers on a remote RPC server.
7783 * @param c A net_context structure.
7784 * @param argc Standard main() style argc.
7785 * @param argv Standard main() style argv. Initial components are already
7788 * @return A shell status integer (0 for success).
7790 static int rpc_printer_driver_list(struct net_context *c, int argc,
7793 if (c->display_usage) {
7795 "net rpc printer driver\n"
7798 _("List printer-drivers on a remote RPC server"));
7802 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7803 rpc_printer_driver_list_internals,
7808 * Publish printer in ADS via MSRPC.
7810 * @param c A net_context structure.
7811 * @param argc Standard main() style argc.
7812 * @param argv Standard main() style argv. Initial components are already
7815 * @return A shell status integer (0 for success).
7817 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7820 if (c->display_usage) {
7822 "net rpc printer publish publish\n"
7825 _("Publish printer in ADS via MSRPC"));
7829 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7830 rpc_printer_publish_publish_internals,
7835 * Update printer in ADS via MSRPC.
7837 * @param c A net_context structure.
7838 * @param argc Standard main() style argc.
7839 * @param argv Standard main() style argv. Initial components are already
7842 * @return A shell status integer (0 for success).
7844 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7846 if (c->display_usage) {
7848 "net rpc printer publish update\n"
7851 _("Update printer in ADS via MSRPC"));
7855 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7856 rpc_printer_publish_update_internals,
7861 * UnPublish printer in ADS via MSRPC.
7863 * @param c A net_context structure.
7864 * @param argc Standard main() style argc.
7865 * @param argv Standard main() style argv. Initial components are already
7868 * @return A shell status integer (0 for success).
7870 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7873 if (c->display_usage) {
7875 "net rpc printer publish unpublish\n"
7878 _("UnPublish printer in ADS via MSRPC"));
7882 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7883 rpc_printer_publish_unpublish_internals,
7888 * List published printers via MSRPC.
7890 * @param c A net_context structure.
7891 * @param argc Standard main() style argc.
7892 * @param argv Standard main() style argv. Initial components are already
7895 * @return A shell status integer (0 for success).
7897 static int rpc_printer_publish_list(struct net_context *c, int argc,
7900 if (c->display_usage) {
7902 "net rpc printer publish list\n"
7905 _("List published printers via MSRPC"));
7909 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7910 rpc_printer_publish_list_internals,
7916 * Publish printer in ADS.
7918 * @param c A net_context structure.
7919 * @param argc Standard main() style argc.
7920 * @param argv Standard main() style argv. Initial components are already
7923 * @return A shell status integer (0 for success).
7925 static int rpc_printer_publish(struct net_context *c, int argc,
7929 struct functable func[] = {
7932 rpc_printer_publish_publish,
7934 N_("Publish printer in AD"),
7935 N_("net rpc printer publish publish\n"
7936 " Publish printer in AD")
7940 rpc_printer_publish_update,
7942 N_("Update printer in AD"),
7943 N_("net rpc printer publish update\n"
7944 " Update printer in AD")
7948 rpc_printer_publish_unpublish,
7950 N_("Unpublish printer"),
7951 N_("net rpc printer publish unpublish\n"
7952 " Unpublish printer")
7956 rpc_printer_publish_list,
7958 N_("List published printers"),
7959 N_("net rpc printer publish list\n"
7960 " List published printers")
7962 {NULL, NULL, 0, NULL, NULL}
7966 if (c->display_usage) {
7967 d_printf(_("Usage:\n"));
7968 d_printf(_("net rpc printer publish\n"
7969 " List published printers\n"
7970 " Alias of net rpc printer publish "
7972 net_display_usage_from_functable(func);
7975 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7976 rpc_printer_publish_list_internals,
7980 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7986 * Display rpc printer help page.
7988 * @param c A net_context structure.
7989 * @param argc Standard main() style argc.
7990 * @param argv Standard main() style argv. Initial components are already
7993 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7995 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7996 "\tlists all printers on print-server\n\n"));
7997 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7998 "\tlists all printer-drivers on print-server\n\n"));
7999 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
8000 "\tpublishes printer settings in Active Directory\n"
8001 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
8002 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
8003 "\n\tmigrates printers from remote to local server\n\n"));
8004 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
8005 "\n\tmigrates printer-settings from remote to local server\n\n"));
8006 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
8007 "\n\tmigrates printer-drivers from remote to local server\n\n"));
8008 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
8009 "\n\tmigrates printer-forms from remote to local server\n\n"));
8010 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
8011 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
8012 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
8013 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
8014 "\tremote to local print-server\n\n"));
8015 net_common_methods_usage(c, argc, argv);
8016 net_common_flags_usage(c, argc, argv);
8018 "\t-v or --verbose\t\t\tgive verbose output\n"
8019 "\t --destination\t\tmigration target server (default: localhost)\n"));
8025 * 'net rpc printer' entrypoint.
8027 * @param c A net_context structure.
8028 * @param argc Standard main() style argc.
8029 * @param argv Standard main() style argv. Initial components are already
8032 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
8034 struct functable func[] = {
8039 N_("List all printers on print server"),
8040 N_("net rpc printer list\n"
8041 " List all printers on print server")
8045 rpc_printer_migrate,
8047 N_("Migrate printer to local server"),
8048 N_("net rpc printer migrate\n"
8049 " Migrate printer to local server")
8053 rpc_printer_driver_list,
8055 N_("List printer drivers"),
8056 N_("net rpc printer driver\n"
8057 " List printer drivers")
8061 rpc_printer_publish,
8063 N_("Publish printer in AD"),
8064 N_("net rpc printer publish\n"
8065 " Publish printer in AD")
8067 {NULL, NULL, 0, NULL, NULL}
8071 if (c->display_usage) {
8072 d_printf(_("Usage:\n"));
8073 d_printf(_("net rpc printer\n"
8074 " List printers\n"));
8075 net_display_usage_from_functable(func);
8078 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8079 rpc_printer_list_internals,
8083 return net_run_function(c, argc, argv, "net rpc printer", func);
8087 * 'net rpc' entrypoint.
8089 * @param c A net_context structure.
8090 * @param argc Standard main() style argc.
8091 * @param argv Standard main() style argv. Initial components are already
8095 int net_rpc(struct net_context *c, int argc, const char **argv)
8097 NET_API_STATUS status;
8099 struct functable func[] = {
8104 N_("Modify global audit settings"),
8105 N_("net rpc audit\n"
8106 " Modify global audit settings")
8112 N_("Show basic info about a domain"),
8114 " Show basic info about a domain")
8120 N_("Join a domain"),
8128 N_("Join a domain created in server manager"),
8129 N_("net rpc oldjoin\n"
8130 " Join a domain created in server manager")
8136 N_("Test that a join is valid"),
8137 N_("net rpc testjoin\n"
8138 " Test that a join is valid")
8144 N_("List/modify users"),
8146 " List/modify users")
8152 N_("Change a user password"),
8153 N_("net rpc password\n"
8154 " Change a user password\n"
8155 " Alias for net rpc user password")
8161 N_("List/modify groups"),
8162 N_("net rpc group\n"
8163 " List/modify groups")
8169 N_("List/modify shares"),
8170 N_("net rpc share\n"
8171 " List/modify shares")
8177 N_("List open files"),
8185 N_("List/modify printers"),
8186 N_("net rpc printer\n"
8187 " List/modify printers")
8191 net_rpc_changetrustpw,
8193 N_("Change trust account password"),
8194 N_("net rpc changetrustpw\n"
8195 " Change trust account password")
8201 N_("Modify domain trusts"),
8202 N_("net rpc trustdom\n"
8203 " Modify domain trusts")
8209 N_("Abort a remote shutdown"),
8210 N_("net rpc abortshutdown\n"
8211 " Abort a remote shutdown")
8217 N_("Shutdown a remote server"),
8218 N_("net rpc shutdown\n"
8219 " Shutdown a remote server")
8225 N_("Dump SAM data of remote NT PDC"),
8226 N_("net rpc samdump\n"
8227 " Dump SAM data of remote NT PDC")
8233 N_("Sync a remote NT PDC's data into local passdb"),
8234 N_("net rpc vampire\n"
8235 " Sync a remote NT PDC's data into local passdb")
8241 N_("Fetch the domain sid into local secrets.tdb"),
8242 N_("net rpc getsid\n"
8243 " Fetch the domain sid into local secrets.tdb")
8249 N_("Manage privileges assigned to SID"),
8250 N_("net rpc rights\n"
8251 " Manage privileges assigned to SID")
8257 N_("Start/stop/query remote services"),
8258 N_("net rpc service\n"
8259 " Start/stop/query remote services")
8265 N_("Manage registry hives"),
8266 N_("net rpc registry\n"
8267 " Manage registry hives")
8273 N_("Open interactive shell on remote server"),
8274 N_("net rpc shell\n"
8275 " Open interactive shell on remote server")
8281 N_("Manage trusts"),
8282 N_("net rpc trust\n"
8289 N_("Configure a remote samba server"),
8291 " Configure a remote samba server")
8293 {NULL, NULL, 0, NULL, NULL}
8296 status = libnetapi_net_init(&c->netapi_ctx);
8300 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8301 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8302 if (c->opt_kerberos) {
8303 libnetapi_set_use_kerberos(c->netapi_ctx);
8305 if (c->opt_ccache) {
8306 libnetapi_set_use_ccache(c->netapi_ctx);
8309 return net_run_function(c, argc, argv, "net rpc", func);