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)
6 Copyright (C) 2004 Guenther Deschner (gd@samba.org)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "utils/net.h"
25 static int net_mode_share;
30 * @brief RPC based subcommands for the 'net' utility.
32 * This file should contain much of the functionality that used to
33 * be found in rpcclient, execpt that the commands should change
34 * less often, and the fucntionality should be sane (the user is not
35 * expected to know a rid/sid before they conduct an operation etc.)
37 * @todo Perhaps eventually these should be split out into a number
38 * of files, as this could get quite big.
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, char **domain_name)
55 NTSTATUS result = NT_STATUS_OK;
56 uint32 info_class = 5;
58 if (!cli_nt_session_open (cli, PI_LSARPC)) {
59 fprintf(stderr, "could not initialise lsa pipe\n");
63 result = cli_lsa_open_policy(cli, mem_ctx, False,
64 SEC_RIGHTS_MAXIMUM_ALLOWED,
66 if (!NT_STATUS_IS_OK(result)) {
70 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
71 domain_name, &domain_sid);
72 if (!NT_STATUS_IS_OK(result)) {
74 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
76 if (!NT_STATUS_IS_OK(result)) {
77 fprintf(stderr, "error: %s\n", nt_errstr(result));
83 cli_lsa_close(cli, mem_ctx, &pol);
84 cli_nt_session_close(cli);
90 * Run a single RPC command, from start to finish.
92 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
93 * @param conn_flag a NET_FLAG_ combination. Passed to
94 * net_make_ipc_connection.
95 * @param argc Standard main() style argc
96 * @param argc Standard main() style argv. Initial components are already
98 * @return A shell status integer (0 for success)
101 int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
103 int argc, const char **argv)
105 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, &domain_name);
131 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
132 if (!cli_nt_session_open(cli, pipe_idx)) {
133 DEBUG(0, ("Could not initialise pipe\n"));
137 nt_status = fn(domain_sid, domain_name, cli, mem_ctx, argc, argv);
139 if (!NT_STATUS_IS_OK(nt_status)) {
140 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
142 DEBUG(5, ("rpc command function succedded\n"));
145 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
146 if (cli->pipes[cli->pipe_idx].fnum)
147 cli_nt_session_close(cli);
150 /* close the connection only if it was opened here */
154 talloc_destroy(mem_ctx);
156 return (!NT_STATUS_IS_OK(nt_status));
160 /****************************************************************************/
164 * Force a change of the trust acccount password.
166 * All parameters are provided by the run_rpc_command function, except for
167 * argc, argv which are passes through.
169 * @param domain_sid The domain sid aquired from the remote server
170 * @param cli A cli_state connected to the server.
171 * @param mem_ctx Talloc context, destoyed on compleation of the function.
172 * @param argc Standard main() style argc
173 * @param argc Standard main() style argv. Initial components are already
176 * @return Normal NTSTATUS return.
179 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, const char *domain_name,
180 struct cli_state *cli, TALLOC_CTX *mem_ctx,
181 int argc, const char **argv) {
183 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
187 * Force a change of the trust acccount password.
189 * @param argc Standard main() style argc
190 * @param argc Standard main() style argv. Initial components are already
193 * @return A shell status integer (0 for success)
196 int net_rpc_changetrustpw(int argc, const char **argv)
198 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
199 rpc_changetrustpw_internals,
204 /****************************************************************************/
208 * Join a domain, the old way.
210 * This uses 'machinename' as the inital password, and changes it.
212 * The password should be created with 'server manager' or equiv first.
214 * All parameters are provided by the run_rpc_command function, except for
215 * argc, argv which are passes through.
217 * @param domain_sid The domain sid aquired from the remote server
218 * @param cli A cli_state connected to the server.
219 * @param mem_ctx Talloc context, destoyed on compleation of the function.
220 * @param argc Standard main() style argc
221 * @param argc Standard main() style argv. Initial components are already
224 * @return Normal NTSTATUS return.
227 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, const char *domain_name,
228 struct cli_state *cli,
230 int argc, const char **argv) {
232 fstring trust_passwd;
233 unsigned char orig_trust_passwd_hash[16];
235 uint32 sec_channel_type;
238 check what type of join - if the user want's to join as
239 a BDC, the server must agree that we are a BDC.
242 sec_channel_type = get_sec_channel_type(argv[0]);
244 sec_channel_type = get_sec_channel_type(NULL);
247 fstrcpy(trust_passwd, global_myname());
248 strlower_m(trust_passwd);
251 * Machine names can be 15 characters, but the max length on
252 * a password is 14. --jerry
255 trust_passwd[14] = '\0';
257 E_md4hash(trust_passwd, orig_trust_passwd_hash);
259 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
260 orig_trust_passwd_hash,
263 if (NT_STATUS_IS_OK(result))
264 printf("Joined domain %s.\n",opt_target_workgroup);
267 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
268 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
269 result = NT_STATUS_UNSUCCESSFUL;
276 * Join a domain, the old way.
278 * @param argc Standard main() style argc
279 * @param argc Standard main() style argv. Initial components are already
282 * @return A shell status integer (0 for success)
285 static int net_rpc_perform_oldjoin(int argc, const char **argv)
287 return run_rpc_command(NULL, PI_NETLOGON,
288 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
289 rpc_oldjoin_internals,
294 * Join a domain, the old way. This function exists to allow
295 * the message to be displayed when oldjoin was explicitly
296 * requested, but not when it was implied by "net rpc join"
298 * @param argc Standard main() style argc
299 * @param argc Standard main() style argv. Initial components are already
302 * @return A shell status integer (0 for success)
305 static int net_rpc_oldjoin(int argc, const char **argv)
307 int rc = net_rpc_perform_oldjoin(argc, argv);
310 d_printf("Failed to join domain\n");
317 * Basic usage function for 'net rpc join'
318 * @param argc Standard main() style argc
319 * @param argc Standard main() style argv. Initial components are already
323 static int rpc_join_usage(int argc, const char **argv)
325 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
326 "\t to join a domain with admin username & password\n"\
327 "\t\t password will be prompted if needed and none is specified\n"\
328 "\t <type> can be (default MEMBER)\n"\
329 "\t\t BDC - Join as a BDC\n"\
330 "\t\t PDC - Join as a PDC\n"\
331 "\t\t MEMBER - Join as a MEMBER server\n");
333 net_common_flags_usage(argc, argv);
338 * 'net rpc join' entrypoint.
339 * @param argc Standard main() style argc
340 * @param argc Standard main() style argv. Initial components are already
343 * Main 'net_rpc_join()' (where the admain username/password is used) is
345 * Try to just change the password, but if that doesn't work, use/prompt
346 * for a username/password.
349 int net_rpc_join(int argc, const char **argv)
351 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
354 return net_rpc_join_newstyle(argc, argv);
360 * display info about a rpc domain
362 * All parameters are provided by the run_rpc_command function, except for
363 * argc, argv which are passed through.
365 * @param domain_sid The domain sid acquired from the remote server
366 * @param cli A cli_state connected to the server.
367 * @param mem_ctx Talloc context, destoyed on completion of the function.
368 * @param argc Standard main() style argc
369 * @param argv Standard main() style argv. Initial components are already
372 * @return Normal NTSTATUS return.
376 rpc_info_internals(const DOM_SID *domain_sid, const char *domain_name,
377 struct cli_state *cli,
378 TALLOC_CTX *mem_ctx, int argc, const char **argv)
380 POLICY_HND connect_pol, domain_pol;
381 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
385 sid_to_string(sid_str, domain_sid);
387 /* Get sam policy handle */
388 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
390 if (!NT_STATUS_IS_OK(result)) {
394 /* Get domain policy handle */
395 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
396 MAXIMUM_ALLOWED_ACCESS,
397 domain_sid, &domain_pol);
398 if (!NT_STATUS_IS_OK(result)) {
403 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
405 if (NT_STATUS_IS_OK(result)) {
406 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
407 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
408 d_printf("Domain SID: %s\n", sid_str);
409 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num.low);
410 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
411 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
412 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
422 * 'net rpc info' entrypoint.
423 * @param argc Standard main() style argc
424 * @param argc Standard main() style argv. Initial components are already
427 int net_rpc_info(int argc, const char **argv)
429 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
436 * Fetch domain SID into the local secrets.tdb
438 * All parameters are provided by the run_rpc_command function, except for
439 * argc, argv which are passes through.
441 * @param domain_sid The domain sid acquired from the remote server
442 * @param cli A cli_state connected to the server.
443 * @param mem_ctx Talloc context, destoyed on completion of the function.
444 * @param argc Standard main() style argc
445 * @param argv Standard main() style argv. Initial components are already
448 * @return Normal NTSTATUS return.
452 rpc_getsid_internals(const DOM_SID *domain_sid, const char *domain_name,
453 struct cli_state *cli,
454 TALLOC_CTX *mem_ctx, int argc, const char **argv)
458 sid_to_string(sid_str, domain_sid);
459 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
460 sid_str, domain_name);
462 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
463 DEBUG(0,("Can't store domain SID\n"));
464 return NT_STATUS_UNSUCCESSFUL;
472 * 'net rpc getsid' entrypoint.
473 * @param argc Standard main() style argc
474 * @param argc Standard main() style argv. Initial components are already
477 int net_rpc_getsid(int argc, const char **argv)
479 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
480 rpc_getsid_internals,
485 /****************************************************************************/
488 * Basic usage function for 'net rpc user'
489 * @param argc Standard main() style argc.
490 * @param argv Standard main() style argv. Initial components are already
494 static int rpc_user_usage(int argc, const char **argv)
496 return net_help_user(argc, argv);
500 * Add a new user to a remote RPC server
502 * All parameters are provided by the run_rpc_command function, except for
503 * argc, argv which are passes through.
505 * @param domain_sid The domain sid acquired from the remote server
506 * @param cli A cli_state connected to the server.
507 * @param mem_ctx Talloc context, destoyed on completion of the function.
508 * @param argc Standard main() style argc
509 * @param argv Standard main() style argv. Initial components are already
512 * @return Normal NTSTATUS return.
515 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, const char *domain_name,
516 struct cli_state *cli, TALLOC_CTX *mem_ctx,
517 int argc, const char **argv) {
519 POLICY_HND connect_pol, domain_pol, user_pol;
520 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
521 const char *acct_name;
523 uint32 unknown, user_rid;
526 d_printf("User must be specified\n");
527 rpc_user_usage(argc, argv);
533 /* Get sam policy handle */
535 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
537 if (!NT_STATUS_IS_OK(result)) {
541 /* Get domain policy handle */
543 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
544 MAXIMUM_ALLOWED_ACCESS,
545 domain_sid, &domain_pol);
546 if (!NT_STATUS_IS_OK(result)) {
550 /* Create domain user */
552 acb_info = ACB_NORMAL;
553 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
555 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
556 acct_name, acb_info, unknown,
557 &user_pol, &user_rid);
558 if (!NT_STATUS_IS_OK(result)) {
563 if (!NT_STATUS_IS_OK(result)) {
564 d_printf("Failed to add user %s - %s\n", acct_name,
567 d_printf("Added user %s\n", acct_name);
573 * Add a new user to a remote RPC server
575 * @param argc Standard main() style argc
576 * @param argv Standard main() style argv. Initial components are already
579 * @return A shell status integer (0 for success)
582 static int rpc_user_add(int argc, const char **argv)
584 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
589 * Delete a user from a remote RPC server
591 * All parameters are provided by the run_rpc_command function, except for
592 * argc, argv which are passes through.
594 * @param domain_sid The domain sid acquired from the remote server
595 * @param cli A cli_state connected to the server.
596 * @param mem_ctx Talloc context, destoyed on completion of the function.
597 * @param argc Standard main() style argc
598 * @param argv Standard main() style argv. Initial components are already
601 * @return Normal NTSTATUS return.
604 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
605 const char *domain_name,
606 struct cli_state *cli,
608 int argc, const char **argv)
610 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
611 POLICY_HND connect_pol, domain_pol, user_pol;
614 d_printf("User must be specified\n");
615 rpc_user_usage(argc, argv);
618 /* Get sam policy and domain handles */
620 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
623 if (!NT_STATUS_IS_OK(result)) {
627 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
628 MAXIMUM_ALLOWED_ACCESS,
629 domain_sid, &domain_pol);
631 if (!NT_STATUS_IS_OK(result)) {
635 /* Get handle on user */
638 uint32 *user_rids, num_rids, *name_types;
639 uint32 flags = 0x000003e8; /* Unknown */
641 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
643 &num_rids, &user_rids,
646 if (!NT_STATUS_IS_OK(result)) {
650 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
651 MAXIMUM_ALLOWED_ACCESS,
652 user_rids[0], &user_pol);
654 if (!NT_STATUS_IS_OK(result)) {
661 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
663 if (!NT_STATUS_IS_OK(result)) {
667 /* Display results */
668 if (!NT_STATUS_IS_OK(result)) {
669 d_printf("Failed to delete user account - %s\n", nt_errstr(result));
671 d_printf("Deleted user account\n");
680 * Rename a user on a remote RPC server
682 * All parameters are provided by the run_rpc_command function, except for
683 * argc, argv which are passes through.
685 * @param domain_sid The domain sid acquired from the remote server
686 * @param cli A cli_state connected to the server.
687 * @param mem_ctx Talloc context, destoyed on completion of the function.
688 * @param argc Standard main() style argc
689 * @param argv Standard main() style argv. Initial components are already
692 * @return Normal NTSTATUS return.
695 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid, const char *domain_name,
696 struct cli_state *cli, TALLOC_CTX *mem_ctx,
697 int argc, const char **argv) {
699 POLICY_HND connect_pol, domain_pol, user_pol;
700 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
701 uint32 info_level = 7;
702 const char *old_name, *new_name;
704 uint32 flags = 0x000003e8; /* Unknown */
705 uint32 num_rids, *name_types;
706 uint32 num_names = 1;
708 SAM_USERINFO_CTR *user_ctr;
709 SAM_USERINFO_CTR ctr;
710 SAM_USER_INFO_7 info7;
713 d_printf("Old and new username must be specified\n");
714 rpc_user_usage(argc, argv);
722 ZERO_STRUCT(user_ctr);
724 /* Get sam policy handle */
726 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
728 if (!NT_STATUS_IS_OK(result)) {
732 /* Get domain policy handle */
734 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
735 MAXIMUM_ALLOWED_ACCESS,
736 domain_sid, &domain_pol);
737 if (!NT_STATUS_IS_OK(result)) {
741 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
743 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
744 flags, num_names, names,
745 &num_rids, &user_rid, &name_types);
746 if (!NT_STATUS_IS_OK(result)) {
750 /* Open domain user */
751 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
752 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
754 if (!NT_STATUS_IS_OK(result)) {
758 /* Query user info */
759 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
760 info_level, &user_ctr);
762 if (!NT_STATUS_IS_OK(result)) {
766 ctr.switch_value = info_level;
767 ctr.info.id7 = &info7;
769 init_sam_user_info7(&info7, new_name);
772 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol,
773 info_level, &cli->user_session_key, &ctr);
775 if (!NT_STATUS_IS_OK(result)) {
780 if (!NT_STATUS_IS_OK(result)) {
781 d_printf("Failed to rename user from %s to %s - %s\n", old_name, new_name,
784 d_printf("Renamed user from %s to %s\n", old_name, new_name);
791 * Rename a user on a remote RPC server
793 * @param argc Standard main() style argc
794 * @param argv Standard main() style argv. Initial components are already
797 * @return A shell status integer (0 for success)
800 static int rpc_user_rename(int argc, const char **argv)
802 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
807 * Delete a user from a remote RPC server
809 * @param argc Standard main() style argc
810 * @param argv Standard main() style argv. Initial components are already
813 * @return A shell status integer (0 for success)
816 static int rpc_user_delete(int argc, const char **argv)
818 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
823 * Set a password for a user on a remote RPC server
825 * All parameters are provided by the run_rpc_command function, except for
826 * argc, argv which are passes through.
828 * @param domain_sid The domain sid acquired from the remote server
829 * @param cli A cli_state connected to the server.
830 * @param mem_ctx Talloc context, destoyed on completion of the function.
831 * @param argc Standard main() style argc
832 * @param argv Standard main() style argv. Initial components are already
835 * @return Normal NTSTATUS return.
838 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
839 const char *domain_name,
840 struct cli_state *cli,
842 int argc, const char **argv)
844 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
845 POLICY_HND connect_pol, domain_pol, user_pol;
846 SAM_USERINFO_CTR ctr;
847 SAM_USER_INFO_24 p24;
850 const char *new_password;
854 d_printf("User must be specified\n");
855 rpc_user_usage(argc, argv);
862 new_password = argv[1];
864 asprintf(&prompt, "Enter new password for %s:", user);
865 new_password = getpass(prompt);
869 /* Get sam policy and domain handles */
871 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
874 if (!NT_STATUS_IS_OK(result)) {
878 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
879 MAXIMUM_ALLOWED_ACCESS,
880 domain_sid, &domain_pol);
882 if (!NT_STATUS_IS_OK(result)) {
886 /* Get handle on user */
889 uint32 *user_rids, num_rids, *name_types;
890 uint32 flags = 0x000003e8; /* Unknown */
892 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
894 &num_rids, &user_rids,
897 if (!NT_STATUS_IS_OK(result)) {
901 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
902 MAXIMUM_ALLOWED_ACCESS,
903 user_rids[0], &user_pol);
905 if (!NT_STATUS_IS_OK(result)) {
910 /* Set password on account */
915 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
917 init_sam_user_info24(&p24, (char *)pwbuf,24);
919 ctr.switch_value = 24;
920 ctr.info.id24 = &p24;
922 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
923 &cli->user_session_key, &ctr);
925 if (!NT_STATUS_IS_OK(result)) {
929 /* Display results */
937 * Set a user's password on a remote RPC server
939 * @param argc Standard main() style argc
940 * @param argv Standard main() style argv. Initial components are already
943 * @return A shell status integer (0 for success)
946 static int rpc_user_password(int argc, const char **argv)
948 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
953 * List user's groups on a remote RPC server
955 * All parameters are provided by the run_rpc_command function, except for
956 * argc, argv which are passes through.
958 * @param domain_sid The domain sid acquired from the remote server
959 * @param cli A cli_state connected to the server.
960 * @param mem_ctx Talloc context, destoyed on completion of the function.
961 * @param argc Standard main() style argc
962 * @param argv Standard main() style argv. Initial components are already
965 * @return Normal NTSTATUS return.
969 rpc_user_info_internals(const DOM_SID *domain_sid, const char *domain_name,
970 struct cli_state *cli,
971 TALLOC_CTX *mem_ctx, int argc, const char **argv)
973 POLICY_HND connect_pol, domain_pol, user_pol;
974 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
975 uint32 *rids, num_rids, *name_types, num_names;
976 uint32 flags = 0x000003e8; /* Unknown */
982 d_printf("User must be specified\n");
983 rpc_user_usage(argc, argv);
986 /* Get sam policy handle */
988 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
990 if (!NT_STATUS_IS_OK(result)) goto done;
992 /* Get domain policy handle */
994 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
995 MAXIMUM_ALLOWED_ACCESS,
996 domain_sid, &domain_pol);
997 if (!NT_STATUS_IS_OK(result)) goto done;
999 /* Get handle on user */
1001 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1003 &num_rids, &rids, &name_types);
1005 if (!NT_STATUS_IS_OK(result)) goto done;
1007 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1008 MAXIMUM_ALLOWED_ACCESS,
1009 rids[0], &user_pol);
1010 if (!NT_STATUS_IS_OK(result)) goto done;
1012 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
1013 &num_rids, &user_gids);
1015 if (!NT_STATUS_IS_OK(result)) goto done;
1020 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1022 for (i = 0; i < num_rids; i++)
1023 rids[i] = user_gids[i].g_rid;
1025 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
1027 &num_names, &names, &name_types);
1029 if (!NT_STATUS_IS_OK(result)) {
1033 /* Display results */
1035 for (i = 0; i < num_names; i++)
1036 printf("%s\n", names[i]);
1043 * List a user's groups from a remote RPC server
1045 * @param argc Standard main() style argc
1046 * @param argv Standard main() style argv. Initial components are already
1049 * @return A shell status integer (0 for success)
1052 static int rpc_user_info(int argc, const char **argv)
1054 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1059 * List users on a remote RPC server
1061 * All parameters are provided by the run_rpc_command function, except for
1062 * argc, argv which are passes through.
1064 * @param domain_sid The domain sid acquired from the remote server
1065 * @param cli A cli_state connected to the server.
1066 * @param mem_ctx Talloc context, destoyed on completion of the function.
1067 * @param argc Standard main() style argc
1068 * @param argv Standard main() style argv. Initial components are already
1071 * @return Normal NTSTATUS return.
1075 rpc_user_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1076 struct cli_state *cli,
1077 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1079 POLICY_HND connect_pol, domain_pol;
1080 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1081 uint32 start_idx=0, num_entries, i, loop_count = 0;
1082 SAM_DISPINFO_CTR ctr;
1083 SAM_DISPINFO_1 info1;
1085 /* Get sam policy handle */
1087 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1089 if (!NT_STATUS_IS_OK(result)) {
1093 /* Get domain policy handle */
1095 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1096 MAXIMUM_ALLOWED_ACCESS,
1097 domain_sid, &domain_pol);
1098 if (!NT_STATUS_IS_OK(result)) {
1102 /* Query domain users */
1105 ctr.sam.info1 = &info1;
1106 if (opt_long_list_entries)
1107 d_printf("\nUser name Comment"\
1108 "\n-----------------------------\n");
1111 uint32 max_entries, max_size;
1113 get_query_dispinfo_params(
1114 loop_count, &max_entries, &max_size);
1116 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1117 &start_idx, 1, &num_entries,
1118 max_entries, max_size, &ctr);
1121 for (i = 0; i < num_entries; i++) {
1122 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1123 if (opt_long_list_entries)
1124 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1126 if (opt_long_list_entries)
1127 printf("%-21.21s %s\n", user, desc);
1129 printf("%s\n", user);
1131 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1138 * 'net rpc user' entrypoint.
1139 * @param argc Standard main() style argc
1140 * @param argc Standard main() style argv. Initial components are already
1144 int net_rpc_user(int argc, const char **argv)
1146 struct functable func[] = {
1147 {"add", rpc_user_add},
1148 {"info", rpc_user_info},
1149 {"delete", rpc_user_delete},
1150 {"password", rpc_user_password},
1151 {"rename", rpc_user_rename},
1156 return run_rpc_command(NULL,PI_SAMR, 0,
1157 rpc_user_list_internals,
1161 return net_run_function(argc, argv, func, rpc_user_usage);
1165 /****************************************************************************/
1168 * Basic usage function for 'net rpc group'
1169 * @param argc Standard main() style argc.
1170 * @param argv Standard main() style argv. Initial components are already
1174 static int rpc_group_usage(int argc, const char **argv)
1176 return net_help_group(argc, argv);
1180 * Delete group on a remote RPC server
1182 * All parameters are provided by the run_rpc_command function, except for
1183 * argc, argv which are passes through.
1185 * @param domain_sid The domain sid acquired from the remote server
1186 * @param cli A cli_state connected to the server.
1187 * @param mem_ctx Talloc context, destoyed on completion of the function.
1188 * @param argc Standard main() style argc
1189 * @param argv Standard main() style argv. Initial components are already
1192 * @return Normal NTSTATUS return.
1195 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1196 const char *domain_name,
1197 struct cli_state *cli,
1198 TALLOC_CTX *mem_ctx,
1199 int argc, const char **argv)
1201 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1202 BOOL group_is_primary = False;
1203 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1205 uint32 *group_rids, num_rids, *name_types, num_members,
1206 *group_attrs, group_rid;
1207 uint32 flags = 0x000003e8; /* Unknown */
1210 /* DOM_GID *user_gids; */
1211 SAM_USERINFO_CTR *user_ctr;
1215 d_printf("specify group\n");
1216 rpc_group_usage(argc,argv);
1217 return NT_STATUS_OK; /* ok? */
1220 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1223 if (!NT_STATUS_IS_OK(result)) {
1224 d_printf("Request samr_connect failed\n");
1228 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1229 MAXIMUM_ALLOWED_ACCESS,
1230 domain_sid, &domain_pol);
1232 if (!NT_STATUS_IS_OK(result)) {
1233 d_printf("Request open_domain failed\n");
1237 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1239 &num_rids, &group_rids,
1242 if (!NT_STATUS_IS_OK(result)) {
1243 d_printf("Lookup of '%s' failed\n",argv[0]);
1247 switch (name_types[0])
1249 case SID_NAME_DOM_GRP:
1250 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1251 MAXIMUM_ALLOWED_ACCESS,
1252 group_rids[0], &group_pol);
1253 if (!NT_STATUS_IS_OK(result)) {
1254 d_printf("Request open_group failed");
1258 group_rid = group_rids[0];
1260 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1261 &num_members, &group_rids,
1264 if (!NT_STATUS_IS_OK(result)) {
1265 d_printf("Unable to query group members of %s",argv[0]);
1270 d_printf("Domain Group %s (rid: %d) has %d members\n",
1271 argv[0],group_rid,num_members);
1274 /* Check if group is anyone's primary group */
1275 for (i = 0; i < num_members; i++)
1277 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1278 MAXIMUM_ALLOWED_ACCESS,
1279 group_rids[i], &user_pol);
1281 if (!NT_STATUS_IS_OK(result)) {
1282 d_printf("Unable to open group member %d\n",group_rids[i]);
1286 ZERO_STRUCT(user_ctr);
1288 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
1291 if (!NT_STATUS_IS_OK(result)) {
1292 d_printf("Unable to lookup userinfo for group member %d\n",group_rids[i]);
1296 if (user_ctr->info.id21->group_rid == group_rid) {
1297 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1300 d_printf("Group is primary group of %s\n",temp);
1301 group_is_primary = True;
1304 cli_samr_close(cli, mem_ctx, &user_pol);
1307 if (group_is_primary) {
1308 d_printf("Unable to delete group because some of it's "
1309 "members have it as primary group\n");
1310 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1314 /* remove all group members */
1315 for (i = 0; i < num_members; i++)
1318 d_printf("Remove group member %d...",group_rids[i]);
1319 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, group_rids[i]);
1321 if (NT_STATUS_IS_OK(result)) {
1326 d_printf("failed\n");
1331 result = cli_samr_delete_dom_group(cli, mem_ctx, &group_pol);
1334 /* removing a local group is easier... */
1335 case SID_NAME_ALIAS:
1336 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1337 MAXIMUM_ALLOWED_ACCESS,
1338 group_rids[0], &group_pol);
1340 if (!NT_STATUS_IS_OK(result)) {
1341 d_printf("Request open_alias failed\n");
1345 result = cli_samr_delete_dom_alias(cli, mem_ctx, &group_pol);
1348 d_printf("%s is of type %s. This command is only for deleting local or global groups\n",
1349 argv[0],sid_type_lookup(name_types[0]));
1350 result = NT_STATUS_UNSUCCESSFUL;
1355 if (NT_STATUS_IS_OK(result)) {
1357 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1359 d_printf("Deleting of %s failed: %s\n",argv[0],
1360 get_friendly_nt_error_msg(result));
1368 static int rpc_group_delete(int argc, const char **argv)
1370 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1375 rpc_group_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1376 struct cli_state *cli,
1377 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1379 POLICY_HND connect_pol, domain_pol, group_pol;
1380 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1381 GROUP_INFO_CTR group_info;
1384 d_printf("Group name must be specified\n");
1385 rpc_group_usage(argc, argv);
1386 return NT_STATUS_OK;
1389 /* Get sam policy handle */
1391 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1393 if (!NT_STATUS_IS_OK(result)) goto done;
1395 /* Get domain policy handle */
1397 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1398 MAXIMUM_ALLOWED_ACCESS,
1399 domain_sid, &domain_pol);
1400 if (!NT_STATUS_IS_OK(result)) goto done;
1402 /* Create the group */
1404 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1405 argv[0], MAXIMUM_ALLOWED_ACCESS,
1407 if (!NT_STATUS_IS_OK(result)) goto done;
1409 if (strlen(opt_comment) == 0) goto done;
1411 /* We've got a comment to set */
1413 group_info.switch_value1 = 4;
1414 init_samr_group_info4(&group_info.group.info4, opt_comment);
1416 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &group_info);
1417 if (!NT_STATUS_IS_OK(result)) goto done;
1420 if (NT_STATUS_IS_OK(result))
1421 DEBUG(5, ("add group succeeded\n"));
1423 d_printf("add group failed: %s\n", nt_errstr(result));
1429 rpc_alias_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1430 struct cli_state *cli,
1431 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1433 POLICY_HND connect_pol, domain_pol, alias_pol;
1434 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1435 ALIAS_INFO_CTR alias_info;
1438 d_printf("Alias name must be specified\n");
1439 rpc_group_usage(argc, argv);
1440 return NT_STATUS_OK;
1443 /* Get sam policy handle */
1445 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1447 if (!NT_STATUS_IS_OK(result)) goto done;
1449 /* Get domain policy handle */
1451 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1452 MAXIMUM_ALLOWED_ACCESS,
1453 domain_sid, &domain_pol);
1454 if (!NT_STATUS_IS_OK(result)) goto done;
1456 /* Create the group */
1458 result = cli_samr_create_dom_alias(cli, mem_ctx, &domain_pol,
1459 argv[0], &alias_pol);
1460 if (!NT_STATUS_IS_OK(result)) goto done;
1462 if (strlen(opt_comment) == 0) goto done;
1464 /* We've got a comment to set */
1466 alias_info.level = 3;
1467 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1469 result = cli_samr_set_aliasinfo(cli, mem_ctx, &alias_pol, &alias_info);
1470 if (!NT_STATUS_IS_OK(result)) goto done;
1473 if (NT_STATUS_IS_OK(result))
1474 DEBUG(5, ("add alias succeeded\n"));
1476 d_printf("add alias failed: %s\n", nt_errstr(result));
1481 static int rpc_group_add(int argc, const char **argv)
1484 return run_rpc_command(NULL, PI_SAMR, 0,
1485 rpc_alias_add_internals,
1488 return run_rpc_command(NULL, PI_SAMR, 0,
1489 rpc_group_add_internals,
1494 get_sid_from_name(struct cli_state *cli, TALLOC_CTX *mem_ctx, const char *name,
1495 DOM_SID *sid, enum SID_NAME_USE *type)
1497 int current_pipe = cli->pipe_idx;
1499 DOM_SID *sids = NULL;
1500 uint32 *types = NULL;
1502 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1504 if (current_pipe != PI_LSARPC) {
1506 if (current_pipe != -1)
1507 cli_nt_session_close(cli);
1509 if (!cli_nt_session_open(cli, PI_LSARPC))
1513 result = cli_lsa_open_policy(cli, mem_ctx, False,
1514 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1516 if (!NT_STATUS_IS_OK(result))
1519 result = cli_lsa_lookup_names(cli, mem_ctx, &lsa_pol, 1,
1520 &name, &sids, &types);
1522 if (NT_STATUS_IS_OK(result)) {
1523 sid_copy(sid, &sids[0]);
1527 cli_lsa_close(cli, mem_ctx, &lsa_pol);
1530 if (current_pipe != PI_LSARPC) {
1531 cli_nt_session_close(cli);
1532 if (current_pipe != -1)
1533 cli_nt_session_open(cli, current_pipe);
1536 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1538 /* Try as S-1-5-whatever */
1542 if (string_to_sid(&tmp_sid, name)) {
1543 sid_copy(sid, &tmp_sid);
1544 *type = SID_NAME_UNKNOWN;
1545 result = NT_STATUS_OK;
1553 rpc_add_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1554 const DOM_SID *group_sid, const char *member)
1556 POLICY_HND connect_pol, domain_pol;
1559 POLICY_HND group_pol;
1562 uint32 *rids = NULL;
1563 uint32 *rid_types = NULL;
1567 sid_copy(&sid, group_sid);
1569 if (!sid_split_rid(&sid, &group_rid))
1570 return NT_STATUS_UNSUCCESSFUL;
1572 /* Get sam policy handle */
1573 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1575 if (!NT_STATUS_IS_OK(result))
1578 /* Get domain policy handle */
1579 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1580 MAXIMUM_ALLOWED_ACCESS,
1582 if (!NT_STATUS_IS_OK(result))
1585 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1587 &num_rids, &rids, &rid_types);
1589 if (!NT_STATUS_IS_OK(result)) {
1590 d_printf("Could not lookup up group member %s\n", member);
1594 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1595 MAXIMUM_ALLOWED_ACCESS,
1596 group_rid, &group_pol);
1598 if (!NT_STATUS_IS_OK(result))
1601 result = cli_samr_add_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1604 cli_samr_close(cli, mem_ctx, &connect_pol);
1609 rpc_add_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1610 const DOM_SID *alias_sid, const char *member)
1612 POLICY_HND connect_pol, domain_pol;
1615 POLICY_HND alias_pol;
1618 enum SID_NAME_USE member_type;
1622 sid_copy(&sid, alias_sid);
1624 if (!sid_split_rid(&sid, &alias_rid))
1625 return NT_STATUS_UNSUCCESSFUL;
1627 result = get_sid_from_name(cli, mem_ctx, member,
1628 &member_sid, &member_type);
1630 if (!NT_STATUS_IS_OK(result)) {
1631 d_printf("Could not lookup up group member %s\n", member);
1635 /* Get sam policy handle */
1636 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1638 if (!NT_STATUS_IS_OK(result)) {
1642 /* Get domain policy handle */
1643 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1644 MAXIMUM_ALLOWED_ACCESS,
1646 if (!NT_STATUS_IS_OK(result)) {
1650 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1651 MAXIMUM_ALLOWED_ACCESS,
1652 alias_rid, &alias_pol);
1654 if (!NT_STATUS_IS_OK(result))
1657 result = cli_samr_add_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1659 if (!NT_STATUS_IS_OK(result))
1663 cli_samr_close(cli, mem_ctx, &connect_pol);
1668 rpc_group_addmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1669 struct cli_state *cli,
1670 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1673 enum SID_NAME_USE group_type;
1676 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
1677 return NT_STATUS_UNSUCCESSFUL;
1680 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1681 &group_sid, &group_type))) {
1682 d_printf("Could not lookup group name %s\n", argv[0]);
1683 return NT_STATUS_UNSUCCESSFUL;
1686 if (group_type == SID_NAME_DOM_GRP) {
1687 NTSTATUS result = rpc_add_groupmem(cli, mem_ctx,
1688 &group_sid, argv[1]);
1690 if (!NT_STATUS_IS_OK(result)) {
1691 d_printf("Could not add %s to %s: %s\n",
1692 argv[1], argv[0], nt_errstr(result));
1697 if (group_type == SID_NAME_ALIAS) {
1698 NTSTATUS result = rpc_add_aliasmem(cli, mem_ctx,
1699 &group_sid, argv[1]);
1701 if (!NT_STATUS_IS_OK(result)) {
1702 d_printf("Could not add %s to %s: %s\n",
1703 argv[1], argv[0], nt_errstr(result));
1708 d_printf("Can only add members to global or local groups which "
1709 "%s is not\n", argv[0]);
1711 return NT_STATUS_UNSUCCESSFUL;
1714 static int rpc_group_addmem(int argc, const char **argv)
1716 return run_rpc_command(NULL, PI_SAMR, 0,
1717 rpc_group_addmem_internals,
1722 rpc_del_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1723 const DOM_SID *group_sid, const char *member)
1725 POLICY_HND connect_pol, domain_pol;
1728 POLICY_HND group_pol;
1731 uint32 *rids = NULL;
1732 uint32 *rid_types = NULL;
1736 sid_copy(&sid, group_sid);
1738 if (!sid_split_rid(&sid, &group_rid))
1739 return NT_STATUS_UNSUCCESSFUL;
1741 /* Get sam policy handle */
1742 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1744 if (!NT_STATUS_IS_OK(result))
1747 /* Get domain policy handle */
1748 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1749 MAXIMUM_ALLOWED_ACCESS,
1751 if (!NT_STATUS_IS_OK(result))
1754 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1756 &num_rids, &rids, &rid_types);
1758 if (!NT_STATUS_IS_OK(result)) {
1759 d_printf("Could not lookup up group member %s\n", member);
1763 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1764 MAXIMUM_ALLOWED_ACCESS,
1765 group_rid, &group_pol);
1767 if (!NT_STATUS_IS_OK(result))
1770 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1773 cli_samr_close(cli, mem_ctx, &connect_pol);
1778 rpc_del_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1779 const DOM_SID *alias_sid, const char *member)
1781 POLICY_HND connect_pol, domain_pol;
1784 POLICY_HND alias_pol;
1787 enum SID_NAME_USE member_type;
1791 sid_copy(&sid, alias_sid);
1793 if (!sid_split_rid(&sid, &alias_rid))
1794 return NT_STATUS_UNSUCCESSFUL;
1796 result = get_sid_from_name(cli, mem_ctx, member,
1797 &member_sid, &member_type);
1799 if (!NT_STATUS_IS_OK(result)) {
1800 d_printf("Could not lookup up group member %s\n", member);
1804 /* Get sam policy handle */
1805 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1807 if (!NT_STATUS_IS_OK(result)) {
1811 /* Get domain policy handle */
1812 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1813 MAXIMUM_ALLOWED_ACCESS,
1815 if (!NT_STATUS_IS_OK(result)) {
1819 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1820 MAXIMUM_ALLOWED_ACCESS,
1821 alias_rid, &alias_pol);
1823 if (!NT_STATUS_IS_OK(result))
1826 result = cli_samr_del_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1828 if (!NT_STATUS_IS_OK(result))
1832 cli_samr_close(cli, mem_ctx, &connect_pol);
1837 rpc_group_delmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1838 struct cli_state *cli,
1839 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1842 enum SID_NAME_USE group_type;
1845 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
1846 return NT_STATUS_UNSUCCESSFUL;
1849 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1850 &group_sid, &group_type))) {
1851 d_printf("Could not lookup group name %s\n", argv[0]);
1852 return NT_STATUS_UNSUCCESSFUL;
1855 if (group_type == SID_NAME_DOM_GRP) {
1856 NTSTATUS result = rpc_del_groupmem(cli, mem_ctx,
1857 &group_sid, argv[1]);
1859 if (!NT_STATUS_IS_OK(result)) {
1860 d_printf("Could not del %s from %s: %s\n",
1861 argv[1], argv[0], nt_errstr(result));
1866 if (group_type == SID_NAME_ALIAS) {
1867 NTSTATUS result = rpc_del_aliasmem(cli, mem_ctx,
1868 &group_sid, argv[1]);
1870 if (!NT_STATUS_IS_OK(result)) {
1871 d_printf("Could not del %s from %s: %s\n",
1872 argv[1], argv[0], nt_errstr(result));
1877 d_printf("Can only delete members from global or local groups which "
1878 "%s is not\n", argv[0]);
1880 return NT_STATUS_UNSUCCESSFUL;
1883 static int rpc_group_delmem(int argc, const char **argv)
1885 return run_rpc_command(NULL, PI_SAMR, 0,
1886 rpc_group_delmem_internals,
1891 * List groups on a remote RPC server
1893 * All parameters are provided by the run_rpc_command function, except for
1894 * argc, argv which are passes through.
1896 * @param domain_sid The domain sid acquired from the remote server
1897 * @param cli A cli_state connected to the server.
1898 * @param mem_ctx Talloc context, destoyed on completion of the function.
1899 * @param argc Standard main() style argc
1900 * @param argv Standard main() style argv. Initial components are already
1903 * @return Normal NTSTATUS return.
1907 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1908 struct cli_state *cli,
1909 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1911 POLICY_HND connect_pol, domain_pol;
1912 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1913 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1914 struct acct_info *groups;
1915 BOOL global = False;
1917 BOOL builtin = False;
1925 for (i=0; i<argc; i++) {
1926 if (strequal(argv[i], "global"))
1929 if (strequal(argv[i], "local"))
1932 if (strequal(argv[i], "builtin"))
1936 /* Get sam policy handle */
1938 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1940 if (!NT_STATUS_IS_OK(result)) {
1944 /* Get domain policy handle */
1946 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1947 MAXIMUM_ALLOWED_ACCESS,
1948 domain_sid, &domain_pol);
1949 if (!NT_STATUS_IS_OK(result)) {
1953 /* Query domain groups */
1954 if (opt_long_list_entries)
1955 d_printf("\nGroup name Comment"\
1956 "\n-----------------------------\n");
1958 SAM_DISPINFO_CTR ctr;
1959 SAM_DISPINFO_3 info3;
1964 ctr.sam.info3 = &info3;
1968 get_query_dispinfo_params(
1969 loop_count, &max_entries, &max_size);
1971 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1972 &start_idx, 3, &num_entries,
1973 max_entries, max_size, &ctr);
1975 if (!NT_STATUS_IS_OK(result) &&
1976 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1979 for (i = 0; i < num_entries; i++) {
1981 fstring group, desc;
1983 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1984 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1986 if (opt_long_list_entries)
1987 printf("%-21.21s %-50.50s\n",
1990 printf("%s\n", group);
1992 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1993 /* query domain aliases */
1998 /* The max_size field in cli_samr_enum_als_groups is more like
1999 * an account_control field with indiviual bits what to
2000 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2001 * everything. I'm too lazy (sorry) to get this through to
2002 * rpc_parse/ etc. Volker */
2004 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
2006 &groups, &num_entries);
2008 if (!NT_STATUS_IS_OK(result) &&
2009 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2012 for (i = 0; i < num_entries; i++) {
2014 char *description = NULL;
2016 if (opt_long_list_entries) {
2018 POLICY_HND alias_pol;
2021 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
2026 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
2029 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
2031 description = unistr2_tdup(mem_ctx,
2032 ctr.alias.info3.description.string);
2036 if (description != NULL) {
2037 printf("%-21.21s %-50.50s\n",
2038 groups[i].acct_name,
2041 printf("%s\n", groups[i].acct_name);
2044 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2045 cli_samr_close(cli, mem_ctx, &domain_pol);
2046 /* Get builtin policy handle */
2048 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2049 MAXIMUM_ALLOWED_ACCESS,
2050 &global_sid_Builtin, &domain_pol);
2051 if (!NT_STATUS_IS_OK(result)) {
2054 /* query builtin aliases */
2057 if (!builtin) break;
2059 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
2060 &start_idx, max_entries,
2061 &groups, &num_entries);
2063 if (!NT_STATUS_IS_OK(result) &&
2064 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2067 for (i = 0; i < num_entries; i++) {
2069 char *description = NULL;
2071 if (opt_long_list_entries) {
2073 POLICY_HND alias_pol;
2076 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
2081 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
2084 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
2086 description = unistr2_tdup(mem_ctx,
2087 ctr.alias.info3.description.string);
2091 if (description != NULL) {
2092 printf("%-21.21s %-50.50s\n",
2093 groups[i].acct_name,
2096 printf("%s\n", groups[i].acct_name);
2099 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2105 static int rpc_group_list(int argc, const char **argv)
2107 return run_rpc_command(NULL, PI_SAMR, 0,
2108 rpc_group_list_internals,
2113 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2114 const char *domain_name, const DOM_SID *domain_sid,
2115 POLICY_HND *domain_pol, uint32 rid)
2118 POLICY_HND group_pol;
2119 uint32 num_members, *group_rids, *group_attrs;
2126 sid_to_string(sid_str, domain_sid);
2128 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
2129 MAXIMUM_ALLOWED_ACCESS,
2132 if (!NT_STATUS_IS_OK(result))
2135 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
2136 &num_members, &group_rids,
2139 if (!NT_STATUS_IS_OK(result))
2142 while (num_members > 0) {
2143 int this_time = 512;
2145 if (num_members < this_time)
2146 this_time = num_members;
2148 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol,
2149 this_time, group_rids,
2150 &num_names, &names, &name_types);
2152 if (!NT_STATUS_IS_OK(result))
2155 /* We only have users as members, but make the output
2156 the same as the output of alias members */
2158 for (i = 0; i < this_time; i++) {
2160 if (opt_long_list_entries) {
2161 printf("%s-%d %s\\%s %d\n", sid_str,
2162 group_rids[i], domain_name, names[i],
2165 printf("%s\\%s\n", domain_name, names[i]);
2169 num_members -= this_time;
2173 return NT_STATUS_OK;
2177 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2178 POLICY_HND *domain_pol, uint32 rid)
2181 POLICY_HND alias_pol, lsa_pol;
2183 DOM_SID *alias_sids;
2189 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
2190 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2192 if (!NT_STATUS_IS_OK(result))
2195 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
2196 &num_members, &alias_sids);
2198 if (!NT_STATUS_IS_OK(result)) {
2199 d_printf("Couldn't list alias members\n");
2203 if (num_members == 0) {
2204 return NT_STATUS_OK;
2207 cli_nt_session_close(cli);
2209 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2210 d_printf("Couldn't open LSA pipe\n");
2214 result = cli_lsa_open_policy(cli, mem_ctx, True,
2215 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2217 if (!NT_STATUS_IS_OK(result)) {
2218 d_printf("Couldn't open LSA policy handle\n");
2222 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
2224 &domains, &names, &types);
2226 if (!NT_STATUS_IS_OK(result) &&
2227 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2228 d_printf("Couldn't lookup SIDs\n");
2232 for (i = 0; i < num_members; i++) {
2234 sid_to_string(sid_str, &alias_sids[i]);
2236 if (opt_long_list_entries) {
2237 printf("%s %s\\%s %d\n", sid_str,
2238 domains[i] ? domains[i] : "*unknown*",
2239 names[i] ? names[i] : "*unknown*", types[i]);
2242 printf("%s\\%s\n", domains[i], names[i]);
2244 printf("%s\n", sid_str);
2248 return NT_STATUS_OK;
2252 rpc_group_members_internals(const DOM_SID *domain_sid,
2253 const char *domain_name,
2254 struct cli_state *cli,
2255 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2258 POLICY_HND connect_pol, domain_pol;
2259 uint32 num_rids, *rids, *rid_types;
2261 /* Get sam policy handle */
2263 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2266 if (!NT_STATUS_IS_OK(result))
2269 /* Get domain policy handle */
2271 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2272 MAXIMUM_ALLOWED_ACCESS,
2273 domain_sid, &domain_pol);
2275 if (!NT_STATUS_IS_OK(result))
2278 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2279 1, argv, &num_rids, &rids, &rid_types);
2281 if (!NT_STATUS_IS_OK(result)) {
2283 /* Ok, did not find it in the global sam, try with builtin */
2285 DOM_SID sid_Builtin;
2287 cli_samr_close(cli, mem_ctx, &domain_pol);
2289 string_to_sid(&sid_Builtin, "S-1-5-32");
2291 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2292 MAXIMUM_ALLOWED_ACCESS,
2293 &sid_Builtin, &domain_pol);
2295 if (!NT_STATUS_IS_OK(result)) {
2296 d_printf("Couldn't find group %s\n", argv[0]);
2300 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2304 if (!NT_STATUS_IS_OK(result)) {
2305 d_printf("Couldn't find group %s\n", argv[0]);
2310 if (num_rids != 1) {
2311 d_printf("Couldn't find group %s\n", argv[0]);
2315 if (rid_types[0] == SID_NAME_DOM_GRP) {
2316 return rpc_list_group_members(cli, mem_ctx, domain_name,
2317 domain_sid, &domain_pol,
2321 if (rid_types[0] == SID_NAME_ALIAS) {
2322 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
2326 return NT_STATUS_NO_SUCH_GROUP;
2329 static int rpc_group_members(int argc, const char **argv)
2332 return rpc_group_usage(argc, argv);
2335 return run_rpc_command(NULL, PI_SAMR, 0,
2336 rpc_group_members_internals,
2341 rpc_group_rename_internals(const DOM_SID *domain_sid,
2342 const char *domain_name,
2343 struct cli_state *cli,
2344 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2347 POLICY_HND connect_pol, domain_pol, group_pol;
2348 uint32 num_rids, *rids, *rid_types;
2352 d_printf("Usage: 'net rpc group rename group newname'\n");
2353 return NT_STATUS_UNSUCCESSFUL;
2356 /* Get sam policy handle */
2358 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2361 if (!NT_STATUS_IS_OK(result))
2364 /* Get domain policy handle */
2366 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2367 MAXIMUM_ALLOWED_ACCESS,
2368 domain_sid, &domain_pol);
2370 if (!NT_STATUS_IS_OK(result))
2373 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2374 1, argv, &num_rids, &rids, &rid_types);
2376 if (num_rids != 1) {
2377 d_printf("Couldn't find group %s\n", argv[0]);
2381 if (rid_types[0] != SID_NAME_DOM_GRP) {
2382 d_printf("Can only rename domain groups\n");
2383 return NT_STATUS_UNSUCCESSFUL;
2386 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
2387 MAXIMUM_ALLOWED_ACCESS,
2388 rids[0], &group_pol);
2390 if (!NT_STATUS_IS_OK(result))
2395 ctr.switch_value1 = 2;
2396 init_samr_group_info2(&ctr.group.info2, argv[1]);
2398 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &ctr);
2400 if (!NT_STATUS_IS_OK(result))
2403 return NT_STATUS_NO_SUCH_GROUP;
2406 static int rpc_group_rename(int argc, const char **argv)
2409 return rpc_group_usage(argc, argv);
2412 return run_rpc_command(NULL, PI_SAMR, 0,
2413 rpc_group_rename_internals,
2418 * 'net rpc group' entrypoint.
2419 * @param argc Standard main() style argc
2420 * @param argc Standard main() style argv. Initial components are already
2424 int net_rpc_group(int argc, const char **argv)
2426 struct functable func[] = {
2427 {"add", rpc_group_add},
2428 {"delete", rpc_group_delete},
2429 {"addmem", rpc_group_addmem},
2430 {"delmem", rpc_group_delmem},
2431 {"list", rpc_group_list},
2432 {"members", rpc_group_members},
2433 {"rename", rpc_group_rename},
2438 return run_rpc_command(NULL, PI_SAMR, 0,
2439 rpc_group_list_internals,
2443 return net_run_function(argc, argv, func, rpc_group_usage);
2446 /****************************************************************************/
2448 static int rpc_share_usage(int argc, const char **argv)
2450 return net_help_share(argc, argv);
2454 * Add a share on a remote RPC server
2456 * All parameters are provided by the run_rpc_command function, except for
2457 * argc, argv which are passes through.
2459 * @param domain_sid The domain sid acquired from the remote server
2460 * @param cli A cli_state connected to the server.
2461 * @param mem_ctx Talloc context, destoyed on completion of the function.
2462 * @param argc Standard main() style argc
2463 * @param argv Standard main() style argv. Initial components are already
2466 * @return Normal NTSTATUS return.
2469 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
2470 struct cli_state *cli,
2471 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2474 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2476 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2477 uint32 num_users=0, perms=0;
2478 char *password=NULL; /* don't allow a share password */
2481 path = strchr(sharename, '=');
2483 return NT_STATUS_UNSUCCESSFUL;
2486 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
2487 opt_comment, perms, opt_maxusers,
2488 num_users, path, password,
2490 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2493 static int rpc_share_add(int argc, const char **argv)
2495 if ((argc < 1) || !strchr(argv[0], '=')) {
2496 DEBUG(1,("Sharename or path not specified on add\n"));
2497 return rpc_share_usage(argc, argv);
2499 return run_rpc_command(NULL, PI_SRVSVC, 0,
2500 rpc_share_add_internals,
2505 * Delete a share on a remote RPC server
2507 * All parameters are provided by the run_rpc_command function, except for
2508 * argc, argv which are passes through.
2510 * @param domain_sid The domain sid acquired from the remote server
2511 * @param cli A cli_state connected to the server.
2512 * @param mem_ctx Talloc context, destoyed on completion of the function.
2513 * @param argc Standard main() style argc
2514 * @param argv Standard main() style argv. Initial components are already
2517 * @return Normal NTSTATUS return.
2520 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name,
2521 struct cli_state *cli,
2522 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2526 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
2527 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2531 * Delete a share on a remote RPC server
2533 * @param domain_sid The domain sid acquired from the remote server
2534 * @param argc Standard main() style argc
2535 * @param argv Standard main() style argv. Initial components are already
2538 * @return A shell status integer (0 for success)
2540 static int rpc_share_delete(int argc, const char **argv)
2543 DEBUG(1,("Sharename not specified on delete\n"));
2544 return rpc_share_usage(argc, argv);
2546 return run_rpc_command(NULL, PI_SRVSVC, 0,
2547 rpc_share_del_internals,
2552 * Formatted print of share info
2554 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2557 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
2559 fstring netname = "", remark = "";
2561 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
2562 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
2564 if (opt_long_list_entries) {
2565 d_printf("%-12s %-8.8s %-50s\n",
2566 netname, share_type[info1->info_1.type], remark);
2568 d_printf("%s\n", netname);
2574 static WERROR get_share_info(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2575 uint32 level, int argc, const char **argv,
2576 SRV_SHARE_INFO_CTR *ctr)
2579 SRV_SHARE_INFO info;
2581 /* no specific share requested, enumerate all */
2585 uint32 preferred_len = 0xffffffff;
2587 init_enum_hnd(&hnd, 0);
2589 result = cli_srvsvc_net_share_enum(cli, mem_ctx, level, ctr,
2590 preferred_len, &hnd);
2594 /* request just one share */
2595 result = cli_srvsvc_net_share_get_info(cli, mem_ctx, argv[0], level, &info);
2597 if (!W_ERROR_IS_OK(result))
2603 ctr->info_level = ctr->switch_value = level;
2604 ctr->ptr_share_info = ctr->ptr_entries = 1;
2605 ctr->num_entries = ctr->num_entries2 = 1;
2611 SRV_SHARE_INFO_1 *info1;
2613 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
2614 info1 = ctr->share.info1;
2616 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
2618 /* Copy pointer crap */
2620 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
2622 /* Duplicate strings */
2624 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_netname);
2626 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
2628 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_remark);
2630 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
2635 SRV_SHARE_INFO_2 *info2;
2637 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
2638 info2 = ctr->share.info2;
2640 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
2642 /* Copy pointer crap */
2644 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
2646 /* Duplicate strings */
2648 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_netname);
2650 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
2652 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_remark);
2654 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
2656 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_path);
2658 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
2660 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
2662 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
2667 SRV_SHARE_INFO_502 *info502;
2669 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
2670 info502 = ctr->share.info502;
2672 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
2674 /* Copy pointer crap */
2676 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
2678 /* Duplicate strings */
2680 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_netname);
2682 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
2684 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_remark);
2686 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
2688 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_path);
2690 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
2692 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
2694 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
2696 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
2707 * List shares on a remote RPC server
2709 * All parameters are provided by the run_rpc_command function, except for
2710 * argc, argv which are passes through.
2712 * @param domain_sid The domain sid acquired from the remote server
2713 * @param cli A cli_state connected to the server.
2714 * @param mem_ctx Talloc context, destoyed on completion of the function.
2715 * @param argc Standard main() style argc
2716 * @param argv Standard main() style argv. Initial components are already
2719 * @return Normal NTSTATUS return.
2723 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
2724 struct cli_state *cli,
2725 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2727 SRV_SHARE_INFO_CTR ctr;
2729 uint32 i, level = 1;
2731 result = get_share_info(cli, mem_ctx, level, argc, argv, &ctr);
2732 if (!W_ERROR_IS_OK(result))
2735 /* Display results */
2737 if (opt_long_list_entries) {
2739 "\nEnumerating shared resources (exports) on remote server:\n\n"\
2740 "\nShare name Type Description\n"\
2741 "---------- ---- -----------\n");
2743 for (i = 0; i < ctr.num_entries; i++)
2744 display_share_info_1(&ctr.share.info1[i]);
2746 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2750 * 'net rpc share list' entrypoint.
2751 * @param argc Standard main() style argc
2752 * @param argv Standard main() style argv. Initial components are already
2755 static int rpc_share_list(int argc, const char **argv)
2757 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
2760 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
2762 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2763 d_printf("skipping [%s]: not a file share.\n", netname);
2773 static BOOL check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
2775 /* only support disk shares */
2776 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
2777 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
2781 /* skip builtin shares */
2782 /* FIXME: should print$ be added too ? */
2783 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
2784 strequal(netname,"global"))
2787 if (opt_exclude && in_list(netname, opt_exclude, False)) {
2788 printf("excluding [%s]\n", netname);
2792 return check_share_availability(cli, netname);
2796 * Migrate shares from a remote RPC server to the local RPC srever
2798 * All parameters are provided by the run_rpc_command function, except for
2799 * argc, argv which are passes through.
2801 * @param domain_sid The domain sid acquired from the remote server
2802 * @param cli A cli_state connected to the server.
2803 * @param mem_ctx Talloc context, destoyed on completion of the function.
2804 * @param argc Standard main() style argc
2805 * @param argv Standard main() style argv. Initial components are already
2808 * @return Normal NTSTATUS return.
2811 rpc_share_migrate_shares_internals(const DOM_SID *domain_sid, const char *domain_name,
2812 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2813 int argc, const char **argv)
2816 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2817 SRV_SHARE_INFO_CTR ctr_src;
2818 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2819 char *password = NULL; /* don't allow a share password */
2821 BOOL got_dst_srvsvc_pipe = False;
2822 struct cli_state *cli_dst = NULL;
2823 uint32 level = 502; /* includes secdesc */
2825 result = get_share_info(cli, mem_ctx, level, argc, argv, &ctr_src);
2826 if (!W_ERROR_IS_OK(result))
2829 /* connect destination PI_SRVSVC */
2830 nt_status = connect_pipe(&cli_dst, PI_SRVSVC, &got_dst_srvsvc_pipe);
2831 if (!NT_STATUS_IS_OK(nt_status))
2835 for (i = 0; i < ctr_src.num_entries; i++) {
2837 fstring netname = "", remark = "", path = "";
2838 /* reset error-code */
2839 nt_status = NT_STATUS_UNSUCCESSFUL;
2841 rpcstr_pull_unistr2_fstring(
2842 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
2843 rpcstr_pull_unistr2_fstring(
2844 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
2845 rpcstr_pull_unistr2_fstring(
2846 path, &ctr_src.share.info502[i].info_502_str.uni_path);
2848 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
2851 /* finally add the share on the dst server */
2853 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
2854 netname, path, remark);
2856 result = cli_srvsvc_net_share_add(cli_dst, mem_ctx, netname, type, remark,
2857 ctr_src.share.info502[i].info_502.perms,
2858 ctr_src.share.info502[i].info_502.max_uses,
2859 ctr_src.share.info502[i].info_502.num_uses,
2860 path, password, level,
2863 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
2864 printf(" [%s] does already exist\n", netname);
2868 if (!W_ERROR_IS_OK(result)) {
2869 printf("cannot add share: %s\n", dos_errstr(result));
2875 nt_status = NT_STATUS_OK;
2878 if (got_dst_srvsvc_pipe) {
2879 cli_nt_session_close(cli_dst);
2880 cli_shutdown(cli_dst);
2888 * Migrate shares from a rpc-server to another
2890 * @param argc Standard main() style argc
2891 * @param argv Standard main() style argv. Initial components are already
2894 * @return A shell status integer (0 for success)
2896 static int rpc_share_migrate_shares(int argc, const char **argv)
2900 printf("no server to migrate\n");
2904 return run_rpc_command(NULL, PI_SRVSVC, 0,
2905 rpc_share_migrate_shares_internals,
2912 * @param f file_info
2913 * @param mask current search mask
2914 * @param state arg-pointer
2917 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
2919 static NTSTATUS nt_status;
2920 static struct copy_clistate *local_state;
2921 static fstring filename, new_mask;
2925 local_state = (struct copy_clistate *)state;
2926 nt_status = NT_STATUS_UNSUCCESSFUL;
2928 if (strequal(f->name, ".") || strequal(f->name, ".."))
2931 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
2934 if (f->mode & aDIR) {
2936 DEBUG(3,("got dir: %s\n", f->name));
2938 fstrcpy(dir, local_state->cwd);
2940 fstrcat(dir, f->name);
2942 switch (net_mode_share)
2944 case NET_MODE_SHARE_MIGRATE:
2945 /* create that directory */
2946 nt_status = net_copy_file(local_state->mem_ctx,
2947 local_state->cli_share_src,
2948 local_state->cli_share_dst,
2950 opt_acls? True : False,
2951 opt_attrs? True : False,
2952 opt_timestamps? True : False,
2956 d_printf("Unsupported mode %d\n", net_mode_share);
2960 if (!NT_STATUS_IS_OK(nt_status))
2961 printf("could not handle dir %s: %s\n",
2962 dir, nt_errstr(nt_status));
2964 /* search below that directory */
2965 fstrcpy(new_mask, dir);
2966 fstrcat(new_mask, "\\*");
2968 old_dir = local_state->cwd;
2969 local_state->cwd = dir;
2970 if (!sync_files(local_state, new_mask))
2971 printf("could not handle files\n");
2972 local_state->cwd = old_dir;
2979 fstrcpy(filename, local_state->cwd);
2980 fstrcat(filename, "\\");
2981 fstrcat(filename, f->name);
2983 DEBUG(3,("got file: %s\n", filename));
2985 switch (net_mode_share)
2987 case NET_MODE_SHARE_MIGRATE:
2988 nt_status = net_copy_file(local_state->mem_ctx,
2989 local_state->cli_share_src,
2990 local_state->cli_share_dst,
2992 opt_acls? True : False,
2993 opt_attrs? True : False,
2994 opt_timestamps? True: False,
2998 d_printf("Unsupported file mode %d\n", net_mode_share);
3002 if (!NT_STATUS_IS_OK(nt_status))
3003 printf("could not handle file %s: %s\n",
3004 filename, nt_errstr(nt_status));
3009 * sync files, can be called recursivly to list files
3010 * and then call copy_fn for each file
3012 * @param cp_clistate pointer to the copy_clistate we work with
3013 * @param mask the current search mask
3015 * @return Boolean result
3017 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3020 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3022 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3023 d_printf("listing %s failed with error: %s\n",
3024 mask, cli_errstr(cp_clistate->cli_share_src));
3033 * Set the top level directory permissions before we do any further copies.
3034 * Should set up ACL inheritance.
3037 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3038 const char *sharename)
3042 switch (net_mode_share) {
3043 case NET_MODE_SHARE_MIGRATE:
3044 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3045 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3046 cp_clistate->cli_share_src,
3047 cp_clistate->cli_share_dst,
3049 opt_acls? True : False,
3050 opt_attrs? True : False,
3051 opt_timestamps? True: False,
3055 d_printf("Unsupported mode %d\n", net_mode_share);
3059 if (!NT_STATUS_IS_OK(nt_status)) {
3060 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3061 sharename, nt_errstr(nt_status));
3070 * Sync all files inside a remote share to another share (over smb)
3072 * All parameters are provided by the run_rpc_command function, except for
3073 * argc, argv which are passes through.
3075 * @param domain_sid The domain sid acquired from the remote server
3076 * @param cli A cli_state connected to the server.
3077 * @param mem_ctx Talloc context, destoyed on completion of the function.
3078 * @param argc Standard main() style argc
3079 * @param argv Standard main() style argv. Initial components are already
3082 * @return Normal NTSTATUS return.
3085 rpc_share_migrate_files_internals(const DOM_SID *domain_sid, const char *domain_name,
3086 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3087 int argc, const char **argv)
3090 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3091 SRV_SHARE_INFO_CTR ctr_src;
3094 struct copy_clistate cp_clistate;
3095 BOOL got_src_share = False;
3096 BOOL got_dst_share = False;
3097 pstring mask = "\\*";
3100 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3102 result = get_share_info(cli, mem_ctx, level, argc, argv, &ctr_src);
3104 if (!W_ERROR_IS_OK(result))
3107 for (i = 0; i < ctr_src.num_entries; i++) {
3109 fstring netname = "";
3111 rpcstr_pull_unistr2_fstring(
3112 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3114 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3117 /* one might not want to mirror whole discs :) */
3118 if (strequal(netname, "print$") || netname[1] == '$') {
3119 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3123 switch (net_mode_share)
3125 case NET_MODE_SHARE_MIGRATE:
3129 d_printf("Unsupported mode %d\n", net_mode_share);
3132 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3134 opt_acls ? "including" : "without",
3135 opt_attrs ? "including" : "without",
3136 opt_timestamps ? "(preserving timestamps)" : "");
3138 cp_clistate.mem_ctx = mem_ctx;
3139 cp_clistate.cli_share_src = NULL;
3140 cp_clistate.cli_share_dst = NULL;
3141 cp_clistate.cwd = NULL;
3142 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3144 /* open share source */
3145 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3146 &cli->dest_ip, cli->desthost,
3148 if (!NT_STATUS_IS_OK(nt_status))
3151 got_src_share = True;
3153 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3154 /* open share destination */
3155 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3156 NULL, dst, netname, "A:");
3157 if (!NT_STATUS_IS_OK(nt_status))
3160 got_dst_share = True;
3163 if (!copy_top_level_perms(&cp_clistate, netname)) {
3164 d_printf("Could not handle the top level directory permissions for the share: %s\n", netname);
3165 nt_status = NT_STATUS_UNSUCCESSFUL;
3169 if (!sync_files(&cp_clistate, mask)) {
3170 d_printf("could not handle files for share: %s\n", netname);
3171 nt_status = NT_STATUS_UNSUCCESSFUL;
3176 nt_status = NT_STATUS_OK;
3181 cli_shutdown(cp_clistate.cli_share_src);
3184 cli_shutdown(cp_clistate.cli_share_dst);
3190 static int rpc_share_migrate_files(int argc, const char **argv)
3194 printf("no server to migrate\n");
3198 return run_rpc_command(NULL, PI_SRVSVC, 0,
3199 rpc_share_migrate_files_internals,
3204 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3206 * All parameters are provided by the run_rpc_command function, except for
3207 * argc, argv which are passes through.
3209 * @param domain_sid The domain sid acquired from the remote server
3210 * @param cli A cli_state connected to the server.
3211 * @param mem_ctx Talloc context, destoyed on completion of the function.
3212 * @param argc Standard main() style argc
3213 * @param argv Standard main() style argv. Initial components are already
3216 * @return Normal NTSTATUS return.
3219 rpc_share_migrate_security_internals(const DOM_SID *domain_sid, const char *domain_name,
3220 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3221 int argc, const char **argv)
3224 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3225 SRV_SHARE_INFO_CTR ctr_src;
3226 SRV_SHARE_INFO info;
3228 BOOL got_dst_srvsvc_pipe = False;
3229 struct cli_state *cli_dst = NULL;
3230 uint32 level = 502; /* includes secdesc */
3232 result = get_share_info(cli, mem_ctx, level, argc, argv, &ctr_src);
3234 if (!W_ERROR_IS_OK(result))
3237 /* connect destination PI_SRVSVC */
3238 nt_status = connect_pipe(&cli_dst, PI_SRVSVC, &got_dst_srvsvc_pipe);
3239 if (!NT_STATUS_IS_OK(nt_status))
3243 for (i = 0; i < ctr_src.num_entries; i++) {
3245 fstring netname = "", remark = "", path = "";
3246 /* reset error-code */
3247 nt_status = NT_STATUS_UNSUCCESSFUL;
3249 rpcstr_pull_unistr2_fstring(
3250 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3251 rpcstr_pull_unistr2_fstring(
3252 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3253 rpcstr_pull_unistr2_fstring(
3254 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3256 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3259 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3260 netname, path, remark);
3263 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
3268 info.switch_value = level;
3269 info.ptr_share_ctr = 1;
3271 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3272 info.share.info502 = ctr_src.share.info502[i];
3274 /* finally modify the share on the dst server */
3275 result = cli_srvsvc_net_share_set_info(cli_dst, mem_ctx, netname, level, &info);
3277 if (!W_ERROR_IS_OK(result)) {
3278 printf("cannot set share-acl: %s\n", dos_errstr(result));
3284 nt_status = NT_STATUS_OK;
3287 if (got_dst_srvsvc_pipe) {
3288 cli_nt_session_close(cli_dst);
3289 cli_shutdown(cli_dst);
3297 * Migrate share-acls from a rpc-server to another
3299 * @param argc Standard main() style argc
3300 * @param argv Standard main() style argv. Initial components are already
3303 * @return A shell status integer (0 for success)
3305 static int rpc_share_migrate_security(int argc, const char **argv)
3309 printf("no server to migrate\n");
3313 return run_rpc_command(NULL, PI_SRVSVC, 0,
3314 rpc_share_migrate_security_internals,
3319 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3320 * from one server to another
3322 * @param argc Standard main() style argc
3323 * @param argv Standard main() style argv. Initial components are already
3326 * @return A shell status integer (0 for success)
3329 static int rpc_share_migrate_all(int argc, const char **argv)
3334 printf("no server to migrate\n");
3338 /* order is important. we don't want to be locked out by the share-acl
3339 * before copying files - gd */
3341 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3345 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3349 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3354 * 'net rpc share migrate' entrypoint.
3355 * @param argc Standard main() style argc
3356 * @param argv Standard main() style argv. Initial components are already
3359 static int rpc_share_migrate(int argc, const char **argv)
3362 struct functable func[] = {
3363 {"all", rpc_share_migrate_all},
3364 {"files", rpc_share_migrate_files},
3365 {"help", rpc_share_usage},
3366 {"security", rpc_share_migrate_security},
3367 {"shares", rpc_share_migrate_shares},
3371 net_mode_share = NET_MODE_SHARE_MIGRATE;
3373 return net_run_function(argc, argv, func, rpc_share_usage);
3382 static int num_server_aliases;
3383 static struct full_alias *server_aliases;
3386 * Add an alias to the static list.
3388 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3390 if (server_aliases == NULL)
3391 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3393 server_aliases[num_server_aliases] = *alias;
3394 num_server_aliases += 1;
3398 * For a specific domain on the server, fetch all the aliases
3399 * and their members. Add all of them to the server_aliases.
3402 rpc_fetch_domain_aliases(struct cli_state *cli, TALLOC_CTX *mem_ctx,
3403 POLICY_HND *connect_pol,
3404 const DOM_SID *domain_sid)
3406 uint32 start_idx, max_entries, num_entries, i;
3407 struct acct_info *groups;
3409 POLICY_HND domain_pol;
3411 /* Get domain policy handle */
3413 result = cli_samr_open_domain(cli, mem_ctx, connect_pol,
3414 MAXIMUM_ALLOWED_ACCESS,
3415 domain_sid, &domain_pol);
3416 if (!NT_STATUS_IS_OK(result))
3423 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
3424 &start_idx, max_entries,
3425 &groups, &num_entries);
3427 for (i = 0; i < num_entries; i++) {
3429 POLICY_HND alias_pol;
3430 struct full_alias alias;
3434 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
3435 MAXIMUM_ALLOWED_ACCESS,
3438 if (!NT_STATUS_IS_OK(result))
3441 result = cli_samr_query_aliasmem(cli, mem_ctx,
3445 if (!NT_STATUS_IS_OK(result))
3448 result = cli_samr_close(cli, mem_ctx, &alias_pol);
3449 if (!NT_STATUS_IS_OK(result))
3452 alias.members = NULL;
3454 if (alias.num_members > 0) {
3455 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3457 for (j = 0; j < alias.num_members; j++)
3458 sid_copy(&alias.members[j],
3462 sid_copy(&alias.sid, domain_sid);
3463 sid_append_rid(&alias.sid, groups[i].rid);
3465 push_alias(mem_ctx, &alias);
3467 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3469 result = NT_STATUS_OK;
3472 cli_samr_close(cli, mem_ctx, &domain_pol);
3478 * Dump server_aliases as names for debugging purposes.
3481 rpc_aliaslist_dump(const DOM_SID *domain_sid, const char *domain_name,
3482 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3483 int argc, const char **argv)
3489 result = cli_lsa_open_policy(cli, mem_ctx, True,
3490 SEC_RIGHTS_MAXIMUM_ALLOWED,
3492 if (!NT_STATUS_IS_OK(result))
3495 for (i=0; i<num_server_aliases; i++) {
3501 struct full_alias *alias = &server_aliases[i];
3503 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, 1,
3505 &domains, &names, &types);
3506 if (!NT_STATUS_IS_OK(result))
3509 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3511 if (alias->num_members == 0) {
3516 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol,
3519 &domains, &names, &types);
3521 if (!NT_STATUS_IS_OK(result) &&
3522 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3525 for (j=0; j<alias->num_members; j++)
3526 DEBUG(1, ("%s\\%s (%d); ",
3527 domains[j] ? domains[j] : "*unknown*",
3528 names[j] ? names[j] : "*unknown*",types[j]));
3532 cli_lsa_close(cli, mem_ctx, &lsa_pol);
3534 return NT_STATUS_OK;
3538 * Fetch a list of all server aliases and their members into
3542 rpc_aliaslist_internals(const DOM_SID *domain_sid, const char *domain_name,
3543 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3544 int argc, const char **argv)
3547 POLICY_HND connect_pol;
3549 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
3552 if (!NT_STATUS_IS_OK(result))
3555 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3556 &global_sid_Builtin);
3558 if (!NT_STATUS_IS_OK(result))
3561 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3564 cli_samr_close(cli, mem_ctx, &connect_pol);
3569 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3571 token->num_sids = 4;
3573 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
3575 token->user_sids[0] = *user_sid;
3576 sid_copy(&token->user_sids[1], &global_sid_World);
3577 sid_copy(&token->user_sids[2], &global_sid_Network);
3578 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3581 static void free_user_token(NT_USER_TOKEN *token)
3583 SAFE_FREE(token->user_sids);
3586 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
3590 for (i=0; i<token->num_sids; i++) {
3591 if (sid_compare(sid, &token->user_sids[i]) == 0)
3597 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3599 if (is_sid_in_token(token, sid))
3602 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3604 sid_copy(&token->user_sids[token->num_sids], sid);
3606 token->num_sids += 1;
3611 NT_USER_TOKEN token;
3614 static void dump_user_token(struct user_token *token)
3618 d_printf("%s\n", token->name);
3620 for (i=0; i<token->token.num_sids; i++) {
3621 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
3625 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
3629 for (i=0; i<alias->num_members; i++) {
3630 if (sid_compare(sid, &alias->members[i]) == 0)
3637 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
3641 for (i=0; i<num_server_aliases; i++) {
3642 if (is_alias_member(&sid, &server_aliases[i]))
3643 add_sid_to_token(token, &server_aliases[i].sid);
3648 * We got a user token with all the SIDs we can know about without asking the
3649 * server directly. These are the user and domain group sids. All of these can
3650 * be members of aliases. So scan the list of aliases for each of the SIDs and
3651 * add them to the token.
3654 static void collect_alias_memberships(NT_USER_TOKEN *token)
3656 int num_global_sids = token->num_sids;
3659 for (i=0; i<num_global_sids; i++) {
3660 collect_sid_memberships(token, token->user_sids[i]);
3664 static BOOL get_user_sids(const char *domain, const char *user,
3665 NT_USER_TOKEN *token)
3667 struct winbindd_request request;
3668 struct winbindd_response response;
3676 fstr_sprintf(full_name, "%s%c%s",
3677 domain, *lp_winbind_separator(), user);
3679 /* First let's find out the user sid */
3681 ZERO_STRUCT(request);
3682 ZERO_STRUCT(response);
3684 fstrcpy(request.data.name.dom_name, domain);
3685 fstrcpy(request.data.name.name, user);
3687 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
3689 if (result != NSS_STATUS_SUCCESS) {
3690 DEBUG(1, ("winbind could not find %s\n", full_name));
3694 if (response.data.sid.type != SID_NAME_USER) {
3695 DEBUG(1, ("%s is not a user\n", full_name));
3699 string_to_sid(&user_sid, response.data.sid.sid);
3701 init_user_token(token, &user_sid);
3703 /* And now the groups winbind knows about */
3705 ZERO_STRUCT(response);
3707 fstrcpy(request.data.username, full_name);
3709 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
3711 if (result != NSS_STATUS_SUCCESS) {
3712 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
3716 for (i = 0; i < response.data.num_entries; i++) {
3717 gid_t gid = ((gid_t *)response.extra_data)[i];
3720 struct winbindd_request sidrequest;
3721 struct winbindd_response sidresponse;
3723 ZERO_STRUCT(sidrequest);
3724 ZERO_STRUCT(sidresponse);
3726 sidrequest.data.gid = gid;
3728 result = winbindd_request_response(WINBINDD_GID_TO_SID,
3729 &sidrequest, &sidresponse);
3731 if (result != NSS_STATUS_SUCCESS) {
3732 DEBUG(1, ("winbind could not find SID of gid %d\n",
3737 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
3739 string_to_sid(&sid, sidresponse.data.sid.sid);
3740 add_sid_to_token(token, &sid);
3743 SAFE_FREE(response.extra_data);
3749 * Get a list of all user tokens we want to look at
3751 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
3753 struct winbindd_request request;
3754 struct winbindd_response response;
3755 const char *extra_data;
3758 struct user_token *result;
3760 if (lp_winbind_use_default_domain() &&
3761 (opt_target_workgroup == NULL)) {
3762 d_printf("winbind use default domain = yes set, please "
3763 "specify a workgroup\n");
3767 /* Send request to winbind daemon */
3769 ZERO_STRUCT(request);
3770 ZERO_STRUCT(response);
3772 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
3776 /* Look through extra data */
3778 if (!response.extra_data)
3781 extra_data = (const char *)response.extra_data;
3784 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3788 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
3790 if (result == NULL) {
3791 DEBUG(1, ("Could not malloc sid array\n"));
3795 extra_data = (const char *)response.extra_data;
3798 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3800 fstring domain, user;
3803 fstrcpy(result[i].name, name);
3805 p = strchr(name, *lp_winbind_separator());
3807 DEBUG(3, ("%s\n", name));
3810 fstrcpy(domain, opt_target_workgroup);
3811 fstrcpy(user, name);
3814 fstrcpy(domain, name);
3819 get_user_sids(domain, user, &(result[i].token));
3823 SAFE_FREE(response.extra_data);
3825 *user_tokens = result;
3830 static BOOL get_user_tokens_from_file(FILE *f,
3832 struct user_token **tokens)
3834 struct user_token *token = NULL;
3839 if (fgets(line, sizeof(line)-1, f) == NULL) {
3843 if (line[strlen(line)-1] == '\n')
3844 line[strlen(line)-1] = '\0';
3846 if (line[0] == ' ') {
3850 string_to_sid(&sid, &line[1]);
3852 if (token == NULL) {
3853 DEBUG(0, ("File does not begin with username"));
3857 add_sid_to_token(&token->token, &sid);
3861 /* And a new user... */
3864 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
3865 if (*tokens == NULL) {
3866 DEBUG(0, ("Could not realloc tokens\n"));
3870 token = &((*tokens)[*num_tokens-1]);
3872 fstrcpy(token->name, line);
3873 token->token.num_sids = 0;
3874 token->token.user_sids = NULL;
3883 * Show the list of all users that have access to a share
3886 static void show_userlist(struct cli_state *cli,
3887 TALLOC_CTX *mem_ctx, const char *netname,
3888 int num_tokens, struct user_token *tokens)
3891 SEC_DESC *share_sd = NULL;
3892 SEC_DESC *root_sd = NULL;
3894 SRV_SHARE_INFO info;
3898 result = cli_srvsvc_net_share_get_info(cli, mem_ctx, netname,
3901 if (!W_ERROR_IS_OK(result)) {
3902 DEBUG(1, ("Coult not query secdesc for share %s\n",
3907 share_sd = info.share.info502.info_502_str.sd;
3908 if (share_sd == NULL) {
3909 DEBUG(1, ("Got no secdesc for share %s\n",
3915 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3919 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
3922 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
3925 for (i=0; i<num_tokens; i++) {
3929 if (share_sd != NULL) {
3930 if (!se_access_check(share_sd, &tokens[i].token,
3931 1, &acc_granted, &status)) {
3932 DEBUG(1, ("Could not check share_sd for "
3938 if (!NT_STATUS_IS_OK(status))
3942 if (root_sd == NULL) {
3943 d_printf(" %s\n", tokens[i].name);
3947 if (!se_access_check(root_sd, &tokens[i].token,
3948 1, &acc_granted, &status)) {
3949 DEBUG(1, ("Could not check root_sd for user %s\n",
3954 if (!NT_STATUS_IS_OK(status))
3957 d_printf(" %s\n", tokens[i].name);
3961 cli_close(cli, fnum);
3973 static void collect_share(const char *name, uint32 m,
3974 const char *comment, void *state)
3976 struct share_list *share_list = (struct share_list *)state;
3978 if (m != STYPE_DISKTREE)
3981 share_list->num_shares += 1;
3982 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
3983 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
3986 static void rpc_share_userlist_usage(void)
3992 * List shares on a remote RPC server, including the security descriptors
3994 * All parameters are provided by the run_rpc_command function, except for
3995 * argc, argv which are passes through.
3997 * @param domain_sid The domain sid acquired from the remote server
3998 * @param cli A cli_state connected to the server.
3999 * @param mem_ctx Talloc context, destoyed on completion of the function.
4000 * @param argc Standard main() style argc
4001 * @param argv Standard main() style argv. Initial components are already
4004 * @return Normal NTSTATUS return.
4008 rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4009 const char *domain_name,
4010 struct cli_state *cli,
4011 TALLOC_CTX *mem_ctx,
4012 int argc, const char **argv)
4020 struct user_token *tokens = NULL;
4023 struct share_list share_list;
4026 rpc_share_userlist_usage();
4027 return NT_STATUS_UNSUCCESSFUL;
4033 f = fopen(argv[0], "r");
4037 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4038 return NT_STATUS_UNSUCCESSFUL;
4041 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4047 DEBUG(0, ("Could not read users from file\n"));
4048 return NT_STATUS_UNSUCCESSFUL;
4051 for (i=0; i<num_tokens; i++)
4052 collect_alias_memberships(&tokens[i].token);
4054 init_enum_hnd(&hnd, 0);
4056 share_list.num_shares = 0;
4057 share_list.shares = NULL;
4059 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4062 DEBUG(0, ("Error returning browse list: %s\n",
4067 for (i = 0; i < share_list.num_shares; i++) {
4068 char *netname = share_list.shares[i];
4070 if (netname[strlen(netname)-1] == '$')
4073 d_printf("%s\n", netname);
4075 show_userlist(cli, mem_ctx, netname,
4076 num_tokens, tokens);
4079 for (i=0; i<num_tokens; i++) {
4080 free_user_token(&tokens[i].token);
4083 SAFE_FREE(share_list.shares);
4085 return NT_STATUS_OK;
4089 rpc_share_allowedusers(int argc, const char **argv)
4093 result = run_rpc_command(NULL, PI_SAMR, 0,
4094 rpc_aliaslist_internals,
4099 result = run_rpc_command(NULL, PI_LSARPC, 0,
4105 return run_rpc_command(NULL, PI_SRVSVC, 0,
4106 rpc_share_allowedusers_internals,
4110 int net_usersidlist(int argc, const char **argv)
4113 struct user_token *tokens = NULL;
4117 net_usersidlist_usage(argc, argv);
4121 if (!get_user_tokens(&num_tokens, &tokens)) {
4122 DEBUG(0, ("Could not get the user/sid list\n"));
4126 for (i=0; i<num_tokens; i++) {
4127 dump_user_token(&tokens[i]);
4128 free_user_token(&tokens[i].token);
4135 int net_usersidlist_usage(int argc, const char **argv)
4137 d_printf("net usersidlist\n"
4138 "\tprints out a list of all users the running winbind knows\n"
4139 "\tabout, together with all their SIDs. This is used as\n"
4140 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4142 net_common_flags_usage(argc, argv);
4147 * 'net rpc share' entrypoint.
4148 * @param argc Standard main() style argc
4149 * @param argv Standard main() style argv. Initial components are already
4153 int net_rpc_share(int argc, const char **argv)
4155 struct functable func[] = {
4156 {"add", rpc_share_add},
4157 {"delete", rpc_share_delete},
4158 {"allowedusers", rpc_share_allowedusers},
4159 {"migrate", rpc_share_migrate},
4160 {"list", rpc_share_list},
4165 return run_rpc_command(NULL, PI_SRVSVC, 0,
4166 rpc_share_list_internals,
4169 return net_run_function(argc, argv, func, rpc_share_usage);
4172 /****************************************************************************/
4174 static int rpc_file_usage(int argc, const char **argv)
4176 return net_help_file(argc, argv);
4180 * Close a file on a remote RPC server
4182 * All parameters are provided by the run_rpc_command function, except for
4183 * argc, argv which are passes through.
4185 * @param domain_sid The domain sid acquired from the remote server
4186 * @param cli A cli_state connected to the server.
4187 * @param mem_ctx Talloc context, destoyed on completion of the function.
4188 * @param argc Standard main() style argc
4189 * @param argv Standard main() style argv. Initial components are already
4192 * @return Normal NTSTATUS return.
4195 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
4196 struct cli_state *cli,
4197 TALLOC_CTX *mem_ctx, int argc, const char **argv)
4200 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
4201 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4205 * Close a file on a remote RPC server
4207 * @param argc Standard main() style argc
4208 * @param argv Standard main() style argv. Initial components are already
4211 * @return A shell status integer (0 for success)
4213 static int rpc_file_close(int argc, const char **argv)
4216 DEBUG(1, ("No fileid given on close\n"));
4217 return(rpc_file_usage(argc, argv));
4220 return run_rpc_command(NULL, PI_SRVSVC, 0,
4221 rpc_file_close_internals,
4226 * Formatted print of open file info
4228 * @param info3 FILE_INFO_3 contents
4229 * @param str3 strings for FILE_INFO_3
4232 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
4234 fstring user = "", path = "";
4236 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
4237 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
4239 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4240 info3->id, user, info3->perms, info3->num_locks, path);
4244 * List open files on a remote RPC server
4246 * All parameters are provided by the run_rpc_command function, except for
4247 * argc, argv which are passes through.
4249 * @param domain_sid The domain sid acquired from the remote server
4250 * @param cli A cli_state connected to the server.
4251 * @param mem_ctx Talloc context, destoyed on completion of the function.
4252 * @param argc Standard main() style argc
4253 * @param argv Standard main() style argv. Initial components are already
4256 * @return Normal NTSTATUS return.
4260 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
4261 struct cli_state *cli,
4262 TALLOC_CTX *mem_ctx, int argc, const char **argv)
4264 SRV_FILE_INFO_CTR ctr;
4267 uint32 preferred_len = 0xffffffff, i;
4268 const char *username=NULL;
4270 init_enum_hnd(&hnd, 0);
4272 /* if argc > 0, must be user command */
4274 username = smb_xstrdup(argv[0]);
4276 result = cli_srvsvc_net_file_enum(
4277 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4279 if (!W_ERROR_IS_OK(result))
4282 /* Display results */
4285 "\nEnumerating open files on remote server:\n\n"\
4286 "\nFileId Opened by Perms Locks Path"\
4287 "\n------ --------- ----- ----- ---- \n");
4288 for (i = 0; i < ctr.num_entries; i++)
4289 display_file_info_3(&ctr.file.info3[i].info_3,
4290 &ctr.file.info3[i].info_3_str);
4292 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4297 * List files for a user on a remote RPC server
4299 * @param argc Standard main() style argc
4300 * @param argv Standard main() style argv. Initial components are already
4303 * @return A shell status integer (0 for success)
4305 static int rpc_file_user(int argc, const char **argv)
4308 DEBUG(1, ("No username given\n"));
4309 return(rpc_file_usage(argc, argv));
4312 return run_rpc_command(NULL, PI_SRVSVC, 0,
4313 rpc_file_list_internals,
4319 * 'net rpc file' entrypoint.
4320 * @param argc Standard main() style argc
4321 * @param argv Standard main() style argv. Initial components are already
4325 int net_rpc_file(int argc, const char **argv)
4327 struct functable func[] = {
4328 {"close", rpc_file_close},
4329 {"user", rpc_file_user},
4331 {"info", rpc_file_info},
4337 return run_rpc_command(NULL, PI_SRVSVC, 0,
4338 rpc_file_list_internals,
4341 return net_run_function(argc, argv, func, rpc_file_usage);
4344 /****************************************************************************/
4349 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4351 * All parameters are provided by the run_rpc_command function, except for
4352 * argc, argv which are passed through.
4354 * @param domain_sid The domain sid aquired from the remote server
4355 * @param cli A cli_state connected to the server.
4356 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4357 * @param argc Standard main() style argc
4358 * @param argv Standard main() style argv. Initial components are already
4361 * @return Normal NTSTATUS return.
4364 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4365 const char *domain_name,
4366 struct cli_state *cli,
4367 TALLOC_CTX *mem_ctx,
4368 int argc, const char **argv)
4370 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4372 result = cli_shutdown_abort(cli, mem_ctx);
4374 if (NT_STATUS_IS_OK(result)) {
4375 d_printf("\nShutdown successfully aborted\n");
4376 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4378 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4385 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4387 * All parameters are provided by the run_rpc_command function, except for
4388 * argc, argv which are passed through.
4390 * @param domain_sid The domain sid aquired from the remote server
4391 * @param cli A cli_state connected to the server.
4392 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4393 * @param argc Standard main() style argc
4394 * @param argv Standard main() style argv. Initial components are already
4397 * @return Normal NTSTATUS return.
4400 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
4401 const char *domain_name,
4402 struct cli_state *cli,
4403 TALLOC_CTX *mem_ctx,
4404 int argc, const char **argv)
4406 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4408 result = werror_to_ntstatus(cli_reg_abort_shutdown(cli, mem_ctx));
4410 if (NT_STATUS_IS_OK(result)) {
4411 d_printf("\nShutdown successfully aborted\n");
4412 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4414 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4420 * ABORT the Shut down of a remote RPC server
4422 * @param argc Standard main() style argc
4423 * @param argv Standard main() style argv. Initial components are already
4426 * @return A shell status integer (0 for success)
4429 static int rpc_shutdown_abort(int argc, const char **argv)
4431 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4432 rpc_shutdown_abort_internals,
4438 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4440 return run_rpc_command(NULL, PI_WINREG, 0,
4441 rpc_reg_shutdown_abort_internals,
4446 * Shut down a remote RPC Server via initshutdown pipe
4448 * All parameters are provided by the run_rpc_command function, except for
4449 * argc, argv which are passes through.
4451 * @param domain_sid The domain sid aquired from the remote server
4452 * @param cli A cli_state connected to the server.
4453 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4454 * @param argc Standard main() style argc
4455 * @param argc Standard main() style argv. Initial components are already
4458 * @return Normal NTSTATUS return.
4461 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
4462 const char *domain_name,
4463 struct cli_state *cli,
4464 TALLOC_CTX *mem_ctx,
4465 int argc, const char **argv)
4467 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4468 const char *msg = "This machine will be shutdown shortly";
4469 uint32 timeout = 20;
4475 timeout = opt_timeout;
4478 /* create an entry */
4479 result = cli_shutdown_init(cli, mem_ctx, msg, timeout, opt_reboot,
4482 if (NT_STATUS_IS_OK(result)) {
4483 d_printf("\nShutdown of remote machine succeeded\n");
4484 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4486 DEBUG(0,("Shutdown of remote machine failed!\n"));
4492 * Shut down a remote RPC Server via winreg pipe
4494 * All parameters are provided by the run_rpc_command function, except for
4495 * argc, argv which are passes through.
4497 * @param domain_sid The domain sid aquired from the remote server
4498 * @param cli A cli_state connected to the server.
4499 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4500 * @param argc Standard main() style argc
4501 * @param argc Standard main() style argv. Initial components are already
4504 * @return Normal NTSTATUS return.
4507 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
4508 const char *domain_name,
4509 struct cli_state *cli,
4510 TALLOC_CTX *mem_ctx,
4511 int argc, const char **argv)
4513 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4514 const char *msg = "This machine will be shutdown shortly";
4515 uint32 timeout = 20;
4520 struct poptOption long_options[] = {
4521 {"message", 'm', POPT_ARG_STRING, &msg},
4522 {"timeout", 't', POPT_ARG_INT, &timeout},
4523 {"reboot", 'r', POPT_ARG_NONE, &reboot},
4524 {"force", 'f', POPT_ARG_NONE, &force},
4528 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
4529 POPT_CONTEXT_KEEP_FIRST);
4531 rc = poptGetNextOpt(pc);
4534 /* an error occurred during option processing */
4535 DEBUG(0, ("%s: %s\n",
4536 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
4538 return NT_STATUS_INVALID_PARAMETER;
4545 timeout = opt_timeout;
4548 /* create an entry */
4549 result = werror_to_ntstatus(cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force));
4551 if (NT_STATUS_IS_OK(result)) {
4552 d_printf("\nShutdown of remote machine succeeded\n");
4553 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4556 DEBUG(0,("Shutdown of remote machine failed!\n"));
4562 * Shut down a remote RPC server
4564 * @param argc Standard main() style argc
4565 * @param argc Standard main() style argv. Initial components are already
4568 * @return A shell status integer (0 for success)
4571 static int rpc_shutdown(int argc, const char **argv)
4573 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4574 rpc_init_shutdown_internals,
4579 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4581 return run_rpc_command(NULL, PI_WINREG, 0, rpc_reg_shutdown_internals,
4585 /***************************************************************************
4586 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
4588 ***************************************************************************/
4591 * Add interdomain trust account to the RPC server.
4592 * All parameters (except for argc and argv) are passed by run_rpc_command
4595 * @param domain_sid The domain sid acquired from the server
4596 * @param cli A cli_state connected to the server.
4597 * @param mem_ctx Talloc context, destoyed on completion of the function.
4598 * @param argc Standard main() style argc
4599 * @param argc Standard main() style argv. Initial components are already
4602 * @return normal NTSTATUS return code
4605 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
4606 const char *domain_name,
4607 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4608 int argc, const char **argv) {
4610 POLICY_HND connect_pol, domain_pol, user_pol;
4611 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4614 uint32 unknown, user_rid;
4617 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
4618 return NT_STATUS_INVALID_PARAMETER;
4622 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4625 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
4626 return NT_STATUS_NO_MEMORY;
4629 strupper_m(acct_name);
4631 /* Get samr policy handle */
4632 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4634 if (!NT_STATUS_IS_OK(result)) {
4638 /* Get domain policy handle */
4639 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4640 MAXIMUM_ALLOWED_ACCESS,
4641 domain_sid, &domain_pol);
4642 if (!NT_STATUS_IS_OK(result)) {
4646 /* Create trusting domain's account */
4647 acb_info = ACB_NORMAL;
4648 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
4649 mimir: yes, most probably it is */
4651 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
4652 acct_name, acb_info, unknown,
4653 &user_pol, &user_rid);
4654 if (!NT_STATUS_IS_OK(result)) {
4659 SAM_USERINFO_CTR ctr;
4660 SAM_USER_INFO_23 p23;
4666 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
4670 ZERO_STRUCT(notime);
4674 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
4675 acb_info = ACB_DOMTRUST;
4677 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
4678 ¬ime, ¬ime, ¬ime,
4679 nostr, nostr, nostr, nostr, nostr,
4680 nostr, nostr, nostr, nostr, nostr,
4681 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
4682 0, 0, (char *)pwbuf);
4683 ctr.switch_value = 23;
4684 ctr.info.id23 = &p23;
4685 p23.passmustchange = 0;
4687 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 23,
4688 &cli->user_session_key, &ctr);
4690 if (!NT_STATUS_IS_OK(result)) {
4691 DEBUG(0,("Could not set trust account password: %s\n",
4692 nt_errstr(result)));
4698 SAFE_FREE(acct_name);
4703 * Create interdomain trust account for a remote domain.
4705 * @param argc standard argc
4706 * @param argv standard argv without initial components
4708 * @return Integer status (0 means success)
4711 static int rpc_trustdom_add(int argc, const char **argv)
4714 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
4717 d_printf("Usage: net rpc trustdom add <domain>\n");
4724 * Remove interdomain trust account from the RPC server.
4725 * All parameters (except for argc and argv) are passed by run_rpc_command
4728 * @param domain_sid The domain sid acquired from the server
4729 * @param cli A cli_state connected to the server.
4730 * @param mem_ctx Talloc context, destoyed on completion of the function.
4731 * @param argc Standard main() style argc
4732 * @param argc Standard main() style argv. Initial components are already
4735 * @return normal NTSTATUS return code
4738 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
4739 const char *domain_name,
4740 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4741 int argc, const char **argv) {
4743 POLICY_HND connect_pol, domain_pol, user_pol;
4744 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4747 DOM_SID trust_acct_sid;
4748 uint32 *user_rids, num_rids, *name_types;
4749 uint32 flags = 0x000003e8; /* Unknown */
4752 d_printf("Usage: net rpc trustdom del <domain_name>\n");
4753 return NT_STATUS_INVALID_PARAMETER;
4757 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4759 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
4761 if (acct_name == NULL)
4762 return NT_STATUS_NO_MEMORY;
4764 strupper_m(acct_name);
4766 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
4767 names[0] = acct_name;
4770 /* Get samr policy handle */
4771 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4773 if (!NT_STATUS_IS_OK(result)) {
4777 /* Get domain policy handle */
4778 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4779 MAXIMUM_ALLOWED_ACCESS,
4780 domain_sid, &domain_pol);
4781 if (!NT_STATUS_IS_OK(result)) {
4785 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, flags, 1,
4787 &user_rids, &name_types);
4789 if (!NT_STATUS_IS_OK(result)) {
4793 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
4794 MAXIMUM_ALLOWED_ACCESS,
4795 user_rids[0], &user_pol);
4797 if (!NT_STATUS_IS_OK(result)) {
4801 /* append the rid to the domain sid */
4802 sid_copy(&trust_acct_sid, domain_sid);
4803 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
4807 /* remove the sid */
4809 result = cli_samr_remove_sid_foreign_domain(cli, mem_ctx, &user_pol,
4812 if (!NT_STATUS_IS_OK(result)) {
4818 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
4820 if (!NT_STATUS_IS_OK(result)) {
4824 if (!NT_STATUS_IS_OK(result)) {
4825 DEBUG(0,("Could not set trust account password: %s\n",
4826 nt_errstr(result)));
4835 * Delete interdomain trust account for a remote domain.
4837 * @param argc standard argc
4838 * @param argv standard argv without initial components
4840 * @return Integer status (0 means success)
4843 static int rpc_trustdom_del(int argc, const char **argv)
4846 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
4849 d_printf("Usage: net rpc trustdom del <domain>\n");
4856 * Establish trust relationship to a trusting domain.
4857 * Interdomain account must already be created on remote PDC.
4859 * @param argc standard argc
4860 * @param argv standard argv without initial components
4862 * @return Integer status (0 means success)
4865 static int rpc_trustdom_establish(int argc, const char **argv)
4867 struct cli_state *cli;
4868 struct in_addr server_ip;
4869 POLICY_HND connect_hnd;
4870 TALLOC_CTX *mem_ctx;
4872 DOM_SID *domain_sid;
4873 smb_ucs2_t *uni_domain_name;
4876 char* domain_name_pol;
4881 * Connect to \\server\ipc$ as 'our domain' account with password
4885 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
4889 domain_name = smb_xstrdup(argv[0]);
4890 strupper_m(domain_name);
4892 /* account name used at first is our domain's name with '$' */
4893 asprintf(&acct_name, "%s$", lp_workgroup());
4894 strupper_m(acct_name);
4897 * opt_workgroup will be used by connection functions further,
4898 * hence it should be set to remote domain name instead of ours
4900 if (opt_workgroup) {
4901 opt_workgroup = smb_xstrdup(domain_name);
4904 opt_user_name = acct_name;
4906 /* find the domain controller */
4907 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
4908 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
4912 /* connect to ipc$ as username/password */
4913 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
4914 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
4916 /* Is it trusting domain account for sure ? */
4917 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
4918 nt_errstr(nt_status)));
4923 * Connect to \\server\ipc$ again (this time anonymously)
4926 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
4928 if (NT_STATUS_IS_ERR(nt_status)) {
4929 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
4930 domain_name, nt_errstr(nt_status)));
4934 * Use NetServerEnum2 to make sure we're talking to a proper server
4937 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
4938 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
4939 for domain %s\n", domain_name));
4942 if (!(mem_ctx = talloc_init("establishing trust relationship to "
4943 "domain %s", domain_name))) {
4944 DEBUG(0, ("talloc_init() failed\n"));
4950 * Call LsaOpenPolicy and LsaQueryInfo
4953 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4954 DEBUG(0, ("Could not initialise lsa pipe\n"));
4959 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
4961 if (NT_STATUS_IS_ERR(nt_status)) {
4962 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4963 nt_errstr(nt_status)));
4967 /* Querying info level 5 */
4969 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
4971 &domain_name_pol, &domain_sid);
4972 if (NT_STATUS_IS_ERR(nt_status)) {
4973 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4974 nt_errstr(nt_status)));
4978 if (push_ucs2_talloc(mem_ctx, &uni_domain_name, domain_name_pol) < 0) {
4979 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4984 /* There should be actually query info level 3 (following nt serv behaviour),
4985 but I still don't know if it's _really_ necessary */
4988 * Store the password in secrets db
4991 if (!secrets_store_trusted_domain_password(domain_name,
4993 strlen_w(uni_domain_name)+1,
4996 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5001 * Close the pipes and clean up
5004 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
5005 if (NT_STATUS_IS_ERR(nt_status)) {
5006 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5007 nt_errstr(nt_status)));
5011 if (cli->pipes[cli->pipe_idx].fnum)
5012 cli_nt_session_close(cli);
5016 talloc_destroy(mem_ctx);
5018 d_printf("Trust to domain %s established\n", domain_name);
5023 * Revoke trust relationship to the remote domain
5025 * @param argc standard argc
5026 * @param argv standard argv without initial components
5028 * @return Integer status (0 means success)
5031 static int rpc_trustdom_revoke(int argc, const char **argv)
5035 if (argc < 1) return -1;
5037 /* generate upper cased domain name */
5038 domain_name = smb_xstrdup(argv[0]);
5039 strupper_m(domain_name);
5041 /* delete password of the trust */
5042 if (!trusted_domain_password_delete(domain_name)) {
5043 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5052 * Usage for 'net rpc trustdom' command
5054 * @param argc standard argc
5055 * @param argv standard argv without inital components
5057 * @return Integer status returned to shell
5060 static int rpc_trustdom_usage(int argc, const char **argv)
5062 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5063 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5064 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5065 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5066 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5067 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5072 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5073 const char *domain_name,
5074 struct cli_state *cli, TALLOC_CTX *mem_ctx,
5075 int argc, const char **argv)
5078 sid_to_string(str_sid, domain_sid);
5079 d_printf("%s\n", str_sid);
5080 return NT_STATUS_OK;
5083 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5085 fstring ascii_sid, padding;
5086 int pad_len, col_len = 20;
5088 /* convert sid into ascii string */
5089 sid_to_string(ascii_sid, dom_sid);
5091 /* calculate padding space for d_printf to look nicer */
5092 pad_len = col_len - strlen(trusted_dom_name);
5093 padding[pad_len] = 0;
5094 do padding[--pad_len] = ' '; while (pad_len);
5096 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5099 static NTSTATUS vampire_trusted_domain(struct cli_state *cli,
5100 TALLOC_CTX *mem_ctx,
5103 const char *trusted_dom_name)
5106 LSA_TRUSTED_DOMAIN_INFO *info;
5109 smb_ucs2_t *uni_dom_name;
5111 nt_status = cli_lsa_query_trusted_domain_info_by_sid(cli, mem_ctx, pol, 4, &dom_sid, &info);
5113 if (NT_STATUS_IS_ERR(nt_status)) {
5114 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5115 nt_errstr(nt_status)));
5119 data = data_blob(NULL, info->password.password.length);
5121 memcpy(data.data, info->password.password.data, info->password.password.length);
5122 data.length = info->password.password.length;
5124 cleartextpwd = decrypt_trustdom_secret(cli->pwd.password, &data);
5126 if (cleartextpwd == NULL) {
5127 DEBUG(0,("retrieved NULL password\n"));
5128 nt_status = NT_STATUS_UNSUCCESSFUL;
5132 if (push_ucs2_talloc(mem_ctx, &uni_dom_name, trusted_dom_name) < 0) {
5133 DEBUG(0, ("Could not convert domain name %s to unicode\n",
5135 nt_status = NT_STATUS_UNSUCCESSFUL;
5139 if (!secrets_store_trusted_domain_password(trusted_dom_name,
5141 strlen_w(uni_dom_name)+1,
5144 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5145 nt_status = NT_STATUS_UNSUCCESSFUL;
5149 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5150 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5153 SAFE_FREE(cleartextpwd);
5154 data_blob_free(&data);
5159 static int rpc_trustdom_vampire(int argc, const char **argv)
5161 /* common variables */
5162 TALLOC_CTX* mem_ctx;
5163 struct cli_state *cli;
5165 const char *domain_name = NULL;
5166 DOM_SID *queried_dom_sid;
5167 POLICY_HND connect_hnd;
5169 /* trusted domains listing variables */
5170 unsigned int num_domains, enum_ctx = 0;
5172 DOM_SID *domain_sids;
5173 char **trusted_dom_names;
5178 * Listing trusted domains (stored in secrets.tdb, if local)
5181 mem_ctx = talloc_init("trust relationships vampire");
5184 * set domain and pdc name to local samba server (default)
5185 * or to remote one given in command line
5188 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5189 domain_name = opt_workgroup;
5190 opt_target_workgroup = opt_workgroup;
5192 fstrcpy(pdc_name, global_myname());
5193 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5194 opt_target_workgroup = domain_name;
5197 /* open \PIPE\lsarpc and open policy handle */
5198 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5199 DEBUG(0, ("Couldn't connect to domain controller\n"));
5203 if (!cli_nt_session_open(cli, PI_LSARPC)) {
5204 DEBUG(0, ("Could not initialise lsa pipe\n"));
5208 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5210 if (NT_STATUS_IS_ERR(nt_status)) {
5211 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5212 nt_errstr(nt_status)));
5216 /* query info level 5 to obtain sid of a domain being queried */
5217 nt_status = cli_lsa_query_info_policy(
5218 cli, mem_ctx, &connect_hnd, 5 /* info level */,
5219 &dummy, &queried_dom_sid);
5221 if (NT_STATUS_IS_ERR(nt_status)) {
5222 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5223 nt_errstr(nt_status)));
5228 * Keep calling LsaEnumTrustdom over opened pipe until
5229 * the end of enumeration is reached
5232 d_printf("Vampire trusted domains:\n\n");
5235 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
5237 &trusted_dom_names, &domain_sids);
5239 if (NT_STATUS_IS_ERR(nt_status)) {
5240 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5241 nt_errstr(nt_status)));
5245 for (i = 0; i < num_domains; i++) {
5247 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5249 nt_status = vampire_trusted_domain(cli, mem_ctx, &connect_hnd,
5250 domain_sids[i], trusted_dom_names[i]);
5251 if (!NT_STATUS_IS_OK(nt_status))
5256 * in case of no trusted domains say something rather
5257 * than just display blank line
5259 if (!num_domains) d_printf("none\n");
5261 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5263 /* close this connection before doing next one */
5264 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
5265 if (NT_STATUS_IS_ERR(nt_status)) {
5266 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5267 nt_errstr(nt_status)));
5271 /* close lsarpc pipe and connection to IPC$ */
5272 cli_nt_session_close(cli);
5275 talloc_destroy(mem_ctx);
5279 static int rpc_trustdom_list(int argc, const char **argv)
5281 /* common variables */
5282 TALLOC_CTX* mem_ctx;
5283 struct cli_state *cli, *remote_cli;
5285 const char *domain_name = NULL;
5286 DOM_SID *queried_dom_sid;
5288 int ascii_dom_name_len;
5289 POLICY_HND connect_hnd;
5291 /* trusted domains listing variables */
5292 unsigned int num_domains, enum_ctx = 0;
5293 int i, pad_len, col_len = 20;
5294 DOM_SID *domain_sids;
5295 char **trusted_dom_names;
5299 /* trusting domains listing variables */
5300 POLICY_HND domain_hnd;
5301 char **trusting_dom_names;
5302 uint32 *trusting_dom_rids;
5305 * Listing trusted domains (stored in secrets.tdb, if local)
5308 mem_ctx = talloc_init("trust relationships listing");
5311 * set domain and pdc name to local samba server (default)
5312 * or to remote one given in command line
5315 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5316 domain_name = opt_workgroup;
5317 opt_target_workgroup = opt_workgroup;
5319 fstrcpy(pdc_name, global_myname());
5320 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5321 opt_target_workgroup = domain_name;
5324 /* open \PIPE\lsarpc and open policy handle */
5325 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5326 DEBUG(0, ("Couldn't connect to domain controller\n"));
5330 if (!cli_nt_session_open(cli, PI_LSARPC)) {
5331 DEBUG(0, ("Could not initialise lsa pipe\n"));
5335 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5337 if (NT_STATUS_IS_ERR(nt_status)) {
5338 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5339 nt_errstr(nt_status)));
5343 /* query info level 5 to obtain sid of a domain being queried */
5344 nt_status = cli_lsa_query_info_policy(
5345 cli, mem_ctx, &connect_hnd, 5 /* info level */,
5346 &dummy, &queried_dom_sid);
5348 if (NT_STATUS_IS_ERR(nt_status)) {
5349 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5350 nt_errstr(nt_status)));
5355 * Keep calling LsaEnumTrustdom over opened pipe until
5356 * the end of enumeration is reached
5359 d_printf("Trusted domains list:\n\n");
5362 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
5364 &trusted_dom_names, &domain_sids);
5366 if (NT_STATUS_IS_ERR(nt_status)) {
5367 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5368 nt_errstr(nt_status)));
5372 for (i = 0; i < num_domains; i++) {
5373 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5377 * in case of no trusted domains say something rather
5378 * than just display blank line
5380 if (!num_domains) d_printf("none\n");
5382 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5384 /* close this connection before doing next one */
5385 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
5386 if (NT_STATUS_IS_ERR(nt_status)) {
5387 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5388 nt_errstr(nt_status)));
5392 cli_nt_session_close(cli);
5395 * Listing trusting domains (stored in passdb backend, if local)
5398 d_printf("\nTrusting domains list:\n\n");
5401 * Open \PIPE\samr and get needed policy handles
5403 if (!cli_nt_session_open(cli, PI_SAMR)) {
5404 DEBUG(0, ("Could not initialise samr pipe\n"));
5409 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
5411 if (!NT_STATUS_IS_OK(nt_status)) {
5412 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
5413 nt_errstr(nt_status)));
5417 /* SamrOpenDomain - we have to open domain policy handle in order to be
5418 able to enumerate accounts*/
5419 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
5420 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
5421 queried_dom_sid, &domain_hnd);
5422 if (!NT_STATUS_IS_OK(nt_status)) {
5423 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
5424 nt_errstr(nt_status)));
5429 * perform actual enumeration
5432 enum_ctx = 0; /* reset enumeration context from last enumeration */
5435 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
5436 &enum_ctx, ACB_DOMTRUST, 0xffff,
5437 &trusting_dom_names, &trusting_dom_rids,
5439 if (NT_STATUS_IS_ERR(nt_status)) {
5440 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
5441 nt_errstr(nt_status)));
5445 for (i = 0; i < num_domains; i++) {
5448 * get each single domain's sid (do we _really_ need this ?):
5449 * 1) connect to domain's pdc
5450 * 2) query the pdc for domain's sid
5453 /* get rid of '$' tail */
5454 ascii_dom_name_len = strlen(trusting_dom_names[i]);
5455 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
5456 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
5458 /* calculate padding space for d_printf to look nicer */
5459 pad_len = col_len - strlen(trusting_dom_names[i]);
5460 padding[pad_len] = 0;
5461 do padding[--pad_len] = ' '; while (pad_len);
5463 /* set opt_* variables to remote domain */
5464 strupper_m(trusting_dom_names[i]);
5465 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
5466 opt_target_workgroup = opt_workgroup;
5468 d_printf("%s%s", trusting_dom_names[i], padding);
5470 /* connect to remote domain controller */
5471 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
5473 /* query for domain's sid */
5474 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
5475 d_printf("couldn't get domain's sid\n");
5477 cli_shutdown(remote_cli);
5480 d_printf("domain controller is not responding\n");
5484 if (!num_domains) d_printf("none\n");
5486 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5488 /* close opened samr and domain policy handles */
5489 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
5490 if (!NT_STATUS_IS_OK(nt_status)) {
5491 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
5494 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
5495 if (!NT_STATUS_IS_OK(nt_status)) {
5496 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
5499 /* close samr pipe and connection to IPC$ */
5500 cli_nt_session_close(cli);
5503 talloc_destroy(mem_ctx);
5508 * Entrypoint for 'net rpc trustdom' code
5510 * @param argc standard argc
5511 * @param argv standard argv without initial components
5513 * @return Integer status (0 means success)
5516 static int rpc_trustdom(int argc, const char **argv)
5518 struct functable func[] = {
5519 {"add", rpc_trustdom_add},
5520 {"del", rpc_trustdom_del},
5521 {"establish", rpc_trustdom_establish},
5522 {"revoke", rpc_trustdom_revoke},
5523 {"help", rpc_trustdom_usage},
5524 {"list", rpc_trustdom_list},
5525 {"vampire", rpc_trustdom_vampire},
5530 rpc_trustdom_usage(argc, argv);
5534 return (net_run_function(argc, argv, func, rpc_user_usage));
5538 * Check if a server will take rpc commands
5539 * @param flags Type of server to connect to (PDC, DMB, localhost)
5540 * if the host is not explicitly specified
5541 * @return BOOL (true means rpc supported)
5543 BOOL net_rpc_check(unsigned flags)
5545 struct cli_state cli;
5547 struct in_addr server_ip;
5548 char *server_name = NULL;
5550 /* flags (i.e. server type) may depend on command */
5551 if (!net_find_server(flags, &server_ip, &server_name))
5555 if (cli_initialise(&cli) == False)
5558 if (!cli_connect(&cli, server_name, &server_ip))
5560 if (!attempt_netbios_session_request(&cli, global_myname(),
5561 server_name, &server_ip))
5563 if (!cli_negprot(&cli))
5565 if (cli.protocol < PROTOCOL_NT1)
5574 /* dump sam database via samsync rpc calls */
5575 static int rpc_samdump(int argc, const char **argv) {
5576 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
5580 /* syncronise sam database via samsync rpc calls */
5581 static int rpc_vampire(int argc, const char **argv) {
5582 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
5587 * Migrate everything from a print-server
5589 * @param argc Standard main() style argc
5590 * @param argv Standard main() style argv. Initial components are already
5593 * @return A shell status integer (0 for success)
5595 * The order is important !
5596 * To successfully add drivers the print-queues have to exist !
5597 * Applying ACLs should be the last step, because you're easily locked out
5600 static int rpc_printer_migrate_all(int argc, const char **argv)
5605 printf("no server to migrate\n");
5609 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
5613 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
5617 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
5621 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
5625 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
5630 * Migrate print-drivers from a print-server
5632 * @param argc Standard main() style argc
5633 * @param argv Standard main() style argv. Initial components are already
5636 * @return A shell status integer (0 for success)
5638 static int rpc_printer_migrate_drivers(int argc, const char **argv)
5641 printf("no server to migrate\n");
5645 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5646 rpc_printer_migrate_drivers_internals,
5651 * Migrate print-forms from a print-server
5653 * @param argc Standard main() style argc
5654 * @param argv Standard main() style argv. Initial components are already
5657 * @return A shell status integer (0 for success)
5659 static int rpc_printer_migrate_forms(int argc, const char **argv)
5662 printf("no server to migrate\n");
5666 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5667 rpc_printer_migrate_forms_internals,
5672 * Migrate printers from a print-server
5674 * @param argc Standard main() style argc
5675 * @param argv Standard main() style argv. Initial components are already
5678 * @return A shell status integer (0 for success)
5680 static int rpc_printer_migrate_printers(int argc, const char **argv)
5683 printf("no server to migrate\n");
5687 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5688 rpc_printer_migrate_printers_internals,
5693 * Migrate printer-ACLs from a print-server
5695 * @param argc Standard main() style argc
5696 * @param argv Standard main() style argv. Initial components are already
5699 * @return A shell status integer (0 for success)
5701 static int rpc_printer_migrate_security(int argc, const char **argv)
5704 printf("no server to migrate\n");
5708 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5709 rpc_printer_migrate_security_internals,
5714 * Migrate printer-settings from a print-server
5716 * @param argc Standard main() style argc
5717 * @param argv Standard main() style argv. Initial components are already
5720 * @return A shell status integer (0 for success)
5722 static int rpc_printer_migrate_settings(int argc, const char **argv)
5725 printf("no server to migrate\n");
5729 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5730 rpc_printer_migrate_settings_internals,
5735 * 'net rpc printer' entrypoint.
5736 * @param argc Standard main() style argc
5737 * @param argv Standard main() style argv. Initial components are already
5741 int rpc_printer_migrate(int argc, const char **argv)
5744 /* ouch: when addriver and setdriver are called from within
5745 rpc_printer_migrate_drivers_internals, the printer-queue already
5748 struct functable func[] = {
5749 {"all", rpc_printer_migrate_all},
5750 {"drivers", rpc_printer_migrate_drivers},
5751 {"forms", rpc_printer_migrate_forms},
5752 {"help", rpc_printer_usage},
5753 {"printers", rpc_printer_migrate_printers},
5754 {"security", rpc_printer_migrate_security},
5755 {"settings", rpc_printer_migrate_settings},
5759 return net_run_function(argc, argv, func, rpc_printer_usage);
5764 * List printers on a remote RPC server
5766 * @param argc Standard main() style argc
5767 * @param argv Standard main() style argv. Initial components are already
5770 * @return A shell status integer (0 for success)
5772 static int rpc_printer_list(int argc, const char **argv)
5775 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5776 rpc_printer_list_internals,
5781 * List printer-drivers on a remote RPC server
5783 * @param argc Standard main() style argc
5784 * @param argv Standard main() style argv. Initial components are already
5787 * @return A shell status integer (0 for success)
5789 static int rpc_printer_driver_list(int argc, const char **argv)
5792 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5793 rpc_printer_driver_list_internals,
5798 * Publish printer in ADS via MSRPC
5800 * @param argc Standard main() style argc
5801 * @param argv Standard main() style argv. Initial components are already
5804 * @return A shell status integer (0 for success)
5806 static int rpc_printer_publish_publish(int argc, const char **argv)
5809 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5810 rpc_printer_publish_publish_internals,
5815 * Update printer in ADS via MSRPC
5817 * @param argc Standard main() style argc
5818 * @param argv Standard main() style argv. Initial components are already
5821 * @return A shell status integer (0 for success)
5823 static int rpc_printer_publish_update(int argc, const char **argv)
5826 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5827 rpc_printer_publish_update_internals,
5832 * UnPublish printer in ADS via MSRPC
5834 * @param argc Standard main() style argc
5835 * @param argv Standard main() style argv. Initial components are already
5838 * @return A shell status integer (0 for success)
5840 static int rpc_printer_publish_unpublish(int argc, const char **argv)
5843 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5844 rpc_printer_publish_unpublish_internals,
5849 * List published printers via MSRPC
5851 * @param argc Standard main() style argc
5852 * @param argv Standard main() style argv. Initial components are already
5855 * @return A shell status integer (0 for success)
5857 static int rpc_printer_publish_list(int argc, const char **argv)
5860 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5861 rpc_printer_publish_list_internals,
5867 * Publish printer in ADS
5869 * @param argc Standard main() style argc
5870 * @param argv Standard main() style argv. Initial components are already
5873 * @return A shell status integer (0 for success)
5875 static int rpc_printer_publish(int argc, const char **argv)
5878 struct functable func[] = {
5879 {"publish", rpc_printer_publish_publish},
5880 {"update", rpc_printer_publish_update},
5881 {"unpublish", rpc_printer_publish_unpublish},
5882 {"list", rpc_printer_publish_list},
5883 {"help", rpc_printer_usage},
5888 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5889 rpc_printer_publish_list_internals,
5892 return net_run_function(argc, argv, func, rpc_printer_usage);
5898 * Display rpc printer help page.
5899 * @param argc Standard main() style argc
5900 * @param argv Standard main() style argv. Initial components are already
5903 int rpc_printer_usage(int argc, const char **argv)
5905 return net_help_printer(argc, argv);
5909 * 'net rpc printer' entrypoint.
5910 * @param argc Standard main() style argc
5911 * @param argv Standard main() style argv. Initial components are already
5914 int net_rpc_printer(int argc, const char **argv)
5916 struct functable func[] = {
5917 {"list", rpc_printer_list},
5918 {"migrate", rpc_printer_migrate},
5919 {"driver", rpc_printer_driver_list},
5920 {"publish", rpc_printer_publish},
5925 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5926 rpc_printer_list_internals,
5929 return net_run_function(argc, argv, func, rpc_printer_usage);
5932 /****************************************************************************/
5936 * Basic usage function for 'net rpc'
5937 * @param argc Standard main() style argc
5938 * @param argv Standard main() style argv. Initial components are already
5942 int net_rpc_usage(int argc, const char **argv)
5944 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
5945 d_printf(" net rpc join \t\t\tto join a domain \n");
5946 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
5947 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
5948 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
5949 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
5950 d_printf(" net rpc group \t\tto list groups\n");
5951 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
5952 d_printf(" net rpc printer \t\tto list and migrate printers\n");
5953 d_printf(" net rpc file \t\t\tto list open files\n");
5954 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
5955 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
5956 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
5957 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
5958 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
5959 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
5960 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
5961 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
5962 d_printf(" net rpc registry\t\tto manage registry hives\n");
5964 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
5965 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
5966 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
5967 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
5968 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
5974 * Help function for 'net rpc'. Calls command specific help if requested
5975 * or displays usage of net rpc
5976 * @param argc Standard main() style argc
5977 * @param argv Standard main() style argv. Initial components are already
5981 int net_rpc_help(int argc, const char **argv)
5983 struct functable func[] = {
5984 {"join", rpc_join_usage},
5985 {"user", rpc_user_usage},
5986 {"group", rpc_group_usage},
5987 {"share", rpc_share_usage},
5988 /*{"changetrustpw", rpc_changetrustpw_usage}, */
5989 {"trustdom", rpc_trustdom_usage},
5990 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
5991 /*{"shutdown", rpc_shutdown_usage}, */
5992 {"vampire", rpc_vampire_usage},
5997 net_rpc_usage(argc, argv);
6001 return (net_run_function(argc, argv, func, rpc_user_usage));
6006 * 'net rpc' entrypoint.
6007 * @param argc Standard main() style argc
6008 * @param argv Standard main() style argv. Initial components are already
6012 int net_rpc(int argc, const char **argv)
6014 struct functable func[] = {
6015 {"info", net_rpc_info},
6016 {"join", net_rpc_join},
6017 {"oldjoin", net_rpc_oldjoin},
6018 {"testjoin", net_rpc_testjoin},
6019 {"user", net_rpc_user},
6020 {"password", rpc_user_password},
6021 {"group", net_rpc_group},
6022 {"share", net_rpc_share},
6023 {"file", net_rpc_file},
6024 {"printer", net_rpc_printer},
6025 {"changetrustpw", net_rpc_changetrustpw},
6026 {"trustdom", rpc_trustdom},
6027 {"abortshutdown", rpc_shutdown_abort},
6028 {"shutdown", rpc_shutdown},
6029 {"samdump", rpc_samdump},
6030 {"vampire", rpc_vampire},
6031 {"getsid", net_rpc_getsid},
6032 {"rights", net_rpc_rights},
6033 {"service", net_rpc_service},
6034 {"registry", net_rpc_registry},
6035 {"help", net_rpc_help},
6038 return net_run_function(argc, argv, func, net_rpc_usage);