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 *, const char *,
41 struct cli_state *, TALLOC_CTX *, int, const char **);
44 * Many of the RPC functions need the domain sid. This function gets
45 * it at the start of every run
47 * @param cli A cli_state already connected to the remote machine
49 * @return The Domain SID of the remote machine.
52 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx, char **domain_name)
56 NTSTATUS result = NT_STATUS_OK;
57 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;
112 /* make use of cli_state handed over as an argument, if possible */
114 cli = net_make_ipc_connection(conn_flags);
124 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
125 DEBUG(0, ("talloc_init() failed\n"));
130 domain_sid = net_get_remote_domain_sid(cli, mem_ctx, &domain_name);
132 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
133 if (!cli_nt_session_open(cli, pipe_idx)) {
134 DEBUG(0, ("Could not initialise pipe\n"));
138 nt_status = fn(domain_sid, domain_name, cli, mem_ctx, argc, argv);
140 if (!NT_STATUS_IS_OK(nt_status)) {
141 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
143 DEBUG(5, ("rpc command function succedded\n"));
146 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
147 if (cli->nt_pipe_fnum)
148 cli_nt_session_close(cli);
151 /* close the connection only if it was opened here */
155 talloc_destroy(mem_ctx);
157 return (!NT_STATUS_IS_OK(nt_status));
161 /****************************************************************************/
165 * Force a change of the trust acccount password.
167 * All parameters are provided by the run_rpc_command function, except for
168 * argc, argv which are passes through.
170 * @param domain_sid The domain sid aquired from the remote server
171 * @param cli A cli_state connected to the server.
172 * @param mem_ctx Talloc context, destoyed on compleation of the function.
173 * @param argc Standard main() style argc
174 * @param argc Standard main() style argv. Initial components are already
177 * @return Normal NTSTATUS return.
180 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, const char *domain_name,
181 struct cli_state *cli, TALLOC_CTX *mem_ctx,
182 int argc, const char **argv) {
184 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
188 * Force a change of the trust acccount password.
190 * @param argc Standard main() style argc
191 * @param argc Standard main() style argv. Initial components are already
194 * @return A shell status integer (0 for success)
197 int net_rpc_changetrustpw(int argc, const char **argv)
199 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
200 rpc_changetrustpw_internals,
205 /****************************************************************************/
209 * Join a domain, the old way.
211 * This uses 'machinename' as the inital password, and changes it.
213 * The password should be created with 'server manager' or equiv first.
215 * All parameters are provided by the run_rpc_command function, except for
216 * argc, argv which are passes through.
218 * @param domain_sid The domain sid aquired from the remote server
219 * @param cli A cli_state connected to the server.
220 * @param mem_ctx Talloc context, destoyed on compleation of the function.
221 * @param argc Standard main() style argc
222 * @param argc Standard main() style argv. Initial components are already
225 * @return Normal NTSTATUS return.
228 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, const char *domain_name,
229 struct cli_state *cli,
231 int argc, const char **argv) {
233 fstring trust_passwd;
234 unsigned char orig_trust_passwd_hash[16];
236 uint32 sec_channel_type;
239 check what type of join - if the user want's to join as
240 a BDC, the server must agree that we are a BDC.
243 sec_channel_type = get_sec_channel_type(argv[0]);
245 sec_channel_type = get_sec_channel_type(NULL);
248 fstrcpy(trust_passwd, global_myname());
249 strlower_m(trust_passwd);
252 * Machine names can be 15 characters, but the max length on
253 * a password is 14. --jerry
256 trust_passwd[14] = '\0';
258 E_md4hash(trust_passwd, orig_trust_passwd_hash);
260 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
261 orig_trust_passwd_hash,
264 if (NT_STATUS_IS_OK(result))
265 printf("Joined domain %s.\n",opt_target_workgroup);
268 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
269 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
270 result = NT_STATUS_UNSUCCESSFUL;
277 * Join a domain, the old way.
279 * @param argc Standard main() style argc
280 * @param argc Standard main() style argv. Initial components are already
283 * @return A shell status integer (0 for success)
286 static int net_rpc_perform_oldjoin(int argc, const char **argv)
288 return run_rpc_command(NULL, PI_NETLOGON,
289 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
290 rpc_oldjoin_internals,
295 * Join a domain, the old way. This function exists to allow
296 * the message to be displayed when oldjoin was explicitly
297 * requested, but not when it was implied by "net rpc join"
299 * @param argc Standard main() style argc
300 * @param argc Standard main() style argv. Initial components are already
303 * @return A shell status integer (0 for success)
306 static int net_rpc_oldjoin(int argc, const char **argv)
308 int rc = net_rpc_perform_oldjoin(argc, argv);
311 d_printf("Failed to join domain\n");
318 * Basic usage function for 'net rpc join'
319 * @param argc Standard main() style argc
320 * @param argc Standard main() style argv. Initial components are already
324 static int rpc_join_usage(int argc, const char **argv)
326 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
327 "\t to join a domain with admin username & password\n"\
328 "\t\t password will be prompted if needed and none is specified\n"\
329 "\t <type> can be (default MEMBER)\n"\
330 "\t\t BDC - Join as a BDC\n"\
331 "\t\t PDC - Join as a PDC\n"\
332 "\t\t MEMBER - Join as a MEMBER server\n");
334 net_common_flags_usage(argc, argv);
339 * 'net rpc join' entrypoint.
340 * @param argc Standard main() style argc
341 * @param argc Standard main() style argv. Initial components are already
344 * Main 'net_rpc_join()' (where the admain username/password is used) is
346 * Try to just change the password, but if that doesn't work, use/prompt
347 * for a username/password.
350 int net_rpc_join(int argc, const char **argv)
352 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
355 return net_rpc_join_newstyle(argc, argv);
361 * display info about a rpc domain
363 * All parameters are provided by the run_rpc_command function, except for
364 * argc, argv which are passed through.
366 * @param domain_sid The domain sid acquired from the remote server
367 * @param cli A cli_state connected to the server.
368 * @param mem_ctx Talloc context, destoyed on completion of the function.
369 * @param argc Standard main() style argc
370 * @param argv Standard main() style argv. Initial components are already
373 * @return Normal NTSTATUS return.
377 rpc_info_internals(const DOM_SID *domain_sid, const char *domain_name,
378 struct cli_state *cli,
379 TALLOC_CTX *mem_ctx, int argc, const char **argv)
381 POLICY_HND connect_pol, domain_pol;
382 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
386 sid_to_string(sid_str, domain_sid);
388 /* Get sam policy handle */
389 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
391 if (!NT_STATUS_IS_OK(result)) {
395 /* Get domain policy handle */
396 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
397 MAXIMUM_ALLOWED_ACCESS,
398 domain_sid, &domain_pol);
399 if (!NT_STATUS_IS_OK(result)) {
404 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
406 if (NT_STATUS_IS_OK(result)) {
407 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
408 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
409 d_printf("Domain SID: %s\n", sid_str);
410 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
411 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
412 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
413 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
423 * 'net rpc info' entrypoint.
424 * @param argc Standard main() style argc
425 * @param argc Standard main() style argv. Initial components are already
428 int net_rpc_info(int argc, const char **argv)
430 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
437 * Fetch domain SID into the local secrets.tdb
439 * All parameters are provided by the run_rpc_command function, except for
440 * argc, argv which are passes through.
442 * @param domain_sid The domain sid acquired from the remote server
443 * @param cli A cli_state connected to the server.
444 * @param mem_ctx Talloc context, destoyed on completion of the function.
445 * @param argc Standard main() style argc
446 * @param argv Standard main() style argv. Initial components are already
449 * @return Normal NTSTATUS return.
453 rpc_getsid_internals(const DOM_SID *domain_sid, const char *domain_name,
454 struct cli_state *cli,
455 TALLOC_CTX *mem_ctx, int argc, const char **argv)
459 sid_to_string(sid_str, domain_sid);
460 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
461 sid_str, domain_name);
463 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
464 DEBUG(0,("Can't store domain SID\n"));
465 return NT_STATUS_UNSUCCESSFUL;
473 * 'net rpc getsid' entrypoint.
474 * @param argc Standard main() style argc
475 * @param argc Standard main() style argv. Initial components are already
478 int net_rpc_getsid(int argc, const char **argv)
480 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
481 rpc_getsid_internals,
486 /****************************************************************************/
489 * Basic usage function for 'net rpc user'
490 * @param argc Standard main() style argc.
491 * @param argv Standard main() style argv. Initial components are already
495 static int rpc_user_usage(int argc, const char **argv)
497 return net_help_user(argc, argv);
501 * Add a new user to a remote RPC server
503 * All parameters are provided by the run_rpc_command function, except for
504 * argc, argv which are passes through.
506 * @param domain_sid The domain sid acquired from the remote server
507 * @param cli A cli_state connected to the server.
508 * @param mem_ctx Talloc context, destoyed on completion of the function.
509 * @param argc Standard main() style argc
510 * @param argv Standard main() style argv. Initial components are already
513 * @return Normal NTSTATUS return.
516 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, const char *domain_name,
517 struct cli_state *cli, TALLOC_CTX *mem_ctx,
518 int argc, const char **argv) {
520 POLICY_HND connect_pol, domain_pol, user_pol;
521 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
522 const char *acct_name;
524 uint32 unknown, user_rid;
527 d_printf("User must be specified\n");
528 rpc_user_usage(argc, argv);
534 /* Get sam policy handle */
536 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
538 if (!NT_STATUS_IS_OK(result)) {
542 /* Get domain policy handle */
544 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
545 MAXIMUM_ALLOWED_ACCESS,
546 domain_sid, &domain_pol);
547 if (!NT_STATUS_IS_OK(result)) {
551 /* Create domain user */
553 acb_info = ACB_NORMAL;
554 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
556 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
557 acct_name, acb_info, unknown,
558 &user_pol, &user_rid);
559 if (!NT_STATUS_IS_OK(result)) {
564 if (!NT_STATUS_IS_OK(result)) {
565 d_printf("Failed to add user %s - %s\n", acct_name,
568 d_printf("Added user %s\n", acct_name);
574 * Add a new user to a remote RPC server
576 * @param argc Standard main() style argc
577 * @param argv Standard main() style argv. Initial components are already
580 * @return A shell status integer (0 for success)
583 static int rpc_user_add(int argc, const char **argv)
585 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
590 * Delete a user from a remote RPC server
592 * All parameters are provided by the run_rpc_command function, except for
593 * argc, argv which are passes through.
595 * @param domain_sid The domain sid acquired from the remote server
596 * @param cli A cli_state connected to the server.
597 * @param mem_ctx Talloc context, destoyed on completion of the function.
598 * @param argc Standard main() style argc
599 * @param argv Standard main() style argv. Initial components are already
602 * @return Normal NTSTATUS return.
605 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
606 const char *domain_name,
607 struct cli_state *cli,
609 int argc, const char **argv)
611 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
612 POLICY_HND connect_pol, domain_pol, user_pol;
615 d_printf("User must be specified\n");
616 rpc_user_usage(argc, argv);
619 /* Get sam policy and domain handles */
621 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
624 if (!NT_STATUS_IS_OK(result)) {
628 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
629 MAXIMUM_ALLOWED_ACCESS,
630 domain_sid, &domain_pol);
632 if (!NT_STATUS_IS_OK(result)) {
636 /* Get handle on user */
639 uint32 *user_rids, num_rids, *name_types;
640 uint32 flags = 0x000003e8; /* Unknown */
642 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
644 &num_rids, &user_rids,
647 if (!NT_STATUS_IS_OK(result)) {
651 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
652 MAXIMUM_ALLOWED_ACCESS,
653 user_rids[0], &user_pol);
655 if (!NT_STATUS_IS_OK(result)) {
662 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
664 if (!NT_STATUS_IS_OK(result)) {
668 /* Display results */
676 * Delete a user from a remote RPC server
678 * @param argc Standard main() style argc
679 * @param argv Standard main() style argv. Initial components are already
682 * @return A shell status integer (0 for success)
685 static int rpc_user_delete(int argc, const char **argv)
687 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
692 * Set a password for a user on a remote RPC server
694 * All parameters are provided by the run_rpc_command function, except for
695 * argc, argv which are passes through.
697 * @param domain_sid The domain sid acquired from the remote server
698 * @param cli A cli_state connected to the server.
699 * @param mem_ctx Talloc context, destoyed on completion of the function.
700 * @param argc Standard main() style argc
701 * @param argv Standard main() style argv. Initial components are already
704 * @return Normal NTSTATUS return.
707 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
708 const char *domain_name,
709 struct cli_state *cli,
711 int argc, const char **argv)
713 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
714 POLICY_HND connect_pol, domain_pol, user_pol;
715 SAM_USERINFO_CTR ctr;
716 SAM_USER_INFO_24 p24;
719 const char *new_password;
723 d_printf("User must be specified\n");
724 rpc_user_usage(argc, argv);
731 new_password = argv[1];
733 asprintf(&prompt, "Enter new password for %s:", user);
734 new_password = getpass(prompt);
738 /* Get sam policy and domain handles */
740 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
743 if (!NT_STATUS_IS_OK(result)) {
747 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
748 MAXIMUM_ALLOWED_ACCESS,
749 domain_sid, &domain_pol);
751 if (!NT_STATUS_IS_OK(result)) {
755 /* Get handle on user */
758 uint32 *user_rids, num_rids, *name_types;
759 uint32 flags = 0x000003e8; /* Unknown */
761 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
763 &num_rids, &user_rids,
766 if (!NT_STATUS_IS_OK(result)) {
770 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
771 MAXIMUM_ALLOWED_ACCESS,
772 user_rids[0], &user_pol);
774 if (!NT_STATUS_IS_OK(result)) {
779 /* Set password on account */
784 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
786 init_sam_user_info24(&p24, (char *)pwbuf,24);
788 ctr.switch_value = 24;
789 ctr.info.id24 = &p24;
791 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
792 &cli->user_session_key, &ctr);
794 if (!NT_STATUS_IS_OK(result)) {
798 /* Display results */
806 * Set a user's password on a remote RPC server
808 * @param argc Standard main() style argc
809 * @param argv Standard main() style argv. Initial components are already
812 * @return A shell status integer (0 for success)
815 static int rpc_user_password(int argc, const char **argv)
817 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
822 * List user's groups on a remote RPC server
824 * All parameters are provided by the run_rpc_command function, except for
825 * argc, argv which are passes through.
827 * @param domain_sid The domain sid acquired from the remote server
828 * @param cli A cli_state connected to the server.
829 * @param mem_ctx Talloc context, destoyed on completion of the function.
830 * @param argc Standard main() style argc
831 * @param argv Standard main() style argv. Initial components are already
834 * @return Normal NTSTATUS return.
838 rpc_user_info_internals(const DOM_SID *domain_sid, const char *domain_name,
839 struct cli_state *cli,
840 TALLOC_CTX *mem_ctx, int argc, const char **argv)
842 POLICY_HND connect_pol, domain_pol, user_pol;
843 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
844 uint32 *rids, num_rids, *name_types, num_names;
845 uint32 flags = 0x000003e8; /* Unknown */
851 d_printf("User must be specified\n");
852 rpc_user_usage(argc, argv);
855 /* Get sam policy handle */
857 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
859 if (!NT_STATUS_IS_OK(result)) goto done;
861 /* Get domain policy handle */
863 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
864 MAXIMUM_ALLOWED_ACCESS,
865 domain_sid, &domain_pol);
866 if (!NT_STATUS_IS_OK(result)) goto done;
868 /* Get handle on user */
870 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
872 &num_rids, &rids, &name_types);
874 if (!NT_STATUS_IS_OK(result)) goto done;
876 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
877 MAXIMUM_ALLOWED_ACCESS,
879 if (!NT_STATUS_IS_OK(result)) goto done;
881 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
882 &num_rids, &user_gids);
886 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
888 for (i = 0; i < num_rids; i++)
889 rids[i] = user_gids[i].g_rid;
891 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
892 flags, num_rids, rids,
893 &num_names, &names, &name_types);
895 if (!NT_STATUS_IS_OK(result)) {
899 /* Display results */
901 for (i = 0; i < num_names; i++)
902 printf("%s\n", names[i]);
909 * List a user's groups from a remote RPC server
911 * @param argc Standard main() style argc
912 * @param argv Standard main() style argv. Initial components are already
915 * @return A shell status integer (0 for success)
918 static int rpc_user_info(int argc, const char **argv)
920 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
925 * List users on a remote RPC server
927 * All parameters are provided by the run_rpc_command function, except for
928 * argc, argv which are passes through.
930 * @param domain_sid The domain sid acquired from the remote server
931 * @param cli A cli_state connected to the server.
932 * @param mem_ctx Talloc context, destoyed on completion of the function.
933 * @param argc Standard main() style argc
934 * @param argv Standard main() style argv. Initial components are already
937 * @return Normal NTSTATUS return.
941 rpc_user_list_internals(const DOM_SID *domain_sid, const char *domain_name,
942 struct cli_state *cli,
943 TALLOC_CTX *mem_ctx, int argc, const char **argv)
945 POLICY_HND connect_pol, domain_pol;
946 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
947 uint32 start_idx=0, num_entries, i, loop_count = 0;
948 SAM_DISPINFO_CTR ctr;
949 SAM_DISPINFO_1 info1;
951 /* Get sam policy handle */
953 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
955 if (!NT_STATUS_IS_OK(result)) {
959 /* Get domain policy handle */
961 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
962 MAXIMUM_ALLOWED_ACCESS,
963 domain_sid, &domain_pol);
964 if (!NT_STATUS_IS_OK(result)) {
968 /* Query domain users */
971 ctr.sam.info1 = &info1;
972 if (opt_long_list_entries)
973 d_printf("\nUser name Comment"\
974 "\n-----------------------------\n");
977 uint32 max_entries, max_size;
979 get_query_dispinfo_params(
980 loop_count, &max_entries, &max_size);
982 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
983 &start_idx, 1, &num_entries,
984 max_entries, max_size, &ctr);
987 for (i = 0; i < num_entries; i++) {
988 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
989 if (opt_long_list_entries)
990 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
992 if (opt_long_list_entries)
993 printf("%-21.21s %s\n", user, desc);
995 printf("%s\n", user);
997 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1004 * 'net rpc user' entrypoint.
1005 * @param argc Standard main() style argc
1006 * @param argc Standard main() style argv. Initial components are already
1010 int net_rpc_user(int argc, const char **argv)
1012 struct functable func[] = {
1013 {"add", rpc_user_add},
1014 {"info", rpc_user_info},
1015 {"delete", rpc_user_delete},
1016 {"password", rpc_user_password},
1021 if (opt_long_list_entries) {
1024 return run_rpc_command(NULL,PI_SAMR, 0,
1025 rpc_user_list_internals,
1029 return net_run_function(argc, argv, func, rpc_user_usage);
1033 /****************************************************************************/
1036 * Basic usage function for 'net rpc group'
1037 * @param argc Standard main() style argc.
1038 * @param argv Standard main() style argv. Initial components are already
1042 static int rpc_group_usage(int argc, const char **argv)
1044 return net_help_group(argc, argv);
1048 rpc_group_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1049 struct cli_state *cli,
1050 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1052 POLICY_HND connect_pol, domain_pol, group_pol;
1053 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1054 GROUP_INFO_CTR group_info;
1057 d_printf("Group name must be specified\n");
1058 rpc_group_usage(argc, argv);
1059 return NT_STATUS_OK;
1062 /* Get sam policy handle */
1064 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1066 if (!NT_STATUS_IS_OK(result)) goto done;
1068 /* Get domain policy handle */
1070 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1071 MAXIMUM_ALLOWED_ACCESS,
1072 domain_sid, &domain_pol);
1073 if (!NT_STATUS_IS_OK(result)) goto done;
1075 /* Create the group */
1077 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1078 argv[0], MAXIMUM_ALLOWED_ACCESS,
1080 if (!NT_STATUS_IS_OK(result)) goto done;
1082 if (strlen(opt_comment) == 0) goto done;
1084 /* We've got a comment to set */
1086 group_info.switch_value1 = 4;
1087 init_samr_group_info4(&group_info.group.info4, opt_comment);
1089 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &group_info);
1090 if (!NT_STATUS_IS_OK(result)) goto done;
1093 if (NT_STATUS_IS_OK(result))
1094 DEBUG(5, ("add group succeeded\n"));
1096 d_printf("add group failed: %s\n", nt_errstr(result));
1101 static int rpc_group_add(int argc, const char **argv)
1103 return run_rpc_command(NULL, PI_SAMR, 0,
1104 rpc_group_add_internals,
1109 * List groups on a remote RPC server
1111 * All parameters are provided by the run_rpc_command function, except for
1112 * argc, argv which are passes through.
1114 * @param domain_sid The domain sid acquired from the remote server
1115 * @param cli A cli_state connected to the server.
1116 * @param mem_ctx Talloc context, destoyed on completion of the function.
1117 * @param argc Standard main() style argc
1118 * @param argv Standard main() style argv. Initial components are already
1121 * @return Normal NTSTATUS return.
1125 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1126 struct cli_state *cli,
1127 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1129 POLICY_HND connect_pol, domain_pol;
1130 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1131 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1132 struct acct_info *groups;
1133 DOM_SID global_sid_Builtin;
1134 BOOL global = False;
1136 BOOL builtin = False;
1144 for (i=0; i<argc; i++) {
1145 if (strequal(argv[i], "global"))
1148 if (strequal(argv[i], "local"))
1151 if (strequal(argv[i], "builtin"))
1155 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1157 /* Get sam policy handle */
1159 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1161 if (!NT_STATUS_IS_OK(result)) {
1165 /* Get domain policy handle */
1167 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1168 MAXIMUM_ALLOWED_ACCESS,
1169 domain_sid, &domain_pol);
1170 if (!NT_STATUS_IS_OK(result)) {
1174 /* Query domain groups */
1175 if (opt_long_list_entries)
1176 d_printf("\nGroup name Comment"\
1177 "\n-----------------------------\n");
1179 SAM_DISPINFO_CTR ctr;
1180 SAM_DISPINFO_3 info3;
1185 ctr.sam.info3 = &info3;
1189 get_query_dispinfo_params(
1190 loop_count, &max_entries, &max_size);
1192 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1193 &start_idx, 3, &num_entries,
1194 max_entries, max_size, &ctr);
1196 if (!NT_STATUS_IS_OK(result) &&
1197 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1200 for (i = 0; i < num_entries; i++) {
1202 fstring group, desc;
1204 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1205 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1207 if (opt_long_list_entries)
1208 printf("%-21.21s %-50.50s\n",
1211 printf("%s\n", group);
1213 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1214 /* query domain aliases */
1219 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1220 &start_idx, max_entries,
1221 &groups, &num_entries);
1223 if (!NT_STATUS_IS_OK(result) &&
1224 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1227 for (i = 0; i < num_entries; i++) {
1229 char *description = NULL;
1231 if (opt_long_list_entries) {
1233 POLICY_HND alias_pol;
1236 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1241 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1244 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1246 description = unistr2_tdup(mem_ctx,
1247 &ctr.alias.info3.uni_acct_desc);
1251 if (description != NULL) {
1252 printf("%-21.21s %-50.50s\n",
1253 groups[i].acct_name,
1256 printf("%s\n", groups[i].acct_name);
1259 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1260 cli_samr_close(cli, mem_ctx, &domain_pol);
1261 /* Get builtin policy handle */
1263 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1264 MAXIMUM_ALLOWED_ACCESS,
1265 &global_sid_Builtin, &domain_pol);
1266 if (!NT_STATUS_IS_OK(result)) {
1269 /* query builtin aliases */
1272 if (!builtin) break;
1274 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1275 &start_idx, max_entries,
1276 &groups, &num_entries);
1278 if (!NT_STATUS_IS_OK(result) &&
1279 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1282 for (i = 0; i < num_entries; i++) {
1284 char *description = NULL;
1286 if (opt_long_list_entries) {
1288 POLICY_HND alias_pol;
1291 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1296 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1299 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1301 description = unistr2_tdup(mem_ctx,
1302 &ctr.alias.info3.uni_acct_desc);
1306 if (description != NULL) {
1307 printf("%-21.21s %-50.50s\n",
1308 groups[i].acct_name,
1311 printf("%s\n", groups[i].acct_name);
1314 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1320 static int rpc_group_list(int argc, const char **argv)
1322 return run_rpc_command(NULL, PI_SAMR, 0,
1323 rpc_group_list_internals,
1328 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1329 const char *domain_name, const DOM_SID *domain_sid,
1330 POLICY_HND *domain_pol, uint32 rid)
1333 POLICY_HND group_pol;
1334 uint32 num_members, *group_rids, *group_attrs;
1341 sid_to_string(sid_str, domain_sid);
1343 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
1344 MAXIMUM_ALLOWED_ACCESS,
1347 if (!NT_STATUS_IS_OK(result))
1350 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1351 &num_members, &group_rids,
1354 if (!NT_STATUS_IS_OK(result))
1357 while (num_members > 0) {
1358 int this_time = 512;
1360 if (num_members < this_time)
1361 this_time = num_members;
1363 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol, 1000,
1364 this_time, group_rids,
1365 &num_names, &names, &name_types);
1367 if (!NT_STATUS_IS_OK(result))
1370 /* We only have users as members, but make the output
1371 the same as the output of alias members */
1373 for (i = 0; i < this_time; i++) {
1375 if (opt_long_list_entries) {
1376 printf("%s-%d %s\\%s %d\n", sid_str,
1377 group_rids[i], domain_name, names[i],
1380 printf("%s\\%s\n", domain_name, names[i]);
1384 num_members -= this_time;
1388 return NT_STATUS_OK;
1392 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1393 POLICY_HND *domain_pol, uint32 rid)
1396 POLICY_HND alias_pol, lsa_pol;
1398 DOM_SID *alias_sids;
1404 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
1405 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
1407 if (!NT_STATUS_IS_OK(result))
1410 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
1411 &num_members, &alias_sids);
1413 if (!NT_STATUS_IS_OK(result)) {
1414 d_printf("Couldn't list alias members\n");
1418 if (num_members == 0) {
1419 return NT_STATUS_OK;
1422 cli_nt_session_close(cli);
1424 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1425 d_printf("Couldn't open LSA pipe\n");
1429 result = cli_lsa_open_policy(cli, mem_ctx, True,
1430 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1432 if (!NT_STATUS_IS_OK(result)) {
1433 d_printf("Couldn't open LSA policy handle\n");
1437 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
1439 &domains, &names, &types);
1441 if (!NT_STATUS_IS_OK(result) &&
1442 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
1443 d_printf("Couldn't lookup SIDs\n");
1447 for (i = 0; i < num_members; i++) {
1449 sid_to_string(sid_str, &alias_sids[i]);
1451 if (opt_long_list_entries) {
1452 printf("%s %s\\%s %d\n", sid_str,
1453 domains[i] ? domains[i] : "*unknown*",
1454 names[i] ? names[i] : "*unknown*", types[i]);
1457 printf("%s\\%s\n", domains[i], names[i]);
1459 printf("%s\n", sid_str);
1463 return NT_STATUS_OK;
1467 rpc_group_members_internals(const DOM_SID *domain_sid,
1468 const char *domain_name,
1469 struct cli_state *cli,
1470 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1473 POLICY_HND connect_pol, domain_pol;
1474 uint32 num_rids, *rids, *rid_types;
1476 /* Get sam policy handle */
1478 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1481 if (!NT_STATUS_IS_OK(result))
1484 /* Get domain policy handle */
1486 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1487 MAXIMUM_ALLOWED_ACCESS,
1488 domain_sid, &domain_pol);
1490 if (!NT_STATUS_IS_OK(result))
1493 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1494 1, argv, &num_rids, &rids, &rid_types);
1496 if (!NT_STATUS_IS_OK(result)) {
1498 /* Ok, did not find it in the global sam, try with builtin */
1500 DOM_SID sid_Builtin;
1502 cli_samr_close(cli, mem_ctx, &domain_pol);
1504 string_to_sid(&sid_Builtin, "S-1-5-32");
1506 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1507 MAXIMUM_ALLOWED_ACCESS,
1508 &sid_Builtin, &domain_pol);
1510 if (!NT_STATUS_IS_OK(result)) {
1511 d_printf("Couldn't find group %s\n", argv[0]);
1515 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1519 if (!NT_STATUS_IS_OK(result)) {
1520 d_printf("Couldn't find group %s\n", argv[0]);
1525 if (num_rids != 1) {
1526 d_printf("Couldn't find group %s\n", argv[0]);
1530 if (rid_types[0] == SID_NAME_DOM_GRP) {
1531 return rpc_list_group_members(cli, mem_ctx, domain_name,
1532 domain_sid, &domain_pol,
1536 if (rid_types[0] == SID_NAME_ALIAS) {
1537 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
1541 return NT_STATUS_NO_SUCH_GROUP;
1544 static int rpc_group_members(int argc, const char **argv)
1547 return rpc_group_usage(argc, argv);
1550 return run_rpc_command(NULL, PI_SAMR, 0,
1551 rpc_group_members_internals,
1556 * 'net rpc group' entrypoint.
1557 * @param argc Standard main() style argc
1558 * @param argc Standard main() style argv. Initial components are already
1562 int net_rpc_group(int argc, const char **argv)
1564 struct functable func[] = {
1565 {"add", rpc_group_add},
1567 {"delete", rpc_group_delete},
1569 {"list", rpc_group_list},
1570 {"members", rpc_group_members},
1575 if (opt_long_list_entries) {
1578 return run_rpc_command(NULL, PI_SAMR, 0,
1579 rpc_group_list_internals,
1583 return net_run_function(argc, argv, func, rpc_group_usage);
1586 /****************************************************************************/
1588 static int rpc_share_usage(int argc, const char **argv)
1590 return net_help_share(argc, argv);
1594 * Add a share on a remote RPC server
1596 * All parameters are provided by the run_rpc_command function, except for
1597 * argc, argv which are passes through.
1599 * @param domain_sid The domain sid acquired from the remote server
1600 * @param cli A cli_state connected to the server.
1601 * @param mem_ctx Talloc context, destoyed on completion of the function.
1602 * @param argc Standard main() style argc
1603 * @param argv Standard main() style argv. Initial components are already
1606 * @return Normal NTSTATUS return.
1609 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1610 struct cli_state *cli,
1611 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1614 char *sharename=talloc_strdup(mem_ctx, argv[0]);
1616 uint32 type=0; /* only allow disk shares to be added */
1617 uint32 num_users=0, perms=0;
1618 char *password=NULL; /* don't allow a share password */
1620 path = strchr(sharename, '=');
1622 return NT_STATUS_UNSUCCESSFUL;
1625 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1626 opt_comment, perms, opt_maxusers,
1627 num_users, path, password);
1628 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1631 static int rpc_share_add(int argc, const char **argv)
1633 if ((argc < 1) || !strchr(argv[0], '=')) {
1634 DEBUG(1,("Sharename or path not specified on add\n"));
1635 return rpc_share_usage(argc, argv);
1637 return run_rpc_command(NULL, PI_SRVSVC, 0,
1638 rpc_share_add_internals,
1643 * Delete a share on a remote RPC server
1645 * All parameters are provided by the run_rpc_command function, except for
1646 * argc, argv which are passes through.
1648 * @param domain_sid The domain sid acquired from the remote server
1649 * @param cli A cli_state connected to the server.
1650 * @param mem_ctx Talloc context, destoyed on completion of the function.
1651 * @param argc Standard main() style argc
1652 * @param argv Standard main() style argv. Initial components are already
1655 * @return Normal NTSTATUS return.
1658 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name,
1659 struct cli_state *cli,
1660 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1664 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1665 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1669 * Delete a share on a remote RPC server
1671 * @param domain_sid The domain sid acquired from the remote 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)
1678 static int rpc_share_delete(int argc, const char **argv)
1681 DEBUG(1,("Sharename not specified on delete\n"));
1682 return rpc_share_usage(argc, argv);
1684 return run_rpc_command(NULL, PI_SRVSVC, 0,
1685 rpc_share_del_internals,
1690 * Formatted print of share info
1692 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1695 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1697 fstring netname = "", remark = "";
1699 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1700 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1702 if (opt_long_list_entries) {
1703 d_printf("%-12s %-8.8s %-50s\n",
1704 netname, share_type[info1->info_1.type], remark);
1706 d_printf("%s\n", netname);
1712 * List shares on a remote RPC server
1714 * All parameters are provided by the run_rpc_command function, except for
1715 * argc, argv which are passes through.
1717 * @param domain_sid The domain sid acquired from the remote server
1718 * @param cli A cli_state connected to the server.
1719 * @param mem_ctx Talloc context, destoyed on completion of the function.
1720 * @param argc Standard main() style argc
1721 * @param argv Standard main() style argv. Initial components are already
1724 * @return Normal NTSTATUS return.
1728 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1729 struct cli_state *cli,
1730 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1732 SRV_SHARE_INFO_CTR ctr;
1735 uint32 preferred_len = 0xffffffff, i;
1737 init_enum_hnd(&hnd, 0);
1739 result = cli_srvsvc_net_share_enum(
1740 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1742 if (!W_ERROR_IS_OK(result))
1745 /* Display results */
1747 if (opt_long_list_entries) {
1749 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1750 "\nShare name Type Description\n"\
1751 "---------- ---- -----------\n");
1753 for (i = 0; i < ctr.num_entries; i++)
1754 display_share_info_1(&ctr.share.info1[i]);
1756 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1760 * 'net rpc share' entrypoint.
1761 * @param argc Standard main() style argc
1762 * @param argv Standard main() style argv. Initial components are already
1766 int net_rpc_share(int argc, const char **argv)
1768 struct functable func[] = {
1769 {"add", rpc_share_add},
1770 {"delete", rpc_share_delete},
1775 return run_rpc_command(NULL, PI_SRVSVC, 0,
1776 rpc_share_list_internals,
1779 return net_run_function(argc, argv, func, rpc_share_usage);
1782 /****************************************************************************/
1784 static int rpc_file_usage(int argc, const char **argv)
1786 return net_help_file(argc, argv);
1790 * Close a file on a remote RPC server
1792 * All parameters are provided by the run_rpc_command function, except for
1793 * argc, argv which are passes through.
1795 * @param domain_sid The domain sid acquired from the remote server
1796 * @param cli A cli_state connected to the server.
1797 * @param mem_ctx Talloc context, destoyed on completion of the function.
1798 * @param argc Standard main() style argc
1799 * @param argv Standard main() style argv. Initial components are already
1802 * @return Normal NTSTATUS return.
1805 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
1806 struct cli_state *cli,
1807 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1810 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1811 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1815 * Close a file on a remote RPC server
1817 * @param argc Standard main() style argc
1818 * @param argv Standard main() style argv. Initial components are already
1821 * @return A shell status integer (0 for success)
1823 static int rpc_file_close(int argc, const char **argv)
1826 DEBUG(1, ("No fileid given on close\n"));
1827 return(rpc_file_usage(argc, argv));
1830 return run_rpc_command(NULL, PI_SRVSVC, 0,
1831 rpc_file_close_internals,
1836 * Formatted print of open file info
1838 * @param info3 FILE_INFO_3 contents
1839 * @param str3 strings for FILE_INFO_3
1842 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1844 fstring user = "", path = "";
1846 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1847 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1849 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1850 info3->id, user, info3->perms, info3->num_locks, path);
1854 * List open files on a remote RPC server
1856 * All parameters are provided by the run_rpc_command function, except for
1857 * argc, argv which are passes through.
1859 * @param domain_sid The domain sid acquired from the remote server
1860 * @param cli A cli_state connected to the server.
1861 * @param mem_ctx Talloc context, destoyed on completion of the function.
1862 * @param argc Standard main() style argc
1863 * @param argv Standard main() style argv. Initial components are already
1866 * @return Normal NTSTATUS return.
1870 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1871 struct cli_state *cli,
1872 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1874 SRV_FILE_INFO_CTR ctr;
1877 uint32 preferred_len = 0xffffffff, i;
1878 const char *username=NULL;
1880 init_enum_hnd(&hnd, 0);
1882 /* if argc > 0, must be user command */
1884 username = smb_xstrdup(argv[0]);
1886 result = cli_srvsvc_net_file_enum(
1887 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1889 if (!W_ERROR_IS_OK(result))
1892 /* Display results */
1895 "\nEnumerating open files on remote server:\n\n"\
1896 "\nFileId Opened by Perms Locks Path"\
1897 "\n------ --------- ----- ----- ---- \n");
1898 for (i = 0; i < ctr.num_entries; i++)
1899 display_file_info_3(&ctr.file.info3[i].info_3,
1900 &ctr.file.info3[i].info_3_str);
1902 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1907 * List files for a user on a remote RPC server
1909 * @param argc Standard main() style argc
1910 * @param argv Standard main() style argv. Initial components are already
1913 * @return A shell status integer (0 for success)
1915 static int rpc_file_user(int argc, const char **argv)
1918 DEBUG(1, ("No username given\n"));
1919 return(rpc_file_usage(argc, argv));
1922 return run_rpc_command(NULL, PI_SRVSVC, 0,
1923 rpc_file_list_internals,
1929 * 'net rpc file' entrypoint.
1930 * @param argc Standard main() style argc
1931 * @param argv Standard main() style argv. Initial components are already
1935 int net_rpc_file(int argc, const char **argv)
1937 struct functable func[] = {
1938 {"close", rpc_file_close},
1939 {"user", rpc_file_user},
1941 {"info", rpc_file_info},
1947 return run_rpc_command(NULL, PI_SRVSVC, 0,
1948 rpc_file_list_internals,
1951 return net_run_function(argc, argv, func, rpc_file_usage);
1954 /****************************************************************************/
1959 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
1961 * All parameters are provided by the run_rpc_command function, except for
1962 * argc, argv which are passed through.
1964 * @param domain_sid The domain sid aquired from the remote server
1965 * @param cli A cli_state connected to the server.
1966 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1967 * @param argc Standard main() style argc
1968 * @param argv Standard main() style argv. Initial components are already
1971 * @return Normal NTSTATUS return.
1974 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
1975 const char *domain_name,
1976 struct cli_state *cli,
1977 TALLOC_CTX *mem_ctx,
1978 int argc, const char **argv)
1980 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1982 result = cli_shutdown_abort(cli, mem_ctx);
1984 if (NT_STATUS_IS_OK(result))
1985 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1987 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1994 * ABORT the shutdown of a remote RPC Server, over winreg pipe
1996 * All parameters are provided by the run_rpc_command function, except for
1997 * argc, argv which are passed through.
1999 * @param domain_sid The domain sid aquired from the remote server
2000 * @param cli A cli_state connected to the server.
2001 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2002 * @param argc Standard main() style argc
2003 * @param argv Standard main() style argv. Initial components are already
2006 * @return Normal NTSTATUS return.
2009 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
2010 const char *domain_name,
2011 struct cli_state *cli,
2012 TALLOC_CTX *mem_ctx,
2013 int argc, const char **argv)
2015 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2017 result = cli_reg_abort_shutdown(cli, mem_ctx);
2019 if (NT_STATUS_IS_OK(result))
2020 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
2022 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
2028 * ABORT the Shut down of a remote RPC server
2030 * @param argc Standard main() style argc
2031 * @param argv Standard main() style argv. Initial components are already
2034 * @return A shell status integer (0 for success)
2037 static int rpc_shutdown_abort(int argc, const char **argv)
2039 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
2040 rpc_shutdown_abort_internals,
2046 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
2048 return run_rpc_command(NULL, PI_WINREG, 0,
2049 rpc_reg_shutdown_abort_internals,
2054 * Shut down a remote RPC Server
2056 * All parameters are provided by the run_rpc_command function, except for
2057 * argc, argv which are passes through.
2059 * @param domain_sid The domain sid aquired from the remote server
2060 * @param cli A cli_state connected to the server.
2061 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2062 * @param argc Standard main() style argc
2063 * @param argc Standard main() style argv. Initial components are already
2066 * @return Normal NTSTATUS return.
2069 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid,
2070 const char *domain_name,
2071 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2072 int argc, const char **argv)
2074 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2075 const char *msg = "This machine will be shutdown shortly";
2076 uint32 timeout = 20;
2081 struct poptOption long_options[] = {
2082 {"message", 'm', POPT_ARG_STRING, &msg},
2083 {"timeout", 't', POPT_ARG_INT, &timeout},
2084 {"reboot", 'r', POPT_ARG_NONE, &reboot},
2085 {"force", 'f', POPT_ARG_NONE, &force},
2089 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
2090 POPT_CONTEXT_KEEP_FIRST);
2092 rc = poptGetNextOpt(pc);
2095 /* an error occurred during option processing */
2096 DEBUG(0, ("%s: %s\n",
2097 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
2099 return NT_STATUS_INVALID_PARAMETER;
2106 timeout = opt_timeout;
2109 /* create an entry */
2110 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
2112 if (NT_STATUS_IS_OK(result))
2113 DEBUG(5,("Shutdown of remote machine succeeded\n"));
2115 DEBUG(0,("Shutdown of remote machine failed!\n"));
2121 * Shut down a remote RPC server
2123 * @param argc Standard main() style argc
2124 * @param argc Standard main() style argv. Initial components are already
2127 * @return A shell status integer (0 for success)
2130 static int rpc_shutdown(int argc, const char **argv)
2132 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
2136 /***************************************************************************
2137 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
2139 ***************************************************************************/
2142 * Add interdomain trust account to the RPC server.
2143 * All parameters (except for argc and argv) are passed by run_rpc_command
2146 * @param domain_sid The domain sid acquired from the server
2147 * @param cli A cli_state connected to the server.
2148 * @param mem_ctx Talloc context, destoyed on completion of the function.
2149 * @param argc Standard main() style argc
2150 * @param argc Standard main() style argv. Initial components are already
2153 * @return normal NTSTATUS return code
2156 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
2157 const char *domain_name,
2158 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2159 int argc, const char **argv) {
2161 POLICY_HND connect_pol, domain_pol, user_pol;
2162 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2165 uint32 unknown, user_rid;
2168 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
2169 return NT_STATUS_INVALID_PARAMETER;
2173 * Make valid trusting domain account (ie. uppercased and with '$' appended)
2176 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
2177 return NT_STATUS_NO_MEMORY;
2180 strupper_m(acct_name);
2182 /* Get samr policy handle */
2183 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2185 if (!NT_STATUS_IS_OK(result)) {
2189 /* Get domain policy handle */
2190 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2191 MAXIMUM_ALLOWED_ACCESS,
2192 domain_sid, &domain_pol);
2193 if (!NT_STATUS_IS_OK(result)) {
2197 /* Create trusting domain's account */
2198 acb_info = ACB_DOMTRUST;
2199 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
2200 mimir: yes, most probably it is */
2202 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
2203 acct_name, acb_info, unknown,
2204 &user_pol, &user_rid);
2205 if (!NT_STATUS_IS_OK(result)) {
2210 SAM_USERINFO_CTR ctr;
2211 SAM_USER_INFO_24 p24;
2214 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
2219 init_sam_user_info24(&p24, (char *)pwbuf, 24);
2221 ctr.switch_value = 24;
2222 ctr.info.id24 = &p24;
2224 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
2225 &cli->user_session_key, &ctr);
2227 if (!NT_STATUS_IS_OK(result)) {
2228 DEBUG(0,("Could not set trust account password: %s\n",
2229 nt_errstr(result)));
2235 SAFE_FREE(acct_name);
2240 * Create interdomain trust account for a remote domain.
2242 * @param argc standard argc
2243 * @param argv standard argv without initial components
2245 * @return Integer status (0 means success)
2248 static int rpc_trustdom_add(int argc, const char **argv)
2251 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
2254 d_printf("Usage: net rpc trustdom add <domain>\n");
2261 * Delete interdomain trust account for a remote domain.
2263 * @param argc standard argc
2264 * @param argv standard argv without initial components
2266 * @return Integer status (0 means success)
2269 static int rpc_trustdom_del(int argc, const char **argv)
2271 d_printf("Sorry, not yet implemented.\n");
2272 d_printf("Use 'smbpasswd -x -i' instead.\n");
2278 * Establish trust relationship to a trusting domain.
2279 * Interdomain account must already be created on remote PDC.
2281 * @param argc standard argc
2282 * @param argv standard argv without initial components
2284 * @return Integer status (0 means success)
2287 static int rpc_trustdom_establish(int argc, const char **argv)
2289 struct cli_state *cli;
2290 struct in_addr server_ip;
2291 POLICY_HND connect_hnd;
2292 TALLOC_CTX *mem_ctx;
2294 DOM_SID *domain_sid;
2295 WKS_INFO_100 wks_info;
2298 char* domain_name_pol;
2303 * Connect to \\server\ipc$ as 'our domain' account with password
2307 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
2311 domain_name = smb_xstrdup(argv[0]);
2312 strupper_m(domain_name);
2314 /* account name used at first is our domain's name with '$' */
2315 asprintf(&acct_name, "%s$", lp_workgroup());
2316 strupper_m(acct_name);
2319 * opt_workgroup will be used by connection functions further,
2320 * hence it should be set to remote domain name instead of ours
2322 if (opt_workgroup) {
2323 opt_workgroup = smb_xstrdup(domain_name);
2326 opt_user_name = acct_name;
2328 /* find the domain controller */
2329 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
2330 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
2334 /* connect to ipc$ as username/password */
2335 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
2336 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
2338 /* Is it trusting domain account for sure ? */
2339 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
2340 nt_errstr(nt_status)));
2345 * Connect to \\server\ipc$ again (this time anonymously)
2348 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
2350 if (NT_STATUS_IS_ERR(nt_status)) {
2351 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
2352 domain_name, nt_errstr(nt_status)));
2356 * Use NetServerEnum2 to make sure we're talking to a proper server
2359 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
2360 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2361 for domain %s\n", domain_name));
2365 * Call WksQueryInfo to check remote server's capabilities
2366 * note: It is now used only to get unicode domain name
2369 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
2370 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2374 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
2376 DEBUG(0, ("talloc_init() failed\n"));
2381 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
2383 if (NT_STATUS_IS_ERR(nt_status)) {
2384 DEBUG(0, ("WksQueryInfo call failed.\n"));
2388 if (cli->nt_pipe_fnum)
2389 cli_nt_session_close(cli);
2393 * Call LsaOpenPolicy and LsaQueryInfo
2396 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
2397 DEBUG(0, ("talloc_init() failed\n"));
2402 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2403 DEBUG(0, ("Could not initialise lsa pipe\n"));
2408 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
2410 if (NT_STATUS_IS_ERR(nt_status)) {
2411 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2412 nt_errstr(nt_status)));
2416 /* Querying info level 5 */
2418 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
2419 5 /* info level */, &domain_name_pol,
2421 if (NT_STATUS_IS_ERR(nt_status)) {
2422 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2423 nt_errstr(nt_status)));
2430 /* There should be actually query info level 3 (following nt serv behaviour),
2431 but I still don't know if it's _really_ necessary */
2434 * Store the password in secrets db
2437 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
2438 wks_info.uni_lan_grp.uni_str_len, opt_password,
2440 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2445 * Close the pipes and clean up
2448 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2449 if (NT_STATUS_IS_ERR(nt_status)) {
2450 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2451 nt_errstr(nt_status)));
2455 if (cli->nt_pipe_fnum)
2456 cli_nt_session_close(cli);
2458 talloc_destroy(mem_ctx);
2460 DEBUG(0, ("Success!\n"));
2465 * Revoke trust relationship to the remote domain
2467 * @param argc standard argc
2468 * @param argv standard argv without initial components
2470 * @return Integer status (0 means success)
2473 static int rpc_trustdom_revoke(int argc, const char **argv)
2477 if (argc < 1) return -1;
2479 /* generate upper cased domain name */
2480 domain_name = smb_xstrdup(argv[0]);
2481 strupper_m(domain_name);
2483 /* delete password of the trust */
2484 if (!trusted_domain_password_delete(domain_name)) {
2485 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2494 * Usage for 'net rpc trustdom' command
2496 * @param argc standard argc
2497 * @param argv standard argv without inital components
2499 * @return Integer status returned to shell
2502 static int rpc_trustdom_usage(int argc, const char **argv)
2504 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
2505 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
2506 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
2507 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2508 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
2513 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
2514 const char *domain_name,
2515 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2516 int argc, const char **argv)
2519 sid_to_string(str_sid, domain_sid);
2520 d_printf("%s\n", str_sid);
2521 return NT_STATUS_OK;
2525 static int rpc_trustdom_list(int argc, const char **argv)
2527 /* common variables */
2528 TALLOC_CTX* mem_ctx;
2529 struct cli_state *cli, *remote_cli;
2531 const char *domain_name = NULL;
2532 DOM_SID *queried_dom_sid;
2533 fstring ascii_sid, padding;
2534 int ascii_dom_name_len;
2535 POLICY_HND connect_hnd;
2537 /* trusted domains listing variables */
2538 unsigned int num_domains, enum_ctx = 0;
2539 int i, pad_len, col_len = 20;
2540 DOM_SID *domain_sids;
2541 char **trusted_dom_names;
2545 /* trusting domains listing variables */
2546 POLICY_HND domain_hnd;
2547 char **trusting_dom_names;
2548 uint32 *trusting_dom_rids;
2551 * Listing trusted domains (stored in secrets.tdb, if local)
2554 mem_ctx = talloc_init("trust relationships listing");
2557 * set domain and pdc name to local samba server (default)
2558 * or to remote one given in command line
2561 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2562 domain_name = opt_workgroup;
2563 opt_target_workgroup = opt_workgroup;
2565 fstrcpy(pdc_name, global_myname());
2566 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2567 opt_target_workgroup = domain_name;
2570 /* open \PIPE\lsarpc and open policy handle */
2571 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2572 DEBUG(0, ("Couldn't connect to domain controller\n"));
2576 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2577 DEBUG(0, ("Could not initialise lsa pipe\n"));
2581 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2583 if (NT_STATUS_IS_ERR(nt_status)) {
2584 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2585 nt_errstr(nt_status)));
2589 /* query info level 5 to obtain sid of a domain being queried */
2590 nt_status = cli_lsa_query_info_policy(
2591 cli, mem_ctx, &connect_hnd, 5 /* info level */,
2592 &dummy, &queried_dom_sid);
2594 if (NT_STATUS_IS_ERR(nt_status)) {
2595 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2596 nt_errstr(nt_status)));
2601 * Keep calling LsaEnumTrustdom over opened pipe until
2602 * the end of enumeration is reached
2605 d_printf("Trusted domains list:\n\n");
2608 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2610 &trusted_dom_names, &domain_sids);
2612 if (NT_STATUS_IS_ERR(nt_status)) {
2613 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2614 nt_errstr(nt_status)));
2618 for (i = 0; i < num_domains; i++) {
2619 /* convert sid into ascii string */
2620 sid_to_string(ascii_sid, &(domain_sids[i]));
2622 /* calculate padding space for d_printf to look nicer */
2623 pad_len = col_len - strlen(trusted_dom_names[i]);
2624 padding[pad_len] = 0;
2625 do padding[--pad_len] = ' '; while (pad_len);
2627 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2631 * in case of no trusted domains say something rather
2632 * than just display blank line
2634 if (!num_domains) d_printf("none\n");
2636 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2638 /* close this connection before doing next one */
2639 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2640 if (NT_STATUS_IS_ERR(nt_status)) {
2641 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2642 nt_errstr(nt_status)));
2646 cli_nt_session_close(cli);
2649 * Listing trusting domains (stored in passdb backend, if local)
2652 d_printf("\nTrusting domains list:\n\n");
2655 * Open \PIPE\samr and get needed policy handles
2657 if (!cli_nt_session_open(cli, PI_SAMR)) {
2658 DEBUG(0, ("Could not initialise samr pipe\n"));
2663 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2665 if (!NT_STATUS_IS_OK(nt_status)) {
2666 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2667 nt_errstr(nt_status)));
2671 /* SamrOpenDomain - we have to open domain policy handle in order to be
2672 able to enumerate accounts*/
2673 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2674 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2675 queried_dom_sid, &domain_hnd);
2676 if (!NT_STATUS_IS_OK(nt_status)) {
2677 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2678 nt_errstr(nt_status)));
2683 * perform actual enumeration
2686 enum_ctx = 0; /* reset enumeration context from last enumeration */
2689 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2690 &enum_ctx, ACB_DOMTRUST, 0xffff,
2691 &trusting_dom_names, &trusting_dom_rids,
2693 if (NT_STATUS_IS_ERR(nt_status)) {
2694 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2695 nt_errstr(nt_status)));
2699 for (i = 0; i < num_domains; i++) {
2702 * get each single domain's sid (do we _really_ need this ?):
2703 * 1) connect to domain's pdc
2704 * 2) query the pdc for domain's sid
2707 /* get rid of '$' tail */
2708 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2709 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2710 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2712 /* calculate padding space for d_printf to look nicer */
2713 pad_len = col_len - strlen(trusting_dom_names[i]);
2714 padding[pad_len] = 0;
2715 do padding[--pad_len] = ' '; while (pad_len);
2717 /* set opt_* variables to remote domain */
2718 strupper_m(trusting_dom_names[i]);
2719 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2720 opt_target_workgroup = opt_workgroup;
2722 d_printf("%s%s", trusting_dom_names[i], padding);
2724 /* connect to remote domain controller */
2725 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2727 /* query for domain's sid */
2728 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2729 d_printf("couldn't get domain's sid\n");
2731 cli_shutdown(remote_cli);
2734 d_printf("domain controller is not responding\n");
2738 if (!num_domains) d_printf("none\n");
2740 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2742 /* close opened samr and domain policy handles */
2743 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2744 if (!NT_STATUS_IS_OK(nt_status)) {
2745 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2748 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2749 if (!NT_STATUS_IS_OK(nt_status)) {
2750 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2753 /* close samr pipe and connection to IPC$ */
2754 cli_nt_session_close(cli);
2757 talloc_destroy(mem_ctx);
2762 * Entrypoint for 'net rpc trustdom' code
2764 * @param argc standard argc
2765 * @param argv standard argv without initial components
2767 * @return Integer status (0 means success)
2770 static int rpc_trustdom(int argc, const char **argv)
2772 struct functable func[] = {
2773 {"add", rpc_trustdom_add},
2774 {"del", rpc_trustdom_del},
2775 {"establish", rpc_trustdom_establish},
2776 {"revoke", rpc_trustdom_revoke},
2777 {"help", rpc_trustdom_usage},
2778 {"list", rpc_trustdom_list},
2783 rpc_trustdom_usage(argc, argv);
2787 return (net_run_function(argc, argv, func, rpc_user_usage));
2791 * Check if a server will take rpc commands
2792 * @param flags Type of server to connect to (PDC, DMB, localhost)
2793 * if the host is not explicitly specified
2794 * @return BOOL (true means rpc supported)
2796 BOOL net_rpc_check(unsigned flags)
2798 struct cli_state cli;
2800 struct in_addr server_ip;
2801 char *server_name = NULL;
2803 /* flags (i.e. server type) may depend on command */
2804 if (!net_find_server(flags, &server_ip, &server_name))
2808 if (cli_initialise(&cli) == False)
2811 if (!cli_connect(&cli, server_name, &server_ip))
2813 if (!attempt_netbios_session_request(&cli, global_myname(),
2814 server_name, &server_ip))
2816 if (!cli_negprot(&cli))
2818 if (cli.protocol < PROTOCOL_NT1)
2827 /* dump sam database via samsync rpc calls */
2828 static int rpc_samdump(int argc, const char **argv) {
2829 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
2833 /* syncronise sam database via samsync rpc calls */
2834 static int rpc_vampire(int argc, const char **argv) {
2835 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
2838 /****************************************************************************/
2842 * Basic usage function for 'net rpc'
2843 * @param argc Standard main() style argc
2844 * @param argv Standard main() style argv. Initial components are already
2848 int net_rpc_usage(int argc, const char **argv)
2850 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2851 d_printf(" net rpc join \t\t\tto join a domain \n");
2852 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2853 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2854 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2855 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass");
2856 d_printf(" net rpc group \t\tto list groups\n");
2857 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2858 d_printf(" net rpc file \t\t\tto list open files\n");
2859 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2860 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2861 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2862 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2863 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2864 "\t\t\t\t\tor establish trust\n");
2865 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2866 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2868 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2869 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2870 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2871 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2872 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2878 * Help function for 'net rpc'. Calls command specific help if requested
2879 * or displays usage of net rpc
2880 * @param argc Standard main() style argc
2881 * @param argv Standard main() style argv. Initial components are already
2885 int net_rpc_help(int argc, const char **argv)
2887 struct functable func[] = {
2888 {"join", rpc_join_usage},
2889 {"user", rpc_user_usage},
2890 {"group", rpc_group_usage},
2891 {"share", rpc_share_usage},
2892 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2893 {"trustdom", rpc_trustdom_usage},
2894 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2895 /*{"shutdown", rpc_shutdown_usage}, */
2900 net_rpc_usage(argc, argv);
2904 return (net_run_function(argc, argv, func, rpc_user_usage));
2909 * 'net rpc' entrypoint.
2910 * @param argc Standard main() style argc
2911 * @param argv Standard main() style argv. Initial components are already
2915 int net_rpc(int argc, const char **argv)
2917 struct functable func[] = {
2918 {"info", net_rpc_info},
2919 {"join", net_rpc_join},
2920 {"oldjoin", net_rpc_oldjoin},
2921 {"testjoin", net_rpc_testjoin},
2922 {"user", net_rpc_user},
2923 {"password", rpc_user_password},
2924 {"group", net_rpc_group},
2925 {"share", net_rpc_share},
2926 {"file", net_rpc_file},
2927 {"changetrustpw", net_rpc_changetrustpw},
2928 {"trustdom", rpc_trustdom},
2929 {"abortshutdown", rpc_shutdown_abort},
2930 {"shutdown", rpc_shutdown},
2931 {"samdump", rpc_samdump},
2932 {"vampire", rpc_vampire},
2933 {"getsid", net_rpc_getsid},
2934 {"help", net_rpc_help},
2937 return net_run_function(argc, argv, func, net_rpc_usage);