2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/cli_samr.h"
29 #include "rpc_client/init_samr.h"
30 #include "../librpc/gen_ndr/ndr_lsa_c.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
34 #include "../librpc/gen_ndr/ndr_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
36 #include "../librpc/gen_ndr/ndr_winreg_c.h"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "rpc_client/init_lsa.h"
41 #include "../libcli/security/security.h"
42 #include "libsmb/libsmb.h"
43 #include "libsmb/clirap.h"
44 #include "nsswitch/libwbclient/wbclient.h"
47 static int net_mode_share
;
48 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
);
53 * @brief RPC based subcommands for the 'net' utility.
55 * This file should contain much of the functionality that used to
56 * be found in rpcclient, execpt that the commands should change
57 * less often, and the fucntionality should be sane (the user is not
58 * expected to know a rid/sid before they conduct an operation etc.)
60 * @todo Perhaps eventually these should be split out into a number
61 * of files, as this could get quite big.
66 * Many of the RPC functions need the domain sid. This function gets
67 * it at the start of every run
69 * @param cli A cli_state already connected to the remote machine
71 * @return The Domain SID of the remote machine.
74 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
75 struct dom_sid
**domain_sid
,
76 const char **domain_name
)
78 struct rpc_pipe_client
*lsa_pipe
= NULL
;
79 struct policy_handle pol
;
80 NTSTATUS status
, result
;
81 union lsa_PolicyInformation
*info
= NULL
;
82 struct dcerpc_binding_handle
*b
;
84 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
86 if (!NT_STATUS_IS_OK(status
)) {
87 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
91 b
= lsa_pipe
->binding_handle
;
93 status
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
94 SEC_FLAG_MAXIMUM_ALLOWED
,
96 if (!NT_STATUS_IS_OK(status
)) {
97 d_fprintf(stderr
, "open_policy %s: %s\n",
103 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
105 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
108 if (!NT_STATUS_IS_OK(status
)) {
109 d_fprintf(stderr
, "lsaquery %s: %s\n",
114 if (!NT_STATUS_IS_OK(result
)) {
115 d_fprintf(stderr
, "lsaquery %s: %s\n",
121 *domain_name
= info
->account_domain
.name
.string
;
122 *domain_sid
= info
->account_domain
.sid
;
124 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
125 TALLOC_FREE(lsa_pipe
);
131 * Run a single RPC command, from start to finish.
133 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
134 * @param conn_flag a NET_FLAG_ combination. Passed to
135 * net_make_ipc_connection.
136 * @param argc Standard main() style argc.
137 * @param argv Standard main() style argv. Initial components are already
139 * @return A shell status integer (0 for success).
142 int run_rpc_command(struct net_context
*c
,
143 struct cli_state
*cli_arg
,
144 const struct ndr_interface_table
*table
,
150 struct cli_state
*cli
= NULL
;
151 struct rpc_pipe_client
*pipe_hnd
= NULL
;
154 struct dom_sid
*domain_sid
;
155 const char *domain_name
;
158 /* make use of cli_state handed over as an argument, if possible */
160 nt_status
= net_make_ipc_connection(c
, conn_flags
, &cli
);
161 if (!NT_STATUS_IS_OK(nt_status
)) {
162 DEBUG(1, ("failed to make ipc connection: %s\n",
163 nt_errstr(nt_status
)));
176 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
177 DEBUG(0, ("talloc_init() failed\n"));
181 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
183 if (!NT_STATUS_IS_OK(nt_status
)) {
187 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
188 if (lp_client_schannel()
189 && (ndr_syntax_id_equal(&table
->syntax_id
,
190 &ndr_table_netlogon
.syntax_id
))) {
191 /* Always try and create an schannel netlogon pipe. */
192 nt_status
= cli_rpc_pipe_open_schannel(
193 cli
, &table
->syntax_id
, NCACN_NP
,
194 DCERPC_AUTH_LEVEL_PRIVACY
, domain_name
,
196 if (!NT_STATUS_IS_OK(nt_status
)) {
197 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
198 nt_errstr(nt_status
) ));
202 if (conn_flags
& NET_FLAGS_SEAL
) {
203 nt_status
= cli_rpc_pipe_open_generic_auth(
205 (conn_flags
& NET_FLAGS_TCP
) ?
206 NCACN_IP_TCP
: NCACN_NP
,
207 DCERPC_AUTH_TYPE_NTLMSSP
,
208 DCERPC_AUTH_LEVEL_PRIVACY
,
209 cli_state_remote_name(cli
),
210 lp_workgroup(), c
->opt_user_name
,
211 c
->opt_password
, &pipe_hnd
);
213 nt_status
= cli_rpc_pipe_open_noauth(
214 cli
, &table
->syntax_id
,
217 if (!NT_STATUS_IS_OK(nt_status
)) {
218 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
220 nt_errstr(nt_status
) ));
226 nt_status
= fn(c
, domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
228 if (!NT_STATUS_IS_OK(nt_status
)) {
229 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
232 DEBUG(5, ("rpc command function succedded\n"));
235 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
237 TALLOC_FREE(pipe_hnd
);
242 /* close the connection only if it was opened here */
247 talloc_destroy(mem_ctx
);
252 * Force a change of the trust acccount password.
254 * All parameters are provided by the run_rpc_command function, except for
255 * argc, argv which are passed through.
257 * @param domain_sid The domain sid acquired from the remote server.
258 * @param cli A cli_state connected to the server.
259 * @param mem_ctx Talloc context, destroyed on completion of the function.
260 * @param argc Standard main() style argc.
261 * @param argv Standard main() style argv. Initial components are already
264 * @return Normal NTSTATUS return.
267 static NTSTATUS
rpc_changetrustpw_internals(struct net_context
*c
,
268 const struct dom_sid
*domain_sid
,
269 const char *domain_name
,
270 struct cli_state
*cli
,
271 struct rpc_pipe_client
*pipe_hnd
,
278 status
= trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
);
279 if (!NT_STATUS_IS_OK(status
)) {
280 d_fprintf(stderr
, _("Failed to change machine account password: %s\n"),
289 * Force a change of the trust acccount password.
291 * @param argc Standard main() style argc.
292 * @param argv Standard main() style argv. Initial components are already
295 * @return A shell status integer (0 for success).
298 int net_rpc_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
300 if (c
->display_usage
) {
302 "net rpc changetrustpw\n"
305 _("Change the machine trust password"));
309 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
,
310 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
311 rpc_changetrustpw_internals
,
316 * Join a domain, the old way.
318 * This uses 'machinename' as the inital password, and changes it.
320 * The password should be created with 'server manager' or equiv first.
322 * All parameters are provided by the run_rpc_command function, except for
323 * argc, argv which are passed through.
325 * @param domain_sid The domain sid acquired from the remote server.
326 * @param cli A cli_state connected to the server.
327 * @param mem_ctx Talloc context, destroyed on completion of the function.
328 * @param argc Standard main() style argc.
329 * @param argv Standard main() style argv. Initial components are already
332 * @return Normal NTSTATUS return.
335 static NTSTATUS
rpc_oldjoin_internals(struct net_context
*c
,
336 const struct dom_sid
*domain_sid
,
337 const char *domain_name
,
338 struct cli_state
*cli
,
339 struct rpc_pipe_client
*pipe_hnd
,
345 fstring trust_passwd
;
346 unsigned char orig_trust_passwd_hash
[16];
348 enum netr_SchannelType sec_channel_type
;
350 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
352 if (!NT_STATUS_IS_OK(result
)) {
353 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
355 cli_state_remote_name(cli
),
356 nt_errstr(result
) ));
361 check what type of join - if the user want's to join as
362 a BDC, the server must agree that we are a BDC.
365 sec_channel_type
= get_sec_channel_type(argv
[0]);
367 sec_channel_type
= get_sec_channel_type(NULL
);
370 fstrcpy(trust_passwd
, lp_netbios_name());
371 strlower_m(trust_passwd
);
374 * Machine names can be 15 characters, but the max length on
375 * a password is 14. --jerry
378 trust_passwd
[14] = '\0';
380 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
382 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
,
384 orig_trust_passwd_hash
,
387 if (NT_STATUS_IS_OK(result
))
388 printf(_("Joined domain %s.\n"), c
->opt_target_workgroup
);
391 if (!secrets_store_domain_sid(c
->opt_target_workgroup
, domain_sid
)) {
392 DEBUG(0, ("error storing domain sid for %s\n", c
->opt_target_workgroup
));
393 result
= NT_STATUS_UNSUCCESSFUL
;
400 * Join a domain, the old way.
402 * @param argc Standard main() style argc.
403 * @param argv Standard main() style argv. Initial components are already
406 * @return A shell status integer (0 for success).
409 static int net_rpc_perform_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
411 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
,
412 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
413 rpc_oldjoin_internals
,
418 * Join a domain, the old way. This function exists to allow
419 * the message to be displayed when oldjoin was explicitly
420 * requested, but not when it was implied by "net rpc join".
422 * @param argc Standard main() style argc.
423 * @param argv Standard main() style argv. Initial components are already
426 * @return A shell status integer (0 for success).
429 static int net_rpc_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
433 if (c
->display_usage
) {
438 _("Join a domain the old way"));
442 rc
= net_rpc_perform_oldjoin(c
, argc
, argv
);
445 d_fprintf(stderr
, _("Failed to join domain\n"));
452 * 'net rpc join' entrypoint.
453 * @param argc Standard main() style argc.
454 * @param argv Standard main() style argv. Initial components are already
457 * Main 'net_rpc_join()' (where the admin username/password is used) is
459 * Try to just change the password, but if that doesn't work, use/prompt
460 * for a username/password.
463 int net_rpc_join(struct net_context
*c
, int argc
, const char **argv
)
465 if (c
->display_usage
) {
468 _("net rpc join -U <username>[%%password] <type>\n"
470 " username\tName of the admin user"
471 " password\tPassword of the admin user, will "
472 "prompt if not specified\n"
473 " type\tCan be one of the following:\n"
474 "\t\tMEMBER\tJoin as member server (default)\n"
475 "\t\tBDC\tJoin as BDC\n"
476 "\t\tPDC\tJoin as PDC\n"));
480 if (lp_server_role() == ROLE_STANDALONE
) {
481 d_printf(_("cannot join as standalone machine\n"));
485 if (strlen(lp_netbios_name()) > 15) {
486 d_printf(_("Our netbios name can be at most 15 chars long, "
487 "\"%s\" is %u chars long\n"),
488 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
492 if ((net_rpc_perform_oldjoin(c
, argc
, argv
) == 0))
495 return net_rpc_join_newstyle(c
, argc
, argv
);
499 * display info about a rpc domain
501 * All parameters are provided by the run_rpc_command function, except for
502 * argc, argv which are passed through.
504 * @param domain_sid The domain sid acquired from the remote server
505 * @param cli A cli_state connected to the server.
506 * @param mem_ctx Talloc context, destroyed on completion of the function.
507 * @param argc Standard main() style argc.
508 * @param argv Standard main() style argv. Initial components are already
511 * @return Normal NTSTATUS return.
514 NTSTATUS
rpc_info_internals(struct net_context
*c
,
515 const struct dom_sid
*domain_sid
,
516 const char *domain_name
,
517 struct cli_state
*cli
,
518 struct rpc_pipe_client
*pipe_hnd
,
523 struct policy_handle connect_pol
, domain_pol
;
524 NTSTATUS status
, result
;
525 union samr_DomainInfo
*info
= NULL
;
527 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
529 sid_to_fstring(sid_str
, domain_sid
);
531 /* Get sam policy handle */
532 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
534 MAXIMUM_ALLOWED_ACCESS
,
537 if (!NT_STATUS_IS_OK(status
)) {
538 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
543 if (!NT_STATUS_IS_OK(result
)) {
545 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
550 /* Get domain policy handle */
551 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
553 MAXIMUM_ALLOWED_ACCESS
,
554 discard_const_p(struct dom_sid2
, domain_sid
),
557 if (!NT_STATUS_IS_OK(status
)) {
558 d_fprintf(stderr
, _("Could not open domain: %s\n"),
562 if (!NT_STATUS_IS_OK(result
)) {
564 d_fprintf(stderr
, _("Could not open domain: %s\n"),
569 status
= dcerpc_samr_QueryDomainInfo(b
, mem_ctx
,
574 if (!NT_STATUS_IS_OK(status
)) {
578 if (NT_STATUS_IS_OK(result
)) {
579 d_printf(_("Domain Name: %s\n"),
580 info
->general
.domain_name
.string
);
581 d_printf(_("Domain SID: %s\n"), sid_str
);
582 d_printf(_("Sequence number: %llu\n"),
583 (unsigned long long)info
->general
.sequence_num
);
584 d_printf(_("Num users: %u\n"), info
->general
.num_users
);
585 d_printf(_("Num domain groups: %u\n"),info
->general
.num_groups
);
586 d_printf(_("Num local groups: %u\n"),info
->general
.num_aliases
);
594 * 'net rpc info' entrypoint.
595 * @param argc Standard main() style argc.
596 * @param argv Standard main() style argv. Initial components are already
600 int net_rpc_info(struct net_context
*c
, int argc
, const char **argv
)
602 if (c
->display_usage
) {
607 _("Display information about the domain"));
611 return run_rpc_command(c
, NULL
, &ndr_table_samr
,
612 NET_FLAGS_PDC
, rpc_info_internals
,
617 * Fetch domain SID into the local secrets.tdb.
619 * All parameters are provided by the run_rpc_command function, except for
620 * argc, argv which are passed through.
622 * @param domain_sid The domain sid acquired from the remote server.
623 * @param cli A cli_state connected to the server.
624 * @param mem_ctx Talloc context, destroyed on completion of the function.
625 * @param argc Standard main() style argc.
626 * @param argv Standard main() style argv. Initial components are already
629 * @return Normal NTSTATUS return.
632 static NTSTATUS
rpc_getsid_internals(struct net_context
*c
,
633 const struct dom_sid
*domain_sid
,
634 const char *domain_name
,
635 struct cli_state
*cli
,
636 struct rpc_pipe_client
*pipe_hnd
,
643 sid_to_fstring(sid_str
, domain_sid
);
644 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
645 sid_str
, domain_name
);
647 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
648 DEBUG(0,("Can't store domain SID\n"));
649 return NT_STATUS_UNSUCCESSFUL
;
656 * 'net rpc getsid' entrypoint.
657 * @param argc Standard main() style argc.
658 * @param argv Standard main() style argv. Initial components are already
662 int net_rpc_getsid(struct net_context
*c
, int argc
, const char **argv
)
664 int conn_flags
= NET_FLAGS_PDC
;
666 if (!c
->opt_user_specified
) {
667 conn_flags
|= NET_FLAGS_ANONYMOUS
;
670 if (c
->display_usage
) {
675 _("Fetch domain SID into local secrets.tdb"));
679 return run_rpc_command(c
, NULL
, &ndr_table_samr
,
681 rpc_getsid_internals
,
685 /****************************************************************************/
688 * Basic usage function for 'net rpc user'.
689 * @param argc Standard main() style argc.
690 * @param argv Standard main() style argv. Initial components are already
694 static int rpc_user_usage(struct net_context
*c
, int argc
, const char **argv
)
696 return net_user_usage(c
, argc
, argv
);
700 * Add a new user to a remote RPC server.
702 * @param argc Standard main() style argc.
703 * @param argv Standard main() style argv. Initial components are already
706 * @return A shell status integer (0 for success).
709 static int rpc_user_add(struct net_context
*c
, int argc
, const char **argv
)
711 NET_API_STATUS status
;
712 struct USER_INFO_1 info1
;
713 uint32_t parm_error
= 0;
715 if (argc
< 1 || c
->display_usage
) {
716 rpc_user_usage(c
, argc
, argv
);
722 info1
.usri1_name
= argv
[0];
724 info1
.usri1_password
= argv
[1];
727 status
= NetUserAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
730 d_fprintf(stderr
,_("Failed to add user '%s' with error: %s.\n"),
731 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
735 d_printf(_("Added user '%s'.\n"), argv
[0]);
742 * Rename a user on a remote RPC server.
744 * @param argc Standard main() style argc.
745 * @param argv Standard main() style argv. Initial components are already
748 * @return A shell status integer (0 for success).
751 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
753 NET_API_STATUS status
;
754 struct USER_INFO_0 u0
;
755 uint32_t parm_err
= 0;
757 if (argc
!= 2 || c
->display_usage
) {
758 rpc_user_usage(c
, argc
, argv
);
762 u0
.usri0_name
= argv
[1];
764 status
= NetUserSetInfo(c
->opt_host
, argv
[0],
765 0, (uint8_t *)&u0
, &parm_err
);
768 _("Failed to rename user from %s to %s - %s\n"),
770 libnetapi_get_error_string(c
->netapi_ctx
, status
));
772 d_printf(_("Renamed user from %s to %s\n"), argv
[0], argv
[1]);
779 * Set a user's primary group
781 * @param argc Standard main() style argc.
782 * @param argv Standard main() style argv. Initial components are already
785 * @return A shell status integer (0 for success).
788 static int rpc_user_setprimarygroup(struct net_context
*c
, int argc
,
791 NET_API_STATUS status
;
793 struct GROUP_INFO_2
*g2
;
794 struct USER_INFO_1051 u1051
;
795 uint32_t parm_err
= 0;
797 if (argc
!= 2 || c
->display_usage
) {
798 rpc_user_usage(c
, argc
, argv
);
802 status
= NetGroupGetInfo(c
->opt_host
, argv
[1], 2, &buffer
);
804 d_fprintf(stderr
, _("Failed to find group name %s -- %s\n"),
806 libnetapi_get_error_string(c
->netapi_ctx
, status
));
809 g2
= (struct GROUP_INFO_2
*)buffer
;
811 u1051
.usri1051_primary_group_id
= g2
->grpi2_group_id
;
813 NetApiBufferFree(buffer
);
815 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1051,
816 (uint8_t *)&u1051
, &parm_err
);
819 _("Failed to set user's primary group %s to %s - "
820 "%s\n"), argv
[0], argv
[1],
821 libnetapi_get_error_string(c
->netapi_ctx
, status
));
823 d_printf(_("Set primary group of user %s to %s\n"), argv
[0],
830 * Delete a user from a remote RPC server.
832 * @param argc Standard main() style argc.
833 * @param argv Standard main() style argv. Initial components are already
836 * @return A shell status integer (0 for success).
839 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
841 NET_API_STATUS status
;
843 if (argc
< 1 || c
->display_usage
) {
844 rpc_user_usage(c
, argc
, argv
);
848 status
= NetUserDel(c
->opt_host
, argv
[0]);
851 d_fprintf(stderr
, _("Failed to delete user '%s' with: %s.\n"),
853 libnetapi_get_error_string(c
->netapi_ctx
, status
));
856 d_printf(_("Deleted user '%s'.\n"), argv
[0]);
863 * Set a user's password on a remote RPC server.
865 * @param argc Standard main() style argc.
866 * @param argv Standard main() style argv. Initial components are already
869 * @return A shell status integer (0 for success).
872 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
874 NET_API_STATUS status
;
876 struct USER_INFO_1003 u1003
;
877 uint32_t parm_err
= 0;
880 if (argc
< 1 || c
->display_usage
) {
881 rpc_user_usage(c
, argc
, argv
);
886 u1003
.usri1003_password
= argv
[1];
888 ret
= asprintf(&prompt
, _("Enter new password for %s:"),
893 u1003
.usri1003_password
= talloc_strdup(c
, getpass(prompt
));
895 if (u1003
.usri1003_password
== NULL
) {
900 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
902 /* Display results */
905 _("Failed to set password for '%s' with error: %s.\n"),
906 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
915 * List a user's groups from a remote RPC server.
917 * @param argc Standard main() style argc.
918 * @param argv Standard main() style argv. Initial components are already
921 * @return A shell status integer (0 for success)
924 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
927 NET_API_STATUS status
;
928 struct GROUP_USERS_INFO_0
*u0
= NULL
;
929 uint32_t entries_read
= 0;
930 uint32_t total_entries
= 0;
934 if (argc
< 1 || c
->display_usage
) {
935 rpc_user_usage(c
, argc
, argv
);
939 status
= NetUserGetGroups(c
->opt_host
,
942 (uint8_t **)(void *)&u0
,
948 _("Failed to get groups for '%s' with error: %s.\n"),
949 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
954 for (i
=0; i
< entries_read
; i
++) {
955 printf("%s\n", u0
->grui0_name
);
963 * List users on a remote RPC server.
965 * All parameters are provided by the run_rpc_command function, except for
966 * argc, argv which are passed through.
968 * @param domain_sid The domain sid acquired from the remote server.
969 * @param cli A cli_state connected to the server.
970 * @param mem_ctx Talloc context, destroyed on completion of the function.
971 * @param argc Standard main() style argc.
972 * @param argv Standard main() style argv. Initial components are already
975 * @return Normal NTSTATUS return.
978 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
980 NET_API_STATUS status
;
981 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
982 struct NET_DISPLAY_USER
*info
= NULL
;
985 /* Query domain users */
986 if (c
->opt_long_list_entries
)
987 d_printf(_("\nUser name Comment"
988 "\n-----------------------------\n"));
990 uint32_t max_entries
, max_size
;
992 dcerpc_get_query_dispinfo_params(
993 loop_count
, &max_entries
, &max_size
);
995 status
= NetQueryDisplayInformation(c
->opt_host
,
1002 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
1006 info
= (struct NET_DISPLAY_USER
*)buffer
;
1008 for (i
= 0; i
< num_entries
; i
++) {
1010 if (c
->opt_long_list_entries
)
1011 printf("%-21.21s %s\n", info
->usri1_name
,
1012 info
->usri1_comment
);
1014 printf("%s\n", info
->usri1_name
);
1018 NetApiBufferFree(buffer
);
1021 start_idx
+= num_entries
;
1023 } while (status
== ERROR_MORE_DATA
);
1029 * 'net rpc user' entrypoint.
1030 * @param argc Standard main() style argc.
1031 * @param argv Standard main() style argv. Initial components are already
1035 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
1037 NET_API_STATUS status
;
1039 struct functable func
[] = {
1044 N_("Add specified user"),
1045 N_("net rpc user add\n"
1046 " Add specified user")
1052 N_("List domain groups of user"),
1053 N_("net rpc user info\n"
1054 " List domain groups of user")
1060 N_("Remove specified user"),
1061 N_("net rpc user delete\n"
1062 " Remove specified user")
1068 N_("Change user password"),
1069 N_("net rpc user password\n"
1070 " Change user password")
1076 N_("Rename specified user"),
1077 N_("net rpc user rename\n"
1078 " Rename specified user")
1082 rpc_user_setprimarygroup
,
1084 "Set a user's primary group",
1085 "net rpc user setprimarygroup\n"
1086 " Set a user's primary group"
1088 {NULL
, NULL
, 0, NULL
, NULL
}
1091 status
= libnetapi_net_init(&c
->netapi_ctx
);
1095 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1096 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1097 if (c
->opt_kerberos
) {
1098 libnetapi_set_use_kerberos(c
->netapi_ctx
);
1102 if (c
->display_usage
) {
1107 _("List all users"));
1108 net_display_usage_from_functable(func
);
1112 return rpc_user_list(c
, argc
, argv
);
1115 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
1118 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1119 TALLOC_CTX
*mem_ctx
,
1120 struct rpc_sh_ctx
*ctx
,
1121 struct rpc_pipe_client
*pipe_hnd
,
1122 int argc
, const char **argv
)
1124 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
1127 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1128 TALLOC_CTX
*mem_ctx
,
1129 struct rpc_sh_ctx
*ctx
,
1130 struct rpc_pipe_client
*pipe_hnd
,
1131 int argc
, const char **argv
)
1133 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
1136 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1137 TALLOC_CTX
*mem_ctx
,
1138 struct rpc_sh_ctx
*ctx
,
1139 struct rpc_pipe_client
*pipe_hnd
,
1140 int argc
, const char **argv
,
1142 struct net_context
*c
,
1143 TALLOC_CTX
*mem_ctx
,
1144 struct rpc_sh_ctx
*ctx
,
1145 struct rpc_pipe_client
*pipe_hnd
,
1146 struct policy_handle
*user_hnd
,
1147 int argc
, const char **argv
))
1149 struct policy_handle connect_pol
, domain_pol
, user_pol
;
1150 NTSTATUS status
, result
;
1153 enum lsa_SidType type
;
1154 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1157 d_fprintf(stderr
, "%s %s <username>\n", _("Usage:"),
1159 return NT_STATUS_INVALID_PARAMETER
;
1162 ZERO_STRUCT(connect_pol
);
1163 ZERO_STRUCT(domain_pol
);
1164 ZERO_STRUCT(user_pol
);
1166 status
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1167 argv
[0], NULL
, NULL
, &sid
, &type
);
1168 if (!NT_STATUS_IS_OK(status
)) {
1169 d_fprintf(stderr
, _("Could not lookup %s: %s\n"), argv
[0],
1174 if (type
!= SID_NAME_USER
) {
1175 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
1176 sid_type_lookup(type
));
1177 status
= NT_STATUS_NO_SUCH_USER
;
1181 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1182 d_fprintf(stderr
, _("%s is not in our domain\n"), argv
[0]);
1183 status
= NT_STATUS_NO_SUCH_USER
;
1187 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1189 MAXIMUM_ALLOWED_ACCESS
,
1192 if (!NT_STATUS_IS_OK(status
)) {
1195 if (!NT_STATUS_IS_OK(result
)) {
1200 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1202 MAXIMUM_ALLOWED_ACCESS
,
1206 if (!NT_STATUS_IS_OK(status
)) {
1209 if (!NT_STATUS_IS_OK(result
)) {
1214 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1216 MAXIMUM_ALLOWED_ACCESS
,
1220 if (!NT_STATUS_IS_OK(status
)) {
1223 if (!NT_STATUS_IS_OK(result
)) {
1228 status
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1231 if (is_valid_policy_hnd(&user_pol
)) {
1232 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1234 if (is_valid_policy_hnd(&domain_pol
)) {
1235 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1237 if (is_valid_policy_hnd(&connect_pol
)) {
1238 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
1243 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1244 TALLOC_CTX
*mem_ctx
,
1245 struct rpc_sh_ctx
*ctx
,
1246 struct rpc_pipe_client
*pipe_hnd
,
1247 struct policy_handle
*user_hnd
,
1248 int argc
, const char **argv
)
1250 NTSTATUS status
, result
;
1251 union samr_UserInfo
*info
= NULL
;
1252 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1255 d_fprintf(stderr
, "%s %s show <username>\n", _("Usage:"),
1257 return NT_STATUS_INVALID_PARAMETER
;
1260 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1265 if (!NT_STATUS_IS_OK(status
)) {
1268 if (!NT_STATUS_IS_OK(result
)) {
1272 d_printf(_("user rid: %d, group rid: %d\n"),
1274 info
->info21
.primary_gid
);
1279 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1280 TALLOC_CTX
*mem_ctx
,
1281 struct rpc_sh_ctx
*ctx
,
1282 struct rpc_pipe_client
*pipe_hnd
,
1283 int argc
, const char **argv
)
1285 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1286 rpc_sh_user_show_internals
);
1289 #define FETCHSTR(name, rec) \
1290 do { if (strequal(ctx->thiscmd, name)) { \
1291 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1294 #define SETSTR(name, rec, flag) \
1295 do { if (strequal(ctx->thiscmd, name)) { \
1296 init_lsa_String(&(info->info21.rec), argv[0]); \
1297 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1300 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1301 TALLOC_CTX
*mem_ctx
,
1302 struct rpc_sh_ctx
*ctx
,
1303 struct rpc_pipe_client
*pipe_hnd
,
1304 struct policy_handle
*user_hnd
,
1305 int argc
, const char **argv
)
1307 NTSTATUS status
, result
;
1308 const char *username
;
1309 const char *oldval
= "";
1310 union samr_UserInfo
*info
= NULL
;
1311 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1314 d_fprintf(stderr
, "%s %s <username> [new value|NULL]\n",
1315 _("Usage:"), ctx
->whoami
);
1316 return NT_STATUS_INVALID_PARAMETER
;
1319 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1324 if (!NT_STATUS_IS_OK(status
)) {
1327 if (!NT_STATUS_IS_OK(result
)) {
1331 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1333 FETCHSTR("fullname", full_name
);
1334 FETCHSTR("homedir", home_directory
);
1335 FETCHSTR("homedrive", home_drive
);
1336 FETCHSTR("logonscript", logon_script
);
1337 FETCHSTR("profilepath", profile_path
);
1338 FETCHSTR("description", description
);
1341 d_printf(_("%s's %s: [%s]\n"), username
, ctx
->thiscmd
, oldval
);
1345 if (strcmp(argv
[0], "NULL") == 0) {
1349 ZERO_STRUCT(info
->info21
);
1351 SETSTR("fullname", full_name
, FULL_NAME
);
1352 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1353 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1354 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1355 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1356 SETSTR("description", description
, DESCRIPTION
);
1358 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1363 if (!NT_STATUS_IS_OK(status
)) {
1369 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username
,
1370 ctx
->thiscmd
, oldval
, argv
[0]);
1377 #define HANDLEFLG(name, rec) \
1378 do { if (strequal(ctx->thiscmd, name)) { \
1379 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1381 newflags = oldflags | ACB_##rec; \
1383 newflags = oldflags & ~ACB_##rec; \
1386 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1387 TALLOC_CTX
*mem_ctx
,
1388 struct rpc_sh_ctx
*ctx
,
1389 struct rpc_pipe_client
*pipe_hnd
,
1390 int argc
, const char **argv
)
1392 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1393 rpc_sh_user_str_edit_internals
);
1396 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1397 TALLOC_CTX
*mem_ctx
,
1398 struct rpc_sh_ctx
*ctx
,
1399 struct rpc_pipe_client
*pipe_hnd
,
1400 struct policy_handle
*user_hnd
,
1401 int argc
, const char **argv
)
1403 NTSTATUS status
, result
;
1404 const char *username
;
1405 const char *oldval
= "unknown";
1406 uint32 oldflags
, newflags
;
1408 union samr_UserInfo
*info
= NULL
;
1409 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1412 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1413 !strequal(argv
[0], "no"))) {
1414 /* TRANSATORS: The yes|no here are program keywords. Please do
1416 d_fprintf(stderr
, _("Usage: %s <username> [yes|no]\n"),
1418 return NT_STATUS_INVALID_PARAMETER
;
1421 newval
= strequal(argv
[0], "yes");
1423 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1428 if (!NT_STATUS_IS_OK(status
)) {
1431 if (!NT_STATUS_IS_OK(result
)) {
1435 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1436 oldflags
= info
->info21
.acct_flags
;
1437 newflags
= info
->info21
.acct_flags
;
1439 HANDLEFLG("disabled", DISABLED
);
1440 HANDLEFLG("pwnotreq", PWNOTREQ
);
1441 HANDLEFLG("autolock", AUTOLOCK
);
1442 HANDLEFLG("pwnoexp", PWNOEXP
);
1445 d_printf(_("%s's %s flag: %s\n"), username
, ctx
->thiscmd
,
1450 ZERO_STRUCT(info
->info21
);
1452 info
->info21
.acct_flags
= newflags
;
1453 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1455 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1460 if (!NT_STATUS_IS_OK(status
)) {
1464 if (NT_STATUS_IS_OK(result
)) {
1465 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username
,
1466 ctx
->thiscmd
, oldval
, argv
[0]);
1474 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1475 TALLOC_CTX
*mem_ctx
,
1476 struct rpc_sh_ctx
*ctx
,
1477 struct rpc_pipe_client
*pipe_hnd
,
1478 int argc
, const char **argv
)
1480 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1481 rpc_sh_user_flag_edit_internals
);
1484 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1485 TALLOC_CTX
*mem_ctx
,
1486 struct rpc_sh_ctx
*ctx
)
1488 static struct rpc_sh_cmd cmds
[] = {
1490 { "fullname", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1491 N_("Show/Set a user's full name") },
1493 { "homedir", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1494 N_("Show/Set a user's home directory") },
1496 { "homedrive", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1497 N_("Show/Set a user's home drive") },
1499 { "logonscript", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1500 N_("Show/Set a user's logon script") },
1502 { "profilepath", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1503 N_("Show/Set a user's profile path") },
1505 { "description", NULL
, &ndr_table_samr
, rpc_sh_user_str_edit
,
1506 N_("Show/Set a user's description") },
1508 { "disabled", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1509 N_("Show/Set whether a user is disabled") },
1511 { "autolock", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1512 N_("Show/Set whether a user locked out") },
1514 { "pwnotreq", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1515 N_("Show/Set whether a user does not need a password") },
1517 { "pwnoexp", NULL
, &ndr_table_samr
, rpc_sh_user_flag_edit
,
1518 N_("Show/Set whether a user's password does not expire") },
1520 { NULL
, NULL
, 0, NULL
, NULL
}
1526 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1527 TALLOC_CTX
*mem_ctx
,
1528 struct rpc_sh_ctx
*ctx
)
1530 static struct rpc_sh_cmd cmds
[] = {
1532 { "list", NULL
, &ndr_table_samr
, rpc_sh_user_list
,
1533 N_("List available users") },
1535 { "info", NULL
, &ndr_table_samr
, rpc_sh_user_info
,
1536 N_("List the domain groups a user is member of") },
1538 { "show", NULL
, &ndr_table_samr
, rpc_sh_user_show
,
1539 N_("Show info about a user") },
1541 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1542 N_("Show/Modify a user's fields") },
1544 { NULL
, NULL
, 0, NULL
, NULL
}
1550 /****************************************************************************/
1553 * Basic usage function for 'net rpc group'.
1554 * @param argc Standard main() style argc.
1555 * @param argv Standard main() style argv. Initial components are already
1559 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1561 return net_group_usage(c
, argc
, argv
);
1565 * Delete group on a remote RPC server.
1567 * All parameters are provided by the run_rpc_command function, except for
1568 * argc, argv which are passed through.
1570 * @param domain_sid The domain sid acquired from the remote server.
1571 * @param cli A cli_state connected to the server.
1572 * @param mem_ctx Talloc context, destroyed on completion of the function.
1573 * @param argc Standard main() style argc.
1574 * @param argv Standard main() style argv. Initial components are already
1577 * @return Normal NTSTATUS return.
1580 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1581 const struct dom_sid
*domain_sid
,
1582 const char *domain_name
,
1583 struct cli_state
*cli
,
1584 struct rpc_pipe_client
*pipe_hnd
,
1585 TALLOC_CTX
*mem_ctx
,
1589 struct policy_handle connect_pol
, domain_pol
, group_pol
, user_pol
;
1590 bool group_is_primary
= false;
1591 NTSTATUS status
, result
;
1593 struct samr_RidAttrArray
*rids
= NULL
;
1596 /* struct samr_RidWithAttribute *user_gids; */
1597 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1599 struct samr_Ids group_rids
, name_types
;
1600 struct lsa_String lsa_acct_name
;
1601 union samr_UserInfo
*info
= NULL
;
1603 if (argc
< 1 || c
->display_usage
) {
1604 rpc_group_usage(c
, argc
,argv
);
1605 return NT_STATUS_OK
; /* ok? */
1608 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1610 MAXIMUM_ALLOWED_ACCESS
,
1613 if (!NT_STATUS_IS_OK(status
)) {
1614 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1618 if (!NT_STATUS_IS_OK(result
)) {
1620 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1624 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1626 MAXIMUM_ALLOWED_ACCESS
,
1627 discard_const_p(struct dom_sid2
, domain_sid
),
1630 if (!NT_STATUS_IS_OK(status
)) {
1631 d_fprintf(stderr
, _("Request open_domain failed\n"));
1635 if (!NT_STATUS_IS_OK(result
)) {
1637 d_fprintf(stderr
, _("Request open_domain failed\n"));
1641 init_lsa_String(&lsa_acct_name
, argv
[0]);
1643 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1650 if (!NT_STATUS_IS_OK(status
)) {
1651 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1655 if (!NT_STATUS_IS_OK(result
)) {
1657 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1661 switch (name_types
.ids
[0])
1663 case SID_NAME_DOM_GRP
:
1664 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
1666 MAXIMUM_ALLOWED_ACCESS
,
1670 if (!NT_STATUS_IS_OK(status
)) {
1671 d_fprintf(stderr
, _("Request open_group failed"));
1675 if (!NT_STATUS_IS_OK(result
)) {
1677 d_fprintf(stderr
, _("Request open_group failed"));
1681 group_rid
= group_rids
.ids
[0];
1683 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
1687 if (!NT_STATUS_IS_OK(status
)) {
1689 _("Unable to query group members of %s"),
1694 if (!NT_STATUS_IS_OK(result
)) {
1697 _("Unable to query group members of %s"),
1702 if (c
->opt_verbose
) {
1704 _("Domain Group %s (rid: %d) has %d members\n"),
1705 argv
[0],group_rid
, rids
->count
);
1708 /* Check if group is anyone's primary group */
1709 for (i
= 0; i
< rids
->count
; i
++)
1711 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1713 MAXIMUM_ALLOWED_ACCESS
,
1717 if (!NT_STATUS_IS_OK(status
)) {
1719 _("Unable to open group member %d\n"),
1724 if (!NT_STATUS_IS_OK(result
)) {
1727 _("Unable to open group member %d\n"),
1732 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1737 if (!NT_STATUS_IS_OK(status
)) {
1739 _("Unable to lookup userinfo for group "
1745 if (!NT_STATUS_IS_OK(result
)) {
1748 _("Unable to lookup userinfo for group "
1754 if (info
->info21
.primary_gid
== group_rid
) {
1755 if (c
->opt_verbose
) {
1756 d_printf(_("Group is primary group "
1758 info
->info21
.account_name
.string
);
1760 group_is_primary
= true;
1763 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1766 if (group_is_primary
) {
1767 d_fprintf(stderr
, _("Unable to delete group because "
1768 "some of it's members have it as primary "
1770 status
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1774 /* remove all group members */
1775 for (i
= 0; i
< rids
->count
; i
++)
1778 d_printf(_("Remove group member %d..."),
1780 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
1784 if (!NT_STATUS_IS_OK(status
)) {
1788 if (NT_STATUS_IS_OK(result
)) {
1790 d_printf(_("ok\n"));
1793 d_printf("%s\n", _("failed"));
1798 status
= dcerpc_samr_DeleteDomainGroup(b
, mem_ctx
,
1801 if (!NT_STATUS_IS_OK(status
)) {
1808 /* removing a local group is easier... */
1809 case SID_NAME_ALIAS
:
1810 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
1812 MAXIMUM_ALLOWED_ACCESS
,
1816 if (!NT_STATUS_IS_OK(status
)) {
1817 d_fprintf(stderr
, _("Request open_alias failed\n"));
1820 if (!NT_STATUS_IS_OK(result
)) {
1822 d_fprintf(stderr
, _("Request open_alias failed\n"));
1826 status
= dcerpc_samr_DeleteDomAlias(b
, mem_ctx
,
1829 if (!NT_STATUS_IS_OK(status
)) {
1837 d_fprintf(stderr
, _("%s is of type %s. This command is only "
1838 "for deleting local or global groups\n"),
1839 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1840 status
= NT_STATUS_UNSUCCESSFUL
;
1844 if (NT_STATUS_IS_OK(status
)) {
1846 d_printf(_("Deleted %s '%s'\n"),
1847 sid_type_lookup(name_types
.ids
[0]), argv
[0]);
1849 d_fprintf(stderr
, _("Deleting of %s failed: %s\n"), argv
[0],
1850 get_friendly_nt_error_msg(status
));
1858 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1860 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
1861 rpc_group_delete_internals
, argc
,argv
);
1864 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1866 NET_API_STATUS status
;
1867 struct GROUP_INFO_1 info1
;
1868 uint32_t parm_error
= 0;
1870 if (argc
!= 1 || c
->display_usage
) {
1871 rpc_group_usage(c
, argc
, argv
);
1877 info1
.grpi1_name
= argv
[0];
1878 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1879 info1
.grpi1_comment
= c
->opt_comment
;
1882 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1886 _("Failed to add group '%s' with error: %s.\n"),
1887 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1891 d_printf(_("Added group '%s'.\n"), argv
[0]);
1897 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1899 NET_API_STATUS status
;
1900 struct LOCALGROUP_INFO_1 info1
;
1901 uint32_t parm_error
= 0;
1903 if (argc
!= 1 || c
->display_usage
) {
1904 rpc_group_usage(c
, argc
, argv
);
1910 info1
.lgrpi1_name
= argv
[0];
1911 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1912 info1
.lgrpi1_comment
= c
->opt_comment
;
1915 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1919 _("Failed to add alias '%s' with error: %s.\n"),
1920 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1924 d_printf(_("Added alias '%s'.\n"), argv
[0]);
1930 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1932 if (c
->opt_localgroup
)
1933 return rpc_alias_add_internals(c
, argc
, argv
);
1935 return rpc_group_add_internals(c
, argc
, argv
);
1938 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1939 TALLOC_CTX
*mem_ctx
,
1941 struct dom_sid
*sid
,
1942 enum lsa_SidType
*type
)
1944 struct dom_sid
*sids
= NULL
;
1945 enum lsa_SidType
*types
= NULL
;
1946 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1947 struct policy_handle lsa_pol
;
1948 NTSTATUS status
, result
;
1949 struct dcerpc_binding_handle
*b
;
1951 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1953 if (!NT_STATUS_IS_OK(status
)) {
1957 b
= pipe_hnd
->binding_handle
;
1959 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1960 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1962 if (!NT_STATUS_IS_OK(status
)) {
1966 status
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1967 &name
, NULL
, 1, &sids
, &types
);
1969 if (NT_STATUS_IS_OK(status
)) {
1970 sid_copy(sid
, &sids
[0]);
1974 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1978 TALLOC_FREE(pipe_hnd
);
1981 if (!NT_STATUS_IS_OK(status
) && (strncasecmp_m(name
, "S-", 2) == 0)) {
1983 /* Try as S-1-5-whatever */
1985 struct dom_sid tmp_sid
;
1987 if (string_to_sid(&tmp_sid
, name
)) {
1988 sid_copy(sid
, &tmp_sid
);
1989 *type
= SID_NAME_UNKNOWN
;
1990 status
= NT_STATUS_OK
;
1997 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
1998 TALLOC_CTX
*mem_ctx
,
1999 const struct dom_sid
*group_sid
,
2002 struct policy_handle connect_pol
, domain_pol
;
2003 NTSTATUS status
, result
;
2005 struct policy_handle group_pol
;
2006 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2008 struct samr_Ids rids
, rid_types
;
2009 struct lsa_String lsa_acct_name
;
2013 sid_copy(&sid
, group_sid
);
2015 if (!sid_split_rid(&sid
, &group_rid
)) {
2016 return NT_STATUS_UNSUCCESSFUL
;
2019 /* Get sam policy handle */
2020 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2022 MAXIMUM_ALLOWED_ACCESS
,
2025 if (!NT_STATUS_IS_OK(status
)) {
2028 if (!NT_STATUS_IS_OK(result
)) {
2032 /* Get domain policy handle */
2033 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2035 MAXIMUM_ALLOWED_ACCESS
,
2039 if (!NT_STATUS_IS_OK(status
)) {
2042 if (!NT_STATUS_IS_OK(result
)) {
2046 init_lsa_String(&lsa_acct_name
, member
);
2048 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2055 if (!NT_STATUS_IS_OK(status
)) {
2056 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2061 if (!NT_STATUS_IS_OK(result
)) {
2063 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2068 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2070 MAXIMUM_ALLOWED_ACCESS
,
2074 if (!NT_STATUS_IS_OK(status
)) {
2078 if (!NT_STATUS_IS_OK(result
)) {
2083 status
= dcerpc_samr_AddGroupMember(b
, mem_ctx
,
2086 0x0005, /* unknown flags */
2088 if (!NT_STATUS_IS_OK(status
)) {
2095 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2099 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2100 TALLOC_CTX
*mem_ctx
,
2101 const struct dom_sid
*alias_sid
,
2104 struct policy_handle connect_pol
, domain_pol
;
2105 NTSTATUS status
, result
;
2107 struct policy_handle alias_pol
;
2108 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2110 struct dom_sid member_sid
;
2111 enum lsa_SidType member_type
;
2115 sid_copy(&sid
, alias_sid
);
2117 if (!sid_split_rid(&sid
, &alias_rid
)) {
2118 return NT_STATUS_UNSUCCESSFUL
;
2121 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2122 member
, &member_sid
, &member_type
);
2124 if (!NT_STATUS_IS_OK(result
)) {
2125 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2130 /* Get sam policy handle */
2131 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2133 MAXIMUM_ALLOWED_ACCESS
,
2136 if (!NT_STATUS_IS_OK(status
)) {
2139 if (!NT_STATUS_IS_OK(result
)) {
2144 /* Get domain policy handle */
2145 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2147 MAXIMUM_ALLOWED_ACCESS
,
2151 if (!NT_STATUS_IS_OK(status
)) {
2154 if (!NT_STATUS_IS_OK(result
)) {
2159 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2161 MAXIMUM_ALLOWED_ACCESS
,
2165 if (!NT_STATUS_IS_OK(status
)) {
2168 if (!NT_STATUS_IS_OK(result
)) {
2172 status
= dcerpc_samr_AddAliasMember(b
, mem_ctx
,
2176 if (!NT_STATUS_IS_OK(status
)) {
2183 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2187 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
2188 const struct dom_sid
*domain_sid
,
2189 const char *domain_name
,
2190 struct cli_state
*cli
,
2191 struct rpc_pipe_client
*pipe_hnd
,
2192 TALLOC_CTX
*mem_ctx
,
2196 struct dom_sid group_sid
;
2197 enum lsa_SidType group_type
;
2199 if (argc
!= 2 || c
->display_usage
) {
2202 _("net rpc group addmem <group> <member>\n"
2203 " Add a member to a group\n"
2204 " group\tGroup to add member to\n"
2205 " member\tMember to add to group\n"));
2206 return NT_STATUS_UNSUCCESSFUL
;
2209 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2210 &group_sid
, &group_type
))) {
2211 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2213 return NT_STATUS_UNSUCCESSFUL
;
2216 if (group_type
== SID_NAME_DOM_GRP
) {
2217 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2218 &group_sid
, argv
[1]);
2220 if (!NT_STATUS_IS_OK(result
)) {
2221 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2222 argv
[1], argv
[0], nt_errstr(result
));
2227 if (group_type
== SID_NAME_ALIAS
) {
2228 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2229 &group_sid
, argv
[1]);
2231 if (!NT_STATUS_IS_OK(result
)) {
2232 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2233 argv
[1], argv
[0], nt_errstr(result
));
2238 d_fprintf(stderr
, _("Can only add members to global or local groups "
2239 "which %s is not\n"), argv
[0]);
2241 return NT_STATUS_UNSUCCESSFUL
;
2244 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2246 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2247 rpc_group_addmem_internals
,
2251 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2252 struct rpc_pipe_client
*pipe_hnd
,
2253 TALLOC_CTX
*mem_ctx
,
2254 const struct dom_sid
*group_sid
,
2257 struct policy_handle connect_pol
, domain_pol
;
2258 NTSTATUS status
, result
;
2260 struct policy_handle group_pol
;
2261 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2263 struct samr_Ids rids
, rid_types
;
2264 struct lsa_String lsa_acct_name
;
2268 sid_copy(&sid
, group_sid
);
2270 if (!sid_split_rid(&sid
, &group_rid
))
2271 return NT_STATUS_UNSUCCESSFUL
;
2273 /* Get sam policy handle */
2274 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2276 MAXIMUM_ALLOWED_ACCESS
,
2279 if (!NT_STATUS_IS_OK(status
)) {
2282 if (!NT_STATUS_IS_OK(result
)) {
2287 /* Get domain policy handle */
2288 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2290 MAXIMUM_ALLOWED_ACCESS
,
2294 if (!NT_STATUS_IS_OK(status
)) {
2297 if (!NT_STATUS_IS_OK(result
)) {
2301 init_lsa_String(&lsa_acct_name
, member
);
2303 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2310 if (!NT_STATUS_IS_OK(status
)) {
2311 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2316 if (!NT_STATUS_IS_OK(result
)) {
2318 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2323 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2325 MAXIMUM_ALLOWED_ACCESS
,
2329 if (!NT_STATUS_IS_OK(status
)) {
2332 if (!NT_STATUS_IS_OK(result
)) {
2337 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
2341 if (!NT_STATUS_IS_OK(status
)) {
2347 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2351 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2352 TALLOC_CTX
*mem_ctx
,
2353 const struct dom_sid
*alias_sid
,
2356 struct policy_handle connect_pol
, domain_pol
;
2357 NTSTATUS status
, result
;
2359 struct policy_handle alias_pol
;
2360 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2362 struct dom_sid member_sid
;
2363 enum lsa_SidType member_type
;
2367 sid_copy(&sid
, alias_sid
);
2369 if (!sid_split_rid(&sid
, &alias_rid
))
2370 return NT_STATUS_UNSUCCESSFUL
;
2372 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2373 member
, &member_sid
, &member_type
);
2375 if (!NT_STATUS_IS_OK(result
)) {
2376 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2381 /* Get sam policy handle */
2382 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2384 MAXIMUM_ALLOWED_ACCESS
,
2387 if (!NT_STATUS_IS_OK(status
)) {
2390 if (!NT_STATUS_IS_OK(result
)) {
2395 /* Get domain policy handle */
2396 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2398 MAXIMUM_ALLOWED_ACCESS
,
2402 if (!NT_STATUS_IS_OK(status
)) {
2405 if (!NT_STATUS_IS_OK(result
)) {
2410 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2412 MAXIMUM_ALLOWED_ACCESS
,
2416 if (!NT_STATUS_IS_OK(status
)) {
2420 if (!NT_STATUS_IS_OK(result
)) {
2424 status
= dcerpc_samr_DeleteAliasMember(b
, mem_ctx
,
2429 if (!NT_STATUS_IS_OK(status
)) {
2436 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2440 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2441 const struct dom_sid
*domain_sid
,
2442 const char *domain_name
,
2443 struct cli_state
*cli
,
2444 struct rpc_pipe_client
*pipe_hnd
,
2445 TALLOC_CTX
*mem_ctx
,
2449 struct dom_sid group_sid
;
2450 enum lsa_SidType group_type
;
2452 if (argc
!= 2 || c
->display_usage
) {
2455 _("net rpc group delmem <group> <member>\n"
2456 " Delete a member from a group\n"
2457 " group\tGroup to delete member from\n"
2458 " member\tMember to delete from group\n"));
2459 return NT_STATUS_UNSUCCESSFUL
;
2462 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2463 &group_sid
, &group_type
))) {
2464 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2466 return NT_STATUS_UNSUCCESSFUL
;
2469 if (group_type
== SID_NAME_DOM_GRP
) {
2470 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2471 &group_sid
, argv
[1]);
2473 if (!NT_STATUS_IS_OK(result
)) {
2474 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2475 argv
[1], argv
[0], nt_errstr(result
));
2480 if (group_type
== SID_NAME_ALIAS
) {
2481 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2482 &group_sid
, argv
[1]);
2484 if (!NT_STATUS_IS_OK(result
)) {
2485 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2486 argv
[1], argv
[0], nt_errstr(result
));
2491 d_fprintf(stderr
, _("Can only delete members from global or local "
2492 "groups which %s is not\n"), argv
[0]);
2494 return NT_STATUS_UNSUCCESSFUL
;
2497 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2499 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2500 rpc_group_delmem_internals
,
2505 * List groups on a remote RPC server.
2507 * All parameters are provided by the run_rpc_command function, except for
2508 * argc, argv which are passes through.
2510 * @param domain_sid The domain sid acquired from the remote server.
2511 * @param cli A cli_state connected to the server.
2512 * @param mem_ctx Talloc context, destroyed on completion of the function.
2513 * @param argc Standard main() style argc.
2514 * @param argv Standard main() style argv. Initial components are already
2517 * @return Normal NTSTATUS return.
2520 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2521 const struct dom_sid
*domain_sid
,
2522 const char *domain_name
,
2523 struct cli_state
*cli
,
2524 struct rpc_pipe_client
*pipe_hnd
,
2525 TALLOC_CTX
*mem_ctx
,
2529 struct policy_handle connect_pol
, domain_pol
;
2530 NTSTATUS status
, result
;
2531 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2532 struct samr_SamArray
*groups
= NULL
;
2533 bool global
= false;
2535 bool builtin
= false;
2536 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2538 if (c
->display_usage
) {
2541 _("net rpc group list [global] [local] [builtin]\n"
2542 " List groups on RPC server\n"
2543 " global\tList global groups\n"
2544 " local\tList local groups\n"
2545 " builtin\tList builtin groups\n"
2546 " If none of global, local or builtin is "
2547 "specified, all three options are considered "
2549 return NT_STATUS_OK
;
2558 for (i
=0; i
<argc
; i
++) {
2559 if (strequal(argv
[i
], "global"))
2562 if (strequal(argv
[i
], "local"))
2565 if (strequal(argv
[i
], "builtin"))
2569 /* Get sam policy handle */
2571 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2573 MAXIMUM_ALLOWED_ACCESS
,
2576 if (!NT_STATUS_IS_OK(status
)) {
2579 if (!NT_STATUS_IS_OK(result
)) {
2584 /* Get domain policy handle */
2586 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2588 MAXIMUM_ALLOWED_ACCESS
,
2589 discard_const_p(struct dom_sid2
, domain_sid
),
2592 if (!NT_STATUS_IS_OK(status
)) {
2595 if (!NT_STATUS_IS_OK(result
)) {
2600 /* Query domain groups */
2601 if (c
->opt_long_list_entries
)
2602 d_printf(_("\nGroup name Comment"
2603 "\n-----------------------------\n"));
2605 uint32_t max_size
, total_size
, returned_size
;
2606 union samr_DispInfo info
;
2610 dcerpc_get_query_dispinfo_params(
2611 loop_count
, &max_entries
, &max_size
);
2613 status
= dcerpc_samr_QueryDisplayInfo(b
, mem_ctx
,
2623 if (!NT_STATUS_IS_OK(status
)) {
2626 num_entries
= info
.info3
.count
;
2627 start_idx
+= info
.info3
.count
;
2629 if (!NT_STATUS_IS_OK(result
) &&
2630 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2633 for (i
= 0; i
< num_entries
; i
++) {
2635 const char *group
= NULL
;
2636 const char *desc
= NULL
;
2638 group
= info
.info3
.entries
[i
].account_name
.string
;
2639 desc
= info
.info3
.entries
[i
].description
.string
;
2641 if (c
->opt_long_list_entries
)
2642 printf("%-21.21s %-50.50s\n",
2645 printf("%s\n", group
);
2647 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2648 /* query domain aliases */
2653 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2660 if (!NT_STATUS_IS_OK(status
)) {
2663 if (!NT_STATUS_IS_OK(result
) &&
2664 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2667 for (i
= 0; i
< num_entries
; i
++) {
2669 const char *description
= NULL
;
2671 if (c
->opt_long_list_entries
) {
2673 struct policy_handle alias_pol
;
2674 union samr_AliasInfo
*info
= NULL
;
2677 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2680 groups
->entries
[i
].idx
,
2683 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2684 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2689 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2690 status
= dcerpc_samr_Close(b
, mem_ctx
,
2693 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2694 description
= info
->description
.string
;
2700 if (description
!= NULL
) {
2701 printf("%-21.21s %-50.50s\n",
2702 groups
->entries
[i
].name
.string
,
2705 printf("%s\n", groups
->entries
[i
].name
.string
);
2708 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2709 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
2710 /* Get builtin policy handle */
2712 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2714 MAXIMUM_ALLOWED_ACCESS
,
2715 discard_const_p(struct dom_sid2
, &global_sid_Builtin
),
2718 if (!NT_STATUS_IS_OK(status
)) {
2721 if (!NT_STATUS_IS_OK(result
)) {
2726 /* query builtin aliases */
2729 if (!builtin
) break;
2731 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2738 if (!NT_STATUS_IS_OK(status
)) {
2741 if (!NT_STATUS_IS_OK(result
) &&
2742 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
2747 for (i
= 0; i
< num_entries
; i
++) {
2749 const char *description
= NULL
;
2751 if (c
->opt_long_list_entries
) {
2753 struct policy_handle alias_pol
;
2754 union samr_AliasInfo
*info
= NULL
;
2757 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2760 groups
->entries
[i
].idx
,
2763 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2764 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2769 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2770 status
= dcerpc_samr_Close(b
, mem_ctx
,
2773 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2774 description
= info
->description
.string
;
2780 if (description
!= NULL
) {
2781 printf("%-21.21s %-50.50s\n",
2782 groups
->entries
[i
].name
.string
,
2785 printf("%s\n", groups
->entries
[i
].name
.string
);
2788 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2796 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2798 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
2799 rpc_group_list_internals
,
2803 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2804 struct rpc_pipe_client
*pipe_hnd
,
2805 TALLOC_CTX
*mem_ctx
,
2806 const char *domain_name
,
2807 const struct dom_sid
*domain_sid
,
2808 struct policy_handle
*domain_pol
,
2811 NTSTATUS result
, status
;
2812 struct policy_handle group_pol
;
2813 uint32 num_members
, *group_rids
;
2815 struct samr_RidAttrArray
*rids
= NULL
;
2816 struct lsa_Strings names
;
2817 struct samr_Ids types
;
2818 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2821 sid_to_fstring(sid_str
, domain_sid
);
2823 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2825 MAXIMUM_ALLOWED_ACCESS
,
2829 if (!NT_STATUS_IS_OK(status
)) {
2832 if (!NT_STATUS_IS_OK(result
)) {
2836 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
2840 if (!NT_STATUS_IS_OK(status
)) {
2843 if (!NT_STATUS_IS_OK(result
)) {
2847 num_members
= rids
->count
;
2848 group_rids
= rids
->rids
;
2850 while (num_members
> 0) {
2851 int this_time
= 512;
2853 if (num_members
< this_time
)
2854 this_time
= num_members
;
2856 status
= dcerpc_samr_LookupRids(b
, mem_ctx
,
2863 if (!NT_STATUS_IS_OK(status
)) {
2866 if (!NT_STATUS_IS_OK(result
)) {
2870 /* We only have users as members, but make the output
2871 the same as the output of alias members */
2873 for (i
= 0; i
< this_time
; i
++) {
2875 if (c
->opt_long_list_entries
) {
2876 printf("%s-%d %s\\%s %d\n", sid_str
,
2877 group_rids
[i
], domain_name
,
2878 names
.names
[i
].string
,
2881 printf("%s\\%s\n", domain_name
,
2882 names
.names
[i
].string
);
2886 num_members
-= this_time
;
2890 return NT_STATUS_OK
;
2893 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2894 struct rpc_pipe_client
*pipe_hnd
,
2895 TALLOC_CTX
*mem_ctx
,
2896 struct policy_handle
*domain_pol
,
2899 NTSTATUS result
, status
;
2900 struct rpc_pipe_client
*lsa_pipe
;
2901 struct policy_handle alias_pol
, lsa_pol
;
2903 struct dom_sid
*alias_sids
;
2906 enum lsa_SidType
*types
;
2908 struct lsa_SidArray sid_array
;
2909 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2911 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2913 MAXIMUM_ALLOWED_ACCESS
,
2917 if (!NT_STATUS_IS_OK(status
)) {
2920 if (!NT_STATUS_IS_OK(result
)) {
2924 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
2928 if (!NT_STATUS_IS_OK(status
)) {
2929 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2932 if (!NT_STATUS_IS_OK(result
)) {
2933 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2937 num_members
= sid_array
.num_sids
;
2939 if (num_members
== 0) {
2940 return NT_STATUS_OK
;
2943 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2944 &ndr_table_lsarpc
.syntax_id
,
2946 if (!NT_STATUS_IS_OK(result
)) {
2947 d_fprintf(stderr
, _("Couldn't open LSA pipe. Error was %s\n"),
2948 nt_errstr(result
) );
2952 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2953 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
2955 if (!NT_STATUS_IS_OK(result
)) {
2956 d_fprintf(stderr
, _("Couldn't open LSA policy handle\n"));
2957 TALLOC_FREE(lsa_pipe
);
2961 alias_sids
= talloc_zero_array(mem_ctx
, struct dom_sid
, num_members
);
2963 d_fprintf(stderr
, _("Out of memory\n"));
2964 TALLOC_FREE(lsa_pipe
);
2965 return NT_STATUS_NO_MEMORY
;
2968 for (i
=0; i
<num_members
; i
++) {
2969 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2972 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2973 num_members
, alias_sids
,
2974 &domains
, &names
, &types
);
2976 if (!NT_STATUS_IS_OK(result
) &&
2977 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2978 d_fprintf(stderr
, _("Couldn't lookup SIDs\n"));
2979 TALLOC_FREE(lsa_pipe
);
2983 for (i
= 0; i
< num_members
; i
++) {
2985 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2987 if (c
->opt_long_list_entries
) {
2988 printf("%s %s\\%s %d\n", sid_str
,
2989 domains
[i
] ? domains
[i
] : _("*unknown*"),
2990 names
[i
] ? names
[i
] : _("*unknown*"), types
[i
]);
2993 printf("%s\\%s\n", domains
[i
], names
[i
]);
2995 printf("%s\n", sid_str
);
2999 TALLOC_FREE(lsa_pipe
);
3000 return NT_STATUS_OK
;
3003 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
3004 const struct dom_sid
*domain_sid
,
3005 const char *domain_name
,
3006 struct cli_state
*cli
,
3007 struct rpc_pipe_client
*pipe_hnd
,
3008 TALLOC_CTX
*mem_ctx
,
3012 NTSTATUS result
, status
;
3013 struct policy_handle connect_pol
, domain_pol
;
3014 struct samr_Ids rids
, rid_types
;
3015 struct lsa_String lsa_acct_name
;
3016 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3018 /* Get sam policy handle */
3020 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
3022 MAXIMUM_ALLOWED_ACCESS
,
3025 if (!NT_STATUS_IS_OK(status
)) {
3028 if (!NT_STATUS_IS_OK(result
)) {
3032 /* Get domain policy handle */
3034 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3036 MAXIMUM_ALLOWED_ACCESS
,
3037 discard_const_p(struct dom_sid2
, domain_sid
),
3040 if (!NT_STATUS_IS_OK(status
)) {
3043 if (!NT_STATUS_IS_OK(result
)) {
3047 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
3049 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3056 if (!NT_STATUS_IS_OK(status
)) {
3060 if (!NT_STATUS_IS_OK(result
)) {
3062 /* Ok, did not find it in the global sam, try with builtin */
3064 struct dom_sid sid_Builtin
;
3066 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
3068 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
3070 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3072 MAXIMUM_ALLOWED_ACCESS
,
3076 if (!NT_STATUS_IS_OK(status
)) {
3079 if (!NT_STATUS_IS_OK(result
)) {
3080 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3085 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3092 if (!NT_STATUS_IS_OK(status
)) {
3095 if (!NT_STATUS_IS_OK(result
)) {
3096 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3102 if (rids
.count
!= 1) {
3103 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3108 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
3109 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
3110 domain_sid
, &domain_pol
,
3114 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
3115 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
3119 return NT_STATUS_NO_SUCH_GROUP
;
3122 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
3124 if (argc
!= 1 || c
->display_usage
) {
3125 return rpc_group_usage(c
, argc
, argv
);
3128 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
3129 rpc_group_members_internals
,
3133 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
3135 NET_API_STATUS status
;
3136 struct GROUP_INFO_0 g0
;
3140 d_printf(_("Usage:\n"));
3141 d_printf("net rpc group rename group newname\n");
3145 g0
.grpi0_name
= argv
[1];
3147 status
= NetGroupSetInfo(c
->opt_host
,
3154 d_fprintf(stderr
, _("Renaming group %s failed with: %s\n"),
3155 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
3163 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
3165 if (argc
!= 2 || c
->display_usage
) {
3166 return rpc_group_usage(c
, argc
, argv
);
3169 return rpc_group_rename_internals(c
, argc
, argv
);
3173 * 'net rpc group' entrypoint.
3174 * @param argc Standard main() style argc.
3175 * @param argv Standard main() style argv. Initial components are already
3179 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
3181 NET_API_STATUS status
;
3183 struct functable func
[] = {
3188 N_("Create specified group"),
3189 N_("net rpc group add\n"
3190 " Create specified group")
3196 N_("Delete specified group"),
3197 N_("net rpc group delete\n"
3198 " Delete specified group")
3204 N_("Add member to group"),
3205 N_("net rpc group addmem\n"
3206 " Add member to group")
3212 N_("Remove member from group"),
3213 N_("net rpc group delmem\n"
3214 " Remove member from group")
3221 N_("net rpc group list\n"
3228 N_("List group members"),
3229 N_("net rpc group members\n"
3230 " List group members")
3237 N_("net rpc group rename\n"
3240 {NULL
, NULL
, 0, NULL
, NULL
}
3243 status
= libnetapi_net_init(&c
->netapi_ctx
);
3247 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
3248 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
3249 if (c
->opt_kerberos
) {
3250 libnetapi_set_use_kerberos(c
->netapi_ctx
);
3254 if (c
->display_usage
) {
3255 d_printf(_("Usage:\n"));
3256 d_printf(_("net rpc group\n"
3257 " Alias for net rpc group list global "
3258 "local builtin\n"));
3259 net_display_usage_from_functable(func
);
3263 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
3264 rpc_group_list_internals
,
3268 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
3271 /****************************************************************************/
3273 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
3275 return net_share_usage(c
, argc
, argv
);
3279 * Add a share on a remote RPC server.
3281 * @param argc Standard main() style argc.
3282 * @param argv Standard main() style argv. Initial components are already
3285 * @return A shell status integer (0 for success).
3288 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
3290 NET_API_STATUS status
;
3293 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3294 uint32 num_users
=0, perms
=0;
3295 char *password
=NULL
; /* don't allow a share password */
3296 struct SHARE_INFO_2 i2
;
3297 uint32_t parm_error
= 0;
3299 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
3300 return rpc_share_usage(c
, argc
, argv
);
3303 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
3307 path
= strchr(sharename
, '=');
3314 i2
.shi2_netname
= sharename
;
3315 i2
.shi2_type
= type
;
3316 i2
.shi2_remark
= c
->opt_comment
;
3317 i2
.shi2_permissions
= perms
;
3318 i2
.shi2_max_uses
= c
->opt_maxusers
;
3319 i2
.shi2_current_uses
= num_users
;
3320 i2
.shi2_path
= path
;
3321 i2
.shi2_passwd
= password
;
3323 status
= NetShareAdd(c
->opt_host
,
3328 printf(_("NetShareAdd failed with: %s\n"),
3329 libnetapi_get_error_string(c
->netapi_ctx
, status
));
3336 * Delete a share on a remote RPC server.
3338 * @param domain_sid The domain sid acquired from the remote server.
3339 * @param argc Standard main() style argc.
3340 * @param argv Standard main() style argv. Initial components are already
3343 * @return A shell status integer (0 for success).
3345 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3347 if (argc
< 1 || c
->display_usage
) {
3348 return rpc_share_usage(c
, argc
, argv
);
3351 return NetShareDel(c
->opt_host
, argv
[0], 0);
3355 * Formatted print of share info
3357 * @param r pointer to SHARE_INFO_1 to format
3360 static void display_share_info_1(struct net_context
*c
,
3361 struct SHARE_INFO_1
*r
)
3363 if (c
->opt_long_list_entries
) {
3364 d_printf("%-12s %-8.8s %-50s\n",
3366 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
3369 d_printf("%s\n", r
->shi1_netname
);
3373 static WERROR
get_share_info(struct net_context
*c
,
3374 struct rpc_pipe_client
*pipe_hnd
,
3375 TALLOC_CTX
*mem_ctx
,
3379 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3383 union srvsvc_NetShareInfo info
;
3384 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3386 /* no specific share requested, enumerate all */
3389 uint32_t preferred_len
= 0xffffffff;
3390 uint32_t total_entries
= 0;
3391 uint32_t resume_handle
= 0;
3393 info_ctr
->level
= level
;
3395 status
= dcerpc_srvsvc_NetShareEnumAll(b
, mem_ctx
,
3402 if (!NT_STATUS_IS_OK(status
)) {
3403 return ntstatus_to_werror(status
);
3408 /* request just one share */
3409 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
3416 if (!NT_STATUS_IS_OK(status
)) {
3417 result
= ntstatus_to_werror(status
);
3421 if (!W_ERROR_IS_OK(result
)) {
3426 ZERO_STRUCTP(info_ctr
);
3428 info_ctr
->level
= level
;
3433 struct srvsvc_NetShareCtr1
*ctr1
;
3435 ctr1
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr1
);
3436 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3439 ctr1
->array
= info
.info1
;
3441 info_ctr
->ctr
.ctr1
= ctr1
;
3447 struct srvsvc_NetShareCtr2
*ctr2
;
3449 ctr2
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr2
);
3450 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3453 ctr2
->array
= info
.info2
;
3455 info_ctr
->ctr
.ctr2
= ctr2
;
3461 struct srvsvc_NetShareCtr502
*ctr502
;
3463 ctr502
= talloc_zero(mem_ctx
, struct srvsvc_NetShareCtr502
);
3464 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3467 ctr502
->array
= info
.info502
;
3469 info_ctr
->ctr
.ctr502
= ctr502
;
3479 * 'net rpc share list' entrypoint.
3480 * @param argc Standard main() style argc.
3481 * @param argv Standard main() style argv. Initial components are already
3484 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3486 NET_API_STATUS status
;
3487 struct SHARE_INFO_1
*i1
= NULL
;
3488 uint32_t entries_read
= 0;
3489 uint32_t total_entries
= 0;
3490 uint32_t resume_handle
= 0;
3491 uint32_t i
, level
= 1;
3493 if (c
->display_usage
) {
3495 "net rpc share list\n"
3498 _("List shares on remote server"));
3502 status
= NetShareEnum(c
->opt_host
,
3504 (uint8_t **)(void *)&i1
,
3513 /* Display results */
3515 if (c
->opt_long_list_entries
) {
3517 "\nEnumerating shared resources (exports) on remote server:\n\n"
3518 "\nShare name Type Description\n"
3519 "---------- ---- -----------\n"));
3521 for (i
= 0; i
< entries_read
; i
++)
3522 display_share_info_1(c
, &i1
[i
]);
3527 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3531 status
= cli_tree_connect(cli
, netname
, "A:", "", 0);
3532 if (!NT_STATUS_IS_OK(status
)) {
3533 d_printf(_("skipping [%s]: not a file share.\n"), netname
);
3537 status
= cli_tdis(cli
);
3538 if (!NT_STATUS_IS_OK(status
)) {
3539 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status
));
3546 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3547 const char *netname
, uint32 type
)
3549 /* only support disk shares */
3550 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3551 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname
,
3556 /* skip builtin shares */
3557 /* FIXME: should print$ be added too ? */
3558 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3559 strequal(netname
,"global"))
3562 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3563 printf(_("excluding [%s]\n"), netname
);
3567 return check_share_availability(cli
, netname
);
3571 * Migrate shares from a remote RPC server to the local RPC server.
3573 * All parameters are provided by the run_rpc_command function, except for
3574 * argc, argv which are passed through.
3576 * @param domain_sid The domain sid acquired from the remote server.
3577 * @param cli A cli_state connected to the server.
3578 * @param mem_ctx Talloc context, destroyed on completion of the function.
3579 * @param argc Standard main() style argc.
3580 * @param argv Standard main() style argv. Initial components are already
3583 * @return Normal NTSTATUS return.
3586 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3587 const struct dom_sid
*domain_sid
,
3588 const char *domain_name
,
3589 struct cli_state
*cli
,
3590 struct rpc_pipe_client
*pipe_hnd
,
3591 TALLOC_CTX
*mem_ctx
,
3596 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3597 struct srvsvc_NetShareInfoCtr ctr_src
;
3599 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3600 struct cli_state
*cli_dst
= NULL
;
3601 uint32 level
= 502; /* includes secdesc */
3602 uint32_t parm_error
= 0;
3603 struct dcerpc_binding_handle
*b
;
3605 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3607 if (!W_ERROR_IS_OK(result
))
3610 /* connect destination PI_SRVSVC */
3611 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3612 &ndr_table_srvsvc
.syntax_id
);
3613 if (!NT_STATUS_IS_OK(nt_status
))
3616 b
= srvsvc_pipe
->binding_handle
;
3618 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3620 union srvsvc_NetShareInfo info
;
3621 struct srvsvc_NetShareInfo502 info502
=
3622 ctr_src
.ctr
.ctr502
->array
[i
];
3624 /* reset error-code */
3625 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3627 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3630 /* finally add the share on the dst server */
3632 printf(_("migrating: [%s], path: %s, comment: %s, without "
3634 info502
.name
, info502
.path
, info502
.comment
);
3636 info
.info502
= &info502
;
3638 nt_status
= dcerpc_srvsvc_NetShareAdd(b
, mem_ctx
,
3639 srvsvc_pipe
->desthost
,
3644 if (!NT_STATUS_IS_OK(nt_status
)) {
3645 printf(_("cannot add share: %s\n"),
3646 nt_errstr(nt_status
));
3649 if (W_ERROR_V(result
) == W_ERROR_V(WERR_FILE_EXISTS
)) {
3650 printf(_(" [%s] does already exist\n"),
3655 if (!W_ERROR_IS_OK(result
)) {
3656 nt_status
= werror_to_ntstatus(result
);
3657 printf(_("cannot add share: %s\n"),
3658 win_errstr(result
));
3664 nt_status
= NT_STATUS_OK
;
3668 cli_shutdown(cli_dst
);
3676 * Migrate shares from a RPC server to another.
3678 * @param argc Standard main() style argc.
3679 * @param argv Standard main() style argv. Initial components are already
3682 * @return A shell status integer (0 for success).
3684 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3687 if (c
->display_usage
) {
3689 "net rpc share migrate shares\n"
3692 _("Migrate shares to local server"));
3697 printf(_("no server to migrate\n"));
3701 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
3702 rpc_share_migrate_shares_internals
,
3709 * @param f file_info
3710 * @param mask current search mask
3711 * @param state arg-pointer
3714 static NTSTATUS
copy_fn(const char *mnt
, struct file_info
*f
,
3715 const char *mask
, void *state
)
3717 static NTSTATUS nt_status
;
3718 static struct copy_clistate
*local_state
;
3719 static fstring filename
, new_mask
;
3722 struct net_context
*c
;
3724 local_state
= (struct copy_clistate
*)state
;
3725 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3729 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3730 return NT_STATUS_OK
;
3732 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3735 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
3737 DEBUG(3,("got dir: %s\n", f
->name
));
3739 fstrcpy(dir
, local_state
->cwd
);
3741 fstrcat(dir
, f
->name
);
3743 switch (net_mode_share
)
3745 case NET_MODE_SHARE_MIGRATE
:
3746 /* create that directory */
3747 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3748 local_state
->cli_share_src
,
3749 local_state
->cli_share_dst
,
3751 c
->opt_acls
? true : false,
3752 c
->opt_attrs
? true : false,
3753 c
->opt_timestamps
? true:false,
3757 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3758 return NT_STATUS_INTERNAL_ERROR
;
3761 if (!NT_STATUS_IS_OK(nt_status
)) {
3762 printf(_("could not handle dir %s: %s\n"),
3763 dir
, nt_errstr(nt_status
));
3767 /* search below that directory */
3768 if (strlcpy(new_mask
, dir
, sizeof(new_mask
)) >= sizeof(new_mask
)) {
3769 return NT_STATUS_NO_MEMORY
;
3771 if (strlcat(new_mask
, "\\*", sizeof(new_mask
)) >= sizeof(new_mask
)) {
3772 return NT_STATUS_NO_MEMORY
;
3775 old_dir
= local_state
->cwd
;
3776 local_state
->cwd
= dir
;
3777 nt_status
= sync_files(local_state
, new_mask
);
3778 if (!NT_STATUS_IS_OK(nt_status
)) {
3779 printf(_("could not handle files\n"));
3781 local_state
->cwd
= old_dir
;
3788 fstrcpy(filename
, local_state
->cwd
);
3789 fstrcat(filename
, "\\");
3790 fstrcat(filename
, f
->name
);
3792 DEBUG(3,("got file: %s\n", filename
));
3794 switch (net_mode_share
)
3796 case NET_MODE_SHARE_MIGRATE
:
3797 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3798 local_state
->cli_share_src
,
3799 local_state
->cli_share_dst
,
3801 c
->opt_acls
? true : false,
3802 c
->opt_attrs
? true : false,
3803 c
->opt_timestamps
? true: false,
3807 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3809 return NT_STATUS_INTERNAL_ERROR
;
3812 if (!NT_STATUS_IS_OK(nt_status
))
3813 printf(_("could not handle file %s: %s\n"),
3814 filename
, nt_errstr(nt_status
));
3819 * sync files, can be called recursivly to list files
3820 * and then call copy_fn for each file
3822 * @param cp_clistate pointer to the copy_clistate we work with
3823 * @param mask the current search mask
3825 * @return Boolean result
3827 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3829 struct cli_state
*targetcli
;
3830 char *targetpath
= NULL
;
3833 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3835 status
= cli_resolve_path(talloc_tos(), "", NULL
,
3836 cp_clistate
->cli_share_src
,
3837 mask
, &targetcli
, &targetpath
);
3838 if (!NT_STATUS_IS_OK(status
)) {
3839 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3841 mask
, nt_errstr(status
));
3845 status
= cli_list(targetcli
, targetpath
, cp_clistate
->attribute
,
3846 copy_fn
, cp_clistate
);
3847 if (!NT_STATUS_IS_OK(status
)) {
3848 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3849 mask
, nt_errstr(status
));
3857 * Set the top level directory permissions before we do any further copies.
3858 * Should set up ACL inheritance.
3861 bool copy_top_level_perms(struct net_context
*c
,
3862 struct copy_clistate
*cp_clistate
,
3863 const char *sharename
)
3865 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3867 switch (net_mode_share
) {
3868 case NET_MODE_SHARE_MIGRATE
:
3869 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3870 nt_status
= net_copy_fileattr(c
,
3871 cp_clistate
->mem_ctx
,
3872 cp_clistate
->cli_share_src
,
3873 cp_clistate
->cli_share_dst
,
3875 c
->opt_acls
? true : false,
3876 c
->opt_attrs
? true : false,
3877 c
->opt_timestamps
? true: false,
3881 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3885 if (!NT_STATUS_IS_OK(nt_status
)) {
3886 printf(_("Could handle directory attributes for top level "
3887 "directory of share %s. Error %s\n"),
3888 sharename
, nt_errstr(nt_status
));
3896 * Sync all files inside a remote share to another share (over smb).
3898 * All parameters are provided by the run_rpc_command function, except for
3899 * argc, argv which are passed through.
3901 * @param domain_sid The domain sid acquired from the remote server.
3902 * @param cli A cli_state connected to the server.
3903 * @param mem_ctx Talloc context, destroyed on completion of the function.
3904 * @param argc Standard main() style argc.
3905 * @param argv Standard main() style argv. Initial components are already
3908 * @return Normal NTSTATUS return.
3911 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3912 const struct dom_sid
*domain_sid
,
3913 const char *domain_name
,
3914 struct cli_state
*cli
,
3915 struct rpc_pipe_client
*pipe_hnd
,
3916 TALLOC_CTX
*mem_ctx
,
3921 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3922 struct srvsvc_NetShareInfoCtr ctr_src
;
3925 struct copy_clistate cp_clistate
;
3926 bool got_src_share
= false;
3927 bool got_dst_share
= false;
3928 const char *mask
= "\\*";
3931 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3933 nt_status
= NT_STATUS_NO_MEMORY
;
3937 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3940 if (!W_ERROR_IS_OK(result
))
3943 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3945 struct srvsvc_NetShareInfo502 info502
=
3946 ctr_src
.ctr
.ctr502
->array
[i
];
3948 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3951 /* one might not want to mirror whole discs :) */
3952 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3953 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3958 switch (net_mode_share
)
3960 case NET_MODE_SHARE_MIGRATE
:
3964 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3968 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3971 c
->opt_acls
? _("including") : _("without"),
3972 c
->opt_attrs
? _("including") : _("without"),
3973 c
->opt_timestamps
? _("(preserving timestamps)") : "");
3975 cp_clistate
.mem_ctx
= mem_ctx
;
3976 cp_clistate
.cli_share_src
= NULL
;
3977 cp_clistate
.cli_share_dst
= NULL
;
3978 cp_clistate
.cwd
= NULL
;
3979 cp_clistate
.attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
;
3982 /* open share source */
3983 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3984 cli_state_remote_sockaddr(cli
),
3985 cli_state_remote_name(cli
),
3986 info502
.name
, "A:");
3987 if (!NT_STATUS_IS_OK(nt_status
))
3990 got_src_share
= true;
3992 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3993 /* open share destination */
3994 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3995 NULL
, dst
, info502
.name
, "A:");
3996 if (!NT_STATUS_IS_OK(nt_status
))
3999 got_dst_share
= true;
4002 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
4003 d_fprintf(stderr
, _("Could not handle the top level "
4004 "directory permissions for the "
4005 "share: %s\n"), info502
.name
);
4006 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4010 nt_status
= sync_files(&cp_clistate
, mask
);
4011 if (!NT_STATUS_IS_OK(nt_status
)) {
4012 d_fprintf(stderr
, _("could not handle files for share: "
4013 "%s\n"), info502
.name
);
4018 nt_status
= NT_STATUS_OK
;
4023 cli_shutdown(cp_clistate
.cli_share_src
);
4026 cli_shutdown(cp_clistate
.cli_share_dst
);
4033 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
4035 if (c
->display_usage
) {
4037 "net share migrate files\n"
4040 _("Migrate files to local server"));
4045 d_printf(_("no server to migrate\n"));
4049 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4050 rpc_share_migrate_files_internals
,
4055 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4057 * All parameters are provided by the run_rpc_command function, except for
4058 * argc, argv which are passed through.
4060 * @param domain_sid The domain sid acquired from the remote server.
4061 * @param cli A cli_state connected to the server.
4062 * @param mem_ctx Talloc context, destroyed on completion of the function.
4063 * @param argc Standard main() style argc.
4064 * @param argv Standard main() style argv. Initial components are already
4067 * @return Normal NTSTATUS return.
4070 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
4071 const struct dom_sid
*domain_sid
,
4072 const char *domain_name
,
4073 struct cli_state
*cli
,
4074 struct rpc_pipe_client
*pipe_hnd
,
4075 TALLOC_CTX
*mem_ctx
,
4080 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
4081 struct srvsvc_NetShareInfoCtr ctr_src
;
4082 union srvsvc_NetShareInfo info
;
4084 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
4085 struct cli_state
*cli_dst
= NULL
;
4086 uint32 level
= 502; /* includes secdesc */
4087 uint32_t parm_error
= 0;
4088 struct dcerpc_binding_handle
*b
;
4090 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
4093 if (!W_ERROR_IS_OK(result
))
4096 /* connect destination PI_SRVSVC */
4097 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
4098 &ndr_table_srvsvc
.syntax_id
);
4099 if (!NT_STATUS_IS_OK(nt_status
))
4102 b
= srvsvc_pipe
->binding_handle
;
4104 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
4106 struct srvsvc_NetShareInfo502 info502
=
4107 ctr_src
.ctr
.ctr502
->array
[i
];
4109 /* reset error-code */
4110 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4112 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
4115 printf(_("migrating: [%s], path: %s, comment: %s, including "
4117 info502
.name
, info502
.path
, info502
.comment
);
4120 display_sec_desc(info502
.sd_buf
.sd
);
4122 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4123 info
.info502
= &info502
;
4125 /* finally modify the share on the dst server */
4126 nt_status
= dcerpc_srvsvc_NetShareSetInfo(b
, mem_ctx
,
4127 srvsvc_pipe
->desthost
,
4133 if (!NT_STATUS_IS_OK(nt_status
)) {
4134 printf(_("cannot set share-acl: %s\n"),
4135 nt_errstr(nt_status
));
4138 if (!W_ERROR_IS_OK(result
)) {
4139 nt_status
= werror_to_ntstatus(result
);
4140 printf(_("cannot set share-acl: %s\n"),
4141 win_errstr(result
));
4147 nt_status
= NT_STATUS_OK
;
4151 cli_shutdown(cli_dst
);
4159 * Migrate share-acls from a RPC server to another.
4161 * @param argc Standard main() style argc.
4162 * @param argv Standard main() style argv. Initial components are already
4165 * @return A shell status integer (0 for success).
4167 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
4170 if (c
->display_usage
) {
4172 "net rpc share migrate security\n"
4175 _("Migrate share-acls to local server"));
4180 d_printf(_("no server to migrate\n"));
4184 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4185 rpc_share_migrate_security_internals
,
4190 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4191 * from one server to another.
4193 * @param argc Standard main() style argc.
4194 * @param argv Standard main() style argv. Initial components are already
4197 * @return A shell status integer (0 for success).
4200 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
4205 if (c
->display_usage
) {
4207 "net rpc share migrate all\n"
4210 _("Migrates shares including all share settings"));
4215 d_printf(_("no server to migrate\n"));
4219 /* order is important. we don't want to be locked out by the share-acl
4220 * before copying files - gd */
4222 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4223 rpc_share_migrate_shares_internals
, argc
, argv
);
4227 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4228 rpc_share_migrate_files_internals
, argc
, argv
);
4232 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4233 rpc_share_migrate_security_internals
, argc
,
4239 * 'net rpc share migrate' entrypoint.
4240 * @param argc Standard main() style argc.
4241 * @param argv Standard main() style argv. Initial components are already
4244 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4247 struct functable func
[] = {
4250 rpc_share_migrate_all
,
4252 N_("Migrate shares from remote to local server"),
4253 N_("net rpc share migrate all\n"
4254 " Migrate shares from remote to local server")
4258 rpc_share_migrate_files
,
4260 N_("Migrate files from remote to local server"),
4261 N_("net rpc share migrate files\n"
4262 " Migrate files from remote to local server")
4266 rpc_share_migrate_security
,
4268 N_("Migrate share-ACLs from remote to local server"),
4269 N_("net rpc share migrate security\n"
4270 " Migrate share-ACLs from remote to local server")
4274 rpc_share_migrate_shares
,
4276 N_("Migrate shares from remote to local server"),
4277 N_("net rpc share migrate shares\n"
4278 " Migrate shares from remote to local server")
4280 {NULL
, NULL
, 0, NULL
, NULL
}
4283 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4285 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
4291 struct dom_sid
*members
;
4294 static int num_server_aliases
;
4295 static struct full_alias
*server_aliases
;
4298 * Add an alias to the static list.
4300 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4302 if (server_aliases
== NULL
)
4303 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4305 server_aliases
[num_server_aliases
] = *alias
;
4306 num_server_aliases
+= 1;
4310 * For a specific domain on the server, fetch all the aliases
4311 * and their members. Add all of them to the server_aliases.
4314 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4315 TALLOC_CTX
*mem_ctx
,
4316 struct policy_handle
*connect_pol
,
4317 const struct dom_sid
*domain_sid
)
4319 uint32 start_idx
, max_entries
, num_entries
, i
;
4320 struct samr_SamArray
*groups
= NULL
;
4321 NTSTATUS result
, status
;
4322 struct policy_handle domain_pol
;
4323 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4325 /* Get domain policy handle */
4327 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
4329 MAXIMUM_ALLOWED_ACCESS
,
4330 discard_const_p(struct dom_sid2
, domain_sid
),
4333 if (!NT_STATUS_IS_OK(status
)) {
4336 if (!NT_STATUS_IS_OK(result
)) {
4344 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
4351 if (!NT_STATUS_IS_OK(status
)) {
4354 for (i
= 0; i
< num_entries
; i
++) {
4356 struct policy_handle alias_pol
;
4357 struct full_alias alias
;
4358 struct lsa_SidArray sid_array
;
4362 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
4364 MAXIMUM_ALLOWED_ACCESS
,
4365 groups
->entries
[i
].idx
,
4368 if (!NT_STATUS_IS_OK(status
)) {
4371 if (!NT_STATUS_IS_OK(_result
)) {
4376 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
4380 if (!NT_STATUS_IS_OK(status
)) {
4383 if (!NT_STATUS_IS_OK(_result
)) {
4388 alias
.num_members
= sid_array
.num_sids
;
4390 status
= dcerpc_samr_Close(b
, mem_ctx
, &alias_pol
, &_result
);
4391 if (!NT_STATUS_IS_OK(status
)) {
4394 if (!NT_STATUS_IS_OK(_result
)) {
4399 alias
.members
= NULL
;
4401 if (alias
.num_members
> 0) {
4402 alias
.members
= SMB_MALLOC_ARRAY(struct dom_sid
, alias
.num_members
);
4404 for (j
= 0; j
< alias
.num_members
; j
++)
4405 sid_copy(&alias
.members
[j
],
4406 sid_array
.sids
[j
].sid
);
4409 sid_compose(&alias
.sid
, domain_sid
,
4410 groups
->entries
[i
].idx
);
4412 push_alias(mem_ctx
, &alias
);
4414 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4416 status
= NT_STATUS_OK
;
4419 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
4425 * Dump server_aliases as names for debugging purposes.
4428 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4429 const struct dom_sid
*domain_sid
,
4430 const char *domain_name
,
4431 struct cli_state
*cli
,
4432 struct rpc_pipe_client
*pipe_hnd
,
4433 TALLOC_CTX
*mem_ctx
,
4439 struct policy_handle lsa_pol
;
4440 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4442 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4443 SEC_FLAG_MAXIMUM_ALLOWED
,
4445 if (!NT_STATUS_IS_OK(result
))
4448 for (i
=0; i
<num_server_aliases
; i
++) {
4451 enum lsa_SidType
*types
;
4454 struct full_alias
*alias
= &server_aliases
[i
];
4456 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4458 &domains
, &names
, &types
);
4459 if (!NT_STATUS_IS_OK(result
))
4462 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4464 if (alias
->num_members
== 0) {
4469 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4472 &domains
, &names
, &types
);
4474 if (!NT_STATUS_IS_OK(result
) &&
4475 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4478 for (j
=0; j
<alias
->num_members
; j
++)
4479 DEBUG(1, ("%s\\%s (%d); ",
4480 domains
[j
] ? domains
[j
] : "*unknown*",
4481 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4485 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
4487 return NT_STATUS_OK
;
4491 * Fetch a list of all server aliases and their members into
4495 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4496 const struct dom_sid
*domain_sid
,
4497 const char *domain_name
,
4498 struct cli_state
*cli
,
4499 struct rpc_pipe_client
*pipe_hnd
,
4500 TALLOC_CTX
*mem_ctx
,
4504 NTSTATUS result
, status
;
4505 struct policy_handle connect_pol
;
4506 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4508 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
4510 MAXIMUM_ALLOWED_ACCESS
,
4513 if (!NT_STATUS_IS_OK(status
)) {
4516 if (!NT_STATUS_IS_OK(result
)) {
4521 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4522 &global_sid_Builtin
);
4523 if (!NT_STATUS_IS_OK(status
)) {
4527 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4530 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
4535 static void init_user_token(struct security_token
*token
, struct dom_sid
*user_sid
)
4537 token
->num_sids
= 4;
4539 if (!(token
->sids
= SMB_MALLOC_ARRAY(struct dom_sid
, 4))) {
4540 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4541 token
->num_sids
= 0;
4545 token
->sids
[0] = *user_sid
;
4546 sid_copy(&token
->sids
[1], &global_sid_World
);
4547 sid_copy(&token
->sids
[2], &global_sid_Network
);
4548 sid_copy(&token
->sids
[3], &global_sid_Authenticated_Users
);
4551 static void free_user_token(struct security_token
*token
)
4553 SAFE_FREE(token
->sids
);
4556 static void add_sid_to_token(struct security_token
*token
, struct dom_sid
*sid
)
4558 if (security_token_has_sid(token
, sid
))
4561 token
->sids
= SMB_REALLOC_ARRAY(token
->sids
, struct dom_sid
, token
->num_sids
+1);
4566 sid_copy(&token
->sids
[token
->num_sids
], sid
);
4568 token
->num_sids
+= 1;
4573 struct security_token token
;
4576 static void dump_user_token(struct user_token
*token
)
4580 d_printf("%s\n", token
->name
);
4582 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4583 d_printf(" %s\n", sid_string_tos(&token
->token
.sids
[i
]));
4587 static bool is_alias_member(struct dom_sid
*sid
, struct full_alias
*alias
)
4591 for (i
=0; i
<alias
->num_members
; i
++) {
4592 if (dom_sid_compare(sid
, &alias
->members
[i
]) == 0)
4599 static void collect_sid_memberships(struct security_token
*token
, struct dom_sid sid
)
4603 for (i
=0; i
<num_server_aliases
; i
++) {
4604 if (is_alias_member(&sid
, &server_aliases
[i
]))
4605 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4610 * We got a user token with all the SIDs we can know about without asking the
4611 * server directly. These are the user and domain group sids. All of these can
4612 * be members of aliases. So scan the list of aliases for each of the SIDs and
4613 * add them to the token.
4616 static void collect_alias_memberships(struct security_token
*token
)
4618 int num_global_sids
= token
->num_sids
;
4621 for (i
=0; i
<num_global_sids
; i
++) {
4622 collect_sid_memberships(token
, token
->sids
[i
]);
4626 static bool get_user_sids(const char *domain
, const char *user
, struct security_token
*token
)
4628 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4629 enum wbcSidType type
;
4631 struct wbcDomainSid wsid
;
4632 char sid_str
[WBC_SID_STRING_BUFLEN
];
4633 struct dom_sid user_sid
;
4634 uint32_t num_groups
;
4635 gid_t
*groups
= NULL
;
4638 fstr_sprintf(full_name
, "%s%c%s",
4639 domain
, *lp_winbind_separator(), user
);
4641 /* First let's find out the user sid */
4643 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4645 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4646 DEBUG(1, ("winbind could not find %s: %s\n",
4647 full_name
, wbcErrorString(wbc_status
)));
4651 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4653 if (type
!= WBC_SID_NAME_USER
) {
4654 DEBUG(1, ("%s is not a user\n", full_name
));
4658 if (!string_to_sid(&user_sid
, sid_str
)) {
4659 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4663 init_user_token(token
, &user_sid
);
4665 /* And now the groups winbind knows about */
4667 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4668 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4669 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4670 full_name
, wbcErrorString(wbc_status
)));
4674 for (i
= 0; i
< num_groups
; i
++) {
4675 gid_t gid
= groups
[i
];
4678 wbc_status
= wbcGidToSid(gid
, &wsid
);
4679 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4680 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4681 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4682 wbcFreeMemory(groups
);
4686 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4688 DEBUG(3, (" %s\n", sid_str
));
4690 string_to_sid(&sid
, sid_str
);
4691 add_sid_to_token(token
, &sid
);
4693 wbcFreeMemory(groups
);
4699 * Get a list of all user tokens we want to look at
4702 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4703 struct user_token
**user_tokens
)
4705 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4706 uint32_t i
, num_users
;
4708 struct user_token
*result
;
4709 TALLOC_CTX
*frame
= NULL
;
4711 if (lp_winbind_use_default_domain() &&
4712 (c
->opt_target_workgroup
== NULL
)) {
4713 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4714 "please specify a workgroup\n"));
4718 /* Send request to winbind daemon */
4720 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4721 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4722 DEBUG(1, (_("winbind could not list users: %s\n"),
4723 wbcErrorString(wbc_status
)));
4727 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4729 if (result
== NULL
) {
4730 DEBUG(1, ("Could not malloc sid array\n"));
4731 wbcFreeMemory(users
);
4735 frame
= talloc_stackframe();
4736 for (i
=0; i
< num_users
; i
++) {
4737 fstring domain
, user
;
4740 fstrcpy(result
[i
].name
, users
[i
]);
4742 p
= strchr(users
[i
], *lp_winbind_separator());
4744 DEBUG(3, ("%s\n", users
[i
]));
4747 fstrcpy(domain
, c
->opt_target_workgroup
);
4748 fstrcpy(user
, users
[i
]);
4751 fstrcpy(domain
, users
[i
]);
4756 get_user_sids(domain
, user
, &(result
[i
].token
));
4759 wbcFreeMemory(users
);
4761 *num_tokens
= num_users
;
4762 *user_tokens
= result
;
4767 static bool get_user_tokens_from_file(FILE *f
,
4769 struct user_token
**tokens
)
4771 struct user_token
*token
= NULL
;
4776 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4780 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4781 line
[strlen(line
)-1] = '\0';
4784 if (line
[0] == ' ') {
4788 if(!string_to_sid(&sid
, &line
[1])) {
4789 DEBUG(1,("get_user_tokens_from_file: Could "
4790 "not convert sid %s \n",&line
[1]));
4794 if (token
== NULL
) {
4795 DEBUG(0, ("File does not begin with username"));
4799 add_sid_to_token(&token
->token
, &sid
);
4803 /* And a new user... */
4806 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4807 if (*tokens
== NULL
) {
4808 DEBUG(0, ("Could not realloc tokens\n"));
4812 token
= &((*tokens
)[*num_tokens
-1]);
4814 if (strlcpy(token
->name
, line
, sizeof(token
->name
)) >= sizeof(token
->name
)) {
4817 token
->token
.num_sids
= 0;
4818 token
->token
.sids
= NULL
;
4827 * Show the list of all users that have access to a share
4830 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4831 TALLOC_CTX
*mem_ctx
,
4832 const char *netname
,
4834 struct user_token
*tokens
)
4837 struct security_descriptor
*share_sd
= NULL
;
4838 struct security_descriptor
*root_sd
= NULL
;
4839 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4841 union srvsvc_NetShareInfo info
;
4845 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4847 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
4854 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4855 DEBUG(1, ("Coult not query secdesc for share %s\n",
4860 share_sd
= info
.info502
->sd_buf
.sd
;
4861 if (share_sd
== NULL
) {
4862 DEBUG(1, ("Got no secdesc for share %s\n",
4866 cnum
= cli_state_get_tid(cli
);
4868 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, netname
, "A:", "", 0))) {
4872 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4873 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4874 cli_query_secdesc(cli
, fnum
, mem_ctx
, &root_sd
);
4877 for (i
=0; i
<num_tokens
; i
++) {
4880 if (share_sd
!= NULL
) {
4881 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4884 if (!NT_STATUS_IS_OK(status
)) {
4885 DEBUG(1, ("Could not check share_sd for "
4892 if (root_sd
== NULL
) {
4893 d_printf(" %s\n", tokens
[i
].name
);
4897 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4899 if (!NT_STATUS_IS_OK(status
)) {
4900 DEBUG(1, ("Could not check root_sd for user %s\n",
4904 d_printf(" %s\n", tokens
[i
].name
);
4907 if (fnum
!= (uint16_t)-1)
4908 cli_close(cli
, fnum
);
4910 cli_state_set_tid(cli
, cnum
);
4920 static void collect_share(const char *name
, uint32 m
,
4921 const char *comment
, void *state
)
4923 struct share_list
*share_list
= (struct share_list
*)state
;
4925 if (m
!= STYPE_DISKTREE
)
4928 share_list
->num_shares
+= 1;
4929 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4930 if (!share_list
->shares
) {
4931 share_list
->num_shares
= 0;
4934 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4938 * List shares on a remote RPC server, including the security descriptors.
4940 * All parameters are provided by the run_rpc_command function, except for
4941 * argc, argv which are passed through.
4943 * @param domain_sid The domain sid acquired from the remote server.
4944 * @param cli A cli_state connected to the server.
4945 * @param mem_ctx Talloc context, destroyed on completion of the function.
4946 * @param argc Standard main() style argc.
4947 * @param argv Standard main() style argv. Initial components are already
4950 * @return Normal NTSTATUS return.
4953 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4954 const struct dom_sid
*domain_sid
,
4955 const char *domain_name
,
4956 struct cli_state
*cli
,
4957 struct rpc_pipe_client
*pipe_hnd
,
4958 TALLOC_CTX
*mem_ctx
,
4967 struct user_token
*tokens
= NULL
;
4970 struct share_list share_list
;
4975 f
= fopen(argv
[0], "r");
4979 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4980 return NT_STATUS_UNSUCCESSFUL
;
4983 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4989 DEBUG(0, ("Could not read users from file\n"));
4990 return NT_STATUS_UNSUCCESSFUL
;
4993 for (i
=0; i
<num_tokens
; i
++)
4994 collect_alias_memberships(&tokens
[i
].token
);
4996 share_list
.num_shares
= 0;
4997 share_list
.shares
= NULL
;
4999 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
5002 DEBUG(0, ("Error returning browse list: %s\n",
5007 for (i
= 0; i
< share_list
.num_shares
; i
++) {
5008 char *netname
= share_list
.shares
[i
];
5010 if (netname
[strlen(netname
)-1] == '$')
5013 d_printf("%s\n", netname
);
5015 show_userlist(pipe_hnd
, mem_ctx
, netname
,
5016 num_tokens
, tokens
);
5019 for (i
=0; i
<num_tokens
; i
++) {
5020 free_user_token(&tokens
[i
].token
);
5023 SAFE_FREE(share_list
.shares
);
5025 return NT_STATUS_OK
;
5028 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
5033 if (c
->display_usage
) {
5035 "net rpc share allowedusers\n"
5038 _("List allowed users"));
5042 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5043 rpc_aliaslist_internals
,
5048 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
, 0,
5054 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
5055 rpc_share_allowedusers_internals
,
5059 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
5062 struct user_token
*tokens
= NULL
;
5066 net_usersidlist_usage(c
, argc
, argv
);
5070 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
5071 DEBUG(0, ("Could not get the user/sid list\n"));
5075 for (i
=0; i
<num_tokens
; i
++) {
5076 dump_user_token(&tokens
[i
]);
5077 free_user_token(&tokens
[i
].token
);
5084 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
5086 d_printf(_("net usersidlist\n"
5087 "\tprints out a list of all users the running winbind knows\n"
5088 "\tabout, together with all their SIDs. This is used as\n"
5089 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5091 net_common_flags_usage(c
, argc
, argv
);
5096 * 'net rpc share' entrypoint.
5097 * @param argc Standard main() style argc.
5098 * @param argv Standard main() style argv. Initial components are already
5102 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
5104 NET_API_STATUS status
;
5106 struct functable func
[] = {
5112 N_("net rpc share add\n"
5120 N_("net rpc share delete\n"
5125 rpc_share_allowedusers
,
5127 N_("Modify allowed users"),
5128 N_("net rpc share allowedusers\n"
5129 " Modify allowed users")
5135 N_("Migrate share to local server"),
5136 N_("net rpc share migrate\n"
5137 " Migrate share to local server")
5144 N_("net rpc share list\n"
5147 {NULL
, NULL
, 0, NULL
, NULL
}
5150 status
= libnetapi_net_init(&c
->netapi_ctx
);
5154 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5155 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5156 if (c
->opt_kerberos
) {
5157 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5161 if (c
->display_usage
) {
5166 " Alias for net rpc share list\n"));
5167 net_display_usage_from_functable(func
);
5171 return rpc_share_list(c
, argc
, argv
);
5174 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5177 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5178 TALLOC_CTX
*mem_ctx
,
5179 struct rpc_sh_ctx
*ctx
,
5180 struct rpc_pipe_client
*pipe_hnd
,
5181 int argc
, const char **argv
)
5184 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
5187 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
5188 TALLOC_CTX
*mem_ctx
,
5189 struct rpc_sh_ctx
*ctx
,
5190 struct rpc_pipe_client
*pipe_hnd
,
5191 int argc
, const char **argv
)
5193 NET_API_STATUS status
;
5194 uint32_t parm_err
= 0;
5195 struct SHARE_INFO_2 i2
;
5197 if ((argc
< 2) || (argc
> 3)) {
5198 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
5200 return NT_STATUS_INVALID_PARAMETER
;
5203 i2
.shi2_netname
= argv
[0];
5204 i2
.shi2_type
= STYPE_DISKTREE
;
5205 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
5206 i2
.shi2_permissions
= 0;
5207 i2
.shi2_max_uses
= 0;
5208 i2
.shi2_current_uses
= 0;
5209 i2
.shi2_path
= argv
[1];
5210 i2
.shi2_passwd
= NULL
;
5212 status
= NetShareAdd(pipe_hnd
->desthost
,
5217 return werror_to_ntstatus(W_ERROR(status
));
5220 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5221 TALLOC_CTX
*mem_ctx
,
5222 struct rpc_sh_ctx
*ctx
,
5223 struct rpc_pipe_client
*pipe_hnd
,
5224 int argc
, const char **argv
)
5227 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5228 return NT_STATUS_INVALID_PARAMETER
;
5231 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
5234 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5235 TALLOC_CTX
*mem_ctx
,
5236 struct rpc_sh_ctx
*ctx
,
5237 struct rpc_pipe_client
*pipe_hnd
,
5238 int argc
, const char **argv
)
5240 union srvsvc_NetShareInfo info
;
5243 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5246 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5247 return NT_STATUS_INVALID_PARAMETER
;
5250 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
5256 if (!NT_STATUS_IS_OK(status
)) {
5257 result
= ntstatus_to_werror(status
);
5260 if (!W_ERROR_IS_OK(result
)) {
5264 d_printf(_("Name: %s\n"), info
.info2
->name
);
5265 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
5266 d_printf(_("Path: %s\n"), info
.info2
->path
);
5267 d_printf(_("Password: %s\n"), info
.info2
->password
);
5270 return werror_to_ntstatus(result
);
5273 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5274 struct rpc_sh_ctx
*ctx
)
5276 static struct rpc_sh_cmd cmds
[] = {
5278 { "list", NULL
, &ndr_table_srvsvc
, rpc_sh_share_list
,
5279 N_("List available shares") },
5281 { "add", NULL
, &ndr_table_srvsvc
, rpc_sh_share_add
,
5282 N_("Add a share") },
5284 { "delete", NULL
, &ndr_table_srvsvc
, rpc_sh_share_delete
,
5285 N_("Delete a share") },
5287 { "info", NULL
, &ndr_table_srvsvc
, rpc_sh_share_info
,
5288 N_("Get information about a share") },
5290 { NULL
, NULL
, 0, NULL
, NULL
}
5296 /****************************************************************************/
5298 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5300 return net_file_usage(c
, argc
, argv
);
5304 * Close a file on a remote RPC server.
5306 * @param argc Standard main() style argc.
5307 * @param argv Standard main() style argv. Initial components are already
5310 * @return A shell status integer (0 for success).
5312 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5314 if (argc
< 1 || c
->display_usage
) {
5315 return rpc_file_usage(c
, argc
, argv
);
5318 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
5322 * Formatted print of open file info
5324 * @param r struct FILE_INFO_3 contents
5327 static void display_file_info_3(struct FILE_INFO_3
*r
)
5329 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5330 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
5331 r
->fi3_num_locks
, r
->fi3_pathname
);
5335 * List files for a user on a remote RPC server.
5337 * @param argc Standard main() style argc.
5338 * @param argv Standard main() style argv. Initial components are already
5341 * @return A shell status integer (0 for success)..
5344 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5346 NET_API_STATUS status
;
5347 uint32 preferred_len
= 0xffffffff, i
;
5348 const char *username
=NULL
;
5349 uint32_t total_entries
= 0;
5350 uint32_t entries_read
= 0;
5351 uint32_t resume_handle
= 0;
5352 struct FILE_INFO_3
*i3
= NULL
;
5354 if (c
->display_usage
) {
5355 return rpc_file_usage(c
, argc
, argv
);
5358 /* if argc > 0, must be user command */
5360 username
= smb_xstrdup(argv
[0]);
5363 status
= NetFileEnum(c
->opt_host
,
5367 (uint8_t **)(void *)&i3
,
5377 /* Display results */
5380 "\nEnumerating open files on remote server:\n\n"
5381 "\nFileId Opened by Perms Locks Path"
5382 "\n------ --------- ----- ----- ---- \n"));
5383 for (i
= 0; i
< entries_read
; i
++) {
5384 display_file_info_3(&i3
[i
]);
5391 * 'net rpc file' entrypoint.
5392 * @param argc Standard main() style argc.
5393 * @param argv Standard main() style argv. Initial components are already
5397 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5399 NET_API_STATUS status
;
5401 struct functable func
[] = {
5406 N_("Close opened file"),
5407 N_("net rpc file close\n"
5408 " Close opened file")
5414 N_("List files opened by user"),
5415 N_("net rpc file user\n"
5416 " List files opened by user")
5423 N_("Display information about opened file"),
5424 N_("net rpc file info\n"
5425 " Display information about opened file")
5428 {NULL
, NULL
, 0, NULL
, NULL
}
5431 status
= libnetapi_net_init(&c
->netapi_ctx
);
5435 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5436 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5437 if (c
->opt_kerberos
) {
5438 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5442 if (c
->display_usage
) {
5443 d_printf(_("Usage:\n"));
5444 d_printf(_("net rpc file\n"
5445 " List opened files\n"));
5446 net_display_usage_from_functable(func
);
5450 return rpc_file_user(c
, argc
, argv
);
5453 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5457 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5459 * All parameters are provided by the run_rpc_command function, except for
5460 * argc, argv which are passed through.
5462 * @param c A net_context structure.
5463 * @param domain_sid The domain sid acquired from the remote server.
5464 * @param cli A cli_state connected to the server.
5465 * @param mem_ctx Talloc context, destroyed on completion of the function.
5466 * @param argc Standard main() style argc.
5467 * @param argv Standard main() style argv. Initial components are already
5470 * @return Normal NTSTATUS return.
5473 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5474 const struct dom_sid
*domain_sid
,
5475 const char *domain_name
,
5476 struct cli_state
*cli
,
5477 struct rpc_pipe_client
*pipe_hnd
,
5478 TALLOC_CTX
*mem_ctx
,
5482 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5484 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5486 status
= dcerpc_initshutdown_Abort(b
, mem_ctx
, NULL
, &result
);
5487 if (!NT_STATUS_IS_OK(status
)) {
5490 if (W_ERROR_IS_OK(result
)) {
5491 d_printf(_("\nShutdown successfully aborted\n"));
5492 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5494 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5496 return werror_to_ntstatus(result
);
5500 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5502 * All parameters are provided by the run_rpc_command function, except for
5503 * argc, argv which are passed through.
5505 * @param c A net_context structure.
5506 * @param domain_sid The domain sid acquired from the remote server.
5507 * @param cli A cli_state connected to the server.
5508 * @param mem_ctx Talloc context, destroyed on completion of the function.
5509 * @param argc Standard main() style argc.
5510 * @param argv Standard main() style argv. Initial components are already
5513 * @return Normal NTSTATUS return.
5516 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5517 const struct dom_sid
*domain_sid
,
5518 const char *domain_name
,
5519 struct cli_state
*cli
,
5520 struct rpc_pipe_client
*pipe_hnd
,
5521 TALLOC_CTX
*mem_ctx
,
5525 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5527 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5529 result
= dcerpc_winreg_AbortSystemShutdown(b
, mem_ctx
, NULL
, &werr
);
5531 if (!NT_STATUS_IS_OK(result
)) {
5532 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5535 if (W_ERROR_IS_OK(werr
)) {
5536 d_printf(_("\nShutdown successfully aborted\n"));
5537 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5539 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5541 return werror_to_ntstatus(werr
);
5545 * ABORT the shutdown of a remote RPC server.
5547 * @param argc Standard main() style argc.
5548 * @param argv Standard main() style argv. Initial components are already
5551 * @return A shell status integer (0 for success).
5554 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5559 if (c
->display_usage
) {
5561 "net rpc abortshutdown\n"
5564 _("Abort a scheduled shutdown"));
5568 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5569 rpc_shutdown_abort_internals
, argc
, argv
);
5574 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5576 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5577 rpc_reg_shutdown_abort_internals
,
5582 * Shut down a remote RPC Server via initshutdown pipe.
5584 * All parameters are provided by the run_rpc_command function, except for
5585 * argc, argv which are passed through.
5587 * @param c A net_context structure.
5588 * @param domain_sid The domain sid acquired from the remote server.
5589 * @param cli A cli_state connected to the server.
5590 * @param mem_ctx Talloc context, destroyed on completion of the function.
5591 * @param argc Standard main() style argc.
5592 * @param argv Standard main() style argv. Initial components are already
5595 * @return Normal NTSTATUS return.
5598 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5599 const struct dom_sid
*domain_sid
,
5600 const char *domain_name
,
5601 struct cli_state
*cli
,
5602 struct rpc_pipe_client
*pipe_hnd
,
5603 TALLOC_CTX
*mem_ctx
,
5607 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5609 const char *msg
= N_("This machine will be shutdown shortly");
5610 uint32 timeout
= 20;
5611 struct lsa_StringLarge msg_string
;
5612 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5614 if (c
->opt_comment
) {
5615 msg
= c
->opt_comment
;
5617 if (c
->opt_timeout
) {
5618 timeout
= c
->opt_timeout
;
5621 msg_string
.string
= msg
;
5623 /* create an entry */
5624 status
= dcerpc_initshutdown_Init(b
, mem_ctx
, NULL
,
5625 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5627 if (!NT_STATUS_IS_OK(status
)) {
5630 if (W_ERROR_IS_OK(result
)) {
5631 d_printf(_("\nShutdown of remote machine succeeded\n"));
5632 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5634 DEBUG(1,("Shutdown of remote machine failed!\n"));
5636 return werror_to_ntstatus(result
);
5640 * Shut down a remote RPC Server via winreg pipe.
5642 * All parameters are provided by the run_rpc_command function, except for
5643 * argc, argv which are passed through.
5645 * @param c A net_context structure.
5646 * @param domain_sid The domain sid acquired from the remote server.
5647 * @param cli A cli_state connected to the server.
5648 * @param mem_ctx Talloc context, destroyed on completion of the function.
5649 * @param argc Standard main() style argc.
5650 * @param argv Standard main() style argv. Initial components are already
5653 * @return Normal NTSTATUS return.
5656 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5657 const struct dom_sid
*domain_sid
,
5658 const char *domain_name
,
5659 struct cli_state
*cli
,
5660 struct rpc_pipe_client
*pipe_hnd
,
5661 TALLOC_CTX
*mem_ctx
,
5665 const char *msg
= N_("This machine will be shutdown shortly");
5666 uint32 timeout
= 20;
5667 struct lsa_StringLarge msg_string
;
5670 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5672 if (c
->opt_comment
) {
5673 msg
= c
->opt_comment
;
5675 msg_string
.string
= msg
;
5677 if (c
->opt_timeout
) {
5678 timeout
= c
->opt_timeout
;
5681 /* create an entry */
5682 result
= dcerpc_winreg_InitiateSystemShutdown(b
, mem_ctx
, NULL
,
5683 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5685 if (!NT_STATUS_IS_OK(result
)) {
5686 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5690 if (W_ERROR_IS_OK(werr
)) {
5691 d_printf(_("\nShutdown of remote machine succeeded\n"));
5693 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5694 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5695 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5697 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5700 return werror_to_ntstatus(werr
);
5704 * Shut down a remote RPC server.
5706 * @param argc Standard main() style argc.
5707 * @param argv Standard main() style argv. Initial components are already
5710 * @return A shell status integer (0 for success).
5713 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5717 if (c
->display_usage
) {
5719 "net rpc shutdown\n"
5722 _("Shut down a remote RPC server"));
5726 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5727 rpc_init_shutdown_internals
, argc
, argv
);
5730 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5731 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5732 rpc_reg_shutdown_internals
, argc
, argv
);
5738 /***************************************************************************
5739 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5740 ***************************************************************************/
5743 * Add interdomain trust account to the RPC server.
5744 * All parameters (except for argc and argv) are passed by run_rpc_command
5747 * @param c A net_context structure.
5748 * @param domain_sid The domain sid acquired from the server.
5749 * @param cli A cli_state connected to the server.
5750 * @param mem_ctx Talloc context, destroyed on completion of the function.
5751 * @param argc Standard main() style argc.
5752 * @param argv Standard main() style argv. Initial components are already
5755 * @return normal NTSTATUS return code.
5758 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5759 const struct dom_sid
*domain_sid
,
5760 const char *domain_name
,
5761 struct cli_state
*cli
,
5762 struct rpc_pipe_client
*pipe_hnd
,
5763 TALLOC_CTX
*mem_ctx
,
5767 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5768 NTSTATUS status
, result
;
5770 struct lsa_String lsa_acct_name
;
5772 uint32 acct_flags
=0;
5774 uint32_t access_granted
= 0;
5775 union samr_UserInfo info
;
5776 unsigned int orig_timeout
;
5777 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5782 _(" net rpc trustdom add <domain_name> "
5783 "<trust password>\n"));
5784 return NT_STATUS_INVALID_PARAMETER
;
5788 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5791 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5792 return NT_STATUS_NO_MEMORY
;
5795 strupper_m(acct_name
);
5797 init_lsa_String(&lsa_acct_name
, acct_name
);
5799 /* Get samr policy handle */
5800 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5802 MAXIMUM_ALLOWED_ACCESS
,
5805 if (!NT_STATUS_IS_OK(status
)) {
5808 if (!NT_STATUS_IS_OK(result
)) {
5813 /* Get domain policy handle */
5814 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5816 MAXIMUM_ALLOWED_ACCESS
,
5817 discard_const_p(struct dom_sid2
, domain_sid
),
5820 if (!NT_STATUS_IS_OK(status
)) {
5823 if (!NT_STATUS_IS_OK(result
)) {
5828 /* This call can take a long time - allow the server to time out.
5829 * 35 seconds should do it. */
5831 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5833 /* Create trusting domain's account */
5834 acb_info
= ACB_NORMAL
;
5835 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5836 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5837 SAMR_USER_ACCESS_SET_PASSWORD
|
5838 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5839 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5841 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
5850 if (!NT_STATUS_IS_OK(status
)) {
5853 /* And restore our original timeout. */
5854 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5856 if (!NT_STATUS_IS_OK(result
)) {
5858 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5859 acct_name
, nt_errstr(result
));
5864 struct samr_CryptPassword crypt_pwd
;
5866 ZERO_STRUCT(info
.info23
);
5868 init_samr_CryptPassword(argv
[1],
5869 &cli
->user_session_key
,
5872 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5873 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5874 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5875 info
.info23
.password
= crypt_pwd
;
5877 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
5882 if (!NT_STATUS_IS_OK(status
)) {
5886 if (!NT_STATUS_IS_OK(result
)) {
5888 DEBUG(0,("Could not set trust account password: %s\n",
5889 nt_errstr(result
)));
5895 SAFE_FREE(acct_name
);
5900 * Create interdomain trust account for a remote domain.
5902 * @param argc Standard argc.
5903 * @param argv Standard argv without initial components.
5905 * @return Integer status (0 means success).
5908 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5910 if (argc
> 0 && !c
->display_usage
) {
5911 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5912 rpc_trustdom_add_internals
, argc
, argv
);
5916 _("net rpc trustdom add <domain_name> <trust "
5924 * Remove interdomain trust account from the RPC server.
5925 * All parameters (except for argc and argv) are passed by run_rpc_command
5928 * @param c A net_context structure.
5929 * @param domain_sid The domain sid acquired from the server.
5930 * @param cli A cli_state connected to the server.
5931 * @param mem_ctx Talloc context, destroyed on completion of the function.
5932 * @param argc Standard main() style argc.
5933 * @param argv Standard main() style argv. Initial components are already
5936 * @return normal NTSTATUS return code.
5939 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5940 const struct dom_sid
*domain_sid
,
5941 const char *domain_name
,
5942 struct cli_state
*cli
,
5943 struct rpc_pipe_client
*pipe_hnd
,
5944 TALLOC_CTX
*mem_ctx
,
5948 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5949 NTSTATUS status
, result
;
5951 struct dom_sid trust_acct_sid
;
5952 struct samr_Ids user_rids
, name_types
;
5953 struct lsa_String lsa_acct_name
;
5954 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5959 _(" net rpc trustdom del <domain_name>\n"));
5960 return NT_STATUS_INVALID_PARAMETER
;
5964 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5966 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5968 if (acct_name
== NULL
)
5969 return NT_STATUS_NO_MEMORY
;
5971 strupper_m(acct_name
);
5973 /* Get samr policy handle */
5974 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5976 MAXIMUM_ALLOWED_ACCESS
,
5979 if (!NT_STATUS_IS_OK(status
)) {
5982 if (!NT_STATUS_IS_OK(result
)) {
5987 /* Get domain policy handle */
5988 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5990 MAXIMUM_ALLOWED_ACCESS
,
5991 discard_const_p(struct dom_sid2
, domain_sid
),
5994 if (!NT_STATUS_IS_OK(status
)) {
5997 if (!NT_STATUS_IS_OK(result
)) {
6002 init_lsa_String(&lsa_acct_name
, acct_name
);
6004 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
6011 if (!NT_STATUS_IS_OK(status
)) {
6012 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6014 acct_name
, nt_errstr(status
));
6017 if (!NT_STATUS_IS_OK(result
)) {
6019 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6021 acct_name
, nt_errstr(result
) );
6025 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
6027 MAXIMUM_ALLOWED_ACCESS
,
6031 if (!NT_STATUS_IS_OK(status
)) {
6032 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6034 acct_name
, nt_errstr(status
) );
6038 if (!NT_STATUS_IS_OK(result
)) {
6040 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6042 acct_name
, nt_errstr(result
) );
6046 /* append the rid to the domain sid */
6047 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
6051 /* remove the sid */
6053 status
= dcerpc_samr_RemoveMemberFromForeignDomain(b
, mem_ctx
,
6057 if (!NT_STATUS_IS_OK(status
)) {
6058 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6059 " on user %s failed %s\n"),
6060 acct_name
, nt_errstr(status
));
6063 if (!NT_STATUS_IS_OK(result
)) {
6065 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6066 " on user %s failed %s\n"),
6067 acct_name
, nt_errstr(result
) );
6074 status
= dcerpc_samr_DeleteUser(b
, mem_ctx
,
6077 if (!NT_STATUS_IS_OK(status
)) {
6078 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6080 acct_name
, nt_errstr(status
));
6084 if (!NT_STATUS_IS_OK(result
)) {
6086 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6088 acct_name
, nt_errstr(result
) );
6092 if (!NT_STATUS_IS_OK(result
)) {
6093 d_printf(_("Could not set trust account password: %s\n"),
6103 * Delete interdomain trust account for a remote domain.
6105 * @param argc Standard argc.
6106 * @param argv Standard argv without initial components.
6108 * @return Integer status (0 means success).
6111 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
6113 if (argc
> 0 && !c
->display_usage
) {
6114 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
6115 rpc_trustdom_del_internals
, argc
, argv
);
6119 _("net rpc trustdom del <domain>\n"));
6124 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
6125 struct cli_state
*cli
,
6126 TALLOC_CTX
*mem_ctx
,
6127 const char *domain_name
)
6129 char *dc_name
= NULL
;
6130 const char *buffer
= NULL
;
6131 struct rpc_pipe_client
*netr
;
6134 struct dcerpc_binding_handle
*b
;
6136 /* Use NetServerEnum2 */
6138 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
6140 return NT_STATUS_OK
;
6143 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6144 for domain %s\n", domain_name
));
6146 /* Try netr_GetDcName */
6148 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
6150 if (!NT_STATUS_IS_OK(status
)) {
6154 b
= netr
->binding_handle
;
6156 status
= dcerpc_netr_GetDcName(b
, mem_ctx
,
6163 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(result
)) {
6167 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6168 for domain %s\n", domain_name
));
6170 if (!NT_STATUS_IS_OK(status
)) {
6174 return werror_to_ntstatus(result
);
6178 * Establish trust relationship to a trusting domain.
6179 * Interdomain account must already be created on remote PDC.
6181 * @param c A net_context structure.
6182 * @param argc Standard argc.
6183 * @param argv Standard argv without initial components.
6185 * @return Integer status (0 means success).
6188 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6191 struct cli_state
*cli
= NULL
;
6192 struct sockaddr_storage server_ss
;
6193 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6194 struct policy_handle connect_hnd
;
6195 TALLOC_CTX
*mem_ctx
;
6196 NTSTATUS nt_status
, result
;
6197 struct dom_sid
*domain_sid
;
6202 union lsa_PolicyInformation
*info
= NULL
;
6203 struct dcerpc_binding_handle
*b
;
6206 * Connect to \\server\ipc$ as 'our domain' account with password
6209 if (argc
!= 1 || c
->display_usage
) {
6212 _("net rpc trustdom establish <domain_name>\n"));
6216 domain_name
= smb_xstrdup(argv
[0]);
6217 strupper_m(domain_name
);
6219 /* account name used at first is our domain's name with '$' */
6220 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
6223 strupper_m(acct_name
);
6226 * opt_workgroup will be used by connection functions further,
6227 * hence it should be set to remote domain name instead of ours
6229 if (c
->opt_workgroup
) {
6230 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6233 c
->opt_user_name
= acct_name
;
6235 /* find the domain controller */
6236 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6237 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6241 /* connect to ipc$ as username/password */
6242 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6243 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6245 /* Is it trusting domain account for sure ? */
6246 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6247 nt_errstr(nt_status
)));
6251 /* store who we connected to */
6253 saf_store( domain_name
, pdc_name
);
6256 * Connect to \\server\ipc$ again (this time anonymously)
6259 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6262 if (NT_STATUS_IS_ERR(nt_status
)) {
6263 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6264 domain_name
, nt_errstr(nt_status
)));
6268 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6269 "domain %s", domain_name
))) {
6270 DEBUG(0, ("talloc_init() failed\n"));
6275 /* Make sure we're talking to a proper server */
6277 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6278 if (!NT_STATUS_IS_OK(nt_status
)) {
6280 talloc_destroy(mem_ctx
);
6285 * Call LsaOpenPolicy and LsaQueryInfo
6288 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6290 if (!NT_STATUS_IS_OK(nt_status
)) {
6291 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6293 talloc_destroy(mem_ctx
);
6297 b
= pipe_hnd
->binding_handle
;
6299 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
6301 if (NT_STATUS_IS_ERR(nt_status
)) {
6302 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6303 nt_errstr(nt_status
)));
6305 talloc_destroy(mem_ctx
);
6309 /* Querying info level 5 */
6311 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6313 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6316 if (NT_STATUS_IS_ERR(nt_status
)) {
6317 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6318 nt_errstr(nt_status
)));
6320 talloc_destroy(mem_ctx
);
6323 if (NT_STATUS_IS_ERR(result
)) {
6324 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6325 nt_errstr(result
)));
6327 talloc_destroy(mem_ctx
);
6331 domain_sid
= info
->account_domain
.sid
;
6333 /* There should be actually query info level 3 (following nt serv behaviour),
6334 but I still don't know if it's _really_ necessary */
6337 * Store the password in secrets db
6340 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6341 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6343 talloc_destroy(mem_ctx
);
6348 * Close the pipes and clean up
6351 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6352 if (NT_STATUS_IS_ERR(nt_status
)) {
6353 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6354 nt_errstr(nt_status
)));
6356 talloc_destroy(mem_ctx
);
6362 talloc_destroy(mem_ctx
);
6364 d_printf(_("Trust to domain %s established\n"), domain_name
);
6369 * Revoke trust relationship to the remote domain.
6371 * @param c A net_context structure.
6372 * @param argc Standard argc.
6373 * @param argv Standard argv without initial components.
6375 * @return Integer status (0 means success).
6378 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6384 if (argc
< 1 || c
->display_usage
) {
6387 _("net rpc trustdom revoke <domain_name>\n"
6388 " Revoke trust relationship\n"
6389 " domain_name\tName of domain to revoke trust\n"));
6393 /* generate upper cased domain name */
6394 domain_name
= smb_xstrdup(argv
[0]);
6395 strupper_m(domain_name
);
6397 /* delete password of the trust */
6398 if (!pdb_del_trusteddom_pw(domain_name
)) {
6399 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6406 SAFE_FREE(domain_name
);
6410 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6411 const struct dom_sid
*domain_sid
,
6412 const char *domain_name
,
6413 struct cli_state
*cli
,
6414 struct rpc_pipe_client
*pipe_hnd
,
6415 TALLOC_CTX
*mem_ctx
,
6420 if (!sid_to_fstring(str_sid
, domain_sid
)) {
6421 return NT_STATUS_UNSUCCESSFUL
;
6423 d_printf("%s\n", str_sid
);
6424 return NT_STATUS_OK
;
6427 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
6431 /* convert sid into ascii string */
6432 sid_to_fstring(ascii_sid
, dom_sid
);
6434 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
6437 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6438 TALLOC_CTX
*mem_ctx
,
6439 struct policy_handle
*pol
,
6440 struct dom_sid dom_sid
,
6441 const char *trusted_dom_name
)
6443 NTSTATUS nt_status
, result
;
6444 union lsa_TrustedDomainInfo
*info
= NULL
;
6445 char *cleartextpwd
= NULL
;
6446 DATA_BLOB session_key
;
6447 DATA_BLOB data
= data_blob_null
;
6448 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6450 nt_status
= dcerpc_lsa_QueryTrustedDomainInfoBySid(b
, mem_ctx
,
6453 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6456 if (NT_STATUS_IS_ERR(nt_status
)) {
6457 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6458 nt_errstr(nt_status
)));
6461 if (NT_STATUS_IS_ERR(result
)) {
6463 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6464 nt_errstr(result
)));
6468 data
= data_blob(info
->password
.password
->data
,
6469 info
->password
.password
->length
);
6471 nt_status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
6472 if (!NT_STATUS_IS_OK(nt_status
)) {
6473 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status
)));
6477 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key
);
6478 data_blob_free(&session_key
);
6480 if (cleartextpwd
== NULL
) {
6481 DEBUG(0,("retrieved NULL password\n"));
6482 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6486 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6487 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6488 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6492 #ifdef DEBUG_PASSWORD
6493 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6494 "password: [%s]\n", trusted_dom_name
,
6495 sid_string_dbg(&dom_sid
), cleartextpwd
));
6499 SAFE_FREE(cleartextpwd
);
6500 data_blob_free(&data
);
6505 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6508 /* common variables */
6509 TALLOC_CTX
* mem_ctx
;
6510 struct cli_state
*cli
= NULL
;
6511 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6512 NTSTATUS nt_status
, result
;
6513 const char *domain_name
= NULL
;
6514 struct policy_handle connect_hnd
;
6515 union lsa_PolicyInformation
*info
= NULL
;
6517 /* trusted domains listing variables */
6518 unsigned int enum_ctx
= 0;
6520 struct lsa_DomainList dom_list
;
6522 struct dcerpc_binding_handle
*b
;
6524 if (c
->display_usage
) {
6526 "net rpc trustdom vampire\n"
6529 _("Vampire trust relationship from remote server"));
6534 * Listing trusted domains (stored in secrets.tdb, if local)
6537 mem_ctx
= talloc_init("trust relationships vampire");
6540 * set domain and pdc name to local samba server (default)
6541 * or to remote one given in command line
6544 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6545 domain_name
= c
->opt_workgroup
;
6546 c
->opt_target_workgroup
= c
->opt_workgroup
;
6548 fstrcpy(pdc_name
, lp_netbios_name());
6549 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6550 c
->opt_target_workgroup
= domain_name
;
6553 /* open \PIPE\lsarpc and open policy handle */
6554 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6555 if (!NT_STATUS_IS_OK(nt_status
)) {
6556 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6557 nt_errstr(nt_status
)));
6558 talloc_destroy(mem_ctx
);
6562 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6564 if (!NT_STATUS_IS_OK(nt_status
)) {
6565 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6566 nt_errstr(nt_status
) ));
6568 talloc_destroy(mem_ctx
);
6572 b
= pipe_hnd
->binding_handle
;
6574 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6576 if (NT_STATUS_IS_ERR(nt_status
)) {
6577 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6578 nt_errstr(nt_status
)));
6580 talloc_destroy(mem_ctx
);
6584 /* query info level 5 to obtain sid of a domain being queried */
6585 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6587 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6591 if (NT_STATUS_IS_ERR(nt_status
)) {
6592 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6593 nt_errstr(nt_status
)));
6595 talloc_destroy(mem_ctx
);
6598 if (NT_STATUS_IS_ERR(result
)) {
6599 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6600 nt_errstr(result
)));
6602 talloc_destroy(mem_ctx
);
6607 * Keep calling LsaEnumTrustdom over opened pipe until
6608 * the end of enumeration is reached
6611 d_printf(_("Vampire trusted domains:\n\n"));
6614 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6620 if (NT_STATUS_IS_ERR(nt_status
)) {
6621 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6622 nt_errstr(nt_status
)));
6624 talloc_destroy(mem_ctx
);
6627 if (NT_STATUS_IS_ERR(result
)) {
6629 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6630 nt_errstr(result
)));
6632 talloc_destroy(mem_ctx
);
6637 for (i
= 0; i
< dom_list
.count
; i
++) {
6639 print_trusted_domain(dom_list
.domains
[i
].sid
,
6640 dom_list
.domains
[i
].name
.string
);
6642 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6643 *dom_list
.domains
[i
].sid
,
6644 dom_list
.domains
[i
].name
.string
);
6645 if (!NT_STATUS_IS_OK(nt_status
)) {
6647 talloc_destroy(mem_ctx
);
6653 * in case of no trusted domains say something rather
6654 * than just display blank line
6656 if (!dom_list
.count
) d_printf(_("none\n"));
6658 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6660 /* close this connection before doing next one */
6661 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6662 if (NT_STATUS_IS_ERR(nt_status
)) {
6663 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6664 nt_errstr(nt_status
)));
6666 talloc_destroy(mem_ctx
);
6670 /* close lsarpc pipe and connection to IPC$ */
6673 talloc_destroy(mem_ctx
);
6677 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6679 /* common variables */
6680 TALLOC_CTX
* mem_ctx
;
6681 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6682 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6683 NTSTATUS nt_status
, result
;
6684 const char *domain_name
= NULL
;
6685 struct dom_sid
*queried_dom_sid
;
6686 int ascii_dom_name_len
;
6687 struct policy_handle connect_hnd
;
6688 union lsa_PolicyInformation
*info
= NULL
;
6689 struct dcerpc_binding_handle
*b
= NULL
;
6691 /* trusted domains listing variables */
6692 unsigned int num_domains
, enum_ctx
= 0;
6694 struct lsa_DomainList dom_list
;
6698 /* trusting domains listing variables */
6699 struct policy_handle domain_hnd
;
6700 struct samr_SamArray
*trusts
= NULL
;
6702 if (c
->display_usage
) {
6704 "net rpc trustdom list\n"
6707 _("List incoming and outgoing trust relationships"));
6712 * Listing trusted domains (stored in secrets.tdb, if local)
6715 mem_ctx
= talloc_init("trust relationships listing");
6718 * set domain and pdc name to local samba server (default)
6719 * or to remote one given in command line
6722 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6723 domain_name
= c
->opt_workgroup
;
6724 c
->opt_target_workgroup
= c
->opt_workgroup
;
6726 fstrcpy(pdc_name
, lp_netbios_name());
6727 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6728 c
->opt_target_workgroup
= domain_name
;
6731 /* open \PIPE\lsarpc and open policy handle */
6732 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6733 if (!NT_STATUS_IS_OK(nt_status
)) {
6734 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6735 nt_errstr(nt_status
)));
6736 talloc_destroy(mem_ctx
);
6740 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6742 if (!NT_STATUS_IS_OK(nt_status
)) {
6743 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6744 nt_errstr(nt_status
) ));
6746 talloc_destroy(mem_ctx
);
6750 b
= pipe_hnd
->binding_handle
;
6752 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6754 if (NT_STATUS_IS_ERR(nt_status
)) {
6755 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6756 nt_errstr(nt_status
)));
6758 talloc_destroy(mem_ctx
);
6762 /* query info level 5 to obtain sid of a domain being queried */
6763 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6765 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6769 if (NT_STATUS_IS_ERR(nt_status
)) {
6770 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6771 nt_errstr(nt_status
)));
6773 talloc_destroy(mem_ctx
);
6776 if (NT_STATUS_IS_ERR(result
)) {
6777 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6778 nt_errstr(result
)));
6780 talloc_destroy(mem_ctx
);
6784 queried_dom_sid
= info
->account_domain
.sid
;
6787 * Keep calling LsaEnumTrustdom over opened pipe until
6788 * the end of enumeration is reached
6791 d_printf(_("Trusted domains list:\n\n"));
6793 found_domain
= false;
6796 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6802 if (NT_STATUS_IS_ERR(nt_status
)) {
6803 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6804 nt_errstr(nt_status
)));
6806 talloc_destroy(mem_ctx
);
6809 if (NT_STATUS_IS_ERR(result
)) {
6810 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6811 nt_errstr(result
)));
6813 talloc_destroy(mem_ctx
);
6818 for (i
= 0; i
< dom_list
.count
; i
++) {
6819 print_trusted_domain(dom_list
.domains
[i
].sid
,
6820 dom_list
.domains
[i
].name
.string
);
6821 found_domain
= true;
6825 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6828 * in case of no trusted domains say something rather
6829 * than just display blank line
6831 if (!found_domain
) {
6832 d_printf(_("none\n"));
6835 /* close this connection before doing next one */
6836 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6837 if (NT_STATUS_IS_ERR(nt_status
)) {
6838 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6839 nt_errstr(nt_status
)));
6841 talloc_destroy(mem_ctx
);
6845 TALLOC_FREE(pipe_hnd
);
6848 * Listing trusting domains (stored in passdb backend, if local)
6851 d_printf(_("\nTrusting domains list:\n\n"));
6854 * Open \PIPE\samr and get needed policy handles
6856 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6858 if (!NT_STATUS_IS_OK(nt_status
)) {
6859 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6861 talloc_destroy(mem_ctx
);
6865 b
= pipe_hnd
->binding_handle
;
6868 nt_status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6870 SAMR_ACCESS_LOOKUP_DOMAIN
,
6873 if (!NT_STATUS_IS_OK(nt_status
)) {
6874 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6875 nt_errstr(nt_status
)));
6877 talloc_destroy(mem_ctx
);
6880 if (!NT_STATUS_IS_OK(result
)) {
6882 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6883 nt_errstr(result
)));
6885 talloc_destroy(mem_ctx
);
6889 /* SamrOpenDomain - we have to open domain policy handle in order to be
6890 able to enumerate accounts*/
6891 nt_status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6893 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6897 if (!NT_STATUS_IS_OK(nt_status
)) {
6898 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6899 nt_errstr(nt_status
)));
6901 talloc_destroy(mem_ctx
);
6904 if (!NT_STATUS_IS_OK(result
)) {
6906 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6907 nt_errstr(result
)));
6909 talloc_destroy(mem_ctx
);
6914 * perform actual enumeration
6917 found_domain
= false;
6919 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6922 nt_status
= dcerpc_samr_EnumDomainUsers(b
, mem_ctx
,
6930 if (NT_STATUS_IS_ERR(nt_status
)) {
6931 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6932 nt_errstr(nt_status
)));
6934 talloc_destroy(mem_ctx
);
6937 if (NT_STATUS_IS_ERR(result
)) {
6939 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6940 nt_errstr(result
)));
6942 talloc_destroy(mem_ctx
);
6946 for (i
= 0; i
< num_domains
; i
++) {
6948 char *str
= discard_const_p(char, trusts
->entries
[i
].name
.string
);
6950 found_domain
= true;
6953 * get each single domain's sid (do we _really_ need this ?):
6954 * 1) connect to domain's pdc
6955 * 2) query the pdc for domain's sid
6958 /* get rid of '$' tail */
6959 ascii_dom_name_len
= strlen(str
);
6960 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6961 str
[ascii_dom_name_len
- 1] = '\0';
6963 /* set opt_* variables to remote domain */
6965 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6966 c
->opt_target_workgroup
= c
->opt_workgroup
;
6968 d_printf("%-20s", str
);
6970 /* connect to remote domain controller */
6971 nt_status
= net_make_ipc_connection(c
,
6972 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6974 if (NT_STATUS_IS_OK(nt_status
)) {
6975 /* query for domain's sid */
6976 if (run_rpc_command(
6978 &ndr_table_lsarpc
, 0,
6979 rpc_query_domain_sid
, argc
,
6981 d_printf(_("strange - couldn't get domain's sid\n"));
6983 cli_shutdown(remote_cli
);
6986 d_fprintf(stderr
, _("domain controller is not "
6987 "responding: %s\n"),
6988 nt_errstr(nt_status
));
6989 d_printf(_("couldn't get domain's sid\n"));
6993 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
6995 if (!found_domain
) {
6999 /* close opened samr and domain policy handles */
7000 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &domain_hnd
, &result
);
7001 if (!NT_STATUS_IS_OK(nt_status
)) {
7002 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
7005 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &connect_hnd
, &result
);
7006 if (!NT_STATUS_IS_OK(nt_status
)) {
7007 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
7010 /* close samr pipe and connection to IPC$ */
7013 talloc_destroy(mem_ctx
);
7018 * Entrypoint for 'net rpc trustdom' code.
7020 * @param argc Standard argc.
7021 * @param argv Standard argv without initial components.
7023 * @return Integer status (0 means success).
7026 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
7028 struct functable func
[] = {
7033 N_("Add trusting domain's account"),
7034 N_("net rpc trustdom add\n"
7035 " Add trusting domain's account")
7041 N_("Remove trusting domain's account"),
7042 N_("net rpc trustdom del\n"
7043 " Remove trusting domain's account")
7047 rpc_trustdom_establish
,
7049 N_("Establish outgoing trust relationship"),
7050 N_("net rpc trustdom establish\n"
7051 " Establish outgoing trust relationship")
7055 rpc_trustdom_revoke
,
7057 N_("Revoke outgoing trust relationship"),
7058 N_("net rpc trustdom revoke\n"
7059 " Revoke outgoing trust relationship")
7065 N_("List in- and outgoing domain trusts"),
7066 N_("net rpc trustdom list\n"
7067 " List in- and outgoing domain trusts")
7071 rpc_trustdom_vampire
,
7073 N_("Vampire trusts from remote server"),
7074 N_("net rpc trustdom vampire\n"
7075 " Vampire trusts from remote server")
7077 {NULL
, NULL
, 0, NULL
, NULL
}
7080 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
7084 * Check if a server will take rpc commands
7085 * @param flags Type of server to connect to (PDC, DMB, localhost)
7086 * if the host is not explicitly specified
7087 * @return bool (true means rpc supported)
7089 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
7091 struct cli_state
*cli
;
7093 struct sockaddr_storage server_ss
;
7094 char *server_name
= NULL
;
7097 /* flags (i.e. server type) may depend on command */
7098 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
7101 status
= cli_connect_nb(server_name
, &server_ss
, 0, 0x20,
7102 lp_netbios_name(), SMB_SIGNING_DEFAULT
,
7104 if (!NT_STATUS_IS_OK(status
)) {
7107 status
= cli_negprot(cli
, PROTOCOL_NT1
);
7108 if (!NT_STATUS_IS_OK(status
))
7110 if (cli_state_protocol(cli
) < PROTOCOL_NT1
)
7119 /* dump sam database via samsync rpc calls */
7120 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
7121 if (c
->display_usage
) {
7126 _("Dump remote SAM database"));
7130 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
,
7131 NET_FLAGS_ANONYMOUS
,
7132 rpc_samdump_internals
, argc
, argv
);
7135 /* syncronise sam database via samsync rpc calls */
7136 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
7138 struct functable func
[] = {
7143 N_("Dump remote SAM database to ldif"),
7144 N_("net rpc vampire ldif\n"
7145 " Dump remote SAM database to LDIF file or "
7152 N_("Dump remote SAM database to Kerberos Keytab"),
7153 N_("net rpc vampire keytab\n"
7154 " Dump remote SAM database to Kerberos keytab "
7161 N_("Dump remote SAM database to passdb"),
7162 N_("net rpc vampire passdb\n"
7163 " Dump remote SAM database to passdb")
7166 {NULL
, NULL
, 0, NULL
, NULL
}
7170 if (c
->display_usage
) {
7175 _("Vampire remote SAM database"));
7179 return rpc_vampire_passdb(c
, argc
, argv
);
7182 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
7186 * Migrate everything from a print server.
7188 * @param c A net_context structure.
7189 * @param argc Standard main() style argc.
7190 * @param argv Standard main() style argv. Initial components are already
7193 * @return A shell status integer (0 for success).
7195 * The order is important !
7196 * To successfully add drivers the print queues have to exist !
7197 * Applying ACLs should be the last step, because you're easily locked out.
7200 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
7205 if (c
->display_usage
) {
7207 "net rpc printer migrate all\n"
7210 _("Migrate everything from a print server"));
7215 d_printf(_("no server to migrate\n"));
7219 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7220 rpc_printer_migrate_printers_internals
, argc
,
7225 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7226 rpc_printer_migrate_drivers_internals
, argc
,
7231 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7232 rpc_printer_migrate_forms_internals
, argc
, argv
);
7236 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7237 rpc_printer_migrate_settings_internals
, argc
,
7242 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7243 rpc_printer_migrate_security_internals
, argc
,
7249 * Migrate print drivers from a print server.
7251 * @param c A net_context structure.
7252 * @param argc Standard main() style argc.
7253 * @param argv Standard main() style argv. Initial components are already
7256 * @return A shell status integer (0 for success).
7258 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
7261 if (c
->display_usage
) {
7263 "net rpc printer migrate drivers\n"
7266 _("Migrate print-drivers from a print-server"));
7271 d_printf(_("no server to migrate\n"));
7275 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7276 rpc_printer_migrate_drivers_internals
,
7281 * Migrate print-forms from a print-server.
7283 * @param c A net_context structure.
7284 * @param argc Standard main() style argc.
7285 * @param argv Standard main() style argv. Initial components are already
7288 * @return A shell status integer (0 for success).
7290 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7293 if (c
->display_usage
) {
7295 "net rpc printer migrate forms\n"
7298 _("Migrate print-forms from a print-server"));
7303 d_printf(_("no server to migrate\n"));
7307 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7308 rpc_printer_migrate_forms_internals
,
7313 * Migrate printers from a print-server.
7315 * @param c A net_context structure.
7316 * @param argc Standard main() style argc.
7317 * @param argv Standard main() style argv. Initial components are already
7320 * @return A shell status integer (0 for success).
7322 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7325 if (c
->display_usage
) {
7327 "net rpc printer migrate printers\n"
7330 _("Migrate printers from a print-server"));
7335 d_printf(_("no server to migrate\n"));
7339 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7340 rpc_printer_migrate_printers_internals
,
7345 * Migrate printer-ACLs from a print-server
7347 * @param c A net_context structure.
7348 * @param argc Standard main() style argc.
7349 * @param argv Standard main() style argv. Initial components are already
7352 * @return A shell status integer (0 for success).
7354 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7357 if (c
->display_usage
) {
7359 "net rpc printer migrate security\n"
7362 _("Migrate printer-ACLs from a print-server"));
7367 d_printf(_("no server to migrate\n"));
7371 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7372 rpc_printer_migrate_security_internals
,
7377 * Migrate printer-settings from a print-server.
7379 * @param c A net_context structure.
7380 * @param argc Standard main() style argc.
7381 * @param argv Standard main() style argv. Initial components are already
7384 * @return A shell status integer (0 for success).
7386 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7389 if (c
->display_usage
) {
7391 "net rpc printer migrate settings\n"
7394 _("Migrate printer-settings from a "
7400 d_printf(_("no server to migrate\n"));
7404 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7405 rpc_printer_migrate_settings_internals
,
7410 * 'net rpc printer' entrypoint.
7412 * @param c A net_context structure.
7413 * @param argc Standard main() style argc.
7414 * @param argv Standard main() style argv. Initial components are already
7418 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7421 /* ouch: when addriver and setdriver are called from within
7422 rpc_printer_migrate_drivers_internals, the printer-queue already
7425 struct functable func
[] = {
7428 rpc_printer_migrate_all
,
7430 N_("Migrate all from remote to local print server"),
7431 N_("net rpc printer migrate all\n"
7432 " Migrate all from remote to local print server")
7436 rpc_printer_migrate_drivers
,
7438 N_("Migrate drivers to local server"),
7439 N_("net rpc printer migrate drivers\n"
7440 " Migrate drivers to local server")
7444 rpc_printer_migrate_forms
,
7446 N_("Migrate froms to local server"),
7447 N_("net rpc printer migrate forms\n"
7448 " Migrate froms to local server")
7452 rpc_printer_migrate_printers
,
7454 N_("Migrate printers to local server"),
7455 N_("net rpc printer migrate printers\n"
7456 " Migrate printers to local server")
7460 rpc_printer_migrate_security
,
7462 N_("Mirgate printer ACLs to local server"),
7463 N_("net rpc printer migrate security\n"
7464 " Mirgate printer ACLs to local server")
7468 rpc_printer_migrate_settings
,
7470 N_("Migrate printer settings to local server"),
7471 N_("net rpc printer migrate settings\n"
7472 " Migrate printer settings to local server")
7474 {NULL
, NULL
, 0, NULL
, NULL
}
7477 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7482 * List printers on a remote RPC server.
7484 * @param c A net_context structure.
7485 * @param argc Standard main() style argc.
7486 * @param argv Standard main() style argv. Initial components are already
7489 * @return A shell status integer (0 for success).
7491 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7493 if (c
->display_usage
) {
7495 "net rpc printer list\n"
7498 _("List printers on a remote RPC server"));
7502 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7503 rpc_printer_list_internals
,
7508 * List printer-drivers on a remote RPC server.
7510 * @param c A net_context structure.
7511 * @param argc Standard main() style argc.
7512 * @param argv Standard main() style argv. Initial components are already
7515 * @return A shell status integer (0 for success).
7517 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7520 if (c
->display_usage
) {
7522 "net rpc printer driver\n"
7525 _("List printer-drivers on a remote RPC server"));
7529 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7530 rpc_printer_driver_list_internals
,
7535 * Publish printer in ADS via MSRPC.
7537 * @param c A net_context structure.
7538 * @param argc Standard main() style argc.
7539 * @param argv Standard main() style argv. Initial components are already
7542 * @return A shell status integer (0 for success).
7544 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7547 if (c
->display_usage
) {
7549 "net rpc printer publish publish\n"
7552 _("Publish printer in ADS via MSRPC"));
7556 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7557 rpc_printer_publish_publish_internals
,
7562 * Update printer in ADS via MSRPC.
7564 * @param c A net_context structure.
7565 * @param argc Standard main() style argc.
7566 * @param argv Standard main() style argv. Initial components are already
7569 * @return A shell status integer (0 for success).
7571 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7573 if (c
->display_usage
) {
7575 "net rpc printer publish update\n"
7578 _("Update printer in ADS via MSRPC"));
7582 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7583 rpc_printer_publish_update_internals
,
7588 * UnPublish printer in ADS via MSRPC.
7590 * @param c A net_context structure.
7591 * @param argc Standard main() style argc.
7592 * @param argv Standard main() style argv. Initial components are already
7595 * @return A shell status integer (0 for success).
7597 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7600 if (c
->display_usage
) {
7602 "net rpc printer publish unpublish\n"
7605 _("UnPublish printer in ADS via MSRPC"));
7609 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7610 rpc_printer_publish_unpublish_internals
,
7615 * List published printers via MSRPC.
7617 * @param c A net_context structure.
7618 * @param argc Standard main() style argc.
7619 * @param argv Standard main() style argv. Initial components are already
7622 * @return A shell status integer (0 for success).
7624 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7627 if (c
->display_usage
) {
7629 "net rpc printer publish list\n"
7632 _("List published printers via MSRPC"));
7636 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7637 rpc_printer_publish_list_internals
,
7643 * Publish printer in ADS.
7645 * @param c A net_context structure.
7646 * @param argc Standard main() style argc.
7647 * @param argv Standard main() style argv. Initial components are already
7650 * @return A shell status integer (0 for success).
7652 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7656 struct functable func
[] = {
7659 rpc_printer_publish_publish
,
7661 N_("Publish printer in AD"),
7662 N_("net rpc printer publish publish\n"
7663 " Publish printer in AD")
7667 rpc_printer_publish_update
,
7669 N_("Update printer in AD"),
7670 N_("net rpc printer publish update\n"
7671 " Update printer in AD")
7675 rpc_printer_publish_unpublish
,
7677 N_("Unpublish printer"),
7678 N_("net rpc printer publish unpublish\n"
7679 " Unpublish printer")
7683 rpc_printer_publish_list
,
7685 N_("List published printers"),
7686 N_("net rpc printer publish list\n"
7687 " List published printers")
7689 {NULL
, NULL
, 0, NULL
, NULL
}
7693 if (c
->display_usage
) {
7694 d_printf(_("Usage:\n"));
7695 d_printf(_("net rpc printer publish\n"
7696 " List published printers\n"
7697 " Alias of net rpc printer publish "
7699 net_display_usage_from_functable(func
);
7702 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7703 rpc_printer_publish_list_internals
,
7707 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7713 * Display rpc printer help page.
7715 * @param c A net_context structure.
7716 * @param argc Standard main() style argc.
7717 * @param argv Standard main() style argv. Initial components are already
7720 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7722 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7723 "\tlists all printers on print-server\n\n"));
7724 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7725 "\tlists all printer-drivers on print-server\n\n"));
7726 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7727 "\tpublishes printer settings in Active Directory\n"
7728 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7729 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7730 "\n\tmigrates printers from remote to local server\n\n"));
7731 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7732 "\n\tmigrates printer-settings from remote to local server\n\n"));
7733 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7734 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7735 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7736 "\n\tmigrates printer-forms from remote to local server\n\n"));
7737 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7738 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7739 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7740 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7741 "\tremote to local print-server\n\n"));
7742 net_common_methods_usage(c
, argc
, argv
);
7743 net_common_flags_usage(c
, argc
, argv
);
7745 "\t-v or --verbose\t\t\tgive verbose output\n"
7746 "\t --destination\t\tmigration target server (default: localhost)\n"));
7752 * 'net rpc printer' entrypoint.
7754 * @param c A net_context structure.
7755 * @param argc Standard main() style argc.
7756 * @param argv Standard main() style argv. Initial components are already
7759 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7761 struct functable func
[] = {
7766 N_("List all printers on print server"),
7767 N_("net rpc printer list\n"
7768 " List all printers on print server")
7772 rpc_printer_migrate
,
7774 N_("Migrate printer to local server"),
7775 N_("net rpc printer migrate\n"
7776 " Migrate printer to local server")
7780 rpc_printer_driver_list
,
7782 N_("List printer drivers"),
7783 N_("net rpc printer driver\n"
7784 " List printer drivers")
7788 rpc_printer_publish
,
7790 N_("Publish printer in AD"),
7791 N_("net rpc printer publish\n"
7792 " Publish printer in AD")
7794 {NULL
, NULL
, 0, NULL
, NULL
}
7798 if (c
->display_usage
) {
7799 d_printf(_("Usage:\n"));
7800 d_printf(_("net rpc printer\n"
7801 " List printers\n"));
7802 net_display_usage_from_functable(func
);
7805 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7806 rpc_printer_list_internals
,
7810 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7814 * 'net rpc' entrypoint.
7816 * @param c A net_context structure.
7817 * @param argc Standard main() style argc.
7818 * @param argv Standard main() style argv. Initial components are already
7822 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7824 NET_API_STATUS status
;
7826 struct functable func
[] = {
7831 N_("Modify global audit settings"),
7832 N_("net rpc audit\n"
7833 " Modify global audit settings")
7839 N_("Show basic info about a domain"),
7841 " Show basic info about a domain")
7847 N_("Join a domain"),
7855 N_("Join a domain created in server manager"),
7856 N_("net rpc oldjoin\n"
7857 " Join a domain created in server manager")
7863 N_("Test that a join is valid"),
7864 N_("net rpc testjoin\n"
7865 " Test that a join is valid")
7871 N_("List/modify users"),
7873 " List/modify users")
7879 N_("Change a user password"),
7880 N_("net rpc password\n"
7881 " Change a user password\n"
7882 " Alias for net rpc user password")
7888 N_("List/modify groups"),
7889 N_("net rpc group\n"
7890 " List/modify groups")
7896 N_("List/modify shares"),
7897 N_("net rpc share\n"
7898 " List/modify shares")
7904 N_("List open files"),
7912 N_("List/modify printers"),
7913 N_("net rpc printer\n"
7914 " List/modify printers")
7918 net_rpc_changetrustpw
,
7920 N_("Change trust account password"),
7921 N_("net rpc changetrustpw\n"
7922 " Change trust account password")
7928 N_("Modify domain trusts"),
7929 N_("net rpc trustdom\n"
7930 " Modify domain trusts")
7936 N_("Abort a remote shutdown"),
7937 N_("net rpc abortshutdown\n"
7938 " Abort a remote shutdown")
7944 N_("Shutdown a remote server"),
7945 N_("net rpc shutdown\n"
7946 " Shutdown a remote server")
7952 N_("Dump SAM data of remote NT PDC"),
7953 N_("net rpc samdump\n"
7954 " Dump SAM data of remote NT PDC")
7960 N_("Sync a remote NT PDC's data into local passdb"),
7961 N_("net rpc vampire\n"
7962 " Sync a remote NT PDC's data into local passdb")
7968 N_("Fetch the domain sid into local secrets.tdb"),
7969 N_("net rpc getsid\n"
7970 " Fetch the domain sid into local secrets.tdb")
7976 N_("Manage privileges assigned to SID"),
7977 N_("net rpc rights\n"
7978 " Manage privileges assigned to SID")
7984 N_("Start/stop/query remote services"),
7985 N_("net rpc service\n"
7986 " Start/stop/query remote services")
7992 N_("Manage registry hives"),
7993 N_("net rpc registry\n"
7994 " Manage registry hives")
8000 N_("Open interactive shell on remote server"),
8001 N_("net rpc shell\n"
8002 " Open interactive shell on remote server")
8008 N_("Manage trusts"),
8009 N_("net rpc trust\n"
8016 N_("Configure a remote samba server"),
8018 " Configure a remote samba server")
8020 {NULL
, NULL
, 0, NULL
, NULL
}
8023 status
= libnetapi_net_init(&c
->netapi_ctx
);
8027 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
8028 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
8029 if (c
->opt_kerberos
) {
8030 libnetapi_set_use_kerberos(c
->netapi_ctx
);
8032 if (c
->opt_ccache
) {
8033 libnetapi_set_use_ccache(c
->netapi_ctx
);
8036 return net_run_function(c
, argc
, argv
, "net rpc", func
);