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 if ((names = TALLOC_ARRAY(mem_ctx, const char *, num_names)) == NULL) {
862 result = NT_STATUS_NO_MEMORY;
866 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
867 flags, num_names, names,
868 &num_rids, &user_rid, &name_types);
869 if (!NT_STATUS_IS_OK(result)) {
873 /* Open domain user */
874 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
875 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
877 if (!NT_STATUS_IS_OK(result)) {
881 /* Query user info */
882 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
883 info_level, &user_ctr);
885 if (!NT_STATUS_IS_OK(result)) {
889 ctr.switch_value = info_level;
890 ctr.info.id7 = &info7;
892 init_sam_user_info7(&info7, new_name);
895 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
896 info_level, &cli->user_session_key, &ctr);
898 if (!NT_STATUS_IS_OK(result)) {
903 if (!NT_STATUS_IS_OK(result)) {
904 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
907 d_printf("Renamed user from %s to %s\n", old_name, new_name);
913 * Rename a user on a remote RPC server
915 * @param argc Standard main() style argc
916 * @param argv Standard main() style argv. Initial components are already
919 * @return A shell status integer (0 for success)
922 static int rpc_user_rename(int argc, const char **argv)
924 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
929 * Delete a user from a remote RPC server
931 * @param argc Standard main() style argc
932 * @param argv Standard main() style argv. Initial components are already
935 * @return A shell status integer (0 for success)
938 static int rpc_user_delete(int argc, const char **argv)
940 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
945 * Set a password for a user on a remote RPC server
947 * All parameters are provided by the run_rpc_command function, except for
948 * argc, argv which are passes through.
950 * @param domain_sid The domain sid acquired from the remote server
951 * @param cli A cli_state connected to the server.
952 * @param mem_ctx Talloc context, destoyed on completion of the function.
953 * @param argc Standard main() style argc
954 * @param argv Standard main() style argv. Initial components are already
957 * @return Normal NTSTATUS return.
960 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
961 const char *domain_name,
962 struct cli_state *cli,
963 struct rpc_pipe_client *pipe_hnd,
968 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
969 POLICY_HND connect_pol, domain_pol, user_pol;
970 SAM_USERINFO_CTR ctr;
971 SAM_USER_INFO_24 p24;
974 const char *new_password;
978 d_printf("User must be specified\n");
979 rpc_user_usage(argc, argv);
986 new_password = argv[1];
988 asprintf(&prompt, "Enter new password for %s:", user);
989 new_password = getpass(prompt);
993 /* Get sam policy and domain handles */
995 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
998 if (!NT_STATUS_IS_OK(result)) {
1002 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1003 MAXIMUM_ALLOWED_ACCESS,
1004 domain_sid, &domain_pol);
1006 if (!NT_STATUS_IS_OK(result)) {
1010 /* Get handle on user */
1013 uint32 *user_rids, num_rids, *name_types;
1014 uint32 flags = 0x000003e8; /* Unknown */
1016 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1018 &num_rids, &user_rids,
1021 if (!NT_STATUS_IS_OK(result)) {
1025 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1026 MAXIMUM_ALLOWED_ACCESS,
1027 user_rids[0], &user_pol);
1029 if (!NT_STATUS_IS_OK(result)) {
1034 /* Set password on account */
1039 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
1041 init_sam_user_info24(&p24, (char *)pwbuf,24);
1043 ctr.switch_value = 24;
1044 ctr.info.id24 = &p24;
1046 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
1047 &cli->user_session_key, &ctr);
1049 if (!NT_STATUS_IS_OK(result)) {
1053 /* Display results */
1061 * Set a user's password on a remote RPC server
1063 * @param argc Standard main() style argc
1064 * @param argv Standard main() style argv. Initial components are already
1067 * @return A shell status integer (0 for success)
1070 static int rpc_user_password(int argc, const char **argv)
1072 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1077 * List user's groups on a remote RPC server
1079 * All parameters are provided by the run_rpc_command function, except for
1080 * argc, argv which are passes through.
1082 * @param domain_sid The domain sid acquired from the remote server
1083 * @param cli A cli_state connected to the server.
1084 * @param mem_ctx Talloc context, destoyed on completion of the function.
1085 * @param argc Standard main() style argc
1086 * @param argv Standard main() style argv. Initial components are already
1089 * @return Normal NTSTATUS return.
1092 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1093 const char *domain_name,
1094 struct cli_state *cli,
1095 struct rpc_pipe_client *pipe_hnd,
1096 TALLOC_CTX *mem_ctx,
1100 POLICY_HND connect_pol, domain_pol, user_pol;
1101 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1102 uint32 *rids, num_rids, *name_types, num_names;
1103 uint32 flags = 0x000003e8; /* Unknown */
1109 d_printf("User must be specified\n");
1110 rpc_user_usage(argc, argv);
1111 return NT_STATUS_OK;
1113 /* Get sam policy handle */
1115 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1117 if (!NT_STATUS_IS_OK(result)) goto done;
1119 /* Get domain policy handle */
1121 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1122 MAXIMUM_ALLOWED_ACCESS,
1123 domain_sid, &domain_pol);
1124 if (!NT_STATUS_IS_OK(result)) goto done;
1126 /* Get handle on user */
1128 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1130 &num_rids, &rids, &name_types);
1132 if (!NT_STATUS_IS_OK(result)) goto done;
1134 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1135 MAXIMUM_ALLOWED_ACCESS,
1136 rids[0], &user_pol);
1137 if (!NT_STATUS_IS_OK(result)) goto done;
1139 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1140 &num_rids, &user_gids);
1142 if (!NT_STATUS_IS_OK(result)) goto done;
1147 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1148 result = NT_STATUS_NO_MEMORY;
1152 for (i = 0; i < num_rids; i++)
1153 rids[i] = user_gids[i].g_rid;
1155 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1157 &num_names, &names, &name_types);
1159 if (!NT_STATUS_IS_OK(result)) {
1163 /* Display results */
1165 for (i = 0; i < num_names; i++)
1166 printf("%s\n", names[i]);
1173 * List a user's groups from a remote RPC server
1175 * @param argc Standard main() style argc
1176 * @param argv Standard main() style argv. Initial components are already
1179 * @return A shell status integer (0 for success)
1182 static int rpc_user_info(int argc, const char **argv)
1184 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1189 * List users on a remote RPC server
1191 * All parameters are provided by the run_rpc_command function, except for
1192 * argc, argv which are passes through.
1194 * @param domain_sid The domain sid acquired from the remote server
1195 * @param cli A cli_state connected to the server.
1196 * @param mem_ctx Talloc context, destoyed on completion of the function.
1197 * @param argc Standard main() style argc
1198 * @param argv Standard main() style argv. Initial components are already
1201 * @return Normal NTSTATUS return.
1204 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1205 const char *domain_name,
1206 struct cli_state *cli,
1207 struct rpc_pipe_client *pipe_hnd,
1208 TALLOC_CTX *mem_ctx,
1212 POLICY_HND connect_pol, domain_pol;
1213 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1214 uint32 start_idx=0, num_entries, i, loop_count = 0;
1215 SAM_DISPINFO_CTR ctr;
1216 SAM_DISPINFO_1 info1;
1218 /* Get sam policy handle */
1220 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1222 if (!NT_STATUS_IS_OK(result)) {
1226 /* Get domain policy handle */
1228 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1229 MAXIMUM_ALLOWED_ACCESS,
1230 domain_sid, &domain_pol);
1231 if (!NT_STATUS_IS_OK(result)) {
1235 /* Query domain users */
1238 ctr.sam.info1 = &info1;
1239 if (opt_long_list_entries)
1240 d_printf("\nUser name Comment"\
1241 "\n-----------------------------\n");
1244 uint32 max_entries, max_size;
1246 get_query_dispinfo_params(
1247 loop_count, &max_entries, &max_size);
1249 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1250 &start_idx, 1, &num_entries,
1251 max_entries, max_size, &ctr);
1254 for (i = 0; i < num_entries; i++) {
1255 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1256 if (opt_long_list_entries)
1257 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1259 if (opt_long_list_entries)
1260 printf("%-21.21s %s\n", user, desc);
1262 printf("%s\n", user);
1264 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1271 * 'net rpc user' entrypoint.
1272 * @param argc Standard main() style argc
1273 * @param argc Standard main() style argv. Initial components are already
1277 int net_rpc_user(int argc, const char **argv)
1279 struct functable func[] = {
1280 {"add", rpc_user_add},
1281 {"info", rpc_user_info},
1282 {"delete", rpc_user_delete},
1283 {"password", rpc_user_password},
1284 {"rename", rpc_user_rename},
1289 return run_rpc_command(NULL,PI_SAMR, 0,
1290 rpc_user_list_internals,
1294 return net_run_function(argc, argv, func, rpc_user_usage);
1297 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1298 struct rpc_sh_ctx *ctx,
1299 struct rpc_pipe_client *pipe_hnd,
1300 int argc, const char **argv)
1302 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1303 ctx->cli, pipe_hnd, mem_ctx,
1307 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1308 struct rpc_sh_ctx *ctx,
1309 struct rpc_pipe_client *pipe_hnd,
1310 int argc, const char **argv)
1312 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1313 ctx->cli, pipe_hnd, mem_ctx,
1317 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1318 struct rpc_sh_ctx *ctx,
1319 struct rpc_pipe_client *pipe_hnd,
1320 int argc, const char **argv,
1322 TALLOC_CTX *mem_ctx,
1323 struct rpc_sh_ctx *ctx,
1324 struct rpc_pipe_client *pipe_hnd,
1325 const POLICY_HND *user_hnd,
1326 int argc, const char **argv))
1329 POLICY_HND connect_pol, domain_pol, user_pol;
1330 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1333 enum SID_NAME_USE type;
1336 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1337 return NT_STATUS_INVALID_PARAMETER;
1340 ZERO_STRUCT(connect_pol);
1341 ZERO_STRUCT(domain_pol);
1342 ZERO_STRUCT(user_pol);
1344 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1345 NULL, NULL, &sid, &type);
1346 if (!NT_STATUS_IS_OK(result)) {
1347 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1352 if (type != SID_NAME_USER) {
1353 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1354 sid_type_lookup(type));
1355 result = NT_STATUS_NO_SUCH_USER;
1359 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1360 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1361 result = NT_STATUS_NO_SUCH_USER;
1365 result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1366 MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1367 if (!NT_STATUS_IS_OK(result)) {
1371 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1372 MAXIMUM_ALLOWED_ACCESS,
1373 ctx->domain_sid, &domain_pol);
1374 if (!NT_STATUS_IS_OK(result)) {
1378 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1379 MAXIMUM_ALLOWED_ACCESS,
1381 if (!NT_STATUS_IS_OK(result)) {
1385 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1388 if (is_valid_policy_hnd(&user_pol)) {
1389 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1391 if (is_valid_policy_hnd(&domain_pol)) {
1392 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1394 if (is_valid_policy_hnd(&connect_pol)) {
1395 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1400 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1401 struct rpc_sh_ctx *ctx,
1402 struct rpc_pipe_client *pipe_hnd,
1403 const POLICY_HND *user_hnd,
1404 int argc, const char **argv)
1407 SAM_USERINFO_CTR *ctr;
1408 SAM_USER_INFO_21 *info;
1411 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1412 return NT_STATUS_INVALID_PARAMETER;
1415 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1417 if (!NT_STATUS_IS_OK(result)) {
1421 info = ctr->info.id21;
1423 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1429 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1430 struct rpc_sh_ctx *ctx,
1431 struct rpc_pipe_client *pipe_hnd,
1432 int argc, const char **argv)
1434 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1435 rpc_sh_user_show_internals);
1438 #define FETCHSTR(name, rec) \
1439 do { if (strequal(ctx->thiscmd, name)) { \
1440 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1443 #define SETSTR(name, rec, flag) \
1444 do { if (strequal(ctx->thiscmd, name)) { \
1445 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1446 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1447 usr->fields_present |= ACCT_##flag; } \
1450 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1451 struct rpc_sh_ctx *ctx,
1452 struct rpc_pipe_client *pipe_hnd,
1453 const POLICY_HND *user_hnd,
1454 int argc, const char **argv)
1457 SAM_USERINFO_CTR *ctr;
1458 SAM_USER_INFO_21 *usr;
1459 const char *username;
1460 const char *oldval = "";
1463 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1465 return NT_STATUS_INVALID_PARAMETER;
1468 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1470 if (!NT_STATUS_IS_OK(result)) {
1474 usr = ctr->info.id21;
1476 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1478 FETCHSTR("fullname", full_name);
1479 FETCHSTR("homedir", home_dir);
1480 FETCHSTR("homedrive", dir_drive);
1481 FETCHSTR("logonscript", logon_script);
1482 FETCHSTR("profilepath", profile_path);
1483 FETCHSTR("description", acct_desc);
1486 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1492 if (strcmp(argv[0], "NULL") == 0) {
1496 SETSTR("fullname", full_name, FULL_NAME);
1497 SETSTR("homedir", home_dir, HOME_DIR);
1498 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1499 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1500 SETSTR("profilepath", profile_path, PROFILE);
1501 SETSTR("description", acct_desc, DESCRIPTION);
1503 result = rpccli_samr_set_userinfo2(
1504 pipe_hnd, mem_ctx, user_hnd, 21,
1505 &pipe_hnd->cli->user_session_key, ctr);
1507 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1508 ctx->thiscmd, oldval, argv[0]);
1515 #define HANDLEFLG(name, rec) \
1516 do { if (strequal(ctx->thiscmd, name)) { \
1517 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1519 newflags = oldflags | ACB_##rec; \
1521 newflags = oldflags & ~ACB_##rec; \
1524 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1525 struct rpc_sh_ctx *ctx,
1526 struct rpc_pipe_client *pipe_hnd,
1527 int argc, const char **argv)
1529 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1530 rpc_sh_user_str_edit_internals);
1533 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1534 struct rpc_sh_ctx *ctx,
1535 struct rpc_pipe_client *pipe_hnd,
1536 const POLICY_HND *user_hnd,
1537 int argc, const char **argv)
1540 SAM_USERINFO_CTR *ctr;
1541 SAM_USER_INFO_21 *usr;
1542 const char *username;
1543 const char *oldval = "unknown";
1544 uint32 oldflags, newflags;
1548 ((argc == 1) && !strequal(argv[0], "yes") &&
1549 !strequal(argv[0], "no"))) {
1550 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1552 return NT_STATUS_INVALID_PARAMETER;
1555 newval = strequal(argv[0], "yes");
1557 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1559 if (!NT_STATUS_IS_OK(result)) {
1563 usr = ctr->info.id21;
1565 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1566 oldflags = usr->acb_info;
1567 newflags = usr->acb_info;
1569 HANDLEFLG("disabled", DISABLED);
1570 HANDLEFLG("pwnotreq", PWNOTREQ);
1571 HANDLEFLG("autolock", AUTOLOCK);
1572 HANDLEFLG("pwnoexp", PWNOEXP);
1575 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1581 usr->acb_info = newflags;
1582 usr->fields_present = ACCT_FLAGS;
1584 result = rpccli_samr_set_userinfo2(
1585 pipe_hnd, mem_ctx, user_hnd, 21,
1586 &pipe_hnd->cli->user_session_key, ctr);
1588 if (NT_STATUS_IS_OK(result)) {
1589 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1590 ctx->thiscmd, oldval, argv[0]);
1598 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1599 struct rpc_sh_ctx *ctx,
1600 struct rpc_pipe_client *pipe_hnd,
1601 int argc, const char **argv)
1603 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1604 rpc_sh_user_flag_edit_internals);
1607 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1608 struct rpc_sh_ctx *ctx)
1610 static struct rpc_sh_cmd cmds[] = {
1612 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1613 "Show/Set a user's full name" },
1615 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1616 "Show/Set a user's home directory" },
1618 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1619 "Show/Set a user's home drive" },
1621 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1622 "Show/Set a user's logon script" },
1624 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1625 "Show/Set a user's profile path" },
1627 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1628 "Show/Set a user's description" },
1630 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1631 "Show/Set whether a user is disabled" },
1633 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1634 "Show/Set whether a user locked out" },
1636 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1637 "Show/Set whether a user does not need a password" },
1639 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1640 "Show/Set whether a user's password does not expire" },
1642 { NULL, NULL, 0, NULL, NULL }
1648 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1649 struct rpc_sh_ctx *ctx)
1651 static struct rpc_sh_cmd cmds[] = {
1653 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1654 "List available users" },
1656 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1657 "List the domain groups a user is member of" },
1659 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1660 "Show info about a user" },
1662 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1663 "Show/Modify a user's fields" },
1665 { NULL, NULL, 0, NULL, NULL }
1671 /****************************************************************************/
1674 * Basic usage function for 'net rpc group'
1675 * @param argc Standard main() style argc.
1676 * @param argv Standard main() style argv. Initial components are already
1680 static int rpc_group_usage(int argc, const char **argv)
1682 return net_help_group(argc, argv);
1686 * Delete group on a remote RPC server
1688 * All parameters are provided by the run_rpc_command function, except for
1689 * argc, argv which are passes through.
1691 * @param domain_sid The domain sid acquired from the remote server
1692 * @param cli A cli_state connected to the server.
1693 * @param mem_ctx Talloc context, destoyed on completion of the function.
1694 * @param argc Standard main() style argc
1695 * @param argv Standard main() style argv. Initial components are already
1698 * @return Normal NTSTATUS return.
1701 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1702 const char *domain_name,
1703 struct cli_state *cli,
1704 struct rpc_pipe_client *pipe_hnd,
1705 TALLOC_CTX *mem_ctx,
1709 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1710 BOOL group_is_primary = False;
1711 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1713 uint32 *group_rids, num_rids, *name_types, num_members,
1714 *group_attrs, group_rid;
1715 uint32 flags = 0x000003e8; /* Unknown */
1718 /* DOM_GID *user_gids; */
1719 SAM_USERINFO_CTR *user_ctr;
1723 d_printf("specify group\n");
1724 rpc_group_usage(argc,argv);
1725 return NT_STATUS_OK; /* ok? */
1728 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1731 if (!NT_STATUS_IS_OK(result)) {
1732 d_fprintf(stderr, "Request samr_connect failed\n");
1736 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1737 MAXIMUM_ALLOWED_ACCESS,
1738 domain_sid, &domain_pol);
1740 if (!NT_STATUS_IS_OK(result)) {
1741 d_fprintf(stderr, "Request open_domain failed\n");
1745 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1747 &num_rids, &group_rids,
1750 if (!NT_STATUS_IS_OK(result)) {
1751 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1755 switch (name_types[0])
1757 case SID_NAME_DOM_GRP:
1758 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1759 MAXIMUM_ALLOWED_ACCESS,
1760 group_rids[0], &group_pol);
1761 if (!NT_STATUS_IS_OK(result)) {
1762 d_fprintf(stderr, "Request open_group failed");
1766 group_rid = group_rids[0];
1768 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1769 &num_members, &group_rids,
1772 if (!NT_STATUS_IS_OK(result)) {
1773 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1778 d_printf("Domain Group %s (rid: %d) has %d members\n",
1779 argv[0],group_rid,num_members);
1782 /* Check if group is anyone's primary group */
1783 for (i = 0; i < num_members; i++)
1785 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1786 MAXIMUM_ALLOWED_ACCESS,
1787 group_rids[i], &user_pol);
1789 if (!NT_STATUS_IS_OK(result)) {
1790 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1794 ZERO_STRUCT(user_ctr);
1796 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1799 if (!NT_STATUS_IS_OK(result)) {
1800 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1804 if (user_ctr->info.id21->group_rid == group_rid) {
1805 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1808 d_printf("Group is primary group of %s\n",temp);
1809 group_is_primary = True;
1812 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1815 if (group_is_primary) {
1816 d_fprintf(stderr, "Unable to delete group because some "
1817 "of it's members have it as primary group\n");
1818 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1822 /* remove all group members */
1823 for (i = 0; i < num_members; i++)
1826 d_printf("Remove group member %d...",group_rids[i]);
1827 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1829 if (NT_STATUS_IS_OK(result)) {
1834 d_printf("failed\n");
1839 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1842 /* removing a local group is easier... */
1843 case SID_NAME_ALIAS:
1844 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1845 MAXIMUM_ALLOWED_ACCESS,
1846 group_rids[0], &group_pol);
1848 if (!NT_STATUS_IS_OK(result)) {
1849 d_fprintf(stderr, "Request open_alias failed\n");
1853 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1856 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1857 argv[0],sid_type_lookup(name_types[0]));
1858 result = NT_STATUS_UNSUCCESSFUL;
1863 if (NT_STATUS_IS_OK(result)) {
1865 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1867 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1868 get_friendly_nt_error_msg(result));
1876 static int rpc_group_delete(int argc, const char **argv)
1878 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1882 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1883 const char *domain_name,
1884 struct cli_state *cli,
1885 struct rpc_pipe_client *pipe_hnd,
1886 TALLOC_CTX *mem_ctx,
1890 POLICY_HND connect_pol, domain_pol, group_pol;
1891 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1892 GROUP_INFO_CTR group_info;
1895 d_printf("Group name must be specified\n");
1896 rpc_group_usage(argc, argv);
1897 return NT_STATUS_OK;
1900 /* Get sam policy handle */
1902 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1904 if (!NT_STATUS_IS_OK(result)) goto done;
1906 /* Get domain policy handle */
1908 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1909 MAXIMUM_ALLOWED_ACCESS,
1910 domain_sid, &domain_pol);
1911 if (!NT_STATUS_IS_OK(result)) goto done;
1913 /* Create the group */
1915 result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1916 argv[0], MAXIMUM_ALLOWED_ACCESS,
1918 if (!NT_STATUS_IS_OK(result)) goto done;
1920 if (strlen(opt_comment) == 0) goto done;
1922 /* We've got a comment to set */
1924 group_info.switch_value1 = 4;
1925 init_samr_group_info4(&group_info.group.info4, opt_comment);
1927 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1928 if (!NT_STATUS_IS_OK(result)) goto done;
1931 if (NT_STATUS_IS_OK(result))
1932 DEBUG(5, ("add group succeeded\n"));
1934 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1939 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1940 const char *domain_name,
1941 struct cli_state *cli,
1942 struct rpc_pipe_client *pipe_hnd,
1943 TALLOC_CTX *mem_ctx,
1947 POLICY_HND connect_pol, domain_pol, alias_pol;
1948 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1949 ALIAS_INFO_CTR alias_info;
1952 d_printf("Alias name must be specified\n");
1953 rpc_group_usage(argc, argv);
1954 return NT_STATUS_OK;
1957 /* Get sam policy handle */
1959 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1961 if (!NT_STATUS_IS_OK(result)) goto done;
1963 /* Get domain policy handle */
1965 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1966 MAXIMUM_ALLOWED_ACCESS,
1967 domain_sid, &domain_pol);
1968 if (!NT_STATUS_IS_OK(result)) goto done;
1970 /* Create the group */
1972 result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1973 argv[0], &alias_pol);
1974 if (!NT_STATUS_IS_OK(result)) goto done;
1976 if (strlen(opt_comment) == 0) goto done;
1978 /* We've got a comment to set */
1980 alias_info.level = 3;
1981 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1983 result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1984 if (!NT_STATUS_IS_OK(result)) goto done;
1987 if (NT_STATUS_IS_OK(result))
1988 DEBUG(5, ("add alias succeeded\n"));
1990 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1995 static int rpc_group_add(int argc, const char **argv)
1998 return run_rpc_command(NULL, PI_SAMR, 0,
1999 rpc_alias_add_internals,
2002 return run_rpc_command(NULL, PI_SAMR, 0,
2003 rpc_group_add_internals,
2007 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2008 TALLOC_CTX *mem_ctx,
2011 enum SID_NAME_USE *type)
2013 DOM_SID *sids = NULL;
2014 uint32 *types = NULL;
2015 struct rpc_pipe_client *pipe_hnd;
2017 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2019 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2024 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2025 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2027 if (!NT_STATUS_IS_OK(result)) {
2031 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2032 &name, NULL, &sids, &types);
2034 if (NT_STATUS_IS_OK(result)) {
2035 sid_copy(sid, &sids[0]);
2039 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
2043 cli_rpc_pipe_close(pipe_hnd);
2046 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2048 /* Try as S-1-5-whatever */
2052 if (string_to_sid(&tmp_sid, name)) {
2053 sid_copy(sid, &tmp_sid);
2054 *type = SID_NAME_UNKNOWN;
2055 result = NT_STATUS_OK;
2062 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2063 TALLOC_CTX *mem_ctx,
2064 const DOM_SID *group_sid,
2067 POLICY_HND connect_pol, domain_pol;
2070 POLICY_HND group_pol;
2073 uint32 *rids = NULL;
2074 uint32 *rid_types = NULL;
2078 sid_copy(&sid, group_sid);
2080 if (!sid_split_rid(&sid, &group_rid)) {
2081 return NT_STATUS_UNSUCCESSFUL;
2084 /* Get sam policy handle */
2085 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2087 if (!NT_STATUS_IS_OK(result)) {
2091 /* Get domain policy handle */
2092 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2093 MAXIMUM_ALLOWED_ACCESS,
2095 if (!NT_STATUS_IS_OK(result)) {
2099 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2101 &num_rids, &rids, &rid_types);
2103 if (!NT_STATUS_IS_OK(result)) {
2104 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2108 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2109 MAXIMUM_ALLOWED_ACCESS,
2110 group_rid, &group_pol);
2112 if (!NT_STATUS_IS_OK(result)) {
2116 result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2119 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2123 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2124 TALLOC_CTX *mem_ctx,
2125 const DOM_SID *alias_sid,
2128 POLICY_HND connect_pol, domain_pol;
2131 POLICY_HND alias_pol;
2134 enum SID_NAME_USE member_type;
2138 sid_copy(&sid, alias_sid);
2140 if (!sid_split_rid(&sid, &alias_rid)) {
2141 return NT_STATUS_UNSUCCESSFUL;
2144 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2145 &member_sid, &member_type);
2147 if (!NT_STATUS_IS_OK(result)) {
2148 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2152 /* Get sam policy handle */
2153 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2155 if (!NT_STATUS_IS_OK(result)) {
2159 /* Get domain policy handle */
2160 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2161 MAXIMUM_ALLOWED_ACCESS,
2163 if (!NT_STATUS_IS_OK(result)) {
2167 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2168 MAXIMUM_ALLOWED_ACCESS,
2169 alias_rid, &alias_pol);
2171 if (!NT_STATUS_IS_OK(result)) {
2175 result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2177 if (!NT_STATUS_IS_OK(result)) {
2182 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2186 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2187 const char *domain_name,
2188 struct cli_state *cli,
2189 struct rpc_pipe_client *pipe_hnd,
2190 TALLOC_CTX *mem_ctx,
2195 enum SID_NAME_USE group_type;
2198 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2199 return NT_STATUS_UNSUCCESSFUL;
2202 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2203 &group_sid, &group_type))) {
2204 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2205 return NT_STATUS_UNSUCCESSFUL;
2208 if (group_type == SID_NAME_DOM_GRP) {
2209 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2210 &group_sid, argv[1]);
2212 if (!NT_STATUS_IS_OK(result)) {
2213 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2214 argv[1], argv[0], nt_errstr(result));
2219 if (group_type == SID_NAME_ALIAS) {
2220 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2221 &group_sid, argv[1]);
2223 if (!NT_STATUS_IS_OK(result)) {
2224 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2225 argv[1], argv[0], nt_errstr(result));
2230 d_fprintf(stderr, "Can only add members to global or local groups "
2231 "which %s is not\n", argv[0]);
2233 return NT_STATUS_UNSUCCESSFUL;
2236 static int rpc_group_addmem(int argc, const char **argv)
2238 return run_rpc_command(NULL, PI_SAMR, 0,
2239 rpc_group_addmem_internals,
2243 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2244 TALLOC_CTX *mem_ctx,
2245 const DOM_SID *group_sid,
2248 POLICY_HND connect_pol, domain_pol;
2251 POLICY_HND group_pol;
2254 uint32 *rids = NULL;
2255 uint32 *rid_types = NULL;
2259 sid_copy(&sid, group_sid);
2261 if (!sid_split_rid(&sid, &group_rid))
2262 return NT_STATUS_UNSUCCESSFUL;
2264 /* Get sam policy handle */
2265 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2267 if (!NT_STATUS_IS_OK(result))
2270 /* Get domain policy handle */
2271 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2272 MAXIMUM_ALLOWED_ACCESS,
2274 if (!NT_STATUS_IS_OK(result))
2277 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2279 &num_rids, &rids, &rid_types);
2281 if (!NT_STATUS_IS_OK(result)) {
2282 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2286 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2287 MAXIMUM_ALLOWED_ACCESS,
2288 group_rid, &group_pol);
2290 if (!NT_STATUS_IS_OK(result))
2293 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2296 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2300 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2301 TALLOC_CTX *mem_ctx,
2302 const DOM_SID *alias_sid,
2305 POLICY_HND connect_pol, domain_pol;
2308 POLICY_HND alias_pol;
2311 enum SID_NAME_USE member_type;
2315 sid_copy(&sid, alias_sid);
2317 if (!sid_split_rid(&sid, &alias_rid))
2318 return NT_STATUS_UNSUCCESSFUL;
2320 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2321 &member_sid, &member_type);
2323 if (!NT_STATUS_IS_OK(result)) {
2324 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2328 /* Get sam policy handle */
2329 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2331 if (!NT_STATUS_IS_OK(result)) {
2335 /* Get domain policy handle */
2336 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2337 MAXIMUM_ALLOWED_ACCESS,
2339 if (!NT_STATUS_IS_OK(result)) {
2343 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2344 MAXIMUM_ALLOWED_ACCESS,
2345 alias_rid, &alias_pol);
2347 if (!NT_STATUS_IS_OK(result))
2350 result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2352 if (!NT_STATUS_IS_OK(result))
2356 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2360 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2361 const char *domain_name,
2362 struct cli_state *cli,
2363 struct rpc_pipe_client *pipe_hnd,
2364 TALLOC_CTX *mem_ctx,
2369 enum SID_NAME_USE group_type;
2372 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2373 return NT_STATUS_UNSUCCESSFUL;
2376 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2377 &group_sid, &group_type))) {
2378 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2379 return NT_STATUS_UNSUCCESSFUL;
2382 if (group_type == SID_NAME_DOM_GRP) {
2383 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2384 &group_sid, argv[1]);
2386 if (!NT_STATUS_IS_OK(result)) {
2387 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2388 argv[1], argv[0], nt_errstr(result));
2393 if (group_type == SID_NAME_ALIAS) {
2394 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2395 &group_sid, argv[1]);
2397 if (!NT_STATUS_IS_OK(result)) {
2398 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2399 argv[1], argv[0], nt_errstr(result));
2404 d_fprintf(stderr, "Can only delete members from global or local groups "
2405 "which %s is not\n", argv[0]);
2407 return NT_STATUS_UNSUCCESSFUL;
2410 static int rpc_group_delmem(int argc, const char **argv)
2412 return run_rpc_command(NULL, PI_SAMR, 0,
2413 rpc_group_delmem_internals,
2418 * List groups on a remote RPC server
2420 * All parameters are provided by the run_rpc_command function, except for
2421 * argc, argv which are passes through.
2423 * @param domain_sid The domain sid acquired from the remote server
2424 * @param cli A cli_state connected to the server.
2425 * @param mem_ctx Talloc context, destoyed on completion of the function.
2426 * @param argc Standard main() style argc
2427 * @param argv Standard main() style argv. Initial components are already
2430 * @return Normal NTSTATUS return.
2433 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2434 const char *domain_name,
2435 struct cli_state *cli,
2436 struct rpc_pipe_client *pipe_hnd,
2437 TALLOC_CTX *mem_ctx,
2441 POLICY_HND connect_pol, domain_pol;
2442 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2443 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2444 struct acct_info *groups;
2445 BOOL global = False;
2447 BOOL builtin = False;
2455 for (i=0; i<argc; i++) {
2456 if (strequal(argv[i], "global"))
2459 if (strequal(argv[i], "local"))
2462 if (strequal(argv[i], "builtin"))
2466 /* Get sam policy handle */
2468 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2470 if (!NT_STATUS_IS_OK(result)) {
2474 /* Get domain policy handle */
2476 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2477 MAXIMUM_ALLOWED_ACCESS,
2478 domain_sid, &domain_pol);
2479 if (!NT_STATUS_IS_OK(result)) {
2483 /* Query domain groups */
2484 if (opt_long_list_entries)
2485 d_printf("\nGroup name Comment"\
2486 "\n-----------------------------\n");
2488 SAM_DISPINFO_CTR ctr;
2489 SAM_DISPINFO_3 info3;
2494 ctr.sam.info3 = &info3;
2498 get_query_dispinfo_params(
2499 loop_count, &max_entries, &max_size);
2501 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2502 &start_idx, 3, &num_entries,
2503 max_entries, max_size, &ctr);
2505 if (!NT_STATUS_IS_OK(result) &&
2506 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2509 for (i = 0; i < num_entries; i++) {
2511 fstring group, desc;
2513 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2514 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2516 if (opt_long_list_entries)
2517 printf("%-21.21s %-50.50s\n",
2520 printf("%s\n", group);
2522 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2523 /* query domain aliases */
2528 /* The max_size field in cli_samr_enum_als_groups is more like
2529 * an account_control field with indiviual bits what to
2530 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2531 * everything. I'm too lazy (sorry) to get this through to
2532 * rpc_parse/ etc. Volker */
2534 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2536 &groups, &num_entries);
2538 if (!NT_STATUS_IS_OK(result) &&
2539 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2542 for (i = 0; i < num_entries; i++) {
2544 char *description = NULL;
2546 if (opt_long_list_entries) {
2548 POLICY_HND alias_pol;
2551 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2556 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2559 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2561 description = unistr2_tdup(mem_ctx,
2562 ctr.alias.info3.description.string);
2566 if (description != NULL) {
2567 printf("%-21.21s %-50.50s\n",
2568 groups[i].acct_name,
2571 printf("%s\n", groups[i].acct_name);
2574 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2575 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2576 /* Get builtin policy handle */
2578 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2579 MAXIMUM_ALLOWED_ACCESS,
2580 &global_sid_Builtin, &domain_pol);
2581 if (!NT_STATUS_IS_OK(result)) {
2584 /* query builtin aliases */
2587 if (!builtin) break;
2589 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2590 &start_idx, max_entries,
2591 &groups, &num_entries);
2593 if (!NT_STATUS_IS_OK(result) &&
2594 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2597 for (i = 0; i < num_entries; i++) {
2599 char *description = NULL;
2601 if (opt_long_list_entries) {
2603 POLICY_HND alias_pol;
2606 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2611 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2614 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2616 description = unistr2_tdup(mem_ctx,
2617 ctr.alias.info3.description.string);
2621 if (description != NULL) {
2622 printf("%-21.21s %-50.50s\n",
2623 groups[i].acct_name,
2626 printf("%s\n", groups[i].acct_name);
2629 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2635 static int rpc_group_list(int argc, const char **argv)
2637 return run_rpc_command(NULL, PI_SAMR, 0,
2638 rpc_group_list_internals,
2642 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2643 TALLOC_CTX *mem_ctx,
2644 const char *domain_name,
2645 const DOM_SID *domain_sid,
2646 POLICY_HND *domain_pol,
2650 POLICY_HND group_pol;
2651 uint32 num_members, *group_rids, *group_attrs;
2658 sid_to_string(sid_str, domain_sid);
2660 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2661 MAXIMUM_ALLOWED_ACCESS,
2664 if (!NT_STATUS_IS_OK(result))
2667 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2668 &num_members, &group_rids,
2671 if (!NT_STATUS_IS_OK(result))
2674 while (num_members > 0) {
2675 int this_time = 512;
2677 if (num_members < this_time)
2678 this_time = num_members;
2680 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2681 this_time, group_rids,
2682 &num_names, &names, &name_types);
2684 if (!NT_STATUS_IS_OK(result))
2687 /* We only have users as members, but make the output
2688 the same as the output of alias members */
2690 for (i = 0; i < this_time; i++) {
2692 if (opt_long_list_entries) {
2693 printf("%s-%d %s\\%s %d\n", sid_str,
2694 group_rids[i], domain_name, names[i],
2697 printf("%s\\%s\n", domain_name, names[i]);
2701 num_members -= this_time;
2705 return NT_STATUS_OK;
2708 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2709 TALLOC_CTX *mem_ctx,
2710 POLICY_HND *domain_pol,
2714 struct rpc_pipe_client *lsa_pipe;
2715 POLICY_HND alias_pol, lsa_pol;
2717 DOM_SID *alias_sids;
2723 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2724 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2726 if (!NT_STATUS_IS_OK(result))
2729 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2730 &num_members, &alias_sids);
2732 if (!NT_STATUS_IS_OK(result)) {
2733 d_fprintf(stderr, "Couldn't list alias members\n");
2737 if (num_members == 0) {
2738 return NT_STATUS_OK;
2741 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2743 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2744 nt_errstr(result) );
2748 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2749 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2751 if (!NT_STATUS_IS_OK(result)) {
2752 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2753 cli_rpc_pipe_close(lsa_pipe);
2757 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2759 &domains, &names, &types);
2761 if (!NT_STATUS_IS_OK(result) &&
2762 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2763 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2764 cli_rpc_pipe_close(lsa_pipe);
2768 for (i = 0; i < num_members; i++) {
2770 sid_to_string(sid_str, &alias_sids[i]);
2772 if (opt_long_list_entries) {
2773 printf("%s %s\\%s %d\n", sid_str,
2774 domains[i] ? domains[i] : "*unknown*",
2775 names[i] ? names[i] : "*unknown*", types[i]);
2778 printf("%s\\%s\n", domains[i], names[i]);
2780 printf("%s\n", sid_str);
2784 cli_rpc_pipe_close(lsa_pipe);
2785 return NT_STATUS_OK;
2788 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2789 const char *domain_name,
2790 struct cli_state *cli,
2791 struct rpc_pipe_client *pipe_hnd,
2792 TALLOC_CTX *mem_ctx,
2797 POLICY_HND connect_pol, domain_pol;
2798 uint32 num_rids, *rids, *rid_types;
2800 /* Get sam policy handle */
2802 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2805 if (!NT_STATUS_IS_OK(result))
2808 /* Get domain policy handle */
2810 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2811 MAXIMUM_ALLOWED_ACCESS,
2812 domain_sid, &domain_pol);
2814 if (!NT_STATUS_IS_OK(result))
2817 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2818 1, argv, &num_rids, &rids, &rid_types);
2820 if (!NT_STATUS_IS_OK(result)) {
2822 /* Ok, did not find it in the global sam, try with builtin */
2824 DOM_SID sid_Builtin;
2826 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2828 string_to_sid(&sid_Builtin, "S-1-5-32");
2830 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2831 MAXIMUM_ALLOWED_ACCESS,
2832 &sid_Builtin, &domain_pol);
2834 if (!NT_STATUS_IS_OK(result)) {
2835 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2839 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2843 if (!NT_STATUS_IS_OK(result)) {
2844 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2849 if (num_rids != 1) {
2850 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2854 if (rid_types[0] == SID_NAME_DOM_GRP) {
2855 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2856 domain_sid, &domain_pol,
2860 if (rid_types[0] == SID_NAME_ALIAS) {
2861 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2865 return NT_STATUS_NO_SUCH_GROUP;
2868 static int rpc_group_members(int argc, const char **argv)
2871 return rpc_group_usage(argc, argv);
2874 return run_rpc_command(NULL, PI_SAMR, 0,
2875 rpc_group_members_internals,
2879 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2880 const char *domain_name,
2881 struct cli_state *cli,
2882 struct rpc_pipe_client *pipe_hnd,
2883 TALLOC_CTX *mem_ctx,
2888 POLICY_HND connect_pol, domain_pol, group_pol;
2889 uint32 num_rids, *rids, *rid_types;
2893 d_printf("Usage: 'net rpc group rename group newname'\n");
2894 return NT_STATUS_UNSUCCESSFUL;
2897 /* Get sam policy handle */
2899 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2902 if (!NT_STATUS_IS_OK(result))
2905 /* Get domain policy handle */
2907 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2908 MAXIMUM_ALLOWED_ACCESS,
2909 domain_sid, &domain_pol);
2911 if (!NT_STATUS_IS_OK(result))
2914 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2915 1, argv, &num_rids, &rids, &rid_types);
2917 if (num_rids != 1) {
2918 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2922 if (rid_types[0] != SID_NAME_DOM_GRP) {
2923 d_fprintf(stderr, "Can only rename domain groups\n");
2924 return NT_STATUS_UNSUCCESSFUL;
2927 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2928 MAXIMUM_ALLOWED_ACCESS,
2929 rids[0], &group_pol);
2931 if (!NT_STATUS_IS_OK(result))
2936 ctr.switch_value1 = 2;
2937 init_samr_group_info2(&ctr.group.info2, argv[1]);
2939 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2941 if (!NT_STATUS_IS_OK(result))
2944 return NT_STATUS_NO_SUCH_GROUP;
2947 static int rpc_group_rename(int argc, const char **argv)
2950 return rpc_group_usage(argc, argv);
2953 return run_rpc_command(NULL, PI_SAMR, 0,
2954 rpc_group_rename_internals,
2959 * 'net rpc group' entrypoint.
2960 * @param argc Standard main() style argc
2961 * @param argc Standard main() style argv. Initial components are already
2965 int net_rpc_group(int argc, const char **argv)
2967 struct functable func[] = {
2968 {"add", rpc_group_add},
2969 {"delete", rpc_group_delete},
2970 {"addmem", rpc_group_addmem},
2971 {"delmem", rpc_group_delmem},
2972 {"list", rpc_group_list},
2973 {"members", rpc_group_members},
2974 {"rename", rpc_group_rename},
2979 return run_rpc_command(NULL, PI_SAMR, 0,
2980 rpc_group_list_internals,
2984 return net_run_function(argc, argv, func, rpc_group_usage);
2987 /****************************************************************************/
2989 static int rpc_share_usage(int argc, const char **argv)
2991 return net_help_share(argc, argv);
2995 * Add a share on a remote RPC server
2997 * All parameters are provided by the run_rpc_command function, except for
2998 * argc, argv which are passes through.
3000 * @param domain_sid The domain sid acquired from the remote server
3001 * @param cli A cli_state connected to the server.
3002 * @param mem_ctx Talloc context, destoyed on completion of the function.
3003 * @param argc Standard main() style argc
3004 * @param argv Standard main() style argv. Initial components are already
3007 * @return Normal NTSTATUS return.
3009 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3010 const char *domain_name,
3011 struct cli_state *cli,
3012 struct rpc_pipe_client *pipe_hnd,
3013 TALLOC_CTX *mem_ctx,int argc,
3019 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3020 uint32 num_users=0, perms=0;
3021 char *password=NULL; /* don't allow a share password */
3024 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3025 return NT_STATUS_NO_MEMORY;
3028 path = strchr(sharename, '=');
3030 return NT_STATUS_UNSUCCESSFUL;
3033 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
3034 opt_comment, perms, opt_maxusers,
3035 num_users, path, password,
3037 return werror_to_ntstatus(result);
3040 static int rpc_share_add(int argc, const char **argv)
3042 if ((argc < 1) || !strchr(argv[0], '=')) {
3043 DEBUG(1,("Sharename or path not specified on add\n"));
3044 return rpc_share_usage(argc, argv);
3046 return run_rpc_command(NULL, PI_SRVSVC, 0,
3047 rpc_share_add_internals,
3052 * Delete a share on a remote RPC server
3054 * All parameters are provided by the run_rpc_command function, except for
3055 * argc, argv which are passes through.
3057 * @param domain_sid The domain sid acquired from the remote server
3058 * @param cli A cli_state connected to the server.
3059 * @param mem_ctx Talloc context, destoyed on completion of the function.
3060 * @param argc Standard main() style argc
3061 * @param argv Standard main() style argv. Initial components are already
3064 * @return Normal NTSTATUS return.
3066 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3067 const char *domain_name,
3068 struct cli_state *cli,
3069 struct rpc_pipe_client *pipe_hnd,
3070 TALLOC_CTX *mem_ctx,
3076 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
3077 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3081 * Delete a share on a remote RPC server
3083 * @param domain_sid The domain sid acquired from the remote server
3084 * @param argc Standard main() style argc
3085 * @param argv Standard main() style argv. Initial components are already
3088 * @return A shell status integer (0 for success)
3090 static int rpc_share_delete(int argc, const char **argv)
3093 DEBUG(1,("Sharename not specified on delete\n"));
3094 return rpc_share_usage(argc, argv);
3096 return run_rpc_command(NULL, PI_SRVSVC, 0,
3097 rpc_share_del_internals,
3102 * Formatted print of share info
3104 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3107 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3109 fstring netname = "", remark = "";
3111 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3112 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3114 if (opt_long_list_entries) {
3115 d_printf("%-12s %-8.8s %-50s\n",
3116 netname, share_type[info1->info_1.type], remark);
3118 d_printf("%s\n", netname);
3123 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3124 TALLOC_CTX *mem_ctx,
3128 SRV_SHARE_INFO_CTR *ctr)
3131 SRV_SHARE_INFO info;
3133 /* no specific share requested, enumerate all */
3137 uint32 preferred_len = 0xffffffff;
3139 init_enum_hnd(&hnd, 0);
3141 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3142 preferred_len, &hnd);
3145 /* request just one share */
3146 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3148 if (!W_ERROR_IS_OK(result))
3154 ctr->info_level = ctr->switch_value = level;
3155 ctr->ptr_share_info = ctr->ptr_entries = 1;
3156 ctr->num_entries = ctr->num_entries2 = 1;
3162 SRV_SHARE_INFO_1 *info1;
3164 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3165 if (ctr->share.info1 == NULL) {
3166 result = WERR_NOMEM;
3169 info1 = ctr->share.info1;
3171 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3173 /* Copy pointer crap */
3175 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3177 /* Duplicate strings */
3179 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3181 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3183 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3185 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3190 SRV_SHARE_INFO_2 *info2;
3192 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3193 if (ctr->share.info2 == NULL) {
3194 result = WERR_NOMEM;
3197 info2 = ctr->share.info2;
3199 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3201 /* Copy pointer crap */
3203 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3205 /* Duplicate strings */
3207 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3209 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3211 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3213 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3215 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_path);
3217 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3219 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3221 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3226 SRV_SHARE_INFO_502 *info502;
3228 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3229 if (ctr->share.info502 == NULL) {
3230 result = WERR_NOMEM;
3233 info502 = ctr->share.info502;
3235 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3237 /* Copy pointer crap */
3239 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3241 /* Duplicate strings */
3243 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3245 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3247 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3249 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3251 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_path);
3253 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3255 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3257 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3259 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3270 * List shares on a remote RPC server
3272 * All parameters are provided by the run_rpc_command function, except for
3273 * argc, argv which are passes through.
3275 * @param domain_sid The domain sid acquired from the remote server
3276 * @param cli A cli_state connected to the server.
3277 * @param mem_ctx Talloc context, destoyed on completion of the function.
3278 * @param argc Standard main() style argc
3279 * @param argv Standard main() style argv. Initial components are already
3282 * @return Normal NTSTATUS return.
3285 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3286 const char *domain_name,
3287 struct cli_state *cli,
3288 struct rpc_pipe_client *pipe_hnd,
3289 TALLOC_CTX *mem_ctx,
3293 SRV_SHARE_INFO_CTR ctr;
3295 uint32 i, level = 1;
3297 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3298 if (!W_ERROR_IS_OK(result))
3301 /* Display results */
3303 if (opt_long_list_entries) {
3305 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3306 "\nShare name Type Description\n"\
3307 "---------- ---- -----------\n");
3309 for (i = 0; i < ctr.num_entries; i++)
3310 display_share_info_1(&ctr.share.info1[i]);
3312 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3316 * 'net rpc share list' entrypoint.
3317 * @param argc Standard main() style argc
3318 * @param argv Standard main() style argv. Initial components are already
3321 static int rpc_share_list(int argc, const char **argv)
3323 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3326 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3328 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3329 d_printf("skipping [%s]: not a file share.\n", netname);
3339 static BOOL check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3341 /* only support disk shares */
3342 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3343 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3347 /* skip builtin shares */
3348 /* FIXME: should print$ be added too ? */
3349 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3350 strequal(netname,"global"))
3353 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3354 printf("excluding [%s]\n", netname);
3358 return check_share_availability(cli, netname);
3362 * Migrate shares from a remote RPC server to the local RPC srever
3364 * All parameters are provided by the run_rpc_command function, except for
3365 * argc, argv which are passes through.
3367 * @param domain_sid The domain sid acquired from the remote server
3368 * @param cli A cli_state connected to the server.
3369 * @param mem_ctx Talloc context, destoyed on completion of the function.
3370 * @param argc Standard main() style argc
3371 * @param argv Standard main() style argv. Initial components are already
3374 * @return Normal NTSTATUS return.
3377 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3378 const char *domain_name,
3379 struct cli_state *cli,
3380 struct rpc_pipe_client *pipe_hnd,
3381 TALLOC_CTX *mem_ctx,
3386 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3387 SRV_SHARE_INFO_CTR ctr_src;
3388 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3389 char *password = NULL; /* don't allow a share password */
3391 struct rpc_pipe_client *srvsvc_pipe = NULL;
3392 struct cli_state *cli_dst = NULL;
3393 uint32 level = 502; /* includes secdesc */
3395 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3396 if (!W_ERROR_IS_OK(result))
3399 /* connect destination PI_SRVSVC */
3400 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3401 if (!NT_STATUS_IS_OK(nt_status))
3405 for (i = 0; i < ctr_src.num_entries; i++) {
3407 fstring netname = "", remark = "", path = "";
3408 /* reset error-code */
3409 nt_status = NT_STATUS_UNSUCCESSFUL;
3411 rpcstr_pull_unistr2_fstring(
3412 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3413 rpcstr_pull_unistr2_fstring(
3414 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3415 rpcstr_pull_unistr2_fstring(
3416 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3418 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3421 /* finally add the share on the dst server */
3423 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3424 netname, path, remark);
3426 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3427 ctr_src.share.info502[i].info_502.perms,
3428 ctr_src.share.info502[i].info_502.max_uses,
3429 ctr_src.share.info502[i].info_502.num_uses,
3430 path, password, level,
3433 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3434 printf(" [%s] does already exist\n", netname);
3438 if (!W_ERROR_IS_OK(result)) {
3439 printf("cannot add share: %s\n", dos_errstr(result));
3445 nt_status = NT_STATUS_OK;
3449 cli_shutdown(cli_dst);
3457 * Migrate shares from a rpc-server to another
3459 * @param argc Standard main() style argc
3460 * @param argv Standard main() style argv. Initial components are already
3463 * @return A shell status integer (0 for success)
3465 static int rpc_share_migrate_shares(int argc, const char **argv)
3469 printf("no server to migrate\n");
3473 return run_rpc_command(NULL, PI_SRVSVC, 0,
3474 rpc_share_migrate_shares_internals,
3481 * @param f file_info
3482 * @param mask current search mask
3483 * @param state arg-pointer
3486 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3488 static NTSTATUS nt_status;
3489 static struct copy_clistate *local_state;
3490 static fstring filename, new_mask;
3494 local_state = (struct copy_clistate *)state;
3495 nt_status = NT_STATUS_UNSUCCESSFUL;
3497 if (strequal(f->name, ".") || strequal(f->name, ".."))
3500 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3503 if (f->mode & aDIR) {
3505 DEBUG(3,("got dir: %s\n", f->name));
3507 fstrcpy(dir, local_state->cwd);
3509 fstrcat(dir, f->name);
3511 switch (net_mode_share)
3513 case NET_MODE_SHARE_MIGRATE:
3514 /* create that directory */
3515 nt_status = net_copy_file(local_state->mem_ctx,
3516 local_state->cli_share_src,
3517 local_state->cli_share_dst,
3519 opt_acls? True : False,
3520 opt_attrs? True : False,
3521 opt_timestamps? True : False,
3525 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3529 if (!NT_STATUS_IS_OK(nt_status))
3530 printf("could not handle dir %s: %s\n",
3531 dir, nt_errstr(nt_status));
3533 /* search below that directory */
3534 fstrcpy(new_mask, dir);
3535 fstrcat(new_mask, "\\*");
3537 old_dir = local_state->cwd;
3538 local_state->cwd = dir;
3539 if (!sync_files(local_state, new_mask))
3540 printf("could not handle files\n");
3541 local_state->cwd = old_dir;
3548 fstrcpy(filename, local_state->cwd);
3549 fstrcat(filename, "\\");
3550 fstrcat(filename, f->name);
3552 DEBUG(3,("got file: %s\n", filename));
3554 switch (net_mode_share)
3556 case NET_MODE_SHARE_MIGRATE:
3557 nt_status = net_copy_file(local_state->mem_ctx,
3558 local_state->cli_share_src,
3559 local_state->cli_share_dst,
3561 opt_acls? True : False,
3562 opt_attrs? True : False,
3563 opt_timestamps? True: False,
3567 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3571 if (!NT_STATUS_IS_OK(nt_status))
3572 printf("could not handle file %s: %s\n",
3573 filename, nt_errstr(nt_status));
3578 * sync files, can be called recursivly to list files
3579 * and then call copy_fn for each file
3581 * @param cp_clistate pointer to the copy_clistate we work with
3582 * @param mask the current search mask
3584 * @return Boolean result
3586 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3589 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3591 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3592 d_fprintf(stderr, "listing %s failed with error: %s\n",
3593 mask, cli_errstr(cp_clistate->cli_share_src));
3602 * Set the top level directory permissions before we do any further copies.
3603 * Should set up ACL inheritance.
3606 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3607 const char *sharename)
3609 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3611 switch (net_mode_share) {
3612 case NET_MODE_SHARE_MIGRATE:
3613 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3614 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3615 cp_clistate->cli_share_src,
3616 cp_clistate->cli_share_dst,
3618 opt_acls? True : False,
3619 opt_attrs? True : False,
3620 opt_timestamps? True: False,
3624 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3628 if (!NT_STATUS_IS_OK(nt_status)) {
3629 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3630 sharename, nt_errstr(nt_status));
3638 * Sync all files inside a remote share to another share (over smb)
3640 * All parameters are provided by the run_rpc_command function, except for
3641 * argc, argv which are passes through.
3643 * @param domain_sid The domain sid acquired from the remote server
3644 * @param cli A cli_state connected to the server.
3645 * @param mem_ctx Talloc context, destoyed on completion of the function.
3646 * @param argc Standard main() style argc
3647 * @param argv Standard main() style argv. Initial components are already
3650 * @return Normal NTSTATUS return.
3653 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3654 const char *domain_name,
3655 struct cli_state *cli,
3656 struct rpc_pipe_client *pipe_hnd,
3657 TALLOC_CTX *mem_ctx,
3662 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3663 SRV_SHARE_INFO_CTR ctr_src;
3666 struct copy_clistate cp_clistate;
3667 BOOL got_src_share = False;
3668 BOOL got_dst_share = False;
3669 pstring mask = "\\*";
3672 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3674 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3676 if (!W_ERROR_IS_OK(result))
3679 for (i = 0; i < ctr_src.num_entries; i++) {
3681 fstring netname = "";
3683 rpcstr_pull_unistr2_fstring(
3684 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3686 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3689 /* one might not want to mirror whole discs :) */
3690 if (strequal(netname, "print$") || netname[1] == '$') {
3691 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3695 switch (net_mode_share)
3697 case NET_MODE_SHARE_MIGRATE:
3701 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3704 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3706 opt_acls ? "including" : "without",
3707 opt_attrs ? "including" : "without",
3708 opt_timestamps ? "(preserving timestamps)" : "");
3710 cp_clistate.mem_ctx = mem_ctx;
3711 cp_clistate.cli_share_src = NULL;
3712 cp_clistate.cli_share_dst = NULL;
3713 cp_clistate.cwd = NULL;
3714 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3716 /* open share source */
3717 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3718 &cli->dest_ip, cli->desthost,
3720 if (!NT_STATUS_IS_OK(nt_status))
3723 got_src_share = True;
3725 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3726 /* open share destination */
3727 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3728 NULL, dst, netname, "A:");
3729 if (!NT_STATUS_IS_OK(nt_status))
3732 got_dst_share = True;
3735 if (!copy_top_level_perms(&cp_clistate, netname)) {
3736 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
3737 nt_status = NT_STATUS_UNSUCCESSFUL;
3741 if (!sync_files(&cp_clistate, mask)) {
3742 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
3743 nt_status = NT_STATUS_UNSUCCESSFUL;
3748 nt_status = NT_STATUS_OK;
3753 cli_shutdown(cp_clistate.cli_share_src);
3756 cli_shutdown(cp_clistate.cli_share_dst);
3762 static int rpc_share_migrate_files(int argc, const char **argv)
3766 printf("no server to migrate\n");
3770 return run_rpc_command(NULL, PI_SRVSVC, 0,
3771 rpc_share_migrate_files_internals,
3776 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3778 * All parameters are provided by the run_rpc_command function, except for
3779 * argc, argv which are passes through.
3781 * @param domain_sid The domain sid acquired from the remote server
3782 * @param cli A cli_state connected to the server.
3783 * @param mem_ctx Talloc context, destoyed on completion of the function.
3784 * @param argc Standard main() style argc
3785 * @param argv Standard main() style argv. Initial components are already
3788 * @return Normal NTSTATUS return.
3791 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3792 const char *domain_name,
3793 struct cli_state *cli,
3794 struct rpc_pipe_client *pipe_hnd,
3795 TALLOC_CTX *mem_ctx,
3800 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3801 SRV_SHARE_INFO_CTR ctr_src;
3802 SRV_SHARE_INFO info;
3804 struct rpc_pipe_client *srvsvc_pipe = NULL;
3805 struct cli_state *cli_dst = NULL;
3806 uint32 level = 502; /* includes secdesc */
3808 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3810 if (!W_ERROR_IS_OK(result))
3813 /* connect destination PI_SRVSVC */
3814 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3815 if (!NT_STATUS_IS_OK(nt_status))
3819 for (i = 0; i < ctr_src.num_entries; i++) {
3821 fstring netname = "", remark = "", path = "";
3822 /* reset error-code */
3823 nt_status = NT_STATUS_UNSUCCESSFUL;
3825 rpcstr_pull_unistr2_fstring(
3826 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3827 rpcstr_pull_unistr2_fstring(
3828 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3829 rpcstr_pull_unistr2_fstring(
3830 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3832 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3835 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3836 netname, path, remark);
3839 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
3844 info.switch_value = level;
3845 info.ptr_share_ctr = 1;
3847 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3848 info.share.info502 = ctr_src.share.info502[i];
3850 /* finally modify the share on the dst server */
3851 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
3853 if (!W_ERROR_IS_OK(result)) {
3854 printf("cannot set share-acl: %s\n", dos_errstr(result));
3860 nt_status = NT_STATUS_OK;
3864 cli_shutdown(cli_dst);
3872 * Migrate share-acls from a rpc-server to another
3874 * @param argc Standard main() style argc
3875 * @param argv Standard main() style argv. Initial components are already
3878 * @return A shell status integer (0 for success)
3880 static int rpc_share_migrate_security(int argc, const char **argv)
3884 printf("no server to migrate\n");
3888 return run_rpc_command(NULL, PI_SRVSVC, 0,
3889 rpc_share_migrate_security_internals,
3894 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3895 * from one server to another
3897 * @param argc Standard main() style argc
3898 * @param argv Standard main() style argv. Initial components are already
3901 * @return A shell status integer (0 for success)
3904 static int rpc_share_migrate_all(int argc, const char **argv)
3909 printf("no server to migrate\n");
3913 /* order is important. we don't want to be locked out by the share-acl
3914 * before copying files - gd */
3916 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3920 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3924 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3929 * 'net rpc share migrate' entrypoint.
3930 * @param argc Standard main() style argc
3931 * @param argv Standard main() style argv. Initial components are already
3934 static int rpc_share_migrate(int argc, const char **argv)
3937 struct functable func[] = {
3938 {"all", rpc_share_migrate_all},
3939 {"files", rpc_share_migrate_files},
3940 {"help", rpc_share_usage},
3941 {"security", rpc_share_migrate_security},
3942 {"shares", rpc_share_migrate_shares},
3946 net_mode_share = NET_MODE_SHARE_MIGRATE;
3948 return net_run_function(argc, argv, func, rpc_share_usage);
3957 static int num_server_aliases;
3958 static struct full_alias *server_aliases;
3961 * Add an alias to the static list.
3963 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3965 if (server_aliases == NULL)
3966 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3968 server_aliases[num_server_aliases] = *alias;
3969 num_server_aliases += 1;
3973 * For a specific domain on the server, fetch all the aliases
3974 * and their members. Add all of them to the server_aliases.
3977 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3978 TALLOC_CTX *mem_ctx,
3979 POLICY_HND *connect_pol,
3980 const DOM_SID *domain_sid)
3982 uint32 start_idx, max_entries, num_entries, i;
3983 struct acct_info *groups;
3985 POLICY_HND domain_pol;
3987 /* Get domain policy handle */
3989 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3990 MAXIMUM_ALLOWED_ACCESS,
3991 domain_sid, &domain_pol);
3992 if (!NT_STATUS_IS_OK(result))
3999 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
4000 &start_idx, max_entries,
4001 &groups, &num_entries);
4003 for (i = 0; i < num_entries; i++) {
4005 POLICY_HND alias_pol;
4006 struct full_alias alias;
4010 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
4011 MAXIMUM_ALLOWED_ACCESS,
4014 if (!NT_STATUS_IS_OK(result))
4017 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
4021 if (!NT_STATUS_IS_OK(result))
4024 result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
4025 if (!NT_STATUS_IS_OK(result))
4028 alias.members = NULL;
4030 if (alias.num_members > 0) {
4031 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4033 for (j = 0; j < alias.num_members; j++)
4034 sid_copy(&alias.members[j],
4038 sid_copy(&alias.sid, domain_sid);
4039 sid_append_rid(&alias.sid, groups[i].rid);
4041 push_alias(mem_ctx, &alias);
4043 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4045 result = NT_STATUS_OK;
4048 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
4054 * Dump server_aliases as names for debugging purposes.
4057 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
4058 const char *domain_name,
4059 struct cli_state *cli,
4060 struct rpc_pipe_client *pipe_hnd,
4061 TALLOC_CTX *mem_ctx,
4069 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
4070 SEC_RIGHTS_MAXIMUM_ALLOWED,
4072 if (!NT_STATUS_IS_OK(result))
4075 for (i=0; i<num_server_aliases; i++) {
4081 struct full_alias *alias = &server_aliases[i];
4083 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4085 &domains, &names, &types);
4086 if (!NT_STATUS_IS_OK(result))
4089 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4091 if (alias->num_members == 0) {
4096 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4099 &domains, &names, &types);
4101 if (!NT_STATUS_IS_OK(result) &&
4102 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4105 for (j=0; j<alias->num_members; j++)
4106 DEBUG(1, ("%s\\%s (%d); ",
4107 domains[j] ? domains[j] : "*unknown*",
4108 names[j] ? names[j] : "*unknown*",types[j]));
4112 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
4114 return NT_STATUS_OK;
4118 * Fetch a list of all server aliases and their members into
4122 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4123 const char *domain_name,
4124 struct cli_state *cli,
4125 struct rpc_pipe_client *pipe_hnd,
4126 TALLOC_CTX *mem_ctx,
4131 POLICY_HND connect_pol;
4133 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4136 if (!NT_STATUS_IS_OK(result))
4139 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4140 &global_sid_Builtin);
4142 if (!NT_STATUS_IS_OK(result))
4145 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4148 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4153 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4155 token->num_sids = 4;
4157 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4159 token->user_sids[0] = *user_sid;
4160 sid_copy(&token->user_sids[1], &global_sid_World);
4161 sid_copy(&token->user_sids[2], &global_sid_Network);
4162 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4165 static void free_user_token(NT_USER_TOKEN *token)
4167 SAFE_FREE(token->user_sids);
4170 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4174 for (i=0; i<token->num_sids; i++) {
4175 if (sid_compare(sid, &token->user_sids[i]) == 0)
4181 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4183 if (is_sid_in_token(token, sid))
4186 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4187 if (!token->user_sids) {
4191 sid_copy(&token->user_sids[token->num_sids], sid);
4193 token->num_sids += 1;
4198 NT_USER_TOKEN token;
4201 static void dump_user_token(struct user_token *token)
4205 d_printf("%s\n", token->name);
4207 for (i=0; i<token->token.num_sids; i++) {
4208 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4212 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4216 for (i=0; i<alias->num_members; i++) {
4217 if (sid_compare(sid, &alias->members[i]) == 0)
4224 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4228 for (i=0; i<num_server_aliases; i++) {
4229 if (is_alias_member(&sid, &server_aliases[i]))
4230 add_sid_to_token(token, &server_aliases[i].sid);
4235 * We got a user token with all the SIDs we can know about without asking the
4236 * server directly. These are the user and domain group sids. All of these can
4237 * be members of aliases. So scan the list of aliases for each of the SIDs and
4238 * add them to the token.
4241 static void collect_alias_memberships(NT_USER_TOKEN *token)
4243 int num_global_sids = token->num_sids;
4246 for (i=0; i<num_global_sids; i++) {
4247 collect_sid_memberships(token, token->user_sids[i]);
4251 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4253 struct winbindd_request request;
4254 struct winbindd_response response;
4262 fstr_sprintf(full_name, "%s%c%s",
4263 domain, *lp_winbind_separator(), user);
4265 /* First let's find out the user sid */
4267 ZERO_STRUCT(request);
4268 ZERO_STRUCT(response);
4270 fstrcpy(request.data.name.dom_name, domain);
4271 fstrcpy(request.data.name.name, user);
4273 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4275 if (result != NSS_STATUS_SUCCESS) {
4276 DEBUG(1, ("winbind could not find %s\n", full_name));
4280 if (response.data.sid.type != SID_NAME_USER) {
4281 DEBUG(1, ("%s is not a user\n", full_name));
4285 string_to_sid(&user_sid, response.data.sid.sid);
4287 init_user_token(token, &user_sid);
4289 /* And now the groups winbind knows about */
4291 ZERO_STRUCT(response);
4293 fstrcpy(request.data.username, full_name);
4295 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4297 if (result != NSS_STATUS_SUCCESS) {
4298 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4302 for (i = 0; i < response.data.num_entries; i++) {
4303 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4306 struct winbindd_request sidrequest;
4307 struct winbindd_response sidresponse;
4309 ZERO_STRUCT(sidrequest);
4310 ZERO_STRUCT(sidresponse);
4312 sidrequest.data.gid = gid;
4314 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4315 &sidrequest, &sidresponse);
4317 if (result != NSS_STATUS_SUCCESS) {
4318 DEBUG(1, ("winbind could not find SID of gid %d\n",
4323 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4325 string_to_sid(&sid, sidresponse.data.sid.sid);
4326 add_sid_to_token(token, &sid);
4329 SAFE_FREE(response.extra_data.data);
4335 * Get a list of all user tokens we want to look at
4338 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4340 struct winbindd_request request;
4341 struct winbindd_response response;
4342 const char *extra_data;
4345 struct user_token *result;
4347 if (lp_winbind_use_default_domain() &&
4348 (opt_target_workgroup == NULL)) {
4349 d_fprintf(stderr, "winbind use default domain = yes set, "
4350 "please specify a workgroup\n");
4354 /* Send request to winbind daemon */
4356 ZERO_STRUCT(request);
4357 ZERO_STRUCT(response);
4359 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4363 /* Look through extra data */
4365 if (!response.extra_data.data)
4368 extra_data = (const char *)response.extra_data.data;
4371 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4375 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4377 if (result == NULL) {
4378 DEBUG(1, ("Could not malloc sid array\n"));
4382 extra_data = (const char *)response.extra_data.data;
4385 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4387 fstring domain, user;
4390 fstrcpy(result[i].name, name);
4392 p = strchr(name, *lp_winbind_separator());
4394 DEBUG(3, ("%s\n", name));
4397 fstrcpy(domain, opt_target_workgroup);
4398 fstrcpy(user, name);
4401 fstrcpy(domain, name);
4406 get_user_sids(domain, user, &(result[i].token));
4410 SAFE_FREE(response.extra_data.data);
4412 *user_tokens = result;
4417 static BOOL get_user_tokens_from_file(FILE *f,
4419 struct user_token **tokens)
4421 struct user_token *token = NULL;
4426 if (fgets(line, sizeof(line)-1, f) == NULL) {
4430 if (line[strlen(line)-1] == '\n')
4431 line[strlen(line)-1] = '\0';
4433 if (line[0] == ' ') {
4437 string_to_sid(&sid, &line[1]);
4439 if (token == NULL) {
4440 DEBUG(0, ("File does not begin with username"));
4444 add_sid_to_token(&token->token, &sid);
4448 /* And a new user... */
4451 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4452 if (*tokens == NULL) {
4453 DEBUG(0, ("Could not realloc tokens\n"));
4457 token = &((*tokens)[*num_tokens-1]);
4459 fstrcpy(token->name, line);
4460 token->token.num_sids = 0;
4461 token->token.user_sids = NULL;
4470 * Show the list of all users that have access to a share
4473 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4474 TALLOC_CTX *mem_ctx,
4475 const char *netname,
4477 struct user_token *tokens)
4480 SEC_DESC *share_sd = NULL;
4481 SEC_DESC *root_sd = NULL;
4482 struct cli_state *cli = pipe_hnd->cli;
4484 SRV_SHARE_INFO info;
4488 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4491 if (!W_ERROR_IS_OK(result)) {
4492 DEBUG(1, ("Coult not query secdesc for share %s\n",
4497 share_sd = info.share.info502.info_502_str.sd;
4498 if (share_sd == NULL) {
4499 DEBUG(1, ("Got no secdesc for share %s\n",
4505 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4509 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4512 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4515 for (i=0; i<num_tokens; i++) {
4519 if (share_sd != NULL) {
4520 if (!se_access_check(share_sd, &tokens[i].token,
4521 1, &acc_granted, &status)) {
4522 DEBUG(1, ("Could not check share_sd for "
4528 if (!NT_STATUS_IS_OK(status))
4532 if (root_sd == NULL) {
4533 d_printf(" %s\n", tokens[i].name);
4537 if (!se_access_check(root_sd, &tokens[i].token,
4538 1, &acc_granted, &status)) {
4539 DEBUG(1, ("Could not check root_sd for user %s\n",
4544 if (!NT_STATUS_IS_OK(status))
4547 d_printf(" %s\n", tokens[i].name);
4551 cli_close(cli, fnum);
4563 static void collect_share(const char *name, uint32 m,
4564 const char *comment, void *state)
4566 struct share_list *share_list = (struct share_list *)state;
4568 if (m != STYPE_DISKTREE)
4571 share_list->num_shares += 1;
4572 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4573 if (!share_list->shares) {
4574 share_list->num_shares = 0;
4577 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4580 static void rpc_share_userlist_usage(void)
4586 * List shares on a remote RPC server, including the security descriptors
4588 * All parameters are provided by the run_rpc_command function, except for
4589 * argc, argv which are passes through.
4591 * @param domain_sid The domain sid acquired from the remote server
4592 * @param cli A cli_state connected to the server.
4593 * @param mem_ctx Talloc context, destoyed on completion of the function.
4594 * @param argc Standard main() style argc
4595 * @param argv Standard main() style argv. Initial components are already
4598 * @return Normal NTSTATUS return.
4601 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4602 const char *domain_name,
4603 struct cli_state *cli,
4604 struct rpc_pipe_client *pipe_hnd,
4605 TALLOC_CTX *mem_ctx,
4615 struct user_token *tokens = NULL;
4618 struct share_list share_list;
4621 rpc_share_userlist_usage();
4622 return NT_STATUS_UNSUCCESSFUL;
4628 f = fopen(argv[0], "r");
4632 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4633 return NT_STATUS_UNSUCCESSFUL;
4636 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4642 DEBUG(0, ("Could not read users from file\n"));
4643 return NT_STATUS_UNSUCCESSFUL;
4646 for (i=0; i<num_tokens; i++)
4647 collect_alias_memberships(&tokens[i].token);
4649 init_enum_hnd(&hnd, 0);
4651 share_list.num_shares = 0;
4652 share_list.shares = NULL;
4654 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4657 DEBUG(0, ("Error returning browse list: %s\n",
4662 for (i = 0; i < share_list.num_shares; i++) {
4663 char *netname = share_list.shares[i];
4665 if (netname[strlen(netname)-1] == '$')
4668 d_printf("%s\n", netname);
4670 show_userlist(pipe_hnd, mem_ctx, netname,
4671 num_tokens, tokens);
4674 for (i=0; i<num_tokens; i++) {
4675 free_user_token(&tokens[i].token);
4678 SAFE_FREE(share_list.shares);
4680 return NT_STATUS_OK;
4683 static int rpc_share_allowedusers(int argc, const char **argv)
4687 result = run_rpc_command(NULL, PI_SAMR, 0,
4688 rpc_aliaslist_internals,
4693 result = run_rpc_command(NULL, PI_LSARPC, 0,
4699 return run_rpc_command(NULL, PI_SRVSVC, 0,
4700 rpc_share_allowedusers_internals,
4704 int net_usersidlist(int argc, const char **argv)
4707 struct user_token *tokens = NULL;
4711 net_usersidlist_usage(argc, argv);
4715 if (!get_user_tokens(&num_tokens, &tokens)) {
4716 DEBUG(0, ("Could not get the user/sid list\n"));
4720 for (i=0; i<num_tokens; i++) {
4721 dump_user_token(&tokens[i]);
4722 free_user_token(&tokens[i].token);
4729 int net_usersidlist_usage(int argc, const char **argv)
4731 d_printf("net usersidlist\n"
4732 "\tprints out a list of all users the running winbind knows\n"
4733 "\tabout, together with all their SIDs. This is used as\n"
4734 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4736 net_common_flags_usage(argc, argv);
4741 * 'net rpc share' entrypoint.
4742 * @param argc Standard main() style argc
4743 * @param argv Standard main() style argv. Initial components are already
4747 int net_rpc_share(int argc, const char **argv)
4749 struct functable func[] = {
4750 {"add", rpc_share_add},
4751 {"delete", rpc_share_delete},
4752 {"allowedusers", rpc_share_allowedusers},
4753 {"migrate", rpc_share_migrate},
4754 {"list", rpc_share_list},
4759 return run_rpc_command(NULL, PI_SRVSVC, 0,
4760 rpc_share_list_internals,
4763 return net_run_function(argc, argv, func, rpc_share_usage);
4766 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4767 struct rpc_sh_ctx *ctx,
4768 struct rpc_pipe_client *pipe_hnd,
4769 int argc, const char **argv)
4771 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4772 ctx->cli, pipe_hnd, mem_ctx,
4776 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4777 struct rpc_sh_ctx *ctx,
4778 struct rpc_pipe_client *pipe_hnd,
4779 int argc, const char **argv)
4783 if ((argc < 2) || (argc > 3)) {
4784 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4786 return NT_STATUS_INVALID_PARAMETER;
4789 result = rpccli_srvsvc_net_share_add(
4790 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
4791 (argc == 3) ? argv[2] : "",
4792 0, 0, 0, argv[1], NULL, 2, NULL);
4794 return werror_to_ntstatus(result);
4797 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4798 struct rpc_sh_ctx *ctx,
4799 struct rpc_pipe_client *pipe_hnd,
4800 int argc, const char **argv)
4805 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4806 return NT_STATUS_INVALID_PARAMETER;
4809 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
4810 return werror_to_ntstatus(result);
4813 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4814 struct rpc_sh_ctx *ctx,
4815 struct rpc_pipe_client *pipe_hnd,
4816 int argc, const char **argv)
4818 SRV_SHARE_INFO info;
4819 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
4823 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4824 return NT_STATUS_INVALID_PARAMETER;
4827 result = rpccli_srvsvc_net_share_get_info(
4828 pipe_hnd, mem_ctx, argv[0], 2, &info);
4829 if (!W_ERROR_IS_OK(result)) {
4833 d_printf("Name: %s\n",
4834 rpcstr_pull_unistr2_talloc(mem_ctx,
4835 &info2->info_2_str.uni_netname));
4836 d_printf("Comment: %s\n",
4837 rpcstr_pull_unistr2_talloc(mem_ctx,
4838 &info2->info_2_str.uni_remark));
4840 d_printf("Path: %s\n",
4841 rpcstr_pull_unistr2_talloc(mem_ctx,
4842 &info2->info_2_str.uni_path));
4843 d_printf("Password: %s\n",
4844 rpcstr_pull_unistr2_talloc(mem_ctx,
4845 &info2->info_2_str.uni_passwd));
4848 return werror_to_ntstatus(result);
4851 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4852 struct rpc_sh_ctx *ctx)
4854 static struct rpc_sh_cmd cmds[] = {
4856 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4857 "List available shares" },
4859 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4862 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4865 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4866 "Get information about a share" },
4868 { NULL, NULL, 0, NULL, NULL }
4874 /****************************************************************************/
4876 static int rpc_file_usage(int argc, const char **argv)
4878 return net_help_file(argc, argv);
4882 * Close a file on a remote RPC server
4884 * All parameters are provided by the run_rpc_command function, except for
4885 * argc, argv which are passes through.
4887 * @param domain_sid The domain sid acquired from the remote server
4888 * @param cli A cli_state connected to the server.
4889 * @param mem_ctx Talloc context, destoyed on completion of the function.
4890 * @param argc Standard main() style argc
4891 * @param argv Standard main() style argv. Initial components are already
4894 * @return Normal NTSTATUS return.
4896 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4897 const char *domain_name,
4898 struct cli_state *cli,
4899 struct rpc_pipe_client *pipe_hnd,
4900 TALLOC_CTX *mem_ctx,
4905 result = rpccli_srvsvc_net_file_close(pipe_hnd, mem_ctx, atoi(argv[0]));
4906 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4910 * Close a file on a remote RPC server
4912 * @param argc Standard main() style argc
4913 * @param argv Standard main() style argv. Initial components are already
4916 * @return A shell status integer (0 for success)
4918 static int rpc_file_close(int argc, const char **argv)
4921 DEBUG(1, ("No fileid given on close\n"));
4922 return(rpc_file_usage(argc, argv));
4925 return run_rpc_command(NULL, PI_SRVSVC, 0,
4926 rpc_file_close_internals,
4931 * Formatted print of open file info
4933 * @param info3 FILE_INFO_3 contents
4934 * @param str3 strings for FILE_INFO_3
4937 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
4939 fstring user = "", path = "";
4941 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
4942 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
4944 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4945 info3->id, user, info3->perms, info3->num_locks, path);
4949 * List open files on a remote RPC server
4951 * All parameters are provided by the run_rpc_command function, except for
4952 * argc, argv which are passes through.
4954 * @param domain_sid The domain sid acquired from the remote server
4955 * @param cli A cli_state connected to the server.
4956 * @param mem_ctx Talloc context, destoyed on completion of the function.
4957 * @param argc Standard main() style argc
4958 * @param argv Standard main() style argv. Initial components are already
4961 * @return Normal NTSTATUS return.
4964 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4965 const char *domain_name,
4966 struct cli_state *cli,
4967 struct rpc_pipe_client *pipe_hnd,
4968 TALLOC_CTX *mem_ctx,
4972 SRV_FILE_INFO_CTR ctr;
4975 uint32 preferred_len = 0xffffffff, i;
4976 const char *username=NULL;
4978 init_enum_hnd(&hnd, 0);
4980 /* if argc > 0, must be user command */
4982 username = smb_xstrdup(argv[0]);
4984 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
4985 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4987 if (!W_ERROR_IS_OK(result))
4990 /* Display results */
4993 "\nEnumerating open files on remote server:\n\n"\
4994 "\nFileId Opened by Perms Locks Path"\
4995 "\n------ --------- ----- ----- ---- \n");
4996 for (i = 0; i < ctr.num_entries; i++)
4997 display_file_info_3(&ctr.file.info3[i].info_3,
4998 &ctr.file.info3[i].info_3_str);
5000 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5004 * List files for a user on a remote RPC server
5006 * @param argc Standard main() style argc
5007 * @param argv Standard main() style argv. Initial components are already
5010 * @return A shell status integer (0 for success)
5013 static int rpc_file_user(int argc, const char **argv)
5016 DEBUG(1, ("No username given\n"));
5017 return(rpc_file_usage(argc, argv));
5020 return run_rpc_command(NULL, PI_SRVSVC, 0,
5021 rpc_file_list_internals,
5026 * 'net rpc file' entrypoint.
5027 * @param argc Standard main() style argc
5028 * @param argv Standard main() style argv. Initial components are already
5032 int net_rpc_file(int argc, const char **argv)
5034 struct functable func[] = {
5035 {"close", rpc_file_close},
5036 {"user", rpc_file_user},
5038 {"info", rpc_file_info},
5044 return run_rpc_command(NULL, PI_SRVSVC, 0,
5045 rpc_file_list_internals,
5048 return net_run_function(argc, argv, func, rpc_file_usage);
5052 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5054 * All parameters are provided by the run_rpc_command function, except for
5055 * argc, argv which are passed through.
5057 * @param domain_sid The domain sid aquired from the remote server
5058 * @param cli A cli_state connected to the server.
5059 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5060 * @param argc Standard main() style argc
5061 * @param argv Standard main() style argv. Initial components are already
5064 * @return Normal NTSTATUS return.
5067 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
5068 const char *domain_name,
5069 struct cli_state *cli,
5070 struct rpc_pipe_client *pipe_hnd,
5071 TALLOC_CTX *mem_ctx,
5075 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5077 result = rpccli_shutdown_abort(pipe_hnd, mem_ctx);
5079 if (NT_STATUS_IS_OK(result)) {
5080 d_printf("\nShutdown successfully aborted\n");
5081 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5083 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5089 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5091 * All parameters are provided by the run_rpc_command function, except for
5092 * argc, argv which are passed through.
5094 * @param domain_sid The domain sid aquired from the remote server
5095 * @param cli A cli_state connected to the server.
5096 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5097 * @param argc Standard main() style argc
5098 * @param argv Standard main() style argv. Initial components are already
5101 * @return Normal NTSTATUS return.
5104 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5105 const char *domain_name,
5106 struct cli_state *cli,
5107 struct rpc_pipe_client *pipe_hnd,
5108 TALLOC_CTX *mem_ctx,
5112 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5114 result = werror_to_ntstatus(rpccli_reg_abort_shutdown(pipe_hnd, mem_ctx));
5116 if (NT_STATUS_IS_OK(result)) {
5117 d_printf("\nShutdown successfully aborted\n");
5118 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5120 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5126 * ABORT the Shut down of a remote RPC server
5128 * @param argc Standard main() style argc
5129 * @param argv Standard main() style argv. Initial components are already
5132 * @return A shell status integer (0 for success)
5135 static int rpc_shutdown_abort(int argc, const char **argv)
5137 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5138 rpc_shutdown_abort_internals,
5144 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5146 return run_rpc_command(NULL, PI_WINREG, 0,
5147 rpc_reg_shutdown_abort_internals,
5152 * Shut down a remote RPC Server via initshutdown pipe
5154 * All parameters are provided by the run_rpc_command function, except for
5155 * argc, argv which are passes through.
5157 * @param domain_sid The domain sid aquired from the remote server
5158 * @param cli A cli_state connected to the server.
5159 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5160 * @param argc Standard main() style argc
5161 * @param argc Standard main() style argv. Initial components are already
5164 * @return Normal NTSTATUS return.
5167 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5168 const char *domain_name,
5169 struct cli_state *cli,
5170 struct rpc_pipe_client *pipe_hnd,
5171 TALLOC_CTX *mem_ctx,
5175 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5176 const char *msg = "This machine will be shutdown shortly";
5177 uint32 timeout = 20;
5183 timeout = opt_timeout;
5186 /* create an entry */
5187 result = rpccli_shutdown_init(pipe_hnd, mem_ctx, msg, timeout, opt_reboot,
5190 if (NT_STATUS_IS_OK(result)) {
5191 d_printf("\nShutdown of remote machine succeeded\n");
5192 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5194 DEBUG(1,("Shutdown of remote machine failed!\n"));
5200 * Shut down a remote RPC Server via winreg pipe
5202 * All parameters are provided by the run_rpc_command function, except for
5203 * argc, argv which are passes through.
5205 * @param domain_sid The domain sid aquired from the remote server
5206 * @param cli A cli_state connected to the server.
5207 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5208 * @param argc Standard main() style argc
5209 * @param argc Standard main() style argv. Initial components are already
5212 * @return Normal NTSTATUS return.
5215 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5216 const char *domain_name,
5217 struct cli_state *cli,
5218 struct rpc_pipe_client *pipe_hnd,
5219 TALLOC_CTX *mem_ctx,
5224 const char *msg = "This machine will be shutdown shortly";
5225 uint32 timeout = 20;
5230 struct poptOption long_options[] = {
5231 {"message", 'm', POPT_ARG_STRING, &msg},
5232 {"timeout", 't', POPT_ARG_INT, &timeout},
5233 {"reboot", 'r', POPT_ARG_NONE, &reboot},
5234 {"force", 'f', POPT_ARG_NONE, &force},
5238 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
5239 POPT_CONTEXT_KEEP_FIRST);
5241 rc = poptGetNextOpt(pc);
5244 /* an error occurred during option processing */
5245 DEBUG(0, ("%s: %s\n",
5246 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
5248 return NT_STATUS_INVALID_PARAMETER;
5255 timeout = opt_timeout;
5258 /* create an entry */
5259 result = rpccli_reg_shutdown(pipe_hnd, mem_ctx, msg, timeout, opt_reboot, opt_force);
5261 if (W_ERROR_IS_OK(result)) {
5262 d_printf("\nShutdown of remote machine succeeded\n");
5264 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5265 if (W_ERROR_EQUAL(result,WERR_MACHINE_LOCKED))
5266 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5268 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(result));
5271 return werror_to_ntstatus(result);
5275 * Shut down a remote RPC server
5277 * @param argc Standard main() style argc
5278 * @param argc Standard main() style argv. Initial components are already
5281 * @return A shell status integer (0 for success)
5284 static int rpc_shutdown(int argc, const char **argv)
5286 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5287 rpc_init_shutdown_internals,
5291 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5292 rc = run_rpc_command(NULL, PI_WINREG, 0,
5293 rpc_reg_shutdown_internals, argc, argv);
5299 /***************************************************************************
5300 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5302 ***************************************************************************/
5305 * Add interdomain trust account to the RPC server.
5306 * All parameters (except for argc and argv) are passed by run_rpc_command
5309 * @param domain_sid The domain sid acquired from the server
5310 * @param cli A cli_state connected to the server.
5311 * @param mem_ctx Talloc context, destoyed on completion of the function.
5312 * @param argc Standard main() style argc
5313 * @param argc Standard main() style argv. Initial components are already
5316 * @return normal NTSTATUS return code
5319 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5320 const char *domain_name,
5321 struct cli_state *cli,
5322 struct rpc_pipe_client *pipe_hnd,
5323 TALLOC_CTX *mem_ctx,
5327 POLICY_HND connect_pol, domain_pol, user_pol;
5328 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5331 uint32 unknown, user_rid;
5334 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5335 return NT_STATUS_INVALID_PARAMETER;
5339 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5342 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5343 return NT_STATUS_NO_MEMORY;
5346 strupper_m(acct_name);
5348 /* Get samr policy handle */
5349 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5351 if (!NT_STATUS_IS_OK(result)) {
5355 /* Get domain policy handle */
5356 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5357 MAXIMUM_ALLOWED_ACCESS,
5358 domain_sid, &domain_pol);
5359 if (!NT_STATUS_IS_OK(result)) {
5363 /* Create trusting domain's account */
5364 acb_info = ACB_NORMAL;
5365 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5366 mimir: yes, most probably it is */
5368 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5369 acct_name, acb_info, unknown,
5370 &user_pol, &user_rid);
5371 if (!NT_STATUS_IS_OK(result)) {
5376 SAM_USERINFO_CTR ctr;
5377 SAM_USER_INFO_23 p23;
5383 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5387 ZERO_STRUCT(notime);
5391 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5392 acb_info = ACB_DOMTRUST;
5394 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5395 ¬ime, ¬ime, ¬ime,
5396 nostr, nostr, nostr, nostr, nostr,
5397 nostr, nostr, nostr, nostr, nostr,
5398 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5399 0, 0, (char *)pwbuf);
5400 ctr.switch_value = 23;
5401 ctr.info.id23 = &p23;
5402 p23.passmustchange = 0;
5404 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5405 &cli->user_session_key, &ctr);
5407 if (!NT_STATUS_IS_OK(result)) {
5408 DEBUG(0,("Could not set trust account password: %s\n",
5409 nt_errstr(result)));
5415 SAFE_FREE(acct_name);
5420 * Create interdomain trust account for a remote domain.
5422 * @param argc standard argc
5423 * @param argv standard argv without initial components
5425 * @return Integer status (0 means success)
5428 static int rpc_trustdom_add(int argc, const char **argv)
5431 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5434 d_printf("Usage: net rpc trustdom add <domain>\n");
5441 * Remove interdomain trust account from the RPC server.
5442 * All parameters (except for argc and argv) are passed by run_rpc_command
5445 * @param domain_sid The domain sid acquired from the server
5446 * @param cli A cli_state connected to the server.
5447 * @param mem_ctx Talloc context, destoyed on completion of the function.
5448 * @param argc Standard main() style argc
5449 * @param argc Standard main() style argv. Initial components are already
5452 * @return normal NTSTATUS return code
5455 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5456 const char *domain_name,
5457 struct cli_state *cli,
5458 struct rpc_pipe_client *pipe_hnd,
5459 TALLOC_CTX *mem_ctx,
5463 POLICY_HND connect_pol, domain_pol, user_pol;
5464 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5467 DOM_SID trust_acct_sid;
5468 uint32 *user_rids, num_rids, *name_types;
5469 uint32 flags = 0x000003e8; /* Unknown */
5472 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5473 return NT_STATUS_INVALID_PARAMETER;
5477 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5479 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5481 if (acct_name == NULL)
5482 return NT_STATUS_NO_MEMORY;
5484 strupper_m(acct_name);
5486 if ((names = TALLOC_ARRAY(mem_ctx, const char *, 1)) == NULL) {
5487 return NT_STATUS_NO_MEMORY;
5489 names[0] = acct_name;
5492 /* Get samr policy handle */
5493 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5495 if (!NT_STATUS_IS_OK(result)) {
5499 /* Get domain policy handle */
5500 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5501 MAXIMUM_ALLOWED_ACCESS,
5502 domain_sid, &domain_pol);
5503 if (!NT_STATUS_IS_OK(result)) {
5507 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5509 &user_rids, &name_types);
5511 if (!NT_STATUS_IS_OK(result)) {
5515 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5516 MAXIMUM_ALLOWED_ACCESS,
5517 user_rids[0], &user_pol);
5519 if (!NT_STATUS_IS_OK(result)) {
5523 /* append the rid to the domain sid */
5524 sid_copy(&trust_acct_sid, domain_sid);
5525 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5529 /* remove the sid */
5531 result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5534 if (!NT_STATUS_IS_OK(result)) {
5540 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5542 if (!NT_STATUS_IS_OK(result)) {
5546 if (!NT_STATUS_IS_OK(result)) {
5547 DEBUG(0,("Could not set trust account password: %s\n",
5548 nt_errstr(result)));
5557 * Delete interdomain trust account for a remote domain.
5559 * @param argc standard argc
5560 * @param argv standard argv without initial components
5562 * @return Integer status (0 means success)
5565 static int rpc_trustdom_del(int argc, const char **argv)
5568 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5571 d_printf("Usage: net rpc trustdom del <domain>\n");
5578 * Establish trust relationship to a trusting domain.
5579 * Interdomain account must already be created on remote PDC.
5581 * @param argc standard argc
5582 * @param argv standard argv without initial components
5584 * @return Integer status (0 means success)
5587 static int rpc_trustdom_establish(int argc, const char **argv)
5589 struct cli_state *cli = NULL;
5590 struct in_addr server_ip;
5591 struct rpc_pipe_client *pipe_hnd = NULL;
5592 POLICY_HND connect_hnd;
5593 TALLOC_CTX *mem_ctx;
5595 DOM_SID *domain_sid;
5598 char* domain_name_pol;
5603 * Connect to \\server\ipc$ as 'our domain' account with password
5607 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5611 domain_name = smb_xstrdup(argv[0]);
5612 strupper_m(domain_name);
5614 /* account name used at first is our domain's name with '$' */
5615 asprintf(&acct_name, "%s$", lp_workgroup());
5616 strupper_m(acct_name);
5619 * opt_workgroup will be used by connection functions further,
5620 * hence it should be set to remote domain name instead of ours
5622 if (opt_workgroup) {
5623 opt_workgroup = smb_xstrdup(domain_name);
5626 opt_user_name = acct_name;
5628 /* find the domain controller */
5629 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5630 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5634 /* connect to ipc$ as username/password */
5635 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5636 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5638 /* Is it trusting domain account for sure ? */
5639 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5640 nt_errstr(nt_status)));
5644 /* store who we connected to */
5646 saf_store( domain_name, pdc_name );
5649 * Connect to \\server\ipc$ again (this time anonymously)
5652 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5654 if (NT_STATUS_IS_ERR(nt_status)) {
5655 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5656 domain_name, nt_errstr(nt_status)));
5660 * Use NetServerEnum2 to make sure we're talking to a proper server
5663 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5664 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5665 for domain %s\n", domain_name));
5668 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5669 "domain %s", domain_name))) {
5670 DEBUG(0, ("talloc_init() failed\n"));
5676 * Call LsaOpenPolicy and LsaQueryInfo
5679 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5681 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5686 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5688 if (NT_STATUS_IS_ERR(nt_status)) {
5689 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5690 nt_errstr(nt_status)));
5695 /* Querying info level 5 */
5697 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5699 &domain_name_pol, &domain_sid);
5700 if (NT_STATUS_IS_ERR(nt_status)) {
5701 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5702 nt_errstr(nt_status)));
5707 /* There should be actually query info level 3 (following nt serv behaviour),
5708 but I still don't know if it's _really_ necessary */
5711 * Store the password in secrets db
5714 if (!secrets_store_trusted_domain_password(domain_name,
5717 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5723 * Close the pipes and clean up
5726 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5727 if (NT_STATUS_IS_ERR(nt_status)) {
5728 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5729 nt_errstr(nt_status)));
5736 talloc_destroy(mem_ctx);
5738 d_printf("Trust to domain %s established\n", domain_name);
5743 * Revoke trust relationship to the remote domain
5745 * @param argc standard argc
5746 * @param argv standard argv without initial components
5748 * @return Integer status (0 means success)
5751 static int rpc_trustdom_revoke(int argc, const char **argv)
5755 if (argc < 1) return -1;
5757 /* generate upper cased domain name */
5758 domain_name = smb_xstrdup(argv[0]);
5759 strupper_m(domain_name);
5761 /* delete password of the trust */
5762 if (!trusted_domain_password_delete(domain_name)) {
5763 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5772 * Usage for 'net rpc trustdom' command
5774 * @param argc standard argc
5775 * @param argv standard argv without inital components
5777 * @return Integer status returned to shell
5780 static int rpc_trustdom_usage(int argc, const char **argv)
5782 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5783 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5784 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5785 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5786 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5787 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5792 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5793 const char *domain_name,
5794 struct cli_state *cli,
5795 struct rpc_pipe_client *pipe_hnd,
5796 TALLOC_CTX *mem_ctx,
5801 sid_to_string(str_sid, domain_sid);
5802 d_printf("%s\n", str_sid);
5803 return NT_STATUS_OK;
5806 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5808 fstring ascii_sid, padding;
5809 int pad_len, col_len = 20;
5811 /* convert sid into ascii string */
5812 sid_to_string(ascii_sid, dom_sid);
5814 /* calculate padding space for d_printf to look nicer */
5815 pad_len = col_len - strlen(trusted_dom_name);
5816 padding[pad_len] = 0;
5817 do padding[--pad_len] = ' '; while (pad_len);
5819 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5822 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5823 TALLOC_CTX *mem_ctx,
5826 const char *trusted_dom_name)
5829 LSA_TRUSTED_DOMAIN_INFO *info;
5830 char *cleartextpwd = NULL;
5833 nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5835 if (NT_STATUS_IS_ERR(nt_status)) {
5836 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5837 nt_errstr(nt_status)));
5841 data = data_blob(NULL, info->password.password.length);
5843 memcpy(data.data, info->password.password.data, info->password.password.length);
5844 data.length = info->password.password.length;
5846 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5848 if (cleartextpwd == NULL) {
5849 DEBUG(0,("retrieved NULL password\n"));
5850 nt_status = NT_STATUS_UNSUCCESSFUL;
5854 if (!secrets_store_trusted_domain_password(trusted_dom_name,
5857 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5858 nt_status = NT_STATUS_UNSUCCESSFUL;
5862 #ifdef DEBUG_PASSWORD
5863 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5864 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5868 SAFE_FREE(cleartextpwd);
5869 data_blob_free(&data);
5874 static int rpc_trustdom_vampire(int argc, const char **argv)
5876 /* common variables */
5877 TALLOC_CTX* mem_ctx;
5878 struct cli_state *cli = NULL;
5879 struct rpc_pipe_client *pipe_hnd = NULL;
5881 const char *domain_name = NULL;
5882 DOM_SID *queried_dom_sid;
5883 POLICY_HND connect_hnd;
5885 /* trusted domains listing variables */
5886 unsigned int num_domains, enum_ctx = 0;
5888 DOM_SID *domain_sids;
5889 char **trusted_dom_names;
5894 * Listing trusted domains (stored in secrets.tdb, if local)
5897 mem_ctx = talloc_init("trust relationships vampire");
5900 * set domain and pdc name to local samba server (default)
5901 * or to remote one given in command line
5904 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5905 domain_name = opt_workgroup;
5906 opt_target_workgroup = opt_workgroup;
5908 fstrcpy(pdc_name, global_myname());
5909 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5910 opt_target_workgroup = domain_name;
5913 /* open \PIPE\lsarpc and open policy handle */
5914 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5915 DEBUG(0, ("Couldn't connect to domain controller\n"));
5919 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5921 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5922 nt_errstr(nt_status) ));
5927 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5929 if (NT_STATUS_IS_ERR(nt_status)) {
5930 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5931 nt_errstr(nt_status)));
5936 /* query info level 5 to obtain sid of a domain being queried */
5937 nt_status = rpccli_lsa_query_info_policy(
5938 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5939 &dummy, &queried_dom_sid);
5941 if (NT_STATUS_IS_ERR(nt_status)) {
5942 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5943 nt_errstr(nt_status)));
5949 * Keep calling LsaEnumTrustdom over opened pipe until
5950 * the end of enumeration is reached
5953 d_printf("Vampire trusted domains:\n\n");
5956 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5958 &trusted_dom_names, &domain_sids);
5960 if (NT_STATUS_IS_ERR(nt_status)) {
5961 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5962 nt_errstr(nt_status)));
5967 for (i = 0; i < num_domains; i++) {
5969 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5971 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5972 domain_sids[i], trusted_dom_names[i]);
5973 if (!NT_STATUS_IS_OK(nt_status)) {
5980 * in case of no trusted domains say something rather
5981 * than just display blank line
5983 if (!num_domains) d_printf("none\n");
5985 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5987 /* close this connection before doing next one */
5988 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5989 if (NT_STATUS_IS_ERR(nt_status)) {
5990 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5991 nt_errstr(nt_status)));
5996 /* close lsarpc pipe and connection to IPC$ */
5999 talloc_destroy(mem_ctx);
6003 static int rpc_trustdom_list(int argc, const char **argv)
6005 /* common variables */
6006 TALLOC_CTX* mem_ctx;
6007 struct cli_state *cli = NULL, *remote_cli = NULL;
6008 struct rpc_pipe_client *pipe_hnd = NULL;
6010 const char *domain_name = NULL;
6011 DOM_SID *queried_dom_sid;
6013 int ascii_dom_name_len;
6014 POLICY_HND connect_hnd;
6016 /* trusted domains listing variables */
6017 unsigned int num_domains, enum_ctx = 0;
6018 int i, pad_len, col_len = 20;
6019 DOM_SID *domain_sids;
6020 char **trusted_dom_names;
6024 /* trusting domains listing variables */
6025 POLICY_HND domain_hnd;
6026 char **trusting_dom_names;
6027 uint32 *trusting_dom_rids;
6030 * Listing trusted domains (stored in secrets.tdb, if local)
6033 mem_ctx = talloc_init("trust relationships listing");
6036 * set domain and pdc name to local samba server (default)
6037 * or to remote one given in command line
6040 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6041 domain_name = opt_workgroup;
6042 opt_target_workgroup = opt_workgroup;
6044 fstrcpy(pdc_name, global_myname());
6045 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6046 opt_target_workgroup = domain_name;
6049 /* open \PIPE\lsarpc and open policy handle */
6050 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
6051 DEBUG(0, ("Couldn't connect to domain controller\n"));
6055 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6057 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6058 nt_errstr(nt_status) ));
6062 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6064 if (NT_STATUS_IS_ERR(nt_status)) {
6065 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6066 nt_errstr(nt_status)));
6070 /* query info level 5 to obtain sid of a domain being queried */
6071 nt_status = rpccli_lsa_query_info_policy(
6072 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
6073 &dummy, &queried_dom_sid);
6075 if (NT_STATUS_IS_ERR(nt_status)) {
6076 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6077 nt_errstr(nt_status)));
6082 * Keep calling LsaEnumTrustdom over opened pipe until
6083 * the end of enumeration is reached
6086 d_printf("Trusted domains list:\n\n");
6089 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6091 &trusted_dom_names, &domain_sids);
6093 if (NT_STATUS_IS_ERR(nt_status)) {
6094 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6095 nt_errstr(nt_status)));
6099 for (i = 0; i < num_domains; i++) {
6100 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6104 * in case of no trusted domains say something rather
6105 * than just display blank line
6107 if (!num_domains) d_printf("none\n");
6109 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6111 /* close this connection before doing next one */
6112 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
6113 if (NT_STATUS_IS_ERR(nt_status)) {
6114 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6115 nt_errstr(nt_status)));
6119 cli_rpc_pipe_close(pipe_hnd);
6122 * Listing trusting domains (stored in passdb backend, if local)
6125 d_printf("\nTrusting domains list:\n\n");
6128 * Open \PIPE\samr and get needed policy handles
6130 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6132 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6137 nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
6139 if (!NT_STATUS_IS_OK(nt_status)) {
6140 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6141 nt_errstr(nt_status)));
6145 /* SamrOpenDomain - we have to open domain policy handle in order to be
6146 able to enumerate accounts*/
6147 nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
6148 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6149 queried_dom_sid, &domain_hnd);
6150 if (!NT_STATUS_IS_OK(nt_status)) {
6151 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6152 nt_errstr(nt_status)));
6157 * perform actual enumeration
6160 enum_ctx = 0; /* reset enumeration context from last enumeration */
6163 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6164 &enum_ctx, ACB_DOMTRUST, 0xffff,
6165 &trusting_dom_names, &trusting_dom_rids,
6167 if (NT_STATUS_IS_ERR(nt_status)) {
6168 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6169 nt_errstr(nt_status)));
6173 for (i = 0; i < num_domains; i++) {
6176 * get each single domain's sid (do we _really_ need this ?):
6177 * 1) connect to domain's pdc
6178 * 2) query the pdc for domain's sid
6181 /* get rid of '$' tail */
6182 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6183 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6184 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6186 /* calculate padding space for d_printf to look nicer */
6187 pad_len = col_len - strlen(trusting_dom_names[i]);
6188 padding[pad_len] = 0;
6189 do padding[--pad_len] = ' '; while (pad_len);
6191 /* set opt_* variables to remote domain */
6192 strupper_m(trusting_dom_names[i]);
6193 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6194 opt_target_workgroup = opt_workgroup;
6196 d_printf("%s%s", trusting_dom_names[i], padding);
6198 /* connect to remote domain controller */
6199 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6201 /* query for domain's sid */
6202 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6203 d_fprintf(stderr, "couldn't get domain's sid\n");
6205 cli_shutdown(remote_cli);
6208 d_fprintf(stderr, "domain controller is not responding\n");
6212 if (!num_domains) d_printf("none\n");
6214 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6216 /* close opened samr and domain policy handles */
6217 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6218 if (!NT_STATUS_IS_OK(nt_status)) {
6219 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6222 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6223 if (!NT_STATUS_IS_OK(nt_status)) {
6224 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6227 /* close samr pipe and connection to IPC$ */
6230 talloc_destroy(mem_ctx);
6235 * Entrypoint for 'net rpc trustdom' code
6237 * @param argc standard argc
6238 * @param argv standard argv without initial components
6240 * @return Integer status (0 means success)
6243 static int rpc_trustdom(int argc, const char **argv)
6245 struct functable func[] = {
6246 {"add", rpc_trustdom_add},
6247 {"del", rpc_trustdom_del},
6248 {"establish", rpc_trustdom_establish},
6249 {"revoke", rpc_trustdom_revoke},
6250 {"help", rpc_trustdom_usage},
6251 {"list", rpc_trustdom_list},
6252 {"vampire", rpc_trustdom_vampire},
6257 rpc_trustdom_usage(argc, argv);
6261 return (net_run_function(argc, argv, func, rpc_user_usage));
6265 * Check if a server will take rpc commands
6266 * @param flags Type of server to connect to (PDC, DMB, localhost)
6267 * if the host is not explicitly specified
6268 * @return BOOL (true means rpc supported)
6270 BOOL net_rpc_check(unsigned flags)
6272 struct cli_state cli;
6274 struct in_addr server_ip;
6275 char *server_name = NULL;
6277 /* flags (i.e. server type) may depend on command */
6278 if (!net_find_server(NULL, flags, &server_ip, &server_name))
6282 if (cli_initialise(&cli) == False)
6285 if (!cli_connect(&cli, server_name, &server_ip))
6287 if (!attempt_netbios_session_request(&cli, global_myname(),
6288 server_name, &server_ip))
6290 if (!cli_negprot(&cli))
6292 if (cli.protocol < PROTOCOL_NT1)
6301 /* dump sam database via samsync rpc calls */
6302 static int rpc_samdump(int argc, const char **argv) {
6303 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6307 /* syncronise sam database via samsync rpc calls */
6308 static int rpc_vampire(int argc, const char **argv) {
6309 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6314 * Migrate everything from a print-server
6316 * @param argc Standard main() style argc
6317 * @param argv Standard main() style argv. Initial components are already
6320 * @return A shell status integer (0 for success)
6322 * The order is important !
6323 * To successfully add drivers the print-queues have to exist !
6324 * Applying ACLs should be the last step, because you're easily locked out
6327 static int rpc_printer_migrate_all(int argc, const char **argv)
6332 printf("no server to migrate\n");
6336 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6340 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6344 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6348 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6352 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6357 * Migrate print-drivers from a print-server
6359 * @param argc Standard main() style argc
6360 * @param argv Standard main() style argv. Initial components are already
6363 * @return A shell status integer (0 for success)
6365 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6368 printf("no server to migrate\n");
6372 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6373 rpc_printer_migrate_drivers_internals,
6378 * Migrate print-forms from a print-server
6380 * @param argc Standard main() style argc
6381 * @param argv Standard main() style argv. Initial components are already
6384 * @return A shell status integer (0 for success)
6386 static int rpc_printer_migrate_forms(int argc, const char **argv)
6389 printf("no server to migrate\n");
6393 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6394 rpc_printer_migrate_forms_internals,
6399 * Migrate printers from a print-server
6401 * @param argc Standard main() style argc
6402 * @param argv Standard main() style argv. Initial components are already
6405 * @return A shell status integer (0 for success)
6407 static int rpc_printer_migrate_printers(int argc, const char **argv)
6410 printf("no server to migrate\n");
6414 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6415 rpc_printer_migrate_printers_internals,
6420 * Migrate printer-ACLs from a print-server
6422 * @param argc Standard main() style argc
6423 * @param argv Standard main() style argv. Initial components are already
6426 * @return A shell status integer (0 for success)
6428 static int rpc_printer_migrate_security(int argc, const char **argv)
6431 printf("no server to migrate\n");
6435 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6436 rpc_printer_migrate_security_internals,
6441 * Migrate printer-settings from a print-server
6443 * @param argc Standard main() style argc
6444 * @param argv Standard main() style argv. Initial components are already
6447 * @return A shell status integer (0 for success)
6449 static int rpc_printer_migrate_settings(int argc, const char **argv)
6452 printf("no server to migrate\n");
6456 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6457 rpc_printer_migrate_settings_internals,
6462 * 'net rpc printer' entrypoint.
6463 * @param argc Standard main() style argc
6464 * @param argv Standard main() style argv. Initial components are already
6468 int rpc_printer_migrate(int argc, const char **argv)
6471 /* ouch: when addriver and setdriver are called from within
6472 rpc_printer_migrate_drivers_internals, the printer-queue already
6475 struct functable func[] = {
6476 {"all", rpc_printer_migrate_all},
6477 {"drivers", rpc_printer_migrate_drivers},
6478 {"forms", rpc_printer_migrate_forms},
6479 {"help", rpc_printer_usage},
6480 {"printers", rpc_printer_migrate_printers},
6481 {"security", rpc_printer_migrate_security},
6482 {"settings", rpc_printer_migrate_settings},
6486 return net_run_function(argc, argv, func, rpc_printer_usage);
6491 * List printers on a remote RPC server
6493 * @param argc Standard main() style argc
6494 * @param argv Standard main() style argv. Initial components are already
6497 * @return A shell status integer (0 for success)
6499 static int rpc_printer_list(int argc, const char **argv)
6502 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6503 rpc_printer_list_internals,
6508 * List printer-drivers on a remote RPC server
6510 * @param argc Standard main() style argc
6511 * @param argv Standard main() style argv. Initial components are already
6514 * @return A shell status integer (0 for success)
6516 static int rpc_printer_driver_list(int argc, const char **argv)
6519 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6520 rpc_printer_driver_list_internals,
6525 * Publish printer in ADS via MSRPC
6527 * @param argc Standard main() style argc
6528 * @param argv Standard main() style argv. Initial components are already
6531 * @return A shell status integer (0 for success)
6533 static int rpc_printer_publish_publish(int argc, const char **argv)
6536 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6537 rpc_printer_publish_publish_internals,
6542 * Update printer in ADS via MSRPC
6544 * @param argc Standard main() style argc
6545 * @param argv Standard main() style argv. Initial components are already
6548 * @return A shell status integer (0 for success)
6550 static int rpc_printer_publish_update(int argc, const char **argv)
6553 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6554 rpc_printer_publish_update_internals,
6559 * UnPublish printer in ADS via MSRPC
6561 * @param argc Standard main() style argc
6562 * @param argv Standard main() style argv. Initial components are already
6565 * @return A shell status integer (0 for success)
6567 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6570 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6571 rpc_printer_publish_unpublish_internals,
6576 * List published printers via MSRPC
6578 * @param argc Standard main() style argc
6579 * @param argv Standard main() style argv. Initial components are already
6582 * @return A shell status integer (0 for success)
6584 static int rpc_printer_publish_list(int argc, const char **argv)
6587 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6588 rpc_printer_publish_list_internals,
6594 * Publish printer in ADS
6596 * @param argc Standard main() style argc
6597 * @param argv Standard main() style argv. Initial components are already
6600 * @return A shell status integer (0 for success)
6602 static int rpc_printer_publish(int argc, const char **argv)
6605 struct functable func[] = {
6606 {"publish", rpc_printer_publish_publish},
6607 {"update", rpc_printer_publish_update},
6608 {"unpublish", rpc_printer_publish_unpublish},
6609 {"list", rpc_printer_publish_list},
6610 {"help", rpc_printer_usage},
6615 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6616 rpc_printer_publish_list_internals,
6619 return net_run_function(argc, argv, func, rpc_printer_usage);
6625 * Display rpc printer help page.
6626 * @param argc Standard main() style argc
6627 * @param argv Standard main() style argv. Initial components are already
6630 int rpc_printer_usage(int argc, const char **argv)
6632 return net_help_printer(argc, argv);
6636 * 'net rpc printer' entrypoint.
6637 * @param argc Standard main() style argc
6638 * @param argv Standard main() style argv. Initial components are already
6641 int net_rpc_printer(int argc, const char **argv)
6643 struct functable func[] = {
6644 {"list", rpc_printer_list},
6645 {"migrate", rpc_printer_migrate},
6646 {"driver", rpc_printer_driver_list},
6647 {"publish", rpc_printer_publish},
6652 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6653 rpc_printer_list_internals,
6656 return net_run_function(argc, argv, func, rpc_printer_usage);
6659 /****************************************************************************/
6663 * Basic usage function for 'net rpc'
6664 * @param argc Standard main() style argc
6665 * @param argv Standard main() style argv. Initial components are already
6669 int net_rpc_usage(int argc, const char **argv)
6671 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6672 d_printf(" net rpc join \t\t\tto join a domain \n");
6673 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6674 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6675 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6676 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6677 d_printf(" net rpc group \t\tto list groups\n");
6678 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6679 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6680 d_printf(" net rpc file \t\t\tto list open files\n");
6681 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6682 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6683 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6684 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6685 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6686 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6687 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6688 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6689 d_printf(" net rpc registry\t\tto manage registry hives\n");
6690 d_printf(" net rpc service\t\tto start, stop and query services\n");
6691 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6692 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6694 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6695 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6696 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6697 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6698 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6704 * Help function for 'net rpc'. Calls command specific help if requested
6705 * or displays usage of net rpc
6706 * @param argc Standard main() style argc
6707 * @param argv Standard main() style argv. Initial components are already
6711 int net_rpc_help(int argc, const char **argv)
6713 struct functable func[] = {
6714 {"join", rpc_join_usage},
6715 {"user", rpc_user_usage},
6716 {"group", rpc_group_usage},
6717 {"share", rpc_share_usage},
6718 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6719 {"trustdom", rpc_trustdom_usage},
6720 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6721 /*{"shutdown", rpc_shutdown_usage}, */
6722 {"vampire", rpc_vampire_usage},
6727 net_rpc_usage(argc, argv);
6731 return (net_run_function(argc, argv, func, rpc_user_usage));
6735 * 'net rpc' entrypoint.
6736 * @param argc Standard main() style argc
6737 * @param argv Standard main() style argv. Initial components are already
6741 int net_rpc(int argc, const char **argv)
6743 struct functable func[] = {
6744 {"audit", net_rpc_audit},
6745 {"info", net_rpc_info},
6746 {"join", net_rpc_join},
6747 {"oldjoin", net_rpc_oldjoin},
6748 {"testjoin", net_rpc_testjoin},
6749 {"user", net_rpc_user},
6750 {"password", rpc_user_password},
6751 {"group", net_rpc_group},
6752 {"share", net_rpc_share},
6753 {"file", net_rpc_file},
6754 {"printer", net_rpc_printer},
6755 {"changetrustpw", net_rpc_changetrustpw},
6756 {"trustdom", rpc_trustdom},
6757 {"abortshutdown", rpc_shutdown_abort},
6758 {"shutdown", rpc_shutdown},
6759 {"samdump", rpc_samdump},
6760 {"vampire", rpc_vampire},
6761 {"getsid", net_rpc_getsid},
6762 {"rights", net_rpc_rights},
6763 {"service", net_rpc_service},
6764 {"registry", net_rpc_registry},
6765 {"shell", net_rpc_shell},
6766 {"help", net_rpc_help},
6769 return net_run_function(argc, argv, func, net_rpc_usage);