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 "libsmb/namequery.h"
26 #include "rpc_client/cli_pipe.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "../librpc/gen_ndr/ndr_samr_c.h"
29 #include "rpc_client/cli_samr.h"
30 #include "rpc_client/init_samr.h"
31 #include "../librpc/gen_ndr/ndr_lsa_c.h"
32 #include "rpc_client/cli_lsarpc.h"
33 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
34 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
35 #include "../librpc/gen_ndr/ndr_spoolss.h"
36 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
37 #include "../librpc/gen_ndr/ndr_winreg_c.h"
39 #include "lib/netapi/netapi.h"
40 #include "lib/netapi/netapi_net.h"
41 #include "librpc/gen_ndr/libnet_join.h"
42 #include "libnet/libnet_join.h"
43 #include "rpc_client/init_lsa.h"
44 #include "../libcli/security/security.h"
45 #include "libsmb/libsmb.h"
47 #include "nsswitch/libwbclient/wbclient.h"
49 #include "../libcli/smb/smbXcli_base.h"
50 #include "libsmb/dsgetdcname.h"
51 #include "lib/util/string_wrappers.h"
53 static int net_mode_share;
54 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
59 * @brief RPC based subcommands for the 'net' utility.
61 * This file should contain much of the functionality that used to
62 * be found in rpcclient, except that the commands should change
63 * less often, and the functionality should be sane (the user is not
64 * expected to know a rid/sid before they conduct an operation etc.)
66 * @todo Perhaps eventually these should be split out into a number
67 * of files, as this could get quite big.
72 * Many of the RPC functions need the domain sid. This function gets
73 * it at the start of every run
75 * @param cli A cli_state already connected to the remote machine
77 * @return The Domain SID of the remote machine.
80 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
81 struct dom_sid **domain_sid,
82 const char **domain_name)
84 struct rpc_pipe_client *lsa_pipe = NULL;
85 struct policy_handle pol;
86 NTSTATUS status, result;
87 union lsa_PolicyInformation *info = NULL;
88 struct dcerpc_binding_handle *b;
90 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
92 if (!NT_STATUS_IS_OK(status)) {
93 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
97 b = lsa_pipe->binding_handle;
99 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
100 SEC_FLAG_MAXIMUM_ALLOWED,
102 if (!NT_STATUS_IS_OK(status)) {
103 d_fprintf(stderr, "open_policy %s: %s\n",
109 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
111 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
114 if (any_nt_status_not_ok(status, result, &status)) {
115 d_fprintf(stderr, "lsaquery %s: %s\n",
121 *domain_name = info->account_domain.name.string;
122 *domain_sid = info->account_domain.sid;
124 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
125 TALLOC_FREE(lsa_pipe);
131 * Run a single RPC command, from start to finish.
133 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
134 * @param conn_flag a NET_FLAG_ combination. Passed to
135 * net_make_ipc_connection.
136 * @param argc Standard main() style argc.
137 * @param argv Standard main() style argv. Initial components are already
139 * @return A shell status integer (0 for success).
142 int run_rpc_command(struct net_context *c,
143 struct cli_state *cli_arg,
144 const struct ndr_interface_table *table,
150 struct cli_state *cli = NULL;
151 struct rpc_pipe_client *pipe_hnd = NULL;
154 struct dom_sid *domain_sid;
155 const char *domain_name;
158 /* make use of cli_state handed over as an argument, if possible */
160 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
161 if (!NT_STATUS_IS_OK(nt_status)) {
162 DEBUG(1, ("failed to make ipc connection: %s\n",
163 nt_errstr(nt_status)));
176 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
177 DEBUG(0, ("talloc_init() failed\n"));
181 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
183 if (!NT_STATUS_IS_OK(nt_status)) {
187 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
188 if (lp_client_schannel()
189 && (ndr_syntax_id_equal(&table->syntax_id,
190 &ndr_table_netlogon.syntax_id))) {
191 const char *remote_name =
192 smbXcli_conn_remote_name(cli->conn);
193 const struct sockaddr_storage *remote_sockaddr =
194 smbXcli_conn_remote_sockaddr(cli->conn);
196 /* Always try and create an schannel netlogon pipe. */
197 TALLOC_FREE(c->netlogon_creds);
198 nt_status = cli_rpc_pipe_open_schannel(
199 cli, c->msg_ctx, table, NCACN_NP,
203 &pipe_hnd, c, &c->netlogon_creds);
204 if (!NT_STATUS_IS_OK(nt_status)) {
205 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
206 nt_errstr(nt_status) ));
210 if (conn_flags & NET_FLAGS_SEAL) {
211 nt_status = cli_rpc_pipe_open_with_creds(
213 (conn_flags & NET_FLAGS_TCP) ?
214 NCACN_IP_TCP : NCACN_NP,
215 DCERPC_AUTH_TYPE_NTLMSSP,
216 DCERPC_AUTH_LEVEL_PRIVACY,
217 smbXcli_conn_remote_name(cli->conn),
218 smbXcli_conn_remote_sockaddr(cli->conn),
219 c->creds, &pipe_hnd);
221 nt_status = cli_rpc_pipe_open_noauth(
225 if (!NT_STATUS_IS_OK(nt_status)) {
226 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
228 nt_errstr(nt_status) ));
234 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
236 if (!NT_STATUS_IS_OK(nt_status)) {
237 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
240 DEBUG(5, ("rpc command function succeeded\n"));
243 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
245 TALLOC_FREE(pipe_hnd);
250 /* close the connection only if it was opened here */
255 talloc_destroy(mem_ctx);
260 * Force a change of the trust account password.
262 * All parameters are provided by the run_rpc_command function, except for
263 * argc, argv which are passed through.
265 * @param domain_sid The domain sid acquired from the remote server.
266 * @param cli A cli_state connected to the server.
267 * @param mem_ctx Talloc context, destroyed on completion of the function.
268 * @param argc Standard main() style argc.
269 * @param argv Standard main() style argv. Initial components are already
272 * @return Normal NTSTATUS return.
275 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
276 const struct dom_sid *domain_sid,
277 const char *domain_name,
278 struct cli_state *cli,
279 struct rpc_pipe_client *pipe_hnd,
285 const char *dcname = NULL;
288 return NT_STATUS_INTERNAL_ERROR;
291 dcname = smbXcli_conn_remote_name(cli->conn);
293 status = trust_pw_change(c->netlogon_creds,
295 pipe_hnd->binding_handle,
296 c->opt_target_workgroup,
299 if (!NT_STATUS_IS_OK(status)) {
300 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
309 * Force a change of the trust account password.
311 * @param argc Standard main() style argc.
312 * @param argv Standard main() style argv. Initial components are already
315 * @return A shell status integer (0 for success).
318 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
320 int conn_flags = NET_FLAGS_PDC;
322 if (!c->opt_user_specified && !c->opt_kerberos) {
323 conn_flags |= NET_FLAGS_ANONYMOUS;
326 if (c->display_usage) {
328 "net rpc changetrustpw\n"
331 _("Change the machine trust password"));
335 return run_rpc_command(c, NULL, &ndr_table_netlogon,
337 rpc_changetrustpw_internals,
342 * Join a domain, the old way. This function exists to allow
343 * the message to be displayed when oldjoin was explicitly
344 * requested, but not when it was implied by "net rpc join".
346 * This uses 'machinename' as the initial password, and changes it.
348 * The password should be created with 'server manager' or equiv first.
350 * @param argc Standard main() style argc.
351 * @param argv Standard main() style argv. Initial components are already
354 * @return A shell status integer (0 for success).
357 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
359 struct libnet_JoinCtx *r = NULL;
362 const char *domain = lp_workgroup(); /* FIXME */
363 bool modify_config = lp_config_backend_is_registry();
364 enum netr_SchannelType sec_chan_type;
367 if (c->display_usage) {
370 " Join a domain the old way\n");
374 net_warn_member_options();
376 mem_ctx = talloc_init("net_rpc_oldjoin");
381 werr = libnet_init_JoinCtx(mem_ctx, &r);
382 if (!W_ERROR_IS_OK(werr)) {
387 check what type of join - if the user wants to join as
388 a BDC, the server must agree that we are a BDC.
391 sec_chan_type = get_sec_channel_type(argv[0]);
393 sec_chan_type = get_sec_channel_type(NULL);
397 d_fprintf(stderr, _("Could not initialise message context. "
398 "Try running as root\n"));
399 werr = WERR_ACCESS_DENIED;
403 pw = talloc_strndup(r, lp_netbios_name(), 14);
405 werr = WERR_NOT_ENOUGH_MEMORY;
409 r->in.msg_ctx = c->msg_ctx;
410 r->in.domain_name = domain;
411 r->in.secure_channel_type = sec_chan_type;
412 r->in.dc_name = c->opt_host;
413 r->in.admin_account = "";
414 r->in.admin_password = strlower_talloc(r, pw);
415 if (r->in.admin_password == NULL) {
416 werr = WERR_NOT_ENOUGH_MEMORY;
420 r->in.modify_config = modify_config;
421 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
422 WKSSVC_JOIN_FLAGS_JOIN_UNSECURE |
423 WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED;
425 werr = libnet_Join(mem_ctx, r);
426 if (!W_ERROR_IS_OK(werr)) {
430 /* Check the short name of the domain */
432 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
433 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
434 d_printf("domain name obtained from the server.\n");
435 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
436 d_printf("You should set \"workgroup = %s\" in %s.\n",
437 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
440 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
442 if (r->out.dns_domain_name) {
443 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
444 r->out.dns_domain_name);
446 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
447 r->out.netbios_domain_name);
450 /* print out informative error string in case there is one */
451 if (r->out.error_string != NULL) {
452 d_printf("%s\n", r->out.error_string);
455 TALLOC_FREE(mem_ctx);
460 if (c->opt_flags & NET_FLAGS_EXPECT_FALLBACK) {
464 /* issue an overall failure message at the end. */
465 d_fprintf(stderr, _("Failed to join domain: %s\n"),
466 r && r->out.error_string ? r->out.error_string :
467 get_friendly_werror_msg(werr));
470 TALLOC_FREE(mem_ctx);
476 * check that a join is OK
478 * @return A shell status integer (0 for success)
481 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
485 const char *domain = c->opt_target_workgroup;
486 const char *dc = c->opt_host;
488 if (c->display_usage) {
491 " Test if a join is OK\n");
495 net_warn_member_options();
497 mem_ctx = talloc_init("net_rpc_testjoin");
503 struct netr_DsRGetDCNameInfo *info;
506 d_fprintf(stderr, _("Could not initialise message context. "
507 "Try running as root\n"));
508 talloc_destroy(mem_ctx);
512 status = dsgetdcname(mem_ctx,
519 if (!NT_STATUS_IS_OK(status)) {
520 talloc_destroy(mem_ctx);
524 dc = strip_hostname(info->dc_unc);
527 /* Display success or failure */
528 status = libnet_join_ok(c->msg_ctx,
532 if (!NT_STATUS_IS_OK(status)) {
533 fprintf(stderr,"Join to domain '%s' is not valid: %s\n",
534 domain, nt_errstr(status));
535 talloc_destroy(mem_ctx);
539 printf("Join to '%s' is OK\n",domain);
540 talloc_destroy(mem_ctx);
546 * Join a domain using the administrator username and password
548 * @param argc Standard main() style argc
549 * @param argc Standard main() style argv. Initial components are already
550 * stripped. Currently not used.
551 * @return A shell status integer (0 for success)
555 static int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
557 struct libnet_JoinCtx *r = NULL;
560 const char *domain = lp_workgroup(); /* FIXME */
561 bool modify_config = lp_config_backend_is_registry();
562 enum netr_SchannelType sec_chan_type;
564 if (c->display_usage) {
567 " Join a domain the new way\n");
571 net_warn_member_options();
573 mem_ctx = talloc_init("net_rpc_join_newstyle");
578 werr = libnet_init_JoinCtx(mem_ctx, &r);
579 if (!W_ERROR_IS_OK(werr)) {
584 check what type of join - if the user wants to join as
585 a BDC, the server must agree that we are a BDC.
588 sec_chan_type = get_sec_channel_type(argv[0]);
590 sec_chan_type = get_sec_channel_type(NULL);
594 d_fprintf(stderr, _("Could not initialise message context. "
595 "Try running as root\n"));
596 werr = WERR_ACCESS_DENIED;
600 r->in.msg_ctx = c->msg_ctx;
601 r->in.domain_name = domain;
602 r->in.secure_channel_type = sec_chan_type;
603 r->in.dc_name = c->opt_host;
604 r->in.admin_account = c->opt_user_name;
605 r->in.admin_password = net_prompt_pass(c, c->opt_user_name);
607 r->in.use_kerberos = c->opt_kerberos;
608 r->in.modify_config = modify_config;
609 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
610 WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE |
611 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
613 werr = libnet_Join(mem_ctx, r);
614 if (!W_ERROR_IS_OK(werr)) {
618 /* Check the short name of the domain */
620 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
621 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
622 d_printf("domain name obtained from the server.\n");
623 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
624 d_printf("You should set \"workgroup = %s\" in %s.\n",
625 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
628 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
630 if (r->out.dns_domain_name) {
631 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
632 r->out.dns_domain_name);
634 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
635 r->out.netbios_domain_name);
638 /* print out informative error string in case there is one */
639 if (r->out.error_string != NULL) {
640 d_printf("%s\n", r->out.error_string);
643 TALLOC_FREE(mem_ctx);
648 /* issue an overall failure message at the end. */
649 d_printf("Failed to join domain: %s\n",
650 r && r->out.error_string ? r->out.error_string :
651 get_friendly_werror_msg(werr));
653 TALLOC_FREE(mem_ctx);
659 * 'net rpc join' entrypoint.
660 * @param argc Standard main() style argc.
661 * @param argv Standard main() style argv. Initial components are already
664 * Main 'net_rpc_join()' (where the admin username/password is used) is
666 * Try to just change the password, but if that doesn't work, use/prompt
667 * for a username/password.
670 int net_rpc_join(struct net_context *c, int argc, const char **argv)
674 if (c->display_usage) {
677 _("net rpc join -U <username>[%%password] <type>\n"
679 " username\tName of the admin user"
680 " password\tPassword of the admin user, will "
681 "prompt if not specified\n"
682 " type\tCan be one of the following:\n"
683 "\t\tMEMBER\tJoin as member server (default)\n"
684 "\t\tBDC\tJoin as BDC\n"
685 "\t\tPDC\tJoin as PDC\n"));
689 if (lp_server_role() == ROLE_STANDALONE) {
690 d_printf(_("cannot join as standalone machine\n"));
694 net_warn_member_options();
696 if (strlen(lp_netbios_name()) > 15) {
697 d_printf(_("Our netbios name can be at most 15 chars long, "
698 "\"%s\" is %u chars long\n"),
699 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
703 c->opt_flags |= NET_FLAGS_EXPECT_FALLBACK;
704 ret = net_rpc_oldjoin(c, argc, argv);
705 c->opt_flags &= ~NET_FLAGS_EXPECT_FALLBACK;
710 return net_rpc_join_newstyle(c, argc, argv);
714 * display info about a rpc domain
716 * All parameters are provided by the run_rpc_command function, except for
717 * argc, argv which are passed through.
719 * @param domain_sid The domain sid acquired from the remote server
720 * @param cli A cli_state connected to the server.
721 * @param mem_ctx Talloc context, destroyed on completion of the function.
722 * @param argc Standard main() style argc.
723 * @param argv Standard main() style argv. Initial components are already
726 * @return Normal NTSTATUS return.
729 NTSTATUS rpc_info_internals(struct net_context *c,
730 const struct dom_sid *domain_sid,
731 const char *domain_name,
732 struct cli_state *cli,
733 struct rpc_pipe_client *pipe_hnd,
738 struct policy_handle connect_pol, domain_pol;
739 NTSTATUS status, result;
740 union samr_DomainInfo *info = NULL;
741 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
743 /* Get sam policy handle */
744 status = dcerpc_samr_Connect2(b, mem_ctx,
746 MAXIMUM_ALLOWED_ACCESS,
749 if (!NT_STATUS_IS_OK(status)) {
750 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
755 if (!NT_STATUS_IS_OK(result)) {
757 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
762 /* Get domain policy handle */
763 status = dcerpc_samr_OpenDomain(b, mem_ctx,
765 MAXIMUM_ALLOWED_ACCESS,
766 discard_const_p(struct dom_sid2, domain_sid),
769 if (!NT_STATUS_IS_OK(status)) {
770 d_fprintf(stderr, _("Could not open domain: %s\n"),
774 if (!NT_STATUS_IS_OK(result)) {
776 d_fprintf(stderr, _("Could not open domain: %s\n"),
781 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
786 if (!NT_STATUS_IS_OK(status)) {
790 if (NT_STATUS_IS_OK(result)) {
791 struct dom_sid_buf sid_str;
793 d_printf(_("Domain Name: %s\n"),
794 info->general.domain_name.string);
795 d_printf(_("Domain SID: %s\n"),
796 dom_sid_str_buf(domain_sid, &sid_str));
797 d_printf(_("Sequence number: %llu\n"),
798 (unsigned long long)info->general.sequence_num);
799 d_printf(_("Num users: %u\n"), info->general.num_users);
800 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
801 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
809 * 'net rpc info' entrypoint.
810 * @param argc Standard main() style argc.
811 * @param argv Standard main() style argv. Initial components are already
815 int net_rpc_info(struct net_context *c, int argc, const char **argv)
817 if (c->display_usage) {
822 _("Display information about the domain"));
826 net_warn_member_options();
828 return run_rpc_command(c, NULL, &ndr_table_samr,
829 NET_FLAGS_PDC, rpc_info_internals,
834 * Fetch domain SID into the local secrets.tdb.
836 * All parameters are provided by the run_rpc_command function, except for
837 * argc, argv which are passed through.
839 * @param domain_sid The domain sid acquired from the remote server.
840 * @param cli A cli_state connected to the server.
841 * @param mem_ctx Talloc context, destroyed on completion of the function.
842 * @param argc Standard main() style argc.
843 * @param argv Standard main() style argv. Initial components are already
846 * @return Normal NTSTATUS return.
849 static NTSTATUS rpc_getsid_internals(struct net_context *c,
850 const struct dom_sid *domain_sid,
851 const char *domain_name,
852 struct cli_state *cli,
853 struct rpc_pipe_client *pipe_hnd,
858 struct dom_sid_buf sid_str;
860 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
861 dom_sid_str_buf(domain_sid, &sid_str),
864 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
865 DEBUG(0,("Can't store domain SID\n"));
866 return NT_STATUS_UNSUCCESSFUL;
873 * 'net rpc getsid' entrypoint.
874 * @param argc Standard main() style argc.
875 * @param argv Standard main() style argv. Initial components are already
879 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
881 int conn_flags = NET_FLAGS_PDC;
883 if (!c->opt_user_specified && !c->opt_kerberos) {
884 conn_flags |= NET_FLAGS_ANONYMOUS;
887 if (c->display_usage) {
892 _("Fetch domain SID into local secrets.tdb"));
896 return run_rpc_command(c, NULL, &ndr_table_samr,
898 rpc_getsid_internals,
902 /****************************************************************************/
905 * Basic usage function for 'net rpc user'.
906 * @param argc Standard main() style argc.
907 * @param argv Standard main() style argv. Initial components are already
911 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
913 return net_user_usage(c, argc, argv);
917 * Add a new user to a remote RPC server.
919 * @param argc Standard main() style argc.
920 * @param argv Standard main() style argv. Initial components are already
923 * @return A shell status integer (0 for success).
926 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
928 NET_API_STATUS status;
929 struct USER_INFO_1 info1;
930 uint32_t parm_error = 0;
932 if (argc < 1 || c->display_usage) {
933 rpc_user_usage(c, argc, argv);
939 info1.usri1_name = argv[0];
941 info1.usri1_password = argv[1];
944 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
947 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
948 argv[0], libnetapi_get_error_string(c->netapi_ctx,
952 d_printf(_("Added user '%s'.\n"), argv[0]);
959 * Rename a user on a remote RPC server.
961 * @param argc Standard main() style argc.
962 * @param argv Standard main() style argv. Initial components are already
965 * @return A shell status integer (0 for success).
968 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
970 NET_API_STATUS status;
971 struct USER_INFO_0 u0;
972 uint32_t parm_err = 0;
974 if (argc != 2 || c->display_usage) {
975 rpc_user_usage(c, argc, argv);
979 u0.usri0_name = argv[1];
981 status = NetUserSetInfo(c->opt_host, argv[0],
982 0, (uint8_t *)&u0, &parm_err);
985 _("Failed to rename user from %s to %s - %s\n"),
987 libnetapi_get_error_string(c->netapi_ctx, status));
989 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
996 * Set a user's primary group
998 * @param argc Standard main() style argc.
999 * @param argv Standard main() style argv. Initial components are already
1002 * @return A shell status integer (0 for success).
1005 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
1008 NET_API_STATUS status;
1010 struct GROUP_INFO_2 *g2;
1011 struct USER_INFO_1051 u1051;
1012 uint32_t parm_err = 0;
1014 if (argc != 2 || c->display_usage) {
1015 rpc_user_usage(c, argc, argv);
1019 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
1021 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
1023 libnetapi_get_error_string(c->netapi_ctx, status));
1026 g2 = (struct GROUP_INFO_2 *)buffer;
1028 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
1030 NetApiBufferFree(buffer);
1032 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
1033 (uint8_t *)&u1051, &parm_err);
1036 _("Failed to set user's primary group %s to %s - "
1037 "%s\n"), argv[0], argv[1],
1038 libnetapi_get_error_string(c->netapi_ctx, status));
1040 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
1047 * Delete a user from a remote RPC server.
1049 * @param argc Standard main() style argc.
1050 * @param argv Standard main() style argv. Initial components are already
1053 * @return A shell status integer (0 for success).
1056 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
1058 NET_API_STATUS status;
1060 if (argc < 1 || c->display_usage) {
1061 rpc_user_usage(c, argc, argv);
1065 status = NetUserDel(c->opt_host, argv[0]);
1068 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
1070 libnetapi_get_error_string(c->netapi_ctx, status));
1073 d_printf(_("Deleted user '%s'.\n"), argv[0]);
1080 * Set a user's password on a remote RPC server.
1082 * @param argc Standard main() style argc.
1083 * @param argv Standard main() style argv. Initial components are already
1086 * @return A shell status integer (0 for success).
1089 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
1091 NET_API_STATUS status;
1092 char *prompt = NULL;
1093 struct USER_INFO_1003 u1003;
1094 uint32_t parm_err = 0;
1097 if (argc < 1 || c->display_usage) {
1098 rpc_user_usage(c, argc, argv);
1103 u1003.usri1003_password = argv[1];
1105 char pwd[256] = {0};
1106 ret = asprintf(&prompt, _("Enter new password for %s:"),
1112 ret = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
1118 u1003.usri1003_password = talloc_strdup(c, pwd);
1119 if (u1003.usri1003_password == NULL) {
1124 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
1126 /* Display results */
1129 _("Failed to set password for '%s' with error: %s.\n"),
1130 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1139 * List a user's groups from a remote RPC server.
1141 * @param argc Standard main() style argc.
1142 * @param argv Standard main() style argv. Initial components are already
1145 * @return A shell status integer (0 for success)
1148 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1151 NET_API_STATUS status;
1152 struct GROUP_USERS_INFO_0 *u0 = NULL;
1153 uint32_t entries_read = 0;
1154 uint32_t total_entries = 0;
1158 if (argc < 1 || c->display_usage) {
1159 rpc_user_usage(c, argc, argv);
1163 status = NetUserGetGroups(c->opt_host,
1166 (uint8_t **)(void *)&u0,
1172 _("Failed to get groups for '%s' with error: %s.\n"),
1173 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1178 for (i=0; i < entries_read; i++) {
1179 printf("%s\n", u0->grui0_name);
1187 * List users on a remote RPC server.
1189 * All parameters are provided by the run_rpc_command function, except for
1190 * argc, argv which are passed through.
1192 * @param domain_sid The domain sid acquired from the remote server.
1193 * @param cli A cli_state connected to the server.
1194 * @param mem_ctx Talloc context, destroyed on completion of the function.
1195 * @param argc Standard main() style argc.
1196 * @param argv Standard main() style argv. Initial components are already
1199 * @return Normal NTSTATUS return.
1202 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
1204 NET_API_STATUS status;
1205 uint32_t start_idx=0, num_entries, i, loop_count = 0;
1206 struct NET_DISPLAY_USER *info = NULL;
1207 void *buffer = NULL;
1209 /* Query domain users */
1210 if (c->opt_long_list_entries)
1211 d_printf(_("\nUser name Comment"
1212 "\n-----------------------------\n"));
1214 uint32_t max_entries, max_size;
1216 dcerpc_get_query_dispinfo_params(
1217 loop_count, &max_entries, &max_size);
1219 status = NetQueryDisplayInformation(c->opt_host,
1226 if (status != 0 && status != ERROR_MORE_DATA) {
1230 info = (struct NET_DISPLAY_USER *)buffer;
1232 for (i = 0; i < num_entries; i++) {
1234 if (c->opt_long_list_entries)
1235 printf("%-21.21s %s\n", info->usri1_name,
1236 info->usri1_comment);
1238 printf("%s\n", info->usri1_name);
1242 NetApiBufferFree(buffer);
1245 start_idx += num_entries;
1247 } while (status == ERROR_MORE_DATA);
1253 * 'net rpc user' entrypoint.
1254 * @param argc Standard main() style argc.
1255 * @param argv Standard main() style argv. Initial components are already
1259 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1261 NET_API_STATUS status;
1263 struct functable func[] = {
1268 N_("Add specified user"),
1269 N_("net rpc user add\n"
1270 " Add specified user")
1276 N_("List domain groups of user"),
1277 N_("net rpc user info\n"
1278 " List domain groups of user")
1284 N_("Remove specified user"),
1285 N_("net rpc user delete\n"
1286 " Remove specified user")
1292 N_("Change user password"),
1293 N_("net rpc user password\n"
1294 " Change user password")
1300 N_("Rename specified user"),
1301 N_("net rpc user rename\n"
1302 " Rename specified user")
1306 rpc_user_setprimarygroup,
1308 "Set a user's primary group",
1309 "net rpc user setprimarygroup\n"
1310 " Set a user's primary group"
1312 {NULL, NULL, 0, NULL, NULL}
1315 status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
1321 if (c->display_usage) {
1326 _("List all users"));
1327 net_display_usage_from_functable(func);
1331 return rpc_user_list(c, argc, argv);
1334 return net_run_function(c, argc, argv, "net rpc user", func);
1337 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1338 TALLOC_CTX *mem_ctx,
1339 struct rpc_sh_ctx *ctx,
1340 struct rpc_pipe_client *pipe_hnd,
1341 int argc, const char **argv)
1343 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1346 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1347 TALLOC_CTX *mem_ctx,
1348 struct rpc_sh_ctx *ctx,
1349 struct rpc_pipe_client *pipe_hnd,
1350 int argc, const char **argv)
1352 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1355 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1356 TALLOC_CTX *mem_ctx,
1357 struct rpc_sh_ctx *ctx,
1358 struct rpc_pipe_client *pipe_hnd,
1359 int argc, const char **argv,
1361 struct net_context *c,
1362 TALLOC_CTX *mem_ctx,
1363 struct rpc_sh_ctx *ctx,
1364 struct rpc_pipe_client *pipe_hnd,
1365 struct policy_handle *user_hnd,
1366 int argc, const char **argv))
1368 struct policy_handle connect_pol, domain_pol, user_pol;
1369 NTSTATUS status, result;
1372 enum lsa_SidType type;
1373 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1376 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1378 return NT_STATUS_INVALID_PARAMETER;
1381 ZERO_STRUCT(connect_pol);
1382 ZERO_STRUCT(domain_pol);
1383 ZERO_STRUCT(user_pol);
1385 status = net_rpc_lookup_name(c, mem_ctx, ctx->cli,
1386 argv[0], NULL, NULL, &sid, &type);
1387 if (!NT_STATUS_IS_OK(status)) {
1388 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1393 if (type != SID_NAME_USER) {
1394 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1395 sid_type_lookup(type));
1396 status = NT_STATUS_NO_SUCH_USER;
1400 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1401 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1402 status = NT_STATUS_NO_SUCH_USER;
1406 status = dcerpc_samr_Connect2(b, mem_ctx,
1408 MAXIMUM_ALLOWED_ACCESS,
1411 if (!NT_STATUS_IS_OK(status)) {
1414 if (!NT_STATUS_IS_OK(result)) {
1419 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1421 MAXIMUM_ALLOWED_ACCESS,
1425 if (!NT_STATUS_IS_OK(status)) {
1428 if (!NT_STATUS_IS_OK(result)) {
1433 status = dcerpc_samr_OpenUser(b, mem_ctx,
1435 MAXIMUM_ALLOWED_ACCESS,
1439 if (!NT_STATUS_IS_OK(status)) {
1442 if (!NT_STATUS_IS_OK(result)) {
1447 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1450 if (is_valid_policy_hnd(&user_pol)) {
1451 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1453 if (is_valid_policy_hnd(&domain_pol)) {
1454 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1456 if (is_valid_policy_hnd(&connect_pol)) {
1457 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1462 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1463 TALLOC_CTX *mem_ctx,
1464 struct rpc_sh_ctx *ctx,
1465 struct rpc_pipe_client *pipe_hnd,
1466 struct policy_handle *user_hnd,
1467 int argc, const char **argv)
1469 NTSTATUS status, result;
1470 union samr_UserInfo *info = NULL;
1471 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1474 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1476 return NT_STATUS_INVALID_PARAMETER;
1479 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1484 if (!NT_STATUS_IS_OK(status)) {
1487 if (!NT_STATUS_IS_OK(result)) {
1491 d_printf(_("user rid: %d, group rid: %d\n"),
1493 info->info21.primary_gid);
1498 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1499 TALLOC_CTX *mem_ctx,
1500 struct rpc_sh_ctx *ctx,
1501 struct rpc_pipe_client *pipe_hnd,
1502 int argc, const char **argv)
1504 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1505 rpc_sh_user_show_internals);
1508 #define FETCHSTR(name, rec) \
1509 do { if (strequal(ctx->thiscmd, name)) { \
1510 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1513 #define SETSTR(name, rec, flag) \
1514 do { if (strequal(ctx->thiscmd, name)) { \
1515 init_lsa_String(&(info->info21.rec), argv[0]); \
1516 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1519 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1520 TALLOC_CTX *mem_ctx,
1521 struct rpc_sh_ctx *ctx,
1522 struct rpc_pipe_client *pipe_hnd,
1523 struct policy_handle *user_hnd,
1524 int argc, const char **argv)
1526 NTSTATUS status, result;
1527 const char *username;
1528 const char *oldval = "";
1529 union samr_UserInfo *info = NULL;
1530 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1533 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1534 _("Usage:"), ctx->whoami);
1535 return NT_STATUS_INVALID_PARAMETER;
1538 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1543 if (!NT_STATUS_IS_OK(status)) {
1546 if (!NT_STATUS_IS_OK(result)) {
1550 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1552 FETCHSTR("fullname", full_name);
1553 FETCHSTR("homedir", home_directory);
1554 FETCHSTR("homedrive", home_drive);
1555 FETCHSTR("logonscript", logon_script);
1556 FETCHSTR("profilepath", profile_path);
1557 FETCHSTR("description", description);
1560 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1564 if (strcmp(argv[0], "NULL") == 0) {
1568 ZERO_STRUCT(info->info21);
1570 SETSTR("fullname", full_name, FULL_NAME);
1571 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1572 SETSTR("homedrive", home_drive, HOME_DRIVE);
1573 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1574 SETSTR("profilepath", profile_path, PROFILE_PATH);
1575 SETSTR("description", description, DESCRIPTION);
1577 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1582 if (!NT_STATUS_IS_OK(status)) {
1588 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1589 ctx->thiscmd, oldval, argv[0]);
1596 #define HANDLEFLG(name, rec) \
1597 do { if (strequal(ctx->thiscmd, name)) { \
1598 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1600 newflags = oldflags | ACB_##rec; \
1602 newflags = oldflags & ~ACB_##rec; \
1605 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1606 TALLOC_CTX *mem_ctx,
1607 struct rpc_sh_ctx *ctx,
1608 struct rpc_pipe_client *pipe_hnd,
1609 int argc, const char **argv)
1611 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1612 rpc_sh_user_str_edit_internals);
1615 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1616 TALLOC_CTX *mem_ctx,
1617 struct rpc_sh_ctx *ctx,
1618 struct rpc_pipe_client *pipe_hnd,
1619 struct policy_handle *user_hnd,
1620 int argc, const char **argv)
1622 NTSTATUS status, result;
1623 const char *username;
1624 const char *oldval = "unknown";
1625 uint32_t oldflags, newflags;
1627 union samr_UserInfo *info = NULL;
1628 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1631 ((argc == 1) && !strequal(argv[0], "yes") &&
1632 !strequal(argv[0], "no"))) {
1633 /* TRANSLATORS: The yes|no here are program keywords. Please do
1635 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1637 return NT_STATUS_INVALID_PARAMETER;
1640 newval = strequal(argv[0], "yes");
1642 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1647 if (!NT_STATUS_IS_OK(status)) {
1650 if (!NT_STATUS_IS_OK(result)) {
1654 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1655 oldflags = info->info21.acct_flags;
1656 newflags = info->info21.acct_flags;
1658 HANDLEFLG("disabled", DISABLED);
1659 HANDLEFLG("pwnotreq", PWNOTREQ);
1660 HANDLEFLG("autolock", AUTOLOCK);
1661 HANDLEFLG("pwnoexp", PWNOEXP);
1664 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1669 ZERO_STRUCT(info->info21);
1671 info->info21.acct_flags = newflags;
1672 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1674 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1679 if (!NT_STATUS_IS_OK(status)) {
1683 if (NT_STATUS_IS_OK(result)) {
1684 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1685 ctx->thiscmd, oldval, argv[0]);
1693 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1694 TALLOC_CTX *mem_ctx,
1695 struct rpc_sh_ctx *ctx,
1696 struct rpc_pipe_client *pipe_hnd,
1697 int argc, const char **argv)
1699 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1700 rpc_sh_user_flag_edit_internals);
1703 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1704 TALLOC_CTX *mem_ctx,
1705 struct rpc_sh_ctx *ctx)
1707 static struct rpc_sh_cmd cmds[] = {
1709 { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1710 N_("Show/Set a user's full name") },
1712 { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1713 N_("Show/Set a user's home directory") },
1715 { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1716 N_("Show/Set a user's home drive") },
1718 { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1719 N_("Show/Set a user's logon script") },
1721 { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1722 N_("Show/Set a user's profile path") },
1724 { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1725 N_("Show/Set a user's description") },
1727 { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1728 N_("Show/Set whether a user is disabled") },
1730 { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1731 N_("Show/Set whether a user locked out") },
1733 { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1734 N_("Show/Set whether a user does not need a password") },
1736 { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1737 N_("Show/Set whether a user's password does not expire") },
1739 { NULL, NULL, 0, NULL, NULL }
1745 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1746 TALLOC_CTX *mem_ctx,
1747 struct rpc_sh_ctx *ctx)
1749 static struct rpc_sh_cmd cmds[] = {
1751 { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
1752 N_("List available users") },
1754 { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
1755 N_("List the domain groups a user is member of") },
1757 { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
1758 N_("Show info about a user") },
1760 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1761 N_("Show/Modify a user's fields") },
1763 { NULL, NULL, 0, NULL, NULL }
1769 /****************************************************************************/
1772 * Basic usage function for 'net rpc group'.
1773 * @param argc Standard main() style argc.
1774 * @param argv Standard main() style argv. Initial components are already
1778 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1780 return net_group_usage(c, argc, argv);
1784 * Delete group on a remote RPC server.
1786 * All parameters are provided by the run_rpc_command function, except for
1787 * argc, argv which are passed through.
1789 * @param domain_sid The domain sid acquired from the remote server.
1790 * @param cli A cli_state connected to the server.
1791 * @param mem_ctx Talloc context, destroyed on completion of the function.
1792 * @param argc Standard main() style argc.
1793 * @param argv Standard main() style argv. Initial components are already
1796 * @return Normal NTSTATUS return.
1799 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1800 const struct dom_sid *domain_sid,
1801 const char *domain_name,
1802 struct cli_state *cli,
1803 struct rpc_pipe_client *pipe_hnd,
1804 TALLOC_CTX *mem_ctx,
1808 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1809 bool group_is_primary = false;
1810 NTSTATUS status, result;
1812 struct samr_RidAttrArray *rids = NULL;
1815 /* struct samr_RidWithAttribute *user_gids; */
1816 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1818 struct samr_Ids group_rids, name_types;
1819 struct lsa_String lsa_acct_name;
1820 union samr_UserInfo *info = NULL;
1822 if (argc < 1 || c->display_usage) {
1823 rpc_group_usage(c, argc,argv);
1824 return NT_STATUS_OK; /* ok? */
1827 status = dcerpc_samr_Connect2(b, mem_ctx,
1829 MAXIMUM_ALLOWED_ACCESS,
1832 if (!NT_STATUS_IS_OK(status)) {
1833 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1837 if (!NT_STATUS_IS_OK(result)) {
1839 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1843 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1845 MAXIMUM_ALLOWED_ACCESS,
1846 discard_const_p(struct dom_sid2, domain_sid),
1849 if (!NT_STATUS_IS_OK(status)) {
1850 d_fprintf(stderr, _("Request open_domain failed\n"));
1854 if (!NT_STATUS_IS_OK(result)) {
1856 d_fprintf(stderr, _("Request open_domain failed\n"));
1860 init_lsa_String(&lsa_acct_name, argv[0]);
1862 status = dcerpc_samr_LookupNames(b, mem_ctx,
1869 if (!NT_STATUS_IS_OK(status)) {
1870 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1874 if (!NT_STATUS_IS_OK(result)) {
1876 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1879 if (group_rids.count != 1) {
1880 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1883 if (name_types.count != 1) {
1884 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1888 switch (name_types.ids[0])
1890 case SID_NAME_DOM_GRP:
1891 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1893 MAXIMUM_ALLOWED_ACCESS,
1897 if (!NT_STATUS_IS_OK(status)) {
1898 d_fprintf(stderr, _("Request open_group failed"));
1902 if (!NT_STATUS_IS_OK(result)) {
1904 d_fprintf(stderr, _("Request open_group failed"));
1908 group_rid = group_rids.ids[0];
1910 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1914 if (!NT_STATUS_IS_OK(status)) {
1916 _("Unable to query group members of %s"),
1921 if (!NT_STATUS_IS_OK(result)) {
1924 _("Unable to query group members of %s"),
1929 if (c->opt_verbose) {
1931 _("Domain Group %s (rid: %d) has %d members\n"),
1932 argv[0],group_rid, rids->count);
1935 /* Check if group is anyone's primary group */
1936 for (i = 0; i < rids->count; i++)
1938 status = dcerpc_samr_OpenUser(b, mem_ctx,
1940 MAXIMUM_ALLOWED_ACCESS,
1944 if (!NT_STATUS_IS_OK(status)) {
1946 _("Unable to open group member %d\n"),
1951 if (!NT_STATUS_IS_OK(result)) {
1954 _("Unable to open group member %d\n"),
1959 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1964 if (!NT_STATUS_IS_OK(status)) {
1966 _("Unable to lookup userinfo for group "
1972 if (!NT_STATUS_IS_OK(result)) {
1975 _("Unable to lookup userinfo for group "
1981 if (info->info21.primary_gid == group_rid) {
1982 if (c->opt_verbose) {
1983 d_printf(_("Group is primary group "
1985 info->info21.account_name.string);
1987 group_is_primary = true;
1990 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1993 if (group_is_primary) {
1994 d_fprintf(stderr, _("Unable to delete group because "
1995 "some of it's members have it as primary "
1997 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
2001 /* remove all group members */
2002 for (i = 0; i < rids->count; i++)
2005 d_printf(_("Remove group member %d..."),
2007 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2011 if (!NT_STATUS_IS_OK(status)) {
2015 if (NT_STATUS_IS_OK(result)) {
2017 d_printf(_("ok\n"));
2020 d_printf("%s\n", _("failed"));
2025 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2028 if (!NT_STATUS_IS_OK(status)) {
2035 /* removing a local group is easier... */
2036 case SID_NAME_ALIAS:
2037 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2039 MAXIMUM_ALLOWED_ACCESS,
2043 if (!NT_STATUS_IS_OK(status)) {
2044 d_fprintf(stderr, _("Request open_alias failed\n"));
2047 if (!NT_STATUS_IS_OK(result)) {
2049 d_fprintf(stderr, _("Request open_alias failed\n"));
2053 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
2056 if (!NT_STATUS_IS_OK(status)) {
2064 d_fprintf(stderr, _("%s is of type %s. This command is only "
2065 "for deleting local or global groups\n"),
2066 argv[0],sid_type_lookup(name_types.ids[0]));
2067 status = NT_STATUS_UNSUCCESSFUL;
2071 if (NT_STATUS_IS_OK(status)) {
2073 d_printf(_("Deleted %s '%s'\n"),
2074 sid_type_lookup(name_types.ids[0]), argv[0]);
2076 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
2077 get_friendly_nt_error_msg(status));
2085 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
2087 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2088 rpc_group_delete_internals, argc,argv);
2091 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
2093 NET_API_STATUS status;
2094 struct GROUP_INFO_1 info1;
2095 uint32_t parm_error = 0;
2097 if (argc != 1 || c->display_usage) {
2098 rpc_group_usage(c, argc, argv);
2104 info1.grpi1_name = argv[0];
2105 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2106 info1.grpi1_comment = c->opt_comment;
2109 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2113 _("Failed to add group '%s' with error: %s.\n"),
2114 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2118 d_printf(_("Added group '%s'.\n"), argv[0]);
2124 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
2126 NET_API_STATUS status;
2127 struct LOCALGROUP_INFO_1 info1;
2128 uint32_t parm_error = 0;
2130 if (argc != 1 || c->display_usage) {
2131 rpc_group_usage(c, argc, argv);
2137 info1.lgrpi1_name = argv[0];
2138 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2139 info1.lgrpi1_comment = c->opt_comment;
2142 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2146 _("Failed to add alias '%s' with error: %s.\n"),
2147 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2151 d_printf(_("Added alias '%s'.\n"), argv[0]);
2157 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
2159 if (c->opt_localgroup)
2160 return rpc_alias_add_internals(c, argc, argv);
2162 return rpc_group_add_internals(c, argc, argv);
2165 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2166 TALLOC_CTX *mem_ctx,
2168 struct dom_sid *sid,
2169 enum lsa_SidType *type)
2171 struct dom_sid *sids = NULL;
2172 enum lsa_SidType *types = NULL;
2173 struct rpc_pipe_client *pipe_hnd = NULL;
2174 struct policy_handle lsa_pol;
2175 NTSTATUS status, result;
2176 struct dcerpc_binding_handle *b;
2178 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
2180 if (!NT_STATUS_IS_OK(status)) {
2184 b = pipe_hnd->binding_handle;
2186 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2187 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2189 if (!NT_STATUS_IS_OK(status)) {
2193 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2194 &name, NULL, 1, &sids, &types);
2196 if (NT_STATUS_IS_OK(status)) {
2197 sid_copy(sid, &sids[0]);
2201 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
2205 TALLOC_FREE(pipe_hnd);
2208 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
2210 /* Try as S-1-5-whatever */
2212 struct dom_sid tmp_sid;
2214 if (string_to_sid(&tmp_sid, name)) {
2215 sid_copy(sid, &tmp_sid);
2216 *type = SID_NAME_UNKNOWN;
2217 status = NT_STATUS_OK;
2224 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2225 TALLOC_CTX *mem_ctx,
2226 const struct dom_sid *group_sid,
2229 struct policy_handle connect_pol, domain_pol;
2230 NTSTATUS status, result;
2232 struct policy_handle group_pol;
2233 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2235 struct samr_Ids rids, rid_types;
2236 struct lsa_String lsa_acct_name;
2240 sid_copy(&sid, group_sid);
2242 if (!sid_split_rid(&sid, &group_rid)) {
2243 return NT_STATUS_UNSUCCESSFUL;
2246 /* Get sam policy handle */
2247 status = dcerpc_samr_Connect2(b, mem_ctx,
2249 MAXIMUM_ALLOWED_ACCESS,
2252 if (!NT_STATUS_IS_OK(status)) {
2255 if (!NT_STATUS_IS_OK(result)) {
2259 /* Get domain policy handle */
2260 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2262 MAXIMUM_ALLOWED_ACCESS,
2266 if (!NT_STATUS_IS_OK(status)) {
2269 if (!NT_STATUS_IS_OK(result)) {
2273 init_lsa_String(&lsa_acct_name, member);
2275 status = dcerpc_samr_LookupNames(b, mem_ctx,
2282 if (!NT_STATUS_IS_OK(status)) {
2283 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2288 if (!NT_STATUS_IS_OK(result)) {
2290 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2294 if (rids.count != 1) {
2295 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2298 if (rid_types.count != 1) {
2299 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2303 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2305 MAXIMUM_ALLOWED_ACCESS,
2309 if (!NT_STATUS_IS_OK(status)) {
2313 if (!NT_STATUS_IS_OK(result)) {
2318 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2321 0x0005, /* unknown flags */
2323 if (!NT_STATUS_IS_OK(status)) {
2330 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2334 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2335 struct cli_state *cli,
2336 TALLOC_CTX *mem_ctx,
2337 const struct dom_sid *alias_sid,
2340 struct policy_handle connect_pol, domain_pol;
2341 NTSTATUS status, result;
2343 struct policy_handle alias_pol;
2344 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2346 struct dom_sid member_sid;
2347 enum lsa_SidType member_type;
2351 sid_copy(&sid, alias_sid);
2353 if (!sid_split_rid(&sid, &alias_rid)) {
2354 return NT_STATUS_UNSUCCESSFUL;
2357 result = get_sid_from_name(cli, mem_ctx,
2358 member, &member_sid, &member_type);
2360 if (!NT_STATUS_IS_OK(result)) {
2361 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2366 /* Get sam policy handle */
2367 status = dcerpc_samr_Connect2(b, mem_ctx,
2369 MAXIMUM_ALLOWED_ACCESS,
2372 if (!NT_STATUS_IS_OK(status)) {
2375 if (!NT_STATUS_IS_OK(result)) {
2380 /* Get domain policy handle */
2381 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2383 MAXIMUM_ALLOWED_ACCESS,
2387 if (!NT_STATUS_IS_OK(status)) {
2390 if (!NT_STATUS_IS_OK(result)) {
2395 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2397 MAXIMUM_ALLOWED_ACCESS,
2401 if (!NT_STATUS_IS_OK(status)) {
2404 if (!NT_STATUS_IS_OK(result)) {
2408 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2412 if (!NT_STATUS_IS_OK(status)) {
2419 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2423 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2424 const struct dom_sid *domain_sid,
2425 const char *domain_name,
2426 struct cli_state *cli,
2427 struct rpc_pipe_client *pipe_hnd,
2428 TALLOC_CTX *mem_ctx,
2432 struct dom_sid group_sid;
2433 enum lsa_SidType group_type;
2435 if (argc != 2 || c->display_usage) {
2438 _("net rpc group addmem <group> <member>\n"
2439 " Add a member to a group\n"
2440 " group\tGroup to add member to\n"
2441 " member\tMember to add to group\n"));
2442 return NT_STATUS_UNSUCCESSFUL;
2445 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2446 &group_sid, &group_type))) {
2447 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2449 return NT_STATUS_UNSUCCESSFUL;
2452 if (group_type == SID_NAME_DOM_GRP) {
2453 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2454 &group_sid, argv[1]);
2456 if (!NT_STATUS_IS_OK(result)) {
2457 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2458 argv[1], argv[0], nt_errstr(result));
2463 if (group_type == SID_NAME_ALIAS) {
2464 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, cli, mem_ctx,
2465 &group_sid, argv[1]);
2467 if (!NT_STATUS_IS_OK(result)) {
2468 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2469 argv[1], argv[0], nt_errstr(result));
2474 d_fprintf(stderr, _("Can only add members to global or local groups "
2475 "which %s is not\n"), argv[0]);
2477 return NT_STATUS_UNSUCCESSFUL;
2480 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2482 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2483 rpc_group_addmem_internals,
2487 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2488 struct rpc_pipe_client *pipe_hnd,
2489 TALLOC_CTX *mem_ctx,
2490 const struct dom_sid *group_sid,
2493 struct policy_handle connect_pol, domain_pol;
2494 NTSTATUS status, result;
2496 struct policy_handle group_pol;
2497 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2499 struct samr_Ids rids, rid_types;
2500 struct lsa_String lsa_acct_name;
2504 sid_copy(&sid, group_sid);
2506 if (!sid_split_rid(&sid, &group_rid))
2507 return NT_STATUS_UNSUCCESSFUL;
2509 /* Get sam policy handle */
2510 status = dcerpc_samr_Connect2(b, mem_ctx,
2512 MAXIMUM_ALLOWED_ACCESS,
2515 if (!NT_STATUS_IS_OK(status)) {
2518 if (!NT_STATUS_IS_OK(result)) {
2523 /* Get domain policy handle */
2524 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2526 MAXIMUM_ALLOWED_ACCESS,
2530 if (!NT_STATUS_IS_OK(status)) {
2533 if (!NT_STATUS_IS_OK(result)) {
2537 init_lsa_String(&lsa_acct_name, member);
2539 status = dcerpc_samr_LookupNames(b, mem_ctx,
2546 if (!NT_STATUS_IS_OK(status)) {
2547 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2552 if (!NT_STATUS_IS_OK(result)) {
2554 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2558 if (rids.count != 1) {
2559 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2562 if (rid_types.count != 1) {
2563 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2567 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2569 MAXIMUM_ALLOWED_ACCESS,
2573 if (!NT_STATUS_IS_OK(status)) {
2576 if (!NT_STATUS_IS_OK(result)) {
2581 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2585 if (!NT_STATUS_IS_OK(status)) {
2591 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2595 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2596 struct cli_state *cli,
2597 TALLOC_CTX *mem_ctx,
2598 const struct dom_sid *alias_sid,
2601 struct policy_handle connect_pol, domain_pol;
2602 NTSTATUS status, result;
2604 struct policy_handle alias_pol;
2605 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2607 struct dom_sid member_sid;
2608 enum lsa_SidType member_type;
2612 sid_copy(&sid, alias_sid);
2614 if (!sid_split_rid(&sid, &alias_rid))
2615 return NT_STATUS_UNSUCCESSFUL;
2617 result = get_sid_from_name(cli, mem_ctx,
2618 member, &member_sid, &member_type);
2620 if (!NT_STATUS_IS_OK(result)) {
2621 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2626 /* Get sam policy handle */
2627 status = dcerpc_samr_Connect2(b, mem_ctx,
2629 MAXIMUM_ALLOWED_ACCESS,
2632 if (!NT_STATUS_IS_OK(status)) {
2635 if (!NT_STATUS_IS_OK(result)) {
2640 /* Get domain policy handle */
2641 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2643 MAXIMUM_ALLOWED_ACCESS,
2647 if (!NT_STATUS_IS_OK(status)) {
2650 if (!NT_STATUS_IS_OK(result)) {
2655 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2657 MAXIMUM_ALLOWED_ACCESS,
2661 if (!NT_STATUS_IS_OK(status)) {
2665 if (!NT_STATUS_IS_OK(result)) {
2669 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2674 if (!NT_STATUS_IS_OK(status)) {
2681 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2685 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2686 const struct dom_sid *domain_sid,
2687 const char *domain_name,
2688 struct cli_state *cli,
2689 struct rpc_pipe_client *pipe_hnd,
2690 TALLOC_CTX *mem_ctx,
2694 struct dom_sid group_sid;
2695 enum lsa_SidType group_type;
2697 if (argc != 2 || c->display_usage) {
2700 _("net rpc group delmem <group> <member>\n"
2701 " Delete a member from a group\n"
2702 " group\tGroup to delete member from\n"
2703 " member\tMember to delete from group\n"));
2704 return NT_STATUS_UNSUCCESSFUL;
2707 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2708 &group_sid, &group_type))) {
2709 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2711 return NT_STATUS_UNSUCCESSFUL;
2714 if (group_type == SID_NAME_DOM_GRP) {
2715 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2716 &group_sid, argv[1]);
2718 if (!NT_STATUS_IS_OK(result)) {
2719 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2720 argv[1], argv[0], nt_errstr(result));
2725 if (group_type == SID_NAME_ALIAS) {
2726 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, cli, mem_ctx,
2727 &group_sid, argv[1]);
2729 if (!NT_STATUS_IS_OK(result)) {
2730 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2731 argv[1], argv[0], nt_errstr(result));
2736 d_fprintf(stderr, _("Can only delete members from global or local "
2737 "groups which %s is not\n"), argv[0]);
2739 return NT_STATUS_UNSUCCESSFUL;
2742 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2744 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2745 rpc_group_delmem_internals,
2750 * List groups on a remote RPC server.
2752 * All parameters are provided by the run_rpc_command function, except for
2753 * argc, argv which are passes through.
2755 * @param domain_sid The domain sid acquired from the remote server.
2756 * @param cli A cli_state connected to the server.
2757 * @param mem_ctx Talloc context, destroyed on completion of the function.
2758 * @param argc Standard main() style argc.
2759 * @param argv Standard main() style argv. Initial components are already
2762 * @return Normal NTSTATUS return.
2765 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2766 const struct dom_sid *domain_sid,
2767 const char *domain_name,
2768 struct cli_state *cli,
2769 struct rpc_pipe_client *pipe_hnd,
2770 TALLOC_CTX *mem_ctx,
2774 struct policy_handle connect_pol, domain_pol;
2775 NTSTATUS status, result;
2776 uint32_t start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2777 struct samr_SamArray *groups = NULL;
2778 bool global = false;
2780 bool builtin = false;
2781 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2783 if (c->display_usage) {
2786 _("net rpc group list [global] [local] [builtin]\n"
2787 " List groups on RPC server\n"
2788 " global\tList global groups\n"
2789 " local\tList local groups\n"
2790 " builtin\tList builtin groups\n"
2791 " If none of global, local or builtin is "
2792 "specified, all three options are considered "
2794 return NT_STATUS_OK;
2803 for (i=0; i<argc; i++) {
2804 if (strequal(argv[i], "global"))
2807 if (strequal(argv[i], "local"))
2810 if (strequal(argv[i], "builtin"))
2814 /* Get sam policy handle */
2816 status = dcerpc_samr_Connect2(b, mem_ctx,
2818 MAXIMUM_ALLOWED_ACCESS,
2821 if (!NT_STATUS_IS_OK(status)) {
2824 if (!NT_STATUS_IS_OK(result)) {
2829 /* Get domain policy handle */
2831 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2833 MAXIMUM_ALLOWED_ACCESS,
2834 discard_const_p(struct dom_sid2, domain_sid),
2837 if (!NT_STATUS_IS_OK(status)) {
2840 if (!NT_STATUS_IS_OK(result)) {
2845 /* Query domain groups */
2846 if (c->opt_long_list_entries)
2847 d_printf(_("\nGroup name Comment"
2848 "\n-----------------------------\n"));
2850 uint32_t max_size, total_size, returned_size;
2851 union samr_DispInfo info;
2855 dcerpc_get_query_dispinfo_params(
2856 loop_count, &max_entries, &max_size);
2858 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2868 if (!NT_STATUS_IS_OK(status)) {
2871 num_entries = info.info3.count;
2872 start_idx += info.info3.count;
2874 if (!NT_STATUS_IS_OK(result) &&
2875 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2878 for (i = 0; i < num_entries; i++) {
2880 const char *group = NULL;
2881 const char *desc = NULL;
2883 group = info.info3.entries[i].account_name.string;
2884 desc = info.info3.entries[i].description.string;
2886 if (c->opt_long_list_entries)
2887 printf("%-21.21s %-50.50s\n",
2890 printf("%s\n", group);
2892 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2893 /* query domain aliases */
2898 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2905 if (!NT_STATUS_IS_OK(status)) {
2908 if (!NT_STATUS_IS_OK(result) &&
2909 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2912 for (i = 0; i < num_entries; i++) {
2914 const char *description = NULL;
2916 if (c->opt_long_list_entries) {
2918 struct policy_handle alias_pol;
2919 union samr_AliasInfo *info = NULL;
2922 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2925 groups->entries[i].idx,
2928 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2929 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2934 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2935 status = dcerpc_samr_Close(b, mem_ctx,
2938 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2939 description = info->description.string;
2945 if (description != NULL) {
2946 printf("%-21.21s %-50.50s\n",
2947 groups->entries[i].name.string,
2950 printf("%s\n", groups->entries[i].name.string);
2953 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2954 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2955 /* Get builtin policy handle */
2957 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2959 MAXIMUM_ALLOWED_ACCESS,
2960 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2963 if (!NT_STATUS_IS_OK(status)) {
2966 if (!NT_STATUS_IS_OK(result)) {
2971 /* query builtin aliases */
2974 if (!builtin) break;
2976 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2983 if (!NT_STATUS_IS_OK(status)) {
2986 if (!NT_STATUS_IS_OK(result) &&
2987 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2992 for (i = 0; i < num_entries; i++) {
2994 const char *description = NULL;
2996 if (c->opt_long_list_entries) {
2998 struct policy_handle alias_pol;
2999 union samr_AliasInfo *info = NULL;
3002 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3005 groups->entries[i].idx,
3008 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
3009 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
3014 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
3015 status = dcerpc_samr_Close(b, mem_ctx,
3018 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
3019 description = info->description.string;
3025 if (description != NULL) {
3026 printf("%-21.21s %-50.50s\n",
3027 groups->entries[i].name.string,
3030 printf("%s\n", groups->entries[i].name.string);
3033 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3041 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
3043 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3044 rpc_group_list_internals,
3048 static NTSTATUS rpc_list_group_members(struct net_context *c,
3049 struct rpc_pipe_client *pipe_hnd,
3050 TALLOC_CTX *mem_ctx,
3051 const char *domain_name,
3052 const struct dom_sid *domain_sid,
3053 struct policy_handle *domain_pol,
3056 NTSTATUS result, status;
3057 struct policy_handle group_pol;
3058 uint32_t num_members, *group_rids;
3060 struct samr_RidAttrArray *rids = NULL;
3061 struct lsa_Strings names;
3062 struct samr_Ids types;
3063 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3065 status = dcerpc_samr_OpenGroup(b, mem_ctx,
3067 MAXIMUM_ALLOWED_ACCESS,
3071 if (!NT_STATUS_IS_OK(status)) {
3074 if (!NT_STATUS_IS_OK(result)) {
3078 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
3082 if (!NT_STATUS_IS_OK(status)) {
3085 if (!NT_STATUS_IS_OK(result)) {
3089 num_members = rids->count;
3090 group_rids = rids->rids;
3092 while (num_members > 0) {
3093 uint32_t this_time = 512;
3095 if (num_members < this_time)
3096 this_time = num_members;
3098 status = dcerpc_samr_LookupRids(b, mem_ctx,
3105 if (!NT_STATUS_IS_OK(status)) {
3108 if (!NT_STATUS_IS_OK(result)) {
3111 if (names.count != this_time) {
3112 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3114 if (types.count != this_time) {
3115 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3117 /* We only have users as members, but make the output
3118 the same as the output of alias members */
3120 for (i = 0; i < this_time; i++) {
3122 if (c->opt_long_list_entries) {
3124 struct dom_sid_buf sid_str;
3126 sid_compose(&sid, domain_sid, group_rids[i]);
3128 printf("%s %s\\%s %d\n",
3129 dom_sid_str_buf(&sid, &sid_str),
3131 names.names[i].string,
3134 printf("%s\\%s\n", domain_name,
3135 names.names[i].string);
3139 num_members -= this_time;
3143 return NT_STATUS_OK;
3146 static NTSTATUS rpc_list_alias_members(struct net_context *c,
3147 struct rpc_pipe_client *pipe_hnd,
3148 struct cli_state *cli,
3149 TALLOC_CTX *mem_ctx,
3150 struct policy_handle *domain_pol,
3153 NTSTATUS result, status;
3154 struct rpc_pipe_client *lsa_pipe;
3155 struct policy_handle alias_pol, lsa_pol;
3156 uint32_t num_members;
3157 struct dom_sid *alias_sids;
3160 enum lsa_SidType *types;
3162 struct lsa_SidArray sid_array;
3163 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3165 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3167 MAXIMUM_ALLOWED_ACCESS,
3171 if (!NT_STATUS_IS_OK(status)) {
3174 if (!NT_STATUS_IS_OK(result)) {
3178 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
3182 if (!NT_STATUS_IS_OK(status)) {
3183 d_fprintf(stderr, _("Couldn't list alias members\n"));
3186 if (!NT_STATUS_IS_OK(result)) {
3187 d_fprintf(stderr, _("Couldn't list alias members\n"));
3191 num_members = sid_array.num_sids;
3193 if (num_members == 0) {
3194 return NT_STATUS_OK;
3197 result = cli_rpc_pipe_open_noauth(cli,
3200 if (!NT_STATUS_IS_OK(result)) {
3201 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
3202 nt_errstr(result) );
3206 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
3207 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
3209 if (!NT_STATUS_IS_OK(result)) {
3210 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
3211 TALLOC_FREE(lsa_pipe);
3215 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
3217 d_fprintf(stderr, _("Out of memory\n"));
3218 TALLOC_FREE(lsa_pipe);
3219 return NT_STATUS_NO_MEMORY;
3222 for (i=0; i<num_members; i++) {
3223 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
3226 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
3227 num_members, alias_sids,
3228 &domains, &names, &types);
3230 if (!NT_STATUS_IS_OK(result) &&
3231 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3232 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3233 TALLOC_FREE(lsa_pipe);
3237 for (i = 0; i < num_members; i++) {
3238 struct dom_sid_buf sid_str;
3239 dom_sid_str_buf(&alias_sids[i], &sid_str);
3241 if (c->opt_long_list_entries) {
3242 printf("%s %s\\%s %d\n", sid_str.buf,
3243 domains[i] ? domains[i] : _("*unknown*"),
3244 names[i] ? names[i] : _("*unknown*"), types[i]);
3247 printf("%s\\%s\n", domains[i], names[i]);
3249 printf("%s\n", sid_str.buf);
3253 TALLOC_FREE(lsa_pipe);
3254 return NT_STATUS_OK;
3257 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3258 const struct dom_sid *domain_sid,
3259 const char *domain_name,
3260 struct cli_state *cli,
3261 struct rpc_pipe_client *pipe_hnd,
3262 TALLOC_CTX *mem_ctx,
3266 NTSTATUS result, status;
3267 struct policy_handle connect_pol, domain_pol;
3268 struct samr_Ids rids, rid_types;
3269 struct lsa_String lsa_acct_name;
3270 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3272 /* Get sam policy handle */
3274 status = dcerpc_samr_Connect2(b, mem_ctx,
3276 MAXIMUM_ALLOWED_ACCESS,
3279 if (!NT_STATUS_IS_OK(status)) {
3282 if (!NT_STATUS_IS_OK(result)) {
3286 /* Get domain policy handle */
3288 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3290 MAXIMUM_ALLOWED_ACCESS,
3291 discard_const_p(struct dom_sid2, domain_sid),
3294 if (!NT_STATUS_IS_OK(status)) {
3297 if (!NT_STATUS_IS_OK(result)) {
3301 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3303 status = dcerpc_samr_LookupNames(b, mem_ctx,
3310 if (!NT_STATUS_IS_OK(status)) {
3314 if (!NT_STATUS_IS_OK(result)) {
3316 /* Ok, did not find it in the global sam, try with builtin */
3318 struct dom_sid sid_Builtin;
3320 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3322 sid_copy(&sid_Builtin, &global_sid_Builtin);
3324 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3326 MAXIMUM_ALLOWED_ACCESS,
3330 if (!NT_STATUS_IS_OK(status)) {
3333 if (!NT_STATUS_IS_OK(result)) {
3334 d_fprintf(stderr, _("Couldn't find group %s\n"),
3339 status = dcerpc_samr_LookupNames(b, mem_ctx,
3346 if (!NT_STATUS_IS_OK(status)) {
3349 if (!NT_STATUS_IS_OK(result)) {
3350 d_fprintf(stderr, _("Couldn't find group %s\n"),
3356 if (rids.count != 1) {
3357 d_fprintf(stderr, _("Couldn't find group %s\n"),
3359 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3361 if (rid_types.count != 1) {
3362 d_fprintf(stderr, _("Couldn't find group %s\n"),
3364 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3368 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3369 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3370 domain_sid, &domain_pol,
3374 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3375 return rpc_list_alias_members(c, pipe_hnd, cli, mem_ctx, &domain_pol,
3379 return NT_STATUS_NO_SUCH_GROUP;
3382 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3384 if (argc != 1 || c->display_usage) {
3385 return rpc_group_usage(c, argc, argv);
3388 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3389 rpc_group_members_internals,
3393 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3395 NET_API_STATUS status;
3396 struct GROUP_INFO_0 g0;
3400 d_printf(_("Usage:\n"));
3401 d_printf("net rpc group rename group newname\n");
3405 g0.grpi0_name = argv[1];
3407 status = NetGroupSetInfo(c->opt_host,
3414 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3415 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3423 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3425 if (argc != 2 || c->display_usage) {
3426 return rpc_group_usage(c, argc, argv);
3429 return rpc_group_rename_internals(c, argc, argv);
3433 * 'net rpc group' entrypoint.
3434 * @param argc Standard main() style argc.
3435 * @param argv Standard main() style argv. Initial components are already
3439 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3441 NET_API_STATUS status;
3443 struct functable func[] = {
3448 N_("Create specified group"),
3449 N_("net rpc group add\n"
3450 " Create specified group")
3456 N_("Delete specified group"),
3457 N_("net rpc group delete\n"
3458 " Delete specified group")
3464 N_("Add member to group"),
3465 N_("net rpc group addmem\n"
3466 " Add member to group")
3472 N_("Remove member from group"),
3473 N_("net rpc group delmem\n"
3474 " Remove member from group")
3481 N_("net rpc group list\n"
3488 N_("List group members"),
3489 N_("net rpc group members\n"
3490 " List group members")
3497 N_("net rpc group rename\n"
3500 {NULL, NULL, 0, NULL, NULL}
3503 status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
3509 if (c->display_usage) {
3510 d_printf(_("Usage:\n"));
3511 d_printf(_("net rpc group\n"
3512 " Alias for net rpc group list global "
3513 "local builtin\n"));
3514 net_display_usage_from_functable(func);
3518 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3519 rpc_group_list_internals,
3523 return net_run_function(c, argc, argv, "net rpc group", func);
3526 /****************************************************************************/
3528 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3530 return net_share_usage(c, argc, argv);
3534 * Add a share on a remote RPC server.
3536 * @param argc Standard main() style argc.
3537 * @param argv Standard main() style argv. Initial components are already
3540 * @return A shell status integer (0 for success).
3543 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3545 NET_API_STATUS status;
3548 uint32_t type = STYPE_DISKTREE; /* only allow disk shares to be added */
3549 uint32_t num_users=0, perms=0;
3550 char *password=NULL; /* don't allow a share password */
3551 struct SHARE_INFO_2 i2;
3552 uint32_t parm_error = 0;
3554 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3555 return rpc_share_usage(c, argc, argv);
3558 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3562 path = strchr(sharename, '=');
3569 i2.shi2_netname = sharename;
3570 i2.shi2_type = type;
3571 i2.shi2_remark = c->opt_comment;
3572 i2.shi2_permissions = perms;
3573 i2.shi2_max_uses = c->opt_maxusers;
3574 i2.shi2_current_uses = num_users;
3575 i2.shi2_path = path;
3576 i2.shi2_passwd = password;
3578 status = NetShareAdd(c->opt_host,
3583 printf(_("NetShareAdd failed with: %s\n"),
3584 libnetapi_get_error_string(c->netapi_ctx, status));
3591 * Delete a share on a remote RPC server.
3593 * @param domain_sid The domain sid acquired from the remote server.
3594 * @param argc Standard main() style argc.
3595 * @param argv Standard main() style argv. Initial components are already
3598 * @return A shell status integer (0 for success).
3600 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3602 if (argc < 1 || c->display_usage) {
3603 return rpc_share_usage(c, argc, argv);
3606 return NetShareDel(c->opt_host, argv[0], 0);
3610 * Formatted print of share info
3612 * @param r pointer to SHARE_INFO_1 to format
3615 static void display_share_info_1(struct net_context *c,
3616 struct SHARE_INFO_1 *r)
3618 if (c->opt_long_list_entries) {
3619 d_printf("%-12s %-8.8s %-50s\n",
3621 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3624 d_printf("%s\n", r->shi1_netname);
3628 static WERROR get_share_info(struct net_context *c,
3629 struct rpc_pipe_client *pipe_hnd,
3630 TALLOC_CTX *mem_ctx,
3634 struct srvsvc_NetShareInfoCtr *info_ctr)
3638 union srvsvc_NetShareInfo info;
3639 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3641 /* no specific share requested, enumerate all */
3644 uint32_t preferred_len = 0xffffffff;
3645 uint32_t total_entries = 0;
3646 uint32_t resume_handle = 0;
3648 info_ctr->level = level;
3650 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3657 if (!NT_STATUS_IS_OK(status)) {
3658 return ntstatus_to_werror(status);
3663 /* request just one share */
3664 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3671 if (!NT_STATUS_IS_OK(status)) {
3672 result = ntstatus_to_werror(status);
3676 if (!W_ERROR_IS_OK(result)) {
3681 ZERO_STRUCTP(info_ctr);
3683 info_ctr->level = level;
3688 struct srvsvc_NetShareCtr1 *ctr1;
3690 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3691 W_ERROR_HAVE_NO_MEMORY(ctr1);
3694 ctr1->array = info.info1;
3696 info_ctr->ctr.ctr1 = ctr1;
3702 struct srvsvc_NetShareCtr2 *ctr2;
3704 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3705 W_ERROR_HAVE_NO_MEMORY(ctr2);
3708 ctr2->array = info.info2;
3710 info_ctr->ctr.ctr2 = ctr2;
3716 struct srvsvc_NetShareCtr502 *ctr502;
3718 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3719 W_ERROR_HAVE_NO_MEMORY(ctr502);
3722 ctr502->array = info.info502;
3724 info_ctr->ctr.ctr502 = ctr502;
3734 * 'net rpc share list' entrypoint.
3735 * @param argc Standard main() style argc.
3736 * @param argv Standard main() style argv. Initial components are already
3739 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3741 NET_API_STATUS status;
3742 struct SHARE_INFO_1 *i1 = NULL;
3743 uint32_t entries_read = 0;
3744 uint32_t total_entries = 0;
3745 uint32_t resume_handle = 0;
3746 uint32_t i, level = 1;
3748 if (c->display_usage) {
3750 "net rpc share list\n"
3753 _("List shares on remote server"));
3757 status = NetShareEnum(c->opt_host,
3759 (uint8_t **)(void *)&i1,
3768 /* Display results */
3770 if (c->opt_long_list_entries) {
3772 "\nEnumerating shared resources (exports) on remote server:\n\n"
3773 "\nShare name Type Description\n"
3774 "---------- ---- -----------\n"));
3776 for (i = 0; i < entries_read; i++)
3777 display_share_info_1(c, &i1[i]);
3782 static bool check_share_availability(struct cli_state *cli, const char *netname)
3786 status = cli_tree_connect(cli, netname, "A:", NULL);
3787 if (!NT_STATUS_IS_OK(status)) {
3788 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3792 status = cli_tdis(cli);
3793 if (!NT_STATUS_IS_OK(status)) {
3794 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3801 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3802 const char *netname, uint32_t type)
3804 /* only support disk shares */
3805 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3806 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3811 /* skip builtin shares */
3812 /* FIXME: should print$ be added too ? */
3813 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3814 strequal(netname,"global"))
3817 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3818 printf(_("excluding [%s]\n"), netname);
3822 return check_share_availability(cli, netname);
3826 * Migrate shares from a remote RPC server to the local RPC server.
3828 * All parameters are provided by the run_rpc_command function, except for
3829 * argc, argv which are passed through.
3831 * @param domain_sid The domain sid acquired from the remote server.
3832 * @param cli A cli_state connected to the server.
3833 * @param mem_ctx Talloc context, destroyed on completion of the function.
3834 * @param argc Standard main() style argc.
3835 * @param argv Standard main() style argv. Initial components are already
3838 * @return Normal NTSTATUS return.
3841 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3842 const struct dom_sid *domain_sid,
3843 const char *domain_name,
3844 struct cli_state *cli,
3845 struct rpc_pipe_client *pipe_hnd,
3846 TALLOC_CTX *mem_ctx,
3851 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3852 struct srvsvc_NetShareInfoCtr ctr_src;
3854 struct rpc_pipe_client *srvsvc_pipe = NULL;
3855 struct cli_state *cli_dst = NULL;
3856 uint32_t level = 502; /* includes secdesc */
3857 uint32_t parm_error = 0;
3858 struct dcerpc_binding_handle *b;
3860 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3862 if (!W_ERROR_IS_OK(result))
3865 /* connect destination PI_SRVSVC */
3866 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3868 if (!NT_STATUS_IS_OK(nt_status))
3871 b = srvsvc_pipe->binding_handle;
3873 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3875 union srvsvc_NetShareInfo info;
3876 struct srvsvc_NetShareInfo502 info502 =
3877 ctr_src.ctr.ctr502->array[i];
3879 /* reset error-code */
3880 nt_status = NT_STATUS_UNSUCCESSFUL;
3882 if (!check_share_sanity(c, cli, info502.name, info502.type))
3885 /* finally add the share on the dst server */
3887 printf(_("migrating: [%s], path: %s, comment: %s, without "
3889 info502.name, info502.path, info502.comment);
3891 info.info502 = &info502;
3893 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3894 srvsvc_pipe->desthost,
3899 if (!NT_STATUS_IS_OK(nt_status)) {
3900 printf(_("cannot add share: %s\n"),
3901 nt_errstr(nt_status));
3904 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3905 printf(_(" [%s] does already exist\n"),
3910 if (!W_ERROR_IS_OK(result)) {
3911 nt_status = werror_to_ntstatus(result);
3912 printf(_("cannot add share: %s\n"),
3913 win_errstr(result));
3919 nt_status = NT_STATUS_OK;
3923 cli_shutdown(cli_dst);
3931 * Migrate shares from a RPC server to another.
3933 * @param argc Standard main() style argc.
3934 * @param argv Standard main() style argv. Initial components are already
3937 * @return A shell status integer (0 for success).
3939 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3942 if (c->display_usage) {
3944 "net rpc share migrate shares\n"
3947 _("Migrate shares to local server"));
3952 printf(_("no server to migrate\n"));
3956 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3957 rpc_share_migrate_shares_internals,
3964 * @param f file_info
3965 * @param mask current search mask
3966 * @param state arg-pointer
3969 static NTSTATUS copy_fn(struct file_info *f,
3970 const char *mask, void *state)
3972 static NTSTATUS nt_status;
3973 static struct copy_clistate *local_state;
3974 static fstring filename, new_mask;
3977 struct net_context *c;
3979 local_state = (struct copy_clistate *)state;
3980 nt_status = NT_STATUS_UNSUCCESSFUL;
3984 if (strequal(f->name, ".") || strequal(f->name, ".."))
3985 return NT_STATUS_OK;
3987 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3990 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
3992 DEBUG(3,("got dir: %s\n", f->name));
3994 fstrcpy(dir, local_state->cwd);
3996 fstrcat(dir, f->name);
3998 switch (net_mode_share)
4000 case NET_MODE_SHARE_MIGRATE:
4001 /* create that directory */
4002 nt_status = net_copy_file(c, local_state->mem_ctx,
4003 local_state->cli_share_src,
4004 local_state->cli_share_dst,
4006 c->opt_acls? true : false,
4007 c->opt_attrs? true : false,
4008 c->opt_timestamps? true:false,
4012 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4013 return NT_STATUS_INTERNAL_ERROR;
4016 if (!NT_STATUS_IS_OK(nt_status)) {
4017 printf(_("could not handle dir %s: %s\n"),
4018 dir, nt_errstr(nt_status));
4022 /* search below that directory */
4023 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
4024 return NT_STATUS_NO_MEMORY;
4026 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
4027 return NT_STATUS_NO_MEMORY;
4030 old_dir = local_state->cwd;
4031 local_state->cwd = dir;
4032 nt_status = sync_files(local_state, new_mask);
4033 if (!NT_STATUS_IS_OK(nt_status)) {
4034 printf(_("could not handle files\n"));
4036 local_state->cwd = old_dir;
4043 fstrcpy(filename, local_state->cwd);
4044 fstrcat(filename, "\\");
4045 fstrcat(filename, f->name);
4047 DEBUG(3,("got file: %s\n", filename));
4049 switch (net_mode_share)
4051 case NET_MODE_SHARE_MIGRATE:
4052 nt_status = net_copy_file(c, local_state->mem_ctx,
4053 local_state->cli_share_src,
4054 local_state->cli_share_dst,
4056 c->opt_acls? true : false,
4057 c->opt_attrs? true : false,
4058 c->opt_timestamps? true: false,
4062 d_fprintf(stderr, _("Unsupported file mode %d\n"),
4064 return NT_STATUS_INTERNAL_ERROR;
4067 if (!NT_STATUS_IS_OK(nt_status))
4068 printf(_("could not handle file %s: %s\n"),
4069 filename, nt_errstr(nt_status));
4074 * sync files, can be called recursively to list files
4075 * and then call copy_fn for each file
4077 * @param cp_clistate pointer to the copy_clistate we work with
4078 * @param mask the current search mask
4080 * @return Boolean result
4082 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
4084 struct cli_state *targetcli;
4085 char *targetpath = NULL;
4088 DEBUG(3,("calling cli_list with mask: %s\n", mask));
4090 status = cli_resolve_path(talloc_tos(), "", NULL,
4091 cp_clistate->cli_share_src,
4092 mask, &targetcli, &targetpath);
4093 if (!NT_STATUS_IS_OK(status)) {
4094 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
4096 mask, nt_errstr(status));
4100 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
4101 copy_fn, cp_clistate);
4102 if (!NT_STATUS_IS_OK(status)) {
4103 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
4104 mask, nt_errstr(status));
4112 * Set the top level directory permissions before we do any further copies.
4113 * Should set up ACL inheritance.
4116 bool copy_top_level_perms(struct net_context *c,
4117 struct copy_clistate *cp_clistate,
4118 const char *sharename)
4120 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4122 switch (net_mode_share) {
4123 case NET_MODE_SHARE_MIGRATE:
4124 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
4125 nt_status = net_copy_fileattr(c,
4126 cp_clistate->mem_ctx,
4127 cp_clistate->cli_share_src,
4128 cp_clistate->cli_share_dst,
4130 c->opt_acls? true : false,
4131 c->opt_attrs? true : false,
4132 c->opt_timestamps? true: false,
4136 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4140 if (!NT_STATUS_IS_OK(nt_status)) {
4141 printf(_("Could handle directory attributes for top level "
4142 "directory of share %s. Error %s\n"),
4143 sharename, nt_errstr(nt_status));
4151 * Sync all files inside a remote share to another share (over smb).
4153 * All parameters are provided by the run_rpc_command function, except for
4154 * argc, argv which are passed through.
4156 * @param domain_sid The domain sid acquired from the remote server.
4157 * @param cli A cli_state connected to the server.
4158 * @param mem_ctx Talloc context, destroyed on completion of the function.
4159 * @param argc Standard main() style argc.
4160 * @param argv Standard main() style argv. Initial components are already
4163 * @return Normal NTSTATUS return.
4166 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
4167 const struct dom_sid *domain_sid,
4168 const char *domain_name,
4169 struct cli_state *cli,
4170 struct rpc_pipe_client *pipe_hnd,
4171 TALLOC_CTX *mem_ctx,
4176 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4177 struct srvsvc_NetShareInfoCtr ctr_src;
4179 uint32_t level = 502;
4180 struct copy_clistate cp_clistate;
4181 bool got_src_share = false;
4182 bool got_dst_share = false;
4183 const char *mask = "\\*";
4186 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
4188 nt_status = NT_STATUS_NO_MEMORY;
4192 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4195 if (!W_ERROR_IS_OK(result))
4198 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4200 struct srvsvc_NetShareInfo502 info502 =
4201 ctr_src.ctr.ctr502->array[i];
4203 if (!check_share_sanity(c, cli, info502.name, info502.type))
4206 /* one might not want to mirror whole discs :) */
4207 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
4208 d_printf(_("skipping [%s]: builtin/hidden share\n"),
4213 switch (net_mode_share)
4215 case NET_MODE_SHARE_MIGRATE:
4219 d_fprintf(stderr, _("Unsupported mode %d\n"),
4223 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4226 c->opt_acls ? _("including") : _("without"),
4227 c->opt_attrs ? _("including") : _("without"),
4228 c->opt_timestamps ? _("(preserving timestamps)") : "");
4230 cp_clistate.mem_ctx = mem_ctx;
4231 cp_clistate.cli_share_src = NULL;
4232 cp_clistate.cli_share_dst = NULL;
4233 cp_clistate.cwd = NULL;
4234 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4237 /* open share source */
4238 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4239 smbXcli_conn_remote_sockaddr(cli->conn),
4240 smbXcli_conn_remote_name(cli->conn),
4241 info502.name, "A:");
4242 if (!NT_STATUS_IS_OK(nt_status))
4245 got_src_share = true;
4247 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4248 /* open share destination */
4249 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4250 NULL, dst, info502.name, "A:");
4251 if (!NT_STATUS_IS_OK(nt_status))
4254 got_dst_share = true;
4257 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4258 d_fprintf(stderr, _("Could not handle the top level "
4259 "directory permissions for the "
4260 "share: %s\n"), info502.name);
4261 nt_status = NT_STATUS_UNSUCCESSFUL;
4265 nt_status = sync_files(&cp_clistate, mask);
4266 if (!NT_STATUS_IS_OK(nt_status)) {
4267 d_fprintf(stderr, _("could not handle files for share: "
4268 "%s\n"), info502.name);
4273 nt_status = NT_STATUS_OK;
4278 cli_shutdown(cp_clistate.cli_share_src);
4281 cli_shutdown(cp_clistate.cli_share_dst);
4288 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4290 if (c->display_usage) {
4292 "net share migrate files\n"
4295 _("Migrate files to local server"));
4300 d_printf(_("no server to migrate\n"));
4304 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4305 rpc_share_migrate_files_internals,
4310 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4312 * All parameters are provided by the run_rpc_command function, except for
4313 * argc, argv which are passed through.
4315 * @param domain_sid The domain sid acquired from the remote server.
4316 * @param cli A cli_state connected to the server.
4317 * @param mem_ctx Talloc context, destroyed on completion of the function.
4318 * @param argc Standard main() style argc.
4319 * @param argv Standard main() style argv. Initial components are already
4322 * @return Normal NTSTATUS return.
4325 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4326 const struct dom_sid *domain_sid,
4327 const char *domain_name,
4328 struct cli_state *cli,
4329 struct rpc_pipe_client *pipe_hnd,
4330 TALLOC_CTX *mem_ctx,
4335 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4336 struct srvsvc_NetShareInfoCtr ctr_src;
4337 union srvsvc_NetShareInfo info;
4339 struct rpc_pipe_client *srvsvc_pipe = NULL;
4340 struct cli_state *cli_dst = NULL;
4341 uint32_t level = 502; /* includes secdesc */
4342 uint32_t parm_error = 0;
4343 struct dcerpc_binding_handle *b;
4345 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4348 if (!W_ERROR_IS_OK(result))
4351 /* connect destination PI_SRVSVC */
4352 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4354 if (!NT_STATUS_IS_OK(nt_status))
4357 b = srvsvc_pipe->binding_handle;
4359 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4361 struct srvsvc_NetShareInfo502 info502 =
4362 ctr_src.ctr.ctr502->array[i];
4364 /* reset error-code */
4365 nt_status = NT_STATUS_UNSUCCESSFUL;
4367 if (!check_share_sanity(c, cli, info502.name, info502.type))
4370 printf(_("migrating: [%s], path: %s, comment: %s, including "
4372 info502.name, info502.path, info502.comment);
4375 display_sec_desc(info502.sd_buf.sd);
4377 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4378 info.info502 = &info502;
4380 /* finally modify the share on the dst server */
4381 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4382 srvsvc_pipe->desthost,
4388 if (!NT_STATUS_IS_OK(nt_status)) {
4389 printf(_("cannot set share-acl: %s\n"),
4390 nt_errstr(nt_status));
4393 if (!W_ERROR_IS_OK(result)) {
4394 nt_status = werror_to_ntstatus(result);
4395 printf(_("cannot set share-acl: %s\n"),
4396 win_errstr(result));
4402 nt_status = NT_STATUS_OK;
4406 cli_shutdown(cli_dst);
4414 * Migrate share-acls from a RPC server to another.
4416 * @param argc Standard main() style argc.
4417 * @param argv Standard main() style argv. Initial components are already
4420 * @return A shell status integer (0 for success).
4422 static int rpc_share_migrate_security(struct net_context *c, int argc,
4425 if (c->display_usage) {
4427 "net rpc share migrate security\n"
4430 _("Migrate share-acls to local server"));
4435 d_printf(_("no server to migrate\n"));
4439 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4440 rpc_share_migrate_security_internals,
4445 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4446 * from one server to another.
4448 * @param argc Standard main() style argc.
4449 * @param argv Standard main() style argv. Initial components are already
4452 * @return A shell status integer (0 for success).
4455 static int rpc_share_migrate_all(struct net_context *c, int argc,
4460 if (c->display_usage) {
4462 "net rpc share migrate all\n"
4465 _("Migrates shares including all share settings"));
4470 d_printf(_("no server to migrate\n"));
4474 /* order is important. we don't want to be locked out by the share-acl
4475 * before copying files - gd */
4477 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4478 rpc_share_migrate_shares_internals, argc, argv);
4482 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4483 rpc_share_migrate_files_internals, argc, argv);
4487 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4488 rpc_share_migrate_security_internals, argc,
4494 * 'net rpc share migrate' entrypoint.
4495 * @param argc Standard main() style argc.
4496 * @param argv Standard main() style argv. Initial components are already
4499 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4502 struct functable func[] = {
4505 rpc_share_migrate_all,
4507 N_("Migrate shares from remote to local server"),
4508 N_("net rpc share migrate all\n"
4509 " Migrate shares from remote to local server")
4513 rpc_share_migrate_files,
4515 N_("Migrate files from remote to local server"),
4516 N_("net rpc share migrate files\n"
4517 " Migrate files from remote to local server")
4521 rpc_share_migrate_security,
4523 N_("Migrate share-ACLs from remote to local server"),
4524 N_("net rpc share migrate security\n"
4525 " Migrate share-ACLs from remote to local server")
4529 rpc_share_migrate_shares,
4531 N_("Migrate shares from remote to local server"),
4532 N_("net rpc share migrate shares\n"
4533 " Migrate shares from remote to local server")
4535 {NULL, NULL, 0, NULL, NULL}
4538 net_mode_share = NET_MODE_SHARE_MIGRATE;
4540 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4545 uint32_t num_members;
4546 struct dom_sid *members;
4549 static int num_server_aliases;
4550 static struct full_alias *server_aliases;
4553 * Add an alias to the static list.
4555 static void push_alias(struct full_alias *alias)
4559 if (server_aliases == NULL) {
4560 server_aliases = talloc_array(NULL, struct full_alias, 100);
4561 if (server_aliases == NULL) {
4562 smb_panic("talloc_array failed");
4566 array_size = talloc_array_length(server_aliases);
4567 if (array_size == num_server_aliases) {
4568 server_aliases = talloc_realloc(NULL, server_aliases,
4569 struct full_alias, array_size + 100);
4570 if (server_aliases == NULL) {
4571 smb_panic("talloc_realloc failed");
4575 server_aliases[num_server_aliases] = *alias;
4576 num_server_aliases += 1;
4580 * For a specific domain on the server, fetch all the aliases
4581 * and their members. Add all of them to the server_aliases.
4584 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4585 TALLOC_CTX *mem_ctx,
4586 struct policy_handle *connect_pol,
4587 const struct dom_sid *domain_sid)
4589 uint32_t start_idx, max_entries, num_entries, i;
4590 struct samr_SamArray *groups = NULL;
4591 NTSTATUS result, status;
4592 struct policy_handle domain_pol;
4593 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4595 /* Get domain policy handle */
4597 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4599 MAXIMUM_ALLOWED_ACCESS,
4600 discard_const_p(struct dom_sid2, domain_sid),
4603 if (!NT_STATUS_IS_OK(status)) {
4606 if (!NT_STATUS_IS_OK(result)) {
4614 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4621 if (!NT_STATUS_IS_OK(status)) {
4624 for (i = 0; i < num_entries; i++) {
4626 struct policy_handle alias_pol;
4627 struct full_alias alias;
4628 struct lsa_SidArray sid_array;
4632 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4634 MAXIMUM_ALLOWED_ACCESS,
4635 groups->entries[i].idx,
4638 if (!NT_STATUS_IS_OK(status)) {
4641 if (!NT_STATUS_IS_OK(_result)) {
4646 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4650 if (!NT_STATUS_IS_OK(status)) {
4653 if (!NT_STATUS_IS_OK(_result)) {
4658 alias.num_members = sid_array.num_sids;
4660 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4661 if (!NT_STATUS_IS_OK(status)) {
4664 if (!NT_STATUS_IS_OK(_result)) {
4669 alias.members = NULL;
4671 if (alias.num_members > 0) {
4672 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4673 if (alias.members == NULL) {
4674 status = NT_STATUS_NO_MEMORY;
4678 for (j = 0; j < alias.num_members; j++)
4679 sid_copy(&alias.members[j],
4680 sid_array.sids[j].sid);
4683 sid_compose(&alias.sid, domain_sid,
4684 groups->entries[i].idx);
4688 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4690 status = NT_STATUS_OK;
4693 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4699 * Dump server_aliases as names for debugging purposes.
4702 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4703 const struct dom_sid *domain_sid,
4704 const char *domain_name,
4705 struct cli_state *cli,
4706 struct rpc_pipe_client *pipe_hnd,
4707 TALLOC_CTX *mem_ctx,
4713 struct policy_handle lsa_pol;
4714 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4716 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4717 SEC_FLAG_MAXIMUM_ALLOWED,
4719 if (!NT_STATUS_IS_OK(result))
4722 for (i=0; i<num_server_aliases; i++) {
4725 enum lsa_SidType *types;
4728 struct full_alias *alias = &server_aliases[i];
4730 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4732 &domains, &names, &types);
4733 if (!NT_STATUS_IS_OK(result))
4736 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4738 if (alias->num_members == 0) {
4743 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4746 &domains, &names, &types);
4748 if (!NT_STATUS_IS_OK(result) &&
4749 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4752 for (j=0; j<alias->num_members; j++)
4753 DEBUG(1, ("%s\\%s (%d); ",
4754 domains[j] ? domains[j] : "*unknown*",
4755 names[j] ? names[j] : "*unknown*",types[j]));
4759 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4761 return NT_STATUS_OK;
4765 * Fetch a list of all server aliases and their members into
4769 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4770 const struct dom_sid *domain_sid,
4771 const char *domain_name,
4772 struct cli_state *cli,
4773 struct rpc_pipe_client *pipe_hnd,
4774 TALLOC_CTX *mem_ctx,
4778 NTSTATUS result, status;
4779 struct policy_handle connect_pol;
4780 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4782 status = dcerpc_samr_Connect2(b, mem_ctx,
4784 MAXIMUM_ALLOWED_ACCESS,
4787 if (!NT_STATUS_IS_OK(status)) {
4790 if (!NT_STATUS_IS_OK(result)) {
4795 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4796 &global_sid_Builtin);
4797 if (!NT_STATUS_IS_OK(status)) {
4801 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4804 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4811 struct security_token *token;
4814 static void add_sid_to_token(struct security_token *token, const struct dom_sid *sid)
4816 NTSTATUS status = add_sid_to_array_unique(token, sid,
4817 &token->sids, &token->num_sids);
4819 * This is both very unlikely and mostly harmless in a command
4822 SMB_ASSERT(NT_STATUS_IS_OK(status));
4825 static void init_user_token(struct user_token *token_list,
4826 struct security_token **token,
4827 struct dom_sid *user_sid)
4830 * This token is not from the auth stack, only has user SIDs
4831 * and must fail if conditional ACEs are found in the security
4834 *token = security_token_initialise(token_list, CLAIMS_EVALUATION_INVALID_STATE);
4837 add_sid_to_token(*token,
4840 add_sid_to_token(*token,
4843 add_sid_to_token(*token,
4844 &global_sid_Network);
4846 add_sid_to_token(*token,
4847 &global_sid_Authenticated_Users);
4850 static void dump_user_token(struct user_token *token)
4854 d_printf("%s\n", token->name);
4856 for (i=0; i<token->token->num_sids; i++) {
4857 struct dom_sid_buf buf;
4859 dom_sid_str_buf(&token->token->sids[i], &buf));
4863 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4867 for (i=0; i<alias->num_members; i++) {
4868 if (dom_sid_equal(sid, &alias->members[i])) {
4876 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4880 for (i=0; i<num_server_aliases; i++) {
4881 if (is_alias_member(&sid, &server_aliases[i]))
4882 add_sid_to_token(token, &server_aliases[i].sid);
4887 * We got a user token with all the SIDs we can know about without asking the
4888 * server directly. These are the user and domain group sids. All of these can
4889 * be members of aliases. So scan the list of aliases for each of the SIDs and
4890 * add them to the token.
4893 static void collect_alias_memberships(struct security_token *token)
4895 int num_global_sids = token->num_sids;
4898 for (i=0; i<num_global_sids; i++) {
4899 collect_sid_memberships(token, token->sids[i]);
4903 static bool get_user_sids(const char *domain, const char *user,
4904 struct user_token *token_list,
4905 struct security_token **token)
4907 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4908 enum wbcSidType type;
4910 struct wbcDomainSid wsid;
4911 char sid_str[WBC_SID_STRING_BUFLEN];
4912 struct dom_sid user_sid;
4913 uint32_t num_groups;
4914 gid_t *groups = NULL;
4917 fstr_sprintf(full_name, "%s%c%s",
4918 domain, *lp_winbind_separator(), user);
4920 /* First let's find out the user sid */
4922 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4924 if (!WBC_ERROR_IS_OK(wbc_status)) {
4925 DEBUG(1, ("winbind could not find %s: %s\n",
4926 full_name, wbcErrorString(wbc_status)));
4930 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4932 if (type != WBC_SID_NAME_USER) {
4933 DEBUG(1, ("%s is not a user\n", full_name));
4937 if (!string_to_sid(&user_sid, sid_str)) {
4938 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4942 init_user_token(token_list, token, &user_sid);
4944 /* And now the groups winbind knows about */
4946 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4947 if (!WBC_ERROR_IS_OK(wbc_status)) {
4948 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4949 full_name, wbcErrorString(wbc_status)));
4953 for (i = 0; i < num_groups; i++) {
4954 gid_t gid = groups[i];
4958 wbc_status = wbcGidToSid(gid, &wsid);
4959 if (!WBC_ERROR_IS_OK(wbc_status)) {
4960 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4961 (unsigned int)gid, wbcErrorString(wbc_status)));
4962 wbcFreeMemory(groups);
4966 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4968 DEBUG(3, (" %s\n", sid_str));
4970 ok = string_to_sid(&sid, sid_str);
4972 DEBUG(1, ("Failed to convert string to SID\n"));
4973 wbcFreeMemory(groups);
4976 add_sid_to_token(*token, &sid);
4978 wbcFreeMemory(groups);
4984 * Get a list of all user tokens we want to look at
4987 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4988 struct user_token **user_tokens)
4990 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4991 uint32_t i, num_users;
4993 struct user_token *result;
4994 TALLOC_CTX *frame = NULL;
4996 if (lp_winbind_use_default_domain() &&
4997 (c->opt_target_workgroup == NULL)) {
4998 d_fprintf(stderr, _("winbind use default domain = yes set, "
4999 "please specify a workgroup\n"));
5003 /* Send request to winbind daemon */
5005 wbc_status = wbcListUsers(NULL, &num_users, &users);
5006 if (!WBC_ERROR_IS_OK(wbc_status)) {
5007 DEBUG(1, (_("winbind could not list users: %s\n"),
5008 wbcErrorString(wbc_status)));
5012 result = talloc_zero_array(NULL, struct user_token, num_users);
5014 if (result == NULL) {
5015 DBG_ERR("Could not talloc token array\n");
5016 wbcFreeMemory(users);
5020 frame = talloc_stackframe();
5021 for (i=0; i < num_users; i++) {
5022 fstring domain, user;
5025 fstrcpy(result[i].name, users[i]);
5027 p = strchr(users[i], *lp_winbind_separator());
5029 DEBUG(3, ("%s\n", users[i]));
5032 fstrcpy(domain, c->opt_target_workgroup);
5033 fstrcpy(user, users[i]);
5036 fstrcpy(domain, users[i]);
5037 if (!strupper_m(domain)) {
5038 DEBUG(1, ("strupper_m %s failed\n", domain));
5039 wbcFreeMemory(users);
5045 get_user_sids(domain, user, result, &(result[i].token));
5048 wbcFreeMemory(users);
5050 *num_tokens = num_users;
5051 *user_tokens = result;
5056 static bool get_user_tokens_from_file(FILE *f,
5058 struct user_token **tokens)
5060 struct user_token *token = NULL;
5065 if (fgets(line, sizeof(line)-1, f) == NULL) {
5069 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
5070 line[strlen(line)-1] = '\0';
5073 if (line[0] == ' ') {
5077 if(!string_to_sid(&sid, &line[1])) {
5078 DEBUG(1,("get_user_tokens_from_file: Could "
5079 "not convert sid %s \n",&line[1]));
5083 if (token == NULL) {
5084 DEBUG(0, ("File does not begin with username\n"));
5088 add_sid_to_token(token->token, &sid);
5092 /* And a new user... */
5095 *tokens = talloc_realloc(NULL,
5099 if (*tokens == NULL) {
5100 DBG_ERR("Could not talloc_realloc tokens\n");
5104 token = &((*tokens)[*num_tokens-1]);
5106 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
5110 = security_token_initialise(*tokens,
5111 CLAIMS_EVALUATION_INVALID_STATE);
5112 if (token->token == NULL) {
5113 DBG_ERR("security_token_initialise() failed: "
5114 "Could not allocate security_token with \n");
5126 * Show the list of all users that have access to a share
5129 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
5130 struct cli_state *cli,
5131 TALLOC_CTX *mem_ctx,
5132 const char *netname,
5134 struct user_token *tokens)
5137 struct security_descriptor *share_sd = NULL;
5138 struct security_descriptor *root_sd = NULL;
5140 union srvsvc_NetShareInfo info;
5143 struct smbXcli_tcon *orig_tcon = NULL;
5144 char *orig_share = NULL;
5145 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5147 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5154 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5155 DEBUG(1, ("Could not query secdesc for share %s\n",
5160 share_sd = info.info502->sd_buf.sd;
5161 if (share_sd == NULL) {
5162 DEBUG(1, ("Got no secdesc for share %s\n",
5166 if (cli_state_has_tcon(cli)) {
5167 cli_state_save_tcon_share(cli, &orig_tcon, &orig_share);
5170 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", NULL))) {
5171 cli_state_restore_tcon_share(cli, orig_tcon, orig_share);
5175 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
5176 FILE_SHARE_READ|FILE_SHARE_WRITE,
5177 FILE_OPEN, 0x0, 0x0, &fnum, NULL))) {
5178 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
5181 for (i=0; i<num_tokens; i++) {
5182 uint32_t acc_granted;
5184 if (share_sd != NULL) {
5185 status = se_access_check(share_sd, tokens[i].token,
5188 if (!NT_STATUS_IS_OK(status)) {
5189 DEBUG(1, ("Could not check share_sd for "
5196 if (root_sd == NULL) {
5197 d_printf(" %s\n", tokens[i].name);
5201 status = se_access_check(root_sd, tokens[i].token,
5203 if (!NT_STATUS_IS_OK(status)) {
5204 DEBUG(1, ("Could not check root_sd for user %s\n",
5208 d_printf(" %s\n", tokens[i].name);
5211 if (fnum != (uint16_t)-1)
5212 cli_close(cli, fnum);
5214 cli_state_restore_tcon_share(cli, orig_tcon, orig_share);
5220 * List shares on a remote RPC server, including the security descriptors.
5222 * All parameters are provided by the run_rpc_command function, except for
5223 * argc, argv which are passed through.
5225 * @param domain_sid The domain sid acquired from the remote server.
5226 * @param cli A cli_state connected to the server.
5227 * @param mem_ctx Talloc context, destroyed on completion of the function.
5228 * @param argc Standard main() style argc.
5229 * @param argv Standard main() style argv. Initial components are already
5232 * @return Normal NTSTATUS return.
5235 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
5236 const struct dom_sid *domain_sid,
5237 const char *domain_name,
5238 struct cli_state *cli,
5239 struct rpc_pipe_client *pipe_hnd,
5240 TALLOC_CTX *mem_ctx,
5246 NTSTATUS nt_status = NT_STATUS_OK;
5247 uint32_t total_entries = 0;
5248 uint32_t resume_handle = 0;
5249 uint32_t preferred_len = 0xffffffff;
5251 struct dcerpc_binding_handle *b = NULL;
5252 struct srvsvc_NetShareInfoCtr info_ctr;
5253 struct srvsvc_NetShareCtr1 ctr1;
5256 struct user_token *tokens = NULL;
5262 if (strequal(argv[0], "-")) {
5265 f = fopen(argv[0], "r");
5272 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
5273 return NT_STATUS_UNSUCCESSFUL;
5276 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5282 DEBUG(0, ("Could not read users from file\n"));
5283 return NT_STATUS_UNSUCCESSFUL;
5286 for (i=0; i<num_tokens; i++)
5287 collect_alias_memberships(tokens[i].token);
5289 ZERO_STRUCT(info_ctr);
5293 info_ctr.ctr.ctr1 = &ctr1;
5295 b = pipe_hnd->binding_handle;
5298 /* Show results only for shares listed on the command line. */
5300 const char *netname = *argv++;
5301 d_printf("%s\n", netname);
5302 show_userlist(pipe_hnd, cli, mem_ctx, netname,
5303 num_tokens, tokens);
5308 /* Issue the NetShareEnum RPC call and retrieve the response */
5309 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5318 /* Was it successful? */
5319 if (!NT_STATUS_IS_OK(nt_status)) {
5320 /* Nope. Go clean up. */
5324 if (!W_ERROR_IS_OK(result)) {
5325 /* Nope. Go clean up. */
5326 nt_status = werror_to_ntstatus(result);
5330 if (total_entries == 0) {
5334 /* For each returned entry... */
5335 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5336 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5338 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5342 d_printf("%s\n", netname);
5344 show_userlist(pipe_hnd, cli, mem_ctx, netname,
5345 num_tokens, tokens);
5348 TALLOC_FREE(tokens);
5349 TALLOC_FREE(server_aliases);
5354 static int rpc_share_allowedusers(struct net_context *c, int argc,
5359 if (c->display_usage) {
5361 "net rpc share allowedusers\n"
5364 _("List allowed users"));
5368 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5369 rpc_aliaslist_internals,
5374 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5380 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5381 rpc_share_allowedusers_internals,
5385 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5388 struct user_token *tokens = NULL;
5392 net_usersidlist_usage(c, argc, argv);
5396 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5397 DEBUG(0, ("Could not get the user/sid list\n"));
5401 for (i=0; i<num_tokens; i++) {
5402 dump_user_token(&tokens[i]);
5405 TALLOC_FREE(tokens);
5409 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5411 d_printf(_("net usersidlist\n"
5412 "\tprints out a list of all users the running winbind knows\n"
5413 "\tabout, together with all their SIDs. This is used as\n"
5414 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5416 net_common_flags_usage(c, argc, argv);
5421 * 'net rpc share' entrypoint.
5422 * @param argc Standard main() style argc.
5423 * @param argv Standard main() style argv. Initial components are already
5427 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5429 NET_API_STATUS status;
5431 struct functable func[] = {
5437 N_("net rpc share add\n"
5445 N_("net rpc share delete\n"
5450 rpc_share_allowedusers,
5452 N_("List allowed users"),
5453 N_("net rpc share allowedusers\n"
5454 " List allowed users")
5460 N_("Migrate share to local server"),
5461 N_("net rpc share migrate\n"
5462 " Migrate share to local server")
5469 N_("net rpc share list\n"
5472 {NULL, NULL, 0, NULL, NULL}
5475 status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
5481 if (c->display_usage) {
5486 " Alias for net rpc share list\n"));
5487 net_display_usage_from_functable(func);
5491 return rpc_share_list(c, argc, argv);
5494 return net_run_function(c, argc, argv, "net rpc share", func);
5497 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5498 TALLOC_CTX *mem_ctx,
5499 struct rpc_sh_ctx *ctx,
5500 struct rpc_pipe_client *pipe_hnd,
5501 int argc, const char **argv)
5504 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5507 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5508 TALLOC_CTX *mem_ctx,
5509 struct rpc_sh_ctx *ctx,
5510 struct rpc_pipe_client *pipe_hnd,
5511 int argc, const char **argv)
5513 NET_API_STATUS status;
5514 uint32_t parm_err = 0;
5515 struct SHARE_INFO_2 i2;
5517 if ((argc < 2) || (argc > 3)) {
5518 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5520 return NT_STATUS_INVALID_PARAMETER;
5523 i2.shi2_netname = argv[0];
5524 i2.shi2_type = STYPE_DISKTREE;
5525 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5526 i2.shi2_permissions = 0;
5527 i2.shi2_max_uses = 0;
5528 i2.shi2_current_uses = 0;
5529 i2.shi2_path = argv[1];
5530 i2.shi2_passwd = NULL;
5532 status = NetShareAdd(pipe_hnd->desthost,
5537 return werror_to_ntstatus(W_ERROR(status));
5540 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5541 TALLOC_CTX *mem_ctx,
5542 struct rpc_sh_ctx *ctx,
5543 struct rpc_pipe_client *pipe_hnd,
5544 int argc, const char **argv)
5547 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5548 return NT_STATUS_INVALID_PARAMETER;
5551 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5554 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5555 TALLOC_CTX *mem_ctx,
5556 struct rpc_sh_ctx *ctx,
5557 struct rpc_pipe_client *pipe_hnd,
5558 int argc, const char **argv)
5560 union srvsvc_NetShareInfo info;
5563 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5566 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5567 return NT_STATUS_INVALID_PARAMETER;
5570 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5576 if (!NT_STATUS_IS_OK(status)) {
5577 result = ntstatus_to_werror(status);
5580 if (!W_ERROR_IS_OK(result)) {
5584 d_printf(_("Name: %s\n"), info.info2->name);
5585 d_printf(_("Comment: %s\n"), info.info2->comment);
5586 d_printf(_("Path: %s\n"), info.info2->path);
5587 d_printf(_("Password: %s\n"), info.info2->password);
5590 return werror_to_ntstatus(result);
5593 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5594 struct rpc_sh_ctx *ctx)
5596 static struct rpc_sh_cmd cmds[] = {
5598 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5599 N_("List available shares") },
5601 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5602 N_("Add a share") },
5604 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5605 N_("Delete a share") },
5607 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5608 N_("Get information about a share") },
5610 { NULL, NULL, 0, NULL, NULL }
5616 /****************************************************************************/
5618 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5620 return net_file_usage(c, argc, argv);
5624 * Close a file on a remote RPC server.
5626 * @param argc Standard main() style argc.
5627 * @param argv Standard main() style argv. Initial components are already
5630 * @return A shell status integer (0 for success).
5632 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5634 if (argc < 1 || c->display_usage) {
5635 return rpc_file_usage(c, argc, argv);
5638 return NetFileClose(c->opt_host, atoi(argv[0]));
5642 * Formatted print of open file info
5644 * @param r struct FILE_INFO_3 contents
5647 static void display_file_info_3(struct FILE_INFO_3 *r)
5649 d_printf("%-7.1" PRIu32 " %-20.20s 0x%-4.2x %-6.1u %s\n",
5650 r->fi3_id, r->fi3_username, r->fi3_permissions,
5651 r->fi3_num_locks, r->fi3_pathname);
5655 * List files for a user on a remote RPC server.
5657 * @param argc Standard main() style argc.
5658 * @param argv Standard main() style argv. Initial components are already
5661 * @return A shell status integer (0 for success)..
5664 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5666 NET_API_STATUS status;
5667 uint32_t preferred_len = 0xffffffff, i;
5668 char *username=NULL;
5669 uint32_t total_entries = 0;
5670 uint32_t entries_read = 0;
5671 uint32_t resume_handle = 0;
5672 struct FILE_INFO_3 *i3 = NULL;
5674 if (c->display_usage) {
5675 return rpc_file_usage(c, argc, argv);
5678 /* if argc > 0, must be user command */
5680 username = smb_xstrdup(argv[0]);
5683 status = NetFileEnum(c->opt_host,
5687 (uint8_t **)(void *)&i3,
5697 /* Display results */
5700 "\nEnumerating open files on remote server:\n\n"
5701 "\nFileId Opened by Perms Locks Path"
5702 "\n------ --------- ----- ----- ---- \n"));
5703 for (i = 0; i < entries_read; i++) {
5704 display_file_info_3(&i3[i]);
5707 SAFE_FREE(username);
5712 * 'net rpc file' entrypoint.
5713 * @param argc Standard main() style argc.
5714 * @param argv Standard main() style argv. Initial components are already
5718 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5720 NET_API_STATUS status;
5722 struct functable func[] = {
5727 N_("Close opened file"),
5728 N_("net rpc file close\n"
5729 " Close opened file")
5735 N_("List files opened by user"),
5736 N_("net rpc file user\n"
5737 " List files opened by user")
5744 N_("Display information about opened file"),
5745 N_("net rpc file info\n"
5746 " Display information about opened file")
5749 {NULL, NULL, 0, NULL, NULL}
5752 status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
5758 if (c->display_usage) {
5759 d_printf(_("Usage:\n"));
5760 d_printf(_("net rpc file\n"
5761 " List opened files\n"));
5762 net_display_usage_from_functable(func);
5766 return rpc_file_user(c, argc, argv);
5769 return net_run_function(c, argc, argv, "net rpc file", func);
5773 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5775 * All parameters are provided by the run_rpc_command function, except for
5776 * argc, argv which are passed through.
5778 * @param c A net_context structure.
5779 * @param domain_sid The domain sid acquired from the remote server.
5780 * @param cli A cli_state connected to the server.
5781 * @param mem_ctx Talloc context, destroyed on completion of the function.
5782 * @param argc Standard main() style argc.
5783 * @param argv Standard main() style argv. Initial components are already
5786 * @return Normal NTSTATUS return.
5789 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5790 const struct dom_sid *domain_sid,
5791 const char *domain_name,
5792 struct cli_state *cli,
5793 struct rpc_pipe_client *pipe_hnd,
5794 TALLOC_CTX *mem_ctx,
5798 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5800 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5802 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5803 if (!NT_STATUS_IS_OK(status)) {
5806 if (W_ERROR_IS_OK(result)) {
5807 d_printf(_("\nShutdown successfully aborted\n"));
5808 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5810 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5812 return werror_to_ntstatus(result);
5816 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5818 * All parameters are provided by the run_rpc_command function, except for
5819 * argc, argv which are passed through.
5821 * @param c A net_context structure.
5822 * @param domain_sid The domain sid acquired from the remote server.
5823 * @param cli A cli_state connected to the server.
5824 * @param mem_ctx Talloc context, destroyed on completion of the function.
5825 * @param argc Standard main() style argc.
5826 * @param argv Standard main() style argv. Initial components are already
5829 * @return Normal NTSTATUS return.
5832 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5833 const struct dom_sid *domain_sid,
5834 const char *domain_name,
5835 struct cli_state *cli,
5836 struct rpc_pipe_client *pipe_hnd,
5837 TALLOC_CTX *mem_ctx,
5841 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5843 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5845 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5847 if (!NT_STATUS_IS_OK(result)) {
5848 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5851 if (W_ERROR_IS_OK(werr)) {
5852 d_printf(_("\nShutdown successfully aborted\n"));
5853 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5855 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5857 return werror_to_ntstatus(werr);
5861 * ABORT the shutdown of a remote RPC server.
5863 * @param argc Standard main() style argc.
5864 * @param argv Standard main() style argv. Initial components are already
5867 * @return A shell status integer (0 for success).
5870 static int rpc_shutdown_abort(struct net_context *c, int argc,
5875 if (c->display_usage) {
5877 "net rpc abortshutdown\n"
5880 _("Abort a scheduled shutdown"));
5884 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5885 rpc_shutdown_abort_internals, argc, argv);
5890 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5892 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5893 rpc_reg_shutdown_abort_internals,
5898 * Shut down a remote RPC Server via initshutdown pipe.
5900 * All parameters are provided by the run_rpc_command function, except for
5901 * argc, argv which are passed through.
5903 * @param c A net_context structure.
5904 * @param domain_sid The domain sid acquired from the remote server.
5905 * @param cli A cli_state connected to the server.
5906 * @param mem_ctx Talloc context, destroyed on completion of the function.
5907 * @param argc Standard main() style argc.
5908 * @param argv Standard main() style argv. Initial components are already
5911 * @return Normal NTSTATUS return.
5914 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5915 const struct dom_sid *domain_sid,
5916 const char *domain_name,
5917 struct cli_state *cli,
5918 struct rpc_pipe_client *pipe_hnd,
5919 TALLOC_CTX *mem_ctx,
5923 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5925 const char *msg = N_("This machine will be shutdown shortly");
5926 uint32_t timeout = 20;
5927 struct lsa_StringLarge msg_string;
5928 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5930 if (c->opt_comment) {
5931 msg = c->opt_comment;
5933 if (c->opt_timeout) {
5934 timeout = c->opt_timeout;
5937 msg_string.string = msg;
5939 /* create an entry */
5940 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5941 &msg_string, timeout, c->opt_force, c->opt_reboot,
5943 if (!NT_STATUS_IS_OK(status)) {
5946 if (W_ERROR_IS_OK(result)) {
5947 d_printf(_("\nShutdown of remote machine succeeded\n"));
5948 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5950 DEBUG(1,("Shutdown of remote machine failed!\n"));
5952 return werror_to_ntstatus(result);
5956 * Shut down a remote RPC Server via winreg pipe.
5958 * All parameters are provided by the run_rpc_command function, except for
5959 * argc, argv which are passed through.
5961 * @param c A net_context structure.
5962 * @param domain_sid The domain sid acquired from the remote server.
5963 * @param cli A cli_state connected to the server.
5964 * @param mem_ctx Talloc context, destroyed on completion of the function.
5965 * @param argc Standard main() style argc.
5966 * @param argv Standard main() style argv. Initial components are already
5969 * @return Normal NTSTATUS return.
5972 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5973 const struct dom_sid *domain_sid,
5974 const char *domain_name,
5975 struct cli_state *cli,
5976 struct rpc_pipe_client *pipe_hnd,
5977 TALLOC_CTX *mem_ctx,
5981 const char *msg = N_("This machine will be shutdown shortly");
5982 uint32_t timeout = 20;
5983 struct lsa_StringLarge msg_string;
5986 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5988 if (c->opt_comment) {
5989 msg = c->opt_comment;
5991 msg_string.string = msg;
5993 if (c->opt_timeout) {
5994 timeout = c->opt_timeout;
5997 /* create an entry */
5998 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5999 &msg_string, timeout, c->opt_force, c->opt_reboot,
6001 if (!NT_STATUS_IS_OK(result)) {
6002 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
6006 if (W_ERROR_IS_OK(werr)) {
6007 d_printf(_("\nShutdown of remote machine succeeded\n"));
6009 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
6010 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
6011 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
6013 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
6016 return werror_to_ntstatus(werr);
6020 * Shut down a remote RPC server.
6022 * @param argc Standard main() style argc.
6023 * @param argv Standard main() style argv. Initial components are already
6026 * @return A shell status integer (0 for success).
6029 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
6033 if (c->display_usage) {
6035 "net rpc shutdown\n"
6038 _("Shut down a remote RPC server"));
6042 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
6043 rpc_init_shutdown_internals, argc, argv);
6046 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
6047 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
6048 rpc_reg_shutdown_internals, argc, argv);
6054 /***************************************************************************
6055 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
6056 ***************************************************************************/
6059 * Add interdomain trust account to the RPC server.
6060 * All parameters (except for argc and argv) are passed by run_rpc_command
6063 * @param c A net_context structure.
6064 * @param domain_sid The domain sid acquired from the server.
6065 * @param cli A cli_state connected to the server.
6066 * @param mem_ctx Talloc context, destroyed on completion of the function.
6067 * @param argc Standard main() style argc.
6068 * @param argv Standard main() style argv. Initial components are already
6071 * @return normal NTSTATUS return code.
6074 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
6075 const struct dom_sid *domain_sid,
6076 const char *domain_name,
6077 struct cli_state *cli,
6078 struct rpc_pipe_client *pipe_hnd,
6079 TALLOC_CTX *mem_ctx,
6083 struct policy_handle connect_pol, domain_pol, user_pol;
6084 NTSTATUS status, result;
6086 struct lsa_String lsa_acct_name;
6088 uint32_t acct_flags=0;
6090 uint32_t access_granted = 0;
6091 union samr_UserInfo info;
6092 unsigned int orig_timeout;
6093 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6094 DATA_BLOB session_key = data_blob_null;
6095 TALLOC_CTX *frame = NULL;
6100 _(" net rpc trustdom add <domain_name> "
6101 "<trust password>\n"));
6102 return NT_STATUS_INVALID_PARAMETER;
6105 frame = talloc_stackframe();
6108 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6111 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
6112 status = NT_STATUS_NO_MEMORY;
6115 if (!strupper_m(acct_name)) {
6116 status = NT_STATUS_INVALID_PARAMETER;
6120 init_lsa_String(&lsa_acct_name, acct_name);
6122 status = cli_get_session_key(frame, pipe_hnd, &session_key);
6123 if (!NT_STATUS_IS_OK(status)) {
6124 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
6125 nt_errstr(status)));
6129 /* Get samr policy handle */
6130 status = dcerpc_samr_Connect2(b, frame,
6132 MAXIMUM_ALLOWED_ACCESS,
6135 if (!NT_STATUS_IS_OK(status)) {
6138 if (!NT_STATUS_IS_OK(result)) {
6143 /* Get domain policy handle */
6144 status = dcerpc_samr_OpenDomain(b, frame,
6146 MAXIMUM_ALLOWED_ACCESS,
6147 discard_const_p(struct dom_sid2, domain_sid),
6150 if (!NT_STATUS_IS_OK(status)) {
6153 if (!NT_STATUS_IS_OK(result)) {
6158 /* This call can take a long time - allow the server to time out.
6159 * 35 seconds should do it. */
6161 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
6163 /* Create trusting domain's account */
6164 acb_info = ACB_NORMAL;
6165 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
6166 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
6167 SAMR_USER_ACCESS_SET_PASSWORD |
6168 SAMR_USER_ACCESS_GET_ATTRIBUTES |
6169 SAMR_USER_ACCESS_SET_ATTRIBUTES;
6171 status = dcerpc_samr_CreateUser2(b, frame,
6180 if (!NT_STATUS_IS_OK(status)) {
6183 /* And restore our original timeout. */
6184 rpccli_set_timeout(pipe_hnd, orig_timeout);
6186 if (!NT_STATUS_IS_OK(result)) {
6188 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
6189 acct_name, nt_errstr(result));
6194 struct samr_CryptPassword crypt_pwd;
6196 ZERO_STRUCT(info.info23);
6198 status = init_samr_CryptPassword(argv[1],
6201 if (!NT_STATUS_IS_OK(status)) {
6205 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
6206 SAMR_FIELD_NT_PASSWORD_PRESENT;
6207 info.info23.info.acct_flags = ACB_DOMTRUST;
6208 info.info23.password = crypt_pwd;
6210 status = dcerpc_samr_SetUserInfo2(b, frame,
6215 if (!NT_STATUS_IS_OK(status)) {
6219 if (!NT_STATUS_IS_OK(result)) {
6221 DEBUG(0,("Could not set trust account password: %s\n",
6222 nt_errstr(result)));
6227 status = NT_STATUS_OK;
6229 SAFE_FREE(acct_name);
6230 data_blob_clear_free(&session_key);
6236 * Create interdomain trust account for a remote domain.
6238 * @param argc Standard argc.
6239 * @param argv Standard argv without initial components.
6241 * @return Integer status (0 means success).
6244 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
6246 if (argc > 0 && !c->display_usage) {
6247 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6248 rpc_trustdom_add_internals, argc, argv);
6252 _("net rpc trustdom add <domain_name> <trust "
6260 * Remove interdomain trust account from the RPC server.
6261 * All parameters (except for argc and argv) are passed by run_rpc_command
6264 * @param c A net_context structure.
6265 * @param domain_sid The domain sid acquired from the server.
6266 * @param cli A cli_state connected to the server.
6267 * @param mem_ctx Talloc context, destroyed on completion of the function.
6268 * @param argc Standard main() style argc.
6269 * @param argv Standard main() style argv. Initial components are already
6272 * @return normal NTSTATUS return code.
6275 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6276 const struct dom_sid *domain_sid,
6277 const char *domain_name,
6278 struct cli_state *cli,
6279 struct rpc_pipe_client *pipe_hnd,
6280 TALLOC_CTX *mem_ctx,
6284 struct policy_handle connect_pol, domain_pol, user_pol;
6285 NTSTATUS status, result;
6287 struct dom_sid trust_acct_sid;
6288 struct samr_Ids user_rids, name_types;
6289 struct lsa_String lsa_acct_name;
6290 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6295 _(" net rpc trustdom del <domain_name>\n"));
6296 return NT_STATUS_INVALID_PARAMETER;
6300 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6302 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6304 if (acct_name == NULL)
6305 return NT_STATUS_NO_MEMORY;
6307 if (!strupper_m(acct_name)) {
6308 TALLOC_FREE(acct_name);
6309 return NT_STATUS_INVALID_PARAMETER;
6312 /* Get samr policy handle */
6313 status = dcerpc_samr_Connect2(b, mem_ctx,
6315 MAXIMUM_ALLOWED_ACCESS,
6318 if (!NT_STATUS_IS_OK(status)) {
6321 if (!NT_STATUS_IS_OK(result)) {
6326 /* Get domain policy handle */
6327 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6329 MAXIMUM_ALLOWED_ACCESS,
6330 discard_const_p(struct dom_sid2, domain_sid),
6333 if (!NT_STATUS_IS_OK(status)) {
6336 if (!NT_STATUS_IS_OK(result)) {
6341 init_lsa_String(&lsa_acct_name, acct_name);
6343 status = dcerpc_samr_LookupNames(b, mem_ctx,
6350 if (!NT_STATUS_IS_OK(status)) {
6351 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6353 acct_name, nt_errstr(status));
6356 if (!NT_STATUS_IS_OK(result)) {
6358 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6360 acct_name, nt_errstr(result) );
6363 if (user_rids.count != 1) {
6364 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6367 if (name_types.count != 1) {
6368 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6372 status = dcerpc_samr_OpenUser(b, mem_ctx,
6374 MAXIMUM_ALLOWED_ACCESS,
6378 if (!NT_STATUS_IS_OK(status)) {
6379 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6381 acct_name, nt_errstr(status) );
6385 if (!NT_STATUS_IS_OK(result)) {
6387 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6389 acct_name, nt_errstr(result) );
6393 /* append the rid to the domain sid */
6394 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6398 /* remove the sid */
6400 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6404 if (!NT_STATUS_IS_OK(status)) {
6405 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6406 " on user %s failed %s\n"),
6407 acct_name, nt_errstr(status));
6410 if (!NT_STATUS_IS_OK(result)) {
6412 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6413 " on user %s failed %s\n"),
6414 acct_name, nt_errstr(result) );
6421 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6424 if (!NT_STATUS_IS_OK(status)) {
6425 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6427 acct_name, nt_errstr(status));
6431 if (!NT_STATUS_IS_OK(result)) {
6433 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6435 acct_name, nt_errstr(result) );
6439 if (!NT_STATUS_IS_OK(result)) {
6440 d_printf(_("Could not set trust account password: %s\n"),
6450 * Delete interdomain trust account for a remote domain.
6452 * @param argc Standard argc.
6453 * @param argv Standard argv without initial components.
6455 * @return Integer status (0 means success).
6458 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6460 if (argc > 0 && !c->display_usage) {
6461 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6462 rpc_trustdom_del_internals, argc, argv);
6466 _("net rpc trustdom del <domain>\n"));
6471 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6472 struct cli_state *cli,
6473 TALLOC_CTX *mem_ctx,
6474 const char *domain_name)
6476 char *dc_name = NULL;
6477 const char *buffer = NULL;
6478 struct rpc_pipe_client *netr;
6481 struct dcerpc_binding_handle *b;
6483 /* Use NetServerEnum2 */
6485 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6487 return NT_STATUS_OK;
6490 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller "
6491 "for domain %s\n", domain_name));
6493 /* Try netr_GetDcName */
6495 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
6497 if (!NT_STATUS_IS_OK(status)) {
6501 b = netr->binding_handle;
6503 status = dcerpc_netr_GetDcName(b, mem_ctx,
6510 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6514 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller "
6515 "for domain %s\n", domain_name));
6517 if (!NT_STATUS_IS_OK(status)) {
6521 return werror_to_ntstatus(result);
6525 * Establish trust relationship to a trusting domain.
6526 * Interdomain account must already be created on remote PDC.
6528 * @param c A net_context structure.
6529 * @param argc Standard argc.
6530 * @param argv Standard argv without initial components.
6532 * @return Integer status (0 means success).
6535 static int rpc_trustdom_establish(struct net_context *c, int argc,
6538 struct cli_state *cli = NULL;
6539 struct sockaddr_storage server_ss;
6540 struct rpc_pipe_client *pipe_hnd = NULL;
6541 struct policy_handle connect_hnd;
6542 TALLOC_CTX *mem_ctx;
6543 NTSTATUS nt_status, result;
6544 struct dom_sid *domain_sid;
6547 const char *pwd = NULL;
6549 union lsa_PolicyInformation *info = NULL;
6550 struct dcerpc_binding_handle *b;
6551 union lsa_revision_info out_revision_info = {
6556 uint32_t out_version = 0;
6559 * Connect to \\server\ipc$ as 'our domain' account with password
6562 if (argc != 1 || c->display_usage) {
6565 _("net rpc trustdom establish <domain_name>\n"));
6569 domain_name = smb_xstrdup(argv[0]);
6570 if (!strupper_m(domain_name)) {
6571 SAFE_FREE(domain_name);
6575 /* account name used at first is our domain's name with '$' */
6576 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6579 if (!strupper_m(acct_name)) {
6580 SAFE_FREE(domain_name);
6581 SAFE_FREE(acct_name);
6584 cli_credentials_set_username(c->creds, acct_name, CRED_SPECIFIED);
6587 * opt_workgroup will be used by connection functions further,
6588 * hence it should be set to remote domain name instead of ours
6590 if (c->opt_workgroup) {
6591 c->opt_workgroup = smb_xstrdup(domain_name);
6594 /* find the domain controller */
6595 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6596 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6600 /* connect to ipc$ as username/password */
6601 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6602 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6604 /* Is it trusting domain account for sure ? */
6605 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6606 nt_errstr(nt_status)));
6610 /* store who we connected to */
6612 saf_store( domain_name, pdc_name );
6615 * Connect to \\server\ipc$ again (this time anonymously)
6618 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6621 if (NT_STATUS_IS_ERR(nt_status)) {
6622 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6623 domain_name, nt_errstr(nt_status)));
6627 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6628 "domain %s", domain_name))) {
6629 DEBUG(0, ("talloc_init() failed\n"));
6634 /* Make sure we're talking to a proper server */
6636 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6637 if (!NT_STATUS_IS_OK(nt_status)) {
6639 talloc_destroy(mem_ctx);
6644 * Call LsaOpenPolicy and LsaQueryInfo
6647 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6649 if (!NT_STATUS_IS_OK(nt_status)) {
6650 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6652 talloc_destroy(mem_ctx);
6656 b = pipe_hnd->binding_handle;
6658 nt_status = dcerpc_lsa_open_policy_fallback(b,
6660 pipe_hnd->srv_name_slash,
6667 if (any_nt_status_not_ok(nt_status, result, &nt_status)) {
6668 DBG_ERR("Couldn't open policy handle: %s\n",
6669 nt_errstr(nt_status));
6671 talloc_free(mem_ctx);
6675 /* Querying info level 5 */
6677 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6679 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6682 if (NT_STATUS_IS_ERR(nt_status)) {
6683 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6684 nt_errstr(nt_status)));
6686 talloc_destroy(mem_ctx);
6689 if (NT_STATUS_IS_ERR(result)) {
6690 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6691 nt_errstr(result)));
6693 talloc_destroy(mem_ctx);
6697 domain_sid = info->account_domain.sid;
6699 /* There should be actually query info level 3 (following nt serv behaviour),
6700 but I still don't know if it's _really_ necessary */
6703 * Store the password in secrets db
6706 pwd = cli_credentials_get_password(c->creds);
6708 if (!pdb_set_trusteddom_pw(domain_name, pwd, domain_sid)) {
6709 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6711 talloc_destroy(mem_ctx);
6716 * Close the pipes and clean up
6719 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6720 if (NT_STATUS_IS_ERR(nt_status)) {
6721 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6722 nt_errstr(nt_status)));
6724 talloc_destroy(mem_ctx);
6730 talloc_destroy(mem_ctx);
6732 d_printf(_("Trust to domain %s established\n"), domain_name);
6737 * Revoke trust relationship to the remote domain.
6739 * @param c A net_context structure.
6740 * @param argc Standard argc.
6741 * @param argv Standard argv without initial components.
6743 * @return Integer status (0 means success).
6746 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6752 if (argc < 1 || c->display_usage) {
6755 _("net rpc trustdom revoke <domain_name>\n"
6756 " Revoke trust relationship\n"
6757 " domain_name\tName of domain to revoke trust\n"));
6761 /* generate upper cased domain name */
6762 domain_name = smb_xstrdup(argv[0]);
6763 if (!strupper_m(domain_name)) {
6764 SAFE_FREE(domain_name);
6768 /* delete password of the trust */
6769 if (!pdb_del_trusteddom_pw(domain_name)) {
6770 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6777 SAFE_FREE(domain_name);
6781 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6782 const struct dom_sid *domain_sid,
6783 const char *domain_name,
6784 struct cli_state *cli,
6785 struct rpc_pipe_client *pipe_hnd,
6786 TALLOC_CTX *mem_ctx,
6790 struct dom_sid_buf sid_str;
6791 d_printf("%s\n", dom_sid_str_buf(domain_sid, &sid_str));
6792 return NT_STATUS_OK;
6795 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6797 struct dom_sid_buf sid_str;
6799 d_printf("%-20s%s\n",
6801 dom_sid_str_buf(dom_sid, &sid_str));
6804 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6805 TALLOC_CTX *mem_ctx,
6806 struct policy_handle *pol,
6807 struct dom_sid dom_sid,
6808 const char *trusted_dom_name)
6810 NTSTATUS nt_status, result;
6811 union lsa_TrustedDomainInfo *info = NULL;
6812 char *cleartextpwd = NULL;
6813 DATA_BLOB session_key;
6814 DATA_BLOB data = data_blob_null;
6815 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6817 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6820 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6823 if (NT_STATUS_IS_ERR(nt_status)) {
6824 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6825 nt_errstr(nt_status)));
6828 if (NT_STATUS_IS_ERR(result)) {
6830 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6831 nt_errstr(result)));
6835 data = data_blob(info->password.password->data,
6836 info->password.password->length);
6838 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6839 if (!NT_STATUS_IS_OK(nt_status)) {
6840 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6844 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6845 data_blob_free(&session_key);
6847 if (cleartextpwd == NULL) {
6848 DEBUG(0,("retrieved NULL password\n"));
6849 nt_status = NT_STATUS_UNSUCCESSFUL;
6853 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6854 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6855 nt_status = NT_STATUS_UNSUCCESSFUL;
6859 #ifdef DEBUG_PASSWORD
6861 struct dom_sid_buf buf;
6862 DEBUG(100,("successfully vampired trusted domain [%s], "
6863 "sid: [%s], password: [%s]\n",
6865 dom_sid_str_buf(&dom_sid, &buf),
6871 SAFE_FREE(cleartextpwd);
6872 data_blob_free(&data);
6877 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6880 /* common variables */
6881 TALLOC_CTX* mem_ctx;
6882 struct cli_state *cli = NULL;
6883 struct rpc_pipe_client *pipe_hnd = NULL;
6884 NTSTATUS nt_status, result;
6885 const char *domain_name = NULL;
6886 struct policy_handle connect_hnd;
6887 union lsa_PolicyInformation *info = NULL;
6889 /* trusted domains listing variables */
6890 unsigned int enum_ctx = 0;
6891 struct lsa_DomainList dom_list;
6893 struct dcerpc_binding_handle *b;
6894 union lsa_revision_info out_revision_info = {
6899 uint32_t out_version = 0;
6901 if (c->display_usage) {
6903 "net rpc trustdom vampire\n"
6906 _("Vampire trust relationship from remote server"));
6911 * Listing trusted domains (stored in secrets.tdb, if local)
6914 mem_ctx = talloc_init("trust relationships vampire");
6917 * set domain and pdc name to local samba server (default)
6918 * or to remote one given in command line
6921 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6922 domain_name = c->opt_workgroup;
6923 c->opt_target_workgroup = c->opt_workgroup;
6925 fstrcpy(pdc_name, lp_netbios_name());
6926 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6927 c->opt_target_workgroup = domain_name;
6930 /* open \PIPE\lsarpc and open policy handle */
6931 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6932 if (!NT_STATUS_IS_OK(nt_status)) {
6933 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6934 nt_errstr(nt_status)));
6935 talloc_destroy(mem_ctx);
6939 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6941 if (!NT_STATUS_IS_OK(nt_status)) {
6942 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6943 nt_errstr(nt_status) ));
6945 talloc_destroy(mem_ctx);
6949 b = pipe_hnd->binding_handle;
6951 nt_status = dcerpc_lsa_open_policy_fallback(b,
6953 pipe_hnd->srv_name_slash,
6960 if (any_nt_status_not_ok(nt_status, result, &nt_status)) {
6961 DBG_ERR("Couldn't open policy handle: %s\n",
6962 nt_errstr(nt_status));
6964 talloc_free(mem_ctx);
6968 /* query info level 5 to obtain sid of a domain being queried */
6969 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6971 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6975 if (NT_STATUS_IS_ERR(nt_status)) {
6976 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6977 nt_errstr(nt_status)));
6979 talloc_destroy(mem_ctx);
6982 if (NT_STATUS_IS_ERR(result)) {
6983 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6984 nt_errstr(result)));
6986 talloc_destroy(mem_ctx);
6991 * Keep calling LsaEnumTrustdom over opened pipe until
6992 * the end of enumeration is reached
6995 d_printf(_("Vampire trusted domains:\n\n"));
7000 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
7006 if (NT_STATUS_IS_ERR(nt_status)) {
7007 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7008 nt_errstr(nt_status)));
7010 talloc_destroy(mem_ctx);
7013 if (NT_STATUS_IS_ERR(result)) {
7015 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7016 nt_errstr(result)));
7018 talloc_destroy(mem_ctx);
7023 for (i = 0; i < dom_list.count; i++) {
7025 print_trusted_domain(dom_list.domains[i].sid,
7026 dom_list.domains[i].name.string);
7028 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
7029 *dom_list.domains[i].sid,
7030 dom_list.domains[i].name.string);
7031 if (!NT_STATUS_IS_OK(nt_status)) {
7033 talloc_destroy(mem_ctx);
7039 * in case of no trusted domains say something rather
7040 * than just display blank line
7042 if (!dom_list.count) d_printf(_("none\n"));
7044 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7046 /* close this connection before doing next one */
7047 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7048 if (NT_STATUS_IS_ERR(nt_status)) {
7049 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7050 nt_errstr(nt_status)));
7052 talloc_destroy(mem_ctx);
7056 /* close lsarpc pipe and connection to IPC$ */
7059 talloc_destroy(mem_ctx);
7063 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
7065 /* common variables */
7066 TALLOC_CTX* mem_ctx;
7067 struct cli_state *cli = NULL, *remote_cli = NULL;
7068 struct rpc_pipe_client *pipe_hnd = NULL;
7069 NTSTATUS nt_status, result;
7070 const char *domain_name = NULL;
7071 struct dom_sid *queried_dom_sid;
7072 int ascii_dom_name_len;
7073 struct policy_handle connect_hnd;
7074 union lsa_PolicyInformation *info = NULL;
7075 struct dcerpc_binding_handle *b = NULL;
7077 /* trusted domains listing variables */
7078 unsigned int num_domains, enum_ctx = 0;
7080 struct lsa_DomainList dom_list;
7084 /* trusting domains listing variables */
7085 struct policy_handle domain_hnd;
7086 struct samr_SamArray *trusts = NULL;
7087 union lsa_revision_info out_revision_info = {
7092 uint32_t out_version = 0;
7094 if (c->display_usage) {
7096 "net rpc trustdom list\n"
7099 _("List incoming and outgoing trust relationships"));
7104 * Listing trusted domains (stored in secrets.tdb, if local)
7107 mem_ctx = talloc_init("trust relationships listing");
7110 * set domain and pdc name to local samba server (default)
7111 * or to remote one given in command line
7114 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
7115 domain_name = c->opt_workgroup;
7116 c->opt_target_workgroup = c->opt_workgroup;
7118 fstrcpy(pdc_name, lp_netbios_name());
7119 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
7120 c->opt_target_workgroup = domain_name;
7123 /* open \PIPE\lsarpc and open policy handle */
7124 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
7125 if (!NT_STATUS_IS_OK(nt_status)) {
7126 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
7127 nt_errstr(nt_status)));
7128 talloc_destroy(mem_ctx);
7132 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
7134 if (!NT_STATUS_IS_OK(nt_status)) {
7135 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
7136 nt_errstr(nt_status) ));
7138 talloc_destroy(mem_ctx);
7142 b = pipe_hnd->binding_handle;
7144 nt_status = dcerpc_lsa_open_policy_fallback(b,
7146 pipe_hnd->srv_name_slash,
7153 if (any_nt_status_not_ok(nt_status, result, &nt_status)) {
7154 DBG_ERR("Couldn't open policy handle: %s\n",
7155 nt_errstr(nt_status));
7157 talloc_free(mem_ctx);
7161 /* query info level 5 to obtain sid of a domain being queried */
7162 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
7164 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
7168 if (NT_STATUS_IS_ERR(nt_status)) {
7169 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7170 nt_errstr(nt_status)));
7172 talloc_destroy(mem_ctx);
7175 if (NT_STATUS_IS_ERR(result)) {
7176 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7177 nt_errstr(result)));
7179 talloc_destroy(mem_ctx);
7183 queried_dom_sid = info->account_domain.sid;
7186 * Keep calling LsaEnumTrustdom over opened pipe until
7187 * the end of enumeration is reached
7190 d_printf(_("Trusted domains list:\n\n"));
7192 found_domain = false;
7195 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
7201 if (NT_STATUS_IS_ERR(nt_status)) {
7202 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7203 nt_errstr(nt_status)));
7205 talloc_destroy(mem_ctx);
7208 if (NT_STATUS_IS_ERR(result)) {
7209 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7210 nt_errstr(result)));
7212 talloc_destroy(mem_ctx);
7217 for (i = 0; i < dom_list.count; i++) {
7218 print_trusted_domain(dom_list.domains[i].sid,
7219 dom_list.domains[i].name.string);
7220 found_domain = true;
7224 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7227 * in case of no trusted domains say something rather
7228 * than just display blank line
7230 if (!found_domain) {
7231 d_printf(_("none\n"));
7234 /* close this connection before doing next one */
7235 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7236 if (NT_STATUS_IS_ERR(nt_status)) {
7237 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7238 nt_errstr(nt_status)));
7240 talloc_destroy(mem_ctx);
7244 TALLOC_FREE(pipe_hnd);
7247 * Listing trusting domains (stored in passdb backend, if local)
7250 d_printf(_("\nTrusting domains list:\n\n"));
7253 * Open \PIPE\samr and get needed policy handles
7255 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
7257 if (!NT_STATUS_IS_OK(nt_status)) {
7258 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
7260 talloc_destroy(mem_ctx);
7264 b = pipe_hnd->binding_handle;
7267 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
7269 SAMR_ACCESS_LOOKUP_DOMAIN,
7272 if (!NT_STATUS_IS_OK(nt_status)) {
7273 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7274 nt_errstr(nt_status)));
7276 talloc_destroy(mem_ctx);
7279 if (!NT_STATUS_IS_OK(result)) {
7281 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7282 nt_errstr(result)));
7284 talloc_destroy(mem_ctx);
7288 /* SamrOpenDomain - we have to open domain policy handle in order to be
7289 able to enumerate accounts*/
7290 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
7292 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
7296 if (!NT_STATUS_IS_OK(nt_status)) {
7297 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7298 nt_errstr(nt_status)));
7300 talloc_destroy(mem_ctx);
7303 if (!NT_STATUS_IS_OK(result)) {
7305 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7306 nt_errstr(result)));
7308 talloc_destroy(mem_ctx);
7313 * perform actual enumeration
7316 found_domain = false;
7318 enum_ctx = 0; /* reset enumeration context from last enumeration */
7321 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
7329 if (NT_STATUS_IS_ERR(nt_status)) {
7330 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7331 nt_errstr(nt_status)));
7333 talloc_destroy(mem_ctx);
7336 if (NT_STATUS_IS_ERR(result)) {
7338 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7339 nt_errstr(result)));
7341 talloc_destroy(mem_ctx);
7345 for (i = 0; i < num_domains; i++) {
7347 char *str = discard_const_p(char, trusts->entries[i].name.string);
7349 found_domain = true;
7352 * get each single domain's sid (do we _really_ need this ?):
7353 * 1) connect to domain's pdc
7354 * 2) query the pdc for domain's sid
7357 /* get rid of '$' tail */
7358 ascii_dom_name_len = strlen(str);
7359 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7360 str[ascii_dom_name_len - 1] = '\0';
7362 /* set opt_* variables to remote domain */
7363 if (!strupper_m(str)) {
7365 talloc_destroy(mem_ctx);
7368 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7369 c->opt_target_workgroup = c->opt_workgroup;
7371 d_printf("%-20s", str);
7373 /* connect to remote domain controller */
7374 nt_status = net_make_ipc_connection(c,
7375 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7377 if (NT_STATUS_IS_OK(nt_status)) {
7378 /* query for domain's sid */
7379 if (run_rpc_command(
7381 &ndr_table_lsarpc, 0,
7382 rpc_query_domain_sid, argc,
7384 d_printf(_("strange - couldn't get domain's sid\n"));
7386 cli_shutdown(remote_cli);
7389 d_fprintf(stderr, _("domain controller is not "
7390 "responding: %s\n"),
7391 nt_errstr(nt_status));
7392 d_printf(_("couldn't get domain's sid\n"));
7396 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7398 if (!found_domain) {
7402 /* close opened samr and domain policy handles */
7403 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7404 if (!NT_STATUS_IS_OK(nt_status)) {
7405 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7408 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7409 if (!NT_STATUS_IS_OK(nt_status)) {
7410 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7413 /* close samr pipe and connection to IPC$ */
7416 talloc_destroy(mem_ctx);
7421 * Entrypoint for 'net rpc trustdom' code.
7423 * @param argc Standard argc.
7424 * @param argv Standard argv without initial components.
7426 * @return Integer status (0 means success).
7429 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7431 struct functable func[] = {
7436 N_("Add trusting domain's account"),
7437 N_("net rpc trustdom add\n"
7438 " Add trusting domain's account")
7444 N_("Remove trusting domain's account"),
7445 N_("net rpc trustdom del\n"
7446 " Remove trusting domain's account")
7450 rpc_trustdom_establish,
7452 N_("Establish outgoing trust relationship"),
7453 N_("net rpc trustdom establish\n"
7454 " Establish outgoing trust relationship")
7458 rpc_trustdom_revoke,
7460 N_("Revoke outgoing trust relationship"),
7461 N_("net rpc trustdom revoke\n"
7462 " Revoke outgoing trust relationship")
7468 N_("List in- and outgoing domain trusts"),
7469 N_("net rpc trustdom list\n"
7470 " List in- and outgoing domain trusts")
7474 rpc_trustdom_vampire,
7476 N_("Vampire trusts from remote server"),
7477 N_("net rpc trustdom vampire\n"
7478 " Vampire trusts from remote server")
7480 {NULL, NULL, 0, NULL, NULL}
7483 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7487 * Check if a server will take rpc commands
7488 * @param flags Type of server to connect to (PDC, DMB, localhost)
7489 * if the host is not explicitly specified
7490 * @return bool (true means rpc supported)
7492 bool net_rpc_check(struct net_context *c, unsigned flags)
7494 struct cli_state *cli;
7496 struct sockaddr_storage server_ss;
7497 char *server_name = NULL;
7500 /* flags (i.e. server type) may depend on command */
7501 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7504 status = cli_connect_nb(c,
7510 SMB_SIGNING_IPC_DEFAULT,
7513 if (!NT_STATUS_IS_OK(status)) {
7514 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
7515 DBG_ERR("NetBIOS support disabled, unable to connect\n");
7519 status = smbXcli_negprot(cli->conn,
7521 lp_client_min_protocol(),
7522 lp_client_max_protocol(),
7526 if (!NT_STATUS_IS_OK(status))
7528 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7537 /* synchronise sam database via samsync rpc calls */
7538 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7540 struct functable func[] = {
7545 N_("Dump remote SAM database to Kerberos Keytab"),
7546 N_("net rpc vampire keytab\n"
7547 " Dump remote SAM database to Kerberos keytab "
7554 N_("Dump remote SAM database to passdb"),
7555 N_("net rpc vampire passdb\n"
7556 " Dump remote SAM database to passdb")
7559 {NULL, NULL, 0, NULL, NULL}
7563 if (c->display_usage) {
7568 _("Vampire remote SAM database"));
7572 return rpc_vampire_passdb(c, argc, argv);
7575 return net_run_function(c, argc, argv, "net rpc vampire", func);
7579 * Migrate everything from a print server.
7581 * @param c A net_context structure.
7582 * @param argc Standard main() style argc.
7583 * @param argv Standard main() style argv. Initial components are already
7586 * @return A shell status integer (0 for success).
7588 * The order is important !
7589 * To successfully add drivers the print queues have to exist !
7590 * Applying ACLs should be the last step, because you're easily locked out.
7593 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7598 if (c->display_usage) {
7600 "net rpc printer migrate all\n"
7603 _("Migrate everything from a print server"));
7608 d_printf(_("no server to migrate\n"));
7612 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7613 rpc_printer_migrate_printers_internals, argc,
7618 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7619 rpc_printer_migrate_drivers_internals, argc,
7624 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7625 rpc_printer_migrate_forms_internals, argc, argv);
7629 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7630 rpc_printer_migrate_settings_internals, argc,
7635 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7636 rpc_printer_migrate_security_internals, argc,
7642 * Migrate print drivers from a print server.
7644 * @param c A net_context structure.
7645 * @param argc Standard main() style argc.
7646 * @param argv Standard main() style argv. Initial components are already
7649 * @return A shell status integer (0 for success).
7651 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7654 if (c->display_usage) {
7656 "net rpc printer migrate drivers\n"
7659 _("Migrate print-drivers from a print-server"));
7664 d_printf(_("no server to migrate\n"));
7668 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7669 rpc_printer_migrate_drivers_internals,
7674 * Migrate print-forms from a print-server.
7676 * @param c A net_context structure.
7677 * @param argc Standard main() style argc.
7678 * @param argv Standard main() style argv. Initial components are already
7681 * @return A shell status integer (0 for success).
7683 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7686 if (c->display_usage) {
7688 "net rpc printer migrate forms\n"
7691 _("Migrate print-forms from a print-server"));
7696 d_printf(_("no server to migrate\n"));
7700 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7701 rpc_printer_migrate_forms_internals,
7706 * Migrate printers from a print-server.
7708 * @param c A net_context structure.
7709 * @param argc Standard main() style argc.
7710 * @param argv Standard main() style argv. Initial components are already
7713 * @return A shell status integer (0 for success).
7715 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7718 if (c->display_usage) {
7720 "net rpc printer migrate printers\n"
7723 _("Migrate printers from a print-server"));
7728 d_printf(_("no server to migrate\n"));
7732 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7733 rpc_printer_migrate_printers_internals,
7738 * Migrate printer-ACLs from a print-server
7740 * @param c A net_context structure.
7741 * @param argc Standard main() style argc.
7742 * @param argv Standard main() style argv. Initial components are already
7745 * @return A shell status integer (0 for success).
7747 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7750 if (c->display_usage) {
7752 "net rpc printer migrate security\n"
7755 _("Migrate printer-ACLs from a print-server"));
7760 d_printf(_("no server to migrate\n"));
7764 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7765 rpc_printer_migrate_security_internals,
7770 * Migrate printer-settings from a print-server.
7772 * @param c A net_context structure.
7773 * @param argc Standard main() style argc.
7774 * @param argv Standard main() style argv. Initial components are already
7777 * @return A shell status integer (0 for success).
7779 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7782 if (c->display_usage) {
7784 "net rpc printer migrate settings\n"
7787 _("Migrate printer-settings from a "
7793 d_printf(_("no server to migrate\n"));
7797 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7798 rpc_printer_migrate_settings_internals,
7803 * 'net rpc printer' entrypoint.
7805 * @param c A net_context structure.
7806 * @param argc Standard main() style argc.
7807 * @param argv Standard main() style argv. Initial components are already
7811 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7814 /* ouch: when addriver and setdriver are called from within
7815 rpc_printer_migrate_drivers_internals, the printer-queue already
7818 struct functable func[] = {
7821 rpc_printer_migrate_all,
7823 N_("Migrate all from remote to local print server"),
7824 N_("net rpc printer migrate all\n"
7825 " Migrate all from remote to local print server")
7829 rpc_printer_migrate_drivers,
7831 N_("Migrate drivers to local server"),
7832 N_("net rpc printer migrate drivers\n"
7833 " Migrate drivers to local server")
7837 rpc_printer_migrate_forms,
7839 N_("Migrate forms to local server"),
7840 N_("net rpc printer migrate forms\n"
7841 " Migrate forms to local server")
7845 rpc_printer_migrate_printers,
7847 N_("Migrate printers to local server"),
7848 N_("net rpc printer migrate printers\n"
7849 " Migrate printers to local server")
7853 rpc_printer_migrate_security,
7855 N_("Migrate printer ACLs to local server"),
7856 N_("net rpc printer migrate security\n"
7857 " Migrate printer ACLs to local server")
7861 rpc_printer_migrate_settings,
7863 N_("Migrate printer settings to local server"),
7864 N_("net rpc printer migrate settings\n"
7865 " Migrate printer settings to local server")
7867 {NULL, NULL, 0, NULL, NULL}
7870 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7875 * List printers on a remote RPC server.
7877 * @param c A net_context structure.
7878 * @param argc Standard main() style argc.
7879 * @param argv Standard main() style argv. Initial components are already
7882 * @return A shell status integer (0 for success).
7884 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7886 if (c->display_usage) {
7888 "net rpc printer list\n"
7891 _("List printers on a remote RPC server"));
7895 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7896 rpc_printer_list_internals,
7901 * List printer-drivers on a remote RPC server.
7903 * @param c A net_context structure.
7904 * @param argc Standard main() style argc.
7905 * @param argv Standard main() style argv. Initial components are already
7908 * @return A shell status integer (0 for success).
7910 static int rpc_printer_driver_list(struct net_context *c, int argc,
7913 if (c->display_usage) {
7915 "net rpc printer driver\n"
7918 _("List printer-drivers on a remote RPC server"));
7922 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7923 rpc_printer_driver_list_internals,
7928 * Publish printer in ADS via MSRPC.
7930 * @param c A net_context structure.
7931 * @param argc Standard main() style argc.
7932 * @param argv Standard main() style argv. Initial components are already
7935 * @return A shell status integer (0 for success).
7937 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7940 if (c->display_usage) {
7942 "net rpc printer publish publish\n"
7945 _("Publish printer in ADS via MSRPC"));
7949 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7950 rpc_printer_publish_publish_internals,
7955 * Update printer in ADS via MSRPC.
7957 * @param c A net_context structure.
7958 * @param argc Standard main() style argc.
7959 * @param argv Standard main() style argv. Initial components are already
7962 * @return A shell status integer (0 for success).
7964 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7966 if (c->display_usage) {
7968 "net rpc printer publish update\n"
7971 _("Update printer in ADS via MSRPC"));
7975 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7976 rpc_printer_publish_update_internals,
7981 * UnPublish printer in ADS via MSRPC.
7983 * @param c A net_context structure.
7984 * @param argc Standard main() style argc.
7985 * @param argv Standard main() style argv. Initial components are already
7988 * @return A shell status integer (0 for success).
7990 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7993 if (c->display_usage) {
7995 "net rpc printer publish unpublish\n"
7998 _("UnPublish printer in ADS via MSRPC"));
8002 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8003 rpc_printer_publish_unpublish_internals,
8008 * List published printers via MSRPC.
8010 * @param c A net_context structure.
8011 * @param argc Standard main() style argc.
8012 * @param argv Standard main() style argv. Initial components are already
8015 * @return A shell status integer (0 for success).
8017 static int rpc_printer_publish_list(struct net_context *c, int argc,
8020 if (c->display_usage) {
8022 "net rpc printer publish list\n"
8025 _("List published printers via MSRPC"));
8029 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8030 rpc_printer_publish_list_internals,
8036 * Publish printer in ADS.
8038 * @param c A net_context structure.
8039 * @param argc Standard main() style argc.
8040 * @param argv Standard main() style argv. Initial components are already
8043 * @return A shell status integer (0 for success).
8045 static int rpc_printer_publish(struct net_context *c, int argc,
8049 struct functable func[] = {
8052 rpc_printer_publish_publish,
8054 N_("Publish printer in AD"),
8055 N_("net rpc printer publish publish\n"
8056 " Publish printer in AD")
8060 rpc_printer_publish_update,
8062 N_("Update printer in AD"),
8063 N_("net rpc printer publish update\n"
8064 " Update printer in AD")
8068 rpc_printer_publish_unpublish,
8070 N_("Unpublish printer"),
8071 N_("net rpc printer publish unpublish\n"
8072 " Unpublish printer")
8076 rpc_printer_publish_list,
8078 N_("List published printers"),
8079 N_("net rpc printer publish list\n"
8080 " List published printers")
8082 {NULL, NULL, 0, NULL, NULL}
8086 if (c->display_usage) {
8087 d_printf(_("Usage:\n"));
8088 d_printf(_("net rpc printer publish\n"
8089 " List published printers\n"
8090 " Alias of net rpc printer publish "
8092 net_display_usage_from_functable(func);
8095 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8096 rpc_printer_publish_list_internals,
8100 return net_run_function(c, argc, argv, "net rpc printer publish",func);
8106 * Display rpc printer help page.
8108 * @param c A net_context structure.
8109 * @param argc Standard main() style argc.
8110 * @param argv Standard main() style argv. Initial components are already
8113 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
8115 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
8116 "\tlists all printers on print-server\n\n"));
8117 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
8118 "\tlists all printer-drivers on print-server\n\n"));
8119 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
8120 "\tpublishes printer settings in Active Directory\n"
8121 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
8122 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
8123 "\n\tmigrates printers from remote to local server\n\n"));
8124 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
8125 "\n\tmigrates printer-settings from remote to local server\n\n"));
8126 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
8127 "\n\tmigrates printer-drivers from remote to local server\n\n"));
8128 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
8129 "\n\tmigrates printer-forms from remote to local server\n\n"));
8130 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
8131 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
8132 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
8133 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
8134 "\tremote to local print-server\n\n"));
8135 net_common_methods_usage(c, argc, argv);
8136 net_common_flags_usage(c, argc, argv);
8138 "\t-v or --verbose\t\t\tgive verbose output\n"
8139 "\t --destination\t\tmigration target server (default: localhost)\n"));
8145 * 'net rpc printer' entrypoint.
8147 * @param c A net_context structure.
8148 * @param argc Standard main() style argc.
8149 * @param argv Standard main() style argv. Initial components are already
8152 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
8154 struct functable func[] = {
8159 N_("List all printers on print server"),
8160 N_("net rpc printer list\n"
8161 " List all printers on print server")
8165 rpc_printer_migrate,
8167 N_("Migrate printer to local server"),
8168 N_("net rpc printer migrate\n"
8169 " Migrate printer to local server")
8173 rpc_printer_driver_list,
8175 N_("List printer drivers"),
8176 N_("net rpc printer driver\n"
8177 " List printer drivers")
8181 rpc_printer_publish,
8183 N_("Publish printer in AD"),
8184 N_("net rpc printer publish\n"
8185 " Publish printer in AD")
8187 {NULL, NULL, 0, NULL, NULL}
8191 if (c->display_usage) {
8192 d_printf(_("Usage:\n"));
8193 d_printf(_("net rpc printer\n"
8194 " List printers\n"));
8195 net_display_usage_from_functable(func);
8198 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8199 rpc_printer_list_internals,
8203 return net_run_function(c, argc, argv, "net rpc printer", func);
8207 * 'net rpc' entrypoint.
8209 * @param c A net_context structure.
8210 * @param argc Standard main() style argc.
8211 * @param argv Standard main() style argv. Initial components are already
8215 int net_rpc(struct net_context *c, int argc, const char **argv)
8217 NET_API_STATUS status;
8219 struct functable func[] = {
8224 N_("Modify global audit settings"),
8225 N_("net rpc audit\n"
8226 " Modify global audit settings")
8232 N_("Show basic info about a domain"),
8234 " Show basic info about a domain")
8240 N_("Join a domain"),
8248 N_("Join a domain created in server manager"),
8249 N_("net rpc oldjoin\n"
8250 " Join a domain created in server manager")
8256 N_("Test that a join is valid"),
8257 N_("net rpc testjoin\n"
8258 " Test that a join is valid")
8264 N_("List/modify users"),
8266 " List/modify users")
8272 N_("Change a user password"),
8273 N_("net rpc password\n"
8274 " Change a user password\n"
8275 " Alias for net rpc user password")
8281 N_("List/modify groups"),
8282 N_("net rpc group\n"
8283 " List/modify groups")
8289 N_("List/modify shares"),
8290 N_("net rpc share\n"
8291 " List/modify shares")
8297 N_("List open files"),
8305 N_("List/modify printers"),
8306 N_("net rpc printer\n"
8307 " List/modify printers")
8311 net_rpc_changetrustpw,
8313 N_("Change trust account password"),
8314 N_("net rpc changetrustpw\n"
8315 " Change trust account password")
8321 N_("Modify domain trusts"),
8322 N_("net rpc trustdom\n"
8323 " Modify domain trusts")
8329 N_("Abort a remote shutdown"),
8330 N_("net rpc abortshutdown\n"
8331 " Abort a remote shutdown")
8337 N_("Shutdown a remote server"),
8338 N_("net rpc shutdown\n"
8339 " Shutdown a remote server")
8345 N_("Sync a remote NT PDC's data into local passdb"),
8346 N_("net rpc vampire\n"
8347 " Sync a remote NT PDC's data into local passdb")
8353 N_("Fetch the domain sid into local secrets.tdb"),
8354 N_("net rpc getsid\n"
8355 " Fetch the domain sid into local secrets.tdb")
8361 N_("Manage privileges assigned to SID"),
8362 N_("net rpc rights\n"
8363 " Manage privileges assigned to SID")
8369 N_("Start/stop/query remote services"),
8370 N_("net rpc service\n"
8371 " Start/stop/query remote services")
8377 N_("Manage registry hives"),
8378 N_("net rpc registry\n"
8379 " Manage registry hives")
8385 N_("Open interactive shell on remote server"),
8386 N_("net rpc shell\n"
8387 " Open interactive shell on remote server")
8393 N_("Manage trusts"),
8394 N_("net rpc trust\n"
8401 N_("Configure a remote samba server"),
8403 " Configure a remote samba server")
8405 {NULL, NULL, 0, NULL, NULL}
8408 status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
8413 return net_run_function(c, argc, argv, "net rpc", func);