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"
43 #include "nsswitch/libwbclient/wbclient.h"
45 static int net_mode_share;
46 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
51 * @brief RPC based subcommands for the 'net' utility.
53 * This file should contain much of the functionality that used to
54 * be found in rpcclient, execpt that the commands should change
55 * less often, and the fucntionality should be sane (the user is not
56 * expected to know a rid/sid before they conduct an operation etc.)
58 * @todo Perhaps eventually these should be split out into a number
59 * of files, as this could get quite big.
64 * Many of the RPC functions need the domain sid. This function gets
65 * it at the start of every run
67 * @param cli A cli_state already connected to the remote machine
69 * @return The Domain SID of the remote machine.
72 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
73 struct dom_sid **domain_sid,
74 const char **domain_name)
76 struct rpc_pipe_client *lsa_pipe = NULL;
77 struct policy_handle pol;
78 NTSTATUS status, result;
79 union lsa_PolicyInformation *info = NULL;
80 struct dcerpc_binding_handle *b;
82 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
84 if (!NT_STATUS_IS_OK(status)) {
85 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
89 b = lsa_pipe->binding_handle;
91 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
92 SEC_FLAG_MAXIMUM_ALLOWED,
94 if (!NT_STATUS_IS_OK(status)) {
95 d_fprintf(stderr, "open_policy %s: %s\n",
101 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
103 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
106 if (!NT_STATUS_IS_OK(status)) {
107 d_fprintf(stderr, "lsaquery %s: %s\n",
112 if (!NT_STATUS_IS_OK(result)) {
113 d_fprintf(stderr, "lsaquery %s: %s\n",
119 *domain_name = info->account_domain.name.string;
120 *domain_sid = info->account_domain.sid;
122 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
123 TALLOC_FREE(lsa_pipe);
129 * Run a single RPC command, from start to finish.
131 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
132 * @param conn_flag a NET_FLAG_ combination. Passed to
133 * net_make_ipc_connection.
134 * @param argc Standard main() style argc.
135 * @param argv Standard main() style argv. Initial components are already
137 * @return A shell status integer (0 for success).
140 int run_rpc_command(struct net_context *c,
141 struct cli_state *cli_arg,
142 const struct ndr_syntax_id *interface,
148 struct cli_state *cli = NULL;
149 struct rpc_pipe_client *pipe_hnd = NULL;
152 struct dom_sid *domain_sid;
153 const char *domain_name;
156 /* make use of cli_state handed over as an argument, if possible */
158 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
159 if (!NT_STATUS_IS_OK(nt_status)) {
160 DEBUG(1, ("failed to make ipc connection: %s\n",
161 nt_errstr(nt_status)));
174 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
175 DEBUG(0, ("talloc_init() failed\n"));
179 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
181 if (!NT_STATUS_IS_OK(nt_status)) {
185 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
186 if (lp_client_schannel()
187 && (ndr_syntax_id_equal(interface,
188 &ndr_table_netlogon.syntax_id))) {
189 /* Always try and create an schannel netlogon pipe. */
190 nt_status = cli_rpc_pipe_open_schannel(
191 cli, interface, NCACN_NP,
192 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
194 if (!NT_STATUS_IS_OK(nt_status)) {
195 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
196 nt_errstr(nt_status) ));
200 if (conn_flags & NET_FLAGS_SEAL) {
201 nt_status = cli_rpc_pipe_open_ntlmssp(
203 (conn_flags & NET_FLAGS_TCP) ?
204 NCACN_IP_TCP : NCACN_NP,
205 DCERPC_AUTH_LEVEL_PRIVACY,
206 lp_workgroup(), c->opt_user_name,
207 c->opt_password, &pipe_hnd);
209 nt_status = cli_rpc_pipe_open_noauth(
213 if (!NT_STATUS_IS_OK(nt_status)) {
214 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
215 get_pipe_name_from_syntax(
216 talloc_tos(), interface),
217 nt_errstr(nt_status) ));
223 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
225 if (!NT_STATUS_IS_OK(nt_status)) {
226 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
229 DEBUG(5, ("rpc command function succedded\n"));
232 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
234 TALLOC_FREE(pipe_hnd);
239 /* close the connection only if it was opened here */
244 talloc_destroy(mem_ctx);
249 * Force a change of the trust acccount password.
251 * All parameters are provided by the run_rpc_command function, except for
252 * argc, argv which are passed through.
254 * @param domain_sid The domain sid acquired from the remote server.
255 * @param cli A cli_state connected to the server.
256 * @param mem_ctx Talloc context, destroyed on completion of the function.
257 * @param argc Standard main() style argc.
258 * @param argv Standard main() style argv. Initial components are already
261 * @return Normal NTSTATUS return.
264 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
265 const struct dom_sid *domain_sid,
266 const char *domain_name,
267 struct cli_state *cli,
268 struct rpc_pipe_client *pipe_hnd,
275 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
276 if (!NT_STATUS_IS_OK(status)) {
277 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
286 * Force a change of the trust acccount password.
288 * @param argc Standard main() style argc.
289 * @param argv Standard main() style argv. Initial components are already
292 * @return A shell status integer (0 for success).
295 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
297 if (c->display_usage) {
299 "net rpc changetrustpw\n"
302 _("Change the machine trust password"));
306 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
307 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
308 rpc_changetrustpw_internals,
313 * Join a domain, the old way.
315 * This uses 'machinename' as the inital password, and changes it.
317 * The password should be created with 'server manager' or equiv first.
319 * All parameters are provided by the run_rpc_command function, except for
320 * argc, argv which are passed through.
322 * @param domain_sid The domain sid acquired from the remote server.
323 * @param cli A cli_state connected to the server.
324 * @param mem_ctx Talloc context, destroyed on completion of the function.
325 * @param argc Standard main() style argc.
326 * @param argv Standard main() style argv. Initial components are already
329 * @return Normal NTSTATUS return.
332 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
333 const struct dom_sid *domain_sid,
334 const char *domain_name,
335 struct cli_state *cli,
336 struct rpc_pipe_client *pipe_hnd,
342 fstring trust_passwd;
343 unsigned char orig_trust_passwd_hash[16];
345 enum netr_SchannelType sec_channel_type;
347 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
349 if (!NT_STATUS_IS_OK(result)) {
350 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
353 nt_errstr(result) ));
358 check what type of join - if the user want's to join as
359 a BDC, the server must agree that we are a BDC.
362 sec_channel_type = get_sec_channel_type(argv[0]);
364 sec_channel_type = get_sec_channel_type(NULL);
367 fstrcpy(trust_passwd, global_myname());
368 strlower_m(trust_passwd);
371 * Machine names can be 15 characters, but the max length on
372 * a password is 14. --jerry
375 trust_passwd[14] = '\0';
377 E_md4hash(trust_passwd, orig_trust_passwd_hash);
379 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
381 orig_trust_passwd_hash,
384 if (NT_STATUS_IS_OK(result))
385 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
388 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
389 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
390 result = NT_STATUS_UNSUCCESSFUL;
397 * Join a domain, the old way.
399 * @param argc Standard main() style argc.
400 * @param argv Standard main() style argv. Initial components are already
403 * @return A shell status integer (0 for success).
406 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
408 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
409 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
410 rpc_oldjoin_internals,
415 * Join a domain, the old way. This function exists to allow
416 * the message to be displayed when oldjoin was explicitly
417 * requested, but not when it was implied by "net rpc join".
419 * @param argc Standard main() style argc.
420 * @param argv Standard main() style argv. Initial components are already
423 * @return A shell status integer (0 for success).
426 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
430 if (c->display_usage) {
435 _("Join a domain the old way"));
439 rc = net_rpc_perform_oldjoin(c, argc, argv);
442 d_fprintf(stderr, _("Failed to join domain\n"));
449 * 'net rpc join' entrypoint.
450 * @param argc Standard main() style argc.
451 * @param argv Standard main() style argv. Initial components are already
454 * Main 'net_rpc_join()' (where the admin username/password is used) is
456 * Try to just change the password, but if that doesn't work, use/prompt
457 * for a username/password.
460 int net_rpc_join(struct net_context *c, int argc, const char **argv)
462 if (c->display_usage) {
465 _("net rpc join -U <username>[%%password] <type>\n"
467 " username\tName of the admin user"
468 " password\tPassword of the admin user, will "
469 "prompt if not specified\n"
470 " type\tCan be one of the following:\n"
471 "\t\tMEMBER\tJoin as member server (default)\n"
472 "\t\tBDC\tJoin as BDC\n"
473 "\t\tPDC\tJoin as PDC\n"));
477 if (lp_server_role() == ROLE_STANDALONE) {
478 d_printf(_("cannot join as standalone machine\n"));
482 if (strlen(global_myname()) > 15) {
483 d_printf(_("Our netbios name can be at most 15 chars long, "
484 "\"%s\" is %u chars long\n"),
485 global_myname(), (unsigned int)strlen(global_myname()));
489 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
492 return net_rpc_join_newstyle(c, argc, argv);
496 * display info about a rpc domain
498 * All parameters are provided by the run_rpc_command function, except for
499 * argc, argv which are passed through.
501 * @param domain_sid The domain sid acquired from the remote server
502 * @param cli A cli_state connected to the server.
503 * @param mem_ctx Talloc context, destroyed on completion of the function.
504 * @param argc Standard main() style argc.
505 * @param argv Standard main() style argv. Initial components are already
508 * @return Normal NTSTATUS return.
511 NTSTATUS rpc_info_internals(struct net_context *c,
512 const struct dom_sid *domain_sid,
513 const char *domain_name,
514 struct cli_state *cli,
515 struct rpc_pipe_client *pipe_hnd,
520 struct policy_handle connect_pol, domain_pol;
521 NTSTATUS status, result;
522 union samr_DomainInfo *info = NULL;
524 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
526 sid_to_fstring(sid_str, domain_sid);
528 /* Get sam policy handle */
529 status = dcerpc_samr_Connect2(b, mem_ctx,
531 MAXIMUM_ALLOWED_ACCESS,
534 if (!NT_STATUS_IS_OK(status)) {
535 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
540 if (!NT_STATUS_IS_OK(result)) {
542 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
547 /* Get domain policy handle */
548 status = dcerpc_samr_OpenDomain(b, mem_ctx,
550 MAXIMUM_ALLOWED_ACCESS,
551 CONST_DISCARD(struct dom_sid2 *, domain_sid),
554 if (!NT_STATUS_IS_OK(status)) {
555 d_fprintf(stderr, _("Could not open domain: %s\n"),
559 if (!NT_STATUS_IS_OK(result)) {
561 d_fprintf(stderr, _("Could not open domain: %s\n"),
566 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
571 if (!NT_STATUS_IS_OK(status)) {
575 if (NT_STATUS_IS_OK(result)) {
576 d_printf(_("Domain Name: %s\n"),
577 info->general.domain_name.string);
578 d_printf(_("Domain SID: %s\n"), sid_str);
579 d_printf(_("Sequence number: %llu\n"),
580 (unsigned long long)info->general.sequence_num);
581 d_printf(_("Num users: %u\n"), info->general.num_users);
582 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
583 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
591 * 'net rpc info' entrypoint.
592 * @param argc Standard main() style argc.
593 * @param argv Standard main() style argv. Initial components are already
597 int net_rpc_info(struct net_context *c, int argc, const char **argv)
599 if (c->display_usage) {
604 _("Display information about the domain"));
608 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
609 NET_FLAGS_PDC, rpc_info_internals,
614 * Fetch domain SID into the local secrets.tdb.
616 * All parameters are provided by the run_rpc_command function, except for
617 * argc, argv which are passed through.
619 * @param domain_sid The domain sid acquired from the remote server.
620 * @param cli A cli_state connected to the server.
621 * @param mem_ctx Talloc context, destroyed on completion of the function.
622 * @param argc Standard main() style argc.
623 * @param argv Standard main() style argv. Initial components are already
626 * @return Normal NTSTATUS return.
629 static NTSTATUS rpc_getsid_internals(struct net_context *c,
630 const struct dom_sid *domain_sid,
631 const char *domain_name,
632 struct cli_state *cli,
633 struct rpc_pipe_client *pipe_hnd,
640 sid_to_fstring(sid_str, domain_sid);
641 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
642 sid_str, domain_name);
644 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
645 DEBUG(0,("Can't store domain SID\n"));
646 return NT_STATUS_UNSUCCESSFUL;
653 * 'net rpc getsid' entrypoint.
654 * @param argc Standard main() style argc.
655 * @param argv Standard main() style argv. Initial components are already
659 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
661 int conn_flags = NET_FLAGS_PDC;
663 if (!c->opt_user_specified) {
664 conn_flags |= NET_FLAGS_ANONYMOUS;
667 if (c->display_usage) {
672 _("Fetch domain SID into local secrets.tdb"));
676 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
678 rpc_getsid_internals,
682 /****************************************************************************/
685 * Basic usage function for 'net rpc user'.
686 * @param argc Standard main() style argc.
687 * @param argv Standard main() style argv. Initial components are already
691 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
693 return net_user_usage(c, argc, argv);
697 * Add a new user to a remote RPC server.
699 * @param argc Standard main() style argc.
700 * @param argv Standard main() style argv. Initial components are already
703 * @return A shell status integer (0 for success).
706 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
708 NET_API_STATUS status;
709 struct USER_INFO_1 info1;
710 uint32_t parm_error = 0;
712 if (argc < 1 || c->display_usage) {
713 rpc_user_usage(c, argc, argv);
719 info1.usri1_name = argv[0];
721 info1.usri1_password = argv[1];
724 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
727 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
728 argv[0], libnetapi_get_error_string(c->netapi_ctx,
732 d_printf(_("Added user '%s'.\n"), argv[0]);
739 * Rename a user on a remote RPC server.
741 * @param argc Standard main() style argc.
742 * @param argv Standard main() style argv. Initial components are already
745 * @return A shell status integer (0 for success).
748 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
750 NET_API_STATUS status;
751 struct USER_INFO_0 u0;
752 uint32_t parm_err = 0;
754 if (argc != 2 || c->display_usage) {
755 rpc_user_usage(c, argc, argv);
759 u0.usri0_name = argv[1];
761 status = NetUserSetInfo(c->opt_host, argv[0],
762 0, (uint8_t *)&u0, &parm_err);
765 _("Failed to rename user from %s to %s - %s\n"),
767 libnetapi_get_error_string(c->netapi_ctx, status));
769 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
776 * Set a user's primary group
778 * @param argc Standard main() style argc.
779 * @param argv Standard main() style argv. Initial components are already
782 * @return A shell status integer (0 for success).
785 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
788 NET_API_STATUS status;
790 struct GROUP_INFO_2 *g2;
791 struct USER_INFO_1051 u1051;
792 uint32_t parm_err = 0;
794 if (argc != 2 || c->display_usage) {
795 rpc_user_usage(c, argc, argv);
799 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
801 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
803 libnetapi_get_error_string(c->netapi_ctx, status));
806 g2 = (struct GROUP_INFO_2 *)buffer;
808 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
810 NetApiBufferFree(buffer);
812 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
813 (uint8_t *)&u1051, &parm_err);
816 _("Failed to set user's primary group %s to %s - "
817 "%s\n"), argv[0], argv[1],
818 libnetapi_get_error_string(c->netapi_ctx, status));
820 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
827 * Delete a user from a remote RPC server.
829 * @param argc Standard main() style argc.
830 * @param argv Standard main() style argv. Initial components are already
833 * @return A shell status integer (0 for success).
836 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
838 NET_API_STATUS status;
840 if (argc < 1 || c->display_usage) {
841 rpc_user_usage(c, argc, argv);
845 status = NetUserDel(c->opt_host, argv[0]);
848 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
850 libnetapi_get_error_string(c->netapi_ctx, status));
853 d_printf(_("Deleted user '%s'.\n"), argv[0]);
860 * Set a user's password on a remote RPC server.
862 * @param argc Standard main() style argc.
863 * @param argv Standard main() style argv. Initial components are already
866 * @return A shell status integer (0 for success).
869 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
871 NET_API_STATUS status;
873 struct USER_INFO_1003 u1003;
874 uint32_t parm_err = 0;
877 if (argc < 1 || c->display_usage) {
878 rpc_user_usage(c, argc, argv);
883 u1003.usri1003_password = argv[1];
885 ret = asprintf(&prompt, _("Enter new password for %s:"),
890 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
892 if (u1003.usri1003_password == NULL) {
897 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
899 /* Display results */
902 _("Failed to set password for '%s' with error: %s.\n"),
903 argv[0], libnetapi_get_error_string(c->netapi_ctx,
912 * List a user's groups from a remote RPC server.
914 * @param argc Standard main() style argc.
915 * @param argv Standard main() style argv. Initial components are already
918 * @return A shell status integer (0 for success)
921 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
924 NET_API_STATUS status;
925 struct GROUP_USERS_INFO_0 *u0 = NULL;
926 uint32_t entries_read = 0;
927 uint32_t total_entries = 0;
931 if (argc < 1 || c->display_usage) {
932 rpc_user_usage(c, argc, argv);
936 status = NetUserGetGroups(c->opt_host,
939 (uint8_t **)(void *)&u0,
945 _("Failed to get groups for '%s' with error: %s.\n"),
946 argv[0], libnetapi_get_error_string(c->netapi_ctx,
951 for (i=0; i < entries_read; i++) {
952 printf("%s\n", u0->grui0_name);
960 * List users on a remote RPC server.
962 * All parameters are provided by the run_rpc_command function, except for
963 * argc, argv which are passed through.
965 * @param domain_sid The domain sid acquired from the remote server.
966 * @param cli A cli_state connected to the server.
967 * @param mem_ctx Talloc context, destroyed on completion of the function.
968 * @param argc Standard main() style argc.
969 * @param argv Standard main() style argv. Initial components are already
972 * @return Normal NTSTATUS return.
975 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
977 NET_API_STATUS status;
978 uint32_t start_idx=0, num_entries, i, loop_count = 0;
979 struct NET_DISPLAY_USER *info = NULL;
982 /* Query domain users */
983 if (c->opt_long_list_entries)
984 d_printf(_("\nUser name Comment"
985 "\n-----------------------------\n"));
987 uint32_t max_entries, max_size;
989 dcerpc_get_query_dispinfo_params(
990 loop_count, &max_entries, &max_size);
992 status = NetQueryDisplayInformation(c->opt_host,
999 if (status != 0 && status != ERROR_MORE_DATA) {
1003 info = (struct NET_DISPLAY_USER *)buffer;
1005 for (i = 0; i < num_entries; i++) {
1007 if (c->opt_long_list_entries)
1008 printf("%-21.21s %s\n", info->usri1_name,
1009 info->usri1_comment);
1011 printf("%s\n", info->usri1_name);
1015 NetApiBufferFree(buffer);
1018 start_idx += num_entries;
1020 } while (status == ERROR_MORE_DATA);
1026 * 'net rpc user' entrypoint.
1027 * @param argc Standard main() style argc.
1028 * @param argv Standard main() style argv. Initial components are already
1032 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1034 NET_API_STATUS status;
1036 struct functable func[] = {
1041 N_("Add specified user"),
1042 N_("net rpc user add\n"
1043 " Add specified user")
1049 N_("List domain groups of user"),
1050 N_("net rpc user info\n"
1051 " List domain groups of user")
1057 N_("Remove specified user"),
1058 N_("net rpc user delete\n"
1059 " Remove specified user")
1065 N_("Change user password"),
1066 N_("net rpc user password\n"
1067 " Change user password")
1073 N_("Rename specified user"),
1074 N_("net rpc user rename\n"
1075 " Rename specified user")
1079 rpc_user_setprimarygroup,
1081 "Set a user's primary group",
1082 "net rpc user setprimarygroup\n"
1083 " Set a user's primary group"
1085 {NULL, NULL, 0, NULL, NULL}
1088 status = libnetapi_net_init(&c->netapi_ctx);
1092 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1093 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1094 if (c->opt_kerberos) {
1095 libnetapi_set_use_kerberos(c->netapi_ctx);
1099 if (c->display_usage) {
1104 _("List all users"));
1105 net_display_usage_from_functable(func);
1109 return rpc_user_list(c, argc, argv);
1112 return net_run_function(c, argc, argv, "net rpc user", func);
1115 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1116 TALLOC_CTX *mem_ctx,
1117 struct rpc_sh_ctx *ctx,
1118 struct rpc_pipe_client *pipe_hnd,
1119 int argc, const char **argv)
1121 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1124 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1125 TALLOC_CTX *mem_ctx,
1126 struct rpc_sh_ctx *ctx,
1127 struct rpc_pipe_client *pipe_hnd,
1128 int argc, const char **argv)
1130 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1133 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1134 TALLOC_CTX *mem_ctx,
1135 struct rpc_sh_ctx *ctx,
1136 struct rpc_pipe_client *pipe_hnd,
1137 int argc, const char **argv,
1139 struct net_context *c,
1140 TALLOC_CTX *mem_ctx,
1141 struct rpc_sh_ctx *ctx,
1142 struct rpc_pipe_client *pipe_hnd,
1143 struct policy_handle *user_hnd,
1144 int argc, const char **argv))
1146 struct policy_handle connect_pol, domain_pol, user_pol;
1147 NTSTATUS status, result;
1150 enum lsa_SidType type;
1151 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1154 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1156 return NT_STATUS_INVALID_PARAMETER;
1159 ZERO_STRUCT(connect_pol);
1160 ZERO_STRUCT(domain_pol);
1161 ZERO_STRUCT(user_pol);
1163 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1164 argv[0], NULL, NULL, &sid, &type);
1165 if (!NT_STATUS_IS_OK(status)) {
1166 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1171 if (type != SID_NAME_USER) {
1172 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1173 sid_type_lookup(type));
1174 status = NT_STATUS_NO_SUCH_USER;
1178 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1179 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1180 status = NT_STATUS_NO_SUCH_USER;
1184 status = dcerpc_samr_Connect2(b, mem_ctx,
1186 MAXIMUM_ALLOWED_ACCESS,
1189 if (!NT_STATUS_IS_OK(status)) {
1192 if (!NT_STATUS_IS_OK(result)) {
1197 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1199 MAXIMUM_ALLOWED_ACCESS,
1203 if (!NT_STATUS_IS_OK(status)) {
1206 if (!NT_STATUS_IS_OK(result)) {
1211 status = dcerpc_samr_OpenUser(b, mem_ctx,
1213 MAXIMUM_ALLOWED_ACCESS,
1217 if (!NT_STATUS_IS_OK(status)) {
1220 if (!NT_STATUS_IS_OK(result)) {
1225 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1228 if (is_valid_policy_hnd(&user_pol)) {
1229 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1231 if (is_valid_policy_hnd(&domain_pol)) {
1232 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1234 if (is_valid_policy_hnd(&connect_pol)) {
1235 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1240 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1241 TALLOC_CTX *mem_ctx,
1242 struct rpc_sh_ctx *ctx,
1243 struct rpc_pipe_client *pipe_hnd,
1244 struct policy_handle *user_hnd,
1245 int argc, const char **argv)
1247 NTSTATUS status, result;
1248 union samr_UserInfo *info = NULL;
1249 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1252 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1254 return NT_STATUS_INVALID_PARAMETER;
1257 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1262 if (!NT_STATUS_IS_OK(status)) {
1265 if (!NT_STATUS_IS_OK(result)) {
1269 d_printf(_("user rid: %d, group rid: %d\n"),
1271 info->info21.primary_gid);
1276 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1277 TALLOC_CTX *mem_ctx,
1278 struct rpc_sh_ctx *ctx,
1279 struct rpc_pipe_client *pipe_hnd,
1280 int argc, const char **argv)
1282 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1283 rpc_sh_user_show_internals);
1286 #define FETCHSTR(name, rec) \
1287 do { if (strequal(ctx->thiscmd, name)) { \
1288 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1291 #define SETSTR(name, rec, flag) \
1292 do { if (strequal(ctx->thiscmd, name)) { \
1293 init_lsa_String(&(info->info21.rec), argv[0]); \
1294 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1297 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1298 TALLOC_CTX *mem_ctx,
1299 struct rpc_sh_ctx *ctx,
1300 struct rpc_pipe_client *pipe_hnd,
1301 struct policy_handle *user_hnd,
1302 int argc, const char **argv)
1304 NTSTATUS status, result;
1305 const char *username;
1306 const char *oldval = "";
1307 union samr_UserInfo *info = NULL;
1308 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1311 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1312 _("Usage:"), ctx->whoami);
1313 return NT_STATUS_INVALID_PARAMETER;
1316 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1321 if (!NT_STATUS_IS_OK(status)) {
1324 if (!NT_STATUS_IS_OK(result)) {
1328 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1330 FETCHSTR("fullname", full_name);
1331 FETCHSTR("homedir", home_directory);
1332 FETCHSTR("homedrive", home_drive);
1333 FETCHSTR("logonscript", logon_script);
1334 FETCHSTR("profilepath", profile_path);
1335 FETCHSTR("description", description);
1338 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1342 if (strcmp(argv[0], "NULL") == 0) {
1346 ZERO_STRUCT(info->info21);
1348 SETSTR("fullname", full_name, FULL_NAME);
1349 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1350 SETSTR("homedrive", home_drive, HOME_DRIVE);
1351 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1352 SETSTR("profilepath", profile_path, PROFILE_PATH);
1353 SETSTR("description", description, DESCRIPTION);
1355 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1360 if (!NT_STATUS_IS_OK(status)) {
1366 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1367 ctx->thiscmd, oldval, argv[0]);
1374 #define HANDLEFLG(name, rec) \
1375 do { if (strequal(ctx->thiscmd, name)) { \
1376 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1378 newflags = oldflags | ACB_##rec; \
1380 newflags = oldflags & ~ACB_##rec; \
1383 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1384 TALLOC_CTX *mem_ctx,
1385 struct rpc_sh_ctx *ctx,
1386 struct rpc_pipe_client *pipe_hnd,
1387 int argc, const char **argv)
1389 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1390 rpc_sh_user_str_edit_internals);
1393 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1394 TALLOC_CTX *mem_ctx,
1395 struct rpc_sh_ctx *ctx,
1396 struct rpc_pipe_client *pipe_hnd,
1397 struct policy_handle *user_hnd,
1398 int argc, const char **argv)
1400 NTSTATUS status, result;
1401 const char *username;
1402 const char *oldval = "unknown";
1403 uint32 oldflags, newflags;
1405 union samr_UserInfo *info = NULL;
1406 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1409 ((argc == 1) && !strequal(argv[0], "yes") &&
1410 !strequal(argv[0], "no"))) {
1411 /* TRANSATORS: The yes|no here are program keywords. Please do
1413 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1415 return NT_STATUS_INVALID_PARAMETER;
1418 newval = strequal(argv[0], "yes");
1420 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1425 if (!NT_STATUS_IS_OK(status)) {
1428 if (!NT_STATUS_IS_OK(result)) {
1432 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1433 oldflags = info->info21.acct_flags;
1434 newflags = info->info21.acct_flags;
1436 HANDLEFLG("disabled", DISABLED);
1437 HANDLEFLG("pwnotreq", PWNOTREQ);
1438 HANDLEFLG("autolock", AUTOLOCK);
1439 HANDLEFLG("pwnoexp", PWNOEXP);
1442 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1447 ZERO_STRUCT(info->info21);
1449 info->info21.acct_flags = newflags;
1450 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1452 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1457 if (!NT_STATUS_IS_OK(status)) {
1461 if (NT_STATUS_IS_OK(result)) {
1462 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1463 ctx->thiscmd, oldval, argv[0]);
1471 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1472 TALLOC_CTX *mem_ctx,
1473 struct rpc_sh_ctx *ctx,
1474 struct rpc_pipe_client *pipe_hnd,
1475 int argc, const char **argv)
1477 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1478 rpc_sh_user_flag_edit_internals);
1481 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1482 TALLOC_CTX *mem_ctx,
1483 struct rpc_sh_ctx *ctx)
1485 static struct rpc_sh_cmd cmds[] = {
1487 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1488 N_("Show/Set a user's full name") },
1490 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1491 N_("Show/Set a user's home directory") },
1493 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1494 N_("Show/Set a user's home drive") },
1496 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1497 N_("Show/Set a user's logon script") },
1499 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1500 N_("Show/Set a user's profile path") },
1502 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1503 N_("Show/Set a user's description") },
1505 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1506 N_("Show/Set whether a user is disabled") },
1508 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1509 N_("Show/Set whether a user locked out") },
1511 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1512 N_("Show/Set whether a user does not need a password") },
1514 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1515 N_("Show/Set whether a user's password does not expire") },
1517 { NULL, NULL, 0, NULL, NULL }
1523 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1524 TALLOC_CTX *mem_ctx,
1525 struct rpc_sh_ctx *ctx)
1527 static struct rpc_sh_cmd cmds[] = {
1529 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1530 N_("List available users") },
1532 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1533 N_("List the domain groups a user is member of") },
1535 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1536 N_("Show info about a user") },
1538 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1539 N_("Show/Modify a user's fields") },
1541 { NULL, NULL, 0, NULL, NULL }
1547 /****************************************************************************/
1550 * Basic usage function for 'net rpc group'.
1551 * @param argc Standard main() style argc.
1552 * @param argv Standard main() style argv. Initial components are already
1556 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1558 return net_group_usage(c, argc, argv);
1562 * Delete group on a remote RPC server.
1564 * All parameters are provided by the run_rpc_command function, except for
1565 * argc, argv which are passed through.
1567 * @param domain_sid The domain sid acquired from the remote server.
1568 * @param cli A cli_state connected to the server.
1569 * @param mem_ctx Talloc context, destroyed on completion of the function.
1570 * @param argc Standard main() style argc.
1571 * @param argv Standard main() style argv. Initial components are already
1574 * @return Normal NTSTATUS return.
1577 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1578 const struct dom_sid *domain_sid,
1579 const char *domain_name,
1580 struct cli_state *cli,
1581 struct rpc_pipe_client *pipe_hnd,
1582 TALLOC_CTX *mem_ctx,
1586 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1587 bool group_is_primary = false;
1588 NTSTATUS status, result;
1590 struct samr_RidAttrArray *rids = NULL;
1593 /* struct samr_RidWithAttribute *user_gids; */
1594 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1596 struct samr_Ids group_rids, name_types;
1597 struct lsa_String lsa_acct_name;
1598 union samr_UserInfo *info = NULL;
1600 if (argc < 1 || c->display_usage) {
1601 rpc_group_usage(c, argc,argv);
1602 return NT_STATUS_OK; /* ok? */
1605 status = dcerpc_samr_Connect2(b, mem_ctx,
1607 MAXIMUM_ALLOWED_ACCESS,
1610 if (!NT_STATUS_IS_OK(status)) {
1611 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1615 if (!NT_STATUS_IS_OK(result)) {
1617 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1621 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1623 MAXIMUM_ALLOWED_ACCESS,
1624 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1627 if (!NT_STATUS_IS_OK(status)) {
1628 d_fprintf(stderr, _("Request open_domain failed\n"));
1632 if (!NT_STATUS_IS_OK(result)) {
1634 d_fprintf(stderr, _("Request open_domain failed\n"));
1638 init_lsa_String(&lsa_acct_name, argv[0]);
1640 status = dcerpc_samr_LookupNames(b, mem_ctx,
1647 if (!NT_STATUS_IS_OK(status)) {
1648 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1652 if (!NT_STATUS_IS_OK(result)) {
1654 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1658 switch (name_types.ids[0])
1660 case SID_NAME_DOM_GRP:
1661 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1663 MAXIMUM_ALLOWED_ACCESS,
1667 if (!NT_STATUS_IS_OK(status)) {
1668 d_fprintf(stderr, _("Request open_group failed"));
1672 if (!NT_STATUS_IS_OK(result)) {
1674 d_fprintf(stderr, _("Request open_group failed"));
1678 group_rid = group_rids.ids[0];
1680 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1684 if (!NT_STATUS_IS_OK(status)) {
1686 _("Unable to query group members of %s"),
1691 if (!NT_STATUS_IS_OK(result)) {
1694 _("Unable to query group members of %s"),
1699 if (c->opt_verbose) {
1701 _("Domain Group %s (rid: %d) has %d members\n"),
1702 argv[0],group_rid, rids->count);
1705 /* Check if group is anyone's primary group */
1706 for (i = 0; i < rids->count; i++)
1708 status = dcerpc_samr_OpenUser(b, mem_ctx,
1710 MAXIMUM_ALLOWED_ACCESS,
1714 if (!NT_STATUS_IS_OK(status)) {
1716 _("Unable to open group member %d\n"),
1721 if (!NT_STATUS_IS_OK(result)) {
1724 _("Unable to open group member %d\n"),
1729 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1734 if (!NT_STATUS_IS_OK(status)) {
1736 _("Unable to lookup userinfo for group "
1742 if (!NT_STATUS_IS_OK(result)) {
1745 _("Unable to lookup userinfo for group "
1751 if (info->info21.primary_gid == group_rid) {
1752 if (c->opt_verbose) {
1753 d_printf(_("Group is primary group "
1755 info->info21.account_name.string);
1757 group_is_primary = true;
1760 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1763 if (group_is_primary) {
1764 d_fprintf(stderr, _("Unable to delete group because "
1765 "some of it's members have it as primary "
1767 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1771 /* remove all group members */
1772 for (i = 0; i < rids->count; i++)
1775 d_printf(_("Remove group member %d..."),
1777 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1781 if (!NT_STATUS_IS_OK(status)) {
1785 if (NT_STATUS_IS_OK(result)) {
1787 d_printf(_("ok\n"));
1790 d_printf("%s\n", _("failed"));
1795 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1798 if (!NT_STATUS_IS_OK(status)) {
1805 /* removing a local group is easier... */
1806 case SID_NAME_ALIAS:
1807 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1809 MAXIMUM_ALLOWED_ACCESS,
1813 if (!NT_STATUS_IS_OK(status)) {
1814 d_fprintf(stderr, _("Request open_alias failed\n"));
1817 if (!NT_STATUS_IS_OK(result)) {
1819 d_fprintf(stderr, _("Request open_alias failed\n"));
1823 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1826 if (!NT_STATUS_IS_OK(status)) {
1834 d_fprintf(stderr, _("%s is of type %s. This command is only "
1835 "for deleting local or global groups\n"),
1836 argv[0],sid_type_lookup(name_types.ids[0]));
1837 status = NT_STATUS_UNSUCCESSFUL;
1841 if (NT_STATUS_IS_OK(status)) {
1843 d_printf(_("Deleted %s '%s'\n"),
1844 sid_type_lookup(name_types.ids[0]), argv[0]);
1846 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1847 get_friendly_nt_error_msg(status));
1855 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1857 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1858 rpc_group_delete_internals, argc,argv);
1861 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1863 NET_API_STATUS status;
1864 struct GROUP_INFO_1 info1;
1865 uint32_t parm_error = 0;
1867 if (argc != 1 || c->display_usage) {
1868 rpc_group_usage(c, argc, argv);
1874 info1.grpi1_name = argv[0];
1875 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1876 info1.grpi1_comment = c->opt_comment;
1879 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1883 _("Failed to add group '%s' with error: %s.\n"),
1884 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1888 d_printf(_("Added group '%s'.\n"), argv[0]);
1894 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1896 NET_API_STATUS status;
1897 struct LOCALGROUP_INFO_1 info1;
1898 uint32_t parm_error = 0;
1900 if (argc != 1 || c->display_usage) {
1901 rpc_group_usage(c, argc, argv);
1907 info1.lgrpi1_name = argv[0];
1908 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1909 info1.lgrpi1_comment = c->opt_comment;
1912 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1916 _("Failed to add alias '%s' with error: %s.\n"),
1917 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1921 d_printf(_("Added alias '%s'.\n"), argv[0]);
1927 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1929 if (c->opt_localgroup)
1930 return rpc_alias_add_internals(c, argc, argv);
1932 return rpc_group_add_internals(c, argc, argv);
1935 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1936 TALLOC_CTX *mem_ctx,
1938 struct dom_sid *sid,
1939 enum lsa_SidType *type)
1941 struct dom_sid *sids = NULL;
1942 enum lsa_SidType *types = NULL;
1943 struct rpc_pipe_client *pipe_hnd = NULL;
1944 struct policy_handle lsa_pol;
1945 NTSTATUS status, result;
1946 struct dcerpc_binding_handle *b;
1948 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1950 if (!NT_STATUS_IS_OK(status)) {
1954 b = pipe_hnd->binding_handle;
1956 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1957 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1959 if (!NT_STATUS_IS_OK(status)) {
1963 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1964 &name, NULL, 1, &sids, &types);
1966 if (NT_STATUS_IS_OK(status)) {
1967 sid_copy(sid, &sids[0]);
1971 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1975 TALLOC_FREE(pipe_hnd);
1978 if (!NT_STATUS_IS_OK(status) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1980 /* Try as S-1-5-whatever */
1982 struct dom_sid tmp_sid;
1984 if (string_to_sid(&tmp_sid, name)) {
1985 sid_copy(sid, &tmp_sid);
1986 *type = SID_NAME_UNKNOWN;
1987 status = NT_STATUS_OK;
1994 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1995 TALLOC_CTX *mem_ctx,
1996 const struct dom_sid *group_sid,
1999 struct policy_handle connect_pol, domain_pol;
2000 NTSTATUS status, result;
2002 struct policy_handle group_pol;
2003 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2005 struct samr_Ids rids, rid_types;
2006 struct lsa_String lsa_acct_name;
2010 sid_copy(&sid, group_sid);
2012 if (!sid_split_rid(&sid, &group_rid)) {
2013 return NT_STATUS_UNSUCCESSFUL;
2016 /* Get sam policy handle */
2017 status = dcerpc_samr_Connect2(b, mem_ctx,
2019 MAXIMUM_ALLOWED_ACCESS,
2022 if (!NT_STATUS_IS_OK(status)) {
2025 if (!NT_STATUS_IS_OK(result)) {
2029 /* Get domain policy handle */
2030 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2032 MAXIMUM_ALLOWED_ACCESS,
2036 if (!NT_STATUS_IS_OK(status)) {
2039 if (!NT_STATUS_IS_OK(result)) {
2043 init_lsa_String(&lsa_acct_name, member);
2045 status = dcerpc_samr_LookupNames(b, mem_ctx,
2052 if (!NT_STATUS_IS_OK(status)) {
2053 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2058 if (!NT_STATUS_IS_OK(result)) {
2060 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2065 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2067 MAXIMUM_ALLOWED_ACCESS,
2071 if (!NT_STATUS_IS_OK(status)) {
2075 if (!NT_STATUS_IS_OK(result)) {
2080 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2083 0x0005, /* unknown flags */
2085 if (!NT_STATUS_IS_OK(status)) {
2092 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2096 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2097 TALLOC_CTX *mem_ctx,
2098 const struct dom_sid *alias_sid,
2101 struct policy_handle connect_pol, domain_pol;
2102 NTSTATUS status, result;
2104 struct policy_handle alias_pol;
2105 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2107 struct dom_sid member_sid;
2108 enum lsa_SidType member_type;
2112 sid_copy(&sid, alias_sid);
2114 if (!sid_split_rid(&sid, &alias_rid)) {
2115 return NT_STATUS_UNSUCCESSFUL;
2118 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2119 member, &member_sid, &member_type);
2121 if (!NT_STATUS_IS_OK(result)) {
2122 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2127 /* Get sam policy handle */
2128 status = dcerpc_samr_Connect2(b, mem_ctx,
2130 MAXIMUM_ALLOWED_ACCESS,
2133 if (!NT_STATUS_IS_OK(status)) {
2136 if (!NT_STATUS_IS_OK(result)) {
2141 /* Get domain policy handle */
2142 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2144 MAXIMUM_ALLOWED_ACCESS,
2148 if (!NT_STATUS_IS_OK(status)) {
2151 if (!NT_STATUS_IS_OK(result)) {
2156 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2158 MAXIMUM_ALLOWED_ACCESS,
2162 if (!NT_STATUS_IS_OK(status)) {
2165 if (!NT_STATUS_IS_OK(result)) {
2169 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2173 if (!NT_STATUS_IS_OK(status)) {
2180 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2184 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2185 const struct dom_sid *domain_sid,
2186 const char *domain_name,
2187 struct cli_state *cli,
2188 struct rpc_pipe_client *pipe_hnd,
2189 TALLOC_CTX *mem_ctx,
2193 struct dom_sid group_sid;
2194 enum lsa_SidType group_type;
2196 if (argc != 2 || c->display_usage) {
2199 _("net rpc group addmem <group> <member>\n"
2200 " Add a member to a group\n"
2201 " group\tGroup to add member to\n"
2202 " member\tMember to add to group\n"));
2203 return NT_STATUS_UNSUCCESSFUL;
2206 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2207 &group_sid, &group_type))) {
2208 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2210 return NT_STATUS_UNSUCCESSFUL;
2213 if (group_type == SID_NAME_DOM_GRP) {
2214 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2215 &group_sid, argv[1]);
2217 if (!NT_STATUS_IS_OK(result)) {
2218 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2219 argv[1], argv[0], nt_errstr(result));
2224 if (group_type == SID_NAME_ALIAS) {
2225 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2226 &group_sid, argv[1]);
2228 if (!NT_STATUS_IS_OK(result)) {
2229 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2230 argv[1], argv[0], nt_errstr(result));
2235 d_fprintf(stderr, _("Can only add members to global or local groups "
2236 "which %s is not\n"), argv[0]);
2238 return NT_STATUS_UNSUCCESSFUL;
2241 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2243 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2244 rpc_group_addmem_internals,
2248 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2249 struct rpc_pipe_client *pipe_hnd,
2250 TALLOC_CTX *mem_ctx,
2251 const struct dom_sid *group_sid,
2254 struct policy_handle connect_pol, domain_pol;
2255 NTSTATUS status, result;
2257 struct policy_handle group_pol;
2258 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2260 struct samr_Ids rids, rid_types;
2261 struct lsa_String lsa_acct_name;
2265 sid_copy(&sid, group_sid);
2267 if (!sid_split_rid(&sid, &group_rid))
2268 return NT_STATUS_UNSUCCESSFUL;
2270 /* Get sam policy handle */
2271 status = dcerpc_samr_Connect2(b, mem_ctx,
2273 MAXIMUM_ALLOWED_ACCESS,
2276 if (!NT_STATUS_IS_OK(status)) {
2279 if (!NT_STATUS_IS_OK(result)) {
2284 /* Get domain policy handle */
2285 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2287 MAXIMUM_ALLOWED_ACCESS,
2291 if (!NT_STATUS_IS_OK(status)) {
2294 if (!NT_STATUS_IS_OK(result)) {
2298 init_lsa_String(&lsa_acct_name, member);
2300 status = dcerpc_samr_LookupNames(b, mem_ctx,
2307 if (!NT_STATUS_IS_OK(status)) {
2308 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2313 if (!NT_STATUS_IS_OK(result)) {
2315 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2320 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2322 MAXIMUM_ALLOWED_ACCESS,
2326 if (!NT_STATUS_IS_OK(status)) {
2329 if (!NT_STATUS_IS_OK(result)) {
2334 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2338 if (!NT_STATUS_IS_OK(status)) {
2344 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2348 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2349 TALLOC_CTX *mem_ctx,
2350 const struct dom_sid *alias_sid,
2353 struct policy_handle connect_pol, domain_pol;
2354 NTSTATUS status, result;
2356 struct policy_handle alias_pol;
2357 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2359 struct dom_sid member_sid;
2360 enum lsa_SidType member_type;
2364 sid_copy(&sid, alias_sid);
2366 if (!sid_split_rid(&sid, &alias_rid))
2367 return NT_STATUS_UNSUCCESSFUL;
2369 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2370 member, &member_sid, &member_type);
2372 if (!NT_STATUS_IS_OK(result)) {
2373 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2378 /* Get sam policy handle */
2379 status = dcerpc_samr_Connect2(b, mem_ctx,
2381 MAXIMUM_ALLOWED_ACCESS,
2384 if (!NT_STATUS_IS_OK(status)) {
2387 if (!NT_STATUS_IS_OK(result)) {
2392 /* Get domain policy handle */
2393 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2395 MAXIMUM_ALLOWED_ACCESS,
2399 if (!NT_STATUS_IS_OK(status)) {
2402 if (!NT_STATUS_IS_OK(result)) {
2407 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2409 MAXIMUM_ALLOWED_ACCESS,
2413 if (!NT_STATUS_IS_OK(status)) {
2417 if (!NT_STATUS_IS_OK(result)) {
2421 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2426 if (!NT_STATUS_IS_OK(status)) {
2433 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2437 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2438 const struct dom_sid *domain_sid,
2439 const char *domain_name,
2440 struct cli_state *cli,
2441 struct rpc_pipe_client *pipe_hnd,
2442 TALLOC_CTX *mem_ctx,
2446 struct dom_sid group_sid;
2447 enum lsa_SidType group_type;
2449 if (argc != 2 || c->display_usage) {
2452 _("net rpc group delmem <group> <member>\n"
2453 " Delete a member from a group\n"
2454 " group\tGroup to delete member from\n"
2455 " member\tMember to delete from group\n"));
2456 return NT_STATUS_UNSUCCESSFUL;
2459 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2460 &group_sid, &group_type))) {
2461 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2463 return NT_STATUS_UNSUCCESSFUL;
2466 if (group_type == SID_NAME_DOM_GRP) {
2467 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2468 &group_sid, argv[1]);
2470 if (!NT_STATUS_IS_OK(result)) {
2471 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2472 argv[1], argv[0], nt_errstr(result));
2477 if (group_type == SID_NAME_ALIAS) {
2478 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2479 &group_sid, argv[1]);
2481 if (!NT_STATUS_IS_OK(result)) {
2482 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2483 argv[1], argv[0], nt_errstr(result));
2488 d_fprintf(stderr, _("Can only delete members from global or local "
2489 "groups which %s is not\n"), argv[0]);
2491 return NT_STATUS_UNSUCCESSFUL;
2494 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2496 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2497 rpc_group_delmem_internals,
2502 * List groups on a remote RPC server.
2504 * All parameters are provided by the run_rpc_command function, except for
2505 * argc, argv which are passes through.
2507 * @param domain_sid The domain sid acquired from the remote server.
2508 * @param cli A cli_state connected to the server.
2509 * @param mem_ctx Talloc context, destroyed on completion of the function.
2510 * @param argc Standard main() style argc.
2511 * @param argv Standard main() style argv. Initial components are already
2514 * @return Normal NTSTATUS return.
2517 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2518 const struct dom_sid *domain_sid,
2519 const char *domain_name,
2520 struct cli_state *cli,
2521 struct rpc_pipe_client *pipe_hnd,
2522 TALLOC_CTX *mem_ctx,
2526 struct policy_handle connect_pol, domain_pol;
2527 NTSTATUS status, result;
2528 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2529 struct samr_SamArray *groups = NULL;
2530 bool global = false;
2532 bool builtin = false;
2533 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2535 if (c->display_usage) {
2538 _("net rpc group list [global] [local] [builtin]\n"
2539 " List groups on RPC server\n"
2540 " global\tList global groups\n"
2541 " local\tList local groups\n"
2542 " builtin\tList builtin groups\n"
2543 " If none of global, local or builtin is "
2544 "specified, all three options are considered "
2546 return NT_STATUS_OK;
2555 for (i=0; i<argc; i++) {
2556 if (strequal(argv[i], "global"))
2559 if (strequal(argv[i], "local"))
2562 if (strequal(argv[i], "builtin"))
2566 /* Get sam policy handle */
2568 status = dcerpc_samr_Connect2(b, mem_ctx,
2570 MAXIMUM_ALLOWED_ACCESS,
2573 if (!NT_STATUS_IS_OK(status)) {
2576 if (!NT_STATUS_IS_OK(result)) {
2581 /* Get domain policy handle */
2583 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2585 MAXIMUM_ALLOWED_ACCESS,
2586 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2589 if (!NT_STATUS_IS_OK(status)) {
2592 if (!NT_STATUS_IS_OK(result)) {
2597 /* Query domain groups */
2598 if (c->opt_long_list_entries)
2599 d_printf(_("\nGroup name Comment"
2600 "\n-----------------------------\n"));
2602 uint32_t max_size, total_size, returned_size;
2603 union samr_DispInfo info;
2607 dcerpc_get_query_dispinfo_params(
2608 loop_count, &max_entries, &max_size);
2610 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2620 if (!NT_STATUS_IS_OK(status)) {
2623 num_entries = info.info3.count;
2624 start_idx += info.info3.count;
2626 if (!NT_STATUS_IS_OK(result) &&
2627 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2630 for (i = 0; i < num_entries; i++) {
2632 const char *group = NULL;
2633 const char *desc = NULL;
2635 group = info.info3.entries[i].account_name.string;
2636 desc = info.info3.entries[i].description.string;
2638 if (c->opt_long_list_entries)
2639 printf("%-21.21s %-50.50s\n",
2642 printf("%s\n", group);
2644 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2645 /* query domain aliases */
2650 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2657 if (!NT_STATUS_IS_OK(status)) {
2660 if (!NT_STATUS_IS_OK(result) &&
2661 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2664 for (i = 0; i < num_entries; i++) {
2666 const char *description = NULL;
2668 if (c->opt_long_list_entries) {
2670 struct policy_handle alias_pol;
2671 union samr_AliasInfo *info = NULL;
2674 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2677 groups->entries[i].idx,
2680 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2681 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2686 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2687 status = dcerpc_samr_Close(b, mem_ctx,
2690 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2691 description = info->description.string;
2697 if (description != NULL) {
2698 printf("%-21.21s %-50.50s\n",
2699 groups->entries[i].name.string,
2702 printf("%s\n", groups->entries[i].name.string);
2705 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2706 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2707 /* Get builtin policy handle */
2709 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2711 MAXIMUM_ALLOWED_ACCESS,
2712 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2715 if (!NT_STATUS_IS_OK(status)) {
2718 if (!NT_STATUS_IS_OK(result)) {
2723 /* query builtin aliases */
2726 if (!builtin) break;
2728 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2735 if (!NT_STATUS_IS_OK(status)) {
2738 if (!NT_STATUS_IS_OK(result) &&
2739 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2744 for (i = 0; i < num_entries; i++) {
2746 const char *description = NULL;
2748 if (c->opt_long_list_entries) {
2750 struct policy_handle alias_pol;
2751 union samr_AliasInfo *info = NULL;
2754 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2757 groups->entries[i].idx,
2760 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2761 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2766 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2767 status = dcerpc_samr_Close(b, mem_ctx,
2770 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2771 description = info->description.string;
2777 if (description != NULL) {
2778 printf("%-21.21s %-50.50s\n",
2779 groups->entries[i].name.string,
2782 printf("%s\n", groups->entries[i].name.string);
2785 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2793 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2795 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2796 rpc_group_list_internals,
2800 static NTSTATUS rpc_list_group_members(struct net_context *c,
2801 struct rpc_pipe_client *pipe_hnd,
2802 TALLOC_CTX *mem_ctx,
2803 const char *domain_name,
2804 const struct dom_sid *domain_sid,
2805 struct policy_handle *domain_pol,
2808 NTSTATUS result, status;
2809 struct policy_handle group_pol;
2810 uint32 num_members, *group_rids;
2812 struct samr_RidAttrArray *rids = NULL;
2813 struct lsa_Strings names;
2814 struct samr_Ids types;
2815 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2818 sid_to_fstring(sid_str, domain_sid);
2820 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2822 MAXIMUM_ALLOWED_ACCESS,
2826 if (!NT_STATUS_IS_OK(status)) {
2829 if (!NT_STATUS_IS_OK(result)) {
2833 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
2837 if (!NT_STATUS_IS_OK(status)) {
2840 if (!NT_STATUS_IS_OK(result)) {
2844 num_members = rids->count;
2845 group_rids = rids->rids;
2847 while (num_members > 0) {
2848 int this_time = 512;
2850 if (num_members < this_time)
2851 this_time = num_members;
2853 status = dcerpc_samr_LookupRids(b, mem_ctx,
2860 if (!NT_STATUS_IS_OK(status)) {
2863 if (!NT_STATUS_IS_OK(result)) {
2867 /* We only have users as members, but make the output
2868 the same as the output of alias members */
2870 for (i = 0; i < this_time; i++) {
2872 if (c->opt_long_list_entries) {
2873 printf("%s-%d %s\\%s %d\n", sid_str,
2874 group_rids[i], domain_name,
2875 names.names[i].string,
2878 printf("%s\\%s\n", domain_name,
2879 names.names[i].string);
2883 num_members -= this_time;
2887 return NT_STATUS_OK;
2890 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2891 struct rpc_pipe_client *pipe_hnd,
2892 TALLOC_CTX *mem_ctx,
2893 struct policy_handle *domain_pol,
2896 NTSTATUS result, status;
2897 struct rpc_pipe_client *lsa_pipe;
2898 struct policy_handle alias_pol, lsa_pol;
2900 struct dom_sid *alias_sids;
2903 enum lsa_SidType *types;
2905 struct lsa_SidArray sid_array;
2906 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2908 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2910 MAXIMUM_ALLOWED_ACCESS,
2914 if (!NT_STATUS_IS_OK(status)) {
2917 if (!NT_STATUS_IS_OK(result)) {
2921 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
2925 if (!NT_STATUS_IS_OK(status)) {
2926 d_fprintf(stderr, _("Couldn't list alias members\n"));
2929 if (!NT_STATUS_IS_OK(result)) {
2930 d_fprintf(stderr, _("Couldn't list alias members\n"));
2934 num_members = sid_array.num_sids;
2936 if (num_members == 0) {
2937 return NT_STATUS_OK;
2940 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2941 &ndr_table_lsarpc.syntax_id,
2943 if (!NT_STATUS_IS_OK(result)) {
2944 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2945 nt_errstr(result) );
2949 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2950 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2952 if (!NT_STATUS_IS_OK(result)) {
2953 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2954 TALLOC_FREE(lsa_pipe);
2958 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2960 d_fprintf(stderr, _("Out of memory\n"));
2961 TALLOC_FREE(lsa_pipe);
2962 return NT_STATUS_NO_MEMORY;
2965 for (i=0; i<num_members; i++) {
2966 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2969 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2970 num_members, alias_sids,
2971 &domains, &names, &types);
2973 if (!NT_STATUS_IS_OK(result) &&
2974 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2975 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2976 TALLOC_FREE(lsa_pipe);
2980 for (i = 0; i < num_members; i++) {
2982 sid_to_fstring(sid_str, &alias_sids[i]);
2984 if (c->opt_long_list_entries) {
2985 printf("%s %s\\%s %d\n", sid_str,
2986 domains[i] ? domains[i] : _("*unknown*"),
2987 names[i] ? names[i] : _("*unknown*"), types[i]);
2990 printf("%s\\%s\n", domains[i], names[i]);
2992 printf("%s\n", sid_str);
2996 TALLOC_FREE(lsa_pipe);
2997 return NT_STATUS_OK;
3000 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3001 const struct dom_sid *domain_sid,
3002 const char *domain_name,
3003 struct cli_state *cli,
3004 struct rpc_pipe_client *pipe_hnd,
3005 TALLOC_CTX *mem_ctx,
3009 NTSTATUS result, status;
3010 struct policy_handle connect_pol, domain_pol;
3011 struct samr_Ids rids, rid_types;
3012 struct lsa_String lsa_acct_name;
3013 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3015 /* Get sam policy handle */
3017 status = dcerpc_samr_Connect2(b, mem_ctx,
3019 MAXIMUM_ALLOWED_ACCESS,
3022 if (!NT_STATUS_IS_OK(status)) {
3025 if (!NT_STATUS_IS_OK(result)) {
3029 /* Get domain policy handle */
3031 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3033 MAXIMUM_ALLOWED_ACCESS,
3034 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3037 if (!NT_STATUS_IS_OK(status)) {
3040 if (!NT_STATUS_IS_OK(result)) {
3044 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3046 status = dcerpc_samr_LookupNames(b, mem_ctx,
3053 if (!NT_STATUS_IS_OK(status)) {
3057 if (!NT_STATUS_IS_OK(result)) {
3059 /* Ok, did not find it in the global sam, try with builtin */
3061 struct dom_sid sid_Builtin;
3063 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3065 sid_copy(&sid_Builtin, &global_sid_Builtin);
3067 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3069 MAXIMUM_ALLOWED_ACCESS,
3073 if (!NT_STATUS_IS_OK(status)) {
3076 if (!NT_STATUS_IS_OK(result)) {
3077 d_fprintf(stderr, _("Couldn't find group %s\n"),
3082 status = dcerpc_samr_LookupNames(b, mem_ctx,
3089 if (!NT_STATUS_IS_OK(status)) {
3092 if (!NT_STATUS_IS_OK(result)) {
3093 d_fprintf(stderr, _("Couldn't find group %s\n"),
3099 if (rids.count != 1) {
3100 d_fprintf(stderr, _("Couldn't find group %s\n"),
3105 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3106 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3107 domain_sid, &domain_pol,
3111 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3112 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3116 return NT_STATUS_NO_SUCH_GROUP;
3119 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3121 if (argc != 1 || c->display_usage) {
3122 return rpc_group_usage(c, argc, argv);
3125 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3126 rpc_group_members_internals,
3130 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3132 NET_API_STATUS status;
3133 struct GROUP_INFO_0 g0;
3137 d_printf(_("Usage:\n"));
3138 d_printf("net rpc group rename group newname\n");
3142 g0.grpi0_name = argv[1];
3144 status = NetGroupSetInfo(c->opt_host,
3151 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3152 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3160 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3162 if (argc != 2 || c->display_usage) {
3163 return rpc_group_usage(c, argc, argv);
3166 return rpc_group_rename_internals(c, argc, argv);
3170 * 'net rpc group' entrypoint.
3171 * @param argc Standard main() style argc.
3172 * @param argv Standard main() style argv. Initial components are already
3176 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3178 NET_API_STATUS status;
3180 struct functable func[] = {
3185 N_("Create specified group"),
3186 N_("net rpc group add\n"
3187 " Create specified group")
3193 N_("Delete specified group"),
3194 N_("net rpc group delete\n"
3195 " Delete specified group")
3201 N_("Add member to group"),
3202 N_("net rpc group addmem\n"
3203 " Add member to group")
3209 N_("Remove member from group"),
3210 N_("net rpc group delmem\n"
3211 " Remove member from group")
3218 N_("net rpc group list\n"
3225 N_("List group members"),
3226 N_("net rpc group members\n"
3227 " List group members")
3234 N_("net rpc group rename\n"
3237 {NULL, NULL, 0, NULL, NULL}
3240 status = libnetapi_net_init(&c->netapi_ctx);
3244 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3245 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3246 if (c->opt_kerberos) {
3247 libnetapi_set_use_kerberos(c->netapi_ctx);
3251 if (c->display_usage) {
3252 d_printf(_("Usage:\n"));
3253 d_printf(_("net rpc group\n"
3254 " Alias for net rpc group list global "
3255 "local builtin\n"));
3256 net_display_usage_from_functable(func);
3260 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3261 rpc_group_list_internals,
3265 return net_run_function(c, argc, argv, "net rpc group", func);
3268 /****************************************************************************/
3270 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3272 return net_share_usage(c, argc, argv);
3276 * Add a share on a remote RPC server.
3278 * @param argc Standard main() style argc.
3279 * @param argv Standard main() style argv. Initial components are already
3282 * @return A shell status integer (0 for success).
3285 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3287 NET_API_STATUS status;
3290 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3291 uint32 num_users=0, perms=0;
3292 char *password=NULL; /* don't allow a share password */
3293 struct SHARE_INFO_2 i2;
3294 uint32_t parm_error = 0;
3296 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3297 return rpc_share_usage(c, argc, argv);
3300 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3304 path = strchr(sharename, '=');
3311 i2.shi2_netname = sharename;
3312 i2.shi2_type = type;
3313 i2.shi2_remark = c->opt_comment;
3314 i2.shi2_permissions = perms;
3315 i2.shi2_max_uses = c->opt_maxusers;
3316 i2.shi2_current_uses = num_users;
3317 i2.shi2_path = path;
3318 i2.shi2_passwd = password;
3320 status = NetShareAdd(c->opt_host,
3325 printf(_("NetShareAdd failed with: %s\n"),
3326 libnetapi_get_error_string(c->netapi_ctx, status));
3333 * Delete a share on a remote RPC server.
3335 * @param domain_sid The domain sid acquired from the remote server.
3336 * @param argc Standard main() style argc.
3337 * @param argv Standard main() style argv. Initial components are already
3340 * @return A shell status integer (0 for success).
3342 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3344 if (argc < 1 || c->display_usage) {
3345 return rpc_share_usage(c, argc, argv);
3348 return NetShareDel(c->opt_host, argv[0], 0);
3352 * Formatted print of share info
3354 * @param r pointer to SHARE_INFO_1 to format
3357 static void display_share_info_1(struct net_context *c,
3358 struct SHARE_INFO_1 *r)
3360 if (c->opt_long_list_entries) {
3361 d_printf("%-12s %-8.8s %-50s\n",
3363 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3366 d_printf("%s\n", r->shi1_netname);
3370 static WERROR get_share_info(struct net_context *c,
3371 struct rpc_pipe_client *pipe_hnd,
3372 TALLOC_CTX *mem_ctx,
3376 struct srvsvc_NetShareInfoCtr *info_ctr)
3380 union srvsvc_NetShareInfo info;
3381 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3383 /* no specific share requested, enumerate all */
3386 uint32_t preferred_len = 0xffffffff;
3387 uint32_t total_entries = 0;
3388 uint32_t resume_handle = 0;
3390 info_ctr->level = level;
3392 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3399 if (!NT_STATUS_IS_OK(status)) {
3400 return ntstatus_to_werror(status);
3405 /* request just one share */
3406 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3413 if (!NT_STATUS_IS_OK(status)) {
3414 result = ntstatus_to_werror(status);
3418 if (!W_ERROR_IS_OK(result)) {
3423 ZERO_STRUCTP(info_ctr);
3425 info_ctr->level = level;
3430 struct srvsvc_NetShareCtr1 *ctr1;
3432 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3433 W_ERROR_HAVE_NO_MEMORY(ctr1);
3436 ctr1->array = info.info1;
3438 info_ctr->ctr.ctr1 = ctr1;
3444 struct srvsvc_NetShareCtr2 *ctr2;
3446 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3447 W_ERROR_HAVE_NO_MEMORY(ctr2);
3450 ctr2->array = info.info2;
3452 info_ctr->ctr.ctr2 = ctr2;
3458 struct srvsvc_NetShareCtr502 *ctr502;
3460 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3461 W_ERROR_HAVE_NO_MEMORY(ctr502);
3464 ctr502->array = info.info502;
3466 info_ctr->ctr.ctr502 = ctr502;
3476 * 'net rpc share list' entrypoint.
3477 * @param argc Standard main() style argc.
3478 * @param argv Standard main() style argv. Initial components are already
3481 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3483 NET_API_STATUS status;
3484 struct SHARE_INFO_1 *i1 = NULL;
3485 uint32_t entries_read = 0;
3486 uint32_t total_entries = 0;
3487 uint32_t resume_handle = 0;
3488 uint32_t i, level = 1;
3490 if (c->display_usage) {
3492 "net rpc share list\n"
3495 _("List shares on remote server"));
3499 status = NetShareEnum(c->opt_host,
3501 (uint8_t **)(void *)&i1,
3510 /* Display results */
3512 if (c->opt_long_list_entries) {
3514 "\nEnumerating shared resources (exports) on remote server:\n\n"
3515 "\nShare name Type Description\n"
3516 "---------- ---- -----------\n"));
3518 for (i = 0; i < entries_read; i++)
3519 display_share_info_1(c, &i1[i]);
3524 static bool check_share_availability(struct cli_state *cli, const char *netname)
3528 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3529 if (!NT_STATUS_IS_OK(status)) {
3530 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3534 status = cli_tdis(cli);
3535 if (!NT_STATUS_IS_OK(status)) {
3536 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3543 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3544 const char *netname, uint32 type)
3546 /* only support disk shares */
3547 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3548 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3553 /* skip builtin shares */
3554 /* FIXME: should print$ be added too ? */
3555 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3556 strequal(netname,"global"))
3559 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3560 printf(_("excluding [%s]\n"), netname);
3564 return check_share_availability(cli, netname);
3568 * Migrate shares from a remote RPC server to the local RPC server.
3570 * All parameters are provided by the run_rpc_command function, except for
3571 * argc, argv which are passed through.
3573 * @param domain_sid The domain sid acquired from the remote server.
3574 * @param cli A cli_state connected to the server.
3575 * @param mem_ctx Talloc context, destroyed on completion of the function.
3576 * @param argc Standard main() style argc.
3577 * @param argv Standard main() style argv. Initial components are already
3580 * @return Normal NTSTATUS return.
3583 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3584 const struct dom_sid *domain_sid,
3585 const char *domain_name,
3586 struct cli_state *cli,
3587 struct rpc_pipe_client *pipe_hnd,
3588 TALLOC_CTX *mem_ctx,
3593 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3594 struct srvsvc_NetShareInfoCtr ctr_src;
3596 struct rpc_pipe_client *srvsvc_pipe = NULL;
3597 struct cli_state *cli_dst = NULL;
3598 uint32 level = 502; /* includes secdesc */
3599 uint32_t parm_error = 0;
3600 struct dcerpc_binding_handle *b;
3602 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3604 if (!W_ERROR_IS_OK(result))
3607 /* connect destination PI_SRVSVC */
3608 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3609 &ndr_table_srvsvc.syntax_id);
3610 if (!NT_STATUS_IS_OK(nt_status))
3613 b = srvsvc_pipe->binding_handle;
3615 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3617 union srvsvc_NetShareInfo info;
3618 struct srvsvc_NetShareInfo502 info502 =
3619 ctr_src.ctr.ctr502->array[i];
3621 /* reset error-code */
3622 nt_status = NT_STATUS_UNSUCCESSFUL;
3624 if (!check_share_sanity(c, cli, info502.name, info502.type))
3627 /* finally add the share on the dst server */
3629 printf(_("migrating: [%s], path: %s, comment: %s, without "
3631 info502.name, info502.path, info502.comment);
3633 info.info502 = &info502;
3635 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3636 srvsvc_pipe->desthost,
3641 if (!NT_STATUS_IS_OK(nt_status)) {
3642 printf(_("cannot add share: %s\n"),
3643 nt_errstr(nt_status));
3646 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3647 printf(_(" [%s] does already exist\n"),
3652 if (!W_ERROR_IS_OK(result)) {
3653 nt_status = werror_to_ntstatus(result);
3654 printf(_("cannot add share: %s\n"),
3655 win_errstr(result));
3661 nt_status = NT_STATUS_OK;
3665 cli_shutdown(cli_dst);
3673 * Migrate shares from a RPC server to another.
3675 * @param argc Standard main() style argc.
3676 * @param argv Standard main() style argv. Initial components are already
3679 * @return A shell status integer (0 for success).
3681 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3684 if (c->display_usage) {
3686 "net rpc share migrate shares\n"
3689 _("Migrate shares to local server"));
3694 printf(_("no server to migrate\n"));
3698 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3699 rpc_share_migrate_shares_internals,
3706 * @param f file_info
3707 * @param mask current search mask
3708 * @param state arg-pointer
3711 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3712 const char *mask, void *state)
3714 static NTSTATUS nt_status;
3715 static struct copy_clistate *local_state;
3716 static fstring filename, new_mask;
3719 struct net_context *c;
3721 local_state = (struct copy_clistate *)state;
3722 nt_status = NT_STATUS_UNSUCCESSFUL;
3726 if (strequal(f->name, ".") || strequal(f->name, ".."))
3727 return NT_STATUS_OK;
3729 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3732 if (f->mode & aDIR) {
3734 DEBUG(3,("got dir: %s\n", f->name));
3736 fstrcpy(dir, local_state->cwd);
3738 fstrcat(dir, f->name);
3740 switch (net_mode_share)
3742 case NET_MODE_SHARE_MIGRATE:
3743 /* create that directory */
3744 nt_status = net_copy_file(c, local_state->mem_ctx,
3745 local_state->cli_share_src,
3746 local_state->cli_share_dst,
3748 c->opt_acls? true : false,
3749 c->opt_attrs? true : false,
3750 c->opt_timestamps? true:false,
3754 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3755 return NT_STATUS_INTERNAL_ERROR;
3758 if (!NT_STATUS_IS_OK(nt_status)) {
3759 printf(_("could not handle dir %s: %s\n"),
3760 dir, nt_errstr(nt_status));
3764 /* search below that directory */
3765 fstrcpy(new_mask, dir);
3766 fstrcat(new_mask, "\\*");
3768 old_dir = local_state->cwd;
3769 local_state->cwd = dir;
3770 nt_status = sync_files(local_state, new_mask);
3771 if (!NT_STATUS_IS_OK(nt_status)) {
3772 printf(_("could not handle files\n"));
3774 local_state->cwd = old_dir;
3781 fstrcpy(filename, local_state->cwd);
3782 fstrcat(filename, "\\");
3783 fstrcat(filename, f->name);
3785 DEBUG(3,("got file: %s\n", filename));
3787 switch (net_mode_share)
3789 case NET_MODE_SHARE_MIGRATE:
3790 nt_status = net_copy_file(c, local_state->mem_ctx,
3791 local_state->cli_share_src,
3792 local_state->cli_share_dst,
3794 c->opt_acls? true : false,
3795 c->opt_attrs? true : false,
3796 c->opt_timestamps? true: false,
3800 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3802 return NT_STATUS_INTERNAL_ERROR;
3805 if (!NT_STATUS_IS_OK(nt_status))
3806 printf(_("could not handle file %s: %s\n"),
3807 filename, nt_errstr(nt_status));
3812 * sync files, can be called recursivly to list files
3813 * and then call copy_fn for each file
3815 * @param cp_clistate pointer to the copy_clistate we work with
3816 * @param mask the current search mask
3818 * @return Boolean result
3820 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3822 struct cli_state *targetcli;
3823 char *targetpath = NULL;
3826 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3828 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3829 mask, &targetcli, &targetpath ) ) {
3830 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3832 mask, cli_errstr(cp_clistate->cli_share_src));
3833 return cli_nt_error(cp_clistate->cli_share_src);
3836 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3837 copy_fn, cp_clistate);
3838 if (!NT_STATUS_IS_OK(status)) {
3839 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3840 mask, nt_errstr(status));
3848 * Set the top level directory permissions before we do any further copies.
3849 * Should set up ACL inheritance.
3852 bool copy_top_level_perms(struct net_context *c,
3853 struct copy_clistate *cp_clistate,
3854 const char *sharename)
3856 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3858 switch (net_mode_share) {
3859 case NET_MODE_SHARE_MIGRATE:
3860 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3861 nt_status = net_copy_fileattr(c,
3862 cp_clistate->mem_ctx,
3863 cp_clistate->cli_share_src,
3864 cp_clistate->cli_share_dst,
3866 c->opt_acls? true : false,
3867 c->opt_attrs? true : false,
3868 c->opt_timestamps? true: false,
3872 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3876 if (!NT_STATUS_IS_OK(nt_status)) {
3877 printf(_("Could handle directory attributes for top level "
3878 "directory of share %s. Error %s\n"),
3879 sharename, nt_errstr(nt_status));
3887 * Sync all files inside a remote share to another share (over smb).
3889 * All parameters are provided by the run_rpc_command function, except for
3890 * argc, argv which are passed through.
3892 * @param domain_sid The domain sid acquired from the remote server.
3893 * @param cli A cli_state connected to the server.
3894 * @param mem_ctx Talloc context, destroyed on completion of the function.
3895 * @param argc Standard main() style argc.
3896 * @param argv Standard main() style argv. Initial components are already
3899 * @return Normal NTSTATUS return.
3902 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3903 const struct dom_sid *domain_sid,
3904 const char *domain_name,
3905 struct cli_state *cli,
3906 struct rpc_pipe_client *pipe_hnd,
3907 TALLOC_CTX *mem_ctx,
3912 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3913 struct srvsvc_NetShareInfoCtr ctr_src;
3916 struct copy_clistate cp_clistate;
3917 bool got_src_share = false;
3918 bool got_dst_share = false;
3919 const char *mask = "\\*";
3922 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3924 nt_status = NT_STATUS_NO_MEMORY;
3928 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3931 if (!W_ERROR_IS_OK(result))
3934 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3936 struct srvsvc_NetShareInfo502 info502 =
3937 ctr_src.ctr.ctr502->array[i];
3939 if (!check_share_sanity(c, cli, info502.name, info502.type))
3942 /* one might not want to mirror whole discs :) */
3943 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3944 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3949 switch (net_mode_share)
3951 case NET_MODE_SHARE_MIGRATE:
3955 d_fprintf(stderr, _("Unsupported mode %d\n"),
3959 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3962 c->opt_acls ? _("including") : _("without"),
3963 c->opt_attrs ? _("including") : _("without"),
3964 c->opt_timestamps ? _("(preserving timestamps)") : "");
3966 cp_clistate.mem_ctx = mem_ctx;
3967 cp_clistate.cli_share_src = NULL;
3968 cp_clistate.cli_share_dst = NULL;
3969 cp_clistate.cwd = NULL;
3970 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3973 /* open share source */
3974 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3975 &cli->dest_ss, cli->desthost,
3976 info502.name, "A:");
3977 if (!NT_STATUS_IS_OK(nt_status))
3980 got_src_share = true;
3982 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3983 /* open share destination */
3984 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3985 NULL, dst, info502.name, "A:");
3986 if (!NT_STATUS_IS_OK(nt_status))
3989 got_dst_share = true;
3992 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3993 d_fprintf(stderr, _("Could not handle the top level "
3994 "directory permissions for the "
3995 "share: %s\n"), info502.name);
3996 nt_status = NT_STATUS_UNSUCCESSFUL;
4000 nt_status = sync_files(&cp_clistate, mask);
4001 if (!NT_STATUS_IS_OK(nt_status)) {
4002 d_fprintf(stderr, _("could not handle files for share: "
4003 "%s\n"), info502.name);
4008 nt_status = NT_STATUS_OK;
4013 cli_shutdown(cp_clistate.cli_share_src);
4016 cli_shutdown(cp_clistate.cli_share_dst);
4023 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4025 if (c->display_usage) {
4027 "net share migrate files\n"
4030 _("Migrate files to local server"));
4035 d_printf(_("no server to migrate\n"));
4039 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4040 rpc_share_migrate_files_internals,
4045 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4047 * All parameters are provided by the run_rpc_command function, except for
4048 * argc, argv which are passed through.
4050 * @param domain_sid The domain sid acquired from the remote server.
4051 * @param cli A cli_state connected to the server.
4052 * @param mem_ctx Talloc context, destroyed on completion of the function.
4053 * @param argc Standard main() style argc.
4054 * @param argv Standard main() style argv. Initial components are already
4057 * @return Normal NTSTATUS return.
4060 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4061 const struct dom_sid *domain_sid,
4062 const char *domain_name,
4063 struct cli_state *cli,
4064 struct rpc_pipe_client *pipe_hnd,
4065 TALLOC_CTX *mem_ctx,
4070 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4071 struct srvsvc_NetShareInfoCtr ctr_src;
4072 union srvsvc_NetShareInfo info;
4074 struct rpc_pipe_client *srvsvc_pipe = NULL;
4075 struct cli_state *cli_dst = NULL;
4076 uint32 level = 502; /* includes secdesc */
4077 uint32_t parm_error = 0;
4078 struct dcerpc_binding_handle *b;
4080 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4083 if (!W_ERROR_IS_OK(result))
4086 /* connect destination PI_SRVSVC */
4087 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4088 &ndr_table_srvsvc.syntax_id);
4089 if (!NT_STATUS_IS_OK(nt_status))
4092 b = srvsvc_pipe->binding_handle;
4094 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4096 struct srvsvc_NetShareInfo502 info502 =
4097 ctr_src.ctr.ctr502->array[i];
4099 /* reset error-code */
4100 nt_status = NT_STATUS_UNSUCCESSFUL;
4102 if (!check_share_sanity(c, cli, info502.name, info502.type))
4105 printf(_("migrating: [%s], path: %s, comment: %s, including "
4107 info502.name, info502.path, info502.comment);
4110 display_sec_desc(info502.sd_buf.sd);
4112 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4113 info.info502 = &info502;
4115 /* finally modify the share on the dst server */
4116 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4117 srvsvc_pipe->desthost,
4123 if (!NT_STATUS_IS_OK(nt_status)) {
4124 printf(_("cannot set share-acl: %s\n"),
4125 nt_errstr(nt_status));
4128 if (!W_ERROR_IS_OK(result)) {
4129 nt_status = werror_to_ntstatus(result);
4130 printf(_("cannot set share-acl: %s\n"),
4131 win_errstr(result));
4137 nt_status = NT_STATUS_OK;
4141 cli_shutdown(cli_dst);
4149 * Migrate share-acls from a RPC server to another.
4151 * @param argc Standard main() style argc.
4152 * @param argv Standard main() style argv. Initial components are already
4155 * @return A shell status integer (0 for success).
4157 static int rpc_share_migrate_security(struct net_context *c, int argc,
4160 if (c->display_usage) {
4162 "net rpc share migrate security\n"
4165 _("Migrate share-acls to local server"));
4170 d_printf(_("no server to migrate\n"));
4174 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4175 rpc_share_migrate_security_internals,
4180 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4181 * from one server to another.
4183 * @param argc Standard main() style argc.
4184 * @param argv Standard main() style argv. Initial components are already
4187 * @return A shell status integer (0 for success).
4190 static int rpc_share_migrate_all(struct net_context *c, int argc,
4195 if (c->display_usage) {
4197 "net rpc share migrate all\n"
4200 _("Migrates shares including all share settings"));
4205 d_printf(_("no server to migrate\n"));
4209 /* order is important. we don't want to be locked out by the share-acl
4210 * before copying files - gd */
4212 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4213 rpc_share_migrate_shares_internals, argc, argv);
4217 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4218 rpc_share_migrate_files_internals, argc, argv);
4222 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4223 rpc_share_migrate_security_internals, argc,
4229 * 'net rpc share migrate' entrypoint.
4230 * @param argc Standard main() style argc.
4231 * @param argv Standard main() style argv. Initial components are already
4234 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4237 struct functable func[] = {
4240 rpc_share_migrate_all,
4242 N_("Migrate shares from remote to local server"),
4243 N_("net rpc share migrate all\n"
4244 " Migrate shares from remote to local server")
4248 rpc_share_migrate_files,
4250 N_("Migrate files from remote to local server"),
4251 N_("net rpc share migrate files\n"
4252 " Migrate files from remote to local server")
4256 rpc_share_migrate_security,
4258 N_("Migrate share-ACLs from remote to local server"),
4259 N_("net rpc share migrate security\n"
4260 " Migrate share-ACLs from remote to local server")
4264 rpc_share_migrate_shares,
4266 N_("Migrate shares from remote to local server"),
4267 N_("net rpc share migrate shares\n"
4268 " Migrate shares from remote to local server")
4270 {NULL, NULL, 0, NULL, NULL}
4273 net_mode_share = NET_MODE_SHARE_MIGRATE;
4275 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4281 struct dom_sid *members;
4284 static int num_server_aliases;
4285 static struct full_alias *server_aliases;
4288 * Add an alias to the static list.
4290 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4292 if (server_aliases == NULL)
4293 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4295 server_aliases[num_server_aliases] = *alias;
4296 num_server_aliases += 1;
4300 * For a specific domain on the server, fetch all the aliases
4301 * and their members. Add all of them to the server_aliases.
4304 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4305 TALLOC_CTX *mem_ctx,
4306 struct policy_handle *connect_pol,
4307 const struct dom_sid *domain_sid)
4309 uint32 start_idx, max_entries, num_entries, i;
4310 struct samr_SamArray *groups = NULL;
4311 NTSTATUS result, status;
4312 struct policy_handle domain_pol;
4313 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4315 /* Get domain policy handle */
4317 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4319 MAXIMUM_ALLOWED_ACCESS,
4320 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4323 if (!NT_STATUS_IS_OK(status)) {
4326 if (!NT_STATUS_IS_OK(result)) {
4334 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4341 if (!NT_STATUS_IS_OK(status)) {
4344 for (i = 0; i < num_entries; i++) {
4346 struct policy_handle alias_pol;
4347 struct full_alias alias;
4348 struct lsa_SidArray sid_array;
4352 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4354 MAXIMUM_ALLOWED_ACCESS,
4355 groups->entries[i].idx,
4358 if (!NT_STATUS_IS_OK(status)) {
4361 if (!NT_STATUS_IS_OK(_result)) {
4366 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4370 if (!NT_STATUS_IS_OK(status)) {
4373 if (!NT_STATUS_IS_OK(_result)) {
4378 alias.num_members = sid_array.num_sids;
4380 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4381 if (!NT_STATUS_IS_OK(status)) {
4384 if (!NT_STATUS_IS_OK(_result)) {
4389 alias.members = NULL;
4391 if (alias.num_members > 0) {
4392 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4394 for (j = 0; j < alias.num_members; j++)
4395 sid_copy(&alias.members[j],
4396 sid_array.sids[j].sid);
4399 sid_compose(&alias.sid, domain_sid,
4400 groups->entries[i].idx);
4402 push_alias(mem_ctx, &alias);
4404 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4406 status = NT_STATUS_OK;
4409 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4415 * Dump server_aliases as names for debugging purposes.
4418 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4419 const struct dom_sid *domain_sid,
4420 const char *domain_name,
4421 struct cli_state *cli,
4422 struct rpc_pipe_client *pipe_hnd,
4423 TALLOC_CTX *mem_ctx,
4429 struct policy_handle lsa_pol;
4430 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4432 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4433 SEC_FLAG_MAXIMUM_ALLOWED,
4435 if (!NT_STATUS_IS_OK(result))
4438 for (i=0; i<num_server_aliases; i++) {
4441 enum lsa_SidType *types;
4444 struct full_alias *alias = &server_aliases[i];
4446 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4448 &domains, &names, &types);
4449 if (!NT_STATUS_IS_OK(result))
4452 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4454 if (alias->num_members == 0) {
4459 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4462 &domains, &names, &types);
4464 if (!NT_STATUS_IS_OK(result) &&
4465 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4468 for (j=0; j<alias->num_members; j++)
4469 DEBUG(1, ("%s\\%s (%d); ",
4470 domains[j] ? domains[j] : "*unknown*",
4471 names[j] ? names[j] : "*unknown*",types[j]));
4475 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4477 return NT_STATUS_OK;
4481 * Fetch a list of all server aliases and their members into
4485 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4486 const struct dom_sid *domain_sid,
4487 const char *domain_name,
4488 struct cli_state *cli,
4489 struct rpc_pipe_client *pipe_hnd,
4490 TALLOC_CTX *mem_ctx,
4494 NTSTATUS result, status;
4495 struct policy_handle connect_pol;
4496 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4498 status = dcerpc_samr_Connect2(b, mem_ctx,
4500 MAXIMUM_ALLOWED_ACCESS,
4503 if (!NT_STATUS_IS_OK(status)) {
4506 if (!NT_STATUS_IS_OK(result)) {
4511 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4512 &global_sid_Builtin);
4513 if (!NT_STATUS_IS_OK(status)) {
4517 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4520 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4525 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4527 token->num_sids = 4;
4529 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4530 d_fprintf(stderr, "malloc %s\n",_("failed"));
4531 token->num_sids = 0;
4535 token->sids[0] = *user_sid;
4536 sid_copy(&token->sids[1], &global_sid_World);
4537 sid_copy(&token->sids[2], &global_sid_Network);
4538 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4541 static void free_user_token(struct security_token *token)
4543 SAFE_FREE(token->sids);
4546 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4548 if (security_token_has_sid(token, sid))
4551 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4556 sid_copy(&token->sids[token->num_sids], sid);
4558 token->num_sids += 1;
4563 struct security_token token;
4566 static void dump_user_token(struct user_token *token)
4570 d_printf("%s\n", token->name);
4572 for (i=0; i<token->token.num_sids; i++) {
4573 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4577 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4581 for (i=0; i<alias->num_members; i++) {
4582 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4589 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4593 for (i=0; i<num_server_aliases; i++) {
4594 if (is_alias_member(&sid, &server_aliases[i]))
4595 add_sid_to_token(token, &server_aliases[i].sid);
4600 * We got a user token with all the SIDs we can know about without asking the
4601 * server directly. These are the user and domain group sids. All of these can
4602 * be members of aliases. So scan the list of aliases for each of the SIDs and
4603 * add them to the token.
4606 static void collect_alias_memberships(struct security_token *token)
4608 int num_global_sids = token->num_sids;
4611 for (i=0; i<num_global_sids; i++) {
4612 collect_sid_memberships(token, token->sids[i]);
4616 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4618 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4619 enum wbcSidType type;
4621 struct wbcDomainSid wsid;
4622 char sid_str[WBC_SID_STRING_BUFLEN];
4623 struct dom_sid user_sid;
4624 uint32_t num_groups;
4625 gid_t *groups = NULL;
4628 fstr_sprintf(full_name, "%s%c%s",
4629 domain, *lp_winbind_separator(), user);
4631 /* First let's find out the user sid */
4633 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4635 if (!WBC_ERROR_IS_OK(wbc_status)) {
4636 DEBUG(1, ("winbind could not find %s: %s\n",
4637 full_name, wbcErrorString(wbc_status)));
4641 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4643 if (type != WBC_SID_NAME_USER) {
4644 DEBUG(1, ("%s is not a user\n", full_name));
4648 if (!string_to_sid(&user_sid, sid_str)) {
4649 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4653 init_user_token(token, &user_sid);
4655 /* And now the groups winbind knows about */
4657 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4658 if (!WBC_ERROR_IS_OK(wbc_status)) {
4659 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4660 full_name, wbcErrorString(wbc_status)));
4664 for (i = 0; i < num_groups; i++) {
4665 gid_t gid = groups[i];
4668 wbc_status = wbcGidToSid(gid, &wsid);
4669 if (!WBC_ERROR_IS_OK(wbc_status)) {
4670 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4671 (unsigned int)gid, wbcErrorString(wbc_status)));
4672 wbcFreeMemory(groups);
4676 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4678 DEBUG(3, (" %s\n", sid_str));
4680 string_to_sid(&sid, sid_str);
4681 add_sid_to_token(token, &sid);
4683 wbcFreeMemory(groups);
4689 * Get a list of all user tokens we want to look at
4692 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4693 struct user_token **user_tokens)
4695 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4696 uint32_t i, num_users;
4698 struct user_token *result;
4699 TALLOC_CTX *frame = NULL;
4701 if (lp_winbind_use_default_domain() &&
4702 (c->opt_target_workgroup == NULL)) {
4703 d_fprintf(stderr, _("winbind use default domain = yes set, "
4704 "please specify a workgroup\n"));
4708 /* Send request to winbind daemon */
4710 wbc_status = wbcListUsers(NULL, &num_users, &users);
4711 if (!WBC_ERROR_IS_OK(wbc_status)) {
4712 DEBUG(1, (_("winbind could not list users: %s\n"),
4713 wbcErrorString(wbc_status)));
4717 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4719 if (result == NULL) {
4720 DEBUG(1, ("Could not malloc sid array\n"));
4721 wbcFreeMemory(users);
4725 frame = talloc_stackframe();
4726 for (i=0; i < num_users; i++) {
4727 fstring domain, user;
4730 fstrcpy(result[i].name, users[i]);
4732 p = strchr(users[i], *lp_winbind_separator());
4734 DEBUG(3, ("%s\n", users[i]));
4737 fstrcpy(domain, c->opt_target_workgroup);
4738 fstrcpy(user, users[i]);
4741 fstrcpy(domain, users[i]);
4746 get_user_sids(domain, user, &(result[i].token));
4749 wbcFreeMemory(users);
4751 *num_tokens = num_users;
4752 *user_tokens = result;
4757 static bool get_user_tokens_from_file(FILE *f,
4759 struct user_token **tokens)
4761 struct user_token *token = NULL;
4766 if (fgets(line, sizeof(line)-1, f) == NULL) {
4770 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4771 line[strlen(line)-1] = '\0';
4774 if (line[0] == ' ') {
4778 if(!string_to_sid(&sid, &line[1])) {
4779 DEBUG(1,("get_user_tokens_from_file: Could "
4780 "not convert sid %s \n",&line[1]));
4784 if (token == NULL) {
4785 DEBUG(0, ("File does not begin with username"));
4789 add_sid_to_token(&token->token, &sid);
4793 /* And a new user... */
4796 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4797 if (*tokens == NULL) {
4798 DEBUG(0, ("Could not realloc tokens\n"));
4802 token = &((*tokens)[*num_tokens-1]);
4804 fstrcpy(token->name, line);
4805 token->token.num_sids = 0;
4806 token->token.sids = NULL;
4815 * Show the list of all users that have access to a share
4818 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4819 TALLOC_CTX *mem_ctx,
4820 const char *netname,
4822 struct user_token *tokens)
4825 struct security_descriptor *share_sd = NULL;
4826 struct security_descriptor *root_sd = NULL;
4827 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4829 union srvsvc_NetShareInfo info;
4833 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4835 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4842 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4843 DEBUG(1, ("Coult not query secdesc for share %s\n",
4848 share_sd = info.info502->sd_buf.sd;
4849 if (share_sd == NULL) {
4850 DEBUG(1, ("Got no secdesc for share %s\n",
4856 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4860 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4861 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4862 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4865 for (i=0; i<num_tokens; i++) {
4868 if (share_sd != NULL) {
4869 status = se_access_check(share_sd, &tokens[i].token,
4872 if (!NT_STATUS_IS_OK(status)) {
4873 DEBUG(1, ("Could not check share_sd for "
4880 if (root_sd == NULL) {
4881 d_printf(" %s\n", tokens[i].name);
4885 status = se_access_check(root_sd, &tokens[i].token,
4887 if (!NT_STATUS_IS_OK(status)) {
4888 DEBUG(1, ("Could not check root_sd for user %s\n",
4892 d_printf(" %s\n", tokens[i].name);
4895 if (fnum != (uint16_t)-1)
4896 cli_close(cli, fnum);
4908 static void collect_share(const char *name, uint32 m,
4909 const char *comment, void *state)
4911 struct share_list *share_list = (struct share_list *)state;
4913 if (m != STYPE_DISKTREE)
4916 share_list->num_shares += 1;
4917 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4918 if (!share_list->shares) {
4919 share_list->num_shares = 0;
4922 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4926 * List shares on a remote RPC server, including the security descriptors.
4928 * All parameters are provided by the run_rpc_command function, except for
4929 * argc, argv which are passed through.
4931 * @param domain_sid The domain sid acquired from the remote server.
4932 * @param cli A cli_state connected to the server.
4933 * @param mem_ctx Talloc context, destroyed on completion of the function.
4934 * @param argc Standard main() style argc.
4935 * @param argv Standard main() style argv. Initial components are already
4938 * @return Normal NTSTATUS return.
4941 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4942 const struct dom_sid *domain_sid,
4943 const char *domain_name,
4944 struct cli_state *cli,
4945 struct rpc_pipe_client *pipe_hnd,
4946 TALLOC_CTX *mem_ctx,
4955 struct user_token *tokens = NULL;
4958 struct share_list share_list;
4963 f = fopen(argv[0], "r");
4967 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4968 return NT_STATUS_UNSUCCESSFUL;
4971 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4977 DEBUG(0, ("Could not read users from file\n"));
4978 return NT_STATUS_UNSUCCESSFUL;
4981 for (i=0; i<num_tokens; i++)
4982 collect_alias_memberships(&tokens[i].token);
4984 share_list.num_shares = 0;
4985 share_list.shares = NULL;
4987 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4990 DEBUG(0, ("Error returning browse list: %s\n",
4995 for (i = 0; i < share_list.num_shares; i++) {
4996 char *netname = share_list.shares[i];
4998 if (netname[strlen(netname)-1] == '$')
5001 d_printf("%s\n", netname);
5003 show_userlist(pipe_hnd, mem_ctx, netname,
5004 num_tokens, tokens);
5007 for (i=0; i<num_tokens; i++) {
5008 free_user_token(&tokens[i].token);
5011 SAFE_FREE(share_list.shares);
5013 return NT_STATUS_OK;
5016 static int rpc_share_allowedusers(struct net_context *c, int argc,
5021 if (c->display_usage) {
5023 "net rpc share allowedusers\n"
5026 _("List allowed users"));
5030 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5031 rpc_aliaslist_internals,
5036 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
5042 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5043 rpc_share_allowedusers_internals,
5047 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5050 struct user_token *tokens = NULL;
5054 net_usersidlist_usage(c, argc, argv);
5058 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5059 DEBUG(0, ("Could not get the user/sid list\n"));
5063 for (i=0; i<num_tokens; i++) {
5064 dump_user_token(&tokens[i]);
5065 free_user_token(&tokens[i].token);
5072 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5074 d_printf(_("net usersidlist\n"
5075 "\tprints out a list of all users the running winbind knows\n"
5076 "\tabout, together with all their SIDs. This is used as\n"
5077 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5079 net_common_flags_usage(c, argc, argv);
5084 * 'net rpc share' entrypoint.
5085 * @param argc Standard main() style argc.
5086 * @param argv Standard main() style argv. Initial components are already
5090 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5092 NET_API_STATUS status;
5094 struct functable func[] = {
5100 N_("net rpc share add\n"
5108 N_("net rpc share delete\n"
5113 rpc_share_allowedusers,
5115 N_("Modify allowed users"),
5116 N_("net rpc share allowedusers\n"
5117 " Modify allowed users")
5123 N_("Migrate share to local server"),
5124 N_("net rpc share migrate\n"
5125 " Migrate share to local server")
5132 N_("net rpc share list\n"
5135 {NULL, NULL, 0, NULL, NULL}
5138 status = libnetapi_net_init(&c->netapi_ctx);
5142 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5143 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5144 if (c->opt_kerberos) {
5145 libnetapi_set_use_kerberos(c->netapi_ctx);
5149 if (c->display_usage) {
5154 " Alias for net rpc share list\n"));
5155 net_display_usage_from_functable(func);
5159 return rpc_share_list(c, argc, argv);
5162 return net_run_function(c, argc, argv, "net rpc share", func);
5165 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5166 TALLOC_CTX *mem_ctx,
5167 struct rpc_sh_ctx *ctx,
5168 struct rpc_pipe_client *pipe_hnd,
5169 int argc, const char **argv)
5172 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5175 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5176 TALLOC_CTX *mem_ctx,
5177 struct rpc_sh_ctx *ctx,
5178 struct rpc_pipe_client *pipe_hnd,
5179 int argc, const char **argv)
5181 NET_API_STATUS status;
5182 uint32_t parm_err = 0;
5183 struct SHARE_INFO_2 i2;
5185 if ((argc < 2) || (argc > 3)) {
5186 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5188 return NT_STATUS_INVALID_PARAMETER;
5191 i2.shi2_netname = argv[0];
5192 i2.shi2_type = STYPE_DISKTREE;
5193 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5194 i2.shi2_permissions = 0;
5195 i2.shi2_max_uses = 0;
5196 i2.shi2_current_uses = 0;
5197 i2.shi2_path = argv[1];
5198 i2.shi2_passwd = NULL;
5200 status = NetShareAdd(pipe_hnd->desthost,
5205 return werror_to_ntstatus(W_ERROR(status));
5208 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5209 TALLOC_CTX *mem_ctx,
5210 struct rpc_sh_ctx *ctx,
5211 struct rpc_pipe_client *pipe_hnd,
5212 int argc, const char **argv)
5215 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5216 return NT_STATUS_INVALID_PARAMETER;
5219 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5222 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5223 TALLOC_CTX *mem_ctx,
5224 struct rpc_sh_ctx *ctx,
5225 struct rpc_pipe_client *pipe_hnd,
5226 int argc, const char **argv)
5228 union srvsvc_NetShareInfo info;
5231 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5234 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5235 return NT_STATUS_INVALID_PARAMETER;
5238 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5244 if (!NT_STATUS_IS_OK(status)) {
5245 result = ntstatus_to_werror(status);
5248 if (!W_ERROR_IS_OK(result)) {
5252 d_printf(_("Name: %s\n"), info.info2->name);
5253 d_printf(_("Comment: %s\n"), info.info2->comment);
5254 d_printf(_("Path: %s\n"), info.info2->path);
5255 d_printf(_("Password: %s\n"), info.info2->password);
5258 return werror_to_ntstatus(result);
5261 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5262 struct rpc_sh_ctx *ctx)
5264 static struct rpc_sh_cmd cmds[] = {
5266 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
5267 N_("List available shares") },
5269 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
5270 N_("Add a share") },
5272 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
5273 N_("Delete a share") },
5275 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
5276 N_("Get information about a share") },
5278 { NULL, NULL, 0, NULL, NULL }
5284 /****************************************************************************/
5286 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5288 return net_file_usage(c, argc, argv);
5292 * Close a file on a remote RPC server.
5294 * @param argc Standard main() style argc.
5295 * @param argv Standard main() style argv. Initial components are already
5298 * @return A shell status integer (0 for success).
5300 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5302 if (argc < 1 || c->display_usage) {
5303 return rpc_file_usage(c, argc, argv);
5306 return NetFileClose(c->opt_host, atoi(argv[0]));
5310 * Formatted print of open file info
5312 * @param r struct FILE_INFO_3 contents
5315 static void display_file_info_3(struct FILE_INFO_3 *r)
5317 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5318 r->fi3_id, r->fi3_username, r->fi3_permissions,
5319 r->fi3_num_locks, r->fi3_pathname);
5323 * List files for a user on a remote RPC server.
5325 * @param argc Standard main() style argc.
5326 * @param argv Standard main() style argv. Initial components are already
5329 * @return A shell status integer (0 for success)..
5332 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5334 NET_API_STATUS status;
5335 uint32 preferred_len = 0xffffffff, i;
5336 const char *username=NULL;
5337 uint32_t total_entries = 0;
5338 uint32_t entries_read = 0;
5339 uint32_t resume_handle = 0;
5340 struct FILE_INFO_3 *i3 = NULL;
5342 if (c->display_usage) {
5343 return rpc_file_usage(c, argc, argv);
5346 /* if argc > 0, must be user command */
5348 username = smb_xstrdup(argv[0]);
5351 status = NetFileEnum(c->opt_host,
5355 (uint8_t **)(void *)&i3,
5365 /* Display results */
5368 "\nEnumerating open files on remote server:\n\n"
5369 "\nFileId Opened by Perms Locks Path"
5370 "\n------ --------- ----- ----- ---- \n"));
5371 for (i = 0; i < entries_read; i++) {
5372 display_file_info_3(&i3[i]);
5379 * 'net rpc file' entrypoint.
5380 * @param argc Standard main() style argc.
5381 * @param argv Standard main() style argv. Initial components are already
5385 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5387 NET_API_STATUS status;
5389 struct functable func[] = {
5394 N_("Close opened file"),
5395 N_("net rpc file close\n"
5396 " Close opened file")
5402 N_("List files opened by user"),
5403 N_("net rpc file user\n"
5404 " List files opened by user")
5411 N_("Display information about opened file"),
5412 N_("net rpc file info\n"
5413 " Display information about opened file")
5416 {NULL, NULL, 0, NULL, NULL}
5419 status = libnetapi_net_init(&c->netapi_ctx);
5423 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5424 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5425 if (c->opt_kerberos) {
5426 libnetapi_set_use_kerberos(c->netapi_ctx);
5430 if (c->display_usage) {
5431 d_printf(_("Usage:\n"));
5432 d_printf(_("net rpc file\n"
5433 " List opened files\n"));
5434 net_display_usage_from_functable(func);
5438 return rpc_file_user(c, argc, argv);
5441 return net_run_function(c, argc, argv, "net rpc file", func);
5445 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5447 * All parameters are provided by the run_rpc_command function, except for
5448 * argc, argv which are passed through.
5450 * @param c A net_context structure.
5451 * @param domain_sid The domain sid acquired from the remote server.
5452 * @param cli A cli_state connected to the server.
5453 * @param mem_ctx Talloc context, destroyed on completion of the function.
5454 * @param argc Standard main() style argc.
5455 * @param argv Standard main() style argv. Initial components are already
5458 * @return Normal NTSTATUS return.
5461 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5462 const struct dom_sid *domain_sid,
5463 const char *domain_name,
5464 struct cli_state *cli,
5465 struct rpc_pipe_client *pipe_hnd,
5466 TALLOC_CTX *mem_ctx,
5470 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5472 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5474 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5475 if (!NT_STATUS_IS_OK(status)) {
5478 if (W_ERROR_IS_OK(result)) {
5479 d_printf(_("\nShutdown successfully aborted\n"));
5480 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5482 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5484 return werror_to_ntstatus(result);
5488 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5490 * All parameters are provided by the run_rpc_command function, except for
5491 * argc, argv which are passed through.
5493 * @param c A net_context structure.
5494 * @param domain_sid The domain sid acquired from the remote server.
5495 * @param cli A cli_state connected to the server.
5496 * @param mem_ctx Talloc context, destroyed on completion of the function.
5497 * @param argc Standard main() style argc.
5498 * @param argv Standard main() style argv. Initial components are already
5501 * @return Normal NTSTATUS return.
5504 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5505 const struct dom_sid *domain_sid,
5506 const char *domain_name,
5507 struct cli_state *cli,
5508 struct rpc_pipe_client *pipe_hnd,
5509 TALLOC_CTX *mem_ctx,
5513 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5515 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5517 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5519 if (!NT_STATUS_IS_OK(result)) {
5520 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5523 if (W_ERROR_IS_OK(werr)) {
5524 d_printf(_("\nShutdown successfully aborted\n"));
5525 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5527 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5529 return werror_to_ntstatus(werr);
5533 * ABORT the shutdown of a remote RPC server.
5535 * @param argc Standard main() style argc.
5536 * @param argv Standard main() style argv. Initial components are already
5539 * @return A shell status integer (0 for success).
5542 static int rpc_shutdown_abort(struct net_context *c, int argc,
5547 if (c->display_usage) {
5549 "net rpc abortshutdown\n"
5552 _("Abort a scheduled shutdown"));
5556 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5557 rpc_shutdown_abort_internals, argc, argv);
5562 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5564 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5565 rpc_reg_shutdown_abort_internals,
5570 * Shut down a remote RPC Server via initshutdown pipe.
5572 * All parameters are provided by the run_rpc_command function, except for
5573 * argc, argv which are passed through.
5575 * @param c A net_context structure.
5576 * @param domain_sid The domain sid acquired from the remote server.
5577 * @param cli A cli_state connected to the server.
5578 * @param mem_ctx Talloc context, destroyed on completion of the function.
5579 * @param argc Standard main() style argc.
5580 * @param argv Standard main() style argv. Initial components are already
5583 * @return Normal NTSTATUS return.
5586 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5587 const struct dom_sid *domain_sid,
5588 const char *domain_name,
5589 struct cli_state *cli,
5590 struct rpc_pipe_client *pipe_hnd,
5591 TALLOC_CTX *mem_ctx,
5595 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5597 const char *msg = N_("This machine will be shutdown shortly");
5598 uint32 timeout = 20;
5599 struct lsa_StringLarge msg_string;
5600 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5602 if (c->opt_comment) {
5603 msg = c->opt_comment;
5605 if (c->opt_timeout) {
5606 timeout = c->opt_timeout;
5609 msg_string.string = msg;
5611 /* create an entry */
5612 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5613 &msg_string, timeout, c->opt_force, c->opt_reboot,
5615 if (!NT_STATUS_IS_OK(status)) {
5618 if (W_ERROR_IS_OK(result)) {
5619 d_printf(_("\nShutdown of remote machine succeeded\n"));
5620 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5622 DEBUG(1,("Shutdown of remote machine failed!\n"));
5624 return werror_to_ntstatus(result);
5628 * Shut down a remote RPC Server via winreg pipe.
5630 * All parameters are provided by the run_rpc_command function, except for
5631 * argc, argv which are passed through.
5633 * @param c A net_context structure.
5634 * @param domain_sid The domain sid acquired from the remote server.
5635 * @param cli A cli_state connected to the server.
5636 * @param mem_ctx Talloc context, destroyed on completion of the function.
5637 * @param argc Standard main() style argc.
5638 * @param argv Standard main() style argv. Initial components are already
5641 * @return Normal NTSTATUS return.
5644 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5645 const struct dom_sid *domain_sid,
5646 const char *domain_name,
5647 struct cli_state *cli,
5648 struct rpc_pipe_client *pipe_hnd,
5649 TALLOC_CTX *mem_ctx,
5653 const char *msg = N_("This machine will be shutdown shortly");
5654 uint32 timeout = 20;
5655 struct lsa_StringLarge msg_string;
5658 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5660 if (c->opt_comment) {
5661 msg = c->opt_comment;
5663 msg_string.string = msg;
5665 if (c->opt_timeout) {
5666 timeout = c->opt_timeout;
5669 /* create an entry */
5670 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5671 &msg_string, timeout, c->opt_force, c->opt_reboot,
5673 if (!NT_STATUS_IS_OK(result)) {
5674 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5678 if (W_ERROR_IS_OK(werr)) {
5679 d_printf(_("\nShutdown of remote machine succeeded\n"));
5681 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5682 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5683 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5685 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5688 return werror_to_ntstatus(werr);
5692 * Shut down a remote RPC server.
5694 * @param argc Standard main() style argc.
5695 * @param argv Standard main() style argv. Initial components are already
5698 * @return A shell status integer (0 for success).
5701 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5705 if (c->display_usage) {
5707 "net rpc shutdown\n"
5710 _("Shut down a remote RPC server"));
5714 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5715 rpc_init_shutdown_internals, argc, argv);
5718 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5719 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5720 rpc_reg_shutdown_internals, argc, argv);
5726 /***************************************************************************
5727 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5728 ***************************************************************************/
5731 * Add interdomain trust account to the RPC server.
5732 * All parameters (except for argc and argv) are passed by run_rpc_command
5735 * @param c A net_context structure.
5736 * @param domain_sid The domain sid acquired from the server.
5737 * @param cli A cli_state connected to the server.
5738 * @param mem_ctx Talloc context, destroyed on completion of the function.
5739 * @param argc Standard main() style argc.
5740 * @param argv Standard main() style argv. Initial components are already
5743 * @return normal NTSTATUS return code.
5746 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5747 const struct dom_sid *domain_sid,
5748 const char *domain_name,
5749 struct cli_state *cli,
5750 struct rpc_pipe_client *pipe_hnd,
5751 TALLOC_CTX *mem_ctx,
5755 struct policy_handle connect_pol, domain_pol, user_pol;
5756 NTSTATUS status, result;
5758 struct lsa_String lsa_acct_name;
5760 uint32 acct_flags=0;
5762 uint32_t access_granted = 0;
5763 union samr_UserInfo info;
5764 unsigned int orig_timeout;
5765 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5770 _(" net rpc trustdom add <domain_name> "
5771 "<trust password>\n"));
5772 return NT_STATUS_INVALID_PARAMETER;
5776 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5779 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5780 return NT_STATUS_NO_MEMORY;
5783 strupper_m(acct_name);
5785 init_lsa_String(&lsa_acct_name, acct_name);
5787 /* Get samr policy handle */
5788 status = dcerpc_samr_Connect2(b, mem_ctx,
5790 MAXIMUM_ALLOWED_ACCESS,
5793 if (!NT_STATUS_IS_OK(status)) {
5796 if (!NT_STATUS_IS_OK(result)) {
5801 /* Get domain policy handle */
5802 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5804 MAXIMUM_ALLOWED_ACCESS,
5805 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5808 if (!NT_STATUS_IS_OK(status)) {
5811 if (!NT_STATUS_IS_OK(result)) {
5816 /* This call can take a long time - allow the server to time out.
5817 * 35 seconds should do it. */
5819 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5821 /* Create trusting domain's account */
5822 acb_info = ACB_NORMAL;
5823 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5824 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5825 SAMR_USER_ACCESS_SET_PASSWORD |
5826 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5827 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5829 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5838 if (!NT_STATUS_IS_OK(status)) {
5841 /* And restore our original timeout. */
5842 rpccli_set_timeout(pipe_hnd, orig_timeout);
5844 if (!NT_STATUS_IS_OK(result)) {
5846 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5847 acct_name, nt_errstr(result));
5852 struct samr_CryptPassword crypt_pwd;
5854 ZERO_STRUCT(info.info23);
5856 init_samr_CryptPassword(argv[1],
5857 &cli->user_session_key,
5860 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5861 SAMR_FIELD_NT_PASSWORD_PRESENT;
5862 info.info23.info.acct_flags = ACB_DOMTRUST;
5863 info.info23.password = crypt_pwd;
5865 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5870 if (!NT_STATUS_IS_OK(status)) {
5874 if (!NT_STATUS_IS_OK(result)) {
5876 DEBUG(0,("Could not set trust account password: %s\n",
5877 nt_errstr(result)));
5883 SAFE_FREE(acct_name);
5888 * Create interdomain trust account for a remote domain.
5890 * @param argc Standard argc.
5891 * @param argv Standard argv without initial components.
5893 * @return Integer status (0 means success).
5896 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5898 if (argc > 0 && !c->display_usage) {
5899 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5900 rpc_trustdom_add_internals, argc, argv);
5904 _("net rpc trustdom add <domain_name> <trust "
5912 * Remove interdomain trust account from the RPC server.
5913 * All parameters (except for argc and argv) are passed by run_rpc_command
5916 * @param c A net_context structure.
5917 * @param domain_sid The domain sid acquired from the server.
5918 * @param cli A cli_state connected to the server.
5919 * @param mem_ctx Talloc context, destroyed on completion of the function.
5920 * @param argc Standard main() style argc.
5921 * @param argv Standard main() style argv. Initial components are already
5924 * @return normal NTSTATUS return code.
5927 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5928 const struct dom_sid *domain_sid,
5929 const char *domain_name,
5930 struct cli_state *cli,
5931 struct rpc_pipe_client *pipe_hnd,
5932 TALLOC_CTX *mem_ctx,
5936 struct policy_handle connect_pol, domain_pol, user_pol;
5937 NTSTATUS status, result;
5939 struct dom_sid trust_acct_sid;
5940 struct samr_Ids user_rids, name_types;
5941 struct lsa_String lsa_acct_name;
5942 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5947 _(" net rpc trustdom del <domain_name>\n"));
5948 return NT_STATUS_INVALID_PARAMETER;
5952 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5954 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5956 if (acct_name == NULL)
5957 return NT_STATUS_NO_MEMORY;
5959 strupper_m(acct_name);
5961 /* Get samr policy handle */
5962 status = dcerpc_samr_Connect2(b, mem_ctx,
5964 MAXIMUM_ALLOWED_ACCESS,
5967 if (!NT_STATUS_IS_OK(status)) {
5970 if (!NT_STATUS_IS_OK(result)) {
5975 /* Get domain policy handle */
5976 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5978 MAXIMUM_ALLOWED_ACCESS,
5979 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5982 if (!NT_STATUS_IS_OK(status)) {
5985 if (!NT_STATUS_IS_OK(result)) {
5990 init_lsa_String(&lsa_acct_name, acct_name);
5992 status = dcerpc_samr_LookupNames(b, mem_ctx,
5999 if (!NT_STATUS_IS_OK(status)) {
6000 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6002 acct_name, nt_errstr(status));
6005 if (!NT_STATUS_IS_OK(result)) {
6007 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6009 acct_name, nt_errstr(result) );
6013 status = dcerpc_samr_OpenUser(b, mem_ctx,
6015 MAXIMUM_ALLOWED_ACCESS,
6019 if (!NT_STATUS_IS_OK(status)) {
6020 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6022 acct_name, nt_errstr(status) );
6026 if (!NT_STATUS_IS_OK(result)) {
6028 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6030 acct_name, nt_errstr(result) );
6034 /* append the rid to the domain sid */
6035 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6039 /* remove the sid */
6041 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6045 if (!NT_STATUS_IS_OK(status)) {
6046 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6047 " on user %s failed %s\n"),
6048 acct_name, nt_errstr(status));
6051 if (!NT_STATUS_IS_OK(result)) {
6053 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6054 " on user %s failed %s\n"),
6055 acct_name, nt_errstr(result) );
6062 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6065 if (!NT_STATUS_IS_OK(status)) {
6066 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6068 acct_name, nt_errstr(status));
6072 if (!NT_STATUS_IS_OK(result)) {
6074 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6076 acct_name, nt_errstr(result) );
6080 if (!NT_STATUS_IS_OK(result)) {
6081 d_printf(_("Could not set trust account password: %s\n"),
6091 * Delete interdomain trust account for a remote domain.
6093 * @param argc Standard argc.
6094 * @param argv Standard argv without initial components.
6096 * @return Integer status (0 means success).
6099 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6101 if (argc > 0 && !c->display_usage) {
6102 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
6103 rpc_trustdom_del_internals, argc, argv);
6107 _("net rpc trustdom del <domain>\n"));
6112 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6113 struct cli_state *cli,
6114 TALLOC_CTX *mem_ctx,
6115 const char *domain_name)
6117 char *dc_name = NULL;
6118 const char *buffer = NULL;
6119 struct rpc_pipe_client *netr;
6122 struct dcerpc_binding_handle *b;
6124 /* Use NetServerEnum2 */
6126 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6128 return NT_STATUS_OK;
6131 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6132 for domain %s\n", domain_name));
6134 /* Try netr_GetDcName */
6136 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
6138 if (!NT_STATUS_IS_OK(status)) {
6142 b = netr->binding_handle;
6144 status = dcerpc_netr_GetDcName(b, mem_ctx,
6151 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6155 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6156 for domain %s\n", domain_name));
6158 if (!NT_STATUS_IS_OK(status)) {
6162 return werror_to_ntstatus(result);
6166 * Establish trust relationship to a trusting domain.
6167 * Interdomain account must already be created on remote PDC.
6169 * @param c A net_context structure.
6170 * @param argc Standard argc.
6171 * @param argv Standard argv without initial components.
6173 * @return Integer status (0 means success).
6176 static int rpc_trustdom_establish(struct net_context *c, int argc,
6179 struct cli_state *cli = NULL;
6180 struct sockaddr_storage server_ss;
6181 struct rpc_pipe_client *pipe_hnd = NULL;
6182 struct policy_handle connect_hnd;
6183 TALLOC_CTX *mem_ctx;
6184 NTSTATUS nt_status, result;
6185 struct dom_sid *domain_sid;
6190 union lsa_PolicyInformation *info = NULL;
6191 struct dcerpc_binding_handle *b;
6194 * Connect to \\server\ipc$ as 'our domain' account with password
6197 if (argc != 1 || c->display_usage) {
6200 _("net rpc trustdom establish <domain_name>\n"));
6204 domain_name = smb_xstrdup(argv[0]);
6205 strupper_m(domain_name);
6207 /* account name used at first is our domain's name with '$' */
6208 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6211 strupper_m(acct_name);
6214 * opt_workgroup will be used by connection functions further,
6215 * hence it should be set to remote domain name instead of ours
6217 if (c->opt_workgroup) {
6218 c->opt_workgroup = smb_xstrdup(domain_name);
6221 c->opt_user_name = acct_name;
6223 /* find the domain controller */
6224 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6225 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6229 /* connect to ipc$ as username/password */
6230 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6231 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6233 /* Is it trusting domain account for sure ? */
6234 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6235 nt_errstr(nt_status)));
6239 /* store who we connected to */
6241 saf_store( domain_name, pdc_name );
6244 * Connect to \\server\ipc$ again (this time anonymously)
6247 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6250 if (NT_STATUS_IS_ERR(nt_status)) {
6251 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6252 domain_name, nt_errstr(nt_status)));
6256 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6257 "domain %s", domain_name))) {
6258 DEBUG(0, ("talloc_init() failed\n"));
6263 /* Make sure we're talking to a proper server */
6265 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6266 if (!NT_STATUS_IS_OK(nt_status)) {
6268 talloc_destroy(mem_ctx);
6273 * Call LsaOpenPolicy and LsaQueryInfo
6276 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6278 if (!NT_STATUS_IS_OK(nt_status)) {
6279 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6281 talloc_destroy(mem_ctx);
6285 b = pipe_hnd->binding_handle;
6287 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6289 if (NT_STATUS_IS_ERR(nt_status)) {
6290 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6291 nt_errstr(nt_status)));
6293 talloc_destroy(mem_ctx);
6297 /* Querying info level 5 */
6299 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6301 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6304 if (NT_STATUS_IS_ERR(nt_status)) {
6305 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6306 nt_errstr(nt_status)));
6308 talloc_destroy(mem_ctx);
6311 if (NT_STATUS_IS_ERR(result)) {
6312 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6313 nt_errstr(result)));
6315 talloc_destroy(mem_ctx);
6319 domain_sid = info->account_domain.sid;
6321 /* There should be actually query info level 3 (following nt serv behaviour),
6322 but I still don't know if it's _really_ necessary */
6325 * Store the password in secrets db
6328 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6329 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6331 talloc_destroy(mem_ctx);
6336 * Close the pipes and clean up
6339 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6340 if (NT_STATUS_IS_ERR(nt_status)) {
6341 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6342 nt_errstr(nt_status)));
6344 talloc_destroy(mem_ctx);
6350 talloc_destroy(mem_ctx);
6352 d_printf(_("Trust to domain %s established\n"), domain_name);
6357 * Revoke trust relationship to the remote domain.
6359 * @param c A net_context structure.
6360 * @param argc Standard argc.
6361 * @param argv Standard argv without initial components.
6363 * @return Integer status (0 means success).
6366 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6372 if (argc < 1 || c->display_usage) {
6375 _("net rpc trustdom revoke <domain_name>\n"
6376 " Revoke trust relationship\n"
6377 " domain_name\tName of domain to revoke trust\n"));
6381 /* generate upper cased domain name */
6382 domain_name = smb_xstrdup(argv[0]);
6383 strupper_m(domain_name);
6385 /* delete password of the trust */
6386 if (!pdb_del_trusteddom_pw(domain_name)) {
6387 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6394 SAFE_FREE(domain_name);
6398 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6399 const struct dom_sid *domain_sid,
6400 const char *domain_name,
6401 struct cli_state *cli,
6402 struct rpc_pipe_client *pipe_hnd,
6403 TALLOC_CTX *mem_ctx,
6408 if (!sid_to_fstring(str_sid, domain_sid)) {
6409 return NT_STATUS_UNSUCCESSFUL;
6411 d_printf("%s\n", str_sid);
6412 return NT_STATUS_OK;
6415 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6419 /* convert sid into ascii string */
6420 sid_to_fstring(ascii_sid, dom_sid);
6422 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6425 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6426 TALLOC_CTX *mem_ctx,
6427 struct policy_handle *pol,
6428 struct dom_sid dom_sid,
6429 const char *trusted_dom_name)
6431 NTSTATUS nt_status, result;
6432 union lsa_TrustedDomainInfo *info = NULL;
6433 char *cleartextpwd = NULL;
6434 uint8_t session_key[16];
6435 DATA_BLOB session_key_blob;
6436 DATA_BLOB data = data_blob_null;
6437 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6439 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6442 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6445 if (NT_STATUS_IS_ERR(nt_status)) {
6446 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6447 nt_errstr(nt_status)));
6450 if (NT_STATUS_IS_ERR(result)) {
6452 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6453 nt_errstr(result)));
6457 data = data_blob(info->password.password->data,
6458 info->password.password->length);
6460 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
6461 DEBUG(0, ("Could not retrieve password hash\n"));
6465 session_key_blob = data_blob_const(session_key, sizeof(session_key));
6466 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
6468 if (cleartextpwd == NULL) {
6469 DEBUG(0,("retrieved NULL password\n"));
6470 nt_status = NT_STATUS_UNSUCCESSFUL;
6474 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6475 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6476 nt_status = NT_STATUS_UNSUCCESSFUL;
6480 #ifdef DEBUG_PASSWORD
6481 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6482 "password: [%s]\n", trusted_dom_name,
6483 sid_string_dbg(&dom_sid), cleartextpwd));
6487 SAFE_FREE(cleartextpwd);
6488 data_blob_free(&data);
6493 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6496 /* common variables */
6497 TALLOC_CTX* mem_ctx;
6498 struct cli_state *cli = NULL;
6499 struct rpc_pipe_client *pipe_hnd = NULL;
6500 NTSTATUS nt_status, result;
6501 const char *domain_name = NULL;
6502 struct dom_sid *queried_dom_sid;
6503 struct policy_handle connect_hnd;
6504 union lsa_PolicyInformation *info = NULL;
6506 /* trusted domains listing variables */
6507 unsigned int enum_ctx = 0;
6509 struct lsa_DomainList dom_list;
6511 struct dcerpc_binding_handle *b;
6513 if (c->display_usage) {
6515 "net rpc trustdom vampire\n"
6518 _("Vampire trust relationship from remote server"));
6523 * Listing trusted domains (stored in secrets.tdb, if local)
6526 mem_ctx = talloc_init("trust relationships vampire");
6529 * set domain and pdc name to local samba server (default)
6530 * or to remote one given in command line
6533 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6534 domain_name = c->opt_workgroup;
6535 c->opt_target_workgroup = c->opt_workgroup;
6537 fstrcpy(pdc_name, global_myname());
6538 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6539 c->opt_target_workgroup = domain_name;
6542 /* open \PIPE\lsarpc and open policy handle */
6543 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6544 if (!NT_STATUS_IS_OK(nt_status)) {
6545 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6546 nt_errstr(nt_status)));
6547 talloc_destroy(mem_ctx);
6551 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6553 if (!NT_STATUS_IS_OK(nt_status)) {
6554 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6555 nt_errstr(nt_status) ));
6557 talloc_destroy(mem_ctx);
6561 b = pipe_hnd->binding_handle;
6563 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6565 if (NT_STATUS_IS_ERR(nt_status)) {
6566 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6567 nt_errstr(nt_status)));
6569 talloc_destroy(mem_ctx);
6573 /* query info level 5 to obtain sid of a domain being queried */
6574 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6576 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6580 if (NT_STATUS_IS_ERR(nt_status)) {
6581 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6582 nt_errstr(nt_status)));
6584 talloc_destroy(mem_ctx);
6587 if (NT_STATUS_IS_ERR(result)) {
6588 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6589 nt_errstr(result)));
6591 talloc_destroy(mem_ctx);
6595 queried_dom_sid = info->account_domain.sid;
6598 * Keep calling LsaEnumTrustdom over opened pipe until
6599 * the end of enumeration is reached
6602 d_printf(_("Vampire trusted domains:\n\n"));
6605 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6611 if (NT_STATUS_IS_ERR(nt_status)) {
6612 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6613 nt_errstr(nt_status)));
6615 talloc_destroy(mem_ctx);
6618 if (NT_STATUS_IS_ERR(result)) {
6620 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6621 nt_errstr(result)));
6623 talloc_destroy(mem_ctx);
6628 for (i = 0; i < dom_list.count; i++) {
6630 print_trusted_domain(dom_list.domains[i].sid,
6631 dom_list.domains[i].name.string);
6633 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6634 *dom_list.domains[i].sid,
6635 dom_list.domains[i].name.string);
6636 if (!NT_STATUS_IS_OK(nt_status)) {
6638 talloc_destroy(mem_ctx);
6644 * in case of no trusted domains say something rather
6645 * than just display blank line
6647 if (!dom_list.count) d_printf(_("none\n"));
6649 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6651 /* close this connection before doing next one */
6652 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6653 if (NT_STATUS_IS_ERR(nt_status)) {
6654 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6655 nt_errstr(nt_status)));
6657 talloc_destroy(mem_ctx);
6661 /* close lsarpc pipe and connection to IPC$ */
6664 talloc_destroy(mem_ctx);
6668 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6670 /* common variables */
6671 TALLOC_CTX* mem_ctx;
6672 struct cli_state *cli = NULL, *remote_cli = NULL;
6673 struct rpc_pipe_client *pipe_hnd = NULL;
6674 NTSTATUS nt_status, result;
6675 const char *domain_name = NULL;
6676 struct dom_sid *queried_dom_sid;
6677 int ascii_dom_name_len;
6678 struct policy_handle connect_hnd;
6679 union lsa_PolicyInformation *info = NULL;
6680 struct dcerpc_binding_handle *b = NULL;
6682 /* trusted domains listing variables */
6683 unsigned int num_domains, enum_ctx = 0;
6685 struct lsa_DomainList dom_list;
6689 /* trusting domains listing variables */
6690 struct policy_handle domain_hnd;
6691 struct samr_SamArray *trusts = NULL;
6693 if (c->display_usage) {
6695 "net rpc trustdom list\n"
6698 _("List incoming and outgoing trust relationships"));
6703 * Listing trusted domains (stored in secrets.tdb, if local)
6706 mem_ctx = talloc_init("trust relationships listing");
6709 * set domain and pdc name to local samba server (default)
6710 * or to remote one given in command line
6713 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6714 domain_name = c->opt_workgroup;
6715 c->opt_target_workgroup = c->opt_workgroup;
6717 fstrcpy(pdc_name, global_myname());
6718 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6719 c->opt_target_workgroup = domain_name;
6722 /* open \PIPE\lsarpc and open policy handle */
6723 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6724 if (!NT_STATUS_IS_OK(nt_status)) {
6725 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6726 nt_errstr(nt_status)));
6727 talloc_destroy(mem_ctx);
6731 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6733 if (!NT_STATUS_IS_OK(nt_status)) {
6734 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6735 nt_errstr(nt_status) ));
6737 talloc_destroy(mem_ctx);
6741 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6743 if (NT_STATUS_IS_ERR(nt_status)) {
6744 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6745 nt_errstr(nt_status)));
6747 talloc_destroy(mem_ctx);
6751 /* query info level 5 to obtain sid of a domain being queried */
6752 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6754 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6758 if (NT_STATUS_IS_ERR(nt_status)) {
6759 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6760 nt_errstr(nt_status)));
6762 talloc_destroy(mem_ctx);
6765 if (NT_STATUS_IS_ERR(result)) {
6766 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6767 nt_errstr(result)));
6769 talloc_destroy(mem_ctx);
6773 queried_dom_sid = info->account_domain.sid;
6776 * Keep calling LsaEnumTrustdom over opened pipe until
6777 * the end of enumeration is reached
6780 d_printf(_("Trusted domains list:\n\n"));
6782 found_domain = false;
6785 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6791 if (NT_STATUS_IS_ERR(nt_status)) {
6792 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6793 nt_errstr(nt_status)));
6795 talloc_destroy(mem_ctx);
6798 if (NT_STATUS_IS_ERR(result)) {
6799 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6800 nt_errstr(result)));
6802 talloc_destroy(mem_ctx);
6807 for (i = 0; i < dom_list.count; i++) {
6808 print_trusted_domain(dom_list.domains[i].sid,
6809 dom_list.domains[i].name.string);
6810 found_domain = true;
6814 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6817 * in case of no trusted domains say something rather
6818 * than just display blank line
6820 if (!found_domain) {
6821 d_printf(_("none\n"));
6824 /* close this connection before doing next one */
6825 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6826 if (NT_STATUS_IS_ERR(nt_status)) {
6827 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6828 nt_errstr(nt_status)));
6830 talloc_destroy(mem_ctx);
6834 TALLOC_FREE(pipe_hnd);
6837 * Listing trusting domains (stored in passdb backend, if local)
6840 d_printf(_("\nTrusting domains list:\n\n"));
6843 * Open \PIPE\samr and get needed policy handles
6845 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6847 if (!NT_STATUS_IS_OK(nt_status)) {
6848 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6850 talloc_destroy(mem_ctx);
6854 b = pipe_hnd->binding_handle;
6857 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6859 SAMR_ACCESS_LOOKUP_DOMAIN,
6862 if (!NT_STATUS_IS_OK(nt_status)) {
6863 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6864 nt_errstr(nt_status)));
6866 talloc_destroy(mem_ctx);
6869 if (!NT_STATUS_IS_OK(result)) {
6871 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6872 nt_errstr(result)));
6874 talloc_destroy(mem_ctx);
6878 /* SamrOpenDomain - we have to open domain policy handle in order to be
6879 able to enumerate accounts*/
6880 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6882 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6886 if (!NT_STATUS_IS_OK(nt_status)) {
6887 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6888 nt_errstr(nt_status)));
6890 talloc_destroy(mem_ctx);
6893 if (!NT_STATUS_IS_OK(result)) {
6895 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6896 nt_errstr(result)));
6898 talloc_destroy(mem_ctx);
6903 * perform actual enumeration
6906 found_domain = false;
6908 enum_ctx = 0; /* reset enumeration context from last enumeration */
6911 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
6919 if (NT_STATUS_IS_ERR(nt_status)) {
6920 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6921 nt_errstr(nt_status)));
6923 talloc_destroy(mem_ctx);
6926 if (NT_STATUS_IS_ERR(result)) {
6928 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6929 nt_errstr(result)));
6931 talloc_destroy(mem_ctx);
6935 for (i = 0; i < num_domains; i++) {
6937 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6939 found_domain = true;
6942 * get each single domain's sid (do we _really_ need this ?):
6943 * 1) connect to domain's pdc
6944 * 2) query the pdc for domain's sid
6947 /* get rid of '$' tail */
6948 ascii_dom_name_len = strlen(str);
6949 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6950 str[ascii_dom_name_len - 1] = '\0';
6952 /* set opt_* variables to remote domain */
6954 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6955 c->opt_target_workgroup = c->opt_workgroup;
6957 d_printf("%-20s", str);
6959 /* connect to remote domain controller */
6960 nt_status = net_make_ipc_connection(c,
6961 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6963 if (NT_STATUS_IS_OK(nt_status)) {
6964 /* query for domain's sid */
6965 if (run_rpc_command(
6967 &ndr_table_lsarpc.syntax_id, 0,
6968 rpc_query_domain_sid, argc,
6970 d_printf(_("strange - couldn't get domain's sid\n"));
6972 cli_shutdown(remote_cli);
6975 d_fprintf(stderr, _("domain controller is not "
6976 "responding: %s\n"),
6977 nt_errstr(nt_status));
6978 d_printf(_("couldn't get domain's sid\n"));
6982 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
6984 if (!found_domain) {
6988 /* close opened samr and domain policy handles */
6989 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
6990 if (!NT_STATUS_IS_OK(nt_status)) {
6991 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6994 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
6995 if (!NT_STATUS_IS_OK(nt_status)) {
6996 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6999 /* close samr pipe and connection to IPC$ */
7002 talloc_destroy(mem_ctx);
7007 * Entrypoint for 'net rpc trustdom' code.
7009 * @param argc Standard argc.
7010 * @param argv Standard argv without initial components.
7012 * @return Integer status (0 means success).
7015 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7017 struct functable func[] = {
7022 N_("Add trusting domain's account"),
7023 N_("net rpc trustdom add\n"
7024 " Add trusting domain's account")
7030 N_("Remove trusting domain's account"),
7031 N_("net rpc trustdom del\n"
7032 " Remove trusting domain's account")
7036 rpc_trustdom_establish,
7038 N_("Establish outgoing trust relationship"),
7039 N_("net rpc trustdom establish\n"
7040 " Establish outgoing trust relationship")
7044 rpc_trustdom_revoke,
7046 N_("Revoke outgoing trust relationship"),
7047 N_("net rpc trustdom revoke\n"
7048 " Revoke outgoing trust relationship")
7054 N_("List in- and outgoing domain trusts"),
7055 N_("net rpc trustdom list\n"
7056 " List in- and outgoing domain trusts")
7060 rpc_trustdom_vampire,
7062 N_("Vampire trusts from remote server"),
7063 N_("net rpc trustdom vampire\n"
7064 " Vampire trusts from remote server")
7066 {NULL, NULL, 0, NULL, NULL}
7069 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7073 * Check if a server will take rpc commands
7074 * @param flags Type of server to connect to (PDC, DMB, localhost)
7075 * if the host is not explicitly specified
7076 * @return bool (true means rpc supported)
7078 bool net_rpc_check(struct net_context *c, unsigned flags)
7080 struct cli_state *cli;
7082 struct sockaddr_storage server_ss;
7083 char *server_name = NULL;
7086 /* flags (i.e. server type) may depend on command */
7087 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7090 if ((cli = cli_initialise()) == NULL) {
7094 status = cli_connect(cli, server_name, &server_ss);
7095 if (!NT_STATUS_IS_OK(status))
7097 if (!attempt_netbios_session_request(&cli, global_myname(),
7098 server_name, &server_ss))
7100 status = cli_negprot(cli);
7101 if (!NT_STATUS_IS_OK(status))
7103 if (cli->protocol < PROTOCOL_NT1)
7112 /* dump sam database via samsync rpc calls */
7113 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7114 if (c->display_usage) {
7119 _("Dump remote SAM database"));
7123 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
7124 NET_FLAGS_ANONYMOUS,
7125 rpc_samdump_internals, argc, argv);
7128 /* syncronise sam database via samsync rpc calls */
7129 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7131 struct functable func[] = {
7136 N_("Dump remote SAM database to ldif"),
7137 N_("net rpc vampire ldif\n"
7138 " Dump remote SAM database to LDIF file or "
7145 N_("Dump remote SAM database to Kerberos Keytab"),
7146 N_("net rpc vampire keytab\n"
7147 " Dump remote SAM database to Kerberos keytab "
7154 N_("Dump remote SAM database to passdb"),
7155 N_("net rpc vampire passdb\n"
7156 " Dump remote SAM database to passdb")
7159 {NULL, NULL, 0, NULL, NULL}
7163 if (c->display_usage) {
7168 _("Vampire remote SAM database"));
7172 return rpc_vampire_passdb(c, argc, argv);
7175 return net_run_function(c, argc, argv, "net rpc vampire", func);
7179 * Migrate everything from a print server.
7181 * @param c A net_context structure.
7182 * @param argc Standard main() style argc.
7183 * @param argv Standard main() style argv. Initial components are already
7186 * @return A shell status integer (0 for success).
7188 * The order is important !
7189 * To successfully add drivers the print queues have to exist !
7190 * Applying ACLs should be the last step, because you're easily locked out.
7193 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7198 if (c->display_usage) {
7200 "net rpc printer migrate all\n"
7203 _("Migrate everything from a print server"));
7208 d_printf(_("no server to migrate\n"));
7212 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7213 rpc_printer_migrate_printers_internals, argc,
7218 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7219 rpc_printer_migrate_drivers_internals, argc,
7224 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7225 rpc_printer_migrate_forms_internals, argc, argv);
7229 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7230 rpc_printer_migrate_settings_internals, argc,
7235 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7236 rpc_printer_migrate_security_internals, argc,
7242 * Migrate print drivers from a print server.
7244 * @param c A net_context structure.
7245 * @param argc Standard main() style argc.
7246 * @param argv Standard main() style argv. Initial components are already
7249 * @return A shell status integer (0 for success).
7251 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7254 if (c->display_usage) {
7256 "net rpc printer migrate drivers\n"
7259 _("Migrate print-drivers from a print-server"));
7264 d_printf(_("no server to migrate\n"));
7268 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7269 rpc_printer_migrate_drivers_internals,
7274 * Migrate print-forms from a print-server.
7276 * @param c A net_context structure.
7277 * @param argc Standard main() style argc.
7278 * @param argv Standard main() style argv. Initial components are already
7281 * @return A shell status integer (0 for success).
7283 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7286 if (c->display_usage) {
7288 "net rpc printer migrate forms\n"
7291 _("Migrate print-forms from a print-server"));
7296 d_printf(_("no server to migrate\n"));
7300 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7301 rpc_printer_migrate_forms_internals,
7306 * Migrate printers from a print-server.
7308 * @param c A net_context structure.
7309 * @param argc Standard main() style argc.
7310 * @param argv Standard main() style argv. Initial components are already
7313 * @return A shell status integer (0 for success).
7315 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7318 if (c->display_usage) {
7320 "net rpc printer migrate printers\n"
7323 _("Migrate printers from a print-server"));
7328 d_printf(_("no server to migrate\n"));
7332 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7333 rpc_printer_migrate_printers_internals,
7338 * Migrate printer-ACLs from a print-server
7340 * @param c A net_context structure.
7341 * @param argc Standard main() style argc.
7342 * @param argv Standard main() style argv. Initial components are already
7345 * @return A shell status integer (0 for success).
7347 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7350 if (c->display_usage) {
7352 "net rpc printer migrate security\n"
7355 _("Migrate printer-ACLs from a print-server"));
7360 d_printf(_("no server to migrate\n"));
7364 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7365 rpc_printer_migrate_security_internals,
7370 * Migrate printer-settings from a print-server.
7372 * @param c A net_context structure.
7373 * @param argc Standard main() style argc.
7374 * @param argv Standard main() style argv. Initial components are already
7377 * @return A shell status integer (0 for success).
7379 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7382 if (c->display_usage) {
7384 "net rpc printer migrate settings\n"
7387 _("Migrate printer-settings from a "
7393 d_printf(_("no server to migrate\n"));
7397 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7398 rpc_printer_migrate_settings_internals,
7403 * 'net rpc printer' entrypoint.
7405 * @param c A net_context structure.
7406 * @param argc Standard main() style argc.
7407 * @param argv Standard main() style argv. Initial components are already
7411 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7414 /* ouch: when addriver and setdriver are called from within
7415 rpc_printer_migrate_drivers_internals, the printer-queue already
7418 struct functable func[] = {
7421 rpc_printer_migrate_all,
7423 N_("Migrate all from remote to local print server"),
7424 N_("net rpc printer migrate all\n"
7425 " Migrate all from remote to local print server")
7429 rpc_printer_migrate_drivers,
7431 N_("Migrate drivers to local server"),
7432 N_("net rpc printer migrate drivers\n"
7433 " Migrate drivers to local server")
7437 rpc_printer_migrate_forms,
7439 N_("Migrate froms to local server"),
7440 N_("net rpc printer migrate forms\n"
7441 " Migrate froms to local server")
7445 rpc_printer_migrate_printers,
7447 N_("Migrate printers to local server"),
7448 N_("net rpc printer migrate printers\n"
7449 " Migrate printers to local server")
7453 rpc_printer_migrate_security,
7455 N_("Mirgate printer ACLs to local server"),
7456 N_("net rpc printer migrate security\n"
7457 " Mirgate printer ACLs to local server")
7461 rpc_printer_migrate_settings,
7463 N_("Migrate printer settings to local server"),
7464 N_("net rpc printer migrate settings\n"
7465 " Migrate printer settings to local server")
7467 {NULL, NULL, 0, NULL, NULL}
7470 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7475 * List printers on a remote RPC server.
7477 * @param c A net_context structure.
7478 * @param argc Standard main() style argc.
7479 * @param argv Standard main() style argv. Initial components are already
7482 * @return A shell status integer (0 for success).
7484 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7486 if (c->display_usage) {
7488 "net rpc printer list\n"
7491 _("List printers on a remote RPC server"));
7495 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7496 rpc_printer_list_internals,
7501 * List printer-drivers on a remote RPC server.
7503 * @param c A net_context structure.
7504 * @param argc Standard main() style argc.
7505 * @param argv Standard main() style argv. Initial components are already
7508 * @return A shell status integer (0 for success).
7510 static int rpc_printer_driver_list(struct net_context *c, int argc,
7513 if (c->display_usage) {
7515 "net rpc printer driver\n"
7518 _("List printer-drivers on a remote RPC server"));
7522 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7523 rpc_printer_driver_list_internals,
7528 * Publish printer in ADS via MSRPC.
7530 * @param c A net_context structure.
7531 * @param argc Standard main() style argc.
7532 * @param argv Standard main() style argv. Initial components are already
7535 * @return A shell status integer (0 for success).
7537 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7540 if (c->display_usage) {
7542 "net rpc printer publish publish\n"
7545 _("Publish printer in ADS via MSRPC"));
7549 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7550 rpc_printer_publish_publish_internals,
7555 * Update printer in ADS via MSRPC.
7557 * @param c A net_context structure.
7558 * @param argc Standard main() style argc.
7559 * @param argv Standard main() style argv. Initial components are already
7562 * @return A shell status integer (0 for success).
7564 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7566 if (c->display_usage) {
7568 "net rpc printer publish update\n"
7571 _("Update printer in ADS via MSRPC"));
7575 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7576 rpc_printer_publish_update_internals,
7581 * UnPublish printer in ADS via MSRPC.
7583 * @param c A net_context structure.
7584 * @param argc Standard main() style argc.
7585 * @param argv Standard main() style argv. Initial components are already
7588 * @return A shell status integer (0 for success).
7590 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7593 if (c->display_usage) {
7595 "net rpc printer publish unpublish\n"
7598 _("UnPublish printer in ADS via MSRPC"));
7602 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7603 rpc_printer_publish_unpublish_internals,
7608 * List published printers via MSRPC.
7610 * @param c A net_context structure.
7611 * @param argc Standard main() style argc.
7612 * @param argv Standard main() style argv. Initial components are already
7615 * @return A shell status integer (0 for success).
7617 static int rpc_printer_publish_list(struct net_context *c, int argc,
7620 if (c->display_usage) {
7622 "net rpc printer publish list\n"
7625 _("List published printers via MSRPC"));
7629 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7630 rpc_printer_publish_list_internals,
7636 * Publish printer in ADS.
7638 * @param c A net_context structure.
7639 * @param argc Standard main() style argc.
7640 * @param argv Standard main() style argv. Initial components are already
7643 * @return A shell status integer (0 for success).
7645 static int rpc_printer_publish(struct net_context *c, int argc,
7649 struct functable func[] = {
7652 rpc_printer_publish_publish,
7654 N_("Publish printer in AD"),
7655 N_("net rpc printer publish publish\n"
7656 " Publish printer in AD")
7660 rpc_printer_publish_update,
7662 N_("Update printer in AD"),
7663 N_("net rpc printer publish update\n"
7664 " Update printer in AD")
7668 rpc_printer_publish_unpublish,
7670 N_("Unpublish printer"),
7671 N_("net rpc printer publish unpublish\n"
7672 " Unpublish printer")
7676 rpc_printer_publish_list,
7678 N_("List published printers"),
7679 N_("net rpc printer publish list\n"
7680 " List published printers")
7682 {NULL, NULL, 0, NULL, NULL}
7686 if (c->display_usage) {
7687 d_printf(_("Usage:\n"));
7688 d_printf(_("net rpc printer publish\n"
7689 " List published printers\n"
7690 " Alias of net rpc printer publish "
7692 net_display_usage_from_functable(func);
7695 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7696 rpc_printer_publish_list_internals,
7700 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7706 * Display rpc printer help page.
7708 * @param c A net_context structure.
7709 * @param argc Standard main() style argc.
7710 * @param argv Standard main() style argv. Initial components are already
7713 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7715 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7716 "\tlists all printers on print-server\n\n"));
7717 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7718 "\tlists all printer-drivers on print-server\n\n"));
7719 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7720 "\tpublishes printer settings in Active Directory\n"
7721 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7722 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7723 "\n\tmigrates printers from remote to local server\n\n"));
7724 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7725 "\n\tmigrates printer-settings from remote to local server\n\n"));
7726 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7727 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7728 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7729 "\n\tmigrates printer-forms from remote to local server\n\n"));
7730 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7731 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7732 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7733 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7734 "\tremote to local print-server\n\n"));
7735 net_common_methods_usage(c, argc, argv);
7736 net_common_flags_usage(c, argc, argv);
7738 "\t-v or --verbose\t\t\tgive verbose output\n"
7739 "\t --destination\t\tmigration target server (default: localhost)\n"));
7745 * 'net rpc printer' entrypoint.
7747 * @param c A net_context structure.
7748 * @param argc Standard main() style argc.
7749 * @param argv Standard main() style argv. Initial components are already
7752 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7754 struct functable func[] = {
7759 N_("List all printers on print server"),
7760 N_("net rpc printer list\n"
7761 " List all printers on print server")
7765 rpc_printer_migrate,
7767 N_("Migrate printer to local server"),
7768 N_("net rpc printer migrate\n"
7769 " Migrate printer to local server")
7773 rpc_printer_driver_list,
7775 N_("List printer drivers"),
7776 N_("net rpc printer driver\n"
7777 " List printer drivers")
7781 rpc_printer_publish,
7783 N_("Publish printer in AD"),
7784 N_("net rpc printer publish\n"
7785 " Publish printer in AD")
7787 {NULL, NULL, 0, NULL, NULL}
7791 if (c->display_usage) {
7792 d_printf(_("Usage:\n"));
7793 d_printf(_("net rpc printer\n"
7794 " List printers\n"));
7795 net_display_usage_from_functable(func);
7798 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7799 rpc_printer_list_internals,
7803 return net_run_function(c, argc, argv, "net rpc printer", func);
7807 * 'net rpc' entrypoint.
7809 * @param c A net_context structure.
7810 * @param argc Standard main() style argc.
7811 * @param argv Standard main() style argv. Initial components are already
7815 int net_rpc(struct net_context *c, int argc, const char **argv)
7817 NET_API_STATUS status;
7819 struct functable func[] = {
7824 N_("Modify global audit settings"),
7825 N_("net rpc audit\n"
7826 " Modify global audit settings")
7832 N_("Show basic info about a domain"),
7834 " Show basic info about a domain")
7840 N_("Join a domain"),
7848 N_("Join a domain created in server manager"),
7849 N_("net rpc oldjoin\n"
7850 " Join a domain created in server manager")
7856 N_("Test that a join is valid"),
7857 N_("net rpc testjoin\n"
7858 " Test that a join is valid")
7864 N_("List/modify users"),
7866 " List/modify users")
7872 N_("Change a user password"),
7873 N_("net rpc password\n"
7874 " Change a user password\n"
7875 " Alias for net rpc user password")
7881 N_("List/modify groups"),
7882 N_("net rpc group\n"
7883 " List/modify groups")
7889 N_("List/modify shares"),
7890 N_("net rpc share\n"
7891 " List/modify shares")
7897 N_("List open files"),
7905 N_("List/modify printers"),
7906 N_("net rpc printer\n"
7907 " List/modify printers")
7911 net_rpc_changetrustpw,
7913 N_("Change trust account password"),
7914 N_("net rpc changetrustpw\n"
7915 " Change trust account password")
7921 N_("Modify domain trusts"),
7922 N_("net rpc trustdom\n"
7923 " Modify domain trusts")
7929 N_("Abort a remote shutdown"),
7930 N_("net rpc abortshutdown\n"
7931 " Abort a remote shutdown")
7937 N_("Shutdown a remote server"),
7938 N_("net rpc shutdown\n"
7939 " Shutdown a remote server")
7945 N_("Dump SAM data of remote NT PDC"),
7946 N_("net rpc samdump\n"
7947 " Dump SAM data of remote NT PDC")
7953 N_("Sync a remote NT PDC's data into local passdb"),
7954 N_("net rpc vampire\n"
7955 " Sync a remote NT PDC's data into local passdb")
7961 N_("Fetch the domain sid into local secrets.tdb"),
7962 N_("net rpc getsid\n"
7963 " Fetch the domain sid into local secrets.tdb")
7969 N_("Manage privileges assigned to SID"),
7970 N_("net rpc rights\n"
7971 " Manage privileges assigned to SID")
7977 N_("Start/stop/query remote services"),
7978 N_("net rpc service\n"
7979 " Start/stop/query remote services")
7985 N_("Manage registry hives"),
7986 N_("net rpc registry\n"
7987 " Manage registry hives")
7993 N_("Open interactive shell on remote server"),
7994 N_("net rpc shell\n"
7995 " Open interactive shell on remote server")
7997 {NULL, NULL, 0, NULL, NULL}
8000 status = libnetapi_net_init(&c->netapi_ctx);
8004 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8005 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8006 if (c->opt_kerberos) {
8007 libnetapi_set_use_kerberos(c->netapi_ctx);
8009 if (c->opt_ccache) {
8010 libnetapi_set_use_ccache(c->netapi_ctx);
8013 return net_run_function(c, argc, argv, "net rpc", func);