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 samr_SamArray *groups = NULL;
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_EnumDomainAliases(pipe_hnd, mem_ctx,
2735 if (!NT_STATUS_IS_OK(result) &&
2736 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2739 for (i = 0; i < num_entries; i++) {
2741 const char *description = NULL;
2743 if (opt_long_list_entries) {
2745 POLICY_HND alias_pol;
2746 union samr_AliasInfo *info = NULL;
2748 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2751 groups->entries[i].idx,
2753 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2757 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2759 description = info->description.string;
2763 if (description != NULL) {
2764 printf("%-21.21s %-50.50s\n",
2765 groups->entries[i].name.string,
2768 printf("%s\n", groups->entries[i].name.string);
2771 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2772 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2773 /* Get builtin policy handle */
2775 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2777 MAXIMUM_ALLOWED_ACCESS,
2778 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2780 if (!NT_STATUS_IS_OK(result)) {
2783 /* query builtin aliases */
2786 if (!builtin) break;
2788 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2794 if (!NT_STATUS_IS_OK(result) &&
2795 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2798 for (i = 0; i < num_entries; i++) {
2800 const char *description = NULL;
2802 if (opt_long_list_entries) {
2804 POLICY_HND alias_pol;
2805 union samr_AliasInfo *info = NULL;
2807 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2810 groups->entries[i].idx,
2812 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2816 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2818 description = info->description.string;
2822 if (description != NULL) {
2823 printf("%-21.21s %-50.50s\n",
2824 groups->entries[i].name.string,
2827 printf("%s\n", groups->entries[i].name.string);
2830 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2836 static int rpc_group_list(int argc, const char **argv)
2838 return run_rpc_command(NULL, PI_SAMR, 0,
2839 rpc_group_list_internals,
2843 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2844 TALLOC_CTX *mem_ctx,
2845 const char *domain_name,
2846 const DOM_SID *domain_sid,
2847 POLICY_HND *domain_pol,
2851 POLICY_HND group_pol;
2852 uint32 num_members, *group_rids;
2854 struct samr_RidTypeArray *rids = NULL;
2855 struct lsa_Strings names;
2856 struct samr_Ids types;
2859 sid_to_fstring(sid_str, domain_sid);
2861 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2863 MAXIMUM_ALLOWED_ACCESS,
2867 if (!NT_STATUS_IS_OK(result))
2870 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2874 if (!NT_STATUS_IS_OK(result))
2877 num_members = rids->count;
2878 group_rids = rids->rids;
2880 while (num_members > 0) {
2881 int this_time = 512;
2883 if (num_members < this_time)
2884 this_time = num_members;
2886 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2893 if (!NT_STATUS_IS_OK(result))
2896 /* We only have users as members, but make the output
2897 the same as the output of alias members */
2899 for (i = 0; i < this_time; i++) {
2901 if (opt_long_list_entries) {
2902 printf("%s-%d %s\\%s %d\n", sid_str,
2903 group_rids[i], domain_name,
2904 names.names[i].string,
2907 printf("%s\\%s\n", domain_name,
2908 names.names[i].string);
2912 num_members -= this_time;
2916 return NT_STATUS_OK;
2919 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2920 TALLOC_CTX *mem_ctx,
2921 POLICY_HND *domain_pol,
2925 struct rpc_pipe_client *lsa_pipe;
2926 POLICY_HND alias_pol, lsa_pol;
2928 DOM_SID *alias_sids;
2931 enum lsa_SidType *types;
2933 struct lsa_SidArray sid_array;
2935 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2937 MAXIMUM_ALLOWED_ACCESS,
2941 if (!NT_STATUS_IS_OK(result))
2944 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2948 if (!NT_STATUS_IS_OK(result)) {
2949 d_fprintf(stderr, "Couldn't list alias members\n");
2953 num_members = sid_array.num_sids;
2955 if (num_members == 0) {
2956 return NT_STATUS_OK;
2959 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2961 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2962 nt_errstr(result) );
2966 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2967 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2969 if (!NT_STATUS_IS_OK(result)) {
2970 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2971 cli_rpc_pipe_close(lsa_pipe);
2975 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2977 d_fprintf(stderr, "Out of memory\n");
2978 cli_rpc_pipe_close(lsa_pipe);
2979 return NT_STATUS_NO_MEMORY;
2982 for (i=0; i<num_members; i++) {
2983 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2986 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2988 &domains, &names, &types);
2990 if (!NT_STATUS_IS_OK(result) &&
2991 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2992 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2993 cli_rpc_pipe_close(lsa_pipe);
2997 for (i = 0; i < num_members; i++) {
2999 sid_to_fstring(sid_str, &alias_sids[i]);
3001 if (opt_long_list_entries) {
3002 printf("%s %s\\%s %d\n", sid_str,
3003 domains[i] ? domains[i] : "*unknown*",
3004 names[i] ? names[i] : "*unknown*", types[i]);
3007 printf("%s\\%s\n", domains[i], names[i]);
3009 printf("%s\n", sid_str);
3013 cli_rpc_pipe_close(lsa_pipe);
3014 return NT_STATUS_OK;
3017 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
3018 const char *domain_name,
3019 struct cli_state *cli,
3020 struct rpc_pipe_client *pipe_hnd,
3021 TALLOC_CTX *mem_ctx,
3026 POLICY_HND connect_pol, domain_pol;
3027 struct samr_Ids rids, rid_types;
3028 struct lsa_String lsa_acct_name;
3030 /* Get sam policy handle */
3032 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3033 pipe_hnd->cli->desthost,
3034 MAXIMUM_ALLOWED_ACCESS,
3037 if (!NT_STATUS_IS_OK(result))
3040 /* Get domain policy handle */
3042 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3044 MAXIMUM_ALLOWED_ACCESS,
3045 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3048 if (!NT_STATUS_IS_OK(result))
3051 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3053 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
3060 if (!NT_STATUS_IS_OK(result)) {
3062 /* Ok, did not find it in the global sam, try with builtin */
3064 DOM_SID sid_Builtin;
3066 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3068 string_to_sid(&sid_Builtin, "S-1-5-32");
3070 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3072 MAXIMUM_ALLOWED_ACCESS,
3076 if (!NT_STATUS_IS_OK(result)) {
3077 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3081 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
3088 if (!NT_STATUS_IS_OK(result)) {
3089 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3094 if (rids.count != 1) {
3095 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3099 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3100 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
3101 domain_sid, &domain_pol,
3105 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3106 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
3110 return NT_STATUS_NO_SUCH_GROUP;
3113 static int rpc_group_members(int argc, const char **argv)
3116 return rpc_group_usage(argc, argv);
3119 return run_rpc_command(NULL, PI_SAMR, 0,
3120 rpc_group_members_internals,
3124 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
3125 const char *domain_name,
3126 struct cli_state *cli,
3127 struct rpc_pipe_client *pipe_hnd,
3128 TALLOC_CTX *mem_ctx,
3133 POLICY_HND connect_pol, domain_pol, group_pol;
3134 union samr_GroupInfo group_info;
3135 struct samr_Ids rids, rid_types;
3136 struct lsa_String lsa_acct_name;
3139 d_printf("Usage: 'net rpc group rename group newname'\n");
3140 return NT_STATUS_UNSUCCESSFUL;
3143 /* Get sam policy handle */
3145 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3146 pipe_hnd->cli->desthost,
3147 MAXIMUM_ALLOWED_ACCESS,
3150 if (!NT_STATUS_IS_OK(result))
3153 /* Get domain policy handle */
3155 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3157 MAXIMUM_ALLOWED_ACCESS,
3158 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3161 if (!NT_STATUS_IS_OK(result))
3164 init_lsa_String(&lsa_acct_name, argv[0]);
3166 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
3173 if (rids.count != 1) {
3174 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3178 if (rid_types.ids[0] != SID_NAME_DOM_GRP) {
3179 d_fprintf(stderr, "Can only rename domain groups\n");
3180 return NT_STATUS_UNSUCCESSFUL;
3183 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
3185 MAXIMUM_ALLOWED_ACCESS,
3189 if (!NT_STATUS_IS_OK(result))
3192 init_lsa_String(&group_info.name, argv[1]);
3194 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
3199 if (!NT_STATUS_IS_OK(result))
3202 return NT_STATUS_NO_SUCH_GROUP;
3205 static int rpc_group_rename(int argc, const char **argv)
3208 return rpc_group_usage(argc, argv);
3211 return run_rpc_command(NULL, PI_SAMR, 0,
3212 rpc_group_rename_internals,
3217 * 'net rpc group' entrypoint.
3218 * @param argc Standard main() style argc
3219 * @param argc Standard main() style argv. Initial components are already
3223 int net_rpc_group(int argc, const char **argv)
3225 struct functable func[] = {
3226 {"add", rpc_group_add},
3227 {"delete", rpc_group_delete},
3228 {"addmem", rpc_group_addmem},
3229 {"delmem", rpc_group_delmem},
3230 {"list", rpc_group_list},
3231 {"members", rpc_group_members},
3232 {"rename", rpc_group_rename},
3237 return run_rpc_command(NULL, PI_SAMR, 0,
3238 rpc_group_list_internals,
3242 return net_run_function(argc, argv, func, rpc_group_usage);
3245 /****************************************************************************/
3247 static int rpc_share_usage(int argc, const char **argv)
3249 return net_help_share(argc, argv);
3253 * Add a share on a remote RPC server
3255 * All parameters are provided by the run_rpc_command function, except for
3256 * argc, argv which are passes through.
3258 * @param domain_sid The domain sid acquired from the remote server
3259 * @param cli A cli_state connected to the server.
3260 * @param mem_ctx Talloc context, destoyed on completion of the function.
3261 * @param argc Standard main() style argc
3262 * @param argv Standard main() style argv. Initial components are already
3265 * @return Normal NTSTATUS return.
3267 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3268 const char *domain_name,
3269 struct cli_state *cli,
3270 struct rpc_pipe_client *pipe_hnd,
3271 TALLOC_CTX *mem_ctx,int argc,
3277 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3278 uint32 num_users=0, perms=0;
3279 char *password=NULL; /* don't allow a share password */
3282 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3283 return NT_STATUS_NO_MEMORY;
3286 path = strchr(sharename, '=');
3288 return NT_STATUS_UNSUCCESSFUL;
3291 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
3292 opt_comment, perms, opt_maxusers,
3293 num_users, path, password,
3295 return werror_to_ntstatus(result);
3298 static int rpc_share_add(int argc, const char **argv)
3300 if ((argc < 1) || !strchr(argv[0], '=')) {
3301 DEBUG(1,("Sharename or path not specified on add\n"));
3302 return rpc_share_usage(argc, argv);
3304 return run_rpc_command(NULL, PI_SRVSVC, 0,
3305 rpc_share_add_internals,
3310 * Delete a share on a remote RPC server
3312 * All parameters are provided by the run_rpc_command function, except for
3313 * argc, argv which are passes through.
3315 * @param domain_sid The domain sid acquired from the remote server
3316 * @param cli A cli_state connected to the server.
3317 * @param mem_ctx Talloc context, destoyed on completion of the function.
3318 * @param argc Standard main() style argc
3319 * @param argv Standard main() style argv. Initial components are already
3322 * @return Normal NTSTATUS return.
3324 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3325 const char *domain_name,
3326 struct cli_state *cli,
3327 struct rpc_pipe_client *pipe_hnd,
3328 TALLOC_CTX *mem_ctx,
3334 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
3335 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3339 * Delete a share on a remote RPC server
3341 * @param domain_sid The domain sid acquired from the remote server
3342 * @param argc Standard main() style argc
3343 * @param argv Standard main() style argv. Initial components are already
3346 * @return A shell status integer (0 for success)
3348 static int rpc_share_delete(int argc, const char **argv)
3351 DEBUG(1,("Sharename not specified on delete\n"));
3352 return rpc_share_usage(argc, argv);
3354 return run_rpc_command(NULL, PI_SRVSVC, 0,
3355 rpc_share_del_internals,
3360 * Formatted print of share info
3362 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3365 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3367 fstring netname = "", remark = "";
3369 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3370 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3372 if (opt_long_list_entries) {
3373 d_printf("%-12s %-8.8s %-50s\n",
3374 netname, share_type[info1->info_1.type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)], remark);
3376 d_printf("%s\n", netname);
3381 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3382 TALLOC_CTX *mem_ctx,
3386 SRV_SHARE_INFO_CTR *ctr)
3389 SRV_SHARE_INFO info;
3391 /* no specific share requested, enumerate all */
3395 uint32 preferred_len = 0xffffffff;
3397 init_enum_hnd(&hnd, 0);
3399 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3400 preferred_len, &hnd);
3403 /* request just one share */
3404 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3406 if (!W_ERROR_IS_OK(result))
3412 ctr->info_level = ctr->switch_value = level;
3413 ctr->ptr_share_info = ctr->ptr_entries = 1;
3414 ctr->num_entries = ctr->num_entries2 = 1;
3420 SRV_SHARE_INFO_1 *info1;
3422 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3423 if (ctr->share.info1 == NULL) {
3424 result = WERR_NOMEM;
3427 info1 = ctr->share.info1;
3429 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3431 /* Copy pointer crap */
3433 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3435 /* Duplicate strings */
3437 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3439 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3441 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3443 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3448 SRV_SHARE_INFO_2 *info2;
3450 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3451 if (ctr->share.info2 == NULL) {
3452 result = WERR_NOMEM;
3455 info2 = ctr->share.info2;
3457 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3459 /* Copy pointer crap */
3461 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3463 /* Duplicate strings */
3465 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3467 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3469 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3471 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3473 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_path);
3475 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3477 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3479 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3484 SRV_SHARE_INFO_502 *info502;
3486 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3487 if (ctr->share.info502 == NULL) {
3488 result = WERR_NOMEM;
3491 info502 = ctr->share.info502;
3493 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3495 /* Copy pointer crap */
3497 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3499 /* Duplicate strings */
3501 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3503 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3505 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3507 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3509 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_path);
3511 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3513 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3515 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3517 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3528 * List shares on a remote RPC server
3530 * All parameters are provided by the run_rpc_command function, except for
3531 * argc, argv which are passes through.
3533 * @param domain_sid The domain sid acquired from the remote server
3534 * @param cli A cli_state connected to the server.
3535 * @param mem_ctx Talloc context, destoyed on completion of the function.
3536 * @param argc Standard main() style argc
3537 * @param argv Standard main() style argv. Initial components are already
3540 * @return Normal NTSTATUS return.
3543 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3544 const char *domain_name,
3545 struct cli_state *cli,
3546 struct rpc_pipe_client *pipe_hnd,
3547 TALLOC_CTX *mem_ctx,
3551 SRV_SHARE_INFO_CTR ctr;
3553 uint32 i, level = 1;
3555 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3556 if (!W_ERROR_IS_OK(result))
3559 /* Display results */
3561 if (opt_long_list_entries) {
3563 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3564 "\nShare name Type Description\n"\
3565 "---------- ---- -----------\n");
3567 for (i = 0; i < ctr.num_entries; i++)
3568 display_share_info_1(&ctr.share.info1[i]);
3570 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3574 * 'net rpc share list' entrypoint.
3575 * @param argc Standard main() style argc
3576 * @param argv Standard main() style argv. Initial components are already
3579 static int rpc_share_list(int argc, const char **argv)
3581 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3584 static bool check_share_availability(struct cli_state *cli, const char *netname)
3586 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3587 d_printf("skipping [%s]: not a file share.\n", netname);
3597 static bool check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3599 /* only support disk shares */
3600 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3601 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3605 /* skip builtin shares */
3606 /* FIXME: should print$ be added too ? */
3607 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3608 strequal(netname,"global"))
3611 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3612 printf("excluding [%s]\n", netname);
3616 return check_share_availability(cli, netname);
3620 * Migrate shares from a remote RPC server to the local RPC srever
3622 * All parameters are provided by the run_rpc_command function, except for
3623 * argc, argv which are passes through.
3625 * @param domain_sid The domain sid acquired from the remote server
3626 * @param cli A cli_state connected to the server.
3627 * @param mem_ctx Talloc context, destoyed on completion of the function.
3628 * @param argc Standard main() style argc
3629 * @param argv Standard main() style argv. Initial components are already
3632 * @return Normal NTSTATUS return.
3635 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3636 const char *domain_name,
3637 struct cli_state *cli,
3638 struct rpc_pipe_client *pipe_hnd,
3639 TALLOC_CTX *mem_ctx,
3644 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3645 SRV_SHARE_INFO_CTR ctr_src;
3646 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3647 char *password = NULL; /* don't allow a share password */
3649 struct rpc_pipe_client *srvsvc_pipe = NULL;
3650 struct cli_state *cli_dst = NULL;
3651 uint32 level = 502; /* includes secdesc */
3653 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3654 if (!W_ERROR_IS_OK(result))
3657 /* connect destination PI_SRVSVC */
3658 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3659 if (!NT_STATUS_IS_OK(nt_status))
3663 for (i = 0; i < ctr_src.num_entries; i++) {
3665 fstring netname = "", remark = "", path = "";
3666 /* reset error-code */
3667 nt_status = NT_STATUS_UNSUCCESSFUL;
3669 rpcstr_pull_unistr2_fstring(
3670 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3671 rpcstr_pull_unistr2_fstring(
3672 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3673 rpcstr_pull_unistr2_fstring(
3674 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3676 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3679 /* finally add the share on the dst server */
3681 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3682 netname, path, remark);
3684 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3685 ctr_src.share.info502[i].info_502.perms,
3686 ctr_src.share.info502[i].info_502.max_uses,
3687 ctr_src.share.info502[i].info_502.num_uses,
3688 path, password, level,
3691 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3692 printf(" [%s] does already exist\n", netname);
3696 if (!W_ERROR_IS_OK(result)) {
3697 printf("cannot add share: %s\n", dos_errstr(result));
3703 nt_status = NT_STATUS_OK;
3707 cli_shutdown(cli_dst);
3715 * Migrate shares from a rpc-server to another
3717 * @param argc Standard main() style argc
3718 * @param argv Standard main() style argv. Initial components are already
3721 * @return A shell status integer (0 for success)
3723 static int rpc_share_migrate_shares(int argc, const char **argv)
3727 printf("no server to migrate\n");
3731 return run_rpc_command(NULL, PI_SRVSVC, 0,
3732 rpc_share_migrate_shares_internals,
3739 * @param f file_info
3740 * @param mask current search mask
3741 * @param state arg-pointer
3744 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3746 static NTSTATUS nt_status;
3747 static struct copy_clistate *local_state;
3748 static fstring filename, new_mask;
3752 local_state = (struct copy_clistate *)state;
3753 nt_status = NT_STATUS_UNSUCCESSFUL;
3755 if (strequal(f->name, ".") || strequal(f->name, ".."))
3758 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3761 if (f->mode & aDIR) {
3763 DEBUG(3,("got dir: %s\n", f->name));
3765 fstrcpy(dir, local_state->cwd);
3767 fstrcat(dir, f->name);
3769 switch (net_mode_share)
3771 case NET_MODE_SHARE_MIGRATE:
3772 /* create that directory */
3773 nt_status = net_copy_file(local_state->mem_ctx,
3774 local_state->cli_share_src,
3775 local_state->cli_share_dst,
3777 opt_acls? True : False,
3778 opt_attrs? True : False,
3779 opt_timestamps? True : False,
3783 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3787 if (!NT_STATUS_IS_OK(nt_status))
3788 printf("could not handle dir %s: %s\n",
3789 dir, nt_errstr(nt_status));
3791 /* search below that directory */
3792 fstrcpy(new_mask, dir);
3793 fstrcat(new_mask, "\\*");
3795 old_dir = local_state->cwd;
3796 local_state->cwd = dir;
3797 if (!sync_files(local_state, new_mask))
3798 printf("could not handle files\n");
3799 local_state->cwd = old_dir;
3806 fstrcpy(filename, local_state->cwd);
3807 fstrcat(filename, "\\");
3808 fstrcat(filename, f->name);
3810 DEBUG(3,("got file: %s\n", filename));
3812 switch (net_mode_share)
3814 case NET_MODE_SHARE_MIGRATE:
3815 nt_status = net_copy_file(local_state->mem_ctx,
3816 local_state->cli_share_src,
3817 local_state->cli_share_dst,
3819 opt_acls? True : False,
3820 opt_attrs? True : False,
3821 opt_timestamps? True: False,
3825 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3829 if (!NT_STATUS_IS_OK(nt_status))
3830 printf("could not handle file %s: %s\n",
3831 filename, nt_errstr(nt_status));
3836 * sync files, can be called recursivly to list files
3837 * and then call copy_fn for each file
3839 * @param cp_clistate pointer to the copy_clistate we work with
3840 * @param mask the current search mask
3842 * @return Boolean result
3844 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3846 struct cli_state *targetcli;
3847 char *targetpath = NULL;
3849 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3851 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3852 mask, &targetcli, &targetpath ) ) {
3853 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3854 mask, cli_errstr(cp_clistate->cli_share_src));
3858 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3859 d_fprintf(stderr, "listing %s failed with error: %s\n",
3860 mask, cli_errstr(targetcli));
3869 * Set the top level directory permissions before we do any further copies.
3870 * Should set up ACL inheritance.
3873 bool copy_top_level_perms(struct copy_clistate *cp_clistate,
3874 const char *sharename)
3876 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3878 switch (net_mode_share) {
3879 case NET_MODE_SHARE_MIGRATE:
3880 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3881 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3882 cp_clistate->cli_share_src,
3883 cp_clistate->cli_share_dst,
3885 opt_acls? True : False,
3886 opt_attrs? True : False,
3887 opt_timestamps? True: False,
3891 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3895 if (!NT_STATUS_IS_OK(nt_status)) {
3896 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3897 sharename, nt_errstr(nt_status));
3905 * Sync all files inside a remote share to another share (over smb)
3907 * All parameters are provided by the run_rpc_command function, except for
3908 * argc, argv which are passes through.
3910 * @param domain_sid The domain sid acquired from the remote server
3911 * @param cli A cli_state connected to the server.
3912 * @param mem_ctx Talloc context, destoyed on completion of the function.
3913 * @param argc Standard main() style argc
3914 * @param argv Standard main() style argv. Initial components are already
3917 * @return Normal NTSTATUS return.
3920 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3921 const char *domain_name,
3922 struct cli_state *cli,
3923 struct rpc_pipe_client *pipe_hnd,
3924 TALLOC_CTX *mem_ctx,
3929 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3930 SRV_SHARE_INFO_CTR ctr_src;
3933 struct copy_clistate cp_clistate;
3934 bool got_src_share = False;
3935 bool got_dst_share = False;
3936 const char *mask = "\\*";
3939 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3941 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3943 if (!W_ERROR_IS_OK(result))
3946 for (i = 0; i < ctr_src.num_entries; i++) {
3948 fstring netname = "";
3950 rpcstr_pull_unistr2_fstring(
3951 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3953 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3956 /* one might not want to mirror whole discs :) */
3957 if (strequal(netname, "print$") || netname[1] == '$') {
3958 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3962 switch (net_mode_share)
3964 case NET_MODE_SHARE_MIGRATE:
3968 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3971 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3973 opt_acls ? "including" : "without",
3974 opt_attrs ? "including" : "without",
3975 opt_timestamps ? "(preserving timestamps)" : "");
3977 cp_clistate.mem_ctx = mem_ctx;
3978 cp_clistate.cli_share_src = NULL;
3979 cp_clistate.cli_share_dst = NULL;
3980 cp_clistate.cwd = NULL;
3981 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3983 /* open share source */
3984 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3985 &cli->dest_ss, cli->desthost,
3987 if (!NT_STATUS_IS_OK(nt_status))
3990 got_src_share = True;
3992 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3993 /* open share destination */
3994 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3995 NULL, dst, netname, "A:");
3996 if (!NT_STATUS_IS_OK(nt_status))
3999 got_dst_share = True;
4002 if (!copy_top_level_perms(&cp_clistate, netname)) {
4003 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
4004 nt_status = NT_STATUS_UNSUCCESSFUL;
4008 if (!sync_files(&cp_clistate, mask)) {
4009 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
4010 nt_status = NT_STATUS_UNSUCCESSFUL;
4015 nt_status = NT_STATUS_OK;
4020 cli_shutdown(cp_clistate.cli_share_src);
4023 cli_shutdown(cp_clistate.cli_share_dst);
4029 static int rpc_share_migrate_files(int argc, const char **argv)
4033 printf("no server to migrate\n");
4037 return run_rpc_command(NULL, PI_SRVSVC, 0,
4038 rpc_share_migrate_files_internals,
4043 * Migrate share-ACLs from a remote RPC server to the local RPC srever
4045 * All parameters are provided by the run_rpc_command function, except for
4046 * argc, argv which are passes through.
4048 * @param domain_sid The domain sid acquired from the remote server
4049 * @param cli A cli_state connected to the server.
4050 * @param mem_ctx Talloc context, destoyed on completion of the function.
4051 * @param argc Standard main() style argc
4052 * @param argv Standard main() style argv. Initial components are already
4055 * @return Normal NTSTATUS return.
4058 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
4059 const char *domain_name,
4060 struct cli_state *cli,
4061 struct rpc_pipe_client *pipe_hnd,
4062 TALLOC_CTX *mem_ctx,
4067 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4068 SRV_SHARE_INFO_CTR ctr_src;
4069 SRV_SHARE_INFO info;
4071 struct rpc_pipe_client *srvsvc_pipe = NULL;
4072 struct cli_state *cli_dst = NULL;
4073 uint32 level = 502; /* includes secdesc */
4075 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
4077 if (!W_ERROR_IS_OK(result))
4080 /* connect destination PI_SRVSVC */
4081 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
4082 if (!NT_STATUS_IS_OK(nt_status))
4086 for (i = 0; i < ctr_src.num_entries; i++) {
4088 fstring netname = "", remark = "", path = "";
4089 /* reset error-code */
4090 nt_status = NT_STATUS_UNSUCCESSFUL;
4092 rpcstr_pull_unistr2_fstring(
4093 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
4094 rpcstr_pull_unistr2_fstring(
4095 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
4096 rpcstr_pull_unistr2_fstring(
4097 path, &ctr_src.share.info502[i].info_502_str.uni_path);
4099 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
4102 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
4103 netname, path, remark);
4106 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
4111 info.switch_value = level;
4112 info.ptr_share_ctr = 1;
4114 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4115 info.share.info502 = ctr_src.share.info502[i];
4117 /* finally modify the share on the dst server */
4118 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
4120 if (!W_ERROR_IS_OK(result)) {
4121 printf("cannot set share-acl: %s\n", dos_errstr(result));
4127 nt_status = NT_STATUS_OK;
4131 cli_shutdown(cli_dst);
4139 * Migrate share-acls from a rpc-server to another
4141 * @param argc Standard main() style argc
4142 * @param argv Standard main() style argv. Initial components are already
4145 * @return A shell status integer (0 for success)
4147 static int rpc_share_migrate_security(int argc, const char **argv)
4151 printf("no server to migrate\n");
4155 return run_rpc_command(NULL, PI_SRVSVC, 0,
4156 rpc_share_migrate_security_internals,
4161 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4162 * from one server to another
4164 * @param argc Standard main() style argc
4165 * @param argv Standard main() style argv. Initial components are already
4168 * @return A shell status integer (0 for success)
4171 static int rpc_share_migrate_all(int argc, const char **argv)
4176 printf("no server to migrate\n");
4180 /* order is important. we don't want to be locked out by the share-acl
4181 * before copying files - gd */
4183 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
4187 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
4191 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
4196 * 'net rpc share migrate' entrypoint.
4197 * @param argc Standard main() style argc
4198 * @param argv Standard main() style argv. Initial components are already
4201 static int rpc_share_migrate(int argc, const char **argv)
4204 struct functable func[] = {
4205 {"all", rpc_share_migrate_all},
4206 {"files", rpc_share_migrate_files},
4207 {"help", rpc_share_usage},
4208 {"security", rpc_share_migrate_security},
4209 {"shares", rpc_share_migrate_shares},
4213 net_mode_share = NET_MODE_SHARE_MIGRATE;
4215 return net_run_function(argc, argv, func, rpc_share_usage);
4224 static int num_server_aliases;
4225 static struct full_alias *server_aliases;
4228 * Add an alias to the static list.
4230 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4232 if (server_aliases == NULL)
4233 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4235 server_aliases[num_server_aliases] = *alias;
4236 num_server_aliases += 1;
4240 * For a specific domain on the server, fetch all the aliases
4241 * and their members. Add all of them to the server_aliases.
4244 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4245 TALLOC_CTX *mem_ctx,
4246 POLICY_HND *connect_pol,
4247 const DOM_SID *domain_sid)
4249 uint32 start_idx, max_entries, num_entries, i;
4250 struct samr_SamArray *groups = NULL;
4252 POLICY_HND domain_pol;
4254 /* Get domain policy handle */
4256 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4258 MAXIMUM_ALLOWED_ACCESS,
4259 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4261 if (!NT_STATUS_IS_OK(result))
4268 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4274 for (i = 0; i < num_entries; i++) {
4276 POLICY_HND alias_pol;
4277 struct full_alias alias;
4278 struct lsa_SidArray sid_array;
4281 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4283 MAXIMUM_ALLOWED_ACCESS,
4284 groups->entries[i].idx,
4286 if (!NT_STATUS_IS_OK(result))
4289 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4292 if (!NT_STATUS_IS_OK(result))
4295 alias.num_members = sid_array.num_sids;
4297 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4298 if (!NT_STATUS_IS_OK(result))
4301 alias.members = NULL;
4303 if (alias.num_members > 0) {
4304 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4306 for (j = 0; j < alias.num_members; j++)
4307 sid_copy(&alias.members[j],
4308 sid_array.sids[j].sid);
4311 sid_copy(&alias.sid, domain_sid);
4312 sid_append_rid(&alias.sid, groups->entries[i].idx);
4314 push_alias(mem_ctx, &alias);
4316 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4318 result = NT_STATUS_OK;
4321 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4327 * Dump server_aliases as names for debugging purposes.
4330 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
4331 const char *domain_name,
4332 struct cli_state *cli,
4333 struct rpc_pipe_client *pipe_hnd,
4334 TALLOC_CTX *mem_ctx,
4342 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
4343 SEC_RIGHTS_MAXIMUM_ALLOWED,
4345 if (!NT_STATUS_IS_OK(result))
4348 for (i=0; i<num_server_aliases; i++) {
4351 enum lsa_SidType *types;
4354 struct full_alias *alias = &server_aliases[i];
4356 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4358 &domains, &names, &types);
4359 if (!NT_STATUS_IS_OK(result))
4362 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4364 if (alias->num_members == 0) {
4369 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4372 &domains, &names, &types);
4374 if (!NT_STATUS_IS_OK(result) &&
4375 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4378 for (j=0; j<alias->num_members; j++)
4379 DEBUG(1, ("%s\\%s (%d); ",
4380 domains[j] ? domains[j] : "*unknown*",
4381 names[j] ? names[j] : "*unknown*",types[j]));
4385 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4387 return NT_STATUS_OK;
4391 * Fetch a list of all server aliases and their members into
4395 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4396 const char *domain_name,
4397 struct cli_state *cli,
4398 struct rpc_pipe_client *pipe_hnd,
4399 TALLOC_CTX *mem_ctx,
4404 POLICY_HND connect_pol;
4406 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4407 pipe_hnd->cli->desthost,
4408 MAXIMUM_ALLOWED_ACCESS,
4411 if (!NT_STATUS_IS_OK(result))
4414 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4415 &global_sid_Builtin);
4417 if (!NT_STATUS_IS_OK(result))
4420 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4423 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4428 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4430 token->num_sids = 4;
4432 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4433 d_fprintf(stderr, "malloc failed\n");
4434 token->num_sids = 0;
4438 token->user_sids[0] = *user_sid;
4439 sid_copy(&token->user_sids[1], &global_sid_World);
4440 sid_copy(&token->user_sids[2], &global_sid_Network);
4441 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4444 static void free_user_token(NT_USER_TOKEN *token)
4446 SAFE_FREE(token->user_sids);
4449 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4453 for (i=0; i<token->num_sids; i++) {
4454 if (sid_compare(sid, &token->user_sids[i]) == 0)
4460 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4462 if (is_sid_in_token(token, sid))
4465 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4466 if (!token->user_sids) {
4470 sid_copy(&token->user_sids[token->num_sids], sid);
4472 token->num_sids += 1;
4477 NT_USER_TOKEN token;
4480 static void dump_user_token(struct user_token *token)
4484 d_printf("%s\n", token->name);
4486 for (i=0; i<token->token.num_sids; i++) {
4487 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4491 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4495 for (i=0; i<alias->num_members; i++) {
4496 if (sid_compare(sid, &alias->members[i]) == 0)
4503 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4507 for (i=0; i<num_server_aliases; i++) {
4508 if (is_alias_member(&sid, &server_aliases[i]))
4509 add_sid_to_token(token, &server_aliases[i].sid);
4514 * We got a user token with all the SIDs we can know about without asking the
4515 * server directly. These are the user and domain group sids. All of these can
4516 * be members of aliases. So scan the list of aliases for each of the SIDs and
4517 * add them to the token.
4520 static void collect_alias_memberships(NT_USER_TOKEN *token)
4522 int num_global_sids = token->num_sids;
4525 for (i=0; i<num_global_sids; i++) {
4526 collect_sid_memberships(token, token->user_sids[i]);
4530 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4532 struct winbindd_request request;
4533 struct winbindd_response response;
4541 fstr_sprintf(full_name, "%s%c%s",
4542 domain, *lp_winbind_separator(), user);
4544 /* First let's find out the user sid */
4546 ZERO_STRUCT(request);
4547 ZERO_STRUCT(response);
4549 fstrcpy(request.data.name.dom_name, domain);
4550 fstrcpy(request.data.name.name, user);
4552 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4554 if (result != NSS_STATUS_SUCCESS) {
4555 DEBUG(1, ("winbind could not find %s\n", full_name));
4559 if (response.data.sid.type != SID_NAME_USER) {
4560 DEBUG(1, ("%s is not a user\n", full_name));
4564 string_to_sid(&user_sid, response.data.sid.sid);
4566 init_user_token(token, &user_sid);
4568 /* And now the groups winbind knows about */
4570 ZERO_STRUCT(response);
4572 fstrcpy(request.data.username, full_name);
4574 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4576 if (result != NSS_STATUS_SUCCESS) {
4577 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4581 for (i = 0; i < response.data.num_entries; i++) {
4582 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4585 struct winbindd_request sidrequest;
4586 struct winbindd_response sidresponse;
4588 ZERO_STRUCT(sidrequest);
4589 ZERO_STRUCT(sidresponse);
4591 sidrequest.data.gid = gid;
4593 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4594 &sidrequest, &sidresponse);
4596 if (result != NSS_STATUS_SUCCESS) {
4597 DEBUG(1, ("winbind could not find SID of gid %d\n",
4602 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4604 string_to_sid(&sid, sidresponse.data.sid.sid);
4605 add_sid_to_token(token, &sid);
4608 SAFE_FREE(response.extra_data.data);
4614 * Get a list of all user tokens we want to look at
4617 static bool get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4619 struct winbindd_request request;
4620 struct winbindd_response response;
4621 const char *extra_data;
4624 struct user_token *result;
4625 TALLOC_CTX *frame = NULL;
4627 if (lp_winbind_use_default_domain() &&
4628 (opt_target_workgroup == NULL)) {
4629 d_fprintf(stderr, "winbind use default domain = yes set, "
4630 "please specify a workgroup\n");
4634 /* Send request to winbind daemon */
4636 ZERO_STRUCT(request);
4637 ZERO_STRUCT(response);
4639 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4643 /* Look through extra data */
4645 if (!response.extra_data.data)
4648 extra_data = (const char *)response.extra_data.data;
4651 frame = talloc_stackframe();
4652 while(next_token_talloc(frame, &extra_data, &name, ",")) {
4656 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4658 if (result == NULL) {
4659 DEBUG(1, ("Could not malloc sid array\n"));
4664 extra_data = (const char *)response.extra_data.data;
4667 while(next_token_talloc(frame, &extra_data, &name, ",")) {
4668 fstring domain, user;
4671 fstrcpy(result[i].name, name);
4673 p = strchr(name, *lp_winbind_separator());
4675 DEBUG(3, ("%s\n", name));
4678 fstrcpy(domain, opt_target_workgroup);
4679 fstrcpy(user, name);
4682 fstrcpy(domain, name);
4687 get_user_sids(domain, user, &(result[i].token));
4691 SAFE_FREE(response.extra_data.data);
4693 *user_tokens = result;
4698 static bool get_user_tokens_from_file(FILE *f,
4700 struct user_token **tokens)
4702 struct user_token *token = NULL;
4707 if (fgets(line, sizeof(line)-1, f) == NULL) {
4711 if (line[strlen(line)-1] == '\n')
4712 line[strlen(line)-1] = '\0';
4714 if (line[0] == ' ') {
4718 string_to_sid(&sid, &line[1]);
4720 if (token == NULL) {
4721 DEBUG(0, ("File does not begin with username"));
4725 add_sid_to_token(&token->token, &sid);
4729 /* And a new user... */
4732 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4733 if (*tokens == NULL) {
4734 DEBUG(0, ("Could not realloc tokens\n"));
4738 token = &((*tokens)[*num_tokens-1]);
4740 fstrcpy(token->name, line);
4741 token->token.num_sids = 0;
4742 token->token.user_sids = NULL;
4751 * Show the list of all users that have access to a share
4754 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4755 TALLOC_CTX *mem_ctx,
4756 const char *netname,
4758 struct user_token *tokens)
4761 SEC_DESC *share_sd = NULL;
4762 SEC_DESC *root_sd = NULL;
4763 struct cli_state *cli = pipe_hnd->cli;
4765 SRV_SHARE_INFO info;
4769 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4772 if (!W_ERROR_IS_OK(result)) {
4773 DEBUG(1, ("Coult not query secdesc for share %s\n",
4778 share_sd = info.share.info502.info_502_str.sd;
4779 if (share_sd == NULL) {
4780 DEBUG(1, ("Got no secdesc for share %s\n",
4786 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4790 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4793 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4796 for (i=0; i<num_tokens; i++) {
4800 if (share_sd != NULL) {
4801 if (!se_access_check(share_sd, &tokens[i].token,
4802 1, &acc_granted, &status)) {
4803 DEBUG(1, ("Could not check share_sd for "
4809 if (!NT_STATUS_IS_OK(status))
4813 if (root_sd == NULL) {
4814 d_printf(" %s\n", tokens[i].name);
4818 if (!se_access_check(root_sd, &tokens[i].token,
4819 1, &acc_granted, &status)) {
4820 DEBUG(1, ("Could not check root_sd for user %s\n",
4825 if (!NT_STATUS_IS_OK(status))
4828 d_printf(" %s\n", tokens[i].name);
4832 cli_close(cli, fnum);
4844 static void collect_share(const char *name, uint32 m,
4845 const char *comment, void *state)
4847 struct share_list *share_list = (struct share_list *)state;
4849 if (m != STYPE_DISKTREE)
4852 share_list->num_shares += 1;
4853 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4854 if (!share_list->shares) {
4855 share_list->num_shares = 0;
4858 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4861 static void rpc_share_userlist_usage(void)
4867 * List shares on a remote RPC server, including the security descriptors
4869 * All parameters are provided by the run_rpc_command function, except for
4870 * argc, argv which are passes through.
4872 * @param domain_sid The domain sid acquired from the remote server
4873 * @param cli A cli_state connected to the server.
4874 * @param mem_ctx Talloc context, destoyed on completion of the function.
4875 * @param argc Standard main() style argc
4876 * @param argv Standard main() style argv. Initial components are already
4879 * @return Normal NTSTATUS return.
4882 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4883 const char *domain_name,
4884 struct cli_state *cli,
4885 struct rpc_pipe_client *pipe_hnd,
4886 TALLOC_CTX *mem_ctx,
4896 struct user_token *tokens = NULL;
4899 struct share_list share_list;
4902 rpc_share_userlist_usage();
4903 return NT_STATUS_UNSUCCESSFUL;
4909 f = fopen(argv[0], "r");
4913 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4914 return NT_STATUS_UNSUCCESSFUL;
4917 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4923 DEBUG(0, ("Could not read users from file\n"));
4924 return NT_STATUS_UNSUCCESSFUL;
4927 for (i=0; i<num_tokens; i++)
4928 collect_alias_memberships(&tokens[i].token);
4930 init_enum_hnd(&hnd, 0);
4932 share_list.num_shares = 0;
4933 share_list.shares = NULL;
4935 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4938 DEBUG(0, ("Error returning browse list: %s\n",
4943 for (i = 0; i < share_list.num_shares; i++) {
4944 char *netname = share_list.shares[i];
4946 if (netname[strlen(netname)-1] == '$')
4949 d_printf("%s\n", netname);
4951 show_userlist(pipe_hnd, mem_ctx, netname,
4952 num_tokens, tokens);
4955 for (i=0; i<num_tokens; i++) {
4956 free_user_token(&tokens[i].token);
4959 SAFE_FREE(share_list.shares);
4961 return NT_STATUS_OK;
4964 static int rpc_share_allowedusers(int argc, const char **argv)
4968 result = run_rpc_command(NULL, PI_SAMR, 0,
4969 rpc_aliaslist_internals,
4974 result = run_rpc_command(NULL, PI_LSARPC, 0,
4980 return run_rpc_command(NULL, PI_SRVSVC, 0,
4981 rpc_share_allowedusers_internals,
4985 int net_usersidlist(int argc, const char **argv)
4988 struct user_token *tokens = NULL;
4992 net_usersidlist_usage(argc, argv);
4996 if (!get_user_tokens(&num_tokens, &tokens)) {
4997 DEBUG(0, ("Could not get the user/sid list\n"));
5001 for (i=0; i<num_tokens; i++) {
5002 dump_user_token(&tokens[i]);
5003 free_user_token(&tokens[i].token);
5010 int net_usersidlist_usage(int argc, const char **argv)
5012 d_printf("net usersidlist\n"
5013 "\tprints out a list of all users the running winbind knows\n"
5014 "\tabout, together with all their SIDs. This is used as\n"
5015 "\tinput to the 'net rpc share allowedusers' command.\n\n");
5017 net_common_flags_usage(argc, argv);
5022 * 'net rpc share' entrypoint.
5023 * @param argc Standard main() style argc
5024 * @param argv Standard main() style argv. Initial components are already
5028 int net_rpc_share(int argc, const char **argv)
5030 struct functable func[] = {
5031 {"add", rpc_share_add},
5032 {"delete", rpc_share_delete},
5033 {"allowedusers", rpc_share_allowedusers},
5034 {"migrate", rpc_share_migrate},
5035 {"list", rpc_share_list},
5040 return run_rpc_command(NULL, PI_SRVSVC, 0,
5041 rpc_share_list_internals,
5044 return net_run_function(argc, argv, func, rpc_share_usage);
5047 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
5048 struct rpc_sh_ctx *ctx,
5049 struct rpc_pipe_client *pipe_hnd,
5050 int argc, const char **argv)
5052 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
5053 ctx->cli, pipe_hnd, mem_ctx,
5057 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
5058 struct rpc_sh_ctx *ctx,
5059 struct rpc_pipe_client *pipe_hnd,
5060 int argc, const char **argv)
5064 if ((argc < 2) || (argc > 3)) {
5065 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
5067 return NT_STATUS_INVALID_PARAMETER;
5070 result = rpccli_srvsvc_net_share_add(
5071 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
5072 (argc == 3) ? argv[2] : "",
5073 0, 0, 0, argv[1], NULL, 2, NULL);
5075 return werror_to_ntstatus(result);
5078 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
5079 struct rpc_sh_ctx *ctx,
5080 struct rpc_pipe_client *pipe_hnd,
5081 int argc, const char **argv)
5086 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5087 return NT_STATUS_INVALID_PARAMETER;
5090 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
5091 return werror_to_ntstatus(result);
5094 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
5095 struct rpc_sh_ctx *ctx,
5096 struct rpc_pipe_client *pipe_hnd,
5097 int argc, const char **argv)
5099 SRV_SHARE_INFO info;
5100 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
5104 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5105 return NT_STATUS_INVALID_PARAMETER;
5108 result = rpccli_srvsvc_net_share_get_info(
5109 pipe_hnd, mem_ctx, argv[0], 2, &info);
5110 if (!W_ERROR_IS_OK(result)) {
5114 d_printf("Name: %s\n",
5115 rpcstr_pull_unistr2_talloc(mem_ctx,
5116 &info2->info_2_str.uni_netname));
5117 d_printf("Comment: %s\n",
5118 rpcstr_pull_unistr2_talloc(mem_ctx,
5119 &info2->info_2_str.uni_remark));
5121 d_printf("Path: %s\n",
5122 rpcstr_pull_unistr2_talloc(mem_ctx,
5123 &info2->info_2_str.uni_path));
5124 d_printf("Password: %s\n",
5125 rpcstr_pull_unistr2_talloc(mem_ctx,
5126 &info2->info_2_str.uni_passwd));
5129 return werror_to_ntstatus(result);
5132 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
5133 struct rpc_sh_ctx *ctx)
5135 static struct rpc_sh_cmd cmds[] = {
5137 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
5138 "List available shares" },
5140 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
5143 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
5146 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
5147 "Get information about a share" },
5149 { NULL, NULL, 0, NULL, NULL }
5155 /****************************************************************************/
5157 static int rpc_file_usage(int argc, const char **argv)
5159 return net_help_file(argc, argv);
5163 * Close a file on a remote RPC server
5165 * All parameters are provided by the run_rpc_command function, except for
5166 * argc, argv which are passes through.
5168 * @param domain_sid The domain sid acquired from the remote server
5169 * @param cli A cli_state connected to the server.
5170 * @param mem_ctx Talloc context, destoyed on completion of the function.
5171 * @param argc Standard main() style argc
5172 * @param argv Standard main() style argv. Initial components are already
5175 * @return Normal NTSTATUS return.
5177 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
5178 const char *domain_name,
5179 struct cli_state *cli,
5180 struct rpc_pipe_client *pipe_hnd,
5181 TALLOC_CTX *mem_ctx,
5185 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5186 pipe_hnd->cli->desthost,
5187 atoi(argv[0]), NULL);
5191 * Close a file on a remote RPC server
5193 * @param argc Standard main() style argc
5194 * @param argv Standard main() style argv. Initial components are already
5197 * @return A shell status integer (0 for success)
5199 static int rpc_file_close(int argc, const char **argv)
5202 DEBUG(1, ("No fileid given on close\n"));
5203 return(rpc_file_usage(argc, argv));
5206 return run_rpc_command(NULL, PI_SRVSVC, 0,
5207 rpc_file_close_internals,
5212 * Formatted print of open file info
5214 * @param info3 FILE_INFO_3 contents
5215 * @param str3 strings for FILE_INFO_3
5218 static void display_file_info_3( FILE_INFO_3 *info3 )
5220 fstring user = "", path = "";
5222 rpcstr_pull_unistr2_fstring(user, info3->user);
5223 rpcstr_pull_unistr2_fstring(path, info3->path);
5225 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5226 info3->id, user, info3->perms, info3->num_locks, path);
5230 * List open files on a remote RPC server
5232 * All parameters are provided by the run_rpc_command function, except for
5233 * argc, argv which are passes through.
5235 * @param domain_sid The domain sid acquired from the remote server
5236 * @param cli A cli_state connected to the server.
5237 * @param mem_ctx Talloc context, destoyed on completion of the function.
5238 * @param argc Standard main() style argc
5239 * @param argv Standard main() style argv. Initial components are already
5242 * @return Normal NTSTATUS return.
5245 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
5246 const char *domain_name,
5247 struct cli_state *cli,
5248 struct rpc_pipe_client *pipe_hnd,
5249 TALLOC_CTX *mem_ctx,
5253 SRV_FILE_INFO_CTR ctr;
5256 uint32 preferred_len = 0xffffffff, i;
5257 const char *username=NULL;
5259 init_enum_hnd(&hnd, 0);
5261 /* if argc > 0, must be user command */
5263 username = smb_xstrdup(argv[0]);
5265 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
5266 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
5268 if (!W_ERROR_IS_OK(result))
5271 /* Display results */
5274 "\nEnumerating open files on remote server:\n\n"\
5275 "\nFileId Opened by Perms Locks Path"\
5276 "\n------ --------- ----- ----- ---- \n");
5277 for (i = 0; i < ctr.num_entries; i++)
5278 display_file_info_3(&ctr.file.info3[i]);
5280 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5284 * List files for a user on a remote RPC server
5286 * @param argc Standard main() style argc
5287 * @param argv Standard main() style argv. Initial components are already
5290 * @return A shell status integer (0 for success)
5293 static int rpc_file_user(int argc, const char **argv)
5296 DEBUG(1, ("No username given\n"));
5297 return(rpc_file_usage(argc, argv));
5300 return run_rpc_command(NULL, PI_SRVSVC, 0,
5301 rpc_file_list_internals,
5306 * 'net rpc file' entrypoint.
5307 * @param argc Standard main() style argc
5308 * @param argv Standard main() style argv. Initial components are already
5312 int net_rpc_file(int argc, const char **argv)
5314 struct functable func[] = {
5315 {"close", rpc_file_close},
5316 {"user", rpc_file_user},
5318 {"info", rpc_file_info},
5324 return run_rpc_command(NULL, PI_SRVSVC, 0,
5325 rpc_file_list_internals,
5328 return net_run_function(argc, argv, func, rpc_file_usage);
5332 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5334 * All parameters are provided by the run_rpc_command function, except for
5335 * argc, argv which are passed through.
5337 * @param domain_sid The domain sid aquired from the remote server
5338 * @param cli A cli_state connected to the server.
5339 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5340 * @param argc Standard main() style argc
5341 * @param argv Standard main() style argv. Initial components are already
5344 * @return Normal NTSTATUS return.
5347 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
5348 const char *domain_name,
5349 struct cli_state *cli,
5350 struct rpc_pipe_client *pipe_hnd,
5351 TALLOC_CTX *mem_ctx,
5355 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5357 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5359 if (NT_STATUS_IS_OK(result)) {
5360 d_printf("\nShutdown successfully aborted\n");
5361 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5363 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5369 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5371 * All parameters are provided by the run_rpc_command function, except for
5372 * argc, argv which are passed through.
5374 * @param domain_sid The domain sid aquired from the remote server
5375 * @param cli A cli_state connected to the server.
5376 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5377 * @param argc Standard main() style argc
5378 * @param argv Standard main() style argv. Initial components are already
5381 * @return Normal NTSTATUS return.
5384 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5385 const char *domain_name,
5386 struct cli_state *cli,
5387 struct rpc_pipe_client *pipe_hnd,
5388 TALLOC_CTX *mem_ctx,
5392 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5394 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5396 if (NT_STATUS_IS_OK(result)) {
5397 d_printf("\nShutdown successfully aborted\n");
5398 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5400 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5406 * ABORT the Shut down of a remote RPC server
5408 * @param argc Standard main() style argc
5409 * @param argv Standard main() style argv. Initial components are already
5412 * @return A shell status integer (0 for success)
5415 static int rpc_shutdown_abort(int argc, const char **argv)
5417 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5418 rpc_shutdown_abort_internals,
5424 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5426 return run_rpc_command(NULL, PI_WINREG, 0,
5427 rpc_reg_shutdown_abort_internals,
5432 * Shut down a remote RPC Server via initshutdown pipe
5434 * All parameters are provided by the run_rpc_command function, except for
5435 * argc, argv which are passes through.
5437 * @param domain_sid The domain sid aquired from the remote server
5438 * @param cli A cli_state connected to the server.
5439 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5440 * @param argc Standard main() style argc
5441 * @param argc Standard main() style argv. Initial components are already
5444 * @return Normal NTSTATUS return.
5447 NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5448 const char *domain_name,
5449 struct cli_state *cli,
5450 struct rpc_pipe_client *pipe_hnd,
5451 TALLOC_CTX *mem_ctx,
5455 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5456 const char *msg = "This machine will be shutdown shortly";
5457 uint32 timeout = 20;
5458 struct initshutdown_String msg_string;
5459 struct initshutdown_String_sub s;
5465 timeout = opt_timeout;
5469 msg_string.name = &s;
5471 /* create an entry */
5472 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5473 &msg_string, timeout, opt_force, opt_reboot, NULL);
5475 if (NT_STATUS_IS_OK(result)) {
5476 d_printf("\nShutdown of remote machine succeeded\n");
5477 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5479 DEBUG(1,("Shutdown of remote machine failed!\n"));
5485 * Shut down a remote RPC Server via winreg pipe
5487 * All parameters are provided by the run_rpc_command function, except for
5488 * argc, argv which are passes through.
5490 * @param domain_sid The domain sid aquired from the remote server
5491 * @param cli A cli_state connected to the server.
5492 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5493 * @param argc Standard main() style argc
5494 * @param argc Standard main() style argv. Initial components are already
5497 * @return Normal NTSTATUS return.
5500 NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5501 const char *domain_name,
5502 struct cli_state *cli,
5503 struct rpc_pipe_client *pipe_hnd,
5504 TALLOC_CTX *mem_ctx,
5508 const char *msg = "This machine will be shutdown shortly";
5509 uint32 timeout = 20;
5510 struct initshutdown_String msg_string;
5511 struct initshutdown_String_sub s;
5519 msg_string.name = &s;
5522 timeout = opt_timeout;
5525 /* create an entry */
5526 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5527 &msg_string, timeout, opt_force, opt_reboot, &werr);
5529 if (NT_STATUS_IS_OK(result)) {
5530 d_printf("\nShutdown of remote machine succeeded\n");
5532 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5533 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5534 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5536 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5543 * Shut down a remote RPC server
5545 * @param argc Standard main() style argc
5546 * @param argc Standard main() style argv. Initial components are already
5549 * @return A shell status integer (0 for success)
5552 static int rpc_shutdown(int argc, const char **argv)
5554 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5555 rpc_init_shutdown_internals,
5559 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5560 rc = run_rpc_command(NULL, PI_WINREG, 0,
5561 rpc_reg_shutdown_internals, argc, argv);
5567 /***************************************************************************
5568 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5570 ***************************************************************************/
5573 * Add interdomain trust account to the RPC server.
5574 * All parameters (except for argc and argv) are passed by run_rpc_command
5577 * @param domain_sid The domain sid acquired from the server
5578 * @param cli A cli_state connected to the server.
5579 * @param mem_ctx Talloc context, destoyed on completion of the function.
5580 * @param argc Standard main() style argc
5581 * @param argc Standard main() style argv. Initial components are already
5584 * @return normal NTSTATUS return code
5587 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5588 const char *domain_name,
5589 struct cli_state *cli,
5590 struct rpc_pipe_client *pipe_hnd,
5591 TALLOC_CTX *mem_ctx,
5595 POLICY_HND connect_pol, domain_pol, user_pol;
5596 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5598 struct lsa_String lsa_acct_name;
5600 uint32 acct_flags=0;
5602 uint32_t access_granted = 0;
5605 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5606 return NT_STATUS_INVALID_PARAMETER;
5610 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5613 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5614 return NT_STATUS_NO_MEMORY;
5617 strupper_m(acct_name);
5619 init_lsa_String(&lsa_acct_name, acct_name);
5621 /* Get samr policy handle */
5622 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5623 pipe_hnd->cli->desthost,
5624 MAXIMUM_ALLOWED_ACCESS,
5626 if (!NT_STATUS_IS_OK(result)) {
5630 /* Get domain policy handle */
5631 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5633 MAXIMUM_ALLOWED_ACCESS,
5634 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5636 if (!NT_STATUS_IS_OK(result)) {
5640 /* Create trusting domain's account */
5641 acb_info = ACB_NORMAL;
5642 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5643 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5644 SAMR_USER_ACCESS_SET_PASSWORD |
5645 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5646 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5648 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5656 if (!NT_STATUS_IS_OK(result)) {
5661 SAM_USERINFO_CTR ctr;
5662 SAM_USER_INFO_23 p23;
5668 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5672 ZERO_STRUCT(notime);
5676 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5677 acb_info = ACB_DOMTRUST;
5679 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5680 ¬ime, ¬ime, ¬ime,
5681 nostr, nostr, nostr, nostr, nostr,
5682 nostr, nostr, nostr, nostr, nostr,
5683 0, 0, acb_info, SAMR_FIELD_ACCT_FLAGS, 168, &hrs,
5684 0, 0, (char *)pwbuf);
5685 ctr.switch_value = 23;
5686 ctr.info.id23 = &p23;
5687 p23.passmustchange = 0;
5689 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5690 &cli->user_session_key, &ctr);
5692 if (!NT_STATUS_IS_OK(result)) {
5693 DEBUG(0,("Could not set trust account password: %s\n",
5694 nt_errstr(result)));
5700 SAFE_FREE(acct_name);
5705 * Create interdomain trust account for a remote domain.
5707 * @param argc standard argc
5708 * @param argv standard argv without initial components
5710 * @return Integer status (0 means success)
5713 static int rpc_trustdom_add(int argc, const char **argv)
5716 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5719 d_printf("Usage: net rpc trustdom add <domain>\n");
5726 * Remove interdomain trust account from the RPC server.
5727 * All parameters (except for argc and argv) are passed by run_rpc_command
5730 * @param domain_sid The domain sid acquired from the server
5731 * @param cli A cli_state connected to the server.
5732 * @param mem_ctx Talloc context, destoyed on completion of the function.
5733 * @param argc Standard main() style argc
5734 * @param argc Standard main() style argv. Initial components are already
5737 * @return normal NTSTATUS return code
5740 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5741 const char *domain_name,
5742 struct cli_state *cli,
5743 struct rpc_pipe_client *pipe_hnd,
5744 TALLOC_CTX *mem_ctx,
5748 POLICY_HND connect_pol, domain_pol, user_pol;
5749 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5751 DOM_SID trust_acct_sid;
5752 struct samr_Ids user_rids, name_types;
5753 struct lsa_String lsa_acct_name;
5756 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5757 return NT_STATUS_INVALID_PARAMETER;
5761 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5763 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5765 if (acct_name == NULL)
5766 return NT_STATUS_NO_MEMORY;
5768 strupper_m(acct_name);
5770 /* Get samr policy handle */
5771 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5772 pipe_hnd->cli->desthost,
5773 MAXIMUM_ALLOWED_ACCESS,
5775 if (!NT_STATUS_IS_OK(result)) {
5779 /* Get domain policy handle */
5780 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5782 MAXIMUM_ALLOWED_ACCESS,
5783 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5785 if (!NT_STATUS_IS_OK(result)) {
5789 init_lsa_String(&lsa_acct_name, acct_name);
5791 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5798 if (!NT_STATUS_IS_OK(result)) {
5802 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5804 MAXIMUM_ALLOWED_ACCESS,
5808 if (!NT_STATUS_IS_OK(result)) {
5812 /* append the rid to the domain sid */
5813 sid_copy(&trust_acct_sid, domain_sid);
5814 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5818 /* remove the sid */
5820 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5823 if (!NT_STATUS_IS_OK(result)) {
5829 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5832 if (!NT_STATUS_IS_OK(result)) {
5836 if (!NT_STATUS_IS_OK(result)) {
5837 DEBUG(0,("Could not set trust account password: %s\n",
5838 nt_errstr(result)));
5847 * Delete interdomain trust account for a remote domain.
5849 * @param argc standard argc
5850 * @param argv standard argv without initial components
5852 * @return Integer status (0 means success)
5855 static int rpc_trustdom_del(int argc, const char **argv)
5858 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5861 d_printf("Usage: net rpc trustdom del <domain>\n");
5868 * Establish trust relationship to a trusting domain.
5869 * Interdomain account must already be created on remote PDC.
5871 * @param argc standard argc
5872 * @param argv standard argv without initial components
5874 * @return Integer status (0 means success)
5877 static int rpc_trustdom_establish(int argc, const char **argv)
5879 struct cli_state *cli = NULL;
5880 struct sockaddr_storage server_ss;
5881 struct rpc_pipe_client *pipe_hnd = NULL;
5882 POLICY_HND connect_hnd;
5883 TALLOC_CTX *mem_ctx;
5885 DOM_SID *domain_sid;
5891 union lsa_PolicyInformation *info = NULL;
5894 * Connect to \\server\ipc$ as 'our domain' account with password
5898 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5902 domain_name = smb_xstrdup(argv[0]);
5903 strupper_m(domain_name);
5905 /* account name used at first is our domain's name with '$' */
5906 asprintf(&acct_name, "%s$", lp_workgroup());
5907 strupper_m(acct_name);
5910 * opt_workgroup will be used by connection functions further,
5911 * hence it should be set to remote domain name instead of ours
5913 if (opt_workgroup) {
5914 opt_workgroup = smb_xstrdup(domain_name);
5917 opt_user_name = acct_name;
5919 /* find the domain controller */
5920 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5921 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5925 /* connect to ipc$ as username/password */
5926 nt_status = connect_to_ipc(&cli, &server_ss, pdc_name);
5927 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5929 /* Is it trusting domain account for sure ? */
5930 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5931 nt_errstr(nt_status)));
5935 /* store who we connected to */
5937 saf_store( domain_name, pdc_name );
5940 * Connect to \\server\ipc$ again (this time anonymously)
5943 nt_status = connect_to_ipc_anonymous(&cli, &server_ss, (char*)pdc_name);
5945 if (NT_STATUS_IS_ERR(nt_status)) {
5946 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5947 domain_name, nt_errstr(nt_status)));
5952 * Use NetServerEnum2 to make sure we're talking to a proper server
5955 if (!cli_get_pdc_name(cli, domain_name, &dc_name)) {
5956 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5957 for domain %s\n", domain_name));
5963 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5964 "domain %s", domain_name))) {
5965 DEBUG(0, ("talloc_init() failed\n"));
5971 * Call LsaOpenPolicy and LsaQueryInfo
5974 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5976 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5978 talloc_destroy(mem_ctx);
5982 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5984 if (NT_STATUS_IS_ERR(nt_status)) {
5985 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5986 nt_errstr(nt_status)));
5988 talloc_destroy(mem_ctx);
5992 /* Querying info level 5 */
5994 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5996 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5998 if (NT_STATUS_IS_ERR(nt_status)) {
5999 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6000 nt_errstr(nt_status)));
6002 talloc_destroy(mem_ctx);
6006 domain_sid = info->account_domain.sid;
6008 /* There should be actually query info level 3 (following nt serv behaviour),
6009 but I still don't know if it's _really_ necessary */
6012 * Store the password in secrets db
6015 if (!pdb_set_trusteddom_pw(domain_name, opt_password, domain_sid)) {
6016 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6018 talloc_destroy(mem_ctx);
6023 * Close the pipes and clean up
6026 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6027 if (NT_STATUS_IS_ERR(nt_status)) {
6028 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6029 nt_errstr(nt_status)));
6031 talloc_destroy(mem_ctx);
6037 talloc_destroy(mem_ctx);
6039 d_printf("Trust to domain %s established\n", domain_name);
6044 * Revoke trust relationship to the remote domain
6046 * @param argc standard argc
6047 * @param argv standard argv without initial components
6049 * @return Integer status (0 means success)
6052 static int rpc_trustdom_revoke(int argc, const char **argv)
6057 if (argc < 1) return -1;
6059 /* generate upper cased domain name */
6060 domain_name = smb_xstrdup(argv[0]);
6061 strupper_m(domain_name);
6063 /* delete password of the trust */
6064 if (!pdb_del_trusteddom_pw(domain_name)) {
6065 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6072 SAFE_FREE(domain_name);
6077 * Usage for 'net rpc trustdom' command
6079 * @param argc standard argc
6080 * @param argv standard argv without inital components
6082 * @return Integer status returned to shell
6085 static int rpc_trustdom_usage(int argc, const char **argv)
6087 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
6088 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
6089 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
6090 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
6091 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
6092 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
6097 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
6098 const char *domain_name,
6099 struct cli_state *cli,
6100 struct rpc_pipe_client *pipe_hnd,
6101 TALLOC_CTX *mem_ctx,
6106 sid_to_fstring(str_sid, domain_sid);
6107 d_printf("%s\n", str_sid);
6108 return NT_STATUS_OK;
6111 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6113 fstring ascii_sid, padding;
6114 int pad_len, col_len = 20;
6116 /* convert sid into ascii string */
6117 sid_to_fstring(ascii_sid, dom_sid);
6119 /* calculate padding space for d_printf to look nicer */
6120 pad_len = col_len - strlen(trusted_dom_name);
6121 padding[pad_len] = 0;
6122 do padding[--pad_len] = ' '; while (pad_len);
6124 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6127 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6128 TALLOC_CTX *mem_ctx,
6131 const char *trusted_dom_name)
6134 union lsa_TrustedDomainInfo info;
6135 char *cleartextpwd = NULL;
6138 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6141 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6143 if (NT_STATUS_IS_ERR(nt_status)) {
6144 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6145 nt_errstr(nt_status)));
6149 data = data_blob(NULL, info.password.password->length);
6152 info.password.password->data,
6153 info.password.password->length);
6154 data.length = info.password.password->length;
6156 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password,
6159 if (cleartextpwd == NULL) {
6160 DEBUG(0,("retrieved NULL password\n"));
6161 nt_status = NT_STATUS_UNSUCCESSFUL;
6165 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6166 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6167 nt_status = NT_STATUS_UNSUCCESSFUL;
6171 #ifdef DEBUG_PASSWORD
6172 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], "
6173 "password: [%s]\n", trusted_dom_name,
6174 sid_string_dbg(&dom_sid), cleartextpwd));
6178 SAFE_FREE(cleartextpwd);
6179 data_blob_free(&data);
6184 static int rpc_trustdom_vampire(int argc, const char **argv)
6186 /* common variables */
6187 TALLOC_CTX* mem_ctx;
6188 struct cli_state *cli = NULL;
6189 struct rpc_pipe_client *pipe_hnd = NULL;
6191 const char *domain_name = NULL;
6192 DOM_SID *queried_dom_sid;
6193 POLICY_HND connect_hnd;
6194 union lsa_PolicyInformation *info = NULL;
6196 /* trusted domains listing variables */
6197 unsigned int num_domains, enum_ctx = 0;
6199 DOM_SID *domain_sids;
6200 char **trusted_dom_names;
6204 * Listing trusted domains (stored in secrets.tdb, if local)
6207 mem_ctx = talloc_init("trust relationships vampire");
6210 * set domain and pdc name to local samba server (default)
6211 * or to remote one given in command line
6214 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6215 domain_name = opt_workgroup;
6216 opt_target_workgroup = opt_workgroup;
6218 fstrcpy(pdc_name, global_myname());
6219 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6220 opt_target_workgroup = domain_name;
6223 /* open \PIPE\lsarpc and open policy handle */
6224 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6225 if (!NT_STATUS_IS_OK(nt_status)) {
6226 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6227 nt_errstr(nt_status)));
6228 talloc_destroy(mem_ctx);
6232 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6234 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6235 nt_errstr(nt_status) ));
6237 talloc_destroy(mem_ctx);
6241 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6243 if (NT_STATUS_IS_ERR(nt_status)) {
6244 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6245 nt_errstr(nt_status)));
6247 talloc_destroy(mem_ctx);
6251 /* query info level 5 to obtain sid of a domain being queried */
6252 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6254 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6257 if (NT_STATUS_IS_ERR(nt_status)) {
6258 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6259 nt_errstr(nt_status)));
6261 talloc_destroy(mem_ctx);
6265 queried_dom_sid = info->account_domain.sid;
6268 * Keep calling LsaEnumTrustdom over opened pipe until
6269 * the end of enumeration is reached
6272 d_printf("Vampire trusted domains:\n\n");
6275 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6277 &trusted_dom_names, &domain_sids);
6279 if (NT_STATUS_IS_ERR(nt_status)) {
6280 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6281 nt_errstr(nt_status)));
6283 talloc_destroy(mem_ctx);
6287 for (i = 0; i < num_domains; i++) {
6289 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6291 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6292 domain_sids[i], trusted_dom_names[i]);
6293 if (!NT_STATUS_IS_OK(nt_status)) {
6295 talloc_destroy(mem_ctx);
6301 * in case of no trusted domains say something rather
6302 * than just display blank line
6304 if (!num_domains) d_printf("none\n");
6306 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6308 /* close this connection before doing next one */
6309 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6310 if (NT_STATUS_IS_ERR(nt_status)) {
6311 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6312 nt_errstr(nt_status)));
6314 talloc_destroy(mem_ctx);
6318 /* close lsarpc pipe and connection to IPC$ */
6321 talloc_destroy(mem_ctx);
6325 static int rpc_trustdom_list(int argc, const char **argv)
6327 /* common variables */
6328 TALLOC_CTX* mem_ctx;
6329 struct cli_state *cli = NULL, *remote_cli = NULL;
6330 struct rpc_pipe_client *pipe_hnd = NULL;
6332 const char *domain_name = NULL;
6333 DOM_SID *queried_dom_sid;
6335 int ascii_dom_name_len;
6336 POLICY_HND connect_hnd;
6337 union lsa_PolicyInformation *info = NULL;
6339 /* trusted domains listing variables */
6340 unsigned int num_domains, enum_ctx = 0;
6341 int i, pad_len, col_len = 20;
6342 DOM_SID *domain_sids;
6343 char **trusted_dom_names;
6346 /* trusting domains listing variables */
6347 POLICY_HND domain_hnd;
6348 char **trusting_dom_names;
6349 uint32 *trusting_dom_rids;
6352 * Listing trusted domains (stored in secrets.tdb, if local)
6355 mem_ctx = talloc_init("trust relationships listing");
6358 * set domain and pdc name to local samba server (default)
6359 * or to remote one given in command line
6362 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6363 domain_name = opt_workgroup;
6364 opt_target_workgroup = opt_workgroup;
6366 fstrcpy(pdc_name, global_myname());
6367 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6368 opt_target_workgroup = domain_name;
6371 /* open \PIPE\lsarpc and open policy handle */
6372 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6373 if (!NT_STATUS_IS_OK(nt_status)) {
6374 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6375 nt_errstr(nt_status)));
6376 talloc_destroy(mem_ctx);
6380 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6382 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6383 nt_errstr(nt_status) ));
6385 talloc_destroy(mem_ctx);
6389 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6391 if (NT_STATUS_IS_ERR(nt_status)) {
6392 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6393 nt_errstr(nt_status)));
6395 talloc_destroy(mem_ctx);
6399 /* query info level 5 to obtain sid of a domain being queried */
6400 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6402 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6405 if (NT_STATUS_IS_ERR(nt_status)) {
6406 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6407 nt_errstr(nt_status)));
6409 talloc_destroy(mem_ctx);
6413 queried_dom_sid = info->account_domain.sid;
6416 * Keep calling LsaEnumTrustdom over opened pipe until
6417 * the end of enumeration is reached
6420 d_printf("Trusted domains list:\n\n");
6423 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6425 &trusted_dom_names, &domain_sids);
6427 if (NT_STATUS_IS_ERR(nt_status)) {
6428 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6429 nt_errstr(nt_status)));
6431 talloc_destroy(mem_ctx);
6435 for (i = 0; i < num_domains; i++) {
6436 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6440 * in case of no trusted domains say something rather
6441 * than just display blank line
6443 if (!num_domains) d_printf("none\n");
6445 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6447 /* close this connection before doing next one */
6448 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6449 if (NT_STATUS_IS_ERR(nt_status)) {
6450 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6451 nt_errstr(nt_status)));
6453 talloc_destroy(mem_ctx);
6457 cli_rpc_pipe_close(pipe_hnd);
6460 * Listing trusting domains (stored in passdb backend, if local)
6463 d_printf("\nTrusting domains list:\n\n");
6466 * Open \PIPE\samr and get needed policy handles
6468 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6470 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6472 talloc_destroy(mem_ctx);
6477 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6478 pipe_hnd->cli->desthost,
6479 SA_RIGHT_SAM_OPEN_DOMAIN,
6481 if (!NT_STATUS_IS_OK(nt_status)) {
6482 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6483 nt_errstr(nt_status)));
6485 talloc_destroy(mem_ctx);
6489 /* SamrOpenDomain - we have to open domain policy handle in order to be
6490 able to enumerate accounts*/
6491 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6493 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6496 if (!NT_STATUS_IS_OK(nt_status)) {
6497 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6498 nt_errstr(nt_status)));
6500 talloc_destroy(mem_ctx);
6505 * perform actual enumeration
6508 enum_ctx = 0; /* reset enumeration context from last enumeration */
6511 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6512 &enum_ctx, ACB_DOMTRUST, 0xffff,
6513 &trusting_dom_names, &trusting_dom_rids,
6515 if (NT_STATUS_IS_ERR(nt_status)) {
6516 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6517 nt_errstr(nt_status)));
6519 talloc_destroy(mem_ctx);
6523 for (i = 0; i < num_domains; i++) {
6526 * get each single domain's sid (do we _really_ need this ?):
6527 * 1) connect to domain's pdc
6528 * 2) query the pdc for domain's sid
6531 /* get rid of '$' tail */
6532 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6533 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6534 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6536 /* calculate padding space for d_printf to look nicer */
6537 pad_len = col_len - strlen(trusting_dom_names[i]);
6538 padding[pad_len] = 0;
6539 do padding[--pad_len] = ' '; while (pad_len);
6541 /* set opt_* variables to remote domain */
6542 strupper_m(trusting_dom_names[i]);
6543 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6544 opt_target_workgroup = opt_workgroup;
6546 d_printf("%s%s", trusting_dom_names[i], padding);
6548 /* connect to remote domain controller */
6549 nt_status = net_make_ipc_connection(
6550 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6552 if (NT_STATUS_IS_OK(nt_status)) {
6553 /* query for domain's sid */
6554 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6555 d_fprintf(stderr, "couldn't get domain's sid\n");
6557 cli_shutdown(remote_cli);
6560 d_fprintf(stderr, "domain controller is not "
6562 nt_errstr(nt_status));
6566 if (!num_domains) d_printf("none\n");
6568 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6570 /* close opened samr and domain policy handles */
6571 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6572 if (!NT_STATUS_IS_OK(nt_status)) {
6573 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6576 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6577 if (!NT_STATUS_IS_OK(nt_status)) {
6578 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6581 /* close samr pipe and connection to IPC$ */
6584 talloc_destroy(mem_ctx);
6589 * Entrypoint for 'net rpc trustdom' code
6591 * @param argc standard argc
6592 * @param argv standard argv without initial components
6594 * @return Integer status (0 means success)
6597 static int rpc_trustdom(int argc, const char **argv)
6599 struct functable func[] = {
6600 {"add", rpc_trustdom_add},
6601 {"del", rpc_trustdom_del},
6602 {"establish", rpc_trustdom_establish},
6603 {"revoke", rpc_trustdom_revoke},
6604 {"help", rpc_trustdom_usage},
6605 {"list", rpc_trustdom_list},
6606 {"vampire", rpc_trustdom_vampire},
6611 rpc_trustdom_usage(argc, argv);
6615 return (net_run_function(argc, argv, func, rpc_user_usage));
6619 * Check if a server will take rpc commands
6620 * @param flags Type of server to connect to (PDC, DMB, localhost)
6621 * if the host is not explicitly specified
6622 * @return bool (true means rpc supported)
6624 bool net_rpc_check(unsigned flags)
6626 struct cli_state *cli;
6628 struct sockaddr_storage server_ss;
6629 char *server_name = NULL;
6632 /* flags (i.e. server type) may depend on command */
6633 if (!net_find_server(NULL, flags, &server_ss, &server_name))
6636 if ((cli = cli_initialise()) == NULL) {
6640 status = cli_connect(cli, server_name, &server_ss);
6641 if (!NT_STATUS_IS_OK(status))
6643 if (!attempt_netbios_session_request(&cli, global_myname(),
6644 server_name, &server_ss))
6646 if (!cli_negprot(cli))
6648 if (cli->protocol < PROTOCOL_NT1)
6657 /* dump sam database via samsync rpc calls */
6658 static int rpc_samdump(int argc, const char **argv) {
6659 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6663 /* syncronise sam database via samsync rpc calls */
6664 static int rpc_vampire(int argc, const char **argv) {
6665 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6670 * Migrate everything from a print-server
6672 * @param argc Standard main() style argc
6673 * @param argv Standard main() style argv. Initial components are already
6676 * @return A shell status integer (0 for success)
6678 * The order is important !
6679 * To successfully add drivers the print-queues have to exist !
6680 * Applying ACLs should be the last step, because you're easily locked out
6683 static int rpc_printer_migrate_all(int argc, const char **argv)
6688 printf("no server to migrate\n");
6692 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6696 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6700 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6704 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6708 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6713 * Migrate print-drivers from a print-server
6715 * @param argc Standard main() style argc
6716 * @param argv Standard main() style argv. Initial components are already
6719 * @return A shell status integer (0 for success)
6721 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6724 printf("no server to migrate\n");
6728 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6729 rpc_printer_migrate_drivers_internals,
6734 * Migrate print-forms from a print-server
6736 * @param argc Standard main() style argc
6737 * @param argv Standard main() style argv. Initial components are already
6740 * @return A shell status integer (0 for success)
6742 static int rpc_printer_migrate_forms(int argc, const char **argv)
6745 printf("no server to migrate\n");
6749 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6750 rpc_printer_migrate_forms_internals,
6755 * Migrate printers from a print-server
6757 * @param argc Standard main() style argc
6758 * @param argv Standard main() style argv. Initial components are already
6761 * @return A shell status integer (0 for success)
6763 static int rpc_printer_migrate_printers(int argc, const char **argv)
6766 printf("no server to migrate\n");
6770 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6771 rpc_printer_migrate_printers_internals,
6776 * Migrate printer-ACLs from a print-server
6778 * @param argc Standard main() style argc
6779 * @param argv Standard main() style argv. Initial components are already
6782 * @return A shell status integer (0 for success)
6784 static int rpc_printer_migrate_security(int argc, const char **argv)
6787 printf("no server to migrate\n");
6791 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6792 rpc_printer_migrate_security_internals,
6797 * Migrate printer-settings from a print-server
6799 * @param argc Standard main() style argc
6800 * @param argv Standard main() style argv. Initial components are already
6803 * @return A shell status integer (0 for success)
6805 static int rpc_printer_migrate_settings(int argc, const char **argv)
6808 printf("no server to migrate\n");
6812 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6813 rpc_printer_migrate_settings_internals,
6818 * 'net rpc printer' entrypoint.
6819 * @param argc Standard main() style argc
6820 * @param argv Standard main() style argv. Initial components are already
6824 int rpc_printer_migrate(int argc, const char **argv)
6827 /* ouch: when addriver and setdriver are called from within
6828 rpc_printer_migrate_drivers_internals, the printer-queue already
6831 struct functable func[] = {
6832 {"all", rpc_printer_migrate_all},
6833 {"drivers", rpc_printer_migrate_drivers},
6834 {"forms", rpc_printer_migrate_forms},
6835 {"help", rpc_printer_usage},
6836 {"printers", rpc_printer_migrate_printers},
6837 {"security", rpc_printer_migrate_security},
6838 {"settings", rpc_printer_migrate_settings},
6842 return net_run_function(argc, argv, func, rpc_printer_usage);
6847 * List printers on a remote RPC server
6849 * @param argc Standard main() style argc
6850 * @param argv Standard main() style argv. Initial components are already
6853 * @return A shell status integer (0 for success)
6855 static int rpc_printer_list(int argc, const char **argv)
6858 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6859 rpc_printer_list_internals,
6864 * List printer-drivers on a remote RPC server
6866 * @param argc Standard main() style argc
6867 * @param argv Standard main() style argv. Initial components are already
6870 * @return A shell status integer (0 for success)
6872 static int rpc_printer_driver_list(int argc, const char **argv)
6875 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6876 rpc_printer_driver_list_internals,
6881 * Publish printer in ADS via MSRPC
6883 * @param argc Standard main() style argc
6884 * @param argv Standard main() style argv. Initial components are already
6887 * @return A shell status integer (0 for success)
6889 static int rpc_printer_publish_publish(int argc, const char **argv)
6892 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6893 rpc_printer_publish_publish_internals,
6898 * Update printer in ADS via MSRPC
6900 * @param argc Standard main() style argc
6901 * @param argv Standard main() style argv. Initial components are already
6904 * @return A shell status integer (0 for success)
6906 static int rpc_printer_publish_update(int argc, const char **argv)
6909 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6910 rpc_printer_publish_update_internals,
6915 * UnPublish printer in ADS via MSRPC
6917 * @param argc Standard main() style argc
6918 * @param argv Standard main() style argv. Initial components are already
6921 * @return A shell status integer (0 for success)
6923 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6926 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6927 rpc_printer_publish_unpublish_internals,
6932 * List published printers via MSRPC
6934 * @param argc Standard main() style argc
6935 * @param argv Standard main() style argv. Initial components are already
6938 * @return A shell status integer (0 for success)
6940 static int rpc_printer_publish_list(int argc, const char **argv)
6943 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6944 rpc_printer_publish_list_internals,
6950 * Publish printer in ADS
6952 * @param argc Standard main() style argc
6953 * @param argv Standard main() style argv. Initial components are already
6956 * @return A shell status integer (0 for success)
6958 static int rpc_printer_publish(int argc, const char **argv)
6961 struct functable func[] = {
6962 {"publish", rpc_printer_publish_publish},
6963 {"update", rpc_printer_publish_update},
6964 {"unpublish", rpc_printer_publish_unpublish},
6965 {"list", rpc_printer_publish_list},
6966 {"help", rpc_printer_usage},
6971 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6972 rpc_printer_publish_list_internals,
6975 return net_run_function(argc, argv, func, rpc_printer_usage);
6981 * Display rpc printer help page.
6982 * @param argc Standard main() style argc
6983 * @param argv Standard main() style argv. Initial components are already
6986 int rpc_printer_usage(int argc, const char **argv)
6988 return net_help_printer(argc, argv);
6992 * 'net rpc printer' entrypoint.
6993 * @param argc Standard main() style argc
6994 * @param argv Standard main() style argv. Initial components are already
6997 int net_rpc_printer(int argc, const char **argv)
6999 struct functable func[] = {
7000 {"list", rpc_printer_list},
7001 {"migrate", rpc_printer_migrate},
7002 {"driver", rpc_printer_driver_list},
7003 {"publish", rpc_printer_publish},
7008 return run_rpc_command(NULL, PI_SPOOLSS, 0,
7009 rpc_printer_list_internals,
7012 return net_run_function(argc, argv, func, rpc_printer_usage);
7015 /****************************************************************************/
7019 * Basic usage function for 'net rpc'
7020 * @param argc Standard main() style argc
7021 * @param argv Standard main() style argv. Initial components are already
7025 int net_rpc_usage(int argc, const char **argv)
7027 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
7028 d_printf(" net rpc join \t\t\tto join a domain \n");
7029 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
7030 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
7031 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
7032 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
7033 d_printf(" net rpc group \t\tto list groups\n");
7034 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
7035 d_printf(" net rpc printer \t\tto list and migrate printers\n");
7036 d_printf(" net rpc file \t\t\tto list open files\n");
7037 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
7038 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
7039 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
7040 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
7041 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
7042 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
7043 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
7044 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
7045 d_printf(" net rpc registry\t\tto manage registry hives\n");
7046 d_printf(" net rpc service\t\tto start, stop and query services\n");
7047 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
7048 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
7050 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
7051 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
7052 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
7053 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
7054 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
7060 * Help function for 'net rpc'. Calls command specific help if requested
7061 * or displays usage of net rpc
7062 * @param argc Standard main() style argc
7063 * @param argv Standard main() style argv. Initial components are already
7067 int net_rpc_help(int argc, const char **argv)
7069 struct functable func[] = {
7070 {"join", rpc_join_usage},
7071 {"user", rpc_user_usage},
7072 {"group", rpc_group_usage},
7073 {"share", rpc_share_usage},
7074 /*{"changetrustpw", rpc_changetrustpw_usage}, */
7075 {"trustdom", rpc_trustdom_usage},
7076 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
7077 /*{"shutdown", rpc_shutdown_usage}, */
7078 {"vampire", rpc_vampire_usage},
7083 net_rpc_usage(argc, argv);
7087 return (net_run_function(argc, argv, func, rpc_user_usage));
7091 * 'net rpc' entrypoint.
7092 * @param argc Standard main() style argc
7093 * @param argv Standard main() style argv. Initial components are already
7097 int net_rpc(int argc, const char **argv)
7099 struct functable func[] = {
7100 {"audit", net_rpc_audit},
7101 {"info", net_rpc_info},
7102 {"join", net_rpc_join},
7103 {"oldjoin", net_rpc_oldjoin},
7104 {"testjoin", net_rpc_testjoin},
7105 {"user", net_rpc_user},
7106 {"password", rpc_user_password},
7107 {"group", net_rpc_group},
7108 {"share", net_rpc_share},
7109 {"file", net_rpc_file},
7110 {"printer", net_rpc_printer},
7111 {"changetrustpw", net_rpc_changetrustpw},
7112 {"trustdom", rpc_trustdom},
7113 {"abortshutdown", rpc_shutdown_abort},
7114 {"shutdown", rpc_shutdown},
7115 {"samdump", rpc_samdump},
7116 {"vampire", rpc_vampire},
7117 {"getsid", net_rpc_getsid},
7118 {"rights", net_rpc_rights},
7119 {"service", net_rpc_service},
7120 {"registry", net_rpc_registry},
7121 {"shell", net_rpc_shell},
7122 {"help", net_rpc_help},
7125 return net_run_function(argc, argv, func, net_rpc_usage);