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, TALLOC_CTX *mem_ctx)
55 NTSTATUS result = NT_STATUS_OK;
56 uint32 info_class = 5;
59 if (!cli_nt_session_open (cli, PI_LSARPC)) {
60 fprintf(stderr, "could not initialise lsa pipe\n");
64 result = cli_lsa_open_policy(cli, mem_ctx, False,
65 SEC_RIGHTS_MAXIMUM_ALLOWED,
67 if (!NT_STATUS_IS_OK(result)) {
71 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
72 &domain_name, &domain_sid);
73 if (!NT_STATUS_IS_OK(result)) {
75 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
77 if (!NT_STATUS_IS_OK(result)) {
78 fprintf(stderr, "error: %s\n", nt_errstr(result));
84 cli_lsa_close(cli, mem_ctx, &pol);
85 cli_nt_session_close(cli);
91 * Run a single RPC command, from start to finish.
93 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
94 * @param conn_flag a NET_FLAG_ combination. Passed to
95 * net_make_ipc_connection.
96 * @param argc Standard main() style argc
97 * @param argc Standard main() style argv. Initial components are already
99 * @return A shell status integer (0 for success)
102 static int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
104 int argc, const char **argv)
106 struct cli_state *cli = NULL;
111 /* make use of cli_state handed over as an argument, if possible */
113 cli = net_make_ipc_connection(conn_flags);
123 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
124 DEBUG(0, ("talloc_init() failed\n"));
129 domain_sid = net_get_remote_domain_sid(cli, mem_ctx);
131 if (!cli_nt_session_open(cli, pipe_idx)) {
132 DEBUG(0, ("Could not initialise pipe\n"));
135 nt_status = fn(domain_sid, cli, mem_ctx, argc, argv);
137 if (!NT_STATUS_IS_OK(nt_status)) {
138 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
140 DEBUG(5, ("rpc command function succedded\n"));
144 if (cli->nt_pipe_fnum)
145 cli_nt_session_close(cli);
147 /* close the connection only if it was opened here */
151 talloc_destroy(mem_ctx);
153 return (!NT_STATUS_IS_OK(nt_status));
157 /****************************************************************************/
161 * Force a change of the trust acccount password.
163 * All parameters are provided by the run_rpc_command function, except for
164 * argc, argv which are passes through.
166 * @param domain_sid The domain sid aquired from the remote server
167 * @param cli A cli_state connected to the server.
168 * @param mem_ctx Talloc context, destoyed on compleation of the function.
169 * @param argc Standard main() style argc
170 * @param argc Standard main() style argv. Initial components are already
173 * @return Normal NTSTATUS return.
176 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
177 int argc, const char **argv) {
179 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
183 * Force a change of the trust acccount password.
185 * @param argc Standard main() style argc
186 * @param argc Standard main() style argv. Initial components are already
189 * @return A shell status integer (0 for success)
192 int net_rpc_changetrustpw(int argc, const char **argv)
194 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_changetrustpw_internals,
199 /****************************************************************************/
203 * Join a domain, the old way.
205 * This uses 'machinename' as the inital password, and changes it.
207 * The password should be created with 'server manager' or equiv first.
209 * All parameters are provided by the run_rpc_command function, except for
210 * argc, argv which are passes through.
212 * @param domain_sid The domain sid aquired from the remote server
213 * @param cli A cli_state connected to the server.
214 * @param mem_ctx Talloc context, destoyed on compleation of the function.
215 * @param argc Standard main() style argc
216 * @param argc Standard main() style argv. Initial components are already
219 * @return Normal NTSTATUS return.
222 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, struct cli_state *cli,
224 int argc, const char **argv) {
226 fstring trust_passwd;
227 unsigned char orig_trust_passwd_hash[16];
229 uint32 sec_channel_type;
232 check what type of join - if the user want's to join as
233 a BDC, the server must agree that we are a BDC.
236 sec_channel_type = get_sec_channel_type(argv[0]);
238 sec_channel_type = get_sec_channel_type(NULL);
241 fstrcpy(trust_passwd, global_myname());
242 strlower_m(trust_passwd);
245 * Machine names can be 15 characters, but the max length on
246 * a password is 14. --jerry
249 trust_passwd[14] = '\0';
251 E_md4hash(trust_passwd, orig_trust_passwd_hash);
253 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
254 orig_trust_passwd_hash,
257 if (NT_STATUS_IS_OK(result))
258 printf("Joined domain %s.\n",opt_target_workgroup);
261 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
262 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
263 result = NT_STATUS_UNSUCCESSFUL;
270 * Join a domain, the old way.
272 * @param argc Standard main() style argc
273 * @param argc Standard main() style argv. Initial components are already
276 * @return A shell status integer (0 for success)
279 static int net_rpc_perform_oldjoin(int argc, const char **argv)
281 return run_rpc_command(NULL, PI_NETLOGON,
282 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
283 rpc_oldjoin_internals,
288 * Join a domain, the old way. This function exists to allow
289 * the message to be displayed when oldjoin was explicitly
290 * requested, but not when it was implied by "net rpc join"
292 * @param argc Standard main() style argc
293 * @param argc Standard main() style argv. Initial components are already
296 * @return A shell status integer (0 for success)
299 static int net_rpc_oldjoin(int argc, const char **argv)
301 int rc = net_rpc_perform_oldjoin(argc, argv);
304 d_printf("Failed to join domain\n");
309 * Basic usage function for 'net rpc join'
310 * @param argc Standard main() style argc
311 * @param argc Standard main() style argv. Initial components are already
315 static int rpc_join_usage(int argc, const char **argv)
317 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
318 "\t to join a domain with admin username & password\n"\
319 "\t\t password will be prompted if needed and none is specified\n"\
320 "\t <type> can be (default MEMBER)\n"\
321 "\t\t BDC - Join as a BDC\n"\
322 "\t\t PDC - Join as a PDC\n"\
323 "\t\t MEMBER - Join as a MEMBER server\n");
325 net_common_flags_usage(argc, argv);
330 * 'net rpc join' entrypoint.
331 * @param argc Standard main() style argc
332 * @param argc Standard main() style argv. Initial components are already
335 * Main 'net_rpc_join()' (where the admain username/password is used) is
337 * Try to just change the password, but if that doesn't work, use/prompt
338 * for a username/password.
341 int net_rpc_join(int argc, const char **argv)
343 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
346 return net_rpc_join_newstyle(argc, argv);
352 * display info about a rpc domain
354 * All parameters are provided by the run_rpc_command function, except for
355 * argc, argv which are passed through.
357 * @param domain_sid The domain sid acquired from the remote server
358 * @param cli A cli_state connected to the server.
359 * @param mem_ctx Talloc context, destoyed on completion of the function.
360 * @param argc Standard main() style argc
361 * @param argv Standard main() style argv. Initial components are already
364 * @return Normal NTSTATUS return.
368 rpc_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
369 TALLOC_CTX *mem_ctx, int argc, const char **argv)
371 POLICY_HND connect_pol, domain_pol;
372 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
376 sid_to_string(sid_str, domain_sid);
378 /* Get sam policy handle */
379 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
381 if (!NT_STATUS_IS_OK(result)) {
385 /* Get domain policy handle */
386 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
387 MAXIMUM_ALLOWED_ACCESS,
388 domain_sid, &domain_pol);
389 if (!NT_STATUS_IS_OK(result)) {
394 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
396 if (NT_STATUS_IS_OK(result)) {
397 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
398 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
399 d_printf("Domain SID: %s\n", sid_str);
400 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
401 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
402 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
403 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
413 * 'net rpc info' entrypoint.
414 * @param argc Standard main() style argc
415 * @param argc Standard main() style argv. Initial components are already
418 int net_rpc_info(int argc, const char **argv)
420 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
427 * Fetch domain SID into the local secrets.tdb
429 * All parameters are provided by the run_rpc_command function, except for
430 * argc, argv which are passes through.
432 * @param domain_sid The domain sid acquired from the remote server
433 * @param cli A cli_state connected to the server.
434 * @param mem_ctx Talloc context, destoyed on completion of the function.
435 * @param argc Standard main() style argc
436 * @param argv Standard main() style argv. Initial components are already
439 * @return Normal NTSTATUS return.
443 rpc_getsid_internals(const DOM_SID *domain_sid, struct cli_state *cli,
444 TALLOC_CTX *mem_ctx, int argc, const char **argv)
448 sid_to_string(sid_str, domain_sid);
449 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
450 sid_str, lp_workgroup());
452 if (!secrets_store_domain_sid(global_myname(), domain_sid)) {
453 DEBUG(0,("Can't store domain SID\n"));
454 return NT_STATUS_UNSUCCESSFUL;
462 * 'net rpc getsid' entrypoint.
463 * @param argc Standard main() style argc
464 * @param argc Standard main() style argv. Initial components are already
467 int net_rpc_getsid(int argc, const char **argv)
469 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
470 rpc_getsid_internals,
475 /****************************************************************************/
478 * Basic usage function for 'net rpc user'
479 * @param argc Standard main() style argc.
480 * @param argv Standard main() style argv. Initial components are already
484 static int rpc_user_usage(int argc, const char **argv)
486 return net_help_user(argc, argv);
490 * Add a new user to a remote RPC server
492 * All parameters are provided by the run_rpc_command function, except for
493 * argc, argv which are passes through.
495 * @param domain_sid The domain sid acquired from the remote server
496 * @param cli A cli_state connected to the server.
497 * @param mem_ctx Talloc context, destoyed on completion of the function.
498 * @param argc Standard main() style argc
499 * @param argv Standard main() style argv. Initial components are already
502 * @return Normal NTSTATUS return.
505 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
506 int argc, const char **argv) {
508 POLICY_HND connect_pol, domain_pol, user_pol;
509 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
510 const char *acct_name;
512 uint32 unknown, user_rid;
515 d_printf("User must be specified\n");
516 rpc_user_usage(argc, argv);
522 /* Get sam policy handle */
524 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
526 if (!NT_STATUS_IS_OK(result)) {
530 /* Get domain policy handle */
532 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
533 MAXIMUM_ALLOWED_ACCESS,
534 domain_sid, &domain_pol);
535 if (!NT_STATUS_IS_OK(result)) {
539 /* Create domain user */
541 acb_info = ACB_NORMAL;
542 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
544 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
545 acct_name, acb_info, unknown,
546 &user_pol, &user_rid);
547 if (!NT_STATUS_IS_OK(result)) {
552 if (!NT_STATUS_IS_OK(result)) {
553 d_printf("Failed to add user %s - %s\n", acct_name,
556 d_printf("Added user %s\n", acct_name);
562 * Add a new user to a remote RPC server
564 * @param argc Standard main() style argc
565 * @param argv Standard main() style argv. Initial components are already
568 * @return A shell status integer (0 for success)
571 static int rpc_user_add(int argc, const char **argv)
573 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
578 * Delete a user from a remote RPC server
580 * All parameters are provided by the run_rpc_command function, except for
581 * argc, argv which are passes through.
583 * @param domain_sid The domain sid acquired from the remote server
584 * @param cli A cli_state connected to the server.
585 * @param mem_ctx Talloc context, destoyed on completion of the function.
586 * @param argc Standard main() style argc
587 * @param argv Standard main() style argv. Initial components are already
590 * @return Normal NTSTATUS return.
593 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
594 struct cli_state *cli,
596 int argc, const char **argv)
598 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
599 POLICY_HND connect_pol, domain_pol, user_pol;
602 d_printf("User must be specified\n");
603 rpc_user_usage(argc, argv);
606 /* Get sam policy and domain handles */
608 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
611 if (!NT_STATUS_IS_OK(result)) {
615 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
616 MAXIMUM_ALLOWED_ACCESS,
617 domain_sid, &domain_pol);
619 if (!NT_STATUS_IS_OK(result)) {
623 /* Get handle on user */
626 uint32 *user_rids, num_rids, *name_types;
627 uint32 flags = 0x000003e8; /* Unknown */
629 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
631 &num_rids, &user_rids,
634 if (!NT_STATUS_IS_OK(result)) {
638 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
639 MAXIMUM_ALLOWED_ACCESS,
640 user_rids[0], &user_pol);
642 if (!NT_STATUS_IS_OK(result)) {
649 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
651 if (!NT_STATUS_IS_OK(result)) {
655 /* Display results */
663 * Delete a user from a remote RPC server
665 * @param argc Standard main() style argc
666 * @param argv Standard main() style argv. Initial components are already
669 * @return A shell status integer (0 for success)
672 static int rpc_user_delete(int argc, const char **argv)
674 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
679 * List user's groups on a remote RPC server
681 * All parameters are provided by the run_rpc_command function, except for
682 * argc, argv which are passes through.
684 * @param domain_sid The domain sid acquired from the remote server
685 * @param cli A cli_state connected to the server.
686 * @param mem_ctx Talloc context, destoyed on completion of the function.
687 * @param argc Standard main() style argc
688 * @param argv Standard main() style argv. Initial components are already
691 * @return Normal NTSTATUS return.
695 rpc_user_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
696 TALLOC_CTX *mem_ctx, int argc, const char **argv)
698 POLICY_HND connect_pol, domain_pol, user_pol;
699 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
700 uint32 *rids, num_rids, *name_types, num_names;
701 uint32 flags = 0x000003e8; /* Unknown */
707 d_printf("User must be specified\n");
708 rpc_user_usage(argc, argv);
711 /* Get sam policy handle */
713 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
715 if (!NT_STATUS_IS_OK(result)) goto done;
717 /* Get domain policy handle */
719 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
720 MAXIMUM_ALLOWED_ACCESS,
721 domain_sid, &domain_pol);
722 if (!NT_STATUS_IS_OK(result)) goto done;
724 /* Get handle on user */
726 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
728 &num_rids, &rids, &name_types);
730 if (!NT_STATUS_IS_OK(result)) goto done;
732 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
733 MAXIMUM_ALLOWED_ACCESS,
735 if (!NT_STATUS_IS_OK(result)) goto done;
737 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
738 &num_rids, &user_gids);
742 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
744 for (i = 0; i < num_rids; i++)
745 rids[i] = user_gids[i].g_rid;
747 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
748 flags, num_rids, rids,
749 &num_names, &names, &name_types);
751 if (!NT_STATUS_IS_OK(result)) {
755 /* Display results */
757 for (i = 0; i < num_names; i++)
758 printf("%s\n", names[i]);
765 * List a user's groups from a remote RPC server
767 * @param argc Standard main() style argc
768 * @param argv Standard main() style argv. Initial components are already
771 * @return A shell status integer (0 for success)
774 static int rpc_user_info(int argc, const char **argv)
776 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
781 * List users on a remote RPC server
783 * All parameters are provided by the run_rpc_command function, except for
784 * argc, argv which are passes through.
786 * @param domain_sid The domain sid acquired from the remote server
787 * @param cli A cli_state connected to the server.
788 * @param mem_ctx Talloc context, destoyed on completion of the function.
789 * @param argc Standard main() style argc
790 * @param argv Standard main() style argv. Initial components are already
793 * @return Normal NTSTATUS return.
797 rpc_user_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
798 TALLOC_CTX *mem_ctx, int argc, const char **argv)
800 POLICY_HND connect_pol, domain_pol;
801 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
802 uint32 start_idx=0, num_entries, i, loop_count = 0;
803 SAM_DISPINFO_CTR ctr;
804 SAM_DISPINFO_1 info1;
806 /* Get sam policy handle */
808 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
810 if (!NT_STATUS_IS_OK(result)) {
814 /* Get domain policy handle */
816 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
817 MAXIMUM_ALLOWED_ACCESS,
818 domain_sid, &domain_pol);
819 if (!NT_STATUS_IS_OK(result)) {
823 /* Query domain users */
826 ctr.sam.info1 = &info1;
827 if (opt_long_list_entries)
828 d_printf("\nUser name Comment"\
829 "\n-----------------------------\n");
832 uint32 max_entries, max_size;
834 get_query_dispinfo_params(
835 loop_count, &max_entries, &max_size);
837 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
838 &start_idx, 1, &num_entries,
839 max_entries, max_size, &ctr);
842 for (i = 0; i < num_entries; i++) {
843 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
844 if (opt_long_list_entries)
845 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
847 if (opt_long_list_entries)
848 printf("%-21.21s %s\n", user, desc);
850 printf("%s\n", user);
852 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
859 * 'net rpc user' entrypoint.
860 * @param argc Standard main() style argc
861 * @param argc Standard main() style argv. Initial components are already
865 int net_rpc_user(int argc, const char **argv)
867 struct functable func[] = {
868 {"add", rpc_user_add},
869 {"info", rpc_user_info},
870 {"delete", rpc_user_delete},
875 if (opt_long_list_entries) {
878 return run_rpc_command(NULL,PI_SAMR, 0,
879 rpc_user_list_internals,
883 return net_run_function(argc, argv, func, rpc_user_usage);
887 /****************************************************************************/
890 * Basic usage function for 'net rpc group'
891 * @param argc Standard main() style argc.
892 * @param argv Standard main() style argv. Initial components are already
896 static int rpc_group_usage(int argc, const char **argv)
898 return net_help_group(argc, argv);
902 * List groups on a remote RPC server
904 * All parameters are provided by the run_rpc_command function, except for
905 * argc, argv which are passes through.
907 * @param domain_sid The domain sid acquired from the remote server
908 * @param cli A cli_state connected to the server.
909 * @param mem_ctx Talloc context, destoyed on completion of the function.
910 * @param argc Standard main() style argc
911 * @param argv Standard main() style argv. Initial components are already
914 * @return Normal NTSTATUS return.
918 rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
919 TALLOC_CTX *mem_ctx, int argc, const char **argv)
921 POLICY_HND connect_pol, domain_pol;
922 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
923 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
924 struct acct_info *groups;
925 DOM_SID global_sid_Builtin;
928 BOOL builtin = False;
936 for (i=0; i<argc; i++) {
937 if (strequal(argv[i], "global"))
940 if (strequal(argv[i], "local"))
943 if (strequal(argv[i], "builtin"))
947 string_to_sid(&global_sid_Builtin, "S-1-5-32");
949 /* Get sam policy handle */
951 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
953 if (!NT_STATUS_IS_OK(result)) {
957 /* Get domain policy handle */
959 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
960 MAXIMUM_ALLOWED_ACCESS,
961 domain_sid, &domain_pol);
962 if (!NT_STATUS_IS_OK(result)) {
966 /* Query domain groups */
967 if (opt_long_list_entries)
968 d_printf("\nGroup name Comment"\
969 "\n-----------------------------\n");
971 SAM_DISPINFO_CTR ctr;
972 SAM_DISPINFO_3 info3;
977 ctr.sam.info3 = &info3;
981 get_query_dispinfo_params(
982 loop_count, &max_entries, &max_size);
984 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
985 &start_idx, 3, &num_entries,
986 max_entries, max_size, &ctr);
988 for (i = 0; i < num_entries; i++) {
992 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
993 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
995 if (opt_long_list_entries)
996 printf("%-21.21s %-50.50s\n",
999 printf("%s\n", group);
1001 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1002 /* query domain aliases */
1007 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1008 &start_idx, max_entries,
1009 &groups, &num_entries);
1011 for (i = 0; i < num_entries; i++) {
1013 char *description = NULL;
1015 if (opt_long_list_entries) {
1017 POLICY_HND alias_pol;
1020 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1025 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1028 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1030 description = unistr2_tdup(mem_ctx,
1031 &ctr.alias.info3.uni_acct_desc);
1035 if (description != NULL) {
1036 printf("%-21.21s %-50.50s\n",
1037 groups[i].acct_name,
1040 printf("%s\n", groups[i].acct_name);
1043 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1044 cli_samr_close(cli, mem_ctx, &domain_pol);
1045 /* Get builtin policy handle */
1047 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1048 MAXIMUM_ALLOWED_ACCESS,
1049 &global_sid_Builtin, &domain_pol);
1050 if (!NT_STATUS_IS_OK(result)) {
1053 /* query builtin aliases */
1056 if (!builtin) break;
1058 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1059 &start_idx, max_entries,
1060 &groups, &num_entries);
1062 for (i = 0; i < num_entries; i++) {
1064 char *description = NULL;
1066 if (opt_long_list_entries) {
1068 POLICY_HND alias_pol;
1071 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1076 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1079 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1081 description = unistr2_tdup(mem_ctx,
1082 &ctr.alias.info3.uni_acct_desc);
1086 if (description != NULL) {
1087 printf("%-21.21s %-50.50s\n",
1088 groups[i].acct_name,
1091 printf("%s\n", groups[i].acct_name);
1094 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1100 static int rpc_group_list(int argc, const char **argv)
1102 return run_rpc_command(NULL, PI_SAMR, 0,
1103 rpc_group_list_internals,
1108 rpc_group_members_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1109 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1112 POLICY_HND connect_pol, domain_pol, group_pol;
1113 uint32 num_rids, *rids, *rid_types;
1114 uint32 num_members, *group_rids, *group_attrs;
1120 /* Get sam policy handle */
1122 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1124 if (!NT_STATUS_IS_OK(result)) {
1128 /* Get domain policy handle */
1130 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1131 MAXIMUM_ALLOWED_ACCESS,
1132 domain_sid, &domain_pol);
1133 if (!NT_STATUS_IS_OK(result)) {
1137 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1138 1, argv, &num_rids, &rids, &rid_types);
1140 if (!NT_STATUS_IS_OK(result)) {
1144 if (num_rids != 1) {
1145 d_printf("Could not find group %s\n", argv[0]);
1149 if (rid_types[0] != SID_NAME_DOM_GRP) {
1150 d_printf("%s is not a domain group\n", argv[0]);
1154 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1155 MAXIMUM_ALLOWED_ACCESS,
1156 rids[0], &group_pol);
1158 if (!NT_STATUS_IS_OK(result))
1161 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1162 &num_members, &group_rids,
1165 if (!NT_STATUS_IS_OK(result))
1168 while (num_members > 0) {
1169 int this_time = 512;
1171 if (num_members < this_time)
1172 this_time = num_members;
1174 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol, 1000,
1175 this_time, group_rids,
1176 &num_names, &names, &name_types);
1178 if (!NT_STATUS_IS_OK(result))
1181 for (i = 0; i < this_time; i++) {
1182 printf("%s\n", names[i]);
1185 num_members -= this_time;
1193 static int rpc_group_members(int argc, const char **argv)
1196 return rpc_group_usage(argc, argv);
1199 return run_rpc_command(NULL, PI_SAMR, 0,
1200 rpc_group_members_internals,
1205 * 'net rpc group' entrypoint.
1206 * @param argc Standard main() style argc
1207 * @param argc Standard main() style argv. Initial components are already
1211 int net_rpc_group(int argc, const char **argv)
1213 struct functable func[] = {
1215 {"add", rpc_group_add},
1216 {"delete", rpc_group_delete},
1218 {"list", rpc_group_list},
1219 {"members", rpc_group_members},
1224 if (opt_long_list_entries) {
1227 return run_rpc_command(NULL, PI_SAMR, 0,
1228 rpc_group_list_internals,
1232 return net_run_function(argc, argv, func, rpc_group_usage);
1235 /****************************************************************************/
1237 static int rpc_share_usage(int argc, const char **argv)
1239 return net_help_share(argc, argv);
1243 * Add a share on a remote RPC server
1245 * All parameters are provided by the run_rpc_command function, except for
1246 * argc, argv which are passes through.
1248 * @param domain_sid The domain sid acquired from the remote server
1249 * @param cli A cli_state connected to the server.
1250 * @param mem_ctx Talloc context, destoyed on completion of the function.
1251 * @param argc Standard main() style argc
1252 * @param argv Standard main() style argv. Initial components are already
1255 * @return Normal NTSTATUS return.
1258 rpc_share_add_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1259 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1262 char *sharename=talloc_strdup(mem_ctx, argv[0]);
1264 uint32 type=0; /* only allow disk shares to be added */
1265 uint32 num_users=0, perms=0;
1266 char *password=NULL; /* don't allow a share password */
1268 path = strchr(sharename, '=');
1270 return NT_STATUS_UNSUCCESSFUL;
1273 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1274 opt_comment, perms, opt_maxusers,
1275 num_users, path, password);
1276 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1279 static int rpc_share_add(int argc, const char **argv)
1281 if ((argc < 1) || !strchr(argv[0], '=')) {
1282 DEBUG(1,("Sharename or path not specified on add\n"));
1283 return rpc_share_usage(argc, argv);
1285 return run_rpc_command(NULL, PI_SRVSVC, 0,
1286 rpc_share_add_internals,
1291 * Delete a share on a remote RPC server
1293 * All parameters are provided by the run_rpc_command function, except for
1294 * argc, argv which are passes through.
1296 * @param domain_sid The domain sid acquired from the remote server
1297 * @param cli A cli_state connected to the server.
1298 * @param mem_ctx Talloc context, destoyed on completion of the function.
1299 * @param argc Standard main() style argc
1300 * @param argv Standard main() style argv. Initial components are already
1303 * @return Normal NTSTATUS return.
1306 rpc_share_del_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1307 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1311 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1312 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1316 * Delete a share on a remote RPC server
1318 * @param domain_sid The domain sid acquired from the remote server
1319 * @param argc Standard main() style argc
1320 * @param argv Standard main() style argv. Initial components are already
1323 * @return A shell status integer (0 for success)
1325 static int rpc_share_delete(int argc, const char **argv)
1328 DEBUG(1,("Sharename not specified on delete\n"));
1329 return rpc_share_usage(argc, argv);
1331 return run_rpc_command(NULL, PI_SRVSVC, 0,
1332 rpc_share_del_internals,
1337 * Formatted print of share info
1339 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1342 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1344 fstring netname = "", remark = "";
1346 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1347 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1349 if (opt_long_list_entries) {
1350 d_printf("%-12.12s %-8.8s %-50.50s\n",
1351 netname, share_type[info1->info_1.type], remark);
1353 d_printf("%-12.12s\n", netname);
1359 * List shares on a remote RPC server
1361 * All parameters are provided by the run_rpc_command function, except for
1362 * argc, argv which are passes through.
1364 * @param domain_sid The domain sid acquired from the remote server
1365 * @param cli A cli_state connected to the server.
1366 * @param mem_ctx Talloc context, destoyed on completion of the function.
1367 * @param argc Standard main() style argc
1368 * @param argv Standard main() style argv. Initial components are already
1371 * @return Normal NTSTATUS return.
1375 rpc_share_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1376 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1378 SRV_SHARE_INFO_CTR ctr;
1381 uint32 preferred_len = 0xffffffff, i;
1383 init_enum_hnd(&hnd, 0);
1385 result = cli_srvsvc_net_share_enum(
1386 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1388 if (!W_ERROR_IS_OK(result))
1391 /* Display results */
1393 if (opt_long_list_entries) {
1395 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1396 "\nShare name Type Description\n"\
1397 "---------- ---- -----------\n");
1399 for (i = 0; i < ctr.num_entries; i++)
1400 display_share_info_1(&ctr.share.info1[i]);
1402 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1406 * 'net rpc share' entrypoint.
1407 * @param argc Standard main() style argc
1408 * @param argv Standard main() style argv. Initial components are already
1412 int net_rpc_share(int argc, const char **argv)
1414 struct functable func[] = {
1415 {"add", rpc_share_add},
1416 {"delete", rpc_share_delete},
1421 return run_rpc_command(NULL, PI_SRVSVC, 0,
1422 rpc_share_list_internals,
1425 return net_run_function(argc, argv, func, rpc_share_usage);
1428 /****************************************************************************/
1430 static int rpc_file_usage(int argc, const char **argv)
1432 return net_help_file(argc, argv);
1436 * Close a file on a remote RPC server
1438 * All parameters are provided by the run_rpc_command function, except for
1439 * argc, argv which are passes through.
1441 * @param domain_sid The domain sid acquired from the remote server
1442 * @param cli A cli_state connected to the server.
1443 * @param mem_ctx Talloc context, destoyed on completion of the function.
1444 * @param argc Standard main() style argc
1445 * @param argv Standard main() style argv. Initial components are already
1448 * @return Normal NTSTATUS return.
1451 rpc_file_close_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1452 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1455 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1456 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1460 * Close a file on a remote RPC server
1462 * @param argc Standard main() style argc
1463 * @param argv Standard main() style argv. Initial components are already
1466 * @return A shell status integer (0 for success)
1468 static int rpc_file_close(int argc, const char **argv)
1471 DEBUG(1, ("No fileid given on close\n"));
1472 return(rpc_file_usage(argc, argv));
1475 return run_rpc_command(NULL, PI_SRVSVC, 0,
1476 rpc_file_close_internals,
1481 * Formatted print of open file info
1483 * @param info3 FILE_INFO_3 contents
1484 * @param str3 strings for FILE_INFO_3
1487 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1489 fstring user = "", path = "";
1491 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1492 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1494 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1495 info3->id, user, info3->perms, info3->num_locks, path);
1499 * List open files on a remote RPC server
1501 * All parameters are provided by the run_rpc_command function, except for
1502 * argc, argv which are passes through.
1504 * @param domain_sid The domain sid acquired from the remote server
1505 * @param cli A cli_state connected to the server.
1506 * @param mem_ctx Talloc context, destoyed on completion of the function.
1507 * @param argc Standard main() style argc
1508 * @param argv Standard main() style argv. Initial components are already
1511 * @return Normal NTSTATUS return.
1515 rpc_file_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1516 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1518 SRV_FILE_INFO_CTR ctr;
1521 uint32 preferred_len = 0xffffffff, i;
1522 const char *username=NULL;
1524 init_enum_hnd(&hnd, 0);
1526 /* if argc > 0, must be user command */
1528 username = smb_xstrdup(argv[0]);
1530 result = cli_srvsvc_net_file_enum(
1531 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1533 if (!W_ERROR_IS_OK(result))
1536 /* Display results */
1539 "\nEnumerating open files on remote server:\n\n"\
1540 "\nFileId Opened by Perms Locks Path"\
1541 "\n------ --------- ----- ----- ---- \n");
1542 for (i = 0; i < ctr.num_entries; i++)
1543 display_file_info_3(&ctr.file.info3[i].info_3,
1544 &ctr.file.info3[i].info_3_str);
1546 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1551 * List files for a user on a remote RPC server
1553 * @param argc Standard main() style argc
1554 * @param argv Standard main() style argv. Initial components are already
1557 * @return A shell status integer (0 for success)
1559 static int rpc_file_user(int argc, const char **argv)
1562 DEBUG(1, ("No username given\n"));
1563 return(rpc_file_usage(argc, argv));
1566 return run_rpc_command(NULL, PI_SRVSVC, 0,
1567 rpc_file_list_internals,
1573 * 'net rpc file' entrypoint.
1574 * @param argc Standard main() style argc
1575 * @param argv Standard main() style argv. Initial components are already
1579 int net_rpc_file(int argc, const char **argv)
1581 struct functable func[] = {
1582 {"close", rpc_file_close},
1583 {"user", rpc_file_user},
1585 {"info", rpc_file_info},
1591 return run_rpc_command(NULL, PI_SRVSVC, 0,
1592 rpc_file_list_internals,
1595 return net_run_function(argc, argv, func, rpc_file_usage);
1598 /****************************************************************************/
1603 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
1605 * All parameters are provided by the run_rpc_command function, except for
1606 * argc, argv which are passed through.
1608 * @param domain_sid The domain sid aquired from the remote server
1609 * @param cli A cli_state connected to the server.
1610 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1611 * @param argc Standard main() style argc
1612 * @param argv Standard main() style argv. Initial components are already
1615 * @return Normal NTSTATUS return.
1618 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
1619 struct cli_state *cli,
1620 TALLOC_CTX *mem_ctx,
1621 int argc, const char **argv)
1623 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1625 result = cli_shutdown_abort(cli, mem_ctx);
1627 if (NT_STATUS_IS_OK(result))
1628 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1630 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1637 * ABORT the shutdown of a remote RPC Server, over winreg pipe
1639 * All parameters are provided by the run_rpc_command function, except for
1640 * argc, argv which are passed through.
1642 * @param domain_sid The domain sid aquired from the remote server
1643 * @param cli A cli_state connected to the server.
1644 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1645 * @param argc Standard main() style argc
1646 * @param argv Standard main() style argv. Initial components are already
1649 * @return Normal NTSTATUS return.
1652 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
1653 struct cli_state *cli,
1654 TALLOC_CTX *mem_ctx,
1655 int argc, const char **argv)
1657 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1659 result = cli_reg_abort_shutdown(cli, mem_ctx);
1661 if (NT_STATUS_IS_OK(result))
1662 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1664 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1670 * ABORT the Shut down of a remote RPC server
1672 * @param argc Standard main() style argc
1673 * @param argv Standard main() style argv. Initial components are already
1676 * @return A shell status integer (0 for success)
1679 static int rpc_shutdown_abort(int argc, const char **argv)
1681 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
1682 rpc_shutdown_abort_internals,
1688 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
1690 return run_rpc_command(NULL, PI_WINREG, 0,
1691 rpc_reg_shutdown_abort_internals,
1696 * Shut down a remote RPC Server
1698 * All parameters are provided by the run_rpc_command function, except for
1699 * argc, argv which are passes through.
1701 * @param domain_sid The domain sid aquired from the remote server
1702 * @param cli A cli_state connected to the server.
1703 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1704 * @param argc Standard main() style argc
1705 * @param argc Standard main() style argv. Initial components are already
1708 * @return Normal NTSTATUS return.
1711 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1712 int argc, const char **argv)
1714 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1715 const char *msg = "This machine will be shutdown shortly";
1716 uint32 timeout = 20;
1721 struct poptOption long_options[] = {
1722 {"message", 'm', POPT_ARG_STRING, &msg},
1723 {"timeout", 't', POPT_ARG_INT, &timeout},
1724 {"reboot", 'r', POPT_ARG_NONE, &reboot},
1725 {"force", 'f', POPT_ARG_NONE, &force},
1729 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1730 POPT_CONTEXT_KEEP_FIRST);
1732 rc = poptGetNextOpt(pc);
1735 /* an error occurred during option processing */
1736 DEBUG(0, ("%s: %s\n",
1737 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1739 return NT_STATUS_INVALID_PARAMETER;
1746 timeout = opt_timeout;
1749 /* create an entry */
1750 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
1752 if (NT_STATUS_IS_OK(result))
1753 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1755 DEBUG(0,("Shutdown of remote machine failed!\n"));
1761 * Shut down a remote RPC server
1763 * @param argc Standard main() style argc
1764 * @param argc Standard main() style argv. Initial components are already
1767 * @return A shell status integer (0 for success)
1770 static int rpc_shutdown(int argc, const char **argv)
1772 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
1776 /***************************************************************************
1777 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1779 ***************************************************************************/
1782 * Add interdomain trust account to the RPC server.
1783 * All parameters (except for argc and argv) are passed by run_rpc_command
1786 * @param domain_sid The domain sid acquired from the server
1787 * @param cli A cli_state connected to the server.
1788 * @param mem_ctx Talloc context, destoyed on completion of the function.
1789 * @param argc Standard main() style argc
1790 * @param argc Standard main() style argv. Initial components are already
1793 * @return normal NTSTATUS return code
1796 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1797 int argc, const char **argv) {
1799 POLICY_HND connect_pol, domain_pol, user_pol;
1800 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1803 uint32 unknown, user_rid;
1806 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
1807 return NT_STATUS_INVALID_PARAMETER;
1811 * Make valid trusting domain account (ie. uppercased and with '$' appended)
1814 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1815 return NT_STATUS_NO_MEMORY;
1818 strupper_m(acct_name);
1820 /* Get samr policy handle */
1821 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1823 if (!NT_STATUS_IS_OK(result)) {
1827 /* Get domain policy handle */
1828 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1829 MAXIMUM_ALLOWED_ACCESS,
1830 domain_sid, &domain_pol);
1831 if (!NT_STATUS_IS_OK(result)) {
1835 /* Create trusting domain's account */
1836 acb_info = ACB_DOMTRUST;
1837 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
1838 mimir: yes, most probably it is */
1840 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1841 acct_name, acb_info, unknown,
1842 &user_pol, &user_rid);
1843 if (!NT_STATUS_IS_OK(result)) {
1848 SAM_USERINFO_CTR ctr;
1849 SAM_USER_INFO_24 p24;
1850 fstring ucs2_trust_password;
1854 ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password, argv[1],
1855 sizeof(ucs2_trust_password), 0);
1857 encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
1863 init_sam_user_info24(&p24, (char *)pwbuf, 24);
1865 ctr.switch_value = 24;
1866 ctr.info.id24 = &p24;
1868 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
1869 &cli->user_session_key, &ctr);
1871 if (!NT_STATUS_IS_OK(result)) {
1872 DEBUG(0,("Could not set trust account password: %s\n",
1873 nt_errstr(result)));
1879 SAFE_FREE(acct_name);
1884 * Create interdomain trust account for a remote domain.
1886 * @param argc standard argc
1887 * @param argv standard argv without initial components
1889 * @return Integer status (0 means success)
1892 static int rpc_trustdom_add(int argc, const char **argv)
1895 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
1898 d_printf("Usage: net rpc trustdom add <domain>\n");
1905 * Delete interdomain trust account for a remote domain.
1907 * @param argc standard argc
1908 * @param argv standard argv without initial components
1910 * @return Integer status (0 means success)
1913 static int rpc_trustdom_del(int argc, const char **argv)
1915 d_printf("Sorry, not yet implemented.\n");
1916 d_printf("Use 'smbpasswd -x -i' instead.\n");
1922 * Establish trust relationship to a trusting domain.
1923 * Interdomain account must already be created on remote PDC.
1925 * @param argc standard argc
1926 * @param argv standard argv without initial components
1928 * @return Integer status (0 means success)
1931 static int rpc_trustdom_establish(int argc, const char **argv)
1933 struct cli_state *cli;
1934 struct in_addr server_ip;
1935 POLICY_HND connect_hnd;
1936 TALLOC_CTX *mem_ctx;
1938 DOM_SID *domain_sid;
1939 WKS_INFO_100 wks_info;
1942 char* domain_name_pol;
1947 * Connect to \\server\ipc$ as 'our domain' account with password
1951 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
1955 domain_name = smb_xstrdup(argv[0]);
1956 strupper_m(domain_name);
1958 /* account name used at first is our domain's name with '$' */
1959 asprintf(&acct_name, "%s$", lp_workgroup());
1960 strupper_m(acct_name);
1963 * opt_workgroup will be used by connection functions further,
1964 * hence it should be set to remote domain name instead of ours
1966 if (opt_workgroup) {
1967 opt_workgroup = smb_xstrdup(domain_name);
1970 opt_user_name = acct_name;
1972 /* find the domain controller */
1973 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
1974 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
1978 /* connect to ipc$ as username/password */
1979 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
1980 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1982 /* Is it trusting domain account for sure ? */
1983 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
1984 nt_errstr(nt_status)));
1989 * Connect to \\server\ipc$ again (this time anonymously)
1992 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
1994 if (NT_STATUS_IS_ERR(nt_status)) {
1995 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
1996 domain_name, nt_errstr(nt_status)));
2000 * Use NetServerEnum2 to make sure we're talking to a proper server
2003 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
2004 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2005 for domain %s\n", domain_name));
2009 * Call WksQueryInfo to check remote server's capabilities
2010 * note: It is now used only to get unicode domain name
2013 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
2014 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2018 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
2020 DEBUG(0, ("talloc_init() failed\n"));
2025 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
2027 if (NT_STATUS_IS_ERR(nt_status)) {
2028 DEBUG(0, ("WksQueryInfo call failed.\n"));
2032 if (cli->nt_pipe_fnum)
2033 cli_nt_session_close(cli);
2037 * Call LsaOpenPolicy and LsaQueryInfo
2040 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
2041 DEBUG(0, ("talloc_init() failed\n"));
2046 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2047 DEBUG(0, ("Could not initialise lsa pipe\n"));
2052 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
2054 if (NT_STATUS_IS_ERR(nt_status)) {
2055 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2056 nt_errstr(nt_status)));
2060 /* Querying info level 5 */
2062 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
2063 5 /* info level */, &domain_name_pol,
2065 if (NT_STATUS_IS_ERR(nt_status)) {
2066 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2067 nt_errstr(nt_status)));
2074 /* There should be actually query info level 3 (following nt serv behaviour),
2075 but I still don't know if it's _really_ necessary */
2078 * Store the password in secrets db
2081 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
2082 wks_info.uni_lan_grp.uni_str_len, opt_password,
2084 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2089 * Close the pipes and clean up
2092 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2093 if (NT_STATUS_IS_ERR(nt_status)) {
2094 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2095 nt_errstr(nt_status)));
2099 if (cli->nt_pipe_fnum)
2100 cli_nt_session_close(cli);
2102 talloc_destroy(mem_ctx);
2104 DEBUG(0, ("Success!\n"));
2109 * Revoke trust relationship to the remote domain
2111 * @param argc standard argc
2112 * @param argv standard argv without initial components
2114 * @return Integer status (0 means success)
2117 static int rpc_trustdom_revoke(int argc, const char **argv)
2121 if (argc < 1) return -1;
2123 /* generate upper cased domain name */
2124 domain_name = smb_xstrdup(argv[0]);
2125 strupper_m(domain_name);
2127 /* delete password of the trust */
2128 if (!trusted_domain_password_delete(domain_name)) {
2129 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2138 * Usage for 'net rpc trustdom' command
2140 * @param argc standard argc
2141 * @param argv standard argv without inital components
2143 * @return Integer status returned to shell
2146 static int rpc_trustdom_usage(int argc, const char **argv)
2148 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
2149 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
2150 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
2151 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2152 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
2157 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
2158 int argc, const char **argv)
2161 sid_to_string(str_sid, domain_sid);
2162 d_printf("%s\n", str_sid);
2163 return NT_STATUS_OK;
2167 static int rpc_trustdom_list(int argc, const char **argv)
2169 /* common variables */
2170 TALLOC_CTX* mem_ctx;
2171 struct cli_state *cli, *remote_cli;
2173 const char *domain_name = NULL;
2174 DOM_SID *queried_dom_sid;
2175 fstring ascii_sid, padding;
2176 int ascii_dom_name_len;
2177 POLICY_HND connect_hnd;
2179 /* trusted domains listing variables */
2180 unsigned int num_domains, enum_ctx = 0;
2181 int i, pad_len, col_len = 20;
2182 DOM_SID *domain_sids;
2183 char **trusted_dom_names;
2187 /* trusting domains listing variables */
2188 POLICY_HND domain_hnd;
2189 char **trusting_dom_names;
2190 uint32 *trusting_dom_rids;
2193 * Listing trusted domains (stored in secrets.tdb, if local)
2196 mem_ctx = talloc_init("trust relationships listing");
2199 * set domain and pdc name to local samba server (default)
2200 * or to remote one given in command line
2203 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2204 domain_name = opt_workgroup;
2205 opt_target_workgroup = opt_workgroup;
2207 fstrcpy(pdc_name, global_myname());
2208 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2209 opt_target_workgroup = domain_name;
2212 /* open \PIPE\lsarpc and open policy handle */
2213 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2214 DEBUG(0, ("Couldn't connect to domain controller\n"));
2218 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2219 DEBUG(0, ("Could not initialise lsa pipe\n"));
2223 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2225 if (NT_STATUS_IS_ERR(nt_status)) {
2226 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2227 nt_errstr(nt_status)));
2231 /* query info level 5 to obtain sid of a domain being queried */
2232 nt_status = cli_lsa_query_info_policy(
2233 cli, mem_ctx, &connect_hnd, 5 /* info level */,
2234 &dummy, &queried_dom_sid);
2236 if (NT_STATUS_IS_ERR(nt_status)) {
2237 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2238 nt_errstr(nt_status)));
2243 * Keep calling LsaEnumTrustdom over opened pipe until
2244 * the end of enumeration is reached
2247 d_printf("Trusted domains list:\n\n");
2250 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2252 &trusted_dom_names, &domain_sids);
2254 if (NT_STATUS_IS_ERR(nt_status)) {
2255 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2256 nt_errstr(nt_status)));
2260 for (i = 0; i < num_domains; i++) {
2261 /* convert sid into ascii string */
2262 sid_to_string(ascii_sid, &(domain_sids[i]));
2264 /* calculate padding space for d_printf to look nicer */
2265 pad_len = col_len - strlen(trusted_dom_names[i]);
2266 padding[pad_len] = 0;
2267 do padding[--pad_len] = ' '; while (pad_len);
2269 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2273 * in case of no trusted domains say something rather
2274 * than just display blank line
2276 if (!num_domains) d_printf("none\n");
2278 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2280 /* close this connection before doing next one */
2281 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2282 if (NT_STATUS_IS_ERR(nt_status)) {
2283 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2284 nt_errstr(nt_status)));
2288 cli_nt_session_close(cli);
2291 * Listing trusting domains (stored in passdb backend, if local)
2294 d_printf("\nTrusting domains list:\n\n");
2297 * Open \PIPE\samr and get needed policy handles
2299 if (!cli_nt_session_open(cli, PI_SAMR)) {
2300 DEBUG(0, ("Could not initialise samr pipe\n"));
2305 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2307 if (!NT_STATUS_IS_OK(nt_status)) {
2308 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2309 nt_errstr(nt_status)));
2313 /* SamrOpenDomain - we have to open domain policy handle in order to be
2314 able to enumerate accounts*/
2315 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2316 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2317 queried_dom_sid, &domain_hnd);
2318 if (!NT_STATUS_IS_OK(nt_status)) {
2319 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2320 nt_errstr(nt_status)));
2325 * perform actual enumeration
2328 enum_ctx = 0; /* reset enumeration context from last enumeration */
2331 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2332 &enum_ctx, ACB_DOMTRUST, 0xffff,
2333 &trusting_dom_names, &trusting_dom_rids,
2335 if (NT_STATUS_IS_ERR(nt_status)) {
2336 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2337 nt_errstr(nt_status)));
2341 for (i = 0; i < num_domains; i++) {
2344 * get each single domain's sid (do we _really_ need this ?):
2345 * 1) connect to domain's pdc
2346 * 2) query the pdc for domain's sid
2349 /* get rid of '$' tail */
2350 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2351 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2352 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2354 /* calculate padding space for d_printf to look nicer */
2355 pad_len = col_len - strlen(trusting_dom_names[i]);
2356 padding[pad_len] = 0;
2357 do padding[--pad_len] = ' '; while (pad_len);
2359 /* set opt_* variables to remote domain */
2360 strupper_m(trusting_dom_names[i]);
2361 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2362 opt_target_workgroup = opt_workgroup;
2364 d_printf("%s%s", trusting_dom_names[i], padding);
2366 /* connect to remote domain controller */
2367 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2369 /* query for domain's sid */
2370 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2371 d_printf("couldn't get domain's sid\n");
2373 cli_shutdown(remote_cli);
2376 d_printf("domain controller is not responding\n");
2380 if (!num_domains) d_printf("none\n");
2382 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2384 /* close opened samr and domain policy handles */
2385 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2386 if (!NT_STATUS_IS_OK(nt_status)) {
2387 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2390 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2391 if (!NT_STATUS_IS_OK(nt_status)) {
2392 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2395 /* close samr pipe and connection to IPC$ */
2396 cli_nt_session_close(cli);
2399 talloc_destroy(mem_ctx);
2404 * Entrypoint for 'net rpc trustdom' code
2406 * @param argc standard argc
2407 * @param argv standard argv without initial components
2409 * @return Integer status (0 means success)
2412 static int rpc_trustdom(int argc, const char **argv)
2414 struct functable func[] = {
2415 {"add", rpc_trustdom_add},
2416 {"del", rpc_trustdom_del},
2417 {"establish", rpc_trustdom_establish},
2418 {"revoke", rpc_trustdom_revoke},
2419 {"help", rpc_trustdom_usage},
2420 {"list", rpc_trustdom_list},
2425 rpc_trustdom_usage(argc, argv);
2429 return (net_run_function(argc, argv, func, rpc_user_usage));
2433 * Check if a server will take rpc commands
2434 * @param flags Type of server to connect to (PDC, DMB, localhost)
2435 * if the host is not explicitly specified
2436 * @return BOOL (true means rpc supported)
2438 BOOL net_rpc_check(unsigned flags)
2440 struct cli_state cli;
2442 struct in_addr server_ip;
2443 char *server_name = NULL;
2445 /* flags (i.e. server type) may depend on command */
2446 if (!net_find_server(flags, &server_ip, &server_name))
2450 if (cli_initialise(&cli) == False)
2453 if (!cli_connect(&cli, server_name, &server_ip))
2455 if (!attempt_netbios_session_request(&cli, global_myname(),
2456 server_name, &server_ip))
2458 if (!cli_negprot(&cli))
2460 if (cli.protocol < PROTOCOL_NT1)
2470 /****************************************************************************/
2474 * Basic usage function for 'net rpc'
2475 * @param argc Standard main() style argc
2476 * @param argv Standard main() style argv. Initial components are already
2480 int net_rpc_usage(int argc, const char **argv)
2482 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2483 d_printf(" net rpc join \t\t\tto join a domain \n");
2484 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2485 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2486 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2487 d_printf(" net rpc group \t\tto list groups\n");
2488 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2489 d_printf(" net rpc file \t\t\tto list open files\n");
2490 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2491 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2492 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2493 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2494 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2495 "\t\t\t\t\tor establish trust\n");
2496 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2497 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2499 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2500 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2501 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2502 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2503 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2509 * Help function for 'net rpc'. Calls command specific help if requested
2510 * or displays usage of net rpc
2511 * @param argc Standard main() style argc
2512 * @param argv Standard main() style argv. Initial components are already
2516 int net_rpc_help(int argc, const char **argv)
2518 struct functable func[] = {
2519 {"join", rpc_join_usage},
2520 {"user", rpc_user_usage},
2521 {"group", rpc_group_usage},
2522 {"share", rpc_share_usage},
2523 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2524 {"trustdom", rpc_trustdom_usage},
2525 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2526 /*{"shutdown", rpc_shutdown_usage}, */
2531 net_rpc_usage(argc, argv);
2535 return (net_run_function(argc, argv, func, rpc_user_usage));
2540 * 'net rpc' entrypoint.
2541 * @param argc Standard main() style argc
2542 * @param argv Standard main() style argv. Initial components are already
2546 int net_rpc(int argc, const char **argv)
2548 struct functable func[] = {
2549 {"info", net_rpc_info},
2550 {"join", net_rpc_join},
2551 {"oldjoin", net_rpc_oldjoin},
2552 {"testjoin", net_rpc_testjoin},
2553 {"user", net_rpc_user},
2554 {"group", net_rpc_group},
2555 {"share", net_rpc_share},
2556 {"file", net_rpc_file},
2557 {"changetrustpw", net_rpc_changetrustpw},
2558 {"trustdom", rpc_trustdom},
2559 {"abortshutdown", rpc_shutdown_abort},
2560 {"shutdown", rpc_shutdown},
2561 {"samdump", rpc_samdump},
2562 {"vampire", rpc_vampire},
2563 {"getsid", net_rpc_getsid},
2564 {"help", net_rpc_help},
2567 return net_run_function(argc, argv, func, net_rpc_usage);