2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/cli_samr.h"
29 #include "rpc_client/init_samr.h"
30 #include "../librpc/gen_ndr/ndr_lsa_c.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
34 #include "../librpc/gen_ndr/ndr_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
36 #include "../librpc/gen_ndr/ndr_winreg_c.h"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "rpc_client/init_lsa.h"
41 #include "../libcli/security/security.h"
42 #include "libsmb/clirap.h"
43 #include "nsswitch/libwbclient/wbclient.h"
46 static int net_mode_share
;
47 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
);
52 * @brief RPC based subcommands for the 'net' utility.
54 * This file should contain much of the functionality that used to
55 * be found in rpcclient, execpt that the commands should change
56 * less often, and the fucntionality should be sane (the user is not
57 * expected to know a rid/sid before they conduct an operation etc.)
59 * @todo Perhaps eventually these should be split out into a number
60 * of files, as this could get quite big.
65 * Many of the RPC functions need the domain sid. This function gets
66 * it at the start of every run
68 * @param cli A cli_state already connected to the remote machine
70 * @return The Domain SID of the remote machine.
73 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
74 struct dom_sid
**domain_sid
,
75 const char **domain_name
)
77 struct rpc_pipe_client
*lsa_pipe
= NULL
;
78 struct policy_handle pol
;
79 NTSTATUS status
, result
;
80 union lsa_PolicyInformation
*info
= NULL
;
81 struct dcerpc_binding_handle
*b
;
83 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
85 if (!NT_STATUS_IS_OK(status
)) {
86 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
90 b
= lsa_pipe
->binding_handle
;
92 status
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
93 SEC_FLAG_MAXIMUM_ALLOWED
,
95 if (!NT_STATUS_IS_OK(status
)) {
96 d_fprintf(stderr
, "open_policy %s: %s\n",
102 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
104 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
107 if (!NT_STATUS_IS_OK(status
)) {
108 d_fprintf(stderr
, "lsaquery %s: %s\n",
113 if (!NT_STATUS_IS_OK(result
)) {
114 d_fprintf(stderr
, "lsaquery %s: %s\n",
120 *domain_name
= info
->account_domain
.name
.string
;
121 *domain_sid
= info
->account_domain
.sid
;
123 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
124 TALLOC_FREE(lsa_pipe
);
130 * Run a single RPC command, from start to finish.
132 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
133 * @param conn_flag a NET_FLAG_ combination. Passed to
134 * net_make_ipc_connection.
135 * @param argc Standard main() style argc.
136 * @param argv Standard main() style argv. Initial components are already
138 * @return A shell status integer (0 for success).
141 int run_rpc_command(struct net_context
*c
,
142 struct cli_state
*cli_arg
,
143 const struct ndr_syntax_id
*interface
,
149 struct cli_state
*cli
= NULL
;
150 struct rpc_pipe_client
*pipe_hnd
= NULL
;
153 struct dom_sid
*domain_sid
;
154 const char *domain_name
;
157 /* make use of cli_state handed over as an argument, if possible */
159 nt_status
= net_make_ipc_connection(c
, conn_flags
, &cli
);
160 if (!NT_STATUS_IS_OK(nt_status
)) {
161 DEBUG(1, ("failed to make ipc connection: %s\n",
162 nt_errstr(nt_status
)));
175 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
176 DEBUG(0, ("talloc_init() failed\n"));
180 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
182 if (!NT_STATUS_IS_OK(nt_status
)) {
186 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
187 if (lp_client_schannel()
188 && (ndr_syntax_id_equal(interface
,
189 &ndr_table_netlogon
.syntax_id
))) {
190 /* Always try and create an schannel netlogon pipe. */
191 nt_status
= cli_rpc_pipe_open_schannel(
192 cli
, interface
, NCACN_NP
,
193 DCERPC_AUTH_LEVEL_PRIVACY
, domain_name
,
195 if (!NT_STATUS_IS_OK(nt_status
)) {
196 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
197 nt_errstr(nt_status
) ));
201 if (conn_flags
& NET_FLAGS_SEAL
) {
202 nt_status
= cli_rpc_pipe_open_ntlmssp(
204 (conn_flags
& NET_FLAGS_TCP
) ?
205 NCACN_IP_TCP
: NCACN_NP
,
206 DCERPC_AUTH_LEVEL_PRIVACY
,
207 lp_workgroup(), c
->opt_user_name
,
208 c
->opt_password
, &pipe_hnd
);
210 nt_status
= cli_rpc_pipe_open_noauth(
214 if (!NT_STATUS_IS_OK(nt_status
)) {
215 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
216 get_pipe_name_from_syntax(
217 talloc_tos(), interface
),
218 nt_errstr(nt_status
) ));
224 nt_status
= fn(c
, domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
226 if (!NT_STATUS_IS_OK(nt_status
)) {
227 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
230 DEBUG(5, ("rpc command function succedded\n"));
233 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
235 TALLOC_FREE(pipe_hnd
);
240 /* close the connection only if it was opened here */
245 talloc_destroy(mem_ctx
);
250 * Force a change of the trust acccount password.
252 * All parameters are provided by the run_rpc_command function, except for
253 * argc, argv which are passed through.
255 * @param domain_sid The domain sid acquired from the remote server.
256 * @param cli A cli_state connected to the server.
257 * @param mem_ctx Talloc context, destroyed on completion of the function.
258 * @param argc Standard main() style argc.
259 * @param argv Standard main() style argv. Initial components are already
262 * @return Normal NTSTATUS return.
265 static NTSTATUS
rpc_changetrustpw_internals(struct net_context
*c
,
266 const struct dom_sid
*domain_sid
,
267 const char *domain_name
,
268 struct cli_state
*cli
,
269 struct rpc_pipe_client
*pipe_hnd
,
276 status
= trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
);
277 if (!NT_STATUS_IS_OK(status
)) {
278 d_fprintf(stderr
, _("Failed to change machine account password: %s\n"),
287 * Force a change of the trust acccount password.
289 * @param argc Standard main() style argc.
290 * @param argv Standard main() style argv. Initial components are already
293 * @return A shell status integer (0 for success).
296 int net_rpc_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
298 if (c
->display_usage
) {
300 "net rpc changetrustpw\n"
303 _("Change the machine trust password"));
307 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
308 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
309 rpc_changetrustpw_internals
,
314 * Join a domain, the old way.
316 * This uses 'machinename' as the inital password, and changes it.
318 * The password should be created with 'server manager' or equiv first.
320 * All parameters are provided by the run_rpc_command function, except for
321 * argc, argv which are passed through.
323 * @param domain_sid The domain sid acquired from the remote server.
324 * @param cli A cli_state connected to the server.
325 * @param mem_ctx Talloc context, destroyed on completion of the function.
326 * @param argc Standard main() style argc.
327 * @param argv Standard main() style argv. Initial components are already
330 * @return Normal NTSTATUS return.
333 static NTSTATUS
rpc_oldjoin_internals(struct net_context
*c
,
334 const struct dom_sid
*domain_sid
,
335 const char *domain_name
,
336 struct cli_state
*cli
,
337 struct rpc_pipe_client
*pipe_hnd
,
343 fstring trust_passwd
;
344 unsigned char orig_trust_passwd_hash
[16];
346 enum netr_SchannelType sec_channel_type
;
348 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
350 if (!NT_STATUS_IS_OK(result
)) {
351 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
354 nt_errstr(result
) ));
359 check what type of join - if the user want's to join as
360 a BDC, the server must agree that we are a BDC.
363 sec_channel_type
= get_sec_channel_type(argv
[0]);
365 sec_channel_type
= get_sec_channel_type(NULL
);
368 fstrcpy(trust_passwd
, global_myname());
369 strlower_m(trust_passwd
);
372 * Machine names can be 15 characters, but the max length on
373 * a password is 14. --jerry
376 trust_passwd
[14] = '\0';
378 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
380 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
,
382 orig_trust_passwd_hash
,
385 if (NT_STATUS_IS_OK(result
))
386 printf(_("Joined domain %s.\n"), c
->opt_target_workgroup
);
389 if (!secrets_store_domain_sid(c
->opt_target_workgroup
, domain_sid
)) {
390 DEBUG(0, ("error storing domain sid for %s\n", c
->opt_target_workgroup
));
391 result
= NT_STATUS_UNSUCCESSFUL
;
398 * Join a domain, the old way.
400 * @param argc Standard main() style argc.
401 * @param argv Standard main() style argv. Initial components are already
404 * @return A shell status integer (0 for success).
407 static int net_rpc_perform_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
409 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
410 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
411 rpc_oldjoin_internals
,
416 * Join a domain, the old way. This function exists to allow
417 * the message to be displayed when oldjoin was explicitly
418 * requested, but not when it was implied by "net rpc join".
420 * @param argc Standard main() style argc.
421 * @param argv Standard main() style argv. Initial components are already
424 * @return A shell status integer (0 for success).
427 static int net_rpc_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
431 if (c
->display_usage
) {
436 _("Join a domain the old way"));
440 rc
= net_rpc_perform_oldjoin(c
, argc
, argv
);
443 d_fprintf(stderr
, _("Failed to join domain\n"));
450 * 'net rpc join' entrypoint.
451 * @param argc Standard main() style argc.
452 * @param argv Standard main() style argv. Initial components are already
455 * Main 'net_rpc_join()' (where the admin username/password is used) is
457 * Try to just change the password, but if that doesn't work, use/prompt
458 * for a username/password.
461 int net_rpc_join(struct net_context
*c
, int argc
, const char **argv
)
463 if (c
->display_usage
) {
466 _("net rpc join -U <username>[%%password] <type>\n"
468 " username\tName of the admin user"
469 " password\tPassword of the admin user, will "
470 "prompt if not specified\n"
471 " type\tCan be one of the following:\n"
472 "\t\tMEMBER\tJoin as member server (default)\n"
473 "\t\tBDC\tJoin as BDC\n"
474 "\t\tPDC\tJoin as PDC\n"));
478 if (lp_server_role() == ROLE_STANDALONE
) {
479 d_printf(_("cannot join as standalone machine\n"));
483 if (strlen(global_myname()) > 15) {
484 d_printf(_("Our netbios name can be at most 15 chars long, "
485 "\"%s\" is %u chars long\n"),
486 global_myname(), (unsigned int)strlen(global_myname()));
490 if ((net_rpc_perform_oldjoin(c
, argc
, argv
) == 0))
493 return net_rpc_join_newstyle(c
, argc
, argv
);
497 * display info about a rpc domain
499 * All parameters are provided by the run_rpc_command function, except for
500 * argc, argv which are passed through.
502 * @param domain_sid The domain sid acquired from the remote server
503 * @param cli A cli_state connected to the server.
504 * @param mem_ctx Talloc context, destroyed on completion of the function.
505 * @param argc Standard main() style argc.
506 * @param argv Standard main() style argv. Initial components are already
509 * @return Normal NTSTATUS return.
512 NTSTATUS
rpc_info_internals(struct net_context
*c
,
513 const struct dom_sid
*domain_sid
,
514 const char *domain_name
,
515 struct cli_state
*cli
,
516 struct rpc_pipe_client
*pipe_hnd
,
521 struct policy_handle connect_pol
, domain_pol
;
522 NTSTATUS status
, result
;
523 union samr_DomainInfo
*info
= NULL
;
525 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
527 sid_to_fstring(sid_str
, domain_sid
);
529 /* Get sam policy handle */
530 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
532 MAXIMUM_ALLOWED_ACCESS
,
535 if (!NT_STATUS_IS_OK(status
)) {
536 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
541 if (!NT_STATUS_IS_OK(result
)) {
543 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
548 /* Get domain policy handle */
549 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
551 MAXIMUM_ALLOWED_ACCESS
,
552 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
555 if (!NT_STATUS_IS_OK(status
)) {
556 d_fprintf(stderr
, _("Could not open domain: %s\n"),
560 if (!NT_STATUS_IS_OK(result
)) {
562 d_fprintf(stderr
, _("Could not open domain: %s\n"),
567 status
= dcerpc_samr_QueryDomainInfo(b
, mem_ctx
,
572 if (!NT_STATUS_IS_OK(status
)) {
576 if (NT_STATUS_IS_OK(result
)) {
577 d_printf(_("Domain Name: %s\n"),
578 info
->general
.domain_name
.string
);
579 d_printf(_("Domain SID: %s\n"), sid_str
);
580 d_printf(_("Sequence number: %llu\n"),
581 (unsigned long long)info
->general
.sequence_num
);
582 d_printf(_("Num users: %u\n"), info
->general
.num_users
);
583 d_printf(_("Num domain groups: %u\n"),info
->general
.num_groups
);
584 d_printf(_("Num local groups: %u\n"),info
->general
.num_aliases
);
592 * 'net rpc info' entrypoint.
593 * @param argc Standard main() style argc.
594 * @param argv Standard main() style argv. Initial components are already
598 int net_rpc_info(struct net_context
*c
, int argc
, const char **argv
)
600 if (c
->display_usage
) {
605 _("Display information about the domain"));
609 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
610 NET_FLAGS_PDC
, rpc_info_internals
,
615 * Fetch domain SID into the local secrets.tdb.
617 * All parameters are provided by the run_rpc_command function, except for
618 * argc, argv which are passed through.
620 * @param domain_sid The domain sid acquired from the remote server.
621 * @param cli A cli_state connected to the server.
622 * @param mem_ctx Talloc context, destroyed on completion of the function.
623 * @param argc Standard main() style argc.
624 * @param argv Standard main() style argv. Initial components are already
627 * @return Normal NTSTATUS return.
630 static NTSTATUS
rpc_getsid_internals(struct net_context
*c
,
631 const struct dom_sid
*domain_sid
,
632 const char *domain_name
,
633 struct cli_state
*cli
,
634 struct rpc_pipe_client
*pipe_hnd
,
641 sid_to_fstring(sid_str
, domain_sid
);
642 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
643 sid_str
, domain_name
);
645 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
646 DEBUG(0,("Can't store domain SID\n"));
647 return NT_STATUS_UNSUCCESSFUL
;
654 * 'net rpc getsid' entrypoint.
655 * @param argc Standard main() style argc.
656 * @param argv Standard main() style argv. Initial components are already
660 int net_rpc_getsid(struct net_context
*c
, int argc
, const char **argv
)
662 int conn_flags
= NET_FLAGS_PDC
;
664 if (!c
->opt_user_specified
) {
665 conn_flags
|= NET_FLAGS_ANONYMOUS
;
668 if (c
->display_usage
) {
673 _("Fetch domain SID into local secrets.tdb"));
677 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
679 rpc_getsid_internals
,
683 /****************************************************************************/
686 * Basic usage function for 'net rpc user'.
687 * @param argc Standard main() style argc.
688 * @param argv Standard main() style argv. Initial components are already
692 static int rpc_user_usage(struct net_context
*c
, int argc
, const char **argv
)
694 return net_user_usage(c
, argc
, argv
);
698 * Add a new user to a remote RPC server.
700 * @param argc Standard main() style argc.
701 * @param argv Standard main() style argv. Initial components are already
704 * @return A shell status integer (0 for success).
707 static int rpc_user_add(struct net_context
*c
, int argc
, const char **argv
)
709 NET_API_STATUS status
;
710 struct USER_INFO_1 info1
;
711 uint32_t parm_error
= 0;
713 if (argc
< 1 || c
->display_usage
) {
714 rpc_user_usage(c
, argc
, argv
);
720 info1
.usri1_name
= argv
[0];
722 info1
.usri1_password
= argv
[1];
725 status
= NetUserAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
728 d_fprintf(stderr
,_("Failed to add user '%s' with error: %s.\n"),
729 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
733 d_printf(_("Added user '%s'.\n"), argv
[0]);
740 * Rename a user on a remote RPC server.
742 * @param argc Standard main() style argc.
743 * @param argv Standard main() style argv. Initial components are already
746 * @return A shell status integer (0 for success).
749 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
751 NET_API_STATUS status
;
752 struct USER_INFO_0 u0
;
753 uint32_t parm_err
= 0;
755 if (argc
!= 2 || c
->display_usage
) {
756 rpc_user_usage(c
, argc
, argv
);
760 u0
.usri0_name
= argv
[1];
762 status
= NetUserSetInfo(c
->opt_host
, argv
[0],
763 0, (uint8_t *)&u0
, &parm_err
);
766 _("Failed to rename user from %s to %s - %s\n"),
768 libnetapi_get_error_string(c
->netapi_ctx
, status
));
770 d_printf(_("Renamed user from %s to %s\n"), argv
[0], argv
[1]);
777 * Set a user's primary group
779 * @param argc Standard main() style argc.
780 * @param argv Standard main() style argv. Initial components are already
783 * @return A shell status integer (0 for success).
786 static int rpc_user_setprimarygroup(struct net_context
*c
, int argc
,
789 NET_API_STATUS status
;
791 struct GROUP_INFO_2
*g2
;
792 struct USER_INFO_1051 u1051
;
793 uint32_t parm_err
= 0;
795 if (argc
!= 2 || c
->display_usage
) {
796 rpc_user_usage(c
, argc
, argv
);
800 status
= NetGroupGetInfo(c
->opt_host
, argv
[1], 2, &buffer
);
802 d_fprintf(stderr
, _("Failed to find group name %s -- %s\n"),
804 libnetapi_get_error_string(c
->netapi_ctx
, status
));
807 g2
= (struct GROUP_INFO_2
*)buffer
;
809 u1051
.usri1051_primary_group_id
= g2
->grpi2_group_id
;
811 NetApiBufferFree(buffer
);
813 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1051,
814 (uint8_t *)&u1051
, &parm_err
);
817 _("Failed to set user's primary group %s to %s - "
818 "%s\n"), argv
[0], argv
[1],
819 libnetapi_get_error_string(c
->netapi_ctx
, status
));
821 d_printf(_("Set primary group of user %s to %s\n"), argv
[0],
828 * Delete a user from a remote RPC server.
830 * @param argc Standard main() style argc.
831 * @param argv Standard main() style argv. Initial components are already
834 * @return A shell status integer (0 for success).
837 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
839 NET_API_STATUS status
;
841 if (argc
< 1 || c
->display_usage
) {
842 rpc_user_usage(c
, argc
, argv
);
846 status
= NetUserDel(c
->opt_host
, argv
[0]);
849 d_fprintf(stderr
, _("Failed to delete user '%s' with: %s.\n"),
851 libnetapi_get_error_string(c
->netapi_ctx
, status
));
854 d_printf(_("Deleted user '%s'.\n"), argv
[0]);
861 * Set a user's password on a remote RPC server.
863 * @param argc Standard main() style argc.
864 * @param argv Standard main() style argv. Initial components are already
867 * @return A shell status integer (0 for success).
870 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
872 NET_API_STATUS status
;
874 struct USER_INFO_1003 u1003
;
875 uint32_t parm_err
= 0;
878 if (argc
< 1 || c
->display_usage
) {
879 rpc_user_usage(c
, argc
, argv
);
884 u1003
.usri1003_password
= argv
[1];
886 ret
= asprintf(&prompt
, _("Enter new password for %s:"),
891 u1003
.usri1003_password
= talloc_strdup(c
, getpass(prompt
));
893 if (u1003
.usri1003_password
== NULL
) {
898 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
900 /* Display results */
903 _("Failed to set password for '%s' with error: %s.\n"),
904 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
913 * List a user's groups from a remote RPC server.
915 * @param argc Standard main() style argc.
916 * @param argv Standard main() style argv. Initial components are already
919 * @return A shell status integer (0 for success)
922 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
925 NET_API_STATUS status
;
926 struct GROUP_USERS_INFO_0
*u0
= NULL
;
927 uint32_t entries_read
= 0;
928 uint32_t total_entries
= 0;
932 if (argc
< 1 || c
->display_usage
) {
933 rpc_user_usage(c
, argc
, argv
);
937 status
= NetUserGetGroups(c
->opt_host
,
940 (uint8_t **)(void *)&u0
,
946 _("Failed to get groups for '%s' with error: %s.\n"),
947 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
952 for (i
=0; i
< entries_read
; i
++) {
953 printf("%s\n", u0
->grui0_name
);
961 * List users on a remote RPC server.
963 * All parameters are provided by the run_rpc_command function, except for
964 * argc, argv which are passed through.
966 * @param domain_sid The domain sid acquired from the remote server.
967 * @param cli A cli_state connected to the server.
968 * @param mem_ctx Talloc context, destroyed on completion of the function.
969 * @param argc Standard main() style argc.
970 * @param argv Standard main() style argv. Initial components are already
973 * @return Normal NTSTATUS return.
976 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
978 NET_API_STATUS status
;
979 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
980 struct NET_DISPLAY_USER
*info
= NULL
;
983 /* Query domain users */
984 if (c
->opt_long_list_entries
)
985 d_printf(_("\nUser name Comment"
986 "\n-----------------------------\n"));
988 uint32_t max_entries
, max_size
;
990 dcerpc_get_query_dispinfo_params(
991 loop_count
, &max_entries
, &max_size
);
993 status
= NetQueryDisplayInformation(c
->opt_host
,
1000 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
1004 info
= (struct NET_DISPLAY_USER
*)buffer
;
1006 for (i
= 0; i
< num_entries
; i
++) {
1008 if (c
->opt_long_list_entries
)
1009 printf("%-21.21s %s\n", info
->usri1_name
,
1010 info
->usri1_comment
);
1012 printf("%s\n", info
->usri1_name
);
1016 NetApiBufferFree(buffer
);
1019 start_idx
+= num_entries
;
1021 } while (status
== ERROR_MORE_DATA
);
1027 * 'net rpc user' entrypoint.
1028 * @param argc Standard main() style argc.
1029 * @param argv Standard main() style argv. Initial components are already
1033 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
1035 NET_API_STATUS status
;
1037 struct functable func
[] = {
1042 N_("Add specified user"),
1043 N_("net rpc user add\n"
1044 " Add specified user")
1050 N_("List domain groups of user"),
1051 N_("net rpc user info\n"
1052 " List domain groups of user")
1058 N_("Remove specified user"),
1059 N_("net rpc user delete\n"
1060 " Remove specified user")
1066 N_("Change user password"),
1067 N_("net rpc user password\n"
1068 " Change user password")
1074 N_("Rename specified user"),
1075 N_("net rpc user rename\n"
1076 " Rename specified user")
1080 rpc_user_setprimarygroup
,
1082 "Set a user's primary group",
1083 "net rpc user setprimarygroup\n"
1084 " Set a user's primary group"
1086 {NULL
, NULL
, 0, NULL
, NULL
}
1089 status
= libnetapi_net_init(&c
->netapi_ctx
);
1093 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1094 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1095 if (c
->opt_kerberos
) {
1096 libnetapi_set_use_kerberos(c
->netapi_ctx
);
1100 if (c
->display_usage
) {
1105 _("List all users"));
1106 net_display_usage_from_functable(func
);
1110 return rpc_user_list(c
, argc
, argv
);
1113 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
1116 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1117 TALLOC_CTX
*mem_ctx
,
1118 struct rpc_sh_ctx
*ctx
,
1119 struct rpc_pipe_client
*pipe_hnd
,
1120 int argc
, const char **argv
)
1122 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
1125 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1126 TALLOC_CTX
*mem_ctx
,
1127 struct rpc_sh_ctx
*ctx
,
1128 struct rpc_pipe_client
*pipe_hnd
,
1129 int argc
, const char **argv
)
1131 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
1134 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1135 TALLOC_CTX
*mem_ctx
,
1136 struct rpc_sh_ctx
*ctx
,
1137 struct rpc_pipe_client
*pipe_hnd
,
1138 int argc
, const char **argv
,
1140 struct net_context
*c
,
1141 TALLOC_CTX
*mem_ctx
,
1142 struct rpc_sh_ctx
*ctx
,
1143 struct rpc_pipe_client
*pipe_hnd
,
1144 struct policy_handle
*user_hnd
,
1145 int argc
, const char **argv
))
1147 struct policy_handle connect_pol
, domain_pol
, user_pol
;
1148 NTSTATUS status
, result
;
1151 enum lsa_SidType type
;
1152 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1155 d_fprintf(stderr
, "%s %s <username>\n", _("Usage:"),
1157 return NT_STATUS_INVALID_PARAMETER
;
1160 ZERO_STRUCT(connect_pol
);
1161 ZERO_STRUCT(domain_pol
);
1162 ZERO_STRUCT(user_pol
);
1164 status
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1165 argv
[0], NULL
, NULL
, &sid
, &type
);
1166 if (!NT_STATUS_IS_OK(status
)) {
1167 d_fprintf(stderr
, _("Could not lookup %s: %s\n"), argv
[0],
1172 if (type
!= SID_NAME_USER
) {
1173 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
1174 sid_type_lookup(type
));
1175 status
= NT_STATUS_NO_SUCH_USER
;
1179 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1180 d_fprintf(stderr
, _("%s is not in our domain\n"), argv
[0]);
1181 status
= NT_STATUS_NO_SUCH_USER
;
1185 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1187 MAXIMUM_ALLOWED_ACCESS
,
1190 if (!NT_STATUS_IS_OK(status
)) {
1193 if (!NT_STATUS_IS_OK(result
)) {
1198 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1200 MAXIMUM_ALLOWED_ACCESS
,
1204 if (!NT_STATUS_IS_OK(status
)) {
1207 if (!NT_STATUS_IS_OK(result
)) {
1212 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1214 MAXIMUM_ALLOWED_ACCESS
,
1218 if (!NT_STATUS_IS_OK(status
)) {
1221 if (!NT_STATUS_IS_OK(result
)) {
1226 status
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1229 if (is_valid_policy_hnd(&user_pol
)) {
1230 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1232 if (is_valid_policy_hnd(&domain_pol
)) {
1233 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1235 if (is_valid_policy_hnd(&connect_pol
)) {
1236 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
1241 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1242 TALLOC_CTX
*mem_ctx
,
1243 struct rpc_sh_ctx
*ctx
,
1244 struct rpc_pipe_client
*pipe_hnd
,
1245 struct policy_handle
*user_hnd
,
1246 int argc
, const char **argv
)
1248 NTSTATUS status
, result
;
1249 union samr_UserInfo
*info
= NULL
;
1250 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1253 d_fprintf(stderr
, "%s %s show <username>\n", _("Usage:"),
1255 return NT_STATUS_INVALID_PARAMETER
;
1258 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1263 if (!NT_STATUS_IS_OK(status
)) {
1266 if (!NT_STATUS_IS_OK(result
)) {
1270 d_printf(_("user rid: %d, group rid: %d\n"),
1272 info
->info21
.primary_gid
);
1277 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1278 TALLOC_CTX
*mem_ctx
,
1279 struct rpc_sh_ctx
*ctx
,
1280 struct rpc_pipe_client
*pipe_hnd
,
1281 int argc
, const char **argv
)
1283 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1284 rpc_sh_user_show_internals
);
1287 #define FETCHSTR(name, rec) \
1288 do { if (strequal(ctx->thiscmd, name)) { \
1289 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1292 #define SETSTR(name, rec, flag) \
1293 do { if (strequal(ctx->thiscmd, name)) { \
1294 init_lsa_String(&(info->info21.rec), argv[0]); \
1295 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1298 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1299 TALLOC_CTX
*mem_ctx
,
1300 struct rpc_sh_ctx
*ctx
,
1301 struct rpc_pipe_client
*pipe_hnd
,
1302 struct policy_handle
*user_hnd
,
1303 int argc
, const char **argv
)
1305 NTSTATUS status
, result
;
1306 const char *username
;
1307 const char *oldval
= "";
1308 union samr_UserInfo
*info
= NULL
;
1309 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1312 d_fprintf(stderr
, "%s %s <username> [new value|NULL]\n",
1313 _("Usage:"), ctx
->whoami
);
1314 return NT_STATUS_INVALID_PARAMETER
;
1317 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1322 if (!NT_STATUS_IS_OK(status
)) {
1325 if (!NT_STATUS_IS_OK(result
)) {
1329 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1331 FETCHSTR("fullname", full_name
);
1332 FETCHSTR("homedir", home_directory
);
1333 FETCHSTR("homedrive", home_drive
);
1334 FETCHSTR("logonscript", logon_script
);
1335 FETCHSTR("profilepath", profile_path
);
1336 FETCHSTR("description", description
);
1339 d_printf(_("%s's %s: [%s]\n"), username
, ctx
->thiscmd
, oldval
);
1343 if (strcmp(argv
[0], "NULL") == 0) {
1347 ZERO_STRUCT(info
->info21
);
1349 SETSTR("fullname", full_name
, FULL_NAME
);
1350 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1351 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1352 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1353 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1354 SETSTR("description", description
, DESCRIPTION
);
1356 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1361 if (!NT_STATUS_IS_OK(status
)) {
1367 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username
,
1368 ctx
->thiscmd
, oldval
, argv
[0]);
1375 #define HANDLEFLG(name, rec) \
1376 do { if (strequal(ctx->thiscmd, name)) { \
1377 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1379 newflags = oldflags | ACB_##rec; \
1381 newflags = oldflags & ~ACB_##rec; \
1384 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1385 TALLOC_CTX
*mem_ctx
,
1386 struct rpc_sh_ctx
*ctx
,
1387 struct rpc_pipe_client
*pipe_hnd
,
1388 int argc
, const char **argv
)
1390 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1391 rpc_sh_user_str_edit_internals
);
1394 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1395 TALLOC_CTX
*mem_ctx
,
1396 struct rpc_sh_ctx
*ctx
,
1397 struct rpc_pipe_client
*pipe_hnd
,
1398 struct policy_handle
*user_hnd
,
1399 int argc
, const char **argv
)
1401 NTSTATUS status
, result
;
1402 const char *username
;
1403 const char *oldval
= "unknown";
1404 uint32 oldflags
, newflags
;
1406 union samr_UserInfo
*info
= NULL
;
1407 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1410 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1411 !strequal(argv
[0], "no"))) {
1412 /* TRANSATORS: The yes|no here are program keywords. Please do
1414 d_fprintf(stderr
, _("Usage: %s <username> [yes|no]\n"),
1416 return NT_STATUS_INVALID_PARAMETER
;
1419 newval
= strequal(argv
[0], "yes");
1421 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1426 if (!NT_STATUS_IS_OK(status
)) {
1429 if (!NT_STATUS_IS_OK(result
)) {
1433 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1434 oldflags
= info
->info21
.acct_flags
;
1435 newflags
= info
->info21
.acct_flags
;
1437 HANDLEFLG("disabled", DISABLED
);
1438 HANDLEFLG("pwnotreq", PWNOTREQ
);
1439 HANDLEFLG("autolock", AUTOLOCK
);
1440 HANDLEFLG("pwnoexp", PWNOEXP
);
1443 d_printf(_("%s's %s flag: %s\n"), username
, ctx
->thiscmd
,
1448 ZERO_STRUCT(info
->info21
);
1450 info
->info21
.acct_flags
= newflags
;
1451 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1453 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1458 if (!NT_STATUS_IS_OK(status
)) {
1462 if (NT_STATUS_IS_OK(result
)) {
1463 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username
,
1464 ctx
->thiscmd
, oldval
, argv
[0]);
1472 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1473 TALLOC_CTX
*mem_ctx
,
1474 struct rpc_sh_ctx
*ctx
,
1475 struct rpc_pipe_client
*pipe_hnd
,
1476 int argc
, const char **argv
)
1478 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1479 rpc_sh_user_flag_edit_internals
);
1482 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1483 TALLOC_CTX
*mem_ctx
,
1484 struct rpc_sh_ctx
*ctx
)
1486 static struct rpc_sh_cmd cmds
[] = {
1488 { "fullname", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1489 N_("Show/Set a user's full name") },
1491 { "homedir", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1492 N_("Show/Set a user's home directory") },
1494 { "homedrive", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1495 N_("Show/Set a user's home drive") },
1497 { "logonscript", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1498 N_("Show/Set a user's logon script") },
1500 { "profilepath", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1501 N_("Show/Set a user's profile path") },
1503 { "description", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1504 N_("Show/Set a user's description") },
1506 { "disabled", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1507 N_("Show/Set whether a user is disabled") },
1509 { "autolock", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1510 N_("Show/Set whether a user locked out") },
1512 { "pwnotreq", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1513 N_("Show/Set whether a user does not need a password") },
1515 { "pwnoexp", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1516 N_("Show/Set whether a user's password does not expire") },
1518 { NULL
, NULL
, 0, NULL
, NULL
}
1524 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1525 TALLOC_CTX
*mem_ctx
,
1526 struct rpc_sh_ctx
*ctx
)
1528 static struct rpc_sh_cmd cmds
[] = {
1530 { "list", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_list
,
1531 N_("List available users") },
1533 { "info", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_info
,
1534 N_("List the domain groups a user is member of") },
1536 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_show
,
1537 N_("Show info about a user") },
1539 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1540 N_("Show/Modify a user's fields") },
1542 { NULL
, NULL
, 0, NULL
, NULL
}
1548 /****************************************************************************/
1551 * Basic usage function for 'net rpc group'.
1552 * @param argc Standard main() style argc.
1553 * @param argv Standard main() style argv. Initial components are already
1557 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1559 return net_group_usage(c
, argc
, argv
);
1563 * Delete group on a remote RPC server.
1565 * All parameters are provided by the run_rpc_command function, except for
1566 * argc, argv which are passed through.
1568 * @param domain_sid The domain sid acquired from the remote server.
1569 * @param cli A cli_state connected to the server.
1570 * @param mem_ctx Talloc context, destroyed on completion of the function.
1571 * @param argc Standard main() style argc.
1572 * @param argv Standard main() style argv. Initial components are already
1575 * @return Normal NTSTATUS return.
1578 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1579 const struct dom_sid
*domain_sid
,
1580 const char *domain_name
,
1581 struct cli_state
*cli
,
1582 struct rpc_pipe_client
*pipe_hnd
,
1583 TALLOC_CTX
*mem_ctx
,
1587 struct policy_handle connect_pol
, domain_pol
, group_pol
, user_pol
;
1588 bool group_is_primary
= false;
1589 NTSTATUS status
, result
;
1591 struct samr_RidAttrArray
*rids
= NULL
;
1594 /* struct samr_RidWithAttribute *user_gids; */
1595 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1597 struct samr_Ids group_rids
, name_types
;
1598 struct lsa_String lsa_acct_name
;
1599 union samr_UserInfo
*info
= NULL
;
1601 if (argc
< 1 || c
->display_usage
) {
1602 rpc_group_usage(c
, argc
,argv
);
1603 return NT_STATUS_OK
; /* ok? */
1606 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1608 MAXIMUM_ALLOWED_ACCESS
,
1611 if (!NT_STATUS_IS_OK(status
)) {
1612 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1616 if (!NT_STATUS_IS_OK(result
)) {
1618 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1622 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1624 MAXIMUM_ALLOWED_ACCESS
,
1625 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1628 if (!NT_STATUS_IS_OK(status
)) {
1629 d_fprintf(stderr
, _("Request open_domain failed\n"));
1633 if (!NT_STATUS_IS_OK(result
)) {
1635 d_fprintf(stderr
, _("Request open_domain failed\n"));
1639 init_lsa_String(&lsa_acct_name
, argv
[0]);
1641 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1648 if (!NT_STATUS_IS_OK(status
)) {
1649 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1653 if (!NT_STATUS_IS_OK(result
)) {
1655 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1659 switch (name_types
.ids
[0])
1661 case SID_NAME_DOM_GRP
:
1662 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
1664 MAXIMUM_ALLOWED_ACCESS
,
1668 if (!NT_STATUS_IS_OK(status
)) {
1669 d_fprintf(stderr
, _("Request open_group failed"));
1673 if (!NT_STATUS_IS_OK(result
)) {
1675 d_fprintf(stderr
, _("Request open_group failed"));
1679 group_rid
= group_rids
.ids
[0];
1681 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
1685 if (!NT_STATUS_IS_OK(status
)) {
1687 _("Unable to query group members of %s"),
1692 if (!NT_STATUS_IS_OK(result
)) {
1695 _("Unable to query group members of %s"),
1700 if (c
->opt_verbose
) {
1702 _("Domain Group %s (rid: %d) has %d members\n"),
1703 argv
[0],group_rid
, rids
->count
);
1706 /* Check if group is anyone's primary group */
1707 for (i
= 0; i
< rids
->count
; i
++)
1709 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1711 MAXIMUM_ALLOWED_ACCESS
,
1715 if (!NT_STATUS_IS_OK(status
)) {
1717 _("Unable to open group member %d\n"),
1722 if (!NT_STATUS_IS_OK(result
)) {
1725 _("Unable to open group member %d\n"),
1730 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1735 if (!NT_STATUS_IS_OK(status
)) {
1737 _("Unable to lookup userinfo for group "
1743 if (!NT_STATUS_IS_OK(result
)) {
1746 _("Unable to lookup userinfo for group "
1752 if (info
->info21
.primary_gid
== group_rid
) {
1753 if (c
->opt_verbose
) {
1754 d_printf(_("Group is primary group "
1756 info
->info21
.account_name
.string
);
1758 group_is_primary
= true;
1761 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1764 if (group_is_primary
) {
1765 d_fprintf(stderr
, _("Unable to delete group because "
1766 "some of it's members have it as primary "
1768 status
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1772 /* remove all group members */
1773 for (i
= 0; i
< rids
->count
; i
++)
1776 d_printf(_("Remove group member %d..."),
1778 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
1782 if (!NT_STATUS_IS_OK(status
)) {
1786 if (NT_STATUS_IS_OK(result
)) {
1788 d_printf(_("ok\n"));
1791 d_printf("%s\n", _("failed"));
1796 status
= dcerpc_samr_DeleteDomainGroup(b
, mem_ctx
,
1799 if (!NT_STATUS_IS_OK(status
)) {
1806 /* removing a local group is easier... */
1807 case SID_NAME_ALIAS
:
1808 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
1810 MAXIMUM_ALLOWED_ACCESS
,
1814 if (!NT_STATUS_IS_OK(status
)) {
1815 d_fprintf(stderr
, _("Request open_alias failed\n"));
1818 if (!NT_STATUS_IS_OK(result
)) {
1820 d_fprintf(stderr
, _("Request open_alias failed\n"));
1824 status
= dcerpc_samr_DeleteDomAlias(b
, mem_ctx
,
1827 if (!NT_STATUS_IS_OK(status
)) {
1835 d_fprintf(stderr
, _("%s is of type %s. This command is only "
1836 "for deleting local or global groups\n"),
1837 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1838 status
= NT_STATUS_UNSUCCESSFUL
;
1842 if (NT_STATUS_IS_OK(status
)) {
1844 d_printf(_("Deleted %s '%s'\n"),
1845 sid_type_lookup(name_types
.ids
[0]), argv
[0]);
1847 d_fprintf(stderr
, _("Deleting of %s failed: %s\n"), argv
[0],
1848 get_friendly_nt_error_msg(status
));
1856 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1858 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1859 rpc_group_delete_internals
, argc
,argv
);
1862 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1864 NET_API_STATUS status
;
1865 struct GROUP_INFO_1 info1
;
1866 uint32_t parm_error
= 0;
1868 if (argc
!= 1 || c
->display_usage
) {
1869 rpc_group_usage(c
, argc
, argv
);
1875 info1
.grpi1_name
= argv
[0];
1876 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1877 info1
.grpi1_comment
= c
->opt_comment
;
1880 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1884 _("Failed to add group '%s' with error: %s.\n"),
1885 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1889 d_printf(_("Added group '%s'.\n"), argv
[0]);
1895 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1897 NET_API_STATUS status
;
1898 struct LOCALGROUP_INFO_1 info1
;
1899 uint32_t parm_error
= 0;
1901 if (argc
!= 1 || c
->display_usage
) {
1902 rpc_group_usage(c
, argc
, argv
);
1908 info1
.lgrpi1_name
= argv
[0];
1909 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1910 info1
.lgrpi1_comment
= c
->opt_comment
;
1913 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1917 _("Failed to add alias '%s' with error: %s.\n"),
1918 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1922 d_printf(_("Added alias '%s'.\n"), argv
[0]);
1928 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1930 if (c
->opt_localgroup
)
1931 return rpc_alias_add_internals(c
, argc
, argv
);
1933 return rpc_group_add_internals(c
, argc
, argv
);
1936 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1937 TALLOC_CTX
*mem_ctx
,
1939 struct dom_sid
*sid
,
1940 enum lsa_SidType
*type
)
1942 struct dom_sid
*sids
= NULL
;
1943 enum lsa_SidType
*types
= NULL
;
1944 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1945 struct policy_handle lsa_pol
;
1946 NTSTATUS status
, result
;
1947 struct dcerpc_binding_handle
*b
;
1949 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1951 if (!NT_STATUS_IS_OK(status
)) {
1955 b
= pipe_hnd
->binding_handle
;
1957 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1958 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1960 if (!NT_STATUS_IS_OK(status
)) {
1964 status
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1965 &name
, NULL
, 1, &sids
, &types
);
1967 if (NT_STATUS_IS_OK(status
)) {
1968 sid_copy(sid
, &sids
[0]);
1972 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1976 TALLOC_FREE(pipe_hnd
);
1979 if (!NT_STATUS_IS_OK(status
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
1981 /* Try as S-1-5-whatever */
1983 struct dom_sid tmp_sid
;
1985 if (string_to_sid(&tmp_sid
, name
)) {
1986 sid_copy(sid
, &tmp_sid
);
1987 *type
= SID_NAME_UNKNOWN
;
1988 status
= NT_STATUS_OK
;
1995 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
1996 TALLOC_CTX
*mem_ctx
,
1997 const struct dom_sid
*group_sid
,
2000 struct policy_handle connect_pol
, domain_pol
;
2001 NTSTATUS status
, result
;
2003 struct policy_handle group_pol
;
2004 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2006 struct samr_Ids rids
, rid_types
;
2007 struct lsa_String lsa_acct_name
;
2011 sid_copy(&sid
, group_sid
);
2013 if (!sid_split_rid(&sid
, &group_rid
)) {
2014 return NT_STATUS_UNSUCCESSFUL
;
2017 /* Get sam policy handle */
2018 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2020 MAXIMUM_ALLOWED_ACCESS
,
2023 if (!NT_STATUS_IS_OK(status
)) {
2026 if (!NT_STATUS_IS_OK(result
)) {
2030 /* Get domain policy handle */
2031 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2033 MAXIMUM_ALLOWED_ACCESS
,
2037 if (!NT_STATUS_IS_OK(status
)) {
2040 if (!NT_STATUS_IS_OK(result
)) {
2044 init_lsa_String(&lsa_acct_name
, member
);
2046 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2053 if (!NT_STATUS_IS_OK(status
)) {
2054 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2059 if (!NT_STATUS_IS_OK(result
)) {
2061 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2066 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2068 MAXIMUM_ALLOWED_ACCESS
,
2072 if (!NT_STATUS_IS_OK(status
)) {
2076 if (!NT_STATUS_IS_OK(result
)) {
2081 status
= dcerpc_samr_AddGroupMember(b
, mem_ctx
,
2084 0x0005, /* unknown flags */
2086 if (!NT_STATUS_IS_OK(status
)) {
2093 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2097 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2098 TALLOC_CTX
*mem_ctx
,
2099 const struct dom_sid
*alias_sid
,
2102 struct policy_handle connect_pol
, domain_pol
;
2103 NTSTATUS status
, result
;
2105 struct policy_handle alias_pol
;
2106 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2108 struct dom_sid member_sid
;
2109 enum lsa_SidType member_type
;
2113 sid_copy(&sid
, alias_sid
);
2115 if (!sid_split_rid(&sid
, &alias_rid
)) {
2116 return NT_STATUS_UNSUCCESSFUL
;
2119 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2120 member
, &member_sid
, &member_type
);
2122 if (!NT_STATUS_IS_OK(result
)) {
2123 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2128 /* Get sam policy handle */
2129 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2131 MAXIMUM_ALLOWED_ACCESS
,
2134 if (!NT_STATUS_IS_OK(status
)) {
2137 if (!NT_STATUS_IS_OK(result
)) {
2142 /* Get domain policy handle */
2143 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2145 MAXIMUM_ALLOWED_ACCESS
,
2149 if (!NT_STATUS_IS_OK(status
)) {
2152 if (!NT_STATUS_IS_OK(result
)) {
2157 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2159 MAXIMUM_ALLOWED_ACCESS
,
2163 if (!NT_STATUS_IS_OK(status
)) {
2166 if (!NT_STATUS_IS_OK(result
)) {
2170 status
= dcerpc_samr_AddAliasMember(b
, mem_ctx
,
2174 if (!NT_STATUS_IS_OK(status
)) {
2181 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2185 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
2186 const struct dom_sid
*domain_sid
,
2187 const char *domain_name
,
2188 struct cli_state
*cli
,
2189 struct rpc_pipe_client
*pipe_hnd
,
2190 TALLOC_CTX
*mem_ctx
,
2194 struct dom_sid group_sid
;
2195 enum lsa_SidType group_type
;
2197 if (argc
!= 2 || c
->display_usage
) {
2200 _("net rpc group addmem <group> <member>\n"
2201 " Add a member to a group\n"
2202 " group\tGroup to add member to\n"
2203 " member\tMember to add to group\n"));
2204 return NT_STATUS_UNSUCCESSFUL
;
2207 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2208 &group_sid
, &group_type
))) {
2209 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2211 return NT_STATUS_UNSUCCESSFUL
;
2214 if (group_type
== SID_NAME_DOM_GRP
) {
2215 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2216 &group_sid
, argv
[1]);
2218 if (!NT_STATUS_IS_OK(result
)) {
2219 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2220 argv
[1], argv
[0], nt_errstr(result
));
2225 if (group_type
== SID_NAME_ALIAS
) {
2226 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2227 &group_sid
, argv
[1]);
2229 if (!NT_STATUS_IS_OK(result
)) {
2230 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2231 argv
[1], argv
[0], nt_errstr(result
));
2236 d_fprintf(stderr
, _("Can only add members to global or local groups "
2237 "which %s is not\n"), argv
[0]);
2239 return NT_STATUS_UNSUCCESSFUL
;
2242 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2244 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2245 rpc_group_addmem_internals
,
2249 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2250 struct rpc_pipe_client
*pipe_hnd
,
2251 TALLOC_CTX
*mem_ctx
,
2252 const struct dom_sid
*group_sid
,
2255 struct policy_handle connect_pol
, domain_pol
;
2256 NTSTATUS status
, result
;
2258 struct policy_handle group_pol
;
2259 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2261 struct samr_Ids rids
, rid_types
;
2262 struct lsa_String lsa_acct_name
;
2266 sid_copy(&sid
, group_sid
);
2268 if (!sid_split_rid(&sid
, &group_rid
))
2269 return NT_STATUS_UNSUCCESSFUL
;
2271 /* Get sam policy handle */
2272 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2274 MAXIMUM_ALLOWED_ACCESS
,
2277 if (!NT_STATUS_IS_OK(status
)) {
2280 if (!NT_STATUS_IS_OK(result
)) {
2285 /* Get domain policy handle */
2286 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2288 MAXIMUM_ALLOWED_ACCESS
,
2292 if (!NT_STATUS_IS_OK(status
)) {
2295 if (!NT_STATUS_IS_OK(result
)) {
2299 init_lsa_String(&lsa_acct_name
, member
);
2301 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2308 if (!NT_STATUS_IS_OK(status
)) {
2309 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2314 if (!NT_STATUS_IS_OK(result
)) {
2316 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2321 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2323 MAXIMUM_ALLOWED_ACCESS
,
2327 if (!NT_STATUS_IS_OK(status
)) {
2330 if (!NT_STATUS_IS_OK(result
)) {
2335 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
2339 if (!NT_STATUS_IS_OK(status
)) {
2345 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2349 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2350 TALLOC_CTX
*mem_ctx
,
2351 const struct dom_sid
*alias_sid
,
2354 struct policy_handle connect_pol
, domain_pol
;
2355 NTSTATUS status
, result
;
2357 struct policy_handle alias_pol
;
2358 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2360 struct dom_sid member_sid
;
2361 enum lsa_SidType member_type
;
2365 sid_copy(&sid
, alias_sid
);
2367 if (!sid_split_rid(&sid
, &alias_rid
))
2368 return NT_STATUS_UNSUCCESSFUL
;
2370 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2371 member
, &member_sid
, &member_type
);
2373 if (!NT_STATUS_IS_OK(result
)) {
2374 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2379 /* Get sam policy handle */
2380 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2382 MAXIMUM_ALLOWED_ACCESS
,
2385 if (!NT_STATUS_IS_OK(status
)) {
2388 if (!NT_STATUS_IS_OK(result
)) {
2393 /* Get domain policy handle */
2394 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2396 MAXIMUM_ALLOWED_ACCESS
,
2400 if (!NT_STATUS_IS_OK(status
)) {
2403 if (!NT_STATUS_IS_OK(result
)) {
2408 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2410 MAXIMUM_ALLOWED_ACCESS
,
2414 if (!NT_STATUS_IS_OK(status
)) {
2418 if (!NT_STATUS_IS_OK(result
)) {
2422 status
= dcerpc_samr_DeleteAliasMember(b
, mem_ctx
,
2427 if (!NT_STATUS_IS_OK(status
)) {
2434 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2438 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2439 const struct dom_sid
*domain_sid
,
2440 const char *domain_name
,
2441 struct cli_state
*cli
,
2442 struct rpc_pipe_client
*pipe_hnd
,
2443 TALLOC_CTX
*mem_ctx
,
2447 struct dom_sid group_sid
;
2448 enum lsa_SidType group_type
;
2450 if (argc
!= 2 || c
->display_usage
) {
2453 _("net rpc group delmem <group> <member>\n"
2454 " Delete a member from a group\n"
2455 " group\tGroup to delete member from\n"
2456 " member\tMember to delete from group\n"));
2457 return NT_STATUS_UNSUCCESSFUL
;
2460 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2461 &group_sid
, &group_type
))) {
2462 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2464 return NT_STATUS_UNSUCCESSFUL
;
2467 if (group_type
== SID_NAME_DOM_GRP
) {
2468 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2469 &group_sid
, argv
[1]);
2471 if (!NT_STATUS_IS_OK(result
)) {
2472 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2473 argv
[1], argv
[0], nt_errstr(result
));
2478 if (group_type
== SID_NAME_ALIAS
) {
2479 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2480 &group_sid
, argv
[1]);
2482 if (!NT_STATUS_IS_OK(result
)) {
2483 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2484 argv
[1], argv
[0], nt_errstr(result
));
2489 d_fprintf(stderr
, _("Can only delete members from global or local "
2490 "groups which %s is not\n"), argv
[0]);
2492 return NT_STATUS_UNSUCCESSFUL
;
2495 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2497 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2498 rpc_group_delmem_internals
,
2503 * List groups on a remote RPC server.
2505 * All parameters are provided by the run_rpc_command function, except for
2506 * argc, argv which are passes through.
2508 * @param domain_sid The domain sid acquired from the remote server.
2509 * @param cli A cli_state connected to the server.
2510 * @param mem_ctx Talloc context, destroyed on completion of the function.
2511 * @param argc Standard main() style argc.
2512 * @param argv Standard main() style argv. Initial components are already
2515 * @return Normal NTSTATUS return.
2518 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2519 const struct dom_sid
*domain_sid
,
2520 const char *domain_name
,
2521 struct cli_state
*cli
,
2522 struct rpc_pipe_client
*pipe_hnd
,
2523 TALLOC_CTX
*mem_ctx
,
2527 struct policy_handle connect_pol
, domain_pol
;
2528 NTSTATUS status
, result
;
2529 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2530 struct samr_SamArray
*groups
= NULL
;
2531 bool global
= false;
2533 bool builtin
= false;
2534 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2536 if (c
->display_usage
) {
2539 _("net rpc group list [global] [local] [builtin]\n"
2540 " List groups on RPC server\n"
2541 " global\tList global groups\n"
2542 " local\tList local groups\n"
2543 " builtin\tList builtin groups\n"
2544 " If none of global, local or builtin is "
2545 "specified, all three options are considered "
2547 return NT_STATUS_OK
;
2556 for (i
=0; i
<argc
; i
++) {
2557 if (strequal(argv
[i
], "global"))
2560 if (strequal(argv
[i
], "local"))
2563 if (strequal(argv
[i
], "builtin"))
2567 /* Get sam policy handle */
2569 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2571 MAXIMUM_ALLOWED_ACCESS
,
2574 if (!NT_STATUS_IS_OK(status
)) {
2577 if (!NT_STATUS_IS_OK(result
)) {
2582 /* Get domain policy handle */
2584 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2586 MAXIMUM_ALLOWED_ACCESS
,
2587 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2590 if (!NT_STATUS_IS_OK(status
)) {
2593 if (!NT_STATUS_IS_OK(result
)) {
2598 /* Query domain groups */
2599 if (c
->opt_long_list_entries
)
2600 d_printf(_("\nGroup name Comment"
2601 "\n-----------------------------\n"));
2603 uint32_t max_size
, total_size
, returned_size
;
2604 union samr_DispInfo info
;
2608 dcerpc_get_query_dispinfo_params(
2609 loop_count
, &max_entries
, &max_size
);
2611 status
= dcerpc_samr_QueryDisplayInfo(b
, mem_ctx
,
2621 if (!NT_STATUS_IS_OK(status
)) {
2624 num_entries
= info
.info3
.count
;
2625 start_idx
+= info
.info3
.count
;
2627 if (!NT_STATUS_IS_OK(result
) &&
2628 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2631 for (i
= 0; i
< num_entries
; i
++) {
2633 const char *group
= NULL
;
2634 const char *desc
= NULL
;
2636 group
= info
.info3
.entries
[i
].account_name
.string
;
2637 desc
= info
.info3
.entries
[i
].description
.string
;
2639 if (c
->opt_long_list_entries
)
2640 printf("%-21.21s %-50.50s\n",
2643 printf("%s\n", group
);
2645 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2646 /* query domain aliases */
2651 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2658 if (!NT_STATUS_IS_OK(status
)) {
2661 if (!NT_STATUS_IS_OK(result
) &&
2662 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2665 for (i
= 0; i
< num_entries
; i
++) {
2667 const char *description
= NULL
;
2669 if (c
->opt_long_list_entries
) {
2671 struct policy_handle alias_pol
;
2672 union samr_AliasInfo
*info
= NULL
;
2675 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2678 groups
->entries
[i
].idx
,
2681 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2682 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2687 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2688 status
= dcerpc_samr_Close(b
, mem_ctx
,
2691 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2692 description
= info
->description
.string
;
2698 if (description
!= NULL
) {
2699 printf("%-21.21s %-50.50s\n",
2700 groups
->entries
[i
].name
.string
,
2703 printf("%s\n", groups
->entries
[i
].name
.string
);
2706 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2707 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
2708 /* Get builtin policy handle */
2710 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2712 MAXIMUM_ALLOWED_ACCESS
,
2713 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2716 if (!NT_STATUS_IS_OK(status
)) {
2719 if (!NT_STATUS_IS_OK(result
)) {
2724 /* query builtin aliases */
2727 if (!builtin
) break;
2729 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2736 if (!NT_STATUS_IS_OK(status
)) {
2739 if (!NT_STATUS_IS_OK(result
) &&
2740 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
2745 for (i
= 0; i
< num_entries
; i
++) {
2747 const char *description
= NULL
;
2749 if (c
->opt_long_list_entries
) {
2751 struct policy_handle alias_pol
;
2752 union samr_AliasInfo
*info
= NULL
;
2755 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2758 groups
->entries
[i
].idx
,
2761 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2762 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2767 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2768 status
= dcerpc_samr_Close(b
, mem_ctx
,
2771 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2772 description
= info
->description
.string
;
2778 if (description
!= NULL
) {
2779 printf("%-21.21s %-50.50s\n",
2780 groups
->entries
[i
].name
.string
,
2783 printf("%s\n", groups
->entries
[i
].name
.string
);
2786 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2794 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2796 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2797 rpc_group_list_internals
,
2801 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2802 struct rpc_pipe_client
*pipe_hnd
,
2803 TALLOC_CTX
*mem_ctx
,
2804 const char *domain_name
,
2805 const struct dom_sid
*domain_sid
,
2806 struct policy_handle
*domain_pol
,
2809 NTSTATUS result
, status
;
2810 struct policy_handle group_pol
;
2811 uint32 num_members
, *group_rids
;
2813 struct samr_RidAttrArray
*rids
= NULL
;
2814 struct lsa_Strings names
;
2815 struct samr_Ids types
;
2816 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2819 sid_to_fstring(sid_str
, domain_sid
);
2821 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2823 MAXIMUM_ALLOWED_ACCESS
,
2827 if (!NT_STATUS_IS_OK(status
)) {
2830 if (!NT_STATUS_IS_OK(result
)) {
2834 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
2838 if (!NT_STATUS_IS_OK(status
)) {
2841 if (!NT_STATUS_IS_OK(result
)) {
2845 num_members
= rids
->count
;
2846 group_rids
= rids
->rids
;
2848 while (num_members
> 0) {
2849 int this_time
= 512;
2851 if (num_members
< this_time
)
2852 this_time
= num_members
;
2854 status
= dcerpc_samr_LookupRids(b
, mem_ctx
,
2861 if (!NT_STATUS_IS_OK(status
)) {
2864 if (!NT_STATUS_IS_OK(result
)) {
2868 /* We only have users as members, but make the output
2869 the same as the output of alias members */
2871 for (i
= 0; i
< this_time
; i
++) {
2873 if (c
->opt_long_list_entries
) {
2874 printf("%s-%d %s\\%s %d\n", sid_str
,
2875 group_rids
[i
], domain_name
,
2876 names
.names
[i
].string
,
2879 printf("%s\\%s\n", domain_name
,
2880 names
.names
[i
].string
);
2884 num_members
-= this_time
;
2888 return NT_STATUS_OK
;
2891 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2892 struct rpc_pipe_client
*pipe_hnd
,
2893 TALLOC_CTX
*mem_ctx
,
2894 struct policy_handle
*domain_pol
,
2897 NTSTATUS result
, status
;
2898 struct rpc_pipe_client
*lsa_pipe
;
2899 struct policy_handle alias_pol
, lsa_pol
;
2901 struct dom_sid
*alias_sids
;
2904 enum lsa_SidType
*types
;
2906 struct lsa_SidArray sid_array
;
2907 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2909 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2911 MAXIMUM_ALLOWED_ACCESS
,
2915 if (!NT_STATUS_IS_OK(status
)) {
2918 if (!NT_STATUS_IS_OK(result
)) {
2922 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
2926 if (!NT_STATUS_IS_OK(status
)) {
2927 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2930 if (!NT_STATUS_IS_OK(result
)) {
2931 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2935 num_members
= sid_array
.num_sids
;
2937 if (num_members
== 0) {
2938 return NT_STATUS_OK
;
2941 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2942 &ndr_table_lsarpc
.syntax_id
,
2944 if (!NT_STATUS_IS_OK(result
)) {
2945 d_fprintf(stderr
, _("Couldn't open LSA pipe. Error was %s\n"),
2946 nt_errstr(result
) );
2950 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2951 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
2953 if (!NT_STATUS_IS_OK(result
)) {
2954 d_fprintf(stderr
, _("Couldn't open LSA policy handle\n"));
2955 TALLOC_FREE(lsa_pipe
);
2959 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, struct dom_sid
, num_members
);
2961 d_fprintf(stderr
, _("Out of memory\n"));
2962 TALLOC_FREE(lsa_pipe
);
2963 return NT_STATUS_NO_MEMORY
;
2966 for (i
=0; i
<num_members
; i
++) {
2967 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2970 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2971 num_members
, alias_sids
,
2972 &domains
, &names
, &types
);
2974 if (!NT_STATUS_IS_OK(result
) &&
2975 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2976 d_fprintf(stderr
, _("Couldn't lookup SIDs\n"));
2977 TALLOC_FREE(lsa_pipe
);
2981 for (i
= 0; i
< num_members
; i
++) {
2983 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2985 if (c
->opt_long_list_entries
) {
2986 printf("%s %s\\%s %d\n", sid_str
,
2987 domains
[i
] ? domains
[i
] : _("*unknown*"),
2988 names
[i
] ? names
[i
] : _("*unknown*"), types
[i
]);
2991 printf("%s\\%s\n", domains
[i
], names
[i
]);
2993 printf("%s\n", sid_str
);
2997 TALLOC_FREE(lsa_pipe
);
2998 return NT_STATUS_OK
;
3001 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
3002 const struct dom_sid
*domain_sid
,
3003 const char *domain_name
,
3004 struct cli_state
*cli
,
3005 struct rpc_pipe_client
*pipe_hnd
,
3006 TALLOC_CTX
*mem_ctx
,
3010 NTSTATUS result
, status
;
3011 struct policy_handle connect_pol
, domain_pol
;
3012 struct samr_Ids rids
, rid_types
;
3013 struct lsa_String lsa_acct_name
;
3014 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3016 /* Get sam policy handle */
3018 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
3020 MAXIMUM_ALLOWED_ACCESS
,
3023 if (!NT_STATUS_IS_OK(status
)) {
3026 if (!NT_STATUS_IS_OK(result
)) {
3030 /* Get domain policy handle */
3032 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3034 MAXIMUM_ALLOWED_ACCESS
,
3035 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
3038 if (!NT_STATUS_IS_OK(status
)) {
3041 if (!NT_STATUS_IS_OK(result
)) {
3045 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
3047 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3054 if (!NT_STATUS_IS_OK(status
)) {
3058 if (!NT_STATUS_IS_OK(result
)) {
3060 /* Ok, did not find it in the global sam, try with builtin */
3062 struct dom_sid sid_Builtin
;
3064 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
3066 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
3068 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3070 MAXIMUM_ALLOWED_ACCESS
,
3074 if (!NT_STATUS_IS_OK(status
)) {
3077 if (!NT_STATUS_IS_OK(result
)) {
3078 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3083 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3090 if (!NT_STATUS_IS_OK(status
)) {
3093 if (!NT_STATUS_IS_OK(result
)) {
3094 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3100 if (rids
.count
!= 1) {
3101 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3106 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
3107 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
3108 domain_sid
, &domain_pol
,
3112 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
3113 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
3117 return NT_STATUS_NO_SUCH_GROUP
;
3120 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
3122 if (argc
!= 1 || c
->display_usage
) {
3123 return rpc_group_usage(c
, argc
, argv
);
3126 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
3127 rpc_group_members_internals
,
3131 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
3133 NET_API_STATUS status
;
3134 struct GROUP_INFO_0 g0
;
3138 d_printf(_("Usage:\n"));
3139 d_printf("net rpc group rename group newname\n");
3143 g0
.grpi0_name
= argv
[1];
3145 status
= NetGroupSetInfo(c
->opt_host
,
3152 d_fprintf(stderr
, _("Renaming group %s failed with: %s\n"),
3153 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
3161 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
3163 if (argc
!= 2 || c
->display_usage
) {
3164 return rpc_group_usage(c
, argc
, argv
);
3167 return rpc_group_rename_internals(c
, argc
, argv
);
3171 * 'net rpc group' entrypoint.
3172 * @param argc Standard main() style argc.
3173 * @param argv Standard main() style argv. Initial components are already
3177 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
3179 NET_API_STATUS status
;
3181 struct functable func
[] = {
3186 N_("Create specified group"),
3187 N_("net rpc group add\n"
3188 " Create specified group")
3194 N_("Delete specified group"),
3195 N_("net rpc group delete\n"
3196 " Delete specified group")
3202 N_("Add member to group"),
3203 N_("net rpc group addmem\n"
3204 " Add member to group")
3210 N_("Remove member from group"),
3211 N_("net rpc group delmem\n"
3212 " Remove member from group")
3219 N_("net rpc group list\n"
3226 N_("List group members"),
3227 N_("net rpc group members\n"
3228 " List group members")
3235 N_("net rpc group rename\n"
3238 {NULL
, NULL
, 0, NULL
, NULL
}
3241 status
= libnetapi_net_init(&c
->netapi_ctx
);
3245 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
3246 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
3247 if (c
->opt_kerberos
) {
3248 libnetapi_set_use_kerberos(c
->netapi_ctx
);
3252 if (c
->display_usage
) {
3253 d_printf(_("Usage:\n"));
3254 d_printf(_("net rpc group\n"
3255 " Alias for net rpc group list global "
3256 "local builtin\n"));
3257 net_display_usage_from_functable(func
);
3261 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
3262 rpc_group_list_internals
,
3266 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
3269 /****************************************************************************/
3271 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
3273 return net_share_usage(c
, argc
, argv
);
3277 * Add a share on a remote RPC server.
3279 * @param argc Standard main() style argc.
3280 * @param argv Standard main() style argv. Initial components are already
3283 * @return A shell status integer (0 for success).
3286 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
3288 NET_API_STATUS status
;
3291 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3292 uint32 num_users
=0, perms
=0;
3293 char *password
=NULL
; /* don't allow a share password */
3294 struct SHARE_INFO_2 i2
;
3295 uint32_t parm_error
= 0;
3297 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
3298 return rpc_share_usage(c
, argc
, argv
);
3301 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
3305 path
= strchr(sharename
, '=');
3312 i2
.shi2_netname
= sharename
;
3313 i2
.shi2_type
= type
;
3314 i2
.shi2_remark
= c
->opt_comment
;
3315 i2
.shi2_permissions
= perms
;
3316 i2
.shi2_max_uses
= c
->opt_maxusers
;
3317 i2
.shi2_current_uses
= num_users
;
3318 i2
.shi2_path
= path
;
3319 i2
.shi2_passwd
= password
;
3321 status
= NetShareAdd(c
->opt_host
,
3326 printf(_("NetShareAdd failed with: %s\n"),
3327 libnetapi_get_error_string(c
->netapi_ctx
, status
));
3334 * Delete a share on a remote RPC server.
3336 * @param domain_sid The domain sid acquired from the remote server.
3337 * @param argc Standard main() style argc.
3338 * @param argv Standard main() style argv. Initial components are already
3341 * @return A shell status integer (0 for success).
3343 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3345 if (argc
< 1 || c
->display_usage
) {
3346 return rpc_share_usage(c
, argc
, argv
);
3349 return NetShareDel(c
->opt_host
, argv
[0], 0);
3353 * Formatted print of share info
3355 * @param r pointer to SHARE_INFO_1 to format
3358 static void display_share_info_1(struct net_context
*c
,
3359 struct SHARE_INFO_1
*r
)
3361 if (c
->opt_long_list_entries
) {
3362 d_printf("%-12s %-8.8s %-50s\n",
3364 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
3367 d_printf("%s\n", r
->shi1_netname
);
3371 static WERROR
get_share_info(struct net_context
*c
,
3372 struct rpc_pipe_client
*pipe_hnd
,
3373 TALLOC_CTX
*mem_ctx
,
3377 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3381 union srvsvc_NetShareInfo info
;
3382 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3384 /* no specific share requested, enumerate all */
3387 uint32_t preferred_len
= 0xffffffff;
3388 uint32_t total_entries
= 0;
3389 uint32_t resume_handle
= 0;
3391 info_ctr
->level
= level
;
3393 status
= dcerpc_srvsvc_NetShareEnumAll(b
, mem_ctx
,
3400 if (!NT_STATUS_IS_OK(status
)) {
3401 return ntstatus_to_werror(status
);
3406 /* request just one share */
3407 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
3414 if (!NT_STATUS_IS_OK(status
)) {
3415 result
= ntstatus_to_werror(status
);
3419 if (!W_ERROR_IS_OK(result
)) {
3424 ZERO_STRUCTP(info_ctr
);
3426 info_ctr
->level
= level
;
3431 struct srvsvc_NetShareCtr1
*ctr1
;
3433 ctr1
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr1
);
3434 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3437 ctr1
->array
= info
.info1
;
3439 info_ctr
->ctr
.ctr1
= ctr1
;
3445 struct srvsvc_NetShareCtr2
*ctr2
;
3447 ctr2
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr2
);
3448 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3451 ctr2
->array
= info
.info2
;
3453 info_ctr
->ctr
.ctr2
= ctr2
;
3459 struct srvsvc_NetShareCtr502
*ctr502
;
3461 ctr502
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr502
);
3462 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3465 ctr502
->array
= info
.info502
;
3467 info_ctr
->ctr
.ctr502
= ctr502
;
3477 * 'net rpc share list' entrypoint.
3478 * @param argc Standard main() style argc.
3479 * @param argv Standard main() style argv. Initial components are already
3482 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3484 NET_API_STATUS status
;
3485 struct SHARE_INFO_1
*i1
= NULL
;
3486 uint32_t entries_read
= 0;
3487 uint32_t total_entries
= 0;
3488 uint32_t resume_handle
= 0;
3489 uint32_t i
, level
= 1;
3491 if (c
->display_usage
) {
3493 "net rpc share list\n"
3496 _("List shares on remote server"));
3500 status
= NetShareEnum(c
->opt_host
,
3502 (uint8_t **)(void *)&i1
,
3511 /* Display results */
3513 if (c
->opt_long_list_entries
) {
3515 "\nEnumerating shared resources (exports) on remote server:\n\n"
3516 "\nShare name Type Description\n"
3517 "---------- ---- -----------\n"));
3519 for (i
= 0; i
< entries_read
; i
++)
3520 display_share_info_1(c
, &i1
[i
]);
3525 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3529 status
= cli_tcon_andx(cli
, netname
, "A:", "", 0);
3530 if (!NT_STATUS_IS_OK(status
)) {
3531 d_printf(_("skipping [%s]: not a file share.\n"), netname
);
3535 status
= cli_tdis(cli
);
3536 if (!NT_STATUS_IS_OK(status
)) {
3537 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status
));
3544 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3545 const char *netname
, uint32 type
)
3547 /* only support disk shares */
3548 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3549 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname
,
3554 /* skip builtin shares */
3555 /* FIXME: should print$ be added too ? */
3556 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3557 strequal(netname
,"global"))
3560 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3561 printf(_("excluding [%s]\n"), netname
);
3565 return check_share_availability(cli
, netname
);
3569 * Migrate shares from a remote RPC server to the local RPC server.
3571 * All parameters are provided by the run_rpc_command function, except for
3572 * argc, argv which are passed through.
3574 * @param domain_sid The domain sid acquired from the remote server.
3575 * @param cli A cli_state connected to the server.
3576 * @param mem_ctx Talloc context, destroyed on completion of the function.
3577 * @param argc Standard main() style argc.
3578 * @param argv Standard main() style argv. Initial components are already
3581 * @return Normal NTSTATUS return.
3584 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3585 const struct dom_sid
*domain_sid
,
3586 const char *domain_name
,
3587 struct cli_state
*cli
,
3588 struct rpc_pipe_client
*pipe_hnd
,
3589 TALLOC_CTX
*mem_ctx
,
3594 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3595 struct srvsvc_NetShareInfoCtr ctr_src
;
3597 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3598 struct cli_state
*cli_dst
= NULL
;
3599 uint32 level
= 502; /* includes secdesc */
3600 uint32_t parm_error
= 0;
3601 struct dcerpc_binding_handle
*b
;
3603 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3605 if (!W_ERROR_IS_OK(result
))
3608 /* connect destination PI_SRVSVC */
3609 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3610 &ndr_table_srvsvc
.syntax_id
);
3611 if (!NT_STATUS_IS_OK(nt_status
))
3614 b
= srvsvc_pipe
->binding_handle
;
3616 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3618 union srvsvc_NetShareInfo info
;
3619 struct srvsvc_NetShareInfo502 info502
=
3620 ctr_src
.ctr
.ctr502
->array
[i
];
3622 /* reset error-code */
3623 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3625 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3628 /* finally add the share on the dst server */
3630 printf(_("migrating: [%s], path: %s, comment: %s, without "
3632 info502
.name
, info502
.path
, info502
.comment
);
3634 info
.info502
= &info502
;
3636 nt_status
= dcerpc_srvsvc_NetShareAdd(b
, mem_ctx
,
3637 srvsvc_pipe
->desthost
,
3642 if (!NT_STATUS_IS_OK(nt_status
)) {
3643 printf(_("cannot add share: %s\n"),
3644 nt_errstr(nt_status
));
3647 if (W_ERROR_V(result
) == W_ERROR_V(WERR_FILE_EXISTS
)) {
3648 printf(_(" [%s] does already exist\n"),
3653 if (!W_ERROR_IS_OK(result
)) {
3654 nt_status
= werror_to_ntstatus(result
);
3655 printf(_("cannot add share: %s\n"),
3656 win_errstr(result
));
3662 nt_status
= NT_STATUS_OK
;
3666 cli_shutdown(cli_dst
);
3674 * Migrate shares from a RPC server to another.
3676 * @param argc Standard main() style argc.
3677 * @param argv Standard main() style argv. Initial components are already
3680 * @return A shell status integer (0 for success).
3682 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3685 if (c
->display_usage
) {
3687 "net rpc share migrate shares\n"
3690 _("Migrate shares to local server"));
3695 printf(_("no server to migrate\n"));
3699 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3700 rpc_share_migrate_shares_internals
,
3707 * @param f file_info
3708 * @param mask current search mask
3709 * @param state arg-pointer
3712 static NTSTATUS
copy_fn(const char *mnt
, struct file_info
*f
,
3713 const char *mask
, void *state
)
3715 static NTSTATUS nt_status
;
3716 static struct copy_clistate
*local_state
;
3717 static fstring filename
, new_mask
;
3720 struct net_context
*c
;
3722 local_state
= (struct copy_clistate
*)state
;
3723 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3727 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3728 return NT_STATUS_OK
;
3730 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3733 if (f
->mode
& aDIR
) {
3735 DEBUG(3,("got dir: %s\n", f
->name
));
3737 fstrcpy(dir
, local_state
->cwd
);
3739 fstrcat(dir
, f
->name
);
3741 switch (net_mode_share
)
3743 case NET_MODE_SHARE_MIGRATE
:
3744 /* create that directory */
3745 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3746 local_state
->cli_share_src
,
3747 local_state
->cli_share_dst
,
3749 c
->opt_acls
? true : false,
3750 c
->opt_attrs
? true : false,
3751 c
->opt_timestamps
? true:false,
3755 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3756 return NT_STATUS_INTERNAL_ERROR
;
3759 if (!NT_STATUS_IS_OK(nt_status
)) {
3760 printf(_("could not handle dir %s: %s\n"),
3761 dir
, nt_errstr(nt_status
));
3765 /* search below that directory */
3766 fstrcpy(new_mask
, dir
);
3767 fstrcat(new_mask
, "\\*");
3769 old_dir
= local_state
->cwd
;
3770 local_state
->cwd
= dir
;
3771 nt_status
= sync_files(local_state
, new_mask
);
3772 if (!NT_STATUS_IS_OK(nt_status
)) {
3773 printf(_("could not handle files\n"));
3775 local_state
->cwd
= old_dir
;
3782 fstrcpy(filename
, local_state
->cwd
);
3783 fstrcat(filename
, "\\");
3784 fstrcat(filename
, f
->name
);
3786 DEBUG(3,("got file: %s\n", filename
));
3788 switch (net_mode_share
)
3790 case NET_MODE_SHARE_MIGRATE
:
3791 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3792 local_state
->cli_share_src
,
3793 local_state
->cli_share_dst
,
3795 c
->opt_acls
? true : false,
3796 c
->opt_attrs
? true : false,
3797 c
->opt_timestamps
? true: false,
3801 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3803 return NT_STATUS_INTERNAL_ERROR
;
3806 if (!NT_STATUS_IS_OK(nt_status
))
3807 printf(_("could not handle file %s: %s\n"),
3808 filename
, nt_errstr(nt_status
));
3813 * sync files, can be called recursivly to list files
3814 * and then call copy_fn for each file
3816 * @param cp_clistate pointer to the copy_clistate we work with
3817 * @param mask the current search mask
3819 * @return Boolean result
3821 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3823 struct cli_state
*targetcli
;
3824 char *targetpath
= NULL
;
3827 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3829 if ( !cli_resolve_path(talloc_tos(), "", NULL
, cp_clistate
->cli_share_src
,
3830 mask
, &targetcli
, &targetpath
) ) {
3831 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3833 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3834 return cli_nt_error(cp_clistate
->cli_share_src
);
3837 status
= cli_list(targetcli
, targetpath
, cp_clistate
->attribute
,
3838 copy_fn
, cp_clistate
);
3839 if (!NT_STATUS_IS_OK(status
)) {
3840 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3841 mask
, nt_errstr(status
));
3849 * Set the top level directory permissions before we do any further copies.
3850 * Should set up ACL inheritance.
3853 bool copy_top_level_perms(struct net_context
*c
,
3854 struct copy_clistate
*cp_clistate
,
3855 const char *sharename
)
3857 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3859 switch (net_mode_share
) {
3860 case NET_MODE_SHARE_MIGRATE
:
3861 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3862 nt_status
= net_copy_fileattr(c
,
3863 cp_clistate
->mem_ctx
,
3864 cp_clistate
->cli_share_src
,
3865 cp_clistate
->cli_share_dst
,
3867 c
->opt_acls
? true : false,
3868 c
->opt_attrs
? true : false,
3869 c
->opt_timestamps
? true: false,
3873 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3877 if (!NT_STATUS_IS_OK(nt_status
)) {
3878 printf(_("Could handle directory attributes for top level "
3879 "directory of share %s. Error %s\n"),
3880 sharename
, nt_errstr(nt_status
));
3888 * Sync all files inside a remote share to another share (over smb).
3890 * All parameters are provided by the run_rpc_command function, except for
3891 * argc, argv which are passed through.
3893 * @param domain_sid The domain sid acquired from the remote server.
3894 * @param cli A cli_state connected to the server.
3895 * @param mem_ctx Talloc context, destroyed on completion of the function.
3896 * @param argc Standard main() style argc.
3897 * @param argv Standard main() style argv. Initial components are already
3900 * @return Normal NTSTATUS return.
3903 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3904 const struct dom_sid
*domain_sid
,
3905 const char *domain_name
,
3906 struct cli_state
*cli
,
3907 struct rpc_pipe_client
*pipe_hnd
,
3908 TALLOC_CTX
*mem_ctx
,
3913 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3914 struct srvsvc_NetShareInfoCtr ctr_src
;
3917 struct copy_clistate cp_clistate
;
3918 bool got_src_share
= false;
3919 bool got_dst_share
= false;
3920 const char *mask
= "\\*";
3923 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3925 nt_status
= NT_STATUS_NO_MEMORY
;
3929 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3932 if (!W_ERROR_IS_OK(result
))
3935 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3937 struct srvsvc_NetShareInfo502 info502
=
3938 ctr_src
.ctr
.ctr502
->array
[i
];
3940 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3943 /* one might not want to mirror whole discs :) */
3944 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3945 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3950 switch (net_mode_share
)
3952 case NET_MODE_SHARE_MIGRATE
:
3956 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3960 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3963 c
->opt_acls
? _("including") : _("without"),
3964 c
->opt_attrs
? _("including") : _("without"),
3965 c
->opt_timestamps
? _("(preserving timestamps)") : "");
3967 cp_clistate
.mem_ctx
= mem_ctx
;
3968 cp_clistate
.cli_share_src
= NULL
;
3969 cp_clistate
.cli_share_dst
= NULL
;
3970 cp_clistate
.cwd
= NULL
;
3971 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3974 /* open share source */
3975 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3976 &cli
->dest_ss
, cli
->desthost
,
3977 info502
.name
, "A:");
3978 if (!NT_STATUS_IS_OK(nt_status
))
3981 got_src_share
= true;
3983 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3984 /* open share destination */
3985 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3986 NULL
, dst
, info502
.name
, "A:");
3987 if (!NT_STATUS_IS_OK(nt_status
))
3990 got_dst_share
= true;
3993 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3994 d_fprintf(stderr
, _("Could not handle the top level "
3995 "directory permissions for the "
3996 "share: %s\n"), info502
.name
);
3997 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4001 nt_status
= sync_files(&cp_clistate
, mask
);
4002 if (!NT_STATUS_IS_OK(nt_status
)) {
4003 d_fprintf(stderr
, _("could not handle files for share: "
4004 "%s\n"), info502
.name
);
4009 nt_status
= NT_STATUS_OK
;
4014 cli_shutdown(cp_clistate
.cli_share_src
);
4017 cli_shutdown(cp_clistate
.cli_share_dst
);
4024 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
4026 if (c
->display_usage
) {
4028 "net share migrate files\n"
4031 _("Migrate files to local server"));
4036 d_printf(_("no server to migrate\n"));
4040 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4041 rpc_share_migrate_files_internals
,
4046 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4048 * All parameters are provided by the run_rpc_command function, except for
4049 * argc, argv which are passed through.
4051 * @param domain_sid The domain sid acquired from the remote server.
4052 * @param cli A cli_state connected to the server.
4053 * @param mem_ctx Talloc context, destroyed on completion of the function.
4054 * @param argc Standard main() style argc.
4055 * @param argv Standard main() style argv. Initial components are already
4058 * @return Normal NTSTATUS return.
4061 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
4062 const struct dom_sid
*domain_sid
,
4063 const char *domain_name
,
4064 struct cli_state
*cli
,
4065 struct rpc_pipe_client
*pipe_hnd
,
4066 TALLOC_CTX
*mem_ctx
,
4071 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
4072 struct srvsvc_NetShareInfoCtr ctr_src
;
4073 union srvsvc_NetShareInfo info
;
4075 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
4076 struct cli_state
*cli_dst
= NULL
;
4077 uint32 level
= 502; /* includes secdesc */
4078 uint32_t parm_error
= 0;
4079 struct dcerpc_binding_handle
*b
;
4081 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
4084 if (!W_ERROR_IS_OK(result
))
4087 /* connect destination PI_SRVSVC */
4088 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
4089 &ndr_table_srvsvc
.syntax_id
);
4090 if (!NT_STATUS_IS_OK(nt_status
))
4093 b
= srvsvc_pipe
->binding_handle
;
4095 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
4097 struct srvsvc_NetShareInfo502 info502
=
4098 ctr_src
.ctr
.ctr502
->array
[i
];
4100 /* reset error-code */
4101 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4103 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
4106 printf(_("migrating: [%s], path: %s, comment: %s, including "
4108 info502
.name
, info502
.path
, info502
.comment
);
4111 display_sec_desc(info502
.sd_buf
.sd
);
4113 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4114 info
.info502
= &info502
;
4116 /* finally modify the share on the dst server */
4117 nt_status
= dcerpc_srvsvc_NetShareSetInfo(b
, mem_ctx
,
4118 srvsvc_pipe
->desthost
,
4124 if (!NT_STATUS_IS_OK(nt_status
)) {
4125 printf(_("cannot set share-acl: %s\n"),
4126 nt_errstr(nt_status
));
4129 if (!W_ERROR_IS_OK(result
)) {
4130 nt_status
= werror_to_ntstatus(result
);
4131 printf(_("cannot set share-acl: %s\n"),
4132 win_errstr(result
));
4138 nt_status
= NT_STATUS_OK
;
4142 cli_shutdown(cli_dst
);
4150 * Migrate share-acls from a RPC server to another.
4152 * @param argc Standard main() style argc.
4153 * @param argv Standard main() style argv. Initial components are already
4156 * @return A shell status integer (0 for success).
4158 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
4161 if (c
->display_usage
) {
4163 "net rpc share migrate security\n"
4166 _("Migrate share-acls to local server"));
4171 d_printf(_("no server to migrate\n"));
4175 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4176 rpc_share_migrate_security_internals
,
4181 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4182 * from one server to another.
4184 * @param argc Standard main() style argc.
4185 * @param argv Standard main() style argv. Initial components are already
4188 * @return A shell status integer (0 for success).
4191 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
4196 if (c
->display_usage
) {
4198 "net rpc share migrate all\n"
4201 _("Migrates shares including all share settings"));
4206 d_printf(_("no server to migrate\n"));
4210 /* order is important. we don't want to be locked out by the share-acl
4211 * before copying files - gd */
4213 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4214 rpc_share_migrate_shares_internals
, argc
, argv
);
4218 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4219 rpc_share_migrate_files_internals
, argc
, argv
);
4223 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4224 rpc_share_migrate_security_internals
, argc
,
4230 * 'net rpc share migrate' entrypoint.
4231 * @param argc Standard main() style argc.
4232 * @param argv Standard main() style argv. Initial components are already
4235 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4238 struct functable func
[] = {
4241 rpc_share_migrate_all
,
4243 N_("Migrate shares from remote to local server"),
4244 N_("net rpc share migrate all\n"
4245 " Migrate shares from remote to local server")
4249 rpc_share_migrate_files
,
4251 N_("Migrate files from remote to local server"),
4252 N_("net rpc share migrate files\n"
4253 " Migrate files from remote to local server")
4257 rpc_share_migrate_security
,
4259 N_("Migrate share-ACLs from remote to local server"),
4260 N_("net rpc share migrate security\n"
4261 " Migrate share-ACLs from remote to local server")
4265 rpc_share_migrate_shares
,
4267 N_("Migrate shares from remote to local server"),
4268 N_("net rpc share migrate shares\n"
4269 " Migrate shares from remote to local server")
4271 {NULL
, NULL
, 0, NULL
, NULL
}
4274 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4276 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
4282 struct dom_sid
*members
;
4285 static int num_server_aliases
;
4286 static struct full_alias
*server_aliases
;
4289 * Add an alias to the static list.
4291 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4293 if (server_aliases
== NULL
)
4294 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4296 server_aliases
[num_server_aliases
] = *alias
;
4297 num_server_aliases
+= 1;
4301 * For a specific domain on the server, fetch all the aliases
4302 * and their members. Add all of them to the server_aliases.
4305 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4306 TALLOC_CTX
*mem_ctx
,
4307 struct policy_handle
*connect_pol
,
4308 const struct dom_sid
*domain_sid
)
4310 uint32 start_idx
, max_entries
, num_entries
, i
;
4311 struct samr_SamArray
*groups
= NULL
;
4312 NTSTATUS result
, status
;
4313 struct policy_handle domain_pol
;
4314 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4316 /* Get domain policy handle */
4318 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
4320 MAXIMUM_ALLOWED_ACCESS
,
4321 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
4324 if (!NT_STATUS_IS_OK(status
)) {
4327 if (!NT_STATUS_IS_OK(result
)) {
4335 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
4342 if (!NT_STATUS_IS_OK(status
)) {
4345 for (i
= 0; i
< num_entries
; i
++) {
4347 struct policy_handle alias_pol
;
4348 struct full_alias alias
;
4349 struct lsa_SidArray sid_array
;
4353 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
4355 MAXIMUM_ALLOWED_ACCESS
,
4356 groups
->entries
[i
].idx
,
4359 if (!NT_STATUS_IS_OK(status
)) {
4362 if (!NT_STATUS_IS_OK(_result
)) {
4367 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
4371 if (!NT_STATUS_IS_OK(status
)) {
4374 if (!NT_STATUS_IS_OK(_result
)) {
4379 alias
.num_members
= sid_array
.num_sids
;
4381 status
= dcerpc_samr_Close(b
, mem_ctx
, &alias_pol
, &_result
);
4382 if (!NT_STATUS_IS_OK(status
)) {
4385 if (!NT_STATUS_IS_OK(_result
)) {
4390 alias
.members
= NULL
;
4392 if (alias
.num_members
> 0) {
4393 alias
.members
= SMB_MALLOC_ARRAY(struct dom_sid
, alias
.num_members
);
4395 for (j
= 0; j
< alias
.num_members
; j
++)
4396 sid_copy(&alias
.members
[j
],
4397 sid_array
.sids
[j
].sid
);
4400 sid_compose(&alias
.sid
, domain_sid
,
4401 groups
->entries
[i
].idx
);
4403 push_alias(mem_ctx
, &alias
);
4405 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4407 status
= NT_STATUS_OK
;
4410 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
4416 * Dump server_aliases as names for debugging purposes.
4419 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4420 const struct dom_sid
*domain_sid
,
4421 const char *domain_name
,
4422 struct cli_state
*cli
,
4423 struct rpc_pipe_client
*pipe_hnd
,
4424 TALLOC_CTX
*mem_ctx
,
4430 struct policy_handle lsa_pol
;
4431 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4433 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4434 SEC_FLAG_MAXIMUM_ALLOWED
,
4436 if (!NT_STATUS_IS_OK(result
))
4439 for (i
=0; i
<num_server_aliases
; i
++) {
4442 enum lsa_SidType
*types
;
4445 struct full_alias
*alias
= &server_aliases
[i
];
4447 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4449 &domains
, &names
, &types
);
4450 if (!NT_STATUS_IS_OK(result
))
4453 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4455 if (alias
->num_members
== 0) {
4460 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4463 &domains
, &names
, &types
);
4465 if (!NT_STATUS_IS_OK(result
) &&
4466 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4469 for (j
=0; j
<alias
->num_members
; j
++)
4470 DEBUG(1, ("%s\\%s (%d); ",
4471 domains
[j
] ? domains
[j
] : "*unknown*",
4472 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4476 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
4478 return NT_STATUS_OK
;
4482 * Fetch a list of all server aliases and their members into
4486 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4487 const struct dom_sid
*domain_sid
,
4488 const char *domain_name
,
4489 struct cli_state
*cli
,
4490 struct rpc_pipe_client
*pipe_hnd
,
4491 TALLOC_CTX
*mem_ctx
,
4495 NTSTATUS result
, status
;
4496 struct policy_handle connect_pol
;
4497 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4499 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
4501 MAXIMUM_ALLOWED_ACCESS
,
4504 if (!NT_STATUS_IS_OK(status
)) {
4507 if (!NT_STATUS_IS_OK(result
)) {
4512 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4513 &global_sid_Builtin
);
4514 if (!NT_STATUS_IS_OK(status
)) {
4518 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4521 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
4526 static void init_user_token(struct security_token
*token
, struct dom_sid
*user_sid
)
4528 token
->num_sids
= 4;
4530 if (!(token
->sids
= SMB_MALLOC_ARRAY(struct dom_sid
, 4))) {
4531 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4532 token
->num_sids
= 0;
4536 token
->sids
[0] = *user_sid
;
4537 sid_copy(&token
->sids
[1], &global_sid_World
);
4538 sid_copy(&token
->sids
[2], &global_sid_Network
);
4539 sid_copy(&token
->sids
[3], &global_sid_Authenticated_Users
);
4542 static void free_user_token(struct security_token
*token
)
4544 SAFE_FREE(token
->sids
);
4547 static void add_sid_to_token(struct security_token
*token
, struct dom_sid
*sid
)
4549 if (security_token_has_sid(token
, sid
))
4552 token
->sids
= SMB_REALLOC_ARRAY(token
->sids
, struct dom_sid
, token
->num_sids
+1);
4557 sid_copy(&token
->sids
[token
->num_sids
], sid
);
4559 token
->num_sids
+= 1;
4564 struct security_token token
;
4567 static void dump_user_token(struct user_token
*token
)
4571 d_printf("%s\n", token
->name
);
4573 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4574 d_printf(" %s\n", sid_string_tos(&token
->token
.sids
[i
]));
4578 static bool is_alias_member(struct dom_sid
*sid
, struct full_alias
*alias
)
4582 for (i
=0; i
<alias
->num_members
; i
++) {
4583 if (dom_sid_compare(sid
, &alias
->members
[i
]) == 0)
4590 static void collect_sid_memberships(struct security_token
*token
, struct dom_sid sid
)
4594 for (i
=0; i
<num_server_aliases
; i
++) {
4595 if (is_alias_member(&sid
, &server_aliases
[i
]))
4596 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4601 * We got a user token with all the SIDs we can know about without asking the
4602 * server directly. These are the user and domain group sids. All of these can
4603 * be members of aliases. So scan the list of aliases for each of the SIDs and
4604 * add them to the token.
4607 static void collect_alias_memberships(struct security_token
*token
)
4609 int num_global_sids
= token
->num_sids
;
4612 for (i
=0; i
<num_global_sids
; i
++) {
4613 collect_sid_memberships(token
, token
->sids
[i
]);
4617 static bool get_user_sids(const char *domain
, const char *user
, struct security_token
*token
)
4619 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4620 enum wbcSidType type
;
4622 struct wbcDomainSid wsid
;
4623 char sid_str
[WBC_SID_STRING_BUFLEN
];
4624 struct dom_sid user_sid
;
4625 uint32_t num_groups
;
4626 gid_t
*groups
= NULL
;
4629 fstr_sprintf(full_name
, "%s%c%s",
4630 domain
, *lp_winbind_separator(), user
);
4632 /* First let's find out the user sid */
4634 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4636 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4637 DEBUG(1, ("winbind could not find %s: %s\n",
4638 full_name
, wbcErrorString(wbc_status
)));
4642 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4644 if (type
!= WBC_SID_NAME_USER
) {
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 init_user_token(token
, &user_sid
);
4656 /* And now the groups winbind knows about */
4658 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4659 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4660 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4661 full_name
, wbcErrorString(wbc_status
)));
4665 for (i
= 0; i
< num_groups
; i
++) {
4666 gid_t gid
= groups
[i
];
4669 wbc_status
= wbcGidToSid(gid
, &wsid
);
4670 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4671 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4672 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4673 wbcFreeMemory(groups
);
4677 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4679 DEBUG(3, (" %s\n", sid_str
));
4681 string_to_sid(&sid
, sid_str
);
4682 add_sid_to_token(token
, &sid
);
4684 wbcFreeMemory(groups
);
4690 * Get a list of all user tokens we want to look at
4693 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4694 struct user_token
**user_tokens
)
4696 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4697 uint32_t i
, num_users
;
4699 struct user_token
*result
;
4700 TALLOC_CTX
*frame
= NULL
;
4702 if (lp_winbind_use_default_domain() &&
4703 (c
->opt_target_workgroup
== NULL
)) {
4704 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4705 "please specify a workgroup\n"));
4709 /* Send request to winbind daemon */
4711 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4712 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4713 DEBUG(1, (_("winbind could not list users: %s\n"),
4714 wbcErrorString(wbc_status
)));
4718 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4720 if (result
== NULL
) {
4721 DEBUG(1, ("Could not malloc sid array\n"));
4722 wbcFreeMemory(users
);
4726 frame
= talloc_stackframe();
4727 for (i
=0; i
< num_users
; i
++) {
4728 fstring domain
, user
;
4731 fstrcpy(result
[i
].name
, users
[i
]);
4733 p
= strchr(users
[i
], *lp_winbind_separator());
4735 DEBUG(3, ("%s\n", users
[i
]));
4738 fstrcpy(domain
, c
->opt_target_workgroup
);
4739 fstrcpy(user
, users
[i
]);
4742 fstrcpy(domain
, users
[i
]);
4747 get_user_sids(domain
, user
, &(result
[i
].token
));
4750 wbcFreeMemory(users
);
4752 *num_tokens
= num_users
;
4753 *user_tokens
= result
;
4758 static bool get_user_tokens_from_file(FILE *f
,
4760 struct user_token
**tokens
)
4762 struct user_token
*token
= NULL
;
4767 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4771 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4772 line
[strlen(line
)-1] = '\0';
4775 if (line
[0] == ' ') {
4779 if(!string_to_sid(&sid
, &line
[1])) {
4780 DEBUG(1,("get_user_tokens_from_file: Could "
4781 "not convert sid %s \n",&line
[1]));
4785 if (token
== NULL
) {
4786 DEBUG(0, ("File does not begin with username"));
4790 add_sid_to_token(&token
->token
, &sid
);
4794 /* And a new user... */
4797 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4798 if (*tokens
== NULL
) {
4799 DEBUG(0, ("Could not realloc tokens\n"));
4803 token
= &((*tokens
)[*num_tokens
-1]);
4805 fstrcpy(token
->name
, line
);
4806 token
->token
.num_sids
= 0;
4807 token
->token
.sids
= NULL
;
4816 * Show the list of all users that have access to a share
4819 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4820 TALLOC_CTX
*mem_ctx
,
4821 const char *netname
,
4823 struct user_token
*tokens
)
4826 struct security_descriptor
*share_sd
= NULL
;
4827 struct security_descriptor
*root_sd
= NULL
;
4828 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4830 union srvsvc_NetShareInfo info
;
4834 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4836 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
4843 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4844 DEBUG(1, ("Coult not query secdesc for share %s\n",
4849 share_sd
= info
.info502
->sd_buf
.sd
;
4850 if (share_sd
== NULL
) {
4851 DEBUG(1, ("Got no secdesc for share %s\n",
4857 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli
, netname
, "A:", "", 0))) {
4861 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4862 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4863 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4866 for (i
=0; i
<num_tokens
; i
++) {
4869 if (share_sd
!= NULL
) {
4870 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4873 if (!NT_STATUS_IS_OK(status
)) {
4874 DEBUG(1, ("Could not check share_sd for "
4881 if (root_sd
== NULL
) {
4882 d_printf(" %s\n", tokens
[i
].name
);
4886 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4888 if (!NT_STATUS_IS_OK(status
)) {
4889 DEBUG(1, ("Could not check root_sd for user %s\n",
4893 d_printf(" %s\n", tokens
[i
].name
);
4896 if (fnum
!= (uint16_t)-1)
4897 cli_close(cli
, fnum
);
4909 static void collect_share(const char *name
, uint32 m
,
4910 const char *comment
, void *state
)
4912 struct share_list
*share_list
= (struct share_list
*)state
;
4914 if (m
!= STYPE_DISKTREE
)
4917 share_list
->num_shares
+= 1;
4918 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4919 if (!share_list
->shares
) {
4920 share_list
->num_shares
= 0;
4923 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4927 * List shares on a remote RPC server, including the security descriptors.
4929 * All parameters are provided by the run_rpc_command function, except for
4930 * argc, argv which are passed through.
4932 * @param domain_sid The domain sid acquired from the remote server.
4933 * @param cli A cli_state connected to the server.
4934 * @param mem_ctx Talloc context, destroyed on completion of the function.
4935 * @param argc Standard main() style argc.
4936 * @param argv Standard main() style argv. Initial components are already
4939 * @return Normal NTSTATUS return.
4942 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4943 const struct dom_sid
*domain_sid
,
4944 const char *domain_name
,
4945 struct cli_state
*cli
,
4946 struct rpc_pipe_client
*pipe_hnd
,
4947 TALLOC_CTX
*mem_ctx
,
4956 struct user_token
*tokens
= NULL
;
4959 struct share_list share_list
;
4964 f
= fopen(argv
[0], "r");
4968 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4969 return NT_STATUS_UNSUCCESSFUL
;
4972 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4978 DEBUG(0, ("Could not read users from file\n"));
4979 return NT_STATUS_UNSUCCESSFUL
;
4982 for (i
=0; i
<num_tokens
; i
++)
4983 collect_alias_memberships(&tokens
[i
].token
);
4985 share_list
.num_shares
= 0;
4986 share_list
.shares
= NULL
;
4988 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4991 DEBUG(0, ("Error returning browse list: %s\n",
4996 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4997 char *netname
= share_list
.shares
[i
];
4999 if (netname
[strlen(netname
)-1] == '$')
5002 d_printf("%s\n", netname
);
5004 show_userlist(pipe_hnd
, mem_ctx
, netname
,
5005 num_tokens
, tokens
);
5008 for (i
=0; i
<num_tokens
; i
++) {
5009 free_user_token(&tokens
[i
].token
);
5012 SAFE_FREE(share_list
.shares
);
5014 return NT_STATUS_OK
;
5017 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
5022 if (c
->display_usage
) {
5024 "net rpc share allowedusers\n"
5027 _("List allowed users"));
5031 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5032 rpc_aliaslist_internals
,
5037 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
.syntax_id
, 0,
5043 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
5044 rpc_share_allowedusers_internals
,
5048 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
5051 struct user_token
*tokens
= NULL
;
5055 net_usersidlist_usage(c
, argc
, argv
);
5059 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
5060 DEBUG(0, ("Could not get the user/sid list\n"));
5064 for (i
=0; i
<num_tokens
; i
++) {
5065 dump_user_token(&tokens
[i
]);
5066 free_user_token(&tokens
[i
].token
);
5073 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
5075 d_printf(_("net usersidlist\n"
5076 "\tprints out a list of all users the running winbind knows\n"
5077 "\tabout, together with all their SIDs. This is used as\n"
5078 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5080 net_common_flags_usage(c
, argc
, argv
);
5085 * 'net rpc share' entrypoint.
5086 * @param argc Standard main() style argc.
5087 * @param argv Standard main() style argv. Initial components are already
5091 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
5093 NET_API_STATUS status
;
5095 struct functable func
[] = {
5101 N_("net rpc share add\n"
5109 N_("net rpc share delete\n"
5114 rpc_share_allowedusers
,
5116 N_("Modify allowed users"),
5117 N_("net rpc share allowedusers\n"
5118 " Modify allowed users")
5124 N_("Migrate share to local server"),
5125 N_("net rpc share migrate\n"
5126 " Migrate share to local server")
5133 N_("net rpc share list\n"
5136 {NULL
, NULL
, 0, NULL
, NULL
}
5139 status
= libnetapi_net_init(&c
->netapi_ctx
);
5143 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5144 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5145 if (c
->opt_kerberos
) {
5146 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5150 if (c
->display_usage
) {
5155 " Alias for net rpc share list\n"));
5156 net_display_usage_from_functable(func
);
5160 return rpc_share_list(c
, argc
, argv
);
5163 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5166 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5167 TALLOC_CTX
*mem_ctx
,
5168 struct rpc_sh_ctx
*ctx
,
5169 struct rpc_pipe_client
*pipe_hnd
,
5170 int argc
, const char **argv
)
5173 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
5176 static NTSTATUS
rpc_sh_share_add(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
)
5182 NET_API_STATUS status
;
5183 uint32_t parm_err
= 0;
5184 struct SHARE_INFO_2 i2
;
5186 if ((argc
< 2) || (argc
> 3)) {
5187 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
5189 return NT_STATUS_INVALID_PARAMETER
;
5192 i2
.shi2_netname
= argv
[0];
5193 i2
.shi2_type
= STYPE_DISKTREE
;
5194 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
5195 i2
.shi2_permissions
= 0;
5196 i2
.shi2_max_uses
= 0;
5197 i2
.shi2_current_uses
= 0;
5198 i2
.shi2_path
= argv
[1];
5199 i2
.shi2_passwd
= NULL
;
5201 status
= NetShareAdd(pipe_hnd
->desthost
,
5206 return werror_to_ntstatus(W_ERROR(status
));
5209 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5210 TALLOC_CTX
*mem_ctx
,
5211 struct rpc_sh_ctx
*ctx
,
5212 struct rpc_pipe_client
*pipe_hnd
,
5213 int argc
, const char **argv
)
5216 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5217 return NT_STATUS_INVALID_PARAMETER
;
5220 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
5223 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5224 TALLOC_CTX
*mem_ctx
,
5225 struct rpc_sh_ctx
*ctx
,
5226 struct rpc_pipe_client
*pipe_hnd
,
5227 int argc
, const char **argv
)
5229 union srvsvc_NetShareInfo info
;
5232 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5235 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5236 return NT_STATUS_INVALID_PARAMETER
;
5239 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
5245 if (!NT_STATUS_IS_OK(status
)) {
5246 result
= ntstatus_to_werror(status
);
5249 if (!W_ERROR_IS_OK(result
)) {
5253 d_printf(_("Name: %s\n"), info
.info2
->name
);
5254 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
5255 d_printf(_("Path: %s\n"), info
.info2
->path
);
5256 d_printf(_("Password: %s\n"), info
.info2
->password
);
5259 return werror_to_ntstatus(result
);
5262 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5263 struct rpc_sh_ctx
*ctx
)
5265 static struct rpc_sh_cmd cmds
[] = {
5267 { "list", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_list
,
5268 N_("List available shares") },
5270 { "add", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_add
,
5271 N_("Add a share") },
5273 { "delete", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_delete
,
5274 N_("Delete a share") },
5276 { "info", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_info
,
5277 N_("Get information about a share") },
5279 { NULL
, NULL
, 0, NULL
, NULL
}
5285 /****************************************************************************/
5287 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5289 return net_file_usage(c
, argc
, argv
);
5293 * Close a file on a remote RPC server.
5295 * @param argc Standard main() style argc.
5296 * @param argv Standard main() style argv. Initial components are already
5299 * @return A shell status integer (0 for success).
5301 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5303 if (argc
< 1 || c
->display_usage
) {
5304 return rpc_file_usage(c
, argc
, argv
);
5307 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
5311 * Formatted print of open file info
5313 * @param r struct FILE_INFO_3 contents
5316 static void display_file_info_3(struct FILE_INFO_3
*r
)
5318 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5319 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
5320 r
->fi3_num_locks
, r
->fi3_pathname
);
5324 * List files for a user on a remote RPC server.
5326 * @param argc Standard main() style argc.
5327 * @param argv Standard main() style argv. Initial components are already
5330 * @return A shell status integer (0 for success)..
5333 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5335 NET_API_STATUS status
;
5336 uint32 preferred_len
= 0xffffffff, i
;
5337 const char *username
=NULL
;
5338 uint32_t total_entries
= 0;
5339 uint32_t entries_read
= 0;
5340 uint32_t resume_handle
= 0;
5341 struct FILE_INFO_3
*i3
= NULL
;
5343 if (c
->display_usage
) {
5344 return rpc_file_usage(c
, argc
, argv
);
5347 /* if argc > 0, must be user command */
5349 username
= smb_xstrdup(argv
[0]);
5352 status
= NetFileEnum(c
->opt_host
,
5356 (uint8_t **)(void *)&i3
,
5366 /* Display results */
5369 "\nEnumerating open files on remote server:\n\n"
5370 "\nFileId Opened by Perms Locks Path"
5371 "\n------ --------- ----- ----- ---- \n"));
5372 for (i
= 0; i
< entries_read
; i
++) {
5373 display_file_info_3(&i3
[i
]);
5380 * 'net rpc file' entrypoint.
5381 * @param argc Standard main() style argc.
5382 * @param argv Standard main() style argv. Initial components are already
5386 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5388 NET_API_STATUS status
;
5390 struct functable func
[] = {
5395 N_("Close opened file"),
5396 N_("net rpc file close\n"
5397 " Close opened file")
5403 N_("List files opened by user"),
5404 N_("net rpc file user\n"
5405 " List files opened by user")
5412 N_("Display information about opened file"),
5413 N_("net rpc file info\n"
5414 " Display information about opened file")
5417 {NULL
, NULL
, 0, NULL
, NULL
}
5420 status
= libnetapi_net_init(&c
->netapi_ctx
);
5424 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5425 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5426 if (c
->opt_kerberos
) {
5427 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5431 if (c
->display_usage
) {
5432 d_printf(_("Usage:\n"));
5433 d_printf(_("net rpc file\n"
5434 " List opened files\n"));
5435 net_display_usage_from_functable(func
);
5439 return rpc_file_user(c
, argc
, argv
);
5442 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5446 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5448 * All parameters are provided by the run_rpc_command function, except for
5449 * argc, argv which are passed through.
5451 * @param c A net_context structure.
5452 * @param domain_sid The domain sid acquired from the remote server.
5453 * @param cli A cli_state connected to the server.
5454 * @param mem_ctx Talloc context, destroyed on completion of the function.
5455 * @param argc Standard main() style argc.
5456 * @param argv Standard main() style argv. Initial components are already
5459 * @return Normal NTSTATUS return.
5462 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5463 const struct dom_sid
*domain_sid
,
5464 const char *domain_name
,
5465 struct cli_state
*cli
,
5466 struct rpc_pipe_client
*pipe_hnd
,
5467 TALLOC_CTX
*mem_ctx
,
5471 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5473 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5475 status
= dcerpc_initshutdown_Abort(b
, mem_ctx
, NULL
, &result
);
5476 if (!NT_STATUS_IS_OK(status
)) {
5479 if (W_ERROR_IS_OK(result
)) {
5480 d_printf(_("\nShutdown successfully aborted\n"));
5481 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5483 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5485 return werror_to_ntstatus(result
);
5489 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5491 * All parameters are provided by the run_rpc_command function, except for
5492 * argc, argv which are passed through.
5494 * @param c A net_context structure.
5495 * @param domain_sid The domain sid acquired from the remote server.
5496 * @param cli A cli_state connected to the server.
5497 * @param mem_ctx Talloc context, destroyed on completion of the function.
5498 * @param argc Standard main() style argc.
5499 * @param argv Standard main() style argv. Initial components are already
5502 * @return Normal NTSTATUS return.
5505 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5506 const struct dom_sid
*domain_sid
,
5507 const char *domain_name
,
5508 struct cli_state
*cli
,
5509 struct rpc_pipe_client
*pipe_hnd
,
5510 TALLOC_CTX
*mem_ctx
,
5514 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5516 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5518 result
= dcerpc_winreg_AbortSystemShutdown(b
, mem_ctx
, NULL
, &werr
);
5520 if (!NT_STATUS_IS_OK(result
)) {
5521 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5524 if (W_ERROR_IS_OK(werr
)) {
5525 d_printf(_("\nShutdown successfully aborted\n"));
5526 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5528 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5530 return werror_to_ntstatus(werr
);
5534 * ABORT the shutdown of a remote RPC server.
5536 * @param argc Standard main() style argc.
5537 * @param argv Standard main() style argv. Initial components are already
5540 * @return A shell status integer (0 for success).
5543 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5548 if (c
->display_usage
) {
5550 "net rpc abortshutdown\n"
5553 _("Abort a scheduled shutdown"));
5557 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5558 rpc_shutdown_abort_internals
, argc
, argv
);
5563 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5565 return run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5566 rpc_reg_shutdown_abort_internals
,
5571 * Shut down a remote RPC Server via initshutdown pipe.
5573 * All parameters are provided by the run_rpc_command function, except for
5574 * argc, argv which are passed through.
5576 * @param c A net_context structure.
5577 * @param domain_sid The domain sid acquired from the remote server.
5578 * @param cli A cli_state connected to the server.
5579 * @param mem_ctx Talloc context, destroyed on completion of the function.
5580 * @param argc Standard main() style argc.
5581 * @param argv Standard main() style argv. Initial components are already
5584 * @return Normal NTSTATUS return.
5587 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5588 const struct dom_sid
*domain_sid
,
5589 const char *domain_name
,
5590 struct cli_state
*cli
,
5591 struct rpc_pipe_client
*pipe_hnd
,
5592 TALLOC_CTX
*mem_ctx
,
5596 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5598 const char *msg
= N_("This machine will be shutdown shortly");
5599 uint32 timeout
= 20;
5600 struct lsa_StringLarge msg_string
;
5601 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5603 if (c
->opt_comment
) {
5604 msg
= c
->opt_comment
;
5606 if (c
->opt_timeout
) {
5607 timeout
= c
->opt_timeout
;
5610 msg_string
.string
= msg
;
5612 /* create an entry */
5613 status
= dcerpc_initshutdown_Init(b
, mem_ctx
, NULL
,
5614 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5616 if (!NT_STATUS_IS_OK(status
)) {
5619 if (W_ERROR_IS_OK(result
)) {
5620 d_printf(_("\nShutdown of remote machine succeeded\n"));
5621 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5623 DEBUG(1,("Shutdown of remote machine failed!\n"));
5625 return werror_to_ntstatus(result
);
5629 * Shut down a remote RPC Server via winreg pipe.
5631 * All parameters are provided by the run_rpc_command function, except for
5632 * argc, argv which are passed through.
5634 * @param c A net_context structure.
5635 * @param domain_sid The domain sid acquired from the remote server.
5636 * @param cli A cli_state connected to the server.
5637 * @param mem_ctx Talloc context, destroyed on completion of the function.
5638 * @param argc Standard main() style argc.
5639 * @param argv Standard main() style argv. Initial components are already
5642 * @return Normal NTSTATUS return.
5645 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5646 const struct dom_sid
*domain_sid
,
5647 const char *domain_name
,
5648 struct cli_state
*cli
,
5649 struct rpc_pipe_client
*pipe_hnd
,
5650 TALLOC_CTX
*mem_ctx
,
5654 const char *msg
= N_("This machine will be shutdown shortly");
5655 uint32 timeout
= 20;
5656 struct lsa_StringLarge msg_string
;
5659 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5661 if (c
->opt_comment
) {
5662 msg
= c
->opt_comment
;
5664 msg_string
.string
= msg
;
5666 if (c
->opt_timeout
) {
5667 timeout
= c
->opt_timeout
;
5670 /* create an entry */
5671 result
= dcerpc_winreg_InitiateSystemShutdown(b
, mem_ctx
, NULL
,
5672 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5674 if (!NT_STATUS_IS_OK(result
)) {
5675 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5679 if (W_ERROR_IS_OK(werr
)) {
5680 d_printf(_("\nShutdown of remote machine succeeded\n"));
5682 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5683 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5684 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5686 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5689 return werror_to_ntstatus(werr
);
5693 * Shut down a remote RPC server.
5695 * @param argc Standard main() style argc.
5696 * @param argv Standard main() style argv. Initial components are already
5699 * @return A shell status integer (0 for success).
5702 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5706 if (c
->display_usage
) {
5708 "net rpc shutdown\n"
5711 _("Shut down a remote RPC server"));
5715 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5716 rpc_init_shutdown_internals
, argc
, argv
);
5719 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5720 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5721 rpc_reg_shutdown_internals
, argc
, argv
);
5727 /***************************************************************************
5728 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5729 ***************************************************************************/
5732 * Add interdomain trust account to the RPC server.
5733 * All parameters (except for argc and argv) are passed by run_rpc_command
5736 * @param c A net_context structure.
5737 * @param domain_sid The domain sid acquired from the server.
5738 * @param cli A cli_state connected to the server.
5739 * @param mem_ctx Talloc context, destroyed on completion of the function.
5740 * @param argc Standard main() style argc.
5741 * @param argv Standard main() style argv. Initial components are already
5744 * @return normal NTSTATUS return code.
5747 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5748 const struct dom_sid
*domain_sid
,
5749 const char *domain_name
,
5750 struct cli_state
*cli
,
5751 struct rpc_pipe_client
*pipe_hnd
,
5752 TALLOC_CTX
*mem_ctx
,
5756 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5757 NTSTATUS status
, result
;
5759 struct lsa_String lsa_acct_name
;
5761 uint32 acct_flags
=0;
5763 uint32_t access_granted
= 0;
5764 union samr_UserInfo info
;
5765 unsigned int orig_timeout
;
5766 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5771 _(" net rpc trustdom add <domain_name> "
5772 "<trust password>\n"));
5773 return NT_STATUS_INVALID_PARAMETER
;
5777 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5780 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5781 return NT_STATUS_NO_MEMORY
;
5784 strupper_m(acct_name
);
5786 init_lsa_String(&lsa_acct_name
, acct_name
);
5788 /* Get samr policy handle */
5789 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5791 MAXIMUM_ALLOWED_ACCESS
,
5794 if (!NT_STATUS_IS_OK(status
)) {
5797 if (!NT_STATUS_IS_OK(result
)) {
5802 /* Get domain policy handle */
5803 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5805 MAXIMUM_ALLOWED_ACCESS
,
5806 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5809 if (!NT_STATUS_IS_OK(status
)) {
5812 if (!NT_STATUS_IS_OK(result
)) {
5817 /* This call can take a long time - allow the server to time out.
5818 * 35 seconds should do it. */
5820 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5822 /* Create trusting domain's account */
5823 acb_info
= ACB_NORMAL
;
5824 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5825 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5826 SAMR_USER_ACCESS_SET_PASSWORD
|
5827 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5828 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5830 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
5839 if (!NT_STATUS_IS_OK(status
)) {
5842 /* And restore our original timeout. */
5843 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5845 if (!NT_STATUS_IS_OK(result
)) {
5847 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5848 acct_name
, nt_errstr(result
));
5853 struct samr_CryptPassword crypt_pwd
;
5855 ZERO_STRUCT(info
.info23
);
5857 init_samr_CryptPassword(argv
[1],
5858 &cli
->user_session_key
,
5861 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5862 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5863 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5864 info
.info23
.password
= crypt_pwd
;
5866 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
5871 if (!NT_STATUS_IS_OK(status
)) {
5875 if (!NT_STATUS_IS_OK(result
)) {
5877 DEBUG(0,("Could not set trust account password: %s\n",
5878 nt_errstr(result
)));
5884 SAFE_FREE(acct_name
);
5889 * Create interdomain trust account for a remote domain.
5891 * @param argc Standard argc.
5892 * @param argv Standard argv without initial components.
5894 * @return Integer status (0 means success).
5897 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5899 if (argc
> 0 && !c
->display_usage
) {
5900 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5901 rpc_trustdom_add_internals
, argc
, argv
);
5905 _("net rpc trustdom add <domain_name> <trust "
5913 * Remove interdomain trust account from the RPC server.
5914 * All parameters (except for argc and argv) are passed by run_rpc_command
5917 * @param c A net_context structure.
5918 * @param domain_sid The domain sid acquired from the server.
5919 * @param cli A cli_state connected to the server.
5920 * @param mem_ctx Talloc context, destroyed on completion of the function.
5921 * @param argc Standard main() style argc.
5922 * @param argv Standard main() style argv. Initial components are already
5925 * @return normal NTSTATUS return code.
5928 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5929 const struct dom_sid
*domain_sid
,
5930 const char *domain_name
,
5931 struct cli_state
*cli
,
5932 struct rpc_pipe_client
*pipe_hnd
,
5933 TALLOC_CTX
*mem_ctx
,
5937 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5938 NTSTATUS status
, result
;
5940 struct dom_sid trust_acct_sid
;
5941 struct samr_Ids user_rids
, name_types
;
5942 struct lsa_String lsa_acct_name
;
5943 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5948 _(" net rpc trustdom del <domain_name>\n"));
5949 return NT_STATUS_INVALID_PARAMETER
;
5953 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5955 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5957 if (acct_name
== NULL
)
5958 return NT_STATUS_NO_MEMORY
;
5960 strupper_m(acct_name
);
5962 /* Get samr policy handle */
5963 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5965 MAXIMUM_ALLOWED_ACCESS
,
5968 if (!NT_STATUS_IS_OK(status
)) {
5971 if (!NT_STATUS_IS_OK(result
)) {
5976 /* Get domain policy handle */
5977 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5979 MAXIMUM_ALLOWED_ACCESS
,
5980 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5983 if (!NT_STATUS_IS_OK(status
)) {
5986 if (!NT_STATUS_IS_OK(result
)) {
5991 init_lsa_String(&lsa_acct_name
, acct_name
);
5993 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
6000 if (!NT_STATUS_IS_OK(status
)) {
6001 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6003 acct_name
, nt_errstr(status
));
6006 if (!NT_STATUS_IS_OK(result
)) {
6008 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6010 acct_name
, nt_errstr(result
) );
6014 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
6016 MAXIMUM_ALLOWED_ACCESS
,
6020 if (!NT_STATUS_IS_OK(status
)) {
6021 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6023 acct_name
, nt_errstr(status
) );
6027 if (!NT_STATUS_IS_OK(result
)) {
6029 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6031 acct_name
, nt_errstr(result
) );
6035 /* append the rid to the domain sid */
6036 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
6040 /* remove the sid */
6042 status
= dcerpc_samr_RemoveMemberFromForeignDomain(b
, mem_ctx
,
6046 if (!NT_STATUS_IS_OK(status
)) {
6047 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6048 " on user %s failed %s\n"),
6049 acct_name
, nt_errstr(status
));
6052 if (!NT_STATUS_IS_OK(result
)) {
6054 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6055 " on user %s failed %s\n"),
6056 acct_name
, nt_errstr(result
) );
6063 status
= dcerpc_samr_DeleteUser(b
, mem_ctx
,
6066 if (!NT_STATUS_IS_OK(status
)) {
6067 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6069 acct_name
, nt_errstr(status
));
6073 if (!NT_STATUS_IS_OK(result
)) {
6075 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6077 acct_name
, nt_errstr(result
) );
6081 if (!NT_STATUS_IS_OK(result
)) {
6082 d_printf(_("Could not set trust account password: %s\n"),
6092 * Delete interdomain trust account for a remote domain.
6094 * @param argc Standard argc.
6095 * @param argv Standard argv without initial components.
6097 * @return Integer status (0 means success).
6100 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
6102 if (argc
> 0 && !c
->display_usage
) {
6103 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
6104 rpc_trustdom_del_internals
, argc
, argv
);
6108 _("net rpc trustdom del <domain>\n"));
6113 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
6114 struct cli_state
*cli
,
6115 TALLOC_CTX
*mem_ctx
,
6116 const char *domain_name
)
6118 char *dc_name
= NULL
;
6119 const char *buffer
= NULL
;
6120 struct rpc_pipe_client
*netr
;
6123 struct dcerpc_binding_handle
*b
;
6125 /* Use NetServerEnum2 */
6127 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
6129 return NT_STATUS_OK
;
6132 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6133 for domain %s\n", domain_name
));
6135 /* Try netr_GetDcName */
6137 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
6139 if (!NT_STATUS_IS_OK(status
)) {
6143 b
= netr
->binding_handle
;
6145 status
= dcerpc_netr_GetDcName(b
, mem_ctx
,
6152 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(result
)) {
6156 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6157 for domain %s\n", domain_name
));
6159 if (!NT_STATUS_IS_OK(status
)) {
6163 return werror_to_ntstatus(result
);
6167 * Establish trust relationship to a trusting domain.
6168 * Interdomain account must already be created on remote PDC.
6170 * @param c A net_context structure.
6171 * @param argc Standard argc.
6172 * @param argv Standard argv without initial components.
6174 * @return Integer status (0 means success).
6177 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6180 struct cli_state
*cli
= NULL
;
6181 struct sockaddr_storage server_ss
;
6182 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6183 struct policy_handle connect_hnd
;
6184 TALLOC_CTX
*mem_ctx
;
6185 NTSTATUS nt_status
, result
;
6186 struct dom_sid
*domain_sid
;
6191 union lsa_PolicyInformation
*info
= NULL
;
6192 struct dcerpc_binding_handle
*b
;
6195 * Connect to \\server\ipc$ as 'our domain' account with password
6198 if (argc
!= 1 || c
->display_usage
) {
6201 _("net rpc trustdom establish <domain_name>\n"));
6205 domain_name
= smb_xstrdup(argv
[0]);
6206 strupper_m(domain_name
);
6208 /* account name used at first is our domain's name with '$' */
6209 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
6212 strupper_m(acct_name
);
6215 * opt_workgroup will be used by connection functions further,
6216 * hence it should be set to remote domain name instead of ours
6218 if (c
->opt_workgroup
) {
6219 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6222 c
->opt_user_name
= acct_name
;
6224 /* find the domain controller */
6225 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6226 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6230 /* connect to ipc$ as username/password */
6231 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6232 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6234 /* Is it trusting domain account for sure ? */
6235 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6236 nt_errstr(nt_status
)));
6240 /* store who we connected to */
6242 saf_store( domain_name
, pdc_name
);
6245 * Connect to \\server\ipc$ again (this time anonymously)
6248 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6251 if (NT_STATUS_IS_ERR(nt_status
)) {
6252 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6253 domain_name
, nt_errstr(nt_status
)));
6257 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6258 "domain %s", domain_name
))) {
6259 DEBUG(0, ("talloc_init() failed\n"));
6264 /* Make sure we're talking to a proper server */
6266 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6267 if (!NT_STATUS_IS_OK(nt_status
)) {
6269 talloc_destroy(mem_ctx
);
6274 * Call LsaOpenPolicy and LsaQueryInfo
6277 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6279 if (!NT_STATUS_IS_OK(nt_status
)) {
6280 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6282 talloc_destroy(mem_ctx
);
6286 b
= pipe_hnd
->binding_handle
;
6288 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
6290 if (NT_STATUS_IS_ERR(nt_status
)) {
6291 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6292 nt_errstr(nt_status
)));
6294 talloc_destroy(mem_ctx
);
6298 /* Querying info level 5 */
6300 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6302 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6305 if (NT_STATUS_IS_ERR(nt_status
)) {
6306 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6307 nt_errstr(nt_status
)));
6309 talloc_destroy(mem_ctx
);
6312 if (NT_STATUS_IS_ERR(result
)) {
6313 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6314 nt_errstr(result
)));
6316 talloc_destroy(mem_ctx
);
6320 domain_sid
= info
->account_domain
.sid
;
6322 /* There should be actually query info level 3 (following nt serv behaviour),
6323 but I still don't know if it's _really_ necessary */
6326 * Store the password in secrets db
6329 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6330 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6332 talloc_destroy(mem_ctx
);
6337 * Close the pipes and clean up
6340 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6341 if (NT_STATUS_IS_ERR(nt_status
)) {
6342 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6343 nt_errstr(nt_status
)));
6345 talloc_destroy(mem_ctx
);
6351 talloc_destroy(mem_ctx
);
6353 d_printf(_("Trust to domain %s established\n"), domain_name
);
6358 * Revoke trust relationship to the remote domain.
6360 * @param c A net_context structure.
6361 * @param argc Standard argc.
6362 * @param argv Standard argv without initial components.
6364 * @return Integer status (0 means success).
6367 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6373 if (argc
< 1 || c
->display_usage
) {
6376 _("net rpc trustdom revoke <domain_name>\n"
6377 " Revoke trust relationship\n"
6378 " domain_name\tName of domain to revoke trust\n"));
6382 /* generate upper cased domain name */
6383 domain_name
= smb_xstrdup(argv
[0]);
6384 strupper_m(domain_name
);
6386 /* delete password of the trust */
6387 if (!pdb_del_trusteddom_pw(domain_name
)) {
6388 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6395 SAFE_FREE(domain_name
);
6399 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6400 const struct dom_sid
*domain_sid
,
6401 const char *domain_name
,
6402 struct cli_state
*cli
,
6403 struct rpc_pipe_client
*pipe_hnd
,
6404 TALLOC_CTX
*mem_ctx
,
6409 if (!sid_to_fstring(str_sid
, domain_sid
)) {
6410 return NT_STATUS_UNSUCCESSFUL
;
6412 d_printf("%s\n", str_sid
);
6413 return NT_STATUS_OK
;
6416 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
6420 /* convert sid into ascii string */
6421 sid_to_fstring(ascii_sid
, dom_sid
);
6423 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
6426 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6427 TALLOC_CTX
*mem_ctx
,
6428 struct policy_handle
*pol
,
6429 struct dom_sid dom_sid
,
6430 const char *trusted_dom_name
)
6432 NTSTATUS nt_status
, result
;
6433 union lsa_TrustedDomainInfo
*info
= NULL
;
6434 char *cleartextpwd
= NULL
;
6435 uint8_t session_key
[16];
6436 DATA_BLOB session_key_blob
;
6437 DATA_BLOB data
= data_blob_null
;
6438 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6440 nt_status
= dcerpc_lsa_QueryTrustedDomainInfoBySid(b
, mem_ctx
,
6443 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6446 if (NT_STATUS_IS_ERR(nt_status
)) {
6447 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6448 nt_errstr(nt_status
)));
6451 if (NT_STATUS_IS_ERR(result
)) {
6453 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6454 nt_errstr(result
)));
6458 data
= data_blob(info
->password
.password
->data
,
6459 info
->password
.password
->length
);
6461 if (!rpccli_get_pwd_hash(pipe_hnd
, session_key
)) {
6462 DEBUG(0, ("Could not retrieve password hash\n"));
6466 session_key_blob
= data_blob_const(session_key
, sizeof(session_key
));
6467 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key_blob
);
6469 if (cleartextpwd
== NULL
) {
6470 DEBUG(0,("retrieved NULL password\n"));
6471 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6475 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6476 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6477 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6481 #ifdef DEBUG_PASSWORD
6482 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6483 "password: [%s]\n", trusted_dom_name
,
6484 sid_string_dbg(&dom_sid
), cleartextpwd
));
6488 SAFE_FREE(cleartextpwd
);
6489 data_blob_free(&data
);
6494 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6497 /* common variables */
6498 TALLOC_CTX
* mem_ctx
;
6499 struct cli_state
*cli
= NULL
;
6500 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6501 NTSTATUS nt_status
, result
;
6502 const char *domain_name
= NULL
;
6503 struct dom_sid
*queried_dom_sid
;
6504 struct policy_handle connect_hnd
;
6505 union lsa_PolicyInformation
*info
= NULL
;
6507 /* trusted domains listing variables */
6508 unsigned int enum_ctx
= 0;
6510 struct lsa_DomainList dom_list
;
6512 struct dcerpc_binding_handle
*b
;
6514 if (c
->display_usage
) {
6516 "net rpc trustdom vampire\n"
6519 _("Vampire trust relationship from remote server"));
6524 * Listing trusted domains (stored in secrets.tdb, if local)
6527 mem_ctx
= talloc_init("trust relationships vampire");
6530 * set domain and pdc name to local samba server (default)
6531 * or to remote one given in command line
6534 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6535 domain_name
= c
->opt_workgroup
;
6536 c
->opt_target_workgroup
= c
->opt_workgroup
;
6538 fstrcpy(pdc_name
, global_myname());
6539 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6540 c
->opt_target_workgroup
= domain_name
;
6543 /* open \PIPE\lsarpc and open policy handle */
6544 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6545 if (!NT_STATUS_IS_OK(nt_status
)) {
6546 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6547 nt_errstr(nt_status
)));
6548 talloc_destroy(mem_ctx
);
6552 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6554 if (!NT_STATUS_IS_OK(nt_status
)) {
6555 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6556 nt_errstr(nt_status
) ));
6558 talloc_destroy(mem_ctx
);
6562 b
= pipe_hnd
->binding_handle
;
6564 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6566 if (NT_STATUS_IS_ERR(nt_status
)) {
6567 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6568 nt_errstr(nt_status
)));
6570 talloc_destroy(mem_ctx
);
6574 /* query info level 5 to obtain sid of a domain being queried */
6575 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6577 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6581 if (NT_STATUS_IS_ERR(nt_status
)) {
6582 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6583 nt_errstr(nt_status
)));
6585 talloc_destroy(mem_ctx
);
6588 if (NT_STATUS_IS_ERR(result
)) {
6589 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6590 nt_errstr(result
)));
6592 talloc_destroy(mem_ctx
);
6596 queried_dom_sid
= info
->account_domain
.sid
;
6599 * Keep calling LsaEnumTrustdom over opened pipe until
6600 * the end of enumeration is reached
6603 d_printf(_("Vampire trusted domains:\n\n"));
6606 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6612 if (NT_STATUS_IS_ERR(nt_status
)) {
6613 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6614 nt_errstr(nt_status
)));
6616 talloc_destroy(mem_ctx
);
6619 if (NT_STATUS_IS_ERR(result
)) {
6621 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6622 nt_errstr(result
)));
6624 talloc_destroy(mem_ctx
);
6629 for (i
= 0; i
< dom_list
.count
; i
++) {
6631 print_trusted_domain(dom_list
.domains
[i
].sid
,
6632 dom_list
.domains
[i
].name
.string
);
6634 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6635 *dom_list
.domains
[i
].sid
,
6636 dom_list
.domains
[i
].name
.string
);
6637 if (!NT_STATUS_IS_OK(nt_status
)) {
6639 talloc_destroy(mem_ctx
);
6645 * in case of no trusted domains say something rather
6646 * than just display blank line
6648 if (!dom_list
.count
) d_printf(_("none\n"));
6650 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6652 /* close this connection before doing next one */
6653 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6654 if (NT_STATUS_IS_ERR(nt_status
)) {
6655 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6656 nt_errstr(nt_status
)));
6658 talloc_destroy(mem_ctx
);
6662 /* close lsarpc pipe and connection to IPC$ */
6665 talloc_destroy(mem_ctx
);
6669 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6671 /* common variables */
6672 TALLOC_CTX
* mem_ctx
;
6673 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6674 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6675 NTSTATUS nt_status
, result
;
6676 const char *domain_name
= NULL
;
6677 struct dom_sid
*queried_dom_sid
;
6678 int ascii_dom_name_len
;
6679 struct policy_handle connect_hnd
;
6680 union lsa_PolicyInformation
*info
= NULL
;
6681 struct dcerpc_binding_handle
*b
= NULL
;
6683 /* trusted domains listing variables */
6684 unsigned int num_domains
, enum_ctx
= 0;
6686 struct lsa_DomainList dom_list
;
6690 /* trusting domains listing variables */
6691 struct policy_handle domain_hnd
;
6692 struct samr_SamArray
*trusts
= NULL
;
6694 if (c
->display_usage
) {
6696 "net rpc trustdom list\n"
6699 _("List incoming and outgoing trust relationships"));
6704 * Listing trusted domains (stored in secrets.tdb, if local)
6707 mem_ctx
= talloc_init("trust relationships listing");
6710 * set domain and pdc name to local samba server (default)
6711 * or to remote one given in command line
6714 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6715 domain_name
= c
->opt_workgroup
;
6716 c
->opt_target_workgroup
= c
->opt_workgroup
;
6718 fstrcpy(pdc_name
, global_myname());
6719 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6720 c
->opt_target_workgroup
= domain_name
;
6723 /* open \PIPE\lsarpc and open policy handle */
6724 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6725 if (!NT_STATUS_IS_OK(nt_status
)) {
6726 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6727 nt_errstr(nt_status
)));
6728 talloc_destroy(mem_ctx
);
6732 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6734 if (!NT_STATUS_IS_OK(nt_status
)) {
6735 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6736 nt_errstr(nt_status
) ));
6738 talloc_destroy(mem_ctx
);
6742 b
= pipe_hnd
->binding_handle
;
6744 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6746 if (NT_STATUS_IS_ERR(nt_status
)) {
6747 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6748 nt_errstr(nt_status
)));
6750 talloc_destroy(mem_ctx
);
6754 /* query info level 5 to obtain sid of a domain being queried */
6755 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6757 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6761 if (NT_STATUS_IS_ERR(nt_status
)) {
6762 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6763 nt_errstr(nt_status
)));
6765 talloc_destroy(mem_ctx
);
6768 if (NT_STATUS_IS_ERR(result
)) {
6769 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6770 nt_errstr(result
)));
6772 talloc_destroy(mem_ctx
);
6776 queried_dom_sid
= info
->account_domain
.sid
;
6779 * Keep calling LsaEnumTrustdom over opened pipe until
6780 * the end of enumeration is reached
6783 d_printf(_("Trusted domains list:\n\n"));
6785 found_domain
= false;
6788 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6794 if (NT_STATUS_IS_ERR(nt_status
)) {
6795 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6796 nt_errstr(nt_status
)));
6798 talloc_destroy(mem_ctx
);
6801 if (NT_STATUS_IS_ERR(result
)) {
6802 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6803 nt_errstr(result
)));
6805 talloc_destroy(mem_ctx
);
6810 for (i
= 0; i
< dom_list
.count
; i
++) {
6811 print_trusted_domain(dom_list
.domains
[i
].sid
,
6812 dom_list
.domains
[i
].name
.string
);
6813 found_domain
= true;
6817 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6820 * in case of no trusted domains say something rather
6821 * than just display blank line
6823 if (!found_domain
) {
6824 d_printf(_("none\n"));
6827 /* close this connection before doing next one */
6828 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6829 if (NT_STATUS_IS_ERR(nt_status
)) {
6830 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6831 nt_errstr(nt_status
)));
6833 talloc_destroy(mem_ctx
);
6837 TALLOC_FREE(pipe_hnd
);
6840 * Listing trusting domains (stored in passdb backend, if local)
6843 d_printf(_("\nTrusting domains list:\n\n"));
6846 * Open \PIPE\samr and get needed policy handles
6848 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6850 if (!NT_STATUS_IS_OK(nt_status
)) {
6851 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6853 talloc_destroy(mem_ctx
);
6857 b
= pipe_hnd
->binding_handle
;
6860 nt_status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6862 SAMR_ACCESS_LOOKUP_DOMAIN
,
6865 if (!NT_STATUS_IS_OK(nt_status
)) {
6866 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6867 nt_errstr(nt_status
)));
6869 talloc_destroy(mem_ctx
);
6872 if (!NT_STATUS_IS_OK(result
)) {
6874 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6875 nt_errstr(result
)));
6877 talloc_destroy(mem_ctx
);
6881 /* SamrOpenDomain - we have to open domain policy handle in order to be
6882 able to enumerate accounts*/
6883 nt_status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6885 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6889 if (!NT_STATUS_IS_OK(nt_status
)) {
6890 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6891 nt_errstr(nt_status
)));
6893 talloc_destroy(mem_ctx
);
6896 if (!NT_STATUS_IS_OK(result
)) {
6898 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6899 nt_errstr(result
)));
6901 talloc_destroy(mem_ctx
);
6906 * perform actual enumeration
6909 found_domain
= false;
6911 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6914 nt_status
= dcerpc_samr_EnumDomainUsers(b
, mem_ctx
,
6922 if (NT_STATUS_IS_ERR(nt_status
)) {
6923 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6924 nt_errstr(nt_status
)));
6926 talloc_destroy(mem_ctx
);
6929 if (NT_STATUS_IS_ERR(result
)) {
6931 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6932 nt_errstr(result
)));
6934 talloc_destroy(mem_ctx
);
6938 for (i
= 0; i
< num_domains
; i
++) {
6940 char *str
= CONST_DISCARD(char *, trusts
->entries
[i
].name
.string
);
6942 found_domain
= true;
6945 * get each single domain's sid (do we _really_ need this ?):
6946 * 1) connect to domain's pdc
6947 * 2) query the pdc for domain's sid
6950 /* get rid of '$' tail */
6951 ascii_dom_name_len
= strlen(str
);
6952 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6953 str
[ascii_dom_name_len
- 1] = '\0';
6955 /* set opt_* variables to remote domain */
6957 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6958 c
->opt_target_workgroup
= c
->opt_workgroup
;
6960 d_printf("%-20s", str
);
6962 /* connect to remote domain controller */
6963 nt_status
= net_make_ipc_connection(c
,
6964 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6966 if (NT_STATUS_IS_OK(nt_status
)) {
6967 /* query for domain's sid */
6968 if (run_rpc_command(
6970 &ndr_table_lsarpc
.syntax_id
, 0,
6971 rpc_query_domain_sid
, argc
,
6973 d_printf(_("strange - couldn't get domain's sid\n"));
6975 cli_shutdown(remote_cli
);
6978 d_fprintf(stderr
, _("domain controller is not "
6979 "responding: %s\n"),
6980 nt_errstr(nt_status
));
6981 d_printf(_("couldn't get domain's sid\n"));
6985 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
6987 if (!found_domain
) {
6991 /* close opened samr and domain policy handles */
6992 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &domain_hnd
, &result
);
6993 if (!NT_STATUS_IS_OK(nt_status
)) {
6994 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6997 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6998 if (!NT_STATUS_IS_OK(nt_status
)) {
6999 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
7002 /* close samr pipe and connection to IPC$ */
7005 talloc_destroy(mem_ctx
);
7010 * Entrypoint for 'net rpc trustdom' code.
7012 * @param argc Standard argc.
7013 * @param argv Standard argv without initial components.
7015 * @return Integer status (0 means success).
7018 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
7020 struct functable func
[] = {
7025 N_("Add trusting domain's account"),
7026 N_("net rpc trustdom add\n"
7027 " Add trusting domain's account")
7033 N_("Remove trusting domain's account"),
7034 N_("net rpc trustdom del\n"
7035 " Remove trusting domain's account")
7039 rpc_trustdom_establish
,
7041 N_("Establish outgoing trust relationship"),
7042 N_("net rpc trustdom establish\n"
7043 " Establish outgoing trust relationship")
7047 rpc_trustdom_revoke
,
7049 N_("Revoke outgoing trust relationship"),
7050 N_("net rpc trustdom revoke\n"
7051 " Revoke outgoing trust relationship")
7057 N_("List in- and outgoing domain trusts"),
7058 N_("net rpc trustdom list\n"
7059 " List in- and outgoing domain trusts")
7063 rpc_trustdom_vampire
,
7065 N_("Vampire trusts from remote server"),
7066 N_("net rpc trustdom vampire\n"
7067 " Vampire trusts from remote server")
7069 {NULL
, NULL
, 0, NULL
, NULL
}
7072 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
7076 * Check if a server will take rpc commands
7077 * @param flags Type of server to connect to (PDC, DMB, localhost)
7078 * if the host is not explicitly specified
7079 * @return bool (true means rpc supported)
7081 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
7083 struct cli_state
*cli
;
7085 struct sockaddr_storage server_ss
;
7086 char *server_name
= NULL
;
7089 /* flags (i.e. server type) may depend on command */
7090 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
7093 if ((cli
= cli_initialise()) == NULL
) {
7097 status
= cli_connect(cli
, server_name
, &server_ss
);
7098 if (!NT_STATUS_IS_OK(status
))
7100 if (!attempt_netbios_session_request(&cli
, global_myname(),
7101 server_name
, &server_ss
))
7103 status
= cli_negprot(cli
);
7104 if (!NT_STATUS_IS_OK(status
))
7106 if (cli
->protocol
< PROTOCOL_NT1
)
7115 /* dump sam database via samsync rpc calls */
7116 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
7117 if (c
->display_usage
) {
7122 _("Dump remote SAM database"));
7126 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
7127 NET_FLAGS_ANONYMOUS
,
7128 rpc_samdump_internals
, argc
, argv
);
7131 /* syncronise sam database via samsync rpc calls */
7132 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
7134 struct functable func
[] = {
7139 N_("Dump remote SAM database to ldif"),
7140 N_("net rpc vampire ldif\n"
7141 " Dump remote SAM database to LDIF file or "
7148 N_("Dump remote SAM database to Kerberos Keytab"),
7149 N_("net rpc vampire keytab\n"
7150 " Dump remote SAM database to Kerberos keytab "
7157 N_("Dump remote SAM database to passdb"),
7158 N_("net rpc vampire passdb\n"
7159 " Dump remote SAM database to passdb")
7162 {NULL
, NULL
, 0, NULL
, NULL
}
7166 if (c
->display_usage
) {
7171 _("Vampire remote SAM database"));
7175 return rpc_vampire_passdb(c
, argc
, argv
);
7178 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
7182 * Migrate everything from a print server.
7184 * @param c A net_context structure.
7185 * @param argc Standard main() style argc.
7186 * @param argv Standard main() style argv. Initial components are already
7189 * @return A shell status integer (0 for success).
7191 * The order is important !
7192 * To successfully add drivers the print queues have to exist !
7193 * Applying ACLs should be the last step, because you're easily locked out.
7196 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
7201 if (c
->display_usage
) {
7203 "net rpc printer migrate all\n"
7206 _("Migrate everything from a print server"));
7211 d_printf(_("no server to migrate\n"));
7215 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7216 rpc_printer_migrate_printers_internals
, argc
,
7221 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7222 rpc_printer_migrate_drivers_internals
, argc
,
7227 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7228 rpc_printer_migrate_forms_internals
, argc
, argv
);
7232 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7233 rpc_printer_migrate_settings_internals
, argc
,
7238 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7239 rpc_printer_migrate_security_internals
, argc
,
7245 * Migrate print drivers from a print server.
7247 * @param c A net_context structure.
7248 * @param argc Standard main() style argc.
7249 * @param argv Standard main() style argv. Initial components are already
7252 * @return A shell status integer (0 for success).
7254 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
7257 if (c
->display_usage
) {
7259 "net rpc printer migrate drivers\n"
7262 _("Migrate print-drivers from a print-server"));
7267 d_printf(_("no server to migrate\n"));
7271 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7272 rpc_printer_migrate_drivers_internals
,
7277 * Migrate print-forms from a print-server.
7279 * @param c A net_context structure.
7280 * @param argc Standard main() style argc.
7281 * @param argv Standard main() style argv. Initial components are already
7284 * @return A shell status integer (0 for success).
7286 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7289 if (c
->display_usage
) {
7291 "net rpc printer migrate forms\n"
7294 _("Migrate print-forms from a print-server"));
7299 d_printf(_("no server to migrate\n"));
7303 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7304 rpc_printer_migrate_forms_internals
,
7309 * Migrate printers from a print-server.
7311 * @param c A net_context structure.
7312 * @param argc Standard main() style argc.
7313 * @param argv Standard main() style argv. Initial components are already
7316 * @return A shell status integer (0 for success).
7318 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7321 if (c
->display_usage
) {
7323 "net rpc printer migrate printers\n"
7326 _("Migrate printers from a print-server"));
7331 d_printf(_("no server to migrate\n"));
7335 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7336 rpc_printer_migrate_printers_internals
,
7341 * Migrate printer-ACLs from a print-server
7343 * @param c A net_context structure.
7344 * @param argc Standard main() style argc.
7345 * @param argv Standard main() style argv. Initial components are already
7348 * @return A shell status integer (0 for success).
7350 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7353 if (c
->display_usage
) {
7355 "net rpc printer migrate security\n"
7358 _("Migrate printer-ACLs from a print-server"));
7363 d_printf(_("no server to migrate\n"));
7367 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7368 rpc_printer_migrate_security_internals
,
7373 * Migrate printer-settings from a print-server.
7375 * @param c A net_context structure.
7376 * @param argc Standard main() style argc.
7377 * @param argv Standard main() style argv. Initial components are already
7380 * @return A shell status integer (0 for success).
7382 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7385 if (c
->display_usage
) {
7387 "net rpc printer migrate settings\n"
7390 _("Migrate printer-settings from a "
7396 d_printf(_("no server to migrate\n"));
7400 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7401 rpc_printer_migrate_settings_internals
,
7406 * 'net rpc printer' entrypoint.
7408 * @param c A net_context structure.
7409 * @param argc Standard main() style argc.
7410 * @param argv Standard main() style argv. Initial components are already
7414 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7417 /* ouch: when addriver and setdriver are called from within
7418 rpc_printer_migrate_drivers_internals, the printer-queue already
7421 struct functable func
[] = {
7424 rpc_printer_migrate_all
,
7426 N_("Migrate all from remote to local print server"),
7427 N_("net rpc printer migrate all\n"
7428 " Migrate all from remote to local print server")
7432 rpc_printer_migrate_drivers
,
7434 N_("Migrate drivers to local server"),
7435 N_("net rpc printer migrate drivers\n"
7436 " Migrate drivers to local server")
7440 rpc_printer_migrate_forms
,
7442 N_("Migrate froms to local server"),
7443 N_("net rpc printer migrate forms\n"
7444 " Migrate froms to local server")
7448 rpc_printer_migrate_printers
,
7450 N_("Migrate printers to local server"),
7451 N_("net rpc printer migrate printers\n"
7452 " Migrate printers to local server")
7456 rpc_printer_migrate_security
,
7458 N_("Mirgate printer ACLs to local server"),
7459 N_("net rpc printer migrate security\n"
7460 " Mirgate printer ACLs to local server")
7464 rpc_printer_migrate_settings
,
7466 N_("Migrate printer settings to local server"),
7467 N_("net rpc printer migrate settings\n"
7468 " Migrate printer settings to local server")
7470 {NULL
, NULL
, 0, NULL
, NULL
}
7473 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7478 * List printers on a remote RPC server.
7480 * @param c A net_context structure.
7481 * @param argc Standard main() style argc.
7482 * @param argv Standard main() style argv. Initial components are already
7485 * @return A shell status integer (0 for success).
7487 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7489 if (c
->display_usage
) {
7491 "net rpc printer list\n"
7494 _("List printers on a remote RPC server"));
7498 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7499 rpc_printer_list_internals
,
7504 * List printer-drivers on a remote RPC server.
7506 * @param c A net_context structure.
7507 * @param argc Standard main() style argc.
7508 * @param argv Standard main() style argv. Initial components are already
7511 * @return A shell status integer (0 for success).
7513 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7516 if (c
->display_usage
) {
7518 "net rpc printer driver\n"
7521 _("List printer-drivers on a remote RPC server"));
7525 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7526 rpc_printer_driver_list_internals
,
7531 * Publish printer in ADS via MSRPC.
7533 * @param c A net_context structure.
7534 * @param argc Standard main() style argc.
7535 * @param argv Standard main() style argv. Initial components are already
7538 * @return A shell status integer (0 for success).
7540 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7543 if (c
->display_usage
) {
7545 "net rpc printer publish publish\n"
7548 _("Publish printer in ADS via MSRPC"));
7552 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7553 rpc_printer_publish_publish_internals
,
7558 * Update printer in ADS via MSRPC.
7560 * @param c A net_context structure.
7561 * @param argc Standard main() style argc.
7562 * @param argv Standard main() style argv. Initial components are already
7565 * @return A shell status integer (0 for success).
7567 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7569 if (c
->display_usage
) {
7571 "net rpc printer publish update\n"
7574 _("Update printer in ADS via MSRPC"));
7578 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7579 rpc_printer_publish_update_internals
,
7584 * UnPublish printer in ADS via MSRPC.
7586 * @param c A net_context structure.
7587 * @param argc Standard main() style argc.
7588 * @param argv Standard main() style argv. Initial components are already
7591 * @return A shell status integer (0 for success).
7593 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7596 if (c
->display_usage
) {
7598 "net rpc printer publish unpublish\n"
7601 _("UnPublish printer in ADS via MSRPC"));
7605 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7606 rpc_printer_publish_unpublish_internals
,
7611 * List published printers via MSRPC.
7613 * @param c A net_context structure.
7614 * @param argc Standard main() style argc.
7615 * @param argv Standard main() style argv. Initial components are already
7618 * @return A shell status integer (0 for success).
7620 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7623 if (c
->display_usage
) {
7625 "net rpc printer publish list\n"
7628 _("List published printers via MSRPC"));
7632 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7633 rpc_printer_publish_list_internals
,
7639 * Publish printer in ADS.
7641 * @param c A net_context structure.
7642 * @param argc Standard main() style argc.
7643 * @param argv Standard main() style argv. Initial components are already
7646 * @return A shell status integer (0 for success).
7648 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7652 struct functable func
[] = {
7655 rpc_printer_publish_publish
,
7657 N_("Publish printer in AD"),
7658 N_("net rpc printer publish publish\n"
7659 " Publish printer in AD")
7663 rpc_printer_publish_update
,
7665 N_("Update printer in AD"),
7666 N_("net rpc printer publish update\n"
7667 " Update printer in AD")
7671 rpc_printer_publish_unpublish
,
7673 N_("Unpublish printer"),
7674 N_("net rpc printer publish unpublish\n"
7675 " Unpublish printer")
7679 rpc_printer_publish_list
,
7681 N_("List published printers"),
7682 N_("net rpc printer publish list\n"
7683 " List published printers")
7685 {NULL
, NULL
, 0, NULL
, NULL
}
7689 if (c
->display_usage
) {
7690 d_printf(_("Usage:\n"));
7691 d_printf(_("net rpc printer publish\n"
7692 " List published printers\n"
7693 " Alias of net rpc printer publish "
7695 net_display_usage_from_functable(func
);
7698 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7699 rpc_printer_publish_list_internals
,
7703 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7709 * Display rpc printer help page.
7711 * @param c A net_context structure.
7712 * @param argc Standard main() style argc.
7713 * @param argv Standard main() style argv. Initial components are already
7716 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7718 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7719 "\tlists all printers on print-server\n\n"));
7720 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7721 "\tlists all printer-drivers on print-server\n\n"));
7722 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7723 "\tpublishes printer settings in Active Directory\n"
7724 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7725 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7726 "\n\tmigrates printers from remote to local server\n\n"));
7727 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7728 "\n\tmigrates printer-settings from remote to local server\n\n"));
7729 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7730 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7731 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7732 "\n\tmigrates printer-forms from remote to local server\n\n"));
7733 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7734 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7735 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7736 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7737 "\tremote to local print-server\n\n"));
7738 net_common_methods_usage(c
, argc
, argv
);
7739 net_common_flags_usage(c
, argc
, argv
);
7741 "\t-v or --verbose\t\t\tgive verbose output\n"
7742 "\t --destination\t\tmigration target server (default: localhost)\n"));
7748 * 'net rpc printer' entrypoint.
7750 * @param c A net_context structure.
7751 * @param argc Standard main() style argc.
7752 * @param argv Standard main() style argv. Initial components are already
7755 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7757 struct functable func
[] = {
7762 N_("List all printers on print server"),
7763 N_("net rpc printer list\n"
7764 " List all printers on print server")
7768 rpc_printer_migrate
,
7770 N_("Migrate printer to local server"),
7771 N_("net rpc printer migrate\n"
7772 " Migrate printer to local server")
7776 rpc_printer_driver_list
,
7778 N_("List printer drivers"),
7779 N_("net rpc printer driver\n"
7780 " List printer drivers")
7784 rpc_printer_publish
,
7786 N_("Publish printer in AD"),
7787 N_("net rpc printer publish\n"
7788 " Publish printer in AD")
7790 {NULL
, NULL
, 0, NULL
, NULL
}
7794 if (c
->display_usage
) {
7795 d_printf(_("Usage:\n"));
7796 d_printf(_("net rpc printer\n"
7797 " List printers\n"));
7798 net_display_usage_from_functable(func
);
7801 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7802 rpc_printer_list_internals
,
7806 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7810 * 'net rpc' entrypoint.
7812 * @param c A net_context structure.
7813 * @param argc Standard main() style argc.
7814 * @param argv Standard main() style argv. Initial components are already
7818 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7820 NET_API_STATUS status
;
7822 struct functable func
[] = {
7827 N_("Modify global audit settings"),
7828 N_("net rpc audit\n"
7829 " Modify global audit settings")
7835 N_("Show basic info about a domain"),
7837 " Show basic info about a domain")
7843 N_("Join a domain"),
7851 N_("Join a domain created in server manager"),
7852 N_("net rpc oldjoin\n"
7853 " Join a domain created in server manager")
7859 N_("Test that a join is valid"),
7860 N_("net rpc testjoin\n"
7861 " Test that a join is valid")
7867 N_("List/modify users"),
7869 " List/modify users")
7875 N_("Change a user password"),
7876 N_("net rpc password\n"
7877 " Change a user password\n"
7878 " Alias for net rpc user password")
7884 N_("List/modify groups"),
7885 N_("net rpc group\n"
7886 " List/modify groups")
7892 N_("List/modify shares"),
7893 N_("net rpc share\n"
7894 " List/modify shares")
7900 N_("List open files"),
7908 N_("List/modify printers"),
7909 N_("net rpc printer\n"
7910 " List/modify printers")
7914 net_rpc_changetrustpw
,
7916 N_("Change trust account password"),
7917 N_("net rpc changetrustpw\n"
7918 " Change trust account password")
7924 N_("Modify domain trusts"),
7925 N_("net rpc trustdom\n"
7926 " Modify domain trusts")
7932 N_("Abort a remote shutdown"),
7933 N_("net rpc abortshutdown\n"
7934 " Abort a remote shutdown")
7940 N_("Shutdown a remote server"),
7941 N_("net rpc shutdown\n"
7942 " Shutdown a remote server")
7948 N_("Dump SAM data of remote NT PDC"),
7949 N_("net rpc samdump\n"
7950 " Dump SAM data of remote NT PDC")
7956 N_("Sync a remote NT PDC's data into local passdb"),
7957 N_("net rpc vampire\n"
7958 " Sync a remote NT PDC's data into local passdb")
7964 N_("Fetch the domain sid into local secrets.tdb"),
7965 N_("net rpc getsid\n"
7966 " Fetch the domain sid into local secrets.tdb")
7972 N_("Manage privileges assigned to SID"),
7973 N_("net rpc rights\n"
7974 " Manage privileges assigned to SID")
7980 N_("Start/stop/query remote services"),
7981 N_("net rpc service\n"
7982 " Start/stop/query remote services")
7988 N_("Manage registry hives"),
7989 N_("net rpc registry\n"
7990 " Manage registry hives")
7996 N_("Open interactive shell on remote server"),
7997 N_("net rpc shell\n"
7998 " Open interactive shell on remote server")
8004 N_("Manage trusts"),
8005 N_("net rpc trust\n"
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
);