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
];
4686 wbc_status
= wbcGidToSid(gid
, &wsid
);
4687 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4688 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4689 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4690 wbcFreeMemory(groups
);
4694 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4696 DEBUG(3, (" %s\n", sid_str
));
4698 string_to_sid(&sid
, sid_str
);
4699 add_sid_to_token(token
, &sid
);
4701 wbcFreeMemory(groups
);
4707 * Get a list of all user tokens we want to look at
4710 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4711 struct user_token
**user_tokens
)
4713 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4714 uint32_t i
, num_users
;
4716 struct user_token
*result
;
4717 TALLOC_CTX
*frame
= NULL
;
4719 if (lp_winbind_use_default_domain() &&
4720 (c
->opt_target_workgroup
== NULL
)) {
4721 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4722 "please specify a workgroup\n"));
4726 /* Send request to winbind daemon */
4728 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4729 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4730 DEBUG(1, (_("winbind could not list users: %s\n"),
4731 wbcErrorString(wbc_status
)));
4735 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4737 if (result
== NULL
) {
4738 DEBUG(1, ("Could not malloc sid array\n"));
4739 wbcFreeMemory(users
);
4743 frame
= talloc_stackframe();
4744 for (i
=0; i
< num_users
; i
++) {
4745 fstring domain
, user
;
4748 fstrcpy(result
[i
].name
, users
[i
]);
4750 p
= strchr(users
[i
], *lp_winbind_separator());
4752 DEBUG(3, ("%s\n", users
[i
]));
4755 fstrcpy(domain
, c
->opt_target_workgroup
);
4756 fstrcpy(user
, users
[i
]);
4759 fstrcpy(domain
, users
[i
]);
4760 if (!strupper_m(domain
)) {
4761 DEBUG(1, ("strupper_m %s failed\n", domain
));
4762 wbcFreeMemory(users
);
4768 get_user_sids(domain
, user
, &(result
[i
].token
));
4771 wbcFreeMemory(users
);
4773 *num_tokens
= num_users
;
4774 *user_tokens
= result
;
4779 static bool get_user_tokens_from_file(FILE *f
,
4781 struct user_token
**tokens
)
4783 struct user_token
*token
= NULL
;
4788 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4792 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4793 line
[strlen(line
)-1] = '\0';
4796 if (line
[0] == ' ') {
4800 if(!string_to_sid(&sid
, &line
[1])) {
4801 DEBUG(1,("get_user_tokens_from_file: Could "
4802 "not convert sid %s \n",&line
[1]));
4806 if (token
== NULL
) {
4807 DEBUG(0, ("File does not begin with username"));
4811 add_sid_to_token(&token
->token
, &sid
);
4815 /* And a new user... */
4818 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4819 if (*tokens
== NULL
) {
4820 DEBUG(0, ("Could not realloc tokens\n"));
4824 token
= &((*tokens
)[*num_tokens
-1]);
4826 if (strlcpy(token
->name
, line
, sizeof(token
->name
)) >= sizeof(token
->name
)) {
4829 token
->token
.num_sids
= 0;
4830 token
->token
.sids
= NULL
;
4839 * Show the list of all users that have access to a share
4842 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4843 TALLOC_CTX
*mem_ctx
,
4844 const char *netname
,
4846 struct user_token
*tokens
)
4849 struct security_descriptor
*share_sd
= NULL
;
4850 struct security_descriptor
*root_sd
= NULL
;
4851 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4853 union srvsvc_NetShareInfo info
;
4857 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4859 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
4866 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4867 DEBUG(1, ("Coult not query secdesc for share %s\n",
4872 share_sd
= info
.info502
->sd_buf
.sd
;
4873 if (share_sd
== NULL
) {
4874 DEBUG(1, ("Got no secdesc for share %s\n",
4878 cnum
= cli_state_get_tid(cli
);
4880 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, netname
, "A:", "", 0))) {
4884 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4885 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4886 cli_query_secdesc(cli
, fnum
, mem_ctx
, &root_sd
);
4889 for (i
=0; i
<num_tokens
; i
++) {
4892 if (share_sd
!= NULL
) {
4893 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4896 if (!NT_STATUS_IS_OK(status
)) {
4897 DEBUG(1, ("Could not check share_sd for "
4904 if (root_sd
== NULL
) {
4905 d_printf(" %s\n", tokens
[i
].name
);
4909 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4911 if (!NT_STATUS_IS_OK(status
)) {
4912 DEBUG(1, ("Could not check root_sd for user %s\n",
4916 d_printf(" %s\n", tokens
[i
].name
);
4919 if (fnum
!= (uint16_t)-1)
4920 cli_close(cli
, fnum
);
4922 cli_state_set_tid(cli
, cnum
);
4928 * List shares on a remote RPC server, including the security descriptors.
4930 * All parameters are provided by the run_rpc_command function, except for
4931 * argc, argv which are passed through.
4933 * @param domain_sid The domain sid acquired from the remote server.
4934 * @param cli A cli_state connected to the server.
4935 * @param mem_ctx Talloc context, destroyed on completion of the function.
4936 * @param argc Standard main() style argc.
4937 * @param argv Standard main() style argv. Initial components are already
4940 * @return Normal NTSTATUS return.
4943 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4944 const struct dom_sid
*domain_sid
,
4945 const char *domain_name
,
4946 struct cli_state
*cli
,
4947 struct rpc_pipe_client
*pipe_hnd
,
4948 TALLOC_CTX
*mem_ctx
,
4954 NTSTATUS nt_status
= NT_STATUS_OK
;
4955 uint32_t total_entries
= 0;
4956 uint32_t resume_handle
= 0;
4957 uint32_t preferred_len
= 0xffffffff;
4959 struct dcerpc_binding_handle
*b
= NULL
;
4960 struct srvsvc_NetShareInfoCtr info_ctr
;
4961 struct srvsvc_NetShareCtr1 ctr1
;
4964 struct user_token
*tokens
= NULL
;
4970 f
= fopen(argv
[0], "r");
4974 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4975 return NT_STATUS_UNSUCCESSFUL
;
4978 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4984 DEBUG(0, ("Could not read users from file\n"));
4985 return NT_STATUS_UNSUCCESSFUL
;
4988 for (i
=0; i
<num_tokens
; i
++)
4989 collect_alias_memberships(&tokens
[i
].token
);
4991 ZERO_STRUCT(info_ctr
);
4995 info_ctr
.ctr
.ctr1
= &ctr1
;
4997 b
= pipe_hnd
->binding_handle
;
4999 /* Issue the NetShareEnum RPC call and retrieve the response */
5000 nt_status
= dcerpc_srvsvc_NetShareEnumAll(b
,
5009 /* Was it successful? */
5010 if (!NT_STATUS_IS_OK(nt_status
)) {
5011 /* Nope. Go clean up. */
5015 if (!W_ERROR_IS_OK(result
)) {
5016 /* Nope. Go clean up. */
5017 nt_status
= werror_to_ntstatus(result
);
5021 if (total_entries
== 0) {
5025 /* For each returned entry... */
5026 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
5027 const char *netname
= info_ctr
.ctr
.ctr1
->array
[i
].name
;
5029 if (info_ctr
.ctr
.ctr1
->array
[i
].type
!= STYPE_DISKTREE
) {
5033 d_printf("%s\n", netname
);
5035 show_userlist(pipe_hnd
, mem_ctx
, netname
,
5036 num_tokens
, tokens
);
5039 for (i
=0; i
<num_tokens
; i
++) {
5040 free_user_token(&tokens
[i
].token
);
5047 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
5052 if (c
->display_usage
) {
5054 "net rpc share allowedusers\n"
5057 _("List allowed users"));
5061 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5062 rpc_aliaslist_internals
,
5067 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
, 0,
5073 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
5074 rpc_share_allowedusers_internals
,
5078 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
5081 struct user_token
*tokens
= NULL
;
5085 net_usersidlist_usage(c
, argc
, argv
);
5089 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
5090 DEBUG(0, ("Could not get the user/sid list\n"));
5094 for (i
=0; i
<num_tokens
; i
++) {
5095 dump_user_token(&tokens
[i
]);
5096 free_user_token(&tokens
[i
].token
);
5103 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
5105 d_printf(_("net usersidlist\n"
5106 "\tprints out a list of all users the running winbind knows\n"
5107 "\tabout, together with all their SIDs. This is used as\n"
5108 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5110 net_common_flags_usage(c
, argc
, argv
);
5115 * 'net rpc share' entrypoint.
5116 * @param argc Standard main() style argc.
5117 * @param argv Standard main() style argv. Initial components are already
5121 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
5123 NET_API_STATUS status
;
5125 struct functable func
[] = {
5131 N_("net rpc share add\n"
5139 N_("net rpc share delete\n"
5144 rpc_share_allowedusers
,
5146 N_("Modify allowed users"),
5147 N_("net rpc share allowedusers\n"
5148 " Modify allowed users")
5154 N_("Migrate share to local server"),
5155 N_("net rpc share migrate\n"
5156 " Migrate share to local server")
5163 N_("net rpc share list\n"
5166 {NULL
, NULL
, 0, NULL
, NULL
}
5169 status
= libnetapi_net_init(&c
->netapi_ctx
);
5173 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5174 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5175 if (c
->opt_kerberos
) {
5176 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5180 if (c
->display_usage
) {
5185 " Alias for net rpc share list\n"));
5186 net_display_usage_from_functable(func
);
5190 return rpc_share_list(c
, argc
, argv
);
5193 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5196 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5197 TALLOC_CTX
*mem_ctx
,
5198 struct rpc_sh_ctx
*ctx
,
5199 struct rpc_pipe_client
*pipe_hnd
,
5200 int argc
, const char **argv
)
5203 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
5206 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
5207 TALLOC_CTX
*mem_ctx
,
5208 struct rpc_sh_ctx
*ctx
,
5209 struct rpc_pipe_client
*pipe_hnd
,
5210 int argc
, const char **argv
)
5212 NET_API_STATUS status
;
5213 uint32_t parm_err
= 0;
5214 struct SHARE_INFO_2 i2
;
5216 if ((argc
< 2) || (argc
> 3)) {
5217 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
5219 return NT_STATUS_INVALID_PARAMETER
;
5222 i2
.shi2_netname
= argv
[0];
5223 i2
.shi2_type
= STYPE_DISKTREE
;
5224 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
5225 i2
.shi2_permissions
= 0;
5226 i2
.shi2_max_uses
= 0;
5227 i2
.shi2_current_uses
= 0;
5228 i2
.shi2_path
= argv
[1];
5229 i2
.shi2_passwd
= NULL
;
5231 status
= NetShareAdd(pipe_hnd
->desthost
,
5236 return werror_to_ntstatus(W_ERROR(status
));
5239 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5240 TALLOC_CTX
*mem_ctx
,
5241 struct rpc_sh_ctx
*ctx
,
5242 struct rpc_pipe_client
*pipe_hnd
,
5243 int argc
, const char **argv
)
5246 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5247 return NT_STATUS_INVALID_PARAMETER
;
5250 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
5253 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5254 TALLOC_CTX
*mem_ctx
,
5255 struct rpc_sh_ctx
*ctx
,
5256 struct rpc_pipe_client
*pipe_hnd
,
5257 int argc
, const char **argv
)
5259 union srvsvc_NetShareInfo info
;
5262 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5265 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5266 return NT_STATUS_INVALID_PARAMETER
;
5269 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
5275 if (!NT_STATUS_IS_OK(status
)) {
5276 result
= ntstatus_to_werror(status
);
5279 if (!W_ERROR_IS_OK(result
)) {
5283 d_printf(_("Name: %s\n"), info
.info2
->name
);
5284 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
5285 d_printf(_("Path: %s\n"), info
.info2
->path
);
5286 d_printf(_("Password: %s\n"), info
.info2
->password
);
5289 return werror_to_ntstatus(result
);
5292 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5293 struct rpc_sh_ctx
*ctx
)
5295 static struct rpc_sh_cmd cmds
[] = {
5297 { "list", NULL
, &ndr_table_srvsvc
, rpc_sh_share_list
,
5298 N_("List available shares") },
5300 { "add", NULL
, &ndr_table_srvsvc
, rpc_sh_share_add
,
5301 N_("Add a share") },
5303 { "delete", NULL
, &ndr_table_srvsvc
, rpc_sh_share_delete
,
5304 N_("Delete a share") },
5306 { "info", NULL
, &ndr_table_srvsvc
, rpc_sh_share_info
,
5307 N_("Get information about a share") },
5309 { NULL
, NULL
, 0, NULL
, NULL
}
5315 /****************************************************************************/
5317 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5319 return net_file_usage(c
, argc
, argv
);
5323 * Close a file on a remote RPC server.
5325 * @param argc Standard main() style argc.
5326 * @param argv Standard main() style argv. Initial components are already
5329 * @return A shell status integer (0 for success).
5331 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5333 if (argc
< 1 || c
->display_usage
) {
5334 return rpc_file_usage(c
, argc
, argv
);
5337 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
5341 * Formatted print of open file info
5343 * @param r struct FILE_INFO_3 contents
5346 static void display_file_info_3(struct FILE_INFO_3
*r
)
5348 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5349 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
5350 r
->fi3_num_locks
, r
->fi3_pathname
);
5354 * List files for a user on a remote RPC server.
5356 * @param argc Standard main() style argc.
5357 * @param argv Standard main() style argv. Initial components are already
5360 * @return A shell status integer (0 for success)..
5363 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5365 NET_API_STATUS status
;
5366 uint32 preferred_len
= 0xffffffff, i
;
5367 const char *username
=NULL
;
5368 uint32_t total_entries
= 0;
5369 uint32_t entries_read
= 0;
5370 uint32_t resume_handle
= 0;
5371 struct FILE_INFO_3
*i3
= NULL
;
5373 if (c
->display_usage
) {
5374 return rpc_file_usage(c
, argc
, argv
);
5377 /* if argc > 0, must be user command */
5379 username
= smb_xstrdup(argv
[0]);
5382 status
= NetFileEnum(c
->opt_host
,
5386 (uint8_t **)(void *)&i3
,
5396 /* Display results */
5399 "\nEnumerating open files on remote server:\n\n"
5400 "\nFileId Opened by Perms Locks Path"
5401 "\n------ --------- ----- ----- ---- \n"));
5402 for (i
= 0; i
< entries_read
; i
++) {
5403 display_file_info_3(&i3
[i
]);
5410 * 'net rpc file' entrypoint.
5411 * @param argc Standard main() style argc.
5412 * @param argv Standard main() style argv. Initial components are already
5416 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5418 NET_API_STATUS status
;
5420 struct functable func
[] = {
5425 N_("Close opened file"),
5426 N_("net rpc file close\n"
5427 " Close opened file")
5433 N_("List files opened by user"),
5434 N_("net rpc file user\n"
5435 " List files opened by user")
5442 N_("Display information about opened file"),
5443 N_("net rpc file info\n"
5444 " Display information about opened file")
5447 {NULL
, NULL
, 0, NULL
, NULL
}
5450 status
= libnetapi_net_init(&c
->netapi_ctx
);
5454 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5455 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5456 if (c
->opt_kerberos
) {
5457 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5461 if (c
->display_usage
) {
5462 d_printf(_("Usage:\n"));
5463 d_printf(_("net rpc file\n"
5464 " List opened files\n"));
5465 net_display_usage_from_functable(func
);
5469 return rpc_file_user(c
, argc
, argv
);
5472 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5476 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5478 * All parameters are provided by the run_rpc_command function, except for
5479 * argc, argv which are passed through.
5481 * @param c A net_context structure.
5482 * @param domain_sid The domain sid acquired from the remote server.
5483 * @param cli A cli_state connected to the server.
5484 * @param mem_ctx Talloc context, destroyed on completion of the function.
5485 * @param argc Standard main() style argc.
5486 * @param argv Standard main() style argv. Initial components are already
5489 * @return Normal NTSTATUS return.
5492 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5493 const struct dom_sid
*domain_sid
,
5494 const char *domain_name
,
5495 struct cli_state
*cli
,
5496 struct rpc_pipe_client
*pipe_hnd
,
5497 TALLOC_CTX
*mem_ctx
,
5501 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5503 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5505 status
= dcerpc_initshutdown_Abort(b
, mem_ctx
, NULL
, &result
);
5506 if (!NT_STATUS_IS_OK(status
)) {
5509 if (W_ERROR_IS_OK(result
)) {
5510 d_printf(_("\nShutdown successfully aborted\n"));
5511 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5513 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5515 return werror_to_ntstatus(result
);
5519 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5521 * All parameters are provided by the run_rpc_command function, except for
5522 * argc, argv which are passed through.
5524 * @param c A net_context structure.
5525 * @param domain_sid The domain sid acquired from the remote server.
5526 * @param cli A cli_state connected to the server.
5527 * @param mem_ctx Talloc context, destroyed on completion of the function.
5528 * @param argc Standard main() style argc.
5529 * @param argv Standard main() style argv. Initial components are already
5532 * @return Normal NTSTATUS return.
5535 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5536 const struct dom_sid
*domain_sid
,
5537 const char *domain_name
,
5538 struct cli_state
*cli
,
5539 struct rpc_pipe_client
*pipe_hnd
,
5540 TALLOC_CTX
*mem_ctx
,
5544 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5546 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5548 result
= dcerpc_winreg_AbortSystemShutdown(b
, mem_ctx
, NULL
, &werr
);
5550 if (!NT_STATUS_IS_OK(result
)) {
5551 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5554 if (W_ERROR_IS_OK(werr
)) {
5555 d_printf(_("\nShutdown successfully aborted\n"));
5556 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5558 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5560 return werror_to_ntstatus(werr
);
5564 * ABORT the shutdown of a remote RPC server.
5566 * @param argc Standard main() style argc.
5567 * @param argv Standard main() style argv. Initial components are already
5570 * @return A shell status integer (0 for success).
5573 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5578 if (c
->display_usage
) {
5580 "net rpc abortshutdown\n"
5583 _("Abort a scheduled shutdown"));
5587 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5588 rpc_shutdown_abort_internals
, argc
, argv
);
5593 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5595 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5596 rpc_reg_shutdown_abort_internals
,
5601 * Shut down a remote RPC Server via initshutdown pipe.
5603 * All parameters are provided by the run_rpc_command function, except for
5604 * argc, argv which are passed through.
5606 * @param c A net_context structure.
5607 * @param domain_sid The domain sid acquired from the remote server.
5608 * @param cli A cli_state connected to the server.
5609 * @param mem_ctx Talloc context, destroyed on completion of the function.
5610 * @param argc Standard main() style argc.
5611 * @param argv Standard main() style argv. Initial components are already
5614 * @return Normal NTSTATUS return.
5617 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5618 const struct dom_sid
*domain_sid
,
5619 const char *domain_name
,
5620 struct cli_state
*cli
,
5621 struct rpc_pipe_client
*pipe_hnd
,
5622 TALLOC_CTX
*mem_ctx
,
5626 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5628 const char *msg
= N_("This machine will be shutdown shortly");
5629 uint32 timeout
= 20;
5630 struct lsa_StringLarge msg_string
;
5631 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5633 if (c
->opt_comment
) {
5634 msg
= c
->opt_comment
;
5636 if (c
->opt_timeout
) {
5637 timeout
= c
->opt_timeout
;
5640 msg_string
.string
= msg
;
5642 /* create an entry */
5643 status
= dcerpc_initshutdown_Init(b
, mem_ctx
, NULL
,
5644 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5646 if (!NT_STATUS_IS_OK(status
)) {
5649 if (W_ERROR_IS_OK(result
)) {
5650 d_printf(_("\nShutdown of remote machine succeeded\n"));
5651 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5653 DEBUG(1,("Shutdown of remote machine failed!\n"));
5655 return werror_to_ntstatus(result
);
5659 * Shut down a remote RPC Server via winreg pipe.
5661 * All parameters are provided by the run_rpc_command function, except for
5662 * argc, argv which are passed through.
5664 * @param c A net_context structure.
5665 * @param domain_sid The domain sid acquired from the remote server.
5666 * @param cli A cli_state connected to the server.
5667 * @param mem_ctx Talloc context, destroyed on completion of the function.
5668 * @param argc Standard main() style argc.
5669 * @param argv Standard main() style argv. Initial components are already
5672 * @return Normal NTSTATUS return.
5675 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5676 const struct dom_sid
*domain_sid
,
5677 const char *domain_name
,
5678 struct cli_state
*cli
,
5679 struct rpc_pipe_client
*pipe_hnd
,
5680 TALLOC_CTX
*mem_ctx
,
5684 const char *msg
= N_("This machine will be shutdown shortly");
5685 uint32 timeout
= 20;
5686 struct lsa_StringLarge msg_string
;
5689 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5691 if (c
->opt_comment
) {
5692 msg
= c
->opt_comment
;
5694 msg_string
.string
= msg
;
5696 if (c
->opt_timeout
) {
5697 timeout
= c
->opt_timeout
;
5700 /* create an entry */
5701 result
= dcerpc_winreg_InitiateSystemShutdown(b
, mem_ctx
, NULL
,
5702 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5704 if (!NT_STATUS_IS_OK(result
)) {
5705 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5709 if (W_ERROR_IS_OK(werr
)) {
5710 d_printf(_("\nShutdown of remote machine succeeded\n"));
5712 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5713 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5714 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5716 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5719 return werror_to_ntstatus(werr
);
5723 * Shut down a remote RPC server.
5725 * @param argc Standard main() style argc.
5726 * @param argv Standard main() style argv. Initial components are already
5729 * @return A shell status integer (0 for success).
5732 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5736 if (c
->display_usage
) {
5738 "net rpc shutdown\n"
5741 _("Shut down a remote RPC server"));
5745 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5746 rpc_init_shutdown_internals
, argc
, argv
);
5749 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5750 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5751 rpc_reg_shutdown_internals
, argc
, argv
);
5757 /***************************************************************************
5758 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5759 ***************************************************************************/
5762 * Add interdomain trust account to the RPC server.
5763 * All parameters (except for argc and argv) are passed by run_rpc_command
5766 * @param c A net_context structure.
5767 * @param domain_sid The domain sid acquired from the server.
5768 * @param cli A cli_state connected to the server.
5769 * @param mem_ctx Talloc context, destroyed on completion of the function.
5770 * @param argc Standard main() style argc.
5771 * @param argv Standard main() style argv. Initial components are already
5774 * @return normal NTSTATUS return code.
5777 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5778 const struct dom_sid
*domain_sid
,
5779 const char *domain_name
,
5780 struct cli_state
*cli
,
5781 struct rpc_pipe_client
*pipe_hnd
,
5782 TALLOC_CTX
*mem_ctx
,
5786 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5787 NTSTATUS status
, result
;
5789 struct lsa_String lsa_acct_name
;
5791 uint32 acct_flags
=0;
5793 uint32_t access_granted
= 0;
5794 union samr_UserInfo info
;
5795 unsigned int orig_timeout
;
5796 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5797 DATA_BLOB session_key
= data_blob_null
;
5802 _(" net rpc trustdom add <domain_name> "
5803 "<trust password>\n"));
5804 return NT_STATUS_INVALID_PARAMETER
;
5808 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5811 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5812 return NT_STATUS_NO_MEMORY
;
5815 if (!strupper_m(acct_name
)) {
5816 SAFE_FREE(acct_name
);
5817 return NT_STATUS_INVALID_PARAMETER
;
5820 init_lsa_String(&lsa_acct_name
, acct_name
);
5822 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
5823 if (!NT_STATUS_IS_OK(status
)) {
5824 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
5825 nt_errstr(status
)));
5829 /* Get samr policy handle */
5830 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5832 MAXIMUM_ALLOWED_ACCESS
,
5835 if (!NT_STATUS_IS_OK(status
)) {
5838 if (!NT_STATUS_IS_OK(result
)) {
5843 /* Get domain policy handle */
5844 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5846 MAXIMUM_ALLOWED_ACCESS
,
5847 discard_const_p(struct dom_sid2
, domain_sid
),
5850 if (!NT_STATUS_IS_OK(status
)) {
5853 if (!NT_STATUS_IS_OK(result
)) {
5858 /* This call can take a long time - allow the server to time out.
5859 * 35 seconds should do it. */
5861 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5863 /* Create trusting domain's account */
5864 acb_info
= ACB_NORMAL
;
5865 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5866 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5867 SAMR_USER_ACCESS_SET_PASSWORD
|
5868 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5869 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5871 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
5880 if (!NT_STATUS_IS_OK(status
)) {
5883 /* And restore our original timeout. */
5884 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5886 if (!NT_STATUS_IS_OK(result
)) {
5888 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5889 acct_name
, nt_errstr(result
));
5894 struct samr_CryptPassword crypt_pwd
;
5896 ZERO_STRUCT(info
.info23
);
5898 init_samr_CryptPassword(argv
[1],
5902 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5903 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5904 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5905 info
.info23
.password
= crypt_pwd
;
5907 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
5912 if (!NT_STATUS_IS_OK(status
)) {
5916 if (!NT_STATUS_IS_OK(result
)) {
5918 DEBUG(0,("Could not set trust account password: %s\n",
5919 nt_errstr(result
)));
5925 SAFE_FREE(acct_name
);
5926 data_blob_clear_free(&session_key
);
5931 * Create interdomain trust account for a remote domain.
5933 * @param argc Standard argc.
5934 * @param argv Standard argv without initial components.
5936 * @return Integer status (0 means success).
5939 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5941 if (argc
> 0 && !c
->display_usage
) {
5942 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5943 rpc_trustdom_add_internals
, argc
, argv
);
5947 _("net rpc trustdom add <domain_name> <trust "
5955 * Remove interdomain trust account from the RPC server.
5956 * All parameters (except for argc and argv) are passed by run_rpc_command
5959 * @param c A net_context structure.
5960 * @param domain_sid The domain sid acquired from the server.
5961 * @param cli A cli_state connected to the server.
5962 * @param mem_ctx Talloc context, destroyed on completion of the function.
5963 * @param argc Standard main() style argc.
5964 * @param argv Standard main() style argv. Initial components are already
5967 * @return normal NTSTATUS return code.
5970 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5971 const struct dom_sid
*domain_sid
,
5972 const char *domain_name
,
5973 struct cli_state
*cli
,
5974 struct rpc_pipe_client
*pipe_hnd
,
5975 TALLOC_CTX
*mem_ctx
,
5979 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5980 NTSTATUS status
, result
;
5982 struct dom_sid trust_acct_sid
;
5983 struct samr_Ids user_rids
, name_types
;
5984 struct lsa_String lsa_acct_name
;
5985 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5990 _(" net rpc trustdom del <domain_name>\n"));
5991 return NT_STATUS_INVALID_PARAMETER
;
5995 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5997 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5999 if (acct_name
== NULL
)
6000 return NT_STATUS_NO_MEMORY
;
6002 if (!strupper_m(acct_name
)) {
6003 TALLOC_FREE(acct_name
);
6004 return NT_STATUS_INVALID_PARAMETER
;
6007 /* Get samr policy handle */
6008 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6010 MAXIMUM_ALLOWED_ACCESS
,
6013 if (!NT_STATUS_IS_OK(status
)) {
6016 if (!NT_STATUS_IS_OK(result
)) {
6021 /* Get domain policy handle */
6022 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6024 MAXIMUM_ALLOWED_ACCESS
,
6025 discard_const_p(struct dom_sid2
, domain_sid
),
6028 if (!NT_STATUS_IS_OK(status
)) {
6031 if (!NT_STATUS_IS_OK(result
)) {
6036 init_lsa_String(&lsa_acct_name
, acct_name
);
6038 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
6045 if (!NT_STATUS_IS_OK(status
)) {
6046 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6048 acct_name
, nt_errstr(status
));
6051 if (!NT_STATUS_IS_OK(result
)) {
6053 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6055 acct_name
, nt_errstr(result
) );
6059 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
6061 MAXIMUM_ALLOWED_ACCESS
,
6065 if (!NT_STATUS_IS_OK(status
)) {
6066 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6068 acct_name
, nt_errstr(status
) );
6072 if (!NT_STATUS_IS_OK(result
)) {
6074 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6076 acct_name
, nt_errstr(result
) );
6080 /* append the rid to the domain sid */
6081 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
6085 /* remove the sid */
6087 status
= dcerpc_samr_RemoveMemberFromForeignDomain(b
, mem_ctx
,
6091 if (!NT_STATUS_IS_OK(status
)) {
6092 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6093 " on user %s failed %s\n"),
6094 acct_name
, nt_errstr(status
));
6097 if (!NT_STATUS_IS_OK(result
)) {
6099 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6100 " on user %s failed %s\n"),
6101 acct_name
, nt_errstr(result
) );
6108 status
= dcerpc_samr_DeleteUser(b
, mem_ctx
,
6111 if (!NT_STATUS_IS_OK(status
)) {
6112 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6114 acct_name
, nt_errstr(status
));
6118 if (!NT_STATUS_IS_OK(result
)) {
6120 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6122 acct_name
, nt_errstr(result
) );
6126 if (!NT_STATUS_IS_OK(result
)) {
6127 d_printf(_("Could not set trust account password: %s\n"),
6137 * Delete interdomain trust account for a remote domain.
6139 * @param argc Standard argc.
6140 * @param argv Standard argv without initial components.
6142 * @return Integer status (0 means success).
6145 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
6147 if (argc
> 0 && !c
->display_usage
) {
6148 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
6149 rpc_trustdom_del_internals
, argc
, argv
);
6153 _("net rpc trustdom del <domain>\n"));
6158 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
6159 struct cli_state
*cli
,
6160 TALLOC_CTX
*mem_ctx
,
6161 const char *domain_name
)
6163 char *dc_name
= NULL
;
6164 const char *buffer
= NULL
;
6165 struct rpc_pipe_client
*netr
;
6168 struct dcerpc_binding_handle
*b
;
6170 /* Use NetServerEnum2 */
6172 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
6174 return NT_STATUS_OK
;
6177 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6178 for domain %s\n", domain_name
));
6180 /* Try netr_GetDcName */
6182 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
6184 if (!NT_STATUS_IS_OK(status
)) {
6188 b
= netr
->binding_handle
;
6190 status
= dcerpc_netr_GetDcName(b
, mem_ctx
,
6197 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(result
)) {
6201 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6202 for domain %s\n", domain_name
));
6204 if (!NT_STATUS_IS_OK(status
)) {
6208 return werror_to_ntstatus(result
);
6212 * Establish trust relationship to a trusting domain.
6213 * Interdomain account must already be created on remote PDC.
6215 * @param c A net_context structure.
6216 * @param argc Standard argc.
6217 * @param argv Standard argv without initial components.
6219 * @return Integer status (0 means success).
6222 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6225 struct cli_state
*cli
= NULL
;
6226 struct sockaddr_storage server_ss
;
6227 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6228 struct policy_handle connect_hnd
;
6229 TALLOC_CTX
*mem_ctx
;
6230 NTSTATUS nt_status
, result
;
6231 struct dom_sid
*domain_sid
;
6236 union lsa_PolicyInformation
*info
= NULL
;
6237 struct dcerpc_binding_handle
*b
;
6240 * Connect to \\server\ipc$ as 'our domain' account with password
6243 if (argc
!= 1 || c
->display_usage
) {
6246 _("net rpc trustdom establish <domain_name>\n"));
6250 domain_name
= smb_xstrdup(argv
[0]);
6251 if (!strupper_m(domain_name
)) {
6252 SAFE_FREE(domain_name
);
6256 /* account name used at first is our domain's name with '$' */
6257 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
6260 if (!strupper_m(acct_name
)) {
6261 SAFE_FREE(domain_name
);
6262 SAFE_FREE(acct_name
);
6267 * opt_workgroup will be used by connection functions further,
6268 * hence it should be set to remote domain name instead of ours
6270 if (c
->opt_workgroup
) {
6271 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6274 c
->opt_user_name
= acct_name
;
6276 /* find the domain controller */
6277 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6278 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6282 /* connect to ipc$ as username/password */
6283 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6284 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6286 /* Is it trusting domain account for sure ? */
6287 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6288 nt_errstr(nt_status
)));
6292 /* store who we connected to */
6294 saf_store( domain_name
, pdc_name
);
6297 * Connect to \\server\ipc$ again (this time anonymously)
6300 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6303 if (NT_STATUS_IS_ERR(nt_status
)) {
6304 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6305 domain_name
, nt_errstr(nt_status
)));
6309 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6310 "domain %s", domain_name
))) {
6311 DEBUG(0, ("talloc_init() failed\n"));
6316 /* Make sure we're talking to a proper server */
6318 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6319 if (!NT_STATUS_IS_OK(nt_status
)) {
6321 talloc_destroy(mem_ctx
);
6326 * Call LsaOpenPolicy and LsaQueryInfo
6329 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6331 if (!NT_STATUS_IS_OK(nt_status
)) {
6332 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6334 talloc_destroy(mem_ctx
);
6338 b
= pipe_hnd
->binding_handle
;
6340 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
6342 if (NT_STATUS_IS_ERR(nt_status
)) {
6343 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6344 nt_errstr(nt_status
)));
6346 talloc_destroy(mem_ctx
);
6350 /* Querying info level 5 */
6352 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6354 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6357 if (NT_STATUS_IS_ERR(nt_status
)) {
6358 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6359 nt_errstr(nt_status
)));
6361 talloc_destroy(mem_ctx
);
6364 if (NT_STATUS_IS_ERR(result
)) {
6365 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6366 nt_errstr(result
)));
6368 talloc_destroy(mem_ctx
);
6372 domain_sid
= info
->account_domain
.sid
;
6374 /* There should be actually query info level 3 (following nt serv behaviour),
6375 but I still don't know if it's _really_ necessary */
6378 * Store the password in secrets db
6381 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6382 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6384 talloc_destroy(mem_ctx
);
6389 * Close the pipes and clean up
6392 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6393 if (NT_STATUS_IS_ERR(nt_status
)) {
6394 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6395 nt_errstr(nt_status
)));
6397 talloc_destroy(mem_ctx
);
6403 talloc_destroy(mem_ctx
);
6405 d_printf(_("Trust to domain %s established\n"), domain_name
);
6410 * Revoke trust relationship to the remote domain.
6412 * @param c A net_context structure.
6413 * @param argc Standard argc.
6414 * @param argv Standard argv without initial components.
6416 * @return Integer status (0 means success).
6419 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6425 if (argc
< 1 || c
->display_usage
) {
6428 _("net rpc trustdom revoke <domain_name>\n"
6429 " Revoke trust relationship\n"
6430 " domain_name\tName of domain to revoke trust\n"));
6434 /* generate upper cased domain name */
6435 domain_name
= smb_xstrdup(argv
[0]);
6436 if (!strupper_m(domain_name
)) {
6437 SAFE_FREE(domain_name
);
6441 /* delete password of the trust */
6442 if (!pdb_del_trusteddom_pw(domain_name
)) {
6443 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6450 SAFE_FREE(domain_name
);
6454 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6455 const struct dom_sid
*domain_sid
,
6456 const char *domain_name
,
6457 struct cli_state
*cli
,
6458 struct rpc_pipe_client
*pipe_hnd
,
6459 TALLOC_CTX
*mem_ctx
,
6464 if (!sid_to_fstring(str_sid
, domain_sid
)) {
6465 return NT_STATUS_UNSUCCESSFUL
;
6467 d_printf("%s\n", str_sid
);
6468 return NT_STATUS_OK
;
6471 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
6475 /* convert sid into ascii string */
6476 sid_to_fstring(ascii_sid
, dom_sid
);
6478 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
6481 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6482 TALLOC_CTX
*mem_ctx
,
6483 struct policy_handle
*pol
,
6484 struct dom_sid dom_sid
,
6485 const char *trusted_dom_name
)
6487 NTSTATUS nt_status
, result
;
6488 union lsa_TrustedDomainInfo
*info
= NULL
;
6489 char *cleartextpwd
= NULL
;
6490 DATA_BLOB session_key
;
6491 DATA_BLOB data
= data_blob_null
;
6492 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6494 nt_status
= dcerpc_lsa_QueryTrustedDomainInfoBySid(b
, mem_ctx
,
6497 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6500 if (NT_STATUS_IS_ERR(nt_status
)) {
6501 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6502 nt_errstr(nt_status
)));
6505 if (NT_STATUS_IS_ERR(result
)) {
6507 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6508 nt_errstr(result
)));
6512 data
= data_blob(info
->password
.password
->data
,
6513 info
->password
.password
->length
);
6515 nt_status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
6516 if (!NT_STATUS_IS_OK(nt_status
)) {
6517 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status
)));
6521 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key
);
6522 data_blob_free(&session_key
);
6524 if (cleartextpwd
== NULL
) {
6525 DEBUG(0,("retrieved NULL password\n"));
6526 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6530 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6531 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6532 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6536 #ifdef DEBUG_PASSWORD
6537 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6538 "password: [%s]\n", trusted_dom_name
,
6539 sid_string_dbg(&dom_sid
), cleartextpwd
));
6543 SAFE_FREE(cleartextpwd
);
6544 data_blob_free(&data
);
6549 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6552 /* common variables */
6553 TALLOC_CTX
* mem_ctx
;
6554 struct cli_state
*cli
= NULL
;
6555 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6556 NTSTATUS nt_status
, result
;
6557 const char *domain_name
= NULL
;
6558 struct policy_handle connect_hnd
;
6559 union lsa_PolicyInformation
*info
= NULL
;
6561 /* trusted domains listing variables */
6562 unsigned int enum_ctx
= 0;
6564 struct lsa_DomainList dom_list
;
6566 struct dcerpc_binding_handle
*b
;
6568 if (c
->display_usage
) {
6570 "net rpc trustdom vampire\n"
6573 _("Vampire trust relationship from remote server"));
6578 * Listing trusted domains (stored in secrets.tdb, if local)
6581 mem_ctx
= talloc_init("trust relationships vampire");
6584 * set domain and pdc name to local samba server (default)
6585 * or to remote one given in command line
6588 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6589 domain_name
= c
->opt_workgroup
;
6590 c
->opt_target_workgroup
= c
->opt_workgroup
;
6592 fstrcpy(pdc_name
, lp_netbios_name());
6593 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6594 c
->opt_target_workgroup
= domain_name
;
6597 /* open \PIPE\lsarpc and open policy handle */
6598 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6599 if (!NT_STATUS_IS_OK(nt_status
)) {
6600 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6601 nt_errstr(nt_status
)));
6602 talloc_destroy(mem_ctx
);
6606 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6608 if (!NT_STATUS_IS_OK(nt_status
)) {
6609 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6610 nt_errstr(nt_status
) ));
6612 talloc_destroy(mem_ctx
);
6616 b
= pipe_hnd
->binding_handle
;
6618 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6620 if (NT_STATUS_IS_ERR(nt_status
)) {
6621 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6622 nt_errstr(nt_status
)));
6624 talloc_destroy(mem_ctx
);
6628 /* query info level 5 to obtain sid of a domain being queried */
6629 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6631 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6635 if (NT_STATUS_IS_ERR(nt_status
)) {
6636 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6637 nt_errstr(nt_status
)));
6639 talloc_destroy(mem_ctx
);
6642 if (NT_STATUS_IS_ERR(result
)) {
6643 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6644 nt_errstr(result
)));
6646 talloc_destroy(mem_ctx
);
6651 * Keep calling LsaEnumTrustdom over opened pipe until
6652 * the end of enumeration is reached
6655 d_printf(_("Vampire trusted domains:\n\n"));
6658 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6664 if (NT_STATUS_IS_ERR(nt_status
)) {
6665 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6666 nt_errstr(nt_status
)));
6668 talloc_destroy(mem_ctx
);
6671 if (NT_STATUS_IS_ERR(result
)) {
6673 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6674 nt_errstr(result
)));
6676 talloc_destroy(mem_ctx
);
6681 for (i
= 0; i
< dom_list
.count
; i
++) {
6683 print_trusted_domain(dom_list
.domains
[i
].sid
,
6684 dom_list
.domains
[i
].name
.string
);
6686 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6687 *dom_list
.domains
[i
].sid
,
6688 dom_list
.domains
[i
].name
.string
);
6689 if (!NT_STATUS_IS_OK(nt_status
)) {
6691 talloc_destroy(mem_ctx
);
6697 * in case of no trusted domains say something rather
6698 * than just display blank line
6700 if (!dom_list
.count
) d_printf(_("none\n"));
6702 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6704 /* close this connection before doing next one */
6705 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6706 if (NT_STATUS_IS_ERR(nt_status
)) {
6707 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6708 nt_errstr(nt_status
)));
6710 talloc_destroy(mem_ctx
);
6714 /* close lsarpc pipe and connection to IPC$ */
6717 talloc_destroy(mem_ctx
);
6721 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6723 /* common variables */
6724 TALLOC_CTX
* mem_ctx
;
6725 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6726 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6727 NTSTATUS nt_status
, result
;
6728 const char *domain_name
= NULL
;
6729 struct dom_sid
*queried_dom_sid
;
6730 int ascii_dom_name_len
;
6731 struct policy_handle connect_hnd
;
6732 union lsa_PolicyInformation
*info
= NULL
;
6733 struct dcerpc_binding_handle
*b
= NULL
;
6735 /* trusted domains listing variables */
6736 unsigned int num_domains
, enum_ctx
= 0;
6738 struct lsa_DomainList dom_list
;
6742 /* trusting domains listing variables */
6743 struct policy_handle domain_hnd
;
6744 struct samr_SamArray
*trusts
= NULL
;
6746 if (c
->display_usage
) {
6748 "net rpc trustdom list\n"
6751 _("List incoming and outgoing trust relationships"));
6756 * Listing trusted domains (stored in secrets.tdb, if local)
6759 mem_ctx
= talloc_init("trust relationships listing");
6762 * set domain and pdc name to local samba server (default)
6763 * or to remote one given in command line
6766 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6767 domain_name
= c
->opt_workgroup
;
6768 c
->opt_target_workgroup
= c
->opt_workgroup
;
6770 fstrcpy(pdc_name
, lp_netbios_name());
6771 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6772 c
->opt_target_workgroup
= domain_name
;
6775 /* open \PIPE\lsarpc and open policy handle */
6776 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6777 if (!NT_STATUS_IS_OK(nt_status
)) {
6778 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6779 nt_errstr(nt_status
)));
6780 talloc_destroy(mem_ctx
);
6784 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6786 if (!NT_STATUS_IS_OK(nt_status
)) {
6787 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6788 nt_errstr(nt_status
) ));
6790 talloc_destroy(mem_ctx
);
6794 b
= pipe_hnd
->binding_handle
;
6796 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6798 if (NT_STATUS_IS_ERR(nt_status
)) {
6799 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6800 nt_errstr(nt_status
)));
6802 talloc_destroy(mem_ctx
);
6806 /* query info level 5 to obtain sid of a domain being queried */
6807 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6809 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6813 if (NT_STATUS_IS_ERR(nt_status
)) {
6814 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6815 nt_errstr(nt_status
)));
6817 talloc_destroy(mem_ctx
);
6820 if (NT_STATUS_IS_ERR(result
)) {
6821 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6822 nt_errstr(result
)));
6824 talloc_destroy(mem_ctx
);
6828 queried_dom_sid
= info
->account_domain
.sid
;
6831 * Keep calling LsaEnumTrustdom over opened pipe until
6832 * the end of enumeration is reached
6835 d_printf(_("Trusted domains list:\n\n"));
6837 found_domain
= false;
6840 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6846 if (NT_STATUS_IS_ERR(nt_status
)) {
6847 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6848 nt_errstr(nt_status
)));
6850 talloc_destroy(mem_ctx
);
6853 if (NT_STATUS_IS_ERR(result
)) {
6854 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6855 nt_errstr(result
)));
6857 talloc_destroy(mem_ctx
);
6862 for (i
= 0; i
< dom_list
.count
; i
++) {
6863 print_trusted_domain(dom_list
.domains
[i
].sid
,
6864 dom_list
.domains
[i
].name
.string
);
6865 found_domain
= true;
6869 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6872 * in case of no trusted domains say something rather
6873 * than just display blank line
6875 if (!found_domain
) {
6876 d_printf(_("none\n"));
6879 /* close this connection before doing next one */
6880 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6881 if (NT_STATUS_IS_ERR(nt_status
)) {
6882 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6883 nt_errstr(nt_status
)));
6885 talloc_destroy(mem_ctx
);
6889 TALLOC_FREE(pipe_hnd
);
6892 * Listing trusting domains (stored in passdb backend, if local)
6895 d_printf(_("\nTrusting domains list:\n\n"));
6898 * Open \PIPE\samr and get needed policy handles
6900 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6902 if (!NT_STATUS_IS_OK(nt_status
)) {
6903 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6905 talloc_destroy(mem_ctx
);
6909 b
= pipe_hnd
->binding_handle
;
6912 nt_status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6914 SAMR_ACCESS_LOOKUP_DOMAIN
,
6917 if (!NT_STATUS_IS_OK(nt_status
)) {
6918 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6919 nt_errstr(nt_status
)));
6921 talloc_destroy(mem_ctx
);
6924 if (!NT_STATUS_IS_OK(result
)) {
6926 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6927 nt_errstr(result
)));
6929 talloc_destroy(mem_ctx
);
6933 /* SamrOpenDomain - we have to open domain policy handle in order to be
6934 able to enumerate accounts*/
6935 nt_status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6937 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6941 if (!NT_STATUS_IS_OK(nt_status
)) {
6942 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6943 nt_errstr(nt_status
)));
6945 talloc_destroy(mem_ctx
);
6948 if (!NT_STATUS_IS_OK(result
)) {
6950 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6951 nt_errstr(result
)));
6953 talloc_destroy(mem_ctx
);
6958 * perform actual enumeration
6961 found_domain
= false;
6963 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6966 nt_status
= dcerpc_samr_EnumDomainUsers(b
, mem_ctx
,
6974 if (NT_STATUS_IS_ERR(nt_status
)) {
6975 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6976 nt_errstr(nt_status
)));
6978 talloc_destroy(mem_ctx
);
6981 if (NT_STATUS_IS_ERR(result
)) {
6983 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6984 nt_errstr(result
)));
6986 talloc_destroy(mem_ctx
);
6990 for (i
= 0; i
< num_domains
; i
++) {
6992 char *str
= discard_const_p(char, trusts
->entries
[i
].name
.string
);
6994 found_domain
= true;
6997 * get each single domain's sid (do we _really_ need this ?):
6998 * 1) connect to domain's pdc
6999 * 2) query the pdc for domain's sid
7002 /* get rid of '$' tail */
7003 ascii_dom_name_len
= strlen(str
);
7004 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
7005 str
[ascii_dom_name_len
- 1] = '\0';
7007 /* set opt_* variables to remote domain */
7008 if (!strupper_m(str
)) {
7010 talloc_destroy(mem_ctx
);
7013 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
7014 c
->opt_target_workgroup
= c
->opt_workgroup
;
7016 d_printf("%-20s", str
);
7018 /* connect to remote domain controller */
7019 nt_status
= net_make_ipc_connection(c
,
7020 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
7022 if (NT_STATUS_IS_OK(nt_status
)) {
7023 /* query for domain's sid */
7024 if (run_rpc_command(
7026 &ndr_table_lsarpc
, 0,
7027 rpc_query_domain_sid
, argc
,
7029 d_printf(_("strange - couldn't get domain's sid\n"));
7031 cli_shutdown(remote_cli
);
7034 d_fprintf(stderr
, _("domain controller is not "
7035 "responding: %s\n"),
7036 nt_errstr(nt_status
));
7037 d_printf(_("couldn't get domain's sid\n"));
7041 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
7043 if (!found_domain
) {
7047 /* close opened samr and domain policy handles */
7048 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &domain_hnd
, &result
);
7049 if (!NT_STATUS_IS_OK(nt_status
)) {
7050 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
7053 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &connect_hnd
, &result
);
7054 if (!NT_STATUS_IS_OK(nt_status
)) {
7055 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
7058 /* close samr pipe and connection to IPC$ */
7061 talloc_destroy(mem_ctx
);
7066 * Entrypoint for 'net rpc trustdom' code.
7068 * @param argc Standard argc.
7069 * @param argv Standard argv without initial components.
7071 * @return Integer status (0 means success).
7074 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
7076 struct functable func
[] = {
7081 N_("Add trusting domain's account"),
7082 N_("net rpc trustdom add\n"
7083 " Add trusting domain's account")
7089 N_("Remove trusting domain's account"),
7090 N_("net rpc trustdom del\n"
7091 " Remove trusting domain's account")
7095 rpc_trustdom_establish
,
7097 N_("Establish outgoing trust relationship"),
7098 N_("net rpc trustdom establish\n"
7099 " Establish outgoing trust relationship")
7103 rpc_trustdom_revoke
,
7105 N_("Revoke outgoing trust relationship"),
7106 N_("net rpc trustdom revoke\n"
7107 " Revoke outgoing trust relationship")
7113 N_("List in- and outgoing domain trusts"),
7114 N_("net rpc trustdom list\n"
7115 " List in- and outgoing domain trusts")
7119 rpc_trustdom_vampire
,
7121 N_("Vampire trusts from remote server"),
7122 N_("net rpc trustdom vampire\n"
7123 " Vampire trusts from remote server")
7125 {NULL
, NULL
, 0, NULL
, NULL
}
7128 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
7132 * Check if a server will take rpc commands
7133 * @param flags Type of server to connect to (PDC, DMB, localhost)
7134 * if the host is not explicitly specified
7135 * @return bool (true means rpc supported)
7137 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
7139 struct cli_state
*cli
;
7141 struct sockaddr_storage server_ss
;
7142 char *server_name
= NULL
;
7145 /* flags (i.e. server type) may depend on command */
7146 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
7149 status
= cli_connect_nb(server_name
, &server_ss
, 0, 0x20,
7150 lp_netbios_name(), SMB_SIGNING_DEFAULT
,
7152 if (!NT_STATUS_IS_OK(status
)) {
7155 status
= smbXcli_negprot(cli
->conn
, cli
->timeout
, PROTOCOL_CORE
,
7157 if (!NT_STATUS_IS_OK(status
))
7159 if (smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_NT1
)
7168 /* dump sam database via samsync rpc calls */
7169 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
7170 if (c
->display_usage
) {
7175 _("Dump remote SAM database"));
7179 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
,
7180 NET_FLAGS_ANONYMOUS
,
7181 rpc_samdump_internals
, argc
, argv
);
7184 /* syncronise sam database via samsync rpc calls */
7185 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
7187 struct functable func
[] = {
7192 N_("Dump remote SAM database to ldif"),
7193 N_("net rpc vampire ldif\n"
7194 " Dump remote SAM database to LDIF file or "
7201 N_("Dump remote SAM database to Kerberos Keytab"),
7202 N_("net rpc vampire keytab\n"
7203 " Dump remote SAM database to Kerberos keytab "
7210 N_("Dump remote SAM database to passdb"),
7211 N_("net rpc vampire passdb\n"
7212 " Dump remote SAM database to passdb")
7215 {NULL
, NULL
, 0, NULL
, NULL
}
7219 if (c
->display_usage
) {
7224 _("Vampire remote SAM database"));
7228 return rpc_vampire_passdb(c
, argc
, argv
);
7231 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
7235 * Migrate everything from a print server.
7237 * @param c A net_context structure.
7238 * @param argc Standard main() style argc.
7239 * @param argv Standard main() style argv. Initial components are already
7242 * @return A shell status integer (0 for success).
7244 * The order is important !
7245 * To successfully add drivers the print queues have to exist !
7246 * Applying ACLs should be the last step, because you're easily locked out.
7249 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
7254 if (c
->display_usage
) {
7256 "net rpc printer migrate all\n"
7259 _("Migrate everything from a print server"));
7264 d_printf(_("no server to migrate\n"));
7268 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7269 rpc_printer_migrate_printers_internals
, argc
,
7274 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7275 rpc_printer_migrate_drivers_internals
, argc
,
7280 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7281 rpc_printer_migrate_forms_internals
, argc
, argv
);
7285 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7286 rpc_printer_migrate_settings_internals
, argc
,
7291 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7292 rpc_printer_migrate_security_internals
, argc
,
7298 * Migrate print drivers from a print server.
7300 * @param c A net_context structure.
7301 * @param argc Standard main() style argc.
7302 * @param argv Standard main() style argv. Initial components are already
7305 * @return A shell status integer (0 for success).
7307 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
7310 if (c
->display_usage
) {
7312 "net rpc printer migrate drivers\n"
7315 _("Migrate print-drivers from a print-server"));
7320 d_printf(_("no server to migrate\n"));
7324 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7325 rpc_printer_migrate_drivers_internals
,
7330 * Migrate print-forms from a print-server.
7332 * @param c A net_context structure.
7333 * @param argc Standard main() style argc.
7334 * @param argv Standard main() style argv. Initial components are already
7337 * @return A shell status integer (0 for success).
7339 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7342 if (c
->display_usage
) {
7344 "net rpc printer migrate forms\n"
7347 _("Migrate print-forms from a print-server"));
7352 d_printf(_("no server to migrate\n"));
7356 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7357 rpc_printer_migrate_forms_internals
,
7362 * Migrate printers from a print-server.
7364 * @param c A net_context structure.
7365 * @param argc Standard main() style argc.
7366 * @param argv Standard main() style argv. Initial components are already
7369 * @return A shell status integer (0 for success).
7371 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7374 if (c
->display_usage
) {
7376 "net rpc printer migrate printers\n"
7379 _("Migrate printers from a print-server"));
7384 d_printf(_("no server to migrate\n"));
7388 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7389 rpc_printer_migrate_printers_internals
,
7394 * Migrate printer-ACLs from a print-server
7396 * @param c A net_context structure.
7397 * @param argc Standard main() style argc.
7398 * @param argv Standard main() style argv. Initial components are already
7401 * @return A shell status integer (0 for success).
7403 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7406 if (c
->display_usage
) {
7408 "net rpc printer migrate security\n"
7411 _("Migrate printer-ACLs from a print-server"));
7416 d_printf(_("no server to migrate\n"));
7420 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7421 rpc_printer_migrate_security_internals
,
7426 * Migrate printer-settings from a print-server.
7428 * @param c A net_context structure.
7429 * @param argc Standard main() style argc.
7430 * @param argv Standard main() style argv. Initial components are already
7433 * @return A shell status integer (0 for success).
7435 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7438 if (c
->display_usage
) {
7440 "net rpc printer migrate settings\n"
7443 _("Migrate printer-settings from a "
7449 d_printf(_("no server to migrate\n"));
7453 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7454 rpc_printer_migrate_settings_internals
,
7459 * 'net rpc printer' entrypoint.
7461 * @param c A net_context structure.
7462 * @param argc Standard main() style argc.
7463 * @param argv Standard main() style argv. Initial components are already
7467 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7470 /* ouch: when addriver and setdriver are called from within
7471 rpc_printer_migrate_drivers_internals, the printer-queue already
7474 struct functable func
[] = {
7477 rpc_printer_migrate_all
,
7479 N_("Migrate all from remote to local print server"),
7480 N_("net rpc printer migrate all\n"
7481 " Migrate all from remote to local print server")
7485 rpc_printer_migrate_drivers
,
7487 N_("Migrate drivers to local server"),
7488 N_("net rpc printer migrate drivers\n"
7489 " Migrate drivers to local server")
7493 rpc_printer_migrate_forms
,
7495 N_("Migrate froms to local server"),
7496 N_("net rpc printer migrate forms\n"
7497 " Migrate froms to local server")
7501 rpc_printer_migrate_printers
,
7503 N_("Migrate printers to local server"),
7504 N_("net rpc printer migrate printers\n"
7505 " Migrate printers to local server")
7509 rpc_printer_migrate_security
,
7511 N_("Mirgate printer ACLs to local server"),
7512 N_("net rpc printer migrate security\n"
7513 " Mirgate printer ACLs to local server")
7517 rpc_printer_migrate_settings
,
7519 N_("Migrate printer settings to local server"),
7520 N_("net rpc printer migrate settings\n"
7521 " Migrate printer settings to local server")
7523 {NULL
, NULL
, 0, NULL
, NULL
}
7526 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7531 * List printers on a remote RPC server.
7533 * @param c A net_context structure.
7534 * @param argc Standard main() style argc.
7535 * @param argv Standard main() style argv. Initial components are already
7538 * @return A shell status integer (0 for success).
7540 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7542 if (c
->display_usage
) {
7544 "net rpc printer list\n"
7547 _("List printers on a remote RPC server"));
7551 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7552 rpc_printer_list_internals
,
7557 * List printer-drivers on a remote RPC server.
7559 * @param c A net_context structure.
7560 * @param argc Standard main() style argc.
7561 * @param argv Standard main() style argv. Initial components are already
7564 * @return A shell status integer (0 for success).
7566 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7569 if (c
->display_usage
) {
7571 "net rpc printer driver\n"
7574 _("List printer-drivers on a remote RPC server"));
7578 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7579 rpc_printer_driver_list_internals
,
7584 * Publish printer in ADS via MSRPC.
7586 * @param c A net_context structure.
7587 * @param argc Standard main() style argc.
7588 * @param argv Standard main() style argv. Initial components are already
7591 * @return A shell status integer (0 for success).
7593 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7596 if (c
->display_usage
) {
7598 "net rpc printer publish publish\n"
7601 _("Publish printer in ADS via MSRPC"));
7605 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7606 rpc_printer_publish_publish_internals
,
7611 * Update printer in ADS via MSRPC.
7613 * @param c A net_context structure.
7614 * @param argc Standard main() style argc.
7615 * @param argv Standard main() style argv. Initial components are already
7618 * @return A shell status integer (0 for success).
7620 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7622 if (c
->display_usage
) {
7624 "net rpc printer publish update\n"
7627 _("Update printer in ADS via MSRPC"));
7631 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7632 rpc_printer_publish_update_internals
,
7637 * UnPublish printer in ADS via MSRPC.
7639 * @param c A net_context structure.
7640 * @param argc Standard main() style argc.
7641 * @param argv Standard main() style argv. Initial components are already
7644 * @return A shell status integer (0 for success).
7646 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7649 if (c
->display_usage
) {
7651 "net rpc printer publish unpublish\n"
7654 _("UnPublish printer in ADS via MSRPC"));
7658 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7659 rpc_printer_publish_unpublish_internals
,
7664 * List published printers via MSRPC.
7666 * @param c A net_context structure.
7667 * @param argc Standard main() style argc.
7668 * @param argv Standard main() style argv. Initial components are already
7671 * @return A shell status integer (0 for success).
7673 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7676 if (c
->display_usage
) {
7678 "net rpc printer publish list\n"
7681 _("List published printers via MSRPC"));
7685 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7686 rpc_printer_publish_list_internals
,
7692 * Publish printer in ADS.
7694 * @param c A net_context structure.
7695 * @param argc Standard main() style argc.
7696 * @param argv Standard main() style argv. Initial components are already
7699 * @return A shell status integer (0 for success).
7701 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7705 struct functable func
[] = {
7708 rpc_printer_publish_publish
,
7710 N_("Publish printer in AD"),
7711 N_("net rpc printer publish publish\n"
7712 " Publish printer in AD")
7716 rpc_printer_publish_update
,
7718 N_("Update printer in AD"),
7719 N_("net rpc printer publish update\n"
7720 " Update printer in AD")
7724 rpc_printer_publish_unpublish
,
7726 N_("Unpublish printer"),
7727 N_("net rpc printer publish unpublish\n"
7728 " Unpublish printer")
7732 rpc_printer_publish_list
,
7734 N_("List published printers"),
7735 N_("net rpc printer publish list\n"
7736 " List published printers")
7738 {NULL
, NULL
, 0, NULL
, NULL
}
7742 if (c
->display_usage
) {
7743 d_printf(_("Usage:\n"));
7744 d_printf(_("net rpc printer publish\n"
7745 " List published printers\n"
7746 " Alias of net rpc printer publish "
7748 net_display_usage_from_functable(func
);
7751 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7752 rpc_printer_publish_list_internals
,
7756 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7762 * Display rpc printer help page.
7764 * @param c A net_context structure.
7765 * @param argc Standard main() style argc.
7766 * @param argv Standard main() style argv. Initial components are already
7769 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7771 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7772 "\tlists all printers on print-server\n\n"));
7773 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7774 "\tlists all printer-drivers on print-server\n\n"));
7775 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7776 "\tpublishes printer settings in Active Directory\n"
7777 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7778 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7779 "\n\tmigrates printers from remote to local server\n\n"));
7780 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7781 "\n\tmigrates printer-settings from remote to local server\n\n"));
7782 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7783 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7784 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7785 "\n\tmigrates printer-forms from remote to local server\n\n"));
7786 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7787 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7788 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7789 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7790 "\tremote to local print-server\n\n"));
7791 net_common_methods_usage(c
, argc
, argv
);
7792 net_common_flags_usage(c
, argc
, argv
);
7794 "\t-v or --verbose\t\t\tgive verbose output\n"
7795 "\t --destination\t\tmigration target server (default: localhost)\n"));
7801 * 'net rpc printer' entrypoint.
7803 * @param c A net_context structure.
7804 * @param argc Standard main() style argc.
7805 * @param argv Standard main() style argv. Initial components are already
7808 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7810 struct functable func
[] = {
7815 N_("List all printers on print server"),
7816 N_("net rpc printer list\n"
7817 " List all printers on print server")
7821 rpc_printer_migrate
,
7823 N_("Migrate printer to local server"),
7824 N_("net rpc printer migrate\n"
7825 " Migrate printer to local server")
7829 rpc_printer_driver_list
,
7831 N_("List printer drivers"),
7832 N_("net rpc printer driver\n"
7833 " List printer drivers")
7837 rpc_printer_publish
,
7839 N_("Publish printer in AD"),
7840 N_("net rpc printer publish\n"
7841 " Publish printer in AD")
7843 {NULL
, NULL
, 0, NULL
, NULL
}
7847 if (c
->display_usage
) {
7848 d_printf(_("Usage:\n"));
7849 d_printf(_("net rpc printer\n"
7850 " List printers\n"));
7851 net_display_usage_from_functable(func
);
7854 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7855 rpc_printer_list_internals
,
7859 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7863 * 'net rpc' entrypoint.
7865 * @param c A net_context structure.
7866 * @param argc Standard main() style argc.
7867 * @param argv Standard main() style argv. Initial components are already
7871 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7873 NET_API_STATUS status
;
7875 struct functable func
[] = {
7880 N_("Modify global audit settings"),
7881 N_("net rpc audit\n"
7882 " Modify global audit settings")
7888 N_("Show basic info about a domain"),
7890 " Show basic info about a domain")
7896 N_("Join a domain"),
7904 N_("Join a domain created in server manager"),
7905 N_("net rpc oldjoin\n"
7906 " Join a domain created in server manager")
7912 N_("Test that a join is valid"),
7913 N_("net rpc testjoin\n"
7914 " Test that a join is valid")
7920 N_("List/modify users"),
7922 " List/modify users")
7928 N_("Change a user password"),
7929 N_("net rpc password\n"
7930 " Change a user password\n"
7931 " Alias for net rpc user password")
7937 N_("List/modify groups"),
7938 N_("net rpc group\n"
7939 " List/modify groups")
7945 N_("List/modify shares"),
7946 N_("net rpc share\n"
7947 " List/modify shares")
7953 N_("List open files"),
7961 N_("List/modify printers"),
7962 N_("net rpc printer\n"
7963 " List/modify printers")
7967 net_rpc_changetrustpw
,
7969 N_("Change trust account password"),
7970 N_("net rpc changetrustpw\n"
7971 " Change trust account password")
7977 N_("Modify domain trusts"),
7978 N_("net rpc trustdom\n"
7979 " Modify domain trusts")
7985 N_("Abort a remote shutdown"),
7986 N_("net rpc abortshutdown\n"
7987 " Abort a remote shutdown")
7993 N_("Shutdown a remote server"),
7994 N_("net rpc shutdown\n"
7995 " Shutdown a remote server")
8001 N_("Dump SAM data of remote NT PDC"),
8002 N_("net rpc samdump\n"
8003 " Dump SAM data of remote NT PDC")
8009 N_("Sync a remote NT PDC's data into local passdb"),
8010 N_("net rpc vampire\n"
8011 " Sync a remote NT PDC's data into local passdb")
8017 N_("Fetch the domain sid into local secrets.tdb"),
8018 N_("net rpc getsid\n"
8019 " Fetch the domain sid into local secrets.tdb")
8025 N_("Manage privileges assigned to SID"),
8026 N_("net rpc rights\n"
8027 " Manage privileges assigned to SID")
8033 N_("Start/stop/query remote services"),
8034 N_("net rpc service\n"
8035 " Start/stop/query remote services")
8041 N_("Manage registry hives"),
8042 N_("net rpc registry\n"
8043 " Manage registry hives")
8049 N_("Open interactive shell on remote server"),
8050 N_("net rpc shell\n"
8051 " Open interactive shell on remote server")
8057 N_("Manage trusts"),
8058 N_("net rpc trust\n"
8065 N_("Configure a remote samba server"),
8067 " Configure a remote samba server")
8069 {NULL
, NULL
, 0, NULL
, NULL
}
8072 status
= libnetapi_net_init(&c
->netapi_ctx
);
8076 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
8077 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
8078 if (c
->opt_kerberos
) {
8079 libnetapi_set_use_kerberos(c
->netapi_ctx
);
8081 if (c
->opt_ccache
) {
8082 libnetapi_set_use_ccache(c
->netapi_ctx
);
8085 return net_run_function(c
, argc
, argv
, "net rpc", func
);