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)) {
436 /* Get domain policy handle */
437 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
438 MAXIMUM_ALLOWED_ACCESS,
439 domain_sid, &domain_pol);
440 if (!NT_STATUS_IS_OK(result)) {
445 result = rpccli_samr_query_dom_info(pipe_hnd, mem_ctx, &domain_pol,
447 if (NT_STATUS_IS_OK(result)) {
448 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
449 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
450 d_printf("Domain SID: %s\n", sid_str);
451 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num.low);
452 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
453 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
454 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
463 * 'net rpc info' entrypoint.
464 * @param argc Standard main() style argc
465 * @param argc Standard main() style argv. Initial components are already
469 int net_rpc_info(int argc, const char **argv)
471 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
477 * Fetch domain SID into the local secrets.tdb
479 * All parameters are provided by the run_rpc_command function, except for
480 * argc, argv which are passes through.
482 * @param domain_sid The domain sid acquired from the remote server
483 * @param cli A cli_state connected to the server.
484 * @param mem_ctx Talloc context, destoyed on completion of the function.
485 * @param argc Standard main() style argc
486 * @param argv Standard main() style argv. Initial components are already
489 * @return Normal NTSTATUS return.
492 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
493 const char *domain_name,
494 struct cli_state *cli,
495 struct rpc_pipe_client *pipe_hnd,
502 sid_to_string(sid_str, domain_sid);
503 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
504 sid_str, domain_name);
506 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
507 DEBUG(0,("Can't store domain SID\n"));
508 return NT_STATUS_UNSUCCESSFUL;
515 * 'net rpc getsid' entrypoint.
516 * @param argc Standard main() style argc
517 * @param argc Standard main() style argv. Initial components are already
521 int net_rpc_getsid(int argc, const char **argv)
523 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
524 rpc_getsid_internals,
528 /****************************************************************************/
531 * Basic usage function for 'net rpc user'
532 * @param argc Standard main() style argc.
533 * @param argv Standard main() style argv. Initial components are already
537 static int rpc_user_usage(int argc, const char **argv)
539 return net_help_user(argc, argv);
543 * Add a new user to a remote RPC server
545 * All parameters are provided by the run_rpc_command function, except for
546 * argc, argv which are passes through.
548 * @param domain_sid The domain sid acquired from the remote server
549 * @param cli A cli_state connected to the server.
550 * @param mem_ctx Talloc context, destoyed on completion of the function.
551 * @param argc Standard main() style argc
552 * @param argv Standard main() style argv. Initial components are already
555 * @return Normal NTSTATUS return.
558 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
559 const char *domain_name,
560 struct cli_state *cli,
561 struct rpc_pipe_client *pipe_hnd,
563 int argc, const char **argv)
566 POLICY_HND connect_pol, domain_pol, user_pol;
567 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
568 const char *acct_name;
570 uint32 unknown, user_rid;
573 d_printf("User must be specified\n");
574 rpc_user_usage(argc, argv);
580 /* Get sam policy handle */
582 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
584 if (!NT_STATUS_IS_OK(result)) {
588 /* Get domain policy handle */
590 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
591 MAXIMUM_ALLOWED_ACCESS,
592 domain_sid, &domain_pol);
593 if (!NT_STATUS_IS_OK(result)) {
597 /* Create domain user */
599 acb_info = ACB_NORMAL;
600 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
602 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
603 acct_name, acb_info, unknown,
604 &user_pol, &user_rid);
605 if (!NT_STATUS_IS_OK(result)) {
610 if (!NT_STATUS_IS_OK(result)) {
611 d_fprintf(stderr, "Failed to add user %s - %s\n", acct_name,
614 d_printf("Added user %s\n", acct_name);
620 * Add a new user to a remote RPC server
622 * @param argc Standard main() style argc
623 * @param argv Standard main() style argv. Initial components are already
626 * @return A shell status integer (0 for success)
629 static int rpc_user_add(int argc, const char **argv)
631 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
636 * Delete a user from a remote RPC server
638 * All parameters are provided by the run_rpc_command function, except for
639 * argc, argv which are passes through.
641 * @param domain_sid The domain sid acquired from the remote server
642 * @param cli A cli_state connected to the server.
643 * @param mem_ctx Talloc context, destoyed on completion of the function.
644 * @param argc Standard main() style argc
645 * @param argv Standard main() style argv. Initial components are already
648 * @return Normal NTSTATUS return.
651 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
652 const char *domain_name,
653 struct cli_state *cli,
654 struct rpc_pipe_client *pipe_hnd,
659 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
660 POLICY_HND connect_pol, domain_pol, user_pol;
663 d_printf("User must be specified\n");
664 rpc_user_usage(argc, argv);
667 /* Get sam policy and domain handles */
669 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
672 if (!NT_STATUS_IS_OK(result)) {
676 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
677 MAXIMUM_ALLOWED_ACCESS,
678 domain_sid, &domain_pol);
680 if (!NT_STATUS_IS_OK(result)) {
684 /* Get handle on user */
687 uint32 *user_rids, num_rids, *name_types;
688 uint32 flags = 0x000003e8; /* Unknown */
690 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
692 &num_rids, &user_rids,
695 if (!NT_STATUS_IS_OK(result)) {
699 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
700 MAXIMUM_ALLOWED_ACCESS,
701 user_rids[0], &user_pol);
703 if (!NT_STATUS_IS_OK(result)) {
710 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
712 if (!NT_STATUS_IS_OK(result)) {
716 /* Display results */
717 if (!NT_STATUS_IS_OK(result)) {
718 d_fprintf(stderr, "Failed to delete user account - %s\n", nt_errstr(result));
720 d_printf("Deleted user account\n");
728 * Rename a user on a remote RPC server
730 * All parameters are provided by the run_rpc_command function, except for
731 * argc, argv which are passes through.
733 * @param domain_sid The domain sid acquired from the remote server
734 * @param cli A cli_state connected to the server.
735 * @param mem_ctx Talloc context, destoyed on completion of the function.
736 * @param argc Standard main() style argc
737 * @param argv Standard main() style argv. Initial components are already
740 * @return Normal NTSTATUS return.
743 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
744 const char *domain_name,
745 struct cli_state *cli,
746 struct rpc_pipe_client *pipe_hnd,
751 POLICY_HND connect_pol, domain_pol, user_pol;
752 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
753 uint32 info_level = 7;
754 const char *old_name, *new_name;
756 uint32 flags = 0x000003e8; /* Unknown */
757 uint32 num_rids, *name_types;
758 uint32 num_names = 1;
760 SAM_USERINFO_CTR *user_ctr;
761 SAM_USERINFO_CTR ctr;
762 SAM_USER_INFO_7 info7;
765 d_printf("Old and new username must be specified\n");
766 rpc_user_usage(argc, argv);
774 ZERO_STRUCT(user_ctr);
776 /* Get sam policy handle */
778 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
780 if (!NT_STATUS_IS_OK(result)) {
784 /* Get domain policy handle */
786 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
787 MAXIMUM_ALLOWED_ACCESS,
788 domain_sid, &domain_pol);
789 if (!NT_STATUS_IS_OK(result)) {
793 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
795 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
796 flags, num_names, names,
797 &num_rids, &user_rid, &name_types);
798 if (!NT_STATUS_IS_OK(result)) {
802 /* Open domain user */
803 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
804 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
806 if (!NT_STATUS_IS_OK(result)) {
810 /* Query user info */
811 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
812 info_level, &user_ctr);
814 if (!NT_STATUS_IS_OK(result)) {
818 ctr.switch_value = info_level;
819 ctr.info.id7 = &info7;
821 init_sam_user_info7(&info7, new_name);
824 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
825 info_level, &cli->user_session_key, &ctr);
827 if (!NT_STATUS_IS_OK(result)) {
832 if (!NT_STATUS_IS_OK(result)) {
833 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
836 d_printf("Renamed user from %s to %s\n", old_name, new_name);
842 * Rename a user on a remote RPC server
844 * @param argc Standard main() style argc
845 * @param argv Standard main() style argv. Initial components are already
848 * @return A shell status integer (0 for success)
851 static int rpc_user_rename(int argc, const char **argv)
853 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
858 * Delete a user from a remote RPC server
860 * @param argc Standard main() style argc
861 * @param argv Standard main() style argv. Initial components are already
864 * @return A shell status integer (0 for success)
867 static int rpc_user_delete(int argc, const char **argv)
869 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
874 * Set a password for a user on a remote RPC server
876 * All parameters are provided by the run_rpc_command function, except for
877 * argc, argv which are passes through.
879 * @param domain_sid The domain sid acquired from the remote server
880 * @param cli A cli_state connected to the server.
881 * @param mem_ctx Talloc context, destoyed on completion of the function.
882 * @param argc Standard main() style argc
883 * @param argv Standard main() style argv. Initial components are already
886 * @return Normal NTSTATUS return.
889 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
890 const char *domain_name,
891 struct cli_state *cli,
892 struct rpc_pipe_client *pipe_hnd,
897 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
898 POLICY_HND connect_pol, domain_pol, user_pol;
899 SAM_USERINFO_CTR ctr;
900 SAM_USER_INFO_24 p24;
903 const char *new_password;
907 d_printf("User must be specified\n");
908 rpc_user_usage(argc, argv);
915 new_password = argv[1];
917 asprintf(&prompt, "Enter new password for %s:", user);
918 new_password = getpass(prompt);
922 /* Get sam policy and domain handles */
924 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
927 if (!NT_STATUS_IS_OK(result)) {
931 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
932 MAXIMUM_ALLOWED_ACCESS,
933 domain_sid, &domain_pol);
935 if (!NT_STATUS_IS_OK(result)) {
939 /* Get handle on user */
942 uint32 *user_rids, num_rids, *name_types;
943 uint32 flags = 0x000003e8; /* Unknown */
945 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
947 &num_rids, &user_rids,
950 if (!NT_STATUS_IS_OK(result)) {
954 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
955 MAXIMUM_ALLOWED_ACCESS,
956 user_rids[0], &user_pol);
958 if (!NT_STATUS_IS_OK(result)) {
963 /* Set password on account */
968 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
970 init_sam_user_info24(&p24, (char *)pwbuf,24);
972 ctr.switch_value = 24;
973 ctr.info.id24 = &p24;
975 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
976 &cli->user_session_key, &ctr);
978 if (!NT_STATUS_IS_OK(result)) {
982 /* Display results */
990 * Set a user's password on a remote RPC server
992 * @param argc Standard main() style argc
993 * @param argv Standard main() style argv. Initial components are already
996 * @return A shell status integer (0 for success)
999 static int rpc_user_password(int argc, const char **argv)
1001 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1006 * List user's groups on a remote RPC server
1008 * All parameters are provided by the run_rpc_command function, except for
1009 * argc, argv which are passes through.
1011 * @param domain_sid The domain sid acquired from the remote server
1012 * @param cli A cli_state connected to the server.
1013 * @param mem_ctx Talloc context, destoyed on completion of the function.
1014 * @param argc Standard main() style argc
1015 * @param argv Standard main() style argv. Initial components are already
1018 * @return Normal NTSTATUS return.
1021 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1022 const char *domain_name,
1023 struct cli_state *cli,
1024 struct rpc_pipe_client *pipe_hnd,
1025 TALLOC_CTX *mem_ctx,
1029 POLICY_HND connect_pol, domain_pol, user_pol;
1030 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1031 uint32 *rids, num_rids, *name_types, num_names;
1032 uint32 flags = 0x000003e8; /* Unknown */
1038 d_printf("User must be specified\n");
1039 rpc_user_usage(argc, argv);
1040 return NT_STATUS_OK;
1042 /* Get sam policy handle */
1044 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1046 if (!NT_STATUS_IS_OK(result)) goto done;
1048 /* Get domain policy handle */
1050 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1051 MAXIMUM_ALLOWED_ACCESS,
1052 domain_sid, &domain_pol);
1053 if (!NT_STATUS_IS_OK(result)) goto done;
1055 /* Get handle on user */
1057 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1059 &num_rids, &rids, &name_types);
1061 if (!NT_STATUS_IS_OK(result)) goto done;
1063 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1064 MAXIMUM_ALLOWED_ACCESS,
1065 rids[0], &user_pol);
1066 if (!NT_STATUS_IS_OK(result)) goto done;
1068 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1069 &num_rids, &user_gids);
1071 if (!NT_STATUS_IS_OK(result)) goto done;
1076 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1078 for (i = 0; i < num_rids; i++)
1079 rids[i] = user_gids[i].g_rid;
1081 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1083 &num_names, &names, &name_types);
1085 if (!NT_STATUS_IS_OK(result)) {
1089 /* Display results */
1091 for (i = 0; i < num_names; i++)
1092 printf("%s\n", names[i]);
1099 * List a user's groups from a remote RPC server
1101 * @param argc Standard main() style argc
1102 * @param argv Standard main() style argv. Initial components are already
1105 * @return A shell status integer (0 for success)
1108 static int rpc_user_info(int argc, const char **argv)
1110 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1115 * List users on a remote RPC server
1117 * All parameters are provided by the run_rpc_command function, except for
1118 * argc, argv which are passes through.
1120 * @param domain_sid The domain sid acquired from the remote server
1121 * @param cli A cli_state connected to the server.
1122 * @param mem_ctx Talloc context, destoyed on completion of the function.
1123 * @param argc Standard main() style argc
1124 * @param argv Standard main() style argv. Initial components are already
1127 * @return Normal NTSTATUS return.
1130 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1131 const char *domain_name,
1132 struct cli_state *cli,
1133 struct rpc_pipe_client *pipe_hnd,
1134 TALLOC_CTX *mem_ctx,
1138 POLICY_HND connect_pol, domain_pol;
1139 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1140 uint32 start_idx=0, num_entries, i, loop_count = 0;
1141 SAM_DISPINFO_CTR ctr;
1142 SAM_DISPINFO_1 info1;
1144 /* Get sam policy handle */
1146 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1148 if (!NT_STATUS_IS_OK(result)) {
1152 /* Get domain policy handle */
1154 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1155 MAXIMUM_ALLOWED_ACCESS,
1156 domain_sid, &domain_pol);
1157 if (!NT_STATUS_IS_OK(result)) {
1161 /* Query domain users */
1164 ctr.sam.info1 = &info1;
1165 if (opt_long_list_entries)
1166 d_printf("\nUser name Comment"\
1167 "\n-----------------------------\n");
1170 uint32 max_entries, max_size;
1172 get_query_dispinfo_params(
1173 loop_count, &max_entries, &max_size);
1175 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1176 &start_idx, 1, &num_entries,
1177 max_entries, max_size, &ctr);
1180 for (i = 0; i < num_entries; i++) {
1181 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1182 if (opt_long_list_entries)
1183 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1185 if (opt_long_list_entries)
1186 printf("%-21.21s %s\n", user, desc);
1188 printf("%s\n", user);
1190 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1197 * 'net rpc user' entrypoint.
1198 * @param argc Standard main() style argc
1199 * @param argc Standard main() style argv. Initial components are already
1203 int net_rpc_user(int argc, const char **argv)
1205 struct functable func[] = {
1206 {"add", rpc_user_add},
1207 {"info", rpc_user_info},
1208 {"delete", rpc_user_delete},
1209 {"password", rpc_user_password},
1210 {"rename", rpc_user_rename},
1215 return run_rpc_command(NULL,PI_SAMR, 0,
1216 rpc_user_list_internals,
1220 return net_run_function(argc, argv, func, rpc_user_usage);
1223 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1224 struct rpc_sh_ctx *ctx,
1225 struct rpc_pipe_client *pipe_hnd,
1226 int argc, const char **argv)
1228 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1229 ctx->cli, pipe_hnd, mem_ctx,
1233 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1234 struct rpc_sh_ctx *ctx,
1235 struct rpc_pipe_client *pipe_hnd,
1236 int argc, const char **argv)
1238 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1239 ctx->cli, pipe_hnd, mem_ctx,
1243 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1244 struct rpc_sh_ctx *ctx,
1245 struct rpc_pipe_client *pipe_hnd,
1246 int argc, const char **argv,
1248 TALLOC_CTX *mem_ctx,
1249 struct rpc_sh_ctx *ctx,
1250 struct rpc_pipe_client *pipe_hnd,
1251 const POLICY_HND *user_hnd,
1252 int argc, const char **argv))
1255 POLICY_HND connect_pol, domain_pol, user_pol;
1256 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1259 enum SID_NAME_USE type;
1262 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1263 return NT_STATUS_INVALID_PARAMETER;
1266 ZERO_STRUCT(connect_pol);
1267 ZERO_STRUCT(domain_pol);
1268 ZERO_STRUCT(user_pol);
1270 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1271 NULL, NULL, &sid, &type);
1272 if (!NT_STATUS_IS_OK(result)) {
1273 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1278 if (type != SID_NAME_USER) {
1279 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1280 sid_type_lookup(type));
1281 result = NT_STATUS_NO_SUCH_USER;
1285 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1286 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1287 result = NT_STATUS_NO_SUCH_USER;
1291 result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1292 MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1293 if (!NT_STATUS_IS_OK(result)) {
1297 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1298 MAXIMUM_ALLOWED_ACCESS,
1299 ctx->domain_sid, &domain_pol);
1300 if (!NT_STATUS_IS_OK(result)) {
1304 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1305 MAXIMUM_ALLOWED_ACCESS,
1307 if (!NT_STATUS_IS_OK(result)) {
1311 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1314 if (is_valid_policy_hnd(&user_pol)) {
1315 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1317 if (is_valid_policy_hnd(&domain_pol)) {
1318 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1320 if (is_valid_policy_hnd(&connect_pol)) {
1321 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1326 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1327 struct rpc_sh_ctx *ctx,
1328 struct rpc_pipe_client *pipe_hnd,
1329 const POLICY_HND *user_hnd,
1330 int argc, const char **argv)
1333 SAM_USERINFO_CTR *ctr;
1334 SAM_USER_INFO_21 *info;
1337 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1338 return NT_STATUS_INVALID_PARAMETER;
1341 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1343 if (!NT_STATUS_IS_OK(result)) {
1347 info = ctr->info.id21;
1349 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1355 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1356 struct rpc_sh_ctx *ctx,
1357 struct rpc_pipe_client *pipe_hnd,
1358 int argc, const char **argv)
1360 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1361 rpc_sh_user_show_internals);
1364 #define FETCHSTR(name, rec) \
1365 do { if (strequal(ctx->thiscmd, name)) { \
1366 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1369 #define SETSTR(name, rec, flag) \
1370 do { if (strequal(ctx->thiscmd, name)) { \
1371 init_unistr2(&usr->uni_##rec, argv[0], STR_TERMINATE); \
1372 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1373 usr->fields_present |= ACCT_##flag; } \
1376 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1377 struct rpc_sh_ctx *ctx,
1378 struct rpc_pipe_client *pipe_hnd,
1379 const POLICY_HND *user_hnd,
1380 int argc, const char **argv)
1383 SAM_USERINFO_CTR *ctr;
1384 SAM_USER_INFO_21 *usr;
1385 const char *username;
1386 const char *oldval = "";
1389 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1391 return NT_STATUS_INVALID_PARAMETER;
1394 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1396 if (!NT_STATUS_IS_OK(result)) {
1400 usr = ctr->info.id21;
1402 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1404 FETCHSTR("fullname", full_name);
1405 FETCHSTR("homedir", home_dir);
1406 FETCHSTR("homedrive", dir_drive);
1407 FETCHSTR("logonscript", logon_script);
1408 FETCHSTR("profilepath", profile_path);
1409 FETCHSTR("description", acct_desc);
1412 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1418 if (strcmp(argv[0], "NULL") == 0) {
1422 SETSTR("fullname", full_name, FULL_NAME);
1423 SETSTR("homedir", home_dir, HOME_DIR);
1424 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1425 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1426 SETSTR("profilepath", profile_path, PROFILE);
1427 SETSTR("description", acct_desc, DESCRIPTION);
1429 result = rpccli_samr_set_userinfo2(
1430 pipe_hnd, mem_ctx, user_hnd, 21,
1431 &pipe_hnd->cli->user_session_key, ctr);
1433 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1434 ctx->thiscmd, oldval, argv[0]);
1441 #define HANDLEFLG(name, rec) \
1442 do { if (strequal(ctx->thiscmd, name)) { \
1443 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1445 newflags = oldflags | ACB_##rec; \
1447 newflags = oldflags & ~ACB_##rec; \
1450 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1451 struct rpc_sh_ctx *ctx,
1452 struct rpc_pipe_client *pipe_hnd,
1453 int argc, const char **argv)
1455 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1456 rpc_sh_user_str_edit_internals);
1459 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1460 struct rpc_sh_ctx *ctx,
1461 struct rpc_pipe_client *pipe_hnd,
1462 const POLICY_HND *user_hnd,
1463 int argc, const char **argv)
1466 SAM_USERINFO_CTR *ctr;
1467 SAM_USER_INFO_21 *usr;
1468 const char *username;
1469 const char *oldval = "unknown";
1470 uint32 oldflags, newflags;
1474 ((argc == 1) && !strequal(argv[0], "yes") &&
1475 !strequal(argv[0], "no"))) {
1476 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1478 return NT_STATUS_INVALID_PARAMETER;
1481 newval = strequal(argv[0], "yes");
1483 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1485 if (!NT_STATUS_IS_OK(result)) {
1489 usr = ctr->info.id21;
1491 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1492 oldflags = usr->acb_info;
1493 newflags = usr->acb_info;
1495 HANDLEFLG("disabled", DISABLED);
1496 HANDLEFLG("pwnotreq", PWNOTREQ);
1497 HANDLEFLG("autolock", AUTOLOCK);
1498 HANDLEFLG("pwnoexp", PWNOEXP);
1501 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1507 usr->acb_info = newflags;
1508 usr->fields_present = ACCT_FLAGS;
1510 result = rpccli_samr_set_userinfo2(
1511 pipe_hnd, mem_ctx, user_hnd, 21,
1512 &pipe_hnd->cli->user_session_key, ctr);
1514 if (NT_STATUS_IS_OK(result)) {
1515 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1516 ctx->thiscmd, oldval, argv[0]);
1524 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1525 struct rpc_sh_ctx *ctx,
1526 struct rpc_pipe_client *pipe_hnd,
1527 int argc, const char **argv)
1529 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1530 rpc_sh_user_flag_edit_internals);
1533 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1534 struct rpc_sh_ctx *ctx)
1536 static struct rpc_sh_cmd cmds[] = {
1538 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1539 "Show/Set a user's full name" },
1541 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1542 "Show/Set a user's home directory" },
1544 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1545 "Show/Set a user's home drive" },
1547 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1548 "Show/Set a user's logon script" },
1550 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1551 "Show/Set a user's profile path" },
1553 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1554 "Show/Set a user's description" },
1556 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1557 "Show/Set whether a user is disabled" },
1559 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1560 "Show/Set whether a user locked out" },
1562 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1563 "Show/Set whether a user does not need a password" },
1565 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1566 "Show/Set whether a user's password does not expire" },
1568 { NULL, NULL, 0, NULL, NULL }
1574 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1575 struct rpc_sh_ctx *ctx)
1577 static struct rpc_sh_cmd cmds[] = {
1579 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1580 "List available users" },
1582 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1583 "List the domain groups a user is member of" },
1585 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1586 "Show info about a user" },
1588 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1589 "Show/Modify a user's fields" },
1591 { NULL, NULL, 0, NULL, NULL }
1597 /****************************************************************************/
1600 * Basic usage function for 'net rpc group'
1601 * @param argc Standard main() style argc.
1602 * @param argv Standard main() style argv. Initial components are already
1606 static int rpc_group_usage(int argc, const char **argv)
1608 return net_help_group(argc, argv);
1612 * Delete group on a remote RPC server
1614 * All parameters are provided by the run_rpc_command function, except for
1615 * argc, argv which are passes through.
1617 * @param domain_sid The domain sid acquired from the remote server
1618 * @param cli A cli_state connected to the server.
1619 * @param mem_ctx Talloc context, destoyed on completion of the function.
1620 * @param argc Standard main() style argc
1621 * @param argv Standard main() style argv. Initial components are already
1624 * @return Normal NTSTATUS return.
1627 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1628 const char *domain_name,
1629 struct cli_state *cli,
1630 struct rpc_pipe_client *pipe_hnd,
1631 TALLOC_CTX *mem_ctx,
1635 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1636 BOOL group_is_primary = False;
1637 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1639 uint32 *group_rids, num_rids, *name_types, num_members,
1640 *group_attrs, group_rid;
1641 uint32 flags = 0x000003e8; /* Unknown */
1644 /* DOM_GID *user_gids; */
1645 SAM_USERINFO_CTR *user_ctr;
1649 d_printf("specify group\n");
1650 rpc_group_usage(argc,argv);
1651 return NT_STATUS_OK; /* ok? */
1654 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1657 if (!NT_STATUS_IS_OK(result)) {
1658 d_fprintf(stderr, "Request samr_connect failed\n");
1662 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1663 MAXIMUM_ALLOWED_ACCESS,
1664 domain_sid, &domain_pol);
1666 if (!NT_STATUS_IS_OK(result)) {
1667 d_fprintf(stderr, "Request open_domain failed\n");
1671 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1673 &num_rids, &group_rids,
1676 if (!NT_STATUS_IS_OK(result)) {
1677 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1681 switch (name_types[0])
1683 case SID_NAME_DOM_GRP:
1684 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1685 MAXIMUM_ALLOWED_ACCESS,
1686 group_rids[0], &group_pol);
1687 if (!NT_STATUS_IS_OK(result)) {
1688 d_fprintf(stderr, "Request open_group failed");
1692 group_rid = group_rids[0];
1694 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1695 &num_members, &group_rids,
1698 if (!NT_STATUS_IS_OK(result)) {
1699 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1704 d_printf("Domain Group %s (rid: %d) has %d members\n",
1705 argv[0],group_rid,num_members);
1708 /* Check if group is anyone's primary group */
1709 for (i = 0; i < num_members; i++)
1711 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1712 MAXIMUM_ALLOWED_ACCESS,
1713 group_rids[i], &user_pol);
1715 if (!NT_STATUS_IS_OK(result)) {
1716 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1720 ZERO_STRUCT(user_ctr);
1722 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1725 if (!NT_STATUS_IS_OK(result)) {
1726 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1730 if (user_ctr->info.id21->group_rid == group_rid) {
1731 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1734 d_printf("Group is primary group of %s\n",temp);
1735 group_is_primary = True;
1738 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1741 if (group_is_primary) {
1742 d_fprintf(stderr, "Unable to delete group because some "
1743 "of it's members have it as primary group\n");
1744 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1748 /* remove all group members */
1749 for (i = 0; i < num_members; i++)
1752 d_printf("Remove group member %d...",group_rids[i]);
1753 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1755 if (NT_STATUS_IS_OK(result)) {
1760 d_printf("failed\n");
1765 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1768 /* removing a local group is easier... */
1769 case SID_NAME_ALIAS:
1770 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1771 MAXIMUM_ALLOWED_ACCESS,
1772 group_rids[0], &group_pol);
1774 if (!NT_STATUS_IS_OK(result)) {
1775 d_fprintf(stderr, "Request open_alias failed\n");
1779 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1782 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1783 argv[0],sid_type_lookup(name_types[0]));
1784 result = NT_STATUS_UNSUCCESSFUL;
1789 if (NT_STATUS_IS_OK(result)) {
1791 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1793 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1794 get_friendly_nt_error_msg(result));
1802 static int rpc_group_delete(int argc, const char **argv)
1804 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1808 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1809 const char *domain_name,
1810 struct cli_state *cli,
1811 struct rpc_pipe_client *pipe_hnd,
1812 TALLOC_CTX *mem_ctx,
1816 POLICY_HND connect_pol, domain_pol, group_pol;
1817 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1818 GROUP_INFO_CTR group_info;
1821 d_printf("Group name must be specified\n");
1822 rpc_group_usage(argc, argv);
1823 return NT_STATUS_OK;
1826 /* Get sam policy handle */
1828 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1830 if (!NT_STATUS_IS_OK(result)) goto done;
1832 /* Get domain policy handle */
1834 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1835 MAXIMUM_ALLOWED_ACCESS,
1836 domain_sid, &domain_pol);
1837 if (!NT_STATUS_IS_OK(result)) goto done;
1839 /* Create the group */
1841 result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1842 argv[0], MAXIMUM_ALLOWED_ACCESS,
1844 if (!NT_STATUS_IS_OK(result)) goto done;
1846 if (strlen(opt_comment) == 0) goto done;
1848 /* We've got a comment to set */
1850 group_info.switch_value1 = 4;
1851 init_samr_group_info4(&group_info.group.info4, opt_comment);
1853 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1854 if (!NT_STATUS_IS_OK(result)) goto done;
1857 if (NT_STATUS_IS_OK(result))
1858 DEBUG(5, ("add group succeeded\n"));
1860 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1865 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1866 const char *domain_name,
1867 struct cli_state *cli,
1868 struct rpc_pipe_client *pipe_hnd,
1869 TALLOC_CTX *mem_ctx,
1873 POLICY_HND connect_pol, domain_pol, alias_pol;
1874 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1875 ALIAS_INFO_CTR alias_info;
1878 d_printf("Alias name must be specified\n");
1879 rpc_group_usage(argc, argv);
1880 return NT_STATUS_OK;
1883 /* Get sam policy handle */
1885 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1887 if (!NT_STATUS_IS_OK(result)) goto done;
1889 /* Get domain policy handle */
1891 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1892 MAXIMUM_ALLOWED_ACCESS,
1893 domain_sid, &domain_pol);
1894 if (!NT_STATUS_IS_OK(result)) goto done;
1896 /* Create the group */
1898 result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1899 argv[0], &alias_pol);
1900 if (!NT_STATUS_IS_OK(result)) goto done;
1902 if (strlen(opt_comment) == 0) goto done;
1904 /* We've got a comment to set */
1906 alias_info.level = 3;
1907 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1909 result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1910 if (!NT_STATUS_IS_OK(result)) goto done;
1913 if (NT_STATUS_IS_OK(result))
1914 DEBUG(5, ("add alias succeeded\n"));
1916 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1921 static int rpc_group_add(int argc, const char **argv)
1924 return run_rpc_command(NULL, PI_SAMR, 0,
1925 rpc_alias_add_internals,
1928 return run_rpc_command(NULL, PI_SAMR, 0,
1929 rpc_group_add_internals,
1933 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1934 TALLOC_CTX *mem_ctx,
1937 enum SID_NAME_USE *type)
1939 DOM_SID *sids = NULL;
1940 uint32 *types = NULL;
1941 struct rpc_pipe_client *pipe_hnd;
1943 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1945 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1950 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
1951 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1953 if (!NT_STATUS_IS_OK(result)) {
1957 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1958 &name, NULL, &sids, &types);
1960 if (NT_STATUS_IS_OK(result)) {
1961 sid_copy(sid, &sids[0]);
1965 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
1969 cli_rpc_pipe_close(pipe_hnd);
1972 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1974 /* Try as S-1-5-whatever */
1978 if (string_to_sid(&tmp_sid, name)) {
1979 sid_copy(sid, &tmp_sid);
1980 *type = SID_NAME_UNKNOWN;
1981 result = NT_STATUS_OK;
1988 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1989 TALLOC_CTX *mem_ctx,
1990 const DOM_SID *group_sid,
1993 POLICY_HND connect_pol, domain_pol;
1996 POLICY_HND group_pol;
1999 uint32 *rids = NULL;
2000 uint32 *rid_types = NULL;
2004 sid_copy(&sid, group_sid);
2006 if (!sid_split_rid(&sid, &group_rid)) {
2007 return NT_STATUS_UNSUCCESSFUL;
2010 /* Get sam policy handle */
2011 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2013 if (!NT_STATUS_IS_OK(result)) {
2017 /* Get domain policy handle */
2018 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2019 MAXIMUM_ALLOWED_ACCESS,
2021 if (!NT_STATUS_IS_OK(result)) {
2025 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2027 &num_rids, &rids, &rid_types);
2029 if (!NT_STATUS_IS_OK(result)) {
2030 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2034 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2035 MAXIMUM_ALLOWED_ACCESS,
2036 group_rid, &group_pol);
2038 if (!NT_STATUS_IS_OK(result)) {
2042 result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2045 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2049 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2050 TALLOC_CTX *mem_ctx,
2051 const DOM_SID *alias_sid,
2054 POLICY_HND connect_pol, domain_pol;
2057 POLICY_HND alias_pol;
2060 enum SID_NAME_USE member_type;
2064 sid_copy(&sid, alias_sid);
2066 if (!sid_split_rid(&sid, &alias_rid)) {
2067 return NT_STATUS_UNSUCCESSFUL;
2070 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2071 &member_sid, &member_type);
2073 if (!NT_STATUS_IS_OK(result)) {
2074 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2078 /* Get sam policy handle */
2079 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2081 if (!NT_STATUS_IS_OK(result)) {
2085 /* Get domain policy handle */
2086 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2087 MAXIMUM_ALLOWED_ACCESS,
2089 if (!NT_STATUS_IS_OK(result)) {
2093 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2094 MAXIMUM_ALLOWED_ACCESS,
2095 alias_rid, &alias_pol);
2097 if (!NT_STATUS_IS_OK(result)) {
2101 result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2103 if (!NT_STATUS_IS_OK(result)) {
2108 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2112 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2113 const char *domain_name,
2114 struct cli_state *cli,
2115 struct rpc_pipe_client *pipe_hnd,
2116 TALLOC_CTX *mem_ctx,
2121 enum SID_NAME_USE group_type;
2124 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2125 return NT_STATUS_UNSUCCESSFUL;
2128 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2129 &group_sid, &group_type))) {
2130 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2131 return NT_STATUS_UNSUCCESSFUL;
2134 if (group_type == SID_NAME_DOM_GRP) {
2135 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2136 &group_sid, argv[1]);
2138 if (!NT_STATUS_IS_OK(result)) {
2139 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2140 argv[1], argv[0], nt_errstr(result));
2145 if (group_type == SID_NAME_ALIAS) {
2146 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2147 &group_sid, argv[1]);
2149 if (!NT_STATUS_IS_OK(result)) {
2150 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2151 argv[1], argv[0], nt_errstr(result));
2156 d_fprintf(stderr, "Can only add members to global or local groups "
2157 "which %s is not\n", argv[0]);
2159 return NT_STATUS_UNSUCCESSFUL;
2162 static int rpc_group_addmem(int argc, const char **argv)
2164 return run_rpc_command(NULL, PI_SAMR, 0,
2165 rpc_group_addmem_internals,
2169 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2170 TALLOC_CTX *mem_ctx,
2171 const DOM_SID *group_sid,
2174 POLICY_HND connect_pol, domain_pol;
2177 POLICY_HND group_pol;
2180 uint32 *rids = NULL;
2181 uint32 *rid_types = NULL;
2185 sid_copy(&sid, group_sid);
2187 if (!sid_split_rid(&sid, &group_rid))
2188 return NT_STATUS_UNSUCCESSFUL;
2190 /* Get sam policy handle */
2191 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2193 if (!NT_STATUS_IS_OK(result))
2196 /* Get domain policy handle */
2197 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2198 MAXIMUM_ALLOWED_ACCESS,
2200 if (!NT_STATUS_IS_OK(result))
2203 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2205 &num_rids, &rids, &rid_types);
2207 if (!NT_STATUS_IS_OK(result)) {
2208 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2212 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2213 MAXIMUM_ALLOWED_ACCESS,
2214 group_rid, &group_pol);
2216 if (!NT_STATUS_IS_OK(result))
2219 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2222 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2226 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2227 TALLOC_CTX *mem_ctx,
2228 const DOM_SID *alias_sid,
2231 POLICY_HND connect_pol, domain_pol;
2234 POLICY_HND alias_pol;
2237 enum SID_NAME_USE member_type;
2241 sid_copy(&sid, alias_sid);
2243 if (!sid_split_rid(&sid, &alias_rid))
2244 return NT_STATUS_UNSUCCESSFUL;
2246 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2247 &member_sid, &member_type);
2249 if (!NT_STATUS_IS_OK(result)) {
2250 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2254 /* Get sam policy handle */
2255 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2257 if (!NT_STATUS_IS_OK(result)) {
2261 /* Get domain policy handle */
2262 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2263 MAXIMUM_ALLOWED_ACCESS,
2265 if (!NT_STATUS_IS_OK(result)) {
2269 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2270 MAXIMUM_ALLOWED_ACCESS,
2271 alias_rid, &alias_pol);
2273 if (!NT_STATUS_IS_OK(result))
2276 result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2278 if (!NT_STATUS_IS_OK(result))
2282 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2286 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2287 const char *domain_name,
2288 struct cli_state *cli,
2289 struct rpc_pipe_client *pipe_hnd,
2290 TALLOC_CTX *mem_ctx,
2295 enum SID_NAME_USE group_type;
2298 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2299 return NT_STATUS_UNSUCCESSFUL;
2302 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2303 &group_sid, &group_type))) {
2304 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2305 return NT_STATUS_UNSUCCESSFUL;
2308 if (group_type == SID_NAME_DOM_GRP) {
2309 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2310 &group_sid, argv[1]);
2312 if (!NT_STATUS_IS_OK(result)) {
2313 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2314 argv[1], argv[0], nt_errstr(result));
2319 if (group_type == SID_NAME_ALIAS) {
2320 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2321 &group_sid, argv[1]);
2323 if (!NT_STATUS_IS_OK(result)) {
2324 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2325 argv[1], argv[0], nt_errstr(result));
2330 d_fprintf(stderr, "Can only delete members from global or local groups "
2331 "which %s is not\n", argv[0]);
2333 return NT_STATUS_UNSUCCESSFUL;
2336 static int rpc_group_delmem(int argc, const char **argv)
2338 return run_rpc_command(NULL, PI_SAMR, 0,
2339 rpc_group_delmem_internals,
2344 * List groups on a remote RPC server
2346 * All parameters are provided by the run_rpc_command function, except for
2347 * argc, argv which are passes through.
2349 * @param domain_sid The domain sid acquired from the remote server
2350 * @param cli A cli_state connected to the server.
2351 * @param mem_ctx Talloc context, destoyed on completion of the function.
2352 * @param argc Standard main() style argc
2353 * @param argv Standard main() style argv. Initial components are already
2356 * @return Normal NTSTATUS return.
2359 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2360 const char *domain_name,
2361 struct cli_state *cli,
2362 struct rpc_pipe_client *pipe_hnd,
2363 TALLOC_CTX *mem_ctx,
2367 POLICY_HND connect_pol, domain_pol;
2368 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2369 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2370 struct acct_info *groups;
2371 BOOL global = False;
2373 BOOL builtin = False;
2381 for (i=0; i<argc; i++) {
2382 if (strequal(argv[i], "global"))
2385 if (strequal(argv[i], "local"))
2388 if (strequal(argv[i], "builtin"))
2392 /* Get sam policy handle */
2394 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2396 if (!NT_STATUS_IS_OK(result)) {
2400 /* Get domain policy handle */
2402 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2403 MAXIMUM_ALLOWED_ACCESS,
2404 domain_sid, &domain_pol);
2405 if (!NT_STATUS_IS_OK(result)) {
2409 /* Query domain groups */
2410 if (opt_long_list_entries)
2411 d_printf("\nGroup name Comment"\
2412 "\n-----------------------------\n");
2414 SAM_DISPINFO_CTR ctr;
2415 SAM_DISPINFO_3 info3;
2420 ctr.sam.info3 = &info3;
2424 get_query_dispinfo_params(
2425 loop_count, &max_entries, &max_size);
2427 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2428 &start_idx, 3, &num_entries,
2429 max_entries, max_size, &ctr);
2431 if (!NT_STATUS_IS_OK(result) &&
2432 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2435 for (i = 0; i < num_entries; i++) {
2437 fstring group, desc;
2439 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2440 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2442 if (opt_long_list_entries)
2443 printf("%-21.21s %-50.50s\n",
2446 printf("%s\n", group);
2448 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2449 /* query domain aliases */
2454 /* The max_size field in cli_samr_enum_als_groups is more like
2455 * an account_control field with indiviual bits what to
2456 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2457 * everything. I'm too lazy (sorry) to get this through to
2458 * rpc_parse/ etc. Volker */
2460 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2462 &groups, &num_entries);
2464 if (!NT_STATUS_IS_OK(result) &&
2465 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2468 for (i = 0; i < num_entries; i++) {
2470 char *description = NULL;
2472 if (opt_long_list_entries) {
2474 POLICY_HND alias_pol;
2477 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2482 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2485 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2487 description = unistr2_tdup(mem_ctx,
2488 ctr.alias.info3.description.string);
2492 if (description != NULL) {
2493 printf("%-21.21s %-50.50s\n",
2494 groups[i].acct_name,
2497 printf("%s\n", groups[i].acct_name);
2500 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2501 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2502 /* Get builtin policy handle */
2504 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2505 MAXIMUM_ALLOWED_ACCESS,
2506 &global_sid_Builtin, &domain_pol);
2507 if (!NT_STATUS_IS_OK(result)) {
2510 /* query builtin aliases */
2513 if (!builtin) break;
2515 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2516 &start_idx, max_entries,
2517 &groups, &num_entries);
2519 if (!NT_STATUS_IS_OK(result) &&
2520 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2523 for (i = 0; i < num_entries; i++) {
2525 char *description = NULL;
2527 if (opt_long_list_entries) {
2529 POLICY_HND alias_pol;
2532 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2537 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2540 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2542 description = unistr2_tdup(mem_ctx,
2543 ctr.alias.info3.description.string);
2547 if (description != NULL) {
2548 printf("%-21.21s %-50.50s\n",
2549 groups[i].acct_name,
2552 printf("%s\n", groups[i].acct_name);
2555 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2561 static int rpc_group_list(int argc, const char **argv)
2563 return run_rpc_command(NULL, PI_SAMR, 0,
2564 rpc_group_list_internals,
2568 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2569 TALLOC_CTX *mem_ctx,
2570 const char *domain_name,
2571 const DOM_SID *domain_sid,
2572 POLICY_HND *domain_pol,
2576 POLICY_HND group_pol;
2577 uint32 num_members, *group_rids, *group_attrs;
2584 sid_to_string(sid_str, domain_sid);
2586 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2587 MAXIMUM_ALLOWED_ACCESS,
2590 if (!NT_STATUS_IS_OK(result))
2593 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2594 &num_members, &group_rids,
2597 if (!NT_STATUS_IS_OK(result))
2600 while (num_members > 0) {
2601 int this_time = 512;
2603 if (num_members < this_time)
2604 this_time = num_members;
2606 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2607 this_time, group_rids,
2608 &num_names, &names, &name_types);
2610 if (!NT_STATUS_IS_OK(result))
2613 /* We only have users as members, but make the output
2614 the same as the output of alias members */
2616 for (i = 0; i < this_time; i++) {
2618 if (opt_long_list_entries) {
2619 printf("%s-%d %s\\%s %d\n", sid_str,
2620 group_rids[i], domain_name, names[i],
2623 printf("%s\\%s\n", domain_name, names[i]);
2627 num_members -= this_time;
2631 return NT_STATUS_OK;
2634 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2635 TALLOC_CTX *mem_ctx,
2636 POLICY_HND *domain_pol,
2640 struct rpc_pipe_client *lsa_pipe;
2641 POLICY_HND alias_pol, lsa_pol;
2643 DOM_SID *alias_sids;
2649 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2650 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2652 if (!NT_STATUS_IS_OK(result))
2655 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2656 &num_members, &alias_sids);
2658 if (!NT_STATUS_IS_OK(result)) {
2659 d_fprintf(stderr, "Couldn't list alias members\n");
2663 if (num_members == 0) {
2664 return NT_STATUS_OK;
2667 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2669 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2670 nt_errstr(result) );
2674 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2675 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2677 if (!NT_STATUS_IS_OK(result)) {
2678 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2679 cli_rpc_pipe_close(lsa_pipe);
2683 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2685 &domains, &names, &types);
2687 if (!NT_STATUS_IS_OK(result) &&
2688 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2689 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2690 cli_rpc_pipe_close(lsa_pipe);
2694 for (i = 0; i < num_members; i++) {
2696 sid_to_string(sid_str, &alias_sids[i]);
2698 if (opt_long_list_entries) {
2699 printf("%s %s\\%s %d\n", sid_str,
2700 domains[i] ? domains[i] : "*unknown*",
2701 names[i] ? names[i] : "*unknown*", types[i]);
2704 printf("%s\\%s\n", domains[i], names[i]);
2706 printf("%s\n", sid_str);
2710 cli_rpc_pipe_close(lsa_pipe);
2711 return NT_STATUS_OK;
2714 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2715 const char *domain_name,
2716 struct cli_state *cli,
2717 struct rpc_pipe_client *pipe_hnd,
2718 TALLOC_CTX *mem_ctx,
2723 POLICY_HND connect_pol, domain_pol;
2724 uint32 num_rids, *rids, *rid_types;
2726 /* Get sam policy handle */
2728 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2731 if (!NT_STATUS_IS_OK(result))
2734 /* Get domain policy handle */
2736 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2737 MAXIMUM_ALLOWED_ACCESS,
2738 domain_sid, &domain_pol);
2740 if (!NT_STATUS_IS_OK(result))
2743 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2744 1, argv, &num_rids, &rids, &rid_types);
2746 if (!NT_STATUS_IS_OK(result)) {
2748 /* Ok, did not find it in the global sam, try with builtin */
2750 DOM_SID sid_Builtin;
2752 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2754 string_to_sid(&sid_Builtin, "S-1-5-32");
2756 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2757 MAXIMUM_ALLOWED_ACCESS,
2758 &sid_Builtin, &domain_pol);
2760 if (!NT_STATUS_IS_OK(result)) {
2761 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2765 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2769 if (!NT_STATUS_IS_OK(result)) {
2770 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2775 if (num_rids != 1) {
2776 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2780 if (rid_types[0] == SID_NAME_DOM_GRP) {
2781 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2782 domain_sid, &domain_pol,
2786 if (rid_types[0] == SID_NAME_ALIAS) {
2787 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2791 return NT_STATUS_NO_SUCH_GROUP;
2794 static int rpc_group_members(int argc, const char **argv)
2797 return rpc_group_usage(argc, argv);
2800 return run_rpc_command(NULL, PI_SAMR, 0,
2801 rpc_group_members_internals,
2805 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2806 const char *domain_name,
2807 struct cli_state *cli,
2808 struct rpc_pipe_client *pipe_hnd,
2809 TALLOC_CTX *mem_ctx,
2814 POLICY_HND connect_pol, domain_pol, group_pol;
2815 uint32 num_rids, *rids, *rid_types;
2819 d_printf("Usage: 'net rpc group rename group newname'\n");
2820 return NT_STATUS_UNSUCCESSFUL;
2823 /* Get sam policy handle */
2825 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2828 if (!NT_STATUS_IS_OK(result))
2831 /* Get domain policy handle */
2833 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2834 MAXIMUM_ALLOWED_ACCESS,
2835 domain_sid, &domain_pol);
2837 if (!NT_STATUS_IS_OK(result))
2840 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2841 1, argv, &num_rids, &rids, &rid_types);
2843 if (num_rids != 1) {
2844 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2848 if (rid_types[0] != SID_NAME_DOM_GRP) {
2849 d_fprintf(stderr, "Can only rename domain groups\n");
2850 return NT_STATUS_UNSUCCESSFUL;
2853 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2854 MAXIMUM_ALLOWED_ACCESS,
2855 rids[0], &group_pol);
2857 if (!NT_STATUS_IS_OK(result))
2862 ctr.switch_value1 = 2;
2863 init_samr_group_info2(&ctr.group.info2, argv[1]);
2865 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2867 if (!NT_STATUS_IS_OK(result))
2870 return NT_STATUS_NO_SUCH_GROUP;
2873 static int rpc_group_rename(int argc, const char **argv)
2876 return rpc_group_usage(argc, argv);
2879 return run_rpc_command(NULL, PI_SAMR, 0,
2880 rpc_group_rename_internals,
2885 * 'net rpc group' entrypoint.
2886 * @param argc Standard main() style argc
2887 * @param argc Standard main() style argv. Initial components are already
2891 int net_rpc_group(int argc, const char **argv)
2893 struct functable func[] = {
2894 {"add", rpc_group_add},
2895 {"delete", rpc_group_delete},
2896 {"addmem", rpc_group_addmem},
2897 {"delmem", rpc_group_delmem},
2898 {"list", rpc_group_list},
2899 {"members", rpc_group_members},
2900 {"rename", rpc_group_rename},
2905 return run_rpc_command(NULL, PI_SAMR, 0,
2906 rpc_group_list_internals,
2910 return net_run_function(argc, argv, func, rpc_group_usage);
2913 /****************************************************************************/
2915 static int rpc_share_usage(int argc, const char **argv)
2917 return net_help_share(argc, argv);
2921 * Add a share on a remote RPC server
2923 * All parameters are provided by the run_rpc_command function, except for
2924 * argc, argv which are passes through.
2926 * @param domain_sid The domain sid acquired from the remote server
2927 * @param cli A cli_state connected to the server.
2928 * @param mem_ctx Talloc context, destoyed on completion of the function.
2929 * @param argc Standard main() style argc
2930 * @param argv Standard main() style argv. Initial components are already
2933 * @return Normal NTSTATUS return.
2935 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
2936 const char *domain_name,
2937 struct cli_state *cli,
2938 struct rpc_pipe_client *pipe_hnd,
2939 TALLOC_CTX *mem_ctx,int argc,
2943 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2945 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2946 uint32 num_users=0, perms=0;
2947 char *password=NULL; /* don't allow a share password */
2950 path = strchr(sharename, '=');
2952 return NT_STATUS_UNSUCCESSFUL;
2955 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
2956 opt_comment, perms, opt_maxusers,
2957 num_users, path, password,
2959 return werror_to_ntstatus(result);
2962 static int rpc_share_add(int argc, const char **argv)
2964 if ((argc < 1) || !strchr(argv[0], '=')) {
2965 DEBUG(1,("Sharename or path not specified on add\n"));
2966 return rpc_share_usage(argc, argv);
2968 return run_rpc_command(NULL, PI_SRVSVC, 0,
2969 rpc_share_add_internals,
2974 * Delete a share on a remote RPC server
2976 * All parameters are provided by the run_rpc_command function, except for
2977 * argc, argv which are passes through.
2979 * @param domain_sid The domain sid acquired from the remote server
2980 * @param cli A cli_state connected to the server.
2981 * @param mem_ctx Talloc context, destoyed on completion of the function.
2982 * @param argc Standard main() style argc
2983 * @param argv Standard main() style argv. Initial components are already
2986 * @return Normal NTSTATUS return.
2988 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
2989 const char *domain_name,
2990 struct cli_state *cli,
2991 struct rpc_pipe_client *pipe_hnd,
2992 TALLOC_CTX *mem_ctx,
2998 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
2999 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3003 * Delete a share on a remote RPC server
3005 * @param domain_sid The domain sid acquired from the remote server
3006 * @param argc Standard main() style argc
3007 * @param argv Standard main() style argv. Initial components are already
3010 * @return A shell status integer (0 for success)
3012 static int rpc_share_delete(int argc, const char **argv)
3015 DEBUG(1,("Sharename not specified on delete\n"));
3016 return rpc_share_usage(argc, argv);
3018 return run_rpc_command(NULL, PI_SRVSVC, 0,
3019 rpc_share_del_internals,
3024 * Formatted print of share info
3026 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3029 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3031 fstring netname = "", remark = "";
3033 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3034 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3036 if (opt_long_list_entries) {
3037 d_printf("%-12s %-8.8s %-50s\n",
3038 netname, share_type[info1->info_1.type], remark);
3040 d_printf("%s\n", netname);
3045 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3046 TALLOC_CTX *mem_ctx,
3050 SRV_SHARE_INFO_CTR *ctr)
3053 SRV_SHARE_INFO info;
3055 /* no specific share requested, enumerate all */
3059 uint32 preferred_len = 0xffffffff;
3061 init_enum_hnd(&hnd, 0);
3063 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3064 preferred_len, &hnd);
3067 /* request just one share */
3068 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3070 if (!W_ERROR_IS_OK(result))
3076 ctr->info_level = ctr->switch_value = level;
3077 ctr->ptr_share_info = ctr->ptr_entries = 1;
3078 ctr->num_entries = ctr->num_entries2 = 1;
3084 SRV_SHARE_INFO_1 *info1;
3086 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3087 info1 = ctr->share.info1;
3089 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3091 /* Copy pointer crap */
3093 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3095 /* Duplicate strings */
3097 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3099 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3101 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3103 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3108 SRV_SHARE_INFO_2 *info2;
3110 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3111 info2 = ctr->share.info2;
3113 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3115 /* Copy pointer crap */
3117 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3119 /* Duplicate strings */
3121 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3123 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3125 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3127 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3129 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_path);
3131 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3133 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3135 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3140 SRV_SHARE_INFO_502 *info502;
3142 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3143 info502 = ctr->share.info502;
3145 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3147 /* Copy pointer crap */
3149 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3151 /* Duplicate strings */
3153 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3155 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3157 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3159 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3161 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_path);
3163 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3165 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3167 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3169 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3180 * List shares on a remote RPC server
3182 * All parameters are provided by the run_rpc_command function, except for
3183 * argc, argv which are passes through.
3185 * @param domain_sid The domain sid acquired from the remote server
3186 * @param cli A cli_state connected to the server.
3187 * @param mem_ctx Talloc context, destoyed on completion of the function.
3188 * @param argc Standard main() style argc
3189 * @param argv Standard main() style argv. Initial components are already
3192 * @return Normal NTSTATUS return.
3195 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3196 const char *domain_name,
3197 struct cli_state *cli,
3198 struct rpc_pipe_client *pipe_hnd,
3199 TALLOC_CTX *mem_ctx,
3203 SRV_SHARE_INFO_CTR ctr;
3205 uint32 i, level = 1;
3207 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3208 if (!W_ERROR_IS_OK(result))
3211 /* Display results */
3213 if (opt_long_list_entries) {
3215 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3216 "\nShare name Type Description\n"\
3217 "---------- ---- -----------\n");
3219 for (i = 0; i < ctr.num_entries; i++)
3220 display_share_info_1(&ctr.share.info1[i]);
3222 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3226 * 'net rpc share list' entrypoint.
3227 * @param argc Standard main() style argc
3228 * @param argv Standard main() style argv. Initial components are already
3231 static int rpc_share_list(int argc, const char **argv)
3233 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3236 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3238 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3239 d_printf("skipping [%s]: not a file share.\n", netname);
3249 static BOOL check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3251 /* only support disk shares */
3252 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3253 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3257 /* skip builtin shares */
3258 /* FIXME: should print$ be added too ? */
3259 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3260 strequal(netname,"global"))
3263 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3264 printf("excluding [%s]\n", netname);
3268 return check_share_availability(cli, netname);
3272 * Migrate shares from a remote RPC server to the local RPC srever
3274 * All parameters are provided by the run_rpc_command function, except for
3275 * argc, argv which are passes through.
3277 * @param domain_sid The domain sid acquired from the remote server
3278 * @param cli A cli_state connected to the server.
3279 * @param mem_ctx Talloc context, destoyed on completion of the function.
3280 * @param argc Standard main() style argc
3281 * @param argv Standard main() style argv. Initial components are already
3284 * @return Normal NTSTATUS return.
3287 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3288 const char *domain_name,
3289 struct cli_state *cli,
3290 struct rpc_pipe_client *pipe_hnd,
3291 TALLOC_CTX *mem_ctx,
3296 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3297 SRV_SHARE_INFO_CTR ctr_src;
3298 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3299 char *password = NULL; /* don't allow a share password */
3301 struct rpc_pipe_client *srvsvc_pipe = NULL;
3302 struct cli_state *cli_dst = NULL;
3303 uint32 level = 502; /* includes secdesc */
3305 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3306 if (!W_ERROR_IS_OK(result))
3309 /* connect destination PI_SRVSVC */
3310 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3311 if (!NT_STATUS_IS_OK(nt_status))
3315 for (i = 0; i < ctr_src.num_entries; i++) {
3317 fstring netname = "", remark = "", path = "";
3318 /* reset error-code */
3319 nt_status = NT_STATUS_UNSUCCESSFUL;
3321 rpcstr_pull_unistr2_fstring(
3322 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3323 rpcstr_pull_unistr2_fstring(
3324 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3325 rpcstr_pull_unistr2_fstring(
3326 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3328 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3331 /* finally add the share on the dst server */
3333 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3334 netname, path, remark);
3336 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3337 ctr_src.share.info502[i].info_502.perms,
3338 ctr_src.share.info502[i].info_502.max_uses,
3339 ctr_src.share.info502[i].info_502.num_uses,
3340 path, password, level,
3343 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3344 printf(" [%s] does already exist\n", netname);
3348 if (!W_ERROR_IS_OK(result)) {
3349 printf("cannot add share: %s\n", dos_errstr(result));
3355 nt_status = NT_STATUS_OK;
3359 cli_shutdown(cli_dst);
3367 * Migrate shares from a rpc-server to another
3369 * @param argc Standard main() style argc
3370 * @param argv Standard main() style argv. Initial components are already
3373 * @return A shell status integer (0 for success)
3375 static int rpc_share_migrate_shares(int argc, const char **argv)
3379 printf("no server to migrate\n");
3383 return run_rpc_command(NULL, PI_SRVSVC, 0,
3384 rpc_share_migrate_shares_internals,
3391 * @param f file_info
3392 * @param mask current search mask
3393 * @param state arg-pointer
3396 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3398 static NTSTATUS nt_status;
3399 static struct copy_clistate *local_state;
3400 static fstring filename, new_mask;
3404 local_state = (struct copy_clistate *)state;
3405 nt_status = NT_STATUS_UNSUCCESSFUL;
3407 if (strequal(f->name, ".") || strequal(f->name, ".."))
3410 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3413 if (f->mode & aDIR) {
3415 DEBUG(3,("got dir: %s\n", f->name));
3417 fstrcpy(dir, local_state->cwd);
3419 fstrcat(dir, f->name);
3421 switch (net_mode_share)
3423 case NET_MODE_SHARE_MIGRATE:
3424 /* create that directory */
3425 nt_status = net_copy_file(local_state->mem_ctx,
3426 local_state->cli_share_src,
3427 local_state->cli_share_dst,
3429 opt_acls? True : False,
3430 opt_attrs? True : False,
3431 opt_timestamps? True : False,
3435 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3439 if (!NT_STATUS_IS_OK(nt_status))
3440 printf("could not handle dir %s: %s\n",
3441 dir, nt_errstr(nt_status));
3443 /* search below that directory */
3444 fstrcpy(new_mask, dir);
3445 fstrcat(new_mask, "\\*");
3447 old_dir = local_state->cwd;
3448 local_state->cwd = dir;
3449 if (!sync_files(local_state, new_mask))
3450 printf("could not handle files\n");
3451 local_state->cwd = old_dir;
3458 fstrcpy(filename, local_state->cwd);
3459 fstrcat(filename, "\\");
3460 fstrcat(filename, f->name);
3462 DEBUG(3,("got file: %s\n", filename));
3464 switch (net_mode_share)
3466 case NET_MODE_SHARE_MIGRATE:
3467 nt_status = net_copy_file(local_state->mem_ctx,
3468 local_state->cli_share_src,
3469 local_state->cli_share_dst,
3471 opt_acls? True : False,
3472 opt_attrs? True : False,
3473 opt_timestamps? True: False,
3477 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3481 if (!NT_STATUS_IS_OK(nt_status))
3482 printf("could not handle file %s: %s\n",
3483 filename, nt_errstr(nt_status));
3488 * sync files, can be called recursivly to list files
3489 * and then call copy_fn for each file
3491 * @param cp_clistate pointer to the copy_clistate we work with
3492 * @param mask the current search mask
3494 * @return Boolean result
3496 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3499 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3501 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3502 d_fprintf(stderr, "listing %s failed with error: %s\n",
3503 mask, cli_errstr(cp_clistate->cli_share_src));
3512 * Set the top level directory permissions before we do any further copies.
3513 * Should set up ACL inheritance.
3516 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3517 const char *sharename)
3519 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3521 switch (net_mode_share) {
3522 case NET_MODE_SHARE_MIGRATE:
3523 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3524 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3525 cp_clistate->cli_share_src,
3526 cp_clistate->cli_share_dst,
3528 opt_acls? True : False,
3529 opt_attrs? True : False,
3530 opt_timestamps? True: False,
3534 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3538 if (!NT_STATUS_IS_OK(nt_status)) {
3539 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3540 sharename, nt_errstr(nt_status));
3548 * Sync all files inside a remote share to another share (over smb)
3550 * All parameters are provided by the run_rpc_command function, except for
3551 * argc, argv which are passes through.
3553 * @param domain_sid The domain sid acquired from the remote server
3554 * @param cli A cli_state connected to the server.
3555 * @param mem_ctx Talloc context, destoyed on completion of the function.
3556 * @param argc Standard main() style argc
3557 * @param argv Standard main() style argv. Initial components are already
3560 * @return Normal NTSTATUS return.
3563 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3564 const char *domain_name,
3565 struct cli_state *cli,
3566 struct rpc_pipe_client *pipe_hnd,
3567 TALLOC_CTX *mem_ctx,
3572 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3573 SRV_SHARE_INFO_CTR ctr_src;
3576 struct copy_clistate cp_clistate;
3577 BOOL got_src_share = False;
3578 BOOL got_dst_share = False;
3579 pstring mask = "\\*";
3582 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3584 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3586 if (!W_ERROR_IS_OK(result))
3589 for (i = 0; i < ctr_src.num_entries; i++) {
3591 fstring netname = "";
3593 rpcstr_pull_unistr2_fstring(
3594 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3596 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3599 /* one might not want to mirror whole discs :) */
3600 if (strequal(netname, "print$") || netname[1] == '$') {
3601 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3605 switch (net_mode_share)
3607 case NET_MODE_SHARE_MIGRATE:
3611 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3614 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3616 opt_acls ? "including" : "without",
3617 opt_attrs ? "including" : "without",
3618 opt_timestamps ? "(preserving timestamps)" : "");
3620 cp_clistate.mem_ctx = mem_ctx;
3621 cp_clistate.cli_share_src = NULL;
3622 cp_clistate.cli_share_dst = NULL;
3623 cp_clistate.cwd = NULL;
3624 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3626 /* open share source */
3627 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3628 &cli->dest_ip, cli->desthost,
3630 if (!NT_STATUS_IS_OK(nt_status))
3633 got_src_share = True;
3635 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3636 /* open share destination */
3637 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3638 NULL, dst, netname, "A:");
3639 if (!NT_STATUS_IS_OK(nt_status))
3642 got_dst_share = True;
3645 if (!copy_top_level_perms(&cp_clistate, netname)) {
3646 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
3647 nt_status = NT_STATUS_UNSUCCESSFUL;
3651 if (!sync_files(&cp_clistate, mask)) {
3652 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
3653 nt_status = NT_STATUS_UNSUCCESSFUL;
3658 nt_status = NT_STATUS_OK;
3663 cli_shutdown(cp_clistate.cli_share_src);
3666 cli_shutdown(cp_clistate.cli_share_dst);
3672 static int rpc_share_migrate_files(int argc, const char **argv)
3676 printf("no server to migrate\n");
3680 return run_rpc_command(NULL, PI_SRVSVC, 0,
3681 rpc_share_migrate_files_internals,
3686 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3688 * All parameters are provided by the run_rpc_command function, except for
3689 * argc, argv which are passes through.
3691 * @param domain_sid The domain sid acquired from the remote server
3692 * @param cli A cli_state connected to the server.
3693 * @param mem_ctx Talloc context, destoyed on completion of the function.
3694 * @param argc Standard main() style argc
3695 * @param argv Standard main() style argv. Initial components are already
3698 * @return Normal NTSTATUS return.
3701 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3702 const char *domain_name,
3703 struct cli_state *cli,
3704 struct rpc_pipe_client *pipe_hnd,
3705 TALLOC_CTX *mem_ctx,
3710 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3711 SRV_SHARE_INFO_CTR ctr_src;
3712 SRV_SHARE_INFO info;
3714 struct rpc_pipe_client *srvsvc_pipe = NULL;
3715 struct cli_state *cli_dst = NULL;
3716 uint32 level = 502; /* includes secdesc */
3718 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3720 if (!W_ERROR_IS_OK(result))
3723 /* connect destination PI_SRVSVC */
3724 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3725 if (!NT_STATUS_IS_OK(nt_status))
3729 for (i = 0; i < ctr_src.num_entries; i++) {
3731 fstring netname = "", remark = "", path = "";
3732 /* reset error-code */
3733 nt_status = NT_STATUS_UNSUCCESSFUL;
3735 rpcstr_pull_unistr2_fstring(
3736 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3737 rpcstr_pull_unistr2_fstring(
3738 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3739 rpcstr_pull_unistr2_fstring(
3740 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3742 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3745 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3746 netname, path, remark);
3749 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
3754 info.switch_value = level;
3755 info.ptr_share_ctr = 1;
3757 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3758 info.share.info502 = ctr_src.share.info502[i];
3760 /* finally modify the share on the dst server */
3761 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
3763 if (!W_ERROR_IS_OK(result)) {
3764 printf("cannot set share-acl: %s\n", dos_errstr(result));
3770 nt_status = NT_STATUS_OK;
3774 cli_shutdown(cli_dst);
3782 * Migrate share-acls from a rpc-server to another
3784 * @param argc Standard main() style argc
3785 * @param argv Standard main() style argv. Initial components are already
3788 * @return A shell status integer (0 for success)
3790 static int rpc_share_migrate_security(int argc, const char **argv)
3794 printf("no server to migrate\n");
3798 return run_rpc_command(NULL, PI_SRVSVC, 0,
3799 rpc_share_migrate_security_internals,
3804 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3805 * from one server to another
3807 * @param argc Standard main() style argc
3808 * @param argv Standard main() style argv. Initial components are already
3811 * @return A shell status integer (0 for success)
3814 static int rpc_share_migrate_all(int argc, const char **argv)
3819 printf("no server to migrate\n");
3823 /* order is important. we don't want to be locked out by the share-acl
3824 * before copying files - gd */
3826 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3830 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3834 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3839 * 'net rpc share migrate' entrypoint.
3840 * @param argc Standard main() style argc
3841 * @param argv Standard main() style argv. Initial components are already
3844 static int rpc_share_migrate(int argc, const char **argv)
3847 struct functable func[] = {
3848 {"all", rpc_share_migrate_all},
3849 {"files", rpc_share_migrate_files},
3850 {"help", rpc_share_usage},
3851 {"security", rpc_share_migrate_security},
3852 {"shares", rpc_share_migrate_shares},
3856 net_mode_share = NET_MODE_SHARE_MIGRATE;
3858 return net_run_function(argc, argv, func, rpc_share_usage);
3867 static int num_server_aliases;
3868 static struct full_alias *server_aliases;
3871 * Add an alias to the static list.
3873 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3875 if (server_aliases == NULL)
3876 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3878 server_aliases[num_server_aliases] = *alias;
3879 num_server_aliases += 1;
3883 * For a specific domain on the server, fetch all the aliases
3884 * and their members. Add all of them to the server_aliases.
3887 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3888 TALLOC_CTX *mem_ctx,
3889 POLICY_HND *connect_pol,
3890 const DOM_SID *domain_sid)
3892 uint32 start_idx, max_entries, num_entries, i;
3893 struct acct_info *groups;
3895 POLICY_HND domain_pol;
3897 /* Get domain policy handle */
3899 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3900 MAXIMUM_ALLOWED_ACCESS,
3901 domain_sid, &domain_pol);
3902 if (!NT_STATUS_IS_OK(result))
3909 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
3910 &start_idx, max_entries,
3911 &groups, &num_entries);
3913 for (i = 0; i < num_entries; i++) {
3915 POLICY_HND alias_pol;
3916 struct full_alias alias;
3920 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
3921 MAXIMUM_ALLOWED_ACCESS,
3924 if (!NT_STATUS_IS_OK(result))
3927 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
3931 if (!NT_STATUS_IS_OK(result))
3934 result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
3935 if (!NT_STATUS_IS_OK(result))
3938 alias.members = NULL;
3940 if (alias.num_members > 0) {
3941 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3943 for (j = 0; j < alias.num_members; j++)
3944 sid_copy(&alias.members[j],
3948 sid_copy(&alias.sid, domain_sid);
3949 sid_append_rid(&alias.sid, groups[i].rid);
3951 push_alias(mem_ctx, &alias);
3953 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3955 result = NT_STATUS_OK;
3958 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
3964 * Dump server_aliases as names for debugging purposes.
3967 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
3968 const char *domain_name,
3969 struct cli_state *cli,
3970 struct rpc_pipe_client *pipe_hnd,
3971 TALLOC_CTX *mem_ctx,
3979 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
3980 SEC_RIGHTS_MAXIMUM_ALLOWED,
3982 if (!NT_STATUS_IS_OK(result))
3985 for (i=0; i<num_server_aliases; i++) {
3991 struct full_alias *alias = &server_aliases[i];
3993 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3995 &domains, &names, &types);
3996 if (!NT_STATUS_IS_OK(result))
3999 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4001 if (alias->num_members == 0) {
4006 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4009 &domains, &names, &types);
4011 if (!NT_STATUS_IS_OK(result) &&
4012 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4015 for (j=0; j<alias->num_members; j++)
4016 DEBUG(1, ("%s\\%s (%d); ",
4017 domains[j] ? domains[j] : "*unknown*",
4018 names[j] ? names[j] : "*unknown*",types[j]));
4022 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
4024 return NT_STATUS_OK;
4028 * Fetch a list of all server aliases and their members into
4032 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4033 const char *domain_name,
4034 struct cli_state *cli,
4035 struct rpc_pipe_client *pipe_hnd,
4036 TALLOC_CTX *mem_ctx,
4041 POLICY_HND connect_pol;
4043 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4046 if (!NT_STATUS_IS_OK(result))
4049 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4050 &global_sid_Builtin);
4052 if (!NT_STATUS_IS_OK(result))
4055 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4058 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4063 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4065 token->num_sids = 4;
4067 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4069 token->user_sids[0] = *user_sid;
4070 sid_copy(&token->user_sids[1], &global_sid_World);
4071 sid_copy(&token->user_sids[2], &global_sid_Network);
4072 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4075 static void free_user_token(NT_USER_TOKEN *token)
4077 SAFE_FREE(token->user_sids);
4080 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4084 for (i=0; i<token->num_sids; i++) {
4085 if (sid_compare(sid, &token->user_sids[i]) == 0)
4091 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4093 if (is_sid_in_token(token, sid))
4096 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4098 sid_copy(&token->user_sids[token->num_sids], sid);
4100 token->num_sids += 1;
4105 NT_USER_TOKEN token;
4108 static void dump_user_token(struct user_token *token)
4112 d_printf("%s\n", token->name);
4114 for (i=0; i<token->token.num_sids; i++) {
4115 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4119 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4123 for (i=0; i<alias->num_members; i++) {
4124 if (sid_compare(sid, &alias->members[i]) == 0)
4131 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4135 for (i=0; i<num_server_aliases; i++) {
4136 if (is_alias_member(&sid, &server_aliases[i]))
4137 add_sid_to_token(token, &server_aliases[i].sid);
4142 * We got a user token with all the SIDs we can know about without asking the
4143 * server directly. These are the user and domain group sids. All of these can
4144 * be members of aliases. So scan the list of aliases for each of the SIDs and
4145 * add them to the token.
4148 static void collect_alias_memberships(NT_USER_TOKEN *token)
4150 int num_global_sids = token->num_sids;
4153 for (i=0; i<num_global_sids; i++) {
4154 collect_sid_memberships(token, token->user_sids[i]);
4158 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4160 struct winbindd_request request;
4161 struct winbindd_response response;
4169 fstr_sprintf(full_name, "%s%c%s",
4170 domain, *lp_winbind_separator(), user);
4172 /* First let's find out the user sid */
4174 ZERO_STRUCT(request);
4175 ZERO_STRUCT(response);
4177 fstrcpy(request.data.name.dom_name, domain);
4178 fstrcpy(request.data.name.name, user);
4180 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4182 if (result != NSS_STATUS_SUCCESS) {
4183 DEBUG(1, ("winbind could not find %s\n", full_name));
4187 if (response.data.sid.type != SID_NAME_USER) {
4188 DEBUG(1, ("%s is not a user\n", full_name));
4192 string_to_sid(&user_sid, response.data.sid.sid);
4194 init_user_token(token, &user_sid);
4196 /* And now the groups winbind knows about */
4198 ZERO_STRUCT(response);
4200 fstrcpy(request.data.username, full_name);
4202 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4204 if (result != NSS_STATUS_SUCCESS) {
4205 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4209 for (i = 0; i < response.data.num_entries; i++) {
4210 gid_t gid = ((gid_t *)response.extra_data)[i];
4213 struct winbindd_request sidrequest;
4214 struct winbindd_response sidresponse;
4216 ZERO_STRUCT(sidrequest);
4217 ZERO_STRUCT(sidresponse);
4219 sidrequest.data.gid = gid;
4221 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4222 &sidrequest, &sidresponse);
4224 if (result != NSS_STATUS_SUCCESS) {
4225 DEBUG(1, ("winbind could not find SID of gid %d\n",
4230 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4232 string_to_sid(&sid, sidresponse.data.sid.sid);
4233 add_sid_to_token(token, &sid);
4236 SAFE_FREE(response.extra_data);
4242 * Get a list of all user tokens we want to look at
4245 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4247 struct winbindd_request request;
4248 struct winbindd_response response;
4249 const char *extra_data;
4252 struct user_token *result;
4254 if (lp_winbind_use_default_domain() &&
4255 (opt_target_workgroup == NULL)) {
4256 d_fprintf(stderr, "winbind use default domain = yes set, "
4257 "please specify a workgroup\n");
4261 /* Send request to winbind daemon */
4263 ZERO_STRUCT(request);
4264 ZERO_STRUCT(response);
4266 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4270 /* Look through extra data */
4272 if (!response.extra_data)
4275 extra_data = (const char *)response.extra_data;
4278 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4282 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4284 if (result == NULL) {
4285 DEBUG(1, ("Could not malloc sid array\n"));
4289 extra_data = (const char *)response.extra_data;
4292 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4294 fstring domain, user;
4297 fstrcpy(result[i].name, name);
4299 p = strchr(name, *lp_winbind_separator());
4301 DEBUG(3, ("%s\n", name));
4304 fstrcpy(domain, opt_target_workgroup);
4305 fstrcpy(user, name);
4308 fstrcpy(domain, name);
4313 get_user_sids(domain, user, &(result[i].token));
4317 SAFE_FREE(response.extra_data);
4319 *user_tokens = result;
4324 static BOOL get_user_tokens_from_file(FILE *f,
4326 struct user_token **tokens)
4328 struct user_token *token = NULL;
4333 if (fgets(line, sizeof(line)-1, f) == NULL) {
4337 if (line[strlen(line)-1] == '\n')
4338 line[strlen(line)-1] = '\0';
4340 if (line[0] == ' ') {
4344 string_to_sid(&sid, &line[1]);
4346 if (token == NULL) {
4347 DEBUG(0, ("File does not begin with username"));
4351 add_sid_to_token(&token->token, &sid);
4355 /* And a new user... */
4358 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4359 if (*tokens == NULL) {
4360 DEBUG(0, ("Could not realloc tokens\n"));
4364 token = &((*tokens)[*num_tokens-1]);
4366 fstrcpy(token->name, line);
4367 token->token.num_sids = 0;
4368 token->token.user_sids = NULL;
4377 * Show the list of all users that have access to a share
4380 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4381 TALLOC_CTX *mem_ctx,
4382 const char *netname,
4384 struct user_token *tokens)
4387 SEC_DESC *share_sd = NULL;
4388 SEC_DESC *root_sd = NULL;
4389 struct cli_state *cli = pipe_hnd->cli;
4391 SRV_SHARE_INFO info;
4395 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4398 if (!W_ERROR_IS_OK(result)) {
4399 DEBUG(1, ("Coult not query secdesc for share %s\n",
4404 share_sd = info.share.info502.info_502_str.sd;
4405 if (share_sd == NULL) {
4406 DEBUG(1, ("Got no secdesc for share %s\n",
4412 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4416 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4419 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4422 for (i=0; i<num_tokens; i++) {
4426 if (share_sd != NULL) {
4427 if (!se_access_check(share_sd, &tokens[i].token,
4428 1, &acc_granted, &status)) {
4429 DEBUG(1, ("Could not check share_sd for "
4435 if (!NT_STATUS_IS_OK(status))
4439 if (root_sd == NULL) {
4440 d_printf(" %s\n", tokens[i].name);
4444 if (!se_access_check(root_sd, &tokens[i].token,
4445 1, &acc_granted, &status)) {
4446 DEBUG(1, ("Could not check root_sd for user %s\n",
4451 if (!NT_STATUS_IS_OK(status))
4454 d_printf(" %s\n", tokens[i].name);
4458 cli_close(cli, fnum);
4470 static void collect_share(const char *name, uint32 m,
4471 const char *comment, void *state)
4473 struct share_list *share_list = (struct share_list *)state;
4475 if (m != STYPE_DISKTREE)
4478 share_list->num_shares += 1;
4479 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4480 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4483 static void rpc_share_userlist_usage(void)
4489 * List shares on a remote RPC server, including the security descriptors
4491 * All parameters are provided by the run_rpc_command function, except for
4492 * argc, argv which are passes through.
4494 * @param domain_sid The domain sid acquired from the remote server
4495 * @param cli A cli_state connected to the server.
4496 * @param mem_ctx Talloc context, destoyed on completion of the function.
4497 * @param argc Standard main() style argc
4498 * @param argv Standard main() style argv. Initial components are already
4501 * @return Normal NTSTATUS return.
4504 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4505 const char *domain_name,
4506 struct cli_state *cli,
4507 struct rpc_pipe_client *pipe_hnd,
4508 TALLOC_CTX *mem_ctx,
4518 struct user_token *tokens = NULL;
4521 struct share_list share_list;
4524 rpc_share_userlist_usage();
4525 return NT_STATUS_UNSUCCESSFUL;
4531 f = fopen(argv[0], "r");
4535 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4536 return NT_STATUS_UNSUCCESSFUL;
4539 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4545 DEBUG(0, ("Could not read users from file\n"));
4546 return NT_STATUS_UNSUCCESSFUL;
4549 for (i=0; i<num_tokens; i++)
4550 collect_alias_memberships(&tokens[i].token);
4552 init_enum_hnd(&hnd, 0);
4554 share_list.num_shares = 0;
4555 share_list.shares = NULL;
4557 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4560 DEBUG(0, ("Error returning browse list: %s\n",
4565 for (i = 0; i < share_list.num_shares; i++) {
4566 char *netname = share_list.shares[i];
4568 if (netname[strlen(netname)-1] == '$')
4571 d_printf("%s\n", netname);
4573 show_userlist(pipe_hnd, mem_ctx, netname,
4574 num_tokens, tokens);
4577 for (i=0; i<num_tokens; i++) {
4578 free_user_token(&tokens[i].token);
4581 SAFE_FREE(share_list.shares);
4583 return NT_STATUS_OK;
4586 static int rpc_share_allowedusers(int argc, const char **argv)
4590 result = run_rpc_command(NULL, PI_SAMR, 0,
4591 rpc_aliaslist_internals,
4596 result = run_rpc_command(NULL, PI_LSARPC, 0,
4602 return run_rpc_command(NULL, PI_SRVSVC, 0,
4603 rpc_share_allowedusers_internals,
4607 int net_usersidlist(int argc, const char **argv)
4610 struct user_token *tokens = NULL;
4614 net_usersidlist_usage(argc, argv);
4618 if (!get_user_tokens(&num_tokens, &tokens)) {
4619 DEBUG(0, ("Could not get the user/sid list\n"));
4623 for (i=0; i<num_tokens; i++) {
4624 dump_user_token(&tokens[i]);
4625 free_user_token(&tokens[i].token);
4632 int net_usersidlist_usage(int argc, const char **argv)
4634 d_printf("net usersidlist\n"
4635 "\tprints out a list of all users the running winbind knows\n"
4636 "\tabout, together with all their SIDs. This is used as\n"
4637 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4639 net_common_flags_usage(argc, argv);
4644 * 'net rpc share' entrypoint.
4645 * @param argc Standard main() style argc
4646 * @param argv Standard main() style argv. Initial components are already
4650 int net_rpc_share(int argc, const char **argv)
4652 struct functable func[] = {
4653 {"add", rpc_share_add},
4654 {"delete", rpc_share_delete},
4655 {"allowedusers", rpc_share_allowedusers},
4656 {"migrate", rpc_share_migrate},
4657 {"list", rpc_share_list},
4662 return run_rpc_command(NULL, PI_SRVSVC, 0,
4663 rpc_share_list_internals,
4666 return net_run_function(argc, argv, func, rpc_share_usage);
4669 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4670 struct rpc_sh_ctx *ctx,
4671 struct rpc_pipe_client *pipe_hnd,
4672 int argc, const char **argv)
4674 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4675 ctx->cli, pipe_hnd, mem_ctx,
4679 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4680 struct rpc_sh_ctx *ctx,
4681 struct rpc_pipe_client *pipe_hnd,
4682 int argc, const char **argv)
4686 if ((argc < 2) || (argc > 3)) {
4687 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4689 return NT_STATUS_INVALID_PARAMETER;
4692 result = rpccli_srvsvc_net_share_add(
4693 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
4694 (argc == 3) ? argv[2] : "",
4695 0, 0, 0, argv[1], NULL, 2, NULL);
4697 return werror_to_ntstatus(result);
4700 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4701 struct rpc_sh_ctx *ctx,
4702 struct rpc_pipe_client *pipe_hnd,
4703 int argc, const char **argv)
4708 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4709 return NT_STATUS_INVALID_PARAMETER;
4712 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
4713 return werror_to_ntstatus(result);
4716 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4717 struct rpc_sh_ctx *ctx,
4718 struct rpc_pipe_client *pipe_hnd,
4719 int argc, const char **argv)
4721 SRV_SHARE_INFO info;
4722 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
4726 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4727 return NT_STATUS_INVALID_PARAMETER;
4730 result = rpccli_srvsvc_net_share_get_info(
4731 pipe_hnd, mem_ctx, argv[0], 2, &info);
4732 if (!W_ERROR_IS_OK(result)) {
4736 d_printf("Name: %s\n",
4737 rpcstr_pull_unistr2_talloc(mem_ctx,
4738 &info2->info_2_str.uni_netname));
4739 d_printf("Comment: %s\n",
4740 rpcstr_pull_unistr2_talloc(mem_ctx,
4741 &info2->info_2_str.uni_remark));
4743 d_printf("Path: %s\n",
4744 rpcstr_pull_unistr2_talloc(mem_ctx,
4745 &info2->info_2_str.uni_path));
4746 d_printf("Password: %s\n",
4747 rpcstr_pull_unistr2_talloc(mem_ctx,
4748 &info2->info_2_str.uni_passwd));
4751 return werror_to_ntstatus(result);
4754 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4755 struct rpc_sh_ctx *ctx)
4757 static struct rpc_sh_cmd cmds[] = {
4759 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4760 "List available shares" },
4762 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4765 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4768 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4769 "Get information about a share" },
4771 { NULL, NULL, 0, NULL, NULL }
4777 /****************************************************************************/
4779 static int rpc_file_usage(int argc, const char **argv)
4781 return net_help_file(argc, argv);
4785 * Close a file on a remote RPC server
4787 * All parameters are provided by the run_rpc_command function, except for
4788 * argc, argv which are passes through.
4790 * @param domain_sid The domain sid acquired from the remote server
4791 * @param cli A cli_state connected to the server.
4792 * @param mem_ctx Talloc context, destoyed on completion of the function.
4793 * @param argc Standard main() style argc
4794 * @param argv Standard main() style argv. Initial components are already
4797 * @return Normal NTSTATUS return.
4799 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4800 const char *domain_name,
4801 struct cli_state *cli,
4802 struct rpc_pipe_client *pipe_hnd,
4803 TALLOC_CTX *mem_ctx,
4808 result = rpccli_srvsvc_net_file_close(pipe_hnd, mem_ctx, atoi(argv[0]));
4809 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4813 * Close a file on a remote RPC server
4815 * @param argc Standard main() style argc
4816 * @param argv Standard main() style argv. Initial components are already
4819 * @return A shell status integer (0 for success)
4821 static int rpc_file_close(int argc, const char **argv)
4824 DEBUG(1, ("No fileid given on close\n"));
4825 return(rpc_file_usage(argc, argv));
4828 return run_rpc_command(NULL, PI_SRVSVC, 0,
4829 rpc_file_close_internals,
4834 * Formatted print of open file info
4836 * @param info3 FILE_INFO_3 contents
4837 * @param str3 strings for FILE_INFO_3
4840 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
4842 fstring user = "", path = "";
4844 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
4845 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
4847 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4848 info3->id, user, info3->perms, info3->num_locks, path);
4852 * List open files on a remote RPC server
4854 * All parameters are provided by the run_rpc_command function, except for
4855 * argc, argv which are passes through.
4857 * @param domain_sid The domain sid acquired from the remote server
4858 * @param cli A cli_state connected to the server.
4859 * @param mem_ctx Talloc context, destoyed on completion of the function.
4860 * @param argc Standard main() style argc
4861 * @param argv Standard main() style argv. Initial components are already
4864 * @return Normal NTSTATUS return.
4867 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4868 const char *domain_name,
4869 struct cli_state *cli,
4870 struct rpc_pipe_client *pipe_hnd,
4871 TALLOC_CTX *mem_ctx,
4875 SRV_FILE_INFO_CTR ctr;
4878 uint32 preferred_len = 0xffffffff, i;
4879 const char *username=NULL;
4881 init_enum_hnd(&hnd, 0);
4883 /* if argc > 0, must be user command */
4885 username = smb_xstrdup(argv[0]);
4887 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
4888 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4890 if (!W_ERROR_IS_OK(result))
4893 /* Display results */
4896 "\nEnumerating open files on remote server:\n\n"\
4897 "\nFileId Opened by Perms Locks Path"\
4898 "\n------ --------- ----- ----- ---- \n");
4899 for (i = 0; i < ctr.num_entries; i++)
4900 display_file_info_3(&ctr.file.info3[i].info_3,
4901 &ctr.file.info3[i].info_3_str);
4903 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4907 * List files for a user on a remote RPC server
4909 * @param argc Standard main() style argc
4910 * @param argv Standard main() style argv. Initial components are already
4913 * @return A shell status integer (0 for success)
4916 static int rpc_file_user(int argc, const char **argv)
4919 DEBUG(1, ("No username given\n"));
4920 return(rpc_file_usage(argc, argv));
4923 return run_rpc_command(NULL, PI_SRVSVC, 0,
4924 rpc_file_list_internals,
4929 * 'net rpc file' entrypoint.
4930 * @param argc Standard main() style argc
4931 * @param argv Standard main() style argv. Initial components are already
4935 int net_rpc_file(int argc, const char **argv)
4937 struct functable func[] = {
4938 {"close", rpc_file_close},
4939 {"user", rpc_file_user},
4941 {"info", rpc_file_info},
4947 return run_rpc_command(NULL, PI_SRVSVC, 0,
4948 rpc_file_list_internals,
4951 return net_run_function(argc, argv, func, rpc_file_usage);
4955 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4957 * All parameters are provided by the run_rpc_command function, except for
4958 * argc, argv which are passed through.
4960 * @param domain_sid The domain sid aquired from the remote server
4961 * @param cli A cli_state connected to the server.
4962 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4963 * @param argc Standard main() style argc
4964 * @param argv Standard main() style argv. Initial components are already
4967 * @return Normal NTSTATUS return.
4970 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4971 const char *domain_name,
4972 struct cli_state *cli,
4973 struct rpc_pipe_client *pipe_hnd,
4974 TALLOC_CTX *mem_ctx,
4978 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4980 result = rpccli_shutdown_abort(pipe_hnd, mem_ctx);
4982 if (NT_STATUS_IS_OK(result)) {
4983 d_printf("\nShutdown successfully aborted\n");
4984 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4986 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4992 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4994 * All parameters are provided by the run_rpc_command function, except for
4995 * argc, argv which are passed through.
4997 * @param domain_sid The domain sid aquired from the remote server
4998 * @param cli A cli_state connected to the server.
4999 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5000 * @param argc Standard main() style argc
5001 * @param argv Standard main() style argv. Initial components are already
5004 * @return Normal NTSTATUS return.
5007 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5008 const char *domain_name,
5009 struct cli_state *cli,
5010 struct rpc_pipe_client *pipe_hnd,
5011 TALLOC_CTX *mem_ctx,
5015 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5017 result = werror_to_ntstatus(rpccli_reg_abort_shutdown(pipe_hnd, mem_ctx));
5019 if (NT_STATUS_IS_OK(result)) {
5020 d_printf("\nShutdown successfully aborted\n");
5021 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5023 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5029 * ABORT the Shut down of a remote RPC server
5031 * @param argc Standard main() style argc
5032 * @param argv Standard main() style argv. Initial components are already
5035 * @return A shell status integer (0 for success)
5038 static int rpc_shutdown_abort(int argc, const char **argv)
5040 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5041 rpc_shutdown_abort_internals,
5047 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5049 return run_rpc_command(NULL, PI_WINREG, 0,
5050 rpc_reg_shutdown_abort_internals,
5055 * Shut down a remote RPC Server via initshutdown pipe
5057 * All parameters are provided by the run_rpc_command function, except for
5058 * argc, argv which are passes through.
5060 * @param domain_sid The domain sid aquired from the remote server
5061 * @param cli A cli_state connected to the server.
5062 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5063 * @param argc Standard main() style argc
5064 * @param argc Standard main() style argv. Initial components are already
5067 * @return Normal NTSTATUS return.
5070 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5071 const char *domain_name,
5072 struct cli_state *cli,
5073 struct rpc_pipe_client *pipe_hnd,
5074 TALLOC_CTX *mem_ctx,
5078 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5079 const char *msg = "This machine will be shutdown shortly";
5080 uint32 timeout = 20;
5086 timeout = opt_timeout;
5089 /* create an entry */
5090 result = rpccli_shutdown_init(pipe_hnd, mem_ctx, msg, timeout, opt_reboot,
5093 if (NT_STATUS_IS_OK(result)) {
5094 d_printf("\nShutdown of remote machine succeeded\n");
5095 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5097 DEBUG(1,("Shutdown of remote machine failed!\n"));
5103 * Shut down a remote RPC Server via winreg pipe
5105 * All parameters are provided by the run_rpc_command function, except for
5106 * argc, argv which are passes through.
5108 * @param domain_sid The domain sid aquired from the remote server
5109 * @param cli A cli_state connected to the server.
5110 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5111 * @param argc Standard main() style argc
5112 * @param argc Standard main() style argv. Initial components are already
5115 * @return Normal NTSTATUS return.
5118 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5119 const char *domain_name,
5120 struct cli_state *cli,
5121 struct rpc_pipe_client *pipe_hnd,
5122 TALLOC_CTX *mem_ctx,
5127 const char *msg = "This machine will be shutdown shortly";
5128 uint32 timeout = 20;
5133 struct poptOption long_options[] = {
5134 {"message", 'm', POPT_ARG_STRING, &msg},
5135 {"timeout", 't', POPT_ARG_INT, &timeout},
5136 {"reboot", 'r', POPT_ARG_NONE, &reboot},
5137 {"force", 'f', POPT_ARG_NONE, &force},
5141 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
5142 POPT_CONTEXT_KEEP_FIRST);
5144 rc = poptGetNextOpt(pc);
5147 /* an error occurred during option processing */
5148 DEBUG(0, ("%s: %s\n",
5149 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
5151 return NT_STATUS_INVALID_PARAMETER;
5158 timeout = opt_timeout;
5161 /* create an entry */
5162 result = rpccli_reg_shutdown(pipe_hnd, mem_ctx, msg, timeout, opt_reboot, opt_force);
5164 if (W_ERROR_IS_OK(result)) {
5165 d_printf("\nShutdown of remote machine succeeded\n");
5167 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5168 if (W_ERROR_EQUAL(result,WERR_MACHINE_LOCKED))
5169 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5171 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(result));
5174 return werror_to_ntstatus(result);
5178 * Shut down a remote RPC server
5180 * @param argc Standard main() style argc
5181 * @param argc Standard main() style argv. Initial components are already
5184 * @return A shell status integer (0 for success)
5187 static int rpc_shutdown(int argc, const char **argv)
5189 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5190 rpc_init_shutdown_internals,
5194 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5195 rc = run_rpc_command(NULL, PI_WINREG, 0,
5196 rpc_reg_shutdown_internals, argc, argv);
5202 /***************************************************************************
5203 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5205 ***************************************************************************/
5208 * Add interdomain trust account to the RPC server.
5209 * All parameters (except for argc and argv) are passed by run_rpc_command
5212 * @param domain_sid The domain sid acquired from the server
5213 * @param cli A cli_state connected to the server.
5214 * @param mem_ctx Talloc context, destoyed on completion of the function.
5215 * @param argc Standard main() style argc
5216 * @param argc Standard main() style argv. Initial components are already
5219 * @return normal NTSTATUS return code
5222 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5223 const char *domain_name,
5224 struct cli_state *cli,
5225 struct rpc_pipe_client *pipe_hnd,
5226 TALLOC_CTX *mem_ctx,
5230 POLICY_HND connect_pol, domain_pol, user_pol;
5231 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5234 uint32 unknown, user_rid;
5237 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5238 return NT_STATUS_INVALID_PARAMETER;
5242 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5245 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5246 return NT_STATUS_NO_MEMORY;
5249 strupper_m(acct_name);
5251 /* Get samr policy handle */
5252 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5254 if (!NT_STATUS_IS_OK(result)) {
5258 /* Get domain policy handle */
5259 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5260 MAXIMUM_ALLOWED_ACCESS,
5261 domain_sid, &domain_pol);
5262 if (!NT_STATUS_IS_OK(result)) {
5266 /* Create trusting domain's account */
5267 acb_info = ACB_NORMAL;
5268 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5269 mimir: yes, most probably it is */
5271 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5272 acct_name, acb_info, unknown,
5273 &user_pol, &user_rid);
5274 if (!NT_STATUS_IS_OK(result)) {
5279 SAM_USERINFO_CTR ctr;
5280 SAM_USER_INFO_23 p23;
5286 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5290 ZERO_STRUCT(notime);
5294 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5295 acb_info = ACB_DOMTRUST;
5297 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5298 ¬ime, ¬ime, ¬ime,
5299 nostr, nostr, nostr, nostr, nostr,
5300 nostr, nostr, nostr, nostr, nostr,
5301 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5302 0, 0, (char *)pwbuf);
5303 ctr.switch_value = 23;
5304 ctr.info.id23 = &p23;
5305 p23.passmustchange = 0;
5307 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5308 &cli->user_session_key, &ctr);
5310 if (!NT_STATUS_IS_OK(result)) {
5311 DEBUG(0,("Could not set trust account password: %s\n",
5312 nt_errstr(result)));
5318 SAFE_FREE(acct_name);
5323 * Create interdomain trust account for a remote domain.
5325 * @param argc standard argc
5326 * @param argv standard argv without initial components
5328 * @return Integer status (0 means success)
5331 static int rpc_trustdom_add(int argc, const char **argv)
5334 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5337 d_printf("Usage: net rpc trustdom add <domain>\n");
5344 * Remove interdomain trust account from the RPC server.
5345 * All parameters (except for argc and argv) are passed by run_rpc_command
5348 * @param domain_sid The domain sid acquired from the server
5349 * @param cli A cli_state connected to the server.
5350 * @param mem_ctx Talloc context, destoyed on completion of the function.
5351 * @param argc Standard main() style argc
5352 * @param argc Standard main() style argv. Initial components are already
5355 * @return normal NTSTATUS return code
5358 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5359 const char *domain_name,
5360 struct cli_state *cli,
5361 struct rpc_pipe_client *pipe_hnd,
5362 TALLOC_CTX *mem_ctx,
5366 POLICY_HND connect_pol, domain_pol, user_pol;
5367 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5370 DOM_SID trust_acct_sid;
5371 uint32 *user_rids, num_rids, *name_types;
5372 uint32 flags = 0x000003e8; /* Unknown */
5375 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5376 return NT_STATUS_INVALID_PARAMETER;
5380 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5382 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5384 if (acct_name == NULL)
5385 return NT_STATUS_NO_MEMORY;
5387 strupper_m(acct_name);
5389 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
5390 names[0] = acct_name;
5393 /* Get samr policy handle */
5394 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5396 if (!NT_STATUS_IS_OK(result)) {
5400 /* Get domain policy handle */
5401 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5402 MAXIMUM_ALLOWED_ACCESS,
5403 domain_sid, &domain_pol);
5404 if (!NT_STATUS_IS_OK(result)) {
5408 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5410 &user_rids, &name_types);
5412 if (!NT_STATUS_IS_OK(result)) {
5416 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5417 MAXIMUM_ALLOWED_ACCESS,
5418 user_rids[0], &user_pol);
5420 if (!NT_STATUS_IS_OK(result)) {
5424 /* append the rid to the domain sid */
5425 sid_copy(&trust_acct_sid, domain_sid);
5426 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5430 /* remove the sid */
5432 result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5435 if (!NT_STATUS_IS_OK(result)) {
5441 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5443 if (!NT_STATUS_IS_OK(result)) {
5447 if (!NT_STATUS_IS_OK(result)) {
5448 DEBUG(0,("Could not set trust account password: %s\n",
5449 nt_errstr(result)));
5458 * Delete interdomain trust account for a remote domain.
5460 * @param argc standard argc
5461 * @param argv standard argv without initial components
5463 * @return Integer status (0 means success)
5466 static int rpc_trustdom_del(int argc, const char **argv)
5469 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5472 d_printf("Usage: net rpc trustdom del <domain>\n");
5479 * Establish trust relationship to a trusting domain.
5480 * Interdomain account must already be created on remote PDC.
5482 * @param argc standard argc
5483 * @param argv standard argv without initial components
5485 * @return Integer status (0 means success)
5488 static int rpc_trustdom_establish(int argc, const char **argv)
5490 struct cli_state *cli = NULL;
5491 struct in_addr server_ip;
5492 struct rpc_pipe_client *pipe_hnd = NULL;
5493 POLICY_HND connect_hnd;
5494 TALLOC_CTX *mem_ctx;
5496 DOM_SID *domain_sid;
5499 char* domain_name_pol;
5504 * Connect to \\server\ipc$ as 'our domain' account with password
5508 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5512 domain_name = smb_xstrdup(argv[0]);
5513 strupper_m(domain_name);
5515 /* account name used at first is our domain's name with '$' */
5516 asprintf(&acct_name, "%s$", lp_workgroup());
5517 strupper_m(acct_name);
5520 * opt_workgroup will be used by connection functions further,
5521 * hence it should be set to remote domain name instead of ours
5523 if (opt_workgroup) {
5524 opt_workgroup = smb_xstrdup(domain_name);
5527 opt_user_name = acct_name;
5529 /* find the domain controller */
5530 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5531 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5535 /* connect to ipc$ as username/password */
5536 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5537 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5539 /* Is it trusting domain account for sure ? */
5540 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5541 nt_errstr(nt_status)));
5545 /* store who we connected to */
5547 saf_store( domain_name, cli->desthost );
5550 * Connect to \\server\ipc$ again (this time anonymously)
5553 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5555 if (NT_STATUS_IS_ERR(nt_status)) {
5556 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5557 domain_name, nt_errstr(nt_status)));
5561 * Use NetServerEnum2 to make sure we're talking to a proper server
5564 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5565 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5566 for domain %s\n", domain_name));
5569 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5570 "domain %s", domain_name))) {
5571 DEBUG(0, ("talloc_init() failed\n"));
5577 * Call LsaOpenPolicy and LsaQueryInfo
5580 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5582 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5587 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5589 if (NT_STATUS_IS_ERR(nt_status)) {
5590 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5591 nt_errstr(nt_status)));
5596 /* Querying info level 5 */
5598 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5600 &domain_name_pol, &domain_sid);
5601 if (NT_STATUS_IS_ERR(nt_status)) {
5602 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5603 nt_errstr(nt_status)));
5608 /* There should be actually query info level 3 (following nt serv behaviour),
5609 but I still don't know if it's _really_ necessary */
5612 * Store the password in secrets db
5615 if (!secrets_store_trusted_domain_password(domain_name,
5618 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5624 * Close the pipes and clean up
5627 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5628 if (NT_STATUS_IS_ERR(nt_status)) {
5629 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5630 nt_errstr(nt_status)));
5637 talloc_destroy(mem_ctx);
5639 d_printf("Trust to domain %s established\n", domain_name);
5644 * Revoke trust relationship to the remote domain
5646 * @param argc standard argc
5647 * @param argv standard argv without initial components
5649 * @return Integer status (0 means success)
5652 static int rpc_trustdom_revoke(int argc, const char **argv)
5656 if (argc < 1) return -1;
5658 /* generate upper cased domain name */
5659 domain_name = smb_xstrdup(argv[0]);
5660 strupper_m(domain_name);
5662 /* delete password of the trust */
5663 if (!trusted_domain_password_delete(domain_name)) {
5664 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5673 * Usage for 'net rpc trustdom' command
5675 * @param argc standard argc
5676 * @param argv standard argv without inital components
5678 * @return Integer status returned to shell
5681 static int rpc_trustdom_usage(int argc, const char **argv)
5683 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5684 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5685 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5686 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5687 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5688 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5693 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5694 const char *domain_name,
5695 struct cli_state *cli,
5696 struct rpc_pipe_client *pipe_hnd,
5697 TALLOC_CTX *mem_ctx,
5702 sid_to_string(str_sid, domain_sid);
5703 d_printf("%s\n", str_sid);
5704 return NT_STATUS_OK;
5707 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5709 fstring ascii_sid, padding;
5710 int pad_len, col_len = 20;
5712 /* convert sid into ascii string */
5713 sid_to_string(ascii_sid, dom_sid);
5715 /* calculate padding space for d_printf to look nicer */
5716 pad_len = col_len - strlen(trusted_dom_name);
5717 padding[pad_len] = 0;
5718 do padding[--pad_len] = ' '; while (pad_len);
5720 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5723 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5724 TALLOC_CTX *mem_ctx,
5727 const char *trusted_dom_name)
5730 LSA_TRUSTED_DOMAIN_INFO *info;
5731 char *cleartextpwd = NULL;
5734 nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5736 if (NT_STATUS_IS_ERR(nt_status)) {
5737 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5738 nt_errstr(nt_status)));
5742 data = data_blob(NULL, info->password.password.length);
5744 memcpy(data.data, info->password.password.data, info->password.password.length);
5745 data.length = info->password.password.length;
5747 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5749 if (cleartextpwd == NULL) {
5750 DEBUG(0,("retrieved NULL password\n"));
5751 nt_status = NT_STATUS_UNSUCCESSFUL;
5755 if (!secrets_store_trusted_domain_password(trusted_dom_name,
5758 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5759 nt_status = NT_STATUS_UNSUCCESSFUL;
5763 #ifdef DEBUG_PASSWORD
5764 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5765 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5769 SAFE_FREE(cleartextpwd);
5770 data_blob_free(&data);
5775 static int rpc_trustdom_vampire(int argc, const char **argv)
5777 /* common variables */
5778 TALLOC_CTX* mem_ctx;
5779 struct cli_state *cli = NULL;
5780 struct rpc_pipe_client *pipe_hnd = NULL;
5782 const char *domain_name = NULL;
5783 DOM_SID *queried_dom_sid;
5784 POLICY_HND connect_hnd;
5786 /* trusted domains listing variables */
5787 unsigned int num_domains, enum_ctx = 0;
5789 DOM_SID *domain_sids;
5790 char **trusted_dom_names;
5795 * Listing trusted domains (stored in secrets.tdb, if local)
5798 mem_ctx = talloc_init("trust relationships vampire");
5801 * set domain and pdc name to local samba server (default)
5802 * or to remote one given in command line
5805 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5806 domain_name = opt_workgroup;
5807 opt_target_workgroup = opt_workgroup;
5809 fstrcpy(pdc_name, global_myname());
5810 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5811 opt_target_workgroup = domain_name;
5814 /* open \PIPE\lsarpc and open policy handle */
5815 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5816 DEBUG(0, ("Couldn't connect to domain controller\n"));
5820 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5822 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5823 nt_errstr(nt_status) ));
5828 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5830 if (NT_STATUS_IS_ERR(nt_status)) {
5831 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5832 nt_errstr(nt_status)));
5837 /* query info level 5 to obtain sid of a domain being queried */
5838 nt_status = rpccli_lsa_query_info_policy(
5839 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5840 &dummy, &queried_dom_sid);
5842 if (NT_STATUS_IS_ERR(nt_status)) {
5843 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5844 nt_errstr(nt_status)));
5850 * Keep calling LsaEnumTrustdom over opened pipe until
5851 * the end of enumeration is reached
5854 d_printf("Vampire trusted domains:\n\n");
5857 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5859 &trusted_dom_names, &domain_sids);
5861 if (NT_STATUS_IS_ERR(nt_status)) {
5862 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5863 nt_errstr(nt_status)));
5868 for (i = 0; i < num_domains; i++) {
5870 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5872 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5873 domain_sids[i], trusted_dom_names[i]);
5874 if (!NT_STATUS_IS_OK(nt_status)) {
5881 * in case of no trusted domains say something rather
5882 * than just display blank line
5884 if (!num_domains) d_printf("none\n");
5886 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5888 /* close this connection before doing next one */
5889 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5890 if (NT_STATUS_IS_ERR(nt_status)) {
5891 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5892 nt_errstr(nt_status)));
5897 /* close lsarpc pipe and connection to IPC$ */
5900 talloc_destroy(mem_ctx);
5904 static int rpc_trustdom_list(int argc, const char **argv)
5906 /* common variables */
5907 TALLOC_CTX* mem_ctx;
5908 struct cli_state *cli = NULL, *remote_cli = NULL;
5909 struct rpc_pipe_client *pipe_hnd = NULL;
5911 const char *domain_name = NULL;
5912 DOM_SID *queried_dom_sid;
5914 int ascii_dom_name_len;
5915 POLICY_HND connect_hnd;
5917 /* trusted domains listing variables */
5918 unsigned int num_domains, enum_ctx = 0;
5919 int i, pad_len, col_len = 20;
5920 DOM_SID *domain_sids;
5921 char **trusted_dom_names;
5925 /* trusting domains listing variables */
5926 POLICY_HND domain_hnd;
5927 char **trusting_dom_names;
5928 uint32 *trusting_dom_rids;
5931 * Listing trusted domains (stored in secrets.tdb, if local)
5934 mem_ctx = talloc_init("trust relationships listing");
5937 * set domain and pdc name to local samba server (default)
5938 * or to remote one given in command line
5941 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5942 domain_name = opt_workgroup;
5943 opt_target_workgroup = opt_workgroup;
5945 fstrcpy(pdc_name, global_myname());
5946 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5947 opt_target_workgroup = domain_name;
5950 /* open \PIPE\lsarpc and open policy handle */
5951 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5952 DEBUG(0, ("Couldn't connect to domain controller\n"));
5956 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5958 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5959 nt_errstr(nt_status) ));
5963 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5965 if (NT_STATUS_IS_ERR(nt_status)) {
5966 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5967 nt_errstr(nt_status)));
5971 /* query info level 5 to obtain sid of a domain being queried */
5972 nt_status = rpccli_lsa_query_info_policy(
5973 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5974 &dummy, &queried_dom_sid);
5976 if (NT_STATUS_IS_ERR(nt_status)) {
5977 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5978 nt_errstr(nt_status)));
5983 * Keep calling LsaEnumTrustdom over opened pipe until
5984 * the end of enumeration is reached
5987 d_printf("Trusted domains list:\n\n");
5990 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5992 &trusted_dom_names, &domain_sids);
5994 if (NT_STATUS_IS_ERR(nt_status)) {
5995 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5996 nt_errstr(nt_status)));
6000 for (i = 0; i < num_domains; i++) {
6001 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6005 * in case of no trusted domains say something rather
6006 * than just display blank line
6008 if (!num_domains) d_printf("none\n");
6010 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6012 /* close this connection before doing next one */
6013 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
6014 if (NT_STATUS_IS_ERR(nt_status)) {
6015 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6016 nt_errstr(nt_status)));
6020 cli_rpc_pipe_close(pipe_hnd);
6023 * Listing trusting domains (stored in passdb backend, if local)
6026 d_printf("\nTrusting domains list:\n\n");
6029 * Open \PIPE\samr and get needed policy handles
6031 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6033 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6038 nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
6040 if (!NT_STATUS_IS_OK(nt_status)) {
6041 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6042 nt_errstr(nt_status)));
6046 /* SamrOpenDomain - we have to open domain policy handle in order to be
6047 able to enumerate accounts*/
6048 nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
6049 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6050 queried_dom_sid, &domain_hnd);
6051 if (!NT_STATUS_IS_OK(nt_status)) {
6052 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6053 nt_errstr(nt_status)));
6058 * perform actual enumeration
6061 enum_ctx = 0; /* reset enumeration context from last enumeration */
6064 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6065 &enum_ctx, ACB_DOMTRUST, 0xffff,
6066 &trusting_dom_names, &trusting_dom_rids,
6068 if (NT_STATUS_IS_ERR(nt_status)) {
6069 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6070 nt_errstr(nt_status)));
6074 for (i = 0; i < num_domains; i++) {
6077 * get each single domain's sid (do we _really_ need this ?):
6078 * 1) connect to domain's pdc
6079 * 2) query the pdc for domain's sid
6082 /* get rid of '$' tail */
6083 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6084 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6085 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6087 /* calculate padding space for d_printf to look nicer */
6088 pad_len = col_len - strlen(trusting_dom_names[i]);
6089 padding[pad_len] = 0;
6090 do padding[--pad_len] = ' '; while (pad_len);
6092 /* set opt_* variables to remote domain */
6093 strupper_m(trusting_dom_names[i]);
6094 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6095 opt_target_workgroup = opt_workgroup;
6097 d_printf("%s%s", trusting_dom_names[i], padding);
6099 /* connect to remote domain controller */
6100 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6102 /* query for domain's sid */
6103 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6104 d_fprintf(stderr, "couldn't get domain's sid\n");
6106 cli_shutdown(remote_cli);
6109 d_fprintf(stderr, "domain controller is not responding\n");
6113 if (!num_domains) d_printf("none\n");
6115 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6117 /* close opened samr and domain policy handles */
6118 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6119 if (!NT_STATUS_IS_OK(nt_status)) {
6120 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6123 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6124 if (!NT_STATUS_IS_OK(nt_status)) {
6125 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6128 /* close samr pipe and connection to IPC$ */
6131 talloc_destroy(mem_ctx);
6136 * Entrypoint for 'net rpc trustdom' code
6138 * @param argc standard argc
6139 * @param argv standard argv without initial components
6141 * @return Integer status (0 means success)
6144 static int rpc_trustdom(int argc, const char **argv)
6146 struct functable func[] = {
6147 {"add", rpc_trustdom_add},
6148 {"del", rpc_trustdom_del},
6149 {"establish", rpc_trustdom_establish},
6150 {"revoke", rpc_trustdom_revoke},
6151 {"help", rpc_trustdom_usage},
6152 {"list", rpc_trustdom_list},
6153 {"vampire", rpc_trustdom_vampire},
6158 rpc_trustdom_usage(argc, argv);
6162 return (net_run_function(argc, argv, func, rpc_user_usage));
6166 * Check if a server will take rpc commands
6167 * @param flags Type of server to connect to (PDC, DMB, localhost)
6168 * if the host is not explicitly specified
6169 * @return BOOL (true means rpc supported)
6171 BOOL net_rpc_check(unsigned flags)
6173 struct cli_state cli;
6175 struct in_addr server_ip;
6176 char *server_name = NULL;
6178 /* flags (i.e. server type) may depend on command */
6179 if (!net_find_server(flags, &server_ip, &server_name))
6183 if (cli_initialise(&cli) == False)
6186 if (!cli_connect(&cli, server_name, &server_ip))
6188 if (!attempt_netbios_session_request(&cli, global_myname(),
6189 server_name, &server_ip))
6191 if (!cli_negprot(&cli))
6193 if (cli.protocol < PROTOCOL_NT1)
6202 /* dump sam database via samsync rpc calls */
6203 static int rpc_samdump(int argc, const char **argv) {
6204 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6208 /* syncronise sam database via samsync rpc calls */
6209 static int rpc_vampire(int argc, const char **argv) {
6210 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6215 * Migrate everything from a print-server
6217 * @param argc Standard main() style argc
6218 * @param argv Standard main() style argv. Initial components are already
6221 * @return A shell status integer (0 for success)
6223 * The order is important !
6224 * To successfully add drivers the print-queues have to exist !
6225 * Applying ACLs should be the last step, because you're easily locked out
6228 static int rpc_printer_migrate_all(int argc, const char **argv)
6233 printf("no server to migrate\n");
6237 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6241 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6245 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6249 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6253 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6258 * Migrate print-drivers from a print-server
6260 * @param argc Standard main() style argc
6261 * @param argv Standard main() style argv. Initial components are already
6264 * @return A shell status integer (0 for success)
6266 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6269 printf("no server to migrate\n");
6273 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6274 rpc_printer_migrate_drivers_internals,
6279 * Migrate print-forms from a print-server
6281 * @param argc Standard main() style argc
6282 * @param argv Standard main() style argv. Initial components are already
6285 * @return A shell status integer (0 for success)
6287 static int rpc_printer_migrate_forms(int argc, const char **argv)
6290 printf("no server to migrate\n");
6294 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6295 rpc_printer_migrate_forms_internals,
6300 * Migrate printers from a print-server
6302 * @param argc Standard main() style argc
6303 * @param argv Standard main() style argv. Initial components are already
6306 * @return A shell status integer (0 for success)
6308 static int rpc_printer_migrate_printers(int argc, const char **argv)
6311 printf("no server to migrate\n");
6315 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6316 rpc_printer_migrate_printers_internals,
6321 * Migrate printer-ACLs from a print-server
6323 * @param argc Standard main() style argc
6324 * @param argv Standard main() style argv. Initial components are already
6327 * @return A shell status integer (0 for success)
6329 static int rpc_printer_migrate_security(int argc, const char **argv)
6332 printf("no server to migrate\n");
6336 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6337 rpc_printer_migrate_security_internals,
6342 * Migrate printer-settings from a print-server
6344 * @param argc Standard main() style argc
6345 * @param argv Standard main() style argv. Initial components are already
6348 * @return A shell status integer (0 for success)
6350 static int rpc_printer_migrate_settings(int argc, const char **argv)
6353 printf("no server to migrate\n");
6357 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6358 rpc_printer_migrate_settings_internals,
6363 * 'net rpc printer' entrypoint.
6364 * @param argc Standard main() style argc
6365 * @param argv Standard main() style argv. Initial components are already
6369 int rpc_printer_migrate(int argc, const char **argv)
6372 /* ouch: when addriver and setdriver are called from within
6373 rpc_printer_migrate_drivers_internals, the printer-queue already
6376 struct functable func[] = {
6377 {"all", rpc_printer_migrate_all},
6378 {"drivers", rpc_printer_migrate_drivers},
6379 {"forms", rpc_printer_migrate_forms},
6380 {"help", rpc_printer_usage},
6381 {"printers", rpc_printer_migrate_printers},
6382 {"security", rpc_printer_migrate_security},
6383 {"settings", rpc_printer_migrate_settings},
6387 return net_run_function(argc, argv, func, rpc_printer_usage);
6392 * List printers on a remote RPC server
6394 * @param argc Standard main() style argc
6395 * @param argv Standard main() style argv. Initial components are already
6398 * @return A shell status integer (0 for success)
6400 static int rpc_printer_list(int argc, const char **argv)
6403 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6404 rpc_printer_list_internals,
6409 * List printer-drivers on a remote RPC server
6411 * @param argc Standard main() style argc
6412 * @param argv Standard main() style argv. Initial components are already
6415 * @return A shell status integer (0 for success)
6417 static int rpc_printer_driver_list(int argc, const char **argv)
6420 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6421 rpc_printer_driver_list_internals,
6426 * Publish printer in ADS via MSRPC
6428 * @param argc Standard main() style argc
6429 * @param argv Standard main() style argv. Initial components are already
6432 * @return A shell status integer (0 for success)
6434 static int rpc_printer_publish_publish(int argc, const char **argv)
6437 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6438 rpc_printer_publish_publish_internals,
6443 * Update printer in ADS via MSRPC
6445 * @param argc Standard main() style argc
6446 * @param argv Standard main() style argv. Initial components are already
6449 * @return A shell status integer (0 for success)
6451 static int rpc_printer_publish_update(int argc, const char **argv)
6454 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6455 rpc_printer_publish_update_internals,
6460 * UnPublish printer in ADS via MSRPC
6462 * @param argc Standard main() style argc
6463 * @param argv Standard main() style argv. Initial components are already
6466 * @return A shell status integer (0 for success)
6468 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6471 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6472 rpc_printer_publish_unpublish_internals,
6477 * List published printers via MSRPC
6479 * @param argc Standard main() style argc
6480 * @param argv Standard main() style argv. Initial components are already
6483 * @return A shell status integer (0 for success)
6485 static int rpc_printer_publish_list(int argc, const char **argv)
6488 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6489 rpc_printer_publish_list_internals,
6495 * Publish printer in ADS
6497 * @param argc Standard main() style argc
6498 * @param argv Standard main() style argv. Initial components are already
6501 * @return A shell status integer (0 for success)
6503 static int rpc_printer_publish(int argc, const char **argv)
6506 struct functable func[] = {
6507 {"publish", rpc_printer_publish_publish},
6508 {"update", rpc_printer_publish_update},
6509 {"unpublish", rpc_printer_publish_unpublish},
6510 {"list", rpc_printer_publish_list},
6511 {"help", rpc_printer_usage},
6516 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6517 rpc_printer_publish_list_internals,
6520 return net_run_function(argc, argv, func, rpc_printer_usage);
6526 * Display rpc printer help page.
6527 * @param argc Standard main() style argc
6528 * @param argv Standard main() style argv. Initial components are already
6531 int rpc_printer_usage(int argc, const char **argv)
6533 return net_help_printer(argc, argv);
6537 * 'net rpc printer' entrypoint.
6538 * @param argc Standard main() style argc
6539 * @param argv Standard main() style argv. Initial components are already
6542 int net_rpc_printer(int argc, const char **argv)
6544 struct functable func[] = {
6545 {"list", rpc_printer_list},
6546 {"migrate", rpc_printer_migrate},
6547 {"driver", rpc_printer_driver_list},
6548 {"publish", rpc_printer_publish},
6553 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6554 rpc_printer_list_internals,
6557 return net_run_function(argc, argv, func, rpc_printer_usage);
6560 /****************************************************************************/
6564 * Basic usage function for 'net rpc'
6565 * @param argc Standard main() style argc
6566 * @param argv Standard main() style argv. Initial components are already
6570 int net_rpc_usage(int argc, const char **argv)
6572 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6573 d_printf(" net rpc join \t\t\tto join a domain \n");
6574 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6575 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6576 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6577 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6578 d_printf(" net rpc group \t\tto list groups\n");
6579 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6580 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6581 d_printf(" net rpc file \t\t\tto list open files\n");
6582 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6583 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6584 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6585 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6586 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6587 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6588 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6589 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6590 d_printf(" net rpc registry\t\tto manage registry hives\n");
6591 d_printf(" net rpc service\t\tto start, stop and query services\n");
6593 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6594 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6595 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6596 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6597 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6603 * Help function for 'net rpc'. Calls command specific help if requested
6604 * or displays usage of net rpc
6605 * @param argc Standard main() style argc
6606 * @param argv Standard main() style argv. Initial components are already
6610 int net_rpc_help(int argc, const char **argv)
6612 struct functable func[] = {
6613 {"join", rpc_join_usage},
6614 {"user", rpc_user_usage},
6615 {"group", rpc_group_usage},
6616 {"share", rpc_share_usage},
6617 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6618 {"trustdom", rpc_trustdom_usage},
6619 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6620 /*{"shutdown", rpc_shutdown_usage}, */
6621 {"vampire", rpc_vampire_usage},
6626 net_rpc_usage(argc, argv);
6630 return (net_run_function(argc, argv, func, rpc_user_usage));
6634 * 'net rpc' entrypoint.
6635 * @param argc Standard main() style argc
6636 * @param argv Standard main() style argv. Initial components are already
6640 int net_rpc(int argc, const char **argv)
6642 struct functable func[] = {
6643 {"info", net_rpc_info},
6644 {"join", net_rpc_join},
6645 {"oldjoin", net_rpc_oldjoin},
6646 {"testjoin", net_rpc_testjoin},
6647 {"user", net_rpc_user},
6648 {"password", rpc_user_password},
6649 {"group", net_rpc_group},
6650 {"share", net_rpc_share},
6651 {"file", net_rpc_file},
6652 {"printer", net_rpc_printer},
6653 {"changetrustpw", net_rpc_changetrustpw},
6654 {"trustdom", rpc_trustdom},
6655 {"abortshutdown", rpc_shutdown_abort},
6656 {"shutdown", rpc_shutdown},
6657 {"samdump", rpc_samdump},
6658 {"vampire", rpc_vampire},
6659 {"getsid", net_rpc_getsid},
6660 {"rights", net_rpc_rights},
6661 {"service", net_rpc_service},
6662 {"registry", net_rpc_registry},
6663 {"shell", net_rpc_shell},
6664 {"help", net_rpc_help},
6667 return net_run_function(argc, argv, func, net_rpc_usage);