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 strlcpy(new_mask
, dir
, sizeof(new_mask
));
3769 strlcat(new_mask
, "\\*", sizeof(new_mask
));
3771 old_dir
= local_state
->cwd
;
3772 local_state
->cwd
= dir
;
3773 nt_status
= sync_files(local_state
, new_mask
);
3774 if (!NT_STATUS_IS_OK(nt_status
)) {
3775 printf(_("could not handle files\n"));
3777 local_state
->cwd
= old_dir
;
3784 fstrcpy(filename
, local_state
->cwd
);
3785 fstrcat(filename
, "\\");
3786 fstrcat(filename
, f
->name
);
3788 DEBUG(3,("got file: %s\n", filename
));
3790 switch (net_mode_share
)
3792 case NET_MODE_SHARE_MIGRATE
:
3793 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3794 local_state
->cli_share_src
,
3795 local_state
->cli_share_dst
,
3797 c
->opt_acls
? true : false,
3798 c
->opt_attrs
? true : false,
3799 c
->opt_timestamps
? true: false,
3803 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3805 return NT_STATUS_INTERNAL_ERROR
;
3808 if (!NT_STATUS_IS_OK(nt_status
))
3809 printf(_("could not handle file %s: %s\n"),
3810 filename
, nt_errstr(nt_status
));
3815 * sync files, can be called recursivly to list files
3816 * and then call copy_fn for each file
3818 * @param cp_clistate pointer to the copy_clistate we work with
3819 * @param mask the current search mask
3821 * @return Boolean result
3823 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3825 struct cli_state
*targetcli
;
3826 char *targetpath
= NULL
;
3829 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3831 status
= cli_resolve_path(talloc_tos(), "", NULL
,
3832 cp_clistate
->cli_share_src
,
3833 mask
, &targetcli
, &targetpath
);
3834 if (!NT_STATUS_IS_OK(status
)) {
3835 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3837 mask
, nt_errstr(status
));
3841 status
= cli_list(targetcli
, targetpath
, cp_clistate
->attribute
,
3842 copy_fn
, cp_clistate
);
3843 if (!NT_STATUS_IS_OK(status
)) {
3844 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3845 mask
, nt_errstr(status
));
3853 * Set the top level directory permissions before we do any further copies.
3854 * Should set up ACL inheritance.
3857 bool copy_top_level_perms(struct net_context
*c
,
3858 struct copy_clistate
*cp_clistate
,
3859 const char *sharename
)
3861 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3863 switch (net_mode_share
) {
3864 case NET_MODE_SHARE_MIGRATE
:
3865 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3866 nt_status
= net_copy_fileattr(c
,
3867 cp_clistate
->mem_ctx
,
3868 cp_clistate
->cli_share_src
,
3869 cp_clistate
->cli_share_dst
,
3871 c
->opt_acls
? true : false,
3872 c
->opt_attrs
? true : false,
3873 c
->opt_timestamps
? true: false,
3877 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3881 if (!NT_STATUS_IS_OK(nt_status
)) {
3882 printf(_("Could handle directory attributes for top level "
3883 "directory of share %s. Error %s\n"),
3884 sharename
, nt_errstr(nt_status
));
3892 * Sync all files inside a remote share to another share (over smb).
3894 * All parameters are provided by the run_rpc_command function, except for
3895 * argc, argv which are passed through.
3897 * @param domain_sid The domain sid acquired from the remote server.
3898 * @param cli A cli_state connected to the server.
3899 * @param mem_ctx Talloc context, destroyed on completion of the function.
3900 * @param argc Standard main() style argc.
3901 * @param argv Standard main() style argv. Initial components are already
3904 * @return Normal NTSTATUS return.
3907 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3908 const struct dom_sid
*domain_sid
,
3909 const char *domain_name
,
3910 struct cli_state
*cli
,
3911 struct rpc_pipe_client
*pipe_hnd
,
3912 TALLOC_CTX
*mem_ctx
,
3917 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3918 struct srvsvc_NetShareInfoCtr ctr_src
;
3921 struct copy_clistate cp_clistate
;
3922 bool got_src_share
= false;
3923 bool got_dst_share
= false;
3924 const char *mask
= "\\*";
3927 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3929 nt_status
= NT_STATUS_NO_MEMORY
;
3933 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3936 if (!W_ERROR_IS_OK(result
))
3939 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3941 struct srvsvc_NetShareInfo502 info502
=
3942 ctr_src
.ctr
.ctr502
->array
[i
];
3944 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3947 /* one might not want to mirror whole discs :) */
3948 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3949 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3954 switch (net_mode_share
)
3956 case NET_MODE_SHARE_MIGRATE
:
3960 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3964 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3967 c
->opt_acls
? _("including") : _("without"),
3968 c
->opt_attrs
? _("including") : _("without"),
3969 c
->opt_timestamps
? _("(preserving timestamps)") : "");
3971 cp_clistate
.mem_ctx
= mem_ctx
;
3972 cp_clistate
.cli_share_src
= NULL
;
3973 cp_clistate
.cli_share_dst
= NULL
;
3974 cp_clistate
.cwd
= NULL
;
3975 cp_clistate
.attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
;
3978 /* open share source */
3979 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3980 cli_state_remote_sockaddr(cli
),
3981 cli_state_remote_name(cli
),
3982 info502
.name
, "A:");
3983 if (!NT_STATUS_IS_OK(nt_status
))
3986 got_src_share
= true;
3988 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3989 /* open share destination */
3990 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3991 NULL
, dst
, info502
.name
, "A:");
3992 if (!NT_STATUS_IS_OK(nt_status
))
3995 got_dst_share
= true;
3998 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3999 d_fprintf(stderr
, _("Could not handle the top level "
4000 "directory permissions for the "
4001 "share: %s\n"), info502
.name
);
4002 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4006 nt_status
= sync_files(&cp_clistate
, mask
);
4007 if (!NT_STATUS_IS_OK(nt_status
)) {
4008 d_fprintf(stderr
, _("could not handle files for share: "
4009 "%s\n"), info502
.name
);
4014 nt_status
= NT_STATUS_OK
;
4019 cli_shutdown(cp_clistate
.cli_share_src
);
4022 cli_shutdown(cp_clistate
.cli_share_dst
);
4029 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
4031 if (c
->display_usage
) {
4033 "net share migrate files\n"
4036 _("Migrate files to local server"));
4041 d_printf(_("no server to migrate\n"));
4045 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4046 rpc_share_migrate_files_internals
,
4051 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4053 * All parameters are provided by the run_rpc_command function, except for
4054 * argc, argv which are passed through.
4056 * @param domain_sid The domain sid acquired from the remote server.
4057 * @param cli A cli_state connected to the server.
4058 * @param mem_ctx Talloc context, destroyed on completion of the function.
4059 * @param argc Standard main() style argc.
4060 * @param argv Standard main() style argv. Initial components are already
4063 * @return Normal NTSTATUS return.
4066 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
4067 const struct dom_sid
*domain_sid
,
4068 const char *domain_name
,
4069 struct cli_state
*cli
,
4070 struct rpc_pipe_client
*pipe_hnd
,
4071 TALLOC_CTX
*mem_ctx
,
4076 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
4077 struct srvsvc_NetShareInfoCtr ctr_src
;
4078 union srvsvc_NetShareInfo info
;
4080 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
4081 struct cli_state
*cli_dst
= NULL
;
4082 uint32 level
= 502; /* includes secdesc */
4083 uint32_t parm_error
= 0;
4084 struct dcerpc_binding_handle
*b
;
4086 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
4089 if (!W_ERROR_IS_OK(result
))
4092 /* connect destination PI_SRVSVC */
4093 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
4094 &ndr_table_srvsvc
.syntax_id
);
4095 if (!NT_STATUS_IS_OK(nt_status
))
4098 b
= srvsvc_pipe
->binding_handle
;
4100 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
4102 struct srvsvc_NetShareInfo502 info502
=
4103 ctr_src
.ctr
.ctr502
->array
[i
];
4105 /* reset error-code */
4106 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4108 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
4111 printf(_("migrating: [%s], path: %s, comment: %s, including "
4113 info502
.name
, info502
.path
, info502
.comment
);
4116 display_sec_desc(info502
.sd_buf
.sd
);
4118 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4119 info
.info502
= &info502
;
4121 /* finally modify the share on the dst server */
4122 nt_status
= dcerpc_srvsvc_NetShareSetInfo(b
, mem_ctx
,
4123 srvsvc_pipe
->desthost
,
4129 if (!NT_STATUS_IS_OK(nt_status
)) {
4130 printf(_("cannot set share-acl: %s\n"),
4131 nt_errstr(nt_status
));
4134 if (!W_ERROR_IS_OK(result
)) {
4135 nt_status
= werror_to_ntstatus(result
);
4136 printf(_("cannot set share-acl: %s\n"),
4137 win_errstr(result
));
4143 nt_status
= NT_STATUS_OK
;
4147 cli_shutdown(cli_dst
);
4155 * Migrate share-acls from a RPC server to another.
4157 * @param argc Standard main() style argc.
4158 * @param argv Standard main() style argv. Initial components are already
4161 * @return A shell status integer (0 for success).
4163 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
4166 if (c
->display_usage
) {
4168 "net rpc share migrate security\n"
4171 _("Migrate share-acls to local server"));
4176 d_printf(_("no server to migrate\n"));
4180 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4181 rpc_share_migrate_security_internals
,
4186 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4187 * from one server to another.
4189 * @param argc Standard main() style argc.
4190 * @param argv Standard main() style argv. Initial components are already
4193 * @return A shell status integer (0 for success).
4196 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
4201 if (c
->display_usage
) {
4203 "net rpc share migrate all\n"
4206 _("Migrates shares including all share settings"));
4211 d_printf(_("no server to migrate\n"));
4215 /* order is important. we don't want to be locked out by the share-acl
4216 * before copying files - gd */
4218 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4219 rpc_share_migrate_shares_internals
, argc
, argv
);
4223 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4224 rpc_share_migrate_files_internals
, argc
, argv
);
4228 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
4229 rpc_share_migrate_security_internals
, argc
,
4235 * 'net rpc share migrate' entrypoint.
4236 * @param argc Standard main() style argc.
4237 * @param argv Standard main() style argv. Initial components are already
4240 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4243 struct functable func
[] = {
4246 rpc_share_migrate_all
,
4248 N_("Migrate shares from remote to local server"),
4249 N_("net rpc share migrate all\n"
4250 " Migrate shares from remote to local server")
4254 rpc_share_migrate_files
,
4256 N_("Migrate files from remote to local server"),
4257 N_("net rpc share migrate files\n"
4258 " Migrate files from remote to local server")
4262 rpc_share_migrate_security
,
4264 N_("Migrate share-ACLs from remote to local server"),
4265 N_("net rpc share migrate security\n"
4266 " Migrate share-ACLs from remote to local server")
4270 rpc_share_migrate_shares
,
4272 N_("Migrate shares from remote to local server"),
4273 N_("net rpc share migrate shares\n"
4274 " Migrate shares from remote to local server")
4276 {NULL
, NULL
, 0, NULL
, NULL
}
4279 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4281 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
4287 struct dom_sid
*members
;
4290 static int num_server_aliases
;
4291 static struct full_alias
*server_aliases
;
4294 * Add an alias to the static list.
4296 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4298 if (server_aliases
== NULL
)
4299 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4301 server_aliases
[num_server_aliases
] = *alias
;
4302 num_server_aliases
+= 1;
4306 * For a specific domain on the server, fetch all the aliases
4307 * and their members. Add all of them to the server_aliases.
4310 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4311 TALLOC_CTX
*mem_ctx
,
4312 struct policy_handle
*connect_pol
,
4313 const struct dom_sid
*domain_sid
)
4315 uint32 start_idx
, max_entries
, num_entries
, i
;
4316 struct samr_SamArray
*groups
= NULL
;
4317 NTSTATUS result
, status
;
4318 struct policy_handle domain_pol
;
4319 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4321 /* Get domain policy handle */
4323 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
4325 MAXIMUM_ALLOWED_ACCESS
,
4326 discard_const_p(struct dom_sid2
, domain_sid
),
4329 if (!NT_STATUS_IS_OK(status
)) {
4332 if (!NT_STATUS_IS_OK(result
)) {
4340 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
4347 if (!NT_STATUS_IS_OK(status
)) {
4350 for (i
= 0; i
< num_entries
; i
++) {
4352 struct policy_handle alias_pol
;
4353 struct full_alias alias
;
4354 struct lsa_SidArray sid_array
;
4358 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
4360 MAXIMUM_ALLOWED_ACCESS
,
4361 groups
->entries
[i
].idx
,
4364 if (!NT_STATUS_IS_OK(status
)) {
4367 if (!NT_STATUS_IS_OK(_result
)) {
4372 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
4376 if (!NT_STATUS_IS_OK(status
)) {
4379 if (!NT_STATUS_IS_OK(_result
)) {
4384 alias
.num_members
= sid_array
.num_sids
;
4386 status
= dcerpc_samr_Close(b
, mem_ctx
, &alias_pol
, &_result
);
4387 if (!NT_STATUS_IS_OK(status
)) {
4390 if (!NT_STATUS_IS_OK(_result
)) {
4395 alias
.members
= NULL
;
4397 if (alias
.num_members
> 0) {
4398 alias
.members
= SMB_MALLOC_ARRAY(struct dom_sid
, alias
.num_members
);
4400 for (j
= 0; j
< alias
.num_members
; j
++)
4401 sid_copy(&alias
.members
[j
],
4402 sid_array
.sids
[j
].sid
);
4405 sid_compose(&alias
.sid
, domain_sid
,
4406 groups
->entries
[i
].idx
);
4408 push_alias(mem_ctx
, &alias
);
4410 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4412 status
= NT_STATUS_OK
;
4415 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
4421 * Dump server_aliases as names for debugging purposes.
4424 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4425 const struct dom_sid
*domain_sid
,
4426 const char *domain_name
,
4427 struct cli_state
*cli
,
4428 struct rpc_pipe_client
*pipe_hnd
,
4429 TALLOC_CTX
*mem_ctx
,
4435 struct policy_handle lsa_pol
;
4436 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4438 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4439 SEC_FLAG_MAXIMUM_ALLOWED
,
4441 if (!NT_STATUS_IS_OK(result
))
4444 for (i
=0; i
<num_server_aliases
; i
++) {
4447 enum lsa_SidType
*types
;
4450 struct full_alias
*alias
= &server_aliases
[i
];
4452 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4454 &domains
, &names
, &types
);
4455 if (!NT_STATUS_IS_OK(result
))
4458 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4460 if (alias
->num_members
== 0) {
4465 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4468 &domains
, &names
, &types
);
4470 if (!NT_STATUS_IS_OK(result
) &&
4471 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4474 for (j
=0; j
<alias
->num_members
; j
++)
4475 DEBUG(1, ("%s\\%s (%d); ",
4476 domains
[j
] ? domains
[j
] : "*unknown*",
4477 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4481 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
4483 return NT_STATUS_OK
;
4487 * Fetch a list of all server aliases and their members into
4491 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4492 const struct dom_sid
*domain_sid
,
4493 const char *domain_name
,
4494 struct cli_state
*cli
,
4495 struct rpc_pipe_client
*pipe_hnd
,
4496 TALLOC_CTX
*mem_ctx
,
4500 NTSTATUS result
, status
;
4501 struct policy_handle connect_pol
;
4502 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4504 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
4506 MAXIMUM_ALLOWED_ACCESS
,
4509 if (!NT_STATUS_IS_OK(status
)) {
4512 if (!NT_STATUS_IS_OK(result
)) {
4517 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4518 &global_sid_Builtin
);
4519 if (!NT_STATUS_IS_OK(status
)) {
4523 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4526 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
4531 static void init_user_token(struct security_token
*token
, struct dom_sid
*user_sid
)
4533 token
->num_sids
= 4;
4535 if (!(token
->sids
= SMB_MALLOC_ARRAY(struct dom_sid
, 4))) {
4536 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4537 token
->num_sids
= 0;
4541 token
->sids
[0] = *user_sid
;
4542 sid_copy(&token
->sids
[1], &global_sid_World
);
4543 sid_copy(&token
->sids
[2], &global_sid_Network
);
4544 sid_copy(&token
->sids
[3], &global_sid_Authenticated_Users
);
4547 static void free_user_token(struct security_token
*token
)
4549 SAFE_FREE(token
->sids
);
4552 static void add_sid_to_token(struct security_token
*token
, struct dom_sid
*sid
)
4554 if (security_token_has_sid(token
, sid
))
4557 token
->sids
= SMB_REALLOC_ARRAY(token
->sids
, struct dom_sid
, token
->num_sids
+1);
4562 sid_copy(&token
->sids
[token
->num_sids
], sid
);
4564 token
->num_sids
+= 1;
4569 struct security_token token
;
4572 static void dump_user_token(struct user_token
*token
)
4576 d_printf("%s\n", token
->name
);
4578 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4579 d_printf(" %s\n", sid_string_tos(&token
->token
.sids
[i
]));
4583 static bool is_alias_member(struct dom_sid
*sid
, struct full_alias
*alias
)
4587 for (i
=0; i
<alias
->num_members
; i
++) {
4588 if (dom_sid_compare(sid
, &alias
->members
[i
]) == 0)
4595 static void collect_sid_memberships(struct security_token
*token
, struct dom_sid sid
)
4599 for (i
=0; i
<num_server_aliases
; i
++) {
4600 if (is_alias_member(&sid
, &server_aliases
[i
]))
4601 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4606 * We got a user token with all the SIDs we can know about without asking the
4607 * server directly. These are the user and domain group sids. All of these can
4608 * be members of aliases. So scan the list of aliases for each of the SIDs and
4609 * add them to the token.
4612 static void collect_alias_memberships(struct security_token
*token
)
4614 int num_global_sids
= token
->num_sids
;
4617 for (i
=0; i
<num_global_sids
; i
++) {
4618 collect_sid_memberships(token
, token
->sids
[i
]);
4622 static bool get_user_sids(const char *domain
, const char *user
, struct security_token
*token
)
4624 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4625 enum wbcSidType type
;
4627 struct wbcDomainSid wsid
;
4628 char sid_str
[WBC_SID_STRING_BUFLEN
];
4629 struct dom_sid user_sid
;
4630 uint32_t num_groups
;
4631 gid_t
*groups
= NULL
;
4634 fstr_sprintf(full_name
, "%s%c%s",
4635 domain
, *lp_winbind_separator(), user
);
4637 /* First let's find out the user sid */
4639 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4641 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4642 DEBUG(1, ("winbind could not find %s: %s\n",
4643 full_name
, wbcErrorString(wbc_status
)));
4647 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4649 if (type
!= WBC_SID_NAME_USER
) {
4650 DEBUG(1, ("%s is not a user\n", full_name
));
4654 if (!string_to_sid(&user_sid
, sid_str
)) {
4655 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4659 init_user_token(token
, &user_sid
);
4661 /* And now the groups winbind knows about */
4663 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4664 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4665 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4666 full_name
, wbcErrorString(wbc_status
)));
4670 for (i
= 0; i
< num_groups
; i
++) {
4671 gid_t gid
= groups
[i
];
4674 wbc_status
= wbcGidToSid(gid
, &wsid
);
4675 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4676 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4677 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4678 wbcFreeMemory(groups
);
4682 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4684 DEBUG(3, (" %s\n", sid_str
));
4686 string_to_sid(&sid
, sid_str
);
4687 add_sid_to_token(token
, &sid
);
4689 wbcFreeMemory(groups
);
4695 * Get a list of all user tokens we want to look at
4698 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4699 struct user_token
**user_tokens
)
4701 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4702 uint32_t i
, num_users
;
4704 struct user_token
*result
;
4705 TALLOC_CTX
*frame
= NULL
;
4707 if (lp_winbind_use_default_domain() &&
4708 (c
->opt_target_workgroup
== NULL
)) {
4709 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4710 "please specify a workgroup\n"));
4714 /* Send request to winbind daemon */
4716 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4717 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4718 DEBUG(1, (_("winbind could not list users: %s\n"),
4719 wbcErrorString(wbc_status
)));
4723 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4725 if (result
== NULL
) {
4726 DEBUG(1, ("Could not malloc sid array\n"));
4727 wbcFreeMemory(users
);
4731 frame
= talloc_stackframe();
4732 for (i
=0; i
< num_users
; i
++) {
4733 fstring domain
, user
;
4736 fstrcpy(result
[i
].name
, users
[i
]);
4738 p
= strchr(users
[i
], *lp_winbind_separator());
4740 DEBUG(3, ("%s\n", users
[i
]));
4743 fstrcpy(domain
, c
->opt_target_workgroup
);
4744 fstrcpy(user
, users
[i
]);
4747 fstrcpy(domain
, users
[i
]);
4752 get_user_sids(domain
, user
, &(result
[i
].token
));
4755 wbcFreeMemory(users
);
4757 *num_tokens
= num_users
;
4758 *user_tokens
= result
;
4763 static bool get_user_tokens_from_file(FILE *f
,
4765 struct user_token
**tokens
)
4767 struct user_token
*token
= NULL
;
4772 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4776 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4777 line
[strlen(line
)-1] = '\0';
4780 if (line
[0] == ' ') {
4784 if(!string_to_sid(&sid
, &line
[1])) {
4785 DEBUG(1,("get_user_tokens_from_file: Could "
4786 "not convert sid %s \n",&line
[1]));
4790 if (token
== NULL
) {
4791 DEBUG(0, ("File does not begin with username"));
4795 add_sid_to_token(&token
->token
, &sid
);
4799 /* And a new user... */
4802 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4803 if (*tokens
== NULL
) {
4804 DEBUG(0, ("Could not realloc tokens\n"));
4808 token
= &((*tokens
)[*num_tokens
-1]);
4810 strlcpy(token
->name
, line
, sizeof(token
->name
));
4811 token
->token
.num_sids
= 0;
4812 token
->token
.sids
= NULL
;
4821 * Show the list of all users that have access to a share
4824 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4825 TALLOC_CTX
*mem_ctx
,
4826 const char *netname
,
4828 struct user_token
*tokens
)
4831 struct security_descriptor
*share_sd
= NULL
;
4832 struct security_descriptor
*root_sd
= NULL
;
4833 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4835 union srvsvc_NetShareInfo info
;
4839 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4841 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
4848 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4849 DEBUG(1, ("Coult not query secdesc for share %s\n",
4854 share_sd
= info
.info502
->sd_buf
.sd
;
4855 if (share_sd
== NULL
) {
4856 DEBUG(1, ("Got no secdesc for share %s\n",
4860 cnum
= cli_state_get_tid(cli
);
4862 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, netname
, "A:", "", 0))) {
4866 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4867 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4868 cli_query_secdesc(cli
, fnum
, mem_ctx
, &root_sd
);
4871 for (i
=0; i
<num_tokens
; i
++) {
4874 if (share_sd
!= NULL
) {
4875 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4878 if (!NT_STATUS_IS_OK(status
)) {
4879 DEBUG(1, ("Could not check share_sd for "
4886 if (root_sd
== NULL
) {
4887 d_printf(" %s\n", tokens
[i
].name
);
4891 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4893 if (!NT_STATUS_IS_OK(status
)) {
4894 DEBUG(1, ("Could not check root_sd for user %s\n",
4898 d_printf(" %s\n", tokens
[i
].name
);
4901 if (fnum
!= (uint16_t)-1)
4902 cli_close(cli
, fnum
);
4904 cli_state_set_tid(cli
, cnum
);
4914 static void collect_share(const char *name
, uint32 m
,
4915 const char *comment
, void *state
)
4917 struct share_list
*share_list
= (struct share_list
*)state
;
4919 if (m
!= STYPE_DISKTREE
)
4922 share_list
->num_shares
+= 1;
4923 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4924 if (!share_list
->shares
) {
4925 share_list
->num_shares
= 0;
4928 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4932 * List shares on a remote RPC server, including the security descriptors.
4934 * All parameters are provided by the run_rpc_command function, except for
4935 * argc, argv which are passed through.
4937 * @param domain_sid The domain sid acquired from the remote server.
4938 * @param cli A cli_state connected to the server.
4939 * @param mem_ctx Talloc context, destroyed on completion of the function.
4940 * @param argc Standard main() style argc.
4941 * @param argv Standard main() style argv. Initial components are already
4944 * @return Normal NTSTATUS return.
4947 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4948 const struct dom_sid
*domain_sid
,
4949 const char *domain_name
,
4950 struct cli_state
*cli
,
4951 struct rpc_pipe_client
*pipe_hnd
,
4952 TALLOC_CTX
*mem_ctx
,
4961 struct user_token
*tokens
= NULL
;
4964 struct share_list share_list
;
4969 f
= fopen(argv
[0], "r");
4973 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4974 return NT_STATUS_UNSUCCESSFUL
;
4977 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4983 DEBUG(0, ("Could not read users from file\n"));
4984 return NT_STATUS_UNSUCCESSFUL
;
4987 for (i
=0; i
<num_tokens
; i
++)
4988 collect_alias_memberships(&tokens
[i
].token
);
4990 share_list
.num_shares
= 0;
4991 share_list
.shares
= NULL
;
4993 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4996 DEBUG(0, ("Error returning browse list: %s\n",
5001 for (i
= 0; i
< share_list
.num_shares
; i
++) {
5002 char *netname
= share_list
.shares
[i
];
5004 if (netname
[strlen(netname
)-1] == '$')
5007 d_printf("%s\n", netname
);
5009 show_userlist(pipe_hnd
, mem_ctx
, netname
,
5010 num_tokens
, tokens
);
5013 for (i
=0; i
<num_tokens
; i
++) {
5014 free_user_token(&tokens
[i
].token
);
5017 SAFE_FREE(share_list
.shares
);
5019 return NT_STATUS_OK
;
5022 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
5027 if (c
->display_usage
) {
5029 "net rpc share allowedusers\n"
5032 _("List allowed users"));
5036 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5037 rpc_aliaslist_internals
,
5042 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
, 0,
5048 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
, 0,
5049 rpc_share_allowedusers_internals
,
5053 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
5056 struct user_token
*tokens
= NULL
;
5060 net_usersidlist_usage(c
, argc
, argv
);
5064 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
5065 DEBUG(0, ("Could not get the user/sid list\n"));
5069 for (i
=0; i
<num_tokens
; i
++) {
5070 dump_user_token(&tokens
[i
]);
5071 free_user_token(&tokens
[i
].token
);
5078 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
5080 d_printf(_("net usersidlist\n"
5081 "\tprints out a list of all users the running winbind knows\n"
5082 "\tabout, together with all their SIDs. This is used as\n"
5083 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5085 net_common_flags_usage(c
, argc
, argv
);
5090 * 'net rpc share' entrypoint.
5091 * @param argc Standard main() style argc.
5092 * @param argv Standard main() style argv. Initial components are already
5096 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
5098 NET_API_STATUS status
;
5100 struct functable func
[] = {
5106 N_("net rpc share add\n"
5114 N_("net rpc share delete\n"
5119 rpc_share_allowedusers
,
5121 N_("Modify allowed users"),
5122 N_("net rpc share allowedusers\n"
5123 " Modify allowed users")
5129 N_("Migrate share to local server"),
5130 N_("net rpc share migrate\n"
5131 " Migrate share to local server")
5138 N_("net rpc share list\n"
5141 {NULL
, NULL
, 0, NULL
, NULL
}
5144 status
= libnetapi_net_init(&c
->netapi_ctx
);
5148 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5149 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5150 if (c
->opt_kerberos
) {
5151 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5155 if (c
->display_usage
) {
5160 " Alias for net rpc share list\n"));
5161 net_display_usage_from_functable(func
);
5165 return rpc_share_list(c
, argc
, argv
);
5168 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5171 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5172 TALLOC_CTX
*mem_ctx
,
5173 struct rpc_sh_ctx
*ctx
,
5174 struct rpc_pipe_client
*pipe_hnd
,
5175 int argc
, const char **argv
)
5178 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
5181 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
5182 TALLOC_CTX
*mem_ctx
,
5183 struct rpc_sh_ctx
*ctx
,
5184 struct rpc_pipe_client
*pipe_hnd
,
5185 int argc
, const char **argv
)
5187 NET_API_STATUS status
;
5188 uint32_t parm_err
= 0;
5189 struct SHARE_INFO_2 i2
;
5191 if ((argc
< 2) || (argc
> 3)) {
5192 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
5194 return NT_STATUS_INVALID_PARAMETER
;
5197 i2
.shi2_netname
= argv
[0];
5198 i2
.shi2_type
= STYPE_DISKTREE
;
5199 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
5200 i2
.shi2_permissions
= 0;
5201 i2
.shi2_max_uses
= 0;
5202 i2
.shi2_current_uses
= 0;
5203 i2
.shi2_path
= argv
[1];
5204 i2
.shi2_passwd
= NULL
;
5206 status
= NetShareAdd(pipe_hnd
->desthost
,
5211 return werror_to_ntstatus(W_ERROR(status
));
5214 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5215 TALLOC_CTX
*mem_ctx
,
5216 struct rpc_sh_ctx
*ctx
,
5217 struct rpc_pipe_client
*pipe_hnd
,
5218 int argc
, const char **argv
)
5221 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5222 return NT_STATUS_INVALID_PARAMETER
;
5225 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
5228 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5229 TALLOC_CTX
*mem_ctx
,
5230 struct rpc_sh_ctx
*ctx
,
5231 struct rpc_pipe_client
*pipe_hnd
,
5232 int argc
, const char **argv
)
5234 union srvsvc_NetShareInfo info
;
5237 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5240 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5241 return NT_STATUS_INVALID_PARAMETER
;
5244 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
5250 if (!NT_STATUS_IS_OK(status
)) {
5251 result
= ntstatus_to_werror(status
);
5254 if (!W_ERROR_IS_OK(result
)) {
5258 d_printf(_("Name: %s\n"), info
.info2
->name
);
5259 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
5260 d_printf(_("Path: %s\n"), info
.info2
->path
);
5261 d_printf(_("Password: %s\n"), info
.info2
->password
);
5264 return werror_to_ntstatus(result
);
5267 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5268 struct rpc_sh_ctx
*ctx
)
5270 static struct rpc_sh_cmd cmds
[] = {
5272 { "list", NULL
, &ndr_table_srvsvc
, rpc_sh_share_list
,
5273 N_("List available shares") },
5275 { "add", NULL
, &ndr_table_srvsvc
, rpc_sh_share_add
,
5276 N_("Add a share") },
5278 { "delete", NULL
, &ndr_table_srvsvc
, rpc_sh_share_delete
,
5279 N_("Delete a share") },
5281 { "info", NULL
, &ndr_table_srvsvc
, rpc_sh_share_info
,
5282 N_("Get information about a share") },
5284 { NULL
, NULL
, 0, NULL
, NULL
}
5290 /****************************************************************************/
5292 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5294 return net_file_usage(c
, argc
, argv
);
5298 * Close a file on a remote RPC server.
5300 * @param argc Standard main() style argc.
5301 * @param argv Standard main() style argv. Initial components are already
5304 * @return A shell status integer (0 for success).
5306 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5308 if (argc
< 1 || c
->display_usage
) {
5309 return rpc_file_usage(c
, argc
, argv
);
5312 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
5316 * Formatted print of open file info
5318 * @param r struct FILE_INFO_3 contents
5321 static void display_file_info_3(struct FILE_INFO_3
*r
)
5323 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5324 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
5325 r
->fi3_num_locks
, r
->fi3_pathname
);
5329 * List files for a user on a remote RPC server.
5331 * @param argc Standard main() style argc.
5332 * @param argv Standard main() style argv. Initial components are already
5335 * @return A shell status integer (0 for success)..
5338 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5340 NET_API_STATUS status
;
5341 uint32 preferred_len
= 0xffffffff, i
;
5342 const char *username
=NULL
;
5343 uint32_t total_entries
= 0;
5344 uint32_t entries_read
= 0;
5345 uint32_t resume_handle
= 0;
5346 struct FILE_INFO_3
*i3
= NULL
;
5348 if (c
->display_usage
) {
5349 return rpc_file_usage(c
, argc
, argv
);
5352 /* if argc > 0, must be user command */
5354 username
= smb_xstrdup(argv
[0]);
5357 status
= NetFileEnum(c
->opt_host
,
5361 (uint8_t **)(void *)&i3
,
5371 /* Display results */
5374 "\nEnumerating open files on remote server:\n\n"
5375 "\nFileId Opened by Perms Locks Path"
5376 "\n------ --------- ----- ----- ---- \n"));
5377 for (i
= 0; i
< entries_read
; i
++) {
5378 display_file_info_3(&i3
[i
]);
5385 * 'net rpc file' entrypoint.
5386 * @param argc Standard main() style argc.
5387 * @param argv Standard main() style argv. Initial components are already
5391 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5393 NET_API_STATUS status
;
5395 struct functable func
[] = {
5400 N_("Close opened file"),
5401 N_("net rpc file close\n"
5402 " Close opened file")
5408 N_("List files opened by user"),
5409 N_("net rpc file user\n"
5410 " List files opened by user")
5417 N_("Display information about opened file"),
5418 N_("net rpc file info\n"
5419 " Display information about opened file")
5422 {NULL
, NULL
, 0, NULL
, NULL
}
5425 status
= libnetapi_net_init(&c
->netapi_ctx
);
5429 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5430 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5431 if (c
->opt_kerberos
) {
5432 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5436 if (c
->display_usage
) {
5437 d_printf(_("Usage:\n"));
5438 d_printf(_("net rpc file\n"
5439 " List opened files\n"));
5440 net_display_usage_from_functable(func
);
5444 return rpc_file_user(c
, argc
, argv
);
5447 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5451 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5453 * All parameters are provided by the run_rpc_command function, except for
5454 * argc, argv which are passed through.
5456 * @param c A net_context structure.
5457 * @param domain_sid The domain sid acquired from the remote server.
5458 * @param cli A cli_state connected to the server.
5459 * @param mem_ctx Talloc context, destroyed on completion of the function.
5460 * @param argc Standard main() style argc.
5461 * @param argv Standard main() style argv. Initial components are already
5464 * @return Normal NTSTATUS return.
5467 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5468 const struct dom_sid
*domain_sid
,
5469 const char *domain_name
,
5470 struct cli_state
*cli
,
5471 struct rpc_pipe_client
*pipe_hnd
,
5472 TALLOC_CTX
*mem_ctx
,
5476 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5478 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5480 status
= dcerpc_initshutdown_Abort(b
, mem_ctx
, NULL
, &result
);
5481 if (!NT_STATUS_IS_OK(status
)) {
5484 if (W_ERROR_IS_OK(result
)) {
5485 d_printf(_("\nShutdown successfully aborted\n"));
5486 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5488 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5490 return werror_to_ntstatus(result
);
5494 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5496 * All parameters are provided by the run_rpc_command function, except for
5497 * argc, argv which are passed through.
5499 * @param c A net_context structure.
5500 * @param domain_sid The domain sid acquired from the remote server.
5501 * @param cli A cli_state connected to the server.
5502 * @param mem_ctx Talloc context, destroyed on completion of the function.
5503 * @param argc Standard main() style argc.
5504 * @param argv Standard main() style argv. Initial components are already
5507 * @return Normal NTSTATUS return.
5510 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5511 const struct dom_sid
*domain_sid
,
5512 const char *domain_name
,
5513 struct cli_state
*cli
,
5514 struct rpc_pipe_client
*pipe_hnd
,
5515 TALLOC_CTX
*mem_ctx
,
5519 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5521 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5523 result
= dcerpc_winreg_AbortSystemShutdown(b
, mem_ctx
, NULL
, &werr
);
5525 if (!NT_STATUS_IS_OK(result
)) {
5526 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5529 if (W_ERROR_IS_OK(werr
)) {
5530 d_printf(_("\nShutdown successfully aborted\n"));
5531 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5533 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5535 return werror_to_ntstatus(werr
);
5539 * ABORT the shutdown of a remote RPC server.
5541 * @param argc Standard main() style argc.
5542 * @param argv Standard main() style argv. Initial components are already
5545 * @return A shell status integer (0 for success).
5548 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5553 if (c
->display_usage
) {
5555 "net rpc abortshutdown\n"
5558 _("Abort a scheduled shutdown"));
5562 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5563 rpc_shutdown_abort_internals
, argc
, argv
);
5568 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5570 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5571 rpc_reg_shutdown_abort_internals
,
5576 * Shut down a remote RPC Server via initshutdown pipe.
5578 * All parameters are provided by the run_rpc_command function, except for
5579 * argc, argv which are passed through.
5581 * @param c A net_context structure.
5582 * @param domain_sid The domain sid acquired from the remote server.
5583 * @param cli A cli_state connected to the server.
5584 * @param mem_ctx Talloc context, destroyed on completion of the function.
5585 * @param argc Standard main() style argc.
5586 * @param argv Standard main() style argv. Initial components are already
5589 * @return Normal NTSTATUS return.
5592 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5593 const struct dom_sid
*domain_sid
,
5594 const char *domain_name
,
5595 struct cli_state
*cli
,
5596 struct rpc_pipe_client
*pipe_hnd
,
5597 TALLOC_CTX
*mem_ctx
,
5601 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5603 const char *msg
= N_("This machine will be shutdown shortly");
5604 uint32 timeout
= 20;
5605 struct lsa_StringLarge msg_string
;
5606 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5608 if (c
->opt_comment
) {
5609 msg
= c
->opt_comment
;
5611 if (c
->opt_timeout
) {
5612 timeout
= c
->opt_timeout
;
5615 msg_string
.string
= msg
;
5617 /* create an entry */
5618 status
= dcerpc_initshutdown_Init(b
, mem_ctx
, NULL
,
5619 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5621 if (!NT_STATUS_IS_OK(status
)) {
5624 if (W_ERROR_IS_OK(result
)) {
5625 d_printf(_("\nShutdown of remote machine succeeded\n"));
5626 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5628 DEBUG(1,("Shutdown of remote machine failed!\n"));
5630 return werror_to_ntstatus(result
);
5634 * Shut down a remote RPC Server via winreg pipe.
5636 * All parameters are provided by the run_rpc_command function, except for
5637 * argc, argv which are passed through.
5639 * @param c A net_context structure.
5640 * @param domain_sid The domain sid acquired from the remote server.
5641 * @param cli A cli_state connected to the server.
5642 * @param mem_ctx Talloc context, destroyed on completion of the function.
5643 * @param argc Standard main() style argc.
5644 * @param argv Standard main() style argv. Initial components are already
5647 * @return Normal NTSTATUS return.
5650 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5651 const struct dom_sid
*domain_sid
,
5652 const char *domain_name
,
5653 struct cli_state
*cli
,
5654 struct rpc_pipe_client
*pipe_hnd
,
5655 TALLOC_CTX
*mem_ctx
,
5659 const char *msg
= N_("This machine will be shutdown shortly");
5660 uint32 timeout
= 20;
5661 struct lsa_StringLarge msg_string
;
5664 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5666 if (c
->opt_comment
) {
5667 msg
= c
->opt_comment
;
5669 msg_string
.string
= msg
;
5671 if (c
->opt_timeout
) {
5672 timeout
= c
->opt_timeout
;
5675 /* create an entry */
5676 result
= dcerpc_winreg_InitiateSystemShutdown(b
, mem_ctx
, NULL
,
5677 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5679 if (!NT_STATUS_IS_OK(result
)) {
5680 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5684 if (W_ERROR_IS_OK(werr
)) {
5685 d_printf(_("\nShutdown of remote machine succeeded\n"));
5687 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5688 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5689 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5691 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5694 return werror_to_ntstatus(werr
);
5698 * Shut down a remote RPC server.
5700 * @param argc Standard main() style argc.
5701 * @param argv Standard main() style argv. Initial components are already
5704 * @return A shell status integer (0 for success).
5707 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5711 if (c
->display_usage
) {
5713 "net rpc shutdown\n"
5716 _("Shut down a remote RPC server"));
5720 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
, 0,
5721 rpc_init_shutdown_internals
, argc
, argv
);
5724 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5725 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
5726 rpc_reg_shutdown_internals
, argc
, argv
);
5732 /***************************************************************************
5733 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5734 ***************************************************************************/
5737 * Add interdomain trust account to the RPC server.
5738 * All parameters (except for argc and argv) are passed by run_rpc_command
5741 * @param c A net_context structure.
5742 * @param domain_sid The domain sid acquired from the server.
5743 * @param cli A cli_state connected to the server.
5744 * @param mem_ctx Talloc context, destroyed on completion of the function.
5745 * @param argc Standard main() style argc.
5746 * @param argv Standard main() style argv. Initial components are already
5749 * @return normal NTSTATUS return code.
5752 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5753 const struct dom_sid
*domain_sid
,
5754 const char *domain_name
,
5755 struct cli_state
*cli
,
5756 struct rpc_pipe_client
*pipe_hnd
,
5757 TALLOC_CTX
*mem_ctx
,
5761 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5762 NTSTATUS status
, result
;
5764 struct lsa_String lsa_acct_name
;
5766 uint32 acct_flags
=0;
5768 uint32_t access_granted
= 0;
5769 union samr_UserInfo info
;
5770 unsigned int orig_timeout
;
5771 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5776 _(" net rpc trustdom add <domain_name> "
5777 "<trust password>\n"));
5778 return NT_STATUS_INVALID_PARAMETER
;
5782 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5785 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5786 return NT_STATUS_NO_MEMORY
;
5789 strupper_m(acct_name
);
5791 init_lsa_String(&lsa_acct_name
, acct_name
);
5793 /* Get samr policy handle */
5794 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5796 MAXIMUM_ALLOWED_ACCESS
,
5799 if (!NT_STATUS_IS_OK(status
)) {
5802 if (!NT_STATUS_IS_OK(result
)) {
5807 /* Get domain policy handle */
5808 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5810 MAXIMUM_ALLOWED_ACCESS
,
5811 discard_const_p(struct dom_sid2
, domain_sid
),
5814 if (!NT_STATUS_IS_OK(status
)) {
5817 if (!NT_STATUS_IS_OK(result
)) {
5822 /* This call can take a long time - allow the server to time out.
5823 * 35 seconds should do it. */
5825 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5827 /* Create trusting domain's account */
5828 acb_info
= ACB_NORMAL
;
5829 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5830 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5831 SAMR_USER_ACCESS_SET_PASSWORD
|
5832 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5833 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5835 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
5844 if (!NT_STATUS_IS_OK(status
)) {
5847 /* And restore our original timeout. */
5848 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5850 if (!NT_STATUS_IS_OK(result
)) {
5852 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5853 acct_name
, nt_errstr(result
));
5858 struct samr_CryptPassword crypt_pwd
;
5860 ZERO_STRUCT(info
.info23
);
5862 init_samr_CryptPassword(argv
[1],
5863 &cli
->user_session_key
,
5866 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5867 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5868 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5869 info
.info23
.password
= crypt_pwd
;
5871 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
5876 if (!NT_STATUS_IS_OK(status
)) {
5880 if (!NT_STATUS_IS_OK(result
)) {
5882 DEBUG(0,("Could not set trust account password: %s\n",
5883 nt_errstr(result
)));
5889 SAFE_FREE(acct_name
);
5894 * Create interdomain trust account for a remote domain.
5896 * @param argc Standard argc.
5897 * @param argv Standard argv without initial components.
5899 * @return Integer status (0 means success).
5902 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5904 if (argc
> 0 && !c
->display_usage
) {
5905 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
5906 rpc_trustdom_add_internals
, argc
, argv
);
5910 _("net rpc trustdom add <domain_name> <trust "
5918 * Remove interdomain trust account from the RPC server.
5919 * All parameters (except for argc and argv) are passed by run_rpc_command
5922 * @param c A net_context structure.
5923 * @param domain_sid The domain sid acquired from the server.
5924 * @param cli A cli_state connected to the server.
5925 * @param mem_ctx Talloc context, destroyed on completion of the function.
5926 * @param argc Standard main() style argc.
5927 * @param argv Standard main() style argv. Initial components are already
5930 * @return normal NTSTATUS return code.
5933 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5934 const struct dom_sid
*domain_sid
,
5935 const char *domain_name
,
5936 struct cli_state
*cli
,
5937 struct rpc_pipe_client
*pipe_hnd
,
5938 TALLOC_CTX
*mem_ctx
,
5942 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5943 NTSTATUS status
, result
;
5945 struct dom_sid trust_acct_sid
;
5946 struct samr_Ids user_rids
, name_types
;
5947 struct lsa_String lsa_acct_name
;
5948 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5953 _(" net rpc trustdom del <domain_name>\n"));
5954 return NT_STATUS_INVALID_PARAMETER
;
5958 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5960 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5962 if (acct_name
== NULL
)
5963 return NT_STATUS_NO_MEMORY
;
5965 strupper_m(acct_name
);
5967 /* Get samr policy handle */
5968 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5970 MAXIMUM_ALLOWED_ACCESS
,
5973 if (!NT_STATUS_IS_OK(status
)) {
5976 if (!NT_STATUS_IS_OK(result
)) {
5981 /* Get domain policy handle */
5982 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5984 MAXIMUM_ALLOWED_ACCESS
,
5985 discard_const_p(struct dom_sid2
, domain_sid
),
5988 if (!NT_STATUS_IS_OK(status
)) {
5991 if (!NT_STATUS_IS_OK(result
)) {
5996 init_lsa_String(&lsa_acct_name
, acct_name
);
5998 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
6005 if (!NT_STATUS_IS_OK(status
)) {
6006 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6008 acct_name
, nt_errstr(status
));
6011 if (!NT_STATUS_IS_OK(result
)) {
6013 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6015 acct_name
, nt_errstr(result
) );
6019 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
6021 MAXIMUM_ALLOWED_ACCESS
,
6025 if (!NT_STATUS_IS_OK(status
)) {
6026 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6028 acct_name
, nt_errstr(status
) );
6032 if (!NT_STATUS_IS_OK(result
)) {
6034 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6036 acct_name
, nt_errstr(result
) );
6040 /* append the rid to the domain sid */
6041 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
6045 /* remove the sid */
6047 status
= dcerpc_samr_RemoveMemberFromForeignDomain(b
, mem_ctx
,
6051 if (!NT_STATUS_IS_OK(status
)) {
6052 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6053 " on user %s failed %s\n"),
6054 acct_name
, nt_errstr(status
));
6057 if (!NT_STATUS_IS_OK(result
)) {
6059 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6060 " on user %s failed %s\n"),
6061 acct_name
, nt_errstr(result
) );
6068 status
= dcerpc_samr_DeleteUser(b
, mem_ctx
,
6071 if (!NT_STATUS_IS_OK(status
)) {
6072 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6074 acct_name
, nt_errstr(status
));
6078 if (!NT_STATUS_IS_OK(result
)) {
6080 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6082 acct_name
, nt_errstr(result
) );
6086 if (!NT_STATUS_IS_OK(result
)) {
6087 d_printf(_("Could not set trust account password: %s\n"),
6097 * Delete interdomain trust account for a remote domain.
6099 * @param argc Standard argc.
6100 * @param argv Standard argv without initial components.
6102 * @return Integer status (0 means success).
6105 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
6107 if (argc
> 0 && !c
->display_usage
) {
6108 return run_rpc_command(c
, NULL
, &ndr_table_samr
, 0,
6109 rpc_trustdom_del_internals
, argc
, argv
);
6113 _("net rpc trustdom del <domain>\n"));
6118 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
6119 struct cli_state
*cli
,
6120 TALLOC_CTX
*mem_ctx
,
6121 const char *domain_name
)
6123 char *dc_name
= NULL
;
6124 const char *buffer
= NULL
;
6125 struct rpc_pipe_client
*netr
;
6128 struct dcerpc_binding_handle
*b
;
6130 /* Use NetServerEnum2 */
6132 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
6134 return NT_STATUS_OK
;
6137 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6138 for domain %s\n", domain_name
));
6140 /* Try netr_GetDcName */
6142 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
6144 if (!NT_STATUS_IS_OK(status
)) {
6148 b
= netr
->binding_handle
;
6150 status
= dcerpc_netr_GetDcName(b
, mem_ctx
,
6157 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(result
)) {
6161 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6162 for domain %s\n", domain_name
));
6164 if (!NT_STATUS_IS_OK(status
)) {
6168 return werror_to_ntstatus(result
);
6172 * Establish trust relationship to a trusting domain.
6173 * Interdomain account must already be created on remote PDC.
6175 * @param c A net_context structure.
6176 * @param argc Standard argc.
6177 * @param argv Standard argv without initial components.
6179 * @return Integer status (0 means success).
6182 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6185 struct cli_state
*cli
= NULL
;
6186 struct sockaddr_storage server_ss
;
6187 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6188 struct policy_handle connect_hnd
;
6189 TALLOC_CTX
*mem_ctx
;
6190 NTSTATUS nt_status
, result
;
6191 struct dom_sid
*domain_sid
;
6196 union lsa_PolicyInformation
*info
= NULL
;
6197 struct dcerpc_binding_handle
*b
;
6200 * Connect to \\server\ipc$ as 'our domain' account with password
6203 if (argc
!= 1 || c
->display_usage
) {
6206 _("net rpc trustdom establish <domain_name>\n"));
6210 domain_name
= smb_xstrdup(argv
[0]);
6211 strupper_m(domain_name
);
6213 /* account name used at first is our domain's name with '$' */
6214 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
6217 strupper_m(acct_name
);
6220 * opt_workgroup will be used by connection functions further,
6221 * hence it should be set to remote domain name instead of ours
6223 if (c
->opt_workgroup
) {
6224 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6227 c
->opt_user_name
= acct_name
;
6229 /* find the domain controller */
6230 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6231 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6235 /* connect to ipc$ as username/password */
6236 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6237 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6239 /* Is it trusting domain account for sure ? */
6240 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6241 nt_errstr(nt_status
)));
6245 /* store who we connected to */
6247 saf_store( domain_name
, pdc_name
);
6250 * Connect to \\server\ipc$ again (this time anonymously)
6253 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6256 if (NT_STATUS_IS_ERR(nt_status
)) {
6257 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6258 domain_name
, nt_errstr(nt_status
)));
6262 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6263 "domain %s", domain_name
))) {
6264 DEBUG(0, ("talloc_init() failed\n"));
6269 /* Make sure we're talking to a proper server */
6271 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6272 if (!NT_STATUS_IS_OK(nt_status
)) {
6274 talloc_destroy(mem_ctx
);
6279 * Call LsaOpenPolicy and LsaQueryInfo
6282 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6284 if (!NT_STATUS_IS_OK(nt_status
)) {
6285 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6287 talloc_destroy(mem_ctx
);
6291 b
= pipe_hnd
->binding_handle
;
6293 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
6295 if (NT_STATUS_IS_ERR(nt_status
)) {
6296 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6297 nt_errstr(nt_status
)));
6299 talloc_destroy(mem_ctx
);
6303 /* Querying info level 5 */
6305 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6307 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6310 if (NT_STATUS_IS_ERR(nt_status
)) {
6311 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6312 nt_errstr(nt_status
)));
6314 talloc_destroy(mem_ctx
);
6317 if (NT_STATUS_IS_ERR(result
)) {
6318 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6319 nt_errstr(result
)));
6321 talloc_destroy(mem_ctx
);
6325 domain_sid
= info
->account_domain
.sid
;
6327 /* There should be actually query info level 3 (following nt serv behaviour),
6328 but I still don't know if it's _really_ necessary */
6331 * Store the password in secrets db
6334 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6335 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6337 talloc_destroy(mem_ctx
);
6342 * Close the pipes and clean up
6345 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6346 if (NT_STATUS_IS_ERR(nt_status
)) {
6347 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6348 nt_errstr(nt_status
)));
6350 talloc_destroy(mem_ctx
);
6356 talloc_destroy(mem_ctx
);
6358 d_printf(_("Trust to domain %s established\n"), domain_name
);
6363 * Revoke trust relationship to the remote domain.
6365 * @param c A net_context structure.
6366 * @param argc Standard argc.
6367 * @param argv Standard argv without initial components.
6369 * @return Integer status (0 means success).
6372 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6378 if (argc
< 1 || c
->display_usage
) {
6381 _("net rpc trustdom revoke <domain_name>\n"
6382 " Revoke trust relationship\n"
6383 " domain_name\tName of domain to revoke trust\n"));
6387 /* generate upper cased domain name */
6388 domain_name
= smb_xstrdup(argv
[0]);
6389 strupper_m(domain_name
);
6391 /* delete password of the trust */
6392 if (!pdb_del_trusteddom_pw(domain_name
)) {
6393 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6400 SAFE_FREE(domain_name
);
6404 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6405 const struct dom_sid
*domain_sid
,
6406 const char *domain_name
,
6407 struct cli_state
*cli
,
6408 struct rpc_pipe_client
*pipe_hnd
,
6409 TALLOC_CTX
*mem_ctx
,
6414 if (!sid_to_fstring(str_sid
, domain_sid
)) {
6415 return NT_STATUS_UNSUCCESSFUL
;
6417 d_printf("%s\n", str_sid
);
6418 return NT_STATUS_OK
;
6421 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
6425 /* convert sid into ascii string */
6426 sid_to_fstring(ascii_sid
, dom_sid
);
6428 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
6431 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6432 TALLOC_CTX
*mem_ctx
,
6433 struct policy_handle
*pol
,
6434 struct dom_sid dom_sid
,
6435 const char *trusted_dom_name
)
6437 NTSTATUS nt_status
, result
;
6438 union lsa_TrustedDomainInfo
*info
= NULL
;
6439 char *cleartextpwd
= NULL
;
6440 DATA_BLOB session_key
;
6441 DATA_BLOB data
= data_blob_null
;
6442 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6444 nt_status
= dcerpc_lsa_QueryTrustedDomainInfoBySid(b
, mem_ctx
,
6447 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6450 if (NT_STATUS_IS_ERR(nt_status
)) {
6451 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6452 nt_errstr(nt_status
)));
6455 if (NT_STATUS_IS_ERR(result
)) {
6457 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6458 nt_errstr(result
)));
6462 data
= data_blob(info
->password
.password
->data
,
6463 info
->password
.password
->length
);
6465 nt_status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
6466 if (!NT_STATUS_IS_OK(nt_status
)) {
6467 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status
)));
6471 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key
);
6472 data_blob_free(&session_key
);
6474 if (cleartextpwd
== NULL
) {
6475 DEBUG(0,("retrieved NULL password\n"));
6476 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6480 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6481 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6482 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6486 #ifdef DEBUG_PASSWORD
6487 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6488 "password: [%s]\n", trusted_dom_name
,
6489 sid_string_dbg(&dom_sid
), cleartextpwd
));
6493 SAFE_FREE(cleartextpwd
);
6494 data_blob_free(&data
);
6499 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6502 /* common variables */
6503 TALLOC_CTX
* mem_ctx
;
6504 struct cli_state
*cli
= NULL
;
6505 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6506 NTSTATUS nt_status
, result
;
6507 const char *domain_name
= NULL
;
6508 struct policy_handle connect_hnd
;
6509 union lsa_PolicyInformation
*info
= NULL
;
6511 /* trusted domains listing variables */
6512 unsigned int enum_ctx
= 0;
6514 struct lsa_DomainList dom_list
;
6516 struct dcerpc_binding_handle
*b
;
6518 if (c
->display_usage
) {
6520 "net rpc trustdom vampire\n"
6523 _("Vampire trust relationship from remote server"));
6528 * Listing trusted domains (stored in secrets.tdb, if local)
6531 mem_ctx
= talloc_init("trust relationships vampire");
6534 * set domain and pdc name to local samba server (default)
6535 * or to remote one given in command line
6538 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6539 domain_name
= c
->opt_workgroup
;
6540 c
->opt_target_workgroup
= c
->opt_workgroup
;
6542 fstrcpy(pdc_name
, lp_netbios_name());
6543 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6544 c
->opt_target_workgroup
= domain_name
;
6547 /* open \PIPE\lsarpc and open policy handle */
6548 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6549 if (!NT_STATUS_IS_OK(nt_status
)) {
6550 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6551 nt_errstr(nt_status
)));
6552 talloc_destroy(mem_ctx
);
6556 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6558 if (!NT_STATUS_IS_OK(nt_status
)) {
6559 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6560 nt_errstr(nt_status
) ));
6562 talloc_destroy(mem_ctx
);
6566 b
= pipe_hnd
->binding_handle
;
6568 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6570 if (NT_STATUS_IS_ERR(nt_status
)) {
6571 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6572 nt_errstr(nt_status
)));
6574 talloc_destroy(mem_ctx
);
6578 /* query info level 5 to obtain sid of a domain being queried */
6579 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6581 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6585 if (NT_STATUS_IS_ERR(nt_status
)) {
6586 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6587 nt_errstr(nt_status
)));
6589 talloc_destroy(mem_ctx
);
6592 if (NT_STATUS_IS_ERR(result
)) {
6593 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6594 nt_errstr(result
)));
6596 talloc_destroy(mem_ctx
);
6601 * Keep calling LsaEnumTrustdom over opened pipe until
6602 * the end of enumeration is reached
6605 d_printf(_("Vampire trusted domains:\n\n"));
6608 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6614 if (NT_STATUS_IS_ERR(nt_status
)) {
6615 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6616 nt_errstr(nt_status
)));
6618 talloc_destroy(mem_ctx
);
6621 if (NT_STATUS_IS_ERR(result
)) {
6623 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6624 nt_errstr(result
)));
6626 talloc_destroy(mem_ctx
);
6631 for (i
= 0; i
< dom_list
.count
; i
++) {
6633 print_trusted_domain(dom_list
.domains
[i
].sid
,
6634 dom_list
.domains
[i
].name
.string
);
6636 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6637 *dom_list
.domains
[i
].sid
,
6638 dom_list
.domains
[i
].name
.string
);
6639 if (!NT_STATUS_IS_OK(nt_status
)) {
6641 talloc_destroy(mem_ctx
);
6647 * in case of no trusted domains say something rather
6648 * than just display blank line
6650 if (!dom_list
.count
) d_printf(_("none\n"));
6652 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6654 /* close this connection before doing next one */
6655 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6656 if (NT_STATUS_IS_ERR(nt_status
)) {
6657 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6658 nt_errstr(nt_status
)));
6660 talloc_destroy(mem_ctx
);
6664 /* close lsarpc pipe and connection to IPC$ */
6667 talloc_destroy(mem_ctx
);
6671 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6673 /* common variables */
6674 TALLOC_CTX
* mem_ctx
;
6675 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6676 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6677 NTSTATUS nt_status
, result
;
6678 const char *domain_name
= NULL
;
6679 struct dom_sid
*queried_dom_sid
;
6680 int ascii_dom_name_len
;
6681 struct policy_handle connect_hnd
;
6682 union lsa_PolicyInformation
*info
= NULL
;
6683 struct dcerpc_binding_handle
*b
= NULL
;
6685 /* trusted domains listing variables */
6686 unsigned int num_domains
, enum_ctx
= 0;
6688 struct lsa_DomainList dom_list
;
6692 /* trusting domains listing variables */
6693 struct policy_handle domain_hnd
;
6694 struct samr_SamArray
*trusts
= NULL
;
6696 if (c
->display_usage
) {
6698 "net rpc trustdom list\n"
6701 _("List incoming and outgoing trust relationships"));
6706 * Listing trusted domains (stored in secrets.tdb, if local)
6709 mem_ctx
= talloc_init("trust relationships listing");
6712 * set domain and pdc name to local samba server (default)
6713 * or to remote one given in command line
6716 if (strcasecmp_m(c
->opt_workgroup
, lp_workgroup())) {
6717 domain_name
= c
->opt_workgroup
;
6718 c
->opt_target_workgroup
= c
->opt_workgroup
;
6720 fstrcpy(pdc_name
, lp_netbios_name());
6721 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6722 c
->opt_target_workgroup
= domain_name
;
6725 /* open \PIPE\lsarpc and open policy handle */
6726 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6727 if (!NT_STATUS_IS_OK(nt_status
)) {
6728 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6729 nt_errstr(nt_status
)));
6730 talloc_destroy(mem_ctx
);
6734 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6736 if (!NT_STATUS_IS_OK(nt_status
)) {
6737 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6738 nt_errstr(nt_status
) ));
6740 talloc_destroy(mem_ctx
);
6744 b
= pipe_hnd
->binding_handle
;
6746 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6748 if (NT_STATUS_IS_ERR(nt_status
)) {
6749 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6750 nt_errstr(nt_status
)));
6752 talloc_destroy(mem_ctx
);
6756 /* query info level 5 to obtain sid of a domain being queried */
6757 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6759 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6763 if (NT_STATUS_IS_ERR(nt_status
)) {
6764 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6765 nt_errstr(nt_status
)));
6767 talloc_destroy(mem_ctx
);
6770 if (NT_STATUS_IS_ERR(result
)) {
6771 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6772 nt_errstr(result
)));
6774 talloc_destroy(mem_ctx
);
6778 queried_dom_sid
= info
->account_domain
.sid
;
6781 * Keep calling LsaEnumTrustdom over opened pipe until
6782 * the end of enumeration is reached
6785 d_printf(_("Trusted domains list:\n\n"));
6787 found_domain
= false;
6790 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6796 if (NT_STATUS_IS_ERR(nt_status
)) {
6797 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6798 nt_errstr(nt_status
)));
6800 talloc_destroy(mem_ctx
);
6803 if (NT_STATUS_IS_ERR(result
)) {
6804 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6805 nt_errstr(result
)));
6807 talloc_destroy(mem_ctx
);
6812 for (i
= 0; i
< dom_list
.count
; i
++) {
6813 print_trusted_domain(dom_list
.domains
[i
].sid
,
6814 dom_list
.domains
[i
].name
.string
);
6815 found_domain
= true;
6819 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6822 * in case of no trusted domains say something rather
6823 * than just display blank line
6825 if (!found_domain
) {
6826 d_printf(_("none\n"));
6829 /* close this connection before doing next one */
6830 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6831 if (NT_STATUS_IS_ERR(nt_status
)) {
6832 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6833 nt_errstr(nt_status
)));
6835 talloc_destroy(mem_ctx
);
6839 TALLOC_FREE(pipe_hnd
);
6842 * Listing trusting domains (stored in passdb backend, if local)
6845 d_printf(_("\nTrusting domains list:\n\n"));
6848 * Open \PIPE\samr and get needed policy handles
6850 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6852 if (!NT_STATUS_IS_OK(nt_status
)) {
6853 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6855 talloc_destroy(mem_ctx
);
6859 b
= pipe_hnd
->binding_handle
;
6862 nt_status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6864 SAMR_ACCESS_LOOKUP_DOMAIN
,
6867 if (!NT_STATUS_IS_OK(nt_status
)) {
6868 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6869 nt_errstr(nt_status
)));
6871 talloc_destroy(mem_ctx
);
6874 if (!NT_STATUS_IS_OK(result
)) {
6876 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6877 nt_errstr(result
)));
6879 talloc_destroy(mem_ctx
);
6883 /* SamrOpenDomain - we have to open domain policy handle in order to be
6884 able to enumerate accounts*/
6885 nt_status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6887 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6891 if (!NT_STATUS_IS_OK(nt_status
)) {
6892 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6893 nt_errstr(nt_status
)));
6895 talloc_destroy(mem_ctx
);
6898 if (!NT_STATUS_IS_OK(result
)) {
6900 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6901 nt_errstr(result
)));
6903 talloc_destroy(mem_ctx
);
6908 * perform actual enumeration
6911 found_domain
= false;
6913 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6916 nt_status
= dcerpc_samr_EnumDomainUsers(b
, mem_ctx
,
6924 if (NT_STATUS_IS_ERR(nt_status
)) {
6925 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6926 nt_errstr(nt_status
)));
6928 talloc_destroy(mem_ctx
);
6931 if (NT_STATUS_IS_ERR(result
)) {
6933 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6934 nt_errstr(result
)));
6936 talloc_destroy(mem_ctx
);
6940 for (i
= 0; i
< num_domains
; i
++) {
6942 char *str
= discard_const_p(char, trusts
->entries
[i
].name
.string
);
6944 found_domain
= true;
6947 * get each single domain's sid (do we _really_ need this ?):
6948 * 1) connect to domain's pdc
6949 * 2) query the pdc for domain's sid
6952 /* get rid of '$' tail */
6953 ascii_dom_name_len
= strlen(str
);
6954 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6955 str
[ascii_dom_name_len
- 1] = '\0';
6957 /* set opt_* variables to remote domain */
6959 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6960 c
->opt_target_workgroup
= c
->opt_workgroup
;
6962 d_printf("%-20s", str
);
6964 /* connect to remote domain controller */
6965 nt_status
= net_make_ipc_connection(c
,
6966 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6968 if (NT_STATUS_IS_OK(nt_status
)) {
6969 /* query for domain's sid */
6970 if (run_rpc_command(
6972 &ndr_table_lsarpc
, 0,
6973 rpc_query_domain_sid
, argc
,
6975 d_printf(_("strange - couldn't get domain's sid\n"));
6977 cli_shutdown(remote_cli
);
6980 d_fprintf(stderr
, _("domain controller is not "
6981 "responding: %s\n"),
6982 nt_errstr(nt_status
));
6983 d_printf(_("couldn't get domain's sid\n"));
6987 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
6989 if (!found_domain
) {
6993 /* close opened samr and domain policy handles */
6994 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &domain_hnd
, &result
);
6995 if (!NT_STATUS_IS_OK(nt_status
)) {
6996 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6999 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &connect_hnd
, &result
);
7000 if (!NT_STATUS_IS_OK(nt_status
)) {
7001 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
7004 /* close samr pipe and connection to IPC$ */
7007 talloc_destroy(mem_ctx
);
7012 * Entrypoint for 'net rpc trustdom' code.
7014 * @param argc Standard argc.
7015 * @param argv Standard argv without initial components.
7017 * @return Integer status (0 means success).
7020 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
7022 struct functable func
[] = {
7027 N_("Add trusting domain's account"),
7028 N_("net rpc trustdom add\n"
7029 " Add trusting domain's account")
7035 N_("Remove trusting domain's account"),
7036 N_("net rpc trustdom del\n"
7037 " Remove trusting domain's account")
7041 rpc_trustdom_establish
,
7043 N_("Establish outgoing trust relationship"),
7044 N_("net rpc trustdom establish\n"
7045 " Establish outgoing trust relationship")
7049 rpc_trustdom_revoke
,
7051 N_("Revoke outgoing trust relationship"),
7052 N_("net rpc trustdom revoke\n"
7053 " Revoke outgoing trust relationship")
7059 N_("List in- and outgoing domain trusts"),
7060 N_("net rpc trustdom list\n"
7061 " List in- and outgoing domain trusts")
7065 rpc_trustdom_vampire
,
7067 N_("Vampire trusts from remote server"),
7068 N_("net rpc trustdom vampire\n"
7069 " Vampire trusts from remote server")
7071 {NULL
, NULL
, 0, NULL
, NULL
}
7074 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
7078 * Check if a server will take rpc commands
7079 * @param flags Type of server to connect to (PDC, DMB, localhost)
7080 * if the host is not explicitly specified
7081 * @return bool (true means rpc supported)
7083 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
7085 struct cli_state
*cli
;
7087 struct sockaddr_storage server_ss
;
7088 char *server_name
= NULL
;
7091 /* flags (i.e. server type) may depend on command */
7092 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
7095 status
= cli_connect_nb(server_name
, &server_ss
, 0, 0x20,
7096 lp_netbios_name(), SMB_SIGNING_DEFAULT
,
7098 if (!NT_STATUS_IS_OK(status
)) {
7101 status
= cli_negprot(cli
, PROTOCOL_NT1
);
7102 if (!NT_STATUS_IS_OK(status
))
7104 if (cli_state_protocol(cli
) < PROTOCOL_NT1
)
7113 /* dump sam database via samsync rpc calls */
7114 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
7115 if (c
->display_usage
) {
7120 _("Dump remote SAM database"));
7124 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
,
7125 NET_FLAGS_ANONYMOUS
,
7126 rpc_samdump_internals
, argc
, argv
);
7129 /* syncronise sam database via samsync rpc calls */
7130 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
7132 struct functable func
[] = {
7137 N_("Dump remote SAM database to ldif"),
7138 N_("net rpc vampire ldif\n"
7139 " Dump remote SAM database to LDIF file or "
7146 N_("Dump remote SAM database to Kerberos Keytab"),
7147 N_("net rpc vampire keytab\n"
7148 " Dump remote SAM database to Kerberos keytab "
7155 N_("Dump remote SAM database to passdb"),
7156 N_("net rpc vampire passdb\n"
7157 " Dump remote SAM database to passdb")
7160 {NULL
, NULL
, 0, NULL
, NULL
}
7164 if (c
->display_usage
) {
7169 _("Vampire remote SAM database"));
7173 return rpc_vampire_passdb(c
, argc
, argv
);
7176 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
7180 * Migrate everything from a print server.
7182 * @param c A net_context structure.
7183 * @param argc Standard main() style argc.
7184 * @param argv Standard main() style argv. Initial components are already
7187 * @return A shell status integer (0 for success).
7189 * The order is important !
7190 * To successfully add drivers the print queues have to exist !
7191 * Applying ACLs should be the last step, because you're easily locked out.
7194 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
7199 if (c
->display_usage
) {
7201 "net rpc printer migrate all\n"
7204 _("Migrate everything from a print server"));
7209 d_printf(_("no server to migrate\n"));
7213 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7214 rpc_printer_migrate_printers_internals
, argc
,
7219 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7220 rpc_printer_migrate_drivers_internals
, argc
,
7225 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7226 rpc_printer_migrate_forms_internals
, argc
, argv
);
7230 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7231 rpc_printer_migrate_settings_internals
, argc
,
7236 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7237 rpc_printer_migrate_security_internals
, argc
,
7243 * Migrate print drivers from a print server.
7245 * @param c A net_context structure.
7246 * @param argc Standard main() style argc.
7247 * @param argv Standard main() style argv. Initial components are already
7250 * @return A shell status integer (0 for success).
7252 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
7255 if (c
->display_usage
) {
7257 "net rpc printer migrate drivers\n"
7260 _("Migrate print-drivers from a print-server"));
7265 d_printf(_("no server to migrate\n"));
7269 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7270 rpc_printer_migrate_drivers_internals
,
7275 * Migrate print-forms from a print-server.
7277 * @param c A net_context structure.
7278 * @param argc Standard main() style argc.
7279 * @param argv Standard main() style argv. Initial components are already
7282 * @return A shell status integer (0 for success).
7284 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7287 if (c
->display_usage
) {
7289 "net rpc printer migrate forms\n"
7292 _("Migrate print-forms from a print-server"));
7297 d_printf(_("no server to migrate\n"));
7301 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7302 rpc_printer_migrate_forms_internals
,
7307 * Migrate printers from a print-server.
7309 * @param c A net_context structure.
7310 * @param argc Standard main() style argc.
7311 * @param argv Standard main() style argv. Initial components are already
7314 * @return A shell status integer (0 for success).
7316 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7319 if (c
->display_usage
) {
7321 "net rpc printer migrate printers\n"
7324 _("Migrate printers from a print-server"));
7329 d_printf(_("no server to migrate\n"));
7333 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7334 rpc_printer_migrate_printers_internals
,
7339 * Migrate printer-ACLs from a print-server
7341 * @param c A net_context structure.
7342 * @param argc Standard main() style argc.
7343 * @param argv Standard main() style argv. Initial components are already
7346 * @return A shell status integer (0 for success).
7348 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7351 if (c
->display_usage
) {
7353 "net rpc printer migrate security\n"
7356 _("Migrate printer-ACLs from a print-server"));
7361 d_printf(_("no server to migrate\n"));
7365 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7366 rpc_printer_migrate_security_internals
,
7371 * Migrate printer-settings from a print-server.
7373 * @param c A net_context structure.
7374 * @param argc Standard main() style argc.
7375 * @param argv Standard main() style argv. Initial components are already
7378 * @return A shell status integer (0 for success).
7380 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7383 if (c
->display_usage
) {
7385 "net rpc printer migrate settings\n"
7388 _("Migrate printer-settings from a "
7394 d_printf(_("no server to migrate\n"));
7398 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7399 rpc_printer_migrate_settings_internals
,
7404 * 'net rpc printer' entrypoint.
7406 * @param c A net_context structure.
7407 * @param argc Standard main() style argc.
7408 * @param argv Standard main() style argv. Initial components are already
7412 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7415 /* ouch: when addriver and setdriver are called from within
7416 rpc_printer_migrate_drivers_internals, the printer-queue already
7419 struct functable func
[] = {
7422 rpc_printer_migrate_all
,
7424 N_("Migrate all from remote to local print server"),
7425 N_("net rpc printer migrate all\n"
7426 " Migrate all from remote to local print server")
7430 rpc_printer_migrate_drivers
,
7432 N_("Migrate drivers to local server"),
7433 N_("net rpc printer migrate drivers\n"
7434 " Migrate drivers to local server")
7438 rpc_printer_migrate_forms
,
7440 N_("Migrate froms to local server"),
7441 N_("net rpc printer migrate forms\n"
7442 " Migrate froms to local server")
7446 rpc_printer_migrate_printers
,
7448 N_("Migrate printers to local server"),
7449 N_("net rpc printer migrate printers\n"
7450 " Migrate printers to local server")
7454 rpc_printer_migrate_security
,
7456 N_("Mirgate printer ACLs to local server"),
7457 N_("net rpc printer migrate security\n"
7458 " Mirgate printer ACLs to local server")
7462 rpc_printer_migrate_settings
,
7464 N_("Migrate printer settings to local server"),
7465 N_("net rpc printer migrate settings\n"
7466 " Migrate printer settings to local server")
7468 {NULL
, NULL
, 0, NULL
, NULL
}
7471 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7476 * List printers on a remote RPC server.
7478 * @param c A net_context structure.
7479 * @param argc Standard main() style argc.
7480 * @param argv Standard main() style argv. Initial components are already
7483 * @return A shell status integer (0 for success).
7485 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7487 if (c
->display_usage
) {
7489 "net rpc printer list\n"
7492 _("List printers on a remote RPC server"));
7496 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7497 rpc_printer_list_internals
,
7502 * List printer-drivers on a remote RPC server.
7504 * @param c A net_context structure.
7505 * @param argc Standard main() style argc.
7506 * @param argv Standard main() style argv. Initial components are already
7509 * @return A shell status integer (0 for success).
7511 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7514 if (c
->display_usage
) {
7516 "net rpc printer driver\n"
7519 _("List printer-drivers on a remote RPC server"));
7523 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7524 rpc_printer_driver_list_internals
,
7529 * Publish printer in ADS via MSRPC.
7531 * @param c A net_context structure.
7532 * @param argc Standard main() style argc.
7533 * @param argv Standard main() style argv. Initial components are already
7536 * @return A shell status integer (0 for success).
7538 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7541 if (c
->display_usage
) {
7543 "net rpc printer publish publish\n"
7546 _("Publish printer in ADS via MSRPC"));
7550 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7551 rpc_printer_publish_publish_internals
,
7556 * Update printer in ADS via MSRPC.
7558 * @param c A net_context structure.
7559 * @param argc Standard main() style argc.
7560 * @param argv Standard main() style argv. Initial components are already
7563 * @return A shell status integer (0 for success).
7565 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7567 if (c
->display_usage
) {
7569 "net rpc printer publish update\n"
7572 _("Update printer in ADS via MSRPC"));
7576 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7577 rpc_printer_publish_update_internals
,
7582 * UnPublish printer in ADS via MSRPC.
7584 * @param c A net_context structure.
7585 * @param argc Standard main() style argc.
7586 * @param argv Standard main() style argv. Initial components are already
7589 * @return A shell status integer (0 for success).
7591 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7594 if (c
->display_usage
) {
7596 "net rpc printer publish unpublish\n"
7599 _("UnPublish printer in ADS via MSRPC"));
7603 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7604 rpc_printer_publish_unpublish_internals
,
7609 * List published printers via MSRPC.
7611 * @param c A net_context structure.
7612 * @param argc Standard main() style argc.
7613 * @param argv Standard main() style argv. Initial components are already
7616 * @return A shell status integer (0 for success).
7618 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7621 if (c
->display_usage
) {
7623 "net rpc printer publish list\n"
7626 _("List published printers via MSRPC"));
7630 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7631 rpc_printer_publish_list_internals
,
7637 * Publish printer in ADS.
7639 * @param c A net_context structure.
7640 * @param argc Standard main() style argc.
7641 * @param argv Standard main() style argv. Initial components are already
7644 * @return A shell status integer (0 for success).
7646 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7650 struct functable func
[] = {
7653 rpc_printer_publish_publish
,
7655 N_("Publish printer in AD"),
7656 N_("net rpc printer publish publish\n"
7657 " Publish printer in AD")
7661 rpc_printer_publish_update
,
7663 N_("Update printer in AD"),
7664 N_("net rpc printer publish update\n"
7665 " Update printer in AD")
7669 rpc_printer_publish_unpublish
,
7671 N_("Unpublish printer"),
7672 N_("net rpc printer publish unpublish\n"
7673 " Unpublish printer")
7677 rpc_printer_publish_list
,
7679 N_("List published printers"),
7680 N_("net rpc printer publish list\n"
7681 " List published printers")
7683 {NULL
, NULL
, 0, NULL
, NULL
}
7687 if (c
->display_usage
) {
7688 d_printf(_("Usage:\n"));
7689 d_printf(_("net rpc printer publish\n"
7690 " List published printers\n"
7691 " Alias of net rpc printer publish "
7693 net_display_usage_from_functable(func
);
7696 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7697 rpc_printer_publish_list_internals
,
7701 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7707 * Display rpc printer help page.
7709 * @param c A net_context structure.
7710 * @param argc Standard main() style argc.
7711 * @param argv Standard main() style argv. Initial components are already
7714 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7716 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7717 "\tlists all printers on print-server\n\n"));
7718 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7719 "\tlists all printer-drivers on print-server\n\n"));
7720 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7721 "\tpublishes printer settings in Active Directory\n"
7722 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7723 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7724 "\n\tmigrates printers from remote to local server\n\n"));
7725 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7726 "\n\tmigrates printer-settings from remote to local server\n\n"));
7727 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7728 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7729 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7730 "\n\tmigrates printer-forms from remote to local server\n\n"));
7731 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7732 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7733 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7734 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7735 "\tremote to local print-server\n\n"));
7736 net_common_methods_usage(c
, argc
, argv
);
7737 net_common_flags_usage(c
, argc
, argv
);
7739 "\t-v or --verbose\t\t\tgive verbose output\n"
7740 "\t --destination\t\tmigration target server (default: localhost)\n"));
7746 * 'net rpc printer' entrypoint.
7748 * @param c A net_context structure.
7749 * @param argc Standard main() style argc.
7750 * @param argv Standard main() style argv. Initial components are already
7753 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7755 struct functable func
[] = {
7760 N_("List all printers on print server"),
7761 N_("net rpc printer list\n"
7762 " List all printers on print server")
7766 rpc_printer_migrate
,
7768 N_("Migrate printer to local server"),
7769 N_("net rpc printer migrate\n"
7770 " Migrate printer to local server")
7774 rpc_printer_driver_list
,
7776 N_("List printer drivers"),
7777 N_("net rpc printer driver\n"
7778 " List printer drivers")
7782 rpc_printer_publish
,
7784 N_("Publish printer in AD"),
7785 N_("net rpc printer publish\n"
7786 " Publish printer in AD")
7788 {NULL
, NULL
, 0, NULL
, NULL
}
7792 if (c
->display_usage
) {
7793 d_printf(_("Usage:\n"));
7794 d_printf(_("net rpc printer\n"
7795 " List printers\n"));
7796 net_display_usage_from_functable(func
);
7799 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
, 0,
7800 rpc_printer_list_internals
,
7804 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7808 * 'net rpc' entrypoint.
7810 * @param c A net_context structure.
7811 * @param argc Standard main() style argc.
7812 * @param argv Standard main() style argv. Initial components are already
7816 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7818 NET_API_STATUS status
;
7820 struct functable func
[] = {
7825 N_("Modify global audit settings"),
7826 N_("net rpc audit\n"
7827 " Modify global audit settings")
7833 N_("Show basic info about a domain"),
7835 " Show basic info about a domain")
7841 N_("Join a domain"),
7849 N_("Join a domain created in server manager"),
7850 N_("net rpc oldjoin\n"
7851 " Join a domain created in server manager")
7857 N_("Test that a join is valid"),
7858 N_("net rpc testjoin\n"
7859 " Test that a join is valid")
7865 N_("List/modify users"),
7867 " List/modify users")
7873 N_("Change a user password"),
7874 N_("net rpc password\n"
7875 " Change a user password\n"
7876 " Alias for net rpc user password")
7882 N_("List/modify groups"),
7883 N_("net rpc group\n"
7884 " List/modify groups")
7890 N_("List/modify shares"),
7891 N_("net rpc share\n"
7892 " List/modify shares")
7898 N_("List open files"),
7906 N_("List/modify printers"),
7907 N_("net rpc printer\n"
7908 " List/modify printers")
7912 net_rpc_changetrustpw
,
7914 N_("Change trust account password"),
7915 N_("net rpc changetrustpw\n"
7916 " Change trust account password")
7922 N_("Modify domain trusts"),
7923 N_("net rpc trustdom\n"
7924 " Modify domain trusts")
7930 N_("Abort a remote shutdown"),
7931 N_("net rpc abortshutdown\n"
7932 " Abort a remote shutdown")
7938 N_("Shutdown a remote server"),
7939 N_("net rpc shutdown\n"
7940 " Shutdown a remote server")
7946 N_("Dump SAM data of remote NT PDC"),
7947 N_("net rpc samdump\n"
7948 " Dump SAM data of remote NT PDC")
7954 N_("Sync a remote NT PDC's data into local passdb"),
7955 N_("net rpc vampire\n"
7956 " Sync a remote NT PDC's data into local passdb")
7962 N_("Fetch the domain sid into local secrets.tdb"),
7963 N_("net rpc getsid\n"
7964 " Fetch the domain sid into local secrets.tdb")
7970 N_("Manage privileges assigned to SID"),
7971 N_("net rpc rights\n"
7972 " Manage privileges assigned to SID")
7978 N_("Start/stop/query remote services"),
7979 N_("net rpc service\n"
7980 " Start/stop/query remote services")
7986 N_("Manage registry hives"),
7987 N_("net rpc registry\n"
7988 " Manage registry hives")
7994 N_("Open interactive shell on remote server"),
7995 N_("net rpc shell\n"
7996 " Open interactive shell on remote server")
8002 N_("Manage trusts"),
8003 N_("net rpc trust\n"
8010 N_("Configure a remote samba server"),
8012 " Configure a remote samba server")
8014 {NULL
, NULL
, 0, NULL
, NULL
}
8017 status
= libnetapi_net_init(&c
->netapi_ctx
);
8021 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
8022 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
8023 if (c
->opt_kerberos
) {
8024 libnetapi_set_use_kerberos(c
->netapi_ctx
);
8026 if (c
->opt_ccache
) {
8027 libnetapi_set_use_ccache(c
->netapi_ctx
);
8030 return net_run_function(c
, argc
, argv
, "net rpc", func
);