2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
25 #include "../libcli/auth/libcli_auth.h"
27 static int net_mode_share;
28 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
33 * @brief RPC based subcommands for the 'net' utility.
35 * This file should contain much of the functionality that used to
36 * be found in rpcclient, execpt that the commands should change
37 * less often, and the fucntionality should be sane (the user is not
38 * expected to know a rid/sid before they conduct an operation etc.)
40 * @todo Perhaps eventually these should be split out into a number
41 * of files, as this could get quite big.
46 * Many of the RPC functions need the domain sid. This function gets
47 * it at the start of every run
49 * @param cli A cli_state already connected to the remote machine
51 * @return The Domain SID of the remote machine.
54 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
56 const char **domain_name)
58 struct rpc_pipe_client *lsa_pipe = NULL;
59 struct policy_handle pol;
60 NTSTATUS result = NT_STATUS_OK;
61 union lsa_PolicyInformation *info = NULL;
63 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
65 if (!NT_STATUS_IS_OK(result)) {
66 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
70 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
71 SEC_FLAG_MAXIMUM_ALLOWED,
73 if (!NT_STATUS_IS_OK(result)) {
74 d_fprintf(stderr, _("open_policy failed: %s\n"),
79 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
81 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
83 if (!NT_STATUS_IS_OK(result)) {
84 d_fprintf(stderr, _("lsaquery failed: %s\n"),
89 *domain_name = info->account_domain.name.string;
90 *domain_sid = info->account_domain.sid;
92 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
93 TALLOC_FREE(lsa_pipe);
99 * Run a single RPC command, from start to finish.
101 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
102 * @param conn_flag a NET_FLAG_ combination. Passed to
103 * net_make_ipc_connection.
104 * @param argc Standard main() style argc.
105 * @param argv Standard main() style argv. Initial components are already
107 * @return A shell status integer (0 for success).
110 int run_rpc_command(struct net_context *c,
111 struct cli_state *cli_arg,
112 const struct ndr_syntax_id *interface,
118 struct cli_state *cli = NULL;
119 struct rpc_pipe_client *pipe_hnd = NULL;
123 const char *domain_name;
126 /* make use of cli_state handed over as an argument, if possible */
128 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
129 if (!NT_STATUS_IS_OK(nt_status)) {
130 DEBUG(1, ("failed to make ipc connection: %s\n",
131 nt_errstr(nt_status)));
144 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
145 DEBUG(0, ("talloc_init() failed\n"));
149 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
151 if (!NT_STATUS_IS_OK(nt_status)) {
155 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
156 if (lp_client_schannel()
157 && (ndr_syntax_id_equal(interface,
158 &ndr_table_netlogon.syntax_id))) {
159 /* Always try and create an schannel netlogon pipe. */
160 nt_status = cli_rpc_pipe_open_schannel(
162 PIPE_AUTH_LEVEL_PRIVACY, domain_name,
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
166 nt_errstr(nt_status) ));
170 if (conn_flags & NET_FLAGS_SEAL) {
171 nt_status = cli_rpc_pipe_open_ntlmssp(
173 PIPE_AUTH_LEVEL_PRIVACY,
174 lp_workgroup(), c->opt_user_name,
175 c->opt_password, &pipe_hnd);
177 nt_status = cli_rpc_pipe_open_noauth(
181 if (!NT_STATUS_IS_OK(nt_status)) {
182 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
183 get_pipe_name_from_iface(interface),
184 nt_errstr(nt_status) ));
190 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
192 if (!NT_STATUS_IS_OK(nt_status)) {
193 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
196 DEBUG(5, ("rpc command function succedded\n"));
199 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
201 TALLOC_FREE(pipe_hnd);
206 /* close the connection only if it was opened here */
211 talloc_destroy(mem_ctx);
216 * Force a change of the trust acccount password.
218 * All parameters are provided by the run_rpc_command function, except for
219 * argc, argv which are passed through.
221 * @param domain_sid The domain sid acquired from the remote server.
222 * @param cli A cli_state connected to the server.
223 * @param mem_ctx Talloc context, destroyed on completion of the function.
224 * @param argc Standard main() style argc.
225 * @param argv Standard main() style argv. Initial components are already
228 * @return Normal NTSTATUS return.
231 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
232 const DOM_SID *domain_sid,
233 const char *domain_name,
234 struct cli_state *cli,
235 struct rpc_pipe_client *pipe_hnd,
241 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
245 * Force a change of the trust acccount password.
247 * @param argc Standard main() style argc.
248 * @param argv Standard main() style argv. Initial components are already
251 * @return A shell status integer (0 for success).
254 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
256 if (c->display_usage) {
257 d_printf(_("Usage:\n"
258 "net rpc changetrustpw\n"
259 " Change the machine trust password\n"));
263 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
264 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
265 rpc_changetrustpw_internals,
270 * Join a domain, the old way.
272 * This uses 'machinename' as the inital password, and changes it.
274 * The password should be created with 'server manager' or equiv first.
276 * All parameters are provided by the run_rpc_command function, except for
277 * argc, argv which are passed through.
279 * @param domain_sid The domain sid acquired from the remote server.
280 * @param cli A cli_state connected to the server.
281 * @param mem_ctx Talloc context, destroyed on completion of the function.
282 * @param argc Standard main() style argc.
283 * @param argv Standard main() style argv. Initial components are already
286 * @return Normal NTSTATUS return.
289 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
290 const DOM_SID *domain_sid,
291 const char *domain_name,
292 struct cli_state *cli,
293 struct rpc_pipe_client *pipe_hnd,
299 fstring trust_passwd;
300 unsigned char orig_trust_passwd_hash[16];
302 uint32 sec_channel_type;
304 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
306 if (!NT_STATUS_IS_OK(result)) {
307 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
310 nt_errstr(result) ));
315 check what type of join - if the user want's to join as
316 a BDC, the server must agree that we are a BDC.
319 sec_channel_type = get_sec_channel_type(argv[0]);
321 sec_channel_type = get_sec_channel_type(NULL);
324 fstrcpy(trust_passwd, global_myname());
325 strlower_m(trust_passwd);
328 * Machine names can be 15 characters, but the max length on
329 * a password is 14. --jerry
332 trust_passwd[14] = '\0';
334 E_md4hash(trust_passwd, orig_trust_passwd_hash);
336 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
337 orig_trust_passwd_hash,
340 if (NT_STATUS_IS_OK(result))
341 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
344 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
345 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
346 result = NT_STATUS_UNSUCCESSFUL;
353 * Join a domain, the old way.
355 * @param argc Standard main() style argc.
356 * @param argv Standard main() style argv. Initial components are already
359 * @return A shell status integer (0 for success).
362 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
364 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
365 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
366 rpc_oldjoin_internals,
371 * Join a domain, the old way. This function exists to allow
372 * the message to be displayed when oldjoin was explicitly
373 * requested, but not when it was implied by "net rpc join".
375 * @param argc Standard main() style argc.
376 * @param argv Standard main() style argv. Initial components are already
379 * @return A shell status integer (0 for success).
382 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
386 if (c->display_usage) {
387 d_printf(_("Usage:\n"
389 " Join a domain the old way\n"));
393 rc = net_rpc_perform_oldjoin(c, argc, argv);
396 d_fprintf(stderr, _("Failed to join domain\n"));
403 * 'net rpc join' entrypoint.
404 * @param argc Standard main() style argc.
405 * @param argv Standard main() style argv. Initial components are already
408 * Main 'net_rpc_join()' (where the admin username/password is used) is
410 * Try to just change the password, but if that doesn't work, use/prompt
411 * for a username/password.
414 int net_rpc_join(struct net_context *c, int argc, const char **argv)
416 if (c->display_usage) {
417 d_printf(_("Usage:\n"
418 "net rpc join -U <username>[%%password] <type>\n"
420 " username\tName of the admin user"
421 " password\tPassword of the admin user, will "
422 "prompt if not specified\n"
423 " type\tCan be one of the following:\n"
424 "\t\tMEMBER\tJoin as member server (default)\n"
425 "\t\tBDC\tJoin as BDC\n"
426 "\t\tPDC\tJoin as PDC\n"));
430 if (lp_server_role() == ROLE_STANDALONE) {
431 d_printf(_("cannot join as standalone machine\n"));
435 if (strlen(global_myname()) > 15) {
436 d_printf(_("Our netbios name can be at most 15 chars long, "
437 "\"%s\" is %u chars long\n"),
438 global_myname(), (unsigned int)strlen(global_myname()));
442 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
445 return net_rpc_join_newstyle(c, argc, argv);
449 * display info about a rpc domain
451 * All parameters are provided by the run_rpc_command function, except for
452 * argc, argv which are passed through.
454 * @param domain_sid The domain sid acquired from the remote server
455 * @param cli A cli_state connected to the server.
456 * @param mem_ctx Talloc context, destroyed on completion of the function.
457 * @param argc Standard main() style argc.
458 * @param argv Standard main() style argv. Initial components are already
461 * @return Normal NTSTATUS return.
464 NTSTATUS rpc_info_internals(struct net_context *c,
465 const DOM_SID *domain_sid,
466 const char *domain_name,
467 struct cli_state *cli,
468 struct rpc_pipe_client *pipe_hnd,
473 struct policy_handle connect_pol, domain_pol;
474 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
475 union samr_DomainInfo *info = NULL;
478 sid_to_fstring(sid_str, domain_sid);
480 /* Get sam policy handle */
481 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
483 MAXIMUM_ALLOWED_ACCESS,
485 if (!NT_STATUS_IS_OK(result)) {
486 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
491 /* Get domain policy handle */
492 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
494 MAXIMUM_ALLOWED_ACCESS,
495 CONST_DISCARD(struct dom_sid2 *, domain_sid),
497 if (!NT_STATUS_IS_OK(result)) {
498 d_fprintf(stderr, _("Could not open domain: %s\n"),
503 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
507 if (NT_STATUS_IS_OK(result)) {
508 d_printf(_("Domain Name: %s\n"),
509 info->general.domain_name.string);
510 d_printf(_("Domain SID: %s\n"), sid_str);
511 d_printf(_("Sequence number: %llu\n"),
512 (unsigned long long)info->general.sequence_num);
513 d_printf(_("Num users: %u\n"), info->general.num_users);
514 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
515 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
523 * 'net rpc info' entrypoint.
524 * @param argc Standard main() style argc.
525 * @param argv Standard main() style argv. Initial components are already
529 int net_rpc_info(struct net_context *c, int argc, const char **argv)
531 if (c->display_usage) {
532 d_printf(_("Usage:\n"
534 " Display information about the domain\n"));
538 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
539 NET_FLAGS_PDC, rpc_info_internals,
544 * Fetch domain SID into the local secrets.tdb.
546 * All parameters are provided by the run_rpc_command function, except for
547 * argc, argv which are passed through.
549 * @param domain_sid The domain sid acquired from the remote server.
550 * @param cli A cli_state connected to the server.
551 * @param mem_ctx Talloc context, destroyed on completion of the function.
552 * @param argc Standard main() style argc.
553 * @param argv Standard main() style argv. Initial components are already
556 * @return Normal NTSTATUS return.
559 static NTSTATUS rpc_getsid_internals(struct net_context *c,
560 const DOM_SID *domain_sid,
561 const char *domain_name,
562 struct cli_state *cli,
563 struct rpc_pipe_client *pipe_hnd,
570 sid_to_fstring(sid_str, domain_sid);
571 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
572 sid_str, domain_name);
574 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
575 DEBUG(0,("Can't store domain SID\n"));
576 return NT_STATUS_UNSUCCESSFUL;
583 * 'net rpc getsid' entrypoint.
584 * @param argc Standard main() style argc.
585 * @param argv Standard main() style argv. Initial components are already
589 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
591 if (c->display_usage) {
592 d_printf(_("Usage:\n"
594 " Fetch domain SID into local secrets.tdb\n"));
598 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
599 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
600 rpc_getsid_internals,
604 /****************************************************************************/
607 * Basic usage function for 'net rpc user'.
608 * @param argc Standard main() style argc.
609 * @param argv Standard main() style argv. Initial components are already
613 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
615 return net_user_usage(c, argc, argv);
619 * Add a new user to a remote RPC server.
621 * @param argc Standard main() style argc.
622 * @param argv Standard main() style argv. Initial components are already
625 * @return A shell status integer (0 for success).
628 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
630 NET_API_STATUS status;
631 struct USER_INFO_1 info1;
632 uint32_t parm_error = 0;
634 if (argc < 1 || c->display_usage) {
635 rpc_user_usage(c, argc, argv);
641 info1.usri1_name = argv[0];
643 info1.usri1_password = argv[1];
646 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
649 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
650 argv[0], libnetapi_get_error_string(c->netapi_ctx,
654 d_printf(_("Added user '%s'.\n"), argv[0]);
661 * Rename a user on a remote RPC server.
663 * @param argc Standard main() style argc.
664 * @param argv Standard main() style argv. Initial components are already
667 * @return A shell status integer (0 for success).
670 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
672 NET_API_STATUS status;
673 struct USER_INFO_0 u0;
674 uint32_t parm_err = 0;
676 if (argc != 2 || c->display_usage) {
677 rpc_user_usage(c, argc, argv);
681 u0.usri0_name = argv[1];
683 status = NetUserSetInfo(c->opt_host, argv[0],
684 0, (uint8_t *)&u0, &parm_err);
687 _("Failed to rename user from %s to %s - %s\n"),
689 libnetapi_get_error_string(c->netapi_ctx, status));
691 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
698 * Delete a user from a remote RPC server.
700 * @param argc Standard main() style argc.
701 * @param argv Standard main() style argv. Initial components are already
704 * @return A shell status integer (0 for success).
707 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
709 NET_API_STATUS status;
711 if (argc < 1 || c->display_usage) {
712 rpc_user_usage(c, argc, argv);
716 status = NetUserDel(c->opt_host, argv[0]);
719 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
721 libnetapi_get_error_string(c->netapi_ctx, status));
724 d_printf(_("Deleted user '%s'.\n"), argv[0]);
731 * Set a user's password on a remote RPC server.
733 * @param argc Standard main() style argc.
734 * @param argv Standard main() style argv. Initial components are already
737 * @return A shell status integer (0 for success).
740 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
742 NET_API_STATUS status;
744 struct USER_INFO_1003 u1003;
745 uint32_t parm_err = 0;
748 if (argc < 1 || c->display_usage) {
749 rpc_user_usage(c, argc, argv);
754 u1003.usri1003_password = argv[1];
756 ret = asprintf(&prompt, _("Enter new password for %s:"),
761 u1003.usri1003_password = getpass(prompt);
765 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
767 /* Display results */
770 _("Failed to set password for '%s' with error: %s.\n"),
771 argv[0], libnetapi_get_error_string(c->netapi_ctx,
780 * List a user's groups from a remote RPC server.
782 * @param argc Standard main() style argc.
783 * @param argv Standard main() style argv. Initial components are already
786 * @return A shell status integer (0 for success)
789 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
792 NET_API_STATUS status;
793 struct GROUP_USERS_INFO_0 *u0 = NULL;
794 uint32_t entries_read = 0;
795 uint32_t total_entries = 0;
799 if (argc < 1 || c->display_usage) {
800 rpc_user_usage(c, argc, argv);
804 status = NetUserGetGroups(c->opt_host,
807 (uint8_t **)(void *)&u0,
813 _("Failed to get groups for '%s' with error: %s.\n"),
814 argv[0], libnetapi_get_error_string(c->netapi_ctx,
819 for (i=0; i < entries_read; i++) {
820 printf("%s\n", u0->grui0_name);
828 * List users on a remote RPC server.
830 * All parameters are provided by the run_rpc_command function, except for
831 * argc, argv which are passed through.
833 * @param domain_sid The domain sid acquired from the remote server.
834 * @param cli A cli_state connected to the server.
835 * @param mem_ctx Talloc context, destroyed on completion of the function.
836 * @param argc Standard main() style argc.
837 * @param argv Standard main() style argv. Initial components are already
840 * @return Normal NTSTATUS return.
843 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
845 NET_API_STATUS status;
846 uint32_t start_idx=0, num_entries, i, loop_count = 0;
847 struct NET_DISPLAY_USER *info = NULL;
850 /* Query domain users */
851 if (c->opt_long_list_entries)
852 d_printf(_("\nUser name Comment"
853 "\n-----------------------------\n"));
855 uint32_t max_entries, max_size;
857 get_query_dispinfo_params(
858 loop_count, &max_entries, &max_size);
860 status = NetQueryDisplayInformation(c->opt_host,
867 if (status != 0 && status != ERROR_MORE_DATA) {
871 info = (struct NET_DISPLAY_USER *)buffer;
873 for (i = 0; i < num_entries; i++) {
875 if (c->opt_long_list_entries)
876 printf("%-21.21s %s\n", info->usri1_name,
877 info->usri1_comment);
879 printf("%s\n", info->usri1_name);
883 NetApiBufferFree(buffer);
886 start_idx += num_entries;
888 } while (status == ERROR_MORE_DATA);
894 * 'net rpc user' entrypoint.
895 * @param argc Standard main() style argc.
896 * @param argv Standard main() style argv. Initial components are already
900 int net_rpc_user(struct net_context *c, int argc, const char **argv)
902 NET_API_STATUS status;
904 struct functable func[] = {
909 N_("Add specified user"),
910 N_("net rpc user add\n"
911 " Add specified user")
917 N_("List domain groups of user"),
918 N_("net rpc user info\n"
919 " Lis domain groups of user")
925 N_("Remove specified user"),
926 N_("net rpc user delete\n"
927 " Remove specified user")
933 N_("Change user password"),
934 N_("net rpc user password\n"
935 " Change user password")
941 N_("Rename specified user"),
942 N_("net rpc user rename\n"
943 " Rename specified user")
945 {NULL, NULL, 0, NULL, NULL}
948 status = libnetapi_init(&c->netapi_ctx);
952 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
953 libnetapi_set_password(c->netapi_ctx, c->opt_password);
954 if (c->opt_kerberos) {
955 libnetapi_set_use_kerberos(c->netapi_ctx);
959 if (c->display_usage) {
960 d_printf(_("Usage:\n"));
961 d_printf(_("net rpc user\n"
962 " List all users\n"));
963 net_display_usage_from_functable(func);
967 return rpc_user_list(c, argc, argv);
970 return net_run_function(c, argc, argv, "net rpc user", func);
973 static NTSTATUS rpc_sh_user_list(struct net_context *c,
975 struct rpc_sh_ctx *ctx,
976 struct rpc_pipe_client *pipe_hnd,
977 int argc, const char **argv)
979 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
982 static NTSTATUS rpc_sh_user_info(struct net_context *c,
984 struct rpc_sh_ctx *ctx,
985 struct rpc_pipe_client *pipe_hnd,
986 int argc, const char **argv)
988 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
991 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
993 struct rpc_sh_ctx *ctx,
994 struct rpc_pipe_client *pipe_hnd,
995 int argc, const char **argv,
997 struct net_context *c,
999 struct rpc_sh_ctx *ctx,
1000 struct rpc_pipe_client *pipe_hnd,
1001 struct policy_handle *user_hnd,
1002 int argc, const char **argv))
1004 struct policy_handle connect_pol, domain_pol, user_pol;
1005 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1008 enum lsa_SidType type;
1011 d_fprintf(stderr, _("usage: %s <username>\n"), ctx->whoami);
1012 return NT_STATUS_INVALID_PARAMETER;
1015 ZERO_STRUCT(connect_pol);
1016 ZERO_STRUCT(domain_pol);
1017 ZERO_STRUCT(user_pol);
1019 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1020 argv[0], NULL, NULL, &sid, &type);
1021 if (!NT_STATUS_IS_OK(result)) {
1022 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1027 if (type != SID_NAME_USER) {
1028 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1029 sid_type_lookup(type));
1030 result = NT_STATUS_NO_SUCH_USER;
1034 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1035 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1036 result = NT_STATUS_NO_SUCH_USER;
1040 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1042 MAXIMUM_ALLOWED_ACCESS,
1044 if (!NT_STATUS_IS_OK(result)) {
1048 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1050 MAXIMUM_ALLOWED_ACCESS,
1053 if (!NT_STATUS_IS_OK(result)) {
1057 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1059 MAXIMUM_ALLOWED_ACCESS,
1062 if (!NT_STATUS_IS_OK(result)) {
1066 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1069 if (is_valid_policy_hnd(&user_pol)) {
1070 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1072 if (is_valid_policy_hnd(&domain_pol)) {
1073 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1075 if (is_valid_policy_hnd(&connect_pol)) {
1076 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1081 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1082 TALLOC_CTX *mem_ctx,
1083 struct rpc_sh_ctx *ctx,
1084 struct rpc_pipe_client *pipe_hnd,
1085 struct policy_handle *user_hnd,
1086 int argc, const char **argv)
1089 union samr_UserInfo *info = NULL;
1092 d_fprintf(stderr, _("usage: %s show <username>\n"),ctx->whoami);
1093 return NT_STATUS_INVALID_PARAMETER;
1096 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1100 if (!NT_STATUS_IS_OK(result)) {
1104 d_printf(_("user rid: %d, group rid: %d\n"),
1106 info->info21.primary_gid);
1111 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1112 TALLOC_CTX *mem_ctx,
1113 struct rpc_sh_ctx *ctx,
1114 struct rpc_pipe_client *pipe_hnd,
1115 int argc, const char **argv)
1117 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1118 rpc_sh_user_show_internals);
1121 #define FETCHSTR(name, rec) \
1122 do { if (strequal(ctx->thiscmd, name)) { \
1123 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1126 #define SETSTR(name, rec, flag) \
1127 do { if (strequal(ctx->thiscmd, name)) { \
1128 init_lsa_String(&(info->info21.rec), argv[0]); \
1129 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1132 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1133 TALLOC_CTX *mem_ctx,
1134 struct rpc_sh_ctx *ctx,
1135 struct rpc_pipe_client *pipe_hnd,
1136 struct policy_handle *user_hnd,
1137 int argc, const char **argv)
1140 const char *username;
1141 const char *oldval = "";
1142 union samr_UserInfo *info = NULL;
1145 d_fprintf(stderr, _("usage: %s <username> [new value|NULL]\n"),
1147 return NT_STATUS_INVALID_PARAMETER;
1150 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1154 if (!NT_STATUS_IS_OK(result)) {
1158 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1160 FETCHSTR("fullname", full_name);
1161 FETCHSTR("homedir", home_directory);
1162 FETCHSTR("homedrive", home_drive);
1163 FETCHSTR("logonscript", logon_script);
1164 FETCHSTR("profilepath", profile_path);
1165 FETCHSTR("description", description);
1168 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1172 if (strcmp(argv[0], "NULL") == 0) {
1176 ZERO_STRUCT(info->info21);
1178 SETSTR("fullname", full_name, FULL_NAME);
1179 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1180 SETSTR("homedrive", home_drive, HOME_DRIVE);
1181 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1182 SETSTR("profilepath", profile_path, PROFILE_PATH);
1183 SETSTR("description", description, DESCRIPTION);
1185 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1190 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1191 ctx->thiscmd, oldval, argv[0]);
1198 #define HANDLEFLG(name, rec) \
1199 do { if (strequal(ctx->thiscmd, name)) { \
1200 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1202 newflags = oldflags | ACB_##rec; \
1204 newflags = oldflags & ~ACB_##rec; \
1207 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1208 TALLOC_CTX *mem_ctx,
1209 struct rpc_sh_ctx *ctx,
1210 struct rpc_pipe_client *pipe_hnd,
1211 int argc, const char **argv)
1213 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1214 rpc_sh_user_str_edit_internals);
1217 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1218 TALLOC_CTX *mem_ctx,
1219 struct rpc_sh_ctx *ctx,
1220 struct rpc_pipe_client *pipe_hnd,
1221 struct policy_handle *user_hnd,
1222 int argc, const char **argv)
1225 const char *username;
1226 const char *oldval = "unknown";
1227 uint32 oldflags, newflags;
1229 union samr_UserInfo *info = NULL;
1232 ((argc == 1) && !strequal(argv[0], "yes") &&
1233 !strequal(argv[0], "no"))) {
1234 /* TRANSATORS: The yes|no here are program keywords. Please do
1236 d_fprintf(stderr, _("usage: %s <username> [yes|no]\n"),
1238 return NT_STATUS_INVALID_PARAMETER;
1241 newval = strequal(argv[0], "yes");
1243 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1247 if (!NT_STATUS_IS_OK(result)) {
1251 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1252 oldflags = info->info21.acct_flags;
1253 newflags = info->info21.acct_flags;
1255 HANDLEFLG("disabled", DISABLED);
1256 HANDLEFLG("pwnotreq", PWNOTREQ);
1257 HANDLEFLG("autolock", AUTOLOCK);
1258 HANDLEFLG("pwnoexp", PWNOEXP);
1261 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1266 ZERO_STRUCT(info->info21);
1268 info->info21.acct_flags = newflags;
1269 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1271 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1276 if (NT_STATUS_IS_OK(result)) {
1277 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1278 ctx->thiscmd, oldval, argv[0]);
1286 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1287 TALLOC_CTX *mem_ctx,
1288 struct rpc_sh_ctx *ctx,
1289 struct rpc_pipe_client *pipe_hnd,
1290 int argc, const char **argv)
1292 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1293 rpc_sh_user_flag_edit_internals);
1296 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1297 TALLOC_CTX *mem_ctx,
1298 struct rpc_sh_ctx *ctx)
1300 static struct rpc_sh_cmd cmds[] = {
1302 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1303 N_("Show/Set a user's full name") },
1305 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1306 N_("Show/Set a user's home directory") },
1308 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1309 N_("Show/Set a user's home drive") },
1311 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1312 N_("Show/Set a user's logon script") },
1314 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1315 N_("Show/Set a user's profile path") },
1317 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1318 N_("Show/Set a user's description") },
1320 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1321 N_("Show/Set whether a user is disabled") },
1323 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1324 N_("Show/Set whether a user locked out") },
1326 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1327 N_("Show/Set whether a user does not need a password") },
1329 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1330 N_("Show/Set whether a user's password does not expire") },
1332 { NULL, NULL, 0, NULL, NULL }
1338 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1339 TALLOC_CTX *mem_ctx,
1340 struct rpc_sh_ctx *ctx)
1342 static struct rpc_sh_cmd cmds[] = {
1344 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1345 N_("List available users") },
1347 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1348 N_("List the domain groups a user is member of") },
1350 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1351 N_("Show info about a user") },
1353 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1354 N_("Show/Modify a user's fields") },
1356 { NULL, NULL, 0, NULL, NULL }
1362 /****************************************************************************/
1365 * Basic usage function for 'net rpc group'.
1366 * @param argc Standard main() style argc.
1367 * @param argv Standard main() style argv. Initial components are already
1371 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1373 return net_group_usage(c, argc, argv);
1377 * Delete group on a remote RPC server.
1379 * All parameters are provided by the run_rpc_command function, except for
1380 * argc, argv which are passed through.
1382 * @param domain_sid The domain sid acquired from the remote server.
1383 * @param cli A cli_state connected to the server.
1384 * @param mem_ctx Talloc context, destroyed on completion of the function.
1385 * @param argc Standard main() style argc.
1386 * @param argv Standard main() style argv. Initial components are already
1389 * @return Normal NTSTATUS return.
1392 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1393 const DOM_SID *domain_sid,
1394 const char *domain_name,
1395 struct cli_state *cli,
1396 struct rpc_pipe_client *pipe_hnd,
1397 TALLOC_CTX *mem_ctx,
1401 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1402 bool group_is_primary = false;
1403 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1405 struct samr_RidTypeArray *rids = NULL;
1408 /* struct samr_RidWithAttribute *user_gids; */
1410 struct samr_Ids group_rids, name_types;
1411 struct lsa_String lsa_acct_name;
1412 union samr_UserInfo *info = NULL;
1414 if (argc < 1 || c->display_usage) {
1415 rpc_group_usage(c, argc,argv);
1416 return NT_STATUS_OK; /* ok? */
1419 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1421 MAXIMUM_ALLOWED_ACCESS,
1424 if (!NT_STATUS_IS_OK(result)) {
1425 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1429 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1431 MAXIMUM_ALLOWED_ACCESS,
1432 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1435 if (!NT_STATUS_IS_OK(result)) {
1436 d_fprintf(stderr, _("Request open_domain failed\n"));
1440 init_lsa_String(&lsa_acct_name, argv[0]);
1442 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1448 if (!NT_STATUS_IS_OK(result)) {
1449 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1453 switch (name_types.ids[0])
1455 case SID_NAME_DOM_GRP:
1456 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1458 MAXIMUM_ALLOWED_ACCESS,
1461 if (!NT_STATUS_IS_OK(result)) {
1462 d_fprintf(stderr, _("Request open_group failed"));
1466 group_rid = group_rids.ids[0];
1468 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1472 if (!NT_STATUS_IS_OK(result)) {
1474 _("Unable to query group members of %s"),
1479 if (c->opt_verbose) {
1481 _("Domain Group %s (rid: %d) has %d members\n"),
1482 argv[0],group_rid, rids->count);
1485 /* Check if group is anyone's primary group */
1486 for (i = 0; i < rids->count; i++)
1488 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1490 MAXIMUM_ALLOWED_ACCESS,
1494 if (!NT_STATUS_IS_OK(result)) {
1496 _("Unable to open group member %d\n"),
1501 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1506 if (!NT_STATUS_IS_OK(result)) {
1508 _("Unable to lookup userinfo for group "
1514 if (info->info21.primary_gid == group_rid) {
1515 if (c->opt_verbose) {
1516 d_printf(_("Group is primary group "
1518 info->info21.account_name.string);
1520 group_is_primary = true;
1523 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1526 if (group_is_primary) {
1527 d_fprintf(stderr, _("Unable to delete group because "
1528 "some of it's members have it as primary "
1530 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1534 /* remove all group members */
1535 for (i = 0; i < rids->count; i++)
1538 d_printf(_("Remove group member %d..."),
1540 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1544 if (NT_STATUS_IS_OK(result)) {
1546 d_printf(_("ok\n"));
1549 d_printf(_("failed\n"));
1554 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1558 /* removing a local group is easier... */
1559 case SID_NAME_ALIAS:
1560 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1562 MAXIMUM_ALLOWED_ACCESS,
1566 if (!NT_STATUS_IS_OK(result)) {
1567 d_fprintf(stderr, _("Request open_alias failed\n"));
1571 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1575 d_fprintf(stderr, _("%s is of type %s. This command is only "
1576 "for deleting local or global groups\n"),
1577 argv[0],sid_type_lookup(name_types.ids[0]));
1578 result = NT_STATUS_UNSUCCESSFUL;
1582 if (NT_STATUS_IS_OK(result)) {
1584 d_printf(_("Deleted %s '%s'\n"),
1585 sid_type_lookup(name_types.ids[0]), argv[0]);
1587 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1588 get_friendly_nt_error_msg(result));
1596 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1598 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1599 rpc_group_delete_internals, argc,argv);
1602 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1604 NET_API_STATUS status;
1605 struct GROUP_INFO_1 info1;
1606 uint32_t parm_error = 0;
1608 if (argc != 1 || c->display_usage) {
1609 rpc_group_usage(c, argc, argv);
1615 info1.grpi1_name = argv[0];
1616 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1617 info1.grpi1_comment = c->opt_comment;
1620 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1624 _("Failed to add group '%s' with error: %s.\n"),
1625 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1629 d_printf(_("Added group '%s'.\n"), argv[0]);
1635 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1637 NET_API_STATUS status;
1638 struct LOCALGROUP_INFO_1 info1;
1639 uint32_t parm_error = 0;
1641 if (argc != 1 || c->display_usage) {
1642 rpc_group_usage(c, argc, argv);
1648 info1.lgrpi1_name = argv[0];
1649 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1650 info1.lgrpi1_comment = c->opt_comment;
1653 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1657 _("Failed to add alias '%s' with error: %s.\n"),
1658 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1662 d_printf(_("Added alias '%s'.\n"), argv[0]);
1668 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1670 if (c->opt_localgroup)
1671 return rpc_alias_add_internals(c, argc, argv);
1673 return rpc_group_add_internals(c, argc, argv);
1676 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1677 TALLOC_CTX *mem_ctx,
1680 enum lsa_SidType *type)
1682 DOM_SID *sids = NULL;
1683 enum lsa_SidType *types = NULL;
1684 struct rpc_pipe_client *pipe_hnd = NULL;
1685 struct policy_handle lsa_pol;
1686 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1688 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1690 if (!NT_STATUS_IS_OK(result)) {
1694 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1695 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1697 if (!NT_STATUS_IS_OK(result)) {
1701 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1702 &name, NULL, 1, &sids, &types);
1704 if (NT_STATUS_IS_OK(result)) {
1705 sid_copy(sid, &sids[0]);
1709 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1713 TALLOC_FREE(pipe_hnd);
1716 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1718 /* Try as S-1-5-whatever */
1722 if (string_to_sid(&tmp_sid, name)) {
1723 sid_copy(sid, &tmp_sid);
1724 *type = SID_NAME_UNKNOWN;
1725 result = NT_STATUS_OK;
1732 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1733 TALLOC_CTX *mem_ctx,
1734 const DOM_SID *group_sid,
1737 struct policy_handle connect_pol, domain_pol;
1740 struct policy_handle group_pol;
1742 struct samr_Ids rids, rid_types;
1743 struct lsa_String lsa_acct_name;
1747 sid_copy(&sid, group_sid);
1749 if (!sid_split_rid(&sid, &group_rid)) {
1750 return NT_STATUS_UNSUCCESSFUL;
1753 /* Get sam policy handle */
1754 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1756 MAXIMUM_ALLOWED_ACCESS,
1758 if (!NT_STATUS_IS_OK(result)) {
1762 /* Get domain policy handle */
1763 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1765 MAXIMUM_ALLOWED_ACCESS,
1768 if (!NT_STATUS_IS_OK(result)) {
1772 init_lsa_String(&lsa_acct_name, member);
1774 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1781 if (!NT_STATUS_IS_OK(result)) {
1782 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1787 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1789 MAXIMUM_ALLOWED_ACCESS,
1793 if (!NT_STATUS_IS_OK(result)) {
1797 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1800 0x0005); /* unknown flags */
1803 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1807 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1808 TALLOC_CTX *mem_ctx,
1809 const DOM_SID *alias_sid,
1812 struct policy_handle connect_pol, domain_pol;
1815 struct policy_handle alias_pol;
1818 enum lsa_SidType member_type;
1822 sid_copy(&sid, alias_sid);
1824 if (!sid_split_rid(&sid, &alias_rid)) {
1825 return NT_STATUS_UNSUCCESSFUL;
1828 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1829 member, &member_sid, &member_type);
1831 if (!NT_STATUS_IS_OK(result)) {
1832 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1837 /* Get sam policy handle */
1838 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1840 MAXIMUM_ALLOWED_ACCESS,
1842 if (!NT_STATUS_IS_OK(result)) {
1846 /* Get domain policy handle */
1847 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1849 MAXIMUM_ALLOWED_ACCESS,
1852 if (!NT_STATUS_IS_OK(result)) {
1856 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1858 MAXIMUM_ALLOWED_ACCESS,
1862 if (!NT_STATUS_IS_OK(result)) {
1866 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1870 if (!NT_STATUS_IS_OK(result)) {
1875 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1879 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1880 const DOM_SID *domain_sid,
1881 const char *domain_name,
1882 struct cli_state *cli,
1883 struct rpc_pipe_client *pipe_hnd,
1884 TALLOC_CTX *mem_ctx,
1889 enum lsa_SidType group_type;
1891 if (argc != 2 || c->display_usage) {
1892 d_printf(_("Usage:\n"
1893 "net rpc group addmem <group> <member>\n"
1894 " Add a member to a group\n"
1895 " group\tGroup to add member to\n"
1896 " member\tMember to add to group\n"));
1897 return NT_STATUS_UNSUCCESSFUL;
1900 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1901 &group_sid, &group_type))) {
1902 d_fprintf(stderr, _("Could not lookup group name %s\n"),
1904 return NT_STATUS_UNSUCCESSFUL;
1907 if (group_type == SID_NAME_DOM_GRP) {
1908 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1909 &group_sid, argv[1]);
1911 if (!NT_STATUS_IS_OK(result)) {
1912 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1913 argv[1], argv[0], nt_errstr(result));
1918 if (group_type == SID_NAME_ALIAS) {
1919 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1920 &group_sid, argv[1]);
1922 if (!NT_STATUS_IS_OK(result)) {
1923 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1924 argv[1], argv[0], nt_errstr(result));
1929 d_fprintf(stderr, _("Can only add members to global or local groups "
1930 "which %s is not\n"), argv[0]);
1932 return NT_STATUS_UNSUCCESSFUL;
1935 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1937 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1938 rpc_group_addmem_internals,
1942 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1943 struct rpc_pipe_client *pipe_hnd,
1944 TALLOC_CTX *mem_ctx,
1945 const DOM_SID *group_sid,
1948 struct policy_handle connect_pol, domain_pol;
1951 struct policy_handle group_pol;
1953 struct samr_Ids rids, rid_types;
1954 struct lsa_String lsa_acct_name;
1958 sid_copy(&sid, group_sid);
1960 if (!sid_split_rid(&sid, &group_rid))
1961 return NT_STATUS_UNSUCCESSFUL;
1963 /* Get sam policy handle */
1964 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1966 MAXIMUM_ALLOWED_ACCESS,
1968 if (!NT_STATUS_IS_OK(result))
1971 /* Get domain policy handle */
1972 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1974 MAXIMUM_ALLOWED_ACCESS,
1977 if (!NT_STATUS_IS_OK(result))
1980 init_lsa_String(&lsa_acct_name, member);
1982 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1988 if (!NT_STATUS_IS_OK(result)) {
1989 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1994 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1996 MAXIMUM_ALLOWED_ACCESS,
2000 if (!NT_STATUS_IS_OK(result))
2003 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2008 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2012 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2013 TALLOC_CTX *mem_ctx,
2014 const DOM_SID *alias_sid,
2017 struct policy_handle connect_pol, domain_pol;
2020 struct policy_handle alias_pol;
2023 enum lsa_SidType member_type;
2027 sid_copy(&sid, alias_sid);
2029 if (!sid_split_rid(&sid, &alias_rid))
2030 return NT_STATUS_UNSUCCESSFUL;
2032 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2033 member, &member_sid, &member_type);
2035 if (!NT_STATUS_IS_OK(result)) {
2036 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2041 /* Get sam policy handle */
2042 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2044 MAXIMUM_ALLOWED_ACCESS,
2046 if (!NT_STATUS_IS_OK(result)) {
2050 /* Get domain policy handle */
2051 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2053 MAXIMUM_ALLOWED_ACCESS,
2056 if (!NT_STATUS_IS_OK(result)) {
2060 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2062 MAXIMUM_ALLOWED_ACCESS,
2066 if (!NT_STATUS_IS_OK(result))
2069 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2073 if (!NT_STATUS_IS_OK(result))
2077 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2081 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2082 const DOM_SID *domain_sid,
2083 const char *domain_name,
2084 struct cli_state *cli,
2085 struct rpc_pipe_client *pipe_hnd,
2086 TALLOC_CTX *mem_ctx,
2091 enum lsa_SidType group_type;
2093 if (argc != 2 || c->display_usage) {
2094 d_printf(_("Usage:\n"
2095 "net rpc group delmem <group> <member>\n"
2096 " Delete a member from a group\n"
2097 " group\tGroup to delete member from\n"
2098 " member\tMember to delete from group\n"));
2099 return NT_STATUS_UNSUCCESSFUL;
2102 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2103 &group_sid, &group_type))) {
2104 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2106 return NT_STATUS_UNSUCCESSFUL;
2109 if (group_type == SID_NAME_DOM_GRP) {
2110 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2111 &group_sid, argv[1]);
2113 if (!NT_STATUS_IS_OK(result)) {
2114 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2115 argv[1], argv[0], nt_errstr(result));
2120 if (group_type == SID_NAME_ALIAS) {
2121 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2122 &group_sid, argv[1]);
2124 if (!NT_STATUS_IS_OK(result)) {
2125 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2126 argv[1], argv[0], nt_errstr(result));
2131 d_fprintf(stderr, _("Can only delete members from global or local "
2132 "groups which %s is not\n"), argv[0]);
2134 return NT_STATUS_UNSUCCESSFUL;
2137 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2139 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2140 rpc_group_delmem_internals,
2145 * List groups on a remote RPC server.
2147 * All parameters are provided by the run_rpc_command function, except for
2148 * argc, argv which are passes through.
2150 * @param domain_sid The domain sid acquired from the remote server.
2151 * @param cli A cli_state connected to the server.
2152 * @param mem_ctx Talloc context, destroyed on completion of the function.
2153 * @param argc Standard main() style argc.
2154 * @param argv Standard main() style argv. Initial components are already
2157 * @return Normal NTSTATUS return.
2160 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2161 const DOM_SID *domain_sid,
2162 const char *domain_name,
2163 struct cli_state *cli,
2164 struct rpc_pipe_client *pipe_hnd,
2165 TALLOC_CTX *mem_ctx,
2169 struct policy_handle connect_pol, domain_pol;
2170 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2171 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2172 struct samr_SamArray *groups = NULL;
2173 bool global = false;
2175 bool builtin = false;
2177 if (c->display_usage) {
2178 d_printf(_("Usage:\n"
2179 "net rpc group list [global] [local] [builtin]\n"
2180 " List groups on RPC server\n"
2181 " global\tList global groups\n"
2182 " local\tList local groups\n"
2183 " builtin\tList builtin groups\n"
2184 " If none of global, local or builtin is "
2185 "specified, all three options are considered "
2187 return NT_STATUS_OK;
2196 for (i=0; i<argc; i++) {
2197 if (strequal(argv[i], "global"))
2200 if (strequal(argv[i], "local"))
2203 if (strequal(argv[i], "builtin"))
2207 /* Get sam policy handle */
2209 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2211 MAXIMUM_ALLOWED_ACCESS,
2213 if (!NT_STATUS_IS_OK(result)) {
2217 /* Get domain policy handle */
2219 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2221 MAXIMUM_ALLOWED_ACCESS,
2222 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2224 if (!NT_STATUS_IS_OK(result)) {
2228 /* Query domain groups */
2229 if (c->opt_long_list_entries)
2230 d_printf(_("\nGroup name Comment"
2231 "\n-----------------------------\n"));
2233 uint32_t max_size, total_size, returned_size;
2234 union samr_DispInfo info;
2238 get_query_dispinfo_params(
2239 loop_count, &max_entries, &max_size);
2241 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2250 num_entries = info.info3.count;
2251 start_idx += info.info3.count;
2253 if (!NT_STATUS_IS_OK(result) &&
2254 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2257 for (i = 0; i < num_entries; i++) {
2259 const char *group = NULL;
2260 const char *desc = NULL;
2262 group = info.info3.entries[i].account_name.string;
2263 desc = info.info3.entries[i].description.string;
2265 if (c->opt_long_list_entries)
2266 printf("%-21.21s %-50.50s\n",
2269 printf("%s\n", group);
2271 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2272 /* query domain aliases */
2277 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2283 if (!NT_STATUS_IS_OK(result) &&
2284 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2287 for (i = 0; i < num_entries; i++) {
2289 const char *description = NULL;
2291 if (c->opt_long_list_entries) {
2293 struct policy_handle alias_pol;
2294 union samr_AliasInfo *info = NULL;
2296 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2299 groups->entries[i].idx,
2301 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2305 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2307 description = info->description.string;
2311 if (description != NULL) {
2312 printf("%-21.21s %-50.50s\n",
2313 groups->entries[i].name.string,
2316 printf("%s\n", groups->entries[i].name.string);
2319 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2320 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2321 /* Get builtin policy handle */
2323 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2325 MAXIMUM_ALLOWED_ACCESS,
2326 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2328 if (!NT_STATUS_IS_OK(result)) {
2331 /* query builtin aliases */
2334 if (!builtin) break;
2336 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2342 if (!NT_STATUS_IS_OK(result) &&
2343 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2346 for (i = 0; i < num_entries; i++) {
2348 const char *description = NULL;
2350 if (c->opt_long_list_entries) {
2352 struct policy_handle alias_pol;
2353 union samr_AliasInfo *info = NULL;
2355 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2358 groups->entries[i].idx,
2360 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2364 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2366 description = info->description.string;
2370 if (description != NULL) {
2371 printf("%-21.21s %-50.50s\n",
2372 groups->entries[i].name.string,
2375 printf("%s\n", groups->entries[i].name.string);
2378 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2384 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2386 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2387 rpc_group_list_internals,
2391 static NTSTATUS rpc_list_group_members(struct net_context *c,
2392 struct rpc_pipe_client *pipe_hnd,
2393 TALLOC_CTX *mem_ctx,
2394 const char *domain_name,
2395 const DOM_SID *domain_sid,
2396 struct policy_handle *domain_pol,
2400 struct policy_handle group_pol;
2401 uint32 num_members, *group_rids;
2403 struct samr_RidTypeArray *rids = NULL;
2404 struct lsa_Strings names;
2405 struct samr_Ids types;
2408 sid_to_fstring(sid_str, domain_sid);
2410 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2412 MAXIMUM_ALLOWED_ACCESS,
2416 if (!NT_STATUS_IS_OK(result))
2419 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2423 if (!NT_STATUS_IS_OK(result))
2426 num_members = rids->count;
2427 group_rids = rids->rids;
2429 while (num_members > 0) {
2430 int this_time = 512;
2432 if (num_members < this_time)
2433 this_time = num_members;
2435 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2442 if (!NT_STATUS_IS_OK(result))
2445 /* We only have users as members, but make the output
2446 the same as the output of alias members */
2448 for (i = 0; i < this_time; i++) {
2450 if (c->opt_long_list_entries) {
2451 printf("%s-%d %s\\%s %d\n", sid_str,
2452 group_rids[i], domain_name,
2453 names.names[i].string,
2456 printf("%s\\%s\n", domain_name,
2457 names.names[i].string);
2461 num_members -= this_time;
2465 return NT_STATUS_OK;
2468 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2469 struct rpc_pipe_client *pipe_hnd,
2470 TALLOC_CTX *mem_ctx,
2471 struct policy_handle *domain_pol,
2475 struct rpc_pipe_client *lsa_pipe;
2476 struct policy_handle alias_pol, lsa_pol;
2478 DOM_SID *alias_sids;
2481 enum lsa_SidType *types;
2483 struct lsa_SidArray sid_array;
2485 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2487 MAXIMUM_ALLOWED_ACCESS,
2491 if (!NT_STATUS_IS_OK(result))
2494 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2498 if (!NT_STATUS_IS_OK(result)) {
2499 d_fprintf(stderr, _("Couldn't list alias members\n"));
2503 num_members = sid_array.num_sids;
2505 if (num_members == 0) {
2506 return NT_STATUS_OK;
2509 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2510 &ndr_table_lsarpc.syntax_id,
2512 if (!NT_STATUS_IS_OK(result)) {
2513 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2514 nt_errstr(result) );
2518 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2519 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2521 if (!NT_STATUS_IS_OK(result)) {
2522 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2523 TALLOC_FREE(lsa_pipe);
2527 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2529 d_fprintf(stderr, _("Out of memory\n"));
2530 TALLOC_FREE(lsa_pipe);
2531 return NT_STATUS_NO_MEMORY;
2534 for (i=0; i<num_members; i++) {
2535 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2538 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2539 num_members, alias_sids,
2540 &domains, &names, &types);
2542 if (!NT_STATUS_IS_OK(result) &&
2543 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2544 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2545 TALLOC_FREE(lsa_pipe);
2549 for (i = 0; i < num_members; i++) {
2551 sid_to_fstring(sid_str, &alias_sids[i]);
2553 if (c->opt_long_list_entries) {
2554 printf("%s %s\\%s %d\n", sid_str,
2555 domains[i] ? domains[i] : _("*unknown*"),
2556 names[i] ? names[i] : _("*unknown*"), types[i]);
2559 printf("%s\\%s\n", domains[i], names[i]);
2561 printf("%s\n", sid_str);
2565 TALLOC_FREE(lsa_pipe);
2566 return NT_STATUS_OK;
2569 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2570 const DOM_SID *domain_sid,
2571 const char *domain_name,
2572 struct cli_state *cli,
2573 struct rpc_pipe_client *pipe_hnd,
2574 TALLOC_CTX *mem_ctx,
2579 struct policy_handle connect_pol, domain_pol;
2580 struct samr_Ids rids, rid_types;
2581 struct lsa_String lsa_acct_name;
2583 /* Get sam policy handle */
2585 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2587 MAXIMUM_ALLOWED_ACCESS,
2590 if (!NT_STATUS_IS_OK(result))
2593 /* Get domain policy handle */
2595 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2597 MAXIMUM_ALLOWED_ACCESS,
2598 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2601 if (!NT_STATUS_IS_OK(result))
2604 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2606 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2613 if (!NT_STATUS_IS_OK(result)) {
2615 /* Ok, did not find it in the global sam, try with builtin */
2617 DOM_SID sid_Builtin;
2619 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2621 sid_copy(&sid_Builtin, &global_sid_Builtin);
2623 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2625 MAXIMUM_ALLOWED_ACCESS,
2629 if (!NT_STATUS_IS_OK(result)) {
2630 d_fprintf(stderr, _("Couldn't find group %s\n"),
2635 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2642 if (!NT_STATUS_IS_OK(result)) {
2643 d_fprintf(stderr, _("Couldn't find group %s\n"),
2649 if (rids.count != 1) {
2650 d_fprintf(stderr, _("Couldn't find group %s\n"),
2655 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2656 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2657 domain_sid, &domain_pol,
2661 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2662 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2666 return NT_STATUS_NO_SUCH_GROUP;
2669 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2671 if (argc != 1 || c->display_usage) {
2672 return rpc_group_usage(c, argc, argv);
2675 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2676 rpc_group_members_internals,
2680 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2682 NET_API_STATUS status;
2683 struct GROUP_INFO_0 g0;
2687 d_printf(_("Usage: 'net rpc group rename group newname'\n"));
2691 g0.grpi0_name = argv[1];
2693 status = NetGroupSetInfo(c->opt_host,
2700 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2701 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2709 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2711 if (argc != 2 || c->display_usage) {
2712 return rpc_group_usage(c, argc, argv);
2715 return rpc_group_rename_internals(c, argc, argv);
2719 * 'net rpc group' entrypoint.
2720 * @param argc Standard main() style argc.
2721 * @param argv Standard main() style argv. Initial components are already
2725 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2727 NET_API_STATUS status;
2729 struct functable func[] = {
2734 N_("Create specified group"),
2735 N_("net rpc group add\n"
2736 " Create specified group")
2742 N_("Delete specified group"),
2743 N_("net rpc group delete\n"
2744 " Delete specified group")
2750 N_("Add member to group"),
2751 N_("net rpc group addmem\n"
2752 " Add member to group")
2758 N_("Remove member from group"),
2759 N_("net rpc group delmem\n"
2760 " Remove member from group")
2767 N_("net rpc group list\n"
2774 N_("List group members"),
2775 N_("net rpc group members\n"
2776 " List group members")
2783 N_("net rpc group rename\n"
2786 {NULL, NULL, 0, NULL, NULL}
2789 status = libnetapi_init(&c->netapi_ctx);
2793 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2794 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2795 if (c->opt_kerberos) {
2796 libnetapi_set_use_kerberos(c->netapi_ctx);
2800 if (c->display_usage) {
2801 d_printf(_("Usage:\n"));
2802 d_printf(_("net rpc group\n"
2803 " Alias for net rpc group list global "
2804 "local builtin\n"));
2805 net_display_usage_from_functable(func);
2809 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2810 rpc_group_list_internals,
2814 return net_run_function(c, argc, argv, "net rpc group", func);
2817 /****************************************************************************/
2819 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2821 return net_share_usage(c, argc, argv);
2825 * Add a share on a remote RPC server.
2827 * @param argc Standard main() style argc.
2828 * @param argv Standard main() style argv. Initial components are already
2831 * @return A shell status integer (0 for success).
2834 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2836 NET_API_STATUS status;
2839 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2840 uint32 num_users=0, perms=0;
2841 char *password=NULL; /* don't allow a share password */
2842 struct SHARE_INFO_2 i2;
2843 uint32_t parm_error = 0;
2845 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2846 return rpc_share_usage(c, argc, argv);
2849 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2853 path = strchr(sharename, '=');
2860 i2.shi2_netname = sharename;
2861 i2.shi2_type = type;
2862 i2.shi2_remark = c->opt_comment;
2863 i2.shi2_permissions = perms;
2864 i2.shi2_max_uses = c->opt_maxusers;
2865 i2.shi2_current_uses = num_users;
2866 i2.shi2_path = path;
2867 i2.shi2_passwd = password;
2869 status = NetShareAdd(c->opt_host,
2874 printf(_("NetShareAdd failed with: %s\n"),
2875 libnetapi_get_error_string(c->netapi_ctx, status));
2882 * Delete a share on a remote RPC server.
2884 * @param domain_sid The domain sid acquired from the remote server.
2885 * @param argc Standard main() style argc.
2886 * @param argv Standard main() style argv. Initial components are already
2889 * @return A shell status integer (0 for success).
2891 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2893 if (argc < 1 || c->display_usage) {
2894 return rpc_share_usage(c, argc, argv);
2897 return NetShareDel(c->opt_host, argv[0], 0);
2901 * Formatted print of share info
2903 * @param r pointer to SHARE_INFO_1 to format
2906 static void display_share_info_1(struct net_context *c,
2907 struct SHARE_INFO_1 *r)
2909 if (c->opt_long_list_entries) {
2910 d_printf("%-12s %-8.8s %-50s\n",
2912 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2915 d_printf("%s\n", r->shi1_netname);
2919 static WERROR get_share_info(struct net_context *c,
2920 struct rpc_pipe_client *pipe_hnd,
2921 TALLOC_CTX *mem_ctx,
2925 struct srvsvc_NetShareInfoCtr *info_ctr)
2929 union srvsvc_NetShareInfo info;
2931 /* no specific share requested, enumerate all */
2934 uint32_t preferred_len = 0xffffffff;
2935 uint32_t total_entries = 0;
2936 uint32_t resume_handle = 0;
2938 info_ctr->level = level;
2940 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2950 /* request just one share */
2951 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2958 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2963 ZERO_STRUCTP(info_ctr);
2965 info_ctr->level = level;
2970 struct srvsvc_NetShareCtr1 *ctr1;
2972 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2973 W_ERROR_HAVE_NO_MEMORY(ctr1);
2976 ctr1->array = info.info1;
2978 info_ctr->ctr.ctr1 = ctr1;
2982 struct srvsvc_NetShareCtr2 *ctr2;
2984 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2985 W_ERROR_HAVE_NO_MEMORY(ctr2);
2988 ctr2->array = info.info2;
2990 info_ctr->ctr.ctr2 = ctr2;
2994 struct srvsvc_NetShareCtr502 *ctr502;
2996 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
2997 W_ERROR_HAVE_NO_MEMORY(ctr502);
3000 ctr502->array = info.info502;
3002 info_ctr->ctr.ctr502 = ctr502;
3010 * 'net rpc share list' entrypoint.
3011 * @param argc Standard main() style argc.
3012 * @param argv Standard main() style argv. Initial components are already
3015 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3017 NET_API_STATUS status;
3018 struct SHARE_INFO_1 *i1 = NULL;
3019 uint32_t entries_read = 0;
3020 uint32_t total_entries = 0;
3021 uint32_t resume_handle = 0;
3022 uint32_t i, level = 1;
3024 if (c->display_usage) {
3025 d_printf(_("Usage\n"
3026 "net rpc share list\n"
3027 " List shares on remote server\n"));
3031 status = NetShareEnum(c->opt_host,
3033 (uint8_t **)(void *)&i1,
3042 /* Display results */
3044 if (c->opt_long_list_entries) {
3046 "\nEnumerating shared resources (exports) on remote server:\n\n"
3047 "\nShare name Type Description\n"
3048 "---------- ---- -----------\n"));
3050 for (i = 0; i < entries_read; i++)
3051 display_share_info_1(c, &i1[i]);
3056 static bool check_share_availability(struct cli_state *cli, const char *netname)
3058 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
3059 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3069 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3070 const char *netname, uint32 type)
3072 /* only support disk shares */
3073 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3074 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3079 /* skip builtin shares */
3080 /* FIXME: should print$ be added too ? */
3081 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3082 strequal(netname,"global"))
3085 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3086 printf(_("excluding [%s]\n"), netname);
3090 return check_share_availability(cli, netname);
3094 * Migrate shares from a remote RPC server to the local RPC server.
3096 * All parameters are provided by the run_rpc_command function, except for
3097 * argc, argv which are passed through.
3099 * @param domain_sid The domain sid acquired from the remote server.
3100 * @param cli A cli_state connected to the server.
3101 * @param mem_ctx Talloc context, destroyed on completion of the function.
3102 * @param argc Standard main() style argc.
3103 * @param argv Standard main() style argv. Initial components are already
3106 * @return Normal NTSTATUS return.
3109 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3110 const DOM_SID *domain_sid,
3111 const char *domain_name,
3112 struct cli_state *cli,
3113 struct rpc_pipe_client *pipe_hnd,
3114 TALLOC_CTX *mem_ctx,
3119 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3120 struct srvsvc_NetShareInfoCtr ctr_src;
3122 struct rpc_pipe_client *srvsvc_pipe = NULL;
3123 struct cli_state *cli_dst = NULL;
3124 uint32 level = 502; /* includes secdesc */
3125 uint32_t parm_error = 0;
3127 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3129 if (!W_ERROR_IS_OK(result))
3132 /* connect destination PI_SRVSVC */
3133 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3134 &ndr_table_srvsvc.syntax_id);
3135 if (!NT_STATUS_IS_OK(nt_status))
3139 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3141 union srvsvc_NetShareInfo info;
3142 struct srvsvc_NetShareInfo502 info502 =
3143 ctr_src.ctr.ctr502->array[i];
3145 /* reset error-code */
3146 nt_status = NT_STATUS_UNSUCCESSFUL;
3148 if (!check_share_sanity(c, cli, info502.name, info502.type))
3151 /* finally add the share on the dst server */
3153 printf(_("migrating: [%s], path: %s, comment: %s, without "
3155 info502.name, info502.path, info502.comment);
3157 info.info502 = &info502;
3159 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3160 srvsvc_pipe->desthost,
3166 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3167 printf(_(" [%s] does already exist\n"),
3172 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3173 printf(_("cannot add share: %s\n"), win_errstr(result));
3179 nt_status = NT_STATUS_OK;
3183 cli_shutdown(cli_dst);
3191 * Migrate shares from a RPC server to another.
3193 * @param argc Standard main() style argc.
3194 * @param argv Standard main() style argv. Initial components are already
3197 * @return A shell status integer (0 for success).
3199 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3202 if (c->display_usage) {
3203 d_printf(_("Usage:\n"
3204 "net rpc share migrate shares\n"
3205 " Migrate shares to local server\n"));
3210 printf(_("no server to migrate\n"));
3214 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3215 rpc_share_migrate_shares_internals,
3222 * @param f file_info
3223 * @param mask current search mask
3224 * @param state arg-pointer
3227 static void copy_fn(const char *mnt, file_info *f,
3228 const char *mask, void *state)
3230 static NTSTATUS nt_status;
3231 static struct copy_clistate *local_state;
3232 static fstring filename, new_mask;
3235 struct net_context *c;
3237 local_state = (struct copy_clistate *)state;
3238 nt_status = NT_STATUS_UNSUCCESSFUL;
3242 if (strequal(f->name, ".") || strequal(f->name, ".."))
3245 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3248 if (f->mode & aDIR) {
3250 DEBUG(3,("got dir: %s\n", f->name));
3252 fstrcpy(dir, local_state->cwd);
3254 fstrcat(dir, f->name);
3256 switch (net_mode_share)
3258 case NET_MODE_SHARE_MIGRATE:
3259 /* create that directory */
3260 nt_status = net_copy_file(c, local_state->mem_ctx,
3261 local_state->cli_share_src,
3262 local_state->cli_share_dst,
3264 c->opt_acls? true : false,
3265 c->opt_attrs? true : false,
3266 c->opt_timestamps? true:false,
3270 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3274 if (!NT_STATUS_IS_OK(nt_status))
3275 printf(_("could not handle dir %s: %s\n"),
3276 dir, nt_errstr(nt_status));
3278 /* search below that directory */
3279 fstrcpy(new_mask, dir);
3280 fstrcat(new_mask, "\\*");
3282 old_dir = local_state->cwd;
3283 local_state->cwd = dir;
3284 if (!sync_files(local_state, new_mask))
3285 printf(_("could not handle files\n"));
3286 local_state->cwd = old_dir;
3293 fstrcpy(filename, local_state->cwd);
3294 fstrcat(filename, "\\");
3295 fstrcat(filename, f->name);
3297 DEBUG(3,("got file: %s\n", filename));
3299 switch (net_mode_share)
3301 case NET_MODE_SHARE_MIGRATE:
3302 nt_status = net_copy_file(c, local_state->mem_ctx,
3303 local_state->cli_share_src,
3304 local_state->cli_share_dst,
3306 c->opt_acls? true : false,
3307 c->opt_attrs? true : false,
3308 c->opt_timestamps? true: false,
3312 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3317 if (!NT_STATUS_IS_OK(nt_status))
3318 printf(_("could not handle file %s: %s\n"),
3319 filename, nt_errstr(nt_status));
3324 * sync files, can be called recursivly to list files
3325 * and then call copy_fn for each file
3327 * @param cp_clistate pointer to the copy_clistate we work with
3328 * @param mask the current search mask
3330 * @return Boolean result
3332 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3334 struct cli_state *targetcli;
3335 char *targetpath = NULL;
3337 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3339 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3340 mask, &targetcli, &targetpath ) ) {
3341 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3343 mask, cli_errstr(cp_clistate->cli_share_src));
3347 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3348 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3349 mask, cli_errstr(targetcli));
3358 * Set the top level directory permissions before we do any further copies.
3359 * Should set up ACL inheritance.
3362 bool copy_top_level_perms(struct net_context *c,
3363 struct copy_clistate *cp_clistate,
3364 const char *sharename)
3366 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3368 switch (net_mode_share) {
3369 case NET_MODE_SHARE_MIGRATE:
3370 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3371 nt_status = net_copy_fileattr(c,
3372 cp_clistate->mem_ctx,
3373 cp_clistate->cli_share_src,
3374 cp_clistate->cli_share_dst,
3376 c->opt_acls? true : false,
3377 c->opt_attrs? true : false,
3378 c->opt_timestamps? true: false,
3382 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3386 if (!NT_STATUS_IS_OK(nt_status)) {
3387 printf(_("Could handle directory attributes for top level "
3388 "directory of share %s. Error %s\n"),
3389 sharename, nt_errstr(nt_status));
3397 * Sync all files inside a remote share to another share (over smb).
3399 * All parameters are provided by the run_rpc_command function, except for
3400 * argc, argv which are passed through.
3402 * @param domain_sid The domain sid acquired from the remote server.
3403 * @param cli A cli_state connected to the server.
3404 * @param mem_ctx Talloc context, destroyed on completion of the function.
3405 * @param argc Standard main() style argc.
3406 * @param argv Standard main() style argv. Initial components are already
3409 * @return Normal NTSTATUS return.
3412 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3413 const DOM_SID *domain_sid,
3414 const char *domain_name,
3415 struct cli_state *cli,
3416 struct rpc_pipe_client *pipe_hnd,
3417 TALLOC_CTX *mem_ctx,
3422 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3423 struct srvsvc_NetShareInfoCtr ctr_src;
3426 struct copy_clistate cp_clistate;
3427 bool got_src_share = false;
3428 bool got_dst_share = false;
3429 const char *mask = "\\*";
3432 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3434 nt_status = NT_STATUS_NO_MEMORY;
3438 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3441 if (!W_ERROR_IS_OK(result))
3444 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3446 struct srvsvc_NetShareInfo502 info502 =
3447 ctr_src.ctr.ctr502->array[i];
3449 if (!check_share_sanity(c, cli, info502.name, info502.type))
3452 /* one might not want to mirror whole discs :) */
3453 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3454 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3459 switch (net_mode_share)
3461 case NET_MODE_SHARE_MIGRATE:
3465 d_fprintf(stderr, _("Unsupported mode %d\n"),
3469 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3472 c->opt_acls ? _("including") : _("without"),
3473 c->opt_attrs ? _("including") : _("without"),
3474 c->opt_timestamps ? _("(preserving timestamps)") : "");
3476 cp_clistate.mem_ctx = mem_ctx;
3477 cp_clistate.cli_share_src = NULL;
3478 cp_clistate.cli_share_dst = NULL;
3479 cp_clistate.cwd = NULL;
3480 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3483 /* open share source */
3484 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3485 &cli->dest_ss, cli->desthost,
3486 info502.name, "A:");
3487 if (!NT_STATUS_IS_OK(nt_status))
3490 got_src_share = true;
3492 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3493 /* open share destination */
3494 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3495 NULL, dst, info502.name, "A:");
3496 if (!NT_STATUS_IS_OK(nt_status))
3499 got_dst_share = true;
3502 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3503 d_fprintf(stderr, _("Could not handle the top level "
3504 "directory permissions for the "
3505 "share: %s\n"), info502.name);
3506 nt_status = NT_STATUS_UNSUCCESSFUL;
3510 if (!sync_files(&cp_clistate, mask)) {
3511 d_fprintf(stderr, _("could not handle files for share: "
3512 "%s\n"), info502.name);
3513 nt_status = NT_STATUS_UNSUCCESSFUL;
3518 nt_status = NT_STATUS_OK;
3523 cli_shutdown(cp_clistate.cli_share_src);
3526 cli_shutdown(cp_clistate.cli_share_dst);
3533 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3535 if (c->display_usage) {
3536 d_printf(_("Usage:\n"
3537 "net share migrate files\n"
3538 " Migrate files to local server\n"));
3543 d_printf(_("no server to migrate\n"));
3547 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3548 rpc_share_migrate_files_internals,
3553 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3555 * All parameters are provided by the run_rpc_command function, except for
3556 * argc, argv which are passed through.
3558 * @param domain_sid The domain sid acquired from the remote server.
3559 * @param cli A cli_state connected to the server.
3560 * @param mem_ctx Talloc context, destroyed on completion of the function.
3561 * @param argc Standard main() style argc.
3562 * @param argv Standard main() style argv. Initial components are already
3565 * @return Normal NTSTATUS return.
3568 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3569 const DOM_SID *domain_sid,
3570 const char *domain_name,
3571 struct cli_state *cli,
3572 struct rpc_pipe_client *pipe_hnd,
3573 TALLOC_CTX *mem_ctx,
3578 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3579 struct srvsvc_NetShareInfoCtr ctr_src;
3580 union srvsvc_NetShareInfo info;
3582 struct rpc_pipe_client *srvsvc_pipe = NULL;
3583 struct cli_state *cli_dst = NULL;
3584 uint32 level = 502; /* includes secdesc */
3585 uint32_t parm_error = 0;
3587 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3590 if (!W_ERROR_IS_OK(result))
3593 /* connect destination PI_SRVSVC */
3594 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3595 &ndr_table_srvsvc.syntax_id);
3596 if (!NT_STATUS_IS_OK(nt_status))
3600 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3602 struct srvsvc_NetShareInfo502 info502 =
3603 ctr_src.ctr.ctr502->array[i];
3605 /* reset error-code */
3606 nt_status = NT_STATUS_UNSUCCESSFUL;
3608 if (!check_share_sanity(c, cli, info502.name, info502.type))
3611 printf(_("migrating: [%s], path: %s, comment: %s, including "
3613 info502.name, info502.path, info502.comment);
3616 display_sec_desc(info502.sd_buf.sd);
3618 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3619 info.info502 = &info502;
3621 /* finally modify the share on the dst server */
3622 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3623 srvsvc_pipe->desthost,
3629 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3630 printf(_("cannot set share-acl: %s\n"),
3631 win_errstr(result));
3637 nt_status = NT_STATUS_OK;
3641 cli_shutdown(cli_dst);
3649 * Migrate share-acls from a RPC server to another.
3651 * @param argc Standard main() style argc.
3652 * @param argv Standard main() style argv. Initial components are already
3655 * @return A shell status integer (0 for success).
3657 static int rpc_share_migrate_security(struct net_context *c, int argc,
3660 if (c->display_usage) {
3661 d_printf(_("Usage:\n"
3662 "net rpc share migrate security\n"
3663 " Migrate share-acls to local server\n"));
3668 d_printf(_("no server to migrate\n"));
3672 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3673 rpc_share_migrate_security_internals,
3678 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3679 * from one server to another.
3681 * @param argc Standard main() style argc.
3682 * @param argv Standard main() style argv. Initial components are already
3685 * @return A shell status integer (0 for success).
3688 static int rpc_share_migrate_all(struct net_context *c, int argc,
3693 if (c->display_usage) {
3694 d_printf(_("Usage:\n"
3695 "net rpc share migrate all\n"
3696 " Migrates shares including all share settings\n"));
3701 d_printf(_("no server to migrate\n"));
3705 /* order is important. we don't want to be locked out by the share-acl
3706 * before copying files - gd */
3708 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3709 rpc_share_migrate_shares_internals, argc, argv);
3713 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3714 rpc_share_migrate_files_internals, argc, argv);
3718 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3719 rpc_share_migrate_security_internals, argc,
3725 * 'net rpc share migrate' entrypoint.
3726 * @param argc Standard main() style argc.
3727 * @param argv Standard main() style argv. Initial components are already
3730 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3733 struct functable func[] = {
3736 rpc_share_migrate_all,
3738 N_("Migrate shares from remote to local server"),
3739 N_("net rpc share migrate all\n"
3740 " Migrate shares from remote to local server")
3744 rpc_share_migrate_files,
3746 N_("Migrate files from remote to local server"),
3747 N_("net rpc share migrate files\n"
3748 " Migrate files from remote to local server")
3752 rpc_share_migrate_security,
3754 N_("Migrate share-ACLs from remote to local server"),
3755 N_("net rpc share migrate security\n"
3756 " Migrate share-ACLs from remote to local server")
3760 rpc_share_migrate_shares,
3762 N_("Migrate shares from remote to local server"),
3763 N_("net rpc share migrate shares\n"
3764 " Migrate shares from remote to local server")
3766 {NULL, NULL, 0, NULL, NULL}
3769 net_mode_share = NET_MODE_SHARE_MIGRATE;
3771 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3780 static int num_server_aliases;
3781 static struct full_alias *server_aliases;
3784 * Add an alias to the static list.
3786 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3788 if (server_aliases == NULL)
3789 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3791 server_aliases[num_server_aliases] = *alias;
3792 num_server_aliases += 1;
3796 * For a specific domain on the server, fetch all the aliases
3797 * and their members. Add all of them to the server_aliases.
3800 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3801 TALLOC_CTX *mem_ctx,
3802 struct policy_handle *connect_pol,
3803 const DOM_SID *domain_sid)
3805 uint32 start_idx, max_entries, num_entries, i;
3806 struct samr_SamArray *groups = NULL;
3808 struct policy_handle domain_pol;
3810 /* Get domain policy handle */
3812 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3814 MAXIMUM_ALLOWED_ACCESS,
3815 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3817 if (!NT_STATUS_IS_OK(result))
3824 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3830 for (i = 0; i < num_entries; i++) {
3832 struct policy_handle alias_pol;
3833 struct full_alias alias;
3834 struct lsa_SidArray sid_array;
3837 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3839 MAXIMUM_ALLOWED_ACCESS,
3840 groups->entries[i].idx,
3842 if (!NT_STATUS_IS_OK(result))
3845 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3848 if (!NT_STATUS_IS_OK(result))
3851 alias.num_members = sid_array.num_sids;
3853 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3854 if (!NT_STATUS_IS_OK(result))
3857 alias.members = NULL;
3859 if (alias.num_members > 0) {
3860 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3862 for (j = 0; j < alias.num_members; j++)
3863 sid_copy(&alias.members[j],
3864 sid_array.sids[j].sid);
3867 sid_copy(&alias.sid, domain_sid);
3868 sid_append_rid(&alias.sid, groups->entries[i].idx);
3870 push_alias(mem_ctx, &alias);
3872 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3874 result = NT_STATUS_OK;
3877 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3883 * Dump server_aliases as names for debugging purposes.
3886 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3887 const DOM_SID *domain_sid,
3888 const char *domain_name,
3889 struct cli_state *cli,
3890 struct rpc_pipe_client *pipe_hnd,
3891 TALLOC_CTX *mem_ctx,
3897 struct policy_handle lsa_pol;
3899 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3900 SEC_FLAG_MAXIMUM_ALLOWED,
3902 if (!NT_STATUS_IS_OK(result))
3905 for (i=0; i<num_server_aliases; i++) {
3908 enum lsa_SidType *types;
3911 struct full_alias *alias = &server_aliases[i];
3913 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3915 &domains, &names, &types);
3916 if (!NT_STATUS_IS_OK(result))
3919 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3921 if (alias->num_members == 0) {
3926 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3929 &domains, &names, &types);
3931 if (!NT_STATUS_IS_OK(result) &&
3932 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3935 for (j=0; j<alias->num_members; j++)
3936 DEBUG(1, ("%s\\%s (%d); ",
3937 domains[j] ? domains[j] : "*unknown*",
3938 names[j] ? names[j] : "*unknown*",types[j]));
3942 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3944 return NT_STATUS_OK;
3948 * Fetch a list of all server aliases and their members into
3952 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3953 const DOM_SID *domain_sid,
3954 const char *domain_name,
3955 struct cli_state *cli,
3956 struct rpc_pipe_client *pipe_hnd,
3957 TALLOC_CTX *mem_ctx,
3962 struct policy_handle connect_pol;
3964 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3966 MAXIMUM_ALLOWED_ACCESS,
3969 if (!NT_STATUS_IS_OK(result))
3972 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3973 &global_sid_Builtin);
3975 if (!NT_STATUS_IS_OK(result))
3978 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3981 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
3986 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3988 token->num_sids = 4;
3990 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
3991 d_fprintf(stderr, _("malloc failed\n"));
3992 token->num_sids = 0;
3996 token->user_sids[0] = *user_sid;
3997 sid_copy(&token->user_sids[1], &global_sid_World);
3998 sid_copy(&token->user_sids[2], &global_sid_Network);
3999 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4002 static void free_user_token(NT_USER_TOKEN *token)
4004 SAFE_FREE(token->user_sids);
4007 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4009 if (is_sid_in_token(token, sid))
4012 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4013 if (!token->user_sids) {
4017 sid_copy(&token->user_sids[token->num_sids], sid);
4019 token->num_sids += 1;
4024 NT_USER_TOKEN token;
4027 static void dump_user_token(struct user_token *token)
4031 d_printf("%s\n", token->name);
4033 for (i=0; i<token->token.num_sids; i++) {
4034 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4038 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4042 for (i=0; i<alias->num_members; i++) {
4043 if (sid_compare(sid, &alias->members[i]) == 0)
4050 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4054 for (i=0; i<num_server_aliases; i++) {
4055 if (is_alias_member(&sid, &server_aliases[i]))
4056 add_sid_to_token(token, &server_aliases[i].sid);
4061 * We got a user token with all the SIDs we can know about without asking the
4062 * server directly. These are the user and domain group sids. All of these can
4063 * be members of aliases. So scan the list of aliases for each of the SIDs and
4064 * add them to the token.
4067 static void collect_alias_memberships(NT_USER_TOKEN *token)
4069 int num_global_sids = token->num_sids;
4072 for (i=0; i<num_global_sids; i++) {
4073 collect_sid_memberships(token, token->user_sids[i]);
4077 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4079 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4080 enum wbcSidType type;
4082 struct wbcDomainSid wsid;
4083 char *sid_str = NULL;
4085 uint32_t num_groups;
4086 gid_t *groups = NULL;
4089 fstr_sprintf(full_name, "%s%c%s",
4090 domain, *lp_winbind_separator(), user);
4092 /* First let's find out the user sid */
4094 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4096 if (!WBC_ERROR_IS_OK(wbc_status)) {
4097 DEBUG(1, ("winbind could not find %s: %s\n",
4098 full_name, wbcErrorString(wbc_status)));
4102 wbc_status = wbcSidToString(&wsid, &sid_str);
4103 if (!WBC_ERROR_IS_OK(wbc_status)) {
4107 if (type != SID_NAME_USER) {
4108 wbcFreeMemory(sid_str);
4109 DEBUG(1, ("%s is not a user\n", full_name));
4113 if (!string_to_sid(&user_sid, sid_str)) {
4114 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4118 wbcFreeMemory(sid_str);
4121 init_user_token(token, &user_sid);
4123 /* And now the groups winbind knows about */
4125 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4126 if (!WBC_ERROR_IS_OK(wbc_status)) {
4127 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4128 full_name, wbcErrorString(wbc_status)));
4132 for (i = 0; i < num_groups; i++) {
4133 gid_t gid = groups[i];
4136 wbc_status = wbcGidToSid(gid, &wsid);
4137 if (!WBC_ERROR_IS_OK(wbc_status)) {
4138 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4139 (unsigned int)gid, wbcErrorString(wbc_status)));
4140 wbcFreeMemory(groups);
4144 wbc_status = wbcSidToString(&wsid, &sid_str);
4145 if (!WBC_ERROR_IS_OK(wbc_status)) {
4146 wbcFreeMemory(groups);
4150 DEBUG(3, (" %s\n", sid_str));
4152 string_to_sid(&sid, sid_str);
4153 wbcFreeMemory(sid_str);
4156 add_sid_to_token(token, &sid);
4158 wbcFreeMemory(groups);
4164 * Get a list of all user tokens we want to look at
4167 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4168 struct user_token **user_tokens)
4170 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4171 uint32_t i, num_users;
4173 struct user_token *result;
4174 TALLOC_CTX *frame = NULL;
4176 if (lp_winbind_use_default_domain() &&
4177 (c->opt_target_workgroup == NULL)) {
4178 d_fprintf(stderr, _("winbind use default domain = yes set, "
4179 "please specify a workgroup\n"));
4183 /* Send request to winbind daemon */
4185 wbc_status = wbcListUsers(NULL, &num_users, &users);
4186 if (!WBC_ERROR_IS_OK(wbc_status)) {
4187 DEBUG(1, (_("winbind could not list users: %s\n"),
4188 wbcErrorString(wbc_status)));
4192 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4194 if (result == NULL) {
4195 DEBUG(1, ("Could not malloc sid array\n"));
4196 wbcFreeMemory(users);
4200 frame = talloc_stackframe();
4201 for (i=0; i < num_users; i++) {
4202 fstring domain, user;
4205 fstrcpy(result[i].name, users[i]);
4207 p = strchr(users[i], *lp_winbind_separator());
4209 DEBUG(3, ("%s\n", users[i]));
4212 fstrcpy(domain, c->opt_target_workgroup);
4213 fstrcpy(user, users[i]);
4216 fstrcpy(domain, users[i]);
4221 get_user_sids(domain, user, &(result[i].token));
4225 wbcFreeMemory(users);
4227 *num_tokens = num_users;
4228 *user_tokens = result;
4233 static bool get_user_tokens_from_file(FILE *f,
4235 struct user_token **tokens)
4237 struct user_token *token = NULL;
4242 if (fgets(line, sizeof(line)-1, f) == NULL) {
4246 if (line[strlen(line)-1] == '\n')
4247 line[strlen(line)-1] = '\0';
4249 if (line[0] == ' ') {
4253 if(!string_to_sid(&sid, &line[1])) {
4254 DEBUG(1,("get_user_tokens_from_file: Could "
4255 "not convert sid %s \n",&line[1]));
4259 if (token == NULL) {
4260 DEBUG(0, ("File does not begin with username"));
4264 add_sid_to_token(&token->token, &sid);
4268 /* And a new user... */
4271 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4272 if (*tokens == NULL) {
4273 DEBUG(0, ("Could not realloc tokens\n"));
4277 token = &((*tokens)[*num_tokens-1]);
4279 fstrcpy(token->name, line);
4280 token->token.num_sids = 0;
4281 token->token.user_sids = NULL;
4290 * Show the list of all users that have access to a share
4293 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4294 TALLOC_CTX *mem_ctx,
4295 const char *netname,
4297 struct user_token *tokens)
4300 SEC_DESC *share_sd = NULL;
4301 SEC_DESC *root_sd = NULL;
4302 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4304 union srvsvc_NetShareInfo info;
4309 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4316 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4317 DEBUG(1, ("Coult not query secdesc for share %s\n",
4322 share_sd = info.info502->sd_buf.sd;
4323 if (share_sd == NULL) {
4324 DEBUG(1, ("Got no secdesc for share %s\n",
4330 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4334 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4335 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4336 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4339 for (i=0; i<num_tokens; i++) {
4342 if (share_sd != NULL) {
4343 status = se_access_check(share_sd, &tokens[i].token,
4346 if (!NT_STATUS_IS_OK(status)) {
4347 DEBUG(1, ("Could not check share_sd for "
4354 if (root_sd == NULL) {
4355 d_printf(" %s\n", tokens[i].name);
4359 status = se_access_check(root_sd, &tokens[i].token,
4361 if (!NT_STATUS_IS_OK(status)) {
4362 DEBUG(1, ("Could not check root_sd for user %s\n",
4366 d_printf(" %s\n", tokens[i].name);
4369 if (fnum != (uint16_t)-1)
4370 cli_close(cli, fnum);
4382 static void collect_share(const char *name, uint32 m,
4383 const char *comment, void *state)
4385 struct share_list *share_list = (struct share_list *)state;
4387 if (m != STYPE_DISKTREE)
4390 share_list->num_shares += 1;
4391 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4392 if (!share_list->shares) {
4393 share_list->num_shares = 0;
4396 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4400 * List shares on a remote RPC server, including the security descriptors.
4402 * All parameters are provided by the run_rpc_command function, except for
4403 * argc, argv which are passed through.
4405 * @param domain_sid The domain sid acquired from the remote server.
4406 * @param cli A cli_state connected to the server.
4407 * @param mem_ctx Talloc context, destroyed on completion of the function.
4408 * @param argc Standard main() style argc.
4409 * @param argv Standard main() style argv. Initial components are already
4412 * @return Normal NTSTATUS return.
4415 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4416 const DOM_SID *domain_sid,
4417 const char *domain_name,
4418 struct cli_state *cli,
4419 struct rpc_pipe_client *pipe_hnd,
4420 TALLOC_CTX *mem_ctx,
4429 struct user_token *tokens = NULL;
4432 struct share_list share_list;
4437 f = fopen(argv[0], "r");
4441 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4442 return NT_STATUS_UNSUCCESSFUL;
4445 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4451 DEBUG(0, ("Could not read users from file\n"));
4452 return NT_STATUS_UNSUCCESSFUL;
4455 for (i=0; i<num_tokens; i++)
4456 collect_alias_memberships(&tokens[i].token);
4458 share_list.num_shares = 0;
4459 share_list.shares = NULL;
4461 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4464 DEBUG(0, ("Error returning browse list: %s\n",
4469 for (i = 0; i < share_list.num_shares; i++) {
4470 char *netname = share_list.shares[i];
4472 if (netname[strlen(netname)-1] == '$')
4475 d_printf("%s\n", netname);
4477 show_userlist(pipe_hnd, mem_ctx, netname,
4478 num_tokens, tokens);
4481 for (i=0; i<num_tokens; i++) {
4482 free_user_token(&tokens[i].token);
4485 SAFE_FREE(share_list.shares);
4487 return NT_STATUS_OK;
4490 static int rpc_share_allowedusers(struct net_context *c, int argc,
4495 if (c->display_usage) {
4496 d_printf(_("Usage:\n"
4497 "net rpc share allowedusers\n"
4498 " List allowed users\n"));
4502 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4503 rpc_aliaslist_internals,
4508 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4514 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4515 rpc_share_allowedusers_internals,
4519 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4522 struct user_token *tokens = NULL;
4526 net_usersidlist_usage(c, argc, argv);
4530 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4531 DEBUG(0, ("Could not get the user/sid list\n"));
4535 for (i=0; i<num_tokens; i++) {
4536 dump_user_token(&tokens[i]);
4537 free_user_token(&tokens[i].token);
4544 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4546 d_printf(_("net usersidlist\n"
4547 "\tprints out a list of all users the running winbind knows\n"
4548 "\tabout, together with all their SIDs. This is used as\n"
4549 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4551 net_common_flags_usage(c, argc, argv);
4556 * 'net rpc share' entrypoint.
4557 * @param argc Standard main() style argc.
4558 * @param argv Standard main() style argv. Initial components are already
4562 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4564 NET_API_STATUS status;
4566 struct functable func[] = {
4572 N_("net rpc share add\n"
4580 N_("net rpc share delete\n"
4585 rpc_share_allowedusers,
4587 N_("Modify allowed users"),
4588 N_("net rpc share allowedusers\n"
4589 " Modify allowed users")
4595 N_("Migrate share to local server"),
4596 N_("net rpc share migrate\n"
4597 " Migrate share to local server")
4604 N_("net rpc share list\n"
4607 {NULL, NULL, 0, NULL, NULL}
4610 status = libnetapi_init(&c->netapi_ctx);
4614 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4615 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4616 if (c->opt_kerberos) {
4617 libnetapi_set_use_kerberos(c->netapi_ctx);
4621 if (c->display_usage) {
4622 d_printf(_("Usage:\n"
4625 " Alias for net rpc share list\n"));
4626 net_display_usage_from_functable(func);
4630 return rpc_share_list(c, argc, argv);
4633 return net_run_function(c, argc, argv, "net rpc share", func);
4636 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4637 TALLOC_CTX *mem_ctx,
4638 struct rpc_sh_ctx *ctx,
4639 struct rpc_pipe_client *pipe_hnd,
4640 int argc, const char **argv)
4643 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4646 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4647 TALLOC_CTX *mem_ctx,
4648 struct rpc_sh_ctx *ctx,
4649 struct rpc_pipe_client *pipe_hnd,
4650 int argc, const char **argv)
4652 NET_API_STATUS status;
4653 uint32_t parm_err = 0;
4654 struct SHARE_INFO_2 i2;
4656 if ((argc < 2) || (argc > 3)) {
4657 d_fprintf(stderr, _("usage: %s <share> <path> [comment]\n"),
4659 return NT_STATUS_INVALID_PARAMETER;
4662 i2.shi2_netname = argv[0];
4663 i2.shi2_type = STYPE_DISKTREE;
4664 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4665 i2.shi2_permissions = 0;
4666 i2.shi2_max_uses = 0;
4667 i2.shi2_current_uses = 0;
4668 i2.shi2_path = argv[1];
4669 i2.shi2_passwd = NULL;
4671 status = NetShareAdd(pipe_hnd->desthost,
4676 return werror_to_ntstatus(W_ERROR(status));
4679 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4680 TALLOC_CTX *mem_ctx,
4681 struct rpc_sh_ctx *ctx,
4682 struct rpc_pipe_client *pipe_hnd,
4683 int argc, const char **argv)
4686 d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
4687 return NT_STATUS_INVALID_PARAMETER;
4690 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4693 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4694 TALLOC_CTX *mem_ctx,
4695 struct rpc_sh_ctx *ctx,
4696 struct rpc_pipe_client *pipe_hnd,
4697 int argc, const char **argv)
4699 union srvsvc_NetShareInfo info;
4704 d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
4705 return NT_STATUS_INVALID_PARAMETER;
4708 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4714 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4718 d_printf(_("Name: %s\n"), info.info2->name);
4719 d_printf(_("Comment: %s\n"), info.info2->comment);
4720 d_printf(_("Path: %s\n"), info.info2->path);
4721 d_printf(_("Password: %s\n"), info.info2->password);
4724 return werror_to_ntstatus(result);
4727 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4728 struct rpc_sh_ctx *ctx)
4730 static struct rpc_sh_cmd cmds[] = {
4732 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4733 N_("List available shares") },
4735 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4736 N_("Add a share") },
4738 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4739 N_("Delete a share") },
4741 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4742 N_("Get information about a share") },
4744 { NULL, NULL, 0, NULL, NULL }
4750 /****************************************************************************/
4752 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4754 return net_file_usage(c, argc, argv);
4758 * Close a file on a remote RPC server.
4760 * @param argc Standard main() style argc.
4761 * @param argv Standard main() style argv. Initial components are already
4764 * @return A shell status integer (0 for success).
4766 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4768 if (argc < 1 || c->display_usage) {
4769 return rpc_file_usage(c, argc, argv);
4772 return NetFileClose(c->opt_host, atoi(argv[0]));
4776 * Formatted print of open file info
4778 * @param r struct FILE_INFO_3 contents
4781 static void display_file_info_3(struct FILE_INFO_3 *r)
4783 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4784 r->fi3_id, r->fi3_username, r->fi3_permissions,
4785 r->fi3_num_locks, r->fi3_pathname);
4789 * List files for a user on a remote RPC server.
4791 * @param argc Standard main() style argc.
4792 * @param argv Standard main() style argv. Initial components are already
4795 * @return A shell status integer (0 for success)..
4798 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4800 NET_API_STATUS status;
4801 uint32 preferred_len = 0xffffffff, i;
4802 const char *username=NULL;
4803 uint32_t total_entries = 0;
4804 uint32_t entries_read = 0;
4805 uint32_t resume_handle = 0;
4806 struct FILE_INFO_3 *i3 = NULL;
4808 if (c->display_usage) {
4809 return rpc_file_usage(c, argc, argv);
4812 /* if argc > 0, must be user command */
4814 username = smb_xstrdup(argv[0]);
4817 status = NetFileEnum(c->opt_host,
4821 (uint8_t **)(void *)&i3,
4831 /* Display results */
4834 "\nEnumerating open files on remote server:\n\n"
4835 "\nFileId Opened by Perms Locks Path"
4836 "\n------ --------- ----- ----- ---- \n"));
4837 for (i = 0; i < entries_read; i++) {
4838 display_file_info_3(&i3[i]);
4845 * 'net rpc file' entrypoint.
4846 * @param argc Standard main() style argc.
4847 * @param argv Standard main() style argv. Initial components are already
4851 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4853 NET_API_STATUS status;
4855 struct functable func[] = {
4860 N_("Close opened file"),
4861 N_("net rpc file close\n"
4862 " Close opened file")
4868 N_("List files opened by user"),
4869 N_("net rpc file user\n"
4870 " List files opened by user")
4877 N_("Display information about opened file"),
4878 N_("net rpc file info\n"
4879 " Display information about opened file")
4882 {NULL, NULL, 0, NULL, NULL}
4885 status = libnetapi_init(&c->netapi_ctx);
4889 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4890 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4891 if (c->opt_kerberos) {
4892 libnetapi_set_use_kerberos(c->netapi_ctx);
4896 if (c->display_usage) {
4897 d_printf(_("Usage:\n"));
4898 d_printf(_("net rpc file\n"
4899 " List opened files\n"));
4900 net_display_usage_from_functable(func);
4904 return rpc_file_user(c, argc, argv);
4907 return net_run_function(c, argc, argv, "net rpc file", func);
4911 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4913 * All parameters are provided by the run_rpc_command function, except for
4914 * argc, argv which are passed through.
4916 * @param c A net_context structure.
4917 * @param domain_sid The domain sid acquired from the remote server.
4918 * @param cli A cli_state connected to the server.
4919 * @param mem_ctx Talloc context, destroyed on completion of the function.
4920 * @param argc Standard main() style argc.
4921 * @param argv Standard main() style argv. Initial components are already
4924 * @return Normal NTSTATUS return.
4927 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4928 const DOM_SID *domain_sid,
4929 const char *domain_name,
4930 struct cli_state *cli,
4931 struct rpc_pipe_client *pipe_hnd,
4932 TALLOC_CTX *mem_ctx,
4936 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4938 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4940 if (NT_STATUS_IS_OK(result)) {
4941 d_printf(_("\nShutdown successfully aborted\n"));
4942 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4944 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4950 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4952 * All parameters are provided by the run_rpc_command function, except for
4953 * argc, argv which are passed through.
4955 * @param c A net_context structure.
4956 * @param domain_sid The domain sid acquired from the remote server.
4957 * @param cli A cli_state connected to the server.
4958 * @param mem_ctx Talloc context, destroyed on completion of the function.
4959 * @param argc Standard main() style argc.
4960 * @param argv Standard main() style argv. Initial components are already
4963 * @return Normal NTSTATUS return.
4966 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4967 const DOM_SID *domain_sid,
4968 const char *domain_name,
4969 struct cli_state *cli,
4970 struct rpc_pipe_client *pipe_hnd,
4971 TALLOC_CTX *mem_ctx,
4975 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4977 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
4979 if (NT_STATUS_IS_OK(result)) {
4980 d_printf(_("\nShutdown successfully aborted\n"));
4981 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4983 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4989 * ABORT the shutdown of a remote RPC server.
4991 * @param argc Standard main() style argc.
4992 * @param argv Standard main() style argv. Initial components are already
4995 * @return A shell status integer (0 for success).
4998 static int rpc_shutdown_abort(struct net_context *c, int argc,
5003 if (c->display_usage) {
5004 d_printf(_("Usage:\n"
5005 "net rpc abortshutdown\n"
5006 " Abort a scheduled shutdown\n"));
5010 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5011 rpc_shutdown_abort_internals, argc, argv);
5016 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5018 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5019 rpc_reg_shutdown_abort_internals,
5024 * Shut down a remote RPC Server via initshutdown pipe.
5026 * All parameters are provided by the run_rpc_command function, except for
5027 * argc, argv which are passed through.
5029 * @param c A net_context structure.
5030 * @param domain_sid The domain sid acquired from the remote server.
5031 * @param cli A cli_state connected to the server.
5032 * @param mem_ctx Talloc context, destroyed on completion of the function.
5033 * @param argc Standard main() style argc.
5034 * @param argv Standard main() style argv. Initial components are already
5037 * @return Normal NTSTATUS return.
5040 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5041 const DOM_SID *domain_sid,
5042 const char *domain_name,
5043 struct cli_state *cli,
5044 struct rpc_pipe_client *pipe_hnd,
5045 TALLOC_CTX *mem_ctx,
5049 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5050 const char *msg = N_("This machine will be shutdown shortly");
5051 uint32 timeout = 20;
5052 struct lsa_StringLarge msg_string;
5054 if (c->opt_comment) {
5055 msg = c->opt_comment;
5057 if (c->opt_timeout) {
5058 timeout = c->opt_timeout;
5061 msg_string.string = msg;
5063 /* create an entry */
5064 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5065 &msg_string, timeout, c->opt_force, c->opt_reboot,
5068 if (NT_STATUS_IS_OK(result)) {
5069 d_printf(_("\nShutdown of remote machine succeeded\n"));
5070 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5072 DEBUG(1,("Shutdown of remote machine failed!\n"));
5078 * Shut down a remote RPC Server via winreg pipe.
5080 * All parameters are provided by the run_rpc_command function, except for
5081 * argc, argv which are passed through.
5083 * @param c A net_context structure.
5084 * @param domain_sid The domain sid acquired from the remote server.
5085 * @param cli A cli_state connected to the server.
5086 * @param mem_ctx Talloc context, destroyed on completion of the function.
5087 * @param argc Standard main() style argc.
5088 * @param argv Standard main() style argv. Initial components are already
5091 * @return Normal NTSTATUS return.
5094 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5095 const DOM_SID *domain_sid,
5096 const char *domain_name,
5097 struct cli_state *cli,
5098 struct rpc_pipe_client *pipe_hnd,
5099 TALLOC_CTX *mem_ctx,
5103 const char *msg = N_("This machine will be shutdown shortly");
5104 uint32 timeout = 20;
5105 struct lsa_StringLarge msg_string;
5109 if (c->opt_comment) {
5110 msg = c->opt_comment;
5112 msg_string.string = msg;
5114 if (c->opt_timeout) {
5115 timeout = c->opt_timeout;
5118 /* create an entry */
5119 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5120 &msg_string, timeout, c->opt_force, c->opt_reboot,
5123 if (NT_STATUS_IS_OK(result)) {
5124 d_printf(_("\nShutdown of remote machine succeeded\n"));
5126 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5127 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5128 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5130 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5137 * Shut down a remote RPC server.
5139 * @param argc Standard main() style argc.
5140 * @param argv Standard main() style argv. Initial components are already
5143 * @return A shell status integer (0 for success).
5146 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5150 if (c->display_usage) {
5151 d_printf(_("Usage:\n"
5152 "net rpc shutdown\n"
5153 " Shut down a remote RPC server\n"));
5157 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5158 rpc_init_shutdown_internals, argc, argv);
5161 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5162 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5163 rpc_reg_shutdown_internals, argc, argv);
5169 /***************************************************************************
5170 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5171 ***************************************************************************/
5174 * Add interdomain trust account to the RPC server.
5175 * All parameters (except for argc and argv) are passed by run_rpc_command
5178 * @param c A net_context structure.
5179 * @param domain_sid The domain sid acquired from the server.
5180 * @param cli A cli_state connected to the server.
5181 * @param mem_ctx Talloc context, destroyed on completion of the function.
5182 * @param argc Standard main() style argc.
5183 * @param argv Standard main() style argv. Initial components are already
5186 * @return normal NTSTATUS return code.
5189 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5190 const DOM_SID *domain_sid,
5191 const char *domain_name,
5192 struct cli_state *cli,
5193 struct rpc_pipe_client *pipe_hnd,
5194 TALLOC_CTX *mem_ctx,
5198 struct policy_handle connect_pol, domain_pol, user_pol;
5199 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5201 struct lsa_String lsa_acct_name;
5203 uint32 acct_flags=0;
5205 uint32_t access_granted = 0;
5206 union samr_UserInfo info;
5207 unsigned int orig_timeout;
5210 d_printf(_("Usage: net rpc trustdom add <domain_name> "
5211 "<trust password>\n"));
5212 return NT_STATUS_INVALID_PARAMETER;
5216 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5219 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5220 return NT_STATUS_NO_MEMORY;
5223 strupper_m(acct_name);
5225 init_lsa_String(&lsa_acct_name, acct_name);
5227 /* Get samr policy handle */
5228 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5230 MAXIMUM_ALLOWED_ACCESS,
5232 if (!NT_STATUS_IS_OK(result)) {
5236 /* Get domain policy handle */
5237 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5239 MAXIMUM_ALLOWED_ACCESS,
5240 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5242 if (!NT_STATUS_IS_OK(result)) {
5246 /* This call can take a long time - allow the server to time out.
5247 * 35 seconds should do it. */
5249 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5251 /* Create trusting domain's account */
5252 acb_info = ACB_NORMAL;
5253 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5254 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5255 SAMR_USER_ACCESS_SET_PASSWORD |
5256 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5257 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5259 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5268 /* And restore our original timeout. */
5269 rpccli_set_timeout(pipe_hnd, orig_timeout);
5271 if (!NT_STATUS_IS_OK(result)) {
5272 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5273 acct_name, nt_errstr(result));
5278 struct samr_CryptPassword crypt_pwd;
5280 ZERO_STRUCT(info.info23);
5282 init_samr_CryptPassword(argv[1],
5283 &cli->user_session_key,
5286 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5287 SAMR_FIELD_NT_PASSWORD_PRESENT;
5288 info.info23.info.acct_flags = ACB_DOMTRUST;
5289 info.info23.password = crypt_pwd;
5291 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5296 if (!NT_STATUS_IS_OK(result)) {
5297 DEBUG(0,("Could not set trust account password: %s\n",
5298 nt_errstr(result)));
5304 SAFE_FREE(acct_name);
5309 * Create interdomain trust account for a remote domain.
5311 * @param argc Standard argc.
5312 * @param argv Standard argv without initial components.
5314 * @return Integer status (0 means success).
5317 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5319 if (argc > 0 && !c->display_usage) {
5320 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5321 rpc_trustdom_add_internals, argc, argv);
5323 d_printf(_("Usage:\n"
5324 "net rpc trustdom add <domain_name> <trust "
5332 * Remove interdomain trust account from the RPC server.
5333 * All parameters (except for argc and argv) are passed by run_rpc_command
5336 * @param c A net_context structure.
5337 * @param domain_sid The domain sid acquired from the server.
5338 * @param cli A cli_state connected to the server.
5339 * @param mem_ctx Talloc context, destroyed on completion of the function.
5340 * @param argc Standard main() style argc.
5341 * @param argv Standard main() style argv. Initial components are already
5344 * @return normal NTSTATUS return code.
5347 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5348 const DOM_SID *domain_sid,
5349 const char *domain_name,
5350 struct cli_state *cli,
5351 struct rpc_pipe_client *pipe_hnd,
5352 TALLOC_CTX *mem_ctx,
5356 struct policy_handle connect_pol, domain_pol, user_pol;
5357 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5359 DOM_SID trust_acct_sid;
5360 struct samr_Ids user_rids, name_types;
5361 struct lsa_String lsa_acct_name;
5364 d_printf(_("Usage: net rpc trustdom del <domain_name>\n"));
5365 return NT_STATUS_INVALID_PARAMETER;
5369 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5371 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5373 if (acct_name == NULL)
5374 return NT_STATUS_NO_MEMORY;
5376 strupper_m(acct_name);
5378 /* Get samr policy handle */
5379 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5381 MAXIMUM_ALLOWED_ACCESS,
5383 if (!NT_STATUS_IS_OK(result)) {
5387 /* Get domain policy handle */
5388 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5390 MAXIMUM_ALLOWED_ACCESS,
5391 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5393 if (!NT_STATUS_IS_OK(result)) {
5397 init_lsa_String(&lsa_acct_name, acct_name);
5399 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5406 if (!NT_STATUS_IS_OK(result)) {
5407 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5409 acct_name, nt_errstr(result) );
5413 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5415 MAXIMUM_ALLOWED_ACCESS,
5419 if (!NT_STATUS_IS_OK(result)) {
5420 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5422 acct_name, nt_errstr(result) );
5426 /* append the rid to the domain sid */
5427 sid_copy(&trust_acct_sid, domain_sid);
5428 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5432 /* remove the sid */
5434 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5437 if (!NT_STATUS_IS_OK(result)) {
5438 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5439 " on user %s failed %s\n"),
5440 acct_name, nt_errstr(result) );
5446 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5449 if (!NT_STATUS_IS_OK(result)) {
5450 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5452 acct_name, nt_errstr(result) );
5456 if (!NT_STATUS_IS_OK(result)) {
5457 d_printf(_("Could not set trust account password: %s\n"),
5467 * Delete interdomain trust account for a remote domain.
5469 * @param argc Standard argc.
5470 * @param argv Standard argv without initial components.
5472 * @return Integer status (0 means success).
5475 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5477 if (argc > 0 && !c->display_usage) {
5478 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5479 rpc_trustdom_del_internals, argc, argv);
5481 d_printf(_("Usage:\n"
5482 "net rpc trustdom del <domain>\n"));
5487 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5488 struct cli_state *cli,
5489 TALLOC_CTX *mem_ctx,
5490 const char *domain_name)
5492 char *dc_name = NULL;
5493 const char *buffer = NULL;
5494 struct rpc_pipe_client *netr;
5497 /* Use NetServerEnum2 */
5499 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5501 return NT_STATUS_OK;
5504 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5505 for domain %s\n", domain_name));
5507 /* Try netr_GetDcName */
5509 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5511 if (!NT_STATUS_IS_OK(status)) {
5515 status = rpccli_netr_GetDcName(netr, mem_ctx,
5522 if (NT_STATUS_IS_OK(status)) {
5526 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5527 for domain %s\n", domain_name));
5533 * Establish trust relationship to a trusting domain.
5534 * Interdomain account must already be created on remote PDC.
5536 * @param c A net_context structure.
5537 * @param argc Standard argc.
5538 * @param argv Standard argv without initial components.
5540 * @return Integer status (0 means success).
5543 static int rpc_trustdom_establish(struct net_context *c, int argc,
5546 struct cli_state *cli = NULL;
5547 struct sockaddr_storage server_ss;
5548 struct rpc_pipe_client *pipe_hnd = NULL;
5549 struct policy_handle connect_hnd;
5550 TALLOC_CTX *mem_ctx;
5552 DOM_SID *domain_sid;
5557 union lsa_PolicyInformation *info = NULL;
5560 * Connect to \\server\ipc$ as 'our domain' account with password
5563 if (argc != 1 || c->display_usage) {
5564 d_printf(_("Usage:\n"
5565 "net rpc trustdom establish <domain_name>\n"));
5569 domain_name = smb_xstrdup(argv[0]);
5570 strupper_m(domain_name);
5572 /* account name used at first is our domain's name with '$' */
5573 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5576 strupper_m(acct_name);
5579 * opt_workgroup will be used by connection functions further,
5580 * hence it should be set to remote domain name instead of ours
5582 if (c->opt_workgroup) {
5583 c->opt_workgroup = smb_xstrdup(domain_name);
5586 c->opt_user_name = acct_name;
5588 /* find the domain controller */
5589 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5590 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5594 /* connect to ipc$ as username/password */
5595 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5596 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5598 /* Is it trusting domain account for sure ? */
5599 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5600 nt_errstr(nt_status)));
5604 /* store who we connected to */
5606 saf_store( domain_name, pdc_name );
5609 * Connect to \\server\ipc$ again (this time anonymously)
5612 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5615 if (NT_STATUS_IS_ERR(nt_status)) {
5616 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5617 domain_name, nt_errstr(nt_status)));
5621 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5622 "domain %s", domain_name))) {
5623 DEBUG(0, ("talloc_init() failed\n"));
5628 /* Make sure we're talking to a proper server */
5630 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5631 if (!NT_STATUS_IS_OK(nt_status)) {
5633 talloc_destroy(mem_ctx);
5638 * Call LsaOpenPolicy and LsaQueryInfo
5641 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5643 if (!NT_STATUS_IS_OK(nt_status)) {
5644 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5646 talloc_destroy(mem_ctx);
5650 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5652 if (NT_STATUS_IS_ERR(nt_status)) {
5653 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5654 nt_errstr(nt_status)));
5656 talloc_destroy(mem_ctx);
5660 /* Querying info level 5 */
5662 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5664 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5666 if (NT_STATUS_IS_ERR(nt_status)) {
5667 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5668 nt_errstr(nt_status)));
5670 talloc_destroy(mem_ctx);
5674 domain_sid = info->account_domain.sid;
5676 /* There should be actually query info level 3 (following nt serv behaviour),
5677 but I still don't know if it's _really_ necessary */
5680 * Store the password in secrets db
5683 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5684 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5686 talloc_destroy(mem_ctx);
5691 * Close the pipes and clean up
5694 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5695 if (NT_STATUS_IS_ERR(nt_status)) {
5696 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5697 nt_errstr(nt_status)));
5699 talloc_destroy(mem_ctx);
5705 talloc_destroy(mem_ctx);
5707 d_printf(_("Trust to domain %s established\n"), domain_name);
5712 * Revoke trust relationship to the remote domain.
5714 * @param c A net_context structure.
5715 * @param argc Standard argc.
5716 * @param argv Standard argv without initial components.
5718 * @return Integer status (0 means success).
5721 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5727 if (argc < 1 || c->display_usage) {
5728 d_printf(_("Usage:\n"
5729 "net rpc trustdom revoke <domain_name>\n"
5730 " Revoke trust relationship\n"
5731 " domain_name\tName of domain to revoke trust\n"));
5735 /* generate upper cased domain name */
5736 domain_name = smb_xstrdup(argv[0]);
5737 strupper_m(domain_name);
5739 /* delete password of the trust */
5740 if (!pdb_del_trusteddom_pw(domain_name)) {
5741 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5748 SAFE_FREE(domain_name);
5752 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5753 const DOM_SID *domain_sid,
5754 const char *domain_name,
5755 struct cli_state *cli,
5756 struct rpc_pipe_client *pipe_hnd,
5757 TALLOC_CTX *mem_ctx,
5762 sid_to_fstring(str_sid, domain_sid);
5763 d_printf("%s\n", str_sid);
5764 return NT_STATUS_OK;
5767 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5769 fstring ascii_sid, padding;
5770 int pad_len, col_len = 20;
5772 /* convert sid into ascii string */
5773 sid_to_fstring(ascii_sid, dom_sid);
5775 /* calculate padding space for d_printf to look nicer */
5776 pad_len = col_len - strlen(trusted_dom_name);
5777 padding[pad_len] = 0;
5778 do padding[--pad_len] = ' '; while (pad_len);
5780 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5783 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5784 TALLOC_CTX *mem_ctx,
5785 struct policy_handle *pol,
5787 const char *trusted_dom_name)
5790 union lsa_TrustedDomainInfo *info = NULL;
5791 char *cleartextpwd = NULL;
5792 uint8_t session_key[16];
5793 DATA_BLOB session_key_blob;
5794 DATA_BLOB data = data_blob_null;
5796 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5799 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5801 if (NT_STATUS_IS_ERR(nt_status)) {
5802 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5803 nt_errstr(nt_status)));
5807 data = data_blob(info->password.password->data,
5808 info->password.password->length);
5810 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5811 DEBUG(0, ("Could not retrieve password hash\n"));
5815 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5816 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5818 if (cleartextpwd == NULL) {
5819 DEBUG(0,("retrieved NULL password\n"));
5820 nt_status = NT_STATUS_UNSUCCESSFUL;
5824 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5825 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5826 nt_status = NT_STATUS_UNSUCCESSFUL;
5830 #ifdef DEBUG_PASSWORD
5831 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5832 "password: [%s]\n", trusted_dom_name,
5833 sid_string_dbg(&dom_sid), cleartextpwd));
5837 SAFE_FREE(cleartextpwd);
5838 data_blob_free(&data);
5843 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5846 /* common variables */
5847 TALLOC_CTX* mem_ctx;
5848 struct cli_state *cli = NULL;
5849 struct rpc_pipe_client *pipe_hnd = NULL;
5851 const char *domain_name = NULL;
5852 DOM_SID *queried_dom_sid;
5853 struct policy_handle connect_hnd;
5854 union lsa_PolicyInformation *info = NULL;
5856 /* trusted domains listing variables */
5857 unsigned int enum_ctx = 0;
5859 struct lsa_DomainList dom_list;
5862 if (c->display_usage) {
5863 d_printf(_("Usage:\n"
5864 "net rpc trustdom vampire\n"
5865 " Vampire trust relationship from remote server\n"));
5870 * Listing trusted domains (stored in secrets.tdb, if local)
5873 mem_ctx = talloc_init("trust relationships vampire");
5876 * set domain and pdc name to local samba server (default)
5877 * or to remote one given in command line
5880 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5881 domain_name = c->opt_workgroup;
5882 c->opt_target_workgroup = c->opt_workgroup;
5884 fstrcpy(pdc_name, global_myname());
5885 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5886 c->opt_target_workgroup = domain_name;
5889 /* open \PIPE\lsarpc and open policy handle */
5890 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5891 if (!NT_STATUS_IS_OK(nt_status)) {
5892 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5893 nt_errstr(nt_status)));
5894 talloc_destroy(mem_ctx);
5898 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5900 if (!NT_STATUS_IS_OK(nt_status)) {
5901 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5902 nt_errstr(nt_status) ));
5904 talloc_destroy(mem_ctx);
5908 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5910 if (NT_STATUS_IS_ERR(nt_status)) {
5911 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5912 nt_errstr(nt_status)));
5914 talloc_destroy(mem_ctx);
5918 /* query info level 5 to obtain sid of a domain being queried */
5919 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5921 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5924 if (NT_STATUS_IS_ERR(nt_status)) {
5925 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5926 nt_errstr(nt_status)));
5928 talloc_destroy(mem_ctx);
5932 queried_dom_sid = info->account_domain.sid;
5935 * Keep calling LsaEnumTrustdom over opened pipe until
5936 * the end of enumeration is reached
5939 d_printf(_("Vampire trusted domains:\n\n"));
5942 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5947 if (NT_STATUS_IS_ERR(nt_status)) {
5948 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5949 nt_errstr(nt_status)));
5951 talloc_destroy(mem_ctx);
5955 for (i = 0; i < dom_list.count; i++) {
5957 print_trusted_domain(dom_list.domains[i].sid,
5958 dom_list.domains[i].name.string);
5960 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5961 *dom_list.domains[i].sid,
5962 dom_list.domains[i].name.string);
5963 if (!NT_STATUS_IS_OK(nt_status)) {
5965 talloc_destroy(mem_ctx);
5971 * in case of no trusted domains say something rather
5972 * than just display blank line
5974 if (!dom_list.count) d_printf(_("none\n"));
5976 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5978 /* close this connection before doing next one */
5979 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5980 if (NT_STATUS_IS_ERR(nt_status)) {
5981 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5982 nt_errstr(nt_status)));
5984 talloc_destroy(mem_ctx);
5988 /* close lsarpc pipe and connection to IPC$ */
5991 talloc_destroy(mem_ctx);
5995 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
5997 /* common variables */
5998 TALLOC_CTX* mem_ctx;
5999 struct cli_state *cli = NULL, *remote_cli = NULL;
6000 struct rpc_pipe_client *pipe_hnd = NULL;
6002 const char *domain_name = NULL;
6003 DOM_SID *queried_dom_sid;
6005 int ascii_dom_name_len;
6006 struct policy_handle connect_hnd;
6007 union lsa_PolicyInformation *info = NULL;
6009 /* trusted domains listing variables */
6010 unsigned int num_domains, enum_ctx = 0;
6011 int i, pad_len, col_len = 20;
6012 struct lsa_DomainList dom_list;
6015 /* trusting domains listing variables */
6016 struct policy_handle domain_hnd;
6017 struct samr_SamArray *trusts = NULL;
6019 if (c->display_usage) {
6020 d_printf(_("Usage:\n"
6021 "net rpc trustdom list\n"
6022 " List trust relationships\n"));
6027 * Listing trusted domains (stored in secrets.tdb, if local)
6030 mem_ctx = talloc_init("trust relationships listing");
6033 * set domain and pdc name to local samba server (default)
6034 * or to remote one given in command line
6037 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6038 domain_name = c->opt_workgroup;
6039 c->opt_target_workgroup = c->opt_workgroup;
6041 fstrcpy(pdc_name, global_myname());
6042 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6043 c->opt_target_workgroup = domain_name;
6046 /* open \PIPE\lsarpc and open policy handle */
6047 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6048 if (!NT_STATUS_IS_OK(nt_status)) {
6049 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6050 nt_errstr(nt_status)));
6051 talloc_destroy(mem_ctx);
6055 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6057 if (!NT_STATUS_IS_OK(nt_status)) {
6058 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6059 nt_errstr(nt_status) ));
6061 talloc_destroy(mem_ctx);
6065 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6067 if (NT_STATUS_IS_ERR(nt_status)) {
6068 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6069 nt_errstr(nt_status)));
6071 talloc_destroy(mem_ctx);
6075 /* query info level 5 to obtain sid of a domain being queried */
6076 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6078 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6081 if (NT_STATUS_IS_ERR(nt_status)) {
6082 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6083 nt_errstr(nt_status)));
6085 talloc_destroy(mem_ctx);
6089 queried_dom_sid = info->account_domain.sid;
6092 * Keep calling LsaEnumTrustdom over opened pipe until
6093 * the end of enumeration is reached
6096 d_printf(_("Trusted domains list:\n\n"));
6099 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6104 if (NT_STATUS_IS_ERR(nt_status)) {
6105 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6106 nt_errstr(nt_status)));
6108 talloc_destroy(mem_ctx);
6112 for (i = 0; i < dom_list.count; i++) {
6113 print_trusted_domain(dom_list.domains[i].sid,
6114 dom_list.domains[i].name.string);
6118 * in case of no trusted domains say something rather
6119 * than just display blank line
6121 if (!dom_list.count) d_printf(_("none\n"));
6123 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6125 /* close this connection before doing next one */
6126 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6127 if (NT_STATUS_IS_ERR(nt_status)) {
6128 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6129 nt_errstr(nt_status)));
6131 talloc_destroy(mem_ctx);
6135 TALLOC_FREE(pipe_hnd);
6138 * Listing trusting domains (stored in passdb backend, if local)
6141 d_printf(_("\nTrusting domains list:\n\n"));
6144 * Open \PIPE\samr and get needed policy handles
6146 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6148 if (!NT_STATUS_IS_OK(nt_status)) {
6149 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6151 talloc_destroy(mem_ctx);
6156 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6158 SAMR_ACCESS_LOOKUP_DOMAIN,
6160 if (!NT_STATUS_IS_OK(nt_status)) {
6161 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6162 nt_errstr(nt_status)));
6164 talloc_destroy(mem_ctx);
6168 /* SamrOpenDomain - we have to open domain policy handle in order to be
6169 able to enumerate accounts*/
6170 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6172 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6175 if (!NT_STATUS_IS_OK(nt_status)) {
6176 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6177 nt_errstr(nt_status)));
6179 talloc_destroy(mem_ctx);
6184 * perform actual enumeration
6187 enum_ctx = 0; /* reset enumeration context from last enumeration */
6190 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6197 if (NT_STATUS_IS_ERR(nt_status)) {
6198 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6199 nt_errstr(nt_status)));
6201 talloc_destroy(mem_ctx);
6205 for (i = 0; i < num_domains; i++) {
6207 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6210 * get each single domain's sid (do we _really_ need this ?):
6211 * 1) connect to domain's pdc
6212 * 2) query the pdc for domain's sid
6215 /* get rid of '$' tail */
6216 ascii_dom_name_len = strlen(str);
6217 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6218 str[ascii_dom_name_len - 1] = '\0';
6220 /* calculate padding space for d_printf to look nicer */
6221 pad_len = col_len - strlen(str);
6222 padding[pad_len] = 0;
6223 do padding[--pad_len] = ' '; while (pad_len);
6225 /* set opt_* variables to remote domain */
6227 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6228 c->opt_target_workgroup = c->opt_workgroup;
6230 d_printf("%s%s", str, padding);
6232 /* connect to remote domain controller */
6233 nt_status = net_make_ipc_connection(c,
6234 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6236 if (NT_STATUS_IS_OK(nt_status)) {
6237 /* query for domain's sid */
6238 if (run_rpc_command(
6240 &ndr_table_lsarpc.syntax_id, 0,
6241 rpc_query_domain_sid, argc,
6244 _("couldn't get domain's sid\n"));
6246 cli_shutdown(remote_cli);
6249 d_fprintf(stderr, _("domain controller is not "
6250 "responding: %s\n"),
6251 nt_errstr(nt_status));
6255 if (!num_domains) d_printf("none\n");
6257 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6259 /* close opened samr and domain policy handles */
6260 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6261 if (!NT_STATUS_IS_OK(nt_status)) {
6262 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6265 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6266 if (!NT_STATUS_IS_OK(nt_status)) {
6267 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6270 /* close samr pipe and connection to IPC$ */
6273 talloc_destroy(mem_ctx);
6278 * Entrypoint for 'net rpc trustdom' code.
6280 * @param argc Standard argc.
6281 * @param argv Standard argv without initial components.
6283 * @return Integer status (0 means success).
6286 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6288 struct functable func[] = {
6293 N_("Add trusted domain's account"),
6294 N_("net rpc trustdom add\n"
6295 " Add trusted domain's account")
6301 N_("Remove trusted domain's account"),
6302 N_("net rpc trustdom del\n"
6303 " Remove trusted domain's account")
6307 rpc_trustdom_establish,
6309 N_("Establish trust relationship"),
6310 N_("net rpc trustdom establish\n"
6311 " Establish trust relationship")
6315 rpc_trustdom_revoke,
6317 N_("Revoke trust relationship"),
6318 N_("net rpc trustdom revoke\n"
6319 " Revoke trust relationship")
6325 N_("List domain trusts"),
6326 N_("net rpc trustdom list\n"
6327 " List domain trusts")
6331 rpc_trustdom_vampire,
6333 N_("Vampire trusts from remote server"),
6334 N_("net rpc trustdom vampire\n"
6335 " Vampire trusts from remote server")
6337 {NULL, NULL, 0, NULL, NULL}
6340 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6344 * Check if a server will take rpc commands
6345 * @param flags Type of server to connect to (PDC, DMB, localhost)
6346 * if the host is not explicitly specified
6347 * @return bool (true means rpc supported)
6349 bool net_rpc_check(struct net_context *c, unsigned flags)
6351 struct cli_state *cli;
6353 struct sockaddr_storage server_ss;
6354 char *server_name = NULL;
6357 /* flags (i.e. server type) may depend on command */
6358 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6361 if ((cli = cli_initialise()) == NULL) {
6365 status = cli_connect(cli, server_name, &server_ss);
6366 if (!NT_STATUS_IS_OK(status))
6368 if (!attempt_netbios_session_request(&cli, global_myname(),
6369 server_name, &server_ss))
6371 status = cli_negprot(cli);
6372 if (!NT_STATUS_IS_OK(status))
6374 if (cli->protocol < PROTOCOL_NT1)
6383 /* dump sam database via samsync rpc calls */
6384 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6385 if (c->display_usage) {
6386 d_printf(_("Usage:\n"
6388 " Dump remote SAM database\n"));
6392 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6393 NET_FLAGS_ANONYMOUS,
6394 rpc_samdump_internals, argc, argv);
6397 /* syncronise sam database via samsync rpc calls */
6398 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6400 struct functable func[] = {
6405 N_("Dump remote SAM database to ldif"),
6406 N_("net rpc vampire ldif\n"
6407 " Dump remote SAM database to LDIF file or "
6414 N_("Dump remote SAM database to Kerberos Keytab"),
6415 N_("net rpc vampire keytab\n"
6416 " Dump remote SAM database to Kerberos keytab "
6423 N_("Dump remote SAM database to passdb"),
6424 N_("net rpc vampire passdb\n"
6425 " Dump remote SAM database to passdb")
6428 {NULL, NULL, 0, NULL, NULL}
6432 if (c->display_usage) {
6433 d_printf(_("Usage:\n"
6435 " Vampire remote SAM database\n"));
6439 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6440 NET_FLAGS_ANONYMOUS,
6441 rpc_vampire_internals,
6445 return net_run_function(c, argc, argv, "net rpc vampire", func);
6449 * Migrate everything from a print server.
6451 * @param c A net_context structure.
6452 * @param argc Standard main() style argc.
6453 * @param argv Standard main() style argv. Initial components are already
6456 * @return A shell status integer (0 for success).
6458 * The order is important !
6459 * To successfully add drivers the print queues have to exist !
6460 * Applying ACLs should be the last step, because you're easily locked out.
6463 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6468 if (c->display_usage) {
6469 d_printf(_("Usage:\n"
6470 "net rpc printer migrate all\n"
6471 " Migrate everything from a print server\n"));
6476 d_printf(_("no server to migrate\n"));
6480 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6481 rpc_printer_migrate_printers_internals, argc,
6486 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6487 rpc_printer_migrate_drivers_internals, argc,
6492 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6493 rpc_printer_migrate_forms_internals, argc, argv);
6497 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6498 rpc_printer_migrate_settings_internals, argc,
6503 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6504 rpc_printer_migrate_security_internals, argc,
6510 * Migrate print drivers from a print server.
6512 * @param c A net_context structure.
6513 * @param argc Standard main() style argc.
6514 * @param argv Standard main() style argv. Initial components are already
6517 * @return A shell status integer (0 for success).
6519 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6522 if (c->display_usage) {
6523 d_printf(_("Usage:\n"
6524 "net rpc printer migrate drivers\n"
6525 " Migrate print-drivers from a print-server\n"));
6530 d_printf(_("no server to migrate\n"));
6534 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6535 rpc_printer_migrate_drivers_internals,
6540 * Migrate print-forms from a print-server.
6542 * @param c A net_context structure.
6543 * @param argc Standard main() style argc.
6544 * @param argv Standard main() style argv. Initial components are already
6547 * @return A shell status integer (0 for success).
6549 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6552 if (c->display_usage) {
6553 d_printf(_("Usage:\n"
6554 "net rpc printer migrate forms\n"
6555 " Migrate print-forms from a print-server\n"));
6560 d_printf(_("no server to migrate\n"));
6564 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6565 rpc_printer_migrate_forms_internals,
6570 * Migrate printers from a print-server.
6572 * @param c A net_context structure.
6573 * @param argc Standard main() style argc.
6574 * @param argv Standard main() style argv. Initial components are already
6577 * @return A shell status integer (0 for success).
6579 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6582 if (c->display_usage) {
6583 d_printf(_("Usage:\n"
6584 "net rpc printer migrate printers\n"
6585 " Migrate printers from a print-server\n"));
6590 d_printf(_("no server to migrate\n"));
6594 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6595 rpc_printer_migrate_printers_internals,
6600 * Migrate printer-ACLs from a print-server
6602 * @param c A net_context structure.
6603 * @param argc Standard main() style argc.
6604 * @param argv Standard main() style argv. Initial components are already
6607 * @return A shell status integer (0 for success).
6609 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6612 if (c->display_usage) {
6613 d_printf(_("Usage:\n"
6614 "net rpc printer migrate security\n"
6615 " Migrate printer-ACLs from a print-server\n"));
6620 d_printf(_("no server to migrate\n"));
6624 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6625 rpc_printer_migrate_security_internals,
6630 * Migrate printer-settings from a print-server.
6632 * @param c A net_context structure.
6633 * @param argc Standard main() style argc.
6634 * @param argv Standard main() style argv. Initial components are already
6637 * @return A shell status integer (0 for success).
6639 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6642 if (c->display_usage) {
6643 d_printf(_("Usage:\n"
6644 "net rpc printer migrate settings\n"
6645 " Migrate printer-settings from a "
6651 d_printf(_("no server to migrate\n"));
6655 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6656 rpc_printer_migrate_settings_internals,
6661 * 'net rpc printer' entrypoint.
6663 * @param c A net_context structure.
6664 * @param argc Standard main() style argc.
6665 * @param argv Standard main() style argv. Initial components are already
6669 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6672 /* ouch: when addriver and setdriver are called from within
6673 rpc_printer_migrate_drivers_internals, the printer-queue already
6676 struct functable func[] = {
6679 rpc_printer_migrate_all,
6681 N_("Migrate all from remote to local print server"),
6682 N_("net rpc printer migrate all\n"
6683 " Migrate all from remote to local print server")
6687 rpc_printer_migrate_drivers,
6689 N_("Migrate drivers to local server"),
6690 N_("net rpc printer migrate drivers\n"
6691 " Migrate drivers to local server")
6695 rpc_printer_migrate_forms,
6697 N_("Migrate froms to local server"),
6698 N_("net rpc printer migrate forms\n"
6699 " Migrate froms to local server")
6703 rpc_printer_migrate_printers,
6705 N_("Migrate printers to local server"),
6706 N_("net rpc printer migrate printers\n"
6707 " Migrate printers to local server")
6711 rpc_printer_migrate_security,
6713 N_("Mirgate printer ACLs to local server"),
6714 N_("net rpc printer migrate security\n"
6715 " Mirgate printer ACLs to local server")
6719 rpc_printer_migrate_settings,
6721 N_("Migrate printer settings to local server"),
6722 N_("net rpc printer migrate settings\n"
6723 " Migrate printer settings to local server")
6725 {NULL, NULL, 0, NULL, NULL}
6728 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6733 * List printers on a remote RPC server.
6735 * @param c A net_context structure.
6736 * @param argc Standard main() style argc.
6737 * @param argv Standard main() style argv. Initial components are already
6740 * @return A shell status integer (0 for success).
6742 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6744 if (c->display_usage) {
6745 d_printf(_("Usage:\n"
6746 "net rpc printer list\n"
6747 " List printers on a remote RPC server\n"));
6751 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6752 rpc_printer_list_internals,
6757 * List printer-drivers on a remote RPC server.
6759 * @param c A net_context structure.
6760 * @param argc Standard main() style argc.
6761 * @param argv Standard main() style argv. Initial components are already
6764 * @return A shell status integer (0 for success).
6766 static int rpc_printer_driver_list(struct net_context *c, int argc,
6769 if (c->display_usage) {
6770 d_printf(_("Usage:\n"
6771 "net rpc printer driver\n"
6772 " List printer-drivers on a remote RPC server\n"));
6776 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6777 rpc_printer_driver_list_internals,
6782 * Publish printer in ADS via MSRPC.
6784 * @param c A net_context structure.
6785 * @param argc Standard main() style argc.
6786 * @param argv Standard main() style argv. Initial components are already
6789 * @return A shell status integer (0 for success).
6791 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6794 if (c->display_usage) {
6795 d_printf(_("Usage:\n"
6796 "net rpc printer publish publish\n"
6797 " Publish printer in ADS via MSRPC\n"));
6801 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6802 rpc_printer_publish_publish_internals,
6807 * Update printer in ADS via MSRPC.
6809 * @param c A net_context structure.
6810 * @param argc Standard main() style argc.
6811 * @param argv Standard main() style argv. Initial components are already
6814 * @return A shell status integer (0 for success).
6816 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6818 if (c->display_usage) {
6819 d_printf(_("Usage:\n"
6820 "net rpc printer publish update\n"
6821 " Update printer in ADS via MSRPC\n"));
6825 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6826 rpc_printer_publish_update_internals,
6831 * UnPublish printer in ADS via MSRPC.
6833 * @param c A net_context structure.
6834 * @param argc Standard main() style argc.
6835 * @param argv Standard main() style argv. Initial components are already
6838 * @return A shell status integer (0 for success).
6840 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6843 if (c->display_usage) {
6844 d_printf(_("Usage:\n"
6845 "net rpc printer publish unpublish\n"
6846 " UnPublish printer in ADS via MSRPC\n"));
6850 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6851 rpc_printer_publish_unpublish_internals,
6856 * List published printers via MSRPC.
6858 * @param c A net_context structure.
6859 * @param argc Standard main() style argc.
6860 * @param argv Standard main() style argv. Initial components are already
6863 * @return A shell status integer (0 for success).
6865 static int rpc_printer_publish_list(struct net_context *c, int argc,
6868 if (c->display_usage) {
6869 d_printf(_("Usage:\n"
6870 "net rpc printer publish list\n"
6871 " List published printers via MSRPC\n"));
6875 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6876 rpc_printer_publish_list_internals,
6882 * Publish printer in ADS.
6884 * @param c A net_context structure.
6885 * @param argc Standard main() style argc.
6886 * @param argv Standard main() style argv. Initial components are already
6889 * @return A shell status integer (0 for success).
6891 static int rpc_printer_publish(struct net_context *c, int argc,
6895 struct functable func[] = {
6898 rpc_printer_publish_publish,
6900 N_("Publish printer in AD"),
6901 N_("net rpc printer publish publish\n"
6902 " Publish printer in AD")
6906 rpc_printer_publish_update,
6908 N_("Update printer in AD"),
6909 N_("net rpc printer publish update\n"
6910 " Update printer in AD")
6914 rpc_printer_publish_unpublish,
6916 N_("Unpublish printer"),
6917 N_("net rpc printer publish unpublish\n"
6918 " Unpublish printer")
6922 rpc_printer_publish_list,
6924 N_("List published printers"),
6925 N_("net rpc printer publish list\n"
6926 " List published printers")
6928 {NULL, NULL, 0, NULL, NULL}
6932 if (c->display_usage) {
6933 d_printf(_("Usage:\n"));
6934 d_printf(_("net rpc printer publish\n"
6935 " List published printers\n"
6936 " Alias of net rpc printer publish "
6938 net_display_usage_from_functable(func);
6941 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6942 rpc_printer_publish_list_internals,
6946 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6952 * Display rpc printer help page.
6954 * @param c A net_context structure.
6955 * @param argc Standard main() style argc.
6956 * @param argv Standard main() style argv. Initial components are already
6959 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6961 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
6962 "\tlists all printers on print-server\n\n"));
6963 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6964 "\tlists all printer-drivers on print-server\n\n"));
6965 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6966 "\tpublishes printer settings in Active Directory\n"
6967 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
6968 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6969 "\n\tmigrates printers from remote to local server\n\n"));
6970 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6971 "\n\tmigrates printer-settings from remote to local server\n\n"));
6972 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6973 "\n\tmigrates printer-drivers from remote to local server\n\n"));
6974 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6975 "\n\tmigrates printer-forms from remote to local server\n\n"));
6976 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
6977 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
6978 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
6979 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
6980 "\tremote to local print-server\n\n"));
6981 net_common_methods_usage(c, argc, argv);
6982 net_common_flags_usage(c, argc, argv);
6984 "\t-v or --verbose\t\t\tgive verbose output\n"
6985 "\t --destination\t\tmigration target server (default: localhost)\n"));
6991 * 'net rpc printer' entrypoint.
6993 * @param c A net_context structure.
6994 * @param argc Standard main() style argc.
6995 * @param argv Standard main() style argv. Initial components are already
6998 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7000 struct functable func[] = {
7005 N_("List all printers on print server"),
7006 N_("net rpc printer list\n"
7007 " List all printers on print server")
7011 rpc_printer_migrate,
7013 N_("Migrate printer to local server"),
7014 N_("net rpc printer migrate\n"
7015 " Migrate printer to local server")
7019 rpc_printer_driver_list,
7021 N_("List printer drivers"),
7022 N_("net rpc printer driver\n"
7023 " List printer drivers")
7027 rpc_printer_publish,
7029 N_("Publish printer in AD"),
7030 N_("net rpc printer publish\n"
7031 " Publish printer in AD")
7033 {NULL, NULL, 0, NULL, NULL}
7037 if (c->display_usage) {
7038 d_printf(_("Usage:\n"));
7039 d_printf(_("net rpc printer\n"
7040 " List printers\n"));
7041 net_display_usage_from_functable(func);
7044 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7045 rpc_printer_list_internals,
7049 return net_run_function(c, argc, argv, "net rpc printer", func);
7053 * 'net rpc' entrypoint.
7055 * @param c A net_context structure.
7056 * @param argc Standard main() style argc.
7057 * @param argv Standard main() style argv. Initial components are already
7061 int net_rpc(struct net_context *c, int argc, const char **argv)
7063 NET_API_STATUS status;
7065 struct functable func[] = {
7070 N_("Modify global audit settings"),
7071 N_("net rpc audit\n"
7072 " Modify global audit settings")
7078 N_("Show basic info about a domain"),
7080 " Show basic info about a domain")
7086 N_("Join a domain"),
7094 N_("Join a domain created in server manager"),
7095 N_("net rpc oldjoin\n"
7096 " Join a domain created in server manager")
7102 N_("Test that a join is valid"),
7103 N_("net rpc testjoin\n"
7104 " Test that a join is valid")
7110 N_("List/modify users"),
7112 " List/modify users")
7118 N_("Change a user password"),
7119 N_("net rpc password\n"
7120 " Change a user password\n"
7121 " Alias for net rpc user password")
7127 N_("List/modify groups"),
7128 N_("net rpc group\n"
7129 " List/modify groups")
7135 N_("List/modify shares"),
7136 N_("net rpc share\n"
7137 " List/modify shares")
7143 N_("List open files"),
7151 N_("List/modify printers"),
7152 N_("net rpc printer\n"
7153 " List/modify printers")
7157 net_rpc_changetrustpw,
7159 N_("Change trust account password"),
7160 N_("net rpc changetrustpw\n"
7161 " Change trust account password")
7167 N_("Modify domain trusts"),
7168 N_("net rpc trustdom\n"
7169 " Modify domain trusts")
7175 N_("Abort a remote shutdown"),
7176 N_("net rpc abortshutdown\n"
7177 " Abort a remote shutdown")
7183 N_("Shutdown a remote server"),
7184 N_("net rpc shutdown\n"
7185 " Shutdown a remote server")
7191 N_("Dump SAM data of remote NT PDC"),
7192 N_("net rpc samdump\n"
7193 " Dump SAM data of remote NT PDC")
7199 N_("Sync a remote NT PDC's data into local passdb"),
7200 N_("net rpc vampire\n"
7201 " Sync a remote NT PDC's data into local passdb")
7207 N_("Fetch the domain sid into local secrets.tdb"),
7208 N_("net rpc getsid\n"
7209 " Fetch the domain sid into local secrets.tdb")
7215 N_("Manage privileges assigned to SID"),
7216 N_("net rpc rights\n"
7217 " Manage privileges assigned to SID")
7223 N_("Start/stop/query remote services"),
7224 N_("net rpc service\n"
7225 " Start/stop/query remote services")
7231 N_("Manage registry hives"),
7232 N_("net rpc registry\n"
7233 " Manage registry hives")
7239 N_("Open interactive shell on remote server"),
7240 N_("net rpc shell\n"
7241 " Open interactive shell on remote server")
7243 {NULL, NULL, 0, NULL, NULL}
7246 status = libnetapi_init(&c->netapi_ctx);
7250 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7251 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7252 if (c->opt_kerberos) {
7253 libnetapi_set_use_kerberos(c->netapi_ctx);
7256 return net_run_function(c, argc, argv, "net rpc", func);