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 */
1167 struct samr_RidWithAttributeArray *rid_array = NULL;
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_GetGroupsForUser(pipe_hnd, mem_ctx,
1210 if (!NT_STATUS_IS_OK(result)) goto done;
1212 num_rids = rid_array->count;
1217 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1218 result = NT_STATUS_NO_MEMORY;
1222 for (i = 0; i < num_rids; i++)
1223 rids[i] = rid_array->rids[i].rid;
1225 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1227 &num_names, &names, &name_types);
1229 if (!NT_STATUS_IS_OK(result)) {
1233 /* Display results */
1235 for (i = 0; i < num_names; i++)
1236 printf("%s\n", names[i]);
1243 * List a user's groups from a remote RPC server
1245 * @param argc Standard main() style argc
1246 * @param argv Standard main() style argv. Initial components are already
1249 * @return A shell status integer (0 for success)
1252 static int rpc_user_info(int argc, const char **argv)
1254 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1259 * List users on a remote RPC server
1261 * All parameters are provided by the run_rpc_command function, except for
1262 * argc, argv which are passes through.
1264 * @param domain_sid The domain sid acquired from the remote server
1265 * @param cli A cli_state connected to the server.
1266 * @param mem_ctx Talloc context, destoyed on completion of the function.
1267 * @param argc Standard main() style argc
1268 * @param argv Standard main() style argv. Initial components are already
1271 * @return Normal NTSTATUS return.
1274 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1275 const char *domain_name,
1276 struct cli_state *cli,
1277 struct rpc_pipe_client *pipe_hnd,
1278 TALLOC_CTX *mem_ctx,
1282 POLICY_HND connect_pol, domain_pol;
1283 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1284 uint32 start_idx=0, num_entries, i, loop_count = 0;
1286 /* Get sam policy handle */
1288 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1289 pipe_hnd->cli->desthost,
1290 MAXIMUM_ALLOWED_ACCESS,
1292 if (!NT_STATUS_IS_OK(result)) {
1296 /* Get domain policy handle */
1298 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1300 MAXIMUM_ALLOWED_ACCESS,
1301 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1303 if (!NT_STATUS_IS_OK(result)) {
1307 /* Query domain users */
1308 if (opt_long_list_entries)
1309 d_printf("\nUser name Comment"\
1310 "\n-----------------------------\n");
1312 const char *user = NULL;
1313 const char *desc = NULL;
1314 uint32 max_entries, max_size;
1315 uint32_t total_size, returned_size;
1316 union samr_DispInfo info;
1318 get_query_dispinfo_params(
1319 loop_count, &max_entries, &max_size);
1321 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
1331 start_idx += info.info1.count;
1332 num_entries = info.info1.count;
1334 for (i = 0; i < num_entries; i++) {
1335 user = info.info1.entries[i].account_name.string;
1336 if (opt_long_list_entries)
1337 desc = info.info1.entries[i].description.string;
1338 if (opt_long_list_entries)
1339 printf("%-21.21s %s\n", user, desc);
1341 printf("%s\n", user);
1343 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1350 * 'net rpc user' entrypoint.
1351 * @param argc Standard main() style argc
1352 * @param argc Standard main() style argv. Initial components are already
1356 int net_rpc_user(int argc, const char **argv)
1358 struct functable func[] = {
1359 {"add", rpc_user_add},
1360 {"info", rpc_user_info},
1361 {"delete", rpc_user_delete},
1362 {"password", rpc_user_password},
1363 {"rename", rpc_user_rename},
1368 return run_rpc_command(NULL,PI_SAMR, 0,
1369 rpc_user_list_internals,
1373 return net_run_function(argc, argv, func, rpc_user_usage);
1376 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1377 struct rpc_sh_ctx *ctx,
1378 struct rpc_pipe_client *pipe_hnd,
1379 int argc, const char **argv)
1381 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1382 ctx->cli, pipe_hnd, mem_ctx,
1386 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1387 struct rpc_sh_ctx *ctx,
1388 struct rpc_pipe_client *pipe_hnd,
1389 int argc, const char **argv)
1391 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1392 ctx->cli, pipe_hnd, mem_ctx,
1396 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1397 struct rpc_sh_ctx *ctx,
1398 struct rpc_pipe_client *pipe_hnd,
1399 int argc, const char **argv,
1401 TALLOC_CTX *mem_ctx,
1402 struct rpc_sh_ctx *ctx,
1403 struct rpc_pipe_client *pipe_hnd,
1404 const POLICY_HND *user_hnd,
1405 int argc, const char **argv))
1408 POLICY_HND connect_pol, domain_pol, user_pol;
1409 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1412 enum lsa_SidType type;
1415 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1416 return NT_STATUS_INVALID_PARAMETER;
1419 ZERO_STRUCT(connect_pol);
1420 ZERO_STRUCT(domain_pol);
1421 ZERO_STRUCT(user_pol);
1423 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1424 NULL, NULL, &sid, &type);
1425 if (!NT_STATUS_IS_OK(result)) {
1426 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1431 if (type != SID_NAME_USER) {
1432 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1433 sid_type_lookup(type));
1434 result = NT_STATUS_NO_SUCH_USER;
1438 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1439 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1440 result = NT_STATUS_NO_SUCH_USER;
1444 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1445 pipe_hnd->cli->desthost,
1446 MAXIMUM_ALLOWED_ACCESS,
1448 if (!NT_STATUS_IS_OK(result)) {
1452 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1454 MAXIMUM_ALLOWED_ACCESS,
1457 if (!NT_STATUS_IS_OK(result)) {
1461 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1463 MAXIMUM_ALLOWED_ACCESS,
1466 if (!NT_STATUS_IS_OK(result)) {
1470 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1473 if (is_valid_policy_hnd(&user_pol)) {
1474 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1476 if (is_valid_policy_hnd(&domain_pol)) {
1477 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1479 if (is_valid_policy_hnd(&connect_pol)) {
1480 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1485 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1486 struct rpc_sh_ctx *ctx,
1487 struct rpc_pipe_client *pipe_hnd,
1488 const POLICY_HND *user_hnd,
1489 int argc, const char **argv)
1492 SAM_USERINFO_CTR *ctr;
1493 SAM_USER_INFO_21 *info;
1496 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1497 return NT_STATUS_INVALID_PARAMETER;
1500 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1502 if (!NT_STATUS_IS_OK(result)) {
1506 info = ctr->info.id21;
1508 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1514 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1515 struct rpc_sh_ctx *ctx,
1516 struct rpc_pipe_client *pipe_hnd,
1517 int argc, const char **argv)
1519 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1520 rpc_sh_user_show_internals);
1523 #define FETCHSTR(name, rec) \
1524 do { if (strequal(ctx->thiscmd, name)) { \
1525 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1528 #define SETSTR(name, rec, flag) \
1529 do { if (strequal(ctx->thiscmd, name)) { \
1530 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1531 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1532 usr->fields_present |= ACCT_##flag; } \
1535 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1536 struct rpc_sh_ctx *ctx,
1537 struct rpc_pipe_client *pipe_hnd,
1538 const POLICY_HND *user_hnd,
1539 int argc, const char **argv)
1542 SAM_USERINFO_CTR *ctr;
1543 SAM_USER_INFO_21 *usr;
1544 const char *username;
1545 const char *oldval = "";
1548 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1550 return NT_STATUS_INVALID_PARAMETER;
1553 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1555 if (!NT_STATUS_IS_OK(result)) {
1559 usr = ctr->info.id21;
1561 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1563 FETCHSTR("fullname", full_name);
1564 FETCHSTR("homedir", home_dir);
1565 FETCHSTR("homedrive", dir_drive);
1566 FETCHSTR("logonscript", logon_script);
1567 FETCHSTR("profilepath", profile_path);
1568 FETCHSTR("description", acct_desc);
1571 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1577 if (strcmp(argv[0], "NULL") == 0) {
1581 SETSTR("fullname", full_name, FULL_NAME);
1582 SETSTR("homedir", home_dir, HOME_DIR);
1583 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1584 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1585 SETSTR("profilepath", profile_path, PROFILE);
1586 SETSTR("description", acct_desc, DESCRIPTION);
1588 result = rpccli_samr_set_userinfo2(
1589 pipe_hnd, mem_ctx, user_hnd, 21,
1590 &pipe_hnd->cli->user_session_key, ctr);
1592 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1593 ctx->thiscmd, oldval, argv[0]);
1600 #define HANDLEFLG(name, rec) \
1601 do { if (strequal(ctx->thiscmd, name)) { \
1602 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1604 newflags = oldflags | ACB_##rec; \
1606 newflags = oldflags & ~ACB_##rec; \
1609 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1610 struct rpc_sh_ctx *ctx,
1611 struct rpc_pipe_client *pipe_hnd,
1612 int argc, const char **argv)
1614 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1615 rpc_sh_user_str_edit_internals);
1618 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1619 struct rpc_sh_ctx *ctx,
1620 struct rpc_pipe_client *pipe_hnd,
1621 const POLICY_HND *user_hnd,
1622 int argc, const char **argv)
1625 SAM_USERINFO_CTR *ctr;
1626 SAM_USER_INFO_21 *usr;
1627 const char *username;
1628 const char *oldval = "unknown";
1629 uint32 oldflags, newflags;
1633 ((argc == 1) && !strequal(argv[0], "yes") &&
1634 !strequal(argv[0], "no"))) {
1635 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1637 return NT_STATUS_INVALID_PARAMETER;
1640 newval = strequal(argv[0], "yes");
1642 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1644 if (!NT_STATUS_IS_OK(result)) {
1648 usr = ctr->info.id21;
1650 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1651 oldflags = usr->acb_info;
1652 newflags = usr->acb_info;
1654 HANDLEFLG("disabled", DISABLED);
1655 HANDLEFLG("pwnotreq", PWNOTREQ);
1656 HANDLEFLG("autolock", AUTOLOCK);
1657 HANDLEFLG("pwnoexp", PWNOEXP);
1660 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1666 usr->acb_info = newflags;
1667 usr->fields_present = ACCT_FLAGS;
1669 result = rpccli_samr_set_userinfo2(
1670 pipe_hnd, mem_ctx, user_hnd, 21,
1671 &pipe_hnd->cli->user_session_key, ctr);
1673 if (NT_STATUS_IS_OK(result)) {
1674 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1675 ctx->thiscmd, oldval, argv[0]);
1683 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1684 struct rpc_sh_ctx *ctx,
1685 struct rpc_pipe_client *pipe_hnd,
1686 int argc, const char **argv)
1688 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1689 rpc_sh_user_flag_edit_internals);
1692 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1693 struct rpc_sh_ctx *ctx)
1695 static struct rpc_sh_cmd cmds[] = {
1697 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1698 "Show/Set a user's full name" },
1700 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1701 "Show/Set a user's home directory" },
1703 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1704 "Show/Set a user's home drive" },
1706 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1707 "Show/Set a user's logon script" },
1709 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1710 "Show/Set a user's profile path" },
1712 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1713 "Show/Set a user's description" },
1715 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1716 "Show/Set whether a user is disabled" },
1718 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1719 "Show/Set whether a user locked out" },
1721 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1722 "Show/Set whether a user does not need a password" },
1724 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1725 "Show/Set whether a user's password does not expire" },
1727 { NULL, NULL, 0, NULL, NULL }
1733 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1734 struct rpc_sh_ctx *ctx)
1736 static struct rpc_sh_cmd cmds[] = {
1738 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1739 "List available users" },
1741 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1742 "List the domain groups a user is member of" },
1744 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1745 "Show info about a user" },
1747 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1748 "Show/Modify a user's fields" },
1750 { NULL, NULL, 0, NULL, NULL }
1756 /****************************************************************************/
1759 * Basic usage function for 'net rpc group'
1760 * @param argc Standard main() style argc.
1761 * @param argv Standard main() style argv. Initial components are already
1765 static int rpc_group_usage(int argc, const char **argv)
1767 return net_help_group(argc, argv);
1771 * Delete group on a remote RPC server
1773 * All parameters are provided by the run_rpc_command function, except for
1774 * argc, argv which are passes through.
1776 * @param domain_sid The domain sid acquired from the remote server
1777 * @param cli A cli_state connected to the server.
1778 * @param mem_ctx Talloc context, destoyed on completion of the function.
1779 * @param argc Standard main() style argc
1780 * @param argv Standard main() style argv. Initial components are already
1783 * @return Normal NTSTATUS return.
1786 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1787 const char *domain_name,
1788 struct cli_state *cli,
1789 struct rpc_pipe_client *pipe_hnd,
1790 TALLOC_CTX *mem_ctx,
1794 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1795 bool group_is_primary = False;
1796 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1798 uint32 *group_rids, num_rids, *name_types, group_rid;
1799 struct samr_RidTypeArray *rids = NULL;
1800 uint32 flags = 0x000003e8; /* Unknown */
1803 /* DOM_GID *user_gids; */
1804 SAM_USERINFO_CTR *user_ctr;
1808 d_printf("specify group\n");
1809 rpc_group_usage(argc,argv);
1810 return NT_STATUS_OK; /* ok? */
1813 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1814 pipe_hnd->cli->desthost,
1815 MAXIMUM_ALLOWED_ACCESS,
1818 if (!NT_STATUS_IS_OK(result)) {
1819 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1823 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1825 MAXIMUM_ALLOWED_ACCESS,
1826 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1829 if (!NT_STATUS_IS_OK(result)) {
1830 d_fprintf(stderr, "Request open_domain failed\n");
1834 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1836 &num_rids, &group_rids,
1839 if (!NT_STATUS_IS_OK(result)) {
1840 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1844 switch (name_types[0])
1846 case SID_NAME_DOM_GRP:
1847 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1849 MAXIMUM_ALLOWED_ACCESS,
1852 if (!NT_STATUS_IS_OK(result)) {
1853 d_fprintf(stderr, "Request open_group failed");
1857 group_rid = group_rids[0];
1859 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1863 if (!NT_STATUS_IS_OK(result)) {
1864 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1869 d_printf("Domain Group %s (rid: %d) has %d members\n",
1870 argv[0],group_rid, rids->count);
1873 /* Check if group is anyone's primary group */
1874 for (i = 0; i < rids->count; i++)
1876 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1878 MAXIMUM_ALLOWED_ACCESS,
1882 if (!NT_STATUS_IS_OK(result)) {
1883 d_fprintf(stderr, "Unable to open group member %d\n",
1888 ZERO_STRUCT(user_ctr);
1890 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1893 if (!NT_STATUS_IS_OK(result)) {
1894 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1899 if (user_ctr->info.id21->group_rid == group_rid) {
1900 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1903 d_printf("Group is primary group of %s\n",temp);
1904 group_is_primary = True;
1907 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1910 if (group_is_primary) {
1911 d_fprintf(stderr, "Unable to delete group because some "
1912 "of it's members have it as primary group\n");
1913 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1917 /* remove all group members */
1918 for (i = 0; i < rids->count; i++)
1921 d_printf("Remove group member %d...",
1923 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1927 if (NT_STATUS_IS_OK(result)) {
1932 d_printf("failed\n");
1937 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1941 /* removing a local group is easier... */
1942 case SID_NAME_ALIAS:
1943 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1945 MAXIMUM_ALLOWED_ACCESS,
1949 if (!NT_STATUS_IS_OK(result)) {
1950 d_fprintf(stderr, "Request open_alias failed\n");
1954 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1958 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1959 argv[0],sid_type_lookup(name_types[0]));
1960 result = NT_STATUS_UNSUCCESSFUL;
1965 if (NT_STATUS_IS_OK(result)) {
1967 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1969 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1970 get_friendly_nt_error_msg(result));
1978 static int rpc_group_delete(int argc, const char **argv)
1980 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1984 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1985 const char *domain_name,
1986 struct cli_state *cli,
1987 struct rpc_pipe_client *pipe_hnd,
1988 TALLOC_CTX *mem_ctx,
1992 POLICY_HND connect_pol, domain_pol, group_pol;
1993 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1994 union samr_GroupInfo group_info;
1995 struct lsa_String grp_name;
1999 d_printf("Group name must be specified\n");
2000 rpc_group_usage(argc, argv);
2001 return NT_STATUS_OK;
2004 init_lsa_String(&grp_name, argv[0]);
2006 /* Get sam policy handle */
2008 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2009 pipe_hnd->cli->desthost,
2010 MAXIMUM_ALLOWED_ACCESS,
2012 if (!NT_STATUS_IS_OK(result)) goto done;
2014 /* Get domain policy handle */
2016 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2018 MAXIMUM_ALLOWED_ACCESS,
2019 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2021 if (!NT_STATUS_IS_OK(result)) goto done;
2023 /* Create the group */
2025 result = rpccli_samr_CreateDomainGroup(pipe_hnd, mem_ctx,
2028 MAXIMUM_ALLOWED_ACCESS,
2031 if (!NT_STATUS_IS_OK(result)) goto done;
2033 if (strlen(opt_comment) == 0) goto done;
2035 /* We've got a comment to set */
2037 init_lsa_String(&group_info.description, opt_comment);
2039 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
2043 if (!NT_STATUS_IS_OK(result)) goto done;
2046 if (NT_STATUS_IS_OK(result))
2047 DEBUG(5, ("add group succeeded\n"));
2049 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
2054 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
2055 const char *domain_name,
2056 struct cli_state *cli,
2057 struct rpc_pipe_client *pipe_hnd,
2058 TALLOC_CTX *mem_ctx,
2062 POLICY_HND connect_pol, domain_pol, alias_pol;
2063 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2064 union samr_AliasInfo alias_info;
2065 struct lsa_String alias_name;
2069 d_printf("Alias name must be specified\n");
2070 rpc_group_usage(argc, argv);
2071 return NT_STATUS_OK;
2074 init_lsa_String(&alias_name, argv[0]);
2076 /* Get sam policy handle */
2078 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2079 pipe_hnd->cli->desthost,
2080 MAXIMUM_ALLOWED_ACCESS,
2082 if (!NT_STATUS_IS_OK(result)) goto done;
2084 /* Get domain policy handle */
2086 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2088 MAXIMUM_ALLOWED_ACCESS,
2089 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2091 if (!NT_STATUS_IS_OK(result)) goto done;
2093 /* Create the group */
2095 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
2098 MAXIMUM_ALLOWED_ACCESS,
2101 if (!NT_STATUS_IS_OK(result)) goto done;
2103 if (strlen(opt_comment) == 0) goto done;
2105 /* We've got a comment to set */
2107 init_lsa_String(&alias_info.description, opt_comment);
2109 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
2114 if (!NT_STATUS_IS_OK(result)) goto done;
2117 if (NT_STATUS_IS_OK(result))
2118 DEBUG(5, ("add alias succeeded\n"));
2120 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
2125 static int rpc_group_add(int argc, const char **argv)
2128 return run_rpc_command(NULL, PI_SAMR, 0,
2129 rpc_alias_add_internals,
2132 return run_rpc_command(NULL, PI_SAMR, 0,
2133 rpc_group_add_internals,
2137 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2138 TALLOC_CTX *mem_ctx,
2141 enum lsa_SidType *type)
2143 DOM_SID *sids = NULL;
2144 enum lsa_SidType *types = NULL;
2145 struct rpc_pipe_client *pipe_hnd;
2147 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2149 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2154 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2155 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2157 if (!NT_STATUS_IS_OK(result)) {
2161 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2162 &name, NULL, 1, &sids, &types);
2164 if (NT_STATUS_IS_OK(result)) {
2165 sid_copy(sid, &sids[0]);
2169 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2173 cli_rpc_pipe_close(pipe_hnd);
2176 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2178 /* Try as S-1-5-whatever */
2182 if (string_to_sid(&tmp_sid, name)) {
2183 sid_copy(sid, &tmp_sid);
2184 *type = SID_NAME_UNKNOWN;
2185 result = NT_STATUS_OK;
2192 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2193 TALLOC_CTX *mem_ctx,
2194 const DOM_SID *group_sid,
2197 POLICY_HND connect_pol, domain_pol;
2200 POLICY_HND group_pol;
2203 uint32 *rids = NULL;
2204 uint32 *rid_types = NULL;
2208 sid_copy(&sid, group_sid);
2210 if (!sid_split_rid(&sid, &group_rid)) {
2211 return NT_STATUS_UNSUCCESSFUL;
2214 /* Get sam policy handle */
2215 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2216 pipe_hnd->cli->desthost,
2217 MAXIMUM_ALLOWED_ACCESS,
2219 if (!NT_STATUS_IS_OK(result)) {
2223 /* Get domain policy handle */
2224 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2226 MAXIMUM_ALLOWED_ACCESS,
2229 if (!NT_STATUS_IS_OK(result)) {
2233 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2235 &num_rids, &rids, &rid_types);
2237 if (!NT_STATUS_IS_OK(result)) {
2238 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2242 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2244 MAXIMUM_ALLOWED_ACCESS,
2248 if (!NT_STATUS_IS_OK(result)) {
2252 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2255 0x0005); /* unknown flags */
2258 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2262 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2263 TALLOC_CTX *mem_ctx,
2264 const DOM_SID *alias_sid,
2267 POLICY_HND connect_pol, domain_pol;
2270 POLICY_HND alias_pol;
2273 enum lsa_SidType member_type;
2277 sid_copy(&sid, alias_sid);
2279 if (!sid_split_rid(&sid, &alias_rid)) {
2280 return NT_STATUS_UNSUCCESSFUL;
2283 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2284 &member_sid, &member_type);
2286 if (!NT_STATUS_IS_OK(result)) {
2287 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2291 /* Get sam policy handle */
2292 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2293 pipe_hnd->cli->desthost,
2294 MAXIMUM_ALLOWED_ACCESS,
2296 if (!NT_STATUS_IS_OK(result)) {
2300 /* Get domain policy handle */
2301 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2303 MAXIMUM_ALLOWED_ACCESS,
2306 if (!NT_STATUS_IS_OK(result)) {
2310 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2312 MAXIMUM_ALLOWED_ACCESS,
2316 if (!NT_STATUS_IS_OK(result)) {
2320 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2324 if (!NT_STATUS_IS_OK(result)) {
2329 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2333 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2334 const char *domain_name,
2335 struct cli_state *cli,
2336 struct rpc_pipe_client *pipe_hnd,
2337 TALLOC_CTX *mem_ctx,
2342 enum lsa_SidType group_type;
2345 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2346 return NT_STATUS_UNSUCCESSFUL;
2349 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2350 &group_sid, &group_type))) {
2351 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2352 return NT_STATUS_UNSUCCESSFUL;
2355 if (group_type == SID_NAME_DOM_GRP) {
2356 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2357 &group_sid, argv[1]);
2359 if (!NT_STATUS_IS_OK(result)) {
2360 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2361 argv[1], argv[0], nt_errstr(result));
2366 if (group_type == SID_NAME_ALIAS) {
2367 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2368 &group_sid, argv[1]);
2370 if (!NT_STATUS_IS_OK(result)) {
2371 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2372 argv[1], argv[0], nt_errstr(result));
2377 d_fprintf(stderr, "Can only add members to global or local groups "
2378 "which %s is not\n", argv[0]);
2380 return NT_STATUS_UNSUCCESSFUL;
2383 static int rpc_group_addmem(int argc, const char **argv)
2385 return run_rpc_command(NULL, PI_SAMR, 0,
2386 rpc_group_addmem_internals,
2390 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2391 TALLOC_CTX *mem_ctx,
2392 const DOM_SID *group_sid,
2395 POLICY_HND connect_pol, domain_pol;
2398 POLICY_HND group_pol;
2401 uint32 *rids = NULL;
2402 uint32 *rid_types = NULL;
2406 sid_copy(&sid, group_sid);
2408 if (!sid_split_rid(&sid, &group_rid))
2409 return NT_STATUS_UNSUCCESSFUL;
2411 /* Get sam policy handle */
2412 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2413 pipe_hnd->cli->desthost,
2414 MAXIMUM_ALLOWED_ACCESS,
2416 if (!NT_STATUS_IS_OK(result))
2419 /* Get domain policy handle */
2420 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2422 MAXIMUM_ALLOWED_ACCESS,
2425 if (!NT_STATUS_IS_OK(result))
2428 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2430 &num_rids, &rids, &rid_types);
2432 if (!NT_STATUS_IS_OK(result)) {
2433 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2437 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2439 MAXIMUM_ALLOWED_ACCESS,
2443 if (!NT_STATUS_IS_OK(result))
2446 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2451 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2455 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2456 TALLOC_CTX *mem_ctx,
2457 const DOM_SID *alias_sid,
2460 POLICY_HND connect_pol, domain_pol;
2463 POLICY_HND alias_pol;
2466 enum lsa_SidType member_type;
2470 sid_copy(&sid, alias_sid);
2472 if (!sid_split_rid(&sid, &alias_rid))
2473 return NT_STATUS_UNSUCCESSFUL;
2475 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2476 &member_sid, &member_type);
2478 if (!NT_STATUS_IS_OK(result)) {
2479 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2483 /* Get sam policy handle */
2484 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2485 pipe_hnd->cli->desthost,
2486 MAXIMUM_ALLOWED_ACCESS,
2488 if (!NT_STATUS_IS_OK(result)) {
2492 /* Get domain policy handle */
2493 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2495 MAXIMUM_ALLOWED_ACCESS,
2498 if (!NT_STATUS_IS_OK(result)) {
2502 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2504 MAXIMUM_ALLOWED_ACCESS,
2508 if (!NT_STATUS_IS_OK(result))
2511 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2515 if (!NT_STATUS_IS_OK(result))
2519 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2523 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2524 const char *domain_name,
2525 struct cli_state *cli,
2526 struct rpc_pipe_client *pipe_hnd,
2527 TALLOC_CTX *mem_ctx,
2532 enum lsa_SidType group_type;
2535 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2536 return NT_STATUS_UNSUCCESSFUL;
2539 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2540 &group_sid, &group_type))) {
2541 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2542 return NT_STATUS_UNSUCCESSFUL;
2545 if (group_type == SID_NAME_DOM_GRP) {
2546 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2547 &group_sid, argv[1]);
2549 if (!NT_STATUS_IS_OK(result)) {
2550 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2551 argv[1], argv[0], nt_errstr(result));
2556 if (group_type == SID_NAME_ALIAS) {
2557 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2558 &group_sid, argv[1]);
2560 if (!NT_STATUS_IS_OK(result)) {
2561 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2562 argv[1], argv[0], nt_errstr(result));
2567 d_fprintf(stderr, "Can only delete members from global or local groups "
2568 "which %s is not\n", argv[0]);
2570 return NT_STATUS_UNSUCCESSFUL;
2573 static int rpc_group_delmem(int argc, const char **argv)
2575 return run_rpc_command(NULL, PI_SAMR, 0,
2576 rpc_group_delmem_internals,
2581 * List groups on a remote RPC server
2583 * All parameters are provided by the run_rpc_command function, except for
2584 * argc, argv which are passes through.
2586 * @param domain_sid The domain sid acquired from the remote server
2587 * @param cli A cli_state connected to the server.
2588 * @param mem_ctx Talloc context, destoyed on completion of the function.
2589 * @param argc Standard main() style argc
2590 * @param argv Standard main() style argv. Initial components are already
2593 * @return Normal NTSTATUS return.
2596 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2597 const char *domain_name,
2598 struct cli_state *cli,
2599 struct rpc_pipe_client *pipe_hnd,
2600 TALLOC_CTX *mem_ctx,
2604 POLICY_HND connect_pol, domain_pol;
2605 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2606 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2607 struct acct_info *groups;
2608 bool global = False;
2610 bool builtin = False;
2618 for (i=0; i<argc; i++) {
2619 if (strequal(argv[i], "global"))
2622 if (strequal(argv[i], "local"))
2625 if (strequal(argv[i], "builtin"))
2629 /* Get sam policy handle */
2631 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2632 pipe_hnd->cli->desthost,
2633 MAXIMUM_ALLOWED_ACCESS,
2635 if (!NT_STATUS_IS_OK(result)) {
2639 /* Get domain policy handle */
2641 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2643 MAXIMUM_ALLOWED_ACCESS,
2644 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2646 if (!NT_STATUS_IS_OK(result)) {
2650 /* Query domain groups */
2651 if (opt_long_list_entries)
2652 d_printf("\nGroup name Comment"\
2653 "\n-----------------------------\n");
2655 uint32_t max_size, total_size, returned_size;
2656 union samr_DispInfo info;
2660 get_query_dispinfo_params(
2661 loop_count, &max_entries, &max_size);
2663 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2672 num_entries = info.info3.count;
2673 start_idx += info.info3.count;
2675 if (!NT_STATUS_IS_OK(result) &&
2676 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2679 for (i = 0; i < num_entries; i++) {
2681 const char *group = NULL;
2682 const char *desc = NULL;
2684 group = info.info3.entries[i].account_name.string;
2685 desc = info.info3.entries[i].description.string;
2687 if (opt_long_list_entries)
2688 printf("%-21.21s %-50.50s\n",
2691 printf("%s\n", group);
2693 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2694 /* query domain aliases */
2699 /* The max_size field in cli_samr_enum_als_groups is more like
2700 * an account_control field with indiviual bits what to
2701 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2702 * everything. I'm too lazy (sorry) to get this through to
2703 * rpc_parse/ etc. Volker */
2705 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2707 &groups, &num_entries);
2709 if (!NT_STATUS_IS_OK(result) &&
2710 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2713 for (i = 0; i < num_entries; i++) {
2715 const char *description = NULL;
2717 if (opt_long_list_entries) {
2719 POLICY_HND alias_pol;
2720 union samr_AliasInfo *info = NULL;
2722 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2727 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2731 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2733 description = info->description.string;
2737 if (description != NULL) {
2738 printf("%-21.21s %-50.50s\n",
2739 groups[i].acct_name,
2742 printf("%s\n", groups[i].acct_name);
2745 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2746 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2747 /* Get builtin policy handle */
2749 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2751 MAXIMUM_ALLOWED_ACCESS,
2752 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2754 if (!NT_STATUS_IS_OK(result)) {
2757 /* query builtin aliases */
2760 if (!builtin) break;
2762 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2763 &start_idx, max_entries,
2764 &groups, &num_entries);
2766 if (!NT_STATUS_IS_OK(result) &&
2767 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2770 for (i = 0; i < num_entries; i++) {
2772 const char *description = NULL;
2774 if (opt_long_list_entries) {
2776 POLICY_HND alias_pol;
2777 union samr_AliasInfo *info = NULL;
2779 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2784 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2788 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2790 description = info->description.string;
2794 if (description != NULL) {
2795 printf("%-21.21s %-50.50s\n",
2796 groups[i].acct_name,
2799 printf("%s\n", groups[i].acct_name);
2802 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2808 static int rpc_group_list(int argc, const char **argv)
2810 return run_rpc_command(NULL, PI_SAMR, 0,
2811 rpc_group_list_internals,
2815 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2816 TALLOC_CTX *mem_ctx,
2817 const char *domain_name,
2818 const DOM_SID *domain_sid,
2819 POLICY_HND *domain_pol,
2823 POLICY_HND group_pol;
2824 uint32 num_members, *group_rids;
2829 struct samr_RidTypeArray *rids = NULL;
2832 sid_to_fstring(sid_str, domain_sid);
2834 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2836 MAXIMUM_ALLOWED_ACCESS,
2840 if (!NT_STATUS_IS_OK(result))
2843 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2847 if (!NT_STATUS_IS_OK(result))
2850 num_members = rids->count;
2851 group_rids = rids->rids;
2853 while (num_members > 0) {
2854 int this_time = 512;
2856 if (num_members < this_time)
2857 this_time = num_members;
2859 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2860 this_time, group_rids,
2861 &num_names, &names, &name_types);
2863 if (!NT_STATUS_IS_OK(result))
2866 /* We only have users as members, but make the output
2867 the same as the output of alias members */
2869 for (i = 0; i < this_time; i++) {
2871 if (opt_long_list_entries) {
2872 printf("%s-%d %s\\%s %d\n", sid_str,
2873 group_rids[i], domain_name, names[i],
2876 printf("%s\\%s\n", domain_name, names[i]);
2880 num_members -= this_time;
2884 return NT_STATUS_OK;
2887 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2888 TALLOC_CTX *mem_ctx,
2889 POLICY_HND *domain_pol,
2893 struct rpc_pipe_client *lsa_pipe;
2894 POLICY_HND alias_pol, lsa_pol;
2896 DOM_SID *alias_sids;
2899 enum lsa_SidType *types;
2901 struct lsa_SidArray sid_array;
2903 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2905 MAXIMUM_ALLOWED_ACCESS,
2909 if (!NT_STATUS_IS_OK(result))
2912 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2916 if (!NT_STATUS_IS_OK(result)) {
2917 d_fprintf(stderr, "Couldn't list alias members\n");
2921 num_members = sid_array.num_sids;
2923 if (num_members == 0) {
2924 return NT_STATUS_OK;
2927 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2929 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2930 nt_errstr(result) );
2934 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2935 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2937 if (!NT_STATUS_IS_OK(result)) {
2938 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2939 cli_rpc_pipe_close(lsa_pipe);
2943 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2945 d_fprintf(stderr, "Out of memory\n");
2946 cli_rpc_pipe_close(lsa_pipe);
2947 return NT_STATUS_NO_MEMORY;
2950 for (i=0; i<num_members; i++) {
2951 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2954 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2956 &domains, &names, &types);
2958 if (!NT_STATUS_IS_OK(result) &&
2959 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2960 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2961 cli_rpc_pipe_close(lsa_pipe);
2965 for (i = 0; i < num_members; i++) {
2967 sid_to_fstring(sid_str, &alias_sids[i]);
2969 if (opt_long_list_entries) {
2970 printf("%s %s\\%s %d\n", sid_str,
2971 domains[i] ? domains[i] : "*unknown*",
2972 names[i] ? names[i] : "*unknown*", types[i]);
2975 printf("%s\\%s\n", domains[i], names[i]);
2977 printf("%s\n", sid_str);
2981 cli_rpc_pipe_close(lsa_pipe);
2982 return NT_STATUS_OK;
2985 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2986 const char *domain_name,
2987 struct cli_state *cli,
2988 struct rpc_pipe_client *pipe_hnd,
2989 TALLOC_CTX *mem_ctx,
2994 POLICY_HND connect_pol, domain_pol;
2995 uint32 num_rids, *rids, *rid_types;
2997 /* Get sam policy handle */
2999 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3000 pipe_hnd->cli->desthost,
3001 MAXIMUM_ALLOWED_ACCESS,
3004 if (!NT_STATUS_IS_OK(result))
3007 /* Get domain policy handle */
3009 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3011 MAXIMUM_ALLOWED_ACCESS,
3012 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3015 if (!NT_STATUS_IS_OK(result))
3018 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
3019 1, argv, &num_rids, &rids, &rid_types);
3021 if (!NT_STATUS_IS_OK(result)) {
3023 /* Ok, did not find it in the global sam, try with builtin */
3025 DOM_SID sid_Builtin;
3027 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3029 string_to_sid(&sid_Builtin, "S-1-5-32");
3031 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3033 MAXIMUM_ALLOWED_ACCESS,
3037 if (!NT_STATUS_IS_OK(result)) {
3038 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3042 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
3046 if (!NT_STATUS_IS_OK(result)) {
3047 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3052 if (num_rids != 1) {
3053 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3057 if (rid_types[0] == SID_NAME_DOM_GRP) {
3058 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
3059 domain_sid, &domain_pol,
3063 if (rid_types[0] == SID_NAME_ALIAS) {
3064 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
3068 return NT_STATUS_NO_SUCH_GROUP;
3071 static int rpc_group_members(int argc, const char **argv)
3074 return rpc_group_usage(argc, argv);
3077 return run_rpc_command(NULL, PI_SAMR, 0,
3078 rpc_group_members_internals,
3082 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
3083 const char *domain_name,
3084 struct cli_state *cli,
3085 struct rpc_pipe_client *pipe_hnd,
3086 TALLOC_CTX *mem_ctx,
3091 POLICY_HND connect_pol, domain_pol, group_pol;
3092 uint32 num_rids, *rids, *rid_types;
3093 union samr_GroupInfo group_info;
3096 d_printf("Usage: 'net rpc group rename group newname'\n");
3097 return NT_STATUS_UNSUCCESSFUL;
3100 /* Get sam policy handle */
3102 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3103 pipe_hnd->cli->desthost,
3104 MAXIMUM_ALLOWED_ACCESS,
3107 if (!NT_STATUS_IS_OK(result))
3110 /* Get domain policy handle */
3112 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3114 MAXIMUM_ALLOWED_ACCESS,
3115 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3118 if (!NT_STATUS_IS_OK(result))
3121 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
3122 1, argv, &num_rids, &rids, &rid_types);
3124 if (num_rids != 1) {
3125 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
3129 if (rid_types[0] != SID_NAME_DOM_GRP) {
3130 d_fprintf(stderr, "Can only rename domain groups\n");
3131 return NT_STATUS_UNSUCCESSFUL;
3134 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
3136 MAXIMUM_ALLOWED_ACCESS,
3140 if (!NT_STATUS_IS_OK(result))
3143 init_lsa_String(&group_info.name, argv[1]);
3145 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
3150 if (!NT_STATUS_IS_OK(result))
3153 return NT_STATUS_NO_SUCH_GROUP;
3156 static int rpc_group_rename(int argc, const char **argv)
3159 return rpc_group_usage(argc, argv);
3162 return run_rpc_command(NULL, PI_SAMR, 0,
3163 rpc_group_rename_internals,
3168 * 'net rpc group' entrypoint.
3169 * @param argc Standard main() style argc
3170 * @param argc Standard main() style argv. Initial components are already
3174 int net_rpc_group(int argc, const char **argv)
3176 struct functable func[] = {
3177 {"add", rpc_group_add},
3178 {"delete", rpc_group_delete},
3179 {"addmem", rpc_group_addmem},
3180 {"delmem", rpc_group_delmem},
3181 {"list", rpc_group_list},
3182 {"members", rpc_group_members},
3183 {"rename", rpc_group_rename},
3188 return run_rpc_command(NULL, PI_SAMR, 0,
3189 rpc_group_list_internals,
3193 return net_run_function(argc, argv, func, rpc_group_usage);
3196 /****************************************************************************/
3198 static int rpc_share_usage(int argc, const char **argv)
3200 return net_help_share(argc, argv);
3204 * Add a share on a remote RPC server
3206 * All parameters are provided by the run_rpc_command function, except for
3207 * argc, argv which are passes through.
3209 * @param domain_sid The domain sid acquired from the remote server
3210 * @param cli A cli_state connected to the server.
3211 * @param mem_ctx Talloc context, destoyed on completion of the function.
3212 * @param argc Standard main() style argc
3213 * @param argv Standard main() style argv. Initial components are already
3216 * @return Normal NTSTATUS return.
3218 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3219 const char *domain_name,
3220 struct cli_state *cli,
3221 struct rpc_pipe_client *pipe_hnd,
3222 TALLOC_CTX *mem_ctx,int argc,
3228 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3229 uint32 num_users=0, perms=0;
3230 char *password=NULL; /* don't allow a share password */
3233 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3234 return NT_STATUS_NO_MEMORY;
3237 path = strchr(sharename, '=');
3239 return NT_STATUS_UNSUCCESSFUL;
3242 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
3243 opt_comment, perms, opt_maxusers,
3244 num_users, path, password,
3246 return werror_to_ntstatus(result);
3249 static int rpc_share_add(int argc, const char **argv)
3251 if ((argc < 1) || !strchr(argv[0], '=')) {
3252 DEBUG(1,("Sharename or path not specified on add\n"));
3253 return rpc_share_usage(argc, argv);
3255 return run_rpc_command(NULL, PI_SRVSVC, 0,
3256 rpc_share_add_internals,
3261 * Delete a share on a remote RPC server
3263 * All parameters are provided by the run_rpc_command function, except for
3264 * argc, argv which are passes through.
3266 * @param domain_sid The domain sid acquired from the remote server
3267 * @param cli A cli_state connected to the server.
3268 * @param mem_ctx Talloc context, destoyed on completion of the function.
3269 * @param argc Standard main() style argc
3270 * @param argv Standard main() style argv. Initial components are already
3273 * @return Normal NTSTATUS return.
3275 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3276 const char *domain_name,
3277 struct cli_state *cli,
3278 struct rpc_pipe_client *pipe_hnd,
3279 TALLOC_CTX *mem_ctx,
3285 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
3286 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3290 * Delete a share on a remote RPC server
3292 * @param domain_sid The domain sid acquired from the remote server
3293 * @param argc Standard main() style argc
3294 * @param argv Standard main() style argv. Initial components are already
3297 * @return A shell status integer (0 for success)
3299 static int rpc_share_delete(int argc, const char **argv)
3302 DEBUG(1,("Sharename not specified on delete\n"));
3303 return rpc_share_usage(argc, argv);
3305 return run_rpc_command(NULL, PI_SRVSVC, 0,
3306 rpc_share_del_internals,
3311 * Formatted print of share info
3313 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3316 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3318 fstring netname = "", remark = "";
3320 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3321 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3323 if (opt_long_list_entries) {
3324 d_printf("%-12s %-8.8s %-50s\n",
3325 netname, share_type[info1->info_1.type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)], remark);
3327 d_printf("%s\n", netname);
3332 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3333 TALLOC_CTX *mem_ctx,
3337 SRV_SHARE_INFO_CTR *ctr)
3340 SRV_SHARE_INFO info;
3342 /* no specific share requested, enumerate all */
3346 uint32 preferred_len = 0xffffffff;
3348 init_enum_hnd(&hnd, 0);
3350 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3351 preferred_len, &hnd);
3354 /* request just one share */
3355 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3357 if (!W_ERROR_IS_OK(result))
3363 ctr->info_level = ctr->switch_value = level;
3364 ctr->ptr_share_info = ctr->ptr_entries = 1;
3365 ctr->num_entries = ctr->num_entries2 = 1;
3371 SRV_SHARE_INFO_1 *info1;
3373 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3374 if (ctr->share.info1 == NULL) {
3375 result = WERR_NOMEM;
3378 info1 = ctr->share.info1;
3380 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3382 /* Copy pointer crap */
3384 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3386 /* Duplicate strings */
3388 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3390 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3392 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3394 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3399 SRV_SHARE_INFO_2 *info2;
3401 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3402 if (ctr->share.info2 == NULL) {
3403 result = WERR_NOMEM;
3406 info2 = ctr->share.info2;
3408 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3410 /* Copy pointer crap */
3412 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3414 /* Duplicate strings */
3416 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3418 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3420 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3422 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3424 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_path);
3426 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3428 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3430 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3435 SRV_SHARE_INFO_502 *info502;
3437 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3438 if (ctr->share.info502 == NULL) {
3439 result = WERR_NOMEM;
3442 info502 = ctr->share.info502;
3444 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3446 /* Copy pointer crap */
3448 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3450 /* Duplicate strings */
3452 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3454 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3456 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3458 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3460 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_path);
3462 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3464 s = unistr2_to_ascii_talloc(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3466 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3468 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3479 * List shares on a remote RPC server
3481 * All parameters are provided by the run_rpc_command function, except for
3482 * argc, argv which are passes through.
3484 * @param domain_sid The domain sid acquired from the remote server
3485 * @param cli A cli_state connected to the server.
3486 * @param mem_ctx Talloc context, destoyed on completion of the function.
3487 * @param argc Standard main() style argc
3488 * @param argv Standard main() style argv. Initial components are already
3491 * @return Normal NTSTATUS return.
3494 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3495 const char *domain_name,
3496 struct cli_state *cli,
3497 struct rpc_pipe_client *pipe_hnd,
3498 TALLOC_CTX *mem_ctx,
3502 SRV_SHARE_INFO_CTR ctr;
3504 uint32 i, level = 1;
3506 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3507 if (!W_ERROR_IS_OK(result))
3510 /* Display results */
3512 if (opt_long_list_entries) {
3514 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3515 "\nShare name Type Description\n"\
3516 "---------- ---- -----------\n");
3518 for (i = 0; i < ctr.num_entries; i++)
3519 display_share_info_1(&ctr.share.info1[i]);
3521 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3525 * 'net rpc share list' entrypoint.
3526 * @param argc Standard main() style argc
3527 * @param argv Standard main() style argv. Initial components are already
3530 static int rpc_share_list(int argc, const char **argv)
3532 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3535 static bool check_share_availability(struct cli_state *cli, const char *netname)
3537 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3538 d_printf("skipping [%s]: not a file share.\n", netname);
3548 static bool check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3550 /* only support disk shares */
3551 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3552 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3556 /* skip builtin shares */
3557 /* FIXME: should print$ be added too ? */
3558 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3559 strequal(netname,"global"))
3562 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3563 printf("excluding [%s]\n", netname);
3567 return check_share_availability(cli, netname);
3571 * Migrate shares from a remote RPC server to the local RPC srever
3573 * All parameters are provided by the run_rpc_command function, except for
3574 * argc, argv which are passes through.
3576 * @param domain_sid The domain sid acquired from the remote server
3577 * @param cli A cli_state connected to the server.
3578 * @param mem_ctx Talloc context, destoyed on completion of the function.
3579 * @param argc Standard main() style argc
3580 * @param argv Standard main() style argv. Initial components are already
3583 * @return Normal NTSTATUS return.
3586 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3587 const char *domain_name,
3588 struct cli_state *cli,
3589 struct rpc_pipe_client *pipe_hnd,
3590 TALLOC_CTX *mem_ctx,
3595 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3596 SRV_SHARE_INFO_CTR ctr_src;
3597 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3598 char *password = NULL; /* don't allow a share password */
3600 struct rpc_pipe_client *srvsvc_pipe = NULL;
3601 struct cli_state *cli_dst = NULL;
3602 uint32 level = 502; /* includes secdesc */
3604 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3605 if (!W_ERROR_IS_OK(result))
3608 /* connect destination PI_SRVSVC */
3609 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3610 if (!NT_STATUS_IS_OK(nt_status))
3614 for (i = 0; i < ctr_src.num_entries; i++) {
3616 fstring netname = "", remark = "", path = "";
3617 /* reset error-code */
3618 nt_status = NT_STATUS_UNSUCCESSFUL;
3620 rpcstr_pull_unistr2_fstring(
3621 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3622 rpcstr_pull_unistr2_fstring(
3623 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3624 rpcstr_pull_unistr2_fstring(
3625 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3627 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3630 /* finally add the share on the dst server */
3632 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3633 netname, path, remark);
3635 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3636 ctr_src.share.info502[i].info_502.perms,
3637 ctr_src.share.info502[i].info_502.max_uses,
3638 ctr_src.share.info502[i].info_502.num_uses,
3639 path, password, level,
3642 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3643 printf(" [%s] does already exist\n", netname);
3647 if (!W_ERROR_IS_OK(result)) {
3648 printf("cannot add share: %s\n", dos_errstr(result));
3654 nt_status = NT_STATUS_OK;
3658 cli_shutdown(cli_dst);
3666 * Migrate shares from a rpc-server to another
3668 * @param argc Standard main() style argc
3669 * @param argv Standard main() style argv. Initial components are already
3672 * @return A shell status integer (0 for success)
3674 static int rpc_share_migrate_shares(int argc, const char **argv)
3678 printf("no server to migrate\n");
3682 return run_rpc_command(NULL, PI_SRVSVC, 0,
3683 rpc_share_migrate_shares_internals,
3690 * @param f file_info
3691 * @param mask current search mask
3692 * @param state arg-pointer
3695 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3697 static NTSTATUS nt_status;
3698 static struct copy_clistate *local_state;
3699 static fstring filename, new_mask;
3703 local_state = (struct copy_clistate *)state;
3704 nt_status = NT_STATUS_UNSUCCESSFUL;
3706 if (strequal(f->name, ".") || strequal(f->name, ".."))
3709 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3712 if (f->mode & aDIR) {
3714 DEBUG(3,("got dir: %s\n", f->name));
3716 fstrcpy(dir, local_state->cwd);
3718 fstrcat(dir, f->name);
3720 switch (net_mode_share)
3722 case NET_MODE_SHARE_MIGRATE:
3723 /* create that directory */
3724 nt_status = net_copy_file(local_state->mem_ctx,
3725 local_state->cli_share_src,
3726 local_state->cli_share_dst,
3728 opt_acls? True : False,
3729 opt_attrs? True : False,
3730 opt_timestamps? True : False,
3734 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3738 if (!NT_STATUS_IS_OK(nt_status))
3739 printf("could not handle dir %s: %s\n",
3740 dir, nt_errstr(nt_status));
3742 /* search below that directory */
3743 fstrcpy(new_mask, dir);
3744 fstrcat(new_mask, "\\*");
3746 old_dir = local_state->cwd;
3747 local_state->cwd = dir;
3748 if (!sync_files(local_state, new_mask))
3749 printf("could not handle files\n");
3750 local_state->cwd = old_dir;
3757 fstrcpy(filename, local_state->cwd);
3758 fstrcat(filename, "\\");
3759 fstrcat(filename, f->name);
3761 DEBUG(3,("got file: %s\n", filename));
3763 switch (net_mode_share)
3765 case NET_MODE_SHARE_MIGRATE:
3766 nt_status = net_copy_file(local_state->mem_ctx,
3767 local_state->cli_share_src,
3768 local_state->cli_share_dst,
3770 opt_acls? True : False,
3771 opt_attrs? True : False,
3772 opt_timestamps? True: False,
3776 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3780 if (!NT_STATUS_IS_OK(nt_status))
3781 printf("could not handle file %s: %s\n",
3782 filename, nt_errstr(nt_status));
3787 * sync files, can be called recursivly to list files
3788 * and then call copy_fn for each file
3790 * @param cp_clistate pointer to the copy_clistate we work with
3791 * @param mask the current search mask
3793 * @return Boolean result
3795 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3797 struct cli_state *targetcli;
3798 char *targetpath = NULL;
3800 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3802 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3803 mask, &targetcli, &targetpath ) ) {
3804 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3805 mask, cli_errstr(cp_clistate->cli_share_src));
3809 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3810 d_fprintf(stderr, "listing %s failed with error: %s\n",
3811 mask, cli_errstr(targetcli));
3820 * Set the top level directory permissions before we do any further copies.
3821 * Should set up ACL inheritance.
3824 bool copy_top_level_perms(struct copy_clistate *cp_clistate,
3825 const char *sharename)
3827 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3829 switch (net_mode_share) {
3830 case NET_MODE_SHARE_MIGRATE:
3831 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3832 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3833 cp_clistate->cli_share_src,
3834 cp_clistate->cli_share_dst,
3836 opt_acls? True : False,
3837 opt_attrs? True : False,
3838 opt_timestamps? True: False,
3842 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3846 if (!NT_STATUS_IS_OK(nt_status)) {
3847 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3848 sharename, nt_errstr(nt_status));
3856 * Sync all files inside a remote share to another share (over smb)
3858 * All parameters are provided by the run_rpc_command function, except for
3859 * argc, argv which are passes through.
3861 * @param domain_sid The domain sid acquired from the remote server
3862 * @param cli A cli_state connected to the server.
3863 * @param mem_ctx Talloc context, destoyed on completion of the function.
3864 * @param argc Standard main() style argc
3865 * @param argv Standard main() style argv. Initial components are already
3868 * @return Normal NTSTATUS return.
3871 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3872 const char *domain_name,
3873 struct cli_state *cli,
3874 struct rpc_pipe_client *pipe_hnd,
3875 TALLOC_CTX *mem_ctx,
3880 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3881 SRV_SHARE_INFO_CTR ctr_src;
3884 struct copy_clistate cp_clistate;
3885 bool got_src_share = False;
3886 bool got_dst_share = False;
3887 const char *mask = "\\*";
3890 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3892 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3894 if (!W_ERROR_IS_OK(result))
3897 for (i = 0; i < ctr_src.num_entries; i++) {
3899 fstring netname = "";
3901 rpcstr_pull_unistr2_fstring(
3902 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3904 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3907 /* one might not want to mirror whole discs :) */
3908 if (strequal(netname, "print$") || netname[1] == '$') {
3909 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3913 switch (net_mode_share)
3915 case NET_MODE_SHARE_MIGRATE:
3919 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3922 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3924 opt_acls ? "including" : "without",
3925 opt_attrs ? "including" : "without",
3926 opt_timestamps ? "(preserving timestamps)" : "");
3928 cp_clistate.mem_ctx = mem_ctx;
3929 cp_clistate.cli_share_src = NULL;
3930 cp_clistate.cli_share_dst = NULL;
3931 cp_clistate.cwd = NULL;
3932 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3934 /* open share source */
3935 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3936 &cli->dest_ss, cli->desthost,
3938 if (!NT_STATUS_IS_OK(nt_status))
3941 got_src_share = True;
3943 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3944 /* open share destination */
3945 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3946 NULL, dst, netname, "A:");
3947 if (!NT_STATUS_IS_OK(nt_status))
3950 got_dst_share = True;
3953 if (!copy_top_level_perms(&cp_clistate, netname)) {
3954 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
3955 nt_status = NT_STATUS_UNSUCCESSFUL;
3959 if (!sync_files(&cp_clistate, mask)) {
3960 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
3961 nt_status = NT_STATUS_UNSUCCESSFUL;
3966 nt_status = NT_STATUS_OK;
3971 cli_shutdown(cp_clistate.cli_share_src);
3974 cli_shutdown(cp_clistate.cli_share_dst);
3980 static int rpc_share_migrate_files(int argc, const char **argv)
3984 printf("no server to migrate\n");
3988 return run_rpc_command(NULL, PI_SRVSVC, 0,
3989 rpc_share_migrate_files_internals,
3994 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3996 * All parameters are provided by the run_rpc_command function, except for
3997 * argc, argv which are passes through.
3999 * @param domain_sid The domain sid acquired from the remote server
4000 * @param cli A cli_state connected to the server.
4001 * @param mem_ctx Talloc context, destoyed on completion of the function.
4002 * @param argc Standard main() style argc
4003 * @param argv Standard main() style argv. Initial components are already
4006 * @return Normal NTSTATUS return.
4009 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
4010 const char *domain_name,
4011 struct cli_state *cli,
4012 struct rpc_pipe_client *pipe_hnd,
4013 TALLOC_CTX *mem_ctx,
4018 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4019 SRV_SHARE_INFO_CTR ctr_src;
4020 SRV_SHARE_INFO info;
4022 struct rpc_pipe_client *srvsvc_pipe = NULL;
4023 struct cli_state *cli_dst = NULL;
4024 uint32 level = 502; /* includes secdesc */
4026 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
4028 if (!W_ERROR_IS_OK(result))
4031 /* connect destination PI_SRVSVC */
4032 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
4033 if (!NT_STATUS_IS_OK(nt_status))
4037 for (i = 0; i < ctr_src.num_entries; i++) {
4039 fstring netname = "", remark = "", path = "";
4040 /* reset error-code */
4041 nt_status = NT_STATUS_UNSUCCESSFUL;
4043 rpcstr_pull_unistr2_fstring(
4044 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
4045 rpcstr_pull_unistr2_fstring(
4046 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
4047 rpcstr_pull_unistr2_fstring(
4048 path, &ctr_src.share.info502[i].info_502_str.uni_path);
4050 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
4053 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
4054 netname, path, remark);
4057 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
4062 info.switch_value = level;
4063 info.ptr_share_ctr = 1;
4065 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4066 info.share.info502 = ctr_src.share.info502[i];
4068 /* finally modify the share on the dst server */
4069 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
4071 if (!W_ERROR_IS_OK(result)) {
4072 printf("cannot set share-acl: %s\n", dos_errstr(result));
4078 nt_status = NT_STATUS_OK;
4082 cli_shutdown(cli_dst);
4090 * Migrate share-acls from a rpc-server to another
4092 * @param argc Standard main() style argc
4093 * @param argv Standard main() style argv. Initial components are already
4096 * @return A shell status integer (0 for success)
4098 static int rpc_share_migrate_security(int argc, const char **argv)
4102 printf("no server to migrate\n");
4106 return run_rpc_command(NULL, PI_SRVSVC, 0,
4107 rpc_share_migrate_security_internals,
4112 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4113 * from one server to another
4115 * @param argc Standard main() style argc
4116 * @param argv Standard main() style argv. Initial components are already
4119 * @return A shell status integer (0 for success)
4122 static int rpc_share_migrate_all(int argc, const char **argv)
4127 printf("no server to migrate\n");
4131 /* order is important. we don't want to be locked out by the share-acl
4132 * before copying files - gd */
4134 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
4138 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
4142 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
4147 * 'net rpc share migrate' entrypoint.
4148 * @param argc Standard main() style argc
4149 * @param argv Standard main() style argv. Initial components are already
4152 static int rpc_share_migrate(int argc, const char **argv)
4155 struct functable func[] = {
4156 {"all", rpc_share_migrate_all},
4157 {"files", rpc_share_migrate_files},
4158 {"help", rpc_share_usage},
4159 {"security", rpc_share_migrate_security},
4160 {"shares", rpc_share_migrate_shares},
4164 net_mode_share = NET_MODE_SHARE_MIGRATE;
4166 return net_run_function(argc, argv, func, rpc_share_usage);
4175 static int num_server_aliases;
4176 static struct full_alias *server_aliases;
4179 * Add an alias to the static list.
4181 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4183 if (server_aliases == NULL)
4184 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4186 server_aliases[num_server_aliases] = *alias;
4187 num_server_aliases += 1;
4191 * For a specific domain on the server, fetch all the aliases
4192 * and their members. Add all of them to the server_aliases.
4195 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4196 TALLOC_CTX *mem_ctx,
4197 POLICY_HND *connect_pol,
4198 const DOM_SID *domain_sid)
4200 uint32 start_idx, max_entries, num_entries, i;
4201 struct acct_info *groups;
4203 POLICY_HND domain_pol;
4205 /* Get domain policy handle */
4207 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4209 MAXIMUM_ALLOWED_ACCESS,
4210 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4212 if (!NT_STATUS_IS_OK(result))
4219 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
4220 &start_idx, max_entries,
4221 &groups, &num_entries);
4223 for (i = 0; i < num_entries; i++) {
4225 POLICY_HND alias_pol;
4226 struct full_alias alias;
4227 struct lsa_SidArray sid_array;
4230 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4232 MAXIMUM_ALLOWED_ACCESS,
4235 if (!NT_STATUS_IS_OK(result))
4238 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4241 if (!NT_STATUS_IS_OK(result))
4244 alias.num_members = sid_array.num_sids;
4246 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4247 if (!NT_STATUS_IS_OK(result))
4250 alias.members = NULL;
4252 if (alias.num_members > 0) {
4253 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4255 for (j = 0; j < alias.num_members; j++)
4256 sid_copy(&alias.members[j],
4257 sid_array.sids[j].sid);
4260 sid_copy(&alias.sid, domain_sid);
4261 sid_append_rid(&alias.sid, groups[i].rid);
4263 push_alias(mem_ctx, &alias);
4265 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4267 result = NT_STATUS_OK;
4270 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4276 * Dump server_aliases as names for debugging purposes.
4279 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
4280 const char *domain_name,
4281 struct cli_state *cli,
4282 struct rpc_pipe_client *pipe_hnd,
4283 TALLOC_CTX *mem_ctx,
4291 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
4292 SEC_RIGHTS_MAXIMUM_ALLOWED,
4294 if (!NT_STATUS_IS_OK(result))
4297 for (i=0; i<num_server_aliases; i++) {
4300 enum lsa_SidType *types;
4303 struct full_alias *alias = &server_aliases[i];
4305 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4307 &domains, &names, &types);
4308 if (!NT_STATUS_IS_OK(result))
4311 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4313 if (alias->num_members == 0) {
4318 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4321 &domains, &names, &types);
4323 if (!NT_STATUS_IS_OK(result) &&
4324 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4327 for (j=0; j<alias->num_members; j++)
4328 DEBUG(1, ("%s\\%s (%d); ",
4329 domains[j] ? domains[j] : "*unknown*",
4330 names[j] ? names[j] : "*unknown*",types[j]));
4334 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4336 return NT_STATUS_OK;
4340 * Fetch a list of all server aliases and their members into
4344 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4345 const char *domain_name,
4346 struct cli_state *cli,
4347 struct rpc_pipe_client *pipe_hnd,
4348 TALLOC_CTX *mem_ctx,
4353 POLICY_HND connect_pol;
4355 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4356 pipe_hnd->cli->desthost,
4357 MAXIMUM_ALLOWED_ACCESS,
4360 if (!NT_STATUS_IS_OK(result))
4363 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4364 &global_sid_Builtin);
4366 if (!NT_STATUS_IS_OK(result))
4369 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4372 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4377 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4379 token->num_sids = 4;
4381 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4382 d_fprintf(stderr, "malloc failed\n");
4383 token->num_sids = 0;
4387 token->user_sids[0] = *user_sid;
4388 sid_copy(&token->user_sids[1], &global_sid_World);
4389 sid_copy(&token->user_sids[2], &global_sid_Network);
4390 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4393 static void free_user_token(NT_USER_TOKEN *token)
4395 SAFE_FREE(token->user_sids);
4398 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4402 for (i=0; i<token->num_sids; i++) {
4403 if (sid_compare(sid, &token->user_sids[i]) == 0)
4409 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4411 if (is_sid_in_token(token, sid))
4414 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4415 if (!token->user_sids) {
4419 sid_copy(&token->user_sids[token->num_sids], sid);
4421 token->num_sids += 1;
4426 NT_USER_TOKEN token;
4429 static void dump_user_token(struct user_token *token)
4433 d_printf("%s\n", token->name);
4435 for (i=0; i<token->token.num_sids; i++) {
4436 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4440 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4444 for (i=0; i<alias->num_members; i++) {
4445 if (sid_compare(sid, &alias->members[i]) == 0)
4452 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4456 for (i=0; i<num_server_aliases; i++) {
4457 if (is_alias_member(&sid, &server_aliases[i]))
4458 add_sid_to_token(token, &server_aliases[i].sid);
4463 * We got a user token with all the SIDs we can know about without asking the
4464 * server directly. These are the user and domain group sids. All of these can
4465 * be members of aliases. So scan the list of aliases for each of the SIDs and
4466 * add them to the token.
4469 static void collect_alias_memberships(NT_USER_TOKEN *token)
4471 int num_global_sids = token->num_sids;
4474 for (i=0; i<num_global_sids; i++) {
4475 collect_sid_memberships(token, token->user_sids[i]);
4479 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4481 struct winbindd_request request;
4482 struct winbindd_response response;
4490 fstr_sprintf(full_name, "%s%c%s",
4491 domain, *lp_winbind_separator(), user);
4493 /* First let's find out the user sid */
4495 ZERO_STRUCT(request);
4496 ZERO_STRUCT(response);
4498 fstrcpy(request.data.name.dom_name, domain);
4499 fstrcpy(request.data.name.name, user);
4501 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4503 if (result != NSS_STATUS_SUCCESS) {
4504 DEBUG(1, ("winbind could not find %s\n", full_name));
4508 if (response.data.sid.type != SID_NAME_USER) {
4509 DEBUG(1, ("%s is not a user\n", full_name));
4513 string_to_sid(&user_sid, response.data.sid.sid);
4515 init_user_token(token, &user_sid);
4517 /* And now the groups winbind knows about */
4519 ZERO_STRUCT(response);
4521 fstrcpy(request.data.username, full_name);
4523 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4525 if (result != NSS_STATUS_SUCCESS) {
4526 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4530 for (i = 0; i < response.data.num_entries; i++) {
4531 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4534 struct winbindd_request sidrequest;
4535 struct winbindd_response sidresponse;
4537 ZERO_STRUCT(sidrequest);
4538 ZERO_STRUCT(sidresponse);
4540 sidrequest.data.gid = gid;
4542 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4543 &sidrequest, &sidresponse);
4545 if (result != NSS_STATUS_SUCCESS) {
4546 DEBUG(1, ("winbind could not find SID of gid %d\n",
4551 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4553 string_to_sid(&sid, sidresponse.data.sid.sid);
4554 add_sid_to_token(token, &sid);
4557 SAFE_FREE(response.extra_data.data);
4563 * Get a list of all user tokens we want to look at
4566 static bool get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4568 struct winbindd_request request;
4569 struct winbindd_response response;
4570 const char *extra_data;
4573 struct user_token *result;
4574 TALLOC_CTX *frame = NULL;
4576 if (lp_winbind_use_default_domain() &&
4577 (opt_target_workgroup == NULL)) {
4578 d_fprintf(stderr, "winbind use default domain = yes set, "
4579 "please specify a workgroup\n");
4583 /* Send request to winbind daemon */
4585 ZERO_STRUCT(request);
4586 ZERO_STRUCT(response);
4588 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4592 /* Look through extra data */
4594 if (!response.extra_data.data)
4597 extra_data = (const char *)response.extra_data.data;
4600 frame = talloc_stackframe();
4601 while(next_token_talloc(frame, &extra_data, &name, ",")) {
4605 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4607 if (result == NULL) {
4608 DEBUG(1, ("Could not malloc sid array\n"));
4613 extra_data = (const char *)response.extra_data.data;
4616 while(next_token_talloc(frame, &extra_data, &name, ",")) {
4617 fstring domain, user;
4620 fstrcpy(result[i].name, name);
4622 p = strchr(name, *lp_winbind_separator());
4624 DEBUG(3, ("%s\n", name));
4627 fstrcpy(domain, opt_target_workgroup);
4628 fstrcpy(user, name);
4631 fstrcpy(domain, name);
4636 get_user_sids(domain, user, &(result[i].token));
4640 SAFE_FREE(response.extra_data.data);
4642 *user_tokens = result;
4647 static bool get_user_tokens_from_file(FILE *f,
4649 struct user_token **tokens)
4651 struct user_token *token = NULL;
4656 if (fgets(line, sizeof(line)-1, f) == NULL) {
4660 if (line[strlen(line)-1] == '\n')
4661 line[strlen(line)-1] = '\0';
4663 if (line[0] == ' ') {
4667 string_to_sid(&sid, &line[1]);
4669 if (token == NULL) {
4670 DEBUG(0, ("File does not begin with username"));
4674 add_sid_to_token(&token->token, &sid);
4678 /* And a new user... */
4681 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4682 if (*tokens == NULL) {
4683 DEBUG(0, ("Could not realloc tokens\n"));
4687 token = &((*tokens)[*num_tokens-1]);
4689 fstrcpy(token->name, line);
4690 token->token.num_sids = 0;
4691 token->token.user_sids = NULL;
4700 * Show the list of all users that have access to a share
4703 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4704 TALLOC_CTX *mem_ctx,
4705 const char *netname,
4707 struct user_token *tokens)
4710 SEC_DESC *share_sd = NULL;
4711 SEC_DESC *root_sd = NULL;
4712 struct cli_state *cli = pipe_hnd->cli;
4714 SRV_SHARE_INFO info;
4718 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4721 if (!W_ERROR_IS_OK(result)) {
4722 DEBUG(1, ("Coult not query secdesc for share %s\n",
4727 share_sd = info.share.info502.info_502_str.sd;
4728 if (share_sd == NULL) {
4729 DEBUG(1, ("Got no secdesc for share %s\n",
4735 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4739 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4742 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4745 for (i=0; i<num_tokens; i++) {
4749 if (share_sd != NULL) {
4750 if (!se_access_check(share_sd, &tokens[i].token,
4751 1, &acc_granted, &status)) {
4752 DEBUG(1, ("Could not check share_sd for "
4758 if (!NT_STATUS_IS_OK(status))
4762 if (root_sd == NULL) {
4763 d_printf(" %s\n", tokens[i].name);
4767 if (!se_access_check(root_sd, &tokens[i].token,
4768 1, &acc_granted, &status)) {
4769 DEBUG(1, ("Could not check root_sd for user %s\n",
4774 if (!NT_STATUS_IS_OK(status))
4777 d_printf(" %s\n", tokens[i].name);
4781 cli_close(cli, fnum);
4793 static void collect_share(const char *name, uint32 m,
4794 const char *comment, void *state)
4796 struct share_list *share_list = (struct share_list *)state;
4798 if (m != STYPE_DISKTREE)
4801 share_list->num_shares += 1;
4802 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4803 if (!share_list->shares) {
4804 share_list->num_shares = 0;
4807 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4810 static void rpc_share_userlist_usage(void)
4816 * List shares on a remote RPC server, including the security descriptors
4818 * All parameters are provided by the run_rpc_command function, except for
4819 * argc, argv which are passes through.
4821 * @param domain_sid The domain sid acquired from the remote server
4822 * @param cli A cli_state connected to the server.
4823 * @param mem_ctx Talloc context, destoyed on completion of the function.
4824 * @param argc Standard main() style argc
4825 * @param argv Standard main() style argv. Initial components are already
4828 * @return Normal NTSTATUS return.
4831 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4832 const char *domain_name,
4833 struct cli_state *cli,
4834 struct rpc_pipe_client *pipe_hnd,
4835 TALLOC_CTX *mem_ctx,
4845 struct user_token *tokens = NULL;
4848 struct share_list share_list;
4851 rpc_share_userlist_usage();
4852 return NT_STATUS_UNSUCCESSFUL;
4858 f = fopen(argv[0], "r");
4862 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4863 return NT_STATUS_UNSUCCESSFUL;
4866 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4872 DEBUG(0, ("Could not read users from file\n"));
4873 return NT_STATUS_UNSUCCESSFUL;
4876 for (i=0; i<num_tokens; i++)
4877 collect_alias_memberships(&tokens[i].token);
4879 init_enum_hnd(&hnd, 0);
4881 share_list.num_shares = 0;
4882 share_list.shares = NULL;
4884 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4887 DEBUG(0, ("Error returning browse list: %s\n",
4892 for (i = 0; i < share_list.num_shares; i++) {
4893 char *netname = share_list.shares[i];
4895 if (netname[strlen(netname)-1] == '$')
4898 d_printf("%s\n", netname);
4900 show_userlist(pipe_hnd, mem_ctx, netname,
4901 num_tokens, tokens);
4904 for (i=0; i<num_tokens; i++) {
4905 free_user_token(&tokens[i].token);
4908 SAFE_FREE(share_list.shares);
4910 return NT_STATUS_OK;
4913 static int rpc_share_allowedusers(int argc, const char **argv)
4917 result = run_rpc_command(NULL, PI_SAMR, 0,
4918 rpc_aliaslist_internals,
4923 result = run_rpc_command(NULL, PI_LSARPC, 0,
4929 return run_rpc_command(NULL, PI_SRVSVC, 0,
4930 rpc_share_allowedusers_internals,
4934 int net_usersidlist(int argc, const char **argv)
4937 struct user_token *tokens = NULL;
4941 net_usersidlist_usage(argc, argv);
4945 if (!get_user_tokens(&num_tokens, &tokens)) {
4946 DEBUG(0, ("Could not get the user/sid list\n"));
4950 for (i=0; i<num_tokens; i++) {
4951 dump_user_token(&tokens[i]);
4952 free_user_token(&tokens[i].token);
4959 int net_usersidlist_usage(int argc, const char **argv)
4961 d_printf("net usersidlist\n"
4962 "\tprints out a list of all users the running winbind knows\n"
4963 "\tabout, together with all their SIDs. This is used as\n"
4964 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4966 net_common_flags_usage(argc, argv);
4971 * 'net rpc share' entrypoint.
4972 * @param argc Standard main() style argc
4973 * @param argv Standard main() style argv. Initial components are already
4977 int net_rpc_share(int argc, const char **argv)
4979 struct functable func[] = {
4980 {"add", rpc_share_add},
4981 {"delete", rpc_share_delete},
4982 {"allowedusers", rpc_share_allowedusers},
4983 {"migrate", rpc_share_migrate},
4984 {"list", rpc_share_list},
4989 return run_rpc_command(NULL, PI_SRVSVC, 0,
4990 rpc_share_list_internals,
4993 return net_run_function(argc, argv, func, rpc_share_usage);
4996 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4997 struct rpc_sh_ctx *ctx,
4998 struct rpc_pipe_client *pipe_hnd,
4999 int argc, const char **argv)
5001 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
5002 ctx->cli, pipe_hnd, mem_ctx,
5006 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
5007 struct rpc_sh_ctx *ctx,
5008 struct rpc_pipe_client *pipe_hnd,
5009 int argc, const char **argv)
5013 if ((argc < 2) || (argc > 3)) {
5014 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
5016 return NT_STATUS_INVALID_PARAMETER;
5019 result = rpccli_srvsvc_net_share_add(
5020 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
5021 (argc == 3) ? argv[2] : "",
5022 0, 0, 0, argv[1], NULL, 2, NULL);
5024 return werror_to_ntstatus(result);
5027 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
5028 struct rpc_sh_ctx *ctx,
5029 struct rpc_pipe_client *pipe_hnd,
5030 int argc, const char **argv)
5035 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5036 return NT_STATUS_INVALID_PARAMETER;
5039 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
5040 return werror_to_ntstatus(result);
5043 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
5044 struct rpc_sh_ctx *ctx,
5045 struct rpc_pipe_client *pipe_hnd,
5046 int argc, const char **argv)
5048 SRV_SHARE_INFO info;
5049 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
5053 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5054 return NT_STATUS_INVALID_PARAMETER;
5057 result = rpccli_srvsvc_net_share_get_info(
5058 pipe_hnd, mem_ctx, argv[0], 2, &info);
5059 if (!W_ERROR_IS_OK(result)) {
5063 d_printf("Name: %s\n",
5064 rpcstr_pull_unistr2_talloc(mem_ctx,
5065 &info2->info_2_str.uni_netname));
5066 d_printf("Comment: %s\n",
5067 rpcstr_pull_unistr2_talloc(mem_ctx,
5068 &info2->info_2_str.uni_remark));
5070 d_printf("Path: %s\n",
5071 rpcstr_pull_unistr2_talloc(mem_ctx,
5072 &info2->info_2_str.uni_path));
5073 d_printf("Password: %s\n",
5074 rpcstr_pull_unistr2_talloc(mem_ctx,
5075 &info2->info_2_str.uni_passwd));
5078 return werror_to_ntstatus(result);
5081 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
5082 struct rpc_sh_ctx *ctx)
5084 static struct rpc_sh_cmd cmds[] = {
5086 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
5087 "List available shares" },
5089 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
5092 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
5095 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
5096 "Get information about a share" },
5098 { NULL, NULL, 0, NULL, NULL }
5104 /****************************************************************************/
5106 static int rpc_file_usage(int argc, const char **argv)
5108 return net_help_file(argc, argv);
5112 * Close a file on a remote RPC server
5114 * All parameters are provided by the run_rpc_command function, except for
5115 * argc, argv which are passes through.
5117 * @param domain_sid The domain sid acquired from the remote server
5118 * @param cli A cli_state connected to the server.
5119 * @param mem_ctx Talloc context, destoyed on completion of the function.
5120 * @param argc Standard main() style argc
5121 * @param argv Standard main() style argv. Initial components are already
5124 * @return Normal NTSTATUS return.
5126 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
5127 const char *domain_name,
5128 struct cli_state *cli,
5129 struct rpc_pipe_client *pipe_hnd,
5130 TALLOC_CTX *mem_ctx,
5134 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5135 pipe_hnd->cli->desthost,
5136 atoi(argv[0]), NULL);
5140 * Close a file on a remote RPC server
5142 * @param argc Standard main() style argc
5143 * @param argv Standard main() style argv. Initial components are already
5146 * @return A shell status integer (0 for success)
5148 static int rpc_file_close(int argc, const char **argv)
5151 DEBUG(1, ("No fileid given on close\n"));
5152 return(rpc_file_usage(argc, argv));
5155 return run_rpc_command(NULL, PI_SRVSVC, 0,
5156 rpc_file_close_internals,
5161 * Formatted print of open file info
5163 * @param info3 FILE_INFO_3 contents
5164 * @param str3 strings for FILE_INFO_3
5167 static void display_file_info_3( FILE_INFO_3 *info3 )
5169 fstring user = "", path = "";
5171 rpcstr_pull_unistr2_fstring(user, info3->user);
5172 rpcstr_pull_unistr2_fstring(path, info3->path);
5174 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5175 info3->id, user, info3->perms, info3->num_locks, path);
5179 * List open files on a remote RPC server
5181 * All parameters are provided by the run_rpc_command function, except for
5182 * argc, argv which are passes through.
5184 * @param domain_sid The domain sid acquired from the remote server
5185 * @param cli A cli_state connected to the server.
5186 * @param mem_ctx Talloc context, destoyed on completion of the function.
5187 * @param argc Standard main() style argc
5188 * @param argv Standard main() style argv. Initial components are already
5191 * @return Normal NTSTATUS return.
5194 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
5195 const char *domain_name,
5196 struct cli_state *cli,
5197 struct rpc_pipe_client *pipe_hnd,
5198 TALLOC_CTX *mem_ctx,
5202 SRV_FILE_INFO_CTR ctr;
5205 uint32 preferred_len = 0xffffffff, i;
5206 const char *username=NULL;
5208 init_enum_hnd(&hnd, 0);
5210 /* if argc > 0, must be user command */
5212 username = smb_xstrdup(argv[0]);
5214 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
5215 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
5217 if (!W_ERROR_IS_OK(result))
5220 /* Display results */
5223 "\nEnumerating open files on remote server:\n\n"\
5224 "\nFileId Opened by Perms Locks Path"\
5225 "\n------ --------- ----- ----- ---- \n");
5226 for (i = 0; i < ctr.num_entries; i++)
5227 display_file_info_3(&ctr.file.info3[i]);
5229 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5233 * List files for a user on a remote RPC server
5235 * @param argc Standard main() style argc
5236 * @param argv Standard main() style argv. Initial components are already
5239 * @return A shell status integer (0 for success)
5242 static int rpc_file_user(int argc, const char **argv)
5245 DEBUG(1, ("No username given\n"));
5246 return(rpc_file_usage(argc, argv));
5249 return run_rpc_command(NULL, PI_SRVSVC, 0,
5250 rpc_file_list_internals,
5255 * 'net rpc file' entrypoint.
5256 * @param argc Standard main() style argc
5257 * @param argv Standard main() style argv. Initial components are already
5261 int net_rpc_file(int argc, const char **argv)
5263 struct functable func[] = {
5264 {"close", rpc_file_close},
5265 {"user", rpc_file_user},
5267 {"info", rpc_file_info},
5273 return run_rpc_command(NULL, PI_SRVSVC, 0,
5274 rpc_file_list_internals,
5277 return net_run_function(argc, argv, func, rpc_file_usage);
5281 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5283 * All parameters are provided by the run_rpc_command function, except for
5284 * argc, argv which are passed through.
5286 * @param domain_sid The domain sid aquired from the remote server
5287 * @param cli A cli_state connected to the server.
5288 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5289 * @param argc Standard main() style argc
5290 * @param argv Standard main() style argv. Initial components are already
5293 * @return Normal NTSTATUS return.
5296 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
5297 const char *domain_name,
5298 struct cli_state *cli,
5299 struct rpc_pipe_client *pipe_hnd,
5300 TALLOC_CTX *mem_ctx,
5304 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5306 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5308 if (NT_STATUS_IS_OK(result)) {
5309 d_printf("\nShutdown successfully aborted\n");
5310 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5312 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5318 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5320 * All parameters are provided by the run_rpc_command function, except for
5321 * argc, argv which are passed through.
5323 * @param domain_sid The domain sid aquired from the remote server
5324 * @param cli A cli_state connected to the server.
5325 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5326 * @param argc Standard main() style argc
5327 * @param argv Standard main() style argv. Initial components are already
5330 * @return Normal NTSTATUS return.
5333 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5334 const char *domain_name,
5335 struct cli_state *cli,
5336 struct rpc_pipe_client *pipe_hnd,
5337 TALLOC_CTX *mem_ctx,
5341 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5343 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5345 if (NT_STATUS_IS_OK(result)) {
5346 d_printf("\nShutdown successfully aborted\n");
5347 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5349 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5355 * ABORT the Shut down of a remote RPC server
5357 * @param argc Standard main() style argc
5358 * @param argv Standard main() style argv. Initial components are already
5361 * @return A shell status integer (0 for success)
5364 static int rpc_shutdown_abort(int argc, const char **argv)
5366 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5367 rpc_shutdown_abort_internals,
5373 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5375 return run_rpc_command(NULL, PI_WINREG, 0,
5376 rpc_reg_shutdown_abort_internals,
5381 * Shut down a remote RPC Server via initshutdown pipe
5383 * All parameters are provided by the run_rpc_command function, except for
5384 * argc, argv which are passes through.
5386 * @param domain_sid The domain sid aquired from the remote server
5387 * @param cli A cli_state connected to the server.
5388 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5389 * @param argc Standard main() style argc
5390 * @param argc Standard main() style argv. Initial components are already
5393 * @return Normal NTSTATUS return.
5396 NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5397 const char *domain_name,
5398 struct cli_state *cli,
5399 struct rpc_pipe_client *pipe_hnd,
5400 TALLOC_CTX *mem_ctx,
5404 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5405 const char *msg = "This machine will be shutdown shortly";
5406 uint32 timeout = 20;
5407 struct initshutdown_String msg_string;
5408 struct initshutdown_String_sub s;
5414 timeout = opt_timeout;
5418 msg_string.name = &s;
5420 /* create an entry */
5421 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5422 &msg_string, timeout, opt_force, opt_reboot, NULL);
5424 if (NT_STATUS_IS_OK(result)) {
5425 d_printf("\nShutdown of remote machine succeeded\n");
5426 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5428 DEBUG(1,("Shutdown of remote machine failed!\n"));
5434 * Shut down a remote RPC Server via winreg pipe
5436 * All parameters are provided by the run_rpc_command function, except for
5437 * argc, argv which are passes through.
5439 * @param domain_sid The domain sid aquired from the remote server
5440 * @param cli A cli_state connected to the server.
5441 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5442 * @param argc Standard main() style argc
5443 * @param argc Standard main() style argv. Initial components are already
5446 * @return Normal NTSTATUS return.
5449 NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5450 const char *domain_name,
5451 struct cli_state *cli,
5452 struct rpc_pipe_client *pipe_hnd,
5453 TALLOC_CTX *mem_ctx,
5457 const char *msg = "This machine will be shutdown shortly";
5458 uint32 timeout = 20;
5459 struct initshutdown_String msg_string;
5460 struct initshutdown_String_sub s;
5468 msg_string.name = &s;
5471 timeout = opt_timeout;
5474 /* create an entry */
5475 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5476 &msg_string, timeout, opt_force, opt_reboot, &werr);
5478 if (NT_STATUS_IS_OK(result)) {
5479 d_printf("\nShutdown of remote machine succeeded\n");
5481 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5482 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5483 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5485 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5492 * Shut down a remote RPC server
5494 * @param argc Standard main() style argc
5495 * @param argc Standard main() style argv. Initial components are already
5498 * @return A shell status integer (0 for success)
5501 static int rpc_shutdown(int argc, const char **argv)
5503 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5504 rpc_init_shutdown_internals,
5508 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5509 rc = run_rpc_command(NULL, PI_WINREG, 0,
5510 rpc_reg_shutdown_internals, argc, argv);
5516 /***************************************************************************
5517 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5519 ***************************************************************************/
5522 * Add interdomain trust account to the RPC server.
5523 * All parameters (except for argc and argv) are passed by run_rpc_command
5526 * @param domain_sid The domain sid acquired from the server
5527 * @param cli A cli_state connected to the server.
5528 * @param mem_ctx Talloc context, destoyed on completion of the function.
5529 * @param argc Standard main() style argc
5530 * @param argc Standard main() style argv. Initial components are already
5533 * @return normal NTSTATUS return code
5536 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5537 const char *domain_name,
5538 struct cli_state *cli,
5539 struct rpc_pipe_client *pipe_hnd,
5540 TALLOC_CTX *mem_ctx,
5544 POLICY_HND connect_pol, domain_pol, user_pol;
5545 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5547 struct lsa_String lsa_acct_name;
5549 uint32 acct_flags=0;
5551 uint32_t access_granted = 0;
5554 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5555 return NT_STATUS_INVALID_PARAMETER;
5559 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5562 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5563 return NT_STATUS_NO_MEMORY;
5566 strupper_m(acct_name);
5568 init_lsa_String(&lsa_acct_name, acct_name);
5570 /* Get samr policy handle */
5571 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5572 pipe_hnd->cli->desthost,
5573 MAXIMUM_ALLOWED_ACCESS,
5575 if (!NT_STATUS_IS_OK(result)) {
5579 /* Get domain policy handle */
5580 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5582 MAXIMUM_ALLOWED_ACCESS,
5583 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5585 if (!NT_STATUS_IS_OK(result)) {
5589 /* Create trusting domain's account */
5590 acb_info = ACB_NORMAL;
5591 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5592 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5593 SAMR_USER_ACCESS_SET_PASSWORD |
5594 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5595 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5597 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5605 if (!NT_STATUS_IS_OK(result)) {
5610 SAM_USERINFO_CTR ctr;
5611 SAM_USER_INFO_23 p23;
5617 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5621 ZERO_STRUCT(notime);
5625 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5626 acb_info = ACB_DOMTRUST;
5628 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5629 ¬ime, ¬ime, ¬ime,
5630 nostr, nostr, nostr, nostr, nostr,
5631 nostr, nostr, nostr, nostr, nostr,
5632 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5633 0, 0, (char *)pwbuf);
5634 ctr.switch_value = 23;
5635 ctr.info.id23 = &p23;
5636 p23.passmustchange = 0;
5638 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5639 &cli->user_session_key, &ctr);
5641 if (!NT_STATUS_IS_OK(result)) {
5642 DEBUG(0,("Could not set trust account password: %s\n",
5643 nt_errstr(result)));
5649 SAFE_FREE(acct_name);
5654 * Create interdomain trust account for a remote domain.
5656 * @param argc standard argc
5657 * @param argv standard argv without initial components
5659 * @return Integer status (0 means success)
5662 static int rpc_trustdom_add(int argc, const char **argv)
5665 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5668 d_printf("Usage: net rpc trustdom add <domain>\n");
5675 * Remove interdomain trust account from the RPC server.
5676 * All parameters (except for argc and argv) are passed by run_rpc_command
5679 * @param domain_sid The domain sid acquired from the server
5680 * @param cli A cli_state connected to the server.
5681 * @param mem_ctx Talloc context, destoyed on completion of the function.
5682 * @param argc Standard main() style argc
5683 * @param argc Standard main() style argv. Initial components are already
5686 * @return normal NTSTATUS return code
5689 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5690 const char *domain_name,
5691 struct cli_state *cli,
5692 struct rpc_pipe_client *pipe_hnd,
5693 TALLOC_CTX *mem_ctx,
5697 POLICY_HND connect_pol, domain_pol, user_pol;
5698 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5701 DOM_SID trust_acct_sid;
5702 uint32 *user_rids, num_rids, *name_types;
5703 uint32 flags = 0x000003e8; /* Unknown */
5706 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5707 return NT_STATUS_INVALID_PARAMETER;
5711 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5713 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5715 if (acct_name == NULL)
5716 return NT_STATUS_NO_MEMORY;
5718 strupper_m(acct_name);
5720 if ((names = TALLOC_ARRAY(mem_ctx, const char *, 1)) == NULL) {
5721 return NT_STATUS_NO_MEMORY;
5723 names[0] = acct_name;
5726 /* Get samr policy handle */
5727 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5728 pipe_hnd->cli->desthost,
5729 MAXIMUM_ALLOWED_ACCESS,
5731 if (!NT_STATUS_IS_OK(result)) {
5735 /* Get domain policy handle */
5736 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5738 MAXIMUM_ALLOWED_ACCESS,
5739 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5741 if (!NT_STATUS_IS_OK(result)) {
5745 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5747 &user_rids, &name_types);
5749 if (!NT_STATUS_IS_OK(result)) {
5753 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5755 MAXIMUM_ALLOWED_ACCESS,
5759 if (!NT_STATUS_IS_OK(result)) {
5763 /* append the rid to the domain sid */
5764 sid_copy(&trust_acct_sid, domain_sid);
5765 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5769 /* remove the sid */
5771 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5774 if (!NT_STATUS_IS_OK(result)) {
5780 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5783 if (!NT_STATUS_IS_OK(result)) {
5787 if (!NT_STATUS_IS_OK(result)) {
5788 DEBUG(0,("Could not set trust account password: %s\n",
5789 nt_errstr(result)));
5798 * Delete interdomain trust account for a remote domain.
5800 * @param argc standard argc
5801 * @param argv standard argv without initial components
5803 * @return Integer status (0 means success)
5806 static int rpc_trustdom_del(int argc, const char **argv)
5809 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5812 d_printf("Usage: net rpc trustdom del <domain>\n");
5819 * Establish trust relationship to a trusting domain.
5820 * Interdomain account must already be created on remote PDC.
5822 * @param argc standard argc
5823 * @param argv standard argv without initial components
5825 * @return Integer status (0 means success)
5828 static int rpc_trustdom_establish(int argc, const char **argv)
5830 struct cli_state *cli = NULL;
5831 struct sockaddr_storage server_ss;
5832 struct rpc_pipe_client *pipe_hnd = NULL;
5833 POLICY_HND connect_hnd;
5834 TALLOC_CTX *mem_ctx;
5836 DOM_SID *domain_sid;
5839 const char* domain_name_pol;
5845 * Connect to \\server\ipc$ as 'our domain' account with password
5849 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5853 domain_name = smb_xstrdup(argv[0]);
5854 strupper_m(domain_name);
5856 /* account name used at first is our domain's name with '$' */
5857 asprintf(&acct_name, "%s$", lp_workgroup());
5858 strupper_m(acct_name);
5861 * opt_workgroup will be used by connection functions further,
5862 * hence it should be set to remote domain name instead of ours
5864 if (opt_workgroup) {
5865 opt_workgroup = smb_xstrdup(domain_name);
5868 opt_user_name = acct_name;
5870 /* find the domain controller */
5871 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5872 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5876 /* connect to ipc$ as username/password */
5877 nt_status = connect_to_ipc(&cli, &server_ss, pdc_name);
5878 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5880 /* Is it trusting domain account for sure ? */
5881 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5882 nt_errstr(nt_status)));
5886 /* store who we connected to */
5888 saf_store( domain_name, pdc_name );
5891 * Connect to \\server\ipc$ again (this time anonymously)
5894 nt_status = connect_to_ipc_anonymous(&cli, &server_ss, (char*)pdc_name);
5896 if (NT_STATUS_IS_ERR(nt_status)) {
5897 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5898 domain_name, nt_errstr(nt_status)));
5903 * Use NetServerEnum2 to make sure we're talking to a proper server
5906 if (!cli_get_pdc_name(cli, domain_name, &dc_name)) {
5907 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5908 for domain %s\n", domain_name));
5914 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5915 "domain %s", domain_name))) {
5916 DEBUG(0, ("talloc_init() failed\n"));
5922 * Call LsaOpenPolicy and LsaQueryInfo
5925 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5927 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5929 talloc_destroy(mem_ctx);
5933 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5935 if (NT_STATUS_IS_ERR(nt_status)) {
5936 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5937 nt_errstr(nt_status)));
5939 talloc_destroy(mem_ctx);
5943 /* Querying info level 5 */
5945 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5947 &domain_name_pol, &domain_sid);
5948 if (NT_STATUS_IS_ERR(nt_status)) {
5949 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5950 nt_errstr(nt_status)));
5952 talloc_destroy(mem_ctx);
5956 /* There should be actually query info level 3 (following nt serv behaviour),
5957 but I still don't know if it's _really_ necessary */
5960 * Store the password in secrets db
5963 if (!pdb_set_trusteddom_pw(domain_name, opt_password, domain_sid)) {
5964 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5966 talloc_destroy(mem_ctx);
5971 * Close the pipes and clean up
5974 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5975 if (NT_STATUS_IS_ERR(nt_status)) {
5976 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5977 nt_errstr(nt_status)));
5979 talloc_destroy(mem_ctx);
5985 talloc_destroy(mem_ctx);
5987 d_printf("Trust to domain %s established\n", domain_name);
5992 * Revoke trust relationship to the remote domain
5994 * @param argc standard argc
5995 * @param argv standard argv without initial components
5997 * @return Integer status (0 means success)
6000 static int rpc_trustdom_revoke(int argc, const char **argv)
6005 if (argc < 1) return -1;
6007 /* generate upper cased domain name */
6008 domain_name = smb_xstrdup(argv[0]);
6009 strupper_m(domain_name);
6011 /* delete password of the trust */
6012 if (!pdb_del_trusteddom_pw(domain_name)) {
6013 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6020 SAFE_FREE(domain_name);
6025 * Usage for 'net rpc trustdom' command
6027 * @param argc standard argc
6028 * @param argv standard argv without inital components
6030 * @return Integer status returned to shell
6033 static int rpc_trustdom_usage(int argc, const char **argv)
6035 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
6036 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
6037 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
6038 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
6039 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
6040 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
6045 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
6046 const char *domain_name,
6047 struct cli_state *cli,
6048 struct rpc_pipe_client *pipe_hnd,
6049 TALLOC_CTX *mem_ctx,
6054 sid_to_fstring(str_sid, domain_sid);
6055 d_printf("%s\n", str_sid);
6056 return NT_STATUS_OK;
6059 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6061 fstring ascii_sid, padding;
6062 int pad_len, col_len = 20;
6064 /* convert sid into ascii string */
6065 sid_to_fstring(ascii_sid, dom_sid);
6067 /* calculate padding space for d_printf to look nicer */
6068 pad_len = col_len - strlen(trusted_dom_name);
6069 padding[pad_len] = 0;
6070 do padding[--pad_len] = ' '; while (pad_len);
6072 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6075 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6076 TALLOC_CTX *mem_ctx,
6079 const char *trusted_dom_name)
6082 union lsa_TrustedDomainInfo info;
6083 char *cleartextpwd = NULL;
6086 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6089 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6091 if (NT_STATUS_IS_ERR(nt_status)) {
6092 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6093 nt_errstr(nt_status)));
6097 data = data_blob(NULL, info.password.password->length);
6100 info.password.password->data,
6101 info.password.password->length);
6102 data.length = info.password.password->length;
6104 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password,
6107 if (cleartextpwd == NULL) {
6108 DEBUG(0,("retrieved NULL password\n"));
6109 nt_status = NT_STATUS_UNSUCCESSFUL;
6113 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6114 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6115 nt_status = NT_STATUS_UNSUCCESSFUL;
6119 #ifdef DEBUG_PASSWORD
6120 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], "
6121 "password: [%s]\n", trusted_dom_name,
6122 sid_string_dbg(&dom_sid), cleartextpwd));
6126 SAFE_FREE(cleartextpwd);
6127 data_blob_free(&data);
6132 static int rpc_trustdom_vampire(int argc, const char **argv)
6134 /* common variables */
6135 TALLOC_CTX* mem_ctx;
6136 struct cli_state *cli = NULL;
6137 struct rpc_pipe_client *pipe_hnd = NULL;
6139 const char *domain_name = NULL;
6140 DOM_SID *queried_dom_sid;
6141 POLICY_HND connect_hnd;
6143 /* trusted domains listing variables */
6144 unsigned int num_domains, enum_ctx = 0;
6146 DOM_SID *domain_sids;
6147 char **trusted_dom_names;
6152 * Listing trusted domains (stored in secrets.tdb, if local)
6155 mem_ctx = talloc_init("trust relationships vampire");
6158 * set domain and pdc name to local samba server (default)
6159 * or to remote one given in command line
6162 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6163 domain_name = opt_workgroup;
6164 opt_target_workgroup = opt_workgroup;
6166 fstrcpy(pdc_name, global_myname());
6167 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6168 opt_target_workgroup = domain_name;
6171 /* open \PIPE\lsarpc and open policy handle */
6172 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6173 if (!NT_STATUS_IS_OK(nt_status)) {
6174 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6175 nt_errstr(nt_status)));
6176 talloc_destroy(mem_ctx);
6180 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6182 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6183 nt_errstr(nt_status) ));
6185 talloc_destroy(mem_ctx);
6189 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6191 if (NT_STATUS_IS_ERR(nt_status)) {
6192 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6193 nt_errstr(nt_status)));
6195 talloc_destroy(mem_ctx);
6199 /* query info level 5 to obtain sid of a domain being queried */
6200 nt_status = rpccli_lsa_query_info_policy(
6201 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
6202 &dummy, &queried_dom_sid);
6204 if (NT_STATUS_IS_ERR(nt_status)) {
6205 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6206 nt_errstr(nt_status)));
6208 talloc_destroy(mem_ctx);
6213 * Keep calling LsaEnumTrustdom over opened pipe until
6214 * the end of enumeration is reached
6217 d_printf("Vampire trusted domains:\n\n");
6220 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6222 &trusted_dom_names, &domain_sids);
6224 if (NT_STATUS_IS_ERR(nt_status)) {
6225 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6226 nt_errstr(nt_status)));
6228 talloc_destroy(mem_ctx);
6232 for (i = 0; i < num_domains; i++) {
6234 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6236 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6237 domain_sids[i], trusted_dom_names[i]);
6238 if (!NT_STATUS_IS_OK(nt_status)) {
6240 talloc_destroy(mem_ctx);
6246 * in case of no trusted domains say something rather
6247 * than just display blank line
6249 if (!num_domains) d_printf("none\n");
6251 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6253 /* close this connection before doing next one */
6254 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6255 if (NT_STATUS_IS_ERR(nt_status)) {
6256 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6257 nt_errstr(nt_status)));
6259 talloc_destroy(mem_ctx);
6263 /* close lsarpc pipe and connection to IPC$ */
6266 talloc_destroy(mem_ctx);
6270 static int rpc_trustdom_list(int argc, const char **argv)
6272 /* common variables */
6273 TALLOC_CTX* mem_ctx;
6274 struct cli_state *cli = NULL, *remote_cli = NULL;
6275 struct rpc_pipe_client *pipe_hnd = NULL;
6277 const char *domain_name = NULL;
6278 DOM_SID *queried_dom_sid;
6280 int ascii_dom_name_len;
6281 POLICY_HND connect_hnd;
6283 /* trusted domains listing variables */
6284 unsigned int num_domains, enum_ctx = 0;
6285 int i, pad_len, col_len = 20;
6286 DOM_SID *domain_sids;
6287 char **trusted_dom_names;
6291 /* trusting domains listing variables */
6292 POLICY_HND domain_hnd;
6293 char **trusting_dom_names;
6294 uint32 *trusting_dom_rids;
6297 * Listing trusted domains (stored in secrets.tdb, if local)
6300 mem_ctx = talloc_init("trust relationships listing");
6303 * set domain and pdc name to local samba server (default)
6304 * or to remote one given in command line
6307 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6308 domain_name = opt_workgroup;
6309 opt_target_workgroup = opt_workgroup;
6311 fstrcpy(pdc_name, global_myname());
6312 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6313 opt_target_workgroup = domain_name;
6316 /* open \PIPE\lsarpc and open policy handle */
6317 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6318 if (!NT_STATUS_IS_OK(nt_status)) {
6319 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6320 nt_errstr(nt_status)));
6321 talloc_destroy(mem_ctx);
6325 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6327 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6328 nt_errstr(nt_status) ));
6330 talloc_destroy(mem_ctx);
6334 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6336 if (NT_STATUS_IS_ERR(nt_status)) {
6337 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6338 nt_errstr(nt_status)));
6340 talloc_destroy(mem_ctx);
6344 /* query info level 5 to obtain sid of a domain being queried */
6345 nt_status = rpccli_lsa_query_info_policy(
6346 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
6347 &dummy, &queried_dom_sid);
6349 if (NT_STATUS_IS_ERR(nt_status)) {
6350 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6351 nt_errstr(nt_status)));
6353 talloc_destroy(mem_ctx);
6358 * Keep calling LsaEnumTrustdom over opened pipe until
6359 * the end of enumeration is reached
6362 d_printf("Trusted domains list:\n\n");
6365 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6367 &trusted_dom_names, &domain_sids);
6369 if (NT_STATUS_IS_ERR(nt_status)) {
6370 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6371 nt_errstr(nt_status)));
6373 talloc_destroy(mem_ctx);
6377 for (i = 0; i < num_domains; i++) {
6378 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6382 * in case of no trusted domains say something rather
6383 * than just display blank line
6385 if (!num_domains) d_printf("none\n");
6387 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6389 /* close this connection before doing next one */
6390 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6391 if (NT_STATUS_IS_ERR(nt_status)) {
6392 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6393 nt_errstr(nt_status)));
6395 talloc_destroy(mem_ctx);
6399 cli_rpc_pipe_close(pipe_hnd);
6402 * Listing trusting domains (stored in passdb backend, if local)
6405 d_printf("\nTrusting domains list:\n\n");
6408 * Open \PIPE\samr and get needed policy handles
6410 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6412 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6414 talloc_destroy(mem_ctx);
6419 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6420 pipe_hnd->cli->desthost,
6421 SA_RIGHT_SAM_OPEN_DOMAIN,
6423 if (!NT_STATUS_IS_OK(nt_status)) {
6424 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6425 nt_errstr(nt_status)));
6427 talloc_destroy(mem_ctx);
6431 /* SamrOpenDomain - we have to open domain policy handle in order to be
6432 able to enumerate accounts*/
6433 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6435 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6438 if (!NT_STATUS_IS_OK(nt_status)) {
6439 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6440 nt_errstr(nt_status)));
6442 talloc_destroy(mem_ctx);
6447 * perform actual enumeration
6450 enum_ctx = 0; /* reset enumeration context from last enumeration */
6453 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6454 &enum_ctx, ACB_DOMTRUST, 0xffff,
6455 &trusting_dom_names, &trusting_dom_rids,
6457 if (NT_STATUS_IS_ERR(nt_status)) {
6458 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6459 nt_errstr(nt_status)));
6461 talloc_destroy(mem_ctx);
6465 for (i = 0; i < num_domains; i++) {
6468 * get each single domain's sid (do we _really_ need this ?):
6469 * 1) connect to domain's pdc
6470 * 2) query the pdc for domain's sid
6473 /* get rid of '$' tail */
6474 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6475 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6476 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6478 /* calculate padding space for d_printf to look nicer */
6479 pad_len = col_len - strlen(trusting_dom_names[i]);
6480 padding[pad_len] = 0;
6481 do padding[--pad_len] = ' '; while (pad_len);
6483 /* set opt_* variables to remote domain */
6484 strupper_m(trusting_dom_names[i]);
6485 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6486 opt_target_workgroup = opt_workgroup;
6488 d_printf("%s%s", trusting_dom_names[i], padding);
6490 /* connect to remote domain controller */
6491 nt_status = net_make_ipc_connection(
6492 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6494 if (NT_STATUS_IS_OK(nt_status)) {
6495 /* query for domain's sid */
6496 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6497 d_fprintf(stderr, "couldn't get domain's sid\n");
6499 cli_shutdown(remote_cli);
6502 d_fprintf(stderr, "domain controller is not "
6504 nt_errstr(nt_status));
6508 if (!num_domains) d_printf("none\n");
6510 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6512 /* close opened samr and domain policy handles */
6513 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6514 if (!NT_STATUS_IS_OK(nt_status)) {
6515 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6518 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6519 if (!NT_STATUS_IS_OK(nt_status)) {
6520 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6523 /* close samr pipe and connection to IPC$ */
6526 talloc_destroy(mem_ctx);
6531 * Entrypoint for 'net rpc trustdom' code
6533 * @param argc standard argc
6534 * @param argv standard argv without initial components
6536 * @return Integer status (0 means success)
6539 static int rpc_trustdom(int argc, const char **argv)
6541 struct functable func[] = {
6542 {"add", rpc_trustdom_add},
6543 {"del", rpc_trustdom_del},
6544 {"establish", rpc_trustdom_establish},
6545 {"revoke", rpc_trustdom_revoke},
6546 {"help", rpc_trustdom_usage},
6547 {"list", rpc_trustdom_list},
6548 {"vampire", rpc_trustdom_vampire},
6553 rpc_trustdom_usage(argc, argv);
6557 return (net_run_function(argc, argv, func, rpc_user_usage));
6561 * Check if a server will take rpc commands
6562 * @param flags Type of server to connect to (PDC, DMB, localhost)
6563 * if the host is not explicitly specified
6564 * @return bool (true means rpc supported)
6566 bool net_rpc_check(unsigned flags)
6568 struct cli_state *cli;
6570 struct sockaddr_storage server_ss;
6571 char *server_name = NULL;
6574 /* flags (i.e. server type) may depend on command */
6575 if (!net_find_server(NULL, flags, &server_ss, &server_name))
6578 if ((cli = cli_initialise()) == NULL) {
6582 status = cli_connect(cli, server_name, &server_ss);
6583 if (!NT_STATUS_IS_OK(status))
6585 if (!attempt_netbios_session_request(&cli, global_myname(),
6586 server_name, &server_ss))
6588 if (!cli_negprot(cli))
6590 if (cli->protocol < PROTOCOL_NT1)
6599 /* dump sam database via samsync rpc calls */
6600 static int rpc_samdump(int argc, const char **argv) {
6601 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6605 /* syncronise sam database via samsync rpc calls */
6606 static int rpc_vampire(int argc, const char **argv) {
6607 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6612 * Migrate everything from a print-server
6614 * @param argc Standard main() style argc
6615 * @param argv Standard main() style argv. Initial components are already
6618 * @return A shell status integer (0 for success)
6620 * The order is important !
6621 * To successfully add drivers the print-queues have to exist !
6622 * Applying ACLs should be the last step, because you're easily locked out
6625 static int rpc_printer_migrate_all(int argc, const char **argv)
6630 printf("no server to migrate\n");
6634 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6638 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6642 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6646 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6650 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6655 * Migrate print-drivers from a print-server
6657 * @param argc Standard main() style argc
6658 * @param argv Standard main() style argv. Initial components are already
6661 * @return A shell status integer (0 for success)
6663 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6666 printf("no server to migrate\n");
6670 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6671 rpc_printer_migrate_drivers_internals,
6676 * Migrate print-forms from a print-server
6678 * @param argc Standard main() style argc
6679 * @param argv Standard main() style argv. Initial components are already
6682 * @return A shell status integer (0 for success)
6684 static int rpc_printer_migrate_forms(int argc, const char **argv)
6687 printf("no server to migrate\n");
6691 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6692 rpc_printer_migrate_forms_internals,
6697 * Migrate printers from a print-server
6699 * @param argc Standard main() style argc
6700 * @param argv Standard main() style argv. Initial components are already
6703 * @return A shell status integer (0 for success)
6705 static int rpc_printer_migrate_printers(int argc, const char **argv)
6708 printf("no server to migrate\n");
6712 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6713 rpc_printer_migrate_printers_internals,
6718 * Migrate printer-ACLs from a print-server
6720 * @param argc Standard main() style argc
6721 * @param argv Standard main() style argv. Initial components are already
6724 * @return A shell status integer (0 for success)
6726 static int rpc_printer_migrate_security(int argc, const char **argv)
6729 printf("no server to migrate\n");
6733 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6734 rpc_printer_migrate_security_internals,
6739 * Migrate printer-settings from a print-server
6741 * @param argc Standard main() style argc
6742 * @param argv Standard main() style argv. Initial components are already
6745 * @return A shell status integer (0 for success)
6747 static int rpc_printer_migrate_settings(int argc, const char **argv)
6750 printf("no server to migrate\n");
6754 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6755 rpc_printer_migrate_settings_internals,
6760 * 'net rpc printer' entrypoint.
6761 * @param argc Standard main() style argc
6762 * @param argv Standard main() style argv. Initial components are already
6766 int rpc_printer_migrate(int argc, const char **argv)
6769 /* ouch: when addriver and setdriver are called from within
6770 rpc_printer_migrate_drivers_internals, the printer-queue already
6773 struct functable func[] = {
6774 {"all", rpc_printer_migrate_all},
6775 {"drivers", rpc_printer_migrate_drivers},
6776 {"forms", rpc_printer_migrate_forms},
6777 {"help", rpc_printer_usage},
6778 {"printers", rpc_printer_migrate_printers},
6779 {"security", rpc_printer_migrate_security},
6780 {"settings", rpc_printer_migrate_settings},
6784 return net_run_function(argc, argv, func, rpc_printer_usage);
6789 * List printers on a remote RPC server
6791 * @param argc Standard main() style argc
6792 * @param argv Standard main() style argv. Initial components are already
6795 * @return A shell status integer (0 for success)
6797 static int rpc_printer_list(int argc, const char **argv)
6800 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6801 rpc_printer_list_internals,
6806 * List printer-drivers on a remote RPC server
6808 * @param argc Standard main() style argc
6809 * @param argv Standard main() style argv. Initial components are already
6812 * @return A shell status integer (0 for success)
6814 static int rpc_printer_driver_list(int argc, const char **argv)
6817 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6818 rpc_printer_driver_list_internals,
6823 * Publish printer in ADS via MSRPC
6825 * @param argc Standard main() style argc
6826 * @param argv Standard main() style argv. Initial components are already
6829 * @return A shell status integer (0 for success)
6831 static int rpc_printer_publish_publish(int argc, const char **argv)
6834 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6835 rpc_printer_publish_publish_internals,
6840 * Update printer in ADS via MSRPC
6842 * @param argc Standard main() style argc
6843 * @param argv Standard main() style argv. Initial components are already
6846 * @return A shell status integer (0 for success)
6848 static int rpc_printer_publish_update(int argc, const char **argv)
6851 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6852 rpc_printer_publish_update_internals,
6857 * UnPublish printer in ADS via MSRPC
6859 * @param argc Standard main() style argc
6860 * @param argv Standard main() style argv. Initial components are already
6863 * @return A shell status integer (0 for success)
6865 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6868 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6869 rpc_printer_publish_unpublish_internals,
6874 * List published printers via MSRPC
6876 * @param argc Standard main() style argc
6877 * @param argv Standard main() style argv. Initial components are already
6880 * @return A shell status integer (0 for success)
6882 static int rpc_printer_publish_list(int argc, const char **argv)
6885 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6886 rpc_printer_publish_list_internals,
6892 * Publish printer in ADS
6894 * @param argc Standard main() style argc
6895 * @param argv Standard main() style argv. Initial components are already
6898 * @return A shell status integer (0 for success)
6900 static int rpc_printer_publish(int argc, const char **argv)
6903 struct functable func[] = {
6904 {"publish", rpc_printer_publish_publish},
6905 {"update", rpc_printer_publish_update},
6906 {"unpublish", rpc_printer_publish_unpublish},
6907 {"list", rpc_printer_publish_list},
6908 {"help", rpc_printer_usage},
6913 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6914 rpc_printer_publish_list_internals,
6917 return net_run_function(argc, argv, func, rpc_printer_usage);
6923 * Display rpc printer help page.
6924 * @param argc Standard main() style argc
6925 * @param argv Standard main() style argv. Initial components are already
6928 int rpc_printer_usage(int argc, const char **argv)
6930 return net_help_printer(argc, argv);
6934 * 'net rpc printer' entrypoint.
6935 * @param argc Standard main() style argc
6936 * @param argv Standard main() style argv. Initial components are already
6939 int net_rpc_printer(int argc, const char **argv)
6941 struct functable func[] = {
6942 {"list", rpc_printer_list},
6943 {"migrate", rpc_printer_migrate},
6944 {"driver", rpc_printer_driver_list},
6945 {"publish", rpc_printer_publish},
6950 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6951 rpc_printer_list_internals,
6954 return net_run_function(argc, argv, func, rpc_printer_usage);
6957 /****************************************************************************/
6961 * Basic usage function for 'net rpc'
6962 * @param argc Standard main() style argc
6963 * @param argv Standard main() style argv. Initial components are already
6967 int net_rpc_usage(int argc, const char **argv)
6969 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6970 d_printf(" net rpc join \t\t\tto join a domain \n");
6971 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
6972 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6973 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6974 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6975 d_printf(" net rpc group \t\tto list groups\n");
6976 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6977 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6978 d_printf(" net rpc file \t\t\tto list open files\n");
6979 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6980 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6981 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6982 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
6983 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6984 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6985 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6986 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6987 d_printf(" net rpc registry\t\tto manage registry hives\n");
6988 d_printf(" net rpc service\t\tto start, stop and query services\n");
6989 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6990 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6992 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6993 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6994 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6995 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6996 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
7002 * Help function for 'net rpc'. Calls command specific help if requested
7003 * or displays usage of net rpc
7004 * @param argc Standard main() style argc
7005 * @param argv Standard main() style argv. Initial components are already
7009 int net_rpc_help(int argc, const char **argv)
7011 struct functable func[] = {
7012 {"join", rpc_join_usage},
7013 {"user", rpc_user_usage},
7014 {"group", rpc_group_usage},
7015 {"share", rpc_share_usage},
7016 /*{"changetrustpw", rpc_changetrustpw_usage}, */
7017 {"trustdom", rpc_trustdom_usage},
7018 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
7019 /*{"shutdown", rpc_shutdown_usage}, */
7020 {"vampire", rpc_vampire_usage},
7025 net_rpc_usage(argc, argv);
7029 return (net_run_function(argc, argv, func, rpc_user_usage));
7033 * 'net rpc' entrypoint.
7034 * @param argc Standard main() style argc
7035 * @param argv Standard main() style argv. Initial components are already
7039 int net_rpc(int argc, const char **argv)
7041 struct functable func[] = {
7042 {"audit", net_rpc_audit},
7043 {"info", net_rpc_info},
7044 {"join", net_rpc_join},
7045 {"oldjoin", net_rpc_oldjoin},
7046 {"testjoin", net_rpc_testjoin},
7047 {"user", net_rpc_user},
7048 {"password", rpc_user_password},
7049 {"group", net_rpc_group},
7050 {"share", net_rpc_share},
7051 {"file", net_rpc_file},
7052 {"printer", net_rpc_printer},
7053 {"changetrustpw", net_rpc_changetrustpw},
7054 {"trustdom", rpc_trustdom},
7055 {"abortshutdown", rpc_shutdown_abort},
7056 {"shutdown", rpc_shutdown},
7057 {"samdump", rpc_samdump},
7058 {"vampire", rpc_vampire},
7059 {"getsid", net_rpc_getsid},
7060 {"rights", net_rpc_rights},
7061 {"service", net_rpc_service},
7062 {"registry", net_rpc_registry},
7063 {"shell", net_rpc_shell},
7064 {"help", net_rpc_help},
7067 return net_run_function(argc, argv, func, net_rpc_usage);