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"
42 #include "libsmb/clirap.h"
44 static int net_mode_share;
45 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
50 * @brief RPC based subcommands for the 'net' utility.
52 * This file should contain much of the functionality that used to
53 * be found in rpcclient, execpt that the commands should change
54 * less often, and the fucntionality should be sane (the user is not
55 * expected to know a rid/sid before they conduct an operation etc.)
57 * @todo Perhaps eventually these should be split out into a number
58 * of files, as this could get quite big.
63 * Many of the RPC functions need the domain sid. This function gets
64 * it at the start of every run
66 * @param cli A cli_state already connected to the remote machine
68 * @return The Domain SID of the remote machine.
71 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
72 struct dom_sid **domain_sid,
73 const char **domain_name)
75 struct rpc_pipe_client *lsa_pipe = NULL;
76 struct policy_handle pol;
77 NTSTATUS status, result;
78 union lsa_PolicyInformation *info = NULL;
79 struct dcerpc_binding_handle *b;
81 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
83 if (!NT_STATUS_IS_OK(status)) {
84 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
88 b = lsa_pipe->binding_handle;
90 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
91 SEC_FLAG_MAXIMUM_ALLOWED,
93 if (!NT_STATUS_IS_OK(status)) {
94 d_fprintf(stderr, "open_policy %s: %s\n",
100 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
102 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
105 if (!NT_STATUS_IS_OK(status)) {
106 d_fprintf(stderr, "lsaquery %s: %s\n",
111 if (!NT_STATUS_IS_OK(result)) {
112 d_fprintf(stderr, "lsaquery %s: %s\n",
118 *domain_name = info->account_domain.name.string;
119 *domain_sid = info->account_domain.sid;
121 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
122 TALLOC_FREE(lsa_pipe);
128 * Run a single RPC command, from start to finish.
130 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
131 * @param conn_flag a NET_FLAG_ combination. Passed to
132 * net_make_ipc_connection.
133 * @param argc Standard main() style argc.
134 * @param argv Standard main() style argv. Initial components are already
136 * @return A shell status integer (0 for success).
139 int run_rpc_command(struct net_context *c,
140 struct cli_state *cli_arg,
141 const struct ndr_syntax_id *interface,
147 struct cli_state *cli = NULL;
148 struct rpc_pipe_client *pipe_hnd = NULL;
151 struct dom_sid *domain_sid;
152 const char *domain_name;
155 /* make use of cli_state handed over as an argument, if possible */
157 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
158 if (!NT_STATUS_IS_OK(nt_status)) {
159 DEBUG(1, ("failed to make ipc connection: %s\n",
160 nt_errstr(nt_status)));
173 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
174 DEBUG(0, ("talloc_init() failed\n"));
178 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
180 if (!NT_STATUS_IS_OK(nt_status)) {
184 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
185 if (lp_client_schannel()
186 && (ndr_syntax_id_equal(interface,
187 &ndr_table_netlogon.syntax_id))) {
188 /* Always try and create an schannel netlogon pipe. */
189 nt_status = cli_rpc_pipe_open_schannel(
190 cli, interface, NCACN_NP,
191 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
193 if (!NT_STATUS_IS_OK(nt_status)) {
194 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
195 nt_errstr(nt_status) ));
199 if (conn_flags & NET_FLAGS_SEAL) {
200 nt_status = cli_rpc_pipe_open_ntlmssp(
202 (conn_flags & NET_FLAGS_TCP) ?
203 NCACN_IP_TCP : NCACN_NP,
204 DCERPC_AUTH_LEVEL_PRIVACY,
205 lp_workgroup(), c->opt_user_name,
206 c->opt_password, &pipe_hnd);
208 nt_status = cli_rpc_pipe_open_noauth(
212 if (!NT_STATUS_IS_OK(nt_status)) {
213 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
214 get_pipe_name_from_syntax(
215 talloc_tos(), interface),
216 nt_errstr(nt_status) ));
222 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
224 if (!NT_STATUS_IS_OK(nt_status)) {
225 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
228 DEBUG(5, ("rpc command function succedded\n"));
231 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
233 TALLOC_FREE(pipe_hnd);
238 /* close the connection only if it was opened here */
243 talloc_destroy(mem_ctx);
248 * Force a change of the trust acccount password.
250 * All parameters are provided by the run_rpc_command function, except for
251 * argc, argv which are passed through.
253 * @param domain_sid The domain sid acquired from the remote server.
254 * @param cli A cli_state connected to the server.
255 * @param mem_ctx Talloc context, destroyed on completion of the function.
256 * @param argc Standard main() style argc.
257 * @param argv Standard main() style argv. Initial components are already
260 * @return Normal NTSTATUS return.
263 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
264 const struct dom_sid *domain_sid,
265 const char *domain_name,
266 struct cli_state *cli,
267 struct rpc_pipe_client *pipe_hnd,
274 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
275 if (!NT_STATUS_IS_OK(status)) {
276 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
285 * Force a change of the trust acccount password.
287 * @param argc Standard main() style argc.
288 * @param argv Standard main() style argv. Initial components are already
291 * @return A shell status integer (0 for success).
294 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
296 if (c->display_usage) {
298 "net rpc changetrustpw\n"
301 _("Change the machine trust password"));
305 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
306 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
307 rpc_changetrustpw_internals,
312 * Join a domain, the old way.
314 * This uses 'machinename' as the inital password, and changes it.
316 * The password should be created with 'server manager' or equiv first.
318 * All parameters are provided by the run_rpc_command function, except for
319 * argc, argv which are passed through.
321 * @param domain_sid The domain sid acquired from the remote server.
322 * @param cli A cli_state connected to the server.
323 * @param mem_ctx Talloc context, destroyed on completion of the function.
324 * @param argc Standard main() style argc.
325 * @param argv Standard main() style argv. Initial components are already
328 * @return Normal NTSTATUS return.
331 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
332 const struct dom_sid *domain_sid,
333 const char *domain_name,
334 struct cli_state *cli,
335 struct rpc_pipe_client *pipe_hnd,
341 fstring trust_passwd;
342 unsigned char orig_trust_passwd_hash[16];
344 enum netr_SchannelType sec_channel_type;
346 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
348 if (!NT_STATUS_IS_OK(result)) {
349 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
352 nt_errstr(result) ));
357 check what type of join - if the user want's to join as
358 a BDC, the server must agree that we are a BDC.
361 sec_channel_type = get_sec_channel_type(argv[0]);
363 sec_channel_type = get_sec_channel_type(NULL);
366 fstrcpy(trust_passwd, global_myname());
367 strlower_m(trust_passwd);
370 * Machine names can be 15 characters, but the max length on
371 * a password is 14. --jerry
374 trust_passwd[14] = '\0';
376 E_md4hash(trust_passwd, orig_trust_passwd_hash);
378 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
380 orig_trust_passwd_hash,
383 if (NT_STATUS_IS_OK(result))
384 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
387 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
388 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
389 result = NT_STATUS_UNSUCCESSFUL;
396 * Join a domain, the old way.
398 * @param argc Standard main() style argc.
399 * @param argv Standard main() style argv. Initial components are already
402 * @return A shell status integer (0 for success).
405 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
407 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
408 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
409 rpc_oldjoin_internals,
414 * Join a domain, the old way. This function exists to allow
415 * the message to be displayed when oldjoin was explicitly
416 * requested, but not when it was implied by "net rpc join".
418 * @param argc Standard main() style argc.
419 * @param argv Standard main() style argv. Initial components are already
422 * @return A shell status integer (0 for success).
425 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
429 if (c->display_usage) {
434 _("Join a domain the old way"));
438 rc = net_rpc_perform_oldjoin(c, argc, argv);
441 d_fprintf(stderr, _("Failed to join domain\n"));
448 * 'net rpc join' entrypoint.
449 * @param argc Standard main() style argc.
450 * @param argv Standard main() style argv. Initial components are already
453 * Main 'net_rpc_join()' (where the admin username/password is used) is
455 * Try to just change the password, but if that doesn't work, use/prompt
456 * for a username/password.
459 int net_rpc_join(struct net_context *c, int argc, const char **argv)
461 if (c->display_usage) {
464 _("net rpc join -U <username>[%%password] <type>\n"
466 " username\tName of the admin user"
467 " password\tPassword of the admin user, will "
468 "prompt if not specified\n"
469 " type\tCan be one of the following:\n"
470 "\t\tMEMBER\tJoin as member server (default)\n"
471 "\t\tBDC\tJoin as BDC\n"
472 "\t\tPDC\tJoin as PDC\n"));
476 if (lp_server_role() == ROLE_STANDALONE) {
477 d_printf(_("cannot join as standalone machine\n"));
481 if (strlen(global_myname()) > 15) {
482 d_printf(_("Our netbios name can be at most 15 chars long, "
483 "\"%s\" is %u chars long\n"),
484 global_myname(), (unsigned int)strlen(global_myname()));
488 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
491 return net_rpc_join_newstyle(c, argc, argv);
495 * display info about a rpc domain
497 * All parameters are provided by the run_rpc_command function, except for
498 * argc, argv which are passed through.
500 * @param domain_sid The domain sid acquired from the remote server
501 * @param cli A cli_state connected to the server.
502 * @param mem_ctx Talloc context, destroyed on completion of the function.
503 * @param argc Standard main() style argc.
504 * @param argv Standard main() style argv. Initial components are already
507 * @return Normal NTSTATUS return.
510 NTSTATUS rpc_info_internals(struct net_context *c,
511 const struct dom_sid *domain_sid,
512 const char *domain_name,
513 struct cli_state *cli,
514 struct rpc_pipe_client *pipe_hnd,
519 struct policy_handle connect_pol, domain_pol;
520 NTSTATUS status, result;
521 union samr_DomainInfo *info = NULL;
523 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
525 sid_to_fstring(sid_str, domain_sid);
527 /* Get sam policy handle */
528 status = dcerpc_samr_Connect2(b, mem_ctx,
530 MAXIMUM_ALLOWED_ACCESS,
533 if (!NT_STATUS_IS_OK(status)) {
534 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
539 if (!NT_STATUS_IS_OK(result)) {
541 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
546 /* Get domain policy handle */
547 status = dcerpc_samr_OpenDomain(b, mem_ctx,
549 MAXIMUM_ALLOWED_ACCESS,
550 CONST_DISCARD(struct dom_sid2 *, domain_sid),
553 if (!NT_STATUS_IS_OK(status)) {
554 d_fprintf(stderr, _("Could not open domain: %s\n"),
558 if (!NT_STATUS_IS_OK(result)) {
560 d_fprintf(stderr, _("Could not open domain: %s\n"),
565 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
570 if (!NT_STATUS_IS_OK(status)) {
574 if (NT_STATUS_IS_OK(result)) {
575 d_printf(_("Domain Name: %s\n"),
576 info->general.domain_name.string);
577 d_printf(_("Domain SID: %s\n"), sid_str);
578 d_printf(_("Sequence number: %llu\n"),
579 (unsigned long long)info->general.sequence_num);
580 d_printf(_("Num users: %u\n"), info->general.num_users);
581 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
582 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
590 * 'net rpc info' entrypoint.
591 * @param argc Standard main() style argc.
592 * @param argv Standard main() style argv. Initial components are already
596 int net_rpc_info(struct net_context *c, int argc, const char **argv)
598 if (c->display_usage) {
603 _("Display information about the domain"));
607 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
608 NET_FLAGS_PDC, rpc_info_internals,
613 * Fetch domain SID into the local secrets.tdb.
615 * All parameters are provided by the run_rpc_command function, except for
616 * argc, argv which are passed through.
618 * @param domain_sid The domain sid acquired from the remote server.
619 * @param cli A cli_state connected to the server.
620 * @param mem_ctx Talloc context, destroyed on completion of the function.
621 * @param argc Standard main() style argc.
622 * @param argv Standard main() style argv. Initial components are already
625 * @return Normal NTSTATUS return.
628 static NTSTATUS rpc_getsid_internals(struct net_context *c,
629 const struct dom_sid *domain_sid,
630 const char *domain_name,
631 struct cli_state *cli,
632 struct rpc_pipe_client *pipe_hnd,
639 sid_to_fstring(sid_str, domain_sid);
640 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
641 sid_str, domain_name);
643 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
644 DEBUG(0,("Can't store domain SID\n"));
645 return NT_STATUS_UNSUCCESSFUL;
652 * 'net rpc getsid' entrypoint.
653 * @param argc Standard main() style argc.
654 * @param argv Standard main() style argv. Initial components are already
658 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
660 int conn_flags = NET_FLAGS_PDC;
662 if (!c->opt_user_specified) {
663 conn_flags |= NET_FLAGS_ANONYMOUS;
666 if (c->display_usage) {
671 _("Fetch domain SID into local secrets.tdb"));
675 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
677 rpc_getsid_internals,
681 /****************************************************************************/
684 * Basic usage function for 'net rpc user'.
685 * @param argc Standard main() style argc.
686 * @param argv Standard main() style argv. Initial components are already
690 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
692 return net_user_usage(c, argc, argv);
696 * Add a new user to a remote RPC server.
698 * @param argc Standard main() style argc.
699 * @param argv Standard main() style argv. Initial components are already
702 * @return A shell status integer (0 for success).
705 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
707 NET_API_STATUS status;
708 struct USER_INFO_1 info1;
709 uint32_t parm_error = 0;
711 if (argc < 1 || c->display_usage) {
712 rpc_user_usage(c, argc, argv);
718 info1.usri1_name = argv[0];
720 info1.usri1_password = argv[1];
723 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
726 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
727 argv[0], libnetapi_get_error_string(c->netapi_ctx,
731 d_printf(_("Added user '%s'.\n"), argv[0]);
738 * Rename a user on a remote RPC server.
740 * @param argc Standard main() style argc.
741 * @param argv Standard main() style argv. Initial components are already
744 * @return A shell status integer (0 for success).
747 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
749 NET_API_STATUS status;
750 struct USER_INFO_0 u0;
751 uint32_t parm_err = 0;
753 if (argc != 2 || c->display_usage) {
754 rpc_user_usage(c, argc, argv);
758 u0.usri0_name = argv[1];
760 status = NetUserSetInfo(c->opt_host, argv[0],
761 0, (uint8_t *)&u0, &parm_err);
764 _("Failed to rename user from %s to %s - %s\n"),
766 libnetapi_get_error_string(c->netapi_ctx, status));
768 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
775 * Set a user's primary group
777 * @param argc Standard main() style argc.
778 * @param argv Standard main() style argv. Initial components are already
781 * @return A shell status integer (0 for success).
784 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
787 NET_API_STATUS status;
789 struct GROUP_INFO_2 *g2;
790 struct USER_INFO_1051 u1051;
791 uint32_t parm_err = 0;
793 if (argc != 2 || c->display_usage) {
794 rpc_user_usage(c, argc, argv);
798 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
800 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
802 libnetapi_get_error_string(c->netapi_ctx, status));
805 g2 = (struct GROUP_INFO_2 *)buffer;
807 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
809 NetApiBufferFree(buffer);
811 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
812 (uint8_t *)&u1051, &parm_err);
815 _("Failed to set user's primary group %s to %s - "
816 "%s\n"), argv[0], argv[1],
817 libnetapi_get_error_string(c->netapi_ctx, status));
819 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
826 * Delete a user from a remote RPC server.
828 * @param argc Standard main() style argc.
829 * @param argv Standard main() style argv. Initial components are already
832 * @return A shell status integer (0 for success).
835 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
837 NET_API_STATUS status;
839 if (argc < 1 || c->display_usage) {
840 rpc_user_usage(c, argc, argv);
844 status = NetUserDel(c->opt_host, argv[0]);
847 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
849 libnetapi_get_error_string(c->netapi_ctx, status));
852 d_printf(_("Deleted user '%s'.\n"), argv[0]);
859 * Set a user's password on a remote RPC server.
861 * @param argc Standard main() style argc.
862 * @param argv Standard main() style argv. Initial components are already
865 * @return A shell status integer (0 for success).
868 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
870 NET_API_STATUS status;
872 struct USER_INFO_1003 u1003;
873 uint32_t parm_err = 0;
876 if (argc < 1 || c->display_usage) {
877 rpc_user_usage(c, argc, argv);
882 u1003.usri1003_password = argv[1];
884 ret = asprintf(&prompt, _("Enter new password for %s:"),
889 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
891 if (u1003.usri1003_password == NULL) {
896 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
898 /* Display results */
901 _("Failed to set password for '%s' with error: %s.\n"),
902 argv[0], libnetapi_get_error_string(c->netapi_ctx,
911 * List a user's groups from a remote RPC server.
913 * @param argc Standard main() style argc.
914 * @param argv Standard main() style argv. Initial components are already
917 * @return A shell status integer (0 for success)
920 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
923 NET_API_STATUS status;
924 struct GROUP_USERS_INFO_0 *u0 = NULL;
925 uint32_t entries_read = 0;
926 uint32_t total_entries = 0;
930 if (argc < 1 || c->display_usage) {
931 rpc_user_usage(c, argc, argv);
935 status = NetUserGetGroups(c->opt_host,
938 (uint8_t **)(void *)&u0,
944 _("Failed to get groups for '%s' with error: %s.\n"),
945 argv[0], libnetapi_get_error_string(c->netapi_ctx,
950 for (i=0; i < entries_read; i++) {
951 printf("%s\n", u0->grui0_name);
959 * List users on a remote RPC server.
961 * All parameters are provided by the run_rpc_command function, except for
962 * argc, argv which are passed through.
964 * @param domain_sid The domain sid acquired from the remote server.
965 * @param cli A cli_state connected to the server.
966 * @param mem_ctx Talloc context, destroyed on completion of the function.
967 * @param argc Standard main() style argc.
968 * @param argv Standard main() style argv. Initial components are already
971 * @return Normal NTSTATUS return.
974 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
976 NET_API_STATUS status;
977 uint32_t start_idx=0, num_entries, i, loop_count = 0;
978 struct NET_DISPLAY_USER *info = NULL;
981 /* Query domain users */
982 if (c->opt_long_list_entries)
983 d_printf(_("\nUser name Comment"
984 "\n-----------------------------\n"));
986 uint32_t max_entries, max_size;
988 dcerpc_get_query_dispinfo_params(
989 loop_count, &max_entries, &max_size);
991 status = NetQueryDisplayInformation(c->opt_host,
998 if (status != 0 && status != ERROR_MORE_DATA) {
1002 info = (struct NET_DISPLAY_USER *)buffer;
1004 for (i = 0; i < num_entries; i++) {
1006 if (c->opt_long_list_entries)
1007 printf("%-21.21s %s\n", info->usri1_name,
1008 info->usri1_comment);
1010 printf("%s\n", info->usri1_name);
1014 NetApiBufferFree(buffer);
1017 start_idx += num_entries;
1019 } while (status == ERROR_MORE_DATA);
1025 * 'net rpc user' entrypoint.
1026 * @param argc Standard main() style argc.
1027 * @param argv Standard main() style argv. Initial components are already
1031 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1033 NET_API_STATUS status;
1035 struct functable func[] = {
1040 N_("Add specified user"),
1041 N_("net rpc user add\n"
1042 " Add specified user")
1048 N_("List domain groups of user"),
1049 N_("net rpc user info\n"
1050 " List domain groups of user")
1056 N_("Remove specified user"),
1057 N_("net rpc user delete\n"
1058 " Remove specified user")
1064 N_("Change user password"),
1065 N_("net rpc user password\n"
1066 " Change user password")
1072 N_("Rename specified user"),
1073 N_("net rpc user rename\n"
1074 " Rename specified user")
1078 rpc_user_setprimarygroup,
1080 "Set a user's primary group",
1081 "net rpc user setprimarygroup\n"
1082 " Set a user's primary group"
1084 {NULL, NULL, 0, NULL, NULL}
1087 status = libnetapi_net_init(&c->netapi_ctx);
1091 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1092 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1093 if (c->opt_kerberos) {
1094 libnetapi_set_use_kerberos(c->netapi_ctx);
1098 if (c->display_usage) {
1103 _("List all users"));
1104 net_display_usage_from_functable(func);
1108 return rpc_user_list(c, argc, argv);
1111 return net_run_function(c, argc, argv, "net rpc user", func);
1114 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1115 TALLOC_CTX *mem_ctx,
1116 struct rpc_sh_ctx *ctx,
1117 struct rpc_pipe_client *pipe_hnd,
1118 int argc, const char **argv)
1120 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1123 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1124 TALLOC_CTX *mem_ctx,
1125 struct rpc_sh_ctx *ctx,
1126 struct rpc_pipe_client *pipe_hnd,
1127 int argc, const char **argv)
1129 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1132 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1133 TALLOC_CTX *mem_ctx,
1134 struct rpc_sh_ctx *ctx,
1135 struct rpc_pipe_client *pipe_hnd,
1136 int argc, const char **argv,
1138 struct net_context *c,
1139 TALLOC_CTX *mem_ctx,
1140 struct rpc_sh_ctx *ctx,
1141 struct rpc_pipe_client *pipe_hnd,
1142 struct policy_handle *user_hnd,
1143 int argc, const char **argv))
1145 struct policy_handle connect_pol, domain_pol, user_pol;
1146 NTSTATUS status, result;
1149 enum lsa_SidType type;
1150 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1153 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1155 return NT_STATUS_INVALID_PARAMETER;
1158 ZERO_STRUCT(connect_pol);
1159 ZERO_STRUCT(domain_pol);
1160 ZERO_STRUCT(user_pol);
1162 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1163 argv[0], NULL, NULL, &sid, &type);
1164 if (!NT_STATUS_IS_OK(status)) {
1165 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1170 if (type != SID_NAME_USER) {
1171 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1172 sid_type_lookup(type));
1173 status = NT_STATUS_NO_SUCH_USER;
1177 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1178 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1179 status = NT_STATUS_NO_SUCH_USER;
1183 status = dcerpc_samr_Connect2(b, mem_ctx,
1185 MAXIMUM_ALLOWED_ACCESS,
1188 if (!NT_STATUS_IS_OK(status)) {
1191 if (!NT_STATUS_IS_OK(result)) {
1196 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1198 MAXIMUM_ALLOWED_ACCESS,
1202 if (!NT_STATUS_IS_OK(status)) {
1205 if (!NT_STATUS_IS_OK(result)) {
1210 status = dcerpc_samr_OpenUser(b, mem_ctx,
1212 MAXIMUM_ALLOWED_ACCESS,
1216 if (!NT_STATUS_IS_OK(status)) {
1219 if (!NT_STATUS_IS_OK(result)) {
1224 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1227 if (is_valid_policy_hnd(&user_pol)) {
1228 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1230 if (is_valid_policy_hnd(&domain_pol)) {
1231 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1233 if (is_valid_policy_hnd(&connect_pol)) {
1234 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1239 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1240 TALLOC_CTX *mem_ctx,
1241 struct rpc_sh_ctx *ctx,
1242 struct rpc_pipe_client *pipe_hnd,
1243 struct policy_handle *user_hnd,
1244 int argc, const char **argv)
1246 NTSTATUS status, result;
1247 union samr_UserInfo *info = NULL;
1248 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1251 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1253 return NT_STATUS_INVALID_PARAMETER;
1256 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1261 if (!NT_STATUS_IS_OK(status)) {
1264 if (!NT_STATUS_IS_OK(result)) {
1268 d_printf(_("user rid: %d, group rid: %d\n"),
1270 info->info21.primary_gid);
1275 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1276 TALLOC_CTX *mem_ctx,
1277 struct rpc_sh_ctx *ctx,
1278 struct rpc_pipe_client *pipe_hnd,
1279 int argc, const char **argv)
1281 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1282 rpc_sh_user_show_internals);
1285 #define FETCHSTR(name, rec) \
1286 do { if (strequal(ctx->thiscmd, name)) { \
1287 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1290 #define SETSTR(name, rec, flag) \
1291 do { if (strequal(ctx->thiscmd, name)) { \
1292 init_lsa_String(&(info->info21.rec), argv[0]); \
1293 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1296 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1297 TALLOC_CTX *mem_ctx,
1298 struct rpc_sh_ctx *ctx,
1299 struct rpc_pipe_client *pipe_hnd,
1300 struct policy_handle *user_hnd,
1301 int argc, const char **argv)
1303 NTSTATUS status, result;
1304 const char *username;
1305 const char *oldval = "";
1306 union samr_UserInfo *info = NULL;
1307 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1310 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1311 _("Usage:"), ctx->whoami);
1312 return NT_STATUS_INVALID_PARAMETER;
1315 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1320 if (!NT_STATUS_IS_OK(status)) {
1323 if (!NT_STATUS_IS_OK(result)) {
1327 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1329 FETCHSTR("fullname", full_name);
1330 FETCHSTR("homedir", home_directory);
1331 FETCHSTR("homedrive", home_drive);
1332 FETCHSTR("logonscript", logon_script);
1333 FETCHSTR("profilepath", profile_path);
1334 FETCHSTR("description", description);
1337 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1341 if (strcmp(argv[0], "NULL") == 0) {
1345 ZERO_STRUCT(info->info21);
1347 SETSTR("fullname", full_name, FULL_NAME);
1348 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1349 SETSTR("homedrive", home_drive, HOME_DRIVE);
1350 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1351 SETSTR("profilepath", profile_path, PROFILE_PATH);
1352 SETSTR("description", description, DESCRIPTION);
1354 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1359 if (!NT_STATUS_IS_OK(status)) {
1365 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1366 ctx->thiscmd, oldval, argv[0]);
1373 #define HANDLEFLG(name, rec) \
1374 do { if (strequal(ctx->thiscmd, name)) { \
1375 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1377 newflags = oldflags | ACB_##rec; \
1379 newflags = oldflags & ~ACB_##rec; \
1382 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1383 TALLOC_CTX *mem_ctx,
1384 struct rpc_sh_ctx *ctx,
1385 struct rpc_pipe_client *pipe_hnd,
1386 int argc, const char **argv)
1388 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1389 rpc_sh_user_str_edit_internals);
1392 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1393 TALLOC_CTX *mem_ctx,
1394 struct rpc_sh_ctx *ctx,
1395 struct rpc_pipe_client *pipe_hnd,
1396 struct policy_handle *user_hnd,
1397 int argc, const char **argv)
1399 NTSTATUS status, result;
1400 const char *username;
1401 const char *oldval = "unknown";
1402 uint32 oldflags, newflags;
1404 union samr_UserInfo *info = NULL;
1405 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1408 ((argc == 1) && !strequal(argv[0], "yes") &&
1409 !strequal(argv[0], "no"))) {
1410 /* TRANSATORS: The yes|no here are program keywords. Please do
1412 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1414 return NT_STATUS_INVALID_PARAMETER;
1417 newval = strequal(argv[0], "yes");
1419 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1424 if (!NT_STATUS_IS_OK(status)) {
1427 if (!NT_STATUS_IS_OK(result)) {
1431 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1432 oldflags = info->info21.acct_flags;
1433 newflags = info->info21.acct_flags;
1435 HANDLEFLG("disabled", DISABLED);
1436 HANDLEFLG("pwnotreq", PWNOTREQ);
1437 HANDLEFLG("autolock", AUTOLOCK);
1438 HANDLEFLG("pwnoexp", PWNOEXP);
1441 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1446 ZERO_STRUCT(info->info21);
1448 info->info21.acct_flags = newflags;
1449 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1451 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1456 if (!NT_STATUS_IS_OK(status)) {
1460 if (NT_STATUS_IS_OK(result)) {
1461 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1462 ctx->thiscmd, oldval, argv[0]);
1470 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1471 TALLOC_CTX *mem_ctx,
1472 struct rpc_sh_ctx *ctx,
1473 struct rpc_pipe_client *pipe_hnd,
1474 int argc, const char **argv)
1476 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1477 rpc_sh_user_flag_edit_internals);
1480 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1481 TALLOC_CTX *mem_ctx,
1482 struct rpc_sh_ctx *ctx)
1484 static struct rpc_sh_cmd cmds[] = {
1486 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1487 N_("Show/Set a user's full name") },
1489 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1490 N_("Show/Set a user's home directory") },
1492 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1493 N_("Show/Set a user's home drive") },
1495 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1496 N_("Show/Set a user's logon script") },
1498 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1499 N_("Show/Set a user's profile path") },
1501 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1502 N_("Show/Set a user's description") },
1504 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1505 N_("Show/Set whether a user is disabled") },
1507 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1508 N_("Show/Set whether a user locked out") },
1510 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1511 N_("Show/Set whether a user does not need a password") },
1513 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1514 N_("Show/Set whether a user's password does not expire") },
1516 { NULL, NULL, 0, NULL, NULL }
1522 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1523 TALLOC_CTX *mem_ctx,
1524 struct rpc_sh_ctx *ctx)
1526 static struct rpc_sh_cmd cmds[] = {
1528 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1529 N_("List available users") },
1531 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1532 N_("List the domain groups a user is member of") },
1534 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1535 N_("Show info about a user") },
1537 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1538 N_("Show/Modify a user's fields") },
1540 { NULL, NULL, 0, NULL, NULL }
1546 /****************************************************************************/
1549 * Basic usage function for 'net rpc group'.
1550 * @param argc Standard main() style argc.
1551 * @param argv Standard main() style argv. Initial components are already
1555 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1557 return net_group_usage(c, argc, argv);
1561 * Delete group on a remote RPC server.
1563 * All parameters are provided by the run_rpc_command function, except for
1564 * argc, argv which are passed through.
1566 * @param domain_sid The domain sid acquired from the remote server.
1567 * @param cli A cli_state connected to the server.
1568 * @param mem_ctx Talloc context, destroyed on completion of the function.
1569 * @param argc Standard main() style argc.
1570 * @param argv Standard main() style argv. Initial components are already
1573 * @return Normal NTSTATUS return.
1576 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1577 const struct dom_sid *domain_sid,
1578 const char *domain_name,
1579 struct cli_state *cli,
1580 struct rpc_pipe_client *pipe_hnd,
1581 TALLOC_CTX *mem_ctx,
1585 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1586 bool group_is_primary = false;
1587 NTSTATUS status, result;
1589 struct samr_RidAttrArray *rids = NULL;
1592 /* struct samr_RidWithAttribute *user_gids; */
1593 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1595 struct samr_Ids group_rids, name_types;
1596 struct lsa_String lsa_acct_name;
1597 union samr_UserInfo *info = NULL;
1599 if (argc < 1 || c->display_usage) {
1600 rpc_group_usage(c, argc,argv);
1601 return NT_STATUS_OK; /* ok? */
1604 status = dcerpc_samr_Connect2(b, mem_ctx,
1606 MAXIMUM_ALLOWED_ACCESS,
1609 if (!NT_STATUS_IS_OK(status)) {
1610 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1614 if (!NT_STATUS_IS_OK(result)) {
1616 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1620 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1622 MAXIMUM_ALLOWED_ACCESS,
1623 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1626 if (!NT_STATUS_IS_OK(status)) {
1627 d_fprintf(stderr, _("Request open_domain failed\n"));
1631 if (!NT_STATUS_IS_OK(result)) {
1633 d_fprintf(stderr, _("Request open_domain failed\n"));
1637 init_lsa_String(&lsa_acct_name, argv[0]);
1639 status = dcerpc_samr_LookupNames(b, mem_ctx,
1646 if (!NT_STATUS_IS_OK(status)) {
1647 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1651 if (!NT_STATUS_IS_OK(result)) {
1653 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1657 switch (name_types.ids[0])
1659 case SID_NAME_DOM_GRP:
1660 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1662 MAXIMUM_ALLOWED_ACCESS,
1666 if (!NT_STATUS_IS_OK(status)) {
1667 d_fprintf(stderr, _("Request open_group failed"));
1671 if (!NT_STATUS_IS_OK(result)) {
1673 d_fprintf(stderr, _("Request open_group failed"));
1677 group_rid = group_rids.ids[0];
1679 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1683 if (!NT_STATUS_IS_OK(status)) {
1685 _("Unable to query group members of %s"),
1690 if (!NT_STATUS_IS_OK(result)) {
1693 _("Unable to query group members of %s"),
1698 if (c->opt_verbose) {
1700 _("Domain Group %s (rid: %d) has %d members\n"),
1701 argv[0],group_rid, rids->count);
1704 /* Check if group is anyone's primary group */
1705 for (i = 0; i < rids->count; i++)
1707 status = dcerpc_samr_OpenUser(b, mem_ctx,
1709 MAXIMUM_ALLOWED_ACCESS,
1713 if (!NT_STATUS_IS_OK(status)) {
1715 _("Unable to open group member %d\n"),
1720 if (!NT_STATUS_IS_OK(result)) {
1723 _("Unable to open group member %d\n"),
1728 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1733 if (!NT_STATUS_IS_OK(status)) {
1735 _("Unable to lookup userinfo for group "
1741 if (!NT_STATUS_IS_OK(result)) {
1744 _("Unable to lookup userinfo for group "
1750 if (info->info21.primary_gid == group_rid) {
1751 if (c->opt_verbose) {
1752 d_printf(_("Group is primary group "
1754 info->info21.account_name.string);
1756 group_is_primary = true;
1759 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1762 if (group_is_primary) {
1763 d_fprintf(stderr, _("Unable to delete group because "
1764 "some of it's members have it as primary "
1766 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1770 /* remove all group members */
1771 for (i = 0; i < rids->count; i++)
1774 d_printf(_("Remove group member %d..."),
1776 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1780 if (!NT_STATUS_IS_OK(status)) {
1784 if (NT_STATUS_IS_OK(result)) {
1786 d_printf(_("ok\n"));
1789 d_printf("%s\n", _("failed"));
1794 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1797 if (!NT_STATUS_IS_OK(status)) {
1804 /* removing a local group is easier... */
1805 case SID_NAME_ALIAS:
1806 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1808 MAXIMUM_ALLOWED_ACCESS,
1812 if (!NT_STATUS_IS_OK(status)) {
1813 d_fprintf(stderr, _("Request open_alias failed\n"));
1816 if (!NT_STATUS_IS_OK(result)) {
1818 d_fprintf(stderr, _("Request open_alias failed\n"));
1822 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1825 if (!NT_STATUS_IS_OK(status)) {
1833 d_fprintf(stderr, _("%s is of type %s. This command is only "
1834 "for deleting local or global groups\n"),
1835 argv[0],sid_type_lookup(name_types.ids[0]));
1836 status = NT_STATUS_UNSUCCESSFUL;
1840 if (NT_STATUS_IS_OK(status)) {
1842 d_printf(_("Deleted %s '%s'\n"),
1843 sid_type_lookup(name_types.ids[0]), argv[0]);
1845 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1846 get_friendly_nt_error_msg(status));
1854 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1856 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1857 rpc_group_delete_internals, argc,argv);
1860 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1862 NET_API_STATUS status;
1863 struct GROUP_INFO_1 info1;
1864 uint32_t parm_error = 0;
1866 if (argc != 1 || c->display_usage) {
1867 rpc_group_usage(c, argc, argv);
1873 info1.grpi1_name = argv[0];
1874 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1875 info1.grpi1_comment = c->opt_comment;
1878 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1882 _("Failed to add group '%s' with error: %s.\n"),
1883 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1887 d_printf(_("Added group '%s'.\n"), argv[0]);
1893 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1895 NET_API_STATUS status;
1896 struct LOCALGROUP_INFO_1 info1;
1897 uint32_t parm_error = 0;
1899 if (argc != 1 || c->display_usage) {
1900 rpc_group_usage(c, argc, argv);
1906 info1.lgrpi1_name = argv[0];
1907 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1908 info1.lgrpi1_comment = c->opt_comment;
1911 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1915 _("Failed to add alias '%s' with error: %s.\n"),
1916 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1920 d_printf(_("Added alias '%s'.\n"), argv[0]);
1926 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1928 if (c->opt_localgroup)
1929 return rpc_alias_add_internals(c, argc, argv);
1931 return rpc_group_add_internals(c, argc, argv);
1934 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1935 TALLOC_CTX *mem_ctx,
1937 struct dom_sid *sid,
1938 enum lsa_SidType *type)
1940 struct dom_sid *sids = NULL;
1941 enum lsa_SidType *types = NULL;
1942 struct rpc_pipe_client *pipe_hnd = NULL;
1943 struct policy_handle lsa_pol;
1944 NTSTATUS status, result;
1945 struct dcerpc_binding_handle *b;
1947 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1949 if (!NT_STATUS_IS_OK(status)) {
1953 b = pipe_hnd->binding_handle;
1955 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1956 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1958 if (!NT_STATUS_IS_OK(status)) {
1962 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1963 &name, NULL, 1, &sids, &types);
1965 if (NT_STATUS_IS_OK(status)) {
1966 sid_copy(sid, &sids[0]);
1970 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1974 TALLOC_FREE(pipe_hnd);
1977 if (!NT_STATUS_IS_OK(status) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1979 /* Try as S-1-5-whatever */
1981 struct dom_sid tmp_sid;
1983 if (string_to_sid(&tmp_sid, name)) {
1984 sid_copy(sid, &tmp_sid);
1985 *type = SID_NAME_UNKNOWN;
1986 status = NT_STATUS_OK;
1993 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1994 TALLOC_CTX *mem_ctx,
1995 const struct dom_sid *group_sid,
1998 struct policy_handle connect_pol, domain_pol;
1999 NTSTATUS status, result;
2001 struct policy_handle group_pol;
2002 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2004 struct samr_Ids rids, rid_types;
2005 struct lsa_String lsa_acct_name;
2009 sid_copy(&sid, group_sid);
2011 if (!sid_split_rid(&sid, &group_rid)) {
2012 return NT_STATUS_UNSUCCESSFUL;
2015 /* Get sam policy handle */
2016 status = dcerpc_samr_Connect2(b, mem_ctx,
2018 MAXIMUM_ALLOWED_ACCESS,
2021 if (!NT_STATUS_IS_OK(status)) {
2024 if (!NT_STATUS_IS_OK(result)) {
2028 /* Get domain policy handle */
2029 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2031 MAXIMUM_ALLOWED_ACCESS,
2035 if (!NT_STATUS_IS_OK(status)) {
2038 if (!NT_STATUS_IS_OK(result)) {
2042 init_lsa_String(&lsa_acct_name, member);
2044 status = dcerpc_samr_LookupNames(b, mem_ctx,
2051 if (!NT_STATUS_IS_OK(status)) {
2052 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2057 if (!NT_STATUS_IS_OK(result)) {
2059 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2064 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2066 MAXIMUM_ALLOWED_ACCESS,
2070 if (!NT_STATUS_IS_OK(status)) {
2074 if (!NT_STATUS_IS_OK(result)) {
2079 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2082 0x0005, /* unknown flags */
2084 if (!NT_STATUS_IS_OK(status)) {
2091 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2095 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2096 TALLOC_CTX *mem_ctx,
2097 const struct dom_sid *alias_sid,
2100 struct policy_handle connect_pol, domain_pol;
2101 NTSTATUS status, result;
2103 struct policy_handle alias_pol;
2104 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2106 struct dom_sid member_sid;
2107 enum lsa_SidType member_type;
2111 sid_copy(&sid, alias_sid);
2113 if (!sid_split_rid(&sid, &alias_rid)) {
2114 return NT_STATUS_UNSUCCESSFUL;
2117 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2118 member, &member_sid, &member_type);
2120 if (!NT_STATUS_IS_OK(result)) {
2121 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2126 /* Get sam policy handle */
2127 status = dcerpc_samr_Connect2(b, mem_ctx,
2129 MAXIMUM_ALLOWED_ACCESS,
2132 if (!NT_STATUS_IS_OK(status)) {
2135 if (!NT_STATUS_IS_OK(result)) {
2140 /* Get domain policy handle */
2141 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2143 MAXIMUM_ALLOWED_ACCESS,
2147 if (!NT_STATUS_IS_OK(status)) {
2150 if (!NT_STATUS_IS_OK(result)) {
2155 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2157 MAXIMUM_ALLOWED_ACCESS,
2161 if (!NT_STATUS_IS_OK(status)) {
2164 if (!NT_STATUS_IS_OK(result)) {
2168 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2172 if (!NT_STATUS_IS_OK(status)) {
2179 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2183 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2184 const struct dom_sid *domain_sid,
2185 const char *domain_name,
2186 struct cli_state *cli,
2187 struct rpc_pipe_client *pipe_hnd,
2188 TALLOC_CTX *mem_ctx,
2192 struct dom_sid group_sid;
2193 enum lsa_SidType group_type;
2195 if (argc != 2 || c->display_usage) {
2198 _("net rpc group addmem <group> <member>\n"
2199 " Add a member to a group\n"
2200 " group\tGroup to add member to\n"
2201 " member\tMember to add to group\n"));
2202 return NT_STATUS_UNSUCCESSFUL;
2205 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2206 &group_sid, &group_type))) {
2207 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2209 return NT_STATUS_UNSUCCESSFUL;
2212 if (group_type == SID_NAME_DOM_GRP) {
2213 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2214 &group_sid, argv[1]);
2216 if (!NT_STATUS_IS_OK(result)) {
2217 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2218 argv[1], argv[0], nt_errstr(result));
2223 if (group_type == SID_NAME_ALIAS) {
2224 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2225 &group_sid, argv[1]);
2227 if (!NT_STATUS_IS_OK(result)) {
2228 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2229 argv[1], argv[0], nt_errstr(result));
2234 d_fprintf(stderr, _("Can only add members to global or local groups "
2235 "which %s is not\n"), argv[0]);
2237 return NT_STATUS_UNSUCCESSFUL;
2240 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2242 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2243 rpc_group_addmem_internals,
2247 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2248 struct rpc_pipe_client *pipe_hnd,
2249 TALLOC_CTX *mem_ctx,
2250 const struct dom_sid *group_sid,
2253 struct policy_handle connect_pol, domain_pol;
2254 NTSTATUS status, result;
2256 struct policy_handle group_pol;
2257 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2259 struct samr_Ids rids, rid_types;
2260 struct lsa_String lsa_acct_name;
2264 sid_copy(&sid, group_sid);
2266 if (!sid_split_rid(&sid, &group_rid))
2267 return NT_STATUS_UNSUCCESSFUL;
2269 /* Get sam policy handle */
2270 status = dcerpc_samr_Connect2(b, mem_ctx,
2272 MAXIMUM_ALLOWED_ACCESS,
2275 if (!NT_STATUS_IS_OK(status)) {
2278 if (!NT_STATUS_IS_OK(result)) {
2283 /* Get domain policy handle */
2284 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2286 MAXIMUM_ALLOWED_ACCESS,
2290 if (!NT_STATUS_IS_OK(status)) {
2293 if (!NT_STATUS_IS_OK(result)) {
2297 init_lsa_String(&lsa_acct_name, member);
2299 status = dcerpc_samr_LookupNames(b, mem_ctx,
2306 if (!NT_STATUS_IS_OK(status)) {
2307 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2312 if (!NT_STATUS_IS_OK(result)) {
2314 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2319 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2321 MAXIMUM_ALLOWED_ACCESS,
2325 if (!NT_STATUS_IS_OK(status)) {
2328 if (!NT_STATUS_IS_OK(result)) {
2333 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2337 if (!NT_STATUS_IS_OK(status)) {
2343 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2347 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2348 TALLOC_CTX *mem_ctx,
2349 const struct dom_sid *alias_sid,
2352 struct policy_handle connect_pol, domain_pol;
2353 NTSTATUS status, result;
2355 struct policy_handle alias_pol;
2356 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2358 struct dom_sid member_sid;
2359 enum lsa_SidType member_type;
2363 sid_copy(&sid, alias_sid);
2365 if (!sid_split_rid(&sid, &alias_rid))
2366 return NT_STATUS_UNSUCCESSFUL;
2368 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2369 member, &member_sid, &member_type);
2371 if (!NT_STATUS_IS_OK(result)) {
2372 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2377 /* Get sam policy handle */
2378 status = dcerpc_samr_Connect2(b, mem_ctx,
2380 MAXIMUM_ALLOWED_ACCESS,
2383 if (!NT_STATUS_IS_OK(status)) {
2386 if (!NT_STATUS_IS_OK(result)) {
2391 /* Get domain policy handle */
2392 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2394 MAXIMUM_ALLOWED_ACCESS,
2398 if (!NT_STATUS_IS_OK(status)) {
2401 if (!NT_STATUS_IS_OK(result)) {
2406 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2408 MAXIMUM_ALLOWED_ACCESS,
2412 if (!NT_STATUS_IS_OK(status)) {
2416 if (!NT_STATUS_IS_OK(result)) {
2420 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2425 if (!NT_STATUS_IS_OK(status)) {
2432 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2436 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2437 const struct dom_sid *domain_sid,
2438 const char *domain_name,
2439 struct cli_state *cli,
2440 struct rpc_pipe_client *pipe_hnd,
2441 TALLOC_CTX *mem_ctx,
2445 struct dom_sid group_sid;
2446 enum lsa_SidType group_type;
2448 if (argc != 2 || c->display_usage) {
2451 _("net rpc group delmem <group> <member>\n"
2452 " Delete a member from a group\n"
2453 " group\tGroup to delete member from\n"
2454 " member\tMember to delete from group\n"));
2455 return NT_STATUS_UNSUCCESSFUL;
2458 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2459 &group_sid, &group_type))) {
2460 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2462 return NT_STATUS_UNSUCCESSFUL;
2465 if (group_type == SID_NAME_DOM_GRP) {
2466 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2467 &group_sid, argv[1]);
2469 if (!NT_STATUS_IS_OK(result)) {
2470 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2471 argv[1], argv[0], nt_errstr(result));
2476 if (group_type == SID_NAME_ALIAS) {
2477 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2478 &group_sid, argv[1]);
2480 if (!NT_STATUS_IS_OK(result)) {
2481 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2482 argv[1], argv[0], nt_errstr(result));
2487 d_fprintf(stderr, _("Can only delete members from global or local "
2488 "groups which %s is not\n"), argv[0]);
2490 return NT_STATUS_UNSUCCESSFUL;
2493 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2495 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2496 rpc_group_delmem_internals,
2501 * List groups on a remote RPC server.
2503 * All parameters are provided by the run_rpc_command function, except for
2504 * argc, argv which are passes through.
2506 * @param domain_sid The domain sid acquired from the remote server.
2507 * @param cli A cli_state connected to the server.
2508 * @param mem_ctx Talloc context, destroyed on completion of the function.
2509 * @param argc Standard main() style argc.
2510 * @param argv Standard main() style argv. Initial components are already
2513 * @return Normal NTSTATUS return.
2516 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2517 const struct dom_sid *domain_sid,
2518 const char *domain_name,
2519 struct cli_state *cli,
2520 struct rpc_pipe_client *pipe_hnd,
2521 TALLOC_CTX *mem_ctx,
2525 struct policy_handle connect_pol, domain_pol;
2526 NTSTATUS status, result;
2527 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2528 struct samr_SamArray *groups = NULL;
2529 bool global = false;
2531 bool builtin = false;
2532 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2534 if (c->display_usage) {
2537 _("net rpc group list [global] [local] [builtin]\n"
2538 " List groups on RPC server\n"
2539 " global\tList global groups\n"
2540 " local\tList local groups\n"
2541 " builtin\tList builtin groups\n"
2542 " If none of global, local or builtin is "
2543 "specified, all three options are considered "
2545 return NT_STATUS_OK;
2554 for (i=0; i<argc; i++) {
2555 if (strequal(argv[i], "global"))
2558 if (strequal(argv[i], "local"))
2561 if (strequal(argv[i], "builtin"))
2565 /* Get sam policy handle */
2567 status = dcerpc_samr_Connect2(b, mem_ctx,
2569 MAXIMUM_ALLOWED_ACCESS,
2572 if (!NT_STATUS_IS_OK(status)) {
2575 if (!NT_STATUS_IS_OK(result)) {
2580 /* Get domain policy handle */
2582 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2584 MAXIMUM_ALLOWED_ACCESS,
2585 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2588 if (!NT_STATUS_IS_OK(status)) {
2591 if (!NT_STATUS_IS_OK(result)) {
2596 /* Query domain groups */
2597 if (c->opt_long_list_entries)
2598 d_printf(_("\nGroup name Comment"
2599 "\n-----------------------------\n"));
2601 uint32_t max_size, total_size, returned_size;
2602 union samr_DispInfo info;
2606 dcerpc_get_query_dispinfo_params(
2607 loop_count, &max_entries, &max_size);
2609 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2619 if (!NT_STATUS_IS_OK(status)) {
2622 num_entries = info.info3.count;
2623 start_idx += info.info3.count;
2625 if (!NT_STATUS_IS_OK(result) &&
2626 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2629 for (i = 0; i < num_entries; i++) {
2631 const char *group = NULL;
2632 const char *desc = NULL;
2634 group = info.info3.entries[i].account_name.string;
2635 desc = info.info3.entries[i].description.string;
2637 if (c->opt_long_list_entries)
2638 printf("%-21.21s %-50.50s\n",
2641 printf("%s\n", group);
2643 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2644 /* query domain aliases */
2649 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2656 if (!NT_STATUS_IS_OK(status)) {
2659 if (!NT_STATUS_IS_OK(result) &&
2660 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2663 for (i = 0; i < num_entries; i++) {
2665 const char *description = NULL;
2667 if (c->opt_long_list_entries) {
2669 struct policy_handle alias_pol;
2670 union samr_AliasInfo *info = NULL;
2673 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2676 groups->entries[i].idx,
2679 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2680 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2685 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2686 status = dcerpc_samr_Close(b, mem_ctx,
2689 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2690 description = info->description.string;
2696 if (description != NULL) {
2697 printf("%-21.21s %-50.50s\n",
2698 groups->entries[i].name.string,
2701 printf("%s\n", groups->entries[i].name.string);
2704 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2705 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2706 /* Get builtin policy handle */
2708 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2710 MAXIMUM_ALLOWED_ACCESS,
2711 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2714 if (!NT_STATUS_IS_OK(status)) {
2717 if (!NT_STATUS_IS_OK(result)) {
2722 /* query builtin aliases */
2725 if (!builtin) break;
2727 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2734 if (!NT_STATUS_IS_OK(status)) {
2737 if (!NT_STATUS_IS_OK(result) &&
2738 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2743 for (i = 0; i < num_entries; i++) {
2745 const char *description = NULL;
2747 if (c->opt_long_list_entries) {
2749 struct policy_handle alias_pol;
2750 union samr_AliasInfo *info = NULL;
2753 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2756 groups->entries[i].idx,
2759 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2760 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2765 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2766 status = dcerpc_samr_Close(b, mem_ctx,
2769 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2770 description = info->description.string;
2776 if (description != NULL) {
2777 printf("%-21.21s %-50.50s\n",
2778 groups->entries[i].name.string,
2781 printf("%s\n", groups->entries[i].name.string);
2784 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2792 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2794 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2795 rpc_group_list_internals,
2799 static NTSTATUS rpc_list_group_members(struct net_context *c,
2800 struct rpc_pipe_client *pipe_hnd,
2801 TALLOC_CTX *mem_ctx,
2802 const char *domain_name,
2803 const struct dom_sid *domain_sid,
2804 struct policy_handle *domain_pol,
2807 NTSTATUS result, status;
2808 struct policy_handle group_pol;
2809 uint32 num_members, *group_rids;
2811 struct samr_RidAttrArray *rids = NULL;
2812 struct lsa_Strings names;
2813 struct samr_Ids types;
2814 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2817 sid_to_fstring(sid_str, domain_sid);
2819 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2821 MAXIMUM_ALLOWED_ACCESS,
2825 if (!NT_STATUS_IS_OK(status)) {
2828 if (!NT_STATUS_IS_OK(result)) {
2832 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
2836 if (!NT_STATUS_IS_OK(status)) {
2839 if (!NT_STATUS_IS_OK(result)) {
2843 num_members = rids->count;
2844 group_rids = rids->rids;
2846 while (num_members > 0) {
2847 int this_time = 512;
2849 if (num_members < this_time)
2850 this_time = num_members;
2852 status = dcerpc_samr_LookupRids(b, mem_ctx,
2859 if (!NT_STATUS_IS_OK(status)) {
2862 if (!NT_STATUS_IS_OK(result)) {
2866 /* We only have users as members, but make the output
2867 the same as the output of alias members */
2869 for (i = 0; i < this_time; i++) {
2871 if (c->opt_long_list_entries) {
2872 printf("%s-%d %s\\%s %d\n", sid_str,
2873 group_rids[i], domain_name,
2874 names.names[i].string,
2877 printf("%s\\%s\n", domain_name,
2878 names.names[i].string);
2882 num_members -= this_time;
2886 return NT_STATUS_OK;
2889 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2890 struct rpc_pipe_client *pipe_hnd,
2891 TALLOC_CTX *mem_ctx,
2892 struct policy_handle *domain_pol,
2895 NTSTATUS result, status;
2896 struct rpc_pipe_client *lsa_pipe;
2897 struct policy_handle alias_pol, lsa_pol;
2899 struct dom_sid *alias_sids;
2902 enum lsa_SidType *types;
2904 struct lsa_SidArray sid_array;
2905 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2907 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2909 MAXIMUM_ALLOWED_ACCESS,
2913 if (!NT_STATUS_IS_OK(status)) {
2916 if (!NT_STATUS_IS_OK(result)) {
2920 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
2924 if (!NT_STATUS_IS_OK(status)) {
2925 d_fprintf(stderr, _("Couldn't list alias members\n"));
2928 if (!NT_STATUS_IS_OK(result)) {
2929 d_fprintf(stderr, _("Couldn't list alias members\n"));
2933 num_members = sid_array.num_sids;
2935 if (num_members == 0) {
2936 return NT_STATUS_OK;
2939 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2940 &ndr_table_lsarpc.syntax_id,
2942 if (!NT_STATUS_IS_OK(result)) {
2943 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2944 nt_errstr(result) );
2948 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2949 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2951 if (!NT_STATUS_IS_OK(result)) {
2952 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2953 TALLOC_FREE(lsa_pipe);
2957 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2959 d_fprintf(stderr, _("Out of memory\n"));
2960 TALLOC_FREE(lsa_pipe);
2961 return NT_STATUS_NO_MEMORY;
2964 for (i=0; i<num_members; i++) {
2965 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2968 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2969 num_members, alias_sids,
2970 &domains, &names, &types);
2972 if (!NT_STATUS_IS_OK(result) &&
2973 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2974 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2975 TALLOC_FREE(lsa_pipe);
2979 for (i = 0; i < num_members; i++) {
2981 sid_to_fstring(sid_str, &alias_sids[i]);
2983 if (c->opt_long_list_entries) {
2984 printf("%s %s\\%s %d\n", sid_str,
2985 domains[i] ? domains[i] : _("*unknown*"),
2986 names[i] ? names[i] : _("*unknown*"), types[i]);
2989 printf("%s\\%s\n", domains[i], names[i]);
2991 printf("%s\n", sid_str);
2995 TALLOC_FREE(lsa_pipe);
2996 return NT_STATUS_OK;
2999 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3000 const struct dom_sid *domain_sid,
3001 const char *domain_name,
3002 struct cli_state *cli,
3003 struct rpc_pipe_client *pipe_hnd,
3004 TALLOC_CTX *mem_ctx,
3008 NTSTATUS result, status;
3009 struct policy_handle connect_pol, domain_pol;
3010 struct samr_Ids rids, rid_types;
3011 struct lsa_String lsa_acct_name;
3012 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3014 /* Get sam policy handle */
3016 status = dcerpc_samr_Connect2(b, mem_ctx,
3018 MAXIMUM_ALLOWED_ACCESS,
3021 if (!NT_STATUS_IS_OK(status)) {
3024 if (!NT_STATUS_IS_OK(result)) {
3028 /* Get domain policy handle */
3030 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3032 MAXIMUM_ALLOWED_ACCESS,
3033 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3036 if (!NT_STATUS_IS_OK(status)) {
3039 if (!NT_STATUS_IS_OK(result)) {
3043 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3045 status = dcerpc_samr_LookupNames(b, mem_ctx,
3052 if (!NT_STATUS_IS_OK(status)) {
3056 if (!NT_STATUS_IS_OK(result)) {
3058 /* Ok, did not find it in the global sam, try with builtin */
3060 struct dom_sid sid_Builtin;
3062 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3064 sid_copy(&sid_Builtin, &global_sid_Builtin);
3066 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3068 MAXIMUM_ALLOWED_ACCESS,
3072 if (!NT_STATUS_IS_OK(status)) {
3075 if (!NT_STATUS_IS_OK(result)) {
3076 d_fprintf(stderr, _("Couldn't find group %s\n"),
3081 status = dcerpc_samr_LookupNames(b, mem_ctx,
3088 if (!NT_STATUS_IS_OK(status)) {
3091 if (!NT_STATUS_IS_OK(result)) {
3092 d_fprintf(stderr, _("Couldn't find group %s\n"),
3098 if (rids.count != 1) {
3099 d_fprintf(stderr, _("Couldn't find group %s\n"),
3104 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3105 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3106 domain_sid, &domain_pol,
3110 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3111 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3115 return NT_STATUS_NO_SUCH_GROUP;
3118 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3120 if (argc != 1 || c->display_usage) {
3121 return rpc_group_usage(c, argc, argv);
3124 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3125 rpc_group_members_internals,
3129 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3131 NET_API_STATUS status;
3132 struct GROUP_INFO_0 g0;
3136 d_printf(_("Usage:\n"));
3137 d_printf("net rpc group rename group newname\n");
3141 g0.grpi0_name = argv[1];
3143 status = NetGroupSetInfo(c->opt_host,
3150 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3151 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3159 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3161 if (argc != 2 || c->display_usage) {
3162 return rpc_group_usage(c, argc, argv);
3165 return rpc_group_rename_internals(c, argc, argv);
3169 * 'net rpc group' entrypoint.
3170 * @param argc Standard main() style argc.
3171 * @param argv Standard main() style argv. Initial components are already
3175 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3177 NET_API_STATUS status;
3179 struct functable func[] = {
3184 N_("Create specified group"),
3185 N_("net rpc group add\n"
3186 " Create specified group")
3192 N_("Delete specified group"),
3193 N_("net rpc group delete\n"
3194 " Delete specified group")
3200 N_("Add member to group"),
3201 N_("net rpc group addmem\n"
3202 " Add member to group")
3208 N_("Remove member from group"),
3209 N_("net rpc group delmem\n"
3210 " Remove member from group")
3217 N_("net rpc group list\n"
3224 N_("List group members"),
3225 N_("net rpc group members\n"
3226 " List group members")
3233 N_("net rpc group rename\n"
3236 {NULL, NULL, 0, NULL, NULL}
3239 status = libnetapi_net_init(&c->netapi_ctx);
3243 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3244 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3245 if (c->opt_kerberos) {
3246 libnetapi_set_use_kerberos(c->netapi_ctx);
3250 if (c->display_usage) {
3251 d_printf(_("Usage:\n"));
3252 d_printf(_("net rpc group\n"
3253 " Alias for net rpc group list global "
3254 "local builtin\n"));
3255 net_display_usage_from_functable(func);
3259 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3260 rpc_group_list_internals,
3264 return net_run_function(c, argc, argv, "net rpc group", func);
3267 /****************************************************************************/
3269 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3271 return net_share_usage(c, argc, argv);
3275 * Add a share on a remote RPC server.
3277 * @param argc Standard main() style argc.
3278 * @param argv Standard main() style argv. Initial components are already
3281 * @return A shell status integer (0 for success).
3284 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3286 NET_API_STATUS status;
3289 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3290 uint32 num_users=0, perms=0;
3291 char *password=NULL; /* don't allow a share password */
3292 struct SHARE_INFO_2 i2;
3293 uint32_t parm_error = 0;
3295 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3296 return rpc_share_usage(c, argc, argv);
3299 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3303 path = strchr(sharename, '=');
3310 i2.shi2_netname = sharename;
3311 i2.shi2_type = type;
3312 i2.shi2_remark = c->opt_comment;
3313 i2.shi2_permissions = perms;
3314 i2.shi2_max_uses = c->opt_maxusers;
3315 i2.shi2_current_uses = num_users;
3316 i2.shi2_path = path;
3317 i2.shi2_passwd = password;
3319 status = NetShareAdd(c->opt_host,
3324 printf(_("NetShareAdd failed with: %s\n"),
3325 libnetapi_get_error_string(c->netapi_ctx, status));
3332 * Delete a share on a remote RPC server.
3334 * @param domain_sid The domain sid acquired from the remote server.
3335 * @param argc Standard main() style argc.
3336 * @param argv Standard main() style argv. Initial components are already
3339 * @return A shell status integer (0 for success).
3341 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3343 if (argc < 1 || c->display_usage) {
3344 return rpc_share_usage(c, argc, argv);
3347 return NetShareDel(c->opt_host, argv[0], 0);
3351 * Formatted print of share info
3353 * @param r pointer to SHARE_INFO_1 to format
3356 static void display_share_info_1(struct net_context *c,
3357 struct SHARE_INFO_1 *r)
3359 if (c->opt_long_list_entries) {
3360 d_printf("%-12s %-8.8s %-50s\n",
3362 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3365 d_printf("%s\n", r->shi1_netname);
3369 static WERROR get_share_info(struct net_context *c,
3370 struct rpc_pipe_client *pipe_hnd,
3371 TALLOC_CTX *mem_ctx,
3375 struct srvsvc_NetShareInfoCtr *info_ctr)
3379 union srvsvc_NetShareInfo info;
3380 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3382 /* no specific share requested, enumerate all */
3385 uint32_t preferred_len = 0xffffffff;
3386 uint32_t total_entries = 0;
3387 uint32_t resume_handle = 0;
3389 info_ctr->level = level;
3391 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3398 if (!NT_STATUS_IS_OK(status)) {
3399 return ntstatus_to_werror(status);
3404 /* request just one share */
3405 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3412 if (!NT_STATUS_IS_OK(status)) {
3413 result = ntstatus_to_werror(status);
3417 if (!W_ERROR_IS_OK(result)) {
3422 ZERO_STRUCTP(info_ctr);
3424 info_ctr->level = level;
3429 struct srvsvc_NetShareCtr1 *ctr1;
3431 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3432 W_ERROR_HAVE_NO_MEMORY(ctr1);
3435 ctr1->array = info.info1;
3437 info_ctr->ctr.ctr1 = ctr1;
3443 struct srvsvc_NetShareCtr2 *ctr2;
3445 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3446 W_ERROR_HAVE_NO_MEMORY(ctr2);
3449 ctr2->array = info.info2;
3451 info_ctr->ctr.ctr2 = ctr2;
3457 struct srvsvc_NetShareCtr502 *ctr502;
3459 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3460 W_ERROR_HAVE_NO_MEMORY(ctr502);
3463 ctr502->array = info.info502;
3465 info_ctr->ctr.ctr502 = ctr502;
3475 * 'net rpc share list' entrypoint.
3476 * @param argc Standard main() style argc.
3477 * @param argv Standard main() style argv. Initial components are already
3480 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3482 NET_API_STATUS status;
3483 struct SHARE_INFO_1 *i1 = NULL;
3484 uint32_t entries_read = 0;
3485 uint32_t total_entries = 0;
3486 uint32_t resume_handle = 0;
3487 uint32_t i, level = 1;
3489 if (c->display_usage) {
3491 "net rpc share list\n"
3494 _("List shares on remote server"));
3498 status = NetShareEnum(c->opt_host,
3500 (uint8_t **)(void *)&i1,
3509 /* Display results */
3511 if (c->opt_long_list_entries) {
3513 "\nEnumerating shared resources (exports) on remote server:\n\n"
3514 "\nShare name Type Description\n"
3515 "---------- ---- -----------\n"));
3517 for (i = 0; i < entries_read; i++)
3518 display_share_info_1(c, &i1[i]);
3523 static bool check_share_availability(struct cli_state *cli, const char *netname)
3527 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3528 if (!NT_STATUS_IS_OK(status)) {
3529 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3533 status = cli_tdis(cli);
3534 if (!NT_STATUS_IS_OK(status)) {
3535 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3542 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3543 const char *netname, uint32 type)
3545 /* only support disk shares */
3546 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3547 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3552 /* skip builtin shares */
3553 /* FIXME: should print$ be added too ? */
3554 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3555 strequal(netname,"global"))
3558 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3559 printf(_("excluding [%s]\n"), netname);
3563 return check_share_availability(cli, netname);
3567 * Migrate shares from a remote RPC server to the local RPC server.
3569 * All parameters are provided by the run_rpc_command function, except for
3570 * argc, argv which are passed through.
3572 * @param domain_sid The domain sid acquired from the remote server.
3573 * @param cli A cli_state connected to the server.
3574 * @param mem_ctx Talloc context, destroyed on completion of the function.
3575 * @param argc Standard main() style argc.
3576 * @param argv Standard main() style argv. Initial components are already
3579 * @return Normal NTSTATUS return.
3582 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3583 const struct dom_sid *domain_sid,
3584 const char *domain_name,
3585 struct cli_state *cli,
3586 struct rpc_pipe_client *pipe_hnd,
3587 TALLOC_CTX *mem_ctx,
3592 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3593 struct srvsvc_NetShareInfoCtr ctr_src;
3595 struct rpc_pipe_client *srvsvc_pipe = NULL;
3596 struct cli_state *cli_dst = NULL;
3597 uint32 level = 502; /* includes secdesc */
3598 uint32_t parm_error = 0;
3599 struct dcerpc_binding_handle *b;
3601 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3603 if (!W_ERROR_IS_OK(result))
3606 /* connect destination PI_SRVSVC */
3607 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3608 &ndr_table_srvsvc.syntax_id);
3609 if (!NT_STATUS_IS_OK(nt_status))
3612 b = srvsvc_pipe->binding_handle;
3614 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3616 union srvsvc_NetShareInfo info;
3617 struct srvsvc_NetShareInfo502 info502 =
3618 ctr_src.ctr.ctr502->array[i];
3620 /* reset error-code */
3621 nt_status = NT_STATUS_UNSUCCESSFUL;
3623 if (!check_share_sanity(c, cli, info502.name, info502.type))
3626 /* finally add the share on the dst server */
3628 printf(_("migrating: [%s], path: %s, comment: %s, without "
3630 info502.name, info502.path, info502.comment);
3632 info.info502 = &info502;
3634 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3635 srvsvc_pipe->desthost,
3640 if (!NT_STATUS_IS_OK(nt_status)) {
3641 printf(_("cannot add share: %s\n"),
3642 nt_errstr(nt_status));
3645 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3646 printf(_(" [%s] does already exist\n"),
3651 if (!W_ERROR_IS_OK(result)) {
3652 nt_status = werror_to_ntstatus(result);
3653 printf(_("cannot add share: %s\n"),
3654 win_errstr(result));
3660 nt_status = NT_STATUS_OK;
3664 cli_shutdown(cli_dst);
3672 * Migrate shares from a RPC server to another.
3674 * @param argc Standard main() style argc.
3675 * @param argv Standard main() style argv. Initial components are already
3678 * @return A shell status integer (0 for success).
3680 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3683 if (c->display_usage) {
3685 "net rpc share migrate shares\n"
3688 _("Migrate shares to local server"));
3693 printf(_("no server to migrate\n"));
3697 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3698 rpc_share_migrate_shares_internals,
3705 * @param f file_info
3706 * @param mask current search mask
3707 * @param state arg-pointer
3710 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3711 const char *mask, void *state)
3713 static NTSTATUS nt_status;
3714 static struct copy_clistate *local_state;
3715 static fstring filename, new_mask;
3718 struct net_context *c;
3720 local_state = (struct copy_clistate *)state;
3721 nt_status = NT_STATUS_UNSUCCESSFUL;
3725 if (strequal(f->name, ".") || strequal(f->name, ".."))
3726 return NT_STATUS_OK;
3728 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3731 if (f->mode & aDIR) {
3733 DEBUG(3,("got dir: %s\n", f->name));
3735 fstrcpy(dir, local_state->cwd);
3737 fstrcat(dir, f->name);
3739 switch (net_mode_share)
3741 case NET_MODE_SHARE_MIGRATE:
3742 /* create that directory */
3743 nt_status = net_copy_file(c, local_state->mem_ctx,
3744 local_state->cli_share_src,
3745 local_state->cli_share_dst,
3747 c->opt_acls? true : false,
3748 c->opt_attrs? true : false,
3749 c->opt_timestamps? true:false,
3753 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3754 return NT_STATUS_INTERNAL_ERROR;
3757 if (!NT_STATUS_IS_OK(nt_status)) {
3758 printf(_("could not handle dir %s: %s\n"),
3759 dir, nt_errstr(nt_status));
3763 /* search below that directory */
3764 fstrcpy(new_mask, dir);
3765 fstrcat(new_mask, "\\*");
3767 old_dir = local_state->cwd;
3768 local_state->cwd = dir;
3769 nt_status = sync_files(local_state, new_mask);
3770 if (!NT_STATUS_IS_OK(nt_status)) {
3771 printf(_("could not handle files\n"));
3773 local_state->cwd = old_dir;
3780 fstrcpy(filename, local_state->cwd);
3781 fstrcat(filename, "\\");
3782 fstrcat(filename, f->name);
3784 DEBUG(3,("got file: %s\n", filename));
3786 switch (net_mode_share)
3788 case NET_MODE_SHARE_MIGRATE:
3789 nt_status = net_copy_file(c, local_state->mem_ctx,
3790 local_state->cli_share_src,
3791 local_state->cli_share_dst,
3793 c->opt_acls? true : false,
3794 c->opt_attrs? true : false,
3795 c->opt_timestamps? true: false,
3799 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3801 return NT_STATUS_INTERNAL_ERROR;
3804 if (!NT_STATUS_IS_OK(nt_status))
3805 printf(_("could not handle file %s: %s\n"),
3806 filename, nt_errstr(nt_status));
3811 * sync files, can be called recursivly to list files
3812 * and then call copy_fn for each file
3814 * @param cp_clistate pointer to the copy_clistate we work with
3815 * @param mask the current search mask
3817 * @return Boolean result
3819 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3821 struct cli_state *targetcli;
3822 char *targetpath = NULL;
3825 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3827 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3828 mask, &targetcli, &targetpath ) ) {
3829 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3831 mask, cli_errstr(cp_clistate->cli_share_src));
3832 return cli_nt_error(cp_clistate->cli_share_src);
3835 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3836 copy_fn, cp_clistate);
3837 if (!NT_STATUS_IS_OK(status)) {
3838 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3839 mask, nt_errstr(status));
3847 * Set the top level directory permissions before we do any further copies.
3848 * Should set up ACL inheritance.
3851 bool copy_top_level_perms(struct net_context *c,
3852 struct copy_clistate *cp_clistate,
3853 const char *sharename)
3855 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3857 switch (net_mode_share) {
3858 case NET_MODE_SHARE_MIGRATE:
3859 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3860 nt_status = net_copy_fileattr(c,
3861 cp_clistate->mem_ctx,
3862 cp_clistate->cli_share_src,
3863 cp_clistate->cli_share_dst,
3865 c->opt_acls? true : false,
3866 c->opt_attrs? true : false,
3867 c->opt_timestamps? true: false,
3871 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3875 if (!NT_STATUS_IS_OK(nt_status)) {
3876 printf(_("Could handle directory attributes for top level "
3877 "directory of share %s. Error %s\n"),
3878 sharename, nt_errstr(nt_status));
3886 * Sync all files inside a remote share to another share (over smb).
3888 * All parameters are provided by the run_rpc_command function, except for
3889 * argc, argv which are passed through.
3891 * @param domain_sid The domain sid acquired from the remote server.
3892 * @param cli A cli_state connected to the server.
3893 * @param mem_ctx Talloc context, destroyed on completion of the function.
3894 * @param argc Standard main() style argc.
3895 * @param argv Standard main() style argv. Initial components are already
3898 * @return Normal NTSTATUS return.
3901 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3902 const struct dom_sid *domain_sid,
3903 const char *domain_name,
3904 struct cli_state *cli,
3905 struct rpc_pipe_client *pipe_hnd,
3906 TALLOC_CTX *mem_ctx,
3911 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3912 struct srvsvc_NetShareInfoCtr ctr_src;
3915 struct copy_clistate cp_clistate;
3916 bool got_src_share = false;
3917 bool got_dst_share = false;
3918 const char *mask = "\\*";
3921 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3923 nt_status = NT_STATUS_NO_MEMORY;
3927 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3930 if (!W_ERROR_IS_OK(result))
3933 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3935 struct srvsvc_NetShareInfo502 info502 =
3936 ctr_src.ctr.ctr502->array[i];
3938 if (!check_share_sanity(c, cli, info502.name, info502.type))
3941 /* one might not want to mirror whole discs :) */
3942 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3943 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3948 switch (net_mode_share)
3950 case NET_MODE_SHARE_MIGRATE:
3954 d_fprintf(stderr, _("Unsupported mode %d\n"),
3958 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3961 c->opt_acls ? _("including") : _("without"),
3962 c->opt_attrs ? _("including") : _("without"),
3963 c->opt_timestamps ? _("(preserving timestamps)") : "");
3965 cp_clistate.mem_ctx = mem_ctx;
3966 cp_clistate.cli_share_src = NULL;
3967 cp_clistate.cli_share_dst = NULL;
3968 cp_clistate.cwd = NULL;
3969 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3972 /* open share source */
3973 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3974 &cli->dest_ss, cli->desthost,
3975 info502.name, "A:");
3976 if (!NT_STATUS_IS_OK(nt_status))
3979 got_src_share = true;
3981 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3982 /* open share destination */
3983 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3984 NULL, dst, info502.name, "A:");
3985 if (!NT_STATUS_IS_OK(nt_status))
3988 got_dst_share = true;
3991 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3992 d_fprintf(stderr, _("Could not handle the top level "
3993 "directory permissions for the "
3994 "share: %s\n"), info502.name);
3995 nt_status = NT_STATUS_UNSUCCESSFUL;
3999 nt_status = sync_files(&cp_clistate, mask);
4000 if (!NT_STATUS_IS_OK(nt_status)) {
4001 d_fprintf(stderr, _("could not handle files for share: "
4002 "%s\n"), info502.name);
4007 nt_status = NT_STATUS_OK;
4012 cli_shutdown(cp_clistate.cli_share_src);
4015 cli_shutdown(cp_clistate.cli_share_dst);
4022 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4024 if (c->display_usage) {
4026 "net share migrate files\n"
4029 _("Migrate files to local server"));
4034 d_printf(_("no server to migrate\n"));
4038 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4039 rpc_share_migrate_files_internals,
4044 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4046 * All parameters are provided by the run_rpc_command function, except for
4047 * argc, argv which are passed through.
4049 * @param domain_sid The domain sid acquired from the remote server.
4050 * @param cli A cli_state connected to the server.
4051 * @param mem_ctx Talloc context, destroyed on completion of the function.
4052 * @param argc Standard main() style argc.
4053 * @param argv Standard main() style argv. Initial components are already
4056 * @return Normal NTSTATUS return.
4059 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4060 const struct dom_sid *domain_sid,
4061 const char *domain_name,
4062 struct cli_state *cli,
4063 struct rpc_pipe_client *pipe_hnd,
4064 TALLOC_CTX *mem_ctx,
4069 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4070 struct srvsvc_NetShareInfoCtr ctr_src;
4071 union srvsvc_NetShareInfo info;
4073 struct rpc_pipe_client *srvsvc_pipe = NULL;
4074 struct cli_state *cli_dst = NULL;
4075 uint32 level = 502; /* includes secdesc */
4076 uint32_t parm_error = 0;
4077 struct dcerpc_binding_handle *b;
4079 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4082 if (!W_ERROR_IS_OK(result))
4085 /* connect destination PI_SRVSVC */
4086 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4087 &ndr_table_srvsvc.syntax_id);
4088 if (!NT_STATUS_IS_OK(nt_status))
4091 b = srvsvc_pipe->binding_handle;
4093 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4095 struct srvsvc_NetShareInfo502 info502 =
4096 ctr_src.ctr.ctr502->array[i];
4098 /* reset error-code */
4099 nt_status = NT_STATUS_UNSUCCESSFUL;
4101 if (!check_share_sanity(c, cli, info502.name, info502.type))
4104 printf(_("migrating: [%s], path: %s, comment: %s, including "
4106 info502.name, info502.path, info502.comment);
4109 display_sec_desc(info502.sd_buf.sd);
4111 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4112 info.info502 = &info502;
4114 /* finally modify the share on the dst server */
4115 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4116 srvsvc_pipe->desthost,
4122 if (!NT_STATUS_IS_OK(nt_status)) {
4123 printf(_("cannot set share-acl: %s\n"),
4124 nt_errstr(nt_status));
4127 if (!W_ERROR_IS_OK(result)) {
4128 nt_status = werror_to_ntstatus(result);
4129 printf(_("cannot set share-acl: %s\n"),
4130 win_errstr(result));
4136 nt_status = NT_STATUS_OK;
4140 cli_shutdown(cli_dst);
4148 * Migrate share-acls from a RPC server to another.
4150 * @param argc Standard main() style argc.
4151 * @param argv Standard main() style argv. Initial components are already
4154 * @return A shell status integer (0 for success).
4156 static int rpc_share_migrate_security(struct net_context *c, int argc,
4159 if (c->display_usage) {
4161 "net rpc share migrate security\n"
4164 _("Migrate share-acls to local server"));
4169 d_printf(_("no server to migrate\n"));
4173 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4174 rpc_share_migrate_security_internals,
4179 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4180 * from one server to another.
4182 * @param argc Standard main() style argc.
4183 * @param argv Standard main() style argv. Initial components are already
4186 * @return A shell status integer (0 for success).
4189 static int rpc_share_migrate_all(struct net_context *c, int argc,
4194 if (c->display_usage) {
4196 "net rpc share migrate all\n"
4199 _("Migrates shares including all share settings"));
4204 d_printf(_("no server to migrate\n"));
4208 /* order is important. we don't want to be locked out by the share-acl
4209 * before copying files - gd */
4211 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4212 rpc_share_migrate_shares_internals, argc, argv);
4216 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4217 rpc_share_migrate_files_internals, argc, argv);
4221 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4222 rpc_share_migrate_security_internals, argc,
4228 * 'net rpc share migrate' entrypoint.
4229 * @param argc Standard main() style argc.
4230 * @param argv Standard main() style argv. Initial components are already
4233 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4236 struct functable func[] = {
4239 rpc_share_migrate_all,
4241 N_("Migrate shares from remote to local server"),
4242 N_("net rpc share migrate all\n"
4243 " Migrate shares from remote to local server")
4247 rpc_share_migrate_files,
4249 N_("Migrate files from remote to local server"),
4250 N_("net rpc share migrate files\n"
4251 " Migrate files from remote to local server")
4255 rpc_share_migrate_security,
4257 N_("Migrate share-ACLs from remote to local server"),
4258 N_("net rpc share migrate security\n"
4259 " Migrate share-ACLs from remote to local server")
4263 rpc_share_migrate_shares,
4265 N_("Migrate shares from remote to local server"),
4266 N_("net rpc share migrate shares\n"
4267 " Migrate shares from remote to local server")
4269 {NULL, NULL, 0, NULL, NULL}
4272 net_mode_share = NET_MODE_SHARE_MIGRATE;
4274 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4280 struct dom_sid *members;
4283 static int num_server_aliases;
4284 static struct full_alias *server_aliases;
4287 * Add an alias to the static list.
4289 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4291 if (server_aliases == NULL)
4292 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4294 server_aliases[num_server_aliases] = *alias;
4295 num_server_aliases += 1;
4299 * For a specific domain on the server, fetch all the aliases
4300 * and their members. Add all of them to the server_aliases.
4303 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4304 TALLOC_CTX *mem_ctx,
4305 struct policy_handle *connect_pol,
4306 const struct dom_sid *domain_sid)
4308 uint32 start_idx, max_entries, num_entries, i;
4309 struct samr_SamArray *groups = NULL;
4310 NTSTATUS result, status;
4311 struct policy_handle domain_pol;
4312 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4314 /* Get domain policy handle */
4316 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4318 MAXIMUM_ALLOWED_ACCESS,
4319 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4322 if (!NT_STATUS_IS_OK(status)) {
4325 if (!NT_STATUS_IS_OK(result)) {
4333 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4340 if (!NT_STATUS_IS_OK(status)) {
4343 for (i = 0; i < num_entries; i++) {
4345 struct policy_handle alias_pol;
4346 struct full_alias alias;
4347 struct lsa_SidArray sid_array;
4351 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4353 MAXIMUM_ALLOWED_ACCESS,
4354 groups->entries[i].idx,
4357 if (!NT_STATUS_IS_OK(status)) {
4360 if (!NT_STATUS_IS_OK(_result)) {
4365 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4369 if (!NT_STATUS_IS_OK(status)) {
4372 if (!NT_STATUS_IS_OK(_result)) {
4377 alias.num_members = sid_array.num_sids;
4379 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4380 if (!NT_STATUS_IS_OK(status)) {
4383 if (!NT_STATUS_IS_OK(_result)) {
4388 alias.members = NULL;
4390 if (alias.num_members > 0) {
4391 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4393 for (j = 0; j < alias.num_members; j++)
4394 sid_copy(&alias.members[j],
4395 sid_array.sids[j].sid);
4398 sid_compose(&alias.sid, domain_sid,
4399 groups->entries[i].idx);
4401 push_alias(mem_ctx, &alias);
4403 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4405 status = NT_STATUS_OK;
4408 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4414 * Dump server_aliases as names for debugging purposes.
4417 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4418 const struct dom_sid *domain_sid,
4419 const char *domain_name,
4420 struct cli_state *cli,
4421 struct rpc_pipe_client *pipe_hnd,
4422 TALLOC_CTX *mem_ctx,
4428 struct policy_handle lsa_pol;
4429 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4431 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4432 SEC_FLAG_MAXIMUM_ALLOWED,
4434 if (!NT_STATUS_IS_OK(result))
4437 for (i=0; i<num_server_aliases; i++) {
4440 enum lsa_SidType *types;
4443 struct full_alias *alias = &server_aliases[i];
4445 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4447 &domains, &names, &types);
4448 if (!NT_STATUS_IS_OK(result))
4451 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4453 if (alias->num_members == 0) {
4458 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4461 &domains, &names, &types);
4463 if (!NT_STATUS_IS_OK(result) &&
4464 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4467 for (j=0; j<alias->num_members; j++)
4468 DEBUG(1, ("%s\\%s (%d); ",
4469 domains[j] ? domains[j] : "*unknown*",
4470 names[j] ? names[j] : "*unknown*",types[j]));
4474 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4476 return NT_STATUS_OK;
4480 * Fetch a list of all server aliases and their members into
4484 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4485 const struct dom_sid *domain_sid,
4486 const char *domain_name,
4487 struct cli_state *cli,
4488 struct rpc_pipe_client *pipe_hnd,
4489 TALLOC_CTX *mem_ctx,
4493 NTSTATUS result, status;
4494 struct policy_handle connect_pol;
4495 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4497 status = dcerpc_samr_Connect2(b, mem_ctx,
4499 MAXIMUM_ALLOWED_ACCESS,
4502 if (!NT_STATUS_IS_OK(status)) {
4505 if (!NT_STATUS_IS_OK(result)) {
4510 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4511 &global_sid_Builtin);
4512 if (!NT_STATUS_IS_OK(status)) {
4516 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4519 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4524 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4526 token->num_sids = 4;
4528 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4529 d_fprintf(stderr, "malloc %s\n",_("failed"));
4530 token->num_sids = 0;
4534 token->sids[0] = *user_sid;
4535 sid_copy(&token->sids[1], &global_sid_World);
4536 sid_copy(&token->sids[2], &global_sid_Network);
4537 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4540 static void free_user_token(struct security_token *token)
4542 SAFE_FREE(token->sids);
4545 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4547 if (security_token_has_sid(token, sid))
4550 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4555 sid_copy(&token->sids[token->num_sids], sid);
4557 token->num_sids += 1;
4562 struct security_token token;
4565 static void dump_user_token(struct user_token *token)
4569 d_printf("%s\n", token->name);
4571 for (i=0; i<token->token.num_sids; i++) {
4572 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4576 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4580 for (i=0; i<alias->num_members; i++) {
4581 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4588 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4592 for (i=0; i<num_server_aliases; i++) {
4593 if (is_alias_member(&sid, &server_aliases[i]))
4594 add_sid_to_token(token, &server_aliases[i].sid);
4599 * We got a user token with all the SIDs we can know about without asking the
4600 * server directly. These are the user and domain group sids. All of these can
4601 * be members of aliases. So scan the list of aliases for each of the SIDs and
4602 * add them to the token.
4605 static void collect_alias_memberships(struct security_token *token)
4607 int num_global_sids = token->num_sids;
4610 for (i=0; i<num_global_sids; i++) {
4611 collect_sid_memberships(token, token->sids[i]);
4615 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4617 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4618 enum wbcSidType type;
4620 struct wbcDomainSid wsid;
4621 char sid_str[WBC_SID_STRING_BUFLEN];
4622 struct dom_sid user_sid;
4623 uint32_t num_groups;
4624 gid_t *groups = NULL;
4627 fstr_sprintf(full_name, "%s%c%s",
4628 domain, *lp_winbind_separator(), user);
4630 /* First let's find out the user sid */
4632 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4634 if (!WBC_ERROR_IS_OK(wbc_status)) {
4635 DEBUG(1, ("winbind could not find %s: %s\n",
4636 full_name, wbcErrorString(wbc_status)));
4640 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4642 if (type != WBC_SID_NAME_USER) {
4643 DEBUG(1, ("%s is not a user\n", full_name));
4647 if (!string_to_sid(&user_sid, sid_str)) {
4648 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4652 init_user_token(token, &user_sid);
4654 /* And now the groups winbind knows about */
4656 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4657 if (!WBC_ERROR_IS_OK(wbc_status)) {
4658 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4659 full_name, wbcErrorString(wbc_status)));
4663 for (i = 0; i < num_groups; i++) {
4664 gid_t gid = groups[i];
4667 wbc_status = wbcGidToSid(gid, &wsid);
4668 if (!WBC_ERROR_IS_OK(wbc_status)) {
4669 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4670 (unsigned int)gid, wbcErrorString(wbc_status)));
4671 wbcFreeMemory(groups);
4675 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4677 DEBUG(3, (" %s\n", sid_str));
4679 string_to_sid(&sid, sid_str);
4680 add_sid_to_token(token, &sid);
4682 wbcFreeMemory(groups);
4688 * Get a list of all user tokens we want to look at
4691 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4692 struct user_token **user_tokens)
4694 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4695 uint32_t i, num_users;
4697 struct user_token *result;
4698 TALLOC_CTX *frame = NULL;
4700 if (lp_winbind_use_default_domain() &&
4701 (c->opt_target_workgroup == NULL)) {
4702 d_fprintf(stderr, _("winbind use default domain = yes set, "
4703 "please specify a workgroup\n"));
4707 /* Send request to winbind daemon */
4709 wbc_status = wbcListUsers(NULL, &num_users, &users);
4710 if (!WBC_ERROR_IS_OK(wbc_status)) {
4711 DEBUG(1, (_("winbind could not list users: %s\n"),
4712 wbcErrorString(wbc_status)));
4716 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4718 if (result == NULL) {
4719 DEBUG(1, ("Could not malloc sid array\n"));
4720 wbcFreeMemory(users);
4724 frame = talloc_stackframe();
4725 for (i=0; i < num_users; i++) {
4726 fstring domain, user;
4729 fstrcpy(result[i].name, users[i]);
4731 p = strchr(users[i], *lp_winbind_separator());
4733 DEBUG(3, ("%s\n", users[i]));
4736 fstrcpy(domain, c->opt_target_workgroup);
4737 fstrcpy(user, users[i]);
4740 fstrcpy(domain, users[i]);
4745 get_user_sids(domain, user, &(result[i].token));
4748 wbcFreeMemory(users);
4750 *num_tokens = num_users;
4751 *user_tokens = result;
4756 static bool get_user_tokens_from_file(FILE *f,
4758 struct user_token **tokens)
4760 struct user_token *token = NULL;
4765 if (fgets(line, sizeof(line)-1, f) == NULL) {
4769 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4770 line[strlen(line)-1] = '\0';
4773 if (line[0] == ' ') {
4777 if(!string_to_sid(&sid, &line[1])) {
4778 DEBUG(1,("get_user_tokens_from_file: Could "
4779 "not convert sid %s \n",&line[1]));
4783 if (token == NULL) {
4784 DEBUG(0, ("File does not begin with username"));
4788 add_sid_to_token(&token->token, &sid);
4792 /* And a new user... */
4795 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4796 if (*tokens == NULL) {
4797 DEBUG(0, ("Could not realloc tokens\n"));
4801 token = &((*tokens)[*num_tokens-1]);
4803 fstrcpy(token->name, line);
4804 token->token.num_sids = 0;
4805 token->token.sids = NULL;
4814 * Show the list of all users that have access to a share
4817 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4818 TALLOC_CTX *mem_ctx,
4819 const char *netname,
4821 struct user_token *tokens)
4824 struct security_descriptor *share_sd = NULL;
4825 struct security_descriptor *root_sd = NULL;
4826 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4828 union srvsvc_NetShareInfo info;
4832 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4834 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4841 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4842 DEBUG(1, ("Coult not query secdesc for share %s\n",
4847 share_sd = info.info502->sd_buf.sd;
4848 if (share_sd == NULL) {
4849 DEBUG(1, ("Got no secdesc for share %s\n",
4855 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4859 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4860 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4861 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4864 for (i=0; i<num_tokens; i++) {
4867 if (share_sd != NULL) {
4868 status = se_access_check(share_sd, &tokens[i].token,
4871 if (!NT_STATUS_IS_OK(status)) {
4872 DEBUG(1, ("Could not check share_sd for "
4879 if (root_sd == NULL) {
4880 d_printf(" %s\n", tokens[i].name);
4884 status = se_access_check(root_sd, &tokens[i].token,
4886 if (!NT_STATUS_IS_OK(status)) {
4887 DEBUG(1, ("Could not check root_sd for user %s\n",
4891 d_printf(" %s\n", tokens[i].name);
4894 if (fnum != (uint16_t)-1)
4895 cli_close(cli, fnum);
4907 static void collect_share(const char *name, uint32 m,
4908 const char *comment, void *state)
4910 struct share_list *share_list = (struct share_list *)state;
4912 if (m != STYPE_DISKTREE)
4915 share_list->num_shares += 1;
4916 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4917 if (!share_list->shares) {
4918 share_list->num_shares = 0;
4921 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4925 * List shares on a remote RPC server, including the security descriptors.
4927 * All parameters are provided by the run_rpc_command function, except for
4928 * argc, argv which are passed through.
4930 * @param domain_sid The domain sid acquired from the remote server.
4931 * @param cli A cli_state connected to the server.
4932 * @param mem_ctx Talloc context, destroyed on completion of the function.
4933 * @param argc Standard main() style argc.
4934 * @param argv Standard main() style argv. Initial components are already
4937 * @return Normal NTSTATUS return.
4940 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4941 const struct dom_sid *domain_sid,
4942 const char *domain_name,
4943 struct cli_state *cli,
4944 struct rpc_pipe_client *pipe_hnd,
4945 TALLOC_CTX *mem_ctx,
4954 struct user_token *tokens = NULL;
4957 struct share_list share_list;
4962 f = fopen(argv[0], "r");
4966 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4967 return NT_STATUS_UNSUCCESSFUL;
4970 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4976 DEBUG(0, ("Could not read users from file\n"));
4977 return NT_STATUS_UNSUCCESSFUL;
4980 for (i=0; i<num_tokens; i++)
4981 collect_alias_memberships(&tokens[i].token);
4983 share_list.num_shares = 0;
4984 share_list.shares = NULL;
4986 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4989 DEBUG(0, ("Error returning browse list: %s\n",
4994 for (i = 0; i < share_list.num_shares; i++) {
4995 char *netname = share_list.shares[i];
4997 if (netname[strlen(netname)-1] == '$')
5000 d_printf("%s\n", netname);
5002 show_userlist(pipe_hnd, mem_ctx, netname,
5003 num_tokens, tokens);
5006 for (i=0; i<num_tokens; i++) {
5007 free_user_token(&tokens[i].token);
5010 SAFE_FREE(share_list.shares);
5012 return NT_STATUS_OK;
5015 static int rpc_share_allowedusers(struct net_context *c, int argc,
5020 if (c->display_usage) {
5022 "net rpc share allowedusers\n"
5025 _("List allowed users"));
5029 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5030 rpc_aliaslist_internals,
5035 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
5041 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5042 rpc_share_allowedusers_internals,
5046 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5049 struct user_token *tokens = NULL;
5053 net_usersidlist_usage(c, argc, argv);
5057 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5058 DEBUG(0, ("Could not get the user/sid list\n"));
5062 for (i=0; i<num_tokens; i++) {
5063 dump_user_token(&tokens[i]);
5064 free_user_token(&tokens[i].token);
5071 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5073 d_printf(_("net usersidlist\n"
5074 "\tprints out a list of all users the running winbind knows\n"
5075 "\tabout, together with all their SIDs. This is used as\n"
5076 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5078 net_common_flags_usage(c, argc, argv);
5083 * 'net rpc share' entrypoint.
5084 * @param argc Standard main() style argc.
5085 * @param argv Standard main() style argv. Initial components are already
5089 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5091 NET_API_STATUS status;
5093 struct functable func[] = {
5099 N_("net rpc share add\n"
5107 N_("net rpc share delete\n"
5112 rpc_share_allowedusers,
5114 N_("Modify allowed users"),
5115 N_("net rpc share allowedusers\n"
5116 " Modify allowed users")
5122 N_("Migrate share to local server"),
5123 N_("net rpc share migrate\n"
5124 " Migrate share to local server")
5131 N_("net rpc share list\n"
5134 {NULL, NULL, 0, NULL, NULL}
5137 status = libnetapi_net_init(&c->netapi_ctx);
5141 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5142 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5143 if (c->opt_kerberos) {
5144 libnetapi_set_use_kerberos(c->netapi_ctx);
5148 if (c->display_usage) {
5153 " Alias for net rpc share list\n"));
5154 net_display_usage_from_functable(func);
5158 return rpc_share_list(c, argc, argv);
5161 return net_run_function(c, argc, argv, "net rpc share", func);
5164 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5165 TALLOC_CTX *mem_ctx,
5166 struct rpc_sh_ctx *ctx,
5167 struct rpc_pipe_client *pipe_hnd,
5168 int argc, const char **argv)
5171 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5174 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5175 TALLOC_CTX *mem_ctx,
5176 struct rpc_sh_ctx *ctx,
5177 struct rpc_pipe_client *pipe_hnd,
5178 int argc, const char **argv)
5180 NET_API_STATUS status;
5181 uint32_t parm_err = 0;
5182 struct SHARE_INFO_2 i2;
5184 if ((argc < 2) || (argc > 3)) {
5185 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5187 return NT_STATUS_INVALID_PARAMETER;
5190 i2.shi2_netname = argv[0];
5191 i2.shi2_type = STYPE_DISKTREE;
5192 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5193 i2.shi2_permissions = 0;
5194 i2.shi2_max_uses = 0;
5195 i2.shi2_current_uses = 0;
5196 i2.shi2_path = argv[1];
5197 i2.shi2_passwd = NULL;
5199 status = NetShareAdd(pipe_hnd->desthost,
5204 return werror_to_ntstatus(W_ERROR(status));
5207 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5208 TALLOC_CTX *mem_ctx,
5209 struct rpc_sh_ctx *ctx,
5210 struct rpc_pipe_client *pipe_hnd,
5211 int argc, const char **argv)
5214 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5215 return NT_STATUS_INVALID_PARAMETER;
5218 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5221 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5222 TALLOC_CTX *mem_ctx,
5223 struct rpc_sh_ctx *ctx,
5224 struct rpc_pipe_client *pipe_hnd,
5225 int argc, const char **argv)
5227 union srvsvc_NetShareInfo info;
5230 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5233 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5234 return NT_STATUS_INVALID_PARAMETER;
5237 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5243 if (!NT_STATUS_IS_OK(status)) {
5244 result = ntstatus_to_werror(status);
5247 if (!W_ERROR_IS_OK(result)) {
5251 d_printf(_("Name: %s\n"), info.info2->name);
5252 d_printf(_("Comment: %s\n"), info.info2->comment);
5253 d_printf(_("Path: %s\n"), info.info2->path);
5254 d_printf(_("Password: %s\n"), info.info2->password);
5257 return werror_to_ntstatus(result);
5260 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5261 struct rpc_sh_ctx *ctx)
5263 static struct rpc_sh_cmd cmds[] = {
5265 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
5266 N_("List available shares") },
5268 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
5269 N_("Add a share") },
5271 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
5272 N_("Delete a share") },
5274 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
5275 N_("Get information about a share") },
5277 { NULL, NULL, 0, NULL, NULL }
5283 /****************************************************************************/
5285 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5287 return net_file_usage(c, argc, argv);
5291 * Close a file on a remote RPC server.
5293 * @param argc Standard main() style argc.
5294 * @param argv Standard main() style argv. Initial components are already
5297 * @return A shell status integer (0 for success).
5299 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5301 if (argc < 1 || c->display_usage) {
5302 return rpc_file_usage(c, argc, argv);
5305 return NetFileClose(c->opt_host, atoi(argv[0]));
5309 * Formatted print of open file info
5311 * @param r struct FILE_INFO_3 contents
5314 static void display_file_info_3(struct FILE_INFO_3 *r)
5316 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5317 r->fi3_id, r->fi3_username, r->fi3_permissions,
5318 r->fi3_num_locks, r->fi3_pathname);
5322 * List files for a user on a remote RPC server.
5324 * @param argc Standard main() style argc.
5325 * @param argv Standard main() style argv. Initial components are already
5328 * @return A shell status integer (0 for success)..
5331 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5333 NET_API_STATUS status;
5334 uint32 preferred_len = 0xffffffff, i;
5335 const char *username=NULL;
5336 uint32_t total_entries = 0;
5337 uint32_t entries_read = 0;
5338 uint32_t resume_handle = 0;
5339 struct FILE_INFO_3 *i3 = NULL;
5341 if (c->display_usage) {
5342 return rpc_file_usage(c, argc, argv);
5345 /* if argc > 0, must be user command */
5347 username = smb_xstrdup(argv[0]);
5350 status = NetFileEnum(c->opt_host,
5354 (uint8_t **)(void *)&i3,
5364 /* Display results */
5367 "\nEnumerating open files on remote server:\n\n"
5368 "\nFileId Opened by Perms Locks Path"
5369 "\n------ --------- ----- ----- ---- \n"));
5370 for (i = 0; i < entries_read; i++) {
5371 display_file_info_3(&i3[i]);
5378 * 'net rpc file' entrypoint.
5379 * @param argc Standard main() style argc.
5380 * @param argv Standard main() style argv. Initial components are already
5384 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5386 NET_API_STATUS status;
5388 struct functable func[] = {
5393 N_("Close opened file"),
5394 N_("net rpc file close\n"
5395 " Close opened file")
5401 N_("List files opened by user"),
5402 N_("net rpc file user\n"
5403 " List files opened by user")
5410 N_("Display information about opened file"),
5411 N_("net rpc file info\n"
5412 " Display information about opened file")
5415 {NULL, NULL, 0, NULL, NULL}
5418 status = libnetapi_net_init(&c->netapi_ctx);
5422 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5423 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5424 if (c->opt_kerberos) {
5425 libnetapi_set_use_kerberos(c->netapi_ctx);
5429 if (c->display_usage) {
5430 d_printf(_("Usage:\n"));
5431 d_printf(_("net rpc file\n"
5432 " List opened files\n"));
5433 net_display_usage_from_functable(func);
5437 return rpc_file_user(c, argc, argv);
5440 return net_run_function(c, argc, argv, "net rpc file", func);
5444 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5446 * All parameters are provided by the run_rpc_command function, except for
5447 * argc, argv which are passed through.
5449 * @param c A net_context structure.
5450 * @param domain_sid The domain sid acquired from the remote server.
5451 * @param cli A cli_state connected to the server.
5452 * @param mem_ctx Talloc context, destroyed on completion of the function.
5453 * @param argc Standard main() style argc.
5454 * @param argv Standard main() style argv. Initial components are already
5457 * @return Normal NTSTATUS return.
5460 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5461 const struct dom_sid *domain_sid,
5462 const char *domain_name,
5463 struct cli_state *cli,
5464 struct rpc_pipe_client *pipe_hnd,
5465 TALLOC_CTX *mem_ctx,
5469 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5471 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5473 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5474 if (!NT_STATUS_IS_OK(status)) {
5477 if (W_ERROR_IS_OK(result)) {
5478 d_printf(_("\nShutdown successfully aborted\n"));
5479 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5481 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5483 return werror_to_ntstatus(result);
5487 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5489 * All parameters are provided by the run_rpc_command function, except for
5490 * argc, argv which are passed through.
5492 * @param c A net_context structure.
5493 * @param domain_sid The domain sid acquired from the remote server.
5494 * @param cli A cli_state connected to the server.
5495 * @param mem_ctx Talloc context, destroyed on completion of the function.
5496 * @param argc Standard main() style argc.
5497 * @param argv Standard main() style argv. Initial components are already
5500 * @return Normal NTSTATUS return.
5503 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5504 const struct dom_sid *domain_sid,
5505 const char *domain_name,
5506 struct cli_state *cli,
5507 struct rpc_pipe_client *pipe_hnd,
5508 TALLOC_CTX *mem_ctx,
5512 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5514 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5516 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5518 if (!NT_STATUS_IS_OK(result)) {
5519 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5522 if (W_ERROR_IS_OK(werr)) {
5523 d_printf(_("\nShutdown successfully aborted\n"));
5524 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5526 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5528 return werror_to_ntstatus(werr);
5532 * ABORT the shutdown of a remote RPC server.
5534 * @param argc Standard main() style argc.
5535 * @param argv Standard main() style argv. Initial components are already
5538 * @return A shell status integer (0 for success).
5541 static int rpc_shutdown_abort(struct net_context *c, int argc,
5546 if (c->display_usage) {
5548 "net rpc abortshutdown\n"
5551 _("Abort a scheduled shutdown"));
5555 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5556 rpc_shutdown_abort_internals, argc, argv);
5561 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5563 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5564 rpc_reg_shutdown_abort_internals,
5569 * Shut down a remote RPC Server via initshutdown pipe.
5571 * All parameters are provided by the run_rpc_command function, except for
5572 * argc, argv which are passed through.
5574 * @param c A net_context structure.
5575 * @param domain_sid The domain sid acquired from the remote server.
5576 * @param cli A cli_state connected to the server.
5577 * @param mem_ctx Talloc context, destroyed on completion of the function.
5578 * @param argc Standard main() style argc.
5579 * @param argv Standard main() style argv. Initial components are already
5582 * @return Normal NTSTATUS return.
5585 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5586 const struct dom_sid *domain_sid,
5587 const char *domain_name,
5588 struct cli_state *cli,
5589 struct rpc_pipe_client *pipe_hnd,
5590 TALLOC_CTX *mem_ctx,
5594 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5596 const char *msg = N_("This machine will be shutdown shortly");
5597 uint32 timeout = 20;
5598 struct lsa_StringLarge msg_string;
5599 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5601 if (c->opt_comment) {
5602 msg = c->opt_comment;
5604 if (c->opt_timeout) {
5605 timeout = c->opt_timeout;
5608 msg_string.string = msg;
5610 /* create an entry */
5611 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5612 &msg_string, timeout, c->opt_force, c->opt_reboot,
5614 if (!NT_STATUS_IS_OK(status)) {
5617 if (W_ERROR_IS_OK(result)) {
5618 d_printf(_("\nShutdown of remote machine succeeded\n"));
5619 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5621 DEBUG(1,("Shutdown of remote machine failed!\n"));
5623 return werror_to_ntstatus(result);
5627 * Shut down a remote RPC Server via winreg pipe.
5629 * All parameters are provided by the run_rpc_command function, except for
5630 * argc, argv which are passed through.
5632 * @param c A net_context structure.
5633 * @param domain_sid The domain sid acquired from the remote server.
5634 * @param cli A cli_state connected to the server.
5635 * @param mem_ctx Talloc context, destroyed on completion of the function.
5636 * @param argc Standard main() style argc.
5637 * @param argv Standard main() style argv. Initial components are already
5640 * @return Normal NTSTATUS return.
5643 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5644 const struct dom_sid *domain_sid,
5645 const char *domain_name,
5646 struct cli_state *cli,
5647 struct rpc_pipe_client *pipe_hnd,
5648 TALLOC_CTX *mem_ctx,
5652 const char *msg = N_("This machine will be shutdown shortly");
5653 uint32 timeout = 20;
5654 struct lsa_StringLarge msg_string;
5657 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5659 if (c->opt_comment) {
5660 msg = c->opt_comment;
5662 msg_string.string = msg;
5664 if (c->opt_timeout) {
5665 timeout = c->opt_timeout;
5668 /* create an entry */
5669 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5670 &msg_string, timeout, c->opt_force, c->opt_reboot,
5672 if (!NT_STATUS_IS_OK(result)) {
5673 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5677 if (W_ERROR_IS_OK(werr)) {
5678 d_printf(_("\nShutdown of remote machine succeeded\n"));
5680 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5681 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5682 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5684 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5687 return werror_to_ntstatus(werr);
5691 * Shut down a remote RPC server.
5693 * @param argc Standard main() style argc.
5694 * @param argv Standard main() style argv. Initial components are already
5697 * @return A shell status integer (0 for success).
5700 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5704 if (c->display_usage) {
5706 "net rpc shutdown\n"
5709 _("Shut down a remote RPC server"));
5713 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5714 rpc_init_shutdown_internals, argc, argv);
5717 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5718 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5719 rpc_reg_shutdown_internals, argc, argv);
5725 /***************************************************************************
5726 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5727 ***************************************************************************/
5730 * Add interdomain trust account to the RPC server.
5731 * All parameters (except for argc and argv) are passed by run_rpc_command
5734 * @param c A net_context structure.
5735 * @param domain_sid The domain sid acquired from the server.
5736 * @param cli A cli_state connected to the server.
5737 * @param mem_ctx Talloc context, destroyed on completion of the function.
5738 * @param argc Standard main() style argc.
5739 * @param argv Standard main() style argv. Initial components are already
5742 * @return normal NTSTATUS return code.
5745 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5746 const struct dom_sid *domain_sid,
5747 const char *domain_name,
5748 struct cli_state *cli,
5749 struct rpc_pipe_client *pipe_hnd,
5750 TALLOC_CTX *mem_ctx,
5754 struct policy_handle connect_pol, domain_pol, user_pol;
5755 NTSTATUS status, result;
5757 struct lsa_String lsa_acct_name;
5759 uint32 acct_flags=0;
5761 uint32_t access_granted = 0;
5762 union samr_UserInfo info;
5763 unsigned int orig_timeout;
5764 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5769 _(" net rpc trustdom add <domain_name> "
5770 "<trust password>\n"));
5771 return NT_STATUS_INVALID_PARAMETER;
5775 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5778 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5779 return NT_STATUS_NO_MEMORY;
5782 strupper_m(acct_name);
5784 init_lsa_String(&lsa_acct_name, acct_name);
5786 /* Get samr policy handle */
5787 status = dcerpc_samr_Connect2(b, mem_ctx,
5789 MAXIMUM_ALLOWED_ACCESS,
5792 if (!NT_STATUS_IS_OK(status)) {
5795 if (!NT_STATUS_IS_OK(result)) {
5800 /* Get domain policy handle */
5801 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5803 MAXIMUM_ALLOWED_ACCESS,
5804 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5807 if (!NT_STATUS_IS_OK(status)) {
5810 if (!NT_STATUS_IS_OK(result)) {
5815 /* This call can take a long time - allow the server to time out.
5816 * 35 seconds should do it. */
5818 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5820 /* Create trusting domain's account */
5821 acb_info = ACB_NORMAL;
5822 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5823 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5824 SAMR_USER_ACCESS_SET_PASSWORD |
5825 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5826 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5828 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5837 if (!NT_STATUS_IS_OK(status)) {
5840 /* And restore our original timeout. */
5841 rpccli_set_timeout(pipe_hnd, orig_timeout);
5843 if (!NT_STATUS_IS_OK(result)) {
5845 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5846 acct_name, nt_errstr(result));
5851 struct samr_CryptPassword crypt_pwd;
5853 ZERO_STRUCT(info.info23);
5855 init_samr_CryptPassword(argv[1],
5856 &cli->user_session_key,
5859 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5860 SAMR_FIELD_NT_PASSWORD_PRESENT;
5861 info.info23.info.acct_flags = ACB_DOMTRUST;
5862 info.info23.password = crypt_pwd;
5864 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5869 if (!NT_STATUS_IS_OK(status)) {
5873 if (!NT_STATUS_IS_OK(result)) {
5875 DEBUG(0,("Could not set trust account password: %s\n",
5876 nt_errstr(result)));
5882 SAFE_FREE(acct_name);
5887 * Create interdomain trust account for a remote domain.
5889 * @param argc Standard argc.
5890 * @param argv Standard argv without initial components.
5892 * @return Integer status (0 means success).
5895 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5897 if (argc > 0 && !c->display_usage) {
5898 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5899 rpc_trustdom_add_internals, argc, argv);
5903 _("net rpc trustdom add <domain_name> <trust "
5911 * Remove interdomain trust account from the RPC server.
5912 * All parameters (except for argc and argv) are passed by run_rpc_command
5915 * @param c A net_context structure.
5916 * @param domain_sid The domain sid acquired from the server.
5917 * @param cli A cli_state connected to the server.
5918 * @param mem_ctx Talloc context, destroyed on completion of the function.
5919 * @param argc Standard main() style argc.
5920 * @param argv Standard main() style argv. Initial components are already
5923 * @return normal NTSTATUS return code.
5926 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5927 const struct dom_sid *domain_sid,
5928 const char *domain_name,
5929 struct cli_state *cli,
5930 struct rpc_pipe_client *pipe_hnd,
5931 TALLOC_CTX *mem_ctx,
5935 struct policy_handle connect_pol, domain_pol, user_pol;
5936 NTSTATUS status, result;
5938 struct dom_sid trust_acct_sid;
5939 struct samr_Ids user_rids, name_types;
5940 struct lsa_String lsa_acct_name;
5941 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5946 _(" net rpc trustdom del <domain_name>\n"));
5947 return NT_STATUS_INVALID_PARAMETER;
5951 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5953 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5955 if (acct_name == NULL)
5956 return NT_STATUS_NO_MEMORY;
5958 strupper_m(acct_name);
5960 /* Get samr policy handle */
5961 status = dcerpc_samr_Connect2(b, mem_ctx,
5963 MAXIMUM_ALLOWED_ACCESS,
5966 if (!NT_STATUS_IS_OK(status)) {
5969 if (!NT_STATUS_IS_OK(result)) {
5974 /* Get domain policy handle */
5975 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5977 MAXIMUM_ALLOWED_ACCESS,
5978 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5981 if (!NT_STATUS_IS_OK(status)) {
5984 if (!NT_STATUS_IS_OK(result)) {
5989 init_lsa_String(&lsa_acct_name, acct_name);
5991 status = dcerpc_samr_LookupNames(b, mem_ctx,
5998 if (!NT_STATUS_IS_OK(status)) {
5999 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6001 acct_name, nt_errstr(status));
6004 if (!NT_STATUS_IS_OK(result)) {
6006 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6008 acct_name, nt_errstr(result) );
6012 status = dcerpc_samr_OpenUser(b, mem_ctx,
6014 MAXIMUM_ALLOWED_ACCESS,
6018 if (!NT_STATUS_IS_OK(status)) {
6019 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6021 acct_name, nt_errstr(status) );
6025 if (!NT_STATUS_IS_OK(result)) {
6027 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6029 acct_name, nt_errstr(result) );
6033 /* append the rid to the domain sid */
6034 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6038 /* remove the sid */
6040 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6044 if (!NT_STATUS_IS_OK(status)) {
6045 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6046 " on user %s failed %s\n"),
6047 acct_name, nt_errstr(status));
6050 if (!NT_STATUS_IS_OK(result)) {
6052 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6053 " on user %s failed %s\n"),
6054 acct_name, nt_errstr(result) );
6061 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6064 if (!NT_STATUS_IS_OK(status)) {
6065 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6067 acct_name, nt_errstr(status));
6071 if (!NT_STATUS_IS_OK(result)) {
6073 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6075 acct_name, nt_errstr(result) );
6079 if (!NT_STATUS_IS_OK(result)) {
6080 d_printf(_("Could not set trust account password: %s\n"),
6090 * Delete interdomain trust account for a remote domain.
6092 * @param argc Standard argc.
6093 * @param argv Standard argv without initial components.
6095 * @return Integer status (0 means success).
6098 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6100 if (argc > 0 && !c->display_usage) {
6101 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
6102 rpc_trustdom_del_internals, argc, argv);
6106 _("net rpc trustdom del <domain>\n"));
6111 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6112 struct cli_state *cli,
6113 TALLOC_CTX *mem_ctx,
6114 const char *domain_name)
6116 char *dc_name = NULL;
6117 const char *buffer = NULL;
6118 struct rpc_pipe_client *netr;
6121 struct dcerpc_binding_handle *b;
6123 /* Use NetServerEnum2 */
6125 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6127 return NT_STATUS_OK;
6130 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6131 for domain %s\n", domain_name));
6133 /* Try netr_GetDcName */
6135 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
6137 if (!NT_STATUS_IS_OK(status)) {
6141 b = netr->binding_handle;
6143 status = dcerpc_netr_GetDcName(b, mem_ctx,
6150 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6154 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6155 for domain %s\n", domain_name));
6157 if (!NT_STATUS_IS_OK(status)) {
6161 return werror_to_ntstatus(result);
6165 * Establish trust relationship to a trusting domain.
6166 * Interdomain account must already be created on remote PDC.
6168 * @param c A net_context structure.
6169 * @param argc Standard argc.
6170 * @param argv Standard argv without initial components.
6172 * @return Integer status (0 means success).
6175 static int rpc_trustdom_establish(struct net_context *c, int argc,
6178 struct cli_state *cli = NULL;
6179 struct sockaddr_storage server_ss;
6180 struct rpc_pipe_client *pipe_hnd = NULL;
6181 struct policy_handle connect_hnd;
6182 TALLOC_CTX *mem_ctx;
6183 NTSTATUS nt_status, result;
6184 struct dom_sid *domain_sid;
6189 union lsa_PolicyInformation *info = NULL;
6190 struct dcerpc_binding_handle *b;
6193 * Connect to \\server\ipc$ as 'our domain' account with password
6196 if (argc != 1 || c->display_usage) {
6199 _("net rpc trustdom establish <domain_name>\n"));
6203 domain_name = smb_xstrdup(argv[0]);
6204 strupper_m(domain_name);
6206 /* account name used at first is our domain's name with '$' */
6207 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6210 strupper_m(acct_name);
6213 * opt_workgroup will be used by connection functions further,
6214 * hence it should be set to remote domain name instead of ours
6216 if (c->opt_workgroup) {
6217 c->opt_workgroup = smb_xstrdup(domain_name);
6220 c->opt_user_name = acct_name;
6222 /* find the domain controller */
6223 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6224 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6228 /* connect to ipc$ as username/password */
6229 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6230 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6232 /* Is it trusting domain account for sure ? */
6233 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6234 nt_errstr(nt_status)));
6238 /* store who we connected to */
6240 saf_store( domain_name, pdc_name );
6243 * Connect to \\server\ipc$ again (this time anonymously)
6246 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6249 if (NT_STATUS_IS_ERR(nt_status)) {
6250 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6251 domain_name, nt_errstr(nt_status)));
6255 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6256 "domain %s", domain_name))) {
6257 DEBUG(0, ("talloc_init() failed\n"));
6262 /* Make sure we're talking to a proper server */
6264 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6265 if (!NT_STATUS_IS_OK(nt_status)) {
6267 talloc_destroy(mem_ctx);
6272 * Call LsaOpenPolicy and LsaQueryInfo
6275 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6277 if (!NT_STATUS_IS_OK(nt_status)) {
6278 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6280 talloc_destroy(mem_ctx);
6284 b = pipe_hnd->binding_handle;
6286 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6288 if (NT_STATUS_IS_ERR(nt_status)) {
6289 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6290 nt_errstr(nt_status)));
6292 talloc_destroy(mem_ctx);
6296 /* Querying info level 5 */
6298 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6300 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6303 if (NT_STATUS_IS_ERR(nt_status)) {
6304 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6305 nt_errstr(nt_status)));
6307 talloc_destroy(mem_ctx);
6310 if (NT_STATUS_IS_ERR(result)) {
6311 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6312 nt_errstr(result)));
6314 talloc_destroy(mem_ctx);
6318 domain_sid = info->account_domain.sid;
6320 /* There should be actually query info level 3 (following nt serv behaviour),
6321 but I still don't know if it's _really_ necessary */
6324 * Store the password in secrets db
6327 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6328 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6330 talloc_destroy(mem_ctx);
6335 * Close the pipes and clean up
6338 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6339 if (NT_STATUS_IS_ERR(nt_status)) {
6340 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6341 nt_errstr(nt_status)));
6343 talloc_destroy(mem_ctx);
6349 talloc_destroy(mem_ctx);
6351 d_printf(_("Trust to domain %s established\n"), domain_name);
6356 * Revoke trust relationship to the remote domain.
6358 * @param c A net_context structure.
6359 * @param argc Standard argc.
6360 * @param argv Standard argv without initial components.
6362 * @return Integer status (0 means success).
6365 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6371 if (argc < 1 || c->display_usage) {
6374 _("net rpc trustdom revoke <domain_name>\n"
6375 " Revoke trust relationship\n"
6376 " domain_name\tName of domain to revoke trust\n"));
6380 /* generate upper cased domain name */
6381 domain_name = smb_xstrdup(argv[0]);
6382 strupper_m(domain_name);
6384 /* delete password of the trust */
6385 if (!pdb_del_trusteddom_pw(domain_name)) {
6386 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6393 SAFE_FREE(domain_name);
6397 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6398 const struct dom_sid *domain_sid,
6399 const char *domain_name,
6400 struct cli_state *cli,
6401 struct rpc_pipe_client *pipe_hnd,
6402 TALLOC_CTX *mem_ctx,
6407 if (!sid_to_fstring(str_sid, domain_sid)) {
6408 return NT_STATUS_UNSUCCESSFUL;
6410 d_printf("%s\n", str_sid);
6411 return NT_STATUS_OK;
6414 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6418 /* convert sid into ascii string */
6419 sid_to_fstring(ascii_sid, dom_sid);
6421 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6424 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6425 TALLOC_CTX *mem_ctx,
6426 struct policy_handle *pol,
6427 struct dom_sid dom_sid,
6428 const char *trusted_dom_name)
6430 NTSTATUS nt_status, result;
6431 union lsa_TrustedDomainInfo *info = NULL;
6432 char *cleartextpwd = NULL;
6433 uint8_t session_key[16];
6434 DATA_BLOB session_key_blob;
6435 DATA_BLOB data = data_blob_null;
6436 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6438 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6441 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6444 if (NT_STATUS_IS_ERR(nt_status)) {
6445 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6446 nt_errstr(nt_status)));
6449 if (NT_STATUS_IS_ERR(result)) {
6451 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6452 nt_errstr(result)));
6456 data = data_blob(info->password.password->data,
6457 info->password.password->length);
6459 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
6460 DEBUG(0, ("Could not retrieve password hash\n"));
6464 session_key_blob = data_blob_const(session_key, sizeof(session_key));
6465 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
6467 if (cleartextpwd == NULL) {
6468 DEBUG(0,("retrieved NULL password\n"));
6469 nt_status = NT_STATUS_UNSUCCESSFUL;
6473 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6474 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6475 nt_status = NT_STATUS_UNSUCCESSFUL;
6479 #ifdef DEBUG_PASSWORD
6480 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6481 "password: [%s]\n", trusted_dom_name,
6482 sid_string_dbg(&dom_sid), cleartextpwd));
6486 SAFE_FREE(cleartextpwd);
6487 data_blob_free(&data);
6492 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6495 /* common variables */
6496 TALLOC_CTX* mem_ctx;
6497 struct cli_state *cli = NULL;
6498 struct rpc_pipe_client *pipe_hnd = NULL;
6499 NTSTATUS nt_status, result;
6500 const char *domain_name = NULL;
6501 struct dom_sid *queried_dom_sid;
6502 struct policy_handle connect_hnd;
6503 union lsa_PolicyInformation *info = NULL;
6505 /* trusted domains listing variables */
6506 unsigned int enum_ctx = 0;
6508 struct lsa_DomainList dom_list;
6510 struct dcerpc_binding_handle *b;
6512 if (c->display_usage) {
6514 "net rpc trustdom vampire\n"
6517 _("Vampire trust relationship from remote server"));
6522 * Listing trusted domains (stored in secrets.tdb, if local)
6525 mem_ctx = talloc_init("trust relationships vampire");
6528 * set domain and pdc name to local samba server (default)
6529 * or to remote one given in command line
6532 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6533 domain_name = c->opt_workgroup;
6534 c->opt_target_workgroup = c->opt_workgroup;
6536 fstrcpy(pdc_name, global_myname());
6537 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6538 c->opt_target_workgroup = domain_name;
6541 /* open \PIPE\lsarpc and open policy handle */
6542 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6543 if (!NT_STATUS_IS_OK(nt_status)) {
6544 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6545 nt_errstr(nt_status)));
6546 talloc_destroy(mem_ctx);
6550 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6552 if (!NT_STATUS_IS_OK(nt_status)) {
6553 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6554 nt_errstr(nt_status) ));
6556 talloc_destroy(mem_ctx);
6560 b = pipe_hnd->binding_handle;
6562 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6564 if (NT_STATUS_IS_ERR(nt_status)) {
6565 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6566 nt_errstr(nt_status)));
6568 talloc_destroy(mem_ctx);
6572 /* query info level 5 to obtain sid of a domain being queried */
6573 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6575 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6579 if (NT_STATUS_IS_ERR(nt_status)) {
6580 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6581 nt_errstr(nt_status)));
6583 talloc_destroy(mem_ctx);
6586 if (NT_STATUS_IS_ERR(result)) {
6587 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6588 nt_errstr(result)));
6590 talloc_destroy(mem_ctx);
6594 queried_dom_sid = info->account_domain.sid;
6597 * Keep calling LsaEnumTrustdom over opened pipe until
6598 * the end of enumeration is reached
6601 d_printf(_("Vampire trusted domains:\n\n"));
6604 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6610 if (NT_STATUS_IS_ERR(nt_status)) {
6611 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6612 nt_errstr(nt_status)));
6614 talloc_destroy(mem_ctx);
6617 if (NT_STATUS_IS_ERR(result)) {
6619 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6620 nt_errstr(result)));
6622 talloc_destroy(mem_ctx);
6627 for (i = 0; i < dom_list.count; i++) {
6629 print_trusted_domain(dom_list.domains[i].sid,
6630 dom_list.domains[i].name.string);
6632 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6633 *dom_list.domains[i].sid,
6634 dom_list.domains[i].name.string);
6635 if (!NT_STATUS_IS_OK(nt_status)) {
6637 talloc_destroy(mem_ctx);
6643 * in case of no trusted domains say something rather
6644 * than just display blank line
6646 if (!dom_list.count) d_printf(_("none\n"));
6648 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6650 /* close this connection before doing next one */
6651 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6652 if (NT_STATUS_IS_ERR(nt_status)) {
6653 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6654 nt_errstr(nt_status)));
6656 talloc_destroy(mem_ctx);
6660 /* close lsarpc pipe and connection to IPC$ */
6663 talloc_destroy(mem_ctx);
6667 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6669 /* common variables */
6670 TALLOC_CTX* mem_ctx;
6671 struct cli_state *cli = NULL, *remote_cli = NULL;
6672 struct rpc_pipe_client *pipe_hnd = NULL;
6673 NTSTATUS nt_status, result;
6674 const char *domain_name = NULL;
6675 struct dom_sid *queried_dom_sid;
6676 int ascii_dom_name_len;
6677 struct policy_handle connect_hnd;
6678 union lsa_PolicyInformation *info = NULL;
6679 struct dcerpc_binding_handle *b = NULL;
6681 /* trusted domains listing variables */
6682 unsigned int num_domains, enum_ctx = 0;
6684 struct lsa_DomainList dom_list;
6688 /* trusting domains listing variables */
6689 struct policy_handle domain_hnd;
6690 struct samr_SamArray *trusts = NULL;
6692 if (c->display_usage) {
6694 "net rpc trustdom list\n"
6697 _("List incoming and outgoing trust relationships"));
6702 * Listing trusted domains (stored in secrets.tdb, if local)
6705 mem_ctx = talloc_init("trust relationships listing");
6708 * set domain and pdc name to local samba server (default)
6709 * or to remote one given in command line
6712 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6713 domain_name = c->opt_workgroup;
6714 c->opt_target_workgroup = c->opt_workgroup;
6716 fstrcpy(pdc_name, global_myname());
6717 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6718 c->opt_target_workgroup = domain_name;
6721 /* open \PIPE\lsarpc and open policy handle */
6722 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6723 if (!NT_STATUS_IS_OK(nt_status)) {
6724 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6725 nt_errstr(nt_status)));
6726 talloc_destroy(mem_ctx);
6730 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6732 if (!NT_STATUS_IS_OK(nt_status)) {
6733 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6734 nt_errstr(nt_status) ));
6736 talloc_destroy(mem_ctx);
6740 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6742 if (NT_STATUS_IS_ERR(nt_status)) {
6743 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6744 nt_errstr(nt_status)));
6746 talloc_destroy(mem_ctx);
6750 /* query info level 5 to obtain sid of a domain being queried */
6751 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6753 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6757 if (NT_STATUS_IS_ERR(nt_status)) {
6758 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6759 nt_errstr(nt_status)));
6761 talloc_destroy(mem_ctx);
6764 if (NT_STATUS_IS_ERR(result)) {
6765 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6766 nt_errstr(result)));
6768 talloc_destroy(mem_ctx);
6772 queried_dom_sid = info->account_domain.sid;
6775 * Keep calling LsaEnumTrustdom over opened pipe until
6776 * the end of enumeration is reached
6779 d_printf(_("Trusted domains list:\n\n"));
6781 found_domain = false;
6784 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6790 if (NT_STATUS_IS_ERR(nt_status)) {
6791 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6792 nt_errstr(nt_status)));
6794 talloc_destroy(mem_ctx);
6797 if (NT_STATUS_IS_ERR(result)) {
6798 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6799 nt_errstr(result)));
6801 talloc_destroy(mem_ctx);
6806 for (i = 0; i < dom_list.count; i++) {
6807 print_trusted_domain(dom_list.domains[i].sid,
6808 dom_list.domains[i].name.string);
6809 found_domain = true;
6813 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6816 * in case of no trusted domains say something rather
6817 * than just display blank line
6819 if (!found_domain) {
6820 d_printf(_("none\n"));
6823 /* close this connection before doing next one */
6824 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6825 if (NT_STATUS_IS_ERR(nt_status)) {
6826 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6827 nt_errstr(nt_status)));
6829 talloc_destroy(mem_ctx);
6833 TALLOC_FREE(pipe_hnd);
6836 * Listing trusting domains (stored in passdb backend, if local)
6839 d_printf(_("\nTrusting domains list:\n\n"));
6842 * Open \PIPE\samr and get needed policy handles
6844 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6846 if (!NT_STATUS_IS_OK(nt_status)) {
6847 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6849 talloc_destroy(mem_ctx);
6853 b = pipe_hnd->binding_handle;
6856 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6858 SAMR_ACCESS_LOOKUP_DOMAIN,
6861 if (!NT_STATUS_IS_OK(nt_status)) {
6862 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6863 nt_errstr(nt_status)));
6865 talloc_destroy(mem_ctx);
6868 if (!NT_STATUS_IS_OK(result)) {
6870 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6871 nt_errstr(result)));
6873 talloc_destroy(mem_ctx);
6877 /* SamrOpenDomain - we have to open domain policy handle in order to be
6878 able to enumerate accounts*/
6879 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6881 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6885 if (!NT_STATUS_IS_OK(nt_status)) {
6886 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6887 nt_errstr(nt_status)));
6889 talloc_destroy(mem_ctx);
6892 if (!NT_STATUS_IS_OK(result)) {
6894 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6895 nt_errstr(result)));
6897 talloc_destroy(mem_ctx);
6902 * perform actual enumeration
6905 found_domain = false;
6907 enum_ctx = 0; /* reset enumeration context from last enumeration */
6910 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
6918 if (NT_STATUS_IS_ERR(nt_status)) {
6919 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6920 nt_errstr(nt_status)));
6922 talloc_destroy(mem_ctx);
6925 if (NT_STATUS_IS_ERR(result)) {
6927 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6928 nt_errstr(result)));
6930 talloc_destroy(mem_ctx);
6934 for (i = 0; i < num_domains; i++) {
6936 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6938 found_domain = true;
6941 * get each single domain's sid (do we _really_ need this ?):
6942 * 1) connect to domain's pdc
6943 * 2) query the pdc for domain's sid
6946 /* get rid of '$' tail */
6947 ascii_dom_name_len = strlen(str);
6948 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6949 str[ascii_dom_name_len - 1] = '\0';
6951 /* set opt_* variables to remote domain */
6953 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6954 c->opt_target_workgroup = c->opt_workgroup;
6956 d_printf("%-20s", str);
6958 /* connect to remote domain controller */
6959 nt_status = net_make_ipc_connection(c,
6960 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6962 if (NT_STATUS_IS_OK(nt_status)) {
6963 /* query for domain's sid */
6964 if (run_rpc_command(
6966 &ndr_table_lsarpc.syntax_id, 0,
6967 rpc_query_domain_sid, argc,
6969 d_printf(_("strange - couldn't get domain's sid\n"));
6971 cli_shutdown(remote_cli);
6974 d_fprintf(stderr, _("domain controller is not "
6975 "responding: %s\n"),
6976 nt_errstr(nt_status));
6977 d_printf(_("couldn't get domain's sid\n"));
6981 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
6983 if (!found_domain) {
6987 /* close opened samr and domain policy handles */
6988 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
6989 if (!NT_STATUS_IS_OK(nt_status)) {
6990 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6993 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
6994 if (!NT_STATUS_IS_OK(nt_status)) {
6995 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6998 /* close samr pipe and connection to IPC$ */
7001 talloc_destroy(mem_ctx);
7006 * Entrypoint for 'net rpc trustdom' code.
7008 * @param argc Standard argc.
7009 * @param argv Standard argv without initial components.
7011 * @return Integer status (0 means success).
7014 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7016 struct functable func[] = {
7021 N_("Add trusting domain's account"),
7022 N_("net rpc trustdom add\n"
7023 " Add trusting domain's account")
7029 N_("Remove trusting domain's account"),
7030 N_("net rpc trustdom del\n"
7031 " Remove trusting domain's account")
7035 rpc_trustdom_establish,
7037 N_("Establish outgoing trust relationship"),
7038 N_("net rpc trustdom establish\n"
7039 " Establish outgoing trust relationship")
7043 rpc_trustdom_revoke,
7045 N_("Revoke outgoing trust relationship"),
7046 N_("net rpc trustdom revoke\n"
7047 " Revoke outgoing trust relationship")
7053 N_("List in- and outgoing domain trusts"),
7054 N_("net rpc trustdom list\n"
7055 " List in- and outgoing domain trusts")
7059 rpc_trustdom_vampire,
7061 N_("Vampire trusts from remote server"),
7062 N_("net rpc trustdom vampire\n"
7063 " Vampire trusts from remote server")
7065 {NULL, NULL, 0, NULL, NULL}
7068 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7072 * Check if a server will take rpc commands
7073 * @param flags Type of server to connect to (PDC, DMB, localhost)
7074 * if the host is not explicitly specified
7075 * @return bool (true means rpc supported)
7077 bool net_rpc_check(struct net_context *c, unsigned flags)
7079 struct cli_state *cli;
7081 struct sockaddr_storage server_ss;
7082 char *server_name = NULL;
7085 /* flags (i.e. server type) may depend on command */
7086 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7089 if ((cli = cli_initialise()) == NULL) {
7093 status = cli_connect(cli, server_name, &server_ss);
7094 if (!NT_STATUS_IS_OK(status))
7096 if (!attempt_netbios_session_request(&cli, global_myname(),
7097 server_name, &server_ss))
7099 status = cli_negprot(cli);
7100 if (!NT_STATUS_IS_OK(status))
7102 if (cli->protocol < PROTOCOL_NT1)
7111 /* dump sam database via samsync rpc calls */
7112 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7113 if (c->display_usage) {
7118 _("Dump remote SAM database"));
7122 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
7123 NET_FLAGS_ANONYMOUS,
7124 rpc_samdump_internals, argc, argv);
7127 /* syncronise sam database via samsync rpc calls */
7128 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7130 struct functable func[] = {
7135 N_("Dump remote SAM database to ldif"),
7136 N_("net rpc vampire ldif\n"
7137 " Dump remote SAM database to LDIF file or "
7144 N_("Dump remote SAM database to Kerberos Keytab"),
7145 N_("net rpc vampire keytab\n"
7146 " Dump remote SAM database to Kerberos keytab "
7153 N_("Dump remote SAM database to passdb"),
7154 N_("net rpc vampire passdb\n"
7155 " Dump remote SAM database to passdb")
7158 {NULL, NULL, 0, NULL, NULL}
7162 if (c->display_usage) {
7167 _("Vampire remote SAM database"));
7171 return rpc_vampire_passdb(c, argc, argv);
7174 return net_run_function(c, argc, argv, "net rpc vampire", func);
7178 * Migrate everything from a print server.
7180 * @param c A net_context structure.
7181 * @param argc Standard main() style argc.
7182 * @param argv Standard main() style argv. Initial components are already
7185 * @return A shell status integer (0 for success).
7187 * The order is important !
7188 * To successfully add drivers the print queues have to exist !
7189 * Applying ACLs should be the last step, because you're easily locked out.
7192 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7197 if (c->display_usage) {
7199 "net rpc printer migrate all\n"
7202 _("Migrate everything from a print server"));
7207 d_printf(_("no server to migrate\n"));
7211 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7212 rpc_printer_migrate_printers_internals, argc,
7217 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7218 rpc_printer_migrate_drivers_internals, argc,
7223 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7224 rpc_printer_migrate_forms_internals, argc, argv);
7228 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7229 rpc_printer_migrate_settings_internals, argc,
7234 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7235 rpc_printer_migrate_security_internals, argc,
7241 * Migrate print drivers from a print server.
7243 * @param c A net_context structure.
7244 * @param argc Standard main() style argc.
7245 * @param argv Standard main() style argv. Initial components are already
7248 * @return A shell status integer (0 for success).
7250 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7253 if (c->display_usage) {
7255 "net rpc printer migrate drivers\n"
7258 _("Migrate print-drivers from a print-server"));
7263 d_printf(_("no server to migrate\n"));
7267 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7268 rpc_printer_migrate_drivers_internals,
7273 * Migrate print-forms from a print-server.
7275 * @param c A net_context structure.
7276 * @param argc Standard main() style argc.
7277 * @param argv Standard main() style argv. Initial components are already
7280 * @return A shell status integer (0 for success).
7282 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7285 if (c->display_usage) {
7287 "net rpc printer migrate forms\n"
7290 _("Migrate print-forms from a print-server"));
7295 d_printf(_("no server to migrate\n"));
7299 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7300 rpc_printer_migrate_forms_internals,
7305 * Migrate printers from a print-server.
7307 * @param c A net_context structure.
7308 * @param argc Standard main() style argc.
7309 * @param argv Standard main() style argv. Initial components are already
7312 * @return A shell status integer (0 for success).
7314 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7317 if (c->display_usage) {
7319 "net rpc printer migrate printers\n"
7322 _("Migrate printers from a print-server"));
7327 d_printf(_("no server to migrate\n"));
7331 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7332 rpc_printer_migrate_printers_internals,
7337 * Migrate printer-ACLs from a print-server
7339 * @param c A net_context structure.
7340 * @param argc Standard main() style argc.
7341 * @param argv Standard main() style argv. Initial components are already
7344 * @return A shell status integer (0 for success).
7346 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7349 if (c->display_usage) {
7351 "net rpc printer migrate security\n"
7354 _("Migrate printer-ACLs from a print-server"));
7359 d_printf(_("no server to migrate\n"));
7363 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7364 rpc_printer_migrate_security_internals,
7369 * Migrate printer-settings from a print-server.
7371 * @param c A net_context structure.
7372 * @param argc Standard main() style argc.
7373 * @param argv Standard main() style argv. Initial components are already
7376 * @return A shell status integer (0 for success).
7378 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7381 if (c->display_usage) {
7383 "net rpc printer migrate settings\n"
7386 _("Migrate printer-settings from a "
7392 d_printf(_("no server to migrate\n"));
7396 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7397 rpc_printer_migrate_settings_internals,
7402 * 'net rpc printer' entrypoint.
7404 * @param c A net_context structure.
7405 * @param argc Standard main() style argc.
7406 * @param argv Standard main() style argv. Initial components are already
7410 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7413 /* ouch: when addriver and setdriver are called from within
7414 rpc_printer_migrate_drivers_internals, the printer-queue already
7417 struct functable func[] = {
7420 rpc_printer_migrate_all,
7422 N_("Migrate all from remote to local print server"),
7423 N_("net rpc printer migrate all\n"
7424 " Migrate all from remote to local print server")
7428 rpc_printer_migrate_drivers,
7430 N_("Migrate drivers to local server"),
7431 N_("net rpc printer migrate drivers\n"
7432 " Migrate drivers to local server")
7436 rpc_printer_migrate_forms,
7438 N_("Migrate froms to local server"),
7439 N_("net rpc printer migrate forms\n"
7440 " Migrate froms to local server")
7444 rpc_printer_migrate_printers,
7446 N_("Migrate printers to local server"),
7447 N_("net rpc printer migrate printers\n"
7448 " Migrate printers to local server")
7452 rpc_printer_migrate_security,
7454 N_("Mirgate printer ACLs to local server"),
7455 N_("net rpc printer migrate security\n"
7456 " Mirgate printer ACLs to local server")
7460 rpc_printer_migrate_settings,
7462 N_("Migrate printer settings to local server"),
7463 N_("net rpc printer migrate settings\n"
7464 " Migrate printer settings to local server")
7466 {NULL, NULL, 0, NULL, NULL}
7469 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7474 * List printers on a remote RPC server.
7476 * @param c A net_context structure.
7477 * @param argc Standard main() style argc.
7478 * @param argv Standard main() style argv. Initial components are already
7481 * @return A shell status integer (0 for success).
7483 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7485 if (c->display_usage) {
7487 "net rpc printer list\n"
7490 _("List printers on a remote RPC server"));
7494 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7495 rpc_printer_list_internals,
7500 * List printer-drivers on a remote RPC server.
7502 * @param c A net_context structure.
7503 * @param argc Standard main() style argc.
7504 * @param argv Standard main() style argv. Initial components are already
7507 * @return A shell status integer (0 for success).
7509 static int rpc_printer_driver_list(struct net_context *c, int argc,
7512 if (c->display_usage) {
7514 "net rpc printer driver\n"
7517 _("List printer-drivers on a remote RPC server"));
7521 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7522 rpc_printer_driver_list_internals,
7527 * Publish printer in ADS via MSRPC.
7529 * @param c A net_context structure.
7530 * @param argc Standard main() style argc.
7531 * @param argv Standard main() style argv. Initial components are already
7534 * @return A shell status integer (0 for success).
7536 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7539 if (c->display_usage) {
7541 "net rpc printer publish publish\n"
7544 _("Publish printer in ADS via MSRPC"));
7548 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7549 rpc_printer_publish_publish_internals,
7554 * Update printer in ADS via MSRPC.
7556 * @param c A net_context structure.
7557 * @param argc Standard main() style argc.
7558 * @param argv Standard main() style argv. Initial components are already
7561 * @return A shell status integer (0 for success).
7563 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7565 if (c->display_usage) {
7567 "net rpc printer publish update\n"
7570 _("Update printer in ADS via MSRPC"));
7574 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7575 rpc_printer_publish_update_internals,
7580 * UnPublish printer in ADS via MSRPC.
7582 * @param c A net_context structure.
7583 * @param argc Standard main() style argc.
7584 * @param argv Standard main() style argv. Initial components are already
7587 * @return A shell status integer (0 for success).
7589 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7592 if (c->display_usage) {
7594 "net rpc printer publish unpublish\n"
7597 _("UnPublish printer in ADS via MSRPC"));
7601 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7602 rpc_printer_publish_unpublish_internals,
7607 * List published printers via MSRPC.
7609 * @param c A net_context structure.
7610 * @param argc Standard main() style argc.
7611 * @param argv Standard main() style argv. Initial components are already
7614 * @return A shell status integer (0 for success).
7616 static int rpc_printer_publish_list(struct net_context *c, int argc,
7619 if (c->display_usage) {
7621 "net rpc printer publish list\n"
7624 _("List published printers via MSRPC"));
7628 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7629 rpc_printer_publish_list_internals,
7635 * Publish printer in ADS.
7637 * @param c A net_context structure.
7638 * @param argc Standard main() style argc.
7639 * @param argv Standard main() style argv. Initial components are already
7642 * @return A shell status integer (0 for success).
7644 static int rpc_printer_publish(struct net_context *c, int argc,
7648 struct functable func[] = {
7651 rpc_printer_publish_publish,
7653 N_("Publish printer in AD"),
7654 N_("net rpc printer publish publish\n"
7655 " Publish printer in AD")
7659 rpc_printer_publish_update,
7661 N_("Update printer in AD"),
7662 N_("net rpc printer publish update\n"
7663 " Update printer in AD")
7667 rpc_printer_publish_unpublish,
7669 N_("Unpublish printer"),
7670 N_("net rpc printer publish unpublish\n"
7671 " Unpublish printer")
7675 rpc_printer_publish_list,
7677 N_("List published printers"),
7678 N_("net rpc printer publish list\n"
7679 " List published printers")
7681 {NULL, NULL, 0, NULL, NULL}
7685 if (c->display_usage) {
7686 d_printf(_("Usage:\n"));
7687 d_printf(_("net rpc printer publish\n"
7688 " List published printers\n"
7689 " Alias of net rpc printer publish "
7691 net_display_usage_from_functable(func);
7694 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7695 rpc_printer_publish_list_internals,
7699 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7705 * Display rpc printer help page.
7707 * @param c A net_context structure.
7708 * @param argc Standard main() style argc.
7709 * @param argv Standard main() style argv. Initial components are already
7712 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7714 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7715 "\tlists all printers on print-server\n\n"));
7716 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7717 "\tlists all printer-drivers on print-server\n\n"));
7718 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7719 "\tpublishes printer settings in Active Directory\n"
7720 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7721 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7722 "\n\tmigrates printers from remote to local server\n\n"));
7723 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7724 "\n\tmigrates printer-settings from remote to local server\n\n"));
7725 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7726 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7727 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7728 "\n\tmigrates printer-forms from remote to local server\n\n"));
7729 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7730 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7731 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7732 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7733 "\tremote to local print-server\n\n"));
7734 net_common_methods_usage(c, argc, argv);
7735 net_common_flags_usage(c, argc, argv);
7737 "\t-v or --verbose\t\t\tgive verbose output\n"
7738 "\t --destination\t\tmigration target server (default: localhost)\n"));
7744 * 'net rpc printer' entrypoint.
7746 * @param c A net_context structure.
7747 * @param argc Standard main() style argc.
7748 * @param argv Standard main() style argv. Initial components are already
7751 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7753 struct functable func[] = {
7758 N_("List all printers on print server"),
7759 N_("net rpc printer list\n"
7760 " List all printers on print server")
7764 rpc_printer_migrate,
7766 N_("Migrate printer to local server"),
7767 N_("net rpc printer migrate\n"
7768 " Migrate printer to local server")
7772 rpc_printer_driver_list,
7774 N_("List printer drivers"),
7775 N_("net rpc printer driver\n"
7776 " List printer drivers")
7780 rpc_printer_publish,
7782 N_("Publish printer in AD"),
7783 N_("net rpc printer publish\n"
7784 " Publish printer in AD")
7786 {NULL, NULL, 0, NULL, NULL}
7790 if (c->display_usage) {
7791 d_printf(_("Usage:\n"));
7792 d_printf(_("net rpc printer\n"
7793 " List printers\n"));
7794 net_display_usage_from_functable(func);
7797 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7798 rpc_printer_list_internals,
7802 return net_run_function(c, argc, argv, "net rpc printer", func);
7806 * 'net rpc' entrypoint.
7808 * @param c A net_context structure.
7809 * @param argc Standard main() style argc.
7810 * @param argv Standard main() style argv. Initial components are already
7814 int net_rpc(struct net_context *c, int argc, const char **argv)
7816 NET_API_STATUS status;
7818 struct functable func[] = {
7823 N_("Modify global audit settings"),
7824 N_("net rpc audit\n"
7825 " Modify global audit settings")
7831 N_("Show basic info about a domain"),
7833 " Show basic info about a domain")
7839 N_("Join a domain"),
7847 N_("Join a domain created in server manager"),
7848 N_("net rpc oldjoin\n"
7849 " Join a domain created in server manager")
7855 N_("Test that a join is valid"),
7856 N_("net rpc testjoin\n"
7857 " Test that a join is valid")
7863 N_("List/modify users"),
7865 " List/modify users")
7871 N_("Change a user password"),
7872 N_("net rpc password\n"
7873 " Change a user password\n"
7874 " Alias for net rpc user password")
7880 N_("List/modify groups"),
7881 N_("net rpc group\n"
7882 " List/modify groups")
7888 N_("List/modify shares"),
7889 N_("net rpc share\n"
7890 " List/modify shares")
7896 N_("List open files"),
7904 N_("List/modify printers"),
7905 N_("net rpc printer\n"
7906 " List/modify printers")
7910 net_rpc_changetrustpw,
7912 N_("Change trust account password"),
7913 N_("net rpc changetrustpw\n"
7914 " Change trust account password")
7920 N_("Modify domain trusts"),
7921 N_("net rpc trustdom\n"
7922 " Modify domain trusts")
7928 N_("Abort a remote shutdown"),
7929 N_("net rpc abortshutdown\n"
7930 " Abort a remote shutdown")
7936 N_("Shutdown a remote server"),
7937 N_("net rpc shutdown\n"
7938 " Shutdown a remote server")
7944 N_("Dump SAM data of remote NT PDC"),
7945 N_("net rpc samdump\n"
7946 " Dump SAM data of remote NT PDC")
7952 N_("Sync a remote NT PDC's data into local passdb"),
7953 N_("net rpc vampire\n"
7954 " Sync a remote NT PDC's data into local passdb")
7960 N_("Fetch the domain sid into local secrets.tdb"),
7961 N_("net rpc getsid\n"
7962 " Fetch the domain sid into local secrets.tdb")
7968 N_("Manage privileges assigned to SID"),
7969 N_("net rpc rights\n"
7970 " Manage privileges assigned to SID")
7976 N_("Start/stop/query remote services"),
7977 N_("net rpc service\n"
7978 " Start/stop/query remote services")
7984 N_("Manage registry hives"),
7985 N_("net rpc registry\n"
7986 " Manage registry hives")
7992 N_("Open interactive shell on remote server"),
7993 N_("net rpc shell\n"
7994 " Open interactive shell on remote server")
7996 {NULL, NULL, 0, NULL, NULL}
7999 status = libnetapi_net_init(&c->netapi_ctx);
8003 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8004 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8005 if (c->opt_kerberos) {
8006 libnetapi_set_use_kerberos(c->netapi_ctx);
8008 if (c->opt_ccache) {
8009 libnetapi_set_use_ccache(c->netapi_ctx);
8012 return net_run_function(c, argc, argv, "net rpc", func);