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"
52 static int net_mode_share;
53 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
58 * @brief RPC based subcommands for the 'net' utility.
60 * This file should contain much of the functionality that used to
61 * be found in rpcclient, except that the commands should change
62 * less often, and the functionality should be sane (the user is not
63 * expected to know a rid/sid before they conduct an operation etc.)
65 * @todo Perhaps eventually these should be split out into a number
66 * of files, as this could get quite big.
71 * Many of the RPC functions need the domain sid. This function gets
72 * it at the start of every run
74 * @param cli A cli_state already connected to the remote machine
76 * @return The Domain SID of the remote machine.
79 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
80 struct dom_sid **domain_sid,
81 const char **domain_name)
83 struct rpc_pipe_client *lsa_pipe = NULL;
84 struct policy_handle pol;
85 NTSTATUS status, result;
86 union lsa_PolicyInformation *info = NULL;
87 struct dcerpc_binding_handle *b;
89 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
91 if (!NT_STATUS_IS_OK(status)) {
92 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
96 b = lsa_pipe->binding_handle;
98 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
99 SEC_FLAG_MAXIMUM_ALLOWED,
101 if (!NT_STATUS_IS_OK(status)) {
102 d_fprintf(stderr, "open_policy %s: %s\n",
108 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
110 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
113 if (!NT_STATUS_IS_OK(status)) {
114 d_fprintf(stderr, "lsaquery %s: %s\n",
119 if (!NT_STATUS_IS_OK(result)) {
120 d_fprintf(stderr, "lsaquery %s: %s\n",
126 *domain_name = info->account_domain.name.string;
127 *domain_sid = info->account_domain.sid;
129 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
130 TALLOC_FREE(lsa_pipe);
136 * Run a single RPC command, from start to finish.
138 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
139 * @param conn_flag a NET_FLAG_ combination. Passed to
140 * net_make_ipc_connection.
141 * @param argc Standard main() style argc.
142 * @param argv Standard main() style argv. Initial components are already
144 * @return A shell status integer (0 for success).
147 int run_rpc_command(struct net_context *c,
148 struct cli_state *cli_arg,
149 const struct ndr_interface_table *table,
155 struct cli_state *cli = NULL;
156 struct rpc_pipe_client *pipe_hnd = NULL;
159 struct dom_sid *domain_sid;
160 const char *domain_name;
163 /* make use of cli_state handed over as an argument, if possible */
165 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
166 if (!NT_STATUS_IS_OK(nt_status)) {
167 DEBUG(1, ("failed to make ipc connection: %s\n",
168 nt_errstr(nt_status)));
181 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
182 DEBUG(0, ("talloc_init() failed\n"));
186 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
188 if (!NT_STATUS_IS_OK(nt_status)) {
192 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
193 if (lp_client_schannel()
194 && (ndr_syntax_id_equal(&table->syntax_id,
195 &ndr_table_netlogon.syntax_id))) {
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,
201 &pipe_hnd, c, &c->netlogon_creds);
202 if (!NT_STATUS_IS_OK(nt_status)) {
203 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
204 nt_errstr(nt_status) ));
208 if (conn_flags & NET_FLAGS_SEAL) {
209 struct cli_credentials *creds = NULL;
211 creds = net_context_creds(c, mem_ctx);
213 DBG_ERR("net_rpc_ntlm_creds() failed\n");
214 nt_status = NT_STATUS_INTERNAL_ERROR;
218 nt_status = cli_rpc_pipe_open_with_creds(
220 (conn_flags & NET_FLAGS_TCP) ?
221 NCACN_IP_TCP : NCACN_NP,
222 DCERPC_AUTH_TYPE_NTLMSSP,
223 DCERPC_AUTH_LEVEL_PRIVACY,
224 smbXcli_conn_remote_name(cli->conn),
227 nt_status = cli_rpc_pipe_open_noauth(
231 if (!NT_STATUS_IS_OK(nt_status)) {
232 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
234 nt_errstr(nt_status) ));
240 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
242 if (!NT_STATUS_IS_OK(nt_status)) {
243 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
246 DEBUG(5, ("rpc command function succeeded\n"));
249 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
251 TALLOC_FREE(pipe_hnd);
256 /* close the connection only if it was opened here */
261 talloc_destroy(mem_ctx);
266 * Force a change of the trust account password.
268 * All parameters are provided by the run_rpc_command function, except for
269 * argc, argv which are passed through.
271 * @param domain_sid The domain sid acquired from the remote server.
272 * @param cli A cli_state connected to the server.
273 * @param mem_ctx Talloc context, destroyed on completion of the function.
274 * @param argc Standard main() style argc.
275 * @param argv Standard main() style argv. Initial components are already
278 * @return Normal NTSTATUS return.
281 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
282 const struct dom_sid *domain_sid,
283 const char *domain_name,
284 struct cli_state *cli,
285 struct rpc_pipe_client *pipe_hnd,
291 const char *dcname = NULL;
294 return NT_STATUS_INTERNAL_ERROR;
297 dcname = smbXcli_conn_remote_name(cli->conn);
299 status = trust_pw_change(c->netlogon_creds,
301 pipe_hnd->binding_handle,
302 c->opt_target_workgroup,
305 if (!NT_STATUS_IS_OK(status)) {
306 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
315 * Force a change of the trust account password.
317 * @param argc Standard main() style argc.
318 * @param argv Standard main() style argv. Initial components are already
321 * @return A shell status integer (0 for success).
324 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
326 int conn_flags = NET_FLAGS_PDC;
328 if (!c->opt_user_specified && !c->opt_kerberos) {
329 conn_flags |= NET_FLAGS_ANONYMOUS;
332 if (c->display_usage) {
334 "net rpc changetrustpw\n"
337 _("Change the machine trust password"));
341 return run_rpc_command(c, NULL, &ndr_table_netlogon,
343 rpc_changetrustpw_internals,
348 * Join a domain, the old way. This function exists to allow
349 * the message to be displayed when oldjoin was explicitly
350 * requested, but not when it was implied by "net rpc join".
352 * This uses 'machinename' as the initial password, and changes it.
354 * The password should be created with 'server manager' or equiv first.
356 * @param argc Standard main() style argc.
357 * @param argv Standard main() style argv. Initial components are already
360 * @return A shell status integer (0 for success).
363 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
365 struct libnet_JoinCtx *r = NULL;
368 const char *domain = lp_workgroup(); /* FIXME */
369 bool modify_config = lp_config_backend_is_registry();
370 enum netr_SchannelType sec_chan_type;
373 if (c->display_usage) {
376 " Join a domain the old way\n");
380 mem_ctx = talloc_init("net_rpc_oldjoin");
385 werr = libnet_init_JoinCtx(mem_ctx, &r);
386 if (!W_ERROR_IS_OK(werr)) {
391 check what type of join - if the user wants to join as
392 a BDC, the server must agree that we are a BDC.
395 sec_chan_type = get_sec_channel_type(argv[0]);
397 sec_chan_type = get_sec_channel_type(NULL);
401 d_fprintf(stderr, _("Could not initialise message context. "
402 "Try running as root\n"));
403 werr = WERR_ACCESS_DENIED;
407 pw = talloc_strndup(r, lp_netbios_name(), 14);
409 werr = WERR_NOT_ENOUGH_MEMORY;
413 r->in.msg_ctx = c->msg_ctx;
414 r->in.domain_name = domain;
415 r->in.secure_channel_type = sec_chan_type;
416 r->in.dc_name = c->opt_host;
417 r->in.admin_account = "";
418 r->in.admin_password = strlower_talloc(r, pw);
419 if (r->in.admin_password == NULL) {
420 werr = WERR_NOT_ENOUGH_MEMORY;
424 r->in.modify_config = modify_config;
425 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
426 WKSSVC_JOIN_FLAGS_JOIN_UNSECURE |
427 WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED;
429 werr = libnet_Join(mem_ctx, r);
430 if (!W_ERROR_IS_OK(werr)) {
434 /* Check the short name of the domain */
436 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
437 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
438 d_printf("domain name obtained from the server.\n");
439 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
440 d_printf("You should set \"workgroup = %s\" in %s.\n",
441 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
444 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
446 if (r->out.dns_domain_name) {
447 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
448 r->out.dns_domain_name);
450 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
451 r->out.netbios_domain_name);
454 /* print out informative error string in case there is one */
455 if (r->out.error_string != NULL) {
456 d_printf("%s\n", r->out.error_string);
459 TALLOC_FREE(mem_ctx);
464 if (c->opt_flags & NET_FLAGS_EXPECT_FALLBACK) {
468 /* issue an overall failure message at the end. */
469 d_fprintf(stderr, _("Failed to join domain: %s\n"),
470 r && r->out.error_string ? r->out.error_string :
471 get_friendly_werror_msg(werr));
474 TALLOC_FREE(mem_ctx);
480 * check that a join is OK
482 * @return A shell status integer (0 for success)
485 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
489 const char *domain = c->opt_target_workgroup;
490 const char *dc = c->opt_host;
492 if (c->display_usage) {
495 " Test if a join is OK\n");
499 mem_ctx = talloc_init("net_rpc_testjoin");
505 struct netr_DsRGetDCNameInfo *info;
508 d_fprintf(stderr, _("Could not initialise message context. "
509 "Try running as root\n"));
510 talloc_destroy(mem_ctx);
514 status = dsgetdcname(mem_ctx,
521 if (!NT_STATUS_IS_OK(status)) {
522 talloc_destroy(mem_ctx);
526 dc = strip_hostname(info->dc_unc);
529 /* Display success or failure */
530 status = libnet_join_ok(c->msg_ctx,
534 if (!NT_STATUS_IS_OK(status)) {
535 fprintf(stderr,"Join to domain '%s' is not valid: %s\n",
536 domain, nt_errstr(status));
537 talloc_destroy(mem_ctx);
541 printf("Join to '%s' is OK\n",domain);
542 talloc_destroy(mem_ctx);
548 * Join a domain using the administrator username and password
550 * @param argc Standard main() style argc
551 * @param argc Standard main() style argv. Initial components are already
552 * stripped. Currently not used.
553 * @return A shell status integer (0 for success)
557 static int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
559 struct libnet_JoinCtx *r = NULL;
562 const char *domain = lp_workgroup(); /* FIXME */
563 bool modify_config = lp_config_backend_is_registry();
564 enum netr_SchannelType sec_chan_type;
566 if (c->display_usage) {
569 " Join a domain the new way\n");
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 if (strlen(lp_netbios_name()) > 15) {
695 d_printf(_("Our netbios name can be at most 15 chars long, "
696 "\"%s\" is %u chars long\n"),
697 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
701 c->opt_flags |= NET_FLAGS_EXPECT_FALLBACK;
702 ret = net_rpc_oldjoin(c, argc, argv);
703 c->opt_flags &= ~NET_FLAGS_EXPECT_FALLBACK;
708 return net_rpc_join_newstyle(c, argc, argv);
712 * display info about a rpc domain
714 * All parameters are provided by the run_rpc_command function, except for
715 * argc, argv which are passed through.
717 * @param domain_sid The domain sid acquired from the remote server
718 * @param cli A cli_state connected to the server.
719 * @param mem_ctx Talloc context, destroyed on completion of the function.
720 * @param argc Standard main() style argc.
721 * @param argv Standard main() style argv. Initial components are already
724 * @return Normal NTSTATUS return.
727 NTSTATUS rpc_info_internals(struct net_context *c,
728 const struct dom_sid *domain_sid,
729 const char *domain_name,
730 struct cli_state *cli,
731 struct rpc_pipe_client *pipe_hnd,
736 struct policy_handle connect_pol, domain_pol;
737 NTSTATUS status, result;
738 union samr_DomainInfo *info = NULL;
739 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
741 /* Get sam policy handle */
742 status = dcerpc_samr_Connect2(b, mem_ctx,
744 MAXIMUM_ALLOWED_ACCESS,
747 if (!NT_STATUS_IS_OK(status)) {
748 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
753 if (!NT_STATUS_IS_OK(result)) {
755 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
760 /* Get domain policy handle */
761 status = dcerpc_samr_OpenDomain(b, mem_ctx,
763 MAXIMUM_ALLOWED_ACCESS,
764 discard_const_p(struct dom_sid2, domain_sid),
767 if (!NT_STATUS_IS_OK(status)) {
768 d_fprintf(stderr, _("Could not open domain: %s\n"),
772 if (!NT_STATUS_IS_OK(result)) {
774 d_fprintf(stderr, _("Could not open domain: %s\n"),
779 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
784 if (!NT_STATUS_IS_OK(status)) {
788 if (NT_STATUS_IS_OK(result)) {
789 struct dom_sid_buf sid_str;
791 d_printf(_("Domain Name: %s\n"),
792 info->general.domain_name.string);
793 d_printf(_("Domain SID: %s\n"),
794 dom_sid_str_buf(domain_sid, &sid_str));
795 d_printf(_("Sequence number: %llu\n"),
796 (unsigned long long)info->general.sequence_num);
797 d_printf(_("Num users: %u\n"), info->general.num_users);
798 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
799 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
807 * 'net rpc info' entrypoint.
808 * @param argc Standard main() style argc.
809 * @param argv Standard main() style argv. Initial components are already
813 int net_rpc_info(struct net_context *c, int argc, const char **argv)
815 if (c->display_usage) {
820 _("Display information about the domain"));
824 return run_rpc_command(c, NULL, &ndr_table_samr,
825 NET_FLAGS_PDC, rpc_info_internals,
830 * Fetch domain SID into the local secrets.tdb.
832 * All parameters are provided by the run_rpc_command function, except for
833 * argc, argv which are passed through.
835 * @param domain_sid The domain sid acquired from the remote server.
836 * @param cli A cli_state connected to the server.
837 * @param mem_ctx Talloc context, destroyed on completion of the function.
838 * @param argc Standard main() style argc.
839 * @param argv Standard main() style argv. Initial components are already
842 * @return Normal NTSTATUS return.
845 static NTSTATUS rpc_getsid_internals(struct net_context *c,
846 const struct dom_sid *domain_sid,
847 const char *domain_name,
848 struct cli_state *cli,
849 struct rpc_pipe_client *pipe_hnd,
854 struct dom_sid_buf sid_str;
856 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
857 dom_sid_str_buf(domain_sid, &sid_str),
860 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
861 DEBUG(0,("Can't store domain SID\n"));
862 return NT_STATUS_UNSUCCESSFUL;
869 * 'net rpc getsid' entrypoint.
870 * @param argc Standard main() style argc.
871 * @param argv Standard main() style argv. Initial components are already
875 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
877 int conn_flags = NET_FLAGS_PDC;
879 if (!c->opt_user_specified && !c->opt_kerberos) {
880 conn_flags |= NET_FLAGS_ANONYMOUS;
883 if (c->display_usage) {
888 _("Fetch domain SID into local secrets.tdb"));
892 return run_rpc_command(c, NULL, &ndr_table_samr,
894 rpc_getsid_internals,
898 /****************************************************************************/
901 * Basic usage function for 'net rpc user'.
902 * @param argc Standard main() style argc.
903 * @param argv Standard main() style argv. Initial components are already
907 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
909 return net_user_usage(c, argc, argv);
913 * Add a new user to a remote RPC server.
915 * @param argc Standard main() style argc.
916 * @param argv Standard main() style argv. Initial components are already
919 * @return A shell status integer (0 for success).
922 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
924 NET_API_STATUS status;
925 struct USER_INFO_1 info1;
926 uint32_t parm_error = 0;
928 if (argc < 1 || c->display_usage) {
929 rpc_user_usage(c, argc, argv);
935 info1.usri1_name = argv[0];
937 info1.usri1_password = argv[1];
940 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
943 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
944 argv[0], libnetapi_get_error_string(c->netapi_ctx,
948 d_printf(_("Added user '%s'.\n"), argv[0]);
955 * Rename a user on a remote RPC server.
957 * @param argc Standard main() style argc.
958 * @param argv Standard main() style argv. Initial components are already
961 * @return A shell status integer (0 for success).
964 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
966 NET_API_STATUS status;
967 struct USER_INFO_0 u0;
968 uint32_t parm_err = 0;
970 if (argc != 2 || c->display_usage) {
971 rpc_user_usage(c, argc, argv);
975 u0.usri0_name = argv[1];
977 status = NetUserSetInfo(c->opt_host, argv[0],
978 0, (uint8_t *)&u0, &parm_err);
981 _("Failed to rename user from %s to %s - %s\n"),
983 libnetapi_get_error_string(c->netapi_ctx, status));
985 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
992 * Set a user's primary group
994 * @param argc Standard main() style argc.
995 * @param argv Standard main() style argv. Initial components are already
998 * @return A shell status integer (0 for success).
1001 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
1004 NET_API_STATUS status;
1006 struct GROUP_INFO_2 *g2;
1007 struct USER_INFO_1051 u1051;
1008 uint32_t parm_err = 0;
1010 if (argc != 2 || c->display_usage) {
1011 rpc_user_usage(c, argc, argv);
1015 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
1017 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
1019 libnetapi_get_error_string(c->netapi_ctx, status));
1022 g2 = (struct GROUP_INFO_2 *)buffer;
1024 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
1026 NetApiBufferFree(buffer);
1028 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
1029 (uint8_t *)&u1051, &parm_err);
1032 _("Failed to set user's primary group %s to %s - "
1033 "%s\n"), argv[0], argv[1],
1034 libnetapi_get_error_string(c->netapi_ctx, status));
1036 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
1043 * Delete a user from a remote RPC server.
1045 * @param argc Standard main() style argc.
1046 * @param argv Standard main() style argv. Initial components are already
1049 * @return A shell status integer (0 for success).
1052 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
1054 NET_API_STATUS status;
1056 if (argc < 1 || c->display_usage) {
1057 rpc_user_usage(c, argc, argv);
1061 status = NetUserDel(c->opt_host, argv[0]);
1064 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
1066 libnetapi_get_error_string(c->netapi_ctx, status));
1069 d_printf(_("Deleted user '%s'.\n"), argv[0]);
1076 * Set a user's password on a remote RPC server.
1078 * @param argc Standard main() style argc.
1079 * @param argv Standard main() style argv. Initial components are already
1082 * @return A shell status integer (0 for success).
1085 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
1087 NET_API_STATUS status;
1088 char *prompt = NULL;
1089 struct USER_INFO_1003 u1003;
1090 uint32_t parm_err = 0;
1093 if (argc < 1 || c->display_usage) {
1094 rpc_user_usage(c, argc, argv);
1099 u1003.usri1003_password = argv[1];
1101 char pwd[256] = {0};
1102 ret = asprintf(&prompt, _("Enter new password for %s:"),
1108 ret = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
1114 u1003.usri1003_password = talloc_strdup(c, pwd);
1115 if (u1003.usri1003_password == NULL) {
1120 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
1122 /* Display results */
1125 _("Failed to set password for '%s' with error: %s.\n"),
1126 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1135 * List a user's groups from a remote RPC server.
1137 * @param argc Standard main() style argc.
1138 * @param argv Standard main() style argv. Initial components are already
1141 * @return A shell status integer (0 for success)
1144 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1147 NET_API_STATUS status;
1148 struct GROUP_USERS_INFO_0 *u0 = NULL;
1149 uint32_t entries_read = 0;
1150 uint32_t total_entries = 0;
1154 if (argc < 1 || c->display_usage) {
1155 rpc_user_usage(c, argc, argv);
1159 status = NetUserGetGroups(c->opt_host,
1162 (uint8_t **)(void *)&u0,
1168 _("Failed to get groups for '%s' with error: %s.\n"),
1169 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1174 for (i=0; i < entries_read; i++) {
1175 printf("%s\n", u0->grui0_name);
1183 * List users on a remote RPC server.
1185 * All parameters are provided by the run_rpc_command function, except for
1186 * argc, argv which are passed through.
1188 * @param domain_sid The domain sid acquired from the remote server.
1189 * @param cli A cli_state connected to the server.
1190 * @param mem_ctx Talloc context, destroyed on completion of the function.
1191 * @param argc Standard main() style argc.
1192 * @param argv Standard main() style argv. Initial components are already
1195 * @return Normal NTSTATUS return.
1198 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
1200 NET_API_STATUS status;
1201 uint32_t start_idx=0, num_entries, i, loop_count = 0;
1202 struct NET_DISPLAY_USER *info = NULL;
1203 void *buffer = NULL;
1205 /* Query domain users */
1206 if (c->opt_long_list_entries)
1207 d_printf(_("\nUser name Comment"
1208 "\n-----------------------------\n"));
1210 uint32_t max_entries, max_size;
1212 dcerpc_get_query_dispinfo_params(
1213 loop_count, &max_entries, &max_size);
1215 status = NetQueryDisplayInformation(c->opt_host,
1222 if (status != 0 && status != ERROR_MORE_DATA) {
1226 info = (struct NET_DISPLAY_USER *)buffer;
1228 for (i = 0; i < num_entries; i++) {
1230 if (c->opt_long_list_entries)
1231 printf("%-21.21s %s\n", info->usri1_name,
1232 info->usri1_comment);
1234 printf("%s\n", info->usri1_name);
1238 NetApiBufferFree(buffer);
1241 start_idx += num_entries;
1243 } while (status == ERROR_MORE_DATA);
1249 * 'net rpc user' entrypoint.
1250 * @param argc Standard main() style argc.
1251 * @param argv Standard main() style argv. Initial components are already
1255 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1257 NET_API_STATUS status;
1259 struct functable func[] = {
1264 N_("Add specified user"),
1265 N_("net rpc user add\n"
1266 " Add specified user")
1272 N_("List domain groups of user"),
1273 N_("net rpc user info\n"
1274 " List domain groups of user")
1280 N_("Remove specified user"),
1281 N_("net rpc user delete\n"
1282 " Remove specified user")
1288 N_("Change user password"),
1289 N_("net rpc user password\n"
1290 " Change user password")
1296 N_("Rename specified user"),
1297 N_("net rpc user rename\n"
1298 " Rename specified user")
1302 rpc_user_setprimarygroup,
1304 "Set a user's primary group",
1305 "net rpc user setprimarygroup\n"
1306 " Set a user's primary group"
1308 {NULL, NULL, 0, NULL, NULL}
1311 status = libnetapi_net_init(&c->netapi_ctx);
1315 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1316 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1317 if (c->opt_kerberos) {
1318 libnetapi_set_use_kerberos(c->netapi_ctx);
1322 if (c->display_usage) {
1327 _("List all users"));
1328 net_display_usage_from_functable(func);
1332 return rpc_user_list(c, argc, argv);
1335 return net_run_function(c, argc, argv, "net rpc user", func);
1338 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1339 TALLOC_CTX *mem_ctx,
1340 struct rpc_sh_ctx *ctx,
1341 struct rpc_pipe_client *pipe_hnd,
1342 int argc, const char **argv)
1344 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1347 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1348 TALLOC_CTX *mem_ctx,
1349 struct rpc_sh_ctx *ctx,
1350 struct rpc_pipe_client *pipe_hnd,
1351 int argc, const char **argv)
1353 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1356 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1357 TALLOC_CTX *mem_ctx,
1358 struct rpc_sh_ctx *ctx,
1359 struct rpc_pipe_client *pipe_hnd,
1360 int argc, const char **argv,
1362 struct net_context *c,
1363 TALLOC_CTX *mem_ctx,
1364 struct rpc_sh_ctx *ctx,
1365 struct rpc_pipe_client *pipe_hnd,
1366 struct policy_handle *user_hnd,
1367 int argc, const char **argv))
1369 struct policy_handle connect_pol, domain_pol, user_pol;
1370 NTSTATUS status, result;
1373 enum lsa_SidType type;
1374 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1377 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1379 return NT_STATUS_INVALID_PARAMETER;
1382 ZERO_STRUCT(connect_pol);
1383 ZERO_STRUCT(domain_pol);
1384 ZERO_STRUCT(user_pol);
1386 status = net_rpc_lookup_name(c, mem_ctx, ctx->cli,
1387 argv[0], NULL, NULL, &sid, &type);
1388 if (!NT_STATUS_IS_OK(status)) {
1389 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1394 if (type != SID_NAME_USER) {
1395 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1396 sid_type_lookup(type));
1397 status = NT_STATUS_NO_SUCH_USER;
1401 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1402 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1403 status = NT_STATUS_NO_SUCH_USER;
1407 status = dcerpc_samr_Connect2(b, mem_ctx,
1409 MAXIMUM_ALLOWED_ACCESS,
1412 if (!NT_STATUS_IS_OK(status)) {
1415 if (!NT_STATUS_IS_OK(result)) {
1420 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1422 MAXIMUM_ALLOWED_ACCESS,
1426 if (!NT_STATUS_IS_OK(status)) {
1429 if (!NT_STATUS_IS_OK(result)) {
1434 status = dcerpc_samr_OpenUser(b, mem_ctx,
1436 MAXIMUM_ALLOWED_ACCESS,
1440 if (!NT_STATUS_IS_OK(status)) {
1443 if (!NT_STATUS_IS_OK(result)) {
1448 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1451 if (is_valid_policy_hnd(&user_pol)) {
1452 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1454 if (is_valid_policy_hnd(&domain_pol)) {
1455 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1457 if (is_valid_policy_hnd(&connect_pol)) {
1458 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1463 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1464 TALLOC_CTX *mem_ctx,
1465 struct rpc_sh_ctx *ctx,
1466 struct rpc_pipe_client *pipe_hnd,
1467 struct policy_handle *user_hnd,
1468 int argc, const char **argv)
1470 NTSTATUS status, result;
1471 union samr_UserInfo *info = NULL;
1472 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1475 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1477 return NT_STATUS_INVALID_PARAMETER;
1480 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1485 if (!NT_STATUS_IS_OK(status)) {
1488 if (!NT_STATUS_IS_OK(result)) {
1492 d_printf(_("user rid: %d, group rid: %d\n"),
1494 info->info21.primary_gid);
1499 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1500 TALLOC_CTX *mem_ctx,
1501 struct rpc_sh_ctx *ctx,
1502 struct rpc_pipe_client *pipe_hnd,
1503 int argc, const char **argv)
1505 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1506 rpc_sh_user_show_internals);
1509 #define FETCHSTR(name, rec) \
1510 do { if (strequal(ctx->thiscmd, name)) { \
1511 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1514 #define SETSTR(name, rec, flag) \
1515 do { if (strequal(ctx->thiscmd, name)) { \
1516 init_lsa_String(&(info->info21.rec), argv[0]); \
1517 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1520 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1521 TALLOC_CTX *mem_ctx,
1522 struct rpc_sh_ctx *ctx,
1523 struct rpc_pipe_client *pipe_hnd,
1524 struct policy_handle *user_hnd,
1525 int argc, const char **argv)
1527 NTSTATUS status, result;
1528 const char *username;
1529 const char *oldval = "";
1530 union samr_UserInfo *info = NULL;
1531 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1534 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1535 _("Usage:"), ctx->whoami);
1536 return NT_STATUS_INVALID_PARAMETER;
1539 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1544 if (!NT_STATUS_IS_OK(status)) {
1547 if (!NT_STATUS_IS_OK(result)) {
1551 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1553 FETCHSTR("fullname", full_name);
1554 FETCHSTR("homedir", home_directory);
1555 FETCHSTR("homedrive", home_drive);
1556 FETCHSTR("logonscript", logon_script);
1557 FETCHSTR("profilepath", profile_path);
1558 FETCHSTR("description", description);
1561 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1565 if (strcmp(argv[0], "NULL") == 0) {
1569 ZERO_STRUCT(info->info21);
1571 SETSTR("fullname", full_name, FULL_NAME);
1572 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1573 SETSTR("homedrive", home_drive, HOME_DRIVE);
1574 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1575 SETSTR("profilepath", profile_path, PROFILE_PATH);
1576 SETSTR("description", description, DESCRIPTION);
1578 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1583 if (!NT_STATUS_IS_OK(status)) {
1589 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1590 ctx->thiscmd, oldval, argv[0]);
1597 #define HANDLEFLG(name, rec) \
1598 do { if (strequal(ctx->thiscmd, name)) { \
1599 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1601 newflags = oldflags | ACB_##rec; \
1603 newflags = oldflags & ~ACB_##rec; \
1606 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1607 TALLOC_CTX *mem_ctx,
1608 struct rpc_sh_ctx *ctx,
1609 struct rpc_pipe_client *pipe_hnd,
1610 int argc, const char **argv)
1612 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1613 rpc_sh_user_str_edit_internals);
1616 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1617 TALLOC_CTX *mem_ctx,
1618 struct rpc_sh_ctx *ctx,
1619 struct rpc_pipe_client *pipe_hnd,
1620 struct policy_handle *user_hnd,
1621 int argc, const char **argv)
1623 NTSTATUS status, result;
1624 const char *username;
1625 const char *oldval = "unknown";
1626 uint32_t oldflags, newflags;
1628 union samr_UserInfo *info = NULL;
1629 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1632 ((argc == 1) && !strequal(argv[0], "yes") &&
1633 !strequal(argv[0], "no"))) {
1634 /* TRANSATORS: The yes|no here are program keywords. Please do
1636 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1638 return NT_STATUS_INVALID_PARAMETER;
1641 newval = strequal(argv[0], "yes");
1643 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1648 if (!NT_STATUS_IS_OK(status)) {
1651 if (!NT_STATUS_IS_OK(result)) {
1655 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1656 oldflags = info->info21.acct_flags;
1657 newflags = info->info21.acct_flags;
1659 HANDLEFLG("disabled", DISABLED);
1660 HANDLEFLG("pwnotreq", PWNOTREQ);
1661 HANDLEFLG("autolock", AUTOLOCK);
1662 HANDLEFLG("pwnoexp", PWNOEXP);
1665 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1670 ZERO_STRUCT(info->info21);
1672 info->info21.acct_flags = newflags;
1673 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1675 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1680 if (!NT_STATUS_IS_OK(status)) {
1684 if (NT_STATUS_IS_OK(result)) {
1685 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1686 ctx->thiscmd, oldval, argv[0]);
1694 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1695 TALLOC_CTX *mem_ctx,
1696 struct rpc_sh_ctx *ctx,
1697 struct rpc_pipe_client *pipe_hnd,
1698 int argc, const char **argv)
1700 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1701 rpc_sh_user_flag_edit_internals);
1704 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1705 TALLOC_CTX *mem_ctx,
1706 struct rpc_sh_ctx *ctx)
1708 static struct rpc_sh_cmd cmds[] = {
1710 { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1711 N_("Show/Set a user's full name") },
1713 { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1714 N_("Show/Set a user's home directory") },
1716 { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1717 N_("Show/Set a user's home drive") },
1719 { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1720 N_("Show/Set a user's logon script") },
1722 { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1723 N_("Show/Set a user's profile path") },
1725 { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1726 N_("Show/Set a user's description") },
1728 { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1729 N_("Show/Set whether a user is disabled") },
1731 { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1732 N_("Show/Set whether a user locked out") },
1734 { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1735 N_("Show/Set whether a user does not need a password") },
1737 { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1738 N_("Show/Set whether a user's password does not expire") },
1740 { NULL, NULL, 0, NULL, NULL }
1746 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1747 TALLOC_CTX *mem_ctx,
1748 struct rpc_sh_ctx *ctx)
1750 static struct rpc_sh_cmd cmds[] = {
1752 { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
1753 N_("List available users") },
1755 { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
1756 N_("List the domain groups a user is member of") },
1758 { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
1759 N_("Show info about a user") },
1761 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1762 N_("Show/Modify a user's fields") },
1764 { NULL, NULL, 0, NULL, NULL }
1770 /****************************************************************************/
1773 * Basic usage function for 'net rpc group'.
1774 * @param argc Standard main() style argc.
1775 * @param argv Standard main() style argv. Initial components are already
1779 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1781 return net_group_usage(c, argc, argv);
1785 * Delete group on a remote RPC server.
1787 * All parameters are provided by the run_rpc_command function, except for
1788 * argc, argv which are passed through.
1790 * @param domain_sid The domain sid acquired from the remote server.
1791 * @param cli A cli_state connected to the server.
1792 * @param mem_ctx Talloc context, destroyed on completion of the function.
1793 * @param argc Standard main() style argc.
1794 * @param argv Standard main() style argv. Initial components are already
1797 * @return Normal NTSTATUS return.
1800 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1801 const struct dom_sid *domain_sid,
1802 const char *domain_name,
1803 struct cli_state *cli,
1804 struct rpc_pipe_client *pipe_hnd,
1805 TALLOC_CTX *mem_ctx,
1809 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1810 bool group_is_primary = false;
1811 NTSTATUS status, result;
1813 struct samr_RidAttrArray *rids = NULL;
1816 /* struct samr_RidWithAttribute *user_gids; */
1817 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1819 struct samr_Ids group_rids, name_types;
1820 struct lsa_String lsa_acct_name;
1821 union samr_UserInfo *info = NULL;
1823 if (argc < 1 || c->display_usage) {
1824 rpc_group_usage(c, argc,argv);
1825 return NT_STATUS_OK; /* ok? */
1828 status = dcerpc_samr_Connect2(b, mem_ctx,
1830 MAXIMUM_ALLOWED_ACCESS,
1833 if (!NT_STATUS_IS_OK(status)) {
1834 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1838 if (!NT_STATUS_IS_OK(result)) {
1840 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1844 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1846 MAXIMUM_ALLOWED_ACCESS,
1847 discard_const_p(struct dom_sid2, domain_sid),
1850 if (!NT_STATUS_IS_OK(status)) {
1851 d_fprintf(stderr, _("Request open_domain failed\n"));
1855 if (!NT_STATUS_IS_OK(result)) {
1857 d_fprintf(stderr, _("Request open_domain failed\n"));
1861 init_lsa_String(&lsa_acct_name, argv[0]);
1863 status = dcerpc_samr_LookupNames(b, mem_ctx,
1870 if (!NT_STATUS_IS_OK(status)) {
1871 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1875 if (!NT_STATUS_IS_OK(result)) {
1877 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1880 if (group_rids.count != 1) {
1881 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1884 if (name_types.count != 1) {
1885 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1889 switch (name_types.ids[0])
1891 case SID_NAME_DOM_GRP:
1892 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1894 MAXIMUM_ALLOWED_ACCESS,
1898 if (!NT_STATUS_IS_OK(status)) {
1899 d_fprintf(stderr, _("Request open_group failed"));
1903 if (!NT_STATUS_IS_OK(result)) {
1905 d_fprintf(stderr, _("Request open_group failed"));
1909 group_rid = group_rids.ids[0];
1911 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1915 if (!NT_STATUS_IS_OK(status)) {
1917 _("Unable to query group members of %s"),
1922 if (!NT_STATUS_IS_OK(result)) {
1925 _("Unable to query group members of %s"),
1930 if (c->opt_verbose) {
1932 _("Domain Group %s (rid: %d) has %d members\n"),
1933 argv[0],group_rid, rids->count);
1936 /* Check if group is anyone's primary group */
1937 for (i = 0; i < rids->count; i++)
1939 status = dcerpc_samr_OpenUser(b, mem_ctx,
1941 MAXIMUM_ALLOWED_ACCESS,
1945 if (!NT_STATUS_IS_OK(status)) {
1947 _("Unable to open group member %d\n"),
1952 if (!NT_STATUS_IS_OK(result)) {
1955 _("Unable to open group member %d\n"),
1960 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1965 if (!NT_STATUS_IS_OK(status)) {
1967 _("Unable to lookup userinfo for group "
1973 if (!NT_STATUS_IS_OK(result)) {
1976 _("Unable to lookup userinfo for group "
1982 if (info->info21.primary_gid == group_rid) {
1983 if (c->opt_verbose) {
1984 d_printf(_("Group is primary group "
1986 info->info21.account_name.string);
1988 group_is_primary = true;
1991 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1994 if (group_is_primary) {
1995 d_fprintf(stderr, _("Unable to delete group because "
1996 "some of it's members have it as primary "
1998 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
2002 /* remove all group members */
2003 for (i = 0; i < rids->count; i++)
2006 d_printf(_("Remove group member %d..."),
2008 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2012 if (!NT_STATUS_IS_OK(status)) {
2016 if (NT_STATUS_IS_OK(result)) {
2018 d_printf(_("ok\n"));
2021 d_printf("%s\n", _("failed"));
2026 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2029 if (!NT_STATUS_IS_OK(status)) {
2036 /* removing a local group is easier... */
2037 case SID_NAME_ALIAS:
2038 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2040 MAXIMUM_ALLOWED_ACCESS,
2044 if (!NT_STATUS_IS_OK(status)) {
2045 d_fprintf(stderr, _("Request open_alias failed\n"));
2048 if (!NT_STATUS_IS_OK(result)) {
2050 d_fprintf(stderr, _("Request open_alias failed\n"));
2054 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
2057 if (!NT_STATUS_IS_OK(status)) {
2065 d_fprintf(stderr, _("%s is of type %s. This command is only "
2066 "for deleting local or global groups\n"),
2067 argv[0],sid_type_lookup(name_types.ids[0]));
2068 status = NT_STATUS_UNSUCCESSFUL;
2072 if (NT_STATUS_IS_OK(status)) {
2074 d_printf(_("Deleted %s '%s'\n"),
2075 sid_type_lookup(name_types.ids[0]), argv[0]);
2077 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
2078 get_friendly_nt_error_msg(status));
2086 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
2088 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2089 rpc_group_delete_internals, argc,argv);
2092 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
2094 NET_API_STATUS status;
2095 struct GROUP_INFO_1 info1;
2096 uint32_t parm_error = 0;
2098 if (argc != 1 || c->display_usage) {
2099 rpc_group_usage(c, argc, argv);
2105 info1.grpi1_name = argv[0];
2106 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2107 info1.grpi1_comment = c->opt_comment;
2110 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2114 _("Failed to add group '%s' with error: %s.\n"),
2115 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2119 d_printf(_("Added group '%s'.\n"), argv[0]);
2125 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
2127 NET_API_STATUS status;
2128 struct LOCALGROUP_INFO_1 info1;
2129 uint32_t parm_error = 0;
2131 if (argc != 1 || c->display_usage) {
2132 rpc_group_usage(c, argc, argv);
2138 info1.lgrpi1_name = argv[0];
2139 if (c->opt_comment && strlen(c->opt_comment) > 0) {
2140 info1.lgrpi1_comment = c->opt_comment;
2143 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
2147 _("Failed to add alias '%s' with error: %s.\n"),
2148 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2152 d_printf(_("Added alias '%s'.\n"), argv[0]);
2158 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
2160 if (c->opt_localgroup)
2161 return rpc_alias_add_internals(c, argc, argv);
2163 return rpc_group_add_internals(c, argc, argv);
2166 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2167 TALLOC_CTX *mem_ctx,
2169 struct dom_sid *sid,
2170 enum lsa_SidType *type)
2172 struct dom_sid *sids = NULL;
2173 enum lsa_SidType *types = NULL;
2174 struct rpc_pipe_client *pipe_hnd = NULL;
2175 struct policy_handle lsa_pol;
2176 NTSTATUS status, result;
2177 struct dcerpc_binding_handle *b;
2179 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
2181 if (!NT_STATUS_IS_OK(status)) {
2185 b = pipe_hnd->binding_handle;
2187 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2188 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2190 if (!NT_STATUS_IS_OK(status)) {
2194 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2195 &name, NULL, 1, &sids, &types);
2197 if (NT_STATUS_IS_OK(status)) {
2198 sid_copy(sid, &sids[0]);
2202 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
2206 TALLOC_FREE(pipe_hnd);
2209 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
2211 /* Try as S-1-5-whatever */
2213 struct dom_sid tmp_sid;
2215 if (string_to_sid(&tmp_sid, name)) {
2216 sid_copy(sid, &tmp_sid);
2217 *type = SID_NAME_UNKNOWN;
2218 status = NT_STATUS_OK;
2225 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2226 TALLOC_CTX *mem_ctx,
2227 const struct dom_sid *group_sid,
2230 struct policy_handle connect_pol, domain_pol;
2231 NTSTATUS status, result;
2233 struct policy_handle group_pol;
2234 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2236 struct samr_Ids rids, rid_types;
2237 struct lsa_String lsa_acct_name;
2241 sid_copy(&sid, group_sid);
2243 if (!sid_split_rid(&sid, &group_rid)) {
2244 return NT_STATUS_UNSUCCESSFUL;
2247 /* Get sam policy handle */
2248 status = dcerpc_samr_Connect2(b, mem_ctx,
2250 MAXIMUM_ALLOWED_ACCESS,
2253 if (!NT_STATUS_IS_OK(status)) {
2256 if (!NT_STATUS_IS_OK(result)) {
2260 /* Get domain policy handle */
2261 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2263 MAXIMUM_ALLOWED_ACCESS,
2267 if (!NT_STATUS_IS_OK(status)) {
2270 if (!NT_STATUS_IS_OK(result)) {
2274 init_lsa_String(&lsa_acct_name, member);
2276 status = dcerpc_samr_LookupNames(b, mem_ctx,
2283 if (!NT_STATUS_IS_OK(status)) {
2284 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2289 if (!NT_STATUS_IS_OK(result)) {
2291 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2295 if (rids.count != 1) {
2296 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2299 if (rid_types.count != 1) {
2300 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2304 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2306 MAXIMUM_ALLOWED_ACCESS,
2310 if (!NT_STATUS_IS_OK(status)) {
2314 if (!NT_STATUS_IS_OK(result)) {
2319 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2322 0x0005, /* unknown flags */
2324 if (!NT_STATUS_IS_OK(status)) {
2331 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2335 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2336 struct cli_state *cli,
2337 TALLOC_CTX *mem_ctx,
2338 const struct dom_sid *alias_sid,
2341 struct policy_handle connect_pol, domain_pol;
2342 NTSTATUS status, result;
2344 struct policy_handle alias_pol;
2345 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2347 struct dom_sid member_sid;
2348 enum lsa_SidType member_type;
2352 sid_copy(&sid, alias_sid);
2354 if (!sid_split_rid(&sid, &alias_rid)) {
2355 return NT_STATUS_UNSUCCESSFUL;
2358 result = get_sid_from_name(cli, mem_ctx,
2359 member, &member_sid, &member_type);
2361 if (!NT_STATUS_IS_OK(result)) {
2362 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2367 /* Get sam policy handle */
2368 status = dcerpc_samr_Connect2(b, mem_ctx,
2370 MAXIMUM_ALLOWED_ACCESS,
2373 if (!NT_STATUS_IS_OK(status)) {
2376 if (!NT_STATUS_IS_OK(result)) {
2381 /* Get domain policy handle */
2382 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2384 MAXIMUM_ALLOWED_ACCESS,
2388 if (!NT_STATUS_IS_OK(status)) {
2391 if (!NT_STATUS_IS_OK(result)) {
2396 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2398 MAXIMUM_ALLOWED_ACCESS,
2402 if (!NT_STATUS_IS_OK(status)) {
2405 if (!NT_STATUS_IS_OK(result)) {
2409 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2413 if (!NT_STATUS_IS_OK(status)) {
2420 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2424 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2425 const struct dom_sid *domain_sid,
2426 const char *domain_name,
2427 struct cli_state *cli,
2428 struct rpc_pipe_client *pipe_hnd,
2429 TALLOC_CTX *mem_ctx,
2433 struct dom_sid group_sid;
2434 enum lsa_SidType group_type;
2436 if (argc != 2 || c->display_usage) {
2439 _("net rpc group addmem <group> <member>\n"
2440 " Add a member to a group\n"
2441 " group\tGroup to add member to\n"
2442 " member\tMember to add to group\n"));
2443 return NT_STATUS_UNSUCCESSFUL;
2446 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2447 &group_sid, &group_type))) {
2448 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2450 return NT_STATUS_UNSUCCESSFUL;
2453 if (group_type == SID_NAME_DOM_GRP) {
2454 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2455 &group_sid, argv[1]);
2457 if (!NT_STATUS_IS_OK(result)) {
2458 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2459 argv[1], argv[0], nt_errstr(result));
2464 if (group_type == SID_NAME_ALIAS) {
2465 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, cli, mem_ctx,
2466 &group_sid, argv[1]);
2468 if (!NT_STATUS_IS_OK(result)) {
2469 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2470 argv[1], argv[0], nt_errstr(result));
2475 d_fprintf(stderr, _("Can only add members to global or local groups "
2476 "which %s is not\n"), argv[0]);
2478 return NT_STATUS_UNSUCCESSFUL;
2481 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2483 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2484 rpc_group_addmem_internals,
2488 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2489 struct rpc_pipe_client *pipe_hnd,
2490 TALLOC_CTX *mem_ctx,
2491 const struct dom_sid *group_sid,
2494 struct policy_handle connect_pol, domain_pol;
2495 NTSTATUS status, result;
2497 struct policy_handle group_pol;
2498 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2500 struct samr_Ids rids, rid_types;
2501 struct lsa_String lsa_acct_name;
2505 sid_copy(&sid, group_sid);
2507 if (!sid_split_rid(&sid, &group_rid))
2508 return NT_STATUS_UNSUCCESSFUL;
2510 /* Get sam policy handle */
2511 status = dcerpc_samr_Connect2(b, mem_ctx,
2513 MAXIMUM_ALLOWED_ACCESS,
2516 if (!NT_STATUS_IS_OK(status)) {
2519 if (!NT_STATUS_IS_OK(result)) {
2524 /* Get domain policy handle */
2525 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2527 MAXIMUM_ALLOWED_ACCESS,
2531 if (!NT_STATUS_IS_OK(status)) {
2534 if (!NT_STATUS_IS_OK(result)) {
2538 init_lsa_String(&lsa_acct_name, member);
2540 status = dcerpc_samr_LookupNames(b, mem_ctx,
2547 if (!NT_STATUS_IS_OK(status)) {
2548 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2553 if (!NT_STATUS_IS_OK(result)) {
2555 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2559 if (rids.count != 1) {
2560 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2563 if (rid_types.count != 1) {
2564 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2568 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2570 MAXIMUM_ALLOWED_ACCESS,
2574 if (!NT_STATUS_IS_OK(status)) {
2577 if (!NT_STATUS_IS_OK(result)) {
2582 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2586 if (!NT_STATUS_IS_OK(status)) {
2592 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2596 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2597 struct cli_state *cli,
2598 TALLOC_CTX *mem_ctx,
2599 const struct dom_sid *alias_sid,
2602 struct policy_handle connect_pol, domain_pol;
2603 NTSTATUS status, result;
2605 struct policy_handle alias_pol;
2606 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2608 struct dom_sid member_sid;
2609 enum lsa_SidType member_type;
2613 sid_copy(&sid, alias_sid);
2615 if (!sid_split_rid(&sid, &alias_rid))
2616 return NT_STATUS_UNSUCCESSFUL;
2618 result = get_sid_from_name(cli, mem_ctx,
2619 member, &member_sid, &member_type);
2621 if (!NT_STATUS_IS_OK(result)) {
2622 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2627 /* Get sam policy handle */
2628 status = dcerpc_samr_Connect2(b, mem_ctx,
2630 MAXIMUM_ALLOWED_ACCESS,
2633 if (!NT_STATUS_IS_OK(status)) {
2636 if (!NT_STATUS_IS_OK(result)) {
2641 /* Get domain policy handle */
2642 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2644 MAXIMUM_ALLOWED_ACCESS,
2648 if (!NT_STATUS_IS_OK(status)) {
2651 if (!NT_STATUS_IS_OK(result)) {
2656 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2658 MAXIMUM_ALLOWED_ACCESS,
2662 if (!NT_STATUS_IS_OK(status)) {
2666 if (!NT_STATUS_IS_OK(result)) {
2670 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2675 if (!NT_STATUS_IS_OK(status)) {
2682 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2686 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2687 const struct dom_sid *domain_sid,
2688 const char *domain_name,
2689 struct cli_state *cli,
2690 struct rpc_pipe_client *pipe_hnd,
2691 TALLOC_CTX *mem_ctx,
2695 struct dom_sid group_sid;
2696 enum lsa_SidType group_type;
2698 if (argc != 2 || c->display_usage) {
2701 _("net rpc group delmem <group> <member>\n"
2702 " Delete a member from a group\n"
2703 " group\tGroup to delete member from\n"
2704 " member\tMember to delete from group\n"));
2705 return NT_STATUS_UNSUCCESSFUL;
2708 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2709 &group_sid, &group_type))) {
2710 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2712 return NT_STATUS_UNSUCCESSFUL;
2715 if (group_type == SID_NAME_DOM_GRP) {
2716 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2717 &group_sid, argv[1]);
2719 if (!NT_STATUS_IS_OK(result)) {
2720 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2721 argv[1], argv[0], nt_errstr(result));
2726 if (group_type == SID_NAME_ALIAS) {
2727 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, cli, mem_ctx,
2728 &group_sid, argv[1]);
2730 if (!NT_STATUS_IS_OK(result)) {
2731 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2732 argv[1], argv[0], nt_errstr(result));
2737 d_fprintf(stderr, _("Can only delete members from global or local "
2738 "groups which %s is not\n"), argv[0]);
2740 return NT_STATUS_UNSUCCESSFUL;
2743 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2745 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2746 rpc_group_delmem_internals,
2751 * List groups on a remote RPC server.
2753 * All parameters are provided by the run_rpc_command function, except for
2754 * argc, argv which are passes through.
2756 * @param domain_sid The domain sid acquired from the remote server.
2757 * @param cli A cli_state connected to the server.
2758 * @param mem_ctx Talloc context, destroyed on completion of the function.
2759 * @param argc Standard main() style argc.
2760 * @param argv Standard main() style argv. Initial components are already
2763 * @return Normal NTSTATUS return.
2766 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2767 const struct dom_sid *domain_sid,
2768 const char *domain_name,
2769 struct cli_state *cli,
2770 struct rpc_pipe_client *pipe_hnd,
2771 TALLOC_CTX *mem_ctx,
2775 struct policy_handle connect_pol, domain_pol;
2776 NTSTATUS status, result;
2777 uint32_t start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2778 struct samr_SamArray *groups = NULL;
2779 bool global = false;
2781 bool builtin = false;
2782 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2784 if (c->display_usage) {
2787 _("net rpc group list [global] [local] [builtin]\n"
2788 " List groups on RPC server\n"
2789 " global\tList global groups\n"
2790 " local\tList local groups\n"
2791 " builtin\tList builtin groups\n"
2792 " If none of global, local or builtin is "
2793 "specified, all three options are considered "
2795 return NT_STATUS_OK;
2804 for (i=0; i<argc; i++) {
2805 if (strequal(argv[i], "global"))
2808 if (strequal(argv[i], "local"))
2811 if (strequal(argv[i], "builtin"))
2815 /* Get sam policy handle */
2817 status = dcerpc_samr_Connect2(b, mem_ctx,
2819 MAXIMUM_ALLOWED_ACCESS,
2822 if (!NT_STATUS_IS_OK(status)) {
2825 if (!NT_STATUS_IS_OK(result)) {
2830 /* Get domain policy handle */
2832 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2834 MAXIMUM_ALLOWED_ACCESS,
2835 discard_const_p(struct dom_sid2, domain_sid),
2838 if (!NT_STATUS_IS_OK(status)) {
2841 if (!NT_STATUS_IS_OK(result)) {
2846 /* Query domain groups */
2847 if (c->opt_long_list_entries)
2848 d_printf(_("\nGroup name Comment"
2849 "\n-----------------------------\n"));
2851 uint32_t max_size, total_size, returned_size;
2852 union samr_DispInfo info;
2856 dcerpc_get_query_dispinfo_params(
2857 loop_count, &max_entries, &max_size);
2859 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2869 if (!NT_STATUS_IS_OK(status)) {
2872 num_entries = info.info3.count;
2873 start_idx += info.info3.count;
2875 if (!NT_STATUS_IS_OK(result) &&
2876 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2879 for (i = 0; i < num_entries; i++) {
2881 const char *group = NULL;
2882 const char *desc = NULL;
2884 group = info.info3.entries[i].account_name.string;
2885 desc = info.info3.entries[i].description.string;
2887 if (c->opt_long_list_entries)
2888 printf("%-21.21s %-50.50s\n",
2891 printf("%s\n", group);
2893 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2894 /* query domain aliases */
2899 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2906 if (!NT_STATUS_IS_OK(status)) {
2909 if (!NT_STATUS_IS_OK(result) &&
2910 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2913 for (i = 0; i < num_entries; i++) {
2915 const char *description = NULL;
2917 if (c->opt_long_list_entries) {
2919 struct policy_handle alias_pol;
2920 union samr_AliasInfo *info = NULL;
2923 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2926 groups->entries[i].idx,
2929 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2930 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2935 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2936 status = dcerpc_samr_Close(b, mem_ctx,
2939 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2940 description = info->description.string;
2946 if (description != NULL) {
2947 printf("%-21.21s %-50.50s\n",
2948 groups->entries[i].name.string,
2951 printf("%s\n", groups->entries[i].name.string);
2954 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2955 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2956 /* Get builtin policy handle */
2958 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2960 MAXIMUM_ALLOWED_ACCESS,
2961 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2964 if (!NT_STATUS_IS_OK(status)) {
2967 if (!NT_STATUS_IS_OK(result)) {
2972 /* query builtin aliases */
2975 if (!builtin) break;
2977 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2984 if (!NT_STATUS_IS_OK(status)) {
2987 if (!NT_STATUS_IS_OK(result) &&
2988 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2993 for (i = 0; i < num_entries; i++) {
2995 const char *description = NULL;
2997 if (c->opt_long_list_entries) {
2999 struct policy_handle alias_pol;
3000 union samr_AliasInfo *info = NULL;
3003 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3006 groups->entries[i].idx,
3009 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
3010 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
3015 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
3016 status = dcerpc_samr_Close(b, mem_ctx,
3019 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
3020 description = info->description.string;
3026 if (description != NULL) {
3027 printf("%-21.21s %-50.50s\n",
3028 groups->entries[i].name.string,
3031 printf("%s\n", groups->entries[i].name.string);
3034 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3042 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
3044 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3045 rpc_group_list_internals,
3049 static NTSTATUS rpc_list_group_members(struct net_context *c,
3050 struct rpc_pipe_client *pipe_hnd,
3051 TALLOC_CTX *mem_ctx,
3052 const char *domain_name,
3053 const struct dom_sid *domain_sid,
3054 struct policy_handle *domain_pol,
3057 NTSTATUS result, status;
3058 struct policy_handle group_pol;
3059 uint32_t num_members, *group_rids;
3061 struct samr_RidAttrArray *rids = NULL;
3062 struct lsa_Strings names;
3063 struct samr_Ids types;
3064 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3066 status = dcerpc_samr_OpenGroup(b, mem_ctx,
3068 MAXIMUM_ALLOWED_ACCESS,
3072 if (!NT_STATUS_IS_OK(status)) {
3075 if (!NT_STATUS_IS_OK(result)) {
3079 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
3083 if (!NT_STATUS_IS_OK(status)) {
3086 if (!NT_STATUS_IS_OK(result)) {
3090 num_members = rids->count;
3091 group_rids = rids->rids;
3093 while (num_members > 0) {
3094 int this_time = 512;
3096 if (num_members < this_time)
3097 this_time = num_members;
3099 status = dcerpc_samr_LookupRids(b, mem_ctx,
3106 if (!NT_STATUS_IS_OK(status)) {
3109 if (!NT_STATUS_IS_OK(result)) {
3112 if (names.count != this_time) {
3113 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3115 if (types.count != this_time) {
3116 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3118 /* We only have users as members, but make the output
3119 the same as the output of alias members */
3121 for (i = 0; i < this_time; i++) {
3123 if (c->opt_long_list_entries) {
3125 struct dom_sid_buf sid_str;
3127 sid_compose(&sid, domain_sid, group_rids[i]);
3129 printf("%s %s\\%s %d\n",
3130 dom_sid_str_buf(&sid, &sid_str),
3132 names.names[i].string,
3135 printf("%s\\%s\n", domain_name,
3136 names.names[i].string);
3140 num_members -= this_time;
3144 return NT_STATUS_OK;
3147 static NTSTATUS rpc_list_alias_members(struct net_context *c,
3148 struct rpc_pipe_client *pipe_hnd,
3149 struct cli_state *cli,
3150 TALLOC_CTX *mem_ctx,
3151 struct policy_handle *domain_pol,
3154 NTSTATUS result, status;
3155 struct rpc_pipe_client *lsa_pipe;
3156 struct policy_handle alias_pol, lsa_pol;
3157 uint32_t num_members;
3158 struct dom_sid *alias_sids;
3161 enum lsa_SidType *types;
3163 struct lsa_SidArray sid_array;
3164 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3166 status = dcerpc_samr_OpenAlias(b, mem_ctx,
3168 MAXIMUM_ALLOWED_ACCESS,
3172 if (!NT_STATUS_IS_OK(status)) {
3175 if (!NT_STATUS_IS_OK(result)) {
3179 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
3183 if (!NT_STATUS_IS_OK(status)) {
3184 d_fprintf(stderr, _("Couldn't list alias members\n"));
3187 if (!NT_STATUS_IS_OK(result)) {
3188 d_fprintf(stderr, _("Couldn't list alias members\n"));
3192 num_members = sid_array.num_sids;
3194 if (num_members == 0) {
3195 return NT_STATUS_OK;
3198 result = cli_rpc_pipe_open_noauth(cli,
3201 if (!NT_STATUS_IS_OK(result)) {
3202 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
3203 nt_errstr(result) );
3207 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
3208 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
3210 if (!NT_STATUS_IS_OK(result)) {
3211 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
3212 TALLOC_FREE(lsa_pipe);
3216 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
3218 d_fprintf(stderr, _("Out of memory\n"));
3219 TALLOC_FREE(lsa_pipe);
3220 return NT_STATUS_NO_MEMORY;
3223 for (i=0; i<num_members; i++) {
3224 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
3227 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
3228 num_members, alias_sids,
3229 &domains, &names, &types);
3231 if (!NT_STATUS_IS_OK(result) &&
3232 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3233 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3234 TALLOC_FREE(lsa_pipe);
3238 for (i = 0; i < num_members; i++) {
3239 struct dom_sid_buf sid_str;
3240 dom_sid_str_buf(&alias_sids[i], &sid_str);
3242 if (c->opt_long_list_entries) {
3243 printf("%s %s\\%s %d\n", sid_str.buf,
3244 domains[i] ? domains[i] : _("*unknown*"),
3245 names[i] ? names[i] : _("*unknown*"), types[i]);
3248 printf("%s\\%s\n", domains[i], names[i]);
3250 printf("%s\n", sid_str.buf);
3254 TALLOC_FREE(lsa_pipe);
3255 return NT_STATUS_OK;
3258 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3259 const struct dom_sid *domain_sid,
3260 const char *domain_name,
3261 struct cli_state *cli,
3262 struct rpc_pipe_client *pipe_hnd,
3263 TALLOC_CTX *mem_ctx,
3267 NTSTATUS result, status;
3268 struct policy_handle connect_pol, domain_pol;
3269 struct samr_Ids rids, rid_types;
3270 struct lsa_String lsa_acct_name;
3271 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3273 /* Get sam policy handle */
3275 status = dcerpc_samr_Connect2(b, mem_ctx,
3277 MAXIMUM_ALLOWED_ACCESS,
3280 if (!NT_STATUS_IS_OK(status)) {
3283 if (!NT_STATUS_IS_OK(result)) {
3287 /* Get domain policy handle */
3289 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3291 MAXIMUM_ALLOWED_ACCESS,
3292 discard_const_p(struct dom_sid2, domain_sid),
3295 if (!NT_STATUS_IS_OK(status)) {
3298 if (!NT_STATUS_IS_OK(result)) {
3302 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3304 status = dcerpc_samr_LookupNames(b, mem_ctx,
3311 if (!NT_STATUS_IS_OK(status)) {
3315 if (!NT_STATUS_IS_OK(result)) {
3317 /* Ok, did not find it in the global sam, try with builtin */
3319 struct dom_sid sid_Builtin;
3321 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3323 sid_copy(&sid_Builtin, &global_sid_Builtin);
3325 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3327 MAXIMUM_ALLOWED_ACCESS,
3331 if (!NT_STATUS_IS_OK(status)) {
3334 if (!NT_STATUS_IS_OK(result)) {
3335 d_fprintf(stderr, _("Couldn't find group %s\n"),
3340 status = dcerpc_samr_LookupNames(b, mem_ctx,
3347 if (!NT_STATUS_IS_OK(status)) {
3350 if (!NT_STATUS_IS_OK(result)) {
3351 d_fprintf(stderr, _("Couldn't find group %s\n"),
3357 if (rids.count != 1) {
3358 d_fprintf(stderr, _("Couldn't find group %s\n"),
3360 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3362 if (rid_types.count != 1) {
3363 d_fprintf(stderr, _("Couldn't find group %s\n"),
3365 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3369 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3370 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3371 domain_sid, &domain_pol,
3375 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3376 return rpc_list_alias_members(c, pipe_hnd, cli, mem_ctx, &domain_pol,
3380 return NT_STATUS_NO_SUCH_GROUP;
3383 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3385 if (argc != 1 || c->display_usage) {
3386 return rpc_group_usage(c, argc, argv);
3389 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3390 rpc_group_members_internals,
3394 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3396 NET_API_STATUS status;
3397 struct GROUP_INFO_0 g0;
3401 d_printf(_("Usage:\n"));
3402 d_printf("net rpc group rename group newname\n");
3406 g0.grpi0_name = argv[1];
3408 status = NetGroupSetInfo(c->opt_host,
3415 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3416 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3424 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3426 if (argc != 2 || c->display_usage) {
3427 return rpc_group_usage(c, argc, argv);
3430 return rpc_group_rename_internals(c, argc, argv);
3434 * 'net rpc group' entrypoint.
3435 * @param argc Standard main() style argc.
3436 * @param argv Standard main() style argv. Initial components are already
3440 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3442 NET_API_STATUS status;
3444 struct functable func[] = {
3449 N_("Create specified group"),
3450 N_("net rpc group add\n"
3451 " Create specified group")
3457 N_("Delete specified group"),
3458 N_("net rpc group delete\n"
3459 " Delete specified group")
3465 N_("Add member to group"),
3466 N_("net rpc group addmem\n"
3467 " Add member to group")
3473 N_("Remove member from group"),
3474 N_("net rpc group delmem\n"
3475 " Remove member from group")
3482 N_("net rpc group list\n"
3489 N_("List group members"),
3490 N_("net rpc group members\n"
3491 " List group members")
3498 N_("net rpc group rename\n"
3501 {NULL, NULL, 0, NULL, NULL}
3504 status = libnetapi_net_init(&c->netapi_ctx);
3508 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3509 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3510 if (c->opt_kerberos) {
3511 libnetapi_set_use_kerberos(c->netapi_ctx);
3515 if (c->display_usage) {
3516 d_printf(_("Usage:\n"));
3517 d_printf(_("net rpc group\n"
3518 " Alias for net rpc group list global "
3519 "local builtin\n"));
3520 net_display_usage_from_functable(func);
3524 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3525 rpc_group_list_internals,
3529 return net_run_function(c, argc, argv, "net rpc group", func);
3532 /****************************************************************************/
3534 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3536 return net_share_usage(c, argc, argv);
3540 * Add a share on a remote RPC server.
3542 * @param argc Standard main() style argc.
3543 * @param argv Standard main() style argv. Initial components are already
3546 * @return A shell status integer (0 for success).
3549 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3551 NET_API_STATUS status;
3554 uint32_t type = STYPE_DISKTREE; /* only allow disk shares to be added */
3555 uint32_t num_users=0, perms=0;
3556 char *password=NULL; /* don't allow a share password */
3557 struct SHARE_INFO_2 i2;
3558 uint32_t parm_error = 0;
3560 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3561 return rpc_share_usage(c, argc, argv);
3564 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3568 path = strchr(sharename, '=');
3575 i2.shi2_netname = sharename;
3576 i2.shi2_type = type;
3577 i2.shi2_remark = c->opt_comment;
3578 i2.shi2_permissions = perms;
3579 i2.shi2_max_uses = c->opt_maxusers;
3580 i2.shi2_current_uses = num_users;
3581 i2.shi2_path = path;
3582 i2.shi2_passwd = password;
3584 status = NetShareAdd(c->opt_host,
3589 printf(_("NetShareAdd failed with: %s\n"),
3590 libnetapi_get_error_string(c->netapi_ctx, status));
3597 * Delete a share on a remote RPC server.
3599 * @param domain_sid The domain sid acquired from the remote server.
3600 * @param argc Standard main() style argc.
3601 * @param argv Standard main() style argv. Initial components are already
3604 * @return A shell status integer (0 for success).
3606 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3608 if (argc < 1 || c->display_usage) {
3609 return rpc_share_usage(c, argc, argv);
3612 return NetShareDel(c->opt_host, argv[0], 0);
3616 * Formatted print of share info
3618 * @param r pointer to SHARE_INFO_1 to format
3621 static void display_share_info_1(struct net_context *c,
3622 struct SHARE_INFO_1 *r)
3624 if (c->opt_long_list_entries) {
3625 d_printf("%-12s %-8.8s %-50s\n",
3627 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3630 d_printf("%s\n", r->shi1_netname);
3634 static WERROR get_share_info(struct net_context *c,
3635 struct rpc_pipe_client *pipe_hnd,
3636 TALLOC_CTX *mem_ctx,
3640 struct srvsvc_NetShareInfoCtr *info_ctr)
3644 union srvsvc_NetShareInfo info;
3645 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3647 /* no specific share requested, enumerate all */
3650 uint32_t preferred_len = 0xffffffff;
3651 uint32_t total_entries = 0;
3652 uint32_t resume_handle = 0;
3654 info_ctr->level = level;
3656 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3663 if (!NT_STATUS_IS_OK(status)) {
3664 return ntstatus_to_werror(status);
3669 /* request just one share */
3670 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3677 if (!NT_STATUS_IS_OK(status)) {
3678 result = ntstatus_to_werror(status);
3682 if (!W_ERROR_IS_OK(result)) {
3687 ZERO_STRUCTP(info_ctr);
3689 info_ctr->level = level;
3694 struct srvsvc_NetShareCtr1 *ctr1;
3696 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3697 W_ERROR_HAVE_NO_MEMORY(ctr1);
3700 ctr1->array = info.info1;
3702 info_ctr->ctr.ctr1 = ctr1;
3708 struct srvsvc_NetShareCtr2 *ctr2;
3710 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3711 W_ERROR_HAVE_NO_MEMORY(ctr2);
3714 ctr2->array = info.info2;
3716 info_ctr->ctr.ctr2 = ctr2;
3722 struct srvsvc_NetShareCtr502 *ctr502;
3724 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3725 W_ERROR_HAVE_NO_MEMORY(ctr502);
3728 ctr502->array = info.info502;
3730 info_ctr->ctr.ctr502 = ctr502;
3740 * 'net rpc share list' entrypoint.
3741 * @param argc Standard main() style argc.
3742 * @param argv Standard main() style argv. Initial components are already
3745 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3747 NET_API_STATUS status;
3748 struct SHARE_INFO_1 *i1 = NULL;
3749 uint32_t entries_read = 0;
3750 uint32_t total_entries = 0;
3751 uint32_t resume_handle = 0;
3752 uint32_t i, level = 1;
3754 if (c->display_usage) {
3756 "net rpc share list\n"
3759 _("List shares on remote server"));
3763 status = NetShareEnum(c->opt_host,
3765 (uint8_t **)(void *)&i1,
3774 /* Display results */
3776 if (c->opt_long_list_entries) {
3778 "\nEnumerating shared resources (exports) on remote server:\n\n"
3779 "\nShare name Type Description\n"
3780 "---------- ---- -----------\n"));
3782 for (i = 0; i < entries_read; i++)
3783 display_share_info_1(c, &i1[i]);
3788 static bool check_share_availability(struct cli_state *cli, const char *netname)
3792 status = cli_tree_connect(cli, netname, "A:", NULL);
3793 if (!NT_STATUS_IS_OK(status)) {
3794 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3798 status = cli_tdis(cli);
3799 if (!NT_STATUS_IS_OK(status)) {
3800 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3807 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3808 const char *netname, uint32_t type)
3810 /* only support disk shares */
3811 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3812 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3817 /* skip builtin shares */
3818 /* FIXME: should print$ be added too ? */
3819 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3820 strequal(netname,"global"))
3823 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3824 printf(_("excluding [%s]\n"), netname);
3828 return check_share_availability(cli, netname);
3832 * Migrate shares from a remote RPC server to the local RPC server.
3834 * All parameters are provided by the run_rpc_command function, except for
3835 * argc, argv which are passed through.
3837 * @param domain_sid The domain sid acquired from the remote server.
3838 * @param cli A cli_state connected to the server.
3839 * @param mem_ctx Talloc context, destroyed on completion of the function.
3840 * @param argc Standard main() style argc.
3841 * @param argv Standard main() style argv. Initial components are already
3844 * @return Normal NTSTATUS return.
3847 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3848 const struct dom_sid *domain_sid,
3849 const char *domain_name,
3850 struct cli_state *cli,
3851 struct rpc_pipe_client *pipe_hnd,
3852 TALLOC_CTX *mem_ctx,
3857 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3858 struct srvsvc_NetShareInfoCtr ctr_src;
3860 struct rpc_pipe_client *srvsvc_pipe = NULL;
3861 struct cli_state *cli_dst = NULL;
3862 uint32_t level = 502; /* includes secdesc */
3863 uint32_t parm_error = 0;
3864 struct dcerpc_binding_handle *b;
3866 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3868 if (!W_ERROR_IS_OK(result))
3871 /* connect destination PI_SRVSVC */
3872 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3874 if (!NT_STATUS_IS_OK(nt_status))
3877 b = srvsvc_pipe->binding_handle;
3879 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3881 union srvsvc_NetShareInfo info;
3882 struct srvsvc_NetShareInfo502 info502 =
3883 ctr_src.ctr.ctr502->array[i];
3885 /* reset error-code */
3886 nt_status = NT_STATUS_UNSUCCESSFUL;
3888 if (!check_share_sanity(c, cli, info502.name, info502.type))
3891 /* finally add the share on the dst server */
3893 printf(_("migrating: [%s], path: %s, comment: %s, without "
3895 info502.name, info502.path, info502.comment);
3897 info.info502 = &info502;
3899 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3900 srvsvc_pipe->desthost,
3905 if (!NT_STATUS_IS_OK(nt_status)) {
3906 printf(_("cannot add share: %s\n"),
3907 nt_errstr(nt_status));
3910 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3911 printf(_(" [%s] does already exist\n"),
3916 if (!W_ERROR_IS_OK(result)) {
3917 nt_status = werror_to_ntstatus(result);
3918 printf(_("cannot add share: %s\n"),
3919 win_errstr(result));
3925 nt_status = NT_STATUS_OK;
3929 cli_shutdown(cli_dst);
3937 * Migrate shares from a RPC server to another.
3939 * @param argc Standard main() style argc.
3940 * @param argv Standard main() style argv. Initial components are already
3943 * @return A shell status integer (0 for success).
3945 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3948 if (c->display_usage) {
3950 "net rpc share migrate shares\n"
3953 _("Migrate shares to local server"));
3958 printf(_("no server to migrate\n"));
3962 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3963 rpc_share_migrate_shares_internals,
3970 * @param f file_info
3971 * @param mask current search mask
3972 * @param state arg-pointer
3975 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3976 const char *mask, void *state)
3978 static NTSTATUS nt_status;
3979 static struct copy_clistate *local_state;
3980 static fstring filename, new_mask;
3983 struct net_context *c;
3985 local_state = (struct copy_clistate *)state;
3986 nt_status = NT_STATUS_UNSUCCESSFUL;
3990 if (strequal(f->name, ".") || strequal(f->name, ".."))
3991 return NT_STATUS_OK;
3993 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3996 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
3998 DEBUG(3,("got dir: %s\n", f->name));
4000 fstrcpy(dir, local_state->cwd);
4002 fstrcat(dir, f->name);
4004 switch (net_mode_share)
4006 case NET_MODE_SHARE_MIGRATE:
4007 /* create that directory */
4008 nt_status = net_copy_file(c, local_state->mem_ctx,
4009 local_state->cli_share_src,
4010 local_state->cli_share_dst,
4012 c->opt_acls? true : false,
4013 c->opt_attrs? true : false,
4014 c->opt_timestamps? true:false,
4018 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4019 return NT_STATUS_INTERNAL_ERROR;
4022 if (!NT_STATUS_IS_OK(nt_status)) {
4023 printf(_("could not handle dir %s: %s\n"),
4024 dir, nt_errstr(nt_status));
4028 /* search below that directory */
4029 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
4030 return NT_STATUS_NO_MEMORY;
4032 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
4033 return NT_STATUS_NO_MEMORY;
4036 old_dir = local_state->cwd;
4037 local_state->cwd = dir;
4038 nt_status = sync_files(local_state, new_mask);
4039 if (!NT_STATUS_IS_OK(nt_status)) {
4040 printf(_("could not handle files\n"));
4042 local_state->cwd = old_dir;
4049 fstrcpy(filename, local_state->cwd);
4050 fstrcat(filename, "\\");
4051 fstrcat(filename, f->name);
4053 DEBUG(3,("got file: %s\n", filename));
4055 switch (net_mode_share)
4057 case NET_MODE_SHARE_MIGRATE:
4058 nt_status = net_copy_file(c, local_state->mem_ctx,
4059 local_state->cli_share_src,
4060 local_state->cli_share_dst,
4062 c->opt_acls? true : false,
4063 c->opt_attrs? true : false,
4064 c->opt_timestamps? true: false,
4068 d_fprintf(stderr, _("Unsupported file mode %d\n"),
4070 return NT_STATUS_INTERNAL_ERROR;
4073 if (!NT_STATUS_IS_OK(nt_status))
4074 printf(_("could not handle file %s: %s\n"),
4075 filename, nt_errstr(nt_status));
4080 * sync files, can be called recursively to list files
4081 * and then call copy_fn for each file
4083 * @param cp_clistate pointer to the copy_clistate we work with
4084 * @param mask the current search mask
4086 * @return Boolean result
4088 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
4090 struct cli_state *targetcli;
4091 char *targetpath = NULL;
4094 DEBUG(3,("calling cli_list with mask: %s\n", mask));
4096 status = cli_resolve_path(talloc_tos(), "", NULL,
4097 cp_clistate->cli_share_src,
4098 mask, &targetcli, &targetpath);
4099 if (!NT_STATUS_IS_OK(status)) {
4100 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
4102 mask, nt_errstr(status));
4106 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
4107 copy_fn, cp_clistate);
4108 if (!NT_STATUS_IS_OK(status)) {
4109 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
4110 mask, nt_errstr(status));
4118 * Set the top level directory permissions before we do any further copies.
4119 * Should set up ACL inheritance.
4122 bool copy_top_level_perms(struct net_context *c,
4123 struct copy_clistate *cp_clistate,
4124 const char *sharename)
4126 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4128 switch (net_mode_share) {
4129 case NET_MODE_SHARE_MIGRATE:
4130 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
4131 nt_status = net_copy_fileattr(c,
4132 cp_clistate->mem_ctx,
4133 cp_clistate->cli_share_src,
4134 cp_clistate->cli_share_dst,
4136 c->opt_acls? true : false,
4137 c->opt_attrs? true : false,
4138 c->opt_timestamps? true: false,
4142 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
4146 if (!NT_STATUS_IS_OK(nt_status)) {
4147 printf(_("Could handle directory attributes for top level "
4148 "directory of share %s. Error %s\n"),
4149 sharename, nt_errstr(nt_status));
4157 * Sync all files inside a remote share to another share (over smb).
4159 * All parameters are provided by the run_rpc_command function, except for
4160 * argc, argv which are passed through.
4162 * @param domain_sid The domain sid acquired from the remote server.
4163 * @param cli A cli_state connected to the server.
4164 * @param mem_ctx Talloc context, destroyed on completion of the function.
4165 * @param argc Standard main() style argc.
4166 * @param argv Standard main() style argv. Initial components are already
4169 * @return Normal NTSTATUS return.
4172 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
4173 const struct dom_sid *domain_sid,
4174 const char *domain_name,
4175 struct cli_state *cli,
4176 struct rpc_pipe_client *pipe_hnd,
4177 TALLOC_CTX *mem_ctx,
4182 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4183 struct srvsvc_NetShareInfoCtr ctr_src;
4185 uint32_t level = 502;
4186 struct copy_clistate cp_clistate;
4187 bool got_src_share = false;
4188 bool got_dst_share = false;
4189 const char *mask = "\\*";
4192 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
4194 nt_status = NT_STATUS_NO_MEMORY;
4198 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4201 if (!W_ERROR_IS_OK(result))
4204 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4206 struct srvsvc_NetShareInfo502 info502 =
4207 ctr_src.ctr.ctr502->array[i];
4209 if (!check_share_sanity(c, cli, info502.name, info502.type))
4212 /* one might not want to mirror whole discs :) */
4213 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
4214 d_printf(_("skipping [%s]: builtin/hidden share\n"),
4219 switch (net_mode_share)
4221 case NET_MODE_SHARE_MIGRATE:
4225 d_fprintf(stderr, _("Unsupported mode %d\n"),
4229 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4232 c->opt_acls ? _("including") : _("without"),
4233 c->opt_attrs ? _("including") : _("without"),
4234 c->opt_timestamps ? _("(preserving timestamps)") : "");
4236 cp_clistate.mem_ctx = mem_ctx;
4237 cp_clistate.cli_share_src = NULL;
4238 cp_clistate.cli_share_dst = NULL;
4239 cp_clistate.cwd = NULL;
4240 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4243 /* open share source */
4244 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4245 smbXcli_conn_remote_sockaddr(cli->conn),
4246 smbXcli_conn_remote_name(cli->conn),
4247 info502.name, "A:");
4248 if (!NT_STATUS_IS_OK(nt_status))
4251 got_src_share = true;
4253 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4254 /* open share destination */
4255 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4256 NULL, dst, info502.name, "A:");
4257 if (!NT_STATUS_IS_OK(nt_status))
4260 got_dst_share = true;
4263 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4264 d_fprintf(stderr, _("Could not handle the top level "
4265 "directory permissions for the "
4266 "share: %s\n"), info502.name);
4267 nt_status = NT_STATUS_UNSUCCESSFUL;
4271 nt_status = sync_files(&cp_clistate, mask);
4272 if (!NT_STATUS_IS_OK(nt_status)) {
4273 d_fprintf(stderr, _("could not handle files for share: "
4274 "%s\n"), info502.name);
4279 nt_status = NT_STATUS_OK;
4284 cli_shutdown(cp_clistate.cli_share_src);
4287 cli_shutdown(cp_clistate.cli_share_dst);
4294 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4296 if (c->display_usage) {
4298 "net share migrate files\n"
4301 _("Migrate files to local server"));
4306 d_printf(_("no server to migrate\n"));
4310 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4311 rpc_share_migrate_files_internals,
4316 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4318 * All parameters are provided by the run_rpc_command function, except for
4319 * argc, argv which are passed through.
4321 * @param domain_sid The domain sid acquired from the remote server.
4322 * @param cli A cli_state connected to the server.
4323 * @param mem_ctx Talloc context, destroyed on completion of the function.
4324 * @param argc Standard main() style argc.
4325 * @param argv Standard main() style argv. Initial components are already
4328 * @return Normal NTSTATUS return.
4331 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4332 const struct dom_sid *domain_sid,
4333 const char *domain_name,
4334 struct cli_state *cli,
4335 struct rpc_pipe_client *pipe_hnd,
4336 TALLOC_CTX *mem_ctx,
4341 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4342 struct srvsvc_NetShareInfoCtr ctr_src;
4343 union srvsvc_NetShareInfo info;
4345 struct rpc_pipe_client *srvsvc_pipe = NULL;
4346 struct cli_state *cli_dst = NULL;
4347 uint32_t level = 502; /* includes secdesc */
4348 uint32_t parm_error = 0;
4349 struct dcerpc_binding_handle *b;
4351 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4354 if (!W_ERROR_IS_OK(result))
4357 /* connect destination PI_SRVSVC */
4358 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4360 if (!NT_STATUS_IS_OK(nt_status))
4363 b = srvsvc_pipe->binding_handle;
4365 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4367 struct srvsvc_NetShareInfo502 info502 =
4368 ctr_src.ctr.ctr502->array[i];
4370 /* reset error-code */
4371 nt_status = NT_STATUS_UNSUCCESSFUL;
4373 if (!check_share_sanity(c, cli, info502.name, info502.type))
4376 printf(_("migrating: [%s], path: %s, comment: %s, including "
4378 info502.name, info502.path, info502.comment);
4381 display_sec_desc(info502.sd_buf.sd);
4383 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4384 info.info502 = &info502;
4386 /* finally modify the share on the dst server */
4387 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4388 srvsvc_pipe->desthost,
4394 if (!NT_STATUS_IS_OK(nt_status)) {
4395 printf(_("cannot set share-acl: %s\n"),
4396 nt_errstr(nt_status));
4399 if (!W_ERROR_IS_OK(result)) {
4400 nt_status = werror_to_ntstatus(result);
4401 printf(_("cannot set share-acl: %s\n"),
4402 win_errstr(result));
4408 nt_status = NT_STATUS_OK;
4412 cli_shutdown(cli_dst);
4420 * Migrate share-acls from a RPC server to another.
4422 * @param argc Standard main() style argc.
4423 * @param argv Standard main() style argv. Initial components are already
4426 * @return A shell status integer (0 for success).
4428 static int rpc_share_migrate_security(struct net_context *c, int argc,
4431 if (c->display_usage) {
4433 "net rpc share migrate security\n"
4436 _("Migrate share-acls to local server"));
4441 d_printf(_("no server to migrate\n"));
4445 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4446 rpc_share_migrate_security_internals,
4451 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4452 * from one server to another.
4454 * @param argc Standard main() style argc.
4455 * @param argv Standard main() style argv. Initial components are already
4458 * @return A shell status integer (0 for success).
4461 static int rpc_share_migrate_all(struct net_context *c, int argc,
4466 if (c->display_usage) {
4468 "net rpc share migrate all\n"
4471 _("Migrates shares including all share settings"));
4476 d_printf(_("no server to migrate\n"));
4480 /* order is important. we don't want to be locked out by the share-acl
4481 * before copying files - gd */
4483 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4484 rpc_share_migrate_shares_internals, argc, argv);
4488 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4489 rpc_share_migrate_files_internals, argc, argv);
4493 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4494 rpc_share_migrate_security_internals, argc,
4500 * 'net rpc share migrate' entrypoint.
4501 * @param argc Standard main() style argc.
4502 * @param argv Standard main() style argv. Initial components are already
4505 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4508 struct functable func[] = {
4511 rpc_share_migrate_all,
4513 N_("Migrate shares from remote to local server"),
4514 N_("net rpc share migrate all\n"
4515 " Migrate shares from remote to local server")
4519 rpc_share_migrate_files,
4521 N_("Migrate files from remote to local server"),
4522 N_("net rpc share migrate files\n"
4523 " Migrate files from remote to local server")
4527 rpc_share_migrate_security,
4529 N_("Migrate share-ACLs from remote to local server"),
4530 N_("net rpc share migrate security\n"
4531 " Migrate share-ACLs from remote to local server")
4535 rpc_share_migrate_shares,
4537 N_("Migrate shares from remote to local server"),
4538 N_("net rpc share migrate shares\n"
4539 " Migrate shares from remote to local server")
4541 {NULL, NULL, 0, NULL, NULL}
4544 net_mode_share = NET_MODE_SHARE_MIGRATE;
4546 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4551 uint32_t num_members;
4552 struct dom_sid *members;
4555 static int num_server_aliases;
4556 static struct full_alias *server_aliases;
4559 * Add an alias to the static list.
4561 static void push_alias(struct full_alias *alias)
4565 if (server_aliases == NULL) {
4566 server_aliases = talloc_array(NULL, struct full_alias, 100);
4567 if (server_aliases == NULL) {
4568 smb_panic("talloc_array failed");
4572 array_size = talloc_array_length(server_aliases);
4573 if (array_size == num_server_aliases) {
4574 server_aliases = talloc_realloc(NULL, server_aliases,
4575 struct full_alias, array_size + 100);
4576 if (server_aliases == NULL) {
4577 smb_panic("talloc_realloc failed");
4581 server_aliases[num_server_aliases] = *alias;
4582 num_server_aliases += 1;
4586 * For a specific domain on the server, fetch all the aliases
4587 * and their members. Add all of them to the server_aliases.
4590 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4591 TALLOC_CTX *mem_ctx,
4592 struct policy_handle *connect_pol,
4593 const struct dom_sid *domain_sid)
4595 uint32_t start_idx, max_entries, num_entries, i;
4596 struct samr_SamArray *groups = NULL;
4597 NTSTATUS result, status;
4598 struct policy_handle domain_pol;
4599 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4601 /* Get domain policy handle */
4603 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4605 MAXIMUM_ALLOWED_ACCESS,
4606 discard_const_p(struct dom_sid2, domain_sid),
4609 if (!NT_STATUS_IS_OK(status)) {
4612 if (!NT_STATUS_IS_OK(result)) {
4620 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4627 if (!NT_STATUS_IS_OK(status)) {
4630 for (i = 0; i < num_entries; i++) {
4632 struct policy_handle alias_pol;
4633 struct full_alias alias;
4634 struct lsa_SidArray sid_array;
4638 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4640 MAXIMUM_ALLOWED_ACCESS,
4641 groups->entries[i].idx,
4644 if (!NT_STATUS_IS_OK(status)) {
4647 if (!NT_STATUS_IS_OK(_result)) {
4652 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4656 if (!NT_STATUS_IS_OK(status)) {
4659 if (!NT_STATUS_IS_OK(_result)) {
4664 alias.num_members = sid_array.num_sids;
4666 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4667 if (!NT_STATUS_IS_OK(status)) {
4670 if (!NT_STATUS_IS_OK(_result)) {
4675 alias.members = NULL;
4677 if (alias.num_members > 0) {
4678 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4679 if (alias.members == NULL) {
4680 status = NT_STATUS_NO_MEMORY;
4684 for (j = 0; j < alias.num_members; j++)
4685 sid_copy(&alias.members[j],
4686 sid_array.sids[j].sid);
4689 sid_compose(&alias.sid, domain_sid,
4690 groups->entries[i].idx);
4694 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4696 status = NT_STATUS_OK;
4699 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4705 * Dump server_aliases as names for debugging purposes.
4708 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4709 const struct dom_sid *domain_sid,
4710 const char *domain_name,
4711 struct cli_state *cli,
4712 struct rpc_pipe_client *pipe_hnd,
4713 TALLOC_CTX *mem_ctx,
4719 struct policy_handle lsa_pol;
4720 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4722 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4723 SEC_FLAG_MAXIMUM_ALLOWED,
4725 if (!NT_STATUS_IS_OK(result))
4728 for (i=0; i<num_server_aliases; i++) {
4731 enum lsa_SidType *types;
4734 struct full_alias *alias = &server_aliases[i];
4736 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4738 &domains, &names, &types);
4739 if (!NT_STATUS_IS_OK(result))
4742 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4744 if (alias->num_members == 0) {
4749 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4752 &domains, &names, &types);
4754 if (!NT_STATUS_IS_OK(result) &&
4755 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4758 for (j=0; j<alias->num_members; j++)
4759 DEBUG(1, ("%s\\%s (%d); ",
4760 domains[j] ? domains[j] : "*unknown*",
4761 names[j] ? names[j] : "*unknown*",types[j]));
4765 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4767 return NT_STATUS_OK;
4771 * Fetch a list of all server aliases and their members into
4775 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4776 const struct dom_sid *domain_sid,
4777 const char *domain_name,
4778 struct cli_state *cli,
4779 struct rpc_pipe_client *pipe_hnd,
4780 TALLOC_CTX *mem_ctx,
4784 NTSTATUS result, status;
4785 struct policy_handle connect_pol;
4786 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4788 status = dcerpc_samr_Connect2(b, mem_ctx,
4790 MAXIMUM_ALLOWED_ACCESS,
4793 if (!NT_STATUS_IS_OK(status)) {
4796 if (!NT_STATUS_IS_OK(result)) {
4801 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4802 &global_sid_Builtin);
4803 if (!NT_STATUS_IS_OK(status)) {
4807 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4810 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4815 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4817 token->num_sids = 4;
4819 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4820 d_fprintf(stderr, "malloc %s\n",_("failed"));
4821 token->num_sids = 0;
4825 token->sids[0] = *user_sid;
4826 sid_copy(&token->sids[1], &global_sid_World);
4827 sid_copy(&token->sids[2], &global_sid_Network);
4828 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4831 static void free_user_token(struct security_token *token)
4833 SAFE_FREE(token->sids);
4836 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4838 if (security_token_has_sid(token, sid))
4841 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4846 sid_copy(&token->sids[token->num_sids], sid);
4848 token->num_sids += 1;
4853 struct security_token token;
4856 static void dump_user_token(struct user_token *token)
4860 d_printf("%s\n", token->name);
4862 for (i=0; i<token->token.num_sids; i++) {
4863 struct dom_sid_buf buf;
4865 dom_sid_str_buf(&token->token.sids[i], &buf));
4869 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4873 for (i=0; i<alias->num_members; i++) {
4874 if (dom_sid_equal(sid, &alias->members[i])) {
4882 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4886 for (i=0; i<num_server_aliases; i++) {
4887 if (is_alias_member(&sid, &server_aliases[i]))
4888 add_sid_to_token(token, &server_aliases[i].sid);
4893 * We got a user token with all the SIDs we can know about without asking the
4894 * server directly. These are the user and domain group sids. All of these can
4895 * be members of aliases. So scan the list of aliases for each of the SIDs and
4896 * add them to the token.
4899 static void collect_alias_memberships(struct security_token *token)
4901 int num_global_sids = token->num_sids;
4904 for (i=0; i<num_global_sids; i++) {
4905 collect_sid_memberships(token, token->sids[i]);
4909 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4911 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4912 enum wbcSidType type;
4914 struct wbcDomainSid wsid;
4915 char sid_str[WBC_SID_STRING_BUFLEN];
4916 struct dom_sid user_sid;
4917 uint32_t num_groups;
4918 gid_t *groups = NULL;
4921 fstr_sprintf(full_name, "%s%c%s",
4922 domain, *lp_winbind_separator(), user);
4924 /* First let's find out the user sid */
4926 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4928 if (!WBC_ERROR_IS_OK(wbc_status)) {
4929 DEBUG(1, ("winbind could not find %s: %s\n",
4930 full_name, wbcErrorString(wbc_status)));
4934 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4936 if (type != WBC_SID_NAME_USER) {
4937 DEBUG(1, ("%s is not a user\n", full_name));
4941 if (!string_to_sid(&user_sid, sid_str)) {
4942 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4946 init_user_token(token, &user_sid);
4948 /* And now the groups winbind knows about */
4950 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4951 if (!WBC_ERROR_IS_OK(wbc_status)) {
4952 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4953 full_name, wbcErrorString(wbc_status)));
4957 for (i = 0; i < num_groups; i++) {
4958 gid_t gid = groups[i];
4962 wbc_status = wbcGidToSid(gid, &wsid);
4963 if (!WBC_ERROR_IS_OK(wbc_status)) {
4964 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4965 (unsigned int)gid, wbcErrorString(wbc_status)));
4966 wbcFreeMemory(groups);
4970 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4972 DEBUG(3, (" %s\n", sid_str));
4974 ok = string_to_sid(&sid, sid_str);
4976 DEBUG(1, ("Failed to convert string to SID\n"));
4977 wbcFreeMemory(groups);
4980 add_sid_to_token(token, &sid);
4982 wbcFreeMemory(groups);
4988 * Get a list of all user tokens we want to look at
4991 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4992 struct user_token **user_tokens)
4994 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4995 uint32_t i, num_users;
4997 struct user_token *result;
4998 TALLOC_CTX *frame = NULL;
5000 if (lp_winbind_use_default_domain() &&
5001 (c->opt_target_workgroup == NULL)) {
5002 d_fprintf(stderr, _("winbind use default domain = yes set, "
5003 "please specify a workgroup\n"));
5007 /* Send request to winbind daemon */
5009 wbc_status = wbcListUsers(NULL, &num_users, &users);
5010 if (!WBC_ERROR_IS_OK(wbc_status)) {
5011 DEBUG(1, (_("winbind could not list users: %s\n"),
5012 wbcErrorString(wbc_status)));
5016 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
5018 if (result == NULL) {
5019 DEBUG(1, ("Could not malloc sid array\n"));
5020 wbcFreeMemory(users);
5024 frame = talloc_stackframe();
5025 for (i=0; i < num_users; i++) {
5026 fstring domain, user;
5029 fstrcpy(result[i].name, users[i]);
5031 p = strchr(users[i], *lp_winbind_separator());
5033 DEBUG(3, ("%s\n", users[i]));
5036 fstrcpy(domain, c->opt_target_workgroup);
5037 fstrcpy(user, users[i]);
5040 fstrcpy(domain, users[i]);
5041 if (!strupper_m(domain)) {
5042 DEBUG(1, ("strupper_m %s failed\n", domain));
5043 wbcFreeMemory(users);
5049 get_user_sids(domain, user, &(result[i].token));
5052 wbcFreeMemory(users);
5054 *num_tokens = num_users;
5055 *user_tokens = result;
5060 static bool get_user_tokens_from_file(FILE *f,
5062 struct user_token **tokens)
5064 struct user_token *token = NULL;
5069 if (fgets(line, sizeof(line)-1, f) == NULL) {
5073 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
5074 line[strlen(line)-1] = '\0';
5077 if (line[0] == ' ') {
5081 if(!string_to_sid(&sid, &line[1])) {
5082 DEBUG(1,("get_user_tokens_from_file: Could "
5083 "not convert sid %s \n",&line[1]));
5087 if (token == NULL) {
5088 DEBUG(0, ("File does not begin with username"));
5092 add_sid_to_token(&token->token, &sid);
5096 /* And a new user... */
5099 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
5100 if (*tokens == NULL) {
5101 DEBUG(0, ("Could not realloc tokens\n"));
5105 token = &((*tokens)[*num_tokens-1]);
5107 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
5110 token->token.num_sids = 0;
5111 token->token.sids = NULL;
5120 * Show the list of all users that have access to a share
5123 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
5124 struct cli_state *cli,
5125 TALLOC_CTX *mem_ctx,
5126 const char *netname,
5128 struct user_token *tokens)
5131 struct security_descriptor *share_sd = NULL;
5132 struct security_descriptor *root_sd = NULL;
5134 union srvsvc_NetShareInfo info;
5137 struct smbXcli_tcon *orig_tcon = NULL;
5138 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5140 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5147 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5148 DEBUG(1, ("Could not query secdesc for share %s\n",
5153 share_sd = info.info502->sd_buf.sd;
5154 if (share_sd == NULL) {
5155 DEBUG(1, ("Got no secdesc for share %s\n",
5159 if (cli_state_has_tcon(cli)) {
5160 orig_tcon = cli_state_save_tcon(cli);
5161 if (orig_tcon == NULL) {
5166 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", NULL))) {
5167 cli_state_restore_tcon(cli, orig_tcon);
5171 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
5172 FILE_SHARE_READ|FILE_SHARE_WRITE,
5173 FILE_OPEN, 0x0, 0x0, &fnum, NULL))) {
5174 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
5177 for (i=0; i<num_tokens; i++) {
5178 uint32_t acc_granted;
5180 if (share_sd != NULL) {
5181 status = se_access_check(share_sd, &tokens[i].token,
5184 if (!NT_STATUS_IS_OK(status)) {
5185 DEBUG(1, ("Could not check share_sd for "
5192 if (root_sd == NULL) {
5193 d_printf(" %s\n", tokens[i].name);
5197 status = se_access_check(root_sd, &tokens[i].token,
5199 if (!NT_STATUS_IS_OK(status)) {
5200 DEBUG(1, ("Could not check root_sd for user %s\n",
5204 d_printf(" %s\n", tokens[i].name);
5207 if (fnum != (uint16_t)-1)
5208 cli_close(cli, fnum);
5210 cli_state_restore_tcon(cli, orig_tcon);
5216 * List shares on a remote RPC server, including the security descriptors.
5218 * All parameters are provided by the run_rpc_command function, except for
5219 * argc, argv which are passed through.
5221 * @param domain_sid The domain sid acquired from the remote server.
5222 * @param cli A cli_state connected to the server.
5223 * @param mem_ctx Talloc context, destroyed on completion of the function.
5224 * @param argc Standard main() style argc.
5225 * @param argv Standard main() style argv. Initial components are already
5228 * @return Normal NTSTATUS return.
5231 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
5232 const struct dom_sid *domain_sid,
5233 const char *domain_name,
5234 struct cli_state *cli,
5235 struct rpc_pipe_client *pipe_hnd,
5236 TALLOC_CTX *mem_ctx,
5242 NTSTATUS nt_status = NT_STATUS_OK;
5243 uint32_t total_entries = 0;
5244 uint32_t resume_handle = 0;
5245 uint32_t preferred_len = 0xffffffff;
5247 struct dcerpc_binding_handle *b = NULL;
5248 struct srvsvc_NetShareInfoCtr info_ctr;
5249 struct srvsvc_NetShareCtr1 ctr1;
5252 struct user_token *tokens = NULL;
5258 if (strequal(argv[0], "-")) {
5261 f = fopen(argv[0], "r");
5268 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
5269 return NT_STATUS_UNSUCCESSFUL;
5272 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5278 DEBUG(0, ("Could not read users from file\n"));
5279 return NT_STATUS_UNSUCCESSFUL;
5282 for (i=0; i<num_tokens; i++)
5283 collect_alias_memberships(&tokens[i].token);
5285 ZERO_STRUCT(info_ctr);
5289 info_ctr.ctr.ctr1 = &ctr1;
5291 b = pipe_hnd->binding_handle;
5294 /* Show results only for shares listed on the command line. */
5296 const char *netname = *argv++;
5297 d_printf("%s\n", netname);
5298 show_userlist(pipe_hnd, cli, mem_ctx, netname,
5299 num_tokens, tokens);
5304 /* Issue the NetShareEnum RPC call and retrieve the response */
5305 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5314 /* Was it successful? */
5315 if (!NT_STATUS_IS_OK(nt_status)) {
5316 /* Nope. Go clean up. */
5320 if (!W_ERROR_IS_OK(result)) {
5321 /* Nope. Go clean up. */
5322 nt_status = werror_to_ntstatus(result);
5326 if (total_entries == 0) {
5330 /* For each returned entry... */
5331 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5332 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5334 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5338 d_printf("%s\n", netname);
5340 show_userlist(pipe_hnd, cli, mem_ctx, netname,
5341 num_tokens, tokens);
5344 for (i=0; i<num_tokens; i++) {
5345 free_user_token(&tokens[i].token);
5348 TALLOC_FREE(server_aliases);
5353 static int rpc_share_allowedusers(struct net_context *c, int argc,
5358 if (c->display_usage) {
5360 "net rpc share allowedusers\n"
5363 _("List allowed users"));
5367 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5368 rpc_aliaslist_internals,
5373 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5379 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5380 rpc_share_allowedusers_internals,
5384 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5387 struct user_token *tokens = NULL;
5391 net_usersidlist_usage(c, argc, argv);
5395 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5396 DEBUG(0, ("Could not get the user/sid list\n"));
5400 for (i=0; i<num_tokens; i++) {
5401 dump_user_token(&tokens[i]);
5402 free_user_token(&tokens[i].token);
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);
5479 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5480 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5481 if (c->opt_kerberos) {
5482 libnetapi_set_use_kerberos(c->netapi_ctx);
5486 if (c->display_usage) {
5491 " Alias for net rpc share list\n"));
5492 net_display_usage_from_functable(func);
5496 return rpc_share_list(c, argc, argv);
5499 return net_run_function(c, argc, argv, "net rpc share", func);
5502 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5503 TALLOC_CTX *mem_ctx,
5504 struct rpc_sh_ctx *ctx,
5505 struct rpc_pipe_client *pipe_hnd,
5506 int argc, const char **argv)
5509 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5512 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5513 TALLOC_CTX *mem_ctx,
5514 struct rpc_sh_ctx *ctx,
5515 struct rpc_pipe_client *pipe_hnd,
5516 int argc, const char **argv)
5518 NET_API_STATUS status;
5519 uint32_t parm_err = 0;
5520 struct SHARE_INFO_2 i2;
5522 if ((argc < 2) || (argc > 3)) {
5523 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5525 return NT_STATUS_INVALID_PARAMETER;
5528 i2.shi2_netname = argv[0];
5529 i2.shi2_type = STYPE_DISKTREE;
5530 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5531 i2.shi2_permissions = 0;
5532 i2.shi2_max_uses = 0;
5533 i2.shi2_current_uses = 0;
5534 i2.shi2_path = argv[1];
5535 i2.shi2_passwd = NULL;
5537 status = NetShareAdd(pipe_hnd->desthost,
5542 return werror_to_ntstatus(W_ERROR(status));
5545 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5546 TALLOC_CTX *mem_ctx,
5547 struct rpc_sh_ctx *ctx,
5548 struct rpc_pipe_client *pipe_hnd,
5549 int argc, const char **argv)
5552 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5553 return NT_STATUS_INVALID_PARAMETER;
5556 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5559 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5560 TALLOC_CTX *mem_ctx,
5561 struct rpc_sh_ctx *ctx,
5562 struct rpc_pipe_client *pipe_hnd,
5563 int argc, const char **argv)
5565 union srvsvc_NetShareInfo info;
5568 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5571 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5572 return NT_STATUS_INVALID_PARAMETER;
5575 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5581 if (!NT_STATUS_IS_OK(status)) {
5582 result = ntstatus_to_werror(status);
5585 if (!W_ERROR_IS_OK(result)) {
5589 d_printf(_("Name: %s\n"), info.info2->name);
5590 d_printf(_("Comment: %s\n"), info.info2->comment);
5591 d_printf(_("Path: %s\n"), info.info2->path);
5592 d_printf(_("Password: %s\n"), info.info2->password);
5595 return werror_to_ntstatus(result);
5598 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5599 struct rpc_sh_ctx *ctx)
5601 static struct rpc_sh_cmd cmds[] = {
5603 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5604 N_("List available shares") },
5606 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5607 N_("Add a share") },
5609 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5610 N_("Delete a share") },
5612 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5613 N_("Get information about a share") },
5615 { NULL, NULL, 0, NULL, NULL }
5621 /****************************************************************************/
5623 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5625 return net_file_usage(c, argc, argv);
5629 * Close a file on a remote RPC server.
5631 * @param argc Standard main() style argc.
5632 * @param argv Standard main() style argv. Initial components are already
5635 * @return A shell status integer (0 for success).
5637 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5639 if (argc < 1 || c->display_usage) {
5640 return rpc_file_usage(c, argc, argv);
5643 return NetFileClose(c->opt_host, atoi(argv[0]));
5647 * Formatted print of open file info
5649 * @param r struct FILE_INFO_3 contents
5652 static void display_file_info_3(struct FILE_INFO_3 *r)
5654 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5655 r->fi3_id, r->fi3_username, r->fi3_permissions,
5656 r->fi3_num_locks, r->fi3_pathname);
5660 * List files for a user on a remote RPC server.
5662 * @param argc Standard main() style argc.
5663 * @param argv Standard main() style argv. Initial components are already
5666 * @return A shell status integer (0 for success)..
5669 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5671 NET_API_STATUS status;
5672 uint32_t preferred_len = 0xffffffff, i;
5673 char *username=NULL;
5674 uint32_t total_entries = 0;
5675 uint32_t entries_read = 0;
5676 uint32_t resume_handle = 0;
5677 struct FILE_INFO_3 *i3 = NULL;
5679 if (c->display_usage) {
5680 return rpc_file_usage(c, argc, argv);
5683 /* if argc > 0, must be user command */
5685 username = smb_xstrdup(argv[0]);
5688 status = NetFileEnum(c->opt_host,
5692 (uint8_t **)(void *)&i3,
5702 /* Display results */
5705 "\nEnumerating open files on remote server:\n\n"
5706 "\nFileId Opened by Perms Locks Path"
5707 "\n------ --------- ----- ----- ---- \n"));
5708 for (i = 0; i < entries_read; i++) {
5709 display_file_info_3(&i3[i]);
5712 SAFE_FREE(username);
5717 * 'net rpc file' entrypoint.
5718 * @param argc Standard main() style argc.
5719 * @param argv Standard main() style argv. Initial components are already
5723 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5725 NET_API_STATUS status;
5727 struct functable func[] = {
5732 N_("Close opened file"),
5733 N_("net rpc file close\n"
5734 " Close opened file")
5740 N_("List files opened by user"),
5741 N_("net rpc file user\n"
5742 " List files opened by user")
5749 N_("Display information about opened file"),
5750 N_("net rpc file info\n"
5751 " Display information about opened file")
5754 {NULL, NULL, 0, NULL, NULL}
5757 status = libnetapi_net_init(&c->netapi_ctx);
5761 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5762 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5763 if (c->opt_kerberos) {
5764 libnetapi_set_use_kerberos(c->netapi_ctx);
5768 if (c->display_usage) {
5769 d_printf(_("Usage:\n"));
5770 d_printf(_("net rpc file\n"
5771 " List opened files\n"));
5772 net_display_usage_from_functable(func);
5776 return rpc_file_user(c, argc, argv);
5779 return net_run_function(c, argc, argv, "net rpc file", func);
5783 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5785 * All parameters are provided by the run_rpc_command function, except for
5786 * argc, argv which are passed through.
5788 * @param c A net_context structure.
5789 * @param domain_sid The domain sid acquired from the remote server.
5790 * @param cli A cli_state connected to the server.
5791 * @param mem_ctx Talloc context, destroyed on completion of the function.
5792 * @param argc Standard main() style argc.
5793 * @param argv Standard main() style argv. Initial components are already
5796 * @return Normal NTSTATUS return.
5799 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5800 const struct dom_sid *domain_sid,
5801 const char *domain_name,
5802 struct cli_state *cli,
5803 struct rpc_pipe_client *pipe_hnd,
5804 TALLOC_CTX *mem_ctx,
5808 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5810 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5812 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5813 if (!NT_STATUS_IS_OK(status)) {
5816 if (W_ERROR_IS_OK(result)) {
5817 d_printf(_("\nShutdown successfully aborted\n"));
5818 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5820 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5822 return werror_to_ntstatus(result);
5826 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5828 * All parameters are provided by the run_rpc_command function, except for
5829 * argc, argv which are passed through.
5831 * @param c A net_context structure.
5832 * @param domain_sid The domain sid acquired from the remote server.
5833 * @param cli A cli_state connected to the server.
5834 * @param mem_ctx Talloc context, destroyed on completion of the function.
5835 * @param argc Standard main() style argc.
5836 * @param argv Standard main() style argv. Initial components are already
5839 * @return Normal NTSTATUS return.
5842 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5843 const struct dom_sid *domain_sid,
5844 const char *domain_name,
5845 struct cli_state *cli,
5846 struct rpc_pipe_client *pipe_hnd,
5847 TALLOC_CTX *mem_ctx,
5851 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5853 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5855 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5857 if (!NT_STATUS_IS_OK(result)) {
5858 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5861 if (W_ERROR_IS_OK(werr)) {
5862 d_printf(_("\nShutdown successfully aborted\n"));
5863 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5865 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5867 return werror_to_ntstatus(werr);
5871 * ABORT the shutdown of a remote RPC server.
5873 * @param argc Standard main() style argc.
5874 * @param argv Standard main() style argv. Initial components are already
5877 * @return A shell status integer (0 for success).
5880 static int rpc_shutdown_abort(struct net_context *c, int argc,
5885 if (c->display_usage) {
5887 "net rpc abortshutdown\n"
5890 _("Abort a scheduled shutdown"));
5894 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5895 rpc_shutdown_abort_internals, argc, argv);
5900 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5902 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5903 rpc_reg_shutdown_abort_internals,
5908 * Shut down a remote RPC Server via initshutdown pipe.
5910 * All parameters are provided by the run_rpc_command function, except for
5911 * argc, argv which are passed through.
5913 * @param c A net_context structure.
5914 * @param domain_sid The domain sid acquired from the remote server.
5915 * @param cli A cli_state connected to the server.
5916 * @param mem_ctx Talloc context, destroyed on completion of the function.
5917 * @param argc Standard main() style argc.
5918 * @param argv Standard main() style argv. Initial components are already
5921 * @return Normal NTSTATUS return.
5924 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5925 const struct dom_sid *domain_sid,
5926 const char *domain_name,
5927 struct cli_state *cli,
5928 struct rpc_pipe_client *pipe_hnd,
5929 TALLOC_CTX *mem_ctx,
5933 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5935 const char *msg = N_("This machine will be shutdown shortly");
5936 uint32_t timeout = 20;
5937 struct lsa_StringLarge msg_string;
5938 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5940 if (c->opt_comment) {
5941 msg = c->opt_comment;
5943 if (c->opt_timeout) {
5944 timeout = c->opt_timeout;
5947 msg_string.string = msg;
5949 /* create an entry */
5950 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5951 &msg_string, timeout, c->opt_force, c->opt_reboot,
5953 if (!NT_STATUS_IS_OK(status)) {
5956 if (W_ERROR_IS_OK(result)) {
5957 d_printf(_("\nShutdown of remote machine succeeded\n"));
5958 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5960 DEBUG(1,("Shutdown of remote machine failed!\n"));
5962 return werror_to_ntstatus(result);
5966 * Shut down a remote RPC Server via winreg pipe.
5968 * All parameters are provided by the run_rpc_command function, except for
5969 * argc, argv which are passed through.
5971 * @param c A net_context structure.
5972 * @param domain_sid The domain sid acquired from the remote server.
5973 * @param cli A cli_state connected to the server.
5974 * @param mem_ctx Talloc context, destroyed on completion of the function.
5975 * @param argc Standard main() style argc.
5976 * @param argv Standard main() style argv. Initial components are already
5979 * @return Normal NTSTATUS return.
5982 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5983 const struct dom_sid *domain_sid,
5984 const char *domain_name,
5985 struct cli_state *cli,
5986 struct rpc_pipe_client *pipe_hnd,
5987 TALLOC_CTX *mem_ctx,
5991 const char *msg = N_("This machine will be shutdown shortly");
5992 uint32_t timeout = 20;
5993 struct lsa_StringLarge msg_string;
5996 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5998 if (c->opt_comment) {
5999 msg = c->opt_comment;
6001 msg_string.string = msg;
6003 if (c->opt_timeout) {
6004 timeout = c->opt_timeout;
6007 /* create an entry */
6008 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
6009 &msg_string, timeout, c->opt_force, c->opt_reboot,
6011 if (!NT_STATUS_IS_OK(result)) {
6012 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
6016 if (W_ERROR_IS_OK(werr)) {
6017 d_printf(_("\nShutdown of remote machine succeeded\n"));
6019 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
6020 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
6021 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
6023 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
6026 return werror_to_ntstatus(werr);
6030 * Shut down a remote RPC server.
6032 * @param argc Standard main() style argc.
6033 * @param argv Standard main() style argv. Initial components are already
6036 * @return A shell status integer (0 for success).
6039 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
6043 if (c->display_usage) {
6045 "net rpc shutdown\n"
6048 _("Shut down a remote RPC server"));
6052 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
6053 rpc_init_shutdown_internals, argc, argv);
6056 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
6057 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
6058 rpc_reg_shutdown_internals, argc, argv);
6064 /***************************************************************************
6065 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
6066 ***************************************************************************/
6069 * Add interdomain trust account to the RPC server.
6070 * All parameters (except for argc and argv) are passed by run_rpc_command
6073 * @param c A net_context structure.
6074 * @param domain_sid The domain sid acquired from the server.
6075 * @param cli A cli_state connected to the server.
6076 * @param mem_ctx Talloc context, destroyed on completion of the function.
6077 * @param argc Standard main() style argc.
6078 * @param argv Standard main() style argv. Initial components are already
6081 * @return normal NTSTATUS return code.
6084 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
6085 const struct dom_sid *domain_sid,
6086 const char *domain_name,
6087 struct cli_state *cli,
6088 struct rpc_pipe_client *pipe_hnd,
6089 TALLOC_CTX *mem_ctx,
6093 struct policy_handle connect_pol, domain_pol, user_pol;
6094 NTSTATUS status, result;
6096 struct lsa_String lsa_acct_name;
6098 uint32_t acct_flags=0;
6100 uint32_t access_granted = 0;
6101 union samr_UserInfo info;
6102 unsigned int orig_timeout;
6103 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6104 DATA_BLOB session_key = data_blob_null;
6105 TALLOC_CTX *frame = NULL;
6110 _(" net rpc trustdom add <domain_name> "
6111 "<trust password>\n"));
6112 return NT_STATUS_INVALID_PARAMETER;
6115 frame = talloc_stackframe();
6118 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6121 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
6122 status = NT_STATUS_NO_MEMORY;
6125 if (!strupper_m(acct_name)) {
6126 status = NT_STATUS_INVALID_PARAMETER;
6130 init_lsa_String(&lsa_acct_name, acct_name);
6132 status = cli_get_session_key(frame, pipe_hnd, &session_key);
6133 if (!NT_STATUS_IS_OK(status)) {
6134 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
6135 nt_errstr(status)));
6139 /* Get samr policy handle */
6140 status = dcerpc_samr_Connect2(b, frame,
6142 MAXIMUM_ALLOWED_ACCESS,
6145 if (!NT_STATUS_IS_OK(status)) {
6148 if (!NT_STATUS_IS_OK(result)) {
6153 /* Get domain policy handle */
6154 status = dcerpc_samr_OpenDomain(b, frame,
6156 MAXIMUM_ALLOWED_ACCESS,
6157 discard_const_p(struct dom_sid2, domain_sid),
6160 if (!NT_STATUS_IS_OK(status)) {
6163 if (!NT_STATUS_IS_OK(result)) {
6168 /* This call can take a long time - allow the server to time out.
6169 * 35 seconds should do it. */
6171 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
6173 /* Create trusting domain's account */
6174 acb_info = ACB_NORMAL;
6175 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
6176 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
6177 SAMR_USER_ACCESS_SET_PASSWORD |
6178 SAMR_USER_ACCESS_GET_ATTRIBUTES |
6179 SAMR_USER_ACCESS_SET_ATTRIBUTES;
6181 status = dcerpc_samr_CreateUser2(b, frame,
6190 if (!NT_STATUS_IS_OK(status)) {
6193 /* And restore our original timeout. */
6194 rpccli_set_timeout(pipe_hnd, orig_timeout);
6196 if (!NT_STATUS_IS_OK(result)) {
6198 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
6199 acct_name, nt_errstr(result));
6204 struct samr_CryptPassword crypt_pwd;
6206 ZERO_STRUCT(info.info23);
6208 status = init_samr_CryptPassword(argv[1],
6211 if (!NT_STATUS_IS_OK(status)) {
6215 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
6216 SAMR_FIELD_NT_PASSWORD_PRESENT;
6217 info.info23.info.acct_flags = ACB_DOMTRUST;
6218 info.info23.password = crypt_pwd;
6220 status = dcerpc_samr_SetUserInfo2(b, frame,
6225 if (!NT_STATUS_IS_OK(status)) {
6229 if (!NT_STATUS_IS_OK(result)) {
6231 DEBUG(0,("Could not set trust account password: %s\n",
6232 nt_errstr(result)));
6237 status = NT_STATUS_OK;
6239 SAFE_FREE(acct_name);
6240 data_blob_clear_free(&session_key);
6246 * Create interdomain trust account for a remote domain.
6248 * @param argc Standard argc.
6249 * @param argv Standard argv without initial components.
6251 * @return Integer status (0 means success).
6254 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
6256 if (argc > 0 && !c->display_usage) {
6257 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6258 rpc_trustdom_add_internals, argc, argv);
6262 _("net rpc trustdom add <domain_name> <trust "
6270 * Remove interdomain trust account from the RPC server.
6271 * All parameters (except for argc and argv) are passed by run_rpc_command
6274 * @param c A net_context structure.
6275 * @param domain_sid The domain sid acquired from the server.
6276 * @param cli A cli_state connected to the server.
6277 * @param mem_ctx Talloc context, destroyed on completion of the function.
6278 * @param argc Standard main() style argc.
6279 * @param argv Standard main() style argv. Initial components are already
6282 * @return normal NTSTATUS return code.
6285 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6286 const struct dom_sid *domain_sid,
6287 const char *domain_name,
6288 struct cli_state *cli,
6289 struct rpc_pipe_client *pipe_hnd,
6290 TALLOC_CTX *mem_ctx,
6294 struct policy_handle connect_pol, domain_pol, user_pol;
6295 NTSTATUS status, result;
6297 struct dom_sid trust_acct_sid;
6298 struct samr_Ids user_rids, name_types;
6299 struct lsa_String lsa_acct_name;
6300 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6305 _(" net rpc trustdom del <domain_name>\n"));
6306 return NT_STATUS_INVALID_PARAMETER;
6310 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6312 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6314 if (acct_name == NULL)
6315 return NT_STATUS_NO_MEMORY;
6317 if (!strupper_m(acct_name)) {
6318 TALLOC_FREE(acct_name);
6319 return NT_STATUS_INVALID_PARAMETER;
6322 /* Get samr policy handle */
6323 status = dcerpc_samr_Connect2(b, mem_ctx,
6325 MAXIMUM_ALLOWED_ACCESS,
6328 if (!NT_STATUS_IS_OK(status)) {
6331 if (!NT_STATUS_IS_OK(result)) {
6336 /* Get domain policy handle */
6337 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6339 MAXIMUM_ALLOWED_ACCESS,
6340 discard_const_p(struct dom_sid2, domain_sid),
6343 if (!NT_STATUS_IS_OK(status)) {
6346 if (!NT_STATUS_IS_OK(result)) {
6351 init_lsa_String(&lsa_acct_name, acct_name);
6353 status = dcerpc_samr_LookupNames(b, mem_ctx,
6360 if (!NT_STATUS_IS_OK(status)) {
6361 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6363 acct_name, nt_errstr(status));
6366 if (!NT_STATUS_IS_OK(result)) {
6368 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6370 acct_name, nt_errstr(result) );
6373 if (user_rids.count != 1) {
6374 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6377 if (name_types.count != 1) {
6378 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6382 status = dcerpc_samr_OpenUser(b, mem_ctx,
6384 MAXIMUM_ALLOWED_ACCESS,
6388 if (!NT_STATUS_IS_OK(status)) {
6389 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6391 acct_name, nt_errstr(status) );
6395 if (!NT_STATUS_IS_OK(result)) {
6397 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6399 acct_name, nt_errstr(result) );
6403 /* append the rid to the domain sid */
6404 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6408 /* remove the sid */
6410 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6414 if (!NT_STATUS_IS_OK(status)) {
6415 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6416 " on user %s failed %s\n"),
6417 acct_name, nt_errstr(status));
6420 if (!NT_STATUS_IS_OK(result)) {
6422 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6423 " on user %s failed %s\n"),
6424 acct_name, nt_errstr(result) );
6431 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6434 if (!NT_STATUS_IS_OK(status)) {
6435 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6437 acct_name, nt_errstr(status));
6441 if (!NT_STATUS_IS_OK(result)) {
6443 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6445 acct_name, nt_errstr(result) );
6449 if (!NT_STATUS_IS_OK(result)) {
6450 d_printf(_("Could not set trust account password: %s\n"),
6460 * Delete interdomain trust account for a remote domain.
6462 * @param argc Standard argc.
6463 * @param argv Standard argv without initial components.
6465 * @return Integer status (0 means success).
6468 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6470 if (argc > 0 && !c->display_usage) {
6471 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6472 rpc_trustdom_del_internals, argc, argv);
6476 _("net rpc trustdom del <domain>\n"));
6481 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6482 struct cli_state *cli,
6483 TALLOC_CTX *mem_ctx,
6484 const char *domain_name)
6486 char *dc_name = NULL;
6487 const char *buffer = NULL;
6488 struct rpc_pipe_client *netr;
6491 struct dcerpc_binding_handle *b;
6493 /* Use NetServerEnum2 */
6495 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6497 return NT_STATUS_OK;
6500 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6501 for domain %s\n", domain_name));
6503 /* Try netr_GetDcName */
6505 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
6507 if (!NT_STATUS_IS_OK(status)) {
6511 b = netr->binding_handle;
6513 status = dcerpc_netr_GetDcName(b, mem_ctx,
6520 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6524 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6525 for domain %s\n", domain_name));
6527 if (!NT_STATUS_IS_OK(status)) {
6531 return werror_to_ntstatus(result);
6535 * Establish trust relationship to a trusting domain.
6536 * Interdomain account must already be created on remote PDC.
6538 * @param c A net_context structure.
6539 * @param argc Standard argc.
6540 * @param argv Standard argv without initial components.
6542 * @return Integer status (0 means success).
6545 static int rpc_trustdom_establish(struct net_context *c, int argc,
6548 struct cli_state *cli = NULL;
6549 struct sockaddr_storage server_ss;
6550 struct rpc_pipe_client *pipe_hnd = NULL;
6551 struct policy_handle connect_hnd;
6552 TALLOC_CTX *mem_ctx;
6553 NTSTATUS nt_status, result;
6554 struct dom_sid *domain_sid;
6559 union lsa_PolicyInformation *info = NULL;
6560 struct dcerpc_binding_handle *b;
6563 * Connect to \\server\ipc$ as 'our domain' account with password
6566 if (argc != 1 || c->display_usage) {
6569 _("net rpc trustdom establish <domain_name>\n"));
6573 domain_name = smb_xstrdup(argv[0]);
6574 if (!strupper_m(domain_name)) {
6575 SAFE_FREE(domain_name);
6579 /* account name used at first is our domain's name with '$' */
6580 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6583 if (!strupper_m(acct_name)) {
6584 SAFE_FREE(domain_name);
6585 SAFE_FREE(acct_name);
6590 * opt_workgroup will be used by connection functions further,
6591 * hence it should be set to remote domain name instead of ours
6593 if (c->opt_workgroup) {
6594 c->opt_workgroup = smb_xstrdup(domain_name);
6597 c->opt_user_name = acct_name;
6598 c->opt_user_specified = true;
6600 /* find the domain controller */
6601 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6602 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6606 /* connect to ipc$ as username/password */
6607 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6608 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6610 /* Is it trusting domain account for sure ? */
6611 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6612 nt_errstr(nt_status)));
6616 /* store who we connected to */
6618 saf_store( domain_name, pdc_name );
6621 * Connect to \\server\ipc$ again (this time anonymously)
6624 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6627 if (NT_STATUS_IS_ERR(nt_status)) {
6628 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6629 domain_name, nt_errstr(nt_status)));
6633 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6634 "domain %s", domain_name))) {
6635 DEBUG(0, ("talloc_init() failed\n"));
6640 /* Make sure we're talking to a proper server */
6642 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6643 if (!NT_STATUS_IS_OK(nt_status)) {
6645 talloc_destroy(mem_ctx);
6650 * Call LsaOpenPolicy and LsaQueryInfo
6653 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6655 if (!NT_STATUS_IS_OK(nt_status)) {
6656 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6658 talloc_destroy(mem_ctx);
6662 b = pipe_hnd->binding_handle;
6664 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6666 if (NT_STATUS_IS_ERR(nt_status)) {
6667 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6668 nt_errstr(nt_status)));
6670 talloc_destroy(mem_ctx);
6674 /* Querying info level 5 */
6676 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6678 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6681 if (NT_STATUS_IS_ERR(nt_status)) {
6682 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6683 nt_errstr(nt_status)));
6685 talloc_destroy(mem_ctx);
6688 if (NT_STATUS_IS_ERR(result)) {
6689 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6690 nt_errstr(result)));
6692 talloc_destroy(mem_ctx);
6696 domain_sid = info->account_domain.sid;
6698 /* There should be actually query info level 3 (following nt serv behaviour),
6699 but I still don't know if it's _really_ necessary */
6702 * Store the password in secrets db
6705 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6706 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6708 talloc_destroy(mem_ctx);
6713 * Close the pipes and clean up
6716 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6717 if (NT_STATUS_IS_ERR(nt_status)) {
6718 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6719 nt_errstr(nt_status)));
6721 talloc_destroy(mem_ctx);
6727 talloc_destroy(mem_ctx);
6729 d_printf(_("Trust to domain %s established\n"), domain_name);
6734 * Revoke trust relationship to the remote domain.
6736 * @param c A net_context structure.
6737 * @param argc Standard argc.
6738 * @param argv Standard argv without initial components.
6740 * @return Integer status (0 means success).
6743 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6749 if (argc < 1 || c->display_usage) {
6752 _("net rpc trustdom revoke <domain_name>\n"
6753 " Revoke trust relationship\n"
6754 " domain_name\tName of domain to revoke trust\n"));
6758 /* generate upper cased domain name */
6759 domain_name = smb_xstrdup(argv[0]);
6760 if (!strupper_m(domain_name)) {
6761 SAFE_FREE(domain_name);
6765 /* delete password of the trust */
6766 if (!pdb_del_trusteddom_pw(domain_name)) {
6767 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6774 SAFE_FREE(domain_name);
6778 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6779 const struct dom_sid *domain_sid,
6780 const char *domain_name,
6781 struct cli_state *cli,
6782 struct rpc_pipe_client *pipe_hnd,
6783 TALLOC_CTX *mem_ctx,
6787 struct dom_sid_buf sid_str;
6788 d_printf("%s\n", dom_sid_str_buf(domain_sid, &sid_str));
6789 return NT_STATUS_OK;
6792 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6794 struct dom_sid_buf sid_str;
6796 d_printf("%-20s%s\n",
6798 dom_sid_str_buf(dom_sid, &sid_str));
6801 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6802 TALLOC_CTX *mem_ctx,
6803 struct policy_handle *pol,
6804 struct dom_sid dom_sid,
6805 const char *trusted_dom_name)
6807 NTSTATUS nt_status, result;
6808 union lsa_TrustedDomainInfo *info = NULL;
6809 char *cleartextpwd = NULL;
6810 DATA_BLOB session_key;
6811 DATA_BLOB data = data_blob_null;
6812 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6814 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6817 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6820 if (NT_STATUS_IS_ERR(nt_status)) {
6821 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6822 nt_errstr(nt_status)));
6825 if (NT_STATUS_IS_ERR(result)) {
6827 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6828 nt_errstr(result)));
6832 data = data_blob(info->password.password->data,
6833 info->password.password->length);
6835 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6836 if (!NT_STATUS_IS_OK(nt_status)) {
6837 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6841 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6842 data_blob_free(&session_key);
6844 if (cleartextpwd == NULL) {
6845 DEBUG(0,("retrieved NULL password\n"));
6846 nt_status = NT_STATUS_UNSUCCESSFUL;
6850 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6851 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6852 nt_status = NT_STATUS_UNSUCCESSFUL;
6856 #ifdef DEBUG_PASSWORD
6858 struct dom_sid_buf buf;
6859 DEBUG(100,("successfully vampired trusted domain [%s], "
6860 "sid: [%s], password: [%s]\n",
6862 dom_sid_str_buf(&dom_sid, &buf),
6868 SAFE_FREE(cleartextpwd);
6869 data_blob_free(&data);
6874 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6877 /* common variables */
6878 TALLOC_CTX* mem_ctx;
6879 struct cli_state *cli = NULL;
6880 struct rpc_pipe_client *pipe_hnd = NULL;
6881 NTSTATUS nt_status, result;
6882 const char *domain_name = NULL;
6883 struct policy_handle connect_hnd;
6884 union lsa_PolicyInformation *info = NULL;
6886 /* trusted domains listing variables */
6887 unsigned int enum_ctx = 0;
6889 struct lsa_DomainList dom_list;
6891 struct dcerpc_binding_handle *b;
6893 if (c->display_usage) {
6895 "net rpc trustdom vampire\n"
6898 _("Vampire trust relationship from remote server"));
6903 * Listing trusted domains (stored in secrets.tdb, if local)
6906 mem_ctx = talloc_init("trust relationships vampire");
6909 * set domain and pdc name to local samba server (default)
6910 * or to remote one given in command line
6913 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6914 domain_name = c->opt_workgroup;
6915 c->opt_target_workgroup = c->opt_workgroup;
6917 fstrcpy(pdc_name, lp_netbios_name());
6918 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6919 c->opt_target_workgroup = domain_name;
6922 /* open \PIPE\lsarpc and open policy handle */
6923 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6924 if (!NT_STATUS_IS_OK(nt_status)) {
6925 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6926 nt_errstr(nt_status)));
6927 talloc_destroy(mem_ctx);
6931 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6933 if (!NT_STATUS_IS_OK(nt_status)) {
6934 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6935 nt_errstr(nt_status) ));
6937 talloc_destroy(mem_ctx);
6941 b = pipe_hnd->binding_handle;
6943 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6945 if (NT_STATUS_IS_ERR(nt_status)) {
6946 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6947 nt_errstr(nt_status)));
6949 talloc_destroy(mem_ctx);
6953 /* query info level 5 to obtain sid of a domain being queried */
6954 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6956 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6960 if (NT_STATUS_IS_ERR(nt_status)) {
6961 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6962 nt_errstr(nt_status)));
6964 talloc_destroy(mem_ctx);
6967 if (NT_STATUS_IS_ERR(result)) {
6968 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6969 nt_errstr(result)));
6971 talloc_destroy(mem_ctx);
6976 * Keep calling LsaEnumTrustdom over opened pipe until
6977 * the end of enumeration is reached
6980 d_printf(_("Vampire trusted domains:\n\n"));
6983 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6989 if (NT_STATUS_IS_ERR(nt_status)) {
6990 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6991 nt_errstr(nt_status)));
6993 talloc_destroy(mem_ctx);
6996 if (NT_STATUS_IS_ERR(result)) {
6998 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6999 nt_errstr(result)));
7001 talloc_destroy(mem_ctx);
7006 for (i = 0; i < dom_list.count; i++) {
7008 print_trusted_domain(dom_list.domains[i].sid,
7009 dom_list.domains[i].name.string);
7011 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
7012 *dom_list.domains[i].sid,
7013 dom_list.domains[i].name.string);
7014 if (!NT_STATUS_IS_OK(nt_status)) {
7016 talloc_destroy(mem_ctx);
7022 * in case of no trusted domains say something rather
7023 * than just display blank line
7025 if (!dom_list.count) d_printf(_("none\n"));
7027 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7029 /* close this connection before doing next one */
7030 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7031 if (NT_STATUS_IS_ERR(nt_status)) {
7032 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7033 nt_errstr(nt_status)));
7035 talloc_destroy(mem_ctx);
7039 /* close lsarpc pipe and connection to IPC$ */
7042 talloc_destroy(mem_ctx);
7046 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
7048 /* common variables */
7049 TALLOC_CTX* mem_ctx;
7050 struct cli_state *cli = NULL, *remote_cli = NULL;
7051 struct rpc_pipe_client *pipe_hnd = NULL;
7052 NTSTATUS nt_status, result;
7053 const char *domain_name = NULL;
7054 struct dom_sid *queried_dom_sid;
7055 int ascii_dom_name_len;
7056 struct policy_handle connect_hnd;
7057 union lsa_PolicyInformation *info = NULL;
7058 struct dcerpc_binding_handle *b = NULL;
7060 /* trusted domains listing variables */
7061 unsigned int num_domains, enum_ctx = 0;
7063 struct lsa_DomainList dom_list;
7067 /* trusting domains listing variables */
7068 struct policy_handle domain_hnd;
7069 struct samr_SamArray *trusts = NULL;
7071 if (c->display_usage) {
7073 "net rpc trustdom list\n"
7076 _("List incoming and outgoing trust relationships"));
7081 * Listing trusted domains (stored in secrets.tdb, if local)
7084 mem_ctx = talloc_init("trust relationships listing");
7087 * set domain and pdc name to local samba server (default)
7088 * or to remote one given in command line
7091 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
7092 domain_name = c->opt_workgroup;
7093 c->opt_target_workgroup = c->opt_workgroup;
7095 fstrcpy(pdc_name, lp_netbios_name());
7096 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
7097 c->opt_target_workgroup = domain_name;
7100 /* open \PIPE\lsarpc and open policy handle */
7101 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
7102 if (!NT_STATUS_IS_OK(nt_status)) {
7103 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
7104 nt_errstr(nt_status)));
7105 talloc_destroy(mem_ctx);
7109 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
7111 if (!NT_STATUS_IS_OK(nt_status)) {
7112 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
7113 nt_errstr(nt_status) ));
7115 talloc_destroy(mem_ctx);
7119 b = pipe_hnd->binding_handle;
7121 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
7123 if (NT_STATUS_IS_ERR(nt_status)) {
7124 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
7125 nt_errstr(nt_status)));
7127 talloc_destroy(mem_ctx);
7131 /* query info level 5 to obtain sid of a domain being queried */
7132 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
7134 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
7138 if (NT_STATUS_IS_ERR(nt_status)) {
7139 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7140 nt_errstr(nt_status)));
7142 talloc_destroy(mem_ctx);
7145 if (NT_STATUS_IS_ERR(result)) {
7146 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
7147 nt_errstr(result)));
7149 talloc_destroy(mem_ctx);
7153 queried_dom_sid = info->account_domain.sid;
7156 * Keep calling LsaEnumTrustdom over opened pipe until
7157 * the end of enumeration is reached
7160 d_printf(_("Trusted domains list:\n\n"));
7162 found_domain = false;
7165 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
7171 if (NT_STATUS_IS_ERR(nt_status)) {
7172 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7173 nt_errstr(nt_status)));
7175 talloc_destroy(mem_ctx);
7178 if (NT_STATUS_IS_ERR(result)) {
7179 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
7180 nt_errstr(result)));
7182 talloc_destroy(mem_ctx);
7187 for (i = 0; i < dom_list.count; i++) {
7188 print_trusted_domain(dom_list.domains[i].sid,
7189 dom_list.domains[i].name.string);
7190 found_domain = true;
7194 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
7197 * in case of no trusted domains say something rather
7198 * than just display blank line
7200 if (!found_domain) {
7201 d_printf(_("none\n"));
7204 /* close this connection before doing next one */
7205 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
7206 if (NT_STATUS_IS_ERR(nt_status)) {
7207 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
7208 nt_errstr(nt_status)));
7210 talloc_destroy(mem_ctx);
7214 TALLOC_FREE(pipe_hnd);
7217 * Listing trusting domains (stored in passdb backend, if local)
7220 d_printf(_("\nTrusting domains list:\n\n"));
7223 * Open \PIPE\samr and get needed policy handles
7225 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
7227 if (!NT_STATUS_IS_OK(nt_status)) {
7228 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
7230 talloc_destroy(mem_ctx);
7234 b = pipe_hnd->binding_handle;
7237 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
7239 SAMR_ACCESS_LOOKUP_DOMAIN,
7242 if (!NT_STATUS_IS_OK(nt_status)) {
7243 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7244 nt_errstr(nt_status)));
7246 talloc_destroy(mem_ctx);
7249 if (!NT_STATUS_IS_OK(result)) {
7251 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
7252 nt_errstr(result)));
7254 talloc_destroy(mem_ctx);
7258 /* SamrOpenDomain - we have to open domain policy handle in order to be
7259 able to enumerate accounts*/
7260 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
7262 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
7266 if (!NT_STATUS_IS_OK(nt_status)) {
7267 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7268 nt_errstr(nt_status)));
7270 talloc_destroy(mem_ctx);
7273 if (!NT_STATUS_IS_OK(result)) {
7275 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7276 nt_errstr(result)));
7278 talloc_destroy(mem_ctx);
7283 * perform actual enumeration
7286 found_domain = false;
7288 enum_ctx = 0; /* reset enumeration context from last enumeration */
7291 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
7299 if (NT_STATUS_IS_ERR(nt_status)) {
7300 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7301 nt_errstr(nt_status)));
7303 talloc_destroy(mem_ctx);
7306 if (NT_STATUS_IS_ERR(result)) {
7308 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7309 nt_errstr(result)));
7311 talloc_destroy(mem_ctx);
7315 for (i = 0; i < num_domains; i++) {
7317 char *str = discard_const_p(char, trusts->entries[i].name.string);
7319 found_domain = true;
7322 * get each single domain's sid (do we _really_ need this ?):
7323 * 1) connect to domain's pdc
7324 * 2) query the pdc for domain's sid
7327 /* get rid of '$' tail */
7328 ascii_dom_name_len = strlen(str);
7329 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7330 str[ascii_dom_name_len - 1] = '\0';
7332 /* set opt_* variables to remote domain */
7333 if (!strupper_m(str)) {
7335 talloc_destroy(mem_ctx);
7338 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7339 c->opt_target_workgroup = c->opt_workgroup;
7341 d_printf("%-20s", str);
7343 /* connect to remote domain controller */
7344 nt_status = net_make_ipc_connection(c,
7345 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7347 if (NT_STATUS_IS_OK(nt_status)) {
7348 /* query for domain's sid */
7349 if (run_rpc_command(
7351 &ndr_table_lsarpc, 0,
7352 rpc_query_domain_sid, argc,
7354 d_printf(_("strange - couldn't get domain's sid\n"));
7356 cli_shutdown(remote_cli);
7359 d_fprintf(stderr, _("domain controller is not "
7360 "responding: %s\n"),
7361 nt_errstr(nt_status));
7362 d_printf(_("couldn't get domain's sid\n"));
7366 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7368 if (!found_domain) {
7372 /* close opened samr and domain policy handles */
7373 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7374 if (!NT_STATUS_IS_OK(nt_status)) {
7375 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7378 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7379 if (!NT_STATUS_IS_OK(nt_status)) {
7380 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7383 /* close samr pipe and connection to IPC$ */
7386 talloc_destroy(mem_ctx);
7391 * Entrypoint for 'net rpc trustdom' code.
7393 * @param argc Standard argc.
7394 * @param argv Standard argv without initial components.
7396 * @return Integer status (0 means success).
7399 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7401 struct functable func[] = {
7406 N_("Add trusting domain's account"),
7407 N_("net rpc trustdom add\n"
7408 " Add trusting domain's account")
7414 N_("Remove trusting domain's account"),
7415 N_("net rpc trustdom del\n"
7416 " Remove trusting domain's account")
7420 rpc_trustdom_establish,
7422 N_("Establish outgoing trust relationship"),
7423 N_("net rpc trustdom establish\n"
7424 " Establish outgoing trust relationship")
7428 rpc_trustdom_revoke,
7430 N_("Revoke outgoing trust relationship"),
7431 N_("net rpc trustdom revoke\n"
7432 " Revoke outgoing trust relationship")
7438 N_("List in- and outgoing domain trusts"),
7439 N_("net rpc trustdom list\n"
7440 " List in- and outgoing domain trusts")
7444 rpc_trustdom_vampire,
7446 N_("Vampire trusts from remote server"),
7447 N_("net rpc trustdom vampire\n"
7448 " Vampire trusts from remote server")
7450 {NULL, NULL, 0, NULL, NULL}
7453 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7457 * Check if a server will take rpc commands
7458 * @param flags Type of server to connect to (PDC, DMB, localhost)
7459 * if the host is not explicitly specified
7460 * @return bool (true means rpc supported)
7462 bool net_rpc_check(struct net_context *c, unsigned flags)
7464 struct cli_state *cli;
7466 struct sockaddr_storage server_ss;
7467 char *server_name = NULL;
7470 /* flags (i.e. server type) may depend on command */
7471 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7474 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7475 lp_netbios_name(), SMB_SIGNING_IPC_DEFAULT,
7477 if (!NT_STATUS_IS_OK(status)) {
7478 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
7479 DBG_ERR("NetBIOS support disabled, unable to connect\n");
7483 status = smbXcli_negprot(cli->conn, cli->timeout,
7484 lp_client_min_protocol(),
7485 lp_client_max_protocol());
7486 if (!NT_STATUS_IS_OK(status))
7488 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7497 /* syncronise sam database via samsync rpc calls */
7498 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7500 struct functable func[] = {
7505 N_("Dump remote SAM database to Kerberos Keytab"),
7506 N_("net rpc vampire keytab\n"
7507 " Dump remote SAM database to Kerberos keytab "
7514 N_("Dump remote SAM database to passdb"),
7515 N_("net rpc vampire passdb\n"
7516 " Dump remote SAM database to passdb")
7519 {NULL, NULL, 0, NULL, NULL}
7523 if (c->display_usage) {
7528 _("Vampire remote SAM database"));
7532 return rpc_vampire_passdb(c, argc, argv);
7535 return net_run_function(c, argc, argv, "net rpc vampire", func);
7539 * Migrate everything from a print server.
7541 * @param c A net_context structure.
7542 * @param argc Standard main() style argc.
7543 * @param argv Standard main() style argv. Initial components are already
7546 * @return A shell status integer (0 for success).
7548 * The order is important !
7549 * To successfully add drivers the print queues have to exist !
7550 * Applying ACLs should be the last step, because you're easily locked out.
7553 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7558 if (c->display_usage) {
7560 "net rpc printer migrate all\n"
7563 _("Migrate everything from a print server"));
7568 d_printf(_("no server to migrate\n"));
7572 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7573 rpc_printer_migrate_printers_internals, argc,
7578 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7579 rpc_printer_migrate_drivers_internals, argc,
7584 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7585 rpc_printer_migrate_forms_internals, argc, argv);
7589 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7590 rpc_printer_migrate_settings_internals, argc,
7595 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7596 rpc_printer_migrate_security_internals, argc,
7602 * Migrate print drivers from a print server.
7604 * @param c A net_context structure.
7605 * @param argc Standard main() style argc.
7606 * @param argv Standard main() style argv. Initial components are already
7609 * @return A shell status integer (0 for success).
7611 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7614 if (c->display_usage) {
7616 "net rpc printer migrate drivers\n"
7619 _("Migrate print-drivers from a print-server"));
7624 d_printf(_("no server to migrate\n"));
7628 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7629 rpc_printer_migrate_drivers_internals,
7634 * Migrate print-forms from a print-server.
7636 * @param c A net_context structure.
7637 * @param argc Standard main() style argc.
7638 * @param argv Standard main() style argv. Initial components are already
7641 * @return A shell status integer (0 for success).
7643 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7646 if (c->display_usage) {
7648 "net rpc printer migrate forms\n"
7651 _("Migrate print-forms from a print-server"));
7656 d_printf(_("no server to migrate\n"));
7660 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7661 rpc_printer_migrate_forms_internals,
7666 * Migrate printers from a print-server.
7668 * @param c A net_context structure.
7669 * @param argc Standard main() style argc.
7670 * @param argv Standard main() style argv. Initial components are already
7673 * @return A shell status integer (0 for success).
7675 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7678 if (c->display_usage) {
7680 "net rpc printer migrate printers\n"
7683 _("Migrate printers from a print-server"));
7688 d_printf(_("no server to migrate\n"));
7692 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7693 rpc_printer_migrate_printers_internals,
7698 * Migrate printer-ACLs from a print-server
7700 * @param c A net_context structure.
7701 * @param argc Standard main() style argc.
7702 * @param argv Standard main() style argv. Initial components are already
7705 * @return A shell status integer (0 for success).
7707 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7710 if (c->display_usage) {
7712 "net rpc printer migrate security\n"
7715 _("Migrate printer-ACLs from a print-server"));
7720 d_printf(_("no server to migrate\n"));
7724 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7725 rpc_printer_migrate_security_internals,
7730 * Migrate printer-settings from a print-server.
7732 * @param c A net_context structure.
7733 * @param argc Standard main() style argc.
7734 * @param argv Standard main() style argv. Initial components are already
7737 * @return A shell status integer (0 for success).
7739 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7742 if (c->display_usage) {
7744 "net rpc printer migrate settings\n"
7747 _("Migrate printer-settings from a "
7753 d_printf(_("no server to migrate\n"));
7757 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7758 rpc_printer_migrate_settings_internals,
7763 * 'net rpc printer' entrypoint.
7765 * @param c A net_context structure.
7766 * @param argc Standard main() style argc.
7767 * @param argv Standard main() style argv. Initial components are already
7771 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7774 /* ouch: when addriver and setdriver are called from within
7775 rpc_printer_migrate_drivers_internals, the printer-queue already
7778 struct functable func[] = {
7781 rpc_printer_migrate_all,
7783 N_("Migrate all from remote to local print server"),
7784 N_("net rpc printer migrate all\n"
7785 " Migrate all from remote to local print server")
7789 rpc_printer_migrate_drivers,
7791 N_("Migrate drivers to local server"),
7792 N_("net rpc printer migrate drivers\n"
7793 " Migrate drivers to local server")
7797 rpc_printer_migrate_forms,
7799 N_("Migrate froms to local server"),
7800 N_("net rpc printer migrate forms\n"
7801 " Migrate froms to local server")
7805 rpc_printer_migrate_printers,
7807 N_("Migrate printers to local server"),
7808 N_("net rpc printer migrate printers\n"
7809 " Migrate printers to local server")
7813 rpc_printer_migrate_security,
7815 N_("Migrate printer ACLs to local server"),
7816 N_("net rpc printer migrate security\n"
7817 " Migrate printer ACLs to local server")
7821 rpc_printer_migrate_settings,
7823 N_("Migrate printer settings to local server"),
7824 N_("net rpc printer migrate settings\n"
7825 " Migrate printer settings to local server")
7827 {NULL, NULL, 0, NULL, NULL}
7830 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7835 * List printers on a remote RPC server.
7837 * @param c A net_context structure.
7838 * @param argc Standard main() style argc.
7839 * @param argv Standard main() style argv. Initial components are already
7842 * @return A shell status integer (0 for success).
7844 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7846 if (c->display_usage) {
7848 "net rpc printer list\n"
7851 _("List printers on a remote RPC server"));
7855 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7856 rpc_printer_list_internals,
7861 * List printer-drivers on a remote RPC server.
7863 * @param c A net_context structure.
7864 * @param argc Standard main() style argc.
7865 * @param argv Standard main() style argv. Initial components are already
7868 * @return A shell status integer (0 for success).
7870 static int rpc_printer_driver_list(struct net_context *c, int argc,
7873 if (c->display_usage) {
7875 "net rpc printer driver\n"
7878 _("List printer-drivers on a remote RPC server"));
7882 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7883 rpc_printer_driver_list_internals,
7888 * Publish printer in ADS via MSRPC.
7890 * @param c A net_context structure.
7891 * @param argc Standard main() style argc.
7892 * @param argv Standard main() style argv. Initial components are already
7895 * @return A shell status integer (0 for success).
7897 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7900 if (c->display_usage) {
7902 "net rpc printer publish publish\n"
7905 _("Publish printer in ADS via MSRPC"));
7909 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7910 rpc_printer_publish_publish_internals,
7915 * Update printer in ADS via MSRPC.
7917 * @param c A net_context structure.
7918 * @param argc Standard main() style argc.
7919 * @param argv Standard main() style argv. Initial components are already
7922 * @return A shell status integer (0 for success).
7924 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7926 if (c->display_usage) {
7928 "net rpc printer publish update\n"
7931 _("Update printer in ADS via MSRPC"));
7935 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7936 rpc_printer_publish_update_internals,
7941 * UnPublish printer in ADS via MSRPC.
7943 * @param c A net_context structure.
7944 * @param argc Standard main() style argc.
7945 * @param argv Standard main() style argv. Initial components are already
7948 * @return A shell status integer (0 for success).
7950 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7953 if (c->display_usage) {
7955 "net rpc printer publish unpublish\n"
7958 _("UnPublish printer in ADS via MSRPC"));
7962 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7963 rpc_printer_publish_unpublish_internals,
7968 * List published printers via MSRPC.
7970 * @param c A net_context structure.
7971 * @param argc Standard main() style argc.
7972 * @param argv Standard main() style argv. Initial components are already
7975 * @return A shell status integer (0 for success).
7977 static int rpc_printer_publish_list(struct net_context *c, int argc,
7980 if (c->display_usage) {
7982 "net rpc printer publish list\n"
7985 _("List published printers via MSRPC"));
7989 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7990 rpc_printer_publish_list_internals,
7996 * Publish printer in ADS.
7998 * @param c A net_context structure.
7999 * @param argc Standard main() style argc.
8000 * @param argv Standard main() style argv. Initial components are already
8003 * @return A shell status integer (0 for success).
8005 static int rpc_printer_publish(struct net_context *c, int argc,
8009 struct functable func[] = {
8012 rpc_printer_publish_publish,
8014 N_("Publish printer in AD"),
8015 N_("net rpc printer publish publish\n"
8016 " Publish printer in AD")
8020 rpc_printer_publish_update,
8022 N_("Update printer in AD"),
8023 N_("net rpc printer publish update\n"
8024 " Update printer in AD")
8028 rpc_printer_publish_unpublish,
8030 N_("Unpublish printer"),
8031 N_("net rpc printer publish unpublish\n"
8032 " Unpublish printer")
8036 rpc_printer_publish_list,
8038 N_("List published printers"),
8039 N_("net rpc printer publish list\n"
8040 " List published printers")
8042 {NULL, NULL, 0, NULL, NULL}
8046 if (c->display_usage) {
8047 d_printf(_("Usage:\n"));
8048 d_printf(_("net rpc printer publish\n"
8049 " List published printers\n"
8050 " Alias of net rpc printer publish "
8052 net_display_usage_from_functable(func);
8055 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8056 rpc_printer_publish_list_internals,
8060 return net_run_function(c, argc, argv, "net rpc printer publish",func);
8066 * Display rpc printer help page.
8068 * @param c A net_context structure.
8069 * @param argc Standard main() style argc.
8070 * @param argv Standard main() style argv. Initial components are already
8073 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
8075 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
8076 "\tlists all printers on print-server\n\n"));
8077 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
8078 "\tlists all printer-drivers on print-server\n\n"));
8079 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
8080 "\tpublishes printer settings in Active Directory\n"
8081 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
8082 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
8083 "\n\tmigrates printers from remote to local server\n\n"));
8084 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
8085 "\n\tmigrates printer-settings from remote to local server\n\n"));
8086 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
8087 "\n\tmigrates printer-drivers from remote to local server\n\n"));
8088 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
8089 "\n\tmigrates printer-forms from remote to local server\n\n"));
8090 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
8091 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
8092 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
8093 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
8094 "\tremote to local print-server\n\n"));
8095 net_common_methods_usage(c, argc, argv);
8096 net_common_flags_usage(c, argc, argv);
8098 "\t-v or --verbose\t\t\tgive verbose output\n"
8099 "\t --destination\t\tmigration target server (default: localhost)\n"));
8105 * 'net rpc printer' entrypoint.
8107 * @param c A net_context structure.
8108 * @param argc Standard main() style argc.
8109 * @param argv Standard main() style argv. Initial components are already
8112 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
8114 struct functable func[] = {
8119 N_("List all printers on print server"),
8120 N_("net rpc printer list\n"
8121 " List all printers on print server")
8125 rpc_printer_migrate,
8127 N_("Migrate printer to local server"),
8128 N_("net rpc printer migrate\n"
8129 " Migrate printer to local server")
8133 rpc_printer_driver_list,
8135 N_("List printer drivers"),
8136 N_("net rpc printer driver\n"
8137 " List printer drivers")
8141 rpc_printer_publish,
8143 N_("Publish printer in AD"),
8144 N_("net rpc printer publish\n"
8145 " Publish printer in AD")
8147 {NULL, NULL, 0, NULL, NULL}
8151 if (c->display_usage) {
8152 d_printf(_("Usage:\n"));
8153 d_printf(_("net rpc printer\n"
8154 " List printers\n"));
8155 net_display_usage_from_functable(func);
8158 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
8159 rpc_printer_list_internals,
8163 return net_run_function(c, argc, argv, "net rpc printer", func);
8167 * 'net rpc' entrypoint.
8169 * @param c A net_context structure.
8170 * @param argc Standard main() style argc.
8171 * @param argv Standard main() style argv. Initial components are already
8175 int net_rpc(struct net_context *c, int argc, const char **argv)
8177 NET_API_STATUS status;
8179 struct functable func[] = {
8184 N_("Modify global audit settings"),
8185 N_("net rpc audit\n"
8186 " Modify global audit settings")
8192 N_("Show basic info about a domain"),
8194 " Show basic info about a domain")
8200 N_("Join a domain"),
8208 N_("Join a domain created in server manager"),
8209 N_("net rpc oldjoin\n"
8210 " Join a domain created in server manager")
8216 N_("Test that a join is valid"),
8217 N_("net rpc testjoin\n"
8218 " Test that a join is valid")
8224 N_("List/modify users"),
8226 " List/modify users")
8232 N_("Change a user password"),
8233 N_("net rpc password\n"
8234 " Change a user password\n"
8235 " Alias for net rpc user password")
8241 N_("List/modify groups"),
8242 N_("net rpc group\n"
8243 " List/modify groups")
8249 N_("List/modify shares"),
8250 N_("net rpc share\n"
8251 " List/modify shares")
8257 N_("List open files"),
8265 N_("List/modify printers"),
8266 N_("net rpc printer\n"
8267 " List/modify printers")
8271 net_rpc_changetrustpw,
8273 N_("Change trust account password"),
8274 N_("net rpc changetrustpw\n"
8275 " Change trust account password")
8281 N_("Modify domain trusts"),
8282 N_("net rpc trustdom\n"
8283 " Modify domain trusts")
8289 N_("Abort a remote shutdown"),
8290 N_("net rpc abortshutdown\n"
8291 " Abort a remote shutdown")
8297 N_("Shutdown a remote server"),
8298 N_("net rpc shutdown\n"
8299 " Shutdown a remote server")
8305 N_("Sync a remote NT PDC's data into local passdb"),
8306 N_("net rpc vampire\n"
8307 " Sync a remote NT PDC's data into local passdb")
8313 N_("Fetch the domain sid into local secrets.tdb"),
8314 N_("net rpc getsid\n"
8315 " Fetch the domain sid into local secrets.tdb")
8321 N_("Manage privileges assigned to SID"),
8322 N_("net rpc rights\n"
8323 " Manage privileges assigned to SID")
8329 N_("Start/stop/query remote services"),
8330 N_("net rpc service\n"
8331 " Start/stop/query remote services")
8337 N_("Manage registry hives"),
8338 N_("net rpc registry\n"
8339 " Manage registry hives")
8345 N_("Open interactive shell on remote server"),
8346 N_("net rpc shell\n"
8347 " Open interactive shell on remote server")
8353 N_("Manage trusts"),
8354 N_("net rpc trust\n"
8361 N_("Configure a remote samba server"),
8363 " Configure a remote samba server")
8365 {NULL, NULL, 0, NULL, NULL}
8368 status = libnetapi_net_init(&c->netapi_ctx);
8372 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8373 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8374 if (c->opt_kerberos) {
8375 libnetapi_set_use_kerberos(c->netapi_ctx);
8377 if (c->opt_ccache) {
8378 libnetapi_set_use_ccache(c->netapi_ctx);
8381 return net_run_function(c, argc, argv, "net rpc", func);