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"
46 #include "../libcli/smb/smbXcli_base.h"
48 static int net_mode_share
;
49 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
);
54 * @brief RPC based subcommands for the 'net' utility.
56 * This file should contain much of the functionality that used to
57 * be found in rpcclient, execpt that the commands should change
58 * less often, and the fucntionality should be sane (the user is not
59 * expected to know a rid/sid before they conduct an operation etc.)
61 * @todo Perhaps eventually these should be split out into a number
62 * of files, as this could get quite big.
67 * Many of the RPC functions need the domain sid. This function gets
68 * it at the start of every run
70 * @param cli A cli_state already connected to the remote machine
72 * @return The Domain SID of the remote machine.
75 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
76 struct dom_sid
**domain_sid
,
77 const char **domain_name
)
79 struct rpc_pipe_client
*lsa_pipe
= NULL
;
80 struct policy_handle pol
;
81 NTSTATUS status
, result
;
82 union lsa_PolicyInformation
*info
= NULL
;
83 struct dcerpc_binding_handle
*b
;
85 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
87 if (!NT_STATUS_IS_OK(status
)) {
88 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
92 b
= lsa_pipe
->binding_handle
;
94 status
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
95 SEC_FLAG_MAXIMUM_ALLOWED
,
97 if (!NT_STATUS_IS_OK(status
)) {
98 d_fprintf(stderr
, "open_policy %s: %s\n",
104 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
106 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
109 if (!NT_STATUS_IS_OK(status
)) {
110 d_fprintf(stderr
, "lsaquery %s: %s\n",
115 if (!NT_STATUS_IS_OK(result
)) {
116 d_fprintf(stderr
, "lsaquery %s: %s\n",
122 *domain_name
= info
->account_domain
.name
.string
;
123 *domain_sid
= info
->account_domain
.sid
;
125 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
126 TALLOC_FREE(lsa_pipe
);
132 * Run a single RPC command, from start to finish.
134 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
135 * @param conn_flag a NET_FLAG_ combination. Passed to
136 * net_make_ipc_connection.
137 * @param argc Standard main() style argc.
138 * @param argv Standard main() style argv. Initial components are already
140 * @return A shell status integer (0 for success).
143 int run_rpc_command(struct net_context
*c
,
144 struct cli_state
*cli_arg
,
145 const struct ndr_interface_table
*table
,
151 struct cli_state
*cli
= NULL
;
152 struct rpc_pipe_client
*pipe_hnd
= NULL
;
155 struct dom_sid
*domain_sid
;
156 const char *domain_name
;
159 /* make use of cli_state handed over as an argument, if possible */
161 nt_status
= net_make_ipc_connection(c
, conn_flags
, &cli
);
162 if (!NT_STATUS_IS_OK(nt_status
)) {
163 DEBUG(1, ("failed to make ipc connection: %s\n",
164 nt_errstr(nt_status
)));
177 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
178 DEBUG(0, ("talloc_init() failed\n"));
182 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
184 if (!NT_STATUS_IS_OK(nt_status
)) {
188 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
189 if (lp_client_schannel()
190 && (ndr_syntax_id_equal(&table
->syntax_id
,
191 &ndr_table_netlogon
.syntax_id
))) {
192 /* Always try and create an schannel netlogon pipe. */
193 nt_status
= cli_rpc_pipe_open_schannel(
194 cli
, &table
->syntax_id
, NCACN_NP
,
195 DCERPC_AUTH_LEVEL_PRIVACY
, domain_name
,
197 if (!NT_STATUS_IS_OK(nt_status
)) {
198 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
199 nt_errstr(nt_status
) ));
203 if (conn_flags
& NET_FLAGS_SEAL
) {
204 nt_status
= cli_rpc_pipe_open_generic_auth(
206 (conn_flags
& NET_FLAGS_TCP
) ?
207 NCACN_IP_TCP
: NCACN_NP
,
208 DCERPC_AUTH_TYPE_NTLMSSP
,
209 DCERPC_AUTH_LEVEL_PRIVACY
,
210 smbXcli_conn_remote_name(cli
->conn
),
211 lp_workgroup(), c
->opt_user_name
,
212 c
->opt_password
, &pipe_hnd
);
214 nt_status
= cli_rpc_pipe_open_noauth(
215 cli
, &table
->syntax_id
,
218 if (!NT_STATUS_IS_OK(nt_status
)) {
219 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
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
,
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 smbXcli_conn_remote_name(cli
->conn
),
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
,
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
,
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
,
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
, rpc_sh_user_str_edit
,
1492 N_("Show/Set a user's full name") },
1494 { "homedir", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1495 N_("Show/Set a user's home directory") },
1497 { "homedrive", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1498 N_("Show/Set a user's home drive") },
1500 { "logonscript", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1501 N_("Show/Set a user's logon script") },
1503 { "profilepath", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1504 N_("Show/Set a user's profile path") },
1506 { "description", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1507 N_("Show/Set a user's description") },
1509 { "disabled", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1510 N_("Show/Set whether a user is disabled") },
1512 { "autolock", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1513 N_("Show/Set whether a user locked out") },
1515 { "pwnotreq", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1516 N_("Show/Set whether a user does not need a password") },
1518 { "pwnoexp", NULL
, &ndr_table_samr
, 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
, rpc_sh_user_list
,
1534 N_("List available users") },
1536 { "info", NULL
, &ndr_table_samr
, rpc_sh_user_info
,
1537 N_("List the domain groups a user is member of") },
1539 { "show", NULL
, &ndr_table_samr
, 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]);
1661 if (group_rids
.count
!= 1) {
1662 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1665 if (name_types
.count
!= 1) {
1666 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1670 switch (name_types
.ids
[0])
1672 case SID_NAME_DOM_GRP
:
1673 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
1675 MAXIMUM_ALLOWED_ACCESS
,
1679 if (!NT_STATUS_IS_OK(status
)) {
1680 d_fprintf(stderr
, _("Request open_group failed"));
1684 if (!NT_STATUS_IS_OK(result
)) {
1686 d_fprintf(stderr
, _("Request open_group failed"));
1690 group_rid
= group_rids
.ids
[0];
1692 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
1696 if (!NT_STATUS_IS_OK(status
)) {
1698 _("Unable to query group members of %s"),
1703 if (!NT_STATUS_IS_OK(result
)) {
1706 _("Unable to query group members of %s"),
1711 if (c
->opt_verbose
) {
1713 _("Domain Group %s (rid: %d) has %d members\n"),
1714 argv
[0],group_rid
, rids
->count
);
1717 /* Check if group is anyone's primary group */
1718 for (i
= 0; i
< rids
->count
; i
++)
1720 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1722 MAXIMUM_ALLOWED_ACCESS
,
1726 if (!NT_STATUS_IS_OK(status
)) {
1728 _("Unable to open group member %d\n"),
1733 if (!NT_STATUS_IS_OK(result
)) {
1736 _("Unable to open group member %d\n"),
1741 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1746 if (!NT_STATUS_IS_OK(status
)) {
1748 _("Unable to lookup userinfo for group "
1754 if (!NT_STATUS_IS_OK(result
)) {
1757 _("Unable to lookup userinfo for group "
1763 if (info
->info21
.primary_gid
== group_rid
) {
1764 if (c
->opt_verbose
) {
1765 d_printf(_("Group is primary group "
1767 info
->info21
.account_name
.string
);
1769 group_is_primary
= true;
1772 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1775 if (group_is_primary
) {
1776 d_fprintf(stderr
, _("Unable to delete group because "
1777 "some of it's members have it as primary "
1779 status
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1783 /* remove all group members */
1784 for (i
= 0; i
< rids
->count
; i
++)
1787 d_printf(_("Remove group member %d..."),
1789 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
1793 if (!NT_STATUS_IS_OK(status
)) {
1797 if (NT_STATUS_IS_OK(result
)) {
1799 d_printf(_("ok\n"));
1802 d_printf("%s\n", _("failed"));
1807 status
= dcerpc_samr_DeleteDomainGroup(b
, mem_ctx
,
1810 if (!NT_STATUS_IS_OK(status
)) {
1817 /* removing a local group is easier... */
1818 case SID_NAME_ALIAS
:
1819 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
1821 MAXIMUM_ALLOWED_ACCESS
,
1825 if (!NT_STATUS_IS_OK(status
)) {
1826 d_fprintf(stderr
, _("Request open_alias failed\n"));
1829 if (!NT_STATUS_IS_OK(result
)) {
1831 d_fprintf(stderr
, _("Request open_alias failed\n"));
1835 status
= dcerpc_samr_DeleteDomAlias(b
, mem_ctx
,
1838 if (!NT_STATUS_IS_OK(status
)) {
1846 d_fprintf(stderr
, _("%s is of type %s. This command is only "
1847 "for deleting local or global groups\n"),
1848 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1849 status
= NT_STATUS_UNSUCCESSFUL
;
1853 if (NT_STATUS_IS_OK(status
)) {
1855 d_printf(_("Deleted %s '%s'\n"),
1856 sid_type_lookup(name_types
.ids
[0]), argv
[0]);
1858 d_fprintf(stderr
, _("Deleting of %s failed: %s\n"), argv
[0],
1859 get_friendly_nt_error_msg(status
));
1867 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1869 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
1870 rpc_group_delete_internals
, argc
,argv
);
1873 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1875 NET_API_STATUS status
;
1876 struct GROUP_INFO_1 info1
;
1877 uint32_t parm_error
= 0;
1879 if (argc
!= 1 || c
->display_usage
) {
1880 rpc_group_usage(c
, argc
, argv
);
1886 info1
.grpi1_name
= argv
[0];
1887 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1888 info1
.grpi1_comment
= c
->opt_comment
;
1891 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1895 _("Failed to add group '%s' with error: %s.\n"),
1896 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1900 d_printf(_("Added group '%s'.\n"), argv
[0]);
1906 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1908 NET_API_STATUS status
;
1909 struct LOCALGROUP_INFO_1 info1
;
1910 uint32_t parm_error
= 0;
1912 if (argc
!= 1 || c
->display_usage
) {
1913 rpc_group_usage(c
, argc
, argv
);
1919 info1
.lgrpi1_name
= argv
[0];
1920 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1921 info1
.lgrpi1_comment
= c
->opt_comment
;
1924 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1928 _("Failed to add alias '%s' with error: %s.\n"),
1929 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1933 d_printf(_("Added alias '%s'.\n"), argv
[0]);
1939 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1941 if (c
->opt_localgroup
)
1942 return rpc_alias_add_internals(c
, argc
, argv
);
1944 return rpc_group_add_internals(c
, argc
, argv
);
1947 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1948 TALLOC_CTX
*mem_ctx
,
1950 struct dom_sid
*sid
,
1951 enum lsa_SidType
*type
)
1953 struct dom_sid
*sids
= NULL
;
1954 enum lsa_SidType
*types
= NULL
;
1955 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1956 struct policy_handle lsa_pol
;
1957 NTSTATUS status
, result
;
1958 struct dcerpc_binding_handle
*b
;
1960 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1962 if (!NT_STATUS_IS_OK(status
)) {
1966 b
= pipe_hnd
->binding_handle
;
1968 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1969 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1971 if (!NT_STATUS_IS_OK(status
)) {
1975 status
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1976 &name
, NULL
, 1, &sids
, &types
);
1978 if (NT_STATUS_IS_OK(status
)) {
1979 sid_copy(sid
, &sids
[0]);
1983 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1987 TALLOC_FREE(pipe_hnd
);
1990 if (!NT_STATUS_IS_OK(status
) && (strncasecmp_m(name
, "S-", 2) == 0)) {
1992 /* Try as S-1-5-whatever */
1994 struct dom_sid tmp_sid
;
1996 if (string_to_sid(&tmp_sid
, name
)) {
1997 sid_copy(sid
, &tmp_sid
);
1998 *type
= SID_NAME_UNKNOWN
;
1999 status
= NT_STATUS_OK
;
2006 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
2007 TALLOC_CTX
*mem_ctx
,
2008 const struct dom_sid
*group_sid
,
2011 struct policy_handle connect_pol
, domain_pol
;
2012 NTSTATUS status
, result
;
2014 struct policy_handle group_pol
;
2015 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2017 struct samr_Ids rids
, rid_types
;
2018 struct lsa_String lsa_acct_name
;
2022 sid_copy(&sid
, group_sid
);
2024 if (!sid_split_rid(&sid
, &group_rid
)) {
2025 return NT_STATUS_UNSUCCESSFUL
;
2028 /* Get sam policy handle */
2029 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2031 MAXIMUM_ALLOWED_ACCESS
,
2034 if (!NT_STATUS_IS_OK(status
)) {
2037 if (!NT_STATUS_IS_OK(result
)) {
2041 /* Get domain policy handle */
2042 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2044 MAXIMUM_ALLOWED_ACCESS
,
2048 if (!NT_STATUS_IS_OK(status
)) {
2051 if (!NT_STATUS_IS_OK(result
)) {
2055 init_lsa_String(&lsa_acct_name
, member
);
2057 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2064 if (!NT_STATUS_IS_OK(status
)) {
2065 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2070 if (!NT_STATUS_IS_OK(result
)) {
2072 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2076 if (rids
.count
!= 1) {
2077 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
2080 if (rid_types
.count
!= 1) {
2081 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
2085 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2087 MAXIMUM_ALLOWED_ACCESS
,
2091 if (!NT_STATUS_IS_OK(status
)) {
2095 if (!NT_STATUS_IS_OK(result
)) {
2100 status
= dcerpc_samr_AddGroupMember(b
, mem_ctx
,
2103 0x0005, /* unknown flags */
2105 if (!NT_STATUS_IS_OK(status
)) {
2112 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2116 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2117 TALLOC_CTX
*mem_ctx
,
2118 const struct dom_sid
*alias_sid
,
2121 struct policy_handle connect_pol
, domain_pol
;
2122 NTSTATUS status
, result
;
2124 struct policy_handle alias_pol
;
2125 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2127 struct dom_sid member_sid
;
2128 enum lsa_SidType member_type
;
2132 sid_copy(&sid
, alias_sid
);
2134 if (!sid_split_rid(&sid
, &alias_rid
)) {
2135 return NT_STATUS_UNSUCCESSFUL
;
2138 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2139 member
, &member_sid
, &member_type
);
2141 if (!NT_STATUS_IS_OK(result
)) {
2142 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2147 /* Get sam policy handle */
2148 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2150 MAXIMUM_ALLOWED_ACCESS
,
2153 if (!NT_STATUS_IS_OK(status
)) {
2156 if (!NT_STATUS_IS_OK(result
)) {
2161 /* Get domain policy handle */
2162 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2164 MAXIMUM_ALLOWED_ACCESS
,
2168 if (!NT_STATUS_IS_OK(status
)) {
2171 if (!NT_STATUS_IS_OK(result
)) {
2176 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2178 MAXIMUM_ALLOWED_ACCESS
,
2182 if (!NT_STATUS_IS_OK(status
)) {
2185 if (!NT_STATUS_IS_OK(result
)) {
2189 status
= dcerpc_samr_AddAliasMember(b
, mem_ctx
,
2193 if (!NT_STATUS_IS_OK(status
)) {
2200 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2204 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
2205 const struct dom_sid
*domain_sid
,
2206 const char *domain_name
,
2207 struct cli_state
*cli
,
2208 struct rpc_pipe_client
*pipe_hnd
,
2209 TALLOC_CTX
*mem_ctx
,
2213 struct dom_sid group_sid
;
2214 enum lsa_SidType group_type
;
2216 if (argc
!= 2 || c
->display_usage
) {
2219 _("net rpc group addmem <group> <member>\n"
2220 " Add a member to a group\n"
2221 " group\tGroup to add member to\n"
2222 " member\tMember to add to group\n"));
2223 return NT_STATUS_UNSUCCESSFUL
;
2226 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2227 &group_sid
, &group_type
))) {
2228 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2230 return NT_STATUS_UNSUCCESSFUL
;
2233 if (group_type
== SID_NAME_DOM_GRP
) {
2234 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2235 &group_sid
, argv
[1]);
2237 if (!NT_STATUS_IS_OK(result
)) {
2238 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2239 argv
[1], argv
[0], nt_errstr(result
));
2244 if (group_type
== SID_NAME_ALIAS
) {
2245 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2246 &group_sid
, argv
[1]);
2248 if (!NT_STATUS_IS_OK(result
)) {
2249 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2250 argv
[1], argv
[0], nt_errstr(result
));
2255 d_fprintf(stderr
, _("Can only add members to global or local groups "
2256 "which %s is not\n"), argv
[0]);
2258 return NT_STATUS_UNSUCCESSFUL
;
2261 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2263 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2264 rpc_group_addmem_internals
,
2268 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2269 struct rpc_pipe_client
*pipe_hnd
,
2270 TALLOC_CTX
*mem_ctx
,
2271 const struct dom_sid
*group_sid
,
2274 struct policy_handle connect_pol
, domain_pol
;
2275 NTSTATUS status
, result
;
2277 struct policy_handle group_pol
;
2278 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2280 struct samr_Ids rids
, rid_types
;
2281 struct lsa_String lsa_acct_name
;
2285 sid_copy(&sid
, group_sid
);
2287 if (!sid_split_rid(&sid
, &group_rid
))
2288 return NT_STATUS_UNSUCCESSFUL
;
2290 /* Get sam policy handle */
2291 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2293 MAXIMUM_ALLOWED_ACCESS
,
2296 if (!NT_STATUS_IS_OK(status
)) {
2299 if (!NT_STATUS_IS_OK(result
)) {
2304 /* Get domain policy handle */
2305 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2307 MAXIMUM_ALLOWED_ACCESS
,
2311 if (!NT_STATUS_IS_OK(status
)) {
2314 if (!NT_STATUS_IS_OK(result
)) {
2318 init_lsa_String(&lsa_acct_name
, member
);
2320 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2327 if (!NT_STATUS_IS_OK(status
)) {
2328 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2333 if (!NT_STATUS_IS_OK(result
)) {
2335 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2339 if (rids
.count
!= 1) {
2340 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
2343 if (rid_types
.count
!= 1) {
2344 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
2348 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2350 MAXIMUM_ALLOWED_ACCESS
,
2354 if (!NT_STATUS_IS_OK(status
)) {
2357 if (!NT_STATUS_IS_OK(result
)) {
2362 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
2366 if (!NT_STATUS_IS_OK(status
)) {
2372 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2376 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2377 TALLOC_CTX
*mem_ctx
,
2378 const struct dom_sid
*alias_sid
,
2381 struct policy_handle connect_pol
, domain_pol
;
2382 NTSTATUS status
, result
;
2384 struct policy_handle alias_pol
;
2385 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2387 struct dom_sid member_sid
;
2388 enum lsa_SidType member_type
;
2392 sid_copy(&sid
, alias_sid
);
2394 if (!sid_split_rid(&sid
, &alias_rid
))
2395 return NT_STATUS_UNSUCCESSFUL
;
2397 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2398 member
, &member_sid
, &member_type
);
2400 if (!NT_STATUS_IS_OK(result
)) {
2401 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2406 /* Get sam policy handle */
2407 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2409 MAXIMUM_ALLOWED_ACCESS
,
2412 if (!NT_STATUS_IS_OK(status
)) {
2415 if (!NT_STATUS_IS_OK(result
)) {
2420 /* Get domain policy handle */
2421 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2423 MAXIMUM_ALLOWED_ACCESS
,
2427 if (!NT_STATUS_IS_OK(status
)) {
2430 if (!NT_STATUS_IS_OK(result
)) {
2435 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2437 MAXIMUM_ALLOWED_ACCESS
,
2441 if (!NT_STATUS_IS_OK(status
)) {
2445 if (!NT_STATUS_IS_OK(result
)) {
2449 status
= dcerpc_samr_DeleteAliasMember(b
, mem_ctx
,
2454 if (!NT_STATUS_IS_OK(status
)) {
2461 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2465 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2466 const struct dom_sid
*domain_sid
,
2467 const char *domain_name
,
2468 struct cli_state
*cli
,
2469 struct rpc_pipe_client
*pipe_hnd
,
2470 TALLOC_CTX
*mem_ctx
,
2474 struct dom_sid group_sid
;
2475 enum lsa_SidType group_type
;
2477 if (argc
!= 2 || c
->display_usage
) {
2480 _("net rpc group delmem <group> <member>\n"
2481 " Delete a member from a group\n"
2482 " group\tGroup to delete member from\n"
2483 " member\tMember to delete from group\n"));
2484 return NT_STATUS_UNSUCCESSFUL
;
2487 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2488 &group_sid
, &group_type
))) {
2489 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2491 return NT_STATUS_UNSUCCESSFUL
;
2494 if (group_type
== SID_NAME_DOM_GRP
) {
2495 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2496 &group_sid
, argv
[1]);
2498 if (!NT_STATUS_IS_OK(result
)) {
2499 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2500 argv
[1], argv
[0], nt_errstr(result
));
2505 if (group_type
== SID_NAME_ALIAS
) {
2506 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2507 &group_sid
, argv
[1]);
2509 if (!NT_STATUS_IS_OK(result
)) {
2510 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2511 argv
[1], argv
[0], nt_errstr(result
));
2516 d_fprintf(stderr
, _("Can only delete members from global or local "
2517 "groups which %s is not\n"), argv
[0]);
2519 return NT_STATUS_UNSUCCESSFUL
;
2522 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2524 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2525 rpc_group_delmem_internals
,
2530 * List groups on a remote RPC server.
2532 * All parameters are provided by the run_rpc_command function, except for
2533 * argc, argv which are passes through.
2535 * @param domain_sid The domain sid acquired from the remote server.
2536 * @param cli A cli_state connected to the server.
2537 * @param mem_ctx Talloc context, destroyed on completion of the function.
2538 * @param argc Standard main() style argc.
2539 * @param argv Standard main() style argv. Initial components are already
2542 * @return Normal NTSTATUS return.
2545 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2546 const struct dom_sid
*domain_sid
,
2547 const char *domain_name
,
2548 struct cli_state
*cli
,
2549 struct rpc_pipe_client
*pipe_hnd
,
2550 TALLOC_CTX
*mem_ctx
,
2554 struct policy_handle connect_pol
, domain_pol
;
2555 NTSTATUS status
, result
;
2556 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2557 struct samr_SamArray
*groups
= NULL
;
2558 bool global
= false;
2560 bool builtin
= false;
2561 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2563 if (c
->display_usage
) {
2566 _("net rpc group list [global] [local] [builtin]\n"
2567 " List groups on RPC server\n"
2568 " global\tList global groups\n"
2569 " local\tList local groups\n"
2570 " builtin\tList builtin groups\n"
2571 " If none of global, local or builtin is "
2572 "specified, all three options are considered "
2574 return NT_STATUS_OK
;
2583 for (i
=0; i
<argc
; i
++) {
2584 if (strequal(argv
[i
], "global"))
2587 if (strequal(argv
[i
], "local"))
2590 if (strequal(argv
[i
], "builtin"))
2594 /* Get sam policy handle */
2596 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2598 MAXIMUM_ALLOWED_ACCESS
,
2601 if (!NT_STATUS_IS_OK(status
)) {
2604 if (!NT_STATUS_IS_OK(result
)) {
2609 /* Get domain policy handle */
2611 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2613 MAXIMUM_ALLOWED_ACCESS
,
2614 discard_const_p(struct dom_sid2
, domain_sid
),
2617 if (!NT_STATUS_IS_OK(status
)) {
2620 if (!NT_STATUS_IS_OK(result
)) {
2625 /* Query domain groups */
2626 if (c
->opt_long_list_entries
)
2627 d_printf(_("\nGroup name Comment"
2628 "\n-----------------------------\n"));
2630 uint32_t max_size
, total_size
, returned_size
;
2631 union samr_DispInfo info
;
2635 dcerpc_get_query_dispinfo_params(
2636 loop_count
, &max_entries
, &max_size
);
2638 status
= dcerpc_samr_QueryDisplayInfo(b
, mem_ctx
,
2648 if (!NT_STATUS_IS_OK(status
)) {
2651 num_entries
= info
.info3
.count
;
2652 start_idx
+= info
.info3
.count
;
2654 if (!NT_STATUS_IS_OK(result
) &&
2655 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2658 for (i
= 0; i
< num_entries
; i
++) {
2660 const char *group
= NULL
;
2661 const char *desc
= NULL
;
2663 group
= info
.info3
.entries
[i
].account_name
.string
;
2664 desc
= info
.info3
.entries
[i
].description
.string
;
2666 if (c
->opt_long_list_entries
)
2667 printf("%-21.21s %-50.50s\n",
2670 printf("%s\n", group
);
2672 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2673 /* query domain aliases */
2678 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2685 if (!NT_STATUS_IS_OK(status
)) {
2688 if (!NT_STATUS_IS_OK(result
) &&
2689 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2692 for (i
= 0; i
< num_entries
; i
++) {
2694 const char *description
= NULL
;
2696 if (c
->opt_long_list_entries
) {
2698 struct policy_handle alias_pol
;
2699 union samr_AliasInfo
*info
= NULL
;
2702 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2705 groups
->entries
[i
].idx
,
2708 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2709 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2714 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2715 status
= dcerpc_samr_Close(b
, mem_ctx
,
2718 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2719 description
= info
->description
.string
;
2725 if (description
!= NULL
) {
2726 printf("%-21.21s %-50.50s\n",
2727 groups
->entries
[i
].name
.string
,
2730 printf("%s\n", groups
->entries
[i
].name
.string
);
2733 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2734 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
2735 /* Get builtin policy handle */
2737 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2739 MAXIMUM_ALLOWED_ACCESS
,
2740 discard_const_p(struct dom_sid2
, &global_sid_Builtin
),
2743 if (!NT_STATUS_IS_OK(status
)) {
2746 if (!NT_STATUS_IS_OK(result
)) {
2751 /* query builtin aliases */
2754 if (!builtin
) break;
2756 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2763 if (!NT_STATUS_IS_OK(status
)) {
2766 if (!NT_STATUS_IS_OK(result
) &&
2767 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
2772 for (i
= 0; i
< num_entries
; i
++) {
2774 const char *description
= NULL
;
2776 if (c
->opt_long_list_entries
) {
2778 struct policy_handle alias_pol
;
2779 union samr_AliasInfo
*info
= NULL
;
2782 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2785 groups
->entries
[i
].idx
,
2788 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2789 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2794 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2795 status
= dcerpc_samr_Close(b
, mem_ctx
,
2798 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2799 description
= info
->description
.string
;
2805 if (description
!= NULL
) {
2806 printf("%-21.21s %-50.50s\n",
2807 groups
->entries
[i
].name
.string
,
2810 printf("%s\n", groups
->entries
[i
].name
.string
);
2813 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2821 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2823 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2824 rpc_group_list_internals
,
2828 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2829 struct rpc_pipe_client
*pipe_hnd
,
2830 TALLOC_CTX
*mem_ctx
,
2831 const char *domain_name
,
2832 const struct dom_sid
*domain_sid
,
2833 struct policy_handle
*domain_pol
,
2836 NTSTATUS result
, status
;
2837 struct policy_handle group_pol
;
2838 uint32 num_members
, *group_rids
;
2840 struct samr_RidAttrArray
*rids
= NULL
;
2841 struct lsa_Strings names
;
2842 struct samr_Ids types
;
2843 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2846 sid_to_fstring(sid_str
, domain_sid
);
2848 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2850 MAXIMUM_ALLOWED_ACCESS
,
2854 if (!NT_STATUS_IS_OK(status
)) {
2857 if (!NT_STATUS_IS_OK(result
)) {
2861 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
2865 if (!NT_STATUS_IS_OK(status
)) {
2868 if (!NT_STATUS_IS_OK(result
)) {
2872 num_members
= rids
->count
;
2873 group_rids
= rids
->rids
;
2875 while (num_members
> 0) {
2876 int this_time
= 512;
2878 if (num_members
< this_time
)
2879 this_time
= num_members
;
2881 status
= dcerpc_samr_LookupRids(b
, mem_ctx
,
2888 if (!NT_STATUS_IS_OK(status
)) {
2891 if (!NT_STATUS_IS_OK(result
)) {
2895 /* We only have users as members, but make the output
2896 the same as the output of alias members */
2898 for (i
= 0; i
< this_time
; i
++) {
2900 if (c
->opt_long_list_entries
) {
2901 printf("%s-%d %s\\%s %d\n", sid_str
,
2902 group_rids
[i
], domain_name
,
2903 names
.names
[i
].string
,
2906 printf("%s\\%s\n", domain_name
,
2907 names
.names
[i
].string
);
2911 num_members
-= this_time
;
2915 return NT_STATUS_OK
;
2918 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2919 struct rpc_pipe_client
*pipe_hnd
,
2920 TALLOC_CTX
*mem_ctx
,
2921 struct policy_handle
*domain_pol
,
2924 NTSTATUS result
, status
;
2925 struct rpc_pipe_client
*lsa_pipe
;
2926 struct policy_handle alias_pol
, lsa_pol
;
2928 struct dom_sid
*alias_sids
;
2931 enum lsa_SidType
*types
;
2933 struct lsa_SidArray sid_array
;
2934 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2936 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2938 MAXIMUM_ALLOWED_ACCESS
,
2942 if (!NT_STATUS_IS_OK(status
)) {
2945 if (!NT_STATUS_IS_OK(result
)) {
2949 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
2953 if (!NT_STATUS_IS_OK(status
)) {
2954 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2957 if (!NT_STATUS_IS_OK(result
)) {
2958 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2962 num_members
= sid_array
.num_sids
;
2964 if (num_members
== 0) {
2965 return NT_STATUS_OK
;
2968 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2969 &ndr_table_lsarpc
.syntax_id
,
2971 if (!NT_STATUS_IS_OK(result
)) {
2972 d_fprintf(stderr
, _("Couldn't open LSA pipe. Error was %s\n"),
2973 nt_errstr(result
) );
2977 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2978 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
2980 if (!NT_STATUS_IS_OK(result
)) {
2981 d_fprintf(stderr
, _("Couldn't open LSA policy handle\n"));
2982 TALLOC_FREE(lsa_pipe
);
2986 alias_sids
= talloc_zero_array(mem_ctx
, struct dom_sid
, num_members
);
2988 d_fprintf(stderr
, _("Out of memory\n"));
2989 TALLOC_FREE(lsa_pipe
);
2990 return NT_STATUS_NO_MEMORY
;
2993 for (i
=0; i
<num_members
; i
++) {
2994 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2997 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2998 num_members
, alias_sids
,
2999 &domains
, &names
, &types
);
3001 if (!NT_STATUS_IS_OK(result
) &&
3002 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
3003 d_fprintf(stderr
, _("Couldn't lookup SIDs\n"));
3004 TALLOC_FREE(lsa_pipe
);
3008 for (i
= 0; i
< num_members
; i
++) {
3010 sid_to_fstring(sid_str
, &alias_sids
[i
]);
3012 if (c
->opt_long_list_entries
) {
3013 printf("%s %s\\%s %d\n", sid_str
,
3014 domains
[i
] ? domains
[i
] : _("*unknown*"),
3015 names
[i
] ? names
[i
] : _("*unknown*"), types
[i
]);
3018 printf("%s\\%s\n", domains
[i
], names
[i
]);
3020 printf("%s\n", sid_str
);
3024 TALLOC_FREE(lsa_pipe
);
3025 return NT_STATUS_OK
;
3028 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
3029 const struct dom_sid
*domain_sid
,
3030 const char *domain_name
,
3031 struct cli_state
*cli
,
3032 struct rpc_pipe_client
*pipe_hnd
,
3033 TALLOC_CTX
*mem_ctx
,
3037 NTSTATUS result
, status
;
3038 struct policy_handle connect_pol
, domain_pol
;
3039 struct samr_Ids rids
, rid_types
;
3040 struct lsa_String lsa_acct_name
;
3041 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3043 /* Get sam policy handle */
3045 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
3047 MAXIMUM_ALLOWED_ACCESS
,
3050 if (!NT_STATUS_IS_OK(status
)) {
3053 if (!NT_STATUS_IS_OK(result
)) {
3057 /* Get domain policy handle */
3059 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3061 MAXIMUM_ALLOWED_ACCESS
,
3062 discard_const_p(struct dom_sid2
, domain_sid
),
3065 if (!NT_STATUS_IS_OK(status
)) {
3068 if (!NT_STATUS_IS_OK(result
)) {
3072 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
3074 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3081 if (!NT_STATUS_IS_OK(status
)) {
3085 if (!NT_STATUS_IS_OK(result
)) {
3087 /* Ok, did not find it in the global sam, try with builtin */
3089 struct dom_sid sid_Builtin
;
3091 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
3093 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
3095 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3097 MAXIMUM_ALLOWED_ACCESS
,
3101 if (!NT_STATUS_IS_OK(status
)) {
3104 if (!NT_STATUS_IS_OK(result
)) {
3105 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3110 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3117 if (!NT_STATUS_IS_OK(status
)) {
3120 if (!NT_STATUS_IS_OK(result
)) {
3121 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3127 if (rids
.count
!= 1) {
3128 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3130 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3132 if (rid_types
.count
!= 1) {
3133 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3135 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3139 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
3140 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
3141 domain_sid
, &domain_pol
,
3145 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
3146 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
3150 return NT_STATUS_NO_SUCH_GROUP
;
3153 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
3155 if (argc
!= 1 || c
->display_usage
) {
3156 return rpc_group_usage(c
, argc
, argv
);
3159 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
3160 rpc_group_members_internals
,
3164 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
3166 NET_API_STATUS status
;
3167 struct GROUP_INFO_0 g0
;
3171 d_printf(_("Usage:\n"));
3172 d_printf("net rpc group rename group newname\n");
3176 g0
.grpi0_name
= argv
[1];
3178 status
= NetGroupSetInfo(c
->opt_host
,
3185 d_fprintf(stderr
, _("Renaming group %s failed with: %s\n"),
3186 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
3194 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
3196 if (argc
!= 2 || c
->display_usage
) {
3197 return rpc_group_usage(c
, argc
, argv
);
3200 return rpc_group_rename_internals(c
, argc
, argv
);
3204 * 'net rpc group' entrypoint.
3205 * @param argc Standard main() style argc.
3206 * @param argv Standard main() style argv. Initial components are already
3210 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
3212 NET_API_STATUS status
;
3214 struct functable func
[] = {
3219 N_("Create specified group"),
3220 N_("net rpc group add\n"
3221 " Create specified group")
3227 N_("Delete specified group"),
3228 N_("net rpc group delete\n"
3229 " Delete specified group")
3235 N_("Add member to group"),
3236 N_("net rpc group addmem\n"
3237 " Add member to group")
3243 N_("Remove member from group"),
3244 N_("net rpc group delmem\n"
3245 " Remove member from group")
3252 N_("net rpc group list\n"
3259 N_("List group members"),
3260 N_("net rpc group members\n"
3261 " List group members")
3268 N_("net rpc group rename\n"
3271 {NULL
, NULL
, 0, NULL
, NULL
}
3274 status
= libnetapi_net_init(&c
->netapi_ctx
);
3278 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
3279 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
3280 if (c
->opt_kerberos
) {
3281 libnetapi_set_use_kerberos(c
->netapi_ctx
);
3285 if (c
->display_usage
) {
3286 d_printf(_("Usage:\n"));
3287 d_printf(_("net rpc group\n"
3288 " Alias for net rpc group list global "
3289 "local builtin\n"));
3290 net_display_usage_from_functable(func
);
3294 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
3295 rpc_group_list_internals
,
3299 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
3302 /****************************************************************************/
3304 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
3306 return net_share_usage(c
, argc
, argv
);
3310 * Add a share on a remote RPC server.
3312 * @param argc Standard main() style argc.
3313 * @param argv Standard main() style argv. Initial components are already
3316 * @return A shell status integer (0 for success).
3319 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
3321 NET_API_STATUS status
;
3324 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3325 uint32 num_users
=0, perms
=0;
3326 char *password
=NULL
; /* don't allow a share password */
3327 struct SHARE_INFO_2 i2
;
3328 uint32_t parm_error
= 0;
3330 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
3331 return rpc_share_usage(c
, argc
, argv
);
3334 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
3338 path
= strchr(sharename
, '=');
3345 i2
.shi2_netname
= sharename
;
3346 i2
.shi2_type
= type
;
3347 i2
.shi2_remark
= c
->opt_comment
;
3348 i2
.shi2_permissions
= perms
;
3349 i2
.shi2_max_uses
= c
->opt_maxusers
;
3350 i2
.shi2_current_uses
= num_users
;
3351 i2
.shi2_path
= path
;
3352 i2
.shi2_passwd
= password
;
3354 status
= NetShareAdd(c
->opt_host
,
3359 printf(_("NetShareAdd failed with: %s\n"),
3360 libnetapi_get_error_string(c
->netapi_ctx
, status
));
3367 * Delete a share on a remote RPC server.
3369 * @param domain_sid The domain sid acquired from the remote server.
3370 * @param argc Standard main() style argc.
3371 * @param argv Standard main() style argv. Initial components are already
3374 * @return A shell status integer (0 for success).
3376 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3378 if (argc
< 1 || c
->display_usage
) {
3379 return rpc_share_usage(c
, argc
, argv
);
3382 return NetShareDel(c
->opt_host
, argv
[0], 0);
3386 * Formatted print of share info
3388 * @param r pointer to SHARE_INFO_1 to format
3391 static void display_share_info_1(struct net_context
*c
,
3392 struct SHARE_INFO_1
*r
)
3394 if (c
->opt_long_list_entries
) {
3395 d_printf("%-12s %-8.8s %-50s\n",
3397 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
3400 d_printf("%s\n", r
->shi1_netname
);
3404 static WERROR
get_share_info(struct net_context
*c
,
3405 struct rpc_pipe_client
*pipe_hnd
,
3406 TALLOC_CTX
*mem_ctx
,
3410 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3414 union srvsvc_NetShareInfo info
;
3415 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3417 /* no specific share requested, enumerate all */
3420 uint32_t preferred_len
= 0xffffffff;
3421 uint32_t total_entries
= 0;
3422 uint32_t resume_handle
= 0;
3424 info_ctr
->level
= level
;
3426 status
= dcerpc_srvsvc_NetShareEnumAll(b
, mem_ctx
,
3433 if (!NT_STATUS_IS_OK(status
)) {
3434 return ntstatus_to_werror(status
);
3439 /* request just one share */
3440 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
3447 if (!NT_STATUS_IS_OK(status
)) {
3448 result
= ntstatus_to_werror(status
);
3452 if (!W_ERROR_IS_OK(result
)) {
3457 ZERO_STRUCTP(info_ctr
);
3459 info_ctr
->level
= level
;
3464 struct srvsvc_NetShareCtr1
*ctr1
;
3466 ctr1
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr1
);
3467 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3470 ctr1
->array
= info
.info1
;
3472 info_ctr
->ctr
.ctr1
= ctr1
;
3478 struct srvsvc_NetShareCtr2
*ctr2
;
3480 ctr2
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr2
);
3481 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3484 ctr2
->array
= info
.info2
;
3486 info_ctr
->ctr
.ctr2
= ctr2
;
3492 struct srvsvc_NetShareCtr502
*ctr502
;
3494 ctr502
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr502
);
3495 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3498 ctr502
->array
= info
.info502
;
3500 info_ctr
->ctr
.ctr502
= ctr502
;
3510 * 'net rpc share list' entrypoint.
3511 * @param argc Standard main() style argc.
3512 * @param argv Standard main() style argv. Initial components are already
3515 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3517 NET_API_STATUS status
;
3518 struct SHARE_INFO_1
*i1
= NULL
;
3519 uint32_t entries_read
= 0;
3520 uint32_t total_entries
= 0;
3521 uint32_t resume_handle
= 0;
3522 uint32_t i
, level
= 1;
3524 if (c
->display_usage
) {
3526 "net rpc share list\n"
3529 _("List shares on remote server"));
3533 status
= NetShareEnum(c
->opt_host
,
3535 (uint8_t **)(void *)&i1
,
3544 /* Display results */
3546 if (c
->opt_long_list_entries
) {
3548 "\nEnumerating shared resources (exports) on remote server:\n\n"
3549 "\nShare name Type Description\n"
3550 "---------- ---- -----------\n"));
3552 for (i
= 0; i
< entries_read
; i
++)
3553 display_share_info_1(c
, &i1
[i
]);
3558 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3562 status
= cli_tree_connect(cli
, netname
, "A:", "", 0);
3563 if (!NT_STATUS_IS_OK(status
)) {
3564 d_printf(_("skipping [%s]: not a file share.\n"), netname
);
3568 status
= cli_tdis(cli
);
3569 if (!NT_STATUS_IS_OK(status
)) {
3570 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status
));
3577 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3578 const char *netname
, uint32 type
)
3580 /* only support disk shares */
3581 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3582 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname
,
3587 /* skip builtin shares */
3588 /* FIXME: should print$ be added too ? */
3589 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3590 strequal(netname
,"global"))
3593 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3594 printf(_("excluding [%s]\n"), netname
);
3598 return check_share_availability(cli
, netname
);
3602 * Migrate shares from a remote RPC server to the local RPC server.
3604 * All parameters are provided by the run_rpc_command function, except for
3605 * argc, argv which are passed through.
3607 * @param domain_sid The domain sid acquired from the remote server.
3608 * @param cli A cli_state connected to the server.
3609 * @param mem_ctx Talloc context, destroyed on completion of the function.
3610 * @param argc Standard main() style argc.
3611 * @param argv Standard main() style argv. Initial components are already
3614 * @return Normal NTSTATUS return.
3617 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3618 const struct dom_sid
*domain_sid
,
3619 const char *domain_name
,
3620 struct cli_state
*cli
,
3621 struct rpc_pipe_client
*pipe_hnd
,
3622 TALLOC_CTX
*mem_ctx
,
3627 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3628 struct srvsvc_NetShareInfoCtr ctr_src
;
3630 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3631 struct cli_state
*cli_dst
= NULL
;
3632 uint32 level
= 502; /* includes secdesc */
3633 uint32_t parm_error
= 0;
3634 struct dcerpc_binding_handle
*b
;
3636 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3638 if (!W_ERROR_IS_OK(result
))
3641 /* connect destination PI_SRVSVC */
3642 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3643 &ndr_table_srvsvc
.syntax_id
);
3644 if (!NT_STATUS_IS_OK(nt_status
))
3647 b
= srvsvc_pipe
->binding_handle
;
3649 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3651 union srvsvc_NetShareInfo info
;
3652 struct srvsvc_NetShareInfo502 info502
=
3653 ctr_src
.ctr
.ctr502
->array
[i
];
3655 /* reset error-code */
3656 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3658 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3661 /* finally add the share on the dst server */
3663 printf(_("migrating: [%s], path: %s, comment: %s, without "
3665 info502
.name
, info502
.path
, info502
.comment
);
3667 info
.info502
= &info502
;
3669 nt_status
= dcerpc_srvsvc_NetShareAdd(b
, mem_ctx
,
3670 srvsvc_pipe
->desthost
,
3675 if (!NT_STATUS_IS_OK(nt_status
)) {
3676 printf(_("cannot add share: %s\n"),
3677 nt_errstr(nt_status
));
3680 if (W_ERROR_V(result
) == W_ERROR_V(WERR_FILE_EXISTS
)) {
3681 printf(_(" [%s] does already exist\n"),
3686 if (!W_ERROR_IS_OK(result
)) {
3687 nt_status
= werror_to_ntstatus(result
);
3688 printf(_("cannot add share: %s\n"),
3689 win_errstr(result
));
3695 nt_status
= NT_STATUS_OK
;
3699 cli_shutdown(cli_dst
);
3707 * Migrate shares from a RPC server to another.
3709 * @param argc Standard main() style argc.
3710 * @param argv Standard main() style argv. Initial components are already
3713 * @return A shell status integer (0 for success).
3715 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3718 if (c
->display_usage
) {
3720 "net rpc share migrate shares\n"
3723 _("Migrate shares to local server"));
3728 printf(_("no server to migrate\n"));
3732 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
3733 rpc_share_migrate_shares_internals
,
3740 * @param f file_info
3741 * @param mask current search mask
3742 * @param state arg-pointer
3745 static NTSTATUS
copy_fn(const char *mnt
, struct file_info
*f
,
3746 const char *mask
, void *state
)
3748 static NTSTATUS nt_status
;
3749 static struct copy_clistate
*local_state
;
3750 static fstring filename
, new_mask
;
3753 struct net_context
*c
;
3755 local_state
= (struct copy_clistate
*)state
;
3756 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3760 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3761 return NT_STATUS_OK
;
3763 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3766 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
3768 DEBUG(3,("got dir: %s\n", f
->name
));
3770 fstrcpy(dir
, local_state
->cwd
);
3772 fstrcat(dir
, f
->name
);
3774 switch (net_mode_share
)
3776 case NET_MODE_SHARE_MIGRATE
:
3777 /* create that directory */
3778 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3779 local_state
->cli_share_src
,
3780 local_state
->cli_share_dst
,
3782 c
->opt_acls
? true : false,
3783 c
->opt_attrs
? true : false,
3784 c
->opt_timestamps
? true:false,
3788 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3789 return NT_STATUS_INTERNAL_ERROR
;
3792 if (!NT_STATUS_IS_OK(nt_status
)) {
3793 printf(_("could not handle dir %s: %s\n"),
3794 dir
, nt_errstr(nt_status
));
3798 /* search below that directory */
3799 if (strlcpy(new_mask
, dir
, sizeof(new_mask
)) >= sizeof(new_mask
)) {
3800 return NT_STATUS_NO_MEMORY
;
3802 if (strlcat(new_mask
, "\\*", sizeof(new_mask
)) >= sizeof(new_mask
)) {
3803 return NT_STATUS_NO_MEMORY
;
3806 old_dir
= local_state
->cwd
;
3807 local_state
->cwd
= dir
;
3808 nt_status
= sync_files(local_state
, new_mask
);
3809 if (!NT_STATUS_IS_OK(nt_status
)) {
3810 printf(_("could not handle files\n"));
3812 local_state
->cwd
= old_dir
;
3819 fstrcpy(filename
, local_state
->cwd
);
3820 fstrcat(filename
, "\\");
3821 fstrcat(filename
, f
->name
);
3823 DEBUG(3,("got file: %s\n", filename
));
3825 switch (net_mode_share
)
3827 case NET_MODE_SHARE_MIGRATE
:
3828 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3829 local_state
->cli_share_src
,
3830 local_state
->cli_share_dst
,
3832 c
->opt_acls
? true : false,
3833 c
->opt_attrs
? true : false,
3834 c
->opt_timestamps
? true: false,
3838 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3840 return NT_STATUS_INTERNAL_ERROR
;
3843 if (!NT_STATUS_IS_OK(nt_status
))
3844 printf(_("could not handle file %s: %s\n"),
3845 filename
, nt_errstr(nt_status
));
3850 * sync files, can be called recursivly to list files
3851 * and then call copy_fn for each file
3853 * @param cp_clistate pointer to the copy_clistate we work with
3854 * @param mask the current search mask
3856 * @return Boolean result
3858 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3860 struct cli_state
*targetcli
;
3861 char *targetpath
= NULL
;
3864 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3866 status
= cli_resolve_path(talloc_tos(), "", NULL
,
3867 cp_clistate
->cli_share_src
,
3868 mask
, &targetcli
, &targetpath
);
3869 if (!NT_STATUS_IS_OK(status
)) {
3870 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3872 mask
, nt_errstr(status
));
3876 status
= cli_list(targetcli
, targetpath
, cp_clistate
->attribute
,
3877 copy_fn
, cp_clistate
);
3878 if (!NT_STATUS_IS_OK(status
)) {
3879 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3880 mask
, nt_errstr(status
));
3888 * Set the top level directory permissions before we do any further copies.
3889 * Should set up ACL inheritance.
3892 bool copy_top_level_perms(struct net_context
*c
,
3893 struct copy_clistate
*cp_clistate
,
3894 const char *sharename
)
3896 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3898 switch (net_mode_share
) {
3899 case NET_MODE_SHARE_MIGRATE
:
3900 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3901 nt_status
= net_copy_fileattr(c
,
3902 cp_clistate
->mem_ctx
,
3903 cp_clistate
->cli_share_src
,
3904 cp_clistate
->cli_share_dst
,
3906 c
->opt_acls
? true : false,
3907 c
->opt_attrs
? true : false,
3908 c
->opt_timestamps
? true: false,
3912 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3916 if (!NT_STATUS_IS_OK(nt_status
)) {
3917 printf(_("Could handle directory attributes for top level "
3918 "directory of share %s. Error %s\n"),
3919 sharename
, nt_errstr(nt_status
));
3927 * Sync all files inside a remote share to another share (over smb).
3929 * All parameters are provided by the run_rpc_command function, except for
3930 * argc, argv which are passed through.
3932 * @param domain_sid The domain sid acquired from the remote server.
3933 * @param cli A cli_state connected to the server.
3934 * @param mem_ctx Talloc context, destroyed on completion of the function.
3935 * @param argc Standard main() style argc.
3936 * @param argv Standard main() style argv. Initial components are already
3939 * @return Normal NTSTATUS return.
3942 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3943 const struct dom_sid
*domain_sid
,
3944 const char *domain_name
,
3945 struct cli_state
*cli
,
3946 struct rpc_pipe_client
*pipe_hnd
,
3947 TALLOC_CTX
*mem_ctx
,
3952 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3953 struct srvsvc_NetShareInfoCtr ctr_src
;
3956 struct copy_clistate cp_clistate
;
3957 bool got_src_share
= false;
3958 bool got_dst_share
= false;
3959 const char *mask
= "\\*";
3962 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3964 nt_status
= NT_STATUS_NO_MEMORY
;
3968 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3971 if (!W_ERROR_IS_OK(result
))
3974 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3976 struct srvsvc_NetShareInfo502 info502
=
3977 ctr_src
.ctr
.ctr502
->array
[i
];
3979 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3982 /* one might not want to mirror whole discs :) */
3983 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3984 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3989 switch (net_mode_share
)
3991 case NET_MODE_SHARE_MIGRATE
:
3995 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3999 printf(_(" [%s] files and directories %s ACLs, %s DOS "
4002 c
->opt_acls
? _("including") : _("without"),
4003 c
->opt_attrs
? _("including") : _("without"),
4004 c
->opt_timestamps
? _("(preserving timestamps)") : "");
4006 cp_clistate
.mem_ctx
= mem_ctx
;
4007 cp_clistate
.cli_share_src
= NULL
;
4008 cp_clistate
.cli_share_dst
= NULL
;
4009 cp_clistate
.cwd
= NULL
;
4010 cp_clistate
.attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
;
4013 /* open share source */
4014 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
4015 smbXcli_conn_remote_sockaddr(cli
->conn
),
4016 smbXcli_conn_remote_name(cli
->conn
),
4017 info502
.name
, "A:");
4018 if (!NT_STATUS_IS_OK(nt_status
))
4021 got_src_share
= true;
4023 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
4024 /* open share destination */
4025 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
4026 NULL
, dst
, info502
.name
, "A:");
4027 if (!NT_STATUS_IS_OK(nt_status
))
4030 got_dst_share
= true;
4033 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
4034 d_fprintf(stderr
, _("Could not handle the top level "
4035 "directory permissions for the "
4036 "share: %s\n"), info502
.name
);
4037 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4041 nt_status
= sync_files(&cp_clistate
, mask
);
4042 if (!NT_STATUS_IS_OK(nt_status
)) {
4043 d_fprintf(stderr
, _("could not handle files for share: "
4044 "%s\n"), info502
.name
);
4049 nt_status
= NT_STATUS_OK
;
4054 cli_shutdown(cp_clistate
.cli_share_src
);
4057 cli_shutdown(cp_clistate
.cli_share_dst
);
4064 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
4066 if (c
->display_usage
) {
4068 "net share migrate files\n"
4071 _("Migrate files to local server"));
4076 d_printf(_("no server to migrate\n"));
4080 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4081 rpc_share_migrate_files_internals
,
4086 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4088 * All parameters are provided by the run_rpc_command function, except for
4089 * argc, argv which are passed through.
4091 * @param domain_sid The domain sid acquired from the remote server.
4092 * @param cli A cli_state connected to the server.
4093 * @param mem_ctx Talloc context, destroyed on completion of the function.
4094 * @param argc Standard main() style argc.
4095 * @param argv Standard main() style argv. Initial components are already
4098 * @return Normal NTSTATUS return.
4101 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
4102 const struct dom_sid
*domain_sid
,
4103 const char *domain_name
,
4104 struct cli_state
*cli
,
4105 struct rpc_pipe_client
*pipe_hnd
,
4106 TALLOC_CTX
*mem_ctx
,
4111 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
4112 struct srvsvc_NetShareInfoCtr ctr_src
;
4113 union srvsvc_NetShareInfo info
;
4115 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
4116 struct cli_state
*cli_dst
= NULL
;
4117 uint32 level
= 502; /* includes secdesc */
4118 uint32_t parm_error
= 0;
4119 struct dcerpc_binding_handle
*b
;
4121 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
4124 if (!W_ERROR_IS_OK(result
))
4127 /* connect destination PI_SRVSVC */
4128 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
4129 &ndr_table_srvsvc
.syntax_id
);
4130 if (!NT_STATUS_IS_OK(nt_status
))
4133 b
= srvsvc_pipe
->binding_handle
;
4135 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
4137 struct srvsvc_NetShareInfo502 info502
=
4138 ctr_src
.ctr
.ctr502
->array
[i
];
4140 /* reset error-code */
4141 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4143 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
4146 printf(_("migrating: [%s], path: %s, comment: %s, including "
4148 info502
.name
, info502
.path
, info502
.comment
);
4151 display_sec_desc(info502
.sd_buf
.sd
);
4153 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4154 info
.info502
= &info502
;
4156 /* finally modify the share on the dst server */
4157 nt_status
= dcerpc_srvsvc_NetShareSetInfo(b
, mem_ctx
,
4158 srvsvc_pipe
->desthost
,
4164 if (!NT_STATUS_IS_OK(nt_status
)) {
4165 printf(_("cannot set share-acl: %s\n"),
4166 nt_errstr(nt_status
));
4169 if (!W_ERROR_IS_OK(result
)) {
4170 nt_status
= werror_to_ntstatus(result
);
4171 printf(_("cannot set share-acl: %s\n"),
4172 win_errstr(result
));
4178 nt_status
= NT_STATUS_OK
;
4182 cli_shutdown(cli_dst
);
4190 * Migrate share-acls from a RPC server to another.
4192 * @param argc Standard main() style argc.
4193 * @param argv Standard main() style argv. Initial components are already
4196 * @return A shell status integer (0 for success).
4198 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
4201 if (c
->display_usage
) {
4203 "net rpc share migrate security\n"
4206 _("Migrate share-acls to local server"));
4211 d_printf(_("no server to migrate\n"));
4215 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4216 rpc_share_migrate_security_internals
,
4221 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4222 * from one server to another.
4224 * @param argc Standard main() style argc.
4225 * @param argv Standard main() style argv. Initial components are already
4228 * @return A shell status integer (0 for success).
4231 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
4236 if (c
->display_usage
) {
4238 "net rpc share migrate all\n"
4241 _("Migrates shares including all share settings"));
4246 d_printf(_("no server to migrate\n"));
4250 /* order is important. we don't want to be locked out by the share-acl
4251 * before copying files - gd */
4253 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4254 rpc_share_migrate_shares_internals
, argc
, argv
);
4258 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4259 rpc_share_migrate_files_internals
, argc
, argv
);
4263 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4264 rpc_share_migrate_security_internals
, argc
,
4270 * 'net rpc share migrate' entrypoint.
4271 * @param argc Standard main() style argc.
4272 * @param argv Standard main() style argv. Initial components are already
4275 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4278 struct functable func
[] = {
4281 rpc_share_migrate_all
,
4283 N_("Migrate shares from remote to local server"),
4284 N_("net rpc share migrate all\n"
4285 " Migrate shares from remote to local server")
4289 rpc_share_migrate_files
,
4291 N_("Migrate files from remote to local server"),
4292 N_("net rpc share migrate files\n"
4293 " Migrate files from remote to local server")
4297 rpc_share_migrate_security
,
4299 N_("Migrate share-ACLs from remote to local server"),
4300 N_("net rpc share migrate security\n"
4301 " Migrate share-ACLs from remote to local server")
4305 rpc_share_migrate_shares
,
4307 N_("Migrate shares from remote to local server"),
4308 N_("net rpc share migrate shares\n"
4309 " Migrate shares from remote to local server")
4311 {NULL
, NULL
, 0, NULL
, NULL
}
4314 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4316 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
4322 struct dom_sid
*members
;
4325 static int num_server_aliases
;
4326 static struct full_alias
*server_aliases
;
4329 * Add an alias to the static list.
4331 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4333 if (server_aliases
== NULL
)
4334 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4336 server_aliases
[num_server_aliases
] = *alias
;
4337 num_server_aliases
+= 1;
4341 * For a specific domain on the server, fetch all the aliases
4342 * and their members. Add all of them to the server_aliases.
4345 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4346 TALLOC_CTX
*mem_ctx
,
4347 struct policy_handle
*connect_pol
,
4348 const struct dom_sid
*domain_sid
)
4350 uint32 start_idx
, max_entries
, num_entries
, i
;
4351 struct samr_SamArray
*groups
= NULL
;
4352 NTSTATUS result
, status
;
4353 struct policy_handle domain_pol
;
4354 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4356 /* Get domain policy handle */
4358 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
4360 MAXIMUM_ALLOWED_ACCESS
,
4361 discard_const_p(struct dom_sid2
, domain_sid
),
4364 if (!NT_STATUS_IS_OK(status
)) {
4367 if (!NT_STATUS_IS_OK(result
)) {
4375 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
4382 if (!NT_STATUS_IS_OK(status
)) {
4385 for (i
= 0; i
< num_entries
; i
++) {
4387 struct policy_handle alias_pol
;
4388 struct full_alias alias
;
4389 struct lsa_SidArray sid_array
;
4393 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
4395 MAXIMUM_ALLOWED_ACCESS
,
4396 groups
->entries
[i
].idx
,
4399 if (!NT_STATUS_IS_OK(status
)) {
4402 if (!NT_STATUS_IS_OK(_result
)) {
4407 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
4411 if (!NT_STATUS_IS_OK(status
)) {
4414 if (!NT_STATUS_IS_OK(_result
)) {
4419 alias
.num_members
= sid_array
.num_sids
;
4421 status
= dcerpc_samr_Close(b
, mem_ctx
, &alias_pol
, &_result
);
4422 if (!NT_STATUS_IS_OK(status
)) {
4425 if (!NT_STATUS_IS_OK(_result
)) {
4430 alias
.members
= NULL
;
4432 if (alias
.num_members
> 0) {
4433 alias
.members
= SMB_MALLOC_ARRAY(struct dom_sid
, alias
.num_members
);
4435 for (j
= 0; j
< alias
.num_members
; j
++)
4436 sid_copy(&alias
.members
[j
],
4437 sid_array
.sids
[j
].sid
);
4440 sid_compose(&alias
.sid
, domain_sid
,
4441 groups
->entries
[i
].idx
);
4443 push_alias(mem_ctx
, &alias
);
4445 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4447 status
= NT_STATUS_OK
;
4450 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
4456 * Dump server_aliases as names for debugging purposes.
4459 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4460 const struct dom_sid
*domain_sid
,
4461 const char *domain_name
,
4462 struct cli_state
*cli
,
4463 struct rpc_pipe_client
*pipe_hnd
,
4464 TALLOC_CTX
*mem_ctx
,
4470 struct policy_handle lsa_pol
;
4471 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4473 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4474 SEC_FLAG_MAXIMUM_ALLOWED
,
4476 if (!NT_STATUS_IS_OK(result
))
4479 for (i
=0; i
<num_server_aliases
; i
++) {
4482 enum lsa_SidType
*types
;
4485 struct full_alias
*alias
= &server_aliases
[i
];
4487 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4489 &domains
, &names
, &types
);
4490 if (!NT_STATUS_IS_OK(result
))
4493 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4495 if (alias
->num_members
== 0) {
4500 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4503 &domains
, &names
, &types
);
4505 if (!NT_STATUS_IS_OK(result
) &&
4506 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4509 for (j
=0; j
<alias
->num_members
; j
++)
4510 DEBUG(1, ("%s\\%s (%d); ",
4511 domains
[j
] ? domains
[j
] : "*unknown*",
4512 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4516 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
4518 return NT_STATUS_OK
;
4522 * Fetch a list of all server aliases and their members into
4526 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4527 const struct dom_sid
*domain_sid
,
4528 const char *domain_name
,
4529 struct cli_state
*cli
,
4530 struct rpc_pipe_client
*pipe_hnd
,
4531 TALLOC_CTX
*mem_ctx
,
4535 NTSTATUS result
, status
;
4536 struct policy_handle connect_pol
;
4537 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4539 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
4541 MAXIMUM_ALLOWED_ACCESS
,
4544 if (!NT_STATUS_IS_OK(status
)) {
4547 if (!NT_STATUS_IS_OK(result
)) {
4552 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4553 &global_sid_Builtin
);
4554 if (!NT_STATUS_IS_OK(status
)) {
4558 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4561 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
4566 static void init_user_token(struct security_token
*token
, struct dom_sid
*user_sid
)
4568 token
->num_sids
= 4;
4570 if (!(token
->sids
= SMB_MALLOC_ARRAY(struct dom_sid
, 4))) {
4571 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4572 token
->num_sids
= 0;
4576 token
->sids
[0] = *user_sid
;
4577 sid_copy(&token
->sids
[1], &global_sid_World
);
4578 sid_copy(&token
->sids
[2], &global_sid_Network
);
4579 sid_copy(&token
->sids
[3], &global_sid_Authenticated_Users
);
4582 static void free_user_token(struct security_token
*token
)
4584 SAFE_FREE(token
->sids
);
4587 static void add_sid_to_token(struct security_token
*token
, struct dom_sid
*sid
)
4589 if (security_token_has_sid(token
, sid
))
4592 token
->sids
= SMB_REALLOC_ARRAY(token
->sids
, struct dom_sid
, token
->num_sids
+1);
4597 sid_copy(&token
->sids
[token
->num_sids
], sid
);
4599 token
->num_sids
+= 1;
4604 struct security_token token
;
4607 static void dump_user_token(struct user_token
*token
)
4611 d_printf("%s\n", token
->name
);
4613 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4614 d_printf(" %s\n", sid_string_tos(&token
->token
.sids
[i
]));
4618 static bool is_alias_member(struct dom_sid
*sid
, struct full_alias
*alias
)
4622 for (i
=0; i
<alias
->num_members
; i
++) {
4623 if (dom_sid_compare(sid
, &alias
->members
[i
]) == 0)
4630 static void collect_sid_memberships(struct security_token
*token
, struct dom_sid sid
)
4634 for (i
=0; i
<num_server_aliases
; i
++) {
4635 if (is_alias_member(&sid
, &server_aliases
[i
]))
4636 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4641 * We got a user token with all the SIDs we can know about without asking the
4642 * server directly. These are the user and domain group sids. All of these can
4643 * be members of aliases. So scan the list of aliases for each of the SIDs and
4644 * add them to the token.
4647 static void collect_alias_memberships(struct security_token
*token
)
4649 int num_global_sids
= token
->num_sids
;
4652 for (i
=0; i
<num_global_sids
; i
++) {
4653 collect_sid_memberships(token
, token
->sids
[i
]);
4657 static bool get_user_sids(const char *domain
, const char *user
, struct security_token
*token
)
4659 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4660 enum wbcSidType type
;
4662 struct wbcDomainSid wsid
;
4663 char sid_str
[WBC_SID_STRING_BUFLEN
];
4664 struct dom_sid user_sid
;
4665 uint32_t num_groups
;
4666 gid_t
*groups
= NULL
;
4669 fstr_sprintf(full_name
, "%s%c%s",
4670 domain
, *lp_winbind_separator(), user
);
4672 /* First let's find out the user sid */
4674 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4676 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4677 DEBUG(1, ("winbind could not find %s: %s\n",
4678 full_name
, wbcErrorString(wbc_status
)));
4682 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4684 if (type
!= WBC_SID_NAME_USER
) {
4685 DEBUG(1, ("%s is not a user\n", full_name
));
4689 if (!string_to_sid(&user_sid
, sid_str
)) {
4690 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4694 init_user_token(token
, &user_sid
);
4696 /* And now the groups winbind knows about */
4698 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4699 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4700 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4701 full_name
, wbcErrorString(wbc_status
)));
4705 for (i
= 0; i
< num_groups
; i
++) {
4706 gid_t gid
= groups
[i
];
4709 wbc_status
= wbcGidToSid(gid
, &wsid
);
4710 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4711 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4712 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4713 wbcFreeMemory(groups
);
4717 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4719 DEBUG(3, (" %s\n", sid_str
));
4721 string_to_sid(&sid
, sid_str
);
4722 add_sid_to_token(token
, &sid
);
4724 wbcFreeMemory(groups
);
4730 * Get a list of all user tokens we want to look at
4733 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4734 struct user_token
**user_tokens
)
4736 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4737 uint32_t i
, num_users
;
4739 struct user_token
*result
;
4740 TALLOC_CTX
*frame
= NULL
;
4742 if (lp_winbind_use_default_domain() &&
4743 (c
->opt_target_workgroup
== NULL
)) {
4744 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4745 "please specify a workgroup\n"));
4749 /* Send request to winbind daemon */
4751 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4752 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4753 DEBUG(1, (_("winbind could not list users: %s\n"),
4754 wbcErrorString(wbc_status
)));
4758 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4760 if (result
== NULL
) {
4761 DEBUG(1, ("Could not malloc sid array\n"));
4762 wbcFreeMemory(users
);
4766 frame
= talloc_stackframe();
4767 for (i
=0; i
< num_users
; i
++) {
4768 fstring domain
, user
;
4771 fstrcpy(result
[i
].name
, users
[i
]);
4773 p
= strchr(users
[i
], *lp_winbind_separator());
4775 DEBUG(3, ("%s\n", users
[i
]));
4778 fstrcpy(domain
, c
->opt_target_workgroup
);
4779 fstrcpy(user
, users
[i
]);
4782 fstrcpy(domain
, users
[i
]);
4783 if (!strupper_m(domain
)) {
4784 DEBUG(1, ("strupper_m %s failed\n", domain
));
4785 wbcFreeMemory(users
);
4791 get_user_sids(domain
, user
, &(result
[i
].token
));
4794 wbcFreeMemory(users
);
4796 *num_tokens
= num_users
;
4797 *user_tokens
= result
;
4802 static bool get_user_tokens_from_file(FILE *f
,
4804 struct user_token
**tokens
)
4806 struct user_token
*token
= NULL
;
4811 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4815 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4816 line
[strlen(line
)-1] = '\0';
4819 if (line
[0] == ' ') {
4823 if(!string_to_sid(&sid
, &line
[1])) {
4824 DEBUG(1,("get_user_tokens_from_file: Could "
4825 "not convert sid %s \n",&line
[1]));
4829 if (token
== NULL
) {
4830 DEBUG(0, ("File does not begin with username"));
4834 add_sid_to_token(&token
->token
, &sid
);
4838 /* And a new user... */
4841 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4842 if (*tokens
== NULL
) {
4843 DEBUG(0, ("Could not realloc tokens\n"));
4847 token
= &((*tokens
)[*num_tokens
-1]);
4849 if (strlcpy(token
->name
, line
, sizeof(token
->name
)) >= sizeof(token
->name
)) {
4852 token
->token
.num_sids
= 0;
4853 token
->token
.sids
= NULL
;
4862 * Show the list of all users that have access to a share
4865 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4866 TALLOC_CTX
*mem_ctx
,
4867 const char *netname
,
4869 struct user_token
*tokens
)
4872 struct security_descriptor
*share_sd
= NULL
;
4873 struct security_descriptor
*root_sd
= NULL
;
4874 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4876 union srvsvc_NetShareInfo info
;
4880 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4882 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
4889 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4890 DEBUG(1, ("Coult not query secdesc for share %s\n",
4895 share_sd
= info
.info502
->sd_buf
.sd
;
4896 if (share_sd
== NULL
) {
4897 DEBUG(1, ("Got no secdesc for share %s\n",
4901 cnum
= cli_state_get_tid(cli
);
4903 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, netname
, "A:", "", 0))) {
4907 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4908 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4909 cli_query_secdesc(cli
, fnum
, mem_ctx
, &root_sd
);
4912 for (i
=0; i
<num_tokens
; i
++) {
4915 if (share_sd
!= NULL
) {
4916 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4919 if (!NT_STATUS_IS_OK(status
)) {
4920 DEBUG(1, ("Could not check share_sd for "
4927 if (root_sd
== NULL
) {
4928 d_printf(" %s\n", tokens
[i
].name
);
4932 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4934 if (!NT_STATUS_IS_OK(status
)) {
4935 DEBUG(1, ("Could not check root_sd for user %s\n",
4939 d_printf(" %s\n", tokens
[i
].name
);
4942 if (fnum
!= (uint16_t)-1)
4943 cli_close(cli
, fnum
);
4945 cli_state_set_tid(cli
, cnum
);
4951 * List shares on a remote RPC server, including the security descriptors.
4953 * All parameters are provided by the run_rpc_command function, except for
4954 * argc, argv which are passed through.
4956 * @param domain_sid The domain sid acquired from the remote server.
4957 * @param cli A cli_state connected to the server.
4958 * @param mem_ctx Talloc context, destroyed on completion of the function.
4959 * @param argc Standard main() style argc.
4960 * @param argv Standard main() style argv. Initial components are already
4963 * @return Normal NTSTATUS return.
4966 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4967 const struct dom_sid
*domain_sid
,
4968 const char *domain_name
,
4969 struct cli_state
*cli
,
4970 struct rpc_pipe_client
*pipe_hnd
,
4971 TALLOC_CTX
*mem_ctx
,
4977 NTSTATUS nt_status
= NT_STATUS_OK
;
4978 uint32_t total_entries
= 0;
4979 uint32_t resume_handle
= 0;
4980 uint32_t preferred_len
= 0xffffffff;
4982 struct dcerpc_binding_handle
*b
= NULL
;
4983 struct srvsvc_NetShareInfoCtr info_ctr
;
4984 struct srvsvc_NetShareCtr1 ctr1
;
4987 struct user_token
*tokens
= NULL
;
4993 f
= fopen(argv
[0], "r");
4997 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4998 return NT_STATUS_UNSUCCESSFUL
;
5001 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
5007 DEBUG(0, ("Could not read users from file\n"));
5008 return NT_STATUS_UNSUCCESSFUL
;
5011 for (i
=0; i
<num_tokens
; i
++)
5012 collect_alias_memberships(&tokens
[i
].token
);
5014 ZERO_STRUCT(info_ctr
);
5018 info_ctr
.ctr
.ctr1
= &ctr1
;
5020 b
= pipe_hnd
->binding_handle
;
5022 /* Issue the NetShareEnum RPC call and retrieve the response */
5023 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
,
5032 /* Was it successful? */
5033 if (!NT_STATUS_IS_OK(nt_status
)) {
5034 /* Nope. Go clean up. */
5038 if (!W_ERROR_IS_OK(result
)) {
5039 /* Nope. Go clean up. */
5040 nt_status
= werror_to_ntstatus(result
);
5044 if (total_entries
== 0) {
5048 /* For each returned entry... */
5049 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
5050 const char *netname
= info_ctr
.ctr
.ctr1
->array
[i
].name
;
5052 if (info_ctr
.ctr
.ctr1
->array
[i
].type
!= STYPE_DISKTREE
) {
5056 d_printf("%s\n", netname
);
5058 show_userlist(pipe_hnd
, mem_ctx
, netname
,
5059 num_tokens
, tokens
);
5062 for (i
=0; i
<num_tokens
; i
++) {
5063 free_user_token(&tokens
[i
].token
);
5070 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
5075 if (c
->display_usage
) {
5077 "net rpc share allowedusers\n"
5080 _("List allowed users"));
5084 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5085 rpc_aliaslist_internals
,
5090 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
, 0,
5096 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
5097 rpc_share_allowedusers_internals
,
5101 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
5104 struct user_token
*tokens
= NULL
;
5108 net_usersidlist_usage(c
, argc
, argv
);
5112 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
5113 DEBUG(0, ("Could not get the user/sid list\n"));
5117 for (i
=0; i
<num_tokens
; i
++) {
5118 dump_user_token(&tokens
[i
]);
5119 free_user_token(&tokens
[i
].token
);
5126 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
5128 d_printf(_("net usersidlist\n"
5129 "\tprints out a list of all users the running winbind knows\n"
5130 "\tabout, together with all their SIDs. This is used as\n"
5131 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5133 net_common_flags_usage(c
, argc
, argv
);
5138 * 'net rpc share' entrypoint.
5139 * @param argc Standard main() style argc.
5140 * @param argv Standard main() style argv. Initial components are already
5144 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
5146 NET_API_STATUS status
;
5148 struct functable func
[] = {
5154 N_("net rpc share add\n"
5162 N_("net rpc share delete\n"
5167 rpc_share_allowedusers
,
5169 N_("Modify allowed users"),
5170 N_("net rpc share allowedusers\n"
5171 " Modify allowed users")
5177 N_("Migrate share to local server"),
5178 N_("net rpc share migrate\n"
5179 " Migrate share to local server")
5186 N_("net rpc share list\n"
5189 {NULL
, NULL
, 0, NULL
, NULL
}
5192 status
= libnetapi_net_init(&c
->netapi_ctx
);
5196 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5197 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5198 if (c
->opt_kerberos
) {
5199 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5203 if (c
->display_usage
) {
5208 " Alias for net rpc share list\n"));
5209 net_display_usage_from_functable(func
);
5213 return rpc_share_list(c
, argc
, argv
);
5216 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5219 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5220 TALLOC_CTX
*mem_ctx
,
5221 struct rpc_sh_ctx
*ctx
,
5222 struct rpc_pipe_client
*pipe_hnd
,
5223 int argc
, const char **argv
)
5226 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
5229 static NTSTATUS
rpc_sh_share_add(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 NET_API_STATUS status
;
5236 uint32_t parm_err
= 0;
5237 struct SHARE_INFO_2 i2
;
5239 if ((argc
< 2) || (argc
> 3)) {
5240 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
5242 return NT_STATUS_INVALID_PARAMETER
;
5245 i2
.shi2_netname
= argv
[0];
5246 i2
.shi2_type
= STYPE_DISKTREE
;
5247 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
5248 i2
.shi2_permissions
= 0;
5249 i2
.shi2_max_uses
= 0;
5250 i2
.shi2_current_uses
= 0;
5251 i2
.shi2_path
= argv
[1];
5252 i2
.shi2_passwd
= NULL
;
5254 status
= NetShareAdd(pipe_hnd
->desthost
,
5259 return werror_to_ntstatus(W_ERROR(status
));
5262 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5263 TALLOC_CTX
*mem_ctx
,
5264 struct rpc_sh_ctx
*ctx
,
5265 struct rpc_pipe_client
*pipe_hnd
,
5266 int argc
, const char **argv
)
5269 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5270 return NT_STATUS_INVALID_PARAMETER
;
5273 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
5276 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5277 TALLOC_CTX
*mem_ctx
,
5278 struct rpc_sh_ctx
*ctx
,
5279 struct rpc_pipe_client
*pipe_hnd
,
5280 int argc
, const char **argv
)
5282 union srvsvc_NetShareInfo info
;
5285 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5288 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5289 return NT_STATUS_INVALID_PARAMETER
;
5292 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
5298 if (!NT_STATUS_IS_OK(status
)) {
5299 result
= ntstatus_to_werror(status
);
5302 if (!W_ERROR_IS_OK(result
)) {
5306 d_printf(_("Name: %s\n"), info
.info2
->name
);
5307 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
5308 d_printf(_("Path: %s\n"), info
.info2
->path
);
5309 d_printf(_("Password: %s\n"), info
.info2
->password
);
5312 return werror_to_ntstatus(result
);
5315 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5316 struct rpc_sh_ctx
*ctx
)
5318 static struct rpc_sh_cmd cmds
[] = {
5320 { "list", NULL
, &ndr_table_srvsvc
, rpc_sh_share_list
,
5321 N_("List available shares") },
5323 { "add", NULL
, &ndr_table_srvsvc
, rpc_sh_share_add
,
5324 N_("Add a share") },
5326 { "delete", NULL
, &ndr_table_srvsvc
, rpc_sh_share_delete
,
5327 N_("Delete a share") },
5329 { "info", NULL
, &ndr_table_srvsvc
, rpc_sh_share_info
,
5330 N_("Get information about a share") },
5332 { NULL
, NULL
, 0, NULL
, NULL
}
5338 /****************************************************************************/
5340 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5342 return net_file_usage(c
, argc
, argv
);
5346 * Close a file on a remote RPC server.
5348 * @param argc Standard main() style argc.
5349 * @param argv Standard main() style argv. Initial components are already
5352 * @return A shell status integer (0 for success).
5354 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5356 if (argc
< 1 || c
->display_usage
) {
5357 return rpc_file_usage(c
, argc
, argv
);
5360 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
5364 * Formatted print of open file info
5366 * @param r struct FILE_INFO_3 contents
5369 static void display_file_info_3(struct FILE_INFO_3
*r
)
5371 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5372 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
5373 r
->fi3_num_locks
, r
->fi3_pathname
);
5377 * List files for a user on a remote RPC server.
5379 * @param argc Standard main() style argc.
5380 * @param argv Standard main() style argv. Initial components are already
5383 * @return A shell status integer (0 for success)..
5386 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5388 NET_API_STATUS status
;
5389 uint32 preferred_len
= 0xffffffff, i
;
5390 const char *username
=NULL
;
5391 uint32_t total_entries
= 0;
5392 uint32_t entries_read
= 0;
5393 uint32_t resume_handle
= 0;
5394 struct FILE_INFO_3
*i3
= NULL
;
5396 if (c
->display_usage
) {
5397 return rpc_file_usage(c
, argc
, argv
);
5400 /* if argc > 0, must be user command */
5402 username
= smb_xstrdup(argv
[0]);
5405 status
= NetFileEnum(c
->opt_host
,
5409 (uint8_t **)(void *)&i3
,
5419 /* Display results */
5422 "\nEnumerating open files on remote server:\n\n"
5423 "\nFileId Opened by Perms Locks Path"
5424 "\n------ --------- ----- ----- ---- \n"));
5425 for (i
= 0; i
< entries_read
; i
++) {
5426 display_file_info_3(&i3
[i
]);
5433 * 'net rpc file' entrypoint.
5434 * @param argc Standard main() style argc.
5435 * @param argv Standard main() style argv. Initial components are already
5439 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5441 NET_API_STATUS status
;
5443 struct functable func
[] = {
5448 N_("Close opened file"),
5449 N_("net rpc file close\n"
5450 " Close opened file")
5456 N_("List files opened by user"),
5457 N_("net rpc file user\n"
5458 " List files opened by user")
5465 N_("Display information about opened file"),
5466 N_("net rpc file info\n"
5467 " Display information about opened file")
5470 {NULL
, NULL
, 0, NULL
, NULL
}
5473 status
= libnetapi_net_init(&c
->netapi_ctx
);
5477 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5478 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5479 if (c
->opt_kerberos
) {
5480 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5484 if (c
->display_usage
) {
5485 d_printf(_("Usage:\n"));
5486 d_printf(_("net rpc file\n"
5487 " List opened files\n"));
5488 net_display_usage_from_functable(func
);
5492 return rpc_file_user(c
, argc
, argv
);
5495 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5499 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5501 * All parameters are provided by the run_rpc_command function, except for
5502 * argc, argv which are passed through.
5504 * @param c A net_context structure.
5505 * @param domain_sid The domain sid acquired from the remote server.
5506 * @param cli A cli_state connected to the server.
5507 * @param mem_ctx Talloc context, destroyed on completion of the function.
5508 * @param argc Standard main() style argc.
5509 * @param argv Standard main() style argv. Initial components are already
5512 * @return Normal NTSTATUS return.
5515 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5516 const struct dom_sid
*domain_sid
,
5517 const char *domain_name
,
5518 struct cli_state
*cli
,
5519 struct rpc_pipe_client
*pipe_hnd
,
5520 TALLOC_CTX
*mem_ctx
,
5524 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5526 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5528 status
= dcerpc_initshutdown_Abort(b
, mem_ctx
, NULL
, &result
);
5529 if (!NT_STATUS_IS_OK(status
)) {
5532 if (W_ERROR_IS_OK(result
)) {
5533 d_printf(_("\nShutdown successfully aborted\n"));
5534 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5536 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5538 return werror_to_ntstatus(result
);
5542 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5544 * All parameters are provided by the run_rpc_command function, except for
5545 * argc, argv which are passed through.
5547 * @param c A net_context structure.
5548 * @param domain_sid The domain sid acquired from the remote server.
5549 * @param cli A cli_state connected to the server.
5550 * @param mem_ctx Talloc context, destroyed on completion of the function.
5551 * @param argc Standard main() style argc.
5552 * @param argv Standard main() style argv. Initial components are already
5555 * @return Normal NTSTATUS return.
5558 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5559 const struct dom_sid
*domain_sid
,
5560 const char *domain_name
,
5561 struct cli_state
*cli
,
5562 struct rpc_pipe_client
*pipe_hnd
,
5563 TALLOC_CTX
*mem_ctx
,
5567 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5569 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5571 result
= dcerpc_winreg_AbortSystemShutdown(b
, mem_ctx
, NULL
, &werr
);
5573 if (!NT_STATUS_IS_OK(result
)) {
5574 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5577 if (W_ERROR_IS_OK(werr
)) {
5578 d_printf(_("\nShutdown successfully aborted\n"));
5579 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5581 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5583 return werror_to_ntstatus(werr
);
5587 * ABORT the shutdown of a remote RPC server.
5589 * @param argc Standard main() style argc.
5590 * @param argv Standard main() style argv. Initial components are already
5593 * @return A shell status integer (0 for success).
5596 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5601 if (c
->display_usage
) {
5603 "net rpc abortshutdown\n"
5606 _("Abort a scheduled shutdown"));
5610 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5611 rpc_shutdown_abort_internals
, argc
, argv
);
5616 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5618 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5619 rpc_reg_shutdown_abort_internals
,
5624 * Shut down a remote RPC Server via initshutdown pipe.
5626 * All parameters are provided by the run_rpc_command function, except for
5627 * argc, argv which are passed through.
5629 * @param c A net_context structure.
5630 * @param domain_sid The domain sid acquired from the remote server.
5631 * @param cli A cli_state connected to the server.
5632 * @param mem_ctx Talloc context, destroyed on completion of the function.
5633 * @param argc Standard main() style argc.
5634 * @param argv Standard main() style argv. Initial components are already
5637 * @return Normal NTSTATUS return.
5640 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5641 const struct dom_sid
*domain_sid
,
5642 const char *domain_name
,
5643 struct cli_state
*cli
,
5644 struct rpc_pipe_client
*pipe_hnd
,
5645 TALLOC_CTX
*mem_ctx
,
5649 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5651 const char *msg
= N_("This machine will be shutdown shortly");
5652 uint32 timeout
= 20;
5653 struct lsa_StringLarge msg_string
;
5654 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5656 if (c
->opt_comment
) {
5657 msg
= c
->opt_comment
;
5659 if (c
->opt_timeout
) {
5660 timeout
= c
->opt_timeout
;
5663 msg_string
.string
= msg
;
5665 /* create an entry */
5666 status
= dcerpc_initshutdown_Init(b
, mem_ctx
, NULL
,
5667 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5669 if (!NT_STATUS_IS_OK(status
)) {
5672 if (W_ERROR_IS_OK(result
)) {
5673 d_printf(_("\nShutdown of remote machine succeeded\n"));
5674 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5676 DEBUG(1,("Shutdown of remote machine failed!\n"));
5678 return werror_to_ntstatus(result
);
5682 * Shut down a remote RPC Server via winreg pipe.
5684 * All parameters are provided by the run_rpc_command function, except for
5685 * argc, argv which are passed through.
5687 * @param c A net_context structure.
5688 * @param domain_sid The domain sid acquired from the remote server.
5689 * @param cli A cli_state connected to the server.
5690 * @param mem_ctx Talloc context, destroyed on completion of the function.
5691 * @param argc Standard main() style argc.
5692 * @param argv Standard main() style argv. Initial components are already
5695 * @return Normal NTSTATUS return.
5698 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5699 const struct dom_sid
*domain_sid
,
5700 const char *domain_name
,
5701 struct cli_state
*cli
,
5702 struct rpc_pipe_client
*pipe_hnd
,
5703 TALLOC_CTX
*mem_ctx
,
5707 const char *msg
= N_("This machine will be shutdown shortly");
5708 uint32 timeout
= 20;
5709 struct lsa_StringLarge msg_string
;
5712 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5714 if (c
->opt_comment
) {
5715 msg
= c
->opt_comment
;
5717 msg_string
.string
= msg
;
5719 if (c
->opt_timeout
) {
5720 timeout
= c
->opt_timeout
;
5723 /* create an entry */
5724 result
= dcerpc_winreg_InitiateSystemShutdown(b
, mem_ctx
, NULL
,
5725 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5727 if (!NT_STATUS_IS_OK(result
)) {
5728 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5732 if (W_ERROR_IS_OK(werr
)) {
5733 d_printf(_("\nShutdown of remote machine succeeded\n"));
5735 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5736 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5737 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5739 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5742 return werror_to_ntstatus(werr
);
5746 * Shut down a remote RPC server.
5748 * @param argc Standard main() style argc.
5749 * @param argv Standard main() style argv. Initial components are already
5752 * @return A shell status integer (0 for success).
5755 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5759 if (c
->display_usage
) {
5761 "net rpc shutdown\n"
5764 _("Shut down a remote RPC server"));
5768 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5769 rpc_init_shutdown_internals
, argc
, argv
);
5772 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5773 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5774 rpc_reg_shutdown_internals
, argc
, argv
);
5780 /***************************************************************************
5781 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5782 ***************************************************************************/
5785 * Add interdomain trust account to the RPC server.
5786 * All parameters (except for argc and argv) are passed by run_rpc_command
5789 * @param c A net_context structure.
5790 * @param domain_sid The domain sid acquired from the server.
5791 * @param cli A cli_state connected to the server.
5792 * @param mem_ctx Talloc context, destroyed on completion of the function.
5793 * @param argc Standard main() style argc.
5794 * @param argv Standard main() style argv. Initial components are already
5797 * @return normal NTSTATUS return code.
5800 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5801 const struct dom_sid
*domain_sid
,
5802 const char *domain_name
,
5803 struct cli_state
*cli
,
5804 struct rpc_pipe_client
*pipe_hnd
,
5805 TALLOC_CTX
*mem_ctx
,
5809 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5810 NTSTATUS status
, result
;
5812 struct lsa_String lsa_acct_name
;
5814 uint32 acct_flags
=0;
5816 uint32_t access_granted
= 0;
5817 union samr_UserInfo info
;
5818 unsigned int orig_timeout
;
5819 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5820 DATA_BLOB session_key
= data_blob_null
;
5825 _(" net rpc trustdom add <domain_name> "
5826 "<trust password>\n"));
5827 return NT_STATUS_INVALID_PARAMETER
;
5831 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5834 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5835 return NT_STATUS_NO_MEMORY
;
5838 if (!strupper_m(acct_name
)) {
5839 SAFE_FREE(acct_name
);
5840 return NT_STATUS_INVALID_PARAMETER
;
5843 init_lsa_String(&lsa_acct_name
, acct_name
);
5845 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
5846 if (!NT_STATUS_IS_OK(status
)) {
5847 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
5848 nt_errstr(status
)));
5852 /* Get samr policy handle */
5853 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5855 MAXIMUM_ALLOWED_ACCESS
,
5858 if (!NT_STATUS_IS_OK(status
)) {
5861 if (!NT_STATUS_IS_OK(result
)) {
5866 /* Get domain policy handle */
5867 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5869 MAXIMUM_ALLOWED_ACCESS
,
5870 discard_const_p(struct dom_sid2
, domain_sid
),
5873 if (!NT_STATUS_IS_OK(status
)) {
5876 if (!NT_STATUS_IS_OK(result
)) {
5881 /* This call can take a long time - allow the server to time out.
5882 * 35 seconds should do it. */
5884 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5886 /* Create trusting domain's account */
5887 acb_info
= ACB_NORMAL
;
5888 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5889 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5890 SAMR_USER_ACCESS_SET_PASSWORD
|
5891 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5892 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5894 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
5903 if (!NT_STATUS_IS_OK(status
)) {
5906 /* And restore our original timeout. */
5907 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5909 if (!NT_STATUS_IS_OK(result
)) {
5911 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5912 acct_name
, nt_errstr(result
));
5917 struct samr_CryptPassword crypt_pwd
;
5919 ZERO_STRUCT(info
.info23
);
5921 init_samr_CryptPassword(argv
[1],
5925 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5926 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5927 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5928 info
.info23
.password
= crypt_pwd
;
5930 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
5935 if (!NT_STATUS_IS_OK(status
)) {
5939 if (!NT_STATUS_IS_OK(result
)) {
5941 DEBUG(0,("Could not set trust account password: %s\n",
5942 nt_errstr(result
)));
5948 SAFE_FREE(acct_name
);
5949 data_blob_clear_free(&session_key
);
5954 * Create interdomain trust account for a remote domain.
5956 * @param argc Standard argc.
5957 * @param argv Standard argv without initial components.
5959 * @return Integer status (0 means success).
5962 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5964 if (argc
> 0 && !c
->display_usage
) {
5965 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5966 rpc_trustdom_add_internals
, argc
, argv
);
5970 _("net rpc trustdom add <domain_name> <trust "
5978 * Remove interdomain trust account from the RPC server.
5979 * All parameters (except for argc and argv) are passed by run_rpc_command
5982 * @param c A net_context structure.
5983 * @param domain_sid The domain sid acquired from the server.
5984 * @param cli A cli_state connected to the server.
5985 * @param mem_ctx Talloc context, destroyed on completion of the function.
5986 * @param argc Standard main() style argc.
5987 * @param argv Standard main() style argv. Initial components are already
5990 * @return normal NTSTATUS return code.
5993 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5994 const struct dom_sid
*domain_sid
,
5995 const char *domain_name
,
5996 struct cli_state
*cli
,
5997 struct rpc_pipe_client
*pipe_hnd
,
5998 TALLOC_CTX
*mem_ctx
,
6002 struct policy_handle connect_pol
, domain_pol
, user_pol
;
6003 NTSTATUS status
, result
;
6005 struct dom_sid trust_acct_sid
;
6006 struct samr_Ids user_rids
, name_types
;
6007 struct lsa_String lsa_acct_name
;
6008 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6013 _(" net rpc trustdom del <domain_name>\n"));
6014 return NT_STATUS_INVALID_PARAMETER
;
6018 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6020 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
6022 if (acct_name
== NULL
)
6023 return NT_STATUS_NO_MEMORY
;
6025 if (!strupper_m(acct_name
)) {
6026 TALLOC_FREE(acct_name
);
6027 return NT_STATUS_INVALID_PARAMETER
;
6030 /* Get samr policy handle */
6031 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6033 MAXIMUM_ALLOWED_ACCESS
,
6036 if (!NT_STATUS_IS_OK(status
)) {
6039 if (!NT_STATUS_IS_OK(result
)) {
6044 /* Get domain policy handle */
6045 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6047 MAXIMUM_ALLOWED_ACCESS
,
6048 discard_const_p(struct dom_sid2
, domain_sid
),
6051 if (!NT_STATUS_IS_OK(status
)) {
6054 if (!NT_STATUS_IS_OK(result
)) {
6059 init_lsa_String(&lsa_acct_name
, acct_name
);
6061 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
6068 if (!NT_STATUS_IS_OK(status
)) {
6069 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6071 acct_name
, nt_errstr(status
));
6074 if (!NT_STATUS_IS_OK(result
)) {
6076 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6078 acct_name
, nt_errstr(result
) );
6081 if (user_rids
.count
!= 1) {
6082 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
6085 if (name_types
.count
!= 1) {
6086 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
6090 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
6092 MAXIMUM_ALLOWED_ACCESS
,
6096 if (!NT_STATUS_IS_OK(status
)) {
6097 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6099 acct_name
, nt_errstr(status
) );
6103 if (!NT_STATUS_IS_OK(result
)) {
6105 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6107 acct_name
, nt_errstr(result
) );
6111 /* append the rid to the domain sid */
6112 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
6116 /* remove the sid */
6118 status
= dcerpc_samr_RemoveMemberFromForeignDomain(b
, mem_ctx
,
6122 if (!NT_STATUS_IS_OK(status
)) {
6123 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6124 " on user %s failed %s\n"),
6125 acct_name
, nt_errstr(status
));
6128 if (!NT_STATUS_IS_OK(result
)) {
6130 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6131 " on user %s failed %s\n"),
6132 acct_name
, nt_errstr(result
) );
6139 status
= dcerpc_samr_DeleteUser(b
, mem_ctx
,
6142 if (!NT_STATUS_IS_OK(status
)) {
6143 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6145 acct_name
, nt_errstr(status
));
6149 if (!NT_STATUS_IS_OK(result
)) {
6151 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6153 acct_name
, nt_errstr(result
) );
6157 if (!NT_STATUS_IS_OK(result
)) {
6158 d_printf(_("Could not set trust account password: %s\n"),
6168 * Delete interdomain trust account for a remote domain.
6170 * @param argc Standard argc.
6171 * @param argv Standard argv without initial components.
6173 * @return Integer status (0 means success).
6176 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
6178 if (argc
> 0 && !c
->display_usage
) {
6179 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
6180 rpc_trustdom_del_internals
, argc
, argv
);
6184 _("net rpc trustdom del <domain>\n"));
6189 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
6190 struct cli_state
*cli
,
6191 TALLOC_CTX
*mem_ctx
,
6192 const char *domain_name
)
6194 char *dc_name
= NULL
;
6195 const char *buffer
= NULL
;
6196 struct rpc_pipe_client
*netr
;
6199 struct dcerpc_binding_handle
*b
;
6201 /* Use NetServerEnum2 */
6203 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
6205 return NT_STATUS_OK
;
6208 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6209 for domain %s\n", domain_name
));
6211 /* Try netr_GetDcName */
6213 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
6215 if (!NT_STATUS_IS_OK(status
)) {
6219 b
= netr
->binding_handle
;
6221 status
= dcerpc_netr_GetDcName(b
, mem_ctx
,
6228 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(result
)) {
6232 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6233 for domain %s\n", domain_name
));
6235 if (!NT_STATUS_IS_OK(status
)) {
6239 return werror_to_ntstatus(result
);
6243 * Establish trust relationship to a trusting domain.
6244 * Interdomain account must already be created on remote PDC.
6246 * @param c A net_context structure.
6247 * @param argc Standard argc.
6248 * @param argv Standard argv without initial components.
6250 * @return Integer status (0 means success).
6253 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6256 struct cli_state
*cli
= NULL
;
6257 struct sockaddr_storage server_ss
;
6258 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6259 struct policy_handle connect_hnd
;
6260 TALLOC_CTX
*mem_ctx
;
6261 NTSTATUS nt_status
, result
;
6262 struct dom_sid
*domain_sid
;
6267 union lsa_PolicyInformation
*info
= NULL
;
6268 struct dcerpc_binding_handle
*b
;
6271 * Connect to \\server\ipc$ as 'our domain' account with password
6274 if (argc
!= 1 || c
->display_usage
) {
6277 _("net rpc trustdom establish <domain_name>\n"));
6281 domain_name
= smb_xstrdup(argv
[0]);
6282 if (!strupper_m(domain_name
)) {
6283 SAFE_FREE(domain_name
);
6287 /* account name used at first is our domain's name with '$' */
6288 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
6291 if (!strupper_m(acct_name
)) {
6292 SAFE_FREE(domain_name
);
6293 SAFE_FREE(acct_name
);
6298 * opt_workgroup will be used by connection functions further,
6299 * hence it should be set to remote domain name instead of ours
6301 if (c
->opt_workgroup
) {
6302 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6305 c
->opt_user_name
= acct_name
;
6307 /* find the domain controller */
6308 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6309 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6313 /* connect to ipc$ as username/password */
6314 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6315 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6317 /* Is it trusting domain account for sure ? */
6318 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6319 nt_errstr(nt_status
)));
6323 /* store who we connected to */
6325 saf_store( domain_name
, pdc_name
);
6328 * Connect to \\server\ipc$ again (this time anonymously)
6331 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6334 if (NT_STATUS_IS_ERR(nt_status
)) {
6335 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6336 domain_name
, nt_errstr(nt_status
)));
6340 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6341 "domain %s", domain_name
))) {
6342 DEBUG(0, ("talloc_init() failed\n"));
6347 /* Make sure we're talking to a proper server */
6349 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6350 if (!NT_STATUS_IS_OK(nt_status
)) {
6352 talloc_destroy(mem_ctx
);
6357 * Call LsaOpenPolicy and LsaQueryInfo
6360 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6362 if (!NT_STATUS_IS_OK(nt_status
)) {
6363 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6365 talloc_destroy(mem_ctx
);
6369 b
= pipe_hnd
->binding_handle
;
6371 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
6373 if (NT_STATUS_IS_ERR(nt_status
)) {
6374 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6375 nt_errstr(nt_status
)));
6377 talloc_destroy(mem_ctx
);
6381 /* Querying info level 5 */
6383 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6385 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6388 if (NT_STATUS_IS_ERR(nt_status
)) {
6389 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6390 nt_errstr(nt_status
)));
6392 talloc_destroy(mem_ctx
);
6395 if (NT_STATUS_IS_ERR(result
)) {
6396 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6397 nt_errstr(result
)));
6399 talloc_destroy(mem_ctx
);
6403 domain_sid
= info
->account_domain
.sid
;
6405 /* There should be actually query info level 3 (following nt serv behaviour),
6406 but I still don't know if it's _really_ necessary */
6409 * Store the password in secrets db
6412 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6413 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6415 talloc_destroy(mem_ctx
);
6420 * Close the pipes and clean up
6423 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6424 if (NT_STATUS_IS_ERR(nt_status
)) {
6425 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6426 nt_errstr(nt_status
)));
6428 talloc_destroy(mem_ctx
);
6434 talloc_destroy(mem_ctx
);
6436 d_printf(_("Trust to domain %s established\n"), domain_name
);
6441 * Revoke trust relationship to the remote domain.
6443 * @param c A net_context structure.
6444 * @param argc Standard argc.
6445 * @param argv Standard argv without initial components.
6447 * @return Integer status (0 means success).
6450 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6456 if (argc
< 1 || c
->display_usage
) {
6459 _("net rpc trustdom revoke <domain_name>\n"
6460 " Revoke trust relationship\n"
6461 " domain_name\tName of domain to revoke trust\n"));
6465 /* generate upper cased domain name */
6466 domain_name
= smb_xstrdup(argv
[0]);
6467 if (!strupper_m(domain_name
)) {
6468 SAFE_FREE(domain_name
);
6472 /* delete password of the trust */
6473 if (!pdb_del_trusteddom_pw(domain_name
)) {
6474 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6481 SAFE_FREE(domain_name
);
6485 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6486 const struct dom_sid
*domain_sid
,
6487 const char *domain_name
,
6488 struct cli_state
*cli
,
6489 struct rpc_pipe_client
*pipe_hnd
,
6490 TALLOC_CTX
*mem_ctx
,
6495 if (!sid_to_fstring(str_sid
, domain_sid
)) {
6496 return NT_STATUS_UNSUCCESSFUL
;
6498 d_printf("%s\n", str_sid
);
6499 return NT_STATUS_OK
;
6502 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
6506 /* convert sid into ascii string */
6507 sid_to_fstring(ascii_sid
, dom_sid
);
6509 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
6512 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6513 TALLOC_CTX
*mem_ctx
,
6514 struct policy_handle
*pol
,
6515 struct dom_sid dom_sid
,
6516 const char *trusted_dom_name
)
6518 NTSTATUS nt_status
, result
;
6519 union lsa_TrustedDomainInfo
*info
= NULL
;
6520 char *cleartextpwd
= NULL
;
6521 DATA_BLOB session_key
;
6522 DATA_BLOB data
= data_blob_null
;
6523 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6525 nt_status
= dcerpc_lsa_QueryTrustedDomainInfoBySid(b
, mem_ctx
,
6528 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6531 if (NT_STATUS_IS_ERR(nt_status
)) {
6532 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6533 nt_errstr(nt_status
)));
6536 if (NT_STATUS_IS_ERR(result
)) {
6538 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6539 nt_errstr(result
)));
6543 data
= data_blob(info
->password
.password
->data
,
6544 info
->password
.password
->length
);
6546 nt_status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
6547 if (!NT_STATUS_IS_OK(nt_status
)) {
6548 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status
)));
6552 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key
);
6553 data_blob_free(&session_key
);
6555 if (cleartextpwd
== NULL
) {
6556 DEBUG(0,("retrieved NULL password\n"));
6557 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6561 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6562 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6563 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6567 #ifdef DEBUG_PASSWORD
6568 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6569 "password: [%s]\n", trusted_dom_name
,
6570 sid_string_dbg(&dom_sid
), cleartextpwd
));
6574 SAFE_FREE(cleartextpwd
);
6575 data_blob_free(&data
);
6580 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6583 /* common variables */
6584 TALLOC_CTX
* mem_ctx
;
6585 struct cli_state
*cli
= NULL
;
6586 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6587 NTSTATUS nt_status
, result
;
6588 const char *domain_name
= NULL
;
6589 struct policy_handle connect_hnd
;
6590 union lsa_PolicyInformation
*info
= NULL
;
6592 /* trusted domains listing variables */
6593 unsigned int enum_ctx
= 0;
6595 struct lsa_DomainList dom_list
;
6597 struct dcerpc_binding_handle
*b
;
6599 if (c
->display_usage
) {
6601 "net rpc trustdom vampire\n"
6604 _("Vampire trust relationship from remote server"));
6609 * Listing trusted domains (stored in secrets.tdb, if local)
6612 mem_ctx
= talloc_init("trust relationships vampire");
6615 * set domain and pdc name to local samba server (default)
6616 * or to remote one given in command line
6619 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6620 domain_name
= c
->opt_workgroup
;
6621 c
->opt_target_workgroup
= c
->opt_workgroup
;
6623 fstrcpy(pdc_name
, lp_netbios_name());
6624 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6625 c
->opt_target_workgroup
= domain_name
;
6628 /* open \PIPE\lsarpc and open policy handle */
6629 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6630 if (!NT_STATUS_IS_OK(nt_status
)) {
6631 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6632 nt_errstr(nt_status
)));
6633 talloc_destroy(mem_ctx
);
6637 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6639 if (!NT_STATUS_IS_OK(nt_status
)) {
6640 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6641 nt_errstr(nt_status
) ));
6643 talloc_destroy(mem_ctx
);
6647 b
= pipe_hnd
->binding_handle
;
6649 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6651 if (NT_STATUS_IS_ERR(nt_status
)) {
6652 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6653 nt_errstr(nt_status
)));
6655 talloc_destroy(mem_ctx
);
6659 /* query info level 5 to obtain sid of a domain being queried */
6660 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6662 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6666 if (NT_STATUS_IS_ERR(nt_status
)) {
6667 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6668 nt_errstr(nt_status
)));
6670 talloc_destroy(mem_ctx
);
6673 if (NT_STATUS_IS_ERR(result
)) {
6674 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6675 nt_errstr(result
)));
6677 talloc_destroy(mem_ctx
);
6682 * Keep calling LsaEnumTrustdom over opened pipe until
6683 * the end of enumeration is reached
6686 d_printf(_("Vampire trusted domains:\n\n"));
6689 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6695 if (NT_STATUS_IS_ERR(nt_status
)) {
6696 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6697 nt_errstr(nt_status
)));
6699 talloc_destroy(mem_ctx
);
6702 if (NT_STATUS_IS_ERR(result
)) {
6704 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6705 nt_errstr(result
)));
6707 talloc_destroy(mem_ctx
);
6712 for (i
= 0; i
< dom_list
.count
; i
++) {
6714 print_trusted_domain(dom_list
.domains
[i
].sid
,
6715 dom_list
.domains
[i
].name
.string
);
6717 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6718 *dom_list
.domains
[i
].sid
,
6719 dom_list
.domains
[i
].name
.string
);
6720 if (!NT_STATUS_IS_OK(nt_status
)) {
6722 talloc_destroy(mem_ctx
);
6728 * in case of no trusted domains say something rather
6729 * than just display blank line
6731 if (!dom_list
.count
) d_printf(_("none\n"));
6733 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6735 /* close this connection before doing next one */
6736 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6737 if (NT_STATUS_IS_ERR(nt_status
)) {
6738 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6739 nt_errstr(nt_status
)));
6741 talloc_destroy(mem_ctx
);
6745 /* close lsarpc pipe and connection to IPC$ */
6748 talloc_destroy(mem_ctx
);
6752 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6754 /* common variables */
6755 TALLOC_CTX
* mem_ctx
;
6756 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6757 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6758 NTSTATUS nt_status
, result
;
6759 const char *domain_name
= NULL
;
6760 struct dom_sid
*queried_dom_sid
;
6761 int ascii_dom_name_len
;
6762 struct policy_handle connect_hnd
;
6763 union lsa_PolicyInformation
*info
= NULL
;
6764 struct dcerpc_binding_handle
*b
= NULL
;
6766 /* trusted domains listing variables */
6767 unsigned int num_domains
, enum_ctx
= 0;
6769 struct lsa_DomainList dom_list
;
6773 /* trusting domains listing variables */
6774 struct policy_handle domain_hnd
;
6775 struct samr_SamArray
*trusts
= NULL
;
6777 if (c
->display_usage
) {
6779 "net rpc trustdom list\n"
6782 _("List incoming and outgoing trust relationships"));
6787 * Listing trusted domains (stored in secrets.tdb, if local)
6790 mem_ctx
= talloc_init("trust relationships listing");
6793 * set domain and pdc name to local samba server (default)
6794 * or to remote one given in command line
6797 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6798 domain_name
= c
->opt_workgroup
;
6799 c
->opt_target_workgroup
= c
->opt_workgroup
;
6801 fstrcpy(pdc_name
, lp_netbios_name());
6802 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6803 c
->opt_target_workgroup
= domain_name
;
6806 /* open \PIPE\lsarpc and open policy handle */
6807 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6808 if (!NT_STATUS_IS_OK(nt_status
)) {
6809 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6810 nt_errstr(nt_status
)));
6811 talloc_destroy(mem_ctx
);
6815 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6817 if (!NT_STATUS_IS_OK(nt_status
)) {
6818 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6819 nt_errstr(nt_status
) ));
6821 talloc_destroy(mem_ctx
);
6825 b
= pipe_hnd
->binding_handle
;
6827 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6829 if (NT_STATUS_IS_ERR(nt_status
)) {
6830 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6831 nt_errstr(nt_status
)));
6833 talloc_destroy(mem_ctx
);
6837 /* query info level 5 to obtain sid of a domain being queried */
6838 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6840 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6844 if (NT_STATUS_IS_ERR(nt_status
)) {
6845 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6846 nt_errstr(nt_status
)));
6848 talloc_destroy(mem_ctx
);
6851 if (NT_STATUS_IS_ERR(result
)) {
6852 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6853 nt_errstr(result
)));
6855 talloc_destroy(mem_ctx
);
6859 queried_dom_sid
= info
->account_domain
.sid
;
6862 * Keep calling LsaEnumTrustdom over opened pipe until
6863 * the end of enumeration is reached
6866 d_printf(_("Trusted domains list:\n\n"));
6868 found_domain
= false;
6871 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6877 if (NT_STATUS_IS_ERR(nt_status
)) {
6878 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6879 nt_errstr(nt_status
)));
6881 talloc_destroy(mem_ctx
);
6884 if (NT_STATUS_IS_ERR(result
)) {
6885 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6886 nt_errstr(result
)));
6888 talloc_destroy(mem_ctx
);
6893 for (i
= 0; i
< dom_list
.count
; i
++) {
6894 print_trusted_domain(dom_list
.domains
[i
].sid
,
6895 dom_list
.domains
[i
].name
.string
);
6896 found_domain
= true;
6900 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6903 * in case of no trusted domains say something rather
6904 * than just display blank line
6906 if (!found_domain
) {
6907 d_printf(_("none\n"));
6910 /* close this connection before doing next one */
6911 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6912 if (NT_STATUS_IS_ERR(nt_status
)) {
6913 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6914 nt_errstr(nt_status
)));
6916 talloc_destroy(mem_ctx
);
6920 TALLOC_FREE(pipe_hnd
);
6923 * Listing trusting domains (stored in passdb backend, if local)
6926 d_printf(_("\nTrusting domains list:\n\n"));
6929 * Open \PIPE\samr and get needed policy handles
6931 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6933 if (!NT_STATUS_IS_OK(nt_status
)) {
6934 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6936 talloc_destroy(mem_ctx
);
6940 b
= pipe_hnd
->binding_handle
;
6943 nt_status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6945 SAMR_ACCESS_LOOKUP_DOMAIN
,
6948 if (!NT_STATUS_IS_OK(nt_status
)) {
6949 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6950 nt_errstr(nt_status
)));
6952 talloc_destroy(mem_ctx
);
6955 if (!NT_STATUS_IS_OK(result
)) {
6957 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6958 nt_errstr(result
)));
6960 talloc_destroy(mem_ctx
);
6964 /* SamrOpenDomain - we have to open domain policy handle in order to be
6965 able to enumerate accounts*/
6966 nt_status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6968 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6972 if (!NT_STATUS_IS_OK(nt_status
)) {
6973 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6974 nt_errstr(nt_status
)));
6976 talloc_destroy(mem_ctx
);
6979 if (!NT_STATUS_IS_OK(result
)) {
6981 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6982 nt_errstr(result
)));
6984 talloc_destroy(mem_ctx
);
6989 * perform actual enumeration
6992 found_domain
= false;
6994 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6997 nt_status
= dcerpc_samr_EnumDomainUsers(b
, mem_ctx
,
7005 if (NT_STATUS_IS_ERR(nt_status
)) {
7006 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7007 nt_errstr(nt_status
)));
7009 talloc_destroy(mem_ctx
);
7012 if (NT_STATUS_IS_ERR(result
)) {
7014 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
7015 nt_errstr(result
)));
7017 talloc_destroy(mem_ctx
);
7021 for (i
= 0; i
< num_domains
; i
++) {
7023 char *str
= discard_const_p(char, trusts
->entries
[i
].name
.string
);
7025 found_domain
= true;
7028 * get each single domain's sid (do we _really_ need this ?):
7029 * 1) connect to domain's pdc
7030 * 2) query the pdc for domain's sid
7033 /* get rid of '$' tail */
7034 ascii_dom_name_len
= strlen(str
);
7035 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
7036 str
[ascii_dom_name_len
- 1] = '\0';
7038 /* set opt_* variables to remote domain */
7039 if (!strupper_m(str
)) {
7041 talloc_destroy(mem_ctx
);
7044 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
7045 c
->opt_target_workgroup
= c
->opt_workgroup
;
7047 d_printf("%-20s", str
);
7049 /* connect to remote domain controller */
7050 nt_status
= net_make_ipc_connection(c
,
7051 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
7053 if (NT_STATUS_IS_OK(nt_status
)) {
7054 /* query for domain's sid */
7055 if (run_rpc_command(
7057 &ndr_table_lsarpc
, 0,
7058 rpc_query_domain_sid
, argc
,
7060 d_printf(_("strange - couldn't get domain's sid\n"));
7062 cli_shutdown(remote_cli
);
7065 d_fprintf(stderr
, _("domain controller is not "
7066 "responding: %s\n"),
7067 nt_errstr(nt_status
));
7068 d_printf(_("couldn't get domain's sid\n"));
7072 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
7074 if (!found_domain
) {
7078 /* close opened samr and domain policy handles */
7079 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &domain_hnd
, &result
);
7080 if (!NT_STATUS_IS_OK(nt_status
)) {
7081 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
7084 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &connect_hnd
, &result
);
7085 if (!NT_STATUS_IS_OK(nt_status
)) {
7086 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
7089 /* close samr pipe and connection to IPC$ */
7092 talloc_destroy(mem_ctx
);
7097 * Entrypoint for 'net rpc trustdom' code.
7099 * @param argc Standard argc.
7100 * @param argv Standard argv without initial components.
7102 * @return Integer status (0 means success).
7105 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
7107 struct functable func
[] = {
7112 N_("Add trusting domain's account"),
7113 N_("net rpc trustdom add\n"
7114 " Add trusting domain's account")
7120 N_("Remove trusting domain's account"),
7121 N_("net rpc trustdom del\n"
7122 " Remove trusting domain's account")
7126 rpc_trustdom_establish
,
7128 N_("Establish outgoing trust relationship"),
7129 N_("net rpc trustdom establish\n"
7130 " Establish outgoing trust relationship")
7134 rpc_trustdom_revoke
,
7136 N_("Revoke outgoing trust relationship"),
7137 N_("net rpc trustdom revoke\n"
7138 " Revoke outgoing trust relationship")
7144 N_("List in- and outgoing domain trusts"),
7145 N_("net rpc trustdom list\n"
7146 " List in- and outgoing domain trusts")
7150 rpc_trustdom_vampire
,
7152 N_("Vampire trusts from remote server"),
7153 N_("net rpc trustdom vampire\n"
7154 " Vampire trusts from remote server")
7156 {NULL
, NULL
, 0, NULL
, NULL
}
7159 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
7163 * Check if a server will take rpc commands
7164 * @param flags Type of server to connect to (PDC, DMB, localhost)
7165 * if the host is not explicitly specified
7166 * @return bool (true means rpc supported)
7168 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
7170 struct cli_state
*cli
;
7172 struct sockaddr_storage server_ss
;
7173 char *server_name
= NULL
;
7176 /* flags (i.e. server type) may depend on command */
7177 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
7180 status
= cli_connect_nb(server_name
, &server_ss
, 0, 0x20,
7181 lp_netbios_name(), SMB_SIGNING_DEFAULT
,
7183 if (!NT_STATUS_IS_OK(status
)) {
7186 status
= smbXcli_negprot(cli
->conn
, cli
->timeout
, PROTOCOL_CORE
,
7188 if (!NT_STATUS_IS_OK(status
))
7190 if (smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_NT1
)
7199 /* dump sam database via samsync rpc calls */
7200 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
7201 if (c
->display_usage
) {
7206 _("Dump remote SAM database"));
7210 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
,
7211 NET_FLAGS_ANONYMOUS
,
7212 rpc_samdump_internals
, argc
, argv
);
7215 /* syncronise sam database via samsync rpc calls */
7216 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
7218 struct functable func
[] = {
7223 N_("Dump remote SAM database to ldif"),
7224 N_("net rpc vampire ldif\n"
7225 " Dump remote SAM database to LDIF file or "
7232 N_("Dump remote SAM database to Kerberos Keytab"),
7233 N_("net rpc vampire keytab\n"
7234 " Dump remote SAM database to Kerberos keytab "
7241 N_("Dump remote SAM database to passdb"),
7242 N_("net rpc vampire passdb\n"
7243 " Dump remote SAM database to passdb")
7246 {NULL
, NULL
, 0, NULL
, NULL
}
7250 if (c
->display_usage
) {
7255 _("Vampire remote SAM database"));
7259 return rpc_vampire_passdb(c
, argc
, argv
);
7262 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
7266 * Migrate everything from a print server.
7268 * @param c A net_context structure.
7269 * @param argc Standard main() style argc.
7270 * @param argv Standard main() style argv. Initial components are already
7273 * @return A shell status integer (0 for success).
7275 * The order is important !
7276 * To successfully add drivers the print queues have to exist !
7277 * Applying ACLs should be the last step, because you're easily locked out.
7280 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
7285 if (c
->display_usage
) {
7287 "net rpc printer migrate all\n"
7290 _("Migrate everything from a print server"));
7295 d_printf(_("no server to migrate\n"));
7299 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7300 rpc_printer_migrate_printers_internals
, argc
,
7305 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7306 rpc_printer_migrate_drivers_internals
, argc
,
7311 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7312 rpc_printer_migrate_forms_internals
, argc
, argv
);
7316 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7317 rpc_printer_migrate_settings_internals
, argc
,
7322 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7323 rpc_printer_migrate_security_internals
, argc
,
7329 * Migrate print drivers from a print server.
7331 * @param c A net_context structure.
7332 * @param argc Standard main() style argc.
7333 * @param argv Standard main() style argv. Initial components are already
7336 * @return A shell status integer (0 for success).
7338 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
7341 if (c
->display_usage
) {
7343 "net rpc printer migrate drivers\n"
7346 _("Migrate print-drivers from a print-server"));
7351 d_printf(_("no server to migrate\n"));
7355 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7356 rpc_printer_migrate_drivers_internals
,
7361 * Migrate print-forms from a print-server.
7363 * @param c A net_context structure.
7364 * @param argc Standard main() style argc.
7365 * @param argv Standard main() style argv. Initial components are already
7368 * @return A shell status integer (0 for success).
7370 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7373 if (c
->display_usage
) {
7375 "net rpc printer migrate forms\n"
7378 _("Migrate print-forms from a print-server"));
7383 d_printf(_("no server to migrate\n"));
7387 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7388 rpc_printer_migrate_forms_internals
,
7393 * Migrate printers from a print-server.
7395 * @param c A net_context structure.
7396 * @param argc Standard main() style argc.
7397 * @param argv Standard main() style argv. Initial components are already
7400 * @return A shell status integer (0 for success).
7402 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7405 if (c
->display_usage
) {
7407 "net rpc printer migrate printers\n"
7410 _("Migrate printers from a print-server"));
7415 d_printf(_("no server to migrate\n"));
7419 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7420 rpc_printer_migrate_printers_internals
,
7425 * Migrate printer-ACLs from a print-server
7427 * @param c A net_context structure.
7428 * @param argc Standard main() style argc.
7429 * @param argv Standard main() style argv. Initial components are already
7432 * @return A shell status integer (0 for success).
7434 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7437 if (c
->display_usage
) {
7439 "net rpc printer migrate security\n"
7442 _("Migrate printer-ACLs from a print-server"));
7447 d_printf(_("no server to migrate\n"));
7451 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7452 rpc_printer_migrate_security_internals
,
7457 * Migrate printer-settings from a print-server.
7459 * @param c A net_context structure.
7460 * @param argc Standard main() style argc.
7461 * @param argv Standard main() style argv. Initial components are already
7464 * @return A shell status integer (0 for success).
7466 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7469 if (c
->display_usage
) {
7471 "net rpc printer migrate settings\n"
7474 _("Migrate printer-settings from a "
7480 d_printf(_("no server to migrate\n"));
7484 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7485 rpc_printer_migrate_settings_internals
,
7490 * 'net rpc printer' entrypoint.
7492 * @param c A net_context structure.
7493 * @param argc Standard main() style argc.
7494 * @param argv Standard main() style argv. Initial components are already
7498 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7501 /* ouch: when addriver and setdriver are called from within
7502 rpc_printer_migrate_drivers_internals, the printer-queue already
7505 struct functable func
[] = {
7508 rpc_printer_migrate_all
,
7510 N_("Migrate all from remote to local print server"),
7511 N_("net rpc printer migrate all\n"
7512 " Migrate all from remote to local print server")
7516 rpc_printer_migrate_drivers
,
7518 N_("Migrate drivers to local server"),
7519 N_("net rpc printer migrate drivers\n"
7520 " Migrate drivers to local server")
7524 rpc_printer_migrate_forms
,
7526 N_("Migrate froms to local server"),
7527 N_("net rpc printer migrate forms\n"
7528 " Migrate froms to local server")
7532 rpc_printer_migrate_printers
,
7534 N_("Migrate printers to local server"),
7535 N_("net rpc printer migrate printers\n"
7536 " Migrate printers to local server")
7540 rpc_printer_migrate_security
,
7542 N_("Mirgate printer ACLs to local server"),
7543 N_("net rpc printer migrate security\n"
7544 " Mirgate printer ACLs to local server")
7548 rpc_printer_migrate_settings
,
7550 N_("Migrate printer settings to local server"),
7551 N_("net rpc printer migrate settings\n"
7552 " Migrate printer settings to local server")
7554 {NULL
, NULL
, 0, NULL
, NULL
}
7557 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7562 * List printers on a remote RPC server.
7564 * @param c A net_context structure.
7565 * @param argc Standard main() style argc.
7566 * @param argv Standard main() style argv. Initial components are already
7569 * @return A shell status integer (0 for success).
7571 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7573 if (c
->display_usage
) {
7575 "net rpc printer list\n"
7578 _("List printers on a remote RPC server"));
7582 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7583 rpc_printer_list_internals
,
7588 * List printer-drivers on a remote RPC server.
7590 * @param c A net_context structure.
7591 * @param argc Standard main() style argc.
7592 * @param argv Standard main() style argv. Initial components are already
7595 * @return A shell status integer (0 for success).
7597 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7600 if (c
->display_usage
) {
7602 "net rpc printer driver\n"
7605 _("List printer-drivers on a remote RPC server"));
7609 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7610 rpc_printer_driver_list_internals
,
7615 * Publish printer in ADS via MSRPC.
7617 * @param c A net_context structure.
7618 * @param argc Standard main() style argc.
7619 * @param argv Standard main() style argv. Initial components are already
7622 * @return A shell status integer (0 for success).
7624 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7627 if (c
->display_usage
) {
7629 "net rpc printer publish publish\n"
7632 _("Publish printer in ADS via MSRPC"));
7636 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7637 rpc_printer_publish_publish_internals
,
7642 * Update printer in ADS via MSRPC.
7644 * @param c A net_context structure.
7645 * @param argc Standard main() style argc.
7646 * @param argv Standard main() style argv. Initial components are already
7649 * @return A shell status integer (0 for success).
7651 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7653 if (c
->display_usage
) {
7655 "net rpc printer publish update\n"
7658 _("Update printer in ADS via MSRPC"));
7662 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7663 rpc_printer_publish_update_internals
,
7668 * UnPublish printer in ADS via MSRPC.
7670 * @param c A net_context structure.
7671 * @param argc Standard main() style argc.
7672 * @param argv Standard main() style argv. Initial components are already
7675 * @return A shell status integer (0 for success).
7677 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7680 if (c
->display_usage
) {
7682 "net rpc printer publish unpublish\n"
7685 _("UnPublish printer in ADS via MSRPC"));
7689 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7690 rpc_printer_publish_unpublish_internals
,
7695 * List published printers via MSRPC.
7697 * @param c A net_context structure.
7698 * @param argc Standard main() style argc.
7699 * @param argv Standard main() style argv. Initial components are already
7702 * @return A shell status integer (0 for success).
7704 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7707 if (c
->display_usage
) {
7709 "net rpc printer publish list\n"
7712 _("List published printers via MSRPC"));
7716 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7717 rpc_printer_publish_list_internals
,
7723 * Publish printer in ADS.
7725 * @param c A net_context structure.
7726 * @param argc Standard main() style argc.
7727 * @param argv Standard main() style argv. Initial components are already
7730 * @return A shell status integer (0 for success).
7732 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7736 struct functable func
[] = {
7739 rpc_printer_publish_publish
,
7741 N_("Publish printer in AD"),
7742 N_("net rpc printer publish publish\n"
7743 " Publish printer in AD")
7747 rpc_printer_publish_update
,
7749 N_("Update printer in AD"),
7750 N_("net rpc printer publish update\n"
7751 " Update printer in AD")
7755 rpc_printer_publish_unpublish
,
7757 N_("Unpublish printer"),
7758 N_("net rpc printer publish unpublish\n"
7759 " Unpublish printer")
7763 rpc_printer_publish_list
,
7765 N_("List published printers"),
7766 N_("net rpc printer publish list\n"
7767 " List published printers")
7769 {NULL
, NULL
, 0, NULL
, NULL
}
7773 if (c
->display_usage
) {
7774 d_printf(_("Usage:\n"));
7775 d_printf(_("net rpc printer publish\n"
7776 " List published printers\n"
7777 " Alias of net rpc printer publish "
7779 net_display_usage_from_functable(func
);
7782 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7783 rpc_printer_publish_list_internals
,
7787 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7793 * Display rpc printer help page.
7795 * @param c A net_context structure.
7796 * @param argc Standard main() style argc.
7797 * @param argv Standard main() style argv. Initial components are already
7800 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7802 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7803 "\tlists all printers on print-server\n\n"));
7804 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7805 "\tlists all printer-drivers on print-server\n\n"));
7806 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7807 "\tpublishes printer settings in Active Directory\n"
7808 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7809 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7810 "\n\tmigrates printers from remote to local server\n\n"));
7811 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7812 "\n\tmigrates printer-settings from remote to local server\n\n"));
7813 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7814 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7815 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7816 "\n\tmigrates printer-forms from remote to local server\n\n"));
7817 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7818 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7819 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7820 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7821 "\tremote to local print-server\n\n"));
7822 net_common_methods_usage(c
, argc
, argv
);
7823 net_common_flags_usage(c
, argc
, argv
);
7825 "\t-v or --verbose\t\t\tgive verbose output\n"
7826 "\t --destination\t\tmigration target server (default: localhost)\n"));
7832 * 'net rpc printer' entrypoint.
7834 * @param c A net_context structure.
7835 * @param argc Standard main() style argc.
7836 * @param argv Standard main() style argv. Initial components are already
7839 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7841 struct functable func
[] = {
7846 N_("List all printers on print server"),
7847 N_("net rpc printer list\n"
7848 " List all printers on print server")
7852 rpc_printer_migrate
,
7854 N_("Migrate printer to local server"),
7855 N_("net rpc printer migrate\n"
7856 " Migrate printer to local server")
7860 rpc_printer_driver_list
,
7862 N_("List printer drivers"),
7863 N_("net rpc printer driver\n"
7864 " List printer drivers")
7868 rpc_printer_publish
,
7870 N_("Publish printer in AD"),
7871 N_("net rpc printer publish\n"
7872 " Publish printer in AD")
7874 {NULL
, NULL
, 0, NULL
, NULL
}
7878 if (c
->display_usage
) {
7879 d_printf(_("Usage:\n"));
7880 d_printf(_("net rpc printer\n"
7881 " List printers\n"));
7882 net_display_usage_from_functable(func
);
7885 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7886 rpc_printer_list_internals
,
7890 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7894 * 'net rpc' entrypoint.
7896 * @param c A net_context structure.
7897 * @param argc Standard main() style argc.
7898 * @param argv Standard main() style argv. Initial components are already
7902 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7904 NET_API_STATUS status
;
7906 struct functable func
[] = {
7911 N_("Modify global audit settings"),
7912 N_("net rpc audit\n"
7913 " Modify global audit settings")
7919 N_("Show basic info about a domain"),
7921 " Show basic info about a domain")
7927 N_("Join a domain"),
7935 N_("Join a domain created in server manager"),
7936 N_("net rpc oldjoin\n"
7937 " Join a domain created in server manager")
7943 N_("Test that a join is valid"),
7944 N_("net rpc testjoin\n"
7945 " Test that a join is valid")
7951 N_("List/modify users"),
7953 " List/modify users")
7959 N_("Change a user password"),
7960 N_("net rpc password\n"
7961 " Change a user password\n"
7962 " Alias for net rpc user password")
7968 N_("List/modify groups"),
7969 N_("net rpc group\n"
7970 " List/modify groups")
7976 N_("List/modify shares"),
7977 N_("net rpc share\n"
7978 " List/modify shares")
7984 N_("List open files"),
7992 N_("List/modify printers"),
7993 N_("net rpc printer\n"
7994 " List/modify printers")
7998 net_rpc_changetrustpw
,
8000 N_("Change trust account password"),
8001 N_("net rpc changetrustpw\n"
8002 " Change trust account password")
8008 N_("Modify domain trusts"),
8009 N_("net rpc trustdom\n"
8010 " Modify domain trusts")
8016 N_("Abort a remote shutdown"),
8017 N_("net rpc abortshutdown\n"
8018 " Abort a remote shutdown")
8024 N_("Shutdown a remote server"),
8025 N_("net rpc shutdown\n"
8026 " Shutdown a remote server")
8032 N_("Dump SAM data of remote NT PDC"),
8033 N_("net rpc samdump\n"
8034 " Dump SAM data of remote NT PDC")
8040 N_("Sync a remote NT PDC's data into local passdb"),
8041 N_("net rpc vampire\n"
8042 " Sync a remote NT PDC's data into local passdb")
8048 N_("Fetch the domain sid into local secrets.tdb"),
8049 N_("net rpc getsid\n"
8050 " Fetch the domain sid into local secrets.tdb")
8056 N_("Manage privileges assigned to SID"),
8057 N_("net rpc rights\n"
8058 " Manage privileges assigned to SID")
8064 N_("Start/stop/query remote services"),
8065 N_("net rpc service\n"
8066 " Start/stop/query remote services")
8072 N_("Manage registry hives"),
8073 N_("net rpc registry\n"
8074 " Manage registry hives")
8080 N_("Open interactive shell on remote server"),
8081 N_("net rpc shell\n"
8082 " Open interactive shell on remote server")
8088 N_("Manage trusts"),
8089 N_("net rpc trust\n"
8096 N_("Configure a remote samba server"),
8098 " Configure a remote samba server")
8100 {NULL
, NULL
, 0, NULL
, NULL
}
8103 status
= libnetapi_net_init(&c
->netapi_ctx
);
8107 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
8108 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
8109 if (c
->opt_kerberos
) {
8110 libnetapi_set_use_kerberos(c
->netapi_ctx
);
8112 if (c
->opt_ccache
) {
8113 libnetapi_set_use_ccache(c
->netapi_ctx
);
8116 return net_run_function(c
, argc
, argv
, "net rpc", func
);