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 (lp_server_role() == ROLE_STANDALONE) {
393 d_printf("cannot join as standalone machine\n");
397 if (strlen(global_myname()) > 15) {
398 d_printf("Our netbios name can be at most 15 chars long, "
399 "\"%s\" is %u chars long\n",
400 global_myname(), (unsigned int)strlen(global_myname()));
404 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
407 return net_rpc_join_newstyle(argc, argv);
411 * display info about a rpc domain
413 * All parameters are provided by the run_rpc_command function, except for
414 * argc, argv which are passed through.
416 * @param domain_sid The domain sid acquired from the remote server
417 * @param cli A cli_state connected to the server.
418 * @param mem_ctx Talloc context, destoyed on completion of the function.
419 * @param argc Standard main() style argc
420 * @param argv Standard main() style argv. Initial components are already
423 * @return Normal NTSTATUS return.
426 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
427 const char *domain_name,
428 struct cli_state *cli,
429 struct rpc_pipe_client *pipe_hnd,
434 POLICY_HND connect_pol, domain_pol;
435 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
439 sid_to_string(sid_str, domain_sid);
441 /* Get sam policy handle */
442 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
444 if (!NT_STATUS_IS_OK(result)) {
445 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
449 /* Get domain policy handle */
450 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
451 MAXIMUM_ALLOWED_ACCESS,
452 domain_sid, &domain_pol);
453 if (!NT_STATUS_IS_OK(result)) {
454 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
459 result = rpccli_samr_query_dom_info(pipe_hnd, mem_ctx, &domain_pol,
461 if (NT_STATUS_IS_OK(result)) {
462 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
463 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
464 d_printf("Domain SID: %s\n", sid_str);
465 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num.low);
466 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
467 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
468 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
477 * 'net rpc info' entrypoint.
478 * @param argc Standard main() style argc
479 * @param argc Standard main() style argv. Initial components are already
483 int net_rpc_info(int argc, const char **argv)
485 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC,
491 * Fetch domain SID into the local secrets.tdb
493 * All parameters are provided by the run_rpc_command function, except for
494 * argc, argv which are passes through.
496 * @param domain_sid The domain sid acquired from the remote server
497 * @param cli A cli_state connected to the server.
498 * @param mem_ctx Talloc context, destoyed on completion of the function.
499 * @param argc Standard main() style argc
500 * @param argv Standard main() style argv. Initial components are already
503 * @return Normal NTSTATUS return.
506 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
507 const char *domain_name,
508 struct cli_state *cli,
509 struct rpc_pipe_client *pipe_hnd,
516 sid_to_string(sid_str, domain_sid);
517 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
518 sid_str, domain_name);
520 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
521 DEBUG(0,("Can't store domain SID\n"));
522 return NT_STATUS_UNSUCCESSFUL;
529 * 'net rpc getsid' entrypoint.
530 * @param argc Standard main() style argc
531 * @param argc Standard main() style argv. Initial components are already
535 int net_rpc_getsid(int argc, const char **argv)
537 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
538 rpc_getsid_internals,
542 /****************************************************************************/
545 * Basic usage function for 'net rpc user'
546 * @param argc Standard main() style argc.
547 * @param argv Standard main() style argv. Initial components are already
551 static int rpc_user_usage(int argc, const char **argv)
553 return net_help_user(argc, argv);
557 * Add a new user to a remote RPC server
559 * All parameters are provided by the run_rpc_command function, except for
560 * argc, argv which are passes through.
562 * @param domain_sid The domain sid acquired from the remote server
563 * @param cli A cli_state connected to the server.
564 * @param mem_ctx Talloc context, destoyed on completion of the function.
565 * @param argc Standard main() style argc
566 * @param argv Standard main() style argv. Initial components are already
569 * @return Normal NTSTATUS return.
572 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
573 const char *domain_name,
574 struct cli_state *cli,
575 struct rpc_pipe_client *pipe_hnd,
577 int argc, const char **argv)
580 POLICY_HND connect_pol, domain_pol, user_pol;
581 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
582 const char *acct_name;
584 uint32 unknown, user_rid;
587 d_printf("User must be specified\n");
588 rpc_user_usage(argc, argv);
594 /* Get sam policy handle */
596 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
598 if (!NT_STATUS_IS_OK(result)) {
602 /* Get domain policy handle */
604 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
605 MAXIMUM_ALLOWED_ACCESS,
606 domain_sid, &domain_pol);
607 if (!NT_STATUS_IS_OK(result)) {
611 /* Create domain user */
613 acb_info = ACB_NORMAL;
614 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
616 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
617 acct_name, acb_info, unknown,
618 &user_pol, &user_rid);
619 if (!NT_STATUS_IS_OK(result)) {
625 uint32 *user_rids, num_rids, *name_types;
626 uint32 flags = 0x000003e8; /* Unknown */
627 SAM_USERINFO_CTR ctr;
628 SAM_USER_INFO_24 p24;
631 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
632 flags, 1, &acct_name,
633 &num_rids, &user_rids,
636 if (!NT_STATUS_IS_OK(result)) {
640 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
641 MAXIMUM_ALLOWED_ACCESS,
642 user_rids[0], &user_pol);
644 if (!NT_STATUS_IS_OK(result)) {
648 /* Set password on account */
653 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
655 init_sam_user_info24(&p24, (char *)pwbuf,24);
657 ctr.switch_value = 24;
658 ctr.info.id24 = &p24;
660 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
661 &cli->user_session_key, &ctr);
663 if (!NT_STATUS_IS_OK(result)) {
664 d_fprintf(stderr, "Failed to set password for user %s - %s\n",
665 acct_name, nt_errstr(result));
667 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
669 if (!NT_STATUS_IS_OK(result)) {
670 d_fprintf(stderr, "Failed to delete user %s - %s\n",
671 acct_name, nt_errstr(result));
678 if (!NT_STATUS_IS_OK(result)) {
679 d_fprintf(stderr, "Failed to add user %s - %s\n", acct_name,
682 d_printf("Added user %s\n", acct_name);
688 * Add a new user to a remote RPC server
690 * @param argc Standard main() style argc
691 * @param argv Standard main() style argv. Initial components are already
694 * @return A shell status integer (0 for success)
697 static int rpc_user_add(int argc, const char **argv)
699 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
704 * Delete a user from a remote RPC server
706 * All parameters are provided by the run_rpc_command function, except for
707 * argc, argv which are passes through.
709 * @param domain_sid The domain sid acquired from the remote server
710 * @param cli A cli_state connected to the server.
711 * @param mem_ctx Talloc context, destoyed on completion of the function.
712 * @param argc Standard main() style argc
713 * @param argv Standard main() style argv. Initial components are already
716 * @return Normal NTSTATUS return.
719 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
720 const char *domain_name,
721 struct cli_state *cli,
722 struct rpc_pipe_client *pipe_hnd,
727 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
728 POLICY_HND connect_pol, domain_pol, user_pol;
731 d_printf("User must be specified\n");
732 rpc_user_usage(argc, argv);
735 /* Get sam policy and domain handles */
737 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
740 if (!NT_STATUS_IS_OK(result)) {
744 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
745 MAXIMUM_ALLOWED_ACCESS,
746 domain_sid, &domain_pol);
748 if (!NT_STATUS_IS_OK(result)) {
752 /* Get handle on user */
755 uint32 *user_rids, num_rids, *name_types;
756 uint32 flags = 0x000003e8; /* Unknown */
758 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
760 &num_rids, &user_rids,
763 if (!NT_STATUS_IS_OK(result)) {
767 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
768 MAXIMUM_ALLOWED_ACCESS,
769 user_rids[0], &user_pol);
771 if (!NT_STATUS_IS_OK(result)) {
778 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
780 if (!NT_STATUS_IS_OK(result)) {
784 /* Display results */
785 if (!NT_STATUS_IS_OK(result)) {
786 d_fprintf(stderr, "Failed to delete user account - %s\n", nt_errstr(result));
788 d_printf("Deleted user account\n");
796 * Rename a user on a remote RPC server
798 * All parameters are provided by the run_rpc_command function, except for
799 * argc, argv which are passes through.
801 * @param domain_sid The domain sid acquired from the remote server
802 * @param cli A cli_state connected to the server.
803 * @param mem_ctx Talloc context, destoyed on completion of the function.
804 * @param argc Standard main() style argc
805 * @param argv Standard main() style argv. Initial components are already
808 * @return Normal NTSTATUS return.
811 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
812 const char *domain_name,
813 struct cli_state *cli,
814 struct rpc_pipe_client *pipe_hnd,
819 POLICY_HND connect_pol, domain_pol, user_pol;
820 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
821 uint32 info_level = 7;
822 const char *old_name, *new_name;
824 uint32 flags = 0x000003e8; /* Unknown */
825 uint32 num_rids, *name_types;
826 uint32 num_names = 1;
828 SAM_USERINFO_CTR *user_ctr;
829 SAM_USERINFO_CTR ctr;
830 SAM_USER_INFO_7 info7;
833 d_printf("Old and new username must be specified\n");
834 rpc_user_usage(argc, argv);
842 ZERO_STRUCT(user_ctr);
844 /* Get sam policy handle */
846 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
848 if (!NT_STATUS_IS_OK(result)) {
852 /* Get domain policy handle */
854 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
855 MAXIMUM_ALLOWED_ACCESS,
856 domain_sid, &domain_pol);
857 if (!NT_STATUS_IS_OK(result)) {
861 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
863 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
864 flags, num_names, names,
865 &num_rids, &user_rid, &name_types);
866 if (!NT_STATUS_IS_OK(result)) {
870 /* Open domain user */
871 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
872 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
874 if (!NT_STATUS_IS_OK(result)) {
878 /* Query user info */
879 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
880 info_level, &user_ctr);
882 if (!NT_STATUS_IS_OK(result)) {
886 ctr.switch_value = info_level;
887 ctr.info.id7 = &info7;
889 init_sam_user_info7(&info7, new_name);
892 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
893 info_level, &cli->user_session_key, &ctr);
895 if (!NT_STATUS_IS_OK(result)) {
900 if (!NT_STATUS_IS_OK(result)) {
901 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
904 d_printf("Renamed user from %s to %s\n", old_name, new_name);
910 * Rename a user on a remote RPC server
912 * @param argc Standard main() style argc
913 * @param argv Standard main() style argv. Initial components are already
916 * @return A shell status integer (0 for success)
919 static int rpc_user_rename(int argc, const char **argv)
921 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
926 * Delete a user from a remote RPC server
928 * @param argc Standard main() style argc
929 * @param argv Standard main() style argv. Initial components are already
932 * @return A shell status integer (0 for success)
935 static int rpc_user_delete(int argc, const char **argv)
937 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
942 * Set a password for a user on a remote RPC server
944 * All parameters are provided by the run_rpc_command function, except for
945 * argc, argv which are passes through.
947 * @param domain_sid The domain sid acquired from the remote server
948 * @param cli A cli_state connected to the server.
949 * @param mem_ctx Talloc context, destoyed on completion of the function.
950 * @param argc Standard main() style argc
951 * @param argv Standard main() style argv. Initial components are already
954 * @return Normal NTSTATUS return.
957 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
958 const char *domain_name,
959 struct cli_state *cli,
960 struct rpc_pipe_client *pipe_hnd,
965 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
966 POLICY_HND connect_pol, domain_pol, user_pol;
967 SAM_USERINFO_CTR ctr;
968 SAM_USER_INFO_24 p24;
971 const char *new_password;
975 d_printf("User must be specified\n");
976 rpc_user_usage(argc, argv);
983 new_password = argv[1];
985 asprintf(&prompt, "Enter new password for %s:", user);
986 new_password = getpass(prompt);
990 /* Get sam policy and domain handles */
992 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
995 if (!NT_STATUS_IS_OK(result)) {
999 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1000 MAXIMUM_ALLOWED_ACCESS,
1001 domain_sid, &domain_pol);
1003 if (!NT_STATUS_IS_OK(result)) {
1007 /* Get handle on user */
1010 uint32 *user_rids, num_rids, *name_types;
1011 uint32 flags = 0x000003e8; /* Unknown */
1013 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1015 &num_rids, &user_rids,
1018 if (!NT_STATUS_IS_OK(result)) {
1022 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1023 MAXIMUM_ALLOWED_ACCESS,
1024 user_rids[0], &user_pol);
1026 if (!NT_STATUS_IS_OK(result)) {
1031 /* Set password on account */
1036 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
1038 init_sam_user_info24(&p24, (char *)pwbuf,24);
1040 ctr.switch_value = 24;
1041 ctr.info.id24 = &p24;
1043 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
1044 &cli->user_session_key, &ctr);
1046 if (!NT_STATUS_IS_OK(result)) {
1050 /* Display results */
1058 * Set a user's password on a remote RPC server
1060 * @param argc Standard main() style argc
1061 * @param argv Standard main() style argv. Initial components are already
1064 * @return A shell status integer (0 for success)
1067 static int rpc_user_password(int argc, const char **argv)
1069 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1074 * List user's groups on a remote RPC server
1076 * All parameters are provided by the run_rpc_command function, except for
1077 * argc, argv which are passes through.
1079 * @param domain_sid The domain sid acquired from the remote server
1080 * @param cli A cli_state connected to the server.
1081 * @param mem_ctx Talloc context, destoyed on completion of the function.
1082 * @param argc Standard main() style argc
1083 * @param argv Standard main() style argv. Initial components are already
1086 * @return Normal NTSTATUS return.
1089 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1090 const char *domain_name,
1091 struct cli_state *cli,
1092 struct rpc_pipe_client *pipe_hnd,
1093 TALLOC_CTX *mem_ctx,
1097 POLICY_HND connect_pol, domain_pol, user_pol;
1098 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1099 uint32 *rids, num_rids, *name_types, num_names;
1100 uint32 flags = 0x000003e8; /* Unknown */
1106 d_printf("User must be specified\n");
1107 rpc_user_usage(argc, argv);
1108 return NT_STATUS_OK;
1110 /* Get sam policy handle */
1112 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1114 if (!NT_STATUS_IS_OK(result)) goto done;
1116 /* Get domain policy handle */
1118 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1119 MAXIMUM_ALLOWED_ACCESS,
1120 domain_sid, &domain_pol);
1121 if (!NT_STATUS_IS_OK(result)) goto done;
1123 /* Get handle on user */
1125 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1127 &num_rids, &rids, &name_types);
1129 if (!NT_STATUS_IS_OK(result)) goto done;
1131 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1132 MAXIMUM_ALLOWED_ACCESS,
1133 rids[0], &user_pol);
1134 if (!NT_STATUS_IS_OK(result)) goto done;
1136 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1137 &num_rids, &user_gids);
1139 if (!NT_STATUS_IS_OK(result)) goto done;
1144 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1146 for (i = 0; i < num_rids; i++)
1147 rids[i] = user_gids[i].g_rid;
1149 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1151 &num_names, &names, &name_types);
1153 if (!NT_STATUS_IS_OK(result)) {
1157 /* Display results */
1159 for (i = 0; i < num_names; i++)
1160 printf("%s\n", names[i]);
1167 * List a user's groups from a remote RPC server
1169 * @param argc Standard main() style argc
1170 * @param argv Standard main() style argv. Initial components are already
1173 * @return A shell status integer (0 for success)
1176 static int rpc_user_info(int argc, const char **argv)
1178 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1183 * List users on a remote RPC server
1185 * All parameters are provided by the run_rpc_command function, except for
1186 * argc, argv which are passes through.
1188 * @param domain_sid The domain sid acquired from the remote server
1189 * @param cli A cli_state connected to the server.
1190 * @param mem_ctx Talloc context, destoyed on completion of the function.
1191 * @param argc Standard main() style argc
1192 * @param argv Standard main() style argv. Initial components are already
1195 * @return Normal NTSTATUS return.
1198 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1199 const char *domain_name,
1200 struct cli_state *cli,
1201 struct rpc_pipe_client *pipe_hnd,
1202 TALLOC_CTX *mem_ctx,
1206 POLICY_HND connect_pol, domain_pol;
1207 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1208 uint32 start_idx=0, num_entries, i, loop_count = 0;
1209 SAM_DISPINFO_CTR ctr;
1210 SAM_DISPINFO_1 info1;
1212 /* Get sam policy handle */
1214 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1216 if (!NT_STATUS_IS_OK(result)) {
1220 /* Get domain policy handle */
1222 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1223 MAXIMUM_ALLOWED_ACCESS,
1224 domain_sid, &domain_pol);
1225 if (!NT_STATUS_IS_OK(result)) {
1229 /* Query domain users */
1232 ctr.sam.info1 = &info1;
1233 if (opt_long_list_entries)
1234 d_printf("\nUser name Comment"\
1235 "\n-----------------------------\n");
1238 uint32 max_entries, max_size;
1240 get_query_dispinfo_params(
1241 loop_count, &max_entries, &max_size);
1243 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1244 &start_idx, 1, &num_entries,
1245 max_entries, max_size, &ctr);
1248 for (i = 0; i < num_entries; i++) {
1249 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1250 if (opt_long_list_entries)
1251 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1253 if (opt_long_list_entries)
1254 printf("%-21.21s %s\n", user, desc);
1256 printf("%s\n", user);
1258 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1265 * 'net rpc user' entrypoint.
1266 * @param argc Standard main() style argc
1267 * @param argc Standard main() style argv. Initial components are already
1271 int net_rpc_user(int argc, const char **argv)
1273 struct functable func[] = {
1274 {"add", rpc_user_add},
1275 {"info", rpc_user_info},
1276 {"delete", rpc_user_delete},
1277 {"password", rpc_user_password},
1278 {"rename", rpc_user_rename},
1283 return run_rpc_command(NULL,PI_SAMR, 0,
1284 rpc_user_list_internals,
1288 return net_run_function(argc, argv, func, rpc_user_usage);
1291 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1292 struct rpc_sh_ctx *ctx,
1293 struct rpc_pipe_client *pipe_hnd,
1294 int argc, const char **argv)
1296 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1297 ctx->cli, pipe_hnd, mem_ctx,
1301 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1302 struct rpc_sh_ctx *ctx,
1303 struct rpc_pipe_client *pipe_hnd,
1304 int argc, const char **argv)
1306 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1307 ctx->cli, pipe_hnd, mem_ctx,
1311 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1312 struct rpc_sh_ctx *ctx,
1313 struct rpc_pipe_client *pipe_hnd,
1314 int argc, const char **argv,
1316 TALLOC_CTX *mem_ctx,
1317 struct rpc_sh_ctx *ctx,
1318 struct rpc_pipe_client *pipe_hnd,
1319 const POLICY_HND *user_hnd,
1320 int argc, const char **argv))
1323 POLICY_HND connect_pol, domain_pol, user_pol;
1324 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1327 enum SID_NAME_USE type;
1330 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1331 return NT_STATUS_INVALID_PARAMETER;
1334 ZERO_STRUCT(connect_pol);
1335 ZERO_STRUCT(domain_pol);
1336 ZERO_STRUCT(user_pol);
1338 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1339 NULL, NULL, &sid, &type);
1340 if (!NT_STATUS_IS_OK(result)) {
1341 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1346 if (type != SID_NAME_USER) {
1347 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1348 sid_type_lookup(type));
1349 result = NT_STATUS_NO_SUCH_USER;
1353 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1354 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1355 result = NT_STATUS_NO_SUCH_USER;
1359 result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1360 MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1361 if (!NT_STATUS_IS_OK(result)) {
1365 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1366 MAXIMUM_ALLOWED_ACCESS,
1367 ctx->domain_sid, &domain_pol);
1368 if (!NT_STATUS_IS_OK(result)) {
1372 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1373 MAXIMUM_ALLOWED_ACCESS,
1375 if (!NT_STATUS_IS_OK(result)) {
1379 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1382 if (is_valid_policy_hnd(&user_pol)) {
1383 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1385 if (is_valid_policy_hnd(&domain_pol)) {
1386 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1388 if (is_valid_policy_hnd(&connect_pol)) {
1389 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1394 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1395 struct rpc_sh_ctx *ctx,
1396 struct rpc_pipe_client *pipe_hnd,
1397 const POLICY_HND *user_hnd,
1398 int argc, const char **argv)
1401 SAM_USERINFO_CTR *ctr;
1402 SAM_USER_INFO_21 *info;
1405 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1406 return NT_STATUS_INVALID_PARAMETER;
1409 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1411 if (!NT_STATUS_IS_OK(result)) {
1415 info = ctr->info.id21;
1417 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1423 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1424 struct rpc_sh_ctx *ctx,
1425 struct rpc_pipe_client *pipe_hnd,
1426 int argc, const char **argv)
1428 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1429 rpc_sh_user_show_internals);
1432 #define FETCHSTR(name, rec) \
1433 do { if (strequal(ctx->thiscmd, name)) { \
1434 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1437 #define SETSTR(name, rec, flag) \
1438 do { if (strequal(ctx->thiscmd, name)) { \
1439 init_unistr2(&usr->uni_##rec, argv[0], STR_TERMINATE); \
1440 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1441 usr->fields_present |= ACCT_##flag; } \
1444 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1445 struct rpc_sh_ctx *ctx,
1446 struct rpc_pipe_client *pipe_hnd,
1447 const POLICY_HND *user_hnd,
1448 int argc, const char **argv)
1451 SAM_USERINFO_CTR *ctr;
1452 SAM_USER_INFO_21 *usr;
1453 const char *username;
1454 const char *oldval = "";
1457 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1459 return NT_STATUS_INVALID_PARAMETER;
1462 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1464 if (!NT_STATUS_IS_OK(result)) {
1468 usr = ctr->info.id21;
1470 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1472 FETCHSTR("fullname", full_name);
1473 FETCHSTR("homedir", home_dir);
1474 FETCHSTR("homedrive", dir_drive);
1475 FETCHSTR("logonscript", logon_script);
1476 FETCHSTR("profilepath", profile_path);
1477 FETCHSTR("description", acct_desc);
1480 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1486 if (strcmp(argv[0], "NULL") == 0) {
1490 SETSTR("fullname", full_name, FULL_NAME);
1491 SETSTR("homedir", home_dir, HOME_DIR);
1492 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1493 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1494 SETSTR("profilepath", profile_path, PROFILE);
1495 SETSTR("description", acct_desc, DESCRIPTION);
1497 result = rpccli_samr_set_userinfo2(
1498 pipe_hnd, mem_ctx, user_hnd, 21,
1499 &pipe_hnd->cli->user_session_key, ctr);
1501 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1502 ctx->thiscmd, oldval, argv[0]);
1509 #define HANDLEFLG(name, rec) \
1510 do { if (strequal(ctx->thiscmd, name)) { \
1511 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1513 newflags = oldflags | ACB_##rec; \
1515 newflags = oldflags & ~ACB_##rec; \
1518 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1519 struct rpc_sh_ctx *ctx,
1520 struct rpc_pipe_client *pipe_hnd,
1521 int argc, const char **argv)
1523 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1524 rpc_sh_user_str_edit_internals);
1527 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1528 struct rpc_sh_ctx *ctx,
1529 struct rpc_pipe_client *pipe_hnd,
1530 const POLICY_HND *user_hnd,
1531 int argc, const char **argv)
1534 SAM_USERINFO_CTR *ctr;
1535 SAM_USER_INFO_21 *usr;
1536 const char *username;
1537 const char *oldval = "unknown";
1538 uint32 oldflags, newflags;
1542 ((argc == 1) && !strequal(argv[0], "yes") &&
1543 !strequal(argv[0], "no"))) {
1544 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1546 return NT_STATUS_INVALID_PARAMETER;
1549 newval = strequal(argv[0], "yes");
1551 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1553 if (!NT_STATUS_IS_OK(result)) {
1557 usr = ctr->info.id21;
1559 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1560 oldflags = usr->acb_info;
1561 newflags = usr->acb_info;
1563 HANDLEFLG("disabled", DISABLED);
1564 HANDLEFLG("pwnotreq", PWNOTREQ);
1565 HANDLEFLG("autolock", AUTOLOCK);
1566 HANDLEFLG("pwnoexp", PWNOEXP);
1569 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1575 usr->acb_info = newflags;
1576 usr->fields_present = ACCT_FLAGS;
1578 result = rpccli_samr_set_userinfo2(
1579 pipe_hnd, mem_ctx, user_hnd, 21,
1580 &pipe_hnd->cli->user_session_key, ctr);
1582 if (NT_STATUS_IS_OK(result)) {
1583 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1584 ctx->thiscmd, oldval, argv[0]);
1592 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1593 struct rpc_sh_ctx *ctx,
1594 struct rpc_pipe_client *pipe_hnd,
1595 int argc, const char **argv)
1597 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1598 rpc_sh_user_flag_edit_internals);
1601 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1602 struct rpc_sh_ctx *ctx)
1604 static struct rpc_sh_cmd cmds[] = {
1606 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1607 "Show/Set a user's full name" },
1609 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1610 "Show/Set a user's home directory" },
1612 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1613 "Show/Set a user's home drive" },
1615 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1616 "Show/Set a user's logon script" },
1618 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1619 "Show/Set a user's profile path" },
1621 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1622 "Show/Set a user's description" },
1624 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1625 "Show/Set whether a user is disabled" },
1627 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1628 "Show/Set whether a user locked out" },
1630 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1631 "Show/Set whether a user does not need a password" },
1633 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1634 "Show/Set whether a user's password does not expire" },
1636 { NULL, NULL, 0, NULL, NULL }
1642 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1643 struct rpc_sh_ctx *ctx)
1645 static struct rpc_sh_cmd cmds[] = {
1647 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1648 "List available users" },
1650 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1651 "List the domain groups a user is member of" },
1653 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1654 "Show info about a user" },
1656 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1657 "Show/Modify a user's fields" },
1659 { NULL, NULL, 0, NULL, NULL }
1665 /****************************************************************************/
1668 * Basic usage function for 'net rpc group'
1669 * @param argc Standard main() style argc.
1670 * @param argv Standard main() style argv. Initial components are already
1674 static int rpc_group_usage(int argc, const char **argv)
1676 return net_help_group(argc, argv);
1680 * Delete group on a remote RPC server
1682 * All parameters are provided by the run_rpc_command function, except for
1683 * argc, argv which are passes through.
1685 * @param domain_sid The domain sid acquired from the remote server
1686 * @param cli A cli_state connected to the server.
1687 * @param mem_ctx Talloc context, destoyed on completion of the function.
1688 * @param argc Standard main() style argc
1689 * @param argv Standard main() style argv. Initial components are already
1692 * @return Normal NTSTATUS return.
1695 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1696 const char *domain_name,
1697 struct cli_state *cli,
1698 struct rpc_pipe_client *pipe_hnd,
1699 TALLOC_CTX *mem_ctx,
1703 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1704 BOOL group_is_primary = False;
1705 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1707 uint32 *group_rids, num_rids, *name_types, num_members,
1708 *group_attrs, group_rid;
1709 uint32 flags = 0x000003e8; /* Unknown */
1712 /* DOM_GID *user_gids; */
1713 SAM_USERINFO_CTR *user_ctr;
1717 d_printf("specify group\n");
1718 rpc_group_usage(argc,argv);
1719 return NT_STATUS_OK; /* ok? */
1722 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1725 if (!NT_STATUS_IS_OK(result)) {
1726 d_fprintf(stderr, "Request samr_connect failed\n");
1730 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1731 MAXIMUM_ALLOWED_ACCESS,
1732 domain_sid, &domain_pol);
1734 if (!NT_STATUS_IS_OK(result)) {
1735 d_fprintf(stderr, "Request open_domain failed\n");
1739 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1741 &num_rids, &group_rids,
1744 if (!NT_STATUS_IS_OK(result)) {
1745 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1749 switch (name_types[0])
1751 case SID_NAME_DOM_GRP:
1752 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1753 MAXIMUM_ALLOWED_ACCESS,
1754 group_rids[0], &group_pol);
1755 if (!NT_STATUS_IS_OK(result)) {
1756 d_fprintf(stderr, "Request open_group failed");
1760 group_rid = group_rids[0];
1762 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1763 &num_members, &group_rids,
1766 if (!NT_STATUS_IS_OK(result)) {
1767 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1772 d_printf("Domain Group %s (rid: %d) has %d members\n",
1773 argv[0],group_rid,num_members);
1776 /* Check if group is anyone's primary group */
1777 for (i = 0; i < num_members; i++)
1779 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1780 MAXIMUM_ALLOWED_ACCESS,
1781 group_rids[i], &user_pol);
1783 if (!NT_STATUS_IS_OK(result)) {
1784 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1788 ZERO_STRUCT(user_ctr);
1790 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1793 if (!NT_STATUS_IS_OK(result)) {
1794 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1798 if (user_ctr->info.id21->group_rid == group_rid) {
1799 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1802 d_printf("Group is primary group of %s\n",temp);
1803 group_is_primary = True;
1806 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1809 if (group_is_primary) {
1810 d_fprintf(stderr, "Unable to delete group because some "
1811 "of it's members have it as primary group\n");
1812 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1816 /* remove all group members */
1817 for (i = 0; i < num_members; i++)
1820 d_printf("Remove group member %d...",group_rids[i]);
1821 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1823 if (NT_STATUS_IS_OK(result)) {
1828 d_printf("failed\n");
1833 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1836 /* removing a local group is easier... */
1837 case SID_NAME_ALIAS:
1838 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1839 MAXIMUM_ALLOWED_ACCESS,
1840 group_rids[0], &group_pol);
1842 if (!NT_STATUS_IS_OK(result)) {
1843 d_fprintf(stderr, "Request open_alias failed\n");
1847 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1850 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1851 argv[0],sid_type_lookup(name_types[0]));
1852 result = NT_STATUS_UNSUCCESSFUL;
1857 if (NT_STATUS_IS_OK(result)) {
1859 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1861 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1862 get_friendly_nt_error_msg(result));
1870 static int rpc_group_delete(int argc, const char **argv)
1872 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1876 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1877 const char *domain_name,
1878 struct cli_state *cli,
1879 struct rpc_pipe_client *pipe_hnd,
1880 TALLOC_CTX *mem_ctx,
1884 POLICY_HND connect_pol, domain_pol, group_pol;
1885 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1886 GROUP_INFO_CTR group_info;
1889 d_printf("Group name must be specified\n");
1890 rpc_group_usage(argc, argv);
1891 return NT_STATUS_OK;
1894 /* Get sam policy handle */
1896 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1898 if (!NT_STATUS_IS_OK(result)) goto done;
1900 /* Get domain policy handle */
1902 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1903 MAXIMUM_ALLOWED_ACCESS,
1904 domain_sid, &domain_pol);
1905 if (!NT_STATUS_IS_OK(result)) goto done;
1907 /* Create the group */
1909 result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1910 argv[0], MAXIMUM_ALLOWED_ACCESS,
1912 if (!NT_STATUS_IS_OK(result)) goto done;
1914 if (strlen(opt_comment) == 0) goto done;
1916 /* We've got a comment to set */
1918 group_info.switch_value1 = 4;
1919 init_samr_group_info4(&group_info.group.info4, opt_comment);
1921 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1922 if (!NT_STATUS_IS_OK(result)) goto done;
1925 if (NT_STATUS_IS_OK(result))
1926 DEBUG(5, ("add group succeeded\n"));
1928 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1933 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1934 const char *domain_name,
1935 struct cli_state *cli,
1936 struct rpc_pipe_client *pipe_hnd,
1937 TALLOC_CTX *mem_ctx,
1941 POLICY_HND connect_pol, domain_pol, alias_pol;
1942 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1943 ALIAS_INFO_CTR alias_info;
1946 d_printf("Alias name must be specified\n");
1947 rpc_group_usage(argc, argv);
1948 return NT_STATUS_OK;
1951 /* Get sam policy handle */
1953 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1955 if (!NT_STATUS_IS_OK(result)) goto done;
1957 /* Get domain policy handle */
1959 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1960 MAXIMUM_ALLOWED_ACCESS,
1961 domain_sid, &domain_pol);
1962 if (!NT_STATUS_IS_OK(result)) goto done;
1964 /* Create the group */
1966 result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1967 argv[0], &alias_pol);
1968 if (!NT_STATUS_IS_OK(result)) goto done;
1970 if (strlen(opt_comment) == 0) goto done;
1972 /* We've got a comment to set */
1974 alias_info.level = 3;
1975 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1977 result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1978 if (!NT_STATUS_IS_OK(result)) goto done;
1981 if (NT_STATUS_IS_OK(result))
1982 DEBUG(5, ("add alias succeeded\n"));
1984 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1989 static int rpc_group_add(int argc, const char **argv)
1992 return run_rpc_command(NULL, PI_SAMR, 0,
1993 rpc_alias_add_internals,
1996 return run_rpc_command(NULL, PI_SAMR, 0,
1997 rpc_group_add_internals,
2001 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2002 TALLOC_CTX *mem_ctx,
2005 enum SID_NAME_USE *type)
2007 DOM_SID *sids = NULL;
2008 uint32 *types = NULL;
2009 struct rpc_pipe_client *pipe_hnd;
2011 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2013 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2018 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2019 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2021 if (!NT_STATUS_IS_OK(result)) {
2025 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2026 &name, NULL, &sids, &types);
2028 if (NT_STATUS_IS_OK(result)) {
2029 sid_copy(sid, &sids[0]);
2033 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
2037 cli_rpc_pipe_close(pipe_hnd);
2040 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2042 /* Try as S-1-5-whatever */
2046 if (string_to_sid(&tmp_sid, name)) {
2047 sid_copy(sid, &tmp_sid);
2048 *type = SID_NAME_UNKNOWN;
2049 result = NT_STATUS_OK;
2056 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2057 TALLOC_CTX *mem_ctx,
2058 const DOM_SID *group_sid,
2061 POLICY_HND connect_pol, domain_pol;
2064 POLICY_HND group_pol;
2067 uint32 *rids = NULL;
2068 uint32 *rid_types = NULL;
2072 sid_copy(&sid, group_sid);
2074 if (!sid_split_rid(&sid, &group_rid)) {
2075 return NT_STATUS_UNSUCCESSFUL;
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_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2095 &num_rids, &rids, &rid_types);
2097 if (!NT_STATUS_IS_OK(result)) {
2098 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2102 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2103 MAXIMUM_ALLOWED_ACCESS,
2104 group_rid, &group_pol);
2106 if (!NT_STATUS_IS_OK(result)) {
2110 result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2113 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2117 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2118 TALLOC_CTX *mem_ctx,
2119 const DOM_SID *alias_sid,
2122 POLICY_HND connect_pol, domain_pol;
2125 POLICY_HND alias_pol;
2128 enum SID_NAME_USE member_type;
2132 sid_copy(&sid, alias_sid);
2134 if (!sid_split_rid(&sid, &alias_rid)) {
2135 return NT_STATUS_UNSUCCESSFUL;
2138 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2139 &member_sid, &member_type);
2141 if (!NT_STATUS_IS_OK(result)) {
2142 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2146 /* Get sam policy handle */
2147 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2149 if (!NT_STATUS_IS_OK(result)) {
2153 /* Get domain policy handle */
2154 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2155 MAXIMUM_ALLOWED_ACCESS,
2157 if (!NT_STATUS_IS_OK(result)) {
2161 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2162 MAXIMUM_ALLOWED_ACCESS,
2163 alias_rid, &alias_pol);
2165 if (!NT_STATUS_IS_OK(result)) {
2169 result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2171 if (!NT_STATUS_IS_OK(result)) {
2176 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2180 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2181 const char *domain_name,
2182 struct cli_state *cli,
2183 struct rpc_pipe_client *pipe_hnd,
2184 TALLOC_CTX *mem_ctx,
2189 enum SID_NAME_USE group_type;
2192 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2193 return NT_STATUS_UNSUCCESSFUL;
2196 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2197 &group_sid, &group_type))) {
2198 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2199 return NT_STATUS_UNSUCCESSFUL;
2202 if (group_type == SID_NAME_DOM_GRP) {
2203 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2204 &group_sid, argv[1]);
2206 if (!NT_STATUS_IS_OK(result)) {
2207 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2208 argv[1], argv[0], nt_errstr(result));
2213 if (group_type == SID_NAME_ALIAS) {
2214 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2215 &group_sid, argv[1]);
2217 if (!NT_STATUS_IS_OK(result)) {
2218 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2219 argv[1], argv[0], nt_errstr(result));
2224 d_fprintf(stderr, "Can only add members to global or local groups "
2225 "which %s is not\n", argv[0]);
2227 return NT_STATUS_UNSUCCESSFUL;
2230 static int rpc_group_addmem(int argc, const char **argv)
2232 return run_rpc_command(NULL, PI_SAMR, 0,
2233 rpc_group_addmem_internals,
2237 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2238 TALLOC_CTX *mem_ctx,
2239 const DOM_SID *group_sid,
2242 POLICY_HND connect_pol, domain_pol;
2245 POLICY_HND group_pol;
2248 uint32 *rids = NULL;
2249 uint32 *rid_types = NULL;
2253 sid_copy(&sid, group_sid);
2255 if (!sid_split_rid(&sid, &group_rid))
2256 return NT_STATUS_UNSUCCESSFUL;
2258 /* Get sam policy handle */
2259 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2261 if (!NT_STATUS_IS_OK(result))
2264 /* Get domain policy handle */
2265 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2266 MAXIMUM_ALLOWED_ACCESS,
2268 if (!NT_STATUS_IS_OK(result))
2271 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2273 &num_rids, &rids, &rid_types);
2275 if (!NT_STATUS_IS_OK(result)) {
2276 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2280 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2281 MAXIMUM_ALLOWED_ACCESS,
2282 group_rid, &group_pol);
2284 if (!NT_STATUS_IS_OK(result))
2287 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2290 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2294 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2295 TALLOC_CTX *mem_ctx,
2296 const DOM_SID *alias_sid,
2299 POLICY_HND connect_pol, domain_pol;
2302 POLICY_HND alias_pol;
2305 enum SID_NAME_USE member_type;
2309 sid_copy(&sid, alias_sid);
2311 if (!sid_split_rid(&sid, &alias_rid))
2312 return NT_STATUS_UNSUCCESSFUL;
2314 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2315 &member_sid, &member_type);
2317 if (!NT_STATUS_IS_OK(result)) {
2318 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2322 /* Get sam policy handle */
2323 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2325 if (!NT_STATUS_IS_OK(result)) {
2329 /* Get domain policy handle */
2330 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2331 MAXIMUM_ALLOWED_ACCESS,
2333 if (!NT_STATUS_IS_OK(result)) {
2337 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2338 MAXIMUM_ALLOWED_ACCESS,
2339 alias_rid, &alias_pol);
2341 if (!NT_STATUS_IS_OK(result))
2344 result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2346 if (!NT_STATUS_IS_OK(result))
2350 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2354 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2355 const char *domain_name,
2356 struct cli_state *cli,
2357 struct rpc_pipe_client *pipe_hnd,
2358 TALLOC_CTX *mem_ctx,
2363 enum SID_NAME_USE group_type;
2366 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2367 return NT_STATUS_UNSUCCESSFUL;
2370 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2371 &group_sid, &group_type))) {
2372 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2373 return NT_STATUS_UNSUCCESSFUL;
2376 if (group_type == SID_NAME_DOM_GRP) {
2377 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2378 &group_sid, argv[1]);
2380 if (!NT_STATUS_IS_OK(result)) {
2381 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2382 argv[1], argv[0], nt_errstr(result));
2387 if (group_type == SID_NAME_ALIAS) {
2388 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2389 &group_sid, argv[1]);
2391 if (!NT_STATUS_IS_OK(result)) {
2392 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2393 argv[1], argv[0], nt_errstr(result));
2398 d_fprintf(stderr, "Can only delete members from global or local groups "
2399 "which %s is not\n", argv[0]);
2401 return NT_STATUS_UNSUCCESSFUL;
2404 static int rpc_group_delmem(int argc, const char **argv)
2406 return run_rpc_command(NULL, PI_SAMR, 0,
2407 rpc_group_delmem_internals,
2412 * List groups on a remote RPC server
2414 * All parameters are provided by the run_rpc_command function, except for
2415 * argc, argv which are passes through.
2417 * @param domain_sid The domain sid acquired from the remote server
2418 * @param cli A cli_state connected to the server.
2419 * @param mem_ctx Talloc context, destoyed on completion of the function.
2420 * @param argc Standard main() style argc
2421 * @param argv Standard main() style argv. Initial components are already
2424 * @return Normal NTSTATUS return.
2427 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2428 const char *domain_name,
2429 struct cli_state *cli,
2430 struct rpc_pipe_client *pipe_hnd,
2431 TALLOC_CTX *mem_ctx,
2435 POLICY_HND connect_pol, domain_pol;
2436 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2437 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2438 struct acct_info *groups;
2439 BOOL global = False;
2441 BOOL builtin = False;
2449 for (i=0; i<argc; i++) {
2450 if (strequal(argv[i], "global"))
2453 if (strequal(argv[i], "local"))
2456 if (strequal(argv[i], "builtin"))
2460 /* Get sam policy handle */
2462 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2464 if (!NT_STATUS_IS_OK(result)) {
2468 /* Get domain policy handle */
2470 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2471 MAXIMUM_ALLOWED_ACCESS,
2472 domain_sid, &domain_pol);
2473 if (!NT_STATUS_IS_OK(result)) {
2477 /* Query domain groups */
2478 if (opt_long_list_entries)
2479 d_printf("\nGroup name Comment"\
2480 "\n-----------------------------\n");
2482 SAM_DISPINFO_CTR ctr;
2483 SAM_DISPINFO_3 info3;
2488 ctr.sam.info3 = &info3;
2492 get_query_dispinfo_params(
2493 loop_count, &max_entries, &max_size);
2495 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2496 &start_idx, 3, &num_entries,
2497 max_entries, max_size, &ctr);
2499 if (!NT_STATUS_IS_OK(result) &&
2500 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2503 for (i = 0; i < num_entries; i++) {
2505 fstring group, desc;
2507 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2508 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2510 if (opt_long_list_entries)
2511 printf("%-21.21s %-50.50s\n",
2514 printf("%s\n", group);
2516 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2517 /* query domain aliases */
2522 /* The max_size field in cli_samr_enum_als_groups is more like
2523 * an account_control field with indiviual bits what to
2524 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2525 * everything. I'm too lazy (sorry) to get this through to
2526 * rpc_parse/ etc. Volker */
2528 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2530 &groups, &num_entries);
2532 if (!NT_STATUS_IS_OK(result) &&
2533 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2536 for (i = 0; i < num_entries; i++) {
2538 char *description = NULL;
2540 if (opt_long_list_entries) {
2542 POLICY_HND alias_pol;
2545 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2550 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2553 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2555 description = unistr2_tdup(mem_ctx,
2556 ctr.alias.info3.description.string);
2560 if (description != NULL) {
2561 printf("%-21.21s %-50.50s\n",
2562 groups[i].acct_name,
2565 printf("%s\n", groups[i].acct_name);
2568 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2569 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2570 /* Get builtin policy handle */
2572 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2573 MAXIMUM_ALLOWED_ACCESS,
2574 &global_sid_Builtin, &domain_pol);
2575 if (!NT_STATUS_IS_OK(result)) {
2578 /* query builtin aliases */
2581 if (!builtin) break;
2583 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2584 &start_idx, max_entries,
2585 &groups, &num_entries);
2587 if (!NT_STATUS_IS_OK(result) &&
2588 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2591 for (i = 0; i < num_entries; i++) {
2593 char *description = NULL;
2595 if (opt_long_list_entries) {
2597 POLICY_HND alias_pol;
2600 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2605 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2608 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2610 description = unistr2_tdup(mem_ctx,
2611 ctr.alias.info3.description.string);
2615 if (description != NULL) {
2616 printf("%-21.21s %-50.50s\n",
2617 groups[i].acct_name,
2620 printf("%s\n", groups[i].acct_name);
2623 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2629 static int rpc_group_list(int argc, const char **argv)
2631 return run_rpc_command(NULL, PI_SAMR, 0,
2632 rpc_group_list_internals,
2636 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2637 TALLOC_CTX *mem_ctx,
2638 const char *domain_name,
2639 const DOM_SID *domain_sid,
2640 POLICY_HND *domain_pol,
2644 POLICY_HND group_pol;
2645 uint32 num_members, *group_rids, *group_attrs;
2652 sid_to_string(sid_str, domain_sid);
2654 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2655 MAXIMUM_ALLOWED_ACCESS,
2658 if (!NT_STATUS_IS_OK(result))
2661 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2662 &num_members, &group_rids,
2665 if (!NT_STATUS_IS_OK(result))
2668 while (num_members > 0) {
2669 int this_time = 512;
2671 if (num_members < this_time)
2672 this_time = num_members;
2674 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2675 this_time, group_rids,
2676 &num_names, &names, &name_types);
2678 if (!NT_STATUS_IS_OK(result))
2681 /* We only have users as members, but make the output
2682 the same as the output of alias members */
2684 for (i = 0; i < this_time; i++) {
2686 if (opt_long_list_entries) {
2687 printf("%s-%d %s\\%s %d\n", sid_str,
2688 group_rids[i], domain_name, names[i],
2691 printf("%s\\%s\n", domain_name, names[i]);
2695 num_members -= this_time;
2699 return NT_STATUS_OK;
2702 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2703 TALLOC_CTX *mem_ctx,
2704 POLICY_HND *domain_pol,
2708 struct rpc_pipe_client *lsa_pipe;
2709 POLICY_HND alias_pol, lsa_pol;
2711 DOM_SID *alias_sids;
2717 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2718 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2720 if (!NT_STATUS_IS_OK(result))
2723 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2724 &num_members, &alias_sids);
2726 if (!NT_STATUS_IS_OK(result)) {
2727 d_fprintf(stderr, "Couldn't list alias members\n");
2731 if (num_members == 0) {
2732 return NT_STATUS_OK;
2735 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2737 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2738 nt_errstr(result) );
2742 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2743 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2745 if (!NT_STATUS_IS_OK(result)) {
2746 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2747 cli_rpc_pipe_close(lsa_pipe);
2751 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2753 &domains, &names, &types);
2755 if (!NT_STATUS_IS_OK(result) &&
2756 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2757 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2758 cli_rpc_pipe_close(lsa_pipe);
2762 for (i = 0; i < num_members; i++) {
2764 sid_to_string(sid_str, &alias_sids[i]);
2766 if (opt_long_list_entries) {
2767 printf("%s %s\\%s %d\n", sid_str,
2768 domains[i] ? domains[i] : "*unknown*",
2769 names[i] ? names[i] : "*unknown*", types[i]);
2772 printf("%s\\%s\n", domains[i], names[i]);
2774 printf("%s\n", sid_str);
2778 cli_rpc_pipe_close(lsa_pipe);
2779 return NT_STATUS_OK;
2782 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2783 const char *domain_name,
2784 struct cli_state *cli,
2785 struct rpc_pipe_client *pipe_hnd,
2786 TALLOC_CTX *mem_ctx,
2791 POLICY_HND connect_pol, domain_pol;
2792 uint32 num_rids, *rids, *rid_types;
2794 /* Get sam policy handle */
2796 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2799 if (!NT_STATUS_IS_OK(result))
2802 /* Get domain policy handle */
2804 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2805 MAXIMUM_ALLOWED_ACCESS,
2806 domain_sid, &domain_pol);
2808 if (!NT_STATUS_IS_OK(result))
2811 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2812 1, argv, &num_rids, &rids, &rid_types);
2814 if (!NT_STATUS_IS_OK(result)) {
2816 /* Ok, did not find it in the global sam, try with builtin */
2818 DOM_SID sid_Builtin;
2820 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2822 string_to_sid(&sid_Builtin, "S-1-5-32");
2824 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2825 MAXIMUM_ALLOWED_ACCESS,
2826 &sid_Builtin, &domain_pol);
2828 if (!NT_STATUS_IS_OK(result)) {
2829 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2833 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2837 if (!NT_STATUS_IS_OK(result)) {
2838 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
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 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2850 domain_sid, &domain_pol,
2854 if (rid_types[0] == SID_NAME_ALIAS) {
2855 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2859 return NT_STATUS_NO_SUCH_GROUP;
2862 static int rpc_group_members(int argc, const char **argv)
2865 return rpc_group_usage(argc, argv);
2868 return run_rpc_command(NULL, PI_SAMR, 0,
2869 rpc_group_members_internals,
2873 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2874 const char *domain_name,
2875 struct cli_state *cli,
2876 struct rpc_pipe_client *pipe_hnd,
2877 TALLOC_CTX *mem_ctx,
2882 POLICY_HND connect_pol, domain_pol, group_pol;
2883 uint32 num_rids, *rids, *rid_types;
2887 d_printf("Usage: 'net rpc group rename group newname'\n");
2888 return NT_STATUS_UNSUCCESSFUL;
2891 /* Get sam policy handle */
2893 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2896 if (!NT_STATUS_IS_OK(result))
2899 /* Get domain policy handle */
2901 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2902 MAXIMUM_ALLOWED_ACCESS,
2903 domain_sid, &domain_pol);
2905 if (!NT_STATUS_IS_OK(result))
2908 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2909 1, argv, &num_rids, &rids, &rid_types);
2911 if (num_rids != 1) {
2912 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2916 if (rid_types[0] != SID_NAME_DOM_GRP) {
2917 d_fprintf(stderr, "Can only rename domain groups\n");
2918 return NT_STATUS_UNSUCCESSFUL;
2921 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2922 MAXIMUM_ALLOWED_ACCESS,
2923 rids[0], &group_pol);
2925 if (!NT_STATUS_IS_OK(result))
2930 ctr.switch_value1 = 2;
2931 init_samr_group_info2(&ctr.group.info2, argv[1]);
2933 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2935 if (!NT_STATUS_IS_OK(result))
2938 return NT_STATUS_NO_SUCH_GROUP;
2941 static int rpc_group_rename(int argc, const char **argv)
2944 return rpc_group_usage(argc, argv);
2947 return run_rpc_command(NULL, PI_SAMR, 0,
2948 rpc_group_rename_internals,
2953 * 'net rpc group' entrypoint.
2954 * @param argc Standard main() style argc
2955 * @param argc Standard main() style argv. Initial components are already
2959 int net_rpc_group(int argc, const char **argv)
2961 struct functable func[] = {
2962 {"add", rpc_group_add},
2963 {"delete", rpc_group_delete},
2964 {"addmem", rpc_group_addmem},
2965 {"delmem", rpc_group_delmem},
2966 {"list", rpc_group_list},
2967 {"members", rpc_group_members},
2968 {"rename", rpc_group_rename},
2973 return run_rpc_command(NULL, PI_SAMR, 0,
2974 rpc_group_list_internals,
2978 return net_run_function(argc, argv, func, rpc_group_usage);
2981 /****************************************************************************/
2983 static int rpc_share_usage(int argc, const char **argv)
2985 return net_help_share(argc, argv);
2989 * Add a share on a remote RPC server
2991 * All parameters are provided by the run_rpc_command function, except for
2992 * argc, argv which are passes through.
2994 * @param domain_sid The domain sid acquired from the remote server
2995 * @param cli A cli_state connected to the server.
2996 * @param mem_ctx Talloc context, destoyed on completion of the function.
2997 * @param argc Standard main() style argc
2998 * @param argv Standard main() style argv. Initial components are already
3001 * @return Normal NTSTATUS return.
3003 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3004 const char *domain_name,
3005 struct cli_state *cli,
3006 struct rpc_pipe_client *pipe_hnd,
3007 TALLOC_CTX *mem_ctx,int argc,
3011 char *sharename=talloc_strdup(mem_ctx, argv[0]);
3013 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3014 uint32 num_users=0, perms=0;
3015 char *password=NULL; /* don't allow a share password */
3018 path = strchr(sharename, '=');
3020 return NT_STATUS_UNSUCCESSFUL;
3023 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
3024 opt_comment, perms, opt_maxusers,
3025 num_users, path, password,
3027 return werror_to_ntstatus(result);
3030 static int rpc_share_add(int argc, const char **argv)
3032 if ((argc < 1) || !strchr(argv[0], '=')) {
3033 DEBUG(1,("Sharename or path not specified on add\n"));
3034 return rpc_share_usage(argc, argv);
3036 return run_rpc_command(NULL, PI_SRVSVC, 0,
3037 rpc_share_add_internals,
3042 * Delete a share on a remote RPC server
3044 * All parameters are provided by the run_rpc_command function, except for
3045 * argc, argv which are passes through.
3047 * @param domain_sid The domain sid acquired from the remote server
3048 * @param cli A cli_state connected to the server.
3049 * @param mem_ctx Talloc context, destoyed on completion of the function.
3050 * @param argc Standard main() style argc
3051 * @param argv Standard main() style argv. Initial components are already
3054 * @return Normal NTSTATUS return.
3056 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3057 const char *domain_name,
3058 struct cli_state *cli,
3059 struct rpc_pipe_client *pipe_hnd,
3060 TALLOC_CTX *mem_ctx,
3066 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
3067 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3071 * Delete a share on a remote RPC server
3073 * @param domain_sid The domain sid acquired from the remote server
3074 * @param argc Standard main() style argc
3075 * @param argv Standard main() style argv. Initial components are already
3078 * @return A shell status integer (0 for success)
3080 static int rpc_share_delete(int argc, const char **argv)
3083 DEBUG(1,("Sharename not specified on delete\n"));
3084 return rpc_share_usage(argc, argv);
3086 return run_rpc_command(NULL, PI_SRVSVC, 0,
3087 rpc_share_del_internals,
3092 * Formatted print of share info
3094 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3097 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3099 fstring netname = "", remark = "";
3101 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3102 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3104 if (opt_long_list_entries) {
3105 d_printf("%-12s %-8.8s %-50s\n",
3106 netname, share_type[info1->info_1.type], remark);
3108 d_printf("%s\n", netname);
3113 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3114 TALLOC_CTX *mem_ctx,
3118 SRV_SHARE_INFO_CTR *ctr)
3121 SRV_SHARE_INFO info;
3123 /* no specific share requested, enumerate all */
3127 uint32 preferred_len = 0xffffffff;
3129 init_enum_hnd(&hnd, 0);
3131 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3132 preferred_len, &hnd);
3135 /* request just one share */
3136 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3138 if (!W_ERROR_IS_OK(result))
3144 ctr->info_level = ctr->switch_value = level;
3145 ctr->ptr_share_info = ctr->ptr_entries = 1;
3146 ctr->num_entries = ctr->num_entries2 = 1;
3152 SRV_SHARE_INFO_1 *info1;
3154 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3155 info1 = ctr->share.info1;
3157 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3159 /* Copy pointer crap */
3161 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3163 /* Duplicate strings */
3165 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3167 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3169 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3171 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3176 SRV_SHARE_INFO_2 *info2;
3178 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3179 info2 = ctr->share.info2;
3181 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3183 /* Copy pointer crap */
3185 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3187 /* Duplicate strings */
3189 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3191 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3193 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3195 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3197 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_path);
3199 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3201 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3203 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3208 SRV_SHARE_INFO_502 *info502;
3210 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3211 info502 = ctr->share.info502;
3213 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3215 /* Copy pointer crap */
3217 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3219 /* Duplicate strings */
3221 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3223 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3225 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3227 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3229 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_path);
3231 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3233 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3235 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3237 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3248 * List shares on a remote RPC server
3250 * All parameters are provided by the run_rpc_command function, except for
3251 * argc, argv which are passes through.
3253 * @param domain_sid The domain sid acquired from the remote server
3254 * @param cli A cli_state connected to the server.
3255 * @param mem_ctx Talloc context, destoyed on completion of the function.
3256 * @param argc Standard main() style argc
3257 * @param argv Standard main() style argv. Initial components are already
3260 * @return Normal NTSTATUS return.
3263 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3264 const char *domain_name,
3265 struct cli_state *cli,
3266 struct rpc_pipe_client *pipe_hnd,
3267 TALLOC_CTX *mem_ctx,
3271 SRV_SHARE_INFO_CTR ctr;
3273 uint32 i, level = 1;
3275 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3276 if (!W_ERROR_IS_OK(result))
3279 /* Display results */
3281 if (opt_long_list_entries) {
3283 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3284 "\nShare name Type Description\n"\
3285 "---------- ---- -----------\n");
3287 for (i = 0; i < ctr.num_entries; i++)
3288 display_share_info_1(&ctr.share.info1[i]);
3290 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3294 * 'net rpc share list' entrypoint.
3295 * @param argc Standard main() style argc
3296 * @param argv Standard main() style argv. Initial components are already
3299 static int rpc_share_list(int argc, const char **argv)
3301 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3304 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3306 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3307 d_printf("skipping [%s]: not a file share.\n", netname);
3317 static BOOL check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3319 /* only support disk shares */
3320 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3321 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3325 /* skip builtin shares */
3326 /* FIXME: should print$ be added too ? */
3327 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3328 strequal(netname,"global"))
3331 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3332 printf("excluding [%s]\n", netname);
3336 return check_share_availability(cli, netname);
3340 * Migrate shares from a remote RPC server to the local RPC srever
3342 * All parameters are provided by the run_rpc_command function, except for
3343 * argc, argv which are passes through.
3345 * @param domain_sid The domain sid acquired from the remote server
3346 * @param cli A cli_state connected to the server.
3347 * @param mem_ctx Talloc context, destoyed on completion of the function.
3348 * @param argc Standard main() style argc
3349 * @param argv Standard main() style argv. Initial components are already
3352 * @return Normal NTSTATUS return.
3355 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3356 const char *domain_name,
3357 struct cli_state *cli,
3358 struct rpc_pipe_client *pipe_hnd,
3359 TALLOC_CTX *mem_ctx,
3364 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3365 SRV_SHARE_INFO_CTR ctr_src;
3366 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3367 char *password = NULL; /* don't allow a share password */
3369 struct rpc_pipe_client *srvsvc_pipe = NULL;
3370 struct cli_state *cli_dst = NULL;
3371 uint32 level = 502; /* includes secdesc */
3373 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3374 if (!W_ERROR_IS_OK(result))
3377 /* connect destination PI_SRVSVC */
3378 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3379 if (!NT_STATUS_IS_OK(nt_status))
3383 for (i = 0; i < ctr_src.num_entries; i++) {
3385 fstring netname = "", remark = "", path = "";
3386 /* reset error-code */
3387 nt_status = NT_STATUS_UNSUCCESSFUL;
3389 rpcstr_pull_unistr2_fstring(
3390 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3391 rpcstr_pull_unistr2_fstring(
3392 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3393 rpcstr_pull_unistr2_fstring(
3394 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3396 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3399 /* finally add the share on the dst server */
3401 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3402 netname, path, remark);
3404 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3405 ctr_src.share.info502[i].info_502.perms,
3406 ctr_src.share.info502[i].info_502.max_uses,
3407 ctr_src.share.info502[i].info_502.num_uses,
3408 path, password, level,
3411 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3412 printf(" [%s] does already exist\n", netname);
3416 if (!W_ERROR_IS_OK(result)) {
3417 printf("cannot add share: %s\n", dos_errstr(result));
3423 nt_status = NT_STATUS_OK;
3427 cli_shutdown(cli_dst);
3435 * Migrate shares from a rpc-server to another
3437 * @param argc Standard main() style argc
3438 * @param argv Standard main() style argv. Initial components are already
3441 * @return A shell status integer (0 for success)
3443 static int rpc_share_migrate_shares(int argc, const char **argv)
3447 printf("no server to migrate\n");
3451 return run_rpc_command(NULL, PI_SRVSVC, 0,
3452 rpc_share_migrate_shares_internals,
3459 * @param f file_info
3460 * @param mask current search mask
3461 * @param state arg-pointer
3464 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3466 static NTSTATUS nt_status;
3467 static struct copy_clistate *local_state;
3468 static fstring filename, new_mask;
3472 local_state = (struct copy_clistate *)state;
3473 nt_status = NT_STATUS_UNSUCCESSFUL;
3475 if (strequal(f->name, ".") || strequal(f->name, ".."))
3478 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3481 if (f->mode & aDIR) {
3483 DEBUG(3,("got dir: %s\n", f->name));
3485 fstrcpy(dir, local_state->cwd);
3487 fstrcat(dir, f->name);
3489 switch (net_mode_share)
3491 case NET_MODE_SHARE_MIGRATE:
3492 /* create that directory */
3493 nt_status = net_copy_file(local_state->mem_ctx,
3494 local_state->cli_share_src,
3495 local_state->cli_share_dst,
3497 opt_acls? True : False,
3498 opt_attrs? True : False,
3499 opt_timestamps? True : False,
3503 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3507 if (!NT_STATUS_IS_OK(nt_status))
3508 printf("could not handle dir %s: %s\n",
3509 dir, nt_errstr(nt_status));
3511 /* search below that directory */
3512 fstrcpy(new_mask, dir);
3513 fstrcat(new_mask, "\\*");
3515 old_dir = local_state->cwd;
3516 local_state->cwd = dir;
3517 if (!sync_files(local_state, new_mask))
3518 printf("could not handle files\n");
3519 local_state->cwd = old_dir;
3526 fstrcpy(filename, local_state->cwd);
3527 fstrcat(filename, "\\");
3528 fstrcat(filename, f->name);
3530 DEBUG(3,("got file: %s\n", filename));
3532 switch (net_mode_share)
3534 case NET_MODE_SHARE_MIGRATE:
3535 nt_status = net_copy_file(local_state->mem_ctx,
3536 local_state->cli_share_src,
3537 local_state->cli_share_dst,
3539 opt_acls? True : False,
3540 opt_attrs? True : False,
3541 opt_timestamps? True: False,
3545 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3549 if (!NT_STATUS_IS_OK(nt_status))
3550 printf("could not handle file %s: %s\n",
3551 filename, nt_errstr(nt_status));
3556 * sync files, can be called recursivly to list files
3557 * and then call copy_fn for each file
3559 * @param cp_clistate pointer to the copy_clistate we work with
3560 * @param mask the current search mask
3562 * @return Boolean result
3564 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3567 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3569 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3570 d_fprintf(stderr, "listing %s failed with error: %s\n",
3571 mask, cli_errstr(cp_clistate->cli_share_src));
3580 * Set the top level directory permissions before we do any further copies.
3581 * Should set up ACL inheritance.
3584 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3585 const char *sharename)
3587 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3589 switch (net_mode_share) {
3590 case NET_MODE_SHARE_MIGRATE:
3591 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3592 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3593 cp_clistate->cli_share_src,
3594 cp_clistate->cli_share_dst,
3596 opt_acls? True : False,
3597 opt_attrs? True : False,
3598 opt_timestamps? True: False,
3602 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3606 if (!NT_STATUS_IS_OK(nt_status)) {
3607 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3608 sharename, nt_errstr(nt_status));
3616 * Sync all files inside a remote share to another share (over smb)
3618 * All parameters are provided by the run_rpc_command function, except for
3619 * argc, argv which are passes through.
3621 * @param domain_sid The domain sid acquired from the remote server
3622 * @param cli A cli_state connected to the server.
3623 * @param mem_ctx Talloc context, destoyed on completion of the function.
3624 * @param argc Standard main() style argc
3625 * @param argv Standard main() style argv. Initial components are already
3628 * @return Normal NTSTATUS return.
3631 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3632 const char *domain_name,
3633 struct cli_state *cli,
3634 struct rpc_pipe_client *pipe_hnd,
3635 TALLOC_CTX *mem_ctx,
3640 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3641 SRV_SHARE_INFO_CTR ctr_src;
3644 struct copy_clistate cp_clistate;
3645 BOOL got_src_share = False;
3646 BOOL got_dst_share = False;
3647 pstring mask = "\\*";
3650 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3652 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3654 if (!W_ERROR_IS_OK(result))
3657 for (i = 0; i < ctr_src.num_entries; i++) {
3659 fstring netname = "";
3661 rpcstr_pull_unistr2_fstring(
3662 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3664 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3667 /* one might not want to mirror whole discs :) */
3668 if (strequal(netname, "print$") || netname[1] == '$') {
3669 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3673 switch (net_mode_share)
3675 case NET_MODE_SHARE_MIGRATE:
3679 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3682 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3684 opt_acls ? "including" : "without",
3685 opt_attrs ? "including" : "without",
3686 opt_timestamps ? "(preserving timestamps)" : "");
3688 cp_clistate.mem_ctx = mem_ctx;
3689 cp_clistate.cli_share_src = NULL;
3690 cp_clistate.cli_share_dst = NULL;
3691 cp_clistate.cwd = NULL;
3692 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3694 /* open share source */
3695 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3696 &cli->dest_ip, cli->desthost,
3698 if (!NT_STATUS_IS_OK(nt_status))
3701 got_src_share = True;
3703 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3704 /* open share destination */
3705 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3706 NULL, dst, netname, "A:");
3707 if (!NT_STATUS_IS_OK(nt_status))
3710 got_dst_share = True;
3713 if (!copy_top_level_perms(&cp_clistate, netname)) {
3714 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
3715 nt_status = NT_STATUS_UNSUCCESSFUL;
3719 if (!sync_files(&cp_clistate, mask)) {
3720 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
3721 nt_status = NT_STATUS_UNSUCCESSFUL;
3726 nt_status = NT_STATUS_OK;
3731 cli_shutdown(cp_clistate.cli_share_src);
3734 cli_shutdown(cp_clistate.cli_share_dst);
3740 static int rpc_share_migrate_files(int argc, const char **argv)
3744 printf("no server to migrate\n");
3748 return run_rpc_command(NULL, PI_SRVSVC, 0,
3749 rpc_share_migrate_files_internals,
3754 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3756 * All parameters are provided by the run_rpc_command function, except for
3757 * argc, argv which are passes through.
3759 * @param domain_sid The domain sid acquired from the remote server
3760 * @param cli A cli_state connected to the server.
3761 * @param mem_ctx Talloc context, destoyed on completion of the function.
3762 * @param argc Standard main() style argc
3763 * @param argv Standard main() style argv. Initial components are already
3766 * @return Normal NTSTATUS return.
3769 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3770 const char *domain_name,
3771 struct cli_state *cli,
3772 struct rpc_pipe_client *pipe_hnd,
3773 TALLOC_CTX *mem_ctx,
3778 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3779 SRV_SHARE_INFO_CTR ctr_src;
3780 SRV_SHARE_INFO info;
3782 struct rpc_pipe_client *srvsvc_pipe = NULL;
3783 struct cli_state *cli_dst = NULL;
3784 uint32 level = 502; /* includes secdesc */
3786 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3788 if (!W_ERROR_IS_OK(result))
3791 /* connect destination PI_SRVSVC */
3792 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3793 if (!NT_STATUS_IS_OK(nt_status))
3797 for (i = 0; i < ctr_src.num_entries; i++) {
3799 fstring netname = "", remark = "", path = "";
3800 /* reset error-code */
3801 nt_status = NT_STATUS_UNSUCCESSFUL;
3803 rpcstr_pull_unistr2_fstring(
3804 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3805 rpcstr_pull_unistr2_fstring(
3806 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3807 rpcstr_pull_unistr2_fstring(
3808 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3810 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3813 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3814 netname, path, remark);
3817 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
3822 info.switch_value = level;
3823 info.ptr_share_ctr = 1;
3825 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3826 info.share.info502 = ctr_src.share.info502[i];
3828 /* finally modify the share on the dst server */
3829 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
3831 if (!W_ERROR_IS_OK(result)) {
3832 printf("cannot set share-acl: %s\n", dos_errstr(result));
3838 nt_status = NT_STATUS_OK;
3842 cli_shutdown(cli_dst);
3850 * Migrate share-acls from a rpc-server to another
3852 * @param argc Standard main() style argc
3853 * @param argv Standard main() style argv. Initial components are already
3856 * @return A shell status integer (0 for success)
3858 static int rpc_share_migrate_security(int argc, const char **argv)
3862 printf("no server to migrate\n");
3866 return run_rpc_command(NULL, PI_SRVSVC, 0,
3867 rpc_share_migrate_security_internals,
3872 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3873 * from one server to another
3875 * @param argc Standard main() style argc
3876 * @param argv Standard main() style argv. Initial components are already
3879 * @return A shell status integer (0 for success)
3882 static int rpc_share_migrate_all(int argc, const char **argv)
3887 printf("no server to migrate\n");
3891 /* order is important. we don't want to be locked out by the share-acl
3892 * before copying files - gd */
3894 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3898 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3902 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3907 * 'net rpc share migrate' entrypoint.
3908 * @param argc Standard main() style argc
3909 * @param argv Standard main() style argv. Initial components are already
3912 static int rpc_share_migrate(int argc, const char **argv)
3915 struct functable func[] = {
3916 {"all", rpc_share_migrate_all},
3917 {"files", rpc_share_migrate_files},
3918 {"help", rpc_share_usage},
3919 {"security", rpc_share_migrate_security},
3920 {"shares", rpc_share_migrate_shares},
3924 net_mode_share = NET_MODE_SHARE_MIGRATE;
3926 return net_run_function(argc, argv, func, rpc_share_usage);
3935 static int num_server_aliases;
3936 static struct full_alias *server_aliases;
3939 * Add an alias to the static list.
3941 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3943 if (server_aliases == NULL)
3944 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3946 server_aliases[num_server_aliases] = *alias;
3947 num_server_aliases += 1;
3951 * For a specific domain on the server, fetch all the aliases
3952 * and their members. Add all of them to the server_aliases.
3955 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3956 TALLOC_CTX *mem_ctx,
3957 POLICY_HND *connect_pol,
3958 const DOM_SID *domain_sid)
3960 uint32 start_idx, max_entries, num_entries, i;
3961 struct acct_info *groups;
3963 POLICY_HND domain_pol;
3965 /* Get domain policy handle */
3967 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3968 MAXIMUM_ALLOWED_ACCESS,
3969 domain_sid, &domain_pol);
3970 if (!NT_STATUS_IS_OK(result))
3977 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
3978 &start_idx, max_entries,
3979 &groups, &num_entries);
3981 for (i = 0; i < num_entries; i++) {
3983 POLICY_HND alias_pol;
3984 struct full_alias alias;
3988 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
3989 MAXIMUM_ALLOWED_ACCESS,
3992 if (!NT_STATUS_IS_OK(result))
3995 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
3999 if (!NT_STATUS_IS_OK(result))
4002 result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
4003 if (!NT_STATUS_IS_OK(result))
4006 alias.members = NULL;
4008 if (alias.num_members > 0) {
4009 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4011 for (j = 0; j < alias.num_members; j++)
4012 sid_copy(&alias.members[j],
4016 sid_copy(&alias.sid, domain_sid);
4017 sid_append_rid(&alias.sid, groups[i].rid);
4019 push_alias(mem_ctx, &alias);
4021 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4023 result = NT_STATUS_OK;
4026 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
4032 * Dump server_aliases as names for debugging purposes.
4035 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
4036 const char *domain_name,
4037 struct cli_state *cli,
4038 struct rpc_pipe_client *pipe_hnd,
4039 TALLOC_CTX *mem_ctx,
4047 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
4048 SEC_RIGHTS_MAXIMUM_ALLOWED,
4050 if (!NT_STATUS_IS_OK(result))
4053 for (i=0; i<num_server_aliases; i++) {
4059 struct full_alias *alias = &server_aliases[i];
4061 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4063 &domains, &names, &types);
4064 if (!NT_STATUS_IS_OK(result))
4067 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4069 if (alias->num_members == 0) {
4074 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4077 &domains, &names, &types);
4079 if (!NT_STATUS_IS_OK(result) &&
4080 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4083 for (j=0; j<alias->num_members; j++)
4084 DEBUG(1, ("%s\\%s (%d); ",
4085 domains[j] ? domains[j] : "*unknown*",
4086 names[j] ? names[j] : "*unknown*",types[j]));
4090 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
4092 return NT_STATUS_OK;
4096 * Fetch a list of all server aliases and their members into
4100 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4101 const char *domain_name,
4102 struct cli_state *cli,
4103 struct rpc_pipe_client *pipe_hnd,
4104 TALLOC_CTX *mem_ctx,
4109 POLICY_HND connect_pol;
4111 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4114 if (!NT_STATUS_IS_OK(result))
4117 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4118 &global_sid_Builtin);
4120 if (!NT_STATUS_IS_OK(result))
4123 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4126 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4131 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4133 token->num_sids = 4;
4135 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4137 token->user_sids[0] = *user_sid;
4138 sid_copy(&token->user_sids[1], &global_sid_World);
4139 sid_copy(&token->user_sids[2], &global_sid_Network);
4140 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4143 static void free_user_token(NT_USER_TOKEN *token)
4145 SAFE_FREE(token->user_sids);
4148 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4152 for (i=0; i<token->num_sids; i++) {
4153 if (sid_compare(sid, &token->user_sids[i]) == 0)
4159 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4161 if (is_sid_in_token(token, sid))
4164 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4165 if (!token->user_sids) {
4169 sid_copy(&token->user_sids[token->num_sids], sid);
4171 token->num_sids += 1;
4176 NT_USER_TOKEN token;
4179 static void dump_user_token(struct user_token *token)
4183 d_printf("%s\n", token->name);
4185 for (i=0; i<token->token.num_sids; i++) {
4186 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4190 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4194 for (i=0; i<alias->num_members; i++) {
4195 if (sid_compare(sid, &alias->members[i]) == 0)
4202 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4206 for (i=0; i<num_server_aliases; i++) {
4207 if (is_alias_member(&sid, &server_aliases[i]))
4208 add_sid_to_token(token, &server_aliases[i].sid);
4213 * We got a user token with all the SIDs we can know about without asking the
4214 * server directly. These are the user and domain group sids. All of these can
4215 * be members of aliases. So scan the list of aliases for each of the SIDs and
4216 * add them to the token.
4219 static void collect_alias_memberships(NT_USER_TOKEN *token)
4221 int num_global_sids = token->num_sids;
4224 for (i=0; i<num_global_sids; i++) {
4225 collect_sid_memberships(token, token->user_sids[i]);
4229 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4231 struct winbindd_request request;
4232 struct winbindd_response response;
4240 fstr_sprintf(full_name, "%s%c%s",
4241 domain, *lp_winbind_separator(), user);
4243 /* First let's find out the user sid */
4245 ZERO_STRUCT(request);
4246 ZERO_STRUCT(response);
4248 fstrcpy(request.data.name.dom_name, domain);
4249 fstrcpy(request.data.name.name, user);
4251 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4253 if (result != NSS_STATUS_SUCCESS) {
4254 DEBUG(1, ("winbind could not find %s\n", full_name));
4258 if (response.data.sid.type != SID_NAME_USER) {
4259 DEBUG(1, ("%s is not a user\n", full_name));
4263 string_to_sid(&user_sid, response.data.sid.sid);
4265 init_user_token(token, &user_sid);
4267 /* And now the groups winbind knows about */
4269 ZERO_STRUCT(response);
4271 fstrcpy(request.data.username, full_name);
4273 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4275 if (result != NSS_STATUS_SUCCESS) {
4276 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4280 for (i = 0; i < response.data.num_entries; i++) {
4281 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4284 struct winbindd_request sidrequest;
4285 struct winbindd_response sidresponse;
4287 ZERO_STRUCT(sidrequest);
4288 ZERO_STRUCT(sidresponse);
4290 sidrequest.data.gid = gid;
4292 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4293 &sidrequest, &sidresponse);
4295 if (result != NSS_STATUS_SUCCESS) {
4296 DEBUG(1, ("winbind could not find SID of gid %d\n",
4301 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4303 string_to_sid(&sid, sidresponse.data.sid.sid);
4304 add_sid_to_token(token, &sid);
4307 SAFE_FREE(response.extra_data.data);
4313 * Get a list of all user tokens we want to look at
4316 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4318 struct winbindd_request request;
4319 struct winbindd_response response;
4320 const char *extra_data;
4323 struct user_token *result;
4325 if (lp_winbind_use_default_domain() &&
4326 (opt_target_workgroup == NULL)) {
4327 d_fprintf(stderr, "winbind use default domain = yes set, "
4328 "please specify a workgroup\n");
4332 /* Send request to winbind daemon */
4334 ZERO_STRUCT(request);
4335 ZERO_STRUCT(response);
4337 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4341 /* Look through extra data */
4343 if (!response.extra_data.data)
4346 extra_data = (const char *)response.extra_data.data;
4349 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4353 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4355 if (result == NULL) {
4356 DEBUG(1, ("Could not malloc sid array\n"));
4360 extra_data = (const char *)response.extra_data.data;
4363 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4365 fstring domain, user;
4368 fstrcpy(result[i].name, name);
4370 p = strchr(name, *lp_winbind_separator());
4372 DEBUG(3, ("%s\n", name));
4375 fstrcpy(domain, opt_target_workgroup);
4376 fstrcpy(user, name);
4379 fstrcpy(domain, name);
4384 get_user_sids(domain, user, &(result[i].token));
4388 SAFE_FREE(response.extra_data.data);
4390 *user_tokens = result;
4395 static BOOL get_user_tokens_from_file(FILE *f,
4397 struct user_token **tokens)
4399 struct user_token *token = NULL;
4404 if (fgets(line, sizeof(line)-1, f) == NULL) {
4408 if (line[strlen(line)-1] == '\n')
4409 line[strlen(line)-1] = '\0';
4411 if (line[0] == ' ') {
4415 string_to_sid(&sid, &line[1]);
4417 if (token == NULL) {
4418 DEBUG(0, ("File does not begin with username"));
4422 add_sid_to_token(&token->token, &sid);
4426 /* And a new user... */
4429 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4430 if (*tokens == NULL) {
4431 DEBUG(0, ("Could not realloc tokens\n"));
4435 token = &((*tokens)[*num_tokens-1]);
4437 fstrcpy(token->name, line);
4438 token->token.num_sids = 0;
4439 token->token.user_sids = NULL;
4448 * Show the list of all users that have access to a share
4451 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4452 TALLOC_CTX *mem_ctx,
4453 const char *netname,
4455 struct user_token *tokens)
4458 SEC_DESC *share_sd = NULL;
4459 SEC_DESC *root_sd = NULL;
4460 struct cli_state *cli = pipe_hnd->cli;
4462 SRV_SHARE_INFO info;
4466 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4469 if (!W_ERROR_IS_OK(result)) {
4470 DEBUG(1, ("Coult not query secdesc for share %s\n",
4475 share_sd = info.share.info502.info_502_str.sd;
4476 if (share_sd == NULL) {
4477 DEBUG(1, ("Got no secdesc for share %s\n",
4483 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4487 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4490 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4493 for (i=0; i<num_tokens; i++) {
4497 if (share_sd != NULL) {
4498 if (!se_access_check(share_sd, &tokens[i].token,
4499 1, &acc_granted, &status)) {
4500 DEBUG(1, ("Could not check share_sd for "
4506 if (!NT_STATUS_IS_OK(status))
4510 if (root_sd == NULL) {
4511 d_printf(" %s\n", tokens[i].name);
4515 if (!se_access_check(root_sd, &tokens[i].token,
4516 1, &acc_granted, &status)) {
4517 DEBUG(1, ("Could not check root_sd for user %s\n",
4522 if (!NT_STATUS_IS_OK(status))
4525 d_printf(" %s\n", tokens[i].name);
4529 cli_close(cli, fnum);
4541 static void collect_share(const char *name, uint32 m,
4542 const char *comment, void *state)
4544 struct share_list *share_list = (struct share_list *)state;
4546 if (m != STYPE_DISKTREE)
4549 share_list->num_shares += 1;
4550 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4551 if (!share_list->shares) {
4552 share_list->num_shares = 0;
4555 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4558 static void rpc_share_userlist_usage(void)
4564 * List shares on a remote RPC server, including the security descriptors
4566 * All parameters are provided by the run_rpc_command function, except for
4567 * argc, argv which are passes through.
4569 * @param domain_sid The domain sid acquired from the remote server
4570 * @param cli A cli_state connected to the server.
4571 * @param mem_ctx Talloc context, destoyed on completion of the function.
4572 * @param argc Standard main() style argc
4573 * @param argv Standard main() style argv. Initial components are already
4576 * @return Normal NTSTATUS return.
4579 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4580 const char *domain_name,
4581 struct cli_state *cli,
4582 struct rpc_pipe_client *pipe_hnd,
4583 TALLOC_CTX *mem_ctx,
4593 struct user_token *tokens = NULL;
4596 struct share_list share_list;
4599 rpc_share_userlist_usage();
4600 return NT_STATUS_UNSUCCESSFUL;
4606 f = fopen(argv[0], "r");
4610 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4611 return NT_STATUS_UNSUCCESSFUL;
4614 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4620 DEBUG(0, ("Could not read users from file\n"));
4621 return NT_STATUS_UNSUCCESSFUL;
4624 for (i=0; i<num_tokens; i++)
4625 collect_alias_memberships(&tokens[i].token);
4627 init_enum_hnd(&hnd, 0);
4629 share_list.num_shares = 0;
4630 share_list.shares = NULL;
4632 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4635 DEBUG(0, ("Error returning browse list: %s\n",
4640 for (i = 0; i < share_list.num_shares; i++) {
4641 char *netname = share_list.shares[i];
4643 if (netname[strlen(netname)-1] == '$')
4646 d_printf("%s\n", netname);
4648 show_userlist(pipe_hnd, mem_ctx, netname,
4649 num_tokens, tokens);
4652 for (i=0; i<num_tokens; i++) {
4653 free_user_token(&tokens[i].token);
4656 SAFE_FREE(share_list.shares);
4658 return NT_STATUS_OK;
4661 static int rpc_share_allowedusers(int argc, const char **argv)
4665 result = run_rpc_command(NULL, PI_SAMR, 0,
4666 rpc_aliaslist_internals,
4671 result = run_rpc_command(NULL, PI_LSARPC, 0,
4677 return run_rpc_command(NULL, PI_SRVSVC, 0,
4678 rpc_share_allowedusers_internals,
4682 int net_usersidlist(int argc, const char **argv)
4685 struct user_token *tokens = NULL;
4689 net_usersidlist_usage(argc, argv);
4693 if (!get_user_tokens(&num_tokens, &tokens)) {
4694 DEBUG(0, ("Could not get the user/sid list\n"));
4698 for (i=0; i<num_tokens; i++) {
4699 dump_user_token(&tokens[i]);
4700 free_user_token(&tokens[i].token);
4707 int net_usersidlist_usage(int argc, const char **argv)
4709 d_printf("net usersidlist\n"
4710 "\tprints out a list of all users the running winbind knows\n"
4711 "\tabout, together with all their SIDs. This is used as\n"
4712 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4714 net_common_flags_usage(argc, argv);
4719 * 'net rpc share' entrypoint.
4720 * @param argc Standard main() style argc
4721 * @param argv Standard main() style argv. Initial components are already
4725 int net_rpc_share(int argc, const char **argv)
4727 struct functable func[] = {
4728 {"add", rpc_share_add},
4729 {"delete", rpc_share_delete},
4730 {"allowedusers", rpc_share_allowedusers},
4731 {"migrate", rpc_share_migrate},
4732 {"list", rpc_share_list},
4737 return run_rpc_command(NULL, PI_SRVSVC, 0,
4738 rpc_share_list_internals,
4741 return net_run_function(argc, argv, func, rpc_share_usage);
4744 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4745 struct rpc_sh_ctx *ctx,
4746 struct rpc_pipe_client *pipe_hnd,
4747 int argc, const char **argv)
4749 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4750 ctx->cli, pipe_hnd, mem_ctx,
4754 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4755 struct rpc_sh_ctx *ctx,
4756 struct rpc_pipe_client *pipe_hnd,
4757 int argc, const char **argv)
4761 if ((argc < 2) || (argc > 3)) {
4762 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4764 return NT_STATUS_INVALID_PARAMETER;
4767 result = rpccli_srvsvc_net_share_add(
4768 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
4769 (argc == 3) ? argv[2] : "",
4770 0, 0, 0, argv[1], NULL, 2, NULL);
4772 return werror_to_ntstatus(result);
4775 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4776 struct rpc_sh_ctx *ctx,
4777 struct rpc_pipe_client *pipe_hnd,
4778 int argc, const char **argv)
4783 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4784 return NT_STATUS_INVALID_PARAMETER;
4787 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
4788 return werror_to_ntstatus(result);
4791 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4792 struct rpc_sh_ctx *ctx,
4793 struct rpc_pipe_client *pipe_hnd,
4794 int argc, const char **argv)
4796 SRV_SHARE_INFO info;
4797 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
4801 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4802 return NT_STATUS_INVALID_PARAMETER;
4805 result = rpccli_srvsvc_net_share_get_info(
4806 pipe_hnd, mem_ctx, argv[0], 2, &info);
4807 if (!W_ERROR_IS_OK(result)) {
4811 d_printf("Name: %s\n",
4812 rpcstr_pull_unistr2_talloc(mem_ctx,
4813 &info2->info_2_str.uni_netname));
4814 d_printf("Comment: %s\n",
4815 rpcstr_pull_unistr2_talloc(mem_ctx,
4816 &info2->info_2_str.uni_remark));
4818 d_printf("Path: %s\n",
4819 rpcstr_pull_unistr2_talloc(mem_ctx,
4820 &info2->info_2_str.uni_path));
4821 d_printf("Password: %s\n",
4822 rpcstr_pull_unistr2_talloc(mem_ctx,
4823 &info2->info_2_str.uni_passwd));
4826 return werror_to_ntstatus(result);
4829 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4830 struct rpc_sh_ctx *ctx)
4832 static struct rpc_sh_cmd cmds[] = {
4834 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4835 "List available shares" },
4837 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4840 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4843 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4844 "Get information about a share" },
4846 { NULL, NULL, 0, NULL, NULL }
4852 /****************************************************************************/
4854 static int rpc_file_usage(int argc, const char **argv)
4856 return net_help_file(argc, argv);
4860 * Close a file on a remote RPC server
4862 * All parameters are provided by the run_rpc_command function, except for
4863 * argc, argv which are passes through.
4865 * @param domain_sid The domain sid acquired from the remote server
4866 * @param cli A cli_state connected to the server.
4867 * @param mem_ctx Talloc context, destoyed on completion of the function.
4868 * @param argc Standard main() style argc
4869 * @param argv Standard main() style argv. Initial components are already
4872 * @return Normal NTSTATUS return.
4874 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4875 const char *domain_name,
4876 struct cli_state *cli,
4877 struct rpc_pipe_client *pipe_hnd,
4878 TALLOC_CTX *mem_ctx,
4883 result = rpccli_srvsvc_net_file_close(pipe_hnd, mem_ctx, atoi(argv[0]));
4884 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4888 * Close a file on a remote RPC server
4890 * @param argc Standard main() style argc
4891 * @param argv Standard main() style argv. Initial components are already
4894 * @return A shell status integer (0 for success)
4896 static int rpc_file_close(int argc, const char **argv)
4899 DEBUG(1, ("No fileid given on close\n"));
4900 return(rpc_file_usage(argc, argv));
4903 return run_rpc_command(NULL, PI_SRVSVC, 0,
4904 rpc_file_close_internals,
4909 * Formatted print of open file info
4911 * @param info3 FILE_INFO_3 contents
4912 * @param str3 strings for FILE_INFO_3
4915 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
4917 fstring user = "", path = "";
4919 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
4920 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
4922 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4923 info3->id, user, info3->perms, info3->num_locks, path);
4927 * List open files on a remote RPC server
4929 * All parameters are provided by the run_rpc_command function, except for
4930 * argc, argv which are passes through.
4932 * @param domain_sid The domain sid acquired from the remote server
4933 * @param cli A cli_state connected to the server.
4934 * @param mem_ctx Talloc context, destoyed on completion of the function.
4935 * @param argc Standard main() style argc
4936 * @param argv Standard main() style argv. Initial components are already
4939 * @return Normal NTSTATUS return.
4942 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4943 const char *domain_name,
4944 struct cli_state *cli,
4945 struct rpc_pipe_client *pipe_hnd,
4946 TALLOC_CTX *mem_ctx,
4950 SRV_FILE_INFO_CTR ctr;
4953 uint32 preferred_len = 0xffffffff, i;
4954 const char *username=NULL;
4956 init_enum_hnd(&hnd, 0);
4958 /* if argc > 0, must be user command */
4960 username = smb_xstrdup(argv[0]);
4962 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
4963 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4965 if (!W_ERROR_IS_OK(result))
4968 /* Display results */
4971 "\nEnumerating open files on remote server:\n\n"\
4972 "\nFileId Opened by Perms Locks Path"\
4973 "\n------ --------- ----- ----- ---- \n");
4974 for (i = 0; i < ctr.num_entries; i++)
4975 display_file_info_3(&ctr.file.info3[i].info_3,
4976 &ctr.file.info3[i].info_3_str);
4978 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4982 * List files for a user on a remote RPC server
4984 * @param argc Standard main() style argc
4985 * @param argv Standard main() style argv. Initial components are already
4988 * @return A shell status integer (0 for success)
4991 static int rpc_file_user(int argc, const char **argv)
4994 DEBUG(1, ("No username given\n"));
4995 return(rpc_file_usage(argc, argv));
4998 return run_rpc_command(NULL, PI_SRVSVC, 0,
4999 rpc_file_list_internals,
5004 * 'net rpc file' entrypoint.
5005 * @param argc Standard main() style argc
5006 * @param argv Standard main() style argv. Initial components are already
5010 int net_rpc_file(int argc, const char **argv)
5012 struct functable func[] = {
5013 {"close", rpc_file_close},
5014 {"user", rpc_file_user},
5016 {"info", rpc_file_info},
5022 return run_rpc_command(NULL, PI_SRVSVC, 0,
5023 rpc_file_list_internals,
5026 return net_run_function(argc, argv, func, rpc_file_usage);
5030 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5032 * All parameters are provided by the run_rpc_command function, except for
5033 * argc, argv which are passed through.
5035 * @param domain_sid The domain sid aquired from the remote server
5036 * @param cli A cli_state connected to the server.
5037 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5038 * @param argc Standard main() style argc
5039 * @param argv Standard main() style argv. Initial components are already
5042 * @return Normal NTSTATUS return.
5045 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
5046 const char *domain_name,
5047 struct cli_state *cli,
5048 struct rpc_pipe_client *pipe_hnd,
5049 TALLOC_CTX *mem_ctx,
5053 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5055 result = rpccli_shutdown_abort(pipe_hnd, mem_ctx);
5057 if (NT_STATUS_IS_OK(result)) {
5058 d_printf("\nShutdown successfully aborted\n");
5059 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5061 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5067 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5069 * All parameters are provided by the run_rpc_command function, except for
5070 * argc, argv which are passed through.
5072 * @param domain_sid The domain sid aquired from the remote server
5073 * @param cli A cli_state connected to the server.
5074 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5075 * @param argc Standard main() style argc
5076 * @param argv Standard main() style argv. Initial components are already
5079 * @return Normal NTSTATUS return.
5082 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5083 const char *domain_name,
5084 struct cli_state *cli,
5085 struct rpc_pipe_client *pipe_hnd,
5086 TALLOC_CTX *mem_ctx,
5090 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5092 result = werror_to_ntstatus(rpccli_reg_abort_shutdown(pipe_hnd, mem_ctx));
5094 if (NT_STATUS_IS_OK(result)) {
5095 d_printf("\nShutdown successfully aborted\n");
5096 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5098 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5104 * ABORT the Shut down of a remote RPC server
5106 * @param argc Standard main() style argc
5107 * @param argv Standard main() style argv. Initial components are already
5110 * @return A shell status integer (0 for success)
5113 static int rpc_shutdown_abort(int argc, const char **argv)
5115 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5116 rpc_shutdown_abort_internals,
5122 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5124 return run_rpc_command(NULL, PI_WINREG, 0,
5125 rpc_reg_shutdown_abort_internals,
5130 * Shut down a remote RPC Server via initshutdown pipe
5132 * All parameters are provided by the run_rpc_command function, except for
5133 * argc, argv which are passes through.
5135 * @param domain_sid The domain sid aquired from the remote server
5136 * @param cli A cli_state connected to the server.
5137 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5138 * @param argc Standard main() style argc
5139 * @param argc Standard main() style argv. Initial components are already
5142 * @return Normal NTSTATUS return.
5145 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5146 const char *domain_name,
5147 struct cli_state *cli,
5148 struct rpc_pipe_client *pipe_hnd,
5149 TALLOC_CTX *mem_ctx,
5153 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5154 const char *msg = "This machine will be shutdown shortly";
5155 uint32 timeout = 20;
5161 timeout = opt_timeout;
5164 /* create an entry */
5165 result = rpccli_shutdown_init(pipe_hnd, mem_ctx, msg, timeout, opt_reboot,
5168 if (NT_STATUS_IS_OK(result)) {
5169 d_printf("\nShutdown of remote machine succeeded\n");
5170 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5172 DEBUG(1,("Shutdown of remote machine failed!\n"));
5178 * Shut down a remote RPC Server via winreg pipe
5180 * All parameters are provided by the run_rpc_command function, except for
5181 * argc, argv which are passes through.
5183 * @param domain_sid The domain sid aquired from the remote server
5184 * @param cli A cli_state connected to the server.
5185 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5186 * @param argc Standard main() style argc
5187 * @param argc Standard main() style argv. Initial components are already
5190 * @return Normal NTSTATUS return.
5193 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5194 const char *domain_name,
5195 struct cli_state *cli,
5196 struct rpc_pipe_client *pipe_hnd,
5197 TALLOC_CTX *mem_ctx,
5202 const char *msg = "This machine will be shutdown shortly";
5203 uint32 timeout = 20;
5208 struct poptOption long_options[] = {
5209 {"message", 'm', POPT_ARG_STRING, &msg},
5210 {"timeout", 't', POPT_ARG_INT, &timeout},
5211 {"reboot", 'r', POPT_ARG_NONE, &reboot},
5212 {"force", 'f', POPT_ARG_NONE, &force},
5216 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
5217 POPT_CONTEXT_KEEP_FIRST);
5219 rc = poptGetNextOpt(pc);
5222 /* an error occurred during option processing */
5223 DEBUG(0, ("%s: %s\n",
5224 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
5226 return NT_STATUS_INVALID_PARAMETER;
5233 timeout = opt_timeout;
5236 /* create an entry */
5237 result = rpccli_reg_shutdown(pipe_hnd, mem_ctx, msg, timeout, opt_reboot, opt_force);
5239 if (W_ERROR_IS_OK(result)) {
5240 d_printf("\nShutdown of remote machine succeeded\n");
5242 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5243 if (W_ERROR_EQUAL(result,WERR_MACHINE_LOCKED))
5244 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5246 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(result));
5249 return werror_to_ntstatus(result);
5253 * Shut down a remote RPC server
5255 * @param argc Standard main() style argc
5256 * @param argc Standard main() style argv. Initial components are already
5259 * @return A shell status integer (0 for success)
5262 static int rpc_shutdown(int argc, const char **argv)
5264 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5265 rpc_init_shutdown_internals,
5269 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5270 rc = run_rpc_command(NULL, PI_WINREG, 0,
5271 rpc_reg_shutdown_internals, argc, argv);
5277 /***************************************************************************
5278 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5280 ***************************************************************************/
5283 * Add interdomain trust account to the RPC server.
5284 * All parameters (except for argc and argv) are passed by run_rpc_command
5287 * @param domain_sid The domain sid acquired from the server
5288 * @param cli A cli_state connected to the server.
5289 * @param mem_ctx Talloc context, destoyed on completion of the function.
5290 * @param argc Standard main() style argc
5291 * @param argc Standard main() style argv. Initial components are already
5294 * @return normal NTSTATUS return code
5297 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5298 const char *domain_name,
5299 struct cli_state *cli,
5300 struct rpc_pipe_client *pipe_hnd,
5301 TALLOC_CTX *mem_ctx,
5305 POLICY_HND connect_pol, domain_pol, user_pol;
5306 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5309 uint32 unknown, user_rid;
5312 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5313 return NT_STATUS_INVALID_PARAMETER;
5317 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5320 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5321 return NT_STATUS_NO_MEMORY;
5324 strupper_m(acct_name);
5326 /* Get samr policy handle */
5327 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5329 if (!NT_STATUS_IS_OK(result)) {
5333 /* Get domain policy handle */
5334 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5335 MAXIMUM_ALLOWED_ACCESS,
5336 domain_sid, &domain_pol);
5337 if (!NT_STATUS_IS_OK(result)) {
5341 /* Create trusting domain's account */
5342 acb_info = ACB_NORMAL;
5343 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5344 mimir: yes, most probably it is */
5346 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5347 acct_name, acb_info, unknown,
5348 &user_pol, &user_rid);
5349 if (!NT_STATUS_IS_OK(result)) {
5354 SAM_USERINFO_CTR ctr;
5355 SAM_USER_INFO_23 p23;
5361 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5365 ZERO_STRUCT(notime);
5369 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5370 acb_info = ACB_DOMTRUST;
5372 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5373 ¬ime, ¬ime, ¬ime,
5374 nostr, nostr, nostr, nostr, nostr,
5375 nostr, nostr, nostr, nostr, nostr,
5376 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5377 0, 0, (char *)pwbuf);
5378 ctr.switch_value = 23;
5379 ctr.info.id23 = &p23;
5380 p23.passmustchange = 0;
5382 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5383 &cli->user_session_key, &ctr);
5385 if (!NT_STATUS_IS_OK(result)) {
5386 DEBUG(0,("Could not set trust account password: %s\n",
5387 nt_errstr(result)));
5393 SAFE_FREE(acct_name);
5398 * Create interdomain trust account for a remote domain.
5400 * @param argc standard argc
5401 * @param argv standard argv without initial components
5403 * @return Integer status (0 means success)
5406 static int rpc_trustdom_add(int argc, const char **argv)
5409 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5412 d_printf("Usage: net rpc trustdom add <domain>\n");
5419 * Remove interdomain trust account from the RPC server.
5420 * All parameters (except for argc and argv) are passed by run_rpc_command
5423 * @param domain_sid The domain sid acquired from the server
5424 * @param cli A cli_state connected to the server.
5425 * @param mem_ctx Talloc context, destoyed on completion of the function.
5426 * @param argc Standard main() style argc
5427 * @param argc Standard main() style argv. Initial components are already
5430 * @return normal NTSTATUS return code
5433 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5434 const char *domain_name,
5435 struct cli_state *cli,
5436 struct rpc_pipe_client *pipe_hnd,
5437 TALLOC_CTX *mem_ctx,
5441 POLICY_HND connect_pol, domain_pol, user_pol;
5442 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5445 DOM_SID trust_acct_sid;
5446 uint32 *user_rids, num_rids, *name_types;
5447 uint32 flags = 0x000003e8; /* Unknown */
5450 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5451 return NT_STATUS_INVALID_PARAMETER;
5455 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5457 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5459 if (acct_name == NULL)
5460 return NT_STATUS_NO_MEMORY;
5462 strupper_m(acct_name);
5464 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
5465 names[0] = acct_name;
5468 /* Get samr policy handle */
5469 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5471 if (!NT_STATUS_IS_OK(result)) {
5475 /* Get domain policy handle */
5476 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5477 MAXIMUM_ALLOWED_ACCESS,
5478 domain_sid, &domain_pol);
5479 if (!NT_STATUS_IS_OK(result)) {
5483 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5485 &user_rids, &name_types);
5487 if (!NT_STATUS_IS_OK(result)) {
5491 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5492 MAXIMUM_ALLOWED_ACCESS,
5493 user_rids[0], &user_pol);
5495 if (!NT_STATUS_IS_OK(result)) {
5499 /* append the rid to the domain sid */
5500 sid_copy(&trust_acct_sid, domain_sid);
5501 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5505 /* remove the sid */
5507 result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5510 if (!NT_STATUS_IS_OK(result)) {
5516 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5518 if (!NT_STATUS_IS_OK(result)) {
5522 if (!NT_STATUS_IS_OK(result)) {
5523 DEBUG(0,("Could not set trust account password: %s\n",
5524 nt_errstr(result)));
5533 * Delete interdomain trust account for a remote domain.
5535 * @param argc standard argc
5536 * @param argv standard argv without initial components
5538 * @return Integer status (0 means success)
5541 static int rpc_trustdom_del(int argc, const char **argv)
5544 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5547 d_printf("Usage: net rpc trustdom del <domain>\n");
5554 * Establish trust relationship to a trusting domain.
5555 * Interdomain account must already be created on remote PDC.
5557 * @param argc standard argc
5558 * @param argv standard argv without initial components
5560 * @return Integer status (0 means success)
5563 static int rpc_trustdom_establish(int argc, const char **argv)
5565 struct cli_state *cli = NULL;
5566 struct in_addr server_ip;
5567 struct rpc_pipe_client *pipe_hnd = NULL;
5568 POLICY_HND connect_hnd;
5569 TALLOC_CTX *mem_ctx;
5571 DOM_SID *domain_sid;
5574 char* domain_name_pol;
5579 * Connect to \\server\ipc$ as 'our domain' account with password
5583 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5587 domain_name = smb_xstrdup(argv[0]);
5588 strupper_m(domain_name);
5590 /* account name used at first is our domain's name with '$' */
5591 asprintf(&acct_name, "%s$", lp_workgroup());
5592 strupper_m(acct_name);
5595 * opt_workgroup will be used by connection functions further,
5596 * hence it should be set to remote domain name instead of ours
5598 if (opt_workgroup) {
5599 opt_workgroup = smb_xstrdup(domain_name);
5602 opt_user_name = acct_name;
5604 /* find the domain controller */
5605 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5606 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5610 /* connect to ipc$ as username/password */
5611 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5612 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5614 /* Is it trusting domain account for sure ? */
5615 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5616 nt_errstr(nt_status)));
5620 /* store who we connected to */
5622 saf_store( domain_name, pdc_name );
5625 * Connect to \\server\ipc$ again (this time anonymously)
5628 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5630 if (NT_STATUS_IS_ERR(nt_status)) {
5631 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5632 domain_name, nt_errstr(nt_status)));
5636 * Use NetServerEnum2 to make sure we're talking to a proper server
5639 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5640 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5641 for domain %s\n", domain_name));
5644 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5645 "domain %s", domain_name))) {
5646 DEBUG(0, ("talloc_init() failed\n"));
5652 * Call LsaOpenPolicy and LsaQueryInfo
5655 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5657 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5662 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5664 if (NT_STATUS_IS_ERR(nt_status)) {
5665 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5666 nt_errstr(nt_status)));
5671 /* Querying info level 5 */
5673 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5675 &domain_name_pol, &domain_sid);
5676 if (NT_STATUS_IS_ERR(nt_status)) {
5677 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5678 nt_errstr(nt_status)));
5683 /* There should be actually query info level 3 (following nt serv behaviour),
5684 but I still don't know if it's _really_ necessary */
5687 * Store the password in secrets db
5690 if (!secrets_store_trusted_domain_password(domain_name,
5693 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5699 * Close the pipes and clean up
5702 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5703 if (NT_STATUS_IS_ERR(nt_status)) {
5704 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5705 nt_errstr(nt_status)));
5712 talloc_destroy(mem_ctx);
5714 d_printf("Trust to domain %s established\n", domain_name);
5719 * Revoke trust relationship to the remote domain
5721 * @param argc standard argc
5722 * @param argv standard argv without initial components
5724 * @return Integer status (0 means success)
5727 static int rpc_trustdom_revoke(int argc, const char **argv)
5731 if (argc < 1) return -1;
5733 /* generate upper cased domain name */
5734 domain_name = smb_xstrdup(argv[0]);
5735 strupper_m(domain_name);
5737 /* delete password of the trust */
5738 if (!trusted_domain_password_delete(domain_name)) {
5739 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5748 * Usage for 'net rpc trustdom' command
5750 * @param argc standard argc
5751 * @param argv standard argv without inital components
5753 * @return Integer status returned to shell
5756 static int rpc_trustdom_usage(int argc, const char **argv)
5758 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5759 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5760 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5761 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5762 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5763 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5768 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5769 const char *domain_name,
5770 struct cli_state *cli,
5771 struct rpc_pipe_client *pipe_hnd,
5772 TALLOC_CTX *mem_ctx,
5777 sid_to_string(str_sid, domain_sid);
5778 d_printf("%s\n", str_sid);
5779 return NT_STATUS_OK;
5782 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5784 fstring ascii_sid, padding;
5785 int pad_len, col_len = 20;
5787 /* convert sid into ascii string */
5788 sid_to_string(ascii_sid, dom_sid);
5790 /* calculate padding space for d_printf to look nicer */
5791 pad_len = col_len - strlen(trusted_dom_name);
5792 padding[pad_len] = 0;
5793 do padding[--pad_len] = ' '; while (pad_len);
5795 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5798 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5799 TALLOC_CTX *mem_ctx,
5802 const char *trusted_dom_name)
5805 LSA_TRUSTED_DOMAIN_INFO *info;
5806 char *cleartextpwd = NULL;
5809 nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5811 if (NT_STATUS_IS_ERR(nt_status)) {
5812 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5813 nt_errstr(nt_status)));
5817 data = data_blob(NULL, info->password.password.length);
5819 memcpy(data.data, info->password.password.data, info->password.password.length);
5820 data.length = info->password.password.length;
5822 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5824 if (cleartextpwd == NULL) {
5825 DEBUG(0,("retrieved NULL password\n"));
5826 nt_status = NT_STATUS_UNSUCCESSFUL;
5830 if (!secrets_store_trusted_domain_password(trusted_dom_name,
5833 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5834 nt_status = NT_STATUS_UNSUCCESSFUL;
5838 #ifdef DEBUG_PASSWORD
5839 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5840 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5844 SAFE_FREE(cleartextpwd);
5845 data_blob_free(&data);
5850 static int rpc_trustdom_vampire(int argc, const char **argv)
5852 /* common variables */
5853 TALLOC_CTX* mem_ctx;
5854 struct cli_state *cli = NULL;
5855 struct rpc_pipe_client *pipe_hnd = NULL;
5857 const char *domain_name = NULL;
5858 DOM_SID *queried_dom_sid;
5859 POLICY_HND connect_hnd;
5861 /* trusted domains listing variables */
5862 unsigned int num_domains, enum_ctx = 0;
5864 DOM_SID *domain_sids;
5865 char **trusted_dom_names;
5870 * Listing trusted domains (stored in secrets.tdb, if local)
5873 mem_ctx = talloc_init("trust relationships vampire");
5876 * set domain and pdc name to local samba server (default)
5877 * or to remote one given in command line
5880 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5881 domain_name = opt_workgroup;
5882 opt_target_workgroup = opt_workgroup;
5884 fstrcpy(pdc_name, global_myname());
5885 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5886 opt_target_workgroup = domain_name;
5889 /* open \PIPE\lsarpc and open policy handle */
5890 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5891 DEBUG(0, ("Couldn't connect to domain controller\n"));
5895 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5897 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5898 nt_errstr(nt_status) ));
5903 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5905 if (NT_STATUS_IS_ERR(nt_status)) {
5906 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5907 nt_errstr(nt_status)));
5912 /* query info level 5 to obtain sid of a domain being queried */
5913 nt_status = rpccli_lsa_query_info_policy(
5914 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5915 &dummy, &queried_dom_sid);
5917 if (NT_STATUS_IS_ERR(nt_status)) {
5918 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5919 nt_errstr(nt_status)));
5925 * Keep calling LsaEnumTrustdom over opened pipe until
5926 * the end of enumeration is reached
5929 d_printf("Vampire trusted domains:\n\n");
5932 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5934 &trusted_dom_names, &domain_sids);
5936 if (NT_STATUS_IS_ERR(nt_status)) {
5937 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5938 nt_errstr(nt_status)));
5943 for (i = 0; i < num_domains; i++) {
5945 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5947 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5948 domain_sids[i], trusted_dom_names[i]);
5949 if (!NT_STATUS_IS_OK(nt_status)) {
5956 * in case of no trusted domains say something rather
5957 * than just display blank line
5959 if (!num_domains) d_printf("none\n");
5961 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5963 /* close this connection before doing next one */
5964 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5965 if (NT_STATUS_IS_ERR(nt_status)) {
5966 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5967 nt_errstr(nt_status)));
5972 /* close lsarpc pipe and connection to IPC$ */
5975 talloc_destroy(mem_ctx);
5979 static int rpc_trustdom_list(int argc, const char **argv)
5981 /* common variables */
5982 TALLOC_CTX* mem_ctx;
5983 struct cli_state *cli = NULL, *remote_cli = NULL;
5984 struct rpc_pipe_client *pipe_hnd = NULL;
5986 const char *domain_name = NULL;
5987 DOM_SID *queried_dom_sid;
5989 int ascii_dom_name_len;
5990 POLICY_HND connect_hnd;
5992 /* trusted domains listing variables */
5993 unsigned int num_domains, enum_ctx = 0;
5994 int i, pad_len, col_len = 20;
5995 DOM_SID *domain_sids;
5996 char **trusted_dom_names;
6000 /* trusting domains listing variables */
6001 POLICY_HND domain_hnd;
6002 char **trusting_dom_names;
6003 uint32 *trusting_dom_rids;
6006 * Listing trusted domains (stored in secrets.tdb, if local)
6009 mem_ctx = talloc_init("trust relationships listing");
6012 * set domain and pdc name to local samba server (default)
6013 * or to remote one given in command line
6016 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6017 domain_name = opt_workgroup;
6018 opt_target_workgroup = opt_workgroup;
6020 fstrcpy(pdc_name, global_myname());
6021 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6022 opt_target_workgroup = domain_name;
6025 /* open \PIPE\lsarpc and open policy handle */
6026 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
6027 DEBUG(0, ("Couldn't connect to domain controller\n"));
6031 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6033 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6034 nt_errstr(nt_status) ));
6038 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6040 if (NT_STATUS_IS_ERR(nt_status)) {
6041 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6042 nt_errstr(nt_status)));
6046 /* query info level 5 to obtain sid of a domain being queried */
6047 nt_status = rpccli_lsa_query_info_policy(
6048 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
6049 &dummy, &queried_dom_sid);
6051 if (NT_STATUS_IS_ERR(nt_status)) {
6052 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6053 nt_errstr(nt_status)));
6058 * Keep calling LsaEnumTrustdom over opened pipe until
6059 * the end of enumeration is reached
6062 d_printf("Trusted domains list:\n\n");
6065 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6067 &trusted_dom_names, &domain_sids);
6069 if (NT_STATUS_IS_ERR(nt_status)) {
6070 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6071 nt_errstr(nt_status)));
6075 for (i = 0; i < num_domains; i++) {
6076 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6080 * in case of no trusted domains say something rather
6081 * than just display blank line
6083 if (!num_domains) d_printf("none\n");
6085 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6087 /* close this connection before doing next one */
6088 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
6089 if (NT_STATUS_IS_ERR(nt_status)) {
6090 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6091 nt_errstr(nt_status)));
6095 cli_rpc_pipe_close(pipe_hnd);
6098 * Listing trusting domains (stored in passdb backend, if local)
6101 d_printf("\nTrusting domains list:\n\n");
6104 * Open \PIPE\samr and get needed policy handles
6106 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6108 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6113 nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
6115 if (!NT_STATUS_IS_OK(nt_status)) {
6116 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6117 nt_errstr(nt_status)));
6121 /* SamrOpenDomain - we have to open domain policy handle in order to be
6122 able to enumerate accounts*/
6123 nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
6124 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6125 queried_dom_sid, &domain_hnd);
6126 if (!NT_STATUS_IS_OK(nt_status)) {
6127 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6128 nt_errstr(nt_status)));
6133 * perform actual enumeration
6136 enum_ctx = 0; /* reset enumeration context from last enumeration */
6139 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6140 &enum_ctx, ACB_DOMTRUST, 0xffff,
6141 &trusting_dom_names, &trusting_dom_rids,
6143 if (NT_STATUS_IS_ERR(nt_status)) {
6144 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6145 nt_errstr(nt_status)));
6149 for (i = 0; i < num_domains; i++) {
6152 * get each single domain's sid (do we _really_ need this ?):
6153 * 1) connect to domain's pdc
6154 * 2) query the pdc for domain's sid
6157 /* get rid of '$' tail */
6158 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6159 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6160 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6162 /* calculate padding space for d_printf to look nicer */
6163 pad_len = col_len - strlen(trusting_dom_names[i]);
6164 padding[pad_len] = 0;
6165 do padding[--pad_len] = ' '; while (pad_len);
6167 /* set opt_* variables to remote domain */
6168 strupper_m(trusting_dom_names[i]);
6169 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6170 opt_target_workgroup = opt_workgroup;
6172 d_printf("%s%s", trusting_dom_names[i], padding);
6174 /* connect to remote domain controller */
6175 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6177 /* query for domain's sid */
6178 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6179 d_fprintf(stderr, "couldn't get domain's sid\n");
6181 cli_shutdown(remote_cli);
6184 d_fprintf(stderr, "domain controller is not responding\n");
6188 if (!num_domains) d_printf("none\n");
6190 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6192 /* close opened samr and domain policy handles */
6193 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6194 if (!NT_STATUS_IS_OK(nt_status)) {
6195 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6198 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6199 if (!NT_STATUS_IS_OK(nt_status)) {
6200 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6203 /* close samr pipe and connection to IPC$ */
6206 talloc_destroy(mem_ctx);
6211 * Entrypoint for 'net rpc trustdom' code
6213 * @param argc standard argc
6214 * @param argv standard argv without initial components
6216 * @return Integer status (0 means success)
6219 static int rpc_trustdom(int argc, const char **argv)
6221 struct functable func[] = {
6222 {"add", rpc_trustdom_add},
6223 {"del", rpc_trustdom_del},
6224 {"establish", rpc_trustdom_establish},
6225 {"revoke", rpc_trustdom_revoke},
6226 {"help", rpc_trustdom_usage},
6227 {"list", rpc_trustdom_list},
6228 {"vampire", rpc_trustdom_vampire},
6233 rpc_trustdom_usage(argc, argv);
6237 return (net_run_function(argc, argv, func, rpc_user_usage));
6241 * Check if a server will take rpc commands
6242 * @param flags Type of server to connect to (PDC, DMB, localhost)
6243 * if the host is not explicitly specified
6244 * @return BOOL (true means rpc supported)
6246 BOOL net_rpc_check(unsigned flags)
6248 struct cli_state cli;
6250 struct in_addr server_ip;
6251 char *server_name = NULL;
6253 /* flags (i.e. server type) may depend on command */
6254 if (!net_find_server(NULL, flags, &server_ip, &server_name))
6258 if (cli_initialise(&cli) == False)
6261 if (!cli_connect(&cli, server_name, &server_ip))
6263 if (!attempt_netbios_session_request(&cli, global_myname(),
6264 server_name, &server_ip))
6266 if (!cli_negprot(&cli))
6268 if (cli.protocol < PROTOCOL_NT1)
6277 /* dump sam database via samsync rpc calls */
6278 static int rpc_samdump(int argc, const char **argv) {
6279 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6283 /* syncronise sam database via samsync rpc calls */
6284 static int rpc_vampire(int argc, const char **argv) {
6285 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6290 * Migrate everything from a print-server
6292 * @param argc Standard main() style argc
6293 * @param argv Standard main() style argv. Initial components are already
6296 * @return A shell status integer (0 for success)
6298 * The order is important !
6299 * To successfully add drivers the print-queues have to exist !
6300 * Applying ACLs should be the last step, because you're easily locked out
6303 static int rpc_printer_migrate_all(int argc, const char **argv)
6308 printf("no server to migrate\n");
6312 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6316 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6320 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6324 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6328 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6333 * Migrate print-drivers from a print-server
6335 * @param argc Standard main() style argc
6336 * @param argv Standard main() style argv. Initial components are already
6339 * @return A shell status integer (0 for success)
6341 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6344 printf("no server to migrate\n");
6348 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6349 rpc_printer_migrate_drivers_internals,
6354 * Migrate print-forms from a print-server
6356 * @param argc Standard main() style argc
6357 * @param argv Standard main() style argv. Initial components are already
6360 * @return A shell status integer (0 for success)
6362 static int rpc_printer_migrate_forms(int argc, const char **argv)
6365 printf("no server to migrate\n");
6369 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6370 rpc_printer_migrate_forms_internals,
6375 * Migrate printers from a print-server
6377 * @param argc Standard main() style argc
6378 * @param argv Standard main() style argv. Initial components are already
6381 * @return A shell status integer (0 for success)
6383 static int rpc_printer_migrate_printers(int argc, const char **argv)
6386 printf("no server to migrate\n");
6390 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6391 rpc_printer_migrate_printers_internals,
6396 * Migrate printer-ACLs from a print-server
6398 * @param argc Standard main() style argc
6399 * @param argv Standard main() style argv. Initial components are already
6402 * @return A shell status integer (0 for success)
6404 static int rpc_printer_migrate_security(int argc, const char **argv)
6407 printf("no server to migrate\n");
6411 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6412 rpc_printer_migrate_security_internals,
6417 * Migrate printer-settings from a print-server
6419 * @param argc Standard main() style argc
6420 * @param argv Standard main() style argv. Initial components are already
6423 * @return A shell status integer (0 for success)
6425 static int rpc_printer_migrate_settings(int argc, const char **argv)
6428 printf("no server to migrate\n");
6432 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6433 rpc_printer_migrate_settings_internals,
6438 * 'net rpc printer' entrypoint.
6439 * @param argc Standard main() style argc
6440 * @param argv Standard main() style argv. Initial components are already
6444 int rpc_printer_migrate(int argc, const char **argv)
6447 /* ouch: when addriver and setdriver are called from within
6448 rpc_printer_migrate_drivers_internals, the printer-queue already
6451 struct functable func[] = {
6452 {"all", rpc_printer_migrate_all},
6453 {"drivers", rpc_printer_migrate_drivers},
6454 {"forms", rpc_printer_migrate_forms},
6455 {"help", rpc_printer_usage},
6456 {"printers", rpc_printer_migrate_printers},
6457 {"security", rpc_printer_migrate_security},
6458 {"settings", rpc_printer_migrate_settings},
6462 return net_run_function(argc, argv, func, rpc_printer_usage);
6467 * List printers on a remote RPC server
6469 * @param argc Standard main() style argc
6470 * @param argv Standard main() style argv. Initial components are already
6473 * @return A shell status integer (0 for success)
6475 static int rpc_printer_list(int argc, const char **argv)
6478 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6479 rpc_printer_list_internals,
6484 * List printer-drivers on a remote RPC server
6486 * @param argc Standard main() style argc
6487 * @param argv Standard main() style argv. Initial components are already
6490 * @return A shell status integer (0 for success)
6492 static int rpc_printer_driver_list(int argc, const char **argv)
6495 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6496 rpc_printer_driver_list_internals,
6501 * Publish printer in ADS via MSRPC
6503 * @param argc Standard main() style argc
6504 * @param argv Standard main() style argv. Initial components are already
6507 * @return A shell status integer (0 for success)
6509 static int rpc_printer_publish_publish(int argc, const char **argv)
6512 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6513 rpc_printer_publish_publish_internals,
6518 * Update printer in ADS via MSRPC
6520 * @param argc Standard main() style argc
6521 * @param argv Standard main() style argv. Initial components are already
6524 * @return A shell status integer (0 for success)
6526 static int rpc_printer_publish_update(int argc, const char **argv)
6529 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6530 rpc_printer_publish_update_internals,
6535 * UnPublish printer in ADS via MSRPC
6537 * @param argc Standard main() style argc
6538 * @param argv Standard main() style argv. Initial components are already
6541 * @return A shell status integer (0 for success)
6543 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6546 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6547 rpc_printer_publish_unpublish_internals,
6552 * List published printers via MSRPC
6554 * @param argc Standard main() style argc
6555 * @param argv Standard main() style argv. Initial components are already
6558 * @return A shell status integer (0 for success)
6560 static int rpc_printer_publish_list(int argc, const char **argv)
6563 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6564 rpc_printer_publish_list_internals,
6570 * Publish printer in ADS
6572 * @param argc Standard main() style argc
6573 * @param argv Standard main() style argv. Initial components are already
6576 * @return A shell status integer (0 for success)
6578 static int rpc_printer_publish(int argc, const char **argv)
6581 struct functable func[] = {
6582 {"publish", rpc_printer_publish_publish},
6583 {"update", rpc_printer_publish_update},
6584 {"unpublish", rpc_printer_publish_unpublish},
6585 {"list", rpc_printer_publish_list},
6586 {"help", rpc_printer_usage},
6591 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6592 rpc_printer_publish_list_internals,
6595 return net_run_function(argc, argv, func, rpc_printer_usage);
6601 * Display rpc printer help page.
6602 * @param argc Standard main() style argc
6603 * @param argv Standard main() style argv. Initial components are already
6606 int rpc_printer_usage(int argc, const char **argv)
6608 return net_help_printer(argc, argv);
6612 * 'net rpc printer' entrypoint.
6613 * @param argc Standard main() style argc
6614 * @param argv Standard main() style argv. Initial components are already
6617 int net_rpc_printer(int argc, const char **argv)
6619 struct functable func[] = {
6620 {"list", rpc_printer_list},
6621 {"migrate", rpc_printer_migrate},
6622 {"driver", rpc_printer_driver_list},
6623 {"publish", rpc_printer_publish},
6628 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6629 rpc_printer_list_internals,
6632 return net_run_function(argc, argv, func, rpc_printer_usage);
6635 /****************************************************************************/
6639 * Basic usage function for 'net rpc'
6640 * @param argc Standard main() style argc
6641 * @param argv Standard main() style argv. Initial components are already
6645 int net_rpc_usage(int argc, const char **argv)
6647 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6648 d_printf(" net rpc join \t\t\tto join a domain \n");
6649 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6650 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6651 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6652 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6653 d_printf(" net rpc group \t\tto list groups\n");
6654 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6655 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6656 d_printf(" net rpc file \t\t\tto list open files\n");
6657 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6658 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6659 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6660 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6661 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6662 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6663 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6664 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6665 d_printf(" net rpc registry\t\tto manage registry hives\n");
6666 d_printf(" net rpc service\t\tto start, stop and query services\n");
6667 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6668 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6670 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6671 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6672 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6673 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6674 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6680 * Help function for 'net rpc'. Calls command specific help if requested
6681 * or displays usage of net rpc
6682 * @param argc Standard main() style argc
6683 * @param argv Standard main() style argv. Initial components are already
6687 int net_rpc_help(int argc, const char **argv)
6689 struct functable func[] = {
6690 {"join", rpc_join_usage},
6691 {"user", rpc_user_usage},
6692 {"group", rpc_group_usage},
6693 {"share", rpc_share_usage},
6694 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6695 {"trustdom", rpc_trustdom_usage},
6696 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6697 /*{"shutdown", rpc_shutdown_usage}, */
6698 {"vampire", rpc_vampire_usage},
6703 net_rpc_usage(argc, argv);
6707 return (net_run_function(argc, argv, func, rpc_user_usage));
6711 * 'net rpc' entrypoint.
6712 * @param argc Standard main() style argc
6713 * @param argv Standard main() style argv. Initial components are already
6717 int net_rpc(int argc, const char **argv)
6719 struct functable func[] = {
6720 {"audit", net_rpc_audit},
6721 {"info", net_rpc_info},
6722 {"join", net_rpc_join},
6723 {"oldjoin", net_rpc_oldjoin},
6724 {"testjoin", net_rpc_testjoin},
6725 {"user", net_rpc_user},
6726 {"password", rpc_user_password},
6727 {"group", net_rpc_group},
6728 {"share", net_rpc_share},
6729 {"file", net_rpc_file},
6730 {"printer", net_rpc_printer},
6731 {"changetrustpw", net_rpc_changetrustpw},
6732 {"trustdom", rpc_trustdom},
6733 {"abortshutdown", rpc_shutdown_abort},
6734 {"shutdown", rpc_shutdown},
6735 {"samdump", rpc_samdump},
6736 {"vampire", rpc_vampire},
6737 {"getsid", net_rpc_getsid},
6738 {"rights", net_rpc_rights},
6739 {"service", net_rpc_service},
6740 {"registry", net_rpc_registry},
6741 {"shell", net_rpc_shell},
6742 {"help", net_rpc_help},
6745 return net_run_function(argc, argv, func, net_rpc_usage);