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)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "../utils/net.h"
27 * @brief RPC based subcommands for the 'net' utility.
29 * This file should contain much of the functionality that used to
30 * be found in rpcclient, execpt that the commands should change
31 * less often, and the fucntionality should be sane (the user is not
32 * expected to know a rid/sid before they conduct an operation etc.)
34 * @todo Perhaps eventually these should be split out into a number
35 * of files, as this could get quite big.
39 /* A function of this type is passed to the 'run_rpc_command' wrapper */
40 typedef NTSTATUS (*rpc_command_fn)(const DOM_SID *, const char *,
41 struct cli_state *, TALLOC_CTX *, int, const char **);
44 * Many of the RPC functions need the domain sid. This function gets
45 * it at the start of every run
47 * @param cli A cli_state already connected to the remote machine
49 * @return The Domain SID of the remote machine.
52 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx, char **domain_name)
56 NTSTATUS result = NT_STATUS_OK;
57 uint32 info_class = 5;
59 if (!cli_nt_session_open (cli, PI_LSARPC)) {
60 fprintf(stderr, "could not initialise lsa pipe\n");
64 result = cli_lsa_open_policy(cli, mem_ctx, False,
65 SEC_RIGHTS_MAXIMUM_ALLOWED,
67 if (!NT_STATUS_IS_OK(result)) {
71 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
72 domain_name, &domain_sid);
73 if (!NT_STATUS_IS_OK(result)) {
75 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
77 if (!NT_STATUS_IS_OK(result)) {
78 fprintf(stderr, "error: %s\n", nt_errstr(result));
84 cli_lsa_close(cli, mem_ctx, &pol);
85 cli_nt_session_close(cli);
91 * Run a single RPC command, from start to finish.
93 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
94 * @param conn_flag a NET_FLAG_ combination. Passed to
95 * net_make_ipc_connection.
96 * @param argc Standard main() style argc
97 * @param argc Standard main() style argv. Initial components are already
99 * @return A shell status integer (0 for success)
102 static int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
104 int argc, const char **argv)
106 struct cli_state *cli = NULL;
112 /* make use of cli_state handed over as an argument, if possible */
114 cli = net_make_ipc_connection(conn_flags);
124 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
125 DEBUG(0, ("talloc_init() failed\n"));
130 domain_sid = net_get_remote_domain_sid(cli, mem_ctx, &domain_name);
132 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
133 if (!cli_nt_session_open(cli, pipe_idx)) {
134 DEBUG(0, ("Could not initialise pipe\n"));
138 nt_status = fn(domain_sid, domain_name, cli, mem_ctx, argc, argv);
140 if (!NT_STATUS_IS_OK(nt_status)) {
141 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
143 DEBUG(5, ("rpc command function succedded\n"));
146 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
147 if (cli->nt_pipe_fnum)
148 cli_nt_session_close(cli);
151 /* close the connection only if it was opened here */
155 talloc_destroy(mem_ctx);
157 return (!NT_STATUS_IS_OK(nt_status));
161 /****************************************************************************/
165 * Force a change of the trust acccount password.
167 * All parameters are provided by the run_rpc_command function, except for
168 * argc, argv which are passes through.
170 * @param domain_sid The domain sid aquired from the remote server
171 * @param cli A cli_state connected to the server.
172 * @param mem_ctx Talloc context, destoyed on compleation of the function.
173 * @param argc Standard main() style argc
174 * @param argc Standard main() style argv. Initial components are already
177 * @return Normal NTSTATUS return.
180 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, const char *domain_name,
181 struct cli_state *cli, TALLOC_CTX *mem_ctx,
182 int argc, const char **argv) {
184 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
188 * Force a change of the trust acccount password.
190 * @param argc Standard main() style argc
191 * @param argc Standard main() style argv. Initial components are already
194 * @return A shell status integer (0 for success)
197 int net_rpc_changetrustpw(int argc, const char **argv)
199 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
200 rpc_changetrustpw_internals,
205 /****************************************************************************/
209 * Join a domain, the old way.
211 * This uses 'machinename' as the inital password, and changes it.
213 * The password should be created with 'server manager' or equiv first.
215 * All parameters are provided by the run_rpc_command function, except for
216 * argc, argv which are passes through.
218 * @param domain_sid The domain sid aquired from the remote server
219 * @param cli A cli_state connected to the server.
220 * @param mem_ctx Talloc context, destoyed on compleation of the function.
221 * @param argc Standard main() style argc
222 * @param argc Standard main() style argv. Initial components are already
225 * @return Normal NTSTATUS return.
228 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, const char *domain_name,
229 struct cli_state *cli,
231 int argc, const char **argv) {
233 fstring trust_passwd;
234 unsigned char orig_trust_passwd_hash[16];
236 uint32 sec_channel_type;
239 check what type of join - if the user want's to join as
240 a BDC, the server must agree that we are a BDC.
243 sec_channel_type = get_sec_channel_type(argv[0]);
245 sec_channel_type = get_sec_channel_type(NULL);
248 fstrcpy(trust_passwd, global_myname());
249 strlower_m(trust_passwd);
252 * Machine names can be 15 characters, but the max length on
253 * a password is 14. --jerry
256 trust_passwd[14] = '\0';
258 E_md4hash(trust_passwd, orig_trust_passwd_hash);
260 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
261 orig_trust_passwd_hash,
264 if (NT_STATUS_IS_OK(result))
265 printf("Joined domain %s.\n",opt_target_workgroup);
268 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
269 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
270 result = NT_STATUS_UNSUCCESSFUL;
277 * Join a domain, the old way.
279 * @param argc Standard main() style argc
280 * @param argc Standard main() style argv. Initial components are already
283 * @return A shell status integer (0 for success)
286 static int net_rpc_perform_oldjoin(int argc, const char **argv)
288 return run_rpc_command(NULL, PI_NETLOGON,
289 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
290 rpc_oldjoin_internals,
295 * Join a domain, the old way. This function exists to allow
296 * the message to be displayed when oldjoin was explicitly
297 * requested, but not when it was implied by "net rpc join"
299 * @param argc Standard main() style argc
300 * @param argc Standard main() style argv. Initial components are already
303 * @return A shell status integer (0 for success)
306 static int net_rpc_oldjoin(int argc, const char **argv)
308 int rc = net_rpc_perform_oldjoin(argc, argv);
311 d_printf("Failed to join domain\n");
318 * Basic usage function for 'net rpc join'
319 * @param argc Standard main() style argc
320 * @param argc Standard main() style argv. Initial components are already
324 static int rpc_join_usage(int argc, const char **argv)
326 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
327 "\t to join a domain with admin username & password\n"\
328 "\t\t password will be prompted if needed and none is specified\n"\
329 "\t <type> can be (default MEMBER)\n"\
330 "\t\t BDC - Join as a BDC\n"\
331 "\t\t PDC - Join as a PDC\n"\
332 "\t\t MEMBER - Join as a MEMBER server\n");
334 net_common_flags_usage(argc, argv);
339 * 'net rpc join' entrypoint.
340 * @param argc Standard main() style argc
341 * @param argc Standard main() style argv. Initial components are already
344 * Main 'net_rpc_join()' (where the admain username/password is used) is
346 * Try to just change the password, but if that doesn't work, use/prompt
347 * for a username/password.
350 int net_rpc_join(int argc, const char **argv)
352 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
355 return net_rpc_join_newstyle(argc, argv);
361 * display info about a rpc domain
363 * All parameters are provided by the run_rpc_command function, except for
364 * argc, argv which are passed through.
366 * @param domain_sid The domain sid acquired from the remote server
367 * @param cli A cli_state connected to the server.
368 * @param mem_ctx Talloc context, destoyed on completion of the function.
369 * @param argc Standard main() style argc
370 * @param argv Standard main() style argv. Initial components are already
373 * @return Normal NTSTATUS return.
377 rpc_info_internals(const DOM_SID *domain_sid, const char *domain_name,
378 struct cli_state *cli,
379 TALLOC_CTX *mem_ctx, int argc, const char **argv)
381 POLICY_HND connect_pol, domain_pol;
382 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
386 sid_to_string(sid_str, domain_sid);
388 /* Get sam policy handle */
389 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
391 if (!NT_STATUS_IS_OK(result)) {
395 /* Get domain policy handle */
396 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
397 MAXIMUM_ALLOWED_ACCESS,
398 domain_sid, &domain_pol);
399 if (!NT_STATUS_IS_OK(result)) {
404 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
406 if (NT_STATUS_IS_OK(result)) {
407 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
408 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
409 d_printf("Domain SID: %s\n", sid_str);
410 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
411 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
412 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
413 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
423 * 'net rpc info' entrypoint.
424 * @param argc Standard main() style argc
425 * @param argc Standard main() style argv. Initial components are already
428 int net_rpc_info(int argc, const char **argv)
430 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
437 * Fetch domain SID into the local secrets.tdb
439 * All parameters are provided by the run_rpc_command function, except for
440 * argc, argv which are passes through.
442 * @param domain_sid The domain sid acquired from the remote server
443 * @param cli A cli_state connected to the server.
444 * @param mem_ctx Talloc context, destoyed on completion of the function.
445 * @param argc Standard main() style argc
446 * @param argv Standard main() style argv. Initial components are already
449 * @return Normal NTSTATUS return.
453 rpc_getsid_internals(const DOM_SID *domain_sid, const char *domain_name,
454 struct cli_state *cli,
455 TALLOC_CTX *mem_ctx, int argc, const char **argv)
459 sid_to_string(sid_str, domain_sid);
460 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
461 sid_str, domain_name);
463 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
464 DEBUG(0,("Can't store domain SID\n"));
465 return NT_STATUS_UNSUCCESSFUL;
473 * 'net rpc getsid' entrypoint.
474 * @param argc Standard main() style argc
475 * @param argc Standard main() style argv. Initial components are already
478 int net_rpc_getsid(int argc, const char **argv)
480 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
481 rpc_getsid_internals,
486 /****************************************************************************/
489 * Basic usage function for 'net rpc user'
490 * @param argc Standard main() style argc.
491 * @param argv Standard main() style argv. Initial components are already
495 static int rpc_user_usage(int argc, const char **argv)
497 return net_help_user(argc, argv);
501 * Add a new user to a remote RPC server
503 * All parameters are provided by the run_rpc_command function, except for
504 * argc, argv which are passes through.
506 * @param domain_sid The domain sid acquired from the remote server
507 * @param cli A cli_state connected to the server.
508 * @param mem_ctx Talloc context, destoyed on completion of the function.
509 * @param argc Standard main() style argc
510 * @param argv Standard main() style argv. Initial components are already
513 * @return Normal NTSTATUS return.
516 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, const char *domain_name,
517 struct cli_state *cli, TALLOC_CTX *mem_ctx,
518 int argc, const char **argv) {
520 POLICY_HND connect_pol, domain_pol, user_pol;
521 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
522 const char *acct_name;
524 uint32 unknown, user_rid;
527 d_printf("User must be specified\n");
528 rpc_user_usage(argc, argv);
534 /* Get sam policy handle */
536 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
538 if (!NT_STATUS_IS_OK(result)) {
542 /* Get domain policy handle */
544 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
545 MAXIMUM_ALLOWED_ACCESS,
546 domain_sid, &domain_pol);
547 if (!NT_STATUS_IS_OK(result)) {
551 /* Create domain user */
553 acb_info = ACB_NORMAL;
554 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
556 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
557 acct_name, acb_info, unknown,
558 &user_pol, &user_rid);
559 if (!NT_STATUS_IS_OK(result)) {
564 if (!NT_STATUS_IS_OK(result)) {
565 d_printf("Failed to add user %s - %s\n", acct_name,
568 d_printf("Added user %s\n", acct_name);
574 * Add a new user to a remote RPC server
576 * @param argc Standard main() style argc
577 * @param argv Standard main() style argv. Initial components are already
580 * @return A shell status integer (0 for success)
583 static int rpc_user_add(int argc, const char **argv)
585 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
590 * Delete a user from a remote RPC server
592 * All parameters are provided by the run_rpc_command function, except for
593 * argc, argv which are passes through.
595 * @param domain_sid The domain sid acquired from the remote server
596 * @param cli A cli_state connected to the server.
597 * @param mem_ctx Talloc context, destoyed on completion of the function.
598 * @param argc Standard main() style argc
599 * @param argv Standard main() style argv. Initial components are already
602 * @return Normal NTSTATUS return.
605 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
606 const char *domain_name,
607 struct cli_state *cli,
609 int argc, const char **argv)
611 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
612 POLICY_HND connect_pol, domain_pol, user_pol;
615 d_printf("User must be specified\n");
616 rpc_user_usage(argc, argv);
619 /* Get sam policy and domain handles */
621 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
624 if (!NT_STATUS_IS_OK(result)) {
628 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
629 MAXIMUM_ALLOWED_ACCESS,
630 domain_sid, &domain_pol);
632 if (!NT_STATUS_IS_OK(result)) {
636 /* Get handle on user */
639 uint32 *user_rids, num_rids, *name_types;
640 uint32 flags = 0x000003e8; /* Unknown */
642 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
644 &num_rids, &user_rids,
647 if (!NT_STATUS_IS_OK(result)) {
651 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
652 MAXIMUM_ALLOWED_ACCESS,
653 user_rids[0], &user_pol);
655 if (!NT_STATUS_IS_OK(result)) {
662 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
664 if (!NT_STATUS_IS_OK(result)) {
668 /* Display results */
676 * Delete a user from a remote RPC server
678 * @param argc Standard main() style argc
679 * @param argv Standard main() style argv. Initial components are already
682 * @return A shell status integer (0 for success)
685 static int rpc_user_delete(int argc, const char **argv)
687 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
692 * Set a password for a user on a remote RPC server
694 * All parameters are provided by the run_rpc_command function, except for
695 * argc, argv which are passes through.
697 * @param domain_sid The domain sid acquired from the remote server
698 * @param cli A cli_state connected to the server.
699 * @param mem_ctx Talloc context, destoyed on completion of the function.
700 * @param argc Standard main() style argc
701 * @param argv Standard main() style argv. Initial components are already
704 * @return Normal NTSTATUS return.
707 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
708 const char *domain_name,
709 struct cli_state *cli,
711 int argc, const char **argv)
713 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
714 POLICY_HND connect_pol, domain_pol, user_pol;
715 SAM_USERINFO_CTR ctr;
716 SAM_USER_INFO_24 p24;
719 const char *new_password;
723 d_printf("User must be specified\n");
724 rpc_user_usage(argc, argv);
731 new_password = argv[1];
733 asprintf(&prompt, "Enter new password for %s:", user);
734 new_password = getpass(prompt);
738 /* Get sam policy and domain handles */
740 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
743 if (!NT_STATUS_IS_OK(result)) {
747 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
748 MAXIMUM_ALLOWED_ACCESS,
749 domain_sid, &domain_pol);
751 if (!NT_STATUS_IS_OK(result)) {
755 /* Get handle on user */
758 uint32 *user_rids, num_rids, *name_types;
759 uint32 flags = 0x000003e8; /* Unknown */
761 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
763 &num_rids, &user_rids,
766 if (!NT_STATUS_IS_OK(result)) {
770 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
771 MAXIMUM_ALLOWED_ACCESS,
772 user_rids[0], &user_pol);
774 if (!NT_STATUS_IS_OK(result)) {
779 /* Set password on account */
784 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
786 init_sam_user_info24(&p24, (char *)pwbuf,24);
788 ctr.switch_value = 24;
789 ctr.info.id24 = &p24;
791 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
792 &cli->user_session_key, &ctr);
794 if (!NT_STATUS_IS_OK(result)) {
798 /* Display results */
806 * Set a user's password on a remote RPC server
808 * @param argc Standard main() style argc
809 * @param argv Standard main() style argv. Initial components are already
812 * @return A shell status integer (0 for success)
815 static int rpc_user_password(int argc, const char **argv)
817 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
822 * List user's groups on a remote RPC server
824 * All parameters are provided by the run_rpc_command function, except for
825 * argc, argv which are passes through.
827 * @param domain_sid The domain sid acquired from the remote server
828 * @param cli A cli_state connected to the server.
829 * @param mem_ctx Talloc context, destoyed on completion of the function.
830 * @param argc Standard main() style argc
831 * @param argv Standard main() style argv. Initial components are already
834 * @return Normal NTSTATUS return.
838 rpc_user_info_internals(const DOM_SID *domain_sid, const char *domain_name,
839 struct cli_state *cli,
840 TALLOC_CTX *mem_ctx, int argc, const char **argv)
842 POLICY_HND connect_pol, domain_pol, user_pol;
843 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
844 uint32 *rids, num_rids, *name_types, num_names;
845 uint32 flags = 0x000003e8; /* Unknown */
851 d_printf("User must be specified\n");
852 rpc_user_usage(argc, argv);
855 /* Get sam policy handle */
857 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
859 if (!NT_STATUS_IS_OK(result)) goto done;
861 /* Get domain policy handle */
863 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
864 MAXIMUM_ALLOWED_ACCESS,
865 domain_sid, &domain_pol);
866 if (!NT_STATUS_IS_OK(result)) goto done;
868 /* Get handle on user */
870 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
872 &num_rids, &rids, &name_types);
874 if (!NT_STATUS_IS_OK(result)) goto done;
876 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
877 MAXIMUM_ALLOWED_ACCESS,
879 if (!NT_STATUS_IS_OK(result)) goto done;
881 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
882 &num_rids, &user_gids);
886 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
888 for (i = 0; i < num_rids; i++)
889 rids[i] = user_gids[i].g_rid;
891 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
892 flags, num_rids, rids,
893 &num_names, &names, &name_types);
895 if (!NT_STATUS_IS_OK(result)) {
899 /* Display results */
901 for (i = 0; i < num_names; i++)
902 printf("%s\n", names[i]);
909 * List a user's groups from a remote RPC server
911 * @param argc Standard main() style argc
912 * @param argv Standard main() style argv. Initial components are already
915 * @return A shell status integer (0 for success)
918 static int rpc_user_info(int argc, const char **argv)
920 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
925 * List users on a remote RPC server
927 * All parameters are provided by the run_rpc_command function, except for
928 * argc, argv which are passes through.
930 * @param domain_sid The domain sid acquired from the remote server
931 * @param cli A cli_state connected to the server.
932 * @param mem_ctx Talloc context, destoyed on completion of the function.
933 * @param argc Standard main() style argc
934 * @param argv Standard main() style argv. Initial components are already
937 * @return Normal NTSTATUS return.
941 rpc_user_list_internals(const DOM_SID *domain_sid, const char *domain_name,
942 struct cli_state *cli,
943 TALLOC_CTX *mem_ctx, int argc, const char **argv)
945 POLICY_HND connect_pol, domain_pol;
946 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
947 uint32 start_idx=0, num_entries, i, loop_count = 0;
948 SAM_DISPINFO_CTR ctr;
949 SAM_DISPINFO_1 info1;
951 /* Get sam policy handle */
953 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
955 if (!NT_STATUS_IS_OK(result)) {
959 /* Get domain policy handle */
961 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
962 MAXIMUM_ALLOWED_ACCESS,
963 domain_sid, &domain_pol);
964 if (!NT_STATUS_IS_OK(result)) {
968 /* Query domain users */
971 ctr.sam.info1 = &info1;
972 if (opt_long_list_entries)
973 d_printf("\nUser name Comment"\
974 "\n-----------------------------\n");
977 uint32 max_entries, max_size;
979 get_query_dispinfo_params(
980 loop_count, &max_entries, &max_size);
982 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
983 &start_idx, 1, &num_entries,
984 max_entries, max_size, &ctr);
987 for (i = 0; i < num_entries; i++) {
988 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
989 if (opt_long_list_entries)
990 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
992 if (opt_long_list_entries)
993 printf("%-21.21s %s\n", user, desc);
995 printf("%s\n", user);
997 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1004 * 'net rpc user' entrypoint.
1005 * @param argc Standard main() style argc
1006 * @param argc Standard main() style argv. Initial components are already
1010 int net_rpc_user(int argc, const char **argv)
1012 struct functable func[] = {
1013 {"add", rpc_user_add},
1014 {"info", rpc_user_info},
1015 {"delete", rpc_user_delete},
1016 {"password", rpc_user_password},
1021 if (opt_long_list_entries) {
1024 return run_rpc_command(NULL,PI_SAMR, 0,
1025 rpc_user_list_internals,
1029 return net_run_function(argc, argv, func, rpc_user_usage);
1033 /****************************************************************************/
1036 * Basic usage function for 'net rpc group'
1037 * @param argc Standard main() style argc.
1038 * @param argv Standard main() style argv. Initial components are already
1042 static int rpc_group_usage(int argc, const char **argv)
1044 return net_help_group(argc, argv);
1048 * Delete group on a remote RPC server
1050 * All parameters are provided by the run_rpc_command function, except for
1051 * argc, argv which are passes through.
1053 * @param domain_sid The domain sid acquired from the remote server
1054 * @param cli A cli_state connected to the server.
1055 * @param mem_ctx Talloc context, destoyed on completion of the function.
1056 * @param argc Standard main() style argc
1057 * @param argv Standard main() style argv. Initial components are already
1060 * @return Normal NTSTATUS return.
1063 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1064 const char *domain_name,
1065 struct cli_state *cli,
1066 TALLOC_CTX *mem_ctx,
1067 int argc, const char **argv)
1069 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1070 BOOL group_is_primary = False;
1071 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1073 uint32 *group_rids, num_rids, *name_types, num_members,
1074 *group_attrs, group_rid;
1075 uint32 flags = 0x000003e8; /* Unknown */
1078 /* DOM_GID *user_gids; */
1079 SAM_USERINFO_CTR *user_ctr;
1083 d_printf("specify group\n");
1084 rpc_group_usage(argc,argv);
1085 return NT_STATUS_OK; /* ok? */
1088 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1091 if (!NT_STATUS_IS_OK(result)) {
1092 d_printf("Request samr_connect failed\n");
1096 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1097 MAXIMUM_ALLOWED_ACCESS,
1098 domain_sid, &domain_pol);
1100 if (!NT_STATUS_IS_OK(result)) {
1101 d_printf("Request open_domain failed\n");
1105 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1107 &num_rids, &group_rids,
1110 if (!NT_STATUS_IS_OK(result)) {
1111 d_printf("Lookup of '%s' failed\n",argv[0]);
1115 switch (name_types[0])
1117 case SID_NAME_DOM_GRP:
1118 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1119 MAXIMUM_ALLOWED_ACCESS,
1120 group_rids[0], &group_pol);
1121 if (!NT_STATUS_IS_OK(result)) {
1122 d_printf("Request open_group failed");
1126 group_rid = group_rids[0];
1128 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1129 &num_members, &group_rids,
1132 if (!NT_STATUS_IS_OK(result)) {
1133 d_printf("Unable to query group members of %s",argv[0]);
1138 d_printf("Domain Group %s (rid: %d) has %d members\n",
1139 argv[0],group_rid,num_members);
1142 /* Check if group is anyone's primary group */
1143 for (i = 0; i < num_members; i++)
1145 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1146 MAXIMUM_ALLOWED_ACCESS,
1147 group_rids[i], &user_pol);
1149 if (!NT_STATUS_IS_OK(result)) {
1150 d_printf("Unable to open group member %d\n",group_rids[i]);
1154 ZERO_STRUCT(user_ctr);
1156 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
1159 if (!NT_STATUS_IS_OK(result)) {
1160 d_printf("Unable to lookup userinfo for group member %d\n",group_rids[i]);
1164 if (user_ctr->info.id21->group_rid == group_rid) {
1165 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1168 d_printf("Group is primary group of %s\n",temp);
1169 group_is_primary = True;
1172 cli_samr_close(cli, mem_ctx, &user_pol);
1175 if (group_is_primary) {
1176 d_printf("Unable to delete group because some of it's "
1177 "members have it as primary group\n");
1178 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1182 /* remove all group members */
1183 for (i = 0; i < num_members; i++)
1186 d_printf("Remove group member %d...",group_rids[i]);
1187 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, group_rids[i]);
1189 if (NT_STATUS_IS_OK(result)) {
1194 d_printf("failed\n");
1199 result = cli_samr_delete_dom_group(cli, mem_ctx, &group_pol);
1202 /* removing a local group is easier... */
1203 case SID_NAME_ALIAS:
1204 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1205 MAXIMUM_ALLOWED_ACCESS,
1206 group_rids[0], &group_pol);
1208 if (!NT_STATUS_IS_OK(result)) {
1209 d_printf("Request open_alias failed\n");
1213 result = cli_samr_delete_dom_alias(cli, mem_ctx, &group_pol);
1216 d_printf("%s is of type %s. This command is only for deleting local or global groups\n",
1217 argv[0],sid_type_lookup(name_types[0]));
1218 result = NT_STATUS_UNSUCCESSFUL;
1223 if (NT_STATUS_IS_OK(result)) {
1225 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1227 d_printf("Deleting of %s failed: %s\n",argv[0],
1228 get_friendly_nt_error_msg(result));
1236 static int rpc_group_delete(int argc, const char **argv)
1238 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1243 rpc_group_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1244 struct cli_state *cli,
1245 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1247 POLICY_HND connect_pol, domain_pol, group_pol;
1248 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1249 GROUP_INFO_CTR group_info;
1252 d_printf("Group name must be specified\n");
1253 rpc_group_usage(argc, argv);
1254 return NT_STATUS_OK;
1257 /* Get sam policy handle */
1259 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1261 if (!NT_STATUS_IS_OK(result)) goto done;
1263 /* Get domain policy handle */
1265 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1266 MAXIMUM_ALLOWED_ACCESS,
1267 domain_sid, &domain_pol);
1268 if (!NT_STATUS_IS_OK(result)) goto done;
1270 /* Create the group */
1272 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1273 argv[0], MAXIMUM_ALLOWED_ACCESS,
1275 if (!NT_STATUS_IS_OK(result)) goto done;
1277 if (strlen(opt_comment) == 0) goto done;
1279 /* We've got a comment to set */
1281 group_info.switch_value1 = 4;
1282 init_samr_group_info4(&group_info.group.info4, opt_comment);
1284 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &group_info);
1285 if (!NT_STATUS_IS_OK(result)) goto done;
1288 if (NT_STATUS_IS_OK(result))
1289 DEBUG(5, ("add group succeeded\n"));
1291 d_printf("add group failed: %s\n", nt_errstr(result));
1297 rpc_alias_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1298 struct cli_state *cli,
1299 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1301 POLICY_HND connect_pol, domain_pol, alias_pol;
1302 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1303 ALIAS_INFO_CTR alias_info;
1306 d_printf("Group name must be specified\n");
1307 rpc_group_usage(argc, argv);
1308 return NT_STATUS_OK;
1311 /* Get sam policy handle */
1313 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1315 if (!NT_STATUS_IS_OK(result)) goto done;
1317 /* Get domain policy handle */
1319 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1320 MAXIMUM_ALLOWED_ACCESS,
1321 domain_sid, &domain_pol);
1322 if (!NT_STATUS_IS_OK(result)) goto done;
1324 /* Create the group */
1326 result = cli_samr_create_dom_alias(cli, mem_ctx, &domain_pol,
1327 argv[0], &alias_pol);
1328 if (!NT_STATUS_IS_OK(result)) goto done;
1330 if (strlen(opt_comment) == 0) goto done;
1332 /* We've got a comment to set */
1334 alias_info.switch_value1 = 3;
1335 alias_info.switch_value2 = 3;
1336 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1338 result = cli_samr_set_aliasinfo(cli, mem_ctx, &alias_pol, &alias_info);
1339 if (!NT_STATUS_IS_OK(result)) goto done;
1342 if (NT_STATUS_IS_OK(result))
1343 DEBUG(5, ("add group succeeded\n"));
1345 d_printf("add group failed: %s\n", nt_errstr(result));
1350 static int rpc_group_add(int argc, const char **argv)
1353 return run_rpc_command(NULL, PI_SAMR, 0,
1354 rpc_alias_add_internals,
1357 return run_rpc_command(NULL, PI_SAMR, 0,
1358 rpc_group_add_internals,
1363 get_sid_from_name(struct cli_state *cli, TALLOC_CTX *mem_ctx, const char *name,
1364 DOM_SID *sid, enum SID_NAME_USE *type)
1366 int current_pipe = cli->pipe_idx;
1368 DOM_SID *sids = NULL;
1369 uint32 *types = NULL;
1371 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1373 if (current_pipe != PI_LSARPC) {
1375 if (current_pipe != -1)
1376 cli_nt_session_close(cli);
1378 if (!cli_nt_session_open(cli, PI_LSARPC))
1382 result = cli_lsa_open_policy(cli, mem_ctx, False,
1383 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1385 if (!NT_STATUS_IS_OK(result))
1388 result = cli_lsa_lookup_names(cli, mem_ctx, &lsa_pol, 1,
1389 &name, &sids, &types);
1391 if (NT_STATUS_IS_OK(result)) {
1392 sid_copy(sid, &sids[0]);
1396 cli_lsa_close(cli, mem_ctx, &lsa_pol);
1399 if (current_pipe != PI_LSARPC) {
1400 cli_nt_session_close(cli);
1401 if (current_pipe != -1)
1402 cli_nt_session_open(cli, current_pipe);
1405 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1407 /* Try as S-1-5-whatever */
1411 if (string_to_sid(&tmp_sid, name)) {
1412 sid_copy(sid, &tmp_sid);
1413 *type = SID_NAME_UNKNOWN;
1414 result = NT_STATUS_OK;
1422 rpc_add_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1423 const DOM_SID *group_sid, const char *member)
1425 POLICY_HND connect_pol, domain_pol;
1428 POLICY_HND group_pol;
1431 uint32 *rids = NULL;
1432 uint32 *rid_types = NULL;
1436 sid_copy(&sid, group_sid);
1438 if (!sid_split_rid(&sid, &group_rid))
1439 return NT_STATUS_UNSUCCESSFUL;
1441 /* Get sam policy handle */
1442 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1444 if (!NT_STATUS_IS_OK(result))
1447 /* Get domain policy handle */
1448 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1449 MAXIMUM_ALLOWED_ACCESS,
1451 if (!NT_STATUS_IS_OK(result))
1454 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1456 &num_rids, &rids, &rid_types);
1458 if (!NT_STATUS_IS_OK(result)) {
1459 d_printf("Could not lookup up group member %s\n", member);
1463 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1464 MAXIMUM_ALLOWED_ACCESS,
1465 group_rid, &group_pol);
1467 if (!NT_STATUS_IS_OK(result))
1470 result = cli_samr_add_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1473 cli_samr_close(cli, mem_ctx, &connect_pol);
1478 rpc_add_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1479 const DOM_SID *alias_sid, const char *member)
1481 POLICY_HND connect_pol, domain_pol;
1484 POLICY_HND alias_pol;
1487 enum SID_NAME_USE member_type;
1491 sid_copy(&sid, alias_sid);
1493 if (!sid_split_rid(&sid, &alias_rid))
1494 return NT_STATUS_UNSUCCESSFUL;
1496 result = get_sid_from_name(cli, mem_ctx, member,
1497 &member_sid, &member_type);
1499 if (!NT_STATUS_IS_OK(result)) {
1500 d_printf("Could not lookup up group member %s\n", member);
1504 /* Get sam policy handle */
1505 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1507 if (!NT_STATUS_IS_OK(result)) {
1511 /* Get domain policy handle */
1512 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1513 MAXIMUM_ALLOWED_ACCESS,
1515 if (!NT_STATUS_IS_OK(result)) {
1519 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1520 MAXIMUM_ALLOWED_ACCESS,
1521 alias_rid, &alias_pol);
1523 if (!NT_STATUS_IS_OK(result))
1526 result = cli_samr_add_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1528 if (!NT_STATUS_IS_OK(result))
1532 cli_samr_close(cli, mem_ctx, &connect_pol);
1537 rpc_group_addmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1538 struct cli_state *cli,
1539 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1542 enum SID_NAME_USE group_type;
1545 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
1546 return NT_STATUS_UNSUCCESSFUL;
1549 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1550 &group_sid, &group_type))) {
1551 d_printf("Could not lookup group name %s\n", argv[0]);
1552 return NT_STATUS_UNSUCCESSFUL;
1555 if (group_type == SID_NAME_DOM_GRP) {
1556 NTSTATUS result = rpc_add_groupmem(cli, mem_ctx,
1557 &group_sid, argv[1]);
1559 if (!NT_STATUS_IS_OK(result)) {
1560 d_printf("Could not add %s to %s: %s\n",
1561 argv[1], argv[0], nt_errstr(result));
1566 if (group_type == SID_NAME_ALIAS) {
1567 NTSTATUS result = rpc_add_aliasmem(cli, mem_ctx,
1568 &group_sid, argv[1]);
1570 if (!NT_STATUS_IS_OK(result)) {
1571 d_printf("Could not add %s to %s: %s\n",
1572 argv[1], argv[0], nt_errstr(result));
1577 d_printf("Can only add members to global or local groups which "
1578 "%s is not\n", argv[0]);
1580 return NT_STATUS_UNSUCCESSFUL;
1583 static int rpc_group_addmem(int argc, const char **argv)
1585 return run_rpc_command(NULL, PI_SAMR, 0,
1586 rpc_group_addmem_internals,
1591 rpc_del_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1592 const DOM_SID *group_sid, const char *member)
1594 POLICY_HND connect_pol, domain_pol;
1597 POLICY_HND group_pol;
1600 uint32 *rids = NULL;
1601 uint32 *rid_types = NULL;
1605 sid_copy(&sid, group_sid);
1607 if (!sid_split_rid(&sid, &group_rid))
1608 return NT_STATUS_UNSUCCESSFUL;
1610 /* Get sam policy handle */
1611 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1613 if (!NT_STATUS_IS_OK(result))
1616 /* Get domain policy handle */
1617 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1618 MAXIMUM_ALLOWED_ACCESS,
1620 if (!NT_STATUS_IS_OK(result))
1623 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1625 &num_rids, &rids, &rid_types);
1627 if (!NT_STATUS_IS_OK(result)) {
1628 d_printf("Could not lookup up group member %s\n", member);
1632 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1633 MAXIMUM_ALLOWED_ACCESS,
1634 group_rid, &group_pol);
1636 if (!NT_STATUS_IS_OK(result))
1639 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1642 cli_samr_close(cli, mem_ctx, &connect_pol);
1647 rpc_del_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1648 const DOM_SID *alias_sid, const char *member)
1650 POLICY_HND connect_pol, domain_pol;
1653 POLICY_HND alias_pol;
1656 enum SID_NAME_USE member_type;
1660 sid_copy(&sid, alias_sid);
1662 if (!sid_split_rid(&sid, &alias_rid))
1663 return NT_STATUS_UNSUCCESSFUL;
1665 result = get_sid_from_name(cli, mem_ctx, member,
1666 &member_sid, &member_type);
1668 if (!NT_STATUS_IS_OK(result)) {
1669 d_printf("Could not lookup up group member %s\n", member);
1673 /* Get sam policy handle */
1674 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1676 if (!NT_STATUS_IS_OK(result)) {
1680 /* Get domain policy handle */
1681 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1682 MAXIMUM_ALLOWED_ACCESS,
1684 if (!NT_STATUS_IS_OK(result)) {
1688 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1689 MAXIMUM_ALLOWED_ACCESS,
1690 alias_rid, &alias_pol);
1692 if (!NT_STATUS_IS_OK(result))
1695 result = cli_samr_del_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1697 if (!NT_STATUS_IS_OK(result))
1701 cli_samr_close(cli, mem_ctx, &connect_pol);
1706 rpc_group_delmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1707 struct cli_state *cli,
1708 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1711 enum SID_NAME_USE group_type;
1714 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
1715 return NT_STATUS_UNSUCCESSFUL;
1718 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1719 &group_sid, &group_type))) {
1720 d_printf("Could not lookup group name %s\n", argv[0]);
1721 return NT_STATUS_UNSUCCESSFUL;
1724 if (group_type == SID_NAME_DOM_GRP) {
1725 NTSTATUS result = rpc_del_groupmem(cli, mem_ctx,
1726 &group_sid, argv[1]);
1728 if (!NT_STATUS_IS_OK(result)) {
1729 d_printf("Could not del %s from %s: %s\n",
1730 argv[1], argv[0], nt_errstr(result));
1735 if (group_type == SID_NAME_ALIAS) {
1736 NTSTATUS result = rpc_del_aliasmem(cli, mem_ctx,
1737 &group_sid, argv[1]);
1739 if (!NT_STATUS_IS_OK(result)) {
1740 d_printf("Could not del %s from %s: %s\n",
1741 argv[1], argv[0], nt_errstr(result));
1746 d_printf("Can only delete members from global or local groups which "
1747 "%s is not\n", argv[0]);
1749 return NT_STATUS_UNSUCCESSFUL;
1752 static int rpc_group_delmem(int argc, const char **argv)
1754 return run_rpc_command(NULL, PI_SAMR, 0,
1755 rpc_group_delmem_internals,
1760 * List groups on a remote RPC server
1762 * All parameters are provided by the run_rpc_command function, except for
1763 * argc, argv which are passes through.
1765 * @param domain_sid The domain sid acquired from the remote server
1766 * @param cli A cli_state connected to the server.
1767 * @param mem_ctx Talloc context, destoyed on completion of the function.
1768 * @param argc Standard main() style argc
1769 * @param argv Standard main() style argv. Initial components are already
1772 * @return Normal NTSTATUS return.
1776 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1777 struct cli_state *cli,
1778 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1780 POLICY_HND connect_pol, domain_pol;
1781 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1782 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1783 struct acct_info *groups;
1784 DOM_SID global_sid_Builtin;
1785 BOOL global = False;
1787 BOOL builtin = False;
1795 for (i=0; i<argc; i++) {
1796 if (strequal(argv[i], "global"))
1799 if (strequal(argv[i], "local"))
1802 if (strequal(argv[i], "builtin"))
1806 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1808 /* Get sam policy handle */
1810 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1812 if (!NT_STATUS_IS_OK(result)) {
1816 /* Get domain policy handle */
1818 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1819 MAXIMUM_ALLOWED_ACCESS,
1820 domain_sid, &domain_pol);
1821 if (!NT_STATUS_IS_OK(result)) {
1825 /* Query domain groups */
1826 if (opt_long_list_entries)
1827 d_printf("\nGroup name Comment"\
1828 "\n-----------------------------\n");
1830 SAM_DISPINFO_CTR ctr;
1831 SAM_DISPINFO_3 info3;
1836 ctr.sam.info3 = &info3;
1840 get_query_dispinfo_params(
1841 loop_count, &max_entries, &max_size);
1843 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1844 &start_idx, 3, &num_entries,
1845 max_entries, max_size, &ctr);
1847 if (!NT_STATUS_IS_OK(result) &&
1848 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1851 for (i = 0; i < num_entries; i++) {
1853 fstring group, desc;
1855 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1856 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1858 if (opt_long_list_entries)
1859 printf("%-21.21s %-50.50s\n",
1862 printf("%s\n", group);
1864 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1865 /* query domain aliases */
1870 /* The max_size field in cli_samr_enum_als_groups is more like
1871 * an account_control field with indiviual bits what to
1872 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
1873 * everything. I'm too lazy (sorry) to get this through to
1874 * rpc_parse/ etc. Volker */
1876 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1878 &groups, &num_entries);
1880 if (!NT_STATUS_IS_OK(result) &&
1881 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1884 for (i = 0; i < num_entries; i++) {
1886 char *description = NULL;
1888 if (opt_long_list_entries) {
1890 POLICY_HND alias_pol;
1893 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1898 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1901 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1903 description = unistr2_tdup(mem_ctx,
1904 &ctr.alias.info3.uni_acct_desc);
1908 if (description != NULL) {
1909 printf("%-21.21s %-50.50s\n",
1910 groups[i].acct_name,
1913 printf("%s\n", groups[i].acct_name);
1916 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1917 cli_samr_close(cli, mem_ctx, &domain_pol);
1918 /* Get builtin policy handle */
1920 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1921 MAXIMUM_ALLOWED_ACCESS,
1922 &global_sid_Builtin, &domain_pol);
1923 if (!NT_STATUS_IS_OK(result)) {
1926 /* query builtin aliases */
1929 if (!builtin) break;
1931 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1932 &start_idx, max_entries,
1933 &groups, &num_entries);
1935 if (!NT_STATUS_IS_OK(result) &&
1936 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1939 for (i = 0; i < num_entries; i++) {
1941 char *description = NULL;
1943 if (opt_long_list_entries) {
1945 POLICY_HND alias_pol;
1948 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1953 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1956 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1958 description = unistr2_tdup(mem_ctx,
1959 &ctr.alias.info3.uni_acct_desc);
1963 if (description != NULL) {
1964 printf("%-21.21s %-50.50s\n",
1965 groups[i].acct_name,
1968 printf("%s\n", groups[i].acct_name);
1971 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1977 static int rpc_group_list(int argc, const char **argv)
1979 return run_rpc_command(NULL, PI_SAMR, 0,
1980 rpc_group_list_internals,
1985 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1986 const char *domain_name, const DOM_SID *domain_sid,
1987 POLICY_HND *domain_pol, uint32 rid)
1990 POLICY_HND group_pol;
1991 uint32 num_members, *group_rids, *group_attrs;
1998 sid_to_string(sid_str, domain_sid);
2000 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
2001 MAXIMUM_ALLOWED_ACCESS,
2004 if (!NT_STATUS_IS_OK(result))
2007 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
2008 &num_members, &group_rids,
2011 if (!NT_STATUS_IS_OK(result))
2014 while (num_members > 0) {
2015 int this_time = 512;
2017 if (num_members < this_time)
2018 this_time = num_members;
2020 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol, 1000,
2021 this_time, group_rids,
2022 &num_names, &names, &name_types);
2024 if (!NT_STATUS_IS_OK(result))
2027 /* We only have users as members, but make the output
2028 the same as the output of alias members */
2030 for (i = 0; i < this_time; i++) {
2032 if (opt_long_list_entries) {
2033 printf("%s-%d %s\\%s %d\n", sid_str,
2034 group_rids[i], domain_name, names[i],
2037 printf("%s\\%s\n", domain_name, names[i]);
2041 num_members -= this_time;
2045 return NT_STATUS_OK;
2049 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2050 POLICY_HND *domain_pol, uint32 rid)
2053 POLICY_HND alias_pol, lsa_pol;
2055 DOM_SID *alias_sids;
2061 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
2062 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2064 if (!NT_STATUS_IS_OK(result))
2067 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
2068 &num_members, &alias_sids);
2070 if (!NT_STATUS_IS_OK(result)) {
2071 d_printf("Couldn't list alias members\n");
2075 if (num_members == 0) {
2076 return NT_STATUS_OK;
2079 cli_nt_session_close(cli);
2081 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2082 d_printf("Couldn't open LSA pipe\n");
2086 result = cli_lsa_open_policy(cli, mem_ctx, True,
2087 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2089 if (!NT_STATUS_IS_OK(result)) {
2090 d_printf("Couldn't open LSA policy handle\n");
2094 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
2096 &domains, &names, &types);
2098 if (!NT_STATUS_IS_OK(result) &&
2099 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2100 d_printf("Couldn't lookup SIDs\n");
2104 for (i = 0; i < num_members; i++) {
2106 sid_to_string(sid_str, &alias_sids[i]);
2108 if (opt_long_list_entries) {
2109 printf("%s %s\\%s %d\n", sid_str,
2110 domains[i] ? domains[i] : "*unknown*",
2111 names[i] ? names[i] : "*unknown*", types[i]);
2114 printf("%s\\%s\n", domains[i], names[i]);
2116 printf("%s\n", sid_str);
2120 return NT_STATUS_OK;
2124 rpc_group_members_internals(const DOM_SID *domain_sid,
2125 const char *domain_name,
2126 struct cli_state *cli,
2127 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2130 POLICY_HND connect_pol, domain_pol;
2131 uint32 num_rids, *rids, *rid_types;
2133 /* Get sam policy handle */
2135 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2138 if (!NT_STATUS_IS_OK(result))
2141 /* Get domain policy handle */
2143 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2144 MAXIMUM_ALLOWED_ACCESS,
2145 domain_sid, &domain_pol);
2147 if (!NT_STATUS_IS_OK(result))
2150 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2151 1, argv, &num_rids, &rids, &rid_types);
2153 if (!NT_STATUS_IS_OK(result)) {
2155 /* Ok, did not find it in the global sam, try with builtin */
2157 DOM_SID sid_Builtin;
2159 cli_samr_close(cli, mem_ctx, &domain_pol);
2161 string_to_sid(&sid_Builtin, "S-1-5-32");
2163 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2164 MAXIMUM_ALLOWED_ACCESS,
2165 &sid_Builtin, &domain_pol);
2167 if (!NT_STATUS_IS_OK(result)) {
2168 d_printf("Couldn't find group %s\n", argv[0]);
2172 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2176 if (!NT_STATUS_IS_OK(result)) {
2177 d_printf("Couldn't find group %s\n", argv[0]);
2182 if (num_rids != 1) {
2183 d_printf("Couldn't find group %s\n", argv[0]);
2187 if (rid_types[0] == SID_NAME_DOM_GRP) {
2188 return rpc_list_group_members(cli, mem_ctx, domain_name,
2189 domain_sid, &domain_pol,
2193 if (rid_types[0] == SID_NAME_ALIAS) {
2194 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
2198 return NT_STATUS_NO_SUCH_GROUP;
2201 static int rpc_group_members(int argc, const char **argv)
2204 return rpc_group_usage(argc, argv);
2207 return run_rpc_command(NULL, PI_SAMR, 0,
2208 rpc_group_members_internals,
2213 rpc_group_rename_internals(const DOM_SID *domain_sid,
2214 const char *domain_name,
2215 struct cli_state *cli,
2216 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2219 POLICY_HND connect_pol, domain_pol, group_pol;
2220 uint32 num_rids, *rids, *rid_types;
2224 d_printf("Usage: 'net rpc group rename group newname'\n");
2225 return NT_STATUS_UNSUCCESSFUL;
2228 /* Get sam policy handle */
2230 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2233 if (!NT_STATUS_IS_OK(result))
2236 /* Get domain policy handle */
2238 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2239 MAXIMUM_ALLOWED_ACCESS,
2240 domain_sid, &domain_pol);
2242 if (!NT_STATUS_IS_OK(result))
2245 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2246 1, argv, &num_rids, &rids, &rid_types);
2248 if (num_rids != 1) {
2249 d_printf("Couldn't find group %s\n", argv[0]);
2253 if (rid_types[0] != SID_NAME_DOM_GRP) {
2254 d_printf("Can only rename domain groups\n");
2255 return NT_STATUS_UNSUCCESSFUL;
2258 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
2259 MAXIMUM_ALLOWED_ACCESS,
2260 rids[0], &group_pol);
2262 if (!NT_STATUS_IS_OK(result))
2267 ctr.switch_value1 = 2;
2268 init_samr_group_info2(&ctr.group.info2, argv[1]);
2270 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &ctr);
2272 if (!NT_STATUS_IS_OK(result))
2275 return NT_STATUS_NO_SUCH_GROUP;
2278 static int rpc_group_rename(int argc, const char **argv)
2281 return rpc_group_usage(argc, argv);
2284 return run_rpc_command(NULL, PI_SAMR, 0,
2285 rpc_group_rename_internals,
2290 * 'net rpc group' entrypoint.
2291 * @param argc Standard main() style argc
2292 * @param argc Standard main() style argv. Initial components are already
2296 int net_rpc_group(int argc, const char **argv)
2298 struct functable func[] = {
2299 {"add", rpc_group_add},
2300 {"delete", rpc_group_delete},
2301 {"addmem", rpc_group_addmem},
2302 {"delmem", rpc_group_delmem},
2303 {"list", rpc_group_list},
2304 {"members", rpc_group_members},
2305 {"rename", rpc_group_rename},
2310 if (opt_long_list_entries) {
2313 return run_rpc_command(NULL, PI_SAMR, 0,
2314 rpc_group_list_internals,
2318 return net_run_function(argc, argv, func, rpc_group_usage);
2321 /****************************************************************************/
2323 static int rpc_share_usage(int argc, const char **argv)
2325 return net_help_share(argc, argv);
2329 * Add a share on a remote RPC server
2331 * All parameters are provided by the run_rpc_command function, except for
2332 * argc, argv which are passes through.
2334 * @param domain_sid The domain sid acquired from the remote server
2335 * @param cli A cli_state connected to the server.
2336 * @param mem_ctx Talloc context, destoyed on completion of the function.
2337 * @param argc Standard main() style argc
2338 * @param argv Standard main() style argv. Initial components are already
2341 * @return Normal NTSTATUS return.
2344 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
2345 struct cli_state *cli,
2346 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2349 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2351 uint32 type=0; /* only allow disk shares to be added */
2352 uint32 num_users=0, perms=0;
2353 char *password=NULL; /* don't allow a share password */
2356 path = strchr(sharename, '=');
2358 return NT_STATUS_UNSUCCESSFUL;
2361 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
2362 opt_comment, perms, opt_maxusers,
2363 num_users, path, password,
2365 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2368 static int rpc_share_add(int argc, const char **argv)
2370 if ((argc < 1) || !strchr(argv[0], '=')) {
2371 DEBUG(1,("Sharename or path not specified on add\n"));
2372 return rpc_share_usage(argc, argv);
2374 return run_rpc_command(NULL, PI_SRVSVC, 0,
2375 rpc_share_add_internals,
2380 * Delete a share on a remote RPC server
2382 * All parameters are provided by the run_rpc_command function, except for
2383 * argc, argv which are passes through.
2385 * @param domain_sid The domain sid acquired from the remote server
2386 * @param cli A cli_state connected to the server.
2387 * @param mem_ctx Talloc context, destoyed on completion of the function.
2388 * @param argc Standard main() style argc
2389 * @param argv Standard main() style argv. Initial components are already
2392 * @return Normal NTSTATUS return.
2395 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name,
2396 struct cli_state *cli,
2397 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2401 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
2402 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2406 * Delete a share on a remote RPC server
2408 * @param domain_sid The domain sid acquired from the remote server
2409 * @param argc Standard main() style argc
2410 * @param argv Standard main() style argv. Initial components are already
2413 * @return A shell status integer (0 for success)
2415 static int rpc_share_delete(int argc, const char **argv)
2418 DEBUG(1,("Sharename not specified on delete\n"));
2419 return rpc_share_usage(argc, argv);
2421 return run_rpc_command(NULL, PI_SRVSVC, 0,
2422 rpc_share_del_internals,
2427 * Formatted print of share info
2429 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2432 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
2434 fstring netname = "", remark = "";
2436 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
2437 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
2439 if (opt_long_list_entries) {
2440 d_printf("%-12s %-8.8s %-50s\n",
2441 netname, share_type[info1->info_1.type], remark);
2443 d_printf("%s\n", netname);
2449 * List shares on a remote RPC server
2451 * All parameters are provided by the run_rpc_command function, except for
2452 * argc, argv which are passes through.
2454 * @param domain_sid The domain sid acquired from the remote server
2455 * @param cli A cli_state connected to the server.
2456 * @param mem_ctx Talloc context, destoyed on completion of the function.
2457 * @param argc Standard main() style argc
2458 * @param argv Standard main() style argv. Initial components are already
2461 * @return Normal NTSTATUS return.
2465 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
2466 struct cli_state *cli,
2467 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2469 SRV_SHARE_INFO_CTR ctr;
2472 uint32 preferred_len = 0xffffffff, i;
2474 init_enum_hnd(&hnd, 0);
2476 result = cli_srvsvc_net_share_enum(
2477 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
2479 if (!W_ERROR_IS_OK(result))
2482 /* Display results */
2484 if (opt_long_list_entries) {
2486 "\nEnumerating shared resources (exports) on remote server:\n\n"\
2487 "\nShare name Type Description\n"\
2488 "---------- ---- -----------\n");
2490 for (i = 0; i < ctr.num_entries; i++)
2491 display_share_info_1(&ctr.share.info1[i]);
2493 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2497 * Migrate shares from a remote RPC server to the local RPC srever
2499 * All parameters are provided by the run_rpc_command function, except for
2500 * argc, argv which are passes through.
2502 * @param domain_sid The domain sid acquired from the remote server
2503 * @param cli A cli_state connected to the server.
2504 * @param mem_ctx Talloc context, destoyed on completion of the function.
2505 * @param argc Standard main() style argc
2506 * @param argv Standard main() style argv. Initial components are already
2509 * @return Normal NTSTATUS return.
2512 rpc_share_migrate_shares_internals(const DOM_SID *domain_sid, const char *domain_name,
2513 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2514 int argc, const char **argv)
2517 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2518 SRV_SHARE_INFO_CTR ctr_src;
2520 uint32 type = 0; /* only allow disk shares to be added */
2521 uint32 num_uses = 0, perms = 0, max_uses = 0;
2522 char *password = NULL; /* don't allow a share password */
2523 uint32 preferred_len = 0xffffffff, i;
2524 BOOL got_dst_srvsvc_pipe = False;
2525 struct cli_state *cli_dst = NULL;
2526 uint32 level = 502; /* includes secdesc */
2527 SEC_DESC *share_sd = NULL;
2529 init_enum_hnd(&hnd, 0);
2531 result = cli_srvsvc_net_share_enum(
2532 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2533 if (!W_ERROR_IS_OK(result))
2536 /* connect local PI_SRVSVC */
2537 nt_status = connect_local_pipe(&cli_dst, PI_SRVSVC, &got_dst_srvsvc_pipe);
2538 if (!NT_STATUS_IS_OK(nt_status))
2542 for (i = 0; i < ctr_src.num_entries; i++) {
2544 fstring netname = "", remark = "", path = "";
2545 /* reset error-code */
2546 nt_status = NT_STATUS_UNSUCCESSFUL;
2548 rpcstr_pull_unistr2_fstring(
2549 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
2550 rpcstr_pull_unistr2_fstring(
2551 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
2552 rpcstr_pull_unistr2_fstring(
2553 path, &ctr_src.share.info502[i].info_502_str.uni_path);
2554 num_uses = ctr_src.share.info502[i].info_502.num_uses;
2555 max_uses = ctr_src.share.info502[i].info_502.max_uses;
2556 perms = ctr_src.share.info502[i].info_502.perms;
2560 share_sd = dup_sec_desc(
2561 mem_ctx, ctr_src.share.info502[i].info_502_str.sd);
2563 /* since we do not have NetShareGetInfo implemented in samba3 we
2564 only can skip inside the enum-ctr_src */
2566 char *one_share = talloc_strdup(mem_ctx, argv[0]);
2567 if (!strequal(netname, one_share))
2571 /* skip builtin shares */
2572 /* FIXME: should print$ be added too ? */
2573 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
2574 strequal(netname,"global"))
2577 /* only work with file-shares */
2578 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2579 d_printf("skipping [%s]: not a file share.\n", netname);
2587 /* finallly add the share on the dst server
2588 please note that samba currently does not allow to
2589 add a share without existing directory */
2591 printf("migrating: [%s], path: %s, comment: %s, %s share-ACLs\n",
2592 netname, path, remark, opt_acls ? "including" : "without" );
2594 if (opt_verbose && opt_acls)
2595 display_sec_desc(share_sd);
2597 result = cli_srvsvc_net_share_add(cli_dst, mem_ctx, netname, type,
2598 remark, perms, max_uses,
2599 num_uses, path, password,
2602 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
2603 printf(" [%s] does already exist\n", netname);
2607 if (!W_ERROR_IS_OK(result)) {
2608 printf("cannot add share: %s\n", dos_errstr(result));
2614 nt_status = NT_STATUS_OK;
2617 if (got_dst_srvsvc_pipe) {
2618 cli_nt_session_close(cli_dst);
2619 cli_shutdown(cli_dst);
2627 * Migrate shares from a rpc-server to another
2629 * @param argc Standard main() style argc
2630 * @param argv Standard main() style argv. Initial components are already
2633 * @return A shell status integer (0 for success)
2635 static int rpc_share_migrate_shares(int argc, const char **argv)
2639 printf("no server to migrate\n");
2643 return run_rpc_command(NULL, PI_SRVSVC, 0,
2644 rpc_share_migrate_shares_internals,
2648 typedef struct copy_clistate {
2649 TALLOC_CTX *mem_ctx;
2650 struct cli_state *cli_share_src;
2651 struct cli_state *cli_share_dst;
2659 * @param f file_info
2660 * @param mask current search mask
2661 * @param state arg-pointer
2664 static void copy_fn(file_info *f, const char *mask, void *state)
2666 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2667 struct copy_clistate *local_state = (struct copy_clistate *)state;
2668 fstring filename, new_mask, dir;
2670 if (strequal(f->name, ".") || strequal(f->name, ".."))
2673 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
2676 if (f->mode & aDIR) {
2678 DEBUG(3,("got dir: %s\n", f->name));
2680 fstrcpy(dir, local_state->cwd);
2682 fstrcat(dir, f->name);
2684 /* create that directory */
2685 nt_status = net_copy_file(local_state->mem_ctx,
2686 local_state->cli_share_src,
2687 local_state->cli_share_dst,
2689 opt_acls? True : False,
2690 opt_attrs? True : False,
2691 opt_timestamps? True : False,
2694 if (!NT_STATUS_IS_OK(nt_status))
2695 printf("could not copy dir %s: %s\n",
2696 dir, nt_errstr(nt_status));
2698 /* search below that directory */
2699 fstrcpy(new_mask, dir);
2700 fstrcat(new_mask, "\\*");
2702 if (!sync_files(local_state->mem_ctx,
2703 local_state->cli_share_src,
2704 local_state->cli_share_dst,
2707 printf("could not sync files\n");
2714 fstrcpy(filename, local_state->cwd);
2715 fstrcat(filename, "\\");
2716 fstrcat(filename, f->name);
2718 DEBUG(3,("got file: %s\n", filename));
2720 nt_status = net_copy_file(local_state->mem_ctx,
2721 local_state->cli_share_src,
2722 local_state->cli_share_dst,
2724 opt_acls? True : False,
2725 opt_attrs? True : False,
2726 opt_timestamps? True: False,
2729 if (!NT_STATUS_IS_OK(nt_status))
2730 printf("could not copy file %s: %s\n",
2731 filename, nt_errstr(nt_status));
2736 * sync files, can be called recursivly to list files
2737 * and then call copy_fn for each file
2739 * @param mem_ctx TALLOC_CTX
2740 * @param cli_share_src a connected share on the originating server
2741 * @param cli_share_dst a connected share on the destination server
2742 * @param mask the current search mask
2743 * @param cwd the current path
2745 * @return Boolean result
2747 BOOL sync_files(TALLOC_CTX *mem_ctx,
2748 struct cli_state *cli_share_src,
2749 struct cli_state *cli_share_dst,
2750 pstring mask, fstring cwd)
2754 uint16 attribute = aSYSTEM | aHIDDEN | aDIR;
2755 struct copy_clistate clistate;
2757 clistate.mem_ctx = mem_ctx;
2758 clistate.cli_share_src = cli_share_src;
2759 clistate.cli_share_dst = cli_share_dst;
2762 DEBUG(3,("calling cli_list with mask: %s\n", mask));
2764 if (cli_list(cli_share_src, mask, attribute, copy_fn, &clistate) == -1) {
2765 d_printf("listing %s failed with error: %s\n",
2766 mask, cli_errstr(cli_share_src));
2775 * Sync all files inside a remote share to another share (over smb)
2777 * All parameters are provided by the run_rpc_command function, except for
2778 * argc, argv which are passes through.
2780 * @param domain_sid The domain sid acquired from the remote server
2781 * @param cli A cli_state connected to the server.
2782 * @param mem_ctx Talloc context, destoyed on completion of the function.
2783 * @param argc Standard main() style argc
2784 * @param argv Standard main() style argv. Initial components are already
2787 * @return Normal NTSTATUS return.
2790 rpc_share_migrate_files_internals(const DOM_SID *domain_sid, const char *domain_name,
2791 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2792 int argc, const char **argv)
2795 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2796 SRV_SHARE_INFO_CTR ctr_src;
2798 uint32 preferred_len = 0xffffffff, i;
2800 struct cli_state *cli_share_src = NULL;
2801 struct cli_state *cli_share_dst = NULL;
2802 BOOL got_src_share = False;
2803 BOOL got_dst_share = False;
2805 extern struct in_addr loopback_ip;
2807 init_enum_hnd(&hnd, 0);
2809 result = cli_srvsvc_net_share_enum(
2810 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2812 if (!W_ERROR_IS_OK(result))
2815 for (i = 0; i < ctr_src.num_entries; i++) {
2817 fstring netname = "", remark = "", path = "";
2819 rpcstr_pull_unistr2_fstring(
2820 netname, &ctr_src.share.info2[i].info_2_str.uni_netname);
2821 rpcstr_pull_unistr2_fstring(
2822 remark, &ctr_src.share.info2[i].info_2_str.uni_remark);
2823 rpcstr_pull_unistr2_fstring(
2824 path, &ctr_src.share.info2[i].info_2_str.uni_path);
2826 /* since we do not have NetShareGetInfo implemented in samba3 we
2827 only can skip inside the enum-ctr_src */
2829 char *one_share = talloc_strdup(mem_ctx, argv[0]);
2830 if (!strequal(netname, one_share))
2834 /* skip builtin and hidden shares
2835 In particular, one might not want to mirror whole discs :) */
2836 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$"))
2839 if (strequal(netname, "print$") || netname[1] == '$') {
2840 d_printf("skipping [%s]: builtin/hidden share\n", netname);
2844 if (opt_exclude && in_list(netname, (char *)opt_exclude, False)) {
2845 printf("excluding [%s]\n", netname);
2849 /* only work with file-shares */
2850 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2851 d_printf("skipping [%s]: not a file share.\n", netname);
2856 return NT_STATUS_UNSUCCESSFUL;
2858 printf("syncing [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
2860 opt_acls ? "including" : "without",
2861 opt_attrs ? "including" : "without",
2862 opt_timestamps ? "(preserving timestamps)" : "");
2865 /* open share source */
2866 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
2867 cli->desthost, netname, "A:");
2868 if (!NT_STATUS_IS_OK(nt_status))
2871 got_src_share = True;
2874 /* open share destination */
2875 nt_status = connect_to_service(&cli_share_dst, &loopback_ip,
2876 "127.0.0.1", netname, "A:");
2877 if (!NT_STATUS_IS_OK(nt_status))
2880 got_dst_share = True;
2883 /* now call the filesync */
2884 pstrcpy(mask, "\\*");
2886 if (!sync_files(mem_ctx, cli_share_src, cli_share_dst, mask, NULL)) {
2887 d_printf("could not sync files for share: %s\n", netname);
2888 nt_status = NT_STATUS_UNSUCCESSFUL;
2894 nt_status = NT_STATUS_OK;
2899 cli_shutdown(cli_share_src);
2902 cli_shutdown(cli_share_dst);
2908 static int rpc_share_migrate_files(int argc, const char **argv)
2912 printf("no server to migrate\n");
2916 return run_rpc_command(NULL, PI_SRVSVC, 0,
2917 rpc_share_migrate_files_internals,
2922 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
2923 * from one server to another
2925 * @param argc Standard main() style argc
2926 * @param argv Standard main() style argv. Initial components are already
2929 * @return A shell status integer (0 for success)
2932 static int rpc_share_migrate_all(int argc, const char **argv)
2937 printf("no server to migrate\n");
2941 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
2945 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_security_internals, argc, argv);
2949 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
2954 * 'net rpc share migrate' entrypoint.
2955 * @param argc Standard main() style argc
2956 * @param argv Standard main() style argv. Initial components are already
2959 static int rpc_share_migrate(int argc, const char **argv)
2962 struct functable func[] = {
2963 {"all", rpc_share_migrate_all},
2964 {"files", rpc_share_migrate_files},
2965 {"help", rpc_share_usage},
2966 /* {"security", rpc_share_migrate_security},*/
2967 {"shares", rpc_share_migrate_shares},
2971 return net_run_function(argc, argv, func, rpc_share_usage);
2975 * 'net rpc share' entrypoint.
2976 * @param argc Standard main() style argc
2977 * @param argv Standard main() style argv. Initial components are already
2981 int net_rpc_share(int argc, const char **argv)
2983 struct functable func[] = {
2984 {"add", rpc_share_add},
2985 {"delete", rpc_share_delete},
2986 {"migrate", rpc_share_migrate},
2991 return run_rpc_command(NULL, PI_SRVSVC, 0,
2992 rpc_share_list_internals,
2995 return net_run_function(argc, argv, func, rpc_share_usage);
2998 /****************************************************************************/
3000 static int rpc_file_usage(int argc, const char **argv)
3002 return net_help_file(argc, argv);
3006 * Close a file on a remote RPC server
3008 * All parameters are provided by the run_rpc_command function, except for
3009 * argc, argv which are passes through.
3011 * @param domain_sid The domain sid acquired from the remote server
3012 * @param cli A cli_state connected to the server.
3013 * @param mem_ctx Talloc context, destoyed on completion of the function.
3014 * @param argc Standard main() style argc
3015 * @param argv Standard main() style argv. Initial components are already
3018 * @return Normal NTSTATUS return.
3021 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
3022 struct cli_state *cli,
3023 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3026 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
3027 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3031 * Close a file on a remote RPC server
3033 * @param argc Standard main() style argc
3034 * @param argv Standard main() style argv. Initial components are already
3037 * @return A shell status integer (0 for success)
3039 static int rpc_file_close(int argc, const char **argv)
3042 DEBUG(1, ("No fileid given on close\n"));
3043 return(rpc_file_usage(argc, argv));
3046 return run_rpc_command(NULL, PI_SRVSVC, 0,
3047 rpc_file_close_internals,
3052 * Formatted print of open file info
3054 * @param info3 FILE_INFO_3 contents
3055 * @param str3 strings for FILE_INFO_3
3058 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
3060 fstring user = "", path = "";
3062 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
3063 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
3065 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
3066 info3->id, user, info3->perms, info3->num_locks, path);
3070 * List open files on a remote RPC server
3072 * All parameters are provided by the run_rpc_command function, except for
3073 * argc, argv which are passes through.
3075 * @param domain_sid The domain sid acquired from the remote server
3076 * @param cli A cli_state connected to the server.
3077 * @param mem_ctx Talloc context, destoyed on completion of the function.
3078 * @param argc Standard main() style argc
3079 * @param argv Standard main() style argv. Initial components are already
3082 * @return Normal NTSTATUS return.
3086 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
3087 struct cli_state *cli,
3088 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3090 SRV_FILE_INFO_CTR ctr;
3093 uint32 preferred_len = 0xffffffff, i;
3094 const char *username=NULL;
3096 init_enum_hnd(&hnd, 0);
3098 /* if argc > 0, must be user command */
3100 username = smb_xstrdup(argv[0]);
3102 result = cli_srvsvc_net_file_enum(
3103 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
3105 if (!W_ERROR_IS_OK(result))
3108 /* Display results */
3111 "\nEnumerating open files on remote server:\n\n"\
3112 "\nFileId Opened by Perms Locks Path"\
3113 "\n------ --------- ----- ----- ---- \n");
3114 for (i = 0; i < ctr.num_entries; i++)
3115 display_file_info_3(&ctr.file.info3[i].info_3,
3116 &ctr.file.info3[i].info_3_str);
3118 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3123 * List files for a user on a remote RPC server
3125 * @param argc Standard main() style argc
3126 * @param argv Standard main() style argv. Initial components are already
3129 * @return A shell status integer (0 for success)
3131 static int rpc_file_user(int argc, const char **argv)
3134 DEBUG(1, ("No username given\n"));
3135 return(rpc_file_usage(argc, argv));
3138 return run_rpc_command(NULL, PI_SRVSVC, 0,
3139 rpc_file_list_internals,
3145 * 'net rpc file' entrypoint.
3146 * @param argc Standard main() style argc
3147 * @param argv Standard main() style argv. Initial components are already
3151 int net_rpc_file(int argc, const char **argv)
3153 struct functable func[] = {
3154 {"close", rpc_file_close},
3155 {"user", rpc_file_user},
3157 {"info", rpc_file_info},
3163 return run_rpc_command(NULL, PI_SRVSVC, 0,
3164 rpc_file_list_internals,
3167 return net_run_function(argc, argv, func, rpc_file_usage);
3170 /****************************************************************************/
3175 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
3177 * All parameters are provided by the run_rpc_command function, except for
3178 * argc, argv which are passed through.
3180 * @param domain_sid The domain sid aquired from the remote server
3181 * @param cli A cli_state connected to the server.
3182 * @param mem_ctx Talloc context, destoyed on compleation of the function.
3183 * @param argc Standard main() style argc
3184 * @param argv Standard main() style argv. Initial components are already
3187 * @return Normal NTSTATUS return.
3190 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
3191 const char *domain_name,
3192 struct cli_state *cli,
3193 TALLOC_CTX *mem_ctx,
3194 int argc, const char **argv)
3196 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
3198 result = cli_shutdown_abort(cli, mem_ctx);
3200 if (NT_STATUS_IS_OK(result))
3201 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
3203 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
3210 * ABORT the shutdown of a remote RPC Server, over winreg pipe
3212 * All parameters are provided by the run_rpc_command function, except for
3213 * argc, argv which are passed through.
3215 * @param domain_sid The domain sid aquired from the remote server
3216 * @param cli A cli_state connected to the server.
3217 * @param mem_ctx Talloc context, destoyed on compleation of the function.
3218 * @param argc Standard main() style argc
3219 * @param argv Standard main() style argv. Initial components are already
3222 * @return Normal NTSTATUS return.
3225 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
3226 const char *domain_name,
3227 struct cli_state *cli,
3228 TALLOC_CTX *mem_ctx,
3229 int argc, const char **argv)
3231 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
3233 result = cli_reg_abort_shutdown(cli, mem_ctx);
3235 if (NT_STATUS_IS_OK(result))
3236 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
3238 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
3244 * ABORT the Shut down of a remote RPC server
3246 * @param argc Standard main() style argc
3247 * @param argv Standard main() style argv. Initial components are already
3250 * @return A shell status integer (0 for success)
3253 static int rpc_shutdown_abort(int argc, const char **argv)
3255 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
3256 rpc_shutdown_abort_internals,
3262 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
3264 return run_rpc_command(NULL, PI_WINREG, 0,
3265 rpc_reg_shutdown_abort_internals,
3270 * Shut down a remote RPC Server
3272 * All parameters are provided by the run_rpc_command function, except for
3273 * argc, argv which are passes through.
3275 * @param domain_sid The domain sid aquired from the remote server
3276 * @param cli A cli_state connected to the server.
3277 * @param mem_ctx Talloc context, destoyed on compleation of the function.
3278 * @param argc Standard main() style argc
3279 * @param argc Standard main() style argv. Initial components are already
3282 * @return Normal NTSTATUS return.
3285 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid,
3286 const char *domain_name,
3287 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3288 int argc, const char **argv)
3290 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
3291 const char *msg = "This machine will be shutdown shortly";
3292 uint32 timeout = 20;
3297 struct poptOption long_options[] = {
3298 {"message", 'm', POPT_ARG_STRING, &msg},
3299 {"timeout", 't', POPT_ARG_INT, &timeout},
3300 {"reboot", 'r', POPT_ARG_NONE, &reboot},
3301 {"force", 'f', POPT_ARG_NONE, &force},
3305 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
3306 POPT_CONTEXT_KEEP_FIRST);
3308 rc = poptGetNextOpt(pc);
3311 /* an error occurred during option processing */
3312 DEBUG(0, ("%s: %s\n",
3313 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
3315 return NT_STATUS_INVALID_PARAMETER;
3322 timeout = opt_timeout;
3325 /* create an entry */
3326 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
3328 if (NT_STATUS_IS_OK(result))
3329 DEBUG(5,("Shutdown of remote machine succeeded\n"));
3331 DEBUG(0,("Shutdown of remote machine failed!\n"));
3337 * Shut down a remote RPC server
3339 * @param argc Standard main() style argc
3340 * @param argc Standard main() style argv. Initial components are already
3343 * @return A shell status integer (0 for success)
3346 static int rpc_shutdown(int argc, const char **argv)
3348 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
3352 /***************************************************************************
3353 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
3355 ***************************************************************************/
3358 * Add interdomain trust account to the RPC server.
3359 * All parameters (except for argc and argv) are passed by run_rpc_command
3362 * @param domain_sid The domain sid acquired from the server
3363 * @param cli A cli_state connected to the server.
3364 * @param mem_ctx Talloc context, destoyed on completion of the function.
3365 * @param argc Standard main() style argc
3366 * @param argc Standard main() style argv. Initial components are already
3369 * @return normal NTSTATUS return code
3372 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
3373 const char *domain_name,
3374 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3375 int argc, const char **argv) {
3377 POLICY_HND connect_pol, domain_pol, user_pol;
3378 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
3381 uint32 unknown, user_rid;
3384 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
3385 return NT_STATUS_INVALID_PARAMETER;
3389 * Make valid trusting domain account (ie. uppercased and with '$' appended)
3392 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
3393 return NT_STATUS_NO_MEMORY;
3396 strupper_m(acct_name);
3398 /* Get samr policy handle */
3399 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
3401 if (!NT_STATUS_IS_OK(result)) {
3405 /* Get domain policy handle */
3406 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
3407 MAXIMUM_ALLOWED_ACCESS,
3408 domain_sid, &domain_pol);
3409 if (!NT_STATUS_IS_OK(result)) {
3413 /* Create trusting domain's account */
3414 acb_info = ACB_DOMTRUST;
3415 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
3416 mimir: yes, most probably it is */
3418 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
3419 acct_name, acb_info, unknown,
3420 &user_pol, &user_rid);
3421 if (!NT_STATUS_IS_OK(result)) {
3426 SAM_USERINFO_CTR ctr;
3427 SAM_USER_INFO_24 p24;
3430 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
3435 init_sam_user_info24(&p24, (char *)pwbuf, 24);
3437 ctr.switch_value = 24;
3438 ctr.info.id24 = &p24;
3440 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
3441 &cli->user_session_key, &ctr);
3443 if (!NT_STATUS_IS_OK(result)) {
3444 DEBUG(0,("Could not set trust account password: %s\n",
3445 nt_errstr(result)));
3451 SAFE_FREE(acct_name);
3456 * Create interdomain trust account for a remote domain.
3458 * @param argc standard argc
3459 * @param argv standard argv without initial components
3461 * @return Integer status (0 means success)
3464 static int rpc_trustdom_add(int argc, const char **argv)
3467 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
3470 d_printf("Usage: net rpc trustdom add <domain>\n");
3477 * Delete interdomain trust account for a remote domain.
3479 * @param argc standard argc
3480 * @param argv standard argv without initial components
3482 * @return Integer status (0 means success)
3485 static int rpc_trustdom_del(int argc, const char **argv)
3487 d_printf("Sorry, not yet implemented.\n");
3488 d_printf("Use 'smbpasswd -x -i' instead.\n");
3494 * Establish trust relationship to a trusting domain.
3495 * Interdomain account must already be created on remote PDC.
3497 * @param argc standard argc
3498 * @param argv standard argv without initial components
3500 * @return Integer status (0 means success)
3503 static int rpc_trustdom_establish(int argc, const char **argv)
3505 struct cli_state *cli;
3506 struct in_addr server_ip;
3507 POLICY_HND connect_hnd;
3508 TALLOC_CTX *mem_ctx;
3510 DOM_SID *domain_sid;
3511 WKS_INFO_100 wks_info;
3514 char* domain_name_pol;
3519 * Connect to \\server\ipc$ as 'our domain' account with password
3523 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
3527 domain_name = smb_xstrdup(argv[0]);
3528 strupper_m(domain_name);
3530 /* account name used at first is our domain's name with '$' */
3531 asprintf(&acct_name, "%s$", lp_workgroup());
3532 strupper_m(acct_name);
3535 * opt_workgroup will be used by connection functions further,
3536 * hence it should be set to remote domain name instead of ours
3538 if (opt_workgroup) {
3539 opt_workgroup = smb_xstrdup(domain_name);
3542 opt_user_name = acct_name;
3544 /* find the domain controller */
3545 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
3546 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
3550 /* connect to ipc$ as username/password */
3551 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
3552 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
3554 /* Is it trusting domain account for sure ? */
3555 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
3556 nt_errstr(nt_status)));
3561 * Connect to \\server\ipc$ again (this time anonymously)
3564 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
3566 if (NT_STATUS_IS_ERR(nt_status)) {
3567 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
3568 domain_name, nt_errstr(nt_status)));
3572 * Use NetServerEnum2 to make sure we're talking to a proper server
3575 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
3576 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
3577 for domain %s\n", domain_name));
3581 * Call WksQueryInfo to check remote server's capabilities
3582 * note: It is now used only to get unicode domain name
3585 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
3586 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
3590 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
3592 DEBUG(0, ("talloc_init() failed\n"));
3597 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
3599 if (NT_STATUS_IS_ERR(nt_status)) {
3600 DEBUG(0, ("WksQueryInfo call failed.\n"));
3604 if (cli->nt_pipe_fnum)
3605 cli_nt_session_close(cli);
3609 * Call LsaOpenPolicy and LsaQueryInfo
3612 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
3613 DEBUG(0, ("talloc_init() failed\n"));
3618 if (!cli_nt_session_open(cli, PI_LSARPC)) {
3619 DEBUG(0, ("Could not initialise lsa pipe\n"));
3624 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
3626 if (NT_STATUS_IS_ERR(nt_status)) {
3627 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
3628 nt_errstr(nt_status)));
3632 /* Querying info level 5 */
3634 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
3635 5 /* info level */, &domain_name_pol,
3637 if (NT_STATUS_IS_ERR(nt_status)) {
3638 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
3639 nt_errstr(nt_status)));
3646 /* There should be actually query info level 3 (following nt serv behaviour),
3647 but I still don't know if it's _really_ necessary */
3650 * Store the password in secrets db
3653 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
3654 wks_info.uni_lan_grp.uni_str_len, opt_password,
3656 DEBUG(0, ("Storing password for trusted domain failed.\n"));
3661 * Close the pipes and clean up
3664 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
3665 if (NT_STATUS_IS_ERR(nt_status)) {
3666 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
3667 nt_errstr(nt_status)));
3671 if (cli->nt_pipe_fnum)
3672 cli_nt_session_close(cli);
3674 talloc_destroy(mem_ctx);
3676 d_printf("Trust to domain %s established\n", domain_name);
3681 * Revoke trust relationship to the remote domain
3683 * @param argc standard argc
3684 * @param argv standard argv without initial components
3686 * @return Integer status (0 means success)
3689 static int rpc_trustdom_revoke(int argc, const char **argv)
3693 if (argc < 1) return -1;
3695 /* generate upper cased domain name */
3696 domain_name = smb_xstrdup(argv[0]);
3697 strupper_m(domain_name);
3699 /* delete password of the trust */
3700 if (!trusted_domain_password_delete(domain_name)) {
3701 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
3710 * Usage for 'net rpc trustdom' command
3712 * @param argc standard argc
3713 * @param argv standard argv without inital components
3715 * @return Integer status returned to shell
3718 static int rpc_trustdom_usage(int argc, const char **argv)
3720 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
3721 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
3722 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
3723 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
3724 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
3729 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
3730 const char *domain_name,
3731 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3732 int argc, const char **argv)
3735 sid_to_string(str_sid, domain_sid);
3736 d_printf("%s\n", str_sid);
3737 return NT_STATUS_OK;
3741 static int rpc_trustdom_list(int argc, const char **argv)
3743 /* common variables */
3744 TALLOC_CTX* mem_ctx;
3745 struct cli_state *cli, *remote_cli;
3747 const char *domain_name = NULL;
3748 DOM_SID *queried_dom_sid;
3749 fstring ascii_sid, padding;
3750 int ascii_dom_name_len;
3751 POLICY_HND connect_hnd;
3753 /* trusted domains listing variables */
3754 unsigned int num_domains, enum_ctx = 0;
3755 int i, pad_len, col_len = 20;
3756 DOM_SID *domain_sids;
3757 char **trusted_dom_names;
3761 /* trusting domains listing variables */
3762 POLICY_HND domain_hnd;
3763 char **trusting_dom_names;
3764 uint32 *trusting_dom_rids;
3767 * Listing trusted domains (stored in secrets.tdb, if local)
3770 mem_ctx = talloc_init("trust relationships listing");
3773 * set domain and pdc name to local samba server (default)
3774 * or to remote one given in command line
3777 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
3778 domain_name = opt_workgroup;
3779 opt_target_workgroup = opt_workgroup;
3781 fstrcpy(pdc_name, global_myname());
3782 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
3783 opt_target_workgroup = domain_name;
3786 /* open \PIPE\lsarpc and open policy handle */
3787 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
3788 DEBUG(0, ("Couldn't connect to domain controller\n"));
3792 if (!cli_nt_session_open(cli, PI_LSARPC)) {
3793 DEBUG(0, ("Could not initialise lsa pipe\n"));
3797 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
3799 if (NT_STATUS_IS_ERR(nt_status)) {
3800 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
3801 nt_errstr(nt_status)));
3805 /* query info level 5 to obtain sid of a domain being queried */
3806 nt_status = cli_lsa_query_info_policy(
3807 cli, mem_ctx, &connect_hnd, 5 /* info level */,
3808 &dummy, &queried_dom_sid);
3810 if (NT_STATUS_IS_ERR(nt_status)) {
3811 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
3812 nt_errstr(nt_status)));
3817 * Keep calling LsaEnumTrustdom over opened pipe until
3818 * the end of enumeration is reached
3821 d_printf("Trusted domains list:\n\n");
3824 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
3826 &trusted_dom_names, &domain_sids);
3828 if (NT_STATUS_IS_ERR(nt_status)) {
3829 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
3830 nt_errstr(nt_status)));
3834 for (i = 0; i < num_domains; i++) {
3835 /* convert sid into ascii string */
3836 sid_to_string(ascii_sid, &(domain_sids[i]));
3838 /* calculate padding space for d_printf to look nicer */
3839 pad_len = col_len - strlen(trusted_dom_names[i]);
3840 padding[pad_len] = 0;
3841 do padding[--pad_len] = ' '; while (pad_len);
3843 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
3847 * in case of no trusted domains say something rather
3848 * than just display blank line
3850 if (!num_domains) d_printf("none\n");
3852 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
3854 /* close this connection before doing next one */
3855 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
3856 if (NT_STATUS_IS_ERR(nt_status)) {
3857 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
3858 nt_errstr(nt_status)));
3862 cli_nt_session_close(cli);
3865 * Listing trusting domains (stored in passdb backend, if local)
3868 d_printf("\nTrusting domains list:\n\n");
3871 * Open \PIPE\samr and get needed policy handles
3873 if (!cli_nt_session_open(cli, PI_SAMR)) {
3874 DEBUG(0, ("Could not initialise samr pipe\n"));
3879 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
3881 if (!NT_STATUS_IS_OK(nt_status)) {
3882 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
3883 nt_errstr(nt_status)));
3887 /* SamrOpenDomain - we have to open domain policy handle in order to be
3888 able to enumerate accounts*/
3889 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
3890 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
3891 queried_dom_sid, &domain_hnd);
3892 if (!NT_STATUS_IS_OK(nt_status)) {
3893 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
3894 nt_errstr(nt_status)));
3899 * perform actual enumeration
3902 enum_ctx = 0; /* reset enumeration context from last enumeration */
3905 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
3906 &enum_ctx, ACB_DOMTRUST, 0xffff,
3907 &trusting_dom_names, &trusting_dom_rids,
3909 if (NT_STATUS_IS_ERR(nt_status)) {
3910 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
3911 nt_errstr(nt_status)));
3915 for (i = 0; i < num_domains; i++) {
3918 * get each single domain's sid (do we _really_ need this ?):
3919 * 1) connect to domain's pdc
3920 * 2) query the pdc for domain's sid
3923 /* get rid of '$' tail */
3924 ascii_dom_name_len = strlen(trusting_dom_names[i]);
3925 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
3926 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
3928 /* calculate padding space for d_printf to look nicer */
3929 pad_len = col_len - strlen(trusting_dom_names[i]);
3930 padding[pad_len] = 0;
3931 do padding[--pad_len] = ' '; while (pad_len);
3933 /* set opt_* variables to remote domain */
3934 strupper_m(trusting_dom_names[i]);
3935 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
3936 opt_target_workgroup = opt_workgroup;
3938 d_printf("%s%s", trusting_dom_names[i], padding);
3940 /* connect to remote domain controller */
3941 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
3943 /* query for domain's sid */
3944 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
3945 d_printf("couldn't get domain's sid\n");
3947 cli_shutdown(remote_cli);
3950 d_printf("domain controller is not responding\n");
3954 if (!num_domains) d_printf("none\n");
3956 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
3958 /* close opened samr and domain policy handles */
3959 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
3960 if (!NT_STATUS_IS_OK(nt_status)) {
3961 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
3964 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
3965 if (!NT_STATUS_IS_OK(nt_status)) {
3966 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
3969 /* close samr pipe and connection to IPC$ */
3970 cli_nt_session_close(cli);
3973 talloc_destroy(mem_ctx);
3978 * Entrypoint for 'net rpc trustdom' code
3980 * @param argc standard argc
3981 * @param argv standard argv without initial components
3983 * @return Integer status (0 means success)
3986 static int rpc_trustdom(int argc, const char **argv)
3988 struct functable func[] = {
3989 {"add", rpc_trustdom_add},
3990 {"del", rpc_trustdom_del},
3991 {"establish", rpc_trustdom_establish},
3992 {"revoke", rpc_trustdom_revoke},
3993 {"help", rpc_trustdom_usage},
3994 {"list", rpc_trustdom_list},
3999 rpc_trustdom_usage(argc, argv);
4003 return (net_run_function(argc, argv, func, rpc_user_usage));
4007 * Check if a server will take rpc commands
4008 * @param flags Type of server to connect to (PDC, DMB, localhost)
4009 * if the host is not explicitly specified
4010 * @return BOOL (true means rpc supported)
4012 BOOL net_rpc_check(unsigned flags)
4014 struct cli_state cli;
4016 struct in_addr server_ip;
4017 char *server_name = NULL;
4019 /* flags (i.e. server type) may depend on command */
4020 if (!net_find_server(flags, &server_ip, &server_name))
4024 if (cli_initialise(&cli) == False)
4027 if (!cli_connect(&cli, server_name, &server_ip))
4029 if (!attempt_netbios_session_request(&cli, global_myname(),
4030 server_name, &server_ip))
4032 if (!cli_negprot(&cli))
4034 if (cli.protocol < PROTOCOL_NT1)
4043 /* dump sam database via samsync rpc calls */
4044 static int rpc_samdump(int argc, const char **argv) {
4045 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
4049 /* syncronise sam database via samsync rpc calls */
4050 static int rpc_vampire(int argc, const char **argv) {
4051 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
4056 * Migrate everything from a print-server
4058 * @param argc Standard main() style argc
4059 * @param argv Standard main() style argv. Initial components are already
4062 * @return A shell status integer (0 for success)
4064 * The order is important !
4065 * To successfully add drivers the print-queues have to exist !
4066 * Applying ACLs should be the last step, because you're easily locked out
4069 static int rpc_printer_migrate_all(int argc, const char **argv)
4074 printf("no server to migrate\n");
4078 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
4082 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
4086 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
4090 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
4094 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
4099 * Migrate print-drivers from a print-server
4101 * @param argc Standard main() style argc
4102 * @param argv Standard main() style argv. Initial components are already
4105 * @return A shell status integer (0 for success)
4107 static int rpc_printer_migrate_drivers(int argc, const char **argv)
4110 printf("no server to migrate\n");
4114 return run_rpc_command(NULL, PI_SPOOLSS, 0,
4115 rpc_printer_migrate_drivers_internals,
4120 * Migrate print-forms from a print-server
4122 * @param argc Standard main() style argc
4123 * @param argv Standard main() style argv. Initial components are already
4126 * @return A shell status integer (0 for success)
4128 static int rpc_printer_migrate_forms(int argc, const char **argv)
4131 printf("no server to migrate\n");
4135 return run_rpc_command(NULL, PI_SPOOLSS, 0,
4136 rpc_printer_migrate_forms_internals,
4141 * Migrate printers from a print-server
4143 * @param argc Standard main() style argc
4144 * @param argv Standard main() style argv. Initial components are already
4147 * @return A shell status integer (0 for success)
4149 static int rpc_printer_migrate_printers(int argc, const char **argv)
4152 printf("no server to migrate\n");
4156 return run_rpc_command(NULL, PI_SPOOLSS, 0,
4157 rpc_printer_migrate_printers_internals,
4162 * Migrate printer-ACLs from a print-server
4164 * @param argc Standard main() style argc
4165 * @param argv Standard main() style argv. Initial components are already
4168 * @return A shell status integer (0 for success)
4170 static int rpc_printer_migrate_security(int argc, const char **argv)
4173 printf("no server to migrate\n");
4177 return run_rpc_command(NULL, PI_SPOOLSS, 0,
4178 rpc_printer_migrate_security_internals,
4183 * Migrate printer-settings from a print-server
4185 * @param argc Standard main() style argc
4186 * @param argv Standard main() style argv. Initial components are already
4189 * @return A shell status integer (0 for success)
4191 static int rpc_printer_migrate_settings(int argc, const char **argv)
4194 printf("no server to migrate\n");
4198 return run_rpc_command(NULL, PI_SPOOLSS, 0,
4199 rpc_printer_migrate_settings_internals,
4204 * 'net rpc printer' entrypoint.
4205 * @param argc Standard main() style argc
4206 * @param argv Standard main() style argv. Initial components are already
4210 int rpc_printer_migrate(int argc, const char **argv)
4213 /* ouch: when addriver and setdriver are called from within
4214 rpc_printer_migrate_drivers_internals, the printer-queue already
4217 struct functable func[] = {
4218 {"all", rpc_printer_migrate_all},
4219 {"drivers", rpc_printer_migrate_drivers},
4220 {"forms", rpc_printer_migrate_forms},
4221 {"help", rpc_printer_usage},
4222 {"printers", rpc_printer_migrate_printers},
4223 {"security", rpc_printer_migrate_security},
4224 {"settings", rpc_printer_migrate_settings},
4228 return net_run_function(argc, argv, func, rpc_printer_usage);
4233 * List printers on a remote RPC server
4235 * @param argc Standard main() style argc
4236 * @param argv Standard main() style argv. Initial components are already
4239 * @return A shell status integer (0 for success)
4241 static int rpc_printer_list(int argc, const char **argv)
4244 return run_rpc_command(NULL, PI_SPOOLSS, 0,
4245 rpc_printer_list_internals,
4250 * List printer-drivers on a remote RPC server
4252 * @param argc Standard main() style argc
4253 * @param argv Standard main() style argv. Initial components are already
4256 * @return A shell status integer (0 for success)
4258 static int rpc_printer_driver_list(int argc, const char **argv)
4261 return run_rpc_command(NULL, PI_SPOOLSS, 0,
4262 rpc_printer_driver_list_internals,
4267 * Display rpc printer help page.
4268 * @param argc Standard main() style argc
4269 * @param argv Standard main() style argv. Initial components are already
4272 int rpc_printer_usage(int argc, const char **argv)
4274 return net_help_printer(argc, argv);
4278 * 'net rpc printer' entrypoint.
4279 * @param argc Standard main() style argc
4280 * @param argv Standard main() style argv. Initial components are already
4283 int net_rpc_printer(int argc, const char **argv)
4285 struct functable func[] = {
4286 {"list", rpc_printer_list},
4287 {"migrate", rpc_printer_migrate},
4288 {"driver", rpc_printer_driver_list},
4293 return run_rpc_command(NULL, PI_SPOOLSS, 0,
4294 rpc_printer_list_internals,
4297 return net_run_function(argc, argv, func, rpc_printer_usage);
4300 /****************************************************************************/
4304 * Basic usage function for 'net rpc'
4305 * @param argc Standard main() style argc
4306 * @param argv Standard main() style argv. Initial components are already
4310 int net_rpc_usage(int argc, const char **argv)
4312 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
4313 d_printf(" net rpc join \t\t\tto join a domain \n");
4314 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
4315 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
4316 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
4317 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass");
4318 d_printf(" net rpc group \t\tto list groups\n");
4319 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
4320 d_printf(" net rpc printer \t\tto list and migrate printers\n");
4321 d_printf(" net rpc file \t\t\tto list open files\n");
4322 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
4323 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
4324 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
4325 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
4326 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
4327 "\t\t\t\t\tor establish trust\n");
4328 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
4329 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
4331 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
4332 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
4333 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
4334 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
4335 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
4341 * Help function for 'net rpc'. Calls command specific help if requested
4342 * or displays usage of net rpc
4343 * @param argc Standard main() style argc
4344 * @param argv Standard main() style argv. Initial components are already
4348 int net_rpc_help(int argc, const char **argv)
4350 struct functable func[] = {
4351 {"join", rpc_join_usage},
4352 {"user", rpc_user_usage},
4353 {"group", rpc_group_usage},
4354 {"share", rpc_share_usage},
4355 /*{"changetrustpw", rpc_changetrustpw_usage}, */
4356 {"trustdom", rpc_trustdom_usage},
4357 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
4358 /*{"shutdown", rpc_shutdown_usage}, */
4363 net_rpc_usage(argc, argv);
4367 return (net_run_function(argc, argv, func, rpc_user_usage));
4372 * 'net rpc' entrypoint.
4373 * @param argc Standard main() style argc
4374 * @param argv Standard main() style argv. Initial components are already
4378 int net_rpc(int argc, const char **argv)
4380 struct functable func[] = {
4381 {"info", net_rpc_info},
4382 {"join", net_rpc_join},
4383 {"oldjoin", net_rpc_oldjoin},
4384 {"testjoin", net_rpc_testjoin},
4385 {"user", net_rpc_user},
4386 {"password", rpc_user_password},
4387 {"group", net_rpc_group},
4388 {"share", net_rpc_share},
4389 {"file", net_rpc_file},
4390 {"printer", net_rpc_printer},
4391 {"changetrustpw", net_rpc_changetrustpw},
4392 {"trustdom", rpc_trustdom},
4393 {"abortshutdown", rpc_shutdown_abort},
4394 {"shutdown", rpc_shutdown},
4395 {"samdump", rpc_samdump},
4396 {"vampire", rpc_vampire},
4397 {"getsid", net_rpc_getsid},
4398 {"help", net_rpc_help},
4401 return net_run_function(argc, argv, func, net_rpc_usage);