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
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, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1480 int argc, const char **argv)
1482 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1484 result = cli_reg_abort_shutdown(cli, mem_ctx);
1486 if (NT_STATUS_IS_OK(result))
1487 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1489 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1496 * ABORT the Shut down of a remote RPC server
1498 * @param argc Standard main() style argc
1499 * @param argv Standard main() style argv. Initial components are already
1502 * @return A shell status integer (0 for success)
1505 static int rpc_shutdown_abort(int argc, const char **argv)
1507 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_abort_internals,
1512 * Shut down a remote RPC Server
1514 * All parameters are provided by the run_rpc_command function, except for
1515 * argc, argv which are passes through.
1517 * @param domain_sid The domain sid aquired from the remote server
1518 * @param cli A cli_state connected to the server.
1519 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1520 * @param argc Standard main() style argc
1521 * @param argc Standard main() style argv. Initial components are already
1524 * @return Normal NTSTATUS return.
1527 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1528 int argc, const char **argv)
1530 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1531 const char *msg = "This machine will be shutdown shortly";
1532 uint32 timeout = 20;
1537 struct poptOption long_options[] = {
1538 {"message", 'm', POPT_ARG_STRING, &msg},
1539 {"timeout", 't', POPT_ARG_INT, &timeout},
1540 {"reboot", 'r', POPT_ARG_NONE, &reboot},
1541 {"force", 'f', POPT_ARG_NONE, &force},
1545 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1546 POPT_CONTEXT_KEEP_FIRST);
1548 rc = poptGetNextOpt(pc);
1551 /* an error occurred during option processing */
1552 DEBUG(0, ("%s: %s\n",
1553 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1555 return NT_STATUS_INVALID_PARAMETER;
1562 timeout = opt_timeout;
1565 /* create an entry */
1566 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
1568 if (NT_STATUS_IS_OK(result))
1569 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1571 DEBUG(0,("Shutdown of remote machine failed!\n"));
1577 * Shut down a remote RPC server
1579 * @param argc Standard main() style argc
1580 * @param argc Standard main() style argv. Initial components are already
1583 * @return A shell status integer (0 for success)
1586 static int rpc_shutdown(int argc, const char **argv)
1588 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
1592 /***************************************************************************
1593 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1595 ***************************************************************************/
1598 * Add interdomain trust account to the RPC server.
1599 * All parameters (except for argc and argv) are passed by run_rpc_command
1602 * @param domain_sid The domain sid acquired from the server
1603 * @param cli A cli_state connected to the server.
1604 * @param mem_ctx Talloc context, destoyed on completion of the function.
1605 * @param argc Standard main() style argc
1606 * @param argc Standard main() style argv. Initial components are already
1609 * @return normal NTSTATUS return code
1612 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1613 int argc, const char **argv) {
1615 POLICY_HND connect_pol, domain_pol, user_pol;
1616 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1619 uint32 unknown, user_rid;
1622 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
1623 return NT_STATUS_INVALID_PARAMETER;
1627 * Make valid trusting domain account (ie. uppercased and with '$' appended)
1630 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1631 return NT_STATUS_NO_MEMORY;
1634 strupper_m(acct_name);
1636 /* Get samr policy handle */
1637 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1639 if (!NT_STATUS_IS_OK(result)) {
1643 /* Get domain policy handle */
1644 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1645 MAXIMUM_ALLOWED_ACCESS,
1646 domain_sid, &domain_pol);
1647 if (!NT_STATUS_IS_OK(result)) {
1651 /* Create trusting domain's account */
1652 acb_info = ACB_DOMTRUST;
1653 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
1654 mimir: yes, most probably it is */
1656 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1657 acct_name, acb_info, unknown,
1658 &user_pol, &user_rid);
1659 if (!NT_STATUS_IS_OK(result)) {
1664 SAM_USERINFO_CTR ctr;
1665 SAM_USER_INFO_24 p24;
1666 fstring ucs2_trust_password;
1670 ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password, argv[1],
1671 sizeof(ucs2_trust_password), 0);
1673 encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
1679 init_sam_user_info24(&p24, (char *)pwbuf, 24);
1681 ctr.switch_value = 24;
1682 ctr.info.id24 = &p24;
1684 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
1685 cli->user_session_key, &ctr);
1687 if (!NT_STATUS_IS_OK(result)) {
1688 DEBUG(0,("Could not set trust account password: %s\n",
1689 nt_errstr(result)));
1695 SAFE_FREE(acct_name);
1700 * Create interdomain trust account for a remote domain.
1702 * @param argc standard argc
1703 * @param argv standard argv without initial components
1705 * @return Integer status (0 means success)
1708 static int rpc_trustdom_add(int argc, const char **argv)
1711 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
1714 d_printf("Usage: net rpc trustdom add <domain>\n");
1721 * Delete interdomain trust account for a remote domain.
1723 * @param argc standard argc
1724 * @param argv standard argv without initial components
1726 * @return Integer status (0 means success)
1729 static int rpc_trustdom_del(int argc, const char **argv)
1731 d_printf("Sorry, not yet implemented.\n");
1732 d_printf("Use 'smbpasswd -x -i' instead.\n");
1738 * Establish trust relationship to a trusting domain.
1739 * Interdomain account must already be created on remote PDC.
1741 * @param argc standard argc
1742 * @param argv standard argv without initial components
1744 * @return Integer status (0 means success)
1747 static int rpc_trustdom_establish(int argc, const char **argv)
1749 struct cli_state *cli;
1750 struct in_addr server_ip;
1751 POLICY_HND connect_hnd;
1752 TALLOC_CTX *mem_ctx;
1755 WKS_INFO_100 wks_info;
1762 * Connect to \\server\ipc$ as 'our domain' account with password
1766 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
1770 domain_name = smb_xstrdup(argv[0]);
1771 strupper_m(domain_name);
1773 /* account name used at first is our domain's name with '$' */
1774 asprintf(&acct_name, "%s$", lp_workgroup());
1775 strupper_m(acct_name);
1778 * opt_workgroup will be used by connection functions further,
1779 * hence it should be set to remote domain name instead of ours
1781 if (opt_workgroup) {
1782 opt_workgroup = smb_xstrdup(domain_name);
1785 opt_user_name = acct_name;
1787 /* find the domain controller */
1788 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
1789 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
1793 /* connect to ipc$ as username/password */
1794 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
1795 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1797 /* Is it trusting domain account for sure ? */
1798 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
1799 nt_errstr(nt_status)));
1804 * Connect to \\server\ipc$ again (this time anonymously)
1807 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
1809 if (NT_STATUS_IS_ERR(nt_status)) {
1810 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
1811 domain_name, nt_errstr(nt_status)));
1815 * Use NetServerEnum2 to make sure we're talking to a proper server
1818 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
1819 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
1820 for domain %s\n", domain_name));
1824 * Call WksQueryInfo to check remote server's capabilities
1825 * note: It is now used only to get unicode domain name
1828 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
1829 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
1833 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
1835 DEBUG(0, ("talloc_init() failed\n"));
1840 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
1842 if (NT_STATUS_IS_ERR(nt_status)) {
1843 DEBUG(0, ("WksQueryInfo call failed.\n"));
1847 if (cli->nt_pipe_fnum)
1848 cli_nt_session_close(cli);
1852 * Call LsaOpenPolicy and LsaQueryInfo
1855 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
1856 DEBUG(0, ("talloc_init() failed\n"));
1861 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1862 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 /* Querying info level 5 */
1877 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1878 5 /* info level */, domain_name,
1880 if (NT_STATUS_IS_ERR(nt_status)) {
1881 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1882 nt_errstr(nt_status)));
1889 /* There should be actually query info level 3 (following nt serv behaviour),
1890 but I still don't know if it's _really_ necessary */
1893 * Store the password in secrets db
1896 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
1897 wks_info.uni_lan_grp.uni_str_len, opt_password,
1899 DEBUG(0, ("Storing password for trusted domain failed.\n"));
1904 * Close the pipes and clean up
1907 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1908 if (NT_STATUS_IS_ERR(nt_status)) {
1909 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
1910 nt_errstr(nt_status)));
1914 if (cli->nt_pipe_fnum)
1915 cli_nt_session_close(cli);
1917 talloc_destroy(mem_ctx);
1919 DEBUG(0, ("Success!\n"));
1924 * Revoke trust relationship to the remote domain
1926 * @param argc standard argc
1927 * @param argv standard argv without initial components
1929 * @return Integer status (0 means success)
1932 static int rpc_trustdom_revoke(int argc, const char **argv)
1936 if (argc < 1) return -1;
1938 /* generate upper cased domain name */
1939 domain_name = smb_xstrdup(argv[0]);
1940 strupper_m(domain_name);
1942 /* delete password of the trust */
1943 if (!trusted_domain_password_delete(domain_name)) {
1944 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
1953 * Usage for 'net rpc trustdom' command
1955 * @param argc standard argc
1956 * @param argv standard argv without inital components
1958 * @return Integer status returned to shell
1961 static int rpc_trustdom_usage(int argc, const char **argv)
1963 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
1964 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
1965 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
1966 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
1967 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
1972 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1973 int argc, const char **argv)
1976 sid_to_string(str_sid, domain_sid);
1977 d_printf("%s\n", str_sid);
1978 return NT_STATUS_OK;
1982 static int rpc_trustdom_list(int argc, const char **argv)
1984 /* common variables */
1985 TALLOC_CTX* mem_ctx;
1986 struct cli_state *cli, *remote_cli;
1988 const char *domain_name = NULL;
1989 DOM_SID queried_dom_sid;
1990 fstring ascii_sid, padding;
1991 int ascii_dom_name_len;
1992 POLICY_HND connect_hnd;
1994 /* trusted domains listing variables */
1995 unsigned int num_domains, enum_ctx = 0;
1996 int i, pad_len, col_len = 20;
1997 DOM_SID *domain_sids;
1998 char **trusted_dom_names;
1999 fstring pdc_name, dummy;
2001 /* trusting domains listing variables */
2002 POLICY_HND domain_hnd;
2003 char **trusting_dom_names;
2004 uint32 *trusting_dom_rids;
2007 * Listing trusted domains (stored in secrets.tdb, if local)
2010 mem_ctx = talloc_init("trust relationships listing");
2013 * set domain and pdc name to local samba server (default)
2014 * or to remote one given in command line
2017 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2018 domain_name = opt_workgroup;
2019 opt_target_workgroup = opt_workgroup;
2021 fstrcpy(pdc_name, global_myname());
2022 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2023 opt_target_workgroup = domain_name;
2026 /* open \PIPE\lsarpc and open policy handle */
2027 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2028 DEBUG(0, ("Couldn't connect to domain controller\n"));
2032 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2033 DEBUG(0, ("Could not initialise lsa pipe\n"));
2037 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2039 if (NT_STATUS_IS_ERR(nt_status)) {
2040 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2041 nt_errstr(nt_status)));
2045 /* query info level 5 to obtain sid of a domain being queried */
2046 nt_status = cli_lsa_query_info_policy(
2047 cli, mem_ctx, &connect_hnd, 5 /* info level */,
2048 dummy, &queried_dom_sid);
2050 if (NT_STATUS_IS_ERR(nt_status)) {
2051 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2052 nt_errstr(nt_status)));
2057 * Keep calling LsaEnumTrustdom over opened pipe until
2058 * the end of enumeration is reached
2061 d_printf("Trusted domains list:\n\n");
2064 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2066 &trusted_dom_names, &domain_sids);
2068 if (NT_STATUS_IS_ERR(nt_status)) {
2069 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2070 nt_errstr(nt_status)));
2074 for (i = 0; i < num_domains; i++) {
2075 /* convert sid into ascii string */
2076 sid_to_string(ascii_sid, &(domain_sids[i]));
2078 /* calculate padding space for d_printf to look nicer */
2079 pad_len = col_len - strlen(trusted_dom_names[i]);
2080 padding[pad_len] = 0;
2081 do padding[--pad_len] = ' '; while (pad_len);
2083 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2087 * in case of no trusted domains say something rather
2088 * than just display blank line
2090 if (!num_domains) d_printf("none\n");
2092 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2094 /* close this connection before doing next one */
2095 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2096 if (NT_STATUS_IS_ERR(nt_status)) {
2097 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2098 nt_errstr(nt_status)));
2102 cli_nt_session_close(cli);
2105 * Listing trusting domains (stored in passdb backend, if local)
2108 d_printf("\nTrusting domains list:\n\n");
2111 * Open \PIPE\samr and get needed policy handles
2113 if (!cli_nt_session_open(cli, PI_SAMR)) {
2114 DEBUG(0, ("Could not initialise samr pipe\n"));
2119 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2121 if (!NT_STATUS_IS_OK(nt_status)) {
2122 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2123 nt_errstr(nt_status)));
2127 /* SamrOpenDomain - we have to open domain policy handle in order to be
2128 able to enumerate accounts*/
2129 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2130 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2131 &queried_dom_sid, &domain_hnd);
2132 if (!NT_STATUS_IS_OK(nt_status)) {
2133 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2134 nt_errstr(nt_status)));
2139 * perform actual enumeration
2142 enum_ctx = 0; /* reset enumeration context from last enumeration */
2145 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2146 &enum_ctx, ACB_DOMTRUST, 0xffff,
2147 &trusting_dom_names, &trusting_dom_rids,
2149 if (NT_STATUS_IS_ERR(nt_status)) {
2150 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2151 nt_errstr(nt_status)));
2155 for (i = 0; i < num_domains; i++) {
2158 * get each single domain's sid (do we _really_ need this ?):
2159 * 1) connect to domain's pdc
2160 * 2) query the pdc for domain's sid
2163 /* get rid of '$' tail */
2164 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2165 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2166 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2168 /* calculate padding space for d_printf to look nicer */
2169 pad_len = col_len - strlen(trusting_dom_names[i]);
2170 padding[pad_len] = 0;
2171 do padding[--pad_len] = ' '; while (pad_len);
2173 /* set opt_* variables to remote domain */
2174 strupper_m(trusting_dom_names[i]);
2175 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2176 opt_target_workgroup = opt_workgroup;
2178 d_printf("%s%s", trusting_dom_names[i], padding);
2180 /* connect to remote domain controller */
2181 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2183 /* query for domain's sid */
2184 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2185 d_printf("couldn't get domain's sid\n");
2187 cli_shutdown(remote_cli);
2190 d_printf("domain controller is not responding\n");
2194 if (!num_domains) d_printf("none\n");
2196 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2198 /* close opened samr and domain policy handles */
2199 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2200 if (!NT_STATUS_IS_OK(nt_status)) {
2201 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2204 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2205 if (!NT_STATUS_IS_OK(nt_status)) {
2206 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2209 /* close samr pipe and connection to IPC$ */
2210 cli_nt_session_close(cli);
2213 talloc_destroy(mem_ctx);
2218 * Entrypoint for 'net rpc trustdom' code
2220 * @param argc standard argc
2221 * @param argv standard argv without initial components
2223 * @return Integer status (0 means success)
2226 static int rpc_trustdom(int argc, const char **argv)
2228 struct functable func[] = {
2229 {"add", rpc_trustdom_add},
2230 {"del", rpc_trustdom_del},
2231 {"establish", rpc_trustdom_establish},
2232 {"revoke", rpc_trustdom_revoke},
2233 {"help", rpc_trustdom_usage},
2234 {"list", rpc_trustdom_list},
2239 rpc_trustdom_usage(argc, argv);
2243 return (net_run_function(argc, argv, func, rpc_user_usage));
2247 * Check if a server will take rpc commands
2248 * @param flags Type of server to connect to (PDC, DMB, localhost)
2249 * if the host is not explicitly specified
2250 * @return BOOL (true means rpc supported)
2252 BOOL net_rpc_check(unsigned flags)
2254 struct cli_state cli;
2256 struct in_addr server_ip;
2257 char *server_name = NULL;
2259 /* flags (i.e. server type) may depend on command */
2260 if (!net_find_server(flags, &server_ip, &server_name))
2264 if (cli_initialise(&cli) == False)
2267 if (!cli_connect(&cli, server_name, &server_ip))
2269 if (!attempt_netbios_session_request(&cli, global_myname(),
2270 server_name, &server_ip))
2272 if (!cli_negprot(&cli))
2274 if (cli.protocol < PROTOCOL_NT1)
2284 /****************************************************************************/
2288 * Basic usage function for 'net rpc'
2289 * @param argc Standard main() style argc
2290 * @param argv Standard main() style argv. Initial components are already
2294 int net_rpc_usage(int argc, const char **argv)
2296 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2297 d_printf(" net rpc join \t\t\tto join a domain \n");
2298 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2299 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2300 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2301 d_printf(" net rpc group \t\tto list groups\n");
2302 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2303 d_printf(" net rpc file \t\t\tto list open files\n");
2304 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2305 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2306 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2307 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2308 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2309 "\t\t\t\t\tor establish trust\n");
2310 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2311 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2313 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2314 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2315 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2316 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2317 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2323 * Help function for 'net rpc'. Calls command specific help if requested
2324 * or displays usage of net rpc
2325 * @param argc Standard main() style argc
2326 * @param argv Standard main() style argv. Initial components are already
2330 int net_rpc_help(int argc, const char **argv)
2332 struct functable func[] = {
2333 {"join", rpc_join_usage},
2334 {"user", rpc_user_usage},
2335 {"group", rpc_group_usage},
2336 {"share", rpc_share_usage},
2337 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2338 {"trustdom", rpc_trustdom_usage},
2339 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2340 /*{"shutdown", rpc_shutdown_usage}, */
2345 net_rpc_usage(argc, argv);
2349 return (net_run_function(argc, argv, func, rpc_user_usage));
2354 * 'net rpc' entrypoint.
2355 * @param argc Standard main() style argc
2356 * @param argv Standard main() style argv. Initial components are already
2360 int net_rpc(int argc, const char **argv)
2362 struct functable func[] = {
2363 {"info", net_rpc_info},
2364 {"join", net_rpc_join},
2365 {"oldjoin", net_rpc_oldjoin},
2366 {"testjoin", net_rpc_testjoin},
2367 {"user", net_rpc_user},
2368 {"group", net_rpc_group},
2369 {"share", net_rpc_share},
2370 {"file", net_rpc_file},
2371 {"changetrustpw", net_rpc_changetrustpw},
2372 {"trustdom", rpc_trustdom},
2373 {"abortshutdown", rpc_shutdown_abort},
2374 {"shutdown", rpc_shutdown},
2375 {"samdump", rpc_samdump},
2376 {"vampire", rpc_vampire},
2377 {"getsid", net_rpc_getsid},
2378 {"help", net_rpc_help},
2381 return net_run_function(argc, argv, func, net_rpc_usage);