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, False,
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)) {
88 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
90 if (!NT_STATUS_IS_OK(result)) {
91 fprintf(stderr, "error: %s\n", nt_errstr(result));
97 cli_lsa_close(cli, mem_ctx, &pol);
98 cli_nt_session_close(cli);
99 talloc_destroy(mem_ctx);
105 * Run a single RPC command, from start to finish.
107 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
108 * @param conn_flag a NET_FLAG_ combination. Passed to
109 * net_make_ipc_connection.
110 * @param argc Standard main() style argc
111 * @param argc Standard main() style argv. Initial components are already
113 * @return A shell status integer (0 for success)
116 static int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
118 int argc, const char **argv)
120 struct cli_state *cli = NULL;
125 /* make use of cli_state handed over as an argument, if possible */
127 cli = net_make_ipc_connection(conn_flags);
135 domain_sid = net_get_remote_domain_sid(cli);
139 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
140 DEBUG(0, ("talloc_init() failed\n"));
145 if (!cli_nt_session_open(cli, pipe_idx)) {
146 DEBUG(0, ("Could not initialise pipe\n"));
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 parameters are provided by the run_rpc_command function, 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 int net_rpc_changetrustpw(int argc, const char **argv)
208 return run_rpc_command(NULL, PI_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 equiv first.
223 * All parameters are provided by the run_rpc_command function, 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_oldjoin_internals(const DOM_SID *domain_sid, struct cli_state *cli,
238 int argc, const char **argv) {
240 fstring trust_passwd;
241 unsigned char orig_trust_passwd_hash[16];
243 uint32 sec_channel_type;
246 check what type of join - if the user want's to join as
247 a BDC, the server must agree that we are a BDC.
250 sec_channel_type = get_sec_channel_type(argv[0]);
252 sec_channel_type = get_sec_channel_type(NULL);
255 fstrcpy(trust_passwd, global_myname());
256 strlower_m(trust_passwd);
259 * Machine names can be 15 characters, but the max length on
260 * a password is 14. --jerry
263 trust_passwd[14] = '\0';
265 E_md4hash(trust_passwd, orig_trust_passwd_hash);
267 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
268 orig_trust_passwd_hash,
271 if (NT_STATUS_IS_OK(result))
272 printf("Joined domain %s.\n",opt_target_workgroup);
275 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
276 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
277 result = NT_STATUS_UNSUCCESSFUL;
284 * Join a domain, the old way.
286 * @param argc Standard main() style argc
287 * @param argc Standard main() style argv. Initial components are already
290 * @return A shell status integer (0 for success)
293 static int net_rpc_oldjoin(int argc, const char **argv)
295 return run_rpc_command(NULL, PI_NETLOGON,
296 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
297 rpc_oldjoin_internals,
302 * Basic usage function for 'net rpc join'
303 * @param argc Standard main() style argc
304 * @param argc Standard main() style argv. Initial components are already
308 static int rpc_join_usage(int argc, const char **argv)
310 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
311 "\t to join a domain with admin username & password\n"\
312 "\t\t password will be prompted if needed and none is specified\n"\
313 "\t <type> can be (default MEMBER)\n"\
314 "\t\t BDC - Join as a BDC\n"\
315 "\t\t PDC - Join as a PDC\n"\
316 "\t\t MEMBER - Join as a MEMBER server\n");
318 net_common_flags_usage(argc, argv);
323 * 'net rpc join' entrypoint.
324 * @param argc Standard main() style argc
325 * @param argc Standard main() style argv. Initial components are already
328 * Main 'net_rpc_join()' (where the admain username/password is used) is
330 * Try to just change the password, but if that doesn't work, use/prompt
331 * for a username/password.
334 int net_rpc_join(int argc, const char **argv)
336 if ((net_rpc_oldjoin(argc, argv) == 0))
339 return net_rpc_join_newstyle(argc, argv);
345 * display info about a rpc domain
347 * All parameters are provided by the run_rpc_command function, except for
348 * argc, argv which are passed through.
350 * @param domain_sid The domain sid acquired from the remote server
351 * @param cli A cli_state connected to the server.
352 * @param mem_ctx Talloc context, destoyed on completion of the function.
353 * @param argc Standard main() style argc
354 * @param argv Standard main() style argv. Initial components are already
357 * @return Normal NTSTATUS return.
361 rpc_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
362 TALLOC_CTX *mem_ctx, int argc, const char **argv)
364 POLICY_HND connect_pol, domain_pol;
365 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
369 sid_to_string(sid_str, domain_sid);
371 /* Get sam policy handle */
372 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
374 if (!NT_STATUS_IS_OK(result)) {
378 /* Get domain policy handle */
379 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
380 MAXIMUM_ALLOWED_ACCESS,
381 domain_sid, &domain_pol);
382 if (!NT_STATUS_IS_OK(result)) {
387 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
389 if (NT_STATUS_IS_OK(result)) {
390 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
391 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
392 d_printf("Domain SID: %s\n", sid_str);
393 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
394 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
395 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
396 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
406 * 'net rpc info' entrypoint.
407 * @param argc Standard main() style argc
408 * @param argc Standard main() style argv. Initial components are already
411 int net_rpc_info(int argc, const char **argv)
413 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
420 * Fetch domain SID into the local secrets.tdb
422 * All parameters are provided by the run_rpc_command function, except for
423 * argc, argv which are passes through.
425 * @param domain_sid The domain sid acquired from the remote server
426 * @param cli A cli_state connected to the server.
427 * @param mem_ctx Talloc context, destoyed on completion of the function.
428 * @param argc Standard main() style argc
429 * @param argv Standard main() style argv. Initial components are already
432 * @return Normal NTSTATUS return.
436 rpc_getsid_internals(const DOM_SID *domain_sid, struct cli_state *cli,
437 TALLOC_CTX *mem_ctx, int argc, const char **argv)
441 sid_to_string(sid_str, domain_sid);
442 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
443 sid_str, lp_workgroup());
445 if (!secrets_store_domain_sid(global_myname(), domain_sid)) {
446 DEBUG(0,("Can't store domain SID\n"));
447 return NT_STATUS_UNSUCCESSFUL;
455 * 'net rpc getsid' entrypoint.
456 * @param argc Standard main() style argc
457 * @param argc Standard main() style argv. Initial components are already
460 int net_rpc_getsid(int argc, const char **argv)
462 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
463 rpc_getsid_internals,
468 /****************************************************************************/
471 * Basic usage function for 'net rpc user'
472 * @param argc Standard main() style argc.
473 * @param argv Standard main() style argv. Initial components are already
477 static int rpc_user_usage(int argc, const char **argv)
479 return net_help_user(argc, argv);
483 * Add a new user to a remote RPC server
485 * All parameters are provided by the run_rpc_command function, except for
486 * argc, argv which are passes through.
488 * @param domain_sid The domain sid acquired from the remote server
489 * @param cli A cli_state connected to the server.
490 * @param mem_ctx Talloc context, destoyed on completion of the function.
491 * @param argc Standard main() style argc
492 * @param argv Standard main() style argv. Initial components are already
495 * @return Normal NTSTATUS return.
498 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
499 int argc, const char **argv) {
501 POLICY_HND connect_pol, domain_pol, user_pol;
502 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
503 const char *acct_name;
505 uint32 unknown, user_rid;
508 d_printf("User must be specified\n");
509 rpc_user_usage(argc, argv);
515 /* Get sam policy handle */
517 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
519 if (!NT_STATUS_IS_OK(result)) {
523 /* Get domain policy handle */
525 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
526 MAXIMUM_ALLOWED_ACCESS,
527 domain_sid, &domain_pol);
528 if (!NT_STATUS_IS_OK(result)) {
532 /* Create domain user */
534 acb_info = ACB_NORMAL;
535 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
537 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
538 acct_name, acb_info, unknown,
539 &user_pol, &user_rid);
540 if (!NT_STATUS_IS_OK(result)) {
545 if (!NT_STATUS_IS_OK(result)) {
546 d_printf("Failed to add user %s - %s\n", acct_name,
549 d_printf("Added user %s\n", acct_name);
555 * Add a new user to a remote RPC server
557 * @param argc Standard main() style argc
558 * @param argv Standard main() style argv. Initial components are already
561 * @return A shell status integer (0 for success)
564 static int rpc_user_add(int argc, const char **argv)
566 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
571 * Delete a user from a remote RPC server
573 * All parameters are provided by the run_rpc_command function, except for
574 * argc, argv which are passes through.
576 * @param domain_sid The domain sid acquired from the remote server
577 * @param cli A cli_state connected to the server.
578 * @param mem_ctx Talloc context, destoyed on completion of the function.
579 * @param argc Standard main() style argc
580 * @param argv Standard main() style argv. Initial components are already
583 * @return Normal NTSTATUS return.
586 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
587 struct cli_state *cli,
589 int argc, const char **argv)
591 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
592 POLICY_HND connect_pol, domain_pol, user_pol;
595 d_printf("User must be specified\n");
596 rpc_user_usage(argc, argv);
599 /* Get sam policy and domain handles */
601 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
604 if (!NT_STATUS_IS_OK(result)) {
608 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
609 MAXIMUM_ALLOWED_ACCESS,
610 domain_sid, &domain_pol);
612 if (!NT_STATUS_IS_OK(result)) {
616 /* Get handle on user */
619 uint32 *user_rids, num_rids, *name_types;
620 uint32 flags = 0x000003e8; /* Unknown */
622 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
624 &num_rids, &user_rids,
627 if (!NT_STATUS_IS_OK(result)) {
631 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
632 MAXIMUM_ALLOWED_ACCESS,
633 user_rids[0], &user_pol);
635 if (!NT_STATUS_IS_OK(result)) {
642 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
644 if (!NT_STATUS_IS_OK(result)) {
648 /* Display results */
656 * Delete a user from a remote RPC server
658 * @param argc Standard main() style argc
659 * @param argv Standard main() style argv. Initial components are already
662 * @return A shell status integer (0 for success)
665 static int rpc_user_delete(int argc, const char **argv)
667 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
672 * List user's groups on a remote RPC server
674 * All parameters are provided by the run_rpc_command function, except for
675 * argc, argv which are passes through.
677 * @param domain_sid The domain sid acquired from the remote server
678 * @param cli A cli_state connected to the server.
679 * @param mem_ctx Talloc context, destoyed on completion of the function.
680 * @param argc Standard main() style argc
681 * @param argv Standard main() style argv. Initial components are already
684 * @return Normal NTSTATUS return.
688 rpc_user_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
689 TALLOC_CTX *mem_ctx, int argc, const char **argv)
691 POLICY_HND connect_pol, domain_pol, user_pol;
692 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
693 uint32 *rids, num_rids, *name_types, num_names;
694 uint32 flags = 0x000003e8; /* Unknown */
700 d_printf("User must be specified\n");
701 rpc_user_usage(argc, argv);
704 /* Get sam policy handle */
706 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
708 if (!NT_STATUS_IS_OK(result)) goto done;
710 /* Get domain policy handle */
712 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
713 MAXIMUM_ALLOWED_ACCESS,
714 domain_sid, &domain_pol);
715 if (!NT_STATUS_IS_OK(result)) goto done;
717 /* Get handle on user */
719 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
721 &num_rids, &rids, &name_types);
723 if (!NT_STATUS_IS_OK(result)) goto done;
725 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
726 MAXIMUM_ALLOWED_ACCESS,
728 if (!NT_STATUS_IS_OK(result)) goto done;
730 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
731 &num_rids, &user_gids);
735 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
737 for (i = 0; i < num_rids; i++)
738 rids[i] = user_gids[i].g_rid;
740 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
741 flags, num_rids, rids,
742 &num_names, &names, &name_types);
744 if (!NT_STATUS_IS_OK(result)) {
748 /* Display results */
750 for (i = 0; i < num_names; i++)
751 printf("%s\n", names[i]);
758 * List a user's groups from a remote RPC server
760 * @param argc Standard main() style argc
761 * @param argv Standard main() style argv. Initial components are already
764 * @return A shell status integer (0 for success)
767 static int rpc_user_info(int argc, const char **argv)
769 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
774 * List users on a remote RPC server
776 * All parameters are provided by the run_rpc_command function, except for
777 * argc, argv which are passes through.
779 * @param domain_sid The domain sid acquired from the remote server
780 * @param cli A cli_state connected to the server.
781 * @param mem_ctx Talloc context, destoyed on completion of the function.
782 * @param argc Standard main() style argc
783 * @param argv Standard main() style argv. Initial components are already
786 * @return Normal NTSTATUS return.
790 rpc_user_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
791 TALLOC_CTX *mem_ctx, int argc, const char **argv)
793 POLICY_HND connect_pol, domain_pol;
794 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
795 uint32 start_idx=0, num_entries, i, loop_count = 0;
796 SAM_DISPINFO_CTR ctr;
797 SAM_DISPINFO_1 info1;
799 /* Get sam policy handle */
801 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
803 if (!NT_STATUS_IS_OK(result)) {
807 /* Get domain policy handle */
809 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
810 MAXIMUM_ALLOWED_ACCESS,
811 domain_sid, &domain_pol);
812 if (!NT_STATUS_IS_OK(result)) {
816 /* Query domain users */
819 ctr.sam.info1 = &info1;
820 if (opt_long_list_entries)
821 d_printf("\nUser name Comment"\
822 "\n-----------------------------\n");
825 uint32 max_entries, max_size;
827 get_query_dispinfo_params(
828 loop_count, &max_entries, &max_size);
830 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
831 &start_idx, 1, &num_entries,
832 max_entries, max_size, &ctr);
835 for (i = 0; i < num_entries; i++) {
836 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
837 if (opt_long_list_entries)
838 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
840 if (opt_long_list_entries)
841 printf("%-21.21s %s\n", user, desc);
843 printf("%s\n", user);
845 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
852 * 'net rpc user' entrypoint.
853 * @param argc Standard main() style argc
854 * @param argc Standard main() style argv. Initial components are already
858 int net_rpc_user(int argc, const char **argv)
860 struct functable func[] = {
861 {"add", rpc_user_add},
862 {"info", rpc_user_info},
863 {"delete", rpc_user_delete},
868 if (opt_long_list_entries) {
871 return run_rpc_command(NULL,PI_SAMR, 0,
872 rpc_user_list_internals,
876 return net_run_function(argc, argv, func, rpc_user_usage);
880 /****************************************************************************/
883 * Basic usage function for 'net rpc group'
884 * @param argc Standard main() style argc.
885 * @param argv Standard main() style argv. Initial components are already
889 static int rpc_group_usage(int argc, const char **argv)
891 return net_help_group(argc, argv);
895 * List groups on a remote RPC server
897 * All parameters are provided by the run_rpc_command function, except for
898 * argc, argv which are passes through.
900 * @param domain_sid The domain sid acquired from the remote server
901 * @param cli A cli_state connected to the server.
902 * @param mem_ctx Talloc context, destoyed on completion of the function.
903 * @param argc Standard main() style argc
904 * @param argv Standard main() style argv. Initial components are already
907 * @return Normal NTSTATUS return.
911 rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
912 TALLOC_CTX *mem_ctx, int argc, const char **argv)
914 POLICY_HND connect_pol, domain_pol;
915 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
916 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
917 struct acct_info *groups;
918 DOM_SID global_sid_Builtin;
920 string_to_sid(&global_sid_Builtin, "S-1-5-32");
922 /* Get sam policy handle */
924 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
926 if (!NT_STATUS_IS_OK(result)) {
930 /* Get domain policy handle */
932 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
933 MAXIMUM_ALLOWED_ACCESS,
934 domain_sid, &domain_pol);
935 if (!NT_STATUS_IS_OK(result)) {
939 /* Query domain groups */
940 if (opt_long_list_entries)
941 d_printf("\nGroup name Comment"\
942 "\n-----------------------------\n");
944 SAM_DISPINFO_CTR ctr;
945 SAM_DISPINFO_3 info3;
950 ctr.sam.info3 = &info3;
952 get_query_dispinfo_params(
953 loop_count, &max_entries, &max_size);
955 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
956 &start_idx, 3, &num_entries,
957 max_entries, max_size, &ctr);
959 for (i = 0; i < num_entries; i++) {
963 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
964 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
966 if (opt_long_list_entries)
967 printf("%-21.21s %-50.50s\n",
970 printf("%-21.21s\n", group);
972 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
973 /* query domain aliases */
976 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
977 &start_idx, max_entries,
978 &groups, &num_entries);
980 for (i = 0; i < num_entries; i++) {
982 char *description = NULL;
984 if (opt_long_list_entries) {
986 POLICY_HND alias_pol;
989 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
994 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
997 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
999 description = unistr2_tdup(mem_ctx,
1000 &ctr.alias.info3.uni_acct_desc);
1004 if (description != NULL) {
1005 printf("%-21.21s %-50.50s\n",
1006 groups[i].acct_name,
1009 printf("%-21.21s\n", groups[i].acct_name);
1012 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1013 cli_samr_close(cli, mem_ctx, &domain_pol);
1014 /* Get builtin policy handle */
1016 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1017 MAXIMUM_ALLOWED_ACCESS,
1018 &global_sid_Builtin, &domain_pol);
1019 if (!NT_STATUS_IS_OK(result)) {
1022 /* query builtin aliases */
1025 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1026 &start_idx, max_entries,
1027 &groups, &num_entries);
1029 for (i = 0; i < num_entries; i++) {
1031 char *description = NULL;
1033 if (opt_long_list_entries) {
1035 POLICY_HND alias_pol;
1038 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1043 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1046 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1048 description = unistr2_tdup(mem_ctx,
1049 &ctr.alias.info3.uni_acct_desc);
1053 if (description != NULL) {
1054 printf("%-21.21s %-50.50s\n",
1055 groups[i].acct_name,
1058 printf("%-21.21s\n", groups[i].acct_name);
1061 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1068 * 'net rpc group' entrypoint.
1069 * @param argc Standard main() style argc
1070 * @param argc Standard main() style argv. Initial components are already
1074 int net_rpc_group(int argc, const char **argv)
1076 struct functable func[] = {
1078 {"add", rpc_group_add},
1079 {"delete", rpc_group_delete},
1085 if (opt_long_list_entries) {
1088 return run_rpc_command(NULL, PI_SAMR, 0,
1089 rpc_group_list_internals,
1093 return net_run_function(argc, argv, func, rpc_group_usage);
1096 /****************************************************************************/
1098 static int rpc_share_usage(int argc, const char **argv)
1100 return net_help_share(argc, argv);
1104 * Add a share on a remote RPC server
1106 * All parameters are provided by the run_rpc_command function, except for
1107 * argc, argv which are passes through.
1109 * @param domain_sid The domain sid acquired from the remote server
1110 * @param cli A cli_state connected to the server.
1111 * @param mem_ctx Talloc context, destoyed on completion of the function.
1112 * @param argc Standard main() style argc
1113 * @param argv Standard main() style argv. Initial components are already
1116 * @return Normal NTSTATUS return.
1119 rpc_share_add_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1120 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1123 char *sharename=talloc_strdup(mem_ctx, argv[0]);
1125 uint32 type=0; /* only allow disk shares to be added */
1126 uint32 num_users=0, perms=0;
1127 char *password=NULL; /* don't allow a share password */
1129 path = strchr(sharename, '=');
1131 return NT_STATUS_UNSUCCESSFUL;
1134 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1135 opt_comment, perms, opt_maxusers,
1136 num_users, path, password);
1137 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1140 static int rpc_share_add(int argc, const char **argv)
1142 if ((argc < 1) || !strchr(argv[0], '=')) {
1143 DEBUG(1,("Sharename or path not specified on add\n"));
1144 return rpc_share_usage(argc, argv);
1146 return run_rpc_command(NULL, PI_SRVSVC, 0,
1147 rpc_share_add_internals,
1152 * Delete a share on a remote RPC server
1154 * All parameters are provided by the run_rpc_command function, except for
1155 * argc, argv which are passes through.
1157 * @param domain_sid The domain sid acquired from the remote server
1158 * @param cli A cli_state connected to the server.
1159 * @param mem_ctx Talloc context, destoyed on completion of the function.
1160 * @param argc Standard main() style argc
1161 * @param argv Standard main() style argv. Initial components are already
1164 * @return Normal NTSTATUS return.
1167 rpc_share_del_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1168 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1172 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1173 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1177 * Delete a share on a remote RPC server
1179 * @param domain_sid The domain sid acquired from the remote server
1180 * @param argc Standard main() style argc
1181 * @param argv Standard main() style argv. Initial components are already
1184 * @return A shell status integer (0 for success)
1186 static int rpc_share_delete(int argc, const char **argv)
1189 DEBUG(1,("Sharename not specified on delete\n"));
1190 return rpc_share_usage(argc, argv);
1192 return run_rpc_command(NULL, PI_SRVSVC, 0,
1193 rpc_share_del_internals,
1198 * Formatted print of share info
1200 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1203 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1205 fstring netname = "", remark = "";
1207 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1208 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1210 if (opt_long_list_entries) {
1211 d_printf("%-12.12s %-8.8s %-50.50s\n",
1212 netname, share_type[info1->info_1.type], remark);
1214 d_printf("%-12.12s\n", netname);
1220 * List shares on a remote RPC server
1222 * All parameters are provided by the run_rpc_command function, except for
1223 * argc, argv which are passes through.
1225 * @param domain_sid The domain sid acquired from the remote server
1226 * @param cli A cli_state connected to the server.
1227 * @param mem_ctx Talloc context, destoyed on completion of the function.
1228 * @param argc Standard main() style argc
1229 * @param argv Standard main() style argv. Initial components are already
1232 * @return Normal NTSTATUS return.
1236 rpc_share_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1237 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1239 SRV_SHARE_INFO_CTR ctr;
1242 uint32 preferred_len = 0xffffffff, i;
1244 init_enum_hnd(&hnd, 0);
1246 result = cli_srvsvc_net_share_enum(
1247 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1249 if (!W_ERROR_IS_OK(result))
1252 /* Display results */
1254 if (opt_long_list_entries) {
1256 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1257 "\nShare name Type Description\n"\
1258 "---------- ---- -----------\n");
1260 for (i = 0; i < ctr.num_entries; i++)
1261 display_share_info_1(&ctr.share.info1[i]);
1263 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1267 * 'net rpc share' entrypoint.
1268 * @param argc Standard main() style argc
1269 * @param argv Standard main() style argv. Initial components are already
1273 int net_rpc_share(int argc, const char **argv)
1275 struct functable func[] = {
1276 {"add", rpc_share_add},
1277 {"delete", rpc_share_delete},
1282 return run_rpc_command(NULL, PI_SRVSVC, 0,
1283 rpc_share_list_internals,
1286 return net_run_function(argc, argv, func, rpc_share_usage);
1289 /****************************************************************************/
1291 static int rpc_file_usage(int argc, const char **argv)
1293 return net_help_file(argc, argv);
1297 * Close a file on a remote RPC server
1299 * All parameters are provided by the run_rpc_command function, except for
1300 * argc, argv which are passes through.
1302 * @param domain_sid The domain sid acquired from the remote server
1303 * @param cli A cli_state connected to the server.
1304 * @param mem_ctx Talloc context, destoyed on completion of the function.
1305 * @param argc Standard main() style argc
1306 * @param argv Standard main() style argv. Initial components are already
1309 * @return Normal NTSTATUS return.
1312 rpc_file_close_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1313 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1316 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1317 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1321 * Close a file on a remote RPC server
1323 * @param argc Standard main() style argc
1324 * @param argv Standard main() style argv. Initial components are already
1327 * @return A shell status integer (0 for success)
1329 static int rpc_file_close(int argc, const char **argv)
1332 DEBUG(1, ("No fileid given on close\n"));
1333 return(rpc_file_usage(argc, argv));
1336 return run_rpc_command(NULL, PI_SRVSVC, 0,
1337 rpc_file_close_internals,
1342 * Formatted print of open file info
1344 * @param info3 FILE_INFO_3 contents
1345 * @param str3 strings for FILE_INFO_3
1348 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1350 fstring user = "", path = "";
1352 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1353 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1355 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1356 info3->id, user, info3->perms, info3->num_locks, path);
1360 * List open files on a remote RPC server
1362 * All parameters are provided by the run_rpc_command function, except for
1363 * argc, argv which are passes through.
1365 * @param domain_sid The domain sid acquired from the remote server
1366 * @param cli A cli_state connected to the server.
1367 * @param mem_ctx Talloc context, destoyed on completion of the function.
1368 * @param argc Standard main() style argc
1369 * @param argv Standard main() style argv. Initial components are already
1372 * @return Normal NTSTATUS return.
1376 rpc_file_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1377 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1379 SRV_FILE_INFO_CTR ctr;
1382 uint32 preferred_len = 0xffffffff, i;
1383 const char *username=NULL;
1385 init_enum_hnd(&hnd, 0);
1387 /* if argc > 0, must be user command */
1389 username = smb_xstrdup(argv[0]);
1391 result = cli_srvsvc_net_file_enum(
1392 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1394 if (!W_ERROR_IS_OK(result))
1397 /* Display results */
1400 "\nEnumerating open files on remote server:\n\n"\
1401 "\nFileId Opened by Perms Locks Path"\
1402 "\n------ --------- ----- ----- ---- \n");
1403 for (i = 0; i < ctr.num_entries; i++)
1404 display_file_info_3(&ctr.file.info3[i].info_3,
1405 &ctr.file.info3[i].info_3_str);
1407 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1412 * List files for a user on a remote RPC server
1414 * @param argc Standard main() style argc
1415 * @param argv Standard main() style argv. Initial components are already
1418 * @return A shell status integer (0 for success)
1420 static int rpc_file_user(int argc, const char **argv)
1423 DEBUG(1, ("No username given\n"));
1424 return(rpc_file_usage(argc, argv));
1427 return run_rpc_command(NULL, PI_SRVSVC, 0,
1428 rpc_file_list_internals,
1434 * 'net rpc file' entrypoint.
1435 * @param argc Standard main() style argc
1436 * @param argv Standard main() style argv. Initial components are already
1440 int net_rpc_file(int argc, const char **argv)
1442 struct functable func[] = {
1443 {"close", rpc_file_close},
1444 {"user", rpc_file_user},
1446 {"info", rpc_file_info},
1452 return run_rpc_command(NULL, PI_SRVSVC, 0,
1453 rpc_file_list_internals,
1456 return net_run_function(argc, argv, func, rpc_file_usage);
1459 /****************************************************************************/
1464 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
1466 * All parameters are provided by the run_rpc_command function, except for
1467 * argc, argv which are passed through.
1469 * @param domain_sid The domain sid aquired from the remote server
1470 * @param cli A cli_state connected to the server.
1471 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1472 * @param argc Standard main() style argc
1473 * @param argv Standard main() style argv. Initial components are already
1476 * @return Normal NTSTATUS return.
1479 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
1480 struct cli_state *cli,
1481 TALLOC_CTX *mem_ctx,
1482 int argc, const char **argv)
1484 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1486 result = cli_shutdown_abort(cli, mem_ctx);
1488 if (NT_STATUS_IS_OK(result))
1489 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1491 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1498 * ABORT the shutdown of a remote RPC Server, over winreg pipe
1500 * All parameters are provided by the run_rpc_command function, except for
1501 * argc, argv which are passed through.
1503 * @param domain_sid The domain sid aquired from the remote server
1504 * @param cli A cli_state connected to the server.
1505 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1506 * @param argc Standard main() style argc
1507 * @param argv Standard main() style argv. Initial components are already
1510 * @return Normal NTSTATUS return.
1513 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
1514 struct cli_state *cli,
1515 TALLOC_CTX *mem_ctx,
1516 int argc, const char **argv)
1518 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1520 result = cli_reg_abort_shutdown(cli, mem_ctx);
1522 if (NT_STATUS_IS_OK(result))
1523 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1525 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1531 * ABORT the Shut down of a remote RPC server
1533 * @param argc Standard main() style argc
1534 * @param argv Standard main() style argv. Initial components are already
1537 * @return A shell status integer (0 for success)
1540 static int rpc_shutdown_abort(int argc, const char **argv)
1542 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
1543 rpc_shutdown_abort_internals,
1549 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
1551 return run_rpc_command(NULL, PI_WINREG, 0,
1552 rpc_reg_shutdown_abort_internals,
1557 * Shut down a remote RPC Server
1559 * All parameters are provided by the run_rpc_command function, except for
1560 * argc, argv which are passes through.
1562 * @param domain_sid The domain sid aquired from the remote server
1563 * @param cli A cli_state connected to the server.
1564 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1565 * @param argc Standard main() style argc
1566 * @param argc Standard main() style argv. Initial components are already
1569 * @return Normal NTSTATUS return.
1572 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1573 int argc, const char **argv)
1575 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1576 const char *msg = "This machine will be shutdown shortly";
1577 uint32 timeout = 20;
1582 struct poptOption long_options[] = {
1583 {"message", 'm', POPT_ARG_STRING, &msg},
1584 {"timeout", 't', POPT_ARG_INT, &timeout},
1585 {"reboot", 'r', POPT_ARG_NONE, &reboot},
1586 {"force", 'f', POPT_ARG_NONE, &force},
1590 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1591 POPT_CONTEXT_KEEP_FIRST);
1593 rc = poptGetNextOpt(pc);
1596 /* an error occurred during option processing */
1597 DEBUG(0, ("%s: %s\n",
1598 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1600 return NT_STATUS_INVALID_PARAMETER;
1607 timeout = opt_timeout;
1610 /* create an entry */
1611 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
1613 if (NT_STATUS_IS_OK(result))
1614 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1616 DEBUG(0,("Shutdown of remote machine failed!\n"));
1622 * Shut down a remote RPC server
1624 * @param argc Standard main() style argc
1625 * @param argc Standard main() style argv. Initial components are already
1628 * @return A shell status integer (0 for success)
1631 static int rpc_shutdown(int argc, const char **argv)
1633 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
1637 /***************************************************************************
1638 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1640 ***************************************************************************/
1643 * Add interdomain trust account to the RPC server.
1644 * All parameters (except for argc and argv) are passed by run_rpc_command
1647 * @param domain_sid The domain sid acquired from the server
1648 * @param cli A cli_state connected to the server.
1649 * @param mem_ctx Talloc context, destoyed on completion of the function.
1650 * @param argc Standard main() style argc
1651 * @param argc Standard main() style argv. Initial components are already
1654 * @return normal NTSTATUS return code
1657 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1658 int argc, const char **argv) {
1660 POLICY_HND connect_pol, domain_pol, user_pol;
1661 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1664 uint32 unknown, user_rid;
1667 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
1668 return NT_STATUS_INVALID_PARAMETER;
1672 * Make valid trusting domain account (ie. uppercased and with '$' appended)
1675 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1676 return NT_STATUS_NO_MEMORY;
1679 strupper_m(acct_name);
1681 /* Get samr policy handle */
1682 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1684 if (!NT_STATUS_IS_OK(result)) {
1688 /* Get domain policy handle */
1689 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1690 MAXIMUM_ALLOWED_ACCESS,
1691 domain_sid, &domain_pol);
1692 if (!NT_STATUS_IS_OK(result)) {
1696 /* Create trusting domain's account */
1697 acb_info = ACB_DOMTRUST;
1698 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
1699 mimir: yes, most probably it is */
1701 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1702 acct_name, acb_info, unknown,
1703 &user_pol, &user_rid);
1704 if (!NT_STATUS_IS_OK(result)) {
1709 SAM_USERINFO_CTR ctr;
1710 SAM_USER_INFO_24 p24;
1711 fstring ucs2_trust_password;
1715 ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password, argv[1],
1716 sizeof(ucs2_trust_password), 0);
1718 encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
1724 init_sam_user_info24(&p24, (char *)pwbuf, 24);
1726 ctr.switch_value = 24;
1727 ctr.info.id24 = &p24;
1729 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
1730 cli->user_session_key, &ctr);
1732 if (!NT_STATUS_IS_OK(result)) {
1733 DEBUG(0,("Could not set trust account password: %s\n",
1734 nt_errstr(result)));
1740 SAFE_FREE(acct_name);
1745 * Create interdomain trust account for a remote domain.
1747 * @param argc standard argc
1748 * @param argv standard argv without initial components
1750 * @return Integer status (0 means success)
1753 static int rpc_trustdom_add(int argc, const char **argv)
1756 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
1759 d_printf("Usage: net rpc trustdom add <domain>\n");
1766 * Delete interdomain trust account for a remote domain.
1768 * @param argc standard argc
1769 * @param argv standard argv without initial components
1771 * @return Integer status (0 means success)
1774 static int rpc_trustdom_del(int argc, const char **argv)
1776 d_printf("Sorry, not yet implemented.\n");
1777 d_printf("Use 'smbpasswd -x -i' instead.\n");
1783 * Establish trust relationship to a trusting domain.
1784 * Interdomain account must already be created on remote PDC.
1786 * @param argc standard argc
1787 * @param argv standard argv without initial components
1789 * @return Integer status (0 means success)
1792 static int rpc_trustdom_establish(int argc, const char **argv)
1794 struct cli_state *cli;
1795 struct in_addr server_ip;
1796 POLICY_HND connect_hnd;
1797 TALLOC_CTX *mem_ctx;
1800 WKS_INFO_100 wks_info;
1807 * Connect to \\server\ipc$ as 'our domain' account with password
1811 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
1815 domain_name = smb_xstrdup(argv[0]);
1816 strupper_m(domain_name);
1818 /* account name used at first is our domain's name with '$' */
1819 asprintf(&acct_name, "%s$", lp_workgroup());
1820 strupper_m(acct_name);
1823 * opt_workgroup will be used by connection functions further,
1824 * hence it should be set to remote domain name instead of ours
1826 if (opt_workgroup) {
1827 opt_workgroup = smb_xstrdup(domain_name);
1830 opt_user_name = acct_name;
1832 /* find the domain controller */
1833 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
1834 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
1838 /* connect to ipc$ as username/password */
1839 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
1840 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1842 /* Is it trusting domain account for sure ? */
1843 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
1844 nt_errstr(nt_status)));
1849 * Connect to \\server\ipc$ again (this time anonymously)
1852 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
1854 if (NT_STATUS_IS_ERR(nt_status)) {
1855 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
1856 domain_name, nt_errstr(nt_status)));
1860 * Use NetServerEnum2 to make sure we're talking to a proper server
1863 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
1864 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
1865 for domain %s\n", domain_name));
1869 * Call WksQueryInfo to check remote server's capabilities
1870 * note: It is now used only to get unicode domain name
1873 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
1874 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
1878 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
1880 DEBUG(0, ("talloc_init() failed\n"));
1885 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
1887 if (NT_STATUS_IS_ERR(nt_status)) {
1888 DEBUG(0, ("WksQueryInfo call failed.\n"));
1892 if (cli->nt_pipe_fnum)
1893 cli_nt_session_close(cli);
1897 * Call LsaOpenPolicy and LsaQueryInfo
1900 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
1901 DEBUG(0, ("talloc_init() failed\n"));
1906 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1907 DEBUG(0, ("Could not initialise lsa pipe\n"));
1912 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1914 if (NT_STATUS_IS_ERR(nt_status)) {
1915 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1916 nt_errstr(nt_status)));
1920 /* Querying info level 5 */
1922 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1923 5 /* info level */, domain_name,
1925 if (NT_STATUS_IS_ERR(nt_status)) {
1926 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1927 nt_errstr(nt_status)));
1934 /* There should be actually query info level 3 (following nt serv behaviour),
1935 but I still don't know if it's _really_ necessary */
1938 * Store the password in secrets db
1941 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
1942 wks_info.uni_lan_grp.uni_str_len, opt_password,
1944 DEBUG(0, ("Storing password for trusted domain failed.\n"));
1949 * Close the pipes and clean up
1952 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1953 if (NT_STATUS_IS_ERR(nt_status)) {
1954 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
1955 nt_errstr(nt_status)));
1959 if (cli->nt_pipe_fnum)
1960 cli_nt_session_close(cli);
1962 talloc_destroy(mem_ctx);
1964 DEBUG(0, ("Success!\n"));
1969 * Revoke trust relationship to the remote domain
1971 * @param argc standard argc
1972 * @param argv standard argv without initial components
1974 * @return Integer status (0 means success)
1977 static int rpc_trustdom_revoke(int argc, const char **argv)
1981 if (argc < 1) return -1;
1983 /* generate upper cased domain name */
1984 domain_name = smb_xstrdup(argv[0]);
1985 strupper_m(domain_name);
1987 /* delete password of the trust */
1988 if (!trusted_domain_password_delete(domain_name)) {
1989 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
1998 * Usage for 'net rpc trustdom' command
2000 * @param argc standard argc
2001 * @param argv standard argv without inital components
2003 * @return Integer status returned to shell
2006 static int rpc_trustdom_usage(int argc, const char **argv)
2008 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
2009 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
2010 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
2011 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2012 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
2017 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
2018 int argc, const char **argv)
2021 sid_to_string(str_sid, domain_sid);
2022 d_printf("%s\n", str_sid);
2023 return NT_STATUS_OK;
2027 static int rpc_trustdom_list(int argc, const char **argv)
2029 /* common variables */
2030 TALLOC_CTX* mem_ctx;
2031 struct cli_state *cli, *remote_cli;
2033 const char *domain_name = NULL;
2034 DOM_SID queried_dom_sid;
2035 fstring ascii_sid, padding;
2036 int ascii_dom_name_len;
2037 POLICY_HND connect_hnd;
2039 /* trusted domains listing variables */
2040 unsigned int num_domains, enum_ctx = 0;
2041 int i, pad_len, col_len = 20;
2042 DOM_SID *domain_sids;
2043 char **trusted_dom_names;
2044 fstring pdc_name, dummy;
2046 /* trusting domains listing variables */
2047 POLICY_HND domain_hnd;
2048 char **trusting_dom_names;
2049 uint32 *trusting_dom_rids;
2052 * Listing trusted domains (stored in secrets.tdb, if local)
2055 mem_ctx = talloc_init("trust relationships listing");
2058 * set domain and pdc name to local samba server (default)
2059 * or to remote one given in command line
2062 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2063 domain_name = opt_workgroup;
2064 opt_target_workgroup = opt_workgroup;
2066 fstrcpy(pdc_name, global_myname());
2067 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2068 opt_target_workgroup = domain_name;
2071 /* open \PIPE\lsarpc and open policy handle */
2072 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2073 DEBUG(0, ("Couldn't connect to domain controller\n"));
2077 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2078 DEBUG(0, ("Could not initialise lsa pipe\n"));
2082 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2084 if (NT_STATUS_IS_ERR(nt_status)) {
2085 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2086 nt_errstr(nt_status)));
2090 /* query info level 5 to obtain sid of a domain being queried */
2091 nt_status = cli_lsa_query_info_policy(
2092 cli, mem_ctx, &connect_hnd, 5 /* info level */,
2093 dummy, &queried_dom_sid);
2095 if (NT_STATUS_IS_ERR(nt_status)) {
2096 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2097 nt_errstr(nt_status)));
2102 * Keep calling LsaEnumTrustdom over opened pipe until
2103 * the end of enumeration is reached
2106 d_printf("Trusted domains list:\n\n");
2109 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2111 &trusted_dom_names, &domain_sids);
2113 if (NT_STATUS_IS_ERR(nt_status)) {
2114 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2115 nt_errstr(nt_status)));
2119 for (i = 0; i < num_domains; i++) {
2120 /* convert sid into ascii string */
2121 sid_to_string(ascii_sid, &(domain_sids[i]));
2123 /* calculate padding space for d_printf to look nicer */
2124 pad_len = col_len - strlen(trusted_dom_names[i]);
2125 padding[pad_len] = 0;
2126 do padding[--pad_len] = ' '; while (pad_len);
2128 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2132 * in case of no trusted domains say something rather
2133 * than just display blank line
2135 if (!num_domains) d_printf("none\n");
2137 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2139 /* close this connection before doing next one */
2140 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2141 if (NT_STATUS_IS_ERR(nt_status)) {
2142 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2143 nt_errstr(nt_status)));
2147 cli_nt_session_close(cli);
2150 * Listing trusting domains (stored in passdb backend, if local)
2153 d_printf("\nTrusting domains list:\n\n");
2156 * Open \PIPE\samr and get needed policy handles
2158 if (!cli_nt_session_open(cli, PI_SAMR)) {
2159 DEBUG(0, ("Could not initialise samr pipe\n"));
2164 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2166 if (!NT_STATUS_IS_OK(nt_status)) {
2167 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2168 nt_errstr(nt_status)));
2172 /* SamrOpenDomain - we have to open domain policy handle in order to be
2173 able to enumerate accounts*/
2174 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2175 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2176 &queried_dom_sid, &domain_hnd);
2177 if (!NT_STATUS_IS_OK(nt_status)) {
2178 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2179 nt_errstr(nt_status)));
2184 * perform actual enumeration
2187 enum_ctx = 0; /* reset enumeration context from last enumeration */
2190 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2191 &enum_ctx, ACB_DOMTRUST, 0xffff,
2192 &trusting_dom_names, &trusting_dom_rids,
2194 if (NT_STATUS_IS_ERR(nt_status)) {
2195 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2196 nt_errstr(nt_status)));
2200 for (i = 0; i < num_domains; i++) {
2203 * get each single domain's sid (do we _really_ need this ?):
2204 * 1) connect to domain's pdc
2205 * 2) query the pdc for domain's sid
2208 /* get rid of '$' tail */
2209 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2210 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2211 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2213 /* calculate padding space for d_printf to look nicer */
2214 pad_len = col_len - strlen(trusting_dom_names[i]);
2215 padding[pad_len] = 0;
2216 do padding[--pad_len] = ' '; while (pad_len);
2218 /* set opt_* variables to remote domain */
2219 strupper_m(trusting_dom_names[i]);
2220 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2221 opt_target_workgroup = opt_workgroup;
2223 d_printf("%s%s", trusting_dom_names[i], padding);
2225 /* connect to remote domain controller */
2226 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2228 /* query for domain's sid */
2229 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2230 d_printf("couldn't get domain's sid\n");
2232 cli_shutdown(remote_cli);
2235 d_printf("domain controller is not responding\n");
2239 if (!num_domains) d_printf("none\n");
2241 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2243 /* close opened samr and domain policy handles */
2244 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2245 if (!NT_STATUS_IS_OK(nt_status)) {
2246 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2249 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2250 if (!NT_STATUS_IS_OK(nt_status)) {
2251 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2254 /* close samr pipe and connection to IPC$ */
2255 cli_nt_session_close(cli);
2258 talloc_destroy(mem_ctx);
2263 * Entrypoint for 'net rpc trustdom' code
2265 * @param argc standard argc
2266 * @param argv standard argv without initial components
2268 * @return Integer status (0 means success)
2271 static int rpc_trustdom(int argc, const char **argv)
2273 struct functable func[] = {
2274 {"add", rpc_trustdom_add},
2275 {"del", rpc_trustdom_del},
2276 {"establish", rpc_trustdom_establish},
2277 {"revoke", rpc_trustdom_revoke},
2278 {"help", rpc_trustdom_usage},
2279 {"list", rpc_trustdom_list},
2284 rpc_trustdom_usage(argc, argv);
2288 return (net_run_function(argc, argv, func, rpc_user_usage));
2292 * Check if a server will take rpc commands
2293 * @param flags Type of server to connect to (PDC, DMB, localhost)
2294 * if the host is not explicitly specified
2295 * @return BOOL (true means rpc supported)
2297 BOOL net_rpc_check(unsigned flags)
2299 struct cli_state cli;
2301 struct in_addr server_ip;
2302 char *server_name = NULL;
2304 /* flags (i.e. server type) may depend on command */
2305 if (!net_find_server(flags, &server_ip, &server_name))
2309 if (cli_initialise(&cli) == False)
2312 if (!cli_connect(&cli, server_name, &server_ip))
2314 if (!attempt_netbios_session_request(&cli, global_myname(),
2315 server_name, &server_ip))
2317 if (!cli_negprot(&cli))
2319 if (cli.protocol < PROTOCOL_NT1)
2329 /****************************************************************************/
2333 * Basic usage function for 'net rpc'
2334 * @param argc Standard main() style argc
2335 * @param argv Standard main() style argv. Initial components are already
2339 int net_rpc_usage(int argc, const char **argv)
2341 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2342 d_printf(" net rpc join \t\t\tto join a domain \n");
2343 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2344 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2345 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2346 d_printf(" net rpc group \t\tto list groups\n");
2347 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2348 d_printf(" net rpc file \t\t\tto list open files\n");
2349 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2350 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2351 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2352 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2353 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2354 "\t\t\t\t\tor establish trust\n");
2355 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2356 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2358 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2359 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2360 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2361 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2362 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2368 * Help function for 'net rpc'. Calls command specific help if requested
2369 * or displays usage of net rpc
2370 * @param argc Standard main() style argc
2371 * @param argv Standard main() style argv. Initial components are already
2375 int net_rpc_help(int argc, const char **argv)
2377 struct functable func[] = {
2378 {"join", rpc_join_usage},
2379 {"user", rpc_user_usage},
2380 {"group", rpc_group_usage},
2381 {"share", rpc_share_usage},
2382 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2383 {"trustdom", rpc_trustdom_usage},
2384 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2385 /*{"shutdown", rpc_shutdown_usage}, */
2390 net_rpc_usage(argc, argv);
2394 return (net_run_function(argc, argv, func, rpc_user_usage));
2399 * 'net rpc' entrypoint.
2400 * @param argc Standard main() style argc
2401 * @param argv Standard main() style argv. Initial components are already
2405 int net_rpc(int argc, const char **argv)
2407 struct functable func[] = {
2408 {"info", net_rpc_info},
2409 {"join", net_rpc_join},
2410 {"oldjoin", net_rpc_oldjoin},
2411 {"testjoin", net_rpc_testjoin},
2412 {"user", net_rpc_user},
2413 {"group", net_rpc_group},
2414 {"share", net_rpc_share},
2415 {"file", net_rpc_file},
2416 {"changetrustpw", net_rpc_changetrustpw},
2417 {"trustdom", rpc_trustdom},
2418 {"abortshutdown", rpc_shutdown_abort},
2419 {"shutdown", rpc_shutdown},
2420 {"samdump", rpc_samdump},
2421 {"vampire", rpc_vampire},
2422 {"getsid", net_rpc_getsid},
2423 {"help", net_rpc_help},
2426 return net_run_function(argc, argv, func, net_rpc_usage);