2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/cli_samr.h"
29 #include "rpc_client/init_samr.h"
30 #include "../librpc/gen_ndr/ndr_lsa_c.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
34 #include "../librpc/gen_ndr/ndr_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
36 #include "../librpc/gen_ndr/ndr_winreg_c.h"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "librpc/gen_ndr/libnet_join.h"
41 #include "libnet/libnet_join.h"
42 #include "rpc_client/init_lsa.h"
43 #include "../libcli/security/security.h"
44 #include "libsmb/libsmb.h"
45 #include "libsmb/clirap.h"
46 #include "nsswitch/libwbclient/wbclient.h"
48 #include "../libcli/smb/smbXcli_base.h"
50 static int net_mode_share;
51 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
56 * @brief RPC based subcommands for the 'net' utility.
58 * This file should contain much of the functionality that used to
59 * be found in rpcclient, execpt that the commands should change
60 * less often, and the fucntionality should be sane (the user is not
61 * expected to know a rid/sid before they conduct an operation etc.)
63 * @todo Perhaps eventually these should be split out into a number
64 * of files, as this could get quite big.
69 * Many of the RPC functions need the domain sid. This function gets
70 * it at the start of every run
72 * @param cli A cli_state already connected to the remote machine
74 * @return The Domain SID of the remote machine.
77 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
78 struct dom_sid **domain_sid,
79 const char **domain_name)
81 struct rpc_pipe_client *lsa_pipe = NULL;
82 struct policy_handle pol;
83 NTSTATUS status, result;
84 union lsa_PolicyInformation *info = NULL;
85 struct dcerpc_binding_handle *b;
87 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
89 if (!NT_STATUS_IS_OK(status)) {
90 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
94 b = lsa_pipe->binding_handle;
96 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
97 SEC_FLAG_MAXIMUM_ALLOWED,
99 if (!NT_STATUS_IS_OK(status)) {
100 d_fprintf(stderr, "open_policy %s: %s\n",
106 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
108 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
111 if (!NT_STATUS_IS_OK(status)) {
112 d_fprintf(stderr, "lsaquery %s: %s\n",
117 if (!NT_STATUS_IS_OK(result)) {
118 d_fprintf(stderr, "lsaquery %s: %s\n",
124 *domain_name = info->account_domain.name.string;
125 *domain_sid = info->account_domain.sid;
127 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
128 TALLOC_FREE(lsa_pipe);
134 * Run a single RPC command, from start to finish.
136 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
137 * @param conn_flag a NET_FLAG_ combination. Passed to
138 * net_make_ipc_connection.
139 * @param argc Standard main() style argc.
140 * @param argv Standard main() style argv. Initial components are already
142 * @return A shell status integer (0 for success).
145 int run_rpc_command(struct net_context *c,
146 struct cli_state *cli_arg,
147 const struct ndr_interface_table *table,
153 struct cli_state *cli = NULL;
154 struct rpc_pipe_client *pipe_hnd = NULL;
157 struct dom_sid *domain_sid;
158 const char *domain_name;
161 /* make use of cli_state handed over as an argument, if possible */
163 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 DEBUG(1, ("failed to make ipc connection: %s\n",
166 nt_errstr(nt_status)));
179 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
180 DEBUG(0, ("talloc_init() failed\n"));
184 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
186 if (!NT_STATUS_IS_OK(nt_status)) {
190 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
191 if (lp_client_schannel()
192 && (ndr_syntax_id_equal(&table->syntax_id,
193 &ndr_table_netlogon.syntax_id))) {
194 /* Always try and create an schannel netlogon pipe. */
195 nt_status = cli_rpc_pipe_open_schannel(
196 cli, table, NCACN_NP,
197 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
199 if (!NT_STATUS_IS_OK(nt_status)) {
200 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
201 nt_errstr(nt_status) ));
205 if (conn_flags & NET_FLAGS_SEAL) {
206 nt_status = cli_rpc_pipe_open_generic_auth(
208 (conn_flags & NET_FLAGS_TCP) ?
209 NCACN_IP_TCP : NCACN_NP,
210 DCERPC_AUTH_TYPE_NTLMSSP,
211 DCERPC_AUTH_LEVEL_PRIVACY,
212 smbXcli_conn_remote_name(cli->conn),
213 lp_workgroup(), c->opt_user_name,
214 c->opt_password, &pipe_hnd);
216 nt_status = cli_rpc_pipe_open_noauth(
220 if (!NT_STATUS_IS_OK(nt_status)) {
221 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
223 nt_errstr(nt_status) ));
229 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
231 if (!NT_STATUS_IS_OK(nt_status)) {
232 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
235 DEBUG(5, ("rpc command function succedded\n"));
238 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
240 TALLOC_FREE(pipe_hnd);
245 /* close the connection only if it was opened here */
250 talloc_destroy(mem_ctx);
255 * Force a change of the trust acccount password.
257 * All parameters are provided by the run_rpc_command function, except for
258 * argc, argv which are passed through.
260 * @param domain_sid The domain sid acquired from the remote server.
261 * @param cli A cli_state connected to the server.
262 * @param mem_ctx Talloc context, destroyed on completion of the function.
263 * @param argc Standard main() style argc.
264 * @param argv Standard main() style argv. Initial components are already
267 * @return Normal NTSTATUS return.
270 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
271 const struct dom_sid *domain_sid,
272 const char *domain_name,
273 struct cli_state *cli,
274 struct rpc_pipe_client *pipe_hnd,
281 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
282 if (!NT_STATUS_IS_OK(status)) {
283 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
292 * Force a change of the trust acccount password.
294 * @param argc Standard main() style argc.
295 * @param argv Standard main() style argv. Initial components are already
298 * @return A shell status integer (0 for success).
301 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
303 if (c->display_usage) {
305 "net rpc changetrustpw\n"
308 _("Change the machine trust password"));
312 return run_rpc_command(c, NULL, &ndr_table_netlogon,
313 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
314 rpc_changetrustpw_internals,
319 * Join a domain, the old way. This function exists to allow
320 * the message to be displayed when oldjoin was explicitly
321 * requested, but not when it was implied by "net rpc join".
323 * This uses 'machinename' as the inital password, and changes it.
325 * The password should be created with 'server manager' or equiv first.
327 * @param argc Standard main() style argc.
328 * @param argv Standard main() style argv. Initial components are already
331 * @return A shell status integer (0 for success).
334 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
336 struct libnet_JoinCtx *r = NULL;
339 const char *domain = lp_workgroup(); /* FIXME */
340 bool modify_config = lp_config_backend_is_registry();
341 enum netr_SchannelType sec_chan_type;
344 if (c->display_usage) {
347 " Join a domain the old way\n");
351 mem_ctx = talloc_init("net_rpc_oldjoin");
356 werr = libnet_init_JoinCtx(mem_ctx, &r);
357 if (!W_ERROR_IS_OK(werr)) {
362 check what type of join - if the user want's to join as
363 a BDC, the server must agree that we are a BDC.
366 sec_chan_type = get_sec_channel_type(argv[0]);
368 sec_chan_type = get_sec_channel_type(NULL);
372 d_fprintf(stderr, _("Could not initialise message context. "
373 "Try running as root\n"));
374 werr = WERR_ACCESS_DENIED;
378 pw = talloc_strndup(r, lp_netbios_name(), 14);
384 r->in.msg_ctx = c->msg_ctx;
385 r->in.domain_name = domain;
386 r->in.secure_channel_type = sec_chan_type;
387 r->in.dc_name = c->opt_host;
388 r->in.admin_account = "";
389 r->in.admin_password = strlower_talloc(r, pw);
390 if (r->in.admin_password == NULL) {
395 r->in.modify_config = modify_config;
396 r->in.join_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
397 WKSSVC_JOIN_FLAGS_JOIN_UNSECURE |
398 WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED;
400 werr = libnet_Join(mem_ctx, r);
401 if (!W_ERROR_IS_OK(werr)) {
405 /* Check the short name of the domain */
407 if (!modify_config && !strequal(lp_workgroup(), r->out.netbios_domain_name)) {
408 d_printf("The workgroup in %s does not match the short\n", get_dyn_CONFIGFILE());
409 d_printf("domain name obtained from the server.\n");
410 d_printf("Using the name [%s] from the server.\n", r->out.netbios_domain_name);
411 d_printf("You should set \"workgroup = %s\" in %s.\n",
412 r->out.netbios_domain_name, get_dyn_CONFIGFILE());
415 d_printf("Using short domain name -- %s\n", r->out.netbios_domain_name);
417 if (r->out.dns_domain_name) {
418 d_printf("Joined '%s' to realm '%s'\n", r->in.machine_name,
419 r->out.dns_domain_name);
421 d_printf("Joined '%s' to domain '%s'\n", r->in.machine_name,
422 r->out.netbios_domain_name);
425 TALLOC_FREE(mem_ctx);
430 /* issue an overall failure message at the end. */
431 d_fprintf(stderr, _("Failed to join domain: %s\n"),
432 r && r->out.error_string ? r->out.error_string :
433 get_friendly_werror_msg(werr));
435 TALLOC_FREE(mem_ctx);
441 * check that a join is OK
443 * @return A shell status integer (0 for success)
446 int net_rpc_testjoin(struct net_context *c, int argc, const char **argv)
450 const char *domain = c->opt_target_workgroup;
451 const char *dc = c->opt_host;
453 if (c->display_usage) {
456 " Test if a join is OK\n");
460 mem_ctx = talloc_init("net_rpc_testjoin");
466 struct netr_DsRGetDCNameInfo *info;
469 d_fprintf(stderr, _("Could not initialise message context. "
470 "Try running as root\n"));
471 talloc_destroy(mem_ctx);
475 status = dsgetdcname(mem_ctx,
482 if (!NT_STATUS_IS_OK(status)) {
483 talloc_destroy(mem_ctx);
487 dc = strip_hostname(info->dc_unc);
490 /* Display success or failure */
491 status = libnet_join_ok(c->opt_workgroup, lp_netbios_name(), dc,
493 if (!NT_STATUS_IS_OK(status)) {
494 fprintf(stderr,"Join to domain '%s' is not valid: %s\n",
495 domain, nt_errstr(status));
496 talloc_destroy(mem_ctx);
500 printf("Join to '%s' is OK\n",domain);
501 talloc_destroy(mem_ctx);
507 * 'net rpc join' entrypoint.
508 * @param argc Standard main() style argc.
509 * @param argv Standard main() style argv. Initial components are already
512 * Main 'net_rpc_join()' (where the admin username/password is used) is
514 * Try to just change the password, but if that doesn't work, use/prompt
515 * for a username/password.
518 int net_rpc_join(struct net_context *c, int argc, const char **argv)
520 if (c->display_usage) {
523 _("net rpc join -U <username>[%%password] <type>\n"
525 " username\tName of the admin user"
526 " password\tPassword of the admin user, will "
527 "prompt if not specified\n"
528 " type\tCan be one of the following:\n"
529 "\t\tMEMBER\tJoin as member server (default)\n"
530 "\t\tBDC\tJoin as BDC\n"
531 "\t\tPDC\tJoin as PDC\n"));
535 if (lp_server_role() == ROLE_STANDALONE) {
536 d_printf(_("cannot join as standalone machine\n"));
540 if (strlen(lp_netbios_name()) > 15) {
541 d_printf(_("Our netbios name can be at most 15 chars long, "
542 "\"%s\" is %u chars long\n"),
543 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
547 if ((net_rpc_oldjoin(c, argc, argv) == 0))
550 return net_rpc_join_newstyle(c, argc, argv);
554 * display info about a rpc domain
556 * All parameters are provided by the run_rpc_command function, except for
557 * argc, argv which are passed through.
559 * @param domain_sid The domain sid acquired from the remote server
560 * @param cli A cli_state connected to the server.
561 * @param mem_ctx Talloc context, destroyed on completion of the function.
562 * @param argc Standard main() style argc.
563 * @param argv Standard main() style argv. Initial components are already
566 * @return Normal NTSTATUS return.
569 NTSTATUS rpc_info_internals(struct net_context *c,
570 const struct dom_sid *domain_sid,
571 const char *domain_name,
572 struct cli_state *cli,
573 struct rpc_pipe_client *pipe_hnd,
578 struct policy_handle connect_pol, domain_pol;
579 NTSTATUS status, result;
580 union samr_DomainInfo *info = NULL;
582 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
584 sid_to_fstring(sid_str, domain_sid);
586 /* Get sam policy handle */
587 status = dcerpc_samr_Connect2(b, mem_ctx,
589 MAXIMUM_ALLOWED_ACCESS,
592 if (!NT_STATUS_IS_OK(status)) {
593 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
598 if (!NT_STATUS_IS_OK(result)) {
600 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
605 /* Get domain policy handle */
606 status = dcerpc_samr_OpenDomain(b, mem_ctx,
608 MAXIMUM_ALLOWED_ACCESS,
609 discard_const_p(struct dom_sid2, domain_sid),
612 if (!NT_STATUS_IS_OK(status)) {
613 d_fprintf(stderr, _("Could not open domain: %s\n"),
617 if (!NT_STATUS_IS_OK(result)) {
619 d_fprintf(stderr, _("Could not open domain: %s\n"),
624 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
629 if (!NT_STATUS_IS_OK(status)) {
633 if (NT_STATUS_IS_OK(result)) {
634 d_printf(_("Domain Name: %s\n"),
635 info->general.domain_name.string);
636 d_printf(_("Domain SID: %s\n"), sid_str);
637 d_printf(_("Sequence number: %llu\n"),
638 (unsigned long long)info->general.sequence_num);
639 d_printf(_("Num users: %u\n"), info->general.num_users);
640 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
641 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
649 * 'net rpc info' entrypoint.
650 * @param argc Standard main() style argc.
651 * @param argv Standard main() style argv. Initial components are already
655 int net_rpc_info(struct net_context *c, int argc, const char **argv)
657 if (c->display_usage) {
662 _("Display information about the domain"));
666 return run_rpc_command(c, NULL, &ndr_table_samr,
667 NET_FLAGS_PDC, rpc_info_internals,
672 * Fetch domain SID into the local secrets.tdb.
674 * All parameters are provided by the run_rpc_command function, except for
675 * argc, argv which are passed through.
677 * @param domain_sid The domain sid acquired from the remote server.
678 * @param cli A cli_state connected to the server.
679 * @param mem_ctx Talloc context, destroyed on completion of the function.
680 * @param argc Standard main() style argc.
681 * @param argv Standard main() style argv. Initial components are already
684 * @return Normal NTSTATUS return.
687 static NTSTATUS rpc_getsid_internals(struct net_context *c,
688 const struct dom_sid *domain_sid,
689 const char *domain_name,
690 struct cli_state *cli,
691 struct rpc_pipe_client *pipe_hnd,
698 sid_to_fstring(sid_str, domain_sid);
699 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
700 sid_str, domain_name);
702 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
703 DEBUG(0,("Can't store domain SID\n"));
704 return NT_STATUS_UNSUCCESSFUL;
711 * 'net rpc getsid' entrypoint.
712 * @param argc Standard main() style argc.
713 * @param argv Standard main() style argv. Initial components are already
717 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
719 int conn_flags = NET_FLAGS_PDC;
721 if (!c->opt_user_specified) {
722 conn_flags |= NET_FLAGS_ANONYMOUS;
725 if (c->display_usage) {
730 _("Fetch domain SID into local secrets.tdb"));
734 return run_rpc_command(c, NULL, &ndr_table_samr,
736 rpc_getsid_internals,
740 /****************************************************************************/
743 * Basic usage function for 'net rpc user'.
744 * @param argc Standard main() style argc.
745 * @param argv Standard main() style argv. Initial components are already
749 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
751 return net_user_usage(c, argc, argv);
755 * Add a new user to a remote RPC server.
757 * @param argc Standard main() style argc.
758 * @param argv Standard main() style argv. Initial components are already
761 * @return A shell status integer (0 for success).
764 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
766 NET_API_STATUS status;
767 struct USER_INFO_1 info1;
768 uint32_t parm_error = 0;
770 if (argc < 1 || c->display_usage) {
771 rpc_user_usage(c, argc, argv);
777 info1.usri1_name = argv[0];
779 info1.usri1_password = argv[1];
782 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
785 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
786 argv[0], libnetapi_get_error_string(c->netapi_ctx,
790 d_printf(_("Added user '%s'.\n"), argv[0]);
797 * Rename a user on a remote RPC server.
799 * @param argc Standard main() style argc.
800 * @param argv Standard main() style argv. Initial components are already
803 * @return A shell status integer (0 for success).
806 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
808 NET_API_STATUS status;
809 struct USER_INFO_0 u0;
810 uint32_t parm_err = 0;
812 if (argc != 2 || c->display_usage) {
813 rpc_user_usage(c, argc, argv);
817 u0.usri0_name = argv[1];
819 status = NetUserSetInfo(c->opt_host, argv[0],
820 0, (uint8_t *)&u0, &parm_err);
823 _("Failed to rename user from %s to %s - %s\n"),
825 libnetapi_get_error_string(c->netapi_ctx, status));
827 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
834 * Set a user's primary group
836 * @param argc Standard main() style argc.
837 * @param argv Standard main() style argv. Initial components are already
840 * @return A shell status integer (0 for success).
843 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
846 NET_API_STATUS status;
848 struct GROUP_INFO_2 *g2;
849 struct USER_INFO_1051 u1051;
850 uint32_t parm_err = 0;
852 if (argc != 2 || c->display_usage) {
853 rpc_user_usage(c, argc, argv);
857 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
859 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
861 libnetapi_get_error_string(c->netapi_ctx, status));
864 g2 = (struct GROUP_INFO_2 *)buffer;
866 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
868 NetApiBufferFree(buffer);
870 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
871 (uint8_t *)&u1051, &parm_err);
874 _("Failed to set user's primary group %s to %s - "
875 "%s\n"), argv[0], argv[1],
876 libnetapi_get_error_string(c->netapi_ctx, status));
878 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
885 * Delete a user from a remote RPC server.
887 * @param argc Standard main() style argc.
888 * @param argv Standard main() style argv. Initial components are already
891 * @return A shell status integer (0 for success).
894 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
896 NET_API_STATUS status;
898 if (argc < 1 || c->display_usage) {
899 rpc_user_usage(c, argc, argv);
903 status = NetUserDel(c->opt_host, argv[0]);
906 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
908 libnetapi_get_error_string(c->netapi_ctx, status));
911 d_printf(_("Deleted user '%s'.\n"), argv[0]);
918 * Set a user's password on a remote RPC server.
920 * @param argc Standard main() style argc.
921 * @param argv Standard main() style argv. Initial components are already
924 * @return A shell status integer (0 for success).
927 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
929 NET_API_STATUS status;
931 struct USER_INFO_1003 u1003;
932 uint32_t parm_err = 0;
935 if (argc < 1 || c->display_usage) {
936 rpc_user_usage(c, argc, argv);
941 u1003.usri1003_password = argv[1];
944 ret = asprintf(&prompt, _("Enter new password for %s:"),
950 ret = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
956 u1003.usri1003_password = talloc_strdup(c, pwd);
957 if (u1003.usri1003_password == NULL) {
962 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
964 /* Display results */
967 _("Failed to set password for '%s' with error: %s.\n"),
968 argv[0], libnetapi_get_error_string(c->netapi_ctx,
977 * List a user's groups from a remote RPC server.
979 * @param argc Standard main() style argc.
980 * @param argv Standard main() style argv. Initial components are already
983 * @return A shell status integer (0 for success)
986 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
989 NET_API_STATUS status;
990 struct GROUP_USERS_INFO_0 *u0 = NULL;
991 uint32_t entries_read = 0;
992 uint32_t total_entries = 0;
996 if (argc < 1 || c->display_usage) {
997 rpc_user_usage(c, argc, argv);
1001 status = NetUserGetGroups(c->opt_host,
1004 (uint8_t **)(void *)&u0,
1010 _("Failed to get groups for '%s' with error: %s.\n"),
1011 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1016 for (i=0; i < entries_read; i++) {
1017 printf("%s\n", u0->grui0_name);
1025 * List users on a remote RPC server.
1027 * All parameters are provided by the run_rpc_command function, except for
1028 * argc, argv which are passed through.
1030 * @param domain_sid The domain sid acquired from the remote server.
1031 * @param cli A cli_state connected to the server.
1032 * @param mem_ctx Talloc context, destroyed on completion of the function.
1033 * @param argc Standard main() style argc.
1034 * @param argv Standard main() style argv. Initial components are already
1037 * @return Normal NTSTATUS return.
1040 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
1042 NET_API_STATUS status;
1043 uint32_t start_idx=0, num_entries, i, loop_count = 0;
1044 struct NET_DISPLAY_USER *info = NULL;
1045 void *buffer = NULL;
1047 /* Query domain users */
1048 if (c->opt_long_list_entries)
1049 d_printf(_("\nUser name Comment"
1050 "\n-----------------------------\n"));
1052 uint32_t max_entries, max_size;
1054 dcerpc_get_query_dispinfo_params(
1055 loop_count, &max_entries, &max_size);
1057 status = NetQueryDisplayInformation(c->opt_host,
1064 if (status != 0 && status != ERROR_MORE_DATA) {
1068 info = (struct NET_DISPLAY_USER *)buffer;
1070 for (i = 0; i < num_entries; i++) {
1072 if (c->opt_long_list_entries)
1073 printf("%-21.21s %s\n", info->usri1_name,
1074 info->usri1_comment);
1076 printf("%s\n", info->usri1_name);
1080 NetApiBufferFree(buffer);
1083 start_idx += num_entries;
1085 } while (status == ERROR_MORE_DATA);
1091 * 'net rpc user' entrypoint.
1092 * @param argc Standard main() style argc.
1093 * @param argv Standard main() style argv. Initial components are already
1097 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1099 NET_API_STATUS status;
1101 struct functable func[] = {
1106 N_("Add specified user"),
1107 N_("net rpc user add\n"
1108 " Add specified user")
1114 N_("List domain groups of user"),
1115 N_("net rpc user info\n"
1116 " List domain groups of user")
1122 N_("Remove specified user"),
1123 N_("net rpc user delete\n"
1124 " Remove specified user")
1130 N_("Change user password"),
1131 N_("net rpc user password\n"
1132 " Change user password")
1138 N_("Rename specified user"),
1139 N_("net rpc user rename\n"
1140 " Rename specified user")
1144 rpc_user_setprimarygroup,
1146 "Set a user's primary group",
1147 "net rpc user setprimarygroup\n"
1148 " Set a user's primary group"
1150 {NULL, NULL, 0, NULL, NULL}
1153 status = libnetapi_net_init(&c->netapi_ctx);
1157 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1158 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1159 if (c->opt_kerberos) {
1160 libnetapi_set_use_kerberos(c->netapi_ctx);
1164 if (c->display_usage) {
1169 _("List all users"));
1170 net_display_usage_from_functable(func);
1174 return rpc_user_list(c, argc, argv);
1177 return net_run_function(c, argc, argv, "net rpc user", func);
1180 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1181 TALLOC_CTX *mem_ctx,
1182 struct rpc_sh_ctx *ctx,
1183 struct rpc_pipe_client *pipe_hnd,
1184 int argc, const char **argv)
1186 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1189 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1190 TALLOC_CTX *mem_ctx,
1191 struct rpc_sh_ctx *ctx,
1192 struct rpc_pipe_client *pipe_hnd,
1193 int argc, const char **argv)
1195 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1198 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1199 TALLOC_CTX *mem_ctx,
1200 struct rpc_sh_ctx *ctx,
1201 struct rpc_pipe_client *pipe_hnd,
1202 int argc, const char **argv,
1204 struct net_context *c,
1205 TALLOC_CTX *mem_ctx,
1206 struct rpc_sh_ctx *ctx,
1207 struct rpc_pipe_client *pipe_hnd,
1208 struct policy_handle *user_hnd,
1209 int argc, const char **argv))
1211 struct policy_handle connect_pol, domain_pol, user_pol;
1212 NTSTATUS status, result;
1215 enum lsa_SidType type;
1216 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1219 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1221 return NT_STATUS_INVALID_PARAMETER;
1224 ZERO_STRUCT(connect_pol);
1225 ZERO_STRUCT(domain_pol);
1226 ZERO_STRUCT(user_pol);
1228 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1229 argv[0], NULL, NULL, &sid, &type);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1236 if (type != SID_NAME_USER) {
1237 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1238 sid_type_lookup(type));
1239 status = NT_STATUS_NO_SUCH_USER;
1243 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1244 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1245 status = NT_STATUS_NO_SUCH_USER;
1249 status = dcerpc_samr_Connect2(b, mem_ctx,
1251 MAXIMUM_ALLOWED_ACCESS,
1254 if (!NT_STATUS_IS_OK(status)) {
1257 if (!NT_STATUS_IS_OK(result)) {
1262 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1264 MAXIMUM_ALLOWED_ACCESS,
1268 if (!NT_STATUS_IS_OK(status)) {
1271 if (!NT_STATUS_IS_OK(result)) {
1276 status = dcerpc_samr_OpenUser(b, mem_ctx,
1278 MAXIMUM_ALLOWED_ACCESS,
1282 if (!NT_STATUS_IS_OK(status)) {
1285 if (!NT_STATUS_IS_OK(result)) {
1290 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1293 if (is_valid_policy_hnd(&user_pol)) {
1294 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1296 if (is_valid_policy_hnd(&domain_pol)) {
1297 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1299 if (is_valid_policy_hnd(&connect_pol)) {
1300 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1305 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1306 TALLOC_CTX *mem_ctx,
1307 struct rpc_sh_ctx *ctx,
1308 struct rpc_pipe_client *pipe_hnd,
1309 struct policy_handle *user_hnd,
1310 int argc, const char **argv)
1312 NTSTATUS status, result;
1313 union samr_UserInfo *info = NULL;
1314 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1317 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1319 return NT_STATUS_INVALID_PARAMETER;
1322 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1327 if (!NT_STATUS_IS_OK(status)) {
1330 if (!NT_STATUS_IS_OK(result)) {
1334 d_printf(_("user rid: %d, group rid: %d\n"),
1336 info->info21.primary_gid);
1341 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1342 TALLOC_CTX *mem_ctx,
1343 struct rpc_sh_ctx *ctx,
1344 struct rpc_pipe_client *pipe_hnd,
1345 int argc, const char **argv)
1347 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1348 rpc_sh_user_show_internals);
1351 #define FETCHSTR(name, rec) \
1352 do { if (strequal(ctx->thiscmd, name)) { \
1353 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1356 #define SETSTR(name, rec, flag) \
1357 do { if (strequal(ctx->thiscmd, name)) { \
1358 init_lsa_String(&(info->info21.rec), argv[0]); \
1359 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1362 static NTSTATUS rpc_sh_user_str_edit_internals(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 NTSTATUS status, result;
1370 const char *username;
1371 const char *oldval = "";
1372 union samr_UserInfo *info = NULL;
1373 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1376 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1377 _("Usage:"), ctx->whoami);
1378 return NT_STATUS_INVALID_PARAMETER;
1381 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1386 if (!NT_STATUS_IS_OK(status)) {
1389 if (!NT_STATUS_IS_OK(result)) {
1393 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1395 FETCHSTR("fullname", full_name);
1396 FETCHSTR("homedir", home_directory);
1397 FETCHSTR("homedrive", home_drive);
1398 FETCHSTR("logonscript", logon_script);
1399 FETCHSTR("profilepath", profile_path);
1400 FETCHSTR("description", description);
1403 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1407 if (strcmp(argv[0], "NULL") == 0) {
1411 ZERO_STRUCT(info->info21);
1413 SETSTR("fullname", full_name, FULL_NAME);
1414 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1415 SETSTR("homedrive", home_drive, HOME_DRIVE);
1416 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1417 SETSTR("profilepath", profile_path, PROFILE_PATH);
1418 SETSTR("description", description, DESCRIPTION);
1420 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1425 if (!NT_STATUS_IS_OK(status)) {
1431 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1432 ctx->thiscmd, oldval, argv[0]);
1439 #define HANDLEFLG(name, rec) \
1440 do { if (strequal(ctx->thiscmd, name)) { \
1441 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1443 newflags = oldflags | ACB_##rec; \
1445 newflags = oldflags & ~ACB_##rec; \
1448 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1449 TALLOC_CTX *mem_ctx,
1450 struct rpc_sh_ctx *ctx,
1451 struct rpc_pipe_client *pipe_hnd,
1452 int argc, const char **argv)
1454 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1455 rpc_sh_user_str_edit_internals);
1458 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1459 TALLOC_CTX *mem_ctx,
1460 struct rpc_sh_ctx *ctx,
1461 struct rpc_pipe_client *pipe_hnd,
1462 struct policy_handle *user_hnd,
1463 int argc, const char **argv)
1465 NTSTATUS status, result;
1466 const char *username;
1467 const char *oldval = "unknown";
1468 uint32 oldflags, newflags;
1470 union samr_UserInfo *info = NULL;
1471 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1474 ((argc == 1) && !strequal(argv[0], "yes") &&
1475 !strequal(argv[0], "no"))) {
1476 /* TRANSATORS: The yes|no here are program keywords. Please do
1478 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1480 return NT_STATUS_INVALID_PARAMETER;
1483 newval = strequal(argv[0], "yes");
1485 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1490 if (!NT_STATUS_IS_OK(status)) {
1493 if (!NT_STATUS_IS_OK(result)) {
1497 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1498 oldflags = info->info21.acct_flags;
1499 newflags = info->info21.acct_flags;
1501 HANDLEFLG("disabled", DISABLED);
1502 HANDLEFLG("pwnotreq", PWNOTREQ);
1503 HANDLEFLG("autolock", AUTOLOCK);
1504 HANDLEFLG("pwnoexp", PWNOEXP);
1507 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1512 ZERO_STRUCT(info->info21);
1514 info->info21.acct_flags = newflags;
1515 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1517 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1522 if (!NT_STATUS_IS_OK(status)) {
1526 if (NT_STATUS_IS_OK(result)) {
1527 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1528 ctx->thiscmd, oldval, argv[0]);
1536 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1537 TALLOC_CTX *mem_ctx,
1538 struct rpc_sh_ctx *ctx,
1539 struct rpc_pipe_client *pipe_hnd,
1540 int argc, const char **argv)
1542 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1543 rpc_sh_user_flag_edit_internals);
1546 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1547 TALLOC_CTX *mem_ctx,
1548 struct rpc_sh_ctx *ctx)
1550 static struct rpc_sh_cmd cmds[] = {
1552 { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1553 N_("Show/Set a user's full name") },
1555 { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1556 N_("Show/Set a user's home directory") },
1558 { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1559 N_("Show/Set a user's home drive") },
1561 { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1562 N_("Show/Set a user's logon script") },
1564 { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1565 N_("Show/Set a user's profile path") },
1567 { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1568 N_("Show/Set a user's description") },
1570 { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1571 N_("Show/Set whether a user is disabled") },
1573 { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1574 N_("Show/Set whether a user locked out") },
1576 { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1577 N_("Show/Set whether a user does not need a password") },
1579 { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1580 N_("Show/Set whether a user's password does not expire") },
1582 { NULL, NULL, 0, NULL, NULL }
1588 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1589 TALLOC_CTX *mem_ctx,
1590 struct rpc_sh_ctx *ctx)
1592 static struct rpc_sh_cmd cmds[] = {
1594 { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
1595 N_("List available users") },
1597 { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
1598 N_("List the domain groups a user is member of") },
1600 { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
1601 N_("Show info about a user") },
1603 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1604 N_("Show/Modify a user's fields") },
1606 { NULL, NULL, 0, NULL, NULL }
1612 /****************************************************************************/
1615 * Basic usage function for 'net rpc group'.
1616 * @param argc Standard main() style argc.
1617 * @param argv Standard main() style argv. Initial components are already
1621 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1623 return net_group_usage(c, argc, argv);
1627 * Delete group on a remote RPC server.
1629 * All parameters are provided by the run_rpc_command function, except for
1630 * argc, argv which are passed through.
1632 * @param domain_sid The domain sid acquired from the remote server.
1633 * @param cli A cli_state connected to the server.
1634 * @param mem_ctx Talloc context, destroyed on completion of the function.
1635 * @param argc Standard main() style argc.
1636 * @param argv Standard main() style argv. Initial components are already
1639 * @return Normal NTSTATUS return.
1642 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1643 const struct dom_sid *domain_sid,
1644 const char *domain_name,
1645 struct cli_state *cli,
1646 struct rpc_pipe_client *pipe_hnd,
1647 TALLOC_CTX *mem_ctx,
1651 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1652 bool group_is_primary = false;
1653 NTSTATUS status, result;
1655 struct samr_RidAttrArray *rids = NULL;
1658 /* struct samr_RidWithAttribute *user_gids; */
1659 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1661 struct samr_Ids group_rids, name_types;
1662 struct lsa_String lsa_acct_name;
1663 union samr_UserInfo *info = NULL;
1665 if (argc < 1 || c->display_usage) {
1666 rpc_group_usage(c, argc,argv);
1667 return NT_STATUS_OK; /* ok? */
1670 status = dcerpc_samr_Connect2(b, mem_ctx,
1672 MAXIMUM_ALLOWED_ACCESS,
1675 if (!NT_STATUS_IS_OK(status)) {
1676 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1680 if (!NT_STATUS_IS_OK(result)) {
1682 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1686 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1688 MAXIMUM_ALLOWED_ACCESS,
1689 discard_const_p(struct dom_sid2, domain_sid),
1692 if (!NT_STATUS_IS_OK(status)) {
1693 d_fprintf(stderr, _("Request open_domain failed\n"));
1697 if (!NT_STATUS_IS_OK(result)) {
1699 d_fprintf(stderr, _("Request open_domain failed\n"));
1703 init_lsa_String(&lsa_acct_name, argv[0]);
1705 status = dcerpc_samr_LookupNames(b, mem_ctx,
1712 if (!NT_STATUS_IS_OK(status)) {
1713 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1717 if (!NT_STATUS_IS_OK(result)) {
1719 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1723 switch (name_types.ids[0])
1725 case SID_NAME_DOM_GRP:
1726 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1728 MAXIMUM_ALLOWED_ACCESS,
1732 if (!NT_STATUS_IS_OK(status)) {
1733 d_fprintf(stderr, _("Request open_group failed"));
1737 if (!NT_STATUS_IS_OK(result)) {
1739 d_fprintf(stderr, _("Request open_group failed"));
1743 group_rid = group_rids.ids[0];
1745 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1749 if (!NT_STATUS_IS_OK(status)) {
1751 _("Unable to query group members of %s"),
1756 if (!NT_STATUS_IS_OK(result)) {
1759 _("Unable to query group members of %s"),
1764 if (c->opt_verbose) {
1766 _("Domain Group %s (rid: %d) has %d members\n"),
1767 argv[0],group_rid, rids->count);
1770 /* Check if group is anyone's primary group */
1771 for (i = 0; i < rids->count; i++)
1773 status = dcerpc_samr_OpenUser(b, mem_ctx,
1775 MAXIMUM_ALLOWED_ACCESS,
1779 if (!NT_STATUS_IS_OK(status)) {
1781 _("Unable to open group member %d\n"),
1786 if (!NT_STATUS_IS_OK(result)) {
1789 _("Unable to open group member %d\n"),
1794 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1799 if (!NT_STATUS_IS_OK(status)) {
1801 _("Unable to lookup userinfo for group "
1807 if (!NT_STATUS_IS_OK(result)) {
1810 _("Unable to lookup userinfo for group "
1816 if (info->info21.primary_gid == group_rid) {
1817 if (c->opt_verbose) {
1818 d_printf(_("Group is primary group "
1820 info->info21.account_name.string);
1822 group_is_primary = true;
1825 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1828 if (group_is_primary) {
1829 d_fprintf(stderr, _("Unable to delete group because "
1830 "some of it's members have it as primary "
1832 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1836 /* remove all group members */
1837 for (i = 0; i < rids->count; i++)
1840 d_printf(_("Remove group member %d..."),
1842 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1846 if (!NT_STATUS_IS_OK(status)) {
1850 if (NT_STATUS_IS_OK(result)) {
1852 d_printf(_("ok\n"));
1855 d_printf("%s\n", _("failed"));
1860 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1863 if (!NT_STATUS_IS_OK(status)) {
1870 /* removing a local group is easier... */
1871 case SID_NAME_ALIAS:
1872 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1874 MAXIMUM_ALLOWED_ACCESS,
1878 if (!NT_STATUS_IS_OK(status)) {
1879 d_fprintf(stderr, _("Request open_alias failed\n"));
1882 if (!NT_STATUS_IS_OK(result)) {
1884 d_fprintf(stderr, _("Request open_alias failed\n"));
1888 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1891 if (!NT_STATUS_IS_OK(status)) {
1899 d_fprintf(stderr, _("%s is of type %s. This command is only "
1900 "for deleting local or global groups\n"),
1901 argv[0],sid_type_lookup(name_types.ids[0]));
1902 status = NT_STATUS_UNSUCCESSFUL;
1906 if (NT_STATUS_IS_OK(status)) {
1908 d_printf(_("Deleted %s '%s'\n"),
1909 sid_type_lookup(name_types.ids[0]), argv[0]);
1911 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1912 get_friendly_nt_error_msg(status));
1920 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1922 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
1923 rpc_group_delete_internals, argc,argv);
1926 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1928 NET_API_STATUS status;
1929 struct GROUP_INFO_1 info1;
1930 uint32_t parm_error = 0;
1932 if (argc != 1 || c->display_usage) {
1933 rpc_group_usage(c, argc, argv);
1939 info1.grpi1_name = argv[0];
1940 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1941 info1.grpi1_comment = c->opt_comment;
1944 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1948 _("Failed to add group '%s' with error: %s.\n"),
1949 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1953 d_printf(_("Added group '%s'.\n"), argv[0]);
1959 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1961 NET_API_STATUS status;
1962 struct LOCALGROUP_INFO_1 info1;
1963 uint32_t parm_error = 0;
1965 if (argc != 1 || c->display_usage) {
1966 rpc_group_usage(c, argc, argv);
1972 info1.lgrpi1_name = argv[0];
1973 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1974 info1.lgrpi1_comment = c->opt_comment;
1977 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1981 _("Failed to add alias '%s' with error: %s.\n"),
1982 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1986 d_printf(_("Added alias '%s'.\n"), argv[0]);
1992 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1994 if (c->opt_localgroup)
1995 return rpc_alias_add_internals(c, argc, argv);
1997 return rpc_group_add_internals(c, argc, argv);
2000 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2001 TALLOC_CTX *mem_ctx,
2003 struct dom_sid *sid,
2004 enum lsa_SidType *type)
2006 struct dom_sid *sids = NULL;
2007 enum lsa_SidType *types = NULL;
2008 struct rpc_pipe_client *pipe_hnd = NULL;
2009 struct policy_handle lsa_pol;
2010 NTSTATUS status, result;
2011 struct dcerpc_binding_handle *b;
2013 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
2015 if (!NT_STATUS_IS_OK(status)) {
2019 b = pipe_hnd->binding_handle;
2021 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2022 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2024 if (!NT_STATUS_IS_OK(status)) {
2028 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2029 &name, NULL, 1, &sids, &types);
2031 if (NT_STATUS_IS_OK(status)) {
2032 sid_copy(sid, &sids[0]);
2036 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
2040 TALLOC_FREE(pipe_hnd);
2043 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
2045 /* Try as S-1-5-whatever */
2047 struct dom_sid tmp_sid;
2049 if (string_to_sid(&tmp_sid, name)) {
2050 sid_copy(sid, &tmp_sid);
2051 *type = SID_NAME_UNKNOWN;
2052 status = NT_STATUS_OK;
2059 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2060 TALLOC_CTX *mem_ctx,
2061 const struct dom_sid *group_sid,
2064 struct policy_handle connect_pol, domain_pol;
2065 NTSTATUS status, result;
2067 struct policy_handle group_pol;
2068 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2070 struct samr_Ids rids, rid_types;
2071 struct lsa_String lsa_acct_name;
2075 sid_copy(&sid, group_sid);
2077 if (!sid_split_rid(&sid, &group_rid)) {
2078 return NT_STATUS_UNSUCCESSFUL;
2081 /* Get sam policy handle */
2082 status = dcerpc_samr_Connect2(b, mem_ctx,
2084 MAXIMUM_ALLOWED_ACCESS,
2087 if (!NT_STATUS_IS_OK(status)) {
2090 if (!NT_STATUS_IS_OK(result)) {
2094 /* Get domain policy handle */
2095 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2097 MAXIMUM_ALLOWED_ACCESS,
2101 if (!NT_STATUS_IS_OK(status)) {
2104 if (!NT_STATUS_IS_OK(result)) {
2108 init_lsa_String(&lsa_acct_name, member);
2110 status = dcerpc_samr_LookupNames(b, mem_ctx,
2117 if (!NT_STATUS_IS_OK(status)) {
2118 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2123 if (!NT_STATUS_IS_OK(result)) {
2125 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2130 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2132 MAXIMUM_ALLOWED_ACCESS,
2136 if (!NT_STATUS_IS_OK(status)) {
2140 if (!NT_STATUS_IS_OK(result)) {
2145 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2148 0x0005, /* unknown flags */
2150 if (!NT_STATUS_IS_OK(status)) {
2157 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2161 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2162 TALLOC_CTX *mem_ctx,
2163 const struct dom_sid *alias_sid,
2166 struct policy_handle connect_pol, domain_pol;
2167 NTSTATUS status, result;
2169 struct policy_handle alias_pol;
2170 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2172 struct dom_sid member_sid;
2173 enum lsa_SidType member_type;
2177 sid_copy(&sid, alias_sid);
2179 if (!sid_split_rid(&sid, &alias_rid)) {
2180 return NT_STATUS_UNSUCCESSFUL;
2183 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2184 member, &member_sid, &member_type);
2186 if (!NT_STATUS_IS_OK(result)) {
2187 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2192 /* Get sam policy handle */
2193 status = dcerpc_samr_Connect2(b, mem_ctx,
2195 MAXIMUM_ALLOWED_ACCESS,
2198 if (!NT_STATUS_IS_OK(status)) {
2201 if (!NT_STATUS_IS_OK(result)) {
2206 /* Get domain policy handle */
2207 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2209 MAXIMUM_ALLOWED_ACCESS,
2213 if (!NT_STATUS_IS_OK(status)) {
2216 if (!NT_STATUS_IS_OK(result)) {
2221 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2223 MAXIMUM_ALLOWED_ACCESS,
2227 if (!NT_STATUS_IS_OK(status)) {
2230 if (!NT_STATUS_IS_OK(result)) {
2234 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2238 if (!NT_STATUS_IS_OK(status)) {
2245 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2249 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2250 const struct dom_sid *domain_sid,
2251 const char *domain_name,
2252 struct cli_state *cli,
2253 struct rpc_pipe_client *pipe_hnd,
2254 TALLOC_CTX *mem_ctx,
2258 struct dom_sid group_sid;
2259 enum lsa_SidType group_type;
2261 if (argc != 2 || c->display_usage) {
2264 _("net rpc group addmem <group> <member>\n"
2265 " Add a member to a group\n"
2266 " group\tGroup to add member to\n"
2267 " member\tMember to add to group\n"));
2268 return NT_STATUS_UNSUCCESSFUL;
2271 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2272 &group_sid, &group_type))) {
2273 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2275 return NT_STATUS_UNSUCCESSFUL;
2278 if (group_type == SID_NAME_DOM_GRP) {
2279 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2280 &group_sid, argv[1]);
2282 if (!NT_STATUS_IS_OK(result)) {
2283 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2284 argv[1], argv[0], nt_errstr(result));
2289 if (group_type == SID_NAME_ALIAS) {
2290 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2291 &group_sid, argv[1]);
2293 if (!NT_STATUS_IS_OK(result)) {
2294 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2295 argv[1], argv[0], nt_errstr(result));
2300 d_fprintf(stderr, _("Can only add members to global or local groups "
2301 "which %s is not\n"), argv[0]);
2303 return NT_STATUS_UNSUCCESSFUL;
2306 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2308 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2309 rpc_group_addmem_internals,
2313 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2314 struct rpc_pipe_client *pipe_hnd,
2315 TALLOC_CTX *mem_ctx,
2316 const struct dom_sid *group_sid,
2319 struct policy_handle connect_pol, domain_pol;
2320 NTSTATUS status, result;
2322 struct policy_handle group_pol;
2323 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2325 struct samr_Ids rids, rid_types;
2326 struct lsa_String lsa_acct_name;
2330 sid_copy(&sid, group_sid);
2332 if (!sid_split_rid(&sid, &group_rid))
2333 return NT_STATUS_UNSUCCESSFUL;
2335 /* Get sam policy handle */
2336 status = dcerpc_samr_Connect2(b, mem_ctx,
2338 MAXIMUM_ALLOWED_ACCESS,
2341 if (!NT_STATUS_IS_OK(status)) {
2344 if (!NT_STATUS_IS_OK(result)) {
2349 /* Get domain policy handle */
2350 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2352 MAXIMUM_ALLOWED_ACCESS,
2356 if (!NT_STATUS_IS_OK(status)) {
2359 if (!NT_STATUS_IS_OK(result)) {
2363 init_lsa_String(&lsa_acct_name, member);
2365 status = dcerpc_samr_LookupNames(b, mem_ctx,
2372 if (!NT_STATUS_IS_OK(status)) {
2373 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2378 if (!NT_STATUS_IS_OK(result)) {
2380 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2385 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2387 MAXIMUM_ALLOWED_ACCESS,
2391 if (!NT_STATUS_IS_OK(status)) {
2394 if (!NT_STATUS_IS_OK(result)) {
2399 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2403 if (!NT_STATUS_IS_OK(status)) {
2409 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2413 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2414 TALLOC_CTX *mem_ctx,
2415 const struct dom_sid *alias_sid,
2418 struct policy_handle connect_pol, domain_pol;
2419 NTSTATUS status, result;
2421 struct policy_handle alias_pol;
2422 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2424 struct dom_sid member_sid;
2425 enum lsa_SidType member_type;
2429 sid_copy(&sid, alias_sid);
2431 if (!sid_split_rid(&sid, &alias_rid))
2432 return NT_STATUS_UNSUCCESSFUL;
2434 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2435 member, &member_sid, &member_type);
2437 if (!NT_STATUS_IS_OK(result)) {
2438 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2443 /* Get sam policy handle */
2444 status = dcerpc_samr_Connect2(b, mem_ctx,
2446 MAXIMUM_ALLOWED_ACCESS,
2449 if (!NT_STATUS_IS_OK(status)) {
2452 if (!NT_STATUS_IS_OK(result)) {
2457 /* Get domain policy handle */
2458 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2460 MAXIMUM_ALLOWED_ACCESS,
2464 if (!NT_STATUS_IS_OK(status)) {
2467 if (!NT_STATUS_IS_OK(result)) {
2472 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2474 MAXIMUM_ALLOWED_ACCESS,
2478 if (!NT_STATUS_IS_OK(status)) {
2482 if (!NT_STATUS_IS_OK(result)) {
2486 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2491 if (!NT_STATUS_IS_OK(status)) {
2498 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2502 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2503 const struct dom_sid *domain_sid,
2504 const char *domain_name,
2505 struct cli_state *cli,
2506 struct rpc_pipe_client *pipe_hnd,
2507 TALLOC_CTX *mem_ctx,
2511 struct dom_sid group_sid;
2512 enum lsa_SidType group_type;
2514 if (argc != 2 || c->display_usage) {
2517 _("net rpc group delmem <group> <member>\n"
2518 " Delete a member from a group\n"
2519 " group\tGroup to delete member from\n"
2520 " member\tMember to delete from group\n"));
2521 return NT_STATUS_UNSUCCESSFUL;
2524 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2525 &group_sid, &group_type))) {
2526 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2528 return NT_STATUS_UNSUCCESSFUL;
2531 if (group_type == SID_NAME_DOM_GRP) {
2532 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2533 &group_sid, argv[1]);
2535 if (!NT_STATUS_IS_OK(result)) {
2536 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2537 argv[1], argv[0], nt_errstr(result));
2542 if (group_type == SID_NAME_ALIAS) {
2543 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2544 &group_sid, argv[1]);
2546 if (!NT_STATUS_IS_OK(result)) {
2547 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2548 argv[1], argv[0], nt_errstr(result));
2553 d_fprintf(stderr, _("Can only delete members from global or local "
2554 "groups which %s is not\n"), argv[0]);
2556 return NT_STATUS_UNSUCCESSFUL;
2559 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2561 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2562 rpc_group_delmem_internals,
2567 * List groups on a remote RPC server.
2569 * All parameters are provided by the run_rpc_command function, except for
2570 * argc, argv which are passes through.
2572 * @param domain_sid The domain sid acquired from the remote server.
2573 * @param cli A cli_state connected to the server.
2574 * @param mem_ctx Talloc context, destroyed on completion of the function.
2575 * @param argc Standard main() style argc.
2576 * @param argv Standard main() style argv. Initial components are already
2579 * @return Normal NTSTATUS return.
2582 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2583 const struct dom_sid *domain_sid,
2584 const char *domain_name,
2585 struct cli_state *cli,
2586 struct rpc_pipe_client *pipe_hnd,
2587 TALLOC_CTX *mem_ctx,
2591 struct policy_handle connect_pol, domain_pol;
2592 NTSTATUS status, result;
2593 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2594 struct samr_SamArray *groups = NULL;
2595 bool global = false;
2597 bool builtin = false;
2598 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2600 if (c->display_usage) {
2603 _("net rpc group list [global] [local] [builtin]\n"
2604 " List groups on RPC server\n"
2605 " global\tList global groups\n"
2606 " local\tList local groups\n"
2607 " builtin\tList builtin groups\n"
2608 " If none of global, local or builtin is "
2609 "specified, all three options are considered "
2611 return NT_STATUS_OK;
2620 for (i=0; i<argc; i++) {
2621 if (strequal(argv[i], "global"))
2624 if (strequal(argv[i], "local"))
2627 if (strequal(argv[i], "builtin"))
2631 /* Get sam policy handle */
2633 status = dcerpc_samr_Connect2(b, mem_ctx,
2635 MAXIMUM_ALLOWED_ACCESS,
2638 if (!NT_STATUS_IS_OK(status)) {
2641 if (!NT_STATUS_IS_OK(result)) {
2646 /* Get domain policy handle */
2648 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2650 MAXIMUM_ALLOWED_ACCESS,
2651 discard_const_p(struct dom_sid2, domain_sid),
2654 if (!NT_STATUS_IS_OK(status)) {
2657 if (!NT_STATUS_IS_OK(result)) {
2662 /* Query domain groups */
2663 if (c->opt_long_list_entries)
2664 d_printf(_("\nGroup name Comment"
2665 "\n-----------------------------\n"));
2667 uint32_t max_size, total_size, returned_size;
2668 union samr_DispInfo info;
2672 dcerpc_get_query_dispinfo_params(
2673 loop_count, &max_entries, &max_size);
2675 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2685 if (!NT_STATUS_IS_OK(status)) {
2688 num_entries = info.info3.count;
2689 start_idx += info.info3.count;
2691 if (!NT_STATUS_IS_OK(result) &&
2692 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2695 for (i = 0; i < num_entries; i++) {
2697 const char *group = NULL;
2698 const char *desc = NULL;
2700 group = info.info3.entries[i].account_name.string;
2701 desc = info.info3.entries[i].description.string;
2703 if (c->opt_long_list_entries)
2704 printf("%-21.21s %-50.50s\n",
2707 printf("%s\n", group);
2709 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2710 /* query domain aliases */
2715 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2722 if (!NT_STATUS_IS_OK(status)) {
2725 if (!NT_STATUS_IS_OK(result) &&
2726 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2729 for (i = 0; i < num_entries; i++) {
2731 const char *description = NULL;
2733 if (c->opt_long_list_entries) {
2735 struct policy_handle alias_pol;
2736 union samr_AliasInfo *info = NULL;
2739 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2742 groups->entries[i].idx,
2745 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2746 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2751 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2752 status = dcerpc_samr_Close(b, mem_ctx,
2755 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2756 description = info->description.string;
2762 if (description != NULL) {
2763 printf("%-21.21s %-50.50s\n",
2764 groups->entries[i].name.string,
2767 printf("%s\n", groups->entries[i].name.string);
2770 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2771 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2772 /* Get builtin policy handle */
2774 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2776 MAXIMUM_ALLOWED_ACCESS,
2777 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2780 if (!NT_STATUS_IS_OK(status)) {
2783 if (!NT_STATUS_IS_OK(result)) {
2788 /* query builtin aliases */
2791 if (!builtin) break;
2793 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2800 if (!NT_STATUS_IS_OK(status)) {
2803 if (!NT_STATUS_IS_OK(result) &&
2804 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2809 for (i = 0; i < num_entries; i++) {
2811 const char *description = NULL;
2813 if (c->opt_long_list_entries) {
2815 struct policy_handle alias_pol;
2816 union samr_AliasInfo *info = NULL;
2819 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2822 groups->entries[i].idx,
2825 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2826 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2831 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2832 status = dcerpc_samr_Close(b, mem_ctx,
2835 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2836 description = info->description.string;
2842 if (description != NULL) {
2843 printf("%-21.21s %-50.50s\n",
2844 groups->entries[i].name.string,
2847 printf("%s\n", groups->entries[i].name.string);
2850 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2858 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2860 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2861 rpc_group_list_internals,
2865 static NTSTATUS rpc_list_group_members(struct net_context *c,
2866 struct rpc_pipe_client *pipe_hnd,
2867 TALLOC_CTX *mem_ctx,
2868 const char *domain_name,
2869 const struct dom_sid *domain_sid,
2870 struct policy_handle *domain_pol,
2873 NTSTATUS result, status;
2874 struct policy_handle group_pol;
2875 uint32 num_members, *group_rids;
2877 struct samr_RidAttrArray *rids = NULL;
2878 struct lsa_Strings names;
2879 struct samr_Ids types;
2880 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2883 sid_to_fstring(sid_str, domain_sid);
2885 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2887 MAXIMUM_ALLOWED_ACCESS,
2891 if (!NT_STATUS_IS_OK(status)) {
2894 if (!NT_STATUS_IS_OK(result)) {
2898 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
2902 if (!NT_STATUS_IS_OK(status)) {
2905 if (!NT_STATUS_IS_OK(result)) {
2909 num_members = rids->count;
2910 group_rids = rids->rids;
2912 while (num_members > 0) {
2913 int this_time = 512;
2915 if (num_members < this_time)
2916 this_time = num_members;
2918 status = dcerpc_samr_LookupRids(b, mem_ctx,
2925 if (!NT_STATUS_IS_OK(status)) {
2928 if (!NT_STATUS_IS_OK(result)) {
2932 /* We only have users as members, but make the output
2933 the same as the output of alias members */
2935 for (i = 0; i < this_time; i++) {
2937 if (c->opt_long_list_entries) {
2938 printf("%s-%d %s\\%s %d\n", sid_str,
2939 group_rids[i], domain_name,
2940 names.names[i].string,
2943 printf("%s\\%s\n", domain_name,
2944 names.names[i].string);
2948 num_members -= this_time;
2952 return NT_STATUS_OK;
2955 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2956 struct rpc_pipe_client *pipe_hnd,
2957 TALLOC_CTX *mem_ctx,
2958 struct policy_handle *domain_pol,
2961 NTSTATUS result, status;
2962 struct rpc_pipe_client *lsa_pipe;
2963 struct policy_handle alias_pol, lsa_pol;
2965 struct dom_sid *alias_sids;
2968 enum lsa_SidType *types;
2970 struct lsa_SidArray sid_array;
2971 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2973 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2975 MAXIMUM_ALLOWED_ACCESS,
2979 if (!NT_STATUS_IS_OK(status)) {
2982 if (!NT_STATUS_IS_OK(result)) {
2986 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
2990 if (!NT_STATUS_IS_OK(status)) {
2991 d_fprintf(stderr, _("Couldn't list alias members\n"));
2994 if (!NT_STATUS_IS_OK(result)) {
2995 d_fprintf(stderr, _("Couldn't list alias members\n"));
2999 num_members = sid_array.num_sids;
3001 if (num_members == 0) {
3002 return NT_STATUS_OK;
3005 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
3008 if (!NT_STATUS_IS_OK(result)) {
3009 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
3010 nt_errstr(result) );
3014 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
3015 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
3017 if (!NT_STATUS_IS_OK(result)) {
3018 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
3019 TALLOC_FREE(lsa_pipe);
3023 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
3025 d_fprintf(stderr, _("Out of memory\n"));
3026 TALLOC_FREE(lsa_pipe);
3027 return NT_STATUS_NO_MEMORY;
3030 for (i=0; i<num_members; i++) {
3031 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
3034 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
3035 num_members, alias_sids,
3036 &domains, &names, &types);
3038 if (!NT_STATUS_IS_OK(result) &&
3039 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
3040 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
3041 TALLOC_FREE(lsa_pipe);
3045 for (i = 0; i < num_members; i++) {
3047 sid_to_fstring(sid_str, &alias_sids[i]);
3049 if (c->opt_long_list_entries) {
3050 printf("%s %s\\%s %d\n", sid_str,
3051 domains[i] ? domains[i] : _("*unknown*"),
3052 names[i] ? names[i] : _("*unknown*"), types[i]);
3055 printf("%s\\%s\n", domains[i], names[i]);
3057 printf("%s\n", sid_str);
3061 TALLOC_FREE(lsa_pipe);
3062 return NT_STATUS_OK;
3065 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3066 const struct dom_sid *domain_sid,
3067 const char *domain_name,
3068 struct cli_state *cli,
3069 struct rpc_pipe_client *pipe_hnd,
3070 TALLOC_CTX *mem_ctx,
3074 NTSTATUS result, status;
3075 struct policy_handle connect_pol, domain_pol;
3076 struct samr_Ids rids, rid_types;
3077 struct lsa_String lsa_acct_name;
3078 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3080 /* Get sam policy handle */
3082 status = dcerpc_samr_Connect2(b, mem_ctx,
3084 MAXIMUM_ALLOWED_ACCESS,
3087 if (!NT_STATUS_IS_OK(status)) {
3090 if (!NT_STATUS_IS_OK(result)) {
3094 /* Get domain policy handle */
3096 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3098 MAXIMUM_ALLOWED_ACCESS,
3099 discard_const_p(struct dom_sid2, domain_sid),
3102 if (!NT_STATUS_IS_OK(status)) {
3105 if (!NT_STATUS_IS_OK(result)) {
3109 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3111 status = dcerpc_samr_LookupNames(b, mem_ctx,
3118 if (!NT_STATUS_IS_OK(status)) {
3122 if (!NT_STATUS_IS_OK(result)) {
3124 /* Ok, did not find it in the global sam, try with builtin */
3126 struct dom_sid sid_Builtin;
3128 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3130 sid_copy(&sid_Builtin, &global_sid_Builtin);
3132 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3134 MAXIMUM_ALLOWED_ACCESS,
3138 if (!NT_STATUS_IS_OK(status)) {
3141 if (!NT_STATUS_IS_OK(result)) {
3142 d_fprintf(stderr, _("Couldn't find group %s\n"),
3147 status = dcerpc_samr_LookupNames(b, mem_ctx,
3154 if (!NT_STATUS_IS_OK(status)) {
3157 if (!NT_STATUS_IS_OK(result)) {
3158 d_fprintf(stderr, _("Couldn't find group %s\n"),
3164 if (rids.count != 1) {
3165 d_fprintf(stderr, _("Couldn't find group %s\n"),
3170 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3171 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3172 domain_sid, &domain_pol,
3176 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3177 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3181 return NT_STATUS_NO_SUCH_GROUP;
3184 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3186 if (argc != 1 || c->display_usage) {
3187 return rpc_group_usage(c, argc, argv);
3190 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3191 rpc_group_members_internals,
3195 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3197 NET_API_STATUS status;
3198 struct GROUP_INFO_0 g0;
3202 d_printf(_("Usage:\n"));
3203 d_printf("net rpc group rename group newname\n");
3207 g0.grpi0_name = argv[1];
3209 status = NetGroupSetInfo(c->opt_host,
3216 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3217 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3225 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3227 if (argc != 2 || c->display_usage) {
3228 return rpc_group_usage(c, argc, argv);
3231 return rpc_group_rename_internals(c, argc, argv);
3235 * 'net rpc group' entrypoint.
3236 * @param argc Standard main() style argc.
3237 * @param argv Standard main() style argv. Initial components are already
3241 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3243 NET_API_STATUS status;
3245 struct functable func[] = {
3250 N_("Create specified group"),
3251 N_("net rpc group add\n"
3252 " Create specified group")
3258 N_("Delete specified group"),
3259 N_("net rpc group delete\n"
3260 " Delete specified group")
3266 N_("Add member to group"),
3267 N_("net rpc group addmem\n"
3268 " Add member to group")
3274 N_("Remove member from group"),
3275 N_("net rpc group delmem\n"
3276 " Remove member from group")
3283 N_("net rpc group list\n"
3290 N_("List group members"),
3291 N_("net rpc group members\n"
3292 " List group members")
3299 N_("net rpc group rename\n"
3302 {NULL, NULL, 0, NULL, NULL}
3305 status = libnetapi_net_init(&c->netapi_ctx);
3309 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3310 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3311 if (c->opt_kerberos) {
3312 libnetapi_set_use_kerberos(c->netapi_ctx);
3316 if (c->display_usage) {
3317 d_printf(_("Usage:\n"));
3318 d_printf(_("net rpc group\n"
3319 " Alias for net rpc group list global "
3320 "local builtin\n"));
3321 net_display_usage_from_functable(func);
3325 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3326 rpc_group_list_internals,
3330 return net_run_function(c, argc, argv, "net rpc group", func);
3333 /****************************************************************************/
3335 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3337 return net_share_usage(c, argc, argv);
3341 * Add a share on a remote RPC server.
3343 * @param argc Standard main() style argc.
3344 * @param argv Standard main() style argv. Initial components are already
3347 * @return A shell status integer (0 for success).
3350 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3352 NET_API_STATUS status;
3355 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3356 uint32 num_users=0, perms=0;
3357 char *password=NULL; /* don't allow a share password */
3358 struct SHARE_INFO_2 i2;
3359 uint32_t parm_error = 0;
3361 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3362 return rpc_share_usage(c, argc, argv);
3365 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3369 path = strchr(sharename, '=');
3376 i2.shi2_netname = sharename;
3377 i2.shi2_type = type;
3378 i2.shi2_remark = c->opt_comment;
3379 i2.shi2_permissions = perms;
3380 i2.shi2_max_uses = c->opt_maxusers;
3381 i2.shi2_current_uses = num_users;
3382 i2.shi2_path = path;
3383 i2.shi2_passwd = password;
3385 status = NetShareAdd(c->opt_host,
3390 printf(_("NetShareAdd failed with: %s\n"),
3391 libnetapi_get_error_string(c->netapi_ctx, status));
3398 * Delete a share on a remote RPC server.
3400 * @param domain_sid The domain sid acquired from the remote server.
3401 * @param argc Standard main() style argc.
3402 * @param argv Standard main() style argv. Initial components are already
3405 * @return A shell status integer (0 for success).
3407 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3409 if (argc < 1 || c->display_usage) {
3410 return rpc_share_usage(c, argc, argv);
3413 return NetShareDel(c->opt_host, argv[0], 0);
3417 * Formatted print of share info
3419 * @param r pointer to SHARE_INFO_1 to format
3422 static void display_share_info_1(struct net_context *c,
3423 struct SHARE_INFO_1 *r)
3425 if (c->opt_long_list_entries) {
3426 d_printf("%-12s %-8.8s %-50s\n",
3428 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3431 d_printf("%s\n", r->shi1_netname);
3435 static WERROR get_share_info(struct net_context *c,
3436 struct rpc_pipe_client *pipe_hnd,
3437 TALLOC_CTX *mem_ctx,
3441 struct srvsvc_NetShareInfoCtr *info_ctr)
3445 union srvsvc_NetShareInfo info;
3446 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3448 /* no specific share requested, enumerate all */
3451 uint32_t preferred_len = 0xffffffff;
3452 uint32_t total_entries = 0;
3453 uint32_t resume_handle = 0;
3455 info_ctr->level = level;
3457 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3464 if (!NT_STATUS_IS_OK(status)) {
3465 return ntstatus_to_werror(status);
3470 /* request just one share */
3471 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3478 if (!NT_STATUS_IS_OK(status)) {
3479 result = ntstatus_to_werror(status);
3483 if (!W_ERROR_IS_OK(result)) {
3488 ZERO_STRUCTP(info_ctr);
3490 info_ctr->level = level;
3495 struct srvsvc_NetShareCtr1 *ctr1;
3497 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3498 W_ERROR_HAVE_NO_MEMORY(ctr1);
3501 ctr1->array = info.info1;
3503 info_ctr->ctr.ctr1 = ctr1;
3509 struct srvsvc_NetShareCtr2 *ctr2;
3511 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3512 W_ERROR_HAVE_NO_MEMORY(ctr2);
3515 ctr2->array = info.info2;
3517 info_ctr->ctr.ctr2 = ctr2;
3523 struct srvsvc_NetShareCtr502 *ctr502;
3525 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3526 W_ERROR_HAVE_NO_MEMORY(ctr502);
3529 ctr502->array = info.info502;
3531 info_ctr->ctr.ctr502 = ctr502;
3541 * 'net rpc share list' entrypoint.
3542 * @param argc Standard main() style argc.
3543 * @param argv Standard main() style argv. Initial components are already
3546 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3548 NET_API_STATUS status;
3549 struct SHARE_INFO_1 *i1 = NULL;
3550 uint32_t entries_read = 0;
3551 uint32_t total_entries = 0;
3552 uint32_t resume_handle = 0;
3553 uint32_t i, level = 1;
3555 if (c->display_usage) {
3557 "net rpc share list\n"
3560 _("List shares on remote server"));
3564 status = NetShareEnum(c->opt_host,
3566 (uint8_t **)(void *)&i1,
3575 /* Display results */
3577 if (c->opt_long_list_entries) {
3579 "\nEnumerating shared resources (exports) on remote server:\n\n"
3580 "\nShare name Type Description\n"
3581 "---------- ---- -----------\n"));
3583 for (i = 0; i < entries_read; i++)
3584 display_share_info_1(c, &i1[i]);
3589 static bool check_share_availability(struct cli_state *cli, const char *netname)
3593 status = cli_tree_connect(cli, netname, "A:", "", 0);
3594 if (!NT_STATUS_IS_OK(status)) {
3595 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3599 status = cli_tdis(cli);
3600 if (!NT_STATUS_IS_OK(status)) {
3601 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3608 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3609 const char *netname, uint32 type)
3611 /* only support disk shares */
3612 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3613 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3618 /* skip builtin shares */
3619 /* FIXME: should print$ be added too ? */
3620 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3621 strequal(netname,"global"))
3624 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3625 printf(_("excluding [%s]\n"), netname);
3629 return check_share_availability(cli, netname);
3633 * Migrate shares from a remote RPC server to the local RPC server.
3635 * All parameters are provided by the run_rpc_command function, except for
3636 * argc, argv which are passed through.
3638 * @param domain_sid The domain sid acquired from the remote server.
3639 * @param cli A cli_state connected to the server.
3640 * @param mem_ctx Talloc context, destroyed on completion of the function.
3641 * @param argc Standard main() style argc.
3642 * @param argv Standard main() style argv. Initial components are already
3645 * @return Normal NTSTATUS return.
3648 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3649 const struct dom_sid *domain_sid,
3650 const char *domain_name,
3651 struct cli_state *cli,
3652 struct rpc_pipe_client *pipe_hnd,
3653 TALLOC_CTX *mem_ctx,
3658 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3659 struct srvsvc_NetShareInfoCtr ctr_src;
3661 struct rpc_pipe_client *srvsvc_pipe = NULL;
3662 struct cli_state *cli_dst = NULL;
3663 uint32 level = 502; /* includes secdesc */
3664 uint32_t parm_error = 0;
3665 struct dcerpc_binding_handle *b;
3667 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3669 if (!W_ERROR_IS_OK(result))
3672 /* connect destination PI_SRVSVC */
3673 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3675 if (!NT_STATUS_IS_OK(nt_status))
3678 b = srvsvc_pipe->binding_handle;
3680 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3682 union srvsvc_NetShareInfo info;
3683 struct srvsvc_NetShareInfo502 info502 =
3684 ctr_src.ctr.ctr502->array[i];
3686 /* reset error-code */
3687 nt_status = NT_STATUS_UNSUCCESSFUL;
3689 if (!check_share_sanity(c, cli, info502.name, info502.type))
3692 /* finally add the share on the dst server */
3694 printf(_("migrating: [%s], path: %s, comment: %s, without "
3696 info502.name, info502.path, info502.comment);
3698 info.info502 = &info502;
3700 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3701 srvsvc_pipe->desthost,
3706 if (!NT_STATUS_IS_OK(nt_status)) {
3707 printf(_("cannot add share: %s\n"),
3708 nt_errstr(nt_status));
3711 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3712 printf(_(" [%s] does already exist\n"),
3717 if (!W_ERROR_IS_OK(result)) {
3718 nt_status = werror_to_ntstatus(result);
3719 printf(_("cannot add share: %s\n"),
3720 win_errstr(result));
3726 nt_status = NT_STATUS_OK;
3730 cli_shutdown(cli_dst);
3738 * Migrate shares from a RPC server to another.
3740 * @param argc Standard main() style argc.
3741 * @param argv Standard main() style argv. Initial components are already
3744 * @return A shell status integer (0 for success).
3746 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3749 if (c->display_usage) {
3751 "net rpc share migrate shares\n"
3754 _("Migrate shares to local server"));
3759 printf(_("no server to migrate\n"));
3763 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3764 rpc_share_migrate_shares_internals,
3771 * @param f file_info
3772 * @param mask current search mask
3773 * @param state arg-pointer
3776 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3777 const char *mask, void *state)
3779 static NTSTATUS nt_status;
3780 static struct copy_clistate *local_state;
3781 static fstring filename, new_mask;
3784 struct net_context *c;
3786 local_state = (struct copy_clistate *)state;
3787 nt_status = NT_STATUS_UNSUCCESSFUL;
3791 if (strequal(f->name, ".") || strequal(f->name, ".."))
3792 return NT_STATUS_OK;
3794 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3797 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3799 DEBUG(3,("got dir: %s\n", f->name));
3801 fstrcpy(dir, local_state->cwd);
3803 fstrcat(dir, f->name);
3805 switch (net_mode_share)
3807 case NET_MODE_SHARE_MIGRATE:
3808 /* create that directory */
3809 nt_status = net_copy_file(c, local_state->mem_ctx,
3810 local_state->cli_share_src,
3811 local_state->cli_share_dst,
3813 c->opt_acls? true : false,
3814 c->opt_attrs? true : false,
3815 c->opt_timestamps? true:false,
3819 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3820 return NT_STATUS_INTERNAL_ERROR;
3823 if (!NT_STATUS_IS_OK(nt_status)) {
3824 printf(_("could not handle dir %s: %s\n"),
3825 dir, nt_errstr(nt_status));
3829 /* search below that directory */
3830 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
3831 return NT_STATUS_NO_MEMORY;
3833 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
3834 return NT_STATUS_NO_MEMORY;
3837 old_dir = local_state->cwd;
3838 local_state->cwd = dir;
3839 nt_status = sync_files(local_state, new_mask);
3840 if (!NT_STATUS_IS_OK(nt_status)) {
3841 printf(_("could not handle files\n"));
3843 local_state->cwd = old_dir;
3850 fstrcpy(filename, local_state->cwd);
3851 fstrcat(filename, "\\");
3852 fstrcat(filename, f->name);
3854 DEBUG(3,("got file: %s\n", filename));
3856 switch (net_mode_share)
3858 case NET_MODE_SHARE_MIGRATE:
3859 nt_status = net_copy_file(c, local_state->mem_ctx,
3860 local_state->cli_share_src,
3861 local_state->cli_share_dst,
3863 c->opt_acls? true : false,
3864 c->opt_attrs? true : false,
3865 c->opt_timestamps? true: false,
3869 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3871 return NT_STATUS_INTERNAL_ERROR;
3874 if (!NT_STATUS_IS_OK(nt_status))
3875 printf(_("could not handle file %s: %s\n"),
3876 filename, nt_errstr(nt_status));
3881 * sync files, can be called recursivly to list files
3882 * and then call copy_fn for each file
3884 * @param cp_clistate pointer to the copy_clistate we work with
3885 * @param mask the current search mask
3887 * @return Boolean result
3889 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3891 struct cli_state *targetcli;
3892 char *targetpath = NULL;
3895 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3897 status = cli_resolve_path(talloc_tos(), "", NULL,
3898 cp_clistate->cli_share_src,
3899 mask, &targetcli, &targetpath);
3900 if (!NT_STATUS_IS_OK(status)) {
3901 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3903 mask, nt_errstr(status));
3907 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3908 copy_fn, cp_clistate);
3909 if (!NT_STATUS_IS_OK(status)) {
3910 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3911 mask, nt_errstr(status));
3919 * Set the top level directory permissions before we do any further copies.
3920 * Should set up ACL inheritance.
3923 bool copy_top_level_perms(struct net_context *c,
3924 struct copy_clistate *cp_clistate,
3925 const char *sharename)
3927 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3929 switch (net_mode_share) {
3930 case NET_MODE_SHARE_MIGRATE:
3931 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3932 nt_status = net_copy_fileattr(c,
3933 cp_clistate->mem_ctx,
3934 cp_clistate->cli_share_src,
3935 cp_clistate->cli_share_dst,
3937 c->opt_acls? true : false,
3938 c->opt_attrs? true : false,
3939 c->opt_timestamps? true: false,
3943 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3947 if (!NT_STATUS_IS_OK(nt_status)) {
3948 printf(_("Could handle directory attributes for top level "
3949 "directory of share %s. Error %s\n"),
3950 sharename, nt_errstr(nt_status));
3958 * Sync all files inside a remote share to another share (over smb).
3960 * All parameters are provided by the run_rpc_command function, except for
3961 * argc, argv which are passed through.
3963 * @param domain_sid The domain sid acquired from the remote server.
3964 * @param cli A cli_state connected to the server.
3965 * @param mem_ctx Talloc context, destroyed on completion of the function.
3966 * @param argc Standard main() style argc.
3967 * @param argv Standard main() style argv. Initial components are already
3970 * @return Normal NTSTATUS return.
3973 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3974 const struct dom_sid *domain_sid,
3975 const char *domain_name,
3976 struct cli_state *cli,
3977 struct rpc_pipe_client *pipe_hnd,
3978 TALLOC_CTX *mem_ctx,
3983 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3984 struct srvsvc_NetShareInfoCtr ctr_src;
3987 struct copy_clistate cp_clistate;
3988 bool got_src_share = false;
3989 bool got_dst_share = false;
3990 const char *mask = "\\*";
3993 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3995 nt_status = NT_STATUS_NO_MEMORY;
3999 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4002 if (!W_ERROR_IS_OK(result))
4005 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4007 struct srvsvc_NetShareInfo502 info502 =
4008 ctr_src.ctr.ctr502->array[i];
4010 if (!check_share_sanity(c, cli, info502.name, info502.type))
4013 /* one might not want to mirror whole discs :) */
4014 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
4015 d_printf(_("skipping [%s]: builtin/hidden share\n"),
4020 switch (net_mode_share)
4022 case NET_MODE_SHARE_MIGRATE:
4026 d_fprintf(stderr, _("Unsupported mode %d\n"),
4030 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4033 c->opt_acls ? _("including") : _("without"),
4034 c->opt_attrs ? _("including") : _("without"),
4035 c->opt_timestamps ? _("(preserving timestamps)") : "");
4037 cp_clistate.mem_ctx = mem_ctx;
4038 cp_clistate.cli_share_src = NULL;
4039 cp_clistate.cli_share_dst = NULL;
4040 cp_clistate.cwd = NULL;
4041 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
4044 /* open share source */
4045 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
4046 smbXcli_conn_remote_sockaddr(cli->conn),
4047 smbXcli_conn_remote_name(cli->conn),
4048 info502.name, "A:");
4049 if (!NT_STATUS_IS_OK(nt_status))
4052 got_src_share = true;
4054 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4055 /* open share destination */
4056 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4057 NULL, dst, info502.name, "A:");
4058 if (!NT_STATUS_IS_OK(nt_status))
4061 got_dst_share = true;
4064 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4065 d_fprintf(stderr, _("Could not handle the top level "
4066 "directory permissions for the "
4067 "share: %s\n"), info502.name);
4068 nt_status = NT_STATUS_UNSUCCESSFUL;
4072 nt_status = sync_files(&cp_clistate, mask);
4073 if (!NT_STATUS_IS_OK(nt_status)) {
4074 d_fprintf(stderr, _("could not handle files for share: "
4075 "%s\n"), info502.name);
4080 nt_status = NT_STATUS_OK;
4085 cli_shutdown(cp_clistate.cli_share_src);
4088 cli_shutdown(cp_clistate.cli_share_dst);
4095 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4097 if (c->display_usage) {
4099 "net share migrate files\n"
4102 _("Migrate files to local server"));
4107 d_printf(_("no server to migrate\n"));
4111 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4112 rpc_share_migrate_files_internals,
4117 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4119 * All parameters are provided by the run_rpc_command function, except for
4120 * argc, argv which are passed through.
4122 * @param domain_sid The domain sid acquired from the remote server.
4123 * @param cli A cli_state connected to the server.
4124 * @param mem_ctx Talloc context, destroyed on completion of the function.
4125 * @param argc Standard main() style argc.
4126 * @param argv Standard main() style argv. Initial components are already
4129 * @return Normal NTSTATUS return.
4132 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4133 const struct dom_sid *domain_sid,
4134 const char *domain_name,
4135 struct cli_state *cli,
4136 struct rpc_pipe_client *pipe_hnd,
4137 TALLOC_CTX *mem_ctx,
4142 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4143 struct srvsvc_NetShareInfoCtr ctr_src;
4144 union srvsvc_NetShareInfo info;
4146 struct rpc_pipe_client *srvsvc_pipe = NULL;
4147 struct cli_state *cli_dst = NULL;
4148 uint32 level = 502; /* includes secdesc */
4149 uint32_t parm_error = 0;
4150 struct dcerpc_binding_handle *b;
4152 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4155 if (!W_ERROR_IS_OK(result))
4158 /* connect destination PI_SRVSVC */
4159 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4161 if (!NT_STATUS_IS_OK(nt_status))
4164 b = srvsvc_pipe->binding_handle;
4166 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4168 struct srvsvc_NetShareInfo502 info502 =
4169 ctr_src.ctr.ctr502->array[i];
4171 /* reset error-code */
4172 nt_status = NT_STATUS_UNSUCCESSFUL;
4174 if (!check_share_sanity(c, cli, info502.name, info502.type))
4177 printf(_("migrating: [%s], path: %s, comment: %s, including "
4179 info502.name, info502.path, info502.comment);
4182 display_sec_desc(info502.sd_buf.sd);
4184 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4185 info.info502 = &info502;
4187 /* finally modify the share on the dst server */
4188 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4189 srvsvc_pipe->desthost,
4195 if (!NT_STATUS_IS_OK(nt_status)) {
4196 printf(_("cannot set share-acl: %s\n"),
4197 nt_errstr(nt_status));
4200 if (!W_ERROR_IS_OK(result)) {
4201 nt_status = werror_to_ntstatus(result);
4202 printf(_("cannot set share-acl: %s\n"),
4203 win_errstr(result));
4209 nt_status = NT_STATUS_OK;
4213 cli_shutdown(cli_dst);
4221 * Migrate share-acls from a RPC server to another.
4223 * @param argc Standard main() style argc.
4224 * @param argv Standard main() style argv. Initial components are already
4227 * @return A shell status integer (0 for success).
4229 static int rpc_share_migrate_security(struct net_context *c, int argc,
4232 if (c->display_usage) {
4234 "net rpc share migrate security\n"
4237 _("Migrate share-acls to local server"));
4242 d_printf(_("no server to migrate\n"));
4246 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4247 rpc_share_migrate_security_internals,
4252 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4253 * from one server to another.
4255 * @param argc Standard main() style argc.
4256 * @param argv Standard main() style argv. Initial components are already
4259 * @return A shell status integer (0 for success).
4262 static int rpc_share_migrate_all(struct net_context *c, int argc,
4267 if (c->display_usage) {
4269 "net rpc share migrate all\n"
4272 _("Migrates shares including all share settings"));
4277 d_printf(_("no server to migrate\n"));
4281 /* order is important. we don't want to be locked out by the share-acl
4282 * before copying files - gd */
4284 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4285 rpc_share_migrate_shares_internals, argc, argv);
4289 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4290 rpc_share_migrate_files_internals, argc, argv);
4294 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4295 rpc_share_migrate_security_internals, argc,
4301 * 'net rpc share migrate' entrypoint.
4302 * @param argc Standard main() style argc.
4303 * @param argv Standard main() style argv. Initial components are already
4306 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4309 struct functable func[] = {
4312 rpc_share_migrate_all,
4314 N_("Migrate shares from remote to local server"),
4315 N_("net rpc share migrate all\n"
4316 " Migrate shares from remote to local server")
4320 rpc_share_migrate_files,
4322 N_("Migrate files from remote to local server"),
4323 N_("net rpc share migrate files\n"
4324 " Migrate files from remote to local server")
4328 rpc_share_migrate_security,
4330 N_("Migrate share-ACLs from remote to local server"),
4331 N_("net rpc share migrate security\n"
4332 " Migrate share-ACLs from remote to local server")
4336 rpc_share_migrate_shares,
4338 N_("Migrate shares from remote to local server"),
4339 N_("net rpc share migrate shares\n"
4340 " Migrate shares from remote to local server")
4342 {NULL, NULL, 0, NULL, NULL}
4345 net_mode_share = NET_MODE_SHARE_MIGRATE;
4347 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4353 struct dom_sid *members;
4356 static int num_server_aliases;
4357 static struct full_alias *server_aliases;
4360 * Add an alias to the static list.
4362 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4364 if (server_aliases == NULL)
4365 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4367 server_aliases[num_server_aliases] = *alias;
4368 num_server_aliases += 1;
4372 * For a specific domain on the server, fetch all the aliases
4373 * and their members. Add all of them to the server_aliases.
4376 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4377 TALLOC_CTX *mem_ctx,
4378 struct policy_handle *connect_pol,
4379 const struct dom_sid *domain_sid)
4381 uint32 start_idx, max_entries, num_entries, i;
4382 struct samr_SamArray *groups = NULL;
4383 NTSTATUS result, status;
4384 struct policy_handle domain_pol;
4385 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4387 /* Get domain policy handle */
4389 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4391 MAXIMUM_ALLOWED_ACCESS,
4392 discard_const_p(struct dom_sid2, domain_sid),
4395 if (!NT_STATUS_IS_OK(status)) {
4398 if (!NT_STATUS_IS_OK(result)) {
4406 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4413 if (!NT_STATUS_IS_OK(status)) {
4416 for (i = 0; i < num_entries; i++) {
4418 struct policy_handle alias_pol;
4419 struct full_alias alias;
4420 struct lsa_SidArray sid_array;
4424 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4426 MAXIMUM_ALLOWED_ACCESS,
4427 groups->entries[i].idx,
4430 if (!NT_STATUS_IS_OK(status)) {
4433 if (!NT_STATUS_IS_OK(_result)) {
4438 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4442 if (!NT_STATUS_IS_OK(status)) {
4445 if (!NT_STATUS_IS_OK(_result)) {
4450 alias.num_members = sid_array.num_sids;
4452 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4453 if (!NT_STATUS_IS_OK(status)) {
4456 if (!NT_STATUS_IS_OK(_result)) {
4461 alias.members = NULL;
4463 if (alias.num_members > 0) {
4464 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4466 for (j = 0; j < alias.num_members; j++)
4467 sid_copy(&alias.members[j],
4468 sid_array.sids[j].sid);
4471 sid_compose(&alias.sid, domain_sid,
4472 groups->entries[i].idx);
4474 push_alias(mem_ctx, &alias);
4476 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4478 status = NT_STATUS_OK;
4481 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4487 * Dump server_aliases as names for debugging purposes.
4490 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4491 const struct dom_sid *domain_sid,
4492 const char *domain_name,
4493 struct cli_state *cli,
4494 struct rpc_pipe_client *pipe_hnd,
4495 TALLOC_CTX *mem_ctx,
4501 struct policy_handle lsa_pol;
4502 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4504 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4505 SEC_FLAG_MAXIMUM_ALLOWED,
4507 if (!NT_STATUS_IS_OK(result))
4510 for (i=0; i<num_server_aliases; i++) {
4513 enum lsa_SidType *types;
4516 struct full_alias *alias = &server_aliases[i];
4518 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4520 &domains, &names, &types);
4521 if (!NT_STATUS_IS_OK(result))
4524 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4526 if (alias->num_members == 0) {
4531 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4534 &domains, &names, &types);
4536 if (!NT_STATUS_IS_OK(result) &&
4537 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4540 for (j=0; j<alias->num_members; j++)
4541 DEBUG(1, ("%s\\%s (%d); ",
4542 domains[j] ? domains[j] : "*unknown*",
4543 names[j] ? names[j] : "*unknown*",types[j]));
4547 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4549 return NT_STATUS_OK;
4553 * Fetch a list of all server aliases and their members into
4557 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4558 const struct dom_sid *domain_sid,
4559 const char *domain_name,
4560 struct cli_state *cli,
4561 struct rpc_pipe_client *pipe_hnd,
4562 TALLOC_CTX *mem_ctx,
4566 NTSTATUS result, status;
4567 struct policy_handle connect_pol;
4568 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4570 status = dcerpc_samr_Connect2(b, mem_ctx,
4572 MAXIMUM_ALLOWED_ACCESS,
4575 if (!NT_STATUS_IS_OK(status)) {
4578 if (!NT_STATUS_IS_OK(result)) {
4583 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4584 &global_sid_Builtin);
4585 if (!NT_STATUS_IS_OK(status)) {
4589 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4592 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4597 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4599 token->num_sids = 4;
4601 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4602 d_fprintf(stderr, "malloc %s\n",_("failed"));
4603 token->num_sids = 0;
4607 token->sids[0] = *user_sid;
4608 sid_copy(&token->sids[1], &global_sid_World);
4609 sid_copy(&token->sids[2], &global_sid_Network);
4610 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4613 static void free_user_token(struct security_token *token)
4615 SAFE_FREE(token->sids);
4618 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4620 if (security_token_has_sid(token, sid))
4623 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4628 sid_copy(&token->sids[token->num_sids], sid);
4630 token->num_sids += 1;
4635 struct security_token token;
4638 static void dump_user_token(struct user_token *token)
4642 d_printf("%s\n", token->name);
4644 for (i=0; i<token->token.num_sids; i++) {
4645 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4649 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4653 for (i=0; i<alias->num_members; i++) {
4654 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4661 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4665 for (i=0; i<num_server_aliases; i++) {
4666 if (is_alias_member(&sid, &server_aliases[i]))
4667 add_sid_to_token(token, &server_aliases[i].sid);
4672 * We got a user token with all the SIDs we can know about without asking the
4673 * server directly. These are the user and domain group sids. All of these can
4674 * be members of aliases. So scan the list of aliases for each of the SIDs and
4675 * add them to the token.
4678 static void collect_alias_memberships(struct security_token *token)
4680 int num_global_sids = token->num_sids;
4683 for (i=0; i<num_global_sids; i++) {
4684 collect_sid_memberships(token, token->sids[i]);
4688 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4690 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4691 enum wbcSidType type;
4693 struct wbcDomainSid wsid;
4694 char sid_str[WBC_SID_STRING_BUFLEN];
4695 struct dom_sid user_sid;
4696 uint32_t num_groups;
4697 gid_t *groups = NULL;
4700 fstr_sprintf(full_name, "%s%c%s",
4701 domain, *lp_winbind_separator(), user);
4703 /* First let's find out the user sid */
4705 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4707 if (!WBC_ERROR_IS_OK(wbc_status)) {
4708 DEBUG(1, ("winbind could not find %s: %s\n",
4709 full_name, wbcErrorString(wbc_status)));
4713 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4715 if (type != WBC_SID_NAME_USER) {
4716 DEBUG(1, ("%s is not a user\n", full_name));
4720 if (!string_to_sid(&user_sid, sid_str)) {
4721 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4725 init_user_token(token, &user_sid);
4727 /* And now the groups winbind knows about */
4729 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4730 if (!WBC_ERROR_IS_OK(wbc_status)) {
4731 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4732 full_name, wbcErrorString(wbc_status)));
4736 for (i = 0; i < num_groups; i++) {
4737 gid_t gid = groups[i];
4741 wbc_status = wbcGidToSid(gid, &wsid);
4742 if (!WBC_ERROR_IS_OK(wbc_status)) {
4743 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4744 (unsigned int)gid, wbcErrorString(wbc_status)));
4745 wbcFreeMemory(groups);
4749 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4751 DEBUG(3, (" %s\n", sid_str));
4753 ok = string_to_sid(&sid, sid_str);
4755 DEBUG(1, ("Failed to convert string to SID\n"));
4756 wbcFreeMemory(groups);
4759 add_sid_to_token(token, &sid);
4761 wbcFreeMemory(groups);
4767 * Get a list of all user tokens we want to look at
4770 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4771 struct user_token **user_tokens)
4773 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4774 uint32_t i, num_users;
4776 struct user_token *result;
4777 TALLOC_CTX *frame = NULL;
4779 if (lp_winbind_use_default_domain() &&
4780 (c->opt_target_workgroup == NULL)) {
4781 d_fprintf(stderr, _("winbind use default domain = yes set, "
4782 "please specify a workgroup\n"));
4786 /* Send request to winbind daemon */
4788 wbc_status = wbcListUsers(NULL, &num_users, &users);
4789 if (!WBC_ERROR_IS_OK(wbc_status)) {
4790 DEBUG(1, (_("winbind could not list users: %s\n"),
4791 wbcErrorString(wbc_status)));
4795 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4797 if (result == NULL) {
4798 DEBUG(1, ("Could not malloc sid array\n"));
4799 wbcFreeMemory(users);
4803 frame = talloc_stackframe();
4804 for (i=0; i < num_users; i++) {
4805 fstring domain, user;
4808 fstrcpy(result[i].name, users[i]);
4810 p = strchr(users[i], *lp_winbind_separator());
4812 DEBUG(3, ("%s\n", users[i]));
4815 fstrcpy(domain, c->opt_target_workgroup);
4816 fstrcpy(user, users[i]);
4819 fstrcpy(domain, users[i]);
4820 if (!strupper_m(domain)) {
4821 DEBUG(1, ("strupper_m %s failed\n", domain));
4822 wbcFreeMemory(users);
4828 get_user_sids(domain, user, &(result[i].token));
4831 wbcFreeMemory(users);
4833 *num_tokens = num_users;
4834 *user_tokens = result;
4839 static bool get_user_tokens_from_file(FILE *f,
4841 struct user_token **tokens)
4843 struct user_token *token = NULL;
4848 if (fgets(line, sizeof(line)-1, f) == NULL) {
4852 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4853 line[strlen(line)-1] = '\0';
4856 if (line[0] == ' ') {
4860 if(!string_to_sid(&sid, &line[1])) {
4861 DEBUG(1,("get_user_tokens_from_file: Could "
4862 "not convert sid %s \n",&line[1]));
4866 if (token == NULL) {
4867 DEBUG(0, ("File does not begin with username"));
4871 add_sid_to_token(&token->token, &sid);
4875 /* And a new user... */
4878 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4879 if (*tokens == NULL) {
4880 DEBUG(0, ("Could not realloc tokens\n"));
4884 token = &((*tokens)[*num_tokens-1]);
4886 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
4889 token->token.num_sids = 0;
4890 token->token.sids = NULL;
4899 * Show the list of all users that have access to a share
4902 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4903 TALLOC_CTX *mem_ctx,
4904 const char *netname,
4906 struct user_token *tokens)
4909 struct security_descriptor *share_sd = NULL;
4910 struct security_descriptor *root_sd = NULL;
4911 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4913 union srvsvc_NetShareInfo info;
4917 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4919 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4926 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4927 DEBUG(1, ("Coult not query secdesc for share %s\n",
4932 share_sd = info.info502->sd_buf.sd;
4933 if (share_sd == NULL) {
4934 DEBUG(1, ("Got no secdesc for share %s\n",
4938 cnum = cli_state_get_tid(cli);
4940 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", "", 0))) {
4944 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4945 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4946 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
4949 for (i=0; i<num_tokens; i++) {
4952 if (share_sd != NULL) {
4953 status = se_access_check(share_sd, &tokens[i].token,
4956 if (!NT_STATUS_IS_OK(status)) {
4957 DEBUG(1, ("Could not check share_sd for "
4964 if (root_sd == NULL) {
4965 d_printf(" %s\n", tokens[i].name);
4969 status = se_access_check(root_sd, &tokens[i].token,
4971 if (!NT_STATUS_IS_OK(status)) {
4972 DEBUG(1, ("Could not check root_sd for user %s\n",
4976 d_printf(" %s\n", tokens[i].name);
4979 if (fnum != (uint16_t)-1)
4980 cli_close(cli, fnum);
4982 cli_state_set_tid(cli, cnum);
4988 * List shares on a remote RPC server, including the security descriptors.
4990 * All parameters are provided by the run_rpc_command function, except for
4991 * argc, argv which are passed through.
4993 * @param domain_sid The domain sid acquired from the remote server.
4994 * @param cli A cli_state connected to the server.
4995 * @param mem_ctx Talloc context, destroyed on completion of the function.
4996 * @param argc Standard main() style argc.
4997 * @param argv Standard main() style argv. Initial components are already
5000 * @return Normal NTSTATUS return.
5003 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
5004 const struct dom_sid *domain_sid,
5005 const char *domain_name,
5006 struct cli_state *cli,
5007 struct rpc_pipe_client *pipe_hnd,
5008 TALLOC_CTX *mem_ctx,
5014 NTSTATUS nt_status = NT_STATUS_OK;
5015 uint32_t total_entries = 0;
5016 uint32_t resume_handle = 0;
5017 uint32_t preferred_len = 0xffffffff;
5019 struct dcerpc_binding_handle *b = NULL;
5020 struct srvsvc_NetShareInfoCtr info_ctr;
5021 struct srvsvc_NetShareCtr1 ctr1;
5024 struct user_token *tokens = NULL;
5030 f = fopen(argv[0], "r");
5034 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
5035 return NT_STATUS_UNSUCCESSFUL;
5038 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
5044 DEBUG(0, ("Could not read users from file\n"));
5045 return NT_STATUS_UNSUCCESSFUL;
5048 for (i=0; i<num_tokens; i++)
5049 collect_alias_memberships(&tokens[i].token);
5051 ZERO_STRUCT(info_ctr);
5055 info_ctr.ctr.ctr1 = &ctr1;
5057 b = pipe_hnd->binding_handle;
5059 /* Issue the NetShareEnum RPC call and retrieve the response */
5060 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5069 /* Was it successful? */
5070 if (!NT_STATUS_IS_OK(nt_status)) {
5071 /* Nope. Go clean up. */
5075 if (!W_ERROR_IS_OK(result)) {
5076 /* Nope. Go clean up. */
5077 nt_status = werror_to_ntstatus(result);
5081 if (total_entries == 0) {
5085 /* For each returned entry... */
5086 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5087 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5089 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5093 d_printf("%s\n", netname);
5095 show_userlist(pipe_hnd, mem_ctx, netname,
5096 num_tokens, tokens);
5099 for (i=0; i<num_tokens; i++) {
5100 free_user_token(&tokens[i].token);
5107 static int rpc_share_allowedusers(struct net_context *c, int argc,
5112 if (c->display_usage) {
5114 "net rpc share allowedusers\n"
5117 _("List allowed users"));
5121 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5122 rpc_aliaslist_internals,
5127 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5133 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5134 rpc_share_allowedusers_internals,
5138 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5141 struct user_token *tokens = NULL;
5145 net_usersidlist_usage(c, argc, argv);
5149 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5150 DEBUG(0, ("Could not get the user/sid list\n"));
5154 for (i=0; i<num_tokens; i++) {
5155 dump_user_token(&tokens[i]);
5156 free_user_token(&tokens[i].token);
5163 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5165 d_printf(_("net usersidlist\n"
5166 "\tprints out a list of all users the running winbind knows\n"
5167 "\tabout, together with all their SIDs. This is used as\n"
5168 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5170 net_common_flags_usage(c, argc, argv);
5175 * 'net rpc share' entrypoint.
5176 * @param argc Standard main() style argc.
5177 * @param argv Standard main() style argv. Initial components are already
5181 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5183 NET_API_STATUS status;
5185 struct functable func[] = {
5191 N_("net rpc share add\n"
5199 N_("net rpc share delete\n"
5204 rpc_share_allowedusers,
5206 N_("Modify allowed users"),
5207 N_("net rpc share allowedusers\n"
5208 " Modify allowed users")
5214 N_("Migrate share to local server"),
5215 N_("net rpc share migrate\n"
5216 " Migrate share to local server")
5223 N_("net rpc share list\n"
5226 {NULL, NULL, 0, NULL, NULL}
5229 status = libnetapi_net_init(&c->netapi_ctx);
5233 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5234 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5235 if (c->opt_kerberos) {
5236 libnetapi_set_use_kerberos(c->netapi_ctx);
5240 if (c->display_usage) {
5245 " Alias for net rpc share list\n"));
5246 net_display_usage_from_functable(func);
5250 return rpc_share_list(c, argc, argv);
5253 return net_run_function(c, argc, argv, "net rpc share", func);
5256 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5257 TALLOC_CTX *mem_ctx,
5258 struct rpc_sh_ctx *ctx,
5259 struct rpc_pipe_client *pipe_hnd,
5260 int argc, const char **argv)
5263 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5266 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5267 TALLOC_CTX *mem_ctx,
5268 struct rpc_sh_ctx *ctx,
5269 struct rpc_pipe_client *pipe_hnd,
5270 int argc, const char **argv)
5272 NET_API_STATUS status;
5273 uint32_t parm_err = 0;
5274 struct SHARE_INFO_2 i2;
5276 if ((argc < 2) || (argc > 3)) {
5277 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5279 return NT_STATUS_INVALID_PARAMETER;
5282 i2.shi2_netname = argv[0];
5283 i2.shi2_type = STYPE_DISKTREE;
5284 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5285 i2.shi2_permissions = 0;
5286 i2.shi2_max_uses = 0;
5287 i2.shi2_current_uses = 0;
5288 i2.shi2_path = argv[1];
5289 i2.shi2_passwd = NULL;
5291 status = NetShareAdd(pipe_hnd->desthost,
5296 return werror_to_ntstatus(W_ERROR(status));
5299 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5300 TALLOC_CTX *mem_ctx,
5301 struct rpc_sh_ctx *ctx,
5302 struct rpc_pipe_client *pipe_hnd,
5303 int argc, const char **argv)
5306 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5307 return NT_STATUS_INVALID_PARAMETER;
5310 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5313 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5314 TALLOC_CTX *mem_ctx,
5315 struct rpc_sh_ctx *ctx,
5316 struct rpc_pipe_client *pipe_hnd,
5317 int argc, const char **argv)
5319 union srvsvc_NetShareInfo info;
5322 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5325 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5326 return NT_STATUS_INVALID_PARAMETER;
5329 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5335 if (!NT_STATUS_IS_OK(status)) {
5336 result = ntstatus_to_werror(status);
5339 if (!W_ERROR_IS_OK(result)) {
5343 d_printf(_("Name: %s\n"), info.info2->name);
5344 d_printf(_("Comment: %s\n"), info.info2->comment);
5345 d_printf(_("Path: %s\n"), info.info2->path);
5346 d_printf(_("Password: %s\n"), info.info2->password);
5349 return werror_to_ntstatus(result);
5352 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5353 struct rpc_sh_ctx *ctx)
5355 static struct rpc_sh_cmd cmds[] = {
5357 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5358 N_("List available shares") },
5360 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5361 N_("Add a share") },
5363 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5364 N_("Delete a share") },
5366 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5367 N_("Get information about a share") },
5369 { NULL, NULL, 0, NULL, NULL }
5375 /****************************************************************************/
5377 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5379 return net_file_usage(c, argc, argv);
5383 * Close a file on a remote RPC server.
5385 * @param argc Standard main() style argc.
5386 * @param argv Standard main() style argv. Initial components are already
5389 * @return A shell status integer (0 for success).
5391 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5393 if (argc < 1 || c->display_usage) {
5394 return rpc_file_usage(c, argc, argv);
5397 return NetFileClose(c->opt_host, atoi(argv[0]));
5401 * Formatted print of open file info
5403 * @param r struct FILE_INFO_3 contents
5406 static void display_file_info_3(struct FILE_INFO_3 *r)
5408 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5409 r->fi3_id, r->fi3_username, r->fi3_permissions,
5410 r->fi3_num_locks, r->fi3_pathname);
5414 * List files for a user on a remote RPC server.
5416 * @param argc Standard main() style argc.
5417 * @param argv Standard main() style argv. Initial components are already
5420 * @return A shell status integer (0 for success)..
5423 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5425 NET_API_STATUS status;
5426 uint32 preferred_len = 0xffffffff, i;
5427 char *username=NULL;
5428 uint32_t total_entries = 0;
5429 uint32_t entries_read = 0;
5430 uint32_t resume_handle = 0;
5431 struct FILE_INFO_3 *i3 = NULL;
5433 if (c->display_usage) {
5434 return rpc_file_usage(c, argc, argv);
5437 /* if argc > 0, must be user command */
5439 username = smb_xstrdup(argv[0]);
5442 status = NetFileEnum(c->opt_host,
5446 (uint8_t **)(void *)&i3,
5456 /* Display results */
5459 "\nEnumerating open files on remote server:\n\n"
5460 "\nFileId Opened by Perms Locks Path"
5461 "\n------ --------- ----- ----- ---- \n"));
5462 for (i = 0; i < entries_read; i++) {
5463 display_file_info_3(&i3[i]);
5466 SAFE_FREE(username);
5471 * 'net rpc file' entrypoint.
5472 * @param argc Standard main() style argc.
5473 * @param argv Standard main() style argv. Initial components are already
5477 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5479 NET_API_STATUS status;
5481 struct functable func[] = {
5486 N_("Close opened file"),
5487 N_("net rpc file close\n"
5488 " Close opened file")
5494 N_("List files opened by user"),
5495 N_("net rpc file user\n"
5496 " List files opened by user")
5503 N_("Display information about opened file"),
5504 N_("net rpc file info\n"
5505 " Display information about opened file")
5508 {NULL, NULL, 0, NULL, NULL}
5511 status = libnetapi_net_init(&c->netapi_ctx);
5515 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5516 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5517 if (c->opt_kerberos) {
5518 libnetapi_set_use_kerberos(c->netapi_ctx);
5522 if (c->display_usage) {
5523 d_printf(_("Usage:\n"));
5524 d_printf(_("net rpc file\n"
5525 " List opened files\n"));
5526 net_display_usage_from_functable(func);
5530 return rpc_file_user(c, argc, argv);
5533 return net_run_function(c, argc, argv, "net rpc file", func);
5537 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5539 * All parameters are provided by the run_rpc_command function, except for
5540 * argc, argv which are passed through.
5542 * @param c A net_context structure.
5543 * @param domain_sid The domain sid acquired from the remote server.
5544 * @param cli A cli_state connected to the server.
5545 * @param mem_ctx Talloc context, destroyed on completion of the function.
5546 * @param argc Standard main() style argc.
5547 * @param argv Standard main() style argv. Initial components are already
5550 * @return Normal NTSTATUS return.
5553 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5554 const struct dom_sid *domain_sid,
5555 const char *domain_name,
5556 struct cli_state *cli,
5557 struct rpc_pipe_client *pipe_hnd,
5558 TALLOC_CTX *mem_ctx,
5562 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5564 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5566 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5567 if (!NT_STATUS_IS_OK(status)) {
5570 if (W_ERROR_IS_OK(result)) {
5571 d_printf(_("\nShutdown successfully aborted\n"));
5572 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5574 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5576 return werror_to_ntstatus(result);
5580 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5582 * All parameters are provided by the run_rpc_command function, except for
5583 * argc, argv which are passed through.
5585 * @param c A net_context structure.
5586 * @param domain_sid The domain sid acquired from the remote server.
5587 * @param cli A cli_state connected to the server.
5588 * @param mem_ctx Talloc context, destroyed on completion of the function.
5589 * @param argc Standard main() style argc.
5590 * @param argv Standard main() style argv. Initial components are already
5593 * @return Normal NTSTATUS return.
5596 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5597 const struct dom_sid *domain_sid,
5598 const char *domain_name,
5599 struct cli_state *cli,
5600 struct rpc_pipe_client *pipe_hnd,
5601 TALLOC_CTX *mem_ctx,
5605 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5607 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5609 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5611 if (!NT_STATUS_IS_OK(result)) {
5612 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5615 if (W_ERROR_IS_OK(werr)) {
5616 d_printf(_("\nShutdown successfully aborted\n"));
5617 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5619 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5621 return werror_to_ntstatus(werr);
5625 * ABORT the shutdown of a remote RPC server.
5627 * @param argc Standard main() style argc.
5628 * @param argv Standard main() style argv. Initial components are already
5631 * @return A shell status integer (0 for success).
5634 static int rpc_shutdown_abort(struct net_context *c, int argc,
5639 if (c->display_usage) {
5641 "net rpc abortshutdown\n"
5644 _("Abort a scheduled shutdown"));
5648 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5649 rpc_shutdown_abort_internals, argc, argv);
5654 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5656 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5657 rpc_reg_shutdown_abort_internals,
5662 * Shut down a remote RPC Server via initshutdown pipe.
5664 * All parameters are provided by the run_rpc_command function, except for
5665 * argc, argv which are passed through.
5667 * @param c A net_context structure.
5668 * @param domain_sid The domain sid acquired from the remote server.
5669 * @param cli A cli_state connected to the server.
5670 * @param mem_ctx Talloc context, destroyed on completion of the function.
5671 * @param argc Standard main() style argc.
5672 * @param argv Standard main() style argv. Initial components are already
5675 * @return Normal NTSTATUS return.
5678 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5679 const struct dom_sid *domain_sid,
5680 const char *domain_name,
5681 struct cli_state *cli,
5682 struct rpc_pipe_client *pipe_hnd,
5683 TALLOC_CTX *mem_ctx,
5687 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5689 const char *msg = N_("This machine will be shutdown shortly");
5690 uint32 timeout = 20;
5691 struct lsa_StringLarge msg_string;
5692 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5694 if (c->opt_comment) {
5695 msg = c->opt_comment;
5697 if (c->opt_timeout) {
5698 timeout = c->opt_timeout;
5701 msg_string.string = msg;
5703 /* create an entry */
5704 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5705 &msg_string, timeout, c->opt_force, c->opt_reboot,
5707 if (!NT_STATUS_IS_OK(status)) {
5710 if (W_ERROR_IS_OK(result)) {
5711 d_printf(_("\nShutdown of remote machine succeeded\n"));
5712 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5714 DEBUG(1,("Shutdown of remote machine failed!\n"));
5716 return werror_to_ntstatus(result);
5720 * Shut down a remote RPC Server via winreg pipe.
5722 * All parameters are provided by the run_rpc_command function, except for
5723 * argc, argv which are passed through.
5725 * @param c A net_context structure.
5726 * @param domain_sid The domain sid acquired from the remote server.
5727 * @param cli A cli_state connected to the server.
5728 * @param mem_ctx Talloc context, destroyed on completion of the function.
5729 * @param argc Standard main() style argc.
5730 * @param argv Standard main() style argv. Initial components are already
5733 * @return Normal NTSTATUS return.
5736 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5737 const struct dom_sid *domain_sid,
5738 const char *domain_name,
5739 struct cli_state *cli,
5740 struct rpc_pipe_client *pipe_hnd,
5741 TALLOC_CTX *mem_ctx,
5745 const char *msg = N_("This machine will be shutdown shortly");
5746 uint32 timeout = 20;
5747 struct lsa_StringLarge msg_string;
5750 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5752 if (c->opt_comment) {
5753 msg = c->opt_comment;
5755 msg_string.string = msg;
5757 if (c->opt_timeout) {
5758 timeout = c->opt_timeout;
5761 /* create an entry */
5762 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5763 &msg_string, timeout, c->opt_force, c->opt_reboot,
5765 if (!NT_STATUS_IS_OK(result)) {
5766 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5770 if (W_ERROR_IS_OK(werr)) {
5771 d_printf(_("\nShutdown of remote machine succeeded\n"));
5773 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5774 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5775 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5777 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5780 return werror_to_ntstatus(werr);
5784 * Shut down a remote RPC server.
5786 * @param argc Standard main() style argc.
5787 * @param argv Standard main() style argv. Initial components are already
5790 * @return A shell status integer (0 for success).
5793 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5797 if (c->display_usage) {
5799 "net rpc shutdown\n"
5802 _("Shut down a remote RPC server"));
5806 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5807 rpc_init_shutdown_internals, argc, argv);
5810 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5811 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5812 rpc_reg_shutdown_internals, argc, argv);
5818 /***************************************************************************
5819 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5820 ***************************************************************************/
5823 * Add interdomain trust account to the RPC server.
5824 * All parameters (except for argc and argv) are passed by run_rpc_command
5827 * @param c A net_context structure.
5828 * @param domain_sid The domain sid acquired from the server.
5829 * @param cli A cli_state connected to the server.
5830 * @param mem_ctx Talloc context, destroyed on completion of the function.
5831 * @param argc Standard main() style argc.
5832 * @param argv Standard main() style argv. Initial components are already
5835 * @return normal NTSTATUS return code.
5838 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5839 const struct dom_sid *domain_sid,
5840 const char *domain_name,
5841 struct cli_state *cli,
5842 struct rpc_pipe_client *pipe_hnd,
5843 TALLOC_CTX *mem_ctx,
5847 struct policy_handle connect_pol, domain_pol, user_pol;
5848 NTSTATUS status, result;
5850 struct lsa_String lsa_acct_name;
5852 uint32 acct_flags=0;
5854 uint32_t access_granted = 0;
5855 union samr_UserInfo info;
5856 unsigned int orig_timeout;
5857 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5858 DATA_BLOB session_key = data_blob_null;
5863 _(" net rpc trustdom add <domain_name> "
5864 "<trust password>\n"));
5865 return NT_STATUS_INVALID_PARAMETER;
5869 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5872 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5873 return NT_STATUS_NO_MEMORY;
5876 if (!strupper_m(acct_name)) {
5877 SAFE_FREE(acct_name);
5878 return NT_STATUS_INVALID_PARAMETER;
5881 init_lsa_String(&lsa_acct_name, acct_name);
5883 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
5884 if (!NT_STATUS_IS_OK(status)) {
5885 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
5886 nt_errstr(status)));
5890 /* Get samr policy handle */
5891 status = dcerpc_samr_Connect2(b, mem_ctx,
5893 MAXIMUM_ALLOWED_ACCESS,
5896 if (!NT_STATUS_IS_OK(status)) {
5899 if (!NT_STATUS_IS_OK(result)) {
5904 /* Get domain policy handle */
5905 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5907 MAXIMUM_ALLOWED_ACCESS,
5908 discard_const_p(struct dom_sid2, domain_sid),
5911 if (!NT_STATUS_IS_OK(status)) {
5914 if (!NT_STATUS_IS_OK(result)) {
5919 /* This call can take a long time - allow the server to time out.
5920 * 35 seconds should do it. */
5922 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5924 /* Create trusting domain's account */
5925 acb_info = ACB_NORMAL;
5926 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5927 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5928 SAMR_USER_ACCESS_SET_PASSWORD |
5929 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5930 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5932 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5941 if (!NT_STATUS_IS_OK(status)) {
5944 /* And restore our original timeout. */
5945 rpccli_set_timeout(pipe_hnd, orig_timeout);
5947 if (!NT_STATUS_IS_OK(result)) {
5949 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5950 acct_name, nt_errstr(result));
5955 struct samr_CryptPassword crypt_pwd;
5957 ZERO_STRUCT(info.info23);
5959 init_samr_CryptPassword(argv[1],
5963 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5964 SAMR_FIELD_NT_PASSWORD_PRESENT;
5965 info.info23.info.acct_flags = ACB_DOMTRUST;
5966 info.info23.password = crypt_pwd;
5968 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5973 if (!NT_STATUS_IS_OK(status)) {
5977 if (!NT_STATUS_IS_OK(result)) {
5979 DEBUG(0,("Could not set trust account password: %s\n",
5980 nt_errstr(result)));
5986 SAFE_FREE(acct_name);
5987 data_blob_clear_free(&session_key);
5992 * Create interdomain trust account for a remote domain.
5994 * @param argc Standard argc.
5995 * @param argv Standard argv without initial components.
5997 * @return Integer status (0 means success).
6000 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
6002 if (argc > 0 && !c->display_usage) {
6003 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6004 rpc_trustdom_add_internals, argc, argv);
6008 _("net rpc trustdom add <domain_name> <trust "
6016 * Remove interdomain trust account from the RPC server.
6017 * All parameters (except for argc and argv) are passed by run_rpc_command
6020 * @param c A net_context structure.
6021 * @param domain_sid The domain sid acquired from the server.
6022 * @param cli A cli_state connected to the server.
6023 * @param mem_ctx Talloc context, destroyed on completion of the function.
6024 * @param argc Standard main() style argc.
6025 * @param argv Standard main() style argv. Initial components are already
6028 * @return normal NTSTATUS return code.
6031 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6032 const struct dom_sid *domain_sid,
6033 const char *domain_name,
6034 struct cli_state *cli,
6035 struct rpc_pipe_client *pipe_hnd,
6036 TALLOC_CTX *mem_ctx,
6040 struct policy_handle connect_pol, domain_pol, user_pol;
6041 NTSTATUS status, result;
6043 struct dom_sid trust_acct_sid;
6044 struct samr_Ids user_rids, name_types;
6045 struct lsa_String lsa_acct_name;
6046 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6051 _(" net rpc trustdom del <domain_name>\n"));
6052 return NT_STATUS_INVALID_PARAMETER;
6056 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6058 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6060 if (acct_name == NULL)
6061 return NT_STATUS_NO_MEMORY;
6063 if (!strupper_m(acct_name)) {
6064 TALLOC_FREE(acct_name);
6065 return NT_STATUS_INVALID_PARAMETER;
6068 /* Get samr policy handle */
6069 status = dcerpc_samr_Connect2(b, mem_ctx,
6071 MAXIMUM_ALLOWED_ACCESS,
6074 if (!NT_STATUS_IS_OK(status)) {
6077 if (!NT_STATUS_IS_OK(result)) {
6082 /* Get domain policy handle */
6083 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6085 MAXIMUM_ALLOWED_ACCESS,
6086 discard_const_p(struct dom_sid2, domain_sid),
6089 if (!NT_STATUS_IS_OK(status)) {
6092 if (!NT_STATUS_IS_OK(result)) {
6097 init_lsa_String(&lsa_acct_name, acct_name);
6099 status = dcerpc_samr_LookupNames(b, mem_ctx,
6106 if (!NT_STATUS_IS_OK(status)) {
6107 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6109 acct_name, nt_errstr(status));
6112 if (!NT_STATUS_IS_OK(result)) {
6114 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6116 acct_name, nt_errstr(result) );
6120 status = dcerpc_samr_OpenUser(b, mem_ctx,
6122 MAXIMUM_ALLOWED_ACCESS,
6126 if (!NT_STATUS_IS_OK(status)) {
6127 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6129 acct_name, nt_errstr(status) );
6133 if (!NT_STATUS_IS_OK(result)) {
6135 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6137 acct_name, nt_errstr(result) );
6141 /* append the rid to the domain sid */
6142 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6146 /* remove the sid */
6148 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6152 if (!NT_STATUS_IS_OK(status)) {
6153 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6154 " on user %s failed %s\n"),
6155 acct_name, nt_errstr(status));
6158 if (!NT_STATUS_IS_OK(result)) {
6160 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6161 " on user %s failed %s\n"),
6162 acct_name, nt_errstr(result) );
6169 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6172 if (!NT_STATUS_IS_OK(status)) {
6173 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6175 acct_name, nt_errstr(status));
6179 if (!NT_STATUS_IS_OK(result)) {
6181 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6183 acct_name, nt_errstr(result) );
6187 if (!NT_STATUS_IS_OK(result)) {
6188 d_printf(_("Could not set trust account password: %s\n"),
6198 * Delete interdomain trust account for a remote domain.
6200 * @param argc Standard argc.
6201 * @param argv Standard argv without initial components.
6203 * @return Integer status (0 means success).
6206 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6208 if (argc > 0 && !c->display_usage) {
6209 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6210 rpc_trustdom_del_internals, argc, argv);
6214 _("net rpc trustdom del <domain>\n"));
6219 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6220 struct cli_state *cli,
6221 TALLOC_CTX *mem_ctx,
6222 const char *domain_name)
6224 char *dc_name = NULL;
6225 const char *buffer = NULL;
6226 struct rpc_pipe_client *netr;
6229 struct dcerpc_binding_handle *b;
6231 /* Use NetServerEnum2 */
6233 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6235 return NT_STATUS_OK;
6238 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6239 for domain %s\n", domain_name));
6241 /* Try netr_GetDcName */
6243 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
6245 if (!NT_STATUS_IS_OK(status)) {
6249 b = netr->binding_handle;
6251 status = dcerpc_netr_GetDcName(b, mem_ctx,
6258 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6262 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6263 for domain %s\n", domain_name));
6265 if (!NT_STATUS_IS_OK(status)) {
6269 return werror_to_ntstatus(result);
6273 * Establish trust relationship to a trusting domain.
6274 * Interdomain account must already be created on remote PDC.
6276 * @param c A net_context structure.
6277 * @param argc Standard argc.
6278 * @param argv Standard argv without initial components.
6280 * @return Integer status (0 means success).
6283 static int rpc_trustdom_establish(struct net_context *c, int argc,
6286 struct cli_state *cli = NULL;
6287 struct sockaddr_storage server_ss;
6288 struct rpc_pipe_client *pipe_hnd = NULL;
6289 struct policy_handle connect_hnd;
6290 TALLOC_CTX *mem_ctx;
6291 NTSTATUS nt_status, result;
6292 struct dom_sid *domain_sid;
6297 union lsa_PolicyInformation *info = NULL;
6298 struct dcerpc_binding_handle *b;
6301 * Connect to \\server\ipc$ as 'our domain' account with password
6304 if (argc != 1 || c->display_usage) {
6307 _("net rpc trustdom establish <domain_name>\n"));
6311 domain_name = smb_xstrdup(argv[0]);
6312 if (!strupper_m(domain_name)) {
6313 SAFE_FREE(domain_name);
6317 /* account name used at first is our domain's name with '$' */
6318 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6321 if (!strupper_m(acct_name)) {
6322 SAFE_FREE(domain_name);
6323 SAFE_FREE(acct_name);
6328 * opt_workgroup will be used by connection functions further,
6329 * hence it should be set to remote domain name instead of ours
6331 if (c->opt_workgroup) {
6332 c->opt_workgroup = smb_xstrdup(domain_name);
6335 c->opt_user_name = acct_name;
6337 /* find the domain controller */
6338 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6339 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6343 /* connect to ipc$ as username/password */
6344 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6345 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6347 /* Is it trusting domain account for sure ? */
6348 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6349 nt_errstr(nt_status)));
6353 /* store who we connected to */
6355 saf_store( domain_name, pdc_name );
6358 * Connect to \\server\ipc$ again (this time anonymously)
6361 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6364 if (NT_STATUS_IS_ERR(nt_status)) {
6365 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6366 domain_name, nt_errstr(nt_status)));
6370 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6371 "domain %s", domain_name))) {
6372 DEBUG(0, ("talloc_init() failed\n"));
6377 /* Make sure we're talking to a proper server */
6379 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6380 if (!NT_STATUS_IS_OK(nt_status)) {
6382 talloc_destroy(mem_ctx);
6387 * Call LsaOpenPolicy and LsaQueryInfo
6390 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6392 if (!NT_STATUS_IS_OK(nt_status)) {
6393 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6395 talloc_destroy(mem_ctx);
6399 b = pipe_hnd->binding_handle;
6401 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6403 if (NT_STATUS_IS_ERR(nt_status)) {
6404 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6405 nt_errstr(nt_status)));
6407 talloc_destroy(mem_ctx);
6411 /* Querying info level 5 */
6413 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6415 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6418 if (NT_STATUS_IS_ERR(nt_status)) {
6419 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6420 nt_errstr(nt_status)));
6422 talloc_destroy(mem_ctx);
6425 if (NT_STATUS_IS_ERR(result)) {
6426 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6427 nt_errstr(result)));
6429 talloc_destroy(mem_ctx);
6433 domain_sid = info->account_domain.sid;
6435 /* There should be actually query info level 3 (following nt serv behaviour),
6436 but I still don't know if it's _really_ necessary */
6439 * Store the password in secrets db
6442 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6443 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6445 talloc_destroy(mem_ctx);
6450 * Close the pipes and clean up
6453 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6454 if (NT_STATUS_IS_ERR(nt_status)) {
6455 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6456 nt_errstr(nt_status)));
6458 talloc_destroy(mem_ctx);
6464 talloc_destroy(mem_ctx);
6466 d_printf(_("Trust to domain %s established\n"), domain_name);
6471 * Revoke trust relationship to the remote domain.
6473 * @param c A net_context structure.
6474 * @param argc Standard argc.
6475 * @param argv Standard argv without initial components.
6477 * @return Integer status (0 means success).
6480 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6486 if (argc < 1 || c->display_usage) {
6489 _("net rpc trustdom revoke <domain_name>\n"
6490 " Revoke trust relationship\n"
6491 " domain_name\tName of domain to revoke trust\n"));
6495 /* generate upper cased domain name */
6496 domain_name = smb_xstrdup(argv[0]);
6497 if (!strupper_m(domain_name)) {
6498 SAFE_FREE(domain_name);
6502 /* delete password of the trust */
6503 if (!pdb_del_trusteddom_pw(domain_name)) {
6504 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6511 SAFE_FREE(domain_name);
6515 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6516 const struct dom_sid *domain_sid,
6517 const char *domain_name,
6518 struct cli_state *cli,
6519 struct rpc_pipe_client *pipe_hnd,
6520 TALLOC_CTX *mem_ctx,
6525 if (!sid_to_fstring(str_sid, domain_sid)) {
6526 return NT_STATUS_UNSUCCESSFUL;
6528 d_printf("%s\n", str_sid);
6529 return NT_STATUS_OK;
6532 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6536 /* convert sid into ascii string */
6537 sid_to_fstring(ascii_sid, dom_sid);
6539 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6542 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6543 TALLOC_CTX *mem_ctx,
6544 struct policy_handle *pol,
6545 struct dom_sid dom_sid,
6546 const char *trusted_dom_name)
6548 NTSTATUS nt_status, result;
6549 union lsa_TrustedDomainInfo *info = NULL;
6550 char *cleartextpwd = NULL;
6551 DATA_BLOB session_key;
6552 DATA_BLOB data = data_blob_null;
6553 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6555 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6558 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6561 if (NT_STATUS_IS_ERR(nt_status)) {
6562 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6563 nt_errstr(nt_status)));
6566 if (NT_STATUS_IS_ERR(result)) {
6568 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6569 nt_errstr(result)));
6573 data = data_blob(info->password.password->data,
6574 info->password.password->length);
6576 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6577 if (!NT_STATUS_IS_OK(nt_status)) {
6578 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6582 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6583 data_blob_free(&session_key);
6585 if (cleartextpwd == NULL) {
6586 DEBUG(0,("retrieved NULL password\n"));
6587 nt_status = NT_STATUS_UNSUCCESSFUL;
6591 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6592 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6593 nt_status = NT_STATUS_UNSUCCESSFUL;
6597 #ifdef DEBUG_PASSWORD
6598 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6599 "password: [%s]\n", trusted_dom_name,
6600 sid_string_dbg(&dom_sid), cleartextpwd));
6604 SAFE_FREE(cleartextpwd);
6605 data_blob_free(&data);
6610 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6613 /* common variables */
6614 TALLOC_CTX* mem_ctx;
6615 struct cli_state *cli = NULL;
6616 struct rpc_pipe_client *pipe_hnd = NULL;
6617 NTSTATUS nt_status, result;
6618 const char *domain_name = NULL;
6619 struct policy_handle connect_hnd;
6620 union lsa_PolicyInformation *info = NULL;
6622 /* trusted domains listing variables */
6623 unsigned int enum_ctx = 0;
6625 struct lsa_DomainList dom_list;
6627 struct dcerpc_binding_handle *b;
6629 if (c->display_usage) {
6631 "net rpc trustdom vampire\n"
6634 _("Vampire trust relationship from remote server"));
6639 * Listing trusted domains (stored in secrets.tdb, if local)
6642 mem_ctx = talloc_init("trust relationships vampire");
6645 * set domain and pdc name to local samba server (default)
6646 * or to remote one given in command line
6649 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6650 domain_name = c->opt_workgroup;
6651 c->opt_target_workgroup = c->opt_workgroup;
6653 fstrcpy(pdc_name, lp_netbios_name());
6654 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6655 c->opt_target_workgroup = domain_name;
6658 /* open \PIPE\lsarpc and open policy handle */
6659 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6660 if (!NT_STATUS_IS_OK(nt_status)) {
6661 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6662 nt_errstr(nt_status)));
6663 talloc_destroy(mem_ctx);
6667 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6669 if (!NT_STATUS_IS_OK(nt_status)) {
6670 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6671 nt_errstr(nt_status) ));
6673 talloc_destroy(mem_ctx);
6677 b = pipe_hnd->binding_handle;
6679 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6681 if (NT_STATUS_IS_ERR(nt_status)) {
6682 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6683 nt_errstr(nt_status)));
6685 talloc_destroy(mem_ctx);
6689 /* query info level 5 to obtain sid of a domain being queried */
6690 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6692 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6696 if (NT_STATUS_IS_ERR(nt_status)) {
6697 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6698 nt_errstr(nt_status)));
6700 talloc_destroy(mem_ctx);
6703 if (NT_STATUS_IS_ERR(result)) {
6704 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6705 nt_errstr(result)));
6707 talloc_destroy(mem_ctx);
6712 * Keep calling LsaEnumTrustdom over opened pipe until
6713 * the end of enumeration is reached
6716 d_printf(_("Vampire trusted domains:\n\n"));
6719 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6725 if (NT_STATUS_IS_ERR(nt_status)) {
6726 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6727 nt_errstr(nt_status)));
6729 talloc_destroy(mem_ctx);
6732 if (NT_STATUS_IS_ERR(result)) {
6734 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6735 nt_errstr(result)));
6737 talloc_destroy(mem_ctx);
6742 for (i = 0; i < dom_list.count; i++) {
6744 print_trusted_domain(dom_list.domains[i].sid,
6745 dom_list.domains[i].name.string);
6747 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6748 *dom_list.domains[i].sid,
6749 dom_list.domains[i].name.string);
6750 if (!NT_STATUS_IS_OK(nt_status)) {
6752 talloc_destroy(mem_ctx);
6758 * in case of no trusted domains say something rather
6759 * than just display blank line
6761 if (!dom_list.count) d_printf(_("none\n"));
6763 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6765 /* close this connection before doing next one */
6766 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6767 if (NT_STATUS_IS_ERR(nt_status)) {
6768 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6769 nt_errstr(nt_status)));
6771 talloc_destroy(mem_ctx);
6775 /* close lsarpc pipe and connection to IPC$ */
6778 talloc_destroy(mem_ctx);
6782 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6784 /* common variables */
6785 TALLOC_CTX* mem_ctx;
6786 struct cli_state *cli = NULL, *remote_cli = NULL;
6787 struct rpc_pipe_client *pipe_hnd = NULL;
6788 NTSTATUS nt_status, result;
6789 const char *domain_name = NULL;
6790 struct dom_sid *queried_dom_sid;
6791 int ascii_dom_name_len;
6792 struct policy_handle connect_hnd;
6793 union lsa_PolicyInformation *info = NULL;
6794 struct dcerpc_binding_handle *b = NULL;
6796 /* trusted domains listing variables */
6797 unsigned int num_domains, enum_ctx = 0;
6799 struct lsa_DomainList dom_list;
6803 /* trusting domains listing variables */
6804 struct policy_handle domain_hnd;
6805 struct samr_SamArray *trusts = NULL;
6807 if (c->display_usage) {
6809 "net rpc trustdom list\n"
6812 _("List incoming and outgoing trust relationships"));
6817 * Listing trusted domains (stored in secrets.tdb, if local)
6820 mem_ctx = talloc_init("trust relationships listing");
6823 * set domain and pdc name to local samba server (default)
6824 * or to remote one given in command line
6827 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6828 domain_name = c->opt_workgroup;
6829 c->opt_target_workgroup = c->opt_workgroup;
6831 fstrcpy(pdc_name, lp_netbios_name());
6832 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6833 c->opt_target_workgroup = domain_name;
6836 /* open \PIPE\lsarpc and open policy handle */
6837 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6838 if (!NT_STATUS_IS_OK(nt_status)) {
6839 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6840 nt_errstr(nt_status)));
6841 talloc_destroy(mem_ctx);
6845 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
6847 if (!NT_STATUS_IS_OK(nt_status)) {
6848 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6849 nt_errstr(nt_status) ));
6851 talloc_destroy(mem_ctx);
6855 b = pipe_hnd->binding_handle;
6857 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6859 if (NT_STATUS_IS_ERR(nt_status)) {
6860 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6861 nt_errstr(nt_status)));
6863 talloc_destroy(mem_ctx);
6867 /* query info level 5 to obtain sid of a domain being queried */
6868 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6870 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6874 if (NT_STATUS_IS_ERR(nt_status)) {
6875 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6876 nt_errstr(nt_status)));
6878 talloc_destroy(mem_ctx);
6881 if (NT_STATUS_IS_ERR(result)) {
6882 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6883 nt_errstr(result)));
6885 talloc_destroy(mem_ctx);
6889 queried_dom_sid = info->account_domain.sid;
6892 * Keep calling LsaEnumTrustdom over opened pipe until
6893 * the end of enumeration is reached
6896 d_printf(_("Trusted domains list:\n\n"));
6898 found_domain = false;
6901 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6907 if (NT_STATUS_IS_ERR(nt_status)) {
6908 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6909 nt_errstr(nt_status)));
6911 talloc_destroy(mem_ctx);
6914 if (NT_STATUS_IS_ERR(result)) {
6915 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6916 nt_errstr(result)));
6918 talloc_destroy(mem_ctx);
6923 for (i = 0; i < dom_list.count; i++) {
6924 print_trusted_domain(dom_list.domains[i].sid,
6925 dom_list.domains[i].name.string);
6926 found_domain = true;
6930 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6933 * in case of no trusted domains say something rather
6934 * than just display blank line
6936 if (!found_domain) {
6937 d_printf(_("none\n"));
6940 /* close this connection before doing next one */
6941 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6942 if (NT_STATUS_IS_ERR(nt_status)) {
6943 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6944 nt_errstr(nt_status)));
6946 talloc_destroy(mem_ctx);
6950 TALLOC_FREE(pipe_hnd);
6953 * Listing trusting domains (stored in passdb backend, if local)
6956 d_printf(_("\nTrusting domains list:\n\n"));
6959 * Open \PIPE\samr and get needed policy handles
6961 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
6963 if (!NT_STATUS_IS_OK(nt_status)) {
6964 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6966 talloc_destroy(mem_ctx);
6970 b = pipe_hnd->binding_handle;
6973 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6975 SAMR_ACCESS_LOOKUP_DOMAIN,
6978 if (!NT_STATUS_IS_OK(nt_status)) {
6979 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6980 nt_errstr(nt_status)));
6982 talloc_destroy(mem_ctx);
6985 if (!NT_STATUS_IS_OK(result)) {
6987 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6988 nt_errstr(result)));
6990 talloc_destroy(mem_ctx);
6994 /* SamrOpenDomain - we have to open domain policy handle in order to be
6995 able to enumerate accounts*/
6996 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6998 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
7002 if (!NT_STATUS_IS_OK(nt_status)) {
7003 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7004 nt_errstr(nt_status)));
7006 talloc_destroy(mem_ctx);
7009 if (!NT_STATUS_IS_OK(result)) {
7011 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
7012 nt_errstr(result)));
7014 talloc_destroy(mem_ctx);
7019 * perform actual enumeration
7022 found_domain = false;
7024 enum_ctx = 0; /* reset enumeration context from last enumeration */
7027 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
7035 if (NT_STATUS_IS_ERR(nt_status)) {
7036 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7037 nt_errstr(nt_status)));
7039 talloc_destroy(mem_ctx);
7042 if (NT_STATUS_IS_ERR(result)) {
7044 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7045 nt_errstr(result)));
7047 talloc_destroy(mem_ctx);
7051 for (i = 0; i < num_domains; i++) {
7053 char *str = discard_const_p(char, trusts->entries[i].name.string);
7055 found_domain = true;
7058 * get each single domain's sid (do we _really_ need this ?):
7059 * 1) connect to domain's pdc
7060 * 2) query the pdc for domain's sid
7063 /* get rid of '$' tail */
7064 ascii_dom_name_len = strlen(str);
7065 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7066 str[ascii_dom_name_len - 1] = '\0';
7068 /* set opt_* variables to remote domain */
7069 if (!strupper_m(str)) {
7071 talloc_destroy(mem_ctx);
7074 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7075 c->opt_target_workgroup = c->opt_workgroup;
7077 d_printf("%-20s", str);
7079 /* connect to remote domain controller */
7080 nt_status = net_make_ipc_connection(c,
7081 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7083 if (NT_STATUS_IS_OK(nt_status)) {
7084 /* query for domain's sid */
7085 if (run_rpc_command(
7087 &ndr_table_lsarpc, 0,
7088 rpc_query_domain_sid, argc,
7090 d_printf(_("strange - couldn't get domain's sid\n"));
7092 cli_shutdown(remote_cli);
7095 d_fprintf(stderr, _("domain controller is not "
7096 "responding: %s\n"),
7097 nt_errstr(nt_status));
7098 d_printf(_("couldn't get domain's sid\n"));
7102 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7104 if (!found_domain) {
7108 /* close opened samr and domain policy handles */
7109 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7110 if (!NT_STATUS_IS_OK(nt_status)) {
7111 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7114 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7115 if (!NT_STATUS_IS_OK(nt_status)) {
7116 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7119 /* close samr pipe and connection to IPC$ */
7122 talloc_destroy(mem_ctx);
7127 * Entrypoint for 'net rpc trustdom' code.
7129 * @param argc Standard argc.
7130 * @param argv Standard argv without initial components.
7132 * @return Integer status (0 means success).
7135 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7137 struct functable func[] = {
7142 N_("Add trusting domain's account"),
7143 N_("net rpc trustdom add\n"
7144 " Add trusting domain's account")
7150 N_("Remove trusting domain's account"),
7151 N_("net rpc trustdom del\n"
7152 " Remove trusting domain's account")
7156 rpc_trustdom_establish,
7158 N_("Establish outgoing trust relationship"),
7159 N_("net rpc trustdom establish\n"
7160 " Establish outgoing trust relationship")
7164 rpc_trustdom_revoke,
7166 N_("Revoke outgoing trust relationship"),
7167 N_("net rpc trustdom revoke\n"
7168 " Revoke outgoing trust relationship")
7174 N_("List in- and outgoing domain trusts"),
7175 N_("net rpc trustdom list\n"
7176 " List in- and outgoing domain trusts")
7180 rpc_trustdom_vampire,
7182 N_("Vampire trusts from remote server"),
7183 N_("net rpc trustdom vampire\n"
7184 " Vampire trusts from remote server")
7186 {NULL, NULL, 0, NULL, NULL}
7189 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7193 * Check if a server will take rpc commands
7194 * @param flags Type of server to connect to (PDC, DMB, localhost)
7195 * if the host is not explicitly specified
7196 * @return bool (true means rpc supported)
7198 bool net_rpc_check(struct net_context *c, unsigned flags)
7200 struct cli_state *cli;
7202 struct sockaddr_storage server_ss;
7203 char *server_name = NULL;
7206 /* flags (i.e. server type) may depend on command */
7207 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7210 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7211 lp_netbios_name(), SMB_SIGNING_DEFAULT,
7213 if (!NT_STATUS_IS_OK(status)) {
7216 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7218 if (!NT_STATUS_IS_OK(status))
7220 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7229 /* dump sam database via samsync rpc calls */
7230 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7231 if (c->display_usage) {
7236 _("Dump remote SAM database"));
7240 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7241 NET_FLAGS_ANONYMOUS,
7242 rpc_samdump_internals, argc, argv);
7245 /* syncronise sam database via samsync rpc calls */
7246 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7248 struct functable func[] = {
7253 N_("Dump remote SAM database to ldif"),
7254 N_("net rpc vampire ldif\n"
7255 " Dump remote SAM database to LDIF file or "
7262 N_("Dump remote SAM database to Kerberos Keytab"),
7263 N_("net rpc vampire keytab\n"
7264 " Dump remote SAM database to Kerberos keytab "
7271 N_("Dump remote SAM database to passdb"),
7272 N_("net rpc vampire passdb\n"
7273 " Dump remote SAM database to passdb")
7276 {NULL, NULL, 0, NULL, NULL}
7280 if (c->display_usage) {
7285 _("Vampire remote SAM database"));
7289 return rpc_vampire_passdb(c, argc, argv);
7292 return net_run_function(c, argc, argv, "net rpc vampire", func);
7296 * Migrate everything from a print server.
7298 * @param c A net_context structure.
7299 * @param argc Standard main() style argc.
7300 * @param argv Standard main() style argv. Initial components are already
7303 * @return A shell status integer (0 for success).
7305 * The order is important !
7306 * To successfully add drivers the print queues have to exist !
7307 * Applying ACLs should be the last step, because you're easily locked out.
7310 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7315 if (c->display_usage) {
7317 "net rpc printer migrate all\n"
7320 _("Migrate everything from a print server"));
7325 d_printf(_("no server to migrate\n"));
7329 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7330 rpc_printer_migrate_printers_internals, argc,
7335 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7336 rpc_printer_migrate_drivers_internals, argc,
7341 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7342 rpc_printer_migrate_forms_internals, argc, argv);
7346 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7347 rpc_printer_migrate_settings_internals, argc,
7352 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7353 rpc_printer_migrate_security_internals, argc,
7359 * Migrate print drivers from a print server.
7361 * @param c A net_context structure.
7362 * @param argc Standard main() style argc.
7363 * @param argv Standard main() style argv. Initial components are already
7366 * @return A shell status integer (0 for success).
7368 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7371 if (c->display_usage) {
7373 "net rpc printer migrate drivers\n"
7376 _("Migrate print-drivers from a print-server"));
7381 d_printf(_("no server to migrate\n"));
7385 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7386 rpc_printer_migrate_drivers_internals,
7391 * Migrate print-forms from a print-server.
7393 * @param c A net_context structure.
7394 * @param argc Standard main() style argc.
7395 * @param argv Standard main() style argv. Initial components are already
7398 * @return A shell status integer (0 for success).
7400 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7403 if (c->display_usage) {
7405 "net rpc printer migrate forms\n"
7408 _("Migrate print-forms from a print-server"));
7413 d_printf(_("no server to migrate\n"));
7417 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7418 rpc_printer_migrate_forms_internals,
7423 * Migrate printers from a print-server.
7425 * @param c A net_context structure.
7426 * @param argc Standard main() style argc.
7427 * @param argv Standard main() style argv. Initial components are already
7430 * @return A shell status integer (0 for success).
7432 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7435 if (c->display_usage) {
7437 "net rpc printer migrate printers\n"
7440 _("Migrate printers from a print-server"));
7445 d_printf(_("no server to migrate\n"));
7449 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7450 rpc_printer_migrate_printers_internals,
7455 * Migrate printer-ACLs from a print-server
7457 * @param c A net_context structure.
7458 * @param argc Standard main() style argc.
7459 * @param argv Standard main() style argv. Initial components are already
7462 * @return A shell status integer (0 for success).
7464 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7467 if (c->display_usage) {
7469 "net rpc printer migrate security\n"
7472 _("Migrate printer-ACLs from a print-server"));
7477 d_printf(_("no server to migrate\n"));
7481 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7482 rpc_printer_migrate_security_internals,
7487 * Migrate printer-settings from a print-server.
7489 * @param c A net_context structure.
7490 * @param argc Standard main() style argc.
7491 * @param argv Standard main() style argv. Initial components are already
7494 * @return A shell status integer (0 for success).
7496 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7499 if (c->display_usage) {
7501 "net rpc printer migrate settings\n"
7504 _("Migrate printer-settings from a "
7510 d_printf(_("no server to migrate\n"));
7514 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7515 rpc_printer_migrate_settings_internals,
7520 * 'net rpc printer' entrypoint.
7522 * @param c A net_context structure.
7523 * @param argc Standard main() style argc.
7524 * @param argv Standard main() style argv. Initial components are already
7528 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7531 /* ouch: when addriver and setdriver are called from within
7532 rpc_printer_migrate_drivers_internals, the printer-queue already
7535 struct functable func[] = {
7538 rpc_printer_migrate_all,
7540 N_("Migrate all from remote to local print server"),
7541 N_("net rpc printer migrate all\n"
7542 " Migrate all from remote to local print server")
7546 rpc_printer_migrate_drivers,
7548 N_("Migrate drivers to local server"),
7549 N_("net rpc printer migrate drivers\n"
7550 " Migrate drivers to local server")
7554 rpc_printer_migrate_forms,
7556 N_("Migrate froms to local server"),
7557 N_("net rpc printer migrate forms\n"
7558 " Migrate froms to local server")
7562 rpc_printer_migrate_printers,
7564 N_("Migrate printers to local server"),
7565 N_("net rpc printer migrate printers\n"
7566 " Migrate printers to local server")
7570 rpc_printer_migrate_security,
7572 N_("Mirgate printer ACLs to local server"),
7573 N_("net rpc printer migrate security\n"
7574 " Mirgate printer ACLs to local server")
7578 rpc_printer_migrate_settings,
7580 N_("Migrate printer settings to local server"),
7581 N_("net rpc printer migrate settings\n"
7582 " Migrate printer settings to local server")
7584 {NULL, NULL, 0, NULL, NULL}
7587 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7592 * List printers on a remote RPC server.
7594 * @param c A net_context structure.
7595 * @param argc Standard main() style argc.
7596 * @param argv Standard main() style argv. Initial components are already
7599 * @return A shell status integer (0 for success).
7601 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7603 if (c->display_usage) {
7605 "net rpc printer list\n"
7608 _("List printers on a remote RPC server"));
7612 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7613 rpc_printer_list_internals,
7618 * List printer-drivers on a remote RPC server.
7620 * @param c A net_context structure.
7621 * @param argc Standard main() style argc.
7622 * @param argv Standard main() style argv. Initial components are already
7625 * @return A shell status integer (0 for success).
7627 static int rpc_printer_driver_list(struct net_context *c, int argc,
7630 if (c->display_usage) {
7632 "net rpc printer driver\n"
7635 _("List printer-drivers on a remote RPC server"));
7639 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7640 rpc_printer_driver_list_internals,
7645 * Publish printer in ADS via MSRPC.
7647 * @param c A net_context structure.
7648 * @param argc Standard main() style argc.
7649 * @param argv Standard main() style argv. Initial components are already
7652 * @return A shell status integer (0 for success).
7654 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7657 if (c->display_usage) {
7659 "net rpc printer publish publish\n"
7662 _("Publish printer in ADS via MSRPC"));
7666 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7667 rpc_printer_publish_publish_internals,
7672 * Update printer in ADS via MSRPC.
7674 * @param c A net_context structure.
7675 * @param argc Standard main() style argc.
7676 * @param argv Standard main() style argv. Initial components are already
7679 * @return A shell status integer (0 for success).
7681 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7683 if (c->display_usage) {
7685 "net rpc printer publish update\n"
7688 _("Update printer in ADS via MSRPC"));
7692 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7693 rpc_printer_publish_update_internals,
7698 * UnPublish printer in ADS via MSRPC.
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_publish_unpublish(struct net_context *c, int argc,
7710 if (c->display_usage) {
7712 "net rpc printer publish unpublish\n"
7715 _("UnPublish printer in ADS via MSRPC"));
7719 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7720 rpc_printer_publish_unpublish_internals,
7725 * List published printers via MSRPC.
7727 * @param c A net_context structure.
7728 * @param argc Standard main() style argc.
7729 * @param argv Standard main() style argv. Initial components are already
7732 * @return A shell status integer (0 for success).
7734 static int rpc_printer_publish_list(struct net_context *c, int argc,
7737 if (c->display_usage) {
7739 "net rpc printer publish list\n"
7742 _("List published printers via MSRPC"));
7746 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7747 rpc_printer_publish_list_internals,
7753 * Publish printer in ADS.
7755 * @param c A net_context structure.
7756 * @param argc Standard main() style argc.
7757 * @param argv Standard main() style argv. Initial components are already
7760 * @return A shell status integer (0 for success).
7762 static int rpc_printer_publish(struct net_context *c, int argc,
7766 struct functable func[] = {
7769 rpc_printer_publish_publish,
7771 N_("Publish printer in AD"),
7772 N_("net rpc printer publish publish\n"
7773 " Publish printer in AD")
7777 rpc_printer_publish_update,
7779 N_("Update printer in AD"),
7780 N_("net rpc printer publish update\n"
7781 " Update printer in AD")
7785 rpc_printer_publish_unpublish,
7787 N_("Unpublish printer"),
7788 N_("net rpc printer publish unpublish\n"
7789 " Unpublish printer")
7793 rpc_printer_publish_list,
7795 N_("List published printers"),
7796 N_("net rpc printer publish list\n"
7797 " List published printers")
7799 {NULL, NULL, 0, NULL, NULL}
7803 if (c->display_usage) {
7804 d_printf(_("Usage:\n"));
7805 d_printf(_("net rpc printer publish\n"
7806 " List published printers\n"
7807 " Alias of net rpc printer publish "
7809 net_display_usage_from_functable(func);
7812 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7813 rpc_printer_publish_list_internals,
7817 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7823 * Display rpc printer help page.
7825 * @param c A net_context structure.
7826 * @param argc Standard main() style argc.
7827 * @param argv Standard main() style argv. Initial components are already
7830 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7832 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7833 "\tlists all printers on print-server\n\n"));
7834 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7835 "\tlists all printer-drivers on print-server\n\n"));
7836 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7837 "\tpublishes printer settings in Active Directory\n"
7838 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7839 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7840 "\n\tmigrates printers from remote to local server\n\n"));
7841 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7842 "\n\tmigrates printer-settings from remote to local server\n\n"));
7843 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7844 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7845 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7846 "\n\tmigrates printer-forms from remote to local server\n\n"));
7847 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7848 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7849 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7850 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7851 "\tremote to local print-server\n\n"));
7852 net_common_methods_usage(c, argc, argv);
7853 net_common_flags_usage(c, argc, argv);
7855 "\t-v or --verbose\t\t\tgive verbose output\n"
7856 "\t --destination\t\tmigration target server (default: localhost)\n"));
7862 * 'net rpc printer' entrypoint.
7864 * @param c A net_context structure.
7865 * @param argc Standard main() style argc.
7866 * @param argv Standard main() style argv. Initial components are already
7869 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7871 struct functable func[] = {
7876 N_("List all printers on print server"),
7877 N_("net rpc printer list\n"
7878 " List all printers on print server")
7882 rpc_printer_migrate,
7884 N_("Migrate printer to local server"),
7885 N_("net rpc printer migrate\n"
7886 " Migrate printer to local server")
7890 rpc_printer_driver_list,
7892 N_("List printer drivers"),
7893 N_("net rpc printer driver\n"
7894 " List printer drivers")
7898 rpc_printer_publish,
7900 N_("Publish printer in AD"),
7901 N_("net rpc printer publish\n"
7902 " Publish printer in AD")
7904 {NULL, NULL, 0, NULL, NULL}
7908 if (c->display_usage) {
7909 d_printf(_("Usage:\n"));
7910 d_printf(_("net rpc printer\n"
7911 " List printers\n"));
7912 net_display_usage_from_functable(func);
7915 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7916 rpc_printer_list_internals,
7920 return net_run_function(c, argc, argv, "net rpc printer", func);
7924 * 'net rpc' entrypoint.
7926 * @param c A net_context structure.
7927 * @param argc Standard main() style argc.
7928 * @param argv Standard main() style argv. Initial components are already
7932 int net_rpc(struct net_context *c, int argc, const char **argv)
7934 NET_API_STATUS status;
7936 struct functable func[] = {
7941 N_("Modify global audit settings"),
7942 N_("net rpc audit\n"
7943 " Modify global audit settings")
7949 N_("Show basic info about a domain"),
7951 " Show basic info about a domain")
7957 N_("Join a domain"),
7965 N_("Join a domain created in server manager"),
7966 N_("net rpc oldjoin\n"
7967 " Join a domain created in server manager")
7973 N_("Test that a join is valid"),
7974 N_("net rpc testjoin\n"
7975 " Test that a join is valid")
7981 N_("List/modify users"),
7983 " List/modify users")
7989 N_("Change a user password"),
7990 N_("net rpc password\n"
7991 " Change a user password\n"
7992 " Alias for net rpc user password")
7998 N_("List/modify groups"),
7999 N_("net rpc group\n"
8000 " List/modify groups")
8006 N_("List/modify shares"),
8007 N_("net rpc share\n"
8008 " List/modify shares")
8014 N_("List open files"),
8022 N_("List/modify printers"),
8023 N_("net rpc printer\n"
8024 " List/modify printers")
8028 net_rpc_changetrustpw,
8030 N_("Change trust account password"),
8031 N_("net rpc changetrustpw\n"
8032 " Change trust account password")
8038 N_("Modify domain trusts"),
8039 N_("net rpc trustdom\n"
8040 " Modify domain trusts")
8046 N_("Abort a remote shutdown"),
8047 N_("net rpc abortshutdown\n"
8048 " Abort a remote shutdown")
8054 N_("Shutdown a remote server"),
8055 N_("net rpc shutdown\n"
8056 " Shutdown a remote server")
8062 N_("Dump SAM data of remote NT PDC"),
8063 N_("net rpc samdump\n"
8064 " Dump SAM data of remote NT PDC")
8070 N_("Sync a remote NT PDC's data into local passdb"),
8071 N_("net rpc vampire\n"
8072 " Sync a remote NT PDC's data into local passdb")
8078 N_("Fetch the domain sid into local secrets.tdb"),
8079 N_("net rpc getsid\n"
8080 " Fetch the domain sid into local secrets.tdb")
8086 N_("Manage privileges assigned to SID"),
8087 N_("net rpc rights\n"
8088 " Manage privileges assigned to SID")
8094 N_("Start/stop/query remote services"),
8095 N_("net rpc service\n"
8096 " Start/stop/query remote services")
8102 N_("Manage registry hives"),
8103 N_("net rpc registry\n"
8104 " Manage registry hives")
8110 N_("Open interactive shell on remote server"),
8111 N_("net rpc shell\n"
8112 " Open interactive shell on remote server")
8118 N_("Manage trusts"),
8119 N_("net rpc trust\n"
8126 N_("Configure a remote samba server"),
8128 " Configure a remote samba server")
8130 {NULL, NULL, 0, NULL, NULL}
8133 status = libnetapi_net_init(&c->netapi_ctx);
8137 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8138 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8139 if (c->opt_kerberos) {
8140 libnetapi_set_use_kerberos(c->netapi_ctx);
8142 if (c->opt_ccache) {
8143 libnetapi_set_use_ccache(c->netapi_ctx);
8146 return net_run_function(c, argc, argv, "net rpc", func);