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)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "../utils/net.h"
27 * @brief RPC based subcommands for the 'net' utility.
29 * This file should contain much of the functionality that used to
30 * be found in rpcclient, execpt that the commands should change
31 * less often, and the fucntionality should be sane (the user is not
32 * expected to know a rid/sid before they conduct an operation etc.)
34 * @todo Perhaps eventually these should be split out into a number
35 * of files, as this could get quite big.
39 /* A function of this type is passed to the 'run_rpc_command' wrapper */
40 typedef NTSTATUS (*rpc_command_fn)(const DOM_SID *, struct cli_state *, TALLOC_CTX *, int, const char **);
43 * Many of the RPC functions need the domain sid. This function gets
44 * it at the start of every run
46 * @param cli A cli_state already connected to the remote machine
48 * @return The Domain SID of the remote machine.
51 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli)
55 NTSTATUS result = NT_STATUS_OK;
56 uint32 info_class = 5;
60 if (!(domain_sid = malloc(sizeof(DOM_SID)))){
61 DEBUG(0,("net_get_remote_domain_sid: malloc returned NULL!\n"));
65 if (!(mem_ctx=talloc_init("net_get_remote_domain_sid")))
67 DEBUG(0,("net_get_remote_domain_sid: talloc_init returned NULL!\n"));
72 if (!cli_nt_session_open (cli, PI_LSARPC)) {
73 fprintf(stderr, "could not initialise lsa pipe\n");
77 result = cli_lsa_open_policy(cli, mem_ctx, True,
78 SEC_RIGHTS_MAXIMUM_ALLOWED,
80 if (!NT_STATUS_IS_OK(result)) {
84 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
85 domain_name, domain_sid);
86 if (!NT_STATUS_IS_OK(result)) {
90 cli_lsa_close(cli, mem_ctx, &pol);
91 cli_nt_session_close(cli);
92 talloc_destroy(mem_ctx);
97 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
99 if (!NT_STATUS_IS_OK(result)) {
100 fprintf(stderr, "error: %s\n", nt_errstr(result));
107 * Run a single RPC command, from start to finish.
109 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
110 * @param conn_flag a NET_FLAG_ combination. Passed to
111 * net_make_ipc_connection.
112 * @param argc Standard main() style argc
113 * @param argc Standard main() style argv. Initial components are already
115 * @return A shell status integer (0 for success)
118 static int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
120 int argc, const char **argv)
122 struct cli_state *cli = NULL;
127 /* make use of cli_state handed over as an argument, if possible */
129 cli = net_make_ipc_connection(conn_flags);
137 domain_sid = net_get_remote_domain_sid(cli);
141 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
142 DEBUG(0, ("talloc_init() failed\n"));
147 if (!cli_nt_session_open(cli, pipe_idx)) {
148 DEBUG(0, ("Could not initialise pipe\n"));
151 nt_status = fn(domain_sid, cli, mem_ctx, argc, argv);
153 if (!NT_STATUS_IS_OK(nt_status)) {
154 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
156 DEBUG(5, ("rpc command function succedded\n"));
160 if (cli->nt_pipe_fnum)
161 cli_nt_session_close(cli);
163 /* close the connection only if it was opened here */
167 talloc_destroy(mem_ctx);
169 return (!NT_STATUS_IS_OK(nt_status));
173 /****************************************************************************/
177 * Force a change of the trust acccount password.
179 * All parameters are provided by the run_rpc_command function, except for
180 * argc, argv which are passes through.
182 * @param domain_sid The domain sid aquired from the remote server
183 * @param cli A cli_state connected to the server.
184 * @param mem_ctx Talloc context, destoyed on compleation of the function.
185 * @param argc Standard main() style argc
186 * @param argc Standard main() style argv. Initial components are already
189 * @return Normal NTSTATUS return.
192 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
193 int argc, const char **argv) {
195 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
199 * Force a change of the trust acccount password.
201 * @param argc Standard main() style argc
202 * @param argc Standard main() style argv. Initial components are already
205 * @return A shell status integer (0 for success)
208 static int rpc_changetrustpw(int argc, const char **argv)
210 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_changetrustpw_internals,
215 /****************************************************************************/
219 * Join a domain, the old way.
221 * This uses 'machinename' as the inital password, and changes it.
223 * The password should be created with 'server manager' or eqiv first.
225 * All parameters are provided by the run_rpc_command function, except for
226 * argc, argv which are passes through.
228 * @param domain_sid The domain sid aquired from the remote server
229 * @param cli A cli_state connected to the server.
230 * @param mem_ctx Talloc context, destoyed on compleation of the function.
231 * @param argc Standard main() style argc
232 * @param argc Standard main() style argv. Initial components are already
235 * @return Normal NTSTATUS return.
238 static NTSTATUS rpc_join_oldstyle_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
239 int argc, const char **argv) {
241 fstring trust_passwd;
242 unsigned char orig_trust_passwd_hash[16];
245 fstrcpy(trust_passwd, global_myname());
246 strlower(trust_passwd);
249 * Machine names can be 15 characters, but the max length on
250 * a password is 14. --jerry
253 trust_passwd[14] = '\0';
255 E_md4hash(trust_passwd, orig_trust_passwd_hash);
257 result = trust_pw_change_and_store_it(cli, mem_ctx, orig_trust_passwd_hash);
259 if (NT_STATUS_IS_OK(result))
260 printf("Joined domain %s.\n",lp_workgroup());
266 * Join a domain, the old way.
268 * @param argc Standard main() style argc
269 * @param argc Standard main() style argv. Initial components are already
272 * @return A shell status integer (0 for success)
275 static int net_rpc_join_oldstyle(int argc, const char **argv)
277 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_join_oldstyle_internals,
282 * Basic usage function for 'net rpc join'
283 * @param argc Standard main() style argc
284 * @param argc Standard main() style argv. Initial components are already
288 static int rpc_join_usage(int argc, const char **argv)
290 d_printf("net rpc join -U <username>[%%password] [options]\n"\
291 "\t to join a domain with admin username & password\n"\
292 "\t\t password will be prompted if none is specified\n");
293 d_printf("net rpc join [options except -U]\n"\
294 "\t to join a domain created in server manager\n\n\n");
296 net_common_flags_usage(argc, argv);
301 * 'net rpc join' entrypoint.
302 * @param argc Standard main() style argc
303 * @param argc Standard main() style argv. Initial components are already
306 * Main 'net_rpc_join()' (where the admain username/password is used) is
308 * Assume if a -U is specified, it's the new style, otherwise it's the
309 * old style. If 'oldstyle' is specfied explicity, do it and don't prompt.
312 int net_rpc_join(int argc, const char **argv)
314 struct functable func[] = {
315 {"oldstyle", net_rpc_join_oldstyle},
320 if ((net_rpc_join_oldstyle(argc, argv) == 0))
323 return net_rpc_join_newstyle(argc, argv);
326 return net_run_function(argc, argv, func, rpc_join_usage);
332 * display info about a rpc domain
334 * All parameters are provided by the run_rpc_command function, except for
335 * argc, argv which are passes through.
337 * @param domain_sid The domain sid acquired from the remote server
338 * @param cli A cli_state connected to the server.
339 * @param mem_ctx Talloc context, destoyed on completion of the function.
340 * @param argc Standard main() style argc
341 * @param argv Standard main() style argv. Initial components are already
344 * @return Normal NTSTATUS return.
348 rpc_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
349 TALLOC_CTX *mem_ctx, int argc, const char **argv)
351 POLICY_HND connect_pol, domain_pol;
352 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
356 sid_to_string(sid_str, domain_sid);
358 /* Get sam policy handle */
359 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
361 if (!NT_STATUS_IS_OK(result)) {
365 /* Get domain policy handle */
366 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
367 MAXIMUM_ALLOWED_ACCESS,
368 domain_sid, &domain_pol);
369 if (!NT_STATUS_IS_OK(result)) {
374 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
376 if (NT_STATUS_IS_OK(result)) {
377 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
378 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
379 d_printf("Domain SID: %s\n", sid_str);
380 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
381 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
382 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
383 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
393 * 'net rpc info' entrypoint.
394 * @param argc Standard main() style argc
395 * @param argc Standard main() style argv. Initial components are already
398 int net_rpc_info(int argc, const char **argv)
400 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
407 * Fetch domain SID into the local secrets.tdb
409 * All parameters are provided by the run_rpc_command function, except for
410 * argc, argv which are passes through.
412 * @param domain_sid The domain sid acquired from the remote server
413 * @param cli A cli_state connected to the server.
414 * @param mem_ctx Talloc context, destoyed on completion of the function.
415 * @param argc Standard main() style argc
416 * @param argv Standard main() style argv. Initial components are already
419 * @return Normal NTSTATUS return.
423 rpc_getsid_internals(const DOM_SID *domain_sid, struct cli_state *cli,
424 TALLOC_CTX *mem_ctx, int argc, const char **argv)
428 sid_to_string(sid_str, domain_sid);
429 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
430 sid_str, lp_workgroup());
432 if (!secrets_store_domain_sid(global_myname(), domain_sid)) {
433 DEBUG(0,("Can't store domain SID\n"));
434 return NT_STATUS_UNSUCCESSFUL;
442 * 'net rpc getsid' entrypoint.
443 * @param argc Standard main() style argc
444 * @param argc Standard main() style argv. Initial components are already
447 int net_rpc_getsid(int argc, const char **argv)
449 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
450 rpc_getsid_internals,
455 /****************************************************************************/
458 * Basic usage function for 'net rpc user'
459 * @param argc Standard main() style argc.
460 * @param argv Standard main() style argv. Initial components are already
464 static int rpc_user_usage(int argc, const char **argv)
466 return net_help_user(argc, argv);
470 * Add a new user to a remote RPC server
472 * All parameters are provided by the run_rpc_command function, except for
473 * argc, argv which are passes through.
475 * @param domain_sid The domain sid acquired from the remote server
476 * @param cli A cli_state connected to the server.
477 * @param mem_ctx Talloc context, destoyed on completion of the function.
478 * @param argc Standard main() style argc
479 * @param argv Standard main() style argv. Initial components are already
482 * @return Normal NTSTATUS return.
485 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
486 int argc, const char **argv) {
488 POLICY_HND connect_pol, domain_pol, user_pol;
489 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
490 const char *acct_name;
492 uint32 unknown, user_rid;
495 d_printf("User must be specified\n");
496 rpc_user_usage(argc, argv);
502 /* Get sam policy handle */
504 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
506 if (!NT_STATUS_IS_OK(result)) {
510 /* Get domain policy handle */
512 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
513 MAXIMUM_ALLOWED_ACCESS,
514 domain_sid, &domain_pol);
515 if (!NT_STATUS_IS_OK(result)) {
519 /* Create domain user */
521 acb_info = ACB_NORMAL;
522 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
524 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
525 acct_name, acb_info, unknown,
526 &user_pol, &user_rid);
527 if (!NT_STATUS_IS_OK(result)) {
532 if (!NT_STATUS_IS_OK(result)) {
533 d_printf("Failed to add user %s - %s\n", acct_name,
536 d_printf("Added user %s\n", acct_name);
542 * Add a new user to a remote RPC server
544 * @param argc Standard main() style argc
545 * @param argv Standard main() style argv. Initial components are already
548 * @return A shell status integer (0 for success)
551 static int rpc_user_add(int argc, const char **argv)
553 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
558 * Delete a user from a remote RPC server
560 * All parameters are provided by the run_rpc_command function, except for
561 * argc, argv which are passes through.
563 * @param domain_sid The domain sid acquired from the remote server
564 * @param cli A cli_state connected to the server.
565 * @param mem_ctx Talloc context, destoyed on completion of the function.
566 * @param argc Standard main() style argc
567 * @param argv Standard main() style argv. Initial components are already
570 * @return Normal NTSTATUS return.
573 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
574 struct cli_state *cli,
576 int argc, const char **argv)
578 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
579 POLICY_HND connect_pol, domain_pol, user_pol;
582 d_printf("User must be specified\n");
583 rpc_user_usage(argc, argv);
586 /* Get sam policy and domain handles */
588 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
591 if (!NT_STATUS_IS_OK(result)) {
595 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
596 MAXIMUM_ALLOWED_ACCESS,
597 domain_sid, &domain_pol);
599 if (!NT_STATUS_IS_OK(result)) {
603 /* Get handle on user */
606 uint32 *user_rids, num_rids, *name_types;
607 uint32 flags = 0x000003e8; /* Unknown */
609 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
611 &num_rids, &user_rids,
614 if (!NT_STATUS_IS_OK(result)) {
618 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
619 MAXIMUM_ALLOWED_ACCESS,
620 user_rids[0], &user_pol);
622 if (!NT_STATUS_IS_OK(result)) {
629 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
631 if (!NT_STATUS_IS_OK(result)) {
635 /* Display results */
643 * Delete a user from a remote RPC server
645 * @param argc Standard main() style argc
646 * @param argv Standard main() style argv. Initial components are already
649 * @return A shell status integer (0 for success)
652 static int rpc_user_delete(int argc, const char **argv)
654 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
659 * List user's groups on a remote RPC server
661 * All parameters are provided by the run_rpc_command function, except for
662 * argc, argv which are passes through.
664 * @param domain_sid The domain sid acquired from the remote server
665 * @param cli A cli_state connected to the server.
666 * @param mem_ctx Talloc context, destoyed on completion of the function.
667 * @param argc Standard main() style argc
668 * @param argv Standard main() style argv. Initial components are already
671 * @return Normal NTSTATUS return.
675 rpc_user_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
676 TALLOC_CTX *mem_ctx, int argc, const char **argv)
678 POLICY_HND connect_pol, domain_pol, user_pol;
679 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
680 uint32 *rids, num_rids, *name_types, num_names;
681 uint32 flags = 0x000003e8; /* Unknown */
687 d_printf("User must be specified\n");
688 rpc_user_usage(argc, argv);
691 /* Get sam policy handle */
693 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
695 if (!NT_STATUS_IS_OK(result)) goto done;
697 /* Get domain policy handle */
699 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
700 MAXIMUM_ALLOWED_ACCESS,
701 domain_sid, &domain_pol);
702 if (!NT_STATUS_IS_OK(result)) goto done;
704 /* Get handle on user */
706 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
708 &num_rids, &rids, &name_types);
710 if (!NT_STATUS_IS_OK(result)) goto done;
712 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
713 MAXIMUM_ALLOWED_ACCESS,
715 if (!NT_STATUS_IS_OK(result)) goto done;
717 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
718 &num_rids, &user_gids);
722 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
724 for (i = 0; i < num_rids; i++)
725 rids[i] = user_gids[i].g_rid;
727 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
728 flags, num_rids, rids,
729 &num_names, &names, &name_types);
731 if (!NT_STATUS_IS_OK(result)) {
735 /* Display results */
737 for (i = 0; i < num_names; i++)
738 printf("%s\n", names[i]);
745 * List a user's groups from a remote RPC server
747 * @param argc Standard main() style argc
748 * @param argv Standard main() style argv. Initial components are already
751 * @return A shell status integer (0 for success)
754 static int rpc_user_info(int argc, const char **argv)
756 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
761 * List users on a remote RPC server
763 * All parameters are provided by the run_rpc_command function, except for
764 * argc, argv which are passes through.
766 * @param domain_sid The domain sid acquired from the remote server
767 * @param cli A cli_state connected to the server.
768 * @param mem_ctx Talloc context, destoyed on completion of the function.
769 * @param argc Standard main() style argc
770 * @param argv Standard main() style argv. Initial components are already
773 * @return Normal NTSTATUS return.
777 rpc_user_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
778 TALLOC_CTX *mem_ctx, int argc, const char **argv)
780 POLICY_HND connect_pol, domain_pol;
781 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
782 uint32 start_idx=0, max_entries=250, num_entries, i;
783 SAM_DISPINFO_CTR ctr;
784 SAM_DISPINFO_1 info1;
786 /* Get sam policy handle */
788 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
790 if (!NT_STATUS_IS_OK(result)) {
794 /* Get domain policy handle */
796 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
797 MAXIMUM_ALLOWED_ACCESS,
798 domain_sid, &domain_pol);
799 if (!NT_STATUS_IS_OK(result)) {
803 /* Query domain users */
806 ctr.sam.info1 = &info1;
807 if (opt_long_list_entries)
808 d_printf("\nUser name Comment"\
809 "\n-----------------------------\n");
812 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
813 &start_idx, 1, &num_entries,
815 for (i = 0; i < num_entries; i++) {
816 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
817 if (opt_long_list_entries)
818 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
820 if (opt_long_list_entries)
821 printf("%-21.21s %-50.50s\n", user, desc);
823 printf("%s\n", user);
825 } while (!NT_STATUS_IS_OK(result));
832 * 'net rpc user' entrypoint.
833 * @param argc Standard main() style argc
834 * @param argc Standard main() style argv. Initial components are already
838 int net_rpc_user(int argc, const char **argv)
840 struct functable func[] = {
841 {"add", rpc_user_add},
842 {"info", rpc_user_info},
843 {"delete", rpc_user_delete},
848 if (opt_long_list_entries) {
851 return run_rpc_command(NULL,PI_SAMR, 0,
852 rpc_user_list_internals,
856 return net_run_function(argc, argv, func, rpc_user_usage);
860 /****************************************************************************/
863 * Basic usage function for 'net rpc group'
864 * @param argc Standard main() style argc.
865 * @param argv Standard main() style argv. Initial components are already
869 static int rpc_group_usage(int argc, const char **argv)
871 return net_help_group(argc, argv);
875 * List groups on a remote RPC server
877 * All parameters are provided by the run_rpc_command function, except for
878 * argc, argv which are passes through.
880 * @param domain_sid The domain sid acquired from the remote server
881 * @param cli A cli_state connected to the server.
882 * @param mem_ctx Talloc context, destoyed on completion of the function.
883 * @param argc Standard main() style argc
884 * @param argv Standard main() style argv. Initial components are already
887 * @return Normal NTSTATUS return.
891 rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
892 TALLOC_CTX *mem_ctx, int argc, const char **argv)
894 POLICY_HND connect_pol, domain_pol;
895 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
896 uint32 start_idx=0, max_entries=250, num_entries, i;
897 struct acct_info *groups;
898 DOM_SID global_sid_Builtin;
900 string_to_sid(&global_sid_Builtin, "S-1-5-32");
902 /* Get sam policy handle */
904 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
906 if (!NT_STATUS_IS_OK(result)) {
910 /* Get domain policy handle */
912 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
913 MAXIMUM_ALLOWED_ACCESS,
914 domain_sid, &domain_pol);
915 if (!NT_STATUS_IS_OK(result)) {
919 /* Query domain groups */
920 if (opt_long_list_entries)
921 d_printf("\nGroup name Comment"\
922 "\n-----------------------------\n");
924 result = cli_samr_enum_dom_groups(cli, mem_ctx, &domain_pol,
925 &start_idx, max_entries,
926 &groups, &num_entries);
928 for (i = 0; i < num_entries; i++) {
929 if (opt_long_list_entries)
930 printf("%-21.21s %-50.50s\n",
932 groups[i].acct_desc);
934 printf("%-21.21s\n", groups[i].acct_name);
936 } while (!NT_STATUS_IS_OK(result));
937 /* query domain aliases */
939 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
940 &start_idx, max_entries,
941 &groups, &num_entries);
943 for (i = 0; i < num_entries; i++) {
944 if (opt_long_list_entries)
945 printf("%-21.21s %-50.50s\n",
947 groups[i].acct_desc);
949 printf("%-21.21s\n", groups[i].acct_name);
951 } while (!NT_STATUS_IS_OK(result));
952 cli_samr_close(cli, mem_ctx, &domain_pol);
953 /* Get builtin policy handle */
955 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
956 MAXIMUM_ALLOWED_ACCESS,
957 &global_sid_Builtin, &domain_pol);
958 if (!NT_STATUS_IS_OK(result)) {
961 /* query builtin aliases */
963 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
964 &start_idx, max_entries,
965 &groups, &num_entries);
967 for (i = 0; i < num_entries; i++) {
968 if (opt_long_list_entries)
969 printf("%-21.21s %-50.50s\n",
971 groups[i].acct_desc);
973 printf("%s\n", groups[i].acct_name);
975 } while (!NT_STATUS_IS_OK(result));
982 * 'net rpc group' entrypoint.
983 * @param argc Standard main() style argc
984 * @param argc Standard main() style argv. Initial components are already
988 int net_rpc_group(int argc, const char **argv)
990 struct functable func[] = {
992 {"add", rpc_group_add},
993 {"delete", rpc_group_delete},
999 if (opt_long_list_entries) {
1002 return run_rpc_command(NULL, PI_SAMR, 0,
1003 rpc_group_list_internals,
1007 return net_run_function(argc, argv, func, rpc_group_usage);
1010 /****************************************************************************/
1012 static int rpc_share_usage(int argc, const char **argv)
1014 return net_help_share(argc, argv);
1018 * Add a share on a remote RPC server
1020 * All parameters are provided by the run_rpc_command function, except for
1021 * argc, argv which are passes through.
1023 * @param domain_sid The domain sid acquired from the remote server
1024 * @param cli A cli_state connected to the server.
1025 * @param mem_ctx Talloc context, destoyed on completion of the function.
1026 * @param argc Standard main() style argc
1027 * @param argv Standard main() style argv. Initial components are already
1030 * @return Normal NTSTATUS return.
1033 rpc_share_add_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1034 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1037 char *sharename=talloc_strdup(mem_ctx, argv[0]);
1039 uint32 type=0; /* only allow disk shares to be added */
1040 uint32 num_users=0, perms=0;
1041 char *password=NULL; /* don't allow a share password */
1043 path = strchr(sharename, '=');
1045 return NT_STATUS_UNSUCCESSFUL;
1048 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1049 opt_comment, perms, opt_maxusers,
1050 num_users, path, password);
1051 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1054 static int rpc_share_add(int argc, const char **argv)
1056 if ((argc < 1) || !strchr(argv[0], '=')) {
1057 DEBUG(1,("Sharename or path not specified on add\n"));
1058 return rpc_share_usage(argc, argv);
1060 return run_rpc_command(NULL, PI_SRVSVC, 0,
1061 rpc_share_add_internals,
1066 * Delete a share on a remote RPC server
1068 * All parameters are provided by the run_rpc_command function, except for
1069 * argc, argv which are passes through.
1071 * @param domain_sid The domain sid acquired from the remote server
1072 * @param cli A cli_state connected to the server.
1073 * @param mem_ctx Talloc context, destoyed on completion of the function.
1074 * @param argc Standard main() style argc
1075 * @param argv Standard main() style argv. Initial components are already
1078 * @return Normal NTSTATUS return.
1081 rpc_share_del_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1082 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1086 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1087 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1091 * Delete a share on a remote RPC server
1093 * @param domain_sid The domain sid acquired from the remote server
1094 * @param argc Standard main() style argc
1095 * @param argv Standard main() style argv. Initial components are already
1098 * @return A shell status integer (0 for success)
1100 static int rpc_share_delete(int argc, const char **argv)
1103 DEBUG(1,("Sharename not specified on delete\n"));
1104 return rpc_share_usage(argc, argv);
1106 return run_rpc_command(NULL, PI_SRVSVC, 0,
1107 rpc_share_del_internals,
1112 * Formatted print of share info
1114 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1117 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1119 fstring netname = "", remark = "";
1121 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1122 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1124 if (opt_long_list_entries) {
1125 d_printf("%-12.12s %-8.8s %-50.50s\n",
1126 netname, share_type[info1->info_1.type], remark);
1128 d_printf("%-12.12s\n", netname);
1134 * List shares on a remote RPC server
1136 * All parameters are provided by the run_rpc_command function, except for
1137 * argc, argv which are passes through.
1139 * @param domain_sid The domain sid acquired from the remote server
1140 * @param cli A cli_state connected to the server.
1141 * @param mem_ctx Talloc context, destoyed on completion of the function.
1142 * @param argc Standard main() style argc
1143 * @param argv Standard main() style argv. Initial components are already
1146 * @return Normal NTSTATUS return.
1150 rpc_share_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1151 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1153 SRV_SHARE_INFO_CTR ctr;
1156 uint32 preferred_len = 0xffffffff, i;
1158 init_enum_hnd(&hnd, 0);
1160 result = cli_srvsvc_net_share_enum(
1161 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1163 if (!W_ERROR_IS_OK(result))
1166 /* Display results */
1168 if (opt_long_list_entries) {
1170 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1171 "\nShare name Type Description\n"\
1172 "---------- ---- -----------\n");
1174 for (i = 0; i < ctr.num_entries; i++)
1175 display_share_info_1(&ctr.share.info1[i]);
1177 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1181 * 'net rpc share' entrypoint.
1182 * @param argc Standard main() style argc
1183 * @param argv Standard main() style argv. Initial components are already
1187 int net_rpc_share(int argc, const char **argv)
1189 struct functable func[] = {
1190 {"add", rpc_share_add},
1191 {"delete", rpc_share_delete},
1196 return run_rpc_command(NULL, PI_SRVSVC, 0,
1197 rpc_share_list_internals,
1200 return net_run_function(argc, argv, func, rpc_share_usage);
1203 /****************************************************************************/
1205 static int rpc_file_usage(int argc, const char **argv)
1207 return net_help_file(argc, argv);
1211 * Close a file on a remote RPC server
1213 * All parameters are provided by the run_rpc_command function, except for
1214 * argc, argv which are passes through.
1216 * @param domain_sid The domain sid acquired from the remote server
1217 * @param cli A cli_state connected to the server.
1218 * @param mem_ctx Talloc context, destoyed on completion of the function.
1219 * @param argc Standard main() style argc
1220 * @param argv Standard main() style argv. Initial components are already
1223 * @return Normal NTSTATUS return.
1226 rpc_file_close_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1227 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1230 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1231 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1235 * Close a file on a remote RPC server
1237 * @param argc Standard main() style argc
1238 * @param argv Standard main() style argv. Initial components are already
1241 * @return A shell status integer (0 for success)
1243 static int rpc_file_close(int argc, const char **argv)
1246 DEBUG(1, ("No fileid given on close\n"));
1247 return(rpc_file_usage(argc, argv));
1250 return run_rpc_command(NULL, PI_SRVSVC, 0,
1251 rpc_file_close_internals,
1256 * Formatted print of open file info
1258 * @param info3 FILE_INFO_3 contents
1259 * @param str3 strings for FILE_INFO_3
1262 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1264 fstring user = "", path = "";
1266 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1267 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1269 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1270 info3->id, user, info3->perms, info3->num_locks, path);
1274 * List open files on a remote RPC server
1276 * All parameters are provided by the run_rpc_command function, except for
1277 * argc, argv which are passes through.
1279 * @param domain_sid The domain sid acquired from the remote server
1280 * @param cli A cli_state connected to the server.
1281 * @param mem_ctx Talloc context, destoyed on completion of the function.
1282 * @param argc Standard main() style argc
1283 * @param argv Standard main() style argv. Initial components are already
1286 * @return Normal NTSTATUS return.
1290 rpc_file_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1291 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1293 SRV_FILE_INFO_CTR ctr;
1296 uint32 preferred_len = 0xffffffff, i;
1297 const char *username=NULL;
1299 init_enum_hnd(&hnd, 0);
1301 /* if argc > 0, must be user command */
1303 username = smb_xstrdup(argv[0]);
1305 result = cli_srvsvc_net_file_enum(
1306 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1308 if (!W_ERROR_IS_OK(result))
1311 /* Display results */
1314 "\nEnumerating open files on remote server:\n\n"\
1315 "\nFileId Opened by Perms Locks Path"\
1316 "\n------ --------- ----- ----- ---- \n");
1317 for (i = 0; i < ctr.num_entries; i++)
1318 display_file_info_3(&ctr.file.info3[i].info_3,
1319 &ctr.file.info3[i].info_3_str);
1321 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1326 * List files for a user on a remote RPC server
1328 * @param argc Standard main() style argc
1329 * @param argv Standard main() style argv. Initial components are already
1332 * @return A shell status integer (0 for success)
1334 static int rpc_file_user(int argc, const char **argv)
1337 DEBUG(1, ("No username given\n"));
1338 return(rpc_file_usage(argc, argv));
1341 return run_rpc_command(NULL, PI_SRVSVC, 0,
1342 rpc_file_list_internals,
1348 * 'net rpc file' entrypoint.
1349 * @param argc Standard main() style argc
1350 * @param argv Standard main() style argv. Initial components are already
1354 int net_rpc_file(int argc, const char **argv)
1356 struct functable func[] = {
1357 {"close", rpc_file_close},
1358 {"user", rpc_file_user},
1360 {"info", rpc_file_info},
1366 return run_rpc_command(NULL, PI_SRVSVC, 0,
1367 rpc_file_list_internals,
1370 return net_run_function(argc, argv, func, rpc_file_usage);
1373 /****************************************************************************/
1378 * ABORT the shutdown of a remote RPC Server
1380 * All parameters are provided by the run_rpc_command function, except for
1381 * argc, argv which are passed through.
1383 * @param domain_sid The domain sid aquired from the remote server
1384 * @param cli A cli_state connected to the server.
1385 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1386 * @param argc Standard main() style argc
1387 * @param argv Standard main() style argv. Initial components are already
1390 * @return Normal NTSTATUS return.
1393 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1394 int argc, const char **argv)
1396 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1398 result = cli_reg_abort_shutdown(cli, mem_ctx);
1400 if (NT_STATUS_IS_OK(result))
1401 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1403 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1410 * ABORT the Shut down of a remote RPC server
1412 * @param argc Standard main() style argc
1413 * @param argv Standard main() style argv. Initial components are already
1416 * @return A shell status integer (0 for success)
1419 static int rpc_shutdown_abort(int argc, const char **argv)
1421 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_abort_internals,
1426 * Shut down a remote RPC Server
1428 * All parameters are provided by the run_rpc_command function, except for
1429 * argc, argv which are passes through.
1431 * @param domain_sid The domain sid aquired from the remote server
1432 * @param cli A cli_state connected to the server.
1433 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1434 * @param argc Standard main() style argc
1435 * @param argc Standard main() style argv. Initial components are already
1438 * @return Normal NTSTATUS return.
1441 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1442 int argc, const char **argv)
1444 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1445 const char *msg = "This machine will be shutdown shortly";
1446 uint32 timeout = 20;
1448 BOOL reboot = opt_reboot;
1449 BOOL force = opt_force;
1454 struct poptOption long_options[] = {
1455 {"message", 'm', POPT_ARG_STRING, &msg},
1456 {"timeout", 't', POPT_ARG_INT, &timeout},
1457 {"reboot", 'r', POPT_ARG_NONE, &reboot},
1458 {"force", 'f', POPT_ARG_NONE, &force},
1462 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1463 POPT_CONTEXT_KEEP_FIRST);
1465 rc = poptGetNextOpt(pc);
1468 /* an error occurred during option processing */
1469 DEBUG(0, ("%s: %s\n",
1470 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1472 return NT_STATUS_INVALID_PARAMETER;
1476 flgs |= REG_REBOOT_ON_SHUTDOWN;
1479 flgs |= REG_FORCE_SHUTDOWN;
1485 timeout = opt_timeout;
1488 /* create an entry */
1489 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, flgs);
1491 if (NT_STATUS_IS_OK(result))
1492 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1494 DEBUG(0,("Shutdown of remote machine failed!\n"));
1500 * Shut down a remote RPC server
1502 * @param argc Standard main() style argc
1503 * @param argc Standard main() style argv. Initial components are already
1506 * @return A shell status integer (0 for success)
1509 static int rpc_shutdown(int argc, const char **argv)
1511 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
1515 /***************************************************************************
1516 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1518 ***************************************************************************/
1521 * Add interdomain trust account to the RPC server.
1522 * All parameters (except for argc and argv) are passed by run_rpc_command
1525 * @param domain_sid The domain sid acquired from the server
1526 * @param cli A cli_state connected to the server.
1527 * @param mem_ctx Talloc context, destoyed on completion of the function.
1528 * @param argc Standard main() style argc
1529 * @param argc Standard main() style argv. Initial components are already
1532 * @return normal NTSTATUS return code
1535 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1536 int argc, const char **argv) {
1538 POLICY_HND connect_pol, domain_pol, user_pol;
1539 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1542 uint32 unknown, user_rid;
1545 d_printf("Usage: net rpc trustdom add <domain_name>\n");
1546 return NT_STATUS_INVALID_PARAMETER;
1550 * Make valid trusting domain account (ie. uppercased and with '$' appended)
1553 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1554 return NT_STATUS_NO_MEMORY;
1557 strupper(acct_name);
1559 /* Get samr policy handle */
1560 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1562 if (!NT_STATUS_IS_OK(result)) {
1566 /* Get domain policy handle */
1567 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1568 MAXIMUM_ALLOWED_ACCESS,
1569 domain_sid, &domain_pol);
1570 if (!NT_STATUS_IS_OK(result)) {
1574 /* Create trusting domain's account */
1575 acb_info = ACB_DOMTRUST;
1576 unknown = 0xe005000b; /* No idea what this is - a permission mask?
1577 mimir: yes, most probably it is */
1579 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1580 acct_name, acb_info, unknown,
1581 &user_pol, &user_rid);
1582 if (!NT_STATUS_IS_OK(result)) {
1587 SAFE_FREE(acct_name);
1592 * Create interdomain trust account for a remote domain.
1594 * @param argc standard argc
1595 * @param argv standard argv without initial components
1597 * @return Integer status (0 means success)
1600 static int rpc_trustdom_add(int argc, const char **argv)
1602 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
1608 * Delete interdomain trust account for a remote domain.
1610 * @param argc standard argc
1611 * @param argv standard argv without initial components
1613 * @return Integer status (0 means success)
1616 static int rpc_trustdom_del(int argc, const char **argv)
1618 d_printf("Sorry, not yet implemented.\n");
1624 * Establish trust relationship to a trusting domain.
1625 * Interdomain account must already be created on remote PDC.
1627 * @param argc standard argc
1628 * @param argv standard argv without initial components
1630 * @return Integer status (0 means success)
1633 static int rpc_trustdom_establish(int argc, const char **argv)
1635 struct cli_state *cli;
1636 struct in_addr server_ip;
1637 POLICY_HND connect_hnd;
1638 TALLOC_CTX *mem_ctx;
1641 WKS_INFO_100 wks_info;
1648 * Connect to \\server\ipc$ as 'our domain' account with password
1652 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
1656 domain_name = smb_xstrdup(argv[0]);
1657 strupper(domain_name);
1659 /* account name used at first is our domain's name with '$' */
1660 asprintf(&acct_name, "%s$", lp_workgroup());
1661 strupper(acct_name);
1664 * opt_workgroup will be used by connection functions further,
1665 * hence it should be set to remote domain name instead of ours
1667 if (opt_workgroup) {
1668 opt_workgroup = smb_xstrdup(domain_name);
1671 opt_user_name = acct_name;
1673 /* find the domain controller */
1674 if (!net_find_dc(&server_ip, pdc_name, domain_name)) {
1675 DEBUG(0, ("Coulnd find domain controller for domain %s\n", domain_name));
1679 /* connect to ipc$ as username/password */
1680 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
1681 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1683 /* Is it trusting domain account for sure ? */
1684 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
1685 nt_errstr(nt_status)));
1690 * Connect to \\server\ipc$ again (this time anonymously)
1693 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
1695 if (NT_STATUS_IS_ERR(nt_status)) {
1696 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
1697 domain_name, nt_errstr(nt_status)));
1701 * Use NetServerEnum2 to make sure we're talking to a proper server
1704 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
1705 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
1706 for domain %s\n", domain_name));
1710 * Call WksQueryInfo to check remote server's capabilities
1711 * note: It is now used only to get unicode domain name
1714 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
1715 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
1719 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
1721 DEBUG(0, ("talloc_init() failed\n"));
1726 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
1728 if (NT_STATUS_IS_ERR(nt_status)) {
1729 DEBUG(0, ("WksQueryInfo call failed.\n"));
1733 if (cli->nt_pipe_fnum)
1734 cli_nt_session_close(cli);
1738 * Call LsaOpenPolicy and LsaQueryInfo
1741 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
1742 DEBUG(0, ("talloc_init() failed\n"));
1747 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1748 DEBUG(0, ("Could not initialise lsa pipe\n"));
1753 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1755 if (NT_STATUS_IS_ERR(nt_status)) {
1756 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1757 nt_errstr(nt_status)));
1761 /* Querying info level 5 */
1763 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1764 5 /* info level */, domain_name,
1766 if (NT_STATUS_IS_ERR(nt_status)) {
1767 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1768 nt_errstr(nt_status)));
1775 /* There should be actually query info level 3 (following nt serv behaviour),
1776 but I still don't know if it's _really_ necessary */
1779 * Store the password in secrets db
1782 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
1783 wks_info.uni_lan_grp.uni_str_len, opt_password,
1785 DEBUG(0, ("Storing password for trusted domain failed.\n"));
1790 * Close the pipes and clean up
1793 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1794 if (NT_STATUS_IS_ERR(nt_status)) {
1795 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
1796 nt_errstr(nt_status)));
1800 if (cli->nt_pipe_fnum)
1801 cli_nt_session_close(cli);
1803 talloc_destroy(mem_ctx);
1805 DEBUG(0, ("Success!\n"));
1810 * Revoke trust relationship to the remote domain
1812 * @param argc standard argc
1813 * @param argv standard argv without initial components
1815 * @return Integer status (0 means success)
1818 static int rpc_trustdom_revoke(int argc, const char **argv)
1822 if (argc < 1) return -1;
1824 /* generate upper cased domain name */
1825 domain_name = smb_xstrdup(argv[0]);
1826 strupper(domain_name);
1828 /* delete password of the trust */
1829 if (!trusted_domain_password_delete(domain_name)) {
1830 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
1839 * Usage for 'net rpc trustdom' command
1841 * @param argc standard argc
1842 * @param argv standard argv without inital components
1844 * @return Integer status returned to shell
1847 static int rpc_trustdom_usage(int argc, const char **argv)
1849 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
1850 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
1851 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
1852 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
1853 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
1858 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1859 int argc, const char **argv)
1862 sid_to_string(str_sid, domain_sid);
1863 d_printf("%s\n", str_sid);
1864 return NT_STATUS_OK;
1868 static int rpc_trustdom_list(int argc, const char **argv)
1870 /* common variables */
1871 TALLOC_CTX* mem_ctx;
1872 struct cli_state *cli, *remote_cli;
1874 const char *domain_name = NULL;
1875 DOM_SID queried_dom_sid;
1876 fstring ascii_sid, padding;
1877 int ascii_dom_name_len;
1878 POLICY_HND connect_hnd;
1880 /* trusted domains listing variables */
1882 int num_domains, i, pad_len, col_len = 20;
1883 DOM_SID *domain_sids;
1884 char **trusted_dom_names;
1887 /* trusting domains listing variables */
1888 POLICY_HND domain_hnd;
1889 char **trusting_dom_names;
1890 uint32 *trusting_dom_rids;
1893 * Listing trusted domains (stored in secrets.tdb, if local)
1896 mem_ctx = talloc_init("trust relationships listing");
1899 * set domain and pdc name to local samba server (default)
1900 * or to remote one given in command line
1903 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
1904 domain_name = opt_workgroup;
1905 opt_target_workgroup = opt_workgroup;
1907 fstrcpy(pdc_name, global_myname());
1908 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
1909 opt_target_workgroup = domain_name;
1912 /* open \PIPE\lsarpc and open policy handle */
1913 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
1914 DEBUG(0, ("Couldn't connect to domain controller\n"));
1918 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1919 DEBUG(0, ("Could not initialise lsa pipe\n"));
1923 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1925 if (NT_STATUS_IS_ERR(nt_status)) {
1926 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1927 nt_errstr(nt_status)));
1931 /* query info level 5 to obtain sid of a domain being queried */
1932 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1933 5 /* info level */, domain_name, &queried_dom_sid);
1934 if (NT_STATUS_IS_ERR(nt_status)) {
1935 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1936 nt_errstr(nt_status)));
1941 * Keep calling LsaEnumTrustdom over opened pipe until
1942 * the end of enumeration is reached
1945 d_printf("Trusted domains list:\n\n");
1948 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
1950 &trusted_dom_names, &domain_sids);
1952 if (NT_STATUS_IS_ERR(nt_status)) {
1953 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
1954 nt_errstr(nt_status)));
1958 for (i = 0; i < num_domains; i++) {
1959 /* convert sid into ascii string */
1960 sid_to_string(ascii_sid, &(domain_sids[i]));
1962 /* calculate padding space for d_printf to look nicer */
1963 pad_len = col_len - strlen(trusted_dom_names[i]);
1964 padding[pad_len] = 0;
1965 do padding[--pad_len] = ' '; while (pad_len);
1967 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
1971 * in case of no trusted domains say something rather
1972 * than just display blank line
1974 if (!num_domains) d_printf("none\n");
1976 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
1978 /* close this connection before doing next one */
1979 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1980 if (NT_STATUS_IS_ERR(nt_status)) {
1981 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
1982 nt_errstr(nt_status)));
1986 cli_nt_session_close(cli);
1989 * Listing trusting domains (stored in passdb backend, if local)
1992 d_printf("\nTrusting domains list:\n\n");
1995 * Open \PIPE\samr and get needed policy handles
1997 if (!cli_nt_session_open(cli, PI_SAMR)) {
1998 DEBUG(0, ("Could not initialise samr pipe\n"));
2003 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2005 if (!NT_STATUS_IS_OK(nt_status)) {
2006 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2007 nt_errstr(nt_status)));
2011 /* SamrOpenDomain - we have to open domain policy handle in order to be
2012 able to enumerate accounts*/
2013 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2014 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2015 &queried_dom_sid, &domain_hnd);
2016 if (!NT_STATUS_IS_OK(nt_status)) {
2017 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2018 nt_errstr(nt_status)));
2023 * perform actual enumeration
2026 enum_ctx = 0; /* reset enumeration context from last enumeration */
2029 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2030 &enum_ctx, ACB_DOMTRUST, 0xffff,
2031 &trusting_dom_names, &trusting_dom_rids,
2033 if (NT_STATUS_IS_ERR(nt_status)) {
2034 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2035 nt_errstr(nt_status)));
2039 for (i = 0; i < num_domains; i++) {
2042 * get each single domain's sid (do we _really_ need this ?):
2043 * 1) connect to domain's pdc
2044 * 2) query the pdc for domain's sid
2047 /* get rid of '$' tail */
2048 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2049 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2050 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2052 /* calculate padding space for d_printf to look nicer */
2053 pad_len = col_len - strlen(trusting_dom_names[i]);
2054 padding[pad_len] = 0;
2055 do padding[--pad_len] = ' '; while (pad_len);
2057 /* set opt_* variables to remote domain */
2058 strupper(trusting_dom_names[i]);
2059 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2060 opt_target_workgroup = opt_workgroup;
2062 d_printf("%s%s", trusting_dom_names[i], padding);
2064 /* connect to remote domain controller */
2065 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2067 /* query for domain's sid */
2068 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2069 d_printf("couldn't get domain's sid\n");
2071 cli_shutdown(remote_cli);
2074 d_printf("domain controller is not responding\n");
2078 if (!num_domains) d_printf("none\n");
2080 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2082 /* close opened samr and domain policy handles */
2083 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2084 if (!NT_STATUS_IS_OK(nt_status)) {
2085 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2088 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2089 if (!NT_STATUS_IS_OK(nt_status)) {
2090 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2093 /* close samr pipe and connection to IPC$ */
2094 cli_nt_session_close(cli);
2097 talloc_destroy(mem_ctx);
2102 * Entrypoint for 'net rpc trustdom' code
2104 * @param argc standard argc
2105 * @param argv standard argv without initial components
2107 * @return Integer status (0 means success)
2110 static int rpc_trustdom(int argc, const char **argv)
2112 struct functable func[] = {
2113 {"add", rpc_trustdom_add},
2114 {"del", rpc_trustdom_del},
2115 {"establish", rpc_trustdom_establish},
2116 {"revoke", rpc_trustdom_revoke},
2117 {"help", rpc_trustdom_usage},
2118 {"list", rpc_trustdom_list},
2123 rpc_trustdom_usage(argc, argv);
2127 return (net_run_function(argc, argv, func, rpc_user_usage));
2131 * Check if a server will take rpc commands
2132 * @param flags Type of server to connect to (PDC, DMB, localhost)
2133 * if the host is not explicitly specified
2134 * @return BOOL (true means rpc supported)
2136 BOOL net_rpc_check(unsigned flags)
2138 struct cli_state cli;
2140 struct in_addr server_ip;
2141 char *server_name = NULL;
2143 /* flags (i.e. server type) may depend on command */
2144 if (!net_find_server(flags, &server_ip, &server_name))
2148 if (cli_initialise(&cli) == False)
2151 if (!cli_connect(&cli, server_name, &server_ip))
2153 if (!attempt_netbios_session_request(&cli, global_myname(),
2154 server_name, &server_ip))
2156 if (!cli_negprot(&cli))
2158 if (cli.protocol < PROTOCOL_NT1)
2168 /****************************************************************************/
2172 * Basic usage function for 'net rpc'
2173 * @param argc Standard main() style argc
2174 * @param argv Standard main() style argv. Initial components are already
2178 int net_rpc_usage(int argc, const char **argv)
2180 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2181 d_printf(" net rpc join \t\t\tto join a domain \n");
2182 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2183 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2184 d_printf(" net rpc group \t\tto list groups\n");
2185 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2186 d_printf(" net rpc file \t\t\tto list open files\n");
2187 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2188 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2189 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2190 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2191 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2192 "\t\t\t\t\tor establish trust\n");
2193 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2194 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2196 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2197 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2198 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2199 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2200 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2206 * Help function for 'net rpc'. Calls command specific help if requested
2207 * or displays usage of net rpc
2208 * @param argc Standard main() style argc
2209 * @param argv Standard main() style argv. Initial components are already
2213 int net_rpc_help(int argc, const char **argv)
2215 struct functable func[] = {
2216 {"join", rpc_join_usage},
2217 {"user", rpc_user_usage},
2218 {"group", rpc_group_usage},
2219 {"share", rpc_share_usage},
2220 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2221 {"trustdom", rpc_trustdom_usage},
2222 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2223 /*{"shutdown", rpc_shutdown_usage}, */
2228 net_rpc_usage(argc, argv);
2232 return (net_run_function(argc, argv, func, rpc_user_usage));
2237 * 'net rpc' entrypoint.
2238 * @param argc Standard main() style argc
2239 * @param argv Standard main() style argv. Initial components are already
2243 int net_rpc(int argc, const char **argv)
2245 struct functable func[] = {
2246 {"info", net_rpc_info},
2247 {"join", net_rpc_join},
2248 {"testjoin", net_rpc_testjoin},
2249 {"user", net_rpc_user},
2250 {"group", net_rpc_group},
2251 {"share", net_rpc_share},
2252 {"file", net_rpc_file},
2253 {"changetrustpw", rpc_changetrustpw},
2254 {"trustdom", rpc_trustdom},
2255 {"abortshutdown", rpc_shutdown_abort},
2256 {"shutdown", rpc_shutdown},
2257 {"samdump", rpc_samdump},
2258 {"vampire", rpc_vampire},
2259 {"getsid", net_rpc_getsid},
2260 {"help", net_rpc_help},
2263 return net_run_function(argc, argv, func, net_rpc_usage);