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/libsmb.h"
43 #include "libsmb/clirap.h"
44 #include "nsswitch/libwbclient/wbclient.h"
47 static int net_mode_share
;
48 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
);
53 * @brief RPC based subcommands for the 'net' utility.
55 * This file should contain much of the functionality that used to
56 * be found in rpcclient, execpt that the commands should change
57 * less often, and the fucntionality should be sane (the user is not
58 * expected to know a rid/sid before they conduct an operation etc.)
60 * @todo Perhaps eventually these should be split out into a number
61 * of files, as this could get quite big.
66 * Many of the RPC functions need the domain sid. This function gets
67 * it at the start of every run
69 * @param cli A cli_state already connected to the remote machine
71 * @return The Domain SID of the remote machine.
74 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
75 struct dom_sid
**domain_sid
,
76 const char **domain_name
)
78 struct rpc_pipe_client
*lsa_pipe
= NULL
;
79 struct policy_handle pol
;
80 NTSTATUS status
, result
;
81 union lsa_PolicyInformation
*info
= NULL
;
82 struct dcerpc_binding_handle
*b
;
84 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
86 if (!NT_STATUS_IS_OK(status
)) {
87 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
91 b
= lsa_pipe
->binding_handle
;
93 status
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
94 SEC_FLAG_MAXIMUM_ALLOWED
,
96 if (!NT_STATUS_IS_OK(status
)) {
97 d_fprintf(stderr
, "open_policy %s: %s\n",
103 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
105 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
108 if (!NT_STATUS_IS_OK(status
)) {
109 d_fprintf(stderr
, "lsaquery %s: %s\n",
114 if (!NT_STATUS_IS_OK(result
)) {
115 d_fprintf(stderr
, "lsaquery %s: %s\n",
121 *domain_name
= info
->account_domain
.name
.string
;
122 *domain_sid
= info
->account_domain
.sid
;
124 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
125 TALLOC_FREE(lsa_pipe
);
131 * Run a single RPC command, from start to finish.
133 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
134 * @param conn_flag a NET_FLAG_ combination. Passed to
135 * net_make_ipc_connection.
136 * @param argc Standard main() style argc.
137 * @param argv Standard main() style argv. Initial components are already
139 * @return A shell status integer (0 for success).
142 int run_rpc_command(struct net_context
*c
,
143 struct cli_state
*cli_arg
,
144 const struct ndr_syntax_id
*interface
,
150 struct cli_state
*cli
= NULL
;
151 struct rpc_pipe_client
*pipe_hnd
= NULL
;
154 struct dom_sid
*domain_sid
;
155 const char *domain_name
;
158 /* make use of cli_state handed over as an argument, if possible */
160 nt_status
= net_make_ipc_connection(c
, conn_flags
, &cli
);
161 if (!NT_STATUS_IS_OK(nt_status
)) {
162 DEBUG(1, ("failed to make ipc connection: %s\n",
163 nt_errstr(nt_status
)));
176 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
177 DEBUG(0, ("talloc_init() failed\n"));
181 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
183 if (!NT_STATUS_IS_OK(nt_status
)) {
187 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
188 if (lp_client_schannel()
189 && (ndr_syntax_id_equal(interface
,
190 &ndr_table_netlogon
.syntax_id
))) {
191 /* Always try and create an schannel netlogon pipe. */
192 nt_status
= cli_rpc_pipe_open_schannel(
193 cli
, interface
, NCACN_NP
,
194 DCERPC_AUTH_LEVEL_PRIVACY
, domain_name
,
196 if (!NT_STATUS_IS_OK(nt_status
)) {
197 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
198 nt_errstr(nt_status
) ));
202 if (conn_flags
& NET_FLAGS_SEAL
) {
203 nt_status
= cli_rpc_pipe_open_generic_auth(
205 (conn_flags
& NET_FLAGS_TCP
) ?
206 NCACN_IP_TCP
: NCACN_NP
,
207 DCERPC_AUTH_TYPE_NTLMSSP
,
208 DCERPC_AUTH_LEVEL_PRIVACY
,
209 cli_state_remote_name(cli
),
210 lp_workgroup(), c
->opt_user_name
,
211 c
->opt_password
, &pipe_hnd
);
213 nt_status
= cli_rpc_pipe_open_noauth(
217 if (!NT_STATUS_IS_OK(nt_status
)) {
218 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
219 get_pipe_name_from_syntax(
220 talloc_tos(), interface
),
221 nt_errstr(nt_status
) ));
227 nt_status
= fn(c
, domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
229 if (!NT_STATUS_IS_OK(nt_status
)) {
230 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
233 DEBUG(5, ("rpc command function succedded\n"));
236 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
238 TALLOC_FREE(pipe_hnd
);
243 /* close the connection only if it was opened here */
248 talloc_destroy(mem_ctx
);
253 * Force a change of the trust acccount password.
255 * All parameters are provided by the run_rpc_command function, except for
256 * argc, argv which are passed through.
258 * @param domain_sid The domain sid acquired from the remote server.
259 * @param cli A cli_state connected to the server.
260 * @param mem_ctx Talloc context, destroyed on completion of the function.
261 * @param argc Standard main() style argc.
262 * @param argv Standard main() style argv. Initial components are already
265 * @return Normal NTSTATUS return.
268 static NTSTATUS
rpc_changetrustpw_internals(struct net_context
*c
,
269 const struct dom_sid
*domain_sid
,
270 const char *domain_name
,
271 struct cli_state
*cli
,
272 struct rpc_pipe_client
*pipe_hnd
,
279 status
= trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
);
280 if (!NT_STATUS_IS_OK(status
)) {
281 d_fprintf(stderr
, _("Failed to change machine account password: %s\n"),
290 * Force a change of the trust acccount password.
292 * @param argc Standard main() style argc.
293 * @param argv Standard main() style argv. Initial components are already
296 * @return A shell status integer (0 for success).
299 int net_rpc_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
301 if (c
->display_usage
) {
303 "net rpc changetrustpw\n"
306 _("Change the machine trust password"));
310 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
311 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
312 rpc_changetrustpw_internals
,
317 * Join a domain, the old way.
319 * This uses 'machinename' as the inital password, and changes it.
321 * The password should be created with 'server manager' or equiv first.
323 * All parameters are provided by the run_rpc_command function, except for
324 * argc, argv which are passed through.
326 * @param domain_sid The domain sid acquired from the remote server.
327 * @param cli A cli_state connected to the server.
328 * @param mem_ctx Talloc context, destroyed on completion of the function.
329 * @param argc Standard main() style argc.
330 * @param argv Standard main() style argv. Initial components are already
333 * @return Normal NTSTATUS return.
336 static NTSTATUS
rpc_oldjoin_internals(struct net_context
*c
,
337 const struct dom_sid
*domain_sid
,
338 const char *domain_name
,
339 struct cli_state
*cli
,
340 struct rpc_pipe_client
*pipe_hnd
,
346 fstring trust_passwd
;
347 unsigned char orig_trust_passwd_hash
[16];
349 enum netr_SchannelType sec_channel_type
;
351 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
353 if (!NT_STATUS_IS_OK(result
)) {
354 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
356 cli_state_remote_name(cli
),
357 nt_errstr(result
) ));
362 check what type of join - if the user want's to join as
363 a BDC, the server must agree that we are a BDC.
366 sec_channel_type
= get_sec_channel_type(argv
[0]);
368 sec_channel_type
= get_sec_channel_type(NULL
);
371 fstrcpy(trust_passwd
, lp_netbios_name());
372 strlower_m(trust_passwd
);
375 * Machine names can be 15 characters, but the max length on
376 * a password is 14. --jerry
379 trust_passwd
[14] = '\0';
381 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
383 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
,
385 orig_trust_passwd_hash
,
388 if (NT_STATUS_IS_OK(result
))
389 printf(_("Joined domain %s.\n"), c
->opt_target_workgroup
);
392 if (!secrets_store_domain_sid(c
->opt_target_workgroup
, domain_sid
)) {
393 DEBUG(0, ("error storing domain sid for %s\n", c
->opt_target_workgroup
));
394 result
= NT_STATUS_UNSUCCESSFUL
;
401 * Join a domain, the old way.
403 * @param argc Standard main() style argc.
404 * @param argv Standard main() style argv. Initial components are already
407 * @return A shell status integer (0 for success).
410 static int net_rpc_perform_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
412 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
413 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
414 rpc_oldjoin_internals
,
419 * Join a domain, the old way. This function exists to allow
420 * the message to be displayed when oldjoin was explicitly
421 * requested, but not when it was implied by "net rpc join".
423 * @param argc Standard main() style argc.
424 * @param argv Standard main() style argv. Initial components are already
427 * @return A shell status integer (0 for success).
430 static int net_rpc_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
434 if (c
->display_usage
) {
439 _("Join a domain the old way"));
443 rc
= net_rpc_perform_oldjoin(c
, argc
, argv
);
446 d_fprintf(stderr
, _("Failed to join domain\n"));
453 * 'net rpc join' entrypoint.
454 * @param argc Standard main() style argc.
455 * @param argv Standard main() style argv. Initial components are already
458 * Main 'net_rpc_join()' (where the admin username/password is used) is
460 * Try to just change the password, but if that doesn't work, use/prompt
461 * for a username/password.
464 int net_rpc_join(struct net_context
*c
, int argc
, const char **argv
)
466 if (c
->display_usage
) {
469 _("net rpc join -U <username>[%%password] <type>\n"
471 " username\tName of the admin user"
472 " password\tPassword of the admin user, will "
473 "prompt if not specified\n"
474 " type\tCan be one of the following:\n"
475 "\t\tMEMBER\tJoin as member server (default)\n"
476 "\t\tBDC\tJoin as BDC\n"
477 "\t\tPDC\tJoin as PDC\n"));
481 if (lp_server_role() == ROLE_STANDALONE
) {
482 d_printf(_("cannot join as standalone machine\n"));
486 if (strlen(lp_netbios_name()) > 15) {
487 d_printf(_("Our netbios name can be at most 15 chars long, "
488 "\"%s\" is %u chars long\n"),
489 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
493 if ((net_rpc_perform_oldjoin(c
, argc
, argv
) == 0))
496 return net_rpc_join_newstyle(c
, argc
, argv
);
500 * display info about a rpc domain
502 * All parameters are provided by the run_rpc_command function, except for
503 * argc, argv which are passed through.
505 * @param domain_sid The domain sid acquired from the remote server
506 * @param cli A cli_state connected to the server.
507 * @param mem_ctx Talloc context, destroyed on completion of the function.
508 * @param argc Standard main() style argc.
509 * @param argv Standard main() style argv. Initial components are already
512 * @return Normal NTSTATUS return.
515 NTSTATUS
rpc_info_internals(struct net_context
*c
,
516 const struct dom_sid
*domain_sid
,
517 const char *domain_name
,
518 struct cli_state
*cli
,
519 struct rpc_pipe_client
*pipe_hnd
,
524 struct policy_handle connect_pol
, domain_pol
;
525 NTSTATUS status
, result
;
526 union samr_DomainInfo
*info
= NULL
;
528 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
530 sid_to_fstring(sid_str
, domain_sid
);
532 /* Get sam policy handle */
533 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
535 MAXIMUM_ALLOWED_ACCESS
,
538 if (!NT_STATUS_IS_OK(status
)) {
539 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
544 if (!NT_STATUS_IS_OK(result
)) {
546 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
551 /* Get domain policy handle */
552 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
554 MAXIMUM_ALLOWED_ACCESS
,
555 discard_const_p(struct dom_sid2
, domain_sid
),
558 if (!NT_STATUS_IS_OK(status
)) {
559 d_fprintf(stderr
, _("Could not open domain: %s\n"),
563 if (!NT_STATUS_IS_OK(result
)) {
565 d_fprintf(stderr
, _("Could not open domain: %s\n"),
570 status
= dcerpc_samr_QueryDomainInfo(b
, mem_ctx
,
575 if (!NT_STATUS_IS_OK(status
)) {
579 if (NT_STATUS_IS_OK(result
)) {
580 d_printf(_("Domain Name: %s\n"),
581 info
->general
.domain_name
.string
);
582 d_printf(_("Domain SID: %s\n"), sid_str
);
583 d_printf(_("Sequence number: %llu\n"),
584 (unsigned long long)info
->general
.sequence_num
);
585 d_printf(_("Num users: %u\n"), info
->general
.num_users
);
586 d_printf(_("Num domain groups: %u\n"),info
->general
.num_groups
);
587 d_printf(_("Num local groups: %u\n"),info
->general
.num_aliases
);
595 * 'net rpc info' entrypoint.
596 * @param argc Standard main() style argc.
597 * @param argv Standard main() style argv. Initial components are already
601 int net_rpc_info(struct net_context
*c
, int argc
, const char **argv
)
603 if (c
->display_usage
) {
608 _("Display information about the domain"));
612 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
613 NET_FLAGS_PDC
, rpc_info_internals
,
618 * Fetch domain SID into the local secrets.tdb.
620 * All parameters are provided by the run_rpc_command function, except for
621 * argc, argv which are passed through.
623 * @param domain_sid The domain sid acquired from the remote server.
624 * @param cli A cli_state connected to the server.
625 * @param mem_ctx Talloc context, destroyed on completion of the function.
626 * @param argc Standard main() style argc.
627 * @param argv Standard main() style argv. Initial components are already
630 * @return Normal NTSTATUS return.
633 static NTSTATUS
rpc_getsid_internals(struct net_context
*c
,
634 const struct dom_sid
*domain_sid
,
635 const char *domain_name
,
636 struct cli_state
*cli
,
637 struct rpc_pipe_client
*pipe_hnd
,
644 sid_to_fstring(sid_str
, domain_sid
);
645 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
646 sid_str
, domain_name
);
648 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
649 DEBUG(0,("Can't store domain SID\n"));
650 return NT_STATUS_UNSUCCESSFUL
;
657 * 'net rpc getsid' entrypoint.
658 * @param argc Standard main() style argc.
659 * @param argv Standard main() style argv. Initial components are already
663 int net_rpc_getsid(struct net_context
*c
, int argc
, const char **argv
)
665 int conn_flags
= NET_FLAGS_PDC
;
667 if (!c
->opt_user_specified
) {
668 conn_flags
|= NET_FLAGS_ANONYMOUS
;
671 if (c
->display_usage
) {
676 _("Fetch domain SID into local secrets.tdb"));
680 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
682 rpc_getsid_internals
,
686 /****************************************************************************/
689 * Basic usage function for 'net rpc user'.
690 * @param argc Standard main() style argc.
691 * @param argv Standard main() style argv. Initial components are already
695 static int rpc_user_usage(struct net_context
*c
, int argc
, const char **argv
)
697 return net_user_usage(c
, argc
, argv
);
701 * Add a new user to a remote RPC server.
703 * @param argc Standard main() style argc.
704 * @param argv Standard main() style argv. Initial components are already
707 * @return A shell status integer (0 for success).
710 static int rpc_user_add(struct net_context
*c
, int argc
, const char **argv
)
712 NET_API_STATUS status
;
713 struct USER_INFO_1 info1
;
714 uint32_t parm_error
= 0;
716 if (argc
< 1 || c
->display_usage
) {
717 rpc_user_usage(c
, argc
, argv
);
723 info1
.usri1_name
= argv
[0];
725 info1
.usri1_password
= argv
[1];
728 status
= NetUserAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
731 d_fprintf(stderr
,_("Failed to add user '%s' with error: %s.\n"),
732 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
736 d_printf(_("Added user '%s'.\n"), argv
[0]);
743 * Rename a user on a remote RPC server.
745 * @param argc Standard main() style argc.
746 * @param argv Standard main() style argv. Initial components are already
749 * @return A shell status integer (0 for success).
752 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
754 NET_API_STATUS status
;
755 struct USER_INFO_0 u0
;
756 uint32_t parm_err
= 0;
758 if (argc
!= 2 || c
->display_usage
) {
759 rpc_user_usage(c
, argc
, argv
);
763 u0
.usri0_name
= argv
[1];
765 status
= NetUserSetInfo(c
->opt_host
, argv
[0],
766 0, (uint8_t *)&u0
, &parm_err
);
769 _("Failed to rename user from %s to %s - %s\n"),
771 libnetapi_get_error_string(c
->netapi_ctx
, status
));
773 d_printf(_("Renamed user from %s to %s\n"), argv
[0], argv
[1]);
780 * Set a user's primary group
782 * @param argc Standard main() style argc.
783 * @param argv Standard main() style argv. Initial components are already
786 * @return A shell status integer (0 for success).
789 static int rpc_user_setprimarygroup(struct net_context
*c
, int argc
,
792 NET_API_STATUS status
;
794 struct GROUP_INFO_2
*g2
;
795 struct USER_INFO_1051 u1051
;
796 uint32_t parm_err
= 0;
798 if (argc
!= 2 || c
->display_usage
) {
799 rpc_user_usage(c
, argc
, argv
);
803 status
= NetGroupGetInfo(c
->opt_host
, argv
[1], 2, &buffer
);
805 d_fprintf(stderr
, _("Failed to find group name %s -- %s\n"),
807 libnetapi_get_error_string(c
->netapi_ctx
, status
));
810 g2
= (struct GROUP_INFO_2
*)buffer
;
812 u1051
.usri1051_primary_group_id
= g2
->grpi2_group_id
;
814 NetApiBufferFree(buffer
);
816 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1051,
817 (uint8_t *)&u1051
, &parm_err
);
820 _("Failed to set user's primary group %s to %s - "
821 "%s\n"), argv
[0], argv
[1],
822 libnetapi_get_error_string(c
->netapi_ctx
, status
));
824 d_printf(_("Set primary group of user %s to %s\n"), argv
[0],
831 * Delete a user from a remote RPC server.
833 * @param argc Standard main() style argc.
834 * @param argv Standard main() style argv. Initial components are already
837 * @return A shell status integer (0 for success).
840 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
842 NET_API_STATUS status
;
844 if (argc
< 1 || c
->display_usage
) {
845 rpc_user_usage(c
, argc
, argv
);
849 status
= NetUserDel(c
->opt_host
, argv
[0]);
852 d_fprintf(stderr
, _("Failed to delete user '%s' with: %s.\n"),
854 libnetapi_get_error_string(c
->netapi_ctx
, status
));
857 d_printf(_("Deleted user '%s'.\n"), argv
[0]);
864 * Set a user's password on a remote RPC server.
866 * @param argc Standard main() style argc.
867 * @param argv Standard main() style argv. Initial components are already
870 * @return A shell status integer (0 for success).
873 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
875 NET_API_STATUS status
;
877 struct USER_INFO_1003 u1003
;
878 uint32_t parm_err
= 0;
881 if (argc
< 1 || c
->display_usage
) {
882 rpc_user_usage(c
, argc
, argv
);
887 u1003
.usri1003_password
= argv
[1];
889 ret
= asprintf(&prompt
, _("Enter new password for %s:"),
894 u1003
.usri1003_password
= talloc_strdup(c
, getpass(prompt
));
896 if (u1003
.usri1003_password
== NULL
) {
901 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
903 /* Display results */
906 _("Failed to set password for '%s' with error: %s.\n"),
907 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
916 * List a user's groups from a remote RPC server.
918 * @param argc Standard main() style argc.
919 * @param argv Standard main() style argv. Initial components are already
922 * @return A shell status integer (0 for success)
925 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
928 NET_API_STATUS status
;
929 struct GROUP_USERS_INFO_0
*u0
= NULL
;
930 uint32_t entries_read
= 0;
931 uint32_t total_entries
= 0;
935 if (argc
< 1 || c
->display_usage
) {
936 rpc_user_usage(c
, argc
, argv
);
940 status
= NetUserGetGroups(c
->opt_host
,
943 (uint8_t **)(void *)&u0
,
949 _("Failed to get groups for '%s' with error: %s.\n"),
950 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
955 for (i
=0; i
< entries_read
; i
++) {
956 printf("%s\n", u0
->grui0_name
);
964 * List users on a remote RPC server.
966 * All parameters are provided by the run_rpc_command function, except for
967 * argc, argv which are passed through.
969 * @param domain_sid The domain sid acquired from the remote server.
970 * @param cli A cli_state connected to the server.
971 * @param mem_ctx Talloc context, destroyed on completion of the function.
972 * @param argc Standard main() style argc.
973 * @param argv Standard main() style argv. Initial components are already
976 * @return Normal NTSTATUS return.
979 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
981 NET_API_STATUS status
;
982 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
983 struct NET_DISPLAY_USER
*info
= NULL
;
986 /* Query domain users */
987 if (c
->opt_long_list_entries
)
988 d_printf(_("\nUser name Comment"
989 "\n-----------------------------\n"));
991 uint32_t max_entries
, max_size
;
993 dcerpc_get_query_dispinfo_params(
994 loop_count
, &max_entries
, &max_size
);
996 status
= NetQueryDisplayInformation(c
->opt_host
,
1003 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
1007 info
= (struct NET_DISPLAY_USER
*)buffer
;
1009 for (i
= 0; i
< num_entries
; i
++) {
1011 if (c
->opt_long_list_entries
)
1012 printf("%-21.21s %s\n", info
->usri1_name
,
1013 info
->usri1_comment
);
1015 printf("%s\n", info
->usri1_name
);
1019 NetApiBufferFree(buffer
);
1022 start_idx
+= num_entries
;
1024 } while (status
== ERROR_MORE_DATA
);
1030 * 'net rpc user' entrypoint.
1031 * @param argc Standard main() style argc.
1032 * @param argv Standard main() style argv. Initial components are already
1036 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
1038 NET_API_STATUS status
;
1040 struct functable func
[] = {
1045 N_("Add specified user"),
1046 N_("net rpc user add\n"
1047 " Add specified user")
1053 N_("List domain groups of user"),
1054 N_("net rpc user info\n"
1055 " List domain groups of user")
1061 N_("Remove specified user"),
1062 N_("net rpc user delete\n"
1063 " Remove specified user")
1069 N_("Change user password"),
1070 N_("net rpc user password\n"
1071 " Change user password")
1077 N_("Rename specified user"),
1078 N_("net rpc user rename\n"
1079 " Rename specified user")
1083 rpc_user_setprimarygroup
,
1085 "Set a user's primary group",
1086 "net rpc user setprimarygroup\n"
1087 " Set a user's primary group"
1089 {NULL
, NULL
, 0, NULL
, NULL
}
1092 status
= libnetapi_net_init(&c
->netapi_ctx
);
1096 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1097 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1098 if (c
->opt_kerberos
) {
1099 libnetapi_set_use_kerberos(c
->netapi_ctx
);
1103 if (c
->display_usage
) {
1108 _("List all users"));
1109 net_display_usage_from_functable(func
);
1113 return rpc_user_list(c
, argc
, argv
);
1116 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
1119 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1120 TALLOC_CTX
*mem_ctx
,
1121 struct rpc_sh_ctx
*ctx
,
1122 struct rpc_pipe_client
*pipe_hnd
,
1123 int argc
, const char **argv
)
1125 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
1128 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1129 TALLOC_CTX
*mem_ctx
,
1130 struct rpc_sh_ctx
*ctx
,
1131 struct rpc_pipe_client
*pipe_hnd
,
1132 int argc
, const char **argv
)
1134 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
1137 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1138 TALLOC_CTX
*mem_ctx
,
1139 struct rpc_sh_ctx
*ctx
,
1140 struct rpc_pipe_client
*pipe_hnd
,
1141 int argc
, const char **argv
,
1143 struct net_context
*c
,
1144 TALLOC_CTX
*mem_ctx
,
1145 struct rpc_sh_ctx
*ctx
,
1146 struct rpc_pipe_client
*pipe_hnd
,
1147 struct policy_handle
*user_hnd
,
1148 int argc
, const char **argv
))
1150 struct policy_handle connect_pol
, domain_pol
, user_pol
;
1151 NTSTATUS status
, result
;
1154 enum lsa_SidType type
;
1155 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1158 d_fprintf(stderr
, "%s %s <username>\n", _("Usage:"),
1160 return NT_STATUS_INVALID_PARAMETER
;
1163 ZERO_STRUCT(connect_pol
);
1164 ZERO_STRUCT(domain_pol
);
1165 ZERO_STRUCT(user_pol
);
1167 status
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1168 argv
[0], NULL
, NULL
, &sid
, &type
);
1169 if (!NT_STATUS_IS_OK(status
)) {
1170 d_fprintf(stderr
, _("Could not lookup %s: %s\n"), argv
[0],
1175 if (type
!= SID_NAME_USER
) {
1176 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
1177 sid_type_lookup(type
));
1178 status
= NT_STATUS_NO_SUCH_USER
;
1182 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1183 d_fprintf(stderr
, _("%s is not in our domain\n"), argv
[0]);
1184 status
= NT_STATUS_NO_SUCH_USER
;
1188 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1190 MAXIMUM_ALLOWED_ACCESS
,
1193 if (!NT_STATUS_IS_OK(status
)) {
1196 if (!NT_STATUS_IS_OK(result
)) {
1201 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1203 MAXIMUM_ALLOWED_ACCESS
,
1207 if (!NT_STATUS_IS_OK(status
)) {
1210 if (!NT_STATUS_IS_OK(result
)) {
1215 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1217 MAXIMUM_ALLOWED_ACCESS
,
1221 if (!NT_STATUS_IS_OK(status
)) {
1224 if (!NT_STATUS_IS_OK(result
)) {
1229 status
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1232 if (is_valid_policy_hnd(&user_pol
)) {
1233 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1235 if (is_valid_policy_hnd(&domain_pol
)) {
1236 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1238 if (is_valid_policy_hnd(&connect_pol
)) {
1239 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
1244 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1245 TALLOC_CTX
*mem_ctx
,
1246 struct rpc_sh_ctx
*ctx
,
1247 struct rpc_pipe_client
*pipe_hnd
,
1248 struct policy_handle
*user_hnd
,
1249 int argc
, const char **argv
)
1251 NTSTATUS status
, result
;
1252 union samr_UserInfo
*info
= NULL
;
1253 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1256 d_fprintf(stderr
, "%s %s show <username>\n", _("Usage:"),
1258 return NT_STATUS_INVALID_PARAMETER
;
1261 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1266 if (!NT_STATUS_IS_OK(status
)) {
1269 if (!NT_STATUS_IS_OK(result
)) {
1273 d_printf(_("user rid: %d, group rid: %d\n"),
1275 info
->info21
.primary_gid
);
1280 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1281 TALLOC_CTX
*mem_ctx
,
1282 struct rpc_sh_ctx
*ctx
,
1283 struct rpc_pipe_client
*pipe_hnd
,
1284 int argc
, const char **argv
)
1286 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1287 rpc_sh_user_show_internals
);
1290 #define FETCHSTR(name, rec) \
1291 do { if (strequal(ctx->thiscmd, name)) { \
1292 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1295 #define SETSTR(name, rec, flag) \
1296 do { if (strequal(ctx->thiscmd, name)) { \
1297 init_lsa_String(&(info->info21.rec), argv[0]); \
1298 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1301 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1302 TALLOC_CTX
*mem_ctx
,
1303 struct rpc_sh_ctx
*ctx
,
1304 struct rpc_pipe_client
*pipe_hnd
,
1305 struct policy_handle
*user_hnd
,
1306 int argc
, const char **argv
)
1308 NTSTATUS status
, result
;
1309 const char *username
;
1310 const char *oldval
= "";
1311 union samr_UserInfo
*info
= NULL
;
1312 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1315 d_fprintf(stderr
, "%s %s <username> [new value|NULL]\n",
1316 _("Usage:"), ctx
->whoami
);
1317 return NT_STATUS_INVALID_PARAMETER
;
1320 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1325 if (!NT_STATUS_IS_OK(status
)) {
1328 if (!NT_STATUS_IS_OK(result
)) {
1332 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1334 FETCHSTR("fullname", full_name
);
1335 FETCHSTR("homedir", home_directory
);
1336 FETCHSTR("homedrive", home_drive
);
1337 FETCHSTR("logonscript", logon_script
);
1338 FETCHSTR("profilepath", profile_path
);
1339 FETCHSTR("description", description
);
1342 d_printf(_("%s's %s: [%s]\n"), username
, ctx
->thiscmd
, oldval
);
1346 if (strcmp(argv
[0], "NULL") == 0) {
1350 ZERO_STRUCT(info
->info21
);
1352 SETSTR("fullname", full_name
, FULL_NAME
);
1353 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1354 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1355 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1356 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1357 SETSTR("description", description
, DESCRIPTION
);
1359 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1364 if (!NT_STATUS_IS_OK(status
)) {
1370 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username
,
1371 ctx
->thiscmd
, oldval
, argv
[0]);
1378 #define HANDLEFLG(name, rec) \
1379 do { if (strequal(ctx->thiscmd, name)) { \
1380 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1382 newflags = oldflags | ACB_##rec; \
1384 newflags = oldflags & ~ACB_##rec; \
1387 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1388 TALLOC_CTX
*mem_ctx
,
1389 struct rpc_sh_ctx
*ctx
,
1390 struct rpc_pipe_client
*pipe_hnd
,
1391 int argc
, const char **argv
)
1393 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1394 rpc_sh_user_str_edit_internals
);
1397 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1398 TALLOC_CTX
*mem_ctx
,
1399 struct rpc_sh_ctx
*ctx
,
1400 struct rpc_pipe_client
*pipe_hnd
,
1401 struct policy_handle
*user_hnd
,
1402 int argc
, const char **argv
)
1404 NTSTATUS status
, result
;
1405 const char *username
;
1406 const char *oldval
= "unknown";
1407 uint32 oldflags
, newflags
;
1409 union samr_UserInfo
*info
= NULL
;
1410 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1413 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1414 !strequal(argv
[0], "no"))) {
1415 /* TRANSATORS: The yes|no here are program keywords. Please do
1417 d_fprintf(stderr
, _("Usage: %s <username> [yes|no]\n"),
1419 return NT_STATUS_INVALID_PARAMETER
;
1422 newval
= strequal(argv
[0], "yes");
1424 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1429 if (!NT_STATUS_IS_OK(status
)) {
1432 if (!NT_STATUS_IS_OK(result
)) {
1436 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1437 oldflags
= info
->info21
.acct_flags
;
1438 newflags
= info
->info21
.acct_flags
;
1440 HANDLEFLG("disabled", DISABLED
);
1441 HANDLEFLG("pwnotreq", PWNOTREQ
);
1442 HANDLEFLG("autolock", AUTOLOCK
);
1443 HANDLEFLG("pwnoexp", PWNOEXP
);
1446 d_printf(_("%s's %s flag: %s\n"), username
, ctx
->thiscmd
,
1451 ZERO_STRUCT(info
->info21
);
1453 info
->info21
.acct_flags
= newflags
;
1454 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1456 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1461 if (!NT_STATUS_IS_OK(status
)) {
1465 if (NT_STATUS_IS_OK(result
)) {
1466 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username
,
1467 ctx
->thiscmd
, oldval
, argv
[0]);
1475 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1476 TALLOC_CTX
*mem_ctx
,
1477 struct rpc_sh_ctx
*ctx
,
1478 struct rpc_pipe_client
*pipe_hnd
,
1479 int argc
, const char **argv
)
1481 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1482 rpc_sh_user_flag_edit_internals
);
1485 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1486 TALLOC_CTX
*mem_ctx
,
1487 struct rpc_sh_ctx
*ctx
)
1489 static struct rpc_sh_cmd cmds
[] = {
1491 { "fullname", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1492 N_("Show/Set a user's full name") },
1494 { "homedir", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1495 N_("Show/Set a user's home directory") },
1497 { "homedrive", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1498 N_("Show/Set a user's home drive") },
1500 { "logonscript", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1501 N_("Show/Set a user's logon script") },
1503 { "profilepath", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1504 N_("Show/Set a user's profile path") },
1506 { "description", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1507 N_("Show/Set a user's description") },
1509 { "disabled", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1510 N_("Show/Set whether a user is disabled") },
1512 { "autolock", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1513 N_("Show/Set whether a user locked out") },
1515 { "pwnotreq", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1516 N_("Show/Set whether a user does not need a password") },
1518 { "pwnoexp", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1519 N_("Show/Set whether a user's password does not expire") },
1521 { NULL
, NULL
, 0, NULL
, NULL
}
1527 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1528 TALLOC_CTX
*mem_ctx
,
1529 struct rpc_sh_ctx
*ctx
)
1531 static struct rpc_sh_cmd cmds
[] = {
1533 { "list", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_list
,
1534 N_("List available users") },
1536 { "info", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_info
,
1537 N_("List the domain groups a user is member of") },
1539 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_show
,
1540 N_("Show info about a user") },
1542 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1543 N_("Show/Modify a user's fields") },
1545 { NULL
, NULL
, 0, NULL
, NULL
}
1551 /****************************************************************************/
1554 * Basic usage function for 'net rpc group'.
1555 * @param argc Standard main() style argc.
1556 * @param argv Standard main() style argv. Initial components are already
1560 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1562 return net_group_usage(c
, argc
, argv
);
1566 * Delete group on a remote RPC server.
1568 * All parameters are provided by the run_rpc_command function, except for
1569 * argc, argv which are passed through.
1571 * @param domain_sid The domain sid acquired from the remote server.
1572 * @param cli A cli_state connected to the server.
1573 * @param mem_ctx Talloc context, destroyed on completion of the function.
1574 * @param argc Standard main() style argc.
1575 * @param argv Standard main() style argv. Initial components are already
1578 * @return Normal NTSTATUS return.
1581 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1582 const struct dom_sid
*domain_sid
,
1583 const char *domain_name
,
1584 struct cli_state
*cli
,
1585 struct rpc_pipe_client
*pipe_hnd
,
1586 TALLOC_CTX
*mem_ctx
,
1590 struct policy_handle connect_pol
, domain_pol
, group_pol
, user_pol
;
1591 bool group_is_primary
= false;
1592 NTSTATUS status
, result
;
1594 struct samr_RidAttrArray
*rids
= NULL
;
1597 /* struct samr_RidWithAttribute *user_gids; */
1598 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1600 struct samr_Ids group_rids
, name_types
;
1601 struct lsa_String lsa_acct_name
;
1602 union samr_UserInfo
*info
= NULL
;
1604 if (argc
< 1 || c
->display_usage
) {
1605 rpc_group_usage(c
, argc
,argv
);
1606 return NT_STATUS_OK
; /* ok? */
1609 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1611 MAXIMUM_ALLOWED_ACCESS
,
1614 if (!NT_STATUS_IS_OK(status
)) {
1615 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1619 if (!NT_STATUS_IS_OK(result
)) {
1621 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1625 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1627 MAXIMUM_ALLOWED_ACCESS
,
1628 discard_const_p(struct dom_sid2
, domain_sid
),
1631 if (!NT_STATUS_IS_OK(status
)) {
1632 d_fprintf(stderr
, _("Request open_domain failed\n"));
1636 if (!NT_STATUS_IS_OK(result
)) {
1638 d_fprintf(stderr
, _("Request open_domain failed\n"));
1642 init_lsa_String(&lsa_acct_name
, argv
[0]);
1644 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1651 if (!NT_STATUS_IS_OK(status
)) {
1652 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1656 if (!NT_STATUS_IS_OK(result
)) {
1658 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1662 switch (name_types
.ids
[0])
1664 case SID_NAME_DOM_GRP
:
1665 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
1667 MAXIMUM_ALLOWED_ACCESS
,
1671 if (!NT_STATUS_IS_OK(status
)) {
1672 d_fprintf(stderr
, _("Request open_group failed"));
1676 if (!NT_STATUS_IS_OK(result
)) {
1678 d_fprintf(stderr
, _("Request open_group failed"));
1682 group_rid
= group_rids
.ids
[0];
1684 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
1688 if (!NT_STATUS_IS_OK(status
)) {
1690 _("Unable to query group members of %s"),
1695 if (!NT_STATUS_IS_OK(result
)) {
1698 _("Unable to query group members of %s"),
1703 if (c
->opt_verbose
) {
1705 _("Domain Group %s (rid: %d) has %d members\n"),
1706 argv
[0],group_rid
, rids
->count
);
1709 /* Check if group is anyone's primary group */
1710 for (i
= 0; i
< rids
->count
; i
++)
1712 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1714 MAXIMUM_ALLOWED_ACCESS
,
1718 if (!NT_STATUS_IS_OK(status
)) {
1720 _("Unable to open group member %d\n"),
1725 if (!NT_STATUS_IS_OK(result
)) {
1728 _("Unable to open group member %d\n"),
1733 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1738 if (!NT_STATUS_IS_OK(status
)) {
1740 _("Unable to lookup userinfo for group "
1746 if (!NT_STATUS_IS_OK(result
)) {
1749 _("Unable to lookup userinfo for group "
1755 if (info
->info21
.primary_gid
== group_rid
) {
1756 if (c
->opt_verbose
) {
1757 d_printf(_("Group is primary group "
1759 info
->info21
.account_name
.string
);
1761 group_is_primary
= true;
1764 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1767 if (group_is_primary
) {
1768 d_fprintf(stderr
, _("Unable to delete group because "
1769 "some of it's members have it as primary "
1771 status
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1775 /* remove all group members */
1776 for (i
= 0; i
< rids
->count
; i
++)
1779 d_printf(_("Remove group member %d..."),
1781 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
1785 if (!NT_STATUS_IS_OK(status
)) {
1789 if (NT_STATUS_IS_OK(result
)) {
1791 d_printf(_("ok\n"));
1794 d_printf("%s\n", _("failed"));
1799 status
= dcerpc_samr_DeleteDomainGroup(b
, mem_ctx
,
1802 if (!NT_STATUS_IS_OK(status
)) {
1809 /* removing a local group is easier... */
1810 case SID_NAME_ALIAS
:
1811 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
1813 MAXIMUM_ALLOWED_ACCESS
,
1817 if (!NT_STATUS_IS_OK(status
)) {
1818 d_fprintf(stderr
, _("Request open_alias failed\n"));
1821 if (!NT_STATUS_IS_OK(result
)) {
1823 d_fprintf(stderr
, _("Request open_alias failed\n"));
1827 status
= dcerpc_samr_DeleteDomAlias(b
, mem_ctx
,
1830 if (!NT_STATUS_IS_OK(status
)) {
1838 d_fprintf(stderr
, _("%s is of type %s. This command is only "
1839 "for deleting local or global groups\n"),
1840 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1841 status
= NT_STATUS_UNSUCCESSFUL
;
1845 if (NT_STATUS_IS_OK(status
)) {
1847 d_printf(_("Deleted %s '%s'\n"),
1848 sid_type_lookup(name_types
.ids
[0]), argv
[0]);
1850 d_fprintf(stderr
, _("Deleting of %s failed: %s\n"), argv
[0],
1851 get_friendly_nt_error_msg(status
));
1859 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1861 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1862 rpc_group_delete_internals
, argc
,argv
);
1865 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1867 NET_API_STATUS status
;
1868 struct GROUP_INFO_1 info1
;
1869 uint32_t parm_error
= 0;
1871 if (argc
!= 1 || c
->display_usage
) {
1872 rpc_group_usage(c
, argc
, argv
);
1878 info1
.grpi1_name
= argv
[0];
1879 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1880 info1
.grpi1_comment
= c
->opt_comment
;
1883 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1887 _("Failed to add group '%s' with error: %s.\n"),
1888 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1892 d_printf(_("Added group '%s'.\n"), argv
[0]);
1898 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1900 NET_API_STATUS status
;
1901 struct LOCALGROUP_INFO_1 info1
;
1902 uint32_t parm_error
= 0;
1904 if (argc
!= 1 || c
->display_usage
) {
1905 rpc_group_usage(c
, argc
, argv
);
1911 info1
.lgrpi1_name
= argv
[0];
1912 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1913 info1
.lgrpi1_comment
= c
->opt_comment
;
1916 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1920 _("Failed to add alias '%s' with error: %s.\n"),
1921 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1925 d_printf(_("Added alias '%s'.\n"), argv
[0]);
1931 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1933 if (c
->opt_localgroup
)
1934 return rpc_alias_add_internals(c
, argc
, argv
);
1936 return rpc_group_add_internals(c
, argc
, argv
);
1939 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1940 TALLOC_CTX
*mem_ctx
,
1942 struct dom_sid
*sid
,
1943 enum lsa_SidType
*type
)
1945 struct dom_sid
*sids
= NULL
;
1946 enum lsa_SidType
*types
= NULL
;
1947 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1948 struct policy_handle lsa_pol
;
1949 NTSTATUS status
, result
;
1950 struct dcerpc_binding_handle
*b
;
1952 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1954 if (!NT_STATUS_IS_OK(status
)) {
1958 b
= pipe_hnd
->binding_handle
;
1960 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1961 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1963 if (!NT_STATUS_IS_OK(status
)) {
1967 status
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1968 &name
, NULL
, 1, &sids
, &types
);
1970 if (NT_STATUS_IS_OK(status
)) {
1971 sid_copy(sid
, &sids
[0]);
1975 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1979 TALLOC_FREE(pipe_hnd
);
1982 if (!NT_STATUS_IS_OK(status
) && (strncasecmp_m(name
, "S-", 2) == 0)) {
1984 /* Try as S-1-5-whatever */
1986 struct dom_sid tmp_sid
;
1988 if (string_to_sid(&tmp_sid
, name
)) {
1989 sid_copy(sid
, &tmp_sid
);
1990 *type
= SID_NAME_UNKNOWN
;
1991 status
= NT_STATUS_OK
;
1998 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
1999 TALLOC_CTX
*mem_ctx
,
2000 const struct dom_sid
*group_sid
,
2003 struct policy_handle connect_pol
, domain_pol
;
2004 NTSTATUS status
, result
;
2006 struct policy_handle group_pol
;
2007 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2009 struct samr_Ids rids
, rid_types
;
2010 struct lsa_String lsa_acct_name
;
2014 sid_copy(&sid
, group_sid
);
2016 if (!sid_split_rid(&sid
, &group_rid
)) {
2017 return NT_STATUS_UNSUCCESSFUL
;
2020 /* Get sam policy handle */
2021 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2023 MAXIMUM_ALLOWED_ACCESS
,
2026 if (!NT_STATUS_IS_OK(status
)) {
2029 if (!NT_STATUS_IS_OK(result
)) {
2033 /* Get domain policy handle */
2034 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2036 MAXIMUM_ALLOWED_ACCESS
,
2040 if (!NT_STATUS_IS_OK(status
)) {
2043 if (!NT_STATUS_IS_OK(result
)) {
2047 init_lsa_String(&lsa_acct_name
, member
);
2049 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2056 if (!NT_STATUS_IS_OK(status
)) {
2057 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2062 if (!NT_STATUS_IS_OK(result
)) {
2064 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2069 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2071 MAXIMUM_ALLOWED_ACCESS
,
2075 if (!NT_STATUS_IS_OK(status
)) {
2079 if (!NT_STATUS_IS_OK(result
)) {
2084 status
= dcerpc_samr_AddGroupMember(b
, mem_ctx
,
2087 0x0005, /* unknown flags */
2089 if (!NT_STATUS_IS_OK(status
)) {
2096 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2100 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2101 TALLOC_CTX
*mem_ctx
,
2102 const struct dom_sid
*alias_sid
,
2105 struct policy_handle connect_pol
, domain_pol
;
2106 NTSTATUS status
, result
;
2108 struct policy_handle alias_pol
;
2109 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2111 struct dom_sid member_sid
;
2112 enum lsa_SidType member_type
;
2116 sid_copy(&sid
, alias_sid
);
2118 if (!sid_split_rid(&sid
, &alias_rid
)) {
2119 return NT_STATUS_UNSUCCESSFUL
;
2122 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2123 member
, &member_sid
, &member_type
);
2125 if (!NT_STATUS_IS_OK(result
)) {
2126 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2131 /* Get sam policy handle */
2132 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2134 MAXIMUM_ALLOWED_ACCESS
,
2137 if (!NT_STATUS_IS_OK(status
)) {
2140 if (!NT_STATUS_IS_OK(result
)) {
2145 /* Get domain policy handle */
2146 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2148 MAXIMUM_ALLOWED_ACCESS
,
2152 if (!NT_STATUS_IS_OK(status
)) {
2155 if (!NT_STATUS_IS_OK(result
)) {
2160 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2162 MAXIMUM_ALLOWED_ACCESS
,
2166 if (!NT_STATUS_IS_OK(status
)) {
2169 if (!NT_STATUS_IS_OK(result
)) {
2173 status
= dcerpc_samr_AddAliasMember(b
, mem_ctx
,
2177 if (!NT_STATUS_IS_OK(status
)) {
2184 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2188 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
2189 const struct dom_sid
*domain_sid
,
2190 const char *domain_name
,
2191 struct cli_state
*cli
,
2192 struct rpc_pipe_client
*pipe_hnd
,
2193 TALLOC_CTX
*mem_ctx
,
2197 struct dom_sid group_sid
;
2198 enum lsa_SidType group_type
;
2200 if (argc
!= 2 || c
->display_usage
) {
2203 _("net rpc group addmem <group> <member>\n"
2204 " Add a member to a group\n"
2205 " group\tGroup to add member to\n"
2206 " member\tMember to add to group\n"));
2207 return NT_STATUS_UNSUCCESSFUL
;
2210 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2211 &group_sid
, &group_type
))) {
2212 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2214 return NT_STATUS_UNSUCCESSFUL
;
2217 if (group_type
== SID_NAME_DOM_GRP
) {
2218 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2219 &group_sid
, argv
[1]);
2221 if (!NT_STATUS_IS_OK(result
)) {
2222 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2223 argv
[1], argv
[0], nt_errstr(result
));
2228 if (group_type
== SID_NAME_ALIAS
) {
2229 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2230 &group_sid
, argv
[1]);
2232 if (!NT_STATUS_IS_OK(result
)) {
2233 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2234 argv
[1], argv
[0], nt_errstr(result
));
2239 d_fprintf(stderr
, _("Can only add members to global or local groups "
2240 "which %s is not\n"), argv
[0]);
2242 return NT_STATUS_UNSUCCESSFUL
;
2245 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2247 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2248 rpc_group_addmem_internals
,
2252 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2253 struct rpc_pipe_client
*pipe_hnd
,
2254 TALLOC_CTX
*mem_ctx
,
2255 const struct dom_sid
*group_sid
,
2258 struct policy_handle connect_pol
, domain_pol
;
2259 NTSTATUS status
, result
;
2261 struct policy_handle group_pol
;
2262 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2264 struct samr_Ids rids
, rid_types
;
2265 struct lsa_String lsa_acct_name
;
2269 sid_copy(&sid
, group_sid
);
2271 if (!sid_split_rid(&sid
, &group_rid
))
2272 return NT_STATUS_UNSUCCESSFUL
;
2274 /* Get sam policy handle */
2275 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2277 MAXIMUM_ALLOWED_ACCESS
,
2280 if (!NT_STATUS_IS_OK(status
)) {
2283 if (!NT_STATUS_IS_OK(result
)) {
2288 /* Get domain policy handle */
2289 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2291 MAXIMUM_ALLOWED_ACCESS
,
2295 if (!NT_STATUS_IS_OK(status
)) {
2298 if (!NT_STATUS_IS_OK(result
)) {
2302 init_lsa_String(&lsa_acct_name
, member
);
2304 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2311 if (!NT_STATUS_IS_OK(status
)) {
2312 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2317 if (!NT_STATUS_IS_OK(result
)) {
2319 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2324 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2326 MAXIMUM_ALLOWED_ACCESS
,
2330 if (!NT_STATUS_IS_OK(status
)) {
2333 if (!NT_STATUS_IS_OK(result
)) {
2338 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
2342 if (!NT_STATUS_IS_OK(status
)) {
2348 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2352 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2353 TALLOC_CTX
*mem_ctx
,
2354 const struct dom_sid
*alias_sid
,
2357 struct policy_handle connect_pol
, domain_pol
;
2358 NTSTATUS status
, result
;
2360 struct policy_handle alias_pol
;
2361 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2363 struct dom_sid member_sid
;
2364 enum lsa_SidType member_type
;
2368 sid_copy(&sid
, alias_sid
);
2370 if (!sid_split_rid(&sid
, &alias_rid
))
2371 return NT_STATUS_UNSUCCESSFUL
;
2373 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2374 member
, &member_sid
, &member_type
);
2376 if (!NT_STATUS_IS_OK(result
)) {
2377 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2382 /* Get sam policy handle */
2383 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2385 MAXIMUM_ALLOWED_ACCESS
,
2388 if (!NT_STATUS_IS_OK(status
)) {
2391 if (!NT_STATUS_IS_OK(result
)) {
2396 /* Get domain policy handle */
2397 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2399 MAXIMUM_ALLOWED_ACCESS
,
2403 if (!NT_STATUS_IS_OK(status
)) {
2406 if (!NT_STATUS_IS_OK(result
)) {
2411 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2413 MAXIMUM_ALLOWED_ACCESS
,
2417 if (!NT_STATUS_IS_OK(status
)) {
2421 if (!NT_STATUS_IS_OK(result
)) {
2425 status
= dcerpc_samr_DeleteAliasMember(b
, mem_ctx
,
2430 if (!NT_STATUS_IS_OK(status
)) {
2437 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2441 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2442 const struct dom_sid
*domain_sid
,
2443 const char *domain_name
,
2444 struct cli_state
*cli
,
2445 struct rpc_pipe_client
*pipe_hnd
,
2446 TALLOC_CTX
*mem_ctx
,
2450 struct dom_sid group_sid
;
2451 enum lsa_SidType group_type
;
2453 if (argc
!= 2 || c
->display_usage
) {
2456 _("net rpc group delmem <group> <member>\n"
2457 " Delete a member from a group\n"
2458 " group\tGroup to delete member from\n"
2459 " member\tMember to delete from group\n"));
2460 return NT_STATUS_UNSUCCESSFUL
;
2463 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2464 &group_sid
, &group_type
))) {
2465 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2467 return NT_STATUS_UNSUCCESSFUL
;
2470 if (group_type
== SID_NAME_DOM_GRP
) {
2471 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2472 &group_sid
, argv
[1]);
2474 if (!NT_STATUS_IS_OK(result
)) {
2475 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2476 argv
[1], argv
[0], nt_errstr(result
));
2481 if (group_type
== SID_NAME_ALIAS
) {
2482 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2483 &group_sid
, argv
[1]);
2485 if (!NT_STATUS_IS_OK(result
)) {
2486 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2487 argv
[1], argv
[0], nt_errstr(result
));
2492 d_fprintf(stderr
, _("Can only delete members from global or local "
2493 "groups which %s is not\n"), argv
[0]);
2495 return NT_STATUS_UNSUCCESSFUL
;
2498 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2500 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2501 rpc_group_delmem_internals
,
2506 * List groups on a remote RPC server.
2508 * All parameters are provided by the run_rpc_command function, except for
2509 * argc, argv which are passes through.
2511 * @param domain_sid The domain sid acquired from the remote server.
2512 * @param cli A cli_state connected to the server.
2513 * @param mem_ctx Talloc context, destroyed on completion of the function.
2514 * @param argc Standard main() style argc.
2515 * @param argv Standard main() style argv. Initial components are already
2518 * @return Normal NTSTATUS return.
2521 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2522 const struct dom_sid
*domain_sid
,
2523 const char *domain_name
,
2524 struct cli_state
*cli
,
2525 struct rpc_pipe_client
*pipe_hnd
,
2526 TALLOC_CTX
*mem_ctx
,
2530 struct policy_handle connect_pol
, domain_pol
;
2531 NTSTATUS status
, result
;
2532 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2533 struct samr_SamArray
*groups
= NULL
;
2534 bool global
= false;
2536 bool builtin
= false;
2537 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2539 if (c
->display_usage
) {
2542 _("net rpc group list [global] [local] [builtin]\n"
2543 " List groups on RPC server\n"
2544 " global\tList global groups\n"
2545 " local\tList local groups\n"
2546 " builtin\tList builtin groups\n"
2547 " If none of global, local or builtin is "
2548 "specified, all three options are considered "
2550 return NT_STATUS_OK
;
2559 for (i
=0; i
<argc
; i
++) {
2560 if (strequal(argv
[i
], "global"))
2563 if (strequal(argv
[i
], "local"))
2566 if (strequal(argv
[i
], "builtin"))
2570 /* Get sam policy handle */
2572 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2574 MAXIMUM_ALLOWED_ACCESS
,
2577 if (!NT_STATUS_IS_OK(status
)) {
2580 if (!NT_STATUS_IS_OK(result
)) {
2585 /* Get domain policy handle */
2587 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2589 MAXIMUM_ALLOWED_ACCESS
,
2590 discard_const_p(struct dom_sid2
, domain_sid
),
2593 if (!NT_STATUS_IS_OK(status
)) {
2596 if (!NT_STATUS_IS_OK(result
)) {
2601 /* Query domain groups */
2602 if (c
->opt_long_list_entries
)
2603 d_printf(_("\nGroup name Comment"
2604 "\n-----------------------------\n"));
2606 uint32_t max_size
, total_size
, returned_size
;
2607 union samr_DispInfo info
;
2611 dcerpc_get_query_dispinfo_params(
2612 loop_count
, &max_entries
, &max_size
);
2614 status
= dcerpc_samr_QueryDisplayInfo(b
, mem_ctx
,
2624 if (!NT_STATUS_IS_OK(status
)) {
2627 num_entries
= info
.info3
.count
;
2628 start_idx
+= info
.info3
.count
;
2630 if (!NT_STATUS_IS_OK(result
) &&
2631 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2634 for (i
= 0; i
< num_entries
; i
++) {
2636 const char *group
= NULL
;
2637 const char *desc
= NULL
;
2639 group
= info
.info3
.entries
[i
].account_name
.string
;
2640 desc
= info
.info3
.entries
[i
].description
.string
;
2642 if (c
->opt_long_list_entries
)
2643 printf("%-21.21s %-50.50s\n",
2646 printf("%s\n", group
);
2648 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2649 /* query domain aliases */
2654 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2661 if (!NT_STATUS_IS_OK(status
)) {
2664 if (!NT_STATUS_IS_OK(result
) &&
2665 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2668 for (i
= 0; i
< num_entries
; i
++) {
2670 const char *description
= NULL
;
2672 if (c
->opt_long_list_entries
) {
2674 struct policy_handle alias_pol
;
2675 union samr_AliasInfo
*info
= NULL
;
2678 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2681 groups
->entries
[i
].idx
,
2684 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2685 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2690 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2691 status
= dcerpc_samr_Close(b
, mem_ctx
,
2694 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2695 description
= info
->description
.string
;
2701 if (description
!= NULL
) {
2702 printf("%-21.21s %-50.50s\n",
2703 groups
->entries
[i
].name
.string
,
2706 printf("%s\n", groups
->entries
[i
].name
.string
);
2709 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2710 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
2711 /* Get builtin policy handle */
2713 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2715 MAXIMUM_ALLOWED_ACCESS
,
2716 discard_const_p(struct dom_sid2
, &global_sid_Builtin
),
2719 if (!NT_STATUS_IS_OK(status
)) {
2722 if (!NT_STATUS_IS_OK(result
)) {
2727 /* query builtin aliases */
2730 if (!builtin
) break;
2732 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2739 if (!NT_STATUS_IS_OK(status
)) {
2742 if (!NT_STATUS_IS_OK(result
) &&
2743 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
2748 for (i
= 0; i
< num_entries
; i
++) {
2750 const char *description
= NULL
;
2752 if (c
->opt_long_list_entries
) {
2754 struct policy_handle alias_pol
;
2755 union samr_AliasInfo
*info
= NULL
;
2758 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2761 groups
->entries
[i
].idx
,
2764 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2765 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2770 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2771 status
= dcerpc_samr_Close(b
, mem_ctx
,
2774 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2775 description
= info
->description
.string
;
2781 if (description
!= NULL
) {
2782 printf("%-21.21s %-50.50s\n",
2783 groups
->entries
[i
].name
.string
,
2786 printf("%s\n", groups
->entries
[i
].name
.string
);
2789 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2797 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2799 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2800 rpc_group_list_internals
,
2804 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2805 struct rpc_pipe_client
*pipe_hnd
,
2806 TALLOC_CTX
*mem_ctx
,
2807 const char *domain_name
,
2808 const struct dom_sid
*domain_sid
,
2809 struct policy_handle
*domain_pol
,
2812 NTSTATUS result
, status
;
2813 struct policy_handle group_pol
;
2814 uint32 num_members
, *group_rids
;
2816 struct samr_RidAttrArray
*rids
= NULL
;
2817 struct lsa_Strings names
;
2818 struct samr_Ids types
;
2819 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2822 sid_to_fstring(sid_str
, domain_sid
);
2824 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2826 MAXIMUM_ALLOWED_ACCESS
,
2830 if (!NT_STATUS_IS_OK(status
)) {
2833 if (!NT_STATUS_IS_OK(result
)) {
2837 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
2841 if (!NT_STATUS_IS_OK(status
)) {
2844 if (!NT_STATUS_IS_OK(result
)) {
2848 num_members
= rids
->count
;
2849 group_rids
= rids
->rids
;
2851 while (num_members
> 0) {
2852 int this_time
= 512;
2854 if (num_members
< this_time
)
2855 this_time
= num_members
;
2857 status
= dcerpc_samr_LookupRids(b
, mem_ctx
,
2864 if (!NT_STATUS_IS_OK(status
)) {
2867 if (!NT_STATUS_IS_OK(result
)) {
2871 /* We only have users as members, but make the output
2872 the same as the output of alias members */
2874 for (i
= 0; i
< this_time
; i
++) {
2876 if (c
->opt_long_list_entries
) {
2877 printf("%s-%d %s\\%s %d\n", sid_str
,
2878 group_rids
[i
], domain_name
,
2879 names
.names
[i
].string
,
2882 printf("%s\\%s\n", domain_name
,
2883 names
.names
[i
].string
);
2887 num_members
-= this_time
;
2891 return NT_STATUS_OK
;
2894 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2895 struct rpc_pipe_client
*pipe_hnd
,
2896 TALLOC_CTX
*mem_ctx
,
2897 struct policy_handle
*domain_pol
,
2900 NTSTATUS result
, status
;
2901 struct rpc_pipe_client
*lsa_pipe
;
2902 struct policy_handle alias_pol
, lsa_pol
;
2904 struct dom_sid
*alias_sids
;
2907 enum lsa_SidType
*types
;
2909 struct lsa_SidArray sid_array
;
2910 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2912 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2914 MAXIMUM_ALLOWED_ACCESS
,
2918 if (!NT_STATUS_IS_OK(status
)) {
2921 if (!NT_STATUS_IS_OK(result
)) {
2925 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
2929 if (!NT_STATUS_IS_OK(status
)) {
2930 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2933 if (!NT_STATUS_IS_OK(result
)) {
2934 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2938 num_members
= sid_array
.num_sids
;
2940 if (num_members
== 0) {
2941 return NT_STATUS_OK
;
2944 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2945 &ndr_table_lsarpc
.syntax_id
,
2947 if (!NT_STATUS_IS_OK(result
)) {
2948 d_fprintf(stderr
, _("Couldn't open LSA pipe. Error was %s\n"),
2949 nt_errstr(result
) );
2953 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2954 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
2956 if (!NT_STATUS_IS_OK(result
)) {
2957 d_fprintf(stderr
, _("Couldn't open LSA policy handle\n"));
2958 TALLOC_FREE(lsa_pipe
);
2962 alias_sids
= talloc_zero_array(mem_ctx
, struct dom_sid
, num_members
);
2964 d_fprintf(stderr
, _("Out of memory\n"));
2965 TALLOC_FREE(lsa_pipe
);
2966 return NT_STATUS_NO_MEMORY
;
2969 for (i
=0; i
<num_members
; i
++) {
2970 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2973 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2974 num_members
, alias_sids
,
2975 &domains
, &names
, &types
);
2977 if (!NT_STATUS_IS_OK(result
) &&
2978 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2979 d_fprintf(stderr
, _("Couldn't lookup SIDs\n"));
2980 TALLOC_FREE(lsa_pipe
);
2984 for (i
= 0; i
< num_members
; i
++) {
2986 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2988 if (c
->opt_long_list_entries
) {
2989 printf("%s %s\\%s %d\n", sid_str
,
2990 domains
[i
] ? domains
[i
] : _("*unknown*"),
2991 names
[i
] ? names
[i
] : _("*unknown*"), types
[i
]);
2994 printf("%s\\%s\n", domains
[i
], names
[i
]);
2996 printf("%s\n", sid_str
);
3000 TALLOC_FREE(lsa_pipe
);
3001 return NT_STATUS_OK
;
3004 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
3005 const struct dom_sid
*domain_sid
,
3006 const char *domain_name
,
3007 struct cli_state
*cli
,
3008 struct rpc_pipe_client
*pipe_hnd
,
3009 TALLOC_CTX
*mem_ctx
,
3013 NTSTATUS result
, status
;
3014 struct policy_handle connect_pol
, domain_pol
;
3015 struct samr_Ids rids
, rid_types
;
3016 struct lsa_String lsa_acct_name
;
3017 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3019 /* Get sam policy handle */
3021 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
3023 MAXIMUM_ALLOWED_ACCESS
,
3026 if (!NT_STATUS_IS_OK(status
)) {
3029 if (!NT_STATUS_IS_OK(result
)) {
3033 /* Get domain policy handle */
3035 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3037 MAXIMUM_ALLOWED_ACCESS
,
3038 discard_const_p(struct dom_sid2
, domain_sid
),
3041 if (!NT_STATUS_IS_OK(status
)) {
3044 if (!NT_STATUS_IS_OK(result
)) {
3048 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
3050 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3057 if (!NT_STATUS_IS_OK(status
)) {
3061 if (!NT_STATUS_IS_OK(result
)) {
3063 /* Ok, did not find it in the global sam, try with builtin */
3065 struct dom_sid sid_Builtin
;
3067 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
3069 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
3071 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3073 MAXIMUM_ALLOWED_ACCESS
,
3077 if (!NT_STATUS_IS_OK(status
)) {
3080 if (!NT_STATUS_IS_OK(result
)) {
3081 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3086 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3093 if (!NT_STATUS_IS_OK(status
)) {
3096 if (!NT_STATUS_IS_OK(result
)) {
3097 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3103 if (rids
.count
!= 1) {
3104 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3109 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
3110 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
3111 domain_sid
, &domain_pol
,
3115 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
3116 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
3120 return NT_STATUS_NO_SUCH_GROUP
;
3123 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
3125 if (argc
!= 1 || c
->display_usage
) {
3126 return rpc_group_usage(c
, argc
, argv
);
3129 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
3130 rpc_group_members_internals
,
3134 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
3136 NET_API_STATUS status
;
3137 struct GROUP_INFO_0 g0
;
3141 d_printf(_("Usage:\n"));
3142 d_printf("net rpc group rename group newname\n");
3146 g0
.grpi0_name
= argv
[1];
3148 status
= NetGroupSetInfo(c
->opt_host
,
3155 d_fprintf(stderr
, _("Renaming group %s failed with: %s\n"),
3156 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
3164 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
3166 if (argc
!= 2 || c
->display_usage
) {
3167 return rpc_group_usage(c
, argc
, argv
);
3170 return rpc_group_rename_internals(c
, argc
, argv
);
3174 * 'net rpc group' entrypoint.
3175 * @param argc Standard main() style argc.
3176 * @param argv Standard main() style argv. Initial components are already
3180 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
3182 NET_API_STATUS status
;
3184 struct functable func
[] = {
3189 N_("Create specified group"),
3190 N_("net rpc group add\n"
3191 " Create specified group")
3197 N_("Delete specified group"),
3198 N_("net rpc group delete\n"
3199 " Delete specified group")
3205 N_("Add member to group"),
3206 N_("net rpc group addmem\n"
3207 " Add member to group")
3213 N_("Remove member from group"),
3214 N_("net rpc group delmem\n"
3215 " Remove member from group")
3222 N_("net rpc group list\n"
3229 N_("List group members"),
3230 N_("net rpc group members\n"
3231 " List group members")
3238 N_("net rpc group rename\n"
3241 {NULL
, NULL
, 0, NULL
, NULL
}
3244 status
= libnetapi_net_init(&c
->netapi_ctx
);
3248 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
3249 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
3250 if (c
->opt_kerberos
) {
3251 libnetapi_set_use_kerberos(c
->netapi_ctx
);
3255 if (c
->display_usage
) {
3256 d_printf(_("Usage:\n"));
3257 d_printf(_("net rpc group\n"
3258 " Alias for net rpc group list global "
3259 "local builtin\n"));
3260 net_display_usage_from_functable(func
);
3264 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
3265 rpc_group_list_internals
,
3269 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
3272 /****************************************************************************/
3274 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
3276 return net_share_usage(c
, argc
, argv
);
3280 * Add a share on a remote RPC server.
3282 * @param argc Standard main() style argc.
3283 * @param argv Standard main() style argv. Initial components are already
3286 * @return A shell status integer (0 for success).
3289 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
3291 NET_API_STATUS status
;
3294 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3295 uint32 num_users
=0, perms
=0;
3296 char *password
=NULL
; /* don't allow a share password */
3297 struct SHARE_INFO_2 i2
;
3298 uint32_t parm_error
= 0;
3300 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
3301 return rpc_share_usage(c
, argc
, argv
);
3304 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
3308 path
= strchr(sharename
, '=');
3315 i2
.shi2_netname
= sharename
;
3316 i2
.shi2_type
= type
;
3317 i2
.shi2_remark
= c
->opt_comment
;
3318 i2
.shi2_permissions
= perms
;
3319 i2
.shi2_max_uses
= c
->opt_maxusers
;
3320 i2
.shi2_current_uses
= num_users
;
3321 i2
.shi2_path
= path
;
3322 i2
.shi2_passwd
= password
;
3324 status
= NetShareAdd(c
->opt_host
,
3329 printf(_("NetShareAdd failed with: %s\n"),
3330 libnetapi_get_error_string(c
->netapi_ctx
, status
));
3337 * Delete a share on a remote RPC server.
3339 * @param domain_sid The domain sid acquired from the remote server.
3340 * @param argc Standard main() style argc.
3341 * @param argv Standard main() style argv. Initial components are already
3344 * @return A shell status integer (0 for success).
3346 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3348 if (argc
< 1 || c
->display_usage
) {
3349 return rpc_share_usage(c
, argc
, argv
);
3352 return NetShareDel(c
->opt_host
, argv
[0], 0);
3356 * Formatted print of share info
3358 * @param r pointer to SHARE_INFO_1 to format
3361 static void display_share_info_1(struct net_context
*c
,
3362 struct SHARE_INFO_1
*r
)
3364 if (c
->opt_long_list_entries
) {
3365 d_printf("%-12s %-8.8s %-50s\n",
3367 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
3370 d_printf("%s\n", r
->shi1_netname
);
3374 static WERROR
get_share_info(struct net_context
*c
,
3375 struct rpc_pipe_client
*pipe_hnd
,
3376 TALLOC_CTX
*mem_ctx
,
3380 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3384 union srvsvc_NetShareInfo info
;
3385 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3387 /* no specific share requested, enumerate all */
3390 uint32_t preferred_len
= 0xffffffff;
3391 uint32_t total_entries
= 0;
3392 uint32_t resume_handle
= 0;
3394 info_ctr
->level
= level
;
3396 status
= dcerpc_srvsvc_NetShareEnumAll(b
, mem_ctx
,
3403 if (!NT_STATUS_IS_OK(status
)) {
3404 return ntstatus_to_werror(status
);
3409 /* request just one share */
3410 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
3417 if (!NT_STATUS_IS_OK(status
)) {
3418 result
= ntstatus_to_werror(status
);
3422 if (!W_ERROR_IS_OK(result
)) {
3427 ZERO_STRUCTP(info_ctr
);
3429 info_ctr
->level
= level
;
3434 struct srvsvc_NetShareCtr1
*ctr1
;
3436 ctr1
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr1
);
3437 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3440 ctr1
->array
= info
.info1
;
3442 info_ctr
->ctr
.ctr1
= ctr1
;
3448 struct srvsvc_NetShareCtr2
*ctr2
;
3450 ctr2
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr2
);
3451 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3454 ctr2
->array
= info
.info2
;
3456 info_ctr
->ctr
.ctr2
= ctr2
;
3462 struct srvsvc_NetShareCtr502
*ctr502
;
3464 ctr502
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr502
);
3465 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3468 ctr502
->array
= info
.info502
;
3470 info_ctr
->ctr
.ctr502
= ctr502
;
3480 * 'net rpc share list' entrypoint.
3481 * @param argc Standard main() style argc.
3482 * @param argv Standard main() style argv. Initial components are already
3485 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3487 NET_API_STATUS status
;
3488 struct SHARE_INFO_1
*i1
= NULL
;
3489 uint32_t entries_read
= 0;
3490 uint32_t total_entries
= 0;
3491 uint32_t resume_handle
= 0;
3492 uint32_t i
, level
= 1;
3494 if (c
->display_usage
) {
3496 "net rpc share list\n"
3499 _("List shares on remote server"));
3503 status
= NetShareEnum(c
->opt_host
,
3505 (uint8_t **)(void *)&i1
,
3514 /* Display results */
3516 if (c
->opt_long_list_entries
) {
3518 "\nEnumerating shared resources (exports) on remote server:\n\n"
3519 "\nShare name Type Description\n"
3520 "---------- ---- -----------\n"));
3522 for (i
= 0; i
< entries_read
; i
++)
3523 display_share_info_1(c
, &i1
[i
]);
3528 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3532 status
= cli_tree_connect(cli
, netname
, "A:", "", 0);
3533 if (!NT_STATUS_IS_OK(status
)) {
3534 d_printf(_("skipping [%s]: not a file share.\n"), netname
);
3538 status
= cli_tdis(cli
);
3539 if (!NT_STATUS_IS_OK(status
)) {
3540 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status
));
3547 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3548 const char *netname
, uint32 type
)
3550 /* only support disk shares */
3551 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3552 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname
,
3557 /* skip builtin shares */
3558 /* FIXME: should print$ be added too ? */
3559 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3560 strequal(netname
,"global"))
3563 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3564 printf(_("excluding [%s]\n"), netname
);
3568 return check_share_availability(cli
, netname
);
3572 * Migrate shares from a remote RPC server to the local RPC server.
3574 * All parameters are provided by the run_rpc_command function, except for
3575 * argc, argv which are passed through.
3577 * @param domain_sid The domain sid acquired from the remote server.
3578 * @param cli A cli_state connected to the server.
3579 * @param mem_ctx Talloc context, destroyed on completion of the function.
3580 * @param argc Standard main() style argc.
3581 * @param argv Standard main() style argv. Initial components are already
3584 * @return Normal NTSTATUS return.
3587 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3588 const struct dom_sid
*domain_sid
,
3589 const char *domain_name
,
3590 struct cli_state
*cli
,
3591 struct rpc_pipe_client
*pipe_hnd
,
3592 TALLOC_CTX
*mem_ctx
,
3597 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3598 struct srvsvc_NetShareInfoCtr ctr_src
;
3600 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3601 struct cli_state
*cli_dst
= NULL
;
3602 uint32 level
= 502; /* includes secdesc */
3603 uint32_t parm_error
= 0;
3604 struct dcerpc_binding_handle
*b
;
3606 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3608 if (!W_ERROR_IS_OK(result
))
3611 /* connect destination PI_SRVSVC */
3612 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3613 &ndr_table_srvsvc
.syntax_id
);
3614 if (!NT_STATUS_IS_OK(nt_status
))
3617 b
= srvsvc_pipe
->binding_handle
;
3619 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3621 union srvsvc_NetShareInfo info
;
3622 struct srvsvc_NetShareInfo502 info502
=
3623 ctr_src
.ctr
.ctr502
->array
[i
];
3625 /* reset error-code */
3626 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3628 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3631 /* finally add the share on the dst server */
3633 printf(_("migrating: [%s], path: %s, comment: %s, without "
3635 info502
.name
, info502
.path
, info502
.comment
);
3637 info
.info502
= &info502
;
3639 nt_status
= dcerpc_srvsvc_NetShareAdd(b
, mem_ctx
,
3640 srvsvc_pipe
->desthost
,
3645 if (!NT_STATUS_IS_OK(nt_status
)) {
3646 printf(_("cannot add share: %s\n"),
3647 nt_errstr(nt_status
));
3650 if (W_ERROR_V(result
) == W_ERROR_V(WERR_FILE_EXISTS
)) {
3651 printf(_(" [%s] does already exist\n"),
3656 if (!W_ERROR_IS_OK(result
)) {
3657 nt_status
= werror_to_ntstatus(result
);
3658 printf(_("cannot add share: %s\n"),
3659 win_errstr(result
));
3665 nt_status
= NT_STATUS_OK
;
3669 cli_shutdown(cli_dst
);
3677 * Migrate shares from a RPC server to another.
3679 * @param argc Standard main() style argc.
3680 * @param argv Standard main() style argv. Initial components are already
3683 * @return A shell status integer (0 for success).
3685 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3688 if (c
->display_usage
) {
3690 "net rpc share migrate shares\n"
3693 _("Migrate shares to local server"));
3698 printf(_("no server to migrate\n"));
3702 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3703 rpc_share_migrate_shares_internals
,
3710 * @param f file_info
3711 * @param mask current search mask
3712 * @param state arg-pointer
3715 static NTSTATUS
copy_fn(const char *mnt
, struct file_info
*f
,
3716 const char *mask
, void *state
)
3718 static NTSTATUS nt_status
;
3719 static struct copy_clistate
*local_state
;
3720 static fstring filename
, new_mask
;
3723 struct net_context
*c
;
3725 local_state
= (struct copy_clistate
*)state
;
3726 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3730 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3731 return NT_STATUS_OK
;
3733 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3736 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
3738 DEBUG(3,("got dir: %s\n", f
->name
));
3740 fstrcpy(dir
, local_state
->cwd
);
3742 fstrcat(dir
, f
->name
);
3744 switch (net_mode_share
)
3746 case NET_MODE_SHARE_MIGRATE
:
3747 /* create that directory */
3748 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3749 local_state
->cli_share_src
,
3750 local_state
->cli_share_dst
,
3752 c
->opt_acls
? true : false,
3753 c
->opt_attrs
? true : false,
3754 c
->opt_timestamps
? true:false,
3758 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3759 return NT_STATUS_INTERNAL_ERROR
;
3762 if (!NT_STATUS_IS_OK(nt_status
)) {
3763 printf(_("could not handle dir %s: %s\n"),
3764 dir
, nt_errstr(nt_status
));
3768 /* search below that directory */
3769 strlcpy(new_mask
, dir
, sizeof(new_mask
));
3770 strlcat(new_mask
, "\\*", sizeof(new_mask
));
3772 old_dir
= local_state
->cwd
;
3773 local_state
->cwd
= dir
;
3774 nt_status
= sync_files(local_state
, new_mask
);
3775 if (!NT_STATUS_IS_OK(nt_status
)) {
3776 printf(_("could not handle files\n"));
3778 local_state
->cwd
= old_dir
;
3785 fstrcpy(filename
, local_state
->cwd
);
3786 fstrcat(filename
, "\\");
3787 fstrcat(filename
, f
->name
);
3789 DEBUG(3,("got file: %s\n", filename
));
3791 switch (net_mode_share
)
3793 case NET_MODE_SHARE_MIGRATE
:
3794 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3795 local_state
->cli_share_src
,
3796 local_state
->cli_share_dst
,
3798 c
->opt_acls
? true : false,
3799 c
->opt_attrs
? true : false,
3800 c
->opt_timestamps
? true: false,
3804 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3806 return NT_STATUS_INTERNAL_ERROR
;
3809 if (!NT_STATUS_IS_OK(nt_status
))
3810 printf(_("could not handle file %s: %s\n"),
3811 filename
, nt_errstr(nt_status
));
3816 * sync files, can be called recursivly to list files
3817 * and then call copy_fn for each file
3819 * @param cp_clistate pointer to the copy_clistate we work with
3820 * @param mask the current search mask
3822 * @return Boolean result
3824 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3826 struct cli_state
*targetcli
;
3827 char *targetpath
= NULL
;
3830 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3832 status
= cli_resolve_path(talloc_tos(), "", NULL
,
3833 cp_clistate
->cli_share_src
,
3834 mask
, &targetcli
, &targetpath
);
3835 if (!NT_STATUS_IS_OK(status
)) {
3836 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3838 mask
, nt_errstr(status
));
3842 status
= cli_list(targetcli
, targetpath
, cp_clistate
->attribute
,
3843 copy_fn
, cp_clistate
);
3844 if (!NT_STATUS_IS_OK(status
)) {
3845 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3846 mask
, nt_errstr(status
));
3854 * Set the top level directory permissions before we do any further copies.
3855 * Should set up ACL inheritance.
3858 bool copy_top_level_perms(struct net_context
*c
,
3859 struct copy_clistate
*cp_clistate
,
3860 const char *sharename
)
3862 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3864 switch (net_mode_share
) {
3865 case NET_MODE_SHARE_MIGRATE
:
3866 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3867 nt_status
= net_copy_fileattr(c
,
3868 cp_clistate
->mem_ctx
,
3869 cp_clistate
->cli_share_src
,
3870 cp_clistate
->cli_share_dst
,
3872 c
->opt_acls
? true : false,
3873 c
->opt_attrs
? true : false,
3874 c
->opt_timestamps
? true: false,
3878 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3882 if (!NT_STATUS_IS_OK(nt_status
)) {
3883 printf(_("Could handle directory attributes for top level "
3884 "directory of share %s. Error %s\n"),
3885 sharename
, nt_errstr(nt_status
));
3893 * Sync all files inside a remote share to another share (over smb).
3895 * All parameters are provided by the run_rpc_command function, except for
3896 * argc, argv which are passed through.
3898 * @param domain_sid The domain sid acquired from the remote server.
3899 * @param cli A cli_state connected to the server.
3900 * @param mem_ctx Talloc context, destroyed on completion of the function.
3901 * @param argc Standard main() style argc.
3902 * @param argv Standard main() style argv. Initial components are already
3905 * @return Normal NTSTATUS return.
3908 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3909 const struct dom_sid
*domain_sid
,
3910 const char *domain_name
,
3911 struct cli_state
*cli
,
3912 struct rpc_pipe_client
*pipe_hnd
,
3913 TALLOC_CTX
*mem_ctx
,
3918 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3919 struct srvsvc_NetShareInfoCtr ctr_src
;
3922 struct copy_clistate cp_clistate
;
3923 bool got_src_share
= false;
3924 bool got_dst_share
= false;
3925 const char *mask
= "\\*";
3928 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3930 nt_status
= NT_STATUS_NO_MEMORY
;
3934 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3937 if (!W_ERROR_IS_OK(result
))
3940 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3942 struct srvsvc_NetShareInfo502 info502
=
3943 ctr_src
.ctr
.ctr502
->array
[i
];
3945 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3948 /* one might not want to mirror whole discs :) */
3949 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3950 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3955 switch (net_mode_share
)
3957 case NET_MODE_SHARE_MIGRATE
:
3961 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3965 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3968 c
->opt_acls
? _("including") : _("without"),
3969 c
->opt_attrs
? _("including") : _("without"),
3970 c
->opt_timestamps
? _("(preserving timestamps)") : "");
3972 cp_clistate
.mem_ctx
= mem_ctx
;
3973 cp_clistate
.cli_share_src
= NULL
;
3974 cp_clistate
.cli_share_dst
= NULL
;
3975 cp_clistate
.cwd
= NULL
;
3976 cp_clistate
.attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
;
3979 /* open share source */
3980 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3981 cli_state_remote_sockaddr(cli
),
3982 cli_state_remote_name(cli
),
3983 info502
.name
, "A:");
3984 if (!NT_STATUS_IS_OK(nt_status
))
3987 got_src_share
= true;
3989 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3990 /* open share destination */
3991 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3992 NULL
, dst
, info502
.name
, "A:");
3993 if (!NT_STATUS_IS_OK(nt_status
))
3996 got_dst_share
= true;
3999 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
4000 d_fprintf(stderr
, _("Could not handle the top level "
4001 "directory permissions for the "
4002 "share: %s\n"), info502
.name
);
4003 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4007 nt_status
= sync_files(&cp_clistate
, mask
);
4008 if (!NT_STATUS_IS_OK(nt_status
)) {
4009 d_fprintf(stderr
, _("could not handle files for share: "
4010 "%s\n"), info502
.name
);
4015 nt_status
= NT_STATUS_OK
;
4020 cli_shutdown(cp_clistate
.cli_share_src
);
4023 cli_shutdown(cp_clistate
.cli_share_dst
);
4030 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
4032 if (c
->display_usage
) {
4034 "net share migrate files\n"
4037 _("Migrate files to local server"));
4042 d_printf(_("no server to migrate\n"));
4046 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4047 rpc_share_migrate_files_internals
,
4052 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4054 * All parameters are provided by the run_rpc_command function, except for
4055 * argc, argv which are passed through.
4057 * @param domain_sid The domain sid acquired from the remote server.
4058 * @param cli A cli_state connected to the server.
4059 * @param mem_ctx Talloc context, destroyed on completion of the function.
4060 * @param argc Standard main() style argc.
4061 * @param argv Standard main() style argv. Initial components are already
4064 * @return Normal NTSTATUS return.
4067 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
4068 const struct dom_sid
*domain_sid
,
4069 const char *domain_name
,
4070 struct cli_state
*cli
,
4071 struct rpc_pipe_client
*pipe_hnd
,
4072 TALLOC_CTX
*mem_ctx
,
4077 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
4078 struct srvsvc_NetShareInfoCtr ctr_src
;
4079 union srvsvc_NetShareInfo info
;
4081 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
4082 struct cli_state
*cli_dst
= NULL
;
4083 uint32 level
= 502; /* includes secdesc */
4084 uint32_t parm_error
= 0;
4085 struct dcerpc_binding_handle
*b
;
4087 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
4090 if (!W_ERROR_IS_OK(result
))
4093 /* connect destination PI_SRVSVC */
4094 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
4095 &ndr_table_srvsvc
.syntax_id
);
4096 if (!NT_STATUS_IS_OK(nt_status
))
4099 b
= srvsvc_pipe
->binding_handle
;
4101 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
4103 struct srvsvc_NetShareInfo502 info502
=
4104 ctr_src
.ctr
.ctr502
->array
[i
];
4106 /* reset error-code */
4107 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4109 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
4112 printf(_("migrating: [%s], path: %s, comment: %s, including "
4114 info502
.name
, info502
.path
, info502
.comment
);
4117 display_sec_desc(info502
.sd_buf
.sd
);
4119 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4120 info
.info502
= &info502
;
4122 /* finally modify the share on the dst server */
4123 nt_status
= dcerpc_srvsvc_NetShareSetInfo(b
, mem_ctx
,
4124 srvsvc_pipe
->desthost
,
4130 if (!NT_STATUS_IS_OK(nt_status
)) {
4131 printf(_("cannot set share-acl: %s\n"),
4132 nt_errstr(nt_status
));
4135 if (!W_ERROR_IS_OK(result
)) {
4136 nt_status
= werror_to_ntstatus(result
);
4137 printf(_("cannot set share-acl: %s\n"),
4138 win_errstr(result
));
4144 nt_status
= NT_STATUS_OK
;
4148 cli_shutdown(cli_dst
);
4156 * Migrate share-acls from a RPC server to another.
4158 * @param argc Standard main() style argc.
4159 * @param argv Standard main() style argv. Initial components are already
4162 * @return A shell status integer (0 for success).
4164 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
4167 if (c
->display_usage
) {
4169 "net rpc share migrate security\n"
4172 _("Migrate share-acls to local server"));
4177 d_printf(_("no server to migrate\n"));
4181 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4182 rpc_share_migrate_security_internals
,
4187 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4188 * from one server to another.
4190 * @param argc Standard main() style argc.
4191 * @param argv Standard main() style argv. Initial components are already
4194 * @return A shell status integer (0 for success).
4197 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
4202 if (c
->display_usage
) {
4204 "net rpc share migrate all\n"
4207 _("Migrates shares including all share settings"));
4212 d_printf(_("no server to migrate\n"));
4216 /* order is important. we don't want to be locked out by the share-acl
4217 * before copying files - gd */
4219 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4220 rpc_share_migrate_shares_internals
, argc
, argv
);
4224 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4225 rpc_share_migrate_files_internals
, argc
, argv
);
4229 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4230 rpc_share_migrate_security_internals
, argc
,
4236 * 'net rpc share migrate' entrypoint.
4237 * @param argc Standard main() style argc.
4238 * @param argv Standard main() style argv. Initial components are already
4241 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4244 struct functable func
[] = {
4247 rpc_share_migrate_all
,
4249 N_("Migrate shares from remote to local server"),
4250 N_("net rpc share migrate all\n"
4251 " Migrate shares from remote to local server")
4255 rpc_share_migrate_files
,
4257 N_("Migrate files from remote to local server"),
4258 N_("net rpc share migrate files\n"
4259 " Migrate files from remote to local server")
4263 rpc_share_migrate_security
,
4265 N_("Migrate share-ACLs from remote to local server"),
4266 N_("net rpc share migrate security\n"
4267 " Migrate share-ACLs from remote to local server")
4271 rpc_share_migrate_shares
,
4273 N_("Migrate shares from remote to local server"),
4274 N_("net rpc share migrate shares\n"
4275 " Migrate shares from remote to local server")
4277 {NULL
, NULL
, 0, NULL
, NULL
}
4280 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4282 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
4288 struct dom_sid
*members
;
4291 static int num_server_aliases
;
4292 static struct full_alias
*server_aliases
;
4295 * Add an alias to the static list.
4297 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4299 if (server_aliases
== NULL
)
4300 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4302 server_aliases
[num_server_aliases
] = *alias
;
4303 num_server_aliases
+= 1;
4307 * For a specific domain on the server, fetch all the aliases
4308 * and their members. Add all of them to the server_aliases.
4311 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4312 TALLOC_CTX
*mem_ctx
,
4313 struct policy_handle
*connect_pol
,
4314 const struct dom_sid
*domain_sid
)
4316 uint32 start_idx
, max_entries
, num_entries
, i
;
4317 struct samr_SamArray
*groups
= NULL
;
4318 NTSTATUS result
, status
;
4319 struct policy_handle domain_pol
;
4320 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4322 /* Get domain policy handle */
4324 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
4326 MAXIMUM_ALLOWED_ACCESS
,
4327 discard_const_p(struct dom_sid2
, domain_sid
),
4330 if (!NT_STATUS_IS_OK(status
)) {
4333 if (!NT_STATUS_IS_OK(result
)) {
4341 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
4348 if (!NT_STATUS_IS_OK(status
)) {
4351 for (i
= 0; i
< num_entries
; i
++) {
4353 struct policy_handle alias_pol
;
4354 struct full_alias alias
;
4355 struct lsa_SidArray sid_array
;
4359 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
4361 MAXIMUM_ALLOWED_ACCESS
,
4362 groups
->entries
[i
].idx
,
4365 if (!NT_STATUS_IS_OK(status
)) {
4368 if (!NT_STATUS_IS_OK(_result
)) {
4373 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
4377 if (!NT_STATUS_IS_OK(status
)) {
4380 if (!NT_STATUS_IS_OK(_result
)) {
4385 alias
.num_members
= sid_array
.num_sids
;
4387 status
= dcerpc_samr_Close(b
, mem_ctx
, &alias_pol
, &_result
);
4388 if (!NT_STATUS_IS_OK(status
)) {
4391 if (!NT_STATUS_IS_OK(_result
)) {
4396 alias
.members
= NULL
;
4398 if (alias
.num_members
> 0) {
4399 alias
.members
= SMB_MALLOC_ARRAY(struct dom_sid
, alias
.num_members
);
4401 for (j
= 0; j
< alias
.num_members
; j
++)
4402 sid_copy(&alias
.members
[j
],
4403 sid_array
.sids
[j
].sid
);
4406 sid_compose(&alias
.sid
, domain_sid
,
4407 groups
->entries
[i
].idx
);
4409 push_alias(mem_ctx
, &alias
);
4411 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4413 status
= NT_STATUS_OK
;
4416 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
4422 * Dump server_aliases as names for debugging purposes.
4425 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4426 const struct dom_sid
*domain_sid
,
4427 const char *domain_name
,
4428 struct cli_state
*cli
,
4429 struct rpc_pipe_client
*pipe_hnd
,
4430 TALLOC_CTX
*mem_ctx
,
4436 struct policy_handle lsa_pol
;
4437 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4439 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4440 SEC_FLAG_MAXIMUM_ALLOWED
,
4442 if (!NT_STATUS_IS_OK(result
))
4445 for (i
=0; i
<num_server_aliases
; i
++) {
4448 enum lsa_SidType
*types
;
4451 struct full_alias
*alias
= &server_aliases
[i
];
4453 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4455 &domains
, &names
, &types
);
4456 if (!NT_STATUS_IS_OK(result
))
4459 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4461 if (alias
->num_members
== 0) {
4466 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4469 &domains
, &names
, &types
);
4471 if (!NT_STATUS_IS_OK(result
) &&
4472 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4475 for (j
=0; j
<alias
->num_members
; j
++)
4476 DEBUG(1, ("%s\\%s (%d); ",
4477 domains
[j
] ? domains
[j
] : "*unknown*",
4478 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4482 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
4484 return NT_STATUS_OK
;
4488 * Fetch a list of all server aliases and their members into
4492 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4493 const struct dom_sid
*domain_sid
,
4494 const char *domain_name
,
4495 struct cli_state
*cli
,
4496 struct rpc_pipe_client
*pipe_hnd
,
4497 TALLOC_CTX
*mem_ctx
,
4501 NTSTATUS result
, status
;
4502 struct policy_handle connect_pol
;
4503 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4505 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
4507 MAXIMUM_ALLOWED_ACCESS
,
4510 if (!NT_STATUS_IS_OK(status
)) {
4513 if (!NT_STATUS_IS_OK(result
)) {
4518 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4519 &global_sid_Builtin
);
4520 if (!NT_STATUS_IS_OK(status
)) {
4524 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4527 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
4532 static void init_user_token(struct security_token
*token
, struct dom_sid
*user_sid
)
4534 token
->num_sids
= 4;
4536 if (!(token
->sids
= SMB_MALLOC_ARRAY(struct dom_sid
, 4))) {
4537 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4538 token
->num_sids
= 0;
4542 token
->sids
[0] = *user_sid
;
4543 sid_copy(&token
->sids
[1], &global_sid_World
);
4544 sid_copy(&token
->sids
[2], &global_sid_Network
);
4545 sid_copy(&token
->sids
[3], &global_sid_Authenticated_Users
);
4548 static void free_user_token(struct security_token
*token
)
4550 SAFE_FREE(token
->sids
);
4553 static void add_sid_to_token(struct security_token
*token
, struct dom_sid
*sid
)
4555 if (security_token_has_sid(token
, sid
))
4558 token
->sids
= SMB_REALLOC_ARRAY(token
->sids
, struct dom_sid
, token
->num_sids
+1);
4563 sid_copy(&token
->sids
[token
->num_sids
], sid
);
4565 token
->num_sids
+= 1;
4570 struct security_token token
;
4573 static void dump_user_token(struct user_token
*token
)
4577 d_printf("%s\n", token
->name
);
4579 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4580 d_printf(" %s\n", sid_string_tos(&token
->token
.sids
[i
]));
4584 static bool is_alias_member(struct dom_sid
*sid
, struct full_alias
*alias
)
4588 for (i
=0; i
<alias
->num_members
; i
++) {
4589 if (dom_sid_compare(sid
, &alias
->members
[i
]) == 0)
4596 static void collect_sid_memberships(struct security_token
*token
, struct dom_sid sid
)
4600 for (i
=0; i
<num_server_aliases
; i
++) {
4601 if (is_alias_member(&sid
, &server_aliases
[i
]))
4602 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4607 * We got a user token with all the SIDs we can know about without asking the
4608 * server directly. These are the user and domain group sids. All of these can
4609 * be members of aliases. So scan the list of aliases for each of the SIDs and
4610 * add them to the token.
4613 static void collect_alias_memberships(struct security_token
*token
)
4615 int num_global_sids
= token
->num_sids
;
4618 for (i
=0; i
<num_global_sids
; i
++) {
4619 collect_sid_memberships(token
, token
->sids
[i
]);
4623 static bool get_user_sids(const char *domain
, const char *user
, struct security_token
*token
)
4625 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4626 enum wbcSidType type
;
4628 struct wbcDomainSid wsid
;
4629 char sid_str
[WBC_SID_STRING_BUFLEN
];
4630 struct dom_sid user_sid
;
4631 uint32_t num_groups
;
4632 gid_t
*groups
= NULL
;
4635 fstr_sprintf(full_name
, "%s%c%s",
4636 domain
, *lp_winbind_separator(), user
);
4638 /* First let's find out the user sid */
4640 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4642 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4643 DEBUG(1, ("winbind could not find %s: %s\n",
4644 full_name
, wbcErrorString(wbc_status
)));
4648 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4650 if (type
!= WBC_SID_NAME_USER
) {
4651 DEBUG(1, ("%s is not a user\n", full_name
));
4655 if (!string_to_sid(&user_sid
, sid_str
)) {
4656 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4660 init_user_token(token
, &user_sid
);
4662 /* And now the groups winbind knows about */
4664 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4665 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4666 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4667 full_name
, wbcErrorString(wbc_status
)));
4671 for (i
= 0; i
< num_groups
; i
++) {
4672 gid_t gid
= groups
[i
];
4675 wbc_status
= wbcGidToSid(gid
, &wsid
);
4676 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4677 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4678 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4679 wbcFreeMemory(groups
);
4683 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4685 DEBUG(3, (" %s\n", sid_str
));
4687 string_to_sid(&sid
, sid_str
);
4688 add_sid_to_token(token
, &sid
);
4690 wbcFreeMemory(groups
);
4696 * Get a list of all user tokens we want to look at
4699 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4700 struct user_token
**user_tokens
)
4702 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4703 uint32_t i
, num_users
;
4705 struct user_token
*result
;
4706 TALLOC_CTX
*frame
= NULL
;
4708 if (lp_winbind_use_default_domain() &&
4709 (c
->opt_target_workgroup
== NULL
)) {
4710 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4711 "please specify a workgroup\n"));
4715 /* Send request to winbind daemon */
4717 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4718 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4719 DEBUG(1, (_("winbind could not list users: %s\n"),
4720 wbcErrorString(wbc_status
)));
4724 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4726 if (result
== NULL
) {
4727 DEBUG(1, ("Could not malloc sid array\n"));
4728 wbcFreeMemory(users
);
4732 frame
= talloc_stackframe();
4733 for (i
=0; i
< num_users
; i
++) {
4734 fstring domain
, user
;
4737 fstrcpy(result
[i
].name
, users
[i
]);
4739 p
= strchr(users
[i
], *lp_winbind_separator());
4741 DEBUG(3, ("%s\n", users
[i
]));
4744 fstrcpy(domain
, c
->opt_target_workgroup
);
4745 fstrcpy(user
, users
[i
]);
4748 fstrcpy(domain
, users
[i
]);
4753 get_user_sids(domain
, user
, &(result
[i
].token
));
4756 wbcFreeMemory(users
);
4758 *num_tokens
= num_users
;
4759 *user_tokens
= result
;
4764 static bool get_user_tokens_from_file(FILE *f
,
4766 struct user_token
**tokens
)
4768 struct user_token
*token
= NULL
;
4773 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4777 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4778 line
[strlen(line
)-1] = '\0';
4781 if (line
[0] == ' ') {
4785 if(!string_to_sid(&sid
, &line
[1])) {
4786 DEBUG(1,("get_user_tokens_from_file: Could "
4787 "not convert sid %s \n",&line
[1]));
4791 if (token
== NULL
) {
4792 DEBUG(0, ("File does not begin with username"));
4796 add_sid_to_token(&token
->token
, &sid
);
4800 /* And a new user... */
4803 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4804 if (*tokens
== NULL
) {
4805 DEBUG(0, ("Could not realloc tokens\n"));
4809 token
= &((*tokens
)[*num_tokens
-1]);
4811 strlcpy(token
->name
, line
, sizeof(token
->name
));
4812 token
->token
.num_sids
= 0;
4813 token
->token
.sids
= NULL
;
4822 * Show the list of all users that have access to a share
4825 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4826 TALLOC_CTX
*mem_ctx
,
4827 const char *netname
,
4829 struct user_token
*tokens
)
4832 struct security_descriptor
*share_sd
= NULL
;
4833 struct security_descriptor
*root_sd
= NULL
;
4834 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4836 union srvsvc_NetShareInfo info
;
4840 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4842 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
4849 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4850 DEBUG(1, ("Coult not query secdesc for share %s\n",
4855 share_sd
= info
.info502
->sd_buf
.sd
;
4856 if (share_sd
== NULL
) {
4857 DEBUG(1, ("Got no secdesc for share %s\n",
4861 cnum
= cli_state_get_tid(cli
);
4863 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, netname
, "A:", "", 0))) {
4867 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4868 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4869 cli_query_secdesc(cli
, fnum
, mem_ctx
, &root_sd
);
4872 for (i
=0; i
<num_tokens
; i
++) {
4875 if (share_sd
!= NULL
) {
4876 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4879 if (!NT_STATUS_IS_OK(status
)) {
4880 DEBUG(1, ("Could not check share_sd for "
4887 if (root_sd
== NULL
) {
4888 d_printf(" %s\n", tokens
[i
].name
);
4892 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4894 if (!NT_STATUS_IS_OK(status
)) {
4895 DEBUG(1, ("Could not check root_sd for user %s\n",
4899 d_printf(" %s\n", tokens
[i
].name
);
4902 if (fnum
!= (uint16_t)-1)
4903 cli_close(cli
, fnum
);
4905 cli_state_set_tid(cli
, cnum
);
4915 static void collect_share(const char *name
, uint32 m
,
4916 const char *comment
, void *state
)
4918 struct share_list
*share_list
= (struct share_list
*)state
;
4920 if (m
!= STYPE_DISKTREE
)
4923 share_list
->num_shares
+= 1;
4924 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4925 if (!share_list
->shares
) {
4926 share_list
->num_shares
= 0;
4929 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4933 * List shares on a remote RPC server, including the security descriptors.
4935 * All parameters are provided by the run_rpc_command function, except for
4936 * argc, argv which are passed through.
4938 * @param domain_sid The domain sid acquired from the remote server.
4939 * @param cli A cli_state connected to the server.
4940 * @param mem_ctx Talloc context, destroyed on completion of the function.
4941 * @param argc Standard main() style argc.
4942 * @param argv Standard main() style argv. Initial components are already
4945 * @return Normal NTSTATUS return.
4948 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4949 const struct dom_sid
*domain_sid
,
4950 const char *domain_name
,
4951 struct cli_state
*cli
,
4952 struct rpc_pipe_client
*pipe_hnd
,
4953 TALLOC_CTX
*mem_ctx
,
4962 struct user_token
*tokens
= NULL
;
4965 struct share_list share_list
;
4970 f
= fopen(argv
[0], "r");
4974 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4975 return NT_STATUS_UNSUCCESSFUL
;
4978 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4984 DEBUG(0, ("Could not read users from file\n"));
4985 return NT_STATUS_UNSUCCESSFUL
;
4988 for (i
=0; i
<num_tokens
; i
++)
4989 collect_alias_memberships(&tokens
[i
].token
);
4991 share_list
.num_shares
= 0;
4992 share_list
.shares
= NULL
;
4994 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4997 DEBUG(0, ("Error returning browse list: %s\n",
5002 for (i
= 0; i
< share_list
.num_shares
; i
++) {
5003 char *netname
= share_list
.shares
[i
];
5005 if (netname
[strlen(netname
)-1] == '$')
5008 d_printf("%s\n", netname
);
5010 show_userlist(pipe_hnd
, mem_ctx
, netname
,
5011 num_tokens
, tokens
);
5014 for (i
=0; i
<num_tokens
; i
++) {
5015 free_user_token(&tokens
[i
].token
);
5018 SAFE_FREE(share_list
.shares
);
5020 return NT_STATUS_OK
;
5023 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
5028 if (c
->display_usage
) {
5030 "net rpc share allowedusers\n"
5033 _("List allowed users"));
5037 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5038 rpc_aliaslist_internals
,
5043 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
.syntax_id
, 0,
5049 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
5050 rpc_share_allowedusers_internals
,
5054 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
5057 struct user_token
*tokens
= NULL
;
5061 net_usersidlist_usage(c
, argc
, argv
);
5065 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
5066 DEBUG(0, ("Could not get the user/sid list\n"));
5070 for (i
=0; i
<num_tokens
; i
++) {
5071 dump_user_token(&tokens
[i
]);
5072 free_user_token(&tokens
[i
].token
);
5079 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
5081 d_printf(_("net usersidlist\n"
5082 "\tprints out a list of all users the running winbind knows\n"
5083 "\tabout, together with all their SIDs. This is used as\n"
5084 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5086 net_common_flags_usage(c
, argc
, argv
);
5091 * 'net rpc share' entrypoint.
5092 * @param argc Standard main() style argc.
5093 * @param argv Standard main() style argv. Initial components are already
5097 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
5099 NET_API_STATUS status
;
5101 struct functable func
[] = {
5107 N_("net rpc share add\n"
5115 N_("net rpc share delete\n"
5120 rpc_share_allowedusers
,
5122 N_("Modify allowed users"),
5123 N_("net rpc share allowedusers\n"
5124 " Modify allowed users")
5130 N_("Migrate share to local server"),
5131 N_("net rpc share migrate\n"
5132 " Migrate share to local server")
5139 N_("net rpc share list\n"
5142 {NULL
, NULL
, 0, NULL
, NULL
}
5145 status
= libnetapi_net_init(&c
->netapi_ctx
);
5149 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5150 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5151 if (c
->opt_kerberos
) {
5152 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5156 if (c
->display_usage
) {
5161 " Alias for net rpc share list\n"));
5162 net_display_usage_from_functable(func
);
5166 return rpc_share_list(c
, argc
, argv
);
5169 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5172 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5173 TALLOC_CTX
*mem_ctx
,
5174 struct rpc_sh_ctx
*ctx
,
5175 struct rpc_pipe_client
*pipe_hnd
,
5176 int argc
, const char **argv
)
5179 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
5182 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
5183 TALLOC_CTX
*mem_ctx
,
5184 struct rpc_sh_ctx
*ctx
,
5185 struct rpc_pipe_client
*pipe_hnd
,
5186 int argc
, const char **argv
)
5188 NET_API_STATUS status
;
5189 uint32_t parm_err
= 0;
5190 struct SHARE_INFO_2 i2
;
5192 if ((argc
< 2) || (argc
> 3)) {
5193 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
5195 return NT_STATUS_INVALID_PARAMETER
;
5198 i2
.shi2_netname
= argv
[0];
5199 i2
.shi2_type
= STYPE_DISKTREE
;
5200 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
5201 i2
.shi2_permissions
= 0;
5202 i2
.shi2_max_uses
= 0;
5203 i2
.shi2_current_uses
= 0;
5204 i2
.shi2_path
= argv
[1];
5205 i2
.shi2_passwd
= NULL
;
5207 status
= NetShareAdd(pipe_hnd
->desthost
,
5212 return werror_to_ntstatus(W_ERROR(status
));
5215 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5216 TALLOC_CTX
*mem_ctx
,
5217 struct rpc_sh_ctx
*ctx
,
5218 struct rpc_pipe_client
*pipe_hnd
,
5219 int argc
, const char **argv
)
5222 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5223 return NT_STATUS_INVALID_PARAMETER
;
5226 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
5229 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5230 TALLOC_CTX
*mem_ctx
,
5231 struct rpc_sh_ctx
*ctx
,
5232 struct rpc_pipe_client
*pipe_hnd
,
5233 int argc
, const char **argv
)
5235 union srvsvc_NetShareInfo info
;
5238 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5241 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5242 return NT_STATUS_INVALID_PARAMETER
;
5245 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
5251 if (!NT_STATUS_IS_OK(status
)) {
5252 result
= ntstatus_to_werror(status
);
5255 if (!W_ERROR_IS_OK(result
)) {
5259 d_printf(_("Name: %s\n"), info
.info2
->name
);
5260 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
5261 d_printf(_("Path: %s\n"), info
.info2
->path
);
5262 d_printf(_("Password: %s\n"), info
.info2
->password
);
5265 return werror_to_ntstatus(result
);
5268 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5269 struct rpc_sh_ctx
*ctx
)
5271 static struct rpc_sh_cmd cmds
[] = {
5273 { "list", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_list
,
5274 N_("List available shares") },
5276 { "add", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_add
,
5277 N_("Add a share") },
5279 { "delete", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_delete
,
5280 N_("Delete a share") },
5282 { "info", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_info
,
5283 N_("Get information about a share") },
5285 { NULL
, NULL
, 0, NULL
, NULL
}
5291 /****************************************************************************/
5293 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5295 return net_file_usage(c
, argc
, argv
);
5299 * Close a file on a remote RPC server.
5301 * @param argc Standard main() style argc.
5302 * @param argv Standard main() style argv. Initial components are already
5305 * @return A shell status integer (0 for success).
5307 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5309 if (argc
< 1 || c
->display_usage
) {
5310 return rpc_file_usage(c
, argc
, argv
);
5313 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
5317 * Formatted print of open file info
5319 * @param r struct FILE_INFO_3 contents
5322 static void display_file_info_3(struct FILE_INFO_3
*r
)
5324 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5325 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
5326 r
->fi3_num_locks
, r
->fi3_pathname
);
5330 * List files for a user on a remote RPC server.
5332 * @param argc Standard main() style argc.
5333 * @param argv Standard main() style argv. Initial components are already
5336 * @return A shell status integer (0 for success)..
5339 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5341 NET_API_STATUS status
;
5342 uint32 preferred_len
= 0xffffffff, i
;
5343 const char *username
=NULL
;
5344 uint32_t total_entries
= 0;
5345 uint32_t entries_read
= 0;
5346 uint32_t resume_handle
= 0;
5347 struct FILE_INFO_3
*i3
= NULL
;
5349 if (c
->display_usage
) {
5350 return rpc_file_usage(c
, argc
, argv
);
5353 /* if argc > 0, must be user command */
5355 username
= smb_xstrdup(argv
[0]);
5358 status
= NetFileEnum(c
->opt_host
,
5362 (uint8_t **)(void *)&i3
,
5372 /* Display results */
5375 "\nEnumerating open files on remote server:\n\n"
5376 "\nFileId Opened by Perms Locks Path"
5377 "\n------ --------- ----- ----- ---- \n"));
5378 for (i
= 0; i
< entries_read
; i
++) {
5379 display_file_info_3(&i3
[i
]);
5386 * 'net rpc file' entrypoint.
5387 * @param argc Standard main() style argc.
5388 * @param argv Standard main() style argv. Initial components are already
5392 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5394 NET_API_STATUS status
;
5396 struct functable func
[] = {
5401 N_("Close opened file"),
5402 N_("net rpc file close\n"
5403 " Close opened file")
5409 N_("List files opened by user"),
5410 N_("net rpc file user\n"
5411 " List files opened by user")
5418 N_("Display information about opened file"),
5419 N_("net rpc file info\n"
5420 " Display information about opened file")
5423 {NULL
, NULL
, 0, NULL
, NULL
}
5426 status
= libnetapi_net_init(&c
->netapi_ctx
);
5430 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5431 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5432 if (c
->opt_kerberos
) {
5433 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5437 if (c
->display_usage
) {
5438 d_printf(_("Usage:\n"));
5439 d_printf(_("net rpc file\n"
5440 " List opened files\n"));
5441 net_display_usage_from_functable(func
);
5445 return rpc_file_user(c
, argc
, argv
);
5448 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5452 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5454 * All parameters are provided by the run_rpc_command function, except for
5455 * argc, argv which are passed through.
5457 * @param c A net_context structure.
5458 * @param domain_sid The domain sid acquired from the remote server.
5459 * @param cli A cli_state connected to the server.
5460 * @param mem_ctx Talloc context, destroyed on completion of the function.
5461 * @param argc Standard main() style argc.
5462 * @param argv Standard main() style argv. Initial components are already
5465 * @return Normal NTSTATUS return.
5468 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5469 const struct dom_sid
*domain_sid
,
5470 const char *domain_name
,
5471 struct cli_state
*cli
,
5472 struct rpc_pipe_client
*pipe_hnd
,
5473 TALLOC_CTX
*mem_ctx
,
5477 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5479 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5481 status
= dcerpc_initshutdown_Abort(b
, mem_ctx
, NULL
, &result
);
5482 if (!NT_STATUS_IS_OK(status
)) {
5485 if (W_ERROR_IS_OK(result
)) {
5486 d_printf(_("\nShutdown successfully aborted\n"));
5487 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5489 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5491 return werror_to_ntstatus(result
);
5495 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5497 * All parameters are provided by the run_rpc_command function, except for
5498 * argc, argv which are passed through.
5500 * @param c A net_context structure.
5501 * @param domain_sid The domain sid acquired from the remote server.
5502 * @param cli A cli_state connected to the server.
5503 * @param mem_ctx Talloc context, destroyed on completion of the function.
5504 * @param argc Standard main() style argc.
5505 * @param argv Standard main() style argv. Initial components are already
5508 * @return Normal NTSTATUS return.
5511 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5512 const struct dom_sid
*domain_sid
,
5513 const char *domain_name
,
5514 struct cli_state
*cli
,
5515 struct rpc_pipe_client
*pipe_hnd
,
5516 TALLOC_CTX
*mem_ctx
,
5520 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5522 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5524 result
= dcerpc_winreg_AbortSystemShutdown(b
, mem_ctx
, NULL
, &werr
);
5526 if (!NT_STATUS_IS_OK(result
)) {
5527 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5530 if (W_ERROR_IS_OK(werr
)) {
5531 d_printf(_("\nShutdown successfully aborted\n"));
5532 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5534 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5536 return werror_to_ntstatus(werr
);
5540 * ABORT the shutdown of a remote RPC server.
5542 * @param argc Standard main() style argc.
5543 * @param argv Standard main() style argv. Initial components are already
5546 * @return A shell status integer (0 for success).
5549 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5554 if (c
->display_usage
) {
5556 "net rpc abortshutdown\n"
5559 _("Abort a scheduled shutdown"));
5563 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5564 rpc_shutdown_abort_internals
, argc
, argv
);
5569 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5571 return run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5572 rpc_reg_shutdown_abort_internals
,
5577 * Shut down a remote RPC Server via initshutdown pipe.
5579 * All parameters are provided by the run_rpc_command function, except for
5580 * argc, argv which are passed through.
5582 * @param c A net_context structure.
5583 * @param domain_sid The domain sid acquired from the remote server.
5584 * @param cli A cli_state connected to the server.
5585 * @param mem_ctx Talloc context, destroyed on completion of the function.
5586 * @param argc Standard main() style argc.
5587 * @param argv Standard main() style argv. Initial components are already
5590 * @return Normal NTSTATUS return.
5593 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5594 const struct dom_sid
*domain_sid
,
5595 const char *domain_name
,
5596 struct cli_state
*cli
,
5597 struct rpc_pipe_client
*pipe_hnd
,
5598 TALLOC_CTX
*mem_ctx
,
5602 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5604 const char *msg
= N_("This machine will be shutdown shortly");
5605 uint32 timeout
= 20;
5606 struct lsa_StringLarge msg_string
;
5607 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5609 if (c
->opt_comment
) {
5610 msg
= c
->opt_comment
;
5612 if (c
->opt_timeout
) {
5613 timeout
= c
->opt_timeout
;
5616 msg_string
.string
= msg
;
5618 /* create an entry */
5619 status
= dcerpc_initshutdown_Init(b
, mem_ctx
, NULL
,
5620 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5622 if (!NT_STATUS_IS_OK(status
)) {
5625 if (W_ERROR_IS_OK(result
)) {
5626 d_printf(_("\nShutdown of remote machine succeeded\n"));
5627 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5629 DEBUG(1,("Shutdown of remote machine failed!\n"));
5631 return werror_to_ntstatus(result
);
5635 * Shut down a remote RPC Server via winreg pipe.
5637 * All parameters are provided by the run_rpc_command function, except for
5638 * argc, argv which are passed through.
5640 * @param c A net_context structure.
5641 * @param domain_sid The domain sid acquired from the remote server.
5642 * @param cli A cli_state connected to the server.
5643 * @param mem_ctx Talloc context, destroyed on completion of the function.
5644 * @param argc Standard main() style argc.
5645 * @param argv Standard main() style argv. Initial components are already
5648 * @return Normal NTSTATUS return.
5651 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5652 const struct dom_sid
*domain_sid
,
5653 const char *domain_name
,
5654 struct cli_state
*cli
,
5655 struct rpc_pipe_client
*pipe_hnd
,
5656 TALLOC_CTX
*mem_ctx
,
5660 const char *msg
= N_("This machine will be shutdown shortly");
5661 uint32 timeout
= 20;
5662 struct lsa_StringLarge msg_string
;
5665 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5667 if (c
->opt_comment
) {
5668 msg
= c
->opt_comment
;
5670 msg_string
.string
= msg
;
5672 if (c
->opt_timeout
) {
5673 timeout
= c
->opt_timeout
;
5676 /* create an entry */
5677 result
= dcerpc_winreg_InitiateSystemShutdown(b
, mem_ctx
, NULL
,
5678 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5680 if (!NT_STATUS_IS_OK(result
)) {
5681 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5685 if (W_ERROR_IS_OK(werr
)) {
5686 d_printf(_("\nShutdown of remote machine succeeded\n"));
5688 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5689 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5690 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5692 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5695 return werror_to_ntstatus(werr
);
5699 * Shut down a remote RPC server.
5701 * @param argc Standard main() style argc.
5702 * @param argv Standard main() style argv. Initial components are already
5705 * @return A shell status integer (0 for success).
5708 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5712 if (c
->display_usage
) {
5714 "net rpc shutdown\n"
5717 _("Shut down a remote RPC server"));
5721 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5722 rpc_init_shutdown_internals
, argc
, argv
);
5725 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5726 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5727 rpc_reg_shutdown_internals
, argc
, argv
);
5733 /***************************************************************************
5734 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5735 ***************************************************************************/
5738 * Add interdomain trust account to the RPC server.
5739 * All parameters (except for argc and argv) are passed by run_rpc_command
5742 * @param c A net_context structure.
5743 * @param domain_sid The domain sid acquired from the server.
5744 * @param cli A cli_state connected to the server.
5745 * @param mem_ctx Talloc context, destroyed on completion of the function.
5746 * @param argc Standard main() style argc.
5747 * @param argv Standard main() style argv. Initial components are already
5750 * @return normal NTSTATUS return code.
5753 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5754 const struct dom_sid
*domain_sid
,
5755 const char *domain_name
,
5756 struct cli_state
*cli
,
5757 struct rpc_pipe_client
*pipe_hnd
,
5758 TALLOC_CTX
*mem_ctx
,
5762 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5763 NTSTATUS status
, result
;
5765 struct lsa_String lsa_acct_name
;
5767 uint32 acct_flags
=0;
5769 uint32_t access_granted
= 0;
5770 union samr_UserInfo info
;
5771 unsigned int orig_timeout
;
5772 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5777 _(" net rpc trustdom add <domain_name> "
5778 "<trust password>\n"));
5779 return NT_STATUS_INVALID_PARAMETER
;
5783 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5786 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5787 return NT_STATUS_NO_MEMORY
;
5790 strupper_m(acct_name
);
5792 init_lsa_String(&lsa_acct_name
, acct_name
);
5794 /* Get samr policy handle */
5795 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5797 MAXIMUM_ALLOWED_ACCESS
,
5800 if (!NT_STATUS_IS_OK(status
)) {
5803 if (!NT_STATUS_IS_OK(result
)) {
5808 /* Get domain policy handle */
5809 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5811 MAXIMUM_ALLOWED_ACCESS
,
5812 discard_const_p(struct dom_sid2
, domain_sid
),
5815 if (!NT_STATUS_IS_OK(status
)) {
5818 if (!NT_STATUS_IS_OK(result
)) {
5823 /* This call can take a long time - allow the server to time out.
5824 * 35 seconds should do it. */
5826 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5828 /* Create trusting domain's account */
5829 acb_info
= ACB_NORMAL
;
5830 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5831 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5832 SAMR_USER_ACCESS_SET_PASSWORD
|
5833 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5834 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5836 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
5845 if (!NT_STATUS_IS_OK(status
)) {
5848 /* And restore our original timeout. */
5849 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5851 if (!NT_STATUS_IS_OK(result
)) {
5853 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5854 acct_name
, nt_errstr(result
));
5859 struct samr_CryptPassword crypt_pwd
;
5861 ZERO_STRUCT(info
.info23
);
5863 init_samr_CryptPassword(argv
[1],
5864 &cli
->user_session_key
,
5867 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5868 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5869 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5870 info
.info23
.password
= crypt_pwd
;
5872 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
5877 if (!NT_STATUS_IS_OK(status
)) {
5881 if (!NT_STATUS_IS_OK(result
)) {
5883 DEBUG(0,("Could not set trust account password: %s\n",
5884 nt_errstr(result
)));
5890 SAFE_FREE(acct_name
);
5895 * Create interdomain trust account for a remote domain.
5897 * @param argc Standard argc.
5898 * @param argv Standard argv without initial components.
5900 * @return Integer status (0 means success).
5903 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5905 if (argc
> 0 && !c
->display_usage
) {
5906 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5907 rpc_trustdom_add_internals
, argc
, argv
);
5911 _("net rpc trustdom add <domain_name> <trust "
5919 * Remove interdomain trust account from the RPC server.
5920 * All parameters (except for argc and argv) are passed by run_rpc_command
5923 * @param c A net_context structure.
5924 * @param domain_sid The domain sid acquired from the server.
5925 * @param cli A cli_state connected to the server.
5926 * @param mem_ctx Talloc context, destroyed on completion of the function.
5927 * @param argc Standard main() style argc.
5928 * @param argv Standard main() style argv. Initial components are already
5931 * @return normal NTSTATUS return code.
5934 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5935 const struct dom_sid
*domain_sid
,
5936 const char *domain_name
,
5937 struct cli_state
*cli
,
5938 struct rpc_pipe_client
*pipe_hnd
,
5939 TALLOC_CTX
*mem_ctx
,
5943 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5944 NTSTATUS status
, result
;
5946 struct dom_sid trust_acct_sid
;
5947 struct samr_Ids user_rids
, name_types
;
5948 struct lsa_String lsa_acct_name
;
5949 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5954 _(" net rpc trustdom del <domain_name>\n"));
5955 return NT_STATUS_INVALID_PARAMETER
;
5959 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5961 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5963 if (acct_name
== NULL
)
5964 return NT_STATUS_NO_MEMORY
;
5966 strupper_m(acct_name
);
5968 /* Get samr policy handle */
5969 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5971 MAXIMUM_ALLOWED_ACCESS
,
5974 if (!NT_STATUS_IS_OK(status
)) {
5977 if (!NT_STATUS_IS_OK(result
)) {
5982 /* Get domain policy handle */
5983 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5985 MAXIMUM_ALLOWED_ACCESS
,
5986 discard_const_p(struct dom_sid2
, domain_sid
),
5989 if (!NT_STATUS_IS_OK(status
)) {
5992 if (!NT_STATUS_IS_OK(result
)) {
5997 init_lsa_String(&lsa_acct_name
, acct_name
);
5999 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
6006 if (!NT_STATUS_IS_OK(status
)) {
6007 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6009 acct_name
, nt_errstr(status
));
6012 if (!NT_STATUS_IS_OK(result
)) {
6014 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6016 acct_name
, nt_errstr(result
) );
6020 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
6022 MAXIMUM_ALLOWED_ACCESS
,
6026 if (!NT_STATUS_IS_OK(status
)) {
6027 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6029 acct_name
, nt_errstr(status
) );
6033 if (!NT_STATUS_IS_OK(result
)) {
6035 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6037 acct_name
, nt_errstr(result
) );
6041 /* append the rid to the domain sid */
6042 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
6046 /* remove the sid */
6048 status
= dcerpc_samr_RemoveMemberFromForeignDomain(b
, mem_ctx
,
6052 if (!NT_STATUS_IS_OK(status
)) {
6053 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6054 " on user %s failed %s\n"),
6055 acct_name
, nt_errstr(status
));
6058 if (!NT_STATUS_IS_OK(result
)) {
6060 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6061 " on user %s failed %s\n"),
6062 acct_name
, nt_errstr(result
) );
6069 status
= dcerpc_samr_DeleteUser(b
, mem_ctx
,
6072 if (!NT_STATUS_IS_OK(status
)) {
6073 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6075 acct_name
, nt_errstr(status
));
6079 if (!NT_STATUS_IS_OK(result
)) {
6081 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6083 acct_name
, nt_errstr(result
) );
6087 if (!NT_STATUS_IS_OK(result
)) {
6088 d_printf(_("Could not set trust account password: %s\n"),
6098 * Delete interdomain trust account for a remote domain.
6100 * @param argc Standard argc.
6101 * @param argv Standard argv without initial components.
6103 * @return Integer status (0 means success).
6106 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
6108 if (argc
> 0 && !c
->display_usage
) {
6109 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
6110 rpc_trustdom_del_internals
, argc
, argv
);
6114 _("net rpc trustdom del <domain>\n"));
6119 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
6120 struct cli_state
*cli
,
6121 TALLOC_CTX
*mem_ctx
,
6122 const char *domain_name
)
6124 char *dc_name
= NULL
;
6125 const char *buffer
= NULL
;
6126 struct rpc_pipe_client
*netr
;
6129 struct dcerpc_binding_handle
*b
;
6131 /* Use NetServerEnum2 */
6133 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
6135 return NT_STATUS_OK
;
6138 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6139 for domain %s\n", domain_name
));
6141 /* Try netr_GetDcName */
6143 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
6145 if (!NT_STATUS_IS_OK(status
)) {
6149 b
= netr
->binding_handle
;
6151 status
= dcerpc_netr_GetDcName(b
, mem_ctx
,
6158 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(result
)) {
6162 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6163 for domain %s\n", domain_name
));
6165 if (!NT_STATUS_IS_OK(status
)) {
6169 return werror_to_ntstatus(result
);
6173 * Establish trust relationship to a trusting domain.
6174 * Interdomain account must already be created on remote PDC.
6176 * @param c A net_context structure.
6177 * @param argc Standard argc.
6178 * @param argv Standard argv without initial components.
6180 * @return Integer status (0 means success).
6183 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6186 struct cli_state
*cli
= NULL
;
6187 struct sockaddr_storage server_ss
;
6188 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6189 struct policy_handle connect_hnd
;
6190 TALLOC_CTX
*mem_ctx
;
6191 NTSTATUS nt_status
, result
;
6192 struct dom_sid
*domain_sid
;
6197 union lsa_PolicyInformation
*info
= NULL
;
6198 struct dcerpc_binding_handle
*b
;
6201 * Connect to \\server\ipc$ as 'our domain' account with password
6204 if (argc
!= 1 || c
->display_usage
) {
6207 _("net rpc trustdom establish <domain_name>\n"));
6211 domain_name
= smb_xstrdup(argv
[0]);
6212 strupper_m(domain_name
);
6214 /* account name used at first is our domain's name with '$' */
6215 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
6218 strupper_m(acct_name
);
6221 * opt_workgroup will be used by connection functions further,
6222 * hence it should be set to remote domain name instead of ours
6224 if (c
->opt_workgroup
) {
6225 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6228 c
->opt_user_name
= acct_name
;
6230 /* find the domain controller */
6231 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6232 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6236 /* connect to ipc$ as username/password */
6237 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6238 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6240 /* Is it trusting domain account for sure ? */
6241 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6242 nt_errstr(nt_status
)));
6246 /* store who we connected to */
6248 saf_store( domain_name
, pdc_name
);
6251 * Connect to \\server\ipc$ again (this time anonymously)
6254 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6257 if (NT_STATUS_IS_ERR(nt_status
)) {
6258 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6259 domain_name
, nt_errstr(nt_status
)));
6263 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6264 "domain %s", domain_name
))) {
6265 DEBUG(0, ("talloc_init() failed\n"));
6270 /* Make sure we're talking to a proper server */
6272 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6273 if (!NT_STATUS_IS_OK(nt_status
)) {
6275 talloc_destroy(mem_ctx
);
6280 * Call LsaOpenPolicy and LsaQueryInfo
6283 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6285 if (!NT_STATUS_IS_OK(nt_status
)) {
6286 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6288 talloc_destroy(mem_ctx
);
6292 b
= pipe_hnd
->binding_handle
;
6294 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
6296 if (NT_STATUS_IS_ERR(nt_status
)) {
6297 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6298 nt_errstr(nt_status
)));
6300 talloc_destroy(mem_ctx
);
6304 /* Querying info level 5 */
6306 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6308 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6311 if (NT_STATUS_IS_ERR(nt_status
)) {
6312 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6313 nt_errstr(nt_status
)));
6315 talloc_destroy(mem_ctx
);
6318 if (NT_STATUS_IS_ERR(result
)) {
6319 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6320 nt_errstr(result
)));
6322 talloc_destroy(mem_ctx
);
6326 domain_sid
= info
->account_domain
.sid
;
6328 /* There should be actually query info level 3 (following nt serv behaviour),
6329 but I still don't know if it's _really_ necessary */
6332 * Store the password in secrets db
6335 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6336 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6338 talloc_destroy(mem_ctx
);
6343 * Close the pipes and clean up
6346 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6347 if (NT_STATUS_IS_ERR(nt_status
)) {
6348 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6349 nt_errstr(nt_status
)));
6351 talloc_destroy(mem_ctx
);
6357 talloc_destroy(mem_ctx
);
6359 d_printf(_("Trust to domain %s established\n"), domain_name
);
6364 * Revoke trust relationship to the remote domain.
6366 * @param c A net_context structure.
6367 * @param argc Standard argc.
6368 * @param argv Standard argv without initial components.
6370 * @return Integer status (0 means success).
6373 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6379 if (argc
< 1 || c
->display_usage
) {
6382 _("net rpc trustdom revoke <domain_name>\n"
6383 " Revoke trust relationship\n"
6384 " domain_name\tName of domain to revoke trust\n"));
6388 /* generate upper cased domain name */
6389 domain_name
= smb_xstrdup(argv
[0]);
6390 strupper_m(domain_name
);
6392 /* delete password of the trust */
6393 if (!pdb_del_trusteddom_pw(domain_name
)) {
6394 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6401 SAFE_FREE(domain_name
);
6405 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6406 const struct dom_sid
*domain_sid
,
6407 const char *domain_name
,
6408 struct cli_state
*cli
,
6409 struct rpc_pipe_client
*pipe_hnd
,
6410 TALLOC_CTX
*mem_ctx
,
6415 if (!sid_to_fstring(str_sid
, domain_sid
)) {
6416 return NT_STATUS_UNSUCCESSFUL
;
6418 d_printf("%s\n", str_sid
);
6419 return NT_STATUS_OK
;
6422 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
6426 /* convert sid into ascii string */
6427 sid_to_fstring(ascii_sid
, dom_sid
);
6429 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
6432 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6433 TALLOC_CTX
*mem_ctx
,
6434 struct policy_handle
*pol
,
6435 struct dom_sid dom_sid
,
6436 const char *trusted_dom_name
)
6438 NTSTATUS nt_status
, result
;
6439 union lsa_TrustedDomainInfo
*info
= NULL
;
6440 char *cleartextpwd
= NULL
;
6441 DATA_BLOB session_key
;
6442 DATA_BLOB data
= data_blob_null
;
6443 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6445 nt_status
= dcerpc_lsa_QueryTrustedDomainInfoBySid(b
, mem_ctx
,
6448 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6451 if (NT_STATUS_IS_ERR(nt_status
)) {
6452 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6453 nt_errstr(nt_status
)));
6456 if (NT_STATUS_IS_ERR(result
)) {
6458 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6459 nt_errstr(result
)));
6463 data
= data_blob(info
->password
.password
->data
,
6464 info
->password
.password
->length
);
6466 nt_status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
6467 if (!NT_STATUS_IS_OK(nt_status
)) {
6468 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status
)));
6472 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key
);
6473 data_blob_free(&session_key
);
6475 if (cleartextpwd
== NULL
) {
6476 DEBUG(0,("retrieved NULL password\n"));
6477 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6481 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6482 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6483 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6487 #ifdef DEBUG_PASSWORD
6488 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6489 "password: [%s]\n", trusted_dom_name
,
6490 sid_string_dbg(&dom_sid
), cleartextpwd
));
6494 SAFE_FREE(cleartextpwd
);
6495 data_blob_free(&data
);
6500 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6503 /* common variables */
6504 TALLOC_CTX
* mem_ctx
;
6505 struct cli_state
*cli
= NULL
;
6506 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6507 NTSTATUS nt_status
, result
;
6508 const char *domain_name
= NULL
;
6509 struct policy_handle connect_hnd
;
6510 union lsa_PolicyInformation
*info
= NULL
;
6512 /* trusted domains listing variables */
6513 unsigned int enum_ctx
= 0;
6515 struct lsa_DomainList dom_list
;
6517 struct dcerpc_binding_handle
*b
;
6519 if (c
->display_usage
) {
6521 "net rpc trustdom vampire\n"
6524 _("Vampire trust relationship from remote server"));
6529 * Listing trusted domains (stored in secrets.tdb, if local)
6532 mem_ctx
= talloc_init("trust relationships vampire");
6535 * set domain and pdc name to local samba server (default)
6536 * or to remote one given in command line
6539 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6540 domain_name
= c
->opt_workgroup
;
6541 c
->opt_target_workgroup
= c
->opt_workgroup
;
6543 fstrcpy(pdc_name
, lp_netbios_name());
6544 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6545 c
->opt_target_workgroup
= domain_name
;
6548 /* open \PIPE\lsarpc and open policy handle */
6549 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6550 if (!NT_STATUS_IS_OK(nt_status
)) {
6551 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6552 nt_errstr(nt_status
)));
6553 talloc_destroy(mem_ctx
);
6557 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6559 if (!NT_STATUS_IS_OK(nt_status
)) {
6560 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6561 nt_errstr(nt_status
) ));
6563 talloc_destroy(mem_ctx
);
6567 b
= pipe_hnd
->binding_handle
;
6569 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6571 if (NT_STATUS_IS_ERR(nt_status
)) {
6572 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6573 nt_errstr(nt_status
)));
6575 talloc_destroy(mem_ctx
);
6579 /* query info level 5 to obtain sid of a domain being queried */
6580 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6582 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6586 if (NT_STATUS_IS_ERR(nt_status
)) {
6587 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6588 nt_errstr(nt_status
)));
6590 talloc_destroy(mem_ctx
);
6593 if (NT_STATUS_IS_ERR(result
)) {
6594 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6595 nt_errstr(result
)));
6597 talloc_destroy(mem_ctx
);
6602 * Keep calling LsaEnumTrustdom over opened pipe until
6603 * the end of enumeration is reached
6606 d_printf(_("Vampire trusted domains:\n\n"));
6609 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6615 if (NT_STATUS_IS_ERR(nt_status
)) {
6616 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6617 nt_errstr(nt_status
)));
6619 talloc_destroy(mem_ctx
);
6622 if (NT_STATUS_IS_ERR(result
)) {
6624 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6625 nt_errstr(result
)));
6627 talloc_destroy(mem_ctx
);
6632 for (i
= 0; i
< dom_list
.count
; i
++) {
6634 print_trusted_domain(dom_list
.domains
[i
].sid
,
6635 dom_list
.domains
[i
].name
.string
);
6637 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6638 *dom_list
.domains
[i
].sid
,
6639 dom_list
.domains
[i
].name
.string
);
6640 if (!NT_STATUS_IS_OK(nt_status
)) {
6642 talloc_destroy(mem_ctx
);
6648 * in case of no trusted domains say something rather
6649 * than just display blank line
6651 if (!dom_list
.count
) d_printf(_("none\n"));
6653 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6655 /* close this connection before doing next one */
6656 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6657 if (NT_STATUS_IS_ERR(nt_status
)) {
6658 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6659 nt_errstr(nt_status
)));
6661 talloc_destroy(mem_ctx
);
6665 /* close lsarpc pipe and connection to IPC$ */
6668 talloc_destroy(mem_ctx
);
6672 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6674 /* common variables */
6675 TALLOC_CTX
* mem_ctx
;
6676 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6677 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6678 NTSTATUS nt_status
, result
;
6679 const char *domain_name
= NULL
;
6680 struct dom_sid
*queried_dom_sid
;
6681 int ascii_dom_name_len
;
6682 struct policy_handle connect_hnd
;
6683 union lsa_PolicyInformation
*info
= NULL
;
6684 struct dcerpc_binding_handle
*b
= NULL
;
6686 /* trusted domains listing variables */
6687 unsigned int num_domains
, enum_ctx
= 0;
6689 struct lsa_DomainList dom_list
;
6693 /* trusting domains listing variables */
6694 struct policy_handle domain_hnd
;
6695 struct samr_SamArray
*trusts
= NULL
;
6697 if (c
->display_usage
) {
6699 "net rpc trustdom list\n"
6702 _("List incoming and outgoing trust relationships"));
6707 * Listing trusted domains (stored in secrets.tdb, if local)
6710 mem_ctx
= talloc_init("trust relationships listing");
6713 * set domain and pdc name to local samba server (default)
6714 * or to remote one given in command line
6717 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6718 domain_name
= c
->opt_workgroup
;
6719 c
->opt_target_workgroup
= c
->opt_workgroup
;
6721 fstrcpy(pdc_name
, lp_netbios_name());
6722 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6723 c
->opt_target_workgroup
= domain_name
;
6726 /* open \PIPE\lsarpc and open policy handle */
6727 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6728 if (!NT_STATUS_IS_OK(nt_status
)) {
6729 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6730 nt_errstr(nt_status
)));
6731 talloc_destroy(mem_ctx
);
6735 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6737 if (!NT_STATUS_IS_OK(nt_status
)) {
6738 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6739 nt_errstr(nt_status
) ));
6741 talloc_destroy(mem_ctx
);
6745 b
= pipe_hnd
->binding_handle
;
6747 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6749 if (NT_STATUS_IS_ERR(nt_status
)) {
6750 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6751 nt_errstr(nt_status
)));
6753 talloc_destroy(mem_ctx
);
6757 /* query info level 5 to obtain sid of a domain being queried */
6758 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6760 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6764 if (NT_STATUS_IS_ERR(nt_status
)) {
6765 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6766 nt_errstr(nt_status
)));
6768 talloc_destroy(mem_ctx
);
6771 if (NT_STATUS_IS_ERR(result
)) {
6772 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6773 nt_errstr(result
)));
6775 talloc_destroy(mem_ctx
);
6779 queried_dom_sid
= info
->account_domain
.sid
;
6782 * Keep calling LsaEnumTrustdom over opened pipe until
6783 * the end of enumeration is reached
6786 d_printf(_("Trusted domains list:\n\n"));
6788 found_domain
= false;
6791 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6797 if (NT_STATUS_IS_ERR(nt_status
)) {
6798 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6799 nt_errstr(nt_status
)));
6801 talloc_destroy(mem_ctx
);
6804 if (NT_STATUS_IS_ERR(result
)) {
6805 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6806 nt_errstr(result
)));
6808 talloc_destroy(mem_ctx
);
6813 for (i
= 0; i
< dom_list
.count
; i
++) {
6814 print_trusted_domain(dom_list
.domains
[i
].sid
,
6815 dom_list
.domains
[i
].name
.string
);
6816 found_domain
= true;
6820 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6823 * in case of no trusted domains say something rather
6824 * than just display blank line
6826 if (!found_domain
) {
6827 d_printf(_("none\n"));
6830 /* close this connection before doing next one */
6831 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6832 if (NT_STATUS_IS_ERR(nt_status
)) {
6833 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6834 nt_errstr(nt_status
)));
6836 talloc_destroy(mem_ctx
);
6840 TALLOC_FREE(pipe_hnd
);
6843 * Listing trusting domains (stored in passdb backend, if local)
6846 d_printf(_("\nTrusting domains list:\n\n"));
6849 * Open \PIPE\samr and get needed policy handles
6851 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6853 if (!NT_STATUS_IS_OK(nt_status
)) {
6854 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6856 talloc_destroy(mem_ctx
);
6860 b
= pipe_hnd
->binding_handle
;
6863 nt_status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6865 SAMR_ACCESS_LOOKUP_DOMAIN
,
6868 if (!NT_STATUS_IS_OK(nt_status
)) {
6869 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6870 nt_errstr(nt_status
)));
6872 talloc_destroy(mem_ctx
);
6875 if (!NT_STATUS_IS_OK(result
)) {
6877 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6878 nt_errstr(result
)));
6880 talloc_destroy(mem_ctx
);
6884 /* SamrOpenDomain - we have to open domain policy handle in order to be
6885 able to enumerate accounts*/
6886 nt_status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6888 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6892 if (!NT_STATUS_IS_OK(nt_status
)) {
6893 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6894 nt_errstr(nt_status
)));
6896 talloc_destroy(mem_ctx
);
6899 if (!NT_STATUS_IS_OK(result
)) {
6901 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6902 nt_errstr(result
)));
6904 talloc_destroy(mem_ctx
);
6909 * perform actual enumeration
6912 found_domain
= false;
6914 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6917 nt_status
= dcerpc_samr_EnumDomainUsers(b
, mem_ctx
,
6925 if (NT_STATUS_IS_ERR(nt_status
)) {
6926 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6927 nt_errstr(nt_status
)));
6929 talloc_destroy(mem_ctx
);
6932 if (NT_STATUS_IS_ERR(result
)) {
6934 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6935 nt_errstr(result
)));
6937 talloc_destroy(mem_ctx
);
6941 for (i
= 0; i
< num_domains
; i
++) {
6943 char *str
= discard_const_p(char, trusts
->entries
[i
].name
.string
);
6945 found_domain
= true;
6948 * get each single domain's sid (do we _really_ need this ?):
6949 * 1) connect to domain's pdc
6950 * 2) query the pdc for domain's sid
6953 /* get rid of '$' tail */
6954 ascii_dom_name_len
= strlen(str
);
6955 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6956 str
[ascii_dom_name_len
- 1] = '\0';
6958 /* set opt_* variables to remote domain */
6960 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6961 c
->opt_target_workgroup
= c
->opt_workgroup
;
6963 d_printf("%-20s", str
);
6965 /* connect to remote domain controller */
6966 nt_status
= net_make_ipc_connection(c
,
6967 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6969 if (NT_STATUS_IS_OK(nt_status
)) {
6970 /* query for domain's sid */
6971 if (run_rpc_command(
6973 &ndr_table_lsarpc
.syntax_id
, 0,
6974 rpc_query_domain_sid
, argc
,
6976 d_printf(_("strange - couldn't get domain's sid\n"));
6978 cli_shutdown(remote_cli
);
6981 d_fprintf(stderr
, _("domain controller is not "
6982 "responding: %s\n"),
6983 nt_errstr(nt_status
));
6984 d_printf(_("couldn't get domain's sid\n"));
6988 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
6990 if (!found_domain
) {
6994 /* close opened samr and domain policy handles */
6995 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &domain_hnd
, &result
);
6996 if (!NT_STATUS_IS_OK(nt_status
)) {
6997 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
7000 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &connect_hnd
, &result
);
7001 if (!NT_STATUS_IS_OK(nt_status
)) {
7002 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
7005 /* close samr pipe and connection to IPC$ */
7008 talloc_destroy(mem_ctx
);
7013 * Entrypoint for 'net rpc trustdom' code.
7015 * @param argc Standard argc.
7016 * @param argv Standard argv without initial components.
7018 * @return Integer status (0 means success).
7021 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
7023 struct functable func
[] = {
7028 N_("Add trusting domain's account"),
7029 N_("net rpc trustdom add\n"
7030 " Add trusting domain's account")
7036 N_("Remove trusting domain's account"),
7037 N_("net rpc trustdom del\n"
7038 " Remove trusting domain's account")
7042 rpc_trustdom_establish
,
7044 N_("Establish outgoing trust relationship"),
7045 N_("net rpc trustdom establish\n"
7046 " Establish outgoing trust relationship")
7050 rpc_trustdom_revoke
,
7052 N_("Revoke outgoing trust relationship"),
7053 N_("net rpc trustdom revoke\n"
7054 " Revoke outgoing trust relationship")
7060 N_("List in- and outgoing domain trusts"),
7061 N_("net rpc trustdom list\n"
7062 " List in- and outgoing domain trusts")
7066 rpc_trustdom_vampire
,
7068 N_("Vampire trusts from remote server"),
7069 N_("net rpc trustdom vampire\n"
7070 " Vampire trusts from remote server")
7072 {NULL
, NULL
, 0, NULL
, NULL
}
7075 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
7079 * Check if a server will take rpc commands
7080 * @param flags Type of server to connect to (PDC, DMB, localhost)
7081 * if the host is not explicitly specified
7082 * @return bool (true means rpc supported)
7084 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
7086 struct cli_state
*cli
;
7088 struct sockaddr_storage server_ss
;
7089 char *server_name
= NULL
;
7092 /* flags (i.e. server type) may depend on command */
7093 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
7096 status
= cli_connect_nb(server_name
, &server_ss
, 0, 0x20,
7097 lp_netbios_name(), SMB_SIGNING_DEFAULT
,
7099 if (!NT_STATUS_IS_OK(status
)) {
7102 status
= cli_negprot(cli
, PROTOCOL_NT1
);
7103 if (!NT_STATUS_IS_OK(status
))
7105 if (cli_state_protocol(cli
) < PROTOCOL_NT1
)
7114 /* dump sam database via samsync rpc calls */
7115 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
7116 if (c
->display_usage
) {
7121 _("Dump remote SAM database"));
7125 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
7126 NET_FLAGS_ANONYMOUS
,
7127 rpc_samdump_internals
, argc
, argv
);
7130 /* syncronise sam database via samsync rpc calls */
7131 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
7133 struct functable func
[] = {
7138 N_("Dump remote SAM database to ldif"),
7139 N_("net rpc vampire ldif\n"
7140 " Dump remote SAM database to LDIF file or "
7147 N_("Dump remote SAM database to Kerberos Keytab"),
7148 N_("net rpc vampire keytab\n"
7149 " Dump remote SAM database to Kerberos keytab "
7156 N_("Dump remote SAM database to passdb"),
7157 N_("net rpc vampire passdb\n"
7158 " Dump remote SAM database to passdb")
7161 {NULL
, NULL
, 0, NULL
, NULL
}
7165 if (c
->display_usage
) {
7170 _("Vampire remote SAM database"));
7174 return rpc_vampire_passdb(c
, argc
, argv
);
7177 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
7181 * Migrate everything from a print server.
7183 * @param c A net_context structure.
7184 * @param argc Standard main() style argc.
7185 * @param argv Standard main() style argv. Initial components are already
7188 * @return A shell status integer (0 for success).
7190 * The order is important !
7191 * To successfully add drivers the print queues have to exist !
7192 * Applying ACLs should be the last step, because you're easily locked out.
7195 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
7200 if (c
->display_usage
) {
7202 "net rpc printer migrate all\n"
7205 _("Migrate everything from a print server"));
7210 d_printf(_("no server to migrate\n"));
7214 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7215 rpc_printer_migrate_printers_internals
, argc
,
7220 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7221 rpc_printer_migrate_drivers_internals
, argc
,
7226 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7227 rpc_printer_migrate_forms_internals
, argc
, argv
);
7231 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7232 rpc_printer_migrate_settings_internals
, argc
,
7237 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7238 rpc_printer_migrate_security_internals
, argc
,
7244 * Migrate print drivers from a print server.
7246 * @param c A net_context structure.
7247 * @param argc Standard main() style argc.
7248 * @param argv Standard main() style argv. Initial components are already
7251 * @return A shell status integer (0 for success).
7253 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
7256 if (c
->display_usage
) {
7258 "net rpc printer migrate drivers\n"
7261 _("Migrate print-drivers from a print-server"));
7266 d_printf(_("no server to migrate\n"));
7270 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7271 rpc_printer_migrate_drivers_internals
,
7276 * Migrate print-forms from a print-server.
7278 * @param c A net_context structure.
7279 * @param argc Standard main() style argc.
7280 * @param argv Standard main() style argv. Initial components are already
7283 * @return A shell status integer (0 for success).
7285 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7288 if (c
->display_usage
) {
7290 "net rpc printer migrate forms\n"
7293 _("Migrate print-forms from a print-server"));
7298 d_printf(_("no server to migrate\n"));
7302 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7303 rpc_printer_migrate_forms_internals
,
7308 * Migrate printers from a print-server.
7310 * @param c A net_context structure.
7311 * @param argc Standard main() style argc.
7312 * @param argv Standard main() style argv. Initial components are already
7315 * @return A shell status integer (0 for success).
7317 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7320 if (c
->display_usage
) {
7322 "net rpc printer migrate printers\n"
7325 _("Migrate printers from a print-server"));
7330 d_printf(_("no server to migrate\n"));
7334 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7335 rpc_printer_migrate_printers_internals
,
7340 * Migrate printer-ACLs from a print-server
7342 * @param c A net_context structure.
7343 * @param argc Standard main() style argc.
7344 * @param argv Standard main() style argv. Initial components are already
7347 * @return A shell status integer (0 for success).
7349 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7352 if (c
->display_usage
) {
7354 "net rpc printer migrate security\n"
7357 _("Migrate printer-ACLs from a print-server"));
7362 d_printf(_("no server to migrate\n"));
7366 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7367 rpc_printer_migrate_security_internals
,
7372 * Migrate printer-settings from a print-server.
7374 * @param c A net_context structure.
7375 * @param argc Standard main() style argc.
7376 * @param argv Standard main() style argv. Initial components are already
7379 * @return A shell status integer (0 for success).
7381 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7384 if (c
->display_usage
) {
7386 "net rpc printer migrate settings\n"
7389 _("Migrate printer-settings from a "
7395 d_printf(_("no server to migrate\n"));
7399 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7400 rpc_printer_migrate_settings_internals
,
7405 * 'net rpc printer' entrypoint.
7407 * @param c A net_context structure.
7408 * @param argc Standard main() style argc.
7409 * @param argv Standard main() style argv. Initial components are already
7413 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7416 /* ouch: when addriver and setdriver are called from within
7417 rpc_printer_migrate_drivers_internals, the printer-queue already
7420 struct functable func
[] = {
7423 rpc_printer_migrate_all
,
7425 N_("Migrate all from remote to local print server"),
7426 N_("net rpc printer migrate all\n"
7427 " Migrate all from remote to local print server")
7431 rpc_printer_migrate_drivers
,
7433 N_("Migrate drivers to local server"),
7434 N_("net rpc printer migrate drivers\n"
7435 " Migrate drivers to local server")
7439 rpc_printer_migrate_forms
,
7441 N_("Migrate froms to local server"),
7442 N_("net rpc printer migrate forms\n"
7443 " Migrate froms to local server")
7447 rpc_printer_migrate_printers
,
7449 N_("Migrate printers to local server"),
7450 N_("net rpc printer migrate printers\n"
7451 " Migrate printers to local server")
7455 rpc_printer_migrate_security
,
7457 N_("Mirgate printer ACLs to local server"),
7458 N_("net rpc printer migrate security\n"
7459 " Mirgate printer ACLs to local server")
7463 rpc_printer_migrate_settings
,
7465 N_("Migrate printer settings to local server"),
7466 N_("net rpc printer migrate settings\n"
7467 " Migrate printer settings to local server")
7469 {NULL
, NULL
, 0, NULL
, NULL
}
7472 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7477 * List printers on a remote RPC server.
7479 * @param c A net_context structure.
7480 * @param argc Standard main() style argc.
7481 * @param argv Standard main() style argv. Initial components are already
7484 * @return A shell status integer (0 for success).
7486 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7488 if (c
->display_usage
) {
7490 "net rpc printer list\n"
7493 _("List printers on a remote RPC server"));
7497 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7498 rpc_printer_list_internals
,
7503 * List printer-drivers on a remote RPC server.
7505 * @param c A net_context structure.
7506 * @param argc Standard main() style argc.
7507 * @param argv Standard main() style argv. Initial components are already
7510 * @return A shell status integer (0 for success).
7512 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7515 if (c
->display_usage
) {
7517 "net rpc printer driver\n"
7520 _("List printer-drivers on a remote RPC server"));
7524 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7525 rpc_printer_driver_list_internals
,
7530 * Publish printer in ADS via MSRPC.
7532 * @param c A net_context structure.
7533 * @param argc Standard main() style argc.
7534 * @param argv Standard main() style argv. Initial components are already
7537 * @return A shell status integer (0 for success).
7539 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7542 if (c
->display_usage
) {
7544 "net rpc printer publish publish\n"
7547 _("Publish printer in ADS via MSRPC"));
7551 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7552 rpc_printer_publish_publish_internals
,
7557 * Update printer in ADS via MSRPC.
7559 * @param c A net_context structure.
7560 * @param argc Standard main() style argc.
7561 * @param argv Standard main() style argv. Initial components are already
7564 * @return A shell status integer (0 for success).
7566 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7568 if (c
->display_usage
) {
7570 "net rpc printer publish update\n"
7573 _("Update printer in ADS via MSRPC"));
7577 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7578 rpc_printer_publish_update_internals
,
7583 * UnPublish printer in ADS via MSRPC.
7585 * @param c A net_context structure.
7586 * @param argc Standard main() style argc.
7587 * @param argv Standard main() style argv. Initial components are already
7590 * @return A shell status integer (0 for success).
7592 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7595 if (c
->display_usage
) {
7597 "net rpc printer publish unpublish\n"
7600 _("UnPublish printer in ADS via MSRPC"));
7604 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7605 rpc_printer_publish_unpublish_internals
,
7610 * List published printers via MSRPC.
7612 * @param c A net_context structure.
7613 * @param argc Standard main() style argc.
7614 * @param argv Standard main() style argv. Initial components are already
7617 * @return A shell status integer (0 for success).
7619 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7622 if (c
->display_usage
) {
7624 "net rpc printer publish list\n"
7627 _("List published printers via MSRPC"));
7631 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7632 rpc_printer_publish_list_internals
,
7638 * Publish printer in ADS.
7640 * @param c A net_context structure.
7641 * @param argc Standard main() style argc.
7642 * @param argv Standard main() style argv. Initial components are already
7645 * @return A shell status integer (0 for success).
7647 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7651 struct functable func
[] = {
7654 rpc_printer_publish_publish
,
7656 N_("Publish printer in AD"),
7657 N_("net rpc printer publish publish\n"
7658 " Publish printer in AD")
7662 rpc_printer_publish_update
,
7664 N_("Update printer in AD"),
7665 N_("net rpc printer publish update\n"
7666 " Update printer in AD")
7670 rpc_printer_publish_unpublish
,
7672 N_("Unpublish printer"),
7673 N_("net rpc printer publish unpublish\n"
7674 " Unpublish printer")
7678 rpc_printer_publish_list
,
7680 N_("List published printers"),
7681 N_("net rpc printer publish list\n"
7682 " List published printers")
7684 {NULL
, NULL
, 0, NULL
, NULL
}
7688 if (c
->display_usage
) {
7689 d_printf(_("Usage:\n"));
7690 d_printf(_("net rpc printer publish\n"
7691 " List published printers\n"
7692 " Alias of net rpc printer publish "
7694 net_display_usage_from_functable(func
);
7697 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7698 rpc_printer_publish_list_internals
,
7702 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7708 * Display rpc printer help page.
7710 * @param c A net_context structure.
7711 * @param argc Standard main() style argc.
7712 * @param argv Standard main() style argv. Initial components are already
7715 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7717 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7718 "\tlists all printers on print-server\n\n"));
7719 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7720 "\tlists all printer-drivers on print-server\n\n"));
7721 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7722 "\tpublishes printer settings in Active Directory\n"
7723 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7724 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7725 "\n\tmigrates printers from remote to local server\n\n"));
7726 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7727 "\n\tmigrates printer-settings from remote to local server\n\n"));
7728 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7729 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7730 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7731 "\n\tmigrates printer-forms from remote to local server\n\n"));
7732 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7733 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7734 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7735 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7736 "\tremote to local print-server\n\n"));
7737 net_common_methods_usage(c
, argc
, argv
);
7738 net_common_flags_usage(c
, argc
, argv
);
7740 "\t-v or --verbose\t\t\tgive verbose output\n"
7741 "\t --destination\t\tmigration target server (default: localhost)\n"));
7747 * 'net rpc printer' entrypoint.
7749 * @param c A net_context structure.
7750 * @param argc Standard main() style argc.
7751 * @param argv Standard main() style argv. Initial components are already
7754 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7756 struct functable func
[] = {
7761 N_("List all printers on print server"),
7762 N_("net rpc printer list\n"
7763 " List all printers on print server")
7767 rpc_printer_migrate
,
7769 N_("Migrate printer to local server"),
7770 N_("net rpc printer migrate\n"
7771 " Migrate printer to local server")
7775 rpc_printer_driver_list
,
7777 N_("List printer drivers"),
7778 N_("net rpc printer driver\n"
7779 " List printer drivers")
7783 rpc_printer_publish
,
7785 N_("Publish printer in AD"),
7786 N_("net rpc printer publish\n"
7787 " Publish printer in AD")
7789 {NULL
, NULL
, 0, NULL
, NULL
}
7793 if (c
->display_usage
) {
7794 d_printf(_("Usage:\n"));
7795 d_printf(_("net rpc printer\n"
7796 " List printers\n"));
7797 net_display_usage_from_functable(func
);
7800 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7801 rpc_printer_list_internals
,
7805 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7809 * 'net rpc' entrypoint.
7811 * @param c A net_context structure.
7812 * @param argc Standard main() style argc.
7813 * @param argv Standard main() style argv. Initial components are already
7817 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7819 NET_API_STATUS status
;
7821 struct functable func
[] = {
7826 N_("Modify global audit settings"),
7827 N_("net rpc audit\n"
7828 " Modify global audit settings")
7834 N_("Show basic info about a domain"),
7836 " Show basic info about a domain")
7842 N_("Join a domain"),
7850 N_("Join a domain created in server manager"),
7851 N_("net rpc oldjoin\n"
7852 " Join a domain created in server manager")
7858 N_("Test that a join is valid"),
7859 N_("net rpc testjoin\n"
7860 " Test that a join is valid")
7866 N_("List/modify users"),
7868 " List/modify users")
7874 N_("Change a user password"),
7875 N_("net rpc password\n"
7876 " Change a user password\n"
7877 " Alias for net rpc user password")
7883 N_("List/modify groups"),
7884 N_("net rpc group\n"
7885 " List/modify groups")
7891 N_("List/modify shares"),
7892 N_("net rpc share\n"
7893 " List/modify shares")
7899 N_("List open files"),
7907 N_("List/modify printers"),
7908 N_("net rpc printer\n"
7909 " List/modify printers")
7913 net_rpc_changetrustpw
,
7915 N_("Change trust account password"),
7916 N_("net rpc changetrustpw\n"
7917 " Change trust account password")
7923 N_("Modify domain trusts"),
7924 N_("net rpc trustdom\n"
7925 " Modify domain trusts")
7931 N_("Abort a remote shutdown"),
7932 N_("net rpc abortshutdown\n"
7933 " Abort a remote shutdown")
7939 N_("Shutdown a remote server"),
7940 N_("net rpc shutdown\n"
7941 " Shutdown a remote server")
7947 N_("Dump SAM data of remote NT PDC"),
7948 N_("net rpc samdump\n"
7949 " Dump SAM data of remote NT PDC")
7955 N_("Sync a remote NT PDC's data into local passdb"),
7956 N_("net rpc vampire\n"
7957 " Sync a remote NT PDC's data into local passdb")
7963 N_("Fetch the domain sid into local secrets.tdb"),
7964 N_("net rpc getsid\n"
7965 " Fetch the domain sid into local secrets.tdb")
7971 N_("Manage privileges assigned to SID"),
7972 N_("net rpc rights\n"
7973 " Manage privileges assigned to SID")
7979 N_("Start/stop/query remote services"),
7980 N_("net rpc service\n"
7981 " Start/stop/query remote services")
7987 N_("Manage registry hives"),
7988 N_("net rpc registry\n"
7989 " Manage registry hives")
7995 N_("Open interactive shell on remote server"),
7996 N_("net rpc shell\n"
7997 " Open interactive shell on remote server")
8003 N_("Manage trusts"),
8004 N_("net rpc trust\n"
8011 N_("Configure a remote samba server"),
8013 " Configure a remote samba server")
8015 {NULL
, NULL
, 0, NULL
, NULL
}
8018 status
= libnetapi_net_init(&c
->netapi_ctx
);
8022 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
8023 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
8024 if (c
->opt_kerberos
) {
8025 libnetapi_set_use_kerberos(c
->netapi_ctx
);
8027 if (c
->opt_ccache
) {
8028 libnetapi_set_use_ccache(c
->netapi_ctx
);
8031 return net_run_function(c
, argc
, argv
, "net rpc", func
);