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 uint32 info_class = 5;
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_query_info_policy(lsa_pipe, mem_ctx, &pol,
83 info_class, domain_name,
85 if (!NT_STATUS_IS_OK(result)) {
86 d_fprintf(stderr, "lsaquery failed: %s\n",
91 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
92 cli_rpc_pipe_close(lsa_pipe);
98 * Run a single RPC command, from start to finish.
100 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
101 * @param conn_flag a NET_FLAG_ combination. Passed to
102 * net_make_ipc_connection.
103 * @param argc Standard main() style argc
104 * @param argc Standard main() style argv. Initial components are already
106 * @return A shell status integer (0 for success)
109 int run_rpc_command(struct cli_state *cli_arg,
116 struct cli_state *cli = NULL;
117 struct rpc_pipe_client *pipe_hnd = NULL;
121 const char *domain_name;
123 /* make use of cli_state handed over as an argument, if possible */
125 nt_status = net_make_ipc_connection(conn_flags, &cli);
126 if (!NT_STATUS_IS_OK(nt_status)) {
127 DEBUG(1, ("failed to make ipc connection: %s\n",
128 nt_errstr(nt_status)));
141 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
142 DEBUG(0, ("talloc_init() failed\n"));
147 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
149 if (!NT_STATUS_IS_OK(nt_status)) {
154 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
155 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
156 /* Always try and create an schannel netlogon pipe. */
157 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
158 PIPE_AUTH_LEVEL_PRIVACY,
162 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
163 nt_errstr(nt_status) ));
168 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
170 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
171 cli_get_pipe_name(pipe_idx),
172 nt_errstr(nt_status) ));
179 nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
181 if (!NT_STATUS_IS_OK(nt_status)) {
182 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
184 DEBUG(5, ("rpc command function succedded\n"));
187 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
189 cli_rpc_pipe_close(pipe_hnd);
193 /* close the connection only if it was opened here */
198 talloc_destroy(mem_ctx);
199 return (!NT_STATUS_IS_OK(nt_status));
203 * Force a change of the trust acccount password.
205 * All parameters are provided by the run_rpc_command function, except for
206 * argc, argv which are passes through.
208 * @param domain_sid The domain sid aquired from the remote server
209 * @param cli A cli_state connected to the server.
210 * @param mem_ctx Talloc context, destoyed on compleation of the function.
211 * @param argc Standard main() style argc
212 * @param argc Standard main() style argv. Initial components are already
215 * @return Normal NTSTATUS return.
218 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
219 const char *domain_name,
220 struct cli_state *cli,
221 struct rpc_pipe_client *pipe_hnd,
227 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
231 * Force a change of the trust acccount password.
233 * @param argc Standard main() style argc
234 * @param argc Standard main() style argv. Initial components are already
237 * @return A shell status integer (0 for success)
240 int net_rpc_changetrustpw(int argc, const char **argv)
242 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
243 rpc_changetrustpw_internals,
248 * Join a domain, the old way.
250 * This uses 'machinename' as the inital password, and changes it.
252 * The password should be created with 'server manager' or equiv first.
254 * All parameters are provided by the run_rpc_command function, except for
255 * argc, argv which are passes through.
257 * @param domain_sid The domain sid aquired from the remote server
258 * @param cli A cli_state connected to the server.
259 * @param mem_ctx Talloc context, destoyed on compleation of the function.
260 * @param argc Standard main() style argc
261 * @param argc Standard main() style argv. Initial components are already
264 * @return Normal NTSTATUS return.
267 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
268 const char *domain_name,
269 struct cli_state *cli,
270 struct rpc_pipe_client *pipe_hnd,
276 fstring trust_passwd;
277 unsigned char orig_trust_passwd_hash[16];
279 uint32 sec_channel_type;
281 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
283 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
286 nt_errstr(result) ));
291 check what type of join - if the user want's to join as
292 a BDC, the server must agree that we are a BDC.
295 sec_channel_type = get_sec_channel_type(argv[0]);
297 sec_channel_type = get_sec_channel_type(NULL);
300 fstrcpy(trust_passwd, global_myname());
301 strlower_m(trust_passwd);
304 * Machine names can be 15 characters, but the max length on
305 * a password is 14. --jerry
308 trust_passwd[14] = '\0';
310 E_md4hash(trust_passwd, orig_trust_passwd_hash);
312 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
313 orig_trust_passwd_hash,
316 if (NT_STATUS_IS_OK(result))
317 printf("Joined domain %s.\n",opt_target_workgroup);
320 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
321 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
322 result = NT_STATUS_UNSUCCESSFUL;
329 * Join a domain, the old way.
331 * @param argc Standard main() style argc
332 * @param argc Standard main() style argv. Initial components are already
335 * @return A shell status integer (0 for success)
338 static int net_rpc_perform_oldjoin(int argc, const char **argv)
340 return run_rpc_command(NULL, PI_NETLOGON,
341 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
342 rpc_oldjoin_internals,
347 * Join a domain, the old way. This function exists to allow
348 * the message to be displayed when oldjoin was explicitly
349 * requested, but not when it was implied by "net rpc join"
351 * @param argc Standard main() style argc
352 * @param argc Standard main() style argv. Initial components are already
355 * @return A shell status integer (0 for success)
358 static int net_rpc_oldjoin(int argc, const char **argv)
360 int rc = net_rpc_perform_oldjoin(argc, argv);
363 d_fprintf(stderr, "Failed to join domain\n");
370 * Basic usage function for 'net rpc join'
371 * @param argc Standard main() style argc
372 * @param argc Standard main() style argv. Initial components are already
376 static int rpc_join_usage(int argc, const char **argv)
378 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
379 "\t to join a domain with admin username & password\n"\
380 "\t\t password will be prompted if needed and none is specified\n"\
381 "\t <type> can be (default MEMBER)\n"\
382 "\t\t BDC - Join as a BDC\n"\
383 "\t\t PDC - Join as a PDC\n"\
384 "\t\t MEMBER - Join as a MEMBER server\n");
386 net_common_flags_usage(argc, argv);
391 * 'net rpc join' entrypoint.
392 * @param argc Standard main() style argc
393 * @param argc Standard main() style argv. Initial components are already
396 * Main 'net_rpc_join()' (where the admain username/password is used) is
398 * Try to just change the password, but if that doesn't work, use/prompt
399 * for a username/password.
402 int net_rpc_join(int argc, const char **argv)
404 if (lp_server_role() == ROLE_STANDALONE) {
405 d_printf("cannot join as standalone machine\n");
409 if (strlen(global_myname()) > 15) {
410 d_printf("Our netbios name can be at most 15 chars long, "
411 "\"%s\" is %u chars long\n",
412 global_myname(), (unsigned int)strlen(global_myname()));
416 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
419 return net_rpc_join_newstyle(argc, argv);
423 * display info about a rpc domain
425 * All parameters are provided by the run_rpc_command function, except for
426 * argc, argv which are passed through.
428 * @param domain_sid The domain sid acquired from the remote server
429 * @param cli A cli_state connected to the server.
430 * @param mem_ctx Talloc context, destoyed on completion of the function.
431 * @param argc Standard main() style argc
432 * @param argv Standard main() style argv. Initial components are already
435 * @return Normal NTSTATUS return.
438 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
439 const char *domain_name,
440 struct cli_state *cli,
441 struct rpc_pipe_client *pipe_hnd,
446 POLICY_HND connect_pol, domain_pol;
447 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
448 union samr_DomainInfo *info = NULL;
451 sid_to_fstring(sid_str, domain_sid);
453 /* Get sam policy handle */
454 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
455 pipe_hnd->cli->desthost,
456 MAXIMUM_ALLOWED_ACCESS,
458 if (!NT_STATUS_IS_OK(result)) {
459 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
463 /* Get domain policy handle */
464 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
466 MAXIMUM_ALLOWED_ACCESS,
467 CONST_DISCARD(struct dom_sid2 *, domain_sid),
469 if (!NT_STATUS_IS_OK(result)) {
470 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
474 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
478 if (NT_STATUS_IS_OK(result)) {
479 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
480 d_printf("Domain SID: %s\n", sid_str);
481 d_printf("Sequence number: %llu\n",
482 (unsigned long long)info->info2.sequence_num);
483 d_printf("Num users: %u\n", info->info2.num_users);
484 d_printf("Num domain groups: %u\n", info->info2.num_groups);
485 d_printf("Num local groups: %u\n", info->info2.num_aliases);
493 * 'net rpc info' entrypoint.
494 * @param argc Standard main() style argc
495 * @param argc Standard main() style argv. Initial components are already
499 int net_rpc_info(int argc, const char **argv)
501 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC,
507 * Fetch domain SID into the local secrets.tdb
509 * All parameters are provided by the run_rpc_command function, except for
510 * argc, argv which are passes through.
512 * @param domain_sid The domain sid acquired from the remote server
513 * @param cli A cli_state connected to the server.
514 * @param mem_ctx Talloc context, destoyed on completion of the function.
515 * @param argc Standard main() style argc
516 * @param argv Standard main() style argv. Initial components are already
519 * @return Normal NTSTATUS return.
522 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
523 const char *domain_name,
524 struct cli_state *cli,
525 struct rpc_pipe_client *pipe_hnd,
532 sid_to_fstring(sid_str, domain_sid);
533 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
534 sid_str, domain_name);
536 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
537 DEBUG(0,("Can't store domain SID\n"));
538 return NT_STATUS_UNSUCCESSFUL;
545 * 'net rpc getsid' entrypoint.
546 * @param argc Standard main() style argc
547 * @param argc Standard main() style argv. Initial components are already
551 int net_rpc_getsid(int argc, const char **argv)
553 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
554 rpc_getsid_internals,
558 /****************************************************************************/
561 * Basic usage function for 'net rpc user'
562 * @param argc Standard main() style argc.
563 * @param argv Standard main() style argv. Initial components are already
567 static int rpc_user_usage(int argc, const char **argv)
569 return net_help_user(argc, argv);
573 * Add a new user to a remote RPC server
575 * All parameters are provided by the run_rpc_command function, except for
576 * argc, argv which are passes through.
578 * @param domain_sid The domain sid acquired from the remote server
579 * @param cli A cli_state connected to the server.
580 * @param mem_ctx Talloc context, destoyed on completion of the function.
581 * @param argc Standard main() style argc
582 * @param argv Standard main() style argv. Initial components are already
585 * @return Normal NTSTATUS return.
588 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
589 const char *domain_name,
590 struct cli_state *cli,
591 struct rpc_pipe_client *pipe_hnd,
593 int argc, const char **argv)
596 POLICY_HND connect_pol, domain_pol, user_pol;
597 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
598 const char *acct_name;
599 struct lsa_String lsa_acct_name;
601 uint32 acct_flags, user_rid;
602 uint32_t access_granted = 0;
605 d_printf("User must be specified\n");
606 rpc_user_usage(argc, argv);
611 init_lsa_String(&lsa_acct_name, acct_name);
613 /* Get sam policy handle */
615 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
616 pipe_hnd->cli->desthost,
617 MAXIMUM_ALLOWED_ACCESS,
619 if (!NT_STATUS_IS_OK(result)) {
623 /* Get domain policy handle */
625 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
627 MAXIMUM_ALLOWED_ACCESS,
628 CONST_DISCARD(struct dom_sid2 *, domain_sid),
630 if (!NT_STATUS_IS_OK(result)) {
634 /* Create domain user */
636 acb_info = ACB_NORMAL;
637 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
638 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
639 SAMR_USER_ACCESS_SET_PASSWORD |
640 SAMR_USER_ACCESS_GET_ATTRIBUTES |
641 SAMR_USER_ACCESS_SET_ATTRIBUTES;
643 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
652 if (!NT_STATUS_IS_OK(result)) {
658 uint32 *user_rids, num_rids, *name_types;
659 uint32 flags = 0x000003e8; /* Unknown */
660 SAM_USERINFO_CTR ctr;
661 SAM_USER_INFO_24 p24;
664 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
665 flags, 1, &acct_name,
666 &num_rids, &user_rids,
669 if (!NT_STATUS_IS_OK(result)) {
673 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
675 MAXIMUM_ALLOWED_ACCESS,
679 if (!NT_STATUS_IS_OK(result)) {
683 /* Set password on account */
688 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
690 init_sam_user_info24(&p24, (char *)pwbuf,24);
692 ctr.switch_value = 24;
693 ctr.info.id24 = &p24;
695 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
696 &cli->user_session_key, &ctr);
698 if (!NT_STATUS_IS_OK(result)) {
699 d_fprintf(stderr, "Failed to set password for user %s - %s\n",
700 acct_name, nt_errstr(result));
702 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
705 if (!NT_STATUS_IS_OK(result)) {
706 d_fprintf(stderr, "Failed to delete user %s - %s\n",
707 acct_name, nt_errstr(result));
714 if (!NT_STATUS_IS_OK(result)) {
715 d_fprintf(stderr, "Failed to add user '%s' with %s.\n",
716 acct_name, nt_errstr(result));
718 d_printf("Added user '%s'.\n", acct_name);
724 * Add a new user to a remote RPC server
726 * @param argc Standard main() style argc
727 * @param argv Standard main() style argv. Initial components are already
730 * @return A shell status integer (0 for success)
733 static int rpc_user_add(int argc, const char **argv)
735 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
740 * Delete a user from a remote RPC server
742 * All parameters are provided by the run_rpc_command function, except for
743 * argc, argv which are passes through.
745 * @param domain_sid The domain sid acquired from the remote server
746 * @param cli A cli_state connected to the server.
747 * @param mem_ctx Talloc context, destoyed on completion of the function.
748 * @param argc Standard main() style argc
749 * @param argv Standard main() style argv. Initial components are already
752 * @return Normal NTSTATUS return.
755 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
756 const char *domain_name,
757 struct cli_state *cli,
758 struct rpc_pipe_client *pipe_hnd,
763 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
764 POLICY_HND connect_pol, domain_pol, user_pol;
765 const char *acct_name;
768 d_printf("User must be specified\n");
769 rpc_user_usage(argc, argv);
775 /* Get sam policy and domain handles */
777 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
778 pipe_hnd->cli->desthost,
779 MAXIMUM_ALLOWED_ACCESS,
782 if (!NT_STATUS_IS_OK(result)) {
786 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
788 MAXIMUM_ALLOWED_ACCESS,
789 CONST_DISCARD(struct dom_sid2 *, domain_sid),
792 if (!NT_STATUS_IS_OK(result)) {
796 /* Get handle on user */
799 uint32 *user_rids, num_rids, *name_types;
800 uint32 flags = 0x000003e8; /* Unknown */
802 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
803 flags, 1, &acct_name,
804 &num_rids, &user_rids,
807 if (!NT_STATUS_IS_OK(result)) {
811 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
813 MAXIMUM_ALLOWED_ACCESS,
817 if (!NT_STATUS_IS_OK(result)) {
824 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
827 if (!NT_STATUS_IS_OK(result)) {
832 if (!NT_STATUS_IS_OK(result)) {
833 d_fprintf(stderr, "Failed to delete user '%s' with %s.\n",
834 acct_name, nt_errstr(result));
836 d_printf("Deleted user '%s'.\n", acct_name);
843 * Rename a user on a remote RPC server
845 * All parameters are provided by the run_rpc_command function, except for
846 * argc, argv which are passes through.
848 * @param domain_sid The domain sid acquired from the remote server
849 * @param cli A cli_state connected to the server.
850 * @param mem_ctx Talloc context, destoyed on completion of the function.
851 * @param argc Standard main() style argc
852 * @param argv Standard main() style argv. Initial components are already
855 * @return Normal NTSTATUS return.
858 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
859 const char *domain_name,
860 struct cli_state *cli,
861 struct rpc_pipe_client *pipe_hnd,
866 POLICY_HND connect_pol, domain_pol, user_pol;
867 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
868 uint32 info_level = 7;
869 const char *old_name, *new_name;
871 uint32 flags = 0x000003e8; /* Unknown */
872 uint32 num_rids, *name_types;
873 uint32 num_names = 1;
875 SAM_USERINFO_CTR *user_ctr;
876 SAM_USERINFO_CTR ctr;
877 SAM_USER_INFO_7 info7;
880 d_printf("Old and new username must be specified\n");
881 rpc_user_usage(argc, argv);
889 ZERO_STRUCT(user_ctr);
891 /* Get sam policy handle */
893 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
894 pipe_hnd->cli->desthost,
895 MAXIMUM_ALLOWED_ACCESS,
898 if (!NT_STATUS_IS_OK(result)) {
902 /* Get domain policy handle */
904 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
906 MAXIMUM_ALLOWED_ACCESS,
907 CONST_DISCARD(struct dom_sid2 *, domain_sid),
909 if (!NT_STATUS_IS_OK(result)) {
913 if ((names = TALLOC_ARRAY(mem_ctx, const char *, num_names)) == NULL) {
914 result = NT_STATUS_NO_MEMORY;
918 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
919 flags, num_names, names,
920 &num_rids, &user_rid, &name_types);
921 if (!NT_STATUS_IS_OK(result)) {
925 /* Open domain user */
926 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
928 MAXIMUM_ALLOWED_ACCESS,
932 if (!NT_STATUS_IS_OK(result)) {
936 /* Query user info */
937 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
938 info_level, &user_ctr);
940 if (!NT_STATUS_IS_OK(result)) {
944 ctr.switch_value = info_level;
945 ctr.info.id7 = &info7;
947 init_sam_user_info7(&info7, new_name);
950 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
951 info_level, &cli->user_session_key, &ctr);
953 if (!NT_STATUS_IS_OK(result)) {
958 if (!NT_STATUS_IS_OK(result)) {
959 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
962 d_printf("Renamed user from %s to %s\n", old_name, new_name);
968 * Rename a user on a remote RPC server
970 * @param argc Standard main() style argc
971 * @param argv Standard main() style argv. Initial components are already
974 * @return A shell status integer (0 for success)
977 static int rpc_user_rename(int argc, const char **argv)
979 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
984 * Delete a user from a remote RPC server
986 * @param argc Standard main() style argc
987 * @param argv Standard main() style argv. Initial components are already
990 * @return A shell status integer (0 for success)
993 static int rpc_user_delete(int argc, const char **argv)
995 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
1000 * Set a password for a user on a remote RPC server
1002 * All parameters are provided by the run_rpc_command function, except for
1003 * argc, argv which are passes through.
1005 * @param domain_sid The domain sid acquired from the remote server
1006 * @param cli A cli_state connected to the server.
1007 * @param mem_ctx Talloc context, destoyed on completion of the function.
1008 * @param argc Standard main() style argc
1009 * @param argv Standard main() style argv. Initial components are already
1012 * @return Normal NTSTATUS return.
1015 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
1016 const char *domain_name,
1017 struct cli_state *cli,
1018 struct rpc_pipe_client *pipe_hnd,
1019 TALLOC_CTX *mem_ctx,
1023 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1024 POLICY_HND connect_pol, domain_pol, user_pol;
1025 SAM_USERINFO_CTR ctr;
1026 SAM_USER_INFO_24 p24;
1029 const char *new_password;
1030 char *prompt = NULL;
1033 d_printf("User must be specified\n");
1034 rpc_user_usage(argc, argv);
1035 return NT_STATUS_OK;
1041 new_password = argv[1];
1043 asprintf(&prompt, "Enter new password for %s:", user);
1044 new_password = getpass(prompt);
1048 /* Get sam policy and domain handles */
1050 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1051 pipe_hnd->cli->desthost,
1052 MAXIMUM_ALLOWED_ACCESS,
1055 if (!NT_STATUS_IS_OK(result)) {
1059 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1061 MAXIMUM_ALLOWED_ACCESS,
1062 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1065 if (!NT_STATUS_IS_OK(result)) {
1069 /* Get handle on user */
1072 uint32 *user_rids, num_rids, *name_types;
1073 uint32 flags = 0x000003e8; /* Unknown */
1075 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1077 &num_rids, &user_rids,
1080 if (!NT_STATUS_IS_OK(result)) {
1084 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1086 MAXIMUM_ALLOWED_ACCESS,
1090 if (!NT_STATUS_IS_OK(result)) {
1095 /* Set password on account */
1100 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
1102 init_sam_user_info24(&p24, (char *)pwbuf,24);
1104 ctr.switch_value = 24;
1105 ctr.info.id24 = &p24;
1107 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
1108 &cli->user_session_key, &ctr);
1110 if (!NT_STATUS_IS_OK(result)) {
1114 /* Display results */
1122 * Set a user's password on a remote RPC server
1124 * @param argc Standard main() style argc
1125 * @param argv Standard main() style argv. Initial components are already
1128 * @return A shell status integer (0 for success)
1131 static int rpc_user_password(int argc, const char **argv)
1133 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1138 * List user's groups on a remote RPC server
1140 * All parameters are provided by the run_rpc_command function, except for
1141 * argc, argv which are passes through.
1143 * @param domain_sid The domain sid acquired from the remote server
1144 * @param cli A cli_state connected to the server.
1145 * @param mem_ctx Talloc context, destoyed on completion of the function.
1146 * @param argc Standard main() style argc
1147 * @param argv Standard main() style argv. Initial components are already
1150 * @return Normal NTSTATUS return.
1153 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1154 const char *domain_name,
1155 struct cli_state *cli,
1156 struct rpc_pipe_client *pipe_hnd,
1157 TALLOC_CTX *mem_ctx,
1161 POLICY_HND connect_pol, domain_pol, user_pol;
1162 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1163 uint32 *rids, num_rids, *name_types, num_names;
1164 uint32 flags = 0x000003e8; /* Unknown */
1170 d_printf("User must be specified\n");
1171 rpc_user_usage(argc, argv);
1172 return NT_STATUS_OK;
1174 /* Get sam policy handle */
1176 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1177 pipe_hnd->cli->desthost,
1178 MAXIMUM_ALLOWED_ACCESS,
1180 if (!NT_STATUS_IS_OK(result)) goto done;
1182 /* Get domain policy handle */
1184 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1186 MAXIMUM_ALLOWED_ACCESS,
1187 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1189 if (!NT_STATUS_IS_OK(result)) goto done;
1191 /* Get handle on user */
1193 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1195 &num_rids, &rids, &name_types);
1197 if (!NT_STATUS_IS_OK(result)) goto done;
1199 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1201 MAXIMUM_ALLOWED_ACCESS,
1204 if (!NT_STATUS_IS_OK(result)) goto done;
1206 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1207 &num_rids, &user_gids);
1209 if (!NT_STATUS_IS_OK(result)) goto done;
1214 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1215 result = NT_STATUS_NO_MEMORY;
1219 for (i = 0; i < num_rids; i++)
1220 rids[i] = user_gids[i].g_rid;
1222 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1224 &num_names, &names, &name_types);
1226 if (!NT_STATUS_IS_OK(result)) {
1230 /* Display results */
1232 for (i = 0; i < num_names; i++)
1233 printf("%s\n", names[i]);
1240 * List a user's groups from a remote RPC server
1242 * @param argc Standard main() style argc
1243 * @param argv Standard main() style argv. Initial components are already
1246 * @return A shell status integer (0 for success)
1249 static int rpc_user_info(int argc, const char **argv)
1251 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1256 * List users on a remote RPC server
1258 * All parameters are provided by the run_rpc_command function, except for
1259 * argc, argv which are passes through.
1261 * @param domain_sid The domain sid acquired from the remote server
1262 * @param cli A cli_state connected to the server.
1263 * @param mem_ctx Talloc context, destoyed on completion of the function.
1264 * @param argc Standard main() style argc
1265 * @param argv Standard main() style argv. Initial components are already
1268 * @return Normal NTSTATUS return.
1271 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1272 const char *domain_name,
1273 struct cli_state *cli,
1274 struct rpc_pipe_client *pipe_hnd,
1275 TALLOC_CTX *mem_ctx,
1279 POLICY_HND connect_pol, domain_pol;
1280 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1281 uint32 start_idx=0, num_entries, i, loop_count = 0;
1282 SAM_DISPINFO_CTR ctr;
1283 SAM_DISPINFO_1 info1;
1285 /* Get sam policy handle */
1287 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1288 pipe_hnd->cli->desthost,
1289 MAXIMUM_ALLOWED_ACCESS,
1291 if (!NT_STATUS_IS_OK(result)) {
1295 /* Get domain policy handle */
1297 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1299 MAXIMUM_ALLOWED_ACCESS,
1300 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1302 if (!NT_STATUS_IS_OK(result)) {
1306 /* Query domain users */
1309 ctr.sam.info1 = &info1;
1310 if (opt_long_list_entries)
1311 d_printf("\nUser name Comment"\
1312 "\n-----------------------------\n");
1315 uint32 max_entries, max_size;
1317 get_query_dispinfo_params(
1318 loop_count, &max_entries, &max_size);
1320 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1321 &start_idx, 1, &num_entries,
1322 max_entries, max_size, &ctr);
1325 for (i = 0; i < num_entries; i++) {
1326 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user));
1327 if (opt_long_list_entries)
1328 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc));
1330 if (opt_long_list_entries)
1331 printf("%-21.21s %s\n", user, desc);
1333 printf("%s\n", user);
1335 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1342 * 'net rpc user' entrypoint.
1343 * @param argc Standard main() style argc
1344 * @param argc Standard main() style argv. Initial components are already
1348 int net_rpc_user(int argc, const char **argv)
1350 struct functable func[] = {
1351 {"add", rpc_user_add},
1352 {"info", rpc_user_info},
1353 {"delete", rpc_user_delete},
1354 {"password", rpc_user_password},
1355 {"rename", rpc_user_rename},
1360 return run_rpc_command(NULL,PI_SAMR, 0,
1361 rpc_user_list_internals,
1365 return net_run_function(argc, argv, func, rpc_user_usage);
1368 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1369 struct rpc_sh_ctx *ctx,
1370 struct rpc_pipe_client *pipe_hnd,
1371 int argc, const char **argv)
1373 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1374 ctx->cli, pipe_hnd, mem_ctx,
1378 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1379 struct rpc_sh_ctx *ctx,
1380 struct rpc_pipe_client *pipe_hnd,
1381 int argc, const char **argv)
1383 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1384 ctx->cli, pipe_hnd, mem_ctx,
1388 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1389 struct rpc_sh_ctx *ctx,
1390 struct rpc_pipe_client *pipe_hnd,
1391 int argc, const char **argv,
1393 TALLOC_CTX *mem_ctx,
1394 struct rpc_sh_ctx *ctx,
1395 struct rpc_pipe_client *pipe_hnd,
1396 const POLICY_HND *user_hnd,
1397 int argc, const char **argv))
1400 POLICY_HND connect_pol, domain_pol, user_pol;
1401 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1404 enum lsa_SidType type;
1407 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1408 return NT_STATUS_INVALID_PARAMETER;
1411 ZERO_STRUCT(connect_pol);
1412 ZERO_STRUCT(domain_pol);
1413 ZERO_STRUCT(user_pol);
1415 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1416 NULL, NULL, &sid, &type);
1417 if (!NT_STATUS_IS_OK(result)) {
1418 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1423 if (type != SID_NAME_USER) {
1424 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1425 sid_type_lookup(type));
1426 result = NT_STATUS_NO_SUCH_USER;
1430 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1431 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1432 result = NT_STATUS_NO_SUCH_USER;
1436 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1437 pipe_hnd->cli->desthost,
1438 MAXIMUM_ALLOWED_ACCESS,
1440 if (!NT_STATUS_IS_OK(result)) {
1444 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1446 MAXIMUM_ALLOWED_ACCESS,
1449 if (!NT_STATUS_IS_OK(result)) {
1453 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1455 MAXIMUM_ALLOWED_ACCESS,
1458 if (!NT_STATUS_IS_OK(result)) {
1462 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1465 if (is_valid_policy_hnd(&user_pol)) {
1466 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1468 if (is_valid_policy_hnd(&domain_pol)) {
1469 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1471 if (is_valid_policy_hnd(&connect_pol)) {
1472 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1477 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1478 struct rpc_sh_ctx *ctx,
1479 struct rpc_pipe_client *pipe_hnd,
1480 const POLICY_HND *user_hnd,
1481 int argc, const char **argv)
1484 SAM_USERINFO_CTR *ctr;
1485 SAM_USER_INFO_21 *info;
1488 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1489 return NT_STATUS_INVALID_PARAMETER;
1492 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1494 if (!NT_STATUS_IS_OK(result)) {
1498 info = ctr->info.id21;
1500 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1506 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1507 struct rpc_sh_ctx *ctx,
1508 struct rpc_pipe_client *pipe_hnd,
1509 int argc, const char **argv)
1511 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1512 rpc_sh_user_show_internals);
1515 #define FETCHSTR(name, rec) \
1516 do { if (strequal(ctx->thiscmd, name)) { \
1517 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1520 #define SETSTR(name, rec, flag) \
1521 do { if (strequal(ctx->thiscmd, name)) { \
1522 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1523 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1524 usr->fields_present |= ACCT_##flag; } \
1527 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1528 struct rpc_sh_ctx *ctx,
1529 struct rpc_pipe_client *pipe_hnd,
1530 const POLICY_HND *user_hnd,
1531 int argc, const char **argv)
1534 SAM_USERINFO_CTR *ctr;
1535 SAM_USER_INFO_21 *usr;
1536 const char *username;
1537 const char *oldval = "";
1540 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1542 return NT_STATUS_INVALID_PARAMETER;
1545 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1547 if (!NT_STATUS_IS_OK(result)) {
1551 usr = ctr->info.id21;
1553 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1555 FETCHSTR("fullname", full_name);
1556 FETCHSTR("homedir", home_dir);
1557 FETCHSTR("homedrive", dir_drive);
1558 FETCHSTR("logonscript", logon_script);
1559 FETCHSTR("profilepath", profile_path);
1560 FETCHSTR("description", acct_desc);
1563 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1569 if (strcmp(argv[0], "NULL") == 0) {
1573 SETSTR("fullname", full_name, FULL_NAME);
1574 SETSTR("homedir", home_dir, HOME_DIR);
1575 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1576 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1577 SETSTR("profilepath", profile_path, PROFILE);
1578 SETSTR("description", acct_desc, DESCRIPTION);
1580 result = rpccli_samr_set_userinfo2(
1581 pipe_hnd, mem_ctx, user_hnd, 21,
1582 &pipe_hnd->cli->user_session_key, ctr);
1584 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1585 ctx->thiscmd, oldval, argv[0]);
1592 #define HANDLEFLG(name, rec) \
1593 do { if (strequal(ctx->thiscmd, name)) { \
1594 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1596 newflags = oldflags | ACB_##rec; \
1598 newflags = oldflags & ~ACB_##rec; \
1601 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1602 struct rpc_sh_ctx *ctx,
1603 struct rpc_pipe_client *pipe_hnd,
1604 int argc, const char **argv)
1606 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1607 rpc_sh_user_str_edit_internals);
1610 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1611 struct rpc_sh_ctx *ctx,
1612 struct rpc_pipe_client *pipe_hnd,
1613 const POLICY_HND *user_hnd,
1614 int argc, const char **argv)
1617 SAM_USERINFO_CTR *ctr;
1618 SAM_USER_INFO_21 *usr;
1619 const char *username;
1620 const char *oldval = "unknown";
1621 uint32 oldflags, newflags;
1625 ((argc == 1) && !strequal(argv[0], "yes") &&
1626 !strequal(argv[0], "no"))) {
1627 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1629 return NT_STATUS_INVALID_PARAMETER;
1632 newval = strequal(argv[0], "yes");
1634 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1636 if (!NT_STATUS_IS_OK(result)) {
1640 usr = ctr->info.id21;
1642 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1643 oldflags = usr->acb_info;
1644 newflags = usr->acb_info;
1646 HANDLEFLG("disabled", DISABLED);
1647 HANDLEFLG("pwnotreq", PWNOTREQ);
1648 HANDLEFLG("autolock", AUTOLOCK);
1649 HANDLEFLG("pwnoexp", PWNOEXP);
1652 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1658 usr->acb_info = newflags;
1659 usr->fields_present = ACCT_FLAGS;
1661 result = rpccli_samr_set_userinfo2(
1662 pipe_hnd, mem_ctx, user_hnd, 21,
1663 &pipe_hnd->cli->user_session_key, ctr);
1665 if (NT_STATUS_IS_OK(result)) {
1666 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1667 ctx->thiscmd, oldval, argv[0]);
1675 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1676 struct rpc_sh_ctx *ctx,
1677 struct rpc_pipe_client *pipe_hnd,
1678 int argc, const char **argv)
1680 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1681 rpc_sh_user_flag_edit_internals);
1684 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1685 struct rpc_sh_ctx *ctx)
1687 static struct rpc_sh_cmd cmds[] = {
1689 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1690 "Show/Set a user's full name" },
1692 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1693 "Show/Set a user's home directory" },
1695 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1696 "Show/Set a user's home drive" },
1698 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1699 "Show/Set a user's logon script" },
1701 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1702 "Show/Set a user's profile path" },
1704 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1705 "Show/Set a user's description" },
1707 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1708 "Show/Set whether a user is disabled" },
1710 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1711 "Show/Set whether a user locked out" },
1713 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1714 "Show/Set whether a user does not need a password" },
1716 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1717 "Show/Set whether a user's password does not expire" },
1719 { NULL, NULL, 0, NULL, NULL }
1725 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1726 struct rpc_sh_ctx *ctx)
1728 static struct rpc_sh_cmd cmds[] = {
1730 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1731 "List available users" },
1733 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1734 "List the domain groups a user is member of" },
1736 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1737 "Show info about a user" },
1739 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1740 "Show/Modify a user's fields" },
1742 { NULL, NULL, 0, NULL, NULL }
1748 /****************************************************************************/
1751 * Basic usage function for 'net rpc group'
1752 * @param argc Standard main() style argc.
1753 * @param argv Standard main() style argv. Initial components are already
1757 static int rpc_group_usage(int argc, const char **argv)
1759 return net_help_group(argc, argv);
1763 * Delete group on a remote RPC server
1765 * All parameters are provided by the run_rpc_command function, except for
1766 * argc, argv which are passes through.
1768 * @param domain_sid The domain sid acquired from the remote server
1769 * @param cli A cli_state connected to the server.
1770 * @param mem_ctx Talloc context, destoyed on completion of the function.
1771 * @param argc Standard main() style argc
1772 * @param argv Standard main() style argv. Initial components are already
1775 * @return Normal NTSTATUS return.
1778 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1779 const char *domain_name,
1780 struct cli_state *cli,
1781 struct rpc_pipe_client *pipe_hnd,
1782 TALLOC_CTX *mem_ctx,
1786 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1787 bool group_is_primary = False;
1788 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1790 uint32 *group_rids, num_rids, *name_types, group_rid;
1791 struct samr_RidTypeArray *rids = NULL;
1792 uint32 flags = 0x000003e8; /* Unknown */
1795 /* DOM_GID *user_gids; */
1796 SAM_USERINFO_CTR *user_ctr;
1800 d_printf("specify group\n");
1801 rpc_group_usage(argc,argv);
1802 return NT_STATUS_OK; /* ok? */
1805 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1806 pipe_hnd->cli->desthost,
1807 MAXIMUM_ALLOWED_ACCESS,
1810 if (!NT_STATUS_IS_OK(result)) {
1811 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1815 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1817 MAXIMUM_ALLOWED_ACCESS,
1818 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1821 if (!NT_STATUS_IS_OK(result)) {
1822 d_fprintf(stderr, "Request open_domain failed\n");
1826 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1828 &num_rids, &group_rids,
1831 if (!NT_STATUS_IS_OK(result)) {
1832 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1836 switch (name_types[0])
1838 case SID_NAME_DOM_GRP:
1839 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1841 MAXIMUM_ALLOWED_ACCESS,
1844 if (!NT_STATUS_IS_OK(result)) {
1845 d_fprintf(stderr, "Request open_group failed");
1849 group_rid = group_rids[0];
1851 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1855 if (!NT_STATUS_IS_OK(result)) {
1856 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1861 d_printf("Domain Group %s (rid: %d) has %d members\n",
1862 argv[0],group_rid, rids->count);
1865 /* Check if group is anyone's primary group */
1866 for (i = 0; i < rids->count; i++)
1868 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1870 MAXIMUM_ALLOWED_ACCESS,
1874 if (!NT_STATUS_IS_OK(result)) {
1875 d_fprintf(stderr, "Unable to open group member %d\n",
1880 ZERO_STRUCT(user_ctr);
1882 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1885 if (!NT_STATUS_IS_OK(result)) {
1886 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1891 if (user_ctr->info.id21->group_rid == group_rid) {
1892 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1895 d_printf("Group is primary group of %s\n",temp);
1896 group_is_primary = True;
1899 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1902 if (group_is_primary) {
1903 d_fprintf(stderr, "Unable to delete group because some "
1904 "of it's members have it as primary group\n");
1905 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1909 /* remove all group members */
1910 for (i = 0; i < rids->count; i++)
1913 d_printf("Remove group member %d...",
1915 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1919 if (NT_STATUS_IS_OK(result)) {
1924 d_printf("failed\n");
1929 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1933 /* removing a local group is easier... */
1934 case SID_NAME_ALIAS:
1935 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1937 MAXIMUM_ALLOWED_ACCESS,
1941 if (!NT_STATUS_IS_OK(result)) {
1942 d_fprintf(stderr, "Request open_alias failed\n");
1946 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1950 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1951 argv[0],sid_type_lookup(name_types[0]));
1952 result = NT_STATUS_UNSUCCESSFUL;
1957 if (NT_STATUS_IS_OK(result)) {
1959 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1961 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1962 get_friendly_nt_error_msg(result));
1970 static int rpc_group_delete(int argc, const char **argv)
1972 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1976 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1977 const char *domain_name,
1978 struct cli_state *cli,
1979 struct rpc_pipe_client *pipe_hnd,
1980 TALLOC_CTX *mem_ctx,
1984 POLICY_HND connect_pol, domain_pol, group_pol;
1985 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1986 union samr_GroupInfo group_info;
1987 struct lsa_String grp_name;
1991 d_printf("Group name must be specified\n");
1992 rpc_group_usage(argc, argv);
1993 return NT_STATUS_OK;
1996 init_lsa_String(&grp_name, argv[0]);
1998 /* Get sam policy handle */
2000 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2001 pipe_hnd->cli->desthost,
2002 MAXIMUM_ALLOWED_ACCESS,
2004 if (!NT_STATUS_IS_OK(result)) goto done;
2006 /* Get domain policy handle */
2008 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2010 MAXIMUM_ALLOWED_ACCESS,
2011 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2013 if (!NT_STATUS_IS_OK(result)) goto done;
2015 /* Create the group */
2017 result = rpccli_samr_CreateDomainGroup(pipe_hnd, mem_ctx,
2020 MAXIMUM_ALLOWED_ACCESS,
2023 if (!NT_STATUS_IS_OK(result)) goto done;
2025 if (strlen(opt_comment) == 0) goto done;
2027 /* We've got a comment to set */
2029 init_lsa_String(&group_info.description, opt_comment);
2031 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
2035 if (!NT_STATUS_IS_OK(result)) goto done;
2038 if (NT_STATUS_IS_OK(result))
2039 DEBUG(5, ("add group succeeded\n"));
2041 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
2046 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
2047 const char *domain_name,
2048 struct cli_state *cli,
2049 struct rpc_pipe_client *pipe_hnd,
2050 TALLOC_CTX *mem_ctx,
2054 POLICY_HND connect_pol, domain_pol, alias_pol;
2055 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2056 union samr_AliasInfo alias_info;
2057 struct lsa_String alias_name;
2061 d_printf("Alias name must be specified\n");
2062 rpc_group_usage(argc, argv);
2063 return NT_STATUS_OK;
2066 init_lsa_String(&alias_name, argv[0]);
2068 /* Get sam policy handle */
2070 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2071 pipe_hnd->cli->desthost,
2072 MAXIMUM_ALLOWED_ACCESS,
2074 if (!NT_STATUS_IS_OK(result)) goto done;
2076 /* Get domain policy handle */
2078 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2080 MAXIMUM_ALLOWED_ACCESS,
2081 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2083 if (!NT_STATUS_IS_OK(result)) goto done;
2085 /* Create the group */
2087 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
2090 MAXIMUM_ALLOWED_ACCESS,
2093 if (!NT_STATUS_IS_OK(result)) goto done;
2095 if (strlen(opt_comment) == 0) goto done;
2097 /* We've got a comment to set */
2099 init_lsa_String(&alias_info.description, opt_comment);
2101 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
2106 if (!NT_STATUS_IS_OK(result)) goto done;
2109 if (NT_STATUS_IS_OK(result))
2110 DEBUG(5, ("add alias succeeded\n"));
2112 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
2117 static int rpc_group_add(int argc, const char **argv)
2120 return run_rpc_command(NULL, PI_SAMR, 0,
2121 rpc_alias_add_internals,
2124 return run_rpc_command(NULL, PI_SAMR, 0,
2125 rpc_group_add_internals,
2129 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2130 TALLOC_CTX *mem_ctx,
2133 enum lsa_SidType *type)
2135 DOM_SID *sids = NULL;
2136 enum lsa_SidType *types = NULL;
2137 struct rpc_pipe_client *pipe_hnd;
2139 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2141 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2146 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2147 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2149 if (!NT_STATUS_IS_OK(result)) {
2153 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2154 &name, NULL, 1, &sids, &types);
2156 if (NT_STATUS_IS_OK(result)) {
2157 sid_copy(sid, &sids[0]);
2161 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2165 cli_rpc_pipe_close(pipe_hnd);
2168 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2170 /* Try as S-1-5-whatever */
2174 if (string_to_sid(&tmp_sid, name)) {
2175 sid_copy(sid, &tmp_sid);
2176 *type = SID_NAME_UNKNOWN;
2177 result = NT_STATUS_OK;
2184 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2185 TALLOC_CTX *mem_ctx,
2186 const DOM_SID *group_sid,
2189 POLICY_HND connect_pol, domain_pol;
2192 POLICY_HND group_pol;
2195 uint32 *rids = NULL;
2196 uint32 *rid_types = NULL;
2200 sid_copy(&sid, group_sid);
2202 if (!sid_split_rid(&sid, &group_rid)) {
2203 return NT_STATUS_UNSUCCESSFUL;
2206 /* Get sam policy handle */
2207 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2208 pipe_hnd->cli->desthost,
2209 MAXIMUM_ALLOWED_ACCESS,
2211 if (!NT_STATUS_IS_OK(result)) {
2215 /* Get domain policy handle */
2216 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2218 MAXIMUM_ALLOWED_ACCESS,
2221 if (!NT_STATUS_IS_OK(result)) {
2225 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2227 &num_rids, &rids, &rid_types);
2229 if (!NT_STATUS_IS_OK(result)) {
2230 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2234 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2236 MAXIMUM_ALLOWED_ACCESS,
2240 if (!NT_STATUS_IS_OK(result)) {
2244 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2247 0x0005); /* unknown flags */
2250 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2254 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2255 TALLOC_CTX *mem_ctx,
2256 const DOM_SID *alias_sid,
2259 POLICY_HND connect_pol, domain_pol;
2262 POLICY_HND alias_pol;
2265 enum lsa_SidType member_type;
2269 sid_copy(&sid, alias_sid);
2271 if (!sid_split_rid(&sid, &alias_rid)) {
2272 return NT_STATUS_UNSUCCESSFUL;
2275 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2276 &member_sid, &member_type);
2278 if (!NT_STATUS_IS_OK(result)) {
2279 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2283 /* Get sam policy handle */
2284 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2285 pipe_hnd->cli->desthost,
2286 MAXIMUM_ALLOWED_ACCESS,
2288 if (!NT_STATUS_IS_OK(result)) {
2292 /* Get domain policy handle */
2293 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2295 MAXIMUM_ALLOWED_ACCESS,
2298 if (!NT_STATUS_IS_OK(result)) {
2302 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2304 MAXIMUM_ALLOWED_ACCESS,
2308 if (!NT_STATUS_IS_OK(result)) {
2312 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2316 if (!NT_STATUS_IS_OK(result)) {
2321 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2325 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2326 const char *domain_name,
2327 struct cli_state *cli,
2328 struct rpc_pipe_client *pipe_hnd,
2329 TALLOC_CTX *mem_ctx,
2334 enum lsa_SidType group_type;
2337 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2338 return NT_STATUS_UNSUCCESSFUL;
2341 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2342 &group_sid, &group_type))) {
2343 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2344 return NT_STATUS_UNSUCCESSFUL;
2347 if (group_type == SID_NAME_DOM_GRP) {
2348 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2349 &group_sid, argv[1]);
2351 if (!NT_STATUS_IS_OK(result)) {
2352 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2353 argv[1], argv[0], nt_errstr(result));
2358 if (group_type == SID_NAME_ALIAS) {
2359 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2360 &group_sid, argv[1]);
2362 if (!NT_STATUS_IS_OK(result)) {
2363 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2364 argv[1], argv[0], nt_errstr(result));
2369 d_fprintf(stderr, "Can only add members to global or local groups "
2370 "which %s is not\n", argv[0]);
2372 return NT_STATUS_UNSUCCESSFUL;
2375 static int rpc_group_addmem(int argc, const char **argv)
2377 return run_rpc_command(NULL, PI_SAMR, 0,
2378 rpc_group_addmem_internals,
2382 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2383 TALLOC_CTX *mem_ctx,
2384 const DOM_SID *group_sid,
2387 POLICY_HND connect_pol, domain_pol;
2390 POLICY_HND group_pol;
2393 uint32 *rids = NULL;
2394 uint32 *rid_types = NULL;
2398 sid_copy(&sid, group_sid);
2400 if (!sid_split_rid(&sid, &group_rid))
2401 return NT_STATUS_UNSUCCESSFUL;
2403 /* Get sam policy handle */
2404 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2405 pipe_hnd->cli->desthost,
2406 MAXIMUM_ALLOWED_ACCESS,
2408 if (!NT_STATUS_IS_OK(result))
2411 /* Get domain policy handle */
2412 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2414 MAXIMUM_ALLOWED_ACCESS,
2417 if (!NT_STATUS_IS_OK(result))
2420 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2422 &num_rids, &rids, &rid_types);
2424 if (!NT_STATUS_IS_OK(result)) {
2425 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2429 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2431 MAXIMUM_ALLOWED_ACCESS,
2435 if (!NT_STATUS_IS_OK(result))
2438 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2443 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2447 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2448 TALLOC_CTX *mem_ctx,
2449 const DOM_SID *alias_sid,
2452 POLICY_HND connect_pol, domain_pol;
2455 POLICY_HND alias_pol;
2458 enum lsa_SidType member_type;
2462 sid_copy(&sid, alias_sid);
2464 if (!sid_split_rid(&sid, &alias_rid))
2465 return NT_STATUS_UNSUCCESSFUL;
2467 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2468 &member_sid, &member_type);
2470 if (!NT_STATUS_IS_OK(result)) {
2471 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2475 /* Get sam policy handle */
2476 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2477 pipe_hnd->cli->desthost,
2478 MAXIMUM_ALLOWED_ACCESS,
2480 if (!NT_STATUS_IS_OK(result)) {
2484 /* Get domain policy handle */
2485 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2487 MAXIMUM_ALLOWED_ACCESS,
2490 if (!NT_STATUS_IS_OK(result)) {
2494 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2496 MAXIMUM_ALLOWED_ACCESS,
2500 if (!NT_STATUS_IS_OK(result))
2503 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2507 if (!NT_STATUS_IS_OK(result))
2511 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2515 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2516 const char *domain_name,
2517 struct cli_state *cli,
2518 struct rpc_pipe_client *pipe_hnd,
2519 TALLOC_CTX *mem_ctx,
2524 enum lsa_SidType group_type;
2527 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2528 return NT_STATUS_UNSUCCESSFUL;
2531 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2532 &group_sid, &group_type))) {
2533 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2534 return NT_STATUS_UNSUCCESSFUL;
2537 if (group_type == SID_NAME_DOM_GRP) {
2538 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2539 &group_sid, argv[1]);
2541 if (!NT_STATUS_IS_OK(result)) {
2542 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2543 argv[1], argv[0], nt_errstr(result));
2548 if (group_type == SID_NAME_ALIAS) {
2549 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2550 &group_sid, argv[1]);
2552 if (!NT_STATUS_IS_OK(result)) {
2553 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2554 argv[1], argv[0], nt_errstr(result));
2559 d_fprintf(stderr, "Can only delete members from global or local groups "
2560 "which %s is not\n", argv[0]);
2562 return NT_STATUS_UNSUCCESSFUL;
2565 static int rpc_group_delmem(int argc, const char **argv)
2567 return run_rpc_command(NULL, PI_SAMR, 0,
2568 rpc_group_delmem_internals,
2573 * List groups on a remote RPC server
2575 * All parameters are provided by the run_rpc_command function, except for
2576 * argc, argv which are passes through.
2578 * @param domain_sid The domain sid acquired from the remote server
2579 * @param cli A cli_state connected to the server.
2580 * @param mem_ctx Talloc context, destoyed on completion of the function.
2581 * @param argc Standard main() style argc
2582 * @param argv Standard main() style argv. Initial components are already
2585 * @return Normal NTSTATUS return.
2588 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2589 const char *domain_name,
2590 struct cli_state *cli,
2591 struct rpc_pipe_client *pipe_hnd,
2592 TALLOC_CTX *mem_ctx,
2596 POLICY_HND connect_pol, domain_pol;
2597 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2598 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2599 struct acct_info *groups;
2600 bool global = False;
2602 bool builtin = False;
2610 for (i=0; i<argc; i++) {
2611 if (strequal(argv[i], "global"))
2614 if (strequal(argv[i], "local"))
2617 if (strequal(argv[i], "builtin"))
2621 /* Get sam policy handle */
2623 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2624 pipe_hnd->cli->desthost,
2625 MAXIMUM_ALLOWED_ACCESS,
2627 if (!NT_STATUS_IS_OK(result)) {
2631 /* Get domain policy handle */
2633 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2635 MAXIMUM_ALLOWED_ACCESS,
2636 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2638 if (!NT_STATUS_IS_OK(result)) {
2642 /* Query domain groups */
2643 if (opt_long_list_entries)
2644 d_printf("\nGroup name Comment"\
2645 "\n-----------------------------\n");
2647 SAM_DISPINFO_CTR ctr;
2648 SAM_DISPINFO_3 info3;
2653 ctr.sam.info3 = &info3;
2657 get_query_dispinfo_params(
2658 loop_count, &max_entries, &max_size);
2660 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2661 &start_idx, 3, &num_entries,
2662 max_entries, max_size, &ctr);
2664 if (!NT_STATUS_IS_OK(result) &&
2665 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2668 for (i = 0; i < num_entries; i++) {
2670 fstring group, desc;
2672 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group));
2673 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc));
2675 if (opt_long_list_entries)
2676 printf("%-21.21s %-50.50s\n",
2679 printf("%s\n", group);
2681 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2682 /* query domain aliases */
2687 /* The max_size field in cli_samr_enum_als_groups is more like
2688 * an account_control field with indiviual bits what to
2689 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2690 * everything. I'm too lazy (sorry) to get this through to
2691 * rpc_parse/ etc. Volker */
2693 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2695 &groups, &num_entries);
2697 if (!NT_STATUS_IS_OK(result) &&
2698 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2701 for (i = 0; i < num_entries; i++) {
2703 char *description = NULL;
2705 if (opt_long_list_entries) {
2707 POLICY_HND alias_pol;
2710 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2715 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2718 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2720 description = unistr2_to_ascii_talloc(mem_ctx,
2721 ctr.alias.info3.description.string);
2725 if (description != NULL) {
2726 printf("%-21.21s %-50.50s\n",
2727 groups[i].acct_name,
2730 printf("%s\n", groups[i].acct_name);
2733 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2734 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2735 /* Get builtin policy handle */
2737 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2739 MAXIMUM_ALLOWED_ACCESS,
2740 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2742 if (!NT_STATUS_IS_OK(result)) {
2745 /* query builtin aliases */
2748 if (!builtin) break;
2750 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2751 &start_idx, max_entries,
2752 &groups, &num_entries);
2754 if (!NT_STATUS_IS_OK(result) &&
2755 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2758 for (i = 0; i < num_entries; i++) {
2760 char *description = NULL;
2762 if (opt_long_list_entries) {
2764 POLICY_HND alias_pol;
2767 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2772 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2775 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2777 description = unistr2_to_ascii_talloc(mem_ctx,
2778 ctr.alias.info3.description.string);
2782 if (description != NULL) {
2783 printf("%-21.21s %-50.50s\n",
2784 groups[i].acct_name,
2787 printf("%s\n", groups[i].acct_name);
2790 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2796 static int rpc_group_list(int argc, const char **argv)
2798 return run_rpc_command(NULL, PI_SAMR, 0,
2799 rpc_group_list_internals,
2803 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2804 TALLOC_CTX *mem_ctx,
2805 const char *domain_name,
2806 const DOM_SID *domain_sid,
2807 POLICY_HND *domain_pol,
2811 POLICY_HND group_pol;
2812 uint32 num_members, *group_rids;
2817 struct samr_RidTypeArray *rids = NULL;
2820 sid_to_fstring(sid_str, domain_sid);
2822 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2824 MAXIMUM_ALLOWED_ACCESS,
2828 if (!NT_STATUS_IS_OK(result))
2831 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2835 if (!NT_STATUS_IS_OK(result))
2838 num_members = rids->count;
2839 group_rids = rids->rids;
2841 while (num_members > 0) {
2842 int this_time = 512;
2844 if (num_members < this_time)
2845 this_time = num_members;
2847 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2848 this_time, group_rids,
2849 &num_names, &names, &name_types);
2851 if (!NT_STATUS_IS_OK(result))
2854 /* We only have users as members, but make the output
2855 the same as the output of alias members */
2857 for (i = 0; i < this_time; i++) {
2859 if (opt_long_list_entries) {
2860 printf("%s-%d %s\\%s %d\n", sid_str,
2861 group_rids[i], domain_name, names[i],
2864 printf("%s\\%s\n", domain_name, names[i]);
2868 num_members -= this_time;
2872 return NT_STATUS_OK;
2875 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2876 TALLOC_CTX *mem_ctx,
2877 POLICY_HND *domain_pol,
2881 struct rpc_pipe_client *lsa_pipe;
2882 POLICY_HND alias_pol, lsa_pol;
2884 DOM_SID *alias_sids;
2887 enum lsa_SidType *types;
2889 struct lsa_SidArray sid_array;
2891 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2893 MAXIMUM_ALLOWED_ACCESS,
2897 if (!NT_STATUS_IS_OK(result))
2900 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2904 if (!NT_STATUS_IS_OK(result)) {
2905 d_fprintf(stderr, "Couldn't list alias members\n");
2909 num_members = sid_array.num_sids;
2911 if (num_members == 0) {
2912 return NT_STATUS_OK;
2915 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2917 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2918 nt_errstr(result) );
2922 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2923 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2925 if (!NT_STATUS_IS_OK(result)) {
2926 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2927 cli_rpc_pipe_close(lsa_pipe);
2931 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2933 d_fprintf(stderr, "Out of memory\n");
2934 cli_rpc_pipe_close(lsa_pipe);
2935 return NT_STATUS_NO_MEMORY;
2938 for (i=0; i<num_members; i++) {
2939 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2942 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2944 &domains, &names, &types);
2946 if (!NT_STATUS_IS_OK(result) &&
2947 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2948 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2949 cli_rpc_pipe_close(lsa_pipe);
2953 for (i = 0; i < num_members; i++) {
2955 sid_to_fstring(sid_str, &alias_sids[i]);
2957 if (opt_long_list_entries) {
2958 printf("%s %s\\%s %d\n", sid_str,
2959 domains[i] ? domains[i] : "*unknown*",
2960 names[i] ? names[i] : "*unknown*", types[i]);
2963 printf("%s\\%s\n", domains[i], names[i]);
2965 printf("%s\n", sid_str);
2969 cli_rpc_pipe_close(lsa_pipe);
2970 return NT_STATUS_OK;
2973 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2974 const char *domain_name,
2975 struct cli_state *cli,
2976 struct rpc_pipe_client *pipe_hnd,
2977 TALLOC_CTX *mem_ctx,
2982 POLICY_HND connect_pol, domain_pol;
2983 uint32 num_rids, *rids, *rid_types;
2985 /* Get sam policy handle */
2987 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2988 pipe_hnd->cli->desthost,
2989 MAXIMUM_ALLOWED_ACCESS,
2992 if (!NT_STATUS_IS_OK(result))
2995 /* Get domain policy handle */
2997 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2999 MAXIMUM_ALLOWED_ACCESS,
3000 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3003 if (!NT_STATUS_IS_OK(result))
3006 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
3007 1, argv, &num_rids, &rids, &rid_types);
3009 if (!NT_STATUS_IS_OK(result)) {
3011 /* Ok, did not find it in the global sam, try with builtin */
3013 DOM_SID sid_Builtin;
3015 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3017 string_to_sid(&sid_Builtin, "S-1-5-32");
3019 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3021 MAXIMUM_ALLOWED_ACCESS,
3025 if (!NT_STATUS_IS_OK(result)) {
3026 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3030 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
3034 if (!NT_STATUS_IS_OK(result)) {
3035 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3040 if (num_rids != 1) {
3041 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3045 if (rid_types[0] == SID_NAME_DOM_GRP) {
3046 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
3047 domain_sid, &domain_pol,
3051 if (rid_types[0] == SID_NAME_ALIAS) {
3052 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
3056 return NT_STATUS_NO_SUCH_GROUP;
3059 static int rpc_group_members(int argc, const char **argv)
3062 return rpc_group_usage(argc, argv);
3065 return run_rpc_command(NULL, PI_SAMR, 0,
3066 rpc_group_members_internals,
3070 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
3071 const char *domain_name,
3072 struct cli_state *cli,
3073 struct rpc_pipe_client *pipe_hnd,
3074 TALLOC_CTX *mem_ctx,
3079 POLICY_HND connect_pol, domain_pol, group_pol;
3080 uint32 num_rids, *rids, *rid_types;
3081 union samr_GroupInfo group_info;
3084 d_printf("Usage: 'net rpc group rename group newname'\n");
3085 return NT_STATUS_UNSUCCESSFUL;
3088 /* Get sam policy handle */
3090 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3091 pipe_hnd->cli->desthost,
3092 MAXIMUM_ALLOWED_ACCESS,
3095 if (!NT_STATUS_IS_OK(result))
3098 /* Get domain policy handle */
3100 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3102 MAXIMUM_ALLOWED_ACCESS,
3103 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3106 if (!NT_STATUS_IS_OK(result))
3109 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
3110 1, argv, &num_rids, &rids, &rid_types);
3112 if (num_rids != 1) {
3113 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3117 if (rid_types[0] != SID_NAME_DOM_GRP) {
3118 d_fprintf(stderr, "Can only rename domain groups\n");
3119 return NT_STATUS_UNSUCCESSFUL;
3122 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
3124 MAXIMUM_ALLOWED_ACCESS,
3128 if (!NT_STATUS_IS_OK(result))
3131 init_lsa_String(&group_info.name, argv[1]);
3133 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
3138 if (!NT_STATUS_IS_OK(result))
3141 return NT_STATUS_NO_SUCH_GROUP;
3144 static int rpc_group_rename(int argc, const char **argv)
3147 return rpc_group_usage(argc, argv);
3150 return run_rpc_command(NULL, PI_SAMR, 0,
3151 rpc_group_rename_internals,
3156 * 'net rpc group' entrypoint.
3157 * @param argc Standard main() style argc
3158 * @param argc Standard main() style argv. Initial components are already
3162 int net_rpc_group(int argc, const char **argv)
3164 struct functable func[] = {
3165 {"add", rpc_group_add},
3166 {"delete", rpc_group_delete},
3167 {"addmem", rpc_group_addmem},
3168 {"delmem", rpc_group_delmem},
3169 {"list", rpc_group_list},
3170 {"members", rpc_group_members},
3171 {"rename", rpc_group_rename},
3176 return run_rpc_command(NULL, PI_SAMR, 0,
3177 rpc_group_list_internals,
3181 return net_run_function(argc, argv, func, rpc_group_usage);
3184 /****************************************************************************/
3186 static int rpc_share_usage(int argc, const char **argv)
3188 return net_help_share(argc, argv);
3192 * Add a share on a remote RPC server
3194 * All parameters are provided by the run_rpc_command function, except for
3195 * argc, argv which are passes through.
3197 * @param domain_sid The domain sid acquired from the remote server
3198 * @param cli A cli_state connected to the server.
3199 * @param mem_ctx Talloc context, destoyed on completion of the function.
3200 * @param argc Standard main() style argc
3201 * @param argv Standard main() style argv. Initial components are already
3204 * @return Normal NTSTATUS return.
3206 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3207 const char *domain_name,
3208 struct cli_state *cli,
3209 struct rpc_pipe_client *pipe_hnd,
3210 TALLOC_CTX *mem_ctx,int argc,
3216 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3217 uint32 num_users=0, perms=0;
3218 char *password=NULL; /* don't allow a share password */
3221 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3222 return NT_STATUS_NO_MEMORY;
3225 path = strchr(sharename, '=');
3227 return NT_STATUS_UNSUCCESSFUL;
3230 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
3231 opt_comment, perms, opt_maxusers,
3232 num_users, path, password,
3234 return werror_to_ntstatus(result);
3237 static int rpc_share_add(int argc, const char **argv)
3239 if ((argc < 1) || !strchr(argv[0], '=')) {
3240 DEBUG(1,("Sharename or path not specified on add\n"));
3241 return rpc_share_usage(argc, argv);
3243 return run_rpc_command(NULL, PI_SRVSVC, 0,
3244 rpc_share_add_internals,
3249 * Delete a share on a remote RPC server
3251 * All parameters are provided by the run_rpc_command function, except for
3252 * argc, argv which are passes through.
3254 * @param domain_sid The domain sid acquired from the remote server
3255 * @param cli A cli_state connected to the server.
3256 * @param mem_ctx Talloc context, destoyed on completion of the function.
3257 * @param argc Standard main() style argc
3258 * @param argv Standard main() style argv. Initial components are already
3261 * @return Normal NTSTATUS return.
3263 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3264 const char *domain_name,
3265 struct cli_state *cli,
3266 struct rpc_pipe_client *pipe_hnd,
3267 TALLOC_CTX *mem_ctx,
3273 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
3274 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3278 * Delete a share on a remote RPC server
3280 * @param domain_sid The domain sid acquired from the remote server
3281 * @param argc Standard main() style argc
3282 * @param argv Standard main() style argv. Initial components are already
3285 * @return A shell status integer (0 for success)
3287 static int rpc_share_delete(int argc, const char **argv)
3290 DEBUG(1,("Sharename not specified on delete\n"));
3291 return rpc_share_usage(argc, argv);
3293 return run_rpc_command(NULL, PI_SRVSVC, 0,
3294 rpc_share_del_internals,
3299 * Formatted print of share info
3301 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3304 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3306 fstring netname = "", remark = "";
3308 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3309 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3311 if (opt_long_list_entries) {
3312 d_printf("%-12s %-8.8s %-50s\n",
3313 netname, share_type[info1->info_1.type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)], remark);
3315 d_printf("%s\n", netname);
3320 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3321 TALLOC_CTX *mem_ctx,
3325 SRV_SHARE_INFO_CTR *ctr)
3328 SRV_SHARE_INFO info;
3330 /* no specific share requested, enumerate all */
3334 uint32 preferred_len = 0xffffffff;
3336 init_enum_hnd(&hnd, 0);
3338 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3339 preferred_len, &hnd);
3342 /* request just one share */
3343 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3345 if (!W_ERROR_IS_OK(result))
3351 ctr->info_level = ctr->switch_value = level;
3352 ctr->ptr_share_info = ctr->ptr_entries = 1;
3353 ctr->num_entries = ctr->num_entries2 = 1;
3359 SRV_SHARE_INFO_1 *info1;
3361 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3362 if (ctr->share.info1 == NULL) {
3363 result = WERR_NOMEM;
3366 info1 = ctr->share.info1;
3368 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3370 /* Copy pointer crap */
3372 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3374 /* Duplicate strings */
3376 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3378 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3380 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3382 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3387 SRV_SHARE_INFO_2 *info2;
3389 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3390 if (ctr->share.info2 == NULL) {
3391 result = WERR_NOMEM;
3394 info2 = ctr->share.info2;
3396 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3398 /* Copy pointer crap */
3400 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3402 /* Duplicate strings */
3404 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3406 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3408 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3410 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3412 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_path);
3414 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3416 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3418 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3423 SRV_SHARE_INFO_502 *info502;
3425 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3426 if (ctr->share.info502 == NULL) {
3427 result = WERR_NOMEM;
3430 info502 = ctr->share.info502;
3432 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3434 /* Copy pointer crap */
3436 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3438 /* Duplicate strings */
3440 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3442 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3444 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3446 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3448 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_path);
3450 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3452 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3454 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3456 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3467 * List shares on a remote RPC server
3469 * All parameters are provided by the run_rpc_command function, except for
3470 * argc, argv which are passes through.
3472 * @param domain_sid The domain sid acquired from the remote server
3473 * @param cli A cli_state connected to the server.
3474 * @param mem_ctx Talloc context, destoyed on completion of the function.
3475 * @param argc Standard main() style argc
3476 * @param argv Standard main() style argv. Initial components are already
3479 * @return Normal NTSTATUS return.
3482 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3483 const char *domain_name,
3484 struct cli_state *cli,
3485 struct rpc_pipe_client *pipe_hnd,
3486 TALLOC_CTX *mem_ctx,
3490 SRV_SHARE_INFO_CTR ctr;
3492 uint32 i, level = 1;
3494 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3495 if (!W_ERROR_IS_OK(result))
3498 /* Display results */
3500 if (opt_long_list_entries) {
3502 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3503 "\nShare name Type Description\n"\
3504 "---------- ---- -----------\n");
3506 for (i = 0; i < ctr.num_entries; i++)
3507 display_share_info_1(&ctr.share.info1[i]);
3509 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3513 * 'net rpc share list' entrypoint.
3514 * @param argc Standard main() style argc
3515 * @param argv Standard main() style argv. Initial components are already
3518 static int rpc_share_list(int argc, const char **argv)
3520 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3523 static bool check_share_availability(struct cli_state *cli, const char *netname)
3525 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3526 d_printf("skipping [%s]: not a file share.\n", netname);
3536 static bool check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3538 /* only support disk shares */
3539 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3540 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3544 /* skip builtin shares */
3545 /* FIXME: should print$ be added too ? */
3546 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3547 strequal(netname,"global"))
3550 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3551 printf("excluding [%s]\n", netname);
3555 return check_share_availability(cli, netname);
3559 * Migrate shares from a remote RPC server to the local RPC srever
3561 * All parameters are provided by the run_rpc_command function, except for
3562 * argc, argv which are passes through.
3564 * @param domain_sid The domain sid acquired from the remote server
3565 * @param cli A cli_state connected to the server.
3566 * @param mem_ctx Talloc context, destoyed on completion of the function.
3567 * @param argc Standard main() style argc
3568 * @param argv Standard main() style argv. Initial components are already
3571 * @return Normal NTSTATUS return.
3574 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3575 const char *domain_name,
3576 struct cli_state *cli,
3577 struct rpc_pipe_client *pipe_hnd,
3578 TALLOC_CTX *mem_ctx,
3583 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3584 SRV_SHARE_INFO_CTR ctr_src;
3585 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3586 char *password = NULL; /* don't allow a share password */
3588 struct rpc_pipe_client *srvsvc_pipe = NULL;
3589 struct cli_state *cli_dst = NULL;
3590 uint32 level = 502; /* includes secdesc */
3592 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3593 if (!W_ERROR_IS_OK(result))
3596 /* connect destination PI_SRVSVC */
3597 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3598 if (!NT_STATUS_IS_OK(nt_status))
3602 for (i = 0; i < ctr_src.num_entries; i++) {
3604 fstring netname = "", remark = "", path = "";
3605 /* reset error-code */
3606 nt_status = NT_STATUS_UNSUCCESSFUL;
3608 rpcstr_pull_unistr2_fstring(
3609 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3610 rpcstr_pull_unistr2_fstring(
3611 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3612 rpcstr_pull_unistr2_fstring(
3613 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3615 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3618 /* finally add the share on the dst server */
3620 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3621 netname, path, remark);
3623 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3624 ctr_src.share.info502[i].info_502.perms,
3625 ctr_src.share.info502[i].info_502.max_uses,
3626 ctr_src.share.info502[i].info_502.num_uses,
3627 path, password, level,
3630 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3631 printf(" [%s] does already exist\n", netname);
3635 if (!W_ERROR_IS_OK(result)) {
3636 printf("cannot add share: %s\n", dos_errstr(result));
3642 nt_status = NT_STATUS_OK;
3646 cli_shutdown(cli_dst);
3654 * Migrate shares from a rpc-server to another
3656 * @param argc Standard main() style argc
3657 * @param argv Standard main() style argv. Initial components are already
3660 * @return A shell status integer (0 for success)
3662 static int rpc_share_migrate_shares(int argc, const char **argv)
3666 printf("no server to migrate\n");
3670 return run_rpc_command(NULL, PI_SRVSVC, 0,
3671 rpc_share_migrate_shares_internals,
3678 * @param f file_info
3679 * @param mask current search mask
3680 * @param state arg-pointer
3683 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3685 static NTSTATUS nt_status;
3686 static struct copy_clistate *local_state;
3687 static fstring filename, new_mask;
3691 local_state = (struct copy_clistate *)state;
3692 nt_status = NT_STATUS_UNSUCCESSFUL;
3694 if (strequal(f->name, ".") || strequal(f->name, ".."))
3697 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3700 if (f->mode & aDIR) {
3702 DEBUG(3,("got dir: %s\n", f->name));
3704 fstrcpy(dir, local_state->cwd);
3706 fstrcat(dir, f->name);
3708 switch (net_mode_share)
3710 case NET_MODE_SHARE_MIGRATE:
3711 /* create that directory */
3712 nt_status = net_copy_file(local_state->mem_ctx,
3713 local_state->cli_share_src,
3714 local_state->cli_share_dst,
3716 opt_acls? True : False,
3717 opt_attrs? True : False,
3718 opt_timestamps? True : False,
3722 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3726 if (!NT_STATUS_IS_OK(nt_status))
3727 printf("could not handle dir %s: %s\n",
3728 dir, nt_errstr(nt_status));
3730 /* search below that directory */
3731 fstrcpy(new_mask, dir);
3732 fstrcat(new_mask, "\\*");
3734 old_dir = local_state->cwd;
3735 local_state->cwd = dir;
3736 if (!sync_files(local_state, new_mask))
3737 printf("could not handle files\n");
3738 local_state->cwd = old_dir;
3745 fstrcpy(filename, local_state->cwd);
3746 fstrcat(filename, "\\");
3747 fstrcat(filename, f->name);
3749 DEBUG(3,("got file: %s\n", filename));
3751 switch (net_mode_share)
3753 case NET_MODE_SHARE_MIGRATE:
3754 nt_status = net_copy_file(local_state->mem_ctx,
3755 local_state->cli_share_src,
3756 local_state->cli_share_dst,
3758 opt_acls? True : False,
3759 opt_attrs? True : False,
3760 opt_timestamps? True: False,
3764 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3768 if (!NT_STATUS_IS_OK(nt_status))
3769 printf("could not handle file %s: %s\n",
3770 filename, nt_errstr(nt_status));
3775 * sync files, can be called recursivly to list files
3776 * and then call copy_fn for each file
3778 * @param cp_clistate pointer to the copy_clistate we work with
3779 * @param mask the current search mask
3781 * @return Boolean result
3783 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3785 struct cli_state *targetcli;
3786 char *targetpath = NULL;
3788 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3790 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3791 mask, &targetcli, &targetpath ) ) {
3792 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3793 mask, cli_errstr(cp_clistate->cli_share_src));
3797 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3798 d_fprintf(stderr, "listing %s failed with error: %s\n",
3799 mask, cli_errstr(targetcli));
3808 * Set the top level directory permissions before we do any further copies.
3809 * Should set up ACL inheritance.
3812 bool copy_top_level_perms(struct copy_clistate *cp_clistate,
3813 const char *sharename)
3815 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3817 switch (net_mode_share) {
3818 case NET_MODE_SHARE_MIGRATE:
3819 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3820 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3821 cp_clistate->cli_share_src,
3822 cp_clistate->cli_share_dst,
3824 opt_acls? True : False,
3825 opt_attrs? True : False,
3826 opt_timestamps? True: False,
3830 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3834 if (!NT_STATUS_IS_OK(nt_status)) {
3835 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3836 sharename, nt_errstr(nt_status));
3844 * Sync all files inside a remote share to another share (over smb)
3846 * All parameters are provided by the run_rpc_command function, except for
3847 * argc, argv which are passes through.
3849 * @param domain_sid The domain sid acquired from the remote server
3850 * @param cli A cli_state connected to the server.
3851 * @param mem_ctx Talloc context, destoyed on completion of the function.
3852 * @param argc Standard main() style argc
3853 * @param argv Standard main() style argv. Initial components are already
3856 * @return Normal NTSTATUS return.
3859 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3860 const char *domain_name,
3861 struct cli_state *cli,
3862 struct rpc_pipe_client *pipe_hnd,
3863 TALLOC_CTX *mem_ctx,
3868 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3869 SRV_SHARE_INFO_CTR ctr_src;
3872 struct copy_clistate cp_clistate;
3873 bool got_src_share = False;
3874 bool got_dst_share = False;
3875 const char *mask = "\\*";
3878 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3880 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3882 if (!W_ERROR_IS_OK(result))
3885 for (i = 0; i < ctr_src.num_entries; i++) {
3887 fstring netname = "";
3889 rpcstr_pull_unistr2_fstring(
3890 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3892 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3895 /* one might not want to mirror whole discs :) */
3896 if (strequal(netname, "print$") || netname[1] == '$') {
3897 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3901 switch (net_mode_share)
3903 case NET_MODE_SHARE_MIGRATE:
3907 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3910 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3912 opt_acls ? "including" : "without",
3913 opt_attrs ? "including" : "without",
3914 opt_timestamps ? "(preserving timestamps)" : "");
3916 cp_clistate.mem_ctx = mem_ctx;
3917 cp_clistate.cli_share_src = NULL;
3918 cp_clistate.cli_share_dst = NULL;
3919 cp_clistate.cwd = NULL;
3920 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3922 /* open share source */
3923 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3924 &cli->dest_ss, cli->desthost,
3926 if (!NT_STATUS_IS_OK(nt_status))
3929 got_src_share = True;
3931 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3932 /* open share destination */
3933 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3934 NULL, dst, netname, "A:");
3935 if (!NT_STATUS_IS_OK(nt_status))
3938 got_dst_share = True;
3941 if (!copy_top_level_perms(&cp_clistate, netname)) {
3942 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
3943 nt_status = NT_STATUS_UNSUCCESSFUL;
3947 if (!sync_files(&cp_clistate, mask)) {
3948 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
3949 nt_status = NT_STATUS_UNSUCCESSFUL;
3954 nt_status = NT_STATUS_OK;
3959 cli_shutdown(cp_clistate.cli_share_src);
3962 cli_shutdown(cp_clistate.cli_share_dst);
3968 static int rpc_share_migrate_files(int argc, const char **argv)
3972 printf("no server to migrate\n");
3976 return run_rpc_command(NULL, PI_SRVSVC, 0,
3977 rpc_share_migrate_files_internals,
3982 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3984 * All parameters are provided by the run_rpc_command function, except for
3985 * argc, argv which are passes through.
3987 * @param domain_sid The domain sid acquired from the remote server
3988 * @param cli A cli_state connected to the server.
3989 * @param mem_ctx Talloc context, destoyed on completion of the function.
3990 * @param argc Standard main() style argc
3991 * @param argv Standard main() style argv. Initial components are already
3994 * @return Normal NTSTATUS return.
3997 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3998 const char *domain_name,
3999 struct cli_state *cli,
4000 struct rpc_pipe_client *pipe_hnd,
4001 TALLOC_CTX *mem_ctx,
4006 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4007 SRV_SHARE_INFO_CTR ctr_src;
4008 SRV_SHARE_INFO info;
4010 struct rpc_pipe_client *srvsvc_pipe = NULL;
4011 struct cli_state *cli_dst = NULL;
4012 uint32 level = 502; /* includes secdesc */
4014 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
4016 if (!W_ERROR_IS_OK(result))
4019 /* connect destination PI_SRVSVC */
4020 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
4021 if (!NT_STATUS_IS_OK(nt_status))
4025 for (i = 0; i < ctr_src.num_entries; i++) {
4027 fstring netname = "", remark = "", path = "";
4028 /* reset error-code */
4029 nt_status = NT_STATUS_UNSUCCESSFUL;
4031 rpcstr_pull_unistr2_fstring(
4032 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
4033 rpcstr_pull_unistr2_fstring(
4034 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
4035 rpcstr_pull_unistr2_fstring(
4036 path, &ctr_src.share.info502[i].info_502_str.uni_path);
4038 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
4041 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
4042 netname, path, remark);
4045 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
4050 info.switch_value = level;
4051 info.ptr_share_ctr = 1;
4053 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4054 info.share.info502 = ctr_src.share.info502[i];
4056 /* finally modify the share on the dst server */
4057 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
4059 if (!W_ERROR_IS_OK(result)) {
4060 printf("cannot set share-acl: %s\n", dos_errstr(result));
4066 nt_status = NT_STATUS_OK;
4070 cli_shutdown(cli_dst);
4078 * Migrate share-acls from a rpc-server to another
4080 * @param argc Standard main() style argc
4081 * @param argv Standard main() style argv. Initial components are already
4084 * @return A shell status integer (0 for success)
4086 static int rpc_share_migrate_security(int argc, const char **argv)
4090 printf("no server to migrate\n");
4094 return run_rpc_command(NULL, PI_SRVSVC, 0,
4095 rpc_share_migrate_security_internals,
4100 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4101 * from one server to another
4103 * @param argc Standard main() style argc
4104 * @param argv Standard main() style argv. Initial components are already
4107 * @return A shell status integer (0 for success)
4110 static int rpc_share_migrate_all(int argc, const char **argv)
4115 printf("no server to migrate\n");
4119 /* order is important. we don't want to be locked out by the share-acl
4120 * before copying files - gd */
4122 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
4126 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
4130 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
4135 * 'net rpc share migrate' entrypoint.
4136 * @param argc Standard main() style argc
4137 * @param argv Standard main() style argv. Initial components are already
4140 static int rpc_share_migrate(int argc, const char **argv)
4143 struct functable func[] = {
4144 {"all", rpc_share_migrate_all},
4145 {"files", rpc_share_migrate_files},
4146 {"help", rpc_share_usage},
4147 {"security", rpc_share_migrate_security},
4148 {"shares", rpc_share_migrate_shares},
4152 net_mode_share = NET_MODE_SHARE_MIGRATE;
4154 return net_run_function(argc, argv, func, rpc_share_usage);
4163 static int num_server_aliases;
4164 static struct full_alias *server_aliases;
4167 * Add an alias to the static list.
4169 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4171 if (server_aliases == NULL)
4172 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4174 server_aliases[num_server_aliases] = *alias;
4175 num_server_aliases += 1;
4179 * For a specific domain on the server, fetch all the aliases
4180 * and their members. Add all of them to the server_aliases.
4183 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4184 TALLOC_CTX *mem_ctx,
4185 POLICY_HND *connect_pol,
4186 const DOM_SID *domain_sid)
4188 uint32 start_idx, max_entries, num_entries, i;
4189 struct acct_info *groups;
4191 POLICY_HND domain_pol;
4193 /* Get domain policy handle */
4195 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4197 MAXIMUM_ALLOWED_ACCESS,
4198 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4200 if (!NT_STATUS_IS_OK(result))
4207 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
4208 &start_idx, max_entries,
4209 &groups, &num_entries);
4211 for (i = 0; i < num_entries; i++) {
4213 POLICY_HND alias_pol;
4214 struct full_alias alias;
4215 struct lsa_SidArray sid_array;
4218 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4220 MAXIMUM_ALLOWED_ACCESS,
4223 if (!NT_STATUS_IS_OK(result))
4226 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4229 if (!NT_STATUS_IS_OK(result))
4232 alias.num_members = sid_array.num_sids;
4234 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4235 if (!NT_STATUS_IS_OK(result))
4238 alias.members = NULL;
4240 if (alias.num_members > 0) {
4241 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4243 for (j = 0; j < alias.num_members; j++)
4244 sid_copy(&alias.members[j],
4245 sid_array.sids[j].sid);
4248 sid_copy(&alias.sid, domain_sid);
4249 sid_append_rid(&alias.sid, groups[i].rid);
4251 push_alias(mem_ctx, &alias);
4253 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4255 result = NT_STATUS_OK;
4258 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4264 * Dump server_aliases as names for debugging purposes.
4267 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
4268 const char *domain_name,
4269 struct cli_state *cli,
4270 struct rpc_pipe_client *pipe_hnd,
4271 TALLOC_CTX *mem_ctx,
4279 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
4280 SEC_RIGHTS_MAXIMUM_ALLOWED,
4282 if (!NT_STATUS_IS_OK(result))
4285 for (i=0; i<num_server_aliases; i++) {
4288 enum lsa_SidType *types;
4291 struct full_alias *alias = &server_aliases[i];
4293 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4295 &domains, &names, &types);
4296 if (!NT_STATUS_IS_OK(result))
4299 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4301 if (alias->num_members == 0) {
4306 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4309 &domains, &names, &types);
4311 if (!NT_STATUS_IS_OK(result) &&
4312 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4315 for (j=0; j<alias->num_members; j++)
4316 DEBUG(1, ("%s\\%s (%d); ",
4317 domains[j] ? domains[j] : "*unknown*",
4318 names[j] ? names[j] : "*unknown*",types[j]));
4322 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4324 return NT_STATUS_OK;
4328 * Fetch a list of all server aliases and their members into
4332 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4333 const char *domain_name,
4334 struct cli_state *cli,
4335 struct rpc_pipe_client *pipe_hnd,
4336 TALLOC_CTX *mem_ctx,
4341 POLICY_HND connect_pol;
4343 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4344 pipe_hnd->cli->desthost,
4345 MAXIMUM_ALLOWED_ACCESS,
4348 if (!NT_STATUS_IS_OK(result))
4351 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4352 &global_sid_Builtin);
4354 if (!NT_STATUS_IS_OK(result))
4357 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4360 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4365 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4367 token->num_sids = 4;
4369 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4370 d_fprintf(stderr, "malloc failed\n");
4371 token->num_sids = 0;
4375 token->user_sids[0] = *user_sid;
4376 sid_copy(&token->user_sids[1], &global_sid_World);
4377 sid_copy(&token->user_sids[2], &global_sid_Network);
4378 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4381 static void free_user_token(NT_USER_TOKEN *token)
4383 SAFE_FREE(token->user_sids);
4386 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4390 for (i=0; i<token->num_sids; i++) {
4391 if (sid_compare(sid, &token->user_sids[i]) == 0)
4397 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4399 if (is_sid_in_token(token, sid))
4402 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4403 if (!token->user_sids) {
4407 sid_copy(&token->user_sids[token->num_sids], sid);
4409 token->num_sids += 1;
4414 NT_USER_TOKEN token;
4417 static void dump_user_token(struct user_token *token)
4421 d_printf("%s\n", token->name);
4423 for (i=0; i<token->token.num_sids; i++) {
4424 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4428 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4432 for (i=0; i<alias->num_members; i++) {
4433 if (sid_compare(sid, &alias->members[i]) == 0)
4440 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4444 for (i=0; i<num_server_aliases; i++) {
4445 if (is_alias_member(&sid, &server_aliases[i]))
4446 add_sid_to_token(token, &server_aliases[i].sid);
4451 * We got a user token with all the SIDs we can know about without asking the
4452 * server directly. These are the user and domain group sids. All of these can
4453 * be members of aliases. So scan the list of aliases for each of the SIDs and
4454 * add them to the token.
4457 static void collect_alias_memberships(NT_USER_TOKEN *token)
4459 int num_global_sids = token->num_sids;
4462 for (i=0; i<num_global_sids; i++) {
4463 collect_sid_memberships(token, token->user_sids[i]);
4467 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4469 struct winbindd_request request;
4470 struct winbindd_response response;
4478 fstr_sprintf(full_name, "%s%c%s",
4479 domain, *lp_winbind_separator(), user);
4481 /* First let's find out the user sid */
4483 ZERO_STRUCT(request);
4484 ZERO_STRUCT(response);
4486 fstrcpy(request.data.name.dom_name, domain);
4487 fstrcpy(request.data.name.name, user);
4489 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4491 if (result != NSS_STATUS_SUCCESS) {
4492 DEBUG(1, ("winbind could not find %s\n", full_name));
4496 if (response.data.sid.type != SID_NAME_USER) {
4497 DEBUG(1, ("%s is not a user\n", full_name));
4501 string_to_sid(&user_sid, response.data.sid.sid);
4503 init_user_token(token, &user_sid);
4505 /* And now the groups winbind knows about */
4507 ZERO_STRUCT(response);
4509 fstrcpy(request.data.username, full_name);
4511 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4513 if (result != NSS_STATUS_SUCCESS) {
4514 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4518 for (i = 0; i < response.data.num_entries; i++) {
4519 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4522 struct winbindd_request sidrequest;
4523 struct winbindd_response sidresponse;
4525 ZERO_STRUCT(sidrequest);
4526 ZERO_STRUCT(sidresponse);
4528 sidrequest.data.gid = gid;
4530 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4531 &sidrequest, &sidresponse);
4533 if (result != NSS_STATUS_SUCCESS) {
4534 DEBUG(1, ("winbind could not find SID of gid %d\n",
4539 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4541 string_to_sid(&sid, sidresponse.data.sid.sid);
4542 add_sid_to_token(token, &sid);
4545 SAFE_FREE(response.extra_data.data);
4551 * Get a list of all user tokens we want to look at
4554 static bool get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4556 struct winbindd_request request;
4557 struct winbindd_response response;
4558 const char *extra_data;
4561 struct user_token *result;
4562 TALLOC_CTX *frame = NULL;
4564 if (lp_winbind_use_default_domain() &&
4565 (opt_target_workgroup == NULL)) {
4566 d_fprintf(stderr, "winbind use default domain = yes set, "
4567 "please specify a workgroup\n");
4571 /* Send request to winbind daemon */
4573 ZERO_STRUCT(request);
4574 ZERO_STRUCT(response);
4576 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4580 /* Look through extra data */
4582 if (!response.extra_data.data)
4585 extra_data = (const char *)response.extra_data.data;
4588 frame = talloc_stackframe();
4589 while(next_token_talloc(frame, &extra_data, &name, ",")) {
4593 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4595 if (result == NULL) {
4596 DEBUG(1, ("Could not malloc sid array\n"));
4601 extra_data = (const char *)response.extra_data.data;
4604 while(next_token_talloc(frame, &extra_data, &name, ",")) {
4605 fstring domain, user;
4608 fstrcpy(result[i].name, name);
4610 p = strchr(name, *lp_winbind_separator());
4612 DEBUG(3, ("%s\n", name));
4615 fstrcpy(domain, opt_target_workgroup);
4616 fstrcpy(user, name);
4619 fstrcpy(domain, name);
4624 get_user_sids(domain, user, &(result[i].token));
4628 SAFE_FREE(response.extra_data.data);
4630 *user_tokens = result;
4635 static bool get_user_tokens_from_file(FILE *f,
4637 struct user_token **tokens)
4639 struct user_token *token = NULL;
4644 if (fgets(line, sizeof(line)-1, f) == NULL) {
4648 if (line[strlen(line)-1] == '\n')
4649 line[strlen(line)-1] = '\0';
4651 if (line[0] == ' ') {
4655 string_to_sid(&sid, &line[1]);
4657 if (token == NULL) {
4658 DEBUG(0, ("File does not begin with username"));
4662 add_sid_to_token(&token->token, &sid);
4666 /* And a new user... */
4669 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4670 if (*tokens == NULL) {
4671 DEBUG(0, ("Could not realloc tokens\n"));
4675 token = &((*tokens)[*num_tokens-1]);
4677 fstrcpy(token->name, line);
4678 token->token.num_sids = 0;
4679 token->token.user_sids = NULL;
4688 * Show the list of all users that have access to a share
4691 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4692 TALLOC_CTX *mem_ctx,
4693 const char *netname,
4695 struct user_token *tokens)
4698 SEC_DESC *share_sd = NULL;
4699 SEC_DESC *root_sd = NULL;
4700 struct cli_state *cli = pipe_hnd->cli;
4702 SRV_SHARE_INFO info;
4706 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4709 if (!W_ERROR_IS_OK(result)) {
4710 DEBUG(1, ("Coult not query secdesc for share %s\n",
4715 share_sd = info.share.info502.info_502_str.sd;
4716 if (share_sd == NULL) {
4717 DEBUG(1, ("Got no secdesc for share %s\n",
4723 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4727 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4730 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4733 for (i=0; i<num_tokens; i++) {
4737 if (share_sd != NULL) {
4738 if (!se_access_check(share_sd, &tokens[i].token,
4739 1, &acc_granted, &status)) {
4740 DEBUG(1, ("Could not check share_sd for "
4746 if (!NT_STATUS_IS_OK(status))
4750 if (root_sd == NULL) {
4751 d_printf(" %s\n", tokens[i].name);
4755 if (!se_access_check(root_sd, &tokens[i].token,
4756 1, &acc_granted, &status)) {
4757 DEBUG(1, ("Could not check root_sd for user %s\n",
4762 if (!NT_STATUS_IS_OK(status))
4765 d_printf(" %s\n", tokens[i].name);
4769 cli_close(cli, fnum);
4781 static void collect_share(const char *name, uint32 m,
4782 const char *comment, void *state)
4784 struct share_list *share_list = (struct share_list *)state;
4786 if (m != STYPE_DISKTREE)
4789 share_list->num_shares += 1;
4790 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4791 if (!share_list->shares) {
4792 share_list->num_shares = 0;
4795 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4798 static void rpc_share_userlist_usage(void)
4804 * List shares on a remote RPC server, including the security descriptors
4806 * All parameters are provided by the run_rpc_command function, except for
4807 * argc, argv which are passes through.
4809 * @param domain_sid The domain sid acquired from the remote server
4810 * @param cli A cli_state connected to the server.
4811 * @param mem_ctx Talloc context, destoyed on completion of the function.
4812 * @param argc Standard main() style argc
4813 * @param argv Standard main() style argv. Initial components are already
4816 * @return Normal NTSTATUS return.
4819 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4820 const char *domain_name,
4821 struct cli_state *cli,
4822 struct rpc_pipe_client *pipe_hnd,
4823 TALLOC_CTX *mem_ctx,
4833 struct user_token *tokens = NULL;
4836 struct share_list share_list;
4839 rpc_share_userlist_usage();
4840 return NT_STATUS_UNSUCCESSFUL;
4846 f = fopen(argv[0], "r");
4850 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4851 return NT_STATUS_UNSUCCESSFUL;
4854 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4860 DEBUG(0, ("Could not read users from file\n"));
4861 return NT_STATUS_UNSUCCESSFUL;
4864 for (i=0; i<num_tokens; i++)
4865 collect_alias_memberships(&tokens[i].token);
4867 init_enum_hnd(&hnd, 0);
4869 share_list.num_shares = 0;
4870 share_list.shares = NULL;
4872 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4875 DEBUG(0, ("Error returning browse list: %s\n",
4880 for (i = 0; i < share_list.num_shares; i++) {
4881 char *netname = share_list.shares[i];
4883 if (netname[strlen(netname)-1] == '$')
4886 d_printf("%s\n", netname);
4888 show_userlist(pipe_hnd, mem_ctx, netname,
4889 num_tokens, tokens);
4892 for (i=0; i<num_tokens; i++) {
4893 free_user_token(&tokens[i].token);
4896 SAFE_FREE(share_list.shares);
4898 return NT_STATUS_OK;
4901 static int rpc_share_allowedusers(int argc, const char **argv)
4905 result = run_rpc_command(NULL, PI_SAMR, 0,
4906 rpc_aliaslist_internals,
4911 result = run_rpc_command(NULL, PI_LSARPC, 0,
4917 return run_rpc_command(NULL, PI_SRVSVC, 0,
4918 rpc_share_allowedusers_internals,
4922 int net_usersidlist(int argc, const char **argv)
4925 struct user_token *tokens = NULL;
4929 net_usersidlist_usage(argc, argv);
4933 if (!get_user_tokens(&num_tokens, &tokens)) {
4934 DEBUG(0, ("Could not get the user/sid list\n"));
4938 for (i=0; i<num_tokens; i++) {
4939 dump_user_token(&tokens[i]);
4940 free_user_token(&tokens[i].token);
4947 int net_usersidlist_usage(int argc, const char **argv)
4949 d_printf("net usersidlist\n"
4950 "\tprints out a list of all users the running winbind knows\n"
4951 "\tabout, together with all their SIDs. This is used as\n"
4952 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4954 net_common_flags_usage(argc, argv);
4959 * 'net rpc share' entrypoint.
4960 * @param argc Standard main() style argc
4961 * @param argv Standard main() style argv. Initial components are already
4965 int net_rpc_share(int argc, const char **argv)
4967 struct functable func[] = {
4968 {"add", rpc_share_add},
4969 {"delete", rpc_share_delete},
4970 {"allowedusers", rpc_share_allowedusers},
4971 {"migrate", rpc_share_migrate},
4972 {"list", rpc_share_list},
4977 return run_rpc_command(NULL, PI_SRVSVC, 0,
4978 rpc_share_list_internals,
4981 return net_run_function(argc, argv, func, rpc_share_usage);
4984 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4985 struct rpc_sh_ctx *ctx,
4986 struct rpc_pipe_client *pipe_hnd,
4987 int argc, const char **argv)
4989 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4990 ctx->cli, pipe_hnd, mem_ctx,
4994 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4995 struct rpc_sh_ctx *ctx,
4996 struct rpc_pipe_client *pipe_hnd,
4997 int argc, const char **argv)
5001 if ((argc < 2) || (argc > 3)) {
5002 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
5004 return NT_STATUS_INVALID_PARAMETER;
5007 result = rpccli_srvsvc_net_share_add(
5008 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
5009 (argc == 3) ? argv[2] : "",
5010 0, 0, 0, argv[1], NULL, 2, NULL);
5012 return werror_to_ntstatus(result);
5015 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
5016 struct rpc_sh_ctx *ctx,
5017 struct rpc_pipe_client *pipe_hnd,
5018 int argc, const char **argv)
5023 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5024 return NT_STATUS_INVALID_PARAMETER;
5027 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
5028 return werror_to_ntstatus(result);
5031 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
5032 struct rpc_sh_ctx *ctx,
5033 struct rpc_pipe_client *pipe_hnd,
5034 int argc, const char **argv)
5036 SRV_SHARE_INFO info;
5037 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
5041 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5042 return NT_STATUS_INVALID_PARAMETER;
5045 result = rpccli_srvsvc_net_share_get_info(
5046 pipe_hnd, mem_ctx, argv[0], 2, &info);
5047 if (!W_ERROR_IS_OK(result)) {
5051 d_printf("Name: %s\n",
5052 rpcstr_pull_unistr2_talloc(mem_ctx,
5053 &info2->info_2_str.uni_netname));
5054 d_printf("Comment: %s\n",
5055 rpcstr_pull_unistr2_talloc(mem_ctx,
5056 &info2->info_2_str.uni_remark));
5058 d_printf("Path: %s\n",
5059 rpcstr_pull_unistr2_talloc(mem_ctx,
5060 &info2->info_2_str.uni_path));
5061 d_printf("Password: %s\n",
5062 rpcstr_pull_unistr2_talloc(mem_ctx,
5063 &info2->info_2_str.uni_passwd));
5066 return werror_to_ntstatus(result);
5069 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
5070 struct rpc_sh_ctx *ctx)
5072 static struct rpc_sh_cmd cmds[] = {
5074 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
5075 "List available shares" },
5077 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
5080 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
5083 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
5084 "Get information about a share" },
5086 { NULL, NULL, 0, NULL, NULL }
5092 /****************************************************************************/
5094 static int rpc_file_usage(int argc, const char **argv)
5096 return net_help_file(argc, argv);
5100 * Close a file on a remote RPC server
5102 * All parameters are provided by the run_rpc_command function, except for
5103 * argc, argv which are passes through.
5105 * @param domain_sid The domain sid acquired from the remote server
5106 * @param cli A cli_state connected to the server.
5107 * @param mem_ctx Talloc context, destoyed on completion of the function.
5108 * @param argc Standard main() style argc
5109 * @param argv Standard main() style argv. Initial components are already
5112 * @return Normal NTSTATUS return.
5114 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
5115 const char *domain_name,
5116 struct cli_state *cli,
5117 struct rpc_pipe_client *pipe_hnd,
5118 TALLOC_CTX *mem_ctx,
5122 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5123 pipe_hnd->cli->desthost,
5124 atoi(argv[0]), NULL);
5128 * Close a file on a remote RPC server
5130 * @param argc Standard main() style argc
5131 * @param argv Standard main() style argv. Initial components are already
5134 * @return A shell status integer (0 for success)
5136 static int rpc_file_close(int argc, const char **argv)
5139 DEBUG(1, ("No fileid given on close\n"));
5140 return(rpc_file_usage(argc, argv));
5143 return run_rpc_command(NULL, PI_SRVSVC, 0,
5144 rpc_file_close_internals,
5149 * Formatted print of open file info
5151 * @param info3 FILE_INFO_3 contents
5152 * @param str3 strings for FILE_INFO_3
5155 static void display_file_info_3( FILE_INFO_3 *info3 )
5157 fstring user = "", path = "";
5159 rpcstr_pull_unistr2_fstring(user, info3->user);
5160 rpcstr_pull_unistr2_fstring(path, info3->path);
5162 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5163 info3->id, user, info3->perms, info3->num_locks, path);
5167 * List open files on a remote RPC server
5169 * All parameters are provided by the run_rpc_command function, except for
5170 * argc, argv which are passes through.
5172 * @param domain_sid The domain sid acquired from the remote server
5173 * @param cli A cli_state connected to the server.
5174 * @param mem_ctx Talloc context, destoyed on completion of the function.
5175 * @param argc Standard main() style argc
5176 * @param argv Standard main() style argv. Initial components are already
5179 * @return Normal NTSTATUS return.
5182 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
5183 const char *domain_name,
5184 struct cli_state *cli,
5185 struct rpc_pipe_client *pipe_hnd,
5186 TALLOC_CTX *mem_ctx,
5190 SRV_FILE_INFO_CTR ctr;
5193 uint32 preferred_len = 0xffffffff, i;
5194 const char *username=NULL;
5196 init_enum_hnd(&hnd, 0);
5198 /* if argc > 0, must be user command */
5200 username = smb_xstrdup(argv[0]);
5202 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
5203 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
5205 if (!W_ERROR_IS_OK(result))
5208 /* Display results */
5211 "\nEnumerating open files on remote server:\n\n"\
5212 "\nFileId Opened by Perms Locks Path"\
5213 "\n------ --------- ----- ----- ---- \n");
5214 for (i = 0; i < ctr.num_entries; i++)
5215 display_file_info_3(&ctr.file.info3[i]);
5217 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5221 * List files for a user on a remote RPC server
5223 * @param argc Standard main() style argc
5224 * @param argv Standard main() style argv. Initial components are already
5227 * @return A shell status integer (0 for success)
5230 static int rpc_file_user(int argc, const char **argv)
5233 DEBUG(1, ("No username given\n"));
5234 return(rpc_file_usage(argc, argv));
5237 return run_rpc_command(NULL, PI_SRVSVC, 0,
5238 rpc_file_list_internals,
5243 * 'net rpc file' entrypoint.
5244 * @param argc Standard main() style argc
5245 * @param argv Standard main() style argv. Initial components are already
5249 int net_rpc_file(int argc, const char **argv)
5251 struct functable func[] = {
5252 {"close", rpc_file_close},
5253 {"user", rpc_file_user},
5255 {"info", rpc_file_info},
5261 return run_rpc_command(NULL, PI_SRVSVC, 0,
5262 rpc_file_list_internals,
5265 return net_run_function(argc, argv, func, rpc_file_usage);
5269 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5271 * All parameters are provided by the run_rpc_command function, except for
5272 * argc, argv which are passed through.
5274 * @param domain_sid The domain sid aquired from the remote server
5275 * @param cli A cli_state connected to the server.
5276 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5277 * @param argc Standard main() style argc
5278 * @param argv Standard main() style argv. Initial components are already
5281 * @return Normal NTSTATUS return.
5284 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
5285 const char *domain_name,
5286 struct cli_state *cli,
5287 struct rpc_pipe_client *pipe_hnd,
5288 TALLOC_CTX *mem_ctx,
5292 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5294 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5296 if (NT_STATUS_IS_OK(result)) {
5297 d_printf("\nShutdown successfully aborted\n");
5298 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5300 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5306 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5308 * All parameters are provided by the run_rpc_command function, except for
5309 * argc, argv which are passed through.
5311 * @param domain_sid The domain sid aquired from the remote server
5312 * @param cli A cli_state connected to the server.
5313 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5314 * @param argc Standard main() style argc
5315 * @param argv Standard main() style argv. Initial components are already
5318 * @return Normal NTSTATUS return.
5321 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5322 const char *domain_name,
5323 struct cli_state *cli,
5324 struct rpc_pipe_client *pipe_hnd,
5325 TALLOC_CTX *mem_ctx,
5329 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5331 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5333 if (NT_STATUS_IS_OK(result)) {
5334 d_printf("\nShutdown successfully aborted\n");
5335 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5337 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5343 * ABORT the Shut down of a remote RPC server
5345 * @param argc Standard main() style argc
5346 * @param argv Standard main() style argv. Initial components are already
5349 * @return A shell status integer (0 for success)
5352 static int rpc_shutdown_abort(int argc, const char **argv)
5354 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5355 rpc_shutdown_abort_internals,
5361 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5363 return run_rpc_command(NULL, PI_WINREG, 0,
5364 rpc_reg_shutdown_abort_internals,
5369 * Shut down a remote RPC Server via initshutdown pipe
5371 * All parameters are provided by the run_rpc_command function, except for
5372 * argc, argv which are passes 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 argc Standard main() style argv. Initial components are already
5381 * @return Normal NTSTATUS return.
5384 NTSTATUS rpc_init_shutdown_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;
5393 const char *msg = "This machine will be shutdown shortly";
5394 uint32 timeout = 20;
5395 struct initshutdown_String msg_string;
5396 struct initshutdown_String_sub s;
5402 timeout = opt_timeout;
5406 msg_string.name = &s;
5408 /* create an entry */
5409 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5410 &msg_string, timeout, opt_force, opt_reboot, NULL);
5412 if (NT_STATUS_IS_OK(result)) {
5413 d_printf("\nShutdown of remote machine succeeded\n");
5414 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5416 DEBUG(1,("Shutdown of remote machine failed!\n"));
5422 * Shut down a remote RPC Server via winreg pipe
5424 * All parameters are provided by the run_rpc_command function, except for
5425 * argc, argv which are passes through.
5427 * @param domain_sid The domain sid aquired from the remote server
5428 * @param cli A cli_state connected to the server.
5429 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5430 * @param argc Standard main() style argc
5431 * @param argc Standard main() style argv. Initial components are already
5434 * @return Normal NTSTATUS return.
5437 NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5438 const char *domain_name,
5439 struct cli_state *cli,
5440 struct rpc_pipe_client *pipe_hnd,
5441 TALLOC_CTX *mem_ctx,
5445 const char *msg = "This machine will be shutdown shortly";
5446 uint32 timeout = 20;
5447 struct initshutdown_String msg_string;
5448 struct initshutdown_String_sub s;
5456 msg_string.name = &s;
5459 timeout = opt_timeout;
5462 /* create an entry */
5463 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5464 &msg_string, timeout, opt_force, opt_reboot, &werr);
5466 if (NT_STATUS_IS_OK(result)) {
5467 d_printf("\nShutdown of remote machine succeeded\n");
5469 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5470 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5471 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5473 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5480 * Shut down a remote RPC server
5482 * @param argc Standard main() style argc
5483 * @param argc Standard main() style argv. Initial components are already
5486 * @return A shell status integer (0 for success)
5489 static int rpc_shutdown(int argc, const char **argv)
5491 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5492 rpc_init_shutdown_internals,
5496 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5497 rc = run_rpc_command(NULL, PI_WINREG, 0,
5498 rpc_reg_shutdown_internals, argc, argv);
5504 /***************************************************************************
5505 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5507 ***************************************************************************/
5510 * Add interdomain trust account to the RPC server.
5511 * All parameters (except for argc and argv) are passed by run_rpc_command
5514 * @param domain_sid The domain sid acquired from the server
5515 * @param cli A cli_state connected to the server.
5516 * @param mem_ctx Talloc context, destoyed on completion of the function.
5517 * @param argc Standard main() style argc
5518 * @param argc Standard main() style argv. Initial components are already
5521 * @return normal NTSTATUS return code
5524 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5525 const char *domain_name,
5526 struct cli_state *cli,
5527 struct rpc_pipe_client *pipe_hnd,
5528 TALLOC_CTX *mem_ctx,
5532 POLICY_HND connect_pol, domain_pol, user_pol;
5533 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5535 struct lsa_String lsa_acct_name;
5537 uint32 acct_flags=0;
5539 uint32_t access_granted = 0;
5542 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5543 return NT_STATUS_INVALID_PARAMETER;
5547 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5550 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5551 return NT_STATUS_NO_MEMORY;
5554 strupper_m(acct_name);
5556 init_lsa_String(&lsa_acct_name, acct_name);
5558 /* Get samr policy handle */
5559 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5560 pipe_hnd->cli->desthost,
5561 MAXIMUM_ALLOWED_ACCESS,
5563 if (!NT_STATUS_IS_OK(result)) {
5567 /* Get domain policy handle */
5568 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5570 MAXIMUM_ALLOWED_ACCESS,
5571 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5573 if (!NT_STATUS_IS_OK(result)) {
5577 /* Create trusting domain's account */
5578 acb_info = ACB_NORMAL;
5579 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5580 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5581 SAMR_USER_ACCESS_SET_PASSWORD |
5582 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5583 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5585 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5593 if (!NT_STATUS_IS_OK(result)) {
5598 SAM_USERINFO_CTR ctr;
5599 SAM_USER_INFO_23 p23;
5605 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5609 ZERO_STRUCT(notime);
5613 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5614 acb_info = ACB_DOMTRUST;
5616 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5617 ¬ime, ¬ime, ¬ime,
5618 nostr, nostr, nostr, nostr, nostr,
5619 nostr, nostr, nostr, nostr, nostr,
5620 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5621 0, 0, (char *)pwbuf);
5622 ctr.switch_value = 23;
5623 ctr.info.id23 = &p23;
5624 p23.passmustchange = 0;
5626 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5627 &cli->user_session_key, &ctr);
5629 if (!NT_STATUS_IS_OK(result)) {
5630 DEBUG(0,("Could not set trust account password: %s\n",
5631 nt_errstr(result)));
5637 SAFE_FREE(acct_name);
5642 * Create interdomain trust account for a remote domain.
5644 * @param argc standard argc
5645 * @param argv standard argv without initial components
5647 * @return Integer status (0 means success)
5650 static int rpc_trustdom_add(int argc, const char **argv)
5653 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5656 d_printf("Usage: net rpc trustdom add <domain>\n");
5663 * Remove interdomain trust account from the RPC server.
5664 * All parameters (except for argc and argv) are passed by run_rpc_command
5667 * @param domain_sid The domain sid acquired from the server
5668 * @param cli A cli_state connected to the server.
5669 * @param mem_ctx Talloc context, destoyed on completion of the function.
5670 * @param argc Standard main() style argc
5671 * @param argc Standard main() style argv. Initial components are already
5674 * @return normal NTSTATUS return code
5677 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5678 const char *domain_name,
5679 struct cli_state *cli,
5680 struct rpc_pipe_client *pipe_hnd,
5681 TALLOC_CTX *mem_ctx,
5685 POLICY_HND connect_pol, domain_pol, user_pol;
5686 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5689 DOM_SID trust_acct_sid;
5690 uint32 *user_rids, num_rids, *name_types;
5691 uint32 flags = 0x000003e8; /* Unknown */
5694 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5695 return NT_STATUS_INVALID_PARAMETER;
5699 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5701 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5703 if (acct_name == NULL)
5704 return NT_STATUS_NO_MEMORY;
5706 strupper_m(acct_name);
5708 if ((names = TALLOC_ARRAY(mem_ctx, const char *, 1)) == NULL) {
5709 return NT_STATUS_NO_MEMORY;
5711 names[0] = acct_name;
5714 /* Get samr policy handle */
5715 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5716 pipe_hnd->cli->desthost,
5717 MAXIMUM_ALLOWED_ACCESS,
5719 if (!NT_STATUS_IS_OK(result)) {
5723 /* Get domain policy handle */
5724 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5726 MAXIMUM_ALLOWED_ACCESS,
5727 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5729 if (!NT_STATUS_IS_OK(result)) {
5733 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5735 &user_rids, &name_types);
5737 if (!NT_STATUS_IS_OK(result)) {
5741 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5743 MAXIMUM_ALLOWED_ACCESS,
5747 if (!NT_STATUS_IS_OK(result)) {
5751 /* append the rid to the domain sid */
5752 sid_copy(&trust_acct_sid, domain_sid);
5753 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5757 /* remove the sid */
5759 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5762 if (!NT_STATUS_IS_OK(result)) {
5768 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5771 if (!NT_STATUS_IS_OK(result)) {
5775 if (!NT_STATUS_IS_OK(result)) {
5776 DEBUG(0,("Could not set trust account password: %s\n",
5777 nt_errstr(result)));
5786 * Delete interdomain trust account for a remote domain.
5788 * @param argc standard argc
5789 * @param argv standard argv without initial components
5791 * @return Integer status (0 means success)
5794 static int rpc_trustdom_del(int argc, const char **argv)
5797 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5800 d_printf("Usage: net rpc trustdom del <domain>\n");
5807 * Establish trust relationship to a trusting domain.
5808 * Interdomain account must already be created on remote PDC.
5810 * @param argc standard argc
5811 * @param argv standard argv without initial components
5813 * @return Integer status (0 means success)
5816 static int rpc_trustdom_establish(int argc, const char **argv)
5818 struct cli_state *cli = NULL;
5819 struct sockaddr_storage server_ss;
5820 struct rpc_pipe_client *pipe_hnd = NULL;
5821 POLICY_HND connect_hnd;
5822 TALLOC_CTX *mem_ctx;
5824 DOM_SID *domain_sid;
5827 const char* domain_name_pol;
5833 * Connect to \\server\ipc$ as 'our domain' account with password
5837 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5841 domain_name = smb_xstrdup(argv[0]);
5842 strupper_m(domain_name);
5844 /* account name used at first is our domain's name with '$' */
5845 asprintf(&acct_name, "%s$", lp_workgroup());
5846 strupper_m(acct_name);
5849 * opt_workgroup will be used by connection functions further,
5850 * hence it should be set to remote domain name instead of ours
5852 if (opt_workgroup) {
5853 opt_workgroup = smb_xstrdup(domain_name);
5856 opt_user_name = acct_name;
5858 /* find the domain controller */
5859 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5860 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5864 /* connect to ipc$ as username/password */
5865 nt_status = connect_to_ipc(&cli, &server_ss, pdc_name);
5866 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5868 /* Is it trusting domain account for sure ? */
5869 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5870 nt_errstr(nt_status)));
5874 /* store who we connected to */
5876 saf_store( domain_name, pdc_name );
5879 * Connect to \\server\ipc$ again (this time anonymously)
5882 nt_status = connect_to_ipc_anonymous(&cli, &server_ss, (char*)pdc_name);
5884 if (NT_STATUS_IS_ERR(nt_status)) {
5885 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5886 domain_name, nt_errstr(nt_status)));
5891 * Use NetServerEnum2 to make sure we're talking to a proper server
5894 if (!cli_get_pdc_name(cli, domain_name, &dc_name)) {
5895 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5896 for domain %s\n", domain_name));
5902 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5903 "domain %s", domain_name))) {
5904 DEBUG(0, ("talloc_init() failed\n"));
5910 * Call LsaOpenPolicy and LsaQueryInfo
5913 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5915 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5917 talloc_destroy(mem_ctx);
5921 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5923 if (NT_STATUS_IS_ERR(nt_status)) {
5924 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5925 nt_errstr(nt_status)));
5927 talloc_destroy(mem_ctx);
5931 /* Querying info level 5 */
5933 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5935 &domain_name_pol, &domain_sid);
5936 if (NT_STATUS_IS_ERR(nt_status)) {
5937 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5938 nt_errstr(nt_status)));
5940 talloc_destroy(mem_ctx);
5944 /* There should be actually query info level 3 (following nt serv behaviour),
5945 but I still don't know if it's _really_ necessary */
5948 * Store the password in secrets db
5951 if (!pdb_set_trusteddom_pw(domain_name, opt_password, domain_sid)) {
5952 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5954 talloc_destroy(mem_ctx);
5959 * Close the pipes and clean up
5962 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5963 if (NT_STATUS_IS_ERR(nt_status)) {
5964 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5965 nt_errstr(nt_status)));
5967 talloc_destroy(mem_ctx);
5973 talloc_destroy(mem_ctx);
5975 d_printf("Trust to domain %s established\n", domain_name);
5980 * Revoke trust relationship to the remote domain
5982 * @param argc standard argc
5983 * @param argv standard argv without initial components
5985 * @return Integer status (0 means success)
5988 static int rpc_trustdom_revoke(int argc, const char **argv)
5993 if (argc < 1) return -1;
5995 /* generate upper cased domain name */
5996 domain_name = smb_xstrdup(argv[0]);
5997 strupper_m(domain_name);
5999 /* delete password of the trust */
6000 if (!pdb_del_trusteddom_pw(domain_name)) {
6001 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6008 SAFE_FREE(domain_name);
6013 * Usage for 'net rpc trustdom' command
6015 * @param argc standard argc
6016 * @param argv standard argv without inital components
6018 * @return Integer status returned to shell
6021 static int rpc_trustdom_usage(int argc, const char **argv)
6023 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
6024 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
6025 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
6026 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
6027 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
6028 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
6033 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
6034 const char *domain_name,
6035 struct cli_state *cli,
6036 struct rpc_pipe_client *pipe_hnd,
6037 TALLOC_CTX *mem_ctx,
6042 sid_to_fstring(str_sid, domain_sid);
6043 d_printf("%s\n", str_sid);
6044 return NT_STATUS_OK;
6047 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6049 fstring ascii_sid, padding;
6050 int pad_len, col_len = 20;
6052 /* convert sid into ascii string */
6053 sid_to_fstring(ascii_sid, dom_sid);
6055 /* calculate padding space for d_printf to look nicer */
6056 pad_len = col_len - strlen(trusted_dom_name);
6057 padding[pad_len] = 0;
6058 do padding[--pad_len] = ' '; while (pad_len);
6060 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6063 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6064 TALLOC_CTX *mem_ctx,
6067 const char *trusted_dom_name)
6070 union lsa_TrustedDomainInfo info;
6071 char *cleartextpwd = NULL;
6074 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6077 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6079 if (NT_STATUS_IS_ERR(nt_status)) {
6080 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6081 nt_errstr(nt_status)));
6085 data = data_blob(NULL, info.password.password->length);
6088 info.password.password->data,
6089 info.password.password->length);
6090 data.length = info.password.password->length;
6092 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password,
6095 if (cleartextpwd == NULL) {
6096 DEBUG(0,("retrieved NULL password\n"));
6097 nt_status = NT_STATUS_UNSUCCESSFUL;
6101 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6102 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6103 nt_status = NT_STATUS_UNSUCCESSFUL;
6107 #ifdef DEBUG_PASSWORD
6108 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], "
6109 "password: [%s]\n", trusted_dom_name,
6110 sid_string_dbg(&dom_sid), cleartextpwd));
6114 SAFE_FREE(cleartextpwd);
6115 data_blob_free(&data);
6120 static int rpc_trustdom_vampire(int argc, const char **argv)
6122 /* common variables */
6123 TALLOC_CTX* mem_ctx;
6124 struct cli_state *cli = NULL;
6125 struct rpc_pipe_client *pipe_hnd = NULL;
6127 const char *domain_name = NULL;
6128 DOM_SID *queried_dom_sid;
6129 POLICY_HND connect_hnd;
6131 /* trusted domains listing variables */
6132 unsigned int num_domains, enum_ctx = 0;
6134 DOM_SID *domain_sids;
6135 char **trusted_dom_names;
6140 * Listing trusted domains (stored in secrets.tdb, if local)
6143 mem_ctx = talloc_init("trust relationships vampire");
6146 * set domain and pdc name to local samba server (default)
6147 * or to remote one given in command line
6150 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6151 domain_name = opt_workgroup;
6152 opt_target_workgroup = opt_workgroup;
6154 fstrcpy(pdc_name, global_myname());
6155 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6156 opt_target_workgroup = domain_name;
6159 /* open \PIPE\lsarpc and open policy handle */
6160 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6161 if (!NT_STATUS_IS_OK(nt_status)) {
6162 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6163 nt_errstr(nt_status)));
6164 talloc_destroy(mem_ctx);
6168 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6170 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6171 nt_errstr(nt_status) ));
6173 talloc_destroy(mem_ctx);
6177 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6179 if (NT_STATUS_IS_ERR(nt_status)) {
6180 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6181 nt_errstr(nt_status)));
6183 talloc_destroy(mem_ctx);
6187 /* query info level 5 to obtain sid of a domain being queried */
6188 nt_status = rpccli_lsa_query_info_policy(
6189 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
6190 &dummy, &queried_dom_sid);
6192 if (NT_STATUS_IS_ERR(nt_status)) {
6193 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6194 nt_errstr(nt_status)));
6196 talloc_destroy(mem_ctx);
6201 * Keep calling LsaEnumTrustdom over opened pipe until
6202 * the end of enumeration is reached
6205 d_printf("Vampire trusted domains:\n\n");
6208 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6210 &trusted_dom_names, &domain_sids);
6212 if (NT_STATUS_IS_ERR(nt_status)) {
6213 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6214 nt_errstr(nt_status)));
6216 talloc_destroy(mem_ctx);
6220 for (i = 0; i < num_domains; i++) {
6222 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6224 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6225 domain_sids[i], trusted_dom_names[i]);
6226 if (!NT_STATUS_IS_OK(nt_status)) {
6228 talloc_destroy(mem_ctx);
6234 * in case of no trusted domains say something rather
6235 * than just display blank line
6237 if (!num_domains) d_printf("none\n");
6239 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6241 /* close this connection before doing next one */
6242 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6243 if (NT_STATUS_IS_ERR(nt_status)) {
6244 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6245 nt_errstr(nt_status)));
6247 talloc_destroy(mem_ctx);
6251 /* close lsarpc pipe and connection to IPC$ */
6254 talloc_destroy(mem_ctx);
6258 static int rpc_trustdom_list(int argc, const char **argv)
6260 /* common variables */
6261 TALLOC_CTX* mem_ctx;
6262 struct cli_state *cli = NULL, *remote_cli = NULL;
6263 struct rpc_pipe_client *pipe_hnd = NULL;
6265 const char *domain_name = NULL;
6266 DOM_SID *queried_dom_sid;
6268 int ascii_dom_name_len;
6269 POLICY_HND connect_hnd;
6271 /* trusted domains listing variables */
6272 unsigned int num_domains, enum_ctx = 0;
6273 int i, pad_len, col_len = 20;
6274 DOM_SID *domain_sids;
6275 char **trusted_dom_names;
6279 /* trusting domains listing variables */
6280 POLICY_HND domain_hnd;
6281 char **trusting_dom_names;
6282 uint32 *trusting_dom_rids;
6285 * Listing trusted domains (stored in secrets.tdb, if local)
6288 mem_ctx = talloc_init("trust relationships listing");
6291 * set domain and pdc name to local samba server (default)
6292 * or to remote one given in command line
6295 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6296 domain_name = opt_workgroup;
6297 opt_target_workgroup = opt_workgroup;
6299 fstrcpy(pdc_name, global_myname());
6300 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6301 opt_target_workgroup = domain_name;
6304 /* open \PIPE\lsarpc and open policy handle */
6305 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6306 if (!NT_STATUS_IS_OK(nt_status)) {
6307 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6308 nt_errstr(nt_status)));
6309 talloc_destroy(mem_ctx);
6313 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6315 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6316 nt_errstr(nt_status) ));
6318 talloc_destroy(mem_ctx);
6322 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6324 if (NT_STATUS_IS_ERR(nt_status)) {
6325 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6326 nt_errstr(nt_status)));
6328 talloc_destroy(mem_ctx);
6332 /* query info level 5 to obtain sid of a domain being queried */
6333 nt_status = rpccli_lsa_query_info_policy(
6334 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
6335 &dummy, &queried_dom_sid);
6337 if (NT_STATUS_IS_ERR(nt_status)) {
6338 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6339 nt_errstr(nt_status)));
6341 talloc_destroy(mem_ctx);
6346 * Keep calling LsaEnumTrustdom over opened pipe until
6347 * the end of enumeration is reached
6350 d_printf("Trusted domains list:\n\n");
6353 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6355 &trusted_dom_names, &domain_sids);
6357 if (NT_STATUS_IS_ERR(nt_status)) {
6358 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6359 nt_errstr(nt_status)));
6361 talloc_destroy(mem_ctx);
6365 for (i = 0; i < num_domains; i++) {
6366 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6370 * in case of no trusted domains say something rather
6371 * than just display blank line
6373 if (!num_domains) d_printf("none\n");
6375 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6377 /* close this connection before doing next one */
6378 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6379 if (NT_STATUS_IS_ERR(nt_status)) {
6380 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6381 nt_errstr(nt_status)));
6383 talloc_destroy(mem_ctx);
6387 cli_rpc_pipe_close(pipe_hnd);
6390 * Listing trusting domains (stored in passdb backend, if local)
6393 d_printf("\nTrusting domains list:\n\n");
6396 * Open \PIPE\samr and get needed policy handles
6398 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6400 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6402 talloc_destroy(mem_ctx);
6407 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6408 pipe_hnd->cli->desthost,
6409 SA_RIGHT_SAM_OPEN_DOMAIN,
6411 if (!NT_STATUS_IS_OK(nt_status)) {
6412 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6413 nt_errstr(nt_status)));
6415 talloc_destroy(mem_ctx);
6419 /* SamrOpenDomain - we have to open domain policy handle in order to be
6420 able to enumerate accounts*/
6421 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6423 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6426 if (!NT_STATUS_IS_OK(nt_status)) {
6427 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6428 nt_errstr(nt_status)));
6430 talloc_destroy(mem_ctx);
6435 * perform actual enumeration
6438 enum_ctx = 0; /* reset enumeration context from last enumeration */
6441 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6442 &enum_ctx, ACB_DOMTRUST, 0xffff,
6443 &trusting_dom_names, &trusting_dom_rids,
6445 if (NT_STATUS_IS_ERR(nt_status)) {
6446 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6447 nt_errstr(nt_status)));
6449 talloc_destroy(mem_ctx);
6453 for (i = 0; i < num_domains; i++) {
6456 * get each single domain's sid (do we _really_ need this ?):
6457 * 1) connect to domain's pdc
6458 * 2) query the pdc for domain's sid
6461 /* get rid of '$' tail */
6462 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6463 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6464 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6466 /* calculate padding space for d_printf to look nicer */
6467 pad_len = col_len - strlen(trusting_dom_names[i]);
6468 padding[pad_len] = 0;
6469 do padding[--pad_len] = ' '; while (pad_len);
6471 /* set opt_* variables to remote domain */
6472 strupper_m(trusting_dom_names[i]);
6473 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6474 opt_target_workgroup = opt_workgroup;
6476 d_printf("%s%s", trusting_dom_names[i], padding);
6478 /* connect to remote domain controller */
6479 nt_status = net_make_ipc_connection(
6480 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6482 if (NT_STATUS_IS_OK(nt_status)) {
6483 /* query for domain's sid */
6484 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6485 d_fprintf(stderr, "couldn't get domain's sid\n");
6487 cli_shutdown(remote_cli);
6490 d_fprintf(stderr, "domain controller is not "
6492 nt_errstr(nt_status));
6496 if (!num_domains) d_printf("none\n");
6498 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6500 /* close opened samr and domain policy handles */
6501 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6502 if (!NT_STATUS_IS_OK(nt_status)) {
6503 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6506 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6507 if (!NT_STATUS_IS_OK(nt_status)) {
6508 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6511 /* close samr pipe and connection to IPC$ */
6514 talloc_destroy(mem_ctx);
6519 * Entrypoint for 'net rpc trustdom' code
6521 * @param argc standard argc
6522 * @param argv standard argv without initial components
6524 * @return Integer status (0 means success)
6527 static int rpc_trustdom(int argc, const char **argv)
6529 struct functable func[] = {
6530 {"add", rpc_trustdom_add},
6531 {"del", rpc_trustdom_del},
6532 {"establish", rpc_trustdom_establish},
6533 {"revoke", rpc_trustdom_revoke},
6534 {"help", rpc_trustdom_usage},
6535 {"list", rpc_trustdom_list},
6536 {"vampire", rpc_trustdom_vampire},
6541 rpc_trustdom_usage(argc, argv);
6545 return (net_run_function(argc, argv, func, rpc_user_usage));
6549 * Check if a server will take rpc commands
6550 * @param flags Type of server to connect to (PDC, DMB, localhost)
6551 * if the host is not explicitly specified
6552 * @return bool (true means rpc supported)
6554 bool net_rpc_check(unsigned flags)
6556 struct cli_state *cli;
6558 struct sockaddr_storage server_ss;
6559 char *server_name = NULL;
6562 /* flags (i.e. server type) may depend on command */
6563 if (!net_find_server(NULL, flags, &server_ss, &server_name))
6566 if ((cli = cli_initialise()) == NULL) {
6570 status = cli_connect(cli, server_name, &server_ss);
6571 if (!NT_STATUS_IS_OK(status))
6573 if (!attempt_netbios_session_request(&cli, global_myname(),
6574 server_name, &server_ss))
6576 if (!cli_negprot(cli))
6578 if (cli->protocol < PROTOCOL_NT1)
6587 /* dump sam database via samsync rpc calls */
6588 static int rpc_samdump(int argc, const char **argv) {
6589 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6593 /* syncronise sam database via samsync rpc calls */
6594 static int rpc_vampire(int argc, const char **argv) {
6595 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6600 * Migrate everything from a print-server
6602 * @param argc Standard main() style argc
6603 * @param argv Standard main() style argv. Initial components are already
6606 * @return A shell status integer (0 for success)
6608 * The order is important !
6609 * To successfully add drivers the print-queues have to exist !
6610 * Applying ACLs should be the last step, because you're easily locked out
6613 static int rpc_printer_migrate_all(int argc, const char **argv)
6618 printf("no server to migrate\n");
6622 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6626 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6630 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6634 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6638 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6643 * Migrate print-drivers from a print-server
6645 * @param argc Standard main() style argc
6646 * @param argv Standard main() style argv. Initial components are already
6649 * @return A shell status integer (0 for success)
6651 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6654 printf("no server to migrate\n");
6658 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6659 rpc_printer_migrate_drivers_internals,
6664 * Migrate print-forms from a print-server
6666 * @param argc Standard main() style argc
6667 * @param argv Standard main() style argv. Initial components are already
6670 * @return A shell status integer (0 for success)
6672 static int rpc_printer_migrate_forms(int argc, const char **argv)
6675 printf("no server to migrate\n");
6679 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6680 rpc_printer_migrate_forms_internals,
6685 * Migrate printers from a print-server
6687 * @param argc Standard main() style argc
6688 * @param argv Standard main() style argv. Initial components are already
6691 * @return A shell status integer (0 for success)
6693 static int rpc_printer_migrate_printers(int argc, const char **argv)
6696 printf("no server to migrate\n");
6700 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6701 rpc_printer_migrate_printers_internals,
6706 * Migrate printer-ACLs from a print-server
6708 * @param argc Standard main() style argc
6709 * @param argv Standard main() style argv. Initial components are already
6712 * @return A shell status integer (0 for success)
6714 static int rpc_printer_migrate_security(int argc, const char **argv)
6717 printf("no server to migrate\n");
6721 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6722 rpc_printer_migrate_security_internals,
6727 * Migrate printer-settings from a print-server
6729 * @param argc Standard main() style argc
6730 * @param argv Standard main() style argv. Initial components are already
6733 * @return A shell status integer (0 for success)
6735 static int rpc_printer_migrate_settings(int argc, const char **argv)
6738 printf("no server to migrate\n");
6742 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6743 rpc_printer_migrate_settings_internals,
6748 * 'net rpc printer' entrypoint.
6749 * @param argc Standard main() style argc
6750 * @param argv Standard main() style argv. Initial components are already
6754 int rpc_printer_migrate(int argc, const char **argv)
6757 /* ouch: when addriver and setdriver are called from within
6758 rpc_printer_migrate_drivers_internals, the printer-queue already
6761 struct functable func[] = {
6762 {"all", rpc_printer_migrate_all},
6763 {"drivers", rpc_printer_migrate_drivers},
6764 {"forms", rpc_printer_migrate_forms},
6765 {"help", rpc_printer_usage},
6766 {"printers", rpc_printer_migrate_printers},
6767 {"security", rpc_printer_migrate_security},
6768 {"settings", rpc_printer_migrate_settings},
6772 return net_run_function(argc, argv, func, rpc_printer_usage);
6777 * List printers on a remote RPC server
6779 * @param argc Standard main() style argc
6780 * @param argv Standard main() style argv. Initial components are already
6783 * @return A shell status integer (0 for success)
6785 static int rpc_printer_list(int argc, const char **argv)
6788 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6789 rpc_printer_list_internals,
6794 * List printer-drivers on a remote RPC server
6796 * @param argc Standard main() style argc
6797 * @param argv Standard main() style argv. Initial components are already
6800 * @return A shell status integer (0 for success)
6802 static int rpc_printer_driver_list(int argc, const char **argv)
6805 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6806 rpc_printer_driver_list_internals,
6811 * Publish printer in ADS via MSRPC
6813 * @param argc Standard main() style argc
6814 * @param argv Standard main() style argv. Initial components are already
6817 * @return A shell status integer (0 for success)
6819 static int rpc_printer_publish_publish(int argc, const char **argv)
6822 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6823 rpc_printer_publish_publish_internals,
6828 * Update printer in ADS via MSRPC
6830 * @param argc Standard main() style argc
6831 * @param argv Standard main() style argv. Initial components are already
6834 * @return A shell status integer (0 for success)
6836 static int rpc_printer_publish_update(int argc, const char **argv)
6839 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6840 rpc_printer_publish_update_internals,
6845 * UnPublish printer in ADS via MSRPC
6847 * @param argc Standard main() style argc
6848 * @param argv Standard main() style argv. Initial components are already
6851 * @return A shell status integer (0 for success)
6853 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6856 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6857 rpc_printer_publish_unpublish_internals,
6862 * List published printers via MSRPC
6864 * @param argc Standard main() style argc
6865 * @param argv Standard main() style argv. Initial components are already
6868 * @return A shell status integer (0 for success)
6870 static int rpc_printer_publish_list(int argc, const char **argv)
6873 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6874 rpc_printer_publish_list_internals,
6880 * Publish printer in ADS
6882 * @param argc Standard main() style argc
6883 * @param argv Standard main() style argv. Initial components are already
6886 * @return A shell status integer (0 for success)
6888 static int rpc_printer_publish(int argc, const char **argv)
6891 struct functable func[] = {
6892 {"publish", rpc_printer_publish_publish},
6893 {"update", rpc_printer_publish_update},
6894 {"unpublish", rpc_printer_publish_unpublish},
6895 {"list", rpc_printer_publish_list},
6896 {"help", rpc_printer_usage},
6901 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6902 rpc_printer_publish_list_internals,
6905 return net_run_function(argc, argv, func, rpc_printer_usage);
6911 * Display rpc printer help page.
6912 * @param argc Standard main() style argc
6913 * @param argv Standard main() style argv. Initial components are already
6916 int rpc_printer_usage(int argc, const char **argv)
6918 return net_help_printer(argc, argv);
6922 * 'net rpc printer' entrypoint.
6923 * @param argc Standard main() style argc
6924 * @param argv Standard main() style argv. Initial components are already
6927 int net_rpc_printer(int argc, const char **argv)
6929 struct functable func[] = {
6930 {"list", rpc_printer_list},
6931 {"migrate", rpc_printer_migrate},
6932 {"driver", rpc_printer_driver_list},
6933 {"publish", rpc_printer_publish},
6938 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6939 rpc_printer_list_internals,
6942 return net_run_function(argc, argv, func, rpc_printer_usage);
6945 /****************************************************************************/
6949 * Basic usage function for 'net rpc'
6950 * @param argc Standard main() style argc
6951 * @param argv Standard main() style argv. Initial components are already
6955 int net_rpc_usage(int argc, const char **argv)
6957 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6958 d_printf(" net rpc join \t\t\tto join a domain \n");
6959 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
6960 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6961 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6962 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6963 d_printf(" net rpc group \t\tto list groups\n");
6964 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6965 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6966 d_printf(" net rpc file \t\t\tto list open files\n");
6967 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6968 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6969 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6970 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
6971 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6972 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6973 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6974 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6975 d_printf(" net rpc registry\t\tto manage registry hives\n");
6976 d_printf(" net rpc service\t\tto start, stop and query services\n");
6977 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6978 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6980 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6981 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6982 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6983 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6984 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6990 * Help function for 'net rpc'. Calls command specific help if requested
6991 * or displays usage of net rpc
6992 * @param argc Standard main() style argc
6993 * @param argv Standard main() style argv. Initial components are already
6997 int net_rpc_help(int argc, const char **argv)
6999 struct functable func[] = {
7000 {"join", rpc_join_usage},
7001 {"user", rpc_user_usage},
7002 {"group", rpc_group_usage},
7003 {"share", rpc_share_usage},
7004 /*{"changetrustpw", rpc_changetrustpw_usage}, */
7005 {"trustdom", rpc_trustdom_usage},
7006 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
7007 /*{"shutdown", rpc_shutdown_usage}, */
7008 {"vampire", rpc_vampire_usage},
7013 net_rpc_usage(argc, argv);
7017 return (net_run_function(argc, argv, func, rpc_user_usage));
7021 * 'net rpc' entrypoint.
7022 * @param argc Standard main() style argc
7023 * @param argv Standard main() style argv. Initial components are already
7027 int net_rpc(int argc, const char **argv)
7029 struct functable func[] = {
7030 {"audit", net_rpc_audit},
7031 {"info", net_rpc_info},
7032 {"join", net_rpc_join},
7033 {"oldjoin", net_rpc_oldjoin},
7034 {"testjoin", net_rpc_testjoin},
7035 {"user", net_rpc_user},
7036 {"password", rpc_user_password},
7037 {"group", net_rpc_group},
7038 {"share", net_rpc_share},
7039 {"file", net_rpc_file},
7040 {"printer", net_rpc_printer},
7041 {"changetrustpw", net_rpc_changetrustpw},
7042 {"trustdom", rpc_trustdom},
7043 {"abortshutdown", rpc_shutdown_abort},
7044 {"shutdown", rpc_shutdown},
7045 {"samdump", rpc_samdump},
7046 {"vampire", rpc_vampire},
7047 {"getsid", net_rpc_getsid},
7048 {"rights", net_rpc_rights},
7049 {"service", net_rpc_service},
7050 {"registry", net_rpc_registry},
7051 {"shell", net_rpc_shell},
7052 {"help", net_rpc_help},
7055 return net_run_function(argc, argv, func, net_rpc_usage);