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)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "utils/net.h"
26 static int net_mode_share;
31 * @brief RPC based subcommands for the 'net' utility.
33 * This file should contain much of the functionality that used to
34 * be found in rpcclient, execpt that the commands should change
35 * less often, and the fucntionality should be sane (the user is not
36 * expected to know a rid/sid before they conduct an operation etc.)
38 * @todo Perhaps eventually these should be split out into a number
39 * of files, as this could get quite big.
44 * Many of the RPC functions need the domain sid. This function gets
45 * it at the start of every run
47 * @param cli A cli_state already connected to the remote machine
49 * @return The Domain SID of the remote machine.
52 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
53 DOM_SID **domain_sid, char **domain_name)
55 struct rpc_pipe_client *lsa_pipe;
57 NTSTATUS result = NT_STATUS_OK;
58 uint32 info_class = 5;
60 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
62 d_fprintf(stderr, "Could not initialise lsa pipe\n");
66 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False,
67 SEC_RIGHTS_MAXIMUM_ALLOWED,
69 if (!NT_STATUS_IS_OK(result)) {
70 d_fprintf(stderr, "open_policy failed: %s\n",
75 result = rpccli_lsa_query_info_policy(lsa_pipe, mem_ctx, &pol,
76 info_class, domain_name,
78 if (!NT_STATUS_IS_OK(result)) {
79 d_fprintf(stderr, "lsaquery failed: %s\n",
84 rpccli_lsa_close(lsa_pipe, mem_ctx, &pol);
85 cli_rpc_pipe_close(lsa_pipe);
91 * Run a single RPC command, from start to finish.
93 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
94 * @param conn_flag a NET_FLAG_ combination. Passed to
95 * net_make_ipc_connection.
96 * @param argc Standard main() style argc
97 * @param argc Standard main() style argv. Initial components are already
99 * @return A shell status integer (0 for success)
102 int run_rpc_command(struct cli_state *cli_arg,
109 struct cli_state *cli = NULL;
110 struct rpc_pipe_client *pipe_hnd = NULL;
116 /* make use of cli_state handed over as an argument, if possible */
118 cli = net_make_ipc_connection(conn_flags);
129 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
130 DEBUG(0, ("talloc_init() failed\n"));
135 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
137 if (!NT_STATUS_IS_OK(nt_status)) {
142 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
143 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
144 /* Always try and create an schannel netlogon pipe. */
145 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
146 PIPE_AUTH_LEVEL_PRIVACY,
150 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
151 nt_errstr(nt_status) ));
156 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
158 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
159 cli_get_pipe_name(pipe_idx),
160 nt_errstr(nt_status) ));
167 nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
169 if (!NT_STATUS_IS_OK(nt_status)) {
170 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
172 DEBUG(5, ("rpc command function succedded\n"));
175 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
177 cli_rpc_pipe_close(pipe_hnd);
181 /* close the connection only if it was opened here */
186 talloc_destroy(mem_ctx);
187 return (!NT_STATUS_IS_OK(nt_status));
191 * Force a change of the trust acccount password.
193 * All parameters are provided by the run_rpc_command function, except for
194 * argc, argv which are passes through.
196 * @param domain_sid The domain sid aquired from the remote server
197 * @param cli A cli_state connected to the server.
198 * @param mem_ctx Talloc context, destoyed on compleation of the function.
199 * @param argc Standard main() style argc
200 * @param argc Standard main() style argv. Initial components are already
203 * @return Normal NTSTATUS return.
206 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
207 const char *domain_name,
208 struct cli_state *cli,
209 struct rpc_pipe_client *pipe_hnd,
215 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
219 * Force a change of the trust acccount password.
221 * @param argc Standard main() style argc
222 * @param argc Standard main() style argv. Initial components are already
225 * @return A shell status integer (0 for success)
228 int net_rpc_changetrustpw(int argc, const char **argv)
230 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
231 rpc_changetrustpw_internals,
236 * Join a domain, the old way.
238 * This uses 'machinename' as the inital password, and changes it.
240 * The password should be created with 'server manager' or equiv first.
242 * All parameters are provided by the run_rpc_command function, except for
243 * argc, argv which are passes through.
245 * @param domain_sid The domain sid aquired from the remote server
246 * @param cli A cli_state connected to the server.
247 * @param mem_ctx Talloc context, destoyed on compleation of the function.
248 * @param argc Standard main() style argc
249 * @param argc Standard main() style argv. Initial components are already
252 * @return Normal NTSTATUS return.
255 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
256 const char *domain_name,
257 struct cli_state *cli,
258 struct rpc_pipe_client *pipe_hnd,
264 fstring trust_passwd;
265 unsigned char orig_trust_passwd_hash[16];
267 uint32 sec_channel_type;
269 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
271 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
274 nt_errstr(result) ));
279 check what type of join - if the user want's to join as
280 a BDC, the server must agree that we are a BDC.
283 sec_channel_type = get_sec_channel_type(argv[0]);
285 sec_channel_type = get_sec_channel_type(NULL);
288 fstrcpy(trust_passwd, global_myname());
289 strlower_m(trust_passwd);
292 * Machine names can be 15 characters, but the max length on
293 * a password is 14. --jerry
296 trust_passwd[14] = '\0';
298 E_md4hash(trust_passwd, orig_trust_passwd_hash);
300 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
301 orig_trust_passwd_hash,
304 if (NT_STATUS_IS_OK(result))
305 printf("Joined domain %s.\n",opt_target_workgroup);
308 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
309 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
310 result = NT_STATUS_UNSUCCESSFUL;
317 * Join a domain, the old way.
319 * @param argc Standard main() style argc
320 * @param argc Standard main() style argv. Initial components are already
323 * @return A shell status integer (0 for success)
326 static int net_rpc_perform_oldjoin(int argc, const char **argv)
328 return run_rpc_command(NULL, PI_NETLOGON,
329 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
330 rpc_oldjoin_internals,
335 * Join a domain, the old way. This function exists to allow
336 * the message to be displayed when oldjoin was explicitly
337 * requested, but not when it was implied by "net rpc join"
339 * @param argc Standard main() style argc
340 * @param argc Standard main() style argv. Initial components are already
343 * @return A shell status integer (0 for success)
346 static int net_rpc_oldjoin(int argc, const char **argv)
348 int rc = net_rpc_perform_oldjoin(argc, argv);
351 d_fprintf(stderr, "Failed to join domain\n");
358 * Basic usage function for 'net rpc join'
359 * @param argc Standard main() style argc
360 * @param argc Standard main() style argv. Initial components are already
364 static int rpc_join_usage(int argc, const char **argv)
366 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
367 "\t to join a domain with admin username & password\n"\
368 "\t\t password will be prompted if needed and none is specified\n"\
369 "\t <type> can be (default MEMBER)\n"\
370 "\t\t BDC - Join as a BDC\n"\
371 "\t\t PDC - Join as a PDC\n"\
372 "\t\t MEMBER - Join as a MEMBER server\n");
374 net_common_flags_usage(argc, argv);
379 * 'net rpc join' entrypoint.
380 * @param argc Standard main() style argc
381 * @param argc Standard main() style argv. Initial components are already
384 * Main 'net_rpc_join()' (where the admain username/password is used) is
386 * Try to just change the password, but if that doesn't work, use/prompt
387 * for a username/password.
390 int net_rpc_join(int argc, const char **argv)
392 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
395 return net_rpc_join_newstyle(argc, argv);
399 * display info about a rpc domain
401 * All parameters are provided by the run_rpc_command function, except for
402 * argc, argv which are passed through.
404 * @param domain_sid The domain sid acquired from the remote server
405 * @param cli A cli_state connected to the server.
406 * @param mem_ctx Talloc context, destoyed on completion of the function.
407 * @param argc Standard main() style argc
408 * @param argv Standard main() style argv. Initial components are already
411 * @return Normal NTSTATUS return.
414 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
415 const char *domain_name,
416 struct cli_state *cli,
417 struct rpc_pipe_client *pipe_hnd,
422 POLICY_HND connect_pol, domain_pol;
423 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
427 sid_to_string(sid_str, domain_sid);
429 /* Get sam policy handle */
430 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
432 if (!NT_STATUS_IS_OK(result)) {
433 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
437 /* Get domain policy handle */
438 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
439 MAXIMUM_ALLOWED_ACCESS,
440 domain_sid, &domain_pol);
441 if (!NT_STATUS_IS_OK(result)) {
442 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
447 result = rpccli_samr_query_dom_info(pipe_hnd, mem_ctx, &domain_pol,
449 if (NT_STATUS_IS_OK(result)) {
450 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
451 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
452 d_printf("Domain SID: %s\n", sid_str);
453 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num.low);
454 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
455 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
456 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
465 * 'net rpc info' entrypoint.
466 * @param argc Standard main() style argc
467 * @param argc Standard main() style argv. Initial components are already
471 int net_rpc_info(int argc, const char **argv)
473 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC,
479 * Fetch domain SID into the local secrets.tdb
481 * All parameters are provided by the run_rpc_command function, except for
482 * argc, argv which are passes through.
484 * @param domain_sid The domain sid acquired from the remote server
485 * @param cli A cli_state connected to the server.
486 * @param mem_ctx Talloc context, destoyed on completion of the function.
487 * @param argc Standard main() style argc
488 * @param argv Standard main() style argv. Initial components are already
491 * @return Normal NTSTATUS return.
494 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
495 const char *domain_name,
496 struct cli_state *cli,
497 struct rpc_pipe_client *pipe_hnd,
504 sid_to_string(sid_str, domain_sid);
505 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
506 sid_str, domain_name);
508 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
509 DEBUG(0,("Can't store domain SID\n"));
510 return NT_STATUS_UNSUCCESSFUL;
517 * 'net rpc getsid' entrypoint.
518 * @param argc Standard main() style argc
519 * @param argc Standard main() style argv. Initial components are already
523 int net_rpc_getsid(int argc, const char **argv)
525 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
526 rpc_getsid_internals,
530 /****************************************************************************/
533 * Basic usage function for 'net rpc user'
534 * @param argc Standard main() style argc.
535 * @param argv Standard main() style argv. Initial components are already
539 static int rpc_user_usage(int argc, const char **argv)
541 return net_help_user(argc, argv);
545 * Add a new user to a remote RPC server
547 * All parameters are provided by the run_rpc_command function, except for
548 * argc, argv which are passes through.
550 * @param domain_sid The domain sid acquired from the remote server
551 * @param cli A cli_state connected to the server.
552 * @param mem_ctx Talloc context, destoyed on completion of the function.
553 * @param argc Standard main() style argc
554 * @param argv Standard main() style argv. Initial components are already
557 * @return Normal NTSTATUS return.
560 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
561 const char *domain_name,
562 struct cli_state *cli,
563 struct rpc_pipe_client *pipe_hnd,
565 int argc, const char **argv)
568 POLICY_HND connect_pol, domain_pol, user_pol;
569 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
570 const char *acct_name;
572 uint32 unknown, user_rid;
575 d_printf("User must be specified\n");
576 rpc_user_usage(argc, argv);
582 /* Get sam policy handle */
584 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
586 if (!NT_STATUS_IS_OK(result)) {
590 /* Get domain policy handle */
592 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
593 MAXIMUM_ALLOWED_ACCESS,
594 domain_sid, &domain_pol);
595 if (!NT_STATUS_IS_OK(result)) {
599 /* Create domain user */
601 acb_info = ACB_NORMAL;
602 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
604 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
605 acct_name, acb_info, unknown,
606 &user_pol, &user_rid);
607 if (!NT_STATUS_IS_OK(result)) {
612 if (!NT_STATUS_IS_OK(result)) {
613 d_fprintf(stderr, "Failed to add user %s - %s\n", acct_name,
616 d_printf("Added user %s\n", acct_name);
622 * Add a new user to a remote RPC server
624 * @param argc Standard main() style argc
625 * @param argv Standard main() style argv. Initial components are already
628 * @return A shell status integer (0 for success)
631 static int rpc_user_add(int argc, const char **argv)
633 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
638 * Delete a user from a remote RPC server
640 * All parameters are provided by the run_rpc_command function, except for
641 * argc, argv which are passes through.
643 * @param domain_sid The domain sid acquired from the remote server
644 * @param cli A cli_state connected to the server.
645 * @param mem_ctx Talloc context, destoyed on completion of the function.
646 * @param argc Standard main() style argc
647 * @param argv Standard main() style argv. Initial components are already
650 * @return Normal NTSTATUS return.
653 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
654 const char *domain_name,
655 struct cli_state *cli,
656 struct rpc_pipe_client *pipe_hnd,
661 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
662 POLICY_HND connect_pol, domain_pol, user_pol;
665 d_printf("User must be specified\n");
666 rpc_user_usage(argc, argv);
669 /* Get sam policy and domain handles */
671 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
674 if (!NT_STATUS_IS_OK(result)) {
678 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
679 MAXIMUM_ALLOWED_ACCESS,
680 domain_sid, &domain_pol);
682 if (!NT_STATUS_IS_OK(result)) {
686 /* Get handle on user */
689 uint32 *user_rids, num_rids, *name_types;
690 uint32 flags = 0x000003e8; /* Unknown */
692 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
694 &num_rids, &user_rids,
697 if (!NT_STATUS_IS_OK(result)) {
701 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
702 MAXIMUM_ALLOWED_ACCESS,
703 user_rids[0], &user_pol);
705 if (!NT_STATUS_IS_OK(result)) {
712 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
714 if (!NT_STATUS_IS_OK(result)) {
718 /* Display results */
719 if (!NT_STATUS_IS_OK(result)) {
720 d_fprintf(stderr, "Failed to delete user account - %s\n", nt_errstr(result));
722 d_printf("Deleted user account\n");
730 * Rename a user on a remote RPC server
732 * All parameters are provided by the run_rpc_command function, except for
733 * argc, argv which are passes through.
735 * @param domain_sid The domain sid acquired from the remote server
736 * @param cli A cli_state connected to the server.
737 * @param mem_ctx Talloc context, destoyed on completion of the function.
738 * @param argc Standard main() style argc
739 * @param argv Standard main() style argv. Initial components are already
742 * @return Normal NTSTATUS return.
745 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
746 const char *domain_name,
747 struct cli_state *cli,
748 struct rpc_pipe_client *pipe_hnd,
753 POLICY_HND connect_pol, domain_pol, user_pol;
754 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
755 uint32 info_level = 7;
756 const char *old_name, *new_name;
758 uint32 flags = 0x000003e8; /* Unknown */
759 uint32 num_rids, *name_types;
760 uint32 num_names = 1;
762 SAM_USERINFO_CTR *user_ctr;
763 SAM_USERINFO_CTR ctr;
764 SAM_USER_INFO_7 info7;
767 d_printf("Old and new username must be specified\n");
768 rpc_user_usage(argc, argv);
776 ZERO_STRUCT(user_ctr);
778 /* Get sam policy handle */
780 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
782 if (!NT_STATUS_IS_OK(result)) {
786 /* Get domain policy handle */
788 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
789 MAXIMUM_ALLOWED_ACCESS,
790 domain_sid, &domain_pol);
791 if (!NT_STATUS_IS_OK(result)) {
795 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
797 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
798 flags, num_names, names,
799 &num_rids, &user_rid, &name_types);
800 if (!NT_STATUS_IS_OK(result)) {
804 /* Open domain user */
805 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
806 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
808 if (!NT_STATUS_IS_OK(result)) {
812 /* Query user info */
813 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
814 info_level, &user_ctr);
816 if (!NT_STATUS_IS_OK(result)) {
820 ctr.switch_value = info_level;
821 ctr.info.id7 = &info7;
823 init_sam_user_info7(&info7, new_name);
826 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
827 info_level, &cli->user_session_key, &ctr);
829 if (!NT_STATUS_IS_OK(result)) {
834 if (!NT_STATUS_IS_OK(result)) {
835 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
838 d_printf("Renamed user from %s to %s\n", old_name, new_name);
844 * Rename a user on a remote RPC server
846 * @param argc Standard main() style argc
847 * @param argv Standard main() style argv. Initial components are already
850 * @return A shell status integer (0 for success)
853 static int rpc_user_rename(int argc, const char **argv)
855 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
860 * Delete a user from a remote RPC server
862 * @param argc Standard main() style argc
863 * @param argv Standard main() style argv. Initial components are already
866 * @return A shell status integer (0 for success)
869 static int rpc_user_delete(int argc, const char **argv)
871 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
876 * Set a password for a user on a remote RPC server
878 * All parameters are provided by the run_rpc_command function, except for
879 * argc, argv which are passes through.
881 * @param domain_sid The domain sid acquired from the remote server
882 * @param cli A cli_state connected to the server.
883 * @param mem_ctx Talloc context, destoyed on completion of the function.
884 * @param argc Standard main() style argc
885 * @param argv Standard main() style argv. Initial components are already
888 * @return Normal NTSTATUS return.
891 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
892 const char *domain_name,
893 struct cli_state *cli,
894 struct rpc_pipe_client *pipe_hnd,
899 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
900 POLICY_HND connect_pol, domain_pol, user_pol;
901 SAM_USERINFO_CTR ctr;
902 SAM_USER_INFO_24 p24;
905 const char *new_password;
909 d_printf("User must be specified\n");
910 rpc_user_usage(argc, argv);
917 new_password = argv[1];
919 asprintf(&prompt, "Enter new password for %s:", user);
920 new_password = getpass(prompt);
924 /* Get sam policy and domain handles */
926 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
929 if (!NT_STATUS_IS_OK(result)) {
933 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
934 MAXIMUM_ALLOWED_ACCESS,
935 domain_sid, &domain_pol);
937 if (!NT_STATUS_IS_OK(result)) {
941 /* Get handle on user */
944 uint32 *user_rids, num_rids, *name_types;
945 uint32 flags = 0x000003e8; /* Unknown */
947 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
949 &num_rids, &user_rids,
952 if (!NT_STATUS_IS_OK(result)) {
956 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
957 MAXIMUM_ALLOWED_ACCESS,
958 user_rids[0], &user_pol);
960 if (!NT_STATUS_IS_OK(result)) {
965 /* Set password on account */
970 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
972 init_sam_user_info24(&p24, (char *)pwbuf,24);
974 ctr.switch_value = 24;
975 ctr.info.id24 = &p24;
977 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
978 &cli->user_session_key, &ctr);
980 if (!NT_STATUS_IS_OK(result)) {
984 /* Display results */
992 * Set a user's password on a remote RPC server
994 * @param argc Standard main() style argc
995 * @param argv Standard main() style argv. Initial components are already
998 * @return A shell status integer (0 for success)
1001 static int rpc_user_password(int argc, const char **argv)
1003 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1008 * List user's groups on a remote RPC server
1010 * All parameters are provided by the run_rpc_command function, except for
1011 * argc, argv which are passes through.
1013 * @param domain_sid The domain sid acquired from the remote server
1014 * @param cli A cli_state connected to the server.
1015 * @param mem_ctx Talloc context, destoyed on completion of the function.
1016 * @param argc Standard main() style argc
1017 * @param argv Standard main() style argv. Initial components are already
1020 * @return Normal NTSTATUS return.
1023 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1024 const char *domain_name,
1025 struct cli_state *cli,
1026 struct rpc_pipe_client *pipe_hnd,
1027 TALLOC_CTX *mem_ctx,
1031 POLICY_HND connect_pol, domain_pol, user_pol;
1032 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1033 uint32 *rids, num_rids, *name_types, num_names;
1034 uint32 flags = 0x000003e8; /* Unknown */
1040 d_printf("User must be specified\n");
1041 rpc_user_usage(argc, argv);
1042 return NT_STATUS_OK;
1044 /* Get sam policy handle */
1046 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1048 if (!NT_STATUS_IS_OK(result)) goto done;
1050 /* Get domain policy handle */
1052 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1053 MAXIMUM_ALLOWED_ACCESS,
1054 domain_sid, &domain_pol);
1055 if (!NT_STATUS_IS_OK(result)) goto done;
1057 /* Get handle on user */
1059 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1061 &num_rids, &rids, &name_types);
1063 if (!NT_STATUS_IS_OK(result)) goto done;
1065 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1066 MAXIMUM_ALLOWED_ACCESS,
1067 rids[0], &user_pol);
1068 if (!NT_STATUS_IS_OK(result)) goto done;
1070 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1071 &num_rids, &user_gids);
1073 if (!NT_STATUS_IS_OK(result)) goto done;
1078 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1080 for (i = 0; i < num_rids; i++)
1081 rids[i] = user_gids[i].g_rid;
1083 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1085 &num_names, &names, &name_types);
1087 if (!NT_STATUS_IS_OK(result)) {
1091 /* Display results */
1093 for (i = 0; i < num_names; i++)
1094 printf("%s\n", names[i]);
1101 * List a user's groups from a remote RPC server
1103 * @param argc Standard main() style argc
1104 * @param argv Standard main() style argv. Initial components are already
1107 * @return A shell status integer (0 for success)
1110 static int rpc_user_info(int argc, const char **argv)
1112 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1117 * List users on a remote RPC server
1119 * All parameters are provided by the run_rpc_command function, except for
1120 * argc, argv which are passes through.
1122 * @param domain_sid The domain sid acquired from the remote server
1123 * @param cli A cli_state connected to the server.
1124 * @param mem_ctx Talloc context, destoyed on completion of the function.
1125 * @param argc Standard main() style argc
1126 * @param argv Standard main() style argv. Initial components are already
1129 * @return Normal NTSTATUS return.
1132 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1133 const char *domain_name,
1134 struct cli_state *cli,
1135 struct rpc_pipe_client *pipe_hnd,
1136 TALLOC_CTX *mem_ctx,
1140 POLICY_HND connect_pol, domain_pol;
1141 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1142 uint32 start_idx=0, num_entries, i, loop_count = 0;
1143 SAM_DISPINFO_CTR ctr;
1144 SAM_DISPINFO_1 info1;
1146 /* Get sam policy handle */
1148 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1150 if (!NT_STATUS_IS_OK(result)) {
1154 /* Get domain policy handle */
1156 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1157 MAXIMUM_ALLOWED_ACCESS,
1158 domain_sid, &domain_pol);
1159 if (!NT_STATUS_IS_OK(result)) {
1163 /* Query domain users */
1166 ctr.sam.info1 = &info1;
1167 if (opt_long_list_entries)
1168 d_printf("\nUser name Comment"\
1169 "\n-----------------------------\n");
1172 uint32 max_entries, max_size;
1174 get_query_dispinfo_params(
1175 loop_count, &max_entries, &max_size);
1177 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1178 &start_idx, 1, &num_entries,
1179 max_entries, max_size, &ctr);
1182 for (i = 0; i < num_entries; i++) {
1183 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1184 if (opt_long_list_entries)
1185 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1187 if (opt_long_list_entries)
1188 printf("%-21.21s %s\n", user, desc);
1190 printf("%s\n", user);
1192 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1199 * 'net rpc user' entrypoint.
1200 * @param argc Standard main() style argc
1201 * @param argc Standard main() style argv. Initial components are already
1205 int net_rpc_user(int argc, const char **argv)
1207 struct functable func[] = {
1208 {"add", rpc_user_add},
1209 {"info", rpc_user_info},
1210 {"delete", rpc_user_delete},
1211 {"password", rpc_user_password},
1212 {"rename", rpc_user_rename},
1217 return run_rpc_command(NULL,PI_SAMR, 0,
1218 rpc_user_list_internals,
1222 return net_run_function(argc, argv, func, rpc_user_usage);
1225 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1226 struct rpc_sh_ctx *ctx,
1227 struct rpc_pipe_client *pipe_hnd,
1228 int argc, const char **argv)
1230 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1231 ctx->cli, pipe_hnd, mem_ctx,
1235 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1236 struct rpc_sh_ctx *ctx,
1237 struct rpc_pipe_client *pipe_hnd,
1238 int argc, const char **argv)
1240 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1241 ctx->cli, pipe_hnd, mem_ctx,
1245 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1246 struct rpc_sh_ctx *ctx,
1247 struct rpc_pipe_client *pipe_hnd,
1248 int argc, const char **argv,
1250 TALLOC_CTX *mem_ctx,
1251 struct rpc_sh_ctx *ctx,
1252 struct rpc_pipe_client *pipe_hnd,
1253 const POLICY_HND *user_hnd,
1254 int argc, const char **argv))
1257 POLICY_HND connect_pol, domain_pol, user_pol;
1258 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1261 enum SID_NAME_USE type;
1264 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1265 return NT_STATUS_INVALID_PARAMETER;
1268 ZERO_STRUCT(connect_pol);
1269 ZERO_STRUCT(domain_pol);
1270 ZERO_STRUCT(user_pol);
1272 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1273 NULL, NULL, &sid, &type);
1274 if (!NT_STATUS_IS_OK(result)) {
1275 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1280 if (type != SID_NAME_USER) {
1281 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1282 sid_type_lookup(type));
1283 result = NT_STATUS_NO_SUCH_USER;
1287 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1288 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1289 result = NT_STATUS_NO_SUCH_USER;
1293 result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1294 MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1295 if (!NT_STATUS_IS_OK(result)) {
1299 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1300 MAXIMUM_ALLOWED_ACCESS,
1301 ctx->domain_sid, &domain_pol);
1302 if (!NT_STATUS_IS_OK(result)) {
1306 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1307 MAXIMUM_ALLOWED_ACCESS,
1309 if (!NT_STATUS_IS_OK(result)) {
1313 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1316 if (is_valid_policy_hnd(&user_pol)) {
1317 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1319 if (is_valid_policy_hnd(&domain_pol)) {
1320 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1322 if (is_valid_policy_hnd(&connect_pol)) {
1323 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1328 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1329 struct rpc_sh_ctx *ctx,
1330 struct rpc_pipe_client *pipe_hnd,
1331 const POLICY_HND *user_hnd,
1332 int argc, const char **argv)
1335 SAM_USERINFO_CTR *ctr;
1336 SAM_USER_INFO_21 *info;
1339 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1340 return NT_STATUS_INVALID_PARAMETER;
1343 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1345 if (!NT_STATUS_IS_OK(result)) {
1349 info = ctr->info.id21;
1351 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1357 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1358 struct rpc_sh_ctx *ctx,
1359 struct rpc_pipe_client *pipe_hnd,
1360 int argc, const char **argv)
1362 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1363 rpc_sh_user_show_internals);
1366 #define FETCHSTR(name, rec) \
1367 do { if (strequal(ctx->thiscmd, name)) { \
1368 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1371 #define SETSTR(name, rec, flag) \
1372 do { if (strequal(ctx->thiscmd, name)) { \
1373 init_unistr2(&usr->uni_##rec, argv[0], STR_TERMINATE); \
1374 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1375 usr->fields_present |= ACCT_##flag; } \
1378 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1379 struct rpc_sh_ctx *ctx,
1380 struct rpc_pipe_client *pipe_hnd,
1381 const POLICY_HND *user_hnd,
1382 int argc, const char **argv)
1385 SAM_USERINFO_CTR *ctr;
1386 SAM_USER_INFO_21 *usr;
1387 const char *username;
1388 const char *oldval = "";
1391 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1393 return NT_STATUS_INVALID_PARAMETER;
1396 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1398 if (!NT_STATUS_IS_OK(result)) {
1402 usr = ctr->info.id21;
1404 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1406 FETCHSTR("fullname", full_name);
1407 FETCHSTR("homedir", home_dir);
1408 FETCHSTR("homedrive", dir_drive);
1409 FETCHSTR("logonscript", logon_script);
1410 FETCHSTR("profilepath", profile_path);
1411 FETCHSTR("description", acct_desc);
1414 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1420 if (strcmp(argv[0], "NULL") == 0) {
1424 SETSTR("fullname", full_name, FULL_NAME);
1425 SETSTR("homedir", home_dir, HOME_DIR);
1426 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1427 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1428 SETSTR("profilepath", profile_path, PROFILE);
1429 SETSTR("description", acct_desc, DESCRIPTION);
1431 result = rpccli_samr_set_userinfo2(
1432 pipe_hnd, mem_ctx, user_hnd, 21,
1433 &pipe_hnd->cli->user_session_key, ctr);
1435 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1436 ctx->thiscmd, oldval, argv[0]);
1443 #define HANDLEFLG(name, rec) \
1444 do { if (strequal(ctx->thiscmd, name)) { \
1445 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1447 newflags = oldflags | ACB_##rec; \
1449 newflags = oldflags & ~ACB_##rec; \
1452 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1453 struct rpc_sh_ctx *ctx,
1454 struct rpc_pipe_client *pipe_hnd,
1455 int argc, const char **argv)
1457 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1458 rpc_sh_user_str_edit_internals);
1461 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1462 struct rpc_sh_ctx *ctx,
1463 struct rpc_pipe_client *pipe_hnd,
1464 const POLICY_HND *user_hnd,
1465 int argc, const char **argv)
1468 SAM_USERINFO_CTR *ctr;
1469 SAM_USER_INFO_21 *usr;
1470 const char *username;
1471 const char *oldval = "unknown";
1472 uint32 oldflags, newflags;
1476 ((argc == 1) && !strequal(argv[0], "yes") &&
1477 !strequal(argv[0], "no"))) {
1478 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1480 return NT_STATUS_INVALID_PARAMETER;
1483 newval = strequal(argv[0], "yes");
1485 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1487 if (!NT_STATUS_IS_OK(result)) {
1491 usr = ctr->info.id21;
1493 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1494 oldflags = usr->acb_info;
1495 newflags = usr->acb_info;
1497 HANDLEFLG("disabled", DISABLED);
1498 HANDLEFLG("pwnotreq", PWNOTREQ);
1499 HANDLEFLG("autolock", AUTOLOCK);
1500 HANDLEFLG("pwnoexp", PWNOEXP);
1503 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1509 usr->acb_info = newflags;
1510 usr->fields_present = ACCT_FLAGS;
1512 result = rpccli_samr_set_userinfo2(
1513 pipe_hnd, mem_ctx, user_hnd, 21,
1514 &pipe_hnd->cli->user_session_key, ctr);
1516 if (NT_STATUS_IS_OK(result)) {
1517 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1518 ctx->thiscmd, oldval, argv[0]);
1526 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1527 struct rpc_sh_ctx *ctx,
1528 struct rpc_pipe_client *pipe_hnd,
1529 int argc, const char **argv)
1531 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1532 rpc_sh_user_flag_edit_internals);
1535 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1536 struct rpc_sh_ctx *ctx)
1538 static struct rpc_sh_cmd cmds[] = {
1540 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1541 "Show/Set a user's full name" },
1543 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1544 "Show/Set a user's home directory" },
1546 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1547 "Show/Set a user's home drive" },
1549 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1550 "Show/Set a user's logon script" },
1552 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1553 "Show/Set a user's profile path" },
1555 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1556 "Show/Set a user's description" },
1558 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1559 "Show/Set whether a user is disabled" },
1561 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1562 "Show/Set whether a user locked out" },
1564 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1565 "Show/Set whether a user does not need a password" },
1567 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1568 "Show/Set whether a user's password does not expire" },
1570 { NULL, NULL, 0, NULL, NULL }
1576 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1577 struct rpc_sh_ctx *ctx)
1579 static struct rpc_sh_cmd cmds[] = {
1581 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1582 "List available users" },
1584 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1585 "List the domain groups a user is member of" },
1587 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1588 "Show info about a user" },
1590 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1591 "Show/Modify a user's fields" },
1593 { NULL, NULL, 0, NULL, NULL }
1599 /****************************************************************************/
1602 * Basic usage function for 'net rpc group'
1603 * @param argc Standard main() style argc.
1604 * @param argv Standard main() style argv. Initial components are already
1608 static int rpc_group_usage(int argc, const char **argv)
1610 return net_help_group(argc, argv);
1614 * Delete group on a remote RPC server
1616 * All parameters are provided by the run_rpc_command function, except for
1617 * argc, argv which are passes through.
1619 * @param domain_sid The domain sid acquired from the remote server
1620 * @param cli A cli_state connected to the server.
1621 * @param mem_ctx Talloc context, destoyed on completion of the function.
1622 * @param argc Standard main() style argc
1623 * @param argv Standard main() style argv. Initial components are already
1626 * @return Normal NTSTATUS return.
1629 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1630 const char *domain_name,
1631 struct cli_state *cli,
1632 struct rpc_pipe_client *pipe_hnd,
1633 TALLOC_CTX *mem_ctx,
1637 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1638 BOOL group_is_primary = False;
1639 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1641 uint32 *group_rids, num_rids, *name_types, num_members,
1642 *group_attrs, group_rid;
1643 uint32 flags = 0x000003e8; /* Unknown */
1646 /* DOM_GID *user_gids; */
1647 SAM_USERINFO_CTR *user_ctr;
1651 d_printf("specify group\n");
1652 rpc_group_usage(argc,argv);
1653 return NT_STATUS_OK; /* ok? */
1656 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1659 if (!NT_STATUS_IS_OK(result)) {
1660 d_fprintf(stderr, "Request samr_connect failed\n");
1664 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1665 MAXIMUM_ALLOWED_ACCESS,
1666 domain_sid, &domain_pol);
1668 if (!NT_STATUS_IS_OK(result)) {
1669 d_fprintf(stderr, "Request open_domain failed\n");
1673 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1675 &num_rids, &group_rids,
1678 if (!NT_STATUS_IS_OK(result)) {
1679 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1683 switch (name_types[0])
1685 case SID_NAME_DOM_GRP:
1686 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1687 MAXIMUM_ALLOWED_ACCESS,
1688 group_rids[0], &group_pol);
1689 if (!NT_STATUS_IS_OK(result)) {
1690 d_fprintf(stderr, "Request open_group failed");
1694 group_rid = group_rids[0];
1696 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1697 &num_members, &group_rids,
1700 if (!NT_STATUS_IS_OK(result)) {
1701 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1706 d_printf("Domain Group %s (rid: %d) has %d members\n",
1707 argv[0],group_rid,num_members);
1710 /* Check if group is anyone's primary group */
1711 for (i = 0; i < num_members; i++)
1713 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1714 MAXIMUM_ALLOWED_ACCESS,
1715 group_rids[i], &user_pol);
1717 if (!NT_STATUS_IS_OK(result)) {
1718 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1722 ZERO_STRUCT(user_ctr);
1724 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1727 if (!NT_STATUS_IS_OK(result)) {
1728 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1732 if (user_ctr->info.id21->group_rid == group_rid) {
1733 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1736 d_printf("Group is primary group of %s\n",temp);
1737 group_is_primary = True;
1740 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1743 if (group_is_primary) {
1744 d_fprintf(stderr, "Unable to delete group because some "
1745 "of it's members have it as primary group\n");
1746 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1750 /* remove all group members */
1751 for (i = 0; i < num_members; i++)
1754 d_printf("Remove group member %d...",group_rids[i]);
1755 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1757 if (NT_STATUS_IS_OK(result)) {
1762 d_printf("failed\n");
1767 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1770 /* removing a local group is easier... */
1771 case SID_NAME_ALIAS:
1772 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1773 MAXIMUM_ALLOWED_ACCESS,
1774 group_rids[0], &group_pol);
1776 if (!NT_STATUS_IS_OK(result)) {
1777 d_fprintf(stderr, "Request open_alias failed\n");
1781 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1784 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1785 argv[0],sid_type_lookup(name_types[0]));
1786 result = NT_STATUS_UNSUCCESSFUL;
1791 if (NT_STATUS_IS_OK(result)) {
1793 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1795 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1796 get_friendly_nt_error_msg(result));
1804 static int rpc_group_delete(int argc, const char **argv)
1806 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1810 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1811 const char *domain_name,
1812 struct cli_state *cli,
1813 struct rpc_pipe_client *pipe_hnd,
1814 TALLOC_CTX *mem_ctx,
1818 POLICY_HND connect_pol, domain_pol, group_pol;
1819 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1820 GROUP_INFO_CTR group_info;
1823 d_printf("Group name must be specified\n");
1824 rpc_group_usage(argc, argv);
1825 return NT_STATUS_OK;
1828 /* Get sam policy handle */
1830 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1832 if (!NT_STATUS_IS_OK(result)) goto done;
1834 /* Get domain policy handle */
1836 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1837 MAXIMUM_ALLOWED_ACCESS,
1838 domain_sid, &domain_pol);
1839 if (!NT_STATUS_IS_OK(result)) goto done;
1841 /* Create the group */
1843 result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1844 argv[0], MAXIMUM_ALLOWED_ACCESS,
1846 if (!NT_STATUS_IS_OK(result)) goto done;
1848 if (strlen(opt_comment) == 0) goto done;
1850 /* We've got a comment to set */
1852 group_info.switch_value1 = 4;
1853 init_samr_group_info4(&group_info.group.info4, opt_comment);
1855 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1856 if (!NT_STATUS_IS_OK(result)) goto done;
1859 if (NT_STATUS_IS_OK(result))
1860 DEBUG(5, ("add group succeeded\n"));
1862 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1867 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1868 const char *domain_name,
1869 struct cli_state *cli,
1870 struct rpc_pipe_client *pipe_hnd,
1871 TALLOC_CTX *mem_ctx,
1875 POLICY_HND connect_pol, domain_pol, alias_pol;
1876 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1877 ALIAS_INFO_CTR alias_info;
1880 d_printf("Alias name must be specified\n");
1881 rpc_group_usage(argc, argv);
1882 return NT_STATUS_OK;
1885 /* Get sam policy handle */
1887 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1889 if (!NT_STATUS_IS_OK(result)) goto done;
1891 /* Get domain policy handle */
1893 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1894 MAXIMUM_ALLOWED_ACCESS,
1895 domain_sid, &domain_pol);
1896 if (!NT_STATUS_IS_OK(result)) goto done;
1898 /* Create the group */
1900 result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1901 argv[0], &alias_pol);
1902 if (!NT_STATUS_IS_OK(result)) goto done;
1904 if (strlen(opt_comment) == 0) goto done;
1906 /* We've got a comment to set */
1908 alias_info.level = 3;
1909 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1911 result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1912 if (!NT_STATUS_IS_OK(result)) goto done;
1915 if (NT_STATUS_IS_OK(result))
1916 DEBUG(5, ("add alias succeeded\n"));
1918 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1923 static int rpc_group_add(int argc, const char **argv)
1926 return run_rpc_command(NULL, PI_SAMR, 0,
1927 rpc_alias_add_internals,
1930 return run_rpc_command(NULL, PI_SAMR, 0,
1931 rpc_group_add_internals,
1935 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1936 TALLOC_CTX *mem_ctx,
1939 enum SID_NAME_USE *type)
1941 DOM_SID *sids = NULL;
1942 uint32 *types = NULL;
1943 struct rpc_pipe_client *pipe_hnd;
1945 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1947 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1952 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
1953 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1955 if (!NT_STATUS_IS_OK(result)) {
1959 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1960 &name, NULL, &sids, &types);
1962 if (NT_STATUS_IS_OK(result)) {
1963 sid_copy(sid, &sids[0]);
1967 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
1971 cli_rpc_pipe_close(pipe_hnd);
1974 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1976 /* Try as S-1-5-whatever */
1980 if (string_to_sid(&tmp_sid, name)) {
1981 sid_copy(sid, &tmp_sid);
1982 *type = SID_NAME_UNKNOWN;
1983 result = NT_STATUS_OK;
1990 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1991 TALLOC_CTX *mem_ctx,
1992 const DOM_SID *group_sid,
1995 POLICY_HND connect_pol, domain_pol;
1998 POLICY_HND group_pol;
2001 uint32 *rids = NULL;
2002 uint32 *rid_types = NULL;
2006 sid_copy(&sid, group_sid);
2008 if (!sid_split_rid(&sid, &group_rid)) {
2009 return NT_STATUS_UNSUCCESSFUL;
2012 /* Get sam policy handle */
2013 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2015 if (!NT_STATUS_IS_OK(result)) {
2019 /* Get domain policy handle */
2020 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2021 MAXIMUM_ALLOWED_ACCESS,
2023 if (!NT_STATUS_IS_OK(result)) {
2027 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2029 &num_rids, &rids, &rid_types);
2031 if (!NT_STATUS_IS_OK(result)) {
2032 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2036 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2037 MAXIMUM_ALLOWED_ACCESS,
2038 group_rid, &group_pol);
2040 if (!NT_STATUS_IS_OK(result)) {
2044 result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2047 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2051 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2052 TALLOC_CTX *mem_ctx,
2053 const DOM_SID *alias_sid,
2056 POLICY_HND connect_pol, domain_pol;
2059 POLICY_HND alias_pol;
2062 enum SID_NAME_USE member_type;
2066 sid_copy(&sid, alias_sid);
2068 if (!sid_split_rid(&sid, &alias_rid)) {
2069 return NT_STATUS_UNSUCCESSFUL;
2072 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2073 &member_sid, &member_type);
2075 if (!NT_STATUS_IS_OK(result)) {
2076 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2080 /* Get sam policy handle */
2081 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2083 if (!NT_STATUS_IS_OK(result)) {
2087 /* Get domain policy handle */
2088 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2089 MAXIMUM_ALLOWED_ACCESS,
2091 if (!NT_STATUS_IS_OK(result)) {
2095 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2096 MAXIMUM_ALLOWED_ACCESS,
2097 alias_rid, &alias_pol);
2099 if (!NT_STATUS_IS_OK(result)) {
2103 result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2105 if (!NT_STATUS_IS_OK(result)) {
2110 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2114 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2115 const char *domain_name,
2116 struct cli_state *cli,
2117 struct rpc_pipe_client *pipe_hnd,
2118 TALLOC_CTX *mem_ctx,
2123 enum SID_NAME_USE group_type;
2126 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2127 return NT_STATUS_UNSUCCESSFUL;
2130 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2131 &group_sid, &group_type))) {
2132 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2133 return NT_STATUS_UNSUCCESSFUL;
2136 if (group_type == SID_NAME_DOM_GRP) {
2137 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2138 &group_sid, argv[1]);
2140 if (!NT_STATUS_IS_OK(result)) {
2141 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2142 argv[1], argv[0], nt_errstr(result));
2147 if (group_type == SID_NAME_ALIAS) {
2148 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2149 &group_sid, argv[1]);
2151 if (!NT_STATUS_IS_OK(result)) {
2152 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2153 argv[1], argv[0], nt_errstr(result));
2158 d_fprintf(stderr, "Can only add members to global or local groups "
2159 "which %s is not\n", argv[0]);
2161 return NT_STATUS_UNSUCCESSFUL;
2164 static int rpc_group_addmem(int argc, const char **argv)
2166 return run_rpc_command(NULL, PI_SAMR, 0,
2167 rpc_group_addmem_internals,
2171 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2172 TALLOC_CTX *mem_ctx,
2173 const DOM_SID *group_sid,
2176 POLICY_HND connect_pol, domain_pol;
2179 POLICY_HND group_pol;
2182 uint32 *rids = NULL;
2183 uint32 *rid_types = NULL;
2187 sid_copy(&sid, group_sid);
2189 if (!sid_split_rid(&sid, &group_rid))
2190 return NT_STATUS_UNSUCCESSFUL;
2192 /* Get sam policy handle */
2193 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2195 if (!NT_STATUS_IS_OK(result))
2198 /* Get domain policy handle */
2199 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2200 MAXIMUM_ALLOWED_ACCESS,
2202 if (!NT_STATUS_IS_OK(result))
2205 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2207 &num_rids, &rids, &rid_types);
2209 if (!NT_STATUS_IS_OK(result)) {
2210 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2214 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2215 MAXIMUM_ALLOWED_ACCESS,
2216 group_rid, &group_pol);
2218 if (!NT_STATUS_IS_OK(result))
2221 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2224 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2228 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2229 TALLOC_CTX *mem_ctx,
2230 const DOM_SID *alias_sid,
2233 POLICY_HND connect_pol, domain_pol;
2236 POLICY_HND alias_pol;
2239 enum SID_NAME_USE member_type;
2243 sid_copy(&sid, alias_sid);
2245 if (!sid_split_rid(&sid, &alias_rid))
2246 return NT_STATUS_UNSUCCESSFUL;
2248 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2249 &member_sid, &member_type);
2251 if (!NT_STATUS_IS_OK(result)) {
2252 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2256 /* Get sam policy handle */
2257 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2259 if (!NT_STATUS_IS_OK(result)) {
2263 /* Get domain policy handle */
2264 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2265 MAXIMUM_ALLOWED_ACCESS,
2267 if (!NT_STATUS_IS_OK(result)) {
2271 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2272 MAXIMUM_ALLOWED_ACCESS,
2273 alias_rid, &alias_pol);
2275 if (!NT_STATUS_IS_OK(result))
2278 result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2280 if (!NT_STATUS_IS_OK(result))
2284 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2288 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2289 const char *domain_name,
2290 struct cli_state *cli,
2291 struct rpc_pipe_client *pipe_hnd,
2292 TALLOC_CTX *mem_ctx,
2297 enum SID_NAME_USE group_type;
2300 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2301 return NT_STATUS_UNSUCCESSFUL;
2304 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2305 &group_sid, &group_type))) {
2306 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2307 return NT_STATUS_UNSUCCESSFUL;
2310 if (group_type == SID_NAME_DOM_GRP) {
2311 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2312 &group_sid, argv[1]);
2314 if (!NT_STATUS_IS_OK(result)) {
2315 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2316 argv[1], argv[0], nt_errstr(result));
2321 if (group_type == SID_NAME_ALIAS) {
2322 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2323 &group_sid, argv[1]);
2325 if (!NT_STATUS_IS_OK(result)) {
2326 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2327 argv[1], argv[0], nt_errstr(result));
2332 d_fprintf(stderr, "Can only delete members from global or local groups "
2333 "which %s is not\n", argv[0]);
2335 return NT_STATUS_UNSUCCESSFUL;
2338 static int rpc_group_delmem(int argc, const char **argv)
2340 return run_rpc_command(NULL, PI_SAMR, 0,
2341 rpc_group_delmem_internals,
2346 * List groups on a remote RPC server
2348 * All parameters are provided by the run_rpc_command function, except for
2349 * argc, argv which are passes through.
2351 * @param domain_sid The domain sid acquired from the remote server
2352 * @param cli A cli_state connected to the server.
2353 * @param mem_ctx Talloc context, destoyed on completion of the function.
2354 * @param argc Standard main() style argc
2355 * @param argv Standard main() style argv. Initial components are already
2358 * @return Normal NTSTATUS return.
2361 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2362 const char *domain_name,
2363 struct cli_state *cli,
2364 struct rpc_pipe_client *pipe_hnd,
2365 TALLOC_CTX *mem_ctx,
2369 POLICY_HND connect_pol, domain_pol;
2370 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2371 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2372 struct acct_info *groups;
2373 BOOL global = False;
2375 BOOL builtin = False;
2383 for (i=0; i<argc; i++) {
2384 if (strequal(argv[i], "global"))
2387 if (strequal(argv[i], "local"))
2390 if (strequal(argv[i], "builtin"))
2394 /* Get sam policy handle */
2396 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2398 if (!NT_STATUS_IS_OK(result)) {
2402 /* Get domain policy handle */
2404 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2405 MAXIMUM_ALLOWED_ACCESS,
2406 domain_sid, &domain_pol);
2407 if (!NT_STATUS_IS_OK(result)) {
2411 /* Query domain groups */
2412 if (opt_long_list_entries)
2413 d_printf("\nGroup name Comment"\
2414 "\n-----------------------------\n");
2416 SAM_DISPINFO_CTR ctr;
2417 SAM_DISPINFO_3 info3;
2422 ctr.sam.info3 = &info3;
2426 get_query_dispinfo_params(
2427 loop_count, &max_entries, &max_size);
2429 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2430 &start_idx, 3, &num_entries,
2431 max_entries, max_size, &ctr);
2433 if (!NT_STATUS_IS_OK(result) &&
2434 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2437 for (i = 0; i < num_entries; i++) {
2439 fstring group, desc;
2441 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2442 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2444 if (opt_long_list_entries)
2445 printf("%-21.21s %-50.50s\n",
2448 printf("%s\n", group);
2450 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2451 /* query domain aliases */
2456 /* The max_size field in cli_samr_enum_als_groups is more like
2457 * an account_control field with indiviual bits what to
2458 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2459 * everything. I'm too lazy (sorry) to get this through to
2460 * rpc_parse/ etc. Volker */
2462 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2464 &groups, &num_entries);
2466 if (!NT_STATUS_IS_OK(result) &&
2467 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2470 for (i = 0; i < num_entries; i++) {
2472 char *description = NULL;
2474 if (opt_long_list_entries) {
2476 POLICY_HND alias_pol;
2479 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2484 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2487 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2489 description = unistr2_tdup(mem_ctx,
2490 ctr.alias.info3.description.string);
2494 if (description != NULL) {
2495 printf("%-21.21s %-50.50s\n",
2496 groups[i].acct_name,
2499 printf("%s\n", groups[i].acct_name);
2502 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2503 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2504 /* Get builtin policy handle */
2506 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2507 MAXIMUM_ALLOWED_ACCESS,
2508 &global_sid_Builtin, &domain_pol);
2509 if (!NT_STATUS_IS_OK(result)) {
2512 /* query builtin aliases */
2515 if (!builtin) break;
2517 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2518 &start_idx, max_entries,
2519 &groups, &num_entries);
2521 if (!NT_STATUS_IS_OK(result) &&
2522 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2525 for (i = 0; i < num_entries; i++) {
2527 char *description = NULL;
2529 if (opt_long_list_entries) {
2531 POLICY_HND alias_pol;
2534 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2539 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2542 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2544 description = unistr2_tdup(mem_ctx,
2545 ctr.alias.info3.description.string);
2549 if (description != NULL) {
2550 printf("%-21.21s %-50.50s\n",
2551 groups[i].acct_name,
2554 printf("%s\n", groups[i].acct_name);
2557 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2563 static int rpc_group_list(int argc, const char **argv)
2565 return run_rpc_command(NULL, PI_SAMR, 0,
2566 rpc_group_list_internals,
2570 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2571 TALLOC_CTX *mem_ctx,
2572 const char *domain_name,
2573 const DOM_SID *domain_sid,
2574 POLICY_HND *domain_pol,
2578 POLICY_HND group_pol;
2579 uint32 num_members, *group_rids, *group_attrs;
2586 sid_to_string(sid_str, domain_sid);
2588 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2589 MAXIMUM_ALLOWED_ACCESS,
2592 if (!NT_STATUS_IS_OK(result))
2595 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2596 &num_members, &group_rids,
2599 if (!NT_STATUS_IS_OK(result))
2602 while (num_members > 0) {
2603 int this_time = 512;
2605 if (num_members < this_time)
2606 this_time = num_members;
2608 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2609 this_time, group_rids,
2610 &num_names, &names, &name_types);
2612 if (!NT_STATUS_IS_OK(result))
2615 /* We only have users as members, but make the output
2616 the same as the output of alias members */
2618 for (i = 0; i < this_time; i++) {
2620 if (opt_long_list_entries) {
2621 printf("%s-%d %s\\%s %d\n", sid_str,
2622 group_rids[i], domain_name, names[i],
2625 printf("%s\\%s\n", domain_name, names[i]);
2629 num_members -= this_time;
2633 return NT_STATUS_OK;
2636 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2637 TALLOC_CTX *mem_ctx,
2638 POLICY_HND *domain_pol,
2642 struct rpc_pipe_client *lsa_pipe;
2643 POLICY_HND alias_pol, lsa_pol;
2645 DOM_SID *alias_sids;
2651 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2652 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2654 if (!NT_STATUS_IS_OK(result))
2657 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2658 &num_members, &alias_sids);
2660 if (!NT_STATUS_IS_OK(result)) {
2661 d_fprintf(stderr, "Couldn't list alias members\n");
2665 if (num_members == 0) {
2666 return NT_STATUS_OK;
2669 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2671 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2672 nt_errstr(result) );
2676 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2677 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2679 if (!NT_STATUS_IS_OK(result)) {
2680 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2681 cli_rpc_pipe_close(lsa_pipe);
2685 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2687 &domains, &names, &types);
2689 if (!NT_STATUS_IS_OK(result) &&
2690 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2691 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2692 cli_rpc_pipe_close(lsa_pipe);
2696 for (i = 0; i < num_members; i++) {
2698 sid_to_string(sid_str, &alias_sids[i]);
2700 if (opt_long_list_entries) {
2701 printf("%s %s\\%s %d\n", sid_str,
2702 domains[i] ? domains[i] : "*unknown*",
2703 names[i] ? names[i] : "*unknown*", types[i]);
2706 printf("%s\\%s\n", domains[i], names[i]);
2708 printf("%s\n", sid_str);
2712 cli_rpc_pipe_close(lsa_pipe);
2713 return NT_STATUS_OK;
2716 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2717 const char *domain_name,
2718 struct cli_state *cli,
2719 struct rpc_pipe_client *pipe_hnd,
2720 TALLOC_CTX *mem_ctx,
2725 POLICY_HND connect_pol, domain_pol;
2726 uint32 num_rids, *rids, *rid_types;
2728 /* Get sam policy handle */
2730 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2733 if (!NT_STATUS_IS_OK(result))
2736 /* Get domain policy handle */
2738 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2739 MAXIMUM_ALLOWED_ACCESS,
2740 domain_sid, &domain_pol);
2742 if (!NT_STATUS_IS_OK(result))
2745 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2746 1, argv, &num_rids, &rids, &rid_types);
2748 if (!NT_STATUS_IS_OK(result)) {
2750 /* Ok, did not find it in the global sam, try with builtin */
2752 DOM_SID sid_Builtin;
2754 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2756 string_to_sid(&sid_Builtin, "S-1-5-32");
2758 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2759 MAXIMUM_ALLOWED_ACCESS,
2760 &sid_Builtin, &domain_pol);
2762 if (!NT_STATUS_IS_OK(result)) {
2763 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2767 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2771 if (!NT_STATUS_IS_OK(result)) {
2772 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2777 if (num_rids != 1) {
2778 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2782 if (rid_types[0] == SID_NAME_DOM_GRP) {
2783 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2784 domain_sid, &domain_pol,
2788 if (rid_types[0] == SID_NAME_ALIAS) {
2789 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2793 return NT_STATUS_NO_SUCH_GROUP;
2796 static int rpc_group_members(int argc, const char **argv)
2799 return rpc_group_usage(argc, argv);
2802 return run_rpc_command(NULL, PI_SAMR, 0,
2803 rpc_group_members_internals,
2807 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2808 const char *domain_name,
2809 struct cli_state *cli,
2810 struct rpc_pipe_client *pipe_hnd,
2811 TALLOC_CTX *mem_ctx,
2816 POLICY_HND connect_pol, domain_pol, group_pol;
2817 uint32 num_rids, *rids, *rid_types;
2821 d_printf("Usage: 'net rpc group rename group newname'\n");
2822 return NT_STATUS_UNSUCCESSFUL;
2825 /* Get sam policy handle */
2827 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2830 if (!NT_STATUS_IS_OK(result))
2833 /* Get domain policy handle */
2835 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2836 MAXIMUM_ALLOWED_ACCESS,
2837 domain_sid, &domain_pol);
2839 if (!NT_STATUS_IS_OK(result))
2842 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2843 1, argv, &num_rids, &rids, &rid_types);
2845 if (num_rids != 1) {
2846 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2850 if (rid_types[0] != SID_NAME_DOM_GRP) {
2851 d_fprintf(stderr, "Can only rename domain groups\n");
2852 return NT_STATUS_UNSUCCESSFUL;
2855 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2856 MAXIMUM_ALLOWED_ACCESS,
2857 rids[0], &group_pol);
2859 if (!NT_STATUS_IS_OK(result))
2864 ctr.switch_value1 = 2;
2865 init_samr_group_info2(&ctr.group.info2, argv[1]);
2867 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2869 if (!NT_STATUS_IS_OK(result))
2872 return NT_STATUS_NO_SUCH_GROUP;
2875 static int rpc_group_rename(int argc, const char **argv)
2878 return rpc_group_usage(argc, argv);
2881 return run_rpc_command(NULL, PI_SAMR, 0,
2882 rpc_group_rename_internals,
2887 * 'net rpc group' entrypoint.
2888 * @param argc Standard main() style argc
2889 * @param argc Standard main() style argv. Initial components are already
2893 int net_rpc_group(int argc, const char **argv)
2895 struct functable func[] = {
2896 {"add", rpc_group_add},
2897 {"delete", rpc_group_delete},
2898 {"addmem", rpc_group_addmem},
2899 {"delmem", rpc_group_delmem},
2900 {"list", rpc_group_list},
2901 {"members", rpc_group_members},
2902 {"rename", rpc_group_rename},
2907 return run_rpc_command(NULL, PI_SAMR, 0,
2908 rpc_group_list_internals,
2912 return net_run_function(argc, argv, func, rpc_group_usage);
2915 /****************************************************************************/
2917 static int rpc_share_usage(int argc, const char **argv)
2919 return net_help_share(argc, argv);
2923 * Add a share on a remote RPC server
2925 * All parameters are provided by the run_rpc_command function, except for
2926 * argc, argv which are passes through.
2928 * @param domain_sid The domain sid acquired from the remote server
2929 * @param cli A cli_state connected to the server.
2930 * @param mem_ctx Talloc context, destoyed on completion of the function.
2931 * @param argc Standard main() style argc
2932 * @param argv Standard main() style argv. Initial components are already
2935 * @return Normal NTSTATUS return.
2937 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
2938 const char *domain_name,
2939 struct cli_state *cli,
2940 struct rpc_pipe_client *pipe_hnd,
2941 TALLOC_CTX *mem_ctx,int argc,
2945 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2947 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2948 uint32 num_users=0, perms=0;
2949 char *password=NULL; /* don't allow a share password */
2952 path = strchr(sharename, '=');
2954 return NT_STATUS_UNSUCCESSFUL;
2957 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
2958 opt_comment, perms, opt_maxusers,
2959 num_users, path, password,
2961 return werror_to_ntstatus(result);
2964 static int rpc_share_add(int argc, const char **argv)
2966 if ((argc < 1) || !strchr(argv[0], '=')) {
2967 DEBUG(1,("Sharename or path not specified on add\n"));
2968 return rpc_share_usage(argc, argv);
2970 return run_rpc_command(NULL, PI_SRVSVC, 0,
2971 rpc_share_add_internals,
2976 * Delete a share on a remote RPC server
2978 * All parameters are provided by the run_rpc_command function, except for
2979 * argc, argv which are passes through.
2981 * @param domain_sid The domain sid acquired from the remote server
2982 * @param cli A cli_state connected to the server.
2983 * @param mem_ctx Talloc context, destoyed on completion of the function.
2984 * @param argc Standard main() style argc
2985 * @param argv Standard main() style argv. Initial components are already
2988 * @return Normal NTSTATUS return.
2990 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
2991 const char *domain_name,
2992 struct cli_state *cli,
2993 struct rpc_pipe_client *pipe_hnd,
2994 TALLOC_CTX *mem_ctx,
3000 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
3001 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3005 * Delete a share on a remote RPC server
3007 * @param domain_sid The domain sid acquired from the remote server
3008 * @param argc Standard main() style argc
3009 * @param argv Standard main() style argv. Initial components are already
3012 * @return A shell status integer (0 for success)
3014 static int rpc_share_delete(int argc, const char **argv)
3017 DEBUG(1,("Sharename not specified on delete\n"));
3018 return rpc_share_usage(argc, argv);
3020 return run_rpc_command(NULL, PI_SRVSVC, 0,
3021 rpc_share_del_internals,
3026 * Formatted print of share info
3028 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3031 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3033 fstring netname = "", remark = "";
3035 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3036 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3038 if (opt_long_list_entries) {
3039 d_printf("%-12s %-8.8s %-50s\n",
3040 netname, share_type[info1->info_1.type], remark);
3042 d_printf("%s\n", netname);
3047 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3048 TALLOC_CTX *mem_ctx,
3052 SRV_SHARE_INFO_CTR *ctr)
3055 SRV_SHARE_INFO info;
3057 /* no specific share requested, enumerate all */
3061 uint32 preferred_len = 0xffffffff;
3063 init_enum_hnd(&hnd, 0);
3065 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3066 preferred_len, &hnd);
3069 /* request just one share */
3070 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3072 if (!W_ERROR_IS_OK(result))
3078 ctr->info_level = ctr->switch_value = level;
3079 ctr->ptr_share_info = ctr->ptr_entries = 1;
3080 ctr->num_entries = ctr->num_entries2 = 1;
3086 SRV_SHARE_INFO_1 *info1;
3088 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3089 info1 = ctr->share.info1;
3091 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3093 /* Copy pointer crap */
3095 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3097 /* Duplicate strings */
3099 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3101 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3103 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3105 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3110 SRV_SHARE_INFO_2 *info2;
3112 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3113 info2 = ctr->share.info2;
3115 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3117 /* Copy pointer crap */
3119 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3121 /* Duplicate strings */
3123 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3125 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3127 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3129 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3131 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_path);
3133 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3135 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3137 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3142 SRV_SHARE_INFO_502 *info502;
3144 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3145 info502 = ctr->share.info502;
3147 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3149 /* Copy pointer crap */
3151 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3153 /* Duplicate strings */
3155 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3157 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3159 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3161 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3163 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_path);
3165 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3167 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3169 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3171 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3182 * List shares on a remote RPC server
3184 * All parameters are provided by the run_rpc_command function, except for
3185 * argc, argv which are passes through.
3187 * @param domain_sid The domain sid acquired from the remote server
3188 * @param cli A cli_state connected to the server.
3189 * @param mem_ctx Talloc context, destoyed on completion of the function.
3190 * @param argc Standard main() style argc
3191 * @param argv Standard main() style argv. Initial components are already
3194 * @return Normal NTSTATUS return.
3197 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3198 const char *domain_name,
3199 struct cli_state *cli,
3200 struct rpc_pipe_client *pipe_hnd,
3201 TALLOC_CTX *mem_ctx,
3205 SRV_SHARE_INFO_CTR ctr;
3207 uint32 i, level = 1;
3209 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3210 if (!W_ERROR_IS_OK(result))
3213 /* Display results */
3215 if (opt_long_list_entries) {
3217 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3218 "\nShare name Type Description\n"\
3219 "---------- ---- -----------\n");
3221 for (i = 0; i < ctr.num_entries; i++)
3222 display_share_info_1(&ctr.share.info1[i]);
3224 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3228 * 'net rpc share list' entrypoint.
3229 * @param argc Standard main() style argc
3230 * @param argv Standard main() style argv. Initial components are already
3233 static int rpc_share_list(int argc, const char **argv)
3235 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3238 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3240 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3241 d_printf("skipping [%s]: not a file share.\n", netname);
3251 static BOOL check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3253 /* only support disk shares */
3254 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3255 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3259 /* skip builtin shares */
3260 /* FIXME: should print$ be added too ? */
3261 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3262 strequal(netname,"global"))
3265 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3266 printf("excluding [%s]\n", netname);
3270 return check_share_availability(cli, netname);
3274 * Migrate shares from a remote RPC server to the local RPC srever
3276 * All parameters are provided by the run_rpc_command function, except for
3277 * argc, argv which are passes through.
3279 * @param domain_sid The domain sid acquired from the remote server
3280 * @param cli A cli_state connected to the server.
3281 * @param mem_ctx Talloc context, destoyed on completion of the function.
3282 * @param argc Standard main() style argc
3283 * @param argv Standard main() style argv. Initial components are already
3286 * @return Normal NTSTATUS return.
3289 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3290 const char *domain_name,
3291 struct cli_state *cli,
3292 struct rpc_pipe_client *pipe_hnd,
3293 TALLOC_CTX *mem_ctx,
3298 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3299 SRV_SHARE_INFO_CTR ctr_src;
3300 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3301 char *password = NULL; /* don't allow a share password */
3303 struct rpc_pipe_client *srvsvc_pipe = NULL;
3304 struct cli_state *cli_dst = NULL;
3305 uint32 level = 502; /* includes secdesc */
3307 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3308 if (!W_ERROR_IS_OK(result))
3311 /* connect destination PI_SRVSVC */
3312 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3313 if (!NT_STATUS_IS_OK(nt_status))
3317 for (i = 0; i < ctr_src.num_entries; i++) {
3319 fstring netname = "", remark = "", path = "";
3320 /* reset error-code */
3321 nt_status = NT_STATUS_UNSUCCESSFUL;
3323 rpcstr_pull_unistr2_fstring(
3324 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3325 rpcstr_pull_unistr2_fstring(
3326 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3327 rpcstr_pull_unistr2_fstring(
3328 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3330 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3333 /* finally add the share on the dst server */
3335 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3336 netname, path, remark);
3338 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3339 ctr_src.share.info502[i].info_502.perms,
3340 ctr_src.share.info502[i].info_502.max_uses,
3341 ctr_src.share.info502[i].info_502.num_uses,
3342 path, password, level,
3345 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3346 printf(" [%s] does already exist\n", netname);
3350 if (!W_ERROR_IS_OK(result)) {
3351 printf("cannot add share: %s\n", dos_errstr(result));
3357 nt_status = NT_STATUS_OK;
3361 cli_shutdown(cli_dst);
3369 * Migrate shares from a rpc-server to another
3371 * @param argc Standard main() style argc
3372 * @param argv Standard main() style argv. Initial components are already
3375 * @return A shell status integer (0 for success)
3377 static int rpc_share_migrate_shares(int argc, const char **argv)
3381 printf("no server to migrate\n");
3385 return run_rpc_command(NULL, PI_SRVSVC, 0,
3386 rpc_share_migrate_shares_internals,
3393 * @param f file_info
3394 * @param mask current search mask
3395 * @param state arg-pointer
3398 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3400 static NTSTATUS nt_status;
3401 static struct copy_clistate *local_state;
3402 static fstring filename, new_mask;
3406 local_state = (struct copy_clistate *)state;
3407 nt_status = NT_STATUS_UNSUCCESSFUL;
3409 if (strequal(f->name, ".") || strequal(f->name, ".."))
3412 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3415 if (f->mode & aDIR) {
3417 DEBUG(3,("got dir: %s\n", f->name));
3419 fstrcpy(dir, local_state->cwd);
3421 fstrcat(dir, f->name);
3423 switch (net_mode_share)
3425 case NET_MODE_SHARE_MIGRATE:
3426 /* create that directory */
3427 nt_status = net_copy_file(local_state->mem_ctx,
3428 local_state->cli_share_src,
3429 local_state->cli_share_dst,
3431 opt_acls? True : False,
3432 opt_attrs? True : False,
3433 opt_timestamps? True : False,
3437 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3441 if (!NT_STATUS_IS_OK(nt_status))
3442 printf("could not handle dir %s: %s\n",
3443 dir, nt_errstr(nt_status));
3445 /* search below that directory */
3446 fstrcpy(new_mask, dir);
3447 fstrcat(new_mask, "\\*");
3449 old_dir = local_state->cwd;
3450 local_state->cwd = dir;
3451 if (!sync_files(local_state, new_mask))
3452 printf("could not handle files\n");
3453 local_state->cwd = old_dir;
3460 fstrcpy(filename, local_state->cwd);
3461 fstrcat(filename, "\\");
3462 fstrcat(filename, f->name);
3464 DEBUG(3,("got file: %s\n", filename));
3466 switch (net_mode_share)
3468 case NET_MODE_SHARE_MIGRATE:
3469 nt_status = net_copy_file(local_state->mem_ctx,
3470 local_state->cli_share_src,
3471 local_state->cli_share_dst,
3473 opt_acls? True : False,
3474 opt_attrs? True : False,
3475 opt_timestamps? True: False,
3479 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3483 if (!NT_STATUS_IS_OK(nt_status))
3484 printf("could not handle file %s: %s\n",
3485 filename, nt_errstr(nt_status));
3490 * sync files, can be called recursivly to list files
3491 * and then call copy_fn for each file
3493 * @param cp_clistate pointer to the copy_clistate we work with
3494 * @param mask the current search mask
3496 * @return Boolean result
3498 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3501 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3503 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3504 d_fprintf(stderr, "listing %s failed with error: %s\n",
3505 mask, cli_errstr(cp_clistate->cli_share_src));
3514 * Set the top level directory permissions before we do any further copies.
3515 * Should set up ACL inheritance.
3518 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3519 const char *sharename)
3521 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3523 switch (net_mode_share) {
3524 case NET_MODE_SHARE_MIGRATE:
3525 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3526 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3527 cp_clistate->cli_share_src,
3528 cp_clistate->cli_share_dst,
3530 opt_acls? True : False,
3531 opt_attrs? True : False,
3532 opt_timestamps? True: False,
3536 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3540 if (!NT_STATUS_IS_OK(nt_status)) {
3541 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3542 sharename, nt_errstr(nt_status));
3550 * Sync all files inside a remote share to another share (over smb)
3552 * All parameters are provided by the run_rpc_command function, except for
3553 * argc, argv which are passes through.
3555 * @param domain_sid The domain sid acquired from the remote server
3556 * @param cli A cli_state connected to the server.
3557 * @param mem_ctx Talloc context, destoyed on completion of the function.
3558 * @param argc Standard main() style argc
3559 * @param argv Standard main() style argv. Initial components are already
3562 * @return Normal NTSTATUS return.
3565 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3566 const char *domain_name,
3567 struct cli_state *cli,
3568 struct rpc_pipe_client *pipe_hnd,
3569 TALLOC_CTX *mem_ctx,
3574 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3575 SRV_SHARE_INFO_CTR ctr_src;
3578 struct copy_clistate cp_clistate;
3579 BOOL got_src_share = False;
3580 BOOL got_dst_share = False;
3581 pstring mask = "\\*";
3584 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3586 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3588 if (!W_ERROR_IS_OK(result))
3591 for (i = 0; i < ctr_src.num_entries; i++) {
3593 fstring netname = "";
3595 rpcstr_pull_unistr2_fstring(
3596 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3598 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3601 /* one might not want to mirror whole discs :) */
3602 if (strequal(netname, "print$") || netname[1] == '$') {
3603 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3607 switch (net_mode_share)
3609 case NET_MODE_SHARE_MIGRATE:
3613 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3616 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3618 opt_acls ? "including" : "without",
3619 opt_attrs ? "including" : "without",
3620 opt_timestamps ? "(preserving timestamps)" : "");
3622 cp_clistate.mem_ctx = mem_ctx;
3623 cp_clistate.cli_share_src = NULL;
3624 cp_clistate.cli_share_dst = NULL;
3625 cp_clistate.cwd = NULL;
3626 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3628 /* open share source */
3629 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3630 &cli->dest_ip, cli->desthost,
3632 if (!NT_STATUS_IS_OK(nt_status))
3635 got_src_share = True;
3637 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3638 /* open share destination */
3639 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3640 NULL, dst, netname, "A:");
3641 if (!NT_STATUS_IS_OK(nt_status))
3644 got_dst_share = True;
3647 if (!copy_top_level_perms(&cp_clistate, netname)) {
3648 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
3649 nt_status = NT_STATUS_UNSUCCESSFUL;
3653 if (!sync_files(&cp_clistate, mask)) {
3654 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
3655 nt_status = NT_STATUS_UNSUCCESSFUL;
3660 nt_status = NT_STATUS_OK;
3665 cli_shutdown(cp_clistate.cli_share_src);
3668 cli_shutdown(cp_clistate.cli_share_dst);
3674 static int rpc_share_migrate_files(int argc, const char **argv)
3678 printf("no server to migrate\n");
3682 return run_rpc_command(NULL, PI_SRVSVC, 0,
3683 rpc_share_migrate_files_internals,
3688 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3690 * All parameters are provided by the run_rpc_command function, except for
3691 * argc, argv which are passes through.
3693 * @param domain_sid The domain sid acquired from the remote server
3694 * @param cli A cli_state connected to the server.
3695 * @param mem_ctx Talloc context, destoyed on completion of the function.
3696 * @param argc Standard main() style argc
3697 * @param argv Standard main() style argv. Initial components are already
3700 * @return Normal NTSTATUS return.
3703 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3704 const char *domain_name,
3705 struct cli_state *cli,
3706 struct rpc_pipe_client *pipe_hnd,
3707 TALLOC_CTX *mem_ctx,
3712 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3713 SRV_SHARE_INFO_CTR ctr_src;
3714 SRV_SHARE_INFO info;
3716 struct rpc_pipe_client *srvsvc_pipe = NULL;
3717 struct cli_state *cli_dst = NULL;
3718 uint32 level = 502; /* includes secdesc */
3720 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3722 if (!W_ERROR_IS_OK(result))
3725 /* connect destination PI_SRVSVC */
3726 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3727 if (!NT_STATUS_IS_OK(nt_status))
3731 for (i = 0; i < ctr_src.num_entries; i++) {
3733 fstring netname = "", remark = "", path = "";
3734 /* reset error-code */
3735 nt_status = NT_STATUS_UNSUCCESSFUL;
3737 rpcstr_pull_unistr2_fstring(
3738 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3739 rpcstr_pull_unistr2_fstring(
3740 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3741 rpcstr_pull_unistr2_fstring(
3742 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3744 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3747 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3748 netname, path, remark);
3751 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
3756 info.switch_value = level;
3757 info.ptr_share_ctr = 1;
3759 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3760 info.share.info502 = ctr_src.share.info502[i];
3762 /* finally modify the share on the dst server */
3763 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
3765 if (!W_ERROR_IS_OK(result)) {
3766 printf("cannot set share-acl: %s\n", dos_errstr(result));
3772 nt_status = NT_STATUS_OK;
3776 cli_shutdown(cli_dst);
3784 * Migrate share-acls from a rpc-server to another
3786 * @param argc Standard main() style argc
3787 * @param argv Standard main() style argv. Initial components are already
3790 * @return A shell status integer (0 for success)
3792 static int rpc_share_migrate_security(int argc, const char **argv)
3796 printf("no server to migrate\n");
3800 return run_rpc_command(NULL, PI_SRVSVC, 0,
3801 rpc_share_migrate_security_internals,
3806 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3807 * from one server to another
3809 * @param argc Standard main() style argc
3810 * @param argv Standard main() style argv. Initial components are already
3813 * @return A shell status integer (0 for success)
3816 static int rpc_share_migrate_all(int argc, const char **argv)
3821 printf("no server to migrate\n");
3825 /* order is important. we don't want to be locked out by the share-acl
3826 * before copying files - gd */
3828 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3832 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3836 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3841 * 'net rpc share migrate' entrypoint.
3842 * @param argc Standard main() style argc
3843 * @param argv Standard main() style argv. Initial components are already
3846 static int rpc_share_migrate(int argc, const char **argv)
3849 struct functable func[] = {
3850 {"all", rpc_share_migrate_all},
3851 {"files", rpc_share_migrate_files},
3852 {"help", rpc_share_usage},
3853 {"security", rpc_share_migrate_security},
3854 {"shares", rpc_share_migrate_shares},
3858 net_mode_share = NET_MODE_SHARE_MIGRATE;
3860 return net_run_function(argc, argv, func, rpc_share_usage);
3869 static int num_server_aliases;
3870 static struct full_alias *server_aliases;
3873 * Add an alias to the static list.
3875 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3877 if (server_aliases == NULL)
3878 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3880 server_aliases[num_server_aliases] = *alias;
3881 num_server_aliases += 1;
3885 * For a specific domain on the server, fetch all the aliases
3886 * and their members. Add all of them to the server_aliases.
3889 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3890 TALLOC_CTX *mem_ctx,
3891 POLICY_HND *connect_pol,
3892 const DOM_SID *domain_sid)
3894 uint32 start_idx, max_entries, num_entries, i;
3895 struct acct_info *groups;
3897 POLICY_HND domain_pol;
3899 /* Get domain policy handle */
3901 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3902 MAXIMUM_ALLOWED_ACCESS,
3903 domain_sid, &domain_pol);
3904 if (!NT_STATUS_IS_OK(result))
3911 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
3912 &start_idx, max_entries,
3913 &groups, &num_entries);
3915 for (i = 0; i < num_entries; i++) {
3917 POLICY_HND alias_pol;
3918 struct full_alias alias;
3922 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
3923 MAXIMUM_ALLOWED_ACCESS,
3926 if (!NT_STATUS_IS_OK(result))
3929 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
3933 if (!NT_STATUS_IS_OK(result))
3936 result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
3937 if (!NT_STATUS_IS_OK(result))
3940 alias.members = NULL;
3942 if (alias.num_members > 0) {
3943 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3945 for (j = 0; j < alias.num_members; j++)
3946 sid_copy(&alias.members[j],
3950 sid_copy(&alias.sid, domain_sid);
3951 sid_append_rid(&alias.sid, groups[i].rid);
3953 push_alias(mem_ctx, &alias);
3955 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3957 result = NT_STATUS_OK;
3960 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
3966 * Dump server_aliases as names for debugging purposes.
3969 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
3970 const char *domain_name,
3971 struct cli_state *cli,
3972 struct rpc_pipe_client *pipe_hnd,
3973 TALLOC_CTX *mem_ctx,
3981 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
3982 SEC_RIGHTS_MAXIMUM_ALLOWED,
3984 if (!NT_STATUS_IS_OK(result))
3987 for (i=0; i<num_server_aliases; i++) {
3993 struct full_alias *alias = &server_aliases[i];
3995 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3997 &domains, &names, &types);
3998 if (!NT_STATUS_IS_OK(result))
4001 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4003 if (alias->num_members == 0) {
4008 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4011 &domains, &names, &types);
4013 if (!NT_STATUS_IS_OK(result) &&
4014 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4017 for (j=0; j<alias->num_members; j++)
4018 DEBUG(1, ("%s\\%s (%d); ",
4019 domains[j] ? domains[j] : "*unknown*",
4020 names[j] ? names[j] : "*unknown*",types[j]));
4024 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
4026 return NT_STATUS_OK;
4030 * Fetch a list of all server aliases and their members into
4034 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4035 const char *domain_name,
4036 struct cli_state *cli,
4037 struct rpc_pipe_client *pipe_hnd,
4038 TALLOC_CTX *mem_ctx,
4043 POLICY_HND connect_pol;
4045 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4048 if (!NT_STATUS_IS_OK(result))
4051 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4052 &global_sid_Builtin);
4054 if (!NT_STATUS_IS_OK(result))
4057 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4060 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4065 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4067 token->num_sids = 4;
4069 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4071 token->user_sids[0] = *user_sid;
4072 sid_copy(&token->user_sids[1], &global_sid_World);
4073 sid_copy(&token->user_sids[2], &global_sid_Network);
4074 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4077 static void free_user_token(NT_USER_TOKEN *token)
4079 SAFE_FREE(token->user_sids);
4082 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4086 for (i=0; i<token->num_sids; i++) {
4087 if (sid_compare(sid, &token->user_sids[i]) == 0)
4093 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4095 if (is_sid_in_token(token, sid))
4098 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4099 if (!token->user_sids) {
4103 sid_copy(&token->user_sids[token->num_sids], sid);
4105 token->num_sids += 1;
4110 NT_USER_TOKEN token;
4113 static void dump_user_token(struct user_token *token)
4117 d_printf("%s\n", token->name);
4119 for (i=0; i<token->token.num_sids; i++) {
4120 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4124 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4128 for (i=0; i<alias->num_members; i++) {
4129 if (sid_compare(sid, &alias->members[i]) == 0)
4136 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4140 for (i=0; i<num_server_aliases; i++) {
4141 if (is_alias_member(&sid, &server_aliases[i]))
4142 add_sid_to_token(token, &server_aliases[i].sid);
4147 * We got a user token with all the SIDs we can know about without asking the
4148 * server directly. These are the user and domain group sids. All of these can
4149 * be members of aliases. So scan the list of aliases for each of the SIDs and
4150 * add them to the token.
4153 static void collect_alias_memberships(NT_USER_TOKEN *token)
4155 int num_global_sids = token->num_sids;
4158 for (i=0; i<num_global_sids; i++) {
4159 collect_sid_memberships(token, token->user_sids[i]);
4163 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4165 struct winbindd_request request;
4166 struct winbindd_response response;
4174 fstr_sprintf(full_name, "%s%c%s",
4175 domain, *lp_winbind_separator(), user);
4177 /* First let's find out the user sid */
4179 ZERO_STRUCT(request);
4180 ZERO_STRUCT(response);
4182 fstrcpy(request.data.name.dom_name, domain);
4183 fstrcpy(request.data.name.name, user);
4185 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4187 if (result != NSS_STATUS_SUCCESS) {
4188 DEBUG(1, ("winbind could not find %s\n", full_name));
4192 if (response.data.sid.type != SID_NAME_USER) {
4193 DEBUG(1, ("%s is not a user\n", full_name));
4197 string_to_sid(&user_sid, response.data.sid.sid);
4199 init_user_token(token, &user_sid);
4201 /* And now the groups winbind knows about */
4203 ZERO_STRUCT(response);
4205 fstrcpy(request.data.username, full_name);
4207 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4209 if (result != NSS_STATUS_SUCCESS) {
4210 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4214 for (i = 0; i < response.data.num_entries; i++) {
4215 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4218 struct winbindd_request sidrequest;
4219 struct winbindd_response sidresponse;
4221 ZERO_STRUCT(sidrequest);
4222 ZERO_STRUCT(sidresponse);
4224 sidrequest.data.gid = gid;
4226 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4227 &sidrequest, &sidresponse);
4229 if (result != NSS_STATUS_SUCCESS) {
4230 DEBUG(1, ("winbind could not find SID of gid %d\n",
4235 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4237 string_to_sid(&sid, sidresponse.data.sid.sid);
4238 add_sid_to_token(token, &sid);
4241 SAFE_FREE(response.extra_data.data);
4247 * Get a list of all user tokens we want to look at
4250 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4252 struct winbindd_request request;
4253 struct winbindd_response response;
4254 const char *extra_data;
4257 struct user_token *result;
4259 if (lp_winbind_use_default_domain() &&
4260 (opt_target_workgroup == NULL)) {
4261 d_fprintf(stderr, "winbind use default domain = yes set, "
4262 "please specify a workgroup\n");
4266 /* Send request to winbind daemon */
4268 ZERO_STRUCT(request);
4269 ZERO_STRUCT(response);
4271 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4275 /* Look through extra data */
4277 if (!response.extra_data.data)
4280 extra_data = (const char *)response.extra_data.data;
4283 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4287 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4289 if (result == NULL) {
4290 DEBUG(1, ("Could not malloc sid array\n"));
4294 extra_data = (const char *)response.extra_data.data;
4297 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4299 fstring domain, user;
4302 fstrcpy(result[i].name, name);
4304 p = strchr(name, *lp_winbind_separator());
4306 DEBUG(3, ("%s\n", name));
4309 fstrcpy(domain, opt_target_workgroup);
4310 fstrcpy(user, name);
4313 fstrcpy(domain, name);
4318 get_user_sids(domain, user, &(result[i].token));
4322 SAFE_FREE(response.extra_data.data);
4324 *user_tokens = result;
4329 static BOOL get_user_tokens_from_file(FILE *f,
4331 struct user_token **tokens)
4333 struct user_token *token = NULL;
4338 if (fgets(line, sizeof(line)-1, f) == NULL) {
4342 if (line[strlen(line)-1] == '\n')
4343 line[strlen(line)-1] = '\0';
4345 if (line[0] == ' ') {
4349 string_to_sid(&sid, &line[1]);
4351 if (token == NULL) {
4352 DEBUG(0, ("File does not begin with username"));
4356 add_sid_to_token(&token->token, &sid);
4360 /* And a new user... */
4363 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4364 if (*tokens == NULL) {
4365 DEBUG(0, ("Could not realloc tokens\n"));
4369 token = &((*tokens)[*num_tokens-1]);
4371 fstrcpy(token->name, line);
4372 token->token.num_sids = 0;
4373 token->token.user_sids = NULL;
4382 * Show the list of all users that have access to a share
4385 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4386 TALLOC_CTX *mem_ctx,
4387 const char *netname,
4389 struct user_token *tokens)
4392 SEC_DESC *share_sd = NULL;
4393 SEC_DESC *root_sd = NULL;
4394 struct cli_state *cli = pipe_hnd->cli;
4396 SRV_SHARE_INFO info;
4400 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4403 if (!W_ERROR_IS_OK(result)) {
4404 DEBUG(1, ("Coult not query secdesc for share %s\n",
4409 share_sd = info.share.info502.info_502_str.sd;
4410 if (share_sd == NULL) {
4411 DEBUG(1, ("Got no secdesc for share %s\n",
4417 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4421 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4424 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4427 for (i=0; i<num_tokens; i++) {
4431 if (share_sd != NULL) {
4432 if (!se_access_check(share_sd, &tokens[i].token,
4433 1, &acc_granted, &status)) {
4434 DEBUG(1, ("Could not check share_sd for "
4440 if (!NT_STATUS_IS_OK(status))
4444 if (root_sd == NULL) {
4445 d_printf(" %s\n", tokens[i].name);
4449 if (!se_access_check(root_sd, &tokens[i].token,
4450 1, &acc_granted, &status)) {
4451 DEBUG(1, ("Could not check root_sd for user %s\n",
4456 if (!NT_STATUS_IS_OK(status))
4459 d_printf(" %s\n", tokens[i].name);
4463 cli_close(cli, fnum);
4475 static void collect_share(const char *name, uint32 m,
4476 const char *comment, void *state)
4478 struct share_list *share_list = (struct share_list *)state;
4480 if (m != STYPE_DISKTREE)
4483 share_list->num_shares += 1;
4484 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4485 if (!share_list->shares) {
4486 share_list->num_shares = 0;
4489 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4492 static void rpc_share_userlist_usage(void)
4498 * List shares on a remote RPC server, including the security descriptors
4500 * All parameters are provided by the run_rpc_command function, except for
4501 * argc, argv which are passes through.
4503 * @param domain_sid The domain sid acquired from the remote server
4504 * @param cli A cli_state connected to the server.
4505 * @param mem_ctx Talloc context, destoyed on completion of the function.
4506 * @param argc Standard main() style argc
4507 * @param argv Standard main() style argv. Initial components are already
4510 * @return Normal NTSTATUS return.
4513 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4514 const char *domain_name,
4515 struct cli_state *cli,
4516 struct rpc_pipe_client *pipe_hnd,
4517 TALLOC_CTX *mem_ctx,
4527 struct user_token *tokens = NULL;
4530 struct share_list share_list;
4533 rpc_share_userlist_usage();
4534 return NT_STATUS_UNSUCCESSFUL;
4540 f = fopen(argv[0], "r");
4544 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4545 return NT_STATUS_UNSUCCESSFUL;
4548 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4554 DEBUG(0, ("Could not read users from file\n"));
4555 return NT_STATUS_UNSUCCESSFUL;
4558 for (i=0; i<num_tokens; i++)
4559 collect_alias_memberships(&tokens[i].token);
4561 init_enum_hnd(&hnd, 0);
4563 share_list.num_shares = 0;
4564 share_list.shares = NULL;
4566 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4569 DEBUG(0, ("Error returning browse list: %s\n",
4574 for (i = 0; i < share_list.num_shares; i++) {
4575 char *netname = share_list.shares[i];
4577 if (netname[strlen(netname)-1] == '$')
4580 d_printf("%s\n", netname);
4582 show_userlist(pipe_hnd, mem_ctx, netname,
4583 num_tokens, tokens);
4586 for (i=0; i<num_tokens; i++) {
4587 free_user_token(&tokens[i].token);
4590 SAFE_FREE(share_list.shares);
4592 return NT_STATUS_OK;
4595 static int rpc_share_allowedusers(int argc, const char **argv)
4599 result = run_rpc_command(NULL, PI_SAMR, 0,
4600 rpc_aliaslist_internals,
4605 result = run_rpc_command(NULL, PI_LSARPC, 0,
4611 return run_rpc_command(NULL, PI_SRVSVC, 0,
4612 rpc_share_allowedusers_internals,
4616 int net_usersidlist(int argc, const char **argv)
4619 struct user_token *tokens = NULL;
4623 net_usersidlist_usage(argc, argv);
4627 if (!get_user_tokens(&num_tokens, &tokens)) {
4628 DEBUG(0, ("Could not get the user/sid list\n"));
4632 for (i=0; i<num_tokens; i++) {
4633 dump_user_token(&tokens[i]);
4634 free_user_token(&tokens[i].token);
4641 int net_usersidlist_usage(int argc, const char **argv)
4643 d_printf("net usersidlist\n"
4644 "\tprints out a list of all users the running winbind knows\n"
4645 "\tabout, together with all their SIDs. This is used as\n"
4646 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4648 net_common_flags_usage(argc, argv);
4653 * 'net rpc share' entrypoint.
4654 * @param argc Standard main() style argc
4655 * @param argv Standard main() style argv. Initial components are already
4659 int net_rpc_share(int argc, const char **argv)
4661 struct functable func[] = {
4662 {"add", rpc_share_add},
4663 {"delete", rpc_share_delete},
4664 {"allowedusers", rpc_share_allowedusers},
4665 {"migrate", rpc_share_migrate},
4666 {"list", rpc_share_list},
4671 return run_rpc_command(NULL, PI_SRVSVC, 0,
4672 rpc_share_list_internals,
4675 return net_run_function(argc, argv, func, rpc_share_usage);
4678 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4679 struct rpc_sh_ctx *ctx,
4680 struct rpc_pipe_client *pipe_hnd,
4681 int argc, const char **argv)
4683 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4684 ctx->cli, pipe_hnd, mem_ctx,
4688 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4689 struct rpc_sh_ctx *ctx,
4690 struct rpc_pipe_client *pipe_hnd,
4691 int argc, const char **argv)
4695 if ((argc < 2) || (argc > 3)) {
4696 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4698 return NT_STATUS_INVALID_PARAMETER;
4701 result = rpccli_srvsvc_net_share_add(
4702 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
4703 (argc == 3) ? argv[2] : "",
4704 0, 0, 0, argv[1], NULL, 2, NULL);
4706 return werror_to_ntstatus(result);
4709 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4710 struct rpc_sh_ctx *ctx,
4711 struct rpc_pipe_client *pipe_hnd,
4712 int argc, const char **argv)
4717 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4718 return NT_STATUS_INVALID_PARAMETER;
4721 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
4722 return werror_to_ntstatus(result);
4725 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4726 struct rpc_sh_ctx *ctx,
4727 struct rpc_pipe_client *pipe_hnd,
4728 int argc, const char **argv)
4730 SRV_SHARE_INFO info;
4731 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
4735 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4736 return NT_STATUS_INVALID_PARAMETER;
4739 result = rpccli_srvsvc_net_share_get_info(
4740 pipe_hnd, mem_ctx, argv[0], 2, &info);
4741 if (!W_ERROR_IS_OK(result)) {
4745 d_printf("Name: %s\n",
4746 rpcstr_pull_unistr2_talloc(mem_ctx,
4747 &info2->info_2_str.uni_netname));
4748 d_printf("Comment: %s\n",
4749 rpcstr_pull_unistr2_talloc(mem_ctx,
4750 &info2->info_2_str.uni_remark));
4752 d_printf("Path: %s\n",
4753 rpcstr_pull_unistr2_talloc(mem_ctx,
4754 &info2->info_2_str.uni_path));
4755 d_printf("Password: %s\n",
4756 rpcstr_pull_unistr2_talloc(mem_ctx,
4757 &info2->info_2_str.uni_passwd));
4760 return werror_to_ntstatus(result);
4763 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4764 struct rpc_sh_ctx *ctx)
4766 static struct rpc_sh_cmd cmds[] = {
4768 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4769 "List available shares" },
4771 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4774 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4777 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4778 "Get information about a share" },
4780 { NULL, NULL, 0, NULL, NULL }
4786 /****************************************************************************/
4788 static int rpc_file_usage(int argc, const char **argv)
4790 return net_help_file(argc, argv);
4794 * Close a file on a remote RPC server
4796 * All parameters are provided by the run_rpc_command function, except for
4797 * argc, argv which are passes through.
4799 * @param domain_sid The domain sid acquired from the remote server
4800 * @param cli A cli_state connected to the server.
4801 * @param mem_ctx Talloc context, destoyed on completion of the function.
4802 * @param argc Standard main() style argc
4803 * @param argv Standard main() style argv. Initial components are already
4806 * @return Normal NTSTATUS return.
4808 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4809 const char *domain_name,
4810 struct cli_state *cli,
4811 struct rpc_pipe_client *pipe_hnd,
4812 TALLOC_CTX *mem_ctx,
4817 result = rpccli_srvsvc_net_file_close(pipe_hnd, mem_ctx, atoi(argv[0]));
4818 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4822 * Close a file on a remote RPC server
4824 * @param argc Standard main() style argc
4825 * @param argv Standard main() style argv. Initial components are already
4828 * @return A shell status integer (0 for success)
4830 static int rpc_file_close(int argc, const char **argv)
4833 DEBUG(1, ("No fileid given on close\n"));
4834 return(rpc_file_usage(argc, argv));
4837 return run_rpc_command(NULL, PI_SRVSVC, 0,
4838 rpc_file_close_internals,
4843 * Formatted print of open file info
4845 * @param info3 FILE_INFO_3 contents
4846 * @param str3 strings for FILE_INFO_3
4849 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
4851 fstring user = "", path = "";
4853 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
4854 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
4856 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4857 info3->id, user, info3->perms, info3->num_locks, path);
4861 * List open files on a remote RPC server
4863 * All parameters are provided by the run_rpc_command function, except for
4864 * argc, argv which are passes through.
4866 * @param domain_sid The domain sid acquired from the remote server
4867 * @param cli A cli_state connected to the server.
4868 * @param mem_ctx Talloc context, destoyed on completion of the function.
4869 * @param argc Standard main() style argc
4870 * @param argv Standard main() style argv. Initial components are already
4873 * @return Normal NTSTATUS return.
4876 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4877 const char *domain_name,
4878 struct cli_state *cli,
4879 struct rpc_pipe_client *pipe_hnd,
4880 TALLOC_CTX *mem_ctx,
4884 SRV_FILE_INFO_CTR ctr;
4887 uint32 preferred_len = 0xffffffff, i;
4888 const char *username=NULL;
4890 init_enum_hnd(&hnd, 0);
4892 /* if argc > 0, must be user command */
4894 username = smb_xstrdup(argv[0]);
4896 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
4897 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4899 if (!W_ERROR_IS_OK(result))
4902 /* Display results */
4905 "\nEnumerating open files on remote server:\n\n"\
4906 "\nFileId Opened by Perms Locks Path"\
4907 "\n------ --------- ----- ----- ---- \n");
4908 for (i = 0; i < ctr.num_entries; i++)
4909 display_file_info_3(&ctr.file.info3[i].info_3,
4910 &ctr.file.info3[i].info_3_str);
4912 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4916 * List files for a user on a remote RPC server
4918 * @param argc Standard main() style argc
4919 * @param argv Standard main() style argv. Initial components are already
4922 * @return A shell status integer (0 for success)
4925 static int rpc_file_user(int argc, const char **argv)
4928 DEBUG(1, ("No username given\n"));
4929 return(rpc_file_usage(argc, argv));
4932 return run_rpc_command(NULL, PI_SRVSVC, 0,
4933 rpc_file_list_internals,
4938 * 'net rpc file' entrypoint.
4939 * @param argc Standard main() style argc
4940 * @param argv Standard main() style argv. Initial components are already
4944 int net_rpc_file(int argc, const char **argv)
4946 struct functable func[] = {
4947 {"close", rpc_file_close},
4948 {"user", rpc_file_user},
4950 {"info", rpc_file_info},
4956 return run_rpc_command(NULL, PI_SRVSVC, 0,
4957 rpc_file_list_internals,
4960 return net_run_function(argc, argv, func, rpc_file_usage);
4964 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4966 * All parameters are provided by the run_rpc_command function, except for
4967 * argc, argv which are passed through.
4969 * @param domain_sid The domain sid aquired from the remote server
4970 * @param cli A cli_state connected to the server.
4971 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4972 * @param argc Standard main() style argc
4973 * @param argv Standard main() style argv. Initial components are already
4976 * @return Normal NTSTATUS return.
4979 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4980 const char *domain_name,
4981 struct cli_state *cli,
4982 struct rpc_pipe_client *pipe_hnd,
4983 TALLOC_CTX *mem_ctx,
4987 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4989 result = rpccli_shutdown_abort(pipe_hnd, mem_ctx);
4991 if (NT_STATUS_IS_OK(result)) {
4992 d_printf("\nShutdown successfully aborted\n");
4993 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4995 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5001 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5003 * All parameters are provided by the run_rpc_command function, except for
5004 * argc, argv which are passed through.
5006 * @param domain_sid The domain sid aquired from the remote server
5007 * @param cli A cli_state connected to the server.
5008 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5009 * @param argc Standard main() style argc
5010 * @param argv Standard main() style argv. Initial components are already
5013 * @return Normal NTSTATUS return.
5016 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5017 const char *domain_name,
5018 struct cli_state *cli,
5019 struct rpc_pipe_client *pipe_hnd,
5020 TALLOC_CTX *mem_ctx,
5024 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5026 result = werror_to_ntstatus(rpccli_reg_abort_shutdown(pipe_hnd, mem_ctx));
5028 if (NT_STATUS_IS_OK(result)) {
5029 d_printf("\nShutdown successfully aborted\n");
5030 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5032 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5038 * ABORT the Shut down of a remote RPC server
5040 * @param argc Standard main() style argc
5041 * @param argv Standard main() style argv. Initial components are already
5044 * @return A shell status integer (0 for success)
5047 static int rpc_shutdown_abort(int argc, const char **argv)
5049 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5050 rpc_shutdown_abort_internals,
5056 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5058 return run_rpc_command(NULL, PI_WINREG, 0,
5059 rpc_reg_shutdown_abort_internals,
5064 * Shut down a remote RPC Server via initshutdown pipe
5066 * All parameters are provided by the run_rpc_command function, except for
5067 * argc, argv which are passes through.
5069 * @param domain_sid The domain sid aquired from the remote server
5070 * @param cli A cli_state connected to the server.
5071 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5072 * @param argc Standard main() style argc
5073 * @param argc Standard main() style argv. Initial components are already
5076 * @return Normal NTSTATUS return.
5079 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5080 const char *domain_name,
5081 struct cli_state *cli,
5082 struct rpc_pipe_client *pipe_hnd,
5083 TALLOC_CTX *mem_ctx,
5087 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5088 const char *msg = "This machine will be shutdown shortly";
5089 uint32 timeout = 20;
5095 timeout = opt_timeout;
5098 /* create an entry */
5099 result = rpccli_shutdown_init(pipe_hnd, mem_ctx, msg, timeout, opt_reboot,
5102 if (NT_STATUS_IS_OK(result)) {
5103 d_printf("\nShutdown of remote machine succeeded\n");
5104 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5106 DEBUG(1,("Shutdown of remote machine failed!\n"));
5112 * Shut down a remote RPC Server via winreg pipe
5114 * All parameters are provided by the run_rpc_command function, except for
5115 * argc, argv which are passes through.
5117 * @param domain_sid The domain sid aquired from the remote server
5118 * @param cli A cli_state connected to the server.
5119 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5120 * @param argc Standard main() style argc
5121 * @param argc Standard main() style argv. Initial components are already
5124 * @return Normal NTSTATUS return.
5127 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5128 const char *domain_name,
5129 struct cli_state *cli,
5130 struct rpc_pipe_client *pipe_hnd,
5131 TALLOC_CTX *mem_ctx,
5136 const char *msg = "This machine will be shutdown shortly";
5137 uint32 timeout = 20;
5142 struct poptOption long_options[] = {
5143 {"message", 'm', POPT_ARG_STRING, &msg},
5144 {"timeout", 't', POPT_ARG_INT, &timeout},
5145 {"reboot", 'r', POPT_ARG_NONE, &reboot},
5146 {"force", 'f', POPT_ARG_NONE, &force},
5150 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
5151 POPT_CONTEXT_KEEP_FIRST);
5153 rc = poptGetNextOpt(pc);
5156 /* an error occurred during option processing */
5157 DEBUG(0, ("%s: %s\n",
5158 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
5160 return NT_STATUS_INVALID_PARAMETER;
5167 timeout = opt_timeout;
5170 /* create an entry */
5171 result = rpccli_reg_shutdown(pipe_hnd, mem_ctx, msg, timeout, opt_reboot, opt_force);
5173 if (W_ERROR_IS_OK(result)) {
5174 d_printf("\nShutdown of remote machine succeeded\n");
5176 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5177 if (W_ERROR_EQUAL(result,WERR_MACHINE_LOCKED))
5178 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5180 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(result));
5183 return werror_to_ntstatus(result);
5187 * Shut down a remote RPC server
5189 * @param argc Standard main() style argc
5190 * @param argc Standard main() style argv. Initial components are already
5193 * @return A shell status integer (0 for success)
5196 static int rpc_shutdown(int argc, const char **argv)
5198 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5199 rpc_init_shutdown_internals,
5203 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5204 rc = run_rpc_command(NULL, PI_WINREG, 0,
5205 rpc_reg_shutdown_internals, argc, argv);
5211 /***************************************************************************
5212 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5214 ***************************************************************************/
5217 * Add interdomain trust account to the RPC server.
5218 * All parameters (except for argc and argv) are passed by run_rpc_command
5221 * @param domain_sid The domain sid acquired from the server
5222 * @param cli A cli_state connected to the server.
5223 * @param mem_ctx Talloc context, destoyed on completion of the function.
5224 * @param argc Standard main() style argc
5225 * @param argc Standard main() style argv. Initial components are already
5228 * @return normal NTSTATUS return code
5231 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5232 const char *domain_name,
5233 struct cli_state *cli,
5234 struct rpc_pipe_client *pipe_hnd,
5235 TALLOC_CTX *mem_ctx,
5239 POLICY_HND connect_pol, domain_pol, user_pol;
5240 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5243 uint32 unknown, user_rid;
5246 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5247 return NT_STATUS_INVALID_PARAMETER;
5251 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5254 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5255 return NT_STATUS_NO_MEMORY;
5258 strupper_m(acct_name);
5260 /* Get samr policy handle */
5261 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5263 if (!NT_STATUS_IS_OK(result)) {
5267 /* Get domain policy handle */
5268 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5269 MAXIMUM_ALLOWED_ACCESS,
5270 domain_sid, &domain_pol);
5271 if (!NT_STATUS_IS_OK(result)) {
5275 /* Create trusting domain's account */
5276 acb_info = ACB_NORMAL;
5277 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5278 mimir: yes, most probably it is */
5280 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5281 acct_name, acb_info, unknown,
5282 &user_pol, &user_rid);
5283 if (!NT_STATUS_IS_OK(result)) {
5288 SAM_USERINFO_CTR ctr;
5289 SAM_USER_INFO_23 p23;
5295 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5299 ZERO_STRUCT(notime);
5303 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5304 acb_info = ACB_DOMTRUST;
5306 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5307 ¬ime, ¬ime, ¬ime,
5308 nostr, nostr, nostr, nostr, nostr,
5309 nostr, nostr, nostr, nostr, nostr,
5310 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5311 0, 0, (char *)pwbuf);
5312 ctr.switch_value = 23;
5313 ctr.info.id23 = &p23;
5314 p23.passmustchange = 0;
5316 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5317 &cli->user_session_key, &ctr);
5319 if (!NT_STATUS_IS_OK(result)) {
5320 DEBUG(0,("Could not set trust account password: %s\n",
5321 nt_errstr(result)));
5327 SAFE_FREE(acct_name);
5332 * Create interdomain trust account for a remote domain.
5334 * @param argc standard argc
5335 * @param argv standard argv without initial components
5337 * @return Integer status (0 means success)
5340 static int rpc_trustdom_add(int argc, const char **argv)
5343 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5346 d_printf("Usage: net rpc trustdom add <domain>\n");
5353 * Remove interdomain trust account from the RPC server.
5354 * All parameters (except for argc and argv) are passed by run_rpc_command
5357 * @param domain_sid The domain sid acquired from the server
5358 * @param cli A cli_state connected to the server.
5359 * @param mem_ctx Talloc context, destoyed on completion of the function.
5360 * @param argc Standard main() style argc
5361 * @param argc Standard main() style argv. Initial components are already
5364 * @return normal NTSTATUS return code
5367 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5368 const char *domain_name,
5369 struct cli_state *cli,
5370 struct rpc_pipe_client *pipe_hnd,
5371 TALLOC_CTX *mem_ctx,
5375 POLICY_HND connect_pol, domain_pol, user_pol;
5376 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5379 DOM_SID trust_acct_sid;
5380 uint32 *user_rids, num_rids, *name_types;
5381 uint32 flags = 0x000003e8; /* Unknown */
5384 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5385 return NT_STATUS_INVALID_PARAMETER;
5389 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5391 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5393 if (acct_name == NULL)
5394 return NT_STATUS_NO_MEMORY;
5396 strupper_m(acct_name);
5398 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
5399 names[0] = acct_name;
5402 /* Get samr policy handle */
5403 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5405 if (!NT_STATUS_IS_OK(result)) {
5409 /* Get domain policy handle */
5410 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5411 MAXIMUM_ALLOWED_ACCESS,
5412 domain_sid, &domain_pol);
5413 if (!NT_STATUS_IS_OK(result)) {
5417 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5419 &user_rids, &name_types);
5421 if (!NT_STATUS_IS_OK(result)) {
5425 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5426 MAXIMUM_ALLOWED_ACCESS,
5427 user_rids[0], &user_pol);
5429 if (!NT_STATUS_IS_OK(result)) {
5433 /* append the rid to the domain sid */
5434 sid_copy(&trust_acct_sid, domain_sid);
5435 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5439 /* remove the sid */
5441 result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5444 if (!NT_STATUS_IS_OK(result)) {
5450 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5452 if (!NT_STATUS_IS_OK(result)) {
5456 if (!NT_STATUS_IS_OK(result)) {
5457 DEBUG(0,("Could not set trust account password: %s\n",
5458 nt_errstr(result)));
5467 * Delete interdomain trust account for a remote domain.
5469 * @param argc standard argc
5470 * @param argv standard argv without initial components
5472 * @return Integer status (0 means success)
5475 static int rpc_trustdom_del(int argc, const char **argv)
5478 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5481 d_printf("Usage: net rpc trustdom del <domain>\n");
5488 * Establish trust relationship to a trusting domain.
5489 * Interdomain account must already be created on remote PDC.
5491 * @param argc standard argc
5492 * @param argv standard argv without initial components
5494 * @return Integer status (0 means success)
5497 static int rpc_trustdom_establish(int argc, const char **argv)
5499 struct cli_state *cli = NULL;
5500 struct in_addr server_ip;
5501 struct rpc_pipe_client *pipe_hnd = NULL;
5502 POLICY_HND connect_hnd;
5503 TALLOC_CTX *mem_ctx;
5505 DOM_SID *domain_sid;
5508 char* domain_name_pol;
5513 * Connect to \\server\ipc$ as 'our domain' account with password
5517 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5521 domain_name = smb_xstrdup(argv[0]);
5522 strupper_m(domain_name);
5524 /* account name used at first is our domain's name with '$' */
5525 asprintf(&acct_name, "%s$", lp_workgroup());
5526 strupper_m(acct_name);
5529 * opt_workgroup will be used by connection functions further,
5530 * hence it should be set to remote domain name instead of ours
5532 if (opt_workgroup) {
5533 opt_workgroup = smb_xstrdup(domain_name);
5536 opt_user_name = acct_name;
5538 /* find the domain controller */
5539 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5540 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5544 /* connect to ipc$ as username/password */
5545 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5546 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5548 /* Is it trusting domain account for sure ? */
5549 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5550 nt_errstr(nt_status)));
5554 /* store who we connected to */
5556 saf_store( domain_name, pdc_name );
5559 * Connect to \\server\ipc$ again (this time anonymously)
5562 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5564 if (NT_STATUS_IS_ERR(nt_status)) {
5565 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5566 domain_name, nt_errstr(nt_status)));
5570 * Use NetServerEnum2 to make sure we're talking to a proper server
5573 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5574 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5575 for domain %s\n", domain_name));
5578 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5579 "domain %s", domain_name))) {
5580 DEBUG(0, ("talloc_init() failed\n"));
5586 * Call LsaOpenPolicy and LsaQueryInfo
5589 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5591 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5596 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5598 if (NT_STATUS_IS_ERR(nt_status)) {
5599 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5600 nt_errstr(nt_status)));
5605 /* Querying info level 5 */
5607 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5609 &domain_name_pol, &domain_sid);
5610 if (NT_STATUS_IS_ERR(nt_status)) {
5611 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5612 nt_errstr(nt_status)));
5617 /* There should be actually query info level 3 (following nt serv behaviour),
5618 but I still don't know if it's _really_ necessary */
5621 * Store the password in secrets db
5624 if (!secrets_store_trusted_domain_password(domain_name,
5627 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5633 * Close the pipes and clean up
5636 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5637 if (NT_STATUS_IS_ERR(nt_status)) {
5638 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5639 nt_errstr(nt_status)));
5646 talloc_destroy(mem_ctx);
5648 d_printf("Trust to domain %s established\n", domain_name);
5653 * Revoke trust relationship to the remote domain
5655 * @param argc standard argc
5656 * @param argv standard argv without initial components
5658 * @return Integer status (0 means success)
5661 static int rpc_trustdom_revoke(int argc, const char **argv)
5665 if (argc < 1) return -1;
5667 /* generate upper cased domain name */
5668 domain_name = smb_xstrdup(argv[0]);
5669 strupper_m(domain_name);
5671 /* delete password of the trust */
5672 if (!trusted_domain_password_delete(domain_name)) {
5673 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5682 * Usage for 'net rpc trustdom' command
5684 * @param argc standard argc
5685 * @param argv standard argv without inital components
5687 * @return Integer status returned to shell
5690 static int rpc_trustdom_usage(int argc, const char **argv)
5692 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5693 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5694 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5695 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5696 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5697 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5702 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5703 const char *domain_name,
5704 struct cli_state *cli,
5705 struct rpc_pipe_client *pipe_hnd,
5706 TALLOC_CTX *mem_ctx,
5711 sid_to_string(str_sid, domain_sid);
5712 d_printf("%s\n", str_sid);
5713 return NT_STATUS_OK;
5716 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5718 fstring ascii_sid, padding;
5719 int pad_len, col_len = 20;
5721 /* convert sid into ascii string */
5722 sid_to_string(ascii_sid, dom_sid);
5724 /* calculate padding space for d_printf to look nicer */
5725 pad_len = col_len - strlen(trusted_dom_name);
5726 padding[pad_len] = 0;
5727 do padding[--pad_len] = ' '; while (pad_len);
5729 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5732 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5733 TALLOC_CTX *mem_ctx,
5736 const char *trusted_dom_name)
5739 LSA_TRUSTED_DOMAIN_INFO *info;
5740 char *cleartextpwd = NULL;
5743 nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5745 if (NT_STATUS_IS_ERR(nt_status)) {
5746 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5747 nt_errstr(nt_status)));
5751 data = data_blob(NULL, info->password.password.length);
5753 memcpy(data.data, info->password.password.data, info->password.password.length);
5754 data.length = info->password.password.length;
5756 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5758 if (cleartextpwd == NULL) {
5759 DEBUG(0,("retrieved NULL password\n"));
5760 nt_status = NT_STATUS_UNSUCCESSFUL;
5764 if (!secrets_store_trusted_domain_password(trusted_dom_name,
5767 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5768 nt_status = NT_STATUS_UNSUCCESSFUL;
5772 #ifdef DEBUG_PASSWORD
5773 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5774 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5778 SAFE_FREE(cleartextpwd);
5779 data_blob_free(&data);
5784 static int rpc_trustdom_vampire(int argc, const char **argv)
5786 /* common variables */
5787 TALLOC_CTX* mem_ctx;
5788 struct cli_state *cli = NULL;
5789 struct rpc_pipe_client *pipe_hnd = NULL;
5791 const char *domain_name = NULL;
5792 DOM_SID *queried_dom_sid;
5793 POLICY_HND connect_hnd;
5795 /* trusted domains listing variables */
5796 unsigned int num_domains, enum_ctx = 0;
5798 DOM_SID *domain_sids;
5799 char **trusted_dom_names;
5804 * Listing trusted domains (stored in secrets.tdb, if local)
5807 mem_ctx = talloc_init("trust relationships vampire");
5810 * set domain and pdc name to local samba server (default)
5811 * or to remote one given in command line
5814 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5815 domain_name = opt_workgroup;
5816 opt_target_workgroup = opt_workgroup;
5818 fstrcpy(pdc_name, global_myname());
5819 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5820 opt_target_workgroup = domain_name;
5823 /* open \PIPE\lsarpc and open policy handle */
5824 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5825 DEBUG(0, ("Couldn't connect to domain controller\n"));
5829 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5831 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5832 nt_errstr(nt_status) ));
5837 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5839 if (NT_STATUS_IS_ERR(nt_status)) {
5840 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5841 nt_errstr(nt_status)));
5846 /* query info level 5 to obtain sid of a domain being queried */
5847 nt_status = rpccli_lsa_query_info_policy(
5848 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5849 &dummy, &queried_dom_sid);
5851 if (NT_STATUS_IS_ERR(nt_status)) {
5852 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5853 nt_errstr(nt_status)));
5859 * Keep calling LsaEnumTrustdom over opened pipe until
5860 * the end of enumeration is reached
5863 d_printf("Vampire trusted domains:\n\n");
5866 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5868 &trusted_dom_names, &domain_sids);
5870 if (NT_STATUS_IS_ERR(nt_status)) {
5871 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5872 nt_errstr(nt_status)));
5877 for (i = 0; i < num_domains; i++) {
5879 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5881 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5882 domain_sids[i], trusted_dom_names[i]);
5883 if (!NT_STATUS_IS_OK(nt_status)) {
5890 * in case of no trusted domains say something rather
5891 * than just display blank line
5893 if (!num_domains) d_printf("none\n");
5895 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5897 /* close this connection before doing next one */
5898 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5899 if (NT_STATUS_IS_ERR(nt_status)) {
5900 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5901 nt_errstr(nt_status)));
5906 /* close lsarpc pipe and connection to IPC$ */
5909 talloc_destroy(mem_ctx);
5913 static int rpc_trustdom_list(int argc, const char **argv)
5915 /* common variables */
5916 TALLOC_CTX* mem_ctx;
5917 struct cli_state *cli = NULL, *remote_cli = NULL;
5918 struct rpc_pipe_client *pipe_hnd = NULL;
5920 const char *domain_name = NULL;
5921 DOM_SID *queried_dom_sid;
5923 int ascii_dom_name_len;
5924 POLICY_HND connect_hnd;
5926 /* trusted domains listing variables */
5927 unsigned int num_domains, enum_ctx = 0;
5928 int i, pad_len, col_len = 20;
5929 DOM_SID *domain_sids;
5930 char **trusted_dom_names;
5934 /* trusting domains listing variables */
5935 POLICY_HND domain_hnd;
5936 char **trusting_dom_names;
5937 uint32 *trusting_dom_rids;
5940 * Listing trusted domains (stored in secrets.tdb, if local)
5943 mem_ctx = talloc_init("trust relationships listing");
5946 * set domain and pdc name to local samba server (default)
5947 * or to remote one given in command line
5950 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5951 domain_name = opt_workgroup;
5952 opt_target_workgroup = opt_workgroup;
5954 fstrcpy(pdc_name, global_myname());
5955 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5956 opt_target_workgroup = domain_name;
5959 /* open \PIPE\lsarpc and open policy handle */
5960 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5961 DEBUG(0, ("Couldn't connect to domain controller\n"));
5965 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5967 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5968 nt_errstr(nt_status) ));
5972 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5974 if (NT_STATUS_IS_ERR(nt_status)) {
5975 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5976 nt_errstr(nt_status)));
5980 /* query info level 5 to obtain sid of a domain being queried */
5981 nt_status = rpccli_lsa_query_info_policy(
5982 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5983 &dummy, &queried_dom_sid);
5985 if (NT_STATUS_IS_ERR(nt_status)) {
5986 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5987 nt_errstr(nt_status)));
5992 * Keep calling LsaEnumTrustdom over opened pipe until
5993 * the end of enumeration is reached
5996 d_printf("Trusted domains list:\n\n");
5999 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6001 &trusted_dom_names, &domain_sids);
6003 if (NT_STATUS_IS_ERR(nt_status)) {
6004 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6005 nt_errstr(nt_status)));
6009 for (i = 0; i < num_domains; i++) {
6010 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6014 * in case of no trusted domains say something rather
6015 * than just display blank line
6017 if (!num_domains) d_printf("none\n");
6019 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6021 /* close this connection before doing next one */
6022 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
6023 if (NT_STATUS_IS_ERR(nt_status)) {
6024 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6025 nt_errstr(nt_status)));
6029 cli_rpc_pipe_close(pipe_hnd);
6032 * Listing trusting domains (stored in passdb backend, if local)
6035 d_printf("\nTrusting domains list:\n\n");
6038 * Open \PIPE\samr and get needed policy handles
6040 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6042 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6047 nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
6049 if (!NT_STATUS_IS_OK(nt_status)) {
6050 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6051 nt_errstr(nt_status)));
6055 /* SamrOpenDomain - we have to open domain policy handle in order to be
6056 able to enumerate accounts*/
6057 nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
6058 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6059 queried_dom_sid, &domain_hnd);
6060 if (!NT_STATUS_IS_OK(nt_status)) {
6061 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6062 nt_errstr(nt_status)));
6067 * perform actual enumeration
6070 enum_ctx = 0; /* reset enumeration context from last enumeration */
6073 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6074 &enum_ctx, ACB_DOMTRUST, 0xffff,
6075 &trusting_dom_names, &trusting_dom_rids,
6077 if (NT_STATUS_IS_ERR(nt_status)) {
6078 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6079 nt_errstr(nt_status)));
6083 for (i = 0; i < num_domains; i++) {
6086 * get each single domain's sid (do we _really_ need this ?):
6087 * 1) connect to domain's pdc
6088 * 2) query the pdc for domain's sid
6091 /* get rid of '$' tail */
6092 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6093 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6094 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6096 /* calculate padding space for d_printf to look nicer */
6097 pad_len = col_len - strlen(trusting_dom_names[i]);
6098 padding[pad_len] = 0;
6099 do padding[--pad_len] = ' '; while (pad_len);
6101 /* set opt_* variables to remote domain */
6102 strupper_m(trusting_dom_names[i]);
6103 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6104 opt_target_workgroup = opt_workgroup;
6106 d_printf("%s%s", trusting_dom_names[i], padding);
6108 /* connect to remote domain controller */
6109 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6111 /* query for domain's sid */
6112 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6113 d_fprintf(stderr, "couldn't get domain's sid\n");
6115 cli_shutdown(remote_cli);
6118 d_fprintf(stderr, "domain controller is not responding\n");
6122 if (!num_domains) d_printf("none\n");
6124 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6126 /* close opened samr and domain policy handles */
6127 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6128 if (!NT_STATUS_IS_OK(nt_status)) {
6129 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6132 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6133 if (!NT_STATUS_IS_OK(nt_status)) {
6134 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6137 /* close samr pipe and connection to IPC$ */
6140 talloc_destroy(mem_ctx);
6145 * Entrypoint for 'net rpc trustdom' code
6147 * @param argc standard argc
6148 * @param argv standard argv without initial components
6150 * @return Integer status (0 means success)
6153 static int rpc_trustdom(int argc, const char **argv)
6155 struct functable func[] = {
6156 {"add", rpc_trustdom_add},
6157 {"del", rpc_trustdom_del},
6158 {"establish", rpc_trustdom_establish},
6159 {"revoke", rpc_trustdom_revoke},
6160 {"help", rpc_trustdom_usage},
6161 {"list", rpc_trustdom_list},
6162 {"vampire", rpc_trustdom_vampire},
6167 rpc_trustdom_usage(argc, argv);
6171 return (net_run_function(argc, argv, func, rpc_user_usage));
6175 * Check if a server will take rpc commands
6176 * @param flags Type of server to connect to (PDC, DMB, localhost)
6177 * if the host is not explicitly specified
6178 * @return BOOL (true means rpc supported)
6180 BOOL net_rpc_check(unsigned flags)
6182 struct cli_state cli;
6184 struct in_addr server_ip;
6185 char *server_name = NULL;
6187 /* flags (i.e. server type) may depend on command */
6188 if (!net_find_server(flags, &server_ip, &server_name))
6192 if (cli_initialise(&cli) == False)
6195 if (!cli_connect(&cli, server_name, &server_ip))
6197 if (!attempt_netbios_session_request(&cli, global_myname(),
6198 server_name, &server_ip))
6200 if (!cli_negprot(&cli))
6202 if (cli.protocol < PROTOCOL_NT1)
6211 /* dump sam database via samsync rpc calls */
6212 static int rpc_samdump(int argc, const char **argv) {
6213 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6217 /* syncronise sam database via samsync rpc calls */
6218 static int rpc_vampire(int argc, const char **argv) {
6219 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6224 * Migrate everything from a print-server
6226 * @param argc Standard main() style argc
6227 * @param argv Standard main() style argv. Initial components are already
6230 * @return A shell status integer (0 for success)
6232 * The order is important !
6233 * To successfully add drivers the print-queues have to exist !
6234 * Applying ACLs should be the last step, because you're easily locked out
6237 static int rpc_printer_migrate_all(int argc, const char **argv)
6242 printf("no server to migrate\n");
6246 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6250 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6254 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6258 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6262 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6267 * Migrate print-drivers from a print-server
6269 * @param argc Standard main() style argc
6270 * @param argv Standard main() style argv. Initial components are already
6273 * @return A shell status integer (0 for success)
6275 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6278 printf("no server to migrate\n");
6282 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6283 rpc_printer_migrate_drivers_internals,
6288 * Migrate print-forms from a print-server
6290 * @param argc Standard main() style argc
6291 * @param argv Standard main() style argv. Initial components are already
6294 * @return A shell status integer (0 for success)
6296 static int rpc_printer_migrate_forms(int argc, const char **argv)
6299 printf("no server to migrate\n");
6303 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6304 rpc_printer_migrate_forms_internals,
6309 * Migrate printers from a print-server
6311 * @param argc Standard main() style argc
6312 * @param argv Standard main() style argv. Initial components are already
6315 * @return A shell status integer (0 for success)
6317 static int rpc_printer_migrate_printers(int argc, const char **argv)
6320 printf("no server to migrate\n");
6324 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6325 rpc_printer_migrate_printers_internals,
6330 * Migrate printer-ACLs from a print-server
6332 * @param argc Standard main() style argc
6333 * @param argv Standard main() style argv. Initial components are already
6336 * @return A shell status integer (0 for success)
6338 static int rpc_printer_migrate_security(int argc, const char **argv)
6341 printf("no server to migrate\n");
6345 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6346 rpc_printer_migrate_security_internals,
6351 * Migrate printer-settings from a print-server
6353 * @param argc Standard main() style argc
6354 * @param argv Standard main() style argv. Initial components are already
6357 * @return A shell status integer (0 for success)
6359 static int rpc_printer_migrate_settings(int argc, const char **argv)
6362 printf("no server to migrate\n");
6366 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6367 rpc_printer_migrate_settings_internals,
6372 * 'net rpc printer' entrypoint.
6373 * @param argc Standard main() style argc
6374 * @param argv Standard main() style argv. Initial components are already
6378 int rpc_printer_migrate(int argc, const char **argv)
6381 /* ouch: when addriver and setdriver are called from within
6382 rpc_printer_migrate_drivers_internals, the printer-queue already
6385 struct functable func[] = {
6386 {"all", rpc_printer_migrate_all},
6387 {"drivers", rpc_printer_migrate_drivers},
6388 {"forms", rpc_printer_migrate_forms},
6389 {"help", rpc_printer_usage},
6390 {"printers", rpc_printer_migrate_printers},
6391 {"security", rpc_printer_migrate_security},
6392 {"settings", rpc_printer_migrate_settings},
6396 return net_run_function(argc, argv, func, rpc_printer_usage);
6401 * List printers on a remote RPC server
6403 * @param argc Standard main() style argc
6404 * @param argv Standard main() style argv. Initial components are already
6407 * @return A shell status integer (0 for success)
6409 static int rpc_printer_list(int argc, const char **argv)
6412 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6413 rpc_printer_list_internals,
6418 * List printer-drivers on a remote RPC server
6420 * @param argc Standard main() style argc
6421 * @param argv Standard main() style argv. Initial components are already
6424 * @return A shell status integer (0 for success)
6426 static int rpc_printer_driver_list(int argc, const char **argv)
6429 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6430 rpc_printer_driver_list_internals,
6435 * Publish printer in ADS via MSRPC
6437 * @param argc Standard main() style argc
6438 * @param argv Standard main() style argv. Initial components are already
6441 * @return A shell status integer (0 for success)
6443 static int rpc_printer_publish_publish(int argc, const char **argv)
6446 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6447 rpc_printer_publish_publish_internals,
6452 * Update printer in ADS via MSRPC
6454 * @param argc Standard main() style argc
6455 * @param argv Standard main() style argv. Initial components are already
6458 * @return A shell status integer (0 for success)
6460 static int rpc_printer_publish_update(int argc, const char **argv)
6463 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6464 rpc_printer_publish_update_internals,
6469 * UnPublish printer in ADS via MSRPC
6471 * @param argc Standard main() style argc
6472 * @param argv Standard main() style argv. Initial components are already
6475 * @return A shell status integer (0 for success)
6477 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6480 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6481 rpc_printer_publish_unpublish_internals,
6486 * List published printers via MSRPC
6488 * @param argc Standard main() style argc
6489 * @param argv Standard main() style argv. Initial components are already
6492 * @return A shell status integer (0 for success)
6494 static int rpc_printer_publish_list(int argc, const char **argv)
6497 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6498 rpc_printer_publish_list_internals,
6504 * Publish printer in ADS
6506 * @param argc Standard main() style argc
6507 * @param argv Standard main() style argv. Initial components are already
6510 * @return A shell status integer (0 for success)
6512 static int rpc_printer_publish(int argc, const char **argv)
6515 struct functable func[] = {
6516 {"publish", rpc_printer_publish_publish},
6517 {"update", rpc_printer_publish_update},
6518 {"unpublish", rpc_printer_publish_unpublish},
6519 {"list", rpc_printer_publish_list},
6520 {"help", rpc_printer_usage},
6525 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6526 rpc_printer_publish_list_internals,
6529 return net_run_function(argc, argv, func, rpc_printer_usage);
6535 * Display rpc printer help page.
6536 * @param argc Standard main() style argc
6537 * @param argv Standard main() style argv. Initial components are already
6540 int rpc_printer_usage(int argc, const char **argv)
6542 return net_help_printer(argc, argv);
6546 * 'net rpc printer' entrypoint.
6547 * @param argc Standard main() style argc
6548 * @param argv Standard main() style argv. Initial components are already
6551 int net_rpc_printer(int argc, const char **argv)
6553 struct functable func[] = {
6554 {"list", rpc_printer_list},
6555 {"migrate", rpc_printer_migrate},
6556 {"driver", rpc_printer_driver_list},
6557 {"publish", rpc_printer_publish},
6562 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6563 rpc_printer_list_internals,
6566 return net_run_function(argc, argv, func, rpc_printer_usage);
6569 /****************************************************************************/
6573 * Basic usage function for 'net rpc'
6574 * @param argc Standard main() style argc
6575 * @param argv Standard main() style argv. Initial components are already
6579 int net_rpc_usage(int argc, const char **argv)
6581 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6582 d_printf(" net rpc join \t\t\tto join a domain \n");
6583 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6584 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6585 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6586 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6587 d_printf(" net rpc group \t\tto list groups\n");
6588 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6589 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6590 d_printf(" net rpc file \t\t\tto list open files\n");
6591 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6592 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6593 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6594 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6595 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6596 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6597 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6598 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6599 d_printf(" net rpc registry\t\tto manage registry hives\n");
6600 d_printf(" net rpc service\t\tto start, stop and query services\n");
6602 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6603 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6604 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6605 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6606 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6612 * Help function for 'net rpc'. Calls command specific help if requested
6613 * or displays usage of net rpc
6614 * @param argc Standard main() style argc
6615 * @param argv Standard main() style argv. Initial components are already
6619 int net_rpc_help(int argc, const char **argv)
6621 struct functable func[] = {
6622 {"join", rpc_join_usage},
6623 {"user", rpc_user_usage},
6624 {"group", rpc_group_usage},
6625 {"share", rpc_share_usage},
6626 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6627 {"trustdom", rpc_trustdom_usage},
6628 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6629 /*{"shutdown", rpc_shutdown_usage}, */
6630 {"vampire", rpc_vampire_usage},
6635 net_rpc_usage(argc, argv);
6639 return (net_run_function(argc, argv, func, rpc_user_usage));
6643 * 'net rpc' entrypoint.
6644 * @param argc Standard main() style argc
6645 * @param argv Standard main() style argv. Initial components are already
6649 int net_rpc(int argc, const char **argv)
6651 struct functable func[] = {
6652 {"audit", net_rpc_audit},
6653 {"info", net_rpc_info},
6654 {"join", net_rpc_join},
6655 {"oldjoin", net_rpc_oldjoin},
6656 {"testjoin", net_rpc_testjoin},
6657 {"user", net_rpc_user},
6658 {"password", rpc_user_password},
6659 {"group", net_rpc_group},
6660 {"share", net_rpc_share},
6661 {"file", net_rpc_file},
6662 {"printer", net_rpc_printer},
6663 {"changetrustpw", net_rpc_changetrustpw},
6664 {"trustdom", rpc_trustdom},
6665 {"abortshutdown", rpc_shutdown_abort},
6666 {"shutdown", rpc_shutdown},
6667 {"samdump", rpc_samdump},
6668 {"vampire", rpc_vampire},
6669 {"getsid", net_rpc_getsid},
6670 {"rights", net_rpc_rights},
6671 {"service", net_rpc_service},
6672 {"registry", net_rpc_registry},
6673 {"shell", net_rpc_shell},
6674 {"help", net_rpc_help},
6677 return net_run_function(argc, argv, func, net_rpc_usage);