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"
24 extern pstring global_myname;
29 * @brief RPC based subcommands for the 'net' utility.
31 * This file should contain much of the functionality that used to
32 * be found in rpcclient, execpt that the commands should change
33 * less often, and the fucntionality should be sane (the user is not
34 * expected to know a rid/sid before they conduct an operation etc.)
36 * @todo Perhaps eventually these should be split out into a number
37 * of files, as this could get quite big.
41 /* A function of this type is passed to the 'run_rpc_command' wrapper */
42 typedef NTSTATUS (*rpc_command_fn)(const DOM_SID *, struct cli_state *, TALLOC_CTX *, int, const char **);
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
53 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli)
57 NTSTATUS result = NT_STATUS_OK;
58 uint32 info_class = 5;
62 if (!(domain_sid = malloc(sizeof(DOM_SID)))){
63 DEBUG(0,("net_get_remote_domain_sid: malloc returned NULL!\n"));
67 if (!(mem_ctx=talloc_init()))
69 DEBUG(0,("net_get_remote_domain_sid: talloc_init returned NULL!\n"));
74 if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
75 fprintf(stderr, "could not initialise lsa pipe\n");
79 result = cli_lsa_open_policy(cli, mem_ctx, True,
80 SEC_RIGHTS_MAXIMUM_ALLOWED,
82 if (!NT_STATUS_IS_OK(result)) {
86 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
87 domain_name, domain_sid);
88 if (!NT_STATUS_IS_OK(result)) {
92 cli_lsa_close(cli, mem_ctx, &pol);
93 cli_nt_session_close(cli);
94 talloc_destroy(mem_ctx);
99 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
101 if (!NT_STATUS_IS_OK(result)) {
102 fprintf(stderr, "error: %s\n", nt_errstr(result));
109 * Run a single RPC command, from start to finish.
111 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
112 * @param conn_flag a NET_FLAG_ combination. Passed to
113 * net_make_ipc_connection.
114 * @param argc Standard main() style argc
115 * @param argc Standard main() style argv. Initial components are already
117 * @return A shell status integer (0 for success)
120 static int run_rpc_command(struct cli_state *cli_arg, const char *pipe_name, int conn_flags,
122 int argc, const char **argv)
124 struct cli_state *cli = NULL;
129 /* make use of cli_state handed over as an argument, if possible */
131 cli = net_make_ipc_connection(conn_flags);
135 domain_sid = net_get_remote_domain_sid(cli);
139 if (!(mem_ctx = talloc_init())) {
140 DEBUG(0, ("talloc_init() failed\n"));
145 if (!cli_nt_session_open(cli, pipe_name)) {
146 DEBUG(0, ("Could not initialise %s pipe\n", pipe_name));
149 nt_status = fn(domain_sid, cli, mem_ctx, argc, argv);
151 if (!NT_STATUS_IS_OK(nt_status)) {
152 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
154 DEBUG(5, ("rpc command function succedded\n"));
158 if (cli->nt_pipe_fnum)
159 cli_nt_session_close(cli);
161 /* close the connection only if it was opened here */
165 talloc_destroy(mem_ctx);
167 return (!NT_STATUS_IS_OK(nt_status));
171 /****************************************************************************/
175 * Force a change of the trust acccount password.
177 * All paramaters are provided by the run_rpc_command funcion, except for
178 * argc, argv which are passes through.
180 * @param domain_sid The domain sid aquired from the remote server
181 * @param cli A cli_state connected to the server.
182 * @param mem_ctx Talloc context, destoyed on compleation of the function.
183 * @param argc Standard main() style argc
184 * @param argc Standard main() style argv. Initial components are already
187 * @return Normal NTSTATUS return.
190 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
191 int argc, const char **argv) {
193 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
197 * Force a change of the trust acccount password.
199 * @param argc Standard main() style argc
200 * @param argc Standard main() style argv. Initial components are already
203 * @return A shell status integer (0 for success)
206 static int rpc_changetrustpw(int argc, const char **argv)
208 return run_rpc_command(NULL, PIPE_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_changetrustpw_internals,
213 /****************************************************************************/
217 * Join a domain, the old way.
219 * This uses 'machinename' as the inital password, and changes it.
221 * The password should be created with 'server manager' or eqiv first.
223 * All paramaters are provided by the run_rpc_command funcion, except for
224 * argc, argv which are passes through.
226 * @param domain_sid The domain sid aquired from the remote server
227 * @param cli A cli_state connected to the server.
228 * @param mem_ctx Talloc context, destoyed on compleation of the function.
229 * @param argc Standard main() style argc
230 * @param argc Standard main() style argv. Initial components are already
233 * @return Normal NTSTATUS return.
236 static NTSTATUS rpc_join_oldstyle_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
237 int argc, const char **argv) {
239 extern pstring global_myname;
240 fstring trust_passwd;
241 unsigned char orig_trust_passwd_hash[16];
243 fstrcpy(trust_passwd, global_myname);
244 strlower(trust_passwd);
247 * Machine names can be 15 characters, but the max length on
248 * a password is 14. --jerry
251 trust_passwd[14] = '\0';
253 E_md4hash(trust_passwd, orig_trust_passwd_hash);
255 return trust_pw_change_and_store_it(cli, mem_ctx, orig_trust_passwd_hash);
259 * Join a domain, the old way.
261 * @param argc Standard main() style argc
262 * @param argc Standard main() style argv. Initial components are already
265 * @return A shell status integer (0 for success)
268 static int net_rpc_join_oldstyle(int argc, const char **argv)
270 return run_rpc_command(NULL, PIPE_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_join_oldstyle_internals,
275 * Basic usage function for 'net rpc join'
276 * @param argc Standard main() style argc
277 * @param argc Standard main() style argv. Initial components are already
281 static int rpc_join_usage(int argc, const char **argv)
283 d_printf("net rpc join -U <username>[%%password] [options]\n"\
284 "\t to join a domain with admin username & password\n"\
285 "\t\t password will be prompted if none is specified\n");
286 d_printf("net rpc join [options except -U]\n"\
287 "\t to join a domain created in server manager\n\n\n");
289 net_common_flags_usage(argc, argv);
294 * 'net rpc join' entrypoint.
295 * @param argc Standard main() style argc
296 * @param argc Standard main() style argv. Initial components are already
299 * Main 'net_rpc_join()' (where the admain username/password is used) is
301 * Assume if a -U is specified, it's the new style, otherwise it's the
305 int net_rpc_join(int argc, const char **argv)
307 if ((net_rpc_join_oldstyle(argc, argv) == 0))
310 return net_rpc_join_newstyle(argc, argv);
316 * display info about a rpc domain
318 * All paramaters are provided by the run_rpc_command function, except for
319 * argc, argv which are passes through.
321 * @param domain_sid The domain sid acquired from the remote server
322 * @param cli A cli_state connected to the server.
323 * @param mem_ctx Talloc context, destoyed on completion of the function.
324 * @param argc Standard main() style argc
325 * @param argv Standard main() style argv. Initial components are already
328 * @return Normal NTSTATUS return.
332 rpc_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
333 TALLOC_CTX *mem_ctx, int argc, const char **argv)
335 POLICY_HND connect_pol, domain_pol;
336 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
339 /* Get sam policy handle */
340 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
342 if (!NT_STATUS_IS_OK(result)) {
346 /* Get domain policy handle */
347 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
348 MAXIMUM_ALLOWED_ACCESS,
349 domain_sid, &domain_pol);
350 if (!NT_STATUS_IS_OK(result)) {
355 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
357 if (NT_STATUS_IS_OK(result)) {
358 TALLOC_CTX *ctx = talloc_init();
359 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
360 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
361 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
362 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
363 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
373 * 'net rpc info' entrypoint.
374 * @param argc Standard main() style argc
375 * @param argc Standard main() style argv. Initial components are already
378 int net_rpc_info(int argc, const char **argv)
380 return run_rpc_command(NULL, PIPE_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
388 /****************************************************************************/
391 * Basic usage function for 'net rpc user'
392 * @param argc Standard main() style argc.
393 * @param argv Standard main() style argv. Initial components are already
397 static int rpc_user_usage(int argc, const char **argv)
399 return net_help_user(argc, argv);
403 * Add a new user to a remote RPC server
405 * All paramaters are provided by the run_rpc_command funcion, except for
406 * argc, argv which are passes through.
408 * @param domain_sid The domain sid acquired from the remote server
409 * @param cli A cli_state connected to the server.
410 * @param mem_ctx Talloc context, destoyed on completion of the function.
411 * @param argc Standard main() style argc
412 * @param argv Standard main() style argv. Initial components are already
415 * @return Normal NTSTATUS return.
418 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
419 int argc, const char **argv) {
421 POLICY_HND connect_pol, domain_pol, user_pol;
422 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
423 const char *acct_name;
425 uint32 unknown, user_rid;
428 d_printf("User must be specified\n");
429 rpc_user_usage(argc, argv);
435 /* Get sam policy handle */
437 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
439 if (!NT_STATUS_IS_OK(result)) {
443 /* Get domain policy handle */
445 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
446 MAXIMUM_ALLOWED_ACCESS,
447 domain_sid, &domain_pol);
448 if (!NT_STATUS_IS_OK(result)) {
452 /* Create domain user */
454 acb_info = ACB_NORMAL;
455 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
457 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
458 acct_name, acb_info, unknown,
459 &user_pol, &user_rid);
460 if (!NT_STATUS_IS_OK(result)) {
465 if (!NT_STATUS_IS_OK(result)) {
466 d_printf("Failed to add user %s - %s\n", acct_name,
469 d_printf("Added user %s\n", acct_name);
475 * Add a new user to a remote RPC server
477 * @param argc Standard main() style argc
478 * @param argv Standard main() style argv. Initial components are already
481 * @return A shell status integer (0 for success)
484 static int rpc_user_add(int argc, const char **argv)
486 return run_rpc_command(NULL, PIPE_SAMR, 0, rpc_user_add_internals,
491 * Delete a user from a remote RPC server
493 * All paramaters are provided by the run_rpc_command funcion, except for
494 * argc, argv which are passes through.
496 * @param domain_sid The domain sid acquired from the remote server
497 * @param cli A cli_state connected to the server.
498 * @param mem_ctx Talloc context, destoyed on completion of the function.
499 * @param argc Standard main() style argc
500 * @param argv Standard main() style argv. Initial components are already
503 * @return Normal NTSTATUS return.
506 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
507 struct cli_state *cli,
509 int argc, const char **argv)
511 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
512 POLICY_HND connect_pol, domain_pol, user_pol;
515 d_printf("User must be specified\n");
516 rpc_user_usage(argc, argv);
519 /* Get sam policy and domain handles */
521 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
524 if (!NT_STATUS_IS_OK(result)) {
528 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
529 MAXIMUM_ALLOWED_ACCESS,
530 domain_sid, &domain_pol);
532 if (!NT_STATUS_IS_OK(result)) {
536 /* Get handle on user */
539 uint32 *user_rids, num_rids, *name_types;
540 uint32 flags = 0x000003e8; /* Unknown */
542 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
544 &num_rids, &user_rids,
547 if (!NT_STATUS_IS_OK(result)) {
551 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
552 MAXIMUM_ALLOWED_ACCESS,
553 user_rids[0], &user_pol);
555 if (!NT_STATUS_IS_OK(result)) {
562 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
564 if (!NT_STATUS_IS_OK(result)) {
568 /* Display results */
576 * Delete a user from a remote RPC server
578 * @param argc Standard main() style argc
579 * @param argv Standard main() style argv. Initial components are already
582 * @return A shell status integer (0 for success)
585 static int rpc_user_delete(int argc, const char **argv)
587 return run_rpc_command(NULL, PIPE_SAMR, 0, rpc_user_del_internals,
592 * List user's groups on a remote RPC server
594 * All paramaters are provided by the run_rpc_command funcion, except for
595 * argc, argv which are passes through.
597 * @param domain_sid The domain sid acquired from the remote server
598 * @param cli A cli_state connected to the server.
599 * @param mem_ctx Talloc context, destoyed on completion of the function.
600 * @param argc Standard main() style argc
601 * @param argv Standard main() style argv. Initial components are already
604 * @return Normal NTSTATUS return.
608 rpc_user_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
609 TALLOC_CTX *mem_ctx, int argc, const char **argv)
611 POLICY_HND connect_pol, domain_pol, user_pol;
612 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
613 uint32 *rids, num_rids, *name_types, num_names;
614 uint32 flags = 0x000003e8; /* Unknown */
620 d_printf("User must be specified\n");
621 rpc_user_usage(argc, argv);
624 /* Get sam policy handle */
626 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
628 if (!NT_STATUS_IS_OK(result)) goto done;
630 /* Get domain policy handle */
632 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
633 MAXIMUM_ALLOWED_ACCESS,
634 domain_sid, &domain_pol);
635 if (!NT_STATUS_IS_OK(result)) goto done;
637 /* Get handle on user */
639 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
641 &num_rids, &rids, &name_types);
643 if (!NT_STATUS_IS_OK(result)) goto done;
645 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
646 MAXIMUM_ALLOWED_ACCESS,
648 if (!NT_STATUS_IS_OK(result)) goto done;
650 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
651 &num_rids, &user_gids);
655 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
657 for (i = 0; i < num_rids; i++)
658 rids[i] = user_gids[i].g_rid;
660 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
661 flags, num_rids, rids,
662 &num_names, &names, &name_types);
664 if (!NT_STATUS_IS_OK(result)) {
668 /* Display results */
670 for (i = 0; i < num_names; i++)
671 printf("%s\n", names[i]);
678 * List a user's groups from a remote RPC server
680 * @param argc Standard main() style argc
681 * @param argv Standard main() style argv. Initial components are already
684 * @return A shell status integer (0 for success)
687 static int rpc_user_info(int argc, const char **argv)
689 return run_rpc_command(NULL, PIPE_SAMR, 0, rpc_user_info_internals,
694 * List users on a remote RPC server
696 * All paramaters are provided by the run_rpc_command function, except for
697 * argc, argv which are passes through.
699 * @param domain_sid The domain sid acquired from the remote server
700 * @param cli A cli_state connected to the server.
701 * @param mem_ctx Talloc context, destoyed on completion of the function.
702 * @param argc Standard main() style argc
703 * @param argv Standard main() style argv. Initial components are already
706 * @return Normal NTSTATUS return.
710 rpc_user_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
711 TALLOC_CTX *mem_ctx, int argc, const char **argv)
713 POLICY_HND connect_pol, domain_pol;
714 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
715 uint32 start_idx=0, max_entries=250, num_entries, i;
716 SAM_DISPINFO_CTR ctr;
717 SAM_DISPINFO_1 info1;
719 /* Get sam policy handle */
721 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
723 if (!NT_STATUS_IS_OK(result)) {
727 /* Get domain policy handle */
729 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
730 MAXIMUM_ALLOWED_ACCESS,
731 domain_sid, &domain_pol);
732 if (!NT_STATUS_IS_OK(result)) {
736 /* Query domain users */
739 ctr.sam.info1 = &info1;
740 if (opt_long_list_entries)
741 d_printf("\nUser name Comment"\
742 "\n-----------------------------\n");
745 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
746 &start_idx, 1, &num_entries,
748 for (i = 0; i < num_entries; i++) {
749 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
750 if (opt_long_list_entries)
751 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
753 if (opt_long_list_entries)
754 printf("%-21.21s %-50.50s\n", user, desc);
756 printf("%s\n", user);
758 } while (!NT_STATUS_IS_OK(result));
765 * 'net rpc user' entrypoint.
766 * @param argc Standard main() style argc
767 * @param argc Standard main() style argv. Initial components are already
771 int net_rpc_user(int argc, const char **argv)
773 struct functable func[] = {
774 {"add", rpc_user_add},
775 {"info", rpc_user_info},
776 {"delete", rpc_user_delete},
781 if (opt_long_list_entries) {
784 return run_rpc_command(NULL,PIPE_SAMR, 0,
785 rpc_user_list_internals,
789 return net_run_function(argc, argv, func, rpc_user_usage);
793 /****************************************************************************/
796 * Basic usage function for 'net rpc group'
797 * @param argc Standard main() style argc.
798 * @param argv Standard main() style argv. Initial components are already
802 static int rpc_group_usage(int argc, const char **argv)
804 return net_help_group(argc, argv);
808 * List groups on a remote RPC server
810 * All paramaters are provided by the run_rpc_command funcion, except for
811 * argc, argv which are passes through.
813 * @param domain_sid The domain sid acquired from the remote server
814 * @param cli A cli_state connected to the server.
815 * @param mem_ctx Talloc context, destoyed on completion of the function.
816 * @param argc Standard main() style argc
817 * @param argv Standard main() style argv. Initial components are already
820 * @return Normal NTSTATUS return.
824 rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
825 TALLOC_CTX *mem_ctx, int argc, const char **argv)
827 POLICY_HND connect_pol, domain_pol;
828 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
829 uint32 start_idx=0, max_entries=250, num_entries, i;
830 struct acct_info *groups;
831 DOM_SID global_sid_Builtin;
833 string_to_sid(&global_sid_Builtin, "S-1-5-32");
835 /* Get sam policy handle */
837 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
839 if (!NT_STATUS_IS_OK(result)) {
843 /* Get domain policy handle */
845 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
846 MAXIMUM_ALLOWED_ACCESS,
847 domain_sid, &domain_pol);
848 if (!NT_STATUS_IS_OK(result)) {
852 /* Query domain groups */
853 if (opt_long_list_entries)
854 d_printf("\nGroup name Comment"\
855 "\n-----------------------------\n");
857 result = cli_samr_enum_dom_groups(cli, mem_ctx, &domain_pol,
858 &start_idx, max_entries,
859 &groups, &num_entries);
861 for (i = 0; i < num_entries; i++) {
862 if (opt_long_list_entries)
863 printf("%-21.21s %-50.50s\n",
865 groups[i].acct_desc);
867 printf("%-21.21s\n", groups[i].acct_name);
869 } while (!NT_STATUS_IS_OK(result));
870 /* query domain aliases */
872 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
873 &start_idx, max_entries,
874 &groups, &num_entries);
876 for (i = 0; i < num_entries; i++) {
877 if (opt_long_list_entries)
878 printf("%-21.21s %-50.50s\n",
880 groups[i].acct_desc);
882 printf("%-21.21s\n", groups[i].acct_name);
884 } while (!NT_STATUS_IS_OK(result));
885 cli_samr_close(cli, mem_ctx, &domain_pol);
886 /* Get builtin policy handle */
888 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
889 MAXIMUM_ALLOWED_ACCESS,
890 &global_sid_Builtin, &domain_pol);
891 if (!NT_STATUS_IS_OK(result)) {
894 /* query builtin aliases */
896 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
897 &start_idx, max_entries,
898 &groups, &num_entries);
900 for (i = 0; i < num_entries; i++) {
901 if (opt_long_list_entries)
902 printf("%-21.21s %-50.50s\n",
904 groups[i].acct_desc);
906 printf("%s\n", groups[i].acct_name);
908 } while (!NT_STATUS_IS_OK(result));
915 * 'net rpc group' entrypoint.
916 * @param argc Standard main() style argc
917 * @param argc Standard main() style argv. Initial components are already
921 int net_rpc_group(int argc, const char **argv)
923 struct functable func[] = {
925 {"add", rpc_group_add},
926 {"delete", rpc_group_delete},
932 if (opt_long_list_entries) {
935 return run_rpc_command(NULL, PIPE_SAMR, 0,
936 rpc_group_list_internals,
940 return net_run_function(argc, argv, func, rpc_group_usage);
943 /****************************************************************************/
945 static int rpc_share_usage(int argc, const char **argv)
947 return net_help_share(argc, argv);
951 * Add a share on a remote RPC server
953 * All paramaters are provided by the run_rpc_command function, except for
954 * argc, argv which are passes through.
956 * @param domain_sid The domain sid acquired from the remote server
957 * @param cli A cli_state connected to the server.
958 * @param mem_ctx Talloc context, destoyed on completion of the function.
959 * @param argc Standard main() style argc
960 * @param argv Standard main() style argv. Initial components are already
963 * @return Normal NTSTATUS return.
966 rpc_share_add_internals(const DOM_SID *domain_sid, struct cli_state *cli,
967 TALLOC_CTX *mem_ctx,int argc, const char **argv)
970 char *sharename=talloc_strdup(mem_ctx, argv[0]);
972 uint32 type=0; /* only allow disk shares to be added */
973 uint32 num_users=0, perms=0;
974 char *password=NULL; /* don't allow a share password */
976 path = strchr(sharename, '=');
978 return NT_STATUS_UNSUCCESSFUL;
981 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
982 opt_comment, perms, opt_maxusers,
983 num_users, path, password);
984 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
987 static int rpc_share_add(int argc, const char **argv)
989 if ((argc < 1) || !strchr(argv[0], '=')) {
990 DEBUG(1,("Sharename or path not specified on add\n"));
991 return rpc_share_usage(argc, argv);
993 return run_rpc_command(NULL, PIPE_SRVSVC, 0,
994 rpc_share_add_internals,
999 * Delete a share on a remote RPC server
1001 * All paramaters are provided by the run_rpc_command function, except for
1002 * argc, argv which are passes through.
1004 * @param domain_sid The domain sid acquired from the remote server
1005 * @param cli A cli_state connected to the server.
1006 * @param mem_ctx Talloc context, destoyed on completion of the function.
1007 * @param argc Standard main() style argc
1008 * @param argv Standard main() style argv. Initial components are already
1011 * @return Normal NTSTATUS return.
1014 rpc_share_del_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1015 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1019 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1020 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1024 * Delete a share on a remote RPC server
1026 * @param domain_sid The domain sid acquired from the remote server
1027 * @param argc Standard main() style argc
1028 * @param argv Standard main() style argv. Initial components are already
1031 * @return A shell status integer (0 for success)
1033 static int rpc_share_delete(int argc, const char **argv)
1036 DEBUG(1,("Sharename not specified on delete\n"));
1037 return rpc_share_usage(argc, argv);
1039 return run_rpc_command(NULL, PIPE_SRVSVC, 0,
1040 rpc_share_del_internals,
1045 * Formatted print of share info
1047 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1050 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1052 fstring netname = "", remark = "";
1054 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1055 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1057 if (opt_long_list_entries) {
1058 d_printf("%-12.12s %-8.8s %-50.50s\n",
1059 netname, share_type[info1->info_1.type], remark);
1061 d_printf("%-12.12s\n", netname);
1067 * List shares on a remote RPC server
1069 * All paramaters are provided by the run_rpc_command function, except for
1070 * argc, argv which are passes through.
1072 * @param domain_sid The domain sid acquired from the remote server
1073 * @param cli A cli_state connected to the server.
1074 * @param mem_ctx Talloc context, destoyed on completion of the function.
1075 * @param argc Standard main() style argc
1076 * @param argv Standard main() style argv. Initial components are already
1079 * @return Normal NTSTATUS return.
1083 rpc_share_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1084 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1086 SRV_SHARE_INFO_CTR ctr;
1089 uint32 preferred_len = 0xffffffff, i;
1091 init_enum_hnd(&hnd, 0);
1093 result = cli_srvsvc_net_share_enum(
1094 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1096 if (!W_ERROR_IS_OK(result))
1099 /* Display results */
1101 if (opt_long_list_entries) {
1103 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1104 "\nShare name Type Description\n"\
1105 "---------- ---- -----------\n");
1107 for (i = 0; i < ctr.num_entries; i++)
1108 display_share_info_1(&ctr.share.info1[i]);
1110 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1114 * 'net rpc share' entrypoint.
1115 * @param argc Standard main() style argc
1116 * @param argv Standard main() style argv. Initial components are already
1120 int net_rpc_share(int argc, const char **argv)
1122 struct functable func[] = {
1123 {"add", rpc_share_add},
1124 {"delete", rpc_share_delete},
1129 return run_rpc_command(NULL, PIPE_SRVSVC, 0,
1130 rpc_share_list_internals,
1133 return net_run_function(argc, argv, func, rpc_share_usage);
1136 /****************************************************************************/
1138 static int rpc_file_usage(int argc, const char **argv)
1140 return net_help_file(argc, argv);
1144 * Close a file on a remote RPC server
1146 * All paramaters are provided by the run_rpc_command function, except for
1147 * argc, argv which are passes through.
1149 * @param domain_sid The domain sid acquired from the remote server
1150 * @param cli A cli_state connected to the server.
1151 * @param mem_ctx Talloc context, destoyed on completion of the function.
1152 * @param argc Standard main() style argc
1153 * @param argv Standard main() style argv. Initial components are already
1156 * @return Normal NTSTATUS return.
1159 rpc_file_close_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1160 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1163 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1164 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1168 * Close a file on a remote RPC server
1170 * @param argc Standard main() style argc
1171 * @param argv Standard main() style argv. Initial components are already
1174 * @return A shell status integer (0 for success)
1176 static int rpc_file_close(int argc, const char **argv)
1179 DEBUG(1, ("No fileid given on close\n"));
1180 return(rpc_file_usage(argc, argv));
1183 return run_rpc_command(NULL, PIPE_SRVSVC, 0,
1184 rpc_file_close_internals,
1189 * Formatted print of open file info
1191 * @param info3 FILE_INFO_3 contents
1192 * @param str3 strings for FILE_INFO_3
1195 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1197 fstring user = "", path = "";
1199 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1200 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1202 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1203 info3->id, user, info3->perms, info3->num_locks, path);
1207 * List open files on a remote RPC server
1209 * All paramaters are provided by the run_rpc_command funcion, except for
1210 * argc, argv which are passes through.
1212 * @param domain_sid The domain sid acquired from the remote server
1213 * @param cli A cli_state connected to the server.
1214 * @param mem_ctx Talloc context, destoyed on completion of the function.
1215 * @param argc Standard main() style argc
1216 * @param argv Standard main() style argv. Initial components are already
1219 * @return Normal NTSTATUS return.
1223 rpc_file_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1224 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1226 SRV_FILE_INFO_CTR ctr;
1229 uint32 preferred_len = 0xffffffff, i;
1230 const char *username=NULL;
1232 init_enum_hnd(&hnd, 0);
1234 /* if argc > 0, must be user command */
1236 username = smb_xstrdup(argv[0]);
1238 result = cli_srvsvc_net_file_enum(
1239 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1241 if (!W_ERROR_IS_OK(result))
1244 /* Display results */
1247 "\nEnumerating open files on remote server:\n\n"\
1248 "\nFileId Opened by Perms Locks Path"\
1249 "\n------ --------- ----- ----- ---- \n");
1250 for (i = 0; i < ctr.num_entries; i++)
1251 display_file_info_3(&ctr.file.info3[i].info_3,
1252 &ctr.file.info3[i].info_3_str);
1254 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1259 * List files for a user on a remote RPC server
1261 * @param argc Standard main() style argc
1262 * @param argv Standard main() style argv. Initial components are already
1265 * @return A shell status integer (0 for success)
1267 static int rpc_file_user(int argc, const char **argv)
1270 DEBUG(1, ("No username given\n"));
1271 return(rpc_file_usage(argc, argv));
1274 return run_rpc_command(NULL, PIPE_SRVSVC, 0,
1275 rpc_file_list_internals,
1281 * 'net rpc file' entrypoint.
1282 * @param argc Standard main() style argc
1283 * @param argv Standard main() style argv. Initial components are already
1287 int net_rpc_file(int argc, const char **argv)
1289 struct functable func[] = {
1290 {"close", rpc_file_close},
1291 {"user", rpc_file_user},
1293 {"info", rpc_file_info},
1299 return run_rpc_command(NULL, PIPE_SRVSVC, 0,
1300 rpc_file_list_internals,
1303 return net_run_function(argc, argv, func, rpc_file_usage);
1306 /****************************************************************************/
1311 * ABORT the shutdown of a remote RPC Server
1313 * All paramaters are provided by the run_rpc_command function, except for
1314 * argc, argv which are passed through.
1316 * @param domain_sid The domain sid aquired from the remote server
1317 * @param cli A cli_state connected to the server.
1318 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1319 * @param argc Standard main() style argc
1320 * @param argv Standard main() style argv. Initial components are already
1323 * @return Normal NTSTATUS return.
1326 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1327 int argc, const char **argv)
1329 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1331 result = cli_reg_abort_shutdown(cli, mem_ctx);
1333 if (NT_STATUS_IS_OK(result))
1334 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1336 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1343 * ABORT the Shut down of a remote RPC server
1345 * @param argc Standard main() style argc
1346 * @param argv Standard main() style argv. Initial components are already
1349 * @return A shell status integer (0 for success)
1352 static int rpc_shutdown_abort(int argc, const char **argv)
1354 return run_rpc_command(NULL, PIPE_WINREG, 0, rpc_shutdown_abort_internals,
1359 * Shut down a remote RPC Server
1361 * All paramaters are provided by the run_rpc_command funcion, except for
1362 * argc, argv which are passes through.
1364 * @param domain_sid The domain sid aquired from the remote server
1365 * @param cli A cli_state connected to the server.
1366 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1367 * @param argc Standard main() style argc
1368 * @param argc Standard main() style argv. Initial components are already
1371 * @return Normal NTSTATUS return.
1374 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1375 int argc, const char **argv)
1377 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1378 char *msg = "This machine will be shutdown shortly";
1379 uint32 timeout = 20;
1381 BOOL reboot = opt_reboot;
1382 BOOL force = opt_force;
1387 struct poptOption long_options[] = {
1388 {"message", 'm', POPT_ARG_STRING, &msg},
1389 {"timeout", 't', POPT_ARG_INT, &timeout},
1390 {"reboot", 'r', POPT_ARG_NONE, &reboot},
1391 {"force", 'f', POPT_ARG_NONE, &force},
1395 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1396 POPT_CONTEXT_KEEP_FIRST);
1398 rc = poptGetNextOpt(pc);
1401 /* an error occurred during option processing */
1402 DEBUG(0, ("%s: %s\n",
1403 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1405 return NT_STATUS_INVALID_PARAMETER;
1409 flgs |= REG_REBOOT_ON_SHUTDOWN;
1412 flgs |= REG_FORCE_SHUTDOWN;
1418 timeout = opt_timeout;
1421 /* create an entry */
1422 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, flgs);
1424 if (NT_STATUS_IS_OK(result))
1425 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1427 DEBUG(0,("Shutdown of remote machine failed!\n"));
1433 * Shut down a remote RPC server
1435 * @param argc Standard main() style argc
1436 * @param argc Standard main() style argv. Initial components are already
1439 * @return A shell status integer (0 for success)
1442 static int rpc_shutdown(int argc, const char **argv)
1444 return run_rpc_command(NULL, PIPE_WINREG, 0, rpc_shutdown_internals,
1448 /***************************************************************************
1449 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1451 ***************************************************************************/
1454 * Add interdomain trust account to the RPC server.
1455 * All parameters (except for argc and argv) are passed by run_rpc_command
1458 * @param domain_sid The domain sid acquired from the server
1459 * @param cli A cli_state connected to the server.
1460 * @param mem_ctx Talloc context, destoyed on completion of the function.
1461 * @param argc Standard main() style argc
1462 * @param argc Standard main() style argv. Initial components are already
1465 * @return normal NTSTATUS return code
1468 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1469 int argc, const char **argv) {
1471 POLICY_HND connect_pol, domain_pol, user_pol;
1472 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1475 uint32 unknown, user_rid;
1478 d_printf("Usage: net rpc trustdom add <domain_name>\n");
1479 return NT_STATUS_INVALID_PARAMETER;
1483 * Make valid trusting domain account (ie. uppercased and with '$' appended)
1486 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1487 return NT_STATUS_NO_MEMORY;
1490 strupper(acct_name);
1492 /* Get samr policy handle */
1493 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1495 if (!NT_STATUS_IS_OK(result)) {
1499 /* Get domain policy handle */
1500 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1501 MAXIMUM_ALLOWED_ACCESS,
1502 domain_sid, &domain_pol);
1503 if (!NT_STATUS_IS_OK(result)) {
1507 /* Create trusting domain's account */
1508 acb_info = ACB_DOMTRUST;
1509 unknown = 0xe005000b; /* No idea what this is - a permission mask?
1510 mimir: yes, most probably it is */
1512 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1513 acct_name, acb_info, unknown,
1514 &user_pol, &user_rid);
1515 if (!NT_STATUS_IS_OK(result)) {
1520 SAFE_FREE(acct_name);
1525 * Create interdomain trust account for a remote domain.
1527 * @param argc standard argc
1528 * @param argv standard argv without initial components
1530 * @return Integer status (0 means success)
1533 static int rpc_trustdom_add(int argc, const char **argv)
1535 return run_rpc_command(NULL, PIPE_SAMR, 0, rpc_trustdom_add_internals,
1541 * Delete interdomain trust account for a remote domain.
1543 * @param argc standard argc
1544 * @param argv standard argv without initial components
1546 * @return Integer status (0 means success)
1549 static int rpc_trustdom_del(int argc, const char **argv)
1551 d_printf("Sorry, not yet implemented.\n");
1557 * Establish trust relationship to a trusting domain.
1558 * Interdomain account must already be created on remote PDC.
1560 * @param argc standard argc
1561 * @param argv standard argv without initial components
1563 * @return Integer status (0 means success)
1566 extern char *opt_user_name;
1567 extern char *opt_password;
1568 extern char *opt_workgroup;
1570 static int rpc_trustdom_establish(int argc, const char **argv)
1572 struct cli_state *cli;
1573 struct in_addr server_ip;
1574 POLICY_HND connect_hnd;
1575 TALLOC_CTX *mem_ctx;
1578 WKS_INFO_100 wks_info;
1585 * Connect to \\server\ipc$ as 'our domain' account with password
1589 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
1593 domain_name = smb_xstrdup(argv[0]);
1594 strupper(domain_name);
1597 * opt_workgroup will be used by connection functions further,
1598 * hence it should be set to remote domain name instead of ours
1600 if (opt_workgroup) {
1601 SAFE_FREE(opt_workgroup);
1602 opt_workgroup = smb_xstrdup(domain_name);
1605 asprintf(&acct_name, "%s$", lp_workgroup());
1606 strupper(acct_name);
1608 opt_user_name = acct_name;
1610 /* find the domain controller */
1611 if (!net_find_dc(&server_ip, pdc_name, domain_name)) {
1612 DEBUG(0, ("Coulnd find domain controller for domain %s\n", domain_name));
1616 /* connect to ipc$ as username/password */
1617 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
1618 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1620 /* Is it trusting domain account for sure ? */
1621 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
1622 nt_errstr(nt_status)));
1627 * Connect to \\server\ipc$ again (this time anonymously)
1630 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
1632 if (NT_STATUS_IS_ERR(nt_status)) {
1633 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
1634 domain_name, nt_errstr(nt_status)));
1638 * Use NetServerEnum2 to make sure we're talking to a proper server
1641 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
1642 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
1643 for domain %s\n", domain_name));
1647 * Call WksQueryInfo to check remote server's capabilities
1648 * note: It is now used only to get unicode domain name
1651 if (!cli_nt_session_open(cli, PIPE_WKSSVC)) {
1652 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
1656 if (!(mem_ctx = talloc_init_named("establishing trust relationship to domain %s",
1658 DEBUG(0, ("talloc_init() failed\n"));
1663 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
1665 if (NT_STATUS_IS_ERR(nt_status)) {
1666 DEBUG(0, ("WksQueryInfo call failed.\n"));
1670 if (cli->nt_pipe_fnum)
1671 cli_nt_session_close(cli);
1675 * Call LsaOpenPolicy and LsaQueryInfo
1678 if (!(mem_ctx = talloc_init())) {
1679 DEBUG(0, ("talloc_init() failed\n"));
1684 if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
1685 DEBUG(0, ("Could not initialise lsa pipe\n"));
1690 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1692 if (NT_STATUS_IS_ERR(nt_status)) {
1693 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1694 nt_errstr(nt_status)));
1698 /* Querying info level 5 */
1700 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1701 5 /* info level */, domain_name,
1703 if (NT_STATUS_IS_ERR(nt_status)) {
1704 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1705 nt_errstr(nt_status)));
1712 /* There should be actually query info level 3 (following nt serv behaviour),
1713 but I still don't know if it's _really_ necessary */
1716 * Store the password in secrets db
1719 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
1720 wks_info.uni_lan_grp.uni_str_len, opt_password,
1722 DEBUG(0, ("Storing password for trusted domain failed.\n"));
1727 * Close the pipes and clean up
1730 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1731 if (NT_STATUS_IS_ERR(nt_status)) {
1732 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
1733 nt_errstr(nt_status)));
1737 if (cli->nt_pipe_fnum)
1738 cli_nt_session_close(cli);
1740 talloc_destroy(mem_ctx);
1742 DEBUG(0, ("Success!\n"));
1747 * Revoke trust relationship to the remote domain
1749 * @param argc standard argc
1750 * @param argv standard argv without initial components
1752 * @return Integer status (0 means success)
1755 static int rpc_trustdom_revoke(int argc, const char **argv)
1759 if (argc < 1) return -1;
1761 /* generate upper cased domain name */
1762 domain_name = smb_xstrdup(argv[0]);
1763 strupper(domain_name);
1765 /* delete password of the trust */
1766 if (!trusted_domain_password_delete(domain_name)) {
1767 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
1776 * Usage for 'net rpc trustdom' command
1778 * @param argc standard argc
1779 * @param argv standard argv without inital components
1781 * @return Integer status returned to shell
1784 static int rpc_trustdom_usage(int argc, const char **argv)
1786 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
1787 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
1788 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
1789 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
1790 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
1795 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1796 int argc, const char **argv)
1799 sid_to_string(str_sid, domain_sid);
1800 d_printf("%s\n", str_sid);
1801 return NT_STATUS_OK;
1805 extern char* opt_workgroup;
1806 extern char* opt_target_worgroup;
1807 extern char* opt_host;
1808 extern char* opt_password;
1810 static int rpc_trustdom_list(int argc, const char **argv)
1812 /* common variables */
1813 TALLOC_CTX* mem_ctx;
1814 struct cli_state *cli, *remote_cli;
1816 char *domain_name = NULL;
1817 DOM_SID queried_dom_sid;
1818 fstring ascii_sid, padding;
1819 int ascii_dom_name_len;
1820 POLICY_HND connect_hnd;
1822 /* trusted domains listing variables */
1823 int enum_ctx = 0, pref_num_domains = 5;
1824 int num_domains, i, pad_len, col_len = 20;
1825 DOM_SID *domain_sids;
1826 char **trusted_dom_names;
1829 /* trusting domains listing variables */
1830 POLICY_HND domain_hnd;
1831 char **trusting_dom_names;
1832 uint32 *trusting_dom_rids;
1835 * Listing trusted domains (stored in secrets.tdb, if local)
1838 mem_ctx = talloc_init_named("trust relationships listing");
1841 * set domain and pdc name to local samba server (default)
1842 * or to remote one given in command line
1844 strupper(opt_workgroup);
1845 if (strcmp(opt_workgroup, lp_workgroup())) {
1846 domain_name = opt_workgroup;
1847 if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
1848 opt_target_workgroup = opt_workgroup;
1850 safe_strcpy(pdc_name, global_myname, FSTRING_LEN);
1851 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
1852 if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
1853 opt_target_workgroup = domain_name;
1856 /* open \PIPE\lsarpc and open policy handle */
1857 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
1858 DEBUG(0, ("Couldn't connect to domain controller\n"));
1862 if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
1863 DEBUG(0, ("Could not initialise lsa pipe\n"));
1867 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1869 if (NT_STATUS_IS_ERR(nt_status)) {
1870 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1871 nt_errstr(nt_status)));
1875 /* query info level 5 to obtain sid of a domain being queried */
1876 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1877 5 /* info level */, domain_name, &queried_dom_sid);
1878 if (NT_STATUS_IS_ERR(nt_status)) {
1879 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1880 nt_errstr(nt_status)));
1885 * Keep calling LsaEnumTrustdom over opened pipe until
1886 * the end of enumeration is reached
1889 d_printf("Trusted domains list:\n\n");
1892 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
1893 &pref_num_domains, &num_domains,
1894 &trusted_dom_names, &domain_sids);
1896 if (NT_STATUS_IS_ERR(nt_status)) {
1897 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
1898 nt_errstr(nt_status)));
1902 for (i = 0; i < num_domains; i++) {
1903 /* convert sid into ascii string */
1904 sid_to_string(ascii_sid, &(domain_sids[i]));
1906 /* calculate padding space for d_printf to look nicer */
1907 pad_len = col_len - strlen(trusted_dom_names[i]);
1908 padding[pad_len] = 0;
1909 do padding[--pad_len] = ' '; while (pad_len);
1911 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
1914 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
1916 /* close this connection before doing next one */
1917 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1918 if (NT_STATUS_IS_ERR(nt_status)) {
1919 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
1920 nt_errstr(nt_status)));
1924 cli_nt_session_close(cli);
1927 * Listing trusting domains (stored in passdb backend, if local)
1930 d_printf("\nTrusting domains list:\n\n");
1933 * Open \PIPE\samr and get needed policy handles
1935 if (!cli_nt_session_open(cli, PIPE_SAMR)) {
1936 DEBUG(0, ("Could not initialise samr pipe\n"));
1941 nt_status = cli_samr_connect(cli, mem_ctx, SAMR_ACCESS_OPEN_DOMAIN,
1943 if (!NT_STATUS_IS_OK(nt_status)) {
1944 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
1945 nt_errstr(nt_status)));
1949 /* SamrOpenDomain - we have to open domain policy handle in order to be
1950 able to enumerate accounts*/
1951 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
1952 DOMAIN_ACCESS_ENUM_ACCOUNTS,
1953 &queried_dom_sid, &domain_hnd);
1954 if (!NT_STATUS_IS_OK(nt_status)) {
1955 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
1956 nt_errstr(nt_status)));
1961 * perform actual enumeration
1964 enum_ctx = 0; /* reset enumeration context from last enumeration */
1967 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
1968 &enum_ctx, ACB_DOMTRUST, 0xffff,
1969 &trusting_dom_names, &trusting_dom_rids,
1971 if (NT_STATUS_IS_ERR(nt_status)) {
1972 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
1973 nt_errstr(nt_status)));
1977 for (i = 0; i < num_domains; i++) {
1980 * get each single domain's sid (do we _really_ need this ?):
1981 * 1) connect to domain's pdc
1982 * 2) query the pdc for domain's sid
1985 /* get rid of '$' tail */
1986 ascii_dom_name_len = strlen(trusting_dom_names[i]);
1987 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
1988 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
1990 /* calculate padding space for d_printf to look nicer */
1991 pad_len = col_len - strlen(trusting_dom_names[i]);
1992 padding[pad_len] = 0;
1993 do padding[--pad_len] = ' '; while (pad_len);
1995 /* set opt_* variables to remote domain */
1996 strupper(trusting_dom_names[i]);
1997 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
1998 if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
1999 opt_target_workgroup = opt_workgroup;
2001 d_printf("%s%s", trusting_dom_names[i], padding);
2003 /* connect to remote domain controller */
2004 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2006 /* query for domain's sid */
2007 if (run_rpc_command(remote_cli, PIPE_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2008 d_printf("couldn't get domain's sid\n");
2010 cli_shutdown(remote_cli);
2013 d_printf("domain controller is not responding\n");
2017 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2019 /* close opened samr and domain policy handles */
2020 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2021 if (!NT_STATUS_IS_OK(nt_status)) {
2022 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2025 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2026 if (!NT_STATUS_IS_OK(nt_status)) {
2027 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2030 /* close samr pipe and connection to IPC$ */
2031 cli_nt_session_close(cli);
2034 talloc_destroy(mem_ctx);
2039 * Entrypoint for 'net rpc trustdom' code
2041 * @param argc standard argc
2042 * @param argv standard argv without initial components
2044 * @return Integer status (0 means success)
2047 static int rpc_trustdom(int argc, const char **argv)
2049 struct functable func[] = {
2050 {"add", rpc_trustdom_add},
2051 {"del", rpc_trustdom_del},
2052 {"establish", rpc_trustdom_establish},
2053 {"revoke", rpc_trustdom_revoke},
2054 {"help", rpc_trustdom_usage},
2055 {"list", rpc_trustdom_list},
2060 rpc_trustdom_usage(argc, argv);
2064 return (net_run_function(argc, argv, func, rpc_user_usage));
2068 * Check if a server will take rpc commands
2069 * @param flags Type of server to connect to (PDC, DMB, localhost)
2070 * if the host is not explicitly specified
2071 * @return BOOL (true means rpc supported)
2073 BOOL net_rpc_check(unsigned flags)
2075 struct cli_state cli;
2077 struct in_addr server_ip;
2078 char *server_name = NULL;
2080 /* flags (i.e. server type) may depend on command */
2081 if (!net_find_server(flags, &server_ip, &server_name))
2085 if (cli_initialise(&cli) == False)
2088 if (!cli_connect(&cli, server_name, &server_ip))
2090 if (!attempt_netbios_session_request(&cli, global_myname,
2091 server_name, &server_ip))
2093 if (!cli_negprot(&cli))
2095 if (cli.protocol < PROTOCOL_NT1)
2105 /****************************************************************************/
2109 * Basic usage function for 'net rpc'
2110 * @param argc Standard main() style argc
2111 * @param argv Standard main() style argv. Initial components are already
2115 int net_rpc_usage(int argc, const char **argv)
2117 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2118 d_printf(" net rpc join \t\t\tto join a domain \n");
2119 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2120 d_printf(" net rpc group \t\tto list groups\n");
2121 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2122 d_printf(" net rpc file \t\t\tto list open files\n");
2123 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2124 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2125 "\t\t\t\t\tor establish trust\n");
2126 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2127 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2129 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2130 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2131 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2132 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2133 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2139 * Help function for 'net rpc'. Calls command specific help if requested
2140 * or displays usage of net rpc
2141 * @param argc Standard main() style argc
2142 * @param argv Standard main() style argv. Initial components are already
2146 int net_rpc_help(int argc, const char **argv)
2148 struct functable func[] = {
2149 {"join", rpc_join_usage},
2150 {"user", rpc_user_usage},
2151 {"group", rpc_group_usage},
2152 {"share", rpc_share_usage},
2153 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2154 {"trustdom", rpc_trustdom_usage},
2155 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2156 /*{"shutdown", rpc_shutdown_usage}, */
2161 net_rpc_usage(argc, argv);
2165 return (net_run_function(argc, argv, func, rpc_user_usage));
2170 * 'net rpc' entrypoint.
2171 * @param argc Standard main() style argc
2172 * @param argv Standard main() style argv. Initial components are already
2176 int net_rpc(int argc, const char **argv)
2178 struct functable func[] = {
2179 {"info", net_rpc_info},
2180 {"join", net_rpc_join},
2181 {"user", net_rpc_user},
2182 {"group", net_rpc_group},
2183 {"share", net_rpc_share},
2184 {"file", net_rpc_file},
2185 {"changetrustpw", rpc_changetrustpw},
2186 {"trustdom", rpc_trustdom},
2187 {"abortshutdown", rpc_shutdown_abort},
2188 {"shutdown", rpc_shutdown},
2189 {"help", net_rpc_help},
2192 return net_run_function(argc, argv, func, net_rpc_usage);