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 "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/cli_samr.h"
29 #include "rpc_client/init_samr.h"
30 #include "../librpc/gen_ndr/ndr_lsa_c.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
34 #include "../librpc/gen_ndr/ndr_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
36 #include "../librpc/gen_ndr/ndr_winreg_c.h"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "rpc_client/init_lsa.h"
41 #include "../libcli/security/security.h"
43 static int net_mode_share;
44 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
49 * @brief RPC based subcommands for the 'net' utility.
51 * This file should contain much of the functionality that used to
52 * be found in rpcclient, execpt that the commands should change
53 * less often, and the fucntionality should be sane (the user is not
54 * expected to know a rid/sid before they conduct an operation etc.)
56 * @todo Perhaps eventually these should be split out into a number
57 * of files, as this could get quite big.
62 * Many of the RPC functions need the domain sid. This function gets
63 * it at the start of every run
65 * @param cli A cli_state already connected to the remote machine
67 * @return The Domain SID of the remote machine.
70 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
71 struct dom_sid **domain_sid,
72 const char **domain_name)
74 struct rpc_pipe_client *lsa_pipe = NULL;
75 struct policy_handle pol;
76 NTSTATUS status, result;
77 union lsa_PolicyInformation *info = NULL;
78 struct dcerpc_binding_handle *b;
80 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
82 if (!NT_STATUS_IS_OK(status)) {
83 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
87 b = lsa_pipe->binding_handle;
89 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
90 SEC_FLAG_MAXIMUM_ALLOWED,
92 if (!NT_STATUS_IS_OK(status)) {
93 d_fprintf(stderr, "open_policy %s: %s\n",
99 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
101 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
104 if (!NT_STATUS_IS_OK(status)) {
105 d_fprintf(stderr, "lsaquery %s: %s\n",
110 if (!NT_STATUS_IS_OK(result)) {
111 d_fprintf(stderr, "lsaquery %s: %s\n",
117 *domain_name = info->account_domain.name.string;
118 *domain_sid = info->account_domain.sid;
120 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
121 TALLOC_FREE(lsa_pipe);
127 * Run a single RPC command, from start to finish.
129 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
130 * @param conn_flag a NET_FLAG_ combination. Passed to
131 * net_make_ipc_connection.
132 * @param argc Standard main() style argc.
133 * @param argv Standard main() style argv. Initial components are already
135 * @return A shell status integer (0 for success).
138 int run_rpc_command(struct net_context *c,
139 struct cli_state *cli_arg,
140 const struct ndr_syntax_id *interface,
146 struct cli_state *cli = NULL;
147 struct rpc_pipe_client *pipe_hnd = NULL;
150 struct dom_sid *domain_sid;
151 const char *domain_name;
154 /* make use of cli_state handed over as an argument, if possible */
156 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
157 if (!NT_STATUS_IS_OK(nt_status)) {
158 DEBUG(1, ("failed to make ipc connection: %s\n",
159 nt_errstr(nt_status)));
172 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
173 DEBUG(0, ("talloc_init() failed\n"));
177 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
179 if (!NT_STATUS_IS_OK(nt_status)) {
183 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
184 if (lp_client_schannel()
185 && (ndr_syntax_id_equal(interface,
186 &ndr_table_netlogon.syntax_id))) {
187 /* Always try and create an schannel netlogon pipe. */
188 nt_status = cli_rpc_pipe_open_schannel(
189 cli, interface, NCACN_NP,
190 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
192 if (!NT_STATUS_IS_OK(nt_status)) {
193 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
194 nt_errstr(nt_status) ));
198 if (conn_flags & NET_FLAGS_SEAL) {
199 nt_status = cli_rpc_pipe_open_ntlmssp(
201 (conn_flags & NET_FLAGS_TCP) ?
202 NCACN_IP_TCP : NCACN_NP,
203 DCERPC_AUTH_LEVEL_PRIVACY,
204 lp_workgroup(), c->opt_user_name,
205 c->opt_password, &pipe_hnd);
207 nt_status = cli_rpc_pipe_open_noauth(
211 if (!NT_STATUS_IS_OK(nt_status)) {
212 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
213 get_pipe_name_from_syntax(
214 talloc_tos(), interface),
215 nt_errstr(nt_status) ));
221 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
223 if (!NT_STATUS_IS_OK(nt_status)) {
224 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
227 DEBUG(5, ("rpc command function succedded\n"));
230 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
232 TALLOC_FREE(pipe_hnd);
237 /* close the connection only if it was opened here */
242 talloc_destroy(mem_ctx);
247 * Force a change of the trust acccount password.
249 * All parameters are provided by the run_rpc_command function, except for
250 * argc, argv which are passed through.
252 * @param domain_sid The domain sid acquired from the remote server.
253 * @param cli A cli_state connected to the server.
254 * @param mem_ctx Talloc context, destroyed on completion of the function.
255 * @param argc Standard main() style argc.
256 * @param argv Standard main() style argv. Initial components are already
259 * @return Normal NTSTATUS return.
262 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
263 const struct dom_sid *domain_sid,
264 const char *domain_name,
265 struct cli_state *cli,
266 struct rpc_pipe_client *pipe_hnd,
273 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
274 if (!NT_STATUS_IS_OK(status)) {
275 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
284 * Force a change of the trust acccount password.
286 * @param argc Standard main() style argc.
287 * @param argv Standard main() style argv. Initial components are already
290 * @return A shell status integer (0 for success).
293 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
295 if (c->display_usage) {
297 "net rpc changetrustpw\n"
300 _("Change the machine trust password"));
304 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
305 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
306 rpc_changetrustpw_internals,
311 * Join a domain, the old way.
313 * This uses 'machinename' as the inital password, and changes it.
315 * The password should be created with 'server manager' or equiv first.
317 * All parameters are provided by the run_rpc_command function, except for
318 * argc, argv which are passed through.
320 * @param domain_sid The domain sid acquired from the remote server.
321 * @param cli A cli_state connected to the server.
322 * @param mem_ctx Talloc context, destroyed on completion of the function.
323 * @param argc Standard main() style argc.
324 * @param argv Standard main() style argv. Initial components are already
327 * @return Normal NTSTATUS return.
330 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
331 const struct dom_sid *domain_sid,
332 const char *domain_name,
333 struct cli_state *cli,
334 struct rpc_pipe_client *pipe_hnd,
340 fstring trust_passwd;
341 unsigned char orig_trust_passwd_hash[16];
343 enum netr_SchannelType sec_channel_type;
345 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
347 if (!NT_STATUS_IS_OK(result)) {
348 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
351 nt_errstr(result) ));
356 check what type of join - if the user want's to join as
357 a BDC, the server must agree that we are a BDC.
360 sec_channel_type = get_sec_channel_type(argv[0]);
362 sec_channel_type = get_sec_channel_type(NULL);
365 fstrcpy(trust_passwd, global_myname());
366 strlower_m(trust_passwd);
369 * Machine names can be 15 characters, but the max length on
370 * a password is 14. --jerry
373 trust_passwd[14] = '\0';
375 E_md4hash(trust_passwd, orig_trust_passwd_hash);
377 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
379 orig_trust_passwd_hash,
382 if (NT_STATUS_IS_OK(result))
383 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
386 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
387 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
388 result = NT_STATUS_UNSUCCESSFUL;
395 * Join a domain, the old way.
397 * @param argc Standard main() style argc.
398 * @param argv Standard main() style argv. Initial components are already
401 * @return A shell status integer (0 for success).
404 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
406 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
407 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
408 rpc_oldjoin_internals,
413 * Join a domain, the old way. This function exists to allow
414 * the message to be displayed when oldjoin was explicitly
415 * requested, but not when it was implied by "net rpc join".
417 * @param argc Standard main() style argc.
418 * @param argv Standard main() style argv. Initial components are already
421 * @return A shell status integer (0 for success).
424 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
428 if (c->display_usage) {
433 _("Join a domain the old way"));
437 rc = net_rpc_perform_oldjoin(c, argc, argv);
440 d_fprintf(stderr, _("Failed to join domain\n"));
447 * 'net rpc join' entrypoint.
448 * @param argc Standard main() style argc.
449 * @param argv Standard main() style argv. Initial components are already
452 * Main 'net_rpc_join()' (where the admin username/password is used) is
454 * Try to just change the password, but if that doesn't work, use/prompt
455 * for a username/password.
458 int net_rpc_join(struct net_context *c, int argc, const char **argv)
460 if (c->display_usage) {
463 _("net rpc join -U <username>[%%password] <type>\n"
465 " username\tName of the admin user"
466 " password\tPassword of the admin user, will "
467 "prompt if not specified\n"
468 " type\tCan be one of the following:\n"
469 "\t\tMEMBER\tJoin as member server (default)\n"
470 "\t\tBDC\tJoin as BDC\n"
471 "\t\tPDC\tJoin as PDC\n"));
475 if (lp_server_role() == ROLE_STANDALONE) {
476 d_printf(_("cannot join as standalone machine\n"));
480 if (strlen(global_myname()) > 15) {
481 d_printf(_("Our netbios name can be at most 15 chars long, "
482 "\"%s\" is %u chars long\n"),
483 global_myname(), (unsigned int)strlen(global_myname()));
487 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
490 return net_rpc_join_newstyle(c, argc, argv);
494 * display info about a rpc domain
496 * All parameters are provided by the run_rpc_command function, except for
497 * argc, argv which are passed through.
499 * @param domain_sid The domain sid acquired from the remote server
500 * @param cli A cli_state connected to the server.
501 * @param mem_ctx Talloc context, destroyed on completion of the function.
502 * @param argc Standard main() style argc.
503 * @param argv Standard main() style argv. Initial components are already
506 * @return Normal NTSTATUS return.
509 NTSTATUS rpc_info_internals(struct net_context *c,
510 const struct dom_sid *domain_sid,
511 const char *domain_name,
512 struct cli_state *cli,
513 struct rpc_pipe_client *pipe_hnd,
518 struct policy_handle connect_pol, domain_pol;
519 NTSTATUS status, result;
520 union samr_DomainInfo *info = NULL;
522 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
524 sid_to_fstring(sid_str, domain_sid);
526 /* Get sam policy handle */
527 status = dcerpc_samr_Connect2(b, mem_ctx,
529 MAXIMUM_ALLOWED_ACCESS,
532 if (!NT_STATUS_IS_OK(status)) {
533 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
538 if (!NT_STATUS_IS_OK(result)) {
540 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
545 /* Get domain policy handle */
546 status = dcerpc_samr_OpenDomain(b, mem_ctx,
548 MAXIMUM_ALLOWED_ACCESS,
549 CONST_DISCARD(struct dom_sid2 *, domain_sid),
552 if (!NT_STATUS_IS_OK(status)) {
553 d_fprintf(stderr, _("Could not open domain: %s\n"),
557 if (!NT_STATUS_IS_OK(result)) {
559 d_fprintf(stderr, _("Could not open domain: %s\n"),
564 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
569 if (!NT_STATUS_IS_OK(status)) {
573 if (NT_STATUS_IS_OK(result)) {
574 d_printf(_("Domain Name: %s\n"),
575 info->general.domain_name.string);
576 d_printf(_("Domain SID: %s\n"), sid_str);
577 d_printf(_("Sequence number: %llu\n"),
578 (unsigned long long)info->general.sequence_num);
579 d_printf(_("Num users: %u\n"), info->general.num_users);
580 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
581 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
589 * 'net rpc info' entrypoint.
590 * @param argc Standard main() style argc.
591 * @param argv Standard main() style argv. Initial components are already
595 int net_rpc_info(struct net_context *c, int argc, const char **argv)
597 if (c->display_usage) {
602 _("Display information about the domain"));
606 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
607 NET_FLAGS_PDC, rpc_info_internals,
612 * Fetch domain SID into the local secrets.tdb.
614 * All parameters are provided by the run_rpc_command function, except for
615 * argc, argv which are passed through.
617 * @param domain_sid The domain sid acquired from the remote server.
618 * @param cli A cli_state connected to the server.
619 * @param mem_ctx Talloc context, destroyed on completion of the function.
620 * @param argc Standard main() style argc.
621 * @param argv Standard main() style argv. Initial components are already
624 * @return Normal NTSTATUS return.
627 static NTSTATUS rpc_getsid_internals(struct net_context *c,
628 const struct dom_sid *domain_sid,
629 const char *domain_name,
630 struct cli_state *cli,
631 struct rpc_pipe_client *pipe_hnd,
638 sid_to_fstring(sid_str, domain_sid);
639 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
640 sid_str, domain_name);
642 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
643 DEBUG(0,("Can't store domain SID\n"));
644 return NT_STATUS_UNSUCCESSFUL;
651 * 'net rpc getsid' entrypoint.
652 * @param argc Standard main() style argc.
653 * @param argv Standard main() style argv. Initial components are already
657 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
659 int conn_flags = NET_FLAGS_PDC;
661 if (!c->opt_user_specified) {
662 conn_flags |= NET_FLAGS_ANONYMOUS;
665 if (c->display_usage) {
670 _("Fetch domain SID into local secrets.tdb"));
674 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
676 rpc_getsid_internals,
680 /****************************************************************************/
683 * Basic usage function for 'net rpc user'.
684 * @param argc Standard main() style argc.
685 * @param argv Standard main() style argv. Initial components are already
689 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
691 return net_user_usage(c, argc, argv);
695 * Add a new user to a remote RPC server.
697 * @param argc Standard main() style argc.
698 * @param argv Standard main() style argv. Initial components are already
701 * @return A shell status integer (0 for success).
704 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
706 NET_API_STATUS status;
707 struct USER_INFO_1 info1;
708 uint32_t parm_error = 0;
710 if (argc < 1 || c->display_usage) {
711 rpc_user_usage(c, argc, argv);
717 info1.usri1_name = argv[0];
719 info1.usri1_password = argv[1];
722 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
725 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
726 argv[0], libnetapi_get_error_string(c->netapi_ctx,
730 d_printf(_("Added user '%s'.\n"), argv[0]);
737 * Rename a user on a remote RPC server.
739 * @param argc Standard main() style argc.
740 * @param argv Standard main() style argv. Initial components are already
743 * @return A shell status integer (0 for success).
746 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
748 NET_API_STATUS status;
749 struct USER_INFO_0 u0;
750 uint32_t parm_err = 0;
752 if (argc != 2 || c->display_usage) {
753 rpc_user_usage(c, argc, argv);
757 u0.usri0_name = argv[1];
759 status = NetUserSetInfo(c->opt_host, argv[0],
760 0, (uint8_t *)&u0, &parm_err);
763 _("Failed to rename user from %s to %s - %s\n"),
765 libnetapi_get_error_string(c->netapi_ctx, status));
767 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
774 * Set a user's primary group
776 * @param argc Standard main() style argc.
777 * @param argv Standard main() style argv. Initial components are already
780 * @return A shell status integer (0 for success).
783 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
786 NET_API_STATUS status;
788 struct GROUP_INFO_2 *g2;
789 struct USER_INFO_1051 u1051;
790 uint32_t parm_err = 0;
792 if (argc != 2 || c->display_usage) {
793 rpc_user_usage(c, argc, argv);
797 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
799 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
801 libnetapi_get_error_string(c->netapi_ctx, status));
804 g2 = (struct GROUP_INFO_2 *)buffer;
806 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
808 NetApiBufferFree(buffer);
810 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
811 (uint8_t *)&u1051, &parm_err);
814 _("Failed to set user's primary group %s to %s - "
815 "%s\n"), argv[0], argv[1],
816 libnetapi_get_error_string(c->netapi_ctx, status));
818 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
825 * Delete a user from a remote RPC server.
827 * @param argc Standard main() style argc.
828 * @param argv Standard main() style argv. Initial components are already
831 * @return A shell status integer (0 for success).
834 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
836 NET_API_STATUS status;
838 if (argc < 1 || c->display_usage) {
839 rpc_user_usage(c, argc, argv);
843 status = NetUserDel(c->opt_host, argv[0]);
846 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
848 libnetapi_get_error_string(c->netapi_ctx, status));
851 d_printf(_("Deleted user '%s'.\n"), argv[0]);
858 * Set a user's password on a remote RPC server.
860 * @param argc Standard main() style argc.
861 * @param argv Standard main() style argv. Initial components are already
864 * @return A shell status integer (0 for success).
867 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
869 NET_API_STATUS status;
871 struct USER_INFO_1003 u1003;
872 uint32_t parm_err = 0;
875 if (argc < 1 || c->display_usage) {
876 rpc_user_usage(c, argc, argv);
881 u1003.usri1003_password = argv[1];
883 ret = asprintf(&prompt, _("Enter new password for %s:"),
888 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
890 if (u1003.usri1003_password == NULL) {
895 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
897 /* Display results */
900 _("Failed to set password for '%s' with error: %s.\n"),
901 argv[0], libnetapi_get_error_string(c->netapi_ctx,
910 * List a user's groups from a remote RPC server.
912 * @param argc Standard main() style argc.
913 * @param argv Standard main() style argv. Initial components are already
916 * @return A shell status integer (0 for success)
919 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
922 NET_API_STATUS status;
923 struct GROUP_USERS_INFO_0 *u0 = NULL;
924 uint32_t entries_read = 0;
925 uint32_t total_entries = 0;
929 if (argc < 1 || c->display_usage) {
930 rpc_user_usage(c, argc, argv);
934 status = NetUserGetGroups(c->opt_host,
937 (uint8_t **)(void *)&u0,
943 _("Failed to get groups for '%s' with error: %s.\n"),
944 argv[0], libnetapi_get_error_string(c->netapi_ctx,
949 for (i=0; i < entries_read; i++) {
950 printf("%s\n", u0->grui0_name);
958 * List users on a remote RPC server.
960 * All parameters are provided by the run_rpc_command function, except for
961 * argc, argv which are passed through.
963 * @param domain_sid The domain sid acquired from the remote server.
964 * @param cli A cli_state connected to the server.
965 * @param mem_ctx Talloc context, destroyed on completion of the function.
966 * @param argc Standard main() style argc.
967 * @param argv Standard main() style argv. Initial components are already
970 * @return Normal NTSTATUS return.
973 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
975 NET_API_STATUS status;
976 uint32_t start_idx=0, num_entries, i, loop_count = 0;
977 struct NET_DISPLAY_USER *info = NULL;
980 /* Query domain users */
981 if (c->opt_long_list_entries)
982 d_printf(_("\nUser name Comment"
983 "\n-----------------------------\n"));
985 uint32_t max_entries, max_size;
987 dcerpc_get_query_dispinfo_params(
988 loop_count, &max_entries, &max_size);
990 status = NetQueryDisplayInformation(c->opt_host,
997 if (status != 0 && status != ERROR_MORE_DATA) {
1001 info = (struct NET_DISPLAY_USER *)buffer;
1003 for (i = 0; i < num_entries; i++) {
1005 if (c->opt_long_list_entries)
1006 printf("%-21.21s %s\n", info->usri1_name,
1007 info->usri1_comment);
1009 printf("%s\n", info->usri1_name);
1013 NetApiBufferFree(buffer);
1016 start_idx += num_entries;
1018 } while (status == ERROR_MORE_DATA);
1024 * 'net rpc user' entrypoint.
1025 * @param argc Standard main() style argc.
1026 * @param argv Standard main() style argv. Initial components are already
1030 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1032 NET_API_STATUS status;
1034 struct functable func[] = {
1039 N_("Add specified user"),
1040 N_("net rpc user add\n"
1041 " Add specified user")
1047 N_("List domain groups of user"),
1048 N_("net rpc user info\n"
1049 " List domain groups of user")
1055 N_("Remove specified user"),
1056 N_("net rpc user delete\n"
1057 " Remove specified user")
1063 N_("Change user password"),
1064 N_("net rpc user password\n"
1065 " Change user password")
1071 N_("Rename specified user"),
1072 N_("net rpc user rename\n"
1073 " Rename specified user")
1077 rpc_user_setprimarygroup,
1079 "Set a user's primary group",
1080 "net rpc user setprimarygroup\n"
1081 " Set a user's primary group"
1083 {NULL, NULL, 0, NULL, NULL}
1086 status = libnetapi_net_init(&c->netapi_ctx);
1090 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1091 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1092 if (c->opt_kerberos) {
1093 libnetapi_set_use_kerberos(c->netapi_ctx);
1097 if (c->display_usage) {
1102 _("List all users"));
1103 net_display_usage_from_functable(func);
1107 return rpc_user_list(c, argc, argv);
1110 return net_run_function(c, argc, argv, "net rpc user", func);
1113 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1114 TALLOC_CTX *mem_ctx,
1115 struct rpc_sh_ctx *ctx,
1116 struct rpc_pipe_client *pipe_hnd,
1117 int argc, const char **argv)
1119 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1122 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1123 TALLOC_CTX *mem_ctx,
1124 struct rpc_sh_ctx *ctx,
1125 struct rpc_pipe_client *pipe_hnd,
1126 int argc, const char **argv)
1128 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1131 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1132 TALLOC_CTX *mem_ctx,
1133 struct rpc_sh_ctx *ctx,
1134 struct rpc_pipe_client *pipe_hnd,
1135 int argc, const char **argv,
1137 struct net_context *c,
1138 TALLOC_CTX *mem_ctx,
1139 struct rpc_sh_ctx *ctx,
1140 struct rpc_pipe_client *pipe_hnd,
1141 struct policy_handle *user_hnd,
1142 int argc, const char **argv))
1144 struct policy_handle connect_pol, domain_pol, user_pol;
1145 NTSTATUS status, result;
1148 enum lsa_SidType type;
1149 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1152 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1154 return NT_STATUS_INVALID_PARAMETER;
1157 ZERO_STRUCT(connect_pol);
1158 ZERO_STRUCT(domain_pol);
1159 ZERO_STRUCT(user_pol);
1161 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1162 argv[0], NULL, NULL, &sid, &type);
1163 if (!NT_STATUS_IS_OK(status)) {
1164 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1169 if (type != SID_NAME_USER) {
1170 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1171 sid_type_lookup(type));
1172 status = NT_STATUS_NO_SUCH_USER;
1176 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1177 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1178 status = NT_STATUS_NO_SUCH_USER;
1182 status = dcerpc_samr_Connect2(b, mem_ctx,
1184 MAXIMUM_ALLOWED_ACCESS,
1187 if (!NT_STATUS_IS_OK(status)) {
1190 if (!NT_STATUS_IS_OK(result)) {
1195 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1197 MAXIMUM_ALLOWED_ACCESS,
1201 if (!NT_STATUS_IS_OK(status)) {
1204 if (!NT_STATUS_IS_OK(result)) {
1209 status = dcerpc_samr_OpenUser(b, mem_ctx,
1211 MAXIMUM_ALLOWED_ACCESS,
1215 if (!NT_STATUS_IS_OK(status)) {
1218 if (!NT_STATUS_IS_OK(result)) {
1223 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1226 if (is_valid_policy_hnd(&user_pol)) {
1227 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1229 if (is_valid_policy_hnd(&domain_pol)) {
1230 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1232 if (is_valid_policy_hnd(&connect_pol)) {
1233 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1238 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1239 TALLOC_CTX *mem_ctx,
1240 struct rpc_sh_ctx *ctx,
1241 struct rpc_pipe_client *pipe_hnd,
1242 struct policy_handle *user_hnd,
1243 int argc, const char **argv)
1245 NTSTATUS status, result;
1246 union samr_UserInfo *info = NULL;
1247 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1250 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1252 return NT_STATUS_INVALID_PARAMETER;
1255 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1260 if (!NT_STATUS_IS_OK(status)) {
1263 if (!NT_STATUS_IS_OK(result)) {
1267 d_printf(_("user rid: %d, group rid: %d\n"),
1269 info->info21.primary_gid);
1274 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1275 TALLOC_CTX *mem_ctx,
1276 struct rpc_sh_ctx *ctx,
1277 struct rpc_pipe_client *pipe_hnd,
1278 int argc, const char **argv)
1280 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1281 rpc_sh_user_show_internals);
1284 #define FETCHSTR(name, rec) \
1285 do { if (strequal(ctx->thiscmd, name)) { \
1286 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1289 #define SETSTR(name, rec, flag) \
1290 do { if (strequal(ctx->thiscmd, name)) { \
1291 init_lsa_String(&(info->info21.rec), argv[0]); \
1292 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1295 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1296 TALLOC_CTX *mem_ctx,
1297 struct rpc_sh_ctx *ctx,
1298 struct rpc_pipe_client *pipe_hnd,
1299 struct policy_handle *user_hnd,
1300 int argc, const char **argv)
1302 NTSTATUS status, result;
1303 const char *username;
1304 const char *oldval = "";
1305 union samr_UserInfo *info = NULL;
1306 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1309 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1310 _("Usage:"), ctx->whoami);
1311 return NT_STATUS_INVALID_PARAMETER;
1314 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1319 if (!NT_STATUS_IS_OK(status)) {
1322 if (!NT_STATUS_IS_OK(result)) {
1326 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1328 FETCHSTR("fullname", full_name);
1329 FETCHSTR("homedir", home_directory);
1330 FETCHSTR("homedrive", home_drive);
1331 FETCHSTR("logonscript", logon_script);
1332 FETCHSTR("profilepath", profile_path);
1333 FETCHSTR("description", description);
1336 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1340 if (strcmp(argv[0], "NULL") == 0) {
1344 ZERO_STRUCT(info->info21);
1346 SETSTR("fullname", full_name, FULL_NAME);
1347 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1348 SETSTR("homedrive", home_drive, HOME_DRIVE);
1349 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1350 SETSTR("profilepath", profile_path, PROFILE_PATH);
1351 SETSTR("description", description, DESCRIPTION);
1353 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1358 if (!NT_STATUS_IS_OK(status)) {
1364 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1365 ctx->thiscmd, oldval, argv[0]);
1372 #define HANDLEFLG(name, rec) \
1373 do { if (strequal(ctx->thiscmd, name)) { \
1374 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1376 newflags = oldflags | ACB_##rec; \
1378 newflags = oldflags & ~ACB_##rec; \
1381 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1382 TALLOC_CTX *mem_ctx,
1383 struct rpc_sh_ctx *ctx,
1384 struct rpc_pipe_client *pipe_hnd,
1385 int argc, const char **argv)
1387 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1388 rpc_sh_user_str_edit_internals);
1391 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1392 TALLOC_CTX *mem_ctx,
1393 struct rpc_sh_ctx *ctx,
1394 struct rpc_pipe_client *pipe_hnd,
1395 struct policy_handle *user_hnd,
1396 int argc, const char **argv)
1398 NTSTATUS status, result;
1399 const char *username;
1400 const char *oldval = "unknown";
1401 uint32 oldflags, newflags;
1403 union samr_UserInfo *info = NULL;
1404 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1407 ((argc == 1) && !strequal(argv[0], "yes") &&
1408 !strequal(argv[0], "no"))) {
1409 /* TRANSATORS: The yes|no here are program keywords. Please do
1411 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1413 return NT_STATUS_INVALID_PARAMETER;
1416 newval = strequal(argv[0], "yes");
1418 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1423 if (!NT_STATUS_IS_OK(status)) {
1426 if (!NT_STATUS_IS_OK(result)) {
1430 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1431 oldflags = info->info21.acct_flags;
1432 newflags = info->info21.acct_flags;
1434 HANDLEFLG("disabled", DISABLED);
1435 HANDLEFLG("pwnotreq", PWNOTREQ);
1436 HANDLEFLG("autolock", AUTOLOCK);
1437 HANDLEFLG("pwnoexp", PWNOEXP);
1440 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1445 ZERO_STRUCT(info->info21);
1447 info->info21.acct_flags = newflags;
1448 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1450 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1455 if (!NT_STATUS_IS_OK(status)) {
1459 if (NT_STATUS_IS_OK(result)) {
1460 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1461 ctx->thiscmd, oldval, argv[0]);
1469 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1470 TALLOC_CTX *mem_ctx,
1471 struct rpc_sh_ctx *ctx,
1472 struct rpc_pipe_client *pipe_hnd,
1473 int argc, const char **argv)
1475 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1476 rpc_sh_user_flag_edit_internals);
1479 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1480 TALLOC_CTX *mem_ctx,
1481 struct rpc_sh_ctx *ctx)
1483 static struct rpc_sh_cmd cmds[] = {
1485 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1486 N_("Show/Set a user's full name") },
1488 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1489 N_("Show/Set a user's home directory") },
1491 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1492 N_("Show/Set a user's home drive") },
1494 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1495 N_("Show/Set a user's logon script") },
1497 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1498 N_("Show/Set a user's profile path") },
1500 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1501 N_("Show/Set a user's description") },
1503 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1504 N_("Show/Set whether a user is disabled") },
1506 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1507 N_("Show/Set whether a user locked out") },
1509 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1510 N_("Show/Set whether a user does not need a password") },
1512 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1513 N_("Show/Set whether a user's password does not expire") },
1515 { NULL, NULL, 0, NULL, NULL }
1521 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1522 TALLOC_CTX *mem_ctx,
1523 struct rpc_sh_ctx *ctx)
1525 static struct rpc_sh_cmd cmds[] = {
1527 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1528 N_("List available users") },
1530 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1531 N_("List the domain groups a user is member of") },
1533 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1534 N_("Show info about a user") },
1536 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1537 N_("Show/Modify a user's fields") },
1539 { NULL, NULL, 0, NULL, NULL }
1545 /****************************************************************************/
1548 * Basic usage function for 'net rpc group'.
1549 * @param argc Standard main() style argc.
1550 * @param argv Standard main() style argv. Initial components are already
1554 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1556 return net_group_usage(c, argc, argv);
1560 * Delete group on a remote RPC server.
1562 * All parameters are provided by the run_rpc_command function, except for
1563 * argc, argv which are passed through.
1565 * @param domain_sid The domain sid acquired from the remote server.
1566 * @param cli A cli_state connected to the server.
1567 * @param mem_ctx Talloc context, destroyed on completion of the function.
1568 * @param argc Standard main() style argc.
1569 * @param argv Standard main() style argv. Initial components are already
1572 * @return Normal NTSTATUS return.
1575 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1576 const struct dom_sid *domain_sid,
1577 const char *domain_name,
1578 struct cli_state *cli,
1579 struct rpc_pipe_client *pipe_hnd,
1580 TALLOC_CTX *mem_ctx,
1584 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1585 bool group_is_primary = false;
1586 NTSTATUS status, result;
1588 struct samr_RidAttrArray *rids = NULL;
1591 /* struct samr_RidWithAttribute *user_gids; */
1592 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1594 struct samr_Ids group_rids, name_types;
1595 struct lsa_String lsa_acct_name;
1596 union samr_UserInfo *info = NULL;
1598 if (argc < 1 || c->display_usage) {
1599 rpc_group_usage(c, argc,argv);
1600 return NT_STATUS_OK; /* ok? */
1603 status = dcerpc_samr_Connect2(b, mem_ctx,
1605 MAXIMUM_ALLOWED_ACCESS,
1608 if (!NT_STATUS_IS_OK(status)) {
1609 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1613 if (!NT_STATUS_IS_OK(result)) {
1615 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1619 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1621 MAXIMUM_ALLOWED_ACCESS,
1622 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1625 if (!NT_STATUS_IS_OK(status)) {
1626 d_fprintf(stderr, _("Request open_domain failed\n"));
1630 if (!NT_STATUS_IS_OK(result)) {
1632 d_fprintf(stderr, _("Request open_domain failed\n"));
1636 init_lsa_String(&lsa_acct_name, argv[0]);
1638 status = dcerpc_samr_LookupNames(b, mem_ctx,
1645 if (!NT_STATUS_IS_OK(status)) {
1646 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1650 if (!NT_STATUS_IS_OK(result)) {
1652 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1656 switch (name_types.ids[0])
1658 case SID_NAME_DOM_GRP:
1659 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1661 MAXIMUM_ALLOWED_ACCESS,
1665 if (!NT_STATUS_IS_OK(status)) {
1666 d_fprintf(stderr, _("Request open_group failed"));
1670 if (!NT_STATUS_IS_OK(result)) {
1672 d_fprintf(stderr, _("Request open_group failed"));
1676 group_rid = group_rids.ids[0];
1678 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1682 if (!NT_STATUS_IS_OK(status)) {
1684 _("Unable to query group members of %s"),
1689 if (!NT_STATUS_IS_OK(result)) {
1692 _("Unable to query group members of %s"),
1697 if (c->opt_verbose) {
1699 _("Domain Group %s (rid: %d) has %d members\n"),
1700 argv[0],group_rid, rids->count);
1703 /* Check if group is anyone's primary group */
1704 for (i = 0; i < rids->count; i++)
1706 status = dcerpc_samr_OpenUser(b, mem_ctx,
1708 MAXIMUM_ALLOWED_ACCESS,
1712 if (!NT_STATUS_IS_OK(status)) {
1714 _("Unable to open group member %d\n"),
1719 if (!NT_STATUS_IS_OK(result)) {
1722 _("Unable to open group member %d\n"),
1727 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1732 if (!NT_STATUS_IS_OK(status)) {
1734 _("Unable to lookup userinfo for group "
1740 if (!NT_STATUS_IS_OK(result)) {
1743 _("Unable to lookup userinfo for group "
1749 if (info->info21.primary_gid == group_rid) {
1750 if (c->opt_verbose) {
1751 d_printf(_("Group is primary group "
1753 info->info21.account_name.string);
1755 group_is_primary = true;
1758 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1761 if (group_is_primary) {
1762 d_fprintf(stderr, _("Unable to delete group because "
1763 "some of it's members have it as primary "
1765 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1769 /* remove all group members */
1770 for (i = 0; i < rids->count; i++)
1773 d_printf(_("Remove group member %d..."),
1775 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1779 if (!NT_STATUS_IS_OK(status)) {
1783 if (NT_STATUS_IS_OK(result)) {
1785 d_printf(_("ok\n"));
1788 d_printf("%s\n", _("failed"));
1793 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1796 if (!NT_STATUS_IS_OK(status)) {
1803 /* removing a local group is easier... */
1804 case SID_NAME_ALIAS:
1805 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1807 MAXIMUM_ALLOWED_ACCESS,
1811 if (!NT_STATUS_IS_OK(status)) {
1812 d_fprintf(stderr, _("Request open_alias failed\n"));
1815 if (!NT_STATUS_IS_OK(result)) {
1817 d_fprintf(stderr, _("Request open_alias failed\n"));
1821 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1824 if (!NT_STATUS_IS_OK(status)) {
1832 d_fprintf(stderr, _("%s is of type %s. This command is only "
1833 "for deleting local or global groups\n"),
1834 argv[0],sid_type_lookup(name_types.ids[0]));
1835 status = NT_STATUS_UNSUCCESSFUL;
1839 if (NT_STATUS_IS_OK(status)) {
1841 d_printf(_("Deleted %s '%s'\n"),
1842 sid_type_lookup(name_types.ids[0]), argv[0]);
1844 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1845 get_friendly_nt_error_msg(status));
1853 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1855 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1856 rpc_group_delete_internals, argc,argv);
1859 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1861 NET_API_STATUS status;
1862 struct GROUP_INFO_1 info1;
1863 uint32_t parm_error = 0;
1865 if (argc != 1 || c->display_usage) {
1866 rpc_group_usage(c, argc, argv);
1872 info1.grpi1_name = argv[0];
1873 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1874 info1.grpi1_comment = c->opt_comment;
1877 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1881 _("Failed to add group '%s' with error: %s.\n"),
1882 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1886 d_printf(_("Added group '%s'.\n"), argv[0]);
1892 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1894 NET_API_STATUS status;
1895 struct LOCALGROUP_INFO_1 info1;
1896 uint32_t parm_error = 0;
1898 if (argc != 1 || c->display_usage) {
1899 rpc_group_usage(c, argc, argv);
1905 info1.lgrpi1_name = argv[0];
1906 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1907 info1.lgrpi1_comment = c->opt_comment;
1910 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1914 _("Failed to add alias '%s' with error: %s.\n"),
1915 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1919 d_printf(_("Added alias '%s'.\n"), argv[0]);
1925 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1927 if (c->opt_localgroup)
1928 return rpc_alias_add_internals(c, argc, argv);
1930 return rpc_group_add_internals(c, argc, argv);
1933 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1934 TALLOC_CTX *mem_ctx,
1936 struct dom_sid *sid,
1937 enum lsa_SidType *type)
1939 struct dom_sid *sids = NULL;
1940 enum lsa_SidType *types = NULL;
1941 struct rpc_pipe_client *pipe_hnd = NULL;
1942 struct policy_handle lsa_pol;
1943 NTSTATUS status, result;
1944 struct dcerpc_binding_handle *b;
1946 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1948 if (!NT_STATUS_IS_OK(status)) {
1952 b = pipe_hnd->binding_handle;
1954 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1955 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1957 if (!NT_STATUS_IS_OK(status)) {
1961 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1962 &name, NULL, 1, &sids, &types);
1964 if (NT_STATUS_IS_OK(status)) {
1965 sid_copy(sid, &sids[0]);
1969 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1973 TALLOC_FREE(pipe_hnd);
1976 if (!NT_STATUS_IS_OK(status) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1978 /* Try as S-1-5-whatever */
1980 struct dom_sid tmp_sid;
1982 if (string_to_sid(&tmp_sid, name)) {
1983 sid_copy(sid, &tmp_sid);
1984 *type = SID_NAME_UNKNOWN;
1985 status = NT_STATUS_OK;
1992 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1993 TALLOC_CTX *mem_ctx,
1994 const struct dom_sid *group_sid,
1997 struct policy_handle connect_pol, domain_pol;
1998 NTSTATUS status, result;
2000 struct policy_handle group_pol;
2001 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2003 struct samr_Ids rids, rid_types;
2004 struct lsa_String lsa_acct_name;
2008 sid_copy(&sid, group_sid);
2010 if (!sid_split_rid(&sid, &group_rid)) {
2011 return NT_STATUS_UNSUCCESSFUL;
2014 /* Get sam policy handle */
2015 status = dcerpc_samr_Connect2(b, mem_ctx,
2017 MAXIMUM_ALLOWED_ACCESS,
2020 if (!NT_STATUS_IS_OK(status)) {
2023 if (!NT_STATUS_IS_OK(result)) {
2027 /* Get domain policy handle */
2028 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2030 MAXIMUM_ALLOWED_ACCESS,
2034 if (!NT_STATUS_IS_OK(status)) {
2037 if (!NT_STATUS_IS_OK(result)) {
2041 init_lsa_String(&lsa_acct_name, member);
2043 status = dcerpc_samr_LookupNames(b, mem_ctx,
2050 if (!NT_STATUS_IS_OK(status)) {
2051 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2056 if (!NT_STATUS_IS_OK(result)) {
2058 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2063 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2065 MAXIMUM_ALLOWED_ACCESS,
2069 if (!NT_STATUS_IS_OK(status)) {
2073 if (!NT_STATUS_IS_OK(result)) {
2078 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2081 0x0005, /* unknown flags */
2083 if (!NT_STATUS_IS_OK(status)) {
2090 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2094 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2095 TALLOC_CTX *mem_ctx,
2096 const struct dom_sid *alias_sid,
2099 struct policy_handle connect_pol, domain_pol;
2100 NTSTATUS status, result;
2102 struct policy_handle alias_pol;
2103 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2105 struct dom_sid member_sid;
2106 enum lsa_SidType member_type;
2110 sid_copy(&sid, alias_sid);
2112 if (!sid_split_rid(&sid, &alias_rid)) {
2113 return NT_STATUS_UNSUCCESSFUL;
2116 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2117 member, &member_sid, &member_type);
2119 if (!NT_STATUS_IS_OK(result)) {
2120 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2125 /* Get sam policy handle */
2126 status = dcerpc_samr_Connect2(b, mem_ctx,
2128 MAXIMUM_ALLOWED_ACCESS,
2131 if (!NT_STATUS_IS_OK(status)) {
2134 if (!NT_STATUS_IS_OK(result)) {
2139 /* Get domain policy handle */
2140 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2142 MAXIMUM_ALLOWED_ACCESS,
2146 if (!NT_STATUS_IS_OK(status)) {
2149 if (!NT_STATUS_IS_OK(result)) {
2154 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2156 MAXIMUM_ALLOWED_ACCESS,
2160 if (!NT_STATUS_IS_OK(status)) {
2163 if (!NT_STATUS_IS_OK(result)) {
2167 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2171 if (!NT_STATUS_IS_OK(status)) {
2178 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2182 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2183 const struct dom_sid *domain_sid,
2184 const char *domain_name,
2185 struct cli_state *cli,
2186 struct rpc_pipe_client *pipe_hnd,
2187 TALLOC_CTX *mem_ctx,
2191 struct dom_sid group_sid;
2192 enum lsa_SidType group_type;
2194 if (argc != 2 || c->display_usage) {
2197 _("net rpc group addmem <group> <member>\n"
2198 " Add a member to a group\n"
2199 " group\tGroup to add member to\n"
2200 " member\tMember to add to group\n"));
2201 return NT_STATUS_UNSUCCESSFUL;
2204 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2205 &group_sid, &group_type))) {
2206 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2208 return NT_STATUS_UNSUCCESSFUL;
2211 if (group_type == SID_NAME_DOM_GRP) {
2212 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2213 &group_sid, argv[1]);
2215 if (!NT_STATUS_IS_OK(result)) {
2216 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2217 argv[1], argv[0], nt_errstr(result));
2222 if (group_type == SID_NAME_ALIAS) {
2223 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2224 &group_sid, argv[1]);
2226 if (!NT_STATUS_IS_OK(result)) {
2227 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2228 argv[1], argv[0], nt_errstr(result));
2233 d_fprintf(stderr, _("Can only add members to global or local groups "
2234 "which %s is not\n"), argv[0]);
2236 return NT_STATUS_UNSUCCESSFUL;
2239 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2241 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2242 rpc_group_addmem_internals,
2246 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2247 struct rpc_pipe_client *pipe_hnd,
2248 TALLOC_CTX *mem_ctx,
2249 const struct dom_sid *group_sid,
2252 struct policy_handle connect_pol, domain_pol;
2253 NTSTATUS status, result;
2255 struct policy_handle group_pol;
2256 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2258 struct samr_Ids rids, rid_types;
2259 struct lsa_String lsa_acct_name;
2263 sid_copy(&sid, group_sid);
2265 if (!sid_split_rid(&sid, &group_rid))
2266 return NT_STATUS_UNSUCCESSFUL;
2268 /* Get sam policy handle */
2269 status = dcerpc_samr_Connect2(b, mem_ctx,
2271 MAXIMUM_ALLOWED_ACCESS,
2274 if (!NT_STATUS_IS_OK(status)) {
2277 if (!NT_STATUS_IS_OK(result)) {
2282 /* Get domain policy handle */
2283 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2285 MAXIMUM_ALLOWED_ACCESS,
2289 if (!NT_STATUS_IS_OK(status)) {
2292 if (!NT_STATUS_IS_OK(result)) {
2296 init_lsa_String(&lsa_acct_name, member);
2298 status = dcerpc_samr_LookupNames(b, mem_ctx,
2305 if (!NT_STATUS_IS_OK(status)) {
2306 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2311 if (!NT_STATUS_IS_OK(result)) {
2313 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2318 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2320 MAXIMUM_ALLOWED_ACCESS,
2324 if (!NT_STATUS_IS_OK(status)) {
2327 if (!NT_STATUS_IS_OK(result)) {
2332 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2336 if (!NT_STATUS_IS_OK(status)) {
2342 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2346 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2347 TALLOC_CTX *mem_ctx,
2348 const struct dom_sid *alias_sid,
2351 struct policy_handle connect_pol, domain_pol;
2352 NTSTATUS status, result;
2354 struct policy_handle alias_pol;
2355 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2357 struct dom_sid member_sid;
2358 enum lsa_SidType member_type;
2362 sid_copy(&sid, alias_sid);
2364 if (!sid_split_rid(&sid, &alias_rid))
2365 return NT_STATUS_UNSUCCESSFUL;
2367 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2368 member, &member_sid, &member_type);
2370 if (!NT_STATUS_IS_OK(result)) {
2371 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2376 /* Get sam policy handle */
2377 status = dcerpc_samr_Connect2(b, mem_ctx,
2379 MAXIMUM_ALLOWED_ACCESS,
2382 if (!NT_STATUS_IS_OK(status)) {
2385 if (!NT_STATUS_IS_OK(result)) {
2390 /* Get domain policy handle */
2391 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2393 MAXIMUM_ALLOWED_ACCESS,
2397 if (!NT_STATUS_IS_OK(status)) {
2400 if (!NT_STATUS_IS_OK(result)) {
2405 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2407 MAXIMUM_ALLOWED_ACCESS,
2411 if (!NT_STATUS_IS_OK(status)) {
2415 if (!NT_STATUS_IS_OK(result)) {
2419 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2424 if (!NT_STATUS_IS_OK(status)) {
2431 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2435 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2436 const struct dom_sid *domain_sid,
2437 const char *domain_name,
2438 struct cli_state *cli,
2439 struct rpc_pipe_client *pipe_hnd,
2440 TALLOC_CTX *mem_ctx,
2444 struct dom_sid group_sid;
2445 enum lsa_SidType group_type;
2447 if (argc != 2 || c->display_usage) {
2450 _("net rpc group delmem <group> <member>\n"
2451 " Delete a member from a group\n"
2452 " group\tGroup to delete member from\n"
2453 " member\tMember to delete from group\n"));
2454 return NT_STATUS_UNSUCCESSFUL;
2457 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2458 &group_sid, &group_type))) {
2459 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2461 return NT_STATUS_UNSUCCESSFUL;
2464 if (group_type == SID_NAME_DOM_GRP) {
2465 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2466 &group_sid, argv[1]);
2468 if (!NT_STATUS_IS_OK(result)) {
2469 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2470 argv[1], argv[0], nt_errstr(result));
2475 if (group_type == SID_NAME_ALIAS) {
2476 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2477 &group_sid, argv[1]);
2479 if (!NT_STATUS_IS_OK(result)) {
2480 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2481 argv[1], argv[0], nt_errstr(result));
2486 d_fprintf(stderr, _("Can only delete members from global or local "
2487 "groups which %s is not\n"), argv[0]);
2489 return NT_STATUS_UNSUCCESSFUL;
2492 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2494 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2495 rpc_group_delmem_internals,
2500 * List groups on a remote RPC server.
2502 * All parameters are provided by the run_rpc_command function, except for
2503 * argc, argv which are passes through.
2505 * @param domain_sid The domain sid acquired from the remote server.
2506 * @param cli A cli_state connected to the server.
2507 * @param mem_ctx Talloc context, destroyed on completion of the function.
2508 * @param argc Standard main() style argc.
2509 * @param argv Standard main() style argv. Initial components are already
2512 * @return Normal NTSTATUS return.
2515 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2516 const struct dom_sid *domain_sid,
2517 const char *domain_name,
2518 struct cli_state *cli,
2519 struct rpc_pipe_client *pipe_hnd,
2520 TALLOC_CTX *mem_ctx,
2524 struct policy_handle connect_pol, domain_pol;
2525 NTSTATUS status, result;
2526 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2527 struct samr_SamArray *groups = NULL;
2528 bool global = false;
2530 bool builtin = false;
2531 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2533 if (c->display_usage) {
2536 _("net rpc group list [global] [local] [builtin]\n"
2537 " List groups on RPC server\n"
2538 " global\tList global groups\n"
2539 " local\tList local groups\n"
2540 " builtin\tList builtin groups\n"
2541 " If none of global, local or builtin is "
2542 "specified, all three options are considered "
2544 return NT_STATUS_OK;
2553 for (i=0; i<argc; i++) {
2554 if (strequal(argv[i], "global"))
2557 if (strequal(argv[i], "local"))
2560 if (strequal(argv[i], "builtin"))
2564 /* Get sam policy handle */
2566 status = dcerpc_samr_Connect2(b, mem_ctx,
2568 MAXIMUM_ALLOWED_ACCESS,
2571 if (!NT_STATUS_IS_OK(status)) {
2574 if (!NT_STATUS_IS_OK(result)) {
2579 /* Get domain policy handle */
2581 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2583 MAXIMUM_ALLOWED_ACCESS,
2584 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2587 if (!NT_STATUS_IS_OK(status)) {
2590 if (!NT_STATUS_IS_OK(result)) {
2595 /* Query domain groups */
2596 if (c->opt_long_list_entries)
2597 d_printf(_("\nGroup name Comment"
2598 "\n-----------------------------\n"));
2600 uint32_t max_size, total_size, returned_size;
2601 union samr_DispInfo info;
2605 dcerpc_get_query_dispinfo_params(
2606 loop_count, &max_entries, &max_size);
2608 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2618 if (!NT_STATUS_IS_OK(status)) {
2621 num_entries = info.info3.count;
2622 start_idx += info.info3.count;
2624 if (!NT_STATUS_IS_OK(result) &&
2625 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2628 for (i = 0; i < num_entries; i++) {
2630 const char *group = NULL;
2631 const char *desc = NULL;
2633 group = info.info3.entries[i].account_name.string;
2634 desc = info.info3.entries[i].description.string;
2636 if (c->opt_long_list_entries)
2637 printf("%-21.21s %-50.50s\n",
2640 printf("%s\n", group);
2642 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2643 /* query domain aliases */
2648 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2655 if (!NT_STATUS_IS_OK(status)) {
2658 if (!NT_STATUS_IS_OK(result) &&
2659 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2662 for (i = 0; i < num_entries; i++) {
2664 const char *description = NULL;
2666 if (c->opt_long_list_entries) {
2668 struct policy_handle alias_pol;
2669 union samr_AliasInfo *info = NULL;
2672 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2675 groups->entries[i].idx,
2678 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2679 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2684 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2685 status = dcerpc_samr_Close(b, mem_ctx,
2688 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2689 description = info->description.string;
2695 if (description != NULL) {
2696 printf("%-21.21s %-50.50s\n",
2697 groups->entries[i].name.string,
2700 printf("%s\n", groups->entries[i].name.string);
2703 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2704 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2705 /* Get builtin policy handle */
2707 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2709 MAXIMUM_ALLOWED_ACCESS,
2710 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2713 if (!NT_STATUS_IS_OK(status)) {
2716 if (!NT_STATUS_IS_OK(result)) {
2721 /* query builtin aliases */
2724 if (!builtin) break;
2726 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2733 if (!NT_STATUS_IS_OK(status)) {
2736 if (!NT_STATUS_IS_OK(result) &&
2737 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2742 for (i = 0; i < num_entries; i++) {
2744 const char *description = NULL;
2746 if (c->opt_long_list_entries) {
2748 struct policy_handle alias_pol;
2749 union samr_AliasInfo *info = NULL;
2752 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2755 groups->entries[i].idx,
2758 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2759 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2764 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2765 status = dcerpc_samr_Close(b, mem_ctx,
2768 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2769 description = info->description.string;
2775 if (description != NULL) {
2776 printf("%-21.21s %-50.50s\n",
2777 groups->entries[i].name.string,
2780 printf("%s\n", groups->entries[i].name.string);
2783 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2791 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2793 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2794 rpc_group_list_internals,
2798 static NTSTATUS rpc_list_group_members(struct net_context *c,
2799 struct rpc_pipe_client *pipe_hnd,
2800 TALLOC_CTX *mem_ctx,
2801 const char *domain_name,
2802 const struct dom_sid *domain_sid,
2803 struct policy_handle *domain_pol,
2806 NTSTATUS result, status;
2807 struct policy_handle group_pol;
2808 uint32 num_members, *group_rids;
2810 struct samr_RidAttrArray *rids = NULL;
2811 struct lsa_Strings names;
2812 struct samr_Ids types;
2813 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2816 sid_to_fstring(sid_str, domain_sid);
2818 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2820 MAXIMUM_ALLOWED_ACCESS,
2824 if (!NT_STATUS_IS_OK(status)) {
2827 if (!NT_STATUS_IS_OK(result)) {
2831 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
2835 if (!NT_STATUS_IS_OK(status)) {
2838 if (!NT_STATUS_IS_OK(result)) {
2842 num_members = rids->count;
2843 group_rids = rids->rids;
2845 while (num_members > 0) {
2846 int this_time = 512;
2848 if (num_members < this_time)
2849 this_time = num_members;
2851 status = dcerpc_samr_LookupRids(b, mem_ctx,
2858 if (!NT_STATUS_IS_OK(status)) {
2861 if (!NT_STATUS_IS_OK(result)) {
2865 /* We only have users as members, but make the output
2866 the same as the output of alias members */
2868 for (i = 0; i < this_time; i++) {
2870 if (c->opt_long_list_entries) {
2871 printf("%s-%d %s\\%s %d\n", sid_str,
2872 group_rids[i], domain_name,
2873 names.names[i].string,
2876 printf("%s\\%s\n", domain_name,
2877 names.names[i].string);
2881 num_members -= this_time;
2885 return NT_STATUS_OK;
2888 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2889 struct rpc_pipe_client *pipe_hnd,
2890 TALLOC_CTX *mem_ctx,
2891 struct policy_handle *domain_pol,
2894 NTSTATUS result, status;
2895 struct rpc_pipe_client *lsa_pipe;
2896 struct policy_handle alias_pol, lsa_pol;
2898 struct dom_sid *alias_sids;
2901 enum lsa_SidType *types;
2903 struct lsa_SidArray sid_array;
2904 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2906 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2908 MAXIMUM_ALLOWED_ACCESS,
2912 if (!NT_STATUS_IS_OK(status)) {
2915 if (!NT_STATUS_IS_OK(result)) {
2919 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
2923 if (!NT_STATUS_IS_OK(status)) {
2924 d_fprintf(stderr, _("Couldn't list alias members\n"));
2927 if (!NT_STATUS_IS_OK(result)) {
2928 d_fprintf(stderr, _("Couldn't list alias members\n"));
2932 num_members = sid_array.num_sids;
2934 if (num_members == 0) {
2935 return NT_STATUS_OK;
2938 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2939 &ndr_table_lsarpc.syntax_id,
2941 if (!NT_STATUS_IS_OK(result)) {
2942 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2943 nt_errstr(result) );
2947 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2948 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2950 if (!NT_STATUS_IS_OK(result)) {
2951 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2952 TALLOC_FREE(lsa_pipe);
2956 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2958 d_fprintf(stderr, _("Out of memory\n"));
2959 TALLOC_FREE(lsa_pipe);
2960 return NT_STATUS_NO_MEMORY;
2963 for (i=0; i<num_members; i++) {
2964 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2967 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2968 num_members, alias_sids,
2969 &domains, &names, &types);
2971 if (!NT_STATUS_IS_OK(result) &&
2972 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2973 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2974 TALLOC_FREE(lsa_pipe);
2978 for (i = 0; i < num_members; i++) {
2980 sid_to_fstring(sid_str, &alias_sids[i]);
2982 if (c->opt_long_list_entries) {
2983 printf("%s %s\\%s %d\n", sid_str,
2984 domains[i] ? domains[i] : _("*unknown*"),
2985 names[i] ? names[i] : _("*unknown*"), types[i]);
2988 printf("%s\\%s\n", domains[i], names[i]);
2990 printf("%s\n", sid_str);
2994 TALLOC_FREE(lsa_pipe);
2995 return NT_STATUS_OK;
2998 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2999 const struct dom_sid *domain_sid,
3000 const char *domain_name,
3001 struct cli_state *cli,
3002 struct rpc_pipe_client *pipe_hnd,
3003 TALLOC_CTX *mem_ctx,
3007 NTSTATUS result, status;
3008 struct policy_handle connect_pol, domain_pol;
3009 struct samr_Ids rids, rid_types;
3010 struct lsa_String lsa_acct_name;
3011 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3013 /* Get sam policy handle */
3015 status = dcerpc_samr_Connect2(b, mem_ctx,
3017 MAXIMUM_ALLOWED_ACCESS,
3020 if (!NT_STATUS_IS_OK(status)) {
3023 if (!NT_STATUS_IS_OK(result)) {
3027 /* Get domain policy handle */
3029 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3031 MAXIMUM_ALLOWED_ACCESS,
3032 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3035 if (!NT_STATUS_IS_OK(status)) {
3038 if (!NT_STATUS_IS_OK(result)) {
3042 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3044 status = dcerpc_samr_LookupNames(b, mem_ctx,
3051 if (!NT_STATUS_IS_OK(status)) {
3055 if (!NT_STATUS_IS_OK(result)) {
3057 /* Ok, did not find it in the global sam, try with builtin */
3059 struct dom_sid sid_Builtin;
3061 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3063 sid_copy(&sid_Builtin, &global_sid_Builtin);
3065 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3067 MAXIMUM_ALLOWED_ACCESS,
3071 if (!NT_STATUS_IS_OK(status)) {
3074 if (!NT_STATUS_IS_OK(result)) {
3075 d_fprintf(stderr, _("Couldn't find group %s\n"),
3080 status = dcerpc_samr_LookupNames(b, mem_ctx,
3087 if (!NT_STATUS_IS_OK(status)) {
3090 if (!NT_STATUS_IS_OK(result)) {
3091 d_fprintf(stderr, _("Couldn't find group %s\n"),
3097 if (rids.count != 1) {
3098 d_fprintf(stderr, _("Couldn't find group %s\n"),
3103 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3104 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3105 domain_sid, &domain_pol,
3109 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3110 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3114 return NT_STATUS_NO_SUCH_GROUP;
3117 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3119 if (argc != 1 || c->display_usage) {
3120 return rpc_group_usage(c, argc, argv);
3123 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3124 rpc_group_members_internals,
3128 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3130 NET_API_STATUS status;
3131 struct GROUP_INFO_0 g0;
3135 d_printf(_("Usage:\n"));
3136 d_printf("net rpc group rename group newname\n");
3140 g0.grpi0_name = argv[1];
3142 status = NetGroupSetInfo(c->opt_host,
3149 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3150 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3158 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3160 if (argc != 2 || c->display_usage) {
3161 return rpc_group_usage(c, argc, argv);
3164 return rpc_group_rename_internals(c, argc, argv);
3168 * 'net rpc group' entrypoint.
3169 * @param argc Standard main() style argc.
3170 * @param argv Standard main() style argv. Initial components are already
3174 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3176 NET_API_STATUS status;
3178 struct functable func[] = {
3183 N_("Create specified group"),
3184 N_("net rpc group add\n"
3185 " Create specified group")
3191 N_("Delete specified group"),
3192 N_("net rpc group delete\n"
3193 " Delete specified group")
3199 N_("Add member to group"),
3200 N_("net rpc group addmem\n"
3201 " Add member to group")
3207 N_("Remove member from group"),
3208 N_("net rpc group delmem\n"
3209 " Remove member from group")
3216 N_("net rpc group list\n"
3223 N_("List group members"),
3224 N_("net rpc group members\n"
3225 " List group members")
3232 N_("net rpc group rename\n"
3235 {NULL, NULL, 0, NULL, NULL}
3238 status = libnetapi_net_init(&c->netapi_ctx);
3242 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3243 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3244 if (c->opt_kerberos) {
3245 libnetapi_set_use_kerberos(c->netapi_ctx);
3249 if (c->display_usage) {
3250 d_printf(_("Usage:\n"));
3251 d_printf(_("net rpc group\n"
3252 " Alias for net rpc group list global "
3253 "local builtin\n"));
3254 net_display_usage_from_functable(func);
3258 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3259 rpc_group_list_internals,
3263 return net_run_function(c, argc, argv, "net rpc group", func);
3266 /****************************************************************************/
3268 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3270 return net_share_usage(c, argc, argv);
3274 * Add a share on a remote RPC server.
3276 * @param argc Standard main() style argc.
3277 * @param argv Standard main() style argv. Initial components are already
3280 * @return A shell status integer (0 for success).
3283 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3285 NET_API_STATUS status;
3288 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3289 uint32 num_users=0, perms=0;
3290 char *password=NULL; /* don't allow a share password */
3291 struct SHARE_INFO_2 i2;
3292 uint32_t parm_error = 0;
3294 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3295 return rpc_share_usage(c, argc, argv);
3298 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3302 path = strchr(sharename, '=');
3309 i2.shi2_netname = sharename;
3310 i2.shi2_type = type;
3311 i2.shi2_remark = c->opt_comment;
3312 i2.shi2_permissions = perms;
3313 i2.shi2_max_uses = c->opt_maxusers;
3314 i2.shi2_current_uses = num_users;
3315 i2.shi2_path = path;
3316 i2.shi2_passwd = password;
3318 status = NetShareAdd(c->opt_host,
3323 printf(_("NetShareAdd failed with: %s\n"),
3324 libnetapi_get_error_string(c->netapi_ctx, status));
3331 * Delete a share on a remote RPC server.
3333 * @param domain_sid The domain sid acquired from the remote server.
3334 * @param argc Standard main() style argc.
3335 * @param argv Standard main() style argv. Initial components are already
3338 * @return A shell status integer (0 for success).
3340 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3342 if (argc < 1 || c->display_usage) {
3343 return rpc_share_usage(c, argc, argv);
3346 return NetShareDel(c->opt_host, argv[0], 0);
3350 * Formatted print of share info
3352 * @param r pointer to SHARE_INFO_1 to format
3355 static void display_share_info_1(struct net_context *c,
3356 struct SHARE_INFO_1 *r)
3358 if (c->opt_long_list_entries) {
3359 d_printf("%-12s %-8.8s %-50s\n",
3361 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3364 d_printf("%s\n", r->shi1_netname);
3368 static WERROR get_share_info(struct net_context *c,
3369 struct rpc_pipe_client *pipe_hnd,
3370 TALLOC_CTX *mem_ctx,
3374 struct srvsvc_NetShareInfoCtr *info_ctr)
3378 union srvsvc_NetShareInfo info;
3379 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3381 /* no specific share requested, enumerate all */
3384 uint32_t preferred_len = 0xffffffff;
3385 uint32_t total_entries = 0;
3386 uint32_t resume_handle = 0;
3388 info_ctr->level = level;
3390 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3397 if (!NT_STATUS_IS_OK(status)) {
3398 return ntstatus_to_werror(status);
3403 /* request just one share */
3404 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3411 if (!NT_STATUS_IS_OK(status)) {
3412 result = ntstatus_to_werror(status);
3416 if (!W_ERROR_IS_OK(result)) {
3421 ZERO_STRUCTP(info_ctr);
3423 info_ctr->level = level;
3428 struct srvsvc_NetShareCtr1 *ctr1;
3430 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3431 W_ERROR_HAVE_NO_MEMORY(ctr1);
3434 ctr1->array = info.info1;
3436 info_ctr->ctr.ctr1 = ctr1;
3442 struct srvsvc_NetShareCtr2 *ctr2;
3444 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3445 W_ERROR_HAVE_NO_MEMORY(ctr2);
3448 ctr2->array = info.info2;
3450 info_ctr->ctr.ctr2 = ctr2;
3456 struct srvsvc_NetShareCtr502 *ctr502;
3458 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3459 W_ERROR_HAVE_NO_MEMORY(ctr502);
3462 ctr502->array = info.info502;
3464 info_ctr->ctr.ctr502 = ctr502;
3474 * 'net rpc share list' entrypoint.
3475 * @param argc Standard main() style argc.
3476 * @param argv Standard main() style argv. Initial components are already
3479 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3481 NET_API_STATUS status;
3482 struct SHARE_INFO_1 *i1 = NULL;
3483 uint32_t entries_read = 0;
3484 uint32_t total_entries = 0;
3485 uint32_t resume_handle = 0;
3486 uint32_t i, level = 1;
3488 if (c->display_usage) {
3490 "net rpc share list\n"
3493 _("List shares on remote server"));
3497 status = NetShareEnum(c->opt_host,
3499 (uint8_t **)(void *)&i1,
3508 /* Display results */
3510 if (c->opt_long_list_entries) {
3512 "\nEnumerating shared resources (exports) on remote server:\n\n"
3513 "\nShare name Type Description\n"
3514 "---------- ---- -----------\n"));
3516 for (i = 0; i < entries_read; i++)
3517 display_share_info_1(c, &i1[i]);
3522 static bool check_share_availability(struct cli_state *cli, const char *netname)
3526 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3527 if (!NT_STATUS_IS_OK(status)) {
3528 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3532 status = cli_tdis(cli);
3533 if (!NT_STATUS_IS_OK(status)) {
3534 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3541 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3542 const char *netname, uint32 type)
3544 /* only support disk shares */
3545 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3546 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3551 /* skip builtin shares */
3552 /* FIXME: should print$ be added too ? */
3553 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3554 strequal(netname,"global"))
3557 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3558 printf(_("excluding [%s]\n"), netname);
3562 return check_share_availability(cli, netname);
3566 * Migrate shares from a remote RPC server to the local RPC server.
3568 * All parameters are provided by the run_rpc_command function, except for
3569 * argc, argv which are passed through.
3571 * @param domain_sid The domain sid acquired from the remote server.
3572 * @param cli A cli_state connected to the server.
3573 * @param mem_ctx Talloc context, destroyed on completion of the function.
3574 * @param argc Standard main() style argc.
3575 * @param argv Standard main() style argv. Initial components are already
3578 * @return Normal NTSTATUS return.
3581 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3582 const struct dom_sid *domain_sid,
3583 const char *domain_name,
3584 struct cli_state *cli,
3585 struct rpc_pipe_client *pipe_hnd,
3586 TALLOC_CTX *mem_ctx,
3591 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3592 struct srvsvc_NetShareInfoCtr ctr_src;
3594 struct rpc_pipe_client *srvsvc_pipe = NULL;
3595 struct cli_state *cli_dst = NULL;
3596 uint32 level = 502; /* includes secdesc */
3597 uint32_t parm_error = 0;
3598 struct dcerpc_binding_handle *b;
3600 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3602 if (!W_ERROR_IS_OK(result))
3605 /* connect destination PI_SRVSVC */
3606 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3607 &ndr_table_srvsvc.syntax_id);
3608 if (!NT_STATUS_IS_OK(nt_status))
3611 b = srvsvc_pipe->binding_handle;
3613 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3615 union srvsvc_NetShareInfo info;
3616 struct srvsvc_NetShareInfo502 info502 =
3617 ctr_src.ctr.ctr502->array[i];
3619 /* reset error-code */
3620 nt_status = NT_STATUS_UNSUCCESSFUL;
3622 if (!check_share_sanity(c, cli, info502.name, info502.type))
3625 /* finally add the share on the dst server */
3627 printf(_("migrating: [%s], path: %s, comment: %s, without "
3629 info502.name, info502.path, info502.comment);
3631 info.info502 = &info502;
3633 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3634 srvsvc_pipe->desthost,
3639 if (!NT_STATUS_IS_OK(nt_status)) {
3640 printf(_("cannot add share: %s\n"),
3641 nt_errstr(nt_status));
3644 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3645 printf(_(" [%s] does already exist\n"),
3650 if (!W_ERROR_IS_OK(result)) {
3651 nt_status = werror_to_ntstatus(result);
3652 printf(_("cannot add share: %s\n"),
3653 win_errstr(result));
3659 nt_status = NT_STATUS_OK;
3663 cli_shutdown(cli_dst);
3671 * Migrate shares from a RPC server to another.
3673 * @param argc Standard main() style argc.
3674 * @param argv Standard main() style argv. Initial components are already
3677 * @return A shell status integer (0 for success).
3679 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3682 if (c->display_usage) {
3684 "net rpc share migrate shares\n"
3687 _("Migrate shares to local server"));
3692 printf(_("no server to migrate\n"));
3696 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3697 rpc_share_migrate_shares_internals,
3704 * @param f file_info
3705 * @param mask current search mask
3706 * @param state arg-pointer
3709 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3710 const char *mask, void *state)
3712 static NTSTATUS nt_status;
3713 static struct copy_clistate *local_state;
3714 static fstring filename, new_mask;
3717 struct net_context *c;
3719 local_state = (struct copy_clistate *)state;
3720 nt_status = NT_STATUS_UNSUCCESSFUL;
3724 if (strequal(f->name, ".") || strequal(f->name, ".."))
3725 return NT_STATUS_OK;
3727 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3730 if (f->mode & aDIR) {
3732 DEBUG(3,("got dir: %s\n", f->name));
3734 fstrcpy(dir, local_state->cwd);
3736 fstrcat(dir, f->name);
3738 switch (net_mode_share)
3740 case NET_MODE_SHARE_MIGRATE:
3741 /* create that directory */
3742 nt_status = net_copy_file(c, local_state->mem_ctx,
3743 local_state->cli_share_src,
3744 local_state->cli_share_dst,
3746 c->opt_acls? true : false,
3747 c->opt_attrs? true : false,
3748 c->opt_timestamps? true:false,
3752 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3753 return NT_STATUS_INTERNAL_ERROR;
3756 if (!NT_STATUS_IS_OK(nt_status)) {
3757 printf(_("could not handle dir %s: %s\n"),
3758 dir, nt_errstr(nt_status));
3762 /* search below that directory */
3763 fstrcpy(new_mask, dir);
3764 fstrcat(new_mask, "\\*");
3766 old_dir = local_state->cwd;
3767 local_state->cwd = dir;
3768 nt_status = sync_files(local_state, new_mask);
3769 if (!NT_STATUS_IS_OK(nt_status)) {
3770 printf(_("could not handle files\n"));
3772 local_state->cwd = old_dir;
3779 fstrcpy(filename, local_state->cwd);
3780 fstrcat(filename, "\\");
3781 fstrcat(filename, f->name);
3783 DEBUG(3,("got file: %s\n", filename));
3785 switch (net_mode_share)
3787 case NET_MODE_SHARE_MIGRATE:
3788 nt_status = net_copy_file(c, local_state->mem_ctx,
3789 local_state->cli_share_src,
3790 local_state->cli_share_dst,
3792 c->opt_acls? true : false,
3793 c->opt_attrs? true : false,
3794 c->opt_timestamps? true: false,
3798 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3800 return NT_STATUS_INTERNAL_ERROR;
3803 if (!NT_STATUS_IS_OK(nt_status))
3804 printf(_("could not handle file %s: %s\n"),
3805 filename, nt_errstr(nt_status));
3810 * sync files, can be called recursivly to list files
3811 * and then call copy_fn for each file
3813 * @param cp_clistate pointer to the copy_clistate we work with
3814 * @param mask the current search mask
3816 * @return Boolean result
3818 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3820 struct cli_state *targetcli;
3821 char *targetpath = NULL;
3824 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3826 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3827 mask, &targetcli, &targetpath ) ) {
3828 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3830 mask, cli_errstr(cp_clistate->cli_share_src));
3831 return cli_nt_error(cp_clistate->cli_share_src);
3834 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3835 copy_fn, cp_clistate);
3836 if (!NT_STATUS_IS_OK(status)) {
3837 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3838 mask, nt_errstr(status));
3846 * Set the top level directory permissions before we do any further copies.
3847 * Should set up ACL inheritance.
3850 bool copy_top_level_perms(struct net_context *c,
3851 struct copy_clistate *cp_clistate,
3852 const char *sharename)
3854 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3856 switch (net_mode_share) {
3857 case NET_MODE_SHARE_MIGRATE:
3858 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3859 nt_status = net_copy_fileattr(c,
3860 cp_clistate->mem_ctx,
3861 cp_clistate->cli_share_src,
3862 cp_clistate->cli_share_dst,
3864 c->opt_acls? true : false,
3865 c->opt_attrs? true : false,
3866 c->opt_timestamps? true: false,
3870 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3874 if (!NT_STATUS_IS_OK(nt_status)) {
3875 printf(_("Could handle directory attributes for top level "
3876 "directory of share %s. Error %s\n"),
3877 sharename, nt_errstr(nt_status));
3885 * Sync all files inside a remote share to another share (over smb).
3887 * All parameters are provided by the run_rpc_command function, except for
3888 * argc, argv which are passed through.
3890 * @param domain_sid The domain sid acquired from the remote server.
3891 * @param cli A cli_state connected to the server.
3892 * @param mem_ctx Talloc context, destroyed on completion of the function.
3893 * @param argc Standard main() style argc.
3894 * @param argv Standard main() style argv. Initial components are already
3897 * @return Normal NTSTATUS return.
3900 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3901 const struct dom_sid *domain_sid,
3902 const char *domain_name,
3903 struct cli_state *cli,
3904 struct rpc_pipe_client *pipe_hnd,
3905 TALLOC_CTX *mem_ctx,
3910 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3911 struct srvsvc_NetShareInfoCtr ctr_src;
3914 struct copy_clistate cp_clistate;
3915 bool got_src_share = false;
3916 bool got_dst_share = false;
3917 const char *mask = "\\*";
3920 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3922 nt_status = NT_STATUS_NO_MEMORY;
3926 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3929 if (!W_ERROR_IS_OK(result))
3932 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3934 struct srvsvc_NetShareInfo502 info502 =
3935 ctr_src.ctr.ctr502->array[i];
3937 if (!check_share_sanity(c, cli, info502.name, info502.type))
3940 /* one might not want to mirror whole discs :) */
3941 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3942 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3947 switch (net_mode_share)
3949 case NET_MODE_SHARE_MIGRATE:
3953 d_fprintf(stderr, _("Unsupported mode %d\n"),
3957 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3960 c->opt_acls ? _("including") : _("without"),
3961 c->opt_attrs ? _("including") : _("without"),
3962 c->opt_timestamps ? _("(preserving timestamps)") : "");
3964 cp_clistate.mem_ctx = mem_ctx;
3965 cp_clistate.cli_share_src = NULL;
3966 cp_clistate.cli_share_dst = NULL;
3967 cp_clistate.cwd = NULL;
3968 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3971 /* open share source */
3972 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3973 &cli->dest_ss, cli->desthost,
3974 info502.name, "A:");
3975 if (!NT_STATUS_IS_OK(nt_status))
3978 got_src_share = true;
3980 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3981 /* open share destination */
3982 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3983 NULL, dst, info502.name, "A:");
3984 if (!NT_STATUS_IS_OK(nt_status))
3987 got_dst_share = true;
3990 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3991 d_fprintf(stderr, _("Could not handle the top level "
3992 "directory permissions for the "
3993 "share: %s\n"), info502.name);
3994 nt_status = NT_STATUS_UNSUCCESSFUL;
3998 nt_status = sync_files(&cp_clistate, mask);
3999 if (!NT_STATUS_IS_OK(nt_status)) {
4000 d_fprintf(stderr, _("could not handle files for share: "
4001 "%s\n"), info502.name);
4006 nt_status = NT_STATUS_OK;
4011 cli_shutdown(cp_clistate.cli_share_src);
4014 cli_shutdown(cp_clistate.cli_share_dst);
4021 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4023 if (c->display_usage) {
4025 "net share migrate files\n"
4028 _("Migrate files to local server"));
4033 d_printf(_("no server to migrate\n"));
4037 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4038 rpc_share_migrate_files_internals,
4043 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4045 * All parameters are provided by the run_rpc_command function, except for
4046 * argc, argv which are passed through.
4048 * @param domain_sid The domain sid acquired from the remote server.
4049 * @param cli A cli_state connected to the server.
4050 * @param mem_ctx Talloc context, destroyed on completion of the function.
4051 * @param argc Standard main() style argc.
4052 * @param argv Standard main() style argv. Initial components are already
4055 * @return Normal NTSTATUS return.
4058 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4059 const struct dom_sid *domain_sid,
4060 const char *domain_name,
4061 struct cli_state *cli,
4062 struct rpc_pipe_client *pipe_hnd,
4063 TALLOC_CTX *mem_ctx,
4068 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4069 struct srvsvc_NetShareInfoCtr ctr_src;
4070 union srvsvc_NetShareInfo info;
4072 struct rpc_pipe_client *srvsvc_pipe = NULL;
4073 struct cli_state *cli_dst = NULL;
4074 uint32 level = 502; /* includes secdesc */
4075 uint32_t parm_error = 0;
4076 struct dcerpc_binding_handle *b;
4078 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4081 if (!W_ERROR_IS_OK(result))
4084 /* connect destination PI_SRVSVC */
4085 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4086 &ndr_table_srvsvc.syntax_id);
4087 if (!NT_STATUS_IS_OK(nt_status))
4090 b = srvsvc_pipe->binding_handle;
4092 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4094 struct srvsvc_NetShareInfo502 info502 =
4095 ctr_src.ctr.ctr502->array[i];
4097 /* reset error-code */
4098 nt_status = NT_STATUS_UNSUCCESSFUL;
4100 if (!check_share_sanity(c, cli, info502.name, info502.type))
4103 printf(_("migrating: [%s], path: %s, comment: %s, including "
4105 info502.name, info502.path, info502.comment);
4108 display_sec_desc(info502.sd_buf.sd);
4110 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4111 info.info502 = &info502;
4113 /* finally modify the share on the dst server */
4114 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4115 srvsvc_pipe->desthost,
4121 if (!NT_STATUS_IS_OK(nt_status)) {
4122 printf(_("cannot set share-acl: %s\n"),
4123 nt_errstr(nt_status));
4126 if (!W_ERROR_IS_OK(result)) {
4127 nt_status = werror_to_ntstatus(result);
4128 printf(_("cannot set share-acl: %s\n"),
4129 win_errstr(result));
4135 nt_status = NT_STATUS_OK;
4139 cli_shutdown(cli_dst);
4147 * Migrate share-acls from a RPC server to another.
4149 * @param argc Standard main() style argc.
4150 * @param argv Standard main() style argv. Initial components are already
4153 * @return A shell status integer (0 for success).
4155 static int rpc_share_migrate_security(struct net_context *c, int argc,
4158 if (c->display_usage) {
4160 "net rpc share migrate security\n"
4163 _("Migrate share-acls to local server"));
4168 d_printf(_("no server to migrate\n"));
4172 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4173 rpc_share_migrate_security_internals,
4178 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4179 * from one server to another.
4181 * @param argc Standard main() style argc.
4182 * @param argv Standard main() style argv. Initial components are already
4185 * @return A shell status integer (0 for success).
4188 static int rpc_share_migrate_all(struct net_context *c, int argc,
4193 if (c->display_usage) {
4195 "net rpc share migrate all\n"
4198 _("Migrates shares including all share settings"));
4203 d_printf(_("no server to migrate\n"));
4207 /* order is important. we don't want to be locked out by the share-acl
4208 * before copying files - gd */
4210 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4211 rpc_share_migrate_shares_internals, argc, argv);
4215 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4216 rpc_share_migrate_files_internals, argc, argv);
4220 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4221 rpc_share_migrate_security_internals, argc,
4227 * 'net rpc share migrate' entrypoint.
4228 * @param argc Standard main() style argc.
4229 * @param argv Standard main() style argv. Initial components are already
4232 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4235 struct functable func[] = {
4238 rpc_share_migrate_all,
4240 N_("Migrate shares from remote to local server"),
4241 N_("net rpc share migrate all\n"
4242 " Migrate shares from remote to local server")
4246 rpc_share_migrate_files,
4248 N_("Migrate files from remote to local server"),
4249 N_("net rpc share migrate files\n"
4250 " Migrate files from remote to local server")
4254 rpc_share_migrate_security,
4256 N_("Migrate share-ACLs from remote to local server"),
4257 N_("net rpc share migrate security\n"
4258 " Migrate share-ACLs from remote to local server")
4262 rpc_share_migrate_shares,
4264 N_("Migrate shares from remote to local server"),
4265 N_("net rpc share migrate shares\n"
4266 " Migrate shares from remote to local server")
4268 {NULL, NULL, 0, NULL, NULL}
4271 net_mode_share = NET_MODE_SHARE_MIGRATE;
4273 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4279 struct dom_sid *members;
4282 static int num_server_aliases;
4283 static struct full_alias *server_aliases;
4286 * Add an alias to the static list.
4288 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4290 if (server_aliases == NULL)
4291 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4293 server_aliases[num_server_aliases] = *alias;
4294 num_server_aliases += 1;
4298 * For a specific domain on the server, fetch all the aliases
4299 * and their members. Add all of them to the server_aliases.
4302 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4303 TALLOC_CTX *mem_ctx,
4304 struct policy_handle *connect_pol,
4305 const struct dom_sid *domain_sid)
4307 uint32 start_idx, max_entries, num_entries, i;
4308 struct samr_SamArray *groups = NULL;
4309 NTSTATUS result, status;
4310 struct policy_handle domain_pol;
4311 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4313 /* Get domain policy handle */
4315 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4317 MAXIMUM_ALLOWED_ACCESS,
4318 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4321 if (!NT_STATUS_IS_OK(status)) {
4324 if (!NT_STATUS_IS_OK(result)) {
4332 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4339 if (!NT_STATUS_IS_OK(status)) {
4342 for (i = 0; i < num_entries; i++) {
4344 struct policy_handle alias_pol;
4345 struct full_alias alias;
4346 struct lsa_SidArray sid_array;
4350 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4352 MAXIMUM_ALLOWED_ACCESS,
4353 groups->entries[i].idx,
4356 if (!NT_STATUS_IS_OK(status)) {
4359 if (!NT_STATUS_IS_OK(_result)) {
4364 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4368 if (!NT_STATUS_IS_OK(status)) {
4371 if (!NT_STATUS_IS_OK(_result)) {
4376 alias.num_members = sid_array.num_sids;
4378 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4379 if (!NT_STATUS_IS_OK(status)) {
4382 if (!NT_STATUS_IS_OK(_result)) {
4387 alias.members = NULL;
4389 if (alias.num_members > 0) {
4390 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4392 for (j = 0; j < alias.num_members; j++)
4393 sid_copy(&alias.members[j],
4394 sid_array.sids[j].sid);
4397 sid_compose(&alias.sid, domain_sid,
4398 groups->entries[i].idx);
4400 push_alias(mem_ctx, &alias);
4402 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4404 status = NT_STATUS_OK;
4407 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4413 * Dump server_aliases as names for debugging purposes.
4416 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4417 const struct dom_sid *domain_sid,
4418 const char *domain_name,
4419 struct cli_state *cli,
4420 struct rpc_pipe_client *pipe_hnd,
4421 TALLOC_CTX *mem_ctx,
4427 struct policy_handle lsa_pol;
4428 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4430 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4431 SEC_FLAG_MAXIMUM_ALLOWED,
4433 if (!NT_STATUS_IS_OK(result))
4436 for (i=0; i<num_server_aliases; i++) {
4439 enum lsa_SidType *types;
4442 struct full_alias *alias = &server_aliases[i];
4444 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4446 &domains, &names, &types);
4447 if (!NT_STATUS_IS_OK(result))
4450 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4452 if (alias->num_members == 0) {
4457 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4460 &domains, &names, &types);
4462 if (!NT_STATUS_IS_OK(result) &&
4463 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4466 for (j=0; j<alias->num_members; j++)
4467 DEBUG(1, ("%s\\%s (%d); ",
4468 domains[j] ? domains[j] : "*unknown*",
4469 names[j] ? names[j] : "*unknown*",types[j]));
4473 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4475 return NT_STATUS_OK;
4479 * Fetch a list of all server aliases and their members into
4483 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4484 const struct dom_sid *domain_sid,
4485 const char *domain_name,
4486 struct cli_state *cli,
4487 struct rpc_pipe_client *pipe_hnd,
4488 TALLOC_CTX *mem_ctx,
4492 NTSTATUS result, status;
4493 struct policy_handle connect_pol;
4494 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4496 status = dcerpc_samr_Connect2(b, mem_ctx,
4498 MAXIMUM_ALLOWED_ACCESS,
4501 if (!NT_STATUS_IS_OK(status)) {
4504 if (!NT_STATUS_IS_OK(result)) {
4509 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4510 &global_sid_Builtin);
4511 if (!NT_STATUS_IS_OK(status)) {
4515 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4518 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4523 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4525 token->num_sids = 4;
4527 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4528 d_fprintf(stderr, "malloc %s\n",_("failed"));
4529 token->num_sids = 0;
4533 token->sids[0] = *user_sid;
4534 sid_copy(&token->sids[1], &global_sid_World);
4535 sid_copy(&token->sids[2], &global_sid_Network);
4536 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4539 static void free_user_token(struct security_token *token)
4541 SAFE_FREE(token->sids);
4544 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4546 if (security_token_has_sid(token, sid))
4549 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4554 sid_copy(&token->sids[token->num_sids], sid);
4556 token->num_sids += 1;
4561 struct security_token token;
4564 static void dump_user_token(struct user_token *token)
4568 d_printf("%s\n", token->name);
4570 for (i=0; i<token->token.num_sids; i++) {
4571 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4575 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4579 for (i=0; i<alias->num_members; i++) {
4580 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4587 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4591 for (i=0; i<num_server_aliases; i++) {
4592 if (is_alias_member(&sid, &server_aliases[i]))
4593 add_sid_to_token(token, &server_aliases[i].sid);
4598 * We got a user token with all the SIDs we can know about without asking the
4599 * server directly. These are the user and domain group sids. All of these can
4600 * be members of aliases. So scan the list of aliases for each of the SIDs and
4601 * add them to the token.
4604 static void collect_alias_memberships(struct security_token *token)
4606 int num_global_sids = token->num_sids;
4609 for (i=0; i<num_global_sids; i++) {
4610 collect_sid_memberships(token, token->sids[i]);
4614 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4616 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4617 enum wbcSidType type;
4619 struct wbcDomainSid wsid;
4620 char sid_str[WBC_SID_STRING_BUFLEN];
4621 struct dom_sid user_sid;
4622 uint32_t num_groups;
4623 gid_t *groups = NULL;
4626 fstr_sprintf(full_name, "%s%c%s",
4627 domain, *lp_winbind_separator(), user);
4629 /* First let's find out the user sid */
4631 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4633 if (!WBC_ERROR_IS_OK(wbc_status)) {
4634 DEBUG(1, ("winbind could not find %s: %s\n",
4635 full_name, wbcErrorString(wbc_status)));
4639 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4641 if (type != WBC_SID_NAME_USER) {
4642 DEBUG(1, ("%s is not a user\n", full_name));
4646 if (!string_to_sid(&user_sid, sid_str)) {
4647 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4651 init_user_token(token, &user_sid);
4653 /* And now the groups winbind knows about */
4655 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4656 if (!WBC_ERROR_IS_OK(wbc_status)) {
4657 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4658 full_name, wbcErrorString(wbc_status)));
4662 for (i = 0; i < num_groups; i++) {
4663 gid_t gid = groups[i];
4666 wbc_status = wbcGidToSid(gid, &wsid);
4667 if (!WBC_ERROR_IS_OK(wbc_status)) {
4668 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4669 (unsigned int)gid, wbcErrorString(wbc_status)));
4670 wbcFreeMemory(groups);
4674 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4676 DEBUG(3, (" %s\n", sid_str));
4678 string_to_sid(&sid, sid_str);
4679 add_sid_to_token(token, &sid);
4681 wbcFreeMemory(groups);
4687 * Get a list of all user tokens we want to look at
4690 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4691 struct user_token **user_tokens)
4693 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4694 uint32_t i, num_users;
4696 struct user_token *result;
4697 TALLOC_CTX *frame = NULL;
4699 if (lp_winbind_use_default_domain() &&
4700 (c->opt_target_workgroup == NULL)) {
4701 d_fprintf(stderr, _("winbind use default domain = yes set, "
4702 "please specify a workgroup\n"));
4706 /* Send request to winbind daemon */
4708 wbc_status = wbcListUsers(NULL, &num_users, &users);
4709 if (!WBC_ERROR_IS_OK(wbc_status)) {
4710 DEBUG(1, (_("winbind could not list users: %s\n"),
4711 wbcErrorString(wbc_status)));
4715 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4717 if (result == NULL) {
4718 DEBUG(1, ("Could not malloc sid array\n"));
4719 wbcFreeMemory(users);
4723 frame = talloc_stackframe();
4724 for (i=0; i < num_users; i++) {
4725 fstring domain, user;
4728 fstrcpy(result[i].name, users[i]);
4730 p = strchr(users[i], *lp_winbind_separator());
4732 DEBUG(3, ("%s\n", users[i]));
4735 fstrcpy(domain, c->opt_target_workgroup);
4736 fstrcpy(user, users[i]);
4739 fstrcpy(domain, users[i]);
4744 get_user_sids(domain, user, &(result[i].token));
4747 wbcFreeMemory(users);
4749 *num_tokens = num_users;
4750 *user_tokens = result;
4755 static bool get_user_tokens_from_file(FILE *f,
4757 struct user_token **tokens)
4759 struct user_token *token = NULL;
4764 if (fgets(line, sizeof(line)-1, f) == NULL) {
4768 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4769 line[strlen(line)-1] = '\0';
4772 if (line[0] == ' ') {
4776 if(!string_to_sid(&sid, &line[1])) {
4777 DEBUG(1,("get_user_tokens_from_file: Could "
4778 "not convert sid %s \n",&line[1]));
4782 if (token == NULL) {
4783 DEBUG(0, ("File does not begin with username"));
4787 add_sid_to_token(&token->token, &sid);
4791 /* And a new user... */
4794 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4795 if (*tokens == NULL) {
4796 DEBUG(0, ("Could not realloc tokens\n"));
4800 token = &((*tokens)[*num_tokens-1]);
4802 fstrcpy(token->name, line);
4803 token->token.num_sids = 0;
4804 token->token.sids = NULL;
4813 * Show the list of all users that have access to a share
4816 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4817 TALLOC_CTX *mem_ctx,
4818 const char *netname,
4820 struct user_token *tokens)
4823 struct security_descriptor *share_sd = NULL;
4824 struct security_descriptor *root_sd = NULL;
4825 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4827 union srvsvc_NetShareInfo info;
4831 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4833 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4840 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4841 DEBUG(1, ("Coult not query secdesc for share %s\n",
4846 share_sd = info.info502->sd_buf.sd;
4847 if (share_sd == NULL) {
4848 DEBUG(1, ("Got no secdesc for share %s\n",
4854 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4858 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4859 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4860 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4863 for (i=0; i<num_tokens; i++) {
4866 if (share_sd != NULL) {
4867 status = se_access_check(share_sd, &tokens[i].token,
4870 if (!NT_STATUS_IS_OK(status)) {
4871 DEBUG(1, ("Could not check share_sd for "
4878 if (root_sd == NULL) {
4879 d_printf(" %s\n", tokens[i].name);
4883 status = se_access_check(root_sd, &tokens[i].token,
4885 if (!NT_STATUS_IS_OK(status)) {
4886 DEBUG(1, ("Could not check root_sd for user %s\n",
4890 d_printf(" %s\n", tokens[i].name);
4893 if (fnum != (uint16_t)-1)
4894 cli_close(cli, fnum);
4906 static void collect_share(const char *name, uint32 m,
4907 const char *comment, void *state)
4909 struct share_list *share_list = (struct share_list *)state;
4911 if (m != STYPE_DISKTREE)
4914 share_list->num_shares += 1;
4915 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4916 if (!share_list->shares) {
4917 share_list->num_shares = 0;
4920 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4924 * List shares on a remote RPC server, including the security descriptors.
4926 * All parameters are provided by the run_rpc_command function, except for
4927 * argc, argv which are passed through.
4929 * @param domain_sid The domain sid acquired from the remote server.
4930 * @param cli A cli_state connected to the server.
4931 * @param mem_ctx Talloc context, destroyed on completion of the function.
4932 * @param argc Standard main() style argc.
4933 * @param argv Standard main() style argv. Initial components are already
4936 * @return Normal NTSTATUS return.
4939 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4940 const struct dom_sid *domain_sid,
4941 const char *domain_name,
4942 struct cli_state *cli,
4943 struct rpc_pipe_client *pipe_hnd,
4944 TALLOC_CTX *mem_ctx,
4953 struct user_token *tokens = NULL;
4956 struct share_list share_list;
4961 f = fopen(argv[0], "r");
4965 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4966 return NT_STATUS_UNSUCCESSFUL;
4969 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4975 DEBUG(0, ("Could not read users from file\n"));
4976 return NT_STATUS_UNSUCCESSFUL;
4979 for (i=0; i<num_tokens; i++)
4980 collect_alias_memberships(&tokens[i].token);
4982 share_list.num_shares = 0;
4983 share_list.shares = NULL;
4985 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4988 DEBUG(0, ("Error returning browse list: %s\n",
4993 for (i = 0; i < share_list.num_shares; i++) {
4994 char *netname = share_list.shares[i];
4996 if (netname[strlen(netname)-1] == '$')
4999 d_printf("%s\n", netname);
5001 show_userlist(pipe_hnd, mem_ctx, netname,
5002 num_tokens, tokens);
5005 for (i=0; i<num_tokens; i++) {
5006 free_user_token(&tokens[i].token);
5009 SAFE_FREE(share_list.shares);
5011 return NT_STATUS_OK;
5014 static int rpc_share_allowedusers(struct net_context *c, int argc,
5019 if (c->display_usage) {
5021 "net rpc share allowedusers\n"
5024 _("List allowed users"));
5028 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5029 rpc_aliaslist_internals,
5034 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
5040 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5041 rpc_share_allowedusers_internals,
5045 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5048 struct user_token *tokens = NULL;
5052 net_usersidlist_usage(c, argc, argv);
5056 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5057 DEBUG(0, ("Could not get the user/sid list\n"));
5061 for (i=0; i<num_tokens; i++) {
5062 dump_user_token(&tokens[i]);
5063 free_user_token(&tokens[i].token);
5070 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5072 d_printf(_("net usersidlist\n"
5073 "\tprints out a list of all users the running winbind knows\n"
5074 "\tabout, together with all their SIDs. This is used as\n"
5075 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5077 net_common_flags_usage(c, argc, argv);
5082 * 'net rpc share' entrypoint.
5083 * @param argc Standard main() style argc.
5084 * @param argv Standard main() style argv. Initial components are already
5088 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5090 NET_API_STATUS status;
5092 struct functable func[] = {
5098 N_("net rpc share add\n"
5106 N_("net rpc share delete\n"
5111 rpc_share_allowedusers,
5113 N_("Modify allowed users"),
5114 N_("net rpc share allowedusers\n"
5115 " Modify allowed users")
5121 N_("Migrate share to local server"),
5122 N_("net rpc share migrate\n"
5123 " Migrate share to local server")
5130 N_("net rpc share list\n"
5133 {NULL, NULL, 0, NULL, NULL}
5136 status = libnetapi_net_init(&c->netapi_ctx);
5140 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5141 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5142 if (c->opt_kerberos) {
5143 libnetapi_set_use_kerberos(c->netapi_ctx);
5147 if (c->display_usage) {
5152 " Alias for net rpc share list\n"));
5153 net_display_usage_from_functable(func);
5157 return rpc_share_list(c, argc, argv);
5160 return net_run_function(c, argc, argv, "net rpc share", func);
5163 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5164 TALLOC_CTX *mem_ctx,
5165 struct rpc_sh_ctx *ctx,
5166 struct rpc_pipe_client *pipe_hnd,
5167 int argc, const char **argv)
5170 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5173 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5174 TALLOC_CTX *mem_ctx,
5175 struct rpc_sh_ctx *ctx,
5176 struct rpc_pipe_client *pipe_hnd,
5177 int argc, const char **argv)
5179 NET_API_STATUS status;
5180 uint32_t parm_err = 0;
5181 struct SHARE_INFO_2 i2;
5183 if ((argc < 2) || (argc > 3)) {
5184 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5186 return NT_STATUS_INVALID_PARAMETER;
5189 i2.shi2_netname = argv[0];
5190 i2.shi2_type = STYPE_DISKTREE;
5191 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5192 i2.shi2_permissions = 0;
5193 i2.shi2_max_uses = 0;
5194 i2.shi2_current_uses = 0;
5195 i2.shi2_path = argv[1];
5196 i2.shi2_passwd = NULL;
5198 status = NetShareAdd(pipe_hnd->desthost,
5203 return werror_to_ntstatus(W_ERROR(status));
5206 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5207 TALLOC_CTX *mem_ctx,
5208 struct rpc_sh_ctx *ctx,
5209 struct rpc_pipe_client *pipe_hnd,
5210 int argc, const char **argv)
5213 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5214 return NT_STATUS_INVALID_PARAMETER;
5217 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5220 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5221 TALLOC_CTX *mem_ctx,
5222 struct rpc_sh_ctx *ctx,
5223 struct rpc_pipe_client *pipe_hnd,
5224 int argc, const char **argv)
5226 union srvsvc_NetShareInfo info;
5229 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5232 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5233 return NT_STATUS_INVALID_PARAMETER;
5236 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5242 if (!NT_STATUS_IS_OK(status)) {
5243 result = ntstatus_to_werror(status);
5246 if (!W_ERROR_IS_OK(result)) {
5250 d_printf(_("Name: %s\n"), info.info2->name);
5251 d_printf(_("Comment: %s\n"), info.info2->comment);
5252 d_printf(_("Path: %s\n"), info.info2->path);
5253 d_printf(_("Password: %s\n"), info.info2->password);
5256 return werror_to_ntstatus(result);
5259 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5260 struct rpc_sh_ctx *ctx)
5262 static struct rpc_sh_cmd cmds[] = {
5264 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
5265 N_("List available shares") },
5267 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
5268 N_("Add a share") },
5270 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
5271 N_("Delete a share") },
5273 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
5274 N_("Get information about a share") },
5276 { NULL, NULL, 0, NULL, NULL }
5282 /****************************************************************************/
5284 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5286 return net_file_usage(c, argc, argv);
5290 * Close a file on a remote RPC server.
5292 * @param argc Standard main() style argc.
5293 * @param argv Standard main() style argv. Initial components are already
5296 * @return A shell status integer (0 for success).
5298 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5300 if (argc < 1 || c->display_usage) {
5301 return rpc_file_usage(c, argc, argv);
5304 return NetFileClose(c->opt_host, atoi(argv[0]));
5308 * Formatted print of open file info
5310 * @param r struct FILE_INFO_3 contents
5313 static void display_file_info_3(struct FILE_INFO_3 *r)
5315 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5316 r->fi3_id, r->fi3_username, r->fi3_permissions,
5317 r->fi3_num_locks, r->fi3_pathname);
5321 * List files for a user on a remote RPC server.
5323 * @param argc Standard main() style argc.
5324 * @param argv Standard main() style argv. Initial components are already
5327 * @return A shell status integer (0 for success)..
5330 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5332 NET_API_STATUS status;
5333 uint32 preferred_len = 0xffffffff, i;
5334 const char *username=NULL;
5335 uint32_t total_entries = 0;
5336 uint32_t entries_read = 0;
5337 uint32_t resume_handle = 0;
5338 struct FILE_INFO_3 *i3 = NULL;
5340 if (c->display_usage) {
5341 return rpc_file_usage(c, argc, argv);
5344 /* if argc > 0, must be user command */
5346 username = smb_xstrdup(argv[0]);
5349 status = NetFileEnum(c->opt_host,
5353 (uint8_t **)(void *)&i3,
5363 /* Display results */
5366 "\nEnumerating open files on remote server:\n\n"
5367 "\nFileId Opened by Perms Locks Path"
5368 "\n------ --------- ----- ----- ---- \n"));
5369 for (i = 0; i < entries_read; i++) {
5370 display_file_info_3(&i3[i]);
5377 * 'net rpc file' entrypoint.
5378 * @param argc Standard main() style argc.
5379 * @param argv Standard main() style argv. Initial components are already
5383 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5385 NET_API_STATUS status;
5387 struct functable func[] = {
5392 N_("Close opened file"),
5393 N_("net rpc file close\n"
5394 " Close opened file")
5400 N_("List files opened by user"),
5401 N_("net rpc file user\n"
5402 " List files opened by user")
5409 N_("Display information about opened file"),
5410 N_("net rpc file info\n"
5411 " Display information about opened file")
5414 {NULL, NULL, 0, NULL, NULL}
5417 status = libnetapi_net_init(&c->netapi_ctx);
5421 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5422 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5423 if (c->opt_kerberos) {
5424 libnetapi_set_use_kerberos(c->netapi_ctx);
5428 if (c->display_usage) {
5429 d_printf(_("Usage:\n"));
5430 d_printf(_("net rpc file\n"
5431 " List opened files\n"));
5432 net_display_usage_from_functable(func);
5436 return rpc_file_user(c, argc, argv);
5439 return net_run_function(c, argc, argv, "net rpc file", func);
5443 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5445 * All parameters are provided by the run_rpc_command function, except for
5446 * argc, argv which are passed through.
5448 * @param c A net_context structure.
5449 * @param domain_sid The domain sid acquired from the remote server.
5450 * @param cli A cli_state connected to the server.
5451 * @param mem_ctx Talloc context, destroyed on completion of the function.
5452 * @param argc Standard main() style argc.
5453 * @param argv Standard main() style argv. Initial components are already
5456 * @return Normal NTSTATUS return.
5459 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5460 const struct dom_sid *domain_sid,
5461 const char *domain_name,
5462 struct cli_state *cli,
5463 struct rpc_pipe_client *pipe_hnd,
5464 TALLOC_CTX *mem_ctx,
5468 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5470 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5472 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5473 if (!NT_STATUS_IS_OK(status)) {
5476 if (W_ERROR_IS_OK(result)) {
5477 d_printf(_("\nShutdown successfully aborted\n"));
5478 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5480 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5482 return werror_to_ntstatus(result);
5486 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5488 * All parameters are provided by the run_rpc_command function, except for
5489 * argc, argv which are passed through.
5491 * @param c A net_context structure.
5492 * @param domain_sid The domain sid acquired from the remote server.
5493 * @param cli A cli_state connected to the server.
5494 * @param mem_ctx Talloc context, destroyed on completion of the function.
5495 * @param argc Standard main() style argc.
5496 * @param argv Standard main() style argv. Initial components are already
5499 * @return Normal NTSTATUS return.
5502 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5503 const struct dom_sid *domain_sid,
5504 const char *domain_name,
5505 struct cli_state *cli,
5506 struct rpc_pipe_client *pipe_hnd,
5507 TALLOC_CTX *mem_ctx,
5511 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5513 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5515 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5517 if (!NT_STATUS_IS_OK(result)) {
5518 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5521 if (W_ERROR_IS_OK(werr)) {
5522 d_printf(_("\nShutdown successfully aborted\n"));
5523 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5525 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5527 return werror_to_ntstatus(werr);
5531 * ABORT the shutdown of a remote RPC server.
5533 * @param argc Standard main() style argc.
5534 * @param argv Standard main() style argv. Initial components are already
5537 * @return A shell status integer (0 for success).
5540 static int rpc_shutdown_abort(struct net_context *c, int argc,
5545 if (c->display_usage) {
5547 "net rpc abortshutdown\n"
5550 _("Abort a scheduled shutdown"));
5554 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5555 rpc_shutdown_abort_internals, argc, argv);
5560 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5562 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5563 rpc_reg_shutdown_abort_internals,
5568 * Shut down a remote RPC Server via initshutdown pipe.
5570 * All parameters are provided by the run_rpc_command function, except for
5571 * argc, argv which are passed through.
5573 * @param c A net_context structure.
5574 * @param domain_sid The domain sid acquired from the remote server.
5575 * @param cli A cli_state connected to the server.
5576 * @param mem_ctx Talloc context, destroyed on completion of the function.
5577 * @param argc Standard main() style argc.
5578 * @param argv Standard main() style argv. Initial components are already
5581 * @return Normal NTSTATUS return.
5584 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5585 const struct dom_sid *domain_sid,
5586 const char *domain_name,
5587 struct cli_state *cli,
5588 struct rpc_pipe_client *pipe_hnd,
5589 TALLOC_CTX *mem_ctx,
5593 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5595 const char *msg = N_("This machine will be shutdown shortly");
5596 uint32 timeout = 20;
5597 struct lsa_StringLarge msg_string;
5598 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5600 if (c->opt_comment) {
5601 msg = c->opt_comment;
5603 if (c->opt_timeout) {
5604 timeout = c->opt_timeout;
5607 msg_string.string = msg;
5609 /* create an entry */
5610 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5611 &msg_string, timeout, c->opt_force, c->opt_reboot,
5613 if (!NT_STATUS_IS_OK(status)) {
5616 if (W_ERROR_IS_OK(result)) {
5617 d_printf(_("\nShutdown of remote machine succeeded\n"));
5618 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5620 DEBUG(1,("Shutdown of remote machine failed!\n"));
5622 return werror_to_ntstatus(result);
5626 * Shut down a remote RPC Server via winreg pipe.
5628 * All parameters are provided by the run_rpc_command function, except for
5629 * argc, argv which are passed through.
5631 * @param c A net_context structure.
5632 * @param domain_sid The domain sid acquired from the remote server.
5633 * @param cli A cli_state connected to the server.
5634 * @param mem_ctx Talloc context, destroyed on completion of the function.
5635 * @param argc Standard main() style argc.
5636 * @param argv Standard main() style argv. Initial components are already
5639 * @return Normal NTSTATUS return.
5642 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5643 const struct dom_sid *domain_sid,
5644 const char *domain_name,
5645 struct cli_state *cli,
5646 struct rpc_pipe_client *pipe_hnd,
5647 TALLOC_CTX *mem_ctx,
5651 const char *msg = N_("This machine will be shutdown shortly");
5652 uint32 timeout = 20;
5653 struct lsa_StringLarge msg_string;
5656 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5658 if (c->opt_comment) {
5659 msg = c->opt_comment;
5661 msg_string.string = msg;
5663 if (c->opt_timeout) {
5664 timeout = c->opt_timeout;
5667 /* create an entry */
5668 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5669 &msg_string, timeout, c->opt_force, c->opt_reboot,
5671 if (!NT_STATUS_IS_OK(result)) {
5672 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5676 if (W_ERROR_IS_OK(werr)) {
5677 d_printf(_("\nShutdown of remote machine succeeded\n"));
5679 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5680 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5681 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5683 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5686 return werror_to_ntstatus(werr);
5690 * Shut down a remote RPC server.
5692 * @param argc Standard main() style argc.
5693 * @param argv Standard main() style argv. Initial components are already
5696 * @return A shell status integer (0 for success).
5699 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5703 if (c->display_usage) {
5705 "net rpc shutdown\n"
5708 _("Shut down a remote RPC server"));
5712 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5713 rpc_init_shutdown_internals, argc, argv);
5716 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5717 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5718 rpc_reg_shutdown_internals, argc, argv);
5724 /***************************************************************************
5725 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5726 ***************************************************************************/
5729 * Add interdomain trust account to the RPC server.
5730 * All parameters (except for argc and argv) are passed by run_rpc_command
5733 * @param c A net_context structure.
5734 * @param domain_sid The domain sid acquired from the server.
5735 * @param cli A cli_state connected to the server.
5736 * @param mem_ctx Talloc context, destroyed on completion of the function.
5737 * @param argc Standard main() style argc.
5738 * @param argv Standard main() style argv. Initial components are already
5741 * @return normal NTSTATUS return code.
5744 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5745 const struct dom_sid *domain_sid,
5746 const char *domain_name,
5747 struct cli_state *cli,
5748 struct rpc_pipe_client *pipe_hnd,
5749 TALLOC_CTX *mem_ctx,
5753 struct policy_handle connect_pol, domain_pol, user_pol;
5754 NTSTATUS status, result;
5756 struct lsa_String lsa_acct_name;
5758 uint32 acct_flags=0;
5760 uint32_t access_granted = 0;
5761 union samr_UserInfo info;
5762 unsigned int orig_timeout;
5763 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5768 _(" net rpc trustdom add <domain_name> "
5769 "<trust password>\n"));
5770 return NT_STATUS_INVALID_PARAMETER;
5774 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5777 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5778 return NT_STATUS_NO_MEMORY;
5781 strupper_m(acct_name);
5783 init_lsa_String(&lsa_acct_name, acct_name);
5785 /* Get samr policy handle */
5786 status = dcerpc_samr_Connect2(b, mem_ctx,
5788 MAXIMUM_ALLOWED_ACCESS,
5791 if (!NT_STATUS_IS_OK(status)) {
5794 if (!NT_STATUS_IS_OK(result)) {
5799 /* Get domain policy handle */
5800 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5802 MAXIMUM_ALLOWED_ACCESS,
5803 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5806 if (!NT_STATUS_IS_OK(status)) {
5809 if (!NT_STATUS_IS_OK(result)) {
5814 /* This call can take a long time - allow the server to time out.
5815 * 35 seconds should do it. */
5817 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5819 /* Create trusting domain's account */
5820 acb_info = ACB_NORMAL;
5821 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5822 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5823 SAMR_USER_ACCESS_SET_PASSWORD |
5824 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5825 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5827 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5836 if (!NT_STATUS_IS_OK(status)) {
5839 /* And restore our original timeout. */
5840 rpccli_set_timeout(pipe_hnd, orig_timeout);
5842 if (!NT_STATUS_IS_OK(result)) {
5844 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5845 acct_name, nt_errstr(result));
5850 struct samr_CryptPassword crypt_pwd;
5852 ZERO_STRUCT(info.info23);
5854 init_samr_CryptPassword(argv[1],
5855 &cli->user_session_key,
5858 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5859 SAMR_FIELD_NT_PASSWORD_PRESENT;
5860 info.info23.info.acct_flags = ACB_DOMTRUST;
5861 info.info23.password = crypt_pwd;
5863 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5868 if (!NT_STATUS_IS_OK(status)) {
5872 if (!NT_STATUS_IS_OK(result)) {
5874 DEBUG(0,("Could not set trust account password: %s\n",
5875 nt_errstr(result)));
5881 SAFE_FREE(acct_name);
5886 * Create interdomain trust account for a remote domain.
5888 * @param argc Standard argc.
5889 * @param argv Standard argv without initial components.
5891 * @return Integer status (0 means success).
5894 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5896 if (argc > 0 && !c->display_usage) {
5897 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5898 rpc_trustdom_add_internals, argc, argv);
5902 _("net rpc trustdom add <domain_name> <trust "
5910 * Remove interdomain trust account from the RPC server.
5911 * All parameters (except for argc and argv) are passed by run_rpc_command
5914 * @param c A net_context structure.
5915 * @param domain_sid The domain sid acquired from the server.
5916 * @param cli A cli_state connected to the server.
5917 * @param mem_ctx Talloc context, destroyed on completion of the function.
5918 * @param argc Standard main() style argc.
5919 * @param argv Standard main() style argv. Initial components are already
5922 * @return normal NTSTATUS return code.
5925 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5926 const struct dom_sid *domain_sid,
5927 const char *domain_name,
5928 struct cli_state *cli,
5929 struct rpc_pipe_client *pipe_hnd,
5930 TALLOC_CTX *mem_ctx,
5934 struct policy_handle connect_pol, domain_pol, user_pol;
5935 NTSTATUS status, result;
5937 struct dom_sid trust_acct_sid;
5938 struct samr_Ids user_rids, name_types;
5939 struct lsa_String lsa_acct_name;
5940 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5945 _(" net rpc trustdom del <domain_name>\n"));
5946 return NT_STATUS_INVALID_PARAMETER;
5950 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5952 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5954 if (acct_name == NULL)
5955 return NT_STATUS_NO_MEMORY;
5957 strupper_m(acct_name);
5959 /* Get samr policy handle */
5960 status = dcerpc_samr_Connect2(b, mem_ctx,
5962 MAXIMUM_ALLOWED_ACCESS,
5965 if (!NT_STATUS_IS_OK(status)) {
5968 if (!NT_STATUS_IS_OK(result)) {
5973 /* Get domain policy handle */
5974 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5976 MAXIMUM_ALLOWED_ACCESS,
5977 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5980 if (!NT_STATUS_IS_OK(status)) {
5983 if (!NT_STATUS_IS_OK(result)) {
5988 init_lsa_String(&lsa_acct_name, acct_name);
5990 status = dcerpc_samr_LookupNames(b, mem_ctx,
5997 if (!NT_STATUS_IS_OK(status)) {
5998 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6000 acct_name, nt_errstr(status));
6003 if (!NT_STATUS_IS_OK(result)) {
6005 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6007 acct_name, nt_errstr(result) );
6011 status = dcerpc_samr_OpenUser(b, mem_ctx,
6013 MAXIMUM_ALLOWED_ACCESS,
6017 if (!NT_STATUS_IS_OK(status)) {
6018 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6020 acct_name, nt_errstr(status) );
6024 if (!NT_STATUS_IS_OK(result)) {
6026 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6028 acct_name, nt_errstr(result) );
6032 /* append the rid to the domain sid */
6033 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6037 /* remove the sid */
6039 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6043 if (!NT_STATUS_IS_OK(status)) {
6044 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6045 " on user %s failed %s\n"),
6046 acct_name, nt_errstr(status));
6049 if (!NT_STATUS_IS_OK(result)) {
6051 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6052 " on user %s failed %s\n"),
6053 acct_name, nt_errstr(result) );
6060 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6063 if (!NT_STATUS_IS_OK(status)) {
6064 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6066 acct_name, nt_errstr(status));
6070 if (!NT_STATUS_IS_OK(result)) {
6072 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6074 acct_name, nt_errstr(result) );
6078 if (!NT_STATUS_IS_OK(result)) {
6079 d_printf(_("Could not set trust account password: %s\n"),
6089 * Delete interdomain trust account for a remote domain.
6091 * @param argc Standard argc.
6092 * @param argv Standard argv without initial components.
6094 * @return Integer status (0 means success).
6097 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6099 if (argc > 0 && !c->display_usage) {
6100 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
6101 rpc_trustdom_del_internals, argc, argv);
6105 _("net rpc trustdom del <domain>\n"));
6110 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6111 struct cli_state *cli,
6112 TALLOC_CTX *mem_ctx,
6113 const char *domain_name)
6115 char *dc_name = NULL;
6116 const char *buffer = NULL;
6117 struct rpc_pipe_client *netr;
6120 struct dcerpc_binding_handle *b;
6122 /* Use NetServerEnum2 */
6124 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6126 return NT_STATUS_OK;
6129 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6130 for domain %s\n", domain_name));
6132 /* Try netr_GetDcName */
6134 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
6136 if (!NT_STATUS_IS_OK(status)) {
6140 b = netr->binding_handle;
6142 status = dcerpc_netr_GetDcName(b, mem_ctx,
6149 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6153 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6154 for domain %s\n", domain_name));
6156 if (!NT_STATUS_IS_OK(status)) {
6160 return werror_to_ntstatus(result);
6164 * Establish trust relationship to a trusting domain.
6165 * Interdomain account must already be created on remote PDC.
6167 * @param c A net_context structure.
6168 * @param argc Standard argc.
6169 * @param argv Standard argv without initial components.
6171 * @return Integer status (0 means success).
6174 static int rpc_trustdom_establish(struct net_context *c, int argc,
6177 struct cli_state *cli = NULL;
6178 struct sockaddr_storage server_ss;
6179 struct rpc_pipe_client *pipe_hnd = NULL;
6180 struct policy_handle connect_hnd;
6181 TALLOC_CTX *mem_ctx;
6182 NTSTATUS nt_status, result;
6183 struct dom_sid *domain_sid;
6188 union lsa_PolicyInformation *info = NULL;
6189 struct dcerpc_binding_handle *b;
6192 * Connect to \\server\ipc$ as 'our domain' account with password
6195 if (argc != 1 || c->display_usage) {
6198 _("net rpc trustdom establish <domain_name>\n"));
6202 domain_name = smb_xstrdup(argv[0]);
6203 strupper_m(domain_name);
6205 /* account name used at first is our domain's name with '$' */
6206 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6209 strupper_m(acct_name);
6212 * opt_workgroup will be used by connection functions further,
6213 * hence it should be set to remote domain name instead of ours
6215 if (c->opt_workgroup) {
6216 c->opt_workgroup = smb_xstrdup(domain_name);
6219 c->opt_user_name = acct_name;
6221 /* find the domain controller */
6222 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6223 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6227 /* connect to ipc$ as username/password */
6228 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6229 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6231 /* Is it trusting domain account for sure ? */
6232 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6233 nt_errstr(nt_status)));
6237 /* store who we connected to */
6239 saf_store( domain_name, pdc_name );
6242 * Connect to \\server\ipc$ again (this time anonymously)
6245 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6248 if (NT_STATUS_IS_ERR(nt_status)) {
6249 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6250 domain_name, nt_errstr(nt_status)));
6254 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6255 "domain %s", domain_name))) {
6256 DEBUG(0, ("talloc_init() failed\n"));
6261 /* Make sure we're talking to a proper server */
6263 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6264 if (!NT_STATUS_IS_OK(nt_status)) {
6266 talloc_destroy(mem_ctx);
6271 * Call LsaOpenPolicy and LsaQueryInfo
6274 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6276 if (!NT_STATUS_IS_OK(nt_status)) {
6277 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6279 talloc_destroy(mem_ctx);
6283 b = pipe_hnd->binding_handle;
6285 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6287 if (NT_STATUS_IS_ERR(nt_status)) {
6288 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6289 nt_errstr(nt_status)));
6291 talloc_destroy(mem_ctx);
6295 /* Querying info level 5 */
6297 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6299 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6302 if (NT_STATUS_IS_ERR(nt_status)) {
6303 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6304 nt_errstr(nt_status)));
6306 talloc_destroy(mem_ctx);
6309 if (NT_STATUS_IS_ERR(result)) {
6310 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6311 nt_errstr(result)));
6313 talloc_destroy(mem_ctx);
6317 domain_sid = info->account_domain.sid;
6319 /* There should be actually query info level 3 (following nt serv behaviour),
6320 but I still don't know if it's _really_ necessary */
6323 * Store the password in secrets db
6326 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6327 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6329 talloc_destroy(mem_ctx);
6334 * Close the pipes and clean up
6337 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6338 if (NT_STATUS_IS_ERR(nt_status)) {
6339 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6340 nt_errstr(nt_status)));
6342 talloc_destroy(mem_ctx);
6348 talloc_destroy(mem_ctx);
6350 d_printf(_("Trust to domain %s established\n"), domain_name);
6355 * Revoke trust relationship to the remote domain.
6357 * @param c A net_context structure.
6358 * @param argc Standard argc.
6359 * @param argv Standard argv without initial components.
6361 * @return Integer status (0 means success).
6364 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6370 if (argc < 1 || c->display_usage) {
6373 _("net rpc trustdom revoke <domain_name>\n"
6374 " Revoke trust relationship\n"
6375 " domain_name\tName of domain to revoke trust\n"));
6379 /* generate upper cased domain name */
6380 domain_name = smb_xstrdup(argv[0]);
6381 strupper_m(domain_name);
6383 /* delete password of the trust */
6384 if (!pdb_del_trusteddom_pw(domain_name)) {
6385 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6392 SAFE_FREE(domain_name);
6396 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6397 const struct dom_sid *domain_sid,
6398 const char *domain_name,
6399 struct cli_state *cli,
6400 struct rpc_pipe_client *pipe_hnd,
6401 TALLOC_CTX *mem_ctx,
6406 if (!sid_to_fstring(str_sid, domain_sid)) {
6407 return NT_STATUS_UNSUCCESSFUL;
6409 d_printf("%s\n", str_sid);
6410 return NT_STATUS_OK;
6413 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6417 /* convert sid into ascii string */
6418 sid_to_fstring(ascii_sid, dom_sid);
6420 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6423 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6424 TALLOC_CTX *mem_ctx,
6425 struct policy_handle *pol,
6426 struct dom_sid dom_sid,
6427 const char *trusted_dom_name)
6429 NTSTATUS nt_status, result;
6430 union lsa_TrustedDomainInfo *info = NULL;
6431 char *cleartextpwd = NULL;
6432 uint8_t session_key[16];
6433 DATA_BLOB session_key_blob;
6434 DATA_BLOB data = data_blob_null;
6435 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6437 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6440 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6443 if (NT_STATUS_IS_ERR(nt_status)) {
6444 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6445 nt_errstr(nt_status)));
6448 if (NT_STATUS_IS_ERR(result)) {
6450 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6451 nt_errstr(result)));
6455 data = data_blob(info->password.password->data,
6456 info->password.password->length);
6458 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
6459 DEBUG(0, ("Could not retrieve password hash\n"));
6463 session_key_blob = data_blob_const(session_key, sizeof(session_key));
6464 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
6466 if (cleartextpwd == NULL) {
6467 DEBUG(0,("retrieved NULL password\n"));
6468 nt_status = NT_STATUS_UNSUCCESSFUL;
6472 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6473 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6474 nt_status = NT_STATUS_UNSUCCESSFUL;
6478 #ifdef DEBUG_PASSWORD
6479 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6480 "password: [%s]\n", trusted_dom_name,
6481 sid_string_dbg(&dom_sid), cleartextpwd));
6485 SAFE_FREE(cleartextpwd);
6486 data_blob_free(&data);
6491 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6494 /* common variables */
6495 TALLOC_CTX* mem_ctx;
6496 struct cli_state *cli = NULL;
6497 struct rpc_pipe_client *pipe_hnd = NULL;
6498 NTSTATUS nt_status, result;
6499 const char *domain_name = NULL;
6500 struct dom_sid *queried_dom_sid;
6501 struct policy_handle connect_hnd;
6502 union lsa_PolicyInformation *info = NULL;
6504 /* trusted domains listing variables */
6505 unsigned int enum_ctx = 0;
6507 struct lsa_DomainList dom_list;
6509 struct dcerpc_binding_handle *b;
6511 if (c->display_usage) {
6513 "net rpc trustdom vampire\n"
6516 _("Vampire trust relationship from remote server"));
6521 * Listing trusted domains (stored in secrets.tdb, if local)
6524 mem_ctx = talloc_init("trust relationships vampire");
6527 * set domain and pdc name to local samba server (default)
6528 * or to remote one given in command line
6531 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6532 domain_name = c->opt_workgroup;
6533 c->opt_target_workgroup = c->opt_workgroup;
6535 fstrcpy(pdc_name, global_myname());
6536 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6537 c->opt_target_workgroup = domain_name;
6540 /* open \PIPE\lsarpc and open policy handle */
6541 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6542 if (!NT_STATUS_IS_OK(nt_status)) {
6543 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6544 nt_errstr(nt_status)));
6545 talloc_destroy(mem_ctx);
6549 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6551 if (!NT_STATUS_IS_OK(nt_status)) {
6552 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6553 nt_errstr(nt_status) ));
6555 talloc_destroy(mem_ctx);
6559 b = pipe_hnd->binding_handle;
6561 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6563 if (NT_STATUS_IS_ERR(nt_status)) {
6564 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6565 nt_errstr(nt_status)));
6567 talloc_destroy(mem_ctx);
6571 /* query info level 5 to obtain sid of a domain being queried */
6572 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6574 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6578 if (NT_STATUS_IS_ERR(nt_status)) {
6579 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6580 nt_errstr(nt_status)));
6582 talloc_destroy(mem_ctx);
6585 if (NT_STATUS_IS_ERR(result)) {
6586 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6587 nt_errstr(result)));
6589 talloc_destroy(mem_ctx);
6593 queried_dom_sid = info->account_domain.sid;
6596 * Keep calling LsaEnumTrustdom over opened pipe until
6597 * the end of enumeration is reached
6600 d_printf(_("Vampire trusted domains:\n\n"));
6603 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6609 if (NT_STATUS_IS_ERR(nt_status)) {
6610 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6611 nt_errstr(nt_status)));
6613 talloc_destroy(mem_ctx);
6616 if (NT_STATUS_IS_ERR(result)) {
6618 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6619 nt_errstr(result)));
6621 talloc_destroy(mem_ctx);
6626 for (i = 0; i < dom_list.count; i++) {
6628 print_trusted_domain(dom_list.domains[i].sid,
6629 dom_list.domains[i].name.string);
6631 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6632 *dom_list.domains[i].sid,
6633 dom_list.domains[i].name.string);
6634 if (!NT_STATUS_IS_OK(nt_status)) {
6636 talloc_destroy(mem_ctx);
6642 * in case of no trusted domains say something rather
6643 * than just display blank line
6645 if (!dom_list.count) d_printf(_("none\n"));
6647 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6649 /* close this connection before doing next one */
6650 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6651 if (NT_STATUS_IS_ERR(nt_status)) {
6652 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6653 nt_errstr(nt_status)));
6655 talloc_destroy(mem_ctx);
6659 /* close lsarpc pipe and connection to IPC$ */
6662 talloc_destroy(mem_ctx);
6666 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6668 /* common variables */
6669 TALLOC_CTX* mem_ctx;
6670 struct cli_state *cli = NULL, *remote_cli = NULL;
6671 struct rpc_pipe_client *pipe_hnd = NULL;
6672 NTSTATUS nt_status, result;
6673 const char *domain_name = NULL;
6674 struct dom_sid *queried_dom_sid;
6675 int ascii_dom_name_len;
6676 struct policy_handle connect_hnd;
6677 union lsa_PolicyInformation *info = NULL;
6678 struct dcerpc_binding_handle *b = NULL;
6680 /* trusted domains listing variables */
6681 unsigned int num_domains, enum_ctx = 0;
6683 struct lsa_DomainList dom_list;
6687 /* trusting domains listing variables */
6688 struct policy_handle domain_hnd;
6689 struct samr_SamArray *trusts = NULL;
6691 if (c->display_usage) {
6693 "net rpc trustdom list\n"
6696 _("List incoming and outgoing trust relationships"));
6701 * Listing trusted domains (stored in secrets.tdb, if local)
6704 mem_ctx = talloc_init("trust relationships listing");
6707 * set domain and pdc name to local samba server (default)
6708 * or to remote one given in command line
6711 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6712 domain_name = c->opt_workgroup;
6713 c->opt_target_workgroup = c->opt_workgroup;
6715 fstrcpy(pdc_name, global_myname());
6716 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6717 c->opt_target_workgroup = domain_name;
6720 /* open \PIPE\lsarpc and open policy handle */
6721 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6722 if (!NT_STATUS_IS_OK(nt_status)) {
6723 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6724 nt_errstr(nt_status)));
6725 talloc_destroy(mem_ctx);
6729 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6731 if (!NT_STATUS_IS_OK(nt_status)) {
6732 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6733 nt_errstr(nt_status) ));
6735 talloc_destroy(mem_ctx);
6739 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6741 if (NT_STATUS_IS_ERR(nt_status)) {
6742 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6743 nt_errstr(nt_status)));
6745 talloc_destroy(mem_ctx);
6749 /* query info level 5 to obtain sid of a domain being queried */
6750 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6752 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6756 if (NT_STATUS_IS_ERR(nt_status)) {
6757 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6758 nt_errstr(nt_status)));
6760 talloc_destroy(mem_ctx);
6763 if (NT_STATUS_IS_ERR(result)) {
6764 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6765 nt_errstr(result)));
6767 talloc_destroy(mem_ctx);
6771 queried_dom_sid = info->account_domain.sid;
6774 * Keep calling LsaEnumTrustdom over opened pipe until
6775 * the end of enumeration is reached
6778 d_printf(_("Trusted domains list:\n\n"));
6780 found_domain = false;
6783 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6789 if (NT_STATUS_IS_ERR(nt_status)) {
6790 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6791 nt_errstr(nt_status)));
6793 talloc_destroy(mem_ctx);
6796 if (NT_STATUS_IS_ERR(result)) {
6797 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6798 nt_errstr(result)));
6800 talloc_destroy(mem_ctx);
6805 for (i = 0; i < dom_list.count; i++) {
6806 print_trusted_domain(dom_list.domains[i].sid,
6807 dom_list.domains[i].name.string);
6808 found_domain = true;
6812 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6815 * in case of no trusted domains say something rather
6816 * than just display blank line
6818 if (!found_domain) {
6819 d_printf(_("none\n"));
6822 /* close this connection before doing next one */
6823 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6824 if (NT_STATUS_IS_ERR(nt_status)) {
6825 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6826 nt_errstr(nt_status)));
6828 talloc_destroy(mem_ctx);
6832 TALLOC_FREE(pipe_hnd);
6835 * Listing trusting domains (stored in passdb backend, if local)
6838 d_printf(_("\nTrusting domains list:\n\n"));
6841 * Open \PIPE\samr and get needed policy handles
6843 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6845 if (!NT_STATUS_IS_OK(nt_status)) {
6846 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6848 talloc_destroy(mem_ctx);
6852 b = pipe_hnd->binding_handle;
6855 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6857 SAMR_ACCESS_LOOKUP_DOMAIN,
6860 if (!NT_STATUS_IS_OK(nt_status)) {
6861 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6862 nt_errstr(nt_status)));
6864 talloc_destroy(mem_ctx);
6867 if (!NT_STATUS_IS_OK(result)) {
6869 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6870 nt_errstr(result)));
6872 talloc_destroy(mem_ctx);
6876 /* SamrOpenDomain - we have to open domain policy handle in order to be
6877 able to enumerate accounts*/
6878 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6880 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6884 if (!NT_STATUS_IS_OK(nt_status)) {
6885 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6886 nt_errstr(nt_status)));
6888 talloc_destroy(mem_ctx);
6891 if (!NT_STATUS_IS_OK(result)) {
6893 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6894 nt_errstr(result)));
6896 talloc_destroy(mem_ctx);
6901 * perform actual enumeration
6904 found_domain = false;
6906 enum_ctx = 0; /* reset enumeration context from last enumeration */
6909 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
6917 if (NT_STATUS_IS_ERR(nt_status)) {
6918 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6919 nt_errstr(nt_status)));
6921 talloc_destroy(mem_ctx);
6924 if (NT_STATUS_IS_ERR(result)) {
6926 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6927 nt_errstr(result)));
6929 talloc_destroy(mem_ctx);
6933 for (i = 0; i < num_domains; i++) {
6935 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6937 found_domain = true;
6940 * get each single domain's sid (do we _really_ need this ?):
6941 * 1) connect to domain's pdc
6942 * 2) query the pdc for domain's sid
6945 /* get rid of '$' tail */
6946 ascii_dom_name_len = strlen(str);
6947 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6948 str[ascii_dom_name_len - 1] = '\0';
6950 /* set opt_* variables to remote domain */
6952 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6953 c->opt_target_workgroup = c->opt_workgroup;
6955 d_printf("%-20s", str);
6957 /* connect to remote domain controller */
6958 nt_status = net_make_ipc_connection(c,
6959 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6961 if (NT_STATUS_IS_OK(nt_status)) {
6962 /* query for domain's sid */
6963 if (run_rpc_command(
6965 &ndr_table_lsarpc.syntax_id, 0,
6966 rpc_query_domain_sid, argc,
6968 d_printf(_("strange - couldn't get domain's sid\n"));
6970 cli_shutdown(remote_cli);
6973 d_fprintf(stderr, _("domain controller is not "
6974 "responding: %s\n"),
6975 nt_errstr(nt_status));
6976 d_printf(_("couldn't get domain's sid\n"));
6980 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
6982 if (!found_domain) {
6986 /* close opened samr and domain policy handles */
6987 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
6988 if (!NT_STATUS_IS_OK(nt_status)) {
6989 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6992 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
6993 if (!NT_STATUS_IS_OK(nt_status)) {
6994 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6997 /* close samr pipe and connection to IPC$ */
7000 talloc_destroy(mem_ctx);
7005 * Entrypoint for 'net rpc trustdom' code.
7007 * @param argc Standard argc.
7008 * @param argv Standard argv without initial components.
7010 * @return Integer status (0 means success).
7013 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7015 struct functable func[] = {
7020 N_("Add trusting domain's account"),
7021 N_("net rpc trustdom add\n"
7022 " Add trusting domain's account")
7028 N_("Remove trusting domain's account"),
7029 N_("net rpc trustdom del\n"
7030 " Remove trusting domain's account")
7034 rpc_trustdom_establish,
7036 N_("Establish outgoing trust relationship"),
7037 N_("net rpc trustdom establish\n"
7038 " Establish outgoing trust relationship")
7042 rpc_trustdom_revoke,
7044 N_("Revoke outgoing trust relationship"),
7045 N_("net rpc trustdom revoke\n"
7046 " Revoke outgoing trust relationship")
7052 N_("List in- and outgoing domain trusts"),
7053 N_("net rpc trustdom list\n"
7054 " List in- and outgoing domain trusts")
7058 rpc_trustdom_vampire,
7060 N_("Vampire trusts from remote server"),
7061 N_("net rpc trustdom vampire\n"
7062 " Vampire trusts from remote server")
7064 {NULL, NULL, 0, NULL, NULL}
7067 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7071 * Check if a server will take rpc commands
7072 * @param flags Type of server to connect to (PDC, DMB, localhost)
7073 * if the host is not explicitly specified
7074 * @return bool (true means rpc supported)
7076 bool net_rpc_check(struct net_context *c, unsigned flags)
7078 struct cli_state *cli;
7080 struct sockaddr_storage server_ss;
7081 char *server_name = NULL;
7084 /* flags (i.e. server type) may depend on command */
7085 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7088 if ((cli = cli_initialise()) == NULL) {
7092 status = cli_connect(cli, server_name, &server_ss);
7093 if (!NT_STATUS_IS_OK(status))
7095 if (!attempt_netbios_session_request(&cli, global_myname(),
7096 server_name, &server_ss))
7098 status = cli_negprot(cli);
7099 if (!NT_STATUS_IS_OK(status))
7101 if (cli->protocol < PROTOCOL_NT1)
7110 /* dump sam database via samsync rpc calls */
7111 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7112 if (c->display_usage) {
7117 _("Dump remote SAM database"));
7121 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
7122 NET_FLAGS_ANONYMOUS,
7123 rpc_samdump_internals, argc, argv);
7126 /* syncronise sam database via samsync rpc calls */
7127 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7129 struct functable func[] = {
7134 N_("Dump remote SAM database to ldif"),
7135 N_("net rpc vampire ldif\n"
7136 " Dump remote SAM database to LDIF file or "
7143 N_("Dump remote SAM database to Kerberos Keytab"),
7144 N_("net rpc vampire keytab\n"
7145 " Dump remote SAM database to Kerberos keytab "
7152 N_("Dump remote SAM database to passdb"),
7153 N_("net rpc vampire passdb\n"
7154 " Dump remote SAM database to passdb")
7157 {NULL, NULL, 0, NULL, NULL}
7161 if (c->display_usage) {
7166 _("Vampire remote SAM database"));
7170 return rpc_vampire_passdb(c, argc, argv);
7173 return net_run_function(c, argc, argv, "net rpc vampire", func);
7177 * Migrate everything from a print server.
7179 * @param c A net_context structure.
7180 * @param argc Standard main() style argc.
7181 * @param argv Standard main() style argv. Initial components are already
7184 * @return A shell status integer (0 for success).
7186 * The order is important !
7187 * To successfully add drivers the print queues have to exist !
7188 * Applying ACLs should be the last step, because you're easily locked out.
7191 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7196 if (c->display_usage) {
7198 "net rpc printer migrate all\n"
7201 _("Migrate everything from a print server"));
7206 d_printf(_("no server to migrate\n"));
7210 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7211 rpc_printer_migrate_printers_internals, argc,
7216 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7217 rpc_printer_migrate_drivers_internals, argc,
7222 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7223 rpc_printer_migrate_forms_internals, argc, argv);
7227 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7228 rpc_printer_migrate_settings_internals, argc,
7233 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7234 rpc_printer_migrate_security_internals, argc,
7240 * Migrate print drivers from a print server.
7242 * @param c A net_context structure.
7243 * @param argc Standard main() style argc.
7244 * @param argv Standard main() style argv. Initial components are already
7247 * @return A shell status integer (0 for success).
7249 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7252 if (c->display_usage) {
7254 "net rpc printer migrate drivers\n"
7257 _("Migrate print-drivers from a print-server"));
7262 d_printf(_("no server to migrate\n"));
7266 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7267 rpc_printer_migrate_drivers_internals,
7272 * Migrate print-forms from a print-server.
7274 * @param c A net_context structure.
7275 * @param argc Standard main() style argc.
7276 * @param argv Standard main() style argv. Initial components are already
7279 * @return A shell status integer (0 for success).
7281 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7284 if (c->display_usage) {
7286 "net rpc printer migrate forms\n"
7289 _("Migrate print-forms from a print-server"));
7294 d_printf(_("no server to migrate\n"));
7298 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7299 rpc_printer_migrate_forms_internals,
7304 * Migrate printers from a print-server.
7306 * @param c A net_context structure.
7307 * @param argc Standard main() style argc.
7308 * @param argv Standard main() style argv. Initial components are already
7311 * @return A shell status integer (0 for success).
7313 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7316 if (c->display_usage) {
7318 "net rpc printer migrate printers\n"
7321 _("Migrate printers from a print-server"));
7326 d_printf(_("no server to migrate\n"));
7330 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7331 rpc_printer_migrate_printers_internals,
7336 * Migrate printer-ACLs from a print-server
7338 * @param c A net_context structure.
7339 * @param argc Standard main() style argc.
7340 * @param argv Standard main() style argv. Initial components are already
7343 * @return A shell status integer (0 for success).
7345 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7348 if (c->display_usage) {
7350 "net rpc printer migrate security\n"
7353 _("Migrate printer-ACLs from a print-server"));
7358 d_printf(_("no server to migrate\n"));
7362 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7363 rpc_printer_migrate_security_internals,
7368 * Migrate printer-settings from a print-server.
7370 * @param c A net_context structure.
7371 * @param argc Standard main() style argc.
7372 * @param argv Standard main() style argv. Initial components are already
7375 * @return A shell status integer (0 for success).
7377 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7380 if (c->display_usage) {
7382 "net rpc printer migrate settings\n"
7385 _("Migrate printer-settings from a "
7391 d_printf(_("no server to migrate\n"));
7395 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7396 rpc_printer_migrate_settings_internals,
7401 * 'net rpc printer' entrypoint.
7403 * @param c A net_context structure.
7404 * @param argc Standard main() style argc.
7405 * @param argv Standard main() style argv. Initial components are already
7409 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7412 /* ouch: when addriver and setdriver are called from within
7413 rpc_printer_migrate_drivers_internals, the printer-queue already
7416 struct functable func[] = {
7419 rpc_printer_migrate_all,
7421 N_("Migrate all from remote to local print server"),
7422 N_("net rpc printer migrate all\n"
7423 " Migrate all from remote to local print server")
7427 rpc_printer_migrate_drivers,
7429 N_("Migrate drivers to local server"),
7430 N_("net rpc printer migrate drivers\n"
7431 " Migrate drivers to local server")
7435 rpc_printer_migrate_forms,
7437 N_("Migrate froms to local server"),
7438 N_("net rpc printer migrate forms\n"
7439 " Migrate froms to local server")
7443 rpc_printer_migrate_printers,
7445 N_("Migrate printers to local server"),
7446 N_("net rpc printer migrate printers\n"
7447 " Migrate printers to local server")
7451 rpc_printer_migrate_security,
7453 N_("Mirgate printer ACLs to local server"),
7454 N_("net rpc printer migrate security\n"
7455 " Mirgate printer ACLs to local server")
7459 rpc_printer_migrate_settings,
7461 N_("Migrate printer settings to local server"),
7462 N_("net rpc printer migrate settings\n"
7463 " Migrate printer settings to local server")
7465 {NULL, NULL, 0, NULL, NULL}
7468 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7473 * List printers on a remote RPC server.
7475 * @param c A net_context structure.
7476 * @param argc Standard main() style argc.
7477 * @param argv Standard main() style argv. Initial components are already
7480 * @return A shell status integer (0 for success).
7482 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7484 if (c->display_usage) {
7486 "net rpc printer list\n"
7489 _("List printers on a remote RPC server"));
7493 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7494 rpc_printer_list_internals,
7499 * List printer-drivers on a remote RPC server.
7501 * @param c A net_context structure.
7502 * @param argc Standard main() style argc.
7503 * @param argv Standard main() style argv. Initial components are already
7506 * @return A shell status integer (0 for success).
7508 static int rpc_printer_driver_list(struct net_context *c, int argc,
7511 if (c->display_usage) {
7513 "net rpc printer driver\n"
7516 _("List printer-drivers on a remote RPC server"));
7520 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7521 rpc_printer_driver_list_internals,
7526 * Publish printer in ADS via MSRPC.
7528 * @param c A net_context structure.
7529 * @param argc Standard main() style argc.
7530 * @param argv Standard main() style argv. Initial components are already
7533 * @return A shell status integer (0 for success).
7535 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7538 if (c->display_usage) {
7540 "net rpc printer publish publish\n"
7543 _("Publish printer in ADS via MSRPC"));
7547 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7548 rpc_printer_publish_publish_internals,
7553 * Update printer in ADS via MSRPC.
7555 * @param c A net_context structure.
7556 * @param argc Standard main() style argc.
7557 * @param argv Standard main() style argv. Initial components are already
7560 * @return A shell status integer (0 for success).
7562 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7564 if (c->display_usage) {
7566 "net rpc printer publish update\n"
7569 _("Update printer in ADS via MSRPC"));
7573 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7574 rpc_printer_publish_update_internals,
7579 * UnPublish printer in ADS via MSRPC.
7581 * @param c A net_context structure.
7582 * @param argc Standard main() style argc.
7583 * @param argv Standard main() style argv. Initial components are already
7586 * @return A shell status integer (0 for success).
7588 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7591 if (c->display_usage) {
7593 "net rpc printer publish unpublish\n"
7596 _("UnPublish printer in ADS via MSRPC"));
7600 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7601 rpc_printer_publish_unpublish_internals,
7606 * List published printers via MSRPC.
7608 * @param c A net_context structure.
7609 * @param argc Standard main() style argc.
7610 * @param argv Standard main() style argv. Initial components are already
7613 * @return A shell status integer (0 for success).
7615 static int rpc_printer_publish_list(struct net_context *c, int argc,
7618 if (c->display_usage) {
7620 "net rpc printer publish list\n"
7623 _("List published printers via MSRPC"));
7627 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7628 rpc_printer_publish_list_internals,
7634 * Publish printer in ADS.
7636 * @param c A net_context structure.
7637 * @param argc Standard main() style argc.
7638 * @param argv Standard main() style argv. Initial components are already
7641 * @return A shell status integer (0 for success).
7643 static int rpc_printer_publish(struct net_context *c, int argc,
7647 struct functable func[] = {
7650 rpc_printer_publish_publish,
7652 N_("Publish printer in AD"),
7653 N_("net rpc printer publish publish\n"
7654 " Publish printer in AD")
7658 rpc_printer_publish_update,
7660 N_("Update printer in AD"),
7661 N_("net rpc printer publish update\n"
7662 " Update printer in AD")
7666 rpc_printer_publish_unpublish,
7668 N_("Unpublish printer"),
7669 N_("net rpc printer publish unpublish\n"
7670 " Unpublish printer")
7674 rpc_printer_publish_list,
7676 N_("List published printers"),
7677 N_("net rpc printer publish list\n"
7678 " List published printers")
7680 {NULL, NULL, 0, NULL, NULL}
7684 if (c->display_usage) {
7685 d_printf(_("Usage:\n"));
7686 d_printf(_("net rpc printer publish\n"
7687 " List published printers\n"
7688 " Alias of net rpc printer publish "
7690 net_display_usage_from_functable(func);
7693 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7694 rpc_printer_publish_list_internals,
7698 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7704 * Display rpc printer help page.
7706 * @param c A net_context structure.
7707 * @param argc Standard main() style argc.
7708 * @param argv Standard main() style argv. Initial components are already
7711 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7713 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7714 "\tlists all printers on print-server\n\n"));
7715 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7716 "\tlists all printer-drivers on print-server\n\n"));
7717 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7718 "\tpublishes printer settings in Active Directory\n"
7719 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7720 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7721 "\n\tmigrates printers from remote to local server\n\n"));
7722 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7723 "\n\tmigrates printer-settings from remote to local server\n\n"));
7724 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7725 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7726 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7727 "\n\tmigrates printer-forms from remote to local server\n\n"));
7728 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7729 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7730 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7731 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7732 "\tremote to local print-server\n\n"));
7733 net_common_methods_usage(c, argc, argv);
7734 net_common_flags_usage(c, argc, argv);
7736 "\t-v or --verbose\t\t\tgive verbose output\n"
7737 "\t --destination\t\tmigration target server (default: localhost)\n"));
7743 * 'net rpc printer' entrypoint.
7745 * @param c A net_context structure.
7746 * @param argc Standard main() style argc.
7747 * @param argv Standard main() style argv. Initial components are already
7750 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7752 struct functable func[] = {
7757 N_("List all printers on print server"),
7758 N_("net rpc printer list\n"
7759 " List all printers on print server")
7763 rpc_printer_migrate,
7765 N_("Migrate printer to local server"),
7766 N_("net rpc printer migrate\n"
7767 " Migrate printer to local server")
7771 rpc_printer_driver_list,
7773 N_("List printer drivers"),
7774 N_("net rpc printer driver\n"
7775 " List printer drivers")
7779 rpc_printer_publish,
7781 N_("Publish printer in AD"),
7782 N_("net rpc printer publish\n"
7783 " Publish printer in AD")
7785 {NULL, NULL, 0, NULL, NULL}
7789 if (c->display_usage) {
7790 d_printf(_("Usage:\n"));
7791 d_printf(_("net rpc printer\n"
7792 " List printers\n"));
7793 net_display_usage_from_functable(func);
7796 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7797 rpc_printer_list_internals,
7801 return net_run_function(c, argc, argv, "net rpc printer", func);
7805 * 'net rpc' entrypoint.
7807 * @param c A net_context structure.
7808 * @param argc Standard main() style argc.
7809 * @param argv Standard main() style argv. Initial components are already
7813 int net_rpc(struct net_context *c, int argc, const char **argv)
7815 NET_API_STATUS status;
7817 struct functable func[] = {
7822 N_("Modify global audit settings"),
7823 N_("net rpc audit\n"
7824 " Modify global audit settings")
7830 N_("Show basic info about a domain"),
7832 " Show basic info about a domain")
7838 N_("Join a domain"),
7846 N_("Join a domain created in server manager"),
7847 N_("net rpc oldjoin\n"
7848 " Join a domain created in server manager")
7854 N_("Test that a join is valid"),
7855 N_("net rpc testjoin\n"
7856 " Test that a join is valid")
7862 N_("List/modify users"),
7864 " List/modify users")
7870 N_("Change a user password"),
7871 N_("net rpc password\n"
7872 " Change a user password\n"
7873 " Alias for net rpc user password")
7879 N_("List/modify groups"),
7880 N_("net rpc group\n"
7881 " List/modify groups")
7887 N_("List/modify shares"),
7888 N_("net rpc share\n"
7889 " List/modify shares")
7895 N_("List open files"),
7903 N_("List/modify printers"),
7904 N_("net rpc printer\n"
7905 " List/modify printers")
7909 net_rpc_changetrustpw,
7911 N_("Change trust account password"),
7912 N_("net rpc changetrustpw\n"
7913 " Change trust account password")
7919 N_("Modify domain trusts"),
7920 N_("net rpc trustdom\n"
7921 " Modify domain trusts")
7927 N_("Abort a remote shutdown"),
7928 N_("net rpc abortshutdown\n"
7929 " Abort a remote shutdown")
7935 N_("Shutdown a remote server"),
7936 N_("net rpc shutdown\n"
7937 " Shutdown a remote server")
7943 N_("Dump SAM data of remote NT PDC"),
7944 N_("net rpc samdump\n"
7945 " Dump SAM data of remote NT PDC")
7951 N_("Sync a remote NT PDC's data into local passdb"),
7952 N_("net rpc vampire\n"
7953 " Sync a remote NT PDC's data into local passdb")
7959 N_("Fetch the domain sid into local secrets.tdb"),
7960 N_("net rpc getsid\n"
7961 " Fetch the domain sid into local secrets.tdb")
7967 N_("Manage privileges assigned to SID"),
7968 N_("net rpc rights\n"
7969 " Manage privileges assigned to SID")
7975 N_("Start/stop/query remote services"),
7976 N_("net rpc service\n"
7977 " Start/stop/query remote services")
7983 N_("Manage registry hives"),
7984 N_("net rpc registry\n"
7985 " Manage registry hives")
7991 N_("Open interactive shell on remote server"),
7992 N_("net rpc shell\n"
7993 " Open interactive shell on remote server")
7995 {NULL, NULL, 0, NULL, NULL}
7998 status = libnetapi_net_init(&c->netapi_ctx);
8002 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8003 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8004 if (c->opt_kerberos) {
8005 libnetapi_set_use_kerberos(c->netapi_ctx);
8007 if (c->opt_ccache) {
8008 libnetapi_set_use_ccache(c->netapi_ctx);
8011 return net_run_function(c, argc, argv, "net rpc", func);