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 "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/cli_samr.h"
27 #include "../librpc/gen_ndr/cli_lsa.h"
28 #include "../librpc/gen_ndr/cli_netlogon.h"
29 #include "../librpc/gen_ndr/cli_srvsvc.h"
30 #include "../librpc/gen_ndr/cli_spoolss.h"
31 #include "../librpc/gen_ndr/cli_initshutdown.h"
32 #include "../librpc/gen_ndr/cli_winreg.h"
34 static int net_mode_share
;
35 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
);
40 * @brief RPC based subcommands for the 'net' utility.
42 * This file should contain much of the functionality that used to
43 * be found in rpcclient, execpt that the commands should change
44 * less often, and the fucntionality should be sane (the user is not
45 * expected to know a rid/sid before they conduct an operation etc.)
47 * @todo Perhaps eventually these should be split out into a number
48 * of files, as this could get quite big.
53 * Many of the RPC functions need the domain sid. This function gets
54 * it at the start of every run
56 * @param cli A cli_state already connected to the remote machine
58 * @return The Domain SID of the remote machine.
61 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
63 const char **domain_name
)
65 struct rpc_pipe_client
*lsa_pipe
= NULL
;
66 struct policy_handle pol
;
67 NTSTATUS result
= NT_STATUS_OK
;
68 union lsa_PolicyInformation
*info
= NULL
;
70 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
72 if (!NT_STATUS_IS_OK(result
)) {
73 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
77 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
78 SEC_FLAG_MAXIMUM_ALLOWED
,
80 if (!NT_STATUS_IS_OK(result
)) {
81 d_fprintf(stderr
, "open_policy %s: %s\n",
87 result
= rpccli_lsa_QueryInfoPolicy(lsa_pipe
, mem_ctx
,
89 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
91 if (!NT_STATUS_IS_OK(result
)) {
92 d_fprintf(stderr
, "lsaquery %s: %s\n",
98 *domain_name
= info
->account_domain
.name
.string
;
99 *domain_sid
= info
->account_domain
.sid
;
101 rpccli_lsa_Close(lsa_pipe
, mem_ctx
, &pol
);
102 TALLOC_FREE(lsa_pipe
);
108 * Run a single RPC command, from start to finish.
110 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
111 * @param conn_flag a NET_FLAG_ combination. Passed to
112 * net_make_ipc_connection.
113 * @param argc Standard main() style argc.
114 * @param argv Standard main() style argv. Initial components are already
116 * @return A shell status integer (0 for success).
119 int run_rpc_command(struct net_context
*c
,
120 struct cli_state
*cli_arg
,
121 const struct ndr_syntax_id
*interface
,
127 struct cli_state
*cli
= NULL
;
128 struct rpc_pipe_client
*pipe_hnd
= NULL
;
132 const char *domain_name
;
135 /* make use of cli_state handed over as an argument, if possible */
137 nt_status
= net_make_ipc_connection(c
, conn_flags
, &cli
);
138 if (!NT_STATUS_IS_OK(nt_status
)) {
139 DEBUG(1, ("failed to make ipc connection: %s\n",
140 nt_errstr(nt_status
)));
153 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
154 DEBUG(0, ("talloc_init() failed\n"));
158 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
160 if (!NT_STATUS_IS_OK(nt_status
)) {
164 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
165 if (lp_client_schannel()
166 && (ndr_syntax_id_equal(interface
,
167 &ndr_table_netlogon
.syntax_id
))) {
168 /* Always try and create an schannel netlogon pipe. */
169 nt_status
= cli_rpc_pipe_open_schannel(
170 cli
, interface
, NCACN_NP
,
171 DCERPC_AUTH_LEVEL_PRIVACY
, domain_name
,
173 if (!NT_STATUS_IS_OK(nt_status
)) {
174 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
175 nt_errstr(nt_status
) ));
179 if (conn_flags
& NET_FLAGS_SEAL
) {
180 nt_status
= cli_rpc_pipe_open_ntlmssp(
182 (conn_flags
& NET_FLAGS_TCP
) ?
183 NCACN_IP_TCP
: NCACN_NP
,
184 DCERPC_AUTH_LEVEL_PRIVACY
,
185 lp_workgroup(), c
->opt_user_name
,
186 c
->opt_password
, &pipe_hnd
);
188 nt_status
= cli_rpc_pipe_open_noauth(
192 if (!NT_STATUS_IS_OK(nt_status
)) {
193 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
194 get_pipe_name_from_syntax(
195 talloc_tos(), interface
),
196 nt_errstr(nt_status
) ));
202 nt_status
= fn(c
, domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
204 if (!NT_STATUS_IS_OK(nt_status
)) {
205 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
208 DEBUG(5, ("rpc command function succedded\n"));
211 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
213 TALLOC_FREE(pipe_hnd
);
218 /* close the connection only if it was opened here */
223 talloc_destroy(mem_ctx
);
228 * Force a change of the trust acccount password.
230 * All parameters are provided by the run_rpc_command function, except for
231 * argc, argv which are passed through.
233 * @param domain_sid The domain sid acquired from the remote server.
234 * @param cli A cli_state connected to the server.
235 * @param mem_ctx Talloc context, destroyed on completion of the function.
236 * @param argc Standard main() style argc.
237 * @param argv Standard main() style argv. Initial components are already
240 * @return Normal NTSTATUS return.
243 static NTSTATUS
rpc_changetrustpw_internals(struct net_context
*c
,
244 const DOM_SID
*domain_sid
,
245 const char *domain_name
,
246 struct cli_state
*cli
,
247 struct rpc_pipe_client
*pipe_hnd
,
254 status
= trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
);
255 if (!NT_STATUS_IS_OK(status
)) {
256 d_fprintf(stderr
, _("Failed to change machine account password: %s\n"),
265 * Force a change of the trust acccount password.
267 * @param argc Standard main() style argc.
268 * @param argv Standard main() style argv. Initial components are already
271 * @return A shell status integer (0 for success).
274 int net_rpc_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
276 if (c
->display_usage
) {
278 "net rpc changetrustpw\n"
281 _("Change the machine trust password"));
285 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
286 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
287 rpc_changetrustpw_internals
,
292 * Join a domain, the old way.
294 * This uses 'machinename' as the inital password, and changes it.
296 * The password should be created with 'server manager' or equiv first.
298 * All parameters are provided by the run_rpc_command function, except for
299 * argc, argv which are passed through.
301 * @param domain_sid The domain sid acquired from the remote server.
302 * @param cli A cli_state connected to the server.
303 * @param mem_ctx Talloc context, destroyed on completion of the function.
304 * @param argc Standard main() style argc.
305 * @param argv Standard main() style argv. Initial components are already
308 * @return Normal NTSTATUS return.
311 static NTSTATUS
rpc_oldjoin_internals(struct net_context
*c
,
312 const DOM_SID
*domain_sid
,
313 const char *domain_name
,
314 struct cli_state
*cli
,
315 struct rpc_pipe_client
*pipe_hnd
,
321 fstring trust_passwd
;
322 unsigned char orig_trust_passwd_hash
[16];
324 enum netr_SchannelType sec_channel_type
;
326 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
328 if (!NT_STATUS_IS_OK(result
)) {
329 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
332 nt_errstr(result
) ));
337 check what type of join - if the user want's to join as
338 a BDC, the server must agree that we are a BDC.
341 sec_channel_type
= get_sec_channel_type(argv
[0]);
343 sec_channel_type
= get_sec_channel_type(NULL
);
346 fstrcpy(trust_passwd
, global_myname());
347 strlower_m(trust_passwd
);
350 * Machine names can be 15 characters, but the max length on
351 * a password is 14. --jerry
354 trust_passwd
[14] = '\0';
356 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
358 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
,
360 orig_trust_passwd_hash
,
363 if (NT_STATUS_IS_OK(result
))
364 printf(_("Joined domain %s.\n"), c
->opt_target_workgroup
);
367 if (!secrets_store_domain_sid(c
->opt_target_workgroup
, domain_sid
)) {
368 DEBUG(0, ("error storing domain sid for %s\n", c
->opt_target_workgroup
));
369 result
= NT_STATUS_UNSUCCESSFUL
;
376 * Join a domain, the old way.
378 * @param argc Standard main() style argc.
379 * @param argv Standard main() style argv. Initial components are already
382 * @return A shell status integer (0 for success).
385 static int net_rpc_perform_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
387 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
388 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
389 rpc_oldjoin_internals
,
394 * Join a domain, the old way. This function exists to allow
395 * the message to be displayed when oldjoin was explicitly
396 * requested, but not when it was implied by "net rpc join".
398 * @param argc Standard main() style argc.
399 * @param argv Standard main() style argv. Initial components are already
402 * @return A shell status integer (0 for success).
405 static int net_rpc_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
409 if (c
->display_usage
) {
414 _("Join a domain the old way"));
418 rc
= net_rpc_perform_oldjoin(c
, argc
, argv
);
421 d_fprintf(stderr
, _("Failed to join domain\n"));
428 * 'net rpc join' entrypoint.
429 * @param argc Standard main() style argc.
430 * @param argv Standard main() style argv. Initial components are already
433 * Main 'net_rpc_join()' (where the admin username/password is used) is
435 * Try to just change the password, but if that doesn't work, use/prompt
436 * for a username/password.
439 int net_rpc_join(struct net_context
*c
, int argc
, const char **argv
)
441 if (c
->display_usage
) {
444 _("net rpc join -U <username>[%%password] <type>\n"
446 " username\tName of the admin user"
447 " password\tPassword of the admin user, will "
448 "prompt if not specified\n"
449 " type\tCan be one of the following:\n"
450 "\t\tMEMBER\tJoin as member server (default)\n"
451 "\t\tBDC\tJoin as BDC\n"
452 "\t\tPDC\tJoin as PDC\n"));
456 if (lp_server_role() == ROLE_STANDALONE
) {
457 d_printf(_("cannot join as standalone machine\n"));
461 if (strlen(global_myname()) > 15) {
462 d_printf(_("Our netbios name can be at most 15 chars long, "
463 "\"%s\" is %u chars long\n"),
464 global_myname(), (unsigned int)strlen(global_myname()));
468 if ((net_rpc_perform_oldjoin(c
, argc
, argv
) == 0))
471 return net_rpc_join_newstyle(c
, argc
, argv
);
475 * display info about a rpc domain
477 * All parameters are provided by the run_rpc_command function, except for
478 * argc, argv which are passed through.
480 * @param domain_sid The domain sid acquired from the remote server
481 * @param cli A cli_state connected to the server.
482 * @param mem_ctx Talloc context, destroyed on completion of the function.
483 * @param argc Standard main() style argc.
484 * @param argv Standard main() style argv. Initial components are already
487 * @return Normal NTSTATUS return.
490 NTSTATUS
rpc_info_internals(struct net_context
*c
,
491 const DOM_SID
*domain_sid
,
492 const char *domain_name
,
493 struct cli_state
*cli
,
494 struct rpc_pipe_client
*pipe_hnd
,
499 struct policy_handle connect_pol
, domain_pol
;
500 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
501 union samr_DomainInfo
*info
= NULL
;
504 sid_to_fstring(sid_str
, domain_sid
);
506 /* Get sam policy handle */
507 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
509 MAXIMUM_ALLOWED_ACCESS
,
511 if (!NT_STATUS_IS_OK(result
)) {
512 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
517 /* Get domain policy handle */
518 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
520 MAXIMUM_ALLOWED_ACCESS
,
521 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
523 if (!NT_STATUS_IS_OK(result
)) {
524 d_fprintf(stderr
, _("Could not open domain: %s\n"),
529 result
= rpccli_samr_QueryDomainInfo(pipe_hnd
, mem_ctx
,
533 if (NT_STATUS_IS_OK(result
)) {
534 d_printf(_("Domain Name: %s\n"),
535 info
->general
.domain_name
.string
);
536 d_printf(_("Domain SID: %s\n"), sid_str
);
537 d_printf(_("Sequence number: %llu\n"),
538 (unsigned long long)info
->general
.sequence_num
);
539 d_printf(_("Num users: %u\n"), info
->general
.num_users
);
540 d_printf(_("Num domain groups: %u\n"),info
->general
.num_groups
);
541 d_printf(_("Num local groups: %u\n"),info
->general
.num_aliases
);
549 * 'net rpc info' entrypoint.
550 * @param argc Standard main() style argc.
551 * @param argv Standard main() style argv. Initial components are already
555 int net_rpc_info(struct net_context
*c
, int argc
, const char **argv
)
557 if (c
->display_usage
) {
562 _("Display information about the domain"));
566 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
567 NET_FLAGS_PDC
, rpc_info_internals
,
572 * Fetch domain SID into the local secrets.tdb.
574 * All parameters are provided by the run_rpc_command function, except for
575 * argc, argv which are passed through.
577 * @param domain_sid The domain sid acquired from the remote server.
578 * @param cli A cli_state connected to the server.
579 * @param mem_ctx Talloc context, destroyed on completion of the function.
580 * @param argc Standard main() style argc.
581 * @param argv Standard main() style argv. Initial components are already
584 * @return Normal NTSTATUS return.
587 static NTSTATUS
rpc_getsid_internals(struct net_context
*c
,
588 const DOM_SID
*domain_sid
,
589 const char *domain_name
,
590 struct cli_state
*cli
,
591 struct rpc_pipe_client
*pipe_hnd
,
598 sid_to_fstring(sid_str
, domain_sid
);
599 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
600 sid_str
, domain_name
);
602 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
603 DEBUG(0,("Can't store domain SID\n"));
604 return NT_STATUS_UNSUCCESSFUL
;
611 * 'net rpc getsid' entrypoint.
612 * @param argc Standard main() style argc.
613 * @param argv Standard main() style argv. Initial components are already
617 int net_rpc_getsid(struct net_context
*c
, int argc
, const char **argv
)
619 int conn_flags
= NET_FLAGS_PDC
;
621 if (!c
->opt_user_specified
) {
622 conn_flags
|= NET_FLAGS_ANONYMOUS
;
625 if (c
->display_usage
) {
630 _("Fetch domain SID into local secrets.tdb"));
634 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
636 rpc_getsid_internals
,
640 /****************************************************************************/
643 * Basic usage function for 'net rpc user'.
644 * @param argc Standard main() style argc.
645 * @param argv Standard main() style argv. Initial components are already
649 static int rpc_user_usage(struct net_context
*c
, int argc
, const char **argv
)
651 return net_user_usage(c
, argc
, argv
);
655 * Add a new user to a remote RPC server.
657 * @param argc Standard main() style argc.
658 * @param argv Standard main() style argv. Initial components are already
661 * @return A shell status integer (0 for success).
664 static int rpc_user_add(struct net_context
*c
, int argc
, const char **argv
)
666 NET_API_STATUS status
;
667 struct USER_INFO_1 info1
;
668 uint32_t parm_error
= 0;
670 if (argc
< 1 || c
->display_usage
) {
671 rpc_user_usage(c
, argc
, argv
);
677 info1
.usri1_name
= argv
[0];
679 info1
.usri1_password
= argv
[1];
682 status
= NetUserAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
685 d_fprintf(stderr
,_("Failed to add user '%s' with error: %s.\n"),
686 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
690 d_printf(_("Added user '%s'.\n"), argv
[0]);
697 * Rename a user on a remote RPC server.
699 * @param argc Standard main() style argc.
700 * @param argv Standard main() style argv. Initial components are already
703 * @return A shell status integer (0 for success).
706 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
708 NET_API_STATUS status
;
709 struct USER_INFO_0 u0
;
710 uint32_t parm_err
= 0;
712 if (argc
!= 2 || c
->display_usage
) {
713 rpc_user_usage(c
, argc
, argv
);
717 u0
.usri0_name
= argv
[1];
719 status
= NetUserSetInfo(c
->opt_host
, argv
[0],
720 0, (uint8_t *)&u0
, &parm_err
);
723 _("Failed to rename user from %s to %s - %s\n"),
725 libnetapi_get_error_string(c
->netapi_ctx
, status
));
727 d_printf(_("Renamed user from %s to %s\n"), argv
[0], argv
[1]);
734 * Set a user's primary group
736 * @param argc Standard main() style argc.
737 * @param argv Standard main() style argv. Initial components are already
740 * @return A shell status integer (0 for success).
743 static int rpc_user_setprimarygroup(struct net_context
*c
, int argc
,
746 NET_API_STATUS status
;
748 struct GROUP_INFO_2
*g2
;
749 struct USER_INFO_1051 u1051
;
750 uint32_t parm_err
= 0;
752 if (argc
!= 2 || c
->display_usage
) {
753 rpc_user_usage(c
, argc
, argv
);
757 status
= NetGroupGetInfo(c
->opt_host
, argv
[1], 2, &buffer
);
759 d_fprintf(stderr
, _("Failed to find group name %s -- %s\n"),
761 libnetapi_get_error_string(c
->netapi_ctx
, status
));
764 g2
= (struct GROUP_INFO_2
*)buffer
;
766 u1051
.usri1051_primary_group_id
= g2
->grpi2_group_id
;
768 NetApiBufferFree(buffer
);
770 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1051,
771 (uint8_t *)&u1051
, &parm_err
);
774 _("Failed to set user's primary group %s to %s - "
775 "%s\n"), argv
[0], argv
[1],
776 libnetapi_get_error_string(c
->netapi_ctx
, status
));
778 d_printf(_("Set primary group of user %s to %s\n"), argv
[0],
785 * Delete a user from a remote RPC server.
787 * @param argc Standard main() style argc.
788 * @param argv Standard main() style argv. Initial components are already
791 * @return A shell status integer (0 for success).
794 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
796 NET_API_STATUS status
;
798 if (argc
< 1 || c
->display_usage
) {
799 rpc_user_usage(c
, argc
, argv
);
803 status
= NetUserDel(c
->opt_host
, argv
[0]);
806 d_fprintf(stderr
, _("Failed to delete user '%s' with: %s.\n"),
808 libnetapi_get_error_string(c
->netapi_ctx
, status
));
811 d_printf(_("Deleted user '%s'.\n"), argv
[0]);
818 * Set a user's password on a remote RPC server.
820 * @param argc Standard main() style argc.
821 * @param argv Standard main() style argv. Initial components are already
824 * @return A shell status integer (0 for success).
827 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
829 NET_API_STATUS status
;
831 struct USER_INFO_1003 u1003
;
832 uint32_t parm_err
= 0;
835 if (argc
< 1 || c
->display_usage
) {
836 rpc_user_usage(c
, argc
, argv
);
841 u1003
.usri1003_password
= argv
[1];
843 ret
= asprintf(&prompt
, _("Enter new password for %s:"),
848 u1003
.usri1003_password
= getpass(prompt
);
852 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
854 /* Display results */
857 _("Failed to set password for '%s' with error: %s.\n"),
858 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
867 * List a user's groups from a remote RPC server.
869 * @param argc Standard main() style argc.
870 * @param argv Standard main() style argv. Initial components are already
873 * @return A shell status integer (0 for success)
876 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
879 NET_API_STATUS status
;
880 struct GROUP_USERS_INFO_0
*u0
= NULL
;
881 uint32_t entries_read
= 0;
882 uint32_t total_entries
= 0;
886 if (argc
< 1 || c
->display_usage
) {
887 rpc_user_usage(c
, argc
, argv
);
891 status
= NetUserGetGroups(c
->opt_host
,
894 (uint8_t **)(void *)&u0
,
900 _("Failed to get groups for '%s' with error: %s.\n"),
901 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
906 for (i
=0; i
< entries_read
; i
++) {
907 printf("%s\n", u0
->grui0_name
);
915 * List users on a remote RPC server.
917 * All parameters are provided by the run_rpc_command function, except for
918 * argc, argv which are passed through.
920 * @param domain_sid The domain sid acquired from the remote server.
921 * @param cli A cli_state connected to the server.
922 * @param mem_ctx Talloc context, destroyed on completion of the function.
923 * @param argc Standard main() style argc.
924 * @param argv Standard main() style argv. Initial components are already
927 * @return Normal NTSTATUS return.
930 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
932 NET_API_STATUS status
;
933 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
934 struct NET_DISPLAY_USER
*info
= NULL
;
937 /* Query domain users */
938 if (c
->opt_long_list_entries
)
939 d_printf(_("\nUser name Comment"
940 "\n-----------------------------\n"));
942 uint32_t max_entries
, max_size
;
944 get_query_dispinfo_params(
945 loop_count
, &max_entries
, &max_size
);
947 status
= NetQueryDisplayInformation(c
->opt_host
,
954 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
958 info
= (struct NET_DISPLAY_USER
*)buffer
;
960 for (i
= 0; i
< num_entries
; i
++) {
962 if (c
->opt_long_list_entries
)
963 printf("%-21.21s %s\n", info
->usri1_name
,
964 info
->usri1_comment
);
966 printf("%s\n", info
->usri1_name
);
970 NetApiBufferFree(buffer
);
973 start_idx
+= num_entries
;
975 } while (status
== ERROR_MORE_DATA
);
981 * 'net rpc user' entrypoint.
982 * @param argc Standard main() style argc.
983 * @param argv Standard main() style argv. Initial components are already
987 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
989 NET_API_STATUS status
;
991 struct functable func
[] = {
996 N_("Add specified user"),
997 N_("net rpc user add\n"
998 " Add specified user")
1004 N_("List domain groups of user"),
1005 N_("net rpc user info\n"
1006 " Lis domain groups of user")
1012 N_("Remove specified user"),
1013 N_("net rpc user delete\n"
1014 " Remove specified user")
1020 N_("Change user password"),
1021 N_("net rpc user password\n"
1022 " Change user password")
1028 N_("Rename specified user"),
1029 N_("net rpc user rename\n"
1030 " Rename specified user")
1034 rpc_user_setprimarygroup
,
1036 "Set a user's primary group",
1037 "net rpc user setprimarygroup\n"
1038 " Set a user's primary group"
1040 {NULL
, NULL
, 0, NULL
, NULL
}
1043 status
= libnetapi_init(&c
->netapi_ctx
);
1047 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1048 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1049 if (c
->opt_kerberos
) {
1050 libnetapi_set_use_kerberos(c
->netapi_ctx
);
1054 if (c
->display_usage
) {
1059 _("List all users"));
1060 net_display_usage_from_functable(func
);
1064 return rpc_user_list(c
, argc
, argv
);
1067 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
1070 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1071 TALLOC_CTX
*mem_ctx
,
1072 struct rpc_sh_ctx
*ctx
,
1073 struct rpc_pipe_client
*pipe_hnd
,
1074 int argc
, const char **argv
)
1076 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
1079 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1080 TALLOC_CTX
*mem_ctx
,
1081 struct rpc_sh_ctx
*ctx
,
1082 struct rpc_pipe_client
*pipe_hnd
,
1083 int argc
, const char **argv
)
1085 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
1088 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1089 TALLOC_CTX
*mem_ctx
,
1090 struct rpc_sh_ctx
*ctx
,
1091 struct rpc_pipe_client
*pipe_hnd
,
1092 int argc
, const char **argv
,
1094 struct net_context
*c
,
1095 TALLOC_CTX
*mem_ctx
,
1096 struct rpc_sh_ctx
*ctx
,
1097 struct rpc_pipe_client
*pipe_hnd
,
1098 struct policy_handle
*user_hnd
,
1099 int argc
, const char **argv
))
1101 struct policy_handle connect_pol
, domain_pol
, user_pol
;
1102 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1105 enum lsa_SidType type
;
1108 d_fprintf(stderr
, "%s %s <username>\n", _("Usage:"),
1110 return NT_STATUS_INVALID_PARAMETER
;
1113 ZERO_STRUCT(connect_pol
);
1114 ZERO_STRUCT(domain_pol
);
1115 ZERO_STRUCT(user_pol
);
1117 result
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1118 argv
[0], NULL
, NULL
, &sid
, &type
);
1119 if (!NT_STATUS_IS_OK(result
)) {
1120 d_fprintf(stderr
, _("Could not lookup %s: %s\n"), argv
[0],
1125 if (type
!= SID_NAME_USER
) {
1126 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
1127 sid_type_lookup(type
));
1128 result
= NT_STATUS_NO_SUCH_USER
;
1132 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1133 d_fprintf(stderr
, _("%s is not in our domain\n"), argv
[0]);
1134 result
= NT_STATUS_NO_SUCH_USER
;
1138 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1140 MAXIMUM_ALLOWED_ACCESS
,
1142 if (!NT_STATUS_IS_OK(result
)) {
1146 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1148 MAXIMUM_ALLOWED_ACCESS
,
1151 if (!NT_STATUS_IS_OK(result
)) {
1155 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1157 MAXIMUM_ALLOWED_ACCESS
,
1160 if (!NT_STATUS_IS_OK(result
)) {
1164 result
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1167 if (is_valid_policy_hnd(&user_pol
)) {
1168 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1170 if (is_valid_policy_hnd(&domain_pol
)) {
1171 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1173 if (is_valid_policy_hnd(&connect_pol
)) {
1174 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1179 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1180 TALLOC_CTX
*mem_ctx
,
1181 struct rpc_sh_ctx
*ctx
,
1182 struct rpc_pipe_client
*pipe_hnd
,
1183 struct policy_handle
*user_hnd
,
1184 int argc
, const char **argv
)
1187 union samr_UserInfo
*info
= NULL
;
1190 d_fprintf(stderr
, "%s %s show <username>\n", _("Usage:"),
1192 return NT_STATUS_INVALID_PARAMETER
;
1195 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1199 if (!NT_STATUS_IS_OK(result
)) {
1203 d_printf(_("user rid: %d, group rid: %d\n"),
1205 info
->info21
.primary_gid
);
1210 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1211 TALLOC_CTX
*mem_ctx
,
1212 struct rpc_sh_ctx
*ctx
,
1213 struct rpc_pipe_client
*pipe_hnd
,
1214 int argc
, const char **argv
)
1216 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1217 rpc_sh_user_show_internals
);
1220 #define FETCHSTR(name, rec) \
1221 do { if (strequal(ctx->thiscmd, name)) { \
1222 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1225 #define SETSTR(name, rec, flag) \
1226 do { if (strequal(ctx->thiscmd, name)) { \
1227 init_lsa_String(&(info->info21.rec), argv[0]); \
1228 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1231 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1232 TALLOC_CTX
*mem_ctx
,
1233 struct rpc_sh_ctx
*ctx
,
1234 struct rpc_pipe_client
*pipe_hnd
,
1235 struct policy_handle
*user_hnd
,
1236 int argc
, const char **argv
)
1239 const char *username
;
1240 const char *oldval
= "";
1241 union samr_UserInfo
*info
= NULL
;
1244 d_fprintf(stderr
, "%s %s <username> [new value|NULL]\n",
1245 _("Usage:"), ctx
->whoami
);
1246 return NT_STATUS_INVALID_PARAMETER
;
1249 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1253 if (!NT_STATUS_IS_OK(result
)) {
1257 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1259 FETCHSTR("fullname", full_name
);
1260 FETCHSTR("homedir", home_directory
);
1261 FETCHSTR("homedrive", home_drive
);
1262 FETCHSTR("logonscript", logon_script
);
1263 FETCHSTR("profilepath", profile_path
);
1264 FETCHSTR("description", description
);
1267 d_printf(_("%s's %s: [%s]\n"), username
, ctx
->thiscmd
, oldval
);
1271 if (strcmp(argv
[0], "NULL") == 0) {
1275 ZERO_STRUCT(info
->info21
);
1277 SETSTR("fullname", full_name
, FULL_NAME
);
1278 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1279 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1280 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1281 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1282 SETSTR("description", description
, DESCRIPTION
);
1284 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1289 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username
,
1290 ctx
->thiscmd
, oldval
, argv
[0]);
1297 #define HANDLEFLG(name, rec) \
1298 do { if (strequal(ctx->thiscmd, name)) { \
1299 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1301 newflags = oldflags | ACB_##rec; \
1303 newflags = oldflags & ~ACB_##rec; \
1306 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1307 TALLOC_CTX
*mem_ctx
,
1308 struct rpc_sh_ctx
*ctx
,
1309 struct rpc_pipe_client
*pipe_hnd
,
1310 int argc
, const char **argv
)
1312 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1313 rpc_sh_user_str_edit_internals
);
1316 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1317 TALLOC_CTX
*mem_ctx
,
1318 struct rpc_sh_ctx
*ctx
,
1319 struct rpc_pipe_client
*pipe_hnd
,
1320 struct policy_handle
*user_hnd
,
1321 int argc
, const char **argv
)
1324 const char *username
;
1325 const char *oldval
= "unknown";
1326 uint32 oldflags
, newflags
;
1328 union samr_UserInfo
*info
= NULL
;
1331 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1332 !strequal(argv
[0], "no"))) {
1333 /* TRANSATORS: The yes|no here are program keywords. Please do
1335 d_fprintf(stderr
, _("Usage: %s <username> [yes|no]\n"),
1337 return NT_STATUS_INVALID_PARAMETER
;
1340 newval
= strequal(argv
[0], "yes");
1342 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1346 if (!NT_STATUS_IS_OK(result
)) {
1350 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1351 oldflags
= info
->info21
.acct_flags
;
1352 newflags
= info
->info21
.acct_flags
;
1354 HANDLEFLG("disabled", DISABLED
);
1355 HANDLEFLG("pwnotreq", PWNOTREQ
);
1356 HANDLEFLG("autolock", AUTOLOCK
);
1357 HANDLEFLG("pwnoexp", PWNOEXP
);
1360 d_printf(_("%s's %s flag: %s\n"), username
, ctx
->thiscmd
,
1365 ZERO_STRUCT(info
->info21
);
1367 info
->info21
.acct_flags
= newflags
;
1368 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1370 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1375 if (NT_STATUS_IS_OK(result
)) {
1376 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username
,
1377 ctx
->thiscmd
, oldval
, argv
[0]);
1385 static NTSTATUS
rpc_sh_user_flag_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_flag_edit_internals
);
1395 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1396 TALLOC_CTX
*mem_ctx
,
1397 struct rpc_sh_ctx
*ctx
)
1399 static struct rpc_sh_cmd cmds
[] = {
1401 { "fullname", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1402 N_("Show/Set a user's full name") },
1404 { "homedir", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1405 N_("Show/Set a user's home directory") },
1407 { "homedrive", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1408 N_("Show/Set a user's home drive") },
1410 { "logonscript", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1411 N_("Show/Set a user's logon script") },
1413 { "profilepath", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1414 N_("Show/Set a user's profile path") },
1416 { "description", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1417 N_("Show/Set a user's description") },
1419 { "disabled", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1420 N_("Show/Set whether a user is disabled") },
1422 { "autolock", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1423 N_("Show/Set whether a user locked out") },
1425 { "pwnotreq", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1426 N_("Show/Set whether a user does not need a password") },
1428 { "pwnoexp", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1429 N_("Show/Set whether a user's password does not expire") },
1431 { NULL
, NULL
, 0, NULL
, NULL
}
1437 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1438 TALLOC_CTX
*mem_ctx
,
1439 struct rpc_sh_ctx
*ctx
)
1441 static struct rpc_sh_cmd cmds
[] = {
1443 { "list", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_list
,
1444 N_("List available users") },
1446 { "info", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_info
,
1447 N_("List the domain groups a user is member of") },
1449 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_show
,
1450 N_("Show info about a user") },
1452 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1453 N_("Show/Modify a user's fields") },
1455 { NULL
, NULL
, 0, NULL
, NULL
}
1461 /****************************************************************************/
1464 * Basic usage function for 'net rpc group'.
1465 * @param argc Standard main() style argc.
1466 * @param argv Standard main() style argv. Initial components are already
1470 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1472 return net_group_usage(c
, argc
, argv
);
1476 * Delete group on a remote RPC server.
1478 * All parameters are provided by the run_rpc_command function, except for
1479 * argc, argv which are passed through.
1481 * @param domain_sid The domain sid acquired from the remote server.
1482 * @param cli A cli_state connected to the server.
1483 * @param mem_ctx Talloc context, destroyed on completion of the function.
1484 * @param argc Standard main() style argc.
1485 * @param argv Standard main() style argv. Initial components are already
1488 * @return Normal NTSTATUS return.
1491 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1492 const DOM_SID
*domain_sid
,
1493 const char *domain_name
,
1494 struct cli_state
*cli
,
1495 struct rpc_pipe_client
*pipe_hnd
,
1496 TALLOC_CTX
*mem_ctx
,
1500 struct policy_handle connect_pol
, domain_pol
, group_pol
, user_pol
;
1501 bool group_is_primary
= false;
1502 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1504 struct samr_RidTypeArray
*rids
= NULL
;
1507 /* struct samr_RidWithAttribute *user_gids; */
1509 struct samr_Ids group_rids
, name_types
;
1510 struct lsa_String lsa_acct_name
;
1511 union samr_UserInfo
*info
= NULL
;
1513 if (argc
< 1 || c
->display_usage
) {
1514 rpc_group_usage(c
, argc
,argv
);
1515 return NT_STATUS_OK
; /* ok? */
1518 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1520 MAXIMUM_ALLOWED_ACCESS
,
1523 if (!NT_STATUS_IS_OK(result
)) {
1524 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1528 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1530 MAXIMUM_ALLOWED_ACCESS
,
1531 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1534 if (!NT_STATUS_IS_OK(result
)) {
1535 d_fprintf(stderr
, _("Request open_domain failed\n"));
1539 init_lsa_String(&lsa_acct_name
, argv
[0]);
1541 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1547 if (!NT_STATUS_IS_OK(result
)) {
1548 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1552 switch (name_types
.ids
[0])
1554 case SID_NAME_DOM_GRP
:
1555 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1557 MAXIMUM_ALLOWED_ACCESS
,
1560 if (!NT_STATUS_IS_OK(result
)) {
1561 d_fprintf(stderr
, _("Request open_group failed"));
1565 group_rid
= group_rids
.ids
[0];
1567 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
1571 if (!NT_STATUS_IS_OK(result
)) {
1573 _("Unable to query group members of %s"),
1578 if (c
->opt_verbose
) {
1580 _("Domain Group %s (rid: %d) has %d members\n"),
1581 argv
[0],group_rid
, rids
->count
);
1584 /* Check if group is anyone's primary group */
1585 for (i
= 0; i
< rids
->count
; i
++)
1587 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1589 MAXIMUM_ALLOWED_ACCESS
,
1593 if (!NT_STATUS_IS_OK(result
)) {
1595 _("Unable to open group member %d\n"),
1600 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1605 if (!NT_STATUS_IS_OK(result
)) {
1607 _("Unable to lookup userinfo for group "
1613 if (info
->info21
.primary_gid
== group_rid
) {
1614 if (c
->opt_verbose
) {
1615 d_printf(_("Group is primary group "
1617 info
->info21
.account_name
.string
);
1619 group_is_primary
= true;
1622 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1625 if (group_is_primary
) {
1626 d_fprintf(stderr
, _("Unable to delete group because "
1627 "some of it's members have it as primary "
1629 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1633 /* remove all group members */
1634 for (i
= 0; i
< rids
->count
; i
++)
1637 d_printf(_("Remove group member %d..."),
1639 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1643 if (NT_STATUS_IS_OK(result
)) {
1645 d_printf(_("ok\n"));
1648 d_printf("%s\n", _("failed"));
1653 result
= rpccli_samr_DeleteDomainGroup(pipe_hnd
, mem_ctx
,
1657 /* removing a local group is easier... */
1658 case SID_NAME_ALIAS
:
1659 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1661 MAXIMUM_ALLOWED_ACCESS
,
1665 if (!NT_STATUS_IS_OK(result
)) {
1666 d_fprintf(stderr
, _("Request open_alias failed\n"));
1670 result
= rpccli_samr_DeleteDomAlias(pipe_hnd
, mem_ctx
,
1674 d_fprintf(stderr
, _("%s is of type %s. This command is only "
1675 "for deleting local or global groups\n"),
1676 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1677 result
= NT_STATUS_UNSUCCESSFUL
;
1681 if (NT_STATUS_IS_OK(result
)) {
1683 d_printf(_("Deleted %s '%s'\n"),
1684 sid_type_lookup(name_types
.ids
[0]), argv
[0]);
1686 d_fprintf(stderr
, _("Deleting of %s failed: %s\n"), argv
[0],
1687 get_friendly_nt_error_msg(result
));
1695 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1697 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1698 rpc_group_delete_internals
, argc
,argv
);
1701 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1703 NET_API_STATUS status
;
1704 struct GROUP_INFO_1 info1
;
1705 uint32_t parm_error
= 0;
1707 if (argc
!= 1 || c
->display_usage
) {
1708 rpc_group_usage(c
, argc
, argv
);
1714 info1
.grpi1_name
= argv
[0];
1715 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1716 info1
.grpi1_comment
= c
->opt_comment
;
1719 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1723 _("Failed to add group '%s' with error: %s.\n"),
1724 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1728 d_printf(_("Added group '%s'.\n"), argv
[0]);
1734 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1736 NET_API_STATUS status
;
1737 struct LOCALGROUP_INFO_1 info1
;
1738 uint32_t parm_error
= 0;
1740 if (argc
!= 1 || c
->display_usage
) {
1741 rpc_group_usage(c
, argc
, argv
);
1747 info1
.lgrpi1_name
= argv
[0];
1748 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1749 info1
.lgrpi1_comment
= c
->opt_comment
;
1752 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1756 _("Failed to add alias '%s' with error: %s.\n"),
1757 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1761 d_printf(_("Added alias '%s'.\n"), argv
[0]);
1767 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1769 if (c
->opt_localgroup
)
1770 return rpc_alias_add_internals(c
, argc
, argv
);
1772 return rpc_group_add_internals(c
, argc
, argv
);
1775 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1776 TALLOC_CTX
*mem_ctx
,
1779 enum lsa_SidType
*type
)
1781 DOM_SID
*sids
= NULL
;
1782 enum lsa_SidType
*types
= NULL
;
1783 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1784 struct policy_handle lsa_pol
;
1785 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1787 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1789 if (!NT_STATUS_IS_OK(result
)) {
1793 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1794 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1796 if (!NT_STATUS_IS_OK(result
)) {
1800 result
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1801 &name
, NULL
, 1, &sids
, &types
);
1803 if (NT_STATUS_IS_OK(result
)) {
1804 sid_copy(sid
, &sids
[0]);
1808 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
1812 TALLOC_FREE(pipe_hnd
);
1815 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
1817 /* Try as S-1-5-whatever */
1821 if (string_to_sid(&tmp_sid
, name
)) {
1822 sid_copy(sid
, &tmp_sid
);
1823 *type
= SID_NAME_UNKNOWN
;
1824 result
= NT_STATUS_OK
;
1831 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
1832 TALLOC_CTX
*mem_ctx
,
1833 const DOM_SID
*group_sid
,
1836 struct policy_handle connect_pol
, domain_pol
;
1839 struct policy_handle group_pol
;
1841 struct samr_Ids rids
, rid_types
;
1842 struct lsa_String lsa_acct_name
;
1846 sid_copy(&sid
, group_sid
);
1848 if (!sid_split_rid(&sid
, &group_rid
)) {
1849 return NT_STATUS_UNSUCCESSFUL
;
1852 /* Get sam policy handle */
1853 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1855 MAXIMUM_ALLOWED_ACCESS
,
1857 if (!NT_STATUS_IS_OK(result
)) {
1861 /* Get domain policy handle */
1862 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1864 MAXIMUM_ALLOWED_ACCESS
,
1867 if (!NT_STATUS_IS_OK(result
)) {
1871 init_lsa_String(&lsa_acct_name
, member
);
1873 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1880 if (!NT_STATUS_IS_OK(result
)) {
1881 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
1886 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1888 MAXIMUM_ALLOWED_ACCESS
,
1892 if (!NT_STATUS_IS_OK(result
)) {
1896 result
= rpccli_samr_AddGroupMember(pipe_hnd
, mem_ctx
,
1899 0x0005); /* unknown flags */
1902 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1906 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
1907 TALLOC_CTX
*mem_ctx
,
1908 const DOM_SID
*alias_sid
,
1911 struct policy_handle connect_pol
, domain_pol
;
1914 struct policy_handle alias_pol
;
1917 enum lsa_SidType member_type
;
1921 sid_copy(&sid
, alias_sid
);
1923 if (!sid_split_rid(&sid
, &alias_rid
)) {
1924 return NT_STATUS_UNSUCCESSFUL
;
1927 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
1928 member
, &member_sid
, &member_type
);
1930 if (!NT_STATUS_IS_OK(result
)) {
1931 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
1936 /* Get sam policy handle */
1937 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1939 MAXIMUM_ALLOWED_ACCESS
,
1941 if (!NT_STATUS_IS_OK(result
)) {
1945 /* Get domain policy handle */
1946 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1948 MAXIMUM_ALLOWED_ACCESS
,
1951 if (!NT_STATUS_IS_OK(result
)) {
1955 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1957 MAXIMUM_ALLOWED_ACCESS
,
1961 if (!NT_STATUS_IS_OK(result
)) {
1965 result
= rpccli_samr_AddAliasMember(pipe_hnd
, mem_ctx
,
1969 if (!NT_STATUS_IS_OK(result
)) {
1974 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1978 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
1979 const DOM_SID
*domain_sid
,
1980 const char *domain_name
,
1981 struct cli_state
*cli
,
1982 struct rpc_pipe_client
*pipe_hnd
,
1983 TALLOC_CTX
*mem_ctx
,
1988 enum lsa_SidType group_type
;
1990 if (argc
!= 2 || c
->display_usage
) {
1993 _("net rpc group addmem <group> <member>\n"
1994 " Add a member to a group\n"
1995 " group\tGroup to add member to\n"
1996 " member\tMember to add to group\n"));
1997 return NT_STATUS_UNSUCCESSFUL
;
2000 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2001 &group_sid
, &group_type
))) {
2002 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2004 return NT_STATUS_UNSUCCESSFUL
;
2007 if (group_type
== SID_NAME_DOM_GRP
) {
2008 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2009 &group_sid
, argv
[1]);
2011 if (!NT_STATUS_IS_OK(result
)) {
2012 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2013 argv
[1], argv
[0], nt_errstr(result
));
2018 if (group_type
== SID_NAME_ALIAS
) {
2019 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2020 &group_sid
, argv
[1]);
2022 if (!NT_STATUS_IS_OK(result
)) {
2023 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2024 argv
[1], argv
[0], nt_errstr(result
));
2029 d_fprintf(stderr
, _("Can only add members to global or local groups "
2030 "which %s is not\n"), argv
[0]);
2032 return NT_STATUS_UNSUCCESSFUL
;
2035 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2037 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2038 rpc_group_addmem_internals
,
2042 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2043 struct rpc_pipe_client
*pipe_hnd
,
2044 TALLOC_CTX
*mem_ctx
,
2045 const DOM_SID
*group_sid
,
2048 struct policy_handle connect_pol
, domain_pol
;
2051 struct policy_handle group_pol
;
2053 struct samr_Ids rids
, rid_types
;
2054 struct lsa_String lsa_acct_name
;
2058 sid_copy(&sid
, group_sid
);
2060 if (!sid_split_rid(&sid
, &group_rid
))
2061 return NT_STATUS_UNSUCCESSFUL
;
2063 /* Get sam policy handle */
2064 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2066 MAXIMUM_ALLOWED_ACCESS
,
2068 if (!NT_STATUS_IS_OK(result
))
2071 /* Get domain policy handle */
2072 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2074 MAXIMUM_ALLOWED_ACCESS
,
2077 if (!NT_STATUS_IS_OK(result
))
2080 init_lsa_String(&lsa_acct_name
, member
);
2082 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2088 if (!NT_STATUS_IS_OK(result
)) {
2089 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2094 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2096 MAXIMUM_ALLOWED_ACCESS
,
2100 if (!NT_STATUS_IS_OK(result
))
2103 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
2108 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2112 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2113 TALLOC_CTX
*mem_ctx
,
2114 const DOM_SID
*alias_sid
,
2117 struct policy_handle connect_pol
, domain_pol
;
2120 struct policy_handle alias_pol
;
2123 enum lsa_SidType member_type
;
2127 sid_copy(&sid
, alias_sid
);
2129 if (!sid_split_rid(&sid
, &alias_rid
))
2130 return NT_STATUS_UNSUCCESSFUL
;
2132 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2133 member
, &member_sid
, &member_type
);
2135 if (!NT_STATUS_IS_OK(result
)) {
2136 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2141 /* Get sam policy handle */
2142 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2144 MAXIMUM_ALLOWED_ACCESS
,
2146 if (!NT_STATUS_IS_OK(result
)) {
2150 /* Get domain policy handle */
2151 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2153 MAXIMUM_ALLOWED_ACCESS
,
2156 if (!NT_STATUS_IS_OK(result
)) {
2160 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2162 MAXIMUM_ALLOWED_ACCESS
,
2166 if (!NT_STATUS_IS_OK(result
))
2169 result
= rpccli_samr_DeleteAliasMember(pipe_hnd
, mem_ctx
,
2173 if (!NT_STATUS_IS_OK(result
))
2177 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2181 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2182 const DOM_SID
*domain_sid
,
2183 const char *domain_name
,
2184 struct cli_state
*cli
,
2185 struct rpc_pipe_client
*pipe_hnd
,
2186 TALLOC_CTX
*mem_ctx
,
2191 enum lsa_SidType group_type
;
2193 if (argc
!= 2 || c
->display_usage
) {
2196 _("net rpc group delmem <group> <member>\n"
2197 " Delete a member from a group\n"
2198 " group\tGroup to delete member from\n"
2199 " member\tMember to delete from group\n"));
2200 return NT_STATUS_UNSUCCESSFUL
;
2203 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2204 &group_sid
, &group_type
))) {
2205 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2207 return NT_STATUS_UNSUCCESSFUL
;
2210 if (group_type
== SID_NAME_DOM_GRP
) {
2211 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2212 &group_sid
, argv
[1]);
2214 if (!NT_STATUS_IS_OK(result
)) {
2215 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2216 argv
[1], argv
[0], nt_errstr(result
));
2221 if (group_type
== SID_NAME_ALIAS
) {
2222 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2223 &group_sid
, argv
[1]);
2225 if (!NT_STATUS_IS_OK(result
)) {
2226 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2227 argv
[1], argv
[0], nt_errstr(result
));
2232 d_fprintf(stderr
, _("Can only delete members from global or local "
2233 "groups which %s is not\n"), argv
[0]);
2235 return NT_STATUS_UNSUCCESSFUL
;
2238 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2240 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2241 rpc_group_delmem_internals
,
2246 * List groups on a remote RPC server.
2248 * All parameters are provided by the run_rpc_command function, except for
2249 * argc, argv which are passes through.
2251 * @param domain_sid The domain sid acquired from the remote server.
2252 * @param cli A cli_state connected to the server.
2253 * @param mem_ctx Talloc context, destroyed on completion of the function.
2254 * @param argc Standard main() style argc.
2255 * @param argv Standard main() style argv. Initial components are already
2258 * @return Normal NTSTATUS return.
2261 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2262 const DOM_SID
*domain_sid
,
2263 const char *domain_name
,
2264 struct cli_state
*cli
,
2265 struct rpc_pipe_client
*pipe_hnd
,
2266 TALLOC_CTX
*mem_ctx
,
2270 struct policy_handle connect_pol
, domain_pol
;
2271 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2272 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2273 struct samr_SamArray
*groups
= NULL
;
2274 bool global
= false;
2276 bool builtin
= false;
2278 if (c
->display_usage
) {
2281 _("net rpc group list [global] [local] [builtin]\n"
2282 " List groups on RPC server\n"
2283 " global\tList global groups\n"
2284 " local\tList local groups\n"
2285 " builtin\tList builtin groups\n"
2286 " If none of global, local or builtin is "
2287 "specified, all three options are considered "
2289 return NT_STATUS_OK
;
2298 for (i
=0; i
<argc
; i
++) {
2299 if (strequal(argv
[i
], "global"))
2302 if (strequal(argv
[i
], "local"))
2305 if (strequal(argv
[i
], "builtin"))
2309 /* Get sam policy handle */
2311 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2313 MAXIMUM_ALLOWED_ACCESS
,
2315 if (!NT_STATUS_IS_OK(result
)) {
2319 /* Get domain policy handle */
2321 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2323 MAXIMUM_ALLOWED_ACCESS
,
2324 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2326 if (!NT_STATUS_IS_OK(result
)) {
2330 /* Query domain groups */
2331 if (c
->opt_long_list_entries
)
2332 d_printf(_("\nGroup name Comment"
2333 "\n-----------------------------\n"));
2335 uint32_t max_size
, total_size
, returned_size
;
2336 union samr_DispInfo info
;
2340 get_query_dispinfo_params(
2341 loop_count
, &max_entries
, &max_size
);
2343 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
2352 num_entries
= info
.info3
.count
;
2353 start_idx
+= info
.info3
.count
;
2355 if (!NT_STATUS_IS_OK(result
) &&
2356 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2359 for (i
= 0; i
< num_entries
; i
++) {
2361 const char *group
= NULL
;
2362 const char *desc
= NULL
;
2364 group
= info
.info3
.entries
[i
].account_name
.string
;
2365 desc
= info
.info3
.entries
[i
].description
.string
;
2367 if (c
->opt_long_list_entries
)
2368 printf("%-21.21s %-50.50s\n",
2371 printf("%s\n", group
);
2373 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2374 /* query domain aliases */
2379 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2385 if (!NT_STATUS_IS_OK(result
) &&
2386 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2389 for (i
= 0; i
< num_entries
; i
++) {
2391 const char *description
= NULL
;
2393 if (c
->opt_long_list_entries
) {
2395 struct policy_handle alias_pol
;
2396 union samr_AliasInfo
*info
= NULL
;
2398 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2401 groups
->entries
[i
].idx
,
2403 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2407 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2409 description
= info
->description
.string
;
2413 if (description
!= NULL
) {
2414 printf("%-21.21s %-50.50s\n",
2415 groups
->entries
[i
].name
.string
,
2418 printf("%s\n", groups
->entries
[i
].name
.string
);
2421 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2422 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2423 /* Get builtin policy handle */
2425 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2427 MAXIMUM_ALLOWED_ACCESS
,
2428 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2430 if (!NT_STATUS_IS_OK(result
)) {
2433 /* query builtin aliases */
2436 if (!builtin
) break;
2438 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2444 if (!NT_STATUS_IS_OK(result
) &&
2445 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2448 for (i
= 0; i
< num_entries
; i
++) {
2450 const char *description
= NULL
;
2452 if (c
->opt_long_list_entries
) {
2454 struct policy_handle alias_pol
;
2455 union samr_AliasInfo
*info
= NULL
;
2457 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2460 groups
->entries
[i
].idx
,
2462 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2466 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2468 description
= info
->description
.string
;
2472 if (description
!= NULL
) {
2473 printf("%-21.21s %-50.50s\n",
2474 groups
->entries
[i
].name
.string
,
2477 printf("%s\n", groups
->entries
[i
].name
.string
);
2480 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2486 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2488 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2489 rpc_group_list_internals
,
2493 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2494 struct rpc_pipe_client
*pipe_hnd
,
2495 TALLOC_CTX
*mem_ctx
,
2496 const char *domain_name
,
2497 const DOM_SID
*domain_sid
,
2498 struct policy_handle
*domain_pol
,
2502 struct policy_handle group_pol
;
2503 uint32 num_members
, *group_rids
;
2505 struct samr_RidTypeArray
*rids
= NULL
;
2506 struct lsa_Strings names
;
2507 struct samr_Ids types
;
2510 sid_to_fstring(sid_str
, domain_sid
);
2512 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2514 MAXIMUM_ALLOWED_ACCESS
,
2518 if (!NT_STATUS_IS_OK(result
))
2521 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
2525 if (!NT_STATUS_IS_OK(result
))
2528 num_members
= rids
->count
;
2529 group_rids
= rids
->rids
;
2531 while (num_members
> 0) {
2532 int this_time
= 512;
2534 if (num_members
< this_time
)
2535 this_time
= num_members
;
2537 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
2544 if (!NT_STATUS_IS_OK(result
))
2547 /* We only have users as members, but make the output
2548 the same as the output of alias members */
2550 for (i
= 0; i
< this_time
; i
++) {
2552 if (c
->opt_long_list_entries
) {
2553 printf("%s-%d %s\\%s %d\n", sid_str
,
2554 group_rids
[i
], domain_name
,
2555 names
.names
[i
].string
,
2558 printf("%s\\%s\n", domain_name
,
2559 names
.names
[i
].string
);
2563 num_members
-= this_time
;
2567 return NT_STATUS_OK
;
2570 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2571 struct rpc_pipe_client
*pipe_hnd
,
2572 TALLOC_CTX
*mem_ctx
,
2573 struct policy_handle
*domain_pol
,
2577 struct rpc_pipe_client
*lsa_pipe
;
2578 struct policy_handle alias_pol
, lsa_pol
;
2580 DOM_SID
*alias_sids
;
2583 enum lsa_SidType
*types
;
2585 struct lsa_SidArray sid_array
;
2587 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2589 MAXIMUM_ALLOWED_ACCESS
,
2593 if (!NT_STATUS_IS_OK(result
))
2596 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
2600 if (!NT_STATUS_IS_OK(result
)) {
2601 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2605 num_members
= sid_array
.num_sids
;
2607 if (num_members
== 0) {
2608 return NT_STATUS_OK
;
2611 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2612 &ndr_table_lsarpc
.syntax_id
,
2614 if (!NT_STATUS_IS_OK(result
)) {
2615 d_fprintf(stderr
, _("Couldn't open LSA pipe. Error was %s\n"),
2616 nt_errstr(result
) );
2620 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2621 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
2623 if (!NT_STATUS_IS_OK(result
)) {
2624 d_fprintf(stderr
, _("Couldn't open LSA policy handle\n"));
2625 TALLOC_FREE(lsa_pipe
);
2629 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_members
);
2631 d_fprintf(stderr
, _("Out of memory\n"));
2632 TALLOC_FREE(lsa_pipe
);
2633 return NT_STATUS_NO_MEMORY
;
2636 for (i
=0; i
<num_members
; i
++) {
2637 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2640 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2641 num_members
, alias_sids
,
2642 &domains
, &names
, &types
);
2644 if (!NT_STATUS_IS_OK(result
) &&
2645 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2646 d_fprintf(stderr
, _("Couldn't lookup SIDs\n"));
2647 TALLOC_FREE(lsa_pipe
);
2651 for (i
= 0; i
< num_members
; i
++) {
2653 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2655 if (c
->opt_long_list_entries
) {
2656 printf("%s %s\\%s %d\n", sid_str
,
2657 domains
[i
] ? domains
[i
] : _("*unknown*"),
2658 names
[i
] ? names
[i
] : _("*unknown*"), types
[i
]);
2661 printf("%s\\%s\n", domains
[i
], names
[i
]);
2663 printf("%s\n", sid_str
);
2667 TALLOC_FREE(lsa_pipe
);
2668 return NT_STATUS_OK
;
2671 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
2672 const DOM_SID
*domain_sid
,
2673 const char *domain_name
,
2674 struct cli_state
*cli
,
2675 struct rpc_pipe_client
*pipe_hnd
,
2676 TALLOC_CTX
*mem_ctx
,
2681 struct policy_handle connect_pol
, domain_pol
;
2682 struct samr_Ids rids
, rid_types
;
2683 struct lsa_String lsa_acct_name
;
2685 /* Get sam policy handle */
2687 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2689 MAXIMUM_ALLOWED_ACCESS
,
2692 if (!NT_STATUS_IS_OK(result
))
2695 /* Get domain policy handle */
2697 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2699 MAXIMUM_ALLOWED_ACCESS
,
2700 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2703 if (!NT_STATUS_IS_OK(result
))
2706 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
2708 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2715 if (!NT_STATUS_IS_OK(result
)) {
2717 /* Ok, did not find it in the global sam, try with builtin */
2719 DOM_SID sid_Builtin
;
2721 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2723 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
2725 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2727 MAXIMUM_ALLOWED_ACCESS
,
2731 if (!NT_STATUS_IS_OK(result
)) {
2732 d_fprintf(stderr
, _("Couldn't find group %s\n"),
2737 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2744 if (!NT_STATUS_IS_OK(result
)) {
2745 d_fprintf(stderr
, _("Couldn't find group %s\n"),
2751 if (rids
.count
!= 1) {
2752 d_fprintf(stderr
, _("Couldn't find group %s\n"),
2757 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
2758 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
2759 domain_sid
, &domain_pol
,
2763 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
2764 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
2768 return NT_STATUS_NO_SUCH_GROUP
;
2771 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
2773 if (argc
!= 1 || c
->display_usage
) {
2774 return rpc_group_usage(c
, argc
, argv
);
2777 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2778 rpc_group_members_internals
,
2782 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
2784 NET_API_STATUS status
;
2785 struct GROUP_INFO_0 g0
;
2789 d_printf(_("Usage:\n"));
2790 d_printf("net rpc group rename group newname\n");
2794 g0
.grpi0_name
= argv
[1];
2796 status
= NetGroupSetInfo(c
->opt_host
,
2803 d_fprintf(stderr
, _("Renaming group %s failed with: %s\n"),
2804 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
2812 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
2814 if (argc
!= 2 || c
->display_usage
) {
2815 return rpc_group_usage(c
, argc
, argv
);
2818 return rpc_group_rename_internals(c
, argc
, argv
);
2822 * 'net rpc group' entrypoint.
2823 * @param argc Standard main() style argc.
2824 * @param argv Standard main() style argv. Initial components are already
2828 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
2830 NET_API_STATUS status
;
2832 struct functable func
[] = {
2837 N_("Create specified group"),
2838 N_("net rpc group add\n"
2839 " Create specified group")
2845 N_("Delete specified group"),
2846 N_("net rpc group delete\n"
2847 " Delete specified group")
2853 N_("Add member to group"),
2854 N_("net rpc group addmem\n"
2855 " Add member to group")
2861 N_("Remove member from group"),
2862 N_("net rpc group delmem\n"
2863 " Remove member from group")
2870 N_("net rpc group list\n"
2877 N_("List group members"),
2878 N_("net rpc group members\n"
2879 " List group members")
2886 N_("net rpc group rename\n"
2889 {NULL
, NULL
, 0, NULL
, NULL
}
2892 status
= libnetapi_init(&c
->netapi_ctx
);
2896 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
2897 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
2898 if (c
->opt_kerberos
) {
2899 libnetapi_set_use_kerberos(c
->netapi_ctx
);
2903 if (c
->display_usage
) {
2904 d_printf(_("Usage:\n"));
2905 d_printf(_("net rpc group\n"
2906 " Alias for net rpc group list global "
2907 "local builtin\n"));
2908 net_display_usage_from_functable(func
);
2912 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2913 rpc_group_list_internals
,
2917 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
2920 /****************************************************************************/
2922 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
2924 return net_share_usage(c
, argc
, argv
);
2928 * Add a share on a remote RPC server.
2930 * @param argc Standard main() style argc.
2931 * @param argv Standard main() style argv. Initial components are already
2934 * @return A shell status integer (0 for success).
2937 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
2939 NET_API_STATUS status
;
2942 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
2943 uint32 num_users
=0, perms
=0;
2944 char *password
=NULL
; /* don't allow a share password */
2945 struct SHARE_INFO_2 i2
;
2946 uint32_t parm_error
= 0;
2948 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
2949 return rpc_share_usage(c
, argc
, argv
);
2952 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
2956 path
= strchr(sharename
, '=');
2963 i2
.shi2_netname
= sharename
;
2964 i2
.shi2_type
= type
;
2965 i2
.shi2_remark
= c
->opt_comment
;
2966 i2
.shi2_permissions
= perms
;
2967 i2
.shi2_max_uses
= c
->opt_maxusers
;
2968 i2
.shi2_current_uses
= num_users
;
2969 i2
.shi2_path
= path
;
2970 i2
.shi2_passwd
= password
;
2972 status
= NetShareAdd(c
->opt_host
,
2977 printf(_("NetShareAdd failed with: %s\n"),
2978 libnetapi_get_error_string(c
->netapi_ctx
, status
));
2985 * Delete a share on a remote RPC server.
2987 * @param domain_sid The domain sid acquired from the remote server.
2988 * @param argc Standard main() style argc.
2989 * @param argv Standard main() style argv. Initial components are already
2992 * @return A shell status integer (0 for success).
2994 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
2996 if (argc
< 1 || c
->display_usage
) {
2997 return rpc_share_usage(c
, argc
, argv
);
3000 return NetShareDel(c
->opt_host
, argv
[0], 0);
3004 * Formatted print of share info
3006 * @param r pointer to SHARE_INFO_1 to format
3009 static void display_share_info_1(struct net_context
*c
,
3010 struct SHARE_INFO_1
*r
)
3012 if (c
->opt_long_list_entries
) {
3013 d_printf("%-12s %-8.8s %-50s\n",
3015 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
3018 d_printf("%s\n", r
->shi1_netname
);
3022 static WERROR
get_share_info(struct net_context
*c
,
3023 struct rpc_pipe_client
*pipe_hnd
,
3024 TALLOC_CTX
*mem_ctx
,
3028 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3032 union srvsvc_NetShareInfo info
;
3034 /* no specific share requested, enumerate all */
3037 uint32_t preferred_len
= 0xffffffff;
3038 uint32_t total_entries
= 0;
3039 uint32_t resume_handle
= 0;
3041 info_ctr
->level
= level
;
3043 status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, mem_ctx
,
3053 /* request just one share */
3054 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
3061 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
3066 ZERO_STRUCTP(info_ctr
);
3068 info_ctr
->level
= level
;
3073 struct srvsvc_NetShareCtr1
*ctr1
;
3075 ctr1
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr1
);
3076 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3079 ctr1
->array
= info
.info1
;
3081 info_ctr
->ctr
.ctr1
= ctr1
;
3085 struct srvsvc_NetShareCtr2
*ctr2
;
3087 ctr2
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr2
);
3088 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3091 ctr2
->array
= info
.info2
;
3093 info_ctr
->ctr
.ctr2
= ctr2
;
3097 struct srvsvc_NetShareCtr502
*ctr502
;
3099 ctr502
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr502
);
3100 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3103 ctr502
->array
= info
.info502
;
3105 info_ctr
->ctr
.ctr502
= ctr502
;
3113 * 'net rpc share list' entrypoint.
3114 * @param argc Standard main() style argc.
3115 * @param argv Standard main() style argv. Initial components are already
3118 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3120 NET_API_STATUS status
;
3121 struct SHARE_INFO_1
*i1
= NULL
;
3122 uint32_t entries_read
= 0;
3123 uint32_t total_entries
= 0;
3124 uint32_t resume_handle
= 0;
3125 uint32_t i
, level
= 1;
3127 if (c
->display_usage
) {
3129 "net rpc share list\n"
3132 _("List shares on remote server"));
3136 status
= NetShareEnum(c
->opt_host
,
3138 (uint8_t **)(void *)&i1
,
3147 /* Display results */
3149 if (c
->opt_long_list_entries
) {
3151 "\nEnumerating shared resources (exports) on remote server:\n\n"
3152 "\nShare name Type Description\n"
3153 "---------- ---- -----------\n"));
3155 for (i
= 0; i
< entries_read
; i
++)
3156 display_share_info_1(c
, &i1
[i
]);
3161 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3165 status
= cli_tcon_andx(cli
, netname
, "A:", "", 0);
3166 if (!NT_STATUS_IS_OK(status
)) {
3167 d_printf(_("skipping [%s]: not a file share.\n"), netname
);
3171 status
= cli_tdis(cli
);
3172 if (!NT_STATUS_IS_OK(status
)) {
3173 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status
));
3180 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3181 const char *netname
, uint32 type
)
3183 /* only support disk shares */
3184 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3185 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname
,
3190 /* skip builtin shares */
3191 /* FIXME: should print$ be added too ? */
3192 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3193 strequal(netname
,"global"))
3196 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3197 printf(_("excluding [%s]\n"), netname
);
3201 return check_share_availability(cli
, netname
);
3205 * Migrate shares from a remote RPC server to the local RPC server.
3207 * All parameters are provided by the run_rpc_command function, except for
3208 * argc, argv which are passed through.
3210 * @param domain_sid The domain sid acquired from the remote server.
3211 * @param cli A cli_state connected to the server.
3212 * @param mem_ctx Talloc context, destroyed on completion of the function.
3213 * @param argc Standard main() style argc.
3214 * @param argv Standard main() style argv. Initial components are already
3217 * @return Normal NTSTATUS return.
3220 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3221 const DOM_SID
*domain_sid
,
3222 const char *domain_name
,
3223 struct cli_state
*cli
,
3224 struct rpc_pipe_client
*pipe_hnd
,
3225 TALLOC_CTX
*mem_ctx
,
3230 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3231 struct srvsvc_NetShareInfoCtr ctr_src
;
3233 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3234 struct cli_state
*cli_dst
= NULL
;
3235 uint32 level
= 502; /* includes secdesc */
3236 uint32_t parm_error
= 0;
3238 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3240 if (!W_ERROR_IS_OK(result
))
3243 /* connect destination PI_SRVSVC */
3244 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3245 &ndr_table_srvsvc
.syntax_id
);
3246 if (!NT_STATUS_IS_OK(nt_status
))
3250 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3252 union srvsvc_NetShareInfo info
;
3253 struct srvsvc_NetShareInfo502 info502
=
3254 ctr_src
.ctr
.ctr502
->array
[i
];
3256 /* reset error-code */
3257 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3259 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3262 /* finally add the share on the dst server */
3264 printf(_("migrating: [%s], path: %s, comment: %s, without "
3266 info502
.name
, info502
.path
, info502
.comment
);
3268 info
.info502
= &info502
;
3270 nt_status
= rpccli_srvsvc_NetShareAdd(srvsvc_pipe
, mem_ctx
,
3271 srvsvc_pipe
->desthost
,
3277 if (W_ERROR_V(result
) == W_ERROR_V(WERR_FILE_EXISTS
)) {
3278 printf(_(" [%s] does already exist\n"),
3283 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3284 printf(_("cannot add share: %s\n"), win_errstr(result
));
3290 nt_status
= NT_STATUS_OK
;
3294 cli_shutdown(cli_dst
);
3302 * Migrate shares from a RPC server to another.
3304 * @param argc Standard main() style argc.
3305 * @param argv Standard main() style argv. Initial components are already
3308 * @return A shell status integer (0 for success).
3310 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3313 if (c
->display_usage
) {
3315 "net rpc share migrate shares\n"
3318 _("Migrate shares to local server"));
3323 printf(_("no server to migrate\n"));
3327 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3328 rpc_share_migrate_shares_internals
,
3335 * @param f file_info
3336 * @param mask current search mask
3337 * @param state arg-pointer
3340 static void copy_fn(const char *mnt
, file_info
*f
,
3341 const char *mask
, void *state
)
3343 static NTSTATUS nt_status
;
3344 static struct copy_clistate
*local_state
;
3345 static fstring filename
, new_mask
;
3348 struct net_context
*c
;
3350 local_state
= (struct copy_clistate
*)state
;
3351 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3355 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3358 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3361 if (f
->mode
& aDIR
) {
3363 DEBUG(3,("got dir: %s\n", f
->name
));
3365 fstrcpy(dir
, local_state
->cwd
);
3367 fstrcat(dir
, f
->name
);
3369 switch (net_mode_share
)
3371 case NET_MODE_SHARE_MIGRATE
:
3372 /* create that directory */
3373 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3374 local_state
->cli_share_src
,
3375 local_state
->cli_share_dst
,
3377 c
->opt_acls
? true : false,
3378 c
->opt_attrs
? true : false,
3379 c
->opt_timestamps
? true:false,
3383 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3387 if (!NT_STATUS_IS_OK(nt_status
))
3388 printf(_("could not handle dir %s: %s\n"),
3389 dir
, nt_errstr(nt_status
));
3391 /* search below that directory */
3392 fstrcpy(new_mask
, dir
);
3393 fstrcat(new_mask
, "\\*");
3395 old_dir
= local_state
->cwd
;
3396 local_state
->cwd
= dir
;
3397 if (!sync_files(local_state
, new_mask
))
3398 printf(_("could not handle files\n"));
3399 local_state
->cwd
= old_dir
;
3406 fstrcpy(filename
, local_state
->cwd
);
3407 fstrcat(filename
, "\\");
3408 fstrcat(filename
, f
->name
);
3410 DEBUG(3,("got file: %s\n", filename
));
3412 switch (net_mode_share
)
3414 case NET_MODE_SHARE_MIGRATE
:
3415 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3416 local_state
->cli_share_src
,
3417 local_state
->cli_share_dst
,
3419 c
->opt_acls
? true : false,
3420 c
->opt_attrs
? true : false,
3421 c
->opt_timestamps
? true: false,
3425 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3430 if (!NT_STATUS_IS_OK(nt_status
))
3431 printf(_("could not handle file %s: %s\n"),
3432 filename
, nt_errstr(nt_status
));
3437 * sync files, can be called recursivly to list files
3438 * and then call copy_fn for each file
3440 * @param cp_clistate pointer to the copy_clistate we work with
3441 * @param mask the current search mask
3443 * @return Boolean result
3445 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3447 struct cli_state
*targetcli
;
3448 char *targetpath
= NULL
;
3450 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3452 if ( !cli_resolve_path(talloc_tos(), "", NULL
, cp_clistate
->cli_share_src
,
3453 mask
, &targetcli
, &targetpath
) ) {
3454 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3456 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3460 if (cli_list(targetcli
, targetpath
, cp_clistate
->attribute
, copy_fn
, cp_clistate
) == -1) {
3461 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3462 mask
, cli_errstr(targetcli
));
3471 * Set the top level directory permissions before we do any further copies.
3472 * Should set up ACL inheritance.
3475 bool copy_top_level_perms(struct net_context
*c
,
3476 struct copy_clistate
*cp_clistate
,
3477 const char *sharename
)
3479 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3481 switch (net_mode_share
) {
3482 case NET_MODE_SHARE_MIGRATE
:
3483 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3484 nt_status
= net_copy_fileattr(c
,
3485 cp_clistate
->mem_ctx
,
3486 cp_clistate
->cli_share_src
,
3487 cp_clistate
->cli_share_dst
,
3489 c
->opt_acls
? true : false,
3490 c
->opt_attrs
? true : false,
3491 c
->opt_timestamps
? true: false,
3495 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3499 if (!NT_STATUS_IS_OK(nt_status
)) {
3500 printf(_("Could handle directory attributes for top level "
3501 "directory of share %s. Error %s\n"),
3502 sharename
, nt_errstr(nt_status
));
3510 * Sync all files inside a remote share to another share (over smb).
3512 * All parameters are provided by the run_rpc_command function, except for
3513 * argc, argv which are passed through.
3515 * @param domain_sid The domain sid acquired from the remote server.
3516 * @param cli A cli_state connected to the server.
3517 * @param mem_ctx Talloc context, destroyed on completion of the function.
3518 * @param argc Standard main() style argc.
3519 * @param argv Standard main() style argv. Initial components are already
3522 * @return Normal NTSTATUS return.
3525 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3526 const DOM_SID
*domain_sid
,
3527 const char *domain_name
,
3528 struct cli_state
*cli
,
3529 struct rpc_pipe_client
*pipe_hnd
,
3530 TALLOC_CTX
*mem_ctx
,
3535 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3536 struct srvsvc_NetShareInfoCtr ctr_src
;
3539 struct copy_clistate cp_clistate
;
3540 bool got_src_share
= false;
3541 bool got_dst_share
= false;
3542 const char *mask
= "\\*";
3545 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3547 nt_status
= NT_STATUS_NO_MEMORY
;
3551 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3554 if (!W_ERROR_IS_OK(result
))
3557 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3559 struct srvsvc_NetShareInfo502 info502
=
3560 ctr_src
.ctr
.ctr502
->array
[i
];
3562 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3565 /* one might not want to mirror whole discs :) */
3566 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3567 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3572 switch (net_mode_share
)
3574 case NET_MODE_SHARE_MIGRATE
:
3578 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3582 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3585 c
->opt_acls
? _("including") : _("without"),
3586 c
->opt_attrs
? _("including") : _("without"),
3587 c
->opt_timestamps
? _("(preserving timestamps)") : "");
3589 cp_clistate
.mem_ctx
= mem_ctx
;
3590 cp_clistate
.cli_share_src
= NULL
;
3591 cp_clistate
.cli_share_dst
= NULL
;
3592 cp_clistate
.cwd
= NULL
;
3593 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3596 /* open share source */
3597 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3598 &cli
->dest_ss
, cli
->desthost
,
3599 info502
.name
, "A:");
3600 if (!NT_STATUS_IS_OK(nt_status
))
3603 got_src_share
= true;
3605 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3606 /* open share destination */
3607 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3608 NULL
, dst
, info502
.name
, "A:");
3609 if (!NT_STATUS_IS_OK(nt_status
))
3612 got_dst_share
= true;
3615 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3616 d_fprintf(stderr
, _("Could not handle the top level "
3617 "directory permissions for the "
3618 "share: %s\n"), info502
.name
);
3619 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3623 if (!sync_files(&cp_clistate
, mask
)) {
3624 d_fprintf(stderr
, _("could not handle files for share: "
3625 "%s\n"), info502
.name
);
3626 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3631 nt_status
= NT_STATUS_OK
;
3636 cli_shutdown(cp_clistate
.cli_share_src
);
3639 cli_shutdown(cp_clistate
.cli_share_dst
);
3646 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
3648 if (c
->display_usage
) {
3650 "net share migrate files\n"
3653 _("Migrate files to local server"));
3658 d_printf(_("no server to migrate\n"));
3662 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3663 rpc_share_migrate_files_internals
,
3668 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3670 * All parameters are provided by the run_rpc_command function, except for
3671 * argc, argv which are passed through.
3673 * @param domain_sid The domain sid acquired from the remote server.
3674 * @param cli A cli_state connected to the server.
3675 * @param mem_ctx Talloc context, destroyed on completion of the function.
3676 * @param argc Standard main() style argc.
3677 * @param argv Standard main() style argv. Initial components are already
3680 * @return Normal NTSTATUS return.
3683 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
3684 const DOM_SID
*domain_sid
,
3685 const char *domain_name
,
3686 struct cli_state
*cli
,
3687 struct rpc_pipe_client
*pipe_hnd
,
3688 TALLOC_CTX
*mem_ctx
,
3693 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3694 struct srvsvc_NetShareInfoCtr ctr_src
;
3695 union srvsvc_NetShareInfo info
;
3697 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3698 struct cli_state
*cli_dst
= NULL
;
3699 uint32 level
= 502; /* includes secdesc */
3700 uint32_t parm_error
= 0;
3702 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3705 if (!W_ERROR_IS_OK(result
))
3708 /* connect destination PI_SRVSVC */
3709 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3710 &ndr_table_srvsvc
.syntax_id
);
3711 if (!NT_STATUS_IS_OK(nt_status
))
3715 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3717 struct srvsvc_NetShareInfo502 info502
=
3718 ctr_src
.ctr
.ctr502
->array
[i
];
3720 /* reset error-code */
3721 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3723 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3726 printf(_("migrating: [%s], path: %s, comment: %s, including "
3728 info502
.name
, info502
.path
, info502
.comment
);
3731 display_sec_desc(info502
.sd_buf
.sd
);
3733 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3734 info
.info502
= &info502
;
3736 /* finally modify the share on the dst server */
3737 nt_status
= rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe
, mem_ctx
,
3738 srvsvc_pipe
->desthost
,
3744 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3745 printf(_("cannot set share-acl: %s\n"),
3746 win_errstr(result
));
3752 nt_status
= NT_STATUS_OK
;
3756 cli_shutdown(cli_dst
);
3764 * Migrate share-acls from a RPC server to another.
3766 * @param argc Standard main() style argc.
3767 * @param argv Standard main() style argv. Initial components are already
3770 * @return A shell status integer (0 for success).
3772 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
3775 if (c
->display_usage
) {
3777 "net rpc share migrate security\n"
3780 _("Migrate share-acls to local server"));
3785 d_printf(_("no server to migrate\n"));
3789 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3790 rpc_share_migrate_security_internals
,
3795 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3796 * from one server to another.
3798 * @param argc Standard main() style argc.
3799 * @param argv Standard main() style argv. Initial components are already
3802 * @return A shell status integer (0 for success).
3805 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
3810 if (c
->display_usage
) {
3812 "net rpc share migrate all\n"
3815 _("Migrates shares including all share settings"));
3820 d_printf(_("no server to migrate\n"));
3824 /* order is important. we don't want to be locked out by the share-acl
3825 * before copying files - gd */
3827 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3828 rpc_share_migrate_shares_internals
, argc
, argv
);
3832 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3833 rpc_share_migrate_files_internals
, argc
, argv
);
3837 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3838 rpc_share_migrate_security_internals
, argc
,
3844 * 'net rpc share migrate' entrypoint.
3845 * @param argc Standard main() style argc.
3846 * @param argv Standard main() style argv. Initial components are already
3849 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
3852 struct functable func
[] = {
3855 rpc_share_migrate_all
,
3857 N_("Migrate shares from remote to local server"),
3858 N_("net rpc share migrate all\n"
3859 " Migrate shares from remote to local server")
3863 rpc_share_migrate_files
,
3865 N_("Migrate files from remote to local server"),
3866 N_("net rpc share migrate files\n"
3867 " Migrate files from remote to local server")
3871 rpc_share_migrate_security
,
3873 N_("Migrate share-ACLs from remote to local server"),
3874 N_("net rpc share migrate security\n"
3875 " Migrate share-ACLs from remote to local server")
3879 rpc_share_migrate_shares
,
3881 N_("Migrate shares from remote to local server"),
3882 N_("net rpc share migrate shares\n"
3883 " Migrate shares from remote to local server")
3885 {NULL
, NULL
, 0, NULL
, NULL
}
3888 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
3890 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
3899 static int num_server_aliases
;
3900 static struct full_alias
*server_aliases
;
3903 * Add an alias to the static list.
3905 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
3907 if (server_aliases
== NULL
)
3908 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
3910 server_aliases
[num_server_aliases
] = *alias
;
3911 num_server_aliases
+= 1;
3915 * For a specific domain on the server, fetch all the aliases
3916 * and their members. Add all of them to the server_aliases.
3919 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
3920 TALLOC_CTX
*mem_ctx
,
3921 struct policy_handle
*connect_pol
,
3922 const DOM_SID
*domain_sid
)
3924 uint32 start_idx
, max_entries
, num_entries
, i
;
3925 struct samr_SamArray
*groups
= NULL
;
3927 struct policy_handle domain_pol
;
3929 /* Get domain policy handle */
3931 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
3933 MAXIMUM_ALLOWED_ACCESS
,
3934 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
3936 if (!NT_STATUS_IS_OK(result
))
3943 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
3949 for (i
= 0; i
< num_entries
; i
++) {
3951 struct policy_handle alias_pol
;
3952 struct full_alias alias
;
3953 struct lsa_SidArray sid_array
;
3956 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
3958 MAXIMUM_ALLOWED_ACCESS
,
3959 groups
->entries
[i
].idx
,
3961 if (!NT_STATUS_IS_OK(result
))
3964 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
3967 if (!NT_STATUS_IS_OK(result
))
3970 alias
.num_members
= sid_array
.num_sids
;
3972 result
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &alias_pol
);
3973 if (!NT_STATUS_IS_OK(result
))
3976 alias
.members
= NULL
;
3978 if (alias
.num_members
> 0) {
3979 alias
.members
= SMB_MALLOC_ARRAY(DOM_SID
, alias
.num_members
);
3981 for (j
= 0; j
< alias
.num_members
; j
++)
3982 sid_copy(&alias
.members
[j
],
3983 sid_array
.sids
[j
].sid
);
3986 sid_compose(&alias
.sid
, domain_sid
,
3987 groups
->entries
[i
].idx
);
3989 push_alias(mem_ctx
, &alias
);
3991 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
3993 result
= NT_STATUS_OK
;
3996 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
4002 * Dump server_aliases as names for debugging purposes.
4005 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4006 const DOM_SID
*domain_sid
,
4007 const char *domain_name
,
4008 struct cli_state
*cli
,
4009 struct rpc_pipe_client
*pipe_hnd
,
4010 TALLOC_CTX
*mem_ctx
,
4016 struct policy_handle lsa_pol
;
4018 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4019 SEC_FLAG_MAXIMUM_ALLOWED
,
4021 if (!NT_STATUS_IS_OK(result
))
4024 for (i
=0; i
<num_server_aliases
; i
++) {
4027 enum lsa_SidType
*types
;
4030 struct full_alias
*alias
= &server_aliases
[i
];
4032 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4034 &domains
, &names
, &types
);
4035 if (!NT_STATUS_IS_OK(result
))
4038 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4040 if (alias
->num_members
== 0) {
4045 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4048 &domains
, &names
, &types
);
4050 if (!NT_STATUS_IS_OK(result
) &&
4051 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4054 for (j
=0; j
<alias
->num_members
; j
++)
4055 DEBUG(1, ("%s\\%s (%d); ",
4056 domains
[j
] ? domains
[j
] : "*unknown*",
4057 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4061 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
4063 return NT_STATUS_OK
;
4067 * Fetch a list of all server aliases and their members into
4071 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4072 const DOM_SID
*domain_sid
,
4073 const char *domain_name
,
4074 struct cli_state
*cli
,
4075 struct rpc_pipe_client
*pipe_hnd
,
4076 TALLOC_CTX
*mem_ctx
,
4081 struct policy_handle connect_pol
;
4083 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
4085 MAXIMUM_ALLOWED_ACCESS
,
4088 if (!NT_STATUS_IS_OK(result
))
4091 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4092 &global_sid_Builtin
);
4094 if (!NT_STATUS_IS_OK(result
))
4097 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4100 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
4105 static void init_user_token(NT_USER_TOKEN
*token
, DOM_SID
*user_sid
)
4107 token
->num_sids
= 4;
4109 if (!(token
->user_sids
= SMB_MALLOC_ARRAY(DOM_SID
, 4))) {
4110 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4111 token
->num_sids
= 0;
4115 token
->user_sids
[0] = *user_sid
;
4116 sid_copy(&token
->user_sids
[1], &global_sid_World
);
4117 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
4118 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
4121 static void free_user_token(NT_USER_TOKEN
*token
)
4123 SAFE_FREE(token
->user_sids
);
4126 static void add_sid_to_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4128 if (is_sid_in_token(token
, sid
))
4131 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, DOM_SID
, token
->num_sids
+1);
4132 if (!token
->user_sids
) {
4136 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
4138 token
->num_sids
+= 1;
4143 NT_USER_TOKEN token
;
4146 static void dump_user_token(struct user_token
*token
)
4150 d_printf("%s\n", token
->name
);
4152 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4153 d_printf(" %s\n", sid_string_tos(&token
->token
.user_sids
[i
]));
4157 static bool is_alias_member(DOM_SID
*sid
, struct full_alias
*alias
)
4161 for (i
=0; i
<alias
->num_members
; i
++) {
4162 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
4169 static void collect_sid_memberships(NT_USER_TOKEN
*token
, DOM_SID sid
)
4173 for (i
=0; i
<num_server_aliases
; i
++) {
4174 if (is_alias_member(&sid
, &server_aliases
[i
]))
4175 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4180 * We got a user token with all the SIDs we can know about without asking the
4181 * server directly. These are the user and domain group sids. All of these can
4182 * be members of aliases. So scan the list of aliases for each of the SIDs and
4183 * add them to the token.
4186 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
4188 int num_global_sids
= token
->num_sids
;
4191 for (i
=0; i
<num_global_sids
; i
++) {
4192 collect_sid_memberships(token
, token
->user_sids
[i
]);
4196 static bool get_user_sids(const char *domain
, const char *user
, NT_USER_TOKEN
*token
)
4198 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4199 enum wbcSidType type
;
4201 struct wbcDomainSid wsid
;
4202 char *sid_str
= NULL
;
4204 uint32_t num_groups
;
4205 gid_t
*groups
= NULL
;
4208 fstr_sprintf(full_name
, "%s%c%s",
4209 domain
, *lp_winbind_separator(), user
);
4211 /* First let's find out the user sid */
4213 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4215 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4216 DEBUG(1, ("winbind could not find %s: %s\n",
4217 full_name
, wbcErrorString(wbc_status
)));
4221 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4222 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4226 if (type
!= SID_NAME_USER
) {
4227 wbcFreeMemory(sid_str
);
4228 DEBUG(1, ("%s is not a user\n", full_name
));
4232 if (!string_to_sid(&user_sid
, sid_str
)) {
4233 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4237 wbcFreeMemory(sid_str
);
4240 init_user_token(token
, &user_sid
);
4242 /* And now the groups winbind knows about */
4244 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4245 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4246 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4247 full_name
, wbcErrorString(wbc_status
)));
4251 for (i
= 0; i
< num_groups
; i
++) {
4252 gid_t gid
= groups
[i
];
4255 wbc_status
= wbcGidToSid(gid
, &wsid
);
4256 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4257 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4258 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4259 wbcFreeMemory(groups
);
4263 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4264 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4265 wbcFreeMemory(groups
);
4269 DEBUG(3, (" %s\n", sid_str
));
4271 string_to_sid(&sid
, sid_str
);
4272 wbcFreeMemory(sid_str
);
4275 add_sid_to_token(token
, &sid
);
4277 wbcFreeMemory(groups
);
4283 * Get a list of all user tokens we want to look at
4286 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4287 struct user_token
**user_tokens
)
4289 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4290 uint32_t i
, num_users
;
4292 struct user_token
*result
;
4293 TALLOC_CTX
*frame
= NULL
;
4295 if (lp_winbind_use_default_domain() &&
4296 (c
->opt_target_workgroup
== NULL
)) {
4297 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4298 "please specify a workgroup\n"));
4302 /* Send request to winbind daemon */
4304 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4305 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4306 DEBUG(1, (_("winbind could not list users: %s\n"),
4307 wbcErrorString(wbc_status
)));
4311 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4313 if (result
== NULL
) {
4314 DEBUG(1, ("Could not malloc sid array\n"));
4315 wbcFreeMemory(users
);
4319 frame
= talloc_stackframe();
4320 for (i
=0; i
< num_users
; i
++) {
4321 fstring domain
, user
;
4324 fstrcpy(result
[i
].name
, users
[i
]);
4326 p
= strchr(users
[i
], *lp_winbind_separator());
4328 DEBUG(3, ("%s\n", users
[i
]));
4331 fstrcpy(domain
, c
->opt_target_workgroup
);
4332 fstrcpy(user
, users
[i
]);
4335 fstrcpy(domain
, users
[i
]);
4340 get_user_sids(domain
, user
, &(result
[i
].token
));
4344 wbcFreeMemory(users
);
4346 *num_tokens
= num_users
;
4347 *user_tokens
= result
;
4352 static bool get_user_tokens_from_file(FILE *f
,
4354 struct user_token
**tokens
)
4356 struct user_token
*token
= NULL
;
4361 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4365 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4366 line
[strlen(line
)-1] = '\0';
4369 if (line
[0] == ' ') {
4373 if(!string_to_sid(&sid
, &line
[1])) {
4374 DEBUG(1,("get_user_tokens_from_file: Could "
4375 "not convert sid %s \n",&line
[1]));
4379 if (token
== NULL
) {
4380 DEBUG(0, ("File does not begin with username"));
4384 add_sid_to_token(&token
->token
, &sid
);
4388 /* And a new user... */
4391 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4392 if (*tokens
== NULL
) {
4393 DEBUG(0, ("Could not realloc tokens\n"));
4397 token
= &((*tokens
)[*num_tokens
-1]);
4399 fstrcpy(token
->name
, line
);
4400 token
->token
.num_sids
= 0;
4401 token
->token
.user_sids
= NULL
;
4410 * Show the list of all users that have access to a share
4413 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4414 TALLOC_CTX
*mem_ctx
,
4415 const char *netname
,
4417 struct user_token
*tokens
)
4420 SEC_DESC
*share_sd
= NULL
;
4421 SEC_DESC
*root_sd
= NULL
;
4422 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4424 union srvsvc_NetShareInfo info
;
4429 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4436 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4437 DEBUG(1, ("Coult not query secdesc for share %s\n",
4442 share_sd
= info
.info502
->sd_buf
.sd
;
4443 if (share_sd
== NULL
) {
4444 DEBUG(1, ("Got no secdesc for share %s\n",
4450 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli
, netname
, "A:", "", 0))) {
4454 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4455 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4456 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4459 for (i
=0; i
<num_tokens
; i
++) {
4462 if (share_sd
!= NULL
) {
4463 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4466 if (!NT_STATUS_IS_OK(status
)) {
4467 DEBUG(1, ("Could not check share_sd for "
4474 if (root_sd
== NULL
) {
4475 d_printf(" %s\n", tokens
[i
].name
);
4479 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4481 if (!NT_STATUS_IS_OK(status
)) {
4482 DEBUG(1, ("Could not check root_sd for user %s\n",
4486 d_printf(" %s\n", tokens
[i
].name
);
4489 if (fnum
!= (uint16_t)-1)
4490 cli_close(cli
, fnum
);
4502 static void collect_share(const char *name
, uint32 m
,
4503 const char *comment
, void *state
)
4505 struct share_list
*share_list
= (struct share_list
*)state
;
4507 if (m
!= STYPE_DISKTREE
)
4510 share_list
->num_shares
+= 1;
4511 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4512 if (!share_list
->shares
) {
4513 share_list
->num_shares
= 0;
4516 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4520 * List shares on a remote RPC server, including the security descriptors.
4522 * All parameters are provided by the run_rpc_command function, except for
4523 * argc, argv which are passed through.
4525 * @param domain_sid The domain sid acquired from the remote server.
4526 * @param cli A cli_state connected to the server.
4527 * @param mem_ctx Talloc context, destroyed on completion of the function.
4528 * @param argc Standard main() style argc.
4529 * @param argv Standard main() style argv. Initial components are already
4532 * @return Normal NTSTATUS return.
4535 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4536 const DOM_SID
*domain_sid
,
4537 const char *domain_name
,
4538 struct cli_state
*cli
,
4539 struct rpc_pipe_client
*pipe_hnd
,
4540 TALLOC_CTX
*mem_ctx
,
4549 struct user_token
*tokens
= NULL
;
4552 struct share_list share_list
;
4557 f
= fopen(argv
[0], "r");
4561 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4562 return NT_STATUS_UNSUCCESSFUL
;
4565 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4571 DEBUG(0, ("Could not read users from file\n"));
4572 return NT_STATUS_UNSUCCESSFUL
;
4575 for (i
=0; i
<num_tokens
; i
++)
4576 collect_alias_memberships(&tokens
[i
].token
);
4578 share_list
.num_shares
= 0;
4579 share_list
.shares
= NULL
;
4581 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4584 DEBUG(0, ("Error returning browse list: %s\n",
4589 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4590 char *netname
= share_list
.shares
[i
];
4592 if (netname
[strlen(netname
)-1] == '$')
4595 d_printf("%s\n", netname
);
4597 show_userlist(pipe_hnd
, mem_ctx
, netname
,
4598 num_tokens
, tokens
);
4601 for (i
=0; i
<num_tokens
; i
++) {
4602 free_user_token(&tokens
[i
].token
);
4605 SAFE_FREE(share_list
.shares
);
4607 return NT_STATUS_OK
;
4610 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
4615 if (c
->display_usage
) {
4617 "net rpc share allowedusers\n"
4620 _("List allowed users"));
4624 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
4625 rpc_aliaslist_internals
,
4630 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
.syntax_id
, 0,
4636 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4637 rpc_share_allowedusers_internals
,
4641 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
4644 struct user_token
*tokens
= NULL
;
4648 net_usersidlist_usage(c
, argc
, argv
);
4652 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
4653 DEBUG(0, ("Could not get the user/sid list\n"));
4657 for (i
=0; i
<num_tokens
; i
++) {
4658 dump_user_token(&tokens
[i
]);
4659 free_user_token(&tokens
[i
].token
);
4666 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
4668 d_printf(_("net usersidlist\n"
4669 "\tprints out a list of all users the running winbind knows\n"
4670 "\tabout, together with all their SIDs. This is used as\n"
4671 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4673 net_common_flags_usage(c
, argc
, argv
);
4678 * 'net rpc share' entrypoint.
4679 * @param argc Standard main() style argc.
4680 * @param argv Standard main() style argv. Initial components are already
4684 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
4686 NET_API_STATUS status
;
4688 struct functable func
[] = {
4694 N_("net rpc share add\n"
4702 N_("net rpc share delete\n"
4707 rpc_share_allowedusers
,
4709 N_("Modify allowed users"),
4710 N_("net rpc share allowedusers\n"
4711 " Modify allowed users")
4717 N_("Migrate share to local server"),
4718 N_("net rpc share migrate\n"
4719 " Migrate share to local server")
4726 N_("net rpc share list\n"
4729 {NULL
, NULL
, 0, NULL
, NULL
}
4732 status
= libnetapi_init(&c
->netapi_ctx
);
4736 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
4737 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
4738 if (c
->opt_kerberos
) {
4739 libnetapi_set_use_kerberos(c
->netapi_ctx
);
4743 if (c
->display_usage
) {
4748 " Alias for net rpc share list\n"));
4749 net_display_usage_from_functable(func
);
4753 return rpc_share_list(c
, argc
, argv
);
4756 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
4759 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
4760 TALLOC_CTX
*mem_ctx
,
4761 struct rpc_sh_ctx
*ctx
,
4762 struct rpc_pipe_client
*pipe_hnd
,
4763 int argc
, const char **argv
)
4766 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
4769 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
4770 TALLOC_CTX
*mem_ctx
,
4771 struct rpc_sh_ctx
*ctx
,
4772 struct rpc_pipe_client
*pipe_hnd
,
4773 int argc
, const char **argv
)
4775 NET_API_STATUS status
;
4776 uint32_t parm_err
= 0;
4777 struct SHARE_INFO_2 i2
;
4779 if ((argc
< 2) || (argc
> 3)) {
4780 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
4782 return NT_STATUS_INVALID_PARAMETER
;
4785 i2
.shi2_netname
= argv
[0];
4786 i2
.shi2_type
= STYPE_DISKTREE
;
4787 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
4788 i2
.shi2_permissions
= 0;
4789 i2
.shi2_max_uses
= 0;
4790 i2
.shi2_current_uses
= 0;
4791 i2
.shi2_path
= argv
[1];
4792 i2
.shi2_passwd
= NULL
;
4794 status
= NetShareAdd(pipe_hnd
->desthost
,
4799 return werror_to_ntstatus(W_ERROR(status
));
4802 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
4803 TALLOC_CTX
*mem_ctx
,
4804 struct rpc_sh_ctx
*ctx
,
4805 struct rpc_pipe_client
*pipe_hnd
,
4806 int argc
, const char **argv
)
4809 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
4810 return NT_STATUS_INVALID_PARAMETER
;
4813 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
4816 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
4817 TALLOC_CTX
*mem_ctx
,
4818 struct rpc_sh_ctx
*ctx
,
4819 struct rpc_pipe_client
*pipe_hnd
,
4820 int argc
, const char **argv
)
4822 union srvsvc_NetShareInfo info
;
4827 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
4828 return NT_STATUS_INVALID_PARAMETER
;
4831 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4837 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4841 d_printf(_("Name: %s\n"), info
.info2
->name
);
4842 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
4843 d_printf(_("Path: %s\n"), info
.info2
->path
);
4844 d_printf(_("Password: %s\n"), info
.info2
->password
);
4847 return werror_to_ntstatus(result
);
4850 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
4851 struct rpc_sh_ctx
*ctx
)
4853 static struct rpc_sh_cmd cmds
[] = {
4855 { "list", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_list
,
4856 N_("List available shares") },
4858 { "add", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_add
,
4859 N_("Add a share") },
4861 { "delete", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_delete
,
4862 N_("Delete a share") },
4864 { "info", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_info
,
4865 N_("Get information about a share") },
4867 { NULL
, NULL
, 0, NULL
, NULL
}
4873 /****************************************************************************/
4875 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
4877 return net_file_usage(c
, argc
, argv
);
4881 * Close a file on a remote RPC server.
4883 * @param argc Standard main() style argc.
4884 * @param argv Standard main() style argv. Initial components are already
4887 * @return A shell status integer (0 for success).
4889 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
4891 if (argc
< 1 || c
->display_usage
) {
4892 return rpc_file_usage(c
, argc
, argv
);
4895 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
4899 * Formatted print of open file info
4901 * @param r struct FILE_INFO_3 contents
4904 static void display_file_info_3(struct FILE_INFO_3
*r
)
4906 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4907 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
4908 r
->fi3_num_locks
, r
->fi3_pathname
);
4912 * List files for a user on a remote RPC server.
4914 * @param argc Standard main() style argc.
4915 * @param argv Standard main() style argv. Initial components are already
4918 * @return A shell status integer (0 for success)..
4921 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
4923 NET_API_STATUS status
;
4924 uint32 preferred_len
= 0xffffffff, i
;
4925 const char *username
=NULL
;
4926 uint32_t total_entries
= 0;
4927 uint32_t entries_read
= 0;
4928 uint32_t resume_handle
= 0;
4929 struct FILE_INFO_3
*i3
= NULL
;
4931 if (c
->display_usage
) {
4932 return rpc_file_usage(c
, argc
, argv
);
4935 /* if argc > 0, must be user command */
4937 username
= smb_xstrdup(argv
[0]);
4940 status
= NetFileEnum(c
->opt_host
,
4944 (uint8_t **)(void *)&i3
,
4954 /* Display results */
4957 "\nEnumerating open files on remote server:\n\n"
4958 "\nFileId Opened by Perms Locks Path"
4959 "\n------ --------- ----- ----- ---- \n"));
4960 for (i
= 0; i
< entries_read
; i
++) {
4961 display_file_info_3(&i3
[i
]);
4968 * 'net rpc file' entrypoint.
4969 * @param argc Standard main() style argc.
4970 * @param argv Standard main() style argv. Initial components are already
4974 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
4976 NET_API_STATUS status
;
4978 struct functable func
[] = {
4983 N_("Close opened file"),
4984 N_("net rpc file close\n"
4985 " Close opened file")
4991 N_("List files opened by user"),
4992 N_("net rpc file user\n"
4993 " List files opened by user")
5000 N_("Display information about opened file"),
5001 N_("net rpc file info\n"
5002 " Display information about opened file")
5005 {NULL
, NULL
, 0, NULL
, NULL
}
5008 status
= libnetapi_init(&c
->netapi_ctx
);
5012 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5013 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5014 if (c
->opt_kerberos
) {
5015 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5019 if (c
->display_usage
) {
5020 d_printf(_("Usage:\n"));
5021 d_printf(_("net rpc file\n"
5022 " List opened files\n"));
5023 net_display_usage_from_functable(func
);
5027 return rpc_file_user(c
, argc
, argv
);
5030 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5034 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5036 * All parameters are provided by the run_rpc_command function, except for
5037 * argc, argv which are passed through.
5039 * @param c A net_context structure.
5040 * @param domain_sid The domain sid acquired from the remote server.
5041 * @param cli A cli_state connected to the server.
5042 * @param mem_ctx Talloc context, destroyed on completion of the function.
5043 * @param argc Standard main() style argc.
5044 * @param argv Standard main() style argv. Initial components are already
5047 * @return Normal NTSTATUS return.
5050 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5051 const DOM_SID
*domain_sid
,
5052 const char *domain_name
,
5053 struct cli_state
*cli
,
5054 struct rpc_pipe_client
*pipe_hnd
,
5055 TALLOC_CTX
*mem_ctx
,
5059 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5061 result
= rpccli_initshutdown_Abort(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5063 if (NT_STATUS_IS_OK(result
)) {
5064 d_printf(_("\nShutdown successfully aborted\n"));
5065 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5067 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5073 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5075 * All parameters are provided by the run_rpc_command function, except for
5076 * argc, argv which are passed through.
5078 * @param c A net_context structure.
5079 * @param domain_sid The domain sid acquired from the remote server.
5080 * @param cli A cli_state connected to the server.
5081 * @param mem_ctx Talloc context, destroyed on completion of the function.
5082 * @param argc Standard main() style argc.
5083 * @param argv Standard main() style argv. Initial components are already
5086 * @return Normal NTSTATUS return.
5089 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5090 const DOM_SID
*domain_sid
,
5091 const char *domain_name
,
5092 struct cli_state
*cli
,
5093 struct rpc_pipe_client
*pipe_hnd
,
5094 TALLOC_CTX
*mem_ctx
,
5098 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5100 result
= rpccli_winreg_AbortSystemShutdown(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5102 if (NT_STATUS_IS_OK(result
)) {
5103 d_printf(_("\nShutdown successfully aborted\n"));
5104 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5106 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5112 * ABORT the shutdown of a remote RPC server.
5114 * @param argc Standard main() style argc.
5115 * @param argv Standard main() style argv. Initial components are already
5118 * @return A shell status integer (0 for success).
5121 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5126 if (c
->display_usage
) {
5128 "net rpc abortshutdown\n"
5131 _("Abort a scheduled shutdown"));
5135 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5136 rpc_shutdown_abort_internals
, argc
, argv
);
5141 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5143 return run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5144 rpc_reg_shutdown_abort_internals
,
5149 * Shut down a remote RPC Server via initshutdown pipe.
5151 * All parameters are provided by the run_rpc_command function, except for
5152 * argc, argv which are passed through.
5154 * @param c A net_context structure.
5155 * @param domain_sid The domain sid acquired from the remote server.
5156 * @param cli A cli_state connected to the server.
5157 * @param mem_ctx Talloc context, destroyed on completion of the function.
5158 * @param argc Standard main() style argc.
5159 * @param argv Standard main() style argv. Initial components are already
5162 * @return Normal NTSTATUS return.
5165 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5166 const DOM_SID
*domain_sid
,
5167 const char *domain_name
,
5168 struct cli_state
*cli
,
5169 struct rpc_pipe_client
*pipe_hnd
,
5170 TALLOC_CTX
*mem_ctx
,
5174 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5175 const char *msg
= N_("This machine will be shutdown shortly");
5176 uint32 timeout
= 20;
5177 struct lsa_StringLarge msg_string
;
5179 if (c
->opt_comment
) {
5180 msg
= c
->opt_comment
;
5182 if (c
->opt_timeout
) {
5183 timeout
= c
->opt_timeout
;
5186 msg_string
.string
= msg
;
5188 /* create an entry */
5189 result
= rpccli_initshutdown_Init(pipe_hnd
, mem_ctx
, NULL
,
5190 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5193 if (NT_STATUS_IS_OK(result
)) {
5194 d_printf(_("\nShutdown of remote machine succeeded\n"));
5195 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5197 DEBUG(1,("Shutdown of remote machine failed!\n"));
5203 * Shut down a remote RPC Server via winreg pipe.
5205 * All parameters are provided by the run_rpc_command function, except for
5206 * argc, argv which are passed through.
5208 * @param c A net_context structure.
5209 * @param domain_sid The domain sid acquired from the remote server.
5210 * @param cli A cli_state connected to the server.
5211 * @param mem_ctx Talloc context, destroyed on completion of the function.
5212 * @param argc Standard main() style argc.
5213 * @param argv Standard main() style argv. Initial components are already
5216 * @return Normal NTSTATUS return.
5219 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5220 const DOM_SID
*domain_sid
,
5221 const char *domain_name
,
5222 struct cli_state
*cli
,
5223 struct rpc_pipe_client
*pipe_hnd
,
5224 TALLOC_CTX
*mem_ctx
,
5228 const char *msg
= N_("This machine will be shutdown shortly");
5229 uint32 timeout
= 20;
5230 struct lsa_StringLarge msg_string
;
5234 if (c
->opt_comment
) {
5235 msg
= c
->opt_comment
;
5237 msg_string
.string
= msg
;
5239 if (c
->opt_timeout
) {
5240 timeout
= c
->opt_timeout
;
5243 /* create an entry */
5244 result
= rpccli_winreg_InitiateSystemShutdown(pipe_hnd
, mem_ctx
, NULL
,
5245 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5248 if (NT_STATUS_IS_OK(result
)) {
5249 d_printf(_("\nShutdown of remote machine succeeded\n"));
5251 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5252 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5253 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5255 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5262 * Shut down a remote RPC server.
5264 * @param argc Standard main() style argc.
5265 * @param argv Standard main() style argv. Initial components are already
5268 * @return A shell status integer (0 for success).
5271 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5275 if (c
->display_usage
) {
5277 "net rpc shutdown\n"
5280 _("Shut down a remote RPC server"));
5284 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5285 rpc_init_shutdown_internals
, argc
, argv
);
5288 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5289 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5290 rpc_reg_shutdown_internals
, argc
, argv
);
5296 /***************************************************************************
5297 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5298 ***************************************************************************/
5301 * Add interdomain trust account to the RPC server.
5302 * All parameters (except for argc and argv) are passed by run_rpc_command
5305 * @param c A net_context structure.
5306 * @param domain_sid The domain sid acquired from the server.
5307 * @param cli A cli_state connected to the server.
5308 * @param mem_ctx Talloc context, destroyed on completion of the function.
5309 * @param argc Standard main() style argc.
5310 * @param argv Standard main() style argv. Initial components are already
5313 * @return normal NTSTATUS return code.
5316 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5317 const DOM_SID
*domain_sid
,
5318 const char *domain_name
,
5319 struct cli_state
*cli
,
5320 struct rpc_pipe_client
*pipe_hnd
,
5321 TALLOC_CTX
*mem_ctx
,
5325 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5326 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5328 struct lsa_String lsa_acct_name
;
5330 uint32 acct_flags
=0;
5332 uint32_t access_granted
= 0;
5333 union samr_UserInfo info
;
5334 unsigned int orig_timeout
;
5339 _(" net rpc trustdom add <domain_name> "
5340 "<trust password>\n"));
5341 return NT_STATUS_INVALID_PARAMETER
;
5345 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5348 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5349 return NT_STATUS_NO_MEMORY
;
5352 strupper_m(acct_name
);
5354 init_lsa_String(&lsa_acct_name
, acct_name
);
5356 /* Get samr policy handle */
5357 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5359 MAXIMUM_ALLOWED_ACCESS
,
5361 if (!NT_STATUS_IS_OK(result
)) {
5365 /* Get domain policy handle */
5366 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5368 MAXIMUM_ALLOWED_ACCESS
,
5369 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5371 if (!NT_STATUS_IS_OK(result
)) {
5375 /* This call can take a long time - allow the server to time out.
5376 * 35 seconds should do it. */
5378 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5380 /* Create trusting domain's account */
5381 acb_info
= ACB_NORMAL
;
5382 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5383 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5384 SAMR_USER_ACCESS_SET_PASSWORD
|
5385 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5386 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5388 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
5397 /* And restore our original timeout. */
5398 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5400 if (!NT_STATUS_IS_OK(result
)) {
5401 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5402 acct_name
, nt_errstr(result
));
5407 struct samr_CryptPassword crypt_pwd
;
5409 ZERO_STRUCT(info
.info23
);
5411 init_samr_CryptPassword(argv
[1],
5412 &cli
->user_session_key
,
5415 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5416 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5417 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5418 info
.info23
.password
= crypt_pwd
;
5420 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
5425 if (!NT_STATUS_IS_OK(result
)) {
5426 DEBUG(0,("Could not set trust account password: %s\n",
5427 nt_errstr(result
)));
5433 SAFE_FREE(acct_name
);
5438 * Create interdomain trust account for a remote domain.
5440 * @param argc Standard argc.
5441 * @param argv Standard argv without initial components.
5443 * @return Integer status (0 means success).
5446 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5448 if (argc
> 0 && !c
->display_usage
) {
5449 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5450 rpc_trustdom_add_internals
, argc
, argv
);
5454 _("net rpc trustdom add <domain_name> <trust "
5462 * Remove interdomain trust account from the RPC server.
5463 * All parameters (except for argc and argv) are passed by run_rpc_command
5466 * @param c A net_context structure.
5467 * @param domain_sid The domain sid acquired from the server.
5468 * @param cli A cli_state connected to the server.
5469 * @param mem_ctx Talloc context, destroyed on completion of the function.
5470 * @param argc Standard main() style argc.
5471 * @param argv Standard main() style argv. Initial components are already
5474 * @return normal NTSTATUS return code.
5477 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5478 const DOM_SID
*domain_sid
,
5479 const char *domain_name
,
5480 struct cli_state
*cli
,
5481 struct rpc_pipe_client
*pipe_hnd
,
5482 TALLOC_CTX
*mem_ctx
,
5486 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5487 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5489 DOM_SID trust_acct_sid
;
5490 struct samr_Ids user_rids
, name_types
;
5491 struct lsa_String lsa_acct_name
;
5496 _(" net rpc trustdom del <domain_name>\n"));
5497 return NT_STATUS_INVALID_PARAMETER
;
5501 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5503 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5505 if (acct_name
== NULL
)
5506 return NT_STATUS_NO_MEMORY
;
5508 strupper_m(acct_name
);
5510 /* Get samr policy handle */
5511 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5513 MAXIMUM_ALLOWED_ACCESS
,
5515 if (!NT_STATUS_IS_OK(result
)) {
5519 /* Get domain policy handle */
5520 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5522 MAXIMUM_ALLOWED_ACCESS
,
5523 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5525 if (!NT_STATUS_IS_OK(result
)) {
5529 init_lsa_String(&lsa_acct_name
, acct_name
);
5531 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
5538 if (!NT_STATUS_IS_OK(result
)) {
5539 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5541 acct_name
, nt_errstr(result
) );
5545 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
5547 MAXIMUM_ALLOWED_ACCESS
,
5551 if (!NT_STATUS_IS_OK(result
)) {
5552 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5554 acct_name
, nt_errstr(result
) );
5558 /* append the rid to the domain sid */
5559 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
5563 /* remove the sid */
5565 result
= rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd
, mem_ctx
,
5568 if (!NT_STATUS_IS_OK(result
)) {
5569 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5570 " on user %s failed %s\n"),
5571 acct_name
, nt_errstr(result
) );
5577 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
5580 if (!NT_STATUS_IS_OK(result
)) {
5581 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5583 acct_name
, nt_errstr(result
) );
5587 if (!NT_STATUS_IS_OK(result
)) {
5588 d_printf(_("Could not set trust account password: %s\n"),
5598 * Delete interdomain trust account for a remote domain.
5600 * @param argc Standard argc.
5601 * @param argv Standard argv without initial components.
5603 * @return Integer status (0 means success).
5606 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
5608 if (argc
> 0 && !c
->display_usage
) {
5609 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5610 rpc_trustdom_del_internals
, argc
, argv
);
5614 _("net rpc trustdom del <domain>\n"));
5619 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
5620 struct cli_state
*cli
,
5621 TALLOC_CTX
*mem_ctx
,
5622 const char *domain_name
)
5624 char *dc_name
= NULL
;
5625 const char *buffer
= NULL
;
5626 struct rpc_pipe_client
*netr
;
5629 /* Use NetServerEnum2 */
5631 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
5633 return NT_STATUS_OK
;
5636 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5637 for domain %s\n", domain_name
));
5639 /* Try netr_GetDcName */
5641 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
5643 if (!NT_STATUS_IS_OK(status
)) {
5647 status
= rpccli_netr_GetDcName(netr
, mem_ctx
,
5654 if (NT_STATUS_IS_OK(status
)) {
5658 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5659 for domain %s\n", domain_name
));
5665 * Establish trust relationship to a trusting domain.
5666 * Interdomain account must already be created on remote PDC.
5668 * @param c A net_context structure.
5669 * @param argc Standard argc.
5670 * @param argv Standard argv without initial components.
5672 * @return Integer status (0 means success).
5675 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
5678 struct cli_state
*cli
= NULL
;
5679 struct sockaddr_storage server_ss
;
5680 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5681 struct policy_handle connect_hnd
;
5682 TALLOC_CTX
*mem_ctx
;
5684 DOM_SID
*domain_sid
;
5689 union lsa_PolicyInformation
*info
= NULL
;
5692 * Connect to \\server\ipc$ as 'our domain' account with password
5695 if (argc
!= 1 || c
->display_usage
) {
5698 _("net rpc trustdom establish <domain_name>\n"));
5702 domain_name
= smb_xstrdup(argv
[0]);
5703 strupper_m(domain_name
);
5705 /* account name used at first is our domain's name with '$' */
5706 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
5709 strupper_m(acct_name
);
5712 * opt_workgroup will be used by connection functions further,
5713 * hence it should be set to remote domain name instead of ours
5715 if (c
->opt_workgroup
) {
5716 c
->opt_workgroup
= smb_xstrdup(domain_name
);
5719 c
->opt_user_name
= acct_name
;
5721 /* find the domain controller */
5722 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
5723 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
5727 /* connect to ipc$ as username/password */
5728 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
5729 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
5731 /* Is it trusting domain account for sure ? */
5732 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5733 nt_errstr(nt_status
)));
5737 /* store who we connected to */
5739 saf_store( domain_name
, pdc_name
);
5742 * Connect to \\server\ipc$ again (this time anonymously)
5745 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
5748 if (NT_STATUS_IS_ERR(nt_status
)) {
5749 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5750 domain_name
, nt_errstr(nt_status
)));
5754 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
5755 "domain %s", domain_name
))) {
5756 DEBUG(0, ("talloc_init() failed\n"));
5761 /* Make sure we're talking to a proper server */
5763 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
5764 if (!NT_STATUS_IS_OK(nt_status
)) {
5766 talloc_destroy(mem_ctx
);
5771 * Call LsaOpenPolicy and LsaQueryInfo
5774 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
5776 if (!NT_STATUS_IS_OK(nt_status
)) {
5777 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
5779 talloc_destroy(mem_ctx
);
5783 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
5785 if (NT_STATUS_IS_ERR(nt_status
)) {
5786 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5787 nt_errstr(nt_status
)));
5789 talloc_destroy(mem_ctx
);
5793 /* Querying info level 5 */
5795 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
5797 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
5799 if (NT_STATUS_IS_ERR(nt_status
)) {
5800 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5801 nt_errstr(nt_status
)));
5803 talloc_destroy(mem_ctx
);
5807 domain_sid
= info
->account_domain
.sid
;
5809 /* There should be actually query info level 3 (following nt serv behaviour),
5810 but I still don't know if it's _really_ necessary */
5813 * Store the password in secrets db
5816 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
5817 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5819 talloc_destroy(mem_ctx
);
5824 * Close the pipes and clean up
5827 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5828 if (NT_STATUS_IS_ERR(nt_status
)) {
5829 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5830 nt_errstr(nt_status
)));
5832 talloc_destroy(mem_ctx
);
5838 talloc_destroy(mem_ctx
);
5840 d_printf(_("Trust to domain %s established\n"), domain_name
);
5845 * Revoke trust relationship to the remote domain.
5847 * @param c A net_context structure.
5848 * @param argc Standard argc.
5849 * @param argv Standard argv without initial components.
5851 * @return Integer status (0 means success).
5854 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
5860 if (argc
< 1 || c
->display_usage
) {
5863 _("net rpc trustdom revoke <domain_name>\n"
5864 " Revoke trust relationship\n"
5865 " domain_name\tName of domain to revoke trust\n"));
5869 /* generate upper cased domain name */
5870 domain_name
= smb_xstrdup(argv
[0]);
5871 strupper_m(domain_name
);
5873 /* delete password of the trust */
5874 if (!pdb_del_trusteddom_pw(domain_name
)) {
5875 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5882 SAFE_FREE(domain_name
);
5886 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
5887 const DOM_SID
*domain_sid
,
5888 const char *domain_name
,
5889 struct cli_state
*cli
,
5890 struct rpc_pipe_client
*pipe_hnd
,
5891 TALLOC_CTX
*mem_ctx
,
5896 if (!sid_to_fstring(str_sid
, domain_sid
)) {
5897 return NT_STATUS_UNSUCCESSFUL
;
5899 d_printf("%s\n", str_sid
);
5900 return NT_STATUS_OK
;
5903 static void print_trusted_domain(DOM_SID
*dom_sid
, const char *trusted_dom_name
)
5907 /* convert sid into ascii string */
5908 sid_to_fstring(ascii_sid
, dom_sid
);
5910 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
5913 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
5914 TALLOC_CTX
*mem_ctx
,
5915 struct policy_handle
*pol
,
5917 const char *trusted_dom_name
)
5920 union lsa_TrustedDomainInfo
*info
= NULL
;
5921 char *cleartextpwd
= NULL
;
5922 uint8_t session_key
[16];
5923 DATA_BLOB session_key_blob
;
5924 DATA_BLOB data
= data_blob_null
;
5926 nt_status
= rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd
, mem_ctx
,
5929 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
5931 if (NT_STATUS_IS_ERR(nt_status
)) {
5932 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5933 nt_errstr(nt_status
)));
5937 data
= data_blob(info
->password
.password
->data
,
5938 info
->password
.password
->length
);
5940 if (!rpccli_get_pwd_hash(pipe_hnd
, session_key
)) {
5941 DEBUG(0, ("Could not retrieve password hash\n"));
5945 session_key_blob
= data_blob_const(session_key
, sizeof(session_key
));
5946 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key_blob
);
5948 if (cleartextpwd
== NULL
) {
5949 DEBUG(0,("retrieved NULL password\n"));
5950 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5954 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
5955 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5956 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5960 #ifdef DEBUG_PASSWORD
5961 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5962 "password: [%s]\n", trusted_dom_name
,
5963 sid_string_dbg(&dom_sid
), cleartextpwd
));
5967 SAFE_FREE(cleartextpwd
);
5968 data_blob_free(&data
);
5973 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
5976 /* common variables */
5977 TALLOC_CTX
* mem_ctx
;
5978 struct cli_state
*cli
= NULL
;
5979 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5981 const char *domain_name
= NULL
;
5982 DOM_SID
*queried_dom_sid
;
5983 struct policy_handle connect_hnd
;
5984 union lsa_PolicyInformation
*info
= NULL
;
5986 /* trusted domains listing variables */
5987 unsigned int enum_ctx
= 0;
5989 struct lsa_DomainList dom_list
;
5992 if (c
->display_usage
) {
5994 "net rpc trustdom vampire\n"
5997 _("Vampire trust relationship from remote server"));
6002 * Listing trusted domains (stored in secrets.tdb, if local)
6005 mem_ctx
= talloc_init("trust relationships vampire");
6008 * set domain and pdc name to local samba server (default)
6009 * or to remote one given in command line
6012 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6013 domain_name
= c
->opt_workgroup
;
6014 c
->opt_target_workgroup
= c
->opt_workgroup
;
6016 fstrcpy(pdc_name
, global_myname());
6017 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6018 c
->opt_target_workgroup
= domain_name
;
6021 /* open \PIPE\lsarpc and open policy handle */
6022 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6023 if (!NT_STATUS_IS_OK(nt_status
)) {
6024 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6025 nt_errstr(nt_status
)));
6026 talloc_destroy(mem_ctx
);
6030 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6032 if (!NT_STATUS_IS_OK(nt_status
)) {
6033 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6034 nt_errstr(nt_status
) ));
6036 talloc_destroy(mem_ctx
);
6040 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6042 if (NT_STATUS_IS_ERR(nt_status
)) {
6043 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6044 nt_errstr(nt_status
)));
6046 talloc_destroy(mem_ctx
);
6050 /* query info level 5 to obtain sid of a domain being queried */
6051 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6053 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6056 if (NT_STATUS_IS_ERR(nt_status
)) {
6057 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6058 nt_errstr(nt_status
)));
6060 talloc_destroy(mem_ctx
);
6064 queried_dom_sid
= info
->account_domain
.sid
;
6067 * Keep calling LsaEnumTrustdom over opened pipe until
6068 * the end of enumeration is reached
6071 d_printf(_("Vampire trusted domains:\n\n"));
6074 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6079 if (NT_STATUS_IS_ERR(nt_status
)) {
6080 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6081 nt_errstr(nt_status
)));
6083 talloc_destroy(mem_ctx
);
6087 for (i
= 0; i
< dom_list
.count
; i
++) {
6089 print_trusted_domain(dom_list
.domains
[i
].sid
,
6090 dom_list
.domains
[i
].name
.string
);
6092 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6093 *dom_list
.domains
[i
].sid
,
6094 dom_list
.domains
[i
].name
.string
);
6095 if (!NT_STATUS_IS_OK(nt_status
)) {
6097 talloc_destroy(mem_ctx
);
6103 * in case of no trusted domains say something rather
6104 * than just display blank line
6106 if (!dom_list
.count
) d_printf(_("none\n"));
6108 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6110 /* close this connection before doing next one */
6111 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6112 if (NT_STATUS_IS_ERR(nt_status
)) {
6113 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6114 nt_errstr(nt_status
)));
6116 talloc_destroy(mem_ctx
);
6120 /* close lsarpc pipe and connection to IPC$ */
6123 talloc_destroy(mem_ctx
);
6127 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6129 /* common variables */
6130 TALLOC_CTX
* mem_ctx
;
6131 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6132 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6134 const char *domain_name
= NULL
;
6135 DOM_SID
*queried_dom_sid
;
6136 int ascii_dom_name_len
;
6137 struct policy_handle connect_hnd
;
6138 union lsa_PolicyInformation
*info
= NULL
;
6140 /* trusted domains listing variables */
6141 unsigned int num_domains
, enum_ctx
= 0;
6143 struct lsa_DomainList dom_list
;
6147 /* trusting domains listing variables */
6148 struct policy_handle domain_hnd
;
6149 struct samr_SamArray
*trusts
= NULL
;
6151 if (c
->display_usage
) {
6153 "net rpc trustdom list\n"
6156 _("List incoming and outgoing trust relationships"));
6161 * Listing trusted domains (stored in secrets.tdb, if local)
6164 mem_ctx
= talloc_init("trust relationships listing");
6167 * set domain and pdc name to local samba server (default)
6168 * or to remote one given in command line
6171 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6172 domain_name
= c
->opt_workgroup
;
6173 c
->opt_target_workgroup
= c
->opt_workgroup
;
6175 fstrcpy(pdc_name
, global_myname());
6176 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6177 c
->opt_target_workgroup
= domain_name
;
6180 /* open \PIPE\lsarpc and open policy handle */
6181 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6182 if (!NT_STATUS_IS_OK(nt_status
)) {
6183 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6184 nt_errstr(nt_status
)));
6185 talloc_destroy(mem_ctx
);
6189 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6191 if (!NT_STATUS_IS_OK(nt_status
)) {
6192 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6193 nt_errstr(nt_status
) ));
6195 talloc_destroy(mem_ctx
);
6199 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6201 if (NT_STATUS_IS_ERR(nt_status
)) {
6202 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6203 nt_errstr(nt_status
)));
6205 talloc_destroy(mem_ctx
);
6209 /* query info level 5 to obtain sid of a domain being queried */
6210 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6212 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6215 if (NT_STATUS_IS_ERR(nt_status
)) {
6216 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6217 nt_errstr(nt_status
)));
6219 talloc_destroy(mem_ctx
);
6223 queried_dom_sid
= info
->account_domain
.sid
;
6226 * Keep calling LsaEnumTrustdom over opened pipe until
6227 * the end of enumeration is reached
6230 d_printf(_("Trusted domains list:\n\n"));
6232 found_domain
= false;
6235 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6240 if (NT_STATUS_IS_ERR(nt_status
)) {
6241 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6242 nt_errstr(nt_status
)));
6244 talloc_destroy(mem_ctx
);
6248 for (i
= 0; i
< dom_list
.count
; i
++) {
6249 print_trusted_domain(dom_list
.domains
[i
].sid
,
6250 dom_list
.domains
[i
].name
.string
);
6251 found_domain
= true;
6255 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6258 * in case of no trusted domains say something rather
6259 * than just display blank line
6261 if (!found_domain
) {
6262 d_printf(_("none\n"));
6265 /* close this connection before doing next one */
6266 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6267 if (NT_STATUS_IS_ERR(nt_status
)) {
6268 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6269 nt_errstr(nt_status
)));
6271 talloc_destroy(mem_ctx
);
6275 TALLOC_FREE(pipe_hnd
);
6278 * Listing trusting domains (stored in passdb backend, if local)
6281 d_printf(_("\nTrusting domains list:\n\n"));
6284 * Open \PIPE\samr and get needed policy handles
6286 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6288 if (!NT_STATUS_IS_OK(nt_status
)) {
6289 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6291 talloc_destroy(mem_ctx
);
6296 nt_status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
6298 SAMR_ACCESS_LOOKUP_DOMAIN
,
6300 if (!NT_STATUS_IS_OK(nt_status
)) {
6301 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6302 nt_errstr(nt_status
)));
6304 talloc_destroy(mem_ctx
);
6308 /* SamrOpenDomain - we have to open domain policy handle in order to be
6309 able to enumerate accounts*/
6310 nt_status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
6312 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6315 if (!NT_STATUS_IS_OK(nt_status
)) {
6316 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6317 nt_errstr(nt_status
)));
6319 talloc_destroy(mem_ctx
);
6324 * perform actual enumeration
6327 found_domain
= false;
6329 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6332 nt_status
= rpccli_samr_EnumDomainUsers(pipe_hnd
, mem_ctx
,
6339 if (NT_STATUS_IS_ERR(nt_status
)) {
6340 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6341 nt_errstr(nt_status
)));
6343 talloc_destroy(mem_ctx
);
6347 for (i
= 0; i
< num_domains
; i
++) {
6349 char *str
= CONST_DISCARD(char *, trusts
->entries
[i
].name
.string
);
6351 found_domain
= true;
6354 * get each single domain's sid (do we _really_ need this ?):
6355 * 1) connect to domain's pdc
6356 * 2) query the pdc for domain's sid
6359 /* get rid of '$' tail */
6360 ascii_dom_name_len
= strlen(str
);
6361 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6362 str
[ascii_dom_name_len
- 1] = '\0';
6364 /* set opt_* variables to remote domain */
6366 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6367 c
->opt_target_workgroup
= c
->opt_workgroup
;
6369 d_printf("%-20s", str
);
6371 /* connect to remote domain controller */
6372 nt_status
= net_make_ipc_connection(c
,
6373 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6375 if (NT_STATUS_IS_OK(nt_status
)) {
6376 /* query for domain's sid */
6377 if (run_rpc_command(
6379 &ndr_table_lsarpc
.syntax_id
, 0,
6380 rpc_query_domain_sid
, argc
,
6382 d_printf(_("strange - couldn't get domain's sid\n"));
6384 cli_shutdown(remote_cli
);
6387 d_fprintf(stderr
, _("domain controller is not "
6388 "responding: %s\n"),
6389 nt_errstr(nt_status
));
6390 d_printf(_("couldn't get domain's sid\n"));
6394 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6396 if (!found_domain
) {
6400 /* close opened samr and domain policy handles */
6401 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_hnd
);
6402 if (!NT_STATUS_IS_OK(nt_status
)) {
6403 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6406 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6407 if (!NT_STATUS_IS_OK(nt_status
)) {
6408 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
6411 /* close samr pipe and connection to IPC$ */
6414 talloc_destroy(mem_ctx
);
6419 * Entrypoint for 'net rpc trustdom' code.
6421 * @param argc Standard argc.
6422 * @param argv Standard argv without initial components.
6424 * @return Integer status (0 means success).
6427 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
6429 struct functable func
[] = {
6434 N_("Add trusting domain's account"),
6435 N_("net rpc trustdom add\n"
6436 " Add trusting domain's account")
6442 N_("Remove trusting domain's account"),
6443 N_("net rpc trustdom del\n"
6444 " Remove trusting domain's account")
6448 rpc_trustdom_establish
,
6450 N_("Establish outgoing trust relationship"),
6451 N_("net rpc trustdom establish\n"
6452 " Establish outgoing trust relationship")
6456 rpc_trustdom_revoke
,
6458 N_("Revoke outgoing trust relationship"),
6459 N_("net rpc trustdom revoke\n"
6460 " Revoke outgoing trust relationship")
6466 N_("List in- and outgoing domain trusts"),
6467 N_("net rpc trustdom list\n"
6468 " List in- and outgoing domain trusts")
6472 rpc_trustdom_vampire
,
6474 N_("Vampire trusts from remote server"),
6475 N_("net rpc trustdom vampire\n"
6476 " Vampire trusts from remote server")
6478 {NULL
, NULL
, 0, NULL
, NULL
}
6481 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
6485 * Check if a server will take rpc commands
6486 * @param flags Type of server to connect to (PDC, DMB, localhost)
6487 * if the host is not explicitly specified
6488 * @return bool (true means rpc supported)
6490 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
6492 struct cli_state
*cli
;
6494 struct sockaddr_storage server_ss
;
6495 char *server_name
= NULL
;
6498 /* flags (i.e. server type) may depend on command */
6499 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
6502 if ((cli
= cli_initialise()) == NULL
) {
6506 status
= cli_connect(cli
, server_name
, &server_ss
);
6507 if (!NT_STATUS_IS_OK(status
))
6509 if (!attempt_netbios_session_request(&cli
, global_myname(),
6510 server_name
, &server_ss
))
6512 status
= cli_negprot(cli
);
6513 if (!NT_STATUS_IS_OK(status
))
6515 if (cli
->protocol
< PROTOCOL_NT1
)
6524 /* dump sam database via samsync rpc calls */
6525 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
6526 if (c
->display_usage
) {
6531 _("Dump remote SAM database"));
6535 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6536 NET_FLAGS_ANONYMOUS
,
6537 rpc_samdump_internals
, argc
, argv
);
6540 /* syncronise sam database via samsync rpc calls */
6541 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
6543 struct functable func
[] = {
6548 N_("Dump remote SAM database to ldif"),
6549 N_("net rpc vampire ldif\n"
6550 " Dump remote SAM database to LDIF file or "
6557 N_("Dump remote SAM database to Kerberos Keytab"),
6558 N_("net rpc vampire keytab\n"
6559 " Dump remote SAM database to Kerberos keytab "
6566 N_("Dump remote SAM database to passdb"),
6567 N_("net rpc vampire passdb\n"
6568 " Dump remote SAM database to passdb")
6571 {NULL
, NULL
, 0, NULL
, NULL
}
6575 if (c
->display_usage
) {
6580 _("Vampire remote SAM database"));
6584 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6585 NET_FLAGS_ANONYMOUS
,
6586 rpc_vampire_internals
,
6590 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
6594 * Migrate everything from a print server.
6596 * @param c A net_context structure.
6597 * @param argc Standard main() style argc.
6598 * @param argv Standard main() style argv. Initial components are already
6601 * @return A shell status integer (0 for success).
6603 * The order is important !
6604 * To successfully add drivers the print queues have to exist !
6605 * Applying ACLs should be the last step, because you're easily locked out.
6608 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
6613 if (c
->display_usage
) {
6615 "net rpc printer migrate all\n"
6618 _("Migrate everything from a print server"));
6623 d_printf(_("no server to migrate\n"));
6627 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6628 rpc_printer_migrate_printers_internals
, argc
,
6633 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6634 rpc_printer_migrate_drivers_internals
, argc
,
6639 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6640 rpc_printer_migrate_forms_internals
, argc
, argv
);
6644 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6645 rpc_printer_migrate_settings_internals
, argc
,
6650 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6651 rpc_printer_migrate_security_internals
, argc
,
6657 * Migrate print drivers from a print server.
6659 * @param c A net_context structure.
6660 * @param argc Standard main() style argc.
6661 * @param argv Standard main() style argv. Initial components are already
6664 * @return A shell status integer (0 for success).
6666 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
6669 if (c
->display_usage
) {
6671 "net rpc printer migrate drivers\n"
6674 _("Migrate print-drivers from a print-server"));
6679 d_printf(_("no server to migrate\n"));
6683 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6684 rpc_printer_migrate_drivers_internals
,
6689 * Migrate print-forms from a print-server.
6691 * @param c A net_context structure.
6692 * @param argc Standard main() style argc.
6693 * @param argv Standard main() style argv. Initial components are already
6696 * @return A shell status integer (0 for success).
6698 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
6701 if (c
->display_usage
) {
6703 "net rpc printer migrate forms\n"
6706 _("Migrate print-forms from a print-server"));
6711 d_printf(_("no server to migrate\n"));
6715 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6716 rpc_printer_migrate_forms_internals
,
6721 * Migrate printers from a print-server.
6723 * @param c A net_context structure.
6724 * @param argc Standard main() style argc.
6725 * @param argv Standard main() style argv. Initial components are already
6728 * @return A shell status integer (0 for success).
6730 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
6733 if (c
->display_usage
) {
6735 "net rpc printer migrate printers\n"
6738 _("Migrate printers from a print-server"));
6743 d_printf(_("no server to migrate\n"));
6747 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6748 rpc_printer_migrate_printers_internals
,
6753 * Migrate printer-ACLs from a print-server
6755 * @param c A net_context structure.
6756 * @param argc Standard main() style argc.
6757 * @param argv Standard main() style argv. Initial components are already
6760 * @return A shell status integer (0 for success).
6762 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
6765 if (c
->display_usage
) {
6767 "net rpc printer migrate security\n"
6770 _("Migrate printer-ACLs from a print-server"));
6775 d_printf(_("no server to migrate\n"));
6779 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6780 rpc_printer_migrate_security_internals
,
6785 * Migrate printer-settings from a print-server.
6787 * @param c A net_context structure.
6788 * @param argc Standard main() style argc.
6789 * @param argv Standard main() style argv. Initial components are already
6792 * @return A shell status integer (0 for success).
6794 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
6797 if (c
->display_usage
) {
6799 "net rpc printer migrate settings\n"
6802 _("Migrate printer-settings from a "
6808 d_printf(_("no server to migrate\n"));
6812 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6813 rpc_printer_migrate_settings_internals
,
6818 * 'net rpc printer' entrypoint.
6820 * @param c A net_context structure.
6821 * @param argc Standard main() style argc.
6822 * @param argv Standard main() style argv. Initial components are already
6826 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
6829 /* ouch: when addriver and setdriver are called from within
6830 rpc_printer_migrate_drivers_internals, the printer-queue already
6833 struct functable func
[] = {
6836 rpc_printer_migrate_all
,
6838 N_("Migrate all from remote to local print server"),
6839 N_("net rpc printer migrate all\n"
6840 " Migrate all from remote to local print server")
6844 rpc_printer_migrate_drivers
,
6846 N_("Migrate drivers to local server"),
6847 N_("net rpc printer migrate drivers\n"
6848 " Migrate drivers to local server")
6852 rpc_printer_migrate_forms
,
6854 N_("Migrate froms to local server"),
6855 N_("net rpc printer migrate forms\n"
6856 " Migrate froms to local server")
6860 rpc_printer_migrate_printers
,
6862 N_("Migrate printers to local server"),
6863 N_("net rpc printer migrate printers\n"
6864 " Migrate printers to local server")
6868 rpc_printer_migrate_security
,
6870 N_("Mirgate printer ACLs to local server"),
6871 N_("net rpc printer migrate security\n"
6872 " Mirgate printer ACLs to local server")
6876 rpc_printer_migrate_settings
,
6878 N_("Migrate printer settings to local server"),
6879 N_("net rpc printer migrate settings\n"
6880 " Migrate printer settings to local server")
6882 {NULL
, NULL
, 0, NULL
, NULL
}
6885 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
6890 * List printers on a remote RPC server.
6892 * @param c A net_context structure.
6893 * @param argc Standard main() style argc.
6894 * @param argv Standard main() style argv. Initial components are already
6897 * @return A shell status integer (0 for success).
6899 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
6901 if (c
->display_usage
) {
6903 "net rpc printer list\n"
6906 _("List printers on a remote RPC server"));
6910 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6911 rpc_printer_list_internals
,
6916 * List printer-drivers on a remote RPC server.
6918 * @param c A net_context structure.
6919 * @param argc Standard main() style argc.
6920 * @param argv Standard main() style argv. Initial components are already
6923 * @return A shell status integer (0 for success).
6925 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
6928 if (c
->display_usage
) {
6930 "net rpc printer driver\n"
6933 _("List printer-drivers on a remote RPC server"));
6937 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6938 rpc_printer_driver_list_internals
,
6943 * Publish printer in ADS via MSRPC.
6945 * @param c A net_context structure.
6946 * @param argc Standard main() style argc.
6947 * @param argv Standard main() style argv. Initial components are already
6950 * @return A shell status integer (0 for success).
6952 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
6955 if (c
->display_usage
) {
6957 "net rpc printer publish publish\n"
6960 _("Publish printer in ADS via MSRPC"));
6964 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6965 rpc_printer_publish_publish_internals
,
6970 * Update printer in ADS via MSRPC.
6972 * @param c A net_context structure.
6973 * @param argc Standard main() style argc.
6974 * @param argv Standard main() style argv. Initial components are already
6977 * @return A shell status integer (0 for success).
6979 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
6981 if (c
->display_usage
) {
6983 "net rpc printer publish update\n"
6986 _("Update printer in ADS via MSRPC"));
6990 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6991 rpc_printer_publish_update_internals
,
6996 * UnPublish printer in ADS via MSRPC.
6998 * @param c A net_context structure.
6999 * @param argc Standard main() style argc.
7000 * @param argv Standard main() style argv. Initial components are already
7003 * @return A shell status integer (0 for success).
7005 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7008 if (c
->display_usage
) {
7010 "net rpc printer publish unpublish\n"
7013 _("UnPublish printer in ADS via MSRPC"));
7017 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7018 rpc_printer_publish_unpublish_internals
,
7023 * List published printers via MSRPC.
7025 * @param c A net_context structure.
7026 * @param argc Standard main() style argc.
7027 * @param argv Standard main() style argv. Initial components are already
7030 * @return A shell status integer (0 for success).
7032 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7035 if (c
->display_usage
) {
7037 "net rpc printer publish list\n"
7040 _("List published printers via MSRPC"));
7044 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7045 rpc_printer_publish_list_internals
,
7051 * Publish printer in ADS.
7053 * @param c A net_context structure.
7054 * @param argc Standard main() style argc.
7055 * @param argv Standard main() style argv. Initial components are already
7058 * @return A shell status integer (0 for success).
7060 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7064 struct functable func
[] = {
7067 rpc_printer_publish_publish
,
7069 N_("Publish printer in AD"),
7070 N_("net rpc printer publish publish\n"
7071 " Publish printer in AD")
7075 rpc_printer_publish_update
,
7077 N_("Update printer in AD"),
7078 N_("net rpc printer publish update\n"
7079 " Update printer in AD")
7083 rpc_printer_publish_unpublish
,
7085 N_("Unpublish printer"),
7086 N_("net rpc printer publish unpublish\n"
7087 " Unpublish printer")
7091 rpc_printer_publish_list
,
7093 N_("List published printers"),
7094 N_("net rpc printer publish list\n"
7095 " List published printers")
7097 {NULL
, NULL
, 0, NULL
, NULL
}
7101 if (c
->display_usage
) {
7102 d_printf(_("Usage:\n"));
7103 d_printf(_("net rpc printer publish\n"
7104 " List published printers\n"
7105 " Alias of net rpc printer publish "
7107 net_display_usage_from_functable(func
);
7110 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7111 rpc_printer_publish_list_internals
,
7115 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7121 * Display rpc printer help page.
7123 * @param c A net_context structure.
7124 * @param argc Standard main() style argc.
7125 * @param argv Standard main() style argv. Initial components are already
7128 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7130 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7131 "\tlists all printers on print-server\n\n"));
7132 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7133 "\tlists all printer-drivers on print-server\n\n"));
7134 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7135 "\tpublishes printer settings in Active Directory\n"
7136 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7137 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7138 "\n\tmigrates printers from remote to local server\n\n"));
7139 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7140 "\n\tmigrates printer-settings from remote to local server\n\n"));
7141 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7142 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7143 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7144 "\n\tmigrates printer-forms from remote to local server\n\n"));
7145 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7146 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7147 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7148 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7149 "\tremote to local print-server\n\n"));
7150 net_common_methods_usage(c
, argc
, argv
);
7151 net_common_flags_usage(c
, argc
, argv
);
7153 "\t-v or --verbose\t\t\tgive verbose output\n"
7154 "\t --destination\t\tmigration target server (default: localhost)\n"));
7160 * 'net rpc printer' entrypoint.
7162 * @param c A net_context structure.
7163 * @param argc Standard main() style argc.
7164 * @param argv Standard main() style argv. Initial components are already
7167 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7169 struct functable func
[] = {
7174 N_("List all printers on print server"),
7175 N_("net rpc printer list\n"
7176 " List all printers on print server")
7180 rpc_printer_migrate
,
7182 N_("Migrate printer to local server"),
7183 N_("net rpc printer migrate\n"
7184 " Migrate printer to local server")
7188 rpc_printer_driver_list
,
7190 N_("List printer drivers"),
7191 N_("net rpc printer driver\n"
7192 " List printer drivers")
7196 rpc_printer_publish
,
7198 N_("Publish printer in AD"),
7199 N_("net rpc printer publish\n"
7200 " Publish printer in AD")
7202 {NULL
, NULL
, 0, NULL
, NULL
}
7206 if (c
->display_usage
) {
7207 d_printf(_("Usage:\n"));
7208 d_printf(_("net rpc printer\n"
7209 " List printers\n"));
7210 net_display_usage_from_functable(func
);
7213 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7214 rpc_printer_list_internals
,
7218 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7222 * 'net rpc' entrypoint.
7224 * @param c A net_context structure.
7225 * @param argc Standard main() style argc.
7226 * @param argv Standard main() style argv. Initial components are already
7230 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7232 NET_API_STATUS status
;
7234 struct functable func
[] = {
7239 N_("Modify global audit settings"),
7240 N_("net rpc audit\n"
7241 " Modify global audit settings")
7247 N_("Show basic info about a domain"),
7249 " Show basic info about a domain")
7255 N_("Join a domain"),
7263 N_("Join a domain created in server manager"),
7264 N_("net rpc oldjoin\n"
7265 " Join a domain created in server manager")
7271 N_("Test that a join is valid"),
7272 N_("net rpc testjoin\n"
7273 " Test that a join is valid")
7279 N_("List/modify users"),
7281 " List/modify users")
7287 N_("Change a user password"),
7288 N_("net rpc password\n"
7289 " Change a user password\n"
7290 " Alias for net rpc user password")
7296 N_("List/modify groups"),
7297 N_("net rpc group\n"
7298 " List/modify groups")
7304 N_("List/modify shares"),
7305 N_("net rpc share\n"
7306 " List/modify shares")
7312 N_("List open files"),
7320 N_("List/modify printers"),
7321 N_("net rpc printer\n"
7322 " List/modify printers")
7326 net_rpc_changetrustpw
,
7328 N_("Change trust account password"),
7329 N_("net rpc changetrustpw\n"
7330 " Change trust account password")
7336 N_("Modify domain trusts"),
7337 N_("net rpc trustdom\n"
7338 " Modify domain trusts")
7344 N_("Abort a remote shutdown"),
7345 N_("net rpc abortshutdown\n"
7346 " Abort a remote shutdown")
7352 N_("Shutdown a remote server"),
7353 N_("net rpc shutdown\n"
7354 " Shutdown a remote server")
7360 N_("Dump SAM data of remote NT PDC"),
7361 N_("net rpc samdump\n"
7362 " Dump SAM data of remote NT PDC")
7368 N_("Sync a remote NT PDC's data into local passdb"),
7369 N_("net rpc vampire\n"
7370 " Sync a remote NT PDC's data into local passdb")
7376 N_("Fetch the domain sid into local secrets.tdb"),
7377 N_("net rpc getsid\n"
7378 " Fetch the domain sid into local secrets.tdb")
7384 N_("Manage privileges assigned to SID"),
7385 N_("net rpc rights\n"
7386 " Manage privileges assigned to SID")
7392 N_("Start/stop/query remote services"),
7393 N_("net rpc service\n"
7394 " Start/stop/query remote services")
7400 N_("Manage registry hives"),
7401 N_("net rpc registry\n"
7402 " Manage registry hives")
7408 N_("Open interactive shell on remote server"),
7409 N_("net rpc shell\n"
7410 " Open interactive shell on remote server")
7412 {NULL
, NULL
, 0, NULL
, NULL
}
7415 status
= libnetapi_init(&c
->netapi_ctx
);
7419 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
7420 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
7421 if (c
->opt_kerberos
) {
7422 libnetapi_set_use_kerberos(c
->netapi_ctx
);
7424 if (c
->opt_ccache
) {
7425 libnetapi_set_use_ccache(c
->netapi_ctx
);
7428 return net_run_function(c
, argc
, argv
, "net rpc", func
);