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_syntax_id
*interface
,
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(interface
,
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
, interface
, 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_ntlmssp(
205 (conn_flags
& NET_FLAGS_TCP
) ?
206 NCACN_IP_TCP
: NCACN_NP
,
207 DCERPC_AUTH_LEVEL_PRIVACY
,
208 lp_workgroup(), c
->opt_user_name
,
209 c
->opt_password
, &pipe_hnd
);
211 nt_status
= cli_rpc_pipe_open_noauth(
215 if (!NT_STATUS_IS_OK(nt_status
)) {
216 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
217 get_pipe_name_from_syntax(
218 talloc_tos(), interface
),
219 nt_errstr(nt_status
) ));
225 nt_status
= fn(c
, domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
227 if (!NT_STATUS_IS_OK(nt_status
)) {
228 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
231 DEBUG(5, ("rpc command function succedded\n"));
234 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
236 TALLOC_FREE(pipe_hnd
);
241 /* close the connection only if it was opened here */
246 talloc_destroy(mem_ctx
);
251 * Force a change of the trust acccount password.
253 * All parameters are provided by the run_rpc_command function, except for
254 * argc, argv which are passed through.
256 * @param domain_sid The domain sid acquired from the remote server.
257 * @param cli A cli_state connected to the server.
258 * @param mem_ctx Talloc context, destroyed on completion of the function.
259 * @param argc Standard main() style argc.
260 * @param argv Standard main() style argv. Initial components are already
263 * @return Normal NTSTATUS return.
266 static NTSTATUS
rpc_changetrustpw_internals(struct net_context
*c
,
267 const struct dom_sid
*domain_sid
,
268 const char *domain_name
,
269 struct cli_state
*cli
,
270 struct rpc_pipe_client
*pipe_hnd
,
277 status
= trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
);
278 if (!NT_STATUS_IS_OK(status
)) {
279 d_fprintf(stderr
, _("Failed to change machine account password: %s\n"),
288 * Force a change of the trust acccount password.
290 * @param argc Standard main() style argc.
291 * @param argv Standard main() style argv. Initial components are already
294 * @return A shell status integer (0 for success).
297 int net_rpc_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
299 if (c
->display_usage
) {
301 "net rpc changetrustpw\n"
304 _("Change the machine trust password"));
308 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
309 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
310 rpc_changetrustpw_internals
,
315 * Join a domain, the old way.
317 * This uses 'machinename' as the inital password, and changes it.
319 * The password should be created with 'server manager' or equiv first.
321 * All parameters are provided by the run_rpc_command function, except for
322 * argc, argv which are passed through.
324 * @param domain_sid The domain sid acquired from the remote server.
325 * @param cli A cli_state connected to the server.
326 * @param mem_ctx Talloc context, destroyed on completion of the function.
327 * @param argc Standard main() style argc.
328 * @param argv Standard main() style argv. Initial components are already
331 * @return Normal NTSTATUS return.
334 static NTSTATUS
rpc_oldjoin_internals(struct net_context
*c
,
335 const struct dom_sid
*domain_sid
,
336 const char *domain_name
,
337 struct cli_state
*cli
,
338 struct rpc_pipe_client
*pipe_hnd
,
344 fstring trust_passwd
;
345 unsigned char orig_trust_passwd_hash
[16];
347 enum netr_SchannelType sec_channel_type
;
349 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
351 if (!NT_STATUS_IS_OK(result
)) {
352 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
355 nt_errstr(result
) ));
360 check what type of join - if the user want's to join as
361 a BDC, the server must agree that we are a BDC.
364 sec_channel_type
= get_sec_channel_type(argv
[0]);
366 sec_channel_type
= get_sec_channel_type(NULL
);
369 fstrcpy(trust_passwd
, global_myname());
370 strlower_m(trust_passwd
);
373 * Machine names can be 15 characters, but the max length on
374 * a password is 14. --jerry
377 trust_passwd
[14] = '\0';
379 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
381 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
,
383 orig_trust_passwd_hash
,
386 if (NT_STATUS_IS_OK(result
))
387 printf(_("Joined domain %s.\n"), c
->opt_target_workgroup
);
390 if (!secrets_store_domain_sid(c
->opt_target_workgroup
, domain_sid
)) {
391 DEBUG(0, ("error storing domain sid for %s\n", c
->opt_target_workgroup
));
392 result
= NT_STATUS_UNSUCCESSFUL
;
399 * Join a domain, the old way.
401 * @param argc Standard main() style argc.
402 * @param argv Standard main() style argv. Initial components are already
405 * @return A shell status integer (0 for success).
408 static int net_rpc_perform_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
410 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
411 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
412 rpc_oldjoin_internals
,
417 * Join a domain, the old way. This function exists to allow
418 * the message to be displayed when oldjoin was explicitly
419 * requested, but not when it was implied by "net rpc join".
421 * @param argc Standard main() style argc.
422 * @param argv Standard main() style argv. Initial components are already
425 * @return A shell status integer (0 for success).
428 static int net_rpc_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
432 if (c
->display_usage
) {
437 _("Join a domain the old way"));
441 rc
= net_rpc_perform_oldjoin(c
, argc
, argv
);
444 d_fprintf(stderr
, _("Failed to join domain\n"));
451 * 'net rpc join' entrypoint.
452 * @param argc Standard main() style argc.
453 * @param argv Standard main() style argv. Initial components are already
456 * Main 'net_rpc_join()' (where the admin username/password is used) is
458 * Try to just change the password, but if that doesn't work, use/prompt
459 * for a username/password.
462 int net_rpc_join(struct net_context
*c
, int argc
, const char **argv
)
464 if (c
->display_usage
) {
467 _("net rpc join -U <username>[%%password] <type>\n"
469 " username\tName of the admin user"
470 " password\tPassword of the admin user, will "
471 "prompt if not specified\n"
472 " type\tCan be one of the following:\n"
473 "\t\tMEMBER\tJoin as member server (default)\n"
474 "\t\tBDC\tJoin as BDC\n"
475 "\t\tPDC\tJoin as PDC\n"));
479 if (lp_server_role() == ROLE_STANDALONE
) {
480 d_printf(_("cannot join as standalone machine\n"));
484 if (strlen(global_myname()) > 15) {
485 d_printf(_("Our netbios name can be at most 15 chars long, "
486 "\"%s\" is %u chars long\n"),
487 global_myname(), (unsigned int)strlen(global_myname()));
491 if ((net_rpc_perform_oldjoin(c
, argc
, argv
) == 0))
494 return net_rpc_join_newstyle(c
, argc
, argv
);
498 * display info about a rpc domain
500 * All parameters are provided by the run_rpc_command function, except for
501 * argc, argv which are passed through.
503 * @param domain_sid The domain sid acquired from the remote server
504 * @param cli A cli_state connected to the server.
505 * @param mem_ctx Talloc context, destroyed on completion of the function.
506 * @param argc Standard main() style argc.
507 * @param argv Standard main() style argv. Initial components are already
510 * @return Normal NTSTATUS return.
513 NTSTATUS
rpc_info_internals(struct net_context
*c
,
514 const struct dom_sid
*domain_sid
,
515 const char *domain_name
,
516 struct cli_state
*cli
,
517 struct rpc_pipe_client
*pipe_hnd
,
522 struct policy_handle connect_pol
, domain_pol
;
523 NTSTATUS status
, result
;
524 union samr_DomainInfo
*info
= NULL
;
526 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
528 sid_to_fstring(sid_str
, domain_sid
);
530 /* Get sam policy handle */
531 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
533 MAXIMUM_ALLOWED_ACCESS
,
536 if (!NT_STATUS_IS_OK(status
)) {
537 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
542 if (!NT_STATUS_IS_OK(result
)) {
544 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
549 /* Get domain policy handle */
550 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
552 MAXIMUM_ALLOWED_ACCESS
,
553 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
556 if (!NT_STATUS_IS_OK(status
)) {
557 d_fprintf(stderr
, _("Could not open domain: %s\n"),
561 if (!NT_STATUS_IS_OK(result
)) {
563 d_fprintf(stderr
, _("Could not open domain: %s\n"),
568 status
= dcerpc_samr_QueryDomainInfo(b
, mem_ctx
,
573 if (!NT_STATUS_IS_OK(status
)) {
577 if (NT_STATUS_IS_OK(result
)) {
578 d_printf(_("Domain Name: %s\n"),
579 info
->general
.domain_name
.string
);
580 d_printf(_("Domain SID: %s\n"), sid_str
);
581 d_printf(_("Sequence number: %llu\n"),
582 (unsigned long long)info
->general
.sequence_num
);
583 d_printf(_("Num users: %u\n"), info
->general
.num_users
);
584 d_printf(_("Num domain groups: %u\n"),info
->general
.num_groups
);
585 d_printf(_("Num local groups: %u\n"),info
->general
.num_aliases
);
593 * 'net rpc info' entrypoint.
594 * @param argc Standard main() style argc.
595 * @param argv Standard main() style argv. Initial components are already
599 int net_rpc_info(struct net_context
*c
, int argc
, const char **argv
)
601 if (c
->display_usage
) {
606 _("Display information about the domain"));
610 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
611 NET_FLAGS_PDC
, rpc_info_internals
,
616 * Fetch domain SID into the local secrets.tdb.
618 * All parameters are provided by the run_rpc_command function, except for
619 * argc, argv which are passed through.
621 * @param domain_sid The domain sid acquired from the remote server.
622 * @param cli A cli_state connected to the server.
623 * @param mem_ctx Talloc context, destroyed on completion of the function.
624 * @param argc Standard main() style argc.
625 * @param argv Standard main() style argv. Initial components are already
628 * @return Normal NTSTATUS return.
631 static NTSTATUS
rpc_getsid_internals(struct net_context
*c
,
632 const struct dom_sid
*domain_sid
,
633 const char *domain_name
,
634 struct cli_state
*cli
,
635 struct rpc_pipe_client
*pipe_hnd
,
642 sid_to_fstring(sid_str
, domain_sid
);
643 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
644 sid_str
, domain_name
);
646 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
647 DEBUG(0,("Can't store domain SID\n"));
648 return NT_STATUS_UNSUCCESSFUL
;
655 * 'net rpc getsid' entrypoint.
656 * @param argc Standard main() style argc.
657 * @param argv Standard main() style argv. Initial components are already
661 int net_rpc_getsid(struct net_context
*c
, int argc
, const char **argv
)
663 int conn_flags
= NET_FLAGS_PDC
;
665 if (!c
->opt_user_specified
) {
666 conn_flags
|= NET_FLAGS_ANONYMOUS
;
669 if (c
->display_usage
) {
674 _("Fetch domain SID into local secrets.tdb"));
678 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
680 rpc_getsid_internals
,
684 /****************************************************************************/
687 * Basic usage function for 'net rpc user'.
688 * @param argc Standard main() style argc.
689 * @param argv Standard main() style argv. Initial components are already
693 static int rpc_user_usage(struct net_context
*c
, int argc
, const char **argv
)
695 return net_user_usage(c
, argc
, argv
);
699 * Add a new user to a remote RPC server.
701 * @param argc Standard main() style argc.
702 * @param argv Standard main() style argv. Initial components are already
705 * @return A shell status integer (0 for success).
708 static int rpc_user_add(struct net_context
*c
, int argc
, const char **argv
)
710 NET_API_STATUS status
;
711 struct USER_INFO_1 info1
;
712 uint32_t parm_error
= 0;
714 if (argc
< 1 || c
->display_usage
) {
715 rpc_user_usage(c
, argc
, argv
);
721 info1
.usri1_name
= argv
[0];
723 info1
.usri1_password
= argv
[1];
726 status
= NetUserAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
729 d_fprintf(stderr
,_("Failed to add user '%s' with error: %s.\n"),
730 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
734 d_printf(_("Added user '%s'.\n"), argv
[0]);
741 * Rename a user on a remote RPC server.
743 * @param argc Standard main() style argc.
744 * @param argv Standard main() style argv. Initial components are already
747 * @return A shell status integer (0 for success).
750 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
752 NET_API_STATUS status
;
753 struct USER_INFO_0 u0
;
754 uint32_t parm_err
= 0;
756 if (argc
!= 2 || c
->display_usage
) {
757 rpc_user_usage(c
, argc
, argv
);
761 u0
.usri0_name
= argv
[1];
763 status
= NetUserSetInfo(c
->opt_host
, argv
[0],
764 0, (uint8_t *)&u0
, &parm_err
);
767 _("Failed to rename user from %s to %s - %s\n"),
769 libnetapi_get_error_string(c
->netapi_ctx
, status
));
771 d_printf(_("Renamed user from %s to %s\n"), argv
[0], argv
[1]);
778 * Set a user's primary group
780 * @param argc Standard main() style argc.
781 * @param argv Standard main() style argv. Initial components are already
784 * @return A shell status integer (0 for success).
787 static int rpc_user_setprimarygroup(struct net_context
*c
, int argc
,
790 NET_API_STATUS status
;
792 struct GROUP_INFO_2
*g2
;
793 struct USER_INFO_1051 u1051
;
794 uint32_t parm_err
= 0;
796 if (argc
!= 2 || c
->display_usage
) {
797 rpc_user_usage(c
, argc
, argv
);
801 status
= NetGroupGetInfo(c
->opt_host
, argv
[1], 2, &buffer
);
803 d_fprintf(stderr
, _("Failed to find group name %s -- %s\n"),
805 libnetapi_get_error_string(c
->netapi_ctx
, status
));
808 g2
= (struct GROUP_INFO_2
*)buffer
;
810 u1051
.usri1051_primary_group_id
= g2
->grpi2_group_id
;
812 NetApiBufferFree(buffer
);
814 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1051,
815 (uint8_t *)&u1051
, &parm_err
);
818 _("Failed to set user's primary group %s to %s - "
819 "%s\n"), argv
[0], argv
[1],
820 libnetapi_get_error_string(c
->netapi_ctx
, status
));
822 d_printf(_("Set primary group of user %s to %s\n"), argv
[0],
829 * Delete a user from a remote RPC server.
831 * @param argc Standard main() style argc.
832 * @param argv Standard main() style argv. Initial components are already
835 * @return A shell status integer (0 for success).
838 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
840 NET_API_STATUS status
;
842 if (argc
< 1 || c
->display_usage
) {
843 rpc_user_usage(c
, argc
, argv
);
847 status
= NetUserDel(c
->opt_host
, argv
[0]);
850 d_fprintf(stderr
, _("Failed to delete user '%s' with: %s.\n"),
852 libnetapi_get_error_string(c
->netapi_ctx
, status
));
855 d_printf(_("Deleted user '%s'.\n"), argv
[0]);
862 * Set a user's password on a remote RPC server.
864 * @param argc Standard main() style argc.
865 * @param argv Standard main() style argv. Initial components are already
868 * @return A shell status integer (0 for success).
871 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
873 NET_API_STATUS status
;
875 struct USER_INFO_1003 u1003
;
876 uint32_t parm_err
= 0;
879 if (argc
< 1 || c
->display_usage
) {
880 rpc_user_usage(c
, argc
, argv
);
885 u1003
.usri1003_password
= argv
[1];
887 ret
= asprintf(&prompt
, _("Enter new password for %s:"),
892 u1003
.usri1003_password
= talloc_strdup(c
, getpass(prompt
));
894 if (u1003
.usri1003_password
== NULL
) {
899 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
901 /* Display results */
904 _("Failed to set password for '%s' with error: %s.\n"),
905 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
914 * List a user's groups from a remote RPC server.
916 * @param argc Standard main() style argc.
917 * @param argv Standard main() style argv. Initial components are already
920 * @return A shell status integer (0 for success)
923 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
926 NET_API_STATUS status
;
927 struct GROUP_USERS_INFO_0
*u0
= NULL
;
928 uint32_t entries_read
= 0;
929 uint32_t total_entries
= 0;
933 if (argc
< 1 || c
->display_usage
) {
934 rpc_user_usage(c
, argc
, argv
);
938 status
= NetUserGetGroups(c
->opt_host
,
941 (uint8_t **)(void *)&u0
,
947 _("Failed to get groups for '%s' with error: %s.\n"),
948 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
953 for (i
=0; i
< entries_read
; i
++) {
954 printf("%s\n", u0
->grui0_name
);
962 * List users on a remote RPC server.
964 * All parameters are provided by the run_rpc_command function, except for
965 * argc, argv which are passed through.
967 * @param domain_sid The domain sid acquired from the remote server.
968 * @param cli A cli_state connected to the server.
969 * @param mem_ctx Talloc context, destroyed on completion of the function.
970 * @param argc Standard main() style argc.
971 * @param argv Standard main() style argv. Initial components are already
974 * @return Normal NTSTATUS return.
977 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
979 NET_API_STATUS status
;
980 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
981 struct NET_DISPLAY_USER
*info
= NULL
;
984 /* Query domain users */
985 if (c
->opt_long_list_entries
)
986 d_printf(_("\nUser name Comment"
987 "\n-----------------------------\n"));
989 uint32_t max_entries
, max_size
;
991 dcerpc_get_query_dispinfo_params(
992 loop_count
, &max_entries
, &max_size
);
994 status
= NetQueryDisplayInformation(c
->opt_host
,
1001 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
1005 info
= (struct NET_DISPLAY_USER
*)buffer
;
1007 for (i
= 0; i
< num_entries
; i
++) {
1009 if (c
->opt_long_list_entries
)
1010 printf("%-21.21s %s\n", info
->usri1_name
,
1011 info
->usri1_comment
);
1013 printf("%s\n", info
->usri1_name
);
1017 NetApiBufferFree(buffer
);
1020 start_idx
+= num_entries
;
1022 } while (status
== ERROR_MORE_DATA
);
1028 * 'net rpc user' entrypoint.
1029 * @param argc Standard main() style argc.
1030 * @param argv Standard main() style argv. Initial components are already
1034 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
1036 NET_API_STATUS status
;
1038 struct functable func
[] = {
1043 N_("Add specified user"),
1044 N_("net rpc user add\n"
1045 " Add specified user")
1051 N_("List domain groups of user"),
1052 N_("net rpc user info\n"
1053 " List domain groups of user")
1059 N_("Remove specified user"),
1060 N_("net rpc user delete\n"
1061 " Remove specified user")
1067 N_("Change user password"),
1068 N_("net rpc user password\n"
1069 " Change user password")
1075 N_("Rename specified user"),
1076 N_("net rpc user rename\n"
1077 " Rename specified user")
1081 rpc_user_setprimarygroup
,
1083 "Set a user's primary group",
1084 "net rpc user setprimarygroup\n"
1085 " Set a user's primary group"
1087 {NULL
, NULL
, 0, NULL
, NULL
}
1090 status
= libnetapi_net_init(&c
->netapi_ctx
);
1094 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1095 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1096 if (c
->opt_kerberos
) {
1097 libnetapi_set_use_kerberos(c
->netapi_ctx
);
1101 if (c
->display_usage
) {
1106 _("List all users"));
1107 net_display_usage_from_functable(func
);
1111 return rpc_user_list(c
, argc
, argv
);
1114 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
1117 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1118 TALLOC_CTX
*mem_ctx
,
1119 struct rpc_sh_ctx
*ctx
,
1120 struct rpc_pipe_client
*pipe_hnd
,
1121 int argc
, const char **argv
)
1123 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
1126 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1127 TALLOC_CTX
*mem_ctx
,
1128 struct rpc_sh_ctx
*ctx
,
1129 struct rpc_pipe_client
*pipe_hnd
,
1130 int argc
, const char **argv
)
1132 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
1135 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1136 TALLOC_CTX
*mem_ctx
,
1137 struct rpc_sh_ctx
*ctx
,
1138 struct rpc_pipe_client
*pipe_hnd
,
1139 int argc
, const char **argv
,
1141 struct net_context
*c
,
1142 TALLOC_CTX
*mem_ctx
,
1143 struct rpc_sh_ctx
*ctx
,
1144 struct rpc_pipe_client
*pipe_hnd
,
1145 struct policy_handle
*user_hnd
,
1146 int argc
, const char **argv
))
1148 struct policy_handle connect_pol
, domain_pol
, user_pol
;
1149 NTSTATUS status
, result
;
1152 enum lsa_SidType type
;
1153 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1156 d_fprintf(stderr
, "%s %s <username>\n", _("Usage:"),
1158 return NT_STATUS_INVALID_PARAMETER
;
1161 ZERO_STRUCT(connect_pol
);
1162 ZERO_STRUCT(domain_pol
);
1163 ZERO_STRUCT(user_pol
);
1165 status
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1166 argv
[0], NULL
, NULL
, &sid
, &type
);
1167 if (!NT_STATUS_IS_OK(status
)) {
1168 d_fprintf(stderr
, _("Could not lookup %s: %s\n"), argv
[0],
1173 if (type
!= SID_NAME_USER
) {
1174 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
1175 sid_type_lookup(type
));
1176 status
= NT_STATUS_NO_SUCH_USER
;
1180 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1181 d_fprintf(stderr
, _("%s is not in our domain\n"), argv
[0]);
1182 status
= NT_STATUS_NO_SUCH_USER
;
1186 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1188 MAXIMUM_ALLOWED_ACCESS
,
1191 if (!NT_STATUS_IS_OK(status
)) {
1194 if (!NT_STATUS_IS_OK(result
)) {
1199 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1201 MAXIMUM_ALLOWED_ACCESS
,
1205 if (!NT_STATUS_IS_OK(status
)) {
1208 if (!NT_STATUS_IS_OK(result
)) {
1213 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1215 MAXIMUM_ALLOWED_ACCESS
,
1219 if (!NT_STATUS_IS_OK(status
)) {
1222 if (!NT_STATUS_IS_OK(result
)) {
1227 status
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1230 if (is_valid_policy_hnd(&user_pol
)) {
1231 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1233 if (is_valid_policy_hnd(&domain_pol
)) {
1234 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1236 if (is_valid_policy_hnd(&connect_pol
)) {
1237 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
1242 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1243 TALLOC_CTX
*mem_ctx
,
1244 struct rpc_sh_ctx
*ctx
,
1245 struct rpc_pipe_client
*pipe_hnd
,
1246 struct policy_handle
*user_hnd
,
1247 int argc
, const char **argv
)
1249 NTSTATUS status
, result
;
1250 union samr_UserInfo
*info
= NULL
;
1251 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1254 d_fprintf(stderr
, "%s %s show <username>\n", _("Usage:"),
1256 return NT_STATUS_INVALID_PARAMETER
;
1259 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1264 if (!NT_STATUS_IS_OK(status
)) {
1267 if (!NT_STATUS_IS_OK(result
)) {
1271 d_printf(_("user rid: %d, group rid: %d\n"),
1273 info
->info21
.primary_gid
);
1278 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1279 TALLOC_CTX
*mem_ctx
,
1280 struct rpc_sh_ctx
*ctx
,
1281 struct rpc_pipe_client
*pipe_hnd
,
1282 int argc
, const char **argv
)
1284 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1285 rpc_sh_user_show_internals
);
1288 #define FETCHSTR(name, rec) \
1289 do { if (strequal(ctx->thiscmd, name)) { \
1290 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1293 #define SETSTR(name, rec, flag) \
1294 do { if (strequal(ctx->thiscmd, name)) { \
1295 init_lsa_String(&(info->info21.rec), argv[0]); \
1296 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1299 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1300 TALLOC_CTX
*mem_ctx
,
1301 struct rpc_sh_ctx
*ctx
,
1302 struct rpc_pipe_client
*pipe_hnd
,
1303 struct policy_handle
*user_hnd
,
1304 int argc
, const char **argv
)
1306 NTSTATUS status
, result
;
1307 const char *username
;
1308 const char *oldval
= "";
1309 union samr_UserInfo
*info
= NULL
;
1310 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1313 d_fprintf(stderr
, "%s %s <username> [new value|NULL]\n",
1314 _("Usage:"), ctx
->whoami
);
1315 return NT_STATUS_INVALID_PARAMETER
;
1318 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1323 if (!NT_STATUS_IS_OK(status
)) {
1326 if (!NT_STATUS_IS_OK(result
)) {
1330 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1332 FETCHSTR("fullname", full_name
);
1333 FETCHSTR("homedir", home_directory
);
1334 FETCHSTR("homedrive", home_drive
);
1335 FETCHSTR("logonscript", logon_script
);
1336 FETCHSTR("profilepath", profile_path
);
1337 FETCHSTR("description", description
);
1340 d_printf(_("%s's %s: [%s]\n"), username
, ctx
->thiscmd
, oldval
);
1344 if (strcmp(argv
[0], "NULL") == 0) {
1348 ZERO_STRUCT(info
->info21
);
1350 SETSTR("fullname", full_name
, FULL_NAME
);
1351 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1352 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1353 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1354 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1355 SETSTR("description", description
, DESCRIPTION
);
1357 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1362 if (!NT_STATUS_IS_OK(status
)) {
1368 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username
,
1369 ctx
->thiscmd
, oldval
, argv
[0]);
1376 #define HANDLEFLG(name, rec) \
1377 do { if (strequal(ctx->thiscmd, name)) { \
1378 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1380 newflags = oldflags | ACB_##rec; \
1382 newflags = oldflags & ~ACB_##rec; \
1385 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1386 TALLOC_CTX
*mem_ctx
,
1387 struct rpc_sh_ctx
*ctx
,
1388 struct rpc_pipe_client
*pipe_hnd
,
1389 int argc
, const char **argv
)
1391 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1392 rpc_sh_user_str_edit_internals
);
1395 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1396 TALLOC_CTX
*mem_ctx
,
1397 struct rpc_sh_ctx
*ctx
,
1398 struct rpc_pipe_client
*pipe_hnd
,
1399 struct policy_handle
*user_hnd
,
1400 int argc
, const char **argv
)
1402 NTSTATUS status
, result
;
1403 const char *username
;
1404 const char *oldval
= "unknown";
1405 uint32 oldflags
, newflags
;
1407 union samr_UserInfo
*info
= NULL
;
1408 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1411 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1412 !strequal(argv
[0], "no"))) {
1413 /* TRANSATORS: The yes|no here are program keywords. Please do
1415 d_fprintf(stderr
, _("Usage: %s <username> [yes|no]\n"),
1417 return NT_STATUS_INVALID_PARAMETER
;
1420 newval
= strequal(argv
[0], "yes");
1422 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1427 if (!NT_STATUS_IS_OK(status
)) {
1430 if (!NT_STATUS_IS_OK(result
)) {
1434 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1435 oldflags
= info
->info21
.acct_flags
;
1436 newflags
= info
->info21
.acct_flags
;
1438 HANDLEFLG("disabled", DISABLED
);
1439 HANDLEFLG("pwnotreq", PWNOTREQ
);
1440 HANDLEFLG("autolock", AUTOLOCK
);
1441 HANDLEFLG("pwnoexp", PWNOEXP
);
1444 d_printf(_("%s's %s flag: %s\n"), username
, ctx
->thiscmd
,
1449 ZERO_STRUCT(info
->info21
);
1451 info
->info21
.acct_flags
= newflags
;
1452 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1454 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1459 if (!NT_STATUS_IS_OK(status
)) {
1463 if (NT_STATUS_IS_OK(result
)) {
1464 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username
,
1465 ctx
->thiscmd
, oldval
, argv
[0]);
1473 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1474 TALLOC_CTX
*mem_ctx
,
1475 struct rpc_sh_ctx
*ctx
,
1476 struct rpc_pipe_client
*pipe_hnd
,
1477 int argc
, const char **argv
)
1479 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1480 rpc_sh_user_flag_edit_internals
);
1483 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1484 TALLOC_CTX
*mem_ctx
,
1485 struct rpc_sh_ctx
*ctx
)
1487 static struct rpc_sh_cmd cmds
[] = {
1489 { "fullname", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1490 N_("Show/Set a user's full name") },
1492 { "homedir", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1493 N_("Show/Set a user's home directory") },
1495 { "homedrive", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1496 N_("Show/Set a user's home drive") },
1498 { "logonscript", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1499 N_("Show/Set a user's logon script") },
1501 { "profilepath", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1502 N_("Show/Set a user's profile path") },
1504 { "description", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1505 N_("Show/Set a user's description") },
1507 { "disabled", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1508 N_("Show/Set whether a user is disabled") },
1510 { "autolock", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1511 N_("Show/Set whether a user locked out") },
1513 { "pwnotreq", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1514 N_("Show/Set whether a user does not need a password") },
1516 { "pwnoexp", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1517 N_("Show/Set whether a user's password does not expire") },
1519 { NULL
, NULL
, 0, NULL
, NULL
}
1525 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1526 TALLOC_CTX
*mem_ctx
,
1527 struct rpc_sh_ctx
*ctx
)
1529 static struct rpc_sh_cmd cmds
[] = {
1531 { "list", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_list
,
1532 N_("List available users") },
1534 { "info", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_info
,
1535 N_("List the domain groups a user is member of") },
1537 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_show
,
1538 N_("Show info about a user") },
1540 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1541 N_("Show/Modify a user's fields") },
1543 { NULL
, NULL
, 0, NULL
, NULL
}
1549 /****************************************************************************/
1552 * Basic usage function for 'net rpc group'.
1553 * @param argc Standard main() style argc.
1554 * @param argv Standard main() style argv. Initial components are already
1558 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1560 return net_group_usage(c
, argc
, argv
);
1564 * Delete group on a remote RPC server.
1566 * All parameters are provided by the run_rpc_command function, except for
1567 * argc, argv which are passed through.
1569 * @param domain_sid The domain sid acquired from the remote server.
1570 * @param cli A cli_state connected to the server.
1571 * @param mem_ctx Talloc context, destroyed on completion of the function.
1572 * @param argc Standard main() style argc.
1573 * @param argv Standard main() style argv. Initial components are already
1576 * @return Normal NTSTATUS return.
1579 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1580 const struct dom_sid
*domain_sid
,
1581 const char *domain_name
,
1582 struct cli_state
*cli
,
1583 struct rpc_pipe_client
*pipe_hnd
,
1584 TALLOC_CTX
*mem_ctx
,
1588 struct policy_handle connect_pol
, domain_pol
, group_pol
, user_pol
;
1589 bool group_is_primary
= false;
1590 NTSTATUS status
, result
;
1592 struct samr_RidAttrArray
*rids
= NULL
;
1595 /* struct samr_RidWithAttribute *user_gids; */
1596 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1598 struct samr_Ids group_rids
, name_types
;
1599 struct lsa_String lsa_acct_name
;
1600 union samr_UserInfo
*info
= NULL
;
1602 if (argc
< 1 || c
->display_usage
) {
1603 rpc_group_usage(c
, argc
,argv
);
1604 return NT_STATUS_OK
; /* ok? */
1607 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1609 MAXIMUM_ALLOWED_ACCESS
,
1612 if (!NT_STATUS_IS_OK(status
)) {
1613 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1617 if (!NT_STATUS_IS_OK(result
)) {
1619 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1623 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1625 MAXIMUM_ALLOWED_ACCESS
,
1626 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1629 if (!NT_STATUS_IS_OK(status
)) {
1630 d_fprintf(stderr
, _("Request open_domain failed\n"));
1634 if (!NT_STATUS_IS_OK(result
)) {
1636 d_fprintf(stderr
, _("Request open_domain failed\n"));
1640 init_lsa_String(&lsa_acct_name
, argv
[0]);
1642 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1649 if (!NT_STATUS_IS_OK(status
)) {
1650 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1654 if (!NT_STATUS_IS_OK(result
)) {
1656 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1660 switch (name_types
.ids
[0])
1662 case SID_NAME_DOM_GRP
:
1663 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
1665 MAXIMUM_ALLOWED_ACCESS
,
1669 if (!NT_STATUS_IS_OK(status
)) {
1670 d_fprintf(stderr
, _("Request open_group failed"));
1674 if (!NT_STATUS_IS_OK(result
)) {
1676 d_fprintf(stderr
, _("Request open_group failed"));
1680 group_rid
= group_rids
.ids
[0];
1682 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
1686 if (!NT_STATUS_IS_OK(status
)) {
1688 _("Unable to query group members of %s"),
1693 if (!NT_STATUS_IS_OK(result
)) {
1696 _("Unable to query group members of %s"),
1701 if (c
->opt_verbose
) {
1703 _("Domain Group %s (rid: %d) has %d members\n"),
1704 argv
[0],group_rid
, rids
->count
);
1707 /* Check if group is anyone's primary group */
1708 for (i
= 0; i
< rids
->count
; i
++)
1710 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1712 MAXIMUM_ALLOWED_ACCESS
,
1716 if (!NT_STATUS_IS_OK(status
)) {
1718 _("Unable to open group member %d\n"),
1723 if (!NT_STATUS_IS_OK(result
)) {
1726 _("Unable to open group member %d\n"),
1731 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1736 if (!NT_STATUS_IS_OK(status
)) {
1738 _("Unable to lookup userinfo for group "
1744 if (!NT_STATUS_IS_OK(result
)) {
1747 _("Unable to lookup userinfo for group "
1753 if (info
->info21
.primary_gid
== group_rid
) {
1754 if (c
->opt_verbose
) {
1755 d_printf(_("Group is primary group "
1757 info
->info21
.account_name
.string
);
1759 group_is_primary
= true;
1762 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1765 if (group_is_primary
) {
1766 d_fprintf(stderr
, _("Unable to delete group because "
1767 "some of it's members have it as primary "
1769 status
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1773 /* remove all group members */
1774 for (i
= 0; i
< rids
->count
; i
++)
1777 d_printf(_("Remove group member %d..."),
1779 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
1783 if (!NT_STATUS_IS_OK(status
)) {
1787 if (NT_STATUS_IS_OK(result
)) {
1789 d_printf(_("ok\n"));
1792 d_printf("%s\n", _("failed"));
1797 status
= dcerpc_samr_DeleteDomainGroup(b
, mem_ctx
,
1800 if (!NT_STATUS_IS_OK(status
)) {
1807 /* removing a local group is easier... */
1808 case SID_NAME_ALIAS
:
1809 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
1811 MAXIMUM_ALLOWED_ACCESS
,
1815 if (!NT_STATUS_IS_OK(status
)) {
1816 d_fprintf(stderr
, _("Request open_alias failed\n"));
1819 if (!NT_STATUS_IS_OK(result
)) {
1821 d_fprintf(stderr
, _("Request open_alias failed\n"));
1825 status
= dcerpc_samr_DeleteDomAlias(b
, mem_ctx
,
1828 if (!NT_STATUS_IS_OK(status
)) {
1836 d_fprintf(stderr
, _("%s is of type %s. This command is only "
1837 "for deleting local or global groups\n"),
1838 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1839 status
= NT_STATUS_UNSUCCESSFUL
;
1843 if (NT_STATUS_IS_OK(status
)) {
1845 d_printf(_("Deleted %s '%s'\n"),
1846 sid_type_lookup(name_types
.ids
[0]), argv
[0]);
1848 d_fprintf(stderr
, _("Deleting of %s failed: %s\n"), argv
[0],
1849 get_friendly_nt_error_msg(status
));
1857 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1859 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1860 rpc_group_delete_internals
, argc
,argv
);
1863 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1865 NET_API_STATUS status
;
1866 struct GROUP_INFO_1 info1
;
1867 uint32_t parm_error
= 0;
1869 if (argc
!= 1 || c
->display_usage
) {
1870 rpc_group_usage(c
, argc
, argv
);
1876 info1
.grpi1_name
= argv
[0];
1877 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1878 info1
.grpi1_comment
= c
->opt_comment
;
1881 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1885 _("Failed to add group '%s' with error: %s.\n"),
1886 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1890 d_printf(_("Added group '%s'.\n"), argv
[0]);
1896 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1898 NET_API_STATUS status
;
1899 struct LOCALGROUP_INFO_1 info1
;
1900 uint32_t parm_error
= 0;
1902 if (argc
!= 1 || c
->display_usage
) {
1903 rpc_group_usage(c
, argc
, argv
);
1909 info1
.lgrpi1_name
= argv
[0];
1910 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1911 info1
.lgrpi1_comment
= c
->opt_comment
;
1914 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1918 _("Failed to add alias '%s' with error: %s.\n"),
1919 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1923 d_printf(_("Added alias '%s'.\n"), argv
[0]);
1929 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1931 if (c
->opt_localgroup
)
1932 return rpc_alias_add_internals(c
, argc
, argv
);
1934 return rpc_group_add_internals(c
, argc
, argv
);
1937 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1938 TALLOC_CTX
*mem_ctx
,
1940 struct dom_sid
*sid
,
1941 enum lsa_SidType
*type
)
1943 struct dom_sid
*sids
= NULL
;
1944 enum lsa_SidType
*types
= NULL
;
1945 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1946 struct policy_handle lsa_pol
;
1947 NTSTATUS status
, result
;
1948 struct dcerpc_binding_handle
*b
;
1950 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1952 if (!NT_STATUS_IS_OK(status
)) {
1956 b
= pipe_hnd
->binding_handle
;
1958 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1959 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1961 if (!NT_STATUS_IS_OK(status
)) {
1965 status
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1966 &name
, NULL
, 1, &sids
, &types
);
1968 if (NT_STATUS_IS_OK(status
)) {
1969 sid_copy(sid
, &sids
[0]);
1973 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1977 TALLOC_FREE(pipe_hnd
);
1980 if (!NT_STATUS_IS_OK(status
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
1982 /* Try as S-1-5-whatever */
1984 struct dom_sid tmp_sid
;
1986 if (string_to_sid(&tmp_sid
, name
)) {
1987 sid_copy(sid
, &tmp_sid
);
1988 *type
= SID_NAME_UNKNOWN
;
1989 status
= NT_STATUS_OK
;
1996 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
1997 TALLOC_CTX
*mem_ctx
,
1998 const struct dom_sid
*group_sid
,
2001 struct policy_handle connect_pol
, domain_pol
;
2002 NTSTATUS status
, result
;
2004 struct policy_handle group_pol
;
2005 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2007 struct samr_Ids rids
, rid_types
;
2008 struct lsa_String lsa_acct_name
;
2012 sid_copy(&sid
, group_sid
);
2014 if (!sid_split_rid(&sid
, &group_rid
)) {
2015 return NT_STATUS_UNSUCCESSFUL
;
2018 /* Get sam policy handle */
2019 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2021 MAXIMUM_ALLOWED_ACCESS
,
2024 if (!NT_STATUS_IS_OK(status
)) {
2027 if (!NT_STATUS_IS_OK(result
)) {
2031 /* Get domain policy handle */
2032 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2034 MAXIMUM_ALLOWED_ACCESS
,
2038 if (!NT_STATUS_IS_OK(status
)) {
2041 if (!NT_STATUS_IS_OK(result
)) {
2045 init_lsa_String(&lsa_acct_name
, member
);
2047 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2054 if (!NT_STATUS_IS_OK(status
)) {
2055 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2060 if (!NT_STATUS_IS_OK(result
)) {
2062 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2067 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2069 MAXIMUM_ALLOWED_ACCESS
,
2073 if (!NT_STATUS_IS_OK(status
)) {
2077 if (!NT_STATUS_IS_OK(result
)) {
2082 status
= dcerpc_samr_AddGroupMember(b
, mem_ctx
,
2085 0x0005, /* unknown flags */
2087 if (!NT_STATUS_IS_OK(status
)) {
2094 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2098 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2099 TALLOC_CTX
*mem_ctx
,
2100 const struct dom_sid
*alias_sid
,
2103 struct policy_handle connect_pol
, domain_pol
;
2104 NTSTATUS status
, result
;
2106 struct policy_handle alias_pol
;
2107 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2109 struct dom_sid member_sid
;
2110 enum lsa_SidType member_type
;
2114 sid_copy(&sid
, alias_sid
);
2116 if (!sid_split_rid(&sid
, &alias_rid
)) {
2117 return NT_STATUS_UNSUCCESSFUL
;
2120 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2121 member
, &member_sid
, &member_type
);
2123 if (!NT_STATUS_IS_OK(result
)) {
2124 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2129 /* Get sam policy handle */
2130 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2132 MAXIMUM_ALLOWED_ACCESS
,
2135 if (!NT_STATUS_IS_OK(status
)) {
2138 if (!NT_STATUS_IS_OK(result
)) {
2143 /* Get domain policy handle */
2144 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2146 MAXIMUM_ALLOWED_ACCESS
,
2150 if (!NT_STATUS_IS_OK(status
)) {
2153 if (!NT_STATUS_IS_OK(result
)) {
2158 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2160 MAXIMUM_ALLOWED_ACCESS
,
2164 if (!NT_STATUS_IS_OK(status
)) {
2167 if (!NT_STATUS_IS_OK(result
)) {
2171 status
= dcerpc_samr_AddAliasMember(b
, mem_ctx
,
2175 if (!NT_STATUS_IS_OK(status
)) {
2182 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2186 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
2187 const struct dom_sid
*domain_sid
,
2188 const char *domain_name
,
2189 struct cli_state
*cli
,
2190 struct rpc_pipe_client
*pipe_hnd
,
2191 TALLOC_CTX
*mem_ctx
,
2195 struct dom_sid group_sid
;
2196 enum lsa_SidType group_type
;
2198 if (argc
!= 2 || c
->display_usage
) {
2201 _("net rpc group addmem <group> <member>\n"
2202 " Add a member to a group\n"
2203 " group\tGroup to add member to\n"
2204 " member\tMember to add to group\n"));
2205 return NT_STATUS_UNSUCCESSFUL
;
2208 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2209 &group_sid
, &group_type
))) {
2210 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2212 return NT_STATUS_UNSUCCESSFUL
;
2215 if (group_type
== SID_NAME_DOM_GRP
) {
2216 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2217 &group_sid
, argv
[1]);
2219 if (!NT_STATUS_IS_OK(result
)) {
2220 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2221 argv
[1], argv
[0], nt_errstr(result
));
2226 if (group_type
== SID_NAME_ALIAS
) {
2227 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2228 &group_sid
, argv
[1]);
2230 if (!NT_STATUS_IS_OK(result
)) {
2231 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2232 argv
[1], argv
[0], nt_errstr(result
));
2237 d_fprintf(stderr
, _("Can only add members to global or local groups "
2238 "which %s is not\n"), argv
[0]);
2240 return NT_STATUS_UNSUCCESSFUL
;
2243 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2245 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2246 rpc_group_addmem_internals
,
2250 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2251 struct rpc_pipe_client
*pipe_hnd
,
2252 TALLOC_CTX
*mem_ctx
,
2253 const struct dom_sid
*group_sid
,
2256 struct policy_handle connect_pol
, domain_pol
;
2257 NTSTATUS status
, result
;
2259 struct policy_handle group_pol
;
2260 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2262 struct samr_Ids rids
, rid_types
;
2263 struct lsa_String lsa_acct_name
;
2267 sid_copy(&sid
, group_sid
);
2269 if (!sid_split_rid(&sid
, &group_rid
))
2270 return NT_STATUS_UNSUCCESSFUL
;
2272 /* Get sam policy handle */
2273 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2275 MAXIMUM_ALLOWED_ACCESS
,
2278 if (!NT_STATUS_IS_OK(status
)) {
2281 if (!NT_STATUS_IS_OK(result
)) {
2286 /* Get domain policy handle */
2287 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2289 MAXIMUM_ALLOWED_ACCESS
,
2293 if (!NT_STATUS_IS_OK(status
)) {
2296 if (!NT_STATUS_IS_OK(result
)) {
2300 init_lsa_String(&lsa_acct_name
, member
);
2302 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
2309 if (!NT_STATUS_IS_OK(status
)) {
2310 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2315 if (!NT_STATUS_IS_OK(result
)) {
2317 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2322 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2324 MAXIMUM_ALLOWED_ACCESS
,
2328 if (!NT_STATUS_IS_OK(status
)) {
2331 if (!NT_STATUS_IS_OK(result
)) {
2336 status
= dcerpc_samr_DeleteGroupMember(b
, mem_ctx
,
2340 if (!NT_STATUS_IS_OK(status
)) {
2346 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2350 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2351 TALLOC_CTX
*mem_ctx
,
2352 const struct dom_sid
*alias_sid
,
2355 struct policy_handle connect_pol
, domain_pol
;
2356 NTSTATUS status
, result
;
2358 struct policy_handle alias_pol
;
2359 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2361 struct dom_sid member_sid
;
2362 enum lsa_SidType member_type
;
2366 sid_copy(&sid
, alias_sid
);
2368 if (!sid_split_rid(&sid
, &alias_rid
))
2369 return NT_STATUS_UNSUCCESSFUL
;
2371 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2372 member
, &member_sid
, &member_type
);
2374 if (!NT_STATUS_IS_OK(result
)) {
2375 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2380 /* Get sam policy handle */
2381 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2383 MAXIMUM_ALLOWED_ACCESS
,
2386 if (!NT_STATUS_IS_OK(status
)) {
2389 if (!NT_STATUS_IS_OK(result
)) {
2394 /* Get domain policy handle */
2395 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2397 MAXIMUM_ALLOWED_ACCESS
,
2401 if (!NT_STATUS_IS_OK(status
)) {
2404 if (!NT_STATUS_IS_OK(result
)) {
2409 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2411 MAXIMUM_ALLOWED_ACCESS
,
2415 if (!NT_STATUS_IS_OK(status
)) {
2419 if (!NT_STATUS_IS_OK(result
)) {
2423 status
= dcerpc_samr_DeleteAliasMember(b
, mem_ctx
,
2428 if (!NT_STATUS_IS_OK(status
)) {
2435 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
2439 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2440 const struct dom_sid
*domain_sid
,
2441 const char *domain_name
,
2442 struct cli_state
*cli
,
2443 struct rpc_pipe_client
*pipe_hnd
,
2444 TALLOC_CTX
*mem_ctx
,
2448 struct dom_sid group_sid
;
2449 enum lsa_SidType group_type
;
2451 if (argc
!= 2 || c
->display_usage
) {
2454 _("net rpc group delmem <group> <member>\n"
2455 " Delete a member from a group\n"
2456 " group\tGroup to delete member from\n"
2457 " member\tMember to delete from group\n"));
2458 return NT_STATUS_UNSUCCESSFUL
;
2461 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2462 &group_sid
, &group_type
))) {
2463 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2465 return NT_STATUS_UNSUCCESSFUL
;
2468 if (group_type
== SID_NAME_DOM_GRP
) {
2469 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2470 &group_sid
, argv
[1]);
2472 if (!NT_STATUS_IS_OK(result
)) {
2473 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2474 argv
[1], argv
[0], nt_errstr(result
));
2479 if (group_type
== SID_NAME_ALIAS
) {
2480 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2481 &group_sid
, argv
[1]);
2483 if (!NT_STATUS_IS_OK(result
)) {
2484 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2485 argv
[1], argv
[0], nt_errstr(result
));
2490 d_fprintf(stderr
, _("Can only delete members from global or local "
2491 "groups which %s is not\n"), argv
[0]);
2493 return NT_STATUS_UNSUCCESSFUL
;
2496 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2498 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2499 rpc_group_delmem_internals
,
2504 * List groups on a remote RPC server.
2506 * All parameters are provided by the run_rpc_command function, except for
2507 * argc, argv which are passes through.
2509 * @param domain_sid The domain sid acquired from the remote server.
2510 * @param cli A cli_state connected to the server.
2511 * @param mem_ctx Talloc context, destroyed on completion of the function.
2512 * @param argc Standard main() style argc.
2513 * @param argv Standard main() style argv. Initial components are already
2516 * @return Normal NTSTATUS return.
2519 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2520 const struct dom_sid
*domain_sid
,
2521 const char *domain_name
,
2522 struct cli_state
*cli
,
2523 struct rpc_pipe_client
*pipe_hnd
,
2524 TALLOC_CTX
*mem_ctx
,
2528 struct policy_handle connect_pol
, domain_pol
;
2529 NTSTATUS status
, result
;
2530 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2531 struct samr_SamArray
*groups
= NULL
;
2532 bool global
= false;
2534 bool builtin
= false;
2535 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2537 if (c
->display_usage
) {
2540 _("net rpc group list [global] [local] [builtin]\n"
2541 " List groups on RPC server\n"
2542 " global\tList global groups\n"
2543 " local\tList local groups\n"
2544 " builtin\tList builtin groups\n"
2545 " If none of global, local or builtin is "
2546 "specified, all three options are considered "
2548 return NT_STATUS_OK
;
2557 for (i
=0; i
<argc
; i
++) {
2558 if (strequal(argv
[i
], "global"))
2561 if (strequal(argv
[i
], "local"))
2564 if (strequal(argv
[i
], "builtin"))
2568 /* Get sam policy handle */
2570 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
2572 MAXIMUM_ALLOWED_ACCESS
,
2575 if (!NT_STATUS_IS_OK(status
)) {
2578 if (!NT_STATUS_IS_OK(result
)) {
2583 /* Get domain policy handle */
2585 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2587 MAXIMUM_ALLOWED_ACCESS
,
2588 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2591 if (!NT_STATUS_IS_OK(status
)) {
2594 if (!NT_STATUS_IS_OK(result
)) {
2599 /* Query domain groups */
2600 if (c
->opt_long_list_entries
)
2601 d_printf(_("\nGroup name Comment"
2602 "\n-----------------------------\n"));
2604 uint32_t max_size
, total_size
, returned_size
;
2605 union samr_DispInfo info
;
2609 dcerpc_get_query_dispinfo_params(
2610 loop_count
, &max_entries
, &max_size
);
2612 status
= dcerpc_samr_QueryDisplayInfo(b
, mem_ctx
,
2622 if (!NT_STATUS_IS_OK(status
)) {
2625 num_entries
= info
.info3
.count
;
2626 start_idx
+= info
.info3
.count
;
2628 if (!NT_STATUS_IS_OK(result
) &&
2629 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2632 for (i
= 0; i
< num_entries
; i
++) {
2634 const char *group
= NULL
;
2635 const char *desc
= NULL
;
2637 group
= info
.info3
.entries
[i
].account_name
.string
;
2638 desc
= info
.info3
.entries
[i
].description
.string
;
2640 if (c
->opt_long_list_entries
)
2641 printf("%-21.21s %-50.50s\n",
2644 printf("%s\n", group
);
2646 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2647 /* query domain aliases */
2652 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2659 if (!NT_STATUS_IS_OK(status
)) {
2662 if (!NT_STATUS_IS_OK(result
) &&
2663 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2666 for (i
= 0; i
< num_entries
; i
++) {
2668 const char *description
= NULL
;
2670 if (c
->opt_long_list_entries
) {
2672 struct policy_handle alias_pol
;
2673 union samr_AliasInfo
*info
= NULL
;
2676 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2679 groups
->entries
[i
].idx
,
2682 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2683 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2688 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2689 status
= dcerpc_samr_Close(b
, mem_ctx
,
2692 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2693 description
= info
->description
.string
;
2699 if (description
!= NULL
) {
2700 printf("%-21.21s %-50.50s\n",
2701 groups
->entries
[i
].name
.string
,
2704 printf("%s\n", groups
->entries
[i
].name
.string
);
2707 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2708 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
2709 /* Get builtin policy handle */
2711 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
2713 MAXIMUM_ALLOWED_ACCESS
,
2714 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2717 if (!NT_STATUS_IS_OK(status
)) {
2720 if (!NT_STATUS_IS_OK(result
)) {
2725 /* query builtin aliases */
2728 if (!builtin
) break;
2730 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
2737 if (!NT_STATUS_IS_OK(status
)) {
2740 if (!NT_STATUS_IS_OK(result
) &&
2741 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
2746 for (i
= 0; i
< num_entries
; i
++) {
2748 const char *description
= NULL
;
2750 if (c
->opt_long_list_entries
) {
2752 struct policy_handle alias_pol
;
2753 union samr_AliasInfo
*info
= NULL
;
2756 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2759 groups
->entries
[i
].idx
,
2762 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2763 status
= dcerpc_samr_QueryAliasInfo(b
, mem_ctx
,
2768 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2769 status
= dcerpc_samr_Close(b
, mem_ctx
,
2772 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(_result
)) {
2773 description
= info
->description
.string
;
2779 if (description
!= NULL
) {
2780 printf("%-21.21s %-50.50s\n",
2781 groups
->entries
[i
].name
.string
,
2784 printf("%s\n", groups
->entries
[i
].name
.string
);
2787 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2795 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2797 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2798 rpc_group_list_internals
,
2802 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2803 struct rpc_pipe_client
*pipe_hnd
,
2804 TALLOC_CTX
*mem_ctx
,
2805 const char *domain_name
,
2806 const struct dom_sid
*domain_sid
,
2807 struct policy_handle
*domain_pol
,
2810 NTSTATUS result
, status
;
2811 struct policy_handle group_pol
;
2812 uint32 num_members
, *group_rids
;
2814 struct samr_RidAttrArray
*rids
= NULL
;
2815 struct lsa_Strings names
;
2816 struct samr_Ids types
;
2817 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2820 sid_to_fstring(sid_str
, domain_sid
);
2822 status
= dcerpc_samr_OpenGroup(b
, mem_ctx
,
2824 MAXIMUM_ALLOWED_ACCESS
,
2828 if (!NT_STATUS_IS_OK(status
)) {
2831 if (!NT_STATUS_IS_OK(result
)) {
2835 status
= dcerpc_samr_QueryGroupMember(b
, mem_ctx
,
2839 if (!NT_STATUS_IS_OK(status
)) {
2842 if (!NT_STATUS_IS_OK(result
)) {
2846 num_members
= rids
->count
;
2847 group_rids
= rids
->rids
;
2849 while (num_members
> 0) {
2850 int this_time
= 512;
2852 if (num_members
< this_time
)
2853 this_time
= num_members
;
2855 status
= dcerpc_samr_LookupRids(b
, mem_ctx
,
2862 if (!NT_STATUS_IS_OK(status
)) {
2865 if (!NT_STATUS_IS_OK(result
)) {
2869 /* We only have users as members, but make the output
2870 the same as the output of alias members */
2872 for (i
= 0; i
< this_time
; i
++) {
2874 if (c
->opt_long_list_entries
) {
2875 printf("%s-%d %s\\%s %d\n", sid_str
,
2876 group_rids
[i
], domain_name
,
2877 names
.names
[i
].string
,
2880 printf("%s\\%s\n", domain_name
,
2881 names
.names
[i
].string
);
2885 num_members
-= this_time
;
2889 return NT_STATUS_OK
;
2892 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2893 struct rpc_pipe_client
*pipe_hnd
,
2894 TALLOC_CTX
*mem_ctx
,
2895 struct policy_handle
*domain_pol
,
2898 NTSTATUS result
, status
;
2899 struct rpc_pipe_client
*lsa_pipe
;
2900 struct policy_handle alias_pol
, lsa_pol
;
2902 struct dom_sid
*alias_sids
;
2905 enum lsa_SidType
*types
;
2907 struct lsa_SidArray sid_array
;
2908 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
2910 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
2912 MAXIMUM_ALLOWED_ACCESS
,
2916 if (!NT_STATUS_IS_OK(status
)) {
2919 if (!NT_STATUS_IS_OK(result
)) {
2923 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
2927 if (!NT_STATUS_IS_OK(status
)) {
2928 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2931 if (!NT_STATUS_IS_OK(result
)) {
2932 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2936 num_members
= sid_array
.num_sids
;
2938 if (num_members
== 0) {
2939 return NT_STATUS_OK
;
2942 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2943 &ndr_table_lsarpc
.syntax_id
,
2945 if (!NT_STATUS_IS_OK(result
)) {
2946 d_fprintf(stderr
, _("Couldn't open LSA pipe. Error was %s\n"),
2947 nt_errstr(result
) );
2951 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2952 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
2954 if (!NT_STATUS_IS_OK(result
)) {
2955 d_fprintf(stderr
, _("Couldn't open LSA policy handle\n"));
2956 TALLOC_FREE(lsa_pipe
);
2960 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, struct dom_sid
, num_members
);
2962 d_fprintf(stderr
, _("Out of memory\n"));
2963 TALLOC_FREE(lsa_pipe
);
2964 return NT_STATUS_NO_MEMORY
;
2967 for (i
=0; i
<num_members
; i
++) {
2968 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2971 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2972 num_members
, alias_sids
,
2973 &domains
, &names
, &types
);
2975 if (!NT_STATUS_IS_OK(result
) &&
2976 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2977 d_fprintf(stderr
, _("Couldn't lookup SIDs\n"));
2978 TALLOC_FREE(lsa_pipe
);
2982 for (i
= 0; i
< num_members
; i
++) {
2984 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2986 if (c
->opt_long_list_entries
) {
2987 printf("%s %s\\%s %d\n", sid_str
,
2988 domains
[i
] ? domains
[i
] : _("*unknown*"),
2989 names
[i
] ? names
[i
] : _("*unknown*"), types
[i
]);
2992 printf("%s\\%s\n", domains
[i
], names
[i
]);
2994 printf("%s\n", sid_str
);
2998 TALLOC_FREE(lsa_pipe
);
2999 return NT_STATUS_OK
;
3002 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
3003 const struct dom_sid
*domain_sid
,
3004 const char *domain_name
,
3005 struct cli_state
*cli
,
3006 struct rpc_pipe_client
*pipe_hnd
,
3007 TALLOC_CTX
*mem_ctx
,
3011 NTSTATUS result
, status
;
3012 struct policy_handle connect_pol
, domain_pol
;
3013 struct samr_Ids rids
, rid_types
;
3014 struct lsa_String lsa_acct_name
;
3015 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3017 /* Get sam policy handle */
3019 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
3021 MAXIMUM_ALLOWED_ACCESS
,
3024 if (!NT_STATUS_IS_OK(status
)) {
3027 if (!NT_STATUS_IS_OK(result
)) {
3031 /* Get domain policy handle */
3033 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3035 MAXIMUM_ALLOWED_ACCESS
,
3036 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
3039 if (!NT_STATUS_IS_OK(status
)) {
3042 if (!NT_STATUS_IS_OK(result
)) {
3046 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
3048 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3055 if (!NT_STATUS_IS_OK(status
)) {
3059 if (!NT_STATUS_IS_OK(result
)) {
3061 /* Ok, did not find it in the global sam, try with builtin */
3063 struct dom_sid sid_Builtin
;
3065 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
3067 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
3069 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
3071 MAXIMUM_ALLOWED_ACCESS
,
3075 if (!NT_STATUS_IS_OK(status
)) {
3078 if (!NT_STATUS_IS_OK(result
)) {
3079 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3084 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
3091 if (!NT_STATUS_IS_OK(status
)) {
3094 if (!NT_STATUS_IS_OK(result
)) {
3095 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3101 if (rids
.count
!= 1) {
3102 d_fprintf(stderr
, _("Couldn't find group %s\n"),
3107 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
3108 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
3109 domain_sid
, &domain_pol
,
3113 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
3114 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
3118 return NT_STATUS_NO_SUCH_GROUP
;
3121 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
3123 if (argc
!= 1 || c
->display_usage
) {
3124 return rpc_group_usage(c
, argc
, argv
);
3127 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
3128 rpc_group_members_internals
,
3132 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
3134 NET_API_STATUS status
;
3135 struct GROUP_INFO_0 g0
;
3139 d_printf(_("Usage:\n"));
3140 d_printf("net rpc group rename group newname\n");
3144 g0
.grpi0_name
= argv
[1];
3146 status
= NetGroupSetInfo(c
->opt_host
,
3153 d_fprintf(stderr
, _("Renaming group %s failed with: %s\n"),
3154 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
3162 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
3164 if (argc
!= 2 || c
->display_usage
) {
3165 return rpc_group_usage(c
, argc
, argv
);
3168 return rpc_group_rename_internals(c
, argc
, argv
);
3172 * 'net rpc group' entrypoint.
3173 * @param argc Standard main() style argc.
3174 * @param argv Standard main() style argv. Initial components are already
3178 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
3180 NET_API_STATUS status
;
3182 struct functable func
[] = {
3187 N_("Create specified group"),
3188 N_("net rpc group add\n"
3189 " Create specified group")
3195 N_("Delete specified group"),
3196 N_("net rpc group delete\n"
3197 " Delete specified group")
3203 N_("Add member to group"),
3204 N_("net rpc group addmem\n"
3205 " Add member to group")
3211 N_("Remove member from group"),
3212 N_("net rpc group delmem\n"
3213 " Remove member from group")
3220 N_("net rpc group list\n"
3227 N_("List group members"),
3228 N_("net rpc group members\n"
3229 " List group members")
3236 N_("net rpc group rename\n"
3239 {NULL
, NULL
, 0, NULL
, NULL
}
3242 status
= libnetapi_net_init(&c
->netapi_ctx
);
3246 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
3247 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
3248 if (c
->opt_kerberos
) {
3249 libnetapi_set_use_kerberos(c
->netapi_ctx
);
3253 if (c
->display_usage
) {
3254 d_printf(_("Usage:\n"));
3255 d_printf(_("net rpc group\n"
3256 " Alias for net rpc group list global "
3257 "local builtin\n"));
3258 net_display_usage_from_functable(func
);
3262 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
3263 rpc_group_list_internals
,
3267 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
3270 /****************************************************************************/
3272 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
3274 return net_share_usage(c
, argc
, argv
);
3278 * Add a share on a remote RPC server.
3280 * @param argc Standard main() style argc.
3281 * @param argv Standard main() style argv. Initial components are already
3284 * @return A shell status integer (0 for success).
3287 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
3289 NET_API_STATUS status
;
3292 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3293 uint32 num_users
=0, perms
=0;
3294 char *password
=NULL
; /* don't allow a share password */
3295 struct SHARE_INFO_2 i2
;
3296 uint32_t parm_error
= 0;
3298 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
3299 return rpc_share_usage(c
, argc
, argv
);
3302 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
3306 path
= strchr(sharename
, '=');
3313 i2
.shi2_netname
= sharename
;
3314 i2
.shi2_type
= type
;
3315 i2
.shi2_remark
= c
->opt_comment
;
3316 i2
.shi2_permissions
= perms
;
3317 i2
.shi2_max_uses
= c
->opt_maxusers
;
3318 i2
.shi2_current_uses
= num_users
;
3319 i2
.shi2_path
= path
;
3320 i2
.shi2_passwd
= password
;
3322 status
= NetShareAdd(c
->opt_host
,
3327 printf(_("NetShareAdd failed with: %s\n"),
3328 libnetapi_get_error_string(c
->netapi_ctx
, status
));
3335 * Delete a share on a remote RPC server.
3337 * @param domain_sid The domain sid acquired from the remote server.
3338 * @param argc Standard main() style argc.
3339 * @param argv Standard main() style argv. Initial components are already
3342 * @return A shell status integer (0 for success).
3344 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3346 if (argc
< 1 || c
->display_usage
) {
3347 return rpc_share_usage(c
, argc
, argv
);
3350 return NetShareDel(c
->opt_host
, argv
[0], 0);
3354 * Formatted print of share info
3356 * @param r pointer to SHARE_INFO_1 to format
3359 static void display_share_info_1(struct net_context
*c
,
3360 struct SHARE_INFO_1
*r
)
3362 if (c
->opt_long_list_entries
) {
3363 d_printf("%-12s %-8.8s %-50s\n",
3365 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
3368 d_printf("%s\n", r
->shi1_netname
);
3372 static WERROR
get_share_info(struct net_context
*c
,
3373 struct rpc_pipe_client
*pipe_hnd
,
3374 TALLOC_CTX
*mem_ctx
,
3378 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3382 union srvsvc_NetShareInfo info
;
3383 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
3385 /* no specific share requested, enumerate all */
3388 uint32_t preferred_len
= 0xffffffff;
3389 uint32_t total_entries
= 0;
3390 uint32_t resume_handle
= 0;
3392 info_ctr
->level
= level
;
3394 status
= dcerpc_srvsvc_NetShareEnumAll(b
, mem_ctx
,
3401 if (!NT_STATUS_IS_OK(status
)) {
3402 return ntstatus_to_werror(status
);
3407 /* request just one share */
3408 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
3415 if (!NT_STATUS_IS_OK(status
)) {
3416 result
= ntstatus_to_werror(status
);
3420 if (!W_ERROR_IS_OK(result
)) {
3425 ZERO_STRUCTP(info_ctr
);
3427 info_ctr
->level
= level
;
3432 struct srvsvc_NetShareCtr1
*ctr1
;
3434 ctr1
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr1
);
3435 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3438 ctr1
->array
= info
.info1
;
3440 info_ctr
->ctr
.ctr1
= ctr1
;
3446 struct srvsvc_NetShareCtr2
*ctr2
;
3448 ctr2
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr2
);
3449 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3452 ctr2
->array
= info
.info2
;
3454 info_ctr
->ctr
.ctr2
= ctr2
;
3460 struct srvsvc_NetShareCtr502
*ctr502
;
3462 ctr502
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr502
);
3463 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3466 ctr502
->array
= info
.info502
;
3468 info_ctr
->ctr
.ctr502
= ctr502
;
3478 * 'net rpc share list' entrypoint.
3479 * @param argc Standard main() style argc.
3480 * @param argv Standard main() style argv. Initial components are already
3483 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3485 NET_API_STATUS status
;
3486 struct SHARE_INFO_1
*i1
= NULL
;
3487 uint32_t entries_read
= 0;
3488 uint32_t total_entries
= 0;
3489 uint32_t resume_handle
= 0;
3490 uint32_t i
, level
= 1;
3492 if (c
->display_usage
) {
3494 "net rpc share list\n"
3497 _("List shares on remote server"));
3501 status
= NetShareEnum(c
->opt_host
,
3503 (uint8_t **)(void *)&i1
,
3512 /* Display results */
3514 if (c
->opt_long_list_entries
) {
3516 "\nEnumerating shared resources (exports) on remote server:\n\n"
3517 "\nShare name Type Description\n"
3518 "---------- ---- -----------\n"));
3520 for (i
= 0; i
< entries_read
; i
++)
3521 display_share_info_1(c
, &i1
[i
]);
3526 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3530 status
= cli_tcon_andx(cli
, netname
, "A:", "", 0);
3531 if (!NT_STATUS_IS_OK(status
)) {
3532 d_printf(_("skipping [%s]: not a file share.\n"), netname
);
3536 status
= cli_tdis(cli
);
3537 if (!NT_STATUS_IS_OK(status
)) {
3538 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status
));
3545 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3546 const char *netname
, uint32 type
)
3548 /* only support disk shares */
3549 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3550 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname
,
3555 /* skip builtin shares */
3556 /* FIXME: should print$ be added too ? */
3557 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3558 strequal(netname
,"global"))
3561 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3562 printf(_("excluding [%s]\n"), netname
);
3566 return check_share_availability(cli
, netname
);
3570 * Migrate shares from a remote RPC server to the local RPC server.
3572 * All parameters are provided by the run_rpc_command function, except for
3573 * argc, argv which are passed through.
3575 * @param domain_sid The domain sid acquired from the remote server.
3576 * @param cli A cli_state connected to the server.
3577 * @param mem_ctx Talloc context, destroyed on completion of the function.
3578 * @param argc Standard main() style argc.
3579 * @param argv Standard main() style argv. Initial components are already
3582 * @return Normal NTSTATUS return.
3585 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3586 const struct dom_sid
*domain_sid
,
3587 const char *domain_name
,
3588 struct cli_state
*cli
,
3589 struct rpc_pipe_client
*pipe_hnd
,
3590 TALLOC_CTX
*mem_ctx
,
3595 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3596 struct srvsvc_NetShareInfoCtr ctr_src
;
3598 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3599 struct cli_state
*cli_dst
= NULL
;
3600 uint32 level
= 502; /* includes secdesc */
3601 uint32_t parm_error
= 0;
3602 struct dcerpc_binding_handle
*b
;
3604 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3606 if (!W_ERROR_IS_OK(result
))
3609 /* connect destination PI_SRVSVC */
3610 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3611 &ndr_table_srvsvc
.syntax_id
);
3612 if (!NT_STATUS_IS_OK(nt_status
))
3615 b
= srvsvc_pipe
->binding_handle
;
3617 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3619 union srvsvc_NetShareInfo info
;
3620 struct srvsvc_NetShareInfo502 info502
=
3621 ctr_src
.ctr
.ctr502
->array
[i
];
3623 /* reset error-code */
3624 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3626 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3629 /* finally add the share on the dst server */
3631 printf(_("migrating: [%s], path: %s, comment: %s, without "
3633 info502
.name
, info502
.path
, info502
.comment
);
3635 info
.info502
= &info502
;
3637 nt_status
= dcerpc_srvsvc_NetShareAdd(b
, mem_ctx
,
3638 srvsvc_pipe
->desthost
,
3643 if (!NT_STATUS_IS_OK(nt_status
)) {
3644 printf(_("cannot add share: %s\n"),
3645 nt_errstr(nt_status
));
3648 if (W_ERROR_V(result
) == W_ERROR_V(WERR_FILE_EXISTS
)) {
3649 printf(_(" [%s] does already exist\n"),
3654 if (!W_ERROR_IS_OK(result
)) {
3655 nt_status
= werror_to_ntstatus(result
);
3656 printf(_("cannot add share: %s\n"),
3657 win_errstr(result
));
3663 nt_status
= NT_STATUS_OK
;
3667 cli_shutdown(cli_dst
);
3675 * Migrate shares from a RPC server to another.
3677 * @param argc Standard main() style argc.
3678 * @param argv Standard main() style argv. Initial components are already
3681 * @return A shell status integer (0 for success).
3683 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3686 if (c
->display_usage
) {
3688 "net rpc share migrate shares\n"
3691 _("Migrate shares to local server"));
3696 printf(_("no server to migrate\n"));
3700 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3701 rpc_share_migrate_shares_internals
,
3708 * @param f file_info
3709 * @param mask current search mask
3710 * @param state arg-pointer
3713 static NTSTATUS
copy_fn(const char *mnt
, struct file_info
*f
,
3714 const char *mask
, void *state
)
3716 static NTSTATUS nt_status
;
3717 static struct copy_clistate
*local_state
;
3718 static fstring filename
, new_mask
;
3721 struct net_context
*c
;
3723 local_state
= (struct copy_clistate
*)state
;
3724 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3728 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3729 return NT_STATUS_OK
;
3731 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3734 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
3736 DEBUG(3,("got dir: %s\n", f
->name
));
3738 fstrcpy(dir
, local_state
->cwd
);
3740 fstrcat(dir
, f
->name
);
3742 switch (net_mode_share
)
3744 case NET_MODE_SHARE_MIGRATE
:
3745 /* create that directory */
3746 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3747 local_state
->cli_share_src
,
3748 local_state
->cli_share_dst
,
3750 c
->opt_acls
? true : false,
3751 c
->opt_attrs
? true : false,
3752 c
->opt_timestamps
? true:false,
3756 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3757 return NT_STATUS_INTERNAL_ERROR
;
3760 if (!NT_STATUS_IS_OK(nt_status
)) {
3761 printf(_("could not handle dir %s: %s\n"),
3762 dir
, nt_errstr(nt_status
));
3766 /* search below that directory */
3767 fstrcpy(new_mask
, dir
);
3768 fstrcat(new_mask
, "\\*");
3770 old_dir
= local_state
->cwd
;
3771 local_state
->cwd
= dir
;
3772 nt_status
= sync_files(local_state
, new_mask
);
3773 if (!NT_STATUS_IS_OK(nt_status
)) {
3774 printf(_("could not handle files\n"));
3776 local_state
->cwd
= old_dir
;
3783 fstrcpy(filename
, local_state
->cwd
);
3784 fstrcat(filename
, "\\");
3785 fstrcat(filename
, f
->name
);
3787 DEBUG(3,("got file: %s\n", filename
));
3789 switch (net_mode_share
)
3791 case NET_MODE_SHARE_MIGRATE
:
3792 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3793 local_state
->cli_share_src
,
3794 local_state
->cli_share_dst
,
3796 c
->opt_acls
? true : false,
3797 c
->opt_attrs
? true : false,
3798 c
->opt_timestamps
? true: false,
3802 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3804 return NT_STATUS_INTERNAL_ERROR
;
3807 if (!NT_STATUS_IS_OK(nt_status
))
3808 printf(_("could not handle file %s: %s\n"),
3809 filename
, nt_errstr(nt_status
));
3814 * sync files, can be called recursivly to list files
3815 * and then call copy_fn for each file
3817 * @param cp_clistate pointer to the copy_clistate we work with
3818 * @param mask the current search mask
3820 * @return Boolean result
3822 static NTSTATUS
sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3824 struct cli_state
*targetcli
;
3825 char *targetpath
= NULL
;
3828 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3830 if ( !cli_resolve_path(talloc_tos(), "", NULL
, cp_clistate
->cli_share_src
,
3831 mask
, &targetcli
, &targetpath
) ) {
3832 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3834 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3835 return cli_nt_error(cp_clistate
->cli_share_src
);
3838 status
= cli_list(targetcli
, targetpath
, cp_clistate
->attribute
,
3839 copy_fn
, cp_clistate
);
3840 if (!NT_STATUS_IS_OK(status
)) {
3841 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3842 mask
, nt_errstr(status
));
3850 * Set the top level directory permissions before we do any further copies.
3851 * Should set up ACL inheritance.
3854 bool copy_top_level_perms(struct net_context
*c
,
3855 struct copy_clistate
*cp_clistate
,
3856 const char *sharename
)
3858 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3860 switch (net_mode_share
) {
3861 case NET_MODE_SHARE_MIGRATE
:
3862 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3863 nt_status
= net_copy_fileattr(c
,
3864 cp_clistate
->mem_ctx
,
3865 cp_clistate
->cli_share_src
,
3866 cp_clistate
->cli_share_dst
,
3868 c
->opt_acls
? true : false,
3869 c
->opt_attrs
? true : false,
3870 c
->opt_timestamps
? true: false,
3874 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3878 if (!NT_STATUS_IS_OK(nt_status
)) {
3879 printf(_("Could handle directory attributes for top level "
3880 "directory of share %s. Error %s\n"),
3881 sharename
, nt_errstr(nt_status
));
3889 * Sync all files inside a remote share to another share (over smb).
3891 * All parameters are provided by the run_rpc_command function, except for
3892 * argc, argv which are passed through.
3894 * @param domain_sid The domain sid acquired from the remote server.
3895 * @param cli A cli_state connected to the server.
3896 * @param mem_ctx Talloc context, destroyed on completion of the function.
3897 * @param argc Standard main() style argc.
3898 * @param argv Standard main() style argv. Initial components are already
3901 * @return Normal NTSTATUS return.
3904 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3905 const struct dom_sid
*domain_sid
,
3906 const char *domain_name
,
3907 struct cli_state
*cli
,
3908 struct rpc_pipe_client
*pipe_hnd
,
3909 TALLOC_CTX
*mem_ctx
,
3914 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3915 struct srvsvc_NetShareInfoCtr ctr_src
;
3918 struct copy_clistate cp_clistate
;
3919 bool got_src_share
= false;
3920 bool got_dst_share
= false;
3921 const char *mask
= "\\*";
3924 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3926 nt_status
= NT_STATUS_NO_MEMORY
;
3930 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3933 if (!W_ERROR_IS_OK(result
))
3936 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3938 struct srvsvc_NetShareInfo502 info502
=
3939 ctr_src
.ctr
.ctr502
->array
[i
];
3941 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3944 /* one might not want to mirror whole discs :) */
3945 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3946 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3951 switch (net_mode_share
)
3953 case NET_MODE_SHARE_MIGRATE
:
3957 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3961 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3964 c
->opt_acls
? _("including") : _("without"),
3965 c
->opt_attrs
? _("including") : _("without"),
3966 c
->opt_timestamps
? _("(preserving timestamps)") : "");
3968 cp_clistate
.mem_ctx
= mem_ctx
;
3969 cp_clistate
.cli_share_src
= NULL
;
3970 cp_clistate
.cli_share_dst
= NULL
;
3971 cp_clistate
.cwd
= NULL
;
3972 cp_clistate
.attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
;
3975 /* open share source */
3976 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3977 &cli
->dest_ss
, cli
->desthost
,
3978 info502
.name
, "A:");
3979 if (!NT_STATUS_IS_OK(nt_status
))
3982 got_src_share
= true;
3984 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3985 /* open share destination */
3986 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3987 NULL
, dst
, info502
.name
, "A:");
3988 if (!NT_STATUS_IS_OK(nt_status
))
3991 got_dst_share
= true;
3994 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3995 d_fprintf(stderr
, _("Could not handle the top level "
3996 "directory permissions for the "
3997 "share: %s\n"), info502
.name
);
3998 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4002 nt_status
= sync_files(&cp_clistate
, mask
);
4003 if (!NT_STATUS_IS_OK(nt_status
)) {
4004 d_fprintf(stderr
, _("could not handle files for share: "
4005 "%s\n"), info502
.name
);
4010 nt_status
= NT_STATUS_OK
;
4015 cli_shutdown(cp_clistate
.cli_share_src
);
4018 cli_shutdown(cp_clistate
.cli_share_dst
);
4025 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
4027 if (c
->display_usage
) {
4029 "net share migrate files\n"
4032 _("Migrate files to local server"));
4037 d_printf(_("no server to migrate\n"));
4041 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4042 rpc_share_migrate_files_internals
,
4047 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4049 * All parameters are provided by the run_rpc_command function, except for
4050 * argc, argv which are passed through.
4052 * @param domain_sid The domain sid acquired from the remote server.
4053 * @param cli A cli_state connected to the server.
4054 * @param mem_ctx Talloc context, destroyed on completion of the function.
4055 * @param argc Standard main() style argc.
4056 * @param argv Standard main() style argv. Initial components are already
4059 * @return Normal NTSTATUS return.
4062 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
4063 const struct dom_sid
*domain_sid
,
4064 const char *domain_name
,
4065 struct cli_state
*cli
,
4066 struct rpc_pipe_client
*pipe_hnd
,
4067 TALLOC_CTX
*mem_ctx
,
4072 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
4073 struct srvsvc_NetShareInfoCtr ctr_src
;
4074 union srvsvc_NetShareInfo info
;
4076 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
4077 struct cli_state
*cli_dst
= NULL
;
4078 uint32 level
= 502; /* includes secdesc */
4079 uint32_t parm_error
= 0;
4080 struct dcerpc_binding_handle
*b
;
4082 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
4085 if (!W_ERROR_IS_OK(result
))
4088 /* connect destination PI_SRVSVC */
4089 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
4090 &ndr_table_srvsvc
.syntax_id
);
4091 if (!NT_STATUS_IS_OK(nt_status
))
4094 b
= srvsvc_pipe
->binding_handle
;
4096 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
4098 struct srvsvc_NetShareInfo502 info502
=
4099 ctr_src
.ctr
.ctr502
->array
[i
];
4101 /* reset error-code */
4102 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4104 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
4107 printf(_("migrating: [%s], path: %s, comment: %s, including "
4109 info502
.name
, info502
.path
, info502
.comment
);
4112 display_sec_desc(info502
.sd_buf
.sd
);
4114 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4115 info
.info502
= &info502
;
4117 /* finally modify the share on the dst server */
4118 nt_status
= dcerpc_srvsvc_NetShareSetInfo(b
, mem_ctx
,
4119 srvsvc_pipe
->desthost
,
4125 if (!NT_STATUS_IS_OK(nt_status
)) {
4126 printf(_("cannot set share-acl: %s\n"),
4127 nt_errstr(nt_status
));
4130 if (!W_ERROR_IS_OK(result
)) {
4131 nt_status
= werror_to_ntstatus(result
);
4132 printf(_("cannot set share-acl: %s\n"),
4133 win_errstr(result
));
4139 nt_status
= NT_STATUS_OK
;
4143 cli_shutdown(cli_dst
);
4151 * Migrate share-acls from a RPC server to another.
4153 * @param argc Standard main() style argc.
4154 * @param argv Standard main() style argv. Initial components are already
4157 * @return A shell status integer (0 for success).
4159 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
4162 if (c
->display_usage
) {
4164 "net rpc share migrate security\n"
4167 _("Migrate share-acls to local server"));
4172 d_printf(_("no server to migrate\n"));
4176 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4177 rpc_share_migrate_security_internals
,
4182 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4183 * from one server to another.
4185 * @param argc Standard main() style argc.
4186 * @param argv Standard main() style argv. Initial components are already
4189 * @return A shell status integer (0 for success).
4192 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
4197 if (c
->display_usage
) {
4199 "net rpc share migrate all\n"
4202 _("Migrates shares including all share settings"));
4207 d_printf(_("no server to migrate\n"));
4211 /* order is important. we don't want to be locked out by the share-acl
4212 * before copying files - gd */
4214 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4215 rpc_share_migrate_shares_internals
, argc
, argv
);
4219 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4220 rpc_share_migrate_files_internals
, argc
, argv
);
4224 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4225 rpc_share_migrate_security_internals
, argc
,
4231 * 'net rpc share migrate' entrypoint.
4232 * @param argc Standard main() style argc.
4233 * @param argv Standard main() style argv. Initial components are already
4236 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4239 struct functable func
[] = {
4242 rpc_share_migrate_all
,
4244 N_("Migrate shares from remote to local server"),
4245 N_("net rpc share migrate all\n"
4246 " Migrate shares from remote to local server")
4250 rpc_share_migrate_files
,
4252 N_("Migrate files from remote to local server"),
4253 N_("net rpc share migrate files\n"
4254 " Migrate files from remote to local server")
4258 rpc_share_migrate_security
,
4260 N_("Migrate share-ACLs from remote to local server"),
4261 N_("net rpc share migrate security\n"
4262 " Migrate share-ACLs from remote to local server")
4266 rpc_share_migrate_shares
,
4268 N_("Migrate shares from remote to local server"),
4269 N_("net rpc share migrate shares\n"
4270 " Migrate shares from remote to local server")
4272 {NULL
, NULL
, 0, NULL
, NULL
}
4275 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4277 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
4283 struct dom_sid
*members
;
4286 static int num_server_aliases
;
4287 static struct full_alias
*server_aliases
;
4290 * Add an alias to the static list.
4292 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4294 if (server_aliases
== NULL
)
4295 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4297 server_aliases
[num_server_aliases
] = *alias
;
4298 num_server_aliases
+= 1;
4302 * For a specific domain on the server, fetch all the aliases
4303 * and their members. Add all of them to the server_aliases.
4306 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4307 TALLOC_CTX
*mem_ctx
,
4308 struct policy_handle
*connect_pol
,
4309 const struct dom_sid
*domain_sid
)
4311 uint32 start_idx
, max_entries
, num_entries
, i
;
4312 struct samr_SamArray
*groups
= NULL
;
4313 NTSTATUS result
, status
;
4314 struct policy_handle domain_pol
;
4315 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4317 /* Get domain policy handle */
4319 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
4321 MAXIMUM_ALLOWED_ACCESS
,
4322 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
4325 if (!NT_STATUS_IS_OK(status
)) {
4328 if (!NT_STATUS_IS_OK(result
)) {
4336 status
= dcerpc_samr_EnumDomainAliases(b
, mem_ctx
,
4343 if (!NT_STATUS_IS_OK(status
)) {
4346 for (i
= 0; i
< num_entries
; i
++) {
4348 struct policy_handle alias_pol
;
4349 struct full_alias alias
;
4350 struct lsa_SidArray sid_array
;
4354 status
= dcerpc_samr_OpenAlias(b
, mem_ctx
,
4356 MAXIMUM_ALLOWED_ACCESS
,
4357 groups
->entries
[i
].idx
,
4360 if (!NT_STATUS_IS_OK(status
)) {
4363 if (!NT_STATUS_IS_OK(_result
)) {
4368 status
= dcerpc_samr_GetMembersInAlias(b
, mem_ctx
,
4372 if (!NT_STATUS_IS_OK(status
)) {
4375 if (!NT_STATUS_IS_OK(_result
)) {
4380 alias
.num_members
= sid_array
.num_sids
;
4382 status
= dcerpc_samr_Close(b
, mem_ctx
, &alias_pol
, &_result
);
4383 if (!NT_STATUS_IS_OK(status
)) {
4386 if (!NT_STATUS_IS_OK(_result
)) {
4391 alias
.members
= NULL
;
4393 if (alias
.num_members
> 0) {
4394 alias
.members
= SMB_MALLOC_ARRAY(struct dom_sid
, alias
.num_members
);
4396 for (j
= 0; j
< alias
.num_members
; j
++)
4397 sid_copy(&alias
.members
[j
],
4398 sid_array
.sids
[j
].sid
);
4401 sid_compose(&alias
.sid
, domain_sid
,
4402 groups
->entries
[i
].idx
);
4404 push_alias(mem_ctx
, &alias
);
4406 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4408 status
= NT_STATUS_OK
;
4411 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
4417 * Dump server_aliases as names for debugging purposes.
4420 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4421 const struct dom_sid
*domain_sid
,
4422 const char *domain_name
,
4423 struct cli_state
*cli
,
4424 struct rpc_pipe_client
*pipe_hnd
,
4425 TALLOC_CTX
*mem_ctx
,
4431 struct policy_handle lsa_pol
;
4432 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4434 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4435 SEC_FLAG_MAXIMUM_ALLOWED
,
4437 if (!NT_STATUS_IS_OK(result
))
4440 for (i
=0; i
<num_server_aliases
; i
++) {
4443 enum lsa_SidType
*types
;
4446 struct full_alias
*alias
= &server_aliases
[i
];
4448 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4450 &domains
, &names
, &types
);
4451 if (!NT_STATUS_IS_OK(result
))
4454 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4456 if (alias
->num_members
== 0) {
4461 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4464 &domains
, &names
, &types
);
4466 if (!NT_STATUS_IS_OK(result
) &&
4467 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4470 for (j
=0; j
<alias
->num_members
; j
++)
4471 DEBUG(1, ("%s\\%s (%d); ",
4472 domains
[j
] ? domains
[j
] : "*unknown*",
4473 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4477 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
4479 return NT_STATUS_OK
;
4483 * Fetch a list of all server aliases and their members into
4487 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4488 const struct dom_sid
*domain_sid
,
4489 const char *domain_name
,
4490 struct cli_state
*cli
,
4491 struct rpc_pipe_client
*pipe_hnd
,
4492 TALLOC_CTX
*mem_ctx
,
4496 NTSTATUS result
, status
;
4497 struct policy_handle connect_pol
;
4498 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4500 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
4502 MAXIMUM_ALLOWED_ACCESS
,
4505 if (!NT_STATUS_IS_OK(status
)) {
4508 if (!NT_STATUS_IS_OK(result
)) {
4513 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4514 &global_sid_Builtin
);
4515 if (!NT_STATUS_IS_OK(status
)) {
4519 status
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4522 dcerpc_samr_Close(b
, mem_ctx
, &connect_pol
, &result
);
4527 static void init_user_token(struct security_token
*token
, struct dom_sid
*user_sid
)
4529 token
->num_sids
= 4;
4531 if (!(token
->sids
= SMB_MALLOC_ARRAY(struct dom_sid
, 4))) {
4532 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4533 token
->num_sids
= 0;
4537 token
->sids
[0] = *user_sid
;
4538 sid_copy(&token
->sids
[1], &global_sid_World
);
4539 sid_copy(&token
->sids
[2], &global_sid_Network
);
4540 sid_copy(&token
->sids
[3], &global_sid_Authenticated_Users
);
4543 static void free_user_token(struct security_token
*token
)
4545 SAFE_FREE(token
->sids
);
4548 static void add_sid_to_token(struct security_token
*token
, struct dom_sid
*sid
)
4550 if (security_token_has_sid(token
, sid
))
4553 token
->sids
= SMB_REALLOC_ARRAY(token
->sids
, struct dom_sid
, token
->num_sids
+1);
4558 sid_copy(&token
->sids
[token
->num_sids
], sid
);
4560 token
->num_sids
+= 1;
4565 struct security_token token
;
4568 static void dump_user_token(struct user_token
*token
)
4572 d_printf("%s\n", token
->name
);
4574 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4575 d_printf(" %s\n", sid_string_tos(&token
->token
.sids
[i
]));
4579 static bool is_alias_member(struct dom_sid
*sid
, struct full_alias
*alias
)
4583 for (i
=0; i
<alias
->num_members
; i
++) {
4584 if (dom_sid_compare(sid
, &alias
->members
[i
]) == 0)
4591 static void collect_sid_memberships(struct security_token
*token
, struct dom_sid sid
)
4595 for (i
=0; i
<num_server_aliases
; i
++) {
4596 if (is_alias_member(&sid
, &server_aliases
[i
]))
4597 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4602 * We got a user token with all the SIDs we can know about without asking the
4603 * server directly. These are the user and domain group sids. All of these can
4604 * be members of aliases. So scan the list of aliases for each of the SIDs and
4605 * add them to the token.
4608 static void collect_alias_memberships(struct security_token
*token
)
4610 int num_global_sids
= token
->num_sids
;
4613 for (i
=0; i
<num_global_sids
; i
++) {
4614 collect_sid_memberships(token
, token
->sids
[i
]);
4618 static bool get_user_sids(const char *domain
, const char *user
, struct security_token
*token
)
4620 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4621 enum wbcSidType type
;
4623 struct wbcDomainSid wsid
;
4624 char sid_str
[WBC_SID_STRING_BUFLEN
];
4625 struct dom_sid user_sid
;
4626 uint32_t num_groups
;
4627 gid_t
*groups
= NULL
;
4630 fstr_sprintf(full_name
, "%s%c%s",
4631 domain
, *lp_winbind_separator(), user
);
4633 /* First let's find out the user sid */
4635 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4637 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4638 DEBUG(1, ("winbind could not find %s: %s\n",
4639 full_name
, wbcErrorString(wbc_status
)));
4643 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4645 if (type
!= WBC_SID_NAME_USER
) {
4646 DEBUG(1, ("%s is not a user\n", full_name
));
4650 if (!string_to_sid(&user_sid
, sid_str
)) {
4651 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4655 init_user_token(token
, &user_sid
);
4657 /* And now the groups winbind knows about */
4659 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4660 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4661 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4662 full_name
, wbcErrorString(wbc_status
)));
4666 for (i
= 0; i
< num_groups
; i
++) {
4667 gid_t gid
= groups
[i
];
4670 wbc_status
= wbcGidToSid(gid
, &wsid
);
4671 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4672 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4673 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4674 wbcFreeMemory(groups
);
4678 wbcSidToStringBuf(&wsid
, sid_str
, sizeof(sid_str
));
4680 DEBUG(3, (" %s\n", sid_str
));
4682 string_to_sid(&sid
, sid_str
);
4683 add_sid_to_token(token
, &sid
);
4685 wbcFreeMemory(groups
);
4691 * Get a list of all user tokens we want to look at
4694 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4695 struct user_token
**user_tokens
)
4697 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4698 uint32_t i
, num_users
;
4700 struct user_token
*result
;
4701 TALLOC_CTX
*frame
= NULL
;
4703 if (lp_winbind_use_default_domain() &&
4704 (c
->opt_target_workgroup
== NULL
)) {
4705 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4706 "please specify a workgroup\n"));
4710 /* Send request to winbind daemon */
4712 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4713 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4714 DEBUG(1, (_("winbind could not list users: %s\n"),
4715 wbcErrorString(wbc_status
)));
4719 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4721 if (result
== NULL
) {
4722 DEBUG(1, ("Could not malloc sid array\n"));
4723 wbcFreeMemory(users
);
4727 frame
= talloc_stackframe();
4728 for (i
=0; i
< num_users
; i
++) {
4729 fstring domain
, user
;
4732 fstrcpy(result
[i
].name
, users
[i
]);
4734 p
= strchr(users
[i
], *lp_winbind_separator());
4736 DEBUG(3, ("%s\n", users
[i
]));
4739 fstrcpy(domain
, c
->opt_target_workgroup
);
4740 fstrcpy(user
, users
[i
]);
4743 fstrcpy(domain
, users
[i
]);
4748 get_user_sids(domain
, user
, &(result
[i
].token
));
4751 wbcFreeMemory(users
);
4753 *num_tokens
= num_users
;
4754 *user_tokens
= result
;
4759 static bool get_user_tokens_from_file(FILE *f
,
4761 struct user_token
**tokens
)
4763 struct user_token
*token
= NULL
;
4768 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4772 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4773 line
[strlen(line
)-1] = '\0';
4776 if (line
[0] == ' ') {
4780 if(!string_to_sid(&sid
, &line
[1])) {
4781 DEBUG(1,("get_user_tokens_from_file: Could "
4782 "not convert sid %s \n",&line
[1]));
4786 if (token
== NULL
) {
4787 DEBUG(0, ("File does not begin with username"));
4791 add_sid_to_token(&token
->token
, &sid
);
4795 /* And a new user... */
4798 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4799 if (*tokens
== NULL
) {
4800 DEBUG(0, ("Could not realloc tokens\n"));
4804 token
= &((*tokens
)[*num_tokens
-1]);
4806 fstrcpy(token
->name
, line
);
4807 token
->token
.num_sids
= 0;
4808 token
->token
.sids
= NULL
;
4817 * Show the list of all users that have access to a share
4820 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4821 TALLOC_CTX
*mem_ctx
,
4822 const char *netname
,
4824 struct user_token
*tokens
)
4827 struct security_descriptor
*share_sd
= NULL
;
4828 struct security_descriptor
*root_sd
= NULL
;
4829 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4831 union srvsvc_NetShareInfo info
;
4835 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
4837 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
4844 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4845 DEBUG(1, ("Coult not query secdesc for share %s\n",
4850 share_sd
= info
.info502
->sd_buf
.sd
;
4851 if (share_sd
== NULL
) {
4852 DEBUG(1, ("Got no secdesc for share %s\n",
4858 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli
, netname
, "A:", "", 0))) {
4862 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4863 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4864 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4867 for (i
=0; i
<num_tokens
; i
++) {
4870 if (share_sd
!= NULL
) {
4871 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4874 if (!NT_STATUS_IS_OK(status
)) {
4875 DEBUG(1, ("Could not check share_sd for "
4882 if (root_sd
== NULL
) {
4883 d_printf(" %s\n", tokens
[i
].name
);
4887 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4889 if (!NT_STATUS_IS_OK(status
)) {
4890 DEBUG(1, ("Could not check root_sd for user %s\n",
4894 d_printf(" %s\n", tokens
[i
].name
);
4897 if (fnum
!= (uint16_t)-1)
4898 cli_close(cli
, fnum
);
4910 static void collect_share(const char *name
, uint32 m
,
4911 const char *comment
, void *state
)
4913 struct share_list
*share_list
= (struct share_list
*)state
;
4915 if (m
!= STYPE_DISKTREE
)
4918 share_list
->num_shares
+= 1;
4919 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4920 if (!share_list
->shares
) {
4921 share_list
->num_shares
= 0;
4924 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4928 * List shares on a remote RPC server, including the security descriptors.
4930 * All parameters are provided by the run_rpc_command function, except for
4931 * argc, argv which are passed through.
4933 * @param domain_sid The domain sid acquired from the remote server.
4934 * @param cli A cli_state connected to the server.
4935 * @param mem_ctx Talloc context, destroyed on completion of the function.
4936 * @param argc Standard main() style argc.
4937 * @param argv Standard main() style argv. Initial components are already
4940 * @return Normal NTSTATUS return.
4943 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4944 const struct dom_sid
*domain_sid
,
4945 const char *domain_name
,
4946 struct cli_state
*cli
,
4947 struct rpc_pipe_client
*pipe_hnd
,
4948 TALLOC_CTX
*mem_ctx
,
4957 struct user_token
*tokens
= NULL
;
4960 struct share_list share_list
;
4965 f
= fopen(argv
[0], "r");
4969 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4970 return NT_STATUS_UNSUCCESSFUL
;
4973 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4979 DEBUG(0, ("Could not read users from file\n"));
4980 return NT_STATUS_UNSUCCESSFUL
;
4983 for (i
=0; i
<num_tokens
; i
++)
4984 collect_alias_memberships(&tokens
[i
].token
);
4986 share_list
.num_shares
= 0;
4987 share_list
.shares
= NULL
;
4989 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4992 DEBUG(0, ("Error returning browse list: %s\n",
4997 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4998 char *netname
= share_list
.shares
[i
];
5000 if (netname
[strlen(netname
)-1] == '$')
5003 d_printf("%s\n", netname
);
5005 show_userlist(pipe_hnd
, mem_ctx
, netname
,
5006 num_tokens
, tokens
);
5009 for (i
=0; i
<num_tokens
; i
++) {
5010 free_user_token(&tokens
[i
].token
);
5013 SAFE_FREE(share_list
.shares
);
5015 return NT_STATUS_OK
;
5018 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
5023 if (c
->display_usage
) {
5025 "net rpc share allowedusers\n"
5028 _("List allowed users"));
5032 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5033 rpc_aliaslist_internals
,
5038 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
.syntax_id
, 0,
5044 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
5045 rpc_share_allowedusers_internals
,
5049 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
5052 struct user_token
*tokens
= NULL
;
5056 net_usersidlist_usage(c
, argc
, argv
);
5060 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
5061 DEBUG(0, ("Could not get the user/sid list\n"));
5065 for (i
=0; i
<num_tokens
; i
++) {
5066 dump_user_token(&tokens
[i
]);
5067 free_user_token(&tokens
[i
].token
);
5074 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
5076 d_printf(_("net usersidlist\n"
5077 "\tprints out a list of all users the running winbind knows\n"
5078 "\tabout, together with all their SIDs. This is used as\n"
5079 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5081 net_common_flags_usage(c
, argc
, argv
);
5086 * 'net rpc share' entrypoint.
5087 * @param argc Standard main() style argc.
5088 * @param argv Standard main() style argv. Initial components are already
5092 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
5094 NET_API_STATUS status
;
5096 struct functable func
[] = {
5102 N_("net rpc share add\n"
5110 N_("net rpc share delete\n"
5115 rpc_share_allowedusers
,
5117 N_("Modify allowed users"),
5118 N_("net rpc share allowedusers\n"
5119 " Modify allowed users")
5125 N_("Migrate share to local server"),
5126 N_("net rpc share migrate\n"
5127 " Migrate share to local server")
5134 N_("net rpc share list\n"
5137 {NULL
, NULL
, 0, NULL
, NULL
}
5140 status
= libnetapi_net_init(&c
->netapi_ctx
);
5144 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5145 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5146 if (c
->opt_kerberos
) {
5147 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5151 if (c
->display_usage
) {
5156 " Alias for net rpc share list\n"));
5157 net_display_usage_from_functable(func
);
5161 return rpc_share_list(c
, argc
, argv
);
5164 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5167 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5168 TALLOC_CTX
*mem_ctx
,
5169 struct rpc_sh_ctx
*ctx
,
5170 struct rpc_pipe_client
*pipe_hnd
,
5171 int argc
, const char **argv
)
5174 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
5177 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
5178 TALLOC_CTX
*mem_ctx
,
5179 struct rpc_sh_ctx
*ctx
,
5180 struct rpc_pipe_client
*pipe_hnd
,
5181 int argc
, const char **argv
)
5183 NET_API_STATUS status
;
5184 uint32_t parm_err
= 0;
5185 struct SHARE_INFO_2 i2
;
5187 if ((argc
< 2) || (argc
> 3)) {
5188 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
5190 return NT_STATUS_INVALID_PARAMETER
;
5193 i2
.shi2_netname
= argv
[0];
5194 i2
.shi2_type
= STYPE_DISKTREE
;
5195 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
5196 i2
.shi2_permissions
= 0;
5197 i2
.shi2_max_uses
= 0;
5198 i2
.shi2_current_uses
= 0;
5199 i2
.shi2_path
= argv
[1];
5200 i2
.shi2_passwd
= NULL
;
5202 status
= NetShareAdd(pipe_hnd
->desthost
,
5207 return werror_to_ntstatus(W_ERROR(status
));
5210 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5211 TALLOC_CTX
*mem_ctx
,
5212 struct rpc_sh_ctx
*ctx
,
5213 struct rpc_pipe_client
*pipe_hnd
,
5214 int argc
, const char **argv
)
5217 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5218 return NT_STATUS_INVALID_PARAMETER
;
5221 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
5224 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5225 TALLOC_CTX
*mem_ctx
,
5226 struct rpc_sh_ctx
*ctx
,
5227 struct rpc_pipe_client
*pipe_hnd
,
5228 int argc
, const char **argv
)
5230 union srvsvc_NetShareInfo info
;
5233 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5236 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
5237 return NT_STATUS_INVALID_PARAMETER
;
5240 status
= dcerpc_srvsvc_NetShareGetInfo(b
, mem_ctx
,
5246 if (!NT_STATUS_IS_OK(status
)) {
5247 result
= ntstatus_to_werror(status
);
5250 if (!W_ERROR_IS_OK(result
)) {
5254 d_printf(_("Name: %s\n"), info
.info2
->name
);
5255 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
5256 d_printf(_("Path: %s\n"), info
.info2
->path
);
5257 d_printf(_("Password: %s\n"), info
.info2
->password
);
5260 return werror_to_ntstatus(result
);
5263 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5264 struct rpc_sh_ctx
*ctx
)
5266 static struct rpc_sh_cmd cmds
[] = {
5268 { "list", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_list
,
5269 N_("List available shares") },
5271 { "add", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_add
,
5272 N_("Add a share") },
5274 { "delete", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_delete
,
5275 N_("Delete a share") },
5277 { "info", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_info
,
5278 N_("Get information about a share") },
5280 { NULL
, NULL
, 0, NULL
, NULL
}
5286 /****************************************************************************/
5288 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5290 return net_file_usage(c
, argc
, argv
);
5294 * Close a file on a remote RPC server.
5296 * @param argc Standard main() style argc.
5297 * @param argv Standard main() style argv. Initial components are already
5300 * @return A shell status integer (0 for success).
5302 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5304 if (argc
< 1 || c
->display_usage
) {
5305 return rpc_file_usage(c
, argc
, argv
);
5308 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
5312 * Formatted print of open file info
5314 * @param r struct FILE_INFO_3 contents
5317 static void display_file_info_3(struct FILE_INFO_3
*r
)
5319 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5320 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
5321 r
->fi3_num_locks
, r
->fi3_pathname
);
5325 * List files for a user on a remote RPC server.
5327 * @param argc Standard main() style argc.
5328 * @param argv Standard main() style argv. Initial components are already
5331 * @return A shell status integer (0 for success)..
5334 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5336 NET_API_STATUS status
;
5337 uint32 preferred_len
= 0xffffffff, i
;
5338 const char *username
=NULL
;
5339 uint32_t total_entries
= 0;
5340 uint32_t entries_read
= 0;
5341 uint32_t resume_handle
= 0;
5342 struct FILE_INFO_3
*i3
= NULL
;
5344 if (c
->display_usage
) {
5345 return rpc_file_usage(c
, argc
, argv
);
5348 /* if argc > 0, must be user command */
5350 username
= smb_xstrdup(argv
[0]);
5353 status
= NetFileEnum(c
->opt_host
,
5357 (uint8_t **)(void *)&i3
,
5367 /* Display results */
5370 "\nEnumerating open files on remote server:\n\n"
5371 "\nFileId Opened by Perms Locks Path"
5372 "\n------ --------- ----- ----- ---- \n"));
5373 for (i
= 0; i
< entries_read
; i
++) {
5374 display_file_info_3(&i3
[i
]);
5381 * 'net rpc file' entrypoint.
5382 * @param argc Standard main() style argc.
5383 * @param argv Standard main() style argv. Initial components are already
5387 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5389 NET_API_STATUS status
;
5391 struct functable func
[] = {
5396 N_("Close opened file"),
5397 N_("net rpc file close\n"
5398 " Close opened file")
5404 N_("List files opened by user"),
5405 N_("net rpc file user\n"
5406 " List files opened by user")
5413 N_("Display information about opened file"),
5414 N_("net rpc file info\n"
5415 " Display information about opened file")
5418 {NULL
, NULL
, 0, NULL
, NULL
}
5421 status
= libnetapi_net_init(&c
->netapi_ctx
);
5425 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5426 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5427 if (c
->opt_kerberos
) {
5428 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5432 if (c
->display_usage
) {
5433 d_printf(_("Usage:\n"));
5434 d_printf(_("net rpc file\n"
5435 " List opened files\n"));
5436 net_display_usage_from_functable(func
);
5440 return rpc_file_user(c
, argc
, argv
);
5443 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5447 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5449 * All parameters are provided by the run_rpc_command function, except for
5450 * argc, argv which are passed through.
5452 * @param c A net_context structure.
5453 * @param domain_sid The domain sid acquired from the remote server.
5454 * @param cli A cli_state connected to the server.
5455 * @param mem_ctx Talloc context, destroyed on completion of the function.
5456 * @param argc Standard main() style argc.
5457 * @param argv Standard main() style argv. Initial components are already
5460 * @return Normal NTSTATUS return.
5463 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5464 const struct dom_sid
*domain_sid
,
5465 const char *domain_name
,
5466 struct cli_state
*cli
,
5467 struct rpc_pipe_client
*pipe_hnd
,
5468 TALLOC_CTX
*mem_ctx
,
5472 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5474 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5476 status
= dcerpc_initshutdown_Abort(b
, mem_ctx
, NULL
, &result
);
5477 if (!NT_STATUS_IS_OK(status
)) {
5480 if (W_ERROR_IS_OK(result
)) {
5481 d_printf(_("\nShutdown successfully aborted\n"));
5482 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5484 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5486 return werror_to_ntstatus(result
);
5490 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5492 * All parameters are provided by the run_rpc_command function, except for
5493 * argc, argv which are passed through.
5495 * @param c A net_context structure.
5496 * @param domain_sid The domain sid acquired from the remote server.
5497 * @param cli A cli_state connected to the server.
5498 * @param mem_ctx Talloc context, destroyed on completion of the function.
5499 * @param argc Standard main() style argc.
5500 * @param argv Standard main() style argv. Initial components are already
5503 * @return Normal NTSTATUS return.
5506 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5507 const struct dom_sid
*domain_sid
,
5508 const char *domain_name
,
5509 struct cli_state
*cli
,
5510 struct rpc_pipe_client
*pipe_hnd
,
5511 TALLOC_CTX
*mem_ctx
,
5515 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5517 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5519 result
= dcerpc_winreg_AbortSystemShutdown(b
, mem_ctx
, NULL
, &werr
);
5521 if (!NT_STATUS_IS_OK(result
)) {
5522 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5525 if (W_ERROR_IS_OK(werr
)) {
5526 d_printf(_("\nShutdown successfully aborted\n"));
5527 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5529 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5531 return werror_to_ntstatus(werr
);
5535 * ABORT the shutdown of a remote RPC server.
5537 * @param argc Standard main() style argc.
5538 * @param argv Standard main() style argv. Initial components are already
5541 * @return A shell status integer (0 for success).
5544 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5549 if (c
->display_usage
) {
5551 "net rpc abortshutdown\n"
5554 _("Abort a scheduled shutdown"));
5558 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5559 rpc_shutdown_abort_internals
, argc
, argv
);
5564 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5566 return run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5567 rpc_reg_shutdown_abort_internals
,
5572 * Shut down a remote RPC Server via initshutdown pipe.
5574 * All parameters are provided by the run_rpc_command function, except for
5575 * argc, argv which are passed through.
5577 * @param c A net_context structure.
5578 * @param domain_sid The domain sid acquired from the remote server.
5579 * @param cli A cli_state connected to the server.
5580 * @param mem_ctx Talloc context, destroyed on completion of the function.
5581 * @param argc Standard main() style argc.
5582 * @param argv Standard main() style argv. Initial components are already
5585 * @return Normal NTSTATUS return.
5588 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5589 const struct dom_sid
*domain_sid
,
5590 const char *domain_name
,
5591 struct cli_state
*cli
,
5592 struct rpc_pipe_client
*pipe_hnd
,
5593 TALLOC_CTX
*mem_ctx
,
5597 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5599 const char *msg
= N_("This machine will be shutdown shortly");
5600 uint32 timeout
= 20;
5601 struct lsa_StringLarge msg_string
;
5602 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5604 if (c
->opt_comment
) {
5605 msg
= c
->opt_comment
;
5607 if (c
->opt_timeout
) {
5608 timeout
= c
->opt_timeout
;
5611 msg_string
.string
= msg
;
5613 /* create an entry */
5614 status
= dcerpc_initshutdown_Init(b
, mem_ctx
, NULL
,
5615 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5617 if (!NT_STATUS_IS_OK(status
)) {
5620 if (W_ERROR_IS_OK(result
)) {
5621 d_printf(_("\nShutdown of remote machine succeeded\n"));
5622 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5624 DEBUG(1,("Shutdown of remote machine failed!\n"));
5626 return werror_to_ntstatus(result
);
5630 * Shut down a remote RPC Server via winreg pipe.
5632 * All parameters are provided by the run_rpc_command function, except for
5633 * argc, argv which are passed through.
5635 * @param c A net_context structure.
5636 * @param domain_sid The domain sid acquired from the remote server.
5637 * @param cli A cli_state connected to the server.
5638 * @param mem_ctx Talloc context, destroyed on completion of the function.
5639 * @param argc Standard main() style argc.
5640 * @param argv Standard main() style argv. Initial components are already
5643 * @return Normal NTSTATUS return.
5646 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5647 const struct dom_sid
*domain_sid
,
5648 const char *domain_name
,
5649 struct cli_state
*cli
,
5650 struct rpc_pipe_client
*pipe_hnd
,
5651 TALLOC_CTX
*mem_ctx
,
5655 const char *msg
= N_("This machine will be shutdown shortly");
5656 uint32 timeout
= 20;
5657 struct lsa_StringLarge msg_string
;
5660 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5662 if (c
->opt_comment
) {
5663 msg
= c
->opt_comment
;
5665 msg_string
.string
= msg
;
5667 if (c
->opt_timeout
) {
5668 timeout
= c
->opt_timeout
;
5671 /* create an entry */
5672 result
= dcerpc_winreg_InitiateSystemShutdown(b
, mem_ctx
, NULL
,
5673 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5675 if (!NT_STATUS_IS_OK(result
)) {
5676 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5680 if (W_ERROR_IS_OK(werr
)) {
5681 d_printf(_("\nShutdown of remote machine succeeded\n"));
5683 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5684 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5685 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5687 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5690 return werror_to_ntstatus(werr
);
5694 * Shut down a remote RPC server.
5696 * @param argc Standard main() style argc.
5697 * @param argv Standard main() style argv. Initial components are already
5700 * @return A shell status integer (0 for success).
5703 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5707 if (c
->display_usage
) {
5709 "net rpc shutdown\n"
5712 _("Shut down a remote RPC server"));
5716 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5717 rpc_init_shutdown_internals
, argc
, argv
);
5720 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5721 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5722 rpc_reg_shutdown_internals
, argc
, argv
);
5728 /***************************************************************************
5729 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5730 ***************************************************************************/
5733 * Add interdomain trust account to the RPC server.
5734 * All parameters (except for argc and argv) are passed by run_rpc_command
5737 * @param c A net_context structure.
5738 * @param domain_sid The domain sid acquired from the server.
5739 * @param cli A cli_state connected to the server.
5740 * @param mem_ctx Talloc context, destroyed on completion of the function.
5741 * @param argc Standard main() style argc.
5742 * @param argv Standard main() style argv. Initial components are already
5745 * @return normal NTSTATUS return code.
5748 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5749 const struct dom_sid
*domain_sid
,
5750 const char *domain_name
,
5751 struct cli_state
*cli
,
5752 struct rpc_pipe_client
*pipe_hnd
,
5753 TALLOC_CTX
*mem_ctx
,
5757 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5758 NTSTATUS status
, result
;
5760 struct lsa_String lsa_acct_name
;
5762 uint32 acct_flags
=0;
5764 uint32_t access_granted
= 0;
5765 union samr_UserInfo info
;
5766 unsigned int orig_timeout
;
5767 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5772 _(" net rpc trustdom add <domain_name> "
5773 "<trust password>\n"));
5774 return NT_STATUS_INVALID_PARAMETER
;
5778 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5781 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5782 return NT_STATUS_NO_MEMORY
;
5785 strupper_m(acct_name
);
5787 init_lsa_String(&lsa_acct_name
, acct_name
);
5789 /* Get samr policy handle */
5790 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5792 MAXIMUM_ALLOWED_ACCESS
,
5795 if (!NT_STATUS_IS_OK(status
)) {
5798 if (!NT_STATUS_IS_OK(result
)) {
5803 /* Get domain policy handle */
5804 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5806 MAXIMUM_ALLOWED_ACCESS
,
5807 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5810 if (!NT_STATUS_IS_OK(status
)) {
5813 if (!NT_STATUS_IS_OK(result
)) {
5818 /* This call can take a long time - allow the server to time out.
5819 * 35 seconds should do it. */
5821 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5823 /* Create trusting domain's account */
5824 acb_info
= ACB_NORMAL
;
5825 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5826 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5827 SAMR_USER_ACCESS_SET_PASSWORD
|
5828 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5829 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5831 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
5840 if (!NT_STATUS_IS_OK(status
)) {
5843 /* And restore our original timeout. */
5844 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5846 if (!NT_STATUS_IS_OK(result
)) {
5848 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5849 acct_name
, nt_errstr(result
));
5854 struct samr_CryptPassword crypt_pwd
;
5856 ZERO_STRUCT(info
.info23
);
5858 init_samr_CryptPassword(argv
[1],
5859 &cli
->user_session_key
,
5862 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5863 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5864 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5865 info
.info23
.password
= crypt_pwd
;
5867 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
5872 if (!NT_STATUS_IS_OK(status
)) {
5876 if (!NT_STATUS_IS_OK(result
)) {
5878 DEBUG(0,("Could not set trust account password: %s\n",
5879 nt_errstr(result
)));
5885 SAFE_FREE(acct_name
);
5890 * Create interdomain trust account for a remote domain.
5892 * @param argc Standard argc.
5893 * @param argv Standard argv without initial components.
5895 * @return Integer status (0 means success).
5898 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5900 if (argc
> 0 && !c
->display_usage
) {
5901 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5902 rpc_trustdom_add_internals
, argc
, argv
);
5906 _("net rpc trustdom add <domain_name> <trust "
5914 * Remove interdomain trust account from the RPC server.
5915 * All parameters (except for argc and argv) are passed by run_rpc_command
5918 * @param c A net_context structure.
5919 * @param domain_sid The domain sid acquired from the server.
5920 * @param cli A cli_state connected to the server.
5921 * @param mem_ctx Talloc context, destroyed on completion of the function.
5922 * @param argc Standard main() style argc.
5923 * @param argv Standard main() style argv. Initial components are already
5926 * @return normal NTSTATUS return code.
5929 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5930 const struct dom_sid
*domain_sid
,
5931 const char *domain_name
,
5932 struct cli_state
*cli
,
5933 struct rpc_pipe_client
*pipe_hnd
,
5934 TALLOC_CTX
*mem_ctx
,
5938 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5939 NTSTATUS status
, result
;
5941 struct dom_sid trust_acct_sid
;
5942 struct samr_Ids user_rids
, name_types
;
5943 struct lsa_String lsa_acct_name
;
5944 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
5949 _(" net rpc trustdom del <domain_name>\n"));
5950 return NT_STATUS_INVALID_PARAMETER
;
5954 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5956 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5958 if (acct_name
== NULL
)
5959 return NT_STATUS_NO_MEMORY
;
5961 strupper_m(acct_name
);
5963 /* Get samr policy handle */
5964 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
5966 MAXIMUM_ALLOWED_ACCESS
,
5969 if (!NT_STATUS_IS_OK(status
)) {
5972 if (!NT_STATUS_IS_OK(result
)) {
5977 /* Get domain policy handle */
5978 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
5980 MAXIMUM_ALLOWED_ACCESS
,
5981 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5984 if (!NT_STATUS_IS_OK(status
)) {
5987 if (!NT_STATUS_IS_OK(result
)) {
5992 init_lsa_String(&lsa_acct_name
, acct_name
);
5994 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
6001 if (!NT_STATUS_IS_OK(status
)) {
6002 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6004 acct_name
, nt_errstr(status
));
6007 if (!NT_STATUS_IS_OK(result
)) {
6009 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6011 acct_name
, nt_errstr(result
) );
6015 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
6017 MAXIMUM_ALLOWED_ACCESS
,
6021 if (!NT_STATUS_IS_OK(status
)) {
6022 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6024 acct_name
, nt_errstr(status
) );
6028 if (!NT_STATUS_IS_OK(result
)) {
6030 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6032 acct_name
, nt_errstr(result
) );
6036 /* append the rid to the domain sid */
6037 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
6041 /* remove the sid */
6043 status
= dcerpc_samr_RemoveMemberFromForeignDomain(b
, mem_ctx
,
6047 if (!NT_STATUS_IS_OK(status
)) {
6048 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6049 " on user %s failed %s\n"),
6050 acct_name
, nt_errstr(status
));
6053 if (!NT_STATUS_IS_OK(result
)) {
6055 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6056 " on user %s failed %s\n"),
6057 acct_name
, nt_errstr(result
) );
6064 status
= dcerpc_samr_DeleteUser(b
, mem_ctx
,
6067 if (!NT_STATUS_IS_OK(status
)) {
6068 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6070 acct_name
, nt_errstr(status
));
6074 if (!NT_STATUS_IS_OK(result
)) {
6076 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6078 acct_name
, nt_errstr(result
) );
6082 if (!NT_STATUS_IS_OK(result
)) {
6083 d_printf(_("Could not set trust account password: %s\n"),
6093 * Delete interdomain trust account for a remote domain.
6095 * @param argc Standard argc.
6096 * @param argv Standard argv without initial components.
6098 * @return Integer status (0 means success).
6101 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
6103 if (argc
> 0 && !c
->display_usage
) {
6104 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
6105 rpc_trustdom_del_internals
, argc
, argv
);
6109 _("net rpc trustdom del <domain>\n"));
6114 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
6115 struct cli_state
*cli
,
6116 TALLOC_CTX
*mem_ctx
,
6117 const char *domain_name
)
6119 char *dc_name
= NULL
;
6120 const char *buffer
= NULL
;
6121 struct rpc_pipe_client
*netr
;
6124 struct dcerpc_binding_handle
*b
;
6126 /* Use NetServerEnum2 */
6128 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
6130 return NT_STATUS_OK
;
6133 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6134 for domain %s\n", domain_name
));
6136 /* Try netr_GetDcName */
6138 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
6140 if (!NT_STATUS_IS_OK(status
)) {
6144 b
= netr
->binding_handle
;
6146 status
= dcerpc_netr_GetDcName(b
, mem_ctx
,
6153 if (NT_STATUS_IS_OK(status
) && W_ERROR_IS_OK(result
)) {
6157 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6158 for domain %s\n", domain_name
));
6160 if (!NT_STATUS_IS_OK(status
)) {
6164 return werror_to_ntstatus(result
);
6168 * Establish trust relationship to a trusting domain.
6169 * Interdomain account must already be created on remote PDC.
6171 * @param c A net_context structure.
6172 * @param argc Standard argc.
6173 * @param argv Standard argv without initial components.
6175 * @return Integer status (0 means success).
6178 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6181 struct cli_state
*cli
= NULL
;
6182 struct sockaddr_storage server_ss
;
6183 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6184 struct policy_handle connect_hnd
;
6185 TALLOC_CTX
*mem_ctx
;
6186 NTSTATUS nt_status
, result
;
6187 struct dom_sid
*domain_sid
;
6192 union lsa_PolicyInformation
*info
= NULL
;
6193 struct dcerpc_binding_handle
*b
;
6196 * Connect to \\server\ipc$ as 'our domain' account with password
6199 if (argc
!= 1 || c
->display_usage
) {
6202 _("net rpc trustdom establish <domain_name>\n"));
6206 domain_name
= smb_xstrdup(argv
[0]);
6207 strupper_m(domain_name
);
6209 /* account name used at first is our domain's name with '$' */
6210 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
6213 strupper_m(acct_name
);
6216 * opt_workgroup will be used by connection functions further,
6217 * hence it should be set to remote domain name instead of ours
6219 if (c
->opt_workgroup
) {
6220 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6223 c
->opt_user_name
= acct_name
;
6225 /* find the domain controller */
6226 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6227 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6231 /* connect to ipc$ as username/password */
6232 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6233 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6235 /* Is it trusting domain account for sure ? */
6236 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6237 nt_errstr(nt_status
)));
6241 /* store who we connected to */
6243 saf_store( domain_name
, pdc_name
);
6246 * Connect to \\server\ipc$ again (this time anonymously)
6249 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6252 if (NT_STATUS_IS_ERR(nt_status
)) {
6253 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6254 domain_name
, nt_errstr(nt_status
)));
6258 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6259 "domain %s", domain_name
))) {
6260 DEBUG(0, ("talloc_init() failed\n"));
6265 /* Make sure we're talking to a proper server */
6267 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6268 if (!NT_STATUS_IS_OK(nt_status
)) {
6270 talloc_destroy(mem_ctx
);
6275 * Call LsaOpenPolicy and LsaQueryInfo
6278 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6280 if (!NT_STATUS_IS_OK(nt_status
)) {
6281 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6283 talloc_destroy(mem_ctx
);
6287 b
= pipe_hnd
->binding_handle
;
6289 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
6291 if (NT_STATUS_IS_ERR(nt_status
)) {
6292 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6293 nt_errstr(nt_status
)));
6295 talloc_destroy(mem_ctx
);
6299 /* Querying info level 5 */
6301 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6303 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6306 if (NT_STATUS_IS_ERR(nt_status
)) {
6307 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6308 nt_errstr(nt_status
)));
6310 talloc_destroy(mem_ctx
);
6313 if (NT_STATUS_IS_ERR(result
)) {
6314 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6315 nt_errstr(result
)));
6317 talloc_destroy(mem_ctx
);
6321 domain_sid
= info
->account_domain
.sid
;
6323 /* There should be actually query info level 3 (following nt serv behaviour),
6324 but I still don't know if it's _really_ necessary */
6327 * Store the password in secrets db
6330 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6331 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6333 talloc_destroy(mem_ctx
);
6338 * Close the pipes and clean up
6341 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6342 if (NT_STATUS_IS_ERR(nt_status
)) {
6343 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6344 nt_errstr(nt_status
)));
6346 talloc_destroy(mem_ctx
);
6352 talloc_destroy(mem_ctx
);
6354 d_printf(_("Trust to domain %s established\n"), domain_name
);
6359 * Revoke trust relationship to the remote domain.
6361 * @param c A net_context structure.
6362 * @param argc Standard argc.
6363 * @param argv Standard argv without initial components.
6365 * @return Integer status (0 means success).
6368 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6374 if (argc
< 1 || c
->display_usage
) {
6377 _("net rpc trustdom revoke <domain_name>\n"
6378 " Revoke trust relationship\n"
6379 " domain_name\tName of domain to revoke trust\n"));
6383 /* generate upper cased domain name */
6384 domain_name
= smb_xstrdup(argv
[0]);
6385 strupper_m(domain_name
);
6387 /* delete password of the trust */
6388 if (!pdb_del_trusteddom_pw(domain_name
)) {
6389 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6396 SAFE_FREE(domain_name
);
6400 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6401 const struct dom_sid
*domain_sid
,
6402 const char *domain_name
,
6403 struct cli_state
*cli
,
6404 struct rpc_pipe_client
*pipe_hnd
,
6405 TALLOC_CTX
*mem_ctx
,
6410 if (!sid_to_fstring(str_sid
, domain_sid
)) {
6411 return NT_STATUS_UNSUCCESSFUL
;
6413 d_printf("%s\n", str_sid
);
6414 return NT_STATUS_OK
;
6417 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
6421 /* convert sid into ascii string */
6422 sid_to_fstring(ascii_sid
, dom_sid
);
6424 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
6427 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6428 TALLOC_CTX
*mem_ctx
,
6429 struct policy_handle
*pol
,
6430 struct dom_sid dom_sid
,
6431 const char *trusted_dom_name
)
6433 NTSTATUS nt_status
, result
;
6434 union lsa_TrustedDomainInfo
*info
= NULL
;
6435 char *cleartextpwd
= NULL
;
6436 uint8_t session_key
[16];
6437 DATA_BLOB session_key_blob
;
6438 DATA_BLOB data
= data_blob_null
;
6439 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
6441 nt_status
= dcerpc_lsa_QueryTrustedDomainInfoBySid(b
, mem_ctx
,
6444 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6447 if (NT_STATUS_IS_ERR(nt_status
)) {
6448 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6449 nt_errstr(nt_status
)));
6452 if (NT_STATUS_IS_ERR(result
)) {
6454 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6455 nt_errstr(result
)));
6459 data
= data_blob(info
->password
.password
->data
,
6460 info
->password
.password
->length
);
6462 if (!rpccli_get_pwd_hash(pipe_hnd
, session_key
)) {
6463 DEBUG(0, ("Could not retrieve password hash\n"));
6467 session_key_blob
= data_blob_const(session_key
, sizeof(session_key
));
6468 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key_blob
);
6470 if (cleartextpwd
== NULL
) {
6471 DEBUG(0,("retrieved NULL password\n"));
6472 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6476 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6477 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6478 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6482 #ifdef DEBUG_PASSWORD
6483 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6484 "password: [%s]\n", trusted_dom_name
,
6485 sid_string_dbg(&dom_sid
), cleartextpwd
));
6489 SAFE_FREE(cleartextpwd
);
6490 data_blob_free(&data
);
6495 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6498 /* common variables */
6499 TALLOC_CTX
* mem_ctx
;
6500 struct cli_state
*cli
= NULL
;
6501 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6502 NTSTATUS nt_status
, result
;
6503 const char *domain_name
= NULL
;
6504 struct dom_sid
*queried_dom_sid
;
6505 struct policy_handle connect_hnd
;
6506 union lsa_PolicyInformation
*info
= NULL
;
6508 /* trusted domains listing variables */
6509 unsigned int enum_ctx
= 0;
6511 struct lsa_DomainList dom_list
;
6513 struct dcerpc_binding_handle
*b
;
6515 if (c
->display_usage
) {
6517 "net rpc trustdom vampire\n"
6520 _("Vampire trust relationship from remote server"));
6525 * Listing trusted domains (stored in secrets.tdb, if local)
6528 mem_ctx
= talloc_init("trust relationships vampire");
6531 * set domain and pdc name to local samba server (default)
6532 * or to remote one given in command line
6535 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6536 domain_name
= c
->opt_workgroup
;
6537 c
->opt_target_workgroup
= c
->opt_workgroup
;
6539 fstrcpy(pdc_name
, global_myname());
6540 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6541 c
->opt_target_workgroup
= domain_name
;
6544 /* open \PIPE\lsarpc and open policy handle */
6545 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6546 if (!NT_STATUS_IS_OK(nt_status
)) {
6547 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6548 nt_errstr(nt_status
)));
6549 talloc_destroy(mem_ctx
);
6553 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6555 if (!NT_STATUS_IS_OK(nt_status
)) {
6556 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6557 nt_errstr(nt_status
) ));
6559 talloc_destroy(mem_ctx
);
6563 b
= pipe_hnd
->binding_handle
;
6565 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6567 if (NT_STATUS_IS_ERR(nt_status
)) {
6568 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6569 nt_errstr(nt_status
)));
6571 talloc_destroy(mem_ctx
);
6575 /* query info level 5 to obtain sid of a domain being queried */
6576 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6578 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6582 if (NT_STATUS_IS_ERR(nt_status
)) {
6583 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6584 nt_errstr(nt_status
)));
6586 talloc_destroy(mem_ctx
);
6589 if (NT_STATUS_IS_ERR(result
)) {
6590 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6591 nt_errstr(result
)));
6593 talloc_destroy(mem_ctx
);
6597 queried_dom_sid
= info
->account_domain
.sid
;
6600 * Keep calling LsaEnumTrustdom over opened pipe until
6601 * the end of enumeration is reached
6604 d_printf(_("Vampire trusted domains:\n\n"));
6607 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6613 if (NT_STATUS_IS_ERR(nt_status
)) {
6614 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6615 nt_errstr(nt_status
)));
6617 talloc_destroy(mem_ctx
);
6620 if (NT_STATUS_IS_ERR(result
)) {
6622 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6623 nt_errstr(result
)));
6625 talloc_destroy(mem_ctx
);
6630 for (i
= 0; i
< dom_list
.count
; i
++) {
6632 print_trusted_domain(dom_list
.domains
[i
].sid
,
6633 dom_list
.domains
[i
].name
.string
);
6635 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6636 *dom_list
.domains
[i
].sid
,
6637 dom_list
.domains
[i
].name
.string
);
6638 if (!NT_STATUS_IS_OK(nt_status
)) {
6640 talloc_destroy(mem_ctx
);
6646 * in case of no trusted domains say something rather
6647 * than just display blank line
6649 if (!dom_list
.count
) d_printf(_("none\n"));
6651 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6653 /* close this connection before doing next one */
6654 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6655 if (NT_STATUS_IS_ERR(nt_status
)) {
6656 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6657 nt_errstr(nt_status
)));
6659 talloc_destroy(mem_ctx
);
6663 /* close lsarpc pipe and connection to IPC$ */
6666 talloc_destroy(mem_ctx
);
6670 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6672 /* common variables */
6673 TALLOC_CTX
* mem_ctx
;
6674 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6675 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6676 NTSTATUS nt_status
, result
;
6677 const char *domain_name
= NULL
;
6678 struct dom_sid
*queried_dom_sid
;
6679 int ascii_dom_name_len
;
6680 struct policy_handle connect_hnd
;
6681 union lsa_PolicyInformation
*info
= NULL
;
6682 struct dcerpc_binding_handle
*b
= NULL
;
6684 /* trusted domains listing variables */
6685 unsigned int num_domains
, enum_ctx
= 0;
6687 struct lsa_DomainList dom_list
;
6691 /* trusting domains listing variables */
6692 struct policy_handle domain_hnd
;
6693 struct samr_SamArray
*trusts
= NULL
;
6695 if (c
->display_usage
) {
6697 "net rpc trustdom list\n"
6700 _("List incoming and outgoing trust relationships"));
6705 * Listing trusted domains (stored in secrets.tdb, if local)
6708 mem_ctx
= talloc_init("trust relationships listing");
6711 * set domain and pdc name to local samba server (default)
6712 * or to remote one given in command line
6715 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6716 domain_name
= c
->opt_workgroup
;
6717 c
->opt_target_workgroup
= c
->opt_workgroup
;
6719 fstrcpy(pdc_name
, global_myname());
6720 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6721 c
->opt_target_workgroup
= domain_name
;
6724 /* open \PIPE\lsarpc and open policy handle */
6725 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6726 if (!NT_STATUS_IS_OK(nt_status
)) {
6727 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6728 nt_errstr(nt_status
)));
6729 talloc_destroy(mem_ctx
);
6733 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6735 if (!NT_STATUS_IS_OK(nt_status
)) {
6736 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6737 nt_errstr(nt_status
) ));
6739 talloc_destroy(mem_ctx
);
6743 b
= pipe_hnd
->binding_handle
;
6745 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6747 if (NT_STATUS_IS_ERR(nt_status
)) {
6748 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6749 nt_errstr(nt_status
)));
6751 talloc_destroy(mem_ctx
);
6755 /* query info level 5 to obtain sid of a domain being queried */
6756 nt_status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
6758 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6762 if (NT_STATUS_IS_ERR(nt_status
)) {
6763 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6764 nt_errstr(nt_status
)));
6766 talloc_destroy(mem_ctx
);
6769 if (NT_STATUS_IS_ERR(result
)) {
6770 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6771 nt_errstr(result
)));
6773 talloc_destroy(mem_ctx
);
6777 queried_dom_sid
= info
->account_domain
.sid
;
6780 * Keep calling LsaEnumTrustdom over opened pipe until
6781 * the end of enumeration is reached
6784 d_printf(_("Trusted domains list:\n\n"));
6786 found_domain
= false;
6789 nt_status
= dcerpc_lsa_EnumTrustDom(b
, mem_ctx
,
6795 if (NT_STATUS_IS_ERR(nt_status
)) {
6796 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6797 nt_errstr(nt_status
)));
6799 talloc_destroy(mem_ctx
);
6802 if (NT_STATUS_IS_ERR(result
)) {
6803 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6804 nt_errstr(result
)));
6806 talloc_destroy(mem_ctx
);
6811 for (i
= 0; i
< dom_list
.count
; i
++) {
6812 print_trusted_domain(dom_list
.domains
[i
].sid
,
6813 dom_list
.domains
[i
].name
.string
);
6814 found_domain
= true;
6818 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6821 * in case of no trusted domains say something rather
6822 * than just display blank line
6824 if (!found_domain
) {
6825 d_printf(_("none\n"));
6828 /* close this connection before doing next one */
6829 nt_status
= dcerpc_lsa_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6830 if (NT_STATUS_IS_ERR(nt_status
)) {
6831 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6832 nt_errstr(nt_status
)));
6834 talloc_destroy(mem_ctx
);
6838 TALLOC_FREE(pipe_hnd
);
6841 * Listing trusting domains (stored in passdb backend, if local)
6844 d_printf(_("\nTrusting domains list:\n\n"));
6847 * Open \PIPE\samr and get needed policy handles
6849 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6851 if (!NT_STATUS_IS_OK(nt_status
)) {
6852 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6854 talloc_destroy(mem_ctx
);
6858 b
= pipe_hnd
->binding_handle
;
6861 nt_status
= dcerpc_samr_Connect2(b
, mem_ctx
,
6863 SAMR_ACCESS_LOOKUP_DOMAIN
,
6866 if (!NT_STATUS_IS_OK(nt_status
)) {
6867 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6868 nt_errstr(nt_status
)));
6870 talloc_destroy(mem_ctx
);
6873 if (!NT_STATUS_IS_OK(result
)) {
6875 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6876 nt_errstr(result
)));
6878 talloc_destroy(mem_ctx
);
6882 /* SamrOpenDomain - we have to open domain policy handle in order to be
6883 able to enumerate accounts*/
6884 nt_status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
6886 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6890 if (!NT_STATUS_IS_OK(nt_status
)) {
6891 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6892 nt_errstr(nt_status
)));
6894 talloc_destroy(mem_ctx
);
6897 if (!NT_STATUS_IS_OK(result
)) {
6899 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6900 nt_errstr(result
)));
6902 talloc_destroy(mem_ctx
);
6907 * perform actual enumeration
6910 found_domain
= false;
6912 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6915 nt_status
= dcerpc_samr_EnumDomainUsers(b
, mem_ctx
,
6923 if (NT_STATUS_IS_ERR(nt_status
)) {
6924 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6925 nt_errstr(nt_status
)));
6927 talloc_destroy(mem_ctx
);
6930 if (NT_STATUS_IS_ERR(result
)) {
6932 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6933 nt_errstr(result
)));
6935 talloc_destroy(mem_ctx
);
6939 for (i
= 0; i
< num_domains
; i
++) {
6941 char *str
= CONST_DISCARD(char *, trusts
->entries
[i
].name
.string
);
6943 found_domain
= true;
6946 * get each single domain's sid (do we _really_ need this ?):
6947 * 1) connect to domain's pdc
6948 * 2) query the pdc for domain's sid
6951 /* get rid of '$' tail */
6952 ascii_dom_name_len
= strlen(str
);
6953 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6954 str
[ascii_dom_name_len
- 1] = '\0';
6956 /* set opt_* variables to remote domain */
6958 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6959 c
->opt_target_workgroup
= c
->opt_workgroup
;
6961 d_printf("%-20s", str
);
6963 /* connect to remote domain controller */
6964 nt_status
= net_make_ipc_connection(c
,
6965 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6967 if (NT_STATUS_IS_OK(nt_status
)) {
6968 /* query for domain's sid */
6969 if (run_rpc_command(
6971 &ndr_table_lsarpc
.syntax_id
, 0,
6972 rpc_query_domain_sid
, argc
,
6974 d_printf(_("strange - couldn't get domain's sid\n"));
6976 cli_shutdown(remote_cli
);
6979 d_fprintf(stderr
, _("domain controller is not "
6980 "responding: %s\n"),
6981 nt_errstr(nt_status
));
6982 d_printf(_("couldn't get domain's sid\n"));
6986 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
6988 if (!found_domain
) {
6992 /* close opened samr and domain policy handles */
6993 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &domain_hnd
, &result
);
6994 if (!NT_STATUS_IS_OK(nt_status
)) {
6995 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6998 nt_status
= dcerpc_samr_Close(b
, mem_ctx
, &connect_hnd
, &result
);
6999 if (!NT_STATUS_IS_OK(nt_status
)) {
7000 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
7003 /* close samr pipe and connection to IPC$ */
7006 talloc_destroy(mem_ctx
);
7011 * Entrypoint for 'net rpc trustdom' code.
7013 * @param argc Standard argc.
7014 * @param argv Standard argv without initial components.
7016 * @return Integer status (0 means success).
7019 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
7021 struct functable func
[] = {
7026 N_("Add trusting domain's account"),
7027 N_("net rpc trustdom add\n"
7028 " Add trusting domain's account")
7034 N_("Remove trusting domain's account"),
7035 N_("net rpc trustdom del\n"
7036 " Remove trusting domain's account")
7040 rpc_trustdom_establish
,
7042 N_("Establish outgoing trust relationship"),
7043 N_("net rpc trustdom establish\n"
7044 " Establish outgoing trust relationship")
7048 rpc_trustdom_revoke
,
7050 N_("Revoke outgoing trust relationship"),
7051 N_("net rpc trustdom revoke\n"
7052 " Revoke outgoing trust relationship")
7058 N_("List in- and outgoing domain trusts"),
7059 N_("net rpc trustdom list\n"
7060 " List in- and outgoing domain trusts")
7064 rpc_trustdom_vampire
,
7066 N_("Vampire trusts from remote server"),
7067 N_("net rpc trustdom vampire\n"
7068 " Vampire trusts from remote server")
7070 {NULL
, NULL
, 0, NULL
, NULL
}
7073 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
7077 * Check if a server will take rpc commands
7078 * @param flags Type of server to connect to (PDC, DMB, localhost)
7079 * if the host is not explicitly specified
7080 * @return bool (true means rpc supported)
7082 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
7084 struct cli_state
*cli
;
7086 struct sockaddr_storage server_ss
;
7087 char *server_name
= NULL
;
7090 /* flags (i.e. server type) may depend on command */
7091 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
7094 if ((cli
= cli_initialise()) == NULL
) {
7098 status
= cli_connect(cli
, server_name
, &server_ss
);
7099 if (!NT_STATUS_IS_OK(status
))
7101 if (!attempt_netbios_session_request(&cli
, global_myname(),
7102 server_name
, &server_ss
))
7104 status
= cli_negprot(cli
);
7105 if (!NT_STATUS_IS_OK(status
))
7107 if (cli
->protocol
< PROTOCOL_NT1
)
7116 /* dump sam database via samsync rpc calls */
7117 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
7118 if (c
->display_usage
) {
7123 _("Dump remote SAM database"));
7127 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
7128 NET_FLAGS_ANONYMOUS
,
7129 rpc_samdump_internals
, argc
, argv
);
7132 /* syncronise sam database via samsync rpc calls */
7133 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
7135 struct functable func
[] = {
7140 N_("Dump remote SAM database to ldif"),
7141 N_("net rpc vampire ldif\n"
7142 " Dump remote SAM database to LDIF file or "
7149 N_("Dump remote SAM database to Kerberos Keytab"),
7150 N_("net rpc vampire keytab\n"
7151 " Dump remote SAM database to Kerberos keytab "
7158 N_("Dump remote SAM database to passdb"),
7159 N_("net rpc vampire passdb\n"
7160 " Dump remote SAM database to passdb")
7163 {NULL
, NULL
, 0, NULL
, NULL
}
7167 if (c
->display_usage
) {
7172 _("Vampire remote SAM database"));
7176 return rpc_vampire_passdb(c
, argc
, argv
);
7179 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
7183 * Migrate everything from a print server.
7185 * @param c A net_context structure.
7186 * @param argc Standard main() style argc.
7187 * @param argv Standard main() style argv. Initial components are already
7190 * @return A shell status integer (0 for success).
7192 * The order is important !
7193 * To successfully add drivers the print queues have to exist !
7194 * Applying ACLs should be the last step, because you're easily locked out.
7197 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
7202 if (c
->display_usage
) {
7204 "net rpc printer migrate all\n"
7207 _("Migrate everything from a print server"));
7212 d_printf(_("no server to migrate\n"));
7216 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7217 rpc_printer_migrate_printers_internals
, argc
,
7222 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7223 rpc_printer_migrate_drivers_internals
, argc
,
7228 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7229 rpc_printer_migrate_forms_internals
, argc
, argv
);
7233 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7234 rpc_printer_migrate_settings_internals
, argc
,
7239 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7240 rpc_printer_migrate_security_internals
, argc
,
7246 * Migrate print drivers from a print server.
7248 * @param c A net_context structure.
7249 * @param argc Standard main() style argc.
7250 * @param argv Standard main() style argv. Initial components are already
7253 * @return A shell status integer (0 for success).
7255 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
7258 if (c
->display_usage
) {
7260 "net rpc printer migrate drivers\n"
7263 _("Migrate print-drivers from a print-server"));
7268 d_printf(_("no server to migrate\n"));
7272 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7273 rpc_printer_migrate_drivers_internals
,
7278 * Migrate print-forms from a print-server.
7280 * @param c A net_context structure.
7281 * @param argc Standard main() style argc.
7282 * @param argv Standard main() style argv. Initial components are already
7285 * @return A shell status integer (0 for success).
7287 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7290 if (c
->display_usage
) {
7292 "net rpc printer migrate forms\n"
7295 _("Migrate print-forms from a print-server"));
7300 d_printf(_("no server to migrate\n"));
7304 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7305 rpc_printer_migrate_forms_internals
,
7310 * Migrate printers from a print-server.
7312 * @param c A net_context structure.
7313 * @param argc Standard main() style argc.
7314 * @param argv Standard main() style argv. Initial components are already
7317 * @return A shell status integer (0 for success).
7319 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7322 if (c
->display_usage
) {
7324 "net rpc printer migrate printers\n"
7327 _("Migrate printers from a print-server"));
7332 d_printf(_("no server to migrate\n"));
7336 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7337 rpc_printer_migrate_printers_internals
,
7342 * Migrate printer-ACLs from a print-server
7344 * @param c A net_context structure.
7345 * @param argc Standard main() style argc.
7346 * @param argv Standard main() style argv. Initial components are already
7349 * @return A shell status integer (0 for success).
7351 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7354 if (c
->display_usage
) {
7356 "net rpc printer migrate security\n"
7359 _("Migrate printer-ACLs from a print-server"));
7364 d_printf(_("no server to migrate\n"));
7368 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7369 rpc_printer_migrate_security_internals
,
7374 * Migrate printer-settings from a print-server.
7376 * @param c A net_context structure.
7377 * @param argc Standard main() style argc.
7378 * @param argv Standard main() style argv. Initial components are already
7381 * @return A shell status integer (0 for success).
7383 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7386 if (c
->display_usage
) {
7388 "net rpc printer migrate settings\n"
7391 _("Migrate printer-settings from a "
7397 d_printf(_("no server to migrate\n"));
7401 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7402 rpc_printer_migrate_settings_internals
,
7407 * 'net rpc printer' entrypoint.
7409 * @param c A net_context structure.
7410 * @param argc Standard main() style argc.
7411 * @param argv Standard main() style argv. Initial components are already
7415 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7418 /* ouch: when addriver and setdriver are called from within
7419 rpc_printer_migrate_drivers_internals, the printer-queue already
7422 struct functable func
[] = {
7425 rpc_printer_migrate_all
,
7427 N_("Migrate all from remote to local print server"),
7428 N_("net rpc printer migrate all\n"
7429 " Migrate all from remote to local print server")
7433 rpc_printer_migrate_drivers
,
7435 N_("Migrate drivers to local server"),
7436 N_("net rpc printer migrate drivers\n"
7437 " Migrate drivers to local server")
7441 rpc_printer_migrate_forms
,
7443 N_("Migrate froms to local server"),
7444 N_("net rpc printer migrate forms\n"
7445 " Migrate froms to local server")
7449 rpc_printer_migrate_printers
,
7451 N_("Migrate printers to local server"),
7452 N_("net rpc printer migrate printers\n"
7453 " Migrate printers to local server")
7457 rpc_printer_migrate_security
,
7459 N_("Mirgate printer ACLs to local server"),
7460 N_("net rpc printer migrate security\n"
7461 " Mirgate printer ACLs to local server")
7465 rpc_printer_migrate_settings
,
7467 N_("Migrate printer settings to local server"),
7468 N_("net rpc printer migrate settings\n"
7469 " Migrate printer settings to local server")
7471 {NULL
, NULL
, 0, NULL
, NULL
}
7474 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7479 * List printers on a remote RPC server.
7481 * @param c A net_context structure.
7482 * @param argc Standard main() style argc.
7483 * @param argv Standard main() style argv. Initial components are already
7486 * @return A shell status integer (0 for success).
7488 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7490 if (c
->display_usage
) {
7492 "net rpc printer list\n"
7495 _("List printers on a remote RPC server"));
7499 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7500 rpc_printer_list_internals
,
7505 * List printer-drivers on a remote RPC server.
7507 * @param c A net_context structure.
7508 * @param argc Standard main() style argc.
7509 * @param argv Standard main() style argv. Initial components are already
7512 * @return A shell status integer (0 for success).
7514 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7517 if (c
->display_usage
) {
7519 "net rpc printer driver\n"
7522 _("List printer-drivers on a remote RPC server"));
7526 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7527 rpc_printer_driver_list_internals
,
7532 * Publish printer in ADS via MSRPC.
7534 * @param c A net_context structure.
7535 * @param argc Standard main() style argc.
7536 * @param argv Standard main() style argv. Initial components are already
7539 * @return A shell status integer (0 for success).
7541 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7544 if (c
->display_usage
) {
7546 "net rpc printer publish publish\n"
7549 _("Publish printer in ADS via MSRPC"));
7553 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7554 rpc_printer_publish_publish_internals
,
7559 * Update printer in ADS via MSRPC.
7561 * @param c A net_context structure.
7562 * @param argc Standard main() style argc.
7563 * @param argv Standard main() style argv. Initial components are already
7566 * @return A shell status integer (0 for success).
7568 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7570 if (c
->display_usage
) {
7572 "net rpc printer publish update\n"
7575 _("Update printer in ADS via MSRPC"));
7579 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7580 rpc_printer_publish_update_internals
,
7585 * UnPublish printer in ADS via MSRPC.
7587 * @param c A net_context structure.
7588 * @param argc Standard main() style argc.
7589 * @param argv Standard main() style argv. Initial components are already
7592 * @return A shell status integer (0 for success).
7594 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7597 if (c
->display_usage
) {
7599 "net rpc printer publish unpublish\n"
7602 _("UnPublish printer in ADS via MSRPC"));
7606 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7607 rpc_printer_publish_unpublish_internals
,
7612 * List published printers via MSRPC.
7614 * @param c A net_context structure.
7615 * @param argc Standard main() style argc.
7616 * @param argv Standard main() style argv. Initial components are already
7619 * @return A shell status integer (0 for success).
7621 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7624 if (c
->display_usage
) {
7626 "net rpc printer publish list\n"
7629 _("List published printers via MSRPC"));
7633 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7634 rpc_printer_publish_list_internals
,
7640 * Publish printer in ADS.
7642 * @param c A net_context structure.
7643 * @param argc Standard main() style argc.
7644 * @param argv Standard main() style argv. Initial components are already
7647 * @return A shell status integer (0 for success).
7649 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7653 struct functable func
[] = {
7656 rpc_printer_publish_publish
,
7658 N_("Publish printer in AD"),
7659 N_("net rpc printer publish publish\n"
7660 " Publish printer in AD")
7664 rpc_printer_publish_update
,
7666 N_("Update printer in AD"),
7667 N_("net rpc printer publish update\n"
7668 " Update printer in AD")
7672 rpc_printer_publish_unpublish
,
7674 N_("Unpublish printer"),
7675 N_("net rpc printer publish unpublish\n"
7676 " Unpublish printer")
7680 rpc_printer_publish_list
,
7682 N_("List published printers"),
7683 N_("net rpc printer publish list\n"
7684 " List published printers")
7686 {NULL
, NULL
, 0, NULL
, NULL
}
7690 if (c
->display_usage
) {
7691 d_printf(_("Usage:\n"));
7692 d_printf(_("net rpc printer publish\n"
7693 " List published printers\n"
7694 " Alias of net rpc printer publish "
7696 net_display_usage_from_functable(func
);
7699 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7700 rpc_printer_publish_list_internals
,
7704 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7710 * Display rpc printer help page.
7712 * @param c A net_context structure.
7713 * @param argc Standard main() style argc.
7714 * @param argv Standard main() style argv. Initial components are already
7717 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7719 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7720 "\tlists all printers on print-server\n\n"));
7721 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7722 "\tlists all printer-drivers on print-server\n\n"));
7723 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7724 "\tpublishes printer settings in Active Directory\n"
7725 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7726 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7727 "\n\tmigrates printers from remote to local server\n\n"));
7728 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7729 "\n\tmigrates printer-settings from remote to local server\n\n"));
7730 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7731 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7732 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7733 "\n\tmigrates printer-forms from remote to local server\n\n"));
7734 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7735 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7736 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7737 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7738 "\tremote to local print-server\n\n"));
7739 net_common_methods_usage(c
, argc
, argv
);
7740 net_common_flags_usage(c
, argc
, argv
);
7742 "\t-v or --verbose\t\t\tgive verbose output\n"
7743 "\t --destination\t\tmigration target server (default: localhost)\n"));
7749 * 'net rpc printer' entrypoint.
7751 * @param c A net_context structure.
7752 * @param argc Standard main() style argc.
7753 * @param argv Standard main() style argv. Initial components are already
7756 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7758 struct functable func
[] = {
7763 N_("List all printers on print server"),
7764 N_("net rpc printer list\n"
7765 " List all printers on print server")
7769 rpc_printer_migrate
,
7771 N_("Migrate printer to local server"),
7772 N_("net rpc printer migrate\n"
7773 " Migrate printer to local server")
7777 rpc_printer_driver_list
,
7779 N_("List printer drivers"),
7780 N_("net rpc printer driver\n"
7781 " List printer drivers")
7785 rpc_printer_publish
,
7787 N_("Publish printer in AD"),
7788 N_("net rpc printer publish\n"
7789 " Publish printer in AD")
7791 {NULL
, NULL
, 0, NULL
, NULL
}
7795 if (c
->display_usage
) {
7796 d_printf(_("Usage:\n"));
7797 d_printf(_("net rpc printer\n"
7798 " List printers\n"));
7799 net_display_usage_from_functable(func
);
7802 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7803 rpc_printer_list_internals
,
7807 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7811 * 'net rpc' entrypoint.
7813 * @param c A net_context structure.
7814 * @param argc Standard main() style argc.
7815 * @param argv Standard main() style argv. Initial components are already
7819 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7821 NET_API_STATUS status
;
7823 struct functable func
[] = {
7828 N_("Modify global audit settings"),
7829 N_("net rpc audit\n"
7830 " Modify global audit settings")
7836 N_("Show basic info about a domain"),
7838 " Show basic info about a domain")
7844 N_("Join a domain"),
7852 N_("Join a domain created in server manager"),
7853 N_("net rpc oldjoin\n"
7854 " Join a domain created in server manager")
7860 N_("Test that a join is valid"),
7861 N_("net rpc testjoin\n"
7862 " Test that a join is valid")
7868 N_("List/modify users"),
7870 " List/modify users")
7876 N_("Change a user password"),
7877 N_("net rpc password\n"
7878 " Change a user password\n"
7879 " Alias for net rpc user password")
7885 N_("List/modify groups"),
7886 N_("net rpc group\n"
7887 " List/modify groups")
7893 N_("List/modify shares"),
7894 N_("net rpc share\n"
7895 " List/modify shares")
7901 N_("List open files"),
7909 N_("List/modify printers"),
7910 N_("net rpc printer\n"
7911 " List/modify printers")
7915 net_rpc_changetrustpw
,
7917 N_("Change trust account password"),
7918 N_("net rpc changetrustpw\n"
7919 " Change trust account password")
7925 N_("Modify domain trusts"),
7926 N_("net rpc trustdom\n"
7927 " Modify domain trusts")
7933 N_("Abort a remote shutdown"),
7934 N_("net rpc abortshutdown\n"
7935 " Abort a remote shutdown")
7941 N_("Shutdown a remote server"),
7942 N_("net rpc shutdown\n"
7943 " Shutdown a remote server")
7949 N_("Dump SAM data of remote NT PDC"),
7950 N_("net rpc samdump\n"
7951 " Dump SAM data of remote NT PDC")
7957 N_("Sync a remote NT PDC's data into local passdb"),
7958 N_("net rpc vampire\n"
7959 " Sync a remote NT PDC's data into local passdb")
7965 N_("Fetch the domain sid into local secrets.tdb"),
7966 N_("net rpc getsid\n"
7967 " Fetch the domain sid into local secrets.tdb")
7973 N_("Manage privileges assigned to SID"),
7974 N_("net rpc rights\n"
7975 " Manage privileges assigned to SID")
7981 N_("Start/stop/query remote services"),
7982 N_("net rpc service\n"
7983 " Start/stop/query remote services")
7989 N_("Manage registry hives"),
7990 N_("net rpc registry\n"
7991 " Manage registry hives")
7997 N_("Open interactive shell on remote server"),
7998 N_("net rpc shell\n"
7999 " Open interactive shell on remote server")
8005 N_("Manage trusts"),
8006 N_("net rpc trust\n"
8009 {NULL
, NULL
, 0, NULL
, NULL
}
8012 status
= libnetapi_net_init(&c
->netapi_ctx
);
8016 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
8017 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
8018 if (c
->opt_kerberos
) {
8019 libnetapi_set_use_kerberos(c
->netapi_ctx
);
8021 if (c
->opt_ccache
) {
8022 libnetapi_set_use_ccache(c
->netapi_ctx
);
8025 return net_run_function(c
, argc
, argv
, "net rpc", func
);