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)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "utils/net.h"
28 * @brief RPC based subcommands for the 'net' utility.
30 * This file should contain much of the functionality that used to
31 * be found in rpcclient, execpt that the commands should change
32 * less often, and the fucntionality should be sane (the user is not
33 * expected to know a rid/sid before they conduct an operation etc.)
35 * @todo Perhaps eventually these should be split out into a number
36 * of files, as this could get quite big.
41 * Many of the RPC functions need the domain sid. This function gets
42 * it at the start of every run
44 * @param cli A cli_state already connected to the remote machine
46 * @return The Domain SID of the remote machine.
49 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx, char **domain_name)
53 NTSTATUS result = NT_STATUS_OK;
54 uint32 info_class = 5;
56 if (!cli_nt_session_open (cli, PI_LSARPC)) {
57 fprintf(stderr, "could not initialise lsa pipe\n");
61 result = cli_lsa_open_policy(cli, mem_ctx, False,
62 SEC_RIGHTS_MAXIMUM_ALLOWED,
64 if (!NT_STATUS_IS_OK(result)) {
68 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
69 domain_name, &domain_sid);
70 if (!NT_STATUS_IS_OK(result)) {
72 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
74 if (!NT_STATUS_IS_OK(result)) {
75 fprintf(stderr, "error: %s\n", nt_errstr(result));
81 cli_lsa_close(cli, mem_ctx, &pol);
82 cli_nt_session_close(cli);
88 * Run a single RPC command, from start to finish.
90 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
91 * @param conn_flag a NET_FLAG_ combination. Passed to
92 * net_make_ipc_connection.
93 * @param argc Standard main() style argc
94 * @param argc Standard main() style argv. Initial components are already
96 * @return A shell status integer (0 for success)
99 int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
101 int argc, const char **argv)
103 struct cli_state *cli = NULL;
109 /* make use of cli_state handed over as an argument, if possible */
111 cli = net_make_ipc_connection(conn_flags);
121 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
122 DEBUG(0, ("talloc_init() failed\n"));
127 domain_sid = net_get_remote_domain_sid(cli, mem_ctx, &domain_name);
129 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
130 if (!cli_nt_session_open(cli, pipe_idx)) {
131 DEBUG(0, ("Could not initialise pipe\n"));
135 nt_status = fn(domain_sid, domain_name, cli, mem_ctx, argc, argv);
137 if (!NT_STATUS_IS_OK(nt_status)) {
138 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
140 DEBUG(5, ("rpc command function succedded\n"));
143 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
144 if (cli->nt_pipe_fnum[cli->pipe_idx])
145 cli_nt_session_close(cli);
148 /* close the connection only if it was opened here */
152 talloc_destroy(mem_ctx);
154 return (!NT_STATUS_IS_OK(nt_status));
158 /****************************************************************************/
162 * Force a change of the trust acccount password.
164 * All parameters are provided by the run_rpc_command function, except for
165 * argc, argv which are passes through.
167 * @param domain_sid The domain sid aquired from the remote server
168 * @param cli A cli_state connected to the server.
169 * @param mem_ctx Talloc context, destoyed on compleation of the function.
170 * @param argc Standard main() style argc
171 * @param argc Standard main() style argv. Initial components are already
174 * @return Normal NTSTATUS return.
177 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, const char *domain_name,
178 struct cli_state *cli, TALLOC_CTX *mem_ctx,
179 int argc, const char **argv) {
181 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
185 * Force a change of the trust acccount password.
187 * @param argc Standard main() style argc
188 * @param argc Standard main() style argv. Initial components are already
191 * @return A shell status integer (0 for success)
194 int net_rpc_changetrustpw(int argc, const char **argv)
196 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
197 rpc_changetrustpw_internals,
202 /****************************************************************************/
206 * Join a domain, the old way.
208 * This uses 'machinename' as the inital password, and changes it.
210 * The password should be created with 'server manager' or equiv first.
212 * All parameters are provided by the run_rpc_command function, except for
213 * argc, argv which are passes through.
215 * @param domain_sid The domain sid aquired from the remote server
216 * @param cli A cli_state connected to the server.
217 * @param mem_ctx Talloc context, destoyed on compleation of the function.
218 * @param argc Standard main() style argc
219 * @param argc Standard main() style argv. Initial components are already
222 * @return Normal NTSTATUS return.
225 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, const char *domain_name,
226 struct cli_state *cli,
228 int argc, const char **argv) {
230 fstring trust_passwd;
231 unsigned char orig_trust_passwd_hash[16];
233 uint32 sec_channel_type;
236 check what type of join - if the user want's to join as
237 a BDC, the server must agree that we are a BDC.
240 sec_channel_type = get_sec_channel_type(argv[0]);
242 sec_channel_type = get_sec_channel_type(NULL);
245 fstrcpy(trust_passwd, global_myname());
246 strlower_m(trust_passwd);
249 * Machine names can be 15 characters, but the max length on
250 * a password is 14. --jerry
253 trust_passwd[14] = '\0';
255 E_md4hash(trust_passwd, orig_trust_passwd_hash);
257 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
258 orig_trust_passwd_hash,
261 if (NT_STATUS_IS_OK(result))
262 printf("Joined domain %s.\n",opt_target_workgroup);
265 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
266 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
267 result = NT_STATUS_UNSUCCESSFUL;
274 * Join a domain, the old way.
276 * @param argc Standard main() style argc
277 * @param argc Standard main() style argv. Initial components are already
280 * @return A shell status integer (0 for success)
283 static int net_rpc_perform_oldjoin(int argc, const char **argv)
285 return run_rpc_command(NULL, PI_NETLOGON,
286 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
287 rpc_oldjoin_internals,
292 * Join a domain, the old way. This function exists to allow
293 * the message to be displayed when oldjoin was explicitly
294 * requested, but not when it was implied by "net rpc join"
296 * @param argc Standard main() style argc
297 * @param argc Standard main() style argv. Initial components are already
300 * @return A shell status integer (0 for success)
303 static int net_rpc_oldjoin(int argc, const char **argv)
305 int rc = net_rpc_perform_oldjoin(argc, argv);
308 d_printf("Failed to join domain\n");
315 * Basic usage function for 'net rpc join'
316 * @param argc Standard main() style argc
317 * @param argc Standard main() style argv. Initial components are already
321 static int rpc_join_usage(int argc, const char **argv)
323 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
324 "\t to join a domain with admin username & password\n"\
325 "\t\t password will be prompted if needed and none is specified\n"\
326 "\t <type> can be (default MEMBER)\n"\
327 "\t\t BDC - Join as a BDC\n"\
328 "\t\t PDC - Join as a PDC\n"\
329 "\t\t MEMBER - Join as a MEMBER server\n");
331 net_common_flags_usage(argc, argv);
336 * 'net rpc join' entrypoint.
337 * @param argc Standard main() style argc
338 * @param argc Standard main() style argv. Initial components are already
341 * Main 'net_rpc_join()' (where the admain username/password is used) is
343 * Try to just change the password, but if that doesn't work, use/prompt
344 * for a username/password.
347 int net_rpc_join(int argc, const char **argv)
349 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
352 return net_rpc_join_newstyle(argc, argv);
358 * display info about a rpc domain
360 * All parameters are provided by the run_rpc_command function, except for
361 * argc, argv which are passed through.
363 * @param domain_sid The domain sid acquired from the remote server
364 * @param cli A cli_state connected to the server.
365 * @param mem_ctx Talloc context, destoyed on completion of the function.
366 * @param argc Standard main() style argc
367 * @param argv Standard main() style argv. Initial components are already
370 * @return Normal NTSTATUS return.
374 rpc_info_internals(const DOM_SID *domain_sid, const char *domain_name,
375 struct cli_state *cli,
376 TALLOC_CTX *mem_ctx, int argc, const char **argv)
378 POLICY_HND connect_pol, domain_pol;
379 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
383 sid_to_string(sid_str, domain_sid);
385 /* Get sam policy handle */
386 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
388 if (!NT_STATUS_IS_OK(result)) {
392 /* Get domain policy handle */
393 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
394 MAXIMUM_ALLOWED_ACCESS,
395 domain_sid, &domain_pol);
396 if (!NT_STATUS_IS_OK(result)) {
401 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
403 if (NT_STATUS_IS_OK(result)) {
404 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
405 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
406 d_printf("Domain SID: %s\n", sid_str);
407 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num.low);
408 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
409 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
410 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
420 * 'net rpc info' entrypoint.
421 * @param argc Standard main() style argc
422 * @param argc Standard main() style argv. Initial components are already
425 int net_rpc_info(int argc, const char **argv)
427 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
434 * Fetch domain SID into the local secrets.tdb
436 * All parameters are provided by the run_rpc_command function, except for
437 * argc, argv which are passes through.
439 * @param domain_sid The domain sid acquired from the remote server
440 * @param cli A cli_state connected to the server.
441 * @param mem_ctx Talloc context, destoyed on completion of the function.
442 * @param argc Standard main() style argc
443 * @param argv Standard main() style argv. Initial components are already
446 * @return Normal NTSTATUS return.
450 rpc_getsid_internals(const DOM_SID *domain_sid, const char *domain_name,
451 struct cli_state *cli,
452 TALLOC_CTX *mem_ctx, int argc, const char **argv)
456 sid_to_string(sid_str, domain_sid);
457 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
458 sid_str, domain_name);
460 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
461 DEBUG(0,("Can't store domain SID\n"));
462 return NT_STATUS_UNSUCCESSFUL;
470 * 'net rpc getsid' entrypoint.
471 * @param argc Standard main() style argc
472 * @param argc Standard main() style argv. Initial components are already
475 int net_rpc_getsid(int argc, const char **argv)
477 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
478 rpc_getsid_internals,
483 /****************************************************************************/
486 * Basic usage function for 'net rpc user'
487 * @param argc Standard main() style argc.
488 * @param argv Standard main() style argv. Initial components are already
492 static int rpc_user_usage(int argc, const char **argv)
494 return net_help_user(argc, argv);
498 * Add a new user to a remote RPC server
500 * All parameters are provided by the run_rpc_command function, except for
501 * argc, argv which are passes through.
503 * @param domain_sid The domain sid acquired from the remote server
504 * @param cli A cli_state connected to the server.
505 * @param mem_ctx Talloc context, destoyed on completion of the function.
506 * @param argc Standard main() style argc
507 * @param argv Standard main() style argv. Initial components are already
510 * @return Normal NTSTATUS return.
513 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, const char *domain_name,
514 struct cli_state *cli, TALLOC_CTX *mem_ctx,
515 int argc, const char **argv) {
517 POLICY_HND connect_pol, domain_pol, user_pol;
518 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
519 const char *acct_name;
521 uint32 unknown, user_rid;
524 d_printf("User must be specified\n");
525 rpc_user_usage(argc, argv);
531 /* Get sam policy handle */
533 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
535 if (!NT_STATUS_IS_OK(result)) {
539 /* Get domain policy handle */
541 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
542 MAXIMUM_ALLOWED_ACCESS,
543 domain_sid, &domain_pol);
544 if (!NT_STATUS_IS_OK(result)) {
548 /* Create domain user */
550 acb_info = ACB_NORMAL;
551 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
553 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
554 acct_name, acb_info, unknown,
555 &user_pol, &user_rid);
556 if (!NT_STATUS_IS_OK(result)) {
561 if (!NT_STATUS_IS_OK(result)) {
562 d_printf("Failed to add user %s - %s\n", acct_name,
565 d_printf("Added user %s\n", acct_name);
571 * Add a new user to a remote RPC server
573 * @param argc Standard main() style argc
574 * @param argv Standard main() style argv. Initial components are already
577 * @return A shell status integer (0 for success)
580 static int rpc_user_add(int argc, const char **argv)
582 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
587 * Delete a user from a remote RPC server
589 * All parameters are provided by the run_rpc_command function, except for
590 * argc, argv which are passes through.
592 * @param domain_sid The domain sid acquired from the remote server
593 * @param cli A cli_state connected to the server.
594 * @param mem_ctx Talloc context, destoyed on completion of the function.
595 * @param argc Standard main() style argc
596 * @param argv Standard main() style argv. Initial components are already
599 * @return Normal NTSTATUS return.
602 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
603 const char *domain_name,
604 struct cli_state *cli,
606 int argc, const char **argv)
608 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
609 POLICY_HND connect_pol, domain_pol, user_pol;
612 d_printf("User must be specified\n");
613 rpc_user_usage(argc, argv);
616 /* Get sam policy and domain handles */
618 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
621 if (!NT_STATUS_IS_OK(result)) {
625 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
626 MAXIMUM_ALLOWED_ACCESS,
627 domain_sid, &domain_pol);
629 if (!NT_STATUS_IS_OK(result)) {
633 /* Get handle on user */
636 uint32 *user_rids, num_rids, *name_types;
637 uint32 flags = 0x000003e8; /* Unknown */
639 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
641 &num_rids, &user_rids,
644 if (!NT_STATUS_IS_OK(result)) {
648 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
649 MAXIMUM_ALLOWED_ACCESS,
650 user_rids[0], &user_pol);
652 if (!NT_STATUS_IS_OK(result)) {
659 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
661 if (!NT_STATUS_IS_OK(result)) {
665 /* Display results */
666 if (!NT_STATUS_IS_OK(result)) {
667 d_printf("Failed to delete user account - %s\n", nt_errstr(result));
669 d_printf("Deleted user account\n");
678 * Rename a user on a remote RPC server
680 * All parameters are provided by the run_rpc_command function, except for
681 * argc, argv which are passes through.
683 * @param domain_sid The domain sid acquired from the remote server
684 * @param cli A cli_state connected to the server.
685 * @param mem_ctx Talloc context, destoyed on completion of the function.
686 * @param argc Standard main() style argc
687 * @param argv Standard main() style argv. Initial components are already
690 * @return Normal NTSTATUS return.
693 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid, const char *domain_name,
694 struct cli_state *cli, TALLOC_CTX *mem_ctx,
695 int argc, const char **argv) {
697 POLICY_HND connect_pol, domain_pol, user_pol;
698 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
699 uint32 info_level = 7;
700 const char *old_name, *new_name;
702 uint32 flags = 0x000003e8; /* Unknown */
703 uint32 num_rids, *name_types;
704 uint32 num_names = 1;
706 SAM_USERINFO_CTR *user_ctr;
707 SAM_USERINFO_CTR ctr;
708 SAM_USER_INFO_7 info7;
711 d_printf("New and old username must be specified\n");
712 rpc_user_usage(argc, argv);
720 ZERO_STRUCT(user_ctr);
722 /* Get sam policy handle */
724 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
726 if (!NT_STATUS_IS_OK(result)) {
730 /* Get domain policy handle */
732 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
733 MAXIMUM_ALLOWED_ACCESS,
734 domain_sid, &domain_pol);
735 if (!NT_STATUS_IS_OK(result)) {
739 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
741 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
742 flags, num_names, names,
743 &num_rids, &user_rid, &name_types);
744 if (!NT_STATUS_IS_OK(result)) {
748 /* Open domain user */
749 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
750 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
752 if (!NT_STATUS_IS_OK(result)) {
756 /* Query user info */
757 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
758 info_level, &user_ctr);
760 if (!NT_STATUS_IS_OK(result)) {
764 ctr.switch_value = info_level;
765 ctr.info.id7 = &info7;
767 init_sam_user_info7(&info7, new_name);
770 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol,
771 info_level, &cli->user_session_key, &ctr);
773 if (!NT_STATUS_IS_OK(result)) {
778 if (!NT_STATUS_IS_OK(result)) {
779 d_printf("Failed to rename user from %s to %s - %s\n", old_name, new_name,
782 d_printf("Renamed user from %s to %s\n", old_name, new_name);
789 * Rename a user on a remote RPC server
791 * @param argc Standard main() style argc
792 * @param argv Standard main() style argv. Initial components are already
795 * @return A shell status integer (0 for success)
798 static int rpc_user_rename(int argc, const char **argv)
800 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
805 * Delete a user from a remote RPC server
807 * @param argc Standard main() style argc
808 * @param argv Standard main() style argv. Initial components are already
811 * @return A shell status integer (0 for success)
814 static int rpc_user_delete(int argc, const char **argv)
816 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
821 * Set a password for a user on a remote RPC server
823 * All parameters are provided by the run_rpc_command function, except for
824 * argc, argv which are passes through.
826 * @param domain_sid The domain sid acquired from the remote server
827 * @param cli A cli_state connected to the server.
828 * @param mem_ctx Talloc context, destoyed on completion of the function.
829 * @param argc Standard main() style argc
830 * @param argv Standard main() style argv. Initial components are already
833 * @return Normal NTSTATUS return.
836 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
837 const char *domain_name,
838 struct cli_state *cli,
840 int argc, const char **argv)
842 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
843 POLICY_HND connect_pol, domain_pol, user_pol;
844 SAM_USERINFO_CTR ctr;
845 SAM_USER_INFO_24 p24;
848 const char *new_password;
852 d_printf("User must be specified\n");
853 rpc_user_usage(argc, argv);
860 new_password = argv[1];
862 asprintf(&prompt, "Enter new password for %s:", user);
863 new_password = getpass(prompt);
867 /* Get sam policy and domain handles */
869 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
872 if (!NT_STATUS_IS_OK(result)) {
876 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
877 MAXIMUM_ALLOWED_ACCESS,
878 domain_sid, &domain_pol);
880 if (!NT_STATUS_IS_OK(result)) {
884 /* Get handle on user */
887 uint32 *user_rids, num_rids, *name_types;
888 uint32 flags = 0x000003e8; /* Unknown */
890 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
892 &num_rids, &user_rids,
895 if (!NT_STATUS_IS_OK(result)) {
899 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
900 MAXIMUM_ALLOWED_ACCESS,
901 user_rids[0], &user_pol);
903 if (!NT_STATUS_IS_OK(result)) {
908 /* Set password on account */
913 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
915 init_sam_user_info24(&p24, (char *)pwbuf,24);
917 ctr.switch_value = 24;
918 ctr.info.id24 = &p24;
920 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
921 &cli->user_session_key, &ctr);
923 if (!NT_STATUS_IS_OK(result)) {
927 /* Display results */
935 * Set a user's password on a remote RPC server
937 * @param argc Standard main() style argc
938 * @param argv Standard main() style argv. Initial components are already
941 * @return A shell status integer (0 for success)
944 static int rpc_user_password(int argc, const char **argv)
946 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
951 * List user's groups on a remote RPC server
953 * All parameters are provided by the run_rpc_command function, except for
954 * argc, argv which are passes through.
956 * @param domain_sid The domain sid acquired from the remote server
957 * @param cli A cli_state connected to the server.
958 * @param mem_ctx Talloc context, destoyed on completion of the function.
959 * @param argc Standard main() style argc
960 * @param argv Standard main() style argv. Initial components are already
963 * @return Normal NTSTATUS return.
967 rpc_user_info_internals(const DOM_SID *domain_sid, const char *domain_name,
968 struct cli_state *cli,
969 TALLOC_CTX *mem_ctx, int argc, const char **argv)
971 POLICY_HND connect_pol, domain_pol, user_pol;
972 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
973 uint32 *rids, num_rids, *name_types, num_names;
974 uint32 flags = 0x000003e8; /* Unknown */
980 d_printf("User must be specified\n");
981 rpc_user_usage(argc, argv);
984 /* Get sam policy handle */
986 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
988 if (!NT_STATUS_IS_OK(result)) goto done;
990 /* Get domain policy handle */
992 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
993 MAXIMUM_ALLOWED_ACCESS,
994 domain_sid, &domain_pol);
995 if (!NT_STATUS_IS_OK(result)) goto done;
997 /* Get handle on user */
999 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1001 &num_rids, &rids, &name_types);
1003 if (!NT_STATUS_IS_OK(result)) goto done;
1005 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1006 MAXIMUM_ALLOWED_ACCESS,
1007 rids[0], &user_pol);
1008 if (!NT_STATUS_IS_OK(result)) goto done;
1010 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
1011 &num_rids, &user_gids);
1013 if (!NT_STATUS_IS_OK(result)) goto done;
1018 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1020 for (i = 0; i < num_rids; i++)
1021 rids[i] = user_gids[i].g_rid;
1023 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
1025 &num_names, &names, &name_types);
1027 if (!NT_STATUS_IS_OK(result)) {
1031 /* Display results */
1033 for (i = 0; i < num_names; i++)
1034 printf("%s\n", names[i]);
1041 * List a user's groups from a remote RPC server
1043 * @param argc Standard main() style argc
1044 * @param argv Standard main() style argv. Initial components are already
1047 * @return A shell status integer (0 for success)
1050 static int rpc_user_info(int argc, const char **argv)
1052 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1057 * List users on a remote RPC server
1059 * All parameters are provided by the run_rpc_command function, except for
1060 * argc, argv which are passes through.
1062 * @param domain_sid The domain sid acquired from the remote server
1063 * @param cli A cli_state connected to the server.
1064 * @param mem_ctx Talloc context, destoyed on completion of the function.
1065 * @param argc Standard main() style argc
1066 * @param argv Standard main() style argv. Initial components are already
1069 * @return Normal NTSTATUS return.
1073 rpc_user_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1074 struct cli_state *cli,
1075 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1077 POLICY_HND connect_pol, domain_pol;
1078 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1079 uint32 start_idx=0, num_entries, i, loop_count = 0;
1080 SAM_DISPINFO_CTR ctr;
1081 SAM_DISPINFO_1 info1;
1083 /* Get sam policy handle */
1085 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1087 if (!NT_STATUS_IS_OK(result)) {
1091 /* Get domain policy handle */
1093 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1094 MAXIMUM_ALLOWED_ACCESS,
1095 domain_sid, &domain_pol);
1096 if (!NT_STATUS_IS_OK(result)) {
1100 /* Query domain users */
1103 ctr.sam.info1 = &info1;
1104 if (opt_long_list_entries)
1105 d_printf("\nUser name Comment"\
1106 "\n-----------------------------\n");
1109 uint32 max_entries, max_size;
1111 get_query_dispinfo_params(
1112 loop_count, &max_entries, &max_size);
1114 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1115 &start_idx, 1, &num_entries,
1116 max_entries, max_size, &ctr);
1119 for (i = 0; i < num_entries; i++) {
1120 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1121 if (opt_long_list_entries)
1122 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1124 if (opt_long_list_entries)
1125 printf("%-21.21s %s\n", user, desc);
1127 printf("%s\n", user);
1129 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1136 * 'net rpc user' entrypoint.
1137 * @param argc Standard main() style argc
1138 * @param argc Standard main() style argv. Initial components are already
1142 int net_rpc_user(int argc, const char **argv)
1144 struct functable func[] = {
1145 {"add", rpc_user_add},
1146 {"info", rpc_user_info},
1147 {"delete", rpc_user_delete},
1148 {"password", rpc_user_password},
1149 {"rename", rpc_user_rename},
1154 if (opt_long_list_entries) {
1157 return run_rpc_command(NULL,PI_SAMR, 0,
1158 rpc_user_list_internals,
1162 return net_run_function(argc, argv, func, rpc_user_usage);
1166 /****************************************************************************/
1169 * Basic usage function for 'net rpc group'
1170 * @param argc Standard main() style argc.
1171 * @param argv Standard main() style argv. Initial components are already
1175 static int rpc_group_usage(int argc, const char **argv)
1177 return net_help_group(argc, argv);
1181 * Delete group on a remote RPC server
1183 * All parameters are provided by the run_rpc_command function, except for
1184 * argc, argv which are passes through.
1186 * @param domain_sid The domain sid acquired from the remote server
1187 * @param cli A cli_state connected to the server.
1188 * @param mem_ctx Talloc context, destoyed on completion of the function.
1189 * @param argc Standard main() style argc
1190 * @param argv Standard main() style argv. Initial components are already
1193 * @return Normal NTSTATUS return.
1196 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1197 const char *domain_name,
1198 struct cli_state *cli,
1199 TALLOC_CTX *mem_ctx,
1200 int argc, const char **argv)
1202 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1203 BOOL group_is_primary = False;
1204 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1206 uint32 *group_rids, num_rids, *name_types, num_members,
1207 *group_attrs, group_rid;
1208 uint32 flags = 0x000003e8; /* Unknown */
1211 /* DOM_GID *user_gids; */
1212 SAM_USERINFO_CTR *user_ctr;
1216 d_printf("specify group\n");
1217 rpc_group_usage(argc,argv);
1218 return NT_STATUS_OK; /* ok? */
1221 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1224 if (!NT_STATUS_IS_OK(result)) {
1225 d_printf("Request samr_connect failed\n");
1229 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1230 MAXIMUM_ALLOWED_ACCESS,
1231 domain_sid, &domain_pol);
1233 if (!NT_STATUS_IS_OK(result)) {
1234 d_printf("Request open_domain failed\n");
1238 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1240 &num_rids, &group_rids,
1243 if (!NT_STATUS_IS_OK(result)) {
1244 d_printf("Lookup of '%s' failed\n",argv[0]);
1248 switch (name_types[0])
1250 case SID_NAME_DOM_GRP:
1251 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1252 MAXIMUM_ALLOWED_ACCESS,
1253 group_rids[0], &group_pol);
1254 if (!NT_STATUS_IS_OK(result)) {
1255 d_printf("Request open_group failed");
1259 group_rid = group_rids[0];
1261 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1262 &num_members, &group_rids,
1265 if (!NT_STATUS_IS_OK(result)) {
1266 d_printf("Unable to query group members of %s",argv[0]);
1271 d_printf("Domain Group %s (rid: %d) has %d members\n",
1272 argv[0],group_rid,num_members);
1275 /* Check if group is anyone's primary group */
1276 for (i = 0; i < num_members; i++)
1278 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1279 MAXIMUM_ALLOWED_ACCESS,
1280 group_rids[i], &user_pol);
1282 if (!NT_STATUS_IS_OK(result)) {
1283 d_printf("Unable to open group member %d\n",group_rids[i]);
1287 ZERO_STRUCT(user_ctr);
1289 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
1292 if (!NT_STATUS_IS_OK(result)) {
1293 d_printf("Unable to lookup userinfo for group member %d\n",group_rids[i]);
1297 if (user_ctr->info.id21->group_rid == group_rid) {
1298 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1301 d_printf("Group is primary group of %s\n",temp);
1302 group_is_primary = True;
1305 cli_samr_close(cli, mem_ctx, &user_pol);
1308 if (group_is_primary) {
1309 d_printf("Unable to delete group because some of it's "
1310 "members have it as primary group\n");
1311 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1315 /* remove all group members */
1316 for (i = 0; i < num_members; i++)
1319 d_printf("Remove group member %d...",group_rids[i]);
1320 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, group_rids[i]);
1322 if (NT_STATUS_IS_OK(result)) {
1327 d_printf("failed\n");
1332 result = cli_samr_delete_dom_group(cli, mem_ctx, &group_pol);
1335 /* removing a local group is easier... */
1336 case SID_NAME_ALIAS:
1337 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1338 MAXIMUM_ALLOWED_ACCESS,
1339 group_rids[0], &group_pol);
1341 if (!NT_STATUS_IS_OK(result)) {
1342 d_printf("Request open_alias failed\n");
1346 result = cli_samr_delete_dom_alias(cli, mem_ctx, &group_pol);
1349 d_printf("%s is of type %s. This command is only for deleting local or global groups\n",
1350 argv[0],sid_type_lookup(name_types[0]));
1351 result = NT_STATUS_UNSUCCESSFUL;
1356 if (NT_STATUS_IS_OK(result)) {
1358 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1360 d_printf("Deleting of %s failed: %s\n",argv[0],
1361 get_friendly_nt_error_msg(result));
1369 static int rpc_group_delete(int argc, const char **argv)
1371 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1376 rpc_group_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1377 struct cli_state *cli,
1378 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1380 POLICY_HND connect_pol, domain_pol, group_pol;
1381 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1382 GROUP_INFO_CTR group_info;
1385 d_printf("Group name must be specified\n");
1386 rpc_group_usage(argc, argv);
1387 return NT_STATUS_OK;
1390 /* Get sam policy handle */
1392 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1394 if (!NT_STATUS_IS_OK(result)) goto done;
1396 /* Get domain policy handle */
1398 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1399 MAXIMUM_ALLOWED_ACCESS,
1400 domain_sid, &domain_pol);
1401 if (!NT_STATUS_IS_OK(result)) goto done;
1403 /* Create the group */
1405 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1406 argv[0], MAXIMUM_ALLOWED_ACCESS,
1408 if (!NT_STATUS_IS_OK(result)) goto done;
1410 if (strlen(opt_comment) == 0) goto done;
1412 /* We've got a comment to set */
1414 group_info.switch_value1 = 4;
1415 init_samr_group_info4(&group_info.group.info4, opt_comment);
1417 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &group_info);
1418 if (!NT_STATUS_IS_OK(result)) goto done;
1421 if (NT_STATUS_IS_OK(result))
1422 DEBUG(5, ("add group succeeded\n"));
1424 d_printf("add group failed: %s\n", nt_errstr(result));
1430 rpc_alias_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1431 struct cli_state *cli,
1432 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1434 POLICY_HND connect_pol, domain_pol, alias_pol;
1435 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1436 ALIAS_INFO_CTR alias_info;
1439 d_printf("Alias name must be specified\n");
1440 rpc_group_usage(argc, argv);
1441 return NT_STATUS_OK;
1444 /* Get sam policy handle */
1446 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1448 if (!NT_STATUS_IS_OK(result)) goto done;
1450 /* Get domain policy handle */
1452 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1453 MAXIMUM_ALLOWED_ACCESS,
1454 domain_sid, &domain_pol);
1455 if (!NT_STATUS_IS_OK(result)) goto done;
1457 /* Create the group */
1459 result = cli_samr_create_dom_alias(cli, mem_ctx, &domain_pol,
1460 argv[0], &alias_pol);
1461 if (!NT_STATUS_IS_OK(result)) goto done;
1463 if (strlen(opt_comment) == 0) goto done;
1465 /* We've got a comment to set */
1467 alias_info.level = 3;
1468 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1470 result = cli_samr_set_aliasinfo(cli, mem_ctx, &alias_pol, &alias_info);
1471 if (!NT_STATUS_IS_OK(result)) goto done;
1474 if (NT_STATUS_IS_OK(result))
1475 DEBUG(5, ("add alias succeeded\n"));
1477 d_printf("add alias failed: %s\n", nt_errstr(result));
1482 static int rpc_group_add(int argc, const char **argv)
1485 return run_rpc_command(NULL, PI_SAMR, 0,
1486 rpc_alias_add_internals,
1489 return run_rpc_command(NULL, PI_SAMR, 0,
1490 rpc_group_add_internals,
1495 get_sid_from_name(struct cli_state *cli, TALLOC_CTX *mem_ctx, const char *name,
1496 DOM_SID *sid, enum SID_NAME_USE *type)
1498 int current_pipe = cli->pipe_idx;
1500 DOM_SID *sids = NULL;
1501 uint32 *types = NULL;
1503 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1505 if (current_pipe != PI_LSARPC) {
1507 if (current_pipe != -1)
1508 cli_nt_session_close(cli);
1510 if (!cli_nt_session_open(cli, PI_LSARPC))
1514 result = cli_lsa_open_policy(cli, mem_ctx, False,
1515 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1517 if (!NT_STATUS_IS_OK(result))
1520 result = cli_lsa_lookup_names(cli, mem_ctx, &lsa_pol, 1,
1521 &name, &sids, &types);
1523 if (NT_STATUS_IS_OK(result)) {
1524 sid_copy(sid, &sids[0]);
1528 cli_lsa_close(cli, mem_ctx, &lsa_pol);
1531 if (current_pipe != PI_LSARPC) {
1532 cli_nt_session_close(cli);
1533 if (current_pipe != -1)
1534 cli_nt_session_open(cli, current_pipe);
1537 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1539 /* Try as S-1-5-whatever */
1543 if (string_to_sid(&tmp_sid, name)) {
1544 sid_copy(sid, &tmp_sid);
1545 *type = SID_NAME_UNKNOWN;
1546 result = NT_STATUS_OK;
1554 rpc_add_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1555 const DOM_SID *group_sid, const char *member)
1557 POLICY_HND connect_pol, domain_pol;
1560 POLICY_HND group_pol;
1563 uint32 *rids = NULL;
1564 uint32 *rid_types = NULL;
1568 sid_copy(&sid, group_sid);
1570 if (!sid_split_rid(&sid, &group_rid))
1571 return NT_STATUS_UNSUCCESSFUL;
1573 /* Get sam policy handle */
1574 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1576 if (!NT_STATUS_IS_OK(result))
1579 /* Get domain policy handle */
1580 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1581 MAXIMUM_ALLOWED_ACCESS,
1583 if (!NT_STATUS_IS_OK(result))
1586 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1588 &num_rids, &rids, &rid_types);
1590 if (!NT_STATUS_IS_OK(result)) {
1591 d_printf("Could not lookup up group member %s\n", member);
1595 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1596 MAXIMUM_ALLOWED_ACCESS,
1597 group_rid, &group_pol);
1599 if (!NT_STATUS_IS_OK(result))
1602 result = cli_samr_add_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1605 cli_samr_close(cli, mem_ctx, &connect_pol);
1610 rpc_add_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1611 const DOM_SID *alias_sid, const char *member)
1613 POLICY_HND connect_pol, domain_pol;
1616 POLICY_HND alias_pol;
1619 enum SID_NAME_USE member_type;
1623 sid_copy(&sid, alias_sid);
1625 if (!sid_split_rid(&sid, &alias_rid))
1626 return NT_STATUS_UNSUCCESSFUL;
1628 result = get_sid_from_name(cli, mem_ctx, member,
1629 &member_sid, &member_type);
1631 if (!NT_STATUS_IS_OK(result)) {
1632 d_printf("Could not lookup up group member %s\n", member);
1636 /* Get sam policy handle */
1637 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1639 if (!NT_STATUS_IS_OK(result)) {
1643 /* Get domain policy handle */
1644 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1645 MAXIMUM_ALLOWED_ACCESS,
1647 if (!NT_STATUS_IS_OK(result)) {
1651 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1652 MAXIMUM_ALLOWED_ACCESS,
1653 alias_rid, &alias_pol);
1655 if (!NT_STATUS_IS_OK(result))
1658 result = cli_samr_add_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1660 if (!NT_STATUS_IS_OK(result))
1664 cli_samr_close(cli, mem_ctx, &connect_pol);
1669 rpc_group_addmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1670 struct cli_state *cli,
1671 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1674 enum SID_NAME_USE group_type;
1677 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
1678 return NT_STATUS_UNSUCCESSFUL;
1681 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1682 &group_sid, &group_type))) {
1683 d_printf("Could not lookup group name %s\n", argv[0]);
1684 return NT_STATUS_UNSUCCESSFUL;
1687 if (group_type == SID_NAME_DOM_GRP) {
1688 NTSTATUS result = rpc_add_groupmem(cli, mem_ctx,
1689 &group_sid, argv[1]);
1691 if (!NT_STATUS_IS_OK(result)) {
1692 d_printf("Could not add %s to %s: %s\n",
1693 argv[1], argv[0], nt_errstr(result));
1698 if (group_type == SID_NAME_ALIAS) {
1699 NTSTATUS result = rpc_add_aliasmem(cli, mem_ctx,
1700 &group_sid, argv[1]);
1702 if (!NT_STATUS_IS_OK(result)) {
1703 d_printf("Could not add %s to %s: %s\n",
1704 argv[1], argv[0], nt_errstr(result));
1709 d_printf("Can only add members to global or local groups which "
1710 "%s is not\n", argv[0]);
1712 return NT_STATUS_UNSUCCESSFUL;
1715 static int rpc_group_addmem(int argc, const char **argv)
1717 return run_rpc_command(NULL, PI_SAMR, 0,
1718 rpc_group_addmem_internals,
1723 rpc_del_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1724 const DOM_SID *group_sid, const char *member)
1726 POLICY_HND connect_pol, domain_pol;
1729 POLICY_HND group_pol;
1732 uint32 *rids = NULL;
1733 uint32 *rid_types = NULL;
1737 sid_copy(&sid, group_sid);
1739 if (!sid_split_rid(&sid, &group_rid))
1740 return NT_STATUS_UNSUCCESSFUL;
1742 /* Get sam policy handle */
1743 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1745 if (!NT_STATUS_IS_OK(result))
1748 /* Get domain policy handle */
1749 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1750 MAXIMUM_ALLOWED_ACCESS,
1752 if (!NT_STATUS_IS_OK(result))
1755 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1757 &num_rids, &rids, &rid_types);
1759 if (!NT_STATUS_IS_OK(result)) {
1760 d_printf("Could not lookup up group member %s\n", member);
1764 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1765 MAXIMUM_ALLOWED_ACCESS,
1766 group_rid, &group_pol);
1768 if (!NT_STATUS_IS_OK(result))
1771 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1774 cli_samr_close(cli, mem_ctx, &connect_pol);
1779 rpc_del_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1780 const DOM_SID *alias_sid, const char *member)
1782 POLICY_HND connect_pol, domain_pol;
1785 POLICY_HND alias_pol;
1788 enum SID_NAME_USE member_type;
1792 sid_copy(&sid, alias_sid);
1794 if (!sid_split_rid(&sid, &alias_rid))
1795 return NT_STATUS_UNSUCCESSFUL;
1797 result = get_sid_from_name(cli, mem_ctx, member,
1798 &member_sid, &member_type);
1800 if (!NT_STATUS_IS_OK(result)) {
1801 d_printf("Could not lookup up group member %s\n", member);
1805 /* Get sam policy handle */
1806 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1808 if (!NT_STATUS_IS_OK(result)) {
1812 /* Get domain policy handle */
1813 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1814 MAXIMUM_ALLOWED_ACCESS,
1816 if (!NT_STATUS_IS_OK(result)) {
1820 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1821 MAXIMUM_ALLOWED_ACCESS,
1822 alias_rid, &alias_pol);
1824 if (!NT_STATUS_IS_OK(result))
1827 result = cli_samr_del_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1829 if (!NT_STATUS_IS_OK(result))
1833 cli_samr_close(cli, mem_ctx, &connect_pol);
1838 rpc_group_delmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1839 struct cli_state *cli,
1840 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1843 enum SID_NAME_USE group_type;
1846 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
1847 return NT_STATUS_UNSUCCESSFUL;
1850 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1851 &group_sid, &group_type))) {
1852 d_printf("Could not lookup group name %s\n", argv[0]);
1853 return NT_STATUS_UNSUCCESSFUL;
1856 if (group_type == SID_NAME_DOM_GRP) {
1857 NTSTATUS result = rpc_del_groupmem(cli, mem_ctx,
1858 &group_sid, argv[1]);
1860 if (!NT_STATUS_IS_OK(result)) {
1861 d_printf("Could not del %s from %s: %s\n",
1862 argv[1], argv[0], nt_errstr(result));
1867 if (group_type == SID_NAME_ALIAS) {
1868 NTSTATUS result = rpc_del_aliasmem(cli, mem_ctx,
1869 &group_sid, argv[1]);
1871 if (!NT_STATUS_IS_OK(result)) {
1872 d_printf("Could not del %s from %s: %s\n",
1873 argv[1], argv[0], nt_errstr(result));
1878 d_printf("Can only delete members from global or local groups which "
1879 "%s is not\n", argv[0]);
1881 return NT_STATUS_UNSUCCESSFUL;
1884 static int rpc_group_delmem(int argc, const char **argv)
1886 return run_rpc_command(NULL, PI_SAMR, 0,
1887 rpc_group_delmem_internals,
1892 * List groups on a remote RPC server
1894 * All parameters are provided by the run_rpc_command function, except for
1895 * argc, argv which are passes through.
1897 * @param domain_sid The domain sid acquired from the remote server
1898 * @param cli A cli_state connected to the server.
1899 * @param mem_ctx Talloc context, destoyed on completion of the function.
1900 * @param argc Standard main() style argc
1901 * @param argv Standard main() style argv. Initial components are already
1904 * @return Normal NTSTATUS return.
1908 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1909 struct cli_state *cli,
1910 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1912 POLICY_HND connect_pol, domain_pol;
1913 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1914 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1915 struct acct_info *groups;
1916 BOOL global = False;
1918 BOOL builtin = False;
1926 for (i=0; i<argc; i++) {
1927 if (strequal(argv[i], "global"))
1930 if (strequal(argv[i], "local"))
1933 if (strequal(argv[i], "builtin"))
1937 /* Get sam policy handle */
1939 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1941 if (!NT_STATUS_IS_OK(result)) {
1945 /* Get domain policy handle */
1947 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1948 MAXIMUM_ALLOWED_ACCESS,
1949 domain_sid, &domain_pol);
1950 if (!NT_STATUS_IS_OK(result)) {
1954 /* Query domain groups */
1955 if (opt_long_list_entries)
1956 d_printf("\nGroup name Comment"\
1957 "\n-----------------------------\n");
1959 SAM_DISPINFO_CTR ctr;
1960 SAM_DISPINFO_3 info3;
1965 ctr.sam.info3 = &info3;
1969 get_query_dispinfo_params(
1970 loop_count, &max_entries, &max_size);
1972 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1973 &start_idx, 3, &num_entries,
1974 max_entries, max_size, &ctr);
1976 if (!NT_STATUS_IS_OK(result) &&
1977 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1980 for (i = 0; i < num_entries; i++) {
1982 fstring group, desc;
1984 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1985 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1987 if (opt_long_list_entries)
1988 printf("%-21.21s %-50.50s\n",
1991 printf("%s\n", group);
1993 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1994 /* query domain aliases */
1999 /* The max_size field in cli_samr_enum_als_groups is more like
2000 * an account_control field with indiviual bits what to
2001 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2002 * everything. I'm too lazy (sorry) to get this through to
2003 * rpc_parse/ etc. Volker */
2005 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
2007 &groups, &num_entries);
2009 if (!NT_STATUS_IS_OK(result) &&
2010 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2013 for (i = 0; i < num_entries; i++) {
2015 char *description = NULL;
2017 if (opt_long_list_entries) {
2019 POLICY_HND alias_pol;
2022 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
2027 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
2030 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
2032 description = unistr2_tdup(mem_ctx,
2033 ctr.alias.info3.description.string);
2037 if (description != NULL) {
2038 printf("%-21.21s %-50.50s\n",
2039 groups[i].acct_name,
2042 printf("%s\n", groups[i].acct_name);
2045 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2046 cli_samr_close(cli, mem_ctx, &domain_pol);
2047 /* Get builtin policy handle */
2049 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2050 MAXIMUM_ALLOWED_ACCESS,
2051 &global_sid_Builtin, &domain_pol);
2052 if (!NT_STATUS_IS_OK(result)) {
2055 /* query builtin aliases */
2058 if (!builtin) break;
2060 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
2061 &start_idx, max_entries,
2062 &groups, &num_entries);
2064 if (!NT_STATUS_IS_OK(result) &&
2065 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2068 for (i = 0; i < num_entries; i++) {
2070 char *description = NULL;
2072 if (opt_long_list_entries) {
2074 POLICY_HND alias_pol;
2077 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
2082 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
2085 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
2087 description = unistr2_tdup(mem_ctx,
2088 ctr.alias.info3.description.string);
2092 if (description != NULL) {
2093 printf("%-21.21s %-50.50s\n",
2094 groups[i].acct_name,
2097 printf("%s\n", groups[i].acct_name);
2100 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2106 static int rpc_group_list(int argc, const char **argv)
2108 return run_rpc_command(NULL, PI_SAMR, 0,
2109 rpc_group_list_internals,
2114 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2115 const char *domain_name, const DOM_SID *domain_sid,
2116 POLICY_HND *domain_pol, uint32 rid)
2119 POLICY_HND group_pol;
2120 uint32 num_members, *group_rids, *group_attrs;
2127 sid_to_string(sid_str, domain_sid);
2129 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
2130 MAXIMUM_ALLOWED_ACCESS,
2133 if (!NT_STATUS_IS_OK(result))
2136 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
2137 &num_members, &group_rids,
2140 if (!NT_STATUS_IS_OK(result))
2143 while (num_members > 0) {
2144 int this_time = 512;
2146 if (num_members < this_time)
2147 this_time = num_members;
2149 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol,
2150 this_time, group_rids,
2151 &num_names, &names, &name_types);
2153 if (!NT_STATUS_IS_OK(result))
2156 /* We only have users as members, but make the output
2157 the same as the output of alias members */
2159 for (i = 0; i < this_time; i++) {
2161 if (opt_long_list_entries) {
2162 printf("%s-%d %s\\%s %d\n", sid_str,
2163 group_rids[i], domain_name, names[i],
2166 printf("%s\\%s\n", domain_name, names[i]);
2170 num_members -= this_time;
2174 return NT_STATUS_OK;
2178 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2179 POLICY_HND *domain_pol, uint32 rid)
2182 POLICY_HND alias_pol, lsa_pol;
2184 DOM_SID *alias_sids;
2190 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
2191 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2193 if (!NT_STATUS_IS_OK(result))
2196 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
2197 &num_members, &alias_sids);
2199 if (!NT_STATUS_IS_OK(result)) {
2200 d_printf("Couldn't list alias members\n");
2204 if (num_members == 0) {
2205 return NT_STATUS_OK;
2208 cli_nt_session_close(cli);
2210 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2211 d_printf("Couldn't open LSA pipe\n");
2215 result = cli_lsa_open_policy(cli, mem_ctx, True,
2216 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2218 if (!NT_STATUS_IS_OK(result)) {
2219 d_printf("Couldn't open LSA policy handle\n");
2223 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
2225 &domains, &names, &types);
2227 if (!NT_STATUS_IS_OK(result) &&
2228 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2229 d_printf("Couldn't lookup SIDs\n");
2233 for (i = 0; i < num_members; i++) {
2235 sid_to_string(sid_str, &alias_sids[i]);
2237 if (opt_long_list_entries) {
2238 printf("%s %s\\%s %d\n", sid_str,
2239 domains[i] ? domains[i] : "*unknown*",
2240 names[i] ? names[i] : "*unknown*", types[i]);
2243 printf("%s\\%s\n", domains[i], names[i]);
2245 printf("%s\n", sid_str);
2249 return NT_STATUS_OK;
2253 rpc_group_members_internals(const DOM_SID *domain_sid,
2254 const char *domain_name,
2255 struct cli_state *cli,
2256 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2259 POLICY_HND connect_pol, domain_pol;
2260 uint32 num_rids, *rids, *rid_types;
2262 /* Get sam policy handle */
2264 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2267 if (!NT_STATUS_IS_OK(result))
2270 /* Get domain policy handle */
2272 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2273 MAXIMUM_ALLOWED_ACCESS,
2274 domain_sid, &domain_pol);
2276 if (!NT_STATUS_IS_OK(result))
2279 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2280 1, argv, &num_rids, &rids, &rid_types);
2282 if (!NT_STATUS_IS_OK(result)) {
2284 /* Ok, did not find it in the global sam, try with builtin */
2286 DOM_SID sid_Builtin;
2288 cli_samr_close(cli, mem_ctx, &domain_pol);
2290 string_to_sid(&sid_Builtin, "S-1-5-32");
2292 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2293 MAXIMUM_ALLOWED_ACCESS,
2294 &sid_Builtin, &domain_pol);
2296 if (!NT_STATUS_IS_OK(result)) {
2297 d_printf("Couldn't find group %s\n", argv[0]);
2301 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2305 if (!NT_STATUS_IS_OK(result)) {
2306 d_printf("Couldn't find group %s\n", argv[0]);
2311 if (num_rids != 1) {
2312 d_printf("Couldn't find group %s\n", argv[0]);
2316 if (rid_types[0] == SID_NAME_DOM_GRP) {
2317 return rpc_list_group_members(cli, mem_ctx, domain_name,
2318 domain_sid, &domain_pol,
2322 if (rid_types[0] == SID_NAME_ALIAS) {
2323 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
2327 return NT_STATUS_NO_SUCH_GROUP;
2330 static int rpc_group_members(int argc, const char **argv)
2333 return rpc_group_usage(argc, argv);
2336 return run_rpc_command(NULL, PI_SAMR, 0,
2337 rpc_group_members_internals,
2342 rpc_group_rename_internals(const DOM_SID *domain_sid,
2343 const char *domain_name,
2344 struct cli_state *cli,
2345 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2348 POLICY_HND connect_pol, domain_pol, group_pol;
2349 uint32 num_rids, *rids, *rid_types;
2353 d_printf("Usage: 'net rpc group rename group newname'\n");
2354 return NT_STATUS_UNSUCCESSFUL;
2357 /* Get sam policy handle */
2359 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2362 if (!NT_STATUS_IS_OK(result))
2365 /* Get domain policy handle */
2367 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2368 MAXIMUM_ALLOWED_ACCESS,
2369 domain_sid, &domain_pol);
2371 if (!NT_STATUS_IS_OK(result))
2374 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2375 1, argv, &num_rids, &rids, &rid_types);
2377 if (num_rids != 1) {
2378 d_printf("Couldn't find group %s\n", argv[0]);
2382 if (rid_types[0] != SID_NAME_DOM_GRP) {
2383 d_printf("Can only rename domain groups\n");
2384 return NT_STATUS_UNSUCCESSFUL;
2387 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
2388 MAXIMUM_ALLOWED_ACCESS,
2389 rids[0], &group_pol);
2391 if (!NT_STATUS_IS_OK(result))
2396 ctr.switch_value1 = 2;
2397 init_samr_group_info2(&ctr.group.info2, argv[1]);
2399 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &ctr);
2401 if (!NT_STATUS_IS_OK(result))
2404 return NT_STATUS_NO_SUCH_GROUP;
2407 static int rpc_group_rename(int argc, const char **argv)
2410 return rpc_group_usage(argc, argv);
2413 return run_rpc_command(NULL, PI_SAMR, 0,
2414 rpc_group_rename_internals,
2419 * 'net rpc group' entrypoint.
2420 * @param argc Standard main() style argc
2421 * @param argc Standard main() style argv. Initial components are already
2425 int net_rpc_group(int argc, const char **argv)
2427 struct functable func[] = {
2428 {"add", rpc_group_add},
2429 {"delete", rpc_group_delete},
2430 {"addmem", rpc_group_addmem},
2431 {"delmem", rpc_group_delmem},
2432 {"list", rpc_group_list},
2433 {"members", rpc_group_members},
2434 {"rename", rpc_group_rename},
2439 if (opt_long_list_entries) {
2442 return run_rpc_command(NULL, PI_SAMR, 0,
2443 rpc_group_list_internals,
2447 return net_run_function(argc, argv, func, rpc_group_usage);
2450 /****************************************************************************/
2452 static int rpc_share_usage(int argc, const char **argv)
2454 return net_help_share(argc, argv);
2458 * Add a share on a remote RPC server
2460 * All parameters are provided by the run_rpc_command function, except for
2461 * argc, argv which are passes through.
2463 * @param domain_sid The domain sid acquired from the remote server
2464 * @param cli A cli_state connected to the server.
2465 * @param mem_ctx Talloc context, destoyed on completion of the function.
2466 * @param argc Standard main() style argc
2467 * @param argv Standard main() style argv. Initial components are already
2470 * @return Normal NTSTATUS return.
2473 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
2474 struct cli_state *cli,
2475 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2478 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2480 uint32 type=0; /* only allow disk shares to be added */
2481 uint32 num_users=0, perms=0;
2482 char *password=NULL; /* don't allow a share password */
2485 path = strchr(sharename, '=');
2487 return NT_STATUS_UNSUCCESSFUL;
2490 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
2491 opt_comment, perms, opt_maxusers,
2492 num_users, path, password,
2494 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2497 static int rpc_share_add(int argc, const char **argv)
2499 if ((argc < 1) || !strchr(argv[0], '=')) {
2500 DEBUG(1,("Sharename or path not specified on add\n"));
2501 return rpc_share_usage(argc, argv);
2503 return run_rpc_command(NULL, PI_SRVSVC, 0,
2504 rpc_share_add_internals,
2509 * Delete a share on a remote RPC server
2511 * All parameters are provided by the run_rpc_command function, except for
2512 * argc, argv which are passes through.
2514 * @param domain_sid The domain sid acquired from the remote server
2515 * @param cli A cli_state connected to the server.
2516 * @param mem_ctx Talloc context, destoyed on completion of the function.
2517 * @param argc Standard main() style argc
2518 * @param argv Standard main() style argv. Initial components are already
2521 * @return Normal NTSTATUS return.
2524 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name,
2525 struct cli_state *cli,
2526 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2530 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
2531 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2535 * Delete a share on a remote RPC server
2537 * @param domain_sid The domain sid acquired from the remote server
2538 * @param argc Standard main() style argc
2539 * @param argv Standard main() style argv. Initial components are already
2542 * @return A shell status integer (0 for success)
2544 static int rpc_share_delete(int argc, const char **argv)
2547 DEBUG(1,("Sharename not specified on delete\n"));
2548 return rpc_share_usage(argc, argv);
2550 return run_rpc_command(NULL, PI_SRVSVC, 0,
2551 rpc_share_del_internals,
2556 * Formatted print of share info
2558 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2561 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
2563 fstring netname = "", remark = "";
2565 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
2566 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
2568 if (opt_long_list_entries) {
2569 d_printf("%-12s %-8.8s %-50s\n",
2570 netname, share_type[info1->info_1.type], remark);
2572 d_printf("%s\n", netname);
2578 * List shares on a remote RPC server
2580 * All parameters are provided by the run_rpc_command function, except for
2581 * argc, argv which are passes through.
2583 * @param domain_sid The domain sid acquired from the remote server
2584 * @param cli A cli_state connected to the server.
2585 * @param mem_ctx Talloc context, destoyed on completion of the function.
2586 * @param argc Standard main() style argc
2587 * @param argv Standard main() style argv. Initial components are already
2590 * @return Normal NTSTATUS return.
2594 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
2595 struct cli_state *cli,
2596 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2598 SRV_SHARE_INFO_CTR ctr;
2601 uint32 preferred_len = 0xffffffff, i;
2603 init_enum_hnd(&hnd, 0);
2605 result = cli_srvsvc_net_share_enum(
2606 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
2608 if (!W_ERROR_IS_OK(result))
2611 /* Display results */
2613 if (opt_long_list_entries) {
2615 "\nEnumerating shared resources (exports) on remote server:\n\n"\
2616 "\nShare name Type Description\n"\
2617 "---------- ---- -----------\n");
2619 for (i = 0; i < ctr.num_entries; i++)
2620 display_share_info_1(&ctr.share.info1[i]);
2622 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2626 * Migrate shares from a remote RPC server to the local RPC srever
2628 * All parameters are provided by the run_rpc_command function, except for
2629 * argc, argv which are passes through.
2631 * @param domain_sid The domain sid acquired from the remote server
2632 * @param cli A cli_state connected to the server.
2633 * @param mem_ctx Talloc context, destoyed on completion of the function.
2634 * @param argc Standard main() style argc
2635 * @param argv Standard main() style argv. Initial components are already
2638 * @return Normal NTSTATUS return.
2641 rpc_share_migrate_shares_internals(const DOM_SID *domain_sid, const char *domain_name,
2642 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2643 int argc, const char **argv)
2646 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2647 SRV_SHARE_INFO_CTR ctr_src;
2649 uint32 type = 0; /* only allow disk shares to be added */
2650 uint32 num_uses = 0, perms = 0, max_uses = 0;
2651 char *password = NULL; /* don't allow a share password */
2652 uint32 preferred_len = 0xffffffff, i;
2653 BOOL got_dst_srvsvc_pipe = False;
2654 struct cli_state *cli_dst = NULL;
2655 uint32 level = 502; /* includes secdesc */
2656 SEC_DESC *share_sd = NULL;
2658 init_enum_hnd(&hnd, 0);
2660 result = cli_srvsvc_net_share_enum(
2661 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2662 if (!W_ERROR_IS_OK(result))
2665 /* connect local PI_SRVSVC */
2666 nt_status = connect_pipe(&cli_dst, PI_SRVSVC, &got_dst_srvsvc_pipe);
2667 if (!NT_STATUS_IS_OK(nt_status))
2671 for (i = 0; i < ctr_src.num_entries; i++) {
2673 fstring netname = "", remark = "", path = "";
2674 /* reset error-code */
2675 nt_status = NT_STATUS_UNSUCCESSFUL;
2677 rpcstr_pull_unistr2_fstring(
2678 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
2679 rpcstr_pull_unistr2_fstring(
2680 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
2681 rpcstr_pull_unistr2_fstring(
2682 path, &ctr_src.share.info502[i].info_502_str.uni_path);
2683 num_uses = ctr_src.share.info502[i].info_502.num_uses;
2684 max_uses = ctr_src.share.info502[i].info_502.max_uses;
2685 perms = ctr_src.share.info502[i].info_502.perms;
2689 share_sd = dup_sec_desc(
2690 mem_ctx, ctr_src.share.info502[i].info_502_str.sd);
2692 /* since we do not have NetShareGetInfo implemented in samba3 we
2693 only can skip inside the enum-ctr_src */
2695 char *one_share = talloc_strdup(mem_ctx, argv[0]);
2696 if (!strequal(netname, one_share))
2700 /* skip builtin shares */
2701 /* FIXME: should print$ be added too ? */
2702 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
2703 strequal(netname,"global"))
2706 if (opt_exclude && in_list(netname, opt_exclude, False)) {
2707 printf("excluding [%s]\n", netname);
2711 /* only work with file-shares */
2712 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2713 d_printf("skipping [%s]: not a file share.\n", netname);
2721 /* finallly add the share on the dst server
2722 please note that samba currently does not allow to
2723 add a share without existing directory */
2725 printf("migrating: [%s], path: %s, comment: %s, %s share-ACLs\n",
2726 netname, path, remark, opt_acls ? "including" : "without" );
2728 if (opt_verbose && opt_acls)
2729 display_sec_desc(share_sd);
2731 result = cli_srvsvc_net_share_add(cli_dst, mem_ctx, netname, type,
2732 remark, perms, max_uses,
2733 num_uses, path, password,
2736 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
2737 printf(" [%s] does already exist\n", netname);
2741 if (!W_ERROR_IS_OK(result)) {
2742 printf("cannot add share: %s\n", dos_errstr(result));
2748 nt_status = NT_STATUS_OK;
2751 if (got_dst_srvsvc_pipe) {
2752 cli_nt_session_close(cli_dst);
2753 cli_shutdown(cli_dst);
2761 * Migrate shares from a rpc-server to another
2763 * @param argc Standard main() style argc
2764 * @param argv Standard main() style argv. Initial components are already
2767 * @return A shell status integer (0 for success)
2769 static int rpc_share_migrate_shares(int argc, const char **argv)
2773 printf("no server to migrate\n");
2777 return run_rpc_command(NULL, PI_SRVSVC, 0,
2778 rpc_share_migrate_shares_internals,
2782 typedef struct copy_clistate {
2783 TALLOC_CTX *mem_ctx;
2784 struct cli_state *cli_share_src;
2785 struct cli_state *cli_share_dst;
2793 * @param f file_info
2794 * @param mask current search mask
2795 * @param state arg-pointer
2798 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
2800 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2801 struct copy_clistate *local_state = (struct copy_clistate *)state;
2802 fstring filename, new_mask, dir;
2804 if (strequal(f->name, ".") || strequal(f->name, ".."))
2807 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
2810 if (f->mode & aDIR) {
2812 DEBUG(3,("got dir: %s\n", f->name));
2814 fstrcpy(dir, local_state->cwd);
2816 fstrcat(dir, f->name);
2818 /* create that directory */
2819 nt_status = net_copy_file(local_state->mem_ctx,
2820 local_state->cli_share_src,
2821 local_state->cli_share_dst,
2823 opt_acls? True : False,
2824 opt_attrs? True : False,
2825 opt_timestamps? True : False,
2828 if (!NT_STATUS_IS_OK(nt_status))
2829 printf("could not copy dir %s: %s\n",
2830 dir, nt_errstr(nt_status));
2832 /* search below that directory */
2833 fstrcpy(new_mask, dir);
2834 fstrcat(new_mask, "\\*");
2836 if (!sync_files(local_state->mem_ctx,
2837 local_state->cli_share_src,
2838 local_state->cli_share_dst,
2841 printf("could not sync files\n");
2848 fstrcpy(filename, local_state->cwd);
2849 fstrcat(filename, "\\");
2850 fstrcat(filename, f->name);
2852 DEBUG(3,("got file: %s\n", filename));
2854 nt_status = net_copy_file(local_state->mem_ctx,
2855 local_state->cli_share_src,
2856 local_state->cli_share_dst,
2858 opt_acls? True : False,
2859 opt_attrs? True : False,
2860 opt_timestamps? True: False,
2863 if (!NT_STATUS_IS_OK(nt_status))
2864 printf("could not copy file %s: %s\n",
2865 filename, nt_errstr(nt_status));
2870 * sync files, can be called recursivly to list files
2871 * and then call copy_fn for each file
2873 * @param mem_ctx TALLOC_CTX
2874 * @param cli_share_src a connected share on the originating server
2875 * @param cli_share_dst a connected share on the destination server
2876 * @param mask the current search mask
2877 * @param cwd the current path
2879 * @return Boolean result
2881 BOOL sync_files(TALLOC_CTX *mem_ctx,
2882 struct cli_state *cli_share_src,
2883 struct cli_state *cli_share_dst,
2884 pstring mask, fstring cwd)
2888 uint16 attribute = aSYSTEM | aHIDDEN | aDIR;
2889 struct copy_clistate clistate;
2891 clistate.mem_ctx = mem_ctx;
2892 clistate.cli_share_src = cli_share_src;
2893 clistate.cli_share_dst = cli_share_dst;
2896 DEBUG(3,("calling cli_list with mask: %s\n", mask));
2898 if (cli_list(cli_share_src, mask, attribute, copy_fn, &clistate) == -1) {
2899 d_printf("listing %s failed with error: %s\n",
2900 mask, cli_errstr(cli_share_src));
2909 * Sync all files inside a remote share to another share (over smb)
2911 * All parameters are provided by the run_rpc_command function, except for
2912 * argc, argv which are passes through.
2914 * @param domain_sid The domain sid acquired from the remote server
2915 * @param cli A cli_state connected to the server.
2916 * @param mem_ctx Talloc context, destoyed on completion of the function.
2917 * @param argc Standard main() style argc
2918 * @param argv Standard main() style argv. Initial components are already
2921 * @return Normal NTSTATUS return.
2924 rpc_share_migrate_files_internals(const DOM_SID *domain_sid, const char *domain_name,
2925 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2926 int argc, const char **argv)
2929 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2930 SRV_SHARE_INFO_CTR ctr_src;
2932 uint32 preferred_len = 0xffffffff, i;
2934 struct cli_state *cli_share_src = NULL;
2935 struct cli_state *cli_share_dst = NULL;
2936 BOOL got_src_share = False;
2937 BOOL got_dst_share = False;
2941 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
2943 init_enum_hnd(&hnd, 0);
2945 result = cli_srvsvc_net_share_enum(
2946 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2948 if (!W_ERROR_IS_OK(result))
2951 for (i = 0; i < ctr_src.num_entries; i++) {
2953 fstring netname = "", remark = "", path = "";
2955 rpcstr_pull_unistr2_fstring(
2956 netname, &ctr_src.share.info2[i].info_2_str.uni_netname);
2957 rpcstr_pull_unistr2_fstring(
2958 remark, &ctr_src.share.info2[i].info_2_str.uni_remark);
2959 rpcstr_pull_unistr2_fstring(
2960 path, &ctr_src.share.info2[i].info_2_str.uni_path);
2962 /* since we do not have NetShareGetInfo implemented in samba3 we
2963 only can skip inside the enum-ctr_src */
2965 char *one_share = talloc_strdup(mem_ctx, argv[0]);
2966 if (!strequal(netname, one_share))
2970 /* skip builtin and hidden shares
2971 In particular, one might not want to mirror whole discs :) */
2972 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$"))
2975 if (strequal(netname, "print$") || netname[1] == '$') {
2976 d_printf("skipping [%s]: builtin/hidden share\n", netname);
2980 if (opt_exclude && in_list(netname, opt_exclude, False)) {
2981 printf("excluding [%s]\n", netname);
2985 /* only work with file-shares */
2986 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2987 d_printf("skipping [%s]: not a file share.\n", netname);
2992 return NT_STATUS_UNSUCCESSFUL;
2994 printf("syncing [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
2996 opt_acls ? "including" : "without",
2997 opt_attrs ? "including" : "without",
2998 opt_timestamps ? "(preserving timestamps)" : "");
3001 /* open share source */
3002 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
3003 cli->desthost, netname, "A:");
3004 if (!NT_STATUS_IS_OK(nt_status))
3007 got_src_share = True;
3010 /* open share destination */
3011 nt_status = connect_to_service(&cli_share_dst, NULL,
3012 dst, netname, "A:");
3013 if (!NT_STATUS_IS_OK(nt_status))
3016 got_dst_share = True;
3019 /* now call the filesync */
3020 pstrcpy(mask, "\\*");
3022 if (!sync_files(mem_ctx, cli_share_src, cli_share_dst, mask, NULL)) {
3023 d_printf("could not sync files for share: %s\n", netname);
3024 nt_status = NT_STATUS_UNSUCCESSFUL;
3030 nt_status = NT_STATUS_OK;
3035 cli_shutdown(cli_share_src);
3038 cli_shutdown(cli_share_dst);
3044 static int rpc_share_migrate_files(int argc, const char **argv)
3048 printf("no server to migrate\n");
3052 return run_rpc_command(NULL, PI_SRVSVC, 0,
3053 rpc_share_migrate_files_internals,
3058 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3059 * from one server to another
3061 * @param argc Standard main() style argc
3062 * @param argv Standard main() style argv. Initial components are already
3065 * @return A shell status integer (0 for success)
3068 static int rpc_share_migrate_all(int argc, const char **argv)
3073 printf("no server to migrate\n");
3077 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3081 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_security_internals, argc, argv);
3085 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3090 * 'net rpc share migrate' entrypoint.
3091 * @param argc Standard main() style argc
3092 * @param argv Standard main() style argv. Initial components are already
3095 static int rpc_share_migrate(int argc, const char **argv)
3098 struct functable func[] = {
3099 {"all", rpc_share_migrate_all},
3100 {"files", rpc_share_migrate_files},
3101 {"help", rpc_share_usage},
3102 /* {"security", rpc_share_migrate_security},*/
3103 {"shares", rpc_share_migrate_shares},
3107 return net_run_function(argc, argv, func, rpc_share_usage);
3116 static int num_server_aliases;
3117 static struct full_alias *server_aliases;
3120 * Add an alias to the static list.
3122 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3124 if (server_aliases == NULL)
3125 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3127 server_aliases[num_server_aliases] = *alias;
3128 num_server_aliases += 1;
3132 * For a specific domain on the server, fetch all the aliases
3133 * and their members. Add all of them to the server_aliases.
3136 rpc_fetch_domain_aliases(struct cli_state *cli, TALLOC_CTX *mem_ctx,
3137 POLICY_HND *connect_pol,
3138 const DOM_SID *domain_sid)
3140 uint32 start_idx, max_entries, num_entries, i;
3141 struct acct_info *groups;
3143 POLICY_HND domain_pol;
3145 /* Get domain policy handle */
3147 result = cli_samr_open_domain(cli, mem_ctx, connect_pol,
3148 MAXIMUM_ALLOWED_ACCESS,
3149 domain_sid, &domain_pol);
3150 if (!NT_STATUS_IS_OK(result))
3157 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
3158 &start_idx, max_entries,
3159 &groups, &num_entries);
3161 for (i = 0; i < num_entries; i++) {
3163 POLICY_HND alias_pol;
3164 struct full_alias alias;
3168 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
3169 MAXIMUM_ALLOWED_ACCESS,
3172 if (!NT_STATUS_IS_OK(result))
3175 result = cli_samr_query_aliasmem(cli, mem_ctx,
3179 if (!NT_STATUS_IS_OK(result))
3182 result = cli_samr_close(cli, mem_ctx, &alias_pol);
3183 if (!NT_STATUS_IS_OK(result))
3186 alias.members = NULL;
3188 if (alias.num_members > 0) {
3189 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3191 for (j = 0; j < alias.num_members; j++)
3192 sid_copy(&alias.members[j],
3196 sid_copy(&alias.sid, domain_sid);
3197 sid_append_rid(&alias.sid, groups[i].rid);
3199 push_alias(mem_ctx, &alias);
3201 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3203 result = NT_STATUS_OK;
3206 cli_samr_close(cli, mem_ctx, &domain_pol);
3212 * Dump server_aliases as names for debugging purposes.
3215 rpc_aliaslist_dump(const DOM_SID *domain_sid, const char *domain_name,
3216 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3217 int argc, const char **argv)
3223 result = cli_lsa_open_policy(cli, mem_ctx, True,
3224 SEC_RIGHTS_MAXIMUM_ALLOWED,
3226 if (!NT_STATUS_IS_OK(result))
3229 for (i=0; i<num_server_aliases; i++) {
3235 struct full_alias *alias = &server_aliases[i];
3237 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, 1,
3239 &domains, &names, &types);
3240 if (!NT_STATUS_IS_OK(result))
3243 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3245 if (alias->num_members == 0) {
3250 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol,
3253 &domains, &names, &types);
3255 if (!NT_STATUS_IS_OK(result) &&
3256 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3259 for (j=0; j<alias->num_members; j++)
3260 DEBUG(1, ("%s\\%s (%d); ",
3261 domains[j] ? domains[j] : "*unknown*",
3262 names[j] ? names[j] : "*unknown*",types[j]));
3266 cli_lsa_close(cli, mem_ctx, &lsa_pol);
3268 return NT_STATUS_OK;
3272 * Fetch a list of all server aliases and their members into
3276 rpc_aliaslist_internals(const DOM_SID *domain_sid, const char *domain_name,
3277 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3278 int argc, const char **argv)
3281 POLICY_HND connect_pol;
3283 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
3286 if (!NT_STATUS_IS_OK(result))
3289 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3290 &global_sid_Builtin);
3292 if (!NT_STATUS_IS_OK(result))
3295 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3298 cli_samr_close(cli, mem_ctx, &connect_pol);
3303 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3305 token->num_sids = 4;
3307 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
3309 token->user_sids[0] = *user_sid;
3310 sid_copy(&token->user_sids[1], &global_sid_World);
3311 sid_copy(&token->user_sids[2], &global_sid_Network);
3312 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3315 static void free_user_token(NT_USER_TOKEN *token)
3317 SAFE_FREE(token->user_sids);
3320 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
3324 for (i=0; i<token->num_sids; i++) {
3325 if (sid_compare(sid, &token->user_sids[i]) == 0)
3331 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3333 if (is_sid_in_token(token, sid))
3336 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3338 sid_copy(&token->user_sids[token->num_sids], sid);
3340 token->num_sids += 1;
3345 NT_USER_TOKEN token;
3348 static void dump_user_token(struct user_token *token)
3352 d_printf("%s\n", token->name);
3354 for (i=0; i<token->token.num_sids; i++) {
3355 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
3359 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
3363 for (i=0; i<alias->num_members; i++) {
3364 if (sid_compare(sid, &alias->members[i]) == 0)
3371 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
3375 for (i=0; i<num_server_aliases; i++) {
3376 if (is_alias_member(&sid, &server_aliases[i]))
3377 add_sid_to_token(token, &server_aliases[i].sid);
3382 * We got a user token with all the SIDs we can know about without asking the
3383 * server directly. These are the user and domain group sids. All of these can
3384 * be members of aliases. So scan the list of aliases for each of the SIDs and
3385 * add them to the token.
3388 static void collect_alias_memberships(NT_USER_TOKEN *token)
3390 int num_global_sids = token->num_sids;
3393 for (i=0; i<num_global_sids; i++) {
3394 collect_sid_memberships(token, token->user_sids[i]);
3398 static BOOL get_user_sids(const char *domain, const char *user,
3399 NT_USER_TOKEN *token)
3401 struct winbindd_request request;
3402 struct winbindd_response response;
3410 fstr_sprintf(full_name, "%s%c%s",
3411 domain, *lp_winbind_separator(), user);
3413 /* First let's find out the user sid */
3415 ZERO_STRUCT(request);
3416 ZERO_STRUCT(response);
3418 fstrcpy(request.data.name.dom_name, domain);
3419 fstrcpy(request.data.name.name, user);
3421 result = winbindd_request(WINBINDD_LOOKUPNAME, &request, &response);
3423 if (result != NSS_STATUS_SUCCESS) {
3424 DEBUG(1, ("winbind could not find %s\n", full_name));
3428 if (response.data.sid.type != SID_NAME_USER) {
3429 DEBUG(1, ("%s is not a user\n", full_name));
3433 string_to_sid(&user_sid, response.data.sid.sid);
3435 init_user_token(token, &user_sid);
3437 /* And now the groups winbind knows about */
3439 ZERO_STRUCT(response);
3441 fstrcpy(request.data.username, full_name);
3443 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
3445 if (result != NSS_STATUS_SUCCESS) {
3446 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
3450 for (i = 0; i < response.data.num_entries; i++) {
3451 gid_t gid = ((gid_t *)response.extra_data)[i];
3454 struct winbindd_request sidrequest;
3455 struct winbindd_response sidresponse;
3457 ZERO_STRUCT(sidrequest);
3458 ZERO_STRUCT(sidresponse);
3460 sidrequest.data.gid = gid;
3462 result = winbindd_request(WINBINDD_GID_TO_SID,
3463 &sidrequest, &sidresponse);
3465 if (result != NSS_STATUS_SUCCESS) {
3466 DEBUG(1, ("winbind could not find SID of gid %d\n",
3471 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
3473 string_to_sid(&sid, sidresponse.data.sid.sid);
3474 add_sid_to_token(token, &sid);
3477 SAFE_FREE(response.extra_data);
3483 * Get a list of all user tokens we want to look at
3485 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
3487 struct winbindd_request request;
3488 struct winbindd_response response;
3489 const char *extra_data;
3492 struct user_token *result;
3494 if (lp_winbind_use_default_domain() &&
3495 (opt_target_workgroup == NULL)) {
3496 d_printf("winbind use default domain = yes set, please "
3497 "specify a workgroup\n");
3501 /* Send request to winbind daemon */
3503 ZERO_STRUCT(request);
3504 ZERO_STRUCT(response);
3506 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
3510 /* Look through extra data */
3512 if (!response.extra_data)
3515 extra_data = (const char *)response.extra_data;
3518 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3522 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
3524 if (result == NULL) {
3525 DEBUG(1, ("Could not malloc sid array\n"));
3529 extra_data = (const char *)response.extra_data;
3532 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3534 fstring domain, user;
3537 fstrcpy(result[i].name, name);
3539 p = strchr(name, *lp_winbind_separator());
3541 DEBUG(3, ("%s\n", name));
3544 fstrcpy(domain, opt_target_workgroup);
3545 fstrcpy(user, name);
3548 fstrcpy(domain, name);
3553 get_user_sids(domain, user, &(result[i].token));
3557 SAFE_FREE(response.extra_data);
3559 *user_tokens = result;
3564 static BOOL get_user_tokens_from_file(FILE *f,
3566 struct user_token **tokens)
3568 struct user_token *token = NULL;
3573 if (fgets(line, sizeof(line)-1, f) == NULL) {
3577 if (line[strlen(line)-1] == '\n')
3578 line[strlen(line)-1] = '\0';
3580 if (line[0] == ' ') {
3584 string_to_sid(&sid, &line[1]);
3586 if (token == NULL) {
3587 DEBUG(0, ("File does not begin with username"));
3591 add_sid_to_token(&token->token, &sid);
3595 /* And a new user... */
3598 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
3599 if (*tokens == NULL) {
3600 DEBUG(0, ("Could not realloc tokens\n"));
3604 token = &((*tokens)[*num_tokens-1]);
3606 fstrcpy(token->name, line);
3607 token->token.num_sids = 0;
3608 token->token.user_sids = NULL;
3617 * Show the list of all users that have access to a share
3620 static void show_userlist(struct cli_state *cli,
3621 TALLOC_CTX *mem_ctx, const char *netname,
3622 int num_tokens, struct user_token *tokens)
3625 SEC_DESC *share_sd = NULL;
3626 SEC_DESC *root_sd = NULL;
3628 SRV_SHARE_INFO info;
3632 result = cli_srvsvc_net_share_get_info(cli, mem_ctx, netname,
3635 if (!W_ERROR_IS_OK(result)) {
3636 DEBUG(1, ("Coult not query secdesc for share %s\n",
3641 share_sd = info.share.info502.info_502_str.sd;
3642 if (share_sd == NULL) {
3643 DEBUG(1, ("Got no secdesc for share %s\n",
3649 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3653 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
3656 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
3659 for (i=0; i<num_tokens; i++) {
3663 if (share_sd != NULL) {
3664 if (!se_access_check(share_sd, &tokens[i].token,
3665 1, &acc_granted, &status)) {
3666 DEBUG(1, ("Could not check share_sd for "
3672 if (!NT_STATUS_IS_OK(status))
3676 if (root_sd == NULL) {
3677 d_printf(" %s\n", tokens[i].name);
3681 if (!se_access_check(root_sd, &tokens[i].token,
3682 1, &acc_granted, &status)) {
3683 DEBUG(1, ("Could not check root_sd for user %s\n",
3688 if (!NT_STATUS_IS_OK(status))
3691 d_printf(" %s\n", tokens[i].name);
3695 cli_close(cli, fnum);
3707 static void collect_share(const char *name, uint32 m,
3708 const char *comment, void *state)
3710 struct share_list *share_list = (struct share_list *)state;
3712 if (m != STYPE_DISKTREE)
3715 share_list->num_shares += 1;
3716 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
3717 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
3720 static void rpc_share_userlist_usage(void)
3726 * List shares on a remote RPC server, including the security descriptors
3728 * All parameters are provided by the run_rpc_command function, except for
3729 * argc, argv which are passes through.
3731 * @param domain_sid The domain sid acquired from the remote server
3732 * @param cli A cli_state connected to the server.
3733 * @param mem_ctx Talloc context, destoyed on completion of the function.
3734 * @param argc Standard main() style argc
3735 * @param argv Standard main() style argv. Initial components are already
3738 * @return Normal NTSTATUS return.
3742 rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
3743 const char *domain_name,
3744 struct cli_state *cli,
3745 TALLOC_CTX *mem_ctx,
3746 int argc, const char **argv)
3754 struct user_token *tokens = NULL;
3757 struct share_list share_list;
3760 rpc_share_userlist_usage();
3761 return NT_STATUS_UNSUCCESSFUL;
3767 f = fopen(argv[0], "r");
3771 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
3772 return NT_STATUS_UNSUCCESSFUL;
3775 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
3781 DEBUG(0, ("Could not read users from file\n"));
3782 return NT_STATUS_UNSUCCESSFUL;
3785 for (i=0; i<num_tokens; i++)
3786 collect_alias_memberships(&tokens[i].token);
3788 init_enum_hnd(&hnd, 0);
3790 share_list.num_shares = 0;
3791 share_list.shares = NULL;
3793 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
3796 DEBUG(0, ("Error returning browse list: %s\n",
3801 for (i = 0; i < share_list.num_shares; i++) {
3802 char *netname = share_list.shares[i];
3804 if (netname[strlen(netname)-1] == '$')
3807 d_printf("%s\n", netname);
3809 show_userlist(cli, mem_ctx, netname,
3810 num_tokens, tokens);
3813 for (i=0; i<num_tokens; i++) {
3814 free_user_token(&tokens[i].token);
3817 SAFE_FREE(share_list.shares);
3819 return NT_STATUS_OK;
3823 rpc_share_allowedusers(int argc, const char **argv)
3827 result = run_rpc_command(NULL, PI_SAMR, 0,
3828 rpc_aliaslist_internals,
3833 result = run_rpc_command(NULL, PI_LSARPC, 0,
3839 return run_rpc_command(NULL, PI_SRVSVC, 0,
3840 rpc_share_allowedusers_internals,
3844 int net_usersidlist(int argc, const char **argv)
3847 struct user_token *tokens = NULL;
3851 net_usersidlist_usage(argc, argv);
3855 if (!get_user_tokens(&num_tokens, &tokens)) {
3856 DEBUG(0, ("Could not get the user/sid list\n"));
3860 for (i=0; i<num_tokens; i++) {
3861 dump_user_token(&tokens[i]);
3862 free_user_token(&tokens[i].token);
3869 int net_usersidlist_usage(int argc, const char **argv)
3871 d_printf("net usersidlist\n"
3872 "\tprints out a list of all users the running winbind knows\n"
3873 "\tabout, together with all their SIDs. This is used as\n"
3874 "\tinput to the 'net rpc share allowedusers' command.\n\n");
3876 net_common_flags_usage(argc, argv);
3881 * 'net rpc share' entrypoint.
3882 * @param argc Standard main() style argc
3883 * @param argv Standard main() style argv. Initial components are already
3887 int net_rpc_share(int argc, const char **argv)
3889 struct functable func[] = {
3890 {"add", rpc_share_add},
3891 {"delete", rpc_share_delete},
3892 {"allowedusers", rpc_share_allowedusers},
3893 {"migrate", rpc_share_migrate},
3898 return run_rpc_command(NULL, PI_SRVSVC, 0,
3899 rpc_share_list_internals,
3902 return net_run_function(argc, argv, func, rpc_share_usage);
3905 /****************************************************************************/
3907 static int rpc_file_usage(int argc, const char **argv)
3909 return net_help_file(argc, argv);
3913 * Close a file on a remote RPC server
3915 * All parameters are provided by the run_rpc_command function, except for
3916 * argc, argv which are passes through.
3918 * @param domain_sid The domain sid acquired from the remote server
3919 * @param cli A cli_state connected to the server.
3920 * @param mem_ctx Talloc context, destoyed on completion of the function.
3921 * @param argc Standard main() style argc
3922 * @param argv Standard main() style argv. Initial components are already
3925 * @return Normal NTSTATUS return.
3928 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
3929 struct cli_state *cli,
3930 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3933 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
3934 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3938 * Close a file on a remote RPC server
3940 * @param argc Standard main() style argc
3941 * @param argv Standard main() style argv. Initial components are already
3944 * @return A shell status integer (0 for success)
3946 static int rpc_file_close(int argc, const char **argv)
3949 DEBUG(1, ("No fileid given on close\n"));
3950 return(rpc_file_usage(argc, argv));
3953 return run_rpc_command(NULL, PI_SRVSVC, 0,
3954 rpc_file_close_internals,
3959 * Formatted print of open file info
3961 * @param info3 FILE_INFO_3 contents
3962 * @param str3 strings for FILE_INFO_3
3965 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
3967 fstring user = "", path = "";
3969 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
3970 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
3972 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
3973 info3->id, user, info3->perms, info3->num_locks, path);
3977 * List open files on a remote RPC server
3979 * All parameters are provided by the run_rpc_command function, except for
3980 * argc, argv which are passes through.
3982 * @param domain_sid The domain sid acquired from the remote server
3983 * @param cli A cli_state connected to the server.
3984 * @param mem_ctx Talloc context, destoyed on completion of the function.
3985 * @param argc Standard main() style argc
3986 * @param argv Standard main() style argv. Initial components are already
3989 * @return Normal NTSTATUS return.
3993 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
3994 struct cli_state *cli,
3995 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3997 SRV_FILE_INFO_CTR ctr;
4000 uint32 preferred_len = 0xffffffff, i;
4001 const char *username=NULL;
4003 init_enum_hnd(&hnd, 0);
4005 /* if argc > 0, must be user command */
4007 username = smb_xstrdup(argv[0]);
4009 result = cli_srvsvc_net_file_enum(
4010 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4012 if (!W_ERROR_IS_OK(result))
4015 /* Display results */
4018 "\nEnumerating open files on remote server:\n\n"\
4019 "\nFileId Opened by Perms Locks Path"\
4020 "\n------ --------- ----- ----- ---- \n");
4021 for (i = 0; i < ctr.num_entries; i++)
4022 display_file_info_3(&ctr.file.info3[i].info_3,
4023 &ctr.file.info3[i].info_3_str);
4025 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4030 * List files for a user on a remote RPC server
4032 * @param argc Standard main() style argc
4033 * @param argv Standard main() style argv. Initial components are already
4036 * @return A shell status integer (0 for success)
4038 static int rpc_file_user(int argc, const char **argv)
4041 DEBUG(1, ("No username given\n"));
4042 return(rpc_file_usage(argc, argv));
4045 return run_rpc_command(NULL, PI_SRVSVC, 0,
4046 rpc_file_list_internals,
4052 * 'net rpc file' entrypoint.
4053 * @param argc Standard main() style argc
4054 * @param argv Standard main() style argv. Initial components are already
4058 int net_rpc_file(int argc, const char **argv)
4060 struct functable func[] = {
4061 {"close", rpc_file_close},
4062 {"user", rpc_file_user},
4064 {"info", rpc_file_info},
4070 return run_rpc_command(NULL, PI_SRVSVC, 0,
4071 rpc_file_list_internals,
4074 return net_run_function(argc, argv, func, rpc_file_usage);
4077 /****************************************************************************/
4082 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4084 * All parameters are provided by the run_rpc_command function, except for
4085 * argc, argv which are passed through.
4087 * @param domain_sid The domain sid aquired from the remote server
4088 * @param cli A cli_state connected to the server.
4089 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4090 * @param argc Standard main() style argc
4091 * @param argv Standard main() style argv. Initial components are already
4094 * @return Normal NTSTATUS return.
4097 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4098 const char *domain_name,
4099 struct cli_state *cli,
4100 TALLOC_CTX *mem_ctx,
4101 int argc, const char **argv)
4103 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4105 result = cli_shutdown_abort(cli, mem_ctx);
4107 if (NT_STATUS_IS_OK(result)) {
4108 d_printf("\nShutdown successfully aborted\n");
4109 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4111 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4118 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4120 * All parameters are provided by the run_rpc_command function, except for
4121 * argc, argv which are passed through.
4123 * @param domain_sid The domain sid aquired from the remote server
4124 * @param cli A cli_state connected to the server.
4125 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4126 * @param argc Standard main() style argc
4127 * @param argv Standard main() style argv. Initial components are already
4130 * @return Normal NTSTATUS return.
4133 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
4134 const char *domain_name,
4135 struct cli_state *cli,
4136 TALLOC_CTX *mem_ctx,
4137 int argc, const char **argv)
4139 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4141 result = werror_to_ntstatus(cli_reg_abort_shutdown(cli, mem_ctx));
4143 if (NT_STATUS_IS_OK(result)) {
4144 d_printf("\nShutdown successfully aborted\n");
4145 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4147 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4153 * ABORT the Shut down of a remote RPC server
4155 * @param argc Standard main() style argc
4156 * @param argv Standard main() style argv. Initial components are already
4159 * @return A shell status integer (0 for success)
4162 static int rpc_shutdown_abort(int argc, const char **argv)
4164 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4165 rpc_shutdown_abort_internals,
4171 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4173 return run_rpc_command(NULL, PI_WINREG, 0,
4174 rpc_reg_shutdown_abort_internals,
4179 * Shut down a remote RPC Server via initshutdown pipe
4181 * All parameters are provided by the run_rpc_command function, except for
4182 * argc, argv which are passes through.
4184 * @param domain_sid The domain sid aquired from the remote server
4185 * @param cli A cli_state connected to the server.
4186 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4187 * @param argc Standard main() style argc
4188 * @param argc Standard main() style argv. Initial components are already
4191 * @return Normal NTSTATUS return.
4194 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
4195 const char *domain_name,
4196 struct cli_state *cli,
4197 TALLOC_CTX *mem_ctx,
4198 int argc, const char **argv)
4200 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4201 const char *msg = "This machine will be shutdown shortly";
4202 uint32 timeout = 20;
4208 timeout = opt_timeout;
4211 /* create an entry */
4212 result = cli_shutdown_init(cli, mem_ctx, msg, timeout, opt_reboot,
4215 if (NT_STATUS_IS_OK(result)) {
4216 d_printf("\nShutdown of remote machine succeeded\n");
4217 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4219 DEBUG(0,("Shutdown of remote machine failed!\n"));
4225 * Shut down a remote RPC Server via winreg pipe
4227 * All parameters are provided by the run_rpc_command function, except for
4228 * argc, argv which are passes through.
4230 * @param domain_sid The domain sid aquired from the remote server
4231 * @param cli A cli_state connected to the server.
4232 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4233 * @param argc Standard main() style argc
4234 * @param argc Standard main() style argv. Initial components are already
4237 * @return Normal NTSTATUS return.
4240 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
4241 const char *domain_name,
4242 struct cli_state *cli,
4243 TALLOC_CTX *mem_ctx,
4244 int argc, const char **argv)
4246 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4247 const char *msg = "This machine will be shutdown shortly";
4248 uint32 timeout = 20;
4253 struct poptOption long_options[] = {
4254 {"message", 'm', POPT_ARG_STRING, &msg},
4255 {"timeout", 't', POPT_ARG_INT, &timeout},
4256 {"reboot", 'r', POPT_ARG_NONE, &reboot},
4257 {"force", 'f', POPT_ARG_NONE, &force},
4261 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
4262 POPT_CONTEXT_KEEP_FIRST);
4264 rc = poptGetNextOpt(pc);
4267 /* an error occurred during option processing */
4268 DEBUG(0, ("%s: %s\n",
4269 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
4271 return NT_STATUS_INVALID_PARAMETER;
4278 timeout = opt_timeout;
4281 /* create an entry */
4282 result = werror_to_ntstatus(cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force));
4284 if (NT_STATUS_IS_OK(result)) {
4285 d_printf("\nShutdown of remote machine succeeded\n");
4286 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4289 DEBUG(0,("Shutdown of remote machine failed!\n"));
4295 * Shut down a remote RPC server
4297 * @param argc Standard main() style argc
4298 * @param argc Standard main() style argv. Initial components are already
4301 * @return A shell status integer (0 for success)
4304 static int rpc_shutdown(int argc, const char **argv)
4306 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4307 rpc_init_shutdown_internals,
4312 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4314 return run_rpc_command(NULL, PI_WINREG, 0, rpc_reg_shutdown_internals,
4318 /***************************************************************************
4319 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
4321 ***************************************************************************/
4324 * Add interdomain trust account to the RPC server.
4325 * All parameters (except for argc and argv) are passed by run_rpc_command
4328 * @param domain_sid The domain sid acquired from the server
4329 * @param cli A cli_state connected to the server.
4330 * @param mem_ctx Talloc context, destoyed on completion of the function.
4331 * @param argc Standard main() style argc
4332 * @param argc Standard main() style argv. Initial components are already
4335 * @return normal NTSTATUS return code
4338 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
4339 const char *domain_name,
4340 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4341 int argc, const char **argv) {
4343 POLICY_HND connect_pol, domain_pol, user_pol;
4344 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4347 uint32 unknown, user_rid;
4350 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
4351 return NT_STATUS_INVALID_PARAMETER;
4355 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4358 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
4359 return NT_STATUS_NO_MEMORY;
4362 strupper_m(acct_name);
4364 /* Get samr policy handle */
4365 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4367 if (!NT_STATUS_IS_OK(result)) {
4371 /* Get domain policy handle */
4372 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4373 MAXIMUM_ALLOWED_ACCESS,
4374 domain_sid, &domain_pol);
4375 if (!NT_STATUS_IS_OK(result)) {
4379 /* Create trusting domain's account */
4380 acb_info = ACB_NORMAL;
4381 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
4382 mimir: yes, most probably it is */
4384 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
4385 acct_name, acb_info, unknown,
4386 &user_pol, &user_rid);
4387 if (!NT_STATUS_IS_OK(result)) {
4392 SAM_USERINFO_CTR ctr;
4393 SAM_USER_INFO_23 p23;
4399 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
4403 ZERO_STRUCT(notime);
4407 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
4408 acb_info = ACB_DOMTRUST;
4410 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
4411 ¬ime, ¬ime, ¬ime,
4412 nostr, nostr, nostr, nostr, nostr,
4413 nostr, nostr, nostr, nostr, nostr,
4414 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
4415 0, 0, (char *)pwbuf);
4416 ctr.switch_value = 23;
4417 ctr.info.id23 = &p23;
4418 p23.passmustchange = 0;
4420 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 23,
4421 &cli->user_session_key, &ctr);
4423 if (!NT_STATUS_IS_OK(result)) {
4424 DEBUG(0,("Could not set trust account password: %s\n",
4425 nt_errstr(result)));
4431 SAFE_FREE(acct_name);
4436 * Create interdomain trust account for a remote domain.
4438 * @param argc standard argc
4439 * @param argv standard argv without initial components
4441 * @return Integer status (0 means success)
4444 static int rpc_trustdom_add(int argc, const char **argv)
4447 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
4450 d_printf("Usage: net rpc trustdom add <domain>\n");
4456 * Remove interdomain trust account from the RPC server.
4457 * All parameters (except for argc and argv) are passed by run_rpc_command
4460 * @param domain_sid The domain sid acquired from the server
4461 * @param cli A cli_state connected to the server.
4462 * @param mem_ctx Talloc context, destoyed on completion of the function.
4463 * @param argc Standard main() style argc
4464 * @param argc Standard main() style argv. Initial components are already
4467 * @return normal NTSTATUS return code
4470 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
4471 const char *domain_name,
4472 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4473 int argc, const char **argv) {
4475 POLICY_HND connect_pol, domain_pol, user_pol;
4476 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4479 DOM_SID trust_acct_sid;
4480 uint32 *user_rids, num_rids, *name_types;
4481 uint32 flags = 0x000003e8; /* Unknown */
4484 d_printf("Usage: net rpc trustdom del <domain_name>\n");
4485 return NT_STATUS_INVALID_PARAMETER;
4489 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4491 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
4493 if (acct_name == NULL)
4494 return NT_STATUS_NO_MEMORY;
4496 strupper_m(acct_name);
4498 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
4499 names[0] = acct_name;
4502 /* Get samr policy handle */
4503 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4505 if (!NT_STATUS_IS_OK(result)) {
4509 /* Get domain policy handle */
4510 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4511 MAXIMUM_ALLOWED_ACCESS,
4512 domain_sid, &domain_pol);
4513 if (!NT_STATUS_IS_OK(result)) {
4517 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, flags, 1,
4519 &user_rids, &name_types);
4521 if (!NT_STATUS_IS_OK(result)) {
4525 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
4526 MAXIMUM_ALLOWED_ACCESS,
4527 user_rids[0], &user_pol);
4529 if (!NT_STATUS_IS_OK(result)) {
4533 /* append the rid to the domain sid */
4534 sid_copy(&trust_acct_sid, domain_sid);
4535 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
4539 /* remove the sid */
4541 result = cli_samr_remove_sid_foreign_domain(cli, mem_ctx, &user_pol,
4544 if (!NT_STATUS_IS_OK(result)) {
4550 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
4552 if (!NT_STATUS_IS_OK(result)) {
4556 if (!NT_STATUS_IS_OK(result)) {
4557 DEBUG(0,("Could not set trust account password: %s\n",
4558 nt_errstr(result)));
4567 * Delete interdomain trust account for a remote domain.
4569 * @param argc standard argc
4570 * @param argv standard argv without initial components
4572 * @return Integer status (0 means success)
4575 static int rpc_trustdom_del(int argc, const char **argv)
4578 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
4581 d_printf("Usage: net rpc trustdom del <domain>\n");
4587 * Establish trust relationship to a trusting domain.
4588 * Interdomain account must already be created on remote PDC.
4590 * @param argc standard argc
4591 * @param argv standard argv without initial components
4593 * @return Integer status (0 means success)
4596 static int rpc_trustdom_establish(int argc, const char **argv)
4598 struct cli_state *cli;
4599 struct in_addr server_ip;
4600 POLICY_HND connect_hnd;
4601 TALLOC_CTX *mem_ctx;
4603 DOM_SID *domain_sid;
4604 smb_ucs2_t *uni_domain_name;
4607 char* domain_name_pol;
4612 * Connect to \\server\ipc$ as 'our domain' account with password
4616 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
4620 domain_name = smb_xstrdup(argv[0]);
4621 strupper_m(domain_name);
4623 /* account name used at first is our domain's name with '$' */
4624 asprintf(&acct_name, "%s$", lp_workgroup());
4625 strupper_m(acct_name);
4628 * opt_workgroup will be used by connection functions further,
4629 * hence it should be set to remote domain name instead of ours
4631 if (opt_workgroup) {
4632 opt_workgroup = smb_xstrdup(domain_name);
4635 opt_user_name = acct_name;
4637 /* find the domain controller */
4638 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
4639 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
4643 /* connect to ipc$ as username/password */
4644 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
4645 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
4647 /* Is it trusting domain account for sure ? */
4648 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
4649 nt_errstr(nt_status)));
4654 * Connect to \\server\ipc$ again (this time anonymously)
4657 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
4659 if (NT_STATUS_IS_ERR(nt_status)) {
4660 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
4661 domain_name, nt_errstr(nt_status)));
4665 * Use NetServerEnum2 to make sure we're talking to a proper server
4668 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
4669 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
4670 for domain %s\n", domain_name));
4673 if (!(mem_ctx = talloc_init("establishing trust relationship to "
4674 "domain %s", domain_name))) {
4675 DEBUG(0, ("talloc_init() failed\n"));
4681 * Call LsaOpenPolicy and LsaQueryInfo
4684 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4685 DEBUG(0, ("Could not initialise lsa pipe\n"));
4690 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
4692 if (NT_STATUS_IS_ERR(nt_status)) {
4693 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4694 nt_errstr(nt_status)));
4698 /* Querying info level 5 */
4700 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
4702 &domain_name_pol, &domain_sid);
4703 if (NT_STATUS_IS_ERR(nt_status)) {
4704 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4705 nt_errstr(nt_status)));
4709 if (push_ucs2_talloc(mem_ctx, &uni_domain_name, domain_name_pol) < 0) {
4710 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4715 /* There should be actually query info level 3 (following nt serv behaviour),
4716 but I still don't know if it's _really_ necessary */
4719 * Store the password in secrets db
4722 if (!secrets_store_trusted_domain_password(domain_name,
4724 strlen_w(uni_domain_name)+1,
4727 DEBUG(0, ("Storing password for trusted domain failed.\n"));
4732 * Close the pipes and clean up
4735 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
4736 if (NT_STATUS_IS_ERR(nt_status)) {
4737 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
4738 nt_errstr(nt_status)));
4742 if (cli->nt_pipe_fnum[cli->pipe_idx])
4743 cli_nt_session_close(cli);
4747 talloc_destroy(mem_ctx);
4749 d_printf("Trust to domain %s established\n", domain_name);
4754 * Revoke trust relationship to the remote domain
4756 * @param argc standard argc
4757 * @param argv standard argv without initial components
4759 * @return Integer status (0 means success)
4762 static int rpc_trustdom_revoke(int argc, const char **argv)
4766 if (argc < 1) return -1;
4768 /* generate upper cased domain name */
4769 domain_name = smb_xstrdup(argv[0]);
4770 strupper_m(domain_name);
4772 /* delete password of the trust */
4773 if (!trusted_domain_password_delete(domain_name)) {
4774 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
4783 * Usage for 'net rpc trustdom' command
4785 * @param argc standard argc
4786 * @param argv standard argv without inital components
4788 * @return Integer status returned to shell
4791 static int rpc_trustdom_usage(int argc, const char **argv)
4793 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
4794 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
4795 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
4796 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
4797 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
4802 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
4803 const char *domain_name,
4804 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4805 int argc, const char **argv)
4808 sid_to_string(str_sid, domain_sid);
4809 d_printf("%s\n", str_sid);
4810 return NT_STATUS_OK;
4814 static int rpc_trustdom_list(int argc, const char **argv)
4816 /* common variables */
4817 TALLOC_CTX* mem_ctx;
4818 struct cli_state *cli, *remote_cli;
4820 const char *domain_name = NULL;
4821 DOM_SID *queried_dom_sid;
4822 fstring ascii_sid, padding;
4823 int ascii_dom_name_len;
4824 POLICY_HND connect_hnd;
4826 /* trusted domains listing variables */
4827 unsigned int num_domains, enum_ctx = 0;
4828 int i, pad_len, col_len = 20;
4829 DOM_SID *domain_sids;
4830 char **trusted_dom_names;
4834 /* trusting domains listing variables */
4835 POLICY_HND domain_hnd;
4836 char **trusting_dom_names;
4837 uint32 *trusting_dom_rids;
4840 * Listing trusted domains (stored in secrets.tdb, if local)
4843 mem_ctx = talloc_init("trust relationships listing");
4846 * set domain and pdc name to local samba server (default)
4847 * or to remote one given in command line
4850 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
4851 domain_name = opt_workgroup;
4852 opt_target_workgroup = opt_workgroup;
4854 fstrcpy(pdc_name, global_myname());
4855 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
4856 opt_target_workgroup = domain_name;
4859 /* open \PIPE\lsarpc and open policy handle */
4860 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
4861 DEBUG(0, ("Couldn't connect to domain controller\n"));
4865 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4866 DEBUG(0, ("Could not initialise lsa pipe\n"));
4870 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
4872 if (NT_STATUS_IS_ERR(nt_status)) {
4873 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4874 nt_errstr(nt_status)));
4878 /* query info level 5 to obtain sid of a domain being queried */
4879 nt_status = cli_lsa_query_info_policy(
4880 cli, mem_ctx, &connect_hnd, 5 /* info level */,
4881 &dummy, &queried_dom_sid);
4883 if (NT_STATUS_IS_ERR(nt_status)) {
4884 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4885 nt_errstr(nt_status)));
4890 * Keep calling LsaEnumTrustdom over opened pipe until
4891 * the end of enumeration is reached
4894 d_printf("Trusted domains list:\n\n");
4897 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
4899 &trusted_dom_names, &domain_sids);
4901 if (NT_STATUS_IS_ERR(nt_status)) {
4902 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
4903 nt_errstr(nt_status)));
4907 for (i = 0; i < num_domains; i++) {
4908 /* convert sid into ascii string */
4909 sid_to_string(ascii_sid, &(domain_sids[i]));
4911 /* calculate padding space for d_printf to look nicer */
4912 pad_len = col_len - strlen(trusted_dom_names[i]);
4913 padding[pad_len] = 0;
4914 do padding[--pad_len] = ' '; while (pad_len);
4916 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
4920 * in case of no trusted domains say something rather
4921 * than just display blank line
4923 if (!num_domains) d_printf("none\n");
4925 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
4927 /* close this connection before doing next one */
4928 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
4929 if (NT_STATUS_IS_ERR(nt_status)) {
4930 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
4931 nt_errstr(nt_status)));
4935 cli_nt_session_close(cli);
4938 * Listing trusting domains (stored in passdb backend, if local)
4941 d_printf("\nTrusting domains list:\n\n");
4944 * Open \PIPE\samr and get needed policy handles
4946 if (!cli_nt_session_open(cli, PI_SAMR)) {
4947 DEBUG(0, ("Could not initialise samr pipe\n"));
4952 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
4954 if (!NT_STATUS_IS_OK(nt_status)) {
4955 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
4956 nt_errstr(nt_status)));
4960 /* SamrOpenDomain - we have to open domain policy handle in order to be
4961 able to enumerate accounts*/
4962 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
4963 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
4964 queried_dom_sid, &domain_hnd);
4965 if (!NT_STATUS_IS_OK(nt_status)) {
4966 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
4967 nt_errstr(nt_status)));
4972 * perform actual enumeration
4975 enum_ctx = 0; /* reset enumeration context from last enumeration */
4978 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
4979 &enum_ctx, ACB_DOMTRUST, 0xffff,
4980 &trusting_dom_names, &trusting_dom_rids,
4982 if (NT_STATUS_IS_ERR(nt_status)) {
4983 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
4984 nt_errstr(nt_status)));
4988 for (i = 0; i < num_domains; i++) {
4991 * get each single domain's sid (do we _really_ need this ?):
4992 * 1) connect to domain's pdc
4993 * 2) query the pdc for domain's sid
4996 /* get rid of '$' tail */
4997 ascii_dom_name_len = strlen(trusting_dom_names[i]);
4998 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
4999 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
5001 /* calculate padding space for d_printf to look nicer */
5002 pad_len = col_len - strlen(trusting_dom_names[i]);
5003 padding[pad_len] = 0;
5004 do padding[--pad_len] = ' '; while (pad_len);
5006 /* set opt_* variables to remote domain */
5007 strupper_m(trusting_dom_names[i]);
5008 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
5009 opt_target_workgroup = opt_workgroup;
5011 d_printf("%s%s", trusting_dom_names[i], padding);
5013 /* connect to remote domain controller */
5014 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
5016 /* query for domain's sid */
5017 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
5018 d_printf("couldn't get domain's sid\n");
5020 cli_shutdown(remote_cli);
5023 d_printf("domain controller is not responding\n");
5027 if (!num_domains) d_printf("none\n");
5029 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5031 /* close opened samr and domain policy handles */
5032 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
5033 if (!NT_STATUS_IS_OK(nt_status)) {
5034 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
5037 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
5038 if (!NT_STATUS_IS_OK(nt_status)) {
5039 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
5042 /* close samr pipe and connection to IPC$ */
5043 cli_nt_session_close(cli);
5046 talloc_destroy(mem_ctx);
5051 * Entrypoint for 'net rpc trustdom' code
5053 * @param argc standard argc
5054 * @param argv standard argv without initial components
5056 * @return Integer status (0 means success)
5059 static int rpc_trustdom(int argc, const char **argv)
5061 struct functable func[] = {
5062 {"add", rpc_trustdom_add},
5063 {"del", rpc_trustdom_del},
5064 {"establish", rpc_trustdom_establish},
5065 {"revoke", rpc_trustdom_revoke},
5066 {"help", rpc_trustdom_usage},
5067 {"list", rpc_trustdom_list},
5072 rpc_trustdom_usage(argc, argv);
5076 return (net_run_function(argc, argv, func, rpc_user_usage));
5080 * Check if a server will take rpc commands
5081 * @param flags Type of server to connect to (PDC, DMB, localhost)
5082 * if the host is not explicitly specified
5083 * @return BOOL (true means rpc supported)
5085 BOOL net_rpc_check(unsigned flags)
5087 struct cli_state cli;
5089 struct in_addr server_ip;
5090 char *server_name = NULL;
5092 /* flags (i.e. server type) may depend on command */
5093 if (!net_find_server(flags, &server_ip, &server_name))
5097 if (cli_initialise(&cli) == False)
5100 if (!cli_connect(&cli, server_name, &server_ip))
5102 if (!attempt_netbios_session_request(&cli, global_myname(),
5103 server_name, &server_ip))
5105 if (!cli_negprot(&cli))
5107 if (cli.protocol < PROTOCOL_NT1)
5116 /* dump sam database via samsync rpc calls */
5117 static int rpc_samdump(int argc, const char **argv) {
5118 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
5122 /* syncronise sam database via samsync rpc calls */
5123 static int rpc_vampire(int argc, const char **argv) {
5124 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
5129 * Migrate everything from a print-server
5131 * @param argc Standard main() style argc
5132 * @param argv Standard main() style argv. Initial components are already
5135 * @return A shell status integer (0 for success)
5137 * The order is important !
5138 * To successfully add drivers the print-queues have to exist !
5139 * Applying ACLs should be the last step, because you're easily locked out
5142 static int rpc_printer_migrate_all(int argc, const char **argv)
5147 printf("no server to migrate\n");
5151 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
5155 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
5159 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
5163 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
5167 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
5172 * Migrate print-drivers from a print-server
5174 * @param argc Standard main() style argc
5175 * @param argv Standard main() style argv. Initial components are already
5178 * @return A shell status integer (0 for success)
5180 static int rpc_printer_migrate_drivers(int argc, const char **argv)
5183 printf("no server to migrate\n");
5187 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5188 rpc_printer_migrate_drivers_internals,
5193 * Migrate print-forms from a print-server
5195 * @param argc Standard main() style argc
5196 * @param argv Standard main() style argv. Initial components are already
5199 * @return A shell status integer (0 for success)
5201 static int rpc_printer_migrate_forms(int argc, const char **argv)
5204 printf("no server to migrate\n");
5208 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5209 rpc_printer_migrate_forms_internals,
5214 * Migrate printers from a print-server
5216 * @param argc Standard main() style argc
5217 * @param argv Standard main() style argv. Initial components are already
5220 * @return A shell status integer (0 for success)
5222 static int rpc_printer_migrate_printers(int argc, const char **argv)
5225 printf("no server to migrate\n");
5229 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5230 rpc_printer_migrate_printers_internals,
5235 * Migrate printer-ACLs from a print-server
5237 * @param argc Standard main() style argc
5238 * @param argv Standard main() style argv. Initial components are already
5241 * @return A shell status integer (0 for success)
5243 static int rpc_printer_migrate_security(int argc, const char **argv)
5246 printf("no server to migrate\n");
5250 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5251 rpc_printer_migrate_security_internals,
5256 * Migrate printer-settings from a print-server
5258 * @param argc Standard main() style argc
5259 * @param argv Standard main() style argv. Initial components are already
5262 * @return A shell status integer (0 for success)
5264 static int rpc_printer_migrate_settings(int argc, const char **argv)
5267 printf("no server to migrate\n");
5271 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5272 rpc_printer_migrate_settings_internals,
5277 * 'net rpc printer' entrypoint.
5278 * @param argc Standard main() style argc
5279 * @param argv Standard main() style argv. Initial components are already
5283 int rpc_printer_migrate(int argc, const char **argv)
5286 /* ouch: when addriver and setdriver are called from within
5287 rpc_printer_migrate_drivers_internals, the printer-queue already
5290 struct functable func[] = {
5291 {"all", rpc_printer_migrate_all},
5292 {"drivers", rpc_printer_migrate_drivers},
5293 {"forms", rpc_printer_migrate_forms},
5294 {"help", rpc_printer_usage},
5295 {"printers", rpc_printer_migrate_printers},
5296 {"security", rpc_printer_migrate_security},
5297 {"settings", rpc_printer_migrate_settings},
5301 return net_run_function(argc, argv, func, rpc_printer_usage);
5306 * List printers on a remote RPC server
5308 * @param argc Standard main() style argc
5309 * @param argv Standard main() style argv. Initial components are already
5312 * @return A shell status integer (0 for success)
5314 static int rpc_printer_list(int argc, const char **argv)
5317 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5318 rpc_printer_list_internals,
5323 * List printer-drivers on a remote RPC server
5325 * @param argc Standard main() style argc
5326 * @param argv Standard main() style argv. Initial components are already
5329 * @return A shell status integer (0 for success)
5331 static int rpc_printer_driver_list(int argc, const char **argv)
5334 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5335 rpc_printer_driver_list_internals,
5340 * Publish printer in ADS via MSRPC
5342 * @param argc Standard main() style argc
5343 * @param argv Standard main() style argv. Initial components are already
5346 * @return A shell status integer (0 for success)
5348 static int rpc_printer_publish_publish(int argc, const char **argv)
5351 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5352 rpc_printer_publish_publish_internals,
5357 * Update printer in ADS via MSRPC
5359 * @param argc Standard main() style argc
5360 * @param argv Standard main() style argv. Initial components are already
5363 * @return A shell status integer (0 for success)
5365 static int rpc_printer_publish_update(int argc, const char **argv)
5368 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5369 rpc_printer_publish_update_internals,
5374 * UnPublish printer in ADS via MSRPC
5376 * @param argc Standard main() style argc
5377 * @param argv Standard main() style argv. Initial components are already
5380 * @return A shell status integer (0 for success)
5382 static int rpc_printer_publish_unpublish(int argc, const char **argv)
5385 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5386 rpc_printer_publish_unpublish_internals,
5391 * List published printers via MSRPC
5393 * @param argc Standard main() style argc
5394 * @param argv Standard main() style argv. Initial components are already
5397 * @return A shell status integer (0 for success)
5399 static int rpc_printer_publish_list(int argc, const char **argv)
5402 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5403 rpc_printer_publish_list_internals,
5409 * Publish printer in ADS
5411 * @param argc Standard main() style argc
5412 * @param argv Standard main() style argv. Initial components are already
5415 * @return A shell status integer (0 for success)
5417 static int rpc_printer_publish(int argc, const char **argv)
5420 struct functable func[] = {
5421 {"publish", rpc_printer_publish_publish},
5422 {"update", rpc_printer_publish_update},
5423 {"unpublish", rpc_printer_publish_unpublish},
5424 {"list", rpc_printer_publish_list},
5425 {"help", rpc_printer_usage},
5430 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5431 rpc_printer_publish_list_internals,
5434 return net_run_function(argc, argv, func, rpc_printer_usage);
5440 * Display rpc printer help page.
5441 * @param argc Standard main() style argc
5442 * @param argv Standard main() style argv. Initial components are already
5445 int rpc_printer_usage(int argc, const char **argv)
5447 return net_help_printer(argc, argv);
5451 * 'net rpc printer' entrypoint.
5452 * @param argc Standard main() style argc
5453 * @param argv Standard main() style argv. Initial components are already
5456 int net_rpc_printer(int argc, const char **argv)
5458 struct functable func[] = {
5459 {"list", rpc_printer_list},
5460 {"migrate", rpc_printer_migrate},
5461 {"driver", rpc_printer_driver_list},
5462 {"publish", rpc_printer_publish},
5467 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5468 rpc_printer_list_internals,
5471 return net_run_function(argc, argv, func, rpc_printer_usage);
5474 /****************************************************************************/
5478 * Basic usage function for 'net rpc'
5479 * @param argc Standard main() style argc
5480 * @param argv Standard main() style argv. Initial components are already
5484 int net_rpc_usage(int argc, const char **argv)
5486 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
5487 d_printf(" net rpc join \t\t\tto join a domain \n");
5488 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
5489 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
5490 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
5491 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
5492 d_printf(" net rpc group \t\tto list groups\n");
5493 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
5494 d_printf(" net rpc printer \t\tto list and migrate printers\n");
5495 d_printf(" net rpc file \t\t\tto list open files\n");
5496 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
5497 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
5498 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
5499 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
5500 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
5501 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
5502 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
5503 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
5504 d_printf(" net rpc registry\t\tto manage registry hives\n");
5506 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
5507 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
5508 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
5509 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
5510 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
5516 * Help function for 'net rpc'. Calls command specific help if requested
5517 * or displays usage of net rpc
5518 * @param argc Standard main() style argc
5519 * @param argv Standard main() style argv. Initial components are already
5523 int net_rpc_help(int argc, const char **argv)
5525 struct functable func[] = {
5526 {"join", rpc_join_usage},
5527 {"user", rpc_user_usage},
5528 {"group", rpc_group_usage},
5529 {"share", rpc_share_usage},
5530 /*{"changetrustpw", rpc_changetrustpw_usage}, */
5531 {"trustdom", rpc_trustdom_usage},
5532 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
5533 /*{"shutdown", rpc_shutdown_usage}, */
5538 net_rpc_usage(argc, argv);
5542 return (net_run_function(argc, argv, func, rpc_user_usage));
5547 * 'net rpc' entrypoint.
5548 * @param argc Standard main() style argc
5549 * @param argv Standard main() style argv. Initial components are already
5553 int net_rpc(int argc, const char **argv)
5555 struct functable func[] = {
5556 {"info", net_rpc_info},
5557 {"join", net_rpc_join},
5558 {"oldjoin", net_rpc_oldjoin},
5559 {"testjoin", net_rpc_testjoin},
5560 {"user", net_rpc_user},
5561 {"password", rpc_user_password},
5562 {"group", net_rpc_group},
5563 {"share", net_rpc_share},
5564 {"file", net_rpc_file},
5565 {"printer", net_rpc_printer},
5566 {"changetrustpw", net_rpc_changetrustpw},
5567 {"trustdom", rpc_trustdom},
5568 {"abortshutdown", rpc_shutdown_abort},
5569 {"shutdown", rpc_shutdown},
5570 {"samdump", rpc_samdump},
5571 {"vampire", rpc_vampire},
5572 {"getsid", net_rpc_getsid},
5573 {"rights", net_rpc_rights},
5574 {"service", net_rpc_service},
5575 {"registry", net_rpc_registry},
5576 {"help", net_rpc_help},
5579 return net_run_function(argc, argv, func, net_rpc_usage);