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)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
26 static void init_lsa_String(struct lsa_String *name, const char *s)
31 static int net_mode_share;
32 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
37 * @brief RPC based subcommands for the 'net' utility.
39 * This file should contain much of the functionality that used to
40 * be found in rpcclient, execpt that the commands should change
41 * less often, and the fucntionality should be sane (the user is not
42 * expected to know a rid/sid before they conduct an operation etc.)
44 * @todo Perhaps eventually these should be split out into a number
45 * of files, as this could get quite big.
50 * Many of the RPC functions need the domain sid. This function gets
51 * it at the start of every run
53 * @param cli A cli_state already connected to the remote machine
55 * @return The Domain SID of the remote machine.
58 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
60 const char **domain_name)
62 struct rpc_pipe_client *lsa_pipe;
64 NTSTATUS result = NT_STATUS_OK;
65 union lsa_PolicyInformation *info = NULL;
67 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
69 d_fprintf(stderr, "Could not initialise lsa pipe\n");
73 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False,
74 SEC_RIGHTS_MAXIMUM_ALLOWED,
76 if (!NT_STATUS_IS_OK(result)) {
77 d_fprintf(stderr, "open_policy failed: %s\n",
82 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
84 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
86 if (!NT_STATUS_IS_OK(result)) {
87 d_fprintf(stderr, "lsaquery failed: %s\n",
92 *domain_name = info->account_domain.name.string;
93 *domain_sid = info->account_domain.sid;
95 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
96 cli_rpc_pipe_close(lsa_pipe);
102 * Run a single RPC command, from start to finish.
104 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
105 * @param conn_flag a NET_FLAG_ combination. Passed to
106 * net_make_ipc_connection.
107 * @param argc Standard main() style argc
108 * @param argc Standard main() style argv. Initial components are already
110 * @return A shell status integer (0 for success)
113 int run_rpc_command(struct cli_state *cli_arg,
120 struct cli_state *cli = NULL;
121 struct rpc_pipe_client *pipe_hnd = NULL;
125 const char *domain_name;
127 /* make use of cli_state handed over as an argument, if possible */
129 nt_status = net_make_ipc_connection(conn_flags, &cli);
130 if (!NT_STATUS_IS_OK(nt_status)) {
131 DEBUG(1, ("failed to make ipc connection: %s\n",
132 nt_errstr(nt_status)));
145 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
146 DEBUG(0, ("talloc_init() failed\n"));
151 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
153 if (!NT_STATUS_IS_OK(nt_status)) {
158 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
159 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
160 /* Always try and create an schannel netlogon pipe. */
161 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
162 PIPE_AUTH_LEVEL_PRIVACY,
166 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
167 nt_errstr(nt_status) ));
172 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
174 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
175 cli_get_pipe_name(pipe_idx),
176 nt_errstr(nt_status) ));
183 nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
185 if (!NT_STATUS_IS_OK(nt_status)) {
186 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
188 DEBUG(5, ("rpc command function succedded\n"));
191 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
193 cli_rpc_pipe_close(pipe_hnd);
197 /* close the connection only if it was opened here */
202 talloc_destroy(mem_ctx);
203 return (!NT_STATUS_IS_OK(nt_status));
207 * Force a change of the trust acccount password.
209 * All parameters are provided by the run_rpc_command function, except for
210 * argc, argv which are passes through.
212 * @param domain_sid The domain sid aquired from the remote server
213 * @param cli A cli_state connected to the server.
214 * @param mem_ctx Talloc context, destoyed on compleation of the function.
215 * @param argc Standard main() style argc
216 * @param argc Standard main() style argv. Initial components are already
219 * @return Normal NTSTATUS return.
222 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
223 const char *domain_name,
224 struct cli_state *cli,
225 struct rpc_pipe_client *pipe_hnd,
231 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
235 * Force a change of the trust acccount password.
237 * @param argc Standard main() style argc
238 * @param argc Standard main() style argv. Initial components are already
241 * @return A shell status integer (0 for success)
244 int net_rpc_changetrustpw(int argc, const char **argv)
246 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
247 rpc_changetrustpw_internals,
252 * Join a domain, the old way.
254 * This uses 'machinename' as the inital password, and changes it.
256 * The password should be created with 'server manager' or equiv first.
258 * All parameters are provided by the run_rpc_command function, except for
259 * argc, argv which are passes through.
261 * @param domain_sid The domain sid aquired from the remote server
262 * @param cli A cli_state connected to the server.
263 * @param mem_ctx Talloc context, destoyed on compleation of the function.
264 * @param argc Standard main() style argc
265 * @param argc Standard main() style argv. Initial components are already
268 * @return Normal NTSTATUS return.
271 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
272 const char *domain_name,
273 struct cli_state *cli,
274 struct rpc_pipe_client *pipe_hnd,
280 fstring trust_passwd;
281 unsigned char orig_trust_passwd_hash[16];
283 uint32 sec_channel_type;
285 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
287 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
290 nt_errstr(result) ));
295 check what type of join - if the user want's to join as
296 a BDC, the server must agree that we are a BDC.
299 sec_channel_type = get_sec_channel_type(argv[0]);
301 sec_channel_type = get_sec_channel_type(NULL);
304 fstrcpy(trust_passwd, global_myname());
305 strlower_m(trust_passwd);
308 * Machine names can be 15 characters, but the max length on
309 * a password is 14. --jerry
312 trust_passwd[14] = '\0';
314 E_md4hash(trust_passwd, orig_trust_passwd_hash);
316 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
317 orig_trust_passwd_hash,
320 if (NT_STATUS_IS_OK(result))
321 printf("Joined domain %s.\n",opt_target_workgroup);
324 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
325 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
326 result = NT_STATUS_UNSUCCESSFUL;
333 * Join a domain, the old way.
335 * @param argc Standard main() style argc
336 * @param argc Standard main() style argv. Initial components are already
339 * @return A shell status integer (0 for success)
342 static int net_rpc_perform_oldjoin(int argc, const char **argv)
344 return run_rpc_command(NULL, PI_NETLOGON,
345 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
346 rpc_oldjoin_internals,
351 * Join a domain, the old way. This function exists to allow
352 * the message to be displayed when oldjoin was explicitly
353 * requested, but not when it was implied by "net rpc join"
355 * @param argc Standard main() style argc
356 * @param argc Standard main() style argv. Initial components are already
359 * @return A shell status integer (0 for success)
362 static int net_rpc_oldjoin(int argc, const char **argv)
364 int rc = net_rpc_perform_oldjoin(argc, argv);
367 d_fprintf(stderr, "Failed to join domain\n");
374 * Basic usage function for 'net rpc join'
375 * @param argc Standard main() style argc
376 * @param argc Standard main() style argv. Initial components are already
380 static int rpc_join_usage(int argc, const char **argv)
382 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
383 "\t to join a domain with admin username & password\n"\
384 "\t\t password will be prompted if needed and none is specified\n"\
385 "\t <type> can be (default MEMBER)\n"\
386 "\t\t BDC - Join as a BDC\n"\
387 "\t\t PDC - Join as a PDC\n"\
388 "\t\t MEMBER - Join as a MEMBER server\n");
390 net_common_flags_usage(argc, argv);
395 * 'net rpc join' entrypoint.
396 * @param argc Standard main() style argc
397 * @param argc Standard main() style argv. Initial components are already
400 * Main 'net_rpc_join()' (where the admin username/password is used) is
402 * Try to just change the password, but if that doesn't work, use/prompt
403 * for a username/password.
406 int net_rpc_join(int argc, const char **argv)
408 if (lp_server_role() == ROLE_STANDALONE) {
409 d_printf("cannot join as standalone machine\n");
413 if (strlen(global_myname()) > 15) {
414 d_printf("Our netbios name can be at most 15 chars long, "
415 "\"%s\" is %u chars long\n",
416 global_myname(), (unsigned int)strlen(global_myname()));
420 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
423 return net_rpc_join_newstyle(argc, argv);
427 * display info about a rpc domain
429 * All parameters are provided by the run_rpc_command function, except for
430 * argc, argv which are passed through.
432 * @param domain_sid The domain sid acquired from the remote server
433 * @param cli A cli_state connected to the server.
434 * @param mem_ctx Talloc context, destoyed on completion of the function.
435 * @param argc Standard main() style argc
436 * @param argv Standard main() style argv. Initial components are already
439 * @return Normal NTSTATUS return.
442 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
443 const char *domain_name,
444 struct cli_state *cli,
445 struct rpc_pipe_client *pipe_hnd,
450 POLICY_HND connect_pol, domain_pol;
451 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
452 union samr_DomainInfo *info = NULL;
455 sid_to_fstring(sid_str, domain_sid);
457 /* Get sam policy handle */
458 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
459 pipe_hnd->cli->desthost,
460 MAXIMUM_ALLOWED_ACCESS,
462 if (!NT_STATUS_IS_OK(result)) {
463 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
467 /* Get domain policy handle */
468 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
470 MAXIMUM_ALLOWED_ACCESS,
471 CONST_DISCARD(struct dom_sid2 *, domain_sid),
473 if (!NT_STATUS_IS_OK(result)) {
474 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
478 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
482 if (NT_STATUS_IS_OK(result)) {
483 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
484 d_printf("Domain SID: %s\n", sid_str);
485 d_printf("Sequence number: %llu\n",
486 (unsigned long long)info->info2.sequence_num);
487 d_printf("Num users: %u\n", info->info2.num_users);
488 d_printf("Num domain groups: %u\n", info->info2.num_groups);
489 d_printf("Num local groups: %u\n", info->info2.num_aliases);
497 * 'net rpc info' entrypoint.
498 * @param argc Standard main() style argc
499 * @param argc Standard main() style argv. Initial components are already
503 int net_rpc_info(int argc, const char **argv)
505 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC,
511 * Fetch domain SID into the local secrets.tdb
513 * All parameters are provided by the run_rpc_command function, except for
514 * argc, argv which are passes through.
516 * @param domain_sid The domain sid acquired from the remote server
517 * @param cli A cli_state connected to the server.
518 * @param mem_ctx Talloc context, destoyed on completion of the function.
519 * @param argc Standard main() style argc
520 * @param argv Standard main() style argv. Initial components are already
523 * @return Normal NTSTATUS return.
526 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
527 const char *domain_name,
528 struct cli_state *cli,
529 struct rpc_pipe_client *pipe_hnd,
536 sid_to_fstring(sid_str, domain_sid);
537 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
538 sid_str, domain_name);
540 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
541 DEBUG(0,("Can't store domain SID\n"));
542 return NT_STATUS_UNSUCCESSFUL;
549 * 'net rpc getsid' entrypoint.
550 * @param argc Standard main() style argc
551 * @param argc Standard main() style argv. Initial components are already
555 int net_rpc_getsid(int argc, const char **argv)
557 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
558 rpc_getsid_internals,
562 /****************************************************************************/
565 * Basic usage function for 'net rpc user'
566 * @param argc Standard main() style argc.
567 * @param argv Standard main() style argv. Initial components are already
571 static int rpc_user_usage(int argc, const char **argv)
573 return net_help_user(argc, argv);
577 * Add a new user to a remote RPC server
579 * All parameters are provided by the run_rpc_command function, except for
580 * argc, argv which are passes through.
582 * @param domain_sid The domain sid acquired from the remote server
583 * @param cli A cli_state connected to the server.
584 * @param mem_ctx Talloc context, destoyed on completion of the function.
585 * @param argc Standard main() style argc
586 * @param argv Standard main() style argv. Initial components are already
589 * @return Normal NTSTATUS return.
592 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
593 const char *domain_name,
594 struct cli_state *cli,
595 struct rpc_pipe_client *pipe_hnd,
597 int argc, const char **argv)
600 POLICY_HND connect_pol, domain_pol, user_pol;
601 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
602 const char *acct_name;
603 struct lsa_String lsa_acct_name;
605 uint32 acct_flags, user_rid;
606 uint32_t access_granted = 0;
607 struct samr_Ids user_rids, name_types;
610 d_printf("User must be specified\n");
611 rpc_user_usage(argc, argv);
616 init_lsa_String(&lsa_acct_name, acct_name);
618 /* Get sam policy handle */
620 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
621 pipe_hnd->cli->desthost,
622 MAXIMUM_ALLOWED_ACCESS,
624 if (!NT_STATUS_IS_OK(result)) {
628 /* Get domain policy handle */
630 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
632 MAXIMUM_ALLOWED_ACCESS,
633 CONST_DISCARD(struct dom_sid2 *, domain_sid),
635 if (!NT_STATUS_IS_OK(result)) {
639 /* Create domain user */
641 acb_info = ACB_NORMAL;
642 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
643 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
644 SAMR_USER_ACCESS_SET_PASSWORD |
645 SAMR_USER_ACCESS_GET_ATTRIBUTES |
646 SAMR_USER_ACCESS_SET_ATTRIBUTES;
648 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
657 if (!NT_STATUS_IS_OK(result)) {
663 SAM_USERINFO_CTR ctr;
664 SAM_USER_INFO_24 p24;
667 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
674 if (!NT_STATUS_IS_OK(result)) {
678 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
680 MAXIMUM_ALLOWED_ACCESS,
684 if (!NT_STATUS_IS_OK(result)) {
688 /* Set password on account */
693 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
695 init_sam_user_info24(&p24, (char *)pwbuf,24);
697 ctr.switch_value = 24;
698 ctr.info.id24 = &p24;
700 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
701 &cli->user_session_key, &ctr);
703 if (!NT_STATUS_IS_OK(result)) {
704 d_fprintf(stderr, "Failed to set password for user %s - %s\n",
705 acct_name, nt_errstr(result));
707 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
710 if (!NT_STATUS_IS_OK(result)) {
711 d_fprintf(stderr, "Failed to delete user %s - %s\n",
712 acct_name, nt_errstr(result));
719 if (!NT_STATUS_IS_OK(result)) {
720 d_fprintf(stderr, "Failed to add user '%s' with %s.\n",
721 acct_name, nt_errstr(result));
723 d_printf("Added user '%s'.\n", acct_name);
729 * Add a new user to a remote RPC server
731 * @param argc Standard main() style argc
732 * @param argv Standard main() style argv. Initial components are already
735 * @return A shell status integer (0 for success)
738 static int rpc_user_add(int argc, const char **argv)
740 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
745 * Delete a user from a remote RPC server
747 * All parameters are provided by the run_rpc_command function, except for
748 * argc, argv which are passes through.
750 * @param domain_sid The domain sid acquired from the remote server
751 * @param cli A cli_state connected to the server.
752 * @param mem_ctx Talloc context, destoyed on completion of the function.
753 * @param argc Standard main() style argc
754 * @param argv Standard main() style argv. Initial components are already
757 * @return Normal NTSTATUS return.
760 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
761 const char *domain_name,
762 struct cli_state *cli,
763 struct rpc_pipe_client *pipe_hnd,
768 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
769 POLICY_HND connect_pol, domain_pol, user_pol;
770 const char *acct_name;
773 d_printf("User must be specified\n");
774 rpc_user_usage(argc, argv);
780 /* Get sam policy and domain handles */
782 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
783 pipe_hnd->cli->desthost,
784 MAXIMUM_ALLOWED_ACCESS,
787 if (!NT_STATUS_IS_OK(result)) {
791 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
793 MAXIMUM_ALLOWED_ACCESS,
794 CONST_DISCARD(struct dom_sid2 *, domain_sid),
797 if (!NT_STATUS_IS_OK(result)) {
801 /* Get handle on user */
804 struct samr_Ids user_rids, name_types;
805 struct lsa_String lsa_acct_name;
807 init_lsa_String(&lsa_acct_name, acct_name);
809 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
816 if (!NT_STATUS_IS_OK(result)) {
820 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
822 MAXIMUM_ALLOWED_ACCESS,
826 if (!NT_STATUS_IS_OK(result)) {
833 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
836 if (!NT_STATUS_IS_OK(result)) {
841 if (!NT_STATUS_IS_OK(result)) {
842 d_fprintf(stderr, "Failed to delete user '%s' with %s.\n",
843 acct_name, nt_errstr(result));
845 d_printf("Deleted user '%s'.\n", acct_name);
852 * Rename a user on a remote RPC server
854 * All parameters are provided by the run_rpc_command function, except for
855 * argc, argv which are passes through.
857 * @param domain_sid The domain sid acquired from the remote server
858 * @param cli A cli_state connected to the server.
859 * @param mem_ctx Talloc context, destoyed on completion of the function.
860 * @param argc Standard main() style argc
861 * @param argv Standard main() style argv. Initial components are already
864 * @return Normal NTSTATUS return.
867 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
868 const char *domain_name,
869 struct cli_state *cli,
870 struct rpc_pipe_client *pipe_hnd,
875 POLICY_HND connect_pol, domain_pol, user_pol;
876 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
877 uint32 info_level = 7;
878 const char *old_name, *new_name;
879 SAM_USERINFO_CTR *user_ctr;
880 SAM_USERINFO_CTR ctr;
881 SAM_USER_INFO_7 info7;
882 struct samr_Ids user_rids, name_types;
883 struct lsa_String lsa_acct_name;
886 d_printf("Old and new username must be specified\n");
887 rpc_user_usage(argc, argv);
895 ZERO_STRUCT(user_ctr);
897 /* Get sam policy handle */
899 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
900 pipe_hnd->cli->desthost,
901 MAXIMUM_ALLOWED_ACCESS,
904 if (!NT_STATUS_IS_OK(result)) {
908 /* Get domain policy handle */
910 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
912 MAXIMUM_ALLOWED_ACCESS,
913 CONST_DISCARD(struct dom_sid2 *, domain_sid),
915 if (!NT_STATUS_IS_OK(result)) {
919 init_lsa_String(&lsa_acct_name, old_name);
921 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
927 if (!NT_STATUS_IS_OK(result)) {
931 /* Open domain user */
932 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
934 MAXIMUM_ALLOWED_ACCESS,
938 if (!NT_STATUS_IS_OK(result)) {
942 /* Query user info */
943 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
944 info_level, &user_ctr);
946 if (!NT_STATUS_IS_OK(result)) {
950 ctr.switch_value = info_level;
951 ctr.info.id7 = &info7;
953 init_sam_user_info7(&info7, new_name);
956 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
957 info_level, &cli->user_session_key, &ctr);
959 if (!NT_STATUS_IS_OK(result)) {
964 if (!NT_STATUS_IS_OK(result)) {
965 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
968 d_printf("Renamed user from %s to %s\n", old_name, new_name);
974 * Rename a user on a remote RPC server
976 * @param argc Standard main() style argc
977 * @param argv Standard main() style argv. Initial components are already
980 * @return A shell status integer (0 for success)
983 static int rpc_user_rename(int argc, const char **argv)
985 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
990 * Delete a user from a remote RPC server
992 * @param argc Standard main() style argc
993 * @param argv Standard main() style argv. Initial components are already
996 * @return A shell status integer (0 for success)
999 static int rpc_user_delete(int argc, const char **argv)
1001 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
1006 * Set a password for a user on a remote RPC server
1008 * All parameters are provided by the run_rpc_command function, except for
1009 * argc, argv which are passes through.
1011 * @param domain_sid The domain sid acquired from the remote server
1012 * @param cli A cli_state connected to the server.
1013 * @param mem_ctx Talloc context, destoyed on completion of the function.
1014 * @param argc Standard main() style argc
1015 * @param argv Standard main() style argv. Initial components are already
1018 * @return Normal NTSTATUS return.
1021 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
1022 const char *domain_name,
1023 struct cli_state *cli,
1024 struct rpc_pipe_client *pipe_hnd,
1025 TALLOC_CTX *mem_ctx,
1029 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1030 POLICY_HND connect_pol, domain_pol, user_pol;
1031 SAM_USERINFO_CTR ctr;
1032 SAM_USER_INFO_24 p24;
1035 const char *new_password;
1036 char *prompt = NULL;
1039 d_printf("User must be specified\n");
1040 rpc_user_usage(argc, argv);
1041 return NT_STATUS_OK;
1047 new_password = argv[1];
1049 asprintf(&prompt, "Enter new password for %s:", user);
1050 new_password = getpass(prompt);
1054 /* Get sam policy and domain handles */
1056 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1057 pipe_hnd->cli->desthost,
1058 MAXIMUM_ALLOWED_ACCESS,
1061 if (!NT_STATUS_IS_OK(result)) {
1065 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1067 MAXIMUM_ALLOWED_ACCESS,
1068 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1071 if (!NT_STATUS_IS_OK(result)) {
1075 /* Get handle on user */
1078 struct samr_Ids user_rids, name_types;
1079 struct lsa_String lsa_acct_name;
1081 init_lsa_String(&lsa_acct_name, user);
1083 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1089 if (!NT_STATUS_IS_OK(result)) {
1093 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1095 MAXIMUM_ALLOWED_ACCESS,
1099 if (!NT_STATUS_IS_OK(result)) {
1104 /* Set password on account */
1109 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
1111 init_sam_user_info24(&p24, (char *)pwbuf,24);
1113 ctr.switch_value = 24;
1114 ctr.info.id24 = &p24;
1116 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
1117 &cli->user_session_key, &ctr);
1119 if (!NT_STATUS_IS_OK(result)) {
1123 /* Display results */
1131 * Set a user's password on a remote RPC server
1133 * @param argc Standard main() style argc
1134 * @param argv Standard main() style argv. Initial components are already
1137 * @return A shell status integer (0 for success)
1140 static int rpc_user_password(int argc, const char **argv)
1142 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1147 * List user's groups on a remote RPC server
1149 * All parameters are provided by the run_rpc_command function, except for
1150 * argc, argv which are passes through.
1152 * @param domain_sid The domain sid acquired from the remote server
1153 * @param cli A cli_state connected to the server.
1154 * @param mem_ctx Talloc context, destoyed on completion of the function.
1155 * @param argc Standard main() style argc
1156 * @param argv Standard main() style argv. Initial components are already
1159 * @return Normal NTSTATUS return.
1162 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1163 const char *domain_name,
1164 struct cli_state *cli,
1165 struct rpc_pipe_client *pipe_hnd,
1166 TALLOC_CTX *mem_ctx,
1170 POLICY_HND connect_pol, domain_pol, user_pol;
1171 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1173 struct samr_RidWithAttributeArray *rid_array = NULL;
1174 struct lsa_Strings names;
1175 struct samr_Ids types;
1176 uint32_t *lrids = NULL;
1177 struct samr_Ids rids, name_types;
1178 struct lsa_String lsa_acct_name;
1182 d_printf("User must be specified\n");
1183 rpc_user_usage(argc, argv);
1184 return NT_STATUS_OK;
1186 /* Get sam policy handle */
1188 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1189 pipe_hnd->cli->desthost,
1190 MAXIMUM_ALLOWED_ACCESS,
1192 if (!NT_STATUS_IS_OK(result)) goto done;
1194 /* Get domain policy handle */
1196 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1198 MAXIMUM_ALLOWED_ACCESS,
1199 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1201 if (!NT_STATUS_IS_OK(result)) goto done;
1203 /* Get handle on user */
1205 init_lsa_String(&lsa_acct_name, argv[0]);
1207 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1214 if (!NT_STATUS_IS_OK(result)) goto done;
1216 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1218 MAXIMUM_ALLOWED_ACCESS,
1221 if (!NT_STATUS_IS_OK(result)) goto done;
1223 result = rpccli_samr_GetGroupsForUser(pipe_hnd, mem_ctx,
1227 if (!NT_STATUS_IS_OK(result)) goto done;
1231 if (rid_array->count) {
1232 if ((lrids = TALLOC_ARRAY(mem_ctx, uint32, rid_array->count)) == NULL) {
1233 result = NT_STATUS_NO_MEMORY;
1237 for (i = 0; i < rid_array->count; i++)
1238 lrids[i] = rid_array->rids[i].rid;
1240 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
1247 if (!NT_STATUS_IS_OK(result)) {
1251 /* Display results */
1253 for (i = 0; i < names.count; i++)
1254 printf("%s\n", names.names[i].string);
1261 * List a user's groups from a remote RPC server
1263 * @param argc Standard main() style argc
1264 * @param argv Standard main() style argv. Initial components are already
1267 * @return A shell status integer (0 for success)
1270 static int rpc_user_info(int argc, const char **argv)
1272 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1277 * List users on a remote RPC server
1279 * All parameters are provided by the run_rpc_command function, except for
1280 * argc, argv which are passes through.
1282 * @param domain_sid The domain sid acquired from the remote server
1283 * @param cli A cli_state connected to the server.
1284 * @param mem_ctx Talloc context, destoyed on completion of the function.
1285 * @param argc Standard main() style argc
1286 * @param argv Standard main() style argv. Initial components are already
1289 * @return Normal NTSTATUS return.
1292 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1293 const char *domain_name,
1294 struct cli_state *cli,
1295 struct rpc_pipe_client *pipe_hnd,
1296 TALLOC_CTX *mem_ctx,
1300 POLICY_HND connect_pol, domain_pol;
1301 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1302 uint32 start_idx=0, num_entries, i, loop_count = 0;
1304 /* Get sam policy handle */
1306 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1307 pipe_hnd->cli->desthost,
1308 MAXIMUM_ALLOWED_ACCESS,
1310 if (!NT_STATUS_IS_OK(result)) {
1314 /* Get domain policy handle */
1316 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1318 MAXIMUM_ALLOWED_ACCESS,
1319 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1321 if (!NT_STATUS_IS_OK(result)) {
1325 /* Query domain users */
1326 if (opt_long_list_entries)
1327 d_printf("\nUser name Comment"\
1328 "\n-----------------------------\n");
1330 const char *user = NULL;
1331 const char *desc = NULL;
1332 uint32 max_entries, max_size;
1333 uint32_t total_size, returned_size;
1334 union samr_DispInfo info;
1336 get_query_dispinfo_params(
1337 loop_count, &max_entries, &max_size);
1339 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
1349 start_idx += info.info1.count;
1350 num_entries = info.info1.count;
1352 for (i = 0; i < num_entries; i++) {
1353 user = info.info1.entries[i].account_name.string;
1354 if (opt_long_list_entries)
1355 desc = info.info1.entries[i].description.string;
1356 if (opt_long_list_entries)
1357 printf("%-21.21s %s\n", user, desc);
1359 printf("%s\n", user);
1361 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1368 * 'net rpc user' entrypoint.
1369 * @param argc Standard main() style argc
1370 * @param argc Standard main() style argv. Initial components are already
1374 int net_rpc_user(int argc, const char **argv)
1376 struct functable func[] = {
1377 {"add", rpc_user_add},
1378 {"info", rpc_user_info},
1379 {"delete", rpc_user_delete},
1380 {"password", rpc_user_password},
1381 {"rename", rpc_user_rename},
1386 return run_rpc_command(NULL,PI_SAMR, 0,
1387 rpc_user_list_internals,
1391 return net_run_function(argc, argv, func, rpc_user_usage);
1394 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1395 struct rpc_sh_ctx *ctx,
1396 struct rpc_pipe_client *pipe_hnd,
1397 int argc, const char **argv)
1399 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1400 ctx->cli, pipe_hnd, mem_ctx,
1404 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1405 struct rpc_sh_ctx *ctx,
1406 struct rpc_pipe_client *pipe_hnd,
1407 int argc, const char **argv)
1409 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1410 ctx->cli, pipe_hnd, mem_ctx,
1414 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1415 struct rpc_sh_ctx *ctx,
1416 struct rpc_pipe_client *pipe_hnd,
1417 int argc, const char **argv,
1419 TALLOC_CTX *mem_ctx,
1420 struct rpc_sh_ctx *ctx,
1421 struct rpc_pipe_client *pipe_hnd,
1422 const POLICY_HND *user_hnd,
1423 int argc, const char **argv))
1426 POLICY_HND connect_pol, domain_pol, user_pol;
1427 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1430 enum lsa_SidType type;
1433 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1434 return NT_STATUS_INVALID_PARAMETER;
1437 ZERO_STRUCT(connect_pol);
1438 ZERO_STRUCT(domain_pol);
1439 ZERO_STRUCT(user_pol);
1441 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1442 NULL, NULL, &sid, &type);
1443 if (!NT_STATUS_IS_OK(result)) {
1444 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1449 if (type != SID_NAME_USER) {
1450 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1451 sid_type_lookup(type));
1452 result = NT_STATUS_NO_SUCH_USER;
1456 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1457 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1458 result = NT_STATUS_NO_SUCH_USER;
1462 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1463 pipe_hnd->cli->desthost,
1464 MAXIMUM_ALLOWED_ACCESS,
1466 if (!NT_STATUS_IS_OK(result)) {
1470 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1472 MAXIMUM_ALLOWED_ACCESS,
1475 if (!NT_STATUS_IS_OK(result)) {
1479 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1481 MAXIMUM_ALLOWED_ACCESS,
1484 if (!NT_STATUS_IS_OK(result)) {
1488 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1491 if (is_valid_policy_hnd(&user_pol)) {
1492 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1494 if (is_valid_policy_hnd(&domain_pol)) {
1495 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1497 if (is_valid_policy_hnd(&connect_pol)) {
1498 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1503 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1504 struct rpc_sh_ctx *ctx,
1505 struct rpc_pipe_client *pipe_hnd,
1506 const POLICY_HND *user_hnd,
1507 int argc, const char **argv)
1510 SAM_USERINFO_CTR *ctr;
1511 SAM_USER_INFO_21 *info;
1514 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1515 return NT_STATUS_INVALID_PARAMETER;
1518 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1520 if (!NT_STATUS_IS_OK(result)) {
1524 info = ctr->info.id21;
1526 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1532 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1533 struct rpc_sh_ctx *ctx,
1534 struct rpc_pipe_client *pipe_hnd,
1535 int argc, const char **argv)
1537 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1538 rpc_sh_user_show_internals);
1541 #define FETCHSTR(name, rec) \
1542 do { if (strequal(ctx->thiscmd, name)) { \
1543 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1546 #define SETSTR(name, rec, flag) \
1547 do { if (strequal(ctx->thiscmd, name)) { \
1548 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1549 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1550 usr->fields_present |= SAMR_FIELD_##flag; } \
1553 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1554 struct rpc_sh_ctx *ctx,
1555 struct rpc_pipe_client *pipe_hnd,
1556 const POLICY_HND *user_hnd,
1557 int argc, const char **argv)
1560 SAM_USERINFO_CTR *ctr;
1561 SAM_USER_INFO_21 *usr;
1562 const char *username;
1563 const char *oldval = "";
1566 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1568 return NT_STATUS_INVALID_PARAMETER;
1571 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1573 if (!NT_STATUS_IS_OK(result)) {
1577 usr = ctr->info.id21;
1579 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1581 FETCHSTR("fullname", full_name);
1582 FETCHSTR("homedir", home_dir);
1583 FETCHSTR("homedrive", dir_drive);
1584 FETCHSTR("logonscript", logon_script);
1585 FETCHSTR("profilepath", profile_path);
1586 FETCHSTR("description", acct_desc);
1589 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1595 if (strcmp(argv[0], "NULL") == 0) {
1599 SETSTR("fullname", full_name, FULL_NAME);
1600 SETSTR("homedir", home_dir, HOME_DIRECTORY);
1601 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1602 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1603 SETSTR("profilepath", profile_path, PROFILE_PATH);
1604 SETSTR("description", acct_desc, DESCRIPTION);
1606 result = rpccli_samr_set_userinfo2(
1607 pipe_hnd, mem_ctx, user_hnd, 21,
1608 &pipe_hnd->cli->user_session_key, ctr);
1610 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1611 ctx->thiscmd, oldval, argv[0]);
1618 #define HANDLEFLG(name, rec) \
1619 do { if (strequal(ctx->thiscmd, name)) { \
1620 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1622 newflags = oldflags | ACB_##rec; \
1624 newflags = oldflags & ~ACB_##rec; \
1627 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1628 struct rpc_sh_ctx *ctx,
1629 struct rpc_pipe_client *pipe_hnd,
1630 int argc, const char **argv)
1632 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1633 rpc_sh_user_str_edit_internals);
1636 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1637 struct rpc_sh_ctx *ctx,
1638 struct rpc_pipe_client *pipe_hnd,
1639 const POLICY_HND *user_hnd,
1640 int argc, const char **argv)
1643 SAM_USERINFO_CTR *ctr;
1644 SAM_USER_INFO_21 *usr;
1645 const char *username;
1646 const char *oldval = "unknown";
1647 uint32 oldflags, newflags;
1651 ((argc == 1) && !strequal(argv[0], "yes") &&
1652 !strequal(argv[0], "no"))) {
1653 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1655 return NT_STATUS_INVALID_PARAMETER;
1658 newval = strequal(argv[0], "yes");
1660 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1662 if (!NT_STATUS_IS_OK(result)) {
1666 usr = ctr->info.id21;
1668 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1669 oldflags = usr->acb_info;
1670 newflags = usr->acb_info;
1672 HANDLEFLG("disabled", DISABLED);
1673 HANDLEFLG("pwnotreq", PWNOTREQ);
1674 HANDLEFLG("autolock", AUTOLOCK);
1675 HANDLEFLG("pwnoexp", PWNOEXP);
1678 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1684 usr->acb_info = newflags;
1685 usr->fields_present = SAMR_FIELD_ACCT_FLAGS;
1687 result = rpccli_samr_set_userinfo2(
1688 pipe_hnd, mem_ctx, user_hnd, 21,
1689 &pipe_hnd->cli->user_session_key, ctr);
1691 if (NT_STATUS_IS_OK(result)) {
1692 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1693 ctx->thiscmd, oldval, argv[0]);
1701 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1702 struct rpc_sh_ctx *ctx,
1703 struct rpc_pipe_client *pipe_hnd,
1704 int argc, const char **argv)
1706 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1707 rpc_sh_user_flag_edit_internals);
1710 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1711 struct rpc_sh_ctx *ctx)
1713 static struct rpc_sh_cmd cmds[] = {
1715 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1716 "Show/Set a user's full name" },
1718 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1719 "Show/Set a user's home directory" },
1721 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1722 "Show/Set a user's home drive" },
1724 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1725 "Show/Set a user's logon script" },
1727 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1728 "Show/Set a user's profile path" },
1730 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1731 "Show/Set a user's description" },
1733 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1734 "Show/Set whether a user is disabled" },
1736 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1737 "Show/Set whether a user locked out" },
1739 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1740 "Show/Set whether a user does not need a password" },
1742 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1743 "Show/Set whether a user's password does not expire" },
1745 { NULL, NULL, 0, NULL, NULL }
1751 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1752 struct rpc_sh_ctx *ctx)
1754 static struct rpc_sh_cmd cmds[] = {
1756 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1757 "List available users" },
1759 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1760 "List the domain groups a user is member of" },
1762 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1763 "Show info about a user" },
1765 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1766 "Show/Modify a user's fields" },
1768 { NULL, NULL, 0, NULL, NULL }
1774 /****************************************************************************/
1777 * Basic usage function for 'net rpc group'
1778 * @param argc Standard main() style argc.
1779 * @param argv Standard main() style argv. Initial components are already
1783 static int rpc_group_usage(int argc, const char **argv)
1785 return net_help_group(argc, argv);
1789 * Delete group on a remote RPC server
1791 * All parameters are provided by the run_rpc_command function, except for
1792 * argc, argv which are passes through.
1794 * @param domain_sid The domain sid acquired from the remote server
1795 * @param cli A cli_state connected to the server.
1796 * @param mem_ctx Talloc context, destoyed on completion of the function.
1797 * @param argc Standard main() style argc
1798 * @param argv Standard main() style argv. Initial components are already
1801 * @return Normal NTSTATUS return.
1804 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1805 const char *domain_name,
1806 struct cli_state *cli,
1807 struct rpc_pipe_client *pipe_hnd,
1808 TALLOC_CTX *mem_ctx,
1812 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1813 bool group_is_primary = False;
1814 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1816 struct samr_RidTypeArray *rids = NULL;
1819 /* DOM_GID *user_gids; */
1820 SAM_USERINFO_CTR *user_ctr;
1823 struct samr_Ids group_rids, name_types;
1824 struct lsa_String lsa_acct_name;
1828 d_printf("specify group\n");
1829 rpc_group_usage(argc,argv);
1830 return NT_STATUS_OK; /* ok? */
1833 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1834 pipe_hnd->cli->desthost,
1835 MAXIMUM_ALLOWED_ACCESS,
1838 if (!NT_STATUS_IS_OK(result)) {
1839 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1843 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1845 MAXIMUM_ALLOWED_ACCESS,
1846 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1849 if (!NT_STATUS_IS_OK(result)) {
1850 d_fprintf(stderr, "Request open_domain failed\n");
1854 init_lsa_String(&lsa_acct_name, argv[0]);
1856 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1862 if (!NT_STATUS_IS_OK(result)) {
1863 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1867 switch (name_types.ids[0])
1869 case SID_NAME_DOM_GRP:
1870 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1872 MAXIMUM_ALLOWED_ACCESS,
1875 if (!NT_STATUS_IS_OK(result)) {
1876 d_fprintf(stderr, "Request open_group failed");
1880 group_rid = group_rids.ids[0];
1882 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1886 if (!NT_STATUS_IS_OK(result)) {
1887 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1892 d_printf("Domain Group %s (rid: %d) has %d members\n",
1893 argv[0],group_rid, rids->count);
1896 /* Check if group is anyone's primary group */
1897 for (i = 0; i < rids->count; i++)
1899 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1901 MAXIMUM_ALLOWED_ACCESS,
1905 if (!NT_STATUS_IS_OK(result)) {
1906 d_fprintf(stderr, "Unable to open group member %d\n",
1911 ZERO_STRUCT(user_ctr);
1913 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1916 if (!NT_STATUS_IS_OK(result)) {
1917 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1922 if (user_ctr->info.id21->group_rid == group_rid) {
1923 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1926 d_printf("Group is primary group of %s\n",temp);
1927 group_is_primary = True;
1930 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1933 if (group_is_primary) {
1934 d_fprintf(stderr, "Unable to delete group because some "
1935 "of it's members have it as primary group\n");
1936 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1940 /* remove all group members */
1941 for (i = 0; i < rids->count; i++)
1944 d_printf("Remove group member %d...",
1946 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1950 if (NT_STATUS_IS_OK(result)) {
1955 d_printf("failed\n");
1960 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1964 /* removing a local group is easier... */
1965 case SID_NAME_ALIAS:
1966 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1968 MAXIMUM_ALLOWED_ACCESS,
1972 if (!NT_STATUS_IS_OK(result)) {
1973 d_fprintf(stderr, "Request open_alias failed\n");
1977 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1981 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1982 argv[0],sid_type_lookup(name_types.ids[0]));
1983 result = NT_STATUS_UNSUCCESSFUL;
1988 if (NT_STATUS_IS_OK(result)) {
1990 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1992 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1993 get_friendly_nt_error_msg(result));
2001 static int rpc_group_delete(int argc, const char **argv)
2003 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
2007 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
2008 const char *domain_name,
2009 struct cli_state *cli,
2010 struct rpc_pipe_client *pipe_hnd,
2011 TALLOC_CTX *mem_ctx,
2015 POLICY_HND connect_pol, domain_pol, group_pol;
2016 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2017 union samr_GroupInfo group_info;
2018 struct lsa_String grp_name;
2022 d_printf("Group name must be specified\n");
2023 rpc_group_usage(argc, argv);
2024 return NT_STATUS_OK;
2027 init_lsa_String(&grp_name, argv[0]);
2029 /* Get sam policy handle */
2031 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2032 pipe_hnd->cli->desthost,
2033 MAXIMUM_ALLOWED_ACCESS,
2035 if (!NT_STATUS_IS_OK(result)) goto done;
2037 /* Get domain policy handle */
2039 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2041 MAXIMUM_ALLOWED_ACCESS,
2042 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2044 if (!NT_STATUS_IS_OK(result)) goto done;
2046 /* Create the group */
2048 result = rpccli_samr_CreateDomainGroup(pipe_hnd, mem_ctx,
2051 MAXIMUM_ALLOWED_ACCESS,
2054 if (!NT_STATUS_IS_OK(result)) goto done;
2056 if (strlen(opt_comment) == 0) goto done;
2058 /* We've got a comment to set */
2060 init_lsa_String(&group_info.description, opt_comment);
2062 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
2066 if (!NT_STATUS_IS_OK(result)) goto done;
2069 if (NT_STATUS_IS_OK(result))
2070 DEBUG(5, ("add group succeeded\n"));
2072 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
2077 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
2078 const char *domain_name,
2079 struct cli_state *cli,
2080 struct rpc_pipe_client *pipe_hnd,
2081 TALLOC_CTX *mem_ctx,
2085 POLICY_HND connect_pol, domain_pol, alias_pol;
2086 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2087 union samr_AliasInfo alias_info;
2088 struct lsa_String alias_name;
2092 d_printf("Alias name must be specified\n");
2093 rpc_group_usage(argc, argv);
2094 return NT_STATUS_OK;
2097 init_lsa_String(&alias_name, argv[0]);
2099 /* Get sam policy handle */
2101 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2102 pipe_hnd->cli->desthost,
2103 MAXIMUM_ALLOWED_ACCESS,
2105 if (!NT_STATUS_IS_OK(result)) goto done;
2107 /* Get domain policy handle */
2109 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2111 MAXIMUM_ALLOWED_ACCESS,
2112 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2114 if (!NT_STATUS_IS_OK(result)) goto done;
2116 /* Create the group */
2118 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
2121 MAXIMUM_ALLOWED_ACCESS,
2124 if (!NT_STATUS_IS_OK(result)) goto done;
2126 if (strlen(opt_comment) == 0) goto done;
2128 /* We've got a comment to set */
2130 init_lsa_String(&alias_info.description, opt_comment);
2132 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
2137 if (!NT_STATUS_IS_OK(result)) goto done;
2140 if (NT_STATUS_IS_OK(result))
2141 DEBUG(5, ("add alias succeeded\n"));
2143 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
2148 static int rpc_group_add(int argc, const char **argv)
2151 return run_rpc_command(NULL, PI_SAMR, 0,
2152 rpc_alias_add_internals,
2155 return run_rpc_command(NULL, PI_SAMR, 0,
2156 rpc_group_add_internals,
2160 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2161 TALLOC_CTX *mem_ctx,
2164 enum lsa_SidType *type)
2166 DOM_SID *sids = NULL;
2167 enum lsa_SidType *types = NULL;
2168 struct rpc_pipe_client *pipe_hnd;
2170 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2172 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2177 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2178 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2180 if (!NT_STATUS_IS_OK(result)) {
2184 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2185 &name, NULL, 1, &sids, &types);
2187 if (NT_STATUS_IS_OK(result)) {
2188 sid_copy(sid, &sids[0]);
2192 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2196 cli_rpc_pipe_close(pipe_hnd);
2199 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2201 /* Try as S-1-5-whatever */
2205 if (string_to_sid(&tmp_sid, name)) {
2206 sid_copy(sid, &tmp_sid);
2207 *type = SID_NAME_UNKNOWN;
2208 result = NT_STATUS_OK;
2215 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2216 TALLOC_CTX *mem_ctx,
2217 const DOM_SID *group_sid,
2220 POLICY_HND connect_pol, domain_pol;
2223 POLICY_HND group_pol;
2225 struct samr_Ids rids, rid_types;
2226 struct lsa_String lsa_acct_name;
2230 sid_copy(&sid, group_sid);
2232 if (!sid_split_rid(&sid, &group_rid)) {
2233 return NT_STATUS_UNSUCCESSFUL;
2236 /* Get sam policy handle */
2237 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2238 pipe_hnd->cli->desthost,
2239 MAXIMUM_ALLOWED_ACCESS,
2241 if (!NT_STATUS_IS_OK(result)) {
2245 /* Get domain policy handle */
2246 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2248 MAXIMUM_ALLOWED_ACCESS,
2251 if (!NT_STATUS_IS_OK(result)) {
2255 init_lsa_String(&lsa_acct_name, member);
2257 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2264 if (!NT_STATUS_IS_OK(result)) {
2265 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2269 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2271 MAXIMUM_ALLOWED_ACCESS,
2275 if (!NT_STATUS_IS_OK(result)) {
2279 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2282 0x0005); /* unknown flags */
2285 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2289 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2290 TALLOC_CTX *mem_ctx,
2291 const DOM_SID *alias_sid,
2294 POLICY_HND connect_pol, domain_pol;
2297 POLICY_HND alias_pol;
2300 enum lsa_SidType member_type;
2304 sid_copy(&sid, alias_sid);
2306 if (!sid_split_rid(&sid, &alias_rid)) {
2307 return NT_STATUS_UNSUCCESSFUL;
2310 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2311 &member_sid, &member_type);
2313 if (!NT_STATUS_IS_OK(result)) {
2314 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2318 /* Get sam policy handle */
2319 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2320 pipe_hnd->cli->desthost,
2321 MAXIMUM_ALLOWED_ACCESS,
2323 if (!NT_STATUS_IS_OK(result)) {
2327 /* Get domain policy handle */
2328 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2330 MAXIMUM_ALLOWED_ACCESS,
2333 if (!NT_STATUS_IS_OK(result)) {
2337 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2339 MAXIMUM_ALLOWED_ACCESS,
2343 if (!NT_STATUS_IS_OK(result)) {
2347 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2351 if (!NT_STATUS_IS_OK(result)) {
2356 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2360 static NTSTATUS rpc_group_addmem_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 lsa_SidType group_type;
2372 d_printf("Usage: 'net rpc group addmem <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_add_groupmem(pipe_hnd, mem_ctx,
2384 &group_sid, argv[1]);
2386 if (!NT_STATUS_IS_OK(result)) {
2387 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2388 argv[1], argv[0], nt_errstr(result));
2393 if (group_type == SID_NAME_ALIAS) {
2394 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2395 &group_sid, argv[1]);
2397 if (!NT_STATUS_IS_OK(result)) {
2398 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2399 argv[1], argv[0], nt_errstr(result));
2404 d_fprintf(stderr, "Can only add members to global or local groups "
2405 "which %s is not\n", argv[0]);
2407 return NT_STATUS_UNSUCCESSFUL;
2410 static int rpc_group_addmem(int argc, const char **argv)
2412 return run_rpc_command(NULL, PI_SAMR, 0,
2413 rpc_group_addmem_internals,
2417 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2418 TALLOC_CTX *mem_ctx,
2419 const DOM_SID *group_sid,
2422 POLICY_HND connect_pol, domain_pol;
2425 POLICY_HND group_pol;
2427 struct samr_Ids rids, rid_types;
2428 struct lsa_String lsa_acct_name;
2432 sid_copy(&sid, group_sid);
2434 if (!sid_split_rid(&sid, &group_rid))
2435 return NT_STATUS_UNSUCCESSFUL;
2437 /* Get sam policy handle */
2438 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2439 pipe_hnd->cli->desthost,
2440 MAXIMUM_ALLOWED_ACCESS,
2442 if (!NT_STATUS_IS_OK(result))
2445 /* Get domain policy handle */
2446 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2448 MAXIMUM_ALLOWED_ACCESS,
2451 if (!NT_STATUS_IS_OK(result))
2454 init_lsa_String(&lsa_acct_name, member);
2456 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2462 if (!NT_STATUS_IS_OK(result)) {
2463 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2467 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2469 MAXIMUM_ALLOWED_ACCESS,
2473 if (!NT_STATUS_IS_OK(result))
2476 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2481 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2485 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2486 TALLOC_CTX *mem_ctx,
2487 const DOM_SID *alias_sid,
2490 POLICY_HND connect_pol, domain_pol;
2493 POLICY_HND alias_pol;
2496 enum lsa_SidType member_type;
2500 sid_copy(&sid, alias_sid);
2502 if (!sid_split_rid(&sid, &alias_rid))
2503 return NT_STATUS_UNSUCCESSFUL;
2505 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2506 &member_sid, &member_type);
2508 if (!NT_STATUS_IS_OK(result)) {
2509 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2513 /* Get sam policy handle */
2514 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2515 pipe_hnd->cli->desthost,
2516 MAXIMUM_ALLOWED_ACCESS,
2518 if (!NT_STATUS_IS_OK(result)) {
2522 /* Get domain policy handle */
2523 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2525 MAXIMUM_ALLOWED_ACCESS,
2528 if (!NT_STATUS_IS_OK(result)) {
2532 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2534 MAXIMUM_ALLOWED_ACCESS,
2538 if (!NT_STATUS_IS_OK(result))
2541 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2545 if (!NT_STATUS_IS_OK(result))
2549 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2553 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2554 const char *domain_name,
2555 struct cli_state *cli,
2556 struct rpc_pipe_client *pipe_hnd,
2557 TALLOC_CTX *mem_ctx,
2562 enum lsa_SidType group_type;
2565 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2566 return NT_STATUS_UNSUCCESSFUL;
2569 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2570 &group_sid, &group_type))) {
2571 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2572 return NT_STATUS_UNSUCCESSFUL;
2575 if (group_type == SID_NAME_DOM_GRP) {
2576 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2577 &group_sid, argv[1]);
2579 if (!NT_STATUS_IS_OK(result)) {
2580 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2581 argv[1], argv[0], nt_errstr(result));
2586 if (group_type == SID_NAME_ALIAS) {
2587 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2588 &group_sid, argv[1]);
2590 if (!NT_STATUS_IS_OK(result)) {
2591 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2592 argv[1], argv[0], nt_errstr(result));
2597 d_fprintf(stderr, "Can only delete members from global or local groups "
2598 "which %s is not\n", argv[0]);
2600 return NT_STATUS_UNSUCCESSFUL;
2603 static int rpc_group_delmem(int argc, const char **argv)
2605 return run_rpc_command(NULL, PI_SAMR, 0,
2606 rpc_group_delmem_internals,
2611 * List groups on a remote RPC server
2613 * All parameters are provided by the run_rpc_command function, except for
2614 * argc, argv which are passes through.
2616 * @param domain_sid The domain sid acquired from the remote server
2617 * @param cli A cli_state connected to the server.
2618 * @param mem_ctx Talloc context, destoyed on completion of the function.
2619 * @param argc Standard main() style argc
2620 * @param argv Standard main() style argv. Initial components are already
2623 * @return Normal NTSTATUS return.
2626 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2627 const char *domain_name,
2628 struct cli_state *cli,
2629 struct rpc_pipe_client *pipe_hnd,
2630 TALLOC_CTX *mem_ctx,
2634 POLICY_HND connect_pol, domain_pol;
2635 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2636 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2637 struct acct_info *groups;
2638 bool global = False;
2640 bool builtin = False;
2648 for (i=0; i<argc; i++) {
2649 if (strequal(argv[i], "global"))
2652 if (strequal(argv[i], "local"))
2655 if (strequal(argv[i], "builtin"))
2659 /* Get sam policy handle */
2661 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2662 pipe_hnd->cli->desthost,
2663 MAXIMUM_ALLOWED_ACCESS,
2665 if (!NT_STATUS_IS_OK(result)) {
2669 /* Get domain policy handle */
2671 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2673 MAXIMUM_ALLOWED_ACCESS,
2674 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2676 if (!NT_STATUS_IS_OK(result)) {
2680 /* Query domain groups */
2681 if (opt_long_list_entries)
2682 d_printf("\nGroup name Comment"\
2683 "\n-----------------------------\n");
2685 uint32_t max_size, total_size, returned_size;
2686 union samr_DispInfo info;
2690 get_query_dispinfo_params(
2691 loop_count, &max_entries, &max_size);
2693 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2702 num_entries = info.info3.count;
2703 start_idx += info.info3.count;
2705 if (!NT_STATUS_IS_OK(result) &&
2706 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2709 for (i = 0; i < num_entries; i++) {
2711 const char *group = NULL;
2712 const char *desc = NULL;
2714 group = info.info3.entries[i].account_name.string;
2715 desc = info.info3.entries[i].description.string;
2717 if (opt_long_list_entries)
2718 printf("%-21.21s %-50.50s\n",
2721 printf("%s\n", group);
2723 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2724 /* query domain aliases */
2729 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2731 &groups, &num_entries);
2733 if (!NT_STATUS_IS_OK(result) &&
2734 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2737 for (i = 0; i < num_entries; i++) {
2739 const char *description = NULL;
2741 if (opt_long_list_entries) {
2743 POLICY_HND alias_pol;
2744 union samr_AliasInfo *info = NULL;
2746 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2751 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2755 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2757 description = info->description.string;
2761 if (description != NULL) {
2762 printf("%-21.21s %-50.50s\n",
2763 groups[i].acct_name,
2766 printf("%s\n", groups[i].acct_name);
2769 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2770 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2771 /* Get builtin policy handle */
2773 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2775 MAXIMUM_ALLOWED_ACCESS,
2776 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2778 if (!NT_STATUS_IS_OK(result)) {
2781 /* query builtin aliases */
2784 if (!builtin) break;
2786 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2787 &start_idx, max_entries,
2788 &groups, &num_entries);
2790 if (!NT_STATUS_IS_OK(result) &&
2791 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2794 for (i = 0; i < num_entries; i++) {
2796 const char *description = NULL;
2798 if (opt_long_list_entries) {
2800 POLICY_HND alias_pol;
2801 union samr_AliasInfo *info = NULL;
2803 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2808 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2812 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2814 description = info->description.string;
2818 if (description != NULL) {
2819 printf("%-21.21s %-50.50s\n",
2820 groups[i].acct_name,
2823 printf("%s\n", groups[i].acct_name);
2826 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2832 static int rpc_group_list(int argc, const char **argv)
2834 return run_rpc_command(NULL, PI_SAMR, 0,
2835 rpc_group_list_internals,
2839 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2840 TALLOC_CTX *mem_ctx,
2841 const char *domain_name,
2842 const DOM_SID *domain_sid,
2843 POLICY_HND *domain_pol,
2847 POLICY_HND group_pol;
2848 uint32 num_members, *group_rids;
2850 struct samr_RidTypeArray *rids = NULL;
2851 struct lsa_Strings names;
2852 struct samr_Ids types;
2855 sid_to_fstring(sid_str, domain_sid);
2857 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2859 MAXIMUM_ALLOWED_ACCESS,
2863 if (!NT_STATUS_IS_OK(result))
2866 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2870 if (!NT_STATUS_IS_OK(result))
2873 num_members = rids->count;
2874 group_rids = rids->rids;
2876 while (num_members > 0) {
2877 int this_time = 512;
2879 if (num_members < this_time)
2880 this_time = num_members;
2882 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2889 if (!NT_STATUS_IS_OK(result))
2892 /* We only have users as members, but make the output
2893 the same as the output of alias members */
2895 for (i = 0; i < this_time; i++) {
2897 if (opt_long_list_entries) {
2898 printf("%s-%d %s\\%s %d\n", sid_str,
2899 group_rids[i], domain_name,
2900 names.names[i].string,
2903 printf("%s\\%s\n", domain_name,
2904 names.names[i].string);
2908 num_members -= this_time;
2912 return NT_STATUS_OK;
2915 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2916 TALLOC_CTX *mem_ctx,
2917 POLICY_HND *domain_pol,
2921 struct rpc_pipe_client *lsa_pipe;
2922 POLICY_HND alias_pol, lsa_pol;
2924 DOM_SID *alias_sids;
2927 enum lsa_SidType *types;
2929 struct lsa_SidArray sid_array;
2931 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2933 MAXIMUM_ALLOWED_ACCESS,
2937 if (!NT_STATUS_IS_OK(result))
2940 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2944 if (!NT_STATUS_IS_OK(result)) {
2945 d_fprintf(stderr, "Couldn't list alias members\n");
2949 num_members = sid_array.num_sids;
2951 if (num_members == 0) {
2952 return NT_STATUS_OK;
2955 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2957 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2958 nt_errstr(result) );
2962 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2963 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2965 if (!NT_STATUS_IS_OK(result)) {
2966 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2967 cli_rpc_pipe_close(lsa_pipe);
2971 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2973 d_fprintf(stderr, "Out of memory\n");
2974 cli_rpc_pipe_close(lsa_pipe);
2975 return NT_STATUS_NO_MEMORY;
2978 for (i=0; i<num_members; i++) {
2979 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2982 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2984 &domains, &names, &types);
2986 if (!NT_STATUS_IS_OK(result) &&
2987 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2988 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2989 cli_rpc_pipe_close(lsa_pipe);
2993 for (i = 0; i < num_members; i++) {
2995 sid_to_fstring(sid_str, &alias_sids[i]);
2997 if (opt_long_list_entries) {
2998 printf("%s %s\\%s %d\n", sid_str,
2999 domains[i] ? domains[i] : "*unknown*",
3000 names[i] ? names[i] : "*unknown*", types[i]);
3003 printf("%s\\%s\n", domains[i], names[i]);
3005 printf("%s\n", sid_str);
3009 cli_rpc_pipe_close(lsa_pipe);
3010 return NT_STATUS_OK;
3013 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
3014 const char *domain_name,
3015 struct cli_state *cli,
3016 struct rpc_pipe_client *pipe_hnd,
3017 TALLOC_CTX *mem_ctx,
3022 POLICY_HND connect_pol, domain_pol;
3023 struct samr_Ids rids, rid_types;
3024 struct lsa_String lsa_acct_name;
3026 /* Get sam policy handle */
3028 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3029 pipe_hnd->cli->desthost,
3030 MAXIMUM_ALLOWED_ACCESS,
3033 if (!NT_STATUS_IS_OK(result))
3036 /* Get domain policy handle */
3038 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3040 MAXIMUM_ALLOWED_ACCESS,
3041 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3044 if (!NT_STATUS_IS_OK(result))
3047 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3049 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
3056 if (!NT_STATUS_IS_OK(result)) {
3058 /* Ok, did not find it in the global sam, try with builtin */
3060 DOM_SID sid_Builtin;
3062 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3064 string_to_sid(&sid_Builtin, "S-1-5-32");
3066 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3068 MAXIMUM_ALLOWED_ACCESS,
3072 if (!NT_STATUS_IS_OK(result)) {
3073 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3077 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
3084 if (!NT_STATUS_IS_OK(result)) {
3085 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3090 if (rids.count != 1) {
3091 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3095 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3096 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
3097 domain_sid, &domain_pol,
3101 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3102 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
3106 return NT_STATUS_NO_SUCH_GROUP;
3109 static int rpc_group_members(int argc, const char **argv)
3112 return rpc_group_usage(argc, argv);
3115 return run_rpc_command(NULL, PI_SAMR, 0,
3116 rpc_group_members_internals,
3120 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
3121 const char *domain_name,
3122 struct cli_state *cli,
3123 struct rpc_pipe_client *pipe_hnd,
3124 TALLOC_CTX *mem_ctx,
3129 POLICY_HND connect_pol, domain_pol, group_pol;
3130 union samr_GroupInfo group_info;
3131 struct samr_Ids rids, rid_types;
3132 struct lsa_String lsa_acct_name;
3135 d_printf("Usage: 'net rpc group rename group newname'\n");
3136 return NT_STATUS_UNSUCCESSFUL;
3139 /* Get sam policy handle */
3141 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3142 pipe_hnd->cli->desthost,
3143 MAXIMUM_ALLOWED_ACCESS,
3146 if (!NT_STATUS_IS_OK(result))
3149 /* Get domain policy handle */
3151 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3153 MAXIMUM_ALLOWED_ACCESS,
3154 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3157 if (!NT_STATUS_IS_OK(result))
3160 init_lsa_String(&lsa_acct_name, argv[0]);
3162 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
3169 if (rids.count != 1) {
3170 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3174 if (rid_types.ids[0] != SID_NAME_DOM_GRP) {
3175 d_fprintf(stderr, "Can only rename domain groups\n");
3176 return NT_STATUS_UNSUCCESSFUL;
3179 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
3181 MAXIMUM_ALLOWED_ACCESS,
3185 if (!NT_STATUS_IS_OK(result))
3188 init_lsa_String(&group_info.name, argv[1]);
3190 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
3195 if (!NT_STATUS_IS_OK(result))
3198 return NT_STATUS_NO_SUCH_GROUP;
3201 static int rpc_group_rename(int argc, const char **argv)
3204 return rpc_group_usage(argc, argv);
3207 return run_rpc_command(NULL, PI_SAMR, 0,
3208 rpc_group_rename_internals,
3213 * 'net rpc group' entrypoint.
3214 * @param argc Standard main() style argc
3215 * @param argc Standard main() style argv. Initial components are already
3219 int net_rpc_group(int argc, const char **argv)
3221 struct functable func[] = {
3222 {"add", rpc_group_add},
3223 {"delete", rpc_group_delete},
3224 {"addmem", rpc_group_addmem},
3225 {"delmem", rpc_group_delmem},
3226 {"list", rpc_group_list},
3227 {"members", rpc_group_members},
3228 {"rename", rpc_group_rename},
3233 return run_rpc_command(NULL, PI_SAMR, 0,
3234 rpc_group_list_internals,
3238 return net_run_function(argc, argv, func, rpc_group_usage);
3241 /****************************************************************************/
3243 static int rpc_share_usage(int argc, const char **argv)
3245 return net_help_share(argc, argv);
3249 * Add a share on a remote RPC server
3251 * All parameters are provided by the run_rpc_command function, except for
3252 * argc, argv which are passes through.
3254 * @param domain_sid The domain sid acquired from the remote server
3255 * @param cli A cli_state connected to the server.
3256 * @param mem_ctx Talloc context, destoyed on completion of the function.
3257 * @param argc Standard main() style argc
3258 * @param argv Standard main() style argv. Initial components are already
3261 * @return Normal NTSTATUS return.
3263 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3264 const char *domain_name,
3265 struct cli_state *cli,
3266 struct rpc_pipe_client *pipe_hnd,
3267 TALLOC_CTX *mem_ctx,int argc,
3273 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3274 uint32 num_users=0, perms=0;
3275 char *password=NULL; /* don't allow a share password */
3278 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3279 return NT_STATUS_NO_MEMORY;
3282 path = strchr(sharename, '=');
3284 return NT_STATUS_UNSUCCESSFUL;
3287 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
3288 opt_comment, perms, opt_maxusers,
3289 num_users, path, password,
3291 return werror_to_ntstatus(result);
3294 static int rpc_share_add(int argc, const char **argv)
3296 if ((argc < 1) || !strchr(argv[0], '=')) {
3297 DEBUG(1,("Sharename or path not specified on add\n"));
3298 return rpc_share_usage(argc, argv);
3300 return run_rpc_command(NULL, PI_SRVSVC, 0,
3301 rpc_share_add_internals,
3306 * Delete a share on a remote RPC server
3308 * All parameters are provided by the run_rpc_command function, except for
3309 * argc, argv which are passes through.
3311 * @param domain_sid The domain sid acquired from the remote server
3312 * @param cli A cli_state connected to the server.
3313 * @param mem_ctx Talloc context, destoyed on completion of the function.
3314 * @param argc Standard main() style argc
3315 * @param argv Standard main() style argv. Initial components are already
3318 * @return Normal NTSTATUS return.
3320 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3321 const char *domain_name,
3322 struct cli_state *cli,
3323 struct rpc_pipe_client *pipe_hnd,
3324 TALLOC_CTX *mem_ctx,
3330 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
3331 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3335 * Delete a share on a remote RPC server
3337 * @param domain_sid The domain sid acquired from the remote server
3338 * @param argc Standard main() style argc
3339 * @param argv Standard main() style argv. Initial components are already
3342 * @return A shell status integer (0 for success)
3344 static int rpc_share_delete(int argc, const char **argv)
3347 DEBUG(1,("Sharename not specified on delete\n"));
3348 return rpc_share_usage(argc, argv);
3350 return run_rpc_command(NULL, PI_SRVSVC, 0,
3351 rpc_share_del_internals,
3356 * Formatted print of share info
3358 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3361 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3363 fstring netname = "", remark = "";
3365 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3366 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3368 if (opt_long_list_entries) {
3369 d_printf("%-12s %-8.8s %-50s\n",
3370 netname, share_type[info1->info_1.type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)], remark);
3372 d_printf("%s\n", netname);
3377 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3378 TALLOC_CTX *mem_ctx,
3382 SRV_SHARE_INFO_CTR *ctr)
3385 SRV_SHARE_INFO info;
3387 /* no specific share requested, enumerate all */
3391 uint32 preferred_len = 0xffffffff;
3393 init_enum_hnd(&hnd, 0);
3395 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3396 preferred_len, &hnd);
3399 /* request just one share */
3400 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3402 if (!W_ERROR_IS_OK(result))
3408 ctr->info_level = ctr->switch_value = level;
3409 ctr->ptr_share_info = ctr->ptr_entries = 1;
3410 ctr->num_entries = ctr->num_entries2 = 1;
3416 SRV_SHARE_INFO_1 *info1;
3418 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3419 if (ctr->share.info1 == NULL) {
3420 result = WERR_NOMEM;
3423 info1 = ctr->share.info1;
3425 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3427 /* Copy pointer crap */
3429 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3431 /* Duplicate strings */
3433 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3435 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3437 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3439 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3444 SRV_SHARE_INFO_2 *info2;
3446 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3447 if (ctr->share.info2 == NULL) {
3448 result = WERR_NOMEM;
3451 info2 = ctr->share.info2;
3453 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3455 /* Copy pointer crap */
3457 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3459 /* Duplicate strings */
3461 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3463 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3465 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3467 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3469 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_path);
3471 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3473 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3475 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3480 SRV_SHARE_INFO_502 *info502;
3482 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3483 if (ctr->share.info502 == NULL) {
3484 result = WERR_NOMEM;
3487 info502 = ctr->share.info502;
3489 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3491 /* Copy pointer crap */
3493 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3495 /* Duplicate strings */
3497 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3499 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3501 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3503 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3505 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_path);
3507 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3509 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3511 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3513 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3524 * List shares on a remote RPC server
3526 * All parameters are provided by the run_rpc_command function, except for
3527 * argc, argv which are passes through.
3529 * @param domain_sid The domain sid acquired from the remote server
3530 * @param cli A cli_state connected to the server.
3531 * @param mem_ctx Talloc context, destoyed on completion of the function.
3532 * @param argc Standard main() style argc
3533 * @param argv Standard main() style argv. Initial components are already
3536 * @return Normal NTSTATUS return.
3539 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3540 const char *domain_name,
3541 struct cli_state *cli,
3542 struct rpc_pipe_client *pipe_hnd,
3543 TALLOC_CTX *mem_ctx,
3547 SRV_SHARE_INFO_CTR ctr;
3549 uint32 i, level = 1;
3551 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3552 if (!W_ERROR_IS_OK(result))
3555 /* Display results */
3557 if (opt_long_list_entries) {
3559 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3560 "\nShare name Type Description\n"\
3561 "---------- ---- -----------\n");
3563 for (i = 0; i < ctr.num_entries; i++)
3564 display_share_info_1(&ctr.share.info1[i]);
3566 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3570 * 'net rpc share list' entrypoint.
3571 * @param argc Standard main() style argc
3572 * @param argv Standard main() style argv. Initial components are already
3575 static int rpc_share_list(int argc, const char **argv)
3577 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3580 static bool check_share_availability(struct cli_state *cli, const char *netname)
3582 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3583 d_printf("skipping [%s]: not a file share.\n", netname);
3593 static bool check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3595 /* only support disk shares */
3596 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3597 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3601 /* skip builtin shares */
3602 /* FIXME: should print$ be added too ? */
3603 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3604 strequal(netname,"global"))
3607 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3608 printf("excluding [%s]\n", netname);
3612 return check_share_availability(cli, netname);
3616 * Migrate shares from a remote RPC server to the local RPC srever
3618 * All parameters are provided by the run_rpc_command function, except for
3619 * argc, argv which are passes through.
3621 * @param domain_sid The domain sid acquired from the remote server
3622 * @param cli A cli_state connected to the server.
3623 * @param mem_ctx Talloc context, destoyed on completion of the function.
3624 * @param argc Standard main() style argc
3625 * @param argv Standard main() style argv. Initial components are already
3628 * @return Normal NTSTATUS return.
3631 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3632 const char *domain_name,
3633 struct cli_state *cli,
3634 struct rpc_pipe_client *pipe_hnd,
3635 TALLOC_CTX *mem_ctx,
3640 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3641 SRV_SHARE_INFO_CTR ctr_src;
3642 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3643 char *password = NULL; /* don't allow a share password */
3645 struct rpc_pipe_client *srvsvc_pipe = NULL;
3646 struct cli_state *cli_dst = NULL;
3647 uint32 level = 502; /* includes secdesc */
3649 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3650 if (!W_ERROR_IS_OK(result))
3653 /* connect destination PI_SRVSVC */
3654 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3655 if (!NT_STATUS_IS_OK(nt_status))
3659 for (i = 0; i < ctr_src.num_entries; i++) {
3661 fstring netname = "", remark = "", path = "";
3662 /* reset error-code */
3663 nt_status = NT_STATUS_UNSUCCESSFUL;
3665 rpcstr_pull_unistr2_fstring(
3666 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3667 rpcstr_pull_unistr2_fstring(
3668 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3669 rpcstr_pull_unistr2_fstring(
3670 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3672 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3675 /* finally add the share on the dst server */
3677 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3678 netname, path, remark);
3680 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3681 ctr_src.share.info502[i].info_502.perms,
3682 ctr_src.share.info502[i].info_502.max_uses,
3683 ctr_src.share.info502[i].info_502.num_uses,
3684 path, password, level,
3687 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3688 printf(" [%s] does already exist\n", netname);
3692 if (!W_ERROR_IS_OK(result)) {
3693 printf("cannot add share: %s\n", dos_errstr(result));
3699 nt_status = NT_STATUS_OK;
3703 cli_shutdown(cli_dst);
3711 * Migrate shares from a rpc-server to another
3713 * @param argc Standard main() style argc
3714 * @param argv Standard main() style argv. Initial components are already
3717 * @return A shell status integer (0 for success)
3719 static int rpc_share_migrate_shares(int argc, const char **argv)
3723 printf("no server to migrate\n");
3727 return run_rpc_command(NULL, PI_SRVSVC, 0,
3728 rpc_share_migrate_shares_internals,
3735 * @param f file_info
3736 * @param mask current search mask
3737 * @param state arg-pointer
3740 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3742 static NTSTATUS nt_status;
3743 static struct copy_clistate *local_state;
3744 static fstring filename, new_mask;
3748 local_state = (struct copy_clistate *)state;
3749 nt_status = NT_STATUS_UNSUCCESSFUL;
3751 if (strequal(f->name, ".") || strequal(f->name, ".."))
3754 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3757 if (f->mode & aDIR) {
3759 DEBUG(3,("got dir: %s\n", f->name));
3761 fstrcpy(dir, local_state->cwd);
3763 fstrcat(dir, f->name);
3765 switch (net_mode_share)
3767 case NET_MODE_SHARE_MIGRATE:
3768 /* create that directory */
3769 nt_status = net_copy_file(local_state->mem_ctx,
3770 local_state->cli_share_src,
3771 local_state->cli_share_dst,
3773 opt_acls? True : False,
3774 opt_attrs? True : False,
3775 opt_timestamps? True : False,
3779 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3783 if (!NT_STATUS_IS_OK(nt_status))
3784 printf("could not handle dir %s: %s\n",
3785 dir, nt_errstr(nt_status));
3787 /* search below that directory */
3788 fstrcpy(new_mask, dir);
3789 fstrcat(new_mask, "\\*");
3791 old_dir = local_state->cwd;
3792 local_state->cwd = dir;
3793 if (!sync_files(local_state, new_mask))
3794 printf("could not handle files\n");
3795 local_state->cwd = old_dir;
3802 fstrcpy(filename, local_state->cwd);
3803 fstrcat(filename, "\\");
3804 fstrcat(filename, f->name);
3806 DEBUG(3,("got file: %s\n", filename));
3808 switch (net_mode_share)
3810 case NET_MODE_SHARE_MIGRATE:
3811 nt_status = net_copy_file(local_state->mem_ctx,
3812 local_state->cli_share_src,
3813 local_state->cli_share_dst,
3815 opt_acls? True : False,
3816 opt_attrs? True : False,
3817 opt_timestamps? True: False,
3821 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3825 if (!NT_STATUS_IS_OK(nt_status))
3826 printf("could not handle file %s: %s\n",
3827 filename, nt_errstr(nt_status));
3832 * sync files, can be called recursivly to list files
3833 * and then call copy_fn for each file
3835 * @param cp_clistate pointer to the copy_clistate we work with
3836 * @param mask the current search mask
3838 * @return Boolean result
3840 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3842 struct cli_state *targetcli;
3843 char *targetpath = NULL;
3845 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3847 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3848 mask, &targetcli, &targetpath ) ) {
3849 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3850 mask, cli_errstr(cp_clistate->cli_share_src));
3854 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3855 d_fprintf(stderr, "listing %s failed with error: %s\n",
3856 mask, cli_errstr(targetcli));
3865 * Set the top level directory permissions before we do any further copies.
3866 * Should set up ACL inheritance.
3869 bool copy_top_level_perms(struct copy_clistate *cp_clistate,
3870 const char *sharename)
3872 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3874 switch (net_mode_share) {
3875 case NET_MODE_SHARE_MIGRATE:
3876 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3877 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3878 cp_clistate->cli_share_src,
3879 cp_clistate->cli_share_dst,
3881 opt_acls? True : False,
3882 opt_attrs? True : False,
3883 opt_timestamps? True: False,
3887 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3891 if (!NT_STATUS_IS_OK(nt_status)) {
3892 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3893 sharename, nt_errstr(nt_status));
3901 * Sync all files inside a remote share to another share (over smb)
3903 * All parameters are provided by the run_rpc_command function, except for
3904 * argc, argv which are passes through.
3906 * @param domain_sid The domain sid acquired from the remote server
3907 * @param cli A cli_state connected to the server.
3908 * @param mem_ctx Talloc context, destoyed on completion of the function.
3909 * @param argc Standard main() style argc
3910 * @param argv Standard main() style argv. Initial components are already
3913 * @return Normal NTSTATUS return.
3916 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3917 const char *domain_name,
3918 struct cli_state *cli,
3919 struct rpc_pipe_client *pipe_hnd,
3920 TALLOC_CTX *mem_ctx,
3925 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3926 SRV_SHARE_INFO_CTR ctr_src;
3929 struct copy_clistate cp_clistate;
3930 bool got_src_share = False;
3931 bool got_dst_share = False;
3932 const char *mask = "\\*";
3935 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3937 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3939 if (!W_ERROR_IS_OK(result))
3942 for (i = 0; i < ctr_src.num_entries; i++) {
3944 fstring netname = "";
3946 rpcstr_pull_unistr2_fstring(
3947 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3949 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3952 /* one might not want to mirror whole discs :) */
3953 if (strequal(netname, "print$") || netname[1] == '$') {
3954 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3958 switch (net_mode_share)
3960 case NET_MODE_SHARE_MIGRATE:
3964 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3967 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3969 opt_acls ? "including" : "without",
3970 opt_attrs ? "including" : "without",
3971 opt_timestamps ? "(preserving timestamps)" : "");
3973 cp_clistate.mem_ctx = mem_ctx;
3974 cp_clistate.cli_share_src = NULL;
3975 cp_clistate.cli_share_dst = NULL;
3976 cp_clistate.cwd = NULL;
3977 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3979 /* open share source */
3980 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3981 &cli->dest_ss, cli->desthost,
3983 if (!NT_STATUS_IS_OK(nt_status))
3986 got_src_share = True;
3988 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3989 /* open share destination */
3990 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3991 NULL, dst, netname, "A:");
3992 if (!NT_STATUS_IS_OK(nt_status))
3995 got_dst_share = True;
3998 if (!copy_top_level_perms(&cp_clistate, netname)) {
3999 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
4000 nt_status = NT_STATUS_UNSUCCESSFUL;
4004 if (!sync_files(&cp_clistate, mask)) {
4005 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
4006 nt_status = NT_STATUS_UNSUCCESSFUL;
4011 nt_status = NT_STATUS_OK;
4016 cli_shutdown(cp_clistate.cli_share_src);
4019 cli_shutdown(cp_clistate.cli_share_dst);
4025 static int rpc_share_migrate_files(int argc, const char **argv)
4029 printf("no server to migrate\n");
4033 return run_rpc_command(NULL, PI_SRVSVC, 0,
4034 rpc_share_migrate_files_internals,
4039 * Migrate share-ACLs from a remote RPC server to the local RPC srever
4041 * All parameters are provided by the run_rpc_command function, except for
4042 * argc, argv which are passes through.
4044 * @param domain_sid The domain sid acquired from the remote server
4045 * @param cli A cli_state connected to the server.
4046 * @param mem_ctx Talloc context, destoyed on completion of the function.
4047 * @param argc Standard main() style argc
4048 * @param argv Standard main() style argv. Initial components are already
4051 * @return Normal NTSTATUS return.
4054 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
4055 const char *domain_name,
4056 struct cli_state *cli,
4057 struct rpc_pipe_client *pipe_hnd,
4058 TALLOC_CTX *mem_ctx,
4063 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4064 SRV_SHARE_INFO_CTR ctr_src;
4065 SRV_SHARE_INFO info;
4067 struct rpc_pipe_client *srvsvc_pipe = NULL;
4068 struct cli_state *cli_dst = NULL;
4069 uint32 level = 502; /* includes secdesc */
4071 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
4073 if (!W_ERROR_IS_OK(result))
4076 /* connect destination PI_SRVSVC */
4077 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
4078 if (!NT_STATUS_IS_OK(nt_status))
4082 for (i = 0; i < ctr_src.num_entries; i++) {
4084 fstring netname = "", remark = "", path = "";
4085 /* reset error-code */
4086 nt_status = NT_STATUS_UNSUCCESSFUL;
4088 rpcstr_pull_unistr2_fstring(
4089 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
4090 rpcstr_pull_unistr2_fstring(
4091 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
4092 rpcstr_pull_unistr2_fstring(
4093 path, &ctr_src.share.info502[i].info_502_str.uni_path);
4095 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
4098 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
4099 netname, path, remark);
4102 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
4107 info.switch_value = level;
4108 info.ptr_share_ctr = 1;
4110 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4111 info.share.info502 = ctr_src.share.info502[i];
4113 /* finally modify the share on the dst server */
4114 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
4116 if (!W_ERROR_IS_OK(result)) {
4117 printf("cannot set share-acl: %s\n", dos_errstr(result));
4123 nt_status = NT_STATUS_OK;
4127 cli_shutdown(cli_dst);
4135 * Migrate share-acls from a rpc-server to another
4137 * @param argc Standard main() style argc
4138 * @param argv Standard main() style argv. Initial components are already
4141 * @return A shell status integer (0 for success)
4143 static int rpc_share_migrate_security(int argc, const char **argv)
4147 printf("no server to migrate\n");
4151 return run_rpc_command(NULL, PI_SRVSVC, 0,
4152 rpc_share_migrate_security_internals,
4157 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4158 * from one server to another
4160 * @param argc Standard main() style argc
4161 * @param argv Standard main() style argv. Initial components are already
4164 * @return A shell status integer (0 for success)
4167 static int rpc_share_migrate_all(int argc, const char **argv)
4172 printf("no server to migrate\n");
4176 /* order is important. we don't want to be locked out by the share-acl
4177 * before copying files - gd */
4179 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
4183 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
4187 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
4192 * 'net rpc share migrate' entrypoint.
4193 * @param argc Standard main() style argc
4194 * @param argv Standard main() style argv. Initial components are already
4197 static int rpc_share_migrate(int argc, const char **argv)
4200 struct functable func[] = {
4201 {"all", rpc_share_migrate_all},
4202 {"files", rpc_share_migrate_files},
4203 {"help", rpc_share_usage},
4204 {"security", rpc_share_migrate_security},
4205 {"shares", rpc_share_migrate_shares},
4209 net_mode_share = NET_MODE_SHARE_MIGRATE;
4211 return net_run_function(argc, argv, func, rpc_share_usage);
4220 static int num_server_aliases;
4221 static struct full_alias *server_aliases;
4224 * Add an alias to the static list.
4226 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4228 if (server_aliases == NULL)
4229 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4231 server_aliases[num_server_aliases] = *alias;
4232 num_server_aliases += 1;
4236 * For a specific domain on the server, fetch all the aliases
4237 * and their members. Add all of them to the server_aliases.
4240 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4241 TALLOC_CTX *mem_ctx,
4242 POLICY_HND *connect_pol,
4243 const DOM_SID *domain_sid)
4245 uint32 start_idx, max_entries, num_entries, i;
4246 struct acct_info *groups;
4248 POLICY_HND domain_pol;
4250 /* Get domain policy handle */
4252 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4254 MAXIMUM_ALLOWED_ACCESS,
4255 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4257 if (!NT_STATUS_IS_OK(result))
4264 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
4265 &start_idx, max_entries,
4266 &groups, &num_entries);
4268 for (i = 0; i < num_entries; i++) {
4270 POLICY_HND alias_pol;
4271 struct full_alias alias;
4272 struct lsa_SidArray sid_array;
4275 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4277 MAXIMUM_ALLOWED_ACCESS,
4280 if (!NT_STATUS_IS_OK(result))
4283 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4286 if (!NT_STATUS_IS_OK(result))
4289 alias.num_members = sid_array.num_sids;
4291 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4292 if (!NT_STATUS_IS_OK(result))
4295 alias.members = NULL;
4297 if (alias.num_members > 0) {
4298 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4300 for (j = 0; j < alias.num_members; j++)
4301 sid_copy(&alias.members[j],
4302 sid_array.sids[j].sid);
4305 sid_copy(&alias.sid, domain_sid);
4306 sid_append_rid(&alias.sid, groups[i].rid);
4308 push_alias(mem_ctx, &alias);
4310 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4312 result = NT_STATUS_OK;
4315 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4321 * Dump server_aliases as names for debugging purposes.
4324 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
4325 const char *domain_name,
4326 struct cli_state *cli,
4327 struct rpc_pipe_client *pipe_hnd,
4328 TALLOC_CTX *mem_ctx,
4336 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
4337 SEC_RIGHTS_MAXIMUM_ALLOWED,
4339 if (!NT_STATUS_IS_OK(result))
4342 for (i=0; i<num_server_aliases; i++) {
4345 enum lsa_SidType *types;
4348 struct full_alias *alias = &server_aliases[i];
4350 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4352 &domains, &names, &types);
4353 if (!NT_STATUS_IS_OK(result))
4356 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4358 if (alias->num_members == 0) {
4363 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4366 &domains, &names, &types);
4368 if (!NT_STATUS_IS_OK(result) &&
4369 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4372 for (j=0; j<alias->num_members; j++)
4373 DEBUG(1, ("%s\\%s (%d); ",
4374 domains[j] ? domains[j] : "*unknown*",
4375 names[j] ? names[j] : "*unknown*",types[j]));
4379 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4381 return NT_STATUS_OK;
4385 * Fetch a list of all server aliases and their members into
4389 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4390 const char *domain_name,
4391 struct cli_state *cli,
4392 struct rpc_pipe_client *pipe_hnd,
4393 TALLOC_CTX *mem_ctx,
4398 POLICY_HND connect_pol;
4400 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4401 pipe_hnd->cli->desthost,
4402 MAXIMUM_ALLOWED_ACCESS,
4405 if (!NT_STATUS_IS_OK(result))
4408 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4409 &global_sid_Builtin);
4411 if (!NT_STATUS_IS_OK(result))
4414 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4417 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4422 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4424 token->num_sids = 4;
4426 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4427 d_fprintf(stderr, "malloc failed\n");
4428 token->num_sids = 0;
4432 token->user_sids[0] = *user_sid;
4433 sid_copy(&token->user_sids[1], &global_sid_World);
4434 sid_copy(&token->user_sids[2], &global_sid_Network);
4435 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4438 static void free_user_token(NT_USER_TOKEN *token)
4440 SAFE_FREE(token->user_sids);
4443 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4447 for (i=0; i<token->num_sids; i++) {
4448 if (sid_compare(sid, &token->user_sids[i]) == 0)
4454 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4456 if (is_sid_in_token(token, sid))
4459 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4460 if (!token->user_sids) {
4464 sid_copy(&token->user_sids[token->num_sids], sid);
4466 token->num_sids += 1;
4471 NT_USER_TOKEN token;
4474 static void dump_user_token(struct user_token *token)
4478 d_printf("%s\n", token->name);
4480 for (i=0; i<token->token.num_sids; i++) {
4481 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4485 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4489 for (i=0; i<alias->num_members; i++) {
4490 if (sid_compare(sid, &alias->members[i]) == 0)
4497 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4501 for (i=0; i<num_server_aliases; i++) {
4502 if (is_alias_member(&sid, &server_aliases[i]))
4503 add_sid_to_token(token, &server_aliases[i].sid);
4508 * We got a user token with all the SIDs we can know about without asking the
4509 * server directly. These are the user and domain group sids. All of these can
4510 * be members of aliases. So scan the list of aliases for each of the SIDs and
4511 * add them to the token.
4514 static void collect_alias_memberships(NT_USER_TOKEN *token)
4516 int num_global_sids = token->num_sids;
4519 for (i=0; i<num_global_sids; i++) {
4520 collect_sid_memberships(token, token->user_sids[i]);
4524 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4526 struct winbindd_request request;
4527 struct winbindd_response response;
4535 fstr_sprintf(full_name, "%s%c%s",
4536 domain, *lp_winbind_separator(), user);
4538 /* First let's find out the user sid */
4540 ZERO_STRUCT(request);
4541 ZERO_STRUCT(response);
4543 fstrcpy(request.data.name.dom_name, domain);
4544 fstrcpy(request.data.name.name, user);
4546 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4548 if (result != NSS_STATUS_SUCCESS) {
4549 DEBUG(1, ("winbind could not find %s\n", full_name));
4553 if (response.data.sid.type != SID_NAME_USER) {
4554 DEBUG(1, ("%s is not a user\n", full_name));
4558 string_to_sid(&user_sid, response.data.sid.sid);
4560 init_user_token(token, &user_sid);
4562 /* And now the groups winbind knows about */
4564 ZERO_STRUCT(response);
4566 fstrcpy(request.data.username, full_name);
4568 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4570 if (result != NSS_STATUS_SUCCESS) {
4571 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4575 for (i = 0; i < response.data.num_entries; i++) {
4576 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4579 struct winbindd_request sidrequest;
4580 struct winbindd_response sidresponse;
4582 ZERO_STRUCT(sidrequest);
4583 ZERO_STRUCT(sidresponse);
4585 sidrequest.data.gid = gid;
4587 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4588 &sidrequest, &sidresponse);
4590 if (result != NSS_STATUS_SUCCESS) {
4591 DEBUG(1, ("winbind could not find SID of gid %d\n",
4596 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4598 string_to_sid(&sid, sidresponse.data.sid.sid);
4599 add_sid_to_token(token, &sid);
4602 SAFE_FREE(response.extra_data.data);
4608 * Get a list of all user tokens we want to look at
4611 static bool get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4613 struct winbindd_request request;
4614 struct winbindd_response response;
4615 const char *extra_data;
4618 struct user_token *result;
4619 TALLOC_CTX *frame = NULL;
4621 if (lp_winbind_use_default_domain() &&
4622 (opt_target_workgroup == NULL)) {
4623 d_fprintf(stderr, "winbind use default domain = yes set, "
4624 "please specify a workgroup\n");
4628 /* Send request to winbind daemon */
4630 ZERO_STRUCT(request);
4631 ZERO_STRUCT(response);
4633 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4637 /* Look through extra data */
4639 if (!response.extra_data.data)
4642 extra_data = (const char *)response.extra_data.data;
4645 frame = talloc_stackframe();
4646 while(next_token_talloc(frame, &extra_data, &name, ",")) {
4650 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4652 if (result == NULL) {
4653 DEBUG(1, ("Could not malloc sid array\n"));
4658 extra_data = (const char *)response.extra_data.data;
4661 while(next_token_talloc(frame, &extra_data, &name, ",")) {
4662 fstring domain, user;
4665 fstrcpy(result[i].name, name);
4667 p = strchr(name, *lp_winbind_separator());
4669 DEBUG(3, ("%s\n", name));
4672 fstrcpy(domain, opt_target_workgroup);
4673 fstrcpy(user, name);
4676 fstrcpy(domain, name);
4681 get_user_sids(domain, user, &(result[i].token));
4685 SAFE_FREE(response.extra_data.data);
4687 *user_tokens = result;
4692 static bool get_user_tokens_from_file(FILE *f,
4694 struct user_token **tokens)
4696 struct user_token *token = NULL;
4701 if (fgets(line, sizeof(line)-1, f) == NULL) {
4705 if (line[strlen(line)-1] == '\n')
4706 line[strlen(line)-1] = '\0';
4708 if (line[0] == ' ') {
4712 string_to_sid(&sid, &line[1]);
4714 if (token == NULL) {
4715 DEBUG(0, ("File does not begin with username"));
4719 add_sid_to_token(&token->token, &sid);
4723 /* And a new user... */
4726 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4727 if (*tokens == NULL) {
4728 DEBUG(0, ("Could not realloc tokens\n"));
4732 token = &((*tokens)[*num_tokens-1]);
4734 fstrcpy(token->name, line);
4735 token->token.num_sids = 0;
4736 token->token.user_sids = NULL;
4745 * Show the list of all users that have access to a share
4748 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4749 TALLOC_CTX *mem_ctx,
4750 const char *netname,
4752 struct user_token *tokens)
4755 SEC_DESC *share_sd = NULL;
4756 SEC_DESC *root_sd = NULL;
4757 struct cli_state *cli = pipe_hnd->cli;
4759 SRV_SHARE_INFO info;
4763 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4766 if (!W_ERROR_IS_OK(result)) {
4767 DEBUG(1, ("Coult not query secdesc for share %s\n",
4772 share_sd = info.share.info502.info_502_str.sd;
4773 if (share_sd == NULL) {
4774 DEBUG(1, ("Got no secdesc for share %s\n",
4780 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4784 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4787 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4790 for (i=0; i<num_tokens; i++) {
4794 if (share_sd != NULL) {
4795 if (!se_access_check(share_sd, &tokens[i].token,
4796 1, &acc_granted, &status)) {
4797 DEBUG(1, ("Could not check share_sd for "
4803 if (!NT_STATUS_IS_OK(status))
4807 if (root_sd == NULL) {
4808 d_printf(" %s\n", tokens[i].name);
4812 if (!se_access_check(root_sd, &tokens[i].token,
4813 1, &acc_granted, &status)) {
4814 DEBUG(1, ("Could not check root_sd for user %s\n",
4819 if (!NT_STATUS_IS_OK(status))
4822 d_printf(" %s\n", tokens[i].name);
4826 cli_close(cli, fnum);
4838 static void collect_share(const char *name, uint32 m,
4839 const char *comment, void *state)
4841 struct share_list *share_list = (struct share_list *)state;
4843 if (m != STYPE_DISKTREE)
4846 share_list->num_shares += 1;
4847 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4848 if (!share_list->shares) {
4849 share_list->num_shares = 0;
4852 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4855 static void rpc_share_userlist_usage(void)
4861 * List shares on a remote RPC server, including the security descriptors
4863 * All parameters are provided by the run_rpc_command function, except for
4864 * argc, argv which are passes through.
4866 * @param domain_sid The domain sid acquired from the remote server
4867 * @param cli A cli_state connected to the server.
4868 * @param mem_ctx Talloc context, destoyed on completion of the function.
4869 * @param argc Standard main() style argc
4870 * @param argv Standard main() style argv. Initial components are already
4873 * @return Normal NTSTATUS return.
4876 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4877 const char *domain_name,
4878 struct cli_state *cli,
4879 struct rpc_pipe_client *pipe_hnd,
4880 TALLOC_CTX *mem_ctx,
4890 struct user_token *tokens = NULL;
4893 struct share_list share_list;
4896 rpc_share_userlist_usage();
4897 return NT_STATUS_UNSUCCESSFUL;
4903 f = fopen(argv[0], "r");
4907 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4908 return NT_STATUS_UNSUCCESSFUL;
4911 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4917 DEBUG(0, ("Could not read users from file\n"));
4918 return NT_STATUS_UNSUCCESSFUL;
4921 for (i=0; i<num_tokens; i++)
4922 collect_alias_memberships(&tokens[i].token);
4924 init_enum_hnd(&hnd, 0);
4926 share_list.num_shares = 0;
4927 share_list.shares = NULL;
4929 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4932 DEBUG(0, ("Error returning browse list: %s\n",
4937 for (i = 0; i < share_list.num_shares; i++) {
4938 char *netname = share_list.shares[i];
4940 if (netname[strlen(netname)-1] == '$')
4943 d_printf("%s\n", netname);
4945 show_userlist(pipe_hnd, mem_ctx, netname,
4946 num_tokens, tokens);
4949 for (i=0; i<num_tokens; i++) {
4950 free_user_token(&tokens[i].token);
4953 SAFE_FREE(share_list.shares);
4955 return NT_STATUS_OK;
4958 static int rpc_share_allowedusers(int argc, const char **argv)
4962 result = run_rpc_command(NULL, PI_SAMR, 0,
4963 rpc_aliaslist_internals,
4968 result = run_rpc_command(NULL, PI_LSARPC, 0,
4974 return run_rpc_command(NULL, PI_SRVSVC, 0,
4975 rpc_share_allowedusers_internals,
4979 int net_usersidlist(int argc, const char **argv)
4982 struct user_token *tokens = NULL;
4986 net_usersidlist_usage(argc, argv);
4990 if (!get_user_tokens(&num_tokens, &tokens)) {
4991 DEBUG(0, ("Could not get the user/sid list\n"));
4995 for (i=0; i<num_tokens; i++) {
4996 dump_user_token(&tokens[i]);
4997 free_user_token(&tokens[i].token);
5004 int net_usersidlist_usage(int argc, const char **argv)
5006 d_printf("net usersidlist\n"
5007 "\tprints out a list of all users the running winbind knows\n"
5008 "\tabout, together with all their SIDs. This is used as\n"
5009 "\tinput to the 'net rpc share allowedusers' command.\n\n");
5011 net_common_flags_usage(argc, argv);
5016 * 'net rpc share' entrypoint.
5017 * @param argc Standard main() style argc
5018 * @param argv Standard main() style argv. Initial components are already
5022 int net_rpc_share(int argc, const char **argv)
5024 struct functable func[] = {
5025 {"add", rpc_share_add},
5026 {"delete", rpc_share_delete},
5027 {"allowedusers", rpc_share_allowedusers},
5028 {"migrate", rpc_share_migrate},
5029 {"list", rpc_share_list},
5034 return run_rpc_command(NULL, PI_SRVSVC, 0,
5035 rpc_share_list_internals,
5038 return net_run_function(argc, argv, func, rpc_share_usage);
5041 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
5042 struct rpc_sh_ctx *ctx,
5043 struct rpc_pipe_client *pipe_hnd,
5044 int argc, const char **argv)
5046 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
5047 ctx->cli, pipe_hnd, mem_ctx,
5051 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
5052 struct rpc_sh_ctx *ctx,
5053 struct rpc_pipe_client *pipe_hnd,
5054 int argc, const char **argv)
5058 if ((argc < 2) || (argc > 3)) {
5059 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
5061 return NT_STATUS_INVALID_PARAMETER;
5064 result = rpccli_srvsvc_net_share_add(
5065 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
5066 (argc == 3) ? argv[2] : "",
5067 0, 0, 0, argv[1], NULL, 2, NULL);
5069 return werror_to_ntstatus(result);
5072 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
5073 struct rpc_sh_ctx *ctx,
5074 struct rpc_pipe_client *pipe_hnd,
5075 int argc, const char **argv)
5080 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5081 return NT_STATUS_INVALID_PARAMETER;
5084 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
5085 return werror_to_ntstatus(result);
5088 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
5089 struct rpc_sh_ctx *ctx,
5090 struct rpc_pipe_client *pipe_hnd,
5091 int argc, const char **argv)
5093 SRV_SHARE_INFO info;
5094 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
5098 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5099 return NT_STATUS_INVALID_PARAMETER;
5102 result = rpccli_srvsvc_net_share_get_info(
5103 pipe_hnd, mem_ctx, argv[0], 2, &info);
5104 if (!W_ERROR_IS_OK(result)) {
5108 d_printf("Name: %s\n",
5109 rpcstr_pull_unistr2_talloc(mem_ctx,
5110 &info2->info_2_str.uni_netname));
5111 d_printf("Comment: %s\n",
5112 rpcstr_pull_unistr2_talloc(mem_ctx,
5113 &info2->info_2_str.uni_remark));
5115 d_printf("Path: %s\n",
5116 rpcstr_pull_unistr2_talloc(mem_ctx,
5117 &info2->info_2_str.uni_path));
5118 d_printf("Password: %s\n",
5119 rpcstr_pull_unistr2_talloc(mem_ctx,
5120 &info2->info_2_str.uni_passwd));
5123 return werror_to_ntstatus(result);
5126 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
5127 struct rpc_sh_ctx *ctx)
5129 static struct rpc_sh_cmd cmds[] = {
5131 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
5132 "List available shares" },
5134 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
5137 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
5140 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
5141 "Get information about a share" },
5143 { NULL, NULL, 0, NULL, NULL }
5149 /****************************************************************************/
5151 static int rpc_file_usage(int argc, const char **argv)
5153 return net_help_file(argc, argv);
5157 * Close a file on a remote RPC server
5159 * All parameters are provided by the run_rpc_command function, except for
5160 * argc, argv which are passes through.
5162 * @param domain_sid The domain sid acquired from the remote server
5163 * @param cli A cli_state connected to the server.
5164 * @param mem_ctx Talloc context, destoyed on completion of the function.
5165 * @param argc Standard main() style argc
5166 * @param argv Standard main() style argv. Initial components are already
5169 * @return Normal NTSTATUS return.
5171 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
5172 const char *domain_name,
5173 struct cli_state *cli,
5174 struct rpc_pipe_client *pipe_hnd,
5175 TALLOC_CTX *mem_ctx,
5179 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5180 pipe_hnd->cli->desthost,
5181 atoi(argv[0]), NULL);
5185 * Close a file on a remote RPC server
5187 * @param argc Standard main() style argc
5188 * @param argv Standard main() style argv. Initial components are already
5191 * @return A shell status integer (0 for success)
5193 static int rpc_file_close(int argc, const char **argv)
5196 DEBUG(1, ("No fileid given on close\n"));
5197 return(rpc_file_usage(argc, argv));
5200 return run_rpc_command(NULL, PI_SRVSVC, 0,
5201 rpc_file_close_internals,
5206 * Formatted print of open file info
5208 * @param info3 FILE_INFO_3 contents
5209 * @param str3 strings for FILE_INFO_3
5212 static void display_file_info_3( FILE_INFO_3 *info3 )
5214 fstring user = "", path = "";
5216 rpcstr_pull_unistr2_fstring(user, info3->user);
5217 rpcstr_pull_unistr2_fstring(path, info3->path);
5219 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5220 info3->id, user, info3->perms, info3->num_locks, path);
5224 * List open files on a remote RPC server
5226 * All parameters are provided by the run_rpc_command function, except for
5227 * argc, argv which are passes through.
5229 * @param domain_sid The domain sid acquired from the remote server
5230 * @param cli A cli_state connected to the server.
5231 * @param mem_ctx Talloc context, destoyed on completion of the function.
5232 * @param argc Standard main() style argc
5233 * @param argv Standard main() style argv. Initial components are already
5236 * @return Normal NTSTATUS return.
5239 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
5240 const char *domain_name,
5241 struct cli_state *cli,
5242 struct rpc_pipe_client *pipe_hnd,
5243 TALLOC_CTX *mem_ctx,
5247 SRV_FILE_INFO_CTR ctr;
5250 uint32 preferred_len = 0xffffffff, i;
5251 const char *username=NULL;
5253 init_enum_hnd(&hnd, 0);
5255 /* if argc > 0, must be user command */
5257 username = smb_xstrdup(argv[0]);
5259 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
5260 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
5262 if (!W_ERROR_IS_OK(result))
5265 /* Display results */
5268 "\nEnumerating open files on remote server:\n\n"\
5269 "\nFileId Opened by Perms Locks Path"\
5270 "\n------ --------- ----- ----- ---- \n");
5271 for (i = 0; i < ctr.num_entries; i++)
5272 display_file_info_3(&ctr.file.info3[i]);
5274 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5278 * List files for a user on a remote RPC server
5280 * @param argc Standard main() style argc
5281 * @param argv Standard main() style argv. Initial components are already
5284 * @return A shell status integer (0 for success)
5287 static int rpc_file_user(int argc, const char **argv)
5290 DEBUG(1, ("No username given\n"));
5291 return(rpc_file_usage(argc, argv));
5294 return run_rpc_command(NULL, PI_SRVSVC, 0,
5295 rpc_file_list_internals,
5300 * 'net rpc file' entrypoint.
5301 * @param argc Standard main() style argc
5302 * @param argv Standard main() style argv. Initial components are already
5306 int net_rpc_file(int argc, const char **argv)
5308 struct functable func[] = {
5309 {"close", rpc_file_close},
5310 {"user", rpc_file_user},
5312 {"info", rpc_file_info},
5318 return run_rpc_command(NULL, PI_SRVSVC, 0,
5319 rpc_file_list_internals,
5322 return net_run_function(argc, argv, func, rpc_file_usage);
5326 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5328 * All parameters are provided by the run_rpc_command function, except for
5329 * argc, argv which are passed through.
5331 * @param domain_sid The domain sid aquired from the remote server
5332 * @param cli A cli_state connected to the server.
5333 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5334 * @param argc Standard main() style argc
5335 * @param argv Standard main() style argv. Initial components are already
5338 * @return Normal NTSTATUS return.
5341 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
5342 const char *domain_name,
5343 struct cli_state *cli,
5344 struct rpc_pipe_client *pipe_hnd,
5345 TALLOC_CTX *mem_ctx,
5349 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5351 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5353 if (NT_STATUS_IS_OK(result)) {
5354 d_printf("\nShutdown successfully aborted\n");
5355 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5357 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5363 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5365 * All parameters are provided by the run_rpc_command function, except for
5366 * argc, argv which are passed through.
5368 * @param domain_sid The domain sid aquired from the remote server
5369 * @param cli A cli_state connected to the server.
5370 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5371 * @param argc Standard main() style argc
5372 * @param argv Standard main() style argv. Initial components are already
5375 * @return Normal NTSTATUS return.
5378 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5379 const char *domain_name,
5380 struct cli_state *cli,
5381 struct rpc_pipe_client *pipe_hnd,
5382 TALLOC_CTX *mem_ctx,
5386 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5388 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5390 if (NT_STATUS_IS_OK(result)) {
5391 d_printf("\nShutdown successfully aborted\n");
5392 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5394 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5400 * ABORT the Shut down of a remote RPC server
5402 * @param argc Standard main() style argc
5403 * @param argv Standard main() style argv. Initial components are already
5406 * @return A shell status integer (0 for success)
5409 static int rpc_shutdown_abort(int argc, const char **argv)
5411 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5412 rpc_shutdown_abort_internals,
5418 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5420 return run_rpc_command(NULL, PI_WINREG, 0,
5421 rpc_reg_shutdown_abort_internals,
5426 * Shut down a remote RPC Server via initshutdown pipe
5428 * All parameters are provided by the run_rpc_command function, except for
5429 * argc, argv which are passes through.
5431 * @param domain_sid The domain sid aquired from the remote server
5432 * @param cli A cli_state connected to the server.
5433 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5434 * @param argc Standard main() style argc
5435 * @param argc Standard main() style argv. Initial components are already
5438 * @return Normal NTSTATUS return.
5441 NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5442 const char *domain_name,
5443 struct cli_state *cli,
5444 struct rpc_pipe_client *pipe_hnd,
5445 TALLOC_CTX *mem_ctx,
5449 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5450 const char *msg = "This machine will be shutdown shortly";
5451 uint32 timeout = 20;
5452 struct initshutdown_String msg_string;
5453 struct initshutdown_String_sub s;
5459 timeout = opt_timeout;
5463 msg_string.name = &s;
5465 /* create an entry */
5466 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5467 &msg_string, timeout, opt_force, opt_reboot, NULL);
5469 if (NT_STATUS_IS_OK(result)) {
5470 d_printf("\nShutdown of remote machine succeeded\n");
5471 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5473 DEBUG(1,("Shutdown of remote machine failed!\n"));
5479 * Shut down a remote RPC Server via winreg pipe
5481 * All parameters are provided by the run_rpc_command function, except for
5482 * argc, argv which are passes through.
5484 * @param domain_sid The domain sid aquired from the remote server
5485 * @param cli A cli_state connected to the server.
5486 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5487 * @param argc Standard main() style argc
5488 * @param argc Standard main() style argv. Initial components are already
5491 * @return Normal NTSTATUS return.
5494 NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5495 const char *domain_name,
5496 struct cli_state *cli,
5497 struct rpc_pipe_client *pipe_hnd,
5498 TALLOC_CTX *mem_ctx,
5502 const char *msg = "This machine will be shutdown shortly";
5503 uint32 timeout = 20;
5504 struct initshutdown_String msg_string;
5505 struct initshutdown_String_sub s;
5513 msg_string.name = &s;
5516 timeout = opt_timeout;
5519 /* create an entry */
5520 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5521 &msg_string, timeout, opt_force, opt_reboot, &werr);
5523 if (NT_STATUS_IS_OK(result)) {
5524 d_printf("\nShutdown of remote machine succeeded\n");
5526 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5527 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5528 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5530 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5537 * Shut down a remote RPC server
5539 * @param argc Standard main() style argc
5540 * @param argc Standard main() style argv. Initial components are already
5543 * @return A shell status integer (0 for success)
5546 static int rpc_shutdown(int argc, const char **argv)
5548 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5549 rpc_init_shutdown_internals,
5553 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5554 rc = run_rpc_command(NULL, PI_WINREG, 0,
5555 rpc_reg_shutdown_internals, argc, argv);
5561 /***************************************************************************
5562 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5564 ***************************************************************************/
5567 * Add interdomain trust account to the RPC server.
5568 * All parameters (except for argc and argv) are passed by run_rpc_command
5571 * @param domain_sid The domain sid acquired from the server
5572 * @param cli A cli_state connected to the server.
5573 * @param mem_ctx Talloc context, destoyed on completion of the function.
5574 * @param argc Standard main() style argc
5575 * @param argc Standard main() style argv. Initial components are already
5578 * @return normal NTSTATUS return code
5581 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5582 const char *domain_name,
5583 struct cli_state *cli,
5584 struct rpc_pipe_client *pipe_hnd,
5585 TALLOC_CTX *mem_ctx,
5589 POLICY_HND connect_pol, domain_pol, user_pol;
5590 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5592 struct lsa_String lsa_acct_name;
5594 uint32 acct_flags=0;
5596 uint32_t access_granted = 0;
5599 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5600 return NT_STATUS_INVALID_PARAMETER;
5604 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5607 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5608 return NT_STATUS_NO_MEMORY;
5611 strupper_m(acct_name);
5613 init_lsa_String(&lsa_acct_name, acct_name);
5615 /* Get samr policy handle */
5616 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5617 pipe_hnd->cli->desthost,
5618 MAXIMUM_ALLOWED_ACCESS,
5620 if (!NT_STATUS_IS_OK(result)) {
5624 /* Get domain policy handle */
5625 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5627 MAXIMUM_ALLOWED_ACCESS,
5628 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5630 if (!NT_STATUS_IS_OK(result)) {
5634 /* Create trusting domain's account */
5635 acb_info = ACB_NORMAL;
5636 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5637 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5638 SAMR_USER_ACCESS_SET_PASSWORD |
5639 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5640 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5642 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5650 if (!NT_STATUS_IS_OK(result)) {
5655 SAM_USERINFO_CTR ctr;
5656 SAM_USER_INFO_23 p23;
5662 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5666 ZERO_STRUCT(notime);
5670 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5671 acb_info = ACB_DOMTRUST;
5673 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5674 ¬ime, ¬ime, ¬ime,
5675 nostr, nostr, nostr, nostr, nostr,
5676 nostr, nostr, nostr, nostr, nostr,
5677 0, 0, acb_info, SAMR_FIELD_ACCT_FLAGS, 168, &hrs,
5678 0, 0, (char *)pwbuf);
5679 ctr.switch_value = 23;
5680 ctr.info.id23 = &p23;
5681 p23.passmustchange = 0;
5683 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5684 &cli->user_session_key, &ctr);
5686 if (!NT_STATUS_IS_OK(result)) {
5687 DEBUG(0,("Could not set trust account password: %s\n",
5688 nt_errstr(result)));
5694 SAFE_FREE(acct_name);
5699 * Create interdomain trust account for a remote domain.
5701 * @param argc standard argc
5702 * @param argv standard argv without initial components
5704 * @return Integer status (0 means success)
5707 static int rpc_trustdom_add(int argc, const char **argv)
5710 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5713 d_printf("Usage: net rpc trustdom add <domain>\n");
5720 * Remove interdomain trust account from the RPC server.
5721 * All parameters (except for argc and argv) are passed by run_rpc_command
5724 * @param domain_sid The domain sid acquired from the server
5725 * @param cli A cli_state connected to the server.
5726 * @param mem_ctx Talloc context, destoyed on completion of the function.
5727 * @param argc Standard main() style argc
5728 * @param argc Standard main() style argv. Initial components are already
5731 * @return normal NTSTATUS return code
5734 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5735 const char *domain_name,
5736 struct cli_state *cli,
5737 struct rpc_pipe_client *pipe_hnd,
5738 TALLOC_CTX *mem_ctx,
5742 POLICY_HND connect_pol, domain_pol, user_pol;
5743 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5745 DOM_SID trust_acct_sid;
5746 struct samr_Ids user_rids, name_types;
5747 struct lsa_String lsa_acct_name;
5750 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5751 return NT_STATUS_INVALID_PARAMETER;
5755 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5757 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5759 if (acct_name == NULL)
5760 return NT_STATUS_NO_MEMORY;
5762 strupper_m(acct_name);
5764 /* Get samr policy handle */
5765 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5766 pipe_hnd->cli->desthost,
5767 MAXIMUM_ALLOWED_ACCESS,
5769 if (!NT_STATUS_IS_OK(result)) {
5773 /* Get domain policy handle */
5774 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5776 MAXIMUM_ALLOWED_ACCESS,
5777 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5779 if (!NT_STATUS_IS_OK(result)) {
5783 init_lsa_String(&lsa_acct_name, acct_name);
5785 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5792 if (!NT_STATUS_IS_OK(result)) {
5796 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5798 MAXIMUM_ALLOWED_ACCESS,
5802 if (!NT_STATUS_IS_OK(result)) {
5806 /* append the rid to the domain sid */
5807 sid_copy(&trust_acct_sid, domain_sid);
5808 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5812 /* remove the sid */
5814 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5817 if (!NT_STATUS_IS_OK(result)) {
5823 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5826 if (!NT_STATUS_IS_OK(result)) {
5830 if (!NT_STATUS_IS_OK(result)) {
5831 DEBUG(0,("Could not set trust account password: %s\n",
5832 nt_errstr(result)));
5841 * Delete interdomain trust account for a remote domain.
5843 * @param argc standard argc
5844 * @param argv standard argv without initial components
5846 * @return Integer status (0 means success)
5849 static int rpc_trustdom_del(int argc, const char **argv)
5852 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5855 d_printf("Usage: net rpc trustdom del <domain>\n");
5862 * Establish trust relationship to a trusting domain.
5863 * Interdomain account must already be created on remote PDC.
5865 * @param argc standard argc
5866 * @param argv standard argv without initial components
5868 * @return Integer status (0 means success)
5871 static int rpc_trustdom_establish(int argc, const char **argv)
5873 struct cli_state *cli = NULL;
5874 struct sockaddr_storage server_ss;
5875 struct rpc_pipe_client *pipe_hnd = NULL;
5876 POLICY_HND connect_hnd;
5877 TALLOC_CTX *mem_ctx;
5879 DOM_SID *domain_sid;
5885 union lsa_PolicyInformation *info = NULL;
5888 * Connect to \\server\ipc$ as 'our domain' account with password
5892 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5896 domain_name = smb_xstrdup(argv[0]);
5897 strupper_m(domain_name);
5899 /* account name used at first is our domain's name with '$' */
5900 asprintf(&acct_name, "%s$", lp_workgroup());
5901 strupper_m(acct_name);
5904 * opt_workgroup will be used by connection functions further,
5905 * hence it should be set to remote domain name instead of ours
5907 if (opt_workgroup) {
5908 opt_workgroup = smb_xstrdup(domain_name);
5911 opt_user_name = acct_name;
5913 /* find the domain controller */
5914 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5915 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5919 /* connect to ipc$ as username/password */
5920 nt_status = connect_to_ipc(&cli, &server_ss, pdc_name);
5921 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5923 /* Is it trusting domain account for sure ? */
5924 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5925 nt_errstr(nt_status)));
5929 /* store who we connected to */
5931 saf_store( domain_name, pdc_name );
5934 * Connect to \\server\ipc$ again (this time anonymously)
5937 nt_status = connect_to_ipc_anonymous(&cli, &server_ss, (char*)pdc_name);
5939 if (NT_STATUS_IS_ERR(nt_status)) {
5940 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5941 domain_name, nt_errstr(nt_status)));
5946 * Use NetServerEnum2 to make sure we're talking to a proper server
5949 if (!cli_get_pdc_name(cli, domain_name, &dc_name)) {
5950 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5951 for domain %s\n", domain_name));
5957 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5958 "domain %s", domain_name))) {
5959 DEBUG(0, ("talloc_init() failed\n"));
5965 * Call LsaOpenPolicy and LsaQueryInfo
5968 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5970 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5972 talloc_destroy(mem_ctx);
5976 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5978 if (NT_STATUS_IS_ERR(nt_status)) {
5979 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5980 nt_errstr(nt_status)));
5982 talloc_destroy(mem_ctx);
5986 /* Querying info level 5 */
5988 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5990 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5992 if (NT_STATUS_IS_ERR(nt_status)) {
5993 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5994 nt_errstr(nt_status)));
5996 talloc_destroy(mem_ctx);
6000 domain_sid = info->account_domain.sid;
6002 /* There should be actually query info level 3 (following nt serv behaviour),
6003 but I still don't know if it's _really_ necessary */
6006 * Store the password in secrets db
6009 if (!pdb_set_trusteddom_pw(domain_name, opt_password, domain_sid)) {
6010 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6012 talloc_destroy(mem_ctx);
6017 * Close the pipes and clean up
6020 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6021 if (NT_STATUS_IS_ERR(nt_status)) {
6022 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6023 nt_errstr(nt_status)));
6025 talloc_destroy(mem_ctx);
6031 talloc_destroy(mem_ctx);
6033 d_printf("Trust to domain %s established\n", domain_name);
6038 * Revoke trust relationship to the remote domain
6040 * @param argc standard argc
6041 * @param argv standard argv without initial components
6043 * @return Integer status (0 means success)
6046 static int rpc_trustdom_revoke(int argc, const char **argv)
6051 if (argc < 1) return -1;
6053 /* generate upper cased domain name */
6054 domain_name = smb_xstrdup(argv[0]);
6055 strupper_m(domain_name);
6057 /* delete password of the trust */
6058 if (!pdb_del_trusteddom_pw(domain_name)) {
6059 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6066 SAFE_FREE(domain_name);
6071 * Usage for 'net rpc trustdom' command
6073 * @param argc standard argc
6074 * @param argv standard argv without inital components
6076 * @return Integer status returned to shell
6079 static int rpc_trustdom_usage(int argc, const char **argv)
6081 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
6082 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
6083 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
6084 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
6085 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
6086 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
6091 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
6092 const char *domain_name,
6093 struct cli_state *cli,
6094 struct rpc_pipe_client *pipe_hnd,
6095 TALLOC_CTX *mem_ctx,
6100 sid_to_fstring(str_sid, domain_sid);
6101 d_printf("%s\n", str_sid);
6102 return NT_STATUS_OK;
6105 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6107 fstring ascii_sid, padding;
6108 int pad_len, col_len = 20;
6110 /* convert sid into ascii string */
6111 sid_to_fstring(ascii_sid, dom_sid);
6113 /* calculate padding space for d_printf to look nicer */
6114 pad_len = col_len - strlen(trusted_dom_name);
6115 padding[pad_len] = 0;
6116 do padding[--pad_len] = ' '; while (pad_len);
6118 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6121 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6122 TALLOC_CTX *mem_ctx,
6125 const char *trusted_dom_name)
6128 union lsa_TrustedDomainInfo info;
6129 char *cleartextpwd = NULL;
6132 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6135 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6137 if (NT_STATUS_IS_ERR(nt_status)) {
6138 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6139 nt_errstr(nt_status)));
6143 data = data_blob(NULL, info.password.password->length);
6146 info.password.password->data,
6147 info.password.password->length);
6148 data.length = info.password.password->length;
6150 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password,
6153 if (cleartextpwd == NULL) {
6154 DEBUG(0,("retrieved NULL password\n"));
6155 nt_status = NT_STATUS_UNSUCCESSFUL;
6159 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6160 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6161 nt_status = NT_STATUS_UNSUCCESSFUL;
6165 #ifdef DEBUG_PASSWORD
6166 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], "
6167 "password: [%s]\n", trusted_dom_name,
6168 sid_string_dbg(&dom_sid), cleartextpwd));
6172 SAFE_FREE(cleartextpwd);
6173 data_blob_free(&data);
6178 static int rpc_trustdom_vampire(int argc, const char **argv)
6180 /* common variables */
6181 TALLOC_CTX* mem_ctx;
6182 struct cli_state *cli = NULL;
6183 struct rpc_pipe_client *pipe_hnd = NULL;
6185 const char *domain_name = NULL;
6186 DOM_SID *queried_dom_sid;
6187 POLICY_HND connect_hnd;
6188 union lsa_PolicyInformation *info = NULL;
6190 /* trusted domains listing variables */
6191 unsigned int num_domains, enum_ctx = 0;
6193 DOM_SID *domain_sids;
6194 char **trusted_dom_names;
6198 * Listing trusted domains (stored in secrets.tdb, if local)
6201 mem_ctx = talloc_init("trust relationships vampire");
6204 * set domain and pdc name to local samba server (default)
6205 * or to remote one given in command line
6208 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6209 domain_name = opt_workgroup;
6210 opt_target_workgroup = opt_workgroup;
6212 fstrcpy(pdc_name, global_myname());
6213 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6214 opt_target_workgroup = domain_name;
6217 /* open \PIPE\lsarpc and open policy handle */
6218 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6219 if (!NT_STATUS_IS_OK(nt_status)) {
6220 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6221 nt_errstr(nt_status)));
6222 talloc_destroy(mem_ctx);
6226 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6228 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6229 nt_errstr(nt_status) ));
6231 talloc_destroy(mem_ctx);
6235 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6237 if (NT_STATUS_IS_ERR(nt_status)) {
6238 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6239 nt_errstr(nt_status)));
6241 talloc_destroy(mem_ctx);
6245 /* query info level 5 to obtain sid of a domain being queried */
6246 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6248 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6251 if (NT_STATUS_IS_ERR(nt_status)) {
6252 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6253 nt_errstr(nt_status)));
6255 talloc_destroy(mem_ctx);
6259 queried_dom_sid = info->account_domain.sid;
6262 * Keep calling LsaEnumTrustdom over opened pipe until
6263 * the end of enumeration is reached
6266 d_printf("Vampire trusted domains:\n\n");
6269 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6271 &trusted_dom_names, &domain_sids);
6273 if (NT_STATUS_IS_ERR(nt_status)) {
6274 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6275 nt_errstr(nt_status)));
6277 talloc_destroy(mem_ctx);
6281 for (i = 0; i < num_domains; i++) {
6283 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6285 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6286 domain_sids[i], trusted_dom_names[i]);
6287 if (!NT_STATUS_IS_OK(nt_status)) {
6289 talloc_destroy(mem_ctx);
6295 * in case of no trusted domains say something rather
6296 * than just display blank line
6298 if (!num_domains) d_printf("none\n");
6300 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6302 /* close this connection before doing next one */
6303 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6304 if (NT_STATUS_IS_ERR(nt_status)) {
6305 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6306 nt_errstr(nt_status)));
6308 talloc_destroy(mem_ctx);
6312 /* close lsarpc pipe and connection to IPC$ */
6315 talloc_destroy(mem_ctx);
6319 static int rpc_trustdom_list(int argc, const char **argv)
6321 /* common variables */
6322 TALLOC_CTX* mem_ctx;
6323 struct cli_state *cli = NULL, *remote_cli = NULL;
6324 struct rpc_pipe_client *pipe_hnd = NULL;
6326 const char *domain_name = NULL;
6327 DOM_SID *queried_dom_sid;
6329 int ascii_dom_name_len;
6330 POLICY_HND connect_hnd;
6331 union lsa_PolicyInformation *info = NULL;
6333 /* trusted domains listing variables */
6334 unsigned int num_domains, enum_ctx = 0;
6335 int i, pad_len, col_len = 20;
6336 DOM_SID *domain_sids;
6337 char **trusted_dom_names;
6340 /* trusting domains listing variables */
6341 POLICY_HND domain_hnd;
6342 char **trusting_dom_names;
6343 uint32 *trusting_dom_rids;
6346 * Listing trusted domains (stored in secrets.tdb, if local)
6349 mem_ctx = talloc_init("trust relationships listing");
6352 * set domain and pdc name to local samba server (default)
6353 * or to remote one given in command line
6356 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6357 domain_name = opt_workgroup;
6358 opt_target_workgroup = opt_workgroup;
6360 fstrcpy(pdc_name, global_myname());
6361 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6362 opt_target_workgroup = domain_name;
6365 /* open \PIPE\lsarpc and open policy handle */
6366 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6367 if (!NT_STATUS_IS_OK(nt_status)) {
6368 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6369 nt_errstr(nt_status)));
6370 talloc_destroy(mem_ctx);
6374 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6376 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6377 nt_errstr(nt_status) ));
6379 talloc_destroy(mem_ctx);
6383 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6385 if (NT_STATUS_IS_ERR(nt_status)) {
6386 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6387 nt_errstr(nt_status)));
6389 talloc_destroy(mem_ctx);
6393 /* query info level 5 to obtain sid of a domain being queried */
6394 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6396 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6399 if (NT_STATUS_IS_ERR(nt_status)) {
6400 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6401 nt_errstr(nt_status)));
6403 talloc_destroy(mem_ctx);
6407 queried_dom_sid = info->account_domain.sid;
6410 * Keep calling LsaEnumTrustdom over opened pipe until
6411 * the end of enumeration is reached
6414 d_printf("Trusted domains list:\n\n");
6417 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6419 &trusted_dom_names, &domain_sids);
6421 if (NT_STATUS_IS_ERR(nt_status)) {
6422 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6423 nt_errstr(nt_status)));
6425 talloc_destroy(mem_ctx);
6429 for (i = 0; i < num_domains; i++) {
6430 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6434 * in case of no trusted domains say something rather
6435 * than just display blank line
6437 if (!num_domains) d_printf("none\n");
6439 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6441 /* close this connection before doing next one */
6442 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6443 if (NT_STATUS_IS_ERR(nt_status)) {
6444 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6445 nt_errstr(nt_status)));
6447 talloc_destroy(mem_ctx);
6451 cli_rpc_pipe_close(pipe_hnd);
6454 * Listing trusting domains (stored in passdb backend, if local)
6457 d_printf("\nTrusting domains list:\n\n");
6460 * Open \PIPE\samr and get needed policy handles
6462 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6464 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6466 talloc_destroy(mem_ctx);
6471 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6472 pipe_hnd->cli->desthost,
6473 SA_RIGHT_SAM_OPEN_DOMAIN,
6475 if (!NT_STATUS_IS_OK(nt_status)) {
6476 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6477 nt_errstr(nt_status)));
6479 talloc_destroy(mem_ctx);
6483 /* SamrOpenDomain - we have to open domain policy handle in order to be
6484 able to enumerate accounts*/
6485 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6487 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6490 if (!NT_STATUS_IS_OK(nt_status)) {
6491 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6492 nt_errstr(nt_status)));
6494 talloc_destroy(mem_ctx);
6499 * perform actual enumeration
6502 enum_ctx = 0; /* reset enumeration context from last enumeration */
6505 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6506 &enum_ctx, ACB_DOMTRUST, 0xffff,
6507 &trusting_dom_names, &trusting_dom_rids,
6509 if (NT_STATUS_IS_ERR(nt_status)) {
6510 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6511 nt_errstr(nt_status)));
6513 talloc_destroy(mem_ctx);
6517 for (i = 0; i < num_domains; i++) {
6520 * get each single domain's sid (do we _really_ need this ?):
6521 * 1) connect to domain's pdc
6522 * 2) query the pdc for domain's sid
6525 /* get rid of '$' tail */
6526 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6527 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6528 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6530 /* calculate padding space for d_printf to look nicer */
6531 pad_len = col_len - strlen(trusting_dom_names[i]);
6532 padding[pad_len] = 0;
6533 do padding[--pad_len] = ' '; while (pad_len);
6535 /* set opt_* variables to remote domain */
6536 strupper_m(trusting_dom_names[i]);
6537 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6538 opt_target_workgroup = opt_workgroup;
6540 d_printf("%s%s", trusting_dom_names[i], padding);
6542 /* connect to remote domain controller */
6543 nt_status = net_make_ipc_connection(
6544 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6546 if (NT_STATUS_IS_OK(nt_status)) {
6547 /* query for domain's sid */
6548 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6549 d_fprintf(stderr, "couldn't get domain's sid\n");
6551 cli_shutdown(remote_cli);
6554 d_fprintf(stderr, "domain controller is not "
6556 nt_errstr(nt_status));
6560 if (!num_domains) d_printf("none\n");
6562 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6564 /* close opened samr and domain policy handles */
6565 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6566 if (!NT_STATUS_IS_OK(nt_status)) {
6567 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6570 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6571 if (!NT_STATUS_IS_OK(nt_status)) {
6572 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6575 /* close samr pipe and connection to IPC$ */
6578 talloc_destroy(mem_ctx);
6583 * Entrypoint for 'net rpc trustdom' code
6585 * @param argc standard argc
6586 * @param argv standard argv without initial components
6588 * @return Integer status (0 means success)
6591 static int rpc_trustdom(int argc, const char **argv)
6593 struct functable func[] = {
6594 {"add", rpc_trustdom_add},
6595 {"del", rpc_trustdom_del},
6596 {"establish", rpc_trustdom_establish},
6597 {"revoke", rpc_trustdom_revoke},
6598 {"help", rpc_trustdom_usage},
6599 {"list", rpc_trustdom_list},
6600 {"vampire", rpc_trustdom_vampire},
6605 rpc_trustdom_usage(argc, argv);
6609 return (net_run_function(argc, argv, func, rpc_user_usage));
6613 * Check if a server will take rpc commands
6614 * @param flags Type of server to connect to (PDC, DMB, localhost)
6615 * if the host is not explicitly specified
6616 * @return bool (true means rpc supported)
6618 bool net_rpc_check(unsigned flags)
6620 struct cli_state *cli;
6622 struct sockaddr_storage server_ss;
6623 char *server_name = NULL;
6626 /* flags (i.e. server type) may depend on command */
6627 if (!net_find_server(NULL, flags, &server_ss, &server_name))
6630 if ((cli = cli_initialise()) == NULL) {
6634 status = cli_connect(cli, server_name, &server_ss);
6635 if (!NT_STATUS_IS_OK(status))
6637 if (!attempt_netbios_session_request(&cli, global_myname(),
6638 server_name, &server_ss))
6640 if (!cli_negprot(cli))
6642 if (cli->protocol < PROTOCOL_NT1)
6651 /* dump sam database via samsync rpc calls */
6652 static int rpc_samdump(int argc, const char **argv) {
6653 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6657 /* syncronise sam database via samsync rpc calls */
6658 static int rpc_vampire(int argc, const char **argv) {
6659 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6664 * Migrate everything from a print-server
6666 * @param argc Standard main() style argc
6667 * @param argv Standard main() style argv. Initial components are already
6670 * @return A shell status integer (0 for success)
6672 * The order is important !
6673 * To successfully add drivers the print-queues have to exist !
6674 * Applying ACLs should be the last step, because you're easily locked out
6677 static int rpc_printer_migrate_all(int argc, const char **argv)
6682 printf("no server to migrate\n");
6686 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6690 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6694 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6698 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6702 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6707 * Migrate print-drivers from a print-server
6709 * @param argc Standard main() style argc
6710 * @param argv Standard main() style argv. Initial components are already
6713 * @return A shell status integer (0 for success)
6715 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6718 printf("no server to migrate\n");
6722 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6723 rpc_printer_migrate_drivers_internals,
6728 * Migrate print-forms from a print-server
6730 * @param argc Standard main() style argc
6731 * @param argv Standard main() style argv. Initial components are already
6734 * @return A shell status integer (0 for success)
6736 static int rpc_printer_migrate_forms(int argc, const char **argv)
6739 printf("no server to migrate\n");
6743 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6744 rpc_printer_migrate_forms_internals,
6749 * Migrate printers from a print-server
6751 * @param argc Standard main() style argc
6752 * @param argv Standard main() style argv. Initial components are already
6755 * @return A shell status integer (0 for success)
6757 static int rpc_printer_migrate_printers(int argc, const char **argv)
6760 printf("no server to migrate\n");
6764 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6765 rpc_printer_migrate_printers_internals,
6770 * Migrate printer-ACLs from a print-server
6772 * @param argc Standard main() style argc
6773 * @param argv Standard main() style argv. Initial components are already
6776 * @return A shell status integer (0 for success)
6778 static int rpc_printer_migrate_security(int argc, const char **argv)
6781 printf("no server to migrate\n");
6785 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6786 rpc_printer_migrate_security_internals,
6791 * Migrate printer-settings from a print-server
6793 * @param argc Standard main() style argc
6794 * @param argv Standard main() style argv. Initial components are already
6797 * @return A shell status integer (0 for success)
6799 static int rpc_printer_migrate_settings(int argc, const char **argv)
6802 printf("no server to migrate\n");
6806 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6807 rpc_printer_migrate_settings_internals,
6812 * 'net rpc printer' entrypoint.
6813 * @param argc Standard main() style argc
6814 * @param argv Standard main() style argv. Initial components are already
6818 int rpc_printer_migrate(int argc, const char **argv)
6821 /* ouch: when addriver and setdriver are called from within
6822 rpc_printer_migrate_drivers_internals, the printer-queue already
6825 struct functable func[] = {
6826 {"all", rpc_printer_migrate_all},
6827 {"drivers", rpc_printer_migrate_drivers},
6828 {"forms", rpc_printer_migrate_forms},
6829 {"help", rpc_printer_usage},
6830 {"printers", rpc_printer_migrate_printers},
6831 {"security", rpc_printer_migrate_security},
6832 {"settings", rpc_printer_migrate_settings},
6836 return net_run_function(argc, argv, func, rpc_printer_usage);
6841 * List printers on a remote RPC server
6843 * @param argc Standard main() style argc
6844 * @param argv Standard main() style argv. Initial components are already
6847 * @return A shell status integer (0 for success)
6849 static int rpc_printer_list(int argc, const char **argv)
6852 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6853 rpc_printer_list_internals,
6858 * List printer-drivers on a remote RPC server
6860 * @param argc Standard main() style argc
6861 * @param argv Standard main() style argv. Initial components are already
6864 * @return A shell status integer (0 for success)
6866 static int rpc_printer_driver_list(int argc, const char **argv)
6869 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6870 rpc_printer_driver_list_internals,
6875 * Publish printer in ADS via MSRPC
6877 * @param argc Standard main() style argc
6878 * @param argv Standard main() style argv. Initial components are already
6881 * @return A shell status integer (0 for success)
6883 static int rpc_printer_publish_publish(int argc, const char **argv)
6886 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6887 rpc_printer_publish_publish_internals,
6892 * Update printer in ADS via MSRPC
6894 * @param argc Standard main() style argc
6895 * @param argv Standard main() style argv. Initial components are already
6898 * @return A shell status integer (0 for success)
6900 static int rpc_printer_publish_update(int argc, const char **argv)
6903 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6904 rpc_printer_publish_update_internals,
6909 * UnPublish printer in ADS via MSRPC
6911 * @param argc Standard main() style argc
6912 * @param argv Standard main() style argv. Initial components are already
6915 * @return A shell status integer (0 for success)
6917 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6920 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6921 rpc_printer_publish_unpublish_internals,
6926 * List published printers via MSRPC
6928 * @param argc Standard main() style argc
6929 * @param argv Standard main() style argv. Initial components are already
6932 * @return A shell status integer (0 for success)
6934 static int rpc_printer_publish_list(int argc, const char **argv)
6937 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6938 rpc_printer_publish_list_internals,
6944 * Publish printer in ADS
6946 * @param argc Standard main() style argc
6947 * @param argv Standard main() style argv. Initial components are already
6950 * @return A shell status integer (0 for success)
6952 static int rpc_printer_publish(int argc, const char **argv)
6955 struct functable func[] = {
6956 {"publish", rpc_printer_publish_publish},
6957 {"update", rpc_printer_publish_update},
6958 {"unpublish", rpc_printer_publish_unpublish},
6959 {"list", rpc_printer_publish_list},
6960 {"help", rpc_printer_usage},
6965 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6966 rpc_printer_publish_list_internals,
6969 return net_run_function(argc, argv, func, rpc_printer_usage);
6975 * Display rpc printer help page.
6976 * @param argc Standard main() style argc
6977 * @param argv Standard main() style argv. Initial components are already
6980 int rpc_printer_usage(int argc, const char **argv)
6982 return net_help_printer(argc, argv);
6986 * 'net rpc printer' entrypoint.
6987 * @param argc Standard main() style argc
6988 * @param argv Standard main() style argv. Initial components are already
6991 int net_rpc_printer(int argc, const char **argv)
6993 struct functable func[] = {
6994 {"list", rpc_printer_list},
6995 {"migrate", rpc_printer_migrate},
6996 {"driver", rpc_printer_driver_list},
6997 {"publish", rpc_printer_publish},
7002 return run_rpc_command(NULL, PI_SPOOLSS, 0,
7003 rpc_printer_list_internals,
7006 return net_run_function(argc, argv, func, rpc_printer_usage);
7009 /****************************************************************************/
7013 * Basic usage function for 'net rpc'
7014 * @param argc Standard main() style argc
7015 * @param argv Standard main() style argv. Initial components are already
7019 int net_rpc_usage(int argc, const char **argv)
7021 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
7022 d_printf(" net rpc join \t\t\tto join a domain \n");
7023 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
7024 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
7025 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
7026 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
7027 d_printf(" net rpc group \t\tto list groups\n");
7028 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
7029 d_printf(" net rpc printer \t\tto list and migrate printers\n");
7030 d_printf(" net rpc file \t\t\tto list open files\n");
7031 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
7032 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
7033 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
7034 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
7035 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
7036 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
7037 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
7038 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
7039 d_printf(" net rpc registry\t\tto manage registry hives\n");
7040 d_printf(" net rpc service\t\tto start, stop and query services\n");
7041 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
7042 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
7044 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
7045 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
7046 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
7047 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
7048 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
7054 * Help function for 'net rpc'. Calls command specific help if requested
7055 * or displays usage of net rpc
7056 * @param argc Standard main() style argc
7057 * @param argv Standard main() style argv. Initial components are already
7061 int net_rpc_help(int argc, const char **argv)
7063 struct functable func[] = {
7064 {"join", rpc_join_usage},
7065 {"user", rpc_user_usage},
7066 {"group", rpc_group_usage},
7067 {"share", rpc_share_usage},
7068 /*{"changetrustpw", rpc_changetrustpw_usage}, */
7069 {"trustdom", rpc_trustdom_usage},
7070 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
7071 /*{"shutdown", rpc_shutdown_usage}, */
7072 {"vampire", rpc_vampire_usage},
7077 net_rpc_usage(argc, argv);
7081 return (net_run_function(argc, argv, func, rpc_user_usage));
7085 * 'net rpc' entrypoint.
7086 * @param argc Standard main() style argc
7087 * @param argv Standard main() style argv. Initial components are already
7091 int net_rpc(int argc, const char **argv)
7093 struct functable func[] = {
7094 {"audit", net_rpc_audit},
7095 {"info", net_rpc_info},
7096 {"join", net_rpc_join},
7097 {"oldjoin", net_rpc_oldjoin},
7098 {"testjoin", net_rpc_testjoin},
7099 {"user", net_rpc_user},
7100 {"password", rpc_user_password},
7101 {"group", net_rpc_group},
7102 {"share", net_rpc_share},
7103 {"file", net_rpc_file},
7104 {"printer", net_rpc_printer},
7105 {"changetrustpw", net_rpc_changetrustpw},
7106 {"trustdom", rpc_trustdom},
7107 {"abortshutdown", rpc_shutdown_abort},
7108 {"shutdown", rpc_shutdown},
7109 {"samdump", rpc_samdump},
7110 {"vampire", rpc_vampire},
7111 {"getsid", net_rpc_getsid},
7112 {"rights", net_rpc_rights},
7113 {"service", net_rpc_service},
7114 {"registry", net_rpc_registry},
7115 {"shell", net_rpc_shell},
7116 {"help", net_rpc_help},
7119 return net_run_function(argc, argv, func, net_rpc_usage);