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"
26 #include "../librpc/gen_ndr/cli_samr.h"
27 #include "../librpc/gen_ndr/cli_lsa.h"
28 #include "../librpc/gen_ndr/cli_netlogon.h"
29 #include "../librpc/gen_ndr/cli_srvsvc.h"
30 #include "../librpc/gen_ndr/cli_spoolss.h"
31 #include "../librpc/gen_ndr/cli_initshutdown.h"
32 #include "../librpc/gen_ndr/cli_winreg.h"
34 static int net_mode_share;
35 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
40 * @brief RPC based subcommands for the 'net' utility.
42 * This file should contain much of the functionality that used to
43 * be found in rpcclient, execpt that the commands should change
44 * less often, and the fucntionality should be sane (the user is not
45 * expected to know a rid/sid before they conduct an operation etc.)
47 * @todo Perhaps eventually these should be split out into a number
48 * of files, as this could get quite big.
53 * Many of the RPC functions need the domain sid. This function gets
54 * it at the start of every run
56 * @param cli A cli_state already connected to the remote machine
58 * @return The Domain SID of the remote machine.
61 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
63 const char **domain_name)
65 struct rpc_pipe_client *lsa_pipe = NULL;
66 struct policy_handle pol;
67 NTSTATUS result = NT_STATUS_OK;
68 union lsa_PolicyInformation *info = NULL;
70 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
72 if (!NT_STATUS_IS_OK(result)) {
73 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
77 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
78 SEC_FLAG_MAXIMUM_ALLOWED,
80 if (!NT_STATUS_IS_OK(result)) {
81 d_fprintf(stderr, _("open_policy failed: %s\n"),
86 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
88 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
90 if (!NT_STATUS_IS_OK(result)) {
91 d_fprintf(stderr, _("lsaquery failed: %s\n"),
96 *domain_name = info->account_domain.name.string;
97 *domain_sid = info->account_domain.sid;
99 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
100 TALLOC_FREE(lsa_pipe);
106 * Run a single RPC command, from start to finish.
108 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
109 * @param conn_flag a NET_FLAG_ combination. Passed to
110 * net_make_ipc_connection.
111 * @param argc Standard main() style argc.
112 * @param argv Standard main() style argv. Initial components are already
114 * @return A shell status integer (0 for success).
117 int run_rpc_command(struct net_context *c,
118 struct cli_state *cli_arg,
119 const struct ndr_syntax_id *interface,
125 struct cli_state *cli = NULL;
126 struct rpc_pipe_client *pipe_hnd = NULL;
130 const char *domain_name;
133 /* make use of cli_state handed over as an argument, if possible */
135 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
136 if (!NT_STATUS_IS_OK(nt_status)) {
137 DEBUG(1, ("failed to make ipc connection: %s\n",
138 nt_errstr(nt_status)));
151 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
152 DEBUG(0, ("talloc_init() failed\n"));
156 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
158 if (!NT_STATUS_IS_OK(nt_status)) {
162 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
163 if (lp_client_schannel()
164 && (ndr_syntax_id_equal(interface,
165 &ndr_table_netlogon.syntax_id))) {
166 /* Always try and create an schannel netlogon pipe. */
167 nt_status = cli_rpc_pipe_open_schannel(
168 cli, interface, NCACN_NP,
169 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
171 if (!NT_STATUS_IS_OK(nt_status)) {
172 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
173 nt_errstr(nt_status) ));
177 if (conn_flags & NET_FLAGS_SEAL) {
178 nt_status = cli_rpc_pipe_open_ntlmssp(
180 (conn_flags & NET_FLAGS_TCP) ?
181 NCACN_IP_TCP : NCACN_NP,
182 DCERPC_AUTH_LEVEL_PRIVACY,
183 lp_workgroup(), c->opt_user_name,
184 c->opt_password, &pipe_hnd);
186 nt_status = cli_rpc_pipe_open_noauth(
190 if (!NT_STATUS_IS_OK(nt_status)) {
191 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
192 get_pipe_name_from_syntax(
193 talloc_tos(), interface),
194 nt_errstr(nt_status) ));
200 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
202 if (!NT_STATUS_IS_OK(nt_status)) {
203 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
206 DEBUG(5, ("rpc command function succedded\n"));
209 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
211 TALLOC_FREE(pipe_hnd);
216 /* close the connection only if it was opened here */
221 talloc_destroy(mem_ctx);
226 * Force a change of the trust acccount password.
228 * All parameters are provided by the run_rpc_command function, except for
229 * argc, argv which are passed through.
231 * @param domain_sid The domain sid acquired from the remote server.
232 * @param cli A cli_state connected to the server.
233 * @param mem_ctx Talloc context, destroyed on completion of the function.
234 * @param argc Standard main() style argc.
235 * @param argv Standard main() style argv. Initial components are already
238 * @return Normal NTSTATUS return.
241 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
242 const DOM_SID *domain_sid,
243 const char *domain_name,
244 struct cli_state *cli,
245 struct rpc_pipe_client *pipe_hnd,
252 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
253 if (!NT_STATUS_IS_OK(status)) {
254 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
263 * Force a change of the trust acccount password.
265 * @param argc Standard main() style argc.
266 * @param argv Standard main() style argv. Initial components are already
269 * @return A shell status integer (0 for success).
272 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
274 if (c->display_usage) {
275 d_printf(_("Usage:\n"
276 "net rpc changetrustpw\n"
277 " Change the machine trust password\n"));
281 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
282 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
283 rpc_changetrustpw_internals,
288 * Join a domain, the old way.
290 * This uses 'machinename' as the inital password, and changes it.
292 * The password should be created with 'server manager' or equiv first.
294 * All parameters are provided by the run_rpc_command function, except for
295 * argc, argv which are passed through.
297 * @param domain_sid The domain sid acquired from the remote server.
298 * @param cli A cli_state connected to the server.
299 * @param mem_ctx Talloc context, destroyed on completion of the function.
300 * @param argc Standard main() style argc.
301 * @param argv Standard main() style argv. Initial components are already
304 * @return Normal NTSTATUS return.
307 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
308 const DOM_SID *domain_sid,
309 const char *domain_name,
310 struct cli_state *cli,
311 struct rpc_pipe_client *pipe_hnd,
317 fstring trust_passwd;
318 unsigned char orig_trust_passwd_hash[16];
320 enum netr_SchannelType sec_channel_type;
322 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
324 if (!NT_STATUS_IS_OK(result)) {
325 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
328 nt_errstr(result) ));
333 check what type of join - if the user want's to join as
334 a BDC, the server must agree that we are a BDC.
337 sec_channel_type = get_sec_channel_type(argv[0]);
339 sec_channel_type = get_sec_channel_type(NULL);
342 fstrcpy(trust_passwd, global_myname());
343 strlower_m(trust_passwd);
346 * Machine names can be 15 characters, but the max length on
347 * a password is 14. --jerry
350 trust_passwd[14] = '\0';
352 E_md4hash(trust_passwd, orig_trust_passwd_hash);
354 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
356 orig_trust_passwd_hash,
359 if (NT_STATUS_IS_OK(result))
360 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
363 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
364 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
365 result = NT_STATUS_UNSUCCESSFUL;
372 * Join a domain, the old way.
374 * @param argc Standard main() style argc.
375 * @param argv Standard main() style argv. Initial components are already
378 * @return A shell status integer (0 for success).
381 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
383 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
384 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
385 rpc_oldjoin_internals,
390 * Join a domain, the old way. This function exists to allow
391 * the message to be displayed when oldjoin was explicitly
392 * requested, but not when it was implied by "net rpc join".
394 * @param argc Standard main() style argc.
395 * @param argv Standard main() style argv. Initial components are already
398 * @return A shell status integer (0 for success).
401 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
405 if (c->display_usage) {
406 d_printf(_("Usage:\n"
408 " Join a domain the old way\n"));
412 rc = net_rpc_perform_oldjoin(c, argc, argv);
415 d_fprintf(stderr, _("Failed to join domain\n"));
422 * 'net rpc join' entrypoint.
423 * @param argc Standard main() style argc.
424 * @param argv Standard main() style argv. Initial components are already
427 * Main 'net_rpc_join()' (where the admin username/password is used) is
429 * Try to just change the password, but if that doesn't work, use/prompt
430 * for a username/password.
433 int net_rpc_join(struct net_context *c, int argc, const char **argv)
435 if (c->display_usage) {
436 d_printf(_("Usage:\n"
437 "net rpc join -U <username>[%%password] <type>\n"
439 " username\tName of the admin user"
440 " password\tPassword of the admin user, will "
441 "prompt if not specified\n"
442 " type\tCan be one of the following:\n"
443 "\t\tMEMBER\tJoin as member server (default)\n"
444 "\t\tBDC\tJoin as BDC\n"
445 "\t\tPDC\tJoin as PDC\n"));
449 if (lp_server_role() == ROLE_STANDALONE) {
450 d_printf(_("cannot join as standalone machine\n"));
454 if (strlen(global_myname()) > 15) {
455 d_printf(_("Our netbios name can be at most 15 chars long, "
456 "\"%s\" is %u chars long\n"),
457 global_myname(), (unsigned int)strlen(global_myname()));
461 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
464 return net_rpc_join_newstyle(c, argc, argv);
468 * display info about a rpc domain
470 * All parameters are provided by the run_rpc_command function, except for
471 * argc, argv which are passed through.
473 * @param domain_sid The domain sid acquired from the remote server
474 * @param cli A cli_state connected to the server.
475 * @param mem_ctx Talloc context, destroyed on completion of the function.
476 * @param argc Standard main() style argc.
477 * @param argv Standard main() style argv. Initial components are already
480 * @return Normal NTSTATUS return.
483 NTSTATUS rpc_info_internals(struct net_context *c,
484 const DOM_SID *domain_sid,
485 const char *domain_name,
486 struct cli_state *cli,
487 struct rpc_pipe_client *pipe_hnd,
492 struct policy_handle connect_pol, domain_pol;
493 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
494 union samr_DomainInfo *info = NULL;
497 sid_to_fstring(sid_str, domain_sid);
499 /* Get sam policy handle */
500 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
502 MAXIMUM_ALLOWED_ACCESS,
504 if (!NT_STATUS_IS_OK(result)) {
505 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
510 /* Get domain policy handle */
511 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
513 MAXIMUM_ALLOWED_ACCESS,
514 CONST_DISCARD(struct dom_sid2 *, domain_sid),
516 if (!NT_STATUS_IS_OK(result)) {
517 d_fprintf(stderr, _("Could not open domain: %s\n"),
522 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
526 if (NT_STATUS_IS_OK(result)) {
527 d_printf(_("Domain Name: %s\n"),
528 info->general.domain_name.string);
529 d_printf(_("Domain SID: %s\n"), sid_str);
530 d_printf(_("Sequence number: %llu\n"),
531 (unsigned long long)info->general.sequence_num);
532 d_printf(_("Num users: %u\n"), info->general.num_users);
533 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
534 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
542 * 'net rpc info' entrypoint.
543 * @param argc Standard main() style argc.
544 * @param argv Standard main() style argv. Initial components are already
548 int net_rpc_info(struct net_context *c, int argc, const char **argv)
550 if (c->display_usage) {
551 d_printf(_("Usage:\n"
553 " Display information about the domain\n"));
557 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
558 NET_FLAGS_PDC, rpc_info_internals,
563 * Fetch domain SID into the local secrets.tdb.
565 * All parameters are provided by the run_rpc_command function, except for
566 * argc, argv which are passed through.
568 * @param domain_sid The domain sid acquired from the remote server.
569 * @param cli A cli_state connected to the server.
570 * @param mem_ctx Talloc context, destroyed on completion of the function.
571 * @param argc Standard main() style argc.
572 * @param argv Standard main() style argv. Initial components are already
575 * @return Normal NTSTATUS return.
578 static NTSTATUS rpc_getsid_internals(struct net_context *c,
579 const DOM_SID *domain_sid,
580 const char *domain_name,
581 struct cli_state *cli,
582 struct rpc_pipe_client *pipe_hnd,
589 sid_to_fstring(sid_str, domain_sid);
590 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
591 sid_str, domain_name);
593 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
594 DEBUG(0,("Can't store domain SID\n"));
595 return NT_STATUS_UNSUCCESSFUL;
602 * 'net rpc getsid' entrypoint.
603 * @param argc Standard main() style argc.
604 * @param argv Standard main() style argv. Initial components are already
608 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
610 if (c->display_usage) {
611 d_printf(_("Usage:\n"
613 " Fetch domain SID into local secrets.tdb\n"));
617 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
618 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
619 rpc_getsid_internals,
623 /****************************************************************************/
626 * Basic usage function for 'net rpc user'.
627 * @param argc Standard main() style argc.
628 * @param argv Standard main() style argv. Initial components are already
632 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
634 return net_user_usage(c, argc, argv);
638 * Add a new user to a remote RPC server.
640 * @param argc Standard main() style argc.
641 * @param argv Standard main() style argv. Initial components are already
644 * @return A shell status integer (0 for success).
647 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
649 NET_API_STATUS status;
650 struct USER_INFO_1 info1;
651 uint32_t parm_error = 0;
653 if (argc < 1 || c->display_usage) {
654 rpc_user_usage(c, argc, argv);
660 info1.usri1_name = argv[0];
662 info1.usri1_password = argv[1];
665 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
668 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
669 argv[0], libnetapi_get_error_string(c->netapi_ctx,
673 d_printf(_("Added user '%s'.\n"), argv[0]);
680 * Rename a user on a remote RPC server.
682 * @param argc Standard main() style argc.
683 * @param argv Standard main() style argv. Initial components are already
686 * @return A shell status integer (0 for success).
689 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
691 NET_API_STATUS status;
692 struct USER_INFO_0 u0;
693 uint32_t parm_err = 0;
695 if (argc != 2 || c->display_usage) {
696 rpc_user_usage(c, argc, argv);
700 u0.usri0_name = argv[1];
702 status = NetUserSetInfo(c->opt_host, argv[0],
703 0, (uint8_t *)&u0, &parm_err);
706 _("Failed to rename user from %s to %s - %s\n"),
708 libnetapi_get_error_string(c->netapi_ctx, status));
710 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
717 * Delete a user from a remote RPC server.
719 * @param argc Standard main() style argc.
720 * @param argv Standard main() style argv. Initial components are already
723 * @return A shell status integer (0 for success).
726 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
728 NET_API_STATUS status;
730 if (argc < 1 || c->display_usage) {
731 rpc_user_usage(c, argc, argv);
735 status = NetUserDel(c->opt_host, argv[0]);
738 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
740 libnetapi_get_error_string(c->netapi_ctx, status));
743 d_printf(_("Deleted user '%s'.\n"), argv[0]);
750 * Set a user's password on a remote RPC server.
752 * @param argc Standard main() style argc.
753 * @param argv Standard main() style argv. Initial components are already
756 * @return A shell status integer (0 for success).
759 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
761 NET_API_STATUS status;
763 struct USER_INFO_1003 u1003;
764 uint32_t parm_err = 0;
767 if (argc < 1 || c->display_usage) {
768 rpc_user_usage(c, argc, argv);
773 u1003.usri1003_password = argv[1];
775 ret = asprintf(&prompt, _("Enter new password for %s:"),
780 u1003.usri1003_password = getpass(prompt);
784 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
786 /* Display results */
789 _("Failed to set password for '%s' with error: %s.\n"),
790 argv[0], libnetapi_get_error_string(c->netapi_ctx,
799 * List a user's groups from a remote RPC server.
801 * @param argc Standard main() style argc.
802 * @param argv Standard main() style argv. Initial components are already
805 * @return A shell status integer (0 for success)
808 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
811 NET_API_STATUS status;
812 struct GROUP_USERS_INFO_0 *u0 = NULL;
813 uint32_t entries_read = 0;
814 uint32_t total_entries = 0;
818 if (argc < 1 || c->display_usage) {
819 rpc_user_usage(c, argc, argv);
823 status = NetUserGetGroups(c->opt_host,
826 (uint8_t **)(void *)&u0,
832 _("Failed to get groups for '%s' with error: %s.\n"),
833 argv[0], libnetapi_get_error_string(c->netapi_ctx,
838 for (i=0; i < entries_read; i++) {
839 printf("%s\n", u0->grui0_name);
847 * List users on a remote RPC server.
849 * All parameters are provided by the run_rpc_command function, except for
850 * argc, argv which are passed through.
852 * @param domain_sid The domain sid acquired from the remote server.
853 * @param cli A cli_state connected to the server.
854 * @param mem_ctx Talloc context, destroyed on completion of the function.
855 * @param argc Standard main() style argc.
856 * @param argv Standard main() style argv. Initial components are already
859 * @return Normal NTSTATUS return.
862 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
864 NET_API_STATUS status;
865 uint32_t start_idx=0, num_entries, i, loop_count = 0;
866 struct NET_DISPLAY_USER *info = NULL;
869 /* Query domain users */
870 if (c->opt_long_list_entries)
871 d_printf(_("\nUser name Comment"
872 "\n-----------------------------\n"));
874 uint32_t max_entries, max_size;
876 get_query_dispinfo_params(
877 loop_count, &max_entries, &max_size);
879 status = NetQueryDisplayInformation(c->opt_host,
886 if (status != 0 && status != ERROR_MORE_DATA) {
890 info = (struct NET_DISPLAY_USER *)buffer;
892 for (i = 0; i < num_entries; i++) {
894 if (c->opt_long_list_entries)
895 printf("%-21.21s %s\n", info->usri1_name,
896 info->usri1_comment);
898 printf("%s\n", info->usri1_name);
902 NetApiBufferFree(buffer);
905 start_idx += num_entries;
907 } while (status == ERROR_MORE_DATA);
913 * 'net rpc user' entrypoint.
914 * @param argc Standard main() style argc.
915 * @param argv Standard main() style argv. Initial components are already
919 int net_rpc_user(struct net_context *c, int argc, const char **argv)
921 NET_API_STATUS status;
923 struct functable func[] = {
928 N_("Add specified user"),
929 N_("net rpc user add\n"
930 " Add specified user")
936 N_("List domain groups of user"),
937 N_("net rpc user info\n"
938 " Lis domain groups of user")
944 N_("Remove specified user"),
945 N_("net rpc user delete\n"
946 " Remove specified user")
952 N_("Change user password"),
953 N_("net rpc user password\n"
954 " Change user password")
960 N_("Rename specified user"),
961 N_("net rpc user rename\n"
962 " Rename specified user")
964 {NULL, NULL, 0, NULL, NULL}
967 status = libnetapi_init(&c->netapi_ctx);
971 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
972 libnetapi_set_password(c->netapi_ctx, c->opt_password);
973 if (c->opt_kerberos) {
974 libnetapi_set_use_kerberos(c->netapi_ctx);
978 if (c->display_usage) {
979 d_printf(_("Usage:\n"));
980 d_printf(_("net rpc user\n"
981 " List all users\n"));
982 net_display_usage_from_functable(func);
986 return rpc_user_list(c, argc, argv);
989 return net_run_function(c, argc, argv, "net rpc user", func);
992 static NTSTATUS rpc_sh_user_list(struct net_context *c,
994 struct rpc_sh_ctx *ctx,
995 struct rpc_pipe_client *pipe_hnd,
996 int argc, const char **argv)
998 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1001 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1002 TALLOC_CTX *mem_ctx,
1003 struct rpc_sh_ctx *ctx,
1004 struct rpc_pipe_client *pipe_hnd,
1005 int argc, const char **argv)
1007 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1010 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1011 TALLOC_CTX *mem_ctx,
1012 struct rpc_sh_ctx *ctx,
1013 struct rpc_pipe_client *pipe_hnd,
1014 int argc, const char **argv,
1016 struct net_context *c,
1017 TALLOC_CTX *mem_ctx,
1018 struct rpc_sh_ctx *ctx,
1019 struct rpc_pipe_client *pipe_hnd,
1020 struct policy_handle *user_hnd,
1021 int argc, const char **argv))
1023 struct policy_handle connect_pol, domain_pol, user_pol;
1024 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1027 enum lsa_SidType type;
1030 d_fprintf(stderr, _("usage: %s <username>\n"), ctx->whoami);
1031 return NT_STATUS_INVALID_PARAMETER;
1034 ZERO_STRUCT(connect_pol);
1035 ZERO_STRUCT(domain_pol);
1036 ZERO_STRUCT(user_pol);
1038 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1039 argv[0], NULL, NULL, &sid, &type);
1040 if (!NT_STATUS_IS_OK(result)) {
1041 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1046 if (type != SID_NAME_USER) {
1047 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1048 sid_type_lookup(type));
1049 result = NT_STATUS_NO_SUCH_USER;
1053 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1054 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1055 result = NT_STATUS_NO_SUCH_USER;
1059 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1061 MAXIMUM_ALLOWED_ACCESS,
1063 if (!NT_STATUS_IS_OK(result)) {
1067 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1069 MAXIMUM_ALLOWED_ACCESS,
1072 if (!NT_STATUS_IS_OK(result)) {
1076 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1078 MAXIMUM_ALLOWED_ACCESS,
1081 if (!NT_STATUS_IS_OK(result)) {
1085 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1088 if (is_valid_policy_hnd(&user_pol)) {
1089 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1091 if (is_valid_policy_hnd(&domain_pol)) {
1092 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1094 if (is_valid_policy_hnd(&connect_pol)) {
1095 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1100 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1101 TALLOC_CTX *mem_ctx,
1102 struct rpc_sh_ctx *ctx,
1103 struct rpc_pipe_client *pipe_hnd,
1104 struct policy_handle *user_hnd,
1105 int argc, const char **argv)
1108 union samr_UserInfo *info = NULL;
1111 d_fprintf(stderr, _("usage: %s show <username>\n"),ctx->whoami);
1112 return NT_STATUS_INVALID_PARAMETER;
1115 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1119 if (!NT_STATUS_IS_OK(result)) {
1123 d_printf(_("user rid: %d, group rid: %d\n"),
1125 info->info21.primary_gid);
1130 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1131 TALLOC_CTX *mem_ctx,
1132 struct rpc_sh_ctx *ctx,
1133 struct rpc_pipe_client *pipe_hnd,
1134 int argc, const char **argv)
1136 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1137 rpc_sh_user_show_internals);
1140 #define FETCHSTR(name, rec) \
1141 do { if (strequal(ctx->thiscmd, name)) { \
1142 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1145 #define SETSTR(name, rec, flag) \
1146 do { if (strequal(ctx->thiscmd, name)) { \
1147 init_lsa_String(&(info->info21.rec), argv[0]); \
1148 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1151 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1152 TALLOC_CTX *mem_ctx,
1153 struct rpc_sh_ctx *ctx,
1154 struct rpc_pipe_client *pipe_hnd,
1155 struct policy_handle *user_hnd,
1156 int argc, const char **argv)
1159 const char *username;
1160 const char *oldval = "";
1161 union samr_UserInfo *info = NULL;
1164 d_fprintf(stderr, _("usage: %s <username> [new value|NULL]\n"),
1166 return NT_STATUS_INVALID_PARAMETER;
1169 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1173 if (!NT_STATUS_IS_OK(result)) {
1177 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1179 FETCHSTR("fullname", full_name);
1180 FETCHSTR("homedir", home_directory);
1181 FETCHSTR("homedrive", home_drive);
1182 FETCHSTR("logonscript", logon_script);
1183 FETCHSTR("profilepath", profile_path);
1184 FETCHSTR("description", description);
1187 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1191 if (strcmp(argv[0], "NULL") == 0) {
1195 ZERO_STRUCT(info->info21);
1197 SETSTR("fullname", full_name, FULL_NAME);
1198 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1199 SETSTR("homedrive", home_drive, HOME_DRIVE);
1200 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1201 SETSTR("profilepath", profile_path, PROFILE_PATH);
1202 SETSTR("description", description, DESCRIPTION);
1204 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1209 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1210 ctx->thiscmd, oldval, argv[0]);
1217 #define HANDLEFLG(name, rec) \
1218 do { if (strequal(ctx->thiscmd, name)) { \
1219 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1221 newflags = oldflags | ACB_##rec; \
1223 newflags = oldflags & ~ACB_##rec; \
1226 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1227 TALLOC_CTX *mem_ctx,
1228 struct rpc_sh_ctx *ctx,
1229 struct rpc_pipe_client *pipe_hnd,
1230 int argc, const char **argv)
1232 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1233 rpc_sh_user_str_edit_internals);
1236 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1237 TALLOC_CTX *mem_ctx,
1238 struct rpc_sh_ctx *ctx,
1239 struct rpc_pipe_client *pipe_hnd,
1240 struct policy_handle *user_hnd,
1241 int argc, const char **argv)
1244 const char *username;
1245 const char *oldval = "unknown";
1246 uint32 oldflags, newflags;
1248 union samr_UserInfo *info = NULL;
1251 ((argc == 1) && !strequal(argv[0], "yes") &&
1252 !strequal(argv[0], "no"))) {
1253 /* TRANSATORS: The yes|no here are program keywords. Please do
1255 d_fprintf(stderr, _("usage: %s <username> [yes|no]\n"),
1257 return NT_STATUS_INVALID_PARAMETER;
1260 newval = strequal(argv[0], "yes");
1262 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1266 if (!NT_STATUS_IS_OK(result)) {
1270 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1271 oldflags = info->info21.acct_flags;
1272 newflags = info->info21.acct_flags;
1274 HANDLEFLG("disabled", DISABLED);
1275 HANDLEFLG("pwnotreq", PWNOTREQ);
1276 HANDLEFLG("autolock", AUTOLOCK);
1277 HANDLEFLG("pwnoexp", PWNOEXP);
1280 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1285 ZERO_STRUCT(info->info21);
1287 info->info21.acct_flags = newflags;
1288 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1290 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1295 if (NT_STATUS_IS_OK(result)) {
1296 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1297 ctx->thiscmd, oldval, argv[0]);
1305 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1306 TALLOC_CTX *mem_ctx,
1307 struct rpc_sh_ctx *ctx,
1308 struct rpc_pipe_client *pipe_hnd,
1309 int argc, const char **argv)
1311 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1312 rpc_sh_user_flag_edit_internals);
1315 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1316 TALLOC_CTX *mem_ctx,
1317 struct rpc_sh_ctx *ctx)
1319 static struct rpc_sh_cmd cmds[] = {
1321 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1322 N_("Show/Set a user's full name") },
1324 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1325 N_("Show/Set a user's home directory") },
1327 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1328 N_("Show/Set a user's home drive") },
1330 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1331 N_("Show/Set a user's logon script") },
1333 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1334 N_("Show/Set a user's profile path") },
1336 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1337 N_("Show/Set a user's description") },
1339 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1340 N_("Show/Set whether a user is disabled") },
1342 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1343 N_("Show/Set whether a user locked out") },
1345 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1346 N_("Show/Set whether a user does not need a password") },
1348 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1349 N_("Show/Set whether a user's password does not expire") },
1351 { NULL, NULL, 0, NULL, NULL }
1357 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1358 TALLOC_CTX *mem_ctx,
1359 struct rpc_sh_ctx *ctx)
1361 static struct rpc_sh_cmd cmds[] = {
1363 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1364 N_("List available users") },
1366 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1367 N_("List the domain groups a user is member of") },
1369 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1370 N_("Show info about a user") },
1372 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1373 N_("Show/Modify a user's fields") },
1375 { NULL, NULL, 0, NULL, NULL }
1381 /****************************************************************************/
1384 * Basic usage function for 'net rpc group'.
1385 * @param argc Standard main() style argc.
1386 * @param argv Standard main() style argv. Initial components are already
1390 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1392 return net_group_usage(c, argc, argv);
1396 * Delete group on a remote RPC server.
1398 * All parameters are provided by the run_rpc_command function, except for
1399 * argc, argv which are passed through.
1401 * @param domain_sid The domain sid acquired from the remote server.
1402 * @param cli A cli_state connected to the server.
1403 * @param mem_ctx Talloc context, destroyed on completion of the function.
1404 * @param argc Standard main() style argc.
1405 * @param argv Standard main() style argv. Initial components are already
1408 * @return Normal NTSTATUS return.
1411 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1412 const DOM_SID *domain_sid,
1413 const char *domain_name,
1414 struct cli_state *cli,
1415 struct rpc_pipe_client *pipe_hnd,
1416 TALLOC_CTX *mem_ctx,
1420 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1421 bool group_is_primary = false;
1422 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1424 struct samr_RidTypeArray *rids = NULL;
1427 /* struct samr_RidWithAttribute *user_gids; */
1429 struct samr_Ids group_rids, name_types;
1430 struct lsa_String lsa_acct_name;
1431 union samr_UserInfo *info = NULL;
1433 if (argc < 1 || c->display_usage) {
1434 rpc_group_usage(c, argc,argv);
1435 return NT_STATUS_OK; /* ok? */
1438 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1440 MAXIMUM_ALLOWED_ACCESS,
1443 if (!NT_STATUS_IS_OK(result)) {
1444 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1448 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1450 MAXIMUM_ALLOWED_ACCESS,
1451 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1454 if (!NT_STATUS_IS_OK(result)) {
1455 d_fprintf(stderr, _("Request open_domain failed\n"));
1459 init_lsa_String(&lsa_acct_name, argv[0]);
1461 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1467 if (!NT_STATUS_IS_OK(result)) {
1468 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1472 switch (name_types.ids[0])
1474 case SID_NAME_DOM_GRP:
1475 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1477 MAXIMUM_ALLOWED_ACCESS,
1480 if (!NT_STATUS_IS_OK(result)) {
1481 d_fprintf(stderr, _("Request open_group failed"));
1485 group_rid = group_rids.ids[0];
1487 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1491 if (!NT_STATUS_IS_OK(result)) {
1493 _("Unable to query group members of %s"),
1498 if (c->opt_verbose) {
1500 _("Domain Group %s (rid: %d) has %d members\n"),
1501 argv[0],group_rid, rids->count);
1504 /* Check if group is anyone's primary group */
1505 for (i = 0; i < rids->count; i++)
1507 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1509 MAXIMUM_ALLOWED_ACCESS,
1513 if (!NT_STATUS_IS_OK(result)) {
1515 _("Unable to open group member %d\n"),
1520 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1525 if (!NT_STATUS_IS_OK(result)) {
1527 _("Unable to lookup userinfo for group "
1533 if (info->info21.primary_gid == group_rid) {
1534 if (c->opt_verbose) {
1535 d_printf(_("Group is primary group "
1537 info->info21.account_name.string);
1539 group_is_primary = true;
1542 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1545 if (group_is_primary) {
1546 d_fprintf(stderr, _("Unable to delete group because "
1547 "some of it's members have it as primary "
1549 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1553 /* remove all group members */
1554 for (i = 0; i < rids->count; i++)
1557 d_printf(_("Remove group member %d..."),
1559 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1563 if (NT_STATUS_IS_OK(result)) {
1565 d_printf(_("ok\n"));
1568 d_printf(_("failed\n"));
1573 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1577 /* removing a local group is easier... */
1578 case SID_NAME_ALIAS:
1579 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1581 MAXIMUM_ALLOWED_ACCESS,
1585 if (!NT_STATUS_IS_OK(result)) {
1586 d_fprintf(stderr, _("Request open_alias failed\n"));
1590 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1594 d_fprintf(stderr, _("%s is of type %s. This command is only "
1595 "for deleting local or global groups\n"),
1596 argv[0],sid_type_lookup(name_types.ids[0]));
1597 result = NT_STATUS_UNSUCCESSFUL;
1601 if (NT_STATUS_IS_OK(result)) {
1603 d_printf(_("Deleted %s '%s'\n"),
1604 sid_type_lookup(name_types.ids[0]), argv[0]);
1606 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1607 get_friendly_nt_error_msg(result));
1615 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1617 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1618 rpc_group_delete_internals, argc,argv);
1621 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1623 NET_API_STATUS status;
1624 struct GROUP_INFO_1 info1;
1625 uint32_t parm_error = 0;
1627 if (argc != 1 || c->display_usage) {
1628 rpc_group_usage(c, argc, argv);
1634 info1.grpi1_name = argv[0];
1635 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1636 info1.grpi1_comment = c->opt_comment;
1639 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1643 _("Failed to add group '%s' with error: %s.\n"),
1644 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1648 d_printf(_("Added group '%s'.\n"), argv[0]);
1654 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1656 NET_API_STATUS status;
1657 struct LOCALGROUP_INFO_1 info1;
1658 uint32_t parm_error = 0;
1660 if (argc != 1 || c->display_usage) {
1661 rpc_group_usage(c, argc, argv);
1667 info1.lgrpi1_name = argv[0];
1668 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1669 info1.lgrpi1_comment = c->opt_comment;
1672 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1676 _("Failed to add alias '%s' with error: %s.\n"),
1677 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1681 d_printf(_("Added alias '%s'.\n"), argv[0]);
1687 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1689 if (c->opt_localgroup)
1690 return rpc_alias_add_internals(c, argc, argv);
1692 return rpc_group_add_internals(c, argc, argv);
1695 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1696 TALLOC_CTX *mem_ctx,
1699 enum lsa_SidType *type)
1701 DOM_SID *sids = NULL;
1702 enum lsa_SidType *types = NULL;
1703 struct rpc_pipe_client *pipe_hnd = NULL;
1704 struct policy_handle lsa_pol;
1705 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1707 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1709 if (!NT_STATUS_IS_OK(result)) {
1713 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1714 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1716 if (!NT_STATUS_IS_OK(result)) {
1720 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1721 &name, NULL, 1, &sids, &types);
1723 if (NT_STATUS_IS_OK(result)) {
1724 sid_copy(sid, &sids[0]);
1728 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1732 TALLOC_FREE(pipe_hnd);
1735 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1737 /* Try as S-1-5-whatever */
1741 if (string_to_sid(&tmp_sid, name)) {
1742 sid_copy(sid, &tmp_sid);
1743 *type = SID_NAME_UNKNOWN;
1744 result = NT_STATUS_OK;
1751 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1752 TALLOC_CTX *mem_ctx,
1753 const DOM_SID *group_sid,
1756 struct policy_handle connect_pol, domain_pol;
1759 struct policy_handle group_pol;
1761 struct samr_Ids rids, rid_types;
1762 struct lsa_String lsa_acct_name;
1766 sid_copy(&sid, group_sid);
1768 if (!sid_split_rid(&sid, &group_rid)) {
1769 return NT_STATUS_UNSUCCESSFUL;
1772 /* Get sam policy handle */
1773 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1775 MAXIMUM_ALLOWED_ACCESS,
1777 if (!NT_STATUS_IS_OK(result)) {
1781 /* Get domain policy handle */
1782 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1784 MAXIMUM_ALLOWED_ACCESS,
1787 if (!NT_STATUS_IS_OK(result)) {
1791 init_lsa_String(&lsa_acct_name, member);
1793 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1800 if (!NT_STATUS_IS_OK(result)) {
1801 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1806 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1808 MAXIMUM_ALLOWED_ACCESS,
1812 if (!NT_STATUS_IS_OK(result)) {
1816 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1819 0x0005); /* unknown flags */
1822 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1826 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1827 TALLOC_CTX *mem_ctx,
1828 const DOM_SID *alias_sid,
1831 struct policy_handle connect_pol, domain_pol;
1834 struct policy_handle alias_pol;
1837 enum lsa_SidType member_type;
1841 sid_copy(&sid, alias_sid);
1843 if (!sid_split_rid(&sid, &alias_rid)) {
1844 return NT_STATUS_UNSUCCESSFUL;
1847 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1848 member, &member_sid, &member_type);
1850 if (!NT_STATUS_IS_OK(result)) {
1851 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1856 /* Get sam policy handle */
1857 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1859 MAXIMUM_ALLOWED_ACCESS,
1861 if (!NT_STATUS_IS_OK(result)) {
1865 /* Get domain policy handle */
1866 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1868 MAXIMUM_ALLOWED_ACCESS,
1871 if (!NT_STATUS_IS_OK(result)) {
1875 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1877 MAXIMUM_ALLOWED_ACCESS,
1881 if (!NT_STATUS_IS_OK(result)) {
1885 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1889 if (!NT_STATUS_IS_OK(result)) {
1894 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1898 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1899 const DOM_SID *domain_sid,
1900 const char *domain_name,
1901 struct cli_state *cli,
1902 struct rpc_pipe_client *pipe_hnd,
1903 TALLOC_CTX *mem_ctx,
1908 enum lsa_SidType group_type;
1910 if (argc != 2 || c->display_usage) {
1911 d_printf(_("Usage:\n"
1912 "net rpc group addmem <group> <member>\n"
1913 " Add a member to a group\n"
1914 " group\tGroup to add member to\n"
1915 " member\tMember to add to group\n"));
1916 return NT_STATUS_UNSUCCESSFUL;
1919 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1920 &group_sid, &group_type))) {
1921 d_fprintf(stderr, _("Could not lookup group name %s\n"),
1923 return NT_STATUS_UNSUCCESSFUL;
1926 if (group_type == SID_NAME_DOM_GRP) {
1927 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1928 &group_sid, argv[1]);
1930 if (!NT_STATUS_IS_OK(result)) {
1931 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1932 argv[1], argv[0], nt_errstr(result));
1937 if (group_type == SID_NAME_ALIAS) {
1938 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1939 &group_sid, argv[1]);
1941 if (!NT_STATUS_IS_OK(result)) {
1942 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1943 argv[1], argv[0], nt_errstr(result));
1948 d_fprintf(stderr, _("Can only add members to global or local groups "
1949 "which %s is not\n"), argv[0]);
1951 return NT_STATUS_UNSUCCESSFUL;
1954 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1956 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1957 rpc_group_addmem_internals,
1961 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1962 struct rpc_pipe_client *pipe_hnd,
1963 TALLOC_CTX *mem_ctx,
1964 const DOM_SID *group_sid,
1967 struct policy_handle connect_pol, domain_pol;
1970 struct policy_handle group_pol;
1972 struct samr_Ids rids, rid_types;
1973 struct lsa_String lsa_acct_name;
1977 sid_copy(&sid, group_sid);
1979 if (!sid_split_rid(&sid, &group_rid))
1980 return NT_STATUS_UNSUCCESSFUL;
1982 /* Get sam policy handle */
1983 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1985 MAXIMUM_ALLOWED_ACCESS,
1987 if (!NT_STATUS_IS_OK(result))
1990 /* Get domain policy handle */
1991 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1993 MAXIMUM_ALLOWED_ACCESS,
1996 if (!NT_STATUS_IS_OK(result))
1999 init_lsa_String(&lsa_acct_name, member);
2001 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2007 if (!NT_STATUS_IS_OK(result)) {
2008 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2013 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2015 MAXIMUM_ALLOWED_ACCESS,
2019 if (!NT_STATUS_IS_OK(result))
2022 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2027 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2031 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2032 TALLOC_CTX *mem_ctx,
2033 const DOM_SID *alias_sid,
2036 struct policy_handle connect_pol, domain_pol;
2039 struct policy_handle alias_pol;
2042 enum lsa_SidType member_type;
2046 sid_copy(&sid, alias_sid);
2048 if (!sid_split_rid(&sid, &alias_rid))
2049 return NT_STATUS_UNSUCCESSFUL;
2051 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2052 member, &member_sid, &member_type);
2054 if (!NT_STATUS_IS_OK(result)) {
2055 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2060 /* Get sam policy handle */
2061 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2063 MAXIMUM_ALLOWED_ACCESS,
2065 if (!NT_STATUS_IS_OK(result)) {
2069 /* Get domain policy handle */
2070 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2072 MAXIMUM_ALLOWED_ACCESS,
2075 if (!NT_STATUS_IS_OK(result)) {
2079 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2081 MAXIMUM_ALLOWED_ACCESS,
2085 if (!NT_STATUS_IS_OK(result))
2088 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2092 if (!NT_STATUS_IS_OK(result))
2096 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2100 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2101 const DOM_SID *domain_sid,
2102 const char *domain_name,
2103 struct cli_state *cli,
2104 struct rpc_pipe_client *pipe_hnd,
2105 TALLOC_CTX *mem_ctx,
2110 enum lsa_SidType group_type;
2112 if (argc != 2 || c->display_usage) {
2113 d_printf(_("Usage:\n"
2114 "net rpc group delmem <group> <member>\n"
2115 " Delete a member from a group\n"
2116 " group\tGroup to delete member from\n"
2117 " member\tMember to delete from group\n"));
2118 return NT_STATUS_UNSUCCESSFUL;
2121 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2122 &group_sid, &group_type))) {
2123 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2125 return NT_STATUS_UNSUCCESSFUL;
2128 if (group_type == SID_NAME_DOM_GRP) {
2129 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2130 &group_sid, argv[1]);
2132 if (!NT_STATUS_IS_OK(result)) {
2133 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2134 argv[1], argv[0], nt_errstr(result));
2139 if (group_type == SID_NAME_ALIAS) {
2140 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2141 &group_sid, argv[1]);
2143 if (!NT_STATUS_IS_OK(result)) {
2144 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2145 argv[1], argv[0], nt_errstr(result));
2150 d_fprintf(stderr, _("Can only delete members from global or local "
2151 "groups which %s is not\n"), argv[0]);
2153 return NT_STATUS_UNSUCCESSFUL;
2156 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2158 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2159 rpc_group_delmem_internals,
2164 * List groups on a remote RPC server.
2166 * All parameters are provided by the run_rpc_command function, except for
2167 * argc, argv which are passes through.
2169 * @param domain_sid The domain sid acquired from the remote server.
2170 * @param cli A cli_state connected to the server.
2171 * @param mem_ctx Talloc context, destroyed on completion of the function.
2172 * @param argc Standard main() style argc.
2173 * @param argv Standard main() style argv. Initial components are already
2176 * @return Normal NTSTATUS return.
2179 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2180 const DOM_SID *domain_sid,
2181 const char *domain_name,
2182 struct cli_state *cli,
2183 struct rpc_pipe_client *pipe_hnd,
2184 TALLOC_CTX *mem_ctx,
2188 struct policy_handle connect_pol, domain_pol;
2189 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2190 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2191 struct samr_SamArray *groups = NULL;
2192 bool global = false;
2194 bool builtin = false;
2196 if (c->display_usage) {
2197 d_printf(_("Usage:\n"
2198 "net rpc group list [global] [local] [builtin]\n"
2199 " List groups on RPC server\n"
2200 " global\tList global groups\n"
2201 " local\tList local groups\n"
2202 " builtin\tList builtin groups\n"
2203 " If none of global, local or builtin is "
2204 "specified, all three options are considered "
2206 return NT_STATUS_OK;
2215 for (i=0; i<argc; i++) {
2216 if (strequal(argv[i], "global"))
2219 if (strequal(argv[i], "local"))
2222 if (strequal(argv[i], "builtin"))
2226 /* Get sam policy handle */
2228 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2230 MAXIMUM_ALLOWED_ACCESS,
2232 if (!NT_STATUS_IS_OK(result)) {
2236 /* Get domain policy handle */
2238 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2240 MAXIMUM_ALLOWED_ACCESS,
2241 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2243 if (!NT_STATUS_IS_OK(result)) {
2247 /* Query domain groups */
2248 if (c->opt_long_list_entries)
2249 d_printf(_("\nGroup name Comment"
2250 "\n-----------------------------\n"));
2252 uint32_t max_size, total_size, returned_size;
2253 union samr_DispInfo info;
2257 get_query_dispinfo_params(
2258 loop_count, &max_entries, &max_size);
2260 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2269 num_entries = info.info3.count;
2270 start_idx += info.info3.count;
2272 if (!NT_STATUS_IS_OK(result) &&
2273 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2276 for (i = 0; i < num_entries; i++) {
2278 const char *group = NULL;
2279 const char *desc = NULL;
2281 group = info.info3.entries[i].account_name.string;
2282 desc = info.info3.entries[i].description.string;
2284 if (c->opt_long_list_entries)
2285 printf("%-21.21s %-50.50s\n",
2288 printf("%s\n", group);
2290 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2291 /* query domain aliases */
2296 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2302 if (!NT_STATUS_IS_OK(result) &&
2303 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2306 for (i = 0; i < num_entries; i++) {
2308 const char *description = NULL;
2310 if (c->opt_long_list_entries) {
2312 struct policy_handle alias_pol;
2313 union samr_AliasInfo *info = NULL;
2315 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2318 groups->entries[i].idx,
2320 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2324 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2326 description = info->description.string;
2330 if (description != NULL) {
2331 printf("%-21.21s %-50.50s\n",
2332 groups->entries[i].name.string,
2335 printf("%s\n", groups->entries[i].name.string);
2338 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2339 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2340 /* Get builtin policy handle */
2342 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2344 MAXIMUM_ALLOWED_ACCESS,
2345 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2347 if (!NT_STATUS_IS_OK(result)) {
2350 /* query builtin aliases */
2353 if (!builtin) break;
2355 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2361 if (!NT_STATUS_IS_OK(result) &&
2362 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2365 for (i = 0; i < num_entries; i++) {
2367 const char *description = NULL;
2369 if (c->opt_long_list_entries) {
2371 struct policy_handle alias_pol;
2372 union samr_AliasInfo *info = NULL;
2374 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2377 groups->entries[i].idx,
2379 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2383 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2385 description = info->description.string;
2389 if (description != NULL) {
2390 printf("%-21.21s %-50.50s\n",
2391 groups->entries[i].name.string,
2394 printf("%s\n", groups->entries[i].name.string);
2397 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2403 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2405 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2406 rpc_group_list_internals,
2410 static NTSTATUS rpc_list_group_members(struct net_context *c,
2411 struct rpc_pipe_client *pipe_hnd,
2412 TALLOC_CTX *mem_ctx,
2413 const char *domain_name,
2414 const DOM_SID *domain_sid,
2415 struct policy_handle *domain_pol,
2419 struct policy_handle group_pol;
2420 uint32 num_members, *group_rids;
2422 struct samr_RidTypeArray *rids = NULL;
2423 struct lsa_Strings names;
2424 struct samr_Ids types;
2427 sid_to_fstring(sid_str, domain_sid);
2429 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2431 MAXIMUM_ALLOWED_ACCESS,
2435 if (!NT_STATUS_IS_OK(result))
2438 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2442 if (!NT_STATUS_IS_OK(result))
2445 num_members = rids->count;
2446 group_rids = rids->rids;
2448 while (num_members > 0) {
2449 int this_time = 512;
2451 if (num_members < this_time)
2452 this_time = num_members;
2454 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2461 if (!NT_STATUS_IS_OK(result))
2464 /* We only have users as members, but make the output
2465 the same as the output of alias members */
2467 for (i = 0; i < this_time; i++) {
2469 if (c->opt_long_list_entries) {
2470 printf("%s-%d %s\\%s %d\n", sid_str,
2471 group_rids[i], domain_name,
2472 names.names[i].string,
2475 printf("%s\\%s\n", domain_name,
2476 names.names[i].string);
2480 num_members -= this_time;
2484 return NT_STATUS_OK;
2487 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2488 struct rpc_pipe_client *pipe_hnd,
2489 TALLOC_CTX *mem_ctx,
2490 struct policy_handle *domain_pol,
2494 struct rpc_pipe_client *lsa_pipe;
2495 struct policy_handle alias_pol, lsa_pol;
2497 DOM_SID *alias_sids;
2500 enum lsa_SidType *types;
2502 struct lsa_SidArray sid_array;
2504 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2506 MAXIMUM_ALLOWED_ACCESS,
2510 if (!NT_STATUS_IS_OK(result))
2513 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2517 if (!NT_STATUS_IS_OK(result)) {
2518 d_fprintf(stderr, _("Couldn't list alias members\n"));
2522 num_members = sid_array.num_sids;
2524 if (num_members == 0) {
2525 return NT_STATUS_OK;
2528 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2529 &ndr_table_lsarpc.syntax_id,
2531 if (!NT_STATUS_IS_OK(result)) {
2532 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2533 nt_errstr(result) );
2537 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2538 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2540 if (!NT_STATUS_IS_OK(result)) {
2541 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2542 TALLOC_FREE(lsa_pipe);
2546 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2548 d_fprintf(stderr, _("Out of memory\n"));
2549 TALLOC_FREE(lsa_pipe);
2550 return NT_STATUS_NO_MEMORY;
2553 for (i=0; i<num_members; i++) {
2554 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2557 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2558 num_members, alias_sids,
2559 &domains, &names, &types);
2561 if (!NT_STATUS_IS_OK(result) &&
2562 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2563 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2564 TALLOC_FREE(lsa_pipe);
2568 for (i = 0; i < num_members; i++) {
2570 sid_to_fstring(sid_str, &alias_sids[i]);
2572 if (c->opt_long_list_entries) {
2573 printf("%s %s\\%s %d\n", sid_str,
2574 domains[i] ? domains[i] : _("*unknown*"),
2575 names[i] ? names[i] : _("*unknown*"), types[i]);
2578 printf("%s\\%s\n", domains[i], names[i]);
2580 printf("%s\n", sid_str);
2584 TALLOC_FREE(lsa_pipe);
2585 return NT_STATUS_OK;
2588 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2589 const DOM_SID *domain_sid,
2590 const char *domain_name,
2591 struct cli_state *cli,
2592 struct rpc_pipe_client *pipe_hnd,
2593 TALLOC_CTX *mem_ctx,
2598 struct policy_handle connect_pol, domain_pol;
2599 struct samr_Ids rids, rid_types;
2600 struct lsa_String lsa_acct_name;
2602 /* Get sam policy handle */
2604 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2606 MAXIMUM_ALLOWED_ACCESS,
2609 if (!NT_STATUS_IS_OK(result))
2612 /* Get domain policy handle */
2614 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2616 MAXIMUM_ALLOWED_ACCESS,
2617 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2620 if (!NT_STATUS_IS_OK(result))
2623 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2625 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2632 if (!NT_STATUS_IS_OK(result)) {
2634 /* Ok, did not find it in the global sam, try with builtin */
2636 DOM_SID sid_Builtin;
2638 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2640 sid_copy(&sid_Builtin, &global_sid_Builtin);
2642 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2644 MAXIMUM_ALLOWED_ACCESS,
2648 if (!NT_STATUS_IS_OK(result)) {
2649 d_fprintf(stderr, _("Couldn't find group %s\n"),
2654 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2661 if (!NT_STATUS_IS_OK(result)) {
2662 d_fprintf(stderr, _("Couldn't find group %s\n"),
2668 if (rids.count != 1) {
2669 d_fprintf(stderr, _("Couldn't find group %s\n"),
2674 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2675 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2676 domain_sid, &domain_pol,
2680 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2681 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2685 return NT_STATUS_NO_SUCH_GROUP;
2688 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2690 if (argc != 1 || c->display_usage) {
2691 return rpc_group_usage(c, argc, argv);
2694 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2695 rpc_group_members_internals,
2699 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2701 NET_API_STATUS status;
2702 struct GROUP_INFO_0 g0;
2706 d_printf(_("Usage: 'net rpc group rename group newname'\n"));
2710 g0.grpi0_name = argv[1];
2712 status = NetGroupSetInfo(c->opt_host,
2719 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2720 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2728 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2730 if (argc != 2 || c->display_usage) {
2731 return rpc_group_usage(c, argc, argv);
2734 return rpc_group_rename_internals(c, argc, argv);
2738 * 'net rpc group' entrypoint.
2739 * @param argc Standard main() style argc.
2740 * @param argv Standard main() style argv. Initial components are already
2744 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2746 NET_API_STATUS status;
2748 struct functable func[] = {
2753 N_("Create specified group"),
2754 N_("net rpc group add\n"
2755 " Create specified group")
2761 N_("Delete specified group"),
2762 N_("net rpc group delete\n"
2763 " Delete specified group")
2769 N_("Add member to group"),
2770 N_("net rpc group addmem\n"
2771 " Add member to group")
2777 N_("Remove member from group"),
2778 N_("net rpc group delmem\n"
2779 " Remove member from group")
2786 N_("net rpc group list\n"
2793 N_("List group members"),
2794 N_("net rpc group members\n"
2795 " List group members")
2802 N_("net rpc group rename\n"
2805 {NULL, NULL, 0, NULL, NULL}
2808 status = libnetapi_init(&c->netapi_ctx);
2812 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2813 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2814 if (c->opt_kerberos) {
2815 libnetapi_set_use_kerberos(c->netapi_ctx);
2819 if (c->display_usage) {
2820 d_printf(_("Usage:\n"));
2821 d_printf(_("net rpc group\n"
2822 " Alias for net rpc group list global "
2823 "local builtin\n"));
2824 net_display_usage_from_functable(func);
2828 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2829 rpc_group_list_internals,
2833 return net_run_function(c, argc, argv, "net rpc group", func);
2836 /****************************************************************************/
2838 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2840 return net_share_usage(c, argc, argv);
2844 * Add a share on a remote RPC server.
2846 * @param argc Standard main() style argc.
2847 * @param argv Standard main() style argv. Initial components are already
2850 * @return A shell status integer (0 for success).
2853 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2855 NET_API_STATUS status;
2858 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2859 uint32 num_users=0, perms=0;
2860 char *password=NULL; /* don't allow a share password */
2861 struct SHARE_INFO_2 i2;
2862 uint32_t parm_error = 0;
2864 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2865 return rpc_share_usage(c, argc, argv);
2868 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2872 path = strchr(sharename, '=');
2879 i2.shi2_netname = sharename;
2880 i2.shi2_type = type;
2881 i2.shi2_remark = c->opt_comment;
2882 i2.shi2_permissions = perms;
2883 i2.shi2_max_uses = c->opt_maxusers;
2884 i2.shi2_current_uses = num_users;
2885 i2.shi2_path = path;
2886 i2.shi2_passwd = password;
2888 status = NetShareAdd(c->opt_host,
2893 printf(_("NetShareAdd failed with: %s\n"),
2894 libnetapi_get_error_string(c->netapi_ctx, status));
2901 * Delete a share on a remote RPC server.
2903 * @param domain_sid The domain sid acquired from the remote server.
2904 * @param argc Standard main() style argc.
2905 * @param argv Standard main() style argv. Initial components are already
2908 * @return A shell status integer (0 for success).
2910 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2912 if (argc < 1 || c->display_usage) {
2913 return rpc_share_usage(c, argc, argv);
2916 return NetShareDel(c->opt_host, argv[0], 0);
2920 * Formatted print of share info
2922 * @param r pointer to SHARE_INFO_1 to format
2925 static void display_share_info_1(struct net_context *c,
2926 struct SHARE_INFO_1 *r)
2928 if (c->opt_long_list_entries) {
2929 d_printf("%-12s %-8.8s %-50s\n",
2931 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2934 d_printf("%s\n", r->shi1_netname);
2938 static WERROR get_share_info(struct net_context *c,
2939 struct rpc_pipe_client *pipe_hnd,
2940 TALLOC_CTX *mem_ctx,
2944 struct srvsvc_NetShareInfoCtr *info_ctr)
2948 union srvsvc_NetShareInfo info;
2950 /* no specific share requested, enumerate all */
2953 uint32_t preferred_len = 0xffffffff;
2954 uint32_t total_entries = 0;
2955 uint32_t resume_handle = 0;
2957 info_ctr->level = level;
2959 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2969 /* request just one share */
2970 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2977 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2982 ZERO_STRUCTP(info_ctr);
2984 info_ctr->level = level;
2989 struct srvsvc_NetShareCtr1 *ctr1;
2991 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2992 W_ERROR_HAVE_NO_MEMORY(ctr1);
2995 ctr1->array = info.info1;
2997 info_ctr->ctr.ctr1 = ctr1;
3001 struct srvsvc_NetShareCtr2 *ctr2;
3003 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3004 W_ERROR_HAVE_NO_MEMORY(ctr2);
3007 ctr2->array = info.info2;
3009 info_ctr->ctr.ctr2 = ctr2;
3013 struct srvsvc_NetShareCtr502 *ctr502;
3015 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3016 W_ERROR_HAVE_NO_MEMORY(ctr502);
3019 ctr502->array = info.info502;
3021 info_ctr->ctr.ctr502 = ctr502;
3029 * 'net rpc share list' entrypoint.
3030 * @param argc Standard main() style argc.
3031 * @param argv Standard main() style argv. Initial components are already
3034 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3036 NET_API_STATUS status;
3037 struct SHARE_INFO_1 *i1 = NULL;
3038 uint32_t entries_read = 0;
3039 uint32_t total_entries = 0;
3040 uint32_t resume_handle = 0;
3041 uint32_t i, level = 1;
3043 if (c->display_usage) {
3044 d_printf(_("Usage\n"
3045 "net rpc share list\n"
3046 " List shares on remote server\n"));
3050 status = NetShareEnum(c->opt_host,
3052 (uint8_t **)(void *)&i1,
3061 /* Display results */
3063 if (c->opt_long_list_entries) {
3065 "\nEnumerating shared resources (exports) on remote server:\n\n"
3066 "\nShare name Type Description\n"
3067 "---------- ---- -----------\n"));
3069 for (i = 0; i < entries_read; i++)
3070 display_share_info_1(c, &i1[i]);
3075 static bool check_share_availability(struct cli_state *cli, const char *netname)
3077 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
3078 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3088 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3089 const char *netname, uint32 type)
3091 /* only support disk shares */
3092 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3093 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3098 /* skip builtin shares */
3099 /* FIXME: should print$ be added too ? */
3100 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3101 strequal(netname,"global"))
3104 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3105 printf(_("excluding [%s]\n"), netname);
3109 return check_share_availability(cli, netname);
3113 * Migrate shares from a remote RPC server to the local RPC server.
3115 * All parameters are provided by the run_rpc_command function, except for
3116 * argc, argv which are passed through.
3118 * @param domain_sid The domain sid acquired from the remote server.
3119 * @param cli A cli_state connected to the server.
3120 * @param mem_ctx Talloc context, destroyed on completion of the function.
3121 * @param argc Standard main() style argc.
3122 * @param argv Standard main() style argv. Initial components are already
3125 * @return Normal NTSTATUS return.
3128 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3129 const DOM_SID *domain_sid,
3130 const char *domain_name,
3131 struct cli_state *cli,
3132 struct rpc_pipe_client *pipe_hnd,
3133 TALLOC_CTX *mem_ctx,
3138 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3139 struct srvsvc_NetShareInfoCtr ctr_src;
3141 struct rpc_pipe_client *srvsvc_pipe = NULL;
3142 struct cli_state *cli_dst = NULL;
3143 uint32 level = 502; /* includes secdesc */
3144 uint32_t parm_error = 0;
3146 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3148 if (!W_ERROR_IS_OK(result))
3151 /* connect destination PI_SRVSVC */
3152 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3153 &ndr_table_srvsvc.syntax_id);
3154 if (!NT_STATUS_IS_OK(nt_status))
3158 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3160 union srvsvc_NetShareInfo info;
3161 struct srvsvc_NetShareInfo502 info502 =
3162 ctr_src.ctr.ctr502->array[i];
3164 /* reset error-code */
3165 nt_status = NT_STATUS_UNSUCCESSFUL;
3167 if (!check_share_sanity(c, cli, info502.name, info502.type))
3170 /* finally add the share on the dst server */
3172 printf(_("migrating: [%s], path: %s, comment: %s, without "
3174 info502.name, info502.path, info502.comment);
3176 info.info502 = &info502;
3178 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3179 srvsvc_pipe->desthost,
3185 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3186 printf(_(" [%s] does already exist\n"),
3191 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3192 printf(_("cannot add share: %s\n"), win_errstr(result));
3198 nt_status = NT_STATUS_OK;
3202 cli_shutdown(cli_dst);
3210 * Migrate shares from a RPC server to another.
3212 * @param argc Standard main() style argc.
3213 * @param argv Standard main() style argv. Initial components are already
3216 * @return A shell status integer (0 for success).
3218 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3221 if (c->display_usage) {
3222 d_printf(_("Usage:\n"
3223 "net rpc share migrate shares\n"
3224 " Migrate shares to local server\n"));
3229 printf(_("no server to migrate\n"));
3233 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3234 rpc_share_migrate_shares_internals,
3241 * @param f file_info
3242 * @param mask current search mask
3243 * @param state arg-pointer
3246 static void copy_fn(const char *mnt, file_info *f,
3247 const char *mask, void *state)
3249 static NTSTATUS nt_status;
3250 static struct copy_clistate *local_state;
3251 static fstring filename, new_mask;
3254 struct net_context *c;
3256 local_state = (struct copy_clistate *)state;
3257 nt_status = NT_STATUS_UNSUCCESSFUL;
3261 if (strequal(f->name, ".") || strequal(f->name, ".."))
3264 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3267 if (f->mode & aDIR) {
3269 DEBUG(3,("got dir: %s\n", f->name));
3271 fstrcpy(dir, local_state->cwd);
3273 fstrcat(dir, f->name);
3275 switch (net_mode_share)
3277 case NET_MODE_SHARE_MIGRATE:
3278 /* create that directory */
3279 nt_status = net_copy_file(c, local_state->mem_ctx,
3280 local_state->cli_share_src,
3281 local_state->cli_share_dst,
3283 c->opt_acls? true : false,
3284 c->opt_attrs? true : false,
3285 c->opt_timestamps? true:false,
3289 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3293 if (!NT_STATUS_IS_OK(nt_status))
3294 printf(_("could not handle dir %s: %s\n"),
3295 dir, nt_errstr(nt_status));
3297 /* search below that directory */
3298 fstrcpy(new_mask, dir);
3299 fstrcat(new_mask, "\\*");
3301 old_dir = local_state->cwd;
3302 local_state->cwd = dir;
3303 if (!sync_files(local_state, new_mask))
3304 printf(_("could not handle files\n"));
3305 local_state->cwd = old_dir;
3312 fstrcpy(filename, local_state->cwd);
3313 fstrcat(filename, "\\");
3314 fstrcat(filename, f->name);
3316 DEBUG(3,("got file: %s\n", filename));
3318 switch (net_mode_share)
3320 case NET_MODE_SHARE_MIGRATE:
3321 nt_status = net_copy_file(c, local_state->mem_ctx,
3322 local_state->cli_share_src,
3323 local_state->cli_share_dst,
3325 c->opt_acls? true : false,
3326 c->opt_attrs? true : false,
3327 c->opt_timestamps? true: false,
3331 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3336 if (!NT_STATUS_IS_OK(nt_status))
3337 printf(_("could not handle file %s: %s\n"),
3338 filename, nt_errstr(nt_status));
3343 * sync files, can be called recursivly to list files
3344 * and then call copy_fn for each file
3346 * @param cp_clistate pointer to the copy_clistate we work with
3347 * @param mask the current search mask
3349 * @return Boolean result
3351 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3353 struct cli_state *targetcli;
3354 char *targetpath = NULL;
3356 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3358 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3359 mask, &targetcli, &targetpath ) ) {
3360 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3362 mask, cli_errstr(cp_clistate->cli_share_src));
3366 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3367 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3368 mask, cli_errstr(targetcli));
3377 * Set the top level directory permissions before we do any further copies.
3378 * Should set up ACL inheritance.
3381 bool copy_top_level_perms(struct net_context *c,
3382 struct copy_clistate *cp_clistate,
3383 const char *sharename)
3385 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3387 switch (net_mode_share) {
3388 case NET_MODE_SHARE_MIGRATE:
3389 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3390 nt_status = net_copy_fileattr(c,
3391 cp_clistate->mem_ctx,
3392 cp_clistate->cli_share_src,
3393 cp_clistate->cli_share_dst,
3395 c->opt_acls? true : false,
3396 c->opt_attrs? true : false,
3397 c->opt_timestamps? true: false,
3401 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3405 if (!NT_STATUS_IS_OK(nt_status)) {
3406 printf(_("Could handle directory attributes for top level "
3407 "directory of share %s. Error %s\n"),
3408 sharename, nt_errstr(nt_status));
3416 * Sync all files inside a remote share to another share (over smb).
3418 * All parameters are provided by the run_rpc_command function, except for
3419 * argc, argv which are passed through.
3421 * @param domain_sid The domain sid acquired from the remote server.
3422 * @param cli A cli_state connected to the server.
3423 * @param mem_ctx Talloc context, destroyed on completion of the function.
3424 * @param argc Standard main() style argc.
3425 * @param argv Standard main() style argv. Initial components are already
3428 * @return Normal NTSTATUS return.
3431 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3432 const DOM_SID *domain_sid,
3433 const char *domain_name,
3434 struct cli_state *cli,
3435 struct rpc_pipe_client *pipe_hnd,
3436 TALLOC_CTX *mem_ctx,
3441 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3442 struct srvsvc_NetShareInfoCtr ctr_src;
3445 struct copy_clistate cp_clistate;
3446 bool got_src_share = false;
3447 bool got_dst_share = false;
3448 const char *mask = "\\*";
3451 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3453 nt_status = NT_STATUS_NO_MEMORY;
3457 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3460 if (!W_ERROR_IS_OK(result))
3463 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3465 struct srvsvc_NetShareInfo502 info502 =
3466 ctr_src.ctr.ctr502->array[i];
3468 if (!check_share_sanity(c, cli, info502.name, info502.type))
3471 /* one might not want to mirror whole discs :) */
3472 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3473 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3478 switch (net_mode_share)
3480 case NET_MODE_SHARE_MIGRATE:
3484 d_fprintf(stderr, _("Unsupported mode %d\n"),
3488 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3491 c->opt_acls ? _("including") : _("without"),
3492 c->opt_attrs ? _("including") : _("without"),
3493 c->opt_timestamps ? _("(preserving timestamps)") : "");
3495 cp_clistate.mem_ctx = mem_ctx;
3496 cp_clistate.cli_share_src = NULL;
3497 cp_clistate.cli_share_dst = NULL;
3498 cp_clistate.cwd = NULL;
3499 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3502 /* open share source */
3503 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3504 &cli->dest_ss, cli->desthost,
3505 info502.name, "A:");
3506 if (!NT_STATUS_IS_OK(nt_status))
3509 got_src_share = true;
3511 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3512 /* open share destination */
3513 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3514 NULL, dst, info502.name, "A:");
3515 if (!NT_STATUS_IS_OK(nt_status))
3518 got_dst_share = true;
3521 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3522 d_fprintf(stderr, _("Could not handle the top level "
3523 "directory permissions for the "
3524 "share: %s\n"), info502.name);
3525 nt_status = NT_STATUS_UNSUCCESSFUL;
3529 if (!sync_files(&cp_clistate, mask)) {
3530 d_fprintf(stderr, _("could not handle files for share: "
3531 "%s\n"), info502.name);
3532 nt_status = NT_STATUS_UNSUCCESSFUL;
3537 nt_status = NT_STATUS_OK;
3542 cli_shutdown(cp_clistate.cli_share_src);
3545 cli_shutdown(cp_clistate.cli_share_dst);
3552 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3554 if (c->display_usage) {
3555 d_printf(_("Usage:\n"
3556 "net share migrate files\n"
3557 " Migrate files to local server\n"));
3562 d_printf(_("no server to migrate\n"));
3566 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3567 rpc_share_migrate_files_internals,
3572 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3574 * All parameters are provided by the run_rpc_command function, except for
3575 * argc, argv which are passed through.
3577 * @param domain_sid The domain sid acquired from the remote server.
3578 * @param cli A cli_state connected to the server.
3579 * @param mem_ctx Talloc context, destroyed on completion of the function.
3580 * @param argc Standard main() style argc.
3581 * @param argv Standard main() style argv. Initial components are already
3584 * @return Normal NTSTATUS return.
3587 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3588 const DOM_SID *domain_sid,
3589 const char *domain_name,
3590 struct cli_state *cli,
3591 struct rpc_pipe_client *pipe_hnd,
3592 TALLOC_CTX *mem_ctx,
3597 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3598 struct srvsvc_NetShareInfoCtr ctr_src;
3599 union srvsvc_NetShareInfo info;
3601 struct rpc_pipe_client *srvsvc_pipe = NULL;
3602 struct cli_state *cli_dst = NULL;
3603 uint32 level = 502; /* includes secdesc */
3604 uint32_t parm_error = 0;
3606 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3609 if (!W_ERROR_IS_OK(result))
3612 /* connect destination PI_SRVSVC */
3613 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3614 &ndr_table_srvsvc.syntax_id);
3615 if (!NT_STATUS_IS_OK(nt_status))
3619 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3621 struct srvsvc_NetShareInfo502 info502 =
3622 ctr_src.ctr.ctr502->array[i];
3624 /* reset error-code */
3625 nt_status = NT_STATUS_UNSUCCESSFUL;
3627 if (!check_share_sanity(c, cli, info502.name, info502.type))
3630 printf(_("migrating: [%s], path: %s, comment: %s, including "
3632 info502.name, info502.path, info502.comment);
3635 display_sec_desc(info502.sd_buf.sd);
3637 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3638 info.info502 = &info502;
3640 /* finally modify the share on the dst server */
3641 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3642 srvsvc_pipe->desthost,
3648 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3649 printf(_("cannot set share-acl: %s\n"),
3650 win_errstr(result));
3656 nt_status = NT_STATUS_OK;
3660 cli_shutdown(cli_dst);
3668 * Migrate share-acls from a RPC server to another.
3670 * @param argc Standard main() style argc.
3671 * @param argv Standard main() style argv. Initial components are already
3674 * @return A shell status integer (0 for success).
3676 static int rpc_share_migrate_security(struct net_context *c, int argc,
3679 if (c->display_usage) {
3680 d_printf(_("Usage:\n"
3681 "net rpc share migrate security\n"
3682 " Migrate share-acls to local server\n"));
3687 d_printf(_("no server to migrate\n"));
3691 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3692 rpc_share_migrate_security_internals,
3697 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3698 * from one server to another.
3700 * @param argc Standard main() style argc.
3701 * @param argv Standard main() style argv. Initial components are already
3704 * @return A shell status integer (0 for success).
3707 static int rpc_share_migrate_all(struct net_context *c, int argc,
3712 if (c->display_usage) {
3713 d_printf(_("Usage:\n"
3714 "net rpc share migrate all\n"
3715 " Migrates shares including all share settings\n"));
3720 d_printf(_("no server to migrate\n"));
3724 /* order is important. we don't want to be locked out by the share-acl
3725 * before copying files - gd */
3727 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3728 rpc_share_migrate_shares_internals, argc, argv);
3732 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3733 rpc_share_migrate_files_internals, argc, argv);
3737 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3738 rpc_share_migrate_security_internals, argc,
3744 * 'net rpc share migrate' entrypoint.
3745 * @param argc Standard main() style argc.
3746 * @param argv Standard main() style argv. Initial components are already
3749 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3752 struct functable func[] = {
3755 rpc_share_migrate_all,
3757 N_("Migrate shares from remote to local server"),
3758 N_("net rpc share migrate all\n"
3759 " Migrate shares from remote to local server")
3763 rpc_share_migrate_files,
3765 N_("Migrate files from remote to local server"),
3766 N_("net rpc share migrate files\n"
3767 " Migrate files from remote to local server")
3771 rpc_share_migrate_security,
3773 N_("Migrate share-ACLs from remote to local server"),
3774 N_("net rpc share migrate security\n"
3775 " Migrate share-ACLs from remote to local server")
3779 rpc_share_migrate_shares,
3781 N_("Migrate shares from remote to local server"),
3782 N_("net rpc share migrate shares\n"
3783 " Migrate shares from remote to local server")
3785 {NULL, NULL, 0, NULL, NULL}
3788 net_mode_share = NET_MODE_SHARE_MIGRATE;
3790 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3799 static int num_server_aliases;
3800 static struct full_alias *server_aliases;
3803 * Add an alias to the static list.
3805 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3807 if (server_aliases == NULL)
3808 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3810 server_aliases[num_server_aliases] = *alias;
3811 num_server_aliases += 1;
3815 * For a specific domain on the server, fetch all the aliases
3816 * and their members. Add all of them to the server_aliases.
3819 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3820 TALLOC_CTX *mem_ctx,
3821 struct policy_handle *connect_pol,
3822 const DOM_SID *domain_sid)
3824 uint32 start_idx, max_entries, num_entries, i;
3825 struct samr_SamArray *groups = NULL;
3827 struct policy_handle domain_pol;
3829 /* Get domain policy handle */
3831 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3833 MAXIMUM_ALLOWED_ACCESS,
3834 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3836 if (!NT_STATUS_IS_OK(result))
3843 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3849 for (i = 0; i < num_entries; i++) {
3851 struct policy_handle alias_pol;
3852 struct full_alias alias;
3853 struct lsa_SidArray sid_array;
3856 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3858 MAXIMUM_ALLOWED_ACCESS,
3859 groups->entries[i].idx,
3861 if (!NT_STATUS_IS_OK(result))
3864 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3867 if (!NT_STATUS_IS_OK(result))
3870 alias.num_members = sid_array.num_sids;
3872 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3873 if (!NT_STATUS_IS_OK(result))
3876 alias.members = NULL;
3878 if (alias.num_members > 0) {
3879 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3881 for (j = 0; j < alias.num_members; j++)
3882 sid_copy(&alias.members[j],
3883 sid_array.sids[j].sid);
3886 sid_copy(&alias.sid, domain_sid);
3887 sid_append_rid(&alias.sid, groups->entries[i].idx);
3889 push_alias(mem_ctx, &alias);
3891 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3893 result = NT_STATUS_OK;
3896 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3902 * Dump server_aliases as names for debugging purposes.
3905 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3906 const DOM_SID *domain_sid,
3907 const char *domain_name,
3908 struct cli_state *cli,
3909 struct rpc_pipe_client *pipe_hnd,
3910 TALLOC_CTX *mem_ctx,
3916 struct policy_handle lsa_pol;
3918 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3919 SEC_FLAG_MAXIMUM_ALLOWED,
3921 if (!NT_STATUS_IS_OK(result))
3924 for (i=0; i<num_server_aliases; i++) {
3927 enum lsa_SidType *types;
3930 struct full_alias *alias = &server_aliases[i];
3932 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3934 &domains, &names, &types);
3935 if (!NT_STATUS_IS_OK(result))
3938 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3940 if (alias->num_members == 0) {
3945 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3948 &domains, &names, &types);
3950 if (!NT_STATUS_IS_OK(result) &&
3951 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3954 for (j=0; j<alias->num_members; j++)
3955 DEBUG(1, ("%s\\%s (%d); ",
3956 domains[j] ? domains[j] : "*unknown*",
3957 names[j] ? names[j] : "*unknown*",types[j]));
3961 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3963 return NT_STATUS_OK;
3967 * Fetch a list of all server aliases and their members into
3971 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3972 const DOM_SID *domain_sid,
3973 const char *domain_name,
3974 struct cli_state *cli,
3975 struct rpc_pipe_client *pipe_hnd,
3976 TALLOC_CTX *mem_ctx,
3981 struct policy_handle connect_pol;
3983 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3985 MAXIMUM_ALLOWED_ACCESS,
3988 if (!NT_STATUS_IS_OK(result))
3991 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3992 &global_sid_Builtin);
3994 if (!NT_STATUS_IS_OK(result))
3997 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4000 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4005 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4007 token->num_sids = 4;
4009 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4010 d_fprintf(stderr, _("malloc failed\n"));
4011 token->num_sids = 0;
4015 token->user_sids[0] = *user_sid;
4016 sid_copy(&token->user_sids[1], &global_sid_World);
4017 sid_copy(&token->user_sids[2], &global_sid_Network);
4018 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4021 static void free_user_token(NT_USER_TOKEN *token)
4023 SAFE_FREE(token->user_sids);
4026 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4028 if (is_sid_in_token(token, sid))
4031 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4032 if (!token->user_sids) {
4036 sid_copy(&token->user_sids[token->num_sids], sid);
4038 token->num_sids += 1;
4043 NT_USER_TOKEN token;
4046 static void dump_user_token(struct user_token *token)
4050 d_printf("%s\n", token->name);
4052 for (i=0; i<token->token.num_sids; i++) {
4053 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4057 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4061 for (i=0; i<alias->num_members; i++) {
4062 if (sid_compare(sid, &alias->members[i]) == 0)
4069 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4073 for (i=0; i<num_server_aliases; i++) {
4074 if (is_alias_member(&sid, &server_aliases[i]))
4075 add_sid_to_token(token, &server_aliases[i].sid);
4080 * We got a user token with all the SIDs we can know about without asking the
4081 * server directly. These are the user and domain group sids. All of these can
4082 * be members of aliases. So scan the list of aliases for each of the SIDs and
4083 * add them to the token.
4086 static void collect_alias_memberships(NT_USER_TOKEN *token)
4088 int num_global_sids = token->num_sids;
4091 for (i=0; i<num_global_sids; i++) {
4092 collect_sid_memberships(token, token->user_sids[i]);
4096 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4098 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4099 enum wbcSidType type;
4101 struct wbcDomainSid wsid;
4102 char *sid_str = NULL;
4104 uint32_t num_groups;
4105 gid_t *groups = NULL;
4108 fstr_sprintf(full_name, "%s%c%s",
4109 domain, *lp_winbind_separator(), user);
4111 /* First let's find out the user sid */
4113 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4115 if (!WBC_ERROR_IS_OK(wbc_status)) {
4116 DEBUG(1, ("winbind could not find %s: %s\n",
4117 full_name, wbcErrorString(wbc_status)));
4121 wbc_status = wbcSidToString(&wsid, &sid_str);
4122 if (!WBC_ERROR_IS_OK(wbc_status)) {
4126 if (type != SID_NAME_USER) {
4127 wbcFreeMemory(sid_str);
4128 DEBUG(1, ("%s is not a user\n", full_name));
4132 if (!string_to_sid(&user_sid, sid_str)) {
4133 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4137 wbcFreeMemory(sid_str);
4140 init_user_token(token, &user_sid);
4142 /* And now the groups winbind knows about */
4144 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4145 if (!WBC_ERROR_IS_OK(wbc_status)) {
4146 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4147 full_name, wbcErrorString(wbc_status)));
4151 for (i = 0; i < num_groups; i++) {
4152 gid_t gid = groups[i];
4155 wbc_status = wbcGidToSid(gid, &wsid);
4156 if (!WBC_ERROR_IS_OK(wbc_status)) {
4157 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4158 (unsigned int)gid, wbcErrorString(wbc_status)));
4159 wbcFreeMemory(groups);
4163 wbc_status = wbcSidToString(&wsid, &sid_str);
4164 if (!WBC_ERROR_IS_OK(wbc_status)) {
4165 wbcFreeMemory(groups);
4169 DEBUG(3, (" %s\n", sid_str));
4171 string_to_sid(&sid, sid_str);
4172 wbcFreeMemory(sid_str);
4175 add_sid_to_token(token, &sid);
4177 wbcFreeMemory(groups);
4183 * Get a list of all user tokens we want to look at
4186 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4187 struct user_token **user_tokens)
4189 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4190 uint32_t i, num_users;
4192 struct user_token *result;
4193 TALLOC_CTX *frame = NULL;
4195 if (lp_winbind_use_default_domain() &&
4196 (c->opt_target_workgroup == NULL)) {
4197 d_fprintf(stderr, _("winbind use default domain = yes set, "
4198 "please specify a workgroup\n"));
4202 /* Send request to winbind daemon */
4204 wbc_status = wbcListUsers(NULL, &num_users, &users);
4205 if (!WBC_ERROR_IS_OK(wbc_status)) {
4206 DEBUG(1, (_("winbind could not list users: %s\n"),
4207 wbcErrorString(wbc_status)));
4211 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4213 if (result == NULL) {
4214 DEBUG(1, ("Could not malloc sid array\n"));
4215 wbcFreeMemory(users);
4219 frame = talloc_stackframe();
4220 for (i=0; i < num_users; i++) {
4221 fstring domain, user;
4224 fstrcpy(result[i].name, users[i]);
4226 p = strchr(users[i], *lp_winbind_separator());
4228 DEBUG(3, ("%s\n", users[i]));
4231 fstrcpy(domain, c->opt_target_workgroup);
4232 fstrcpy(user, users[i]);
4235 fstrcpy(domain, users[i]);
4240 get_user_sids(domain, user, &(result[i].token));
4244 wbcFreeMemory(users);
4246 *num_tokens = num_users;
4247 *user_tokens = result;
4252 static bool get_user_tokens_from_file(FILE *f,
4254 struct user_token **tokens)
4256 struct user_token *token = NULL;
4261 if (fgets(line, sizeof(line)-1, f) == NULL) {
4265 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4266 line[strlen(line)-1] = '\0';
4269 if (line[0] == ' ') {
4273 if(!string_to_sid(&sid, &line[1])) {
4274 DEBUG(1,("get_user_tokens_from_file: Could "
4275 "not convert sid %s \n",&line[1]));
4279 if (token == NULL) {
4280 DEBUG(0, ("File does not begin with username"));
4284 add_sid_to_token(&token->token, &sid);
4288 /* And a new user... */
4291 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4292 if (*tokens == NULL) {
4293 DEBUG(0, ("Could not realloc tokens\n"));
4297 token = &((*tokens)[*num_tokens-1]);
4299 fstrcpy(token->name, line);
4300 token->token.num_sids = 0;
4301 token->token.user_sids = NULL;
4310 * Show the list of all users that have access to a share
4313 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4314 TALLOC_CTX *mem_ctx,
4315 const char *netname,
4317 struct user_token *tokens)
4320 SEC_DESC *share_sd = NULL;
4321 SEC_DESC *root_sd = NULL;
4322 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4324 union srvsvc_NetShareInfo info;
4329 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4336 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4337 DEBUG(1, ("Coult not query secdesc for share %s\n",
4342 share_sd = info.info502->sd_buf.sd;
4343 if (share_sd == NULL) {
4344 DEBUG(1, ("Got no secdesc for share %s\n",
4350 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4354 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4355 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4356 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4359 for (i=0; i<num_tokens; i++) {
4362 if (share_sd != NULL) {
4363 status = se_access_check(share_sd, &tokens[i].token,
4366 if (!NT_STATUS_IS_OK(status)) {
4367 DEBUG(1, ("Could not check share_sd for "
4374 if (root_sd == NULL) {
4375 d_printf(" %s\n", tokens[i].name);
4379 status = se_access_check(root_sd, &tokens[i].token,
4381 if (!NT_STATUS_IS_OK(status)) {
4382 DEBUG(1, ("Could not check root_sd for user %s\n",
4386 d_printf(" %s\n", tokens[i].name);
4389 if (fnum != (uint16_t)-1)
4390 cli_close(cli, fnum);
4402 static void collect_share(const char *name, uint32 m,
4403 const char *comment, void *state)
4405 struct share_list *share_list = (struct share_list *)state;
4407 if (m != STYPE_DISKTREE)
4410 share_list->num_shares += 1;
4411 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4412 if (!share_list->shares) {
4413 share_list->num_shares = 0;
4416 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4420 * List shares on a remote RPC server, including the security descriptors.
4422 * All parameters are provided by the run_rpc_command function, except for
4423 * argc, argv which are passed through.
4425 * @param domain_sid The domain sid acquired from the remote server.
4426 * @param cli A cli_state connected to the server.
4427 * @param mem_ctx Talloc context, destroyed on completion of the function.
4428 * @param argc Standard main() style argc.
4429 * @param argv Standard main() style argv. Initial components are already
4432 * @return Normal NTSTATUS return.
4435 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4436 const DOM_SID *domain_sid,
4437 const char *domain_name,
4438 struct cli_state *cli,
4439 struct rpc_pipe_client *pipe_hnd,
4440 TALLOC_CTX *mem_ctx,
4449 struct user_token *tokens = NULL;
4452 struct share_list share_list;
4457 f = fopen(argv[0], "r");
4461 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4462 return NT_STATUS_UNSUCCESSFUL;
4465 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4471 DEBUG(0, ("Could not read users from file\n"));
4472 return NT_STATUS_UNSUCCESSFUL;
4475 for (i=0; i<num_tokens; i++)
4476 collect_alias_memberships(&tokens[i].token);
4478 share_list.num_shares = 0;
4479 share_list.shares = NULL;
4481 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4484 DEBUG(0, ("Error returning browse list: %s\n",
4489 for (i = 0; i < share_list.num_shares; i++) {
4490 char *netname = share_list.shares[i];
4492 if (netname[strlen(netname)-1] == '$')
4495 d_printf("%s\n", netname);
4497 show_userlist(pipe_hnd, mem_ctx, netname,
4498 num_tokens, tokens);
4501 for (i=0; i<num_tokens; i++) {
4502 free_user_token(&tokens[i].token);
4505 SAFE_FREE(share_list.shares);
4507 return NT_STATUS_OK;
4510 static int rpc_share_allowedusers(struct net_context *c, int argc,
4515 if (c->display_usage) {
4516 d_printf(_("Usage:\n"
4517 "net rpc share allowedusers\n"
4518 " List allowed users\n"));
4522 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4523 rpc_aliaslist_internals,
4528 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4534 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4535 rpc_share_allowedusers_internals,
4539 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4542 struct user_token *tokens = NULL;
4546 net_usersidlist_usage(c, argc, argv);
4550 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4551 DEBUG(0, ("Could not get the user/sid list\n"));
4555 for (i=0; i<num_tokens; i++) {
4556 dump_user_token(&tokens[i]);
4557 free_user_token(&tokens[i].token);
4564 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4566 d_printf(_("net usersidlist\n"
4567 "\tprints out a list of all users the running winbind knows\n"
4568 "\tabout, together with all their SIDs. This is used as\n"
4569 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4571 net_common_flags_usage(c, argc, argv);
4576 * 'net rpc share' entrypoint.
4577 * @param argc Standard main() style argc.
4578 * @param argv Standard main() style argv. Initial components are already
4582 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4584 NET_API_STATUS status;
4586 struct functable func[] = {
4592 N_("net rpc share add\n"
4600 N_("net rpc share delete\n"
4605 rpc_share_allowedusers,
4607 N_("Modify allowed users"),
4608 N_("net rpc share allowedusers\n"
4609 " Modify allowed users")
4615 N_("Migrate share to local server"),
4616 N_("net rpc share migrate\n"
4617 " Migrate share to local server")
4624 N_("net rpc share list\n"
4627 {NULL, NULL, 0, NULL, NULL}
4630 status = libnetapi_init(&c->netapi_ctx);
4634 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4635 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4636 if (c->opt_kerberos) {
4637 libnetapi_set_use_kerberos(c->netapi_ctx);
4641 if (c->display_usage) {
4642 d_printf(_("Usage:\n"
4645 " Alias for net rpc share list\n"));
4646 net_display_usage_from_functable(func);
4650 return rpc_share_list(c, argc, argv);
4653 return net_run_function(c, argc, argv, "net rpc share", func);
4656 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4657 TALLOC_CTX *mem_ctx,
4658 struct rpc_sh_ctx *ctx,
4659 struct rpc_pipe_client *pipe_hnd,
4660 int argc, const char **argv)
4663 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4666 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4667 TALLOC_CTX *mem_ctx,
4668 struct rpc_sh_ctx *ctx,
4669 struct rpc_pipe_client *pipe_hnd,
4670 int argc, const char **argv)
4672 NET_API_STATUS status;
4673 uint32_t parm_err = 0;
4674 struct SHARE_INFO_2 i2;
4676 if ((argc < 2) || (argc > 3)) {
4677 d_fprintf(stderr, _("usage: %s <share> <path> [comment]\n"),
4679 return NT_STATUS_INVALID_PARAMETER;
4682 i2.shi2_netname = argv[0];
4683 i2.shi2_type = STYPE_DISKTREE;
4684 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4685 i2.shi2_permissions = 0;
4686 i2.shi2_max_uses = 0;
4687 i2.shi2_current_uses = 0;
4688 i2.shi2_path = argv[1];
4689 i2.shi2_passwd = NULL;
4691 status = NetShareAdd(pipe_hnd->desthost,
4696 return werror_to_ntstatus(W_ERROR(status));
4699 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4700 TALLOC_CTX *mem_ctx,
4701 struct rpc_sh_ctx *ctx,
4702 struct rpc_pipe_client *pipe_hnd,
4703 int argc, const char **argv)
4706 d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
4707 return NT_STATUS_INVALID_PARAMETER;
4710 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4713 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4714 TALLOC_CTX *mem_ctx,
4715 struct rpc_sh_ctx *ctx,
4716 struct rpc_pipe_client *pipe_hnd,
4717 int argc, const char **argv)
4719 union srvsvc_NetShareInfo info;
4724 d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
4725 return NT_STATUS_INVALID_PARAMETER;
4728 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4734 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4738 d_printf(_("Name: %s\n"), info.info2->name);
4739 d_printf(_("Comment: %s\n"), info.info2->comment);
4740 d_printf(_("Path: %s\n"), info.info2->path);
4741 d_printf(_("Password: %s\n"), info.info2->password);
4744 return werror_to_ntstatus(result);
4747 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4748 struct rpc_sh_ctx *ctx)
4750 static struct rpc_sh_cmd cmds[] = {
4752 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4753 N_("List available shares") },
4755 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4756 N_("Add a share") },
4758 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4759 N_("Delete a share") },
4761 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4762 N_("Get information about a share") },
4764 { NULL, NULL, 0, NULL, NULL }
4770 /****************************************************************************/
4772 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4774 return net_file_usage(c, argc, argv);
4778 * Close a file on a remote RPC server.
4780 * @param argc Standard main() style argc.
4781 * @param argv Standard main() style argv. Initial components are already
4784 * @return A shell status integer (0 for success).
4786 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4788 if (argc < 1 || c->display_usage) {
4789 return rpc_file_usage(c, argc, argv);
4792 return NetFileClose(c->opt_host, atoi(argv[0]));
4796 * Formatted print of open file info
4798 * @param r struct FILE_INFO_3 contents
4801 static void display_file_info_3(struct FILE_INFO_3 *r)
4803 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4804 r->fi3_id, r->fi3_username, r->fi3_permissions,
4805 r->fi3_num_locks, r->fi3_pathname);
4809 * List files for a user on a remote RPC server.
4811 * @param argc Standard main() style argc.
4812 * @param argv Standard main() style argv. Initial components are already
4815 * @return A shell status integer (0 for success)..
4818 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4820 NET_API_STATUS status;
4821 uint32 preferred_len = 0xffffffff, i;
4822 const char *username=NULL;
4823 uint32_t total_entries = 0;
4824 uint32_t entries_read = 0;
4825 uint32_t resume_handle = 0;
4826 struct FILE_INFO_3 *i3 = NULL;
4828 if (c->display_usage) {
4829 return rpc_file_usage(c, argc, argv);
4832 /* if argc > 0, must be user command */
4834 username = smb_xstrdup(argv[0]);
4837 status = NetFileEnum(c->opt_host,
4841 (uint8_t **)(void *)&i3,
4851 /* Display results */
4854 "\nEnumerating open files on remote server:\n\n"
4855 "\nFileId Opened by Perms Locks Path"
4856 "\n------ --------- ----- ----- ---- \n"));
4857 for (i = 0; i < entries_read; i++) {
4858 display_file_info_3(&i3[i]);
4865 * 'net rpc file' entrypoint.
4866 * @param argc Standard main() style argc.
4867 * @param argv Standard main() style argv. Initial components are already
4871 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4873 NET_API_STATUS status;
4875 struct functable func[] = {
4880 N_("Close opened file"),
4881 N_("net rpc file close\n"
4882 " Close opened file")
4888 N_("List files opened by user"),
4889 N_("net rpc file user\n"
4890 " List files opened by user")
4897 N_("Display information about opened file"),
4898 N_("net rpc file info\n"
4899 " Display information about opened file")
4902 {NULL, NULL, 0, NULL, NULL}
4905 status = libnetapi_init(&c->netapi_ctx);
4909 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4910 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4911 if (c->opt_kerberos) {
4912 libnetapi_set_use_kerberos(c->netapi_ctx);
4916 if (c->display_usage) {
4917 d_printf(_("Usage:\n"));
4918 d_printf(_("net rpc file\n"
4919 " List opened files\n"));
4920 net_display_usage_from_functable(func);
4924 return rpc_file_user(c, argc, argv);
4927 return net_run_function(c, argc, argv, "net rpc file", func);
4931 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4933 * All parameters are provided by the run_rpc_command function, except for
4934 * argc, argv which are passed through.
4936 * @param c A net_context structure.
4937 * @param domain_sid The domain sid acquired from the remote server.
4938 * @param cli A cli_state connected to the server.
4939 * @param mem_ctx Talloc context, destroyed on completion of the function.
4940 * @param argc Standard main() style argc.
4941 * @param argv Standard main() style argv. Initial components are already
4944 * @return Normal NTSTATUS return.
4947 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4948 const DOM_SID *domain_sid,
4949 const char *domain_name,
4950 struct cli_state *cli,
4951 struct rpc_pipe_client *pipe_hnd,
4952 TALLOC_CTX *mem_ctx,
4956 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4958 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4960 if (NT_STATUS_IS_OK(result)) {
4961 d_printf(_("\nShutdown successfully aborted\n"));
4962 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4964 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4970 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4972 * All parameters are provided by the run_rpc_command function, except for
4973 * argc, argv which are passed through.
4975 * @param c A net_context structure.
4976 * @param domain_sid The domain sid acquired from the remote server.
4977 * @param cli A cli_state connected to the server.
4978 * @param mem_ctx Talloc context, destroyed on completion of the function.
4979 * @param argc Standard main() style argc.
4980 * @param argv Standard main() style argv. Initial components are already
4983 * @return Normal NTSTATUS return.
4986 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4987 const DOM_SID *domain_sid,
4988 const char *domain_name,
4989 struct cli_state *cli,
4990 struct rpc_pipe_client *pipe_hnd,
4991 TALLOC_CTX *mem_ctx,
4995 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4997 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
4999 if (NT_STATUS_IS_OK(result)) {
5000 d_printf(_("\nShutdown successfully aborted\n"));
5001 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5003 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5009 * ABORT the shutdown of a remote RPC server.
5011 * @param argc Standard main() style argc.
5012 * @param argv Standard main() style argv. Initial components are already
5015 * @return A shell status integer (0 for success).
5018 static int rpc_shutdown_abort(struct net_context *c, int argc,
5023 if (c->display_usage) {
5024 d_printf(_("Usage:\n"
5025 "net rpc abortshutdown\n"
5026 " Abort a scheduled shutdown\n"));
5030 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5031 rpc_shutdown_abort_internals, argc, argv);
5036 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5038 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5039 rpc_reg_shutdown_abort_internals,
5044 * Shut down a remote RPC Server via initshutdown pipe.
5046 * All parameters are provided by the run_rpc_command function, except for
5047 * argc, argv which are passed through.
5049 * @param c A net_context structure.
5050 * @param domain_sid The domain sid acquired from the remote server.
5051 * @param cli A cli_state connected to the server.
5052 * @param mem_ctx Talloc context, destroyed on completion of the function.
5053 * @param argc Standard main() style argc.
5054 * @param argv Standard main() style argv. Initial components are already
5057 * @return Normal NTSTATUS return.
5060 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5061 const DOM_SID *domain_sid,
5062 const char *domain_name,
5063 struct cli_state *cli,
5064 struct rpc_pipe_client *pipe_hnd,
5065 TALLOC_CTX *mem_ctx,
5069 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5070 const char *msg = N_("This machine will be shutdown shortly");
5071 uint32 timeout = 20;
5072 struct lsa_StringLarge msg_string;
5074 if (c->opt_comment) {
5075 msg = c->opt_comment;
5077 if (c->opt_timeout) {
5078 timeout = c->opt_timeout;
5081 msg_string.string = msg;
5083 /* create an entry */
5084 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5085 &msg_string, timeout, c->opt_force, c->opt_reboot,
5088 if (NT_STATUS_IS_OK(result)) {
5089 d_printf(_("\nShutdown of remote machine succeeded\n"));
5090 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5092 DEBUG(1,("Shutdown of remote machine failed!\n"));
5098 * Shut down a remote RPC Server via winreg pipe.
5100 * All parameters are provided by the run_rpc_command function, except for
5101 * argc, argv which are passed through.
5103 * @param c A net_context structure.
5104 * @param domain_sid The domain sid acquired from the remote server.
5105 * @param cli A cli_state connected to the server.
5106 * @param mem_ctx Talloc context, destroyed on completion of the function.
5107 * @param argc Standard main() style argc.
5108 * @param argv Standard main() style argv. Initial components are already
5111 * @return Normal NTSTATUS return.
5114 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5115 const DOM_SID *domain_sid,
5116 const char *domain_name,
5117 struct cli_state *cli,
5118 struct rpc_pipe_client *pipe_hnd,
5119 TALLOC_CTX *mem_ctx,
5123 const char *msg = N_("This machine will be shutdown shortly");
5124 uint32 timeout = 20;
5125 struct lsa_StringLarge msg_string;
5129 if (c->opt_comment) {
5130 msg = c->opt_comment;
5132 msg_string.string = msg;
5134 if (c->opt_timeout) {
5135 timeout = c->opt_timeout;
5138 /* create an entry */
5139 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5140 &msg_string, timeout, c->opt_force, c->opt_reboot,
5143 if (NT_STATUS_IS_OK(result)) {
5144 d_printf(_("\nShutdown of remote machine succeeded\n"));
5146 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5147 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5148 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5150 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5157 * Shut down a remote RPC server.
5159 * @param argc Standard main() style argc.
5160 * @param argv Standard main() style argv. Initial components are already
5163 * @return A shell status integer (0 for success).
5166 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5170 if (c->display_usage) {
5171 d_printf(_("Usage:\n"
5172 "net rpc shutdown\n"
5173 " Shut down a remote RPC server\n"));
5177 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5178 rpc_init_shutdown_internals, argc, argv);
5181 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5182 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5183 rpc_reg_shutdown_internals, argc, argv);
5189 /***************************************************************************
5190 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5191 ***************************************************************************/
5194 * Add interdomain trust account to the RPC server.
5195 * All parameters (except for argc and argv) are passed by run_rpc_command
5198 * @param c A net_context structure.
5199 * @param domain_sid The domain sid acquired from the server.
5200 * @param cli A cli_state connected to the server.
5201 * @param mem_ctx Talloc context, destroyed on completion of the function.
5202 * @param argc Standard main() style argc.
5203 * @param argv Standard main() style argv. Initial components are already
5206 * @return normal NTSTATUS return code.
5209 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5210 const DOM_SID *domain_sid,
5211 const char *domain_name,
5212 struct cli_state *cli,
5213 struct rpc_pipe_client *pipe_hnd,
5214 TALLOC_CTX *mem_ctx,
5218 struct policy_handle connect_pol, domain_pol, user_pol;
5219 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5221 struct lsa_String lsa_acct_name;
5223 uint32 acct_flags=0;
5225 uint32_t access_granted = 0;
5226 union samr_UserInfo info;
5227 unsigned int orig_timeout;
5230 d_printf(_("Usage: net rpc trustdom add <domain_name> "
5231 "<trust password>\n"));
5232 return NT_STATUS_INVALID_PARAMETER;
5236 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5239 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5240 return NT_STATUS_NO_MEMORY;
5243 strupper_m(acct_name);
5245 init_lsa_String(&lsa_acct_name, acct_name);
5247 /* Get samr policy handle */
5248 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5250 MAXIMUM_ALLOWED_ACCESS,
5252 if (!NT_STATUS_IS_OK(result)) {
5256 /* Get domain policy handle */
5257 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5259 MAXIMUM_ALLOWED_ACCESS,
5260 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5262 if (!NT_STATUS_IS_OK(result)) {
5266 /* This call can take a long time - allow the server to time out.
5267 * 35 seconds should do it. */
5269 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5271 /* Create trusting domain's account */
5272 acb_info = ACB_NORMAL;
5273 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5274 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5275 SAMR_USER_ACCESS_SET_PASSWORD |
5276 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5277 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5279 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5288 /* And restore our original timeout. */
5289 rpccli_set_timeout(pipe_hnd, orig_timeout);
5291 if (!NT_STATUS_IS_OK(result)) {
5292 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5293 acct_name, nt_errstr(result));
5298 struct samr_CryptPassword crypt_pwd;
5300 ZERO_STRUCT(info.info23);
5302 init_samr_CryptPassword(argv[1],
5303 &cli->user_session_key,
5306 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5307 SAMR_FIELD_NT_PASSWORD_PRESENT;
5308 info.info23.info.acct_flags = ACB_DOMTRUST;
5309 info.info23.password = crypt_pwd;
5311 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5316 if (!NT_STATUS_IS_OK(result)) {
5317 DEBUG(0,("Could not set trust account password: %s\n",
5318 nt_errstr(result)));
5324 SAFE_FREE(acct_name);
5329 * Create interdomain trust account for a remote domain.
5331 * @param argc Standard argc.
5332 * @param argv Standard argv without initial components.
5334 * @return Integer status (0 means success).
5337 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5339 if (argc > 0 && !c->display_usage) {
5340 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5341 rpc_trustdom_add_internals, argc, argv);
5343 d_printf(_("Usage:\n"
5344 "net rpc trustdom add <domain_name> <trust "
5352 * Remove interdomain trust account from the RPC server.
5353 * All parameters (except for argc and argv) are passed by run_rpc_command
5356 * @param c A net_context structure.
5357 * @param domain_sid The domain sid acquired from the server.
5358 * @param cli A cli_state connected to the server.
5359 * @param mem_ctx Talloc context, destroyed on completion of the function.
5360 * @param argc Standard main() style argc.
5361 * @param argv Standard main() style argv. Initial components are already
5364 * @return normal NTSTATUS return code.
5367 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5368 const DOM_SID *domain_sid,
5369 const char *domain_name,
5370 struct cli_state *cli,
5371 struct rpc_pipe_client *pipe_hnd,
5372 TALLOC_CTX *mem_ctx,
5376 struct policy_handle connect_pol, domain_pol, user_pol;
5377 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5379 DOM_SID trust_acct_sid;
5380 struct samr_Ids user_rids, name_types;
5381 struct lsa_String lsa_acct_name;
5384 d_printf(_("Usage: net rpc trustdom del <domain_name>\n"));
5385 return NT_STATUS_INVALID_PARAMETER;
5389 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5391 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5393 if (acct_name == NULL)
5394 return NT_STATUS_NO_MEMORY;
5396 strupper_m(acct_name);
5398 /* Get samr policy handle */
5399 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5401 MAXIMUM_ALLOWED_ACCESS,
5403 if (!NT_STATUS_IS_OK(result)) {
5407 /* Get domain policy handle */
5408 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5410 MAXIMUM_ALLOWED_ACCESS,
5411 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5413 if (!NT_STATUS_IS_OK(result)) {
5417 init_lsa_String(&lsa_acct_name, acct_name);
5419 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5426 if (!NT_STATUS_IS_OK(result)) {
5427 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5429 acct_name, nt_errstr(result) );
5433 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5435 MAXIMUM_ALLOWED_ACCESS,
5439 if (!NT_STATUS_IS_OK(result)) {
5440 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5442 acct_name, nt_errstr(result) );
5446 /* append the rid to the domain sid */
5447 sid_copy(&trust_acct_sid, domain_sid);
5448 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5452 /* remove the sid */
5454 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5457 if (!NT_STATUS_IS_OK(result)) {
5458 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5459 " on user %s failed %s\n"),
5460 acct_name, nt_errstr(result) );
5466 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5469 if (!NT_STATUS_IS_OK(result)) {
5470 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5472 acct_name, nt_errstr(result) );
5476 if (!NT_STATUS_IS_OK(result)) {
5477 d_printf(_("Could not set trust account password: %s\n"),
5487 * Delete interdomain trust account for a remote domain.
5489 * @param argc Standard argc.
5490 * @param argv Standard argv without initial components.
5492 * @return Integer status (0 means success).
5495 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5497 if (argc > 0 && !c->display_usage) {
5498 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5499 rpc_trustdom_del_internals, argc, argv);
5501 d_printf(_("Usage:\n"
5502 "net rpc trustdom del <domain>\n"));
5507 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5508 struct cli_state *cli,
5509 TALLOC_CTX *mem_ctx,
5510 const char *domain_name)
5512 char *dc_name = NULL;
5513 const char *buffer = NULL;
5514 struct rpc_pipe_client *netr;
5517 /* Use NetServerEnum2 */
5519 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5521 return NT_STATUS_OK;
5524 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5525 for domain %s\n", domain_name));
5527 /* Try netr_GetDcName */
5529 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5531 if (!NT_STATUS_IS_OK(status)) {
5535 status = rpccli_netr_GetDcName(netr, mem_ctx,
5542 if (NT_STATUS_IS_OK(status)) {
5546 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5547 for domain %s\n", domain_name));
5553 * Establish trust relationship to a trusting domain.
5554 * Interdomain account must already be created on remote PDC.
5556 * @param c A net_context structure.
5557 * @param argc Standard argc.
5558 * @param argv Standard argv without initial components.
5560 * @return Integer status (0 means success).
5563 static int rpc_trustdom_establish(struct net_context *c, int argc,
5566 struct cli_state *cli = NULL;
5567 struct sockaddr_storage server_ss;
5568 struct rpc_pipe_client *pipe_hnd = NULL;
5569 struct policy_handle connect_hnd;
5570 TALLOC_CTX *mem_ctx;
5572 DOM_SID *domain_sid;
5577 union lsa_PolicyInformation *info = NULL;
5580 * Connect to \\server\ipc$ as 'our domain' account with password
5583 if (argc != 1 || c->display_usage) {
5584 d_printf(_("Usage:\n"
5585 "net rpc trustdom establish <domain_name>\n"));
5589 domain_name = smb_xstrdup(argv[0]);
5590 strupper_m(domain_name);
5592 /* account name used at first is our domain's name with '$' */
5593 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5596 strupper_m(acct_name);
5599 * opt_workgroup will be used by connection functions further,
5600 * hence it should be set to remote domain name instead of ours
5602 if (c->opt_workgroup) {
5603 c->opt_workgroup = smb_xstrdup(domain_name);
5606 c->opt_user_name = acct_name;
5608 /* find the domain controller */
5609 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5610 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5614 /* connect to ipc$ as username/password */
5615 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5616 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5618 /* Is it trusting domain account for sure ? */
5619 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5620 nt_errstr(nt_status)));
5624 /* store who we connected to */
5626 saf_store( domain_name, pdc_name );
5629 * Connect to \\server\ipc$ again (this time anonymously)
5632 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5635 if (NT_STATUS_IS_ERR(nt_status)) {
5636 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5637 domain_name, nt_errstr(nt_status)));
5641 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5642 "domain %s", domain_name))) {
5643 DEBUG(0, ("talloc_init() failed\n"));
5648 /* Make sure we're talking to a proper server */
5650 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5651 if (!NT_STATUS_IS_OK(nt_status)) {
5653 talloc_destroy(mem_ctx);
5658 * Call LsaOpenPolicy and LsaQueryInfo
5661 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5663 if (!NT_STATUS_IS_OK(nt_status)) {
5664 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5666 talloc_destroy(mem_ctx);
5670 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5672 if (NT_STATUS_IS_ERR(nt_status)) {
5673 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5674 nt_errstr(nt_status)));
5676 talloc_destroy(mem_ctx);
5680 /* Querying info level 5 */
5682 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5684 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5686 if (NT_STATUS_IS_ERR(nt_status)) {
5687 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5688 nt_errstr(nt_status)));
5690 talloc_destroy(mem_ctx);
5694 domain_sid = info->account_domain.sid;
5696 /* There should be actually query info level 3 (following nt serv behaviour),
5697 but I still don't know if it's _really_ necessary */
5700 * Store the password in secrets db
5703 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5704 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5706 talloc_destroy(mem_ctx);
5711 * Close the pipes and clean up
5714 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5715 if (NT_STATUS_IS_ERR(nt_status)) {
5716 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5717 nt_errstr(nt_status)));
5719 talloc_destroy(mem_ctx);
5725 talloc_destroy(mem_ctx);
5727 d_printf(_("Trust to domain %s established\n"), domain_name);
5732 * Revoke trust relationship to the remote domain.
5734 * @param c A net_context structure.
5735 * @param argc Standard argc.
5736 * @param argv Standard argv without initial components.
5738 * @return Integer status (0 means success).
5741 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5747 if (argc < 1 || c->display_usage) {
5748 d_printf(_("Usage:\n"
5749 "net rpc trustdom revoke <domain_name>\n"
5750 " Revoke trust relationship\n"
5751 " domain_name\tName of domain to revoke trust\n"));
5755 /* generate upper cased domain name */
5756 domain_name = smb_xstrdup(argv[0]);
5757 strupper_m(domain_name);
5759 /* delete password of the trust */
5760 if (!pdb_del_trusteddom_pw(domain_name)) {
5761 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5768 SAFE_FREE(domain_name);
5772 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5773 const DOM_SID *domain_sid,
5774 const char *domain_name,
5775 struct cli_state *cli,
5776 struct rpc_pipe_client *pipe_hnd,
5777 TALLOC_CTX *mem_ctx,
5782 if (!sid_to_fstring(str_sid, domain_sid)) {
5783 return NT_STATUS_UNSUCCESSFUL;
5785 d_printf("%s\n", str_sid);
5786 return NT_STATUS_OK;
5789 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5793 /* convert sid into ascii string */
5794 sid_to_fstring(ascii_sid, dom_sid);
5796 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5799 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5800 TALLOC_CTX *mem_ctx,
5801 struct policy_handle *pol,
5803 const char *trusted_dom_name)
5806 union lsa_TrustedDomainInfo *info = NULL;
5807 char *cleartextpwd = NULL;
5808 uint8_t session_key[16];
5809 DATA_BLOB session_key_blob;
5810 DATA_BLOB data = data_blob_null;
5812 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5815 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5817 if (NT_STATUS_IS_ERR(nt_status)) {
5818 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5819 nt_errstr(nt_status)));
5823 data = data_blob(info->password.password->data,
5824 info->password.password->length);
5826 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5827 DEBUG(0, ("Could not retrieve password hash\n"));
5831 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5832 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5834 if (cleartextpwd == NULL) {
5835 DEBUG(0,("retrieved NULL password\n"));
5836 nt_status = NT_STATUS_UNSUCCESSFUL;
5840 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5841 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5842 nt_status = NT_STATUS_UNSUCCESSFUL;
5846 #ifdef DEBUG_PASSWORD
5847 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5848 "password: [%s]\n", trusted_dom_name,
5849 sid_string_dbg(&dom_sid), cleartextpwd));
5853 SAFE_FREE(cleartextpwd);
5854 data_blob_free(&data);
5859 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5862 /* common variables */
5863 TALLOC_CTX* mem_ctx;
5864 struct cli_state *cli = NULL;
5865 struct rpc_pipe_client *pipe_hnd = NULL;
5867 const char *domain_name = NULL;
5868 DOM_SID *queried_dom_sid;
5869 struct policy_handle connect_hnd;
5870 union lsa_PolicyInformation *info = NULL;
5872 /* trusted domains listing variables */
5873 unsigned int enum_ctx = 0;
5875 struct lsa_DomainList dom_list;
5878 if (c->display_usage) {
5879 d_printf(_("Usage:\n"
5880 "net rpc trustdom vampire\n"
5881 " Vampire trust relationship from remote server\n"));
5886 * Listing trusted domains (stored in secrets.tdb, if local)
5889 mem_ctx = talloc_init("trust relationships vampire");
5892 * set domain and pdc name to local samba server (default)
5893 * or to remote one given in command line
5896 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5897 domain_name = c->opt_workgroup;
5898 c->opt_target_workgroup = c->opt_workgroup;
5900 fstrcpy(pdc_name, global_myname());
5901 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5902 c->opt_target_workgroup = domain_name;
5905 /* open \PIPE\lsarpc and open policy handle */
5906 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5907 if (!NT_STATUS_IS_OK(nt_status)) {
5908 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5909 nt_errstr(nt_status)));
5910 talloc_destroy(mem_ctx);
5914 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5916 if (!NT_STATUS_IS_OK(nt_status)) {
5917 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5918 nt_errstr(nt_status) ));
5920 talloc_destroy(mem_ctx);
5924 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5926 if (NT_STATUS_IS_ERR(nt_status)) {
5927 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5928 nt_errstr(nt_status)));
5930 talloc_destroy(mem_ctx);
5934 /* query info level 5 to obtain sid of a domain being queried */
5935 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5937 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5940 if (NT_STATUS_IS_ERR(nt_status)) {
5941 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5942 nt_errstr(nt_status)));
5944 talloc_destroy(mem_ctx);
5948 queried_dom_sid = info->account_domain.sid;
5951 * Keep calling LsaEnumTrustdom over opened pipe until
5952 * the end of enumeration is reached
5955 d_printf(_("Vampire trusted domains:\n\n"));
5958 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5963 if (NT_STATUS_IS_ERR(nt_status)) {
5964 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5965 nt_errstr(nt_status)));
5967 talloc_destroy(mem_ctx);
5971 for (i = 0; i < dom_list.count; i++) {
5973 print_trusted_domain(dom_list.domains[i].sid,
5974 dom_list.domains[i].name.string);
5976 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5977 *dom_list.domains[i].sid,
5978 dom_list.domains[i].name.string);
5979 if (!NT_STATUS_IS_OK(nt_status)) {
5981 talloc_destroy(mem_ctx);
5987 * in case of no trusted domains say something rather
5988 * than just display blank line
5990 if (!dom_list.count) d_printf(_("none\n"));
5992 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5994 /* close this connection before doing next one */
5995 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5996 if (NT_STATUS_IS_ERR(nt_status)) {
5997 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5998 nt_errstr(nt_status)));
6000 talloc_destroy(mem_ctx);
6004 /* close lsarpc pipe and connection to IPC$ */
6007 talloc_destroy(mem_ctx);
6011 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6013 /* common variables */
6014 TALLOC_CTX* mem_ctx;
6015 struct cli_state *cli = NULL, *remote_cli = NULL;
6016 struct rpc_pipe_client *pipe_hnd = NULL;
6018 const char *domain_name = NULL;
6019 DOM_SID *queried_dom_sid;
6020 int ascii_dom_name_len;
6021 struct policy_handle connect_hnd;
6022 union lsa_PolicyInformation *info = NULL;
6024 /* trusted domains listing variables */
6025 unsigned int num_domains, enum_ctx = 0;
6027 struct lsa_DomainList dom_list;
6031 /* trusting domains listing variables */
6032 struct policy_handle domain_hnd;
6033 struct samr_SamArray *trusts = NULL;
6035 if (c->display_usage) {
6036 d_printf(_("Usage:\n"
6037 "net rpc trustdom list\n"
6038 " List in- and outgoing trust relationships\n"));
6043 * Listing trusted domains (stored in secrets.tdb, if local)
6046 mem_ctx = talloc_init("trust relationships listing");
6049 * set domain and pdc name to local samba server (default)
6050 * or to remote one given in command line
6053 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6054 domain_name = c->opt_workgroup;
6055 c->opt_target_workgroup = c->opt_workgroup;
6057 fstrcpy(pdc_name, global_myname());
6058 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6059 c->opt_target_workgroup = domain_name;
6062 /* open \PIPE\lsarpc and open policy handle */
6063 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6064 if (!NT_STATUS_IS_OK(nt_status)) {
6065 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6066 nt_errstr(nt_status)));
6067 talloc_destroy(mem_ctx);
6071 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6073 if (!NT_STATUS_IS_OK(nt_status)) {
6074 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6075 nt_errstr(nt_status) ));
6077 talloc_destroy(mem_ctx);
6081 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6083 if (NT_STATUS_IS_ERR(nt_status)) {
6084 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6085 nt_errstr(nt_status)));
6087 talloc_destroy(mem_ctx);
6091 /* query info level 5 to obtain sid of a domain being queried */
6092 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6094 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6097 if (NT_STATUS_IS_ERR(nt_status)) {
6098 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6099 nt_errstr(nt_status)));
6101 talloc_destroy(mem_ctx);
6105 queried_dom_sid = info->account_domain.sid;
6108 * Keep calling LsaEnumTrustdom over opened pipe until
6109 * the end of enumeration is reached
6112 d_printf(_("Trusted domains list:\n\n"));
6114 found_domain = false;
6117 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6122 if (NT_STATUS_IS_ERR(nt_status)) {
6123 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6124 nt_errstr(nt_status)));
6126 talloc_destroy(mem_ctx);
6130 for (i = 0; i < dom_list.count; i++) {
6131 print_trusted_domain(dom_list.domains[i].sid,
6132 dom_list.domains[i].name.string);
6133 found_domain = true;
6137 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6140 * in case of no trusted domains say something rather
6141 * than just display blank line
6143 if (!found_domain) {
6144 d_printf(_("none\n"));
6147 /* close this connection before doing next one */
6148 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6149 if (NT_STATUS_IS_ERR(nt_status)) {
6150 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6151 nt_errstr(nt_status)));
6153 talloc_destroy(mem_ctx);
6157 TALLOC_FREE(pipe_hnd);
6160 * Listing trusting domains (stored in passdb backend, if local)
6163 d_printf(_("\nTrusting domains list:\n\n"));
6166 * Open \PIPE\samr and get needed policy handles
6168 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6170 if (!NT_STATUS_IS_OK(nt_status)) {
6171 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6173 talloc_destroy(mem_ctx);
6178 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6180 SAMR_ACCESS_LOOKUP_DOMAIN,
6182 if (!NT_STATUS_IS_OK(nt_status)) {
6183 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6184 nt_errstr(nt_status)));
6186 talloc_destroy(mem_ctx);
6190 /* SamrOpenDomain - we have to open domain policy handle in order to be
6191 able to enumerate accounts*/
6192 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6194 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6197 if (!NT_STATUS_IS_OK(nt_status)) {
6198 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6199 nt_errstr(nt_status)));
6201 talloc_destroy(mem_ctx);
6206 * perform actual enumeration
6209 found_domain = false;
6211 enum_ctx = 0; /* reset enumeration context from last enumeration */
6214 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6221 if (NT_STATUS_IS_ERR(nt_status)) {
6222 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6223 nt_errstr(nt_status)));
6225 talloc_destroy(mem_ctx);
6229 for (i = 0; i < num_domains; i++) {
6231 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6233 found_domain = true;
6236 * get each single domain's sid (do we _really_ need this ?):
6237 * 1) connect to domain's pdc
6238 * 2) query the pdc for domain's sid
6241 /* get rid of '$' tail */
6242 ascii_dom_name_len = strlen(str);
6243 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6244 str[ascii_dom_name_len - 1] = '\0';
6246 /* set opt_* variables to remote domain */
6248 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6249 c->opt_target_workgroup = c->opt_workgroup;
6251 d_printf("%-20s", str);
6253 /* connect to remote domain controller */
6254 nt_status = net_make_ipc_connection(c,
6255 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6257 if (NT_STATUS_IS_OK(nt_status)) {
6258 /* query for domain's sid */
6259 if (run_rpc_command(
6261 &ndr_table_lsarpc.syntax_id, 0,
6262 rpc_query_domain_sid, argc,
6264 d_printf(_("strange - couldn't get domain's sid\n"));
6266 cli_shutdown(remote_cli);
6269 d_fprintf(stderr, _("domain controller is not "
6270 "responding: %s\n"),
6271 nt_errstr(nt_status));
6272 d_printf(_("couldn't get domain's sid\n"));
6276 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6278 if (!found_domain) {
6282 /* close opened samr and domain policy handles */
6283 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6284 if (!NT_STATUS_IS_OK(nt_status)) {
6285 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6288 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6289 if (!NT_STATUS_IS_OK(nt_status)) {
6290 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6293 /* close samr pipe and connection to IPC$ */
6296 talloc_destroy(mem_ctx);
6301 * Entrypoint for 'net rpc trustdom' code.
6303 * @param argc Standard argc.
6304 * @param argv Standard argv without initial components.
6306 * @return Integer status (0 means success).
6309 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6311 struct functable func[] = {
6316 N_("Add trusting domain's account"),
6317 N_("net rpc trustdom add\n"
6318 " Add trusting domain's account")
6324 N_("Remove trusting domain's account"),
6325 N_("net rpc trustdom del\n"
6326 " Remove trusting domain's account")
6330 rpc_trustdom_establish,
6332 N_("Establish outgoing trust relationship"),
6333 N_("net rpc trustdom establish\n"
6334 " Establish outgoing trust relationship")
6338 rpc_trustdom_revoke,
6340 N_("Revoke outgoing trust relationship"),
6341 N_("net rpc trustdom revoke\n"
6342 " Revoke outgoing trust relationship")
6348 N_("List in- and outgoing domain trusts"),
6349 N_("net rpc trustdom list\n"
6350 " List in- and outgoing domain trusts")
6354 rpc_trustdom_vampire,
6356 N_("Vampire trusts from remote server"),
6357 N_("net rpc trustdom vampire\n"
6358 " Vampire trusts from remote server")
6360 {NULL, NULL, 0, NULL, NULL}
6363 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6367 * Check if a server will take rpc commands
6368 * @param flags Type of server to connect to (PDC, DMB, localhost)
6369 * if the host is not explicitly specified
6370 * @return bool (true means rpc supported)
6372 bool net_rpc_check(struct net_context *c, unsigned flags)
6374 struct cli_state *cli;
6376 struct sockaddr_storage server_ss;
6377 char *server_name = NULL;
6380 /* flags (i.e. server type) may depend on command */
6381 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6384 if ((cli = cli_initialise()) == NULL) {
6388 status = cli_connect(cli, server_name, &server_ss);
6389 if (!NT_STATUS_IS_OK(status))
6391 if (!attempt_netbios_session_request(&cli, global_myname(),
6392 server_name, &server_ss))
6394 status = cli_negprot(cli);
6395 if (!NT_STATUS_IS_OK(status))
6397 if (cli->protocol < PROTOCOL_NT1)
6406 /* dump sam database via samsync rpc calls */
6407 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6408 if (c->display_usage) {
6409 d_printf(_("Usage:\n"
6411 " Dump remote SAM database\n"));
6415 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6416 NET_FLAGS_ANONYMOUS,
6417 rpc_samdump_internals, argc, argv);
6420 /* syncronise sam database via samsync rpc calls */
6421 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6423 struct functable func[] = {
6428 N_("Dump remote SAM database to ldif"),
6429 N_("net rpc vampire ldif\n"
6430 " Dump remote SAM database to LDIF file or "
6437 N_("Dump remote SAM database to Kerberos Keytab"),
6438 N_("net rpc vampire keytab\n"
6439 " Dump remote SAM database to Kerberos keytab "
6446 N_("Dump remote SAM database to passdb"),
6447 N_("net rpc vampire passdb\n"
6448 " Dump remote SAM database to passdb")
6451 {NULL, NULL, 0, NULL, NULL}
6455 if (c->display_usage) {
6456 d_printf(_("Usage:\n"
6458 " Vampire remote SAM database\n"));
6462 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6463 NET_FLAGS_ANONYMOUS,
6464 rpc_vampire_internals,
6468 return net_run_function(c, argc, argv, "net rpc vampire", func);
6472 * Migrate everything from a print server.
6474 * @param c A net_context structure.
6475 * @param argc Standard main() style argc.
6476 * @param argv Standard main() style argv. Initial components are already
6479 * @return A shell status integer (0 for success).
6481 * The order is important !
6482 * To successfully add drivers the print queues have to exist !
6483 * Applying ACLs should be the last step, because you're easily locked out.
6486 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6491 if (c->display_usage) {
6492 d_printf(_("Usage:\n"
6493 "net rpc printer migrate all\n"
6494 " Migrate everything from a print server\n"));
6499 d_printf(_("no server to migrate\n"));
6503 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6504 rpc_printer_migrate_printers_internals, argc,
6509 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6510 rpc_printer_migrate_drivers_internals, argc,
6515 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6516 rpc_printer_migrate_forms_internals, argc, argv);
6520 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6521 rpc_printer_migrate_settings_internals, argc,
6526 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6527 rpc_printer_migrate_security_internals, argc,
6533 * Migrate print drivers from a print server.
6535 * @param c A net_context structure.
6536 * @param argc Standard main() style argc.
6537 * @param argv Standard main() style argv. Initial components are already
6540 * @return A shell status integer (0 for success).
6542 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6545 if (c->display_usage) {
6546 d_printf(_("Usage:\n"
6547 "net rpc printer migrate drivers\n"
6548 " Migrate print-drivers from a print-server\n"));
6553 d_printf(_("no server to migrate\n"));
6557 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6558 rpc_printer_migrate_drivers_internals,
6563 * Migrate print-forms from a print-server.
6565 * @param c A net_context structure.
6566 * @param argc Standard main() style argc.
6567 * @param argv Standard main() style argv. Initial components are already
6570 * @return A shell status integer (0 for success).
6572 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6575 if (c->display_usage) {
6576 d_printf(_("Usage:\n"
6577 "net rpc printer migrate forms\n"
6578 " Migrate print-forms from a print-server\n"));
6583 d_printf(_("no server to migrate\n"));
6587 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6588 rpc_printer_migrate_forms_internals,
6593 * Migrate printers from a print-server.
6595 * @param c A net_context structure.
6596 * @param argc Standard main() style argc.
6597 * @param argv Standard main() style argv. Initial components are already
6600 * @return A shell status integer (0 for success).
6602 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6605 if (c->display_usage) {
6606 d_printf(_("Usage:\n"
6607 "net rpc printer migrate printers\n"
6608 " Migrate printers from a print-server\n"));
6613 d_printf(_("no server to migrate\n"));
6617 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6618 rpc_printer_migrate_printers_internals,
6623 * Migrate printer-ACLs from a print-server
6625 * @param c A net_context structure.
6626 * @param argc Standard main() style argc.
6627 * @param argv Standard main() style argv. Initial components are already
6630 * @return A shell status integer (0 for success).
6632 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6635 if (c->display_usage) {
6636 d_printf(_("Usage:\n"
6637 "net rpc printer migrate security\n"
6638 " Migrate printer-ACLs from a print-server\n"));
6643 d_printf(_("no server to migrate\n"));
6647 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6648 rpc_printer_migrate_security_internals,
6653 * Migrate printer-settings from a print-server.
6655 * @param c A net_context structure.
6656 * @param argc Standard main() style argc.
6657 * @param argv Standard main() style argv. Initial components are already
6660 * @return A shell status integer (0 for success).
6662 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6665 if (c->display_usage) {
6666 d_printf(_("Usage:\n"
6667 "net rpc printer migrate settings\n"
6668 " Migrate printer-settings from a "
6674 d_printf(_("no server to migrate\n"));
6678 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6679 rpc_printer_migrate_settings_internals,
6684 * 'net rpc printer' entrypoint.
6686 * @param c A net_context structure.
6687 * @param argc Standard main() style argc.
6688 * @param argv Standard main() style argv. Initial components are already
6692 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6695 /* ouch: when addriver and setdriver are called from within
6696 rpc_printer_migrate_drivers_internals, the printer-queue already
6699 struct functable func[] = {
6702 rpc_printer_migrate_all,
6704 N_("Migrate all from remote to local print server"),
6705 N_("net rpc printer migrate all\n"
6706 " Migrate all from remote to local print server")
6710 rpc_printer_migrate_drivers,
6712 N_("Migrate drivers to local server"),
6713 N_("net rpc printer migrate drivers\n"
6714 " Migrate drivers to local server")
6718 rpc_printer_migrate_forms,
6720 N_("Migrate froms to local server"),
6721 N_("net rpc printer migrate forms\n"
6722 " Migrate froms to local server")
6726 rpc_printer_migrate_printers,
6728 N_("Migrate printers to local server"),
6729 N_("net rpc printer migrate printers\n"
6730 " Migrate printers to local server")
6734 rpc_printer_migrate_security,
6736 N_("Mirgate printer ACLs to local server"),
6737 N_("net rpc printer migrate security\n"
6738 " Mirgate printer ACLs to local server")
6742 rpc_printer_migrate_settings,
6744 N_("Migrate printer settings to local server"),
6745 N_("net rpc printer migrate settings\n"
6746 " Migrate printer settings to local server")
6748 {NULL, NULL, 0, NULL, NULL}
6751 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6756 * List printers on a remote RPC server.
6758 * @param c A net_context structure.
6759 * @param argc Standard main() style argc.
6760 * @param argv Standard main() style argv. Initial components are already
6763 * @return A shell status integer (0 for success).
6765 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6767 if (c->display_usage) {
6768 d_printf(_("Usage:\n"
6769 "net rpc printer list\n"
6770 " List printers on a remote RPC server\n"));
6774 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6775 rpc_printer_list_internals,
6780 * List printer-drivers on a remote RPC server.
6782 * @param c A net_context structure.
6783 * @param argc Standard main() style argc.
6784 * @param argv Standard main() style argv. Initial components are already
6787 * @return A shell status integer (0 for success).
6789 static int rpc_printer_driver_list(struct net_context *c, int argc,
6792 if (c->display_usage) {
6793 d_printf(_("Usage:\n"
6794 "net rpc printer driver\n"
6795 " List printer-drivers on a remote RPC server\n"));
6799 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6800 rpc_printer_driver_list_internals,
6805 * Publish printer in ADS via MSRPC.
6807 * @param c A net_context structure.
6808 * @param argc Standard main() style argc.
6809 * @param argv Standard main() style argv. Initial components are already
6812 * @return A shell status integer (0 for success).
6814 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6817 if (c->display_usage) {
6818 d_printf(_("Usage:\n"
6819 "net rpc printer publish publish\n"
6820 " Publish printer in ADS via MSRPC\n"));
6824 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6825 rpc_printer_publish_publish_internals,
6830 * Update printer in ADS via MSRPC.
6832 * @param c A net_context structure.
6833 * @param argc Standard main() style argc.
6834 * @param argv Standard main() style argv. Initial components are already
6837 * @return A shell status integer (0 for success).
6839 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6841 if (c->display_usage) {
6842 d_printf(_("Usage:\n"
6843 "net rpc printer publish update\n"
6844 " Update printer in ADS via MSRPC\n"));
6848 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6849 rpc_printer_publish_update_internals,
6854 * UnPublish printer in ADS via MSRPC.
6856 * @param c A net_context structure.
6857 * @param argc Standard main() style argc.
6858 * @param argv Standard main() style argv. Initial components are already
6861 * @return A shell status integer (0 for success).
6863 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6866 if (c->display_usage) {
6867 d_printf(_("Usage:\n"
6868 "net rpc printer publish unpublish\n"
6869 " UnPublish printer in ADS via MSRPC\n"));
6873 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6874 rpc_printer_publish_unpublish_internals,
6879 * List published printers via MSRPC.
6881 * @param c A net_context structure.
6882 * @param argc Standard main() style argc.
6883 * @param argv Standard main() style argv. Initial components are already
6886 * @return A shell status integer (0 for success).
6888 static int rpc_printer_publish_list(struct net_context *c, int argc,
6891 if (c->display_usage) {
6892 d_printf(_("Usage:\n"
6893 "net rpc printer publish list\n"
6894 " List published printers via MSRPC\n"));
6898 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6899 rpc_printer_publish_list_internals,
6905 * Publish printer in ADS.
6907 * @param c A net_context structure.
6908 * @param argc Standard main() style argc.
6909 * @param argv Standard main() style argv. Initial components are already
6912 * @return A shell status integer (0 for success).
6914 static int rpc_printer_publish(struct net_context *c, int argc,
6918 struct functable func[] = {
6921 rpc_printer_publish_publish,
6923 N_("Publish printer in AD"),
6924 N_("net rpc printer publish publish\n"
6925 " Publish printer in AD")
6929 rpc_printer_publish_update,
6931 N_("Update printer in AD"),
6932 N_("net rpc printer publish update\n"
6933 " Update printer in AD")
6937 rpc_printer_publish_unpublish,
6939 N_("Unpublish printer"),
6940 N_("net rpc printer publish unpublish\n"
6941 " Unpublish printer")
6945 rpc_printer_publish_list,
6947 N_("List published printers"),
6948 N_("net rpc printer publish list\n"
6949 " List published printers")
6951 {NULL, NULL, 0, NULL, NULL}
6955 if (c->display_usage) {
6956 d_printf(_("Usage:\n"));
6957 d_printf(_("net rpc printer publish\n"
6958 " List published printers\n"
6959 " Alias of net rpc printer publish "
6961 net_display_usage_from_functable(func);
6964 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6965 rpc_printer_publish_list_internals,
6969 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6975 * Display rpc printer help page.
6977 * @param c A net_context structure.
6978 * @param argc Standard main() style argc.
6979 * @param argv Standard main() style argv. Initial components are already
6982 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6984 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
6985 "\tlists all printers on print-server\n\n"));
6986 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6987 "\tlists all printer-drivers on print-server\n\n"));
6988 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6989 "\tpublishes printer settings in Active Directory\n"
6990 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
6991 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6992 "\n\tmigrates printers from remote to local server\n\n"));
6993 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6994 "\n\tmigrates printer-settings from remote to local server\n\n"));
6995 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6996 "\n\tmigrates printer-drivers from remote to local server\n\n"));
6997 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6998 "\n\tmigrates printer-forms from remote to local server\n\n"));
6999 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7000 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7001 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7002 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7003 "\tremote to local print-server\n\n"));
7004 net_common_methods_usage(c, argc, argv);
7005 net_common_flags_usage(c, argc, argv);
7007 "\t-v or --verbose\t\t\tgive verbose output\n"
7008 "\t --destination\t\tmigration target server (default: localhost)\n"));
7014 * 'net rpc printer' entrypoint.
7016 * @param c A net_context structure.
7017 * @param argc Standard main() style argc.
7018 * @param argv Standard main() style argv. Initial components are already
7021 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7023 struct functable func[] = {
7028 N_("List all printers on print server"),
7029 N_("net rpc printer list\n"
7030 " List all printers on print server")
7034 rpc_printer_migrate,
7036 N_("Migrate printer to local server"),
7037 N_("net rpc printer migrate\n"
7038 " Migrate printer to local server")
7042 rpc_printer_driver_list,
7044 N_("List printer drivers"),
7045 N_("net rpc printer driver\n"
7046 " List printer drivers")
7050 rpc_printer_publish,
7052 N_("Publish printer in AD"),
7053 N_("net rpc printer publish\n"
7054 " Publish printer in AD")
7056 {NULL, NULL, 0, NULL, NULL}
7060 if (c->display_usage) {
7061 d_printf(_("Usage:\n"));
7062 d_printf(_("net rpc printer\n"
7063 " List printers\n"));
7064 net_display_usage_from_functable(func);
7067 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7068 rpc_printer_list_internals,
7072 return net_run_function(c, argc, argv, "net rpc printer", func);
7076 * 'net rpc' entrypoint.
7078 * @param c A net_context structure.
7079 * @param argc Standard main() style argc.
7080 * @param argv Standard main() style argv. Initial components are already
7084 int net_rpc(struct net_context *c, int argc, const char **argv)
7086 NET_API_STATUS status;
7088 struct functable func[] = {
7093 N_("Modify global audit settings"),
7094 N_("net rpc audit\n"
7095 " Modify global audit settings")
7101 N_("Show basic info about a domain"),
7103 " Show basic info about a domain")
7109 N_("Join a domain"),
7117 N_("Join a domain created in server manager"),
7118 N_("net rpc oldjoin\n"
7119 " Join a domain created in server manager")
7125 N_("Test that a join is valid"),
7126 N_("net rpc testjoin\n"
7127 " Test that a join is valid")
7133 N_("List/modify users"),
7135 " List/modify users")
7141 N_("Change a user password"),
7142 N_("net rpc password\n"
7143 " Change a user password\n"
7144 " Alias for net rpc user password")
7150 N_("List/modify groups"),
7151 N_("net rpc group\n"
7152 " List/modify groups")
7158 N_("List/modify shares"),
7159 N_("net rpc share\n"
7160 " List/modify shares")
7166 N_("List open files"),
7174 N_("List/modify printers"),
7175 N_("net rpc printer\n"
7176 " List/modify printers")
7180 net_rpc_changetrustpw,
7182 N_("Change trust account password"),
7183 N_("net rpc changetrustpw\n"
7184 " Change trust account password")
7190 N_("Modify domain trusts"),
7191 N_("net rpc trustdom\n"
7192 " Modify domain trusts")
7198 N_("Abort a remote shutdown"),
7199 N_("net rpc abortshutdown\n"
7200 " Abort a remote shutdown")
7206 N_("Shutdown a remote server"),
7207 N_("net rpc shutdown\n"
7208 " Shutdown a remote server")
7214 N_("Dump SAM data of remote NT PDC"),
7215 N_("net rpc samdump\n"
7216 " Dump SAM data of remote NT PDC")
7222 N_("Sync a remote NT PDC's data into local passdb"),
7223 N_("net rpc vampire\n"
7224 " Sync a remote NT PDC's data into local passdb")
7230 N_("Fetch the domain sid into local secrets.tdb"),
7231 N_("net rpc getsid\n"
7232 " Fetch the domain sid into local secrets.tdb")
7238 N_("Manage privileges assigned to SID"),
7239 N_("net rpc rights\n"
7240 " Manage privileges assigned to SID")
7246 N_("Start/stop/query remote services"),
7247 N_("net rpc service\n"
7248 " Start/stop/query remote services")
7254 N_("Manage registry hives"),
7255 N_("net rpc registry\n"
7256 " Manage registry hives")
7262 N_("Open interactive shell on remote server"),
7263 N_("net rpc shell\n"
7264 " Open interactive shell on remote server")
7266 {NULL, NULL, 0, NULL, NULL}
7269 status = libnetapi_init(&c->netapi_ctx);
7273 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7274 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7275 if (c->opt_kerberos) {
7276 libnetapi_set_use_kerberos(c->netapi_ctx);
7279 return net_run_function(c, argc, argv, "net rpc", func);