2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/cli_samr.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/ndr_lsa_c.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
32 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
33 #include "../librpc/gen_ndr/ndr_spoolss.h"
34 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
35 #include "../librpc/gen_ndr/ndr_winreg_c.h"
37 #include "lib/netapi/netapi.h"
38 #include "lib/netapi/netapi_net.h"
39 #include "rpc_client/init_lsa.h"
40 #include "../libcli/security/security.h"
42 static int net_mode_share;
43 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
48 * @brief RPC based subcommands for the 'net' utility.
50 * This file should contain much of the functionality that used to
51 * be found in rpcclient, execpt that the commands should change
52 * less often, and the fucntionality should be sane (the user is not
53 * expected to know a rid/sid before they conduct an operation etc.)
55 * @todo Perhaps eventually these should be split out into a number
56 * of files, as this could get quite big.
61 * Many of the RPC functions need the domain sid. This function gets
62 * it at the start of every run
64 * @param cli A cli_state already connected to the remote machine
66 * @return The Domain SID of the remote machine.
69 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
70 struct dom_sid **domain_sid,
71 const char **domain_name)
73 struct rpc_pipe_client *lsa_pipe = NULL;
74 struct policy_handle pol;
75 NTSTATUS status, result;
76 union lsa_PolicyInformation *info = NULL;
77 struct dcerpc_binding_handle *b;
79 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
81 if (!NT_STATUS_IS_OK(status)) {
82 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
86 b = lsa_pipe->binding_handle;
88 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
89 SEC_FLAG_MAXIMUM_ALLOWED,
91 if (!NT_STATUS_IS_OK(status)) {
92 d_fprintf(stderr, "open_policy %s: %s\n",
98 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
100 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
103 if (!NT_STATUS_IS_OK(status)) {
104 d_fprintf(stderr, "lsaquery %s: %s\n",
109 if (!NT_STATUS_IS_OK(result)) {
110 d_fprintf(stderr, "lsaquery %s: %s\n",
116 *domain_name = info->account_domain.name.string;
117 *domain_sid = info->account_domain.sid;
119 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
120 TALLOC_FREE(lsa_pipe);
126 * Run a single RPC command, from start to finish.
128 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
129 * @param conn_flag a NET_FLAG_ combination. Passed to
130 * net_make_ipc_connection.
131 * @param argc Standard main() style argc.
132 * @param argv Standard main() style argv. Initial components are already
134 * @return A shell status integer (0 for success).
137 int run_rpc_command(struct net_context *c,
138 struct cli_state *cli_arg,
139 const struct ndr_syntax_id *interface,
145 struct cli_state *cli = NULL;
146 struct rpc_pipe_client *pipe_hnd = NULL;
149 struct dom_sid *domain_sid;
150 const char *domain_name;
153 /* make use of cli_state handed over as an argument, if possible */
155 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
156 if (!NT_STATUS_IS_OK(nt_status)) {
157 DEBUG(1, ("failed to make ipc connection: %s\n",
158 nt_errstr(nt_status)));
171 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
172 DEBUG(0, ("talloc_init() failed\n"));
176 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
178 if (!NT_STATUS_IS_OK(nt_status)) {
182 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
183 if (lp_client_schannel()
184 && (ndr_syntax_id_equal(interface,
185 &ndr_table_netlogon.syntax_id))) {
186 /* Always try and create an schannel netlogon pipe. */
187 nt_status = cli_rpc_pipe_open_schannel(
188 cli, interface, NCACN_NP,
189 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
191 if (!NT_STATUS_IS_OK(nt_status)) {
192 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
193 nt_errstr(nt_status) ));
197 if (conn_flags & NET_FLAGS_SEAL) {
198 nt_status = cli_rpc_pipe_open_ntlmssp(
200 (conn_flags & NET_FLAGS_TCP) ?
201 NCACN_IP_TCP : NCACN_NP,
202 DCERPC_AUTH_LEVEL_PRIVACY,
203 lp_workgroup(), c->opt_user_name,
204 c->opt_password, &pipe_hnd);
206 nt_status = cli_rpc_pipe_open_noauth(
210 if (!NT_STATUS_IS_OK(nt_status)) {
211 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
212 get_pipe_name_from_syntax(
213 talloc_tos(), interface),
214 nt_errstr(nt_status) ));
220 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
222 if (!NT_STATUS_IS_OK(nt_status)) {
223 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
226 DEBUG(5, ("rpc command function succedded\n"));
229 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
231 TALLOC_FREE(pipe_hnd);
236 /* close the connection only if it was opened here */
241 talloc_destroy(mem_ctx);
246 * Force a change of the trust acccount password.
248 * All parameters are provided by the run_rpc_command function, except for
249 * argc, argv which are passed through.
251 * @param domain_sid The domain sid acquired from the remote server.
252 * @param cli A cli_state connected to the server.
253 * @param mem_ctx Talloc context, destroyed on completion of the function.
254 * @param argc Standard main() style argc.
255 * @param argv Standard main() style argv. Initial components are already
258 * @return Normal NTSTATUS return.
261 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
262 const struct dom_sid *domain_sid,
263 const char *domain_name,
264 struct cli_state *cli,
265 struct rpc_pipe_client *pipe_hnd,
272 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
273 if (!NT_STATUS_IS_OK(status)) {
274 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
283 * Force a change of the trust acccount password.
285 * @param argc Standard main() style argc.
286 * @param argv Standard main() style argv. Initial components are already
289 * @return A shell status integer (0 for success).
292 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
294 if (c->display_usage) {
296 "net rpc changetrustpw\n"
299 _("Change the machine trust password"));
303 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
304 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
305 rpc_changetrustpw_internals,
310 * Join a domain, the old way.
312 * This uses 'machinename' as the inital password, and changes it.
314 * The password should be created with 'server manager' or equiv first.
316 * All parameters are provided by the run_rpc_command function, except for
317 * argc, argv which are passed through.
319 * @param domain_sid The domain sid acquired from the remote server.
320 * @param cli A cli_state connected to the server.
321 * @param mem_ctx Talloc context, destroyed on completion of the function.
322 * @param argc Standard main() style argc.
323 * @param argv Standard main() style argv. Initial components are already
326 * @return Normal NTSTATUS return.
329 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
330 const struct dom_sid *domain_sid,
331 const char *domain_name,
332 struct cli_state *cli,
333 struct rpc_pipe_client *pipe_hnd,
339 fstring trust_passwd;
340 unsigned char orig_trust_passwd_hash[16];
342 enum netr_SchannelType sec_channel_type;
344 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
346 if (!NT_STATUS_IS_OK(result)) {
347 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
350 nt_errstr(result) ));
355 check what type of join - if the user want's to join as
356 a BDC, the server must agree that we are a BDC.
359 sec_channel_type = get_sec_channel_type(argv[0]);
361 sec_channel_type = get_sec_channel_type(NULL);
364 fstrcpy(trust_passwd, global_myname());
365 strlower_m(trust_passwd);
368 * Machine names can be 15 characters, but the max length on
369 * a password is 14. --jerry
372 trust_passwd[14] = '\0';
374 E_md4hash(trust_passwd, orig_trust_passwd_hash);
376 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
378 orig_trust_passwd_hash,
381 if (NT_STATUS_IS_OK(result))
382 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
385 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
386 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
387 result = NT_STATUS_UNSUCCESSFUL;
394 * Join a domain, the old way.
396 * @param argc Standard main() style argc.
397 * @param argv Standard main() style argv. Initial components are already
400 * @return A shell status integer (0 for success).
403 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
405 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
406 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
407 rpc_oldjoin_internals,
412 * Join a domain, the old way. This function exists to allow
413 * the message to be displayed when oldjoin was explicitly
414 * requested, but not when it was implied by "net rpc join".
416 * @param argc Standard main() style argc.
417 * @param argv Standard main() style argv. Initial components are already
420 * @return A shell status integer (0 for success).
423 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
427 if (c->display_usage) {
432 _("Join a domain the old way"));
436 rc = net_rpc_perform_oldjoin(c, argc, argv);
439 d_fprintf(stderr, _("Failed to join domain\n"));
446 * 'net rpc join' entrypoint.
447 * @param argc Standard main() style argc.
448 * @param argv Standard main() style argv. Initial components are already
451 * Main 'net_rpc_join()' (where the admin username/password is used) is
453 * Try to just change the password, but if that doesn't work, use/prompt
454 * for a username/password.
457 int net_rpc_join(struct net_context *c, int argc, const char **argv)
459 if (c->display_usage) {
462 _("net rpc join -U <username>[%%password] <type>\n"
464 " username\tName of the admin user"
465 " password\tPassword of the admin user, will "
466 "prompt if not specified\n"
467 " type\tCan be one of the following:\n"
468 "\t\tMEMBER\tJoin as member server (default)\n"
469 "\t\tBDC\tJoin as BDC\n"
470 "\t\tPDC\tJoin as PDC\n"));
474 if (lp_server_role() == ROLE_STANDALONE) {
475 d_printf(_("cannot join as standalone machine\n"));
479 if (strlen(global_myname()) > 15) {
480 d_printf(_("Our netbios name can be at most 15 chars long, "
481 "\"%s\" is %u chars long\n"),
482 global_myname(), (unsigned int)strlen(global_myname()));
486 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
489 return net_rpc_join_newstyle(c, argc, argv);
493 * display info about a rpc domain
495 * All parameters are provided by the run_rpc_command function, except for
496 * argc, argv which are passed through.
498 * @param domain_sid The domain sid acquired from the remote server
499 * @param cli A cli_state connected to the server.
500 * @param mem_ctx Talloc context, destroyed on completion of the function.
501 * @param argc Standard main() style argc.
502 * @param argv Standard main() style argv. Initial components are already
505 * @return Normal NTSTATUS return.
508 NTSTATUS rpc_info_internals(struct net_context *c,
509 const struct dom_sid *domain_sid,
510 const char *domain_name,
511 struct cli_state *cli,
512 struct rpc_pipe_client *pipe_hnd,
517 struct policy_handle connect_pol, domain_pol;
518 NTSTATUS status, result;
519 union samr_DomainInfo *info = NULL;
521 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
523 sid_to_fstring(sid_str, domain_sid);
525 /* Get sam policy handle */
526 status = dcerpc_samr_Connect2(b, mem_ctx,
528 MAXIMUM_ALLOWED_ACCESS,
531 if (!NT_STATUS_IS_OK(status)) {
532 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
537 if (!NT_STATUS_IS_OK(result)) {
539 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
544 /* Get domain policy handle */
545 status = dcerpc_samr_OpenDomain(b, mem_ctx,
547 MAXIMUM_ALLOWED_ACCESS,
548 CONST_DISCARD(struct dom_sid2 *, domain_sid),
551 if (!NT_STATUS_IS_OK(status)) {
552 d_fprintf(stderr, _("Could not open domain: %s\n"),
556 if (!NT_STATUS_IS_OK(result)) {
558 d_fprintf(stderr, _("Could not open domain: %s\n"),
563 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
568 if (!NT_STATUS_IS_OK(status)) {
572 if (NT_STATUS_IS_OK(result)) {
573 d_printf(_("Domain Name: %s\n"),
574 info->general.domain_name.string);
575 d_printf(_("Domain SID: %s\n"), sid_str);
576 d_printf(_("Sequence number: %llu\n"),
577 (unsigned long long)info->general.sequence_num);
578 d_printf(_("Num users: %u\n"), info->general.num_users);
579 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
580 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
588 * 'net rpc info' entrypoint.
589 * @param argc Standard main() style argc.
590 * @param argv Standard main() style argv. Initial components are already
594 int net_rpc_info(struct net_context *c, int argc, const char **argv)
596 if (c->display_usage) {
601 _("Display information about the domain"));
605 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
606 NET_FLAGS_PDC, rpc_info_internals,
611 * Fetch domain SID into the local secrets.tdb.
613 * All parameters are provided by the run_rpc_command function, except for
614 * argc, argv which are passed through.
616 * @param domain_sid The domain sid acquired from the remote server.
617 * @param cli A cli_state connected to the server.
618 * @param mem_ctx Talloc context, destroyed on completion of the function.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
623 * @return Normal NTSTATUS return.
626 static NTSTATUS rpc_getsid_internals(struct net_context *c,
627 const struct dom_sid *domain_sid,
628 const char *domain_name,
629 struct cli_state *cli,
630 struct rpc_pipe_client *pipe_hnd,
637 sid_to_fstring(sid_str, domain_sid);
638 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
639 sid_str, domain_name);
641 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
642 DEBUG(0,("Can't store domain SID\n"));
643 return NT_STATUS_UNSUCCESSFUL;
650 * 'net rpc getsid' entrypoint.
651 * @param argc Standard main() style argc.
652 * @param argv Standard main() style argv. Initial components are already
656 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
658 int conn_flags = NET_FLAGS_PDC;
660 if (!c->opt_user_specified) {
661 conn_flags |= NET_FLAGS_ANONYMOUS;
664 if (c->display_usage) {
669 _("Fetch domain SID into local secrets.tdb"));
673 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
675 rpc_getsid_internals,
679 /****************************************************************************/
682 * Basic usage function for 'net rpc user'.
683 * @param argc Standard main() style argc.
684 * @param argv Standard main() style argv. Initial components are already
688 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
690 return net_user_usage(c, argc, argv);
694 * Add a new user to a remote RPC server.
696 * @param argc Standard main() style argc.
697 * @param argv Standard main() style argv. Initial components are already
700 * @return A shell status integer (0 for success).
703 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
705 NET_API_STATUS status;
706 struct USER_INFO_1 info1;
707 uint32_t parm_error = 0;
709 if (argc < 1 || c->display_usage) {
710 rpc_user_usage(c, argc, argv);
716 info1.usri1_name = argv[0];
718 info1.usri1_password = argv[1];
721 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
724 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
725 argv[0], libnetapi_get_error_string(c->netapi_ctx,
729 d_printf(_("Added user '%s'.\n"), argv[0]);
736 * Rename a user on a remote RPC server.
738 * @param argc Standard main() style argc.
739 * @param argv Standard main() style argv. Initial components are already
742 * @return A shell status integer (0 for success).
745 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
747 NET_API_STATUS status;
748 struct USER_INFO_0 u0;
749 uint32_t parm_err = 0;
751 if (argc != 2 || c->display_usage) {
752 rpc_user_usage(c, argc, argv);
756 u0.usri0_name = argv[1];
758 status = NetUserSetInfo(c->opt_host, argv[0],
759 0, (uint8_t *)&u0, &parm_err);
762 _("Failed to rename user from %s to %s - %s\n"),
764 libnetapi_get_error_string(c->netapi_ctx, status));
766 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
773 * Set a user's primary group
775 * @param argc Standard main() style argc.
776 * @param argv Standard main() style argv. Initial components are already
779 * @return A shell status integer (0 for success).
782 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
785 NET_API_STATUS status;
787 struct GROUP_INFO_2 *g2;
788 struct USER_INFO_1051 u1051;
789 uint32_t parm_err = 0;
791 if (argc != 2 || c->display_usage) {
792 rpc_user_usage(c, argc, argv);
796 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
798 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
800 libnetapi_get_error_string(c->netapi_ctx, status));
803 g2 = (struct GROUP_INFO_2 *)buffer;
805 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
807 NetApiBufferFree(buffer);
809 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
810 (uint8_t *)&u1051, &parm_err);
813 _("Failed to set user's primary group %s to %s - "
814 "%s\n"), argv[0], argv[1],
815 libnetapi_get_error_string(c->netapi_ctx, status));
817 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
824 * Delete a user from a remote RPC server.
826 * @param argc Standard main() style argc.
827 * @param argv Standard main() style argv. Initial components are already
830 * @return A shell status integer (0 for success).
833 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
835 NET_API_STATUS status;
837 if (argc < 1 || c->display_usage) {
838 rpc_user_usage(c, argc, argv);
842 status = NetUserDel(c->opt_host, argv[0]);
845 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
847 libnetapi_get_error_string(c->netapi_ctx, status));
850 d_printf(_("Deleted user '%s'.\n"), argv[0]);
857 * Set a user's password on a remote RPC server.
859 * @param argc Standard main() style argc.
860 * @param argv Standard main() style argv. Initial components are already
863 * @return A shell status integer (0 for success).
866 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
868 NET_API_STATUS status;
870 struct USER_INFO_1003 u1003;
871 uint32_t parm_err = 0;
874 if (argc < 1 || c->display_usage) {
875 rpc_user_usage(c, argc, argv);
880 u1003.usri1003_password = argv[1];
882 ret = asprintf(&prompt, _("Enter new password for %s:"),
887 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
889 if (u1003.usri1003_password == NULL) {
894 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
896 /* Display results */
899 _("Failed to set password for '%s' with error: %s.\n"),
900 argv[0], libnetapi_get_error_string(c->netapi_ctx,
909 * List a user's groups from a remote RPC server.
911 * @param argc Standard main() style argc.
912 * @param argv Standard main() style argv. Initial components are already
915 * @return A shell status integer (0 for success)
918 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
921 NET_API_STATUS status;
922 struct GROUP_USERS_INFO_0 *u0 = NULL;
923 uint32_t entries_read = 0;
924 uint32_t total_entries = 0;
928 if (argc < 1 || c->display_usage) {
929 rpc_user_usage(c, argc, argv);
933 status = NetUserGetGroups(c->opt_host,
936 (uint8_t **)(void *)&u0,
942 _("Failed to get groups for '%s' with error: %s.\n"),
943 argv[0], libnetapi_get_error_string(c->netapi_ctx,
948 for (i=0; i < entries_read; i++) {
949 printf("%s\n", u0->grui0_name);
957 * List users on a remote RPC server.
959 * All parameters are provided by the run_rpc_command function, except for
960 * argc, argv which are passed through.
962 * @param domain_sid The domain sid acquired from the remote server.
963 * @param cli A cli_state connected to the server.
964 * @param mem_ctx Talloc context, destroyed on completion of the function.
965 * @param argc Standard main() style argc.
966 * @param argv Standard main() style argv. Initial components are already
969 * @return Normal NTSTATUS return.
972 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
974 NET_API_STATUS status;
975 uint32_t start_idx=0, num_entries, i, loop_count = 0;
976 struct NET_DISPLAY_USER *info = NULL;
979 /* Query domain users */
980 if (c->opt_long_list_entries)
981 d_printf(_("\nUser name Comment"
982 "\n-----------------------------\n"));
984 uint32_t max_entries, max_size;
986 dcerpc_get_query_dispinfo_params(
987 loop_count, &max_entries, &max_size);
989 status = NetQueryDisplayInformation(c->opt_host,
996 if (status != 0 && status != ERROR_MORE_DATA) {
1000 info = (struct NET_DISPLAY_USER *)buffer;
1002 for (i = 0; i < num_entries; i++) {
1004 if (c->opt_long_list_entries)
1005 printf("%-21.21s %s\n", info->usri1_name,
1006 info->usri1_comment);
1008 printf("%s\n", info->usri1_name);
1012 NetApiBufferFree(buffer);
1015 start_idx += num_entries;
1017 } while (status == ERROR_MORE_DATA);
1023 * 'net rpc user' entrypoint.
1024 * @param argc Standard main() style argc.
1025 * @param argv Standard main() style argv. Initial components are already
1029 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1031 NET_API_STATUS status;
1033 struct functable func[] = {
1038 N_("Add specified user"),
1039 N_("net rpc user add\n"
1040 " Add specified user")
1046 N_("List domain groups of user"),
1047 N_("net rpc user info\n"
1048 " List domain groups of user")
1054 N_("Remove specified user"),
1055 N_("net rpc user delete\n"
1056 " Remove specified user")
1062 N_("Change user password"),
1063 N_("net rpc user password\n"
1064 " Change user password")
1070 N_("Rename specified user"),
1071 N_("net rpc user rename\n"
1072 " Rename specified user")
1076 rpc_user_setprimarygroup,
1078 "Set a user's primary group",
1079 "net rpc user setprimarygroup\n"
1080 " Set a user's primary group"
1082 {NULL, NULL, 0, NULL, NULL}
1085 status = libnetapi_net_init(&c->netapi_ctx);
1089 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1090 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1091 if (c->opt_kerberos) {
1092 libnetapi_set_use_kerberos(c->netapi_ctx);
1096 if (c->display_usage) {
1101 _("List all users"));
1102 net_display_usage_from_functable(func);
1106 return rpc_user_list(c, argc, argv);
1109 return net_run_function(c, argc, argv, "net rpc user", func);
1112 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1113 TALLOC_CTX *mem_ctx,
1114 struct rpc_sh_ctx *ctx,
1115 struct rpc_pipe_client *pipe_hnd,
1116 int argc, const char **argv)
1118 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1121 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1122 TALLOC_CTX *mem_ctx,
1123 struct rpc_sh_ctx *ctx,
1124 struct rpc_pipe_client *pipe_hnd,
1125 int argc, const char **argv)
1127 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1130 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1131 TALLOC_CTX *mem_ctx,
1132 struct rpc_sh_ctx *ctx,
1133 struct rpc_pipe_client *pipe_hnd,
1134 int argc, const char **argv,
1136 struct net_context *c,
1137 TALLOC_CTX *mem_ctx,
1138 struct rpc_sh_ctx *ctx,
1139 struct rpc_pipe_client *pipe_hnd,
1140 struct policy_handle *user_hnd,
1141 int argc, const char **argv))
1143 struct policy_handle connect_pol, domain_pol, user_pol;
1144 NTSTATUS status, result;
1147 enum lsa_SidType type;
1148 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1151 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1153 return NT_STATUS_INVALID_PARAMETER;
1156 ZERO_STRUCT(connect_pol);
1157 ZERO_STRUCT(domain_pol);
1158 ZERO_STRUCT(user_pol);
1160 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1161 argv[0], NULL, NULL, &sid, &type);
1162 if (!NT_STATUS_IS_OK(status)) {
1163 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1168 if (type != SID_NAME_USER) {
1169 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1170 sid_type_lookup(type));
1171 status = NT_STATUS_NO_SUCH_USER;
1175 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1176 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1177 status = NT_STATUS_NO_SUCH_USER;
1181 status = dcerpc_samr_Connect2(b, mem_ctx,
1183 MAXIMUM_ALLOWED_ACCESS,
1186 if (!NT_STATUS_IS_OK(status)) {
1189 if (!NT_STATUS_IS_OK(result)) {
1194 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1196 MAXIMUM_ALLOWED_ACCESS,
1200 if (!NT_STATUS_IS_OK(status)) {
1203 if (!NT_STATUS_IS_OK(result)) {
1208 status = dcerpc_samr_OpenUser(b, mem_ctx,
1210 MAXIMUM_ALLOWED_ACCESS,
1214 if (!NT_STATUS_IS_OK(status)) {
1217 if (!NT_STATUS_IS_OK(result)) {
1222 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1225 if (is_valid_policy_hnd(&user_pol)) {
1226 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1228 if (is_valid_policy_hnd(&domain_pol)) {
1229 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1231 if (is_valid_policy_hnd(&connect_pol)) {
1232 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1237 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1238 TALLOC_CTX *mem_ctx,
1239 struct rpc_sh_ctx *ctx,
1240 struct rpc_pipe_client *pipe_hnd,
1241 struct policy_handle *user_hnd,
1242 int argc, const char **argv)
1244 NTSTATUS status, result;
1245 union samr_UserInfo *info = NULL;
1246 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1249 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1251 return NT_STATUS_INVALID_PARAMETER;
1254 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1259 if (!NT_STATUS_IS_OK(status)) {
1262 if (!NT_STATUS_IS_OK(result)) {
1266 d_printf(_("user rid: %d, group rid: %d\n"),
1268 info->info21.primary_gid);
1273 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1274 TALLOC_CTX *mem_ctx,
1275 struct rpc_sh_ctx *ctx,
1276 struct rpc_pipe_client *pipe_hnd,
1277 int argc, const char **argv)
1279 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1280 rpc_sh_user_show_internals);
1283 #define FETCHSTR(name, rec) \
1284 do { if (strequal(ctx->thiscmd, name)) { \
1285 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1288 #define SETSTR(name, rec, flag) \
1289 do { if (strequal(ctx->thiscmd, name)) { \
1290 init_lsa_String(&(info->info21.rec), argv[0]); \
1291 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1294 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1295 TALLOC_CTX *mem_ctx,
1296 struct rpc_sh_ctx *ctx,
1297 struct rpc_pipe_client *pipe_hnd,
1298 struct policy_handle *user_hnd,
1299 int argc, const char **argv)
1301 NTSTATUS status, result;
1302 const char *username;
1303 const char *oldval = "";
1304 union samr_UserInfo *info = NULL;
1305 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1308 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1309 _("Usage:"), ctx->whoami);
1310 return NT_STATUS_INVALID_PARAMETER;
1313 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1318 if (!NT_STATUS_IS_OK(status)) {
1321 if (!NT_STATUS_IS_OK(result)) {
1325 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1327 FETCHSTR("fullname", full_name);
1328 FETCHSTR("homedir", home_directory);
1329 FETCHSTR("homedrive", home_drive);
1330 FETCHSTR("logonscript", logon_script);
1331 FETCHSTR("profilepath", profile_path);
1332 FETCHSTR("description", description);
1335 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1339 if (strcmp(argv[0], "NULL") == 0) {
1343 ZERO_STRUCT(info->info21);
1345 SETSTR("fullname", full_name, FULL_NAME);
1346 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1347 SETSTR("homedrive", home_drive, HOME_DRIVE);
1348 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1349 SETSTR("profilepath", profile_path, PROFILE_PATH);
1350 SETSTR("description", description, DESCRIPTION);
1352 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1357 if (!NT_STATUS_IS_OK(status)) {
1363 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1364 ctx->thiscmd, oldval, argv[0]);
1371 #define HANDLEFLG(name, rec) \
1372 do { if (strequal(ctx->thiscmd, name)) { \
1373 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1375 newflags = oldflags | ACB_##rec; \
1377 newflags = oldflags & ~ACB_##rec; \
1380 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1381 TALLOC_CTX *mem_ctx,
1382 struct rpc_sh_ctx *ctx,
1383 struct rpc_pipe_client *pipe_hnd,
1384 int argc, const char **argv)
1386 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1387 rpc_sh_user_str_edit_internals);
1390 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1391 TALLOC_CTX *mem_ctx,
1392 struct rpc_sh_ctx *ctx,
1393 struct rpc_pipe_client *pipe_hnd,
1394 struct policy_handle *user_hnd,
1395 int argc, const char **argv)
1397 NTSTATUS status, result;
1398 const char *username;
1399 const char *oldval = "unknown";
1400 uint32 oldflags, newflags;
1402 union samr_UserInfo *info = NULL;
1403 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1406 ((argc == 1) && !strequal(argv[0], "yes") &&
1407 !strequal(argv[0], "no"))) {
1408 /* TRANSATORS: The yes|no here are program keywords. Please do
1410 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1412 return NT_STATUS_INVALID_PARAMETER;
1415 newval = strequal(argv[0], "yes");
1417 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1422 if (!NT_STATUS_IS_OK(status)) {
1425 if (!NT_STATUS_IS_OK(result)) {
1429 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1430 oldflags = info->info21.acct_flags;
1431 newflags = info->info21.acct_flags;
1433 HANDLEFLG("disabled", DISABLED);
1434 HANDLEFLG("pwnotreq", PWNOTREQ);
1435 HANDLEFLG("autolock", AUTOLOCK);
1436 HANDLEFLG("pwnoexp", PWNOEXP);
1439 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1444 ZERO_STRUCT(info->info21);
1446 info->info21.acct_flags = newflags;
1447 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1449 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1454 if (!NT_STATUS_IS_OK(status)) {
1458 if (NT_STATUS_IS_OK(result)) {
1459 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1460 ctx->thiscmd, oldval, argv[0]);
1468 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1469 TALLOC_CTX *mem_ctx,
1470 struct rpc_sh_ctx *ctx,
1471 struct rpc_pipe_client *pipe_hnd,
1472 int argc, const char **argv)
1474 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1475 rpc_sh_user_flag_edit_internals);
1478 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1479 TALLOC_CTX *mem_ctx,
1480 struct rpc_sh_ctx *ctx)
1482 static struct rpc_sh_cmd cmds[] = {
1484 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1485 N_("Show/Set a user's full name") },
1487 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1488 N_("Show/Set a user's home directory") },
1490 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1491 N_("Show/Set a user's home drive") },
1493 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1494 N_("Show/Set a user's logon script") },
1496 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1497 N_("Show/Set a user's profile path") },
1499 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1500 N_("Show/Set a user's description") },
1502 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1503 N_("Show/Set whether a user is disabled") },
1505 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1506 N_("Show/Set whether a user locked out") },
1508 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1509 N_("Show/Set whether a user does not need a password") },
1511 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1512 N_("Show/Set whether a user's password does not expire") },
1514 { NULL, NULL, 0, NULL, NULL }
1520 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1521 TALLOC_CTX *mem_ctx,
1522 struct rpc_sh_ctx *ctx)
1524 static struct rpc_sh_cmd cmds[] = {
1526 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1527 N_("List available users") },
1529 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1530 N_("List the domain groups a user is member of") },
1532 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1533 N_("Show info about a user") },
1535 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1536 N_("Show/Modify a user's fields") },
1538 { NULL, NULL, 0, NULL, NULL }
1544 /****************************************************************************/
1547 * Basic usage function for 'net rpc group'.
1548 * @param argc Standard main() style argc.
1549 * @param argv Standard main() style argv. Initial components are already
1553 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1555 return net_group_usage(c, argc, argv);
1559 * Delete group on a remote RPC server.
1561 * All parameters are provided by the run_rpc_command function, except for
1562 * argc, argv which are passed through.
1564 * @param domain_sid The domain sid acquired from the remote server.
1565 * @param cli A cli_state connected to the server.
1566 * @param mem_ctx Talloc context, destroyed on completion of the function.
1567 * @param argc Standard main() style argc.
1568 * @param argv Standard main() style argv. Initial components are already
1571 * @return Normal NTSTATUS return.
1574 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1575 const struct dom_sid *domain_sid,
1576 const char *domain_name,
1577 struct cli_state *cli,
1578 struct rpc_pipe_client *pipe_hnd,
1579 TALLOC_CTX *mem_ctx,
1583 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1584 bool group_is_primary = false;
1585 NTSTATUS status, result;
1587 struct samr_RidAttrArray *rids = NULL;
1590 /* struct samr_RidWithAttribute *user_gids; */
1591 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1593 struct samr_Ids group_rids, name_types;
1594 struct lsa_String lsa_acct_name;
1595 union samr_UserInfo *info = NULL;
1597 if (argc < 1 || c->display_usage) {
1598 rpc_group_usage(c, argc,argv);
1599 return NT_STATUS_OK; /* ok? */
1602 status = dcerpc_samr_Connect2(b, mem_ctx,
1604 MAXIMUM_ALLOWED_ACCESS,
1607 if (!NT_STATUS_IS_OK(status)) {
1608 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1612 if (!NT_STATUS_IS_OK(result)) {
1614 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1618 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1620 MAXIMUM_ALLOWED_ACCESS,
1621 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1624 if (!NT_STATUS_IS_OK(status)) {
1625 d_fprintf(stderr, _("Request open_domain failed\n"));
1629 if (!NT_STATUS_IS_OK(result)) {
1631 d_fprintf(stderr, _("Request open_domain failed\n"));
1635 init_lsa_String(&lsa_acct_name, argv[0]);
1637 status = dcerpc_samr_LookupNames(b, mem_ctx,
1644 if (!NT_STATUS_IS_OK(status)) {
1645 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1649 if (!NT_STATUS_IS_OK(result)) {
1651 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1655 switch (name_types.ids[0])
1657 case SID_NAME_DOM_GRP:
1658 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1660 MAXIMUM_ALLOWED_ACCESS,
1664 if (!NT_STATUS_IS_OK(status)) {
1665 d_fprintf(stderr, _("Request open_group failed"));
1669 if (!NT_STATUS_IS_OK(result)) {
1671 d_fprintf(stderr, _("Request open_group failed"));
1675 group_rid = group_rids.ids[0];
1677 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1681 if (!NT_STATUS_IS_OK(status)) {
1683 _("Unable to query group members of %s"),
1688 if (!NT_STATUS_IS_OK(result)) {
1691 _("Unable to query group members of %s"),
1696 if (c->opt_verbose) {
1698 _("Domain Group %s (rid: %d) has %d members\n"),
1699 argv[0],group_rid, rids->count);
1702 /* Check if group is anyone's primary group */
1703 for (i = 0; i < rids->count; i++)
1705 status = dcerpc_samr_OpenUser(b, mem_ctx,
1707 MAXIMUM_ALLOWED_ACCESS,
1711 if (!NT_STATUS_IS_OK(status)) {
1713 _("Unable to open group member %d\n"),
1718 if (!NT_STATUS_IS_OK(result)) {
1721 _("Unable to open group member %d\n"),
1726 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1731 if (!NT_STATUS_IS_OK(status)) {
1733 _("Unable to lookup userinfo for group "
1739 if (!NT_STATUS_IS_OK(result)) {
1742 _("Unable to lookup userinfo for group "
1748 if (info->info21.primary_gid == group_rid) {
1749 if (c->opt_verbose) {
1750 d_printf(_("Group is primary group "
1752 info->info21.account_name.string);
1754 group_is_primary = true;
1757 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1760 if (group_is_primary) {
1761 d_fprintf(stderr, _("Unable to delete group because "
1762 "some of it's members have it as primary "
1764 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1768 /* remove all group members */
1769 for (i = 0; i < rids->count; i++)
1772 d_printf(_("Remove group member %d..."),
1774 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1778 if (!NT_STATUS_IS_OK(status)) {
1782 if (NT_STATUS_IS_OK(result)) {
1784 d_printf(_("ok\n"));
1787 d_printf("%s\n", _("failed"));
1792 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1795 if (!NT_STATUS_IS_OK(status)) {
1802 /* removing a local group is easier... */
1803 case SID_NAME_ALIAS:
1804 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1806 MAXIMUM_ALLOWED_ACCESS,
1810 if (!NT_STATUS_IS_OK(status)) {
1811 d_fprintf(stderr, _("Request open_alias failed\n"));
1814 if (!NT_STATUS_IS_OK(result)) {
1816 d_fprintf(stderr, _("Request open_alias failed\n"));
1820 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1823 if (!NT_STATUS_IS_OK(status)) {
1831 d_fprintf(stderr, _("%s is of type %s. This command is only "
1832 "for deleting local or global groups\n"),
1833 argv[0],sid_type_lookup(name_types.ids[0]));
1834 status = NT_STATUS_UNSUCCESSFUL;
1838 if (NT_STATUS_IS_OK(status)) {
1840 d_printf(_("Deleted %s '%s'\n"),
1841 sid_type_lookup(name_types.ids[0]), argv[0]);
1843 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1844 get_friendly_nt_error_msg(status));
1852 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1854 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1855 rpc_group_delete_internals, argc,argv);
1858 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1860 NET_API_STATUS status;
1861 struct GROUP_INFO_1 info1;
1862 uint32_t parm_error = 0;
1864 if (argc != 1 || c->display_usage) {
1865 rpc_group_usage(c, argc, argv);
1871 info1.grpi1_name = argv[0];
1872 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1873 info1.grpi1_comment = c->opt_comment;
1876 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1880 _("Failed to add group '%s' with error: %s.\n"),
1881 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1885 d_printf(_("Added group '%s'.\n"), argv[0]);
1891 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1893 NET_API_STATUS status;
1894 struct LOCALGROUP_INFO_1 info1;
1895 uint32_t parm_error = 0;
1897 if (argc != 1 || c->display_usage) {
1898 rpc_group_usage(c, argc, argv);
1904 info1.lgrpi1_name = argv[0];
1905 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1906 info1.lgrpi1_comment = c->opt_comment;
1909 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1913 _("Failed to add alias '%s' with error: %s.\n"),
1914 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1918 d_printf(_("Added alias '%s'.\n"), argv[0]);
1924 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1926 if (c->opt_localgroup)
1927 return rpc_alias_add_internals(c, argc, argv);
1929 return rpc_group_add_internals(c, argc, argv);
1932 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1933 TALLOC_CTX *mem_ctx,
1935 struct dom_sid *sid,
1936 enum lsa_SidType *type)
1938 struct dom_sid *sids = NULL;
1939 enum lsa_SidType *types = NULL;
1940 struct rpc_pipe_client *pipe_hnd = NULL;
1941 struct policy_handle lsa_pol;
1942 NTSTATUS status, result;
1943 struct dcerpc_binding_handle *b;
1945 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1947 if (!NT_STATUS_IS_OK(status)) {
1951 b = pipe_hnd->binding_handle;
1953 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1954 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1956 if (!NT_STATUS_IS_OK(status)) {
1960 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1961 &name, NULL, 1, &sids, &types);
1963 if (NT_STATUS_IS_OK(status)) {
1964 sid_copy(sid, &sids[0]);
1968 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1972 TALLOC_FREE(pipe_hnd);
1975 if (!NT_STATUS_IS_OK(status) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1977 /* Try as S-1-5-whatever */
1979 struct dom_sid tmp_sid;
1981 if (string_to_sid(&tmp_sid, name)) {
1982 sid_copy(sid, &tmp_sid);
1983 *type = SID_NAME_UNKNOWN;
1984 status = NT_STATUS_OK;
1991 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1992 TALLOC_CTX *mem_ctx,
1993 const struct dom_sid *group_sid,
1996 struct policy_handle connect_pol, domain_pol;
1997 NTSTATUS status, result;
1999 struct policy_handle group_pol;
2000 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2002 struct samr_Ids rids, rid_types;
2003 struct lsa_String lsa_acct_name;
2007 sid_copy(&sid, group_sid);
2009 if (!sid_split_rid(&sid, &group_rid)) {
2010 return NT_STATUS_UNSUCCESSFUL;
2013 /* Get sam policy handle */
2014 status = dcerpc_samr_Connect2(b, mem_ctx,
2016 MAXIMUM_ALLOWED_ACCESS,
2019 if (!NT_STATUS_IS_OK(status)) {
2022 if (!NT_STATUS_IS_OK(result)) {
2026 /* Get domain policy handle */
2027 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2029 MAXIMUM_ALLOWED_ACCESS,
2033 if (!NT_STATUS_IS_OK(status)) {
2036 if (!NT_STATUS_IS_OK(result)) {
2040 init_lsa_String(&lsa_acct_name, member);
2042 status = dcerpc_samr_LookupNames(b, mem_ctx,
2049 if (!NT_STATUS_IS_OK(status)) {
2050 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2055 if (!NT_STATUS_IS_OK(result)) {
2057 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2062 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2064 MAXIMUM_ALLOWED_ACCESS,
2068 if (!NT_STATUS_IS_OK(status)) {
2072 if (!NT_STATUS_IS_OK(result)) {
2077 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2080 0x0005, /* unknown flags */
2082 if (!NT_STATUS_IS_OK(status)) {
2089 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2093 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2094 TALLOC_CTX *mem_ctx,
2095 const struct dom_sid *alias_sid,
2098 struct policy_handle connect_pol, domain_pol;
2099 NTSTATUS status, result;
2101 struct policy_handle alias_pol;
2102 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2104 struct dom_sid member_sid;
2105 enum lsa_SidType member_type;
2109 sid_copy(&sid, alias_sid);
2111 if (!sid_split_rid(&sid, &alias_rid)) {
2112 return NT_STATUS_UNSUCCESSFUL;
2115 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2116 member, &member_sid, &member_type);
2118 if (!NT_STATUS_IS_OK(result)) {
2119 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2124 /* Get sam policy handle */
2125 status = dcerpc_samr_Connect2(b, mem_ctx,
2127 MAXIMUM_ALLOWED_ACCESS,
2130 if (!NT_STATUS_IS_OK(status)) {
2133 if (!NT_STATUS_IS_OK(result)) {
2138 /* Get domain policy handle */
2139 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2141 MAXIMUM_ALLOWED_ACCESS,
2145 if (!NT_STATUS_IS_OK(status)) {
2148 if (!NT_STATUS_IS_OK(result)) {
2153 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2155 MAXIMUM_ALLOWED_ACCESS,
2159 if (!NT_STATUS_IS_OK(status)) {
2162 if (!NT_STATUS_IS_OK(result)) {
2166 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2170 if (!NT_STATUS_IS_OK(status)) {
2177 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2181 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2182 const struct dom_sid *domain_sid,
2183 const char *domain_name,
2184 struct cli_state *cli,
2185 struct rpc_pipe_client *pipe_hnd,
2186 TALLOC_CTX *mem_ctx,
2190 struct dom_sid group_sid;
2191 enum lsa_SidType group_type;
2193 if (argc != 2 || c->display_usage) {
2196 _("net rpc group addmem <group> <member>\n"
2197 " Add a member to a group\n"
2198 " group\tGroup to add member to\n"
2199 " member\tMember to add to group\n"));
2200 return NT_STATUS_UNSUCCESSFUL;
2203 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2204 &group_sid, &group_type))) {
2205 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2207 return NT_STATUS_UNSUCCESSFUL;
2210 if (group_type == SID_NAME_DOM_GRP) {
2211 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2212 &group_sid, argv[1]);
2214 if (!NT_STATUS_IS_OK(result)) {
2215 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2216 argv[1], argv[0], nt_errstr(result));
2221 if (group_type == SID_NAME_ALIAS) {
2222 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2223 &group_sid, argv[1]);
2225 if (!NT_STATUS_IS_OK(result)) {
2226 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2227 argv[1], argv[0], nt_errstr(result));
2232 d_fprintf(stderr, _("Can only add members to global or local groups "
2233 "which %s is not\n"), argv[0]);
2235 return NT_STATUS_UNSUCCESSFUL;
2238 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2240 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2241 rpc_group_addmem_internals,
2245 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2246 struct rpc_pipe_client *pipe_hnd,
2247 TALLOC_CTX *mem_ctx,
2248 const struct dom_sid *group_sid,
2251 struct policy_handle connect_pol, domain_pol;
2252 NTSTATUS status, result;
2254 struct policy_handle group_pol;
2255 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2257 struct samr_Ids rids, rid_types;
2258 struct lsa_String lsa_acct_name;
2262 sid_copy(&sid, group_sid);
2264 if (!sid_split_rid(&sid, &group_rid))
2265 return NT_STATUS_UNSUCCESSFUL;
2267 /* Get sam policy handle */
2268 status = dcerpc_samr_Connect2(b, mem_ctx,
2270 MAXIMUM_ALLOWED_ACCESS,
2273 if (!NT_STATUS_IS_OK(status)) {
2276 if (!NT_STATUS_IS_OK(result)) {
2281 /* Get domain policy handle */
2282 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2284 MAXIMUM_ALLOWED_ACCESS,
2288 if (!NT_STATUS_IS_OK(status)) {
2291 if (!NT_STATUS_IS_OK(result)) {
2295 init_lsa_String(&lsa_acct_name, member);
2297 status = dcerpc_samr_LookupNames(b, mem_ctx,
2304 if (!NT_STATUS_IS_OK(status)) {
2305 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2310 if (!NT_STATUS_IS_OK(result)) {
2312 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2317 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2319 MAXIMUM_ALLOWED_ACCESS,
2323 if (!NT_STATUS_IS_OK(status)) {
2326 if (!NT_STATUS_IS_OK(result)) {
2331 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2335 if (!NT_STATUS_IS_OK(status)) {
2341 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2345 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2346 TALLOC_CTX *mem_ctx,
2347 const struct dom_sid *alias_sid,
2350 struct policy_handle connect_pol, domain_pol;
2351 NTSTATUS status, result;
2353 struct policy_handle alias_pol;
2354 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2356 struct dom_sid member_sid;
2357 enum lsa_SidType member_type;
2361 sid_copy(&sid, alias_sid);
2363 if (!sid_split_rid(&sid, &alias_rid))
2364 return NT_STATUS_UNSUCCESSFUL;
2366 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2367 member, &member_sid, &member_type);
2369 if (!NT_STATUS_IS_OK(result)) {
2370 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2375 /* Get sam policy handle */
2376 status = dcerpc_samr_Connect2(b, mem_ctx,
2378 MAXIMUM_ALLOWED_ACCESS,
2381 if (!NT_STATUS_IS_OK(status)) {
2384 if (!NT_STATUS_IS_OK(result)) {
2389 /* Get domain policy handle */
2390 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2392 MAXIMUM_ALLOWED_ACCESS,
2396 if (!NT_STATUS_IS_OK(status)) {
2399 if (!NT_STATUS_IS_OK(result)) {
2404 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2406 MAXIMUM_ALLOWED_ACCESS,
2410 if (!NT_STATUS_IS_OK(status)) {
2414 if (!NT_STATUS_IS_OK(result)) {
2418 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2423 if (!NT_STATUS_IS_OK(status)) {
2430 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2434 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2435 const struct dom_sid *domain_sid,
2436 const char *domain_name,
2437 struct cli_state *cli,
2438 struct rpc_pipe_client *pipe_hnd,
2439 TALLOC_CTX *mem_ctx,
2443 struct dom_sid group_sid;
2444 enum lsa_SidType group_type;
2446 if (argc != 2 || c->display_usage) {
2449 _("net rpc group delmem <group> <member>\n"
2450 " Delete a member from a group\n"
2451 " group\tGroup to delete member from\n"
2452 " member\tMember to delete from group\n"));
2453 return NT_STATUS_UNSUCCESSFUL;
2456 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2457 &group_sid, &group_type))) {
2458 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2460 return NT_STATUS_UNSUCCESSFUL;
2463 if (group_type == SID_NAME_DOM_GRP) {
2464 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2465 &group_sid, argv[1]);
2467 if (!NT_STATUS_IS_OK(result)) {
2468 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2469 argv[1], argv[0], nt_errstr(result));
2474 if (group_type == SID_NAME_ALIAS) {
2475 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2476 &group_sid, argv[1]);
2478 if (!NT_STATUS_IS_OK(result)) {
2479 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2480 argv[1], argv[0], nt_errstr(result));
2485 d_fprintf(stderr, _("Can only delete members from global or local "
2486 "groups which %s is not\n"), argv[0]);
2488 return NT_STATUS_UNSUCCESSFUL;
2491 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2493 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2494 rpc_group_delmem_internals,
2499 * List groups on a remote RPC server.
2501 * All parameters are provided by the run_rpc_command function, except for
2502 * argc, argv which are passes through.
2504 * @param domain_sid The domain sid acquired from the remote server.
2505 * @param cli A cli_state connected to the server.
2506 * @param mem_ctx Talloc context, destroyed on completion of the function.
2507 * @param argc Standard main() style argc.
2508 * @param argv Standard main() style argv. Initial components are already
2511 * @return Normal NTSTATUS return.
2514 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2515 const struct dom_sid *domain_sid,
2516 const char *domain_name,
2517 struct cli_state *cli,
2518 struct rpc_pipe_client *pipe_hnd,
2519 TALLOC_CTX *mem_ctx,
2523 struct policy_handle connect_pol, domain_pol;
2524 NTSTATUS status, result;
2525 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2526 struct samr_SamArray *groups = NULL;
2527 bool global = false;
2529 bool builtin = false;
2530 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2532 if (c->display_usage) {
2535 _("net rpc group list [global] [local] [builtin]\n"
2536 " List groups on RPC server\n"
2537 " global\tList global groups\n"
2538 " local\tList local groups\n"
2539 " builtin\tList builtin groups\n"
2540 " If none of global, local or builtin is "
2541 "specified, all three options are considered "
2543 return NT_STATUS_OK;
2552 for (i=0; i<argc; i++) {
2553 if (strequal(argv[i], "global"))
2556 if (strequal(argv[i], "local"))
2559 if (strequal(argv[i], "builtin"))
2563 /* Get sam policy handle */
2565 status = dcerpc_samr_Connect2(b, mem_ctx,
2567 MAXIMUM_ALLOWED_ACCESS,
2570 if (!NT_STATUS_IS_OK(status)) {
2573 if (!NT_STATUS_IS_OK(result)) {
2578 /* Get domain policy handle */
2580 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2582 MAXIMUM_ALLOWED_ACCESS,
2583 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2586 if (!NT_STATUS_IS_OK(status)) {
2589 if (!NT_STATUS_IS_OK(result)) {
2594 /* Query domain groups */
2595 if (c->opt_long_list_entries)
2596 d_printf(_("\nGroup name Comment"
2597 "\n-----------------------------\n"));
2599 uint32_t max_size, total_size, returned_size;
2600 union samr_DispInfo info;
2604 dcerpc_get_query_dispinfo_params(
2605 loop_count, &max_entries, &max_size);
2607 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2617 if (!NT_STATUS_IS_OK(status)) {
2620 num_entries = info.info3.count;
2621 start_idx += info.info3.count;
2623 if (!NT_STATUS_IS_OK(result) &&
2624 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2627 for (i = 0; i < num_entries; i++) {
2629 const char *group = NULL;
2630 const char *desc = NULL;
2632 group = info.info3.entries[i].account_name.string;
2633 desc = info.info3.entries[i].description.string;
2635 if (c->opt_long_list_entries)
2636 printf("%-21.21s %-50.50s\n",
2639 printf("%s\n", group);
2641 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2642 /* query domain aliases */
2647 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2654 if (!NT_STATUS_IS_OK(status)) {
2657 if (!NT_STATUS_IS_OK(result) &&
2658 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2661 for (i = 0; i < num_entries; i++) {
2663 const char *description = NULL;
2665 if (c->opt_long_list_entries) {
2667 struct policy_handle alias_pol;
2668 union samr_AliasInfo *info = NULL;
2671 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2674 groups->entries[i].idx,
2677 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2678 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2683 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2684 status = dcerpc_samr_Close(b, mem_ctx,
2687 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2688 description = info->description.string;
2694 if (description != NULL) {
2695 printf("%-21.21s %-50.50s\n",
2696 groups->entries[i].name.string,
2699 printf("%s\n", groups->entries[i].name.string);
2702 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2703 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2704 /* Get builtin policy handle */
2706 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2708 MAXIMUM_ALLOWED_ACCESS,
2709 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2712 if (!NT_STATUS_IS_OK(status)) {
2715 if (!NT_STATUS_IS_OK(result)) {
2720 /* query builtin aliases */
2723 if (!builtin) break;
2725 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2732 if (!NT_STATUS_IS_OK(status)) {
2735 if (!NT_STATUS_IS_OK(result) &&
2736 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2741 for (i = 0; i < num_entries; i++) {
2743 const char *description = NULL;
2745 if (c->opt_long_list_entries) {
2747 struct policy_handle alias_pol;
2748 union samr_AliasInfo *info = NULL;
2751 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2754 groups->entries[i].idx,
2757 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2758 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2763 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2764 status = dcerpc_samr_Close(b, mem_ctx,
2767 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2768 description = info->description.string;
2774 if (description != NULL) {
2775 printf("%-21.21s %-50.50s\n",
2776 groups->entries[i].name.string,
2779 printf("%s\n", groups->entries[i].name.string);
2782 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2790 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2792 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2793 rpc_group_list_internals,
2797 static NTSTATUS rpc_list_group_members(struct net_context *c,
2798 struct rpc_pipe_client *pipe_hnd,
2799 TALLOC_CTX *mem_ctx,
2800 const char *domain_name,
2801 const struct dom_sid *domain_sid,
2802 struct policy_handle *domain_pol,
2805 NTSTATUS result, status;
2806 struct policy_handle group_pol;
2807 uint32 num_members, *group_rids;
2809 struct samr_RidAttrArray *rids = NULL;
2810 struct lsa_Strings names;
2811 struct samr_Ids types;
2812 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2815 sid_to_fstring(sid_str, domain_sid);
2817 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2819 MAXIMUM_ALLOWED_ACCESS,
2823 if (!NT_STATUS_IS_OK(status)) {
2826 if (!NT_STATUS_IS_OK(result)) {
2830 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
2834 if (!NT_STATUS_IS_OK(status)) {
2837 if (!NT_STATUS_IS_OK(result)) {
2841 num_members = rids->count;
2842 group_rids = rids->rids;
2844 while (num_members > 0) {
2845 int this_time = 512;
2847 if (num_members < this_time)
2848 this_time = num_members;
2850 status = dcerpc_samr_LookupRids(b, mem_ctx,
2857 if (!NT_STATUS_IS_OK(status)) {
2860 if (!NT_STATUS_IS_OK(result)) {
2864 /* We only have users as members, but make the output
2865 the same as the output of alias members */
2867 for (i = 0; i < this_time; i++) {
2869 if (c->opt_long_list_entries) {
2870 printf("%s-%d %s\\%s %d\n", sid_str,
2871 group_rids[i], domain_name,
2872 names.names[i].string,
2875 printf("%s\\%s\n", domain_name,
2876 names.names[i].string);
2880 num_members -= this_time;
2884 return NT_STATUS_OK;
2887 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2888 struct rpc_pipe_client *pipe_hnd,
2889 TALLOC_CTX *mem_ctx,
2890 struct policy_handle *domain_pol,
2893 NTSTATUS result, status;
2894 struct rpc_pipe_client *lsa_pipe;
2895 struct policy_handle alias_pol, lsa_pol;
2897 struct dom_sid *alias_sids;
2900 enum lsa_SidType *types;
2902 struct lsa_SidArray sid_array;
2903 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2905 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2907 MAXIMUM_ALLOWED_ACCESS,
2911 if (!NT_STATUS_IS_OK(status)) {
2914 if (!NT_STATUS_IS_OK(result)) {
2918 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
2922 if (!NT_STATUS_IS_OK(status)) {
2923 d_fprintf(stderr, _("Couldn't list alias members\n"));
2926 if (!NT_STATUS_IS_OK(result)) {
2927 d_fprintf(stderr, _("Couldn't list alias members\n"));
2931 num_members = sid_array.num_sids;
2933 if (num_members == 0) {
2934 return NT_STATUS_OK;
2937 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2938 &ndr_table_lsarpc.syntax_id,
2940 if (!NT_STATUS_IS_OK(result)) {
2941 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2942 nt_errstr(result) );
2946 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2947 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2949 if (!NT_STATUS_IS_OK(result)) {
2950 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2951 TALLOC_FREE(lsa_pipe);
2955 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2957 d_fprintf(stderr, _("Out of memory\n"));
2958 TALLOC_FREE(lsa_pipe);
2959 return NT_STATUS_NO_MEMORY;
2962 for (i=0; i<num_members; i++) {
2963 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2966 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2967 num_members, alias_sids,
2968 &domains, &names, &types);
2970 if (!NT_STATUS_IS_OK(result) &&
2971 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2972 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2973 TALLOC_FREE(lsa_pipe);
2977 for (i = 0; i < num_members; i++) {
2979 sid_to_fstring(sid_str, &alias_sids[i]);
2981 if (c->opt_long_list_entries) {
2982 printf("%s %s\\%s %d\n", sid_str,
2983 domains[i] ? domains[i] : _("*unknown*"),
2984 names[i] ? names[i] : _("*unknown*"), types[i]);
2987 printf("%s\\%s\n", domains[i], names[i]);
2989 printf("%s\n", sid_str);
2993 TALLOC_FREE(lsa_pipe);
2994 return NT_STATUS_OK;
2997 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2998 const struct dom_sid *domain_sid,
2999 const char *domain_name,
3000 struct cli_state *cli,
3001 struct rpc_pipe_client *pipe_hnd,
3002 TALLOC_CTX *mem_ctx,
3006 NTSTATUS result, status;
3007 struct policy_handle connect_pol, domain_pol;
3008 struct samr_Ids rids, rid_types;
3009 struct lsa_String lsa_acct_name;
3010 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3012 /* Get sam policy handle */
3014 status = dcerpc_samr_Connect2(b, mem_ctx,
3016 MAXIMUM_ALLOWED_ACCESS,
3019 if (!NT_STATUS_IS_OK(status)) {
3022 if (!NT_STATUS_IS_OK(result)) {
3026 /* Get domain policy handle */
3028 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3030 MAXIMUM_ALLOWED_ACCESS,
3031 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3034 if (!NT_STATUS_IS_OK(status)) {
3037 if (!NT_STATUS_IS_OK(result)) {
3041 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3043 status = dcerpc_samr_LookupNames(b, mem_ctx,
3050 if (!NT_STATUS_IS_OK(status)) {
3054 if (!NT_STATUS_IS_OK(result)) {
3056 /* Ok, did not find it in the global sam, try with builtin */
3058 struct dom_sid sid_Builtin;
3060 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3062 sid_copy(&sid_Builtin, &global_sid_Builtin);
3064 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3066 MAXIMUM_ALLOWED_ACCESS,
3070 if (!NT_STATUS_IS_OK(status)) {
3073 if (!NT_STATUS_IS_OK(result)) {
3074 d_fprintf(stderr, _("Couldn't find group %s\n"),
3079 status = dcerpc_samr_LookupNames(b, mem_ctx,
3086 if (!NT_STATUS_IS_OK(status)) {
3089 if (!NT_STATUS_IS_OK(result)) {
3090 d_fprintf(stderr, _("Couldn't find group %s\n"),
3096 if (rids.count != 1) {
3097 d_fprintf(stderr, _("Couldn't find group %s\n"),
3102 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3103 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3104 domain_sid, &domain_pol,
3108 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3109 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3113 return NT_STATUS_NO_SUCH_GROUP;
3116 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3118 if (argc != 1 || c->display_usage) {
3119 return rpc_group_usage(c, argc, argv);
3122 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3123 rpc_group_members_internals,
3127 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3129 NET_API_STATUS status;
3130 struct GROUP_INFO_0 g0;
3134 d_printf(_("Usage:\n"));
3135 d_printf("net rpc group rename group newname\n");
3139 g0.grpi0_name = argv[1];
3141 status = NetGroupSetInfo(c->opt_host,
3148 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3149 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3157 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3159 if (argc != 2 || c->display_usage) {
3160 return rpc_group_usage(c, argc, argv);
3163 return rpc_group_rename_internals(c, argc, argv);
3167 * 'net rpc group' entrypoint.
3168 * @param argc Standard main() style argc.
3169 * @param argv Standard main() style argv. Initial components are already
3173 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3175 NET_API_STATUS status;
3177 struct functable func[] = {
3182 N_("Create specified group"),
3183 N_("net rpc group add\n"
3184 " Create specified group")
3190 N_("Delete specified group"),
3191 N_("net rpc group delete\n"
3192 " Delete specified group")
3198 N_("Add member to group"),
3199 N_("net rpc group addmem\n"
3200 " Add member to group")
3206 N_("Remove member from group"),
3207 N_("net rpc group delmem\n"
3208 " Remove member from group")
3215 N_("net rpc group list\n"
3222 N_("List group members"),
3223 N_("net rpc group members\n"
3224 " List group members")
3231 N_("net rpc group rename\n"
3234 {NULL, NULL, 0, NULL, NULL}
3237 status = libnetapi_net_init(&c->netapi_ctx);
3241 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3242 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3243 if (c->opt_kerberos) {
3244 libnetapi_set_use_kerberos(c->netapi_ctx);
3248 if (c->display_usage) {
3249 d_printf(_("Usage:\n"));
3250 d_printf(_("net rpc group\n"
3251 " Alias for net rpc group list global "
3252 "local builtin\n"));
3253 net_display_usage_from_functable(func);
3257 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3258 rpc_group_list_internals,
3262 return net_run_function(c, argc, argv, "net rpc group", func);
3265 /****************************************************************************/
3267 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3269 return net_share_usage(c, argc, argv);
3273 * Add a share on a remote RPC server.
3275 * @param argc Standard main() style argc.
3276 * @param argv Standard main() style argv. Initial components are already
3279 * @return A shell status integer (0 for success).
3282 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3284 NET_API_STATUS status;
3287 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3288 uint32 num_users=0, perms=0;
3289 char *password=NULL; /* don't allow a share password */
3290 struct SHARE_INFO_2 i2;
3291 uint32_t parm_error = 0;
3293 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3294 return rpc_share_usage(c, argc, argv);
3297 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3301 path = strchr(sharename, '=');
3308 i2.shi2_netname = sharename;
3309 i2.shi2_type = type;
3310 i2.shi2_remark = c->opt_comment;
3311 i2.shi2_permissions = perms;
3312 i2.shi2_max_uses = c->opt_maxusers;
3313 i2.shi2_current_uses = num_users;
3314 i2.shi2_path = path;
3315 i2.shi2_passwd = password;
3317 status = NetShareAdd(c->opt_host,
3322 printf(_("NetShareAdd failed with: %s\n"),
3323 libnetapi_get_error_string(c->netapi_ctx, status));
3330 * Delete a share on a remote RPC server.
3332 * @param domain_sid The domain sid acquired from the remote server.
3333 * @param argc Standard main() style argc.
3334 * @param argv Standard main() style argv. Initial components are already
3337 * @return A shell status integer (0 for success).
3339 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3341 if (argc < 1 || c->display_usage) {
3342 return rpc_share_usage(c, argc, argv);
3345 return NetShareDel(c->opt_host, argv[0], 0);
3349 * Formatted print of share info
3351 * @param r pointer to SHARE_INFO_1 to format
3354 static void display_share_info_1(struct net_context *c,
3355 struct SHARE_INFO_1 *r)
3357 if (c->opt_long_list_entries) {
3358 d_printf("%-12s %-8.8s %-50s\n",
3360 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3363 d_printf("%s\n", r->shi1_netname);
3367 static WERROR get_share_info(struct net_context *c,
3368 struct rpc_pipe_client *pipe_hnd,
3369 TALLOC_CTX *mem_ctx,
3373 struct srvsvc_NetShareInfoCtr *info_ctr)
3377 union srvsvc_NetShareInfo info;
3378 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3380 /* no specific share requested, enumerate all */
3383 uint32_t preferred_len = 0xffffffff;
3384 uint32_t total_entries = 0;
3385 uint32_t resume_handle = 0;
3387 info_ctr->level = level;
3389 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3396 if (!NT_STATUS_IS_OK(status)) {
3397 return ntstatus_to_werror(status);
3402 /* request just one share */
3403 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3410 if (!NT_STATUS_IS_OK(status)) {
3411 result = ntstatus_to_werror(status);
3415 if (!W_ERROR_IS_OK(result)) {
3420 ZERO_STRUCTP(info_ctr);
3422 info_ctr->level = level;
3427 struct srvsvc_NetShareCtr1 *ctr1;
3429 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3430 W_ERROR_HAVE_NO_MEMORY(ctr1);
3433 ctr1->array = info.info1;
3435 info_ctr->ctr.ctr1 = ctr1;
3441 struct srvsvc_NetShareCtr2 *ctr2;
3443 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3444 W_ERROR_HAVE_NO_MEMORY(ctr2);
3447 ctr2->array = info.info2;
3449 info_ctr->ctr.ctr2 = ctr2;
3455 struct srvsvc_NetShareCtr502 *ctr502;
3457 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3458 W_ERROR_HAVE_NO_MEMORY(ctr502);
3461 ctr502->array = info.info502;
3463 info_ctr->ctr.ctr502 = ctr502;
3473 * 'net rpc share list' entrypoint.
3474 * @param argc Standard main() style argc.
3475 * @param argv Standard main() style argv. Initial components are already
3478 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3480 NET_API_STATUS status;
3481 struct SHARE_INFO_1 *i1 = NULL;
3482 uint32_t entries_read = 0;
3483 uint32_t total_entries = 0;
3484 uint32_t resume_handle = 0;
3485 uint32_t i, level = 1;
3487 if (c->display_usage) {
3489 "net rpc share list\n"
3492 _("List shares on remote server"));
3496 status = NetShareEnum(c->opt_host,
3498 (uint8_t **)(void *)&i1,
3507 /* Display results */
3509 if (c->opt_long_list_entries) {
3511 "\nEnumerating shared resources (exports) on remote server:\n\n"
3512 "\nShare name Type Description\n"
3513 "---------- ---- -----------\n"));
3515 for (i = 0; i < entries_read; i++)
3516 display_share_info_1(c, &i1[i]);
3521 static bool check_share_availability(struct cli_state *cli, const char *netname)
3525 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3526 if (!NT_STATUS_IS_OK(status)) {
3527 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3531 status = cli_tdis(cli);
3532 if (!NT_STATUS_IS_OK(status)) {
3533 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3540 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3541 const char *netname, uint32 type)
3543 /* only support disk shares */
3544 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3545 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3550 /* skip builtin shares */
3551 /* FIXME: should print$ be added too ? */
3552 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3553 strequal(netname,"global"))
3556 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3557 printf(_("excluding [%s]\n"), netname);
3561 return check_share_availability(cli, netname);
3565 * Migrate shares from a remote RPC server to the local RPC server.
3567 * All parameters are provided by the run_rpc_command function, except for
3568 * argc, argv which are passed through.
3570 * @param domain_sid The domain sid acquired from the remote server.
3571 * @param cli A cli_state connected to the server.
3572 * @param mem_ctx Talloc context, destroyed on completion of the function.
3573 * @param argc Standard main() style argc.
3574 * @param argv Standard main() style argv. Initial components are already
3577 * @return Normal NTSTATUS return.
3580 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3581 const struct dom_sid *domain_sid,
3582 const char *domain_name,
3583 struct cli_state *cli,
3584 struct rpc_pipe_client *pipe_hnd,
3585 TALLOC_CTX *mem_ctx,
3590 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3591 struct srvsvc_NetShareInfoCtr ctr_src;
3593 struct rpc_pipe_client *srvsvc_pipe = NULL;
3594 struct cli_state *cli_dst = NULL;
3595 uint32 level = 502; /* includes secdesc */
3596 uint32_t parm_error = 0;
3597 struct dcerpc_binding_handle *b;
3599 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3601 if (!W_ERROR_IS_OK(result))
3604 /* connect destination PI_SRVSVC */
3605 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3606 &ndr_table_srvsvc.syntax_id);
3607 if (!NT_STATUS_IS_OK(nt_status))
3610 b = srvsvc_pipe->binding_handle;
3612 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3614 union srvsvc_NetShareInfo info;
3615 struct srvsvc_NetShareInfo502 info502 =
3616 ctr_src.ctr.ctr502->array[i];
3618 /* reset error-code */
3619 nt_status = NT_STATUS_UNSUCCESSFUL;
3621 if (!check_share_sanity(c, cli, info502.name, info502.type))
3624 /* finally add the share on the dst server */
3626 printf(_("migrating: [%s], path: %s, comment: %s, without "
3628 info502.name, info502.path, info502.comment);
3630 info.info502 = &info502;
3632 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3633 srvsvc_pipe->desthost,
3638 if (!NT_STATUS_IS_OK(nt_status)) {
3639 printf(_("cannot add share: %s\n"),
3640 nt_errstr(nt_status));
3643 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3644 printf(_(" [%s] does already exist\n"),
3649 if (!W_ERROR_IS_OK(result)) {
3650 nt_status = werror_to_ntstatus(result);
3651 printf(_("cannot add share: %s\n"),
3652 win_errstr(result));
3658 nt_status = NT_STATUS_OK;
3662 cli_shutdown(cli_dst);
3670 * Migrate shares from a RPC server to another.
3672 * @param argc Standard main() style argc.
3673 * @param argv Standard main() style argv. Initial components are already
3676 * @return A shell status integer (0 for success).
3678 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3681 if (c->display_usage) {
3683 "net rpc share migrate shares\n"
3686 _("Migrate shares to local server"));
3691 printf(_("no server to migrate\n"));
3695 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3696 rpc_share_migrate_shares_internals,
3703 * @param f file_info
3704 * @param mask current search mask
3705 * @param state arg-pointer
3708 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3709 const char *mask, void *state)
3711 static NTSTATUS nt_status;
3712 static struct copy_clistate *local_state;
3713 static fstring filename, new_mask;
3716 struct net_context *c;
3718 local_state = (struct copy_clistate *)state;
3719 nt_status = NT_STATUS_UNSUCCESSFUL;
3723 if (strequal(f->name, ".") || strequal(f->name, ".."))
3724 return NT_STATUS_OK;
3726 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3729 if (f->mode & aDIR) {
3731 DEBUG(3,("got dir: %s\n", f->name));
3733 fstrcpy(dir, local_state->cwd);
3735 fstrcat(dir, f->name);
3737 switch (net_mode_share)
3739 case NET_MODE_SHARE_MIGRATE:
3740 /* create that directory */
3741 nt_status = net_copy_file(c, local_state->mem_ctx,
3742 local_state->cli_share_src,
3743 local_state->cli_share_dst,
3745 c->opt_acls? true : false,
3746 c->opt_attrs? true : false,
3747 c->opt_timestamps? true:false,
3751 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3752 return NT_STATUS_INTERNAL_ERROR;
3755 if (!NT_STATUS_IS_OK(nt_status)) {
3756 printf(_("could not handle dir %s: %s\n"),
3757 dir, nt_errstr(nt_status));
3761 /* search below that directory */
3762 fstrcpy(new_mask, dir);
3763 fstrcat(new_mask, "\\*");
3765 old_dir = local_state->cwd;
3766 local_state->cwd = dir;
3767 nt_status = sync_files(local_state, new_mask);
3768 if (!NT_STATUS_IS_OK(nt_status)) {
3769 printf(_("could not handle files\n"));
3771 local_state->cwd = old_dir;
3778 fstrcpy(filename, local_state->cwd);
3779 fstrcat(filename, "\\");
3780 fstrcat(filename, f->name);
3782 DEBUG(3,("got file: %s\n", filename));
3784 switch (net_mode_share)
3786 case NET_MODE_SHARE_MIGRATE:
3787 nt_status = net_copy_file(c, local_state->mem_ctx,
3788 local_state->cli_share_src,
3789 local_state->cli_share_dst,
3791 c->opt_acls? true : false,
3792 c->opt_attrs? true : false,
3793 c->opt_timestamps? true: false,
3797 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3799 return NT_STATUS_INTERNAL_ERROR;
3802 if (!NT_STATUS_IS_OK(nt_status))
3803 printf(_("could not handle file %s: %s\n"),
3804 filename, nt_errstr(nt_status));
3809 * sync files, can be called recursivly to list files
3810 * and then call copy_fn for each file
3812 * @param cp_clistate pointer to the copy_clistate we work with
3813 * @param mask the current search mask
3815 * @return Boolean result
3817 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3819 struct cli_state *targetcli;
3820 char *targetpath = NULL;
3823 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3825 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3826 mask, &targetcli, &targetpath ) ) {
3827 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3829 mask, cli_errstr(cp_clistate->cli_share_src));
3830 return cli_nt_error(cp_clistate->cli_share_src);
3833 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3834 copy_fn, cp_clistate);
3835 if (!NT_STATUS_IS_OK(status)) {
3836 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3837 mask, nt_errstr(status));
3845 * Set the top level directory permissions before we do any further copies.
3846 * Should set up ACL inheritance.
3849 bool copy_top_level_perms(struct net_context *c,
3850 struct copy_clistate *cp_clistate,
3851 const char *sharename)
3853 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3855 switch (net_mode_share) {
3856 case NET_MODE_SHARE_MIGRATE:
3857 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3858 nt_status = net_copy_fileattr(c,
3859 cp_clistate->mem_ctx,
3860 cp_clistate->cli_share_src,
3861 cp_clistate->cli_share_dst,
3863 c->opt_acls? true : false,
3864 c->opt_attrs? true : false,
3865 c->opt_timestamps? true: false,
3869 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3873 if (!NT_STATUS_IS_OK(nt_status)) {
3874 printf(_("Could handle directory attributes for top level "
3875 "directory of share %s. Error %s\n"),
3876 sharename, nt_errstr(nt_status));
3884 * Sync all files inside a remote share to another share (over smb).
3886 * All parameters are provided by the run_rpc_command function, except for
3887 * argc, argv which are passed through.
3889 * @param domain_sid The domain sid acquired from the remote server.
3890 * @param cli A cli_state connected to the server.
3891 * @param mem_ctx Talloc context, destroyed on completion of the function.
3892 * @param argc Standard main() style argc.
3893 * @param argv Standard main() style argv. Initial components are already
3896 * @return Normal NTSTATUS return.
3899 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3900 const struct dom_sid *domain_sid,
3901 const char *domain_name,
3902 struct cli_state *cli,
3903 struct rpc_pipe_client *pipe_hnd,
3904 TALLOC_CTX *mem_ctx,
3909 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3910 struct srvsvc_NetShareInfoCtr ctr_src;
3913 struct copy_clistate cp_clistate;
3914 bool got_src_share = false;
3915 bool got_dst_share = false;
3916 const char *mask = "\\*";
3919 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3921 nt_status = NT_STATUS_NO_MEMORY;
3925 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3928 if (!W_ERROR_IS_OK(result))
3931 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3933 struct srvsvc_NetShareInfo502 info502 =
3934 ctr_src.ctr.ctr502->array[i];
3936 if (!check_share_sanity(c, cli, info502.name, info502.type))
3939 /* one might not want to mirror whole discs :) */
3940 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3941 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3946 switch (net_mode_share)
3948 case NET_MODE_SHARE_MIGRATE:
3952 d_fprintf(stderr, _("Unsupported mode %d\n"),
3956 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3959 c->opt_acls ? _("including") : _("without"),
3960 c->opt_attrs ? _("including") : _("without"),
3961 c->opt_timestamps ? _("(preserving timestamps)") : "");
3963 cp_clistate.mem_ctx = mem_ctx;
3964 cp_clistate.cli_share_src = NULL;
3965 cp_clistate.cli_share_dst = NULL;
3966 cp_clistate.cwd = NULL;
3967 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3970 /* open share source */
3971 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3972 &cli->dest_ss, cli->desthost,
3973 info502.name, "A:");
3974 if (!NT_STATUS_IS_OK(nt_status))
3977 got_src_share = true;
3979 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3980 /* open share destination */
3981 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3982 NULL, dst, info502.name, "A:");
3983 if (!NT_STATUS_IS_OK(nt_status))
3986 got_dst_share = true;
3989 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3990 d_fprintf(stderr, _("Could not handle the top level "
3991 "directory permissions for the "
3992 "share: %s\n"), info502.name);
3993 nt_status = NT_STATUS_UNSUCCESSFUL;
3997 nt_status = sync_files(&cp_clistate, mask);
3998 if (!NT_STATUS_IS_OK(nt_status)) {
3999 d_fprintf(stderr, _("could not handle files for share: "
4000 "%s\n"), info502.name);
4005 nt_status = NT_STATUS_OK;
4010 cli_shutdown(cp_clistate.cli_share_src);
4013 cli_shutdown(cp_clistate.cli_share_dst);
4020 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4022 if (c->display_usage) {
4024 "net share migrate files\n"
4027 _("Migrate files to local server"));
4032 d_printf(_("no server to migrate\n"));
4036 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4037 rpc_share_migrate_files_internals,
4042 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4044 * All parameters are provided by the run_rpc_command function, except for
4045 * argc, argv which are passed through.
4047 * @param domain_sid The domain sid acquired from the remote server.
4048 * @param cli A cli_state connected to the server.
4049 * @param mem_ctx Talloc context, destroyed on completion of the function.
4050 * @param argc Standard main() style argc.
4051 * @param argv Standard main() style argv. Initial components are already
4054 * @return Normal NTSTATUS return.
4057 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4058 const struct dom_sid *domain_sid,
4059 const char *domain_name,
4060 struct cli_state *cli,
4061 struct rpc_pipe_client *pipe_hnd,
4062 TALLOC_CTX *mem_ctx,
4067 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4068 struct srvsvc_NetShareInfoCtr ctr_src;
4069 union srvsvc_NetShareInfo info;
4071 struct rpc_pipe_client *srvsvc_pipe = NULL;
4072 struct cli_state *cli_dst = NULL;
4073 uint32 level = 502; /* includes secdesc */
4074 uint32_t parm_error = 0;
4075 struct dcerpc_binding_handle *b;
4077 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4080 if (!W_ERROR_IS_OK(result))
4083 /* connect destination PI_SRVSVC */
4084 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4085 &ndr_table_srvsvc.syntax_id);
4086 if (!NT_STATUS_IS_OK(nt_status))
4089 b = srvsvc_pipe->binding_handle;
4091 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4093 struct srvsvc_NetShareInfo502 info502 =
4094 ctr_src.ctr.ctr502->array[i];
4096 /* reset error-code */
4097 nt_status = NT_STATUS_UNSUCCESSFUL;
4099 if (!check_share_sanity(c, cli, info502.name, info502.type))
4102 printf(_("migrating: [%s], path: %s, comment: %s, including "
4104 info502.name, info502.path, info502.comment);
4107 display_sec_desc(info502.sd_buf.sd);
4109 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4110 info.info502 = &info502;
4112 /* finally modify the share on the dst server */
4113 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4114 srvsvc_pipe->desthost,
4120 if (!NT_STATUS_IS_OK(nt_status)) {
4121 printf(_("cannot set share-acl: %s\n"),
4122 nt_errstr(nt_status));
4125 if (!W_ERROR_IS_OK(result)) {
4126 nt_status = werror_to_ntstatus(result);
4127 printf(_("cannot set share-acl: %s\n"),
4128 win_errstr(result));
4134 nt_status = NT_STATUS_OK;
4138 cli_shutdown(cli_dst);
4146 * Migrate share-acls from a RPC server to another.
4148 * @param argc Standard main() style argc.
4149 * @param argv Standard main() style argv. Initial components are already
4152 * @return A shell status integer (0 for success).
4154 static int rpc_share_migrate_security(struct net_context *c, int argc,
4157 if (c->display_usage) {
4159 "net rpc share migrate security\n"
4162 _("Migrate share-acls to local server"));
4167 d_printf(_("no server to migrate\n"));
4171 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4172 rpc_share_migrate_security_internals,
4177 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4178 * from one server to another.
4180 * @param argc Standard main() style argc.
4181 * @param argv Standard main() style argv. Initial components are already
4184 * @return A shell status integer (0 for success).
4187 static int rpc_share_migrate_all(struct net_context *c, int argc,
4192 if (c->display_usage) {
4194 "net rpc share migrate all\n"
4197 _("Migrates shares including all share settings"));
4202 d_printf(_("no server to migrate\n"));
4206 /* order is important. we don't want to be locked out by the share-acl
4207 * before copying files - gd */
4209 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4210 rpc_share_migrate_shares_internals, argc, argv);
4214 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4215 rpc_share_migrate_files_internals, argc, argv);
4219 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4220 rpc_share_migrate_security_internals, argc,
4226 * 'net rpc share migrate' entrypoint.
4227 * @param argc Standard main() style argc.
4228 * @param argv Standard main() style argv. Initial components are already
4231 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4234 struct functable func[] = {
4237 rpc_share_migrate_all,
4239 N_("Migrate shares from remote to local server"),
4240 N_("net rpc share migrate all\n"
4241 " Migrate shares from remote to local server")
4245 rpc_share_migrate_files,
4247 N_("Migrate files from remote to local server"),
4248 N_("net rpc share migrate files\n"
4249 " Migrate files from remote to local server")
4253 rpc_share_migrate_security,
4255 N_("Migrate share-ACLs from remote to local server"),
4256 N_("net rpc share migrate security\n"
4257 " Migrate share-ACLs from remote to local server")
4261 rpc_share_migrate_shares,
4263 N_("Migrate shares from remote to local server"),
4264 N_("net rpc share migrate shares\n"
4265 " Migrate shares from remote to local server")
4267 {NULL, NULL, 0, NULL, NULL}
4270 net_mode_share = NET_MODE_SHARE_MIGRATE;
4272 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4278 struct dom_sid *members;
4281 static int num_server_aliases;
4282 static struct full_alias *server_aliases;
4285 * Add an alias to the static list.
4287 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4289 if (server_aliases == NULL)
4290 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4292 server_aliases[num_server_aliases] = *alias;
4293 num_server_aliases += 1;
4297 * For a specific domain on the server, fetch all the aliases
4298 * and their members. Add all of them to the server_aliases.
4301 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4302 TALLOC_CTX *mem_ctx,
4303 struct policy_handle *connect_pol,
4304 const struct dom_sid *domain_sid)
4306 uint32 start_idx, max_entries, num_entries, i;
4307 struct samr_SamArray *groups = NULL;
4308 NTSTATUS result, status;
4309 struct policy_handle domain_pol;
4310 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4312 /* Get domain policy handle */
4314 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4316 MAXIMUM_ALLOWED_ACCESS,
4317 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4320 if (!NT_STATUS_IS_OK(status)) {
4323 if (!NT_STATUS_IS_OK(result)) {
4331 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4338 if (!NT_STATUS_IS_OK(status)) {
4341 for (i = 0; i < num_entries; i++) {
4343 struct policy_handle alias_pol;
4344 struct full_alias alias;
4345 struct lsa_SidArray sid_array;
4349 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4351 MAXIMUM_ALLOWED_ACCESS,
4352 groups->entries[i].idx,
4355 if (!NT_STATUS_IS_OK(status)) {
4358 if (!NT_STATUS_IS_OK(_result)) {
4363 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4367 if (!NT_STATUS_IS_OK(status)) {
4370 if (!NT_STATUS_IS_OK(_result)) {
4375 alias.num_members = sid_array.num_sids;
4377 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4378 if (!NT_STATUS_IS_OK(status)) {
4381 if (!NT_STATUS_IS_OK(_result)) {
4386 alias.members = NULL;
4388 if (alias.num_members > 0) {
4389 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4391 for (j = 0; j < alias.num_members; j++)
4392 sid_copy(&alias.members[j],
4393 sid_array.sids[j].sid);
4396 sid_compose(&alias.sid, domain_sid,
4397 groups->entries[i].idx);
4399 push_alias(mem_ctx, &alias);
4401 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4403 status = NT_STATUS_OK;
4406 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4412 * Dump server_aliases as names for debugging purposes.
4415 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4416 const struct dom_sid *domain_sid,
4417 const char *domain_name,
4418 struct cli_state *cli,
4419 struct rpc_pipe_client *pipe_hnd,
4420 TALLOC_CTX *mem_ctx,
4426 struct policy_handle lsa_pol;
4427 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4429 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4430 SEC_FLAG_MAXIMUM_ALLOWED,
4432 if (!NT_STATUS_IS_OK(result))
4435 for (i=0; i<num_server_aliases; i++) {
4438 enum lsa_SidType *types;
4441 struct full_alias *alias = &server_aliases[i];
4443 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4445 &domains, &names, &types);
4446 if (!NT_STATUS_IS_OK(result))
4449 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4451 if (alias->num_members == 0) {
4456 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4459 &domains, &names, &types);
4461 if (!NT_STATUS_IS_OK(result) &&
4462 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4465 for (j=0; j<alias->num_members; j++)
4466 DEBUG(1, ("%s\\%s (%d); ",
4467 domains[j] ? domains[j] : "*unknown*",
4468 names[j] ? names[j] : "*unknown*",types[j]));
4472 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4474 return NT_STATUS_OK;
4478 * Fetch a list of all server aliases and their members into
4482 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4483 const struct dom_sid *domain_sid,
4484 const char *domain_name,
4485 struct cli_state *cli,
4486 struct rpc_pipe_client *pipe_hnd,
4487 TALLOC_CTX *mem_ctx,
4491 NTSTATUS result, status;
4492 struct policy_handle connect_pol;
4493 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4495 status = dcerpc_samr_Connect2(b, mem_ctx,
4497 MAXIMUM_ALLOWED_ACCESS,
4500 if (!NT_STATUS_IS_OK(status)) {
4503 if (!NT_STATUS_IS_OK(result)) {
4508 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4509 &global_sid_Builtin);
4510 if (!NT_STATUS_IS_OK(status)) {
4514 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4517 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4522 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4524 token->num_sids = 4;
4526 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4527 d_fprintf(stderr, "malloc %s\n",_("failed"));
4528 token->num_sids = 0;
4532 token->sids[0] = *user_sid;
4533 sid_copy(&token->sids[1], &global_sid_World);
4534 sid_copy(&token->sids[2], &global_sid_Network);
4535 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4538 static void free_user_token(struct security_token *token)
4540 SAFE_FREE(token->sids);
4543 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4545 if (security_token_has_sid(token, sid))
4548 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4553 sid_copy(&token->sids[token->num_sids], sid);
4555 token->num_sids += 1;
4560 struct security_token token;
4563 static void dump_user_token(struct user_token *token)
4567 d_printf("%s\n", token->name);
4569 for (i=0; i<token->token.num_sids; i++) {
4570 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4574 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4578 for (i=0; i<alias->num_members; i++) {
4579 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4586 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4590 for (i=0; i<num_server_aliases; i++) {
4591 if (is_alias_member(&sid, &server_aliases[i]))
4592 add_sid_to_token(token, &server_aliases[i].sid);
4597 * We got a user token with all the SIDs we can know about without asking the
4598 * server directly. These are the user and domain group sids. All of these can
4599 * be members of aliases. So scan the list of aliases for each of the SIDs and
4600 * add them to the token.
4603 static void collect_alias_memberships(struct security_token *token)
4605 int num_global_sids = token->num_sids;
4608 for (i=0; i<num_global_sids; i++) {
4609 collect_sid_memberships(token, token->sids[i]);
4613 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4615 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4616 enum wbcSidType type;
4618 struct wbcDomainSid wsid;
4619 char *sid_str = NULL;
4620 struct dom_sid user_sid;
4621 uint32_t num_groups;
4622 gid_t *groups = NULL;
4625 fstr_sprintf(full_name, "%s%c%s",
4626 domain, *lp_winbind_separator(), user);
4628 /* First let's find out the user sid */
4630 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4632 if (!WBC_ERROR_IS_OK(wbc_status)) {
4633 DEBUG(1, ("winbind could not find %s: %s\n",
4634 full_name, wbcErrorString(wbc_status)));
4638 wbc_status = wbcSidToString(&wsid, &sid_str);
4639 if (!WBC_ERROR_IS_OK(wbc_status)) {
4643 if (type != WBC_SID_NAME_USER) {
4644 wbcFreeMemory(sid_str);
4645 DEBUG(1, ("%s is not a user\n", full_name));
4649 if (!string_to_sid(&user_sid, sid_str)) {
4650 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4654 wbcFreeMemory(sid_str);
4657 init_user_token(token, &user_sid);
4659 /* And now the groups winbind knows about */
4661 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4662 if (!WBC_ERROR_IS_OK(wbc_status)) {
4663 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4664 full_name, wbcErrorString(wbc_status)));
4668 for (i = 0; i < num_groups; i++) {
4669 gid_t gid = groups[i];
4672 wbc_status = wbcGidToSid(gid, &wsid);
4673 if (!WBC_ERROR_IS_OK(wbc_status)) {
4674 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4675 (unsigned int)gid, wbcErrorString(wbc_status)));
4676 wbcFreeMemory(groups);
4680 wbc_status = wbcSidToString(&wsid, &sid_str);
4681 if (!WBC_ERROR_IS_OK(wbc_status)) {
4682 wbcFreeMemory(groups);
4686 DEBUG(3, (" %s\n", sid_str));
4688 string_to_sid(&sid, sid_str);
4689 wbcFreeMemory(sid_str);
4692 add_sid_to_token(token, &sid);
4694 wbcFreeMemory(groups);
4700 * Get a list of all user tokens we want to look at
4703 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4704 struct user_token **user_tokens)
4706 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4707 uint32_t i, num_users;
4709 struct user_token *result;
4710 TALLOC_CTX *frame = NULL;
4712 if (lp_winbind_use_default_domain() &&
4713 (c->opt_target_workgroup == NULL)) {
4714 d_fprintf(stderr, _("winbind use default domain = yes set, "
4715 "please specify a workgroup\n"));
4719 /* Send request to winbind daemon */
4721 wbc_status = wbcListUsers(NULL, &num_users, &users);
4722 if (!WBC_ERROR_IS_OK(wbc_status)) {
4723 DEBUG(1, (_("winbind could not list users: %s\n"),
4724 wbcErrorString(wbc_status)));
4728 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4730 if (result == NULL) {
4731 DEBUG(1, ("Could not malloc sid array\n"));
4732 wbcFreeMemory(users);
4736 frame = talloc_stackframe();
4737 for (i=0; i < num_users; i++) {
4738 fstring domain, user;
4741 fstrcpy(result[i].name, users[i]);
4743 p = strchr(users[i], *lp_winbind_separator());
4745 DEBUG(3, ("%s\n", users[i]));
4748 fstrcpy(domain, c->opt_target_workgroup);
4749 fstrcpy(user, users[i]);
4752 fstrcpy(domain, users[i]);
4757 get_user_sids(domain, user, &(result[i].token));
4760 wbcFreeMemory(users);
4762 *num_tokens = num_users;
4763 *user_tokens = result;
4768 static bool get_user_tokens_from_file(FILE *f,
4770 struct user_token **tokens)
4772 struct user_token *token = NULL;
4777 if (fgets(line, sizeof(line)-1, f) == NULL) {
4781 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4782 line[strlen(line)-1] = '\0';
4785 if (line[0] == ' ') {
4789 if(!string_to_sid(&sid, &line[1])) {
4790 DEBUG(1,("get_user_tokens_from_file: Could "
4791 "not convert sid %s \n",&line[1]));
4795 if (token == NULL) {
4796 DEBUG(0, ("File does not begin with username"));
4800 add_sid_to_token(&token->token, &sid);
4804 /* And a new user... */
4807 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4808 if (*tokens == NULL) {
4809 DEBUG(0, ("Could not realloc tokens\n"));
4813 token = &((*tokens)[*num_tokens-1]);
4815 fstrcpy(token->name, line);
4816 token->token.num_sids = 0;
4817 token->token.sids = NULL;
4826 * Show the list of all users that have access to a share
4829 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4830 TALLOC_CTX *mem_ctx,
4831 const char *netname,
4833 struct user_token *tokens)
4836 struct security_descriptor *share_sd = NULL;
4837 struct security_descriptor *root_sd = NULL;
4838 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4840 union srvsvc_NetShareInfo info;
4844 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4846 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4853 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4854 DEBUG(1, ("Coult not query secdesc for share %s\n",
4859 share_sd = info.info502->sd_buf.sd;
4860 if (share_sd == NULL) {
4861 DEBUG(1, ("Got no secdesc for share %s\n",
4867 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4871 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4872 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4873 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4876 for (i=0; i<num_tokens; i++) {
4879 if (share_sd != NULL) {
4880 status = se_access_check(share_sd, &tokens[i].token,
4883 if (!NT_STATUS_IS_OK(status)) {
4884 DEBUG(1, ("Could not check share_sd for "
4891 if (root_sd == NULL) {
4892 d_printf(" %s\n", tokens[i].name);
4896 status = se_access_check(root_sd, &tokens[i].token,
4898 if (!NT_STATUS_IS_OK(status)) {
4899 DEBUG(1, ("Could not check root_sd for user %s\n",
4903 d_printf(" %s\n", tokens[i].name);
4906 if (fnum != (uint16_t)-1)
4907 cli_close(cli, fnum);
4919 static void collect_share(const char *name, uint32 m,
4920 const char *comment, void *state)
4922 struct share_list *share_list = (struct share_list *)state;
4924 if (m != STYPE_DISKTREE)
4927 share_list->num_shares += 1;
4928 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4929 if (!share_list->shares) {
4930 share_list->num_shares = 0;
4933 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4937 * List shares on a remote RPC server, including the security descriptors.
4939 * All parameters are provided by the run_rpc_command function, except for
4940 * argc, argv which are passed through.
4942 * @param domain_sid The domain sid acquired from the remote server.
4943 * @param cli A cli_state connected to the server.
4944 * @param mem_ctx Talloc context, destroyed on completion of the function.
4945 * @param argc Standard main() style argc.
4946 * @param argv Standard main() style argv. Initial components are already
4949 * @return Normal NTSTATUS return.
4952 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4953 const struct dom_sid *domain_sid,
4954 const char *domain_name,
4955 struct cli_state *cli,
4956 struct rpc_pipe_client *pipe_hnd,
4957 TALLOC_CTX *mem_ctx,
4966 struct user_token *tokens = NULL;
4969 struct share_list share_list;
4974 f = fopen(argv[0], "r");
4978 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4979 return NT_STATUS_UNSUCCESSFUL;
4982 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4988 DEBUG(0, ("Could not read users from file\n"));
4989 return NT_STATUS_UNSUCCESSFUL;
4992 for (i=0; i<num_tokens; i++)
4993 collect_alias_memberships(&tokens[i].token);
4995 share_list.num_shares = 0;
4996 share_list.shares = NULL;
4998 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
5001 DEBUG(0, ("Error returning browse list: %s\n",
5006 for (i = 0; i < share_list.num_shares; i++) {
5007 char *netname = share_list.shares[i];
5009 if (netname[strlen(netname)-1] == '$')
5012 d_printf("%s\n", netname);
5014 show_userlist(pipe_hnd, mem_ctx, netname,
5015 num_tokens, tokens);
5018 for (i=0; i<num_tokens; i++) {
5019 free_user_token(&tokens[i].token);
5022 SAFE_FREE(share_list.shares);
5024 return NT_STATUS_OK;
5027 static int rpc_share_allowedusers(struct net_context *c, int argc,
5032 if (c->display_usage) {
5034 "net rpc share allowedusers\n"
5037 _("List allowed users"));
5041 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5042 rpc_aliaslist_internals,
5047 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
5053 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5054 rpc_share_allowedusers_internals,
5058 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5061 struct user_token *tokens = NULL;
5065 net_usersidlist_usage(c, argc, argv);
5069 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5070 DEBUG(0, ("Could not get the user/sid list\n"));
5074 for (i=0; i<num_tokens; i++) {
5075 dump_user_token(&tokens[i]);
5076 free_user_token(&tokens[i].token);
5083 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5085 d_printf(_("net usersidlist\n"
5086 "\tprints out a list of all users the running winbind knows\n"
5087 "\tabout, together with all their SIDs. This is used as\n"
5088 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5090 net_common_flags_usage(c, argc, argv);
5095 * 'net rpc share' entrypoint.
5096 * @param argc Standard main() style argc.
5097 * @param argv Standard main() style argv. Initial components are already
5101 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5103 NET_API_STATUS status;
5105 struct functable func[] = {
5111 N_("net rpc share add\n"
5119 N_("net rpc share delete\n"
5124 rpc_share_allowedusers,
5126 N_("Modify allowed users"),
5127 N_("net rpc share allowedusers\n"
5128 " Modify allowed users")
5134 N_("Migrate share to local server"),
5135 N_("net rpc share migrate\n"
5136 " Migrate share to local server")
5143 N_("net rpc share list\n"
5146 {NULL, NULL, 0, NULL, NULL}
5149 status = libnetapi_net_init(&c->netapi_ctx);
5153 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5154 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5155 if (c->opt_kerberos) {
5156 libnetapi_set_use_kerberos(c->netapi_ctx);
5160 if (c->display_usage) {
5165 " Alias for net rpc share list\n"));
5166 net_display_usage_from_functable(func);
5170 return rpc_share_list(c, argc, argv);
5173 return net_run_function(c, argc, argv, "net rpc share", func);
5176 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5177 TALLOC_CTX *mem_ctx,
5178 struct rpc_sh_ctx *ctx,
5179 struct rpc_pipe_client *pipe_hnd,
5180 int argc, const char **argv)
5183 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5186 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5187 TALLOC_CTX *mem_ctx,
5188 struct rpc_sh_ctx *ctx,
5189 struct rpc_pipe_client *pipe_hnd,
5190 int argc, const char **argv)
5192 NET_API_STATUS status;
5193 uint32_t parm_err = 0;
5194 struct SHARE_INFO_2 i2;
5196 if ((argc < 2) || (argc > 3)) {
5197 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5199 return NT_STATUS_INVALID_PARAMETER;
5202 i2.shi2_netname = argv[0];
5203 i2.shi2_type = STYPE_DISKTREE;
5204 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5205 i2.shi2_permissions = 0;
5206 i2.shi2_max_uses = 0;
5207 i2.shi2_current_uses = 0;
5208 i2.shi2_path = argv[1];
5209 i2.shi2_passwd = NULL;
5211 status = NetShareAdd(pipe_hnd->desthost,
5216 return werror_to_ntstatus(W_ERROR(status));
5219 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5220 TALLOC_CTX *mem_ctx,
5221 struct rpc_sh_ctx *ctx,
5222 struct rpc_pipe_client *pipe_hnd,
5223 int argc, const char **argv)
5226 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5227 return NT_STATUS_INVALID_PARAMETER;
5230 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5233 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5234 TALLOC_CTX *mem_ctx,
5235 struct rpc_sh_ctx *ctx,
5236 struct rpc_pipe_client *pipe_hnd,
5237 int argc, const char **argv)
5239 union srvsvc_NetShareInfo info;
5242 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5245 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5246 return NT_STATUS_INVALID_PARAMETER;
5249 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5255 if (!NT_STATUS_IS_OK(status)) {
5256 result = ntstatus_to_werror(status);
5259 if (!W_ERROR_IS_OK(result)) {
5263 d_printf(_("Name: %s\n"), info.info2->name);
5264 d_printf(_("Comment: %s\n"), info.info2->comment);
5265 d_printf(_("Path: %s\n"), info.info2->path);
5266 d_printf(_("Password: %s\n"), info.info2->password);
5269 return werror_to_ntstatus(result);
5272 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5273 struct rpc_sh_ctx *ctx)
5275 static struct rpc_sh_cmd cmds[] = {
5277 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
5278 N_("List available shares") },
5280 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
5281 N_("Add a share") },
5283 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
5284 N_("Delete a share") },
5286 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
5287 N_("Get information about a share") },
5289 { NULL, NULL, 0, NULL, NULL }
5295 /****************************************************************************/
5297 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5299 return net_file_usage(c, argc, argv);
5303 * Close a file on a remote RPC server.
5305 * @param argc Standard main() style argc.
5306 * @param argv Standard main() style argv. Initial components are already
5309 * @return A shell status integer (0 for success).
5311 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5313 if (argc < 1 || c->display_usage) {
5314 return rpc_file_usage(c, argc, argv);
5317 return NetFileClose(c->opt_host, atoi(argv[0]));
5321 * Formatted print of open file info
5323 * @param r struct FILE_INFO_3 contents
5326 static void display_file_info_3(struct FILE_INFO_3 *r)
5328 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5329 r->fi3_id, r->fi3_username, r->fi3_permissions,
5330 r->fi3_num_locks, r->fi3_pathname);
5334 * List files for a user on a remote RPC server.
5336 * @param argc Standard main() style argc.
5337 * @param argv Standard main() style argv. Initial components are already
5340 * @return A shell status integer (0 for success)..
5343 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5345 NET_API_STATUS status;
5346 uint32 preferred_len = 0xffffffff, i;
5347 const char *username=NULL;
5348 uint32_t total_entries = 0;
5349 uint32_t entries_read = 0;
5350 uint32_t resume_handle = 0;
5351 struct FILE_INFO_3 *i3 = NULL;
5353 if (c->display_usage) {
5354 return rpc_file_usage(c, argc, argv);
5357 /* if argc > 0, must be user command */
5359 username = smb_xstrdup(argv[0]);
5362 status = NetFileEnum(c->opt_host,
5366 (uint8_t **)(void *)&i3,
5376 /* Display results */
5379 "\nEnumerating open files on remote server:\n\n"
5380 "\nFileId Opened by Perms Locks Path"
5381 "\n------ --------- ----- ----- ---- \n"));
5382 for (i = 0; i < entries_read; i++) {
5383 display_file_info_3(&i3[i]);
5390 * 'net rpc file' entrypoint.
5391 * @param argc Standard main() style argc.
5392 * @param argv Standard main() style argv. Initial components are already
5396 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5398 NET_API_STATUS status;
5400 struct functable func[] = {
5405 N_("Close opened file"),
5406 N_("net rpc file close\n"
5407 " Close opened file")
5413 N_("List files opened by user"),
5414 N_("net rpc file user\n"
5415 " List files opened by user")
5422 N_("Display information about opened file"),
5423 N_("net rpc file info\n"
5424 " Display information about opened file")
5427 {NULL, NULL, 0, NULL, NULL}
5430 status = libnetapi_net_init(&c->netapi_ctx);
5434 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5435 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5436 if (c->opt_kerberos) {
5437 libnetapi_set_use_kerberos(c->netapi_ctx);
5441 if (c->display_usage) {
5442 d_printf(_("Usage:\n"));
5443 d_printf(_("net rpc file\n"
5444 " List opened files\n"));
5445 net_display_usage_from_functable(func);
5449 return rpc_file_user(c, argc, argv);
5452 return net_run_function(c, argc, argv, "net rpc file", func);
5456 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5458 * All parameters are provided by the run_rpc_command function, except for
5459 * argc, argv which are passed through.
5461 * @param c A net_context structure.
5462 * @param domain_sid The domain sid acquired from the remote server.
5463 * @param cli A cli_state connected to the server.
5464 * @param mem_ctx Talloc context, destroyed on completion of the function.
5465 * @param argc Standard main() style argc.
5466 * @param argv Standard main() style argv. Initial components are already
5469 * @return Normal NTSTATUS return.
5472 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5473 const struct dom_sid *domain_sid,
5474 const char *domain_name,
5475 struct cli_state *cli,
5476 struct rpc_pipe_client *pipe_hnd,
5477 TALLOC_CTX *mem_ctx,
5481 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5483 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5485 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5486 if (!NT_STATUS_IS_OK(status)) {
5489 if (W_ERROR_IS_OK(result)) {
5490 d_printf(_("\nShutdown successfully aborted\n"));
5491 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5493 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5495 return werror_to_ntstatus(result);
5499 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5501 * All parameters are provided by the run_rpc_command function, except for
5502 * argc, argv which are passed through.
5504 * @param c A net_context structure.
5505 * @param domain_sid The domain sid acquired from the remote server.
5506 * @param cli A cli_state connected to the server.
5507 * @param mem_ctx Talloc context, destroyed on completion of the function.
5508 * @param argc Standard main() style argc.
5509 * @param argv Standard main() style argv. Initial components are already
5512 * @return Normal NTSTATUS return.
5515 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5516 const struct dom_sid *domain_sid,
5517 const char *domain_name,
5518 struct cli_state *cli,
5519 struct rpc_pipe_client *pipe_hnd,
5520 TALLOC_CTX *mem_ctx,
5524 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5526 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5528 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5530 if (!NT_STATUS_IS_OK(result)) {
5531 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5534 if (W_ERROR_IS_OK(werr)) {
5535 d_printf(_("\nShutdown successfully aborted\n"));
5536 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5538 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5540 return werror_to_ntstatus(werr);
5544 * ABORT the shutdown of a remote RPC server.
5546 * @param argc Standard main() style argc.
5547 * @param argv Standard main() style argv. Initial components are already
5550 * @return A shell status integer (0 for success).
5553 static int rpc_shutdown_abort(struct net_context *c, int argc,
5558 if (c->display_usage) {
5560 "net rpc abortshutdown\n"
5563 _("Abort a scheduled shutdown"));
5567 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5568 rpc_shutdown_abort_internals, argc, argv);
5573 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5575 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5576 rpc_reg_shutdown_abort_internals,
5581 * Shut down a remote RPC Server via initshutdown pipe.
5583 * All parameters are provided by the run_rpc_command function, except for
5584 * argc, argv which are passed through.
5586 * @param c A net_context structure.
5587 * @param domain_sid The domain sid acquired from the remote server.
5588 * @param cli A cli_state connected to the server.
5589 * @param mem_ctx Talloc context, destroyed on completion of the function.
5590 * @param argc Standard main() style argc.
5591 * @param argv Standard main() style argv. Initial components are already
5594 * @return Normal NTSTATUS return.
5597 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5598 const struct dom_sid *domain_sid,
5599 const char *domain_name,
5600 struct cli_state *cli,
5601 struct rpc_pipe_client *pipe_hnd,
5602 TALLOC_CTX *mem_ctx,
5606 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5608 const char *msg = N_("This machine will be shutdown shortly");
5609 uint32 timeout = 20;
5610 struct lsa_StringLarge msg_string;
5611 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5613 if (c->opt_comment) {
5614 msg = c->opt_comment;
5616 if (c->opt_timeout) {
5617 timeout = c->opt_timeout;
5620 msg_string.string = msg;
5622 /* create an entry */
5623 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5624 &msg_string, timeout, c->opt_force, c->opt_reboot,
5626 if (!NT_STATUS_IS_OK(status)) {
5629 if (W_ERROR_IS_OK(result)) {
5630 d_printf(_("\nShutdown of remote machine succeeded\n"));
5631 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5633 DEBUG(1,("Shutdown of remote machine failed!\n"));
5635 return werror_to_ntstatus(result);
5639 * Shut down a remote RPC Server via winreg pipe.
5641 * All parameters are provided by the run_rpc_command function, except for
5642 * argc, argv which are passed through.
5644 * @param c A net_context structure.
5645 * @param domain_sid The domain sid acquired from the remote server.
5646 * @param cli A cli_state connected to the server.
5647 * @param mem_ctx Talloc context, destroyed on completion of the function.
5648 * @param argc Standard main() style argc.
5649 * @param argv Standard main() style argv. Initial components are already
5652 * @return Normal NTSTATUS return.
5655 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5656 const struct dom_sid *domain_sid,
5657 const char *domain_name,
5658 struct cli_state *cli,
5659 struct rpc_pipe_client *pipe_hnd,
5660 TALLOC_CTX *mem_ctx,
5664 const char *msg = N_("This machine will be shutdown shortly");
5665 uint32 timeout = 20;
5666 struct lsa_StringLarge msg_string;
5669 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5671 if (c->opt_comment) {
5672 msg = c->opt_comment;
5674 msg_string.string = msg;
5676 if (c->opt_timeout) {
5677 timeout = c->opt_timeout;
5680 /* create an entry */
5681 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5682 &msg_string, timeout, c->opt_force, c->opt_reboot,
5684 if (!NT_STATUS_IS_OK(result)) {
5685 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5689 if (W_ERROR_IS_OK(werr)) {
5690 d_printf(_("\nShutdown of remote machine succeeded\n"));
5692 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5693 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5694 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5696 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5699 return werror_to_ntstatus(werr);
5703 * Shut down a remote RPC server.
5705 * @param argc Standard main() style argc.
5706 * @param argv Standard main() style argv. Initial components are already
5709 * @return A shell status integer (0 for success).
5712 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5716 if (c->display_usage) {
5718 "net rpc shutdown\n"
5721 _("Shut down a remote RPC server"));
5725 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5726 rpc_init_shutdown_internals, argc, argv);
5729 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5730 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5731 rpc_reg_shutdown_internals, argc, argv);
5737 /***************************************************************************
5738 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5739 ***************************************************************************/
5742 * Add interdomain trust account to the RPC server.
5743 * All parameters (except for argc and argv) are passed by run_rpc_command
5746 * @param c A net_context structure.
5747 * @param domain_sid The domain sid acquired from the server.
5748 * @param cli A cli_state connected to the server.
5749 * @param mem_ctx Talloc context, destroyed on completion of the function.
5750 * @param argc Standard main() style argc.
5751 * @param argv Standard main() style argv. Initial components are already
5754 * @return normal NTSTATUS return code.
5757 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5758 const struct dom_sid *domain_sid,
5759 const char *domain_name,
5760 struct cli_state *cli,
5761 struct rpc_pipe_client *pipe_hnd,
5762 TALLOC_CTX *mem_ctx,
5766 struct policy_handle connect_pol, domain_pol, user_pol;
5767 NTSTATUS status, result;
5769 struct lsa_String lsa_acct_name;
5771 uint32 acct_flags=0;
5773 uint32_t access_granted = 0;
5774 union samr_UserInfo info;
5775 unsigned int orig_timeout;
5776 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5781 _(" net rpc trustdom add <domain_name> "
5782 "<trust password>\n"));
5783 return NT_STATUS_INVALID_PARAMETER;
5787 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5790 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5791 return NT_STATUS_NO_MEMORY;
5794 strupper_m(acct_name);
5796 init_lsa_String(&lsa_acct_name, acct_name);
5798 /* Get samr policy handle */
5799 status = dcerpc_samr_Connect2(b, mem_ctx,
5801 MAXIMUM_ALLOWED_ACCESS,
5804 if (!NT_STATUS_IS_OK(status)) {
5807 if (!NT_STATUS_IS_OK(result)) {
5812 /* Get domain policy handle */
5813 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5815 MAXIMUM_ALLOWED_ACCESS,
5816 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5819 if (!NT_STATUS_IS_OK(status)) {
5822 if (!NT_STATUS_IS_OK(result)) {
5827 /* This call can take a long time - allow the server to time out.
5828 * 35 seconds should do it. */
5830 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5832 /* Create trusting domain's account */
5833 acb_info = ACB_NORMAL;
5834 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5835 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5836 SAMR_USER_ACCESS_SET_PASSWORD |
5837 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5838 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5840 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5849 if (!NT_STATUS_IS_OK(status)) {
5852 /* And restore our original timeout. */
5853 rpccli_set_timeout(pipe_hnd, orig_timeout);
5855 if (!NT_STATUS_IS_OK(result)) {
5857 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5858 acct_name, nt_errstr(result));
5863 struct samr_CryptPassword crypt_pwd;
5865 ZERO_STRUCT(info.info23);
5867 init_samr_CryptPassword(argv[1],
5868 &cli->user_session_key,
5871 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5872 SAMR_FIELD_NT_PASSWORD_PRESENT;
5873 info.info23.info.acct_flags = ACB_DOMTRUST;
5874 info.info23.password = crypt_pwd;
5876 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5881 if (!NT_STATUS_IS_OK(status)) {
5885 if (!NT_STATUS_IS_OK(result)) {
5887 DEBUG(0,("Could not set trust account password: %s\n",
5888 nt_errstr(result)));
5894 SAFE_FREE(acct_name);
5899 * Create interdomain trust account for a remote domain.
5901 * @param argc Standard argc.
5902 * @param argv Standard argv without initial components.
5904 * @return Integer status (0 means success).
5907 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5909 if (argc > 0 && !c->display_usage) {
5910 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5911 rpc_trustdom_add_internals, argc, argv);
5915 _("net rpc trustdom add <domain_name> <trust "
5923 * Remove interdomain trust account from the RPC server.
5924 * All parameters (except for argc and argv) are passed by run_rpc_command
5927 * @param c A net_context structure.
5928 * @param domain_sid The domain sid acquired from the server.
5929 * @param cli A cli_state connected to the server.
5930 * @param mem_ctx Talloc context, destroyed on completion of the function.
5931 * @param argc Standard main() style argc.
5932 * @param argv Standard main() style argv. Initial components are already
5935 * @return normal NTSTATUS return code.
5938 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5939 const struct dom_sid *domain_sid,
5940 const char *domain_name,
5941 struct cli_state *cli,
5942 struct rpc_pipe_client *pipe_hnd,
5943 TALLOC_CTX *mem_ctx,
5947 struct policy_handle connect_pol, domain_pol, user_pol;
5948 NTSTATUS status, result;
5950 struct dom_sid trust_acct_sid;
5951 struct samr_Ids user_rids, name_types;
5952 struct lsa_String lsa_acct_name;
5953 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5958 _(" net rpc trustdom del <domain_name>\n"));
5959 return NT_STATUS_INVALID_PARAMETER;
5963 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5965 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5967 if (acct_name == NULL)
5968 return NT_STATUS_NO_MEMORY;
5970 strupper_m(acct_name);
5972 /* Get samr policy handle */
5973 status = dcerpc_samr_Connect2(b, mem_ctx,
5975 MAXIMUM_ALLOWED_ACCESS,
5978 if (!NT_STATUS_IS_OK(status)) {
5981 if (!NT_STATUS_IS_OK(result)) {
5986 /* Get domain policy handle */
5987 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5989 MAXIMUM_ALLOWED_ACCESS,
5990 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5993 if (!NT_STATUS_IS_OK(status)) {
5996 if (!NT_STATUS_IS_OK(result)) {
6001 init_lsa_String(&lsa_acct_name, acct_name);
6003 status = dcerpc_samr_LookupNames(b, mem_ctx,
6010 if (!NT_STATUS_IS_OK(status)) {
6011 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6013 acct_name, nt_errstr(status));
6016 if (!NT_STATUS_IS_OK(result)) {
6018 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6020 acct_name, nt_errstr(result) );
6024 status = dcerpc_samr_OpenUser(b, mem_ctx,
6026 MAXIMUM_ALLOWED_ACCESS,
6030 if (!NT_STATUS_IS_OK(status)) {
6031 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6033 acct_name, nt_errstr(status) );
6037 if (!NT_STATUS_IS_OK(result)) {
6039 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6041 acct_name, nt_errstr(result) );
6045 /* append the rid to the domain sid */
6046 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6050 /* remove the sid */
6052 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6056 if (!NT_STATUS_IS_OK(status)) {
6057 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6058 " on user %s failed %s\n"),
6059 acct_name, nt_errstr(status));
6062 if (!NT_STATUS_IS_OK(result)) {
6064 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6065 " on user %s failed %s\n"),
6066 acct_name, nt_errstr(result) );
6073 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6076 if (!NT_STATUS_IS_OK(status)) {
6077 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6079 acct_name, nt_errstr(status));
6083 if (!NT_STATUS_IS_OK(result)) {
6085 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6087 acct_name, nt_errstr(result) );
6091 if (!NT_STATUS_IS_OK(result)) {
6092 d_printf(_("Could not set trust account password: %s\n"),
6102 * Delete interdomain trust account for a remote domain.
6104 * @param argc Standard argc.
6105 * @param argv Standard argv without initial components.
6107 * @return Integer status (0 means success).
6110 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6112 if (argc > 0 && !c->display_usage) {
6113 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
6114 rpc_trustdom_del_internals, argc, argv);
6118 _("net rpc trustdom del <domain>\n"));
6123 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6124 struct cli_state *cli,
6125 TALLOC_CTX *mem_ctx,
6126 const char *domain_name)
6128 char *dc_name = NULL;
6129 const char *buffer = NULL;
6130 struct rpc_pipe_client *netr;
6133 struct dcerpc_binding_handle *b;
6135 /* Use NetServerEnum2 */
6137 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6139 return NT_STATUS_OK;
6142 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6143 for domain %s\n", domain_name));
6145 /* Try netr_GetDcName */
6147 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
6149 if (!NT_STATUS_IS_OK(status)) {
6153 b = netr->binding_handle;
6155 status = dcerpc_netr_GetDcName(b, mem_ctx,
6162 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6166 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6167 for domain %s\n", domain_name));
6169 if (!NT_STATUS_IS_OK(status)) {
6173 return werror_to_ntstatus(result);
6177 * Establish trust relationship to a trusting domain.
6178 * Interdomain account must already be created on remote PDC.
6180 * @param c A net_context structure.
6181 * @param argc Standard argc.
6182 * @param argv Standard argv without initial components.
6184 * @return Integer status (0 means success).
6187 static int rpc_trustdom_establish(struct net_context *c, int argc,
6190 struct cli_state *cli = NULL;
6191 struct sockaddr_storage server_ss;
6192 struct rpc_pipe_client *pipe_hnd = NULL;
6193 struct policy_handle connect_hnd;
6194 TALLOC_CTX *mem_ctx;
6195 NTSTATUS nt_status, result;
6196 struct dom_sid *domain_sid;
6201 union lsa_PolicyInformation *info = NULL;
6202 struct dcerpc_binding_handle *b;
6205 * Connect to \\server\ipc$ as 'our domain' account with password
6208 if (argc != 1 || c->display_usage) {
6211 _("net rpc trustdom establish <domain_name>\n"));
6215 domain_name = smb_xstrdup(argv[0]);
6216 strupper_m(domain_name);
6218 /* account name used at first is our domain's name with '$' */
6219 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6222 strupper_m(acct_name);
6225 * opt_workgroup will be used by connection functions further,
6226 * hence it should be set to remote domain name instead of ours
6228 if (c->opt_workgroup) {
6229 c->opt_workgroup = smb_xstrdup(domain_name);
6232 c->opt_user_name = acct_name;
6234 /* find the domain controller */
6235 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6236 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6240 /* connect to ipc$ as username/password */
6241 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6242 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6244 /* Is it trusting domain account for sure ? */
6245 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6246 nt_errstr(nt_status)));
6250 /* store who we connected to */
6252 saf_store( domain_name, pdc_name );
6255 * Connect to \\server\ipc$ again (this time anonymously)
6258 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6261 if (NT_STATUS_IS_ERR(nt_status)) {
6262 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6263 domain_name, nt_errstr(nt_status)));
6267 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6268 "domain %s", domain_name))) {
6269 DEBUG(0, ("talloc_init() failed\n"));
6274 /* Make sure we're talking to a proper server */
6276 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6277 if (!NT_STATUS_IS_OK(nt_status)) {
6279 talloc_destroy(mem_ctx);
6284 * Call LsaOpenPolicy and LsaQueryInfo
6287 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6289 if (!NT_STATUS_IS_OK(nt_status)) {
6290 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6292 talloc_destroy(mem_ctx);
6296 b = pipe_hnd->binding_handle;
6298 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6300 if (NT_STATUS_IS_ERR(nt_status)) {
6301 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6302 nt_errstr(nt_status)));
6304 talloc_destroy(mem_ctx);
6308 /* Querying info level 5 */
6310 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6312 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6315 if (NT_STATUS_IS_ERR(nt_status)) {
6316 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6317 nt_errstr(nt_status)));
6319 talloc_destroy(mem_ctx);
6322 if (NT_STATUS_IS_ERR(result)) {
6323 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6324 nt_errstr(result)));
6326 talloc_destroy(mem_ctx);
6330 domain_sid = info->account_domain.sid;
6332 /* There should be actually query info level 3 (following nt serv behaviour),
6333 but I still don't know if it's _really_ necessary */
6336 * Store the password in secrets db
6339 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6340 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6342 talloc_destroy(mem_ctx);
6347 * Close the pipes and clean up
6350 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6351 if (NT_STATUS_IS_ERR(nt_status)) {
6352 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6353 nt_errstr(nt_status)));
6355 talloc_destroy(mem_ctx);
6361 talloc_destroy(mem_ctx);
6363 d_printf(_("Trust to domain %s established\n"), domain_name);
6368 * Revoke trust relationship to the remote domain.
6370 * @param c A net_context structure.
6371 * @param argc Standard argc.
6372 * @param argv Standard argv without initial components.
6374 * @return Integer status (0 means success).
6377 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6383 if (argc < 1 || c->display_usage) {
6386 _("net rpc trustdom revoke <domain_name>\n"
6387 " Revoke trust relationship\n"
6388 " domain_name\tName of domain to revoke trust\n"));
6392 /* generate upper cased domain name */
6393 domain_name = smb_xstrdup(argv[0]);
6394 strupper_m(domain_name);
6396 /* delete password of the trust */
6397 if (!pdb_del_trusteddom_pw(domain_name)) {
6398 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6405 SAFE_FREE(domain_name);
6409 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6410 const struct dom_sid *domain_sid,
6411 const char *domain_name,
6412 struct cli_state *cli,
6413 struct rpc_pipe_client *pipe_hnd,
6414 TALLOC_CTX *mem_ctx,
6419 if (!sid_to_fstring(str_sid, domain_sid)) {
6420 return NT_STATUS_UNSUCCESSFUL;
6422 d_printf("%s\n", str_sid);
6423 return NT_STATUS_OK;
6426 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6430 /* convert sid into ascii string */
6431 sid_to_fstring(ascii_sid, dom_sid);
6433 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6436 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6437 TALLOC_CTX *mem_ctx,
6438 struct policy_handle *pol,
6439 struct dom_sid dom_sid,
6440 const char *trusted_dom_name)
6442 NTSTATUS nt_status, result;
6443 union lsa_TrustedDomainInfo *info = NULL;
6444 char *cleartextpwd = NULL;
6445 uint8_t session_key[16];
6446 DATA_BLOB session_key_blob;
6447 DATA_BLOB data = data_blob_null;
6448 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6450 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6453 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6456 if (NT_STATUS_IS_ERR(nt_status)) {
6457 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6458 nt_errstr(nt_status)));
6461 if (NT_STATUS_IS_ERR(result)) {
6463 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6464 nt_errstr(result)));
6468 data = data_blob(info->password.password->data,
6469 info->password.password->length);
6471 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
6472 DEBUG(0, ("Could not retrieve password hash\n"));
6476 session_key_blob = data_blob_const(session_key, sizeof(session_key));
6477 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
6479 if (cleartextpwd == NULL) {
6480 DEBUG(0,("retrieved NULL password\n"));
6481 nt_status = NT_STATUS_UNSUCCESSFUL;
6485 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6486 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6487 nt_status = NT_STATUS_UNSUCCESSFUL;
6491 #ifdef DEBUG_PASSWORD
6492 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6493 "password: [%s]\n", trusted_dom_name,
6494 sid_string_dbg(&dom_sid), cleartextpwd));
6498 SAFE_FREE(cleartextpwd);
6499 data_blob_free(&data);
6504 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6507 /* common variables */
6508 TALLOC_CTX* mem_ctx;
6509 struct cli_state *cli = NULL;
6510 struct rpc_pipe_client *pipe_hnd = NULL;
6511 NTSTATUS nt_status, result;
6512 const char *domain_name = NULL;
6513 struct dom_sid *queried_dom_sid;
6514 struct policy_handle connect_hnd;
6515 union lsa_PolicyInformation *info = NULL;
6517 /* trusted domains listing variables */
6518 unsigned int enum_ctx = 0;
6520 struct lsa_DomainList dom_list;
6522 struct dcerpc_binding_handle *b;
6524 if (c->display_usage) {
6526 "net rpc trustdom vampire\n"
6529 _("Vampire trust relationship from remote server"));
6534 * Listing trusted domains (stored in secrets.tdb, if local)
6537 mem_ctx = talloc_init("trust relationships vampire");
6540 * set domain and pdc name to local samba server (default)
6541 * or to remote one given in command line
6544 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6545 domain_name = c->opt_workgroup;
6546 c->opt_target_workgroup = c->opt_workgroup;
6548 fstrcpy(pdc_name, global_myname());
6549 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6550 c->opt_target_workgroup = domain_name;
6553 /* open \PIPE\lsarpc and open policy handle */
6554 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6555 if (!NT_STATUS_IS_OK(nt_status)) {
6556 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6557 nt_errstr(nt_status)));
6558 talloc_destroy(mem_ctx);
6562 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6564 if (!NT_STATUS_IS_OK(nt_status)) {
6565 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6566 nt_errstr(nt_status) ));
6568 talloc_destroy(mem_ctx);
6572 b = pipe_hnd->binding_handle;
6574 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6576 if (NT_STATUS_IS_ERR(nt_status)) {
6577 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6578 nt_errstr(nt_status)));
6580 talloc_destroy(mem_ctx);
6584 /* query info level 5 to obtain sid of a domain being queried */
6585 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6587 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6591 if (NT_STATUS_IS_ERR(nt_status)) {
6592 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6593 nt_errstr(nt_status)));
6595 talloc_destroy(mem_ctx);
6598 if (NT_STATUS_IS_ERR(result)) {
6599 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6600 nt_errstr(result)));
6602 talloc_destroy(mem_ctx);
6606 queried_dom_sid = info->account_domain.sid;
6609 * Keep calling LsaEnumTrustdom over opened pipe until
6610 * the end of enumeration is reached
6613 d_printf(_("Vampire trusted domains:\n\n"));
6616 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6622 if (NT_STATUS_IS_ERR(nt_status)) {
6623 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6624 nt_errstr(nt_status)));
6626 talloc_destroy(mem_ctx);
6629 if (NT_STATUS_IS_ERR(result)) {
6631 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6632 nt_errstr(result)));
6634 talloc_destroy(mem_ctx);
6639 for (i = 0; i < dom_list.count; i++) {
6641 print_trusted_domain(dom_list.domains[i].sid,
6642 dom_list.domains[i].name.string);
6644 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6645 *dom_list.domains[i].sid,
6646 dom_list.domains[i].name.string);
6647 if (!NT_STATUS_IS_OK(nt_status)) {
6649 talloc_destroy(mem_ctx);
6655 * in case of no trusted domains say something rather
6656 * than just display blank line
6658 if (!dom_list.count) d_printf(_("none\n"));
6660 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6662 /* close this connection before doing next one */
6663 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6664 if (NT_STATUS_IS_ERR(nt_status)) {
6665 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6666 nt_errstr(nt_status)));
6668 talloc_destroy(mem_ctx);
6672 /* close lsarpc pipe and connection to IPC$ */
6675 talloc_destroy(mem_ctx);
6679 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6681 /* common variables */
6682 TALLOC_CTX* mem_ctx;
6683 struct cli_state *cli = NULL, *remote_cli = NULL;
6684 struct rpc_pipe_client *pipe_hnd = NULL;
6685 NTSTATUS nt_status, result;
6686 const char *domain_name = NULL;
6687 struct dom_sid *queried_dom_sid;
6688 int ascii_dom_name_len;
6689 struct policy_handle connect_hnd;
6690 union lsa_PolicyInformation *info = NULL;
6691 struct dcerpc_binding_handle *b = NULL;
6693 /* trusted domains listing variables */
6694 unsigned int num_domains, enum_ctx = 0;
6696 struct lsa_DomainList dom_list;
6700 /* trusting domains listing variables */
6701 struct policy_handle domain_hnd;
6702 struct samr_SamArray *trusts = NULL;
6704 if (c->display_usage) {
6706 "net rpc trustdom list\n"
6709 _("List incoming and outgoing trust relationships"));
6714 * Listing trusted domains (stored in secrets.tdb, if local)
6717 mem_ctx = talloc_init("trust relationships listing");
6720 * set domain and pdc name to local samba server (default)
6721 * or to remote one given in command line
6724 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6725 domain_name = c->opt_workgroup;
6726 c->opt_target_workgroup = c->opt_workgroup;
6728 fstrcpy(pdc_name, global_myname());
6729 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6730 c->opt_target_workgroup = domain_name;
6733 /* open \PIPE\lsarpc and open policy handle */
6734 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6735 if (!NT_STATUS_IS_OK(nt_status)) {
6736 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6737 nt_errstr(nt_status)));
6738 talloc_destroy(mem_ctx);
6742 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6744 if (!NT_STATUS_IS_OK(nt_status)) {
6745 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6746 nt_errstr(nt_status) ));
6748 talloc_destroy(mem_ctx);
6752 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6754 if (NT_STATUS_IS_ERR(nt_status)) {
6755 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6756 nt_errstr(nt_status)));
6758 talloc_destroy(mem_ctx);
6762 /* query info level 5 to obtain sid of a domain being queried */
6763 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6765 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6769 if (NT_STATUS_IS_ERR(nt_status)) {
6770 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6771 nt_errstr(nt_status)));
6773 talloc_destroy(mem_ctx);
6776 if (NT_STATUS_IS_ERR(result)) {
6777 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6778 nt_errstr(result)));
6780 talloc_destroy(mem_ctx);
6784 queried_dom_sid = info->account_domain.sid;
6787 * Keep calling LsaEnumTrustdom over opened pipe until
6788 * the end of enumeration is reached
6791 d_printf(_("Trusted domains list:\n\n"));
6793 found_domain = false;
6796 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6802 if (NT_STATUS_IS_ERR(nt_status)) {
6803 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6804 nt_errstr(nt_status)));
6806 talloc_destroy(mem_ctx);
6809 if (NT_STATUS_IS_ERR(result)) {
6810 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6811 nt_errstr(result)));
6813 talloc_destroy(mem_ctx);
6818 for (i = 0; i < dom_list.count; i++) {
6819 print_trusted_domain(dom_list.domains[i].sid,
6820 dom_list.domains[i].name.string);
6821 found_domain = true;
6825 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6828 * in case of no trusted domains say something rather
6829 * than just display blank line
6831 if (!found_domain) {
6832 d_printf(_("none\n"));
6835 /* close this connection before doing next one */
6836 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6837 if (NT_STATUS_IS_ERR(nt_status)) {
6838 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6839 nt_errstr(nt_status)));
6841 talloc_destroy(mem_ctx);
6845 TALLOC_FREE(pipe_hnd);
6848 * Listing trusting domains (stored in passdb backend, if local)
6851 d_printf(_("\nTrusting domains list:\n\n"));
6854 * Open \PIPE\samr and get needed policy handles
6856 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6858 if (!NT_STATUS_IS_OK(nt_status)) {
6859 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6861 talloc_destroy(mem_ctx);
6865 b = pipe_hnd->binding_handle;
6868 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6870 SAMR_ACCESS_LOOKUP_DOMAIN,
6873 if (!NT_STATUS_IS_OK(nt_status)) {
6874 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6875 nt_errstr(nt_status)));
6877 talloc_destroy(mem_ctx);
6880 if (!NT_STATUS_IS_OK(result)) {
6882 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6883 nt_errstr(result)));
6885 talloc_destroy(mem_ctx);
6889 /* SamrOpenDomain - we have to open domain policy handle in order to be
6890 able to enumerate accounts*/
6891 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6893 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6897 if (!NT_STATUS_IS_OK(nt_status)) {
6898 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6899 nt_errstr(nt_status)));
6901 talloc_destroy(mem_ctx);
6904 if (!NT_STATUS_IS_OK(result)) {
6906 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6907 nt_errstr(result)));
6909 talloc_destroy(mem_ctx);
6914 * perform actual enumeration
6917 found_domain = false;
6919 enum_ctx = 0; /* reset enumeration context from last enumeration */
6922 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
6930 if (NT_STATUS_IS_ERR(nt_status)) {
6931 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6932 nt_errstr(nt_status)));
6934 talloc_destroy(mem_ctx);
6937 if (NT_STATUS_IS_ERR(result)) {
6939 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6940 nt_errstr(result)));
6942 talloc_destroy(mem_ctx);
6946 for (i = 0; i < num_domains; i++) {
6948 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6950 found_domain = true;
6953 * get each single domain's sid (do we _really_ need this ?):
6954 * 1) connect to domain's pdc
6955 * 2) query the pdc for domain's sid
6958 /* get rid of '$' tail */
6959 ascii_dom_name_len = strlen(str);
6960 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6961 str[ascii_dom_name_len - 1] = '\0';
6963 /* set opt_* variables to remote domain */
6965 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6966 c->opt_target_workgroup = c->opt_workgroup;
6968 d_printf("%-20s", str);
6970 /* connect to remote domain controller */
6971 nt_status = net_make_ipc_connection(c,
6972 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6974 if (NT_STATUS_IS_OK(nt_status)) {
6975 /* query for domain's sid */
6976 if (run_rpc_command(
6978 &ndr_table_lsarpc.syntax_id, 0,
6979 rpc_query_domain_sid, argc,
6981 d_printf(_("strange - couldn't get domain's sid\n"));
6983 cli_shutdown(remote_cli);
6986 d_fprintf(stderr, _("domain controller is not "
6987 "responding: %s\n"),
6988 nt_errstr(nt_status));
6989 d_printf(_("couldn't get domain's sid\n"));
6993 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
6995 if (!found_domain) {
6999 /* close opened samr and domain policy handles */
7000 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7001 if (!NT_STATUS_IS_OK(nt_status)) {
7002 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7005 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7006 if (!NT_STATUS_IS_OK(nt_status)) {
7007 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7010 /* close samr pipe and connection to IPC$ */
7013 talloc_destroy(mem_ctx);
7018 * Entrypoint for 'net rpc trustdom' code.
7020 * @param argc Standard argc.
7021 * @param argv Standard argv without initial components.
7023 * @return Integer status (0 means success).
7026 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7028 struct functable func[] = {
7033 N_("Add trusting domain's account"),
7034 N_("net rpc trustdom add\n"
7035 " Add trusting domain's account")
7041 N_("Remove trusting domain's account"),
7042 N_("net rpc trustdom del\n"
7043 " Remove trusting domain's account")
7047 rpc_trustdom_establish,
7049 N_("Establish outgoing trust relationship"),
7050 N_("net rpc trustdom establish\n"
7051 " Establish outgoing trust relationship")
7055 rpc_trustdom_revoke,
7057 N_("Revoke outgoing trust relationship"),
7058 N_("net rpc trustdom revoke\n"
7059 " Revoke outgoing trust relationship")
7065 N_("List in- and outgoing domain trusts"),
7066 N_("net rpc trustdom list\n"
7067 " List in- and outgoing domain trusts")
7071 rpc_trustdom_vampire,
7073 N_("Vampire trusts from remote server"),
7074 N_("net rpc trustdom vampire\n"
7075 " Vampire trusts from remote server")
7077 {NULL, NULL, 0, NULL, NULL}
7080 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7084 * Check if a server will take rpc commands
7085 * @param flags Type of server to connect to (PDC, DMB, localhost)
7086 * if the host is not explicitly specified
7087 * @return bool (true means rpc supported)
7089 bool net_rpc_check(struct net_context *c, unsigned flags)
7091 struct cli_state *cli;
7093 struct sockaddr_storage server_ss;
7094 char *server_name = NULL;
7097 /* flags (i.e. server type) may depend on command */
7098 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7101 if ((cli = cli_initialise()) == NULL) {
7105 status = cli_connect(cli, server_name, &server_ss);
7106 if (!NT_STATUS_IS_OK(status))
7108 if (!attempt_netbios_session_request(&cli, global_myname(),
7109 server_name, &server_ss))
7111 status = cli_negprot(cli);
7112 if (!NT_STATUS_IS_OK(status))
7114 if (cli->protocol < PROTOCOL_NT1)
7123 /* dump sam database via samsync rpc calls */
7124 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7125 if (c->display_usage) {
7130 _("Dump remote SAM database"));
7134 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
7135 NET_FLAGS_ANONYMOUS,
7136 rpc_samdump_internals, argc, argv);
7139 /* syncronise sam database via samsync rpc calls */
7140 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7142 struct functable func[] = {
7147 N_("Dump remote SAM database to ldif"),
7148 N_("net rpc vampire ldif\n"
7149 " Dump remote SAM database to LDIF file or "
7156 N_("Dump remote SAM database to Kerberos Keytab"),
7157 N_("net rpc vampire keytab\n"
7158 " Dump remote SAM database to Kerberos keytab "
7165 N_("Dump remote SAM database to passdb"),
7166 N_("net rpc vampire passdb\n"
7167 " Dump remote SAM database to passdb")
7170 {NULL, NULL, 0, NULL, NULL}
7174 if (c->display_usage) {
7179 _("Vampire remote SAM database"));
7183 return rpc_vampire_passdb(c, argc, argv);
7186 return net_run_function(c, argc, argv, "net rpc vampire", func);
7190 * Migrate everything from a print server.
7192 * @param c A net_context structure.
7193 * @param argc Standard main() style argc.
7194 * @param argv Standard main() style argv. Initial components are already
7197 * @return A shell status integer (0 for success).
7199 * The order is important !
7200 * To successfully add drivers the print queues have to exist !
7201 * Applying ACLs should be the last step, because you're easily locked out.
7204 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7209 if (c->display_usage) {
7211 "net rpc printer migrate all\n"
7214 _("Migrate everything from a print server"));
7219 d_printf(_("no server to migrate\n"));
7223 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7224 rpc_printer_migrate_printers_internals, argc,
7229 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7230 rpc_printer_migrate_drivers_internals, argc,
7235 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7236 rpc_printer_migrate_forms_internals, argc, argv);
7240 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7241 rpc_printer_migrate_settings_internals, argc,
7246 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7247 rpc_printer_migrate_security_internals, argc,
7253 * Migrate print drivers from a print server.
7255 * @param c A net_context structure.
7256 * @param argc Standard main() style argc.
7257 * @param argv Standard main() style argv. Initial components are already
7260 * @return A shell status integer (0 for success).
7262 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7265 if (c->display_usage) {
7267 "net rpc printer migrate drivers\n"
7270 _("Migrate print-drivers from a print-server"));
7275 d_printf(_("no server to migrate\n"));
7279 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7280 rpc_printer_migrate_drivers_internals,
7285 * Migrate print-forms from a print-server.
7287 * @param c A net_context structure.
7288 * @param argc Standard main() style argc.
7289 * @param argv Standard main() style argv. Initial components are already
7292 * @return A shell status integer (0 for success).
7294 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7297 if (c->display_usage) {
7299 "net rpc printer migrate forms\n"
7302 _("Migrate print-forms from a print-server"));
7307 d_printf(_("no server to migrate\n"));
7311 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7312 rpc_printer_migrate_forms_internals,
7317 * Migrate printers from a print-server.
7319 * @param c A net_context structure.
7320 * @param argc Standard main() style argc.
7321 * @param argv Standard main() style argv. Initial components are already
7324 * @return A shell status integer (0 for success).
7326 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7329 if (c->display_usage) {
7331 "net rpc printer migrate printers\n"
7334 _("Migrate printers from a print-server"));
7339 d_printf(_("no server to migrate\n"));
7343 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7344 rpc_printer_migrate_printers_internals,
7349 * Migrate printer-ACLs from a print-server
7351 * @param c A net_context structure.
7352 * @param argc Standard main() style argc.
7353 * @param argv Standard main() style argv. Initial components are already
7356 * @return A shell status integer (0 for success).
7358 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7361 if (c->display_usage) {
7363 "net rpc printer migrate security\n"
7366 _("Migrate printer-ACLs from a print-server"));
7371 d_printf(_("no server to migrate\n"));
7375 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7376 rpc_printer_migrate_security_internals,
7381 * Migrate printer-settings from a print-server.
7383 * @param c A net_context structure.
7384 * @param argc Standard main() style argc.
7385 * @param argv Standard main() style argv. Initial components are already
7388 * @return A shell status integer (0 for success).
7390 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7393 if (c->display_usage) {
7395 "net rpc printer migrate settings\n"
7398 _("Migrate printer-settings from a "
7404 d_printf(_("no server to migrate\n"));
7408 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7409 rpc_printer_migrate_settings_internals,
7414 * 'net rpc printer' entrypoint.
7416 * @param c A net_context structure.
7417 * @param argc Standard main() style argc.
7418 * @param argv Standard main() style argv. Initial components are already
7422 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7425 /* ouch: when addriver and setdriver are called from within
7426 rpc_printer_migrate_drivers_internals, the printer-queue already
7429 struct functable func[] = {
7432 rpc_printer_migrate_all,
7434 N_("Migrate all from remote to local print server"),
7435 N_("net rpc printer migrate all\n"
7436 " Migrate all from remote to local print server")
7440 rpc_printer_migrate_drivers,
7442 N_("Migrate drivers to local server"),
7443 N_("net rpc printer migrate drivers\n"
7444 " Migrate drivers to local server")
7448 rpc_printer_migrate_forms,
7450 N_("Migrate froms to local server"),
7451 N_("net rpc printer migrate forms\n"
7452 " Migrate froms to local server")
7456 rpc_printer_migrate_printers,
7458 N_("Migrate printers to local server"),
7459 N_("net rpc printer migrate printers\n"
7460 " Migrate printers to local server")
7464 rpc_printer_migrate_security,
7466 N_("Mirgate printer ACLs to local server"),
7467 N_("net rpc printer migrate security\n"
7468 " Mirgate printer ACLs to local server")
7472 rpc_printer_migrate_settings,
7474 N_("Migrate printer settings to local server"),
7475 N_("net rpc printer migrate settings\n"
7476 " Migrate printer settings to local server")
7478 {NULL, NULL, 0, NULL, NULL}
7481 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7486 * List printers on a remote RPC server.
7488 * @param c A net_context structure.
7489 * @param argc Standard main() style argc.
7490 * @param argv Standard main() style argv. Initial components are already
7493 * @return A shell status integer (0 for success).
7495 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7497 if (c->display_usage) {
7499 "net rpc printer list\n"
7502 _("List printers on a remote RPC server"));
7506 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7507 rpc_printer_list_internals,
7512 * List printer-drivers on a remote RPC server.
7514 * @param c A net_context structure.
7515 * @param argc Standard main() style argc.
7516 * @param argv Standard main() style argv. Initial components are already
7519 * @return A shell status integer (0 for success).
7521 static int rpc_printer_driver_list(struct net_context *c, int argc,
7524 if (c->display_usage) {
7526 "net rpc printer driver\n"
7529 _("List printer-drivers on a remote RPC server"));
7533 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7534 rpc_printer_driver_list_internals,
7539 * Publish printer in ADS via MSRPC.
7541 * @param c A net_context structure.
7542 * @param argc Standard main() style argc.
7543 * @param argv Standard main() style argv. Initial components are already
7546 * @return A shell status integer (0 for success).
7548 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7551 if (c->display_usage) {
7553 "net rpc printer publish publish\n"
7556 _("Publish printer in ADS via MSRPC"));
7560 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7561 rpc_printer_publish_publish_internals,
7566 * Update printer in ADS via MSRPC.
7568 * @param c A net_context structure.
7569 * @param argc Standard main() style argc.
7570 * @param argv Standard main() style argv. Initial components are already
7573 * @return A shell status integer (0 for success).
7575 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7577 if (c->display_usage) {
7579 "net rpc printer publish update\n"
7582 _("Update printer in ADS via MSRPC"));
7586 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7587 rpc_printer_publish_update_internals,
7592 * UnPublish printer in ADS via MSRPC.
7594 * @param c A net_context structure.
7595 * @param argc Standard main() style argc.
7596 * @param argv Standard main() style argv. Initial components are already
7599 * @return A shell status integer (0 for success).
7601 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7604 if (c->display_usage) {
7606 "net rpc printer publish unpublish\n"
7609 _("UnPublish printer in ADS via MSRPC"));
7613 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7614 rpc_printer_publish_unpublish_internals,
7619 * List published printers via MSRPC.
7621 * @param c A net_context structure.
7622 * @param argc Standard main() style argc.
7623 * @param argv Standard main() style argv. Initial components are already
7626 * @return A shell status integer (0 for success).
7628 static int rpc_printer_publish_list(struct net_context *c, int argc,
7631 if (c->display_usage) {
7633 "net rpc printer publish list\n"
7636 _("List published printers via MSRPC"));
7640 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7641 rpc_printer_publish_list_internals,
7647 * Publish printer in ADS.
7649 * @param c A net_context structure.
7650 * @param argc Standard main() style argc.
7651 * @param argv Standard main() style argv. Initial components are already
7654 * @return A shell status integer (0 for success).
7656 static int rpc_printer_publish(struct net_context *c, int argc,
7660 struct functable func[] = {
7663 rpc_printer_publish_publish,
7665 N_("Publish printer in AD"),
7666 N_("net rpc printer publish publish\n"
7667 " Publish printer in AD")
7671 rpc_printer_publish_update,
7673 N_("Update printer in AD"),
7674 N_("net rpc printer publish update\n"
7675 " Update printer in AD")
7679 rpc_printer_publish_unpublish,
7681 N_("Unpublish printer"),
7682 N_("net rpc printer publish unpublish\n"
7683 " Unpublish printer")
7687 rpc_printer_publish_list,
7689 N_("List published printers"),
7690 N_("net rpc printer publish list\n"
7691 " List published printers")
7693 {NULL, NULL, 0, NULL, NULL}
7697 if (c->display_usage) {
7698 d_printf(_("Usage:\n"));
7699 d_printf(_("net rpc printer publish\n"
7700 " List published printers\n"
7701 " Alias of net rpc printer publish "
7703 net_display_usage_from_functable(func);
7706 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7707 rpc_printer_publish_list_internals,
7711 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7717 * Display rpc printer help page.
7719 * @param c A net_context structure.
7720 * @param argc Standard main() style argc.
7721 * @param argv Standard main() style argv. Initial components are already
7724 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7726 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7727 "\tlists all printers on print-server\n\n"));
7728 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7729 "\tlists all printer-drivers on print-server\n\n"));
7730 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7731 "\tpublishes printer settings in Active Directory\n"
7732 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7733 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7734 "\n\tmigrates printers from remote to local server\n\n"));
7735 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7736 "\n\tmigrates printer-settings from remote to local server\n\n"));
7737 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7738 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7739 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7740 "\n\tmigrates printer-forms from remote to local server\n\n"));
7741 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7742 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7743 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7744 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7745 "\tremote to local print-server\n\n"));
7746 net_common_methods_usage(c, argc, argv);
7747 net_common_flags_usage(c, argc, argv);
7749 "\t-v or --verbose\t\t\tgive verbose output\n"
7750 "\t --destination\t\tmigration target server (default: localhost)\n"));
7756 * 'net rpc printer' entrypoint.
7758 * @param c A net_context structure.
7759 * @param argc Standard main() style argc.
7760 * @param argv Standard main() style argv. Initial components are already
7763 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7765 struct functable func[] = {
7770 N_("List all printers on print server"),
7771 N_("net rpc printer list\n"
7772 " List all printers on print server")
7776 rpc_printer_migrate,
7778 N_("Migrate printer to local server"),
7779 N_("net rpc printer migrate\n"
7780 " Migrate printer to local server")
7784 rpc_printer_driver_list,
7786 N_("List printer drivers"),
7787 N_("net rpc printer driver\n"
7788 " List printer drivers")
7792 rpc_printer_publish,
7794 N_("Publish printer in AD"),
7795 N_("net rpc printer publish\n"
7796 " Publish printer in AD")
7798 {NULL, NULL, 0, NULL, NULL}
7802 if (c->display_usage) {
7803 d_printf(_("Usage:\n"));
7804 d_printf(_("net rpc printer\n"
7805 " List printers\n"));
7806 net_display_usage_from_functable(func);
7809 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7810 rpc_printer_list_internals,
7814 return net_run_function(c, argc, argv, "net rpc printer", func);
7818 * 'net rpc' entrypoint.
7820 * @param c A net_context structure.
7821 * @param argc Standard main() style argc.
7822 * @param argv Standard main() style argv. Initial components are already
7826 int net_rpc(struct net_context *c, int argc, const char **argv)
7828 NET_API_STATUS status;
7830 struct functable func[] = {
7835 N_("Modify global audit settings"),
7836 N_("net rpc audit\n"
7837 " Modify global audit settings")
7843 N_("Show basic info about a domain"),
7845 " Show basic info about a domain")
7851 N_("Join a domain"),
7859 N_("Join a domain created in server manager"),
7860 N_("net rpc oldjoin\n"
7861 " Join a domain created in server manager")
7867 N_("Test that a join is valid"),
7868 N_("net rpc testjoin\n"
7869 " Test that a join is valid")
7875 N_("List/modify users"),
7877 " List/modify users")
7883 N_("Change a user password"),
7884 N_("net rpc password\n"
7885 " Change a user password\n"
7886 " Alias for net rpc user password")
7892 N_("List/modify groups"),
7893 N_("net rpc group\n"
7894 " List/modify groups")
7900 N_("List/modify shares"),
7901 N_("net rpc share\n"
7902 " List/modify shares")
7908 N_("List open files"),
7916 N_("List/modify printers"),
7917 N_("net rpc printer\n"
7918 " List/modify printers")
7922 net_rpc_changetrustpw,
7924 N_("Change trust account password"),
7925 N_("net rpc changetrustpw\n"
7926 " Change trust account password")
7932 N_("Modify domain trusts"),
7933 N_("net rpc trustdom\n"
7934 " Modify domain trusts")
7940 N_("Abort a remote shutdown"),
7941 N_("net rpc abortshutdown\n"
7942 " Abort a remote shutdown")
7948 N_("Shutdown a remote server"),
7949 N_("net rpc shutdown\n"
7950 " Shutdown a remote server")
7956 N_("Dump SAM data of remote NT PDC"),
7957 N_("net rpc samdump\n"
7958 " Dump SAM data of remote NT PDC")
7964 N_("Sync a remote NT PDC's data into local passdb"),
7965 N_("net rpc vampire\n"
7966 " Sync a remote NT PDC's data into local passdb")
7972 N_("Fetch the domain sid into local secrets.tdb"),
7973 N_("net rpc getsid\n"
7974 " Fetch the domain sid into local secrets.tdb")
7980 N_("Manage privileges assigned to SID"),
7981 N_("net rpc rights\n"
7982 " Manage privileges assigned to SID")
7988 N_("Start/stop/query remote services"),
7989 N_("net rpc service\n"
7990 " Start/stop/query remote services")
7996 N_("Manage registry hives"),
7997 N_("net rpc registry\n"
7998 " Manage registry hives")
8004 N_("Open interactive shell on remote server"),
8005 N_("net rpc shell\n"
8006 " Open interactive shell on remote server")
8008 {NULL, NULL, 0, NULL, NULL}
8011 status = libnetapi_net_init(&c->netapi_ctx);
8015 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8016 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8017 if (c->opt_kerberos) {
8018 libnetapi_set_use_kerberos(c->netapi_ctx);
8020 if (c->opt_ccache) {
8021 libnetapi_set_use_ccache(c->netapi_ctx);
8024 return net_run_function(c, argc, argv, "net rpc", func);