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/ndr_samr_c.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/ndr_netlogon_c.h"
32 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
33 #include "../librpc/gen_ndr/ndr_spoolss.h"
34 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
35 #include "../librpc/gen_ndr/ndr_winreg_c.h"
37 #include "lib/netapi/netapi.h"
38 #include "lib/netapi/netapi_net.h"
39 #include "rpc_client/init_lsa.h"
40 #include "../libcli/security/security.h"
42 static int net_mode_share;
43 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
48 * @brief RPC based subcommands for the 'net' utility.
50 * This file should contain much of the functionality that used to
51 * be found in rpcclient, execpt that the commands should change
52 * less often, and the fucntionality should be sane (the user is not
53 * expected to know a rid/sid before they conduct an operation etc.)
55 * @todo Perhaps eventually these should be split out into a number
56 * of files, as this could get quite big.
61 * Many of the RPC functions need the domain sid. This function gets
62 * it at the start of every run
64 * @param cli A cli_state already connected to the remote machine
66 * @return The Domain SID of the remote machine.
69 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
70 struct dom_sid **domain_sid,
71 const char **domain_name)
73 struct rpc_pipe_client *lsa_pipe = NULL;
74 struct policy_handle pol;
75 NTSTATUS result = NT_STATUS_OK;
76 union lsa_PolicyInformation *info = NULL;
78 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
80 if (!NT_STATUS_IS_OK(result)) {
81 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
85 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
86 SEC_FLAG_MAXIMUM_ALLOWED,
88 if (!NT_STATUS_IS_OK(result)) {
89 d_fprintf(stderr, "open_policy %s: %s\n",
95 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
97 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
99 if (!NT_STATUS_IS_OK(result)) {
100 d_fprintf(stderr, "lsaquery %s: %s\n",
106 *domain_name = info->account_domain.name.string;
107 *domain_sid = info->account_domain.sid;
109 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
110 TALLOC_FREE(lsa_pipe);
116 * Run a single RPC command, from start to finish.
118 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
119 * @param conn_flag a NET_FLAG_ combination. Passed to
120 * net_make_ipc_connection.
121 * @param argc Standard main() style argc.
122 * @param argv Standard main() style argv. Initial components are already
124 * @return A shell status integer (0 for success).
127 int run_rpc_command(struct net_context *c,
128 struct cli_state *cli_arg,
129 const struct ndr_syntax_id *interface,
135 struct cli_state *cli = NULL;
136 struct rpc_pipe_client *pipe_hnd = NULL;
139 struct dom_sid *domain_sid;
140 const char *domain_name;
143 /* make use of cli_state handed over as an argument, if possible */
145 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
146 if (!NT_STATUS_IS_OK(nt_status)) {
147 DEBUG(1, ("failed to make ipc connection: %s\n",
148 nt_errstr(nt_status)));
161 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
162 DEBUG(0, ("talloc_init() failed\n"));
166 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
168 if (!NT_STATUS_IS_OK(nt_status)) {
172 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
173 if (lp_client_schannel()
174 && (ndr_syntax_id_equal(interface,
175 &ndr_table_netlogon.syntax_id))) {
176 /* Always try and create an schannel netlogon pipe. */
177 nt_status = cli_rpc_pipe_open_schannel(
178 cli, interface, NCACN_NP,
179 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
181 if (!NT_STATUS_IS_OK(nt_status)) {
182 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
183 nt_errstr(nt_status) ));
187 if (conn_flags & NET_FLAGS_SEAL) {
188 nt_status = cli_rpc_pipe_open_ntlmssp(
190 (conn_flags & NET_FLAGS_TCP) ?
191 NCACN_IP_TCP : NCACN_NP,
192 DCERPC_AUTH_LEVEL_PRIVACY,
193 lp_workgroup(), c->opt_user_name,
194 c->opt_password, &pipe_hnd);
196 nt_status = cli_rpc_pipe_open_noauth(
200 if (!NT_STATUS_IS_OK(nt_status)) {
201 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
202 get_pipe_name_from_syntax(
203 talloc_tos(), interface),
204 nt_errstr(nt_status) ));
210 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
212 if (!NT_STATUS_IS_OK(nt_status)) {
213 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
216 DEBUG(5, ("rpc command function succedded\n"));
219 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
221 TALLOC_FREE(pipe_hnd);
226 /* close the connection only if it was opened here */
231 talloc_destroy(mem_ctx);
236 * Force a change of the trust acccount password.
238 * All parameters are provided by the run_rpc_command function, except for
239 * argc, argv which are passed through.
241 * @param domain_sid The domain sid acquired from the remote server.
242 * @param cli A cli_state connected to the server.
243 * @param mem_ctx Talloc context, destroyed on completion of the function.
244 * @param argc Standard main() style argc.
245 * @param argv Standard main() style argv. Initial components are already
248 * @return Normal NTSTATUS return.
251 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
252 const struct dom_sid *domain_sid,
253 const char *domain_name,
254 struct cli_state *cli,
255 struct rpc_pipe_client *pipe_hnd,
262 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
263 if (!NT_STATUS_IS_OK(status)) {
264 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
273 * Force a change of the trust acccount password.
275 * @param argc Standard main() style argc.
276 * @param argv Standard main() style argv. Initial components are already
279 * @return A shell status integer (0 for success).
282 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
284 if (c->display_usage) {
286 "net rpc changetrustpw\n"
289 _("Change the machine trust password"));
293 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
294 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
295 rpc_changetrustpw_internals,
300 * Join a domain, the old way.
302 * This uses 'machinename' as the inital password, and changes it.
304 * The password should be created with 'server manager' or equiv first.
306 * All parameters are provided by the run_rpc_command function, except for
307 * argc, argv which are passed through.
309 * @param domain_sid The domain sid acquired from the remote server.
310 * @param cli A cli_state connected to the server.
311 * @param mem_ctx Talloc context, destroyed on completion of the function.
312 * @param argc Standard main() style argc.
313 * @param argv Standard main() style argv. Initial components are already
316 * @return Normal NTSTATUS return.
319 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
320 const struct dom_sid *domain_sid,
321 const char *domain_name,
322 struct cli_state *cli,
323 struct rpc_pipe_client *pipe_hnd,
329 fstring trust_passwd;
330 unsigned char orig_trust_passwd_hash[16];
332 enum netr_SchannelType sec_channel_type;
334 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
336 if (!NT_STATUS_IS_OK(result)) {
337 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
340 nt_errstr(result) ));
345 check what type of join - if the user want's to join as
346 a BDC, the server must agree that we are a BDC.
349 sec_channel_type = get_sec_channel_type(argv[0]);
351 sec_channel_type = get_sec_channel_type(NULL);
354 fstrcpy(trust_passwd, global_myname());
355 strlower_m(trust_passwd);
358 * Machine names can be 15 characters, but the max length on
359 * a password is 14. --jerry
362 trust_passwd[14] = '\0';
364 E_md4hash(trust_passwd, orig_trust_passwd_hash);
366 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
368 orig_trust_passwd_hash,
371 if (NT_STATUS_IS_OK(result))
372 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
375 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
376 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
377 result = NT_STATUS_UNSUCCESSFUL;
384 * Join a domain, the old way.
386 * @param argc Standard main() style argc.
387 * @param argv Standard main() style argv. Initial components are already
390 * @return A shell status integer (0 for success).
393 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
395 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
396 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
397 rpc_oldjoin_internals,
402 * Join a domain, the old way. This function exists to allow
403 * the message to be displayed when oldjoin was explicitly
404 * requested, but not when it was implied by "net rpc join".
406 * @param argc Standard main() style argc.
407 * @param argv Standard main() style argv. Initial components are already
410 * @return A shell status integer (0 for success).
413 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
417 if (c->display_usage) {
422 _("Join a domain the old way"));
426 rc = net_rpc_perform_oldjoin(c, argc, argv);
429 d_fprintf(stderr, _("Failed to join domain\n"));
436 * 'net rpc join' entrypoint.
437 * @param argc Standard main() style argc.
438 * @param argv Standard main() style argv. Initial components are already
441 * Main 'net_rpc_join()' (where the admin username/password is used) is
443 * Try to just change the password, but if that doesn't work, use/prompt
444 * for a username/password.
447 int net_rpc_join(struct net_context *c, int argc, const char **argv)
449 if (c->display_usage) {
452 _("net rpc join -U <username>[%%password] <type>\n"
454 " username\tName of the admin user"
455 " password\tPassword of the admin user, will "
456 "prompt if not specified\n"
457 " type\tCan be one of the following:\n"
458 "\t\tMEMBER\tJoin as member server (default)\n"
459 "\t\tBDC\tJoin as BDC\n"
460 "\t\tPDC\tJoin as PDC\n"));
464 if (lp_server_role() == ROLE_STANDALONE) {
465 d_printf(_("cannot join as standalone machine\n"));
469 if (strlen(global_myname()) > 15) {
470 d_printf(_("Our netbios name can be at most 15 chars long, "
471 "\"%s\" is %u chars long\n"),
472 global_myname(), (unsigned int)strlen(global_myname()));
476 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
479 return net_rpc_join_newstyle(c, argc, argv);
483 * display info about a rpc domain
485 * All parameters are provided by the run_rpc_command function, except for
486 * argc, argv which are passed through.
488 * @param domain_sid The domain sid acquired from the remote server
489 * @param cli A cli_state connected to the server.
490 * @param mem_ctx Talloc context, destroyed on completion of the function.
491 * @param argc Standard main() style argc.
492 * @param argv Standard main() style argv. Initial components are already
495 * @return Normal NTSTATUS return.
498 NTSTATUS rpc_info_internals(struct net_context *c,
499 const struct dom_sid *domain_sid,
500 const char *domain_name,
501 struct cli_state *cli,
502 struct rpc_pipe_client *pipe_hnd,
507 struct policy_handle connect_pol, domain_pol;
508 NTSTATUS status, result;
509 union samr_DomainInfo *info = NULL;
511 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
513 sid_to_fstring(sid_str, domain_sid);
515 /* Get sam policy handle */
516 status = dcerpc_samr_Connect2(b, mem_ctx,
518 MAXIMUM_ALLOWED_ACCESS,
521 if (!NT_STATUS_IS_OK(status)) {
522 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
527 if (!NT_STATUS_IS_OK(result)) {
529 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
534 /* Get domain policy handle */
535 status = dcerpc_samr_OpenDomain(b, mem_ctx,
537 MAXIMUM_ALLOWED_ACCESS,
538 CONST_DISCARD(struct dom_sid2 *, domain_sid),
541 if (!NT_STATUS_IS_OK(status)) {
542 d_fprintf(stderr, _("Could not open domain: %s\n"),
546 if (!NT_STATUS_IS_OK(result)) {
548 d_fprintf(stderr, _("Could not open domain: %s\n"),
553 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
558 if (!NT_STATUS_IS_OK(status)) {
562 if (NT_STATUS_IS_OK(result)) {
563 d_printf(_("Domain Name: %s\n"),
564 info->general.domain_name.string);
565 d_printf(_("Domain SID: %s\n"), sid_str);
566 d_printf(_("Sequence number: %llu\n"),
567 (unsigned long long)info->general.sequence_num);
568 d_printf(_("Num users: %u\n"), info->general.num_users);
569 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
570 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
578 * 'net rpc info' entrypoint.
579 * @param argc Standard main() style argc.
580 * @param argv Standard main() style argv. Initial components are already
584 int net_rpc_info(struct net_context *c, int argc, const char **argv)
586 if (c->display_usage) {
591 _("Display information about the domain"));
595 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
596 NET_FLAGS_PDC, rpc_info_internals,
601 * Fetch domain SID into the local secrets.tdb.
603 * All parameters are provided by the run_rpc_command function, except for
604 * argc, argv which are passed through.
606 * @param domain_sid The domain sid acquired from the remote server.
607 * @param cli A cli_state connected to the server.
608 * @param mem_ctx Talloc context, destroyed on completion of the function.
609 * @param argc Standard main() style argc.
610 * @param argv Standard main() style argv. Initial components are already
613 * @return Normal NTSTATUS return.
616 static NTSTATUS rpc_getsid_internals(struct net_context *c,
617 const struct dom_sid *domain_sid,
618 const char *domain_name,
619 struct cli_state *cli,
620 struct rpc_pipe_client *pipe_hnd,
627 sid_to_fstring(sid_str, domain_sid);
628 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
629 sid_str, domain_name);
631 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
632 DEBUG(0,("Can't store domain SID\n"));
633 return NT_STATUS_UNSUCCESSFUL;
640 * 'net rpc getsid' entrypoint.
641 * @param argc Standard main() style argc.
642 * @param argv Standard main() style argv. Initial components are already
646 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
648 int conn_flags = NET_FLAGS_PDC;
650 if (!c->opt_user_specified) {
651 conn_flags |= NET_FLAGS_ANONYMOUS;
654 if (c->display_usage) {
659 _("Fetch domain SID into local secrets.tdb"));
663 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
665 rpc_getsid_internals,
669 /****************************************************************************/
672 * Basic usage function for 'net rpc user'.
673 * @param argc Standard main() style argc.
674 * @param argv Standard main() style argv. Initial components are already
678 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
680 return net_user_usage(c, argc, argv);
684 * Add a new user to a remote RPC server.
686 * @param argc Standard main() style argc.
687 * @param argv Standard main() style argv. Initial components are already
690 * @return A shell status integer (0 for success).
693 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
695 NET_API_STATUS status;
696 struct USER_INFO_1 info1;
697 uint32_t parm_error = 0;
699 if (argc < 1 || c->display_usage) {
700 rpc_user_usage(c, argc, argv);
706 info1.usri1_name = argv[0];
708 info1.usri1_password = argv[1];
711 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
714 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
715 argv[0], libnetapi_get_error_string(c->netapi_ctx,
719 d_printf(_("Added user '%s'.\n"), argv[0]);
726 * Rename a user on a remote RPC server.
728 * @param argc Standard main() style argc.
729 * @param argv Standard main() style argv. Initial components are already
732 * @return A shell status integer (0 for success).
735 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
737 NET_API_STATUS status;
738 struct USER_INFO_0 u0;
739 uint32_t parm_err = 0;
741 if (argc != 2 || c->display_usage) {
742 rpc_user_usage(c, argc, argv);
746 u0.usri0_name = argv[1];
748 status = NetUserSetInfo(c->opt_host, argv[0],
749 0, (uint8_t *)&u0, &parm_err);
752 _("Failed to rename user from %s to %s - %s\n"),
754 libnetapi_get_error_string(c->netapi_ctx, status));
756 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
763 * Set a user's primary group
765 * @param argc Standard main() style argc.
766 * @param argv Standard main() style argv. Initial components are already
769 * @return A shell status integer (0 for success).
772 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
775 NET_API_STATUS status;
777 struct GROUP_INFO_2 *g2;
778 struct USER_INFO_1051 u1051;
779 uint32_t parm_err = 0;
781 if (argc != 2 || c->display_usage) {
782 rpc_user_usage(c, argc, argv);
786 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
788 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
790 libnetapi_get_error_string(c->netapi_ctx, status));
793 g2 = (struct GROUP_INFO_2 *)buffer;
795 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
797 NetApiBufferFree(buffer);
799 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
800 (uint8_t *)&u1051, &parm_err);
803 _("Failed to set user's primary group %s to %s - "
804 "%s\n"), argv[0], argv[1],
805 libnetapi_get_error_string(c->netapi_ctx, status));
807 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
814 * Delete a user from a remote RPC server.
816 * @param argc Standard main() style argc.
817 * @param argv Standard main() style argv. Initial components are already
820 * @return A shell status integer (0 for success).
823 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
825 NET_API_STATUS status;
827 if (argc < 1 || c->display_usage) {
828 rpc_user_usage(c, argc, argv);
832 status = NetUserDel(c->opt_host, argv[0]);
835 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
837 libnetapi_get_error_string(c->netapi_ctx, status));
840 d_printf(_("Deleted user '%s'.\n"), argv[0]);
847 * Set a user's password on a remote RPC server.
849 * @param argc Standard main() style argc.
850 * @param argv Standard main() style argv. Initial components are already
853 * @return A shell status integer (0 for success).
856 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
858 NET_API_STATUS status;
860 struct USER_INFO_1003 u1003;
861 uint32_t parm_err = 0;
864 if (argc < 1 || c->display_usage) {
865 rpc_user_usage(c, argc, argv);
870 u1003.usri1003_password = argv[1];
872 ret = asprintf(&prompt, _("Enter new password for %s:"),
877 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
879 if (u1003.usri1003_password == NULL) {
884 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
886 /* Display results */
889 _("Failed to set password for '%s' with error: %s.\n"),
890 argv[0], libnetapi_get_error_string(c->netapi_ctx,
899 * List a user's groups from a remote RPC server.
901 * @param argc Standard main() style argc.
902 * @param argv Standard main() style argv. Initial components are already
905 * @return A shell status integer (0 for success)
908 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
911 NET_API_STATUS status;
912 struct GROUP_USERS_INFO_0 *u0 = NULL;
913 uint32_t entries_read = 0;
914 uint32_t total_entries = 0;
918 if (argc < 1 || c->display_usage) {
919 rpc_user_usage(c, argc, argv);
923 status = NetUserGetGroups(c->opt_host,
926 (uint8_t **)(void *)&u0,
932 _("Failed to get groups for '%s' with error: %s.\n"),
933 argv[0], libnetapi_get_error_string(c->netapi_ctx,
938 for (i=0; i < entries_read; i++) {
939 printf("%s\n", u0->grui0_name);
947 * List users on a remote RPC server.
949 * All parameters are provided by the run_rpc_command function, except for
950 * argc, argv which are passed through.
952 * @param domain_sid The domain sid acquired from the remote server.
953 * @param cli A cli_state connected to the server.
954 * @param mem_ctx Talloc context, destroyed on completion of the function.
955 * @param argc Standard main() style argc.
956 * @param argv Standard main() style argv. Initial components are already
959 * @return Normal NTSTATUS return.
962 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
964 NET_API_STATUS status;
965 uint32_t start_idx=0, num_entries, i, loop_count = 0;
966 struct NET_DISPLAY_USER *info = NULL;
969 /* Query domain users */
970 if (c->opt_long_list_entries)
971 d_printf(_("\nUser name Comment"
972 "\n-----------------------------\n"));
974 uint32_t max_entries, max_size;
976 dcerpc_get_query_dispinfo_params(
977 loop_count, &max_entries, &max_size);
979 status = NetQueryDisplayInformation(c->opt_host,
986 if (status != 0 && status != ERROR_MORE_DATA) {
990 info = (struct NET_DISPLAY_USER *)buffer;
992 for (i = 0; i < num_entries; i++) {
994 if (c->opt_long_list_entries)
995 printf("%-21.21s %s\n", info->usri1_name,
996 info->usri1_comment);
998 printf("%s\n", info->usri1_name);
1002 NetApiBufferFree(buffer);
1005 start_idx += num_entries;
1007 } while (status == ERROR_MORE_DATA);
1013 * 'net rpc user' entrypoint.
1014 * @param argc Standard main() style argc.
1015 * @param argv Standard main() style argv. Initial components are already
1019 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1021 NET_API_STATUS status;
1023 struct functable func[] = {
1028 N_("Add specified user"),
1029 N_("net rpc user add\n"
1030 " Add specified user")
1036 N_("List domain groups of user"),
1037 N_("net rpc user info\n"
1038 " List domain groups of user")
1044 N_("Remove specified user"),
1045 N_("net rpc user delete\n"
1046 " Remove specified user")
1052 N_("Change user password"),
1053 N_("net rpc user password\n"
1054 " Change user password")
1060 N_("Rename specified user"),
1061 N_("net rpc user rename\n"
1062 " Rename specified user")
1066 rpc_user_setprimarygroup,
1068 "Set a user's primary group",
1069 "net rpc user setprimarygroup\n"
1070 " Set a user's primary group"
1072 {NULL, NULL, 0, NULL, NULL}
1075 status = libnetapi_net_init(&c->netapi_ctx);
1079 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1080 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1081 if (c->opt_kerberos) {
1082 libnetapi_set_use_kerberos(c->netapi_ctx);
1086 if (c->display_usage) {
1091 _("List all users"));
1092 net_display_usage_from_functable(func);
1096 return rpc_user_list(c, argc, argv);
1099 return net_run_function(c, argc, argv, "net rpc user", func);
1102 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1103 TALLOC_CTX *mem_ctx,
1104 struct rpc_sh_ctx *ctx,
1105 struct rpc_pipe_client *pipe_hnd,
1106 int argc, const char **argv)
1108 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1111 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1112 TALLOC_CTX *mem_ctx,
1113 struct rpc_sh_ctx *ctx,
1114 struct rpc_pipe_client *pipe_hnd,
1115 int argc, const char **argv)
1117 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1120 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1121 TALLOC_CTX *mem_ctx,
1122 struct rpc_sh_ctx *ctx,
1123 struct rpc_pipe_client *pipe_hnd,
1124 int argc, const char **argv,
1126 struct net_context *c,
1127 TALLOC_CTX *mem_ctx,
1128 struct rpc_sh_ctx *ctx,
1129 struct rpc_pipe_client *pipe_hnd,
1130 struct policy_handle *user_hnd,
1131 int argc, const char **argv))
1133 struct policy_handle connect_pol, domain_pol, user_pol;
1134 NTSTATUS status, result;
1137 enum lsa_SidType type;
1138 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1141 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1143 return NT_STATUS_INVALID_PARAMETER;
1146 ZERO_STRUCT(connect_pol);
1147 ZERO_STRUCT(domain_pol);
1148 ZERO_STRUCT(user_pol);
1150 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1151 argv[0], NULL, NULL, &sid, &type);
1152 if (!NT_STATUS_IS_OK(status)) {
1153 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1158 if (type != SID_NAME_USER) {
1159 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1160 sid_type_lookup(type));
1161 status = NT_STATUS_NO_SUCH_USER;
1165 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1166 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1167 status = NT_STATUS_NO_SUCH_USER;
1171 status = dcerpc_samr_Connect2(b, mem_ctx,
1173 MAXIMUM_ALLOWED_ACCESS,
1176 if (!NT_STATUS_IS_OK(status)) {
1179 if (!NT_STATUS_IS_OK(result)) {
1184 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1186 MAXIMUM_ALLOWED_ACCESS,
1190 if (!NT_STATUS_IS_OK(status)) {
1193 if (!NT_STATUS_IS_OK(result)) {
1198 status = dcerpc_samr_OpenUser(b, mem_ctx,
1200 MAXIMUM_ALLOWED_ACCESS,
1204 if (!NT_STATUS_IS_OK(status)) {
1207 if (!NT_STATUS_IS_OK(result)) {
1212 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1215 if (is_valid_policy_hnd(&user_pol)) {
1216 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1218 if (is_valid_policy_hnd(&domain_pol)) {
1219 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1221 if (is_valid_policy_hnd(&connect_pol)) {
1222 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1227 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1228 TALLOC_CTX *mem_ctx,
1229 struct rpc_sh_ctx *ctx,
1230 struct rpc_pipe_client *pipe_hnd,
1231 struct policy_handle *user_hnd,
1232 int argc, const char **argv)
1234 NTSTATUS status, result;
1235 union samr_UserInfo *info = NULL;
1236 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1239 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1241 return NT_STATUS_INVALID_PARAMETER;
1244 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1249 if (!NT_STATUS_IS_OK(status)) {
1252 if (!NT_STATUS_IS_OK(result)) {
1256 d_printf(_("user rid: %d, group rid: %d\n"),
1258 info->info21.primary_gid);
1263 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1264 TALLOC_CTX *mem_ctx,
1265 struct rpc_sh_ctx *ctx,
1266 struct rpc_pipe_client *pipe_hnd,
1267 int argc, const char **argv)
1269 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1270 rpc_sh_user_show_internals);
1273 #define FETCHSTR(name, rec) \
1274 do { if (strequal(ctx->thiscmd, name)) { \
1275 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1278 #define SETSTR(name, rec, flag) \
1279 do { if (strequal(ctx->thiscmd, name)) { \
1280 init_lsa_String(&(info->info21.rec), argv[0]); \
1281 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1284 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1285 TALLOC_CTX *mem_ctx,
1286 struct rpc_sh_ctx *ctx,
1287 struct rpc_pipe_client *pipe_hnd,
1288 struct policy_handle *user_hnd,
1289 int argc, const char **argv)
1291 NTSTATUS status, result;
1292 const char *username;
1293 const char *oldval = "";
1294 union samr_UserInfo *info = NULL;
1295 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1298 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1299 _("Usage:"), ctx->whoami);
1300 return NT_STATUS_INVALID_PARAMETER;
1303 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1308 if (!NT_STATUS_IS_OK(status)) {
1311 if (!NT_STATUS_IS_OK(result)) {
1315 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1317 FETCHSTR("fullname", full_name);
1318 FETCHSTR("homedir", home_directory);
1319 FETCHSTR("homedrive", home_drive);
1320 FETCHSTR("logonscript", logon_script);
1321 FETCHSTR("profilepath", profile_path);
1322 FETCHSTR("description", description);
1325 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1329 if (strcmp(argv[0], "NULL") == 0) {
1333 ZERO_STRUCT(info->info21);
1335 SETSTR("fullname", full_name, FULL_NAME);
1336 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1337 SETSTR("homedrive", home_drive, HOME_DRIVE);
1338 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1339 SETSTR("profilepath", profile_path, PROFILE_PATH);
1340 SETSTR("description", description, DESCRIPTION);
1342 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1347 if (!NT_STATUS_IS_OK(status)) {
1353 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1354 ctx->thiscmd, oldval, argv[0]);
1361 #define HANDLEFLG(name, rec) \
1362 do { if (strequal(ctx->thiscmd, name)) { \
1363 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1365 newflags = oldflags | ACB_##rec; \
1367 newflags = oldflags & ~ACB_##rec; \
1370 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1371 TALLOC_CTX *mem_ctx,
1372 struct rpc_sh_ctx *ctx,
1373 struct rpc_pipe_client *pipe_hnd,
1374 int argc, const char **argv)
1376 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1377 rpc_sh_user_str_edit_internals);
1380 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1381 TALLOC_CTX *mem_ctx,
1382 struct rpc_sh_ctx *ctx,
1383 struct rpc_pipe_client *pipe_hnd,
1384 struct policy_handle *user_hnd,
1385 int argc, const char **argv)
1387 NTSTATUS status, result;
1388 const char *username;
1389 const char *oldval = "unknown";
1390 uint32 oldflags, newflags;
1392 union samr_UserInfo *info = NULL;
1393 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1396 ((argc == 1) && !strequal(argv[0], "yes") &&
1397 !strequal(argv[0], "no"))) {
1398 /* TRANSATORS: The yes|no here are program keywords. Please do
1400 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1402 return NT_STATUS_INVALID_PARAMETER;
1405 newval = strequal(argv[0], "yes");
1407 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1412 if (!NT_STATUS_IS_OK(status)) {
1415 if (!NT_STATUS_IS_OK(result)) {
1419 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1420 oldflags = info->info21.acct_flags;
1421 newflags = info->info21.acct_flags;
1423 HANDLEFLG("disabled", DISABLED);
1424 HANDLEFLG("pwnotreq", PWNOTREQ);
1425 HANDLEFLG("autolock", AUTOLOCK);
1426 HANDLEFLG("pwnoexp", PWNOEXP);
1429 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1434 ZERO_STRUCT(info->info21);
1436 info->info21.acct_flags = newflags;
1437 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1439 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1444 if (!NT_STATUS_IS_OK(status)) {
1448 if (NT_STATUS_IS_OK(result)) {
1449 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1450 ctx->thiscmd, oldval, argv[0]);
1458 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1459 TALLOC_CTX *mem_ctx,
1460 struct rpc_sh_ctx *ctx,
1461 struct rpc_pipe_client *pipe_hnd,
1462 int argc, const char **argv)
1464 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1465 rpc_sh_user_flag_edit_internals);
1468 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1469 TALLOC_CTX *mem_ctx,
1470 struct rpc_sh_ctx *ctx)
1472 static struct rpc_sh_cmd cmds[] = {
1474 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1475 N_("Show/Set a user's full name") },
1477 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1478 N_("Show/Set a user's home directory") },
1480 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1481 N_("Show/Set a user's home drive") },
1483 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1484 N_("Show/Set a user's logon script") },
1486 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1487 N_("Show/Set a user's profile path") },
1489 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1490 N_("Show/Set a user's description") },
1492 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1493 N_("Show/Set whether a user is disabled") },
1495 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1496 N_("Show/Set whether a user locked out") },
1498 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1499 N_("Show/Set whether a user does not need a password") },
1501 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1502 N_("Show/Set whether a user's password does not expire") },
1504 { NULL, NULL, 0, NULL, NULL }
1510 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1511 TALLOC_CTX *mem_ctx,
1512 struct rpc_sh_ctx *ctx)
1514 static struct rpc_sh_cmd cmds[] = {
1516 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1517 N_("List available users") },
1519 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1520 N_("List the domain groups a user is member of") },
1522 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1523 N_("Show info about a user") },
1525 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1526 N_("Show/Modify a user's fields") },
1528 { NULL, NULL, 0, NULL, NULL }
1534 /****************************************************************************/
1537 * Basic usage function for 'net rpc group'.
1538 * @param argc Standard main() style argc.
1539 * @param argv Standard main() style argv. Initial components are already
1543 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1545 return net_group_usage(c, argc, argv);
1549 * Delete group on a remote RPC server.
1551 * All parameters are provided by the run_rpc_command function, except for
1552 * argc, argv which are passed through.
1554 * @param domain_sid The domain sid acquired from the remote server.
1555 * @param cli A cli_state connected to the server.
1556 * @param mem_ctx Talloc context, destroyed on completion of the function.
1557 * @param argc Standard main() style argc.
1558 * @param argv Standard main() style argv. Initial components are already
1561 * @return Normal NTSTATUS return.
1564 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1565 const struct dom_sid *domain_sid,
1566 const char *domain_name,
1567 struct cli_state *cli,
1568 struct rpc_pipe_client *pipe_hnd,
1569 TALLOC_CTX *mem_ctx,
1573 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1574 bool group_is_primary = false;
1575 NTSTATUS status, result;
1577 struct samr_RidAttrArray *rids = NULL;
1580 /* struct samr_RidWithAttribute *user_gids; */
1581 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1583 struct samr_Ids group_rids, name_types;
1584 struct lsa_String lsa_acct_name;
1585 union samr_UserInfo *info = NULL;
1587 if (argc < 1 || c->display_usage) {
1588 rpc_group_usage(c, argc,argv);
1589 return NT_STATUS_OK; /* ok? */
1592 status = dcerpc_samr_Connect2(b, mem_ctx,
1594 MAXIMUM_ALLOWED_ACCESS,
1597 if (!NT_STATUS_IS_OK(status)) {
1598 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1602 if (!NT_STATUS_IS_OK(result)) {
1604 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1608 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1610 MAXIMUM_ALLOWED_ACCESS,
1611 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1614 if (!NT_STATUS_IS_OK(status)) {
1615 d_fprintf(stderr, _("Request open_domain failed\n"));
1619 if (!NT_STATUS_IS_OK(result)) {
1621 d_fprintf(stderr, _("Request open_domain failed\n"));
1625 init_lsa_String(&lsa_acct_name, argv[0]);
1627 status = dcerpc_samr_LookupNames(b, mem_ctx,
1634 if (!NT_STATUS_IS_OK(status)) {
1635 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1639 if (!NT_STATUS_IS_OK(result)) {
1641 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1645 switch (name_types.ids[0])
1647 case SID_NAME_DOM_GRP:
1648 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1650 MAXIMUM_ALLOWED_ACCESS,
1654 if (!NT_STATUS_IS_OK(status)) {
1655 d_fprintf(stderr, _("Request open_group failed"));
1659 if (!NT_STATUS_IS_OK(result)) {
1661 d_fprintf(stderr, _("Request open_group failed"));
1665 group_rid = group_rids.ids[0];
1667 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1671 if (!NT_STATUS_IS_OK(status)) {
1673 _("Unable to query group members of %s"),
1678 if (!NT_STATUS_IS_OK(result)) {
1681 _("Unable to query group members of %s"),
1686 if (c->opt_verbose) {
1688 _("Domain Group %s (rid: %d) has %d members\n"),
1689 argv[0],group_rid, rids->count);
1692 /* Check if group is anyone's primary group */
1693 for (i = 0; i < rids->count; i++)
1695 status = dcerpc_samr_OpenUser(b, mem_ctx,
1697 MAXIMUM_ALLOWED_ACCESS,
1701 if (!NT_STATUS_IS_OK(status)) {
1703 _("Unable to open group member %d\n"),
1708 if (!NT_STATUS_IS_OK(result)) {
1711 _("Unable to open group member %d\n"),
1716 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1721 if (!NT_STATUS_IS_OK(status)) {
1723 _("Unable to lookup userinfo for group "
1729 if (!NT_STATUS_IS_OK(result)) {
1732 _("Unable to lookup userinfo for group "
1738 if (info->info21.primary_gid == group_rid) {
1739 if (c->opt_verbose) {
1740 d_printf(_("Group is primary group "
1742 info->info21.account_name.string);
1744 group_is_primary = true;
1747 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1750 if (group_is_primary) {
1751 d_fprintf(stderr, _("Unable to delete group because "
1752 "some of it's members have it as primary "
1754 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1758 /* remove all group members */
1759 for (i = 0; i < rids->count; i++)
1762 d_printf(_("Remove group member %d..."),
1764 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1768 if (!NT_STATUS_IS_OK(status)) {
1772 if (NT_STATUS_IS_OK(result)) {
1774 d_printf(_("ok\n"));
1777 d_printf("%s\n", _("failed"));
1782 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1785 if (!NT_STATUS_IS_OK(status)) {
1792 /* removing a local group is easier... */
1793 case SID_NAME_ALIAS:
1794 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1796 MAXIMUM_ALLOWED_ACCESS,
1800 if (!NT_STATUS_IS_OK(status)) {
1801 d_fprintf(stderr, _("Request open_alias failed\n"));
1804 if (!NT_STATUS_IS_OK(result)) {
1806 d_fprintf(stderr, _("Request open_alias failed\n"));
1810 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1813 if (!NT_STATUS_IS_OK(status)) {
1821 d_fprintf(stderr, _("%s is of type %s. This command is only "
1822 "for deleting local or global groups\n"),
1823 argv[0],sid_type_lookup(name_types.ids[0]));
1824 status = NT_STATUS_UNSUCCESSFUL;
1828 if (NT_STATUS_IS_OK(status)) {
1830 d_printf(_("Deleted %s '%s'\n"),
1831 sid_type_lookup(name_types.ids[0]), argv[0]);
1833 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1834 get_friendly_nt_error_msg(status));
1842 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1844 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1845 rpc_group_delete_internals, argc,argv);
1848 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1850 NET_API_STATUS status;
1851 struct GROUP_INFO_1 info1;
1852 uint32_t parm_error = 0;
1854 if (argc != 1 || c->display_usage) {
1855 rpc_group_usage(c, argc, argv);
1861 info1.grpi1_name = argv[0];
1862 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1863 info1.grpi1_comment = c->opt_comment;
1866 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1870 _("Failed to add group '%s' with error: %s.\n"),
1871 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1875 d_printf(_("Added group '%s'.\n"), argv[0]);
1881 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1883 NET_API_STATUS status;
1884 struct LOCALGROUP_INFO_1 info1;
1885 uint32_t parm_error = 0;
1887 if (argc != 1 || c->display_usage) {
1888 rpc_group_usage(c, argc, argv);
1894 info1.lgrpi1_name = argv[0];
1895 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1896 info1.lgrpi1_comment = c->opt_comment;
1899 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1903 _("Failed to add alias '%s' with error: %s.\n"),
1904 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1908 d_printf(_("Added alias '%s'.\n"), argv[0]);
1914 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1916 if (c->opt_localgroup)
1917 return rpc_alias_add_internals(c, argc, argv);
1919 return rpc_group_add_internals(c, argc, argv);
1922 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1923 TALLOC_CTX *mem_ctx,
1925 struct dom_sid *sid,
1926 enum lsa_SidType *type)
1928 struct dom_sid *sids = NULL;
1929 enum lsa_SidType *types = NULL;
1930 struct rpc_pipe_client *pipe_hnd = NULL;
1931 struct policy_handle lsa_pol;
1932 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1934 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1936 if (!NT_STATUS_IS_OK(result)) {
1940 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1941 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1943 if (!NT_STATUS_IS_OK(result)) {
1947 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1948 &name, NULL, 1, &sids, &types);
1950 if (NT_STATUS_IS_OK(result)) {
1951 sid_copy(sid, &sids[0]);
1955 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1959 TALLOC_FREE(pipe_hnd);
1962 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1964 /* Try as S-1-5-whatever */
1966 struct dom_sid tmp_sid;
1968 if (string_to_sid(&tmp_sid, name)) {
1969 sid_copy(sid, &tmp_sid);
1970 *type = SID_NAME_UNKNOWN;
1971 result = NT_STATUS_OK;
1978 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1979 TALLOC_CTX *mem_ctx,
1980 const struct dom_sid *group_sid,
1983 struct policy_handle connect_pol, domain_pol;
1986 struct policy_handle group_pol;
1988 struct samr_Ids rids, rid_types;
1989 struct lsa_String lsa_acct_name;
1993 sid_copy(&sid, group_sid);
1995 if (!sid_split_rid(&sid, &group_rid)) {
1996 return NT_STATUS_UNSUCCESSFUL;
1999 /* Get sam policy handle */
2000 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2002 MAXIMUM_ALLOWED_ACCESS,
2004 if (!NT_STATUS_IS_OK(result)) {
2008 /* Get domain policy handle */
2009 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2011 MAXIMUM_ALLOWED_ACCESS,
2014 if (!NT_STATUS_IS_OK(result)) {
2018 init_lsa_String(&lsa_acct_name, member);
2020 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2027 if (!NT_STATUS_IS_OK(result)) {
2028 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2033 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2035 MAXIMUM_ALLOWED_ACCESS,
2039 if (!NT_STATUS_IS_OK(result)) {
2043 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2046 0x0005); /* unknown flags */
2049 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2053 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2054 TALLOC_CTX *mem_ctx,
2055 const struct dom_sid *alias_sid,
2058 struct policy_handle connect_pol, domain_pol;
2061 struct policy_handle alias_pol;
2063 struct dom_sid member_sid;
2064 enum lsa_SidType member_type;
2068 sid_copy(&sid, alias_sid);
2070 if (!sid_split_rid(&sid, &alias_rid)) {
2071 return NT_STATUS_UNSUCCESSFUL;
2074 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2075 member, &member_sid, &member_type);
2077 if (!NT_STATUS_IS_OK(result)) {
2078 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2083 /* Get sam policy handle */
2084 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2086 MAXIMUM_ALLOWED_ACCESS,
2088 if (!NT_STATUS_IS_OK(result)) {
2092 /* Get domain policy handle */
2093 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2095 MAXIMUM_ALLOWED_ACCESS,
2098 if (!NT_STATUS_IS_OK(result)) {
2102 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2104 MAXIMUM_ALLOWED_ACCESS,
2108 if (!NT_STATUS_IS_OK(result)) {
2112 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2116 if (!NT_STATUS_IS_OK(result)) {
2121 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2125 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2126 const struct dom_sid *domain_sid,
2127 const char *domain_name,
2128 struct cli_state *cli,
2129 struct rpc_pipe_client *pipe_hnd,
2130 TALLOC_CTX *mem_ctx,
2134 struct dom_sid group_sid;
2135 enum lsa_SidType group_type;
2137 if (argc != 2 || c->display_usage) {
2140 _("net rpc group addmem <group> <member>\n"
2141 " Add a member to a group\n"
2142 " group\tGroup to add member to\n"
2143 " member\tMember to add to group\n"));
2144 return NT_STATUS_UNSUCCESSFUL;
2147 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2148 &group_sid, &group_type))) {
2149 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2151 return NT_STATUS_UNSUCCESSFUL;
2154 if (group_type == SID_NAME_DOM_GRP) {
2155 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2156 &group_sid, argv[1]);
2158 if (!NT_STATUS_IS_OK(result)) {
2159 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2160 argv[1], argv[0], nt_errstr(result));
2165 if (group_type == SID_NAME_ALIAS) {
2166 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2167 &group_sid, argv[1]);
2169 if (!NT_STATUS_IS_OK(result)) {
2170 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2171 argv[1], argv[0], nt_errstr(result));
2176 d_fprintf(stderr, _("Can only add members to global or local groups "
2177 "which %s is not\n"), argv[0]);
2179 return NT_STATUS_UNSUCCESSFUL;
2182 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2184 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2185 rpc_group_addmem_internals,
2189 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2190 struct rpc_pipe_client *pipe_hnd,
2191 TALLOC_CTX *mem_ctx,
2192 const struct dom_sid *group_sid,
2195 struct policy_handle connect_pol, domain_pol;
2198 struct policy_handle group_pol;
2200 struct samr_Ids rids, rid_types;
2201 struct lsa_String lsa_acct_name;
2205 sid_copy(&sid, group_sid);
2207 if (!sid_split_rid(&sid, &group_rid))
2208 return NT_STATUS_UNSUCCESSFUL;
2210 /* Get sam policy handle */
2211 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2213 MAXIMUM_ALLOWED_ACCESS,
2215 if (!NT_STATUS_IS_OK(result))
2218 /* Get domain policy handle */
2219 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2221 MAXIMUM_ALLOWED_ACCESS,
2224 if (!NT_STATUS_IS_OK(result))
2227 init_lsa_String(&lsa_acct_name, member);
2229 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2235 if (!NT_STATUS_IS_OK(result)) {
2236 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2241 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2243 MAXIMUM_ALLOWED_ACCESS,
2247 if (!NT_STATUS_IS_OK(result))
2250 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2255 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2259 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2260 TALLOC_CTX *mem_ctx,
2261 const struct dom_sid *alias_sid,
2264 struct policy_handle connect_pol, domain_pol;
2267 struct policy_handle alias_pol;
2269 struct dom_sid member_sid;
2270 enum lsa_SidType member_type;
2274 sid_copy(&sid, alias_sid);
2276 if (!sid_split_rid(&sid, &alias_rid))
2277 return NT_STATUS_UNSUCCESSFUL;
2279 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2280 member, &member_sid, &member_type);
2282 if (!NT_STATUS_IS_OK(result)) {
2283 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2288 /* Get sam policy handle */
2289 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2291 MAXIMUM_ALLOWED_ACCESS,
2293 if (!NT_STATUS_IS_OK(result)) {
2297 /* Get domain policy handle */
2298 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2300 MAXIMUM_ALLOWED_ACCESS,
2303 if (!NT_STATUS_IS_OK(result)) {
2307 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2309 MAXIMUM_ALLOWED_ACCESS,
2313 if (!NT_STATUS_IS_OK(result))
2316 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2320 if (!NT_STATUS_IS_OK(result))
2324 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2328 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2329 const struct dom_sid *domain_sid,
2330 const char *domain_name,
2331 struct cli_state *cli,
2332 struct rpc_pipe_client *pipe_hnd,
2333 TALLOC_CTX *mem_ctx,
2337 struct dom_sid group_sid;
2338 enum lsa_SidType group_type;
2340 if (argc != 2 || c->display_usage) {
2343 _("net rpc group delmem <group> <member>\n"
2344 " Delete a member from a group\n"
2345 " group\tGroup to delete member from\n"
2346 " member\tMember to delete from group\n"));
2347 return NT_STATUS_UNSUCCESSFUL;
2350 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2351 &group_sid, &group_type))) {
2352 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2354 return NT_STATUS_UNSUCCESSFUL;
2357 if (group_type == SID_NAME_DOM_GRP) {
2358 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2359 &group_sid, argv[1]);
2361 if (!NT_STATUS_IS_OK(result)) {
2362 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2363 argv[1], argv[0], nt_errstr(result));
2368 if (group_type == SID_NAME_ALIAS) {
2369 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2370 &group_sid, argv[1]);
2372 if (!NT_STATUS_IS_OK(result)) {
2373 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2374 argv[1], argv[0], nt_errstr(result));
2379 d_fprintf(stderr, _("Can only delete members from global or local "
2380 "groups which %s is not\n"), argv[0]);
2382 return NT_STATUS_UNSUCCESSFUL;
2385 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2387 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2388 rpc_group_delmem_internals,
2393 * List groups on a remote RPC server.
2395 * All parameters are provided by the run_rpc_command function, except for
2396 * argc, argv which are passes through.
2398 * @param domain_sid The domain sid acquired from the remote server.
2399 * @param cli A cli_state connected to the server.
2400 * @param mem_ctx Talloc context, destroyed on completion of the function.
2401 * @param argc Standard main() style argc.
2402 * @param argv Standard main() style argv. Initial components are already
2405 * @return Normal NTSTATUS return.
2408 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2409 const struct dom_sid *domain_sid,
2410 const char *domain_name,
2411 struct cli_state *cli,
2412 struct rpc_pipe_client *pipe_hnd,
2413 TALLOC_CTX *mem_ctx,
2417 struct policy_handle connect_pol, domain_pol;
2418 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2419 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2420 struct samr_SamArray *groups = NULL;
2421 bool global = false;
2423 bool builtin = false;
2425 if (c->display_usage) {
2428 _("net rpc group list [global] [local] [builtin]\n"
2429 " List groups on RPC server\n"
2430 " global\tList global groups\n"
2431 " local\tList local groups\n"
2432 " builtin\tList builtin groups\n"
2433 " If none of global, local or builtin is "
2434 "specified, all three options are considered "
2436 return NT_STATUS_OK;
2445 for (i=0; i<argc; i++) {
2446 if (strequal(argv[i], "global"))
2449 if (strequal(argv[i], "local"))
2452 if (strequal(argv[i], "builtin"))
2456 /* Get sam policy handle */
2458 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2460 MAXIMUM_ALLOWED_ACCESS,
2462 if (!NT_STATUS_IS_OK(result)) {
2466 /* Get domain policy handle */
2468 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2470 MAXIMUM_ALLOWED_ACCESS,
2471 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2473 if (!NT_STATUS_IS_OK(result)) {
2477 /* Query domain groups */
2478 if (c->opt_long_list_entries)
2479 d_printf(_("\nGroup name Comment"
2480 "\n-----------------------------\n"));
2482 uint32_t max_size, total_size, returned_size;
2483 union samr_DispInfo info;
2487 dcerpc_get_query_dispinfo_params(
2488 loop_count, &max_entries, &max_size);
2490 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2499 num_entries = info.info3.count;
2500 start_idx += info.info3.count;
2502 if (!NT_STATUS_IS_OK(result) &&
2503 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2506 for (i = 0; i < num_entries; i++) {
2508 const char *group = NULL;
2509 const char *desc = NULL;
2511 group = info.info3.entries[i].account_name.string;
2512 desc = info.info3.entries[i].description.string;
2514 if (c->opt_long_list_entries)
2515 printf("%-21.21s %-50.50s\n",
2518 printf("%s\n", group);
2520 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2521 /* query domain aliases */
2526 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2532 if (!NT_STATUS_IS_OK(result) &&
2533 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2536 for (i = 0; i < num_entries; i++) {
2538 const char *description = NULL;
2540 if (c->opt_long_list_entries) {
2542 struct policy_handle alias_pol;
2543 union samr_AliasInfo *info = NULL;
2545 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2548 groups->entries[i].idx,
2550 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2554 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2556 description = info->description.string;
2560 if (description != NULL) {
2561 printf("%-21.21s %-50.50s\n",
2562 groups->entries[i].name.string,
2565 printf("%s\n", groups->entries[i].name.string);
2568 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2569 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2570 /* Get builtin policy handle */
2572 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2574 MAXIMUM_ALLOWED_ACCESS,
2575 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2577 if (!NT_STATUS_IS_OK(result)) {
2580 /* query builtin aliases */
2583 if (!builtin) break;
2585 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2591 if (!NT_STATUS_IS_OK(result) &&
2592 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2595 for (i = 0; i < num_entries; i++) {
2597 const char *description = NULL;
2599 if (c->opt_long_list_entries) {
2601 struct policy_handle alias_pol;
2602 union samr_AliasInfo *info = NULL;
2604 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2607 groups->entries[i].idx,
2609 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2613 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2615 description = info->description.string;
2619 if (description != NULL) {
2620 printf("%-21.21s %-50.50s\n",
2621 groups->entries[i].name.string,
2624 printf("%s\n", groups->entries[i].name.string);
2627 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2633 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2635 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2636 rpc_group_list_internals,
2640 static NTSTATUS rpc_list_group_members(struct net_context *c,
2641 struct rpc_pipe_client *pipe_hnd,
2642 TALLOC_CTX *mem_ctx,
2643 const char *domain_name,
2644 const struct dom_sid *domain_sid,
2645 struct policy_handle *domain_pol,
2649 struct policy_handle group_pol;
2650 uint32 num_members, *group_rids;
2652 struct samr_RidAttrArray *rids = NULL;
2653 struct lsa_Strings names;
2654 struct samr_Ids types;
2657 sid_to_fstring(sid_str, domain_sid);
2659 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2661 MAXIMUM_ALLOWED_ACCESS,
2665 if (!NT_STATUS_IS_OK(result))
2668 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2672 if (!NT_STATUS_IS_OK(result))
2675 num_members = rids->count;
2676 group_rids = rids->rids;
2678 while (num_members > 0) {
2679 int this_time = 512;
2681 if (num_members < this_time)
2682 this_time = num_members;
2684 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2691 if (!NT_STATUS_IS_OK(result))
2694 /* We only have users as members, but make the output
2695 the same as the output of alias members */
2697 for (i = 0; i < this_time; i++) {
2699 if (c->opt_long_list_entries) {
2700 printf("%s-%d %s\\%s %d\n", sid_str,
2701 group_rids[i], domain_name,
2702 names.names[i].string,
2705 printf("%s\\%s\n", domain_name,
2706 names.names[i].string);
2710 num_members -= this_time;
2714 return NT_STATUS_OK;
2717 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2718 struct rpc_pipe_client *pipe_hnd,
2719 TALLOC_CTX *mem_ctx,
2720 struct policy_handle *domain_pol,
2724 struct rpc_pipe_client *lsa_pipe;
2725 struct policy_handle alias_pol, lsa_pol;
2727 struct dom_sid *alias_sids;
2730 enum lsa_SidType *types;
2732 struct lsa_SidArray sid_array;
2734 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2736 MAXIMUM_ALLOWED_ACCESS,
2740 if (!NT_STATUS_IS_OK(result))
2743 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2747 if (!NT_STATUS_IS_OK(result)) {
2748 d_fprintf(stderr, _("Couldn't list alias members\n"));
2752 num_members = sid_array.num_sids;
2754 if (num_members == 0) {
2755 return NT_STATUS_OK;
2758 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2759 &ndr_table_lsarpc.syntax_id,
2761 if (!NT_STATUS_IS_OK(result)) {
2762 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2763 nt_errstr(result) );
2767 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2768 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2770 if (!NT_STATUS_IS_OK(result)) {
2771 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2772 TALLOC_FREE(lsa_pipe);
2776 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2778 d_fprintf(stderr, _("Out of memory\n"));
2779 TALLOC_FREE(lsa_pipe);
2780 return NT_STATUS_NO_MEMORY;
2783 for (i=0; i<num_members; i++) {
2784 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2787 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2788 num_members, alias_sids,
2789 &domains, &names, &types);
2791 if (!NT_STATUS_IS_OK(result) &&
2792 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2793 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2794 TALLOC_FREE(lsa_pipe);
2798 for (i = 0; i < num_members; i++) {
2800 sid_to_fstring(sid_str, &alias_sids[i]);
2802 if (c->opt_long_list_entries) {
2803 printf("%s %s\\%s %d\n", sid_str,
2804 domains[i] ? domains[i] : _("*unknown*"),
2805 names[i] ? names[i] : _("*unknown*"), types[i]);
2808 printf("%s\\%s\n", domains[i], names[i]);
2810 printf("%s\n", sid_str);
2814 TALLOC_FREE(lsa_pipe);
2815 return NT_STATUS_OK;
2818 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2819 const struct dom_sid *domain_sid,
2820 const char *domain_name,
2821 struct cli_state *cli,
2822 struct rpc_pipe_client *pipe_hnd,
2823 TALLOC_CTX *mem_ctx,
2828 struct policy_handle connect_pol, domain_pol;
2829 struct samr_Ids rids, rid_types;
2830 struct lsa_String lsa_acct_name;
2832 /* Get sam policy handle */
2834 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2836 MAXIMUM_ALLOWED_ACCESS,
2839 if (!NT_STATUS_IS_OK(result))
2842 /* Get domain policy handle */
2844 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2846 MAXIMUM_ALLOWED_ACCESS,
2847 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2850 if (!NT_STATUS_IS_OK(result))
2853 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2855 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2862 if (!NT_STATUS_IS_OK(result)) {
2864 /* Ok, did not find it in the global sam, try with builtin */
2866 struct dom_sid sid_Builtin;
2868 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2870 sid_copy(&sid_Builtin, &global_sid_Builtin);
2872 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2874 MAXIMUM_ALLOWED_ACCESS,
2878 if (!NT_STATUS_IS_OK(result)) {
2879 d_fprintf(stderr, _("Couldn't find group %s\n"),
2884 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2891 if (!NT_STATUS_IS_OK(result)) {
2892 d_fprintf(stderr, _("Couldn't find group %s\n"),
2898 if (rids.count != 1) {
2899 d_fprintf(stderr, _("Couldn't find group %s\n"),
2904 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2905 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2906 domain_sid, &domain_pol,
2910 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2911 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2915 return NT_STATUS_NO_SUCH_GROUP;
2918 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2920 if (argc != 1 || c->display_usage) {
2921 return rpc_group_usage(c, argc, argv);
2924 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2925 rpc_group_members_internals,
2929 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2931 NET_API_STATUS status;
2932 struct GROUP_INFO_0 g0;
2936 d_printf(_("Usage:\n"));
2937 d_printf("net rpc group rename group newname\n");
2941 g0.grpi0_name = argv[1];
2943 status = NetGroupSetInfo(c->opt_host,
2950 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2951 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2959 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2961 if (argc != 2 || c->display_usage) {
2962 return rpc_group_usage(c, argc, argv);
2965 return rpc_group_rename_internals(c, argc, argv);
2969 * 'net rpc group' entrypoint.
2970 * @param argc Standard main() style argc.
2971 * @param argv Standard main() style argv. Initial components are already
2975 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2977 NET_API_STATUS status;
2979 struct functable func[] = {
2984 N_("Create specified group"),
2985 N_("net rpc group add\n"
2986 " Create specified group")
2992 N_("Delete specified group"),
2993 N_("net rpc group delete\n"
2994 " Delete specified group")
3000 N_("Add member to group"),
3001 N_("net rpc group addmem\n"
3002 " Add member to group")
3008 N_("Remove member from group"),
3009 N_("net rpc group delmem\n"
3010 " Remove member from group")
3017 N_("net rpc group list\n"
3024 N_("List group members"),
3025 N_("net rpc group members\n"
3026 " List group members")
3033 N_("net rpc group rename\n"
3036 {NULL, NULL, 0, NULL, NULL}
3039 status = libnetapi_net_init(&c->netapi_ctx);
3043 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3044 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3045 if (c->opt_kerberos) {
3046 libnetapi_set_use_kerberos(c->netapi_ctx);
3050 if (c->display_usage) {
3051 d_printf(_("Usage:\n"));
3052 d_printf(_("net rpc group\n"
3053 " Alias for net rpc group list global "
3054 "local builtin\n"));
3055 net_display_usage_from_functable(func);
3059 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3060 rpc_group_list_internals,
3064 return net_run_function(c, argc, argv, "net rpc group", func);
3067 /****************************************************************************/
3069 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3071 return net_share_usage(c, argc, argv);
3075 * Add a share on a remote RPC server.
3077 * @param argc Standard main() style argc.
3078 * @param argv Standard main() style argv. Initial components are already
3081 * @return A shell status integer (0 for success).
3084 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3086 NET_API_STATUS status;
3089 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3090 uint32 num_users=0, perms=0;
3091 char *password=NULL; /* don't allow a share password */
3092 struct SHARE_INFO_2 i2;
3093 uint32_t parm_error = 0;
3095 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3096 return rpc_share_usage(c, argc, argv);
3099 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3103 path = strchr(sharename, '=');
3110 i2.shi2_netname = sharename;
3111 i2.shi2_type = type;
3112 i2.shi2_remark = c->opt_comment;
3113 i2.shi2_permissions = perms;
3114 i2.shi2_max_uses = c->opt_maxusers;
3115 i2.shi2_current_uses = num_users;
3116 i2.shi2_path = path;
3117 i2.shi2_passwd = password;
3119 status = NetShareAdd(c->opt_host,
3124 printf(_("NetShareAdd failed with: %s\n"),
3125 libnetapi_get_error_string(c->netapi_ctx, status));
3132 * Delete a share on a remote RPC server.
3134 * @param domain_sid The domain sid acquired from the remote server.
3135 * @param argc Standard main() style argc.
3136 * @param argv Standard main() style argv. Initial components are already
3139 * @return A shell status integer (0 for success).
3141 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3143 if (argc < 1 || c->display_usage) {
3144 return rpc_share_usage(c, argc, argv);
3147 return NetShareDel(c->opt_host, argv[0], 0);
3151 * Formatted print of share info
3153 * @param r pointer to SHARE_INFO_1 to format
3156 static void display_share_info_1(struct net_context *c,
3157 struct SHARE_INFO_1 *r)
3159 if (c->opt_long_list_entries) {
3160 d_printf("%-12s %-8.8s %-50s\n",
3162 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3165 d_printf("%s\n", r->shi1_netname);
3169 static WERROR get_share_info(struct net_context *c,
3170 struct rpc_pipe_client *pipe_hnd,
3171 TALLOC_CTX *mem_ctx,
3175 struct srvsvc_NetShareInfoCtr *info_ctr)
3179 union srvsvc_NetShareInfo info;
3180 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3182 /* no specific share requested, enumerate all */
3185 uint32_t preferred_len = 0xffffffff;
3186 uint32_t total_entries = 0;
3187 uint32_t resume_handle = 0;
3189 info_ctr->level = level;
3191 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3198 if (!NT_STATUS_IS_OK(status)) {
3199 return ntstatus_to_werror(status);
3204 /* request just one share */
3205 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3212 if (!NT_STATUS_IS_OK(status)) {
3213 result = ntstatus_to_werror(status);
3217 if (!W_ERROR_IS_OK(result)) {
3222 ZERO_STRUCTP(info_ctr);
3224 info_ctr->level = level;
3229 struct srvsvc_NetShareCtr1 *ctr1;
3231 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3232 W_ERROR_HAVE_NO_MEMORY(ctr1);
3235 ctr1->array = info.info1;
3237 info_ctr->ctr.ctr1 = ctr1;
3243 struct srvsvc_NetShareCtr2 *ctr2;
3245 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3246 W_ERROR_HAVE_NO_MEMORY(ctr2);
3249 ctr2->array = info.info2;
3251 info_ctr->ctr.ctr2 = ctr2;
3257 struct srvsvc_NetShareCtr502 *ctr502;
3259 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3260 W_ERROR_HAVE_NO_MEMORY(ctr502);
3263 ctr502->array = info.info502;
3265 info_ctr->ctr.ctr502 = ctr502;
3275 * 'net rpc share list' entrypoint.
3276 * @param argc Standard main() style argc.
3277 * @param argv Standard main() style argv. Initial components are already
3280 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3282 NET_API_STATUS status;
3283 struct SHARE_INFO_1 *i1 = NULL;
3284 uint32_t entries_read = 0;
3285 uint32_t total_entries = 0;
3286 uint32_t resume_handle = 0;
3287 uint32_t i, level = 1;
3289 if (c->display_usage) {
3291 "net rpc share list\n"
3294 _("List shares on remote server"));
3298 status = NetShareEnum(c->opt_host,
3300 (uint8_t **)(void *)&i1,
3309 /* Display results */
3311 if (c->opt_long_list_entries) {
3313 "\nEnumerating shared resources (exports) on remote server:\n\n"
3314 "\nShare name Type Description\n"
3315 "---------- ---- -----------\n"));
3317 for (i = 0; i < entries_read; i++)
3318 display_share_info_1(c, &i1[i]);
3323 static bool check_share_availability(struct cli_state *cli, const char *netname)
3327 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3328 if (!NT_STATUS_IS_OK(status)) {
3329 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3333 status = cli_tdis(cli);
3334 if (!NT_STATUS_IS_OK(status)) {
3335 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3342 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3343 const char *netname, uint32 type)
3345 /* only support disk shares */
3346 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3347 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3352 /* skip builtin shares */
3353 /* FIXME: should print$ be added too ? */
3354 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3355 strequal(netname,"global"))
3358 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3359 printf(_("excluding [%s]\n"), netname);
3363 return check_share_availability(cli, netname);
3367 * Migrate shares from a remote RPC server to the local RPC server.
3369 * All parameters are provided by the run_rpc_command function, except for
3370 * argc, argv which are passed through.
3372 * @param domain_sid The domain sid acquired from the remote server.
3373 * @param cli A cli_state connected to the server.
3374 * @param mem_ctx Talloc context, destroyed on completion of the function.
3375 * @param argc Standard main() style argc.
3376 * @param argv Standard main() style argv. Initial components are already
3379 * @return Normal NTSTATUS return.
3382 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3383 const struct dom_sid *domain_sid,
3384 const char *domain_name,
3385 struct cli_state *cli,
3386 struct rpc_pipe_client *pipe_hnd,
3387 TALLOC_CTX *mem_ctx,
3392 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3393 struct srvsvc_NetShareInfoCtr ctr_src;
3395 struct rpc_pipe_client *srvsvc_pipe = NULL;
3396 struct cli_state *cli_dst = NULL;
3397 uint32 level = 502; /* includes secdesc */
3398 uint32_t parm_error = 0;
3399 struct dcerpc_binding_handle *b;
3401 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3403 if (!W_ERROR_IS_OK(result))
3406 /* connect destination PI_SRVSVC */
3407 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3408 &ndr_table_srvsvc.syntax_id);
3409 if (!NT_STATUS_IS_OK(nt_status))
3412 b = srvsvc_pipe->binding_handle;
3414 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3416 union srvsvc_NetShareInfo info;
3417 struct srvsvc_NetShareInfo502 info502 =
3418 ctr_src.ctr.ctr502->array[i];
3420 /* reset error-code */
3421 nt_status = NT_STATUS_UNSUCCESSFUL;
3423 if (!check_share_sanity(c, cli, info502.name, info502.type))
3426 /* finally add the share on the dst server */
3428 printf(_("migrating: [%s], path: %s, comment: %s, without "
3430 info502.name, info502.path, info502.comment);
3432 info.info502 = &info502;
3434 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3435 srvsvc_pipe->desthost,
3440 if (!NT_STATUS_IS_OK(nt_status)) {
3441 printf(_("cannot add share: %s\n"),
3442 nt_errstr(nt_status));
3445 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3446 printf(_(" [%s] does already exist\n"),
3451 if (!W_ERROR_IS_OK(result)) {
3452 nt_status = werror_to_ntstatus(result);
3453 printf(_("cannot add share: %s\n"),
3454 win_errstr(result));
3460 nt_status = NT_STATUS_OK;
3464 cli_shutdown(cli_dst);
3472 * Migrate shares from a RPC server to another.
3474 * @param argc Standard main() style argc.
3475 * @param argv Standard main() style argv. Initial components are already
3478 * @return A shell status integer (0 for success).
3480 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3483 if (c->display_usage) {
3485 "net rpc share migrate shares\n"
3488 _("Migrate shares to local server"));
3493 printf(_("no server to migrate\n"));
3497 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3498 rpc_share_migrate_shares_internals,
3505 * @param f file_info
3506 * @param mask current search mask
3507 * @param state arg-pointer
3510 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3511 const char *mask, void *state)
3513 static NTSTATUS nt_status;
3514 static struct copy_clistate *local_state;
3515 static fstring filename, new_mask;
3518 struct net_context *c;
3520 local_state = (struct copy_clistate *)state;
3521 nt_status = NT_STATUS_UNSUCCESSFUL;
3525 if (strequal(f->name, ".") || strequal(f->name, ".."))
3526 return NT_STATUS_OK;
3528 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3531 if (f->mode & aDIR) {
3533 DEBUG(3,("got dir: %s\n", f->name));
3535 fstrcpy(dir, local_state->cwd);
3537 fstrcat(dir, f->name);
3539 switch (net_mode_share)
3541 case NET_MODE_SHARE_MIGRATE:
3542 /* create that directory */
3543 nt_status = net_copy_file(c, local_state->mem_ctx,
3544 local_state->cli_share_src,
3545 local_state->cli_share_dst,
3547 c->opt_acls? true : false,
3548 c->opt_attrs? true : false,
3549 c->opt_timestamps? true:false,
3553 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3554 return NT_STATUS_INTERNAL_ERROR;
3557 if (!NT_STATUS_IS_OK(nt_status)) {
3558 printf(_("could not handle dir %s: %s\n"),
3559 dir, nt_errstr(nt_status));
3563 /* search below that directory */
3564 fstrcpy(new_mask, dir);
3565 fstrcat(new_mask, "\\*");
3567 old_dir = local_state->cwd;
3568 local_state->cwd = dir;
3569 nt_status = sync_files(local_state, new_mask);
3570 if (!NT_STATUS_IS_OK(nt_status)) {
3571 printf(_("could not handle files\n"));
3573 local_state->cwd = old_dir;
3580 fstrcpy(filename, local_state->cwd);
3581 fstrcat(filename, "\\");
3582 fstrcat(filename, f->name);
3584 DEBUG(3,("got file: %s\n", filename));
3586 switch (net_mode_share)
3588 case NET_MODE_SHARE_MIGRATE:
3589 nt_status = net_copy_file(c, local_state->mem_ctx,
3590 local_state->cli_share_src,
3591 local_state->cli_share_dst,
3593 c->opt_acls? true : false,
3594 c->opt_attrs? true : false,
3595 c->opt_timestamps? true: false,
3599 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3601 return NT_STATUS_INTERNAL_ERROR;
3604 if (!NT_STATUS_IS_OK(nt_status))
3605 printf(_("could not handle file %s: %s\n"),
3606 filename, nt_errstr(nt_status));
3611 * sync files, can be called recursivly to list files
3612 * and then call copy_fn for each file
3614 * @param cp_clistate pointer to the copy_clistate we work with
3615 * @param mask the current search mask
3617 * @return Boolean result
3619 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3621 struct cli_state *targetcli;
3622 char *targetpath = NULL;
3625 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3627 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3628 mask, &targetcli, &targetpath ) ) {
3629 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3631 mask, cli_errstr(cp_clistate->cli_share_src));
3632 return cli_nt_error(cp_clistate->cli_share_src);
3635 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3636 copy_fn, cp_clistate);
3637 if (!NT_STATUS_IS_OK(status)) {
3638 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3639 mask, nt_errstr(status));
3647 * Set the top level directory permissions before we do any further copies.
3648 * Should set up ACL inheritance.
3651 bool copy_top_level_perms(struct net_context *c,
3652 struct copy_clistate *cp_clistate,
3653 const char *sharename)
3655 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3657 switch (net_mode_share) {
3658 case NET_MODE_SHARE_MIGRATE:
3659 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3660 nt_status = net_copy_fileattr(c,
3661 cp_clistate->mem_ctx,
3662 cp_clistate->cli_share_src,
3663 cp_clistate->cli_share_dst,
3665 c->opt_acls? true : false,
3666 c->opt_attrs? true : false,
3667 c->opt_timestamps? true: false,
3671 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3675 if (!NT_STATUS_IS_OK(nt_status)) {
3676 printf(_("Could handle directory attributes for top level "
3677 "directory of share %s. Error %s\n"),
3678 sharename, nt_errstr(nt_status));
3686 * Sync all files inside a remote share to another share (over smb).
3688 * All parameters are provided by the run_rpc_command function, except for
3689 * argc, argv which are passed through.
3691 * @param domain_sid The domain sid acquired from the remote server.
3692 * @param cli A cli_state connected to the server.
3693 * @param mem_ctx Talloc context, destroyed on completion of the function.
3694 * @param argc Standard main() style argc.
3695 * @param argv Standard main() style argv. Initial components are already
3698 * @return Normal NTSTATUS return.
3701 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3702 const struct dom_sid *domain_sid,
3703 const char *domain_name,
3704 struct cli_state *cli,
3705 struct rpc_pipe_client *pipe_hnd,
3706 TALLOC_CTX *mem_ctx,
3711 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3712 struct srvsvc_NetShareInfoCtr ctr_src;
3715 struct copy_clistate cp_clistate;
3716 bool got_src_share = false;
3717 bool got_dst_share = false;
3718 const char *mask = "\\*";
3721 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3723 nt_status = NT_STATUS_NO_MEMORY;
3727 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3730 if (!W_ERROR_IS_OK(result))
3733 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3735 struct srvsvc_NetShareInfo502 info502 =
3736 ctr_src.ctr.ctr502->array[i];
3738 if (!check_share_sanity(c, cli, info502.name, info502.type))
3741 /* one might not want to mirror whole discs :) */
3742 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3743 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3748 switch (net_mode_share)
3750 case NET_MODE_SHARE_MIGRATE:
3754 d_fprintf(stderr, _("Unsupported mode %d\n"),
3758 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3761 c->opt_acls ? _("including") : _("without"),
3762 c->opt_attrs ? _("including") : _("without"),
3763 c->opt_timestamps ? _("(preserving timestamps)") : "");
3765 cp_clistate.mem_ctx = mem_ctx;
3766 cp_clistate.cli_share_src = NULL;
3767 cp_clistate.cli_share_dst = NULL;
3768 cp_clistate.cwd = NULL;
3769 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3772 /* open share source */
3773 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3774 &cli->dest_ss, cli->desthost,
3775 info502.name, "A:");
3776 if (!NT_STATUS_IS_OK(nt_status))
3779 got_src_share = true;
3781 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3782 /* open share destination */
3783 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3784 NULL, dst, info502.name, "A:");
3785 if (!NT_STATUS_IS_OK(nt_status))
3788 got_dst_share = true;
3791 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3792 d_fprintf(stderr, _("Could not handle the top level "
3793 "directory permissions for the "
3794 "share: %s\n"), info502.name);
3795 nt_status = NT_STATUS_UNSUCCESSFUL;
3799 nt_status = sync_files(&cp_clistate, mask);
3800 if (!NT_STATUS_IS_OK(nt_status)) {
3801 d_fprintf(stderr, _("could not handle files for share: "
3802 "%s\n"), info502.name);
3807 nt_status = NT_STATUS_OK;
3812 cli_shutdown(cp_clistate.cli_share_src);
3815 cli_shutdown(cp_clistate.cli_share_dst);
3822 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3824 if (c->display_usage) {
3826 "net share migrate files\n"
3829 _("Migrate files to local server"));
3834 d_printf(_("no server to migrate\n"));
3838 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3839 rpc_share_migrate_files_internals,
3844 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3846 * All parameters are provided by the run_rpc_command function, except for
3847 * argc, argv which are passed through.
3849 * @param domain_sid The domain sid acquired from the remote server.
3850 * @param cli A cli_state connected to the server.
3851 * @param mem_ctx Talloc context, destroyed on completion of the function.
3852 * @param argc Standard main() style argc.
3853 * @param argv Standard main() style argv. Initial components are already
3856 * @return Normal NTSTATUS return.
3859 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3860 const struct dom_sid *domain_sid,
3861 const char *domain_name,
3862 struct cli_state *cli,
3863 struct rpc_pipe_client *pipe_hnd,
3864 TALLOC_CTX *mem_ctx,
3869 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3870 struct srvsvc_NetShareInfoCtr ctr_src;
3871 union srvsvc_NetShareInfo info;
3873 struct rpc_pipe_client *srvsvc_pipe = NULL;
3874 struct cli_state *cli_dst = NULL;
3875 uint32 level = 502; /* includes secdesc */
3876 uint32_t parm_error = 0;
3877 struct dcerpc_binding_handle *b;
3879 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3882 if (!W_ERROR_IS_OK(result))
3885 /* connect destination PI_SRVSVC */
3886 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3887 &ndr_table_srvsvc.syntax_id);
3888 if (!NT_STATUS_IS_OK(nt_status))
3891 b = srvsvc_pipe->binding_handle;
3893 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3895 struct srvsvc_NetShareInfo502 info502 =
3896 ctr_src.ctr.ctr502->array[i];
3898 /* reset error-code */
3899 nt_status = NT_STATUS_UNSUCCESSFUL;
3901 if (!check_share_sanity(c, cli, info502.name, info502.type))
3904 printf(_("migrating: [%s], path: %s, comment: %s, including "
3906 info502.name, info502.path, info502.comment);
3909 display_sec_desc(info502.sd_buf.sd);
3911 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3912 info.info502 = &info502;
3914 /* finally modify the share on the dst server */
3915 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
3916 srvsvc_pipe->desthost,
3922 if (!NT_STATUS_IS_OK(nt_status)) {
3923 printf(_("cannot set share-acl: %s\n"),
3924 nt_errstr(nt_status));
3927 if (!W_ERROR_IS_OK(result)) {
3928 nt_status = werror_to_ntstatus(result);
3929 printf(_("cannot set share-acl: %s\n"),
3930 win_errstr(result));
3936 nt_status = NT_STATUS_OK;
3940 cli_shutdown(cli_dst);
3948 * Migrate share-acls from a RPC server to another.
3950 * @param argc Standard main() style argc.
3951 * @param argv Standard main() style argv. Initial components are already
3954 * @return A shell status integer (0 for success).
3956 static int rpc_share_migrate_security(struct net_context *c, int argc,
3959 if (c->display_usage) {
3961 "net rpc share migrate security\n"
3964 _("Migrate share-acls to local server"));
3969 d_printf(_("no server to migrate\n"));
3973 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3974 rpc_share_migrate_security_internals,
3979 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3980 * from one server to another.
3982 * @param argc Standard main() style argc.
3983 * @param argv Standard main() style argv. Initial components are already
3986 * @return A shell status integer (0 for success).
3989 static int rpc_share_migrate_all(struct net_context *c, int argc,
3994 if (c->display_usage) {
3996 "net rpc share migrate all\n"
3999 _("Migrates shares including all share settings"));
4004 d_printf(_("no server to migrate\n"));
4008 /* order is important. we don't want to be locked out by the share-acl
4009 * before copying files - gd */
4011 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4012 rpc_share_migrate_shares_internals, argc, argv);
4016 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4017 rpc_share_migrate_files_internals, argc, argv);
4021 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4022 rpc_share_migrate_security_internals, argc,
4028 * 'net rpc share migrate' entrypoint.
4029 * @param argc Standard main() style argc.
4030 * @param argv Standard main() style argv. Initial components are already
4033 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4036 struct functable func[] = {
4039 rpc_share_migrate_all,
4041 N_("Migrate shares from remote to local server"),
4042 N_("net rpc share migrate all\n"
4043 " Migrate shares from remote to local server")
4047 rpc_share_migrate_files,
4049 N_("Migrate files from remote to local server"),
4050 N_("net rpc share migrate files\n"
4051 " Migrate files from remote to local server")
4055 rpc_share_migrate_security,
4057 N_("Migrate share-ACLs from remote to local server"),
4058 N_("net rpc share migrate security\n"
4059 " Migrate share-ACLs from remote to local server")
4063 rpc_share_migrate_shares,
4065 N_("Migrate shares from remote to local server"),
4066 N_("net rpc share migrate shares\n"
4067 " Migrate shares from remote to local server")
4069 {NULL, NULL, 0, NULL, NULL}
4072 net_mode_share = NET_MODE_SHARE_MIGRATE;
4074 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4080 struct dom_sid *members;
4083 static int num_server_aliases;
4084 static struct full_alias *server_aliases;
4087 * Add an alias to the static list.
4089 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4091 if (server_aliases == NULL)
4092 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4094 server_aliases[num_server_aliases] = *alias;
4095 num_server_aliases += 1;
4099 * For a specific domain on the server, fetch all the aliases
4100 * and their members. Add all of them to the server_aliases.
4103 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4104 TALLOC_CTX *mem_ctx,
4105 struct policy_handle *connect_pol,
4106 const struct dom_sid *domain_sid)
4108 uint32 start_idx, max_entries, num_entries, i;
4109 struct samr_SamArray *groups = NULL;
4111 struct policy_handle domain_pol;
4113 /* Get domain policy handle */
4115 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4117 MAXIMUM_ALLOWED_ACCESS,
4118 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4120 if (!NT_STATUS_IS_OK(result))
4127 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4133 for (i = 0; i < num_entries; i++) {
4135 struct policy_handle alias_pol;
4136 struct full_alias alias;
4137 struct lsa_SidArray sid_array;
4140 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4142 MAXIMUM_ALLOWED_ACCESS,
4143 groups->entries[i].idx,
4145 if (!NT_STATUS_IS_OK(result))
4148 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4151 if (!NT_STATUS_IS_OK(result))
4154 alias.num_members = sid_array.num_sids;
4156 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4157 if (!NT_STATUS_IS_OK(result))
4160 alias.members = NULL;
4162 if (alias.num_members > 0) {
4163 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4165 for (j = 0; j < alias.num_members; j++)
4166 sid_copy(&alias.members[j],
4167 sid_array.sids[j].sid);
4170 sid_compose(&alias.sid, domain_sid,
4171 groups->entries[i].idx);
4173 push_alias(mem_ctx, &alias);
4175 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4177 result = NT_STATUS_OK;
4180 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4186 * Dump server_aliases as names for debugging purposes.
4189 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4190 const struct dom_sid *domain_sid,
4191 const char *domain_name,
4192 struct cli_state *cli,
4193 struct rpc_pipe_client *pipe_hnd,
4194 TALLOC_CTX *mem_ctx,
4200 struct policy_handle lsa_pol;
4202 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4203 SEC_FLAG_MAXIMUM_ALLOWED,
4205 if (!NT_STATUS_IS_OK(result))
4208 for (i=0; i<num_server_aliases; i++) {
4211 enum lsa_SidType *types;
4214 struct full_alias *alias = &server_aliases[i];
4216 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4218 &domains, &names, &types);
4219 if (!NT_STATUS_IS_OK(result))
4222 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4224 if (alias->num_members == 0) {
4229 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4232 &domains, &names, &types);
4234 if (!NT_STATUS_IS_OK(result) &&
4235 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4238 for (j=0; j<alias->num_members; j++)
4239 DEBUG(1, ("%s\\%s (%d); ",
4240 domains[j] ? domains[j] : "*unknown*",
4241 names[j] ? names[j] : "*unknown*",types[j]));
4245 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4247 return NT_STATUS_OK;
4251 * Fetch a list of all server aliases and their members into
4255 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4256 const struct dom_sid *domain_sid,
4257 const char *domain_name,
4258 struct cli_state *cli,
4259 struct rpc_pipe_client *pipe_hnd,
4260 TALLOC_CTX *mem_ctx,
4265 struct policy_handle connect_pol;
4267 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4269 MAXIMUM_ALLOWED_ACCESS,
4272 if (!NT_STATUS_IS_OK(result))
4275 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4276 &global_sid_Builtin);
4278 if (!NT_STATUS_IS_OK(result))
4281 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4284 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4289 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4291 token->num_sids = 4;
4293 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4294 d_fprintf(stderr, "malloc %s\n",_("failed"));
4295 token->num_sids = 0;
4299 token->sids[0] = *user_sid;
4300 sid_copy(&token->sids[1], &global_sid_World);
4301 sid_copy(&token->sids[2], &global_sid_Network);
4302 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4305 static void free_user_token(struct security_token *token)
4307 SAFE_FREE(token->sids);
4310 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4312 if (security_token_has_sid(token, sid))
4315 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4320 sid_copy(&token->sids[token->num_sids], sid);
4322 token->num_sids += 1;
4327 struct security_token token;
4330 static void dump_user_token(struct user_token *token)
4334 d_printf("%s\n", token->name);
4336 for (i=0; i<token->token.num_sids; i++) {
4337 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4341 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4345 for (i=0; i<alias->num_members; i++) {
4346 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4353 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4357 for (i=0; i<num_server_aliases; i++) {
4358 if (is_alias_member(&sid, &server_aliases[i]))
4359 add_sid_to_token(token, &server_aliases[i].sid);
4364 * We got a user token with all the SIDs we can know about without asking the
4365 * server directly. These are the user and domain group sids. All of these can
4366 * be members of aliases. So scan the list of aliases for each of the SIDs and
4367 * add them to the token.
4370 static void collect_alias_memberships(struct security_token *token)
4372 int num_global_sids = token->num_sids;
4375 for (i=0; i<num_global_sids; i++) {
4376 collect_sid_memberships(token, token->sids[i]);
4380 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4382 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4383 enum wbcSidType type;
4385 struct wbcDomainSid wsid;
4386 char *sid_str = NULL;
4387 struct dom_sid user_sid;
4388 uint32_t num_groups;
4389 gid_t *groups = NULL;
4392 fstr_sprintf(full_name, "%s%c%s",
4393 domain, *lp_winbind_separator(), user);
4395 /* First let's find out the user sid */
4397 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4399 if (!WBC_ERROR_IS_OK(wbc_status)) {
4400 DEBUG(1, ("winbind could not find %s: %s\n",
4401 full_name, wbcErrorString(wbc_status)));
4405 wbc_status = wbcSidToString(&wsid, &sid_str);
4406 if (!WBC_ERROR_IS_OK(wbc_status)) {
4410 if (type != WBC_SID_NAME_USER) {
4411 wbcFreeMemory(sid_str);
4412 DEBUG(1, ("%s is not a user\n", full_name));
4416 if (!string_to_sid(&user_sid, sid_str)) {
4417 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4421 wbcFreeMemory(sid_str);
4424 init_user_token(token, &user_sid);
4426 /* And now the groups winbind knows about */
4428 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4429 if (!WBC_ERROR_IS_OK(wbc_status)) {
4430 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4431 full_name, wbcErrorString(wbc_status)));
4435 for (i = 0; i < num_groups; i++) {
4436 gid_t gid = groups[i];
4439 wbc_status = wbcGidToSid(gid, &wsid);
4440 if (!WBC_ERROR_IS_OK(wbc_status)) {
4441 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4442 (unsigned int)gid, wbcErrorString(wbc_status)));
4443 wbcFreeMemory(groups);
4447 wbc_status = wbcSidToString(&wsid, &sid_str);
4448 if (!WBC_ERROR_IS_OK(wbc_status)) {
4449 wbcFreeMemory(groups);
4453 DEBUG(3, (" %s\n", sid_str));
4455 string_to_sid(&sid, sid_str);
4456 wbcFreeMemory(sid_str);
4459 add_sid_to_token(token, &sid);
4461 wbcFreeMemory(groups);
4467 * Get a list of all user tokens we want to look at
4470 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4471 struct user_token **user_tokens)
4473 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4474 uint32_t i, num_users;
4476 struct user_token *result;
4477 TALLOC_CTX *frame = NULL;
4479 if (lp_winbind_use_default_domain() &&
4480 (c->opt_target_workgroup == NULL)) {
4481 d_fprintf(stderr, _("winbind use default domain = yes set, "
4482 "please specify a workgroup\n"));
4486 /* Send request to winbind daemon */
4488 wbc_status = wbcListUsers(NULL, &num_users, &users);
4489 if (!WBC_ERROR_IS_OK(wbc_status)) {
4490 DEBUG(1, (_("winbind could not list users: %s\n"),
4491 wbcErrorString(wbc_status)));
4495 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4497 if (result == NULL) {
4498 DEBUG(1, ("Could not malloc sid array\n"));
4499 wbcFreeMemory(users);
4503 frame = talloc_stackframe();
4504 for (i=0; i < num_users; i++) {
4505 fstring domain, user;
4508 fstrcpy(result[i].name, users[i]);
4510 p = strchr(users[i], *lp_winbind_separator());
4512 DEBUG(3, ("%s\n", users[i]));
4515 fstrcpy(domain, c->opt_target_workgroup);
4516 fstrcpy(user, users[i]);
4519 fstrcpy(domain, users[i]);
4524 get_user_sids(domain, user, &(result[i].token));
4527 wbcFreeMemory(users);
4529 *num_tokens = num_users;
4530 *user_tokens = result;
4535 static bool get_user_tokens_from_file(FILE *f,
4537 struct user_token **tokens)
4539 struct user_token *token = NULL;
4544 if (fgets(line, sizeof(line)-1, f) == NULL) {
4548 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4549 line[strlen(line)-1] = '\0';
4552 if (line[0] == ' ') {
4556 if(!string_to_sid(&sid, &line[1])) {
4557 DEBUG(1,("get_user_tokens_from_file: Could "
4558 "not convert sid %s \n",&line[1]));
4562 if (token == NULL) {
4563 DEBUG(0, ("File does not begin with username"));
4567 add_sid_to_token(&token->token, &sid);
4571 /* And a new user... */
4574 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4575 if (*tokens == NULL) {
4576 DEBUG(0, ("Could not realloc tokens\n"));
4580 token = &((*tokens)[*num_tokens-1]);
4582 fstrcpy(token->name, line);
4583 token->token.num_sids = 0;
4584 token->token.sids = NULL;
4593 * Show the list of all users that have access to a share
4596 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4597 TALLOC_CTX *mem_ctx,
4598 const char *netname,
4600 struct user_token *tokens)
4603 struct security_descriptor *share_sd = NULL;
4604 struct security_descriptor *root_sd = NULL;
4605 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4607 union srvsvc_NetShareInfo info;
4611 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4613 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4620 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4621 DEBUG(1, ("Coult not query secdesc for share %s\n",
4626 share_sd = info.info502->sd_buf.sd;
4627 if (share_sd == NULL) {
4628 DEBUG(1, ("Got no secdesc for share %s\n",
4634 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4638 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4639 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4640 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4643 for (i=0; i<num_tokens; i++) {
4646 if (share_sd != NULL) {
4647 status = se_access_check(share_sd, &tokens[i].token,
4650 if (!NT_STATUS_IS_OK(status)) {
4651 DEBUG(1, ("Could not check share_sd for "
4658 if (root_sd == NULL) {
4659 d_printf(" %s\n", tokens[i].name);
4663 status = se_access_check(root_sd, &tokens[i].token,
4665 if (!NT_STATUS_IS_OK(status)) {
4666 DEBUG(1, ("Could not check root_sd for user %s\n",
4670 d_printf(" %s\n", tokens[i].name);
4673 if (fnum != (uint16_t)-1)
4674 cli_close(cli, fnum);
4686 static void collect_share(const char *name, uint32 m,
4687 const char *comment, void *state)
4689 struct share_list *share_list = (struct share_list *)state;
4691 if (m != STYPE_DISKTREE)
4694 share_list->num_shares += 1;
4695 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4696 if (!share_list->shares) {
4697 share_list->num_shares = 0;
4700 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4704 * List shares on a remote RPC server, including the security descriptors.
4706 * All parameters are provided by the run_rpc_command function, except for
4707 * argc, argv which are passed through.
4709 * @param domain_sid The domain sid acquired from the remote server.
4710 * @param cli A cli_state connected to the server.
4711 * @param mem_ctx Talloc context, destroyed on completion of the function.
4712 * @param argc Standard main() style argc.
4713 * @param argv Standard main() style argv. Initial components are already
4716 * @return Normal NTSTATUS return.
4719 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4720 const struct dom_sid *domain_sid,
4721 const char *domain_name,
4722 struct cli_state *cli,
4723 struct rpc_pipe_client *pipe_hnd,
4724 TALLOC_CTX *mem_ctx,
4733 struct user_token *tokens = NULL;
4736 struct share_list share_list;
4741 f = fopen(argv[0], "r");
4745 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4746 return NT_STATUS_UNSUCCESSFUL;
4749 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4755 DEBUG(0, ("Could not read users from file\n"));
4756 return NT_STATUS_UNSUCCESSFUL;
4759 for (i=0; i<num_tokens; i++)
4760 collect_alias_memberships(&tokens[i].token);
4762 share_list.num_shares = 0;
4763 share_list.shares = NULL;
4765 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4768 DEBUG(0, ("Error returning browse list: %s\n",
4773 for (i = 0; i < share_list.num_shares; i++) {
4774 char *netname = share_list.shares[i];
4776 if (netname[strlen(netname)-1] == '$')
4779 d_printf("%s\n", netname);
4781 show_userlist(pipe_hnd, mem_ctx, netname,
4782 num_tokens, tokens);
4785 for (i=0; i<num_tokens; i++) {
4786 free_user_token(&tokens[i].token);
4789 SAFE_FREE(share_list.shares);
4791 return NT_STATUS_OK;
4794 static int rpc_share_allowedusers(struct net_context *c, int argc,
4799 if (c->display_usage) {
4801 "net rpc share allowedusers\n"
4804 _("List allowed users"));
4808 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4809 rpc_aliaslist_internals,
4814 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4820 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4821 rpc_share_allowedusers_internals,
4825 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4828 struct user_token *tokens = NULL;
4832 net_usersidlist_usage(c, argc, argv);
4836 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4837 DEBUG(0, ("Could not get the user/sid list\n"));
4841 for (i=0; i<num_tokens; i++) {
4842 dump_user_token(&tokens[i]);
4843 free_user_token(&tokens[i].token);
4850 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4852 d_printf(_("net usersidlist\n"
4853 "\tprints out a list of all users the running winbind knows\n"
4854 "\tabout, together with all their SIDs. This is used as\n"
4855 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4857 net_common_flags_usage(c, argc, argv);
4862 * 'net rpc share' entrypoint.
4863 * @param argc Standard main() style argc.
4864 * @param argv Standard main() style argv. Initial components are already
4868 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4870 NET_API_STATUS status;
4872 struct functable func[] = {
4878 N_("net rpc share add\n"
4886 N_("net rpc share delete\n"
4891 rpc_share_allowedusers,
4893 N_("Modify allowed users"),
4894 N_("net rpc share allowedusers\n"
4895 " Modify allowed users")
4901 N_("Migrate share to local server"),
4902 N_("net rpc share migrate\n"
4903 " Migrate share to local server")
4910 N_("net rpc share list\n"
4913 {NULL, NULL, 0, NULL, NULL}
4916 status = libnetapi_net_init(&c->netapi_ctx);
4920 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4921 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4922 if (c->opt_kerberos) {
4923 libnetapi_set_use_kerberos(c->netapi_ctx);
4927 if (c->display_usage) {
4932 " Alias for net rpc share list\n"));
4933 net_display_usage_from_functable(func);
4937 return rpc_share_list(c, argc, argv);
4940 return net_run_function(c, argc, argv, "net rpc share", func);
4943 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4944 TALLOC_CTX *mem_ctx,
4945 struct rpc_sh_ctx *ctx,
4946 struct rpc_pipe_client *pipe_hnd,
4947 int argc, const char **argv)
4950 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4953 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4954 TALLOC_CTX *mem_ctx,
4955 struct rpc_sh_ctx *ctx,
4956 struct rpc_pipe_client *pipe_hnd,
4957 int argc, const char **argv)
4959 NET_API_STATUS status;
4960 uint32_t parm_err = 0;
4961 struct SHARE_INFO_2 i2;
4963 if ((argc < 2) || (argc > 3)) {
4964 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
4966 return NT_STATUS_INVALID_PARAMETER;
4969 i2.shi2_netname = argv[0];
4970 i2.shi2_type = STYPE_DISKTREE;
4971 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4972 i2.shi2_permissions = 0;
4973 i2.shi2_max_uses = 0;
4974 i2.shi2_current_uses = 0;
4975 i2.shi2_path = argv[1];
4976 i2.shi2_passwd = NULL;
4978 status = NetShareAdd(pipe_hnd->desthost,
4983 return werror_to_ntstatus(W_ERROR(status));
4986 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4987 TALLOC_CTX *mem_ctx,
4988 struct rpc_sh_ctx *ctx,
4989 struct rpc_pipe_client *pipe_hnd,
4990 int argc, const char **argv)
4993 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4994 return NT_STATUS_INVALID_PARAMETER;
4997 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5000 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5001 TALLOC_CTX *mem_ctx,
5002 struct rpc_sh_ctx *ctx,
5003 struct rpc_pipe_client *pipe_hnd,
5004 int argc, const char **argv)
5006 union srvsvc_NetShareInfo info;
5009 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5012 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5013 return NT_STATUS_INVALID_PARAMETER;
5016 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5022 if (!NT_STATUS_IS_OK(status)) {
5023 result = ntstatus_to_werror(status);
5026 if (!W_ERROR_IS_OK(result)) {
5030 d_printf(_("Name: %s\n"), info.info2->name);
5031 d_printf(_("Comment: %s\n"), info.info2->comment);
5032 d_printf(_("Path: %s\n"), info.info2->path);
5033 d_printf(_("Password: %s\n"), info.info2->password);
5036 return werror_to_ntstatus(result);
5039 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5040 struct rpc_sh_ctx *ctx)
5042 static struct rpc_sh_cmd cmds[] = {
5044 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
5045 N_("List available shares") },
5047 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
5048 N_("Add a share") },
5050 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
5051 N_("Delete a share") },
5053 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
5054 N_("Get information about a share") },
5056 { NULL, NULL, 0, NULL, NULL }
5062 /****************************************************************************/
5064 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5066 return net_file_usage(c, argc, argv);
5070 * Close a file on a remote RPC server.
5072 * @param argc Standard main() style argc.
5073 * @param argv Standard main() style argv. Initial components are already
5076 * @return A shell status integer (0 for success).
5078 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5080 if (argc < 1 || c->display_usage) {
5081 return rpc_file_usage(c, argc, argv);
5084 return NetFileClose(c->opt_host, atoi(argv[0]));
5088 * Formatted print of open file info
5090 * @param r struct FILE_INFO_3 contents
5093 static void display_file_info_3(struct FILE_INFO_3 *r)
5095 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5096 r->fi3_id, r->fi3_username, r->fi3_permissions,
5097 r->fi3_num_locks, r->fi3_pathname);
5101 * List files for a user on a remote RPC server.
5103 * @param argc Standard main() style argc.
5104 * @param argv Standard main() style argv. Initial components are already
5107 * @return A shell status integer (0 for success)..
5110 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5112 NET_API_STATUS status;
5113 uint32 preferred_len = 0xffffffff, i;
5114 const char *username=NULL;
5115 uint32_t total_entries = 0;
5116 uint32_t entries_read = 0;
5117 uint32_t resume_handle = 0;
5118 struct FILE_INFO_3 *i3 = NULL;
5120 if (c->display_usage) {
5121 return rpc_file_usage(c, argc, argv);
5124 /* if argc > 0, must be user command */
5126 username = smb_xstrdup(argv[0]);
5129 status = NetFileEnum(c->opt_host,
5133 (uint8_t **)(void *)&i3,
5143 /* Display results */
5146 "\nEnumerating open files on remote server:\n\n"
5147 "\nFileId Opened by Perms Locks Path"
5148 "\n------ --------- ----- ----- ---- \n"));
5149 for (i = 0; i < entries_read; i++) {
5150 display_file_info_3(&i3[i]);
5157 * 'net rpc file' entrypoint.
5158 * @param argc Standard main() style argc.
5159 * @param argv Standard main() style argv. Initial components are already
5163 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5165 NET_API_STATUS status;
5167 struct functable func[] = {
5172 N_("Close opened file"),
5173 N_("net rpc file close\n"
5174 " Close opened file")
5180 N_("List files opened by user"),
5181 N_("net rpc file user\n"
5182 " List files opened by user")
5189 N_("Display information about opened file"),
5190 N_("net rpc file info\n"
5191 " Display information about opened file")
5194 {NULL, NULL, 0, NULL, NULL}
5197 status = libnetapi_net_init(&c->netapi_ctx);
5201 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5202 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5203 if (c->opt_kerberos) {
5204 libnetapi_set_use_kerberos(c->netapi_ctx);
5208 if (c->display_usage) {
5209 d_printf(_("Usage:\n"));
5210 d_printf(_("net rpc file\n"
5211 " List opened files\n"));
5212 net_display_usage_from_functable(func);
5216 return rpc_file_user(c, argc, argv);
5219 return net_run_function(c, argc, argv, "net rpc file", func);
5223 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5225 * All parameters are provided by the run_rpc_command function, except for
5226 * argc, argv which are passed through.
5228 * @param c A net_context structure.
5229 * @param domain_sid The domain sid acquired from the remote server.
5230 * @param cli A cli_state connected to the server.
5231 * @param mem_ctx Talloc context, destroyed on completion of the function.
5232 * @param argc Standard main() style argc.
5233 * @param argv Standard main() style argv. Initial components are already
5236 * @return Normal NTSTATUS return.
5239 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5240 const struct dom_sid *domain_sid,
5241 const char *domain_name,
5242 struct cli_state *cli,
5243 struct rpc_pipe_client *pipe_hnd,
5244 TALLOC_CTX *mem_ctx,
5248 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5250 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5252 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5253 if (!NT_STATUS_IS_OK(status)) {
5256 if (W_ERROR_IS_OK(result)) {
5257 d_printf(_("\nShutdown successfully aborted\n"));
5258 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5260 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5262 return werror_to_ntstatus(result);
5266 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5268 * All parameters are provided by the run_rpc_command function, except for
5269 * argc, argv which are passed through.
5271 * @param c A net_context structure.
5272 * @param domain_sid The domain sid acquired from the remote server.
5273 * @param cli A cli_state connected to the server.
5274 * @param mem_ctx Talloc context, destroyed on completion of the function.
5275 * @param argc Standard main() style argc.
5276 * @param argv Standard main() style argv. Initial components are already
5279 * @return Normal NTSTATUS return.
5282 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5283 const struct dom_sid *domain_sid,
5284 const char *domain_name,
5285 struct cli_state *cli,
5286 struct rpc_pipe_client *pipe_hnd,
5287 TALLOC_CTX *mem_ctx,
5291 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5293 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5295 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5297 if (!NT_STATUS_IS_OK(result)) {
5298 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5301 if (W_ERROR_IS_OK(werr)) {
5302 d_printf(_("\nShutdown successfully aborted\n"));
5303 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5305 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5307 return werror_to_ntstatus(werr);
5311 * ABORT the shutdown of a remote RPC server.
5313 * @param argc Standard main() style argc.
5314 * @param argv Standard main() style argv. Initial components are already
5317 * @return A shell status integer (0 for success).
5320 static int rpc_shutdown_abort(struct net_context *c, int argc,
5325 if (c->display_usage) {
5327 "net rpc abortshutdown\n"
5330 _("Abort a scheduled shutdown"));
5334 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5335 rpc_shutdown_abort_internals, argc, argv);
5340 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5342 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5343 rpc_reg_shutdown_abort_internals,
5348 * Shut down a remote RPC Server via initshutdown pipe.
5350 * All parameters are provided by the run_rpc_command function, except for
5351 * argc, argv which are passed through.
5353 * @param c A net_context structure.
5354 * @param domain_sid The domain sid acquired from the remote server.
5355 * @param cli A cli_state connected to the server.
5356 * @param mem_ctx Talloc context, destroyed on completion of the function.
5357 * @param argc Standard main() style argc.
5358 * @param argv Standard main() style argv. Initial components are already
5361 * @return Normal NTSTATUS return.
5364 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5365 const struct dom_sid *domain_sid,
5366 const char *domain_name,
5367 struct cli_state *cli,
5368 struct rpc_pipe_client *pipe_hnd,
5369 TALLOC_CTX *mem_ctx,
5373 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5375 const char *msg = N_("This machine will be shutdown shortly");
5376 uint32 timeout = 20;
5377 struct lsa_StringLarge msg_string;
5378 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5380 if (c->opt_comment) {
5381 msg = c->opt_comment;
5383 if (c->opt_timeout) {
5384 timeout = c->opt_timeout;
5387 msg_string.string = msg;
5389 /* create an entry */
5390 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5391 &msg_string, timeout, c->opt_force, c->opt_reboot,
5393 if (!NT_STATUS_IS_OK(status)) {
5396 if (W_ERROR_IS_OK(result)) {
5397 d_printf(_("\nShutdown of remote machine succeeded\n"));
5398 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5400 DEBUG(1,("Shutdown of remote machine failed!\n"));
5402 return werror_to_ntstatus(result);
5406 * Shut down a remote RPC Server via winreg pipe.
5408 * All parameters are provided by the run_rpc_command function, except for
5409 * argc, argv which are passed through.
5411 * @param c A net_context structure.
5412 * @param domain_sid The domain sid acquired from the remote server.
5413 * @param cli A cli_state connected to the server.
5414 * @param mem_ctx Talloc context, destroyed on completion of the function.
5415 * @param argc Standard main() style argc.
5416 * @param argv Standard main() style argv. Initial components are already
5419 * @return Normal NTSTATUS return.
5422 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5423 const struct dom_sid *domain_sid,
5424 const char *domain_name,
5425 struct cli_state *cli,
5426 struct rpc_pipe_client *pipe_hnd,
5427 TALLOC_CTX *mem_ctx,
5431 const char *msg = N_("This machine will be shutdown shortly");
5432 uint32 timeout = 20;
5433 struct lsa_StringLarge msg_string;
5436 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5438 if (c->opt_comment) {
5439 msg = c->opt_comment;
5441 msg_string.string = msg;
5443 if (c->opt_timeout) {
5444 timeout = c->opt_timeout;
5447 /* create an entry */
5448 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5449 &msg_string, timeout, c->opt_force, c->opt_reboot,
5451 if (!NT_STATUS_IS_OK(result)) {
5452 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5456 if (W_ERROR_IS_OK(werr)) {
5457 d_printf(_("\nShutdown of remote machine succeeded\n"));
5459 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5460 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5461 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5463 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5466 return werror_to_ntstatus(werr);
5470 * Shut down a remote RPC server.
5472 * @param argc Standard main() style argc.
5473 * @param argv Standard main() style argv. Initial components are already
5476 * @return A shell status integer (0 for success).
5479 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5483 if (c->display_usage) {
5485 "net rpc shutdown\n"
5488 _("Shut down a remote RPC server"));
5492 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5493 rpc_init_shutdown_internals, argc, argv);
5496 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5497 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5498 rpc_reg_shutdown_internals, argc, argv);
5504 /***************************************************************************
5505 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5506 ***************************************************************************/
5509 * Add interdomain trust account to the RPC server.
5510 * All parameters (except for argc and argv) are passed by run_rpc_command
5513 * @param c A net_context structure.
5514 * @param domain_sid The domain sid acquired from the server.
5515 * @param cli A cli_state connected to the server.
5516 * @param mem_ctx Talloc context, destroyed on completion of the function.
5517 * @param argc Standard main() style argc.
5518 * @param argv Standard main() style argv. Initial components are already
5521 * @return normal NTSTATUS return code.
5524 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5525 const struct dom_sid *domain_sid,
5526 const char *domain_name,
5527 struct cli_state *cli,
5528 struct rpc_pipe_client *pipe_hnd,
5529 TALLOC_CTX *mem_ctx,
5533 struct policy_handle connect_pol, domain_pol, user_pol;
5534 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5536 struct lsa_String lsa_acct_name;
5538 uint32 acct_flags=0;
5540 uint32_t access_granted = 0;
5541 union samr_UserInfo info;
5542 unsigned int orig_timeout;
5547 _(" net rpc trustdom add <domain_name> "
5548 "<trust password>\n"));
5549 return NT_STATUS_INVALID_PARAMETER;
5553 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5556 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5557 return NT_STATUS_NO_MEMORY;
5560 strupper_m(acct_name);
5562 init_lsa_String(&lsa_acct_name, acct_name);
5564 /* Get samr policy handle */
5565 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5567 MAXIMUM_ALLOWED_ACCESS,
5569 if (!NT_STATUS_IS_OK(result)) {
5573 /* Get domain policy handle */
5574 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5576 MAXIMUM_ALLOWED_ACCESS,
5577 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5579 if (!NT_STATUS_IS_OK(result)) {
5583 /* This call can take a long time - allow the server to time out.
5584 * 35 seconds should do it. */
5586 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5588 /* Create trusting domain's account */
5589 acb_info = ACB_NORMAL;
5590 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5591 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5592 SAMR_USER_ACCESS_SET_PASSWORD |
5593 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5594 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5596 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5605 /* And restore our original timeout. */
5606 rpccli_set_timeout(pipe_hnd, orig_timeout);
5608 if (!NT_STATUS_IS_OK(result)) {
5609 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5610 acct_name, nt_errstr(result));
5615 struct samr_CryptPassword crypt_pwd;
5617 ZERO_STRUCT(info.info23);
5619 init_samr_CryptPassword(argv[1],
5620 &cli->user_session_key,
5623 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5624 SAMR_FIELD_NT_PASSWORD_PRESENT;
5625 info.info23.info.acct_flags = ACB_DOMTRUST;
5626 info.info23.password = crypt_pwd;
5628 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5633 if (!NT_STATUS_IS_OK(result)) {
5634 DEBUG(0,("Could not set trust account password: %s\n",
5635 nt_errstr(result)));
5641 SAFE_FREE(acct_name);
5646 * Create interdomain trust account for a remote domain.
5648 * @param argc Standard argc.
5649 * @param argv Standard argv without initial components.
5651 * @return Integer status (0 means success).
5654 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5656 if (argc > 0 && !c->display_usage) {
5657 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5658 rpc_trustdom_add_internals, argc, argv);
5662 _("net rpc trustdom add <domain_name> <trust "
5670 * Remove interdomain trust account from the RPC server.
5671 * All parameters (except for argc and argv) are passed by run_rpc_command
5674 * @param c A net_context structure.
5675 * @param domain_sid The domain sid acquired from the server.
5676 * @param cli A cli_state connected to the server.
5677 * @param mem_ctx Talloc context, destroyed on completion of the function.
5678 * @param argc Standard main() style argc.
5679 * @param argv Standard main() style argv. Initial components are already
5682 * @return normal NTSTATUS return code.
5685 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5686 const struct dom_sid *domain_sid,
5687 const char *domain_name,
5688 struct cli_state *cli,
5689 struct rpc_pipe_client *pipe_hnd,
5690 TALLOC_CTX *mem_ctx,
5694 struct policy_handle connect_pol, domain_pol, user_pol;
5695 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5697 struct dom_sid trust_acct_sid;
5698 struct samr_Ids user_rids, name_types;
5699 struct lsa_String lsa_acct_name;
5704 _(" net rpc trustdom del <domain_name>\n"));
5705 return NT_STATUS_INVALID_PARAMETER;
5709 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5711 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5713 if (acct_name == NULL)
5714 return NT_STATUS_NO_MEMORY;
5716 strupper_m(acct_name);
5718 /* Get samr policy handle */
5719 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5721 MAXIMUM_ALLOWED_ACCESS,
5723 if (!NT_STATUS_IS_OK(result)) {
5727 /* Get domain policy handle */
5728 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5730 MAXIMUM_ALLOWED_ACCESS,
5731 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5733 if (!NT_STATUS_IS_OK(result)) {
5737 init_lsa_String(&lsa_acct_name, acct_name);
5739 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5746 if (!NT_STATUS_IS_OK(result)) {
5747 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5749 acct_name, nt_errstr(result) );
5753 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5755 MAXIMUM_ALLOWED_ACCESS,
5759 if (!NT_STATUS_IS_OK(result)) {
5760 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5762 acct_name, nt_errstr(result) );
5766 /* append the rid to the domain sid */
5767 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
5771 /* remove the sid */
5773 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5776 if (!NT_STATUS_IS_OK(result)) {
5777 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5778 " on user %s failed %s\n"),
5779 acct_name, nt_errstr(result) );
5785 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5788 if (!NT_STATUS_IS_OK(result)) {
5789 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5791 acct_name, nt_errstr(result) );
5795 if (!NT_STATUS_IS_OK(result)) {
5796 d_printf(_("Could not set trust account password: %s\n"),
5806 * Delete interdomain trust account for a remote domain.
5808 * @param argc Standard argc.
5809 * @param argv Standard argv without initial components.
5811 * @return Integer status (0 means success).
5814 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5816 if (argc > 0 && !c->display_usage) {
5817 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5818 rpc_trustdom_del_internals, argc, argv);
5822 _("net rpc trustdom del <domain>\n"));
5827 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5828 struct cli_state *cli,
5829 TALLOC_CTX *mem_ctx,
5830 const char *domain_name)
5832 char *dc_name = NULL;
5833 const char *buffer = NULL;
5834 struct rpc_pipe_client *netr;
5837 struct dcerpc_binding_handle *b;
5839 /* Use NetServerEnum2 */
5841 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5843 return NT_STATUS_OK;
5846 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5847 for domain %s\n", domain_name));
5849 /* Try netr_GetDcName */
5851 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5853 if (!NT_STATUS_IS_OK(status)) {
5857 b = netr->binding_handle;
5859 status = dcerpc_netr_GetDcName(b, mem_ctx,
5866 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
5870 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5871 for domain %s\n", domain_name));
5873 if (!NT_STATUS_IS_OK(status)) {
5877 return werror_to_ntstatus(result);
5881 * Establish trust relationship to a trusting domain.
5882 * Interdomain account must already be created on remote PDC.
5884 * @param c A net_context structure.
5885 * @param argc Standard argc.
5886 * @param argv Standard argv without initial components.
5888 * @return Integer status (0 means success).
5891 static int rpc_trustdom_establish(struct net_context *c, int argc,
5894 struct cli_state *cli = NULL;
5895 struct sockaddr_storage server_ss;
5896 struct rpc_pipe_client *pipe_hnd = NULL;
5897 struct policy_handle connect_hnd;
5898 TALLOC_CTX *mem_ctx;
5900 struct dom_sid *domain_sid;
5905 union lsa_PolicyInformation *info = NULL;
5908 * Connect to \\server\ipc$ as 'our domain' account with password
5911 if (argc != 1 || c->display_usage) {
5914 _("net rpc trustdom establish <domain_name>\n"));
5918 domain_name = smb_xstrdup(argv[0]);
5919 strupper_m(domain_name);
5921 /* account name used at first is our domain's name with '$' */
5922 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5925 strupper_m(acct_name);
5928 * opt_workgroup will be used by connection functions further,
5929 * hence it should be set to remote domain name instead of ours
5931 if (c->opt_workgroup) {
5932 c->opt_workgroup = smb_xstrdup(domain_name);
5935 c->opt_user_name = acct_name;
5937 /* find the domain controller */
5938 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5939 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5943 /* connect to ipc$ as username/password */
5944 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5945 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5947 /* Is it trusting domain account for sure ? */
5948 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5949 nt_errstr(nt_status)));
5953 /* store who we connected to */
5955 saf_store( domain_name, pdc_name );
5958 * Connect to \\server\ipc$ again (this time anonymously)
5961 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5964 if (NT_STATUS_IS_ERR(nt_status)) {
5965 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5966 domain_name, nt_errstr(nt_status)));
5970 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5971 "domain %s", domain_name))) {
5972 DEBUG(0, ("talloc_init() failed\n"));
5977 /* Make sure we're talking to a proper server */
5979 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5980 if (!NT_STATUS_IS_OK(nt_status)) {
5982 talloc_destroy(mem_ctx);
5987 * Call LsaOpenPolicy and LsaQueryInfo
5990 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5992 if (!NT_STATUS_IS_OK(nt_status)) {
5993 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5995 talloc_destroy(mem_ctx);
5999 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6001 if (NT_STATUS_IS_ERR(nt_status)) {
6002 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6003 nt_errstr(nt_status)));
6005 talloc_destroy(mem_ctx);
6009 /* Querying info level 5 */
6011 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6013 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6015 if (NT_STATUS_IS_ERR(nt_status)) {
6016 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6017 nt_errstr(nt_status)));
6019 talloc_destroy(mem_ctx);
6023 domain_sid = info->account_domain.sid;
6025 /* There should be actually query info level 3 (following nt serv behaviour),
6026 but I still don't know if it's _really_ necessary */
6029 * Store the password in secrets db
6032 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6033 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6035 talloc_destroy(mem_ctx);
6040 * Close the pipes and clean up
6043 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6044 if (NT_STATUS_IS_ERR(nt_status)) {
6045 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6046 nt_errstr(nt_status)));
6048 talloc_destroy(mem_ctx);
6054 talloc_destroy(mem_ctx);
6056 d_printf(_("Trust to domain %s established\n"), domain_name);
6061 * Revoke trust relationship to the remote domain.
6063 * @param c A net_context structure.
6064 * @param argc Standard argc.
6065 * @param argv Standard argv without initial components.
6067 * @return Integer status (0 means success).
6070 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6076 if (argc < 1 || c->display_usage) {
6079 _("net rpc trustdom revoke <domain_name>\n"
6080 " Revoke trust relationship\n"
6081 " domain_name\tName of domain to revoke trust\n"));
6085 /* generate upper cased domain name */
6086 domain_name = smb_xstrdup(argv[0]);
6087 strupper_m(domain_name);
6089 /* delete password of the trust */
6090 if (!pdb_del_trusteddom_pw(domain_name)) {
6091 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6098 SAFE_FREE(domain_name);
6102 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6103 const struct dom_sid *domain_sid,
6104 const char *domain_name,
6105 struct cli_state *cli,
6106 struct rpc_pipe_client *pipe_hnd,
6107 TALLOC_CTX *mem_ctx,
6112 if (!sid_to_fstring(str_sid, domain_sid)) {
6113 return NT_STATUS_UNSUCCESSFUL;
6115 d_printf("%s\n", str_sid);
6116 return NT_STATUS_OK;
6119 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6123 /* convert sid into ascii string */
6124 sid_to_fstring(ascii_sid, dom_sid);
6126 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6129 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6130 TALLOC_CTX *mem_ctx,
6131 struct policy_handle *pol,
6132 struct dom_sid dom_sid,
6133 const char *trusted_dom_name)
6136 union lsa_TrustedDomainInfo *info = NULL;
6137 char *cleartextpwd = NULL;
6138 uint8_t session_key[16];
6139 DATA_BLOB session_key_blob;
6140 DATA_BLOB data = data_blob_null;
6142 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6145 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6147 if (NT_STATUS_IS_ERR(nt_status)) {
6148 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6149 nt_errstr(nt_status)));
6153 data = data_blob(info->password.password->data,
6154 info->password.password->length);
6156 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
6157 DEBUG(0, ("Could not retrieve password hash\n"));
6161 session_key_blob = data_blob_const(session_key, sizeof(session_key));
6162 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
6164 if (cleartextpwd == NULL) {
6165 DEBUG(0,("retrieved NULL password\n"));
6166 nt_status = NT_STATUS_UNSUCCESSFUL;
6170 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6171 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6172 nt_status = NT_STATUS_UNSUCCESSFUL;
6176 #ifdef DEBUG_PASSWORD
6177 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6178 "password: [%s]\n", trusted_dom_name,
6179 sid_string_dbg(&dom_sid), cleartextpwd));
6183 SAFE_FREE(cleartextpwd);
6184 data_blob_free(&data);
6189 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6192 /* common variables */
6193 TALLOC_CTX* mem_ctx;
6194 struct cli_state *cli = NULL;
6195 struct rpc_pipe_client *pipe_hnd = NULL;
6197 const char *domain_name = NULL;
6198 struct dom_sid *queried_dom_sid;
6199 struct policy_handle connect_hnd;
6200 union lsa_PolicyInformation *info = NULL;
6202 /* trusted domains listing variables */
6203 unsigned int enum_ctx = 0;
6205 struct lsa_DomainList dom_list;
6208 if (c->display_usage) {
6210 "net rpc trustdom vampire\n"
6213 _("Vampire trust relationship from remote server"));
6218 * Listing trusted domains (stored in secrets.tdb, if local)
6221 mem_ctx = talloc_init("trust relationships vampire");
6224 * set domain and pdc name to local samba server (default)
6225 * or to remote one given in command line
6228 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6229 domain_name = c->opt_workgroup;
6230 c->opt_target_workgroup = c->opt_workgroup;
6232 fstrcpy(pdc_name, global_myname());
6233 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6234 c->opt_target_workgroup = domain_name;
6237 /* open \PIPE\lsarpc and open policy handle */
6238 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6239 if (!NT_STATUS_IS_OK(nt_status)) {
6240 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6241 nt_errstr(nt_status)));
6242 talloc_destroy(mem_ctx);
6246 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6248 if (!NT_STATUS_IS_OK(nt_status)) {
6249 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6250 nt_errstr(nt_status) ));
6252 talloc_destroy(mem_ctx);
6256 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6258 if (NT_STATUS_IS_ERR(nt_status)) {
6259 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6260 nt_errstr(nt_status)));
6262 talloc_destroy(mem_ctx);
6266 /* query info level 5 to obtain sid of a domain being queried */
6267 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6269 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6272 if (NT_STATUS_IS_ERR(nt_status)) {
6273 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6274 nt_errstr(nt_status)));
6276 talloc_destroy(mem_ctx);
6280 queried_dom_sid = info->account_domain.sid;
6283 * Keep calling LsaEnumTrustdom over opened pipe until
6284 * the end of enumeration is reached
6287 d_printf(_("Vampire trusted domains:\n\n"));
6290 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6295 if (NT_STATUS_IS_ERR(nt_status)) {
6296 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6297 nt_errstr(nt_status)));
6299 talloc_destroy(mem_ctx);
6303 for (i = 0; i < dom_list.count; i++) {
6305 print_trusted_domain(dom_list.domains[i].sid,
6306 dom_list.domains[i].name.string);
6308 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6309 *dom_list.domains[i].sid,
6310 dom_list.domains[i].name.string);
6311 if (!NT_STATUS_IS_OK(nt_status)) {
6313 talloc_destroy(mem_ctx);
6319 * in case of no trusted domains say something rather
6320 * than just display blank line
6322 if (!dom_list.count) d_printf(_("none\n"));
6324 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6326 /* close this connection before doing next one */
6327 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6328 if (NT_STATUS_IS_ERR(nt_status)) {
6329 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6330 nt_errstr(nt_status)));
6332 talloc_destroy(mem_ctx);
6336 /* close lsarpc pipe and connection to IPC$ */
6339 talloc_destroy(mem_ctx);
6343 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6345 /* common variables */
6346 TALLOC_CTX* mem_ctx;
6347 struct cli_state *cli = NULL, *remote_cli = NULL;
6348 struct rpc_pipe_client *pipe_hnd = NULL;
6350 const char *domain_name = NULL;
6351 struct dom_sid *queried_dom_sid;
6352 int ascii_dom_name_len;
6353 struct policy_handle connect_hnd;
6354 union lsa_PolicyInformation *info = NULL;
6356 /* trusted domains listing variables */
6357 unsigned int num_domains, enum_ctx = 0;
6359 struct lsa_DomainList dom_list;
6363 /* trusting domains listing variables */
6364 struct policy_handle domain_hnd;
6365 struct samr_SamArray *trusts = NULL;
6367 if (c->display_usage) {
6369 "net rpc trustdom list\n"
6372 _("List incoming and outgoing trust relationships"));
6377 * Listing trusted domains (stored in secrets.tdb, if local)
6380 mem_ctx = talloc_init("trust relationships listing");
6383 * set domain and pdc name to local samba server (default)
6384 * or to remote one given in command line
6387 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6388 domain_name = c->opt_workgroup;
6389 c->opt_target_workgroup = c->opt_workgroup;
6391 fstrcpy(pdc_name, global_myname());
6392 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6393 c->opt_target_workgroup = domain_name;
6396 /* open \PIPE\lsarpc and open policy handle */
6397 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6398 if (!NT_STATUS_IS_OK(nt_status)) {
6399 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6400 nt_errstr(nt_status)));
6401 talloc_destroy(mem_ctx);
6405 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6407 if (!NT_STATUS_IS_OK(nt_status)) {
6408 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6409 nt_errstr(nt_status) ));
6411 talloc_destroy(mem_ctx);
6415 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6417 if (NT_STATUS_IS_ERR(nt_status)) {
6418 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6419 nt_errstr(nt_status)));
6421 talloc_destroy(mem_ctx);
6425 /* query info level 5 to obtain sid of a domain being queried */
6426 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6428 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6431 if (NT_STATUS_IS_ERR(nt_status)) {
6432 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6433 nt_errstr(nt_status)));
6435 talloc_destroy(mem_ctx);
6439 queried_dom_sid = info->account_domain.sid;
6442 * Keep calling LsaEnumTrustdom over opened pipe until
6443 * the end of enumeration is reached
6446 d_printf(_("Trusted domains list:\n\n"));
6448 found_domain = false;
6451 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6456 if (NT_STATUS_IS_ERR(nt_status)) {
6457 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6458 nt_errstr(nt_status)));
6460 talloc_destroy(mem_ctx);
6464 for (i = 0; i < dom_list.count; i++) {
6465 print_trusted_domain(dom_list.domains[i].sid,
6466 dom_list.domains[i].name.string);
6467 found_domain = true;
6471 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6474 * in case of no trusted domains say something rather
6475 * than just display blank line
6477 if (!found_domain) {
6478 d_printf(_("none\n"));
6481 /* close this connection before doing next one */
6482 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6483 if (NT_STATUS_IS_ERR(nt_status)) {
6484 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6485 nt_errstr(nt_status)));
6487 talloc_destroy(mem_ctx);
6491 TALLOC_FREE(pipe_hnd);
6494 * Listing trusting domains (stored in passdb backend, if local)
6497 d_printf(_("\nTrusting domains list:\n\n"));
6500 * Open \PIPE\samr and get needed policy handles
6502 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6504 if (!NT_STATUS_IS_OK(nt_status)) {
6505 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6507 talloc_destroy(mem_ctx);
6512 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6514 SAMR_ACCESS_LOOKUP_DOMAIN,
6516 if (!NT_STATUS_IS_OK(nt_status)) {
6517 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6518 nt_errstr(nt_status)));
6520 talloc_destroy(mem_ctx);
6524 /* SamrOpenDomain - we have to open domain policy handle in order to be
6525 able to enumerate accounts*/
6526 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6528 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6531 if (!NT_STATUS_IS_OK(nt_status)) {
6532 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6533 nt_errstr(nt_status)));
6535 talloc_destroy(mem_ctx);
6540 * perform actual enumeration
6543 found_domain = false;
6545 enum_ctx = 0; /* reset enumeration context from last enumeration */
6548 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6555 if (NT_STATUS_IS_ERR(nt_status)) {
6556 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6557 nt_errstr(nt_status)));
6559 talloc_destroy(mem_ctx);
6563 for (i = 0; i < num_domains; i++) {
6565 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6567 found_domain = true;
6570 * get each single domain's sid (do we _really_ need this ?):
6571 * 1) connect to domain's pdc
6572 * 2) query the pdc for domain's sid
6575 /* get rid of '$' tail */
6576 ascii_dom_name_len = strlen(str);
6577 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6578 str[ascii_dom_name_len - 1] = '\0';
6580 /* set opt_* variables to remote domain */
6582 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6583 c->opt_target_workgroup = c->opt_workgroup;
6585 d_printf("%-20s", str);
6587 /* connect to remote domain controller */
6588 nt_status = net_make_ipc_connection(c,
6589 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6591 if (NT_STATUS_IS_OK(nt_status)) {
6592 /* query for domain's sid */
6593 if (run_rpc_command(
6595 &ndr_table_lsarpc.syntax_id, 0,
6596 rpc_query_domain_sid, argc,
6598 d_printf(_("strange - couldn't get domain's sid\n"));
6600 cli_shutdown(remote_cli);
6603 d_fprintf(stderr, _("domain controller is not "
6604 "responding: %s\n"),
6605 nt_errstr(nt_status));
6606 d_printf(_("couldn't get domain's sid\n"));
6610 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6612 if (!found_domain) {
6616 /* close opened samr and domain policy handles */
6617 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6618 if (!NT_STATUS_IS_OK(nt_status)) {
6619 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6622 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6623 if (!NT_STATUS_IS_OK(nt_status)) {
6624 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6627 /* close samr pipe and connection to IPC$ */
6630 talloc_destroy(mem_ctx);
6635 * Entrypoint for 'net rpc trustdom' code.
6637 * @param argc Standard argc.
6638 * @param argv Standard argv without initial components.
6640 * @return Integer status (0 means success).
6643 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6645 struct functable func[] = {
6650 N_("Add trusting domain's account"),
6651 N_("net rpc trustdom add\n"
6652 " Add trusting domain's account")
6658 N_("Remove trusting domain's account"),
6659 N_("net rpc trustdom del\n"
6660 " Remove trusting domain's account")
6664 rpc_trustdom_establish,
6666 N_("Establish outgoing trust relationship"),
6667 N_("net rpc trustdom establish\n"
6668 " Establish outgoing trust relationship")
6672 rpc_trustdom_revoke,
6674 N_("Revoke outgoing trust relationship"),
6675 N_("net rpc trustdom revoke\n"
6676 " Revoke outgoing trust relationship")
6682 N_("List in- and outgoing domain trusts"),
6683 N_("net rpc trustdom list\n"
6684 " List in- and outgoing domain trusts")
6688 rpc_trustdom_vampire,
6690 N_("Vampire trusts from remote server"),
6691 N_("net rpc trustdom vampire\n"
6692 " Vampire trusts from remote server")
6694 {NULL, NULL, 0, NULL, NULL}
6697 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6701 * Check if a server will take rpc commands
6702 * @param flags Type of server to connect to (PDC, DMB, localhost)
6703 * if the host is not explicitly specified
6704 * @return bool (true means rpc supported)
6706 bool net_rpc_check(struct net_context *c, unsigned flags)
6708 struct cli_state *cli;
6710 struct sockaddr_storage server_ss;
6711 char *server_name = NULL;
6714 /* flags (i.e. server type) may depend on command */
6715 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6718 if ((cli = cli_initialise()) == NULL) {
6722 status = cli_connect(cli, server_name, &server_ss);
6723 if (!NT_STATUS_IS_OK(status))
6725 if (!attempt_netbios_session_request(&cli, global_myname(),
6726 server_name, &server_ss))
6728 status = cli_negprot(cli);
6729 if (!NT_STATUS_IS_OK(status))
6731 if (cli->protocol < PROTOCOL_NT1)
6740 /* dump sam database via samsync rpc calls */
6741 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6742 if (c->display_usage) {
6747 _("Dump remote SAM database"));
6751 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6752 NET_FLAGS_ANONYMOUS,
6753 rpc_samdump_internals, argc, argv);
6756 /* syncronise sam database via samsync rpc calls */
6757 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6759 struct functable func[] = {
6764 N_("Dump remote SAM database to ldif"),
6765 N_("net rpc vampire ldif\n"
6766 " Dump remote SAM database to LDIF file or "
6773 N_("Dump remote SAM database to Kerberos Keytab"),
6774 N_("net rpc vampire keytab\n"
6775 " Dump remote SAM database to Kerberos keytab "
6782 N_("Dump remote SAM database to passdb"),
6783 N_("net rpc vampire passdb\n"
6784 " Dump remote SAM database to passdb")
6787 {NULL, NULL, 0, NULL, NULL}
6791 if (c->display_usage) {
6796 _("Vampire remote SAM database"));
6800 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6801 NET_FLAGS_ANONYMOUS,
6802 rpc_vampire_internals,
6806 return net_run_function(c, argc, argv, "net rpc vampire", func);
6810 * Migrate everything from a print server.
6812 * @param c A net_context structure.
6813 * @param argc Standard main() style argc.
6814 * @param argv Standard main() style argv. Initial components are already
6817 * @return A shell status integer (0 for success).
6819 * The order is important !
6820 * To successfully add drivers the print queues have to exist !
6821 * Applying ACLs should be the last step, because you're easily locked out.
6824 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6829 if (c->display_usage) {
6831 "net rpc printer migrate all\n"
6834 _("Migrate everything from a print server"));
6839 d_printf(_("no server to migrate\n"));
6843 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6844 rpc_printer_migrate_printers_internals, argc,
6849 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6850 rpc_printer_migrate_drivers_internals, argc,
6855 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6856 rpc_printer_migrate_forms_internals, argc, argv);
6860 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6861 rpc_printer_migrate_settings_internals, argc,
6866 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6867 rpc_printer_migrate_security_internals, argc,
6873 * Migrate print drivers from a print server.
6875 * @param c A net_context structure.
6876 * @param argc Standard main() style argc.
6877 * @param argv Standard main() style argv. Initial components are already
6880 * @return A shell status integer (0 for success).
6882 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6885 if (c->display_usage) {
6887 "net rpc printer migrate drivers\n"
6890 _("Migrate print-drivers from a print-server"));
6895 d_printf(_("no server to migrate\n"));
6899 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6900 rpc_printer_migrate_drivers_internals,
6905 * Migrate print-forms from a print-server.
6907 * @param c A net_context structure.
6908 * @param argc Standard main() style argc.
6909 * @param argv Standard main() style argv. Initial components are already
6912 * @return A shell status integer (0 for success).
6914 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6917 if (c->display_usage) {
6919 "net rpc printer migrate forms\n"
6922 _("Migrate print-forms from a print-server"));
6927 d_printf(_("no server to migrate\n"));
6931 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6932 rpc_printer_migrate_forms_internals,
6937 * Migrate printers from a print-server.
6939 * @param c A net_context structure.
6940 * @param argc Standard main() style argc.
6941 * @param argv Standard main() style argv. Initial components are already
6944 * @return A shell status integer (0 for success).
6946 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6949 if (c->display_usage) {
6951 "net rpc printer migrate printers\n"
6954 _("Migrate printers from a print-server"));
6959 d_printf(_("no server to migrate\n"));
6963 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6964 rpc_printer_migrate_printers_internals,
6969 * Migrate printer-ACLs from a print-server
6971 * @param c A net_context structure.
6972 * @param argc Standard main() style argc.
6973 * @param argv Standard main() style argv. Initial components are already
6976 * @return A shell status integer (0 for success).
6978 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6981 if (c->display_usage) {
6983 "net rpc printer migrate security\n"
6986 _("Migrate printer-ACLs from a print-server"));
6991 d_printf(_("no server to migrate\n"));
6995 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6996 rpc_printer_migrate_security_internals,
7001 * Migrate printer-settings from a print-server.
7003 * @param c A net_context structure.
7004 * @param argc Standard main() style argc.
7005 * @param argv Standard main() style argv. Initial components are already
7008 * @return A shell status integer (0 for success).
7010 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7013 if (c->display_usage) {
7015 "net rpc printer migrate settings\n"
7018 _("Migrate printer-settings from a "
7024 d_printf(_("no server to migrate\n"));
7028 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7029 rpc_printer_migrate_settings_internals,
7034 * 'net rpc printer' entrypoint.
7036 * @param c A net_context structure.
7037 * @param argc Standard main() style argc.
7038 * @param argv Standard main() style argv. Initial components are already
7042 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7045 /* ouch: when addriver and setdriver are called from within
7046 rpc_printer_migrate_drivers_internals, the printer-queue already
7049 struct functable func[] = {
7052 rpc_printer_migrate_all,
7054 N_("Migrate all from remote to local print server"),
7055 N_("net rpc printer migrate all\n"
7056 " Migrate all from remote to local print server")
7060 rpc_printer_migrate_drivers,
7062 N_("Migrate drivers to local server"),
7063 N_("net rpc printer migrate drivers\n"
7064 " Migrate drivers to local server")
7068 rpc_printer_migrate_forms,
7070 N_("Migrate froms to local server"),
7071 N_("net rpc printer migrate forms\n"
7072 " Migrate froms to local server")
7076 rpc_printer_migrate_printers,
7078 N_("Migrate printers to local server"),
7079 N_("net rpc printer migrate printers\n"
7080 " Migrate printers to local server")
7084 rpc_printer_migrate_security,
7086 N_("Mirgate printer ACLs to local server"),
7087 N_("net rpc printer migrate security\n"
7088 " Mirgate printer ACLs to local server")
7092 rpc_printer_migrate_settings,
7094 N_("Migrate printer settings to local server"),
7095 N_("net rpc printer migrate settings\n"
7096 " Migrate printer settings to local server")
7098 {NULL, NULL, 0, NULL, NULL}
7101 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7106 * List printers on a remote RPC server.
7108 * @param c A net_context structure.
7109 * @param argc Standard main() style argc.
7110 * @param argv Standard main() style argv. Initial components are already
7113 * @return A shell status integer (0 for success).
7115 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7117 if (c->display_usage) {
7119 "net rpc printer list\n"
7122 _("List printers on a remote RPC server"));
7126 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7127 rpc_printer_list_internals,
7132 * List printer-drivers on a remote RPC server.
7134 * @param c A net_context structure.
7135 * @param argc Standard main() style argc.
7136 * @param argv Standard main() style argv. Initial components are already
7139 * @return A shell status integer (0 for success).
7141 static int rpc_printer_driver_list(struct net_context *c, int argc,
7144 if (c->display_usage) {
7146 "net rpc printer driver\n"
7149 _("List printer-drivers on a remote RPC server"));
7153 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7154 rpc_printer_driver_list_internals,
7159 * Publish printer in ADS via MSRPC.
7161 * @param c A net_context structure.
7162 * @param argc Standard main() style argc.
7163 * @param argv Standard main() style argv. Initial components are already
7166 * @return A shell status integer (0 for success).
7168 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7171 if (c->display_usage) {
7173 "net rpc printer publish publish\n"
7176 _("Publish printer in ADS via MSRPC"));
7180 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7181 rpc_printer_publish_publish_internals,
7186 * Update printer in ADS via MSRPC.
7188 * @param c A net_context structure.
7189 * @param argc Standard main() style argc.
7190 * @param argv Standard main() style argv. Initial components are already
7193 * @return A shell status integer (0 for success).
7195 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7197 if (c->display_usage) {
7199 "net rpc printer publish update\n"
7202 _("Update printer in ADS via MSRPC"));
7206 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7207 rpc_printer_publish_update_internals,
7212 * UnPublish printer in ADS via MSRPC.
7214 * @param c A net_context structure.
7215 * @param argc Standard main() style argc.
7216 * @param argv Standard main() style argv. Initial components are already
7219 * @return A shell status integer (0 for success).
7221 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7224 if (c->display_usage) {
7226 "net rpc printer publish unpublish\n"
7229 _("UnPublish printer in ADS via MSRPC"));
7233 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7234 rpc_printer_publish_unpublish_internals,
7239 * List published printers via MSRPC.
7241 * @param c A net_context structure.
7242 * @param argc Standard main() style argc.
7243 * @param argv Standard main() style argv. Initial components are already
7246 * @return A shell status integer (0 for success).
7248 static int rpc_printer_publish_list(struct net_context *c, int argc,
7251 if (c->display_usage) {
7253 "net rpc printer publish list\n"
7256 _("List published printers via MSRPC"));
7260 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7261 rpc_printer_publish_list_internals,
7267 * Publish printer in ADS.
7269 * @param c A net_context structure.
7270 * @param argc Standard main() style argc.
7271 * @param argv Standard main() style argv. Initial components are already
7274 * @return A shell status integer (0 for success).
7276 static int rpc_printer_publish(struct net_context *c, int argc,
7280 struct functable func[] = {
7283 rpc_printer_publish_publish,
7285 N_("Publish printer in AD"),
7286 N_("net rpc printer publish publish\n"
7287 " Publish printer in AD")
7291 rpc_printer_publish_update,
7293 N_("Update printer in AD"),
7294 N_("net rpc printer publish update\n"
7295 " Update printer in AD")
7299 rpc_printer_publish_unpublish,
7301 N_("Unpublish printer"),
7302 N_("net rpc printer publish unpublish\n"
7303 " Unpublish printer")
7307 rpc_printer_publish_list,
7309 N_("List published printers"),
7310 N_("net rpc printer publish list\n"
7311 " List published printers")
7313 {NULL, NULL, 0, NULL, NULL}
7317 if (c->display_usage) {
7318 d_printf(_("Usage:\n"));
7319 d_printf(_("net rpc printer publish\n"
7320 " List published printers\n"
7321 " Alias of net rpc printer publish "
7323 net_display_usage_from_functable(func);
7326 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7327 rpc_printer_publish_list_internals,
7331 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7337 * Display rpc printer help page.
7339 * @param c A net_context structure.
7340 * @param argc Standard main() style argc.
7341 * @param argv Standard main() style argv. Initial components are already
7344 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7346 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7347 "\tlists all printers on print-server\n\n"));
7348 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7349 "\tlists all printer-drivers on print-server\n\n"));
7350 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7351 "\tpublishes printer settings in Active Directory\n"
7352 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7353 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7354 "\n\tmigrates printers from remote to local server\n\n"));
7355 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7356 "\n\tmigrates printer-settings from remote to local server\n\n"));
7357 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7358 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7359 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7360 "\n\tmigrates printer-forms from remote to local server\n\n"));
7361 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7362 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7363 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7364 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7365 "\tremote to local print-server\n\n"));
7366 net_common_methods_usage(c, argc, argv);
7367 net_common_flags_usage(c, argc, argv);
7369 "\t-v or --verbose\t\t\tgive verbose output\n"
7370 "\t --destination\t\tmigration target server (default: localhost)\n"));
7376 * 'net rpc printer' entrypoint.
7378 * @param c A net_context structure.
7379 * @param argc Standard main() style argc.
7380 * @param argv Standard main() style argv. Initial components are already
7383 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7385 struct functable func[] = {
7390 N_("List all printers on print server"),
7391 N_("net rpc printer list\n"
7392 " List all printers on print server")
7396 rpc_printer_migrate,
7398 N_("Migrate printer to local server"),
7399 N_("net rpc printer migrate\n"
7400 " Migrate printer to local server")
7404 rpc_printer_driver_list,
7406 N_("List printer drivers"),
7407 N_("net rpc printer driver\n"
7408 " List printer drivers")
7412 rpc_printer_publish,
7414 N_("Publish printer in AD"),
7415 N_("net rpc printer publish\n"
7416 " Publish printer in AD")
7418 {NULL, NULL, 0, NULL, NULL}
7422 if (c->display_usage) {
7423 d_printf(_("Usage:\n"));
7424 d_printf(_("net rpc printer\n"
7425 " List printers\n"));
7426 net_display_usage_from_functable(func);
7429 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7430 rpc_printer_list_internals,
7434 return net_run_function(c, argc, argv, "net rpc printer", func);
7438 * 'net rpc' entrypoint.
7440 * @param c A net_context structure.
7441 * @param argc Standard main() style argc.
7442 * @param argv Standard main() style argv. Initial components are already
7446 int net_rpc(struct net_context *c, int argc, const char **argv)
7448 NET_API_STATUS status;
7450 struct functable func[] = {
7455 N_("Modify global audit settings"),
7456 N_("net rpc audit\n"
7457 " Modify global audit settings")
7463 N_("Show basic info about a domain"),
7465 " Show basic info about a domain")
7471 N_("Join a domain"),
7479 N_("Join a domain created in server manager"),
7480 N_("net rpc oldjoin\n"
7481 " Join a domain created in server manager")
7487 N_("Test that a join is valid"),
7488 N_("net rpc testjoin\n"
7489 " Test that a join is valid")
7495 N_("List/modify users"),
7497 " List/modify users")
7503 N_("Change a user password"),
7504 N_("net rpc password\n"
7505 " Change a user password\n"
7506 " Alias for net rpc user password")
7512 N_("List/modify groups"),
7513 N_("net rpc group\n"
7514 " List/modify groups")
7520 N_("List/modify shares"),
7521 N_("net rpc share\n"
7522 " List/modify shares")
7528 N_("List open files"),
7536 N_("List/modify printers"),
7537 N_("net rpc printer\n"
7538 " List/modify printers")
7542 net_rpc_changetrustpw,
7544 N_("Change trust account password"),
7545 N_("net rpc changetrustpw\n"
7546 " Change trust account password")
7552 N_("Modify domain trusts"),
7553 N_("net rpc trustdom\n"
7554 " Modify domain trusts")
7560 N_("Abort a remote shutdown"),
7561 N_("net rpc abortshutdown\n"
7562 " Abort a remote shutdown")
7568 N_("Shutdown a remote server"),
7569 N_("net rpc shutdown\n"
7570 " Shutdown a remote server")
7576 N_("Dump SAM data of remote NT PDC"),
7577 N_("net rpc samdump\n"
7578 " Dump SAM data of remote NT PDC")
7584 N_("Sync a remote NT PDC's data into local passdb"),
7585 N_("net rpc vampire\n"
7586 " Sync a remote NT PDC's data into local passdb")
7592 N_("Fetch the domain sid into local secrets.tdb"),
7593 N_("net rpc getsid\n"
7594 " Fetch the domain sid into local secrets.tdb")
7600 N_("Manage privileges assigned to SID"),
7601 N_("net rpc rights\n"
7602 " Manage privileges assigned to SID")
7608 N_("Start/stop/query remote services"),
7609 N_("net rpc service\n"
7610 " Start/stop/query remote services")
7616 N_("Manage registry hives"),
7617 N_("net rpc registry\n"
7618 " Manage registry hives")
7624 N_("Open interactive shell on remote server"),
7625 N_("net rpc shell\n"
7626 " Open interactive shell on remote server")
7628 {NULL, NULL, 0, NULL, NULL}
7631 status = libnetapi_net_init(&c->netapi_ctx);
7635 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7636 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7637 if (c->opt_kerberos) {
7638 libnetapi_set_use_kerberos(c->netapi_ctx);
7640 if (c->opt_ccache) {
7641 libnetapi_set_use_ccache(c->netapi_ctx);
7644 return net_run_function(c, argc, argv, "net rpc", func);