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)
3079 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3080 if (!NT_STATUS_IS_OK(status)) {
3081 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3085 status = cli_tdis(cli);
3086 if (!NT_STATUS_IS_OK(status)) {
3087 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3094 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3095 const char *netname, uint32 type)
3097 /* only support disk shares */
3098 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3099 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3104 /* skip builtin shares */
3105 /* FIXME: should print$ be added too ? */
3106 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3107 strequal(netname,"global"))
3110 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3111 printf(_("excluding [%s]\n"), netname);
3115 return check_share_availability(cli, netname);
3119 * Migrate shares from a remote RPC server to the local RPC server.
3121 * All parameters are provided by the run_rpc_command function, except for
3122 * argc, argv which are passed through.
3124 * @param domain_sid The domain sid acquired from the remote server.
3125 * @param cli A cli_state connected to the server.
3126 * @param mem_ctx Talloc context, destroyed on completion of the function.
3127 * @param argc Standard main() style argc.
3128 * @param argv Standard main() style argv. Initial components are already
3131 * @return Normal NTSTATUS return.
3134 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3135 const DOM_SID *domain_sid,
3136 const char *domain_name,
3137 struct cli_state *cli,
3138 struct rpc_pipe_client *pipe_hnd,
3139 TALLOC_CTX *mem_ctx,
3144 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3145 struct srvsvc_NetShareInfoCtr ctr_src;
3147 struct rpc_pipe_client *srvsvc_pipe = NULL;
3148 struct cli_state *cli_dst = NULL;
3149 uint32 level = 502; /* includes secdesc */
3150 uint32_t parm_error = 0;
3152 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3154 if (!W_ERROR_IS_OK(result))
3157 /* connect destination PI_SRVSVC */
3158 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3159 &ndr_table_srvsvc.syntax_id);
3160 if (!NT_STATUS_IS_OK(nt_status))
3164 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3166 union srvsvc_NetShareInfo info;
3167 struct srvsvc_NetShareInfo502 info502 =
3168 ctr_src.ctr.ctr502->array[i];
3170 /* reset error-code */
3171 nt_status = NT_STATUS_UNSUCCESSFUL;
3173 if (!check_share_sanity(c, cli, info502.name, info502.type))
3176 /* finally add the share on the dst server */
3178 printf(_("migrating: [%s], path: %s, comment: %s, without "
3180 info502.name, info502.path, info502.comment);
3182 info.info502 = &info502;
3184 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3185 srvsvc_pipe->desthost,
3191 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3192 printf(_(" [%s] does already exist\n"),
3197 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3198 printf(_("cannot add share: %s\n"), win_errstr(result));
3204 nt_status = NT_STATUS_OK;
3208 cli_shutdown(cli_dst);
3216 * Migrate shares from a RPC server to another.
3218 * @param argc Standard main() style argc.
3219 * @param argv Standard main() style argv. Initial components are already
3222 * @return A shell status integer (0 for success).
3224 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3227 if (c->display_usage) {
3228 d_printf(_("Usage:\n"
3229 "net rpc share migrate shares\n"
3230 " Migrate shares to local server\n"));
3235 printf(_("no server to migrate\n"));
3239 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3240 rpc_share_migrate_shares_internals,
3247 * @param f file_info
3248 * @param mask current search mask
3249 * @param state arg-pointer
3252 static void copy_fn(const char *mnt, file_info *f,
3253 const char *mask, void *state)
3255 static NTSTATUS nt_status;
3256 static struct copy_clistate *local_state;
3257 static fstring filename, new_mask;
3260 struct net_context *c;
3262 local_state = (struct copy_clistate *)state;
3263 nt_status = NT_STATUS_UNSUCCESSFUL;
3267 if (strequal(f->name, ".") || strequal(f->name, ".."))
3270 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3273 if (f->mode & aDIR) {
3275 DEBUG(3,("got dir: %s\n", f->name));
3277 fstrcpy(dir, local_state->cwd);
3279 fstrcat(dir, f->name);
3281 switch (net_mode_share)
3283 case NET_MODE_SHARE_MIGRATE:
3284 /* create that directory */
3285 nt_status = net_copy_file(c, local_state->mem_ctx,
3286 local_state->cli_share_src,
3287 local_state->cli_share_dst,
3289 c->opt_acls? true : false,
3290 c->opt_attrs? true : false,
3291 c->opt_timestamps? true:false,
3295 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3299 if (!NT_STATUS_IS_OK(nt_status))
3300 printf(_("could not handle dir %s: %s\n"),
3301 dir, nt_errstr(nt_status));
3303 /* search below that directory */
3304 fstrcpy(new_mask, dir);
3305 fstrcat(new_mask, "\\*");
3307 old_dir = local_state->cwd;
3308 local_state->cwd = dir;
3309 if (!sync_files(local_state, new_mask))
3310 printf(_("could not handle files\n"));
3311 local_state->cwd = old_dir;
3318 fstrcpy(filename, local_state->cwd);
3319 fstrcat(filename, "\\");
3320 fstrcat(filename, f->name);
3322 DEBUG(3,("got file: %s\n", filename));
3324 switch (net_mode_share)
3326 case NET_MODE_SHARE_MIGRATE:
3327 nt_status = net_copy_file(c, local_state->mem_ctx,
3328 local_state->cli_share_src,
3329 local_state->cli_share_dst,
3331 c->opt_acls? true : false,
3332 c->opt_attrs? true : false,
3333 c->opt_timestamps? true: false,
3337 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3342 if (!NT_STATUS_IS_OK(nt_status))
3343 printf(_("could not handle file %s: %s\n"),
3344 filename, nt_errstr(nt_status));
3349 * sync files, can be called recursivly to list files
3350 * and then call copy_fn for each file
3352 * @param cp_clistate pointer to the copy_clistate we work with
3353 * @param mask the current search mask
3355 * @return Boolean result
3357 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3359 struct cli_state *targetcli;
3360 char *targetpath = NULL;
3362 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3364 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3365 mask, &targetcli, &targetpath ) ) {
3366 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3368 mask, cli_errstr(cp_clistate->cli_share_src));
3372 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3373 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3374 mask, cli_errstr(targetcli));
3383 * Set the top level directory permissions before we do any further copies.
3384 * Should set up ACL inheritance.
3387 bool copy_top_level_perms(struct net_context *c,
3388 struct copy_clistate *cp_clistate,
3389 const char *sharename)
3391 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3393 switch (net_mode_share) {
3394 case NET_MODE_SHARE_MIGRATE:
3395 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3396 nt_status = net_copy_fileattr(c,
3397 cp_clistate->mem_ctx,
3398 cp_clistate->cli_share_src,
3399 cp_clistate->cli_share_dst,
3401 c->opt_acls? true : false,
3402 c->opt_attrs? true : false,
3403 c->opt_timestamps? true: false,
3407 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3411 if (!NT_STATUS_IS_OK(nt_status)) {
3412 printf(_("Could handle directory attributes for top level "
3413 "directory of share %s. Error %s\n"),
3414 sharename, nt_errstr(nt_status));
3422 * Sync all files inside a remote share to another share (over smb).
3424 * All parameters are provided by the run_rpc_command function, except for
3425 * argc, argv which are passed through.
3427 * @param domain_sid The domain sid acquired from the remote server.
3428 * @param cli A cli_state connected to the server.
3429 * @param mem_ctx Talloc context, destroyed on completion of the function.
3430 * @param argc Standard main() style argc.
3431 * @param argv Standard main() style argv. Initial components are already
3434 * @return Normal NTSTATUS return.
3437 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3438 const DOM_SID *domain_sid,
3439 const char *domain_name,
3440 struct cli_state *cli,
3441 struct rpc_pipe_client *pipe_hnd,
3442 TALLOC_CTX *mem_ctx,
3447 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3448 struct srvsvc_NetShareInfoCtr ctr_src;
3451 struct copy_clistate cp_clistate;
3452 bool got_src_share = false;
3453 bool got_dst_share = false;
3454 const char *mask = "\\*";
3457 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3459 nt_status = NT_STATUS_NO_MEMORY;
3463 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3466 if (!W_ERROR_IS_OK(result))
3469 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3471 struct srvsvc_NetShareInfo502 info502 =
3472 ctr_src.ctr.ctr502->array[i];
3474 if (!check_share_sanity(c, cli, info502.name, info502.type))
3477 /* one might not want to mirror whole discs :) */
3478 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3479 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3484 switch (net_mode_share)
3486 case NET_MODE_SHARE_MIGRATE:
3490 d_fprintf(stderr, _("Unsupported mode %d\n"),
3494 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3497 c->opt_acls ? _("including") : _("without"),
3498 c->opt_attrs ? _("including") : _("without"),
3499 c->opt_timestamps ? _("(preserving timestamps)") : "");
3501 cp_clistate.mem_ctx = mem_ctx;
3502 cp_clistate.cli_share_src = NULL;
3503 cp_clistate.cli_share_dst = NULL;
3504 cp_clistate.cwd = NULL;
3505 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3508 /* open share source */
3509 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3510 &cli->dest_ss, cli->desthost,
3511 info502.name, "A:");
3512 if (!NT_STATUS_IS_OK(nt_status))
3515 got_src_share = true;
3517 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3518 /* open share destination */
3519 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3520 NULL, dst, info502.name, "A:");
3521 if (!NT_STATUS_IS_OK(nt_status))
3524 got_dst_share = true;
3527 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3528 d_fprintf(stderr, _("Could not handle the top level "
3529 "directory permissions for the "
3530 "share: %s\n"), info502.name);
3531 nt_status = NT_STATUS_UNSUCCESSFUL;
3535 if (!sync_files(&cp_clistate, mask)) {
3536 d_fprintf(stderr, _("could not handle files for share: "
3537 "%s\n"), info502.name);
3538 nt_status = NT_STATUS_UNSUCCESSFUL;
3543 nt_status = NT_STATUS_OK;
3548 cli_shutdown(cp_clistate.cli_share_src);
3551 cli_shutdown(cp_clistate.cli_share_dst);
3558 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3560 if (c->display_usage) {
3561 d_printf(_("Usage:\n"
3562 "net share migrate files\n"
3563 " Migrate files to local server\n"));
3568 d_printf(_("no server to migrate\n"));
3572 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3573 rpc_share_migrate_files_internals,
3578 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3580 * All parameters are provided by the run_rpc_command function, except for
3581 * argc, argv which are passed through.
3583 * @param domain_sid The domain sid acquired from the remote server.
3584 * @param cli A cli_state connected to the server.
3585 * @param mem_ctx Talloc context, destroyed on completion of the function.
3586 * @param argc Standard main() style argc.
3587 * @param argv Standard main() style argv. Initial components are already
3590 * @return Normal NTSTATUS return.
3593 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3594 const DOM_SID *domain_sid,
3595 const char *domain_name,
3596 struct cli_state *cli,
3597 struct rpc_pipe_client *pipe_hnd,
3598 TALLOC_CTX *mem_ctx,
3603 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3604 struct srvsvc_NetShareInfoCtr ctr_src;
3605 union srvsvc_NetShareInfo info;
3607 struct rpc_pipe_client *srvsvc_pipe = NULL;
3608 struct cli_state *cli_dst = NULL;
3609 uint32 level = 502; /* includes secdesc */
3610 uint32_t parm_error = 0;
3612 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3615 if (!W_ERROR_IS_OK(result))
3618 /* connect destination PI_SRVSVC */
3619 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3620 &ndr_table_srvsvc.syntax_id);
3621 if (!NT_STATUS_IS_OK(nt_status))
3625 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3627 struct srvsvc_NetShareInfo502 info502 =
3628 ctr_src.ctr.ctr502->array[i];
3630 /* reset error-code */
3631 nt_status = NT_STATUS_UNSUCCESSFUL;
3633 if (!check_share_sanity(c, cli, info502.name, info502.type))
3636 printf(_("migrating: [%s], path: %s, comment: %s, including "
3638 info502.name, info502.path, info502.comment);
3641 display_sec_desc(info502.sd_buf.sd);
3643 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3644 info.info502 = &info502;
3646 /* finally modify the share on the dst server */
3647 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3648 srvsvc_pipe->desthost,
3654 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3655 printf(_("cannot set share-acl: %s\n"),
3656 win_errstr(result));
3662 nt_status = NT_STATUS_OK;
3666 cli_shutdown(cli_dst);
3674 * Migrate share-acls from a RPC server to another.
3676 * @param argc Standard main() style argc.
3677 * @param argv Standard main() style argv. Initial components are already
3680 * @return A shell status integer (0 for success).
3682 static int rpc_share_migrate_security(struct net_context *c, int argc,
3685 if (c->display_usage) {
3686 d_printf(_("Usage:\n"
3687 "net rpc share migrate security\n"
3688 " Migrate share-acls to local server\n"));
3693 d_printf(_("no server to migrate\n"));
3697 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3698 rpc_share_migrate_security_internals,
3703 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3704 * from one server to another.
3706 * @param argc Standard main() style argc.
3707 * @param argv Standard main() style argv. Initial components are already
3710 * @return A shell status integer (0 for success).
3713 static int rpc_share_migrate_all(struct net_context *c, int argc,
3718 if (c->display_usage) {
3719 d_printf(_("Usage:\n"
3720 "net rpc share migrate all\n"
3721 " Migrates shares including all share settings\n"));
3726 d_printf(_("no server to migrate\n"));
3730 /* order is important. we don't want to be locked out by the share-acl
3731 * before copying files - gd */
3733 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3734 rpc_share_migrate_shares_internals, argc, argv);
3738 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3739 rpc_share_migrate_files_internals, argc, argv);
3743 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3744 rpc_share_migrate_security_internals, argc,
3750 * 'net rpc share migrate' entrypoint.
3751 * @param argc Standard main() style argc.
3752 * @param argv Standard main() style argv. Initial components are already
3755 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3758 struct functable func[] = {
3761 rpc_share_migrate_all,
3763 N_("Migrate shares from remote to local server"),
3764 N_("net rpc share migrate all\n"
3765 " Migrate shares from remote to local server")
3769 rpc_share_migrate_files,
3771 N_("Migrate files from remote to local server"),
3772 N_("net rpc share migrate files\n"
3773 " Migrate files from remote to local server")
3777 rpc_share_migrate_security,
3779 N_("Migrate share-ACLs from remote to local server"),
3780 N_("net rpc share migrate security\n"
3781 " Migrate share-ACLs from remote to local server")
3785 rpc_share_migrate_shares,
3787 N_("Migrate shares from remote to local server"),
3788 N_("net rpc share migrate shares\n"
3789 " Migrate shares from remote to local server")
3791 {NULL, NULL, 0, NULL, NULL}
3794 net_mode_share = NET_MODE_SHARE_MIGRATE;
3796 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3805 static int num_server_aliases;
3806 static struct full_alias *server_aliases;
3809 * Add an alias to the static list.
3811 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3813 if (server_aliases == NULL)
3814 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3816 server_aliases[num_server_aliases] = *alias;
3817 num_server_aliases += 1;
3821 * For a specific domain on the server, fetch all the aliases
3822 * and their members. Add all of them to the server_aliases.
3825 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3826 TALLOC_CTX *mem_ctx,
3827 struct policy_handle *connect_pol,
3828 const DOM_SID *domain_sid)
3830 uint32 start_idx, max_entries, num_entries, i;
3831 struct samr_SamArray *groups = NULL;
3833 struct policy_handle domain_pol;
3835 /* Get domain policy handle */
3837 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3839 MAXIMUM_ALLOWED_ACCESS,
3840 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3842 if (!NT_STATUS_IS_OK(result))
3849 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3855 for (i = 0; i < num_entries; i++) {
3857 struct policy_handle alias_pol;
3858 struct full_alias alias;
3859 struct lsa_SidArray sid_array;
3862 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3864 MAXIMUM_ALLOWED_ACCESS,
3865 groups->entries[i].idx,
3867 if (!NT_STATUS_IS_OK(result))
3870 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3873 if (!NT_STATUS_IS_OK(result))
3876 alias.num_members = sid_array.num_sids;
3878 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3879 if (!NT_STATUS_IS_OK(result))
3882 alias.members = NULL;
3884 if (alias.num_members > 0) {
3885 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3887 for (j = 0; j < alias.num_members; j++)
3888 sid_copy(&alias.members[j],
3889 sid_array.sids[j].sid);
3892 sid_compose(&alias.sid, domain_sid,
3893 groups->entries[i].idx);
3895 push_alias(mem_ctx, &alias);
3897 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3899 result = NT_STATUS_OK;
3902 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3908 * Dump server_aliases as names for debugging purposes.
3911 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3912 const DOM_SID *domain_sid,
3913 const char *domain_name,
3914 struct cli_state *cli,
3915 struct rpc_pipe_client *pipe_hnd,
3916 TALLOC_CTX *mem_ctx,
3922 struct policy_handle lsa_pol;
3924 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3925 SEC_FLAG_MAXIMUM_ALLOWED,
3927 if (!NT_STATUS_IS_OK(result))
3930 for (i=0; i<num_server_aliases; i++) {
3933 enum lsa_SidType *types;
3936 struct full_alias *alias = &server_aliases[i];
3938 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3940 &domains, &names, &types);
3941 if (!NT_STATUS_IS_OK(result))
3944 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3946 if (alias->num_members == 0) {
3951 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3954 &domains, &names, &types);
3956 if (!NT_STATUS_IS_OK(result) &&
3957 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3960 for (j=0; j<alias->num_members; j++)
3961 DEBUG(1, ("%s\\%s (%d); ",
3962 domains[j] ? domains[j] : "*unknown*",
3963 names[j] ? names[j] : "*unknown*",types[j]));
3967 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3969 return NT_STATUS_OK;
3973 * Fetch a list of all server aliases and their members into
3977 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3978 const DOM_SID *domain_sid,
3979 const char *domain_name,
3980 struct cli_state *cli,
3981 struct rpc_pipe_client *pipe_hnd,
3982 TALLOC_CTX *mem_ctx,
3987 struct policy_handle connect_pol;
3989 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3991 MAXIMUM_ALLOWED_ACCESS,
3994 if (!NT_STATUS_IS_OK(result))
3997 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3998 &global_sid_Builtin);
4000 if (!NT_STATUS_IS_OK(result))
4003 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4006 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4011 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4013 token->num_sids = 4;
4015 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4016 d_fprintf(stderr, _("malloc failed\n"));
4017 token->num_sids = 0;
4021 token->user_sids[0] = *user_sid;
4022 sid_copy(&token->user_sids[1], &global_sid_World);
4023 sid_copy(&token->user_sids[2], &global_sid_Network);
4024 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4027 static void free_user_token(NT_USER_TOKEN *token)
4029 SAFE_FREE(token->user_sids);
4032 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4034 if (is_sid_in_token(token, sid))
4037 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4038 if (!token->user_sids) {
4042 sid_copy(&token->user_sids[token->num_sids], sid);
4044 token->num_sids += 1;
4049 NT_USER_TOKEN token;
4052 static void dump_user_token(struct user_token *token)
4056 d_printf("%s\n", token->name);
4058 for (i=0; i<token->token.num_sids; i++) {
4059 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4063 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4067 for (i=0; i<alias->num_members; i++) {
4068 if (sid_compare(sid, &alias->members[i]) == 0)
4075 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4079 for (i=0; i<num_server_aliases; i++) {
4080 if (is_alias_member(&sid, &server_aliases[i]))
4081 add_sid_to_token(token, &server_aliases[i].sid);
4086 * We got a user token with all the SIDs we can know about without asking the
4087 * server directly. These are the user and domain group sids. All of these can
4088 * be members of aliases. So scan the list of aliases for each of the SIDs and
4089 * add them to the token.
4092 static void collect_alias_memberships(NT_USER_TOKEN *token)
4094 int num_global_sids = token->num_sids;
4097 for (i=0; i<num_global_sids; i++) {
4098 collect_sid_memberships(token, token->user_sids[i]);
4102 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4104 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4105 enum wbcSidType type;
4107 struct wbcDomainSid wsid;
4108 char *sid_str = NULL;
4110 uint32_t num_groups;
4111 gid_t *groups = NULL;
4114 fstr_sprintf(full_name, "%s%c%s",
4115 domain, *lp_winbind_separator(), user);
4117 /* First let's find out the user sid */
4119 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4121 if (!WBC_ERROR_IS_OK(wbc_status)) {
4122 DEBUG(1, ("winbind could not find %s: %s\n",
4123 full_name, wbcErrorString(wbc_status)));
4127 wbc_status = wbcSidToString(&wsid, &sid_str);
4128 if (!WBC_ERROR_IS_OK(wbc_status)) {
4132 if (type != SID_NAME_USER) {
4133 wbcFreeMemory(sid_str);
4134 DEBUG(1, ("%s is not a user\n", full_name));
4138 if (!string_to_sid(&user_sid, sid_str)) {
4139 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4143 wbcFreeMemory(sid_str);
4146 init_user_token(token, &user_sid);
4148 /* And now the groups winbind knows about */
4150 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4151 if (!WBC_ERROR_IS_OK(wbc_status)) {
4152 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4153 full_name, wbcErrorString(wbc_status)));
4157 for (i = 0; i < num_groups; i++) {
4158 gid_t gid = groups[i];
4161 wbc_status = wbcGidToSid(gid, &wsid);
4162 if (!WBC_ERROR_IS_OK(wbc_status)) {
4163 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4164 (unsigned int)gid, wbcErrorString(wbc_status)));
4165 wbcFreeMemory(groups);
4169 wbc_status = wbcSidToString(&wsid, &sid_str);
4170 if (!WBC_ERROR_IS_OK(wbc_status)) {
4171 wbcFreeMemory(groups);
4175 DEBUG(3, (" %s\n", sid_str));
4177 string_to_sid(&sid, sid_str);
4178 wbcFreeMemory(sid_str);
4181 add_sid_to_token(token, &sid);
4183 wbcFreeMemory(groups);
4189 * Get a list of all user tokens we want to look at
4192 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4193 struct user_token **user_tokens)
4195 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4196 uint32_t i, num_users;
4198 struct user_token *result;
4199 TALLOC_CTX *frame = NULL;
4201 if (lp_winbind_use_default_domain() &&
4202 (c->opt_target_workgroup == NULL)) {
4203 d_fprintf(stderr, _("winbind use default domain = yes set, "
4204 "please specify a workgroup\n"));
4208 /* Send request to winbind daemon */
4210 wbc_status = wbcListUsers(NULL, &num_users, &users);
4211 if (!WBC_ERROR_IS_OK(wbc_status)) {
4212 DEBUG(1, (_("winbind could not list users: %s\n"),
4213 wbcErrorString(wbc_status)));
4217 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4219 if (result == NULL) {
4220 DEBUG(1, ("Could not malloc sid array\n"));
4221 wbcFreeMemory(users);
4225 frame = talloc_stackframe();
4226 for (i=0; i < num_users; i++) {
4227 fstring domain, user;
4230 fstrcpy(result[i].name, users[i]);
4232 p = strchr(users[i], *lp_winbind_separator());
4234 DEBUG(3, ("%s\n", users[i]));
4237 fstrcpy(domain, c->opt_target_workgroup);
4238 fstrcpy(user, users[i]);
4241 fstrcpy(domain, users[i]);
4246 get_user_sids(domain, user, &(result[i].token));
4250 wbcFreeMemory(users);
4252 *num_tokens = num_users;
4253 *user_tokens = result;
4258 static bool get_user_tokens_from_file(FILE *f,
4260 struct user_token **tokens)
4262 struct user_token *token = NULL;
4267 if (fgets(line, sizeof(line)-1, f) == NULL) {
4271 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4272 line[strlen(line)-1] = '\0';
4275 if (line[0] == ' ') {
4279 if(!string_to_sid(&sid, &line[1])) {
4280 DEBUG(1,("get_user_tokens_from_file: Could "
4281 "not convert sid %s \n",&line[1]));
4285 if (token == NULL) {
4286 DEBUG(0, ("File does not begin with username"));
4290 add_sid_to_token(&token->token, &sid);
4294 /* And a new user... */
4297 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4298 if (*tokens == NULL) {
4299 DEBUG(0, ("Could not realloc tokens\n"));
4303 token = &((*tokens)[*num_tokens-1]);
4305 fstrcpy(token->name, line);
4306 token->token.num_sids = 0;
4307 token->token.user_sids = NULL;
4316 * Show the list of all users that have access to a share
4319 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4320 TALLOC_CTX *mem_ctx,
4321 const char *netname,
4323 struct user_token *tokens)
4326 SEC_DESC *share_sd = NULL;
4327 SEC_DESC *root_sd = NULL;
4328 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4330 union srvsvc_NetShareInfo info;
4335 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4342 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4343 DEBUG(1, ("Coult not query secdesc for share %s\n",
4348 share_sd = info.info502->sd_buf.sd;
4349 if (share_sd == NULL) {
4350 DEBUG(1, ("Got no secdesc for share %s\n",
4356 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4360 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4361 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4362 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4365 for (i=0; i<num_tokens; i++) {
4368 if (share_sd != NULL) {
4369 status = se_access_check(share_sd, &tokens[i].token,
4372 if (!NT_STATUS_IS_OK(status)) {
4373 DEBUG(1, ("Could not check share_sd for "
4380 if (root_sd == NULL) {
4381 d_printf(" %s\n", tokens[i].name);
4385 status = se_access_check(root_sd, &tokens[i].token,
4387 if (!NT_STATUS_IS_OK(status)) {
4388 DEBUG(1, ("Could not check root_sd for user %s\n",
4392 d_printf(" %s\n", tokens[i].name);
4395 if (fnum != (uint16_t)-1)
4396 cli_close(cli, fnum);
4408 static void collect_share(const char *name, uint32 m,
4409 const char *comment, void *state)
4411 struct share_list *share_list = (struct share_list *)state;
4413 if (m != STYPE_DISKTREE)
4416 share_list->num_shares += 1;
4417 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4418 if (!share_list->shares) {
4419 share_list->num_shares = 0;
4422 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4426 * List shares on a remote RPC server, including the security descriptors.
4428 * All parameters are provided by the run_rpc_command function, except for
4429 * argc, argv which are passed through.
4431 * @param domain_sid The domain sid acquired from the remote server.
4432 * @param cli A cli_state connected to the server.
4433 * @param mem_ctx Talloc context, destroyed on completion of the function.
4434 * @param argc Standard main() style argc.
4435 * @param argv Standard main() style argv. Initial components are already
4438 * @return Normal NTSTATUS return.
4441 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4442 const DOM_SID *domain_sid,
4443 const char *domain_name,
4444 struct cli_state *cli,
4445 struct rpc_pipe_client *pipe_hnd,
4446 TALLOC_CTX *mem_ctx,
4455 struct user_token *tokens = NULL;
4458 struct share_list share_list;
4463 f = fopen(argv[0], "r");
4467 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4468 return NT_STATUS_UNSUCCESSFUL;
4471 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4477 DEBUG(0, ("Could not read users from file\n"));
4478 return NT_STATUS_UNSUCCESSFUL;
4481 for (i=0; i<num_tokens; i++)
4482 collect_alias_memberships(&tokens[i].token);
4484 share_list.num_shares = 0;
4485 share_list.shares = NULL;
4487 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4490 DEBUG(0, ("Error returning browse list: %s\n",
4495 for (i = 0; i < share_list.num_shares; i++) {
4496 char *netname = share_list.shares[i];
4498 if (netname[strlen(netname)-1] == '$')
4501 d_printf("%s\n", netname);
4503 show_userlist(pipe_hnd, mem_ctx, netname,
4504 num_tokens, tokens);
4507 for (i=0; i<num_tokens; i++) {
4508 free_user_token(&tokens[i].token);
4511 SAFE_FREE(share_list.shares);
4513 return NT_STATUS_OK;
4516 static int rpc_share_allowedusers(struct net_context *c, int argc,
4521 if (c->display_usage) {
4522 d_printf(_("Usage:\n"
4523 "net rpc share allowedusers\n"
4524 " List allowed users\n"));
4528 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4529 rpc_aliaslist_internals,
4534 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4540 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4541 rpc_share_allowedusers_internals,
4545 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4548 struct user_token *tokens = NULL;
4552 net_usersidlist_usage(c, argc, argv);
4556 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4557 DEBUG(0, ("Could not get the user/sid list\n"));
4561 for (i=0; i<num_tokens; i++) {
4562 dump_user_token(&tokens[i]);
4563 free_user_token(&tokens[i].token);
4570 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4572 d_printf(_("net usersidlist\n"
4573 "\tprints out a list of all users the running winbind knows\n"
4574 "\tabout, together with all their SIDs. This is used as\n"
4575 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4577 net_common_flags_usage(c, argc, argv);
4582 * 'net rpc share' entrypoint.
4583 * @param argc Standard main() style argc.
4584 * @param argv Standard main() style argv. Initial components are already
4588 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4590 NET_API_STATUS status;
4592 struct functable func[] = {
4598 N_("net rpc share add\n"
4606 N_("net rpc share delete\n"
4611 rpc_share_allowedusers,
4613 N_("Modify allowed users"),
4614 N_("net rpc share allowedusers\n"
4615 " Modify allowed users")
4621 N_("Migrate share to local server"),
4622 N_("net rpc share migrate\n"
4623 " Migrate share to local server")
4630 N_("net rpc share list\n"
4633 {NULL, NULL, 0, NULL, NULL}
4636 status = libnetapi_init(&c->netapi_ctx);
4640 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4641 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4642 if (c->opt_kerberos) {
4643 libnetapi_set_use_kerberos(c->netapi_ctx);
4647 if (c->display_usage) {
4648 d_printf(_("Usage:\n"
4651 " Alias for net rpc share list\n"));
4652 net_display_usage_from_functable(func);
4656 return rpc_share_list(c, argc, argv);
4659 return net_run_function(c, argc, argv, "net rpc share", func);
4662 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4663 TALLOC_CTX *mem_ctx,
4664 struct rpc_sh_ctx *ctx,
4665 struct rpc_pipe_client *pipe_hnd,
4666 int argc, const char **argv)
4669 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4672 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4673 TALLOC_CTX *mem_ctx,
4674 struct rpc_sh_ctx *ctx,
4675 struct rpc_pipe_client *pipe_hnd,
4676 int argc, const char **argv)
4678 NET_API_STATUS status;
4679 uint32_t parm_err = 0;
4680 struct SHARE_INFO_2 i2;
4682 if ((argc < 2) || (argc > 3)) {
4683 d_fprintf(stderr, _("usage: %s <share> <path> [comment]\n"),
4685 return NT_STATUS_INVALID_PARAMETER;
4688 i2.shi2_netname = argv[0];
4689 i2.shi2_type = STYPE_DISKTREE;
4690 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4691 i2.shi2_permissions = 0;
4692 i2.shi2_max_uses = 0;
4693 i2.shi2_current_uses = 0;
4694 i2.shi2_path = argv[1];
4695 i2.shi2_passwd = NULL;
4697 status = NetShareAdd(pipe_hnd->desthost,
4702 return werror_to_ntstatus(W_ERROR(status));
4705 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4706 TALLOC_CTX *mem_ctx,
4707 struct rpc_sh_ctx *ctx,
4708 struct rpc_pipe_client *pipe_hnd,
4709 int argc, const char **argv)
4712 d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
4713 return NT_STATUS_INVALID_PARAMETER;
4716 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4719 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4720 TALLOC_CTX *mem_ctx,
4721 struct rpc_sh_ctx *ctx,
4722 struct rpc_pipe_client *pipe_hnd,
4723 int argc, const char **argv)
4725 union srvsvc_NetShareInfo info;
4730 d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
4731 return NT_STATUS_INVALID_PARAMETER;
4734 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4740 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4744 d_printf(_("Name: %s\n"), info.info2->name);
4745 d_printf(_("Comment: %s\n"), info.info2->comment);
4746 d_printf(_("Path: %s\n"), info.info2->path);
4747 d_printf(_("Password: %s\n"), info.info2->password);
4750 return werror_to_ntstatus(result);
4753 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4754 struct rpc_sh_ctx *ctx)
4756 static struct rpc_sh_cmd cmds[] = {
4758 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4759 N_("List available shares") },
4761 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4762 N_("Add a share") },
4764 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4765 N_("Delete a share") },
4767 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4768 N_("Get information about a share") },
4770 { NULL, NULL, 0, NULL, NULL }
4776 /****************************************************************************/
4778 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4780 return net_file_usage(c, argc, argv);
4784 * Close a file on a remote RPC server.
4786 * @param argc Standard main() style argc.
4787 * @param argv Standard main() style argv. Initial components are already
4790 * @return A shell status integer (0 for success).
4792 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4794 if (argc < 1 || c->display_usage) {
4795 return rpc_file_usage(c, argc, argv);
4798 return NetFileClose(c->opt_host, atoi(argv[0]));
4802 * Formatted print of open file info
4804 * @param r struct FILE_INFO_3 contents
4807 static void display_file_info_3(struct FILE_INFO_3 *r)
4809 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4810 r->fi3_id, r->fi3_username, r->fi3_permissions,
4811 r->fi3_num_locks, r->fi3_pathname);
4815 * List files for a user on a remote RPC server.
4817 * @param argc Standard main() style argc.
4818 * @param argv Standard main() style argv. Initial components are already
4821 * @return A shell status integer (0 for success)..
4824 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4826 NET_API_STATUS status;
4827 uint32 preferred_len = 0xffffffff, i;
4828 const char *username=NULL;
4829 uint32_t total_entries = 0;
4830 uint32_t entries_read = 0;
4831 uint32_t resume_handle = 0;
4832 struct FILE_INFO_3 *i3 = NULL;
4834 if (c->display_usage) {
4835 return rpc_file_usage(c, argc, argv);
4838 /* if argc > 0, must be user command */
4840 username = smb_xstrdup(argv[0]);
4843 status = NetFileEnum(c->opt_host,
4847 (uint8_t **)(void *)&i3,
4857 /* Display results */
4860 "\nEnumerating open files on remote server:\n\n"
4861 "\nFileId Opened by Perms Locks Path"
4862 "\n------ --------- ----- ----- ---- \n"));
4863 for (i = 0; i < entries_read; i++) {
4864 display_file_info_3(&i3[i]);
4871 * 'net rpc file' entrypoint.
4872 * @param argc Standard main() style argc.
4873 * @param argv Standard main() style argv. Initial components are already
4877 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4879 NET_API_STATUS status;
4881 struct functable func[] = {
4886 N_("Close opened file"),
4887 N_("net rpc file close\n"
4888 " Close opened file")
4894 N_("List files opened by user"),
4895 N_("net rpc file user\n"
4896 " List files opened by user")
4903 N_("Display information about opened file"),
4904 N_("net rpc file info\n"
4905 " Display information about opened file")
4908 {NULL, NULL, 0, NULL, NULL}
4911 status = libnetapi_init(&c->netapi_ctx);
4915 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4916 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4917 if (c->opt_kerberos) {
4918 libnetapi_set_use_kerberos(c->netapi_ctx);
4922 if (c->display_usage) {
4923 d_printf(_("Usage:\n"));
4924 d_printf(_("net rpc file\n"
4925 " List opened files\n"));
4926 net_display_usage_from_functable(func);
4930 return rpc_file_user(c, argc, argv);
4933 return net_run_function(c, argc, argv, "net rpc file", func);
4937 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4939 * All parameters are provided by the run_rpc_command function, except for
4940 * argc, argv which are passed through.
4942 * @param c A net_context structure.
4943 * @param domain_sid The domain sid acquired from the remote server.
4944 * @param cli A cli_state connected to the server.
4945 * @param mem_ctx Talloc context, destroyed on completion of the function.
4946 * @param argc Standard main() style argc.
4947 * @param argv Standard main() style argv. Initial components are already
4950 * @return Normal NTSTATUS return.
4953 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4954 const DOM_SID *domain_sid,
4955 const char *domain_name,
4956 struct cli_state *cli,
4957 struct rpc_pipe_client *pipe_hnd,
4958 TALLOC_CTX *mem_ctx,
4962 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4964 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4966 if (NT_STATUS_IS_OK(result)) {
4967 d_printf(_("\nShutdown successfully aborted\n"));
4968 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4970 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4976 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4978 * All parameters are provided by the run_rpc_command function, except for
4979 * argc, argv which are passed through.
4981 * @param c A net_context structure.
4982 * @param domain_sid The domain sid acquired from the remote server.
4983 * @param cli A cli_state connected to the server.
4984 * @param mem_ctx Talloc context, destroyed on completion of the function.
4985 * @param argc Standard main() style argc.
4986 * @param argv Standard main() style argv. Initial components are already
4989 * @return Normal NTSTATUS return.
4992 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4993 const DOM_SID *domain_sid,
4994 const char *domain_name,
4995 struct cli_state *cli,
4996 struct rpc_pipe_client *pipe_hnd,
4997 TALLOC_CTX *mem_ctx,
5001 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5003 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5005 if (NT_STATUS_IS_OK(result)) {
5006 d_printf(_("\nShutdown successfully aborted\n"));
5007 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5009 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5015 * ABORT the shutdown of a remote RPC server.
5017 * @param argc Standard main() style argc.
5018 * @param argv Standard main() style argv. Initial components are already
5021 * @return A shell status integer (0 for success).
5024 static int rpc_shutdown_abort(struct net_context *c, int argc,
5029 if (c->display_usage) {
5030 d_printf(_("Usage:\n"
5031 "net rpc abortshutdown\n"
5032 " Abort a scheduled shutdown\n"));
5036 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5037 rpc_shutdown_abort_internals, argc, argv);
5042 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5044 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5045 rpc_reg_shutdown_abort_internals,
5050 * Shut down a remote RPC Server via initshutdown pipe.
5052 * All parameters are provided by the run_rpc_command function, except for
5053 * argc, argv which are passed through.
5055 * @param c A net_context structure.
5056 * @param domain_sid The domain sid acquired from the remote server.
5057 * @param cli A cli_state connected to the server.
5058 * @param mem_ctx Talloc context, destroyed on completion of the function.
5059 * @param argc Standard main() style argc.
5060 * @param argv Standard main() style argv. Initial components are already
5063 * @return Normal NTSTATUS return.
5066 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5067 const DOM_SID *domain_sid,
5068 const char *domain_name,
5069 struct cli_state *cli,
5070 struct rpc_pipe_client *pipe_hnd,
5071 TALLOC_CTX *mem_ctx,
5075 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5076 const char *msg = N_("This machine will be shutdown shortly");
5077 uint32 timeout = 20;
5078 struct lsa_StringLarge msg_string;
5080 if (c->opt_comment) {
5081 msg = c->opt_comment;
5083 if (c->opt_timeout) {
5084 timeout = c->opt_timeout;
5087 msg_string.string = msg;
5089 /* create an entry */
5090 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5091 &msg_string, timeout, c->opt_force, c->opt_reboot,
5094 if (NT_STATUS_IS_OK(result)) {
5095 d_printf(_("\nShutdown of remote machine succeeded\n"));
5096 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5098 DEBUG(1,("Shutdown of remote machine failed!\n"));
5104 * Shut down a remote RPC Server via winreg pipe.
5106 * All parameters are provided by the run_rpc_command function, except for
5107 * argc, argv which are passed through.
5109 * @param c A net_context structure.
5110 * @param domain_sid The domain sid acquired from the remote server.
5111 * @param cli A cli_state connected to the server.
5112 * @param mem_ctx Talloc context, destroyed on completion of the function.
5113 * @param argc Standard main() style argc.
5114 * @param argv Standard main() style argv. Initial components are already
5117 * @return Normal NTSTATUS return.
5120 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5121 const DOM_SID *domain_sid,
5122 const char *domain_name,
5123 struct cli_state *cli,
5124 struct rpc_pipe_client *pipe_hnd,
5125 TALLOC_CTX *mem_ctx,
5129 const char *msg = N_("This machine will be shutdown shortly");
5130 uint32 timeout = 20;
5131 struct lsa_StringLarge msg_string;
5135 if (c->opt_comment) {
5136 msg = c->opt_comment;
5138 msg_string.string = msg;
5140 if (c->opt_timeout) {
5141 timeout = c->opt_timeout;
5144 /* create an entry */
5145 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5146 &msg_string, timeout, c->opt_force, c->opt_reboot,
5149 if (NT_STATUS_IS_OK(result)) {
5150 d_printf(_("\nShutdown of remote machine succeeded\n"));
5152 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5153 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5154 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5156 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5163 * Shut down a remote RPC server.
5165 * @param argc Standard main() style argc.
5166 * @param argv Standard main() style argv. Initial components are already
5169 * @return A shell status integer (0 for success).
5172 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5176 if (c->display_usage) {
5177 d_printf(_("Usage:\n"
5178 "net rpc shutdown\n"
5179 " Shut down a remote RPC server\n"));
5183 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5184 rpc_init_shutdown_internals, argc, argv);
5187 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5188 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5189 rpc_reg_shutdown_internals, argc, argv);
5195 /***************************************************************************
5196 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5197 ***************************************************************************/
5200 * Add interdomain trust account to the RPC server.
5201 * All parameters (except for argc and argv) are passed by run_rpc_command
5204 * @param c A net_context structure.
5205 * @param domain_sid The domain sid acquired from the server.
5206 * @param cli A cli_state connected to the server.
5207 * @param mem_ctx Talloc context, destroyed on completion of the function.
5208 * @param argc Standard main() style argc.
5209 * @param argv Standard main() style argv. Initial components are already
5212 * @return normal NTSTATUS return code.
5215 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5216 const DOM_SID *domain_sid,
5217 const char *domain_name,
5218 struct cli_state *cli,
5219 struct rpc_pipe_client *pipe_hnd,
5220 TALLOC_CTX *mem_ctx,
5224 struct policy_handle connect_pol, domain_pol, user_pol;
5225 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5227 struct lsa_String lsa_acct_name;
5229 uint32 acct_flags=0;
5231 uint32_t access_granted = 0;
5232 union samr_UserInfo info;
5233 unsigned int orig_timeout;
5236 d_printf(_("Usage: net rpc trustdom add <domain_name> "
5237 "<trust password>\n"));
5238 return NT_STATUS_INVALID_PARAMETER;
5242 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5245 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5246 return NT_STATUS_NO_MEMORY;
5249 strupper_m(acct_name);
5251 init_lsa_String(&lsa_acct_name, acct_name);
5253 /* Get samr policy handle */
5254 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5256 MAXIMUM_ALLOWED_ACCESS,
5258 if (!NT_STATUS_IS_OK(result)) {
5262 /* Get domain policy handle */
5263 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5265 MAXIMUM_ALLOWED_ACCESS,
5266 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5268 if (!NT_STATUS_IS_OK(result)) {
5272 /* This call can take a long time - allow the server to time out.
5273 * 35 seconds should do it. */
5275 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5277 /* Create trusting domain's account */
5278 acb_info = ACB_NORMAL;
5279 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5280 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5281 SAMR_USER_ACCESS_SET_PASSWORD |
5282 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5283 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5285 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5294 /* And restore our original timeout. */
5295 rpccli_set_timeout(pipe_hnd, orig_timeout);
5297 if (!NT_STATUS_IS_OK(result)) {
5298 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5299 acct_name, nt_errstr(result));
5304 struct samr_CryptPassword crypt_pwd;
5306 ZERO_STRUCT(info.info23);
5308 init_samr_CryptPassword(argv[1],
5309 &cli->user_session_key,
5312 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5313 SAMR_FIELD_NT_PASSWORD_PRESENT;
5314 info.info23.info.acct_flags = ACB_DOMTRUST;
5315 info.info23.password = crypt_pwd;
5317 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5322 if (!NT_STATUS_IS_OK(result)) {
5323 DEBUG(0,("Could not set trust account password: %s\n",
5324 nt_errstr(result)));
5330 SAFE_FREE(acct_name);
5335 * Create interdomain trust account for a remote domain.
5337 * @param argc Standard argc.
5338 * @param argv Standard argv without initial components.
5340 * @return Integer status (0 means success).
5343 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5345 if (argc > 0 && !c->display_usage) {
5346 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5347 rpc_trustdom_add_internals, argc, argv);
5349 d_printf(_("Usage:\n"
5350 "net rpc trustdom add <domain_name> <trust "
5358 * Remove interdomain trust account from the RPC server.
5359 * All parameters (except for argc and argv) are passed by run_rpc_command
5362 * @param c A net_context structure.
5363 * @param domain_sid The domain sid acquired from the server.
5364 * @param cli A cli_state connected to the server.
5365 * @param mem_ctx Talloc context, destroyed on completion of the function.
5366 * @param argc Standard main() style argc.
5367 * @param argv Standard main() style argv. Initial components are already
5370 * @return normal NTSTATUS return code.
5373 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5374 const DOM_SID *domain_sid,
5375 const char *domain_name,
5376 struct cli_state *cli,
5377 struct rpc_pipe_client *pipe_hnd,
5378 TALLOC_CTX *mem_ctx,
5382 struct policy_handle connect_pol, domain_pol, user_pol;
5383 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5385 DOM_SID trust_acct_sid;
5386 struct samr_Ids user_rids, name_types;
5387 struct lsa_String lsa_acct_name;
5390 d_printf(_("Usage: net rpc trustdom del <domain_name>\n"));
5391 return NT_STATUS_INVALID_PARAMETER;
5395 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5397 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5399 if (acct_name == NULL)
5400 return NT_STATUS_NO_MEMORY;
5402 strupper_m(acct_name);
5404 /* Get samr policy handle */
5405 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5407 MAXIMUM_ALLOWED_ACCESS,
5409 if (!NT_STATUS_IS_OK(result)) {
5413 /* Get domain policy handle */
5414 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5416 MAXIMUM_ALLOWED_ACCESS,
5417 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5419 if (!NT_STATUS_IS_OK(result)) {
5423 init_lsa_String(&lsa_acct_name, acct_name);
5425 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5432 if (!NT_STATUS_IS_OK(result)) {
5433 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5435 acct_name, nt_errstr(result) );
5439 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5441 MAXIMUM_ALLOWED_ACCESS,
5445 if (!NT_STATUS_IS_OK(result)) {
5446 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5448 acct_name, nt_errstr(result) );
5452 /* append the rid to the domain sid */
5453 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
5457 /* remove the sid */
5459 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5462 if (!NT_STATUS_IS_OK(result)) {
5463 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5464 " on user %s failed %s\n"),
5465 acct_name, nt_errstr(result) );
5471 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5474 if (!NT_STATUS_IS_OK(result)) {
5475 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5477 acct_name, nt_errstr(result) );
5481 if (!NT_STATUS_IS_OK(result)) {
5482 d_printf(_("Could not set trust account password: %s\n"),
5492 * Delete interdomain trust account for a remote domain.
5494 * @param argc Standard argc.
5495 * @param argv Standard argv without initial components.
5497 * @return Integer status (0 means success).
5500 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5502 if (argc > 0 && !c->display_usage) {
5503 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5504 rpc_trustdom_del_internals, argc, argv);
5506 d_printf(_("Usage:\n"
5507 "net rpc trustdom del <domain>\n"));
5512 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5513 struct cli_state *cli,
5514 TALLOC_CTX *mem_ctx,
5515 const char *domain_name)
5517 char *dc_name = NULL;
5518 const char *buffer = NULL;
5519 struct rpc_pipe_client *netr;
5522 /* Use NetServerEnum2 */
5524 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5526 return NT_STATUS_OK;
5529 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5530 for domain %s\n", domain_name));
5532 /* Try netr_GetDcName */
5534 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5536 if (!NT_STATUS_IS_OK(status)) {
5540 status = rpccli_netr_GetDcName(netr, mem_ctx,
5547 if (NT_STATUS_IS_OK(status)) {
5551 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5552 for domain %s\n", domain_name));
5558 * Establish trust relationship to a trusting domain.
5559 * Interdomain account must already be created on remote PDC.
5561 * @param c A net_context structure.
5562 * @param argc Standard argc.
5563 * @param argv Standard argv without initial components.
5565 * @return Integer status (0 means success).
5568 static int rpc_trustdom_establish(struct net_context *c, int argc,
5571 struct cli_state *cli = NULL;
5572 struct sockaddr_storage server_ss;
5573 struct rpc_pipe_client *pipe_hnd = NULL;
5574 struct policy_handle connect_hnd;
5575 TALLOC_CTX *mem_ctx;
5577 DOM_SID *domain_sid;
5582 union lsa_PolicyInformation *info = NULL;
5585 * Connect to \\server\ipc$ as 'our domain' account with password
5588 if (argc != 1 || c->display_usage) {
5589 d_printf(_("Usage:\n"
5590 "net rpc trustdom establish <domain_name>\n"));
5594 domain_name = smb_xstrdup(argv[0]);
5595 strupper_m(domain_name);
5597 /* account name used at first is our domain's name with '$' */
5598 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5601 strupper_m(acct_name);
5604 * opt_workgroup will be used by connection functions further,
5605 * hence it should be set to remote domain name instead of ours
5607 if (c->opt_workgroup) {
5608 c->opt_workgroup = smb_xstrdup(domain_name);
5611 c->opt_user_name = acct_name;
5613 /* find the domain controller */
5614 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5615 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5619 /* connect to ipc$ as username/password */
5620 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5621 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5623 /* Is it trusting domain account for sure ? */
5624 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5625 nt_errstr(nt_status)));
5629 /* store who we connected to */
5631 saf_store( domain_name, pdc_name );
5634 * Connect to \\server\ipc$ again (this time anonymously)
5637 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5640 if (NT_STATUS_IS_ERR(nt_status)) {
5641 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5642 domain_name, nt_errstr(nt_status)));
5646 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5647 "domain %s", domain_name))) {
5648 DEBUG(0, ("talloc_init() failed\n"));
5653 /* Make sure we're talking to a proper server */
5655 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5656 if (!NT_STATUS_IS_OK(nt_status)) {
5658 talloc_destroy(mem_ctx);
5663 * Call LsaOpenPolicy and LsaQueryInfo
5666 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5668 if (!NT_STATUS_IS_OK(nt_status)) {
5669 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5671 talloc_destroy(mem_ctx);
5675 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5677 if (NT_STATUS_IS_ERR(nt_status)) {
5678 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5679 nt_errstr(nt_status)));
5681 talloc_destroy(mem_ctx);
5685 /* Querying info level 5 */
5687 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5689 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5691 if (NT_STATUS_IS_ERR(nt_status)) {
5692 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5693 nt_errstr(nt_status)));
5695 talloc_destroy(mem_ctx);
5699 domain_sid = info->account_domain.sid;
5701 /* There should be actually query info level 3 (following nt serv behaviour),
5702 but I still don't know if it's _really_ necessary */
5705 * Store the password in secrets db
5708 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5709 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5711 talloc_destroy(mem_ctx);
5716 * Close the pipes and clean up
5719 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5720 if (NT_STATUS_IS_ERR(nt_status)) {
5721 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5722 nt_errstr(nt_status)));
5724 talloc_destroy(mem_ctx);
5730 talloc_destroy(mem_ctx);
5732 d_printf(_("Trust to domain %s established\n"), domain_name);
5737 * Revoke trust relationship to the remote domain.
5739 * @param c A net_context structure.
5740 * @param argc Standard argc.
5741 * @param argv Standard argv without initial components.
5743 * @return Integer status (0 means success).
5746 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5752 if (argc < 1 || c->display_usage) {
5753 d_printf(_("Usage:\n"
5754 "net rpc trustdom revoke <domain_name>\n"
5755 " Revoke trust relationship\n"
5756 " domain_name\tName of domain to revoke trust\n"));
5760 /* generate upper cased domain name */
5761 domain_name = smb_xstrdup(argv[0]);
5762 strupper_m(domain_name);
5764 /* delete password of the trust */
5765 if (!pdb_del_trusteddom_pw(domain_name)) {
5766 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5773 SAFE_FREE(domain_name);
5777 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5778 const DOM_SID *domain_sid,
5779 const char *domain_name,
5780 struct cli_state *cli,
5781 struct rpc_pipe_client *pipe_hnd,
5782 TALLOC_CTX *mem_ctx,
5787 if (!sid_to_fstring(str_sid, domain_sid)) {
5788 return NT_STATUS_UNSUCCESSFUL;
5790 d_printf("%s\n", str_sid);
5791 return NT_STATUS_OK;
5794 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5798 /* convert sid into ascii string */
5799 sid_to_fstring(ascii_sid, dom_sid);
5801 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5804 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5805 TALLOC_CTX *mem_ctx,
5806 struct policy_handle *pol,
5808 const char *trusted_dom_name)
5811 union lsa_TrustedDomainInfo *info = NULL;
5812 char *cleartextpwd = NULL;
5813 uint8_t session_key[16];
5814 DATA_BLOB session_key_blob;
5815 DATA_BLOB data = data_blob_null;
5817 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5820 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5822 if (NT_STATUS_IS_ERR(nt_status)) {
5823 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5824 nt_errstr(nt_status)));
5828 data = data_blob(info->password.password->data,
5829 info->password.password->length);
5831 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5832 DEBUG(0, ("Could not retrieve password hash\n"));
5836 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5837 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5839 if (cleartextpwd == NULL) {
5840 DEBUG(0,("retrieved NULL password\n"));
5841 nt_status = NT_STATUS_UNSUCCESSFUL;
5845 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5846 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5847 nt_status = NT_STATUS_UNSUCCESSFUL;
5851 #ifdef DEBUG_PASSWORD
5852 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5853 "password: [%s]\n", trusted_dom_name,
5854 sid_string_dbg(&dom_sid), cleartextpwd));
5858 SAFE_FREE(cleartextpwd);
5859 data_blob_free(&data);
5864 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5867 /* common variables */
5868 TALLOC_CTX* mem_ctx;
5869 struct cli_state *cli = NULL;
5870 struct rpc_pipe_client *pipe_hnd = NULL;
5872 const char *domain_name = NULL;
5873 DOM_SID *queried_dom_sid;
5874 struct policy_handle connect_hnd;
5875 union lsa_PolicyInformation *info = NULL;
5877 /* trusted domains listing variables */
5878 unsigned int enum_ctx = 0;
5880 struct lsa_DomainList dom_list;
5883 if (c->display_usage) {
5884 d_printf(_("Usage:\n"
5885 "net rpc trustdom vampire\n"
5886 " Vampire trust relationship from remote server\n"));
5891 * Listing trusted domains (stored in secrets.tdb, if local)
5894 mem_ctx = talloc_init("trust relationships vampire");
5897 * set domain and pdc name to local samba server (default)
5898 * or to remote one given in command line
5901 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5902 domain_name = c->opt_workgroup;
5903 c->opt_target_workgroup = c->opt_workgroup;
5905 fstrcpy(pdc_name, global_myname());
5906 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5907 c->opt_target_workgroup = domain_name;
5910 /* open \PIPE\lsarpc and open policy handle */
5911 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5912 if (!NT_STATUS_IS_OK(nt_status)) {
5913 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5914 nt_errstr(nt_status)));
5915 talloc_destroy(mem_ctx);
5919 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5921 if (!NT_STATUS_IS_OK(nt_status)) {
5922 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5923 nt_errstr(nt_status) ));
5925 talloc_destroy(mem_ctx);
5929 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5931 if (NT_STATUS_IS_ERR(nt_status)) {
5932 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5933 nt_errstr(nt_status)));
5935 talloc_destroy(mem_ctx);
5939 /* query info level 5 to obtain sid of a domain being queried */
5940 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5942 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5945 if (NT_STATUS_IS_ERR(nt_status)) {
5946 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5947 nt_errstr(nt_status)));
5949 talloc_destroy(mem_ctx);
5953 queried_dom_sid = info->account_domain.sid;
5956 * Keep calling LsaEnumTrustdom over opened pipe until
5957 * the end of enumeration is reached
5960 d_printf(_("Vampire trusted domains:\n\n"));
5963 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5968 if (NT_STATUS_IS_ERR(nt_status)) {
5969 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5970 nt_errstr(nt_status)));
5972 talloc_destroy(mem_ctx);
5976 for (i = 0; i < dom_list.count; i++) {
5978 print_trusted_domain(dom_list.domains[i].sid,
5979 dom_list.domains[i].name.string);
5981 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5982 *dom_list.domains[i].sid,
5983 dom_list.domains[i].name.string);
5984 if (!NT_STATUS_IS_OK(nt_status)) {
5986 talloc_destroy(mem_ctx);
5992 * in case of no trusted domains say something rather
5993 * than just display blank line
5995 if (!dom_list.count) d_printf(_("none\n"));
5997 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5999 /* close this connection before doing next one */
6000 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6001 if (NT_STATUS_IS_ERR(nt_status)) {
6002 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6003 nt_errstr(nt_status)));
6005 talloc_destroy(mem_ctx);
6009 /* close lsarpc pipe and connection to IPC$ */
6012 talloc_destroy(mem_ctx);
6016 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6018 /* common variables */
6019 TALLOC_CTX* mem_ctx;
6020 struct cli_state *cli = NULL, *remote_cli = NULL;
6021 struct rpc_pipe_client *pipe_hnd = NULL;
6023 const char *domain_name = NULL;
6024 DOM_SID *queried_dom_sid;
6025 int ascii_dom_name_len;
6026 struct policy_handle connect_hnd;
6027 union lsa_PolicyInformation *info = NULL;
6029 /* trusted domains listing variables */
6030 unsigned int num_domains, enum_ctx = 0;
6032 struct lsa_DomainList dom_list;
6036 /* trusting domains listing variables */
6037 struct policy_handle domain_hnd;
6038 struct samr_SamArray *trusts = NULL;
6040 if (c->display_usage) {
6041 d_printf(_("Usage:\n"
6042 "net rpc trustdom list\n"
6043 " List in- and outgoing trust relationships\n"));
6048 * Listing trusted domains (stored in secrets.tdb, if local)
6051 mem_ctx = talloc_init("trust relationships listing");
6054 * set domain and pdc name to local samba server (default)
6055 * or to remote one given in command line
6058 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6059 domain_name = c->opt_workgroup;
6060 c->opt_target_workgroup = c->opt_workgroup;
6062 fstrcpy(pdc_name, global_myname());
6063 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6064 c->opt_target_workgroup = domain_name;
6067 /* open \PIPE\lsarpc and open policy handle */
6068 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6069 if (!NT_STATUS_IS_OK(nt_status)) {
6070 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6071 nt_errstr(nt_status)));
6072 talloc_destroy(mem_ctx);
6076 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6078 if (!NT_STATUS_IS_OK(nt_status)) {
6079 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6080 nt_errstr(nt_status) ));
6082 talloc_destroy(mem_ctx);
6086 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6088 if (NT_STATUS_IS_ERR(nt_status)) {
6089 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6090 nt_errstr(nt_status)));
6092 talloc_destroy(mem_ctx);
6096 /* query info level 5 to obtain sid of a domain being queried */
6097 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6099 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6102 if (NT_STATUS_IS_ERR(nt_status)) {
6103 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6104 nt_errstr(nt_status)));
6106 talloc_destroy(mem_ctx);
6110 queried_dom_sid = info->account_domain.sid;
6113 * Keep calling LsaEnumTrustdom over opened pipe until
6114 * the end of enumeration is reached
6117 d_printf(_("Trusted domains list:\n\n"));
6119 found_domain = false;
6122 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6127 if (NT_STATUS_IS_ERR(nt_status)) {
6128 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6129 nt_errstr(nt_status)));
6131 talloc_destroy(mem_ctx);
6135 for (i = 0; i < dom_list.count; i++) {
6136 print_trusted_domain(dom_list.domains[i].sid,
6137 dom_list.domains[i].name.string);
6138 found_domain = true;
6142 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6145 * in case of no trusted domains say something rather
6146 * than just display blank line
6148 if (!found_domain) {
6149 d_printf(_("none\n"));
6152 /* close this connection before doing next one */
6153 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6154 if (NT_STATUS_IS_ERR(nt_status)) {
6155 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6156 nt_errstr(nt_status)));
6158 talloc_destroy(mem_ctx);
6162 TALLOC_FREE(pipe_hnd);
6165 * Listing trusting domains (stored in passdb backend, if local)
6168 d_printf(_("\nTrusting domains list:\n\n"));
6171 * Open \PIPE\samr and get needed policy handles
6173 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6175 if (!NT_STATUS_IS_OK(nt_status)) {
6176 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6178 talloc_destroy(mem_ctx);
6183 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6185 SAMR_ACCESS_LOOKUP_DOMAIN,
6187 if (!NT_STATUS_IS_OK(nt_status)) {
6188 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6189 nt_errstr(nt_status)));
6191 talloc_destroy(mem_ctx);
6195 /* SamrOpenDomain - we have to open domain policy handle in order to be
6196 able to enumerate accounts*/
6197 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6199 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6202 if (!NT_STATUS_IS_OK(nt_status)) {
6203 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6204 nt_errstr(nt_status)));
6206 talloc_destroy(mem_ctx);
6211 * perform actual enumeration
6214 found_domain = false;
6216 enum_ctx = 0; /* reset enumeration context from last enumeration */
6219 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6226 if (NT_STATUS_IS_ERR(nt_status)) {
6227 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6228 nt_errstr(nt_status)));
6230 talloc_destroy(mem_ctx);
6234 for (i = 0; i < num_domains; i++) {
6236 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6238 found_domain = true;
6241 * get each single domain's sid (do we _really_ need this ?):
6242 * 1) connect to domain's pdc
6243 * 2) query the pdc for domain's sid
6246 /* get rid of '$' tail */
6247 ascii_dom_name_len = strlen(str);
6248 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6249 str[ascii_dom_name_len - 1] = '\0';
6251 /* set opt_* variables to remote domain */
6253 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6254 c->opt_target_workgroup = c->opt_workgroup;
6256 d_printf("%-20s", str);
6258 /* connect to remote domain controller */
6259 nt_status = net_make_ipc_connection(c,
6260 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6262 if (NT_STATUS_IS_OK(nt_status)) {
6263 /* query for domain's sid */
6264 if (run_rpc_command(
6266 &ndr_table_lsarpc.syntax_id, 0,
6267 rpc_query_domain_sid, argc,
6269 d_printf(_("strange - couldn't get domain's sid\n"));
6271 cli_shutdown(remote_cli);
6274 d_fprintf(stderr, _("domain controller is not "
6275 "responding: %s\n"),
6276 nt_errstr(nt_status));
6277 d_printf(_("couldn't get domain's sid\n"));
6281 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6283 if (!found_domain) {
6287 /* close opened samr and domain policy handles */
6288 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6289 if (!NT_STATUS_IS_OK(nt_status)) {
6290 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6293 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6294 if (!NT_STATUS_IS_OK(nt_status)) {
6295 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6298 /* close samr pipe and connection to IPC$ */
6301 talloc_destroy(mem_ctx);
6306 * Entrypoint for 'net rpc trustdom' code.
6308 * @param argc Standard argc.
6309 * @param argv Standard argv without initial components.
6311 * @return Integer status (0 means success).
6314 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6316 struct functable func[] = {
6321 N_("Add trusting domain's account"),
6322 N_("net rpc trustdom add\n"
6323 " Add trusting domain's account")
6329 N_("Remove trusting domain's account"),
6330 N_("net rpc trustdom del\n"
6331 " Remove trusting domain's account")
6335 rpc_trustdom_establish,
6337 N_("Establish outgoing trust relationship"),
6338 N_("net rpc trustdom establish\n"
6339 " Establish outgoing trust relationship")
6343 rpc_trustdom_revoke,
6345 N_("Revoke outgoing trust relationship"),
6346 N_("net rpc trustdom revoke\n"
6347 " Revoke outgoing trust relationship")
6353 N_("List in- and outgoing domain trusts"),
6354 N_("net rpc trustdom list\n"
6355 " List in- and outgoing domain trusts")
6359 rpc_trustdom_vampire,
6361 N_("Vampire trusts from remote server"),
6362 N_("net rpc trustdom vampire\n"
6363 " Vampire trusts from remote server")
6365 {NULL, NULL, 0, NULL, NULL}
6368 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6372 * Check if a server will take rpc commands
6373 * @param flags Type of server to connect to (PDC, DMB, localhost)
6374 * if the host is not explicitly specified
6375 * @return bool (true means rpc supported)
6377 bool net_rpc_check(struct net_context *c, unsigned flags)
6379 struct cli_state *cli;
6381 struct sockaddr_storage server_ss;
6382 char *server_name = NULL;
6385 /* flags (i.e. server type) may depend on command */
6386 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6389 if ((cli = cli_initialise()) == NULL) {
6393 status = cli_connect(cli, server_name, &server_ss);
6394 if (!NT_STATUS_IS_OK(status))
6396 if (!attempt_netbios_session_request(&cli, global_myname(),
6397 server_name, &server_ss))
6399 status = cli_negprot(cli);
6400 if (!NT_STATUS_IS_OK(status))
6402 if (cli->protocol < PROTOCOL_NT1)
6411 /* dump sam database via samsync rpc calls */
6412 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6413 if (c->display_usage) {
6414 d_printf(_("Usage:\n"
6416 " Dump remote SAM database\n"));
6420 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6421 NET_FLAGS_ANONYMOUS,
6422 rpc_samdump_internals, argc, argv);
6425 /* syncronise sam database via samsync rpc calls */
6426 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6428 struct functable func[] = {
6433 N_("Dump remote SAM database to ldif"),
6434 N_("net rpc vampire ldif\n"
6435 " Dump remote SAM database to LDIF file or "
6442 N_("Dump remote SAM database to Kerberos Keytab"),
6443 N_("net rpc vampire keytab\n"
6444 " Dump remote SAM database to Kerberos keytab "
6451 N_("Dump remote SAM database to passdb"),
6452 N_("net rpc vampire passdb\n"
6453 " Dump remote SAM database to passdb")
6456 {NULL, NULL, 0, NULL, NULL}
6460 if (c->display_usage) {
6461 d_printf(_("Usage:\n"
6463 " Vampire remote SAM database\n"));
6467 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6468 NET_FLAGS_ANONYMOUS,
6469 rpc_vampire_internals,
6473 return net_run_function(c, argc, argv, "net rpc vampire", func);
6477 * Migrate everything from a print server.
6479 * @param c A net_context structure.
6480 * @param argc Standard main() style argc.
6481 * @param argv Standard main() style argv. Initial components are already
6484 * @return A shell status integer (0 for success).
6486 * The order is important !
6487 * To successfully add drivers the print queues have to exist !
6488 * Applying ACLs should be the last step, because you're easily locked out.
6491 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6496 if (c->display_usage) {
6497 d_printf(_("Usage:\n"
6498 "net rpc printer migrate all\n"
6499 " Migrate everything from a print server\n"));
6504 d_printf(_("no server to migrate\n"));
6508 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6509 rpc_printer_migrate_printers_internals, argc,
6514 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6515 rpc_printer_migrate_drivers_internals, argc,
6520 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6521 rpc_printer_migrate_forms_internals, argc, argv);
6525 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6526 rpc_printer_migrate_settings_internals, argc,
6531 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6532 rpc_printer_migrate_security_internals, argc,
6538 * Migrate print drivers from a print server.
6540 * @param c A net_context structure.
6541 * @param argc Standard main() style argc.
6542 * @param argv Standard main() style argv. Initial components are already
6545 * @return A shell status integer (0 for success).
6547 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6550 if (c->display_usage) {
6551 d_printf(_("Usage:\n"
6552 "net rpc printer migrate drivers\n"
6553 " Migrate print-drivers from a print-server\n"));
6558 d_printf(_("no server to migrate\n"));
6562 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6563 rpc_printer_migrate_drivers_internals,
6568 * Migrate print-forms from a print-server.
6570 * @param c A net_context structure.
6571 * @param argc Standard main() style argc.
6572 * @param argv Standard main() style argv. Initial components are already
6575 * @return A shell status integer (0 for success).
6577 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6580 if (c->display_usage) {
6581 d_printf(_("Usage:\n"
6582 "net rpc printer migrate forms\n"
6583 " Migrate print-forms from a print-server\n"));
6588 d_printf(_("no server to migrate\n"));
6592 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6593 rpc_printer_migrate_forms_internals,
6598 * Migrate printers from a print-server.
6600 * @param c A net_context structure.
6601 * @param argc Standard main() style argc.
6602 * @param argv Standard main() style argv. Initial components are already
6605 * @return A shell status integer (0 for success).
6607 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6610 if (c->display_usage) {
6611 d_printf(_("Usage:\n"
6612 "net rpc printer migrate printers\n"
6613 " Migrate printers from a print-server\n"));
6618 d_printf(_("no server to migrate\n"));
6622 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6623 rpc_printer_migrate_printers_internals,
6628 * Migrate printer-ACLs from a print-server
6630 * @param c A net_context structure.
6631 * @param argc Standard main() style argc.
6632 * @param argv Standard main() style argv. Initial components are already
6635 * @return A shell status integer (0 for success).
6637 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6640 if (c->display_usage) {
6641 d_printf(_("Usage:\n"
6642 "net rpc printer migrate security\n"
6643 " Migrate printer-ACLs from a print-server\n"));
6648 d_printf(_("no server to migrate\n"));
6652 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6653 rpc_printer_migrate_security_internals,
6658 * Migrate printer-settings from a print-server.
6660 * @param c A net_context structure.
6661 * @param argc Standard main() style argc.
6662 * @param argv Standard main() style argv. Initial components are already
6665 * @return A shell status integer (0 for success).
6667 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6670 if (c->display_usage) {
6671 d_printf(_("Usage:\n"
6672 "net rpc printer migrate settings\n"
6673 " Migrate printer-settings from a "
6679 d_printf(_("no server to migrate\n"));
6683 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6684 rpc_printer_migrate_settings_internals,
6689 * 'net rpc printer' entrypoint.
6691 * @param c A net_context structure.
6692 * @param argc Standard main() style argc.
6693 * @param argv Standard main() style argv. Initial components are already
6697 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6700 /* ouch: when addriver and setdriver are called from within
6701 rpc_printer_migrate_drivers_internals, the printer-queue already
6704 struct functable func[] = {
6707 rpc_printer_migrate_all,
6709 N_("Migrate all from remote to local print server"),
6710 N_("net rpc printer migrate all\n"
6711 " Migrate all from remote to local print server")
6715 rpc_printer_migrate_drivers,
6717 N_("Migrate drivers to local server"),
6718 N_("net rpc printer migrate drivers\n"
6719 " Migrate drivers to local server")
6723 rpc_printer_migrate_forms,
6725 N_("Migrate froms to local server"),
6726 N_("net rpc printer migrate forms\n"
6727 " Migrate froms to local server")
6731 rpc_printer_migrate_printers,
6733 N_("Migrate printers to local server"),
6734 N_("net rpc printer migrate printers\n"
6735 " Migrate printers to local server")
6739 rpc_printer_migrate_security,
6741 N_("Mirgate printer ACLs to local server"),
6742 N_("net rpc printer migrate security\n"
6743 " Mirgate printer ACLs to local server")
6747 rpc_printer_migrate_settings,
6749 N_("Migrate printer settings to local server"),
6750 N_("net rpc printer migrate settings\n"
6751 " Migrate printer settings to local server")
6753 {NULL, NULL, 0, NULL, NULL}
6756 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6761 * List printers on a remote RPC server.
6763 * @param c A net_context structure.
6764 * @param argc Standard main() style argc.
6765 * @param argv Standard main() style argv. Initial components are already
6768 * @return A shell status integer (0 for success).
6770 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6772 if (c->display_usage) {
6773 d_printf(_("Usage:\n"
6774 "net rpc printer list\n"
6775 " List printers on a remote RPC server\n"));
6779 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6780 rpc_printer_list_internals,
6785 * List printer-drivers on a remote RPC server.
6787 * @param c A net_context structure.
6788 * @param argc Standard main() style argc.
6789 * @param argv Standard main() style argv. Initial components are already
6792 * @return A shell status integer (0 for success).
6794 static int rpc_printer_driver_list(struct net_context *c, int argc,
6797 if (c->display_usage) {
6798 d_printf(_("Usage:\n"
6799 "net rpc printer driver\n"
6800 " List printer-drivers on a remote RPC server\n"));
6804 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6805 rpc_printer_driver_list_internals,
6810 * Publish printer in ADS via MSRPC.
6812 * @param c A net_context structure.
6813 * @param argc Standard main() style argc.
6814 * @param argv Standard main() style argv. Initial components are already
6817 * @return A shell status integer (0 for success).
6819 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6822 if (c->display_usage) {
6823 d_printf(_("Usage:\n"
6824 "net rpc printer publish publish\n"
6825 " Publish printer in ADS via MSRPC\n"));
6829 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6830 rpc_printer_publish_publish_internals,
6835 * Update printer in ADS via MSRPC.
6837 * @param c A net_context structure.
6838 * @param argc Standard main() style argc.
6839 * @param argv Standard main() style argv. Initial components are already
6842 * @return A shell status integer (0 for success).
6844 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6846 if (c->display_usage) {
6847 d_printf(_("Usage:\n"
6848 "net rpc printer publish update\n"
6849 " Update printer in ADS via MSRPC\n"));
6853 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6854 rpc_printer_publish_update_internals,
6859 * UnPublish printer in ADS via MSRPC.
6861 * @param c A net_context structure.
6862 * @param argc Standard main() style argc.
6863 * @param argv Standard main() style argv. Initial components are already
6866 * @return A shell status integer (0 for success).
6868 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6871 if (c->display_usage) {
6872 d_printf(_("Usage:\n"
6873 "net rpc printer publish unpublish\n"
6874 " UnPublish printer in ADS via MSRPC\n"));
6878 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6879 rpc_printer_publish_unpublish_internals,
6884 * List published printers via MSRPC.
6886 * @param c A net_context structure.
6887 * @param argc Standard main() style argc.
6888 * @param argv Standard main() style argv. Initial components are already
6891 * @return A shell status integer (0 for success).
6893 static int rpc_printer_publish_list(struct net_context *c, int argc,
6896 if (c->display_usage) {
6897 d_printf(_("Usage:\n"
6898 "net rpc printer publish list\n"
6899 " List published printers via MSRPC\n"));
6903 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6904 rpc_printer_publish_list_internals,
6910 * Publish printer in ADS.
6912 * @param c A net_context structure.
6913 * @param argc Standard main() style argc.
6914 * @param argv Standard main() style argv. Initial components are already
6917 * @return A shell status integer (0 for success).
6919 static int rpc_printer_publish(struct net_context *c, int argc,
6923 struct functable func[] = {
6926 rpc_printer_publish_publish,
6928 N_("Publish printer in AD"),
6929 N_("net rpc printer publish publish\n"
6930 " Publish printer in AD")
6934 rpc_printer_publish_update,
6936 N_("Update printer in AD"),
6937 N_("net rpc printer publish update\n"
6938 " Update printer in AD")
6942 rpc_printer_publish_unpublish,
6944 N_("Unpublish printer"),
6945 N_("net rpc printer publish unpublish\n"
6946 " Unpublish printer")
6950 rpc_printer_publish_list,
6952 N_("List published printers"),
6953 N_("net rpc printer publish list\n"
6954 " List published printers")
6956 {NULL, NULL, 0, NULL, NULL}
6960 if (c->display_usage) {
6961 d_printf(_("Usage:\n"));
6962 d_printf(_("net rpc printer publish\n"
6963 " List published printers\n"
6964 " Alias of net rpc printer publish "
6966 net_display_usage_from_functable(func);
6969 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6970 rpc_printer_publish_list_internals,
6974 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6980 * Display rpc printer help page.
6982 * @param c A net_context structure.
6983 * @param argc Standard main() style argc.
6984 * @param argv Standard main() style argv. Initial components are already
6987 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6989 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
6990 "\tlists all printers on print-server\n\n"));
6991 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6992 "\tlists all printer-drivers on print-server\n\n"));
6993 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6994 "\tpublishes printer settings in Active Directory\n"
6995 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
6996 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6997 "\n\tmigrates printers from remote to local server\n\n"));
6998 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6999 "\n\tmigrates printer-settings from remote to local server\n\n"));
7000 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7001 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7002 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7003 "\n\tmigrates printer-forms from remote to local server\n\n"));
7004 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7005 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7006 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7007 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7008 "\tremote to local print-server\n\n"));
7009 net_common_methods_usage(c, argc, argv);
7010 net_common_flags_usage(c, argc, argv);
7012 "\t-v or --verbose\t\t\tgive verbose output\n"
7013 "\t --destination\t\tmigration target server (default: localhost)\n"));
7019 * 'net rpc printer' entrypoint.
7021 * @param c A net_context structure.
7022 * @param argc Standard main() style argc.
7023 * @param argv Standard main() style argv. Initial components are already
7026 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7028 struct functable func[] = {
7033 N_("List all printers on print server"),
7034 N_("net rpc printer list\n"
7035 " List all printers on print server")
7039 rpc_printer_migrate,
7041 N_("Migrate printer to local server"),
7042 N_("net rpc printer migrate\n"
7043 " Migrate printer to local server")
7047 rpc_printer_driver_list,
7049 N_("List printer drivers"),
7050 N_("net rpc printer driver\n"
7051 " List printer drivers")
7055 rpc_printer_publish,
7057 N_("Publish printer in AD"),
7058 N_("net rpc printer publish\n"
7059 " Publish printer in AD")
7061 {NULL, NULL, 0, NULL, NULL}
7065 if (c->display_usage) {
7066 d_printf(_("Usage:\n"));
7067 d_printf(_("net rpc printer\n"
7068 " List printers\n"));
7069 net_display_usage_from_functable(func);
7072 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7073 rpc_printer_list_internals,
7077 return net_run_function(c, argc, argv, "net rpc printer", func);
7081 * 'net rpc' entrypoint.
7083 * @param c A net_context structure.
7084 * @param argc Standard main() style argc.
7085 * @param argv Standard main() style argv. Initial components are already
7089 int net_rpc(struct net_context *c, int argc, const char **argv)
7091 NET_API_STATUS status;
7093 struct functable func[] = {
7098 N_("Modify global audit settings"),
7099 N_("net rpc audit\n"
7100 " Modify global audit settings")
7106 N_("Show basic info about a domain"),
7108 " Show basic info about a domain")
7114 N_("Join a domain"),
7122 N_("Join a domain created in server manager"),
7123 N_("net rpc oldjoin\n"
7124 " Join a domain created in server manager")
7130 N_("Test that a join is valid"),
7131 N_("net rpc testjoin\n"
7132 " Test that a join is valid")
7138 N_("List/modify users"),
7140 " List/modify users")
7146 N_("Change a user password"),
7147 N_("net rpc password\n"
7148 " Change a user password\n"
7149 " Alias for net rpc user password")
7155 N_("List/modify groups"),
7156 N_("net rpc group\n"
7157 " List/modify groups")
7163 N_("List/modify shares"),
7164 N_("net rpc share\n"
7165 " List/modify shares")
7171 N_("List open files"),
7179 N_("List/modify printers"),
7180 N_("net rpc printer\n"
7181 " List/modify printers")
7185 net_rpc_changetrustpw,
7187 N_("Change trust account password"),
7188 N_("net rpc changetrustpw\n"
7189 " Change trust account password")
7195 N_("Modify domain trusts"),
7196 N_("net rpc trustdom\n"
7197 " Modify domain trusts")
7203 N_("Abort a remote shutdown"),
7204 N_("net rpc abortshutdown\n"
7205 " Abort a remote shutdown")
7211 N_("Shutdown a remote server"),
7212 N_("net rpc shutdown\n"
7213 " Shutdown a remote server")
7219 N_("Dump SAM data of remote NT PDC"),
7220 N_("net rpc samdump\n"
7221 " Dump SAM data of remote NT PDC")
7227 N_("Sync a remote NT PDC's data into local passdb"),
7228 N_("net rpc vampire\n"
7229 " Sync a remote NT PDC's data into local passdb")
7235 N_("Fetch the domain sid into local secrets.tdb"),
7236 N_("net rpc getsid\n"
7237 " Fetch the domain sid into local secrets.tdb")
7243 N_("Manage privileges assigned to SID"),
7244 N_("net rpc rights\n"
7245 " Manage privileges assigned to SID")
7251 N_("Start/stop/query remote services"),
7252 N_("net rpc service\n"
7253 " Start/stop/query remote services")
7259 N_("Manage registry hives"),
7260 N_("net rpc registry\n"
7261 " Manage registry hives")
7267 N_("Open interactive shell on remote server"),
7268 N_("net rpc shell\n"
7269 " Open interactive shell on remote server")
7271 {NULL, NULL, 0, NULL, NULL}
7274 status = libnetapi_init(&c->netapi_ctx);
7278 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7279 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7280 if (c->opt_kerberos) {
7281 libnetapi_set_use_kerberos(c->netapi_ctx);
7284 return net_run_function(c, argc, argv, "net rpc", func);