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];
890 ret
= asprintf(&prompt
, _("Enter new password for %s:"),
896 ret
= samba_getpass(prompt
, pwd
, sizeof(pwd
), false, false);
902 u1003
.usri1003_password
= talloc_strdup(c
, pwd
);
903 if (u1003
.usri1003_password
== NULL
) {
908 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
910 /* Display results */
913 _("Failed to set password for '%s' with error: %s.\n"),
914 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
923 * List a user's groups from a remote RPC server.
925 * @param argc Standard main() style argc.
926 * @param argv Standard main() style argv. Initial components are already
929 * @return A shell status integer (0 for success)
932 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
935 NET_API_STATUS status
;
936 struct GROUP_USERS_INFO_0
*u0
= NULL
;
937 uint32_t entries_read
= 0;
938 uint32_t total_entries
= 0;
942 if (argc
< 1 || c
->display_usage
) {
943 rpc_user_usage(c
, argc
, argv
);
947 status
= NetUserGetGroups(c
->opt_host
,
950 (uint8_t **)(void *)&u0
,
956 _("Failed to get groups for '%s' with error: %s.\n"),
957 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
962 for (i
=0; i
< entries_read
; i
++) {
963 printf("%s\n", u0
->grui0_name
);
971 * List users on a remote RPC server.
973 * All parameters are provided by the run_rpc_command function, except for
974 * argc, argv which are passed through.
976 * @param domain_sid The domain sid acquired from the remote server.
977 * @param cli A cli_state connected to the server.
978 * @param mem_ctx Talloc context, destroyed on completion of the function.
979 * @param argc Standard main() style argc.
980 * @param argv Standard main() style argv. Initial components are already
983 * @return Normal NTSTATUS return.
986 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
988 NET_API_STATUS status
;
989 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
990 struct NET_DISPLAY_USER
*info
= NULL
;
993 /* Query domain users */
994 if (c
->opt_long_list_entries
)
995 d_printf(_("\nUser name Comment"
996 "\n-----------------------------\n"));
998 uint32_t max_entries
, max_size
;
1000 dcerpc_get_query_dispinfo_params(
1001 loop_count
, &max_entries
, &max_size
);
1003 status
= NetQueryDisplayInformation(c
->opt_host
,
1010 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
1014 info
= (struct NET_DISPLAY_USER
*)buffer
;
1016 for (i
= 0; i
< num_entries
; i
++) {
1018 if (c
->opt_long_list_entries
)
1019 printf("%-21.21s %s\n", info
->usri1_name
,
1020 info
->usri1_comment
);
1022 printf("%s\n", info
->usri1_name
);
1026 NetApiBufferFree(buffer
);
1029 start_idx
+= num_entries
;
1031 } while (status
== ERROR_MORE_DATA
);
1037 * 'net rpc user' entrypoint.
1038 * @param argc Standard main() style argc.
1039 * @param argv Standard main() style argv. Initial components are already
1043 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
1045 NET_API_STATUS status
;
1047 struct functable func
[] = {
1052 N_("Add specified user"),
1053 N_("net rpc user add\n"
1054 " Add specified user")
1060 N_("List domain groups of user"),
1061 N_("net rpc user info\n"
1062 " List domain groups of user")
1068 N_("Remove specified user"),
1069 N_("net rpc user delete\n"
1070 " Remove specified user")
1076 N_("Change user password"),
1077 N_("net rpc user password\n"
1078 " Change user password")
1084 N_("Rename specified user"),
1085 N_("net rpc user rename\n"
1086 " Rename specified user")
1090 rpc_user_setprimarygroup
,
1092 "Set a user's primary group",
1093 "net rpc user setprimarygroup\n"
1094 " Set a user's primary group"
1096 {NULL
, NULL
, 0, NULL
, NULL
}
1099 status
= libnetapi_net_init(&c
->netapi_ctx
);
1103 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1104 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1105 if (c
->opt_kerberos
) {
1106 libnetapi_set_use_kerberos(c
->netapi_ctx
);
1110 if (c
->display_usage
) {
1115 _("List all users"));
1116 net_display_usage_from_functable(func
);
1120 return rpc_user_list(c
, argc
, argv
);
1123 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
1126 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1127 TALLOC_CTX
*mem_ctx
,
1128 struct rpc_sh_ctx
*ctx
,
1129 struct rpc_pipe_client
*pipe_hnd
,
1130 int argc
, const char **argv
)
1132 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
1135 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1136 TALLOC_CTX
*mem_ctx
,
1137 struct rpc_sh_ctx
*ctx
,
1138 struct rpc_pipe_client
*pipe_hnd
,
1139 int argc
, const char **argv
)
1141 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
1144 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1145 TALLOC_CTX
*mem_ctx
,
1146 struct rpc_sh_ctx
*ctx
,
1147 struct rpc_pipe_client
*pipe_hnd
,
1148 int argc
, const char **argv
,
1150 struct net_context
*c
,
1151 TALLOC_CTX
*mem_ctx
,
1152 struct rpc_sh_ctx
*ctx
,
1153 struct rpc_pipe_client
*pipe_hnd
,
1154 struct policy_handle
*user_hnd
,
1155 int argc
, const char **argv
))
1157 struct policy_handle connect_pol
, domain_pol
, user_pol
;
1158 NTSTATUS status
, result
;
1161 enum lsa_SidType type
;
1162 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1165 d_fprintf(stderr
, "%s %s <username>\n", _("Usage:"),
1167 return NT_STATUS_INVALID_PARAMETER
;
1170 ZERO_STRUCT(connect_pol
);
1171 ZERO_STRUCT(domain_pol
);
1172 ZERO_STRUCT(user_pol
);
1174 status
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1175 argv
[0], NULL
, NULL
, &sid
, &type
);
1176 if (!NT_STATUS_IS_OK(status
)) {
1177 d_fprintf(stderr
, _("Could not lookup %s: %s\n"), argv
[0],
1182 if (type
!= SID_NAME_USER
) {
1183 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
1184 sid_type_lookup(type
));
1185 status
= NT_STATUS_NO_SUCH_USER
;
1189 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1190 d_fprintf(stderr
, _("%s is not in our domain\n"), argv
[0]);
1191 status
= NT_STATUS_NO_SUCH_USER
;
1195 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1197 MAXIMUM_ALLOWED_ACCESS
,
1200 if (!NT_STATUS_IS_OK(status
)) {
1203 if (!NT_STATUS_IS_OK(result
)) {
1208 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1210 MAXIMUM_ALLOWED_ACCESS
,
1214 if (!NT_STATUS_IS_OK(status
)) {
1217 if (!NT_STATUS_IS_OK(result
)) {
1222 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1224 MAXIMUM_ALLOWED_ACCESS
,
1228 if (!NT_STATUS_IS_OK(status
)) {
1231 if (!NT_STATUS_IS_OK(result
)) {
1236 status
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1239 if (is_valid_policy_hnd(&user_pol
)) {
1240 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1242 if (is_valid_policy_hnd(&domain_pol
)) {
1243 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1245 if (is_valid_policy_hnd(&connect_pol
)) {
1246 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
1251 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1252 TALLOC_CTX
*mem_ctx
,
1253 struct rpc_sh_ctx
*ctx
,
1254 struct rpc_pipe_client
*pipe_hnd
,
1255 struct policy_handle
*user_hnd
,
1256 int argc
, const char **argv
)
1258 NTSTATUS status
, result
;
1259 union samr_UserInfo
*info
= NULL
;
1260 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1263 d_fprintf(stderr
, "%s %s show <username>\n", _("Usage:"),
1265 return NT_STATUS_INVALID_PARAMETER
;
1268 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1273 if (!NT_STATUS_IS_OK(status
)) {
1276 if (!NT_STATUS_IS_OK(result
)) {
1280 d_printf(_("user rid: %d, group rid: %d\n"),
1282 info
->info21
.primary_gid
);
1287 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1288 TALLOC_CTX
*mem_ctx
,
1289 struct rpc_sh_ctx
*ctx
,
1290 struct rpc_pipe_client
*pipe_hnd
,
1291 int argc
, const char **argv
)
1293 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1294 rpc_sh_user_show_internals
);
1297 #define FETCHSTR(name, rec) \
1298 do { if (strequal(ctx->thiscmd, name)) { \
1299 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1302 #define SETSTR(name, rec, flag) \
1303 do { if (strequal(ctx->thiscmd, name)) { \
1304 init_lsa_String(&(info->info21.rec), argv[0]); \
1305 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1308 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1309 TALLOC_CTX
*mem_ctx
,
1310 struct rpc_sh_ctx
*ctx
,
1311 struct rpc_pipe_client
*pipe_hnd
,
1312 struct policy_handle
*user_hnd
,
1313 int argc
, const char **argv
)
1315 NTSTATUS status
, result
;
1316 const char *username
;
1317 const char *oldval
= "";
1318 union samr_UserInfo
*info
= NULL
;
1319 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1322 d_fprintf(stderr
, "%s %s <username> [new value|NULL]\n",
1323 _("Usage:"), ctx
->whoami
);
1324 return NT_STATUS_INVALID_PARAMETER
;
1327 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1332 if (!NT_STATUS_IS_OK(status
)) {
1335 if (!NT_STATUS_IS_OK(result
)) {
1339 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1341 FETCHSTR("fullname", full_name
);
1342 FETCHSTR("homedir", home_directory
);
1343 FETCHSTR("homedrive", home_drive
);
1344 FETCHSTR("logonscript", logon_script
);
1345 FETCHSTR("profilepath", profile_path
);
1346 FETCHSTR("description", description
);
1349 d_printf(_("%s's %s: [%s]\n"), username
, ctx
->thiscmd
, oldval
);
1353 if (strcmp(argv
[0], "NULL") == 0) {
1357 ZERO_STRUCT(info
->info21
);
1359 SETSTR("fullname", full_name
, FULL_NAME
);
1360 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1361 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1362 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1363 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1364 SETSTR("description", description
, DESCRIPTION
);
1366 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1371 if (!NT_STATUS_IS_OK(status
)) {
1377 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username
,
1378 ctx
->thiscmd
, oldval
, argv
[0]);
1385 #define HANDLEFLG(name, rec) \
1386 do { if (strequal(ctx->thiscmd, name)) { \
1387 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1389 newflags = oldflags | ACB_##rec; \
1391 newflags = oldflags & ~ACB_##rec; \
1394 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1395 TALLOC_CTX
*mem_ctx
,
1396 struct rpc_sh_ctx
*ctx
,
1397 struct rpc_pipe_client
*pipe_hnd
,
1398 int argc
, const char **argv
)
1400 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1401 rpc_sh_user_str_edit_internals
);
1404 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1405 TALLOC_CTX
*mem_ctx
,
1406 struct rpc_sh_ctx
*ctx
,
1407 struct rpc_pipe_client
*pipe_hnd
,
1408 struct policy_handle
*user_hnd
,
1409 int argc
, const char **argv
)
1411 NTSTATUS status
, result
;
1412 const char *username
;
1413 const char *oldval
= "unknown";
1414 uint32 oldflags
, newflags
;
1416 union samr_UserInfo
*info
= NULL
;
1417 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1420 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1421 !strequal(argv
[0], "no"))) {
1422 /* TRANSATORS: The yes|no here are program keywords. Please do
1424 d_fprintf(stderr
, _("Usage: %s <username> [yes|no]\n"),
1426 return NT_STATUS_INVALID_PARAMETER
;
1429 newval
= strequal(argv
[0], "yes");
1431 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1436 if (!NT_STATUS_IS_OK(status
)) {
1439 if (!NT_STATUS_IS_OK(result
)) {
1443 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1444 oldflags
= info
->info21
.acct_flags
;
1445 newflags
= info
->info21
.acct_flags
;
1447 HANDLEFLG("disabled", DISABLED
);
1448 HANDLEFLG("pwnotreq", PWNOTREQ
);
1449 HANDLEFLG("autolock", AUTOLOCK
);
1450 HANDLEFLG("pwnoexp", PWNOEXP
);
1453 d_printf(_("%s's %s flag: %s\n"), username
, ctx
->thiscmd
,
1458 ZERO_STRUCT(info
->info21
);
1460 info
->info21
.acct_flags
= newflags
;
1461 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1463 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1468 if (!NT_STATUS_IS_OK(status
)) {
1472 if (NT_STATUS_IS_OK(result
)) {
1473 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username
,
1474 ctx
->thiscmd
, oldval
, argv
[0]);
1482 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1483 TALLOC_CTX
*mem_ctx
,
1484 struct rpc_sh_ctx
*ctx
,
1485 struct rpc_pipe_client
*pipe_hnd
,
1486 int argc
, const char **argv
)
1488 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1489 rpc_sh_user_flag_edit_internals
);
1492 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1493 TALLOC_CTX
*mem_ctx
,
1494 struct rpc_sh_ctx
*ctx
)
1496 static struct rpc_sh_cmd cmds
[] = {
1498 { "fullname", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1499 N_("Show/Set a user's full name") },
1501 { "homedir", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1502 N_("Show/Set a user's home directory") },
1504 { "homedrive", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1505 N_("Show/Set a user's home drive") },
1507 { "logonscript", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1508 N_("Show/Set a user's logon script") },
1510 { "profilepath", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1511 N_("Show/Set a user's profile path") },
1513 { "description", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1514 N_("Show/Set a user's description") },
1516 { "disabled", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1517 N_("Show/Set whether a user is disabled") },
1519 { "autolock", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1520 N_("Show/Set whether a user locked out") },
1522 { "pwnotreq", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1523 N_("Show/Set whether a user does not need a password") },
1525 { "pwnoexp", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1526 N_("Show/Set whether a user's password does not expire") },
1528 { NULL
, NULL
, 0, NULL
, NULL
}
1534 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1535 TALLOC_CTX
*mem_ctx
,
1536 struct rpc_sh_ctx
*ctx
)
1538 static struct rpc_sh_cmd cmds
[] = {
1540 { "list", NULL
, &ndr_table_samr
, rpc_sh_user_list
,
1541 N_("List available users") },
1543 { "info", NULL
, &ndr_table_samr
, rpc_sh_user_info
,
1544 N_("List the domain groups a user is member of") },
1546 { "show", NULL
, &ndr_table_samr
, rpc_sh_user_show
,
1547 N_("Show info about a user") },
1549 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1550 N_("Show/Modify a user's fields") },
1552 { NULL
, NULL
, 0, NULL
, NULL
}
1558 /****************************************************************************/
1561 * Basic usage function for 'net rpc group'.
1562 * @param argc Standard main() style argc.
1563 * @param argv Standard main() style argv. Initial components are already
1567 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1569 return net_group_usage(c
, argc
, argv
);
1573 * Delete group on a remote RPC server.
1575 * All parameters are provided by the run_rpc_command function, except for
1576 * argc, argv which are passed through.
1578 * @param domain_sid The domain sid acquired from the remote server.
1579 * @param cli A cli_state connected to the server.
1580 * @param mem_ctx Talloc context, destroyed on completion of the function.
1581 * @param argc Standard main() style argc.
1582 * @param argv Standard main() style argv. Initial components are already
1585 * @return Normal NTSTATUS return.
1588 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1589 const struct dom_sid
*domain_sid
,
1590 const char *domain_name
,
1591 struct cli_state
*cli
,
1592 struct rpc_pipe_client
*pipe_hnd
,
1593 TALLOC_CTX
*mem_ctx
,
1597 struct policy_handle connect_pol
, domain_pol
, group_pol
, user_pol
;
1598 bool group_is_primary
= false;
1599 NTSTATUS status
, result
;
1601 struct samr_RidAttrArray
*rids
= NULL
;
1604 /* struct samr_RidWithAttribute *user_gids; */
1605 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1607 struct samr_Ids group_rids
, name_types
;
1608 struct lsa_String lsa_acct_name
;
1609 union samr_UserInfo
*info
= NULL
;
1611 if (argc
< 1 || c
->display_usage
) {
1612 rpc_group_usage(c
, argc
,argv
);
1613 return NT_STATUS_OK
; /* ok? */
1616 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1618 MAXIMUM_ALLOWED_ACCESS
,
1621 if (!NT_STATUS_IS_OK(status
)) {
1622 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1626 if (!NT_STATUS_IS_OK(result
)) {
1628 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1632 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1634 MAXIMUM_ALLOWED_ACCESS
,
1635 discard_const_p(struct dom_sid2
, domain_sid
),
1638 if (!NT_STATUS_IS_OK(status
)) {
1639 d_fprintf(stderr
, _("Request open_domain failed\n"));
1643 if (!NT_STATUS_IS_OK(result
)) {
1645 d_fprintf(stderr
, _("Request open_domain failed\n"));
1649 init_lsa_String(&lsa_acct_name
, argv
[0]);
1651 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1658 if (!NT_STATUS_IS_OK(status
)) {
1659 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1663 if (!NT_STATUS_IS_OK(result
)) {
1665 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1669 switch (name_types
.ids
[0])
1671 case SID_NAME_DOM_GRP
:
1672 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
1674 MAXIMUM_ALLOWED_ACCESS
,
1678 if (!NT_STATUS_IS_OK(status
)) {
1679 d_fprintf(stderr
, _("Request open_group failed"));
1683 if (!NT_STATUS_IS_OK(result
)) {
1685 d_fprintf(stderr
, _("Request open_group failed"));
1689 group_rid
= group_rids
.ids
[0];
1691 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
1695 if (!NT_STATUS_IS_OK(status
)) {
1697 _("Unable to query group members of %s"),
1702 if (!NT_STATUS_IS_OK(result
)) {
1705 _("Unable to query group members of %s"),
1710 if (c
->opt_verbose
) {
1712 _("Domain Group %s (rid: %d) has %d members\n"),
1713 argv
[0],group_rid
, rids
->count
);
1716 /* Check if group is anyone's primary group */
1717 for (i
= 0; i
< rids
->count
; i
++)
1719 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1721 MAXIMUM_ALLOWED_ACCESS
,
1725 if (!NT_STATUS_IS_OK(status
)) {
1727 _("Unable to open group member %d\n"),
1732 if (!NT_STATUS_IS_OK(result
)) {
1735 _("Unable to open group member %d\n"),
1740 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1745 if (!NT_STATUS_IS_OK(status
)) {
1747 _("Unable to lookup userinfo for group "
1753 if (!NT_STATUS_IS_OK(result
)) {
1756 _("Unable to lookup userinfo for group "
1762 if (info
->info21
.primary_gid
== group_rid
) {
1763 if (c
->opt_verbose
) {
1764 d_printf(_("Group is primary group "
1766 info
->info21
.account_name
.string
);
1768 group_is_primary
= true;
1771 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1774 if (group_is_primary
) {
1775 d_fprintf(stderr
, _("Unable to delete group because "
1776 "some of it's members have it as primary "
1778 status
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1782 /* remove all group members */
1783 for (i
= 0; i
< rids
->count
; i
++)
1786 d_printf(_("Remove group member %d..."),
1788 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
1792 if (!NT_STATUS_IS_OK(status
)) {
1796 if (NT_STATUS_IS_OK(result
)) {
1798 d_printf(_("ok\n"));
1801 d_printf("%s\n", _("failed"));
1806 status
= dcerpc_samr_DeleteDomainGroup(b
, mem_ctx
,
1809 if (!NT_STATUS_IS_OK(status
)) {
1816 /* removing a local group is easier... */
1817 case SID_NAME_ALIAS
:
1818 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
1820 MAXIMUM_ALLOWED_ACCESS
,
1824 if (!NT_STATUS_IS_OK(status
)) {
1825 d_fprintf(stderr
, _("Request open_alias failed\n"));
1828 if (!NT_STATUS_IS_OK(result
)) {
1830 d_fprintf(stderr
, _("Request open_alias failed\n"));
1834 status
= dcerpc_samr_DeleteDomAlias(b
, mem_ctx
,
1837 if (!NT_STATUS_IS_OK(status
)) {
1845 d_fprintf(stderr
, _("%s is of type %s. This command is only "
1846 "for deleting local or global groups\n"),
1847 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1848 status
= NT_STATUS_UNSUCCESSFUL
;
1852 if (NT_STATUS_IS_OK(status
)) {
1854 d_printf(_("Deleted %s '%s'\n"),
1855 sid_type_lookup(name_types
.ids
[0]), argv
[0]);
1857 d_fprintf(stderr
, _("Deleting of %s failed: %s\n"), argv
[0],
1858 get_friendly_nt_error_msg(status
));
1866 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1868 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
1869 rpc_group_delete_internals
, argc
,argv
);
1872 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1874 NET_API_STATUS status
;
1875 struct GROUP_INFO_1 info1
;
1876 uint32_t parm_error
= 0;
1878 if (argc
!= 1 || c
->display_usage
) {
1879 rpc_group_usage(c
, argc
, argv
);
1885 info1
.grpi1_name
= argv
[0];
1886 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1887 info1
.grpi1_comment
= c
->opt_comment
;
1890 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1894 _("Failed to add group '%s' with error: %s.\n"),
1895 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1899 d_printf(_("Added group '%s'.\n"), argv
[0]);
1905 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1907 NET_API_STATUS status
;
1908 struct LOCALGROUP_INFO_1 info1
;
1909 uint32_t parm_error
= 0;
1911 if (argc
!= 1 || c
->display_usage
) {
1912 rpc_group_usage(c
, argc
, argv
);
1918 info1
.lgrpi1_name
= argv
[0];
1919 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1920 info1
.lgrpi1_comment
= c
->opt_comment
;
1923 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1927 _("Failed to add alias '%s' with error: %s.\n"),
1928 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1932 d_printf(_("Added alias '%s'.\n"), argv
[0]);
1938 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1940 if (c
->opt_localgroup
)
1941 return rpc_alias_add_internals(c
, argc
, argv
);
1943 return rpc_group_add_internals(c
, argc
, argv
);
1946 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1947 TALLOC_CTX
*mem_ctx
,
1949 struct dom_sid
*sid
,
1950 enum lsa_SidType
*type
)
1952 struct dom_sid
*sids
= NULL
;
1953 enum lsa_SidType
*types
= NULL
;
1954 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1955 struct policy_handle lsa_pol
;
1956 NTSTATUS status
, result
;
1957 struct dcerpc_binding_handle
*b
;
1959 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1961 if (!NT_STATUS_IS_OK(status
)) {
1965 b
= pipe_hnd
->binding_handle
;
1967 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1968 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1970 if (!NT_STATUS_IS_OK(status
)) {
1974 status
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1975 &name
, NULL
, 1, &sids
, &types
);
1977 if (NT_STATUS_IS_OK(status
)) {
1978 sid_copy(sid
, &sids
[0]);
1982 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1986 TALLOC_FREE(pipe_hnd
);
1989 if (!NT_STATUS_IS_OK(status
) && (strncasecmp_m(name
, "S-", 2) == 0)) {
1991 /* Try as S-1-5-whatever */
1993 struct dom_sid tmp_sid
;
1995 if (string_to_sid(&tmp_sid
, name
)) {
1996 sid_copy(sid
, &tmp_sid
);
1997 *type
= SID_NAME_UNKNOWN
;
1998 status
= NT_STATUS_OK
;
2005 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
2006 TALLOC_CTX
*mem_ctx
,
2007 const struct dom_sid
*group_sid
,
2010 struct policy_handle connect_pol
, domain_pol
;
2011 NTSTATUS status
, result
;
2013 struct policy_handle group_pol
;
2014 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2016 struct samr_Ids rids
, rid_types
;
2017 struct lsa_String lsa_acct_name
;
2021 sid_copy(&sid
, group_sid
);
2023 if (!sid_split_rid(&sid
, &group_rid
)) {
2024 return NT_STATUS_UNSUCCESSFUL
;
2027 /* Get sam policy handle */
2028 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2030 MAXIMUM_ALLOWED_ACCESS
,
2033 if (!NT_STATUS_IS_OK(status
)) {
2036 if (!NT_STATUS_IS_OK(result
)) {
2040 /* Get domain policy handle */
2041 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2043 MAXIMUM_ALLOWED_ACCESS
,
2047 if (!NT_STATUS_IS_OK(status
)) {
2050 if (!NT_STATUS_IS_OK(result
)) {
2054 init_lsa_String(&lsa_acct_name
, member
);
2056 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2063 if (!NT_STATUS_IS_OK(status
)) {
2064 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2069 if (!NT_STATUS_IS_OK(result
)) {
2071 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2076 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2078 MAXIMUM_ALLOWED_ACCESS
,
2082 if (!NT_STATUS_IS_OK(status
)) {
2086 if (!NT_STATUS_IS_OK(result
)) {
2091 status
= dcerpc_samr_AddGroupMember(b
, mem_ctx
,
2094 0x0005, /* unknown flags */
2096 if (!NT_STATUS_IS_OK(status
)) {
2103 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2107 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2108 TALLOC_CTX
*mem_ctx
,
2109 const struct dom_sid
*alias_sid
,
2112 struct policy_handle connect_pol
, domain_pol
;
2113 NTSTATUS status
, result
;
2115 struct policy_handle alias_pol
;
2116 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2118 struct dom_sid member_sid
;
2119 enum lsa_SidType member_type
;
2123 sid_copy(&sid
, alias_sid
);
2125 if (!sid_split_rid(&sid
, &alias_rid
)) {
2126 return NT_STATUS_UNSUCCESSFUL
;
2129 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2130 member
, &member_sid
, &member_type
);
2132 if (!NT_STATUS_IS_OK(result
)) {
2133 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2138 /* Get sam policy handle */
2139 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2141 MAXIMUM_ALLOWED_ACCESS
,
2144 if (!NT_STATUS_IS_OK(status
)) {
2147 if (!NT_STATUS_IS_OK(result
)) {
2152 /* Get domain policy handle */
2153 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2155 MAXIMUM_ALLOWED_ACCESS
,
2159 if (!NT_STATUS_IS_OK(status
)) {
2162 if (!NT_STATUS_IS_OK(result
)) {
2167 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2169 MAXIMUM_ALLOWED_ACCESS
,
2173 if (!NT_STATUS_IS_OK(status
)) {
2176 if (!NT_STATUS_IS_OK(result
)) {
2180 status
= dcerpc_samr_AddAliasMember(b
, mem_ctx
,
2184 if (!NT_STATUS_IS_OK(status
)) {
2191 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2195 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
2196 const struct dom_sid
*domain_sid
,
2197 const char *domain_name
,
2198 struct cli_state
*cli
,
2199 struct rpc_pipe_client
*pipe_hnd
,
2200 TALLOC_CTX
*mem_ctx
,
2204 struct dom_sid group_sid
;
2205 enum lsa_SidType group_type
;
2207 if (argc
!= 2 || c
->display_usage
) {
2210 _("net rpc group addmem <group> <member>\n"
2211 " Add a member to a group\n"
2212 " group\tGroup to add member to\n"
2213 " member\tMember to add to group\n"));
2214 return NT_STATUS_UNSUCCESSFUL
;
2217 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2218 &group_sid
, &group_type
))) {
2219 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2221 return NT_STATUS_UNSUCCESSFUL
;
2224 if (group_type
== SID_NAME_DOM_GRP
) {
2225 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2226 &group_sid
, argv
[1]);
2228 if (!NT_STATUS_IS_OK(result
)) {
2229 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2230 argv
[1], argv
[0], nt_errstr(result
));
2235 if (group_type
== SID_NAME_ALIAS
) {
2236 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2237 &group_sid
, argv
[1]);
2239 if (!NT_STATUS_IS_OK(result
)) {
2240 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2241 argv
[1], argv
[0], nt_errstr(result
));
2246 d_fprintf(stderr
, _("Can only add members to global or local groups "
2247 "which %s is not\n"), argv
[0]);
2249 return NT_STATUS_UNSUCCESSFUL
;
2252 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2254 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2255 rpc_group_addmem_internals
,
2259 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2260 struct rpc_pipe_client
*pipe_hnd
,
2261 TALLOC_CTX
*mem_ctx
,
2262 const struct dom_sid
*group_sid
,
2265 struct policy_handle connect_pol
, domain_pol
;
2266 NTSTATUS status
, result
;
2268 struct policy_handle group_pol
;
2269 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2271 struct samr_Ids rids
, rid_types
;
2272 struct lsa_String lsa_acct_name
;
2276 sid_copy(&sid
, group_sid
);
2278 if (!sid_split_rid(&sid
, &group_rid
))
2279 return NT_STATUS_UNSUCCESSFUL
;
2281 /* Get sam policy handle */
2282 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2284 MAXIMUM_ALLOWED_ACCESS
,
2287 if (!NT_STATUS_IS_OK(status
)) {
2290 if (!NT_STATUS_IS_OK(result
)) {
2295 /* Get domain policy handle */
2296 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2298 MAXIMUM_ALLOWED_ACCESS
,
2302 if (!NT_STATUS_IS_OK(status
)) {
2305 if (!NT_STATUS_IS_OK(result
)) {
2309 init_lsa_String(&lsa_acct_name
, member
);
2311 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2318 if (!NT_STATUS_IS_OK(status
)) {
2319 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2324 if (!NT_STATUS_IS_OK(result
)) {
2326 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2331 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2333 MAXIMUM_ALLOWED_ACCESS
,
2337 if (!NT_STATUS_IS_OK(status
)) {
2340 if (!NT_STATUS_IS_OK(result
)) {
2345 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
2349 if (!NT_STATUS_IS_OK(status
)) {
2355 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2359 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2360 TALLOC_CTX
*mem_ctx
,
2361 const struct dom_sid
*alias_sid
,
2364 struct policy_handle connect_pol
, domain_pol
;
2365 NTSTATUS status
, result
;
2367 struct policy_handle alias_pol
;
2368 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2370 struct dom_sid member_sid
;
2371 enum lsa_SidType member_type
;
2375 sid_copy(&sid
, alias_sid
);
2377 if (!sid_split_rid(&sid
, &alias_rid
))
2378 return NT_STATUS_UNSUCCESSFUL
;
2380 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2381 member
, &member_sid
, &member_type
);
2383 if (!NT_STATUS_IS_OK(result
)) {
2384 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2389 /* Get sam policy handle */
2390 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2392 MAXIMUM_ALLOWED_ACCESS
,
2395 if (!NT_STATUS_IS_OK(status
)) {
2398 if (!NT_STATUS_IS_OK(result
)) {
2403 /* Get domain policy handle */
2404 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2406 MAXIMUM_ALLOWED_ACCESS
,
2410 if (!NT_STATUS_IS_OK(status
)) {
2413 if (!NT_STATUS_IS_OK(result
)) {
2418 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2420 MAXIMUM_ALLOWED_ACCESS
,
2424 if (!NT_STATUS_IS_OK(status
)) {
2428 if (!NT_STATUS_IS_OK(result
)) {
2432 status
= dcerpc_samr_DeleteAliasMember(b
, mem_ctx
,
2437 if (!NT_STATUS_IS_OK(status
)) {
2444 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2448 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2449 const struct dom_sid
*domain_sid
,
2450 const char *domain_name
,
2451 struct cli_state
*cli
,
2452 struct rpc_pipe_client
*pipe_hnd
,
2453 TALLOC_CTX
*mem_ctx
,
2457 struct dom_sid group_sid
;
2458 enum lsa_SidType group_type
;
2460 if (argc
!= 2 || c
->display_usage
) {
2463 _("net rpc group delmem <group> <member>\n"
2464 " Delete a member from a group\n"
2465 " group\tGroup to delete member from\n"
2466 " member\tMember to delete from group\n"));
2467 return NT_STATUS_UNSUCCESSFUL
;
2470 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2471 &group_sid
, &group_type
))) {
2472 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2474 return NT_STATUS_UNSUCCESSFUL
;
2477 if (group_type
== SID_NAME_DOM_GRP
) {
2478 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2479 &group_sid
, argv
[1]);
2481 if (!NT_STATUS_IS_OK(result
)) {
2482 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2483 argv
[1], argv
[0], nt_errstr(result
));
2488 if (group_type
== SID_NAME_ALIAS
) {
2489 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2490 &group_sid
, argv
[1]);
2492 if (!NT_STATUS_IS_OK(result
)) {
2493 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2494 argv
[1], argv
[0], nt_errstr(result
));
2499 d_fprintf(stderr
, _("Can only delete members from global or local "
2500 "groups which %s is not\n"), argv
[0]);
2502 return NT_STATUS_UNSUCCESSFUL
;
2505 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2507 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2508 rpc_group_delmem_internals
,
2513 * List groups on a remote RPC server.
2515 * All parameters are provided by the run_rpc_command function, except for
2516 * argc, argv which are passes through.
2518 * @param domain_sid The domain sid acquired from the remote server.
2519 * @param cli A cli_state connected to the server.
2520 * @param mem_ctx Talloc context, destroyed on completion of the function.
2521 * @param argc Standard main() style argc.
2522 * @param argv Standard main() style argv. Initial components are already
2525 * @return Normal NTSTATUS return.
2528 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2529 const struct dom_sid
*domain_sid
,
2530 const char *domain_name
,
2531 struct cli_state
*cli
,
2532 struct rpc_pipe_client
*pipe_hnd
,
2533 TALLOC_CTX
*mem_ctx
,
2537 struct policy_handle connect_pol
, domain_pol
;
2538 NTSTATUS status
, result
;
2539 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2540 struct samr_SamArray
*groups
= NULL
;
2541 bool global
= false;
2543 bool builtin
= false;
2544 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2546 if (c
->display_usage
) {
2549 _("net rpc group list [global] [local] [builtin]\n"
2550 " List groups on RPC server\n"
2551 " global\tList global groups\n"
2552 " local\tList local groups\n"
2553 " builtin\tList builtin groups\n"
2554 " If none of global, local or builtin is "
2555 "specified, all three options are considered "
2557 return NT_STATUS_OK
;
2566 for (i
=0; i
<argc
; i
++) {
2567 if (strequal(argv
[i
], "global"))
2570 if (strequal(argv
[i
], "local"))
2573 if (strequal(argv
[i
], "builtin"))
2577 /* Get sam policy handle */
2579 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2581 MAXIMUM_ALLOWED_ACCESS
,
2584 if (!NT_STATUS_IS_OK(status
)) {
2587 if (!NT_STATUS_IS_OK(result
)) {
2592 /* Get domain policy handle */
2594 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2596 MAXIMUM_ALLOWED_ACCESS
,
2597 discard_const_p(struct dom_sid2
, domain_sid
),
2600 if (!NT_STATUS_IS_OK(status
)) {
2603 if (!NT_STATUS_IS_OK(result
)) {
2608 /* Query domain groups */
2609 if (c
->opt_long_list_entries
)
2610 d_printf(_("\nGroup name Comment"
2611 "\n-----------------------------\n"));
2613 uint32_t max_size
, total_size
, returned_size
;
2614 union samr_DispInfo info
;
2618 dcerpc_get_query_dispinfo_params(
2619 loop_count
, &max_entries
, &max_size
);
2621 status
= dcerpc_samr_QueryDisplayInfo(b
, mem_ctx
,
2631 if (!NT_STATUS_IS_OK(status
)) {
2634 num_entries
= info
.info3
.count
;
2635 start_idx
+= info
.info3
.count
;
2637 if (!NT_STATUS_IS_OK(result
) &&
2638 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2641 for (i
= 0; i
< num_entries
; i
++) {
2643 const char *group
= NULL
;
2644 const char *desc
= NULL
;
2646 group
= info
.info3
.entries
[i
].account_name
.string
;
2647 desc
= info
.info3
.entries
[i
].description
.string
;
2649 if (c
->opt_long_list_entries
)
2650 printf("%-21.21s %-50.50s\n",
2653 printf("%s\n", group
);
2655 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2656 /* query domain aliases */
2661 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2668 if (!NT_STATUS_IS_OK(status
)) {
2671 if (!NT_STATUS_IS_OK(result
) &&
2672 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2675 for (i
= 0; i
< num_entries
; i
++) {
2677 const char *description
= NULL
;
2679 if (c
->opt_long_list_entries
) {
2681 struct policy_handle alias_pol
;
2682 union samr_AliasInfo
*info
= NULL
;
2685 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2688 groups
->entries
[i
].idx
,
2691 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2692 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2697 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2698 status
= dcerpc_samr_Close(b
, mem_ctx
,
2701 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2702 description
= info
->description
.string
;
2708 if (description
!= NULL
) {
2709 printf("%-21.21s %-50.50s\n",
2710 groups
->entries
[i
].name
.string
,
2713 printf("%s\n", groups
->entries
[i
].name
.string
);
2716 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2717 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
2718 /* Get builtin policy handle */
2720 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2722 MAXIMUM_ALLOWED_ACCESS
,
2723 discard_const_p(struct dom_sid2
, &global_sid_Builtin
),
2726 if (!NT_STATUS_IS_OK(status
)) {
2729 if (!NT_STATUS_IS_OK(result
)) {
2734 /* query builtin aliases */
2737 if (!builtin
) break;
2739 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2746 if (!NT_STATUS_IS_OK(status
)) {
2749 if (!NT_STATUS_IS_OK(result
) &&
2750 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
2755 for (i
= 0; i
< num_entries
; i
++) {
2757 const char *description
= NULL
;
2759 if (c
->opt_long_list_entries
) {
2761 struct policy_handle alias_pol
;
2762 union samr_AliasInfo
*info
= NULL
;
2765 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2768 groups
->entries
[i
].idx
,
2771 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2772 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2777 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2778 status
= dcerpc_samr_Close(b
, mem_ctx
,
2781 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2782 description
= info
->description
.string
;
2788 if (description
!= NULL
) {
2789 printf("%-21.21s %-50.50s\n",
2790 groups
->entries
[i
].name
.string
,
2793 printf("%s\n", groups
->entries
[i
].name
.string
);
2796 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2804 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2806 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2807 rpc_group_list_internals
,
2811 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2812 struct rpc_pipe_client
*pipe_hnd
,
2813 TALLOC_CTX
*mem_ctx
,
2814 const char *domain_name
,
2815 const struct dom_sid
*domain_sid
,
2816 struct policy_handle
*domain_pol
,
2819 NTSTATUS result
, status
;
2820 struct policy_handle group_pol
;
2821 uint32 num_members
, *group_rids
;
2823 struct samr_RidAttrArray
*rids
= NULL
;
2824 struct lsa_Strings names
;
2825 struct samr_Ids types
;
2826 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2829 sid_to_fstring(sid_str
, domain_sid
);
2831 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2833 MAXIMUM_ALLOWED_ACCESS
,
2837 if (!NT_STATUS_IS_OK(status
)) {
2840 if (!NT_STATUS_IS_OK(result
)) {
2844 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
2848 if (!NT_STATUS_IS_OK(status
)) {
2851 if (!NT_STATUS_IS_OK(result
)) {
2855 num_members
= rids
->count
;
2856 group_rids
= rids
->rids
;
2858 while (num_members
> 0) {
2859 int this_time
= 512;
2861 if (num_members
< this_time
)
2862 this_time
= num_members
;
2864 status
= dcerpc_samr_LookupRids(b
, mem_ctx
,
2871 if (!NT_STATUS_IS_OK(status
)) {
2874 if (!NT_STATUS_IS_OK(result
)) {
2878 /* We only have users as members, but make the output
2879 the same as the output of alias members */
2881 for (i
= 0; i
< this_time
; i
++) {
2883 if (c
->opt_long_list_entries
) {
2884 printf("%s-%d %s\\%s %d\n", sid_str
,
2885 group_rids
[i
], domain_name
,
2886 names
.names
[i
].string
,
2889 printf("%s\\%s\n", domain_name
,
2890 names
.names
[i
].string
);
2894 num_members
-= this_time
;
2898 return NT_STATUS_OK
;
2901 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2902 struct rpc_pipe_client
*pipe_hnd
,
2903 TALLOC_CTX
*mem_ctx
,
2904 struct policy_handle
*domain_pol
,
2907 NTSTATUS result
, status
;
2908 struct rpc_pipe_client
*lsa_pipe
;
2909 struct policy_handle alias_pol
, lsa_pol
;
2911 struct dom_sid
*alias_sids
;
2914 enum lsa_SidType
*types
;
2916 struct lsa_SidArray sid_array
;
2917 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2919 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2921 MAXIMUM_ALLOWED_ACCESS
,
2925 if (!NT_STATUS_IS_OK(status
)) {
2928 if (!NT_STATUS_IS_OK(result
)) {
2932 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
2936 if (!NT_STATUS_IS_OK(status
)) {
2937 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2940 if (!NT_STATUS_IS_OK(result
)) {
2941 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2945 num_members
= sid_array
.num_sids
;
2947 if (num_members
== 0) {
2948 return NT_STATUS_OK
;
2951 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2952 &ndr_table_lsarpc
.syntax_id
,
2954 if (!NT_STATUS_IS_OK(result
)) {
2955 d_fprintf(stderr
, _("Couldn't open LSA pipe. Error was %s\n"),
2956 nt_errstr(result
) );
2960 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2961 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
2963 if (!NT_STATUS_IS_OK(result
)) {
2964 d_fprintf(stderr
, _("Couldn't open LSA policy handle\n"));
2965 TALLOC_FREE(lsa_pipe
);
2969 alias_sids
= talloc_zero_array(mem_ctx
, struct dom_sid
, num_members
);
2971 d_fprintf(stderr
, _("Out of memory\n"));
2972 TALLOC_FREE(lsa_pipe
);
2973 return NT_STATUS_NO_MEMORY
;
2976 for (i
=0; i
<num_members
; i
++) {
2977 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2980 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2981 num_members
, alias_sids
,
2982 &domains
, &names
, &types
);
2984 if (!NT_STATUS_IS_OK(result
) &&
2985 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2986 d_fprintf(stderr
, _("Couldn't lookup SIDs\n"));
2987 TALLOC_FREE(lsa_pipe
);
2991 for (i
= 0; i
< num_members
; i
++) {
2993 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2995 if (c
->opt_long_list_entries
) {
2996 printf("%s %s\\%s %d\n", sid_str
,
2997 domains
[i
] ? domains
[i
] : _("*unknown*"),
2998 names
[i
] ? names
[i
] : _("*unknown*"), types
[i
]);
3001 printf("%s\\%s\n", domains
[i
], names
[i
]);
3003 printf("%s\n", sid_str
);
3007 TALLOC_FREE(lsa_pipe
);
3008 return NT_STATUS_OK
;
3011 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
3012 const struct dom_sid
*domain_sid
,
3013 const char *domain_name
,
3014 struct cli_state
*cli
,
3015 struct rpc_pipe_client
*pipe_hnd
,
3016 TALLOC_CTX
*mem_ctx
,
3020 NTSTATUS result
, status
;
3021 struct policy_handle connect_pol
, domain_pol
;
3022 struct samr_Ids rids
, rid_types
;
3023 struct lsa_String lsa_acct_name
;
3024 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3026 /* Get sam policy handle */
3028 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
3030 MAXIMUM_ALLOWED_ACCESS
,
3033 if (!NT_STATUS_IS_OK(status
)) {
3036 if (!NT_STATUS_IS_OK(result
)) {
3040 /* Get domain policy handle */
3042 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3044 MAXIMUM_ALLOWED_ACCESS
,
3045 discard_const_p(struct dom_sid2
, domain_sid
),
3048 if (!NT_STATUS_IS_OK(status
)) {
3051 if (!NT_STATUS_IS_OK(result
)) {
3055 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
3057 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3064 if (!NT_STATUS_IS_OK(status
)) {
3068 if (!NT_STATUS_IS_OK(result
)) {
3070 /* Ok, did not find it in the global sam, try with builtin */
3072 struct dom_sid sid_Builtin
;
3074 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
3076 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
3078 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3080 MAXIMUM_ALLOWED_ACCESS
,
3084 if (!NT_STATUS_IS_OK(status
)) {
3087 if (!NT_STATUS_IS_OK(result
)) {
3088 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3093 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3100 if (!NT_STATUS_IS_OK(status
)) {
3103 if (!NT_STATUS_IS_OK(result
)) {
3104 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3110 if (rids
.count
!= 1) {
3111 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3116 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
3117 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
3118 domain_sid
, &domain_pol
,
3122 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
3123 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
3127 return NT_STATUS_NO_SUCH_GROUP
;
3130 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
3132 if (argc
!= 1 || c
->display_usage
) {
3133 return rpc_group_usage(c
, argc
, argv
);
3136 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
3137 rpc_group_members_internals
,
3141 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
3143 NET_API_STATUS status
;
3144 struct GROUP_INFO_0 g0
;
3148 d_printf(_("Usage:\n"));
3149 d_printf("net rpc group rename group newname\n");
3153 g0
.grpi0_name
= argv
[1];
3155 status
= NetGroupSetInfo(c
->opt_host
,
3162 d_fprintf(stderr
, _("Renaming group %s failed with: %s\n"),
3163 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
3171 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
3173 if (argc
!= 2 || c
->display_usage
) {
3174 return rpc_group_usage(c
, argc
, argv
);
3177 return rpc_group_rename_internals(c
, argc
, argv
);
3181 * 'net rpc group' entrypoint.
3182 * @param argc Standard main() style argc.
3183 * @param argv Standard main() style argv. Initial components are already
3187 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
3189 NET_API_STATUS status
;
3191 struct functable func
[] = {
3196 N_("Create specified group"),
3197 N_("net rpc group add\n"
3198 " Create specified group")
3204 N_("Delete specified group"),
3205 N_("net rpc group delete\n"
3206 " Delete specified group")
3212 N_("Add member to group"),
3213 N_("net rpc group addmem\n"
3214 " Add member to group")
3220 N_("Remove member from group"),
3221 N_("net rpc group delmem\n"
3222 " Remove member from group")
3229 N_("net rpc group list\n"
3236 N_("List group members"),
3237 N_("net rpc group members\n"
3238 " List group members")
3245 N_("net rpc group rename\n"
3248 {NULL
, NULL
, 0, NULL
, NULL
}
3251 status
= libnetapi_net_init(&c
->netapi_ctx
);
3255 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
3256 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
3257 if (c
->opt_kerberos
) {
3258 libnetapi_set_use_kerberos(c
->netapi_ctx
);
3262 if (c
->display_usage
) {
3263 d_printf(_("Usage:\n"));
3264 d_printf(_("net rpc group\n"
3265 " Alias for net rpc group list global "
3266 "local builtin\n"));
3267 net_display_usage_from_functable(func
);
3271 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
3272 rpc_group_list_internals
,
3276 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
3279 /****************************************************************************/
3281 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
3283 return net_share_usage(c
, argc
, argv
);
3287 * Add a share on a remote RPC server.
3289 * @param argc Standard main() style argc.
3290 * @param argv Standard main() style argv. Initial components are already
3293 * @return A shell status integer (0 for success).
3296 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
3298 NET_API_STATUS status
;
3301 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3302 uint32 num_users
=0, perms
=0;
3303 char *password
=NULL
; /* don't allow a share password */
3304 struct SHARE_INFO_2 i2
;
3305 uint32_t parm_error
= 0;
3307 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
3308 return rpc_share_usage(c
, argc
, argv
);
3311 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
3315 path
= strchr(sharename
, '=');
3322 i2
.shi2_netname
= sharename
;
3323 i2
.shi2_type
= type
;
3324 i2
.shi2_remark
= c
->opt_comment
;
3325 i2
.shi2_permissions
= perms
;
3326 i2
.shi2_max_uses
= c
->opt_maxusers
;
3327 i2
.shi2_current_uses
= num_users
;
3328 i2
.shi2_path
= path
;
3329 i2
.shi2_passwd
= password
;
3331 status
= NetShareAdd(c
->opt_host
,
3336 printf(_("NetShareAdd failed with: %s\n"),
3337 libnetapi_get_error_string(c
->netapi_ctx
, status
));
3344 * Delete a share on a remote RPC server.
3346 * @param domain_sid The domain sid acquired from the remote server.
3347 * @param argc Standard main() style argc.
3348 * @param argv Standard main() style argv. Initial components are already
3351 * @return A shell status integer (0 for success).
3353 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3355 if (argc
< 1 || c
->display_usage
) {
3356 return rpc_share_usage(c
, argc
, argv
);
3359 return NetShareDel(c
->opt_host
, argv
[0], 0);
3363 * Formatted print of share info
3365 * @param r pointer to SHARE_INFO_1 to format
3368 static void display_share_info_1(struct net_context
*c
,
3369 struct SHARE_INFO_1
*r
)
3371 if (c
->opt_long_list_entries
) {
3372 d_printf("%-12s %-8.8s %-50s\n",
3374 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
3377 d_printf("%s\n", r
->shi1_netname
);
3381 static WERROR
get_share_info(struct net_context
*c
,
3382 struct rpc_pipe_client
*pipe_hnd
,
3383 TALLOC_CTX
*mem_ctx
,
3387 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3391 union srvsvc_NetShareInfo info
;
3392 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3394 /* no specific share requested, enumerate all */
3397 uint32_t preferred_len
= 0xffffffff;
3398 uint32_t total_entries
= 0;
3399 uint32_t resume_handle
= 0;
3401 info_ctr
->level
= level
;
3403 status
= dcerpc_srvsvc_NetShareEnumAll(b
, mem_ctx
,
3410 if (!NT_STATUS_IS_OK(status
)) {
3411 return ntstatus_to_werror(status
);
3416 /* request just one share */
3417 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
3424 if (!NT_STATUS_IS_OK(status
)) {
3425 result
= ntstatus_to_werror(status
);
3429 if (!W_ERROR_IS_OK(result
)) {
3434 ZERO_STRUCTP(info_ctr
);
3436 info_ctr
->level
= level
;
3441 struct srvsvc_NetShareCtr1
*ctr1
;
3443 ctr1
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr1
);
3444 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3447 ctr1
->array
= info
.info1
;
3449 info_ctr
->ctr
.ctr1
= ctr1
;
3455 struct srvsvc_NetShareCtr2
*ctr2
;
3457 ctr2
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr2
);
3458 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3461 ctr2
->array
= info
.info2
;
3463 info_ctr
->ctr
.ctr2
= ctr2
;
3469 struct srvsvc_NetShareCtr502
*ctr502
;
3471 ctr502
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr502
);
3472 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3475 ctr502
->array
= info
.info502
;
3477 info_ctr
->ctr
.ctr502
= ctr502
;
3487 * 'net rpc share list' entrypoint.
3488 * @param argc Standard main() style argc.
3489 * @param argv Standard main() style argv. Initial components are already
3492 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3494 NET_API_STATUS status
;
3495 struct SHARE_INFO_1
*i1
= NULL
;
3496 uint32_t entries_read
= 0;
3497 uint32_t total_entries
= 0;
3498 uint32_t resume_handle
= 0;
3499 uint32_t i
, level
= 1;
3501 if (c
->display_usage
) {
3503 "net rpc share list\n"
3506 _("List shares on remote server"));
3510 status
= NetShareEnum(c
->opt_host
,
3512 (uint8_t **)(void *)&i1
,
3521 /* Display results */
3523 if (c
->opt_long_list_entries
) {
3525 "\nEnumerating shared resources (exports) on remote server:\n\n"
3526 "\nShare name Type Description\n"
3527 "---------- ---- -----------\n"));
3529 for (i
= 0; i
< entries_read
; i
++)
3530 display_share_info_1(c
, &i1
[i
]);
3535 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3539 status
= cli_tree_connect(cli
, netname
, "A:", "", 0);
3540 if (!NT_STATUS_IS_OK(status
)) {
3541 d_printf(_("skipping [%s]: not a file share.\n"), netname
);
3545 status
= cli_tdis(cli
);
3546 if (!NT_STATUS_IS_OK(status
)) {
3547 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status
));
3554 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3555 const char *netname
, uint32 type
)
3557 /* only support disk shares */
3558 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3559 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname
,
3564 /* skip builtin shares */
3565 /* FIXME: should print$ be added too ? */
3566 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3567 strequal(netname
,"global"))
3570 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3571 printf(_("excluding [%s]\n"), netname
);
3575 return check_share_availability(cli
, netname
);
3579 * Migrate shares from a remote RPC server to the local RPC server.
3581 * All parameters are provided by the run_rpc_command function, except for
3582 * argc, argv which are passed through.
3584 * @param domain_sid The domain sid acquired from the remote server.
3585 * @param cli A cli_state connected to the server.
3586 * @param mem_ctx Talloc context, destroyed on completion of the function.
3587 * @param argc Standard main() style argc.
3588 * @param argv Standard main() style argv. Initial components are already
3591 * @return Normal NTSTATUS return.
3594 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3595 const struct dom_sid
*domain_sid
,
3596 const char *domain_name
,
3597 struct cli_state
*cli
,
3598 struct rpc_pipe_client
*pipe_hnd
,
3599 TALLOC_CTX
*mem_ctx
,
3604 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3605 struct srvsvc_NetShareInfoCtr ctr_src
;
3607 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3608 struct cli_state
*cli_dst
= NULL
;
3609 uint32 level
= 502; /* includes secdesc */
3610 uint32_t parm_error
= 0;
3611 struct dcerpc_binding_handle
*b
;
3613 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3615 if (!W_ERROR_IS_OK(result
))
3618 /* connect destination PI_SRVSVC */
3619 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3620 &ndr_table_srvsvc
.syntax_id
);
3621 if (!NT_STATUS_IS_OK(nt_status
))
3624 b
= srvsvc_pipe
->binding_handle
;
3626 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3628 union srvsvc_NetShareInfo info
;
3629 struct srvsvc_NetShareInfo502 info502
=
3630 ctr_src
.ctr
.ctr502
->array
[i
];
3632 /* reset error-code */
3633 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3635 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3638 /* finally add the share on the dst server */
3640 printf(_("migrating: [%s], path: %s, comment: %s, without "
3642 info502
.name
, info502
.path
, info502
.comment
);
3644 info
.info502
= &info502
;
3646 nt_status
= dcerpc_srvsvc_NetShareAdd(b
, mem_ctx
,
3647 srvsvc_pipe
->desthost
,
3652 if (!NT_STATUS_IS_OK(nt_status
)) {
3653 printf(_("cannot add share: %s\n"),
3654 nt_errstr(nt_status
));
3657 if (W_ERROR_V(result
) == W_ERROR_V(WERR_FILE_EXISTS
)) {
3658 printf(_(" [%s] does already exist\n"),
3663 if (!W_ERROR_IS_OK(result
)) {
3664 nt_status
= werror_to_ntstatus(result
);
3665 printf(_("cannot add share: %s\n"),
3666 win_errstr(result
));
3672 nt_status
= NT_STATUS_OK
;
3676 cli_shutdown(cli_dst
);
3684 * Migrate shares from a RPC server to another.
3686 * @param argc Standard main() style argc.
3687 * @param argv Standard main() style argv. Initial components are already
3690 * @return A shell status integer (0 for success).
3692 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3695 if (c
->display_usage
) {
3697 "net rpc share migrate shares\n"
3700 _("Migrate shares to local server"));
3705 printf(_("no server to migrate\n"));
3709 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
3710 rpc_share_migrate_shares_internals
,
3717 * @param f file_info
3718 * @param mask current search mask
3719 * @param state arg-pointer
3722 static NTSTATUS
copy_fn(const char *mnt
, struct file_info
*f
,
3723 const char *mask
, void *state
)
3725 static NTSTATUS nt_status
;
3726 static struct copy_clistate
*local_state
;
3727 static fstring filename
, new_mask
;
3730 struct net_context
*c
;
3732 local_state
= (struct copy_clistate
*)state
;
3733 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3737 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3738 return NT_STATUS_OK
;
3740 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3743 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
3745 DEBUG(3,("got dir: %s\n", f
->name
));
3747 fstrcpy(dir
, local_state
->cwd
);
3749 fstrcat(dir
, f
->name
);
3751 switch (net_mode_share
)
3753 case NET_MODE_SHARE_MIGRATE
:
3754 /* create that directory */
3755 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3756 local_state
->cli_share_src
,
3757 local_state
->cli_share_dst
,
3759 c
->opt_acls
? true : false,
3760 c
->opt_attrs
? true : false,
3761 c
->opt_timestamps
? true:false,
3765 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3766 return NT_STATUS_INTERNAL_ERROR
;
3769 if (!NT_STATUS_IS_OK(nt_status
)) {
3770 printf(_("could not handle dir %s: %s\n"),
3771 dir
, nt_errstr(nt_status
));
3775 /* search below that directory */
3776 if (strlcpy(new_mask
, dir
, sizeof(new_mask
)) >= sizeof(new_mask
)) {
3777 return NT_STATUS_NO_MEMORY
;
3779 if (strlcat(new_mask
, "\\*", sizeof(new_mask
)) >= sizeof(new_mask
)) {
3780 return NT_STATUS_NO_MEMORY
;
3783 old_dir
= local_state
->cwd
;
3784 local_state
->cwd
= dir
;
3785 nt_status
= sync_files(local_state
, new_mask
);
3786 if (!NT_STATUS_IS_OK(nt_status
)) {
3787 printf(_("could not handle files\n"));
3789 local_state
->cwd
= old_dir
;
3796 fstrcpy(filename
, local_state
->cwd
);
3797 fstrcat(filename
, "\\");
3798 fstrcat(filename
, f
->name
);
3800 DEBUG(3,("got file: %s\n", filename
));
3802 switch (net_mode_share
)
3804 case NET_MODE_SHARE_MIGRATE
:
3805 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3806 local_state
->cli_share_src
,
3807 local_state
->cli_share_dst
,
3809 c
->opt_acls
? true : false,
3810 c
->opt_attrs
? true : false,
3811 c
->opt_timestamps
? true: false,
3815 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3817 return NT_STATUS_INTERNAL_ERROR
;
3820 if (!NT_STATUS_IS_OK(nt_status
))
3821 printf(_("could not handle file %s: %s\n"),
3822 filename
, nt_errstr(nt_status
));
3827 * sync files, can be called recursivly to list files
3828 * and then call copy_fn for each file
3830 * @param cp_clistate pointer to the copy_clistate we work with
3831 * @param mask the current search mask
3833 * @return Boolean result
3835 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3837 struct cli_state
*targetcli
;
3838 char *targetpath
= NULL
;
3841 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3843 status
= cli_resolve_path(talloc_tos(), "", NULL
,
3844 cp_clistate
->cli_share_src
,
3845 mask
, &targetcli
, &targetpath
);
3846 if (!NT_STATUS_IS_OK(status
)) {
3847 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3849 mask
, nt_errstr(status
));
3853 status
= cli_list(targetcli
, targetpath
, cp_clistate
->attribute
,
3854 copy_fn
, cp_clistate
);
3855 if (!NT_STATUS_IS_OK(status
)) {
3856 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3857 mask
, nt_errstr(status
));
3865 * Set the top level directory permissions before we do any further copies.
3866 * Should set up ACL inheritance.
3869 bool copy_top_level_perms(struct net_context
*c
,
3870 struct copy_clistate
*cp_clistate
,
3871 const char *sharename
)
3873 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3875 switch (net_mode_share
) {
3876 case NET_MODE_SHARE_MIGRATE
:
3877 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3878 nt_status
= net_copy_fileattr(c
,
3879 cp_clistate
->mem_ctx
,
3880 cp_clistate
->cli_share_src
,
3881 cp_clistate
->cli_share_dst
,
3883 c
->opt_acls
? true : false,
3884 c
->opt_attrs
? true : false,
3885 c
->opt_timestamps
? true: false,
3889 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3893 if (!NT_STATUS_IS_OK(nt_status
)) {
3894 printf(_("Could handle directory attributes for top level "
3895 "directory of share %s. Error %s\n"),
3896 sharename
, nt_errstr(nt_status
));
3904 * Sync all files inside a remote share to another share (over smb).
3906 * All parameters are provided by the run_rpc_command function, except for
3907 * argc, argv which are passed through.
3909 * @param domain_sid The domain sid acquired from the remote server.
3910 * @param cli A cli_state connected to the server.
3911 * @param mem_ctx Talloc context, destroyed on completion of the function.
3912 * @param argc Standard main() style argc.
3913 * @param argv Standard main() style argv. Initial components are already
3916 * @return Normal NTSTATUS return.
3919 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3920 const struct dom_sid
*domain_sid
,
3921 const char *domain_name
,
3922 struct cli_state
*cli
,
3923 struct rpc_pipe_client
*pipe_hnd
,
3924 TALLOC_CTX
*mem_ctx
,
3929 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3930 struct srvsvc_NetShareInfoCtr ctr_src
;
3933 struct copy_clistate cp_clistate
;
3934 bool got_src_share
= false;
3935 bool got_dst_share
= false;
3936 const char *mask
= "\\*";
3939 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3941 nt_status
= NT_STATUS_NO_MEMORY
;
3945 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3948 if (!W_ERROR_IS_OK(result
))
3951 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3953 struct srvsvc_NetShareInfo502 info502
=
3954 ctr_src
.ctr
.ctr502
->array
[i
];
3956 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3959 /* one might not want to mirror whole discs :) */
3960 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3961 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3966 switch (net_mode_share
)
3968 case NET_MODE_SHARE_MIGRATE
:
3972 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3976 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3979 c
->opt_acls
? _("including") : _("without"),
3980 c
->opt_attrs
? _("including") : _("without"),
3981 c
->opt_timestamps
? _("(preserving timestamps)") : "");
3983 cp_clistate
.mem_ctx
= mem_ctx
;
3984 cp_clistate
.cli_share_src
= NULL
;
3985 cp_clistate
.cli_share_dst
= NULL
;
3986 cp_clistate
.cwd
= NULL
;
3987 cp_clistate
.attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
;
3990 /* open share source */
3991 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3992 smbXcli_conn_remote_sockaddr(cli
->conn
),
3993 smbXcli_conn_remote_name(cli
->conn
),
3994 info502
.name
, "A:");
3995 if (!NT_STATUS_IS_OK(nt_status
))
3998 got_src_share
= true;
4000 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
4001 /* open share destination */
4002 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
4003 NULL
, dst
, info502
.name
, "A:");
4004 if (!NT_STATUS_IS_OK(nt_status
))
4007 got_dst_share
= true;
4010 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
4011 d_fprintf(stderr
, _("Could not handle the top level "
4012 "directory permissions for the "
4013 "share: %s\n"), info502
.name
);
4014 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4018 nt_status
= sync_files(&cp_clistate
, mask
);
4019 if (!NT_STATUS_IS_OK(nt_status
)) {
4020 d_fprintf(stderr
, _("could not handle files for share: "
4021 "%s\n"), info502
.name
);
4026 nt_status
= NT_STATUS_OK
;
4031 cli_shutdown(cp_clistate
.cli_share_src
);
4034 cli_shutdown(cp_clistate
.cli_share_dst
);
4041 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
4043 if (c
->display_usage
) {
4045 "net share migrate files\n"
4048 _("Migrate files to local server"));
4053 d_printf(_("no server to migrate\n"));
4057 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4058 rpc_share_migrate_files_internals
,
4063 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4065 * All parameters are provided by the run_rpc_command function, except for
4066 * argc, argv which are passed through.
4068 * @param domain_sid The domain sid acquired from the remote server.
4069 * @param cli A cli_state connected to the server.
4070 * @param mem_ctx Talloc context, destroyed on completion of the function.
4071 * @param argc Standard main() style argc.
4072 * @param argv Standard main() style argv. Initial components are already
4075 * @return Normal NTSTATUS return.
4078 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
4079 const struct dom_sid
*domain_sid
,
4080 const char *domain_name
,
4081 struct cli_state
*cli
,
4082 struct rpc_pipe_client
*pipe_hnd
,
4083 TALLOC_CTX
*mem_ctx
,
4088 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
4089 struct srvsvc_NetShareInfoCtr ctr_src
;
4090 union srvsvc_NetShareInfo info
;
4092 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
4093 struct cli_state
*cli_dst
= NULL
;
4094 uint32 level
= 502; /* includes secdesc */
4095 uint32_t parm_error
= 0;
4096 struct dcerpc_binding_handle
*b
;
4098 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
4101 if (!W_ERROR_IS_OK(result
))
4104 /* connect destination PI_SRVSVC */
4105 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
4106 &ndr_table_srvsvc
.syntax_id
);
4107 if (!NT_STATUS_IS_OK(nt_status
))
4110 b
= srvsvc_pipe
->binding_handle
;
4112 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
4114 struct srvsvc_NetShareInfo502 info502
=
4115 ctr_src
.ctr
.ctr502
->array
[i
];
4117 /* reset error-code */
4118 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4120 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
4123 printf(_("migrating: [%s], path: %s, comment: %s, including "
4125 info502
.name
, info502
.path
, info502
.comment
);
4128 display_sec_desc(info502
.sd_buf
.sd
);
4130 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4131 info
.info502
= &info502
;
4133 /* finally modify the share on the dst server */
4134 nt_status
= dcerpc_srvsvc_NetShareSetInfo(b
, mem_ctx
,
4135 srvsvc_pipe
->desthost
,
4141 if (!NT_STATUS_IS_OK(nt_status
)) {
4142 printf(_("cannot set share-acl: %s\n"),
4143 nt_errstr(nt_status
));
4146 if (!W_ERROR_IS_OK(result
)) {
4147 nt_status
= werror_to_ntstatus(result
);
4148 printf(_("cannot set share-acl: %s\n"),
4149 win_errstr(result
));
4155 nt_status
= NT_STATUS_OK
;
4159 cli_shutdown(cli_dst
);
4167 * Migrate share-acls from a RPC server to another.
4169 * @param argc Standard main() style argc.
4170 * @param argv Standard main() style argv. Initial components are already
4173 * @return A shell status integer (0 for success).
4175 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
4178 if (c
->display_usage
) {
4180 "net rpc share migrate security\n"
4183 _("Migrate share-acls to local server"));
4188 d_printf(_("no server to migrate\n"));
4192 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4193 rpc_share_migrate_security_internals
,
4198 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4199 * from one server to another.
4201 * @param argc Standard main() style argc.
4202 * @param argv Standard main() style argv. Initial components are already
4205 * @return A shell status integer (0 for success).
4208 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
4213 if (c
->display_usage
) {
4215 "net rpc share migrate all\n"
4218 _("Migrates shares including all share settings"));
4223 d_printf(_("no server to migrate\n"));
4227 /* order is important. we don't want to be locked out by the share-acl
4228 * before copying files - gd */
4230 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4231 rpc_share_migrate_shares_internals
, argc
, argv
);
4235 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4236 rpc_share_migrate_files_internals
, argc
, argv
);
4240 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4241 rpc_share_migrate_security_internals
, argc
,
4247 * 'net rpc share migrate' entrypoint.
4248 * @param argc Standard main() style argc.
4249 * @param argv Standard main() style argv. Initial components are already
4252 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4255 struct functable func
[] = {
4258 rpc_share_migrate_all
,
4260 N_("Migrate shares from remote to local server"),
4261 N_("net rpc share migrate all\n"
4262 " Migrate shares from remote to local server")
4266 rpc_share_migrate_files
,
4268 N_("Migrate files from remote to local server"),
4269 N_("net rpc share migrate files\n"
4270 " Migrate files from remote to local server")
4274 rpc_share_migrate_security
,
4276 N_("Migrate share-ACLs from remote to local server"),
4277 N_("net rpc share migrate security\n"
4278 " Migrate share-ACLs from remote to local server")
4282 rpc_share_migrate_shares
,
4284 N_("Migrate shares from remote to local server"),
4285 N_("net rpc share migrate shares\n"
4286 " Migrate shares from remote to local server")
4288 {NULL
, NULL
, 0, NULL
, NULL
}
4291 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4293 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
4299 struct dom_sid
*members
;
4302 static int num_server_aliases
;
4303 static struct full_alias
*server_aliases
;
4306 * Add an alias to the static list.
4308 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4310 if (server_aliases
== NULL
)
4311 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4313 server_aliases
[num_server_aliases
] = *alias
;
4314 num_server_aliases
+= 1;
4318 * For a specific domain on the server, fetch all the aliases
4319 * and their members. Add all of them to the server_aliases.
4322 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4323 TALLOC_CTX
*mem_ctx
,
4324 struct policy_handle
*connect_pol
,
4325 const struct dom_sid
*domain_sid
)
4327 uint32 start_idx
, max_entries
, num_entries
, i
;
4328 struct samr_SamArray
*groups
= NULL
;
4329 NTSTATUS result
, status
;
4330 struct policy_handle domain_pol
;
4331 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4333 /* Get domain policy handle */
4335 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
4337 MAXIMUM_ALLOWED_ACCESS
,
4338 discard_const_p(struct dom_sid2
, domain_sid
),
4341 if (!NT_STATUS_IS_OK(status
)) {
4344 if (!NT_STATUS_IS_OK(result
)) {
4352 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
4359 if (!NT_STATUS_IS_OK(status
)) {
4362 for (i
= 0; i
< num_entries
; i
++) {
4364 struct policy_handle alias_pol
;
4365 struct full_alias alias
;
4366 struct lsa_SidArray sid_array
;
4370 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
4372 MAXIMUM_ALLOWED_ACCESS
,
4373 groups
->entries
[i
].idx
,
4376 if (!NT_STATUS_IS_OK(status
)) {
4379 if (!NT_STATUS_IS_OK(_result
)) {
4384 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
4388 if (!NT_STATUS_IS_OK(status
)) {
4391 if (!NT_STATUS_IS_OK(_result
)) {
4396 alias
.num_members
= sid_array
.num_sids
;
4398 status
= dcerpc_samr_Close(b
, mem_ctx
, &alias_pol
, &_result
);
4399 if (!NT_STATUS_IS_OK(status
)) {
4402 if (!NT_STATUS_IS_OK(_result
)) {
4407 alias
.members
= NULL
;
4409 if (alias
.num_members
> 0) {
4410 alias
.members
= SMB_MALLOC_ARRAY(struct dom_sid
, alias
.num_members
);
4412 for (j
= 0; j
< alias
.num_members
; j
++)
4413 sid_copy(&alias
.members
[j
],
4414 sid_array
.sids
[j
].sid
);
4417 sid_compose(&alias
.sid
, domain_sid
,
4418 groups
->entries
[i
].idx
);
4420 push_alias(mem_ctx
, &alias
);
4422 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4424 status
= NT_STATUS_OK
;
4427 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
4433 * Dump server_aliases as names for debugging purposes.
4436 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4437 const struct dom_sid
*domain_sid
,
4438 const char *domain_name
,
4439 struct cli_state
*cli
,
4440 struct rpc_pipe_client
*pipe_hnd
,
4441 TALLOC_CTX
*mem_ctx
,
4447 struct policy_handle lsa_pol
;
4448 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4450 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4451 SEC_FLAG_MAXIMUM_ALLOWED
,
4453 if (!NT_STATUS_IS_OK(result
))
4456 for (i
=0; i
<num_server_aliases
; i
++) {
4459 enum lsa_SidType
*types
;
4462 struct full_alias
*alias
= &server_aliases
[i
];
4464 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4466 &domains
, &names
, &types
);
4467 if (!NT_STATUS_IS_OK(result
))
4470 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4472 if (alias
->num_members
== 0) {
4477 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4480 &domains
, &names
, &types
);
4482 if (!NT_STATUS_IS_OK(result
) &&
4483 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4486 for (j
=0; j
<alias
->num_members
; j
++)
4487 DEBUG(1, ("%s\\%s (%d); ",
4488 domains
[j
] ? domains
[j
] : "*unknown*",
4489 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4493 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
4495 return NT_STATUS_OK
;
4499 * Fetch a list of all server aliases and their members into
4503 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4504 const struct dom_sid
*domain_sid
,
4505 const char *domain_name
,
4506 struct cli_state
*cli
,
4507 struct rpc_pipe_client
*pipe_hnd
,
4508 TALLOC_CTX
*mem_ctx
,
4512 NTSTATUS result
, status
;
4513 struct policy_handle connect_pol
;
4514 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4516 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
4518 MAXIMUM_ALLOWED_ACCESS
,
4521 if (!NT_STATUS_IS_OK(status
)) {
4524 if (!NT_STATUS_IS_OK(result
)) {
4529 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4530 &global_sid_Builtin
);
4531 if (!NT_STATUS_IS_OK(status
)) {
4535 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4538 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
4543 static void init_user_token(struct security_token
*token
, struct dom_sid
*user_sid
)
4545 token
->num_sids
= 4;
4547 if (!(token
->sids
= SMB_MALLOC_ARRAY(struct dom_sid
, 4))) {
4548 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4549 token
->num_sids
= 0;
4553 token
->sids
[0] = *user_sid
;
4554 sid_copy(&token
->sids
[1], &global_sid_World
);
4555 sid_copy(&token
->sids
[2], &global_sid_Network
);
4556 sid_copy(&token
->sids
[3], &global_sid_Authenticated_Users
);
4559 static void free_user_token(struct security_token
*token
)
4561 SAFE_FREE(token
->sids
);
4564 static void add_sid_to_token(struct security_token
*token
, struct dom_sid
*sid
)
4566 if (security_token_has_sid(token
, sid
))
4569 token
->sids
= SMB_REALLOC_ARRAY(token
->sids
, struct dom_sid
, token
->num_sids
+1);
4574 sid_copy(&token
->sids
[token
->num_sids
], sid
);
4576 token
->num_sids
+= 1;
4581 struct security_token token
;
4584 static void dump_user_token(struct user_token
*token
)
4588 d_printf("%s\n", token
->name
);
4590 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4591 d_printf(" %s\n", sid_string_tos(&token
->token
.sids
[i
]));
4595 static bool is_alias_member(struct dom_sid
*sid
, struct full_alias
*alias
)
4599 for (i
=0; i
<alias
->num_members
; i
++) {
4600 if (dom_sid_compare(sid
, &alias
->members
[i
]) == 0)
4607 static void collect_sid_memberships(struct security_token
*token
, struct dom_sid sid
)
4611 for (i
=0; i
<num_server_aliases
; i
++) {
4612 if (is_alias_member(&sid
, &server_aliases
[i
]))
4613 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4618 * We got a user token with all the SIDs we can know about without asking the
4619 * server directly. These are the user and domain group sids. All of these can
4620 * be members of aliases. So scan the list of aliases for each of the SIDs and
4621 * add them to the token.
4624 static void collect_alias_memberships(struct security_token
*token
)
4626 int num_global_sids
= token
->num_sids
;
4629 for (i
=0; i
<num_global_sids
; i
++) {
4630 collect_sid_memberships(token
, token
->sids
[i
]);
4634 static bool get_user_sids(const char *domain
, const char *user
, struct security_token
*token
)
4636 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4637 enum wbcSidType type
;
4639 struct wbcDomainSid wsid
;
4640 char sid_str
[WBC_SID_STRING_BUFLEN
];
4641 struct dom_sid user_sid
;
4642 uint32_t num_groups
;
4643 gid_t
*groups
= NULL
;
4646 fstr_sprintf(full_name
, "%s%c%s",
4647 domain
, *lp_winbind_separator(), user
);
4649 /* First let's find out the user sid */
4651 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4653 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4654 DEBUG(1, ("winbind could not find %s: %s\n",
4655 full_name
, wbcErrorString(wbc_status
)));
4659 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4661 if (type
!= WBC_SID_NAME_USER
) {
4662 DEBUG(1, ("%s is not a user\n", full_name
));
4666 if (!string_to_sid(&user_sid
, sid_str
)) {
4667 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4671 init_user_token(token
, &user_sid
);
4673 /* And now the groups winbind knows about */
4675 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4676 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4677 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4678 full_name
, wbcErrorString(wbc_status
)));
4682 for (i
= 0; i
< num_groups
; i
++) {
4683 gid_t gid
= groups
[i
];
4687 wbc_status
= wbcGidToSid(gid
, &wsid
);
4688 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4689 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4690 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4691 wbcFreeMemory(groups
);
4695 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4697 DEBUG(3, (" %s\n", sid_str
));
4699 ok
= string_to_sid(&sid
, sid_str
);
4701 DEBUG(1, ("Failed to convert string to SID\n"));
4702 wbcFreeMemory(groups
);
4705 add_sid_to_token(token
, &sid
);
4707 wbcFreeMemory(groups
);
4713 * Get a list of all user tokens we want to look at
4716 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4717 struct user_token
**user_tokens
)
4719 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4720 uint32_t i
, num_users
;
4722 struct user_token
*result
;
4723 TALLOC_CTX
*frame
= NULL
;
4725 if (lp_winbind_use_default_domain() &&
4726 (c
->opt_target_workgroup
== NULL
)) {
4727 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4728 "please specify a workgroup\n"));
4732 /* Send request to winbind daemon */
4734 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4735 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4736 DEBUG(1, (_("winbind could not list users: %s\n"),
4737 wbcErrorString(wbc_status
)));
4741 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4743 if (result
== NULL
) {
4744 DEBUG(1, ("Could not malloc sid array\n"));
4745 wbcFreeMemory(users
);
4749 frame
= talloc_stackframe();
4750 for (i
=0; i
< num_users
; i
++) {
4751 fstring domain
, user
;
4754 fstrcpy(result
[i
].name
, users
[i
]);
4756 p
= strchr(users
[i
], *lp_winbind_separator());
4758 DEBUG(3, ("%s\n", users
[i
]));
4761 fstrcpy(domain
, c
->opt_target_workgroup
);
4762 fstrcpy(user
, users
[i
]);
4765 fstrcpy(domain
, users
[i
]);
4766 if (!strupper_m(domain
)) {
4767 DEBUG(1, ("strupper_m %s failed\n", domain
));
4768 wbcFreeMemory(users
);
4774 get_user_sids(domain
, user
, &(result
[i
].token
));
4777 wbcFreeMemory(users
);
4779 *num_tokens
= num_users
;
4780 *user_tokens
= result
;
4785 static bool get_user_tokens_from_file(FILE *f
,
4787 struct user_token
**tokens
)
4789 struct user_token
*token
= NULL
;
4794 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4798 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4799 line
[strlen(line
)-1] = '\0';
4802 if (line
[0] == ' ') {
4806 if(!string_to_sid(&sid
, &line
[1])) {
4807 DEBUG(1,("get_user_tokens_from_file: Could "
4808 "not convert sid %s \n",&line
[1]));
4812 if (token
== NULL
) {
4813 DEBUG(0, ("File does not begin with username"));
4817 add_sid_to_token(&token
->token
, &sid
);
4821 /* And a new user... */
4824 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4825 if (*tokens
== NULL
) {
4826 DEBUG(0, ("Could not realloc tokens\n"));
4830 token
= &((*tokens
)[*num_tokens
-1]);
4832 if (strlcpy(token
->name
, line
, sizeof(token
->name
)) >= sizeof(token
->name
)) {
4835 token
->token
.num_sids
= 0;
4836 token
->token
.sids
= NULL
;
4845 * Show the list of all users that have access to a share
4848 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4849 TALLOC_CTX
*mem_ctx
,
4850 const char *netname
,
4852 struct user_token
*tokens
)
4855 struct security_descriptor
*share_sd
= NULL
;
4856 struct security_descriptor
*root_sd
= NULL
;
4857 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4859 union srvsvc_NetShareInfo info
;
4863 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4865 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
4872 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4873 DEBUG(1, ("Coult not query secdesc for share %s\n",
4878 share_sd
= info
.info502
->sd_buf
.sd
;
4879 if (share_sd
== NULL
) {
4880 DEBUG(1, ("Got no secdesc for share %s\n",
4884 cnum
= cli_state_get_tid(cli
);
4886 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, netname
, "A:", "", 0))) {
4890 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4891 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4892 cli_query_secdesc(cli
, fnum
, mem_ctx
, &root_sd
);
4895 for (i
=0; i
<num_tokens
; i
++) {
4898 if (share_sd
!= NULL
) {
4899 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4902 if (!NT_STATUS_IS_OK(status
)) {
4903 DEBUG(1, ("Could not check share_sd for "
4910 if (root_sd
== NULL
) {
4911 d_printf(" %s\n", tokens
[i
].name
);
4915 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4917 if (!NT_STATUS_IS_OK(status
)) {
4918 DEBUG(1, ("Could not check root_sd for user %s\n",
4922 d_printf(" %s\n", tokens
[i
].name
);
4925 if (fnum
!= (uint16_t)-1)
4926 cli_close(cli
, fnum
);
4928 cli_state_set_tid(cli
, cnum
);
4934 * List shares on a remote RPC server, including the security descriptors.
4936 * All parameters are provided by the run_rpc_command function, except for
4937 * argc, argv which are passed through.
4939 * @param domain_sid The domain sid acquired from the remote server.
4940 * @param cli A cli_state connected to the server.
4941 * @param mem_ctx Talloc context, destroyed on completion of the function.
4942 * @param argc Standard main() style argc.
4943 * @param argv Standard main() style argv. Initial components are already
4946 * @return Normal NTSTATUS return.
4949 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4950 const struct dom_sid
*domain_sid
,
4951 const char *domain_name
,
4952 struct cli_state
*cli
,
4953 struct rpc_pipe_client
*pipe_hnd
,
4954 TALLOC_CTX
*mem_ctx
,
4960 NTSTATUS nt_status
= NT_STATUS_OK
;
4961 uint32_t total_entries
= 0;
4962 uint32_t resume_handle
= 0;
4963 uint32_t preferred_len
= 0xffffffff;
4965 struct dcerpc_binding_handle
*b
= NULL
;
4966 struct srvsvc_NetShareInfoCtr info_ctr
;
4967 struct srvsvc_NetShareCtr1 ctr1
;
4970 struct user_token
*tokens
= NULL
;
4976 f
= fopen(argv
[0], "r");
4980 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4981 return NT_STATUS_UNSUCCESSFUL
;
4984 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4990 DEBUG(0, ("Could not read users from file\n"));
4991 return NT_STATUS_UNSUCCESSFUL
;
4994 for (i
=0; i
<num_tokens
; i
++)
4995 collect_alias_memberships(&tokens
[i
].token
);
4997 ZERO_STRUCT(info_ctr
);
5001 info_ctr
.ctr
.ctr1
= &ctr1
;
5003 b
= pipe_hnd
->binding_handle
;
5005 /* Issue the NetShareEnum RPC call and retrieve the response */
5006 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
,
5015 /* Was it successful? */
5016 if (!NT_STATUS_IS_OK(nt_status
)) {
5017 /* Nope. Go clean up. */
5021 if (!W_ERROR_IS_OK(result
)) {
5022 /* Nope. Go clean up. */
5023 nt_status
= werror_to_ntstatus(result
);
5027 if (total_entries
== 0) {
5031 /* For each returned entry... */
5032 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
5033 const char *netname
= info_ctr
.ctr
.ctr1
->array
[i
].name
;
5035 if (info_ctr
.ctr
.ctr1
->array
[i
].type
!= STYPE_DISKTREE
) {
5039 d_printf("%s\n", netname
);
5041 show_userlist(pipe_hnd
, mem_ctx
, netname
,
5042 num_tokens
, tokens
);
5045 for (i
=0; i
<num_tokens
; i
++) {
5046 free_user_token(&tokens
[i
].token
);
5053 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
5058 if (c
->display_usage
) {
5060 "net rpc share allowedusers\n"
5063 _("List allowed users"));
5067 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5068 rpc_aliaslist_internals
,
5073 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
, 0,
5079 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
5080 rpc_share_allowedusers_internals
,
5084 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
5087 struct user_token
*tokens
= NULL
;
5091 net_usersidlist_usage(c
, argc
, argv
);
5095 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
5096 DEBUG(0, ("Could not get the user/sid list\n"));
5100 for (i
=0; i
<num_tokens
; i
++) {
5101 dump_user_token(&tokens
[i
]);
5102 free_user_token(&tokens
[i
].token
);
5109 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
5111 d_printf(_("net usersidlist\n"
5112 "\tprints out a list of all users the running winbind knows\n"
5113 "\tabout, together with all their SIDs. This is used as\n"
5114 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5116 net_common_flags_usage(c
, argc
, argv
);
5121 * 'net rpc share' entrypoint.
5122 * @param argc Standard main() style argc.
5123 * @param argv Standard main() style argv. Initial components are already
5127 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
5129 NET_API_STATUS status
;
5131 struct functable func
[] = {
5137 N_("net rpc share add\n"
5145 N_("net rpc share delete\n"
5150 rpc_share_allowedusers
,
5152 N_("Modify allowed users"),
5153 N_("net rpc share allowedusers\n"
5154 " Modify allowed users")
5160 N_("Migrate share to local server"),
5161 N_("net rpc share migrate\n"
5162 " Migrate share to local server")
5169 N_("net rpc share list\n"
5172 {NULL
, NULL
, 0, NULL
, NULL
}
5175 status
= libnetapi_net_init(&c
->netapi_ctx
);
5179 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5180 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5181 if (c
->opt_kerberos
) {
5182 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5186 if (c
->display_usage
) {
5191 " Alias for net rpc share list\n"));
5192 net_display_usage_from_functable(func
);
5196 return rpc_share_list(c
, argc
, argv
);
5199 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5202 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5203 TALLOC_CTX
*mem_ctx
,
5204 struct rpc_sh_ctx
*ctx
,
5205 struct rpc_pipe_client
*pipe_hnd
,
5206 int argc
, const char **argv
)
5209 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
5212 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
5213 TALLOC_CTX
*mem_ctx
,
5214 struct rpc_sh_ctx
*ctx
,
5215 struct rpc_pipe_client
*pipe_hnd
,
5216 int argc
, const char **argv
)
5218 NET_API_STATUS status
;
5219 uint32_t parm_err
= 0;
5220 struct SHARE_INFO_2 i2
;
5222 if ((argc
< 2) || (argc
> 3)) {
5223 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
5225 return NT_STATUS_INVALID_PARAMETER
;
5228 i2
.shi2_netname
= argv
[0];
5229 i2
.shi2_type
= STYPE_DISKTREE
;
5230 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
5231 i2
.shi2_permissions
= 0;
5232 i2
.shi2_max_uses
= 0;
5233 i2
.shi2_current_uses
= 0;
5234 i2
.shi2_path
= argv
[1];
5235 i2
.shi2_passwd
= NULL
;
5237 status
= NetShareAdd(pipe_hnd
->desthost
,
5242 return werror_to_ntstatus(W_ERROR(status
));
5245 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5246 TALLOC_CTX
*mem_ctx
,
5247 struct rpc_sh_ctx
*ctx
,
5248 struct rpc_pipe_client
*pipe_hnd
,
5249 int argc
, const char **argv
)
5252 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5253 return NT_STATUS_INVALID_PARAMETER
;
5256 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
5259 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5260 TALLOC_CTX
*mem_ctx
,
5261 struct rpc_sh_ctx
*ctx
,
5262 struct rpc_pipe_client
*pipe_hnd
,
5263 int argc
, const char **argv
)
5265 union srvsvc_NetShareInfo info
;
5268 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5271 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5272 return NT_STATUS_INVALID_PARAMETER
;
5275 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
5281 if (!NT_STATUS_IS_OK(status
)) {
5282 result
= ntstatus_to_werror(status
);
5285 if (!W_ERROR_IS_OK(result
)) {
5289 d_printf(_("Name: %s\n"), info
.info2
->name
);
5290 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
5291 d_printf(_("Path: %s\n"), info
.info2
->path
);
5292 d_printf(_("Password: %s\n"), info
.info2
->password
);
5295 return werror_to_ntstatus(result
);
5298 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5299 struct rpc_sh_ctx
*ctx
)
5301 static struct rpc_sh_cmd cmds
[] = {
5303 { "list", NULL
, &ndr_table_srvsvc
, rpc_sh_share_list
,
5304 N_("List available shares") },
5306 { "add", NULL
, &ndr_table_srvsvc
, rpc_sh_share_add
,
5307 N_("Add a share") },
5309 { "delete", NULL
, &ndr_table_srvsvc
, rpc_sh_share_delete
,
5310 N_("Delete a share") },
5312 { "info", NULL
, &ndr_table_srvsvc
, rpc_sh_share_info
,
5313 N_("Get information about a share") },
5315 { NULL
, NULL
, 0, NULL
, NULL
}
5321 /****************************************************************************/
5323 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5325 return net_file_usage(c
, argc
, argv
);
5329 * Close a file on a remote RPC server.
5331 * @param argc Standard main() style argc.
5332 * @param argv Standard main() style argv. Initial components are already
5335 * @return A shell status integer (0 for success).
5337 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5339 if (argc
< 1 || c
->display_usage
) {
5340 return rpc_file_usage(c
, argc
, argv
);
5343 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
5347 * Formatted print of open file info
5349 * @param r struct FILE_INFO_3 contents
5352 static void display_file_info_3(struct FILE_INFO_3
*r
)
5354 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5355 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
5356 r
->fi3_num_locks
, r
->fi3_pathname
);
5360 * List files for a user on a remote RPC server.
5362 * @param argc Standard main() style argc.
5363 * @param argv Standard main() style argv. Initial components are already
5366 * @return A shell status integer (0 for success)..
5369 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5371 NET_API_STATUS status
;
5372 uint32 preferred_len
= 0xffffffff, i
;
5373 const char *username
=NULL
;
5374 uint32_t total_entries
= 0;
5375 uint32_t entries_read
= 0;
5376 uint32_t resume_handle
= 0;
5377 struct FILE_INFO_3
*i3
= NULL
;
5379 if (c
->display_usage
) {
5380 return rpc_file_usage(c
, argc
, argv
);
5383 /* if argc > 0, must be user command */
5385 username
= smb_xstrdup(argv
[0]);
5388 status
= NetFileEnum(c
->opt_host
,
5392 (uint8_t **)(void *)&i3
,
5402 /* Display results */
5405 "\nEnumerating open files on remote server:\n\n"
5406 "\nFileId Opened by Perms Locks Path"
5407 "\n------ --------- ----- ----- ---- \n"));
5408 for (i
= 0; i
< entries_read
; i
++) {
5409 display_file_info_3(&i3
[i
]);
5416 * 'net rpc file' entrypoint.
5417 * @param argc Standard main() style argc.
5418 * @param argv Standard main() style argv. Initial components are already
5422 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5424 NET_API_STATUS status
;
5426 struct functable func
[] = {
5431 N_("Close opened file"),
5432 N_("net rpc file close\n"
5433 " Close opened file")
5439 N_("List files opened by user"),
5440 N_("net rpc file user\n"
5441 " List files opened by user")
5448 N_("Display information about opened file"),
5449 N_("net rpc file info\n"
5450 " Display information about opened file")
5453 {NULL
, NULL
, 0, NULL
, NULL
}
5456 status
= libnetapi_net_init(&c
->netapi_ctx
);
5460 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5461 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5462 if (c
->opt_kerberos
) {
5463 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5467 if (c
->display_usage
) {
5468 d_printf(_("Usage:\n"));
5469 d_printf(_("net rpc file\n"
5470 " List opened files\n"));
5471 net_display_usage_from_functable(func
);
5475 return rpc_file_user(c
, argc
, argv
);
5478 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5482 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5484 * All parameters are provided by the run_rpc_command function, except for
5485 * argc, argv which are passed through.
5487 * @param c A net_context structure.
5488 * @param domain_sid The domain sid acquired from the remote server.
5489 * @param cli A cli_state connected to the server.
5490 * @param mem_ctx Talloc context, destroyed on completion of the function.
5491 * @param argc Standard main() style argc.
5492 * @param argv Standard main() style argv. Initial components are already
5495 * @return Normal NTSTATUS return.
5498 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5499 const struct dom_sid
*domain_sid
,
5500 const char *domain_name
,
5501 struct cli_state
*cli
,
5502 struct rpc_pipe_client
*pipe_hnd
,
5503 TALLOC_CTX
*mem_ctx
,
5507 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5509 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5511 status
= dcerpc_initshutdown_Abort(b
, mem_ctx
, NULL
, &result
);
5512 if (!NT_STATUS_IS_OK(status
)) {
5515 if (W_ERROR_IS_OK(result
)) {
5516 d_printf(_("\nShutdown successfully aborted\n"));
5517 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5519 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5521 return werror_to_ntstatus(result
);
5525 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5527 * All parameters are provided by the run_rpc_command function, except for
5528 * argc, argv which are passed through.
5530 * @param c A net_context structure.
5531 * @param domain_sid The domain sid acquired from the remote server.
5532 * @param cli A cli_state connected to the server.
5533 * @param mem_ctx Talloc context, destroyed on completion of the function.
5534 * @param argc Standard main() style argc.
5535 * @param argv Standard main() style argv. Initial components are already
5538 * @return Normal NTSTATUS return.
5541 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5542 const struct dom_sid
*domain_sid
,
5543 const char *domain_name
,
5544 struct cli_state
*cli
,
5545 struct rpc_pipe_client
*pipe_hnd
,
5546 TALLOC_CTX
*mem_ctx
,
5550 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5552 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5554 result
= dcerpc_winreg_AbortSystemShutdown(b
, mem_ctx
, NULL
, &werr
);
5556 if (!NT_STATUS_IS_OK(result
)) {
5557 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5560 if (W_ERROR_IS_OK(werr
)) {
5561 d_printf(_("\nShutdown successfully aborted\n"));
5562 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5564 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5566 return werror_to_ntstatus(werr
);
5570 * ABORT the shutdown of a remote RPC server.
5572 * @param argc Standard main() style argc.
5573 * @param argv Standard main() style argv. Initial components are already
5576 * @return A shell status integer (0 for success).
5579 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5584 if (c
->display_usage
) {
5586 "net rpc abortshutdown\n"
5589 _("Abort a scheduled shutdown"));
5593 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5594 rpc_shutdown_abort_internals
, argc
, argv
);
5599 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5601 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5602 rpc_reg_shutdown_abort_internals
,
5607 * Shut down a remote RPC Server via initshutdown pipe.
5609 * All parameters are provided by the run_rpc_command function, except for
5610 * argc, argv which are passed through.
5612 * @param c A net_context structure.
5613 * @param domain_sid The domain sid acquired from the remote server.
5614 * @param cli A cli_state connected to the server.
5615 * @param mem_ctx Talloc context, destroyed on completion of the function.
5616 * @param argc Standard main() style argc.
5617 * @param argv Standard main() style argv. Initial components are already
5620 * @return Normal NTSTATUS return.
5623 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5624 const struct dom_sid
*domain_sid
,
5625 const char *domain_name
,
5626 struct cli_state
*cli
,
5627 struct rpc_pipe_client
*pipe_hnd
,
5628 TALLOC_CTX
*mem_ctx
,
5632 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5634 const char *msg
= N_("This machine will be shutdown shortly");
5635 uint32 timeout
= 20;
5636 struct lsa_StringLarge msg_string
;
5637 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5639 if (c
->opt_comment
) {
5640 msg
= c
->opt_comment
;
5642 if (c
->opt_timeout
) {
5643 timeout
= c
->opt_timeout
;
5646 msg_string
.string
= msg
;
5648 /* create an entry */
5649 status
= dcerpc_initshutdown_Init(b
, mem_ctx
, NULL
,
5650 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5652 if (!NT_STATUS_IS_OK(status
)) {
5655 if (W_ERROR_IS_OK(result
)) {
5656 d_printf(_("\nShutdown of remote machine succeeded\n"));
5657 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5659 DEBUG(1,("Shutdown of remote machine failed!\n"));
5661 return werror_to_ntstatus(result
);
5665 * Shut down a remote RPC Server via winreg pipe.
5667 * All parameters are provided by the run_rpc_command function, except for
5668 * argc, argv which are passed through.
5670 * @param c A net_context structure.
5671 * @param domain_sid The domain sid acquired from the remote server.
5672 * @param cli A cli_state connected to the server.
5673 * @param mem_ctx Talloc context, destroyed on completion of the function.
5674 * @param argc Standard main() style argc.
5675 * @param argv Standard main() style argv. Initial components are already
5678 * @return Normal NTSTATUS return.
5681 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5682 const struct dom_sid
*domain_sid
,
5683 const char *domain_name
,
5684 struct cli_state
*cli
,
5685 struct rpc_pipe_client
*pipe_hnd
,
5686 TALLOC_CTX
*mem_ctx
,
5690 const char *msg
= N_("This machine will be shutdown shortly");
5691 uint32 timeout
= 20;
5692 struct lsa_StringLarge msg_string
;
5695 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5697 if (c
->opt_comment
) {
5698 msg
= c
->opt_comment
;
5700 msg_string
.string
= msg
;
5702 if (c
->opt_timeout
) {
5703 timeout
= c
->opt_timeout
;
5706 /* create an entry */
5707 result
= dcerpc_winreg_InitiateSystemShutdown(b
, mem_ctx
, NULL
,
5708 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5710 if (!NT_STATUS_IS_OK(result
)) {
5711 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5715 if (W_ERROR_IS_OK(werr
)) {
5716 d_printf(_("\nShutdown of remote machine succeeded\n"));
5718 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5719 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5720 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5722 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5725 return werror_to_ntstatus(werr
);
5729 * Shut down a remote RPC server.
5731 * @param argc Standard main() style argc.
5732 * @param argv Standard main() style argv. Initial components are already
5735 * @return A shell status integer (0 for success).
5738 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5742 if (c
->display_usage
) {
5744 "net rpc shutdown\n"
5747 _("Shut down a remote RPC server"));
5751 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5752 rpc_init_shutdown_internals
, argc
, argv
);
5755 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5756 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5757 rpc_reg_shutdown_internals
, argc
, argv
);
5763 /***************************************************************************
5764 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5765 ***************************************************************************/
5768 * Add interdomain trust account to the RPC server.
5769 * All parameters (except for argc and argv) are passed by run_rpc_command
5772 * @param c A net_context structure.
5773 * @param domain_sid The domain sid acquired from the server.
5774 * @param cli A cli_state connected to the server.
5775 * @param mem_ctx Talloc context, destroyed on completion of the function.
5776 * @param argc Standard main() style argc.
5777 * @param argv Standard main() style argv. Initial components are already
5780 * @return normal NTSTATUS return code.
5783 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5784 const struct dom_sid
*domain_sid
,
5785 const char *domain_name
,
5786 struct cli_state
*cli
,
5787 struct rpc_pipe_client
*pipe_hnd
,
5788 TALLOC_CTX
*mem_ctx
,
5792 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5793 NTSTATUS status
, result
;
5795 struct lsa_String lsa_acct_name
;
5797 uint32 acct_flags
=0;
5799 uint32_t access_granted
= 0;
5800 union samr_UserInfo info
;
5801 unsigned int orig_timeout
;
5802 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5803 DATA_BLOB session_key
= data_blob_null
;
5808 _(" net rpc trustdom add <domain_name> "
5809 "<trust password>\n"));
5810 return NT_STATUS_INVALID_PARAMETER
;
5814 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5817 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5818 return NT_STATUS_NO_MEMORY
;
5821 if (!strupper_m(acct_name
)) {
5822 SAFE_FREE(acct_name
);
5823 return NT_STATUS_INVALID_PARAMETER
;
5826 init_lsa_String(&lsa_acct_name
, acct_name
);
5828 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
5829 if (!NT_STATUS_IS_OK(status
)) {
5830 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
5831 nt_errstr(status
)));
5835 /* Get samr policy handle */
5836 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5838 MAXIMUM_ALLOWED_ACCESS
,
5841 if (!NT_STATUS_IS_OK(status
)) {
5844 if (!NT_STATUS_IS_OK(result
)) {
5849 /* Get domain policy handle */
5850 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5852 MAXIMUM_ALLOWED_ACCESS
,
5853 discard_const_p(struct dom_sid2
, domain_sid
),
5856 if (!NT_STATUS_IS_OK(status
)) {
5859 if (!NT_STATUS_IS_OK(result
)) {
5864 /* This call can take a long time - allow the server to time out.
5865 * 35 seconds should do it. */
5867 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5869 /* Create trusting domain's account */
5870 acb_info
= ACB_NORMAL
;
5871 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5872 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5873 SAMR_USER_ACCESS_SET_PASSWORD
|
5874 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5875 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5877 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
5886 if (!NT_STATUS_IS_OK(status
)) {
5889 /* And restore our original timeout. */
5890 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5892 if (!NT_STATUS_IS_OK(result
)) {
5894 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5895 acct_name
, nt_errstr(result
));
5900 struct samr_CryptPassword crypt_pwd
;
5902 ZERO_STRUCT(info
.info23
);
5904 init_samr_CryptPassword(argv
[1],
5908 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5909 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5910 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5911 info
.info23
.password
= crypt_pwd
;
5913 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
5918 if (!NT_STATUS_IS_OK(status
)) {
5922 if (!NT_STATUS_IS_OK(result
)) {
5924 DEBUG(0,("Could not set trust account password: %s\n",
5925 nt_errstr(result
)));
5931 SAFE_FREE(acct_name
);
5932 data_blob_clear_free(&session_key
);
5937 * Create interdomain trust account for a remote domain.
5939 * @param argc Standard argc.
5940 * @param argv Standard argv without initial components.
5942 * @return Integer status (0 means success).
5945 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5947 if (argc
> 0 && !c
->display_usage
) {
5948 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5949 rpc_trustdom_add_internals
, argc
, argv
);
5953 _("net rpc trustdom add <domain_name> <trust "
5961 * Remove interdomain trust account from the RPC server.
5962 * All parameters (except for argc and argv) are passed by run_rpc_command
5965 * @param c A net_context structure.
5966 * @param domain_sid The domain sid acquired from the server.
5967 * @param cli A cli_state connected to the server.
5968 * @param mem_ctx Talloc context, destroyed on completion of the function.
5969 * @param argc Standard main() style argc.
5970 * @param argv Standard main() style argv. Initial components are already
5973 * @return normal NTSTATUS return code.
5976 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5977 const struct dom_sid
*domain_sid
,
5978 const char *domain_name
,
5979 struct cli_state
*cli
,
5980 struct rpc_pipe_client
*pipe_hnd
,
5981 TALLOC_CTX
*mem_ctx
,
5985 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5986 NTSTATUS status
, result
;
5988 struct dom_sid trust_acct_sid
;
5989 struct samr_Ids user_rids
, name_types
;
5990 struct lsa_String lsa_acct_name
;
5991 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5996 _(" net rpc trustdom del <domain_name>\n"));
5997 return NT_STATUS_INVALID_PARAMETER
;
6001 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6003 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
6005 if (acct_name
== NULL
)
6006 return NT_STATUS_NO_MEMORY
;
6008 if (!strupper_m(acct_name
)) {
6009 TALLOC_FREE(acct_name
);
6010 return NT_STATUS_INVALID_PARAMETER
;
6013 /* Get samr policy handle */
6014 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6016 MAXIMUM_ALLOWED_ACCESS
,
6019 if (!NT_STATUS_IS_OK(status
)) {
6022 if (!NT_STATUS_IS_OK(result
)) {
6027 /* Get domain policy handle */
6028 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6030 MAXIMUM_ALLOWED_ACCESS
,
6031 discard_const_p(struct dom_sid2
, domain_sid
),
6034 if (!NT_STATUS_IS_OK(status
)) {
6037 if (!NT_STATUS_IS_OK(result
)) {
6042 init_lsa_String(&lsa_acct_name
, acct_name
);
6044 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
6051 if (!NT_STATUS_IS_OK(status
)) {
6052 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6054 acct_name
, nt_errstr(status
));
6057 if (!NT_STATUS_IS_OK(result
)) {
6059 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6061 acct_name
, nt_errstr(result
) );
6065 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
6067 MAXIMUM_ALLOWED_ACCESS
,
6071 if (!NT_STATUS_IS_OK(status
)) {
6072 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6074 acct_name
, nt_errstr(status
) );
6078 if (!NT_STATUS_IS_OK(result
)) {
6080 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6082 acct_name
, nt_errstr(result
) );
6086 /* append the rid to the domain sid */
6087 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
6091 /* remove the sid */
6093 status
= dcerpc_samr_RemoveMemberFromForeignDomain(b
, mem_ctx
,
6097 if (!NT_STATUS_IS_OK(status
)) {
6098 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6099 " on user %s failed %s\n"),
6100 acct_name
, nt_errstr(status
));
6103 if (!NT_STATUS_IS_OK(result
)) {
6105 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6106 " on user %s failed %s\n"),
6107 acct_name
, nt_errstr(result
) );
6114 status
= dcerpc_samr_DeleteUser(b
, mem_ctx
,
6117 if (!NT_STATUS_IS_OK(status
)) {
6118 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6120 acct_name
, nt_errstr(status
));
6124 if (!NT_STATUS_IS_OK(result
)) {
6126 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6128 acct_name
, nt_errstr(result
) );
6132 if (!NT_STATUS_IS_OK(result
)) {
6133 d_printf(_("Could not set trust account password: %s\n"),
6143 * Delete interdomain trust account for a remote domain.
6145 * @param argc Standard argc.
6146 * @param argv Standard argv without initial components.
6148 * @return Integer status (0 means success).
6151 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
6153 if (argc
> 0 && !c
->display_usage
) {
6154 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
6155 rpc_trustdom_del_internals
, argc
, argv
);
6159 _("net rpc trustdom del <domain>\n"));
6164 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
6165 struct cli_state
*cli
,
6166 TALLOC_CTX
*mem_ctx
,
6167 const char *domain_name
)
6169 char *dc_name
= NULL
;
6170 const char *buffer
= NULL
;
6171 struct rpc_pipe_client
*netr
;
6174 struct dcerpc_binding_handle
*b
;
6176 /* Use NetServerEnum2 */
6178 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
6180 return NT_STATUS_OK
;
6183 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6184 for domain %s\n", domain_name
));
6186 /* Try netr_GetDcName */
6188 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
6190 if (!NT_STATUS_IS_OK(status
)) {
6194 b
= netr
->binding_handle
;
6196 status
= dcerpc_netr_GetDcName(b
, mem_ctx
,
6203 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(result
)) {
6207 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6208 for domain %s\n", domain_name
));
6210 if (!NT_STATUS_IS_OK(status
)) {
6214 return werror_to_ntstatus(result
);
6218 * Establish trust relationship to a trusting domain.
6219 * Interdomain account must already be created on remote PDC.
6221 * @param c A net_context structure.
6222 * @param argc Standard argc.
6223 * @param argv Standard argv without initial components.
6225 * @return Integer status (0 means success).
6228 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6231 struct cli_state
*cli
= NULL
;
6232 struct sockaddr_storage server_ss
;
6233 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6234 struct policy_handle connect_hnd
;
6235 TALLOC_CTX
*mem_ctx
;
6236 NTSTATUS nt_status
, result
;
6237 struct dom_sid
*domain_sid
;
6242 union lsa_PolicyInformation
*info
= NULL
;
6243 struct dcerpc_binding_handle
*b
;
6246 * Connect to \\server\ipc$ as 'our domain' account with password
6249 if (argc
!= 1 || c
->display_usage
) {
6252 _("net rpc trustdom establish <domain_name>\n"));
6256 domain_name
= smb_xstrdup(argv
[0]);
6257 if (!strupper_m(domain_name
)) {
6258 SAFE_FREE(domain_name
);
6262 /* account name used at first is our domain's name with '$' */
6263 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
6266 if (!strupper_m(acct_name
)) {
6267 SAFE_FREE(domain_name
);
6268 SAFE_FREE(acct_name
);
6273 * opt_workgroup will be used by connection functions further,
6274 * hence it should be set to remote domain name instead of ours
6276 if (c
->opt_workgroup
) {
6277 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6280 c
->opt_user_name
= acct_name
;
6282 /* find the domain controller */
6283 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6284 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6288 /* connect to ipc$ as username/password */
6289 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6290 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6292 /* Is it trusting domain account for sure ? */
6293 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6294 nt_errstr(nt_status
)));
6298 /* store who we connected to */
6300 saf_store( domain_name
, pdc_name
);
6303 * Connect to \\server\ipc$ again (this time anonymously)
6306 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6309 if (NT_STATUS_IS_ERR(nt_status
)) {
6310 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6311 domain_name
, nt_errstr(nt_status
)));
6315 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6316 "domain %s", domain_name
))) {
6317 DEBUG(0, ("talloc_init() failed\n"));
6322 /* Make sure we're talking to a proper server */
6324 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6325 if (!NT_STATUS_IS_OK(nt_status
)) {
6327 talloc_destroy(mem_ctx
);
6332 * Call LsaOpenPolicy and LsaQueryInfo
6335 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6337 if (!NT_STATUS_IS_OK(nt_status
)) {
6338 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6340 talloc_destroy(mem_ctx
);
6344 b
= pipe_hnd
->binding_handle
;
6346 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
6348 if (NT_STATUS_IS_ERR(nt_status
)) {
6349 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6350 nt_errstr(nt_status
)));
6352 talloc_destroy(mem_ctx
);
6356 /* Querying info level 5 */
6358 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6360 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6363 if (NT_STATUS_IS_ERR(nt_status
)) {
6364 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6365 nt_errstr(nt_status
)));
6367 talloc_destroy(mem_ctx
);
6370 if (NT_STATUS_IS_ERR(result
)) {
6371 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6372 nt_errstr(result
)));
6374 talloc_destroy(mem_ctx
);
6378 domain_sid
= info
->account_domain
.sid
;
6380 /* There should be actually query info level 3 (following nt serv behaviour),
6381 but I still don't know if it's _really_ necessary */
6384 * Store the password in secrets db
6387 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6388 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6390 talloc_destroy(mem_ctx
);
6395 * Close the pipes and clean up
6398 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6399 if (NT_STATUS_IS_ERR(nt_status
)) {
6400 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6401 nt_errstr(nt_status
)));
6403 talloc_destroy(mem_ctx
);
6409 talloc_destroy(mem_ctx
);
6411 d_printf(_("Trust to domain %s established\n"), domain_name
);
6416 * Revoke trust relationship to the remote domain.
6418 * @param c A net_context structure.
6419 * @param argc Standard argc.
6420 * @param argv Standard argv without initial components.
6422 * @return Integer status (0 means success).
6425 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6431 if (argc
< 1 || c
->display_usage
) {
6434 _("net rpc trustdom revoke <domain_name>\n"
6435 " Revoke trust relationship\n"
6436 " domain_name\tName of domain to revoke trust\n"));
6440 /* generate upper cased domain name */
6441 domain_name
= smb_xstrdup(argv
[0]);
6442 if (!strupper_m(domain_name
)) {
6443 SAFE_FREE(domain_name
);
6447 /* delete password of the trust */
6448 if (!pdb_del_trusteddom_pw(domain_name
)) {
6449 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6456 SAFE_FREE(domain_name
);
6460 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6461 const struct dom_sid
*domain_sid
,
6462 const char *domain_name
,
6463 struct cli_state
*cli
,
6464 struct rpc_pipe_client
*pipe_hnd
,
6465 TALLOC_CTX
*mem_ctx
,
6470 if (!sid_to_fstring(str_sid
, domain_sid
)) {
6471 return NT_STATUS_UNSUCCESSFUL
;
6473 d_printf("%s\n", str_sid
);
6474 return NT_STATUS_OK
;
6477 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
6481 /* convert sid into ascii string */
6482 sid_to_fstring(ascii_sid
, dom_sid
);
6484 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
6487 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6488 TALLOC_CTX
*mem_ctx
,
6489 struct policy_handle
*pol
,
6490 struct dom_sid dom_sid
,
6491 const char *trusted_dom_name
)
6493 NTSTATUS nt_status
, result
;
6494 union lsa_TrustedDomainInfo
*info
= NULL
;
6495 char *cleartextpwd
= NULL
;
6496 DATA_BLOB session_key
;
6497 DATA_BLOB data
= data_blob_null
;
6498 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6500 nt_status
= dcerpc_lsa_QueryTrustedDomainInfoBySid(b
, mem_ctx
,
6503 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6506 if (NT_STATUS_IS_ERR(nt_status
)) {
6507 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6508 nt_errstr(nt_status
)));
6511 if (NT_STATUS_IS_ERR(result
)) {
6513 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6514 nt_errstr(result
)));
6518 data
= data_blob(info
->password
.password
->data
,
6519 info
->password
.password
->length
);
6521 nt_status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
6522 if (!NT_STATUS_IS_OK(nt_status
)) {
6523 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status
)));
6527 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key
);
6528 data_blob_free(&session_key
);
6530 if (cleartextpwd
== NULL
) {
6531 DEBUG(0,("retrieved NULL password\n"));
6532 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6536 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6537 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6538 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6542 #ifdef DEBUG_PASSWORD
6543 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6544 "password: [%s]\n", trusted_dom_name
,
6545 sid_string_dbg(&dom_sid
), cleartextpwd
));
6549 SAFE_FREE(cleartextpwd
);
6550 data_blob_free(&data
);
6555 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6558 /* common variables */
6559 TALLOC_CTX
* mem_ctx
;
6560 struct cli_state
*cli
= NULL
;
6561 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6562 NTSTATUS nt_status
, result
;
6563 const char *domain_name
= NULL
;
6564 struct policy_handle connect_hnd
;
6565 union lsa_PolicyInformation
*info
= NULL
;
6567 /* trusted domains listing variables */
6568 unsigned int enum_ctx
= 0;
6570 struct lsa_DomainList dom_list
;
6572 struct dcerpc_binding_handle
*b
;
6574 if (c
->display_usage
) {
6576 "net rpc trustdom vampire\n"
6579 _("Vampire trust relationship from remote server"));
6584 * Listing trusted domains (stored in secrets.tdb, if local)
6587 mem_ctx
= talloc_init("trust relationships vampire");
6590 * set domain and pdc name to local samba server (default)
6591 * or to remote one given in command line
6594 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6595 domain_name
= c
->opt_workgroup
;
6596 c
->opt_target_workgroup
= c
->opt_workgroup
;
6598 fstrcpy(pdc_name
, lp_netbios_name());
6599 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6600 c
->opt_target_workgroup
= domain_name
;
6603 /* open \PIPE\lsarpc and open policy handle */
6604 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6605 if (!NT_STATUS_IS_OK(nt_status
)) {
6606 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6607 nt_errstr(nt_status
)));
6608 talloc_destroy(mem_ctx
);
6612 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6614 if (!NT_STATUS_IS_OK(nt_status
)) {
6615 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6616 nt_errstr(nt_status
) ));
6618 talloc_destroy(mem_ctx
);
6622 b
= pipe_hnd
->binding_handle
;
6624 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6626 if (NT_STATUS_IS_ERR(nt_status
)) {
6627 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6628 nt_errstr(nt_status
)));
6630 talloc_destroy(mem_ctx
);
6634 /* query info level 5 to obtain sid of a domain being queried */
6635 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6637 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6641 if (NT_STATUS_IS_ERR(nt_status
)) {
6642 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6643 nt_errstr(nt_status
)));
6645 talloc_destroy(mem_ctx
);
6648 if (NT_STATUS_IS_ERR(result
)) {
6649 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6650 nt_errstr(result
)));
6652 talloc_destroy(mem_ctx
);
6657 * Keep calling LsaEnumTrustdom over opened pipe until
6658 * the end of enumeration is reached
6661 d_printf(_("Vampire trusted domains:\n\n"));
6664 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6670 if (NT_STATUS_IS_ERR(nt_status
)) {
6671 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6672 nt_errstr(nt_status
)));
6674 talloc_destroy(mem_ctx
);
6677 if (NT_STATUS_IS_ERR(result
)) {
6679 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6680 nt_errstr(result
)));
6682 talloc_destroy(mem_ctx
);
6687 for (i
= 0; i
< dom_list
.count
; i
++) {
6689 print_trusted_domain(dom_list
.domains
[i
].sid
,
6690 dom_list
.domains
[i
].name
.string
);
6692 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6693 *dom_list
.domains
[i
].sid
,
6694 dom_list
.domains
[i
].name
.string
);
6695 if (!NT_STATUS_IS_OK(nt_status
)) {
6697 talloc_destroy(mem_ctx
);
6703 * in case of no trusted domains say something rather
6704 * than just display blank line
6706 if (!dom_list
.count
) d_printf(_("none\n"));
6708 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6710 /* close this connection before doing next one */
6711 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6712 if (NT_STATUS_IS_ERR(nt_status
)) {
6713 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6714 nt_errstr(nt_status
)));
6716 talloc_destroy(mem_ctx
);
6720 /* close lsarpc pipe and connection to IPC$ */
6723 talloc_destroy(mem_ctx
);
6727 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6729 /* common variables */
6730 TALLOC_CTX
* mem_ctx
;
6731 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6732 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6733 NTSTATUS nt_status
, result
;
6734 const char *domain_name
= NULL
;
6735 struct dom_sid
*queried_dom_sid
;
6736 int ascii_dom_name_len
;
6737 struct policy_handle connect_hnd
;
6738 union lsa_PolicyInformation
*info
= NULL
;
6739 struct dcerpc_binding_handle
*b
= NULL
;
6741 /* trusted domains listing variables */
6742 unsigned int num_domains
, enum_ctx
= 0;
6744 struct lsa_DomainList dom_list
;
6748 /* trusting domains listing variables */
6749 struct policy_handle domain_hnd
;
6750 struct samr_SamArray
*trusts
= NULL
;
6752 if (c
->display_usage
) {
6754 "net rpc trustdom list\n"
6757 _("List incoming and outgoing trust relationships"));
6762 * Listing trusted domains (stored in secrets.tdb, if local)
6765 mem_ctx
= talloc_init("trust relationships listing");
6768 * set domain and pdc name to local samba server (default)
6769 * or to remote one given in command line
6772 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6773 domain_name
= c
->opt_workgroup
;
6774 c
->opt_target_workgroup
= c
->opt_workgroup
;
6776 fstrcpy(pdc_name
, lp_netbios_name());
6777 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6778 c
->opt_target_workgroup
= domain_name
;
6781 /* open \PIPE\lsarpc and open policy handle */
6782 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6783 if (!NT_STATUS_IS_OK(nt_status
)) {
6784 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6785 nt_errstr(nt_status
)));
6786 talloc_destroy(mem_ctx
);
6790 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6792 if (!NT_STATUS_IS_OK(nt_status
)) {
6793 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6794 nt_errstr(nt_status
) ));
6796 talloc_destroy(mem_ctx
);
6800 b
= pipe_hnd
->binding_handle
;
6802 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6804 if (NT_STATUS_IS_ERR(nt_status
)) {
6805 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6806 nt_errstr(nt_status
)));
6808 talloc_destroy(mem_ctx
);
6812 /* query info level 5 to obtain sid of a domain being queried */
6813 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6815 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6819 if (NT_STATUS_IS_ERR(nt_status
)) {
6820 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6821 nt_errstr(nt_status
)));
6823 talloc_destroy(mem_ctx
);
6826 if (NT_STATUS_IS_ERR(result
)) {
6827 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6828 nt_errstr(result
)));
6830 talloc_destroy(mem_ctx
);
6834 queried_dom_sid
= info
->account_domain
.sid
;
6837 * Keep calling LsaEnumTrustdom over opened pipe until
6838 * the end of enumeration is reached
6841 d_printf(_("Trusted domains list:\n\n"));
6843 found_domain
= false;
6846 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6852 if (NT_STATUS_IS_ERR(nt_status
)) {
6853 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6854 nt_errstr(nt_status
)));
6856 talloc_destroy(mem_ctx
);
6859 if (NT_STATUS_IS_ERR(result
)) {
6860 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6861 nt_errstr(result
)));
6863 talloc_destroy(mem_ctx
);
6868 for (i
= 0; i
< dom_list
.count
; i
++) {
6869 print_trusted_domain(dom_list
.domains
[i
].sid
,
6870 dom_list
.domains
[i
].name
.string
);
6871 found_domain
= true;
6875 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6878 * in case of no trusted domains say something rather
6879 * than just display blank line
6881 if (!found_domain
) {
6882 d_printf(_("none\n"));
6885 /* close this connection before doing next one */
6886 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6887 if (NT_STATUS_IS_ERR(nt_status
)) {
6888 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6889 nt_errstr(nt_status
)));
6891 talloc_destroy(mem_ctx
);
6895 TALLOC_FREE(pipe_hnd
);
6898 * Listing trusting domains (stored in passdb backend, if local)
6901 d_printf(_("\nTrusting domains list:\n\n"));
6904 * Open \PIPE\samr and get needed policy handles
6906 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6908 if (!NT_STATUS_IS_OK(nt_status
)) {
6909 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6911 talloc_destroy(mem_ctx
);
6915 b
= pipe_hnd
->binding_handle
;
6918 nt_status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6920 SAMR_ACCESS_LOOKUP_DOMAIN
,
6923 if (!NT_STATUS_IS_OK(nt_status
)) {
6924 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6925 nt_errstr(nt_status
)));
6927 talloc_destroy(mem_ctx
);
6930 if (!NT_STATUS_IS_OK(result
)) {
6932 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6933 nt_errstr(result
)));
6935 talloc_destroy(mem_ctx
);
6939 /* SamrOpenDomain - we have to open domain policy handle in order to be
6940 able to enumerate accounts*/
6941 nt_status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6943 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6947 if (!NT_STATUS_IS_OK(nt_status
)) {
6948 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6949 nt_errstr(nt_status
)));
6951 talloc_destroy(mem_ctx
);
6954 if (!NT_STATUS_IS_OK(result
)) {
6956 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6957 nt_errstr(result
)));
6959 talloc_destroy(mem_ctx
);
6964 * perform actual enumeration
6967 found_domain
= false;
6969 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6972 nt_status
= dcerpc_samr_EnumDomainUsers(b
, mem_ctx
,
6980 if (NT_STATUS_IS_ERR(nt_status
)) {
6981 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6982 nt_errstr(nt_status
)));
6984 talloc_destroy(mem_ctx
);
6987 if (NT_STATUS_IS_ERR(result
)) {
6989 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6990 nt_errstr(result
)));
6992 talloc_destroy(mem_ctx
);
6996 for (i
= 0; i
< num_domains
; i
++) {
6998 char *str
= discard_const_p(char, trusts
->entries
[i
].name
.string
);
7000 found_domain
= true;
7003 * get each single domain's sid (do we _really_ need this ?):
7004 * 1) connect to domain's pdc
7005 * 2) query the pdc for domain's sid
7008 /* get rid of '$' tail */
7009 ascii_dom_name_len
= strlen(str
);
7010 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
7011 str
[ascii_dom_name_len
- 1] = '\0';
7013 /* set opt_* variables to remote domain */
7014 if (!strupper_m(str
)) {
7016 talloc_destroy(mem_ctx
);
7019 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
7020 c
->opt_target_workgroup
= c
->opt_workgroup
;
7022 d_printf("%-20s", str
);
7024 /* connect to remote domain controller */
7025 nt_status
= net_make_ipc_connection(c
,
7026 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
7028 if (NT_STATUS_IS_OK(nt_status
)) {
7029 /* query for domain's sid */
7030 if (run_rpc_command(
7032 &ndr_table_lsarpc
, 0,
7033 rpc_query_domain_sid
, argc
,
7035 d_printf(_("strange - couldn't get domain's sid\n"));
7037 cli_shutdown(remote_cli
);
7040 d_fprintf(stderr
, _("domain controller is not "
7041 "responding: %s\n"),
7042 nt_errstr(nt_status
));
7043 d_printf(_("couldn't get domain's sid\n"));
7047 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
7049 if (!found_domain
) {
7053 /* close opened samr and domain policy handles */
7054 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &domain_hnd
, &result
);
7055 if (!NT_STATUS_IS_OK(nt_status
)) {
7056 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
7059 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &connect_hnd
, &result
);
7060 if (!NT_STATUS_IS_OK(nt_status
)) {
7061 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
7064 /* close samr pipe and connection to IPC$ */
7067 talloc_destroy(mem_ctx
);
7072 * Entrypoint for 'net rpc trustdom' code.
7074 * @param argc Standard argc.
7075 * @param argv Standard argv without initial components.
7077 * @return Integer status (0 means success).
7080 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
7082 struct functable func
[] = {
7087 N_("Add trusting domain's account"),
7088 N_("net rpc trustdom add\n"
7089 " Add trusting domain's account")
7095 N_("Remove trusting domain's account"),
7096 N_("net rpc trustdom del\n"
7097 " Remove trusting domain's account")
7101 rpc_trustdom_establish
,
7103 N_("Establish outgoing trust relationship"),
7104 N_("net rpc trustdom establish\n"
7105 " Establish outgoing trust relationship")
7109 rpc_trustdom_revoke
,
7111 N_("Revoke outgoing trust relationship"),
7112 N_("net rpc trustdom revoke\n"
7113 " Revoke outgoing trust relationship")
7119 N_("List in- and outgoing domain trusts"),
7120 N_("net rpc trustdom list\n"
7121 " List in- and outgoing domain trusts")
7125 rpc_trustdom_vampire
,
7127 N_("Vampire trusts from remote server"),
7128 N_("net rpc trustdom vampire\n"
7129 " Vampire trusts from remote server")
7131 {NULL
, NULL
, 0, NULL
, NULL
}
7134 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
7138 * Check if a server will take rpc commands
7139 * @param flags Type of server to connect to (PDC, DMB, localhost)
7140 * if the host is not explicitly specified
7141 * @return bool (true means rpc supported)
7143 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
7145 struct cli_state
*cli
;
7147 struct sockaddr_storage server_ss
;
7148 char *server_name
= NULL
;
7151 /* flags (i.e. server type) may depend on command */
7152 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
7155 status
= cli_connect_nb(server_name
, &server_ss
, 0, 0x20,
7156 lp_netbios_name(), SMB_SIGNING_DEFAULT
,
7158 if (!NT_STATUS_IS_OK(status
)) {
7161 status
= smbXcli_negprot(cli
->conn
, cli
->timeout
, PROTOCOL_CORE
,
7163 if (!NT_STATUS_IS_OK(status
))
7165 if (smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_NT1
)
7174 /* dump sam database via samsync rpc calls */
7175 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
7176 if (c
->display_usage
) {
7181 _("Dump remote SAM database"));
7185 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
,
7186 NET_FLAGS_ANONYMOUS
,
7187 rpc_samdump_internals
, argc
, argv
);
7190 /* syncronise sam database via samsync rpc calls */
7191 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
7193 struct functable func
[] = {
7198 N_("Dump remote SAM database to ldif"),
7199 N_("net rpc vampire ldif\n"
7200 " Dump remote SAM database to LDIF file or "
7207 N_("Dump remote SAM database to Kerberos Keytab"),
7208 N_("net rpc vampire keytab\n"
7209 " Dump remote SAM database to Kerberos keytab "
7216 N_("Dump remote SAM database to passdb"),
7217 N_("net rpc vampire passdb\n"
7218 " Dump remote SAM database to passdb")
7221 {NULL
, NULL
, 0, NULL
, NULL
}
7225 if (c
->display_usage
) {
7230 _("Vampire remote SAM database"));
7234 return rpc_vampire_passdb(c
, argc
, argv
);
7237 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
7241 * Migrate everything from a print server.
7243 * @param c A net_context structure.
7244 * @param argc Standard main() style argc.
7245 * @param argv Standard main() style argv. Initial components are already
7248 * @return A shell status integer (0 for success).
7250 * The order is important !
7251 * To successfully add drivers the print queues have to exist !
7252 * Applying ACLs should be the last step, because you're easily locked out.
7255 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
7260 if (c
->display_usage
) {
7262 "net rpc printer migrate all\n"
7265 _("Migrate everything from a print server"));
7270 d_printf(_("no server to migrate\n"));
7274 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7275 rpc_printer_migrate_printers_internals
, argc
,
7280 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7281 rpc_printer_migrate_drivers_internals
, argc
,
7286 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7287 rpc_printer_migrate_forms_internals
, argc
, argv
);
7291 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7292 rpc_printer_migrate_settings_internals
, argc
,
7297 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7298 rpc_printer_migrate_security_internals
, argc
,
7304 * Migrate print drivers from a print server.
7306 * @param c A net_context structure.
7307 * @param argc Standard main() style argc.
7308 * @param argv Standard main() style argv. Initial components are already
7311 * @return A shell status integer (0 for success).
7313 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
7316 if (c
->display_usage
) {
7318 "net rpc printer migrate drivers\n"
7321 _("Migrate print-drivers from a print-server"));
7326 d_printf(_("no server to migrate\n"));
7330 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7331 rpc_printer_migrate_drivers_internals
,
7336 * Migrate print-forms from a print-server.
7338 * @param c A net_context structure.
7339 * @param argc Standard main() style argc.
7340 * @param argv Standard main() style argv. Initial components are already
7343 * @return A shell status integer (0 for success).
7345 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7348 if (c
->display_usage
) {
7350 "net rpc printer migrate forms\n"
7353 _("Migrate print-forms from a print-server"));
7358 d_printf(_("no server to migrate\n"));
7362 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7363 rpc_printer_migrate_forms_internals
,
7368 * Migrate printers from a print-server.
7370 * @param c A net_context structure.
7371 * @param argc Standard main() style argc.
7372 * @param argv Standard main() style argv. Initial components are already
7375 * @return A shell status integer (0 for success).
7377 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7380 if (c
->display_usage
) {
7382 "net rpc printer migrate printers\n"
7385 _("Migrate printers from a print-server"));
7390 d_printf(_("no server to migrate\n"));
7394 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7395 rpc_printer_migrate_printers_internals
,
7400 * Migrate printer-ACLs from a print-server
7402 * @param c A net_context structure.
7403 * @param argc Standard main() style argc.
7404 * @param argv Standard main() style argv. Initial components are already
7407 * @return A shell status integer (0 for success).
7409 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7412 if (c
->display_usage
) {
7414 "net rpc printer migrate security\n"
7417 _("Migrate printer-ACLs from a print-server"));
7422 d_printf(_("no server to migrate\n"));
7426 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7427 rpc_printer_migrate_security_internals
,
7432 * Migrate printer-settings from a print-server.
7434 * @param c A net_context structure.
7435 * @param argc Standard main() style argc.
7436 * @param argv Standard main() style argv. Initial components are already
7439 * @return A shell status integer (0 for success).
7441 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7444 if (c
->display_usage
) {
7446 "net rpc printer migrate settings\n"
7449 _("Migrate printer-settings from a "
7455 d_printf(_("no server to migrate\n"));
7459 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7460 rpc_printer_migrate_settings_internals
,
7465 * 'net rpc printer' entrypoint.
7467 * @param c A net_context structure.
7468 * @param argc Standard main() style argc.
7469 * @param argv Standard main() style argv. Initial components are already
7473 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7476 /* ouch: when addriver and setdriver are called from within
7477 rpc_printer_migrate_drivers_internals, the printer-queue already
7480 struct functable func
[] = {
7483 rpc_printer_migrate_all
,
7485 N_("Migrate all from remote to local print server"),
7486 N_("net rpc printer migrate all\n"
7487 " Migrate all from remote to local print server")
7491 rpc_printer_migrate_drivers
,
7493 N_("Migrate drivers to local server"),
7494 N_("net rpc printer migrate drivers\n"
7495 " Migrate drivers to local server")
7499 rpc_printer_migrate_forms
,
7501 N_("Migrate froms to local server"),
7502 N_("net rpc printer migrate forms\n"
7503 " Migrate froms to local server")
7507 rpc_printer_migrate_printers
,
7509 N_("Migrate printers to local server"),
7510 N_("net rpc printer migrate printers\n"
7511 " Migrate printers to local server")
7515 rpc_printer_migrate_security
,
7517 N_("Mirgate printer ACLs to local server"),
7518 N_("net rpc printer migrate security\n"
7519 " Mirgate printer ACLs to local server")
7523 rpc_printer_migrate_settings
,
7525 N_("Migrate printer settings to local server"),
7526 N_("net rpc printer migrate settings\n"
7527 " Migrate printer settings to local server")
7529 {NULL
, NULL
, 0, NULL
, NULL
}
7532 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7537 * List printers on a remote RPC server.
7539 * @param c A net_context structure.
7540 * @param argc Standard main() style argc.
7541 * @param argv Standard main() style argv. Initial components are already
7544 * @return A shell status integer (0 for success).
7546 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7548 if (c
->display_usage
) {
7550 "net rpc printer list\n"
7553 _("List printers on a remote RPC server"));
7557 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7558 rpc_printer_list_internals
,
7563 * List printer-drivers on a remote RPC server.
7565 * @param c A net_context structure.
7566 * @param argc Standard main() style argc.
7567 * @param argv Standard main() style argv. Initial components are already
7570 * @return A shell status integer (0 for success).
7572 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7575 if (c
->display_usage
) {
7577 "net rpc printer driver\n"
7580 _("List printer-drivers on a remote RPC server"));
7584 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7585 rpc_printer_driver_list_internals
,
7590 * Publish printer in ADS via MSRPC.
7592 * @param c A net_context structure.
7593 * @param argc Standard main() style argc.
7594 * @param argv Standard main() style argv. Initial components are already
7597 * @return A shell status integer (0 for success).
7599 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7602 if (c
->display_usage
) {
7604 "net rpc printer publish publish\n"
7607 _("Publish printer in ADS via MSRPC"));
7611 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7612 rpc_printer_publish_publish_internals
,
7617 * Update printer in ADS via MSRPC.
7619 * @param c A net_context structure.
7620 * @param argc Standard main() style argc.
7621 * @param argv Standard main() style argv. Initial components are already
7624 * @return A shell status integer (0 for success).
7626 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7628 if (c
->display_usage
) {
7630 "net rpc printer publish update\n"
7633 _("Update printer in ADS via MSRPC"));
7637 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7638 rpc_printer_publish_update_internals
,
7643 * UnPublish printer in ADS via MSRPC.
7645 * @param c A net_context structure.
7646 * @param argc Standard main() style argc.
7647 * @param argv Standard main() style argv. Initial components are already
7650 * @return A shell status integer (0 for success).
7652 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7655 if (c
->display_usage
) {
7657 "net rpc printer publish unpublish\n"
7660 _("UnPublish printer in ADS via MSRPC"));
7664 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7665 rpc_printer_publish_unpublish_internals
,
7670 * List published printers via MSRPC.
7672 * @param c A net_context structure.
7673 * @param argc Standard main() style argc.
7674 * @param argv Standard main() style argv. Initial components are already
7677 * @return A shell status integer (0 for success).
7679 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7682 if (c
->display_usage
) {
7684 "net rpc printer publish list\n"
7687 _("List published printers via MSRPC"));
7691 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7692 rpc_printer_publish_list_internals
,
7698 * Publish printer in ADS.
7700 * @param c A net_context structure.
7701 * @param argc Standard main() style argc.
7702 * @param argv Standard main() style argv. Initial components are already
7705 * @return A shell status integer (0 for success).
7707 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7711 struct functable func
[] = {
7714 rpc_printer_publish_publish
,
7716 N_("Publish printer in AD"),
7717 N_("net rpc printer publish publish\n"
7718 " Publish printer in AD")
7722 rpc_printer_publish_update
,
7724 N_("Update printer in AD"),
7725 N_("net rpc printer publish update\n"
7726 " Update printer in AD")
7730 rpc_printer_publish_unpublish
,
7732 N_("Unpublish printer"),
7733 N_("net rpc printer publish unpublish\n"
7734 " Unpublish printer")
7738 rpc_printer_publish_list
,
7740 N_("List published printers"),
7741 N_("net rpc printer publish list\n"
7742 " List published printers")
7744 {NULL
, NULL
, 0, NULL
, NULL
}
7748 if (c
->display_usage
) {
7749 d_printf(_("Usage:\n"));
7750 d_printf(_("net rpc printer publish\n"
7751 " List published printers\n"
7752 " Alias of net rpc printer publish "
7754 net_display_usage_from_functable(func
);
7757 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7758 rpc_printer_publish_list_internals
,
7762 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7768 * Display rpc printer help page.
7770 * @param c A net_context structure.
7771 * @param argc Standard main() style argc.
7772 * @param argv Standard main() style argv. Initial components are already
7775 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7777 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7778 "\tlists all printers on print-server\n\n"));
7779 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7780 "\tlists all printer-drivers on print-server\n\n"));
7781 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7782 "\tpublishes printer settings in Active Directory\n"
7783 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7784 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7785 "\n\tmigrates printers from remote to local server\n\n"));
7786 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7787 "\n\tmigrates printer-settings from remote to local server\n\n"));
7788 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7789 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7790 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7791 "\n\tmigrates printer-forms from remote to local server\n\n"));
7792 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7793 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7794 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7795 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7796 "\tremote to local print-server\n\n"));
7797 net_common_methods_usage(c
, argc
, argv
);
7798 net_common_flags_usage(c
, argc
, argv
);
7800 "\t-v or --verbose\t\t\tgive verbose output\n"
7801 "\t --destination\t\tmigration target server (default: localhost)\n"));
7807 * 'net rpc printer' entrypoint.
7809 * @param c A net_context structure.
7810 * @param argc Standard main() style argc.
7811 * @param argv Standard main() style argv. Initial components are already
7814 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7816 struct functable func
[] = {
7821 N_("List all printers on print server"),
7822 N_("net rpc printer list\n"
7823 " List all printers on print server")
7827 rpc_printer_migrate
,
7829 N_("Migrate printer to local server"),
7830 N_("net rpc printer migrate\n"
7831 " Migrate printer to local server")
7835 rpc_printer_driver_list
,
7837 N_("List printer drivers"),
7838 N_("net rpc printer driver\n"
7839 " List printer drivers")
7843 rpc_printer_publish
,
7845 N_("Publish printer in AD"),
7846 N_("net rpc printer publish\n"
7847 " Publish printer in AD")
7849 {NULL
, NULL
, 0, NULL
, NULL
}
7853 if (c
->display_usage
) {
7854 d_printf(_("Usage:\n"));
7855 d_printf(_("net rpc printer\n"
7856 " List printers\n"));
7857 net_display_usage_from_functable(func
);
7860 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7861 rpc_printer_list_internals
,
7865 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7869 * 'net rpc' entrypoint.
7871 * @param c A net_context structure.
7872 * @param argc Standard main() style argc.
7873 * @param argv Standard main() style argv. Initial components are already
7877 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7879 NET_API_STATUS status
;
7881 struct functable func
[] = {
7886 N_("Modify global audit settings"),
7887 N_("net rpc audit\n"
7888 " Modify global audit settings")
7894 N_("Show basic info about a domain"),
7896 " Show basic info about a domain")
7902 N_("Join a domain"),
7910 N_("Join a domain created in server manager"),
7911 N_("net rpc oldjoin\n"
7912 " Join a domain created in server manager")
7918 N_("Test that a join is valid"),
7919 N_("net rpc testjoin\n"
7920 " Test that a join is valid")
7926 N_("List/modify users"),
7928 " List/modify users")
7934 N_("Change a user password"),
7935 N_("net rpc password\n"
7936 " Change a user password\n"
7937 " Alias for net rpc user password")
7943 N_("List/modify groups"),
7944 N_("net rpc group\n"
7945 " List/modify groups")
7951 N_("List/modify shares"),
7952 N_("net rpc share\n"
7953 " List/modify shares")
7959 N_("List open files"),
7967 N_("List/modify printers"),
7968 N_("net rpc printer\n"
7969 " List/modify printers")
7973 net_rpc_changetrustpw
,
7975 N_("Change trust account password"),
7976 N_("net rpc changetrustpw\n"
7977 " Change trust account password")
7983 N_("Modify domain trusts"),
7984 N_("net rpc trustdom\n"
7985 " Modify domain trusts")
7991 N_("Abort a remote shutdown"),
7992 N_("net rpc abortshutdown\n"
7993 " Abort a remote shutdown")
7999 N_("Shutdown a remote server"),
8000 N_("net rpc shutdown\n"
8001 " Shutdown a remote server")
8007 N_("Dump SAM data of remote NT PDC"),
8008 N_("net rpc samdump\n"
8009 " Dump SAM data of remote NT PDC")
8015 N_("Sync a remote NT PDC's data into local passdb"),
8016 N_("net rpc vampire\n"
8017 " Sync a remote NT PDC's data into local passdb")
8023 N_("Fetch the domain sid into local secrets.tdb"),
8024 N_("net rpc getsid\n"
8025 " Fetch the domain sid into local secrets.tdb")
8031 N_("Manage privileges assigned to SID"),
8032 N_("net rpc rights\n"
8033 " Manage privileges assigned to SID")
8039 N_("Start/stop/query remote services"),
8040 N_("net rpc service\n"
8041 " Start/stop/query remote services")
8047 N_("Manage registry hives"),
8048 N_("net rpc registry\n"
8049 " Manage registry hives")
8055 N_("Open interactive shell on remote server"),
8056 N_("net rpc shell\n"
8057 " Open interactive shell on remote server")
8063 N_("Manage trusts"),
8064 N_("net rpc trust\n"
8071 N_("Configure a remote samba server"),
8073 " Configure a remote samba server")
8075 {NULL
, NULL
, 0, NULL
, NULL
}
8078 status
= libnetapi_net_init(&c
->netapi_ctx
);
8082 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
8083 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
8084 if (c
->opt_kerberos
) {
8085 libnetapi_set_use_kerberos(c
->netapi_ctx
);
8087 if (c
->opt_ccache
) {
8088 libnetapi_set_use_ccache(c
->netapi_ctx
);
8091 return net_run_function(c
, argc
, argv
, "net rpc", func
);