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 "../librpc/gen_ndr/cli_lsa.h"
29 #include "../librpc/gen_ndr/cli_netlogon.h"
30 #include "../librpc/gen_ndr/cli_srvsvc.h"
31 #include "../librpc/gen_ndr/cli_spoolss.h"
32 #include "../librpc/gen_ndr/cli_initshutdown.h"
33 #include "../librpc/gen_ndr/cli_winreg.h"
35 static int net_mode_share;
36 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
41 * @brief RPC based subcommands for the 'net' utility.
43 * This file should contain much of the functionality that used to
44 * be found in rpcclient, execpt that the commands should change
45 * less often, and the fucntionality should be sane (the user is not
46 * expected to know a rid/sid before they conduct an operation etc.)
48 * @todo Perhaps eventually these should be split out into a number
49 * of files, as this could get quite big.
54 * Many of the RPC functions need the domain sid. This function gets
55 * it at the start of every run
57 * @param cli A cli_state already connected to the remote machine
59 * @return The Domain SID of the remote machine.
62 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
64 const char **domain_name)
66 struct rpc_pipe_client *lsa_pipe = NULL;
67 struct policy_handle pol;
68 NTSTATUS result = NT_STATUS_OK;
69 union lsa_PolicyInformation *info = NULL;
71 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
73 if (!NT_STATUS_IS_OK(result)) {
74 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
78 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
79 SEC_FLAG_MAXIMUM_ALLOWED,
81 if (!NT_STATUS_IS_OK(result)) {
82 d_fprintf(stderr, "open_policy %s: %s\n",
88 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
90 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
92 if (!NT_STATUS_IS_OK(result)) {
93 d_fprintf(stderr, "lsaquery %s: %s\n",
99 *domain_name = info->account_domain.name.string;
100 *domain_sid = info->account_domain.sid;
102 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
103 TALLOC_FREE(lsa_pipe);
109 * Run a single RPC command, from start to finish.
111 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
112 * @param conn_flag a NET_FLAG_ combination. Passed to
113 * net_make_ipc_connection.
114 * @param argc Standard main() style argc.
115 * @param argv Standard main() style argv. Initial components are already
117 * @return A shell status integer (0 for success).
120 int run_rpc_command(struct net_context *c,
121 struct cli_state *cli_arg,
122 const struct ndr_syntax_id *interface,
128 struct cli_state *cli = NULL;
129 struct rpc_pipe_client *pipe_hnd = NULL;
133 const char *domain_name;
136 /* make use of cli_state handed over as an argument, if possible */
138 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
139 if (!NT_STATUS_IS_OK(nt_status)) {
140 DEBUG(1, ("failed to make ipc connection: %s\n",
141 nt_errstr(nt_status)));
154 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
155 DEBUG(0, ("talloc_init() failed\n"));
159 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
161 if (!NT_STATUS_IS_OK(nt_status)) {
165 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
166 if (lp_client_schannel()
167 && (ndr_syntax_id_equal(interface,
168 &ndr_table_netlogon.syntax_id))) {
169 /* Always try and create an schannel netlogon pipe. */
170 nt_status = cli_rpc_pipe_open_schannel(
171 cli, interface, NCACN_NP,
172 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
174 if (!NT_STATUS_IS_OK(nt_status)) {
175 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
176 nt_errstr(nt_status) ));
180 if (conn_flags & NET_FLAGS_SEAL) {
181 nt_status = cli_rpc_pipe_open_ntlmssp(
183 (conn_flags & NET_FLAGS_TCP) ?
184 NCACN_IP_TCP : NCACN_NP,
185 DCERPC_AUTH_LEVEL_PRIVACY,
186 lp_workgroup(), c->opt_user_name,
187 c->opt_password, &pipe_hnd);
189 nt_status = cli_rpc_pipe_open_noauth(
193 if (!NT_STATUS_IS_OK(nt_status)) {
194 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
195 get_pipe_name_from_syntax(
196 talloc_tos(), interface),
197 nt_errstr(nt_status) ));
203 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
205 if (!NT_STATUS_IS_OK(nt_status)) {
206 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
209 DEBUG(5, ("rpc command function succedded\n"));
212 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
214 TALLOC_FREE(pipe_hnd);
219 /* close the connection only if it was opened here */
224 talloc_destroy(mem_ctx);
229 * Force a change of the trust acccount password.
231 * All parameters are provided by the run_rpc_command function, except for
232 * argc, argv which are passed through.
234 * @param domain_sid The domain sid acquired from the remote server.
235 * @param cli A cli_state connected to the server.
236 * @param mem_ctx Talloc context, destroyed on completion of the function.
237 * @param argc Standard main() style argc.
238 * @param argv Standard main() style argv. Initial components are already
241 * @return Normal NTSTATUS return.
244 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
245 const DOM_SID *domain_sid,
246 const char *domain_name,
247 struct cli_state *cli,
248 struct rpc_pipe_client *pipe_hnd,
255 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
256 if (!NT_STATUS_IS_OK(status)) {
257 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
266 * Force a change of the trust acccount password.
268 * @param argc Standard main() style argc.
269 * @param argv Standard main() style argv. Initial components are already
272 * @return A shell status integer (0 for success).
275 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
277 if (c->display_usage) {
279 "net rpc changetrustpw\n"
282 _("Change the machine trust password"));
286 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
287 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
288 rpc_changetrustpw_internals,
293 * Join a domain, the old way.
295 * This uses 'machinename' as the inital password, and changes it.
297 * The password should be created with 'server manager' or equiv first.
299 * All parameters are provided by the run_rpc_command function, except for
300 * argc, argv which are passed through.
302 * @param domain_sid The domain sid acquired from the remote server.
303 * @param cli A cli_state connected to the server.
304 * @param mem_ctx Talloc context, destroyed on completion of the function.
305 * @param argc Standard main() style argc.
306 * @param argv Standard main() style argv. Initial components are already
309 * @return Normal NTSTATUS return.
312 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
313 const DOM_SID *domain_sid,
314 const char *domain_name,
315 struct cli_state *cli,
316 struct rpc_pipe_client *pipe_hnd,
322 fstring trust_passwd;
323 unsigned char orig_trust_passwd_hash[16];
325 enum netr_SchannelType sec_channel_type;
327 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
329 if (!NT_STATUS_IS_OK(result)) {
330 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
333 nt_errstr(result) ));
338 check what type of join - if the user want's to join as
339 a BDC, the server must agree that we are a BDC.
342 sec_channel_type = get_sec_channel_type(argv[0]);
344 sec_channel_type = get_sec_channel_type(NULL);
347 fstrcpy(trust_passwd, global_myname());
348 strlower_m(trust_passwd);
351 * Machine names can be 15 characters, but the max length on
352 * a password is 14. --jerry
355 trust_passwd[14] = '\0';
357 E_md4hash(trust_passwd, orig_trust_passwd_hash);
359 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
361 orig_trust_passwd_hash,
364 if (NT_STATUS_IS_OK(result))
365 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
368 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
369 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
370 result = NT_STATUS_UNSUCCESSFUL;
377 * Join a domain, the old way.
379 * @param argc Standard main() style argc.
380 * @param argv Standard main() style argv. Initial components are already
383 * @return A shell status integer (0 for success).
386 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
388 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
389 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
390 rpc_oldjoin_internals,
395 * Join a domain, the old way. This function exists to allow
396 * the message to be displayed when oldjoin was explicitly
397 * requested, but not when it was implied by "net rpc join".
399 * @param argc Standard main() style argc.
400 * @param argv Standard main() style argv. Initial components are already
403 * @return A shell status integer (0 for success).
406 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
410 if (c->display_usage) {
415 _("Join a domain the old way"));
419 rc = net_rpc_perform_oldjoin(c, argc, argv);
422 d_fprintf(stderr, _("Failed to join domain\n"));
429 * 'net rpc join' entrypoint.
430 * @param argc Standard main() style argc.
431 * @param argv Standard main() style argv. Initial components are already
434 * Main 'net_rpc_join()' (where the admin username/password is used) is
436 * Try to just change the password, but if that doesn't work, use/prompt
437 * for a username/password.
440 int net_rpc_join(struct net_context *c, int argc, const char **argv)
442 if (c->display_usage) {
445 _("net rpc join -U <username>[%%password] <type>\n"
447 " username\tName of the admin user"
448 " password\tPassword of the admin user, will "
449 "prompt if not specified\n"
450 " type\tCan be one of the following:\n"
451 "\t\tMEMBER\tJoin as member server (default)\n"
452 "\t\tBDC\tJoin as BDC\n"
453 "\t\tPDC\tJoin as PDC\n"));
457 if (lp_server_role() == ROLE_STANDALONE) {
458 d_printf(_("cannot join as standalone machine\n"));
462 if (strlen(global_myname()) > 15) {
463 d_printf(_("Our netbios name can be at most 15 chars long, "
464 "\"%s\" is %u chars long\n"),
465 global_myname(), (unsigned int)strlen(global_myname()));
469 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
472 return net_rpc_join_newstyle(c, argc, argv);
476 * display info about a rpc domain
478 * All parameters are provided by the run_rpc_command function, except for
479 * argc, argv which are passed through.
481 * @param domain_sid The domain sid acquired from the remote server
482 * @param cli A cli_state connected to the server.
483 * @param mem_ctx Talloc context, destroyed on completion of the function.
484 * @param argc Standard main() style argc.
485 * @param argv Standard main() style argv. Initial components are already
488 * @return Normal NTSTATUS return.
491 NTSTATUS rpc_info_internals(struct net_context *c,
492 const DOM_SID *domain_sid,
493 const char *domain_name,
494 struct cli_state *cli,
495 struct rpc_pipe_client *pipe_hnd,
500 struct policy_handle connect_pol, domain_pol;
501 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
502 union samr_DomainInfo *info = NULL;
505 sid_to_fstring(sid_str, domain_sid);
507 /* Get sam policy handle */
508 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
510 MAXIMUM_ALLOWED_ACCESS,
512 if (!NT_STATUS_IS_OK(result)) {
513 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
518 /* Get domain policy handle */
519 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
521 MAXIMUM_ALLOWED_ACCESS,
522 CONST_DISCARD(struct dom_sid2 *, domain_sid),
524 if (!NT_STATUS_IS_OK(result)) {
525 d_fprintf(stderr, _("Could not open domain: %s\n"),
530 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
534 if (NT_STATUS_IS_OK(result)) {
535 d_printf(_("Domain Name: %s\n"),
536 info->general.domain_name.string);
537 d_printf(_("Domain SID: %s\n"), sid_str);
538 d_printf(_("Sequence number: %llu\n"),
539 (unsigned long long)info->general.sequence_num);
540 d_printf(_("Num users: %u\n"), info->general.num_users);
541 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
542 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
550 * 'net rpc info' entrypoint.
551 * @param argc Standard main() style argc.
552 * @param argv Standard main() style argv. Initial components are already
556 int net_rpc_info(struct net_context *c, int argc, const char **argv)
558 if (c->display_usage) {
563 _("Display information about the domain"));
567 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
568 NET_FLAGS_PDC, rpc_info_internals,
573 * Fetch domain SID into the local secrets.tdb.
575 * All parameters are provided by the run_rpc_command function, except for
576 * argc, argv which are passed through.
578 * @param domain_sid The domain sid acquired from the remote server.
579 * @param cli A cli_state connected to the server.
580 * @param mem_ctx Talloc context, destroyed on completion of the function.
581 * @param argc Standard main() style argc.
582 * @param argv Standard main() style argv. Initial components are already
585 * @return Normal NTSTATUS return.
588 static NTSTATUS rpc_getsid_internals(struct net_context *c,
589 const DOM_SID *domain_sid,
590 const char *domain_name,
591 struct cli_state *cli,
592 struct rpc_pipe_client *pipe_hnd,
599 sid_to_fstring(sid_str, domain_sid);
600 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
601 sid_str, domain_name);
603 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
604 DEBUG(0,("Can't store domain SID\n"));
605 return NT_STATUS_UNSUCCESSFUL;
612 * 'net rpc getsid' entrypoint.
613 * @param argc Standard main() style argc.
614 * @param argv Standard main() style argv. Initial components are already
618 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
620 int conn_flags = NET_FLAGS_PDC;
622 if (!c->opt_user_specified) {
623 conn_flags |= NET_FLAGS_ANONYMOUS;
626 if (c->display_usage) {
631 _("Fetch domain SID into local secrets.tdb"));
635 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
637 rpc_getsid_internals,
641 /****************************************************************************/
644 * Basic usage function for 'net rpc user'.
645 * @param argc Standard main() style argc.
646 * @param argv Standard main() style argv. Initial components are already
650 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
652 return net_user_usage(c, argc, argv);
656 * Add a new user to a remote RPC server.
658 * @param argc Standard main() style argc.
659 * @param argv Standard main() style argv. Initial components are already
662 * @return A shell status integer (0 for success).
665 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
667 NET_API_STATUS status;
668 struct USER_INFO_1 info1;
669 uint32_t parm_error = 0;
671 if (argc < 1 || c->display_usage) {
672 rpc_user_usage(c, argc, argv);
678 info1.usri1_name = argv[0];
680 info1.usri1_password = argv[1];
683 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
686 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
687 argv[0], libnetapi_get_error_string(c->netapi_ctx,
691 d_printf(_("Added user '%s'.\n"), argv[0]);
698 * Rename a user on a remote RPC server.
700 * @param argc Standard main() style argc.
701 * @param argv Standard main() style argv. Initial components are already
704 * @return A shell status integer (0 for success).
707 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
709 NET_API_STATUS status;
710 struct USER_INFO_0 u0;
711 uint32_t parm_err = 0;
713 if (argc != 2 || c->display_usage) {
714 rpc_user_usage(c, argc, argv);
718 u0.usri0_name = argv[1];
720 status = NetUserSetInfo(c->opt_host, argv[0],
721 0, (uint8_t *)&u0, &parm_err);
724 _("Failed to rename user from %s to %s - %s\n"),
726 libnetapi_get_error_string(c->netapi_ctx, status));
728 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
735 * Set a user's primary group
737 * @param argc Standard main() style argc.
738 * @param argv Standard main() style argv. Initial components are already
741 * @return A shell status integer (0 for success).
744 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
747 NET_API_STATUS status;
749 struct GROUP_INFO_2 *g2;
750 struct USER_INFO_1051 u1051;
751 uint32_t parm_err = 0;
753 if (argc != 2 || c->display_usage) {
754 rpc_user_usage(c, argc, argv);
758 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
760 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
762 libnetapi_get_error_string(c->netapi_ctx, status));
765 g2 = (struct GROUP_INFO_2 *)buffer;
767 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
769 NetApiBufferFree(buffer);
771 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
772 (uint8_t *)&u1051, &parm_err);
775 _("Failed to set user's primary group %s to %s - "
776 "%s\n"), argv[0], argv[1],
777 libnetapi_get_error_string(c->netapi_ctx, status));
779 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
786 * Delete a user from a remote RPC server.
788 * @param argc Standard main() style argc.
789 * @param argv Standard main() style argv. Initial components are already
792 * @return A shell status integer (0 for success).
795 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
797 NET_API_STATUS status;
799 if (argc < 1 || c->display_usage) {
800 rpc_user_usage(c, argc, argv);
804 status = NetUserDel(c->opt_host, argv[0]);
807 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
809 libnetapi_get_error_string(c->netapi_ctx, status));
812 d_printf(_("Deleted user '%s'.\n"), argv[0]);
819 * Set a user's password on a remote RPC server.
821 * @param argc Standard main() style argc.
822 * @param argv Standard main() style argv. Initial components are already
825 * @return A shell status integer (0 for success).
828 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
830 NET_API_STATUS status;
832 struct USER_INFO_1003 u1003;
833 uint32_t parm_err = 0;
836 if (argc < 1 || c->display_usage) {
837 rpc_user_usage(c, argc, argv);
842 u1003.usri1003_password = argv[1];
844 ret = asprintf(&prompt, _("Enter new password for %s:"),
849 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
851 if (u1003.usri1003_password == NULL) {
856 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
858 /* Display results */
861 _("Failed to set password for '%s' with error: %s.\n"),
862 argv[0], libnetapi_get_error_string(c->netapi_ctx,
871 * List a user's groups from a remote RPC server.
873 * @param argc Standard main() style argc.
874 * @param argv Standard main() style argv. Initial components are already
877 * @return A shell status integer (0 for success)
880 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
883 NET_API_STATUS status;
884 struct GROUP_USERS_INFO_0 *u0 = NULL;
885 uint32_t entries_read = 0;
886 uint32_t total_entries = 0;
890 if (argc < 1 || c->display_usage) {
891 rpc_user_usage(c, argc, argv);
895 status = NetUserGetGroups(c->opt_host,
898 (uint8_t **)(void *)&u0,
904 _("Failed to get groups for '%s' with error: %s.\n"),
905 argv[0], libnetapi_get_error_string(c->netapi_ctx,
910 for (i=0; i < entries_read; i++) {
911 printf("%s\n", u0->grui0_name);
919 * List users on a remote RPC server.
921 * All parameters are provided by the run_rpc_command function, except for
922 * argc, argv which are passed through.
924 * @param domain_sid The domain sid acquired from the remote server.
925 * @param cli A cli_state connected to the server.
926 * @param mem_ctx Talloc context, destroyed on completion of the function.
927 * @param argc Standard main() style argc.
928 * @param argv Standard main() style argv. Initial components are already
931 * @return Normal NTSTATUS return.
934 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
936 NET_API_STATUS status;
937 uint32_t start_idx=0, num_entries, i, loop_count = 0;
938 struct NET_DISPLAY_USER *info = NULL;
941 /* Query domain users */
942 if (c->opt_long_list_entries)
943 d_printf(_("\nUser name Comment"
944 "\n-----------------------------\n"));
946 uint32_t max_entries, max_size;
948 get_query_dispinfo_params(
949 loop_count, &max_entries, &max_size);
951 status = NetQueryDisplayInformation(c->opt_host,
958 if (status != 0 && status != ERROR_MORE_DATA) {
962 info = (struct NET_DISPLAY_USER *)buffer;
964 for (i = 0; i < num_entries; i++) {
966 if (c->opt_long_list_entries)
967 printf("%-21.21s %s\n", info->usri1_name,
968 info->usri1_comment);
970 printf("%s\n", info->usri1_name);
974 NetApiBufferFree(buffer);
977 start_idx += num_entries;
979 } while (status == ERROR_MORE_DATA);
985 * 'net rpc user' entrypoint.
986 * @param argc Standard main() style argc.
987 * @param argv Standard main() style argv. Initial components are already
991 int net_rpc_user(struct net_context *c, int argc, const char **argv)
993 NET_API_STATUS status;
995 struct functable func[] = {
1000 N_("Add specified user"),
1001 N_("net rpc user add\n"
1002 " Add specified user")
1008 N_("List domain groups of user"),
1009 N_("net rpc user info\n"
1010 " Lis domain groups of user")
1016 N_("Remove specified user"),
1017 N_("net rpc user delete\n"
1018 " Remove specified user")
1024 N_("Change user password"),
1025 N_("net rpc user password\n"
1026 " Change user password")
1032 N_("Rename specified user"),
1033 N_("net rpc user rename\n"
1034 " Rename specified user")
1038 rpc_user_setprimarygroup,
1040 "Set a user's primary group",
1041 "net rpc user setprimarygroup\n"
1042 " Set a user's primary group"
1044 {NULL, NULL, 0, NULL, NULL}
1047 status = libnetapi_init(&c->netapi_ctx);
1051 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1052 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1053 if (c->opt_kerberos) {
1054 libnetapi_set_use_kerberos(c->netapi_ctx);
1058 if (c->display_usage) {
1063 _("List all users"));
1064 net_display_usage_from_functable(func);
1068 return rpc_user_list(c, argc, argv);
1071 return net_run_function(c, argc, argv, "net rpc user", func);
1074 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1075 TALLOC_CTX *mem_ctx,
1076 struct rpc_sh_ctx *ctx,
1077 struct rpc_pipe_client *pipe_hnd,
1078 int argc, const char **argv)
1080 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1083 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1084 TALLOC_CTX *mem_ctx,
1085 struct rpc_sh_ctx *ctx,
1086 struct rpc_pipe_client *pipe_hnd,
1087 int argc, const char **argv)
1089 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1092 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1093 TALLOC_CTX *mem_ctx,
1094 struct rpc_sh_ctx *ctx,
1095 struct rpc_pipe_client *pipe_hnd,
1096 int argc, const char **argv,
1098 struct net_context *c,
1099 TALLOC_CTX *mem_ctx,
1100 struct rpc_sh_ctx *ctx,
1101 struct rpc_pipe_client *pipe_hnd,
1102 struct policy_handle *user_hnd,
1103 int argc, const char **argv))
1105 struct policy_handle connect_pol, domain_pol, user_pol;
1106 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1109 enum lsa_SidType type;
1112 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1114 return NT_STATUS_INVALID_PARAMETER;
1117 ZERO_STRUCT(connect_pol);
1118 ZERO_STRUCT(domain_pol);
1119 ZERO_STRUCT(user_pol);
1121 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1122 argv[0], NULL, NULL, &sid, &type);
1123 if (!NT_STATUS_IS_OK(result)) {
1124 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1129 if (type != SID_NAME_USER) {
1130 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1131 sid_type_lookup(type));
1132 result = NT_STATUS_NO_SUCH_USER;
1136 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1137 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1138 result = NT_STATUS_NO_SUCH_USER;
1142 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1144 MAXIMUM_ALLOWED_ACCESS,
1146 if (!NT_STATUS_IS_OK(result)) {
1150 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1152 MAXIMUM_ALLOWED_ACCESS,
1155 if (!NT_STATUS_IS_OK(result)) {
1159 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1161 MAXIMUM_ALLOWED_ACCESS,
1164 if (!NT_STATUS_IS_OK(result)) {
1168 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1171 if (is_valid_policy_hnd(&user_pol)) {
1172 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1174 if (is_valid_policy_hnd(&domain_pol)) {
1175 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1177 if (is_valid_policy_hnd(&connect_pol)) {
1178 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1183 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1184 TALLOC_CTX *mem_ctx,
1185 struct rpc_sh_ctx *ctx,
1186 struct rpc_pipe_client *pipe_hnd,
1187 struct policy_handle *user_hnd,
1188 int argc, const char **argv)
1191 union samr_UserInfo *info = NULL;
1194 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1196 return NT_STATUS_INVALID_PARAMETER;
1199 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1203 if (!NT_STATUS_IS_OK(result)) {
1207 d_printf(_("user rid: %d, group rid: %d\n"),
1209 info->info21.primary_gid);
1214 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1215 TALLOC_CTX *mem_ctx,
1216 struct rpc_sh_ctx *ctx,
1217 struct rpc_pipe_client *pipe_hnd,
1218 int argc, const char **argv)
1220 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1221 rpc_sh_user_show_internals);
1224 #define FETCHSTR(name, rec) \
1225 do { if (strequal(ctx->thiscmd, name)) { \
1226 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1229 #define SETSTR(name, rec, flag) \
1230 do { if (strequal(ctx->thiscmd, name)) { \
1231 init_lsa_String(&(info->info21.rec), argv[0]); \
1232 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1235 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1236 TALLOC_CTX *mem_ctx,
1237 struct rpc_sh_ctx *ctx,
1238 struct rpc_pipe_client *pipe_hnd,
1239 struct policy_handle *user_hnd,
1240 int argc, const char **argv)
1243 const char *username;
1244 const char *oldval = "";
1245 union samr_UserInfo *info = NULL;
1248 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1249 _("Usage:"), ctx->whoami);
1250 return NT_STATUS_INVALID_PARAMETER;
1253 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1257 if (!NT_STATUS_IS_OK(result)) {
1261 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1263 FETCHSTR("fullname", full_name);
1264 FETCHSTR("homedir", home_directory);
1265 FETCHSTR("homedrive", home_drive);
1266 FETCHSTR("logonscript", logon_script);
1267 FETCHSTR("profilepath", profile_path);
1268 FETCHSTR("description", description);
1271 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1275 if (strcmp(argv[0], "NULL") == 0) {
1279 ZERO_STRUCT(info->info21);
1281 SETSTR("fullname", full_name, FULL_NAME);
1282 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1283 SETSTR("homedrive", home_drive, HOME_DRIVE);
1284 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1285 SETSTR("profilepath", profile_path, PROFILE_PATH);
1286 SETSTR("description", description, DESCRIPTION);
1288 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1293 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1294 ctx->thiscmd, oldval, argv[0]);
1301 #define HANDLEFLG(name, rec) \
1302 do { if (strequal(ctx->thiscmd, name)) { \
1303 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1305 newflags = oldflags | ACB_##rec; \
1307 newflags = oldflags & ~ACB_##rec; \
1310 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1311 TALLOC_CTX *mem_ctx,
1312 struct rpc_sh_ctx *ctx,
1313 struct rpc_pipe_client *pipe_hnd,
1314 int argc, const char **argv)
1316 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1317 rpc_sh_user_str_edit_internals);
1320 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1321 TALLOC_CTX *mem_ctx,
1322 struct rpc_sh_ctx *ctx,
1323 struct rpc_pipe_client *pipe_hnd,
1324 struct policy_handle *user_hnd,
1325 int argc, const char **argv)
1328 const char *username;
1329 const char *oldval = "unknown";
1330 uint32 oldflags, newflags;
1332 union samr_UserInfo *info = NULL;
1335 ((argc == 1) && !strequal(argv[0], "yes") &&
1336 !strequal(argv[0], "no"))) {
1337 /* TRANSATORS: The yes|no here are program keywords. Please do
1339 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1341 return NT_STATUS_INVALID_PARAMETER;
1344 newval = strequal(argv[0], "yes");
1346 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1350 if (!NT_STATUS_IS_OK(result)) {
1354 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1355 oldflags = info->info21.acct_flags;
1356 newflags = info->info21.acct_flags;
1358 HANDLEFLG("disabled", DISABLED);
1359 HANDLEFLG("pwnotreq", PWNOTREQ);
1360 HANDLEFLG("autolock", AUTOLOCK);
1361 HANDLEFLG("pwnoexp", PWNOEXP);
1364 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1369 ZERO_STRUCT(info->info21);
1371 info->info21.acct_flags = newflags;
1372 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1374 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1379 if (NT_STATUS_IS_OK(result)) {
1380 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1381 ctx->thiscmd, oldval, argv[0]);
1389 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1390 TALLOC_CTX *mem_ctx,
1391 struct rpc_sh_ctx *ctx,
1392 struct rpc_pipe_client *pipe_hnd,
1393 int argc, const char **argv)
1395 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1396 rpc_sh_user_flag_edit_internals);
1399 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1400 TALLOC_CTX *mem_ctx,
1401 struct rpc_sh_ctx *ctx)
1403 static struct rpc_sh_cmd cmds[] = {
1405 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1406 N_("Show/Set a user's full name") },
1408 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1409 N_("Show/Set a user's home directory") },
1411 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1412 N_("Show/Set a user's home drive") },
1414 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1415 N_("Show/Set a user's logon script") },
1417 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1418 N_("Show/Set a user's profile path") },
1420 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1421 N_("Show/Set a user's description") },
1423 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1424 N_("Show/Set whether a user is disabled") },
1426 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1427 N_("Show/Set whether a user locked out") },
1429 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1430 N_("Show/Set whether a user does not need a password") },
1432 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1433 N_("Show/Set whether a user's password does not expire") },
1435 { NULL, NULL, 0, NULL, NULL }
1441 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1442 TALLOC_CTX *mem_ctx,
1443 struct rpc_sh_ctx *ctx)
1445 static struct rpc_sh_cmd cmds[] = {
1447 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1448 N_("List available users") },
1450 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1451 N_("List the domain groups a user is member of") },
1453 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1454 N_("Show info about a user") },
1456 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1457 N_("Show/Modify a user's fields") },
1459 { NULL, NULL, 0, NULL, NULL }
1465 /****************************************************************************/
1468 * Basic usage function for 'net rpc group'.
1469 * @param argc Standard main() style argc.
1470 * @param argv Standard main() style argv. Initial components are already
1474 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1476 return net_group_usage(c, argc, argv);
1480 * Delete group on a remote RPC server.
1482 * All parameters are provided by the run_rpc_command function, except for
1483 * argc, argv which are passed through.
1485 * @param domain_sid The domain sid acquired from the remote server.
1486 * @param cli A cli_state connected to the server.
1487 * @param mem_ctx Talloc context, destroyed on completion of the function.
1488 * @param argc Standard main() style argc.
1489 * @param argv Standard main() style argv. Initial components are already
1492 * @return Normal NTSTATUS return.
1495 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1496 const DOM_SID *domain_sid,
1497 const char *domain_name,
1498 struct cli_state *cli,
1499 struct rpc_pipe_client *pipe_hnd,
1500 TALLOC_CTX *mem_ctx,
1504 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1505 bool group_is_primary = false;
1506 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1508 struct samr_RidTypeArray *rids = NULL;
1511 /* struct samr_RidWithAttribute *user_gids; */
1513 struct samr_Ids group_rids, name_types;
1514 struct lsa_String lsa_acct_name;
1515 union samr_UserInfo *info = NULL;
1517 if (argc < 1 || c->display_usage) {
1518 rpc_group_usage(c, argc,argv);
1519 return NT_STATUS_OK; /* ok? */
1522 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1524 MAXIMUM_ALLOWED_ACCESS,
1527 if (!NT_STATUS_IS_OK(result)) {
1528 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1532 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1534 MAXIMUM_ALLOWED_ACCESS,
1535 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1538 if (!NT_STATUS_IS_OK(result)) {
1539 d_fprintf(stderr, _("Request open_domain failed\n"));
1543 init_lsa_String(&lsa_acct_name, argv[0]);
1545 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1551 if (!NT_STATUS_IS_OK(result)) {
1552 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1556 switch (name_types.ids[0])
1558 case SID_NAME_DOM_GRP:
1559 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1561 MAXIMUM_ALLOWED_ACCESS,
1564 if (!NT_STATUS_IS_OK(result)) {
1565 d_fprintf(stderr, _("Request open_group failed"));
1569 group_rid = group_rids.ids[0];
1571 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1575 if (!NT_STATUS_IS_OK(result)) {
1577 _("Unable to query group members of %s"),
1582 if (c->opt_verbose) {
1584 _("Domain Group %s (rid: %d) has %d members\n"),
1585 argv[0],group_rid, rids->count);
1588 /* Check if group is anyone's primary group */
1589 for (i = 0; i < rids->count; i++)
1591 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1593 MAXIMUM_ALLOWED_ACCESS,
1597 if (!NT_STATUS_IS_OK(result)) {
1599 _("Unable to open group member %d\n"),
1604 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1609 if (!NT_STATUS_IS_OK(result)) {
1611 _("Unable to lookup userinfo for group "
1617 if (info->info21.primary_gid == group_rid) {
1618 if (c->opt_verbose) {
1619 d_printf(_("Group is primary group "
1621 info->info21.account_name.string);
1623 group_is_primary = true;
1626 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1629 if (group_is_primary) {
1630 d_fprintf(stderr, _("Unable to delete group because "
1631 "some of it's members have it as primary "
1633 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1637 /* remove all group members */
1638 for (i = 0; i < rids->count; i++)
1641 d_printf(_("Remove group member %d..."),
1643 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1647 if (NT_STATUS_IS_OK(result)) {
1649 d_printf(_("ok\n"));
1652 d_printf("%s\n", _("failed"));
1657 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1661 /* removing a local group is easier... */
1662 case SID_NAME_ALIAS:
1663 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1665 MAXIMUM_ALLOWED_ACCESS,
1669 if (!NT_STATUS_IS_OK(result)) {
1670 d_fprintf(stderr, _("Request open_alias failed\n"));
1674 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1678 d_fprintf(stderr, _("%s is of type %s. This command is only "
1679 "for deleting local or global groups\n"),
1680 argv[0],sid_type_lookup(name_types.ids[0]));
1681 result = NT_STATUS_UNSUCCESSFUL;
1685 if (NT_STATUS_IS_OK(result)) {
1687 d_printf(_("Deleted %s '%s'\n"),
1688 sid_type_lookup(name_types.ids[0]), argv[0]);
1690 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1691 get_friendly_nt_error_msg(result));
1699 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1701 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1702 rpc_group_delete_internals, argc,argv);
1705 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1707 NET_API_STATUS status;
1708 struct GROUP_INFO_1 info1;
1709 uint32_t parm_error = 0;
1711 if (argc != 1 || c->display_usage) {
1712 rpc_group_usage(c, argc, argv);
1718 info1.grpi1_name = argv[0];
1719 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1720 info1.grpi1_comment = c->opt_comment;
1723 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1727 _("Failed to add group '%s' with error: %s.\n"),
1728 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1732 d_printf(_("Added group '%s'.\n"), argv[0]);
1738 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1740 NET_API_STATUS status;
1741 struct LOCALGROUP_INFO_1 info1;
1742 uint32_t parm_error = 0;
1744 if (argc != 1 || c->display_usage) {
1745 rpc_group_usage(c, argc, argv);
1751 info1.lgrpi1_name = argv[0];
1752 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1753 info1.lgrpi1_comment = c->opt_comment;
1756 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1760 _("Failed to add alias '%s' with error: %s.\n"),
1761 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1765 d_printf(_("Added alias '%s'.\n"), argv[0]);
1771 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1773 if (c->opt_localgroup)
1774 return rpc_alias_add_internals(c, argc, argv);
1776 return rpc_group_add_internals(c, argc, argv);
1779 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1780 TALLOC_CTX *mem_ctx,
1783 enum lsa_SidType *type)
1785 DOM_SID *sids = NULL;
1786 enum lsa_SidType *types = NULL;
1787 struct rpc_pipe_client *pipe_hnd = NULL;
1788 struct policy_handle lsa_pol;
1789 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1791 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1793 if (!NT_STATUS_IS_OK(result)) {
1797 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1798 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1800 if (!NT_STATUS_IS_OK(result)) {
1804 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1805 &name, NULL, 1, &sids, &types);
1807 if (NT_STATUS_IS_OK(result)) {
1808 sid_copy(sid, &sids[0]);
1812 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1816 TALLOC_FREE(pipe_hnd);
1819 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1821 /* Try as S-1-5-whatever */
1825 if (string_to_sid(&tmp_sid, name)) {
1826 sid_copy(sid, &tmp_sid);
1827 *type = SID_NAME_UNKNOWN;
1828 result = NT_STATUS_OK;
1835 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1836 TALLOC_CTX *mem_ctx,
1837 const DOM_SID *group_sid,
1840 struct policy_handle connect_pol, domain_pol;
1843 struct policy_handle group_pol;
1845 struct samr_Ids rids, rid_types;
1846 struct lsa_String lsa_acct_name;
1850 sid_copy(&sid, group_sid);
1852 if (!sid_split_rid(&sid, &group_rid)) {
1853 return NT_STATUS_UNSUCCESSFUL;
1856 /* Get sam policy handle */
1857 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1859 MAXIMUM_ALLOWED_ACCESS,
1861 if (!NT_STATUS_IS_OK(result)) {
1865 /* Get domain policy handle */
1866 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1868 MAXIMUM_ALLOWED_ACCESS,
1871 if (!NT_STATUS_IS_OK(result)) {
1875 init_lsa_String(&lsa_acct_name, member);
1877 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1884 if (!NT_STATUS_IS_OK(result)) {
1885 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1890 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1892 MAXIMUM_ALLOWED_ACCESS,
1896 if (!NT_STATUS_IS_OK(result)) {
1900 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1903 0x0005); /* unknown flags */
1906 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1910 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1911 TALLOC_CTX *mem_ctx,
1912 const DOM_SID *alias_sid,
1915 struct policy_handle connect_pol, domain_pol;
1918 struct policy_handle alias_pol;
1921 enum lsa_SidType member_type;
1925 sid_copy(&sid, alias_sid);
1927 if (!sid_split_rid(&sid, &alias_rid)) {
1928 return NT_STATUS_UNSUCCESSFUL;
1931 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1932 member, &member_sid, &member_type);
1934 if (!NT_STATUS_IS_OK(result)) {
1935 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1940 /* Get sam policy handle */
1941 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1943 MAXIMUM_ALLOWED_ACCESS,
1945 if (!NT_STATUS_IS_OK(result)) {
1949 /* Get domain policy handle */
1950 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1952 MAXIMUM_ALLOWED_ACCESS,
1955 if (!NT_STATUS_IS_OK(result)) {
1959 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1961 MAXIMUM_ALLOWED_ACCESS,
1965 if (!NT_STATUS_IS_OK(result)) {
1969 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1973 if (!NT_STATUS_IS_OK(result)) {
1978 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1982 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1983 const DOM_SID *domain_sid,
1984 const char *domain_name,
1985 struct cli_state *cli,
1986 struct rpc_pipe_client *pipe_hnd,
1987 TALLOC_CTX *mem_ctx,
1992 enum lsa_SidType group_type;
1994 if (argc != 2 || c->display_usage) {
1997 _("net rpc group addmem <group> <member>\n"
1998 " Add a member to a group\n"
1999 " group\tGroup to add member to\n"
2000 " member\tMember to add to group\n"));
2001 return NT_STATUS_UNSUCCESSFUL;
2004 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2005 &group_sid, &group_type))) {
2006 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2008 return NT_STATUS_UNSUCCESSFUL;
2011 if (group_type == SID_NAME_DOM_GRP) {
2012 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2013 &group_sid, argv[1]);
2015 if (!NT_STATUS_IS_OK(result)) {
2016 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2017 argv[1], argv[0], nt_errstr(result));
2022 if (group_type == SID_NAME_ALIAS) {
2023 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2024 &group_sid, argv[1]);
2026 if (!NT_STATUS_IS_OK(result)) {
2027 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2028 argv[1], argv[0], nt_errstr(result));
2033 d_fprintf(stderr, _("Can only add members to global or local groups "
2034 "which %s is not\n"), argv[0]);
2036 return NT_STATUS_UNSUCCESSFUL;
2039 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2041 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2042 rpc_group_addmem_internals,
2046 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2047 struct rpc_pipe_client *pipe_hnd,
2048 TALLOC_CTX *mem_ctx,
2049 const DOM_SID *group_sid,
2052 struct policy_handle connect_pol, domain_pol;
2055 struct policy_handle group_pol;
2057 struct samr_Ids rids, rid_types;
2058 struct lsa_String lsa_acct_name;
2062 sid_copy(&sid, group_sid);
2064 if (!sid_split_rid(&sid, &group_rid))
2065 return NT_STATUS_UNSUCCESSFUL;
2067 /* Get sam policy handle */
2068 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2070 MAXIMUM_ALLOWED_ACCESS,
2072 if (!NT_STATUS_IS_OK(result))
2075 /* Get domain policy handle */
2076 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2078 MAXIMUM_ALLOWED_ACCESS,
2081 if (!NT_STATUS_IS_OK(result))
2084 init_lsa_String(&lsa_acct_name, member);
2086 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2092 if (!NT_STATUS_IS_OK(result)) {
2093 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2098 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2100 MAXIMUM_ALLOWED_ACCESS,
2104 if (!NT_STATUS_IS_OK(result))
2107 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2112 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2116 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2117 TALLOC_CTX *mem_ctx,
2118 const DOM_SID *alias_sid,
2121 struct policy_handle connect_pol, domain_pol;
2124 struct policy_handle alias_pol;
2127 enum lsa_SidType member_type;
2131 sid_copy(&sid, alias_sid);
2133 if (!sid_split_rid(&sid, &alias_rid))
2134 return NT_STATUS_UNSUCCESSFUL;
2136 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2137 member, &member_sid, &member_type);
2139 if (!NT_STATUS_IS_OK(result)) {
2140 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2145 /* Get sam policy handle */
2146 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2148 MAXIMUM_ALLOWED_ACCESS,
2150 if (!NT_STATUS_IS_OK(result)) {
2154 /* Get domain policy handle */
2155 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2157 MAXIMUM_ALLOWED_ACCESS,
2160 if (!NT_STATUS_IS_OK(result)) {
2164 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2166 MAXIMUM_ALLOWED_ACCESS,
2170 if (!NT_STATUS_IS_OK(result))
2173 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2177 if (!NT_STATUS_IS_OK(result))
2181 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2185 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2186 const DOM_SID *domain_sid,
2187 const char *domain_name,
2188 struct cli_state *cli,
2189 struct rpc_pipe_client *pipe_hnd,
2190 TALLOC_CTX *mem_ctx,
2195 enum lsa_SidType group_type;
2197 if (argc != 2 || c->display_usage) {
2200 _("net rpc group delmem <group> <member>\n"
2201 " Delete a member from a group\n"
2202 " group\tGroup to delete member from\n"
2203 " member\tMember to delete from group\n"));
2204 return NT_STATUS_UNSUCCESSFUL;
2207 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2208 &group_sid, &group_type))) {
2209 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2211 return NT_STATUS_UNSUCCESSFUL;
2214 if (group_type == SID_NAME_DOM_GRP) {
2215 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2216 &group_sid, argv[1]);
2218 if (!NT_STATUS_IS_OK(result)) {
2219 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2220 argv[1], argv[0], nt_errstr(result));
2225 if (group_type == SID_NAME_ALIAS) {
2226 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2227 &group_sid, argv[1]);
2229 if (!NT_STATUS_IS_OK(result)) {
2230 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2231 argv[1], argv[0], nt_errstr(result));
2236 d_fprintf(stderr, _("Can only delete members from global or local "
2237 "groups which %s is not\n"), argv[0]);
2239 return NT_STATUS_UNSUCCESSFUL;
2242 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2244 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2245 rpc_group_delmem_internals,
2250 * List groups on a remote RPC server.
2252 * All parameters are provided by the run_rpc_command function, except for
2253 * argc, argv which are passes through.
2255 * @param domain_sid The domain sid acquired from the remote server.
2256 * @param cli A cli_state connected to the server.
2257 * @param mem_ctx Talloc context, destroyed on completion of the function.
2258 * @param argc Standard main() style argc.
2259 * @param argv Standard main() style argv. Initial components are already
2262 * @return Normal NTSTATUS return.
2265 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2266 const DOM_SID *domain_sid,
2267 const char *domain_name,
2268 struct cli_state *cli,
2269 struct rpc_pipe_client *pipe_hnd,
2270 TALLOC_CTX *mem_ctx,
2274 struct policy_handle connect_pol, domain_pol;
2275 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2276 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2277 struct samr_SamArray *groups = NULL;
2278 bool global = false;
2280 bool builtin = false;
2282 if (c->display_usage) {
2285 _("net rpc group list [global] [local] [builtin]\n"
2286 " List groups on RPC server\n"
2287 " global\tList global groups\n"
2288 " local\tList local groups\n"
2289 " builtin\tList builtin groups\n"
2290 " If none of global, local or builtin is "
2291 "specified, all three options are considered "
2293 return NT_STATUS_OK;
2302 for (i=0; i<argc; i++) {
2303 if (strequal(argv[i], "global"))
2306 if (strequal(argv[i], "local"))
2309 if (strequal(argv[i], "builtin"))
2313 /* Get sam policy handle */
2315 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2317 MAXIMUM_ALLOWED_ACCESS,
2319 if (!NT_STATUS_IS_OK(result)) {
2323 /* Get domain policy handle */
2325 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2327 MAXIMUM_ALLOWED_ACCESS,
2328 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2330 if (!NT_STATUS_IS_OK(result)) {
2334 /* Query domain groups */
2335 if (c->opt_long_list_entries)
2336 d_printf(_("\nGroup name Comment"
2337 "\n-----------------------------\n"));
2339 uint32_t max_size, total_size, returned_size;
2340 union samr_DispInfo info;
2344 get_query_dispinfo_params(
2345 loop_count, &max_entries, &max_size);
2347 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2356 num_entries = info.info3.count;
2357 start_idx += info.info3.count;
2359 if (!NT_STATUS_IS_OK(result) &&
2360 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2363 for (i = 0; i < num_entries; i++) {
2365 const char *group = NULL;
2366 const char *desc = NULL;
2368 group = info.info3.entries[i].account_name.string;
2369 desc = info.info3.entries[i].description.string;
2371 if (c->opt_long_list_entries)
2372 printf("%-21.21s %-50.50s\n",
2375 printf("%s\n", group);
2377 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2378 /* query domain aliases */
2383 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2389 if (!NT_STATUS_IS_OK(result) &&
2390 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2393 for (i = 0; i < num_entries; i++) {
2395 const char *description = NULL;
2397 if (c->opt_long_list_entries) {
2399 struct policy_handle alias_pol;
2400 union samr_AliasInfo *info = NULL;
2402 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2405 groups->entries[i].idx,
2407 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2411 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2413 description = info->description.string;
2417 if (description != NULL) {
2418 printf("%-21.21s %-50.50s\n",
2419 groups->entries[i].name.string,
2422 printf("%s\n", groups->entries[i].name.string);
2425 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2426 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2427 /* Get builtin policy handle */
2429 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2431 MAXIMUM_ALLOWED_ACCESS,
2432 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2434 if (!NT_STATUS_IS_OK(result)) {
2437 /* query builtin aliases */
2440 if (!builtin) break;
2442 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2448 if (!NT_STATUS_IS_OK(result) &&
2449 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2452 for (i = 0; i < num_entries; i++) {
2454 const char *description = NULL;
2456 if (c->opt_long_list_entries) {
2458 struct policy_handle alias_pol;
2459 union samr_AliasInfo *info = NULL;
2461 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2464 groups->entries[i].idx,
2466 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2470 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2472 description = info->description.string;
2476 if (description != NULL) {
2477 printf("%-21.21s %-50.50s\n",
2478 groups->entries[i].name.string,
2481 printf("%s\n", groups->entries[i].name.string);
2484 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2490 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2492 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2493 rpc_group_list_internals,
2497 static NTSTATUS rpc_list_group_members(struct net_context *c,
2498 struct rpc_pipe_client *pipe_hnd,
2499 TALLOC_CTX *mem_ctx,
2500 const char *domain_name,
2501 const DOM_SID *domain_sid,
2502 struct policy_handle *domain_pol,
2506 struct policy_handle group_pol;
2507 uint32 num_members, *group_rids;
2509 struct samr_RidTypeArray *rids = NULL;
2510 struct lsa_Strings names;
2511 struct samr_Ids types;
2514 sid_to_fstring(sid_str, domain_sid);
2516 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2518 MAXIMUM_ALLOWED_ACCESS,
2522 if (!NT_STATUS_IS_OK(result))
2525 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2529 if (!NT_STATUS_IS_OK(result))
2532 num_members = rids->count;
2533 group_rids = rids->rids;
2535 while (num_members > 0) {
2536 int this_time = 512;
2538 if (num_members < this_time)
2539 this_time = num_members;
2541 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2548 if (!NT_STATUS_IS_OK(result))
2551 /* We only have users as members, but make the output
2552 the same as the output of alias members */
2554 for (i = 0; i < this_time; i++) {
2556 if (c->opt_long_list_entries) {
2557 printf("%s-%d %s\\%s %d\n", sid_str,
2558 group_rids[i], domain_name,
2559 names.names[i].string,
2562 printf("%s\\%s\n", domain_name,
2563 names.names[i].string);
2567 num_members -= this_time;
2571 return NT_STATUS_OK;
2574 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2575 struct rpc_pipe_client *pipe_hnd,
2576 TALLOC_CTX *mem_ctx,
2577 struct policy_handle *domain_pol,
2581 struct rpc_pipe_client *lsa_pipe;
2582 struct policy_handle alias_pol, lsa_pol;
2584 DOM_SID *alias_sids;
2587 enum lsa_SidType *types;
2589 struct lsa_SidArray sid_array;
2591 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2593 MAXIMUM_ALLOWED_ACCESS,
2597 if (!NT_STATUS_IS_OK(result))
2600 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2604 if (!NT_STATUS_IS_OK(result)) {
2605 d_fprintf(stderr, _("Couldn't list alias members\n"));
2609 num_members = sid_array.num_sids;
2611 if (num_members == 0) {
2612 return NT_STATUS_OK;
2615 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2616 &ndr_table_lsarpc.syntax_id,
2618 if (!NT_STATUS_IS_OK(result)) {
2619 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2620 nt_errstr(result) );
2624 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2625 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2627 if (!NT_STATUS_IS_OK(result)) {
2628 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2629 TALLOC_FREE(lsa_pipe);
2633 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2635 d_fprintf(stderr, _("Out of memory\n"));
2636 TALLOC_FREE(lsa_pipe);
2637 return NT_STATUS_NO_MEMORY;
2640 for (i=0; i<num_members; i++) {
2641 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2644 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2645 num_members, alias_sids,
2646 &domains, &names, &types);
2648 if (!NT_STATUS_IS_OK(result) &&
2649 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2650 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2651 TALLOC_FREE(lsa_pipe);
2655 for (i = 0; i < num_members; i++) {
2657 sid_to_fstring(sid_str, &alias_sids[i]);
2659 if (c->opt_long_list_entries) {
2660 printf("%s %s\\%s %d\n", sid_str,
2661 domains[i] ? domains[i] : _("*unknown*"),
2662 names[i] ? names[i] : _("*unknown*"), types[i]);
2665 printf("%s\\%s\n", domains[i], names[i]);
2667 printf("%s\n", sid_str);
2671 TALLOC_FREE(lsa_pipe);
2672 return NT_STATUS_OK;
2675 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2676 const DOM_SID *domain_sid,
2677 const char *domain_name,
2678 struct cli_state *cli,
2679 struct rpc_pipe_client *pipe_hnd,
2680 TALLOC_CTX *mem_ctx,
2685 struct policy_handle connect_pol, domain_pol;
2686 struct samr_Ids rids, rid_types;
2687 struct lsa_String lsa_acct_name;
2689 /* Get sam policy handle */
2691 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2693 MAXIMUM_ALLOWED_ACCESS,
2696 if (!NT_STATUS_IS_OK(result))
2699 /* Get domain policy handle */
2701 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2703 MAXIMUM_ALLOWED_ACCESS,
2704 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2707 if (!NT_STATUS_IS_OK(result))
2710 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2712 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2719 if (!NT_STATUS_IS_OK(result)) {
2721 /* Ok, did not find it in the global sam, try with builtin */
2723 DOM_SID sid_Builtin;
2725 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2727 sid_copy(&sid_Builtin, &global_sid_Builtin);
2729 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2731 MAXIMUM_ALLOWED_ACCESS,
2735 if (!NT_STATUS_IS_OK(result)) {
2736 d_fprintf(stderr, _("Couldn't find group %s\n"),
2741 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2748 if (!NT_STATUS_IS_OK(result)) {
2749 d_fprintf(stderr, _("Couldn't find group %s\n"),
2755 if (rids.count != 1) {
2756 d_fprintf(stderr, _("Couldn't find group %s\n"),
2761 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2762 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2763 domain_sid, &domain_pol,
2767 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2768 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2772 return NT_STATUS_NO_SUCH_GROUP;
2775 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2777 if (argc != 1 || c->display_usage) {
2778 return rpc_group_usage(c, argc, argv);
2781 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2782 rpc_group_members_internals,
2786 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2788 NET_API_STATUS status;
2789 struct GROUP_INFO_0 g0;
2793 d_printf(_("Usage:\n"));
2794 d_printf("net rpc group rename group newname\n");
2798 g0.grpi0_name = argv[1];
2800 status = NetGroupSetInfo(c->opt_host,
2807 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2808 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2816 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2818 if (argc != 2 || c->display_usage) {
2819 return rpc_group_usage(c, argc, argv);
2822 return rpc_group_rename_internals(c, argc, argv);
2826 * 'net rpc group' entrypoint.
2827 * @param argc Standard main() style argc.
2828 * @param argv Standard main() style argv. Initial components are already
2832 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2834 NET_API_STATUS status;
2836 struct functable func[] = {
2841 N_("Create specified group"),
2842 N_("net rpc group add\n"
2843 " Create specified group")
2849 N_("Delete specified group"),
2850 N_("net rpc group delete\n"
2851 " Delete specified group")
2857 N_("Add member to group"),
2858 N_("net rpc group addmem\n"
2859 " Add member to group")
2865 N_("Remove member from group"),
2866 N_("net rpc group delmem\n"
2867 " Remove member from group")
2874 N_("net rpc group list\n"
2881 N_("List group members"),
2882 N_("net rpc group members\n"
2883 " List group members")
2890 N_("net rpc group rename\n"
2893 {NULL, NULL, 0, NULL, NULL}
2896 status = libnetapi_init(&c->netapi_ctx);
2900 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2901 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2902 if (c->opt_kerberos) {
2903 libnetapi_set_use_kerberos(c->netapi_ctx);
2907 if (c->display_usage) {
2908 d_printf(_("Usage:\n"));
2909 d_printf(_("net rpc group\n"
2910 " Alias for net rpc group list global "
2911 "local builtin\n"));
2912 net_display_usage_from_functable(func);
2916 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2917 rpc_group_list_internals,
2921 return net_run_function(c, argc, argv, "net rpc group", func);
2924 /****************************************************************************/
2926 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2928 return net_share_usage(c, argc, argv);
2932 * Add a share on a remote RPC server.
2934 * @param argc Standard main() style argc.
2935 * @param argv Standard main() style argv. Initial components are already
2938 * @return A shell status integer (0 for success).
2941 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2943 NET_API_STATUS status;
2946 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2947 uint32 num_users=0, perms=0;
2948 char *password=NULL; /* don't allow a share password */
2949 struct SHARE_INFO_2 i2;
2950 uint32_t parm_error = 0;
2952 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2953 return rpc_share_usage(c, argc, argv);
2956 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2960 path = strchr(sharename, '=');
2967 i2.shi2_netname = sharename;
2968 i2.shi2_type = type;
2969 i2.shi2_remark = c->opt_comment;
2970 i2.shi2_permissions = perms;
2971 i2.shi2_max_uses = c->opt_maxusers;
2972 i2.shi2_current_uses = num_users;
2973 i2.shi2_path = path;
2974 i2.shi2_passwd = password;
2976 status = NetShareAdd(c->opt_host,
2981 printf(_("NetShareAdd failed with: %s\n"),
2982 libnetapi_get_error_string(c->netapi_ctx, status));
2989 * Delete a share on a remote RPC server.
2991 * @param domain_sid The domain sid acquired from the remote server.
2992 * @param argc Standard main() style argc.
2993 * @param argv Standard main() style argv. Initial components are already
2996 * @return A shell status integer (0 for success).
2998 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3000 if (argc < 1 || c->display_usage) {
3001 return rpc_share_usage(c, argc, argv);
3004 return NetShareDel(c->opt_host, argv[0], 0);
3008 * Formatted print of share info
3010 * @param r pointer to SHARE_INFO_1 to format
3013 static void display_share_info_1(struct net_context *c,
3014 struct SHARE_INFO_1 *r)
3016 if (c->opt_long_list_entries) {
3017 d_printf("%-12s %-8.8s %-50s\n",
3019 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3022 d_printf("%s\n", r->shi1_netname);
3026 static WERROR get_share_info(struct net_context *c,
3027 struct rpc_pipe_client *pipe_hnd,
3028 TALLOC_CTX *mem_ctx,
3032 struct srvsvc_NetShareInfoCtr *info_ctr)
3036 union srvsvc_NetShareInfo info;
3038 /* no specific share requested, enumerate all */
3041 uint32_t preferred_len = 0xffffffff;
3042 uint32_t total_entries = 0;
3043 uint32_t resume_handle = 0;
3045 info_ctr->level = level;
3047 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3057 /* request just one share */
3058 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3065 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3070 ZERO_STRUCTP(info_ctr);
3072 info_ctr->level = level;
3077 struct srvsvc_NetShareCtr1 *ctr1;
3079 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3080 W_ERROR_HAVE_NO_MEMORY(ctr1);
3083 ctr1->array = info.info1;
3085 info_ctr->ctr.ctr1 = ctr1;
3089 struct srvsvc_NetShareCtr2 *ctr2;
3091 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3092 W_ERROR_HAVE_NO_MEMORY(ctr2);
3095 ctr2->array = info.info2;
3097 info_ctr->ctr.ctr2 = ctr2;
3101 struct srvsvc_NetShareCtr502 *ctr502;
3103 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3104 W_ERROR_HAVE_NO_MEMORY(ctr502);
3107 ctr502->array = info.info502;
3109 info_ctr->ctr.ctr502 = ctr502;
3117 * 'net rpc share list' entrypoint.
3118 * @param argc Standard main() style argc.
3119 * @param argv Standard main() style argv. Initial components are already
3122 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3124 NET_API_STATUS status;
3125 struct SHARE_INFO_1 *i1 = NULL;
3126 uint32_t entries_read = 0;
3127 uint32_t total_entries = 0;
3128 uint32_t resume_handle = 0;
3129 uint32_t i, level = 1;
3131 if (c->display_usage) {
3133 "net rpc share list\n"
3136 _("List shares on remote server"));
3140 status = NetShareEnum(c->opt_host,
3142 (uint8_t **)(void *)&i1,
3151 /* Display results */
3153 if (c->opt_long_list_entries) {
3155 "\nEnumerating shared resources (exports) on remote server:\n\n"
3156 "\nShare name Type Description\n"
3157 "---------- ---- -----------\n"));
3159 for (i = 0; i < entries_read; i++)
3160 display_share_info_1(c, &i1[i]);
3165 static bool check_share_availability(struct cli_state *cli, const char *netname)
3169 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3170 if (!NT_STATUS_IS_OK(status)) {
3171 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3175 status = cli_tdis(cli);
3176 if (!NT_STATUS_IS_OK(status)) {
3177 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3184 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3185 const char *netname, uint32 type)
3187 /* only support disk shares */
3188 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3189 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3194 /* skip builtin shares */
3195 /* FIXME: should print$ be added too ? */
3196 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3197 strequal(netname,"global"))
3200 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3201 printf(_("excluding [%s]\n"), netname);
3205 return check_share_availability(cli, netname);
3209 * Migrate shares from a remote RPC server to the local RPC server.
3211 * All parameters are provided by the run_rpc_command function, except for
3212 * argc, argv which are passed through.
3214 * @param domain_sid The domain sid acquired from the remote server.
3215 * @param cli A cli_state connected to the server.
3216 * @param mem_ctx Talloc context, destroyed on completion of the function.
3217 * @param argc Standard main() style argc.
3218 * @param argv Standard main() style argv. Initial components are already
3221 * @return Normal NTSTATUS return.
3224 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3225 const DOM_SID *domain_sid,
3226 const char *domain_name,
3227 struct cli_state *cli,
3228 struct rpc_pipe_client *pipe_hnd,
3229 TALLOC_CTX *mem_ctx,
3234 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3235 struct srvsvc_NetShareInfoCtr ctr_src;
3237 struct rpc_pipe_client *srvsvc_pipe = NULL;
3238 struct cli_state *cli_dst = NULL;
3239 uint32 level = 502; /* includes secdesc */
3240 uint32_t parm_error = 0;
3242 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3244 if (!W_ERROR_IS_OK(result))
3247 /* connect destination PI_SRVSVC */
3248 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3249 &ndr_table_srvsvc.syntax_id);
3250 if (!NT_STATUS_IS_OK(nt_status))
3254 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3256 union srvsvc_NetShareInfo info;
3257 struct srvsvc_NetShareInfo502 info502 =
3258 ctr_src.ctr.ctr502->array[i];
3260 /* reset error-code */
3261 nt_status = NT_STATUS_UNSUCCESSFUL;
3263 if (!check_share_sanity(c, cli, info502.name, info502.type))
3266 /* finally add the share on the dst server */
3268 printf(_("migrating: [%s], path: %s, comment: %s, without "
3270 info502.name, info502.path, info502.comment);
3272 info.info502 = &info502;
3274 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3275 srvsvc_pipe->desthost,
3281 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3282 printf(_(" [%s] does already exist\n"),
3287 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3288 printf(_("cannot add share: %s\n"), win_errstr(result));
3294 nt_status = NT_STATUS_OK;
3298 cli_shutdown(cli_dst);
3306 * Migrate shares from a RPC server to another.
3308 * @param argc Standard main() style argc.
3309 * @param argv Standard main() style argv. Initial components are already
3312 * @return A shell status integer (0 for success).
3314 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3317 if (c->display_usage) {
3319 "net rpc share migrate shares\n"
3322 _("Migrate shares to local server"));
3327 printf(_("no server to migrate\n"));
3331 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3332 rpc_share_migrate_shares_internals,
3339 * @param f file_info
3340 * @param mask current search mask
3341 * @param state arg-pointer
3344 static void copy_fn(const char *mnt, file_info *f,
3345 const char *mask, void *state)
3347 static NTSTATUS nt_status;
3348 static struct copy_clistate *local_state;
3349 static fstring filename, new_mask;
3352 struct net_context *c;
3354 local_state = (struct copy_clistate *)state;
3355 nt_status = NT_STATUS_UNSUCCESSFUL;
3359 if (strequal(f->name, ".") || strequal(f->name, ".."))
3362 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3365 if (f->mode & aDIR) {
3367 DEBUG(3,("got dir: %s\n", f->name));
3369 fstrcpy(dir, local_state->cwd);
3371 fstrcat(dir, f->name);
3373 switch (net_mode_share)
3375 case NET_MODE_SHARE_MIGRATE:
3376 /* create that directory */
3377 nt_status = net_copy_file(c, local_state->mem_ctx,
3378 local_state->cli_share_src,
3379 local_state->cli_share_dst,
3381 c->opt_acls? true : false,
3382 c->opt_attrs? true : false,
3383 c->opt_timestamps? true:false,
3387 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3391 if (!NT_STATUS_IS_OK(nt_status))
3392 printf(_("could not handle dir %s: %s\n"),
3393 dir, nt_errstr(nt_status));
3395 /* search below that directory */
3396 fstrcpy(new_mask, dir);
3397 fstrcat(new_mask, "\\*");
3399 old_dir = local_state->cwd;
3400 local_state->cwd = dir;
3401 if (!sync_files(local_state, new_mask))
3402 printf(_("could not handle files\n"));
3403 local_state->cwd = old_dir;
3410 fstrcpy(filename, local_state->cwd);
3411 fstrcat(filename, "\\");
3412 fstrcat(filename, f->name);
3414 DEBUG(3,("got file: %s\n", filename));
3416 switch (net_mode_share)
3418 case NET_MODE_SHARE_MIGRATE:
3419 nt_status = net_copy_file(c, local_state->mem_ctx,
3420 local_state->cli_share_src,
3421 local_state->cli_share_dst,
3423 c->opt_acls? true : false,
3424 c->opt_attrs? true : false,
3425 c->opt_timestamps? true: false,
3429 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3434 if (!NT_STATUS_IS_OK(nt_status))
3435 printf(_("could not handle file %s: %s\n"),
3436 filename, nt_errstr(nt_status));
3441 * sync files, can be called recursivly to list files
3442 * and then call copy_fn for each file
3444 * @param cp_clistate pointer to the copy_clistate we work with
3445 * @param mask the current search mask
3447 * @return Boolean result
3449 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3451 struct cli_state *targetcli;
3452 char *targetpath = NULL;
3454 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3456 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3457 mask, &targetcli, &targetpath ) ) {
3458 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3460 mask, cli_errstr(cp_clistate->cli_share_src));
3464 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3465 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3466 mask, cli_errstr(targetcli));
3475 * Set the top level directory permissions before we do any further copies.
3476 * Should set up ACL inheritance.
3479 bool copy_top_level_perms(struct net_context *c,
3480 struct copy_clistate *cp_clistate,
3481 const char *sharename)
3483 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3485 switch (net_mode_share) {
3486 case NET_MODE_SHARE_MIGRATE:
3487 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3488 nt_status = net_copy_fileattr(c,
3489 cp_clistate->mem_ctx,
3490 cp_clistate->cli_share_src,
3491 cp_clistate->cli_share_dst,
3493 c->opt_acls? true : false,
3494 c->opt_attrs? true : false,
3495 c->opt_timestamps? true: false,
3499 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3503 if (!NT_STATUS_IS_OK(nt_status)) {
3504 printf(_("Could handle directory attributes for top level "
3505 "directory of share %s. Error %s\n"),
3506 sharename, nt_errstr(nt_status));
3514 * Sync all files inside a remote share to another share (over smb).
3516 * All parameters are provided by the run_rpc_command function, except for
3517 * argc, argv which are passed through.
3519 * @param domain_sid The domain sid acquired from the remote server.
3520 * @param cli A cli_state connected to the server.
3521 * @param mem_ctx Talloc context, destroyed on completion of the function.
3522 * @param argc Standard main() style argc.
3523 * @param argv Standard main() style argv. Initial components are already
3526 * @return Normal NTSTATUS return.
3529 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3530 const DOM_SID *domain_sid,
3531 const char *domain_name,
3532 struct cli_state *cli,
3533 struct rpc_pipe_client *pipe_hnd,
3534 TALLOC_CTX *mem_ctx,
3539 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3540 struct srvsvc_NetShareInfoCtr ctr_src;
3543 struct copy_clistate cp_clistate;
3544 bool got_src_share = false;
3545 bool got_dst_share = false;
3546 const char *mask = "\\*";
3549 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3551 nt_status = NT_STATUS_NO_MEMORY;
3555 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3558 if (!W_ERROR_IS_OK(result))
3561 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3563 struct srvsvc_NetShareInfo502 info502 =
3564 ctr_src.ctr.ctr502->array[i];
3566 if (!check_share_sanity(c, cli, info502.name, info502.type))
3569 /* one might not want to mirror whole discs :) */
3570 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3571 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3576 switch (net_mode_share)
3578 case NET_MODE_SHARE_MIGRATE:
3582 d_fprintf(stderr, _("Unsupported mode %d\n"),
3586 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3589 c->opt_acls ? _("including") : _("without"),
3590 c->opt_attrs ? _("including") : _("without"),
3591 c->opt_timestamps ? _("(preserving timestamps)") : "");
3593 cp_clistate.mem_ctx = mem_ctx;
3594 cp_clistate.cli_share_src = NULL;
3595 cp_clistate.cli_share_dst = NULL;
3596 cp_clistate.cwd = NULL;
3597 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3600 /* open share source */
3601 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3602 &cli->dest_ss, cli->desthost,
3603 info502.name, "A:");
3604 if (!NT_STATUS_IS_OK(nt_status))
3607 got_src_share = true;
3609 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3610 /* open share destination */
3611 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3612 NULL, dst, info502.name, "A:");
3613 if (!NT_STATUS_IS_OK(nt_status))
3616 got_dst_share = true;
3619 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3620 d_fprintf(stderr, _("Could not handle the top level "
3621 "directory permissions for the "
3622 "share: %s\n"), info502.name);
3623 nt_status = NT_STATUS_UNSUCCESSFUL;
3627 if (!sync_files(&cp_clistate, mask)) {
3628 d_fprintf(stderr, _("could not handle files for share: "
3629 "%s\n"), info502.name);
3630 nt_status = NT_STATUS_UNSUCCESSFUL;
3635 nt_status = NT_STATUS_OK;
3640 cli_shutdown(cp_clistate.cli_share_src);
3643 cli_shutdown(cp_clistate.cli_share_dst);
3650 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3652 if (c->display_usage) {
3654 "net share migrate files\n"
3657 _("Migrate files to local server"));
3662 d_printf(_("no server to migrate\n"));
3666 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3667 rpc_share_migrate_files_internals,
3672 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3674 * All parameters are provided by the run_rpc_command function, except for
3675 * argc, argv which are passed through.
3677 * @param domain_sid The domain sid acquired from the remote server.
3678 * @param cli A cli_state connected to the server.
3679 * @param mem_ctx Talloc context, destroyed on completion of the function.
3680 * @param argc Standard main() style argc.
3681 * @param argv Standard main() style argv. Initial components are already
3684 * @return Normal NTSTATUS return.
3687 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3688 const DOM_SID *domain_sid,
3689 const char *domain_name,
3690 struct cli_state *cli,
3691 struct rpc_pipe_client *pipe_hnd,
3692 TALLOC_CTX *mem_ctx,
3697 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3698 struct srvsvc_NetShareInfoCtr ctr_src;
3699 union srvsvc_NetShareInfo info;
3701 struct rpc_pipe_client *srvsvc_pipe = NULL;
3702 struct cli_state *cli_dst = NULL;
3703 uint32 level = 502; /* includes secdesc */
3704 uint32_t parm_error = 0;
3706 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3709 if (!W_ERROR_IS_OK(result))
3712 /* connect destination PI_SRVSVC */
3713 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3714 &ndr_table_srvsvc.syntax_id);
3715 if (!NT_STATUS_IS_OK(nt_status))
3719 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3721 struct srvsvc_NetShareInfo502 info502 =
3722 ctr_src.ctr.ctr502->array[i];
3724 /* reset error-code */
3725 nt_status = NT_STATUS_UNSUCCESSFUL;
3727 if (!check_share_sanity(c, cli, info502.name, info502.type))
3730 printf(_("migrating: [%s], path: %s, comment: %s, including "
3732 info502.name, info502.path, info502.comment);
3735 display_sec_desc(info502.sd_buf.sd);
3737 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3738 info.info502 = &info502;
3740 /* finally modify the share on the dst server */
3741 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3742 srvsvc_pipe->desthost,
3748 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3749 printf(_("cannot set share-acl: %s\n"),
3750 win_errstr(result));
3756 nt_status = NT_STATUS_OK;
3760 cli_shutdown(cli_dst);
3768 * Migrate share-acls from a RPC server to another.
3770 * @param argc Standard main() style argc.
3771 * @param argv Standard main() style argv. Initial components are already
3774 * @return A shell status integer (0 for success).
3776 static int rpc_share_migrate_security(struct net_context *c, int argc,
3779 if (c->display_usage) {
3781 "net rpc share migrate security\n"
3784 _("Migrate share-acls to local server"));
3789 d_printf(_("no server to migrate\n"));
3793 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3794 rpc_share_migrate_security_internals,
3799 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3800 * from one server to another.
3802 * @param argc Standard main() style argc.
3803 * @param argv Standard main() style argv. Initial components are already
3806 * @return A shell status integer (0 for success).
3809 static int rpc_share_migrate_all(struct net_context *c, int argc,
3814 if (c->display_usage) {
3816 "net rpc share migrate all\n"
3819 _("Migrates shares including all share settings"));
3824 d_printf(_("no server to migrate\n"));
3828 /* order is important. we don't want to be locked out by the share-acl
3829 * before copying files - gd */
3831 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3832 rpc_share_migrate_shares_internals, argc, argv);
3836 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3837 rpc_share_migrate_files_internals, argc, argv);
3841 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3842 rpc_share_migrate_security_internals, argc,
3848 * 'net rpc share migrate' entrypoint.
3849 * @param argc Standard main() style argc.
3850 * @param argv Standard main() style argv. Initial components are already
3853 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3856 struct functable func[] = {
3859 rpc_share_migrate_all,
3861 N_("Migrate shares from remote to local server"),
3862 N_("net rpc share migrate all\n"
3863 " Migrate shares from remote to local server")
3867 rpc_share_migrate_files,
3869 N_("Migrate files from remote to local server"),
3870 N_("net rpc share migrate files\n"
3871 " Migrate files from remote to local server")
3875 rpc_share_migrate_security,
3877 N_("Migrate share-ACLs from remote to local server"),
3878 N_("net rpc share migrate security\n"
3879 " Migrate share-ACLs from remote to local server")
3883 rpc_share_migrate_shares,
3885 N_("Migrate shares from remote to local server"),
3886 N_("net rpc share migrate shares\n"
3887 " Migrate shares from remote to local server")
3889 {NULL, NULL, 0, NULL, NULL}
3892 net_mode_share = NET_MODE_SHARE_MIGRATE;
3894 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3903 static int num_server_aliases;
3904 static struct full_alias *server_aliases;
3907 * Add an alias to the static list.
3909 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3911 if (server_aliases == NULL)
3912 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3914 server_aliases[num_server_aliases] = *alias;
3915 num_server_aliases += 1;
3919 * For a specific domain on the server, fetch all the aliases
3920 * and their members. Add all of them to the server_aliases.
3923 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3924 TALLOC_CTX *mem_ctx,
3925 struct policy_handle *connect_pol,
3926 const DOM_SID *domain_sid)
3928 uint32 start_idx, max_entries, num_entries, i;
3929 struct samr_SamArray *groups = NULL;
3931 struct policy_handle domain_pol;
3933 /* Get domain policy handle */
3935 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3937 MAXIMUM_ALLOWED_ACCESS,
3938 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3940 if (!NT_STATUS_IS_OK(result))
3947 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3953 for (i = 0; i < num_entries; i++) {
3955 struct policy_handle alias_pol;
3956 struct full_alias alias;
3957 struct lsa_SidArray sid_array;
3960 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3962 MAXIMUM_ALLOWED_ACCESS,
3963 groups->entries[i].idx,
3965 if (!NT_STATUS_IS_OK(result))
3968 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3971 if (!NT_STATUS_IS_OK(result))
3974 alias.num_members = sid_array.num_sids;
3976 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3977 if (!NT_STATUS_IS_OK(result))
3980 alias.members = NULL;
3982 if (alias.num_members > 0) {
3983 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3985 for (j = 0; j < alias.num_members; j++)
3986 sid_copy(&alias.members[j],
3987 sid_array.sids[j].sid);
3990 sid_compose(&alias.sid, domain_sid,
3991 groups->entries[i].idx);
3993 push_alias(mem_ctx, &alias);
3995 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3997 result = NT_STATUS_OK;
4000 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4006 * Dump server_aliases as names for debugging purposes.
4009 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4010 const DOM_SID *domain_sid,
4011 const char *domain_name,
4012 struct cli_state *cli,
4013 struct rpc_pipe_client *pipe_hnd,
4014 TALLOC_CTX *mem_ctx,
4020 struct policy_handle lsa_pol;
4022 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4023 SEC_FLAG_MAXIMUM_ALLOWED,
4025 if (!NT_STATUS_IS_OK(result))
4028 for (i=0; i<num_server_aliases; i++) {
4031 enum lsa_SidType *types;
4034 struct full_alias *alias = &server_aliases[i];
4036 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4038 &domains, &names, &types);
4039 if (!NT_STATUS_IS_OK(result))
4042 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4044 if (alias->num_members == 0) {
4049 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4052 &domains, &names, &types);
4054 if (!NT_STATUS_IS_OK(result) &&
4055 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4058 for (j=0; j<alias->num_members; j++)
4059 DEBUG(1, ("%s\\%s (%d); ",
4060 domains[j] ? domains[j] : "*unknown*",
4061 names[j] ? names[j] : "*unknown*",types[j]));
4065 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4067 return NT_STATUS_OK;
4071 * Fetch a list of all server aliases and their members into
4075 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4076 const DOM_SID *domain_sid,
4077 const char *domain_name,
4078 struct cli_state *cli,
4079 struct rpc_pipe_client *pipe_hnd,
4080 TALLOC_CTX *mem_ctx,
4085 struct policy_handle connect_pol;
4087 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4089 MAXIMUM_ALLOWED_ACCESS,
4092 if (!NT_STATUS_IS_OK(result))
4095 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4096 &global_sid_Builtin);
4098 if (!NT_STATUS_IS_OK(result))
4101 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4104 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4109 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4111 token->num_sids = 4;
4113 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4114 d_fprintf(stderr, "malloc %s\n",_("failed"));
4115 token->num_sids = 0;
4119 token->user_sids[0] = *user_sid;
4120 sid_copy(&token->user_sids[1], &global_sid_World);
4121 sid_copy(&token->user_sids[2], &global_sid_Network);
4122 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4125 static void free_user_token(NT_USER_TOKEN *token)
4127 SAFE_FREE(token->user_sids);
4130 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4132 if (is_sid_in_token(token, sid))
4135 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4136 if (!token->user_sids) {
4140 sid_copy(&token->user_sids[token->num_sids], sid);
4142 token->num_sids += 1;
4147 NT_USER_TOKEN token;
4150 static void dump_user_token(struct user_token *token)
4154 d_printf("%s\n", token->name);
4156 for (i=0; i<token->token.num_sids; i++) {
4157 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4161 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4165 for (i=0; i<alias->num_members; i++) {
4166 if (sid_compare(sid, &alias->members[i]) == 0)
4173 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4177 for (i=0; i<num_server_aliases; i++) {
4178 if (is_alias_member(&sid, &server_aliases[i]))
4179 add_sid_to_token(token, &server_aliases[i].sid);
4184 * We got a user token with all the SIDs we can know about without asking the
4185 * server directly. These are the user and domain group sids. All of these can
4186 * be members of aliases. So scan the list of aliases for each of the SIDs and
4187 * add them to the token.
4190 static void collect_alias_memberships(NT_USER_TOKEN *token)
4192 int num_global_sids = token->num_sids;
4195 for (i=0; i<num_global_sids; i++) {
4196 collect_sid_memberships(token, token->user_sids[i]);
4200 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4202 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4203 enum wbcSidType type;
4205 struct wbcDomainSid wsid;
4206 char *sid_str = NULL;
4208 uint32_t num_groups;
4209 gid_t *groups = NULL;
4212 fstr_sprintf(full_name, "%s%c%s",
4213 domain, *lp_winbind_separator(), user);
4215 /* First let's find out the user sid */
4217 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4219 if (!WBC_ERROR_IS_OK(wbc_status)) {
4220 DEBUG(1, ("winbind could not find %s: %s\n",
4221 full_name, wbcErrorString(wbc_status)));
4225 wbc_status = wbcSidToString(&wsid, &sid_str);
4226 if (!WBC_ERROR_IS_OK(wbc_status)) {
4230 if (type != SID_NAME_USER) {
4231 wbcFreeMemory(sid_str);
4232 DEBUG(1, ("%s is not a user\n", full_name));
4236 if (!string_to_sid(&user_sid, sid_str)) {
4237 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4241 wbcFreeMemory(sid_str);
4244 init_user_token(token, &user_sid);
4246 /* And now the groups winbind knows about */
4248 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4249 if (!WBC_ERROR_IS_OK(wbc_status)) {
4250 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4251 full_name, wbcErrorString(wbc_status)));
4255 for (i = 0; i < num_groups; i++) {
4256 gid_t gid = groups[i];
4259 wbc_status = wbcGidToSid(gid, &wsid);
4260 if (!WBC_ERROR_IS_OK(wbc_status)) {
4261 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4262 (unsigned int)gid, wbcErrorString(wbc_status)));
4263 wbcFreeMemory(groups);
4267 wbc_status = wbcSidToString(&wsid, &sid_str);
4268 if (!WBC_ERROR_IS_OK(wbc_status)) {
4269 wbcFreeMemory(groups);
4273 DEBUG(3, (" %s\n", sid_str));
4275 string_to_sid(&sid, sid_str);
4276 wbcFreeMemory(sid_str);
4279 add_sid_to_token(token, &sid);
4281 wbcFreeMemory(groups);
4287 * Get a list of all user tokens we want to look at
4290 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4291 struct user_token **user_tokens)
4293 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4294 uint32_t i, num_users;
4296 struct user_token *result;
4297 TALLOC_CTX *frame = NULL;
4299 if (lp_winbind_use_default_domain() &&
4300 (c->opt_target_workgroup == NULL)) {
4301 d_fprintf(stderr, _("winbind use default domain = yes set, "
4302 "please specify a workgroup\n"));
4306 /* Send request to winbind daemon */
4308 wbc_status = wbcListUsers(NULL, &num_users, &users);
4309 if (!WBC_ERROR_IS_OK(wbc_status)) {
4310 DEBUG(1, (_("winbind could not list users: %s\n"),
4311 wbcErrorString(wbc_status)));
4315 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4317 if (result == NULL) {
4318 DEBUG(1, ("Could not malloc sid array\n"));
4319 wbcFreeMemory(users);
4323 frame = talloc_stackframe();
4324 for (i=0; i < num_users; i++) {
4325 fstring domain, user;
4328 fstrcpy(result[i].name, users[i]);
4330 p = strchr(users[i], *lp_winbind_separator());
4332 DEBUG(3, ("%s\n", users[i]));
4335 fstrcpy(domain, c->opt_target_workgroup);
4336 fstrcpy(user, users[i]);
4339 fstrcpy(domain, users[i]);
4344 get_user_sids(domain, user, &(result[i].token));
4348 wbcFreeMemory(users);
4350 *num_tokens = num_users;
4351 *user_tokens = result;
4356 static bool get_user_tokens_from_file(FILE *f,
4358 struct user_token **tokens)
4360 struct user_token *token = NULL;
4365 if (fgets(line, sizeof(line)-1, f) == NULL) {
4369 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4370 line[strlen(line)-1] = '\0';
4373 if (line[0] == ' ') {
4377 if(!string_to_sid(&sid, &line[1])) {
4378 DEBUG(1,("get_user_tokens_from_file: Could "
4379 "not convert sid %s \n",&line[1]));
4383 if (token == NULL) {
4384 DEBUG(0, ("File does not begin with username"));
4388 add_sid_to_token(&token->token, &sid);
4392 /* And a new user... */
4395 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4396 if (*tokens == NULL) {
4397 DEBUG(0, ("Could not realloc tokens\n"));
4401 token = &((*tokens)[*num_tokens-1]);
4403 fstrcpy(token->name, line);
4404 token->token.num_sids = 0;
4405 token->token.user_sids = NULL;
4414 * Show the list of all users that have access to a share
4417 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4418 TALLOC_CTX *mem_ctx,
4419 const char *netname,
4421 struct user_token *tokens)
4424 struct security_descriptor *share_sd = NULL;
4425 struct security_descriptor *root_sd = NULL;
4426 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4428 union srvsvc_NetShareInfo info;
4433 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4440 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4441 DEBUG(1, ("Coult not query secdesc for share %s\n",
4446 share_sd = info.info502->sd_buf.sd;
4447 if (share_sd == NULL) {
4448 DEBUG(1, ("Got no secdesc for share %s\n",
4454 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4458 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4459 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4460 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4463 for (i=0; i<num_tokens; i++) {
4466 if (share_sd != NULL) {
4467 status = se_access_check(share_sd, &tokens[i].token,
4470 if (!NT_STATUS_IS_OK(status)) {
4471 DEBUG(1, ("Could not check share_sd for "
4478 if (root_sd == NULL) {
4479 d_printf(" %s\n", tokens[i].name);
4483 status = se_access_check(root_sd, &tokens[i].token,
4485 if (!NT_STATUS_IS_OK(status)) {
4486 DEBUG(1, ("Could not check root_sd for user %s\n",
4490 d_printf(" %s\n", tokens[i].name);
4493 if (fnum != (uint16_t)-1)
4494 cli_close(cli, fnum);
4506 static void collect_share(const char *name, uint32 m,
4507 const char *comment, void *state)
4509 struct share_list *share_list = (struct share_list *)state;
4511 if (m != STYPE_DISKTREE)
4514 share_list->num_shares += 1;
4515 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4516 if (!share_list->shares) {
4517 share_list->num_shares = 0;
4520 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4524 * List shares on a remote RPC server, including the security descriptors.
4526 * All parameters are provided by the run_rpc_command function, except for
4527 * argc, argv which are passed through.
4529 * @param domain_sid The domain sid acquired from the remote server.
4530 * @param cli A cli_state connected to the server.
4531 * @param mem_ctx Talloc context, destroyed on completion of the function.
4532 * @param argc Standard main() style argc.
4533 * @param argv Standard main() style argv. Initial components are already
4536 * @return Normal NTSTATUS return.
4539 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4540 const DOM_SID *domain_sid,
4541 const char *domain_name,
4542 struct cli_state *cli,
4543 struct rpc_pipe_client *pipe_hnd,
4544 TALLOC_CTX *mem_ctx,
4553 struct user_token *tokens = NULL;
4556 struct share_list share_list;
4561 f = fopen(argv[0], "r");
4565 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4566 return NT_STATUS_UNSUCCESSFUL;
4569 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4575 DEBUG(0, ("Could not read users from file\n"));
4576 return NT_STATUS_UNSUCCESSFUL;
4579 for (i=0; i<num_tokens; i++)
4580 collect_alias_memberships(&tokens[i].token);
4582 share_list.num_shares = 0;
4583 share_list.shares = NULL;
4585 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4588 DEBUG(0, ("Error returning browse list: %s\n",
4593 for (i = 0; i < share_list.num_shares; i++) {
4594 char *netname = share_list.shares[i];
4596 if (netname[strlen(netname)-1] == '$')
4599 d_printf("%s\n", netname);
4601 show_userlist(pipe_hnd, mem_ctx, netname,
4602 num_tokens, tokens);
4605 for (i=0; i<num_tokens; i++) {
4606 free_user_token(&tokens[i].token);
4609 SAFE_FREE(share_list.shares);
4611 return NT_STATUS_OK;
4614 static int rpc_share_allowedusers(struct net_context *c, int argc,
4619 if (c->display_usage) {
4621 "net rpc share allowedusers\n"
4624 _("List allowed users"));
4628 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4629 rpc_aliaslist_internals,
4634 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4640 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4641 rpc_share_allowedusers_internals,
4645 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4648 struct user_token *tokens = NULL;
4652 net_usersidlist_usage(c, argc, argv);
4656 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4657 DEBUG(0, ("Could not get the user/sid list\n"));
4661 for (i=0; i<num_tokens; i++) {
4662 dump_user_token(&tokens[i]);
4663 free_user_token(&tokens[i].token);
4670 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4672 d_printf(_("net usersidlist\n"
4673 "\tprints out a list of all users the running winbind knows\n"
4674 "\tabout, together with all their SIDs. This is used as\n"
4675 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4677 net_common_flags_usage(c, argc, argv);
4682 * 'net rpc share' entrypoint.
4683 * @param argc Standard main() style argc.
4684 * @param argv Standard main() style argv. Initial components are already
4688 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4690 NET_API_STATUS status;
4692 struct functable func[] = {
4698 N_("net rpc share add\n"
4706 N_("net rpc share delete\n"
4711 rpc_share_allowedusers,
4713 N_("Modify allowed users"),
4714 N_("net rpc share allowedusers\n"
4715 " Modify allowed users")
4721 N_("Migrate share to local server"),
4722 N_("net rpc share migrate\n"
4723 " Migrate share to local server")
4730 N_("net rpc share list\n"
4733 {NULL, NULL, 0, NULL, NULL}
4736 status = libnetapi_init(&c->netapi_ctx);
4740 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4741 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4742 if (c->opt_kerberos) {
4743 libnetapi_set_use_kerberos(c->netapi_ctx);
4747 if (c->display_usage) {
4752 " Alias for net rpc share list\n"));
4753 net_display_usage_from_functable(func);
4757 return rpc_share_list(c, argc, argv);
4760 return net_run_function(c, argc, argv, "net rpc share", func);
4763 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4764 TALLOC_CTX *mem_ctx,
4765 struct rpc_sh_ctx *ctx,
4766 struct rpc_pipe_client *pipe_hnd,
4767 int argc, const char **argv)
4770 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4773 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4774 TALLOC_CTX *mem_ctx,
4775 struct rpc_sh_ctx *ctx,
4776 struct rpc_pipe_client *pipe_hnd,
4777 int argc, const char **argv)
4779 NET_API_STATUS status;
4780 uint32_t parm_err = 0;
4781 struct SHARE_INFO_2 i2;
4783 if ((argc < 2) || (argc > 3)) {
4784 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
4786 return NT_STATUS_INVALID_PARAMETER;
4789 i2.shi2_netname = argv[0];
4790 i2.shi2_type = STYPE_DISKTREE;
4791 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4792 i2.shi2_permissions = 0;
4793 i2.shi2_max_uses = 0;
4794 i2.shi2_current_uses = 0;
4795 i2.shi2_path = argv[1];
4796 i2.shi2_passwd = NULL;
4798 status = NetShareAdd(pipe_hnd->desthost,
4803 return werror_to_ntstatus(W_ERROR(status));
4806 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4807 TALLOC_CTX *mem_ctx,
4808 struct rpc_sh_ctx *ctx,
4809 struct rpc_pipe_client *pipe_hnd,
4810 int argc, const char **argv)
4813 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4814 return NT_STATUS_INVALID_PARAMETER;
4817 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4820 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4821 TALLOC_CTX *mem_ctx,
4822 struct rpc_sh_ctx *ctx,
4823 struct rpc_pipe_client *pipe_hnd,
4824 int argc, const char **argv)
4826 union srvsvc_NetShareInfo info;
4831 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4832 return NT_STATUS_INVALID_PARAMETER;
4835 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4841 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4845 d_printf(_("Name: %s\n"), info.info2->name);
4846 d_printf(_("Comment: %s\n"), info.info2->comment);
4847 d_printf(_("Path: %s\n"), info.info2->path);
4848 d_printf(_("Password: %s\n"), info.info2->password);
4851 return werror_to_ntstatus(result);
4854 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4855 struct rpc_sh_ctx *ctx)
4857 static struct rpc_sh_cmd cmds[] = {
4859 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4860 N_("List available shares") },
4862 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4863 N_("Add a share") },
4865 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4866 N_("Delete a share") },
4868 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4869 N_("Get information about a share") },
4871 { NULL, NULL, 0, NULL, NULL }
4877 /****************************************************************************/
4879 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4881 return net_file_usage(c, argc, argv);
4885 * Close a file on a remote RPC server.
4887 * @param argc Standard main() style argc.
4888 * @param argv Standard main() style argv. Initial components are already
4891 * @return A shell status integer (0 for success).
4893 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4895 if (argc < 1 || c->display_usage) {
4896 return rpc_file_usage(c, argc, argv);
4899 return NetFileClose(c->opt_host, atoi(argv[0]));
4903 * Formatted print of open file info
4905 * @param r struct FILE_INFO_3 contents
4908 static void display_file_info_3(struct FILE_INFO_3 *r)
4910 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4911 r->fi3_id, r->fi3_username, r->fi3_permissions,
4912 r->fi3_num_locks, r->fi3_pathname);
4916 * List files for a user on a remote RPC server.
4918 * @param argc Standard main() style argc.
4919 * @param argv Standard main() style argv. Initial components are already
4922 * @return A shell status integer (0 for success)..
4925 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4927 NET_API_STATUS status;
4928 uint32 preferred_len = 0xffffffff, i;
4929 const char *username=NULL;
4930 uint32_t total_entries = 0;
4931 uint32_t entries_read = 0;
4932 uint32_t resume_handle = 0;
4933 struct FILE_INFO_3 *i3 = NULL;
4935 if (c->display_usage) {
4936 return rpc_file_usage(c, argc, argv);
4939 /* if argc > 0, must be user command */
4941 username = smb_xstrdup(argv[0]);
4944 status = NetFileEnum(c->opt_host,
4948 (uint8_t **)(void *)&i3,
4958 /* Display results */
4961 "\nEnumerating open files on remote server:\n\n"
4962 "\nFileId Opened by Perms Locks Path"
4963 "\n------ --------- ----- ----- ---- \n"));
4964 for (i = 0; i < entries_read; i++) {
4965 display_file_info_3(&i3[i]);
4972 * 'net rpc file' entrypoint.
4973 * @param argc Standard main() style argc.
4974 * @param argv Standard main() style argv. Initial components are already
4978 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4980 NET_API_STATUS status;
4982 struct functable func[] = {
4987 N_("Close opened file"),
4988 N_("net rpc file close\n"
4989 " Close opened file")
4995 N_("List files opened by user"),
4996 N_("net rpc file user\n"
4997 " List files opened by user")
5004 N_("Display information about opened file"),
5005 N_("net rpc file info\n"
5006 " Display information about opened file")
5009 {NULL, NULL, 0, NULL, NULL}
5012 status = libnetapi_init(&c->netapi_ctx);
5016 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5017 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5018 if (c->opt_kerberos) {
5019 libnetapi_set_use_kerberos(c->netapi_ctx);
5023 if (c->display_usage) {
5024 d_printf(_("Usage:\n"));
5025 d_printf(_("net rpc file\n"
5026 " List opened files\n"));
5027 net_display_usage_from_functable(func);
5031 return rpc_file_user(c, argc, argv);
5034 return net_run_function(c, argc, argv, "net rpc file", func);
5038 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5040 * All parameters are provided by the run_rpc_command function, except for
5041 * argc, argv which are passed through.
5043 * @param c A net_context structure.
5044 * @param domain_sid The domain sid acquired from the remote server.
5045 * @param cli A cli_state connected to the server.
5046 * @param mem_ctx Talloc context, destroyed on completion of the function.
5047 * @param argc Standard main() style argc.
5048 * @param argv Standard main() style argv. Initial components are already
5051 * @return Normal NTSTATUS return.
5054 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5055 const DOM_SID *domain_sid,
5056 const char *domain_name,
5057 struct cli_state *cli,
5058 struct rpc_pipe_client *pipe_hnd,
5059 TALLOC_CTX *mem_ctx,
5063 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5065 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5067 if (NT_STATUS_IS_OK(result)) {
5068 d_printf(_("\nShutdown successfully aborted\n"));
5069 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5071 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5077 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5079 * All parameters are provided by the run_rpc_command function, except for
5080 * argc, argv which are passed through.
5082 * @param c A net_context structure.
5083 * @param domain_sid The domain sid acquired from the remote server.
5084 * @param cli A cli_state connected to the server.
5085 * @param mem_ctx Talloc context, destroyed on completion of the function.
5086 * @param argc Standard main() style argc.
5087 * @param argv Standard main() style argv. Initial components are already
5090 * @return Normal NTSTATUS return.
5093 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5094 const DOM_SID *domain_sid,
5095 const char *domain_name,
5096 struct cli_state *cli,
5097 struct rpc_pipe_client *pipe_hnd,
5098 TALLOC_CTX *mem_ctx,
5102 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5104 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5106 if (NT_STATUS_IS_OK(result)) {
5107 d_printf(_("\nShutdown successfully aborted\n"));
5108 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5110 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5116 * ABORT the shutdown of a remote RPC server.
5118 * @param argc Standard main() style argc.
5119 * @param argv Standard main() style argv. Initial components are already
5122 * @return A shell status integer (0 for success).
5125 static int rpc_shutdown_abort(struct net_context *c, int argc,
5130 if (c->display_usage) {
5132 "net rpc abortshutdown\n"
5135 _("Abort a scheduled shutdown"));
5139 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5140 rpc_shutdown_abort_internals, argc, argv);
5145 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5147 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5148 rpc_reg_shutdown_abort_internals,
5153 * Shut down a remote RPC Server via initshutdown pipe.
5155 * All parameters are provided by the run_rpc_command function, except for
5156 * argc, argv which are passed through.
5158 * @param c A net_context structure.
5159 * @param domain_sid The domain sid acquired from the remote server.
5160 * @param cli A cli_state connected to the server.
5161 * @param mem_ctx Talloc context, destroyed on completion of the function.
5162 * @param argc Standard main() style argc.
5163 * @param argv Standard main() style argv. Initial components are already
5166 * @return Normal NTSTATUS return.
5169 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5170 const DOM_SID *domain_sid,
5171 const char *domain_name,
5172 struct cli_state *cli,
5173 struct rpc_pipe_client *pipe_hnd,
5174 TALLOC_CTX *mem_ctx,
5178 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5179 const char *msg = N_("This machine will be shutdown shortly");
5180 uint32 timeout = 20;
5181 struct lsa_StringLarge msg_string;
5183 if (c->opt_comment) {
5184 msg = c->opt_comment;
5186 if (c->opt_timeout) {
5187 timeout = c->opt_timeout;
5190 msg_string.string = msg;
5192 /* create an entry */
5193 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5194 &msg_string, timeout, c->opt_force, c->opt_reboot,
5197 if (NT_STATUS_IS_OK(result)) {
5198 d_printf(_("\nShutdown of remote machine succeeded\n"));
5199 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5201 DEBUG(1,("Shutdown of remote machine failed!\n"));
5207 * Shut down a remote RPC Server via winreg pipe.
5209 * All parameters are provided by the run_rpc_command function, except for
5210 * argc, argv which are passed through.
5212 * @param c A net_context structure.
5213 * @param domain_sid The domain sid acquired from the remote server.
5214 * @param cli A cli_state connected to the server.
5215 * @param mem_ctx Talloc context, destroyed on completion of the function.
5216 * @param argc Standard main() style argc.
5217 * @param argv Standard main() style argv. Initial components are already
5220 * @return Normal NTSTATUS return.
5223 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5224 const DOM_SID *domain_sid,
5225 const char *domain_name,
5226 struct cli_state *cli,
5227 struct rpc_pipe_client *pipe_hnd,
5228 TALLOC_CTX *mem_ctx,
5232 const char *msg = N_("This machine will be shutdown shortly");
5233 uint32 timeout = 20;
5234 struct lsa_StringLarge msg_string;
5238 if (c->opt_comment) {
5239 msg = c->opt_comment;
5241 msg_string.string = msg;
5243 if (c->opt_timeout) {
5244 timeout = c->opt_timeout;
5247 /* create an entry */
5248 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5249 &msg_string, timeout, c->opt_force, c->opt_reboot,
5252 if (NT_STATUS_IS_OK(result)) {
5253 d_printf(_("\nShutdown of remote machine succeeded\n"));
5255 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5256 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5257 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5259 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5266 * Shut down a remote RPC server.
5268 * @param argc Standard main() style argc.
5269 * @param argv Standard main() style argv. Initial components are already
5272 * @return A shell status integer (0 for success).
5275 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5279 if (c->display_usage) {
5281 "net rpc shutdown\n"
5284 _("Shut down a remote RPC server"));
5288 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5289 rpc_init_shutdown_internals, argc, argv);
5292 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5293 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5294 rpc_reg_shutdown_internals, argc, argv);
5300 /***************************************************************************
5301 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5302 ***************************************************************************/
5305 * Add interdomain trust account to the RPC server.
5306 * All parameters (except for argc and argv) are passed by run_rpc_command
5309 * @param c A net_context structure.
5310 * @param domain_sid The domain sid acquired from the server.
5311 * @param cli A cli_state connected to the server.
5312 * @param mem_ctx Talloc context, destroyed on completion of the function.
5313 * @param argc Standard main() style argc.
5314 * @param argv Standard main() style argv. Initial components are already
5317 * @return normal NTSTATUS return code.
5320 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5321 const DOM_SID *domain_sid,
5322 const char *domain_name,
5323 struct cli_state *cli,
5324 struct rpc_pipe_client *pipe_hnd,
5325 TALLOC_CTX *mem_ctx,
5329 struct policy_handle connect_pol, domain_pol, user_pol;
5330 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5332 struct lsa_String lsa_acct_name;
5334 uint32 acct_flags=0;
5336 uint32_t access_granted = 0;
5337 union samr_UserInfo info;
5338 unsigned int orig_timeout;
5343 _(" net rpc trustdom add <domain_name> "
5344 "<trust password>\n"));
5345 return NT_STATUS_INVALID_PARAMETER;
5349 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5352 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5353 return NT_STATUS_NO_MEMORY;
5356 strupper_m(acct_name);
5358 init_lsa_String(&lsa_acct_name, acct_name);
5360 /* Get samr policy handle */
5361 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5363 MAXIMUM_ALLOWED_ACCESS,
5365 if (!NT_STATUS_IS_OK(result)) {
5369 /* Get domain policy handle */
5370 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5372 MAXIMUM_ALLOWED_ACCESS,
5373 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5375 if (!NT_STATUS_IS_OK(result)) {
5379 /* This call can take a long time - allow the server to time out.
5380 * 35 seconds should do it. */
5382 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5384 /* Create trusting domain's account */
5385 acb_info = ACB_NORMAL;
5386 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5387 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5388 SAMR_USER_ACCESS_SET_PASSWORD |
5389 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5390 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5392 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5401 /* And restore our original timeout. */
5402 rpccli_set_timeout(pipe_hnd, orig_timeout);
5404 if (!NT_STATUS_IS_OK(result)) {
5405 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5406 acct_name, nt_errstr(result));
5411 struct samr_CryptPassword crypt_pwd;
5413 ZERO_STRUCT(info.info23);
5415 init_samr_CryptPassword(argv[1],
5416 &cli->user_session_key,
5419 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5420 SAMR_FIELD_NT_PASSWORD_PRESENT;
5421 info.info23.info.acct_flags = ACB_DOMTRUST;
5422 info.info23.password = crypt_pwd;
5424 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5429 if (!NT_STATUS_IS_OK(result)) {
5430 DEBUG(0,("Could not set trust account password: %s\n",
5431 nt_errstr(result)));
5437 SAFE_FREE(acct_name);
5442 * Create interdomain trust account for a remote domain.
5444 * @param argc Standard argc.
5445 * @param argv Standard argv without initial components.
5447 * @return Integer status (0 means success).
5450 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5452 if (argc > 0 && !c->display_usage) {
5453 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5454 rpc_trustdom_add_internals, argc, argv);
5458 _("net rpc trustdom add <domain_name> <trust "
5466 * Remove interdomain trust account from the RPC server.
5467 * All parameters (except for argc and argv) are passed by run_rpc_command
5470 * @param c A net_context structure.
5471 * @param domain_sid The domain sid acquired from the server.
5472 * @param cli A cli_state connected to the server.
5473 * @param mem_ctx Talloc context, destroyed on completion of the function.
5474 * @param argc Standard main() style argc.
5475 * @param argv Standard main() style argv. Initial components are already
5478 * @return normal NTSTATUS return code.
5481 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5482 const DOM_SID *domain_sid,
5483 const char *domain_name,
5484 struct cli_state *cli,
5485 struct rpc_pipe_client *pipe_hnd,
5486 TALLOC_CTX *mem_ctx,
5490 struct policy_handle connect_pol, domain_pol, user_pol;
5491 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5493 DOM_SID trust_acct_sid;
5494 struct samr_Ids user_rids, name_types;
5495 struct lsa_String lsa_acct_name;
5500 _(" net rpc trustdom del <domain_name>\n"));
5501 return NT_STATUS_INVALID_PARAMETER;
5505 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5507 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5509 if (acct_name == NULL)
5510 return NT_STATUS_NO_MEMORY;
5512 strupper_m(acct_name);
5514 /* Get samr policy handle */
5515 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5517 MAXIMUM_ALLOWED_ACCESS,
5519 if (!NT_STATUS_IS_OK(result)) {
5523 /* Get domain policy handle */
5524 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5526 MAXIMUM_ALLOWED_ACCESS,
5527 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5529 if (!NT_STATUS_IS_OK(result)) {
5533 init_lsa_String(&lsa_acct_name, acct_name);
5535 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5542 if (!NT_STATUS_IS_OK(result)) {
5543 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5545 acct_name, nt_errstr(result) );
5549 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5551 MAXIMUM_ALLOWED_ACCESS,
5555 if (!NT_STATUS_IS_OK(result)) {
5556 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5558 acct_name, nt_errstr(result) );
5562 /* append the rid to the domain sid */
5563 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
5567 /* remove the sid */
5569 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5572 if (!NT_STATUS_IS_OK(result)) {
5573 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5574 " on user %s failed %s\n"),
5575 acct_name, nt_errstr(result) );
5581 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5584 if (!NT_STATUS_IS_OK(result)) {
5585 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5587 acct_name, nt_errstr(result) );
5591 if (!NT_STATUS_IS_OK(result)) {
5592 d_printf(_("Could not set trust account password: %s\n"),
5602 * Delete interdomain trust account for a remote domain.
5604 * @param argc Standard argc.
5605 * @param argv Standard argv without initial components.
5607 * @return Integer status (0 means success).
5610 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5612 if (argc > 0 && !c->display_usage) {
5613 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5614 rpc_trustdom_del_internals, argc, argv);
5618 _("net rpc trustdom del <domain>\n"));
5623 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5624 struct cli_state *cli,
5625 TALLOC_CTX *mem_ctx,
5626 const char *domain_name)
5628 char *dc_name = NULL;
5629 const char *buffer = NULL;
5630 struct rpc_pipe_client *netr;
5633 /* Use NetServerEnum2 */
5635 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5637 return NT_STATUS_OK;
5640 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5641 for domain %s\n", domain_name));
5643 /* Try netr_GetDcName */
5645 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5647 if (!NT_STATUS_IS_OK(status)) {
5651 status = rpccli_netr_GetDcName(netr, mem_ctx,
5658 if (NT_STATUS_IS_OK(status)) {
5662 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5663 for domain %s\n", domain_name));
5669 * Establish trust relationship to a trusting domain.
5670 * Interdomain account must already be created on remote PDC.
5672 * @param c A net_context structure.
5673 * @param argc Standard argc.
5674 * @param argv Standard argv without initial components.
5676 * @return Integer status (0 means success).
5679 static int rpc_trustdom_establish(struct net_context *c, int argc,
5682 struct cli_state *cli = NULL;
5683 struct sockaddr_storage server_ss;
5684 struct rpc_pipe_client *pipe_hnd = NULL;
5685 struct policy_handle connect_hnd;
5686 TALLOC_CTX *mem_ctx;
5688 DOM_SID *domain_sid;
5693 union lsa_PolicyInformation *info = NULL;
5696 * Connect to \\server\ipc$ as 'our domain' account with password
5699 if (argc != 1 || c->display_usage) {
5702 _("net rpc trustdom establish <domain_name>\n"));
5706 domain_name = smb_xstrdup(argv[0]);
5707 strupper_m(domain_name);
5709 /* account name used at first is our domain's name with '$' */
5710 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5713 strupper_m(acct_name);
5716 * opt_workgroup will be used by connection functions further,
5717 * hence it should be set to remote domain name instead of ours
5719 if (c->opt_workgroup) {
5720 c->opt_workgroup = smb_xstrdup(domain_name);
5723 c->opt_user_name = acct_name;
5725 /* find the domain controller */
5726 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5727 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5731 /* connect to ipc$ as username/password */
5732 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5733 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5735 /* Is it trusting domain account for sure ? */
5736 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5737 nt_errstr(nt_status)));
5741 /* store who we connected to */
5743 saf_store( domain_name, pdc_name );
5746 * Connect to \\server\ipc$ again (this time anonymously)
5749 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5752 if (NT_STATUS_IS_ERR(nt_status)) {
5753 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5754 domain_name, nt_errstr(nt_status)));
5758 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5759 "domain %s", domain_name))) {
5760 DEBUG(0, ("talloc_init() failed\n"));
5765 /* Make sure we're talking to a proper server */
5767 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5768 if (!NT_STATUS_IS_OK(nt_status)) {
5770 talloc_destroy(mem_ctx);
5775 * Call LsaOpenPolicy and LsaQueryInfo
5778 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5780 if (!NT_STATUS_IS_OK(nt_status)) {
5781 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5783 talloc_destroy(mem_ctx);
5787 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5789 if (NT_STATUS_IS_ERR(nt_status)) {
5790 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5791 nt_errstr(nt_status)));
5793 talloc_destroy(mem_ctx);
5797 /* Querying info level 5 */
5799 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5801 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5803 if (NT_STATUS_IS_ERR(nt_status)) {
5804 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5805 nt_errstr(nt_status)));
5807 talloc_destroy(mem_ctx);
5811 domain_sid = info->account_domain.sid;
5813 /* There should be actually query info level 3 (following nt serv behaviour),
5814 but I still don't know if it's _really_ necessary */
5817 * Store the password in secrets db
5820 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5821 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5823 talloc_destroy(mem_ctx);
5828 * Close the pipes and clean up
5831 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5832 if (NT_STATUS_IS_ERR(nt_status)) {
5833 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5834 nt_errstr(nt_status)));
5836 talloc_destroy(mem_ctx);
5842 talloc_destroy(mem_ctx);
5844 d_printf(_("Trust to domain %s established\n"), domain_name);
5849 * Revoke trust relationship to the remote domain.
5851 * @param c A net_context structure.
5852 * @param argc Standard argc.
5853 * @param argv Standard argv without initial components.
5855 * @return Integer status (0 means success).
5858 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5864 if (argc < 1 || c->display_usage) {
5867 _("net rpc trustdom revoke <domain_name>\n"
5868 " Revoke trust relationship\n"
5869 " domain_name\tName of domain to revoke trust\n"));
5873 /* generate upper cased domain name */
5874 domain_name = smb_xstrdup(argv[0]);
5875 strupper_m(domain_name);
5877 /* delete password of the trust */
5878 if (!pdb_del_trusteddom_pw(domain_name)) {
5879 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5886 SAFE_FREE(domain_name);
5890 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5891 const DOM_SID *domain_sid,
5892 const char *domain_name,
5893 struct cli_state *cli,
5894 struct rpc_pipe_client *pipe_hnd,
5895 TALLOC_CTX *mem_ctx,
5900 if (!sid_to_fstring(str_sid, domain_sid)) {
5901 return NT_STATUS_UNSUCCESSFUL;
5903 d_printf("%s\n", str_sid);
5904 return NT_STATUS_OK;
5907 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5911 /* convert sid into ascii string */
5912 sid_to_fstring(ascii_sid, dom_sid);
5914 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5917 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5918 TALLOC_CTX *mem_ctx,
5919 struct policy_handle *pol,
5921 const char *trusted_dom_name)
5924 union lsa_TrustedDomainInfo *info = NULL;
5925 char *cleartextpwd = NULL;
5926 uint8_t session_key[16];
5927 DATA_BLOB session_key_blob;
5928 DATA_BLOB data = data_blob_null;
5930 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5933 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5935 if (NT_STATUS_IS_ERR(nt_status)) {
5936 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5937 nt_errstr(nt_status)));
5941 data = data_blob(info->password.password->data,
5942 info->password.password->length);
5944 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5945 DEBUG(0, ("Could not retrieve password hash\n"));
5949 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5950 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5952 if (cleartextpwd == NULL) {
5953 DEBUG(0,("retrieved NULL password\n"));
5954 nt_status = NT_STATUS_UNSUCCESSFUL;
5958 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5959 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5960 nt_status = NT_STATUS_UNSUCCESSFUL;
5964 #ifdef DEBUG_PASSWORD
5965 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5966 "password: [%s]\n", trusted_dom_name,
5967 sid_string_dbg(&dom_sid), cleartextpwd));
5971 SAFE_FREE(cleartextpwd);
5972 data_blob_free(&data);
5977 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5980 /* common variables */
5981 TALLOC_CTX* mem_ctx;
5982 struct cli_state *cli = NULL;
5983 struct rpc_pipe_client *pipe_hnd = NULL;
5985 const char *domain_name = NULL;
5986 DOM_SID *queried_dom_sid;
5987 struct policy_handle connect_hnd;
5988 union lsa_PolicyInformation *info = NULL;
5990 /* trusted domains listing variables */
5991 unsigned int enum_ctx = 0;
5993 struct lsa_DomainList dom_list;
5996 if (c->display_usage) {
5998 "net rpc trustdom vampire\n"
6001 _("Vampire trust relationship from remote server"));
6006 * Listing trusted domains (stored in secrets.tdb, if local)
6009 mem_ctx = talloc_init("trust relationships vampire");
6012 * set domain and pdc name to local samba server (default)
6013 * or to remote one given in command line
6016 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6017 domain_name = c->opt_workgroup;
6018 c->opt_target_workgroup = c->opt_workgroup;
6020 fstrcpy(pdc_name, global_myname());
6021 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6022 c->opt_target_workgroup = domain_name;
6025 /* open \PIPE\lsarpc and open policy handle */
6026 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6027 if (!NT_STATUS_IS_OK(nt_status)) {
6028 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6029 nt_errstr(nt_status)));
6030 talloc_destroy(mem_ctx);
6034 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6036 if (!NT_STATUS_IS_OK(nt_status)) {
6037 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6038 nt_errstr(nt_status) ));
6040 talloc_destroy(mem_ctx);
6044 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6046 if (NT_STATUS_IS_ERR(nt_status)) {
6047 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6048 nt_errstr(nt_status)));
6050 talloc_destroy(mem_ctx);
6054 /* query info level 5 to obtain sid of a domain being queried */
6055 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6057 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6060 if (NT_STATUS_IS_ERR(nt_status)) {
6061 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6062 nt_errstr(nt_status)));
6064 talloc_destroy(mem_ctx);
6068 queried_dom_sid = info->account_domain.sid;
6071 * Keep calling LsaEnumTrustdom over opened pipe until
6072 * the end of enumeration is reached
6075 d_printf(_("Vampire trusted domains:\n\n"));
6078 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6083 if (NT_STATUS_IS_ERR(nt_status)) {
6084 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6085 nt_errstr(nt_status)));
6087 talloc_destroy(mem_ctx);
6091 for (i = 0; i < dom_list.count; i++) {
6093 print_trusted_domain(dom_list.domains[i].sid,
6094 dom_list.domains[i].name.string);
6096 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6097 *dom_list.domains[i].sid,
6098 dom_list.domains[i].name.string);
6099 if (!NT_STATUS_IS_OK(nt_status)) {
6101 talloc_destroy(mem_ctx);
6107 * in case of no trusted domains say something rather
6108 * than just display blank line
6110 if (!dom_list.count) d_printf(_("none\n"));
6112 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6114 /* close this connection before doing next one */
6115 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6116 if (NT_STATUS_IS_ERR(nt_status)) {
6117 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6118 nt_errstr(nt_status)));
6120 talloc_destroy(mem_ctx);
6124 /* close lsarpc pipe and connection to IPC$ */
6127 talloc_destroy(mem_ctx);
6131 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6133 /* common variables */
6134 TALLOC_CTX* mem_ctx;
6135 struct cli_state *cli = NULL, *remote_cli = NULL;
6136 struct rpc_pipe_client *pipe_hnd = NULL;
6138 const char *domain_name = NULL;
6139 DOM_SID *queried_dom_sid;
6140 int ascii_dom_name_len;
6141 struct policy_handle connect_hnd;
6142 union lsa_PolicyInformation *info = NULL;
6144 /* trusted domains listing variables */
6145 unsigned int num_domains, enum_ctx = 0;
6147 struct lsa_DomainList dom_list;
6151 /* trusting domains listing variables */
6152 struct policy_handle domain_hnd;
6153 struct samr_SamArray *trusts = NULL;
6155 if (c->display_usage) {
6157 "net rpc trustdom list\n"
6160 _("List incoming and outgoing trust relationships"));
6165 * Listing trusted domains (stored in secrets.tdb, if local)
6168 mem_ctx = talloc_init("trust relationships listing");
6171 * set domain and pdc name to local samba server (default)
6172 * or to remote one given in command line
6175 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6176 domain_name = c->opt_workgroup;
6177 c->opt_target_workgroup = c->opt_workgroup;
6179 fstrcpy(pdc_name, global_myname());
6180 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6181 c->opt_target_workgroup = domain_name;
6184 /* open \PIPE\lsarpc and open policy handle */
6185 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6186 if (!NT_STATUS_IS_OK(nt_status)) {
6187 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6188 nt_errstr(nt_status)));
6189 talloc_destroy(mem_ctx);
6193 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6195 if (!NT_STATUS_IS_OK(nt_status)) {
6196 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6197 nt_errstr(nt_status) ));
6199 talloc_destroy(mem_ctx);
6203 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6205 if (NT_STATUS_IS_ERR(nt_status)) {
6206 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6207 nt_errstr(nt_status)));
6209 talloc_destroy(mem_ctx);
6213 /* query info level 5 to obtain sid of a domain being queried */
6214 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6216 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6219 if (NT_STATUS_IS_ERR(nt_status)) {
6220 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6221 nt_errstr(nt_status)));
6223 talloc_destroy(mem_ctx);
6227 queried_dom_sid = info->account_domain.sid;
6230 * Keep calling LsaEnumTrustdom over opened pipe until
6231 * the end of enumeration is reached
6234 d_printf(_("Trusted domains list:\n\n"));
6236 found_domain = false;
6239 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6244 if (NT_STATUS_IS_ERR(nt_status)) {
6245 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6246 nt_errstr(nt_status)));
6248 talloc_destroy(mem_ctx);
6252 for (i = 0; i < dom_list.count; i++) {
6253 print_trusted_domain(dom_list.domains[i].sid,
6254 dom_list.domains[i].name.string);
6255 found_domain = true;
6259 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6262 * in case of no trusted domains say something rather
6263 * than just display blank line
6265 if (!found_domain) {
6266 d_printf(_("none\n"));
6269 /* close this connection before doing next one */
6270 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6271 if (NT_STATUS_IS_ERR(nt_status)) {
6272 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6273 nt_errstr(nt_status)));
6275 talloc_destroy(mem_ctx);
6279 TALLOC_FREE(pipe_hnd);
6282 * Listing trusting domains (stored in passdb backend, if local)
6285 d_printf(_("\nTrusting domains list:\n\n"));
6288 * Open \PIPE\samr and get needed policy handles
6290 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6292 if (!NT_STATUS_IS_OK(nt_status)) {
6293 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6295 talloc_destroy(mem_ctx);
6300 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6302 SAMR_ACCESS_LOOKUP_DOMAIN,
6304 if (!NT_STATUS_IS_OK(nt_status)) {
6305 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6306 nt_errstr(nt_status)));
6308 talloc_destroy(mem_ctx);
6312 /* SamrOpenDomain - we have to open domain policy handle in order to be
6313 able to enumerate accounts*/
6314 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6316 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6319 if (!NT_STATUS_IS_OK(nt_status)) {
6320 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6321 nt_errstr(nt_status)));
6323 talloc_destroy(mem_ctx);
6328 * perform actual enumeration
6331 found_domain = false;
6333 enum_ctx = 0; /* reset enumeration context from last enumeration */
6336 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6343 if (NT_STATUS_IS_ERR(nt_status)) {
6344 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6345 nt_errstr(nt_status)));
6347 talloc_destroy(mem_ctx);
6351 for (i = 0; i < num_domains; i++) {
6353 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6355 found_domain = true;
6358 * get each single domain's sid (do we _really_ need this ?):
6359 * 1) connect to domain's pdc
6360 * 2) query the pdc for domain's sid
6363 /* get rid of '$' tail */
6364 ascii_dom_name_len = strlen(str);
6365 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6366 str[ascii_dom_name_len - 1] = '\0';
6368 /* set opt_* variables to remote domain */
6370 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6371 c->opt_target_workgroup = c->opt_workgroup;
6373 d_printf("%-20s", str);
6375 /* connect to remote domain controller */
6376 nt_status = net_make_ipc_connection(c,
6377 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6379 if (NT_STATUS_IS_OK(nt_status)) {
6380 /* query for domain's sid */
6381 if (run_rpc_command(
6383 &ndr_table_lsarpc.syntax_id, 0,
6384 rpc_query_domain_sid, argc,
6386 d_printf(_("strange - couldn't get domain's sid\n"));
6388 cli_shutdown(remote_cli);
6391 d_fprintf(stderr, _("domain controller is not "
6392 "responding: %s\n"),
6393 nt_errstr(nt_status));
6394 d_printf(_("couldn't get domain's sid\n"));
6398 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6400 if (!found_domain) {
6404 /* close opened samr and domain policy handles */
6405 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6406 if (!NT_STATUS_IS_OK(nt_status)) {
6407 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6410 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6411 if (!NT_STATUS_IS_OK(nt_status)) {
6412 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6415 /* close samr pipe and connection to IPC$ */
6418 talloc_destroy(mem_ctx);
6423 * Entrypoint for 'net rpc trustdom' code.
6425 * @param argc Standard argc.
6426 * @param argv Standard argv without initial components.
6428 * @return Integer status (0 means success).
6431 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6433 struct functable func[] = {
6438 N_("Add trusting domain's account"),
6439 N_("net rpc trustdom add\n"
6440 " Add trusting domain's account")
6446 N_("Remove trusting domain's account"),
6447 N_("net rpc trustdom del\n"
6448 " Remove trusting domain's account")
6452 rpc_trustdom_establish,
6454 N_("Establish outgoing trust relationship"),
6455 N_("net rpc trustdom establish\n"
6456 " Establish outgoing trust relationship")
6460 rpc_trustdom_revoke,
6462 N_("Revoke outgoing trust relationship"),
6463 N_("net rpc trustdom revoke\n"
6464 " Revoke outgoing trust relationship")
6470 N_("List in- and outgoing domain trusts"),
6471 N_("net rpc trustdom list\n"
6472 " List in- and outgoing domain trusts")
6476 rpc_trustdom_vampire,
6478 N_("Vampire trusts from remote server"),
6479 N_("net rpc trustdom vampire\n"
6480 " Vampire trusts from remote server")
6482 {NULL, NULL, 0, NULL, NULL}
6485 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6489 * Check if a server will take rpc commands
6490 * @param flags Type of server to connect to (PDC, DMB, localhost)
6491 * if the host is not explicitly specified
6492 * @return bool (true means rpc supported)
6494 bool net_rpc_check(struct net_context *c, unsigned flags)
6496 struct cli_state *cli;
6498 struct sockaddr_storage server_ss;
6499 char *server_name = NULL;
6502 /* flags (i.e. server type) may depend on command */
6503 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6506 if ((cli = cli_initialise()) == NULL) {
6510 status = cli_connect(cli, server_name, &server_ss);
6511 if (!NT_STATUS_IS_OK(status))
6513 if (!attempt_netbios_session_request(&cli, global_myname(),
6514 server_name, &server_ss))
6516 status = cli_negprot(cli);
6517 if (!NT_STATUS_IS_OK(status))
6519 if (cli->protocol < PROTOCOL_NT1)
6528 /* dump sam database via samsync rpc calls */
6529 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6530 if (c->display_usage) {
6535 _("Dump remote SAM database"));
6539 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6540 NET_FLAGS_ANONYMOUS,
6541 rpc_samdump_internals, argc, argv);
6544 /* syncronise sam database via samsync rpc calls */
6545 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6547 struct functable func[] = {
6552 N_("Dump remote SAM database to ldif"),
6553 N_("net rpc vampire ldif\n"
6554 " Dump remote SAM database to LDIF file or "
6561 N_("Dump remote SAM database to Kerberos Keytab"),
6562 N_("net rpc vampire keytab\n"
6563 " Dump remote SAM database to Kerberos keytab "
6570 N_("Dump remote SAM database to passdb"),
6571 N_("net rpc vampire passdb\n"
6572 " Dump remote SAM database to passdb")
6575 {NULL, NULL, 0, NULL, NULL}
6579 if (c->display_usage) {
6584 _("Vampire remote SAM database"));
6588 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6589 NET_FLAGS_ANONYMOUS,
6590 rpc_vampire_internals,
6594 return net_run_function(c, argc, argv, "net rpc vampire", func);
6598 * Migrate everything from a print server.
6600 * @param c A net_context structure.
6601 * @param argc Standard main() style argc.
6602 * @param argv Standard main() style argv. Initial components are already
6605 * @return A shell status integer (0 for success).
6607 * The order is important !
6608 * To successfully add drivers the print queues have to exist !
6609 * Applying ACLs should be the last step, because you're easily locked out.
6612 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6617 if (c->display_usage) {
6619 "net rpc printer migrate all\n"
6622 _("Migrate everything from a print server"));
6627 d_printf(_("no server to migrate\n"));
6631 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6632 rpc_printer_migrate_printers_internals, argc,
6637 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6638 rpc_printer_migrate_drivers_internals, argc,
6643 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6644 rpc_printer_migrate_forms_internals, argc, argv);
6648 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6649 rpc_printer_migrate_settings_internals, argc,
6654 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6655 rpc_printer_migrate_security_internals, argc,
6661 * Migrate print drivers from a print server.
6663 * @param c A net_context structure.
6664 * @param argc Standard main() style argc.
6665 * @param argv Standard main() style argv. Initial components are already
6668 * @return A shell status integer (0 for success).
6670 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6673 if (c->display_usage) {
6675 "net rpc printer migrate drivers\n"
6678 _("Migrate print-drivers from a print-server"));
6683 d_printf(_("no server to migrate\n"));
6687 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6688 rpc_printer_migrate_drivers_internals,
6693 * Migrate print-forms from a print-server.
6695 * @param c A net_context structure.
6696 * @param argc Standard main() style argc.
6697 * @param argv Standard main() style argv. Initial components are already
6700 * @return A shell status integer (0 for success).
6702 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6705 if (c->display_usage) {
6707 "net rpc printer migrate forms\n"
6710 _("Migrate print-forms from a print-server"));
6715 d_printf(_("no server to migrate\n"));
6719 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6720 rpc_printer_migrate_forms_internals,
6725 * Migrate printers from a print-server.
6727 * @param c A net_context structure.
6728 * @param argc Standard main() style argc.
6729 * @param argv Standard main() style argv. Initial components are already
6732 * @return A shell status integer (0 for success).
6734 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6737 if (c->display_usage) {
6739 "net rpc printer migrate printers\n"
6742 _("Migrate printers from a print-server"));
6747 d_printf(_("no server to migrate\n"));
6751 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6752 rpc_printer_migrate_printers_internals,
6757 * Migrate printer-ACLs from a print-server
6759 * @param c A net_context structure.
6760 * @param argc Standard main() style argc.
6761 * @param argv Standard main() style argv. Initial components are already
6764 * @return A shell status integer (0 for success).
6766 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6769 if (c->display_usage) {
6771 "net rpc printer migrate security\n"
6774 _("Migrate printer-ACLs from a print-server"));
6779 d_printf(_("no server to migrate\n"));
6783 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6784 rpc_printer_migrate_security_internals,
6789 * Migrate printer-settings from a print-server.
6791 * @param c A net_context structure.
6792 * @param argc Standard main() style argc.
6793 * @param argv Standard main() style argv. Initial components are already
6796 * @return A shell status integer (0 for success).
6798 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6801 if (c->display_usage) {
6803 "net rpc printer migrate settings\n"
6806 _("Migrate printer-settings from a "
6812 d_printf(_("no server to migrate\n"));
6816 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6817 rpc_printer_migrate_settings_internals,
6822 * 'net rpc printer' entrypoint.
6824 * @param c A net_context structure.
6825 * @param argc Standard main() style argc.
6826 * @param argv Standard main() style argv. Initial components are already
6830 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6833 /* ouch: when addriver and setdriver are called from within
6834 rpc_printer_migrate_drivers_internals, the printer-queue already
6837 struct functable func[] = {
6840 rpc_printer_migrate_all,
6842 N_("Migrate all from remote to local print server"),
6843 N_("net rpc printer migrate all\n"
6844 " Migrate all from remote to local print server")
6848 rpc_printer_migrate_drivers,
6850 N_("Migrate drivers to local server"),
6851 N_("net rpc printer migrate drivers\n"
6852 " Migrate drivers to local server")
6856 rpc_printer_migrate_forms,
6858 N_("Migrate froms to local server"),
6859 N_("net rpc printer migrate forms\n"
6860 " Migrate froms to local server")
6864 rpc_printer_migrate_printers,
6866 N_("Migrate printers to local server"),
6867 N_("net rpc printer migrate printers\n"
6868 " Migrate printers to local server")
6872 rpc_printer_migrate_security,
6874 N_("Mirgate printer ACLs to local server"),
6875 N_("net rpc printer migrate security\n"
6876 " Mirgate printer ACLs to local server")
6880 rpc_printer_migrate_settings,
6882 N_("Migrate printer settings to local server"),
6883 N_("net rpc printer migrate settings\n"
6884 " Migrate printer settings to local server")
6886 {NULL, NULL, 0, NULL, NULL}
6889 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6894 * List printers on a remote RPC server.
6896 * @param c A net_context structure.
6897 * @param argc Standard main() style argc.
6898 * @param argv Standard main() style argv. Initial components are already
6901 * @return A shell status integer (0 for success).
6903 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6905 if (c->display_usage) {
6907 "net rpc printer list\n"
6910 _("List printers on a remote RPC server"));
6914 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6915 rpc_printer_list_internals,
6920 * List printer-drivers on a remote RPC server.
6922 * @param c A net_context structure.
6923 * @param argc Standard main() style argc.
6924 * @param argv Standard main() style argv. Initial components are already
6927 * @return A shell status integer (0 for success).
6929 static int rpc_printer_driver_list(struct net_context *c, int argc,
6932 if (c->display_usage) {
6934 "net rpc printer driver\n"
6937 _("List printer-drivers on a remote RPC server"));
6941 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6942 rpc_printer_driver_list_internals,
6947 * Publish printer in ADS via MSRPC.
6949 * @param c A net_context structure.
6950 * @param argc Standard main() style argc.
6951 * @param argv Standard main() style argv. Initial components are already
6954 * @return A shell status integer (0 for success).
6956 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6959 if (c->display_usage) {
6961 "net rpc printer publish publish\n"
6964 _("Publish printer in ADS via MSRPC"));
6968 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6969 rpc_printer_publish_publish_internals,
6974 * Update printer in ADS via MSRPC.
6976 * @param c A net_context structure.
6977 * @param argc Standard main() style argc.
6978 * @param argv Standard main() style argv. Initial components are already
6981 * @return A shell status integer (0 for success).
6983 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6985 if (c->display_usage) {
6987 "net rpc printer publish update\n"
6990 _("Update printer in ADS via MSRPC"));
6994 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6995 rpc_printer_publish_update_internals,
7000 * UnPublish printer in ADS via MSRPC.
7002 * @param c A net_context structure.
7003 * @param argc Standard main() style argc.
7004 * @param argv Standard main() style argv. Initial components are already
7007 * @return A shell status integer (0 for success).
7009 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7012 if (c->display_usage) {
7014 "net rpc printer publish unpublish\n"
7017 _("UnPublish printer in ADS via MSRPC"));
7021 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7022 rpc_printer_publish_unpublish_internals,
7027 * List published printers via MSRPC.
7029 * @param c A net_context structure.
7030 * @param argc Standard main() style argc.
7031 * @param argv Standard main() style argv. Initial components are already
7034 * @return A shell status integer (0 for success).
7036 static int rpc_printer_publish_list(struct net_context *c, int argc,
7039 if (c->display_usage) {
7041 "net rpc printer publish list\n"
7044 _("List published printers via MSRPC"));
7048 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7049 rpc_printer_publish_list_internals,
7055 * Publish printer in ADS.
7057 * @param c A net_context structure.
7058 * @param argc Standard main() style argc.
7059 * @param argv Standard main() style argv. Initial components are already
7062 * @return A shell status integer (0 for success).
7064 static int rpc_printer_publish(struct net_context *c, int argc,
7068 struct functable func[] = {
7071 rpc_printer_publish_publish,
7073 N_("Publish printer in AD"),
7074 N_("net rpc printer publish publish\n"
7075 " Publish printer in AD")
7079 rpc_printer_publish_update,
7081 N_("Update printer in AD"),
7082 N_("net rpc printer publish update\n"
7083 " Update printer in AD")
7087 rpc_printer_publish_unpublish,
7089 N_("Unpublish printer"),
7090 N_("net rpc printer publish unpublish\n"
7091 " Unpublish printer")
7095 rpc_printer_publish_list,
7097 N_("List published printers"),
7098 N_("net rpc printer publish list\n"
7099 " List published printers")
7101 {NULL, NULL, 0, NULL, NULL}
7105 if (c->display_usage) {
7106 d_printf(_("Usage:\n"));
7107 d_printf(_("net rpc printer publish\n"
7108 " List published printers\n"
7109 " Alias of net rpc printer publish "
7111 net_display_usage_from_functable(func);
7114 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7115 rpc_printer_publish_list_internals,
7119 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7125 * Display rpc printer help page.
7127 * @param c A net_context structure.
7128 * @param argc Standard main() style argc.
7129 * @param argv Standard main() style argv. Initial components are already
7132 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7134 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7135 "\tlists all printers on print-server\n\n"));
7136 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7137 "\tlists all printer-drivers on print-server\n\n"));
7138 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7139 "\tpublishes printer settings in Active Directory\n"
7140 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7141 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7142 "\n\tmigrates printers from remote to local server\n\n"));
7143 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7144 "\n\tmigrates printer-settings from remote to local server\n\n"));
7145 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7146 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7147 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7148 "\n\tmigrates printer-forms from remote to local server\n\n"));
7149 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7150 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7151 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7152 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7153 "\tremote to local print-server\n\n"));
7154 net_common_methods_usage(c, argc, argv);
7155 net_common_flags_usage(c, argc, argv);
7157 "\t-v or --verbose\t\t\tgive verbose output\n"
7158 "\t --destination\t\tmigration target server (default: localhost)\n"));
7164 * 'net rpc printer' entrypoint.
7166 * @param c A net_context structure.
7167 * @param argc Standard main() style argc.
7168 * @param argv Standard main() style argv. Initial components are already
7171 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7173 struct functable func[] = {
7178 N_("List all printers on print server"),
7179 N_("net rpc printer list\n"
7180 " List all printers on print server")
7184 rpc_printer_migrate,
7186 N_("Migrate printer to local server"),
7187 N_("net rpc printer migrate\n"
7188 " Migrate printer to local server")
7192 rpc_printer_driver_list,
7194 N_("List printer drivers"),
7195 N_("net rpc printer driver\n"
7196 " List printer drivers")
7200 rpc_printer_publish,
7202 N_("Publish printer in AD"),
7203 N_("net rpc printer publish\n"
7204 " Publish printer in AD")
7206 {NULL, NULL, 0, NULL, NULL}
7210 if (c->display_usage) {
7211 d_printf(_("Usage:\n"));
7212 d_printf(_("net rpc printer\n"
7213 " List printers\n"));
7214 net_display_usage_from_functable(func);
7217 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7218 rpc_printer_list_internals,
7222 return net_run_function(c, argc, argv, "net rpc printer", func);
7226 * 'net rpc' entrypoint.
7228 * @param c A net_context structure.
7229 * @param argc Standard main() style argc.
7230 * @param argv Standard main() style argv. Initial components are already
7234 int net_rpc(struct net_context *c, int argc, const char **argv)
7236 NET_API_STATUS status;
7238 struct functable func[] = {
7243 N_("Modify global audit settings"),
7244 N_("net rpc audit\n"
7245 " Modify global audit settings")
7251 N_("Show basic info about a domain"),
7253 " Show basic info about a domain")
7259 N_("Join a domain"),
7267 N_("Join a domain created in server manager"),
7268 N_("net rpc oldjoin\n"
7269 " Join a domain created in server manager")
7275 N_("Test that a join is valid"),
7276 N_("net rpc testjoin\n"
7277 " Test that a join is valid")
7283 N_("List/modify users"),
7285 " List/modify users")
7291 N_("Change a user password"),
7292 N_("net rpc password\n"
7293 " Change a user password\n"
7294 " Alias for net rpc user password")
7300 N_("List/modify groups"),
7301 N_("net rpc group\n"
7302 " List/modify groups")
7308 N_("List/modify shares"),
7309 N_("net rpc share\n"
7310 " List/modify shares")
7316 N_("List open files"),
7324 N_("List/modify printers"),
7325 N_("net rpc printer\n"
7326 " List/modify printers")
7330 net_rpc_changetrustpw,
7332 N_("Change trust account password"),
7333 N_("net rpc changetrustpw\n"
7334 " Change trust account password")
7340 N_("Modify domain trusts"),
7341 N_("net rpc trustdom\n"
7342 " Modify domain trusts")
7348 N_("Abort a remote shutdown"),
7349 N_("net rpc abortshutdown\n"
7350 " Abort a remote shutdown")
7356 N_("Shutdown a remote server"),
7357 N_("net rpc shutdown\n"
7358 " Shutdown a remote server")
7364 N_("Dump SAM data of remote NT PDC"),
7365 N_("net rpc samdump\n"
7366 " Dump SAM data of remote NT PDC")
7372 N_("Sync a remote NT PDC's data into local passdb"),
7373 N_("net rpc vampire\n"
7374 " Sync a remote NT PDC's data into local passdb")
7380 N_("Fetch the domain sid into local secrets.tdb"),
7381 N_("net rpc getsid\n"
7382 " Fetch the domain sid into local secrets.tdb")
7388 N_("Manage privileges assigned to SID"),
7389 N_("net rpc rights\n"
7390 " Manage privileges assigned to SID")
7396 N_("Start/stop/query remote services"),
7397 N_("net rpc service\n"
7398 " Start/stop/query remote services")
7404 N_("Manage registry hives"),
7405 N_("net rpc registry\n"
7406 " Manage registry hives")
7412 N_("Open interactive shell on remote server"),
7413 N_("net rpc shell\n"
7414 " Open interactive shell on remote server")
7416 {NULL, NULL, 0, NULL, NULL}
7419 status = libnetapi_init(&c->netapi_ctx);
7423 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7424 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7425 if (c->opt_kerberos) {
7426 libnetapi_set_use_kerberos(c->netapi_ctx);
7428 if (c->opt_ccache) {
7429 libnetapi_set_use_ccache(c->netapi_ctx);
7432 return net_run_function(c, argc, argv, "net rpc", func);