2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/cli_samr.h"
27 #include "rpc_client/cli_samr.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/cli_lsa.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/cli_netlogon.h"
32 #include "../librpc/gen_ndr/cli_srvsvc.h"
33 #include "../librpc/gen_ndr/cli_spoolss.h"
34 #include "../librpc/gen_ndr/cli_initshutdown.h"
35 #include "../librpc/gen_ndr/cli_winreg.h"
37 static int net_mode_share;
38 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
43 * @brief RPC based subcommands for the 'net' utility.
45 * This file should contain much of the functionality that used to
46 * be found in rpcclient, execpt that the commands should change
47 * less often, and the fucntionality should be sane (the user is not
48 * expected to know a rid/sid before they conduct an operation etc.)
50 * @todo Perhaps eventually these should be split out into a number
51 * of files, as this could get quite big.
56 * Many of the RPC functions need the domain sid. This function gets
57 * it at the start of every run
59 * @param cli A cli_state already connected to the remote machine
61 * @return The Domain SID of the remote machine.
64 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
65 struct dom_sid **domain_sid,
66 const char **domain_name)
68 struct rpc_pipe_client *lsa_pipe = NULL;
69 struct policy_handle pol;
70 NTSTATUS result = NT_STATUS_OK;
71 union lsa_PolicyInformation *info = NULL;
73 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
75 if (!NT_STATUS_IS_OK(result)) {
76 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
80 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
81 SEC_FLAG_MAXIMUM_ALLOWED,
83 if (!NT_STATUS_IS_OK(result)) {
84 d_fprintf(stderr, "open_policy %s: %s\n",
90 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
92 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
94 if (!NT_STATUS_IS_OK(result)) {
95 d_fprintf(stderr, "lsaquery %s: %s\n",
101 *domain_name = info->account_domain.name.string;
102 *domain_sid = info->account_domain.sid;
104 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
105 TALLOC_FREE(lsa_pipe);
111 * Run a single RPC command, from start to finish.
113 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
114 * @param conn_flag a NET_FLAG_ combination. Passed to
115 * net_make_ipc_connection.
116 * @param argc Standard main() style argc.
117 * @param argv Standard main() style argv. Initial components are already
119 * @return A shell status integer (0 for success).
122 int run_rpc_command(struct net_context *c,
123 struct cli_state *cli_arg,
124 const struct ndr_syntax_id *interface,
130 struct cli_state *cli = NULL;
131 struct rpc_pipe_client *pipe_hnd = NULL;
134 struct dom_sid *domain_sid;
135 const char *domain_name;
138 /* make use of cli_state handed over as an argument, if possible */
140 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
141 if (!NT_STATUS_IS_OK(nt_status)) {
142 DEBUG(1, ("failed to make ipc connection: %s\n",
143 nt_errstr(nt_status)));
156 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
157 DEBUG(0, ("talloc_init() failed\n"));
161 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
163 if (!NT_STATUS_IS_OK(nt_status)) {
167 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
168 if (lp_client_schannel()
169 && (ndr_syntax_id_equal(interface,
170 &ndr_table_netlogon.syntax_id))) {
171 /* Always try and create an schannel netlogon pipe. */
172 nt_status = cli_rpc_pipe_open_schannel(
173 cli, interface, NCACN_NP,
174 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
176 if (!NT_STATUS_IS_OK(nt_status)) {
177 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
178 nt_errstr(nt_status) ));
182 if (conn_flags & NET_FLAGS_SEAL) {
183 nt_status = cli_rpc_pipe_open_ntlmssp(
185 (conn_flags & NET_FLAGS_TCP) ?
186 NCACN_IP_TCP : NCACN_NP,
187 DCERPC_AUTH_LEVEL_PRIVACY,
188 lp_workgroup(), c->opt_user_name,
189 c->opt_password, &pipe_hnd);
191 nt_status = cli_rpc_pipe_open_noauth(
195 if (!NT_STATUS_IS_OK(nt_status)) {
196 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
197 get_pipe_name_from_syntax(
198 talloc_tos(), interface),
199 nt_errstr(nt_status) ));
205 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
207 if (!NT_STATUS_IS_OK(nt_status)) {
208 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
211 DEBUG(5, ("rpc command function succedded\n"));
214 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
216 TALLOC_FREE(pipe_hnd);
221 /* close the connection only if it was opened here */
226 talloc_destroy(mem_ctx);
231 * Force a change of the trust acccount password.
233 * All parameters are provided by the run_rpc_command function, except for
234 * argc, argv which are passed through.
236 * @param domain_sid The domain sid acquired from the remote server.
237 * @param cli A cli_state connected to the server.
238 * @param mem_ctx Talloc context, destroyed on completion of the function.
239 * @param argc Standard main() style argc.
240 * @param argv Standard main() style argv. Initial components are already
243 * @return Normal NTSTATUS return.
246 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
247 const struct dom_sid *domain_sid,
248 const char *domain_name,
249 struct cli_state *cli,
250 struct rpc_pipe_client *pipe_hnd,
257 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
258 if (!NT_STATUS_IS_OK(status)) {
259 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
268 * Force a change of the trust acccount password.
270 * @param argc Standard main() style argc.
271 * @param argv Standard main() style argv. Initial components are already
274 * @return A shell status integer (0 for success).
277 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
279 if (c->display_usage) {
281 "net rpc changetrustpw\n"
284 _("Change the machine trust password"));
288 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
289 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
290 rpc_changetrustpw_internals,
295 * Join a domain, the old way.
297 * This uses 'machinename' as the inital password, and changes it.
299 * The password should be created with 'server manager' or equiv first.
301 * All parameters are provided by the run_rpc_command function, except for
302 * argc, argv which are passed through.
304 * @param domain_sid The domain sid acquired from the remote server.
305 * @param cli A cli_state connected to the server.
306 * @param mem_ctx Talloc context, destroyed on completion of the function.
307 * @param argc Standard main() style argc.
308 * @param argv Standard main() style argv. Initial components are already
311 * @return Normal NTSTATUS return.
314 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
315 const struct dom_sid *domain_sid,
316 const char *domain_name,
317 struct cli_state *cli,
318 struct rpc_pipe_client *pipe_hnd,
324 fstring trust_passwd;
325 unsigned char orig_trust_passwd_hash[16];
327 enum netr_SchannelType sec_channel_type;
329 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
331 if (!NT_STATUS_IS_OK(result)) {
332 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
335 nt_errstr(result) ));
340 check what type of join - if the user want's to join as
341 a BDC, the server must agree that we are a BDC.
344 sec_channel_type = get_sec_channel_type(argv[0]);
346 sec_channel_type = get_sec_channel_type(NULL);
349 fstrcpy(trust_passwd, global_myname());
350 strlower_m(trust_passwd);
353 * Machine names can be 15 characters, but the max length on
354 * a password is 14. --jerry
357 trust_passwd[14] = '\0';
359 E_md4hash(trust_passwd, orig_trust_passwd_hash);
361 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
363 orig_trust_passwd_hash,
366 if (NT_STATUS_IS_OK(result))
367 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
370 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
371 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
372 result = NT_STATUS_UNSUCCESSFUL;
379 * Join a domain, the old way.
381 * @param argc Standard main() style argc.
382 * @param argv Standard main() style argv. Initial components are already
385 * @return A shell status integer (0 for success).
388 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
390 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
391 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
392 rpc_oldjoin_internals,
397 * Join a domain, the old way. This function exists to allow
398 * the message to be displayed when oldjoin was explicitly
399 * requested, but not when it was implied by "net rpc join".
401 * @param argc Standard main() style argc.
402 * @param argv Standard main() style argv. Initial components are already
405 * @return A shell status integer (0 for success).
408 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
412 if (c->display_usage) {
417 _("Join a domain the old way"));
421 rc = net_rpc_perform_oldjoin(c, argc, argv);
424 d_fprintf(stderr, _("Failed to join domain\n"));
431 * 'net rpc join' entrypoint.
432 * @param argc Standard main() style argc.
433 * @param argv Standard main() style argv. Initial components are already
436 * Main 'net_rpc_join()' (where the admin username/password is used) is
438 * Try to just change the password, but if that doesn't work, use/prompt
439 * for a username/password.
442 int net_rpc_join(struct net_context *c, int argc, const char **argv)
444 if (c->display_usage) {
447 _("net rpc join -U <username>[%%password] <type>\n"
449 " username\tName of the admin user"
450 " password\tPassword of the admin user, will "
451 "prompt if not specified\n"
452 " type\tCan be one of the following:\n"
453 "\t\tMEMBER\tJoin as member server (default)\n"
454 "\t\tBDC\tJoin as BDC\n"
455 "\t\tPDC\tJoin as PDC\n"));
459 if (lp_server_role() == ROLE_STANDALONE) {
460 d_printf(_("cannot join as standalone machine\n"));
464 if (strlen(global_myname()) > 15) {
465 d_printf(_("Our netbios name can be at most 15 chars long, "
466 "\"%s\" is %u chars long\n"),
467 global_myname(), (unsigned int)strlen(global_myname()));
471 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
474 return net_rpc_join_newstyle(c, argc, argv);
478 * display info about a rpc domain
480 * All parameters are provided by the run_rpc_command function, except for
481 * argc, argv which are passed through.
483 * @param domain_sid The domain sid acquired from the remote server
484 * @param cli A cli_state connected to the server.
485 * @param mem_ctx Talloc context, destroyed on completion of the function.
486 * @param argc Standard main() style argc.
487 * @param argv Standard main() style argv. Initial components are already
490 * @return Normal NTSTATUS return.
493 NTSTATUS rpc_info_internals(struct net_context *c,
494 const struct dom_sid *domain_sid,
495 const char *domain_name,
496 struct cli_state *cli,
497 struct rpc_pipe_client *pipe_hnd,
502 struct policy_handle connect_pol, domain_pol;
503 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
504 union samr_DomainInfo *info = NULL;
507 sid_to_fstring(sid_str, domain_sid);
509 /* Get sam policy handle */
510 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
512 MAXIMUM_ALLOWED_ACCESS,
514 if (!NT_STATUS_IS_OK(result)) {
515 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
520 /* Get domain policy handle */
521 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
523 MAXIMUM_ALLOWED_ACCESS,
524 CONST_DISCARD(struct dom_sid2 *, domain_sid),
526 if (!NT_STATUS_IS_OK(result)) {
527 d_fprintf(stderr, _("Could not open domain: %s\n"),
532 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
536 if (NT_STATUS_IS_OK(result)) {
537 d_printf(_("Domain Name: %s\n"),
538 info->general.domain_name.string);
539 d_printf(_("Domain SID: %s\n"), sid_str);
540 d_printf(_("Sequence number: %llu\n"),
541 (unsigned long long)info->general.sequence_num);
542 d_printf(_("Num users: %u\n"), info->general.num_users);
543 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
544 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
552 * 'net rpc info' entrypoint.
553 * @param argc Standard main() style argc.
554 * @param argv Standard main() style argv. Initial components are already
558 int net_rpc_info(struct net_context *c, int argc, const char **argv)
560 if (c->display_usage) {
565 _("Display information about the domain"));
569 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
570 NET_FLAGS_PDC, rpc_info_internals,
575 * Fetch domain SID into the local secrets.tdb.
577 * All parameters are provided by the run_rpc_command function, except for
578 * argc, argv which are passed through.
580 * @param domain_sid The domain sid acquired from the remote server.
581 * @param cli A cli_state connected to the server.
582 * @param mem_ctx Talloc context, destroyed on completion of the function.
583 * @param argc Standard main() style argc.
584 * @param argv Standard main() style argv. Initial components are already
587 * @return Normal NTSTATUS return.
590 static NTSTATUS rpc_getsid_internals(struct net_context *c,
591 const struct dom_sid *domain_sid,
592 const char *domain_name,
593 struct cli_state *cli,
594 struct rpc_pipe_client *pipe_hnd,
601 sid_to_fstring(sid_str, domain_sid);
602 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
603 sid_str, domain_name);
605 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
606 DEBUG(0,("Can't store domain SID\n"));
607 return NT_STATUS_UNSUCCESSFUL;
614 * 'net rpc getsid' entrypoint.
615 * @param argc Standard main() style argc.
616 * @param argv Standard main() style argv. Initial components are already
620 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
622 int conn_flags = NET_FLAGS_PDC;
624 if (!c->opt_user_specified) {
625 conn_flags |= NET_FLAGS_ANONYMOUS;
628 if (c->display_usage) {
633 _("Fetch domain SID into local secrets.tdb"));
637 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
639 rpc_getsid_internals,
643 /****************************************************************************/
646 * Basic usage function for 'net rpc user'.
647 * @param argc Standard main() style argc.
648 * @param argv Standard main() style argv. Initial components are already
652 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
654 return net_user_usage(c, argc, argv);
658 * Add a new user to a remote RPC server.
660 * @param argc Standard main() style argc.
661 * @param argv Standard main() style argv. Initial components are already
664 * @return A shell status integer (0 for success).
667 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
669 NET_API_STATUS status;
670 struct USER_INFO_1 info1;
671 uint32_t parm_error = 0;
673 if (argc < 1 || c->display_usage) {
674 rpc_user_usage(c, argc, argv);
680 info1.usri1_name = argv[0];
682 info1.usri1_password = argv[1];
685 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
688 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
689 argv[0], libnetapi_get_error_string(c->netapi_ctx,
693 d_printf(_("Added user '%s'.\n"), argv[0]);
700 * Rename a user on a remote RPC server.
702 * @param argc Standard main() style argc.
703 * @param argv Standard main() style argv. Initial components are already
706 * @return A shell status integer (0 for success).
709 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
711 NET_API_STATUS status;
712 struct USER_INFO_0 u0;
713 uint32_t parm_err = 0;
715 if (argc != 2 || c->display_usage) {
716 rpc_user_usage(c, argc, argv);
720 u0.usri0_name = argv[1];
722 status = NetUserSetInfo(c->opt_host, argv[0],
723 0, (uint8_t *)&u0, &parm_err);
726 _("Failed to rename user from %s to %s - %s\n"),
728 libnetapi_get_error_string(c->netapi_ctx, status));
730 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
737 * Set a user's primary group
739 * @param argc Standard main() style argc.
740 * @param argv Standard main() style argv. Initial components are already
743 * @return A shell status integer (0 for success).
746 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
749 NET_API_STATUS status;
751 struct GROUP_INFO_2 *g2;
752 struct USER_INFO_1051 u1051;
753 uint32_t parm_err = 0;
755 if (argc != 2 || c->display_usage) {
756 rpc_user_usage(c, argc, argv);
760 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
762 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
764 libnetapi_get_error_string(c->netapi_ctx, status));
767 g2 = (struct GROUP_INFO_2 *)buffer;
769 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
771 NetApiBufferFree(buffer);
773 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
774 (uint8_t *)&u1051, &parm_err);
777 _("Failed to set user's primary group %s to %s - "
778 "%s\n"), argv[0], argv[1],
779 libnetapi_get_error_string(c->netapi_ctx, status));
781 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
788 * Delete a user from a remote RPC server.
790 * @param argc Standard main() style argc.
791 * @param argv Standard main() style argv. Initial components are already
794 * @return A shell status integer (0 for success).
797 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
799 NET_API_STATUS status;
801 if (argc < 1 || c->display_usage) {
802 rpc_user_usage(c, argc, argv);
806 status = NetUserDel(c->opt_host, argv[0]);
809 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
811 libnetapi_get_error_string(c->netapi_ctx, status));
814 d_printf(_("Deleted user '%s'.\n"), argv[0]);
821 * Set a user's password on a remote RPC server.
823 * @param argc Standard main() style argc.
824 * @param argv Standard main() style argv. Initial components are already
827 * @return A shell status integer (0 for success).
830 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
832 NET_API_STATUS status;
834 struct USER_INFO_1003 u1003;
835 uint32_t parm_err = 0;
838 if (argc < 1 || c->display_usage) {
839 rpc_user_usage(c, argc, argv);
844 u1003.usri1003_password = argv[1];
846 ret = asprintf(&prompt, _("Enter new password for %s:"),
851 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
853 if (u1003.usri1003_password == NULL) {
858 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
860 /* Display results */
863 _("Failed to set password for '%s' with error: %s.\n"),
864 argv[0], libnetapi_get_error_string(c->netapi_ctx,
873 * List a user's groups from a remote RPC server.
875 * @param argc Standard main() style argc.
876 * @param argv Standard main() style argv. Initial components are already
879 * @return A shell status integer (0 for success)
882 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
885 NET_API_STATUS status;
886 struct GROUP_USERS_INFO_0 *u0 = NULL;
887 uint32_t entries_read = 0;
888 uint32_t total_entries = 0;
892 if (argc < 1 || c->display_usage) {
893 rpc_user_usage(c, argc, argv);
897 status = NetUserGetGroups(c->opt_host,
900 (uint8_t **)(void *)&u0,
906 _("Failed to get groups for '%s' with error: %s.\n"),
907 argv[0], libnetapi_get_error_string(c->netapi_ctx,
912 for (i=0; i < entries_read; i++) {
913 printf("%s\n", u0->grui0_name);
921 * List users on a remote RPC server.
923 * All parameters are provided by the run_rpc_command function, except for
924 * argc, argv which are passed through.
926 * @param domain_sid The domain sid acquired from the remote server.
927 * @param cli A cli_state connected to the server.
928 * @param mem_ctx Talloc context, destroyed on completion of the function.
929 * @param argc Standard main() style argc.
930 * @param argv Standard main() style argv. Initial components are already
933 * @return Normal NTSTATUS return.
936 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
938 NET_API_STATUS status;
939 uint32_t start_idx=0, num_entries, i, loop_count = 0;
940 struct NET_DISPLAY_USER *info = NULL;
943 /* Query domain users */
944 if (c->opt_long_list_entries)
945 d_printf(_("\nUser name Comment"
946 "\n-----------------------------\n"));
948 uint32_t max_entries, max_size;
950 get_query_dispinfo_params(
951 loop_count, &max_entries, &max_size);
953 status = NetQueryDisplayInformation(c->opt_host,
960 if (status != 0 && status != ERROR_MORE_DATA) {
964 info = (struct NET_DISPLAY_USER *)buffer;
966 for (i = 0; i < num_entries; i++) {
968 if (c->opt_long_list_entries)
969 printf("%-21.21s %s\n", info->usri1_name,
970 info->usri1_comment);
972 printf("%s\n", info->usri1_name);
976 NetApiBufferFree(buffer);
979 start_idx += num_entries;
981 } while (status == ERROR_MORE_DATA);
987 * 'net rpc user' entrypoint.
988 * @param argc Standard main() style argc.
989 * @param argv Standard main() style argv. Initial components are already
993 int net_rpc_user(struct net_context *c, int argc, const char **argv)
995 NET_API_STATUS status;
997 struct functable func[] = {
1002 N_("Add specified user"),
1003 N_("net rpc user add\n"
1004 " Add specified user")
1010 N_("List domain groups of user"),
1011 N_("net rpc user info\n"
1012 " List domain groups of user")
1018 N_("Remove specified user"),
1019 N_("net rpc user delete\n"
1020 " Remove specified user")
1026 N_("Change user password"),
1027 N_("net rpc user password\n"
1028 " Change user password")
1034 N_("Rename specified user"),
1035 N_("net rpc user rename\n"
1036 " Rename specified user")
1040 rpc_user_setprimarygroup,
1042 "Set a user's primary group",
1043 "net rpc user setprimarygroup\n"
1044 " Set a user's primary group"
1046 {NULL, NULL, 0, NULL, NULL}
1049 status = libnetapi_init(&c->netapi_ctx);
1053 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1054 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1055 if (c->opt_kerberos) {
1056 libnetapi_set_use_kerberos(c->netapi_ctx);
1060 if (c->display_usage) {
1065 _("List all users"));
1066 net_display_usage_from_functable(func);
1070 return rpc_user_list(c, argc, argv);
1073 return net_run_function(c, argc, argv, "net rpc user", func);
1076 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1077 TALLOC_CTX *mem_ctx,
1078 struct rpc_sh_ctx *ctx,
1079 struct rpc_pipe_client *pipe_hnd,
1080 int argc, const char **argv)
1082 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1085 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1086 TALLOC_CTX *mem_ctx,
1087 struct rpc_sh_ctx *ctx,
1088 struct rpc_pipe_client *pipe_hnd,
1089 int argc, const char **argv)
1091 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1094 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1095 TALLOC_CTX *mem_ctx,
1096 struct rpc_sh_ctx *ctx,
1097 struct rpc_pipe_client *pipe_hnd,
1098 int argc, const char **argv,
1100 struct net_context *c,
1101 TALLOC_CTX *mem_ctx,
1102 struct rpc_sh_ctx *ctx,
1103 struct rpc_pipe_client *pipe_hnd,
1104 struct policy_handle *user_hnd,
1105 int argc, const char **argv))
1107 struct policy_handle connect_pol, domain_pol, user_pol;
1108 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1111 enum lsa_SidType type;
1114 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1116 return NT_STATUS_INVALID_PARAMETER;
1119 ZERO_STRUCT(connect_pol);
1120 ZERO_STRUCT(domain_pol);
1121 ZERO_STRUCT(user_pol);
1123 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1124 argv[0], NULL, NULL, &sid, &type);
1125 if (!NT_STATUS_IS_OK(result)) {
1126 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1131 if (type != SID_NAME_USER) {
1132 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1133 sid_type_lookup(type));
1134 result = NT_STATUS_NO_SUCH_USER;
1138 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1139 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1140 result = NT_STATUS_NO_SUCH_USER;
1144 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1146 MAXIMUM_ALLOWED_ACCESS,
1148 if (!NT_STATUS_IS_OK(result)) {
1152 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1154 MAXIMUM_ALLOWED_ACCESS,
1157 if (!NT_STATUS_IS_OK(result)) {
1161 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1163 MAXIMUM_ALLOWED_ACCESS,
1166 if (!NT_STATUS_IS_OK(result)) {
1170 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1173 if (is_valid_policy_hnd(&user_pol)) {
1174 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1176 if (is_valid_policy_hnd(&domain_pol)) {
1177 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1179 if (is_valid_policy_hnd(&connect_pol)) {
1180 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1185 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1186 TALLOC_CTX *mem_ctx,
1187 struct rpc_sh_ctx *ctx,
1188 struct rpc_pipe_client *pipe_hnd,
1189 struct policy_handle *user_hnd,
1190 int argc, const char **argv)
1193 union samr_UserInfo *info = NULL;
1196 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1198 return NT_STATUS_INVALID_PARAMETER;
1201 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1205 if (!NT_STATUS_IS_OK(result)) {
1209 d_printf(_("user rid: %d, group rid: %d\n"),
1211 info->info21.primary_gid);
1216 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1217 TALLOC_CTX *mem_ctx,
1218 struct rpc_sh_ctx *ctx,
1219 struct rpc_pipe_client *pipe_hnd,
1220 int argc, const char **argv)
1222 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1223 rpc_sh_user_show_internals);
1226 #define FETCHSTR(name, rec) \
1227 do { if (strequal(ctx->thiscmd, name)) { \
1228 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1231 #define SETSTR(name, rec, flag) \
1232 do { if (strequal(ctx->thiscmd, name)) { \
1233 init_lsa_String(&(info->info21.rec), argv[0]); \
1234 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1237 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1238 TALLOC_CTX *mem_ctx,
1239 struct rpc_sh_ctx *ctx,
1240 struct rpc_pipe_client *pipe_hnd,
1241 struct policy_handle *user_hnd,
1242 int argc, const char **argv)
1245 const char *username;
1246 const char *oldval = "";
1247 union samr_UserInfo *info = NULL;
1250 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1251 _("Usage:"), ctx->whoami);
1252 return NT_STATUS_INVALID_PARAMETER;
1255 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1259 if (!NT_STATUS_IS_OK(result)) {
1263 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1265 FETCHSTR("fullname", full_name);
1266 FETCHSTR("homedir", home_directory);
1267 FETCHSTR("homedrive", home_drive);
1268 FETCHSTR("logonscript", logon_script);
1269 FETCHSTR("profilepath", profile_path);
1270 FETCHSTR("description", description);
1273 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1277 if (strcmp(argv[0], "NULL") == 0) {
1281 ZERO_STRUCT(info->info21);
1283 SETSTR("fullname", full_name, FULL_NAME);
1284 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1285 SETSTR("homedrive", home_drive, HOME_DRIVE);
1286 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1287 SETSTR("profilepath", profile_path, PROFILE_PATH);
1288 SETSTR("description", description, DESCRIPTION);
1290 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1295 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1296 ctx->thiscmd, oldval, argv[0]);
1303 #define HANDLEFLG(name, rec) \
1304 do { if (strequal(ctx->thiscmd, name)) { \
1305 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1307 newflags = oldflags | ACB_##rec; \
1309 newflags = oldflags & ~ACB_##rec; \
1312 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1313 TALLOC_CTX *mem_ctx,
1314 struct rpc_sh_ctx *ctx,
1315 struct rpc_pipe_client *pipe_hnd,
1316 int argc, const char **argv)
1318 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1319 rpc_sh_user_str_edit_internals);
1322 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1323 TALLOC_CTX *mem_ctx,
1324 struct rpc_sh_ctx *ctx,
1325 struct rpc_pipe_client *pipe_hnd,
1326 struct policy_handle *user_hnd,
1327 int argc, const char **argv)
1330 const char *username;
1331 const char *oldval = "unknown";
1332 uint32 oldflags, newflags;
1334 union samr_UserInfo *info = NULL;
1337 ((argc == 1) && !strequal(argv[0], "yes") &&
1338 !strequal(argv[0], "no"))) {
1339 /* TRANSATORS: The yes|no here are program keywords. Please do
1341 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1343 return NT_STATUS_INVALID_PARAMETER;
1346 newval = strequal(argv[0], "yes");
1348 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1352 if (!NT_STATUS_IS_OK(result)) {
1356 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1357 oldflags = info->info21.acct_flags;
1358 newflags = info->info21.acct_flags;
1360 HANDLEFLG("disabled", DISABLED);
1361 HANDLEFLG("pwnotreq", PWNOTREQ);
1362 HANDLEFLG("autolock", AUTOLOCK);
1363 HANDLEFLG("pwnoexp", PWNOEXP);
1366 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1371 ZERO_STRUCT(info->info21);
1373 info->info21.acct_flags = newflags;
1374 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1376 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1381 if (NT_STATUS_IS_OK(result)) {
1382 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1383 ctx->thiscmd, oldval, argv[0]);
1391 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1392 TALLOC_CTX *mem_ctx,
1393 struct rpc_sh_ctx *ctx,
1394 struct rpc_pipe_client *pipe_hnd,
1395 int argc, const char **argv)
1397 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1398 rpc_sh_user_flag_edit_internals);
1401 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1402 TALLOC_CTX *mem_ctx,
1403 struct rpc_sh_ctx *ctx)
1405 static struct rpc_sh_cmd cmds[] = {
1407 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1408 N_("Show/Set a user's full name") },
1410 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1411 N_("Show/Set a user's home directory") },
1413 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1414 N_("Show/Set a user's home drive") },
1416 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1417 N_("Show/Set a user's logon script") },
1419 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1420 N_("Show/Set a user's profile path") },
1422 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1423 N_("Show/Set a user's description") },
1425 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1426 N_("Show/Set whether a user is disabled") },
1428 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1429 N_("Show/Set whether a user locked out") },
1431 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1432 N_("Show/Set whether a user does not need a password") },
1434 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1435 N_("Show/Set whether a user's password does not expire") },
1437 { NULL, NULL, 0, NULL, NULL }
1443 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1444 TALLOC_CTX *mem_ctx,
1445 struct rpc_sh_ctx *ctx)
1447 static struct rpc_sh_cmd cmds[] = {
1449 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1450 N_("List available users") },
1452 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1453 N_("List the domain groups a user is member of") },
1455 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1456 N_("Show info about a user") },
1458 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1459 N_("Show/Modify a user's fields") },
1461 { NULL, NULL, 0, NULL, NULL }
1467 /****************************************************************************/
1470 * Basic usage function for 'net rpc group'.
1471 * @param argc Standard main() style argc.
1472 * @param argv Standard main() style argv. Initial components are already
1476 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1478 return net_group_usage(c, argc, argv);
1482 * Delete group on a remote RPC server.
1484 * All parameters are provided by the run_rpc_command function, except for
1485 * argc, argv which are passed through.
1487 * @param domain_sid The domain sid acquired from the remote server.
1488 * @param cli A cli_state connected to the server.
1489 * @param mem_ctx Talloc context, destroyed on completion of the function.
1490 * @param argc Standard main() style argc.
1491 * @param argv Standard main() style argv. Initial components are already
1494 * @return Normal NTSTATUS return.
1497 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1498 const struct dom_sid *domain_sid,
1499 const char *domain_name,
1500 struct cli_state *cli,
1501 struct rpc_pipe_client *pipe_hnd,
1502 TALLOC_CTX *mem_ctx,
1506 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1507 bool group_is_primary = false;
1508 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1510 struct samr_RidTypeArray *rids = NULL;
1513 /* struct samr_RidWithAttribute *user_gids; */
1515 struct samr_Ids group_rids, name_types;
1516 struct lsa_String lsa_acct_name;
1517 union samr_UserInfo *info = NULL;
1519 if (argc < 1 || c->display_usage) {
1520 rpc_group_usage(c, argc,argv);
1521 return NT_STATUS_OK; /* ok? */
1524 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1526 MAXIMUM_ALLOWED_ACCESS,
1529 if (!NT_STATUS_IS_OK(result)) {
1530 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1534 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1536 MAXIMUM_ALLOWED_ACCESS,
1537 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1540 if (!NT_STATUS_IS_OK(result)) {
1541 d_fprintf(stderr, _("Request open_domain failed\n"));
1545 init_lsa_String(&lsa_acct_name, argv[0]);
1547 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1553 if (!NT_STATUS_IS_OK(result)) {
1554 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1558 switch (name_types.ids[0])
1560 case SID_NAME_DOM_GRP:
1561 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1563 MAXIMUM_ALLOWED_ACCESS,
1566 if (!NT_STATUS_IS_OK(result)) {
1567 d_fprintf(stderr, _("Request open_group failed"));
1571 group_rid = group_rids.ids[0];
1573 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1577 if (!NT_STATUS_IS_OK(result)) {
1579 _("Unable to query group members of %s"),
1584 if (c->opt_verbose) {
1586 _("Domain Group %s (rid: %d) has %d members\n"),
1587 argv[0],group_rid, rids->count);
1590 /* Check if group is anyone's primary group */
1591 for (i = 0; i < rids->count; i++)
1593 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1595 MAXIMUM_ALLOWED_ACCESS,
1599 if (!NT_STATUS_IS_OK(result)) {
1601 _("Unable to open group member %d\n"),
1606 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1611 if (!NT_STATUS_IS_OK(result)) {
1613 _("Unable to lookup userinfo for group "
1619 if (info->info21.primary_gid == group_rid) {
1620 if (c->opt_verbose) {
1621 d_printf(_("Group is primary group "
1623 info->info21.account_name.string);
1625 group_is_primary = true;
1628 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1631 if (group_is_primary) {
1632 d_fprintf(stderr, _("Unable to delete group because "
1633 "some of it's members have it as primary "
1635 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1639 /* remove all group members */
1640 for (i = 0; i < rids->count; i++)
1643 d_printf(_("Remove group member %d..."),
1645 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1649 if (NT_STATUS_IS_OK(result)) {
1651 d_printf(_("ok\n"));
1654 d_printf("%s\n", _("failed"));
1659 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1663 /* removing a local group is easier... */
1664 case SID_NAME_ALIAS:
1665 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1667 MAXIMUM_ALLOWED_ACCESS,
1671 if (!NT_STATUS_IS_OK(result)) {
1672 d_fprintf(stderr, _("Request open_alias failed\n"));
1676 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1680 d_fprintf(stderr, _("%s is of type %s. This command is only "
1681 "for deleting local or global groups\n"),
1682 argv[0],sid_type_lookup(name_types.ids[0]));
1683 result = NT_STATUS_UNSUCCESSFUL;
1687 if (NT_STATUS_IS_OK(result)) {
1689 d_printf(_("Deleted %s '%s'\n"),
1690 sid_type_lookup(name_types.ids[0]), argv[0]);
1692 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1693 get_friendly_nt_error_msg(result));
1701 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1703 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1704 rpc_group_delete_internals, argc,argv);
1707 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1709 NET_API_STATUS status;
1710 struct GROUP_INFO_1 info1;
1711 uint32_t parm_error = 0;
1713 if (argc != 1 || c->display_usage) {
1714 rpc_group_usage(c, argc, argv);
1720 info1.grpi1_name = argv[0];
1721 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1722 info1.grpi1_comment = c->opt_comment;
1725 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1729 _("Failed to add group '%s' with error: %s.\n"),
1730 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1734 d_printf(_("Added group '%s'.\n"), argv[0]);
1740 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1742 NET_API_STATUS status;
1743 struct LOCALGROUP_INFO_1 info1;
1744 uint32_t parm_error = 0;
1746 if (argc != 1 || c->display_usage) {
1747 rpc_group_usage(c, argc, argv);
1753 info1.lgrpi1_name = argv[0];
1754 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1755 info1.lgrpi1_comment = c->opt_comment;
1758 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1762 _("Failed to add alias '%s' with error: %s.\n"),
1763 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1767 d_printf(_("Added alias '%s'.\n"), argv[0]);
1773 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1775 if (c->opt_localgroup)
1776 return rpc_alias_add_internals(c, argc, argv);
1778 return rpc_group_add_internals(c, argc, argv);
1781 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1782 TALLOC_CTX *mem_ctx,
1784 struct dom_sid *sid,
1785 enum lsa_SidType *type)
1787 struct dom_sid *sids = NULL;
1788 enum lsa_SidType *types = NULL;
1789 struct rpc_pipe_client *pipe_hnd = NULL;
1790 struct policy_handle lsa_pol;
1791 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1793 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1795 if (!NT_STATUS_IS_OK(result)) {
1799 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1800 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1802 if (!NT_STATUS_IS_OK(result)) {
1806 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1807 &name, NULL, 1, &sids, &types);
1809 if (NT_STATUS_IS_OK(result)) {
1810 sid_copy(sid, &sids[0]);
1814 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1818 TALLOC_FREE(pipe_hnd);
1821 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1823 /* Try as S-1-5-whatever */
1825 struct dom_sid tmp_sid;
1827 if (string_to_sid(&tmp_sid, name)) {
1828 sid_copy(sid, &tmp_sid);
1829 *type = SID_NAME_UNKNOWN;
1830 result = NT_STATUS_OK;
1837 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1838 TALLOC_CTX *mem_ctx,
1839 const struct dom_sid *group_sid,
1842 struct policy_handle connect_pol, domain_pol;
1845 struct policy_handle group_pol;
1847 struct samr_Ids rids, rid_types;
1848 struct lsa_String lsa_acct_name;
1852 sid_copy(&sid, group_sid);
1854 if (!sid_split_rid(&sid, &group_rid)) {
1855 return NT_STATUS_UNSUCCESSFUL;
1858 /* Get sam policy handle */
1859 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1861 MAXIMUM_ALLOWED_ACCESS,
1863 if (!NT_STATUS_IS_OK(result)) {
1867 /* Get domain policy handle */
1868 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1870 MAXIMUM_ALLOWED_ACCESS,
1873 if (!NT_STATUS_IS_OK(result)) {
1877 init_lsa_String(&lsa_acct_name, member);
1879 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1886 if (!NT_STATUS_IS_OK(result)) {
1887 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1892 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1894 MAXIMUM_ALLOWED_ACCESS,
1898 if (!NT_STATUS_IS_OK(result)) {
1902 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1905 0x0005); /* unknown flags */
1908 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1912 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1913 TALLOC_CTX *mem_ctx,
1914 const struct dom_sid *alias_sid,
1917 struct policy_handle connect_pol, domain_pol;
1920 struct policy_handle alias_pol;
1922 struct dom_sid member_sid;
1923 enum lsa_SidType member_type;
1927 sid_copy(&sid, alias_sid);
1929 if (!sid_split_rid(&sid, &alias_rid)) {
1930 return NT_STATUS_UNSUCCESSFUL;
1933 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1934 member, &member_sid, &member_type);
1936 if (!NT_STATUS_IS_OK(result)) {
1937 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1942 /* Get sam policy handle */
1943 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1945 MAXIMUM_ALLOWED_ACCESS,
1947 if (!NT_STATUS_IS_OK(result)) {
1951 /* Get domain policy handle */
1952 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1954 MAXIMUM_ALLOWED_ACCESS,
1957 if (!NT_STATUS_IS_OK(result)) {
1961 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1963 MAXIMUM_ALLOWED_ACCESS,
1967 if (!NT_STATUS_IS_OK(result)) {
1971 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1975 if (!NT_STATUS_IS_OK(result)) {
1980 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1984 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1985 const struct dom_sid *domain_sid,
1986 const char *domain_name,
1987 struct cli_state *cli,
1988 struct rpc_pipe_client *pipe_hnd,
1989 TALLOC_CTX *mem_ctx,
1993 struct dom_sid group_sid;
1994 enum lsa_SidType group_type;
1996 if (argc != 2 || c->display_usage) {
1999 _("net rpc group addmem <group> <member>\n"
2000 " Add a member to a group\n"
2001 " group\tGroup to add member to\n"
2002 " member\tMember to add to group\n"));
2003 return NT_STATUS_UNSUCCESSFUL;
2006 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2007 &group_sid, &group_type))) {
2008 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2010 return NT_STATUS_UNSUCCESSFUL;
2013 if (group_type == SID_NAME_DOM_GRP) {
2014 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2015 &group_sid, argv[1]);
2017 if (!NT_STATUS_IS_OK(result)) {
2018 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2019 argv[1], argv[0], nt_errstr(result));
2024 if (group_type == SID_NAME_ALIAS) {
2025 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2026 &group_sid, argv[1]);
2028 if (!NT_STATUS_IS_OK(result)) {
2029 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2030 argv[1], argv[0], nt_errstr(result));
2035 d_fprintf(stderr, _("Can only add members to global or local groups "
2036 "which %s is not\n"), argv[0]);
2038 return NT_STATUS_UNSUCCESSFUL;
2041 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2043 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2044 rpc_group_addmem_internals,
2048 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2049 struct rpc_pipe_client *pipe_hnd,
2050 TALLOC_CTX *mem_ctx,
2051 const struct dom_sid *group_sid,
2054 struct policy_handle connect_pol, domain_pol;
2057 struct policy_handle group_pol;
2059 struct samr_Ids rids, rid_types;
2060 struct lsa_String lsa_acct_name;
2064 sid_copy(&sid, group_sid);
2066 if (!sid_split_rid(&sid, &group_rid))
2067 return NT_STATUS_UNSUCCESSFUL;
2069 /* Get sam policy handle */
2070 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2072 MAXIMUM_ALLOWED_ACCESS,
2074 if (!NT_STATUS_IS_OK(result))
2077 /* Get domain policy handle */
2078 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2080 MAXIMUM_ALLOWED_ACCESS,
2083 if (!NT_STATUS_IS_OK(result))
2086 init_lsa_String(&lsa_acct_name, member);
2088 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2094 if (!NT_STATUS_IS_OK(result)) {
2095 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2100 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2102 MAXIMUM_ALLOWED_ACCESS,
2106 if (!NT_STATUS_IS_OK(result))
2109 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2114 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2118 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2119 TALLOC_CTX *mem_ctx,
2120 const struct dom_sid *alias_sid,
2123 struct policy_handle connect_pol, domain_pol;
2126 struct policy_handle alias_pol;
2128 struct dom_sid member_sid;
2129 enum lsa_SidType member_type;
2133 sid_copy(&sid, alias_sid);
2135 if (!sid_split_rid(&sid, &alias_rid))
2136 return NT_STATUS_UNSUCCESSFUL;
2138 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2139 member, &member_sid, &member_type);
2141 if (!NT_STATUS_IS_OK(result)) {
2142 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2147 /* Get sam policy handle */
2148 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2150 MAXIMUM_ALLOWED_ACCESS,
2152 if (!NT_STATUS_IS_OK(result)) {
2156 /* Get domain policy handle */
2157 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2159 MAXIMUM_ALLOWED_ACCESS,
2162 if (!NT_STATUS_IS_OK(result)) {
2166 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2168 MAXIMUM_ALLOWED_ACCESS,
2172 if (!NT_STATUS_IS_OK(result))
2175 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2179 if (!NT_STATUS_IS_OK(result))
2183 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2187 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2188 const struct dom_sid *domain_sid,
2189 const char *domain_name,
2190 struct cli_state *cli,
2191 struct rpc_pipe_client *pipe_hnd,
2192 TALLOC_CTX *mem_ctx,
2196 struct dom_sid group_sid;
2197 enum lsa_SidType group_type;
2199 if (argc != 2 || c->display_usage) {
2202 _("net rpc group delmem <group> <member>\n"
2203 " Delete a member from a group\n"
2204 " group\tGroup to delete member from\n"
2205 " member\tMember to delete from group\n"));
2206 return NT_STATUS_UNSUCCESSFUL;
2209 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2210 &group_sid, &group_type))) {
2211 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2213 return NT_STATUS_UNSUCCESSFUL;
2216 if (group_type == SID_NAME_DOM_GRP) {
2217 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2218 &group_sid, argv[1]);
2220 if (!NT_STATUS_IS_OK(result)) {
2221 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2222 argv[1], argv[0], nt_errstr(result));
2227 if (group_type == SID_NAME_ALIAS) {
2228 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2229 &group_sid, argv[1]);
2231 if (!NT_STATUS_IS_OK(result)) {
2232 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2233 argv[1], argv[0], nt_errstr(result));
2238 d_fprintf(stderr, _("Can only delete members from global or local "
2239 "groups which %s is not\n"), argv[0]);
2241 return NT_STATUS_UNSUCCESSFUL;
2244 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2246 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2247 rpc_group_delmem_internals,
2252 * List groups on a remote RPC server.
2254 * All parameters are provided by the run_rpc_command function, except for
2255 * argc, argv which are passes through.
2257 * @param domain_sid The domain sid acquired from the remote server.
2258 * @param cli A cli_state connected to the server.
2259 * @param mem_ctx Talloc context, destroyed on completion of the function.
2260 * @param argc Standard main() style argc.
2261 * @param argv Standard main() style argv. Initial components are already
2264 * @return Normal NTSTATUS return.
2267 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2268 const struct dom_sid *domain_sid,
2269 const char *domain_name,
2270 struct cli_state *cli,
2271 struct rpc_pipe_client *pipe_hnd,
2272 TALLOC_CTX *mem_ctx,
2276 struct policy_handle connect_pol, domain_pol;
2277 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2278 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2279 struct samr_SamArray *groups = NULL;
2280 bool global = false;
2282 bool builtin = false;
2284 if (c->display_usage) {
2287 _("net rpc group list [global] [local] [builtin]\n"
2288 " List groups on RPC server\n"
2289 " global\tList global groups\n"
2290 " local\tList local groups\n"
2291 " builtin\tList builtin groups\n"
2292 " If none of global, local or builtin is "
2293 "specified, all three options are considered "
2295 return NT_STATUS_OK;
2304 for (i=0; i<argc; i++) {
2305 if (strequal(argv[i], "global"))
2308 if (strequal(argv[i], "local"))
2311 if (strequal(argv[i], "builtin"))
2315 /* Get sam policy handle */
2317 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2319 MAXIMUM_ALLOWED_ACCESS,
2321 if (!NT_STATUS_IS_OK(result)) {
2325 /* Get domain policy handle */
2327 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2329 MAXIMUM_ALLOWED_ACCESS,
2330 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2332 if (!NT_STATUS_IS_OK(result)) {
2336 /* Query domain groups */
2337 if (c->opt_long_list_entries)
2338 d_printf(_("\nGroup name Comment"
2339 "\n-----------------------------\n"));
2341 uint32_t max_size, total_size, returned_size;
2342 union samr_DispInfo info;
2346 get_query_dispinfo_params(
2347 loop_count, &max_entries, &max_size);
2349 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2358 num_entries = info.info3.count;
2359 start_idx += info.info3.count;
2361 if (!NT_STATUS_IS_OK(result) &&
2362 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2365 for (i = 0; i < num_entries; i++) {
2367 const char *group = NULL;
2368 const char *desc = NULL;
2370 group = info.info3.entries[i].account_name.string;
2371 desc = info.info3.entries[i].description.string;
2373 if (c->opt_long_list_entries)
2374 printf("%-21.21s %-50.50s\n",
2377 printf("%s\n", group);
2379 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2380 /* query domain aliases */
2385 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2391 if (!NT_STATUS_IS_OK(result) &&
2392 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2395 for (i = 0; i < num_entries; i++) {
2397 const char *description = NULL;
2399 if (c->opt_long_list_entries) {
2401 struct policy_handle alias_pol;
2402 union samr_AliasInfo *info = NULL;
2404 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2407 groups->entries[i].idx,
2409 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2413 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2415 description = info->description.string;
2419 if (description != NULL) {
2420 printf("%-21.21s %-50.50s\n",
2421 groups->entries[i].name.string,
2424 printf("%s\n", groups->entries[i].name.string);
2427 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2428 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2429 /* Get builtin policy handle */
2431 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2433 MAXIMUM_ALLOWED_ACCESS,
2434 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2436 if (!NT_STATUS_IS_OK(result)) {
2439 /* query builtin aliases */
2442 if (!builtin) break;
2444 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2450 if (!NT_STATUS_IS_OK(result) &&
2451 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2454 for (i = 0; i < num_entries; i++) {
2456 const char *description = NULL;
2458 if (c->opt_long_list_entries) {
2460 struct policy_handle alias_pol;
2461 union samr_AliasInfo *info = NULL;
2463 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2466 groups->entries[i].idx,
2468 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2472 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2474 description = info->description.string;
2478 if (description != NULL) {
2479 printf("%-21.21s %-50.50s\n",
2480 groups->entries[i].name.string,
2483 printf("%s\n", groups->entries[i].name.string);
2486 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2492 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2494 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2495 rpc_group_list_internals,
2499 static NTSTATUS rpc_list_group_members(struct net_context *c,
2500 struct rpc_pipe_client *pipe_hnd,
2501 TALLOC_CTX *mem_ctx,
2502 const char *domain_name,
2503 const struct dom_sid *domain_sid,
2504 struct policy_handle *domain_pol,
2508 struct policy_handle group_pol;
2509 uint32 num_members, *group_rids;
2511 struct samr_RidTypeArray *rids = NULL;
2512 struct lsa_Strings names;
2513 struct samr_Ids types;
2516 sid_to_fstring(sid_str, domain_sid);
2518 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2520 MAXIMUM_ALLOWED_ACCESS,
2524 if (!NT_STATUS_IS_OK(result))
2527 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2531 if (!NT_STATUS_IS_OK(result))
2534 num_members = rids->count;
2535 group_rids = rids->rids;
2537 while (num_members > 0) {
2538 int this_time = 512;
2540 if (num_members < this_time)
2541 this_time = num_members;
2543 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2550 if (!NT_STATUS_IS_OK(result))
2553 /* We only have users as members, but make the output
2554 the same as the output of alias members */
2556 for (i = 0; i < this_time; i++) {
2558 if (c->opt_long_list_entries) {
2559 printf("%s-%d %s\\%s %d\n", sid_str,
2560 group_rids[i], domain_name,
2561 names.names[i].string,
2564 printf("%s\\%s\n", domain_name,
2565 names.names[i].string);
2569 num_members -= this_time;
2573 return NT_STATUS_OK;
2576 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2577 struct rpc_pipe_client *pipe_hnd,
2578 TALLOC_CTX *mem_ctx,
2579 struct policy_handle *domain_pol,
2583 struct rpc_pipe_client *lsa_pipe;
2584 struct policy_handle alias_pol, lsa_pol;
2586 struct dom_sid *alias_sids;
2589 enum lsa_SidType *types;
2591 struct lsa_SidArray sid_array;
2593 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2595 MAXIMUM_ALLOWED_ACCESS,
2599 if (!NT_STATUS_IS_OK(result))
2602 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2606 if (!NT_STATUS_IS_OK(result)) {
2607 d_fprintf(stderr, _("Couldn't list alias members\n"));
2611 num_members = sid_array.num_sids;
2613 if (num_members == 0) {
2614 return NT_STATUS_OK;
2617 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2618 &ndr_table_lsarpc.syntax_id,
2620 if (!NT_STATUS_IS_OK(result)) {
2621 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2622 nt_errstr(result) );
2626 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2627 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2629 if (!NT_STATUS_IS_OK(result)) {
2630 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2631 TALLOC_FREE(lsa_pipe);
2635 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2637 d_fprintf(stderr, _("Out of memory\n"));
2638 TALLOC_FREE(lsa_pipe);
2639 return NT_STATUS_NO_MEMORY;
2642 for (i=0; i<num_members; i++) {
2643 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2646 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2647 num_members, alias_sids,
2648 &domains, &names, &types);
2650 if (!NT_STATUS_IS_OK(result) &&
2651 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2652 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2653 TALLOC_FREE(lsa_pipe);
2657 for (i = 0; i < num_members; i++) {
2659 sid_to_fstring(sid_str, &alias_sids[i]);
2661 if (c->opt_long_list_entries) {
2662 printf("%s %s\\%s %d\n", sid_str,
2663 domains[i] ? domains[i] : _("*unknown*"),
2664 names[i] ? names[i] : _("*unknown*"), types[i]);
2667 printf("%s\\%s\n", domains[i], names[i]);
2669 printf("%s\n", sid_str);
2673 TALLOC_FREE(lsa_pipe);
2674 return NT_STATUS_OK;
2677 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2678 const struct dom_sid *domain_sid,
2679 const char *domain_name,
2680 struct cli_state *cli,
2681 struct rpc_pipe_client *pipe_hnd,
2682 TALLOC_CTX *mem_ctx,
2687 struct policy_handle connect_pol, domain_pol;
2688 struct samr_Ids rids, rid_types;
2689 struct lsa_String lsa_acct_name;
2691 /* Get sam policy handle */
2693 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2695 MAXIMUM_ALLOWED_ACCESS,
2698 if (!NT_STATUS_IS_OK(result))
2701 /* Get domain policy handle */
2703 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2705 MAXIMUM_ALLOWED_ACCESS,
2706 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2709 if (!NT_STATUS_IS_OK(result))
2712 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2714 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2721 if (!NT_STATUS_IS_OK(result)) {
2723 /* Ok, did not find it in the global sam, try with builtin */
2725 struct dom_sid sid_Builtin;
2727 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2729 sid_copy(&sid_Builtin, &global_sid_Builtin);
2731 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2733 MAXIMUM_ALLOWED_ACCESS,
2737 if (!NT_STATUS_IS_OK(result)) {
2738 d_fprintf(stderr, _("Couldn't find group %s\n"),
2743 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2750 if (!NT_STATUS_IS_OK(result)) {
2751 d_fprintf(stderr, _("Couldn't find group %s\n"),
2757 if (rids.count != 1) {
2758 d_fprintf(stderr, _("Couldn't find group %s\n"),
2763 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2764 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2765 domain_sid, &domain_pol,
2769 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2770 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2774 return NT_STATUS_NO_SUCH_GROUP;
2777 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2779 if (argc != 1 || c->display_usage) {
2780 return rpc_group_usage(c, argc, argv);
2783 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2784 rpc_group_members_internals,
2788 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2790 NET_API_STATUS status;
2791 struct GROUP_INFO_0 g0;
2795 d_printf(_("Usage:\n"));
2796 d_printf("net rpc group rename group newname\n");
2800 g0.grpi0_name = argv[1];
2802 status = NetGroupSetInfo(c->opt_host,
2809 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2810 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2818 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2820 if (argc != 2 || c->display_usage) {
2821 return rpc_group_usage(c, argc, argv);
2824 return rpc_group_rename_internals(c, argc, argv);
2828 * 'net rpc group' entrypoint.
2829 * @param argc Standard main() style argc.
2830 * @param argv Standard main() style argv. Initial components are already
2834 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2836 NET_API_STATUS status;
2838 struct functable func[] = {
2843 N_("Create specified group"),
2844 N_("net rpc group add\n"
2845 " Create specified group")
2851 N_("Delete specified group"),
2852 N_("net rpc group delete\n"
2853 " Delete specified group")
2859 N_("Add member to group"),
2860 N_("net rpc group addmem\n"
2861 " Add member to group")
2867 N_("Remove member from group"),
2868 N_("net rpc group delmem\n"
2869 " Remove member from group")
2876 N_("net rpc group list\n"
2883 N_("List group members"),
2884 N_("net rpc group members\n"
2885 " List group members")
2892 N_("net rpc group rename\n"
2895 {NULL, NULL, 0, NULL, NULL}
2898 status = libnetapi_init(&c->netapi_ctx);
2902 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2903 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2904 if (c->opt_kerberos) {
2905 libnetapi_set_use_kerberos(c->netapi_ctx);
2909 if (c->display_usage) {
2910 d_printf(_("Usage:\n"));
2911 d_printf(_("net rpc group\n"
2912 " Alias for net rpc group list global "
2913 "local builtin\n"));
2914 net_display_usage_from_functable(func);
2918 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2919 rpc_group_list_internals,
2923 return net_run_function(c, argc, argv, "net rpc group", func);
2926 /****************************************************************************/
2928 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2930 return net_share_usage(c, argc, argv);
2934 * Add a share on a remote RPC server.
2936 * @param argc Standard main() style argc.
2937 * @param argv Standard main() style argv. Initial components are already
2940 * @return A shell status integer (0 for success).
2943 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2945 NET_API_STATUS status;
2948 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2949 uint32 num_users=0, perms=0;
2950 char *password=NULL; /* don't allow a share password */
2951 struct SHARE_INFO_2 i2;
2952 uint32_t parm_error = 0;
2954 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2955 return rpc_share_usage(c, argc, argv);
2958 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2962 path = strchr(sharename, '=');
2969 i2.shi2_netname = sharename;
2970 i2.shi2_type = type;
2971 i2.shi2_remark = c->opt_comment;
2972 i2.shi2_permissions = perms;
2973 i2.shi2_max_uses = c->opt_maxusers;
2974 i2.shi2_current_uses = num_users;
2975 i2.shi2_path = path;
2976 i2.shi2_passwd = password;
2978 status = NetShareAdd(c->opt_host,
2983 printf(_("NetShareAdd failed with: %s\n"),
2984 libnetapi_get_error_string(c->netapi_ctx, status));
2991 * Delete a share on a remote RPC server.
2993 * @param domain_sid The domain sid acquired from the remote server.
2994 * @param argc Standard main() style argc.
2995 * @param argv Standard main() style argv. Initial components are already
2998 * @return A shell status integer (0 for success).
3000 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3002 if (argc < 1 || c->display_usage) {
3003 return rpc_share_usage(c, argc, argv);
3006 return NetShareDel(c->opt_host, argv[0], 0);
3010 * Formatted print of share info
3012 * @param r pointer to SHARE_INFO_1 to format
3015 static void display_share_info_1(struct net_context *c,
3016 struct SHARE_INFO_1 *r)
3018 if (c->opt_long_list_entries) {
3019 d_printf("%-12s %-8.8s %-50s\n",
3021 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3024 d_printf("%s\n", r->shi1_netname);
3028 static WERROR get_share_info(struct net_context *c,
3029 struct rpc_pipe_client *pipe_hnd,
3030 TALLOC_CTX *mem_ctx,
3034 struct srvsvc_NetShareInfoCtr *info_ctr)
3038 union srvsvc_NetShareInfo info;
3040 /* no specific share requested, enumerate all */
3043 uint32_t preferred_len = 0xffffffff;
3044 uint32_t total_entries = 0;
3045 uint32_t resume_handle = 0;
3047 info_ctr->level = level;
3049 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3059 /* request just one share */
3060 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3067 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3072 ZERO_STRUCTP(info_ctr);
3074 info_ctr->level = level;
3079 struct srvsvc_NetShareCtr1 *ctr1;
3081 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3082 W_ERROR_HAVE_NO_MEMORY(ctr1);
3085 ctr1->array = info.info1;
3087 info_ctr->ctr.ctr1 = ctr1;
3091 struct srvsvc_NetShareCtr2 *ctr2;
3093 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3094 W_ERROR_HAVE_NO_MEMORY(ctr2);
3097 ctr2->array = info.info2;
3099 info_ctr->ctr.ctr2 = ctr2;
3103 struct srvsvc_NetShareCtr502 *ctr502;
3105 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3106 W_ERROR_HAVE_NO_MEMORY(ctr502);
3109 ctr502->array = info.info502;
3111 info_ctr->ctr.ctr502 = ctr502;
3119 * 'net rpc share list' entrypoint.
3120 * @param argc Standard main() style argc.
3121 * @param argv Standard main() style argv. Initial components are already
3124 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3126 NET_API_STATUS status;
3127 struct SHARE_INFO_1 *i1 = NULL;
3128 uint32_t entries_read = 0;
3129 uint32_t total_entries = 0;
3130 uint32_t resume_handle = 0;
3131 uint32_t i, level = 1;
3133 if (c->display_usage) {
3135 "net rpc share list\n"
3138 _("List shares on remote server"));
3142 status = NetShareEnum(c->opt_host,
3144 (uint8_t **)(void *)&i1,
3153 /* Display results */
3155 if (c->opt_long_list_entries) {
3157 "\nEnumerating shared resources (exports) on remote server:\n\n"
3158 "\nShare name Type Description\n"
3159 "---------- ---- -----------\n"));
3161 for (i = 0; i < entries_read; i++)
3162 display_share_info_1(c, &i1[i]);
3167 static bool check_share_availability(struct cli_state *cli, const char *netname)
3171 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3172 if (!NT_STATUS_IS_OK(status)) {
3173 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3177 status = cli_tdis(cli);
3178 if (!NT_STATUS_IS_OK(status)) {
3179 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3186 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3187 const char *netname, uint32 type)
3189 /* only support disk shares */
3190 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3191 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3196 /* skip builtin shares */
3197 /* FIXME: should print$ be added too ? */
3198 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3199 strequal(netname,"global"))
3202 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3203 printf(_("excluding [%s]\n"), netname);
3207 return check_share_availability(cli, netname);
3211 * Migrate shares from a remote RPC server to the local RPC server.
3213 * All parameters are provided by the run_rpc_command function, except for
3214 * argc, argv which are passed through.
3216 * @param domain_sid The domain sid acquired from the remote server.
3217 * @param cli A cli_state connected to the server.
3218 * @param mem_ctx Talloc context, destroyed on completion of the function.
3219 * @param argc Standard main() style argc.
3220 * @param argv Standard main() style argv. Initial components are already
3223 * @return Normal NTSTATUS return.
3226 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3227 const struct dom_sid *domain_sid,
3228 const char *domain_name,
3229 struct cli_state *cli,
3230 struct rpc_pipe_client *pipe_hnd,
3231 TALLOC_CTX *mem_ctx,
3236 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3237 struct srvsvc_NetShareInfoCtr ctr_src;
3239 struct rpc_pipe_client *srvsvc_pipe = NULL;
3240 struct cli_state *cli_dst = NULL;
3241 uint32 level = 502; /* includes secdesc */
3242 uint32_t parm_error = 0;
3244 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3246 if (!W_ERROR_IS_OK(result))
3249 /* connect destination PI_SRVSVC */
3250 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3251 &ndr_table_srvsvc.syntax_id);
3252 if (!NT_STATUS_IS_OK(nt_status))
3256 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3258 union srvsvc_NetShareInfo info;
3259 struct srvsvc_NetShareInfo502 info502 =
3260 ctr_src.ctr.ctr502->array[i];
3262 /* reset error-code */
3263 nt_status = NT_STATUS_UNSUCCESSFUL;
3265 if (!check_share_sanity(c, cli, info502.name, info502.type))
3268 /* finally add the share on the dst server */
3270 printf(_("migrating: [%s], path: %s, comment: %s, without "
3272 info502.name, info502.path, info502.comment);
3274 info.info502 = &info502;
3276 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3277 srvsvc_pipe->desthost,
3283 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3284 printf(_(" [%s] does already exist\n"),
3289 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3290 printf(_("cannot add share: %s\n"), win_errstr(result));
3296 nt_status = NT_STATUS_OK;
3300 cli_shutdown(cli_dst);
3308 * Migrate shares from a RPC server to another.
3310 * @param argc Standard main() style argc.
3311 * @param argv Standard main() style argv. Initial components are already
3314 * @return A shell status integer (0 for success).
3316 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3319 if (c->display_usage) {
3321 "net rpc share migrate shares\n"
3324 _("Migrate shares to local server"));
3329 printf(_("no server to migrate\n"));
3333 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3334 rpc_share_migrate_shares_internals,
3341 * @param f file_info
3342 * @param mask current search mask
3343 * @param state arg-pointer
3346 static void copy_fn(const char *mnt, file_info *f,
3347 const char *mask, void *state)
3349 static NTSTATUS nt_status;
3350 static struct copy_clistate *local_state;
3351 static fstring filename, new_mask;
3354 struct net_context *c;
3356 local_state = (struct copy_clistate *)state;
3357 nt_status = NT_STATUS_UNSUCCESSFUL;
3361 if (strequal(f->name, ".") || strequal(f->name, ".."))
3364 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3367 if (f->mode & aDIR) {
3369 DEBUG(3,("got dir: %s\n", f->name));
3371 fstrcpy(dir, local_state->cwd);
3373 fstrcat(dir, f->name);
3375 switch (net_mode_share)
3377 case NET_MODE_SHARE_MIGRATE:
3378 /* create that directory */
3379 nt_status = net_copy_file(c, local_state->mem_ctx,
3380 local_state->cli_share_src,
3381 local_state->cli_share_dst,
3383 c->opt_acls? true : false,
3384 c->opt_attrs? true : false,
3385 c->opt_timestamps? true:false,
3389 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3393 if (!NT_STATUS_IS_OK(nt_status))
3394 printf(_("could not handle dir %s: %s\n"),
3395 dir, nt_errstr(nt_status));
3397 /* search below that directory */
3398 fstrcpy(new_mask, dir);
3399 fstrcat(new_mask, "\\*");
3401 old_dir = local_state->cwd;
3402 local_state->cwd = dir;
3403 if (!sync_files(local_state, new_mask))
3404 printf(_("could not handle files\n"));
3405 local_state->cwd = old_dir;
3412 fstrcpy(filename, local_state->cwd);
3413 fstrcat(filename, "\\");
3414 fstrcat(filename, f->name);
3416 DEBUG(3,("got file: %s\n", filename));
3418 switch (net_mode_share)
3420 case NET_MODE_SHARE_MIGRATE:
3421 nt_status = net_copy_file(c, local_state->mem_ctx,
3422 local_state->cli_share_src,
3423 local_state->cli_share_dst,
3425 c->opt_acls? true : false,
3426 c->opt_attrs? true : false,
3427 c->opt_timestamps? true: false,
3431 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3436 if (!NT_STATUS_IS_OK(nt_status))
3437 printf(_("could not handle file %s: %s\n"),
3438 filename, nt_errstr(nt_status));
3443 * sync files, can be called recursivly to list files
3444 * and then call copy_fn for each file
3446 * @param cp_clistate pointer to the copy_clistate we work with
3447 * @param mask the current search mask
3449 * @return Boolean result
3451 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3453 struct cli_state *targetcli;
3454 char *targetpath = NULL;
3456 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3458 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3459 mask, &targetcli, &targetpath ) ) {
3460 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3462 mask, cli_errstr(cp_clistate->cli_share_src));
3466 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3467 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3468 mask, cli_errstr(targetcli));
3477 * Set the top level directory permissions before we do any further copies.
3478 * Should set up ACL inheritance.
3481 bool copy_top_level_perms(struct net_context *c,
3482 struct copy_clistate *cp_clistate,
3483 const char *sharename)
3485 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3487 switch (net_mode_share) {
3488 case NET_MODE_SHARE_MIGRATE:
3489 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3490 nt_status = net_copy_fileattr(c,
3491 cp_clistate->mem_ctx,
3492 cp_clistate->cli_share_src,
3493 cp_clistate->cli_share_dst,
3495 c->opt_acls? true : false,
3496 c->opt_attrs? true : false,
3497 c->opt_timestamps? true: false,
3501 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3505 if (!NT_STATUS_IS_OK(nt_status)) {
3506 printf(_("Could handle directory attributes for top level "
3507 "directory of share %s. Error %s\n"),
3508 sharename, nt_errstr(nt_status));
3516 * Sync all files inside a remote share to another share (over smb).
3518 * All parameters are provided by the run_rpc_command function, except for
3519 * argc, argv which are passed through.
3521 * @param domain_sid The domain sid acquired from the remote server.
3522 * @param cli A cli_state connected to the server.
3523 * @param mem_ctx Talloc context, destroyed on completion of the function.
3524 * @param argc Standard main() style argc.
3525 * @param argv Standard main() style argv. Initial components are already
3528 * @return Normal NTSTATUS return.
3531 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3532 const struct dom_sid *domain_sid,
3533 const char *domain_name,
3534 struct cli_state *cli,
3535 struct rpc_pipe_client *pipe_hnd,
3536 TALLOC_CTX *mem_ctx,
3541 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3542 struct srvsvc_NetShareInfoCtr ctr_src;
3545 struct copy_clistate cp_clistate;
3546 bool got_src_share = false;
3547 bool got_dst_share = false;
3548 const char *mask = "\\*";
3551 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3553 nt_status = NT_STATUS_NO_MEMORY;
3557 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3560 if (!W_ERROR_IS_OK(result))
3563 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3565 struct srvsvc_NetShareInfo502 info502 =
3566 ctr_src.ctr.ctr502->array[i];
3568 if (!check_share_sanity(c, cli, info502.name, info502.type))
3571 /* one might not want to mirror whole discs :) */
3572 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3573 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3578 switch (net_mode_share)
3580 case NET_MODE_SHARE_MIGRATE:
3584 d_fprintf(stderr, _("Unsupported mode %d\n"),
3588 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3591 c->opt_acls ? _("including") : _("without"),
3592 c->opt_attrs ? _("including") : _("without"),
3593 c->opt_timestamps ? _("(preserving timestamps)") : "");
3595 cp_clistate.mem_ctx = mem_ctx;
3596 cp_clistate.cli_share_src = NULL;
3597 cp_clistate.cli_share_dst = NULL;
3598 cp_clistate.cwd = NULL;
3599 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3602 /* open share source */
3603 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3604 &cli->dest_ss, cli->desthost,
3605 info502.name, "A:");
3606 if (!NT_STATUS_IS_OK(nt_status))
3609 got_src_share = true;
3611 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3612 /* open share destination */
3613 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3614 NULL, dst, info502.name, "A:");
3615 if (!NT_STATUS_IS_OK(nt_status))
3618 got_dst_share = true;
3621 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3622 d_fprintf(stderr, _("Could not handle the top level "
3623 "directory permissions for the "
3624 "share: %s\n"), info502.name);
3625 nt_status = NT_STATUS_UNSUCCESSFUL;
3629 if (!sync_files(&cp_clistate, mask)) {
3630 d_fprintf(stderr, _("could not handle files for share: "
3631 "%s\n"), info502.name);
3632 nt_status = NT_STATUS_UNSUCCESSFUL;
3637 nt_status = NT_STATUS_OK;
3642 cli_shutdown(cp_clistate.cli_share_src);
3645 cli_shutdown(cp_clistate.cli_share_dst);
3652 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3654 if (c->display_usage) {
3656 "net share migrate files\n"
3659 _("Migrate files to local server"));
3664 d_printf(_("no server to migrate\n"));
3668 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3669 rpc_share_migrate_files_internals,
3674 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3676 * All parameters are provided by the run_rpc_command function, except for
3677 * argc, argv which are passed through.
3679 * @param domain_sid The domain sid acquired from the remote server.
3680 * @param cli A cli_state connected to the server.
3681 * @param mem_ctx Talloc context, destroyed on completion of the function.
3682 * @param argc Standard main() style argc.
3683 * @param argv Standard main() style argv. Initial components are already
3686 * @return Normal NTSTATUS return.
3689 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3690 const struct dom_sid *domain_sid,
3691 const char *domain_name,
3692 struct cli_state *cli,
3693 struct rpc_pipe_client *pipe_hnd,
3694 TALLOC_CTX *mem_ctx,
3699 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3700 struct srvsvc_NetShareInfoCtr ctr_src;
3701 union srvsvc_NetShareInfo info;
3703 struct rpc_pipe_client *srvsvc_pipe = NULL;
3704 struct cli_state *cli_dst = NULL;
3705 uint32 level = 502; /* includes secdesc */
3706 uint32_t parm_error = 0;
3708 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3711 if (!W_ERROR_IS_OK(result))
3714 /* connect destination PI_SRVSVC */
3715 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3716 &ndr_table_srvsvc.syntax_id);
3717 if (!NT_STATUS_IS_OK(nt_status))
3721 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3723 struct srvsvc_NetShareInfo502 info502 =
3724 ctr_src.ctr.ctr502->array[i];
3726 /* reset error-code */
3727 nt_status = NT_STATUS_UNSUCCESSFUL;
3729 if (!check_share_sanity(c, cli, info502.name, info502.type))
3732 printf(_("migrating: [%s], path: %s, comment: %s, including "
3734 info502.name, info502.path, info502.comment);
3737 display_sec_desc(info502.sd_buf.sd);
3739 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3740 info.info502 = &info502;
3742 /* finally modify the share on the dst server */
3743 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3744 srvsvc_pipe->desthost,
3750 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3751 printf(_("cannot set share-acl: %s\n"),
3752 win_errstr(result));
3758 nt_status = NT_STATUS_OK;
3762 cli_shutdown(cli_dst);
3770 * Migrate share-acls from a RPC server to another.
3772 * @param argc Standard main() style argc.
3773 * @param argv Standard main() style argv. Initial components are already
3776 * @return A shell status integer (0 for success).
3778 static int rpc_share_migrate_security(struct net_context *c, int argc,
3781 if (c->display_usage) {
3783 "net rpc share migrate security\n"
3786 _("Migrate share-acls to local server"));
3791 d_printf(_("no server to migrate\n"));
3795 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3796 rpc_share_migrate_security_internals,
3801 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3802 * from one server to another.
3804 * @param argc Standard main() style argc.
3805 * @param argv Standard main() style argv. Initial components are already
3808 * @return A shell status integer (0 for success).
3811 static int rpc_share_migrate_all(struct net_context *c, int argc,
3816 if (c->display_usage) {
3818 "net rpc share migrate all\n"
3821 _("Migrates shares including all share settings"));
3826 d_printf(_("no server to migrate\n"));
3830 /* order is important. we don't want to be locked out by the share-acl
3831 * before copying files - gd */
3833 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3834 rpc_share_migrate_shares_internals, argc, argv);
3838 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3839 rpc_share_migrate_files_internals, argc, argv);
3843 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3844 rpc_share_migrate_security_internals, argc,
3850 * 'net rpc share migrate' entrypoint.
3851 * @param argc Standard main() style argc.
3852 * @param argv Standard main() style argv. Initial components are already
3855 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3858 struct functable func[] = {
3861 rpc_share_migrate_all,
3863 N_("Migrate shares from remote to local server"),
3864 N_("net rpc share migrate all\n"
3865 " Migrate shares from remote to local server")
3869 rpc_share_migrate_files,
3871 N_("Migrate files from remote to local server"),
3872 N_("net rpc share migrate files\n"
3873 " Migrate files from remote to local server")
3877 rpc_share_migrate_security,
3879 N_("Migrate share-ACLs from remote to local server"),
3880 N_("net rpc share migrate security\n"
3881 " Migrate share-ACLs from remote to local server")
3885 rpc_share_migrate_shares,
3887 N_("Migrate shares from remote to local server"),
3888 N_("net rpc share migrate shares\n"
3889 " Migrate shares from remote to local server")
3891 {NULL, NULL, 0, NULL, NULL}
3894 net_mode_share = NET_MODE_SHARE_MIGRATE;
3896 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3902 struct dom_sid *members;
3905 static int num_server_aliases;
3906 static struct full_alias *server_aliases;
3909 * Add an alias to the static list.
3911 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3913 if (server_aliases == NULL)
3914 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3916 server_aliases[num_server_aliases] = *alias;
3917 num_server_aliases += 1;
3921 * For a specific domain on the server, fetch all the aliases
3922 * and their members. Add all of them to the server_aliases.
3925 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3926 TALLOC_CTX *mem_ctx,
3927 struct policy_handle *connect_pol,
3928 const struct dom_sid *domain_sid)
3930 uint32 start_idx, max_entries, num_entries, i;
3931 struct samr_SamArray *groups = NULL;
3933 struct policy_handle domain_pol;
3935 /* Get domain policy handle */
3937 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3939 MAXIMUM_ALLOWED_ACCESS,
3940 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3942 if (!NT_STATUS_IS_OK(result))
3949 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3955 for (i = 0; i < num_entries; i++) {
3957 struct policy_handle alias_pol;
3958 struct full_alias alias;
3959 struct lsa_SidArray sid_array;
3962 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3964 MAXIMUM_ALLOWED_ACCESS,
3965 groups->entries[i].idx,
3967 if (!NT_STATUS_IS_OK(result))
3970 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3973 if (!NT_STATUS_IS_OK(result))
3976 alias.num_members = sid_array.num_sids;
3978 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3979 if (!NT_STATUS_IS_OK(result))
3982 alias.members = NULL;
3984 if (alias.num_members > 0) {
3985 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
3987 for (j = 0; j < alias.num_members; j++)
3988 sid_copy(&alias.members[j],
3989 sid_array.sids[j].sid);
3992 sid_compose(&alias.sid, domain_sid,
3993 groups->entries[i].idx);
3995 push_alias(mem_ctx, &alias);
3997 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3999 result = NT_STATUS_OK;
4002 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4008 * Dump server_aliases as names for debugging purposes.
4011 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4012 const struct dom_sid *domain_sid,
4013 const char *domain_name,
4014 struct cli_state *cli,
4015 struct rpc_pipe_client *pipe_hnd,
4016 TALLOC_CTX *mem_ctx,
4022 struct policy_handle lsa_pol;
4024 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4025 SEC_FLAG_MAXIMUM_ALLOWED,
4027 if (!NT_STATUS_IS_OK(result))
4030 for (i=0; i<num_server_aliases; i++) {
4033 enum lsa_SidType *types;
4036 struct full_alias *alias = &server_aliases[i];
4038 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4040 &domains, &names, &types);
4041 if (!NT_STATUS_IS_OK(result))
4044 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4046 if (alias->num_members == 0) {
4051 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4054 &domains, &names, &types);
4056 if (!NT_STATUS_IS_OK(result) &&
4057 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4060 for (j=0; j<alias->num_members; j++)
4061 DEBUG(1, ("%s\\%s (%d); ",
4062 domains[j] ? domains[j] : "*unknown*",
4063 names[j] ? names[j] : "*unknown*",types[j]));
4067 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4069 return NT_STATUS_OK;
4073 * Fetch a list of all server aliases and their members into
4077 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4078 const struct dom_sid *domain_sid,
4079 const char *domain_name,
4080 struct cli_state *cli,
4081 struct rpc_pipe_client *pipe_hnd,
4082 TALLOC_CTX *mem_ctx,
4087 struct policy_handle connect_pol;
4089 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4091 MAXIMUM_ALLOWED_ACCESS,
4094 if (!NT_STATUS_IS_OK(result))
4097 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4098 &global_sid_Builtin);
4100 if (!NT_STATUS_IS_OK(result))
4103 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4106 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4111 static void init_user_token(NT_USER_TOKEN *token, struct dom_sid *user_sid)
4113 token->num_sids = 4;
4115 if (!(token->user_sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4116 d_fprintf(stderr, "malloc %s\n",_("failed"));
4117 token->num_sids = 0;
4121 token->user_sids[0] = *user_sid;
4122 sid_copy(&token->user_sids[1], &global_sid_World);
4123 sid_copy(&token->user_sids[2], &global_sid_Network);
4124 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4127 static void free_user_token(NT_USER_TOKEN *token)
4129 SAFE_FREE(token->user_sids);
4132 static void add_sid_to_token(NT_USER_TOKEN *token, struct dom_sid *sid)
4134 if (is_sid_in_token(token, sid))
4137 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, struct dom_sid, token->num_sids+1);
4138 if (!token->user_sids) {
4142 sid_copy(&token->user_sids[token->num_sids], sid);
4144 token->num_sids += 1;
4149 NT_USER_TOKEN token;
4152 static void dump_user_token(struct user_token *token)
4156 d_printf("%s\n", token->name);
4158 for (i=0; i<token->token.num_sids; i++) {
4159 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4163 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4167 for (i=0; i<alias->num_members; i++) {
4168 if (sid_compare(sid, &alias->members[i]) == 0)
4175 static void collect_sid_memberships(NT_USER_TOKEN *token, struct dom_sid sid)
4179 for (i=0; i<num_server_aliases; i++) {
4180 if (is_alias_member(&sid, &server_aliases[i]))
4181 add_sid_to_token(token, &server_aliases[i].sid);
4186 * We got a user token with all the SIDs we can know about without asking the
4187 * server directly. These are the user and domain group sids. All of these can
4188 * be members of aliases. So scan the list of aliases for each of the SIDs and
4189 * add them to the token.
4192 static void collect_alias_memberships(NT_USER_TOKEN *token)
4194 int num_global_sids = token->num_sids;
4197 for (i=0; i<num_global_sids; i++) {
4198 collect_sid_memberships(token, token->user_sids[i]);
4202 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4204 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4205 enum wbcSidType type;
4207 struct wbcDomainSid wsid;
4208 char *sid_str = NULL;
4209 struct dom_sid user_sid;
4210 uint32_t num_groups;
4211 gid_t *groups = NULL;
4214 fstr_sprintf(full_name, "%s%c%s",
4215 domain, *lp_winbind_separator(), user);
4217 /* First let's find out the user sid */
4219 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4221 if (!WBC_ERROR_IS_OK(wbc_status)) {
4222 DEBUG(1, ("winbind could not find %s: %s\n",
4223 full_name, wbcErrorString(wbc_status)));
4227 wbc_status = wbcSidToString(&wsid, &sid_str);
4228 if (!WBC_ERROR_IS_OK(wbc_status)) {
4232 if (type != SID_NAME_USER) {
4233 wbcFreeMemory(sid_str);
4234 DEBUG(1, ("%s is not a user\n", full_name));
4238 if (!string_to_sid(&user_sid, sid_str)) {
4239 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4243 wbcFreeMemory(sid_str);
4246 init_user_token(token, &user_sid);
4248 /* And now the groups winbind knows about */
4250 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4251 if (!WBC_ERROR_IS_OK(wbc_status)) {
4252 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4253 full_name, wbcErrorString(wbc_status)));
4257 for (i = 0; i < num_groups; i++) {
4258 gid_t gid = groups[i];
4261 wbc_status = wbcGidToSid(gid, &wsid);
4262 if (!WBC_ERROR_IS_OK(wbc_status)) {
4263 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4264 (unsigned int)gid, wbcErrorString(wbc_status)));
4265 wbcFreeMemory(groups);
4269 wbc_status = wbcSidToString(&wsid, &sid_str);
4270 if (!WBC_ERROR_IS_OK(wbc_status)) {
4271 wbcFreeMemory(groups);
4275 DEBUG(3, (" %s\n", sid_str));
4277 string_to_sid(&sid, sid_str);
4278 wbcFreeMemory(sid_str);
4281 add_sid_to_token(token, &sid);
4283 wbcFreeMemory(groups);
4289 * Get a list of all user tokens we want to look at
4292 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4293 struct user_token **user_tokens)
4295 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4296 uint32_t i, num_users;
4298 struct user_token *result;
4299 TALLOC_CTX *frame = NULL;
4301 if (lp_winbind_use_default_domain() &&
4302 (c->opt_target_workgroup == NULL)) {
4303 d_fprintf(stderr, _("winbind use default domain = yes set, "
4304 "please specify a workgroup\n"));
4308 /* Send request to winbind daemon */
4310 wbc_status = wbcListUsers(NULL, &num_users, &users);
4311 if (!WBC_ERROR_IS_OK(wbc_status)) {
4312 DEBUG(1, (_("winbind could not list users: %s\n"),
4313 wbcErrorString(wbc_status)));
4317 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4319 if (result == NULL) {
4320 DEBUG(1, ("Could not malloc sid array\n"));
4321 wbcFreeMemory(users);
4325 frame = talloc_stackframe();
4326 for (i=0; i < num_users; i++) {
4327 fstring domain, user;
4330 fstrcpy(result[i].name, users[i]);
4332 p = strchr(users[i], *lp_winbind_separator());
4334 DEBUG(3, ("%s\n", users[i]));
4337 fstrcpy(domain, c->opt_target_workgroup);
4338 fstrcpy(user, users[i]);
4341 fstrcpy(domain, users[i]);
4346 get_user_sids(domain, user, &(result[i].token));
4350 wbcFreeMemory(users);
4352 *num_tokens = num_users;
4353 *user_tokens = result;
4358 static bool get_user_tokens_from_file(FILE *f,
4360 struct user_token **tokens)
4362 struct user_token *token = NULL;
4367 if (fgets(line, sizeof(line)-1, f) == NULL) {
4371 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4372 line[strlen(line)-1] = '\0';
4375 if (line[0] == ' ') {
4379 if(!string_to_sid(&sid, &line[1])) {
4380 DEBUG(1,("get_user_tokens_from_file: Could "
4381 "not convert sid %s \n",&line[1]));
4385 if (token == NULL) {
4386 DEBUG(0, ("File does not begin with username"));
4390 add_sid_to_token(&token->token, &sid);
4394 /* And a new user... */
4397 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4398 if (*tokens == NULL) {
4399 DEBUG(0, ("Could not realloc tokens\n"));
4403 token = &((*tokens)[*num_tokens-1]);
4405 fstrcpy(token->name, line);
4406 token->token.num_sids = 0;
4407 token->token.user_sids = NULL;
4416 * Show the list of all users that have access to a share
4419 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4420 TALLOC_CTX *mem_ctx,
4421 const char *netname,
4423 struct user_token *tokens)
4426 struct security_descriptor *share_sd = NULL;
4427 struct security_descriptor *root_sd = NULL;
4428 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4430 union srvsvc_NetShareInfo info;
4435 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4442 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4443 DEBUG(1, ("Coult not query secdesc for share %s\n",
4448 share_sd = info.info502->sd_buf.sd;
4449 if (share_sd == NULL) {
4450 DEBUG(1, ("Got no secdesc for share %s\n",
4456 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4460 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4461 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4462 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4465 for (i=0; i<num_tokens; i++) {
4468 if (share_sd != NULL) {
4469 status = se_access_check(share_sd, &tokens[i].token,
4472 if (!NT_STATUS_IS_OK(status)) {
4473 DEBUG(1, ("Could not check share_sd for "
4480 if (root_sd == NULL) {
4481 d_printf(" %s\n", tokens[i].name);
4485 status = se_access_check(root_sd, &tokens[i].token,
4487 if (!NT_STATUS_IS_OK(status)) {
4488 DEBUG(1, ("Could not check root_sd for user %s\n",
4492 d_printf(" %s\n", tokens[i].name);
4495 if (fnum != (uint16_t)-1)
4496 cli_close(cli, fnum);
4508 static void collect_share(const char *name, uint32 m,
4509 const char *comment, void *state)
4511 struct share_list *share_list = (struct share_list *)state;
4513 if (m != STYPE_DISKTREE)
4516 share_list->num_shares += 1;
4517 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4518 if (!share_list->shares) {
4519 share_list->num_shares = 0;
4522 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4526 * List shares on a remote RPC server, including the security descriptors.
4528 * All parameters are provided by the run_rpc_command function, except for
4529 * argc, argv which are passed through.
4531 * @param domain_sid The domain sid acquired from the remote server.
4532 * @param cli A cli_state connected to the server.
4533 * @param mem_ctx Talloc context, destroyed on completion of the function.
4534 * @param argc Standard main() style argc.
4535 * @param argv Standard main() style argv. Initial components are already
4538 * @return Normal NTSTATUS return.
4541 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4542 const struct dom_sid *domain_sid,
4543 const char *domain_name,
4544 struct cli_state *cli,
4545 struct rpc_pipe_client *pipe_hnd,
4546 TALLOC_CTX *mem_ctx,
4555 struct user_token *tokens = NULL;
4558 struct share_list share_list;
4563 f = fopen(argv[0], "r");
4567 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4568 return NT_STATUS_UNSUCCESSFUL;
4571 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4577 DEBUG(0, ("Could not read users from file\n"));
4578 return NT_STATUS_UNSUCCESSFUL;
4581 for (i=0; i<num_tokens; i++)
4582 collect_alias_memberships(&tokens[i].token);
4584 share_list.num_shares = 0;
4585 share_list.shares = NULL;
4587 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4590 DEBUG(0, ("Error returning browse list: %s\n",
4595 for (i = 0; i < share_list.num_shares; i++) {
4596 char *netname = share_list.shares[i];
4598 if (netname[strlen(netname)-1] == '$')
4601 d_printf("%s\n", netname);
4603 show_userlist(pipe_hnd, mem_ctx, netname,
4604 num_tokens, tokens);
4607 for (i=0; i<num_tokens; i++) {
4608 free_user_token(&tokens[i].token);
4611 SAFE_FREE(share_list.shares);
4613 return NT_STATUS_OK;
4616 static int rpc_share_allowedusers(struct net_context *c, int argc,
4621 if (c->display_usage) {
4623 "net rpc share allowedusers\n"
4626 _("List allowed users"));
4630 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4631 rpc_aliaslist_internals,
4636 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4642 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4643 rpc_share_allowedusers_internals,
4647 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4650 struct user_token *tokens = NULL;
4654 net_usersidlist_usage(c, argc, argv);
4658 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4659 DEBUG(0, ("Could not get the user/sid list\n"));
4663 for (i=0; i<num_tokens; i++) {
4664 dump_user_token(&tokens[i]);
4665 free_user_token(&tokens[i].token);
4672 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4674 d_printf(_("net usersidlist\n"
4675 "\tprints out a list of all users the running winbind knows\n"
4676 "\tabout, together with all their SIDs. This is used as\n"
4677 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4679 net_common_flags_usage(c, argc, argv);
4684 * 'net rpc share' entrypoint.
4685 * @param argc Standard main() style argc.
4686 * @param argv Standard main() style argv. Initial components are already
4690 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4692 NET_API_STATUS status;
4694 struct functable func[] = {
4700 N_("net rpc share add\n"
4708 N_("net rpc share delete\n"
4713 rpc_share_allowedusers,
4715 N_("Modify allowed users"),
4716 N_("net rpc share allowedusers\n"
4717 " Modify allowed users")
4723 N_("Migrate share to local server"),
4724 N_("net rpc share migrate\n"
4725 " Migrate share to local server")
4732 N_("net rpc share list\n"
4735 {NULL, NULL, 0, NULL, NULL}
4738 status = libnetapi_init(&c->netapi_ctx);
4742 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4743 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4744 if (c->opt_kerberos) {
4745 libnetapi_set_use_kerberos(c->netapi_ctx);
4749 if (c->display_usage) {
4754 " Alias for net rpc share list\n"));
4755 net_display_usage_from_functable(func);
4759 return rpc_share_list(c, argc, argv);
4762 return net_run_function(c, argc, argv, "net rpc share", func);
4765 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4766 TALLOC_CTX *mem_ctx,
4767 struct rpc_sh_ctx *ctx,
4768 struct rpc_pipe_client *pipe_hnd,
4769 int argc, const char **argv)
4772 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4775 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4776 TALLOC_CTX *mem_ctx,
4777 struct rpc_sh_ctx *ctx,
4778 struct rpc_pipe_client *pipe_hnd,
4779 int argc, const char **argv)
4781 NET_API_STATUS status;
4782 uint32_t parm_err = 0;
4783 struct SHARE_INFO_2 i2;
4785 if ((argc < 2) || (argc > 3)) {
4786 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
4788 return NT_STATUS_INVALID_PARAMETER;
4791 i2.shi2_netname = argv[0];
4792 i2.shi2_type = STYPE_DISKTREE;
4793 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4794 i2.shi2_permissions = 0;
4795 i2.shi2_max_uses = 0;
4796 i2.shi2_current_uses = 0;
4797 i2.shi2_path = argv[1];
4798 i2.shi2_passwd = NULL;
4800 status = NetShareAdd(pipe_hnd->desthost,
4805 return werror_to_ntstatus(W_ERROR(status));
4808 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4809 TALLOC_CTX *mem_ctx,
4810 struct rpc_sh_ctx *ctx,
4811 struct rpc_pipe_client *pipe_hnd,
4812 int argc, const char **argv)
4815 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4816 return NT_STATUS_INVALID_PARAMETER;
4819 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4822 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4823 TALLOC_CTX *mem_ctx,
4824 struct rpc_sh_ctx *ctx,
4825 struct rpc_pipe_client *pipe_hnd,
4826 int argc, const char **argv)
4828 union srvsvc_NetShareInfo info;
4833 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4834 return NT_STATUS_INVALID_PARAMETER;
4837 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4843 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4847 d_printf(_("Name: %s\n"), info.info2->name);
4848 d_printf(_("Comment: %s\n"), info.info2->comment);
4849 d_printf(_("Path: %s\n"), info.info2->path);
4850 d_printf(_("Password: %s\n"), info.info2->password);
4853 return werror_to_ntstatus(result);
4856 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4857 struct rpc_sh_ctx *ctx)
4859 static struct rpc_sh_cmd cmds[] = {
4861 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4862 N_("List available shares") },
4864 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4865 N_("Add a share") },
4867 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4868 N_("Delete a share") },
4870 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4871 N_("Get information about a share") },
4873 { NULL, NULL, 0, NULL, NULL }
4879 /****************************************************************************/
4881 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4883 return net_file_usage(c, argc, argv);
4887 * Close a file on a remote RPC server.
4889 * @param argc Standard main() style argc.
4890 * @param argv Standard main() style argv. Initial components are already
4893 * @return A shell status integer (0 for success).
4895 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4897 if (argc < 1 || c->display_usage) {
4898 return rpc_file_usage(c, argc, argv);
4901 return NetFileClose(c->opt_host, atoi(argv[0]));
4905 * Formatted print of open file info
4907 * @param r struct FILE_INFO_3 contents
4910 static void display_file_info_3(struct FILE_INFO_3 *r)
4912 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4913 r->fi3_id, r->fi3_username, r->fi3_permissions,
4914 r->fi3_num_locks, r->fi3_pathname);
4918 * List files for a user on a remote RPC server.
4920 * @param argc Standard main() style argc.
4921 * @param argv Standard main() style argv. Initial components are already
4924 * @return A shell status integer (0 for success)..
4927 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4929 NET_API_STATUS status;
4930 uint32 preferred_len = 0xffffffff, i;
4931 const char *username=NULL;
4932 uint32_t total_entries = 0;
4933 uint32_t entries_read = 0;
4934 uint32_t resume_handle = 0;
4935 struct FILE_INFO_3 *i3 = NULL;
4937 if (c->display_usage) {
4938 return rpc_file_usage(c, argc, argv);
4941 /* if argc > 0, must be user command */
4943 username = smb_xstrdup(argv[0]);
4946 status = NetFileEnum(c->opt_host,
4950 (uint8_t **)(void *)&i3,
4960 /* Display results */
4963 "\nEnumerating open files on remote server:\n\n"
4964 "\nFileId Opened by Perms Locks Path"
4965 "\n------ --------- ----- ----- ---- \n"));
4966 for (i = 0; i < entries_read; i++) {
4967 display_file_info_3(&i3[i]);
4974 * 'net rpc file' entrypoint.
4975 * @param argc Standard main() style argc.
4976 * @param argv Standard main() style argv. Initial components are already
4980 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4982 NET_API_STATUS status;
4984 struct functable func[] = {
4989 N_("Close opened file"),
4990 N_("net rpc file close\n"
4991 " Close opened file")
4997 N_("List files opened by user"),
4998 N_("net rpc file user\n"
4999 " List files opened by user")
5006 N_("Display information about opened file"),
5007 N_("net rpc file info\n"
5008 " Display information about opened file")
5011 {NULL, NULL, 0, NULL, NULL}
5014 status = libnetapi_init(&c->netapi_ctx);
5018 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5019 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5020 if (c->opt_kerberos) {
5021 libnetapi_set_use_kerberos(c->netapi_ctx);
5025 if (c->display_usage) {
5026 d_printf(_("Usage:\n"));
5027 d_printf(_("net rpc file\n"
5028 " List opened files\n"));
5029 net_display_usage_from_functable(func);
5033 return rpc_file_user(c, argc, argv);
5036 return net_run_function(c, argc, argv, "net rpc file", func);
5040 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5042 * All parameters are provided by the run_rpc_command function, except for
5043 * argc, argv which are passed through.
5045 * @param c A net_context structure.
5046 * @param domain_sid The domain sid acquired from the remote server.
5047 * @param cli A cli_state connected to the server.
5048 * @param mem_ctx Talloc context, destroyed on completion of the function.
5049 * @param argc Standard main() style argc.
5050 * @param argv Standard main() style argv. Initial components are already
5053 * @return Normal NTSTATUS return.
5056 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5057 const struct dom_sid *domain_sid,
5058 const char *domain_name,
5059 struct cli_state *cli,
5060 struct rpc_pipe_client *pipe_hnd,
5061 TALLOC_CTX *mem_ctx,
5065 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5067 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5069 if (NT_STATUS_IS_OK(result)) {
5070 d_printf(_("\nShutdown successfully aborted\n"));
5071 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5073 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5079 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5081 * All parameters are provided by the run_rpc_command function, except for
5082 * argc, argv which are passed through.
5084 * @param c A net_context structure.
5085 * @param domain_sid The domain sid acquired from the remote server.
5086 * @param cli A cli_state connected to the server.
5087 * @param mem_ctx Talloc context, destroyed on completion of the function.
5088 * @param argc Standard main() style argc.
5089 * @param argv Standard main() style argv. Initial components are already
5092 * @return Normal NTSTATUS return.
5095 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5096 const struct dom_sid *domain_sid,
5097 const char *domain_name,
5098 struct cli_state *cli,
5099 struct rpc_pipe_client *pipe_hnd,
5100 TALLOC_CTX *mem_ctx,
5104 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5106 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5108 if (NT_STATUS_IS_OK(result)) {
5109 d_printf(_("\nShutdown successfully aborted\n"));
5110 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5112 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5118 * ABORT the shutdown of a remote RPC server.
5120 * @param argc Standard main() style argc.
5121 * @param argv Standard main() style argv. Initial components are already
5124 * @return A shell status integer (0 for success).
5127 static int rpc_shutdown_abort(struct net_context *c, int argc,
5132 if (c->display_usage) {
5134 "net rpc abortshutdown\n"
5137 _("Abort a scheduled shutdown"));
5141 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5142 rpc_shutdown_abort_internals, argc, argv);
5147 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5149 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5150 rpc_reg_shutdown_abort_internals,
5155 * Shut down a remote RPC Server via initshutdown pipe.
5157 * All parameters are provided by the run_rpc_command function, except for
5158 * argc, argv which are passed through.
5160 * @param c A net_context structure.
5161 * @param domain_sid The domain sid acquired from the remote server.
5162 * @param cli A cli_state connected to the server.
5163 * @param mem_ctx Talloc context, destroyed on completion of the function.
5164 * @param argc Standard main() style argc.
5165 * @param argv Standard main() style argv. Initial components are already
5168 * @return Normal NTSTATUS return.
5171 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5172 const struct dom_sid *domain_sid,
5173 const char *domain_name,
5174 struct cli_state *cli,
5175 struct rpc_pipe_client *pipe_hnd,
5176 TALLOC_CTX *mem_ctx,
5180 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5181 const char *msg = N_("This machine will be shutdown shortly");
5182 uint32 timeout = 20;
5183 struct lsa_StringLarge msg_string;
5185 if (c->opt_comment) {
5186 msg = c->opt_comment;
5188 if (c->opt_timeout) {
5189 timeout = c->opt_timeout;
5192 msg_string.string = msg;
5194 /* create an entry */
5195 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5196 &msg_string, timeout, c->opt_force, c->opt_reboot,
5199 if (NT_STATUS_IS_OK(result)) {
5200 d_printf(_("\nShutdown of remote machine succeeded\n"));
5201 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5203 DEBUG(1,("Shutdown of remote machine failed!\n"));
5209 * Shut down a remote RPC Server via winreg pipe.
5211 * All parameters are provided by the run_rpc_command function, except for
5212 * argc, argv which are passed through.
5214 * @param c A net_context structure.
5215 * @param domain_sid The domain sid acquired from the remote server.
5216 * @param cli A cli_state connected to the server.
5217 * @param mem_ctx Talloc context, destroyed on completion of the function.
5218 * @param argc Standard main() style argc.
5219 * @param argv Standard main() style argv. Initial components are already
5222 * @return Normal NTSTATUS return.
5225 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5226 const struct dom_sid *domain_sid,
5227 const char *domain_name,
5228 struct cli_state *cli,
5229 struct rpc_pipe_client *pipe_hnd,
5230 TALLOC_CTX *mem_ctx,
5234 const char *msg = N_("This machine will be shutdown shortly");
5235 uint32 timeout = 20;
5236 struct lsa_StringLarge msg_string;
5240 if (c->opt_comment) {
5241 msg = c->opt_comment;
5243 msg_string.string = msg;
5245 if (c->opt_timeout) {
5246 timeout = c->opt_timeout;
5249 /* create an entry */
5250 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5251 &msg_string, timeout, c->opt_force, c->opt_reboot,
5254 if (NT_STATUS_IS_OK(result)) {
5255 d_printf(_("\nShutdown of remote machine succeeded\n"));
5257 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5258 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5259 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5261 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5268 * Shut down a remote RPC server.
5270 * @param argc Standard main() style argc.
5271 * @param argv Standard main() style argv. Initial components are already
5274 * @return A shell status integer (0 for success).
5277 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5281 if (c->display_usage) {
5283 "net rpc shutdown\n"
5286 _("Shut down a remote RPC server"));
5290 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5291 rpc_init_shutdown_internals, argc, argv);
5294 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5295 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5296 rpc_reg_shutdown_internals, argc, argv);
5302 /***************************************************************************
5303 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5304 ***************************************************************************/
5307 * Add interdomain trust account to the RPC server.
5308 * All parameters (except for argc and argv) are passed by run_rpc_command
5311 * @param c A net_context structure.
5312 * @param domain_sid The domain sid acquired from the server.
5313 * @param cli A cli_state connected to the server.
5314 * @param mem_ctx Talloc context, destroyed on completion of the function.
5315 * @param argc Standard main() style argc.
5316 * @param argv Standard main() style argv. Initial components are already
5319 * @return normal NTSTATUS return code.
5322 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5323 const struct dom_sid *domain_sid,
5324 const char *domain_name,
5325 struct cli_state *cli,
5326 struct rpc_pipe_client *pipe_hnd,
5327 TALLOC_CTX *mem_ctx,
5331 struct policy_handle connect_pol, domain_pol, user_pol;
5332 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5334 struct lsa_String lsa_acct_name;
5336 uint32 acct_flags=0;
5338 uint32_t access_granted = 0;
5339 union samr_UserInfo info;
5340 unsigned int orig_timeout;
5345 _(" net rpc trustdom add <domain_name> "
5346 "<trust password>\n"));
5347 return NT_STATUS_INVALID_PARAMETER;
5351 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5354 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5355 return NT_STATUS_NO_MEMORY;
5358 strupper_m(acct_name);
5360 init_lsa_String(&lsa_acct_name, acct_name);
5362 /* Get samr policy handle */
5363 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5365 MAXIMUM_ALLOWED_ACCESS,
5367 if (!NT_STATUS_IS_OK(result)) {
5371 /* Get domain policy handle */
5372 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5374 MAXIMUM_ALLOWED_ACCESS,
5375 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5377 if (!NT_STATUS_IS_OK(result)) {
5381 /* This call can take a long time - allow the server to time out.
5382 * 35 seconds should do it. */
5384 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5386 /* Create trusting domain's account */
5387 acb_info = ACB_NORMAL;
5388 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5389 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5390 SAMR_USER_ACCESS_SET_PASSWORD |
5391 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5392 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5394 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5403 /* And restore our original timeout. */
5404 rpccli_set_timeout(pipe_hnd, orig_timeout);
5406 if (!NT_STATUS_IS_OK(result)) {
5407 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5408 acct_name, nt_errstr(result));
5413 struct samr_CryptPassword crypt_pwd;
5415 ZERO_STRUCT(info.info23);
5417 init_samr_CryptPassword(argv[1],
5418 &cli->user_session_key,
5421 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5422 SAMR_FIELD_NT_PASSWORD_PRESENT;
5423 info.info23.info.acct_flags = ACB_DOMTRUST;
5424 info.info23.password = crypt_pwd;
5426 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5431 if (!NT_STATUS_IS_OK(result)) {
5432 DEBUG(0,("Could not set trust account password: %s\n",
5433 nt_errstr(result)));
5439 SAFE_FREE(acct_name);
5444 * Create interdomain trust account for a remote domain.
5446 * @param argc Standard argc.
5447 * @param argv Standard argv without initial components.
5449 * @return Integer status (0 means success).
5452 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5454 if (argc > 0 && !c->display_usage) {
5455 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5456 rpc_trustdom_add_internals, argc, argv);
5460 _("net rpc trustdom add <domain_name> <trust "
5468 * Remove interdomain trust account from the RPC server.
5469 * All parameters (except for argc and argv) are passed by run_rpc_command
5472 * @param c A net_context structure.
5473 * @param domain_sid The domain sid acquired from the server.
5474 * @param cli A cli_state connected to the server.
5475 * @param mem_ctx Talloc context, destroyed on completion of the function.
5476 * @param argc Standard main() style argc.
5477 * @param argv Standard main() style argv. Initial components are already
5480 * @return normal NTSTATUS return code.
5483 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5484 const struct dom_sid *domain_sid,
5485 const char *domain_name,
5486 struct cli_state *cli,
5487 struct rpc_pipe_client *pipe_hnd,
5488 TALLOC_CTX *mem_ctx,
5492 struct policy_handle connect_pol, domain_pol, user_pol;
5493 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5495 struct dom_sid trust_acct_sid;
5496 struct samr_Ids user_rids, name_types;
5497 struct lsa_String lsa_acct_name;
5502 _(" net rpc trustdom del <domain_name>\n"));
5503 return NT_STATUS_INVALID_PARAMETER;
5507 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5509 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5511 if (acct_name == NULL)
5512 return NT_STATUS_NO_MEMORY;
5514 strupper_m(acct_name);
5516 /* Get samr policy handle */
5517 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5519 MAXIMUM_ALLOWED_ACCESS,
5521 if (!NT_STATUS_IS_OK(result)) {
5525 /* Get domain policy handle */
5526 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5528 MAXIMUM_ALLOWED_ACCESS,
5529 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5531 if (!NT_STATUS_IS_OK(result)) {
5535 init_lsa_String(&lsa_acct_name, acct_name);
5537 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5544 if (!NT_STATUS_IS_OK(result)) {
5545 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5547 acct_name, nt_errstr(result) );
5551 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5553 MAXIMUM_ALLOWED_ACCESS,
5557 if (!NT_STATUS_IS_OK(result)) {
5558 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5560 acct_name, nt_errstr(result) );
5564 /* append the rid to the domain sid */
5565 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
5569 /* remove the sid */
5571 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5574 if (!NT_STATUS_IS_OK(result)) {
5575 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5576 " on user %s failed %s\n"),
5577 acct_name, nt_errstr(result) );
5583 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5586 if (!NT_STATUS_IS_OK(result)) {
5587 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5589 acct_name, nt_errstr(result) );
5593 if (!NT_STATUS_IS_OK(result)) {
5594 d_printf(_("Could not set trust account password: %s\n"),
5604 * Delete interdomain trust account for a remote domain.
5606 * @param argc Standard argc.
5607 * @param argv Standard argv without initial components.
5609 * @return Integer status (0 means success).
5612 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5614 if (argc > 0 && !c->display_usage) {
5615 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5616 rpc_trustdom_del_internals, argc, argv);
5620 _("net rpc trustdom del <domain>\n"));
5625 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5626 struct cli_state *cli,
5627 TALLOC_CTX *mem_ctx,
5628 const char *domain_name)
5630 char *dc_name = NULL;
5631 const char *buffer = NULL;
5632 struct rpc_pipe_client *netr;
5635 /* Use NetServerEnum2 */
5637 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5639 return NT_STATUS_OK;
5642 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5643 for domain %s\n", domain_name));
5645 /* Try netr_GetDcName */
5647 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5649 if (!NT_STATUS_IS_OK(status)) {
5653 status = rpccli_netr_GetDcName(netr, mem_ctx,
5660 if (NT_STATUS_IS_OK(status)) {
5664 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5665 for domain %s\n", domain_name));
5671 * Establish trust relationship to a trusting domain.
5672 * Interdomain account must already be created on remote PDC.
5674 * @param c A net_context structure.
5675 * @param argc Standard argc.
5676 * @param argv Standard argv without initial components.
5678 * @return Integer status (0 means success).
5681 static int rpc_trustdom_establish(struct net_context *c, int argc,
5684 struct cli_state *cli = NULL;
5685 struct sockaddr_storage server_ss;
5686 struct rpc_pipe_client *pipe_hnd = NULL;
5687 struct policy_handle connect_hnd;
5688 TALLOC_CTX *mem_ctx;
5690 struct dom_sid *domain_sid;
5695 union lsa_PolicyInformation *info = NULL;
5698 * Connect to \\server\ipc$ as 'our domain' account with password
5701 if (argc != 1 || c->display_usage) {
5704 _("net rpc trustdom establish <domain_name>\n"));
5708 domain_name = smb_xstrdup(argv[0]);
5709 strupper_m(domain_name);
5711 /* account name used at first is our domain's name with '$' */
5712 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5715 strupper_m(acct_name);
5718 * opt_workgroup will be used by connection functions further,
5719 * hence it should be set to remote domain name instead of ours
5721 if (c->opt_workgroup) {
5722 c->opt_workgroup = smb_xstrdup(domain_name);
5725 c->opt_user_name = acct_name;
5727 /* find the domain controller */
5728 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5729 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5733 /* connect to ipc$ as username/password */
5734 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5735 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5737 /* Is it trusting domain account for sure ? */
5738 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5739 nt_errstr(nt_status)));
5743 /* store who we connected to */
5745 saf_store( domain_name, pdc_name );
5748 * Connect to \\server\ipc$ again (this time anonymously)
5751 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5754 if (NT_STATUS_IS_ERR(nt_status)) {
5755 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5756 domain_name, nt_errstr(nt_status)));
5760 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5761 "domain %s", domain_name))) {
5762 DEBUG(0, ("talloc_init() failed\n"));
5767 /* Make sure we're talking to a proper server */
5769 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5770 if (!NT_STATUS_IS_OK(nt_status)) {
5772 talloc_destroy(mem_ctx);
5777 * Call LsaOpenPolicy and LsaQueryInfo
5780 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5782 if (!NT_STATUS_IS_OK(nt_status)) {
5783 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5785 talloc_destroy(mem_ctx);
5789 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5791 if (NT_STATUS_IS_ERR(nt_status)) {
5792 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5793 nt_errstr(nt_status)));
5795 talloc_destroy(mem_ctx);
5799 /* Querying info level 5 */
5801 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5803 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5805 if (NT_STATUS_IS_ERR(nt_status)) {
5806 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5807 nt_errstr(nt_status)));
5809 talloc_destroy(mem_ctx);
5813 domain_sid = info->account_domain.sid;
5815 /* There should be actually query info level 3 (following nt serv behaviour),
5816 but I still don't know if it's _really_ necessary */
5819 * Store the password in secrets db
5822 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5823 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5825 talloc_destroy(mem_ctx);
5830 * Close the pipes and clean up
5833 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5834 if (NT_STATUS_IS_ERR(nt_status)) {
5835 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5836 nt_errstr(nt_status)));
5838 talloc_destroy(mem_ctx);
5844 talloc_destroy(mem_ctx);
5846 d_printf(_("Trust to domain %s established\n"), domain_name);
5851 * Revoke trust relationship to the remote domain.
5853 * @param c A net_context structure.
5854 * @param argc Standard argc.
5855 * @param argv Standard argv without initial components.
5857 * @return Integer status (0 means success).
5860 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5866 if (argc < 1 || c->display_usage) {
5869 _("net rpc trustdom revoke <domain_name>\n"
5870 " Revoke trust relationship\n"
5871 " domain_name\tName of domain to revoke trust\n"));
5875 /* generate upper cased domain name */
5876 domain_name = smb_xstrdup(argv[0]);
5877 strupper_m(domain_name);
5879 /* delete password of the trust */
5880 if (!pdb_del_trusteddom_pw(domain_name)) {
5881 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5888 SAFE_FREE(domain_name);
5892 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5893 const struct dom_sid *domain_sid,
5894 const char *domain_name,
5895 struct cli_state *cli,
5896 struct rpc_pipe_client *pipe_hnd,
5897 TALLOC_CTX *mem_ctx,
5902 if (!sid_to_fstring(str_sid, domain_sid)) {
5903 return NT_STATUS_UNSUCCESSFUL;
5905 d_printf("%s\n", str_sid);
5906 return NT_STATUS_OK;
5909 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
5913 /* convert sid into ascii string */
5914 sid_to_fstring(ascii_sid, dom_sid);
5916 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5919 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5920 TALLOC_CTX *mem_ctx,
5921 struct policy_handle *pol,
5922 struct dom_sid dom_sid,
5923 const char *trusted_dom_name)
5926 union lsa_TrustedDomainInfo *info = NULL;
5927 char *cleartextpwd = NULL;
5928 uint8_t session_key[16];
5929 DATA_BLOB session_key_blob;
5930 DATA_BLOB data = data_blob_null;
5932 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5935 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5937 if (NT_STATUS_IS_ERR(nt_status)) {
5938 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5939 nt_errstr(nt_status)));
5943 data = data_blob(info->password.password->data,
5944 info->password.password->length);
5946 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5947 DEBUG(0, ("Could not retrieve password hash\n"));
5951 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5952 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5954 if (cleartextpwd == NULL) {
5955 DEBUG(0,("retrieved NULL password\n"));
5956 nt_status = NT_STATUS_UNSUCCESSFUL;
5960 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5961 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5962 nt_status = NT_STATUS_UNSUCCESSFUL;
5966 #ifdef DEBUG_PASSWORD
5967 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5968 "password: [%s]\n", trusted_dom_name,
5969 sid_string_dbg(&dom_sid), cleartextpwd));
5973 SAFE_FREE(cleartextpwd);
5974 data_blob_free(&data);
5979 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5982 /* common variables */
5983 TALLOC_CTX* mem_ctx;
5984 struct cli_state *cli = NULL;
5985 struct rpc_pipe_client *pipe_hnd = NULL;
5987 const char *domain_name = NULL;
5988 struct dom_sid *queried_dom_sid;
5989 struct policy_handle connect_hnd;
5990 union lsa_PolicyInformation *info = NULL;
5992 /* trusted domains listing variables */
5993 unsigned int enum_ctx = 0;
5995 struct lsa_DomainList dom_list;
5998 if (c->display_usage) {
6000 "net rpc trustdom vampire\n"
6003 _("Vampire trust relationship from remote server"));
6008 * Listing trusted domains (stored in secrets.tdb, if local)
6011 mem_ctx = talloc_init("trust relationships vampire");
6014 * set domain and pdc name to local samba server (default)
6015 * or to remote one given in command line
6018 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6019 domain_name = c->opt_workgroup;
6020 c->opt_target_workgroup = c->opt_workgroup;
6022 fstrcpy(pdc_name, global_myname());
6023 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6024 c->opt_target_workgroup = domain_name;
6027 /* open \PIPE\lsarpc and open policy handle */
6028 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6029 if (!NT_STATUS_IS_OK(nt_status)) {
6030 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6031 nt_errstr(nt_status)));
6032 talloc_destroy(mem_ctx);
6036 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6038 if (!NT_STATUS_IS_OK(nt_status)) {
6039 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6040 nt_errstr(nt_status) ));
6042 talloc_destroy(mem_ctx);
6046 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6048 if (NT_STATUS_IS_ERR(nt_status)) {
6049 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6050 nt_errstr(nt_status)));
6052 talloc_destroy(mem_ctx);
6056 /* query info level 5 to obtain sid of a domain being queried */
6057 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6059 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6062 if (NT_STATUS_IS_ERR(nt_status)) {
6063 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6064 nt_errstr(nt_status)));
6066 talloc_destroy(mem_ctx);
6070 queried_dom_sid = info->account_domain.sid;
6073 * Keep calling LsaEnumTrustdom over opened pipe until
6074 * the end of enumeration is reached
6077 d_printf(_("Vampire trusted domains:\n\n"));
6080 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6085 if (NT_STATUS_IS_ERR(nt_status)) {
6086 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6087 nt_errstr(nt_status)));
6089 talloc_destroy(mem_ctx);
6093 for (i = 0; i < dom_list.count; i++) {
6095 print_trusted_domain(dom_list.domains[i].sid,
6096 dom_list.domains[i].name.string);
6098 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6099 *dom_list.domains[i].sid,
6100 dom_list.domains[i].name.string);
6101 if (!NT_STATUS_IS_OK(nt_status)) {
6103 talloc_destroy(mem_ctx);
6109 * in case of no trusted domains say something rather
6110 * than just display blank line
6112 if (!dom_list.count) d_printf(_("none\n"));
6114 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6116 /* close this connection before doing next one */
6117 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6118 if (NT_STATUS_IS_ERR(nt_status)) {
6119 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6120 nt_errstr(nt_status)));
6122 talloc_destroy(mem_ctx);
6126 /* close lsarpc pipe and connection to IPC$ */
6129 talloc_destroy(mem_ctx);
6133 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6135 /* common variables */
6136 TALLOC_CTX* mem_ctx;
6137 struct cli_state *cli = NULL, *remote_cli = NULL;
6138 struct rpc_pipe_client *pipe_hnd = NULL;
6140 const char *domain_name = NULL;
6141 struct dom_sid *queried_dom_sid;
6142 int ascii_dom_name_len;
6143 struct policy_handle connect_hnd;
6144 union lsa_PolicyInformation *info = NULL;
6146 /* trusted domains listing variables */
6147 unsigned int num_domains, enum_ctx = 0;
6149 struct lsa_DomainList dom_list;
6153 /* trusting domains listing variables */
6154 struct policy_handle domain_hnd;
6155 struct samr_SamArray *trusts = NULL;
6157 if (c->display_usage) {
6159 "net rpc trustdom list\n"
6162 _("List incoming and outgoing trust relationships"));
6167 * Listing trusted domains (stored in secrets.tdb, if local)
6170 mem_ctx = talloc_init("trust relationships listing");
6173 * set domain and pdc name to local samba server (default)
6174 * or to remote one given in command line
6177 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6178 domain_name = c->opt_workgroup;
6179 c->opt_target_workgroup = c->opt_workgroup;
6181 fstrcpy(pdc_name, global_myname());
6182 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6183 c->opt_target_workgroup = domain_name;
6186 /* open \PIPE\lsarpc and open policy handle */
6187 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6188 if (!NT_STATUS_IS_OK(nt_status)) {
6189 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6190 nt_errstr(nt_status)));
6191 talloc_destroy(mem_ctx);
6195 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6197 if (!NT_STATUS_IS_OK(nt_status)) {
6198 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6199 nt_errstr(nt_status) ));
6201 talloc_destroy(mem_ctx);
6205 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6207 if (NT_STATUS_IS_ERR(nt_status)) {
6208 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6209 nt_errstr(nt_status)));
6211 talloc_destroy(mem_ctx);
6215 /* query info level 5 to obtain sid of a domain being queried */
6216 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6218 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6221 if (NT_STATUS_IS_ERR(nt_status)) {
6222 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6223 nt_errstr(nt_status)));
6225 talloc_destroy(mem_ctx);
6229 queried_dom_sid = info->account_domain.sid;
6232 * Keep calling LsaEnumTrustdom over opened pipe until
6233 * the end of enumeration is reached
6236 d_printf(_("Trusted domains list:\n\n"));
6238 found_domain = false;
6241 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6246 if (NT_STATUS_IS_ERR(nt_status)) {
6247 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6248 nt_errstr(nt_status)));
6250 talloc_destroy(mem_ctx);
6254 for (i = 0; i < dom_list.count; i++) {
6255 print_trusted_domain(dom_list.domains[i].sid,
6256 dom_list.domains[i].name.string);
6257 found_domain = true;
6261 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6264 * in case of no trusted domains say something rather
6265 * than just display blank line
6267 if (!found_domain) {
6268 d_printf(_("none\n"));
6271 /* close this connection before doing next one */
6272 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6273 if (NT_STATUS_IS_ERR(nt_status)) {
6274 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6275 nt_errstr(nt_status)));
6277 talloc_destroy(mem_ctx);
6281 TALLOC_FREE(pipe_hnd);
6284 * Listing trusting domains (stored in passdb backend, if local)
6287 d_printf(_("\nTrusting domains list:\n\n"));
6290 * Open \PIPE\samr and get needed policy handles
6292 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6294 if (!NT_STATUS_IS_OK(nt_status)) {
6295 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6297 talloc_destroy(mem_ctx);
6302 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6304 SAMR_ACCESS_LOOKUP_DOMAIN,
6306 if (!NT_STATUS_IS_OK(nt_status)) {
6307 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6308 nt_errstr(nt_status)));
6310 talloc_destroy(mem_ctx);
6314 /* SamrOpenDomain - we have to open domain policy handle in order to be
6315 able to enumerate accounts*/
6316 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6318 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6321 if (!NT_STATUS_IS_OK(nt_status)) {
6322 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6323 nt_errstr(nt_status)));
6325 talloc_destroy(mem_ctx);
6330 * perform actual enumeration
6333 found_domain = false;
6335 enum_ctx = 0; /* reset enumeration context from last enumeration */
6338 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6345 if (NT_STATUS_IS_ERR(nt_status)) {
6346 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6347 nt_errstr(nt_status)));
6349 talloc_destroy(mem_ctx);
6353 for (i = 0; i < num_domains; i++) {
6355 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6357 found_domain = true;
6360 * get each single domain's sid (do we _really_ need this ?):
6361 * 1) connect to domain's pdc
6362 * 2) query the pdc for domain's sid
6365 /* get rid of '$' tail */
6366 ascii_dom_name_len = strlen(str);
6367 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6368 str[ascii_dom_name_len - 1] = '\0';
6370 /* set opt_* variables to remote domain */
6372 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6373 c->opt_target_workgroup = c->opt_workgroup;
6375 d_printf("%-20s", str);
6377 /* connect to remote domain controller */
6378 nt_status = net_make_ipc_connection(c,
6379 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6381 if (NT_STATUS_IS_OK(nt_status)) {
6382 /* query for domain's sid */
6383 if (run_rpc_command(
6385 &ndr_table_lsarpc.syntax_id, 0,
6386 rpc_query_domain_sid, argc,
6388 d_printf(_("strange - couldn't get domain's sid\n"));
6390 cli_shutdown(remote_cli);
6393 d_fprintf(stderr, _("domain controller is not "
6394 "responding: %s\n"),
6395 nt_errstr(nt_status));
6396 d_printf(_("couldn't get domain's sid\n"));
6400 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6402 if (!found_domain) {
6406 /* close opened samr and domain policy handles */
6407 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6408 if (!NT_STATUS_IS_OK(nt_status)) {
6409 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6412 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6413 if (!NT_STATUS_IS_OK(nt_status)) {
6414 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6417 /* close samr pipe and connection to IPC$ */
6420 talloc_destroy(mem_ctx);
6425 * Entrypoint for 'net rpc trustdom' code.
6427 * @param argc Standard argc.
6428 * @param argv Standard argv without initial components.
6430 * @return Integer status (0 means success).
6433 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6435 struct functable func[] = {
6440 N_("Add trusting domain's account"),
6441 N_("net rpc trustdom add\n"
6442 " Add trusting domain's account")
6448 N_("Remove trusting domain's account"),
6449 N_("net rpc trustdom del\n"
6450 " Remove trusting domain's account")
6454 rpc_trustdom_establish,
6456 N_("Establish outgoing trust relationship"),
6457 N_("net rpc trustdom establish\n"
6458 " Establish outgoing trust relationship")
6462 rpc_trustdom_revoke,
6464 N_("Revoke outgoing trust relationship"),
6465 N_("net rpc trustdom revoke\n"
6466 " Revoke outgoing trust relationship")
6472 N_("List in- and outgoing domain trusts"),
6473 N_("net rpc trustdom list\n"
6474 " List in- and outgoing domain trusts")
6478 rpc_trustdom_vampire,
6480 N_("Vampire trusts from remote server"),
6481 N_("net rpc trustdom vampire\n"
6482 " Vampire trusts from remote server")
6484 {NULL, NULL, 0, NULL, NULL}
6487 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6491 * Check if a server will take rpc commands
6492 * @param flags Type of server to connect to (PDC, DMB, localhost)
6493 * if the host is not explicitly specified
6494 * @return bool (true means rpc supported)
6496 bool net_rpc_check(struct net_context *c, unsigned flags)
6498 struct cli_state *cli;
6500 struct sockaddr_storage server_ss;
6501 char *server_name = NULL;
6504 /* flags (i.e. server type) may depend on command */
6505 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6508 if ((cli = cli_initialise()) == NULL) {
6512 status = cli_connect(cli, server_name, &server_ss);
6513 if (!NT_STATUS_IS_OK(status))
6515 if (!attempt_netbios_session_request(&cli, global_myname(),
6516 server_name, &server_ss))
6518 status = cli_negprot(cli);
6519 if (!NT_STATUS_IS_OK(status))
6521 if (cli->protocol < PROTOCOL_NT1)
6530 /* dump sam database via samsync rpc calls */
6531 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6532 if (c->display_usage) {
6537 _("Dump remote SAM database"));
6541 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6542 NET_FLAGS_ANONYMOUS,
6543 rpc_samdump_internals, argc, argv);
6546 /* syncronise sam database via samsync rpc calls */
6547 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6549 struct functable func[] = {
6554 N_("Dump remote SAM database to ldif"),
6555 N_("net rpc vampire ldif\n"
6556 " Dump remote SAM database to LDIF file or "
6563 N_("Dump remote SAM database to Kerberos Keytab"),
6564 N_("net rpc vampire keytab\n"
6565 " Dump remote SAM database to Kerberos keytab "
6572 N_("Dump remote SAM database to passdb"),
6573 N_("net rpc vampire passdb\n"
6574 " Dump remote SAM database to passdb")
6577 {NULL, NULL, 0, NULL, NULL}
6581 if (c->display_usage) {
6586 _("Vampire remote SAM database"));
6590 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6591 NET_FLAGS_ANONYMOUS,
6592 rpc_vampire_internals,
6596 return net_run_function(c, argc, argv, "net rpc vampire", func);
6600 * Migrate everything from a print server.
6602 * @param c A net_context structure.
6603 * @param argc Standard main() style argc.
6604 * @param argv Standard main() style argv. Initial components are already
6607 * @return A shell status integer (0 for success).
6609 * The order is important !
6610 * To successfully add drivers the print queues have to exist !
6611 * Applying ACLs should be the last step, because you're easily locked out.
6614 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6619 if (c->display_usage) {
6621 "net rpc printer migrate all\n"
6624 _("Migrate everything from a print server"));
6629 d_printf(_("no server to migrate\n"));
6633 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6634 rpc_printer_migrate_printers_internals, argc,
6639 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6640 rpc_printer_migrate_drivers_internals, argc,
6645 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6646 rpc_printer_migrate_forms_internals, argc, argv);
6650 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6651 rpc_printer_migrate_settings_internals, argc,
6656 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6657 rpc_printer_migrate_security_internals, argc,
6663 * Migrate print drivers from a print server.
6665 * @param c A net_context structure.
6666 * @param argc Standard main() style argc.
6667 * @param argv Standard main() style argv. Initial components are already
6670 * @return A shell status integer (0 for success).
6672 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6675 if (c->display_usage) {
6677 "net rpc printer migrate drivers\n"
6680 _("Migrate print-drivers from a print-server"));
6685 d_printf(_("no server to migrate\n"));
6689 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6690 rpc_printer_migrate_drivers_internals,
6695 * Migrate print-forms from a print-server.
6697 * @param c A net_context structure.
6698 * @param argc Standard main() style argc.
6699 * @param argv Standard main() style argv. Initial components are already
6702 * @return A shell status integer (0 for success).
6704 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6707 if (c->display_usage) {
6709 "net rpc printer migrate forms\n"
6712 _("Migrate print-forms from a print-server"));
6717 d_printf(_("no server to migrate\n"));
6721 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6722 rpc_printer_migrate_forms_internals,
6727 * Migrate printers from a print-server.
6729 * @param c A net_context structure.
6730 * @param argc Standard main() style argc.
6731 * @param argv Standard main() style argv. Initial components are already
6734 * @return A shell status integer (0 for success).
6736 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6739 if (c->display_usage) {
6741 "net rpc printer migrate printers\n"
6744 _("Migrate printers from a print-server"));
6749 d_printf(_("no server to migrate\n"));
6753 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6754 rpc_printer_migrate_printers_internals,
6759 * Migrate printer-ACLs from a print-server
6761 * @param c A net_context structure.
6762 * @param argc Standard main() style argc.
6763 * @param argv Standard main() style argv. Initial components are already
6766 * @return A shell status integer (0 for success).
6768 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6771 if (c->display_usage) {
6773 "net rpc printer migrate security\n"
6776 _("Migrate printer-ACLs from a print-server"));
6781 d_printf(_("no server to migrate\n"));
6785 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6786 rpc_printer_migrate_security_internals,
6791 * Migrate printer-settings from a print-server.
6793 * @param c A net_context structure.
6794 * @param argc Standard main() style argc.
6795 * @param argv Standard main() style argv. Initial components are already
6798 * @return A shell status integer (0 for success).
6800 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6803 if (c->display_usage) {
6805 "net rpc printer migrate settings\n"
6808 _("Migrate printer-settings from a "
6814 d_printf(_("no server to migrate\n"));
6818 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6819 rpc_printer_migrate_settings_internals,
6824 * 'net rpc printer' entrypoint.
6826 * @param c A net_context structure.
6827 * @param argc Standard main() style argc.
6828 * @param argv Standard main() style argv. Initial components are already
6832 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6835 /* ouch: when addriver and setdriver are called from within
6836 rpc_printer_migrate_drivers_internals, the printer-queue already
6839 struct functable func[] = {
6842 rpc_printer_migrate_all,
6844 N_("Migrate all from remote to local print server"),
6845 N_("net rpc printer migrate all\n"
6846 " Migrate all from remote to local print server")
6850 rpc_printer_migrate_drivers,
6852 N_("Migrate drivers to local server"),
6853 N_("net rpc printer migrate drivers\n"
6854 " Migrate drivers to local server")
6858 rpc_printer_migrate_forms,
6860 N_("Migrate froms to local server"),
6861 N_("net rpc printer migrate forms\n"
6862 " Migrate froms to local server")
6866 rpc_printer_migrate_printers,
6868 N_("Migrate printers to local server"),
6869 N_("net rpc printer migrate printers\n"
6870 " Migrate printers to local server")
6874 rpc_printer_migrate_security,
6876 N_("Mirgate printer ACLs to local server"),
6877 N_("net rpc printer migrate security\n"
6878 " Mirgate printer ACLs to local server")
6882 rpc_printer_migrate_settings,
6884 N_("Migrate printer settings to local server"),
6885 N_("net rpc printer migrate settings\n"
6886 " Migrate printer settings to local server")
6888 {NULL, NULL, 0, NULL, NULL}
6891 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6896 * List printers on a remote RPC server.
6898 * @param c A net_context structure.
6899 * @param argc Standard main() style argc.
6900 * @param argv Standard main() style argv. Initial components are already
6903 * @return A shell status integer (0 for success).
6905 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6907 if (c->display_usage) {
6909 "net rpc printer list\n"
6912 _("List printers on a remote RPC server"));
6916 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6917 rpc_printer_list_internals,
6922 * List printer-drivers on a remote RPC server.
6924 * @param c A net_context structure.
6925 * @param argc Standard main() style argc.
6926 * @param argv Standard main() style argv. Initial components are already
6929 * @return A shell status integer (0 for success).
6931 static int rpc_printer_driver_list(struct net_context *c, int argc,
6934 if (c->display_usage) {
6936 "net rpc printer driver\n"
6939 _("List printer-drivers on a remote RPC server"));
6943 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6944 rpc_printer_driver_list_internals,
6949 * Publish printer in ADS via MSRPC.
6951 * @param c A net_context structure.
6952 * @param argc Standard main() style argc.
6953 * @param argv Standard main() style argv. Initial components are already
6956 * @return A shell status integer (0 for success).
6958 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6961 if (c->display_usage) {
6963 "net rpc printer publish publish\n"
6966 _("Publish printer in ADS via MSRPC"));
6970 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6971 rpc_printer_publish_publish_internals,
6976 * Update printer in ADS via MSRPC.
6978 * @param c A net_context structure.
6979 * @param argc Standard main() style argc.
6980 * @param argv Standard main() style argv. Initial components are already
6983 * @return A shell status integer (0 for success).
6985 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6987 if (c->display_usage) {
6989 "net rpc printer publish update\n"
6992 _("Update printer in ADS via MSRPC"));
6996 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6997 rpc_printer_publish_update_internals,
7002 * UnPublish printer in ADS via MSRPC.
7004 * @param c A net_context structure.
7005 * @param argc Standard main() style argc.
7006 * @param argv Standard main() style argv. Initial components are already
7009 * @return A shell status integer (0 for success).
7011 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7014 if (c->display_usage) {
7016 "net rpc printer publish unpublish\n"
7019 _("UnPublish printer in ADS via MSRPC"));
7023 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7024 rpc_printer_publish_unpublish_internals,
7029 * List published printers via MSRPC.
7031 * @param c A net_context structure.
7032 * @param argc Standard main() style argc.
7033 * @param argv Standard main() style argv. Initial components are already
7036 * @return A shell status integer (0 for success).
7038 static int rpc_printer_publish_list(struct net_context *c, int argc,
7041 if (c->display_usage) {
7043 "net rpc printer publish list\n"
7046 _("List published printers via MSRPC"));
7050 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7051 rpc_printer_publish_list_internals,
7057 * Publish printer in ADS.
7059 * @param c A net_context structure.
7060 * @param argc Standard main() style argc.
7061 * @param argv Standard main() style argv. Initial components are already
7064 * @return A shell status integer (0 for success).
7066 static int rpc_printer_publish(struct net_context *c, int argc,
7070 struct functable func[] = {
7073 rpc_printer_publish_publish,
7075 N_("Publish printer in AD"),
7076 N_("net rpc printer publish publish\n"
7077 " Publish printer in AD")
7081 rpc_printer_publish_update,
7083 N_("Update printer in AD"),
7084 N_("net rpc printer publish update\n"
7085 " Update printer in AD")
7089 rpc_printer_publish_unpublish,
7091 N_("Unpublish printer"),
7092 N_("net rpc printer publish unpublish\n"
7093 " Unpublish printer")
7097 rpc_printer_publish_list,
7099 N_("List published printers"),
7100 N_("net rpc printer publish list\n"
7101 " List published printers")
7103 {NULL, NULL, 0, NULL, NULL}
7107 if (c->display_usage) {
7108 d_printf(_("Usage:\n"));
7109 d_printf(_("net rpc printer publish\n"
7110 " List published printers\n"
7111 " Alias of net rpc printer publish "
7113 net_display_usage_from_functable(func);
7116 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7117 rpc_printer_publish_list_internals,
7121 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7127 * Display rpc printer help page.
7129 * @param c A net_context structure.
7130 * @param argc Standard main() style argc.
7131 * @param argv Standard main() style argv. Initial components are already
7134 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7136 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7137 "\tlists all printers on print-server\n\n"));
7138 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7139 "\tlists all printer-drivers on print-server\n\n"));
7140 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7141 "\tpublishes printer settings in Active Directory\n"
7142 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7143 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7144 "\n\tmigrates printers from remote to local server\n\n"));
7145 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7146 "\n\tmigrates printer-settings from remote to local server\n\n"));
7147 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7148 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7149 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7150 "\n\tmigrates printer-forms from remote to local server\n\n"));
7151 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7152 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7153 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7154 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7155 "\tremote to local print-server\n\n"));
7156 net_common_methods_usage(c, argc, argv);
7157 net_common_flags_usage(c, argc, argv);
7159 "\t-v or --verbose\t\t\tgive verbose output\n"
7160 "\t --destination\t\tmigration target server (default: localhost)\n"));
7166 * 'net rpc printer' entrypoint.
7168 * @param c A net_context structure.
7169 * @param argc Standard main() style argc.
7170 * @param argv Standard main() style argv. Initial components are already
7173 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7175 struct functable func[] = {
7180 N_("List all printers on print server"),
7181 N_("net rpc printer list\n"
7182 " List all printers on print server")
7186 rpc_printer_migrate,
7188 N_("Migrate printer to local server"),
7189 N_("net rpc printer migrate\n"
7190 " Migrate printer to local server")
7194 rpc_printer_driver_list,
7196 N_("List printer drivers"),
7197 N_("net rpc printer driver\n"
7198 " List printer drivers")
7202 rpc_printer_publish,
7204 N_("Publish printer in AD"),
7205 N_("net rpc printer publish\n"
7206 " Publish printer in AD")
7208 {NULL, NULL, 0, NULL, NULL}
7212 if (c->display_usage) {
7213 d_printf(_("Usage:\n"));
7214 d_printf(_("net rpc printer\n"
7215 " List printers\n"));
7216 net_display_usage_from_functable(func);
7219 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7220 rpc_printer_list_internals,
7224 return net_run_function(c, argc, argv, "net rpc printer", func);
7228 * 'net rpc' entrypoint.
7230 * @param c A net_context structure.
7231 * @param argc Standard main() style argc.
7232 * @param argv Standard main() style argv. Initial components are already
7236 int net_rpc(struct net_context *c, int argc, const char **argv)
7238 NET_API_STATUS status;
7240 struct functable func[] = {
7245 N_("Modify global audit settings"),
7246 N_("net rpc audit\n"
7247 " Modify global audit settings")
7253 N_("Show basic info about a domain"),
7255 " Show basic info about a domain")
7261 N_("Join a domain"),
7269 N_("Join a domain created in server manager"),
7270 N_("net rpc oldjoin\n"
7271 " Join a domain created in server manager")
7277 N_("Test that a join is valid"),
7278 N_("net rpc testjoin\n"
7279 " Test that a join is valid")
7285 N_("List/modify users"),
7287 " List/modify users")
7293 N_("Change a user password"),
7294 N_("net rpc password\n"
7295 " Change a user password\n"
7296 " Alias for net rpc user password")
7302 N_("List/modify groups"),
7303 N_("net rpc group\n"
7304 " List/modify groups")
7310 N_("List/modify shares"),
7311 N_("net rpc share\n"
7312 " List/modify shares")
7318 N_("List open files"),
7326 N_("List/modify printers"),
7327 N_("net rpc printer\n"
7328 " List/modify printers")
7332 net_rpc_changetrustpw,
7334 N_("Change trust account password"),
7335 N_("net rpc changetrustpw\n"
7336 " Change trust account password")
7342 N_("Modify domain trusts"),
7343 N_("net rpc trustdom\n"
7344 " Modify domain trusts")
7350 N_("Abort a remote shutdown"),
7351 N_("net rpc abortshutdown\n"
7352 " Abort a remote shutdown")
7358 N_("Shutdown a remote server"),
7359 N_("net rpc shutdown\n"
7360 " Shutdown a remote server")
7366 N_("Dump SAM data of remote NT PDC"),
7367 N_("net rpc samdump\n"
7368 " Dump SAM data of remote NT PDC")
7374 N_("Sync a remote NT PDC's data into local passdb"),
7375 N_("net rpc vampire\n"
7376 " Sync a remote NT PDC's data into local passdb")
7382 N_("Fetch the domain sid into local secrets.tdb"),
7383 N_("net rpc getsid\n"
7384 " Fetch the domain sid into local secrets.tdb")
7390 N_("Manage privileges assigned to SID"),
7391 N_("net rpc rights\n"
7392 " Manage privileges assigned to SID")
7398 N_("Start/stop/query remote services"),
7399 N_("net rpc service\n"
7400 " Start/stop/query remote services")
7406 N_("Manage registry hives"),
7407 N_("net rpc registry\n"
7408 " Manage registry hives")
7414 N_("Open interactive shell on remote server"),
7415 N_("net rpc shell\n"
7416 " Open interactive shell on remote server")
7418 {NULL, NULL, 0, NULL, NULL}
7421 status = libnetapi_init(&c->netapi_ctx);
7425 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7426 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7427 if (c->opt_kerberos) {
7428 libnetapi_set_use_kerberos(c->netapi_ctx);
7430 if (c->opt_ccache) {
7431 libnetapi_set_use_ccache(c->netapi_ctx);
7434 return net_run_function(c, argc, argv, "net rpc", func);