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 TALLOC_FREE(c->netlogon_creds);
196 nt_status = cli_rpc_pipe_open_schannel(
197 cli, c->msg_ctx, table, NCACN_NP,
199 &pipe_hnd, c, &c->netlogon_creds);
200 if (!NT_STATUS_IS_OK(nt_status)) {
201 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
202 nt_errstr(nt_status) ));
206 if (conn_flags & NET_FLAGS_SEAL) {
207 nt_status = cli_rpc_pipe_open_generic_auth(
209 (conn_flags & NET_FLAGS_TCP) ?
210 NCACN_IP_TCP : NCACN_NP,
211 CRED_DONT_USE_KERBEROS,
212 DCERPC_AUTH_TYPE_NTLMSSP,
213 DCERPC_AUTH_LEVEL_PRIVACY,
214 smbXcli_conn_remote_name(cli->conn),
215 lp_workgroup(), c->opt_user_name,
216 c->opt_password, &pipe_hnd);
218 nt_status = cli_rpc_pipe_open_noauth(
222 if (!NT_STATUS_IS_OK(nt_status)) {
223 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
225 nt_errstr(nt_status) ));
231 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
233 if (!NT_STATUS_IS_OK(nt_status)) {
234 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
237 DEBUG(5, ("rpc command function succedded\n"));
240 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
242 TALLOC_FREE(pipe_hnd);
247 /* close the connection only if it was opened here */
252 talloc_destroy(mem_ctx);
257 * Force a change of the trust acccount password.
259 * All parameters are provided by the run_rpc_command function, except for
260 * argc, argv which are passed through.
262 * @param domain_sid The domain sid acquired from the remote server.
263 * @param cli A cli_state connected to the server.
264 * @param mem_ctx Talloc context, destroyed on completion of the function.
265 * @param argc Standard main() style argc.
266 * @param argv Standard main() style argv. Initial components are already
269 * @return Normal NTSTATUS return.
272 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
273 const struct dom_sid *domain_sid,
274 const char *domain_name,
275 struct cli_state *cli,
276 struct rpc_pipe_client *pipe_hnd,
283 status = trust_pw_change(c->netlogon_creds,
285 pipe_hnd->binding_handle,
286 c->opt_target_workgroup,
288 if (!NT_STATUS_IS_OK(status)) {
289 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
298 * Force a change of the trust acccount password.
300 * @param argc Standard main() style argc.
301 * @param argv Standard main() style argv. Initial components are already
304 * @return A shell status integer (0 for success).
307 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
309 if (c->display_usage) {
311 "net rpc changetrustpw\n"
314 _("Change the machine trust password"));
318 return run_rpc_command(c, NULL, &ndr_table_netlogon,
319 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
320 rpc_changetrustpw_internals,
325 * Join a domain, the old way. This function exists to allow
326 * the message to be displayed when oldjoin was explicitly
327 * requested, but not when it was implied by "net rpc join".
329 * This uses 'machinename' as the inital password, and changes it.
331 * The password should be created with 'server manager' or equiv first.
333 * @param argc Standard main() style argc.
334 * @param argv Standard main() style argv. Initial components are already
337 * @return A shell status integer (0 for success).
340 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
342 struct libnet_JoinCtx *r = NULL;
345 const char *domain = lp_workgroup(); /* FIXME */
346 bool modify_config = lp_config_backend_is_registry();
347 enum netr_SchannelType sec_chan_type;
350 if (c->display_usage) {
353 " Join a domain the old way\n");
357 mem_ctx = talloc_init("net_rpc_oldjoin");
362 werr = libnet_init_JoinCtx(mem_ctx, &r);
363 if (!W_ERROR_IS_OK(werr)) {
368 check what type of join - if the user want's to join as
369 a BDC, the server must agree that we are a BDC.
372 sec_chan_type = get_sec_channel_type(argv[0]);
374 sec_chan_type = get_sec_channel_type(NULL);
378 d_fprintf(stderr, _("Could not initialise message context. "
379 "Try running as root\n"));
380 werr = WERR_ACCESS_DENIED;
384 pw = talloc_strndup(r, lp_netbios_name(), 14);
386 werr = WERR_NOT_ENOUGH_MEMORY;
390 r->in.msg_ctx = c->msg_ctx;
391 r->in.domain_name = domain;
392 r->in.secure_channel_type = sec_chan_type;
393 r->in.dc_name = c->opt_host;
394 r->in.admin_account = "";
395 r->in.admin_password = strlower_talloc(r, pw);
396 if (r->in.admin_password == NULL) {
397 werr = WERR_NOT_ENOUGH_MEMORY;
401 r->in.modify_config = modify_config;
402 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
403 WKSSVC_JOIN_FLAGS_JOIN_UNSECURE |
404 WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED;
406 werr = libnet_Join(mem_ctx, r);
407 if (!W_ERROR_IS_OK(werr)) {
411 /* Check the short name of the domain */
413 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
414 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
415 d_printf("domain name obtained from the server.\n");
416 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
417 d_printf("You should set \"workgroup = %s\" in %s.\n",
418 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
421 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
423 if (r->out.dns_domain_name) {
424 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
425 r->out.dns_domain_name);
427 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
428 r->out.netbios_domain_name);
431 /* print out informative error string in case there is one */
432 if (r->out.error_string != NULL) {
433 d_printf("%s\n", r->out.error_string);
436 TALLOC_FREE(mem_ctx);
441 if (c->opt_flags & NET_FLAGS_EXPECT_FALLBACK) {
445 /* issue an overall failure message at the end. */
446 d_fprintf(stderr, _("Failed to join domain: %s\n"),
447 r && r->out.error_string ? r->out.error_string :
448 get_friendly_werror_msg(werr));
451 TALLOC_FREE(mem_ctx);
457 * check that a join is OK
459 * @return A shell status integer (0 for success)
462 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
466 const char *domain = c->opt_target_workgroup;
467 const char *dc = c->opt_host;
469 if (c->display_usage) {
472 " Test if a join is OK\n");
476 mem_ctx = talloc_init("net_rpc_testjoin");
482 struct netr_DsRGetDCNameInfo *info;
485 d_fprintf(stderr, _("Could not initialise message context. "
486 "Try running as root\n"));
487 talloc_destroy(mem_ctx);
491 status = dsgetdcname(mem_ctx,
498 if (!NT_STATUS_IS_OK(status)) {
499 talloc_destroy(mem_ctx);
503 dc = strip_hostname(info->dc_unc);
506 /* Display success or failure */
507 status = libnet_join_ok(c->msg_ctx,
511 if (!NT_STATUS_IS_OK(status)) {
512 fprintf(stderr,"Join to domain '%s' is not valid: %s\n",
513 domain, nt_errstr(status));
514 talloc_destroy(mem_ctx);
518 printf("Join to '%s' is OK\n",domain);
519 talloc_destroy(mem_ctx);
525 * Join a domain using the administrator username and password
527 * @param argc Standard main() style argc
528 * @param argc Standard main() style argv. Initial components are already
529 * stripped. Currently not used.
530 * @return A shell status integer (0 for success)
534 static int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
536 struct libnet_JoinCtx *r = NULL;
539 const char *domain = lp_workgroup(); /* FIXME */
540 bool modify_config = lp_config_backend_is_registry();
541 enum netr_SchannelType sec_chan_type;
543 if (c->display_usage) {
546 " Join a domain the new way\n");
550 mem_ctx = talloc_init("net_rpc_join_newstyle");
555 werr = libnet_init_JoinCtx(mem_ctx, &r);
556 if (!W_ERROR_IS_OK(werr)) {
561 check what type of join - if the user want's to join as
562 a BDC, the server must agree that we are a BDC.
565 sec_chan_type = get_sec_channel_type(argv[0]);
567 sec_chan_type = get_sec_channel_type(NULL);
571 d_fprintf(stderr, _("Could not initialise message context. "
572 "Try running as root\n"));
573 werr = WERR_ACCESS_DENIED;
577 r->in.msg_ctx = c->msg_ctx;
578 r->in.domain_name = domain;
579 r->in.secure_channel_type = sec_chan_type;
580 r->in.dc_name = c->opt_host;
581 r->in.admin_account = c->opt_user_name;
582 r->in.admin_password = net_prompt_pass(c, c->opt_user_name);
584 r->in.use_kerberos = c->opt_kerberos;
585 r->in.modify_config = modify_config;
586 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
587 WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
588 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
590 werr = libnet_Join(mem_ctx, r);
591 if (!W_ERROR_IS_OK(werr)) {
595 /* Check the short name of the domain */
597 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
598 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
599 d_printf("domain name obtained from the server.\n");
600 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
601 d_printf("You should set \"workgroup = %s\" in %s.\n",
602 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
605 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
607 if (r->out.dns_domain_name) {
608 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
609 r->out.dns_domain_name);
611 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
612 r->out.netbios_domain_name);
615 /* print out informative error string in case there is one */
616 if (r->out.error_string != NULL) {
617 d_printf("%s\n", r->out.error_string);
620 TALLOC_FREE(mem_ctx);
625 /* issue an overall failure message at the end. */
626 d_printf("Failed to join domain: %s\n",
627 r && r->out.error_string ? r->out.error_string :
628 get_friendly_werror_msg(werr));
630 TALLOC_FREE(mem_ctx);
636 * 'net rpc join' entrypoint.
637 * @param argc Standard main() style argc.
638 * @param argv Standard main() style argv. Initial components are already
641 * Main 'net_rpc_join()' (where the admin username/password is used) is
643 * Try to just change the password, but if that doesn't work, use/prompt
644 * for a username/password.
647 int net_rpc_join(struct net_context *c, int argc, const char **argv)
651 if (c->display_usage) {
654 _("net rpc join -U <username>[%%password] <type>\n"
656 " username\tName of the admin user"
657 " password\tPassword of the admin user, will "
658 "prompt if not specified\n"
659 " type\tCan be one of the following:\n"
660 "\t\tMEMBER\tJoin as member server (default)\n"
661 "\t\tBDC\tJoin as BDC\n"
662 "\t\tPDC\tJoin as PDC\n"));
666 if (lp_server_role() == ROLE_STANDALONE) {
667 d_printf(_("cannot join as standalone machine\n"));
671 if (strlen(lp_netbios_name()) > 15) {
672 d_printf(_("Our netbios name can be at most 15 chars long, "
673 "\"%s\" is %u chars long\n"),
674 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
678 c->opt_flags |= NET_FLAGS_EXPECT_FALLBACK;
679 ret = net_rpc_oldjoin(c, argc, argv);
680 c->opt_flags &= ~NET_FLAGS_EXPECT_FALLBACK;
685 return net_rpc_join_newstyle(c, argc, argv);
689 * display info about a rpc domain
691 * All parameters are provided by the run_rpc_command function, except for
692 * argc, argv which are passed through.
694 * @param domain_sid The domain sid acquired from the remote server
695 * @param cli A cli_state connected to the server.
696 * @param mem_ctx Talloc context, destroyed on completion of the function.
697 * @param argc Standard main() style argc.
698 * @param argv Standard main() style argv. Initial components are already
701 * @return Normal NTSTATUS return.
704 NTSTATUS rpc_info_internals(struct net_context *c,
705 const struct dom_sid *domain_sid,
706 const char *domain_name,
707 struct cli_state *cli,
708 struct rpc_pipe_client *pipe_hnd,
713 struct policy_handle connect_pol, domain_pol;
714 NTSTATUS status, result;
715 union samr_DomainInfo *info = NULL;
717 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
719 sid_to_fstring(sid_str, domain_sid);
721 /* Get sam policy handle */
722 status = dcerpc_samr_Connect2(b, mem_ctx,
724 MAXIMUM_ALLOWED_ACCESS,
727 if (!NT_STATUS_IS_OK(status)) {
728 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
733 if (!NT_STATUS_IS_OK(result)) {
735 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
740 /* Get domain policy handle */
741 status = dcerpc_samr_OpenDomain(b, mem_ctx,
743 MAXIMUM_ALLOWED_ACCESS,
744 discard_const_p(struct dom_sid2, domain_sid),
747 if (!NT_STATUS_IS_OK(status)) {
748 d_fprintf(stderr, _("Could not open domain: %s\n"),
752 if (!NT_STATUS_IS_OK(result)) {
754 d_fprintf(stderr, _("Could not open domain: %s\n"),
759 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
764 if (!NT_STATUS_IS_OK(status)) {
768 if (NT_STATUS_IS_OK(result)) {
769 d_printf(_("Domain Name: %s\n"),
770 info->general.domain_name.string);
771 d_printf(_("Domain SID: %s\n"), sid_str);
772 d_printf(_("Sequence number: %llu\n"),
773 (unsigned long long)info->general.sequence_num);
774 d_printf(_("Num users: %u\n"), info->general.num_users);
775 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
776 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
784 * 'net rpc info' entrypoint.
785 * @param argc Standard main() style argc.
786 * @param argv Standard main() style argv. Initial components are already
790 int net_rpc_info(struct net_context *c, int argc, const char **argv)
792 if (c->display_usage) {
797 _("Display information about the domain"));
801 return run_rpc_command(c, NULL, &ndr_table_samr,
802 NET_FLAGS_PDC, rpc_info_internals,
807 * Fetch domain SID into the local secrets.tdb.
809 * All parameters are provided by the run_rpc_command function, except for
810 * argc, argv which are passed through.
812 * @param domain_sid The domain sid acquired from the remote server.
813 * @param cli A cli_state connected to the server.
814 * @param mem_ctx Talloc context, destroyed on completion of the function.
815 * @param argc Standard main() style argc.
816 * @param argv Standard main() style argv. Initial components are already
819 * @return Normal NTSTATUS return.
822 static NTSTATUS rpc_getsid_internals(struct net_context *c,
823 const struct dom_sid *domain_sid,
824 const char *domain_name,
825 struct cli_state *cli,
826 struct rpc_pipe_client *pipe_hnd,
833 sid_to_fstring(sid_str, domain_sid);
834 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
835 sid_str, domain_name);
837 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
838 DEBUG(0,("Can't store domain SID\n"));
839 return NT_STATUS_UNSUCCESSFUL;
846 * 'net rpc getsid' entrypoint.
847 * @param argc Standard main() style argc.
848 * @param argv Standard main() style argv. Initial components are already
852 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
854 int conn_flags = NET_FLAGS_PDC;
856 if (!c->opt_user_specified) {
857 conn_flags |= NET_FLAGS_ANONYMOUS;
860 if (c->display_usage) {
865 _("Fetch domain SID into local secrets.tdb"));
869 return run_rpc_command(c, NULL, &ndr_table_samr,
871 rpc_getsid_internals,
875 /****************************************************************************/
878 * Basic usage function for 'net rpc user'.
879 * @param argc Standard main() style argc.
880 * @param argv Standard main() style argv. Initial components are already
884 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
886 return net_user_usage(c, argc, argv);
890 * Add a new user to a remote RPC server.
892 * @param argc Standard main() style argc.
893 * @param argv Standard main() style argv. Initial components are already
896 * @return A shell status integer (0 for success).
899 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
901 NET_API_STATUS status;
902 struct USER_INFO_1 info1;
903 uint32_t parm_error = 0;
905 if (argc < 1 || c->display_usage) {
906 rpc_user_usage(c, argc, argv);
912 info1.usri1_name = argv[0];
914 info1.usri1_password = argv[1];
917 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
920 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
921 argv[0], libnetapi_get_error_string(c->netapi_ctx,
925 d_printf(_("Added user '%s'.\n"), argv[0]);
932 * Rename a user on a remote RPC server.
934 * @param argc Standard main() style argc.
935 * @param argv Standard main() style argv. Initial components are already
938 * @return A shell status integer (0 for success).
941 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
943 NET_API_STATUS status;
944 struct USER_INFO_0 u0;
945 uint32_t parm_err = 0;
947 if (argc != 2 || c->display_usage) {
948 rpc_user_usage(c, argc, argv);
952 u0.usri0_name = argv[1];
954 status = NetUserSetInfo(c->opt_host, argv[0],
955 0, (uint8_t *)&u0, &parm_err);
958 _("Failed to rename user from %s to %s - %s\n"),
960 libnetapi_get_error_string(c->netapi_ctx, status));
962 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
969 * Set a user's primary group
971 * @param argc Standard main() style argc.
972 * @param argv Standard main() style argv. Initial components are already
975 * @return A shell status integer (0 for success).
978 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
981 NET_API_STATUS status;
983 struct GROUP_INFO_2 *g2;
984 struct USER_INFO_1051 u1051;
985 uint32_t parm_err = 0;
987 if (argc != 2 || c->display_usage) {
988 rpc_user_usage(c, argc, argv);
992 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
994 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
996 libnetapi_get_error_string(c->netapi_ctx, status));
999 g2 = (struct GROUP_INFO_2 *)buffer;
1001 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
1003 NetApiBufferFree(buffer);
1005 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
1006 (uint8_t *)&u1051, &parm_err);
1009 _("Failed to set user's primary group %s to %s - "
1010 "%s\n"), argv[0], argv[1],
1011 libnetapi_get_error_string(c->netapi_ctx, status));
1013 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
1020 * Delete a user from a remote RPC server.
1022 * @param argc Standard main() style argc.
1023 * @param argv Standard main() style argv. Initial components are already
1026 * @return A shell status integer (0 for success).
1029 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
1031 NET_API_STATUS status;
1033 if (argc < 1 || c->display_usage) {
1034 rpc_user_usage(c, argc, argv);
1038 status = NetUserDel(c->opt_host, argv[0]);
1041 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
1043 libnetapi_get_error_string(c->netapi_ctx, status));
1046 d_printf(_("Deleted user '%s'.\n"), argv[0]);
1053 * Set a user's password on a remote RPC server.
1055 * @param argc Standard main() style argc.
1056 * @param argv Standard main() style argv. Initial components are already
1059 * @return A shell status integer (0 for success).
1062 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
1064 NET_API_STATUS status;
1065 char *prompt = NULL;
1066 struct USER_INFO_1003 u1003;
1067 uint32_t parm_err = 0;
1070 if (argc < 1 || c->display_usage) {
1071 rpc_user_usage(c, argc, argv);
1076 u1003.usri1003_password = argv[1];
1078 char pwd[256] = {0};
1079 ret = asprintf(&prompt, _("Enter new password for %s:"),
1085 ret = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
1091 u1003.usri1003_password = talloc_strdup(c, pwd);
1092 if (u1003.usri1003_password == NULL) {
1097 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
1099 /* Display results */
1102 _("Failed to set password for '%s' with error: %s.\n"),
1103 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1112 * List a user's groups from a remote RPC server.
1114 * @param argc Standard main() style argc.
1115 * @param argv Standard main() style argv. Initial components are already
1118 * @return A shell status integer (0 for success)
1121 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1124 NET_API_STATUS status;
1125 struct GROUP_USERS_INFO_0 *u0 = NULL;
1126 uint32_t entries_read = 0;
1127 uint32_t total_entries = 0;
1131 if (argc < 1 || c->display_usage) {
1132 rpc_user_usage(c, argc, argv);
1136 status = NetUserGetGroups(c->opt_host,
1139 (uint8_t **)(void *)&u0,
1145 _("Failed to get groups for '%s' with error: %s.\n"),
1146 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1151 for (i=0; i < entries_read; i++) {
1152 printf("%s\n", u0->grui0_name);
1160 * List users on a remote RPC server.
1162 * All parameters are provided by the run_rpc_command function, except for
1163 * argc, argv which are passed through.
1165 * @param domain_sid The domain sid acquired from the remote server.
1166 * @param cli A cli_state connected to the server.
1167 * @param mem_ctx Talloc context, destroyed on completion of the function.
1168 * @param argc Standard main() style argc.
1169 * @param argv Standard main() style argv. Initial components are already
1172 * @return Normal NTSTATUS return.
1175 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
1177 NET_API_STATUS status;
1178 uint32_t start_idx=0, num_entries, i, loop_count = 0;
1179 struct NET_DISPLAY_USER *info = NULL;
1180 void *buffer = NULL;
1182 /* Query domain users */
1183 if (c->opt_long_list_entries)
1184 d_printf(_("\nUser name Comment"
1185 "\n-----------------------------\n"));
1187 uint32_t max_entries, max_size;
1189 dcerpc_get_query_dispinfo_params(
1190 loop_count, &max_entries, &max_size);
1192 status = NetQueryDisplayInformation(c->opt_host,
1199 if (status != 0 && status != ERROR_MORE_DATA) {
1203 info = (struct NET_DISPLAY_USER *)buffer;
1205 for (i = 0; i < num_entries; i++) {
1207 if (c->opt_long_list_entries)
1208 printf("%-21.21s %s\n", info->usri1_name,
1209 info->usri1_comment);
1211 printf("%s\n", info->usri1_name);
1215 NetApiBufferFree(buffer);
1218 start_idx += num_entries;
1220 } while (status == ERROR_MORE_DATA);
1226 * 'net rpc user' entrypoint.
1227 * @param argc Standard main() style argc.
1228 * @param argv Standard main() style argv. Initial components are already
1232 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1234 NET_API_STATUS status;
1236 struct functable func[] = {
1241 N_("Add specified user"),
1242 N_("net rpc user add\n"
1243 " Add specified user")
1249 N_("List domain groups of user"),
1250 N_("net rpc user info\n"
1251 " List domain groups of user")
1257 N_("Remove specified user"),
1258 N_("net rpc user delete\n"
1259 " Remove specified user")
1265 N_("Change user password"),
1266 N_("net rpc user password\n"
1267 " Change user password")
1273 N_("Rename specified user"),
1274 N_("net rpc user rename\n"
1275 " Rename specified user")
1279 rpc_user_setprimarygroup,
1281 "Set a user's primary group",
1282 "net rpc user setprimarygroup\n"
1283 " Set a user's primary group"
1285 {NULL, NULL, 0, NULL, NULL}
1288 status = libnetapi_net_init(&c->netapi_ctx);
1292 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1293 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1294 if (c->opt_kerberos) {
1295 libnetapi_set_use_kerberos(c->netapi_ctx);
1299 if (c->display_usage) {
1304 _("List all users"));
1305 net_display_usage_from_functable(func);
1309 return rpc_user_list(c, argc, argv);
1312 return net_run_function(c, argc, argv, "net rpc user", func);
1315 static NTSTATUS rpc_sh_user_list(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 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1324 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1325 TALLOC_CTX *mem_ctx,
1326 struct rpc_sh_ctx *ctx,
1327 struct rpc_pipe_client *pipe_hnd,
1328 int argc, const char **argv)
1330 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1333 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1334 TALLOC_CTX *mem_ctx,
1335 struct rpc_sh_ctx *ctx,
1336 struct rpc_pipe_client *pipe_hnd,
1337 int argc, const char **argv,
1339 struct net_context *c,
1340 TALLOC_CTX *mem_ctx,
1341 struct rpc_sh_ctx *ctx,
1342 struct rpc_pipe_client *pipe_hnd,
1343 struct policy_handle *user_hnd,
1344 int argc, const char **argv))
1346 struct policy_handle connect_pol, domain_pol, user_pol;
1347 NTSTATUS status, result;
1350 enum lsa_SidType type;
1351 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1354 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1356 return NT_STATUS_INVALID_PARAMETER;
1359 ZERO_STRUCT(connect_pol);
1360 ZERO_STRUCT(domain_pol);
1361 ZERO_STRUCT(user_pol);
1363 status = net_rpc_lookup_name(c, mem_ctx, ctx->cli,
1364 argv[0], NULL, NULL, &sid, &type);
1365 if (!NT_STATUS_IS_OK(status)) {
1366 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1371 if (type != SID_NAME_USER) {
1372 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1373 sid_type_lookup(type));
1374 status = NT_STATUS_NO_SUCH_USER;
1378 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1379 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1380 status = NT_STATUS_NO_SUCH_USER;
1384 status = dcerpc_samr_Connect2(b, mem_ctx,
1386 MAXIMUM_ALLOWED_ACCESS,
1389 if (!NT_STATUS_IS_OK(status)) {
1392 if (!NT_STATUS_IS_OK(result)) {
1397 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1399 MAXIMUM_ALLOWED_ACCESS,
1403 if (!NT_STATUS_IS_OK(status)) {
1406 if (!NT_STATUS_IS_OK(result)) {
1411 status = dcerpc_samr_OpenUser(b, mem_ctx,
1413 MAXIMUM_ALLOWED_ACCESS,
1417 if (!NT_STATUS_IS_OK(status)) {
1420 if (!NT_STATUS_IS_OK(result)) {
1425 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1428 if (is_valid_policy_hnd(&user_pol)) {
1429 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1431 if (is_valid_policy_hnd(&domain_pol)) {
1432 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1434 if (is_valid_policy_hnd(&connect_pol)) {
1435 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1440 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1441 TALLOC_CTX *mem_ctx,
1442 struct rpc_sh_ctx *ctx,
1443 struct rpc_pipe_client *pipe_hnd,
1444 struct policy_handle *user_hnd,
1445 int argc, const char **argv)
1447 NTSTATUS status, result;
1448 union samr_UserInfo *info = NULL;
1449 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1452 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1454 return NT_STATUS_INVALID_PARAMETER;
1457 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1462 if (!NT_STATUS_IS_OK(status)) {
1465 if (!NT_STATUS_IS_OK(result)) {
1469 d_printf(_("user rid: %d, group rid: %d\n"),
1471 info->info21.primary_gid);
1476 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1477 TALLOC_CTX *mem_ctx,
1478 struct rpc_sh_ctx *ctx,
1479 struct rpc_pipe_client *pipe_hnd,
1480 int argc, const char **argv)
1482 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1483 rpc_sh_user_show_internals);
1486 #define FETCHSTR(name, rec) \
1487 do { if (strequal(ctx->thiscmd, name)) { \
1488 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1491 #define SETSTR(name, rec, flag) \
1492 do { if (strequal(ctx->thiscmd, name)) { \
1493 init_lsa_String(&(info->info21.rec), argv[0]); \
1494 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1497 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1498 TALLOC_CTX *mem_ctx,
1499 struct rpc_sh_ctx *ctx,
1500 struct rpc_pipe_client *pipe_hnd,
1501 struct policy_handle *user_hnd,
1502 int argc, const char **argv)
1504 NTSTATUS status, result;
1505 const char *username;
1506 const char *oldval = "";
1507 union samr_UserInfo *info = NULL;
1508 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1511 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1512 _("Usage:"), ctx->whoami);
1513 return NT_STATUS_INVALID_PARAMETER;
1516 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1521 if (!NT_STATUS_IS_OK(status)) {
1524 if (!NT_STATUS_IS_OK(result)) {
1528 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1530 FETCHSTR("fullname", full_name);
1531 FETCHSTR("homedir", home_directory);
1532 FETCHSTR("homedrive", home_drive);
1533 FETCHSTR("logonscript", logon_script);
1534 FETCHSTR("profilepath", profile_path);
1535 FETCHSTR("description", description);
1538 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1542 if (strcmp(argv[0], "NULL") == 0) {
1546 ZERO_STRUCT(info->info21);
1548 SETSTR("fullname", full_name, FULL_NAME);
1549 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1550 SETSTR("homedrive", home_drive, HOME_DRIVE);
1551 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1552 SETSTR("profilepath", profile_path, PROFILE_PATH);
1553 SETSTR("description", description, DESCRIPTION);
1555 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1560 if (!NT_STATUS_IS_OK(status)) {
1566 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1567 ctx->thiscmd, oldval, argv[0]);
1574 #define HANDLEFLG(name, rec) \
1575 do { if (strequal(ctx->thiscmd, name)) { \
1576 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1578 newflags = oldflags | ACB_##rec; \
1580 newflags = oldflags & ~ACB_##rec; \
1583 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1584 TALLOC_CTX *mem_ctx,
1585 struct rpc_sh_ctx *ctx,
1586 struct rpc_pipe_client *pipe_hnd,
1587 int argc, const char **argv)
1589 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1590 rpc_sh_user_str_edit_internals);
1593 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1594 TALLOC_CTX *mem_ctx,
1595 struct rpc_sh_ctx *ctx,
1596 struct rpc_pipe_client *pipe_hnd,
1597 struct policy_handle *user_hnd,
1598 int argc, const char **argv)
1600 NTSTATUS status, result;
1601 const char *username;
1602 const char *oldval = "unknown";
1603 uint32_t oldflags, newflags;
1605 union samr_UserInfo *info = NULL;
1606 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1609 ((argc == 1) && !strequal(argv[0], "yes") &&
1610 !strequal(argv[0], "no"))) {
1611 /* TRANSATORS: The yes|no here are program keywords. Please do
1613 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1615 return NT_STATUS_INVALID_PARAMETER;
1618 newval = strequal(argv[0], "yes");
1620 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1625 if (!NT_STATUS_IS_OK(status)) {
1628 if (!NT_STATUS_IS_OK(result)) {
1632 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1633 oldflags = info->info21.acct_flags;
1634 newflags = info->info21.acct_flags;
1636 HANDLEFLG("disabled", DISABLED);
1637 HANDLEFLG("pwnotreq", PWNOTREQ);
1638 HANDLEFLG("autolock", AUTOLOCK);
1639 HANDLEFLG("pwnoexp", PWNOEXP);
1642 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1647 ZERO_STRUCT(info->info21);
1649 info->info21.acct_flags = newflags;
1650 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1652 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1657 if (!NT_STATUS_IS_OK(status)) {
1661 if (NT_STATUS_IS_OK(result)) {
1662 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1663 ctx->thiscmd, oldval, argv[0]);
1671 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1672 TALLOC_CTX *mem_ctx,
1673 struct rpc_sh_ctx *ctx,
1674 struct rpc_pipe_client *pipe_hnd,
1675 int argc, const char **argv)
1677 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1678 rpc_sh_user_flag_edit_internals);
1681 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1682 TALLOC_CTX *mem_ctx,
1683 struct rpc_sh_ctx *ctx)
1685 static struct rpc_sh_cmd cmds[] = {
1687 { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1688 N_("Show/Set a user's full name") },
1690 { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1691 N_("Show/Set a user's home directory") },
1693 { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1694 N_("Show/Set a user's home drive") },
1696 { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1697 N_("Show/Set a user's logon script") },
1699 { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1700 N_("Show/Set a user's profile path") },
1702 { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1703 N_("Show/Set a user's description") },
1705 { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1706 N_("Show/Set whether a user is disabled") },
1708 { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1709 N_("Show/Set whether a user locked out") },
1711 { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1712 N_("Show/Set whether a user does not need a password") },
1714 { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1715 N_("Show/Set whether a user's password does not expire") },
1717 { NULL, NULL, 0, NULL, NULL }
1723 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1724 TALLOC_CTX *mem_ctx,
1725 struct rpc_sh_ctx *ctx)
1727 static struct rpc_sh_cmd cmds[] = {
1729 { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
1730 N_("List available users") },
1732 { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
1733 N_("List the domain groups a user is member of") },
1735 { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
1736 N_("Show info about a user") },
1738 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1739 N_("Show/Modify a user's fields") },
1741 { NULL, NULL, 0, NULL, NULL }
1747 /****************************************************************************/
1750 * Basic usage function for 'net rpc group'.
1751 * @param argc Standard main() style argc.
1752 * @param argv Standard main() style argv. Initial components are already
1756 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1758 return net_group_usage(c, argc, argv);
1762 * Delete group on a remote RPC server.
1764 * All parameters are provided by the run_rpc_command function, except for
1765 * argc, argv which are passed through.
1767 * @param domain_sid The domain sid acquired from the remote server.
1768 * @param cli A cli_state connected to the server.
1769 * @param mem_ctx Talloc context, destroyed on completion of the function.
1770 * @param argc Standard main() style argc.
1771 * @param argv Standard main() style argv. Initial components are already
1774 * @return Normal NTSTATUS return.
1777 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1778 const struct dom_sid *domain_sid,
1779 const char *domain_name,
1780 struct cli_state *cli,
1781 struct rpc_pipe_client *pipe_hnd,
1782 TALLOC_CTX *mem_ctx,
1786 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1787 bool group_is_primary = false;
1788 NTSTATUS status, result;
1790 struct samr_RidAttrArray *rids = NULL;
1793 /* struct samr_RidWithAttribute *user_gids; */
1794 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1796 struct samr_Ids group_rids, name_types;
1797 struct lsa_String lsa_acct_name;
1798 union samr_UserInfo *info = NULL;
1800 if (argc < 1 || c->display_usage) {
1801 rpc_group_usage(c, argc,argv);
1802 return NT_STATUS_OK; /* ok? */
1805 status = dcerpc_samr_Connect2(b, mem_ctx,
1807 MAXIMUM_ALLOWED_ACCESS,
1810 if (!NT_STATUS_IS_OK(status)) {
1811 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1815 if (!NT_STATUS_IS_OK(result)) {
1817 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1821 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1823 MAXIMUM_ALLOWED_ACCESS,
1824 discard_const_p(struct dom_sid2, domain_sid),
1827 if (!NT_STATUS_IS_OK(status)) {
1828 d_fprintf(stderr, _("Request open_domain failed\n"));
1832 if (!NT_STATUS_IS_OK(result)) {
1834 d_fprintf(stderr, _("Request open_domain failed\n"));
1838 init_lsa_String(&lsa_acct_name, argv[0]);
1840 status = dcerpc_samr_LookupNames(b, mem_ctx,
1847 if (!NT_STATUS_IS_OK(status)) {
1848 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1852 if (!NT_STATUS_IS_OK(result)) {
1854 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1857 if (group_rids.count != 1) {
1858 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1861 if (name_types.count != 1) {
1862 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1866 switch (name_types.ids[0])
1868 case SID_NAME_DOM_GRP:
1869 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1871 MAXIMUM_ALLOWED_ACCESS,
1875 if (!NT_STATUS_IS_OK(status)) {
1876 d_fprintf(stderr, _("Request open_group failed"));
1880 if (!NT_STATUS_IS_OK(result)) {
1882 d_fprintf(stderr, _("Request open_group failed"));
1886 group_rid = group_rids.ids[0];
1888 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1892 if (!NT_STATUS_IS_OK(status)) {
1894 _("Unable to query group members of %s"),
1899 if (!NT_STATUS_IS_OK(result)) {
1902 _("Unable to query group members of %s"),
1907 if (c->opt_verbose) {
1909 _("Domain Group %s (rid: %d) has %d members\n"),
1910 argv[0],group_rid, rids->count);
1913 /* Check if group is anyone's primary group */
1914 for (i = 0; i < rids->count; i++)
1916 status = dcerpc_samr_OpenUser(b, mem_ctx,
1918 MAXIMUM_ALLOWED_ACCESS,
1922 if (!NT_STATUS_IS_OK(status)) {
1924 _("Unable to open group member %d\n"),
1929 if (!NT_STATUS_IS_OK(result)) {
1932 _("Unable to open group member %d\n"),
1937 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1942 if (!NT_STATUS_IS_OK(status)) {
1944 _("Unable to lookup userinfo for group "
1950 if (!NT_STATUS_IS_OK(result)) {
1953 _("Unable to lookup userinfo for group "
1959 if (info->info21.primary_gid == group_rid) {
1960 if (c->opt_verbose) {
1961 d_printf(_("Group is primary group "
1963 info->info21.account_name.string);
1965 group_is_primary = true;
1968 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1971 if (group_is_primary) {
1972 d_fprintf(stderr, _("Unable to delete group because "
1973 "some of it's members have it as primary "
1975 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1979 /* remove all group members */
1980 for (i = 0; i < rids->count; i++)
1983 d_printf(_("Remove group member %d..."),
1985 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1989 if (!NT_STATUS_IS_OK(status)) {
1993 if (NT_STATUS_IS_OK(result)) {
1995 d_printf(_("ok\n"));
1998 d_printf("%s\n", _("failed"));
2003 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2006 if (!NT_STATUS_IS_OK(status)) {
2013 /* removing a local group is easier... */
2014 case SID_NAME_ALIAS:
2015 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2017 MAXIMUM_ALLOWED_ACCESS,
2021 if (!NT_STATUS_IS_OK(status)) {
2022 d_fprintf(stderr, _("Request open_alias failed\n"));
2025 if (!NT_STATUS_IS_OK(result)) {
2027 d_fprintf(stderr, _("Request open_alias failed\n"));
2031 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
2034 if (!NT_STATUS_IS_OK(status)) {
2042 d_fprintf(stderr, _("%s is of type %s. This command is only "
2043 "for deleting local or global groups\n"),
2044 argv[0],sid_type_lookup(name_types.ids[0]));
2045 status = NT_STATUS_UNSUCCESSFUL;
2049 if (NT_STATUS_IS_OK(status)) {
2051 d_printf(_("Deleted %s '%s'\n"),
2052 sid_type_lookup(name_types.ids[0]), argv[0]);
2054 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
2055 get_friendly_nt_error_msg(status));
2063 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
2065 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2066 rpc_group_delete_internals, argc,argv);
2069 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
2071 NET_API_STATUS status;
2072 struct GROUP_INFO_1 info1;
2073 uint32_t parm_error = 0;
2075 if (argc != 1 || c->display_usage) {
2076 rpc_group_usage(c, argc, argv);
2082 info1.grpi1_name = argv[0];
2083 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2084 info1.grpi1_comment = c->opt_comment;
2087 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2091 _("Failed to add group '%s' with error: %s.\n"),
2092 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2096 d_printf(_("Added group '%s'.\n"), argv[0]);
2102 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
2104 NET_API_STATUS status;
2105 struct LOCALGROUP_INFO_1 info1;
2106 uint32_t parm_error = 0;
2108 if (argc != 1 || c->display_usage) {
2109 rpc_group_usage(c, argc, argv);
2115 info1.lgrpi1_name = argv[0];
2116 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2117 info1.lgrpi1_comment = c->opt_comment;
2120 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2124 _("Failed to add alias '%s' with error: %s.\n"),
2125 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2129 d_printf(_("Added alias '%s'.\n"), argv[0]);
2135 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
2137 if (c->opt_localgroup)
2138 return rpc_alias_add_internals(c, argc, argv);
2140 return rpc_group_add_internals(c, argc, argv);
2143 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2144 TALLOC_CTX *mem_ctx,
2146 struct dom_sid *sid,
2147 enum lsa_SidType *type)
2149 struct dom_sid *sids = NULL;
2150 enum lsa_SidType *types = NULL;
2151 struct rpc_pipe_client *pipe_hnd = NULL;
2152 struct policy_handle lsa_pol;
2153 NTSTATUS status, result;
2154 struct dcerpc_binding_handle *b;
2156 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
2158 if (!NT_STATUS_IS_OK(status)) {
2162 b = pipe_hnd->binding_handle;
2164 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2165 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2167 if (!NT_STATUS_IS_OK(status)) {
2171 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2172 &name, NULL, 1, &sids, &types);
2174 if (NT_STATUS_IS_OK(status)) {
2175 sid_copy(sid, &sids[0]);
2179 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
2183 TALLOC_FREE(pipe_hnd);
2186 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
2188 /* Try as S-1-5-whatever */
2190 struct dom_sid tmp_sid;
2192 if (string_to_sid(&tmp_sid, name)) {
2193 sid_copy(sid, &tmp_sid);
2194 *type = SID_NAME_UNKNOWN;
2195 status = NT_STATUS_OK;
2202 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2203 TALLOC_CTX *mem_ctx,
2204 const struct dom_sid *group_sid,
2207 struct policy_handle connect_pol, domain_pol;
2208 NTSTATUS status, result;
2210 struct policy_handle group_pol;
2211 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2213 struct samr_Ids rids, rid_types;
2214 struct lsa_String lsa_acct_name;
2218 sid_copy(&sid, group_sid);
2220 if (!sid_split_rid(&sid, &group_rid)) {
2221 return NT_STATUS_UNSUCCESSFUL;
2224 /* Get sam policy handle */
2225 status = dcerpc_samr_Connect2(b, mem_ctx,
2227 MAXIMUM_ALLOWED_ACCESS,
2230 if (!NT_STATUS_IS_OK(status)) {
2233 if (!NT_STATUS_IS_OK(result)) {
2237 /* Get domain policy handle */
2238 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2240 MAXIMUM_ALLOWED_ACCESS,
2244 if (!NT_STATUS_IS_OK(status)) {
2247 if (!NT_STATUS_IS_OK(result)) {
2251 init_lsa_String(&lsa_acct_name, member);
2253 status = dcerpc_samr_LookupNames(b, mem_ctx,
2260 if (!NT_STATUS_IS_OK(status)) {
2261 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2266 if (!NT_STATUS_IS_OK(result)) {
2268 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2272 if (rids.count != 1) {
2273 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2276 if (rid_types.count != 1) {
2277 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2281 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2283 MAXIMUM_ALLOWED_ACCESS,
2287 if (!NT_STATUS_IS_OK(status)) {
2291 if (!NT_STATUS_IS_OK(result)) {
2296 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2299 0x0005, /* unknown flags */
2301 if (!NT_STATUS_IS_OK(status)) {
2308 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2312 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2313 struct cli_state *cli,
2314 TALLOC_CTX *mem_ctx,
2315 const struct dom_sid *alias_sid,
2318 struct policy_handle connect_pol, domain_pol;
2319 NTSTATUS status, result;
2321 struct policy_handle alias_pol;
2322 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2324 struct dom_sid member_sid;
2325 enum lsa_SidType member_type;
2329 sid_copy(&sid, alias_sid);
2331 if (!sid_split_rid(&sid, &alias_rid)) {
2332 return NT_STATUS_UNSUCCESSFUL;
2335 result = get_sid_from_name(cli, mem_ctx,
2336 member, &member_sid, &member_type);
2338 if (!NT_STATUS_IS_OK(result)) {
2339 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2344 /* Get sam policy handle */
2345 status = dcerpc_samr_Connect2(b, mem_ctx,
2347 MAXIMUM_ALLOWED_ACCESS,
2350 if (!NT_STATUS_IS_OK(status)) {
2353 if (!NT_STATUS_IS_OK(result)) {
2358 /* Get domain policy handle */
2359 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2361 MAXIMUM_ALLOWED_ACCESS,
2365 if (!NT_STATUS_IS_OK(status)) {
2368 if (!NT_STATUS_IS_OK(result)) {
2373 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2375 MAXIMUM_ALLOWED_ACCESS,
2379 if (!NT_STATUS_IS_OK(status)) {
2382 if (!NT_STATUS_IS_OK(result)) {
2386 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2390 if (!NT_STATUS_IS_OK(status)) {
2397 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2401 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2402 const struct dom_sid *domain_sid,
2403 const char *domain_name,
2404 struct cli_state *cli,
2405 struct rpc_pipe_client *pipe_hnd,
2406 TALLOC_CTX *mem_ctx,
2410 struct dom_sid group_sid;
2411 enum lsa_SidType group_type;
2413 if (argc != 2 || c->display_usage) {
2416 _("net rpc group addmem <group> <member>\n"
2417 " Add a member to a group\n"
2418 " group\tGroup to add member to\n"
2419 " member\tMember to add to group\n"));
2420 return NT_STATUS_UNSUCCESSFUL;
2423 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2424 &group_sid, &group_type))) {
2425 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2427 return NT_STATUS_UNSUCCESSFUL;
2430 if (group_type == SID_NAME_DOM_GRP) {
2431 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2432 &group_sid, argv[1]);
2434 if (!NT_STATUS_IS_OK(result)) {
2435 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2436 argv[1], argv[0], nt_errstr(result));
2441 if (group_type == SID_NAME_ALIAS) {
2442 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, cli, mem_ctx,
2443 &group_sid, argv[1]);
2445 if (!NT_STATUS_IS_OK(result)) {
2446 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2447 argv[1], argv[0], nt_errstr(result));
2452 d_fprintf(stderr, _("Can only add members to global or local groups "
2453 "which %s is not\n"), argv[0]);
2455 return NT_STATUS_UNSUCCESSFUL;
2458 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2460 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2461 rpc_group_addmem_internals,
2465 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2466 struct rpc_pipe_client *pipe_hnd,
2467 TALLOC_CTX *mem_ctx,
2468 const struct dom_sid *group_sid,
2471 struct policy_handle connect_pol, domain_pol;
2472 NTSTATUS status, result;
2474 struct policy_handle group_pol;
2475 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2477 struct samr_Ids rids, rid_types;
2478 struct lsa_String lsa_acct_name;
2482 sid_copy(&sid, group_sid);
2484 if (!sid_split_rid(&sid, &group_rid))
2485 return NT_STATUS_UNSUCCESSFUL;
2487 /* Get sam policy handle */
2488 status = dcerpc_samr_Connect2(b, mem_ctx,
2490 MAXIMUM_ALLOWED_ACCESS,
2493 if (!NT_STATUS_IS_OK(status)) {
2496 if (!NT_STATUS_IS_OK(result)) {
2501 /* Get domain policy handle */
2502 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2504 MAXIMUM_ALLOWED_ACCESS,
2508 if (!NT_STATUS_IS_OK(status)) {
2511 if (!NT_STATUS_IS_OK(result)) {
2515 init_lsa_String(&lsa_acct_name, member);
2517 status = dcerpc_samr_LookupNames(b, mem_ctx,
2524 if (!NT_STATUS_IS_OK(status)) {
2525 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2530 if (!NT_STATUS_IS_OK(result)) {
2532 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2536 if (rids.count != 1) {
2537 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2540 if (rid_types.count != 1) {
2541 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2545 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2547 MAXIMUM_ALLOWED_ACCESS,
2551 if (!NT_STATUS_IS_OK(status)) {
2554 if (!NT_STATUS_IS_OK(result)) {
2559 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2563 if (!NT_STATUS_IS_OK(status)) {
2569 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2573 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2574 struct cli_state *cli,
2575 TALLOC_CTX *mem_ctx,
2576 const struct dom_sid *alias_sid,
2579 struct policy_handle connect_pol, domain_pol;
2580 NTSTATUS status, result;
2582 struct policy_handle alias_pol;
2583 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2585 struct dom_sid member_sid;
2586 enum lsa_SidType member_type;
2590 sid_copy(&sid, alias_sid);
2592 if (!sid_split_rid(&sid, &alias_rid))
2593 return NT_STATUS_UNSUCCESSFUL;
2595 result = get_sid_from_name(cli, mem_ctx,
2596 member, &member_sid, &member_type);
2598 if (!NT_STATUS_IS_OK(result)) {
2599 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2604 /* Get sam policy handle */
2605 status = dcerpc_samr_Connect2(b, mem_ctx,
2607 MAXIMUM_ALLOWED_ACCESS,
2610 if (!NT_STATUS_IS_OK(status)) {
2613 if (!NT_STATUS_IS_OK(result)) {
2618 /* Get domain policy handle */
2619 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2621 MAXIMUM_ALLOWED_ACCESS,
2625 if (!NT_STATUS_IS_OK(status)) {
2628 if (!NT_STATUS_IS_OK(result)) {
2633 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2635 MAXIMUM_ALLOWED_ACCESS,
2639 if (!NT_STATUS_IS_OK(status)) {
2643 if (!NT_STATUS_IS_OK(result)) {
2647 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2652 if (!NT_STATUS_IS_OK(status)) {
2659 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2663 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2664 const struct dom_sid *domain_sid,
2665 const char *domain_name,
2666 struct cli_state *cli,
2667 struct rpc_pipe_client *pipe_hnd,
2668 TALLOC_CTX *mem_ctx,
2672 struct dom_sid group_sid;
2673 enum lsa_SidType group_type;
2675 if (argc != 2 || c->display_usage) {
2678 _("net rpc group delmem <group> <member>\n"
2679 " Delete a member from a group\n"
2680 " group\tGroup to delete member from\n"
2681 " member\tMember to delete from group\n"));
2682 return NT_STATUS_UNSUCCESSFUL;
2685 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2686 &group_sid, &group_type))) {
2687 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2689 return NT_STATUS_UNSUCCESSFUL;
2692 if (group_type == SID_NAME_DOM_GRP) {
2693 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2694 &group_sid, argv[1]);
2696 if (!NT_STATUS_IS_OK(result)) {
2697 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2698 argv[1], argv[0], nt_errstr(result));
2703 if (group_type == SID_NAME_ALIAS) {
2704 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, cli, mem_ctx,
2705 &group_sid, argv[1]);
2707 if (!NT_STATUS_IS_OK(result)) {
2708 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2709 argv[1], argv[0], nt_errstr(result));
2714 d_fprintf(stderr, _("Can only delete members from global or local "
2715 "groups which %s is not\n"), argv[0]);
2717 return NT_STATUS_UNSUCCESSFUL;
2720 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2722 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2723 rpc_group_delmem_internals,
2728 * List groups on a remote RPC server.
2730 * All parameters are provided by the run_rpc_command function, except for
2731 * argc, argv which are passes through.
2733 * @param domain_sid The domain sid acquired from the remote server.
2734 * @param cli A cli_state connected to the server.
2735 * @param mem_ctx Talloc context, destroyed on completion of the function.
2736 * @param argc Standard main() style argc.
2737 * @param argv Standard main() style argv. Initial components are already
2740 * @return Normal NTSTATUS return.
2743 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2744 const struct dom_sid *domain_sid,
2745 const char *domain_name,
2746 struct cli_state *cli,
2747 struct rpc_pipe_client *pipe_hnd,
2748 TALLOC_CTX *mem_ctx,
2752 struct policy_handle connect_pol, domain_pol;
2753 NTSTATUS status, result;
2754 uint32_t start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2755 struct samr_SamArray *groups = NULL;
2756 bool global = false;
2758 bool builtin = false;
2759 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2761 if (c->display_usage) {
2764 _("net rpc group list [global] [local] [builtin]\n"
2765 " List groups on RPC server\n"
2766 " global\tList global groups\n"
2767 " local\tList local groups\n"
2768 " builtin\tList builtin groups\n"
2769 " If none of global, local or builtin is "
2770 "specified, all three options are considered "
2772 return NT_STATUS_OK;
2781 for (i=0; i<argc; i++) {
2782 if (strequal(argv[i], "global"))
2785 if (strequal(argv[i], "local"))
2788 if (strequal(argv[i], "builtin"))
2792 /* Get sam policy handle */
2794 status = dcerpc_samr_Connect2(b, mem_ctx,
2796 MAXIMUM_ALLOWED_ACCESS,
2799 if (!NT_STATUS_IS_OK(status)) {
2802 if (!NT_STATUS_IS_OK(result)) {
2807 /* Get domain policy handle */
2809 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2811 MAXIMUM_ALLOWED_ACCESS,
2812 discard_const_p(struct dom_sid2, domain_sid),
2815 if (!NT_STATUS_IS_OK(status)) {
2818 if (!NT_STATUS_IS_OK(result)) {
2823 /* Query domain groups */
2824 if (c->opt_long_list_entries)
2825 d_printf(_("\nGroup name Comment"
2826 "\n-----------------------------\n"));
2828 uint32_t max_size, total_size, returned_size;
2829 union samr_DispInfo info;
2833 dcerpc_get_query_dispinfo_params(
2834 loop_count, &max_entries, &max_size);
2836 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2846 if (!NT_STATUS_IS_OK(status)) {
2849 num_entries = info.info3.count;
2850 start_idx += info.info3.count;
2852 if (!NT_STATUS_IS_OK(result) &&
2853 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2856 for (i = 0; i < num_entries; i++) {
2858 const char *group = NULL;
2859 const char *desc = NULL;
2861 group = info.info3.entries[i].account_name.string;
2862 desc = info.info3.entries[i].description.string;
2864 if (c->opt_long_list_entries)
2865 printf("%-21.21s %-50.50s\n",
2868 printf("%s\n", group);
2870 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2871 /* query domain aliases */
2876 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2883 if (!NT_STATUS_IS_OK(status)) {
2886 if (!NT_STATUS_IS_OK(result) &&
2887 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2890 for (i = 0; i < num_entries; i++) {
2892 const char *description = NULL;
2894 if (c->opt_long_list_entries) {
2896 struct policy_handle alias_pol;
2897 union samr_AliasInfo *info = NULL;
2900 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2903 groups->entries[i].idx,
2906 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2907 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2912 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2913 status = dcerpc_samr_Close(b, mem_ctx,
2916 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2917 description = info->description.string;
2923 if (description != NULL) {
2924 printf("%-21.21s %-50.50s\n",
2925 groups->entries[i].name.string,
2928 printf("%s\n", groups->entries[i].name.string);
2931 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2932 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2933 /* Get builtin policy handle */
2935 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2937 MAXIMUM_ALLOWED_ACCESS,
2938 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2941 if (!NT_STATUS_IS_OK(status)) {
2944 if (!NT_STATUS_IS_OK(result)) {
2949 /* query builtin aliases */
2952 if (!builtin) break;
2954 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2961 if (!NT_STATUS_IS_OK(status)) {
2964 if (!NT_STATUS_IS_OK(result) &&
2965 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2970 for (i = 0; i < num_entries; i++) {
2972 const char *description = NULL;
2974 if (c->opt_long_list_entries) {
2976 struct policy_handle alias_pol;
2977 union samr_AliasInfo *info = NULL;
2980 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2983 groups->entries[i].idx,
2986 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2987 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2992 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2993 status = dcerpc_samr_Close(b, mem_ctx,
2996 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2997 description = info->description.string;
3003 if (description != NULL) {
3004 printf("%-21.21s %-50.50s\n",
3005 groups->entries[i].name.string,
3008 printf("%s\n", groups->entries[i].name.string);
3011 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3019 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
3021 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3022 rpc_group_list_internals,
3026 static NTSTATUS rpc_list_group_members(struct net_context *c,
3027 struct rpc_pipe_client *pipe_hnd,
3028 TALLOC_CTX *mem_ctx,
3029 const char *domain_name,
3030 const struct dom_sid *domain_sid,
3031 struct policy_handle *domain_pol,
3034 NTSTATUS result, status;
3035 struct policy_handle group_pol;
3036 uint32_t num_members, *group_rids;
3038 struct samr_RidAttrArray *rids = NULL;
3039 struct lsa_Strings names;
3040 struct samr_Ids types;
3041 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3044 sid_to_fstring(sid_str, domain_sid);
3046 status = dcerpc_samr_OpenGroup(b, mem_ctx,
3048 MAXIMUM_ALLOWED_ACCESS,
3052 if (!NT_STATUS_IS_OK(status)) {
3055 if (!NT_STATUS_IS_OK(result)) {
3059 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
3063 if (!NT_STATUS_IS_OK(status)) {
3066 if (!NT_STATUS_IS_OK(result)) {
3070 num_members = rids->count;
3071 group_rids = rids->rids;
3073 while (num_members > 0) {
3074 int this_time = 512;
3076 if (num_members < this_time)
3077 this_time = num_members;
3079 status = dcerpc_samr_LookupRids(b, mem_ctx,
3086 if (!NT_STATUS_IS_OK(status)) {
3089 if (!NT_STATUS_IS_OK(result)) {
3092 if (names.count != this_time) {
3093 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3095 if (types.count != this_time) {
3096 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3098 /* We only have users as members, but make the output
3099 the same as the output of alias members */
3101 for (i = 0; i < this_time; i++) {
3103 if (c->opt_long_list_entries) {
3104 printf("%s-%d %s\\%s %d\n", sid_str,
3105 group_rids[i], domain_name,
3106 names.names[i].string,
3109 printf("%s\\%s\n", domain_name,
3110 names.names[i].string);
3114 num_members -= this_time;
3118 return NT_STATUS_OK;
3121 static NTSTATUS rpc_list_alias_members(struct net_context *c,
3122 struct rpc_pipe_client *pipe_hnd,
3123 struct cli_state *cli,
3124 TALLOC_CTX *mem_ctx,
3125 struct policy_handle *domain_pol,
3128 NTSTATUS result, status;
3129 struct rpc_pipe_client *lsa_pipe;
3130 struct policy_handle alias_pol, lsa_pol;
3131 uint32_t num_members;
3132 struct dom_sid *alias_sids;
3135 enum lsa_SidType *types;
3137 struct lsa_SidArray sid_array;
3138 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3140 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3142 MAXIMUM_ALLOWED_ACCESS,
3146 if (!NT_STATUS_IS_OK(status)) {
3149 if (!NT_STATUS_IS_OK(result)) {
3153 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
3157 if (!NT_STATUS_IS_OK(status)) {
3158 d_fprintf(stderr, _("Couldn't list alias members\n"));
3161 if (!NT_STATUS_IS_OK(result)) {
3162 d_fprintf(stderr, _("Couldn't list alias members\n"));
3166 num_members = sid_array.num_sids;
3168 if (num_members == 0) {
3169 return NT_STATUS_OK;
3172 result = cli_rpc_pipe_open_noauth(cli,
3175 if (!NT_STATUS_IS_OK(result)) {
3176 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
3177 nt_errstr(result) );
3181 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
3182 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
3184 if (!NT_STATUS_IS_OK(result)) {
3185 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
3186 TALLOC_FREE(lsa_pipe);
3190 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
3192 d_fprintf(stderr, _("Out of memory\n"));
3193 TALLOC_FREE(lsa_pipe);
3194 return NT_STATUS_NO_MEMORY;
3197 for (i=0; i<num_members; i++) {
3198 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
3201 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
3202 num_members, alias_sids,
3203 &domains, &names, &types);
3205 if (!NT_STATUS_IS_OK(result) &&
3206 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3207 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3208 TALLOC_FREE(lsa_pipe);
3212 for (i = 0; i < num_members; i++) {
3214 sid_to_fstring(sid_str, &alias_sids[i]);
3216 if (c->opt_long_list_entries) {
3217 printf("%s %s\\%s %d\n", sid_str,
3218 domains[i] ? domains[i] : _("*unknown*"),
3219 names[i] ? names[i] : _("*unknown*"), types[i]);
3222 printf("%s\\%s\n", domains[i], names[i]);
3224 printf("%s\n", sid_str);
3228 TALLOC_FREE(lsa_pipe);
3229 return NT_STATUS_OK;
3232 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3233 const struct dom_sid *domain_sid,
3234 const char *domain_name,
3235 struct cli_state *cli,
3236 struct rpc_pipe_client *pipe_hnd,
3237 TALLOC_CTX *mem_ctx,
3241 NTSTATUS result, status;
3242 struct policy_handle connect_pol, domain_pol;
3243 struct samr_Ids rids, rid_types;
3244 struct lsa_String lsa_acct_name;
3245 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3247 /* Get sam policy handle */
3249 status = dcerpc_samr_Connect2(b, mem_ctx,
3251 MAXIMUM_ALLOWED_ACCESS,
3254 if (!NT_STATUS_IS_OK(status)) {
3257 if (!NT_STATUS_IS_OK(result)) {
3261 /* Get domain policy handle */
3263 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3265 MAXIMUM_ALLOWED_ACCESS,
3266 discard_const_p(struct dom_sid2, domain_sid),
3269 if (!NT_STATUS_IS_OK(status)) {
3272 if (!NT_STATUS_IS_OK(result)) {
3276 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3278 status = dcerpc_samr_LookupNames(b, mem_ctx,
3285 if (!NT_STATUS_IS_OK(status)) {
3289 if (!NT_STATUS_IS_OK(result)) {
3291 /* Ok, did not find it in the global sam, try with builtin */
3293 struct dom_sid sid_Builtin;
3295 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3297 sid_copy(&sid_Builtin, &global_sid_Builtin);
3299 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3301 MAXIMUM_ALLOWED_ACCESS,
3305 if (!NT_STATUS_IS_OK(status)) {
3308 if (!NT_STATUS_IS_OK(result)) {
3309 d_fprintf(stderr, _("Couldn't find group %s\n"),
3314 status = dcerpc_samr_LookupNames(b, mem_ctx,
3321 if (!NT_STATUS_IS_OK(status)) {
3324 if (!NT_STATUS_IS_OK(result)) {
3325 d_fprintf(stderr, _("Couldn't find group %s\n"),
3331 if (rids.count != 1) {
3332 d_fprintf(stderr, _("Couldn't find group %s\n"),
3334 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3336 if (rid_types.count != 1) {
3337 d_fprintf(stderr, _("Couldn't find group %s\n"),
3339 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3343 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3344 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3345 domain_sid, &domain_pol,
3349 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3350 return rpc_list_alias_members(c, pipe_hnd, cli, mem_ctx, &domain_pol,
3354 return NT_STATUS_NO_SUCH_GROUP;
3357 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3359 if (argc != 1 || c->display_usage) {
3360 return rpc_group_usage(c, argc, argv);
3363 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3364 rpc_group_members_internals,
3368 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3370 NET_API_STATUS status;
3371 struct GROUP_INFO_0 g0;
3375 d_printf(_("Usage:\n"));
3376 d_printf("net rpc group rename group newname\n");
3380 g0.grpi0_name = argv[1];
3382 status = NetGroupSetInfo(c->opt_host,
3389 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3390 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3398 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3400 if (argc != 2 || c->display_usage) {
3401 return rpc_group_usage(c, argc, argv);
3404 return rpc_group_rename_internals(c, argc, argv);
3408 * 'net rpc group' entrypoint.
3409 * @param argc Standard main() style argc.
3410 * @param argv Standard main() style argv. Initial components are already
3414 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3416 NET_API_STATUS status;
3418 struct functable func[] = {
3423 N_("Create specified group"),
3424 N_("net rpc group add\n"
3425 " Create specified group")
3431 N_("Delete specified group"),
3432 N_("net rpc group delete\n"
3433 " Delete specified group")
3439 N_("Add member to group"),
3440 N_("net rpc group addmem\n"
3441 " Add member to group")
3447 N_("Remove member from group"),
3448 N_("net rpc group delmem\n"
3449 " Remove member from group")
3456 N_("net rpc group list\n"
3463 N_("List group members"),
3464 N_("net rpc group members\n"
3465 " List group members")
3472 N_("net rpc group rename\n"
3475 {NULL, NULL, 0, NULL, NULL}
3478 status = libnetapi_net_init(&c->netapi_ctx);
3482 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3483 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3484 if (c->opt_kerberos) {
3485 libnetapi_set_use_kerberos(c->netapi_ctx);
3489 if (c->display_usage) {
3490 d_printf(_("Usage:\n"));
3491 d_printf(_("net rpc group\n"
3492 " Alias for net rpc group list global "
3493 "local builtin\n"));
3494 net_display_usage_from_functable(func);
3498 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3499 rpc_group_list_internals,
3503 return net_run_function(c, argc, argv, "net rpc group", func);
3506 /****************************************************************************/
3508 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3510 return net_share_usage(c, argc, argv);
3514 * Add a share on a remote RPC server.
3516 * @param argc Standard main() style argc.
3517 * @param argv Standard main() style argv. Initial components are already
3520 * @return A shell status integer (0 for success).
3523 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3525 NET_API_STATUS status;
3528 uint32_t type = STYPE_DISKTREE; /* only allow disk shares to be added */
3529 uint32_t num_users=0, perms=0;
3530 char *password=NULL; /* don't allow a share password */
3531 struct SHARE_INFO_2 i2;
3532 uint32_t parm_error = 0;
3534 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3535 return rpc_share_usage(c, argc, argv);
3538 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3542 path = strchr(sharename, '=');
3549 i2.shi2_netname = sharename;
3550 i2.shi2_type = type;
3551 i2.shi2_remark = c->opt_comment;
3552 i2.shi2_permissions = perms;
3553 i2.shi2_max_uses = c->opt_maxusers;
3554 i2.shi2_current_uses = num_users;
3555 i2.shi2_path = path;
3556 i2.shi2_passwd = password;
3558 status = NetShareAdd(c->opt_host,
3563 printf(_("NetShareAdd failed with: %s\n"),
3564 libnetapi_get_error_string(c->netapi_ctx, status));
3571 * Delete a share on a remote RPC server.
3573 * @param domain_sid The domain sid acquired from the remote server.
3574 * @param argc Standard main() style argc.
3575 * @param argv Standard main() style argv. Initial components are already
3578 * @return A shell status integer (0 for success).
3580 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3582 if (argc < 1 || c->display_usage) {
3583 return rpc_share_usage(c, argc, argv);
3586 return NetShareDel(c->opt_host, argv[0], 0);
3590 * Formatted print of share info
3592 * @param r pointer to SHARE_INFO_1 to format
3595 static void display_share_info_1(struct net_context *c,
3596 struct SHARE_INFO_1 *r)
3598 if (c->opt_long_list_entries) {
3599 d_printf("%-12s %-8.8s %-50s\n",
3601 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3604 d_printf("%s\n", r->shi1_netname);
3608 static WERROR get_share_info(struct net_context *c,
3609 struct rpc_pipe_client *pipe_hnd,
3610 TALLOC_CTX *mem_ctx,
3614 struct srvsvc_NetShareInfoCtr *info_ctr)
3618 union srvsvc_NetShareInfo info;
3619 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3621 /* no specific share requested, enumerate all */
3624 uint32_t preferred_len = 0xffffffff;
3625 uint32_t total_entries = 0;
3626 uint32_t resume_handle = 0;
3628 info_ctr->level = level;
3630 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3637 if (!NT_STATUS_IS_OK(status)) {
3638 return ntstatus_to_werror(status);
3643 /* request just one share */
3644 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3651 if (!NT_STATUS_IS_OK(status)) {
3652 result = ntstatus_to_werror(status);
3656 if (!W_ERROR_IS_OK(result)) {
3661 ZERO_STRUCTP(info_ctr);
3663 info_ctr->level = level;
3668 struct srvsvc_NetShareCtr1 *ctr1;
3670 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3671 W_ERROR_HAVE_NO_MEMORY(ctr1);
3674 ctr1->array = info.info1;
3676 info_ctr->ctr.ctr1 = ctr1;
3682 struct srvsvc_NetShareCtr2 *ctr2;
3684 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3685 W_ERROR_HAVE_NO_MEMORY(ctr2);
3688 ctr2->array = info.info2;
3690 info_ctr->ctr.ctr2 = ctr2;
3696 struct srvsvc_NetShareCtr502 *ctr502;
3698 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3699 W_ERROR_HAVE_NO_MEMORY(ctr502);
3702 ctr502->array = info.info502;
3704 info_ctr->ctr.ctr502 = ctr502;
3714 * 'net rpc share list' entrypoint.
3715 * @param argc Standard main() style argc.
3716 * @param argv Standard main() style argv. Initial components are already
3719 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3721 NET_API_STATUS status;
3722 struct SHARE_INFO_1 *i1 = NULL;
3723 uint32_t entries_read = 0;
3724 uint32_t total_entries = 0;
3725 uint32_t resume_handle = 0;
3726 uint32_t i, level = 1;
3728 if (c->display_usage) {
3730 "net rpc share list\n"
3733 _("List shares on remote server"));
3737 status = NetShareEnum(c->opt_host,
3739 (uint8_t **)(void *)&i1,
3748 /* Display results */
3750 if (c->opt_long_list_entries) {
3752 "\nEnumerating shared resources (exports) on remote server:\n\n"
3753 "\nShare name Type Description\n"
3754 "---------- ---- -----------\n"));
3756 for (i = 0; i < entries_read; i++)
3757 display_share_info_1(c, &i1[i]);
3762 static bool check_share_availability(struct cli_state *cli, const char *netname)
3766 status = cli_tree_connect(cli, netname, "A:", NULL);
3767 if (!NT_STATUS_IS_OK(status)) {
3768 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3772 status = cli_tdis(cli);
3773 if (!NT_STATUS_IS_OK(status)) {
3774 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3781 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3782 const char *netname, uint32_t type)
3784 /* only support disk shares */
3785 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3786 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3791 /* skip builtin shares */
3792 /* FIXME: should print$ be added too ? */
3793 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3794 strequal(netname,"global"))
3797 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3798 printf(_("excluding [%s]\n"), netname);
3802 return check_share_availability(cli, netname);
3806 * Migrate shares from a remote RPC server to the local RPC server.
3808 * All parameters are provided by the run_rpc_command function, except for
3809 * argc, argv which are passed through.
3811 * @param domain_sid The domain sid acquired from the remote server.
3812 * @param cli A cli_state connected to the server.
3813 * @param mem_ctx Talloc context, destroyed on completion of the function.
3814 * @param argc Standard main() style argc.
3815 * @param argv Standard main() style argv. Initial components are already
3818 * @return Normal NTSTATUS return.
3821 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3822 const struct dom_sid *domain_sid,
3823 const char *domain_name,
3824 struct cli_state *cli,
3825 struct rpc_pipe_client *pipe_hnd,
3826 TALLOC_CTX *mem_ctx,
3831 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3832 struct srvsvc_NetShareInfoCtr ctr_src;
3834 struct rpc_pipe_client *srvsvc_pipe = NULL;
3835 struct cli_state *cli_dst = NULL;
3836 uint32_t level = 502; /* includes secdesc */
3837 uint32_t parm_error = 0;
3838 struct dcerpc_binding_handle *b;
3840 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3842 if (!W_ERROR_IS_OK(result))
3845 /* connect destination PI_SRVSVC */
3846 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3848 if (!NT_STATUS_IS_OK(nt_status))
3851 b = srvsvc_pipe->binding_handle;
3853 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3855 union srvsvc_NetShareInfo info;
3856 struct srvsvc_NetShareInfo502 info502 =
3857 ctr_src.ctr.ctr502->array[i];
3859 /* reset error-code */
3860 nt_status = NT_STATUS_UNSUCCESSFUL;
3862 if (!check_share_sanity(c, cli, info502.name, info502.type))
3865 /* finally add the share on the dst server */
3867 printf(_("migrating: [%s], path: %s, comment: %s, without "
3869 info502.name, info502.path, info502.comment);
3871 info.info502 = &info502;
3873 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3874 srvsvc_pipe->desthost,
3879 if (!NT_STATUS_IS_OK(nt_status)) {
3880 printf(_("cannot add share: %s\n"),
3881 nt_errstr(nt_status));
3884 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3885 printf(_(" [%s] does already exist\n"),
3890 if (!W_ERROR_IS_OK(result)) {
3891 nt_status = werror_to_ntstatus(result);
3892 printf(_("cannot add share: %s\n"),
3893 win_errstr(result));
3899 nt_status = NT_STATUS_OK;
3903 cli_shutdown(cli_dst);
3911 * Migrate shares from a RPC server to another.
3913 * @param argc Standard main() style argc.
3914 * @param argv Standard main() style argv. Initial components are already
3917 * @return A shell status integer (0 for success).
3919 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3922 if (c->display_usage) {
3924 "net rpc share migrate shares\n"
3927 _("Migrate shares to local server"));
3932 printf(_("no server to migrate\n"));
3936 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3937 rpc_share_migrate_shares_internals,
3944 * @param f file_info
3945 * @param mask current search mask
3946 * @param state arg-pointer
3949 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3950 const char *mask, void *state)
3952 static NTSTATUS nt_status;
3953 static struct copy_clistate *local_state;
3954 static fstring filename, new_mask;
3957 struct net_context *c;
3959 local_state = (struct copy_clistate *)state;
3960 nt_status = NT_STATUS_UNSUCCESSFUL;
3964 if (strequal(f->name, ".") || strequal(f->name, ".."))
3965 return NT_STATUS_OK;
3967 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3970 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3972 DEBUG(3,("got dir: %s\n", f->name));
3974 fstrcpy(dir, local_state->cwd);
3976 fstrcat(dir, f->name);
3978 switch (net_mode_share)
3980 case NET_MODE_SHARE_MIGRATE:
3981 /* create that directory */
3982 nt_status = net_copy_file(c, local_state->mem_ctx,
3983 local_state->cli_share_src,
3984 local_state->cli_share_dst,
3986 c->opt_acls? true : false,
3987 c->opt_attrs? true : false,
3988 c->opt_timestamps? true:false,
3992 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3993 return NT_STATUS_INTERNAL_ERROR;
3996 if (!NT_STATUS_IS_OK(nt_status)) {
3997 printf(_("could not handle dir %s: %s\n"),
3998 dir, nt_errstr(nt_status));
4002 /* search below that directory */
4003 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
4004 return NT_STATUS_NO_MEMORY;
4006 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
4007 return NT_STATUS_NO_MEMORY;
4010 old_dir = local_state->cwd;
4011 local_state->cwd = dir;
4012 nt_status = sync_files(local_state, new_mask);
4013 if (!NT_STATUS_IS_OK(nt_status)) {
4014 printf(_("could not handle files\n"));
4016 local_state->cwd = old_dir;
4023 fstrcpy(filename, local_state->cwd);
4024 fstrcat(filename, "\\");
4025 fstrcat(filename, f->name);
4027 DEBUG(3,("got file: %s\n", filename));
4029 switch (net_mode_share)
4031 case NET_MODE_SHARE_MIGRATE:
4032 nt_status = net_copy_file(c, local_state->mem_ctx,
4033 local_state->cli_share_src,
4034 local_state->cli_share_dst,
4036 c->opt_acls? true : false,
4037 c->opt_attrs? true : false,
4038 c->opt_timestamps? true: false,
4042 d_fprintf(stderr, _("Unsupported file mode %d\n"),
4044 return NT_STATUS_INTERNAL_ERROR;
4047 if (!NT_STATUS_IS_OK(nt_status))
4048 printf(_("could not handle file %s: %s\n"),
4049 filename, nt_errstr(nt_status));
4054 * sync files, can be called recursivly to list files
4055 * and then call copy_fn for each file
4057 * @param cp_clistate pointer to the copy_clistate we work with
4058 * @param mask the current search mask
4060 * @return Boolean result
4062 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
4064 struct cli_state *targetcli;
4065 char *targetpath = NULL;
4068 DEBUG(3,("calling cli_list with mask: %s\n", mask));
4070 status = cli_resolve_path(talloc_tos(), "", NULL,
4071 cp_clistate->cli_share_src,
4072 mask, &targetcli, &targetpath);
4073 if (!NT_STATUS_IS_OK(status)) {
4074 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
4076 mask, nt_errstr(status));
4080 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
4081 copy_fn, cp_clistate);
4082 if (!NT_STATUS_IS_OK(status)) {
4083 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
4084 mask, nt_errstr(status));
4092 * Set the top level directory permissions before we do any further copies.
4093 * Should set up ACL inheritance.
4096 bool copy_top_level_perms(struct net_context *c,
4097 struct copy_clistate *cp_clistate,
4098 const char *sharename)
4100 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4102 switch (net_mode_share) {
4103 case NET_MODE_SHARE_MIGRATE:
4104 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
4105 nt_status = net_copy_fileattr(c,
4106 cp_clistate->mem_ctx,
4107 cp_clistate->cli_share_src,
4108 cp_clistate->cli_share_dst,
4110 c->opt_acls? true : false,
4111 c->opt_attrs? true : false,
4112 c->opt_timestamps? true: false,
4116 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4120 if (!NT_STATUS_IS_OK(nt_status)) {
4121 printf(_("Could handle directory attributes for top level "
4122 "directory of share %s. Error %s\n"),
4123 sharename, nt_errstr(nt_status));
4131 * Sync all files inside a remote share to another share (over smb).
4133 * All parameters are provided by the run_rpc_command function, except for
4134 * argc, argv which are passed through.
4136 * @param domain_sid The domain sid acquired from the remote server.
4137 * @param cli A cli_state connected to the server.
4138 * @param mem_ctx Talloc context, destroyed on completion of the function.
4139 * @param argc Standard main() style argc.
4140 * @param argv Standard main() style argv. Initial components are already
4143 * @return Normal NTSTATUS return.
4146 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
4147 const struct dom_sid *domain_sid,
4148 const char *domain_name,
4149 struct cli_state *cli,
4150 struct rpc_pipe_client *pipe_hnd,
4151 TALLOC_CTX *mem_ctx,
4156 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4157 struct srvsvc_NetShareInfoCtr ctr_src;
4159 uint32_t level = 502;
4160 struct copy_clistate cp_clistate;
4161 bool got_src_share = false;
4162 bool got_dst_share = false;
4163 const char *mask = "\\*";
4166 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
4168 nt_status = NT_STATUS_NO_MEMORY;
4172 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4175 if (!W_ERROR_IS_OK(result))
4178 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4180 struct srvsvc_NetShareInfo502 info502 =
4181 ctr_src.ctr.ctr502->array[i];
4183 if (!check_share_sanity(c, cli, info502.name, info502.type))
4186 /* one might not want to mirror whole discs :) */
4187 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
4188 d_printf(_("skipping [%s]: builtin/hidden share\n"),
4193 switch (net_mode_share)
4195 case NET_MODE_SHARE_MIGRATE:
4199 d_fprintf(stderr, _("Unsupported mode %d\n"),
4203 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4206 c->opt_acls ? _("including") : _("without"),
4207 c->opt_attrs ? _("including") : _("without"),
4208 c->opt_timestamps ? _("(preserving timestamps)") : "");
4210 cp_clistate.mem_ctx = mem_ctx;
4211 cp_clistate.cli_share_src = NULL;
4212 cp_clistate.cli_share_dst = NULL;
4213 cp_clistate.cwd = NULL;
4214 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4217 /* open share source */
4218 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4219 smbXcli_conn_remote_sockaddr(cli->conn),
4220 smbXcli_conn_remote_name(cli->conn),
4221 info502.name, "A:");
4222 if (!NT_STATUS_IS_OK(nt_status))
4225 got_src_share = true;
4227 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4228 /* open share destination */
4229 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4230 NULL, dst, info502.name, "A:");
4231 if (!NT_STATUS_IS_OK(nt_status))
4234 got_dst_share = true;
4237 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4238 d_fprintf(stderr, _("Could not handle the top level "
4239 "directory permissions for the "
4240 "share: %s\n"), info502.name);
4241 nt_status = NT_STATUS_UNSUCCESSFUL;
4245 nt_status = sync_files(&cp_clistate, mask);
4246 if (!NT_STATUS_IS_OK(nt_status)) {
4247 d_fprintf(stderr, _("could not handle files for share: "
4248 "%s\n"), info502.name);
4253 nt_status = NT_STATUS_OK;
4258 cli_shutdown(cp_clistate.cli_share_src);
4261 cli_shutdown(cp_clistate.cli_share_dst);
4268 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4270 if (c->display_usage) {
4272 "net share migrate files\n"
4275 _("Migrate files to local server"));
4280 d_printf(_("no server to migrate\n"));
4284 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4285 rpc_share_migrate_files_internals,
4290 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4292 * All parameters are provided by the run_rpc_command function, except for
4293 * argc, argv which are passed through.
4295 * @param domain_sid The domain sid acquired from the remote server.
4296 * @param cli A cli_state connected to the server.
4297 * @param mem_ctx Talloc context, destroyed on completion of the function.
4298 * @param argc Standard main() style argc.
4299 * @param argv Standard main() style argv. Initial components are already
4302 * @return Normal NTSTATUS return.
4305 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4306 const struct dom_sid *domain_sid,
4307 const char *domain_name,
4308 struct cli_state *cli,
4309 struct rpc_pipe_client *pipe_hnd,
4310 TALLOC_CTX *mem_ctx,
4315 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4316 struct srvsvc_NetShareInfoCtr ctr_src;
4317 union srvsvc_NetShareInfo info;
4319 struct rpc_pipe_client *srvsvc_pipe = NULL;
4320 struct cli_state *cli_dst = NULL;
4321 uint32_t level = 502; /* includes secdesc */
4322 uint32_t parm_error = 0;
4323 struct dcerpc_binding_handle *b;
4325 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4328 if (!W_ERROR_IS_OK(result))
4331 /* connect destination PI_SRVSVC */
4332 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4334 if (!NT_STATUS_IS_OK(nt_status))
4337 b = srvsvc_pipe->binding_handle;
4339 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4341 struct srvsvc_NetShareInfo502 info502 =
4342 ctr_src.ctr.ctr502->array[i];
4344 /* reset error-code */
4345 nt_status = NT_STATUS_UNSUCCESSFUL;
4347 if (!check_share_sanity(c, cli, info502.name, info502.type))
4350 printf(_("migrating: [%s], path: %s, comment: %s, including "
4352 info502.name, info502.path, info502.comment);
4355 display_sec_desc(info502.sd_buf.sd);
4357 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4358 info.info502 = &info502;
4360 /* finally modify the share on the dst server */
4361 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4362 srvsvc_pipe->desthost,
4368 if (!NT_STATUS_IS_OK(nt_status)) {
4369 printf(_("cannot set share-acl: %s\n"),
4370 nt_errstr(nt_status));
4373 if (!W_ERROR_IS_OK(result)) {
4374 nt_status = werror_to_ntstatus(result);
4375 printf(_("cannot set share-acl: %s\n"),
4376 win_errstr(result));
4382 nt_status = NT_STATUS_OK;
4386 cli_shutdown(cli_dst);
4394 * Migrate share-acls from a RPC server to another.
4396 * @param argc Standard main() style argc.
4397 * @param argv Standard main() style argv. Initial components are already
4400 * @return A shell status integer (0 for success).
4402 static int rpc_share_migrate_security(struct net_context *c, int argc,
4405 if (c->display_usage) {
4407 "net rpc share migrate security\n"
4410 _("Migrate share-acls to local server"));
4415 d_printf(_("no server to migrate\n"));
4419 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4420 rpc_share_migrate_security_internals,
4425 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4426 * from one server to another.
4428 * @param argc Standard main() style argc.
4429 * @param argv Standard main() style argv. Initial components are already
4432 * @return A shell status integer (0 for success).
4435 static int rpc_share_migrate_all(struct net_context *c, int argc,
4440 if (c->display_usage) {
4442 "net rpc share migrate all\n"
4445 _("Migrates shares including all share settings"));
4450 d_printf(_("no server to migrate\n"));
4454 /* order is important. we don't want to be locked out by the share-acl
4455 * before copying files - gd */
4457 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4458 rpc_share_migrate_shares_internals, argc, argv);
4462 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4463 rpc_share_migrate_files_internals, argc, argv);
4467 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4468 rpc_share_migrate_security_internals, argc,
4474 * 'net rpc share migrate' entrypoint.
4475 * @param argc Standard main() style argc.
4476 * @param argv Standard main() style argv. Initial components are already
4479 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4482 struct functable func[] = {
4485 rpc_share_migrate_all,
4487 N_("Migrate shares from remote to local server"),
4488 N_("net rpc share migrate all\n"
4489 " Migrate shares from remote to local server")
4493 rpc_share_migrate_files,
4495 N_("Migrate files from remote to local server"),
4496 N_("net rpc share migrate files\n"
4497 " Migrate files from remote to local server")
4501 rpc_share_migrate_security,
4503 N_("Migrate share-ACLs from remote to local server"),
4504 N_("net rpc share migrate security\n"
4505 " Migrate share-ACLs from remote to local server")
4509 rpc_share_migrate_shares,
4511 N_("Migrate shares from remote to local server"),
4512 N_("net rpc share migrate shares\n"
4513 " Migrate shares from remote to local server")
4515 {NULL, NULL, 0, NULL, NULL}
4518 net_mode_share = NET_MODE_SHARE_MIGRATE;
4520 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4525 uint32_t num_members;
4526 struct dom_sid *members;
4529 static int num_server_aliases;
4530 static struct full_alias *server_aliases;
4533 * Add an alias to the static list.
4535 static void push_alias(struct full_alias *alias)
4539 if (server_aliases == NULL) {
4540 server_aliases = talloc_array(NULL, struct full_alias, 100);
4541 if (server_aliases == NULL) {
4542 smb_panic("talloc_array failed");
4546 array_size = talloc_array_length(server_aliases);
4547 if (array_size == num_server_aliases) {
4548 server_aliases = talloc_realloc(NULL, server_aliases,
4549 struct full_alias, array_size + 100);
4550 if (server_aliases == NULL) {
4551 smb_panic("talloc_realloc failed");
4555 server_aliases[num_server_aliases] = *alias;
4556 num_server_aliases += 1;
4560 * For a specific domain on the server, fetch all the aliases
4561 * and their members. Add all of them to the server_aliases.
4564 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4565 TALLOC_CTX *mem_ctx,
4566 struct policy_handle *connect_pol,
4567 const struct dom_sid *domain_sid)
4569 uint32_t start_idx, max_entries, num_entries, i;
4570 struct samr_SamArray *groups = NULL;
4571 NTSTATUS result, status;
4572 struct policy_handle domain_pol;
4573 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4575 /* Get domain policy handle */
4577 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4579 MAXIMUM_ALLOWED_ACCESS,
4580 discard_const_p(struct dom_sid2, domain_sid),
4583 if (!NT_STATUS_IS_OK(status)) {
4586 if (!NT_STATUS_IS_OK(result)) {
4594 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4601 if (!NT_STATUS_IS_OK(status)) {
4604 for (i = 0; i < num_entries; i++) {
4606 struct policy_handle alias_pol;
4607 struct full_alias alias;
4608 struct lsa_SidArray sid_array;
4612 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4614 MAXIMUM_ALLOWED_ACCESS,
4615 groups->entries[i].idx,
4618 if (!NT_STATUS_IS_OK(status)) {
4621 if (!NT_STATUS_IS_OK(_result)) {
4626 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4630 if (!NT_STATUS_IS_OK(status)) {
4633 if (!NT_STATUS_IS_OK(_result)) {
4638 alias.num_members = sid_array.num_sids;
4640 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4641 if (!NT_STATUS_IS_OK(status)) {
4644 if (!NT_STATUS_IS_OK(_result)) {
4649 alias.members = NULL;
4651 if (alias.num_members > 0) {
4652 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4654 for (j = 0; j < alias.num_members; j++)
4655 sid_copy(&alias.members[j],
4656 sid_array.sids[j].sid);
4659 sid_compose(&alias.sid, domain_sid,
4660 groups->entries[i].idx);
4664 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4666 status = NT_STATUS_OK;
4669 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4675 * Dump server_aliases as names for debugging purposes.
4678 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4679 const struct dom_sid *domain_sid,
4680 const char *domain_name,
4681 struct cli_state *cli,
4682 struct rpc_pipe_client *pipe_hnd,
4683 TALLOC_CTX *mem_ctx,
4689 struct policy_handle lsa_pol;
4690 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4692 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4693 SEC_FLAG_MAXIMUM_ALLOWED,
4695 if (!NT_STATUS_IS_OK(result))
4698 for (i=0; i<num_server_aliases; i++) {
4701 enum lsa_SidType *types;
4704 struct full_alias *alias = &server_aliases[i];
4706 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4708 &domains, &names, &types);
4709 if (!NT_STATUS_IS_OK(result))
4712 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4714 if (alias->num_members == 0) {
4719 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4722 &domains, &names, &types);
4724 if (!NT_STATUS_IS_OK(result) &&
4725 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4728 for (j=0; j<alias->num_members; j++)
4729 DEBUG(1, ("%s\\%s (%d); ",
4730 domains[j] ? domains[j] : "*unknown*",
4731 names[j] ? names[j] : "*unknown*",types[j]));
4735 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4737 return NT_STATUS_OK;
4741 * Fetch a list of all server aliases and their members into
4745 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4746 const struct dom_sid *domain_sid,
4747 const char *domain_name,
4748 struct cli_state *cli,
4749 struct rpc_pipe_client *pipe_hnd,
4750 TALLOC_CTX *mem_ctx,
4754 NTSTATUS result, status;
4755 struct policy_handle connect_pol;
4756 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4758 status = dcerpc_samr_Connect2(b, mem_ctx,
4760 MAXIMUM_ALLOWED_ACCESS,
4763 if (!NT_STATUS_IS_OK(status)) {
4766 if (!NT_STATUS_IS_OK(result)) {
4771 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4772 &global_sid_Builtin);
4773 if (!NT_STATUS_IS_OK(status)) {
4777 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4780 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4785 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4787 token->num_sids = 4;
4789 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4790 d_fprintf(stderr, "malloc %s\n",_("failed"));
4791 token->num_sids = 0;
4795 token->sids[0] = *user_sid;
4796 sid_copy(&token->sids[1], &global_sid_World);
4797 sid_copy(&token->sids[2], &global_sid_Network);
4798 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4801 static void free_user_token(struct security_token *token)
4803 SAFE_FREE(token->sids);
4806 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4808 if (security_token_has_sid(token, sid))
4811 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4816 sid_copy(&token->sids[token->num_sids], sid);
4818 token->num_sids += 1;
4823 struct security_token token;
4826 static void dump_user_token(struct user_token *token)
4830 d_printf("%s\n", token->name);
4832 for (i=0; i<token->token.num_sids; i++) {
4833 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4837 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4841 for (i=0; i<alias->num_members; i++) {
4842 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4849 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4853 for (i=0; i<num_server_aliases; i++) {
4854 if (is_alias_member(&sid, &server_aliases[i]))
4855 add_sid_to_token(token, &server_aliases[i].sid);
4860 * We got a user token with all the SIDs we can know about without asking the
4861 * server directly. These are the user and domain group sids. All of these can
4862 * be members of aliases. So scan the list of aliases for each of the SIDs and
4863 * add them to the token.
4866 static void collect_alias_memberships(struct security_token *token)
4868 int num_global_sids = token->num_sids;
4871 for (i=0; i<num_global_sids; i++) {
4872 collect_sid_memberships(token, token->sids[i]);
4876 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4878 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4879 enum wbcSidType type;
4881 struct wbcDomainSid wsid;
4882 char sid_str[WBC_SID_STRING_BUFLEN];
4883 struct dom_sid user_sid;
4884 uint32_t num_groups;
4885 gid_t *groups = NULL;
4888 fstr_sprintf(full_name, "%s%c%s",
4889 domain, *lp_winbind_separator(), user);
4891 /* First let's find out the user sid */
4893 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4895 if (!WBC_ERROR_IS_OK(wbc_status)) {
4896 DEBUG(1, ("winbind could not find %s: %s\n",
4897 full_name, wbcErrorString(wbc_status)));
4901 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4903 if (type != WBC_SID_NAME_USER) {
4904 DEBUG(1, ("%s is not a user\n", full_name));
4908 if (!string_to_sid(&user_sid, sid_str)) {
4909 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4913 init_user_token(token, &user_sid);
4915 /* And now the groups winbind knows about */
4917 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4918 if (!WBC_ERROR_IS_OK(wbc_status)) {
4919 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4920 full_name, wbcErrorString(wbc_status)));
4924 for (i = 0; i < num_groups; i++) {
4925 gid_t gid = groups[i];
4929 wbc_status = wbcGidToSid(gid, &wsid);
4930 if (!WBC_ERROR_IS_OK(wbc_status)) {
4931 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4932 (unsigned int)gid, wbcErrorString(wbc_status)));
4933 wbcFreeMemory(groups);
4937 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4939 DEBUG(3, (" %s\n", sid_str));
4941 ok = string_to_sid(&sid, sid_str);
4943 DEBUG(1, ("Failed to convert string to SID\n"));
4944 wbcFreeMemory(groups);
4947 add_sid_to_token(token, &sid);
4949 wbcFreeMemory(groups);
4955 * Get a list of all user tokens we want to look at
4958 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4959 struct user_token **user_tokens)
4961 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4962 uint32_t i, num_users;
4964 struct user_token *result;
4965 TALLOC_CTX *frame = NULL;
4967 if (lp_winbind_use_default_domain() &&
4968 (c->opt_target_workgroup == NULL)) {
4969 d_fprintf(stderr, _("winbind use default domain = yes set, "
4970 "please specify a workgroup\n"));
4974 /* Send request to winbind daemon */
4976 wbc_status = wbcListUsers(NULL, &num_users, &users);
4977 if (!WBC_ERROR_IS_OK(wbc_status)) {
4978 DEBUG(1, (_("winbind could not list users: %s\n"),
4979 wbcErrorString(wbc_status)));
4983 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4985 if (result == NULL) {
4986 DEBUG(1, ("Could not malloc sid array\n"));
4987 wbcFreeMemory(users);
4991 frame = talloc_stackframe();
4992 for (i=0; i < num_users; i++) {
4993 fstring domain, user;
4996 fstrcpy(result[i].name, users[i]);
4998 p = strchr(users[i], *lp_winbind_separator());
5000 DEBUG(3, ("%s\n", users[i]));
5003 fstrcpy(domain, c->opt_target_workgroup);
5004 fstrcpy(user, users[i]);
5007 fstrcpy(domain, users[i]);
5008 if (!strupper_m(domain)) {
5009 DEBUG(1, ("strupper_m %s failed\n", domain));
5010 wbcFreeMemory(users);
5016 get_user_sids(domain, user, &(result[i].token));
5019 wbcFreeMemory(users);
5021 *num_tokens = num_users;
5022 *user_tokens = result;
5027 static bool get_user_tokens_from_file(FILE *f,
5029 struct user_token **tokens)
5031 struct user_token *token = NULL;
5036 if (fgets(line, sizeof(line)-1, f) == NULL) {
5040 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
5041 line[strlen(line)-1] = '\0';
5044 if (line[0] == ' ') {
5048 if(!string_to_sid(&sid, &line[1])) {
5049 DEBUG(1,("get_user_tokens_from_file: Could "
5050 "not convert sid %s \n",&line[1]));
5054 if (token == NULL) {
5055 DEBUG(0, ("File does not begin with username"));
5059 add_sid_to_token(&token->token, &sid);
5063 /* And a new user... */
5066 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
5067 if (*tokens == NULL) {
5068 DEBUG(0, ("Could not realloc tokens\n"));
5072 token = &((*tokens)[*num_tokens-1]);
5074 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
5077 token->token.num_sids = 0;
5078 token->token.sids = NULL;
5087 * Show the list of all users that have access to a share
5090 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
5091 struct cli_state *cli,
5092 TALLOC_CTX *mem_ctx,
5093 const char *netname,
5095 struct user_token *tokens)
5098 struct security_descriptor *share_sd = NULL;
5099 struct security_descriptor *root_sd = NULL;
5101 union srvsvc_NetShareInfo info;
5104 struct smbXcli_tcon *orig_tcon = NULL;
5105 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5107 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5114 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5115 DEBUG(1, ("Could not query secdesc for share %s\n",
5120 share_sd = info.info502->sd_buf.sd;
5121 if (share_sd == NULL) {
5122 DEBUG(1, ("Got no secdesc for share %s\n",
5126 if (cli_state_has_tcon(cli)) {
5127 orig_tcon = cli_state_save_tcon(cli);
5128 if (orig_tcon == NULL) {
5133 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", NULL))) {
5134 cli_state_restore_tcon(cli, orig_tcon);
5138 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
5139 FILE_SHARE_READ|FILE_SHARE_WRITE,
5140 FILE_OPEN, 0x0, 0x0, &fnum, NULL))) {
5141 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
5144 for (i=0; i<num_tokens; i++) {
5145 uint32_t acc_granted;
5147 if (share_sd != NULL) {
5148 status = se_access_check(share_sd, &tokens[i].token,
5151 if (!NT_STATUS_IS_OK(status)) {
5152 DEBUG(1, ("Could not check share_sd for "
5159 if (root_sd == NULL) {
5160 d_printf(" %s\n", tokens[i].name);
5164 status = se_access_check(root_sd, &tokens[i].token,
5166 if (!NT_STATUS_IS_OK(status)) {
5167 DEBUG(1, ("Could not check root_sd for user %s\n",
5171 d_printf(" %s\n", tokens[i].name);
5174 if (fnum != (uint16_t)-1)
5175 cli_close(cli, fnum);
5177 cli_state_restore_tcon(cli, orig_tcon);
5183 * List shares on a remote RPC server, including the security descriptors.
5185 * All parameters are provided by the run_rpc_command function, except for
5186 * argc, argv which are passed through.
5188 * @param domain_sid The domain sid acquired from the remote server.
5189 * @param cli A cli_state connected to the server.
5190 * @param mem_ctx Talloc context, destroyed on completion of the function.
5191 * @param argc Standard main() style argc.
5192 * @param argv Standard main() style argv. Initial components are already
5195 * @return Normal NTSTATUS return.
5198 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
5199 const struct dom_sid *domain_sid,
5200 const char *domain_name,
5201 struct cli_state *cli,
5202 struct rpc_pipe_client *pipe_hnd,
5203 TALLOC_CTX *mem_ctx,
5209 NTSTATUS nt_status = NT_STATUS_OK;
5210 uint32_t total_entries = 0;
5211 uint32_t resume_handle = 0;
5212 uint32_t preferred_len = 0xffffffff;
5214 struct dcerpc_binding_handle *b = NULL;
5215 struct srvsvc_NetShareInfoCtr info_ctr;
5216 struct srvsvc_NetShareCtr1 ctr1;
5219 struct user_token *tokens = NULL;
5225 f = fopen(argv[0], "r");
5229 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
5230 return NT_STATUS_UNSUCCESSFUL;
5233 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5239 DEBUG(0, ("Could not read users from file\n"));
5240 return NT_STATUS_UNSUCCESSFUL;
5243 for (i=0; i<num_tokens; i++)
5244 collect_alias_memberships(&tokens[i].token);
5246 ZERO_STRUCT(info_ctr);
5250 info_ctr.ctr.ctr1 = &ctr1;
5252 b = pipe_hnd->binding_handle;
5254 /* Issue the NetShareEnum RPC call and retrieve the response */
5255 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5264 /* Was it successful? */
5265 if (!NT_STATUS_IS_OK(nt_status)) {
5266 /* Nope. Go clean up. */
5270 if (!W_ERROR_IS_OK(result)) {
5271 /* Nope. Go clean up. */
5272 nt_status = werror_to_ntstatus(result);
5276 if (total_entries == 0) {
5280 /* For each returned entry... */
5281 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5282 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5284 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5288 d_printf("%s\n", netname);
5290 show_userlist(pipe_hnd, cli, mem_ctx, netname,
5291 num_tokens, tokens);
5294 for (i=0; i<num_tokens; i++) {
5295 free_user_token(&tokens[i].token);
5298 TALLOC_FREE(server_aliases);
5303 static int rpc_share_allowedusers(struct net_context *c, int argc,
5308 if (c->display_usage) {
5310 "net rpc share allowedusers\n"
5313 _("List allowed users"));
5317 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5318 rpc_aliaslist_internals,
5323 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5329 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5330 rpc_share_allowedusers_internals,
5334 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5337 struct user_token *tokens = NULL;
5341 net_usersidlist_usage(c, argc, argv);
5345 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5346 DEBUG(0, ("Could not get the user/sid list\n"));
5350 for (i=0; i<num_tokens; i++) {
5351 dump_user_token(&tokens[i]);
5352 free_user_token(&tokens[i].token);
5359 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5361 d_printf(_("net usersidlist\n"
5362 "\tprints out a list of all users the running winbind knows\n"
5363 "\tabout, together with all their SIDs. This is used as\n"
5364 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5366 net_common_flags_usage(c, argc, argv);
5371 * 'net rpc share' entrypoint.
5372 * @param argc Standard main() style argc.
5373 * @param argv Standard main() style argv. Initial components are already
5377 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5379 NET_API_STATUS status;
5381 struct functable func[] = {
5387 N_("net rpc share add\n"
5395 N_("net rpc share delete\n"
5400 rpc_share_allowedusers,
5402 N_("Modify allowed users"),
5403 N_("net rpc share allowedusers\n"
5404 " Modify allowed users")
5410 N_("Migrate share to local server"),
5411 N_("net rpc share migrate\n"
5412 " Migrate share to local server")
5419 N_("net rpc share list\n"
5422 {NULL, NULL, 0, NULL, NULL}
5425 status = libnetapi_net_init(&c->netapi_ctx);
5429 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5430 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5431 if (c->opt_kerberos) {
5432 libnetapi_set_use_kerberos(c->netapi_ctx);
5436 if (c->display_usage) {
5441 " Alias for net rpc share list\n"));
5442 net_display_usage_from_functable(func);
5446 return rpc_share_list(c, argc, argv);
5449 return net_run_function(c, argc, argv, "net rpc share", func);
5452 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5453 TALLOC_CTX *mem_ctx,
5454 struct rpc_sh_ctx *ctx,
5455 struct rpc_pipe_client *pipe_hnd,
5456 int argc, const char **argv)
5459 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5462 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5463 TALLOC_CTX *mem_ctx,
5464 struct rpc_sh_ctx *ctx,
5465 struct rpc_pipe_client *pipe_hnd,
5466 int argc, const char **argv)
5468 NET_API_STATUS status;
5469 uint32_t parm_err = 0;
5470 struct SHARE_INFO_2 i2;
5472 if ((argc < 2) || (argc > 3)) {
5473 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5475 return NT_STATUS_INVALID_PARAMETER;
5478 i2.shi2_netname = argv[0];
5479 i2.shi2_type = STYPE_DISKTREE;
5480 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5481 i2.shi2_permissions = 0;
5482 i2.shi2_max_uses = 0;
5483 i2.shi2_current_uses = 0;
5484 i2.shi2_path = argv[1];
5485 i2.shi2_passwd = NULL;
5487 status = NetShareAdd(pipe_hnd->desthost,
5492 return werror_to_ntstatus(W_ERROR(status));
5495 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5496 TALLOC_CTX *mem_ctx,
5497 struct rpc_sh_ctx *ctx,
5498 struct rpc_pipe_client *pipe_hnd,
5499 int argc, const char **argv)
5502 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5503 return NT_STATUS_INVALID_PARAMETER;
5506 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5509 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5510 TALLOC_CTX *mem_ctx,
5511 struct rpc_sh_ctx *ctx,
5512 struct rpc_pipe_client *pipe_hnd,
5513 int argc, const char **argv)
5515 union srvsvc_NetShareInfo info;
5518 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5521 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5522 return NT_STATUS_INVALID_PARAMETER;
5525 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5531 if (!NT_STATUS_IS_OK(status)) {
5532 result = ntstatus_to_werror(status);
5535 if (!W_ERROR_IS_OK(result)) {
5539 d_printf(_("Name: %s\n"), info.info2->name);
5540 d_printf(_("Comment: %s\n"), info.info2->comment);
5541 d_printf(_("Path: %s\n"), info.info2->path);
5542 d_printf(_("Password: %s\n"), info.info2->password);
5545 return werror_to_ntstatus(result);
5548 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5549 struct rpc_sh_ctx *ctx)
5551 static struct rpc_sh_cmd cmds[] = {
5553 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5554 N_("List available shares") },
5556 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5557 N_("Add a share") },
5559 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5560 N_("Delete a share") },
5562 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5563 N_("Get information about a share") },
5565 { NULL, NULL, 0, NULL, NULL }
5571 /****************************************************************************/
5573 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5575 return net_file_usage(c, argc, argv);
5579 * Close a file on a remote RPC server.
5581 * @param argc Standard main() style argc.
5582 * @param argv Standard main() style argv. Initial components are already
5585 * @return A shell status integer (0 for success).
5587 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5589 if (argc < 1 || c->display_usage) {
5590 return rpc_file_usage(c, argc, argv);
5593 return NetFileClose(c->opt_host, atoi(argv[0]));
5597 * Formatted print of open file info
5599 * @param r struct FILE_INFO_3 contents
5602 static void display_file_info_3(struct FILE_INFO_3 *r)
5604 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5605 r->fi3_id, r->fi3_username, r->fi3_permissions,
5606 r->fi3_num_locks, r->fi3_pathname);
5610 * List files for a user on a remote RPC server.
5612 * @param argc Standard main() style argc.
5613 * @param argv Standard main() style argv. Initial components are already
5616 * @return A shell status integer (0 for success)..
5619 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5621 NET_API_STATUS status;
5622 uint32_t preferred_len = 0xffffffff, i;
5623 char *username=NULL;
5624 uint32_t total_entries = 0;
5625 uint32_t entries_read = 0;
5626 uint32_t resume_handle = 0;
5627 struct FILE_INFO_3 *i3 = NULL;
5629 if (c->display_usage) {
5630 return rpc_file_usage(c, argc, argv);
5633 /* if argc > 0, must be user command */
5635 username = smb_xstrdup(argv[0]);
5638 status = NetFileEnum(c->opt_host,
5642 (uint8_t **)(void *)&i3,
5652 /* Display results */
5655 "\nEnumerating open files on remote server:\n\n"
5656 "\nFileId Opened by Perms Locks Path"
5657 "\n------ --------- ----- ----- ---- \n"));
5658 for (i = 0; i < entries_read; i++) {
5659 display_file_info_3(&i3[i]);
5662 SAFE_FREE(username);
5667 * 'net rpc file' entrypoint.
5668 * @param argc Standard main() style argc.
5669 * @param argv Standard main() style argv. Initial components are already
5673 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5675 NET_API_STATUS status;
5677 struct functable func[] = {
5682 N_("Close opened file"),
5683 N_("net rpc file close\n"
5684 " Close opened file")
5690 N_("List files opened by user"),
5691 N_("net rpc file user\n"
5692 " List files opened by user")
5699 N_("Display information about opened file"),
5700 N_("net rpc file info\n"
5701 " Display information about opened file")
5704 {NULL, NULL, 0, NULL, NULL}
5707 status = libnetapi_net_init(&c->netapi_ctx);
5711 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5712 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5713 if (c->opt_kerberos) {
5714 libnetapi_set_use_kerberos(c->netapi_ctx);
5718 if (c->display_usage) {
5719 d_printf(_("Usage:\n"));
5720 d_printf(_("net rpc file\n"
5721 " List opened files\n"));
5722 net_display_usage_from_functable(func);
5726 return rpc_file_user(c, argc, argv);
5729 return net_run_function(c, argc, argv, "net rpc file", func);
5733 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5735 * All parameters are provided by the run_rpc_command function, except for
5736 * argc, argv which are passed through.
5738 * @param c A net_context structure.
5739 * @param domain_sid The domain sid acquired from the remote server.
5740 * @param cli A cli_state connected to the server.
5741 * @param mem_ctx Talloc context, destroyed on completion of the function.
5742 * @param argc Standard main() style argc.
5743 * @param argv Standard main() style argv. Initial components are already
5746 * @return Normal NTSTATUS return.
5749 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5750 const struct dom_sid *domain_sid,
5751 const char *domain_name,
5752 struct cli_state *cli,
5753 struct rpc_pipe_client *pipe_hnd,
5754 TALLOC_CTX *mem_ctx,
5758 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5760 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5762 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5763 if (!NT_STATUS_IS_OK(status)) {
5766 if (W_ERROR_IS_OK(result)) {
5767 d_printf(_("\nShutdown successfully aborted\n"));
5768 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5770 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5772 return werror_to_ntstatus(result);
5776 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5778 * All parameters are provided by the run_rpc_command function, except for
5779 * argc, argv which are passed through.
5781 * @param c A net_context structure.
5782 * @param domain_sid The domain sid acquired from the remote server.
5783 * @param cli A cli_state connected to the server.
5784 * @param mem_ctx Talloc context, destroyed on completion of the function.
5785 * @param argc Standard main() style argc.
5786 * @param argv Standard main() style argv. Initial components are already
5789 * @return Normal NTSTATUS return.
5792 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5793 const struct dom_sid *domain_sid,
5794 const char *domain_name,
5795 struct cli_state *cli,
5796 struct rpc_pipe_client *pipe_hnd,
5797 TALLOC_CTX *mem_ctx,
5801 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5803 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5805 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5807 if (!NT_STATUS_IS_OK(result)) {
5808 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5811 if (W_ERROR_IS_OK(werr)) {
5812 d_printf(_("\nShutdown successfully aborted\n"));
5813 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5815 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5817 return werror_to_ntstatus(werr);
5821 * ABORT the shutdown of a remote RPC server.
5823 * @param argc Standard main() style argc.
5824 * @param argv Standard main() style argv. Initial components are already
5827 * @return A shell status integer (0 for success).
5830 static int rpc_shutdown_abort(struct net_context *c, int argc,
5835 if (c->display_usage) {
5837 "net rpc abortshutdown\n"
5840 _("Abort a scheduled shutdown"));
5844 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5845 rpc_shutdown_abort_internals, argc, argv);
5850 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5852 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5853 rpc_reg_shutdown_abort_internals,
5858 * Shut down a remote RPC Server via initshutdown pipe.
5860 * All parameters are provided by the run_rpc_command function, except for
5861 * argc, argv which are passed through.
5863 * @param c A net_context structure.
5864 * @param domain_sid The domain sid acquired from the remote server.
5865 * @param cli A cli_state connected to the server.
5866 * @param mem_ctx Talloc context, destroyed on completion of the function.
5867 * @param argc Standard main() style argc.
5868 * @param argv Standard main() style argv. Initial components are already
5871 * @return Normal NTSTATUS return.
5874 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5875 const struct dom_sid *domain_sid,
5876 const char *domain_name,
5877 struct cli_state *cli,
5878 struct rpc_pipe_client *pipe_hnd,
5879 TALLOC_CTX *mem_ctx,
5883 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5885 const char *msg = N_("This machine will be shutdown shortly");
5886 uint32_t timeout = 20;
5887 struct lsa_StringLarge msg_string;
5888 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5890 if (c->opt_comment) {
5891 msg = c->opt_comment;
5893 if (c->opt_timeout) {
5894 timeout = c->opt_timeout;
5897 msg_string.string = msg;
5899 /* create an entry */
5900 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5901 &msg_string, timeout, c->opt_force, c->opt_reboot,
5903 if (!NT_STATUS_IS_OK(status)) {
5906 if (W_ERROR_IS_OK(result)) {
5907 d_printf(_("\nShutdown of remote machine succeeded\n"));
5908 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5910 DEBUG(1,("Shutdown of remote machine failed!\n"));
5912 return werror_to_ntstatus(result);
5916 * Shut down a remote RPC Server via winreg pipe.
5918 * All parameters are provided by the run_rpc_command function, except for
5919 * argc, argv which are passed through.
5921 * @param c A net_context structure.
5922 * @param domain_sid The domain sid acquired from the remote server.
5923 * @param cli A cli_state connected to the server.
5924 * @param mem_ctx Talloc context, destroyed on completion of the function.
5925 * @param argc Standard main() style argc.
5926 * @param argv Standard main() style argv. Initial components are already
5929 * @return Normal NTSTATUS return.
5932 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5933 const struct dom_sid *domain_sid,
5934 const char *domain_name,
5935 struct cli_state *cli,
5936 struct rpc_pipe_client *pipe_hnd,
5937 TALLOC_CTX *mem_ctx,
5941 const char *msg = N_("This machine will be shutdown shortly");
5942 uint32_t timeout = 20;
5943 struct lsa_StringLarge msg_string;
5946 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5948 if (c->opt_comment) {
5949 msg = c->opt_comment;
5951 msg_string.string = msg;
5953 if (c->opt_timeout) {
5954 timeout = c->opt_timeout;
5957 /* create an entry */
5958 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5959 &msg_string, timeout, c->opt_force, c->opt_reboot,
5961 if (!NT_STATUS_IS_OK(result)) {
5962 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5966 if (W_ERROR_IS_OK(werr)) {
5967 d_printf(_("\nShutdown of remote machine succeeded\n"));
5969 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5970 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5971 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5973 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5976 return werror_to_ntstatus(werr);
5980 * Shut down a remote RPC server.
5982 * @param argc Standard main() style argc.
5983 * @param argv Standard main() style argv. Initial components are already
5986 * @return A shell status integer (0 for success).
5989 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5993 if (c->display_usage) {
5995 "net rpc shutdown\n"
5998 _("Shut down a remote RPC server"));
6002 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
6003 rpc_init_shutdown_internals, argc, argv);
6006 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
6007 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
6008 rpc_reg_shutdown_internals, argc, argv);
6014 /***************************************************************************
6015 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
6016 ***************************************************************************/
6019 * Add interdomain trust account to the RPC server.
6020 * All parameters (except for argc and argv) are passed by run_rpc_command
6023 * @param c A net_context structure.
6024 * @param domain_sid The domain sid acquired from the server.
6025 * @param cli A cli_state connected to the server.
6026 * @param mem_ctx Talloc context, destroyed on completion of the function.
6027 * @param argc Standard main() style argc.
6028 * @param argv Standard main() style argv. Initial components are already
6031 * @return normal NTSTATUS return code.
6034 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
6035 const struct dom_sid *domain_sid,
6036 const char *domain_name,
6037 struct cli_state *cli,
6038 struct rpc_pipe_client *pipe_hnd,
6039 TALLOC_CTX *mem_ctx,
6043 struct policy_handle connect_pol, domain_pol, user_pol;
6044 NTSTATUS status, result;
6046 struct lsa_String lsa_acct_name;
6048 uint32_t acct_flags=0;
6050 uint32_t access_granted = 0;
6051 union samr_UserInfo info;
6052 unsigned int orig_timeout;
6053 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6054 DATA_BLOB session_key = data_blob_null;
6059 _(" net rpc trustdom add <domain_name> "
6060 "<trust password>\n"));
6061 return NT_STATUS_INVALID_PARAMETER;
6065 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6068 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
6069 return NT_STATUS_NO_MEMORY;
6072 if (!strupper_m(acct_name)) {
6073 SAFE_FREE(acct_name);
6074 return NT_STATUS_INVALID_PARAMETER;
6077 init_lsa_String(&lsa_acct_name, acct_name);
6079 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6080 if (!NT_STATUS_IS_OK(status)) {
6081 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
6082 nt_errstr(status)));
6086 /* Get samr policy handle */
6087 status = dcerpc_samr_Connect2(b, mem_ctx,
6089 MAXIMUM_ALLOWED_ACCESS,
6092 if (!NT_STATUS_IS_OK(status)) {
6095 if (!NT_STATUS_IS_OK(result)) {
6100 /* Get domain policy handle */
6101 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6103 MAXIMUM_ALLOWED_ACCESS,
6104 discard_const_p(struct dom_sid2, domain_sid),
6107 if (!NT_STATUS_IS_OK(status)) {
6110 if (!NT_STATUS_IS_OK(result)) {
6115 /* This call can take a long time - allow the server to time out.
6116 * 35 seconds should do it. */
6118 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
6120 /* Create trusting domain's account */
6121 acb_info = ACB_NORMAL;
6122 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
6123 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
6124 SAMR_USER_ACCESS_SET_PASSWORD |
6125 SAMR_USER_ACCESS_GET_ATTRIBUTES |
6126 SAMR_USER_ACCESS_SET_ATTRIBUTES;
6128 status = dcerpc_samr_CreateUser2(b, mem_ctx,
6137 if (!NT_STATUS_IS_OK(status)) {
6140 /* And restore our original timeout. */
6141 rpccli_set_timeout(pipe_hnd, orig_timeout);
6143 if (!NT_STATUS_IS_OK(result)) {
6145 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
6146 acct_name, nt_errstr(result));
6151 struct samr_CryptPassword crypt_pwd;
6153 ZERO_STRUCT(info.info23);
6155 init_samr_CryptPassword(argv[1],
6159 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
6160 SAMR_FIELD_NT_PASSWORD_PRESENT;
6161 info.info23.info.acct_flags = ACB_DOMTRUST;
6162 info.info23.password = crypt_pwd;
6164 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
6169 if (!NT_STATUS_IS_OK(status)) {
6173 if (!NT_STATUS_IS_OK(result)) {
6175 DEBUG(0,("Could not set trust account password: %s\n",
6176 nt_errstr(result)));
6182 SAFE_FREE(acct_name);
6183 data_blob_clear_free(&session_key);
6188 * Create interdomain trust account for a remote domain.
6190 * @param argc Standard argc.
6191 * @param argv Standard argv without initial components.
6193 * @return Integer status (0 means success).
6196 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
6198 if (argc > 0 && !c->display_usage) {
6199 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6200 rpc_trustdom_add_internals, argc, argv);
6204 _("net rpc trustdom add <domain_name> <trust "
6212 * Remove interdomain trust account from the RPC server.
6213 * All parameters (except for argc and argv) are passed by run_rpc_command
6216 * @param c A net_context structure.
6217 * @param domain_sid The domain sid acquired from the server.
6218 * @param cli A cli_state connected to the server.
6219 * @param mem_ctx Talloc context, destroyed on completion of the function.
6220 * @param argc Standard main() style argc.
6221 * @param argv Standard main() style argv. Initial components are already
6224 * @return normal NTSTATUS return code.
6227 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6228 const struct dom_sid *domain_sid,
6229 const char *domain_name,
6230 struct cli_state *cli,
6231 struct rpc_pipe_client *pipe_hnd,
6232 TALLOC_CTX *mem_ctx,
6236 struct policy_handle connect_pol, domain_pol, user_pol;
6237 NTSTATUS status, result;
6239 struct dom_sid trust_acct_sid;
6240 struct samr_Ids user_rids, name_types;
6241 struct lsa_String lsa_acct_name;
6242 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6247 _(" net rpc trustdom del <domain_name>\n"));
6248 return NT_STATUS_INVALID_PARAMETER;
6252 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6254 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6256 if (acct_name == NULL)
6257 return NT_STATUS_NO_MEMORY;
6259 if (!strupper_m(acct_name)) {
6260 TALLOC_FREE(acct_name);
6261 return NT_STATUS_INVALID_PARAMETER;
6264 /* Get samr policy handle */
6265 status = dcerpc_samr_Connect2(b, mem_ctx,
6267 MAXIMUM_ALLOWED_ACCESS,
6270 if (!NT_STATUS_IS_OK(status)) {
6273 if (!NT_STATUS_IS_OK(result)) {
6278 /* Get domain policy handle */
6279 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6281 MAXIMUM_ALLOWED_ACCESS,
6282 discard_const_p(struct dom_sid2, domain_sid),
6285 if (!NT_STATUS_IS_OK(status)) {
6288 if (!NT_STATUS_IS_OK(result)) {
6293 init_lsa_String(&lsa_acct_name, acct_name);
6295 status = dcerpc_samr_LookupNames(b, mem_ctx,
6302 if (!NT_STATUS_IS_OK(status)) {
6303 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6305 acct_name, nt_errstr(status));
6308 if (!NT_STATUS_IS_OK(result)) {
6310 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6312 acct_name, nt_errstr(result) );
6315 if (user_rids.count != 1) {
6316 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6319 if (name_types.count != 1) {
6320 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6324 status = dcerpc_samr_OpenUser(b, mem_ctx,
6326 MAXIMUM_ALLOWED_ACCESS,
6330 if (!NT_STATUS_IS_OK(status)) {
6331 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6333 acct_name, nt_errstr(status) );
6337 if (!NT_STATUS_IS_OK(result)) {
6339 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6341 acct_name, nt_errstr(result) );
6345 /* append the rid to the domain sid */
6346 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6350 /* remove the sid */
6352 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6356 if (!NT_STATUS_IS_OK(status)) {
6357 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6358 " on user %s failed %s\n"),
6359 acct_name, nt_errstr(status));
6362 if (!NT_STATUS_IS_OK(result)) {
6364 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6365 " on user %s failed %s\n"),
6366 acct_name, nt_errstr(result) );
6373 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6376 if (!NT_STATUS_IS_OK(status)) {
6377 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6379 acct_name, nt_errstr(status));
6383 if (!NT_STATUS_IS_OK(result)) {
6385 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6387 acct_name, nt_errstr(result) );
6391 if (!NT_STATUS_IS_OK(result)) {
6392 d_printf(_("Could not set trust account password: %s\n"),
6402 * Delete interdomain trust account for a remote domain.
6404 * @param argc Standard argc.
6405 * @param argv Standard argv without initial components.
6407 * @return Integer status (0 means success).
6410 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6412 if (argc > 0 && !c->display_usage) {
6413 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6414 rpc_trustdom_del_internals, argc, argv);
6418 _("net rpc trustdom del <domain>\n"));
6423 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6424 struct cli_state *cli,
6425 TALLOC_CTX *mem_ctx,
6426 const char *domain_name)
6428 char *dc_name = NULL;
6429 const char *buffer = NULL;
6430 struct rpc_pipe_client *netr;
6433 struct dcerpc_binding_handle *b;
6435 /* Use NetServerEnum2 */
6437 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6439 return NT_STATUS_OK;
6442 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6443 for domain %s\n", domain_name));
6445 /* Try netr_GetDcName */
6447 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
6449 if (!NT_STATUS_IS_OK(status)) {
6453 b = netr->binding_handle;
6455 status = dcerpc_netr_GetDcName(b, mem_ctx,
6462 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6466 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6467 for domain %s\n", domain_name));
6469 if (!NT_STATUS_IS_OK(status)) {
6473 return werror_to_ntstatus(result);
6477 * Establish trust relationship to a trusting domain.
6478 * Interdomain account must already be created on remote PDC.
6480 * @param c A net_context structure.
6481 * @param argc Standard argc.
6482 * @param argv Standard argv without initial components.
6484 * @return Integer status (0 means success).
6487 static int rpc_trustdom_establish(struct net_context *c, int argc,
6490 struct cli_state *cli = NULL;
6491 struct sockaddr_storage server_ss;
6492 struct rpc_pipe_client *pipe_hnd = NULL;
6493 struct policy_handle connect_hnd;
6494 TALLOC_CTX *mem_ctx;
6495 NTSTATUS nt_status, result;
6496 struct dom_sid *domain_sid;
6501 union lsa_PolicyInformation *info = NULL;
6502 struct dcerpc_binding_handle *b;
6505 * Connect to \\server\ipc$ as 'our domain' account with password
6508 if (argc != 1 || c->display_usage) {
6511 _("net rpc trustdom establish <domain_name>\n"));
6515 domain_name = smb_xstrdup(argv[0]);
6516 if (!strupper_m(domain_name)) {
6517 SAFE_FREE(domain_name);
6521 /* account name used at first is our domain's name with '$' */
6522 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6525 if (!strupper_m(acct_name)) {
6526 SAFE_FREE(domain_name);
6527 SAFE_FREE(acct_name);
6532 * opt_workgroup will be used by connection functions further,
6533 * hence it should be set to remote domain name instead of ours
6535 if (c->opt_workgroup) {
6536 c->opt_workgroup = smb_xstrdup(domain_name);
6539 c->opt_user_name = acct_name;
6541 /* find the domain controller */
6542 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6543 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6547 /* connect to ipc$ as username/password */
6548 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6549 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6551 /* Is it trusting domain account for sure ? */
6552 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6553 nt_errstr(nt_status)));
6557 /* store who we connected to */
6559 saf_store( domain_name, pdc_name );
6562 * Connect to \\server\ipc$ again (this time anonymously)
6565 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6568 if (NT_STATUS_IS_ERR(nt_status)) {
6569 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6570 domain_name, nt_errstr(nt_status)));
6574 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6575 "domain %s", domain_name))) {
6576 DEBUG(0, ("talloc_init() failed\n"));
6581 /* Make sure we're talking to a proper server */
6583 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6584 if (!NT_STATUS_IS_OK(nt_status)) {
6586 talloc_destroy(mem_ctx);
6591 * Call LsaOpenPolicy and LsaQueryInfo
6594 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6596 if (!NT_STATUS_IS_OK(nt_status)) {
6597 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6599 talloc_destroy(mem_ctx);
6603 b = pipe_hnd->binding_handle;
6605 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6607 if (NT_STATUS_IS_ERR(nt_status)) {
6608 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6609 nt_errstr(nt_status)));
6611 talloc_destroy(mem_ctx);
6615 /* Querying info level 5 */
6617 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6619 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6622 if (NT_STATUS_IS_ERR(nt_status)) {
6623 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6624 nt_errstr(nt_status)));
6626 talloc_destroy(mem_ctx);
6629 if (NT_STATUS_IS_ERR(result)) {
6630 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6631 nt_errstr(result)));
6633 talloc_destroy(mem_ctx);
6637 domain_sid = info->account_domain.sid;
6639 /* There should be actually query info level 3 (following nt serv behaviour),
6640 but I still don't know if it's _really_ necessary */
6643 * Store the password in secrets db
6646 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6647 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6649 talloc_destroy(mem_ctx);
6654 * Close the pipes and clean up
6657 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6658 if (NT_STATUS_IS_ERR(nt_status)) {
6659 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6660 nt_errstr(nt_status)));
6662 talloc_destroy(mem_ctx);
6668 talloc_destroy(mem_ctx);
6670 d_printf(_("Trust to domain %s established\n"), domain_name);
6675 * Revoke trust relationship to the remote domain.
6677 * @param c A net_context structure.
6678 * @param argc Standard argc.
6679 * @param argv Standard argv without initial components.
6681 * @return Integer status (0 means success).
6684 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6690 if (argc < 1 || c->display_usage) {
6693 _("net rpc trustdom revoke <domain_name>\n"
6694 " Revoke trust relationship\n"
6695 " domain_name\tName of domain to revoke trust\n"));
6699 /* generate upper cased domain name */
6700 domain_name = smb_xstrdup(argv[0]);
6701 if (!strupper_m(domain_name)) {
6702 SAFE_FREE(domain_name);
6706 /* delete password of the trust */
6707 if (!pdb_del_trusteddom_pw(domain_name)) {
6708 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6715 SAFE_FREE(domain_name);
6719 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6720 const struct dom_sid *domain_sid,
6721 const char *domain_name,
6722 struct cli_state *cli,
6723 struct rpc_pipe_client *pipe_hnd,
6724 TALLOC_CTX *mem_ctx,
6729 if (!sid_to_fstring(str_sid, domain_sid)) {
6730 return NT_STATUS_UNSUCCESSFUL;
6732 d_printf("%s\n", str_sid);
6733 return NT_STATUS_OK;
6736 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6740 /* convert sid into ascii string */
6741 sid_to_fstring(ascii_sid, dom_sid);
6743 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6746 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6747 TALLOC_CTX *mem_ctx,
6748 struct policy_handle *pol,
6749 struct dom_sid dom_sid,
6750 const char *trusted_dom_name)
6752 NTSTATUS nt_status, result;
6753 union lsa_TrustedDomainInfo *info = NULL;
6754 char *cleartextpwd = NULL;
6755 DATA_BLOB session_key;
6756 DATA_BLOB data = data_blob_null;
6757 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6759 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6762 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6765 if (NT_STATUS_IS_ERR(nt_status)) {
6766 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6767 nt_errstr(nt_status)));
6770 if (NT_STATUS_IS_ERR(result)) {
6772 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6773 nt_errstr(result)));
6777 data = data_blob(info->password.password->data,
6778 info->password.password->length);
6780 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6781 if (!NT_STATUS_IS_OK(nt_status)) {
6782 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6786 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6787 data_blob_free(&session_key);
6789 if (cleartextpwd == NULL) {
6790 DEBUG(0,("retrieved NULL password\n"));
6791 nt_status = NT_STATUS_UNSUCCESSFUL;
6795 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6796 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6797 nt_status = NT_STATUS_UNSUCCESSFUL;
6801 #ifdef DEBUG_PASSWORD
6802 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6803 "password: [%s]\n", trusted_dom_name,
6804 sid_string_dbg(&dom_sid), cleartextpwd));
6808 SAFE_FREE(cleartextpwd);
6809 data_blob_free(&data);
6814 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6817 /* common variables */
6818 TALLOC_CTX* mem_ctx;
6819 struct cli_state *cli = NULL;
6820 struct rpc_pipe_client *pipe_hnd = NULL;
6821 NTSTATUS nt_status, result;
6822 const char *domain_name = NULL;
6823 struct policy_handle connect_hnd;
6824 union lsa_PolicyInformation *info = NULL;
6826 /* trusted domains listing variables */
6827 unsigned int enum_ctx = 0;
6829 struct lsa_DomainList dom_list;
6831 struct dcerpc_binding_handle *b;
6833 if (c->display_usage) {
6835 "net rpc trustdom vampire\n"
6838 _("Vampire trust relationship from remote server"));
6843 * Listing trusted domains (stored in secrets.tdb, if local)
6846 mem_ctx = talloc_init("trust relationships vampire");
6849 * set domain and pdc name to local samba server (default)
6850 * or to remote one given in command line
6853 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6854 domain_name = c->opt_workgroup;
6855 c->opt_target_workgroup = c->opt_workgroup;
6857 fstrcpy(pdc_name, lp_netbios_name());
6858 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6859 c->opt_target_workgroup = domain_name;
6862 /* open \PIPE\lsarpc and open policy handle */
6863 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6864 if (!NT_STATUS_IS_OK(nt_status)) {
6865 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6866 nt_errstr(nt_status)));
6867 talloc_destroy(mem_ctx);
6871 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6873 if (!NT_STATUS_IS_OK(nt_status)) {
6874 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6875 nt_errstr(nt_status) ));
6877 talloc_destroy(mem_ctx);
6881 b = pipe_hnd->binding_handle;
6883 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6885 if (NT_STATUS_IS_ERR(nt_status)) {
6886 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6887 nt_errstr(nt_status)));
6889 talloc_destroy(mem_ctx);
6893 /* query info level 5 to obtain sid of a domain being queried */
6894 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6896 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6900 if (NT_STATUS_IS_ERR(nt_status)) {
6901 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6902 nt_errstr(nt_status)));
6904 talloc_destroy(mem_ctx);
6907 if (NT_STATUS_IS_ERR(result)) {
6908 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6909 nt_errstr(result)));
6911 talloc_destroy(mem_ctx);
6916 * Keep calling LsaEnumTrustdom over opened pipe until
6917 * the end of enumeration is reached
6920 d_printf(_("Vampire trusted domains:\n\n"));
6923 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6929 if (NT_STATUS_IS_ERR(nt_status)) {
6930 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6931 nt_errstr(nt_status)));
6933 talloc_destroy(mem_ctx);
6936 if (NT_STATUS_IS_ERR(result)) {
6938 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6939 nt_errstr(result)));
6941 talloc_destroy(mem_ctx);
6946 for (i = 0; i < dom_list.count; i++) {
6948 print_trusted_domain(dom_list.domains[i].sid,
6949 dom_list.domains[i].name.string);
6951 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6952 *dom_list.domains[i].sid,
6953 dom_list.domains[i].name.string);
6954 if (!NT_STATUS_IS_OK(nt_status)) {
6956 talloc_destroy(mem_ctx);
6962 * in case of no trusted domains say something rather
6963 * than just display blank line
6965 if (!dom_list.count) d_printf(_("none\n"));
6967 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6969 /* close this connection before doing next one */
6970 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6971 if (NT_STATUS_IS_ERR(nt_status)) {
6972 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6973 nt_errstr(nt_status)));
6975 talloc_destroy(mem_ctx);
6979 /* close lsarpc pipe and connection to IPC$ */
6982 talloc_destroy(mem_ctx);
6986 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6988 /* common variables */
6989 TALLOC_CTX* mem_ctx;
6990 struct cli_state *cli = NULL, *remote_cli = NULL;
6991 struct rpc_pipe_client *pipe_hnd = NULL;
6992 NTSTATUS nt_status, result;
6993 const char *domain_name = NULL;
6994 struct dom_sid *queried_dom_sid;
6995 int ascii_dom_name_len;
6996 struct policy_handle connect_hnd;
6997 union lsa_PolicyInformation *info = NULL;
6998 struct dcerpc_binding_handle *b = NULL;
7000 /* trusted domains listing variables */
7001 unsigned int num_domains, enum_ctx = 0;
7003 struct lsa_DomainList dom_list;
7007 /* trusting domains listing variables */
7008 struct policy_handle domain_hnd;
7009 struct samr_SamArray *trusts = NULL;
7011 if (c->display_usage) {
7013 "net rpc trustdom list\n"
7016 _("List incoming and outgoing trust relationships"));
7021 * Listing trusted domains (stored in secrets.tdb, if local)
7024 mem_ctx = talloc_init("trust relationships listing");
7027 * set domain and pdc name to local samba server (default)
7028 * or to remote one given in command line
7031 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
7032 domain_name = c->opt_workgroup;
7033 c->opt_target_workgroup = c->opt_workgroup;
7035 fstrcpy(pdc_name, lp_netbios_name());
7036 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
7037 c->opt_target_workgroup = domain_name;
7040 /* open \PIPE\lsarpc and open policy handle */
7041 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
7042 if (!NT_STATUS_IS_OK(nt_status)) {
7043 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
7044 nt_errstr(nt_status)));
7045 talloc_destroy(mem_ctx);
7049 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
7051 if (!NT_STATUS_IS_OK(nt_status)) {
7052 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
7053 nt_errstr(nt_status) ));
7055 talloc_destroy(mem_ctx);
7059 b = pipe_hnd->binding_handle;
7061 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
7063 if (NT_STATUS_IS_ERR(nt_status)) {
7064 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
7065 nt_errstr(nt_status)));
7067 talloc_destroy(mem_ctx);
7071 /* query info level 5 to obtain sid of a domain being queried */
7072 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
7074 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
7078 if (NT_STATUS_IS_ERR(nt_status)) {
7079 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7080 nt_errstr(nt_status)));
7082 talloc_destroy(mem_ctx);
7085 if (NT_STATUS_IS_ERR(result)) {
7086 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7087 nt_errstr(result)));
7089 talloc_destroy(mem_ctx);
7093 queried_dom_sid = info->account_domain.sid;
7096 * Keep calling LsaEnumTrustdom over opened pipe until
7097 * the end of enumeration is reached
7100 d_printf(_("Trusted domains list:\n\n"));
7102 found_domain = false;
7105 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
7111 if (NT_STATUS_IS_ERR(nt_status)) {
7112 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7113 nt_errstr(nt_status)));
7115 talloc_destroy(mem_ctx);
7118 if (NT_STATUS_IS_ERR(result)) {
7119 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7120 nt_errstr(result)));
7122 talloc_destroy(mem_ctx);
7127 for (i = 0; i < dom_list.count; i++) {
7128 print_trusted_domain(dom_list.domains[i].sid,
7129 dom_list.domains[i].name.string);
7130 found_domain = true;
7134 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7137 * in case of no trusted domains say something rather
7138 * than just display blank line
7140 if (!found_domain) {
7141 d_printf(_("none\n"));
7144 /* close this connection before doing next one */
7145 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7146 if (NT_STATUS_IS_ERR(nt_status)) {
7147 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7148 nt_errstr(nt_status)));
7150 talloc_destroy(mem_ctx);
7154 TALLOC_FREE(pipe_hnd);
7157 * Listing trusting domains (stored in passdb backend, if local)
7160 d_printf(_("\nTrusting domains list:\n\n"));
7163 * Open \PIPE\samr and get needed policy handles
7165 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
7167 if (!NT_STATUS_IS_OK(nt_status)) {
7168 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
7170 talloc_destroy(mem_ctx);
7174 b = pipe_hnd->binding_handle;
7177 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
7179 SAMR_ACCESS_LOOKUP_DOMAIN,
7182 if (!NT_STATUS_IS_OK(nt_status)) {
7183 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7184 nt_errstr(nt_status)));
7186 talloc_destroy(mem_ctx);
7189 if (!NT_STATUS_IS_OK(result)) {
7191 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7192 nt_errstr(result)));
7194 talloc_destroy(mem_ctx);
7198 /* SamrOpenDomain - we have to open domain policy handle in order to be
7199 able to enumerate accounts*/
7200 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
7202 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
7206 if (!NT_STATUS_IS_OK(nt_status)) {
7207 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7208 nt_errstr(nt_status)));
7210 talloc_destroy(mem_ctx);
7213 if (!NT_STATUS_IS_OK(result)) {
7215 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7216 nt_errstr(result)));
7218 talloc_destroy(mem_ctx);
7223 * perform actual enumeration
7226 found_domain = false;
7228 enum_ctx = 0; /* reset enumeration context from last enumeration */
7231 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
7239 if (NT_STATUS_IS_ERR(nt_status)) {
7240 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7241 nt_errstr(nt_status)));
7243 talloc_destroy(mem_ctx);
7246 if (NT_STATUS_IS_ERR(result)) {
7248 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7249 nt_errstr(result)));
7251 talloc_destroy(mem_ctx);
7255 for (i = 0; i < num_domains; i++) {
7257 char *str = discard_const_p(char, trusts->entries[i].name.string);
7259 found_domain = true;
7262 * get each single domain's sid (do we _really_ need this ?):
7263 * 1) connect to domain's pdc
7264 * 2) query the pdc for domain's sid
7267 /* get rid of '$' tail */
7268 ascii_dom_name_len = strlen(str);
7269 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7270 str[ascii_dom_name_len - 1] = '\0';
7272 /* set opt_* variables to remote domain */
7273 if (!strupper_m(str)) {
7275 talloc_destroy(mem_ctx);
7278 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7279 c->opt_target_workgroup = c->opt_workgroup;
7281 d_printf("%-20s", str);
7283 /* connect to remote domain controller */
7284 nt_status = net_make_ipc_connection(c,
7285 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7287 if (NT_STATUS_IS_OK(nt_status)) {
7288 /* query for domain's sid */
7289 if (run_rpc_command(
7291 &ndr_table_lsarpc, 0,
7292 rpc_query_domain_sid, argc,
7294 d_printf(_("strange - couldn't get domain's sid\n"));
7296 cli_shutdown(remote_cli);
7299 d_fprintf(stderr, _("domain controller is not "
7300 "responding: %s\n"),
7301 nt_errstr(nt_status));
7302 d_printf(_("couldn't get domain's sid\n"));
7306 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7308 if (!found_domain) {
7312 /* close opened samr and domain policy handles */
7313 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7314 if (!NT_STATUS_IS_OK(nt_status)) {
7315 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7318 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7319 if (!NT_STATUS_IS_OK(nt_status)) {
7320 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7323 /* close samr pipe and connection to IPC$ */
7326 talloc_destroy(mem_ctx);
7331 * Entrypoint for 'net rpc trustdom' code.
7333 * @param argc Standard argc.
7334 * @param argv Standard argv without initial components.
7336 * @return Integer status (0 means success).
7339 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7341 struct functable func[] = {
7346 N_("Add trusting domain's account"),
7347 N_("net rpc trustdom add\n"
7348 " Add trusting domain's account")
7354 N_("Remove trusting domain's account"),
7355 N_("net rpc trustdom del\n"
7356 " Remove trusting domain's account")
7360 rpc_trustdom_establish,
7362 N_("Establish outgoing trust relationship"),
7363 N_("net rpc trustdom establish\n"
7364 " Establish outgoing trust relationship")
7368 rpc_trustdom_revoke,
7370 N_("Revoke outgoing trust relationship"),
7371 N_("net rpc trustdom revoke\n"
7372 " Revoke outgoing trust relationship")
7378 N_("List in- and outgoing domain trusts"),
7379 N_("net rpc trustdom list\n"
7380 " List in- and outgoing domain trusts")
7384 rpc_trustdom_vampire,
7386 N_("Vampire trusts from remote server"),
7387 N_("net rpc trustdom vampire\n"
7388 " Vampire trusts from remote server")
7390 {NULL, NULL, 0, NULL, NULL}
7393 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7397 * Check if a server will take rpc commands
7398 * @param flags Type of server to connect to (PDC, DMB, localhost)
7399 * if the host is not explicitly specified
7400 * @return bool (true means rpc supported)
7402 bool net_rpc_check(struct net_context *c, unsigned flags)
7404 struct cli_state *cli;
7406 struct sockaddr_storage server_ss;
7407 char *server_name = NULL;
7410 /* flags (i.e. server type) may depend on command */
7411 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7414 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7415 lp_netbios_name(), SMB_SIGNING_IPC_DEFAULT,
7417 if (!NT_STATUS_IS_OK(status)) {
7420 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7422 if (!NT_STATUS_IS_OK(status))
7424 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7433 /* dump sam database via samsync rpc calls */
7434 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7435 if (c->display_usage) {
7440 _("Dump remote SAM database"));
7444 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7445 NET_FLAGS_ANONYMOUS,
7446 rpc_samdump_internals, argc, argv);
7449 /* syncronise sam database via samsync rpc calls */
7450 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7452 struct functable func[] = {
7457 N_("Dump remote SAM database to ldif"),
7458 N_("net rpc vampire ldif\n"
7459 " Dump remote SAM database to LDIF file or "
7466 N_("Dump remote SAM database to Kerberos Keytab"),
7467 N_("net rpc vampire keytab\n"
7468 " Dump remote SAM database to Kerberos keytab "
7475 N_("Dump remote SAM database to passdb"),
7476 N_("net rpc vampire passdb\n"
7477 " Dump remote SAM database to passdb")
7480 {NULL, NULL, 0, NULL, NULL}
7484 if (c->display_usage) {
7489 _("Vampire remote SAM database"));
7493 return rpc_vampire_passdb(c, argc, argv);
7496 return net_run_function(c, argc, argv, "net rpc vampire", func);
7500 * Migrate everything from a print server.
7502 * @param c A net_context structure.
7503 * @param argc Standard main() style argc.
7504 * @param argv Standard main() style argv. Initial components are already
7507 * @return A shell status integer (0 for success).
7509 * The order is important !
7510 * To successfully add drivers the print queues have to exist !
7511 * Applying ACLs should be the last step, because you're easily locked out.
7514 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7519 if (c->display_usage) {
7521 "net rpc printer migrate all\n"
7524 _("Migrate everything from a print server"));
7529 d_printf(_("no server to migrate\n"));
7533 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7534 rpc_printer_migrate_printers_internals, argc,
7539 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7540 rpc_printer_migrate_drivers_internals, argc,
7545 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7546 rpc_printer_migrate_forms_internals, argc, argv);
7550 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7551 rpc_printer_migrate_settings_internals, argc,
7556 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7557 rpc_printer_migrate_security_internals, argc,
7563 * Migrate print drivers from a print server.
7565 * @param c A net_context structure.
7566 * @param argc Standard main() style argc.
7567 * @param argv Standard main() style argv. Initial components are already
7570 * @return A shell status integer (0 for success).
7572 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7575 if (c->display_usage) {
7577 "net rpc printer migrate drivers\n"
7580 _("Migrate print-drivers from a print-server"));
7585 d_printf(_("no server to migrate\n"));
7589 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7590 rpc_printer_migrate_drivers_internals,
7595 * Migrate print-forms from a print-server.
7597 * @param c A net_context structure.
7598 * @param argc Standard main() style argc.
7599 * @param argv Standard main() style argv. Initial components are already
7602 * @return A shell status integer (0 for success).
7604 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7607 if (c->display_usage) {
7609 "net rpc printer migrate forms\n"
7612 _("Migrate print-forms from a print-server"));
7617 d_printf(_("no server to migrate\n"));
7621 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7622 rpc_printer_migrate_forms_internals,
7627 * Migrate printers from a print-server.
7629 * @param c A net_context structure.
7630 * @param argc Standard main() style argc.
7631 * @param argv Standard main() style argv. Initial components are already
7634 * @return A shell status integer (0 for success).
7636 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7639 if (c->display_usage) {
7641 "net rpc printer migrate printers\n"
7644 _("Migrate printers from a print-server"));
7649 d_printf(_("no server to migrate\n"));
7653 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7654 rpc_printer_migrate_printers_internals,
7659 * Migrate printer-ACLs from a print-server
7661 * @param c A net_context structure.
7662 * @param argc Standard main() style argc.
7663 * @param argv Standard main() style argv. Initial components are already
7666 * @return A shell status integer (0 for success).
7668 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7671 if (c->display_usage) {
7673 "net rpc printer migrate security\n"
7676 _("Migrate printer-ACLs from a print-server"));
7681 d_printf(_("no server to migrate\n"));
7685 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7686 rpc_printer_migrate_security_internals,
7691 * Migrate printer-settings from a print-server.
7693 * @param c A net_context structure.
7694 * @param argc Standard main() style argc.
7695 * @param argv Standard main() style argv. Initial components are already
7698 * @return A shell status integer (0 for success).
7700 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7703 if (c->display_usage) {
7705 "net rpc printer migrate settings\n"
7708 _("Migrate printer-settings from a "
7714 d_printf(_("no server to migrate\n"));
7718 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7719 rpc_printer_migrate_settings_internals,
7724 * 'net rpc printer' entrypoint.
7726 * @param c A net_context structure.
7727 * @param argc Standard main() style argc.
7728 * @param argv Standard main() style argv. Initial components are already
7732 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7735 /* ouch: when addriver and setdriver are called from within
7736 rpc_printer_migrate_drivers_internals, the printer-queue already
7739 struct functable func[] = {
7742 rpc_printer_migrate_all,
7744 N_("Migrate all from remote to local print server"),
7745 N_("net rpc printer migrate all\n"
7746 " Migrate all from remote to local print server")
7750 rpc_printer_migrate_drivers,
7752 N_("Migrate drivers to local server"),
7753 N_("net rpc printer migrate drivers\n"
7754 " Migrate drivers to local server")
7758 rpc_printer_migrate_forms,
7760 N_("Migrate froms to local server"),
7761 N_("net rpc printer migrate forms\n"
7762 " Migrate froms to local server")
7766 rpc_printer_migrate_printers,
7768 N_("Migrate printers to local server"),
7769 N_("net rpc printer migrate printers\n"
7770 " Migrate printers to local server")
7774 rpc_printer_migrate_security,
7776 N_("Mirgate printer ACLs to local server"),
7777 N_("net rpc printer migrate security\n"
7778 " Mirgate printer ACLs to local server")
7782 rpc_printer_migrate_settings,
7784 N_("Migrate printer settings to local server"),
7785 N_("net rpc printer migrate settings\n"
7786 " Migrate printer settings to local server")
7788 {NULL, NULL, 0, NULL, NULL}
7791 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7796 * List printers on a remote RPC server.
7798 * @param c A net_context structure.
7799 * @param argc Standard main() style argc.
7800 * @param argv Standard main() style argv. Initial components are already
7803 * @return A shell status integer (0 for success).
7805 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7807 if (c->display_usage) {
7809 "net rpc printer list\n"
7812 _("List printers on a remote RPC server"));
7816 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7817 rpc_printer_list_internals,
7822 * List printer-drivers on a remote RPC server.
7824 * @param c A net_context structure.
7825 * @param argc Standard main() style argc.
7826 * @param argv Standard main() style argv. Initial components are already
7829 * @return A shell status integer (0 for success).
7831 static int rpc_printer_driver_list(struct net_context *c, int argc,
7834 if (c->display_usage) {
7836 "net rpc printer driver\n"
7839 _("List printer-drivers on a remote RPC server"));
7843 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7844 rpc_printer_driver_list_internals,
7849 * Publish printer in ADS via MSRPC.
7851 * @param c A net_context structure.
7852 * @param argc Standard main() style argc.
7853 * @param argv Standard main() style argv. Initial components are already
7856 * @return A shell status integer (0 for success).
7858 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7861 if (c->display_usage) {
7863 "net rpc printer publish publish\n"
7866 _("Publish printer in ADS via MSRPC"));
7870 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7871 rpc_printer_publish_publish_internals,
7876 * Update printer in ADS via MSRPC.
7878 * @param c A net_context structure.
7879 * @param argc Standard main() style argc.
7880 * @param argv Standard main() style argv. Initial components are already
7883 * @return A shell status integer (0 for success).
7885 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7887 if (c->display_usage) {
7889 "net rpc printer publish update\n"
7892 _("Update printer in ADS via MSRPC"));
7896 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7897 rpc_printer_publish_update_internals,
7902 * UnPublish printer in ADS via MSRPC.
7904 * @param c A net_context structure.
7905 * @param argc Standard main() style argc.
7906 * @param argv Standard main() style argv. Initial components are already
7909 * @return A shell status integer (0 for success).
7911 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7914 if (c->display_usage) {
7916 "net rpc printer publish unpublish\n"
7919 _("UnPublish printer in ADS via MSRPC"));
7923 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7924 rpc_printer_publish_unpublish_internals,
7929 * List published printers via MSRPC.
7931 * @param c A net_context structure.
7932 * @param argc Standard main() style argc.
7933 * @param argv Standard main() style argv. Initial components are already
7936 * @return A shell status integer (0 for success).
7938 static int rpc_printer_publish_list(struct net_context *c, int argc,
7941 if (c->display_usage) {
7943 "net rpc printer publish list\n"
7946 _("List published printers via MSRPC"));
7950 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7951 rpc_printer_publish_list_internals,
7957 * Publish printer in ADS.
7959 * @param c A net_context structure.
7960 * @param argc Standard main() style argc.
7961 * @param argv Standard main() style argv. Initial components are already
7964 * @return A shell status integer (0 for success).
7966 static int rpc_printer_publish(struct net_context *c, int argc,
7970 struct functable func[] = {
7973 rpc_printer_publish_publish,
7975 N_("Publish printer in AD"),
7976 N_("net rpc printer publish publish\n"
7977 " Publish printer in AD")
7981 rpc_printer_publish_update,
7983 N_("Update printer in AD"),
7984 N_("net rpc printer publish update\n"
7985 " Update printer in AD")
7989 rpc_printer_publish_unpublish,
7991 N_("Unpublish printer"),
7992 N_("net rpc printer publish unpublish\n"
7993 " Unpublish printer")
7997 rpc_printer_publish_list,
7999 N_("List published printers"),
8000 N_("net rpc printer publish list\n"
8001 " List published printers")
8003 {NULL, NULL, 0, NULL, NULL}
8007 if (c->display_usage) {
8008 d_printf(_("Usage:\n"));
8009 d_printf(_("net rpc printer publish\n"
8010 " List published printers\n"
8011 " Alias of net rpc printer publish "
8013 net_display_usage_from_functable(func);
8016 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8017 rpc_printer_publish_list_internals,
8021 return net_run_function(c, argc, argv, "net rpc printer publish",func);
8027 * Display rpc printer help page.
8029 * @param c A net_context structure.
8030 * @param argc Standard main() style argc.
8031 * @param argv Standard main() style argv. Initial components are already
8034 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
8036 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
8037 "\tlists all printers on print-server\n\n"));
8038 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
8039 "\tlists all printer-drivers on print-server\n\n"));
8040 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
8041 "\tpublishes printer settings in Active Directory\n"
8042 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
8043 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
8044 "\n\tmigrates printers from remote to local server\n\n"));
8045 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
8046 "\n\tmigrates printer-settings from remote to local server\n\n"));
8047 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
8048 "\n\tmigrates printer-drivers from remote to local server\n\n"));
8049 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
8050 "\n\tmigrates printer-forms from remote to local server\n\n"));
8051 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
8052 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
8053 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
8054 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
8055 "\tremote to local print-server\n\n"));
8056 net_common_methods_usage(c, argc, argv);
8057 net_common_flags_usage(c, argc, argv);
8059 "\t-v or --verbose\t\t\tgive verbose output\n"
8060 "\t --destination\t\tmigration target server (default: localhost)\n"));
8066 * 'net rpc printer' entrypoint.
8068 * @param c A net_context structure.
8069 * @param argc Standard main() style argc.
8070 * @param argv Standard main() style argv. Initial components are already
8073 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
8075 struct functable func[] = {
8080 N_("List all printers on print server"),
8081 N_("net rpc printer list\n"
8082 " List all printers on print server")
8086 rpc_printer_migrate,
8088 N_("Migrate printer to local server"),
8089 N_("net rpc printer migrate\n"
8090 " Migrate printer to local server")
8094 rpc_printer_driver_list,
8096 N_("List printer drivers"),
8097 N_("net rpc printer driver\n"
8098 " List printer drivers")
8102 rpc_printer_publish,
8104 N_("Publish printer in AD"),
8105 N_("net rpc printer publish\n"
8106 " Publish printer in AD")
8108 {NULL, NULL, 0, NULL, NULL}
8112 if (c->display_usage) {
8113 d_printf(_("Usage:\n"));
8114 d_printf(_("net rpc printer\n"
8115 " List printers\n"));
8116 net_display_usage_from_functable(func);
8119 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8120 rpc_printer_list_internals,
8124 return net_run_function(c, argc, argv, "net rpc printer", func);
8128 * 'net rpc' entrypoint.
8130 * @param c A net_context structure.
8131 * @param argc Standard main() style argc.
8132 * @param argv Standard main() style argv. Initial components are already
8136 int net_rpc(struct net_context *c, int argc, const char **argv)
8138 NET_API_STATUS status;
8140 struct functable func[] = {
8145 N_("Modify global audit settings"),
8146 N_("net rpc audit\n"
8147 " Modify global audit settings")
8153 N_("Show basic info about a domain"),
8155 " Show basic info about a domain")
8161 N_("Join a domain"),
8169 N_("Join a domain created in server manager"),
8170 N_("net rpc oldjoin\n"
8171 " Join a domain created in server manager")
8177 N_("Test that a join is valid"),
8178 N_("net rpc testjoin\n"
8179 " Test that a join is valid")
8185 N_("List/modify users"),
8187 " List/modify users")
8193 N_("Change a user password"),
8194 N_("net rpc password\n"
8195 " Change a user password\n"
8196 " Alias for net rpc user password")
8202 N_("List/modify groups"),
8203 N_("net rpc group\n"
8204 " List/modify groups")
8210 N_("List/modify shares"),
8211 N_("net rpc share\n"
8212 " List/modify shares")
8218 N_("List open files"),
8226 N_("List/modify printers"),
8227 N_("net rpc printer\n"
8228 " List/modify printers")
8232 net_rpc_changetrustpw,
8234 N_("Change trust account password"),
8235 N_("net rpc changetrustpw\n"
8236 " Change trust account password")
8242 N_("Modify domain trusts"),
8243 N_("net rpc trustdom\n"
8244 " Modify domain trusts")
8250 N_("Abort a remote shutdown"),
8251 N_("net rpc abortshutdown\n"
8252 " Abort a remote shutdown")
8258 N_("Shutdown a remote server"),
8259 N_("net rpc shutdown\n"
8260 " Shutdown a remote server")
8266 N_("Dump SAM data of remote NT PDC"),
8267 N_("net rpc samdump\n"
8268 " Dump SAM data of remote NT PDC")
8274 N_("Sync a remote NT PDC's data into local passdb"),
8275 N_("net rpc vampire\n"
8276 " Sync a remote NT PDC's data into local passdb")
8282 N_("Fetch the domain sid into local secrets.tdb"),
8283 N_("net rpc getsid\n"
8284 " Fetch the domain sid into local secrets.tdb")
8290 N_("Manage privileges assigned to SID"),
8291 N_("net rpc rights\n"
8292 " Manage privileges assigned to SID")
8298 N_("Start/stop/query remote services"),
8299 N_("net rpc service\n"
8300 " Start/stop/query remote services")
8306 N_("Manage registry hives"),
8307 N_("net rpc registry\n"
8308 " Manage registry hives")
8314 N_("Open interactive shell on remote server"),
8315 N_("net rpc shell\n"
8316 " Open interactive shell on remote server")
8322 N_("Manage trusts"),
8323 N_("net rpc trust\n"
8330 N_("Configure a remote samba server"),
8332 " Configure a remote samba server")
8334 {NULL, NULL, 0, NULL, NULL}
8337 status = libnetapi_net_init(&c->netapi_ctx);
8341 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8342 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8343 if (c->opt_kerberos) {
8344 libnetapi_set_use_kerberos(c->netapi_ctx);
8346 if (c->opt_ccache) {
8347 libnetapi_set_use_ccache(c->netapi_ctx);
8350 return net_run_function(c, argc, argv, "net rpc", func);