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 "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/cli_samr.h"
27 #include "rpc_client/cli_samr.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/cli_lsa.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/cli_netlogon.h"
32 #include "../librpc/gen_ndr/cli_srvsvc.h"
33 #include "../librpc/gen_ndr/cli_spoolss.h"
34 #include "../librpc/gen_ndr/cli_initshutdown.h"
35 #include "../librpc/gen_ndr/cli_winreg.h"
37 #include "lib/netapi/netapi.h"
38 #include "rpc_client/init_lsa.h"
39 #include "../libcli/security/security.h"
41 static int net_mode_share;
42 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
47 * @brief RPC based subcommands for the 'net' utility.
49 * This file should contain much of the functionality that used to
50 * be found in rpcclient, execpt that the commands should change
51 * less often, and the fucntionality should be sane (the user is not
52 * expected to know a rid/sid before they conduct an operation etc.)
54 * @todo Perhaps eventually these should be split out into a number
55 * of files, as this could get quite big.
60 * Many of the RPC functions need the domain sid. This function gets
61 * it at the start of every run
63 * @param cli A cli_state already connected to the remote machine
65 * @return The Domain SID of the remote machine.
68 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
69 struct dom_sid **domain_sid,
70 const char **domain_name)
72 struct rpc_pipe_client *lsa_pipe = NULL;
73 struct policy_handle pol;
74 NTSTATUS result = NT_STATUS_OK;
75 union lsa_PolicyInformation *info = NULL;
77 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
79 if (!NT_STATUS_IS_OK(result)) {
80 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
84 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
85 SEC_FLAG_MAXIMUM_ALLOWED,
87 if (!NT_STATUS_IS_OK(result)) {
88 d_fprintf(stderr, "open_policy %s: %s\n",
94 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
96 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
98 if (!NT_STATUS_IS_OK(result)) {
99 d_fprintf(stderr, "lsaquery %s: %s\n",
105 *domain_name = info->account_domain.name.string;
106 *domain_sid = info->account_domain.sid;
108 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
109 TALLOC_FREE(lsa_pipe);
115 * Run a single RPC command, from start to finish.
117 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
118 * @param conn_flag a NET_FLAG_ combination. Passed to
119 * net_make_ipc_connection.
120 * @param argc Standard main() style argc.
121 * @param argv Standard main() style argv. Initial components are already
123 * @return A shell status integer (0 for success).
126 int run_rpc_command(struct net_context *c,
127 struct cli_state *cli_arg,
128 const struct ndr_syntax_id *interface,
134 struct cli_state *cli = NULL;
135 struct rpc_pipe_client *pipe_hnd = NULL;
138 struct dom_sid *domain_sid;
139 const char *domain_name;
142 /* make use of cli_state handed over as an argument, if possible */
144 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
145 if (!NT_STATUS_IS_OK(nt_status)) {
146 DEBUG(1, ("failed to make ipc connection: %s\n",
147 nt_errstr(nt_status)));
160 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
161 DEBUG(0, ("talloc_init() failed\n"));
165 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
167 if (!NT_STATUS_IS_OK(nt_status)) {
171 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
172 if (lp_client_schannel()
173 && (ndr_syntax_id_equal(interface,
174 &ndr_table_netlogon.syntax_id))) {
175 /* Always try and create an schannel netlogon pipe. */
176 nt_status = cli_rpc_pipe_open_schannel(
177 cli, interface, NCACN_NP,
178 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
180 if (!NT_STATUS_IS_OK(nt_status)) {
181 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
182 nt_errstr(nt_status) ));
186 if (conn_flags & NET_FLAGS_SEAL) {
187 nt_status = cli_rpc_pipe_open_ntlmssp(
189 (conn_flags & NET_FLAGS_TCP) ?
190 NCACN_IP_TCP : NCACN_NP,
191 DCERPC_AUTH_LEVEL_PRIVACY,
192 lp_workgroup(), c->opt_user_name,
193 c->opt_password, &pipe_hnd);
195 nt_status = cli_rpc_pipe_open_noauth(
199 if (!NT_STATUS_IS_OK(nt_status)) {
200 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
201 get_pipe_name_from_syntax(
202 talloc_tos(), interface),
203 nt_errstr(nt_status) ));
209 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
211 if (!NT_STATUS_IS_OK(nt_status)) {
212 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
215 DEBUG(5, ("rpc command function succedded\n"));
218 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
220 TALLOC_FREE(pipe_hnd);
225 /* close the connection only if it was opened here */
230 talloc_destroy(mem_ctx);
235 * Force a change of the trust acccount password.
237 * All parameters are provided by the run_rpc_command function, except for
238 * argc, argv which are passed through.
240 * @param domain_sid The domain sid acquired from the remote server.
241 * @param cli A cli_state connected to the server.
242 * @param mem_ctx Talloc context, destroyed on completion of the function.
243 * @param argc Standard main() style argc.
244 * @param argv Standard main() style argv. Initial components are already
247 * @return Normal NTSTATUS return.
250 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
251 const struct dom_sid *domain_sid,
252 const char *domain_name,
253 struct cli_state *cli,
254 struct rpc_pipe_client *pipe_hnd,
261 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
262 if (!NT_STATUS_IS_OK(status)) {
263 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
272 * Force a change of the trust acccount password.
274 * @param argc Standard main() style argc.
275 * @param argv Standard main() style argv. Initial components are already
278 * @return A shell status integer (0 for success).
281 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
283 if (c->display_usage) {
285 "net rpc changetrustpw\n"
288 _("Change the machine trust password"));
292 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
293 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
294 rpc_changetrustpw_internals,
299 * Join a domain, the old way.
301 * This uses 'machinename' as the inital password, and changes it.
303 * The password should be created with 'server manager' or equiv first.
305 * All parameters are provided by the run_rpc_command function, except for
306 * argc, argv which are passed through.
308 * @param domain_sid The domain sid acquired from the remote server.
309 * @param cli A cli_state connected to the server.
310 * @param mem_ctx Talloc context, destroyed on completion of the function.
311 * @param argc Standard main() style argc.
312 * @param argv Standard main() style argv. Initial components are already
315 * @return Normal NTSTATUS return.
318 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
319 const struct dom_sid *domain_sid,
320 const char *domain_name,
321 struct cli_state *cli,
322 struct rpc_pipe_client *pipe_hnd,
328 fstring trust_passwd;
329 unsigned char orig_trust_passwd_hash[16];
331 enum netr_SchannelType sec_channel_type;
333 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
335 if (!NT_STATUS_IS_OK(result)) {
336 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
339 nt_errstr(result) ));
344 check what type of join - if the user want's to join as
345 a BDC, the server must agree that we are a BDC.
348 sec_channel_type = get_sec_channel_type(argv[0]);
350 sec_channel_type = get_sec_channel_type(NULL);
353 fstrcpy(trust_passwd, global_myname());
354 strlower_m(trust_passwd);
357 * Machine names can be 15 characters, but the max length on
358 * a password is 14. --jerry
361 trust_passwd[14] = '\0';
363 E_md4hash(trust_passwd, orig_trust_passwd_hash);
365 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
367 orig_trust_passwd_hash,
370 if (NT_STATUS_IS_OK(result))
371 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
374 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
375 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
376 result = NT_STATUS_UNSUCCESSFUL;
383 * Join a domain, the old way.
385 * @param argc Standard main() style argc.
386 * @param argv Standard main() style argv. Initial components are already
389 * @return A shell status integer (0 for success).
392 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
394 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
395 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
396 rpc_oldjoin_internals,
401 * Join a domain, the old way. This function exists to allow
402 * the message to be displayed when oldjoin was explicitly
403 * requested, but not when it was implied by "net rpc join".
405 * @param argc Standard main() style argc.
406 * @param argv Standard main() style argv. Initial components are already
409 * @return A shell status integer (0 for success).
412 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
416 if (c->display_usage) {
421 _("Join a domain the old way"));
425 rc = net_rpc_perform_oldjoin(c, argc, argv);
428 d_fprintf(stderr, _("Failed to join domain\n"));
435 * 'net rpc join' entrypoint.
436 * @param argc Standard main() style argc.
437 * @param argv Standard main() style argv. Initial components are already
440 * Main 'net_rpc_join()' (where the admin username/password is used) is
442 * Try to just change the password, but if that doesn't work, use/prompt
443 * for a username/password.
446 int net_rpc_join(struct net_context *c, int argc, const char **argv)
448 if (c->display_usage) {
451 _("net rpc join -U <username>[%%password] <type>\n"
453 " username\tName of the admin user"
454 " password\tPassword of the admin user, will "
455 "prompt if not specified\n"
456 " type\tCan be one of the following:\n"
457 "\t\tMEMBER\tJoin as member server (default)\n"
458 "\t\tBDC\tJoin as BDC\n"
459 "\t\tPDC\tJoin as PDC\n"));
463 if (lp_server_role() == ROLE_STANDALONE) {
464 d_printf(_("cannot join as standalone machine\n"));
468 if (strlen(global_myname()) > 15) {
469 d_printf(_("Our netbios name can be at most 15 chars long, "
470 "\"%s\" is %u chars long\n"),
471 global_myname(), (unsigned int)strlen(global_myname()));
475 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
478 return net_rpc_join_newstyle(c, argc, argv);
482 * display info about a rpc domain
484 * All parameters are provided by the run_rpc_command function, except for
485 * argc, argv which are passed through.
487 * @param domain_sid The domain sid acquired from the remote server
488 * @param cli A cli_state connected to the server.
489 * @param mem_ctx Talloc context, destroyed on completion of the function.
490 * @param argc Standard main() style argc.
491 * @param argv Standard main() style argv. Initial components are already
494 * @return Normal NTSTATUS return.
497 NTSTATUS rpc_info_internals(struct net_context *c,
498 const struct dom_sid *domain_sid,
499 const char *domain_name,
500 struct cli_state *cli,
501 struct rpc_pipe_client *pipe_hnd,
506 struct policy_handle connect_pol, domain_pol;
507 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
508 union samr_DomainInfo *info = NULL;
511 sid_to_fstring(sid_str, domain_sid);
513 /* Get sam policy handle */
514 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
516 MAXIMUM_ALLOWED_ACCESS,
518 if (!NT_STATUS_IS_OK(result)) {
519 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
524 /* Get domain policy handle */
525 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
527 MAXIMUM_ALLOWED_ACCESS,
528 CONST_DISCARD(struct dom_sid2 *, domain_sid),
530 if (!NT_STATUS_IS_OK(result)) {
531 d_fprintf(stderr, _("Could not open domain: %s\n"),
536 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
540 if (NT_STATUS_IS_OK(result)) {
541 d_printf(_("Domain Name: %s\n"),
542 info->general.domain_name.string);
543 d_printf(_("Domain SID: %s\n"), sid_str);
544 d_printf(_("Sequence number: %llu\n"),
545 (unsigned long long)info->general.sequence_num);
546 d_printf(_("Num users: %u\n"), info->general.num_users);
547 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
548 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
556 * 'net rpc info' entrypoint.
557 * @param argc Standard main() style argc.
558 * @param argv Standard main() style argv. Initial components are already
562 int net_rpc_info(struct net_context *c, int argc, const char **argv)
564 if (c->display_usage) {
569 _("Display information about the domain"));
573 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
574 NET_FLAGS_PDC, rpc_info_internals,
579 * Fetch domain SID into the local secrets.tdb.
581 * All parameters are provided by the run_rpc_command function, except for
582 * argc, argv which are passed through.
584 * @param domain_sid The domain sid acquired from the remote server.
585 * @param cli A cli_state connected to the server.
586 * @param mem_ctx Talloc context, destroyed on completion of the function.
587 * @param argc Standard main() style argc.
588 * @param argv Standard main() style argv. Initial components are already
591 * @return Normal NTSTATUS return.
594 static NTSTATUS rpc_getsid_internals(struct net_context *c,
595 const struct dom_sid *domain_sid,
596 const char *domain_name,
597 struct cli_state *cli,
598 struct rpc_pipe_client *pipe_hnd,
605 sid_to_fstring(sid_str, domain_sid);
606 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
607 sid_str, domain_name);
609 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
610 DEBUG(0,("Can't store domain SID\n"));
611 return NT_STATUS_UNSUCCESSFUL;
618 * 'net rpc getsid' entrypoint.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
624 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
626 int conn_flags = NET_FLAGS_PDC;
628 if (!c->opt_user_specified) {
629 conn_flags |= NET_FLAGS_ANONYMOUS;
632 if (c->display_usage) {
637 _("Fetch domain SID into local secrets.tdb"));
641 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
643 rpc_getsid_internals,
647 /****************************************************************************/
650 * Basic usage function for 'net rpc user'.
651 * @param argc Standard main() style argc.
652 * @param argv Standard main() style argv. Initial components are already
656 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
658 return net_user_usage(c, argc, argv);
662 * Add a new user to a remote RPC server.
664 * @param argc Standard main() style argc.
665 * @param argv Standard main() style argv. Initial components are already
668 * @return A shell status integer (0 for success).
671 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
673 NET_API_STATUS status;
674 struct USER_INFO_1 info1;
675 uint32_t parm_error = 0;
677 if (argc < 1 || c->display_usage) {
678 rpc_user_usage(c, argc, argv);
684 info1.usri1_name = argv[0];
686 info1.usri1_password = argv[1];
689 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
692 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
693 argv[0], libnetapi_get_error_string(c->netapi_ctx,
697 d_printf(_("Added user '%s'.\n"), argv[0]);
704 * Rename a user on a remote RPC server.
706 * @param argc Standard main() style argc.
707 * @param argv Standard main() style argv. Initial components are already
710 * @return A shell status integer (0 for success).
713 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
715 NET_API_STATUS status;
716 struct USER_INFO_0 u0;
717 uint32_t parm_err = 0;
719 if (argc != 2 || c->display_usage) {
720 rpc_user_usage(c, argc, argv);
724 u0.usri0_name = argv[1];
726 status = NetUserSetInfo(c->opt_host, argv[0],
727 0, (uint8_t *)&u0, &parm_err);
730 _("Failed to rename user from %s to %s - %s\n"),
732 libnetapi_get_error_string(c->netapi_ctx, status));
734 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
741 * Set a user's primary group
743 * @param argc Standard main() style argc.
744 * @param argv Standard main() style argv. Initial components are already
747 * @return A shell status integer (0 for success).
750 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
753 NET_API_STATUS status;
755 struct GROUP_INFO_2 *g2;
756 struct USER_INFO_1051 u1051;
757 uint32_t parm_err = 0;
759 if (argc != 2 || c->display_usage) {
760 rpc_user_usage(c, argc, argv);
764 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
766 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
768 libnetapi_get_error_string(c->netapi_ctx, status));
771 g2 = (struct GROUP_INFO_2 *)buffer;
773 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
775 NetApiBufferFree(buffer);
777 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
778 (uint8_t *)&u1051, &parm_err);
781 _("Failed to set user's primary group %s to %s - "
782 "%s\n"), argv[0], argv[1],
783 libnetapi_get_error_string(c->netapi_ctx, status));
785 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
792 * Delete a user from a remote RPC server.
794 * @param argc Standard main() style argc.
795 * @param argv Standard main() style argv. Initial components are already
798 * @return A shell status integer (0 for success).
801 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
803 NET_API_STATUS status;
805 if (argc < 1 || c->display_usage) {
806 rpc_user_usage(c, argc, argv);
810 status = NetUserDel(c->opt_host, argv[0]);
813 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
815 libnetapi_get_error_string(c->netapi_ctx, status));
818 d_printf(_("Deleted user '%s'.\n"), argv[0]);
825 * Set a user's password on a remote RPC server.
827 * @param argc Standard main() style argc.
828 * @param argv Standard main() style argv. Initial components are already
831 * @return A shell status integer (0 for success).
834 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
836 NET_API_STATUS status;
838 struct USER_INFO_1003 u1003;
839 uint32_t parm_err = 0;
842 if (argc < 1 || c->display_usage) {
843 rpc_user_usage(c, argc, argv);
848 u1003.usri1003_password = argv[1];
850 ret = asprintf(&prompt, _("Enter new password for %s:"),
855 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
857 if (u1003.usri1003_password == NULL) {
862 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
864 /* Display results */
867 _("Failed to set password for '%s' with error: %s.\n"),
868 argv[0], libnetapi_get_error_string(c->netapi_ctx,
877 * List a user's groups from a remote RPC server.
879 * @param argc Standard main() style argc.
880 * @param argv Standard main() style argv. Initial components are already
883 * @return A shell status integer (0 for success)
886 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
889 NET_API_STATUS status;
890 struct GROUP_USERS_INFO_0 *u0 = NULL;
891 uint32_t entries_read = 0;
892 uint32_t total_entries = 0;
896 if (argc < 1 || c->display_usage) {
897 rpc_user_usage(c, argc, argv);
901 status = NetUserGetGroups(c->opt_host,
904 (uint8_t **)(void *)&u0,
910 _("Failed to get groups for '%s' with error: %s.\n"),
911 argv[0], libnetapi_get_error_string(c->netapi_ctx,
916 for (i=0; i < entries_read; i++) {
917 printf("%s\n", u0->grui0_name);
925 * List users on a remote RPC server.
927 * All parameters are provided by the run_rpc_command function, except for
928 * argc, argv which are passed through.
930 * @param domain_sid The domain sid acquired from the remote server.
931 * @param cli A cli_state connected to the server.
932 * @param mem_ctx Talloc context, destroyed on completion of the function.
933 * @param argc Standard main() style argc.
934 * @param argv Standard main() style argv. Initial components are already
937 * @return Normal NTSTATUS return.
940 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
942 NET_API_STATUS status;
943 uint32_t start_idx=0, num_entries, i, loop_count = 0;
944 struct NET_DISPLAY_USER *info = NULL;
947 /* Query domain users */
948 if (c->opt_long_list_entries)
949 d_printf(_("\nUser name Comment"
950 "\n-----------------------------\n"));
952 uint32_t max_entries, max_size;
954 get_query_dispinfo_params(
955 loop_count, &max_entries, &max_size);
957 status = NetQueryDisplayInformation(c->opt_host,
964 if (status != 0 && status != ERROR_MORE_DATA) {
968 info = (struct NET_DISPLAY_USER *)buffer;
970 for (i = 0; i < num_entries; i++) {
972 if (c->opt_long_list_entries)
973 printf("%-21.21s %s\n", info->usri1_name,
974 info->usri1_comment);
976 printf("%s\n", info->usri1_name);
980 NetApiBufferFree(buffer);
983 start_idx += num_entries;
985 } while (status == ERROR_MORE_DATA);
991 * 'net rpc user' entrypoint.
992 * @param argc Standard main() style argc.
993 * @param argv Standard main() style argv. Initial components are already
997 int net_rpc_user(struct net_context *c, int argc, const char **argv)
999 NET_API_STATUS status;
1001 struct functable func[] = {
1006 N_("Add specified user"),
1007 N_("net rpc user add\n"
1008 " Add specified user")
1014 N_("List domain groups of user"),
1015 N_("net rpc user info\n"
1016 " List domain groups of user")
1022 N_("Remove specified user"),
1023 N_("net rpc user delete\n"
1024 " Remove specified user")
1030 N_("Change user password"),
1031 N_("net rpc user password\n"
1032 " Change user password")
1038 N_("Rename specified user"),
1039 N_("net rpc user rename\n"
1040 " Rename specified user")
1044 rpc_user_setprimarygroup,
1046 "Set a user's primary group",
1047 "net rpc user setprimarygroup\n"
1048 " Set a user's primary group"
1050 {NULL, NULL, 0, NULL, NULL}
1053 status = libnetapi_init(&c->netapi_ctx);
1057 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1058 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1059 if (c->opt_kerberos) {
1060 libnetapi_set_use_kerberos(c->netapi_ctx);
1064 if (c->display_usage) {
1069 _("List all users"));
1070 net_display_usage_from_functable(func);
1074 return rpc_user_list(c, argc, argv);
1077 return net_run_function(c, argc, argv, "net rpc user", func);
1080 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1081 TALLOC_CTX *mem_ctx,
1082 struct rpc_sh_ctx *ctx,
1083 struct rpc_pipe_client *pipe_hnd,
1084 int argc, const char **argv)
1086 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1089 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1090 TALLOC_CTX *mem_ctx,
1091 struct rpc_sh_ctx *ctx,
1092 struct rpc_pipe_client *pipe_hnd,
1093 int argc, const char **argv)
1095 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1098 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1099 TALLOC_CTX *mem_ctx,
1100 struct rpc_sh_ctx *ctx,
1101 struct rpc_pipe_client *pipe_hnd,
1102 int argc, const char **argv,
1104 struct net_context *c,
1105 TALLOC_CTX *mem_ctx,
1106 struct rpc_sh_ctx *ctx,
1107 struct rpc_pipe_client *pipe_hnd,
1108 struct policy_handle *user_hnd,
1109 int argc, const char **argv))
1111 struct policy_handle connect_pol, domain_pol, user_pol;
1112 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1115 enum lsa_SidType type;
1118 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1120 return NT_STATUS_INVALID_PARAMETER;
1123 ZERO_STRUCT(connect_pol);
1124 ZERO_STRUCT(domain_pol);
1125 ZERO_STRUCT(user_pol);
1127 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1128 argv[0], NULL, NULL, &sid, &type);
1129 if (!NT_STATUS_IS_OK(result)) {
1130 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1135 if (type != SID_NAME_USER) {
1136 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1137 sid_type_lookup(type));
1138 result = NT_STATUS_NO_SUCH_USER;
1142 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1143 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1144 result = NT_STATUS_NO_SUCH_USER;
1148 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1150 MAXIMUM_ALLOWED_ACCESS,
1152 if (!NT_STATUS_IS_OK(result)) {
1156 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1158 MAXIMUM_ALLOWED_ACCESS,
1161 if (!NT_STATUS_IS_OK(result)) {
1165 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1167 MAXIMUM_ALLOWED_ACCESS,
1170 if (!NT_STATUS_IS_OK(result)) {
1174 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1177 if (is_valid_policy_hnd(&user_pol)) {
1178 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1180 if (is_valid_policy_hnd(&domain_pol)) {
1181 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1183 if (is_valid_policy_hnd(&connect_pol)) {
1184 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1189 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1190 TALLOC_CTX *mem_ctx,
1191 struct rpc_sh_ctx *ctx,
1192 struct rpc_pipe_client *pipe_hnd,
1193 struct policy_handle *user_hnd,
1194 int argc, const char **argv)
1197 union samr_UserInfo *info = NULL;
1200 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1202 return NT_STATUS_INVALID_PARAMETER;
1205 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1209 if (!NT_STATUS_IS_OK(result)) {
1213 d_printf(_("user rid: %d, group rid: %d\n"),
1215 info->info21.primary_gid);
1220 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1221 TALLOC_CTX *mem_ctx,
1222 struct rpc_sh_ctx *ctx,
1223 struct rpc_pipe_client *pipe_hnd,
1224 int argc, const char **argv)
1226 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1227 rpc_sh_user_show_internals);
1230 #define FETCHSTR(name, rec) \
1231 do { if (strequal(ctx->thiscmd, name)) { \
1232 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1235 #define SETSTR(name, rec, flag) \
1236 do { if (strequal(ctx->thiscmd, name)) { \
1237 init_lsa_String(&(info->info21.rec), argv[0]); \
1238 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1241 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1242 TALLOC_CTX *mem_ctx,
1243 struct rpc_sh_ctx *ctx,
1244 struct rpc_pipe_client *pipe_hnd,
1245 struct policy_handle *user_hnd,
1246 int argc, const char **argv)
1249 const char *username;
1250 const char *oldval = "";
1251 union samr_UserInfo *info = NULL;
1254 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1255 _("Usage:"), ctx->whoami);
1256 return NT_STATUS_INVALID_PARAMETER;
1259 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1263 if (!NT_STATUS_IS_OK(result)) {
1267 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1269 FETCHSTR("fullname", full_name);
1270 FETCHSTR("homedir", home_directory);
1271 FETCHSTR("homedrive", home_drive);
1272 FETCHSTR("logonscript", logon_script);
1273 FETCHSTR("profilepath", profile_path);
1274 FETCHSTR("description", description);
1277 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1281 if (strcmp(argv[0], "NULL") == 0) {
1285 ZERO_STRUCT(info->info21);
1287 SETSTR("fullname", full_name, FULL_NAME);
1288 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1289 SETSTR("homedrive", home_drive, HOME_DRIVE);
1290 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1291 SETSTR("profilepath", profile_path, PROFILE_PATH);
1292 SETSTR("description", description, DESCRIPTION);
1294 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1299 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1300 ctx->thiscmd, oldval, argv[0]);
1307 #define HANDLEFLG(name, rec) \
1308 do { if (strequal(ctx->thiscmd, name)) { \
1309 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1311 newflags = oldflags | ACB_##rec; \
1313 newflags = oldflags & ~ACB_##rec; \
1316 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1317 TALLOC_CTX *mem_ctx,
1318 struct rpc_sh_ctx *ctx,
1319 struct rpc_pipe_client *pipe_hnd,
1320 int argc, const char **argv)
1322 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1323 rpc_sh_user_str_edit_internals);
1326 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1327 TALLOC_CTX *mem_ctx,
1328 struct rpc_sh_ctx *ctx,
1329 struct rpc_pipe_client *pipe_hnd,
1330 struct policy_handle *user_hnd,
1331 int argc, const char **argv)
1334 const char *username;
1335 const char *oldval = "unknown";
1336 uint32 oldflags, newflags;
1338 union samr_UserInfo *info = NULL;
1341 ((argc == 1) && !strequal(argv[0], "yes") &&
1342 !strequal(argv[0], "no"))) {
1343 /* TRANSATORS: The yes|no here are program keywords. Please do
1345 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1347 return NT_STATUS_INVALID_PARAMETER;
1350 newval = strequal(argv[0], "yes");
1352 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1356 if (!NT_STATUS_IS_OK(result)) {
1360 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1361 oldflags = info->info21.acct_flags;
1362 newflags = info->info21.acct_flags;
1364 HANDLEFLG("disabled", DISABLED);
1365 HANDLEFLG("pwnotreq", PWNOTREQ);
1366 HANDLEFLG("autolock", AUTOLOCK);
1367 HANDLEFLG("pwnoexp", PWNOEXP);
1370 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1375 ZERO_STRUCT(info->info21);
1377 info->info21.acct_flags = newflags;
1378 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1380 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1385 if (NT_STATUS_IS_OK(result)) {
1386 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1387 ctx->thiscmd, oldval, argv[0]);
1395 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1396 TALLOC_CTX *mem_ctx,
1397 struct rpc_sh_ctx *ctx,
1398 struct rpc_pipe_client *pipe_hnd,
1399 int argc, const char **argv)
1401 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1402 rpc_sh_user_flag_edit_internals);
1405 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1406 TALLOC_CTX *mem_ctx,
1407 struct rpc_sh_ctx *ctx)
1409 static struct rpc_sh_cmd cmds[] = {
1411 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1412 N_("Show/Set a user's full name") },
1414 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1415 N_("Show/Set a user's home directory") },
1417 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1418 N_("Show/Set a user's home drive") },
1420 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1421 N_("Show/Set a user's logon script") },
1423 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1424 N_("Show/Set a user's profile path") },
1426 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1427 N_("Show/Set a user's description") },
1429 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1430 N_("Show/Set whether a user is disabled") },
1432 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1433 N_("Show/Set whether a user locked out") },
1435 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1436 N_("Show/Set whether a user does not need a password") },
1438 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1439 N_("Show/Set whether a user's password does not expire") },
1441 { NULL, NULL, 0, NULL, NULL }
1447 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1448 TALLOC_CTX *mem_ctx,
1449 struct rpc_sh_ctx *ctx)
1451 static struct rpc_sh_cmd cmds[] = {
1453 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1454 N_("List available users") },
1456 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1457 N_("List the domain groups a user is member of") },
1459 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1460 N_("Show info about a user") },
1462 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1463 N_("Show/Modify a user's fields") },
1465 { NULL, NULL, 0, NULL, NULL }
1471 /****************************************************************************/
1474 * Basic usage function for 'net rpc group'.
1475 * @param argc Standard main() style argc.
1476 * @param argv Standard main() style argv. Initial components are already
1480 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1482 return net_group_usage(c, argc, argv);
1486 * Delete group on a remote RPC server.
1488 * All parameters are provided by the run_rpc_command function, except for
1489 * argc, argv which are passed through.
1491 * @param domain_sid The domain sid acquired from the remote server.
1492 * @param cli A cli_state connected to the server.
1493 * @param mem_ctx Talloc context, destroyed on completion of the function.
1494 * @param argc Standard main() style argc.
1495 * @param argv Standard main() style argv. Initial components are already
1498 * @return Normal NTSTATUS return.
1501 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1502 const struct dom_sid *domain_sid,
1503 const char *domain_name,
1504 struct cli_state *cli,
1505 struct rpc_pipe_client *pipe_hnd,
1506 TALLOC_CTX *mem_ctx,
1510 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1511 bool group_is_primary = false;
1512 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1514 struct samr_RidAttrArray *rids = NULL;
1517 /* struct samr_RidWithAttribute *user_gids; */
1519 struct samr_Ids group_rids, name_types;
1520 struct lsa_String lsa_acct_name;
1521 union samr_UserInfo *info = NULL;
1523 if (argc < 1 || c->display_usage) {
1524 rpc_group_usage(c, argc,argv);
1525 return NT_STATUS_OK; /* ok? */
1528 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1530 MAXIMUM_ALLOWED_ACCESS,
1533 if (!NT_STATUS_IS_OK(result)) {
1534 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1538 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1540 MAXIMUM_ALLOWED_ACCESS,
1541 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1544 if (!NT_STATUS_IS_OK(result)) {
1545 d_fprintf(stderr, _("Request open_domain failed\n"));
1549 init_lsa_String(&lsa_acct_name, argv[0]);
1551 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1557 if (!NT_STATUS_IS_OK(result)) {
1558 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1562 switch (name_types.ids[0])
1564 case SID_NAME_DOM_GRP:
1565 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1567 MAXIMUM_ALLOWED_ACCESS,
1570 if (!NT_STATUS_IS_OK(result)) {
1571 d_fprintf(stderr, _("Request open_group failed"));
1575 group_rid = group_rids.ids[0];
1577 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1581 if (!NT_STATUS_IS_OK(result)) {
1583 _("Unable to query group members of %s"),
1588 if (c->opt_verbose) {
1590 _("Domain Group %s (rid: %d) has %d members\n"),
1591 argv[0],group_rid, rids->count);
1594 /* Check if group is anyone's primary group */
1595 for (i = 0; i < rids->count; i++)
1597 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1599 MAXIMUM_ALLOWED_ACCESS,
1603 if (!NT_STATUS_IS_OK(result)) {
1605 _("Unable to open group member %d\n"),
1610 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1615 if (!NT_STATUS_IS_OK(result)) {
1617 _("Unable to lookup userinfo for group "
1623 if (info->info21.primary_gid == group_rid) {
1624 if (c->opt_verbose) {
1625 d_printf(_("Group is primary group "
1627 info->info21.account_name.string);
1629 group_is_primary = true;
1632 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1635 if (group_is_primary) {
1636 d_fprintf(stderr, _("Unable to delete group because "
1637 "some of it's members have it as primary "
1639 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1643 /* remove all group members */
1644 for (i = 0; i < rids->count; i++)
1647 d_printf(_("Remove group member %d..."),
1649 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1653 if (NT_STATUS_IS_OK(result)) {
1655 d_printf(_("ok\n"));
1658 d_printf("%s\n", _("failed"));
1663 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1667 /* removing a local group is easier... */
1668 case SID_NAME_ALIAS:
1669 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1671 MAXIMUM_ALLOWED_ACCESS,
1675 if (!NT_STATUS_IS_OK(result)) {
1676 d_fprintf(stderr, _("Request open_alias failed\n"));
1680 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1684 d_fprintf(stderr, _("%s is of type %s. This command is only "
1685 "for deleting local or global groups\n"),
1686 argv[0],sid_type_lookup(name_types.ids[0]));
1687 result = NT_STATUS_UNSUCCESSFUL;
1691 if (NT_STATUS_IS_OK(result)) {
1693 d_printf(_("Deleted %s '%s'\n"),
1694 sid_type_lookup(name_types.ids[0]), argv[0]);
1696 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1697 get_friendly_nt_error_msg(result));
1705 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1707 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1708 rpc_group_delete_internals, argc,argv);
1711 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1713 NET_API_STATUS status;
1714 struct GROUP_INFO_1 info1;
1715 uint32_t parm_error = 0;
1717 if (argc != 1 || c->display_usage) {
1718 rpc_group_usage(c, argc, argv);
1724 info1.grpi1_name = argv[0];
1725 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1726 info1.grpi1_comment = c->opt_comment;
1729 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1733 _("Failed to add group '%s' with error: %s.\n"),
1734 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1738 d_printf(_("Added group '%s'.\n"), argv[0]);
1744 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1746 NET_API_STATUS status;
1747 struct LOCALGROUP_INFO_1 info1;
1748 uint32_t parm_error = 0;
1750 if (argc != 1 || c->display_usage) {
1751 rpc_group_usage(c, argc, argv);
1757 info1.lgrpi1_name = argv[0];
1758 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1759 info1.lgrpi1_comment = c->opt_comment;
1762 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1766 _("Failed to add alias '%s' with error: %s.\n"),
1767 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1771 d_printf(_("Added alias '%s'.\n"), argv[0]);
1777 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1779 if (c->opt_localgroup)
1780 return rpc_alias_add_internals(c, argc, argv);
1782 return rpc_group_add_internals(c, argc, argv);
1785 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1786 TALLOC_CTX *mem_ctx,
1788 struct dom_sid *sid,
1789 enum lsa_SidType *type)
1791 struct dom_sid *sids = NULL;
1792 enum lsa_SidType *types = NULL;
1793 struct rpc_pipe_client *pipe_hnd = NULL;
1794 struct policy_handle lsa_pol;
1795 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1797 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1799 if (!NT_STATUS_IS_OK(result)) {
1803 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1804 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1806 if (!NT_STATUS_IS_OK(result)) {
1810 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1811 &name, NULL, 1, &sids, &types);
1813 if (NT_STATUS_IS_OK(result)) {
1814 sid_copy(sid, &sids[0]);
1818 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1822 TALLOC_FREE(pipe_hnd);
1825 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1827 /* Try as S-1-5-whatever */
1829 struct dom_sid tmp_sid;
1831 if (string_to_sid(&tmp_sid, name)) {
1832 sid_copy(sid, &tmp_sid);
1833 *type = SID_NAME_UNKNOWN;
1834 result = NT_STATUS_OK;
1841 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1842 TALLOC_CTX *mem_ctx,
1843 const struct dom_sid *group_sid,
1846 struct policy_handle connect_pol, domain_pol;
1849 struct policy_handle group_pol;
1851 struct samr_Ids rids, rid_types;
1852 struct lsa_String lsa_acct_name;
1856 sid_copy(&sid, group_sid);
1858 if (!sid_split_rid(&sid, &group_rid)) {
1859 return NT_STATUS_UNSUCCESSFUL;
1862 /* Get sam policy handle */
1863 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1865 MAXIMUM_ALLOWED_ACCESS,
1867 if (!NT_STATUS_IS_OK(result)) {
1871 /* Get domain policy handle */
1872 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1874 MAXIMUM_ALLOWED_ACCESS,
1877 if (!NT_STATUS_IS_OK(result)) {
1881 init_lsa_String(&lsa_acct_name, member);
1883 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1890 if (!NT_STATUS_IS_OK(result)) {
1891 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1896 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1898 MAXIMUM_ALLOWED_ACCESS,
1902 if (!NT_STATUS_IS_OK(result)) {
1906 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1909 0x0005); /* unknown flags */
1912 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1916 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1917 TALLOC_CTX *mem_ctx,
1918 const struct dom_sid *alias_sid,
1921 struct policy_handle connect_pol, domain_pol;
1924 struct policy_handle alias_pol;
1926 struct dom_sid member_sid;
1927 enum lsa_SidType member_type;
1931 sid_copy(&sid, alias_sid);
1933 if (!sid_split_rid(&sid, &alias_rid)) {
1934 return NT_STATUS_UNSUCCESSFUL;
1937 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1938 member, &member_sid, &member_type);
1940 if (!NT_STATUS_IS_OK(result)) {
1941 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1946 /* Get sam policy handle */
1947 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1949 MAXIMUM_ALLOWED_ACCESS,
1951 if (!NT_STATUS_IS_OK(result)) {
1955 /* Get domain policy handle */
1956 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1958 MAXIMUM_ALLOWED_ACCESS,
1961 if (!NT_STATUS_IS_OK(result)) {
1965 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1967 MAXIMUM_ALLOWED_ACCESS,
1971 if (!NT_STATUS_IS_OK(result)) {
1975 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1979 if (!NT_STATUS_IS_OK(result)) {
1984 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1988 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1989 const struct dom_sid *domain_sid,
1990 const char *domain_name,
1991 struct cli_state *cli,
1992 struct rpc_pipe_client *pipe_hnd,
1993 TALLOC_CTX *mem_ctx,
1997 struct dom_sid group_sid;
1998 enum lsa_SidType group_type;
2000 if (argc != 2 || c->display_usage) {
2003 _("net rpc group addmem <group> <member>\n"
2004 " Add a member to a group\n"
2005 " group\tGroup to add member to\n"
2006 " member\tMember to add to group\n"));
2007 return NT_STATUS_UNSUCCESSFUL;
2010 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2011 &group_sid, &group_type))) {
2012 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2014 return NT_STATUS_UNSUCCESSFUL;
2017 if (group_type == SID_NAME_DOM_GRP) {
2018 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2019 &group_sid, argv[1]);
2021 if (!NT_STATUS_IS_OK(result)) {
2022 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2023 argv[1], argv[0], nt_errstr(result));
2028 if (group_type == SID_NAME_ALIAS) {
2029 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2030 &group_sid, argv[1]);
2032 if (!NT_STATUS_IS_OK(result)) {
2033 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2034 argv[1], argv[0], nt_errstr(result));
2039 d_fprintf(stderr, _("Can only add members to global or local groups "
2040 "which %s is not\n"), argv[0]);
2042 return NT_STATUS_UNSUCCESSFUL;
2045 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2047 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2048 rpc_group_addmem_internals,
2052 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2053 struct rpc_pipe_client *pipe_hnd,
2054 TALLOC_CTX *mem_ctx,
2055 const struct dom_sid *group_sid,
2058 struct policy_handle connect_pol, domain_pol;
2061 struct policy_handle group_pol;
2063 struct samr_Ids rids, rid_types;
2064 struct lsa_String lsa_acct_name;
2068 sid_copy(&sid, group_sid);
2070 if (!sid_split_rid(&sid, &group_rid))
2071 return NT_STATUS_UNSUCCESSFUL;
2073 /* Get sam policy handle */
2074 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2076 MAXIMUM_ALLOWED_ACCESS,
2078 if (!NT_STATUS_IS_OK(result))
2081 /* Get domain policy handle */
2082 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2084 MAXIMUM_ALLOWED_ACCESS,
2087 if (!NT_STATUS_IS_OK(result))
2090 init_lsa_String(&lsa_acct_name, member);
2092 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2098 if (!NT_STATUS_IS_OK(result)) {
2099 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2104 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2106 MAXIMUM_ALLOWED_ACCESS,
2110 if (!NT_STATUS_IS_OK(result))
2113 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2118 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2122 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2123 TALLOC_CTX *mem_ctx,
2124 const struct dom_sid *alias_sid,
2127 struct policy_handle connect_pol, domain_pol;
2130 struct policy_handle alias_pol;
2132 struct dom_sid member_sid;
2133 enum lsa_SidType member_type;
2137 sid_copy(&sid, alias_sid);
2139 if (!sid_split_rid(&sid, &alias_rid))
2140 return NT_STATUS_UNSUCCESSFUL;
2142 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2143 member, &member_sid, &member_type);
2145 if (!NT_STATUS_IS_OK(result)) {
2146 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2151 /* Get sam policy handle */
2152 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2154 MAXIMUM_ALLOWED_ACCESS,
2156 if (!NT_STATUS_IS_OK(result)) {
2160 /* Get domain policy handle */
2161 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2163 MAXIMUM_ALLOWED_ACCESS,
2166 if (!NT_STATUS_IS_OK(result)) {
2170 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2172 MAXIMUM_ALLOWED_ACCESS,
2176 if (!NT_STATUS_IS_OK(result))
2179 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2183 if (!NT_STATUS_IS_OK(result))
2187 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2191 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2192 const struct dom_sid *domain_sid,
2193 const char *domain_name,
2194 struct cli_state *cli,
2195 struct rpc_pipe_client *pipe_hnd,
2196 TALLOC_CTX *mem_ctx,
2200 struct dom_sid group_sid;
2201 enum lsa_SidType group_type;
2203 if (argc != 2 || c->display_usage) {
2206 _("net rpc group delmem <group> <member>\n"
2207 " Delete a member from a group\n"
2208 " group\tGroup to delete member from\n"
2209 " member\tMember to delete from group\n"));
2210 return NT_STATUS_UNSUCCESSFUL;
2213 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2214 &group_sid, &group_type))) {
2215 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2217 return NT_STATUS_UNSUCCESSFUL;
2220 if (group_type == SID_NAME_DOM_GRP) {
2221 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2222 &group_sid, argv[1]);
2224 if (!NT_STATUS_IS_OK(result)) {
2225 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2226 argv[1], argv[0], nt_errstr(result));
2231 if (group_type == SID_NAME_ALIAS) {
2232 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2233 &group_sid, argv[1]);
2235 if (!NT_STATUS_IS_OK(result)) {
2236 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2237 argv[1], argv[0], nt_errstr(result));
2242 d_fprintf(stderr, _("Can only delete members from global or local "
2243 "groups which %s is not\n"), argv[0]);
2245 return NT_STATUS_UNSUCCESSFUL;
2248 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2250 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2251 rpc_group_delmem_internals,
2256 * List groups on a remote RPC server.
2258 * All parameters are provided by the run_rpc_command function, except for
2259 * argc, argv which are passes through.
2261 * @param domain_sid The domain sid acquired from the remote server.
2262 * @param cli A cli_state connected to the server.
2263 * @param mem_ctx Talloc context, destroyed on completion of the function.
2264 * @param argc Standard main() style argc.
2265 * @param argv Standard main() style argv. Initial components are already
2268 * @return Normal NTSTATUS return.
2271 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2272 const struct dom_sid *domain_sid,
2273 const char *domain_name,
2274 struct cli_state *cli,
2275 struct rpc_pipe_client *pipe_hnd,
2276 TALLOC_CTX *mem_ctx,
2280 struct policy_handle connect_pol, domain_pol;
2281 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2282 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2283 struct samr_SamArray *groups = NULL;
2284 bool global = false;
2286 bool builtin = false;
2288 if (c->display_usage) {
2291 _("net rpc group list [global] [local] [builtin]\n"
2292 " List groups on RPC server\n"
2293 " global\tList global groups\n"
2294 " local\tList local groups\n"
2295 " builtin\tList builtin groups\n"
2296 " If none of global, local or builtin is "
2297 "specified, all three options are considered "
2299 return NT_STATUS_OK;
2308 for (i=0; i<argc; i++) {
2309 if (strequal(argv[i], "global"))
2312 if (strequal(argv[i], "local"))
2315 if (strequal(argv[i], "builtin"))
2319 /* Get sam policy handle */
2321 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2323 MAXIMUM_ALLOWED_ACCESS,
2325 if (!NT_STATUS_IS_OK(result)) {
2329 /* Get domain policy handle */
2331 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2333 MAXIMUM_ALLOWED_ACCESS,
2334 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2336 if (!NT_STATUS_IS_OK(result)) {
2340 /* Query domain groups */
2341 if (c->opt_long_list_entries)
2342 d_printf(_("\nGroup name Comment"
2343 "\n-----------------------------\n"));
2345 uint32_t max_size, total_size, returned_size;
2346 union samr_DispInfo info;
2350 get_query_dispinfo_params(
2351 loop_count, &max_entries, &max_size);
2353 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2362 num_entries = info.info3.count;
2363 start_idx += info.info3.count;
2365 if (!NT_STATUS_IS_OK(result) &&
2366 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2369 for (i = 0; i < num_entries; i++) {
2371 const char *group = NULL;
2372 const char *desc = NULL;
2374 group = info.info3.entries[i].account_name.string;
2375 desc = info.info3.entries[i].description.string;
2377 if (c->opt_long_list_entries)
2378 printf("%-21.21s %-50.50s\n",
2381 printf("%s\n", group);
2383 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2384 /* query domain aliases */
2389 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2395 if (!NT_STATUS_IS_OK(result) &&
2396 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2399 for (i = 0; i < num_entries; i++) {
2401 const char *description = NULL;
2403 if (c->opt_long_list_entries) {
2405 struct policy_handle alias_pol;
2406 union samr_AliasInfo *info = NULL;
2408 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2411 groups->entries[i].idx,
2413 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2417 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2419 description = info->description.string;
2423 if (description != NULL) {
2424 printf("%-21.21s %-50.50s\n",
2425 groups->entries[i].name.string,
2428 printf("%s\n", groups->entries[i].name.string);
2431 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2432 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2433 /* Get builtin policy handle */
2435 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2437 MAXIMUM_ALLOWED_ACCESS,
2438 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2440 if (!NT_STATUS_IS_OK(result)) {
2443 /* query builtin aliases */
2446 if (!builtin) break;
2448 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2454 if (!NT_STATUS_IS_OK(result) &&
2455 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2458 for (i = 0; i < num_entries; i++) {
2460 const char *description = NULL;
2462 if (c->opt_long_list_entries) {
2464 struct policy_handle alias_pol;
2465 union samr_AliasInfo *info = NULL;
2467 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2470 groups->entries[i].idx,
2472 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2476 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2478 description = info->description.string;
2482 if (description != NULL) {
2483 printf("%-21.21s %-50.50s\n",
2484 groups->entries[i].name.string,
2487 printf("%s\n", groups->entries[i].name.string);
2490 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2496 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2498 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2499 rpc_group_list_internals,
2503 static NTSTATUS rpc_list_group_members(struct net_context *c,
2504 struct rpc_pipe_client *pipe_hnd,
2505 TALLOC_CTX *mem_ctx,
2506 const char *domain_name,
2507 const struct dom_sid *domain_sid,
2508 struct policy_handle *domain_pol,
2512 struct policy_handle group_pol;
2513 uint32 num_members, *group_rids;
2515 struct samr_RidAttrArray *rids = NULL;
2516 struct lsa_Strings names;
2517 struct samr_Ids types;
2520 sid_to_fstring(sid_str, domain_sid);
2522 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2524 MAXIMUM_ALLOWED_ACCESS,
2528 if (!NT_STATUS_IS_OK(result))
2531 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2535 if (!NT_STATUS_IS_OK(result))
2538 num_members = rids->count;
2539 group_rids = rids->rids;
2541 while (num_members > 0) {
2542 int this_time = 512;
2544 if (num_members < this_time)
2545 this_time = num_members;
2547 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2554 if (!NT_STATUS_IS_OK(result))
2557 /* We only have users as members, but make the output
2558 the same as the output of alias members */
2560 for (i = 0; i < this_time; i++) {
2562 if (c->opt_long_list_entries) {
2563 printf("%s-%d %s\\%s %d\n", sid_str,
2564 group_rids[i], domain_name,
2565 names.names[i].string,
2568 printf("%s\\%s\n", domain_name,
2569 names.names[i].string);
2573 num_members -= this_time;
2577 return NT_STATUS_OK;
2580 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2581 struct rpc_pipe_client *pipe_hnd,
2582 TALLOC_CTX *mem_ctx,
2583 struct policy_handle *domain_pol,
2587 struct rpc_pipe_client *lsa_pipe;
2588 struct policy_handle alias_pol, lsa_pol;
2590 struct dom_sid *alias_sids;
2593 enum lsa_SidType *types;
2595 struct lsa_SidArray sid_array;
2597 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2599 MAXIMUM_ALLOWED_ACCESS,
2603 if (!NT_STATUS_IS_OK(result))
2606 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2610 if (!NT_STATUS_IS_OK(result)) {
2611 d_fprintf(stderr, _("Couldn't list alias members\n"));
2615 num_members = sid_array.num_sids;
2617 if (num_members == 0) {
2618 return NT_STATUS_OK;
2621 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2622 &ndr_table_lsarpc.syntax_id,
2624 if (!NT_STATUS_IS_OK(result)) {
2625 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2626 nt_errstr(result) );
2630 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2631 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2633 if (!NT_STATUS_IS_OK(result)) {
2634 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2635 TALLOC_FREE(lsa_pipe);
2639 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2641 d_fprintf(stderr, _("Out of memory\n"));
2642 TALLOC_FREE(lsa_pipe);
2643 return NT_STATUS_NO_MEMORY;
2646 for (i=0; i<num_members; i++) {
2647 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2650 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2651 num_members, alias_sids,
2652 &domains, &names, &types);
2654 if (!NT_STATUS_IS_OK(result) &&
2655 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2656 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2657 TALLOC_FREE(lsa_pipe);
2661 for (i = 0; i < num_members; i++) {
2663 sid_to_fstring(sid_str, &alias_sids[i]);
2665 if (c->opt_long_list_entries) {
2666 printf("%s %s\\%s %d\n", sid_str,
2667 domains[i] ? domains[i] : _("*unknown*"),
2668 names[i] ? names[i] : _("*unknown*"), types[i]);
2671 printf("%s\\%s\n", domains[i], names[i]);
2673 printf("%s\n", sid_str);
2677 TALLOC_FREE(lsa_pipe);
2678 return NT_STATUS_OK;
2681 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2682 const struct dom_sid *domain_sid,
2683 const char *domain_name,
2684 struct cli_state *cli,
2685 struct rpc_pipe_client *pipe_hnd,
2686 TALLOC_CTX *mem_ctx,
2691 struct policy_handle connect_pol, domain_pol;
2692 struct samr_Ids rids, rid_types;
2693 struct lsa_String lsa_acct_name;
2695 /* Get sam policy handle */
2697 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2699 MAXIMUM_ALLOWED_ACCESS,
2702 if (!NT_STATUS_IS_OK(result))
2705 /* Get domain policy handle */
2707 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2709 MAXIMUM_ALLOWED_ACCESS,
2710 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2713 if (!NT_STATUS_IS_OK(result))
2716 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2718 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2725 if (!NT_STATUS_IS_OK(result)) {
2727 /* Ok, did not find it in the global sam, try with builtin */
2729 struct dom_sid sid_Builtin;
2731 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2733 sid_copy(&sid_Builtin, &global_sid_Builtin);
2735 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2737 MAXIMUM_ALLOWED_ACCESS,
2741 if (!NT_STATUS_IS_OK(result)) {
2742 d_fprintf(stderr, _("Couldn't find group %s\n"),
2747 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2754 if (!NT_STATUS_IS_OK(result)) {
2755 d_fprintf(stderr, _("Couldn't find group %s\n"),
2761 if (rids.count != 1) {
2762 d_fprintf(stderr, _("Couldn't find group %s\n"),
2767 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2768 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2769 domain_sid, &domain_pol,
2773 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2774 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2778 return NT_STATUS_NO_SUCH_GROUP;
2781 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2783 if (argc != 1 || c->display_usage) {
2784 return rpc_group_usage(c, argc, argv);
2787 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2788 rpc_group_members_internals,
2792 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2794 NET_API_STATUS status;
2795 struct GROUP_INFO_0 g0;
2799 d_printf(_("Usage:\n"));
2800 d_printf("net rpc group rename group newname\n");
2804 g0.grpi0_name = argv[1];
2806 status = NetGroupSetInfo(c->opt_host,
2813 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2814 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2822 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2824 if (argc != 2 || c->display_usage) {
2825 return rpc_group_usage(c, argc, argv);
2828 return rpc_group_rename_internals(c, argc, argv);
2832 * 'net rpc group' entrypoint.
2833 * @param argc Standard main() style argc.
2834 * @param argv Standard main() style argv. Initial components are already
2838 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2840 NET_API_STATUS status;
2842 struct functable func[] = {
2847 N_("Create specified group"),
2848 N_("net rpc group add\n"
2849 " Create specified group")
2855 N_("Delete specified group"),
2856 N_("net rpc group delete\n"
2857 " Delete specified group")
2863 N_("Add member to group"),
2864 N_("net rpc group addmem\n"
2865 " Add member to group")
2871 N_("Remove member from group"),
2872 N_("net rpc group delmem\n"
2873 " Remove member from group")
2880 N_("net rpc group list\n"
2887 N_("List group members"),
2888 N_("net rpc group members\n"
2889 " List group members")
2896 N_("net rpc group rename\n"
2899 {NULL, NULL, 0, NULL, NULL}
2902 status = libnetapi_init(&c->netapi_ctx);
2906 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2907 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2908 if (c->opt_kerberos) {
2909 libnetapi_set_use_kerberos(c->netapi_ctx);
2913 if (c->display_usage) {
2914 d_printf(_("Usage:\n"));
2915 d_printf(_("net rpc group\n"
2916 " Alias for net rpc group list global "
2917 "local builtin\n"));
2918 net_display_usage_from_functable(func);
2922 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2923 rpc_group_list_internals,
2927 return net_run_function(c, argc, argv, "net rpc group", func);
2930 /****************************************************************************/
2932 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2934 return net_share_usage(c, argc, argv);
2938 * Add a share on a remote RPC server.
2940 * @param argc Standard main() style argc.
2941 * @param argv Standard main() style argv. Initial components are already
2944 * @return A shell status integer (0 for success).
2947 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2949 NET_API_STATUS status;
2952 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2953 uint32 num_users=0, perms=0;
2954 char *password=NULL; /* don't allow a share password */
2955 struct SHARE_INFO_2 i2;
2956 uint32_t parm_error = 0;
2958 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2959 return rpc_share_usage(c, argc, argv);
2962 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2966 path = strchr(sharename, '=');
2973 i2.shi2_netname = sharename;
2974 i2.shi2_type = type;
2975 i2.shi2_remark = c->opt_comment;
2976 i2.shi2_permissions = perms;
2977 i2.shi2_max_uses = c->opt_maxusers;
2978 i2.shi2_current_uses = num_users;
2979 i2.shi2_path = path;
2980 i2.shi2_passwd = password;
2982 status = NetShareAdd(c->opt_host,
2987 printf(_("NetShareAdd failed with: %s\n"),
2988 libnetapi_get_error_string(c->netapi_ctx, status));
2995 * Delete a share on a remote RPC server.
2997 * @param domain_sid The domain sid acquired from the remote server.
2998 * @param argc Standard main() style argc.
2999 * @param argv Standard main() style argv. Initial components are already
3002 * @return A shell status integer (0 for success).
3004 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3006 if (argc < 1 || c->display_usage) {
3007 return rpc_share_usage(c, argc, argv);
3010 return NetShareDel(c->opt_host, argv[0], 0);
3014 * Formatted print of share info
3016 * @param r pointer to SHARE_INFO_1 to format
3019 static void display_share_info_1(struct net_context *c,
3020 struct SHARE_INFO_1 *r)
3022 if (c->opt_long_list_entries) {
3023 d_printf("%-12s %-8.8s %-50s\n",
3025 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3028 d_printf("%s\n", r->shi1_netname);
3032 static WERROR get_share_info(struct net_context *c,
3033 struct rpc_pipe_client *pipe_hnd,
3034 TALLOC_CTX *mem_ctx,
3038 struct srvsvc_NetShareInfoCtr *info_ctr)
3042 union srvsvc_NetShareInfo info;
3044 /* no specific share requested, enumerate all */
3047 uint32_t preferred_len = 0xffffffff;
3048 uint32_t total_entries = 0;
3049 uint32_t resume_handle = 0;
3051 info_ctr->level = level;
3053 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3063 /* request just one share */
3064 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3071 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3076 ZERO_STRUCTP(info_ctr);
3078 info_ctr->level = level;
3083 struct srvsvc_NetShareCtr1 *ctr1;
3085 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3086 W_ERROR_HAVE_NO_MEMORY(ctr1);
3089 ctr1->array = info.info1;
3091 info_ctr->ctr.ctr1 = ctr1;
3095 struct srvsvc_NetShareCtr2 *ctr2;
3097 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3098 W_ERROR_HAVE_NO_MEMORY(ctr2);
3101 ctr2->array = info.info2;
3103 info_ctr->ctr.ctr2 = ctr2;
3107 struct srvsvc_NetShareCtr502 *ctr502;
3109 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3110 W_ERROR_HAVE_NO_MEMORY(ctr502);
3113 ctr502->array = info.info502;
3115 info_ctr->ctr.ctr502 = ctr502;
3123 * 'net rpc share list' entrypoint.
3124 * @param argc Standard main() style argc.
3125 * @param argv Standard main() style argv. Initial components are already
3128 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3130 NET_API_STATUS status;
3131 struct SHARE_INFO_1 *i1 = NULL;
3132 uint32_t entries_read = 0;
3133 uint32_t total_entries = 0;
3134 uint32_t resume_handle = 0;
3135 uint32_t i, level = 1;
3137 if (c->display_usage) {
3139 "net rpc share list\n"
3142 _("List shares on remote server"));
3146 status = NetShareEnum(c->opt_host,
3148 (uint8_t **)(void *)&i1,
3157 /* Display results */
3159 if (c->opt_long_list_entries) {
3161 "\nEnumerating shared resources (exports) on remote server:\n\n"
3162 "\nShare name Type Description\n"
3163 "---------- ---- -----------\n"));
3165 for (i = 0; i < entries_read; i++)
3166 display_share_info_1(c, &i1[i]);
3171 static bool check_share_availability(struct cli_state *cli, const char *netname)
3175 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3176 if (!NT_STATUS_IS_OK(status)) {
3177 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3181 status = cli_tdis(cli);
3182 if (!NT_STATUS_IS_OK(status)) {
3183 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3190 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3191 const char *netname, uint32 type)
3193 /* only support disk shares */
3194 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3195 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3200 /* skip builtin shares */
3201 /* FIXME: should print$ be added too ? */
3202 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3203 strequal(netname,"global"))
3206 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3207 printf(_("excluding [%s]\n"), netname);
3211 return check_share_availability(cli, netname);
3215 * Migrate shares from a remote RPC server to the local RPC server.
3217 * All parameters are provided by the run_rpc_command function, except for
3218 * argc, argv which are passed through.
3220 * @param domain_sid The domain sid acquired from the remote server.
3221 * @param cli A cli_state connected to the server.
3222 * @param mem_ctx Talloc context, destroyed on completion of the function.
3223 * @param argc Standard main() style argc.
3224 * @param argv Standard main() style argv. Initial components are already
3227 * @return Normal NTSTATUS return.
3230 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3231 const struct dom_sid *domain_sid,
3232 const char *domain_name,
3233 struct cli_state *cli,
3234 struct rpc_pipe_client *pipe_hnd,
3235 TALLOC_CTX *mem_ctx,
3240 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3241 struct srvsvc_NetShareInfoCtr ctr_src;
3243 struct rpc_pipe_client *srvsvc_pipe = NULL;
3244 struct cli_state *cli_dst = NULL;
3245 uint32 level = 502; /* includes secdesc */
3246 uint32_t parm_error = 0;
3248 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3250 if (!W_ERROR_IS_OK(result))
3253 /* connect destination PI_SRVSVC */
3254 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3255 &ndr_table_srvsvc.syntax_id);
3256 if (!NT_STATUS_IS_OK(nt_status))
3260 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3262 union srvsvc_NetShareInfo info;
3263 struct srvsvc_NetShareInfo502 info502 =
3264 ctr_src.ctr.ctr502->array[i];
3266 /* reset error-code */
3267 nt_status = NT_STATUS_UNSUCCESSFUL;
3269 if (!check_share_sanity(c, cli, info502.name, info502.type))
3272 /* finally add the share on the dst server */
3274 printf(_("migrating: [%s], path: %s, comment: %s, without "
3276 info502.name, info502.path, info502.comment);
3278 info.info502 = &info502;
3280 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3281 srvsvc_pipe->desthost,
3287 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3288 printf(_(" [%s] does already exist\n"),
3293 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3294 printf(_("cannot add share: %s\n"), win_errstr(result));
3300 nt_status = NT_STATUS_OK;
3304 cli_shutdown(cli_dst);
3312 * Migrate shares from a RPC server to another.
3314 * @param argc Standard main() style argc.
3315 * @param argv Standard main() style argv. Initial components are already
3318 * @return A shell status integer (0 for success).
3320 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3323 if (c->display_usage) {
3325 "net rpc share migrate shares\n"
3328 _("Migrate shares to local server"));
3333 printf(_("no server to migrate\n"));
3337 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3338 rpc_share_migrate_shares_internals,
3345 * @param f file_info
3346 * @param mask current search mask
3347 * @param state arg-pointer
3350 static void copy_fn(const char *mnt, struct file_info *f,
3351 const char *mask, void *state)
3353 static NTSTATUS nt_status;
3354 static struct copy_clistate *local_state;
3355 static fstring filename, new_mask;
3358 struct net_context *c;
3360 local_state = (struct copy_clistate *)state;
3361 nt_status = NT_STATUS_UNSUCCESSFUL;
3365 if (strequal(f->name, ".") || strequal(f->name, ".."))
3368 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3371 if (f->mode & aDIR) {
3373 DEBUG(3,("got dir: %s\n", f->name));
3375 fstrcpy(dir, local_state->cwd);
3377 fstrcat(dir, f->name);
3379 switch (net_mode_share)
3381 case NET_MODE_SHARE_MIGRATE:
3382 /* create that directory */
3383 nt_status = net_copy_file(c, local_state->mem_ctx,
3384 local_state->cli_share_src,
3385 local_state->cli_share_dst,
3387 c->opt_acls? true : false,
3388 c->opt_attrs? true : false,
3389 c->opt_timestamps? true:false,
3393 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3397 if (!NT_STATUS_IS_OK(nt_status))
3398 printf(_("could not handle dir %s: %s\n"),
3399 dir, nt_errstr(nt_status));
3401 /* search below that directory */
3402 fstrcpy(new_mask, dir);
3403 fstrcat(new_mask, "\\*");
3405 old_dir = local_state->cwd;
3406 local_state->cwd = dir;
3407 if (!sync_files(local_state, new_mask))
3408 printf(_("could not handle files\n"));
3409 local_state->cwd = old_dir;
3416 fstrcpy(filename, local_state->cwd);
3417 fstrcat(filename, "\\");
3418 fstrcat(filename, f->name);
3420 DEBUG(3,("got file: %s\n", filename));
3422 switch (net_mode_share)
3424 case NET_MODE_SHARE_MIGRATE:
3425 nt_status = net_copy_file(c, local_state->mem_ctx,
3426 local_state->cli_share_src,
3427 local_state->cli_share_dst,
3429 c->opt_acls? true : false,
3430 c->opt_attrs? true : false,
3431 c->opt_timestamps? true: false,
3435 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3440 if (!NT_STATUS_IS_OK(nt_status))
3441 printf(_("could not handle file %s: %s\n"),
3442 filename, nt_errstr(nt_status));
3447 * sync files, can be called recursivly to list files
3448 * and then call copy_fn for each file
3450 * @param cp_clistate pointer to the copy_clistate we work with
3451 * @param mask the current search mask
3453 * @return Boolean result
3455 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3457 struct cli_state *targetcli;
3458 char *targetpath = NULL;
3461 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3463 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3464 mask, &targetcli, &targetpath ) ) {
3465 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3467 mask, cli_errstr(cp_clistate->cli_share_src));
3471 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3472 copy_fn, cp_clistate);
3473 if (!NT_STATUS_IS_OK(status)) {
3474 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3475 mask, nt_errstr(status));
3484 * Set the top level directory permissions before we do any further copies.
3485 * Should set up ACL inheritance.
3488 bool copy_top_level_perms(struct net_context *c,
3489 struct copy_clistate *cp_clistate,
3490 const char *sharename)
3492 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3494 switch (net_mode_share) {
3495 case NET_MODE_SHARE_MIGRATE:
3496 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3497 nt_status = net_copy_fileattr(c,
3498 cp_clistate->mem_ctx,
3499 cp_clistate->cli_share_src,
3500 cp_clistate->cli_share_dst,
3502 c->opt_acls? true : false,
3503 c->opt_attrs? true : false,
3504 c->opt_timestamps? true: false,
3508 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3512 if (!NT_STATUS_IS_OK(nt_status)) {
3513 printf(_("Could handle directory attributes for top level "
3514 "directory of share %s. Error %s\n"),
3515 sharename, nt_errstr(nt_status));
3523 * Sync all files inside a remote share to another share (over smb).
3525 * All parameters are provided by the run_rpc_command function, except for
3526 * argc, argv which are passed through.
3528 * @param domain_sid The domain sid acquired from the remote server.
3529 * @param cli A cli_state connected to the server.
3530 * @param mem_ctx Talloc context, destroyed on completion of the function.
3531 * @param argc Standard main() style argc.
3532 * @param argv Standard main() style argv. Initial components are already
3535 * @return Normal NTSTATUS return.
3538 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3539 const struct dom_sid *domain_sid,
3540 const char *domain_name,
3541 struct cli_state *cli,
3542 struct rpc_pipe_client *pipe_hnd,
3543 TALLOC_CTX *mem_ctx,
3548 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3549 struct srvsvc_NetShareInfoCtr ctr_src;
3552 struct copy_clistate cp_clistate;
3553 bool got_src_share = false;
3554 bool got_dst_share = false;
3555 const char *mask = "\\*";
3558 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3560 nt_status = NT_STATUS_NO_MEMORY;
3564 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3567 if (!W_ERROR_IS_OK(result))
3570 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3572 struct srvsvc_NetShareInfo502 info502 =
3573 ctr_src.ctr.ctr502->array[i];
3575 if (!check_share_sanity(c, cli, info502.name, info502.type))
3578 /* one might not want to mirror whole discs :) */
3579 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3580 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3585 switch (net_mode_share)
3587 case NET_MODE_SHARE_MIGRATE:
3591 d_fprintf(stderr, _("Unsupported mode %d\n"),
3595 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3598 c->opt_acls ? _("including") : _("without"),
3599 c->opt_attrs ? _("including") : _("without"),
3600 c->opt_timestamps ? _("(preserving timestamps)") : "");
3602 cp_clistate.mem_ctx = mem_ctx;
3603 cp_clistate.cli_share_src = NULL;
3604 cp_clistate.cli_share_dst = NULL;
3605 cp_clistate.cwd = NULL;
3606 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3609 /* open share source */
3610 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3611 &cli->dest_ss, cli->desthost,
3612 info502.name, "A:");
3613 if (!NT_STATUS_IS_OK(nt_status))
3616 got_src_share = true;
3618 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3619 /* open share destination */
3620 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3621 NULL, dst, info502.name, "A:");
3622 if (!NT_STATUS_IS_OK(nt_status))
3625 got_dst_share = true;
3628 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3629 d_fprintf(stderr, _("Could not handle the top level "
3630 "directory permissions for the "
3631 "share: %s\n"), info502.name);
3632 nt_status = NT_STATUS_UNSUCCESSFUL;
3636 if (!sync_files(&cp_clistate, mask)) {
3637 d_fprintf(stderr, _("could not handle files for share: "
3638 "%s\n"), info502.name);
3639 nt_status = NT_STATUS_UNSUCCESSFUL;
3644 nt_status = NT_STATUS_OK;
3649 cli_shutdown(cp_clistate.cli_share_src);
3652 cli_shutdown(cp_clistate.cli_share_dst);
3659 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3661 if (c->display_usage) {
3663 "net share migrate files\n"
3666 _("Migrate files to local server"));
3671 d_printf(_("no server to migrate\n"));
3675 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3676 rpc_share_migrate_files_internals,
3681 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3683 * All parameters are provided by the run_rpc_command function, except for
3684 * argc, argv which are passed through.
3686 * @param domain_sid The domain sid acquired from the remote server.
3687 * @param cli A cli_state connected to the server.
3688 * @param mem_ctx Talloc context, destroyed on completion of the function.
3689 * @param argc Standard main() style argc.
3690 * @param argv Standard main() style argv. Initial components are already
3693 * @return Normal NTSTATUS return.
3696 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3697 const struct dom_sid *domain_sid,
3698 const char *domain_name,
3699 struct cli_state *cli,
3700 struct rpc_pipe_client *pipe_hnd,
3701 TALLOC_CTX *mem_ctx,
3706 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3707 struct srvsvc_NetShareInfoCtr ctr_src;
3708 union srvsvc_NetShareInfo info;
3710 struct rpc_pipe_client *srvsvc_pipe = NULL;
3711 struct cli_state *cli_dst = NULL;
3712 uint32 level = 502; /* includes secdesc */
3713 uint32_t parm_error = 0;
3715 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3718 if (!W_ERROR_IS_OK(result))
3721 /* connect destination PI_SRVSVC */
3722 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3723 &ndr_table_srvsvc.syntax_id);
3724 if (!NT_STATUS_IS_OK(nt_status))
3728 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3730 struct srvsvc_NetShareInfo502 info502 =
3731 ctr_src.ctr.ctr502->array[i];
3733 /* reset error-code */
3734 nt_status = NT_STATUS_UNSUCCESSFUL;
3736 if (!check_share_sanity(c, cli, info502.name, info502.type))
3739 printf(_("migrating: [%s], path: %s, comment: %s, including "
3741 info502.name, info502.path, info502.comment);
3744 display_sec_desc(info502.sd_buf.sd);
3746 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3747 info.info502 = &info502;
3749 /* finally modify the share on the dst server */
3750 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3751 srvsvc_pipe->desthost,
3757 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3758 printf(_("cannot set share-acl: %s\n"),
3759 win_errstr(result));
3765 nt_status = NT_STATUS_OK;
3769 cli_shutdown(cli_dst);
3777 * Migrate share-acls from a RPC server to another.
3779 * @param argc Standard main() style argc.
3780 * @param argv Standard main() style argv. Initial components are already
3783 * @return A shell status integer (0 for success).
3785 static int rpc_share_migrate_security(struct net_context *c, int argc,
3788 if (c->display_usage) {
3790 "net rpc share migrate security\n"
3793 _("Migrate share-acls to local server"));
3798 d_printf(_("no server to migrate\n"));
3802 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3803 rpc_share_migrate_security_internals,
3808 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3809 * from one server to another.
3811 * @param argc Standard main() style argc.
3812 * @param argv Standard main() style argv. Initial components are already
3815 * @return A shell status integer (0 for success).
3818 static int rpc_share_migrate_all(struct net_context *c, int argc,
3823 if (c->display_usage) {
3825 "net rpc share migrate all\n"
3828 _("Migrates shares including all share settings"));
3833 d_printf(_("no server to migrate\n"));
3837 /* order is important. we don't want to be locked out by the share-acl
3838 * before copying files - gd */
3840 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3841 rpc_share_migrate_shares_internals, argc, argv);
3845 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3846 rpc_share_migrate_files_internals, argc, argv);
3850 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3851 rpc_share_migrate_security_internals, argc,
3857 * 'net rpc share migrate' entrypoint.
3858 * @param argc Standard main() style argc.
3859 * @param argv Standard main() style argv. Initial components are already
3862 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3865 struct functable func[] = {
3868 rpc_share_migrate_all,
3870 N_("Migrate shares from remote to local server"),
3871 N_("net rpc share migrate all\n"
3872 " Migrate shares from remote to local server")
3876 rpc_share_migrate_files,
3878 N_("Migrate files from remote to local server"),
3879 N_("net rpc share migrate files\n"
3880 " Migrate files from remote to local server")
3884 rpc_share_migrate_security,
3886 N_("Migrate share-ACLs from remote to local server"),
3887 N_("net rpc share migrate security\n"
3888 " Migrate share-ACLs from remote to local server")
3892 rpc_share_migrate_shares,
3894 N_("Migrate shares from remote to local server"),
3895 N_("net rpc share migrate shares\n"
3896 " Migrate shares from remote to local server")
3898 {NULL, NULL, 0, NULL, NULL}
3901 net_mode_share = NET_MODE_SHARE_MIGRATE;
3903 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3909 struct dom_sid *members;
3912 static int num_server_aliases;
3913 static struct full_alias *server_aliases;
3916 * Add an alias to the static list.
3918 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3920 if (server_aliases == NULL)
3921 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3923 server_aliases[num_server_aliases] = *alias;
3924 num_server_aliases += 1;
3928 * For a specific domain on the server, fetch all the aliases
3929 * and their members. Add all of them to the server_aliases.
3932 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3933 TALLOC_CTX *mem_ctx,
3934 struct policy_handle *connect_pol,
3935 const struct dom_sid *domain_sid)
3937 uint32 start_idx, max_entries, num_entries, i;
3938 struct samr_SamArray *groups = NULL;
3940 struct policy_handle domain_pol;
3942 /* Get domain policy handle */
3944 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3946 MAXIMUM_ALLOWED_ACCESS,
3947 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3949 if (!NT_STATUS_IS_OK(result))
3956 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3962 for (i = 0; i < num_entries; i++) {
3964 struct policy_handle alias_pol;
3965 struct full_alias alias;
3966 struct lsa_SidArray sid_array;
3969 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3971 MAXIMUM_ALLOWED_ACCESS,
3972 groups->entries[i].idx,
3974 if (!NT_STATUS_IS_OK(result))
3977 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3980 if (!NT_STATUS_IS_OK(result))
3983 alias.num_members = sid_array.num_sids;
3985 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3986 if (!NT_STATUS_IS_OK(result))
3989 alias.members = NULL;
3991 if (alias.num_members > 0) {
3992 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
3994 for (j = 0; j < alias.num_members; j++)
3995 sid_copy(&alias.members[j],
3996 sid_array.sids[j].sid);
3999 sid_compose(&alias.sid, domain_sid,
4000 groups->entries[i].idx);
4002 push_alias(mem_ctx, &alias);
4004 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4006 result = NT_STATUS_OK;
4009 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4015 * Dump server_aliases as names for debugging purposes.
4018 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4019 const struct dom_sid *domain_sid,
4020 const char *domain_name,
4021 struct cli_state *cli,
4022 struct rpc_pipe_client *pipe_hnd,
4023 TALLOC_CTX *mem_ctx,
4029 struct policy_handle lsa_pol;
4031 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4032 SEC_FLAG_MAXIMUM_ALLOWED,
4034 if (!NT_STATUS_IS_OK(result))
4037 for (i=0; i<num_server_aliases; i++) {
4040 enum lsa_SidType *types;
4043 struct full_alias *alias = &server_aliases[i];
4045 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4047 &domains, &names, &types);
4048 if (!NT_STATUS_IS_OK(result))
4051 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4053 if (alias->num_members == 0) {
4058 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4061 &domains, &names, &types);
4063 if (!NT_STATUS_IS_OK(result) &&
4064 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4067 for (j=0; j<alias->num_members; j++)
4068 DEBUG(1, ("%s\\%s (%d); ",
4069 domains[j] ? domains[j] : "*unknown*",
4070 names[j] ? names[j] : "*unknown*",types[j]));
4074 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4076 return NT_STATUS_OK;
4080 * Fetch a list of all server aliases and their members into
4084 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4085 const struct dom_sid *domain_sid,
4086 const char *domain_name,
4087 struct cli_state *cli,
4088 struct rpc_pipe_client *pipe_hnd,
4089 TALLOC_CTX *mem_ctx,
4094 struct policy_handle connect_pol;
4096 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4098 MAXIMUM_ALLOWED_ACCESS,
4101 if (!NT_STATUS_IS_OK(result))
4104 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4105 &global_sid_Builtin);
4107 if (!NT_STATUS_IS_OK(result))
4110 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4113 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4118 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4120 token->num_sids = 4;
4122 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4123 d_fprintf(stderr, "malloc %s\n",_("failed"));
4124 token->num_sids = 0;
4128 token->sids[0] = *user_sid;
4129 sid_copy(&token->sids[1], &global_sid_World);
4130 sid_copy(&token->sids[2], &global_sid_Network);
4131 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4134 static void free_user_token(struct security_token *token)
4136 SAFE_FREE(token->sids);
4139 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4141 if (is_sid_in_token(token, sid))
4144 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4149 sid_copy(&token->sids[token->num_sids], sid);
4151 token->num_sids += 1;
4156 struct security_token token;
4159 static void dump_user_token(struct user_token *token)
4163 d_printf("%s\n", token->name);
4165 for (i=0; i<token->token.num_sids; i++) {
4166 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4170 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4174 for (i=0; i<alias->num_members; i++) {
4175 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4182 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4186 for (i=0; i<num_server_aliases; i++) {
4187 if (is_alias_member(&sid, &server_aliases[i]))
4188 add_sid_to_token(token, &server_aliases[i].sid);
4193 * We got a user token with all the SIDs we can know about without asking the
4194 * server directly. These are the user and domain group sids. All of these can
4195 * be members of aliases. So scan the list of aliases for each of the SIDs and
4196 * add them to the token.
4199 static void collect_alias_memberships(struct security_token *token)
4201 int num_global_sids = token->num_sids;
4204 for (i=0; i<num_global_sids; i++) {
4205 collect_sid_memberships(token, token->sids[i]);
4209 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4211 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4212 enum wbcSidType type;
4214 struct wbcDomainSid wsid;
4215 char *sid_str = NULL;
4216 struct dom_sid user_sid;
4217 uint32_t num_groups;
4218 gid_t *groups = NULL;
4221 fstr_sprintf(full_name, "%s%c%s",
4222 domain, *lp_winbind_separator(), user);
4224 /* First let's find out the user sid */
4226 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4228 if (!WBC_ERROR_IS_OK(wbc_status)) {
4229 DEBUG(1, ("winbind could not find %s: %s\n",
4230 full_name, wbcErrorString(wbc_status)));
4234 wbc_status = wbcSidToString(&wsid, &sid_str);
4235 if (!WBC_ERROR_IS_OK(wbc_status)) {
4239 if (type != SID_NAME_USER) {
4240 wbcFreeMemory(sid_str);
4241 DEBUG(1, ("%s is not a user\n", full_name));
4245 if (!string_to_sid(&user_sid, sid_str)) {
4246 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4250 wbcFreeMemory(sid_str);
4253 init_user_token(token, &user_sid);
4255 /* And now the groups winbind knows about */
4257 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4258 if (!WBC_ERROR_IS_OK(wbc_status)) {
4259 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4260 full_name, wbcErrorString(wbc_status)));
4264 for (i = 0; i < num_groups; i++) {
4265 gid_t gid = groups[i];
4268 wbc_status = wbcGidToSid(gid, &wsid);
4269 if (!WBC_ERROR_IS_OK(wbc_status)) {
4270 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4271 (unsigned int)gid, wbcErrorString(wbc_status)));
4272 wbcFreeMemory(groups);
4276 wbc_status = wbcSidToString(&wsid, &sid_str);
4277 if (!WBC_ERROR_IS_OK(wbc_status)) {
4278 wbcFreeMemory(groups);
4282 DEBUG(3, (" %s\n", sid_str));
4284 string_to_sid(&sid, sid_str);
4285 wbcFreeMemory(sid_str);
4288 add_sid_to_token(token, &sid);
4290 wbcFreeMemory(groups);
4296 * Get a list of all user tokens we want to look at
4299 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4300 struct user_token **user_tokens)
4302 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4303 uint32_t i, num_users;
4305 struct user_token *result;
4306 TALLOC_CTX *frame = NULL;
4308 if (lp_winbind_use_default_domain() &&
4309 (c->opt_target_workgroup == NULL)) {
4310 d_fprintf(stderr, _("winbind use default domain = yes set, "
4311 "please specify a workgroup\n"));
4315 /* Send request to winbind daemon */
4317 wbc_status = wbcListUsers(NULL, &num_users, &users);
4318 if (!WBC_ERROR_IS_OK(wbc_status)) {
4319 DEBUG(1, (_("winbind could not list users: %s\n"),
4320 wbcErrorString(wbc_status)));
4324 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4326 if (result == NULL) {
4327 DEBUG(1, ("Could not malloc sid array\n"));
4328 wbcFreeMemory(users);
4332 frame = talloc_stackframe();
4333 for (i=0; i < num_users; i++) {
4334 fstring domain, user;
4337 fstrcpy(result[i].name, users[i]);
4339 p = strchr(users[i], *lp_winbind_separator());
4341 DEBUG(3, ("%s\n", users[i]));
4344 fstrcpy(domain, c->opt_target_workgroup);
4345 fstrcpy(user, users[i]);
4348 fstrcpy(domain, users[i]);
4353 get_user_sids(domain, user, &(result[i].token));
4357 wbcFreeMemory(users);
4359 *num_tokens = num_users;
4360 *user_tokens = result;
4365 static bool get_user_tokens_from_file(FILE *f,
4367 struct user_token **tokens)
4369 struct user_token *token = NULL;
4374 if (fgets(line, sizeof(line)-1, f) == NULL) {
4378 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4379 line[strlen(line)-1] = '\0';
4382 if (line[0] == ' ') {
4386 if(!string_to_sid(&sid, &line[1])) {
4387 DEBUG(1,("get_user_tokens_from_file: Could "
4388 "not convert sid %s \n",&line[1]));
4392 if (token == NULL) {
4393 DEBUG(0, ("File does not begin with username"));
4397 add_sid_to_token(&token->token, &sid);
4401 /* And a new user... */
4404 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4405 if (*tokens == NULL) {
4406 DEBUG(0, ("Could not realloc tokens\n"));
4410 token = &((*tokens)[*num_tokens-1]);
4412 fstrcpy(token->name, line);
4413 token->token.num_sids = 0;
4414 token->token.sids = NULL;
4423 * Show the list of all users that have access to a share
4426 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4427 TALLOC_CTX *mem_ctx,
4428 const char *netname,
4430 struct user_token *tokens)
4433 struct security_descriptor *share_sd = NULL;
4434 struct security_descriptor *root_sd = NULL;
4435 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4437 union srvsvc_NetShareInfo info;
4442 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4449 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4450 DEBUG(1, ("Coult not query secdesc for share %s\n",
4455 share_sd = info.info502->sd_buf.sd;
4456 if (share_sd == NULL) {
4457 DEBUG(1, ("Got no secdesc for share %s\n",
4463 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4467 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4468 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4469 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4472 for (i=0; i<num_tokens; i++) {
4475 if (share_sd != NULL) {
4476 status = se_access_check(share_sd, &tokens[i].token,
4479 if (!NT_STATUS_IS_OK(status)) {
4480 DEBUG(1, ("Could not check share_sd for "
4487 if (root_sd == NULL) {
4488 d_printf(" %s\n", tokens[i].name);
4492 status = se_access_check(root_sd, &tokens[i].token,
4494 if (!NT_STATUS_IS_OK(status)) {
4495 DEBUG(1, ("Could not check root_sd for user %s\n",
4499 d_printf(" %s\n", tokens[i].name);
4502 if (fnum != (uint16_t)-1)
4503 cli_close(cli, fnum);
4515 static void collect_share(const char *name, uint32 m,
4516 const char *comment, void *state)
4518 struct share_list *share_list = (struct share_list *)state;
4520 if (m != STYPE_DISKTREE)
4523 share_list->num_shares += 1;
4524 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4525 if (!share_list->shares) {
4526 share_list->num_shares = 0;
4529 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4533 * List shares on a remote RPC server, including the security descriptors.
4535 * All parameters are provided by the run_rpc_command function, except for
4536 * argc, argv which are passed through.
4538 * @param domain_sid The domain sid acquired from the remote server.
4539 * @param cli A cli_state connected to the server.
4540 * @param mem_ctx Talloc context, destroyed on completion of the function.
4541 * @param argc Standard main() style argc.
4542 * @param argv Standard main() style argv. Initial components are already
4545 * @return Normal NTSTATUS return.
4548 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4549 const struct dom_sid *domain_sid,
4550 const char *domain_name,
4551 struct cli_state *cli,
4552 struct rpc_pipe_client *pipe_hnd,
4553 TALLOC_CTX *mem_ctx,
4562 struct user_token *tokens = NULL;
4565 struct share_list share_list;
4570 f = fopen(argv[0], "r");
4574 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4575 return NT_STATUS_UNSUCCESSFUL;
4578 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4584 DEBUG(0, ("Could not read users from file\n"));
4585 return NT_STATUS_UNSUCCESSFUL;
4588 for (i=0; i<num_tokens; i++)
4589 collect_alias_memberships(&tokens[i].token);
4591 share_list.num_shares = 0;
4592 share_list.shares = NULL;
4594 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4597 DEBUG(0, ("Error returning browse list: %s\n",
4602 for (i = 0; i < share_list.num_shares; i++) {
4603 char *netname = share_list.shares[i];
4605 if (netname[strlen(netname)-1] == '$')
4608 d_printf("%s\n", netname);
4610 show_userlist(pipe_hnd, mem_ctx, netname,
4611 num_tokens, tokens);
4614 for (i=0; i<num_tokens; i++) {
4615 free_user_token(&tokens[i].token);
4618 SAFE_FREE(share_list.shares);
4620 return NT_STATUS_OK;
4623 static int rpc_share_allowedusers(struct net_context *c, int argc,
4628 if (c->display_usage) {
4630 "net rpc share allowedusers\n"
4633 _("List allowed users"));
4637 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4638 rpc_aliaslist_internals,
4643 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4649 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4650 rpc_share_allowedusers_internals,
4654 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4657 struct user_token *tokens = NULL;
4661 net_usersidlist_usage(c, argc, argv);
4665 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4666 DEBUG(0, ("Could not get the user/sid list\n"));
4670 for (i=0; i<num_tokens; i++) {
4671 dump_user_token(&tokens[i]);
4672 free_user_token(&tokens[i].token);
4679 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4681 d_printf(_("net usersidlist\n"
4682 "\tprints out a list of all users the running winbind knows\n"
4683 "\tabout, together with all their SIDs. This is used as\n"
4684 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4686 net_common_flags_usage(c, argc, argv);
4691 * 'net rpc share' entrypoint.
4692 * @param argc Standard main() style argc.
4693 * @param argv Standard main() style argv. Initial components are already
4697 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4699 NET_API_STATUS status;
4701 struct functable func[] = {
4707 N_("net rpc share add\n"
4715 N_("net rpc share delete\n"
4720 rpc_share_allowedusers,
4722 N_("Modify allowed users"),
4723 N_("net rpc share allowedusers\n"
4724 " Modify allowed users")
4730 N_("Migrate share to local server"),
4731 N_("net rpc share migrate\n"
4732 " Migrate share to local server")
4739 N_("net rpc share list\n"
4742 {NULL, NULL, 0, NULL, NULL}
4745 status = libnetapi_init(&c->netapi_ctx);
4749 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4750 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4751 if (c->opt_kerberos) {
4752 libnetapi_set_use_kerberos(c->netapi_ctx);
4756 if (c->display_usage) {
4761 " Alias for net rpc share list\n"));
4762 net_display_usage_from_functable(func);
4766 return rpc_share_list(c, argc, argv);
4769 return net_run_function(c, argc, argv, "net rpc share", func);
4772 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4773 TALLOC_CTX *mem_ctx,
4774 struct rpc_sh_ctx *ctx,
4775 struct rpc_pipe_client *pipe_hnd,
4776 int argc, const char **argv)
4779 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4782 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4783 TALLOC_CTX *mem_ctx,
4784 struct rpc_sh_ctx *ctx,
4785 struct rpc_pipe_client *pipe_hnd,
4786 int argc, const char **argv)
4788 NET_API_STATUS status;
4789 uint32_t parm_err = 0;
4790 struct SHARE_INFO_2 i2;
4792 if ((argc < 2) || (argc > 3)) {
4793 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
4795 return NT_STATUS_INVALID_PARAMETER;
4798 i2.shi2_netname = argv[0];
4799 i2.shi2_type = STYPE_DISKTREE;
4800 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4801 i2.shi2_permissions = 0;
4802 i2.shi2_max_uses = 0;
4803 i2.shi2_current_uses = 0;
4804 i2.shi2_path = argv[1];
4805 i2.shi2_passwd = NULL;
4807 status = NetShareAdd(pipe_hnd->desthost,
4812 return werror_to_ntstatus(W_ERROR(status));
4815 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4816 TALLOC_CTX *mem_ctx,
4817 struct rpc_sh_ctx *ctx,
4818 struct rpc_pipe_client *pipe_hnd,
4819 int argc, const char **argv)
4822 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4823 return NT_STATUS_INVALID_PARAMETER;
4826 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4829 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4830 TALLOC_CTX *mem_ctx,
4831 struct rpc_sh_ctx *ctx,
4832 struct rpc_pipe_client *pipe_hnd,
4833 int argc, const char **argv)
4835 union srvsvc_NetShareInfo info;
4840 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4841 return NT_STATUS_INVALID_PARAMETER;
4844 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4850 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4854 d_printf(_("Name: %s\n"), info.info2->name);
4855 d_printf(_("Comment: %s\n"), info.info2->comment);
4856 d_printf(_("Path: %s\n"), info.info2->path);
4857 d_printf(_("Password: %s\n"), info.info2->password);
4860 return werror_to_ntstatus(result);
4863 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4864 struct rpc_sh_ctx *ctx)
4866 static struct rpc_sh_cmd cmds[] = {
4868 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4869 N_("List available shares") },
4871 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4872 N_("Add a share") },
4874 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4875 N_("Delete a share") },
4877 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4878 N_("Get information about a share") },
4880 { NULL, NULL, 0, NULL, NULL }
4886 /****************************************************************************/
4888 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4890 return net_file_usage(c, argc, argv);
4894 * Close a file on a remote RPC server.
4896 * @param argc Standard main() style argc.
4897 * @param argv Standard main() style argv. Initial components are already
4900 * @return A shell status integer (0 for success).
4902 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4904 if (argc < 1 || c->display_usage) {
4905 return rpc_file_usage(c, argc, argv);
4908 return NetFileClose(c->opt_host, atoi(argv[0]));
4912 * Formatted print of open file info
4914 * @param r struct FILE_INFO_3 contents
4917 static void display_file_info_3(struct FILE_INFO_3 *r)
4919 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4920 r->fi3_id, r->fi3_username, r->fi3_permissions,
4921 r->fi3_num_locks, r->fi3_pathname);
4925 * List files for a user on a remote RPC server.
4927 * @param argc Standard main() style argc.
4928 * @param argv Standard main() style argv. Initial components are already
4931 * @return A shell status integer (0 for success)..
4934 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4936 NET_API_STATUS status;
4937 uint32 preferred_len = 0xffffffff, i;
4938 const char *username=NULL;
4939 uint32_t total_entries = 0;
4940 uint32_t entries_read = 0;
4941 uint32_t resume_handle = 0;
4942 struct FILE_INFO_3 *i3 = NULL;
4944 if (c->display_usage) {
4945 return rpc_file_usage(c, argc, argv);
4948 /* if argc > 0, must be user command */
4950 username = smb_xstrdup(argv[0]);
4953 status = NetFileEnum(c->opt_host,
4957 (uint8_t **)(void *)&i3,
4967 /* Display results */
4970 "\nEnumerating open files on remote server:\n\n"
4971 "\nFileId Opened by Perms Locks Path"
4972 "\n------ --------- ----- ----- ---- \n"));
4973 for (i = 0; i < entries_read; i++) {
4974 display_file_info_3(&i3[i]);
4981 * 'net rpc file' entrypoint.
4982 * @param argc Standard main() style argc.
4983 * @param argv Standard main() style argv. Initial components are already
4987 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4989 NET_API_STATUS status;
4991 struct functable func[] = {
4996 N_("Close opened file"),
4997 N_("net rpc file close\n"
4998 " Close opened file")
5004 N_("List files opened by user"),
5005 N_("net rpc file user\n"
5006 " List files opened by user")
5013 N_("Display information about opened file"),
5014 N_("net rpc file info\n"
5015 " Display information about opened file")
5018 {NULL, NULL, 0, NULL, NULL}
5021 status = libnetapi_init(&c->netapi_ctx);
5025 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5026 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5027 if (c->opt_kerberos) {
5028 libnetapi_set_use_kerberos(c->netapi_ctx);
5032 if (c->display_usage) {
5033 d_printf(_("Usage:\n"));
5034 d_printf(_("net rpc file\n"
5035 " List opened files\n"));
5036 net_display_usage_from_functable(func);
5040 return rpc_file_user(c, argc, argv);
5043 return net_run_function(c, argc, argv, "net rpc file", func);
5047 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5049 * All parameters are provided by the run_rpc_command function, except for
5050 * argc, argv which are passed through.
5052 * @param c A net_context structure.
5053 * @param domain_sid The domain sid acquired from the remote server.
5054 * @param cli A cli_state connected to the server.
5055 * @param mem_ctx Talloc context, destroyed on completion of the function.
5056 * @param argc Standard main() style argc.
5057 * @param argv Standard main() style argv. Initial components are already
5060 * @return Normal NTSTATUS return.
5063 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5064 const struct dom_sid *domain_sid,
5065 const char *domain_name,
5066 struct cli_state *cli,
5067 struct rpc_pipe_client *pipe_hnd,
5068 TALLOC_CTX *mem_ctx,
5072 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5074 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5076 if (NT_STATUS_IS_OK(result)) {
5077 d_printf(_("\nShutdown successfully aborted\n"));
5078 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5080 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5086 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5088 * All parameters are provided by the run_rpc_command function, except for
5089 * argc, argv which are passed through.
5091 * @param c A net_context structure.
5092 * @param domain_sid The domain sid acquired from the remote server.
5093 * @param cli A cli_state connected to the server.
5094 * @param mem_ctx Talloc context, destroyed on completion of the function.
5095 * @param argc Standard main() style argc.
5096 * @param argv Standard main() style argv. Initial components are already
5099 * @return Normal NTSTATUS return.
5102 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5103 const struct dom_sid *domain_sid,
5104 const char *domain_name,
5105 struct cli_state *cli,
5106 struct rpc_pipe_client *pipe_hnd,
5107 TALLOC_CTX *mem_ctx,
5111 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5113 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5115 if (NT_STATUS_IS_OK(result)) {
5116 d_printf(_("\nShutdown successfully aborted\n"));
5117 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5119 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5125 * ABORT the shutdown of a remote RPC server.
5127 * @param argc Standard main() style argc.
5128 * @param argv Standard main() style argv. Initial components are already
5131 * @return A shell status integer (0 for success).
5134 static int rpc_shutdown_abort(struct net_context *c, int argc,
5139 if (c->display_usage) {
5141 "net rpc abortshutdown\n"
5144 _("Abort a scheduled shutdown"));
5148 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5149 rpc_shutdown_abort_internals, argc, argv);
5154 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5156 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5157 rpc_reg_shutdown_abort_internals,
5162 * Shut down a remote RPC Server via initshutdown pipe.
5164 * All parameters are provided by the run_rpc_command function, except for
5165 * argc, argv which are passed through.
5167 * @param c A net_context structure.
5168 * @param domain_sid The domain sid acquired from the remote server.
5169 * @param cli A cli_state connected to the server.
5170 * @param mem_ctx Talloc context, destroyed on completion of the function.
5171 * @param argc Standard main() style argc.
5172 * @param argv Standard main() style argv. Initial components are already
5175 * @return Normal NTSTATUS return.
5178 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5179 const struct dom_sid *domain_sid,
5180 const char *domain_name,
5181 struct cli_state *cli,
5182 struct rpc_pipe_client *pipe_hnd,
5183 TALLOC_CTX *mem_ctx,
5187 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5188 const char *msg = N_("This machine will be shutdown shortly");
5189 uint32 timeout = 20;
5190 struct lsa_StringLarge msg_string;
5192 if (c->opt_comment) {
5193 msg = c->opt_comment;
5195 if (c->opt_timeout) {
5196 timeout = c->opt_timeout;
5199 msg_string.string = msg;
5201 /* create an entry */
5202 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5203 &msg_string, timeout, c->opt_force, c->opt_reboot,
5206 if (NT_STATUS_IS_OK(result)) {
5207 d_printf(_("\nShutdown of remote machine succeeded\n"));
5208 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5210 DEBUG(1,("Shutdown of remote machine failed!\n"));
5216 * Shut down a remote RPC Server via winreg pipe.
5218 * All parameters are provided by the run_rpc_command function, except for
5219 * argc, argv which are passed through.
5221 * @param c A net_context structure.
5222 * @param domain_sid The domain sid acquired from the remote server.
5223 * @param cli A cli_state connected to the server.
5224 * @param mem_ctx Talloc context, destroyed on completion of the function.
5225 * @param argc Standard main() style argc.
5226 * @param argv Standard main() style argv. Initial components are already
5229 * @return Normal NTSTATUS return.
5232 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5233 const struct dom_sid *domain_sid,
5234 const char *domain_name,
5235 struct cli_state *cli,
5236 struct rpc_pipe_client *pipe_hnd,
5237 TALLOC_CTX *mem_ctx,
5241 const char *msg = N_("This machine will be shutdown shortly");
5242 uint32 timeout = 20;
5243 struct lsa_StringLarge msg_string;
5247 if (c->opt_comment) {
5248 msg = c->opt_comment;
5250 msg_string.string = msg;
5252 if (c->opt_timeout) {
5253 timeout = c->opt_timeout;
5256 /* create an entry */
5257 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5258 &msg_string, timeout, c->opt_force, c->opt_reboot,
5261 if (NT_STATUS_IS_OK(result)) {
5262 d_printf(_("\nShutdown of remote machine succeeded\n"));
5264 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5265 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5266 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5268 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5275 * Shut down a remote RPC server.
5277 * @param argc Standard main() style argc.
5278 * @param argv Standard main() style argv. Initial components are already
5281 * @return A shell status integer (0 for success).
5284 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5288 if (c->display_usage) {
5290 "net rpc shutdown\n"
5293 _("Shut down a remote RPC server"));
5297 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5298 rpc_init_shutdown_internals, argc, argv);
5301 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5302 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5303 rpc_reg_shutdown_internals, argc, argv);
5309 /***************************************************************************
5310 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5311 ***************************************************************************/
5314 * Add interdomain trust account to the RPC server.
5315 * All parameters (except for argc and argv) are passed by run_rpc_command
5318 * @param c A net_context structure.
5319 * @param domain_sid The domain sid acquired from the server.
5320 * @param cli A cli_state connected to the server.
5321 * @param mem_ctx Talloc context, destroyed on completion of the function.
5322 * @param argc Standard main() style argc.
5323 * @param argv Standard main() style argv. Initial components are already
5326 * @return normal NTSTATUS return code.
5329 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5330 const struct dom_sid *domain_sid,
5331 const char *domain_name,
5332 struct cli_state *cli,
5333 struct rpc_pipe_client *pipe_hnd,
5334 TALLOC_CTX *mem_ctx,
5338 struct policy_handle connect_pol, domain_pol, user_pol;
5339 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5341 struct lsa_String lsa_acct_name;
5343 uint32 acct_flags=0;
5345 uint32_t access_granted = 0;
5346 union samr_UserInfo info;
5347 unsigned int orig_timeout;
5352 _(" net rpc trustdom add <domain_name> "
5353 "<trust password>\n"));
5354 return NT_STATUS_INVALID_PARAMETER;
5358 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5361 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5362 return NT_STATUS_NO_MEMORY;
5365 strupper_m(acct_name);
5367 init_lsa_String(&lsa_acct_name, acct_name);
5369 /* Get samr policy handle */
5370 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5372 MAXIMUM_ALLOWED_ACCESS,
5374 if (!NT_STATUS_IS_OK(result)) {
5378 /* Get domain policy handle */
5379 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5381 MAXIMUM_ALLOWED_ACCESS,
5382 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5384 if (!NT_STATUS_IS_OK(result)) {
5388 /* This call can take a long time - allow the server to time out.
5389 * 35 seconds should do it. */
5391 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5393 /* Create trusting domain's account */
5394 acb_info = ACB_NORMAL;
5395 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5396 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5397 SAMR_USER_ACCESS_SET_PASSWORD |
5398 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5399 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5401 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5410 /* And restore our original timeout. */
5411 rpccli_set_timeout(pipe_hnd, orig_timeout);
5413 if (!NT_STATUS_IS_OK(result)) {
5414 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5415 acct_name, nt_errstr(result));
5420 struct samr_CryptPassword crypt_pwd;
5422 ZERO_STRUCT(info.info23);
5424 init_samr_CryptPassword(argv[1],
5425 &cli->user_session_key,
5428 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5429 SAMR_FIELD_NT_PASSWORD_PRESENT;
5430 info.info23.info.acct_flags = ACB_DOMTRUST;
5431 info.info23.password = crypt_pwd;
5433 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5438 if (!NT_STATUS_IS_OK(result)) {
5439 DEBUG(0,("Could not set trust account password: %s\n",
5440 nt_errstr(result)));
5446 SAFE_FREE(acct_name);
5451 * Create interdomain trust account for a remote domain.
5453 * @param argc Standard argc.
5454 * @param argv Standard argv without initial components.
5456 * @return Integer status (0 means success).
5459 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5461 if (argc > 0 && !c->display_usage) {
5462 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5463 rpc_trustdom_add_internals, argc, argv);
5467 _("net rpc trustdom add <domain_name> <trust "
5475 * Remove interdomain trust account from the RPC server.
5476 * All parameters (except for argc and argv) are passed by run_rpc_command
5479 * @param c A net_context structure.
5480 * @param domain_sid The domain sid acquired from the server.
5481 * @param cli A cli_state connected to the server.
5482 * @param mem_ctx Talloc context, destroyed on completion of the function.
5483 * @param argc Standard main() style argc.
5484 * @param argv Standard main() style argv. Initial components are already
5487 * @return normal NTSTATUS return code.
5490 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5491 const struct dom_sid *domain_sid,
5492 const char *domain_name,
5493 struct cli_state *cli,
5494 struct rpc_pipe_client *pipe_hnd,
5495 TALLOC_CTX *mem_ctx,
5499 struct policy_handle connect_pol, domain_pol, user_pol;
5500 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5502 struct dom_sid trust_acct_sid;
5503 struct samr_Ids user_rids, name_types;
5504 struct lsa_String lsa_acct_name;
5509 _(" net rpc trustdom del <domain_name>\n"));
5510 return NT_STATUS_INVALID_PARAMETER;
5514 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5516 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5518 if (acct_name == NULL)
5519 return NT_STATUS_NO_MEMORY;
5521 strupper_m(acct_name);
5523 /* Get samr policy handle */
5524 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5526 MAXIMUM_ALLOWED_ACCESS,
5528 if (!NT_STATUS_IS_OK(result)) {
5532 /* Get domain policy handle */
5533 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5535 MAXIMUM_ALLOWED_ACCESS,
5536 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5538 if (!NT_STATUS_IS_OK(result)) {
5542 init_lsa_String(&lsa_acct_name, acct_name);
5544 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5551 if (!NT_STATUS_IS_OK(result)) {
5552 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5554 acct_name, nt_errstr(result) );
5558 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5560 MAXIMUM_ALLOWED_ACCESS,
5564 if (!NT_STATUS_IS_OK(result)) {
5565 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5567 acct_name, nt_errstr(result) );
5571 /* append the rid to the domain sid */
5572 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
5576 /* remove the sid */
5578 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5581 if (!NT_STATUS_IS_OK(result)) {
5582 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5583 " on user %s failed %s\n"),
5584 acct_name, nt_errstr(result) );
5590 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5593 if (!NT_STATUS_IS_OK(result)) {
5594 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5596 acct_name, nt_errstr(result) );
5600 if (!NT_STATUS_IS_OK(result)) {
5601 d_printf(_("Could not set trust account password: %s\n"),
5611 * Delete interdomain trust account for a remote domain.
5613 * @param argc Standard argc.
5614 * @param argv Standard argv without initial components.
5616 * @return Integer status (0 means success).
5619 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5621 if (argc > 0 && !c->display_usage) {
5622 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5623 rpc_trustdom_del_internals, argc, argv);
5627 _("net rpc trustdom del <domain>\n"));
5632 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5633 struct cli_state *cli,
5634 TALLOC_CTX *mem_ctx,
5635 const char *domain_name)
5637 char *dc_name = NULL;
5638 const char *buffer = NULL;
5639 struct rpc_pipe_client *netr;
5642 /* Use NetServerEnum2 */
5644 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5646 return NT_STATUS_OK;
5649 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5650 for domain %s\n", domain_name));
5652 /* Try netr_GetDcName */
5654 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5656 if (!NT_STATUS_IS_OK(status)) {
5660 status = rpccli_netr_GetDcName(netr, mem_ctx,
5667 if (NT_STATUS_IS_OK(status)) {
5671 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5672 for domain %s\n", domain_name));
5678 * Establish trust relationship to a trusting domain.
5679 * Interdomain account must already be created on remote PDC.
5681 * @param c A net_context structure.
5682 * @param argc Standard argc.
5683 * @param argv Standard argv without initial components.
5685 * @return Integer status (0 means success).
5688 static int rpc_trustdom_establish(struct net_context *c, int argc,
5691 struct cli_state *cli = NULL;
5692 struct sockaddr_storage server_ss;
5693 struct rpc_pipe_client *pipe_hnd = NULL;
5694 struct policy_handle connect_hnd;
5695 TALLOC_CTX *mem_ctx;
5697 struct dom_sid *domain_sid;
5702 union lsa_PolicyInformation *info = NULL;
5705 * Connect to \\server\ipc$ as 'our domain' account with password
5708 if (argc != 1 || c->display_usage) {
5711 _("net rpc trustdom establish <domain_name>\n"));
5715 domain_name = smb_xstrdup(argv[0]);
5716 strupper_m(domain_name);
5718 /* account name used at first is our domain's name with '$' */
5719 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5722 strupper_m(acct_name);
5725 * opt_workgroup will be used by connection functions further,
5726 * hence it should be set to remote domain name instead of ours
5728 if (c->opt_workgroup) {
5729 c->opt_workgroup = smb_xstrdup(domain_name);
5732 c->opt_user_name = acct_name;
5734 /* find the domain controller */
5735 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5736 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5740 /* connect to ipc$ as username/password */
5741 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5742 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5744 /* Is it trusting domain account for sure ? */
5745 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5746 nt_errstr(nt_status)));
5750 /* store who we connected to */
5752 saf_store( domain_name, pdc_name );
5755 * Connect to \\server\ipc$ again (this time anonymously)
5758 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5761 if (NT_STATUS_IS_ERR(nt_status)) {
5762 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5763 domain_name, nt_errstr(nt_status)));
5767 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5768 "domain %s", domain_name))) {
5769 DEBUG(0, ("talloc_init() failed\n"));
5774 /* Make sure we're talking to a proper server */
5776 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5777 if (!NT_STATUS_IS_OK(nt_status)) {
5779 talloc_destroy(mem_ctx);
5784 * Call LsaOpenPolicy and LsaQueryInfo
5787 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5789 if (!NT_STATUS_IS_OK(nt_status)) {
5790 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5792 talloc_destroy(mem_ctx);
5796 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5798 if (NT_STATUS_IS_ERR(nt_status)) {
5799 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5800 nt_errstr(nt_status)));
5802 talloc_destroy(mem_ctx);
5806 /* Querying info level 5 */
5808 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5810 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5812 if (NT_STATUS_IS_ERR(nt_status)) {
5813 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5814 nt_errstr(nt_status)));
5816 talloc_destroy(mem_ctx);
5820 domain_sid = info->account_domain.sid;
5822 /* There should be actually query info level 3 (following nt serv behaviour),
5823 but I still don't know if it's _really_ necessary */
5826 * Store the password in secrets db
5829 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5830 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5832 talloc_destroy(mem_ctx);
5837 * Close the pipes and clean up
5840 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5841 if (NT_STATUS_IS_ERR(nt_status)) {
5842 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5843 nt_errstr(nt_status)));
5845 talloc_destroy(mem_ctx);
5851 talloc_destroy(mem_ctx);
5853 d_printf(_("Trust to domain %s established\n"), domain_name);
5858 * Revoke trust relationship to the remote domain.
5860 * @param c A net_context structure.
5861 * @param argc Standard argc.
5862 * @param argv Standard argv without initial components.
5864 * @return Integer status (0 means success).
5867 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5873 if (argc < 1 || c->display_usage) {
5876 _("net rpc trustdom revoke <domain_name>\n"
5877 " Revoke trust relationship\n"
5878 " domain_name\tName of domain to revoke trust\n"));
5882 /* generate upper cased domain name */
5883 domain_name = smb_xstrdup(argv[0]);
5884 strupper_m(domain_name);
5886 /* delete password of the trust */
5887 if (!pdb_del_trusteddom_pw(domain_name)) {
5888 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5895 SAFE_FREE(domain_name);
5899 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5900 const struct dom_sid *domain_sid,
5901 const char *domain_name,
5902 struct cli_state *cli,
5903 struct rpc_pipe_client *pipe_hnd,
5904 TALLOC_CTX *mem_ctx,
5909 if (!sid_to_fstring(str_sid, domain_sid)) {
5910 return NT_STATUS_UNSUCCESSFUL;
5912 d_printf("%s\n", str_sid);
5913 return NT_STATUS_OK;
5916 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
5920 /* convert sid into ascii string */
5921 sid_to_fstring(ascii_sid, dom_sid);
5923 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5926 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5927 TALLOC_CTX *mem_ctx,
5928 struct policy_handle *pol,
5929 struct dom_sid dom_sid,
5930 const char *trusted_dom_name)
5933 union lsa_TrustedDomainInfo *info = NULL;
5934 char *cleartextpwd = NULL;
5935 uint8_t session_key[16];
5936 DATA_BLOB session_key_blob;
5937 DATA_BLOB data = data_blob_null;
5939 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5942 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5944 if (NT_STATUS_IS_ERR(nt_status)) {
5945 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5946 nt_errstr(nt_status)));
5950 data = data_blob(info->password.password->data,
5951 info->password.password->length);
5953 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5954 DEBUG(0, ("Could not retrieve password hash\n"));
5958 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5959 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5961 if (cleartextpwd == NULL) {
5962 DEBUG(0,("retrieved NULL password\n"));
5963 nt_status = NT_STATUS_UNSUCCESSFUL;
5967 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5968 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5969 nt_status = NT_STATUS_UNSUCCESSFUL;
5973 #ifdef DEBUG_PASSWORD
5974 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5975 "password: [%s]\n", trusted_dom_name,
5976 sid_string_dbg(&dom_sid), cleartextpwd));
5980 SAFE_FREE(cleartextpwd);
5981 data_blob_free(&data);
5986 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5989 /* common variables */
5990 TALLOC_CTX* mem_ctx;
5991 struct cli_state *cli = NULL;
5992 struct rpc_pipe_client *pipe_hnd = NULL;
5994 const char *domain_name = NULL;
5995 struct dom_sid *queried_dom_sid;
5996 struct policy_handle connect_hnd;
5997 union lsa_PolicyInformation *info = NULL;
5999 /* trusted domains listing variables */
6000 unsigned int enum_ctx = 0;
6002 struct lsa_DomainList dom_list;
6005 if (c->display_usage) {
6007 "net rpc trustdom vampire\n"
6010 _("Vampire trust relationship from remote server"));
6015 * Listing trusted domains (stored in secrets.tdb, if local)
6018 mem_ctx = talloc_init("trust relationships vampire");
6021 * set domain and pdc name to local samba server (default)
6022 * or to remote one given in command line
6025 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6026 domain_name = c->opt_workgroup;
6027 c->opt_target_workgroup = c->opt_workgroup;
6029 fstrcpy(pdc_name, global_myname());
6030 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6031 c->opt_target_workgroup = domain_name;
6034 /* open \PIPE\lsarpc and open policy handle */
6035 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6036 if (!NT_STATUS_IS_OK(nt_status)) {
6037 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6038 nt_errstr(nt_status)));
6039 talloc_destroy(mem_ctx);
6043 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6045 if (!NT_STATUS_IS_OK(nt_status)) {
6046 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6047 nt_errstr(nt_status) ));
6049 talloc_destroy(mem_ctx);
6053 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6055 if (NT_STATUS_IS_ERR(nt_status)) {
6056 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6057 nt_errstr(nt_status)));
6059 talloc_destroy(mem_ctx);
6063 /* query info level 5 to obtain sid of a domain being queried */
6064 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6066 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6069 if (NT_STATUS_IS_ERR(nt_status)) {
6070 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6071 nt_errstr(nt_status)));
6073 talloc_destroy(mem_ctx);
6077 queried_dom_sid = info->account_domain.sid;
6080 * Keep calling LsaEnumTrustdom over opened pipe until
6081 * the end of enumeration is reached
6084 d_printf(_("Vampire trusted domains:\n\n"));
6087 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6092 if (NT_STATUS_IS_ERR(nt_status)) {
6093 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6094 nt_errstr(nt_status)));
6096 talloc_destroy(mem_ctx);
6100 for (i = 0; i < dom_list.count; i++) {
6102 print_trusted_domain(dom_list.domains[i].sid,
6103 dom_list.domains[i].name.string);
6105 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6106 *dom_list.domains[i].sid,
6107 dom_list.domains[i].name.string);
6108 if (!NT_STATUS_IS_OK(nt_status)) {
6110 talloc_destroy(mem_ctx);
6116 * in case of no trusted domains say something rather
6117 * than just display blank line
6119 if (!dom_list.count) d_printf(_("none\n"));
6121 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6123 /* close this connection before doing next one */
6124 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6125 if (NT_STATUS_IS_ERR(nt_status)) {
6126 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6127 nt_errstr(nt_status)));
6129 talloc_destroy(mem_ctx);
6133 /* close lsarpc pipe and connection to IPC$ */
6136 talloc_destroy(mem_ctx);
6140 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6142 /* common variables */
6143 TALLOC_CTX* mem_ctx;
6144 struct cli_state *cli = NULL, *remote_cli = NULL;
6145 struct rpc_pipe_client *pipe_hnd = NULL;
6147 const char *domain_name = NULL;
6148 struct dom_sid *queried_dom_sid;
6149 int ascii_dom_name_len;
6150 struct policy_handle connect_hnd;
6151 union lsa_PolicyInformation *info = NULL;
6153 /* trusted domains listing variables */
6154 unsigned int num_domains, enum_ctx = 0;
6156 struct lsa_DomainList dom_list;
6160 /* trusting domains listing variables */
6161 struct policy_handle domain_hnd;
6162 struct samr_SamArray *trusts = NULL;
6164 if (c->display_usage) {
6166 "net rpc trustdom list\n"
6169 _("List incoming and outgoing trust relationships"));
6174 * Listing trusted domains (stored in secrets.tdb, if local)
6177 mem_ctx = talloc_init("trust relationships listing");
6180 * set domain and pdc name to local samba server (default)
6181 * or to remote one given in command line
6184 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6185 domain_name = c->opt_workgroup;
6186 c->opt_target_workgroup = c->opt_workgroup;
6188 fstrcpy(pdc_name, global_myname());
6189 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6190 c->opt_target_workgroup = domain_name;
6193 /* open \PIPE\lsarpc and open policy handle */
6194 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6195 if (!NT_STATUS_IS_OK(nt_status)) {
6196 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6197 nt_errstr(nt_status)));
6198 talloc_destroy(mem_ctx);
6202 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6204 if (!NT_STATUS_IS_OK(nt_status)) {
6205 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6206 nt_errstr(nt_status) ));
6208 talloc_destroy(mem_ctx);
6212 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6214 if (NT_STATUS_IS_ERR(nt_status)) {
6215 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6216 nt_errstr(nt_status)));
6218 talloc_destroy(mem_ctx);
6222 /* query info level 5 to obtain sid of a domain being queried */
6223 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6225 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6228 if (NT_STATUS_IS_ERR(nt_status)) {
6229 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6230 nt_errstr(nt_status)));
6232 talloc_destroy(mem_ctx);
6236 queried_dom_sid = info->account_domain.sid;
6239 * Keep calling LsaEnumTrustdom over opened pipe until
6240 * the end of enumeration is reached
6243 d_printf(_("Trusted domains list:\n\n"));
6245 found_domain = false;
6248 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6253 if (NT_STATUS_IS_ERR(nt_status)) {
6254 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6255 nt_errstr(nt_status)));
6257 talloc_destroy(mem_ctx);
6261 for (i = 0; i < dom_list.count; i++) {
6262 print_trusted_domain(dom_list.domains[i].sid,
6263 dom_list.domains[i].name.string);
6264 found_domain = true;
6268 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6271 * in case of no trusted domains say something rather
6272 * than just display blank line
6274 if (!found_domain) {
6275 d_printf(_("none\n"));
6278 /* close this connection before doing next one */
6279 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6280 if (NT_STATUS_IS_ERR(nt_status)) {
6281 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6282 nt_errstr(nt_status)));
6284 talloc_destroy(mem_ctx);
6288 TALLOC_FREE(pipe_hnd);
6291 * Listing trusting domains (stored in passdb backend, if local)
6294 d_printf(_("\nTrusting domains list:\n\n"));
6297 * Open \PIPE\samr and get needed policy handles
6299 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6301 if (!NT_STATUS_IS_OK(nt_status)) {
6302 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6304 talloc_destroy(mem_ctx);
6309 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6311 SAMR_ACCESS_LOOKUP_DOMAIN,
6313 if (!NT_STATUS_IS_OK(nt_status)) {
6314 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6315 nt_errstr(nt_status)));
6317 talloc_destroy(mem_ctx);
6321 /* SamrOpenDomain - we have to open domain policy handle in order to be
6322 able to enumerate accounts*/
6323 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6325 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6328 if (!NT_STATUS_IS_OK(nt_status)) {
6329 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6330 nt_errstr(nt_status)));
6332 talloc_destroy(mem_ctx);
6337 * perform actual enumeration
6340 found_domain = false;
6342 enum_ctx = 0; /* reset enumeration context from last enumeration */
6345 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6352 if (NT_STATUS_IS_ERR(nt_status)) {
6353 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6354 nt_errstr(nt_status)));
6356 talloc_destroy(mem_ctx);
6360 for (i = 0; i < num_domains; i++) {
6362 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6364 found_domain = true;
6367 * get each single domain's sid (do we _really_ need this ?):
6368 * 1) connect to domain's pdc
6369 * 2) query the pdc for domain's sid
6372 /* get rid of '$' tail */
6373 ascii_dom_name_len = strlen(str);
6374 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6375 str[ascii_dom_name_len - 1] = '\0';
6377 /* set opt_* variables to remote domain */
6379 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6380 c->opt_target_workgroup = c->opt_workgroup;
6382 d_printf("%-20s", str);
6384 /* connect to remote domain controller */
6385 nt_status = net_make_ipc_connection(c,
6386 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6388 if (NT_STATUS_IS_OK(nt_status)) {
6389 /* query for domain's sid */
6390 if (run_rpc_command(
6392 &ndr_table_lsarpc.syntax_id, 0,
6393 rpc_query_domain_sid, argc,
6395 d_printf(_("strange - couldn't get domain's sid\n"));
6397 cli_shutdown(remote_cli);
6400 d_fprintf(stderr, _("domain controller is not "
6401 "responding: %s\n"),
6402 nt_errstr(nt_status));
6403 d_printf(_("couldn't get domain's sid\n"));
6407 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6409 if (!found_domain) {
6413 /* close opened samr and domain policy handles */
6414 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6415 if (!NT_STATUS_IS_OK(nt_status)) {
6416 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6419 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6420 if (!NT_STATUS_IS_OK(nt_status)) {
6421 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6424 /* close samr pipe and connection to IPC$ */
6427 talloc_destroy(mem_ctx);
6432 * Entrypoint for 'net rpc trustdom' code.
6434 * @param argc Standard argc.
6435 * @param argv Standard argv without initial components.
6437 * @return Integer status (0 means success).
6440 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6442 struct functable func[] = {
6447 N_("Add trusting domain's account"),
6448 N_("net rpc trustdom add\n"
6449 " Add trusting domain's account")
6455 N_("Remove trusting domain's account"),
6456 N_("net rpc trustdom del\n"
6457 " Remove trusting domain's account")
6461 rpc_trustdom_establish,
6463 N_("Establish outgoing trust relationship"),
6464 N_("net rpc trustdom establish\n"
6465 " Establish outgoing trust relationship")
6469 rpc_trustdom_revoke,
6471 N_("Revoke outgoing trust relationship"),
6472 N_("net rpc trustdom revoke\n"
6473 " Revoke outgoing trust relationship")
6479 N_("List in- and outgoing domain trusts"),
6480 N_("net rpc trustdom list\n"
6481 " List in- and outgoing domain trusts")
6485 rpc_trustdom_vampire,
6487 N_("Vampire trusts from remote server"),
6488 N_("net rpc trustdom vampire\n"
6489 " Vampire trusts from remote server")
6491 {NULL, NULL, 0, NULL, NULL}
6494 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6498 * Check if a server will take rpc commands
6499 * @param flags Type of server to connect to (PDC, DMB, localhost)
6500 * if the host is not explicitly specified
6501 * @return bool (true means rpc supported)
6503 bool net_rpc_check(struct net_context *c, unsigned flags)
6505 struct cli_state *cli;
6507 struct sockaddr_storage server_ss;
6508 char *server_name = NULL;
6511 /* flags (i.e. server type) may depend on command */
6512 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6515 if ((cli = cli_initialise()) == NULL) {
6519 status = cli_connect(cli, server_name, &server_ss);
6520 if (!NT_STATUS_IS_OK(status))
6522 if (!attempt_netbios_session_request(&cli, global_myname(),
6523 server_name, &server_ss))
6525 status = cli_negprot(cli);
6526 if (!NT_STATUS_IS_OK(status))
6528 if (cli->protocol < PROTOCOL_NT1)
6537 /* dump sam database via samsync rpc calls */
6538 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6539 if (c->display_usage) {
6544 _("Dump remote SAM database"));
6548 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6549 NET_FLAGS_ANONYMOUS,
6550 rpc_samdump_internals, argc, argv);
6553 /* syncronise sam database via samsync rpc calls */
6554 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6556 struct functable func[] = {
6561 N_("Dump remote SAM database to ldif"),
6562 N_("net rpc vampire ldif\n"
6563 " Dump remote SAM database to LDIF file or "
6570 N_("Dump remote SAM database to Kerberos Keytab"),
6571 N_("net rpc vampire keytab\n"
6572 " Dump remote SAM database to Kerberos keytab "
6579 N_("Dump remote SAM database to passdb"),
6580 N_("net rpc vampire passdb\n"
6581 " Dump remote SAM database to passdb")
6584 {NULL, NULL, 0, NULL, NULL}
6588 if (c->display_usage) {
6593 _("Vampire remote SAM database"));
6597 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6598 NET_FLAGS_ANONYMOUS,
6599 rpc_vampire_internals,
6603 return net_run_function(c, argc, argv, "net rpc vampire", func);
6607 * Migrate everything from a print server.
6609 * @param c A net_context structure.
6610 * @param argc Standard main() style argc.
6611 * @param argv Standard main() style argv. Initial components are already
6614 * @return A shell status integer (0 for success).
6616 * The order is important !
6617 * To successfully add drivers the print queues have to exist !
6618 * Applying ACLs should be the last step, because you're easily locked out.
6621 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6626 if (c->display_usage) {
6628 "net rpc printer migrate all\n"
6631 _("Migrate everything from a print server"));
6636 d_printf(_("no server to migrate\n"));
6640 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6641 rpc_printer_migrate_printers_internals, argc,
6646 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6647 rpc_printer_migrate_drivers_internals, argc,
6652 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6653 rpc_printer_migrate_forms_internals, argc, argv);
6657 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6658 rpc_printer_migrate_settings_internals, argc,
6663 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6664 rpc_printer_migrate_security_internals, argc,
6670 * Migrate print drivers from a print server.
6672 * @param c A net_context structure.
6673 * @param argc Standard main() style argc.
6674 * @param argv Standard main() style argv. Initial components are already
6677 * @return A shell status integer (0 for success).
6679 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6682 if (c->display_usage) {
6684 "net rpc printer migrate drivers\n"
6687 _("Migrate print-drivers from a print-server"));
6692 d_printf(_("no server to migrate\n"));
6696 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6697 rpc_printer_migrate_drivers_internals,
6702 * Migrate print-forms from a print-server.
6704 * @param c A net_context structure.
6705 * @param argc Standard main() style argc.
6706 * @param argv Standard main() style argv. Initial components are already
6709 * @return A shell status integer (0 for success).
6711 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6714 if (c->display_usage) {
6716 "net rpc printer migrate forms\n"
6719 _("Migrate print-forms from a print-server"));
6724 d_printf(_("no server to migrate\n"));
6728 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6729 rpc_printer_migrate_forms_internals,
6734 * Migrate printers from a print-server.
6736 * @param c A net_context structure.
6737 * @param argc Standard main() style argc.
6738 * @param argv Standard main() style argv. Initial components are already
6741 * @return A shell status integer (0 for success).
6743 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6746 if (c->display_usage) {
6748 "net rpc printer migrate printers\n"
6751 _("Migrate printers from a print-server"));
6756 d_printf(_("no server to migrate\n"));
6760 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6761 rpc_printer_migrate_printers_internals,
6766 * Migrate printer-ACLs from a print-server
6768 * @param c A net_context structure.
6769 * @param argc Standard main() style argc.
6770 * @param argv Standard main() style argv. Initial components are already
6773 * @return A shell status integer (0 for success).
6775 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6778 if (c->display_usage) {
6780 "net rpc printer migrate security\n"
6783 _("Migrate printer-ACLs from a print-server"));
6788 d_printf(_("no server to migrate\n"));
6792 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6793 rpc_printer_migrate_security_internals,
6798 * Migrate printer-settings from a print-server.
6800 * @param c A net_context structure.
6801 * @param argc Standard main() style argc.
6802 * @param argv Standard main() style argv. Initial components are already
6805 * @return A shell status integer (0 for success).
6807 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6810 if (c->display_usage) {
6812 "net rpc printer migrate settings\n"
6815 _("Migrate printer-settings from a "
6821 d_printf(_("no server to migrate\n"));
6825 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6826 rpc_printer_migrate_settings_internals,
6831 * 'net rpc printer' entrypoint.
6833 * @param c A net_context structure.
6834 * @param argc Standard main() style argc.
6835 * @param argv Standard main() style argv. Initial components are already
6839 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6842 /* ouch: when addriver and setdriver are called from within
6843 rpc_printer_migrate_drivers_internals, the printer-queue already
6846 struct functable func[] = {
6849 rpc_printer_migrate_all,
6851 N_("Migrate all from remote to local print server"),
6852 N_("net rpc printer migrate all\n"
6853 " Migrate all from remote to local print server")
6857 rpc_printer_migrate_drivers,
6859 N_("Migrate drivers to local server"),
6860 N_("net rpc printer migrate drivers\n"
6861 " Migrate drivers to local server")
6865 rpc_printer_migrate_forms,
6867 N_("Migrate froms to local server"),
6868 N_("net rpc printer migrate forms\n"
6869 " Migrate froms to local server")
6873 rpc_printer_migrate_printers,
6875 N_("Migrate printers to local server"),
6876 N_("net rpc printer migrate printers\n"
6877 " Migrate printers to local server")
6881 rpc_printer_migrate_security,
6883 N_("Mirgate printer ACLs to local server"),
6884 N_("net rpc printer migrate security\n"
6885 " Mirgate printer ACLs to local server")
6889 rpc_printer_migrate_settings,
6891 N_("Migrate printer settings to local server"),
6892 N_("net rpc printer migrate settings\n"
6893 " Migrate printer settings to local server")
6895 {NULL, NULL, 0, NULL, NULL}
6898 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6903 * List printers on a remote RPC server.
6905 * @param c A net_context structure.
6906 * @param argc Standard main() style argc.
6907 * @param argv Standard main() style argv. Initial components are already
6910 * @return A shell status integer (0 for success).
6912 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6914 if (c->display_usage) {
6916 "net rpc printer list\n"
6919 _("List printers on a remote RPC server"));
6923 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6924 rpc_printer_list_internals,
6929 * List printer-drivers on a remote RPC server.
6931 * @param c A net_context structure.
6932 * @param argc Standard main() style argc.
6933 * @param argv Standard main() style argv. Initial components are already
6936 * @return A shell status integer (0 for success).
6938 static int rpc_printer_driver_list(struct net_context *c, int argc,
6941 if (c->display_usage) {
6943 "net rpc printer driver\n"
6946 _("List printer-drivers on a remote RPC server"));
6950 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6951 rpc_printer_driver_list_internals,
6956 * Publish printer in ADS via MSRPC.
6958 * @param c A net_context structure.
6959 * @param argc Standard main() style argc.
6960 * @param argv Standard main() style argv. Initial components are already
6963 * @return A shell status integer (0 for success).
6965 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6968 if (c->display_usage) {
6970 "net rpc printer publish publish\n"
6973 _("Publish printer in ADS via MSRPC"));
6977 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6978 rpc_printer_publish_publish_internals,
6983 * Update printer in ADS via MSRPC.
6985 * @param c A net_context structure.
6986 * @param argc Standard main() style argc.
6987 * @param argv Standard main() style argv. Initial components are already
6990 * @return A shell status integer (0 for success).
6992 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6994 if (c->display_usage) {
6996 "net rpc printer publish update\n"
6999 _("Update printer in ADS via MSRPC"));
7003 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7004 rpc_printer_publish_update_internals,
7009 * UnPublish printer in ADS via MSRPC.
7011 * @param c A net_context structure.
7012 * @param argc Standard main() style argc.
7013 * @param argv Standard main() style argv. Initial components are already
7016 * @return A shell status integer (0 for success).
7018 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7021 if (c->display_usage) {
7023 "net rpc printer publish unpublish\n"
7026 _("UnPublish printer in ADS via MSRPC"));
7030 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7031 rpc_printer_publish_unpublish_internals,
7036 * List published printers via MSRPC.
7038 * @param c A net_context structure.
7039 * @param argc Standard main() style argc.
7040 * @param argv Standard main() style argv. Initial components are already
7043 * @return A shell status integer (0 for success).
7045 static int rpc_printer_publish_list(struct net_context *c, int argc,
7048 if (c->display_usage) {
7050 "net rpc printer publish list\n"
7053 _("List published printers via MSRPC"));
7057 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7058 rpc_printer_publish_list_internals,
7064 * Publish printer in ADS.
7066 * @param c A net_context structure.
7067 * @param argc Standard main() style argc.
7068 * @param argv Standard main() style argv. Initial components are already
7071 * @return A shell status integer (0 for success).
7073 static int rpc_printer_publish(struct net_context *c, int argc,
7077 struct functable func[] = {
7080 rpc_printer_publish_publish,
7082 N_("Publish printer in AD"),
7083 N_("net rpc printer publish publish\n"
7084 " Publish printer in AD")
7088 rpc_printer_publish_update,
7090 N_("Update printer in AD"),
7091 N_("net rpc printer publish update\n"
7092 " Update printer in AD")
7096 rpc_printer_publish_unpublish,
7098 N_("Unpublish printer"),
7099 N_("net rpc printer publish unpublish\n"
7100 " Unpublish printer")
7104 rpc_printer_publish_list,
7106 N_("List published printers"),
7107 N_("net rpc printer publish list\n"
7108 " List published printers")
7110 {NULL, NULL, 0, NULL, NULL}
7114 if (c->display_usage) {
7115 d_printf(_("Usage:\n"));
7116 d_printf(_("net rpc printer publish\n"
7117 " List published printers\n"
7118 " Alias of net rpc printer publish "
7120 net_display_usage_from_functable(func);
7123 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7124 rpc_printer_publish_list_internals,
7128 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7134 * Display rpc printer help page.
7136 * @param c A net_context structure.
7137 * @param argc Standard main() style argc.
7138 * @param argv Standard main() style argv. Initial components are already
7141 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7143 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7144 "\tlists all printers on print-server\n\n"));
7145 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7146 "\tlists all printer-drivers on print-server\n\n"));
7147 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7148 "\tpublishes printer settings in Active Directory\n"
7149 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7150 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7151 "\n\tmigrates printers from remote to local server\n\n"));
7152 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7153 "\n\tmigrates printer-settings from remote to local server\n\n"));
7154 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7155 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7156 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7157 "\n\tmigrates printer-forms from remote to local server\n\n"));
7158 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7159 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7160 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7161 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7162 "\tremote to local print-server\n\n"));
7163 net_common_methods_usage(c, argc, argv);
7164 net_common_flags_usage(c, argc, argv);
7166 "\t-v or --verbose\t\t\tgive verbose output\n"
7167 "\t --destination\t\tmigration target server (default: localhost)\n"));
7173 * 'net rpc printer' entrypoint.
7175 * @param c A net_context structure.
7176 * @param argc Standard main() style argc.
7177 * @param argv Standard main() style argv. Initial components are already
7180 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7182 struct functable func[] = {
7187 N_("List all printers on print server"),
7188 N_("net rpc printer list\n"
7189 " List all printers on print server")
7193 rpc_printer_migrate,
7195 N_("Migrate printer to local server"),
7196 N_("net rpc printer migrate\n"
7197 " Migrate printer to local server")
7201 rpc_printer_driver_list,
7203 N_("List printer drivers"),
7204 N_("net rpc printer driver\n"
7205 " List printer drivers")
7209 rpc_printer_publish,
7211 N_("Publish printer in AD"),
7212 N_("net rpc printer publish\n"
7213 " Publish printer in AD")
7215 {NULL, NULL, 0, NULL, NULL}
7219 if (c->display_usage) {
7220 d_printf(_("Usage:\n"));
7221 d_printf(_("net rpc printer\n"
7222 " List printers\n"));
7223 net_display_usage_from_functable(func);
7226 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7227 rpc_printer_list_internals,
7231 return net_run_function(c, argc, argv, "net rpc printer", func);
7235 * 'net rpc' entrypoint.
7237 * @param c A net_context structure.
7238 * @param argc Standard main() style argc.
7239 * @param argv Standard main() style argv. Initial components are already
7243 int net_rpc(struct net_context *c, int argc, const char **argv)
7245 NET_API_STATUS status;
7247 struct functable func[] = {
7252 N_("Modify global audit settings"),
7253 N_("net rpc audit\n"
7254 " Modify global audit settings")
7260 N_("Show basic info about a domain"),
7262 " Show basic info about a domain")
7268 N_("Join a domain"),
7276 N_("Join a domain created in server manager"),
7277 N_("net rpc oldjoin\n"
7278 " Join a domain created in server manager")
7284 N_("Test that a join is valid"),
7285 N_("net rpc testjoin\n"
7286 " Test that a join is valid")
7292 N_("List/modify users"),
7294 " List/modify users")
7300 N_("Change a user password"),
7301 N_("net rpc password\n"
7302 " Change a user password\n"
7303 " Alias for net rpc user password")
7309 N_("List/modify groups"),
7310 N_("net rpc group\n"
7311 " List/modify groups")
7317 N_("List/modify shares"),
7318 N_("net rpc share\n"
7319 " List/modify shares")
7325 N_("List open files"),
7333 N_("List/modify printers"),
7334 N_("net rpc printer\n"
7335 " List/modify printers")
7339 net_rpc_changetrustpw,
7341 N_("Change trust account password"),
7342 N_("net rpc changetrustpw\n"
7343 " Change trust account password")
7349 N_("Modify domain trusts"),
7350 N_("net rpc trustdom\n"
7351 " Modify domain trusts")
7357 N_("Abort a remote shutdown"),
7358 N_("net rpc abortshutdown\n"
7359 " Abort a remote shutdown")
7365 N_("Shutdown a remote server"),
7366 N_("net rpc shutdown\n"
7367 " Shutdown a remote server")
7373 N_("Dump SAM data of remote NT PDC"),
7374 N_("net rpc samdump\n"
7375 " Dump SAM data of remote NT PDC")
7381 N_("Sync a remote NT PDC's data into local passdb"),
7382 N_("net rpc vampire\n"
7383 " Sync a remote NT PDC's data into local passdb")
7389 N_("Fetch the domain sid into local secrets.tdb"),
7390 N_("net rpc getsid\n"
7391 " Fetch the domain sid into local secrets.tdb")
7397 N_("Manage privileges assigned to SID"),
7398 N_("net rpc rights\n"
7399 " Manage privileges assigned to SID")
7405 N_("Start/stop/query remote services"),
7406 N_("net rpc service\n"
7407 " Start/stop/query remote services")
7413 N_("Manage registry hives"),
7414 N_("net rpc registry\n"
7415 " Manage registry hives")
7421 N_("Open interactive shell on remote server"),
7422 N_("net rpc shell\n"
7423 " Open interactive shell on remote server")
7425 {NULL, NULL, 0, NULL, NULL}
7428 status = libnetapi_init(&c->netapi_ctx);
7432 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7433 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7434 if (c->opt_kerberos) {
7435 libnetapi_set_use_kerberos(c->netapi_ctx);
7437 if (c->opt_ccache) {
7438 libnetapi_set_use_ccache(c->netapi_ctx);
7441 return net_run_function(c, argc, argv, "net rpc", func);